Skip to content

fix(Mp4): locate moov from the mdat header instead of buffering the whole mdat - #151

Open
whoisdennis wants to merge 1 commit into
dimitris-c:mainfrom
whoisdennis:fix/non-optimized-mp4-early-moov
Open

whoisdennis wants to merge 1 commit into
dimitris-c:mainfrom
whoisdennis:fix/non-optimized-mp4-early-moov

Conversation

@whoisdennis

Copy link
Copy Markdown

Problem

For a non-optimized MP4 ([ftyp][mdat][moov]), Mp4Restructure.checkIsOptimized only returned .needsRestructure once the complete mdat atom was in the buffer. Until then the mdat header hit the "header but not the full atom yet" branch and the result stayed .undetermined, so RemoteMp4Restructure kept streaming from byte 0.

With hi-res ALAC files (24 bit / 96 kHz) that means downloading 100+ MB before playback can start. A 129 MB file never started on an iPhone over Wi-Fi; a 60 MB file took about 6 s.

Fix

The mdat header already carries the atom size, which is all that is needed to compute the moov offset. When the mdat header is seen after ftyp and no moov has been found, record the mdat atom and return .needsRestructure(moovOffset: atomOffset + atomSize) right away. Optimized files are unaffected: moov comes first and returns .optimized before mdat is reached.

Tests

New Mp4RestructureTests:

  • non-optimized file detected from the mdat header alone (fails without the fix)
  • optimized file still detected from the moov header
  • restructure after header-only detection: moov moved before mdat, stco offsets shifted by the moov size, mdatOffset and seekAdjusted correct (fails without the fix)

swift build and swift test --parallel: 57 tests, 0 failures. Verified on device with a 129 MB non-faststart ALAC stream from Navidrome.

…hole mdat

A non-optimized MP4 was only recognized once the complete mdat atom had
been downloaded, so a 128 MB hi-res ALAC file buffered for many seconds
before playback could start. The mdat header already holds its size,
which is enough to request the trailing moov atom.
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.

1 participant