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
Contributor

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.
@chrizzchrizz

Copy link
Copy Markdown
Contributor

I ran into the same issue independently and ended up with the same change in #155. I didn't see this PR before opening mine, sorry about that. I'll close #155 in favour of this one.

Some numbers from my side, in case they help with the review. They come from an app (Amperfy) playing M4A files over a VPN at about 0.5 MB/s:

before after
bytes read by the optimize check 4.9–8.7 MB (whole file) 2.4–13 KB
play request to start of the audio stream 3–20 s 1.0–1.4 s

#155 also has tests with two small real fixtures: a 1 s AAC file from afconvert with the moov at the start, and the same file with the moov moved behind the mdat and its chunk offsets adjusted. One of the tests feeds the file restructured after the early detection through AudioFileStreamProcessor and checks that it parses without errors. If you'd like them here, I'm happy to open a PR against your branch, or you can take them from #155. Both test classes are named Mp4RestructureTests, so they would have to be merged into one.

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.

2 participants