From 214eea3fb5869c32323b4edc23f95fb80e42a286 Mon Sep 17 00:00:00 2001 From: Taner Sener Date: Mon, 17 Oct 2022 20:17:54 +0100 Subject: [PATCH] enable redirection natively for android, improves #593 --- .../src/main/cpp/ffmpegkit.c | 45 +++++++++++-------- .../arthenica/ffmpegkit/FFmpegKitConfig.java | 4 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/android/ffmpeg-kit-android-lib/src/main/cpp/ffmpegkit.c b/android/ffmpeg-kit-android-lib/src/main/cpp/ffmpegkit.c index 6722fa0..585659d 100644 --- a/android/ffmpeg-kit-android-lib/src/main/cpp/ffmpegkit.c +++ b/android/ffmpeg-kit-android-lib/src/main/cpp/ffmpegkit.c @@ -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. * @@ -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; } @@ -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(); } /** diff --git a/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java b/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java index 4c63abd..80ef18b 100644 --- a/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java +++ b/android/ffmpeg-kit-android-lib/src/main/java/com/arthenica/ffmpegkit/FFmpegKitConfig.java @@ -144,8 +144,6 @@ public class FFmpegKitConfig { 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 */ @@ -175,7 +173,7 @@ public class FFmpegKitConfig { 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())); } /**