From 1ec633dbcb4271de7e2bc9068f5640559c3cbbcc Mon Sep 17 00:00:00 2001 From: Gopalakrishnan Nallasamy Date: Mon, 9 Dec 2019 20:54:42 -0800 Subject: [PATCH] MediaMuxerJNI:Throw relevant exception on error MediaMuxerJNI throws more relevant Exception when native MediaMuxer reports ERROR_IO or ERROR_MALFORMED. Bug: 135685864 Test: 1) atest android.media.cts.MediaMuxerTest 2) Filled up sdcard memory on phone and tried to compose large file using MediaMuxer. Writing was stopped when ::write failed because sdcard memory ran out and corresponding exception thrown in test app.( Pre-allocation was disabled). 3) Same as step 2 with pre-allocation enabled, expected error was reported and corresponding exception was thrown. Change-Id: I07d2872585b5d0a328512c3a6eb5d73b8e4540b3 --- media/jni/android_media_MediaMuxer.cpp | 33 ++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/media/jni/android_media_MediaMuxer.cpp b/media/jni/android_media_MediaMuxer.cpp index f0aa4c3f1ab62..0c1e9a2ad7bd2 100644 --- a/media/jni/android_media_MediaMuxer.cpp +++ b/media/jni/android_media_MediaMuxer.cpp @@ -26,11 +26,15 @@ #include #include +#include #include #include #include +#include #include +extern "C" int android_get_application_target_sdk_version(); + namespace android { struct fields_t { @@ -229,10 +233,31 @@ static void android_media_MediaMuxer_stop(JNIEnv *env, jclass /* clazz */, status_t err = muxer->stop(); - if (err != OK) { - jniThrowException(env, "java/lang/IllegalStateException", - "Failed to stop the muxer"); - return; + if (android_get_application_target_sdk_version() >= __ANDROID_API_R__) { + switch (err) { + case OK: + break; + case ERROR_IO: { + jniThrowException(env, "java/lang/UncheckedIOException", + "Muxer stopped unexpectedly"); + return; + } + case ERROR_MALFORMED: { + jniThrowException(env, "java/io/IOError", + "Failure of reading or writing operation"); + return; + } + default: { + jniThrowException(env, "java/lang/IllegalStateException", + "Failed to stop the muxer"); + return; + } + } + } else { + if (err != OK) { + jniThrowException(env, "java/lang/IllegalStateException", "Failed to stop the muxer"); + return; + } } }