Commit fc3b91c9 by christosts Committed by Ian Baker

Add assertion in MediaCodec adapter

Add assertion to check an output format has been propagated before
returning an output buffer when operating MediaCodec in asynchronous
mode.

PiperOrigin-RevId: 350534918
parent 456622a2
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.google.android.exoplayer2.mediacodec; package com.google.android.exoplayer2.mediacodec;
import static com.google.android.exoplayer2.util.Assertions.checkState; import static com.google.android.exoplayer2.util.Assertions.checkState;
import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaFormat; import android.media.MediaFormat;
...@@ -155,6 +156,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -155,6 +156,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
} else { } else {
int bufferIndex = availableOutputBuffers.remove(); int bufferIndex = availableOutputBuffers.remove();
if (bufferIndex >= 0) { if (bufferIndex >= 0) {
checkStateNotNull(currentFormat);
MediaCodec.BufferInfo nextBufferInfo = bufferInfos.remove(); MediaCodec.BufferInfo nextBufferInfo = bufferInfos.remove();
bufferInfo.set( bufferInfo.set(
nextBufferInfo.offset, nextBufferInfo.offset,
......
...@@ -175,7 +175,8 @@ public class AsynchronousMediaCodecCallbackTest { ...@@ -175,7 +175,8 @@ public class AsynchronousMediaCodecCallbackTest {
@Test @Test
public void dequeOutputBufferIndex_returnsEnqueuedBuffers() { public void dequeOutputBufferIndex_returnsEnqueuedBuffers() {
// Send two output buffers to the callback. // Send an output format and two output buffers to the callback.
asynchronousMediaCodecCallback.onOutputFormatChanged(codec, createMediaFormat("format0"));
MediaCodec.BufferInfo bufferInfo1 = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo bufferInfo1 = new MediaCodec.BufferInfo();
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 0, bufferInfo1); asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 0, bufferInfo1);
MediaCodec.BufferInfo bufferInfo2 = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo bufferInfo2 = new MediaCodec.BufferInfo();
...@@ -183,6 +184,10 @@ public class AsynchronousMediaCodecCallbackTest { ...@@ -183,6 +184,10 @@ public class AsynchronousMediaCodecCallbackTest {
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 1, bufferInfo2); asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 1, bufferInfo2);
MediaCodec.BufferInfo outBufferInfo = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo outBufferInfo = new MediaCodec.BufferInfo();
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo))
.isEqualTo(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED);
assertThat(asynchronousMediaCodecCallback.getOutputFormat().getString("name"))
.isEqualTo("format0");
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo)).isEqualTo(0); assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo)).isEqualTo(0);
assertBufferInfosEqual(bufferInfo1, outBufferInfo); assertBufferInfosEqual(bufferInfo1, outBufferInfo);
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo)).isEqualTo(1); assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo)).isEqualTo(1);
...@@ -235,8 +240,9 @@ public class AsynchronousMediaCodecCallbackTest { ...@@ -235,8 +240,9 @@ public class AsynchronousMediaCodecCallbackTest {
Looper callbackThreadLooper = callbackThread.getLooper(); Looper callbackThreadLooper = callbackThread.getLooper();
AtomicBoolean flushCompleted = new AtomicBoolean(); AtomicBoolean flushCompleted = new AtomicBoolean();
// Send two output buffers to the callback, then flush(), then send // Send an output format and two output buffers to the callback, then flush(), then send
// another output buffer. // another output buffer.
asynchronousMediaCodecCallback.onOutputFormatChanged(codec, createMediaFormat("format0"));
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 0, bufferInfo); asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 0, bufferInfo);
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 1, bufferInfo); asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 1, bufferInfo);
asynchronousMediaCodecCallback.flushAsync( asynchronousMediaCodecCallback.flushAsync(
...@@ -247,6 +253,10 @@ public class AsynchronousMediaCodecCallbackTest { ...@@ -247,6 +253,10 @@ public class AsynchronousMediaCodecCallbackTest {
MediaCodec.BufferInfo outBufferInfo = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo outBufferInfo = new MediaCodec.BufferInfo();
assertThat(flushCompleted.get()).isTrue(); assertThat(flushCompleted.get()).isTrue();
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo))
.isEqualTo(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED);
assertThat(asynchronousMediaCodecCallback.getOutputFormat().getString("name"))
.isEqualTo("format0");
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo)).isEqualTo(2); assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outBufferInfo)).isEqualTo(2);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment