Implement missing pause/resume support for NuPlayer.

Change-Id: Ia3f1ce521dd4b70e134ef03aa7c9db8e8d39a134
related-to-bug: 2368598
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
index 5833697..93e5c14 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
@@ -42,7 +42,8 @@
       mFlushingVideo(false),
       mHasAudio(mAudioSink != NULL),
       mHasVideo(true),
-      mSyncQueues(mHasAudio && mHasVideo) {
+      mSyncQueues(mHasAudio && mHasVideo),
+      mPaused(false) {
 }
 
 NuPlayer::Renderer::~Renderer() {
@@ -93,6 +94,14 @@
     mSyncQueues = mHasAudio && mHasVideo;
 }
 
+void NuPlayer::Renderer::pause() {
+    (new AMessage(kWhatPause, id()))->post();
+}
+
+void NuPlayer::Renderer::resume() {
+    (new AMessage(kWhatResume, id()))->post();
+}
+
 void NuPlayer::Renderer::onMessageReceived(const sp<AMessage> &msg) {
     switch (msg->what()) {
         case kWhatDrainAudioQueue:
@@ -151,6 +160,18 @@
             break;
         }
 
+        case kWhatPause:
+        {
+            onPause();
+            break;
+        }
+
+        case kWhatResume:
+        {
+            onResume();
+            break;
+        }
+
         default:
             TRESPASS();
             break;
@@ -158,7 +179,7 @@
 }
 
 void NuPlayer::Renderer::postDrainAudioQueue() {
-    if (mDrainAudioQueuePending || mSyncQueues) {
+    if (mDrainAudioQueuePending || mSyncQueues || mPaused) {
         return;
     }
 
@@ -254,7 +275,7 @@
 }
 
 void NuPlayer::Renderer::postDrainVideoQueue() {
-    if (mDrainVideoQueuePending || mSyncQueues) {
+    if (mDrainVideoQueuePending || mSyncQueues || mPaused) {
         return;
     }
 
@@ -528,5 +549,39 @@
     notify->post();
 }
 
+void NuPlayer::Renderer::onPause() {
+    CHECK(!mPaused);
+
+    mDrainAudioQueuePending = false;
+    ++mAudioQueueGeneration;
+
+    mDrainVideoQueuePending = false;
+    ++mVideoQueueGeneration;
+
+    if (mHasAudio) {
+        mAudioSink->pause();
+    }
+
+    mPaused = true;
+}
+
+void NuPlayer::Renderer::onResume() {
+    CHECK(mPaused);
+
+    if (mHasAudio) {
+        mAudioSink->start();
+    }
+
+    mPaused = false;
+
+    if (!mAudioQueue.empty()) {
+        postDrainAudioQueue();
+    }
+
+    if (!mVideoQueue.empty()) {
+        postDrainVideoQueue();
+    }
+}
+
 }  // namespace android