Skip to content

Commit

Permalink
enable redirection natively for android, improves #593
Browse files Browse the repository at this point in the history
  • Loading branch information
tanersener committed Oct 17, 2022
1 parent 64585b7 commit 214eea3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
45 changes: 27 additions & 18 deletions android/ffmpeg-kit-android-lib/src/main/cpp/ffmpegkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,30 @@ int saf_close(int fd) {
return (*env)->CallStaticIntMethod(env, configClass, safCloseMethod, fd);
}

/**
* Used by JNI methods to enable redirection.
*/
static void enableNativeRedirection() {
mutexLock();

if (redirectionEnabled != 0) {
mutexUnlock();
return;
}
redirectionEnabled = 1;

mutexUnlock();

int rc = pthread_create(&callbackThread, 0, callbackThreadFunction, 0);
if (rc != 0) {
LOGE("Failed to create callback thread (rc=%d).\n", rc);
return;
}

av_log_set_callback(ffmpegkit_log_callback_function);
set_report_callback(ffmpegkit_statistics_callback_function);
}

/**
* Called when 'ffmpegkit' native library is loaded.
*
Expand Down Expand Up @@ -665,6 +689,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
av_set_saf_open(saf_open);
av_set_saf_close(saf_close);

enableNativeRedirection();

return JNI_VERSION_1_6;
}

Expand Down Expand Up @@ -696,24 +722,7 @@ JNIEXPORT jint JNICALL Java_com_arthenica_ffmpegkit_FFmpegKitConfig_getNativeLog
* @param object reference to the class on which this method is invoked
*/
JNIEXPORT void JNICALL Java_com_arthenica_ffmpegkit_FFmpegKitConfig_enableNativeRedirection(JNIEnv *env, jclass object) {
mutexLock();

if (redirectionEnabled != 0) {
mutexUnlock();
return;
}
redirectionEnabled = 1;

mutexUnlock();

int rc = pthread_create(&callbackThread, 0, callbackThreadFunction, 0);
if (rc != 0) {
LOGE("Failed to create callback thread (rc=%d).\n", rc);
return;
}

av_log_set_callback(ffmpegkit_log_callback_function);
set_report_callback(ffmpegkit_statistics_callback_function);
enableNativeRedirection();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ public ParcelFileDescriptor getParcelFileDescriptor() {

NativeLoader.loadFFmpegKit(nativeFFmpegTriedAndFailed);

android.util.Log.i(FFmpegKitConfig.TAG, String.format("Loaded ffmpeg-kit-%s-%s-%s-%s.", NativeLoader.loadPackageName(), NativeLoader.loadAbi(), NativeLoader.loadVersion(), NativeLoader.loadBuildDate()));

uniqueIdGenerator = new AtomicInteger(1);

/* NATIVE LOG LEVEL IS RECEIVED ONLY ON STARTUP */
Expand Down Expand Up @@ -175,7 +173,7 @@ protected boolean removeEldestEntry(Map.Entry<Long, Session> eldest) {
safFileDescriptorMap = new SparseArray<>();
globalLogRedirectionStrategy = LogRedirectionStrategy.PRINT_LOGS_WHEN_NO_CALLBACKS_DEFINED;

NativeLoader.enableRedirection();
android.util.Log.i(FFmpegKitConfig.TAG, String.format("Loaded ffmpeg-kit-%s-%s-%s-%s.", NativeLoader.loadPackageName(), NativeLoader.loadAbi(), NativeLoader.loadVersion(), NativeLoader.loadBuildDate()));
}

/**
Expand Down

0 comments on commit 214eea3

Please sign in to comment.