From e8c3c0382d58c8eabf4b96aa555683252c10d569 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 2 Oct 2023 20:30:38 +0200 Subject: Bug 1470 - FFmpeg / JNI: Perform exception check and rethrow for all Java Callbacks as recommended --- src/jogl/native/JoglCommon.c | 45 ++++++++++++++++++++++++---- src/jogl/native/JoglCommon.h | 2 ++ src/jogl/native/libav/ffmpeg_impl_template.c | 8 ++++- 3 files changed, 48 insertions(+), 7 deletions(-) (limited to 'src/jogl') diff --git a/src/jogl/native/JoglCommon.c b/src/jogl/native/JoglCommon.c index e9984ada2..cdd73c14f 100644 --- a/src/jogl/native/JoglCommon.c +++ b/src/jogl/native/JoglCommon.c @@ -46,24 +46,57 @@ void JoglCommon_FatalError(JNIEnv *env, const char* msg, ...) } } -void JoglCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...) +static void JoglCommon_throwNewRuntimeExceptionVA(JNIEnv *env, const char* msg, va_list ap) { char buffer[512]; - va_list ap; if(NULL==_jvmHandle) { JoglCommon_FatalError(env, "JOGL: NULL JVM handle, call JoglCommon_init 1st\n"); return; } + vsnprintf(buffer, sizeof(buffer), msg, ap); + + if(NULL != env) { + (*env)->ThrowNew(env, runtimeExceptionClz, buffer); + } +} + +void JoglCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...) +{ + va_list ap; + if( NULL != msg ) { va_start(ap, msg); - vsnprintf(buffer, sizeof(buffer), msg, ap); + JoglCommon_throwNewRuntimeExceptionVA(env, msg, ap); va_end(ap); + } +} - if(NULL != env) { - (*env)->ThrowNew(env, runtimeExceptionClz, buffer); - } +jboolean JoglCommon_ExceptionCheck0(JNIEnv *env) +{ + if( (*env)->ExceptionCheck(env) ) { + (*env)->ExceptionDescribe(env); + (*env)->ExceptionClear(env); + return JNI_TRUE; + } else { + return JNI_FALSE; + } +} + +jboolean JoglCommon_ExceptionCheck1_throwNewRuntimeException(JNIEnv *env, const char* msg, ...) +{ + va_list ap; + + if( (*env)->ExceptionCheck(env) ) { + (*env)->ExceptionDescribe(env); + (*env)->ExceptionClear(env); + va_start(ap, msg); + JoglCommon_throwNewRuntimeExceptionVA(env, msg, ap); + va_end(ap); + return JNI_TRUE; + } else { + return JNI_FALSE; } } diff --git a/src/jogl/native/JoglCommon.h b/src/jogl/native/JoglCommon.h index 2aeaf7d1d..09d0051e4 100644 --- a/src/jogl/native/JoglCommon.h +++ b/src/jogl/native/JoglCommon.h @@ -38,6 +38,8 @@ jchar* JoglCommon_GetNullTerminatedStringChars(JNIEnv* env, jstring str); void JoglCommon_FatalError(JNIEnv *env, const char* msg, ...); void JoglCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...); +jboolean JoglCommon_ExceptionCheck0(JNIEnv *env); +jboolean JoglCommon_ExceptionCheck1_throwNewRuntimeException(JNIEnv *env, const char* msg, ...); /** * diff --git a/src/jogl/native/libav/ffmpeg_impl_template.c b/src/jogl/native/libav/ffmpeg_impl_template.c index 57c3f9e65..385635fa3 100644 --- a/src/jogl/native/libav/ffmpeg_impl_template.c +++ b/src/jogl/native/libav/ffmpeg_impl_template.c @@ -334,7 +334,9 @@ JNIEXPORT jboolean JNICALL FF_FUNC(initSymbols0) } static int _isAudioFormatSupported(JNIEnv *env, jobject ffmpegMediaPlayer, enum AVSampleFormat aSampleFmt, int32_t aSampleRate, int32_t aChannels) { - return JNI_TRUE == (*env)->CallBooleanMethod(env, ffmpegMediaPlayer, ffmpeg_jni_mid_isAudioFormatSupported, aSampleFmt, aSampleRate, aChannels); + int res = JNI_TRUE == (*env)->CallBooleanMethod(env, ffmpegMediaPlayer, ffmpeg_jni_mid_isAudioFormatSupported, aSampleFmt, aSampleRate, aChannels); + JoglCommon_ExceptionCheck1_throwNewRuntimeException(env, "FFmpeg: Exception occured at isAudioFormatSupported(..)"); + return res; } static void _updateJavaAttributes(JNIEnv *env, FFMPEGToolBasicAV_t* pAV) { if(NULL!=env) { @@ -344,6 +346,7 @@ static void _updateJavaAttributes(JNIEnv *env, FFMPEGToolBasicAV_t* pAV) { pAV->vTexWidth[0], pAV->vTexWidth[1], pAV->vTexWidth[2], pAV->vWidth, pAV->vHeight, pAV->aid, pAV->aSampleFmtOut, pAV->aSampleRateOut, pAV->aChannelsOut, pAV->aFrameSize); + JoglCommon_ExceptionCheck1_throwNewRuntimeException(env, "FFmpeg: Exception occured at setupFFAttributes(..)"); (*env)->CallVoidMethod(env, pAV->ffmpegMediaPlayer, ffmpeg_jni_mid_updateAttributes, pAV->vid, pAV->aid, pAV->vWidth, pAV->vHeight, @@ -351,11 +354,13 @@ static void _updateJavaAttributes(JNIEnv *env, FFMPEGToolBasicAV_t* pAV) { pAV->fps, pAV->frames_video, pAV->frames_audio, pAV->duration, (*env)->NewStringUTF(env, pAV->vcodec), (*env)->NewStringUTF(env, pAV->acodec) ); + JoglCommon_ExceptionCheck1_throwNewRuntimeException(env, "FFmpeg: Exception occured at updateAttributes(..)"); } } static void _setIsGLOriented(JNIEnv *env, FFMPEGToolBasicAV_t* pAV) { if(NULL!=env) { (*env)->CallVoidMethod(env, pAV->ffmpegMediaPlayer, ffmpeg_jni_mid_setIsGLOriented, pAV->vFlipped); + JoglCommon_ExceptionCheck1_throwNewRuntimeException(env, "FFmpeg: Exception occured at setIsGLOriented(..)"); } } @@ -1363,6 +1368,7 @@ JNIEXPORT jint JNICALL FF_FUNC(readNextPacket0) } } (*env)->CallVoidMethod(env, pAV->ffmpegMediaPlayer, ffmpeg_jni_mid_pushSound, pNIOBufferCurrent->nioRef, data_size, pAV->aPTS); + JoglCommon_ExceptionCheck1_throwNewRuntimeException(env, "FFmpeg: Exception occured at pushSound(..)"); } } } else if(pAV->packet->stream_index==pAV->vid) { -- cgit v1.2.3