MediaMuxer:Maintain proper state after stop()

MediaMuxer.java: move to stop state even if nativeStop() throws
exception.
android_media_MediaMuxer.java: revert recent change, throw
only runtime exception for any error as it used to be. Log error now.

Bug: 156649746

Test: atest android.media.cts.MediaMuxerTest
      atest android.media.cts.MediaRecorderTest
      atest android.mediav2.cts.MuxerTest

Change-Id: I82e16fcb065a40f25036bbad181798f1fd36132b
diff --git a/media/java/android/media/MediaMuxer.java b/media/java/android/media/MediaMuxer.java
index bbd7399..54675d0 100644
--- a/media/java/android/media/MediaMuxer.java
+++ b/media/java/android/media/MediaMuxer.java
@@ -321,6 +321,21 @@
     @UnsupportedAppUsage
     private long mNativeObject;
 
+    private String convertMuxerStateCodeToString(int aState) {
+        switch (aState) {
+            case MUXER_STATE_UNINITIALIZED:
+                return "UNINITIALIZED";
+            case MUXER_STATE_INITIALIZED:
+                return "INITIALIZED";
+            case MUXER_STATE_STARTED:
+                return "STARTED";
+            case MUXER_STATE_STOPPED:
+                return "STOPPED";
+            default:
+                return "UNKNOWN";
+        }
+    }
+
     /**
      * Constructor.
      * Creates a media muxer that writes to the specified path.
@@ -397,7 +412,7 @@
             nativeSetOrientationHint(mNativeObject, degrees);
         } else {
             throw new IllegalStateException("Can't set rotation degrees due" +
-                    " to wrong state.");
+                    " to wrong state(" + convertMuxerStateCodeToString(mState) + ")");
         }
     }
 
@@ -432,7 +447,8 @@
         if (mState == MUXER_STATE_INITIALIZED && mNativeObject != 0) {
             nativeSetLocation(mNativeObject, latitudex10000, longitudex10000);
         } else {
-            throw new IllegalStateException("Can't set location due to wrong state.");
+            throw new IllegalStateException("Can't set location due to wrong state("
+                                             + convertMuxerStateCodeToString(mState) + ")");
         }
     }
 
@@ -451,7 +467,8 @@
             nativeStart(mNativeObject);
             mState = MUXER_STATE_STARTED;
         } else {
-            throw new IllegalStateException("Can't start due to wrong state.");
+            throw new IllegalStateException("Can't start due to wrong state("
+                                             + convertMuxerStateCodeToString(mState) + ")");
         }
     }
 
@@ -462,10 +479,16 @@
      */
     public void stop() {
         if (mState == MUXER_STATE_STARTED) {
-            nativeStop(mNativeObject);
-            mState = MUXER_STATE_STOPPED;
+            try {
+                nativeStop(mNativeObject);
+            } catch (Exception e) {
+                throw e;
+            } finally {
+                mState = MUXER_STATE_STOPPED;
+            }
         } else {
-            throw new IllegalStateException("Can't stop due to wrong state.");
+            throw new IllegalStateException("Can't stop due to wrong state("
+                                             + convertMuxerStateCodeToString(mState) + ")");
         }
     }
 
diff --git a/media/jni/android_media_MediaMuxer.cpp b/media/jni/android_media_MediaMuxer.cpp
index 0c1e9a2..262ec76 100644
--- a/media/jni/android_media_MediaMuxer.cpp
+++ b/media/jni/android_media_MediaMuxer.cpp
@@ -26,15 +26,11 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-#include <android/api-level.h>
 #include <media/stagefright/foundation/ABuffer.h>
 #include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/foundation/AMessage.h>
-#include <media/stagefright/MediaErrors.h>
 #include <media/stagefright/MediaMuxer.h>
 
-extern "C" int android_get_application_target_sdk_version();
-
 namespace android {
 
 struct fields_t {
@@ -233,31 +229,11 @@
 
     status_t err = muxer->stop();
 
-    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;
-        }
+    if (err != OK) {
+        ALOGE("Error during stop:%d", err);
+        jniThrowException(env, "java/lang/IllegalStateException",
+                    "Error during stop(), muxer would have stopped already");
+        return;
     }
 }