enable redirection natively for android, improves #593

This commit is contained in:
Taner Sener 2022-10-17 20:17:54 +01:00
parent 64585b7697
commit 214eea3fb5
2 changed files with 28 additions and 21 deletions

View File

@ -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();
}
/**

View File

@ -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()));
}
/**