Skip to content

fix(video): prime moov from a real audio frame when the first segment has none (#222) - #224

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/issue-222-eac3-moov-prime
Jul 26, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/issue-222-eac3-moov-prime

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Problem

An E-AC-3 source whose first segment carries no audio packet never produces a frame. movenc builds the ec-3 sample entry's dec3 box in handle_eac3, i.e. only from a PARSED bitstream frame, never from codecpar or extradata (#222 verified: a valid dec3 in extradata still returns -22). Under +delay_moov the moov is written at the first fragment flush, so a first cut with video only fails -22 "Cannot write moov atom before EAC3 packets parsed", completedBytes stays 0, and the cut reported plain failure.

flushPendingFragment already refuses that flush. cutFragmentForNextSegment did not, and its only "not now" signal was nil, which the producer reads as a fatal wedge: the VOD revive rebuilt the identical configuration twice more, gave up with "source not muxable in this session", and the host fell back to a server transcode of a file it should have played untouched.

Repro

Synthetic MKV, HEVC Main10 + E-AC-3 5.1, audio starting behind ~12 s of video in file order (-itsoffset), which reproduces the reporter's trace exactly:

[HLSSegmentProducer] seg-0.m4s cut FAILED; muxer is wedged, ending pump
[HLSSegmentProducer] pump finished: reason=muxerFailed packetsRead=95 packetsWritten=93
... x3 ...
[HLSVideoEngine] #99 VOD muxerFailed revive cap reached; giving up (source not muxable in this session)

The EAC3 bridge wedges identically (its encoder output is also E-AC-3). Only the FLAC bridge played, at the cost of the passthrough, which is why bridging is not the fix.

Fix

  • A cut gets a third outcome, deferredAwaitingAudioSampleEntry: nothing written, muxer intact.
  • The pump treats it as recoverable: it scans forward (bounded: 128 MiB / 30 s, discarding everything else) for one real audio frame and exits with it.
  • The host keeps that frame for the whole session, so every later muxer (seek, audio switch, revive) is primed too, and rebuilds. A primed muxer muxes the frame at init, which writes moov with a genuine dec3, then discards the primed fragment's bytes. The delivered segment is unchanged: no out-of-place audio sample, no disjoint track ranges, plan boundaries untouched, E-AC-3 stream-copy and Atmos preserved.
  • No frame inside the bounds, or a second defer, falls through to the existing muxerFailed recovery.
  • finalize() gets the same precondition, so a teardown on a never-primed muxer stops emitting -22s for a moov it cannot write.

Why re-arming +frag_discont is load-bearing

movenc consumes the flag on the primed packet, after which each track's next fragment-opening sample is rewritten to start_dts + track_duration, and tfdt is cluster[0].dts - start_dts. Un-rearmed, the first real audio fragment lands at tfdt 0 no matter where its samples belong: measured on the fixture, a 12 s audio start rendered 12 s early. Re-armed, timestamps match an unprimed session exactly.

Verification

  • Issue222EAC3MoovPrimeTests (6 tests): deferral instead of failure, AAC unaffected, dec3 present in the primed init, primed fragment not served, video-only first segment cuts, and the tfdt regression (proven to fail without the re-arm: tfdt=[-1024, 0] instead of 576000).
  • Full suite 1087/1087, strict-concurrency clean, tvOS simulator build succeeds.
  • E2E on the repro fixture: audioTrack codec='ec-3', first frame 0.15 s, 20 s of playback with no stall, VERDICT: OK. Produced segments: seg0 video-only at 0.000s, seg3 video 11.792s + audio 11.995s, seg4 continuous. 40-seek burst settles at 0.00s error with no second defer.
  • Control fixture (healthy interleave): primes nothing, logs nothing, fragment timestamps unchanged.

Reported and root-caused by @thatcube, who pinned the missing guard and ruled out both dec3-in-extradata and dropping +delay_moov.

Refs #222

🤖 Generated with Claude Code

https://claude.ai/code/session_01QYhPvBB1sLCYoZNasEXFeB

… has none

An E-AC-3 source whose first segment carries no audio packet never produced a
frame: movenc builds the ec-3 sample entry's dec3 box in handle_eac3, i.e. only
from a PARSED bitstream frame, never from codecpar or extradata. Under
+delay_moov the moov is written at the first fragment flush, so a first cut with
video only fails -22 "Cannot write moov atom before EAC3 packets parsed",
completedBytes stays 0, and the cut reported failure. flushPendingFragment
already refuses that flush; the cut did not, and its only "not now" signal was
nil, which the producer reads as a fatal wedge. The VOD revive then rebuilt the
identical configuration twice more and gave up, so the host fell back to a
server transcode of a file it should have played untouched.

Reproduced deterministically with a synthetic MKV whose audio starts behind
~12 s of video (HEVC Main10 + E-AC-3 5.1): packetsRead=95 packetsWritten=93,
three identical failures, then "source not muxable in this session". The EAC3
bridge wedges the same way (its encoder output is also E-AC-3); only the FLAC
bridge played, at the cost of the passthrough.

A cut now has a third outcome, deferredAwaitingAudioSampleEntry: nothing is
written and the muxer stays intact. The pump treats it as recoverable, scans
forward for one real audio frame (bounded, discarding everything else), and
exits with that frame. The host stores it for the whole session and rebuilds:
every muxer from then on muxes the frame at init, which writes moov with a
genuine dec3, then discards the primed fragment's bytes. The delivered segment
is therefore unchanged (no out-of-place audio sample, no disjoint track ranges,
plan boundaries untouched) and the E-AC-3 stream-copy, Atmos included, is kept.

Re-arming +frag_discont after the prime is load-bearing: movenc consumes the
flag on the primed packet and would then rewrite each track's next
fragment-opening sample to start_dts + track_duration, so the first real audio
fragment landed at tfdt 0 regardless of where its samples belong (measured on
the fixture: a 12 s audio start rendered 12 s early). Re-armed, timestamps match
an unprimed session exactly, verified on the produced segments: seg0 video-only
at 0.000s, seg3 video 11.792s + audio 11.995s, seg4 continuous.

finalize() gets the same precondition, so a teardown on a never-primed muxer
stops emitting -22s for a moov it cannot write.

Sources whose audio arrives normally are untouched: the control fixture primes
nothing, logs nothing, and keeps byte-identical fragment timestamps.

Reported and root-caused by thatcube, who pinned the missing guard in
cutFragmentForNextSegment and ruled out both dec3-in-extradata and dropping
+delay_moov.

Co-Authored-By: thatcube <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01QYhPvBB1sLCYoZNasEXFeB
@superuser404notfound
superuser404notfound merged commit e8d919d into main Jul 26, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue-222-eac3-moov-prime branch July 26, 2026 12:06
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