Skip to content

Load during the panel switch instead of behind it (the play gate serialises what could overlap) #348

Open

Description

@superuser404notfound

Split out of the Sodalite#49 / #339 device session, which measured the numbers this rests on.

What happens now

An engine HDR/DV write is followed by a pre-flight waitForSwitch(), and only when that returns does loadNative run. The switch and the load are therefore serialised, although only the first frame has to wait for the panel, not the demuxer, the local HLS server or the AVPlayerItem.

Twelve measurements on an Apple TV 4K (3rd gen), tvOS 26.5, with the mode-switch notifications actually arriving (#339): a switch takes 2779 to 2898 ms, dynamic-range and range-plus-rate alike, and loadNative takes ~850 ms on a LAN source.

Why it currently looks fine

Stage 2's cap is 2000 ms, so the pre-flight gate always breaks out with the panel still switching, the load runs during the remaining ~880 ms, and the play gate waits out the rest:

cap 2000: pre-flight breaks +2020, load ~850 ms, play() at ~+2918   (switch ends +2892)
cap 4000: pre-flight ends +2892 on the notification, load ~850 ms, play() at ~+3740

The too-small cap is an accidental half-parallelisation, and it is the only reason the startup time is good. "Fixing" the cap to match the measured switch costs a full load, ~820 ms. That trap is now written on stage2CapMs.

What to build

Start the load concurrently with the switch and await the settle before play(), so cold start is max(switch, load) rather than cap + load. On this panel that is a wash today; it pays where the load is slow (remote origin, cold cache, large MKV) and on fast-switching panels (QMS), where the pre-flight now returns in milliseconds and the load is the only remaining cost.

load 2000 ms, switch 2900 ms: 2000 + 2000 = 4000 ms today, max(2900, 2000) = 2900 ms concurrent
load 850 ms, QMS switch 200 ms: 200 + 850 = 1050 ms today, max(200, 850) = 850 ms concurrent

The constraint that keeps it sequential

waitForSwitch's own note: starting the decode mid-write races an AVPlayer error on DV Profile 8.1. That is a real failure and it is the one path that cannot be verified on the hardware available here (the test panel does not do DV).

Suggested cut: keep DV sequential, run HDR10 / HLG / SDR concurrently. The unverifiable path then stays byte-for-byte unchanged, and the change is confined to formats that can be measured. Verifying the gain needs a slow origin (Scripts/throttle-origin.py) since a LAN load is dominated by the switch.

Not urgent

Nothing is broken. This is startup latency in a shape where the current good behaviour depends on a constant that is not documented as doing that job, which is exactly how the next well-meant cap change breaks it.

Activity

  1. superuser404notfound commented on Aug 9, 2026

    @superuser404notfound
    OwnerAuthor

    Second coupling, found while sizing this: the pre-flight wait does not only gate play(), it gates the routing decision.

    AetherEngine.swift reads panelHDRAfterHandshake = displayCriteria.currentPanelIsHDR() immediately after the handshake block, and that value decides videoFormat and is handed to loadNative as panelIsInHDRMode, which is what resolveUseMasterPlaylist uses to choose a PQ master over media-direct. Reading it while the switch is still running returns the pre-switch state, so a naive concurrent load would route an HDR title as SDR: no HDR signalling, silently, for the whole session. That is the failure shape panelPresentsHDR exists to prevent, and the kind that surfaces weeks later.

    So the cut is not "DV sequential, everything else concurrent". It is:

    • Panel already proven to engage HDR (panelProvenToEngageHDR), or an SDR route: the routing answer is known without measuring, so the load can run during the switch.
    • First HDR title of a process with no proof yet: the routing decision genuinely needs the settled panel, so this one stays sequential.

    That keeps the concurrency where it is safe and leaves the one case that cannot be answered early alone. It also means the win is smaller on a cold process than the headline numbers suggest, and largest exactly where it matters, on repeat playback with a slow origin.

    Worth checking before building: whether panelProvenToEngageHDR alone is a strong enough answer for the master/media choice, or whether resolveUseMasterPlaylist needs its own explicit input rather than a flag that means two things.

  2. superuser404notfound commented on Aug 9, 2026

    @superuser404notfound
    OwnerAuthor

    Closing this as analysed and deliberately not built. The numbers do not support it, and writing down why is more useful than leaving it open.

    The gain, filtered three times

    Only the HDR/DV path. A rate-only write takes no pre-flight at all, so its load already runs during the switch. Device run, SDR 25p: item ready at +400 ms, gate released at +2880 ms, fully overlapped as it stands.

    The pre-flight is capped at 2 s, so the ceiling on this is 2 s, not the ~2.9 s of a switch.

    And it is zero until the load is slow. With measured numbers (switch 2890 ms, load 850 ms):

    today:      2000 (cap) + 850 (load)  = 2850 ms
    concurrent: max(2890, 850)           = 2890 ms
    

    It turns positive somewhere around 900 ms of load time and reaches the full 2 s only at ~2.9 s of load.

    What the routing coupling costs on top

    Per the comment above, the concurrent path is only safe when the routing answer is already known, that is when panelProvenToEngageHDR is set. That flag is instance state, so it is unset for the first HDR title after every app launch, which is exactly the load that would benefit most. Anyone who plays one title per sitting would never reach the concurrent path at all. Persisting the proof would fix that and introduce a worse failure: a stored "this panel does HDR" is wrong the moment the TV is replaced.

    And the verification is a race

    The regression that matters is not the latency, it is reading the panel state before the switch settles and routing an HDR title as SDR for the whole session, silently. Proving the absence of that needs repeated cold-start runs on hardware, and a race that is not reliably lost on one panel and one server is not reliably won either.

    So: a gain bounded at 2 s, restricted to HDR starts with a genuine mode change, a slow origin, and a second-or-later title in the same app session, against a change touching two couplings whose failure mode is a quiet loss of HDR. Not a good trade today.

    Worth reopening if someone reports slow cold starts on a remote origin with HDR content, which is the one profile where this pays. The measurements are all here.

  3. badrabbit commented on Oct 16, 2026

    @badrabbit

    This is the report the closing comment asked for: slow cold starts on a remote
    origin with HDR content.

    Apple TV 4K (3rd gen), tvOS 27.2, source is a Plex server over HTTP on the LAN,
    Dolby Vision title, AVPlayerViewController host, AetherEngine 7.38.1. Measured
    from the engine's own log (relative seconds):

    time line
    1.031 s [Demuxer] open timings … find_stream_info 930ms, total 1023ms
    1.036 s [DisplayCriteria] SET: format=dolbyVision codec=dvh1 rate=23.976
    1.466 s panel proven to engage HDR (headroom 1.20)
    3.090 s proceed after cap (start in-gate after 11ms, total 2053ms; … most likely already in the target mode)
    3.205 s serving on …/master.m3u8
    3.927 s seg64: served 17135489 B (wait=648.5ms)
    3.952 s switch settled via modeSwitchEnd (… switch 2889ms end to end)
    4.190 s first frame
    5.404 s seg65: served 32430776 B (wait=1346.5ms)
    5.469 s timeControlStatus=playing

    So the two steps are strictly serialised: the HLS server comes up 115 ms after
    the pre-flight cap releases, and the segment build (2.0 s for the first two
    segments of a 4K HEVC title with peaks near 96 Mbit) starts only then. The panel
    finishes switching at 3.952 s, while playback starts at 5.469 s — about 1.5 s of
    the cold start sits behind a switch that had already completed.

    On the "gain is zero when the load is fast" point: that is the part that does not
    hold here. The load is not fast on this route — two segments of this bitrate cost
    2.0 s to cut, and tightening probesize to 4 MB (which took find_stream_info
    from 930 ms to roughly 150 ms) only moved the whole sequence forward, it did not
    overlap anything.

    On the HDR-routing concern from the closing comment: in this session the panel
    readout was already authoritative well before the cap expired —
    panel proven to engage HDR (headroom 1.20) at 1.466 s, 430 ms after the write
    and 1.6 s before the gate released. The gate's own log line says as much
    ("most likely already in the target mode").

    What makes this painful in practice is that it is paid on every start. Leaving
    playback resets the criteria, so the next title writes them again and gets
    .willSwitch — the .unchanged shortcut needs didApply && managerHoldsCriteria,
    and neither survives a stop. The result is a visible 5+ second start on a title
    that the panel is ready for after 2.9 s.

  4. superuser404notfound commented on Oct 19, 2026

    @superuser404notfound
    OwnerAuthor

    Thank you, this is exactly the profile the closing comment asked for, and the numbers hold up against the code. Reopening.

    Checked against main:

    • The pre-flight gate after an HDR/DV write runs with the standard 2000 ms cap, and loadNative only starts once it returns, so the 115 ms between proceed after cap and serving on …/master.m3u8 is the HLS session coming up behind the gate, as described.
    • The closing analysis assumed a ~850 ms load. On this route two segments of a ~96 Mbit/s title take 2.0 s to cut, which is well past the ~900 ms at which overlapping starts to pay.
    • reset() clears the applied criteria on stop, so every title writes them again and gets .willSwitch. The cost is per start, not per app launch.

    The part that changes the design: the routing decision the closing comment worried about is not needed for the expensive work. Inside HLSVideoEngine.start(), the demuxer seek, the segment plan, the local server and the producer that cuts the first segments all run before the master-or-media choice, and panelIsInHDRMode is read only for that choice, at the very end. So the cut is:

    • During the switch: open the session and start producing segments from the resume position.
    • After the gate, unchanged: read the panel, choose master or media, build the AVPlayerItem, play().

    AVPlayer sees nothing before the gate, so the Dolby Vision mid-write race and the "HDR routed as SDR" failure both stay exactly where they are today, and the cut does not need to exclude DV or the first title after launch. On this log it should move playing from ~5.5 s to just after the switch ends (~4.0 s).

    Separately, the proceed after cap … most likely already in the target mode line is wrong in this session: the switch was seen starting 11 ms into the gate and only ended 860 ms after the cap. A latched HDR proof says the panel can engage HDR, not that it already has, once a start was observed. That line gets fixed with the change.

    Will post here when there is a build to test.

  5. superuser404notfound commented on Oct 20, 2026

    @superuser404notfound
    OwnerAuthor

    Released in 7.43.0 (cb25fe83, merged as 4467df77).

    Verified on an Apple TV 4K (3rd gen) with a Dolby Vision Profile 8.1 title, first title after launch: the local server was up 74 ms after the criteria write, every segment was cut inside the 2.9 s switch, and playing followed 218 ms after modeSwitchEnd, routed to the master as before (useMaster=true, dvh1.08.07/db1p). That clip was small, so it shows the order more than the size of the gain; a retest on the Plex route with the ~96 Mbit/s title would show the full effect. The lines to compare are prepared / serving on against switch settled via modeSwitchEnd, and the gap from there to timeControlStatus=playing.

    Live loads keep the serial order, as do Profile 7 sources that may take the dual-layer route.

  6. added a commit that references this issue on Oct 21, 2026
  7. cmcpherson274 commented on Oct 22, 2026

    @cmcpherson274

    A data point for 7.43.0's change from an Apple TV on tvOS 27.0, read on the lines you named: prepared / serving on against switch settled via modeSwitchEnd, and from there to timeControlStatus=playing. It is not the retest on the Plex route you mentioned, but a heavy clip from a Jellyfin server on the LAN. We ran 7.44.0, which includes the change, beside 7.36.0. On an HLG Dolby Vision clip resumed near its end, playing came 1.83 s sooner after the criteria write: 380 ms after the switch ended instead of 2,240 ms.

    Setup. Apple TV 4K (3rd generation, AppleTV14,1), tvOS 27.0 (24J361); the switch times in the issue text were measured on tvOS 26.5. Debug builds of our app, 7.36.0 and 7.44.0 in one evening (20 October, US Eastern), each run in a fresh process; 7.44.0 with none of its optional traits enabled, and FFmpegBuild also differs between the two (3.7.1 and 3.9.0). Full-screen VOD played direct through our loopback route; the engine writes the display criteria itself. 19 starts per version; one HDR10 start per version, in which our bench's HDMI output dropped, is left out of the per-title figures.

    The switch read 2,897.5 ms end to end (median; 2,878 to 2,929 ms over 38 starts), and the 2.0 s pre-flight cap released before it ended on every start. The panel never read as proven (provenHDR=no before each write), and both versions logged unproven HDR master waits for the running switch to end before it is served (#667) on all 19 starts.

    The lines for the HLG Dolby Vision Profile 8.4 clip (3840x2160 at 59.04 fps, 17.5 s long, resumed at 12.48 s; its next two segments 25.0 and 51.8 MB), 3 starts per version, in ms after [DisplayCriteria] SET:, median [range], 7.36.0 → 7.44.0:

    prepared: cap released serving on switch ends playing switch end → playing
    2,955 [2,941–2,959] → 11 [10–17] 2,022 [2,015–2,058] → 2,047 [2,025–2,064] 2,956 [2,943–2,961] → 2,958 [2,931–2,963] 2,921 [2,906–2,923] → 2,955 [2,928–2,960] 5,163 [5,092–5,289] → 3,329 [3,315–3,340] 2,240 [2,171–2,383] → 380 [374–387]

    Switch end → playing on the other five titles, median ms, 7.36.0 → 7.44.0: Kinda Pregnant, Dolby Vision 8.1 (3 starts per version) 915 → 833; an HDR10 film (3) 1,289 → 895; a Dolby Vision 8.1 test clip (7) 1,329 → 1,215; Hotel Chevalier, Dolby Vision 7 (1) 818 → 434; F9, Dolby Vision 7 with a full enhancement layer (1) 1,255 → 517.

    • On 7.36.0 prepared: came after the switch end on all 19 starts. On 7.44.0 it came 4 to 52 ms after the write, and serving on came 0 to 5 ms after the switch end (median 3 ms) on all 19.
    • [HLSSegmentProducer] init.mp4 captured came before AVPlayer's GET /init.mp4 on 19 of 19 starts on 7.44.0 and 3 of 19 on 7.36.0.

    7.44.0 is our app's engine pin as of today (2026-10-21).

  8. superuser404notfound commented on Oct 24, 2026

    @superuser404notfound
    OwnerAuthor

    Thank you for the measurement. It is the order the change was meant to produce, on a second box and on tvOS 27.0: prepared: moved from after the switch end to 4 to 52 ms after the write, and init.mp4 captured now lands before AVPlayer's GET /init.mp4 on every start.

    Two notes on reading the table:

    The smaller gain on the Dolby Vision 8.1 titles fits the same picture: their first segments were cheap to cut, so there was less work to move into the switch.

    Leaving the issue open for the retest on the Plex route with the ~96 Mbit/s title, which is the case the change was sized against.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions