Merge "To support for switching from a non-null surface/ surface texture to a null one, and vice versa. Fix for bug 5137445."
diff --git a/media/java/android/media/MediaPlayer.java b/media/java/android/media/MediaPlayer.java
index 1bacdbb..434ef14 100644
--- a/media/java/android/media/MediaPlayer.java
+++ b/media/java/android/media/MediaPlayer.java
@@ -607,13 +607,13 @@
 
     /**
      * Sets the {@link SurfaceHolder} to use for displaying the video
-     * portion of the media.  A surface must be set if a display is
-     * needed.  Not calling this method when playing back a video will
-     * result in only the audio track being played.
+     * portion of the media.
      *
      * Either a surface holder or surface must be set if a display or video sink
      * is needed.  Not calling this method or {@link #setTexture(SurfaceTexture)}
      * when playing back a video will result in only the audio track being played.
+     * A null surface holder or surface will result in only the audio track being
+     * played.
      *
      * @param sh the SurfaceHolder to use for video display
      */
@@ -634,6 +634,7 @@
      * the media. This is similar to {@link #setDisplay(SurfaceHolder)}, but does not
      * support {@link #setScreenOnWhilePlaying(boolean)} or {@link #updateSurfaceScreenOn()}.
      * Setting a Surface will un-set any Surface or SurfaceHolder that was previously set.
+     * A null surface will result in only the audio track being played.
      *
      * @param surface The {@link Surface} to be used for the video portion of the media.
      *
diff --git a/media/jni/android_media_MediaPlayer.cpp b/media/jni/android_media_MediaPlayer.cpp
index 5663683..9090daa 100644
--- a/media/jni/android_media_MediaPlayer.cpp
+++ b/media/jni/android_media_MediaPlayer.cpp
@@ -247,9 +247,8 @@
 static void setVideoSurfaceOrSurfaceTexture(
         const sp<MediaPlayer>& mp, JNIEnv *env, jobject thiz, const char *prefix)
 {
-    // The Java MediaPlayer class makes sure that at most one of mSurface and
-    // mParcelSurfaceTexture is non-null.  But just in case, we give priority to
-    // mSurface over mParcelSurfaceTexture.
+    // Both mSurface and mParcelSurfaceTexture could be null.
+    // We give priority to mSurface over mParcelSurfaceTexture.
     jobject surface = env->GetObjectField(thiz, fields.surface);
     if (surface != NULL) {
         sp<Surface> native_surface(get_surface(env, surface));
@@ -263,6 +262,8 @@
                     ParcelSurfaceTexture_getISurfaceTexture(env, parcelSurfaceTexture));
             LOGV("%s: texture=%p", prefix, native_surfaceTexture.get());
             mp->setVideoSurfaceTexture(native_surfaceTexture);
+        } else {
+            mp->setVideoSurfaceTexture(NULL);
         }
     }
 }
diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp
index 67f6c79..9c9bc2d 100644
--- a/media/libstagefright/AwesomePlayer.cpp
+++ b/media/libstagefright/AwesomePlayer.cpp
@@ -1157,6 +1157,8 @@
     mSurface.clear();
     if (surfaceTexture != NULL) {
         setNativeWindow_l(new SurfaceTextureClient(surfaceTexture));
+    } else {
+        setNativeWindow_l(NULL);
     }
 }
 
@@ -1769,7 +1771,8 @@
         }
     }
 
-    if (mVideoRendererIsPreview || mVideoRenderer == NULL) {
+    if ((mNativeWindow != NULL)
+            && (mVideoRendererIsPreview || mVideoRenderer == NULL)) {
         mVideoRendererIsPreview = false;
 
         initRenderer_l();