media: DecoderTest: make sure we call AudioTrack.stop()

Make sure stop() is called on the AudioTrack once all buffers
have been written to avoid an underrun condition at the end of test.

bug: 22978539.
Change-Id: Ib8710a6416756a9aac179ff106c5107e4ae6dcb1
diff --git a/tests/tests/media/src/android/media/cts/CodecState.java b/tests/tests/media/src/android/media/cts/CodecState.java
index 6fdbd86..de0bdf8 100644
--- a/tests/tests/media/src/android/media/cts/CodecState.java
+++ b/tests/tests/media/src/android/media/cts/CodecState.java
@@ -220,8 +220,12 @@
                 mSawInputEOS = true;
                 // FIX-ME: in tunneled mode we currently use input EOS as output EOS indicator
                 // we should stream duration
-                if (mTunneled && !mIsAudio) {
-                    mSawOutputEOS = true;
+                if (mTunneled) {
+                    if (!mIsAudio) {
+                        mSawOutputEOS = true;
+                    } else if (mAudioTrack != null) {
+                        mAudioTrack.stop();
+                    }
                 }
                 return false;
             }
@@ -260,6 +264,10 @@
                     index, 0 /* offset */, 0 /* sampleSize */,
                     0 /* sampleTime */, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
 
+            if (mTunneled && mAudioTrack != null) {
+                mAudioTrack.stop();
+            }
+
             mAvailableInputBufferIndices.removeFirst();
         }
 
@@ -271,7 +279,9 @@
         // b/9250789
         Log.d(TAG, "CodecState::onOutputFormatChanged " + mime);
 
+        mIsAudio = false;
         if (mime.startsWith("audio/")) {
+            mIsAudio = true;
             int sampleRate =
                 mOutputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
 
@@ -315,6 +325,9 @@
 
             mSawOutputEOS = true;
 
+            if (mAudioTrack != null && !mTunneled) {
+                mAudioTrack.stop();
+            }
             return false;
         }
 
diff --git a/tests/tests/media/src/android/media/cts/NonBlockingAudioTrack.java b/tests/tests/media/src/android/media/cts/NonBlockingAudioTrack.java
index 3847252..4a4e807 100644
--- a/tests/tests/media/src/android/media/cts/NonBlockingAudioTrack.java
+++ b/tests/tests/media/src/android/media/cts/NonBlockingAudioTrack.java
@@ -43,6 +43,7 @@
     private int mSampleRate;
     private int mNumBytesQueued = 0;
     private LinkedList<QueueElement> mQueue = new LinkedList<QueueElement>();
+    private boolean mStopped;
 
     public NonBlockingAudioTrack(int sampleRate, int channelCount, boolean hwAvSync,
                     int audioSessionId) {
@@ -107,14 +108,17 @@
     }
 
     public void play() {
+        mStopped = false;
         mAudioTrack.play();
     }
 
     public void stop() {
-        mAudioTrack.stop();
-
-        mQueue.clear();
-        mNumBytesQueued = 0;
+        if (mQueue.isEmpty()) {
+            mAudioTrack.stop();
+            mNumBytesQueued = 0;
+        } else {
+            mStopped = true;
+        }
     }
 
     public void pause() {
@@ -128,6 +132,7 @@
         mAudioTrack.flush();
         mQueue.clear();
         mNumBytesQueued = 0;
+        mStopped = false;
     }
 
     public void release() {
@@ -135,6 +140,7 @@
         mNumBytesQueued = 0;
         mAudioTrack.release();
         mAudioTrack = null;
+        mStopped = false;
     }
 
     public void process() {
@@ -153,6 +159,11 @@
             }
             mQueue.removeFirst();
         }
+        if (mStopped) {
+            mAudioTrack.stop();
+            mNumBytesQueued = 0;
+            mStopped = false;
+        }
     }
 
     public int getPlayState() {