Merge "NuPlayer: don't restart AudioSink when torn down due to pause timeout." into lmp-dev
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
index d225851..5270efc 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
@@ -966,6 +966,8 @@
                 ALOGV("Tear down audio offload, fall back to s/w path");
                 int64_t positionUs;
                 CHECK(msg->findInt64("positionUs", &positionUs));
+                int32_t reason;
+                CHECK(msg->findInt32("reason", &reason));
                 closeAudioSink();
                 mAudioDecoder.clear();
                 ++mAudioDecoderGeneration;
@@ -977,7 +979,9 @@
                 mOffloadAudio = false;
 
                 performSeek(positionUs, false /* needNotify */);
-                instantiateDecoder(true /* audio */, &mAudioDecoder);
+                if (reason == Renderer::kDueToError) {
+                    instantiateDecoder(true /* audio */, &mAudioDecoder);
+                }
             }
             break;
         }
@@ -1044,6 +1048,11 @@
             } else {
                 ALOGW("resume called when source is gone or not set");
             }
+            // |mAudioDecoder| may have been released due to the pause timeout, so try to re-create
+            // it if needed.
+            if (mFlushingAudio != SHUT_DOWN) {
+                instantiateDecoder(true /* audio */, &mAudioDecoder);
+            }
             if (mRenderer != NULL) {
                 mRenderer->resume();
             } else {
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
index 7e5087f..d6bf1de 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
@@ -274,7 +274,7 @@
 
         case kWhatAudioOffloadTearDown:
         {
-            onAudioOffloadTearDown();
+            onAudioOffloadTearDown(kDueToError);
             break;
         }
 
@@ -285,7 +285,8 @@
             if (generation != mAudioOffloadPauseTimeoutGeneration) {
                 break;
             }
-            onAudioOffloadTearDown();
+            ALOGV("Audio Offload tear down due to pause timeout.");
+            onAudioOffloadTearDown(kDueToTimeout);
             break;
         }
 
@@ -1089,7 +1090,7 @@
     return durationUs;
 }
 
-void NuPlayer::Renderer::onAudioOffloadTearDown() {
+void NuPlayer::Renderer::onAudioOffloadTearDown(AudioOffloadTearDownReason reason) {
     if (mAudioOffloadTornDown) {
         return;
     }
@@ -1110,6 +1111,7 @@
     sp<AMessage> notify = mNotify->dup();
     notify->setInt32("what", kWhatAudioOffloadTearDown);
     notify->setInt64("positionUs", currentPositionUs);
+    notify->setInt32("reason", reason);
     notify->post();
 }
 
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h
index 8e6112b..4237902 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h
@@ -69,6 +69,11 @@
         kWhatAudioOffloadPauseTimeout = 'aOPT',
     };
 
+    enum AudioOffloadTearDownReason {
+        kDueToError = 0,
+        kDueToTimeout,
+    };
+
 protected:
     virtual ~Renderer();
 
@@ -157,7 +162,7 @@
     void onPause();
     void onResume();
     void onSetVideoFrameRate(float fps);
-    void onAudioOffloadTearDown();
+    void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
 
     void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
     void notifyFlushComplete(bool audio);