OSDN Git Service

MediaPlayer: trigger onError instead of exception on stale native notifications
authorRobert Shih <robertshih@google.com>
Thu, 28 May 2015 01:11:44 +0000 (18:11 -0700)
committerRobert Shih <robertshih@google.com>
Mon, 1 Jun 2015 20:50:00 +0000 (13:50 -0700)
This change also reverted commit 2c838fbd87ad5685c0008b419ea02421159b9b70,
"MediaPlayer: add mPreparing to weed out unwanted prepared messages".

Bug: 21266735
Change-Id: I392342519420bee2fde1fe1915c51d24101ca27d

media/java/android/media/MediaPlayer.java
media/jni/android_media_MediaPlayer.cpp

index 668f80a..13714d3 100644 (file)
@@ -77,7 +77,6 @@ import java.util.Map;
 import java.util.Scanner;
 import java.util.Set;
 import java.util.Vector;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.lang.ref.WeakReference;
 
 /**
@@ -624,9 +623,6 @@ public class MediaPlayer implements SubtitleController.Listener
     private int mUsage = -1;
     private boolean mBypassInterruptionPolicy;
 
-    // use AtomicBoolean instead of boolean so we can use the same member both as a flag and a lock.
-    private AtomicBoolean mPreparing = new AtomicBoolean();
-
     /**
      * Default constructor. Consider using one of the create() methods for
      * synchronously instantiating a MediaPlayer from a Uri or resource.
@@ -1166,10 +1162,6 @@ public class MediaPlayer implements SubtitleController.Listener
      * @throws IllegalStateException if it is called in an invalid state
      */
     public void prepare() throws IOException, IllegalStateException {
-        // The synchronous version of prepare also recieves a MEDIA_PREPARED message.
-        synchronized (mPreparing) {
-            mPreparing.set(true);
-        }
         _prepare();
         scanInternalSubtitleTracks();
     }
@@ -1186,14 +1178,7 @@ public class MediaPlayer implements SubtitleController.Listener
      *
      * @throws IllegalStateException if it is called in an invalid state
      */
-    public void prepareAsync() throws IllegalStateException {
-        synchronized (mPreparing) {
-            mPreparing.set(true);
-        }
-        _prepareAsync();
-    }
-
-    private native void _prepareAsync() throws IllegalStateException;
+    public native void prepareAsync() throws IllegalStateException;
 
     /**
      * Starts or resumes playback. If playback had previously been paused,
@@ -1244,9 +1229,6 @@ public class MediaPlayer implements SubtitleController.Listener
      * initialized.
      */
     public void stop() throws IllegalStateException {
-        synchronized (mPreparing) {
-            mPreparing.set(false);
-        }
         stayAwake(false);
         _stop();
     }
@@ -1676,9 +1658,6 @@ public class MediaPlayer implements SubtitleController.Listener
      * at the same time.
      */
     public void release() {
-        synchronized (mPreparing) {
-            mPreparing.set(false);
-        }
         stayAwake(false);
         updateSurfaceScreenOn();
         mOnPreparedListener = null;
@@ -1705,9 +1684,6 @@ public class MediaPlayer implements SubtitleController.Listener
      * data source and calling prepare().
      */
     public void reset() {
-        synchronized (mPreparing) {
-            mPreparing.set(false);
-        }
         mSelectedSubtitleTrackIndex = -1;
         synchronized(mOpenSubtitleSources) {
             for (final InputStream is: mOpenSubtitleSources) {
@@ -2828,11 +2804,15 @@ public class MediaPlayer implements SubtitleController.Listener
             }
             switch(msg.what) {
             case MEDIA_PREPARED:
-                synchronized (mPreparing) {
-                    if (mPreparing.get()) {
-                        scanInternalSubtitleTracks();
-                        mPreparing.set(false);
-                    }
+                try {
+                    scanInternalSubtitleTracks();
+                } catch (RuntimeException e) {
+                    // send error message instead of crashing;
+                    // send error message instead of inlining a call to onError
+                    // to avoid code duplication.
+                    Message msg2 = obtainMessage(
+                            MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null);
+                    sendMessage(msg2);
                 }
                 if (mOnPreparedListener != null)
                     mOnPreparedListener.onPrepared(mMediaPlayer);
@@ -2908,7 +2888,13 @@ public class MediaPlayer implements SubtitleController.Listener
                     Log.i(TAG, "Info (" + msg.arg1 + "," + msg.arg2 + ")");
                     break;
                 case MEDIA_INFO_METADATA_UPDATE:
-                    scanInternalSubtitleTracks();
+                    try {
+                        scanInternalSubtitleTracks();
+                    } catch (RuntimeException e) {
+                        Message msg2 = obtainMessage(
+                                MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null);
+                        sendMessage(msg2);
+                    }
                     // fall through
 
                 case MEDIA_INFO_EXTERNAL_METADATA_UPDATE:
index 9c67278..d8041f4 100644 (file)
@@ -1045,7 +1045,7 @@ static JNINativeMethod gMethods[] = {
     {"_setDataSource",      "(Landroid/media/MediaDataSource;)V",(void *)android_media_MediaPlayer_setDataSourceCallback },
     {"_setVideoSurface",    "(Landroid/view/Surface;)V",        (void *)android_media_MediaPlayer_setVideoSurface},
     {"_prepare",            "()V",                              (void *)android_media_MediaPlayer_prepare},
-    {"_prepareAsync",       "()V",                              (void *)android_media_MediaPlayer_prepareAsync},
+    {"prepareAsync",        "()V",                              (void *)android_media_MediaPlayer_prepareAsync},
     {"_start",              "()V",                              (void *)android_media_MediaPlayer_start},
     {"_stop",               "()V",                              (void *)android_media_MediaPlayer_stop},
     {"getVideoWidth",       "()I",                              (void *)android_media_MediaPlayer_getVideoWidth},