Merge "Some instrumentation of OMXCodec for future investigation and a possibly temporary fix for the green tinge on video playback."
diff --git a/include/media/stagefright/AudioPlayer.h b/include/media/stagefright/AudioPlayer.h
index ed2f7d7..37af032 100644
--- a/include/media/stagefright/AudioPlayer.h
+++ b/include/media/stagefright/AudioPlayer.h
@@ -49,11 +49,9 @@
 
     status_t start(bool sourceAlreadyStarted = false);
 
-    void pause();
+    void pause(bool playPendingSamples = false);
     void resume();
 
-    void stop();
-
     // Returns the timestamp of the last buffer played (in us).
     int64_t getMediaTimeUs();
 
@@ -107,6 +105,8 @@
 
     int64_t getRealTimeUsLocked() const;
 
+    void reset();
+
     AudioPlayer(const AudioPlayer &);
     AudioPlayer &operator=(const AudioPlayer &);
 };
diff --git a/media/libstagefright/AudioPlayer.cpp b/media/libstagefright/AudioPlayer.cpp
index b314114..5ff934d 100644
--- a/media/libstagefright/AudioPlayer.cpp
+++ b/media/libstagefright/AudioPlayer.cpp
@@ -55,7 +55,7 @@
 
 AudioPlayer::~AudioPlayer() {
     if (mStarted) {
-        stop();
+        reset();
     }
 }
 
@@ -165,13 +165,21 @@
     return OK;
 }
 
-void AudioPlayer::pause() {
+void AudioPlayer::pause(bool playPendingSamples) {
     CHECK(mStarted);
 
-    if (mAudioSink.get() != NULL) {
-        mAudioSink->pause();
+    if (playPendingSamples) {
+        if (mAudioSink.get() != NULL) {
+            mAudioSink->stop();
+        } else {
+            mAudioTrack->stop();
+        }
     } else {
-        mAudioTrack->stop();
+        if (mAudioSink.get() != NULL) {
+            mAudioSink->pause();
+        } else {
+            mAudioTrack->pause();
+        }
     }
 }
 
@@ -185,7 +193,7 @@
     }
 }
 
-void AudioPlayer::stop() {
+void AudioPlayer::reset() {
     CHECK(mStarted);
 
     if (mAudioSink.get() != NULL) {
diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp
index 31c03ad..4f5ff75 100644
--- a/media/libstagefright/AwesomePlayer.cpp
+++ b/media/libstagefright/AwesomePlayer.cpp
@@ -579,7 +579,7 @@
         notifyListener_l(
                 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus);
 
-        pause_l();
+        pause_l(true /* at eos */);
 
         mFlags |= AT_EOS;
         return;
@@ -603,7 +603,7 @@
         LOGV("MEDIA_PLAYBACK_COMPLETE");
         notifyListener_l(MEDIA_PLAYBACK_COMPLETE);
 
-        pause_l();
+        pause_l(true /* at eos */);
 
         mFlags |= AT_EOS;
     }
@@ -752,7 +752,7 @@
     return pause_l();
 }
 
-status_t AwesomePlayer::pause_l() {
+status_t AwesomePlayer::pause_l(bool at_eos) {
     if (!(mFlags & PLAYING)) {
         return OK;
     }
@@ -760,7 +760,14 @@
     cancelPlayerEvents(true /* keepBufferingGoing */);
 
     if (mAudioPlayer != NULL) {
-        mAudioPlayer->pause();
+        if (at_eos) {
+            // If we played the audio stream to completion we
+            // want to make sure that all samples remaining in the audio
+            // track's queue are played out.
+            mAudioPlayer->pause(true /* playPendingSamples */);
+        } else {
+            mAudioPlayer->pause();
+        }
     }
 
     mFlags &= ~PLAYING;
diff --git a/media/libstagefright/include/AwesomePlayer.h b/media/libstagefright/include/AwesomePlayer.h
index db98253..e04a24d 100644
--- a/media/libstagefright/include/AwesomePlayer.h
+++ b/media/libstagefright/include/AwesomePlayer.h
@@ -223,7 +223,7 @@
     status_t setDataSource_l(const sp<MediaExtractor> &extractor);
     void reset_l();
     status_t seekTo_l(int64_t timeUs);
-    status_t pause_l();
+    status_t pause_l(bool at_eos = false);
     void initRenderer_l();
     void notifyVideoSize_l();
     void seekAudioIfNecessary_l();
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 8527059..cd9b07e 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -1715,7 +1715,7 @@
         // The first time a track is added we wait
         // for all its buffers to be filled before processing it
         mAudioMixer->setActiveTrack(track->name());
-        if (cblk->framesReady() && (track->isReady() || track->isStopped()) &&
+        if (cblk->framesReady() && track->isReady() &&
                 !track->isPaused() && !track->isTerminated())
         {
             //LOGV("track %d u=%08x, s=%08x [OK] on thread %p", track->name(), cblk->user, cblk->server, this);
@@ -2231,7 +2231,7 @@
 
                 // The first time a track is added we wait
                 // for all its buffers to be filled before processing it
-                if (cblk->framesReady() && (track->isReady() || track->isStopped()) &&
+                if (cblk->framesReady() && track->isReady() &&
                         !track->isPaused() && !track->isTerminated())
                 {
                     //LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
@@ -3039,7 +3039,7 @@
 }
 
 bool AudioFlinger::PlaybackThread::Track::isReady() const {
-    if (mFillingUpStatus != FS_FILLING) return true;
+    if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true;
 
     if (mCblk->framesReady() >= mCblk->frameCount ||
             (mCblk->flags & CBLK_FORCEREADY_MSK)) {