Assert frame counts with tolerance.

MediaCodec decoders sometimes output frames in
the wrong order. Make our asserts more permissive to
reduce noise in tests.

PiperOrigin-RevId: 747414696
This commit is contained in:
dancho 2025-04-14 07:46:44 -07:00 committed by Copybara-Service
parent a2265f1dae
commit f261fe187a
2 changed files with 33 additions and 7 deletions

View File

@ -556,8 +556,12 @@ public class TransformerEndToEndTest {
.build() .build()
.run(testId, editedMediaItem); .run(testId, editedMediaItem);
// Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(result.exportResult.videoFrameCount) assertThat(result.exportResult.videoFrameCount)
.isEqualTo(MP4_ASSET_WITH_INCREASING_TIMESTAMPS_320W_240H_15S.videoFrameCount); .isWithin(2)
.of(MP4_ASSET_WITH_INCREASING_TIMESTAMPS_320W_240H_15S.videoFrameCount);
assertThat(new File(result.filePath).length()).isGreaterThan(0); assertThat(new File(result.filePath).length()).isGreaterThan(0);
} }
@ -583,8 +587,12 @@ public class TransformerEndToEndTest {
.build() .build()
.run(testId, editedMediaItem); .run(testId, editedMediaItem);
// Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(result.exportResult.videoFrameCount) assertThat(result.exportResult.videoFrameCount)
.isEqualTo(MP4_ASSET_WITH_INCREASING_TIMESTAMPS_320W_240H_15S.videoFrameCount); .isWithin(2)
.of(MP4_ASSET_WITH_INCREASING_TIMESTAMPS_320W_240H_15S.videoFrameCount);
assertThat(new File(result.filePath).length()).isGreaterThan(0); assertThat(new File(result.filePath).length()).isGreaterThan(0);
} }

View File

@ -99,7 +99,10 @@ public class TransformerPauseResumeTest {
ExportResult exportResult = result.exportResult; ExportResult exportResult = result.exportResult;
assertThat(exportResult.processedInputs).hasSize(4); assertThat(exportResult.processedInputs).hasSize(4);
assertThat(exportResult.videoFrameCount).isEqualTo(MP4_ASSET_FRAME_COUNT); // Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(exportResult.videoFrameCount).isWithin(2).of(MP4_ASSET_FRAME_COUNT);
// The first processed media item corresponds to remuxing previous output video. // The first processed media item corresponds to remuxing previous output video.
assertThat(exportResult.processedInputs.get(0).audioDecoderName).isNull(); assertThat(exportResult.processedInputs.get(0).audioDecoderName).isNull();
assertThat(exportResult.processedInputs.get(0).videoDecoderName).isNull(); assertThat(exportResult.processedInputs.get(0).videoDecoderName).isNull();
@ -197,8 +200,12 @@ public class TransformerPauseResumeTest {
.isEqualTo(exportResultWithoutResume.audioEncoderName); .isEqualTo(exportResultWithoutResume.audioEncoderName);
assertThat(exportResultWithResume.videoEncoderName) assertThat(exportResultWithResume.videoEncoderName)
.isEqualTo(exportResultWithoutResume.videoEncoderName); .isEqualTo(exportResultWithoutResume.videoEncoderName);
// Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(exportResultWithResume.videoFrameCount) assertThat(exportResultWithResume.videoFrameCount)
.isEqualTo(exportResultWithoutResume.videoFrameCount); .isWithin(2)
.of(exportResultWithoutResume.videoFrameCount);
// TODO: b/306595508 - Remove this expected difference once inconsistent behaviour of audio // TODO: b/306595508 - Remove this expected difference once inconsistent behaviour of audio
// encoder is fixed. // encoder is fixed.
int maxDiffExpectedInDurationMs = 2; int maxDiffExpectedInDurationMs = 2;
@ -252,8 +259,12 @@ public class TransformerPauseResumeTest {
.isEqualTo(exportResultWithoutResume.audioEncoderName); .isEqualTo(exportResultWithoutResume.audioEncoderName);
assertThat(exportResultWithResume.videoEncoderName) assertThat(exportResultWithResume.videoEncoderName)
.isEqualTo(exportResultWithoutResume.videoEncoderName); .isEqualTo(exportResultWithoutResume.videoEncoderName);
// Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(exportResultWithResume.videoFrameCount) assertThat(exportResultWithResume.videoFrameCount)
.isEqualTo(exportResultWithoutResume.videoFrameCount); .isWithin(2)
.of(exportResultWithoutResume.videoFrameCount);
int maxDiffExpectedInDurationMs = 2; int maxDiffExpectedInDurationMs = 2;
assertThat(exportResultWithResume.durationMs - exportResultWithoutResume.durationMs) assertThat(exportResultWithResume.durationMs - exportResultWithoutResume.durationMs)
.isLessThan(maxDiffExpectedInDurationMs); .isLessThan(maxDiffExpectedInDurationMs);
@ -293,7 +304,10 @@ public class TransformerPauseResumeTest {
ExportResult exportResult = result.exportResult; ExportResult exportResult = result.exportResult;
assertThat(exportResult.processedInputs).hasSize(6); assertThat(exportResult.processedInputs).hasSize(6);
int expectedVideoFrameCount = 2 * MP4_ASSET_FRAME_COUNT; int expectedVideoFrameCount = 2 * MP4_ASSET_FRAME_COUNT;
assertThat(exportResult.videoFrameCount).isEqualTo(expectedVideoFrameCount); // Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(exportResult.videoFrameCount).isWithin(2).of(expectedVideoFrameCount);
// The first processed media item corresponds to remuxing previous output video. // The first processed media item corresponds to remuxing previous output video.
assertThat(exportResult.processedInputs.get(0).audioDecoderName).isNull(); assertThat(exportResult.processedInputs.get(0).audioDecoderName).isNull();
assertThat(exportResult.processedInputs.get(0).videoDecoderName).isNull(); assertThat(exportResult.processedInputs.get(0).videoDecoderName).isNull();
@ -358,8 +372,12 @@ public class TransformerPauseResumeTest {
.isEqualTo(exportResultWithoutResume.audioEncoderName); .isEqualTo(exportResultWithoutResume.audioEncoderName);
assertThat(exportResultWithResume.videoEncoderName) assertThat(exportResultWithResume.videoEncoderName)
.isEqualTo(exportResultWithoutResume.videoEncoderName); .isEqualTo(exportResultWithoutResume.videoEncoderName);
// Rarely, MediaCodec decoders output frames in the wrong order.
// When the MediaCodec encoder sees frames in the wrong order, fewer output frames are produced.
// Use a tolerance when comparing frame counts. See b/343476417#comment5.
assertThat(exportResultWithResume.videoFrameCount) assertThat(exportResultWithResume.videoFrameCount)
.isEqualTo(exportResultWithoutResume.videoFrameCount); .isWithin(2)
.of(exportResultWithoutResume.videoFrameCount);
int maxDiffExpectedInDurationMs = 2; int maxDiffExpectedInDurationMs = 2;
assertThat(exportResultWithResume.durationMs - exportResultWithoutResume.durationMs) assertThat(exportResultWithResume.durationMs - exportResultWithoutResume.durationMs)
.isLessThan(maxDiffExpectedInDurationMs); .isLessThan(maxDiffExpectedInDurationMs);