Skip to content

FEL route: recover from session loss (#739), drain the decoder at end of stream - #784

Merged
superuser404notfound merged 23 commits into
superuser404notfound:mainfrom
pkajaba:fel2/6b-route-recovery
Oct 15, 2026
Merged

superuser404notfound merged 23 commits into
superuser404notfound:mainfrom
pkajaba:fel2/6b-route-recovery

Conversation

@pkajaba

@pkajaba pkajaba commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #770, for the two points in the merge comment. Both only matter with preferDualLayerDolbyVision on.

What's in it

  • Session loss (Software path: VT decode session invalidated by screen lock is never recovered (black video after wake) #739). VideoSessionRebuilding names what the host's recovery uses (onSessionLost, rebuildSession()); HardwareVideoDecoder and DualLayerVideoDecoder conform, and exitBackgroundAudioOnly / recoverVideoSession go through the protocol instead of as? HardwareVideoDecoder. The dual-layer decoder fans onSessionLost in from both sub-decoders and reports one loss per event (a screen lock invalidates both sessions), and rebuildSession() rebuilds both and re-adopts the feed epoch the sub-decoders' rebuilds bump. Pairing state is reset by the host's flush around the rebuild: before it on the background return, and by the reposition seek after it in recoverVideoSession (the route is seekable VOD, so the live no-reposition branch does not apply). A DEBUG hook lets the AE#739 harness (aetherctl bgaudio --lose-session --fel) invalidate both sessions.
  • End of stream. VideoDecodingPipeline.drainForEndOfStream(), a no-op for decoders whose flush() already delivers what they owe. The dual-layer decoder waits on VideoToolbox with pairing live, sends a base frame still waiting for its enhancement layer out alone on its own ticket, and returns once the frames have reached onFrame (or after 0.5 s with a composition still on the GPU). The host calls it before the VOD end-of-source flush; it also calls it at the DVR-ring end and the timeline seam, which the route cannot reach today (seekable VOD only) and where it is a no-op for HardwareVideoDecoder. Seeks keep the discarding flush().
  • makeBlockBuffer's failure path releases the block source's retain only when FreeBlock has not already run (a flag the free callback sets), so it is right whichever way CoreMedia behaves after AllocateBlock fails.
  • The real-clip leak and memory checks in DoviFelRealFileTests can run alone (DV_FEL_MEMORY_SOLO), so parallel suites do not disturb their footprint numbers. Skipped without DV_FEL_TESTS_DIR.
  • CHANGELOG under Unreleased > Fixed.

How it was tested

  • Both test steps locally on this branch with main merged: swift test 4784 tests in 645 suites, and LIBDISPATCH_COOPERATIVE_POOL_STRICT=1 swift test --skip-build --disable-xctest 4784 in 645, green.
  • New in DualLayerVideoDecoderTests: end-of-stream delivery (base frames with and without an EL partner arrive in order, the following flush delivers nothing), seek flush still discards, one loss reported per event, rebuildAdoptsFeedEpoch, drainDispatchesThroughProtocol.
  • Mac (M2 Pro, debug aetherctl with the feat(aetherctl): FEL playback, survey and diagnostics tooling #772 tooling, a 120 s Profile 7 FEL clip):
    • Played to the end from 110 s: all 240 remaining frames arrive (enq=240 disp=240 lost=0, the last at 119.995 s), 0 compose failures. Route off also plays to the end.
    • bgaudio --lose-session --fel: each of the two sessions invalidated once and rebuilt once, video resumes after the background return, 0 compose failures.
    • 45 s with 3 seeks: all landed, lost 0, 0 compose failures, footprint 322 MB.

Not in it

The two smaller notes (the el_type probe's unbounded seek back to 0, no byte budget in completeHEVCRecordFromInBandParameterSets). Happy to send them separately if you want them.

…dropping them

`flush()` raises `draining` and moves the delivery epoch before it drains the
sessions, so everything VideoToolbox, the GPU and the pairing queues still hold
is dropped. That is right for a seek, but the host also calls `flush()` at end of
stream, right after feeding the last packet, and there it drops the last frames
of every title. `drainForEndOfStream()` drains both sessions without the gate,
sends any base-layer frame still waiting for its enhancement layer out alone
under its ticket, and returns once the frames have reached `onFrame` (or after
0.5 s with a composition still on the GPU).
At end of media, at the end of a DVR ring and at a timeline seam the host
flushes the video decoder right after feeding the last packet. On the FEL route
that flush drops the frames the sessions and the GPU still hold, so the last
frames of every title never reached the layer. Drain them first.
…sessions

The host recovers from a session the system invalidated (AE#739) only through
`HardwareVideoDecoder`: it installs `onSessionLost` and calls `rebuildSession()`
on that type alone. On the FEL route neither sub-decoder had a handler, so a
session lost to a screen lock or a media-services reset was never noticed and
the layers dropped every later packet: video stayed frozen while audio played on.

`VideoSessionRebuilding` names the two members the host uses; both decoders
conform. The dual-layer decoder hands the handler to both sub-decoders (and to
the enhancement decoder when it opens), and rebuilds both sessions, then puts
both back on its own feed epoch: each sub-decoder's rebuild moves its epoch,
and the packets the host feeds carry the owner's.
…tes them

The AE#739 recovery (handler, rebuild on loss and on the foreground return)
looked only for `HardwareVideoDecoder`, so on the dual-layer route a lost
session froze the video for the rest of the session. Go through
`VideoSessionRebuilding`, which both decoders implement.
…e per layer

A screen lock invalidates both sessions and each sub-decoder reported it,
so the host rebuilt both sessions twice and repositioned twice for one
event (seen with aetherctl bgaudio --lose-session --fel). The sub-decoders
now report through the dual-layer decoder, which passes the first loss on
and the rest only after rebuildSession() has replaced both sessions.
fel22StreamClassificationAndPacketBalance compared the process-wide
PacketBalanceTracker.alive before and after, so any suite allocating or
freeing packets in parallel failed it now and then. DEBUG builds get a
task-local PacketBalanceTracker.Scope; the test counts only its own task.

longRunStable asserted on the process's resident memory, which grew by
420 to 480 MiB in full runs while the test alone stayed flat at about
106 MiB. The bound is now asserted only on request, for a run on its own
(DV_FEL_MEMORY_SOLO=1); the frame and stall checks always run.
…route counts as VideoToolbox; epoch-adoption and drain-dispatch tests
The one-lane trait serialises a case behind every other lane case, and the
wait counts against the case's .timeLimit. On the CI runner the fuzz suites
hold the lane for ~85 s, so DualLayerStreamClassificationTests and
DualLayerGpuSlotWaitTests hit their 1-minute limit before they ran. The four
dual-layer suites with a time limit are hang watchdogs parked on VideoToolbox
or a semaphore, not CPU loops, so they take the plain .offCooperativePool, as
the trait's comment already asks of DoviComposeAsyncTests.
@superuser404notfound
superuser404notfound merged commit 3221fe8 into superuser404notfound:main Oct 15, 2026
4 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged in 3221fe8, thanks @pkajaba. This closes the two conditions from the #770 merge comment, plus the makeBlockBuffer release.

Reviewed against main:

  • Session loss. One report per event, the rebuild puts both layers back on the decoder's epoch, and the host reaches the route through VideoSessionRebuilding in all three places (the handler, the background return, recoverVideoSession).
  • End-of-stream drain. It takes the same lock order as flush(). An unpaired base frame goes out on its own ticket, and the protocol default keeps HardwareVideoDecoder and the software decoder unchanged. One edge stays as is: a composition still on the GPU after the 0.5 s wait holds the tickets behind it until the following flush drops them. That only matters on a GPU stall at the very end of a title, so I left it.
  • Both test steps on the merged tree: swift test 4834 tests in 652 suites plus 768 XCTest, and LIBDISPATCH_COOPERATIVE_POOL_STRICT=1 swift test --skip-build --disable-xctest 4834 in 652, all green.

One change from the branch: in the conflict with main, the dual-layer suites keep .offCooperativePoolOneLane with five-minute limits (4da2434, after a one-line test waited 71 s for the lane on CI) instead of moving back to the plain trait with 1 to 2 minutes. The plain trait let each blocked job open its own thread and crowd out the timing tests on the 3-core runner. Your new tests are in unchanged apart from that suite line.

The two smaller notes (the el_type probe's unbounded seek back to 0, and the missing byte budget in completeHEVCRecordFromInBandParameterSets) are welcome as a separate PR.

Before a consumer turns preferDualLayerDolbyVision on, what's left is the Apple TV side: the filmed EL residual and counter arms, plus a few minutes of a full title with seeks on this stack, ideally including a screen-lock/sleep return now that #784 covers it.

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