Commit c6ebb2ca authored by Thomas Guillem's avatar Thomas Guillem Committed by Jean-Baptiste Kempf

mediacodec: factorize exception check

Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent c8549fb5
...@@ -297,6 +297,17 @@ static int jstrcmp(JNIEnv* env, jobject str, const char* str2) ...@@ -297,6 +297,17 @@ static int jstrcmp(JNIEnv* env, jobject str, const char* str2)
return ret; return ret;
} }
static inline bool check_exception( JNIEnv *env )
{
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
return true;
}
else
return false;
}
#define CHECK_EXCEPTION() check_exception( env )
/***************************************************************************** /*****************************************************************************
* OpenDecoder: Create the decoder instance * OpenDecoder: Create the decoder instance
*****************************************************************************/ *****************************************************************************/
...@@ -345,9 +356,8 @@ static int OpenDecoder(vlc_object_t *p_this) ...@@ -345,9 +356,8 @@ static int OpenDecoder(vlc_object_t *p_this)
*(jclass*)((uint8_t*)p_sys + classes[i].offset) = *(jclass*)((uint8_t*)p_sys + classes[i].offset) =
(*env)->FindClass(env, classes[i].name); (*env)->FindClass(env, classes[i].name);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Unable to find class %s", classes[i].name); msg_Warn(p_dec, "Unable to find class %s", classes[i].name);
(*env)->ExceptionClear(env);
goto error; goto error;
} }
} }
...@@ -357,9 +367,8 @@ static int OpenDecoder(vlc_object_t *p_this) ...@@ -357,9 +367,8 @@ static int OpenDecoder(vlc_object_t *p_this)
if (i == 0 || strcmp(members[i].class, members[i - 1].class)) if (i == 0 || strcmp(members[i].class, members[i - 1].class))
last_class = (*env)->FindClass(env, members[i].class); last_class = (*env)->FindClass(env, members[i].class);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Unable to find class %s", members[i].class); msg_Warn(p_dec, "Unable to find class %s", members[i].class);
(*env)->ExceptionClear(env);
goto error; goto error;
} }
...@@ -377,10 +386,9 @@ static int OpenDecoder(vlc_object_t *p_this) ...@@ -377,10 +386,9 @@ static int OpenDecoder(vlc_object_t *p_this)
(*env)->GetFieldID(env, last_class, members[i].name, members[i].sig); (*env)->GetFieldID(env, last_class, members[i].name, members[i].sig);
break; break;
} }
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Unable to find the member %s in %s", msg_Warn(p_dec, "Unable to find the member %s in %s",
members[i].name, members[i].class); members[i].name, members[i].class);
(*env)->ExceptionClear(env);
if (members[i].critical) if (members[i].critical)
goto error; goto error;
} }
...@@ -417,9 +425,8 @@ static int OpenDecoder(vlc_object_t *p_this) ...@@ -417,9 +425,8 @@ static int OpenDecoder(vlc_object_t *p_this)
codec_capabilities = (*env)->CallObjectMethod(env, info, p_sys->get_capabilities_for_type, codec_capabilities = (*env)->CallObjectMethod(env, info, p_sys->get_capabilities_for_type,
(*env)->NewStringUTF(env, mime)); (*env)->NewStringUTF(env, mime));
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodecInfo.getCapabilitiesForType"); msg_Warn(p_dec, "Exception occurred in MediaCodecInfo.getCapabilitiesForType");
(*env)->ExceptionClear(env);
goto loopclean; goto loopclean;
} else if (codec_capabilities) { } else if (codec_capabilities) {
profile_levels = (*env)->GetObjectField(env, codec_capabilities, p_sys->profile_levels_field); profile_levels = (*env)->GetObjectField(env, codec_capabilities, p_sys->profile_levels_field);
...@@ -499,9 +506,8 @@ loopclean: ...@@ -499,9 +506,8 @@ loopclean:
// but not in 4.1 devices. // but not in 4.1 devices.
p_sys->codec = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_class, p_sys->codec = (*env)->CallStaticObjectMethod(env, p_sys->media_codec_class,
p_sys->create_by_codec_name, codec_name); p_sys->create_by_codec_name, codec_name);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.createByCodecName."); msg_Warn(p_dec, "Exception occurred in MediaCodec.createByCodecName.");
(*env)->ExceptionClear(env);
goto error; goto error;
} }
p_sys->allocated = true; p_sys->allocated = true;
...@@ -578,9 +584,8 @@ loopclean: ...@@ -578,9 +584,8 @@ loopclean:
if (surf) { if (surf) {
// Configure MediaCodec with the Android surface. // Configure MediaCodec with the Android surface.
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, surf, NULL, 0); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, surf, NULL, 0);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.configure with an output surface."); msg_Warn(p_dec, "Exception occurred in MediaCodec.configure with an output surface.");
(*env)->ExceptionClear(env);
jni_UnlockAndroidSurface(); jni_UnlockAndroidSurface();
goto error; goto error;
} }
...@@ -593,17 +598,15 @@ loopclean: ...@@ -593,17 +598,15 @@ loopclean:
} }
if (!p_sys->direct_rendering) { if (!p_sys->direct_rendering) {
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, NULL, NULL, 0); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->configure, format, NULL, NULL, 0);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.configure"); msg_Warn(p_dec, "Exception occurred in MediaCodec.configure");
(*env)->ExceptionClear(env);
goto error; goto error;
} }
} }
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->start); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->start);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.start"); msg_Warn(p_dec, "Exception occurred in MediaCodec.start");
(*env)->ExceptionClear(env);
goto error; goto error;
} }
p_sys->started = true; p_sys->started = true;
...@@ -655,18 +658,14 @@ static void CloseDecoder(vlc_object_t *p_this) ...@@ -655,18 +658,14 @@ static void CloseDecoder(vlc_object_t *p_this)
if (p_sys->started) if (p_sys->started)
{ {
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->stop); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->stop);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION())
msg_Err(p_dec, "Exception in MediaCodec.stop"); msg_Err(p_dec, "Exception in MediaCodec.stop");
(*env)->ExceptionClear(env);
}
} }
if (p_sys->allocated) if (p_sys->allocated)
{ {
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->release); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION())
msg_Err(p_dec, "Exception in MediaCodec.release"); msg_Err(p_dec, "Exception in MediaCodec.release");
(*env)->ExceptionClear(env);
}
} }
(*env)->DeleteGlobalRef(env, p_sys->codec); (*env)->DeleteGlobalRef(env, p_sys->codec);
} }
...@@ -709,10 +708,8 @@ static void UnlockPicture(picture_t* p_pic, bool b_render) ...@@ -709,10 +708,8 @@ static void UnlockPicture(picture_t* p_pic, bool b_render)
JNIEnv *env = NULL; JNIEnv *env = NULL;
jni_attach_thread(&env, THREAD_NAME); jni_attach_thread(&env, THREAD_NAME);
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_index, b_render); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_index, b_render);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION())
msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (DisplayBuffer)"); msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (DisplayBuffer)");
(*env)->ExceptionClear(env);
}
jni_detach_thread(); jni_detach_thread();
p_picsys->priv.hw.b_valid = false; p_picsys->priv.hw.b_valid = false;
...@@ -762,9 +759,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -762,9 +759,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
while (1) { while (1) {
int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer, int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_output_buffer,
p_sys->buffer_info, timeout); p_sys->buffer_info, timeout);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.dequeueOutputBuffer (GetOutput)"); msg_Err(p_dec, "Exception in MediaCodec.dequeueOutputBuffer (GetOutput)");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
return; return;
} }
...@@ -773,9 +769,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -773,9 +769,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
if (!p_sys->pixel_format) { if (!p_sys->pixel_format) {
msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame"); msg_Warn(p_dec, "Buffers returned before output format is set, dropping frame");
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer"); msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
return; return;
} }
...@@ -789,10 +784,9 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -789,10 +784,9 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
picture_sys_t *p_picsys = p_pic->p_sys; picture_sys_t *p_picsys = p_pic->p_sys;
int i_prev_index = p_picsys->priv.hw.i_index; int i_prev_index = p_picsys->priv.hw.i_index;
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_prev_index, false); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_prev_index, false);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer " \ msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer " \
"(GetOutput, overwriting previous picture)"); "(GetOutput, overwriting previous picture)");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
return; return;
} }
...@@ -859,9 +853,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -859,9 +853,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
} else { } else {
msg_Warn(p_dec, "NewPicture failed"); msg_Warn(p_dec, "NewPicture failed");
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, index, false);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (GetOutput)"); msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (GetOutput)");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
} }
} }
...@@ -875,9 +868,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -875,9 +868,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->output_buffers = (*env)->CallObjectMethod(env, p_sys->codec,
p_sys->get_output_buffers); p_sys->get_output_buffers);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffer (GetOutput)"); msg_Err(p_dec, "Exception in MediaCodec.getOutputBuffer (GetOutput)");
(*env)->ExceptionClear(env);
p_sys->output_buffers = NULL; p_sys->output_buffers = NULL;
p_sys->error_state = true; p_sys->error_state = true;
return; return;
...@@ -886,9 +878,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -886,9 +878,8 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers); p_sys->output_buffers = (*env)->NewGlobalRef(env, p_sys->output_buffers);
} else if (index == INFO_OUTPUT_FORMAT_CHANGED) { } else if (index == INFO_OUTPUT_FORMAT_CHANGED) {
jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format); jobject format = (*env)->CallObjectMethod(env, p_sys->codec, p_sys->get_output_format);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.getOutputFormat (GetOutput)"); msg_Err(p_dec, "Exception in MediaCodec.getOutputFormat (GetOutput)");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
return; return;
} }
...@@ -940,8 +931,7 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t ...@@ -940,8 +931,7 @@ static void GetOutput(decoder_t *p_dec, JNIEnv *env, picture_t **pp_pic, jlong t
p_sys->stride = width; p_sys->stride = width;
if (p_sys->slice_height <= 0) if (p_sys->slice_height <= 0)
p_sys->slice_height = height; p_sys->slice_height = height;
if ((*env)->ExceptionOccurred(env)) CHECK_EXCEPTION();
(*env)->ExceptionClear(env);
ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height, ArchitectureSpecificCopyHooks(p_dec, p_sys->pixel_format, p_sys->slice_height,
p_sys->stride, &p_sys->architecture_specific_data); p_sys->stride, &p_sys->architecture_specific_data);
...@@ -993,9 +983,8 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block) ...@@ -993,9 +983,8 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
InvalidateAllPictures(p_dec); InvalidateAllPictures(p_dec);
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->flush); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->flush);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Warn(p_dec, "Exception occurred in MediaCodec.flush"); msg_Warn(p_dec, "Exception occurred in MediaCodec.flush");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
} }
} }
...@@ -1017,9 +1006,8 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block) ...@@ -1017,9 +1006,8 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
int attempts = 0; int attempts = 0;
while (true) { while (true) {
int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_input_buffer, (jlong) 0); int index = (*env)->CallIntMethod(env, p_sys->codec, p_sys->dequeue_input_buffer, (jlong) 0);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception occurred in MediaCodec.dequeueInputBuffer"); msg_Err(p_dec, "Exception occurred in MediaCodec.dequeueInputBuffer");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
break; break;
} }
...@@ -1090,9 +1078,8 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block) ...@@ -1090,9 +1078,8 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
timestamp_FifoPut(p_sys->timestamp_fifo, p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts); timestamp_FifoPut(p_sys->timestamp_fifo, p_block->i_pts ? VLC_TS_INVALID : p_block->i_dts);
(*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0); (*env)->CallVoidMethod(env, p_sys->codec, p_sys->queue_input_buffer, index, 0, size, ts, 0);
(*env)->DeleteLocalRef(env, buf); (*env)->DeleteLocalRef(env, buf);
if ((*env)->ExceptionOccurred(env)) { if (CHECK_EXCEPTION()) {
msg_Err(p_dec, "Exception in MediaCodec.queueInputBuffer"); msg_Err(p_dec, "Exception in MediaCodec.queueInputBuffer");
(*env)->ExceptionClear(env);
p_sys->error_state = true; p_sys->error_state = true;
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment