Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Segfault if SAF is used with concat demuxer #846

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions android/ffmpeg-kit-android-lib/src/main/cpp/ffmpegkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,21 +564,58 @@ void *callbackThreadFunction() {
}

/**
* Used by saf protocol; is expected to be called from a Java thread, therefore we don't need attach/detach
* Used by saf protocol; If it is called from a Java thread, we don't need attach/detach.
* However it can be called from other threads as well (as it happens for concat demuxer),
* in that case we perform attach & detach.
* Returns file descriptor created for this SAF id or 0 if an error occurs.
*/
int saf_open(int safId) {
JNIEnv *env = NULL;
(*globalVm)->GetEnv(globalVm, (void**) &env, JNI_VERSION_1_6);
return (*env)->CallStaticIntMethod(env, configClass, safOpenMethod, safId);
bool attached = false;
jint getEnvRc = (*globalVm)->GetEnv(globalVm, (void**) &env, JNI_VERSION_1_6);
if (getEnvRc != JNI_OK) {
if (getEnvRc != JNI_EDETACHED) {
LOGE("saf_open failed to GetEnv for class %s with rc %d.\n", configClassName, getEnvRc);
return 0;
}
if ((*globalVm)->AttachCurrentThread(globalVm, &env, NULL) != 0) {
LOGE("saf_open failed to AttachCurrentThread for class %s.\n", configClassName);
return 0;
} else {
attached = true;
}
}
int result = (*env)->CallStaticIntMethod(env, configClass, safOpenMethod, safId);
if (attached) (*globalVm)->DetachCurrentThread(globalVm);
return result;
}

/**
* Used by saf protocol; is expected to be called from a Java thread, therefore we don't need attach/detach
* Used by saf protocol; If it is called from a Java thread, we don't need attach/detach.
* However it can be called from other threads as well (as it happens for concat demuxer),
* in that case we perform attach & detach.
* Returns 1 if the given file descriptor is closed successfully, 0 if an error occurs.
*/
int saf_close(int fd) {
JNIEnv *env = NULL;
(*globalVm)->GetEnv(globalVm, (void**) &env, JNI_VERSION_1_6);
return (*env)->CallStaticIntMethod(env, configClass, safCloseMethod, fd);
bool attached = false;
jint getEnvRc = (*globalVm)->GetEnv(globalVm, (void**) &env, JNI_VERSION_1_6);
if (getEnvRc != JNI_OK) {
if (getEnvRc != JNI_EDETACHED) {
LOGE("saf_close failed to GetEnv for class %s with rc %d.\n", configClassName, getEnvRc);
return 0;
}

if ((*globalVm)->AttachCurrentThread(globalVm, &env, NULL) != 0) {
LOGE("saf_close failed to AttachCurrentThread for class %s.\n", configClassName);
return 0;
} else {
attached = true;
}
}
int result = (*env)->CallStaticIntMethod(env, configClass, safCloseMethod, fd);
if (attached) (*globalVm)->DetachCurrentThread(globalVm);
return result;
}

/**
Expand Down
Loading