Fix flakiness in MediaCodecVideoRendererTest

The test is flaky because the decoding process in the renderer
depends on some timing from MediaCodec beyond our control and
the new keyframe added in the test is sometimes 'dropped' when
it arrives too late.

We can fix this by controlling the test progress a bit more
tightly: first rendering with the same current time until the
key frame is processed and then start increasing the time
until we've reached the end.

#cherrypick

PiperOrigin-RevId: 646064352
This commit is contained in:
tonihei 2024-06-24 05:50:56 -07:00 committed by Copybara-Service
parent e591c37b1e
commit ada4dc982f

View File

@ -261,6 +261,11 @@ public class MediaCodecVideoRendererTest {
/* startPositionUs= */ 0,
/* offsetUs= */ 0,
/* mediaPeriodId= */ new MediaSource.MediaPeriodId(new Object()));
shadowOf(testMainLooper).idle();
ArgumentCaptor<DecoderCounters> argumentDecoderCounters =
ArgumentCaptor.forClass(DecoderCounters.class);
verify(eventListener).onVideoEnabled(argumentDecoderCounters.capture());
DecoderCounters decoderCounters = argumentDecoderCounters.getValue();
mediaCodecVideoRenderer.start();
mediaCodecVideoRenderer.render(0, SystemClock.elapsedRealtime() * 1000);
@ -276,18 +281,18 @@ public class MediaCodecVideoRendererTest {
END_OF_STREAM_ITEM));
fakeSampleStream.writeData(/* startPositionUs= */ 0);
mediaCodecVideoRenderer.setCurrentStreamFinal();
// Render until the new keyframe has been processed and then increase time to reach the end.
while (decoderCounters.renderedOutputBufferCount < 2) {
mediaCodecVideoRenderer.render(posUs, SystemClock.elapsedRealtime() * 1000);
}
while (!mediaCodecVideoRenderer.isEnded()) {
mediaCodecVideoRenderer.render(posUs, SystemClock.elapsedRealtime() * 1000);
posUs += 10_000;
}
shadowOf(testMainLooper).idle();
ArgumentCaptor<DecoderCounters> argumentDecoderCounters =
ArgumentCaptor.forClass(DecoderCounters.class);
verify(eventListener).onVideoEnabled(argumentDecoderCounters.capture());
assertThat(argumentDecoderCounters.getValue().renderedOutputBufferCount).isEqualTo(3);
assertThat(argumentDecoderCounters.getValue().droppedInputBufferCount).isEqualTo(1);
assertThat(argumentDecoderCounters.getValue().droppedToKeyframeCount).isEqualTo(1);
assertThat(decoderCounters.renderedOutputBufferCount).isEqualTo(3);
assertThat(decoderCounters.droppedInputBufferCount).isEqualTo(1);
assertThat(decoderCounters.droppedToKeyframeCount).isEqualTo(1);
}
@Test