Merge "Properly honour any pending seek request when reading the first buffer after"
diff --git a/media/libstagefright/AudioPlayer.cpp b/media/libstagefright/AudioPlayer.cpp
index 69f9c23..dd69e6b 100644
--- a/media/libstagefright/AudioPlayer.cpp
+++ b/media/libstagefright/AudioPlayer.cpp
@@ -84,7 +84,13 @@
 
     CHECK(mFirstBuffer == NULL);
 
-    mFirstBufferResult = mSource->read(&mFirstBuffer);
+    MediaSource::ReadOptions options;
+    if (mSeeking) {
+        options.setSeekTo(mSeekTimeUs);
+        mSeeking = false;
+    }
+
+    mFirstBufferResult = mSource->read(&mFirstBuffer, &options);
     if (mFirstBufferResult == INFO_FORMAT_CHANGED) {
         LOGV("INFO_FORMAT_CHANGED!!!");
 
diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp
index cccd0b7..dcc268f 100644
--- a/media/libstagefright/AwesomePlayer.cpp
+++ b/media/libstagefright/AwesomePlayer.cpp
@@ -831,6 +831,8 @@
     if (!(mFlags & AUDIOPLAYER_STARTED)) {
         mFlags |= AUDIOPLAYER_STARTED;
 
+        bool wasSeeking = mAudioPlayer->isSeeking();
+
         // We've already started the MediaSource in order to enable
         // the prefetcher to read its data.
         status_t err = mAudioPlayer->start(
@@ -840,6 +842,13 @@
             notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
             return err;
         }
+
+        if (wasSeeking) {
+            CHECK(!mAudioPlayer->isSeeking());
+
+            // We will have finished the seek while starting the audio player.
+            postAudioSeekComplete_l();
+        }
     } else {
         mAudioPlayer->resume();
     }
@@ -1957,6 +1966,10 @@
 
 void AwesomePlayer::postAudioSeekComplete() {
     Mutex::Autolock autoLock(mLock);
+    postAudioSeekComplete_l();
+}
+
+void AwesomePlayer::postAudioSeekComplete_l() {
     postCheckAudioStatusEvent_l(0 /* delayUs */);
 }
 
diff --git a/media/libstagefright/include/AwesomePlayer.h b/media/libstagefright/include/AwesomePlayer.h
index fd3ddf7..835d2bb 100644
--- a/media/libstagefright/include/AwesomePlayer.h
+++ b/media/libstagefright/include/AwesomePlayer.h
@@ -281,6 +281,7 @@
     void ensureCacheIsFetching_l();
 
     status_t startAudioPlayer_l();
+    void postAudioSeekComplete_l();
 
     void shutdownVideoDecoder_l();
     void setNativeWindow_l(const sp<ANativeWindow> &native);