Repository navigation
fix(Mp4): locate moov from the mdat header instead of buffering the whole mdat - #151
whoisdennis wants to merge 1 commit into
Conversation
…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.
|
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:
#155 also has tests with two small real fixtures: a 1 s AAC file from |
Problem
For a non-optimized MP4 (
[ftyp][mdat][moov]),Mp4Restructure.checkIsOptimizedonly returned.needsRestructureonce 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, soRemoteMp4Restructurekept 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.optimizedbefore mdat is reached.Tests
New
Mp4RestructureTests:stcooffsets shifted by the moov size,mdatOffsetandseekAdjustedcorrect (fails without the fix)swift buildandswift test --parallel: 57 tests, 0 failures. Verified on device with a 129 MB non-faststart ALAC stream from Navidrome.