media: allow test to continue waiting for complete if there's new encoder output

Bug: 19194926
Change-Id: I2a2cd40dd34cfd8be106ef742d82c3ba4868e3af
diff --git a/tests/tests/media/src/android/media/cts/VideoEncoderTest.java b/tests/tests/media/src/android/media/cts/VideoEncoderTest.java
index c3cf9e9..858e47c 100644
--- a/tests/tests/media/src/android/media/cts/VideoEncoderTest.java
+++ b/tests/tests/media/src/android/media/cts/VideoEncoderTest.java
@@ -552,6 +552,7 @@
             implements SurfaceTexture.OnFrameAvailableListener {
         private static final String TAG = "SurfaceVideoProcessor";
         private boolean mFrameAvailable;
+        private boolean mEncoderIsActive;
         private boolean mGotDecoderEOS;
         private boolean mSignaledEncoderEOS;
 
@@ -594,7 +595,7 @@
                         try {
                             // wait for mFrameAvailable, which is set by onFrameAvailable().
                             // Use a timeout to avoid stalling the test if it doesn't arrive.
-                            if (!mFrameAvailable && !mCompleted) {
+                            if (!mFrameAvailable && !mCompleted && !mEncoderIsActive) {
                                 mCondition.wait(FRAME_TIMEOUT_MS);
                             }
                         } catch (InterruptedException ie) {
@@ -603,6 +604,11 @@
                         if (mCompleted) {
                             break;
                         }
+                        if (mEncoderIsActive) {
+                            mEncoderIsActive = false;
+                            if (DEBUG) Log.d(TAG, "encoder is still active, continue");
+                            continue;
+                        }
                         assertTrue("still waiting for image", mFrameAvailable);
                         if (DEBUG) Log.v(TAG, "got image");
                         info = mInfoOnSurface;
@@ -704,6 +710,12 @@
                 }
             } else if (mediaCodec == mEncoder) {
                 emptyEncoderOutputBuffer(ix, info);
+                synchronized(mCondition) {
+                    if (!mCompleted) {
+                        mEncoderIsActive = true;
+                        mCondition.notifyAll();
+                    }
+                }
             } else {
                 fail("received output buffer on " + mediaCodec.getName());
             }