From ada4dc982fac32a57db069341f1f51c6a3bb6cc5 Mon Sep 17 00:00:00 2001 From: tonihei Date: Mon, 24 Jun 2024 05:50:56 -0700 Subject: [PATCH] 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 --- .../video/MediaCodecVideoRendererTest.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java b/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java index 9ed7aa24c1..02fec74843 100644 --- a/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java +++ b/libraries/exoplayer/src/test/java/androidx/media3/exoplayer/video/MediaCodecVideoRendererTest.java @@ -261,6 +261,11 @@ public class MediaCodecVideoRendererTest { /* startPositionUs= */ 0, /* offsetUs= */ 0, /* mediaPeriodId= */ new MediaSource.MediaPeriodId(new Object())); + shadowOf(testMainLooper).idle(); + ArgumentCaptor 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 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