Merge "Prefix MPEG4-generic audio data with ADTS headers" into jb-dev
diff --git a/include/media/stagefright/AudioPlayer.h b/include/media/stagefright/AudioPlayer.h
index a94b16e..1dc408f 100644
--- a/include/media/stagefright/AudioPlayer.h
+++ b/include/media/stagefright/AudioPlayer.h
@@ -100,6 +100,7 @@
     bool mAllowDeepBuffering;       // allow audio deep audio buffers. Helps with low power audio
                                     // playback but implies high latency
     AwesomePlayer *mObserver;
+    int64_t mPinnedTimeUs;
 
     static void AudioCallback(int event, void *user, void *info);
     void AudioCallback(int event, void *info);
diff --git a/media/libstagefright/AudioPlayer.cpp b/media/libstagefright/AudioPlayer.cpp
index f729a78..6b1be1f 100644
--- a/media/libstagefright/AudioPlayer.cpp
+++ b/media/libstagefright/AudioPlayer.cpp
@@ -54,7 +54,8 @@
       mFirstBuffer(NULL),
       mAudioSink(audioSink),
       mAllowDeepBuffering(allowDeepBuffering),
-      mObserver(observer) {
+      mObserver(observer),
+      mPinnedTimeUs(-1ll) {
 }
 
 AudioPlayer::~AudioPlayer() {
@@ -141,9 +142,10 @@
                 mFirstBuffer = NULL;
             }
 
-            if (!sourceAlreadyStarted) {
-                mSource->stop();
-            }
+            // At this point, source already got started
+            // Stop the source when error is found.
+            mSource->stop();
+            mSource = NULL;
 
             return err;
         }
@@ -173,9 +175,9 @@
                 mFirstBuffer = NULL;
             }
 
-            if (!sourceAlreadyStarted) {
-                mSource->stop();
-            }
+            // At this point, source already got started
+            // Stop the source when error is found.
+            mSource->stop();
 
             return err;
         }
@@ -187,6 +189,7 @@
     }
 
     mStarted = true;
+    mPinnedTimeUs = -1ll;
 
     return OK;
 }
@@ -209,6 +212,8 @@
         } else {
             mAudioTrack->pause();
         }
+
+        mPinnedTimeUs = ALooper::GetNowUs();
     }
 }
 
@@ -490,6 +495,12 @@
         Mutex::Autolock autoLock(mLock);
         mNumFramesPlayed += size_done / mFrameSize;
         mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
+
+        if (mReachedEOS) {
+            mPinnedTimeUs = mNumFramesPlayedSysTimeUs;
+        } else {
+            mPinnedTimeUs = -1ll;
+        }
     }
 
     if (postEOS) {
@@ -516,7 +527,14 @@
     // Compensate for large audio buffers, updates of mNumFramesPlayed
     // are less frequent, therefore to get a "smoother" notion of time we
     // compensate using system time.
-    int64_t diffUs = ALooper::GetNowUs() - mNumFramesPlayedSysTimeUs;
+    int64_t diffUs;
+    if (mPinnedTimeUs >= 0ll) {
+        diffUs = mPinnedTimeUs;
+    } else {
+        diffUs = ALooper::GetNowUs();
+    }
+
+    diffUs -= mNumFramesPlayedSysTimeUs;
 
     return result + diffUs;
 }
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC2.cpp b/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
index b0d305b..4000686 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
+++ b/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
@@ -177,11 +177,11 @@
             } else {
                 pcmParams->nChannels = mStreamInfo->numChannels;
                 pcmParams->nSamplingRate = mStreamInfo->sampleRate;
+                ALOGI("Sampling rate: %lu, channels: %lu",
+                      pcmParams->nSamplingRate,
+                      pcmParams->nChannels);
             }
 
-            if (!pcmParams->nChannels) ALOGW("Audio contains 0 channels");
-            if (!pcmParams->nSamplingRate) ALOGW("Sampling rate of 0 Hz");
-
             return OMX_ErrorNone;
         }