EncodeDecodeTest: use timeout for decoder.dequeueInputBuffer

Otherwise we might end up in a sitation where we wait forever
to dequeue in input buffer, with the decoder waiting for us to
dequeue an output buffer first.

Bug: 25389130
Change-Id: I39af24da2b11bce93ee6af5e8ed13ac7ceb97e26
diff --git a/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java b/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
index 7c4d1fb..b875719 100644
--- a/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
+++ b/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
@@ -575,7 +575,8 @@
         ByteBuffer[] encoderOutputBuffers = encoder.getOutputBuffers();
         ByteBuffer[] decoderInputBuffers = null;
         ByteBuffer[] decoderOutputBuffers = null;
-        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
+        MediaCodec.BufferInfo decoderInfo = new MediaCodec.BufferInfo();
+        MediaCodec.BufferInfo encoderInfo = new MediaCodec.BufferInfo();
         MediaFormat decoderOutputFormat = null;
         int generateIndex = 0;
         int checkIndex = 0;
@@ -614,9 +615,11 @@
         boolean inputDone = false;
         boolean encoderDone = false;
         boolean outputDone = false;
+        int encoderStatus = -1;
         while (!outputDone) {
             if (VERBOSE) Log.d(TAG, "loop");
 
+
             // If we're not done submitting frames, generate a new one and submit it.  By
             // doing this on every loop we're working to ensure that the encoder always has
             // work to do.
@@ -662,7 +665,10 @@
             //
             // Once we get EOS from the encoder, we don't need to do this anymore.
             if (!encoderDone) {
-                int encoderStatus = encoder.dequeueOutputBuffer(info, TIMEOUT_USEC);
+                MediaCodec.BufferInfo info = encoderInfo;
+                if (encoderStatus < 0) {
+                    encoderStatus = encoder.dequeueOutputBuffer(info, TIMEOUT_USEC);
+                }
                 if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
                     // no output available yet
                     if (VERBOSE) Log.d(TAG, "no output from encoder available");
@@ -686,18 +692,7 @@
                     encodedData.position(info.offset);
                     encodedData.limit(info.offset + info.size);
 
-                    encodedSize += info.size;
-                    if (outputStream != null) {
-                        byte[] data = new byte[info.size];
-                        encodedData.get(data);
-                        encodedData.position(info.offset);
-                        try {
-                            outputStream.write(data);
-                        } catch (IOException ioe) {
-                            Log.w(TAG, "failed writing debug data to file");
-                            throw new RuntimeException(ioe);
-                        }
-                    }
+                    boolean releaseBuffer = false;
                     if (!decoderConfigured) {
                         // Codec config info.  Only expected on first packet.  One way to
                         // handle this is to manually stuff the data into the MediaFormat
@@ -719,21 +714,41 @@
                         if (VERBOSE) Log.d(TAG, "decoder configured (" + info.size + " bytes)");
                     }
                     if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) {
-                        // Get a decoder input buffer, blocking until it's available.
+                        // Get a decoder input buffer
                         assertTrue(decoderConfigured);
-                        int inputBufIndex = decoder.dequeueInputBuffer(-1);
-                        ByteBuffer inputBuf = decoderInputBuffers[inputBufIndex];
-                        inputBuf.clear();
-                        inputBuf.put(encodedData);
-                        decoder.queueInputBuffer(inputBufIndex, 0, info.size,
-                                info.presentationTimeUs, info.flags);
+                        int inputBufIndex = decoder.dequeueInputBuffer(TIMEOUT_USEC);
+                        if (inputBufIndex >= 0) {
+                            ByteBuffer inputBuf = decoderInputBuffers[inputBufIndex];
+                            inputBuf.clear();
+                            inputBuf.put(encodedData);
+                            decoder.queueInputBuffer(inputBufIndex, 0, info.size,
+                                    info.presentationTimeUs, info.flags);
 
-                        encoderDone = (info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
-                        if (VERBOSE) Log.d(TAG, "passed " + info.size + " bytes to decoder"
-                                + (encoderDone ? " (EOS)" : ""));
+                            encoderDone = (info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
+                            if (VERBOSE) Log.d(TAG, "passed " + info.size + " bytes to decoder"
+                                    + (encoderDone ? " (EOS)" : ""));
+                            releaseBuffer = true;
+                        }
+                    } else {
+                        releaseBuffer = true;
+                    }
+                    if (releaseBuffer) {
+                        encodedSize += info.size;
+                        if (outputStream != null) {
+                            byte[] data = new byte[info.size];
+                            encodedData.position(info.offset);
+                            encodedData.get(data);
+                            try {
+                                outputStream.write(data);
+                            } catch (IOException ioe) {
+                                Log.w(TAG, "failed writing debug data to file");
+                                throw new RuntimeException(ioe);
+                            }
+                        }
+                        encoder.releaseOutputBuffer(encoderStatus, false);
+                        encoderStatus = -1;
                     }
 
-                    encoder.releaseOutputBuffer(encoderStatus, false);
                 }
             }
 
@@ -744,6 +759,7 @@
             // If we're decoding to a Surface, we'll get notified here as usual but the
             // ByteBuffer references will be null.  The data is sent to Surface instead.
             if (decoderConfigured) {
+                MediaCodec.BufferInfo info = decoderInfo;
                 int decoderStatus = decoder.dequeueOutputBuffer(info, TIMEOUT_USEC);
                 if (decoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
                     // no output available yet