dropbox / djinni

A tool for generating cross-language type declarations and interface bindings.
Apache License 2.0
2.88k stars 488 forks source link

Problem running multiple processes on a android device #418

Closed ingviso closed 5 years ago

ingviso commented 5 years ago

Hello

This is more of a question then a bug.

I have a react-native app that processes images using open-cv, the image processing code is written in C++, the process uses opencv and bridged to JS using djinni. Each time a picture is taken using the app the image gets processed and saved back on the phone. When I run this on a iOS device I'm able to call the image processing multiple times, which allows me to take picture multiple times and the images get processed in the background.

But the issue is when the picture is taken on a Android device then the UI gets stuck until the image processing has finished for each picture as if the UI thread gets taken over.

The JS code is the same for both Android and IOS.

var imageCorrection = NativeModules.ImageProcessing;
imageCorrection.run( data.uri.replace("file://", "" ), mediaType,  processedPathID);

The imageprocessingModule.java is only

package com.processingProject;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;

import proc.imageProcessing.ImageProcessing;

class ImageProcessingModule extends ReactContextBaseJavaModule {
    public ImageProcessingModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "ImageProcessing";
    }

    @ReactMethod
    public void run(String inputUri, String mediaType, String outputUri, Promise promise) {
        ImageProcessing imageProcessingInterface = ImageProcessing.create();
        try {
            String response = imageProcessingInterface.imageProcessing(inputUri, mediaType, outputUri);
            if (!response.isEmpty()) {
                promise.resolve(response);
            } else {
                throw new Exception("Error with ImageProcessing");
            }
        } catch (Exception e) {
            promise.reject(e);
        }
    }
}

The iOS side is:

#import "ImageProcessing.h"
#import "OCImageProcessing.h"

@implementation ImageProcessing {
    OCImageProcessing *_imageprocessingInterface;
}

RCT_EXPORT_MODULE();

RCT_REMAP_METHOD(run,
                 inputUri:(NSString *)inputUri
                 mediaType:(NSString *)mediaType
                 outputUri:(NSString *)outputUri
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject) {
    _imageprocessingInterface = [OCImageProcessing create];
    NSString *response = [_imageprocessingInterface imageProcessing:inputUri mediaType:mediaType outputUri:outputUri ];
    if (response) {
        resolve(response);
    } else {
        reject(@"get_error", @"Error with ImageProcessing", nil);
    }
}

@end

Not sure if the issue lies in there.

There were some issues implementing opencv for the android side and I had to downgrade the ndk to android-ndk-r17c and run it using gnustl_shared , this relates to this issue

To conclude should the processes in Android and iOS both handle multiple calls the same? Is there something that I can change to fix this so I would be able to take multiple pictures without having have to wait for the process to finish on Android side ?

artwyman commented 5 years ago

Sounds like this question is about a lot of details of React Native and not really related to Djinni, so I'm going to close it out. Djinni is agnostic to thread/process concerns, so I suspect it's not involved in whatever different behavior you're seeing. I don't have the context on React Native to help out, though you might consider joining the mobilecpp Slack community mentioned in the README in this repo, where you may find others with related experience.