fix(hls): prime mp4 moov with a parsed audio packet on backward-seek muxer restart (#92)#94
Merged
superuser404notfound merged 2 commits intoJul 1, 2026
Conversation
…muxer restart (superuser404notfound#92) Under +delay_moov the mp4 muxer writes moov lazily on the first av_write_frame(ctx, nil). For AC-3/E-AC-3/TrueHD the audio sample entry (dac3/dec3/dmlp) can only be built from a PARSED audio packet, so a first moov flush that fires video-only errors -22 "Cannot write moov atom before EAC3 packets parsed", the segment cut fails, the muxer wedges, and the segment is retried forever (AVPlayer 503 -> forever-loading spinner). On a mid-file backward seek that lands out of the segment cache the producer tears down and rebuilds a fresh muxer at the restart segment; if that muxer's first moov flush (a superuser404notfound#64 RAM-cap interim flush, or the first segment cut) fires before any audio packet is written, it hits this. AAC never triggers it (its sample entry needs no parsed packet). Fix: - Latch audioPacketWritten once the first audio packet is written. - Latch audioNeedsParsedPacketForMoov at init from the audio codec_id (AC-3/E-AC-3/TrueHD) — the only codecs whose sample entry needs a parsed packet. - Scope BOTH new behaviors to that latch: the superuser404notfound#64 RAM-cap interim flushPendingFragment() guard AND the video-leads-audio proactive flush. For those codecs, proactively flush on the first audio packet so moov is emitted with a parsed audio packet present. Because both arms are codec-scoped, AAC (and every other codec) keeps the exact stock code path: no early proactive flush and the full RAM-cap bound on flushPendingFragment. The first segment cut is intentionally left unguarded: if audio never arrives, moov must still be attempted (fail-as-before) rather than being deferred forever, which would convert the wedge into a permanent stall. Audio routing/placement is untouched, so no audio-dropout regression.
5eb31fb to
218073f
Compare
Owner
|
Merged, thank you. This is exactly the kind of engine-side, mechanism-confirmed fix worth taking: codec-scoped so AAC keeps the stock path, and it only narrows the wedge surface without perturbing the healthy path. The trace against movenc's One bit of context: the PR referenced #92, but #92 is the transient DV-P8 visual glitch, a separate failure mode; this moov wedge stands on its own, so nothing to change on your end. Thanks again for the careful work. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #92. Fixes an E-AC-3/AC-3 (and TrueHD) moov-before-audio-parsed wedge that can strand the fragmented-mp4 muxer in a permanent "forever-loading" state after a mid-file backward seek that lands out of the segment cache.
Root cause
MP4SegmentMuxerruns one long-lived mp4AVFormatContextwith+empty_moov+default_base_moof+frag_custom+delay_moov. Under+delay_moov,moovis written lazily on the firstav_write_frame(ctx, nil).FFmpeg's mp4 muxer builds the AC-3/E-AC-3/TrueHD sample-entry box (
dac3/dec3/dmlp) from a parsed audio packet — it cannot emitmoovfor these codecs fromcodecparalone (seemovenc.cmov_write_dec3_tag/handle_eac3). So if the firstmoovflush fires video-only,mov_write_moovreturns-22:The cut then fails, the muxer wedges, and the segment is retried forever -> AVPlayer 503 -> forever-loading spinner.
On a backward seek out of cache, the producer rebuilds a fresh muxer at the restart segment. If that muxer's first
moovflush -- either the#64RAM-cap interimflushPendingFragment()or the first segment cut -- fires before any audio packet is written, it hits this. AAC never triggers it (its sample entry needs no parsed packet).What changed
audioPacketWrittenonce the first audio packet is written.audioNeedsParsedPacketForMoovat init from the audiocodec_id(AC-3/E-AC-3/TrueHD) — the only codecs whose sample entry needs a parsed packet.#64RAM-cap interimflushPendingFragment()on those latches, so AAC (and every other codec) keeps its full RAM-cap bound and the stock code path.moovis emitted with a parsed audio packet present.The first segment cut is intentionally left unguarded: if audio never arrives,
moovmust still be attempted (fail-as-before) rather than deferred forever -- guarding the cut too would convert the wedge into a guaranteed permanent stall. Audio routing/placement is untouched (no dropout regression).Reproducibility (honest caveat)
This is hard to reproduce on demand and I could not capture a live before/after. The original triggering file's identity was lost (temp log GC'd). On the unpatched build, the closest-duration-match E-AC-3/HEVC file played through 93 producer restarts / 50 out-of-cache backward seeks with zero wedges, because in the normal seek path audio reaches the interleaver 4-30 ms after each restart and beats the first cut. The wedge needs the narrow case where a full segment of video (coarse interleave) or an audio-absent region precedes the first audio packet at the restart point.
So I am offering this as a low-risk guard against a real, mechanism-confirmed crash class -- it can only reduce the wedge surface and does not alter the healthy path. Happy to adjust or hold if you would prefer an issue-first discussion.
Test plan
swift buildand the tvOS device build both pass. See the Reproducibility caveat above — the wedge could not be reproduced on demand even on the unpatched build, so this is a mechanism-confirmed guard rather than a captured before/after.Verification detail:
movenc.c(thedec3/dac3refusal path) and against the vendoredLibavformat.xcframeworkbinary strings.MP4SegmentMuxeris allocated per restart (the latches reset correctly), the change is single-threaded on the producer's serial pump queue, and audio routing/placement is untouched.Checklist
CHANGELOG.mdupdatedfix(hls):,docs(changelog):)internal/private)