Implement MP3 ConstantBitrateSeeker.getDataEndPosition()

This is needed to correctly handle files with trailing non-MP3 data
(which is indicated by the length in the `Info` frame being shorter than
the overall length of the file).

The test file was generated by appending 150kB of `DEADBEEF` onto the
end of `test-cbr-info-header.mp3`, and the test asserts that the
extracted samples are identical.

Issue: androidx/media#1480

PiperOrigin-RevId: 658727595
(cherry picked from commit b09cea9e3a3f9a039b77c6e6db3ee5f450becc50)
This commit is contained in:
ibaker 2024-08-02 02:49:28 -07:00 committed by Tianyi Feng
parent 3dfe43b498
commit eb19aefa57
4 changed files with 21 additions and 1 deletions

View File

@ -7,6 +7,9 @@
* Transformer:
* Track Selection:
* Extractors:
* MP3: Fix `Searched too many bytes` error by correctly ignoring trailing
non-MP3 data based on the length field in an `Info` frame
([#1480](https://github.com/androidx/media/issues/1480)).
* Audio:
* Video:
* Text:

View File

@ -25,6 +25,7 @@ import androidx.media3.extractor.MpegAudioUtil;
/* package */ final class ConstantBitrateSeeker extends ConstantBitrateSeekMap implements Seeker {
private final int bitrate;
private final long dataEndPosition;
/**
* Constructs an instance.
@ -60,6 +61,7 @@ import androidx.media3.extractor.MpegAudioUtil;
boolean allowSeeksIfLengthUnknown) {
super(inputLength, firstFramePosition, bitrate, frameSize, allowSeeksIfLengthUnknown);
this.bitrate = bitrate;
dataEndPosition = inputLength != C.LENGTH_UNSET ? inputLength : C.INDEX_UNSET;
}
@Override
@ -69,7 +71,7 @@ import androidx.media3.extractor.MpegAudioUtil;
@Override
public long getDataEndPosition() {
return C.INDEX_UNSET;
return dataEndPosition;
}
@Override

View File

@ -54,6 +54,21 @@ public final class Mp3ExtractorTest {
Mp3Extractor::new, "media/mp3/test-cbr-info-header-pcut-frame.mp3", simulationConfig);
}
// https://github.com/androidx/media/issues/1480
@Test
public void mp3SampleWithInfoHeaderAndTrailingGarbage() throws Exception {
// This test file is test-cbr-info-header.mp3 with 150kB of 0xDEADBEEF garbage appended on the
// end. The test asserts that the extracted samples are the same as for
// test-cbr-info-header.mp3.
ExtractorAsserts.assertBehavior(
Mp3Extractor::new,
"media/mp3/test-cbr-info-header-trailing-garbage.mp3",
new AssertionConfig.Builder()
.setDumpFilesPrefix("extractordumps/mp3/test-cbr-info-header.mp3")
.build(),
simulationConfig);
}
@Test
public void mp3SampleWithCbrSeeker() throws Exception {
ExtractorAsserts.assertBehavior(