Skip to content

Update ADTS CBR SeekMap end position to last complete frame at EOF - #3576

Open
sahuadarsh0 wants to merge 5 commits into
androidx:mainfrom
sahuadarsh0:fix/3571-adts-cbr-trailing-metadata
Open

sahuadarsh0 wants to merge 5 commits into
androidx:mainfrom
sahuadarsh0:fix/3571-adts-cbr-trailing-metadata

Conversation

@sahuadarsh0

Copy link
Copy Markdown

Fixes #3571

Root cause

When constant-bitrate seeking is enabled on AdtsExtractor, maybeOutputSeekMap() initializes ConstantBitrateSeekMap with the full input.getLength(). If the stream ends with non-audio trailing data (such as an APEv2 or ID3v1 tag, or an incomplete trailing frame), AdtsExtractor never refines the seek map when RESULT_END_OF_INPUT is reached. That leaves the reported duration and seek range inflated by the trailer size, delaying playlist transitions in ExoPlayer.

In addition, for shorter streams (< NUM_FRAMES_FOR_AVERAGE_FRAME_SIZE = 1000 frames) that end with trailing metadata, calculateAverageFrameSize() zeroed numValidFrames upon hitting the non-ADTS trailer bytes, disabling CBR seeking altogether.

Changes

  • Track the buffer offset immediately after each completed audio frame in AdtsReader (getLastAudioSampleEndOffset(), ignoring ID3 metadata samples).
  • Track endPositionOfLastSampleRead in AdtsExtractor (reset on seek(), mirroring Mp3Extractor.maybeUpdateCbrDurationToLastSample()). When RESULT_END_OF_INPUT is reached with a finite CBR seek map and endPositionOfLastSampleRead lies between firstFramePosition and the current seek-map end position, re-publish the ConstantBitrateSeekMap bounded to endPositionOfLastSampleRead.
  • In calculateAverageFrameSize(), keep the computed average when a non-sync word is encountered after at least 4 consecutive valid ADTS frames.
  • Update sample_cbs_truncated.adts.{0,1,2,3}.dump to reflect the truncated trailing frame no longer inflating CBR duration, add unit tests in AdtsExtractorNonParameterizedTest covering short and >1000-frame streams with trailing APEv2 metadata, mid-stream seek before EOF, and seek-past-last-frame without shortening duration, and add a RELEASENOTES.md entry.

Verification

  • ./gradlew :lib-extractor:testDebugUnitTest --tests "androidx.media3.extractor.ts.Adts*" — all passing
  • ./gradlew :lib-inspector:testDebugUnitTest --tests "androidx.media3.inspector.MetadataRetrieverTest" — all passing
  • End-to-end ExoPlayer Robolectric playback with the issue's attached 01-clean.aac, 01-tagged.aac, and 02-next.aac files (both from start and after seekTo(0, 12_000) before prepare()) confirmed 01-tagged.aac refines from 38684 ms to 30128 ms (matching 01-clean.aac) and transitions cleanly to 02-next.aac.

@FongMi

FongMi commented Nov 23, 2026

Copy link
Copy Markdown

Thanks for the fix. The trailing-metadata case passed when I ran the ADTS sources from 32dead67691d in a JVM test. I also reproduced a premature EOF case with DefaultExtractorInput. The input reports a length of 35,901 bytes, but the underlying reader returns EOF at 31,805 bytes. The PR then republishes the CBR seek map, shortening the duration from 3.791 s to 3.357 s.

Could we require input.getPosition() == input.getLength() before refining the seek map for a known-length input? A regression test could assert that the initial seek map is retained when the reader reaches EOF before the declared length.

@sahuadarsh0

Copy link
Copy Markdown
Author

Good catch — updated maybeOutputSeekMap to require input.getPosition() == inputLength before refining the CBR seek map on EOF, and added read_withCbrSeeking_prematureEofBeforeDeclaredLength_retainsInitialSeekMap in AdtsExtractorNonParameterizedTest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants