Crash when calling ffmpeg library (aar) methods


#1

I’m having trouble calling some methods of an Android java library for ffmpeg. I think I successfuly loaded the library because I can console.log the library object:

JS: BuildConfig -> function () { [native code] }
JS: ExecuteBinaryResponseHandler -> function () { [native code] }
JS: FFmpeg -> function () { [native code] }
JS: FFmpegExecuteResponseHandler -> function () { [native code] }
JS: FFmpegLoadBinaryResponseHandler -> function () { [native code] }
JS: LoadBinaryResponseHandler -> function () { [native code] }

This is the code I want to “translate” (under Usage)

This is how far I got (using this nativescript guide) :

// [...]

var MyCustomLoadBinaryResponseHandler = 
com.github.hiteshsondhi88.libffmpeg
.LoadBinaryResponseHandler.extend({
  onStart: function() {
    console.log('Started loading ffmpeg');
  },
  onFailure: function() {
    console.log('Failed loading ffmpeg');
  },
  onSuccess: function() {
    console.log('Successfully loaded ffmpeg');
  },
  onFinish: function() {
    console.log('Finished loading ffmpeg');
  }
});

console.dir(MyCustomLoadBinaryResponseHandler);
//^ this logs the following
//~ JS: === dump(): dumping members ===
//~ JS: "()function () { [native code] }"
//~ JS: === dump(): dumping function and properties names ===
//~ JS: extend()
//~ JS: null()
//~ JS: === dump(): finished ===

var context = app.android.context;

var ffmpeg = 
com.github.hiteshsondhi88.libffmpeg.FFmpeg.getInstance(context);

console.dir(ffmpeg);
//^ this logs the following
//~ JS: === dump(): dumping members ===
//~ JS: {
//~ JS:     "constructor": "constructor()function () { [native code] 
//}"
//~ JS: }
//~ JS: === dump(): dumping function and properties names ===
//~ JS: constructor()
//~ JS: concatenate()
//~ JS: execute()
//~ JS: getDeviceFFmpegVersion()
//~ JS: getLibraryFFmpegVersion()
//~ JS: isFFmpegCommandRunning()
//~ JS: killRunningProcesses()
//~ JS: loadBinary()
//~ JS: setTimeout()
//~ JS: <init>()
//~ JS: clone()
//~ JS: equals()
//~ JS: finalize()
//~ JS: getClass()
//~ JS: hashCode()
//~ JS: notify()
//~ JS: notifyAll()
//~ JS: toString()
//~ JS: wait()
//~ JS: === dump(): finished ===


ffmpeg.loadBinary(
  new MyCustomLoadBinaryResponseHandler()
);

var MyCustomExecuteBinaryResponseHandler = 
com.github.hiteshsondhi88.libffmpeg
.ExecuteBinaryResponseHandler.extend({
  onStart: function() {
    console.log('Started running ffmpeg');
  },
  onProgress: function(thisMessage) {
    console.log(' ffmpeg running');
    console.log(thisMessage);
  },
  onFailure: function(thisMessage) {
    console.log('Failed running ffmpeg');
    console.log(thisMessage);
  },
  onSuccess: function(thisMessage) {
    console.log('Successfully run ffmpeg');
    console.log(thisMessage);
  },
  onFinish: function() {
    console.log('Finished running ffmpeg');
  }
});

//this is where it crashes
ffmpeg.execute('-version', new 
MyCustomExecuteBinaryResponseHandler());

Unfortunately, the whole app crashes with no error message the app and I can’t continue unless I have more information on what is going on. Am I implementing the methods in a wrong way?How do you suggest I continue?

These are the last logs on the console:

09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]     from java.lang.Object com.tns.Runtime.callJSMethodNative(int, int, java.lang.String, int, boolean, java.lang.Object[])
09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethodNative(Native method)
09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1043)
09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethodImpl(Runtime.java:925)
09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethod(Runtime.java:912)
09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethod(Runtime.java:896)
09-06 11:22:58.884 31522 31522 F art     : art/runtime/java_vm_ext.cc:470]   at com.tns.Runtime.callJSMethod(Runtime.java:888)
09-06 11:22:59.021 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethodNative(Native method)
09-06 11:22:59.021 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1043)
09-06 11:22:59.021 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethodImpl(Runtime.java:925)
09-06 11:22:59.021 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethod(Runtime.java:912)
09-06 11:22:59.021 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethod(Runtime.java:896)
09-06 11:22:59.021 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethod(Runtime.java:888)
09-06 11:22:59.022 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethodNative(Native method)
09-06 11:22:59.022 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1043)
09-06 11:22:59.022 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethodImpl(Runtime.java:925)
09-06 11:22:59.022 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethod(Runtime.java:912)
09-06 11:22:59.022 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethod(Runtime.java:896)
09-06 11:22:59.022 31522 31522 F art     : art/runtime/runtime.cc:403]   at com.tns.Runtime.callJSMethod(Runtime.java:888)

#2

Hey, thanks for posting here. Do you have a small sample repo which we could download and allow us to reproduce the problem locally?


#3

Yes, you can find the important files here.


#4

After running the project locally and inspecting the adb logcat I found the following line:

JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void com.github.hiteshsondhi88.libffmpeg.FFmpeg.execute(java.lang.String[], com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteResponseHandler)

As it’s suggesting - execute is not called properly, hence the crash.

The sample source confirms that .execute’s first parameter is a java String array - https://github.com/WritingMinds/ffmpeg-android-java/blob/master/app/src/main/java/com/github/hiteshsondhi88/sampleffmpeg/Home.java#L80

The fix is simple - wrap the -version string in a javascript array like so: ffmpeg.execute(['-version'], new MyCustomExecuteBinaryResponseHandler()); and it starts working properly.

As to why the app crashes abruptly without an exception screen - I’d have to investigate that further, the nativescript runtime should have failed resolving the method when you passed parameters of the incorrect type.


#5

:open_mouth:
I replaced the command string with an array as you suggested, I built it again (the crash is gone) and it works now, even the logs inside the handlers!

Thank you very much Pete!

(here are some victorious logs, I think this is really cool happening so easily with nativescript :grin:)

JS: Started running ffmpeg
JS: page loaded
JS: Successfully loaded ffmpeg
JS: Finished loading ffmpeg
JS: Successfully run ffmpeg
JS: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developersbuilt with gcc 4.8 (GCC)configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=libavutil      55. 17.103 / 55. 17.103libavcodec     57. 24.102 / 57. 24.102libavformat    57. 25.100 / 57. 25.100libavdevice    57.  0.101 / 57.  0.101libavfilter     6. 31.100 /  6. 31.100libswscale      4.  0.100 /  4.  0.100libswresample   2.  0.101 /  2.  0.101libpostproc    54.  0.100 / 54.  0.100
JS: Finished running ffmpeg