Merge "MediaSyncTest: increase ramp up time due to initial latency in AudioTrack." into nyc-dev
diff --git a/tests/tests/media/src/android/media/cts/MediaSyncTest.java b/tests/tests/media/src/android/media/cts/MediaSyncTest.java
index e211682..bdd190f 100644
--- a/tests/tests/media/src/android/media/cts/MediaSyncTest.java
+++ b/tests/tests/media/src/android/media/cts/MediaSyncTest.java
@@ -82,8 +82,10 @@
     private boolean mHasVideo = false;
     private boolean mEosAudio = false;
     private boolean mEosVideo = false;
+    private int mTaggedAudioBufferIndex = -1;
     private final Object mConditionEos = new Object();
     private final Object mConditionEosAudio = new Object();
+    private final Object mConditionTaggedAudioBufferIndex = new Object();
 
     private int mNumBuffersReturned = 0;
 
@@ -138,15 +140,23 @@
         return ((!mHasVideo || mEosVideo) && (!mHasAudio || mEosAudio));
     }
 
+    public void onTaggedAudioBufferIndex(Decoder decoder, int index) {
+        synchronized (mConditionTaggedAudioBufferIndex) {
+            if (decoder == mDecoderAudio) {
+                mTaggedAudioBufferIndex = index;
+            }
+        }
+    }
+
     public void onEos(Decoder decoder) {
-        synchronized(mConditionEosAudio) {
+        synchronized (mConditionEosAudio) {
             if (decoder == mDecoderAudio) {
                 mEosAudio = true;
                 mConditionEosAudio.notify();
             }
         }
 
-        synchronized(mConditionEos) {
+        synchronized (mConditionEos) {
             if (decoder == mDecoderVideo) {
                 mEosVideo = true;
             }
@@ -173,7 +183,7 @@
         }
 
         assertTrue("The stream in test file can not be decoded",
-                mDecoderAudio.setup(INPUT_RESOURCE_ID, null, Long.MAX_VALUE));
+                mDecoderAudio.setup(INPUT_RESOURCE_ID, null, Long.MAX_VALUE, NO_TIMESTAMP));
 
         // get audio track.
         mAudioTrack = mDecoderAudio.getAudioTrack();
@@ -222,7 +232,7 @@
         final Object condition = new Object();
 
         mHasAudio = true;
-        if (mDecoderAudio.setup(inputResourceId, null, Long.MAX_VALUE) == false) {
+        if (mDecoderAudio.setup(inputResourceId, null, Long.MAX_VALUE, NO_TIMESTAMP) == false) {
             return true;
         }
 
@@ -283,7 +293,7 @@
         returnedIndex[0] = -1;
 
         mHasAudio = true;
-        if (mDecoderAudio.setup(inputResourceId, null, Long.MAX_VALUE) == false) {
+        if (mDecoderAudio.setup(inputResourceId, null, Long.MAX_VALUE, NO_TIMESTAMP) == false) {
             return true;
         }
 
@@ -417,8 +427,8 @@
             boolean audio,
             boolean video,
             float playbackRate) {
-        // allow 250ms for playback to get to stable state.
-        final int PLAYBACK_RAMP_UP_TIME_MS = 250;
+        // allow 750ms for playback to get to stable state.
+        final int PLAYBACK_RAMP_UP_TIME_US = 750000;
 
         final Object conditionFirstAudioBuffer = new Object();
 
@@ -427,14 +437,16 @@
             mSurface = mMediaSync.createInputSurface();
 
             if (mDecoderVideo.setup(
-                    inputResourceId, mSurface, lastBufferTimestampUs) == false) {
+                    inputResourceId, mSurface, lastBufferTimestampUs, NO_TIMESTAMP) == false) {
                 return true;
             }
             mHasVideo = true;
         }
 
         if (audio) {
-            if (mDecoderAudio.setup(inputResourceId, null, lastBufferTimestampUs) == false) {
+            if (mDecoderAudio.setup(
+                    inputResourceId, null, lastBufferTimestampUs,
+                    PLAYBACK_RAMP_UP_TIME_US) == false) {
                 return true;
             }
 
@@ -452,7 +464,12 @@
                         decoderAudio.releaseOutputBuffer(bufferIndex, NO_TIMESTAMP);
                     }
                     synchronized (conditionFirstAudioBuffer) {
-                        conditionFirstAudioBuffer.notify();
+                        synchronized (mConditionTaggedAudioBufferIndex) {
+                            if (mTaggedAudioBufferIndex >= 0
+                                    && mTaggedAudioBufferIndex == bufferIndex) {
+                                conditionFirstAudioBuffer.notify();
+                            }
+                        }
                     }
                 }
             }, null);
@@ -484,13 +501,6 @@
         }
 
         if (audio) {
-            try {
-                Thread.sleep(PLAYBACK_RAMP_UP_TIME_MS);
-            } catch (InterruptedException e) {
-                Log.i(LOG_TAG, "worker thread is interrupted during sleeping.");
-                return true;
-            }
-
             MediaTimestamp mediaTimestamp = mMediaSync.getTimestamp();
             assertTrue("No timestamp available for starting", mediaTimestamp != null);
             long checkStartTimeRealUs = System.nanoTime() / 1000;
@@ -542,6 +552,7 @@
         private MediaSync mMediaSync = null;
         private boolean mIsAudio = false;
         private long mLastBufferTimestampUs = 0;
+        private long mStartingAudioTimestampUs = NO_TIMESTAMP;
 
         private Surface mSurface = null;
 
@@ -577,7 +588,9 @@
             mIsAudio = isAudio;
         }
 
-        public boolean setup(int inputResourceId, Surface surface, long lastBufferTimestampUs) {
+        public boolean setup(
+                int inputResourceId, Surface surface, long lastBufferTimestampUs,
+                long startingAudioTimestampUs) {
             if (!mIsAudio) {
                 mSurface = surface;
                 // handle video callback in a separate thread as releaseOutputBuffer is blocking
@@ -586,6 +599,7 @@
                 mHandler = new Handler(mHandlerThread.getLooper());
             }
             mLastBufferTimestampUs = lastBufferTimestampUs;
+            mStartingAudioTimestampUs = startingAudioTimestampUs;
             try {
                 // get extrator.
                 String type = mIsAudio ? "audio/" : "video/";
@@ -728,13 +742,18 @@
                 if (info.size > 0) {
                     if (mIsAudio) {
                         ByteBuffer outputByteBuffer = codec.getOutputBuffer(index);
-                        synchronized(mAudioBufferLock) {
+                        synchronized (mAudioBufferLock) {
                             mAudioBuffers.add(new AudioBuffer(outputByteBuffer, index));
                         }
                         mMediaSync.queueAudio(
                                 outputByteBuffer,
                                 index,
                                 info.presentationTimeUs);
+                        if (mStartingAudioTimestampUs >= 0
+                                && info.presentationTimeUs >= mStartingAudioTimestampUs) {
+                            mMediaSyncTest.onTaggedAudioBufferIndex(this, index);
+                            mStartingAudioTimestampUs = NO_TIMESTAMP;
+                        }
                     } else {
                         codec.releaseOutputBuffer(index, info.presentationTimeUs * 1000);
                     }
@@ -753,7 +772,7 @@
         }
 
         public void checkReturnedAudioBuffer(ByteBuffer byteBuffer, int bufferIndex) {
-            synchronized(mAudioBufferLock) {
+            synchronized (mAudioBufferLock) {
                 AudioBuffer audioBuffer = mAudioBuffers.get(0);
                 if (audioBuffer.mByteBuffer != byteBuffer
                         || audioBuffer.mBufferIndex != bufferIndex) {