Skip to content

Publish a first-frame-presented signal: readyToPlay is not presentation #315

Open

Description

@edde746

NativeAVPlayerHost.playerLayer.isReadyForDisplay is the only real first-frame-on-screen signal (the host's own comment at NativeAVPlayerHost.swift:124 says so), but AetherEngine.nativeHost is internal, so a SPM consumer cannot observe it.

Hosts therefore have to approximate "presenting" from phase == .playing/.paused && isReady. That is AVPlayerItem.readyToPlay, which AVFoundation reaches before AVPlayerLayer has presented anything, and which stays true across a seek. Consequences for a host that lifts a black cover and acknowledges seeks on that edge:

  • a load opened paused lifts the cover before there is a picture;
  • a seek is acknowledged before the new frame is on screen.

Both are visible on tvOS with a slow origin.

Could the engine publish a per-load first-frame signal — e.g. @Published var hasPresentedFirstFrame: Bool reset on load(), driven by isReadyForDisplay on the native path and by the first rendered sample on the software (AVSampleBufferDisplayLayer) path? A published flag keeps one signal meaningful across both pipelines, which host-side KVO on a layer it cannot reach cannot do.

Happy to send a PR if the shape is agreeable.

Activity

  1. added a commit that references this issue on Aug 7, 2026
  2. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Built and released in 6.13.0 (PR #329). Thanks for the offer of a patch, and for framing it as one signal across both pipelines rather than a native-only accessor. That framing is what the implementation follows; three details came out of measuring it and differ from the shape in the issue, so they are worth stating up front.

    The property is hasFirstFrameReadyForDisplay, not hasPresentedFirstFrame. Both layers reach isReadyForDisplay while in no view hierarchy at all. Measured headless, where nothing binds a render surface, the native layer reports it at t+0.05 s and the software layer after 6 enqueued frames, on the same session that also logs the #298 warning that no host ever bound a surface. So "presented" would be false for exactly the hosts #298 is about. What the engine can honestly say is that the pipeline has a first frame ready for display, which is the edge a cover comes off on; a host rendering through bind(view:) sees it then, an AVPlayerViewController host presents through AVKit's own layer a frame or so later.

    It is latched for the load rather than published as a level. A replaceCurrentItem costs the layer its picture for about 40 ms, and it does that for the AE#158 in-place handover too, the swap whose entire purpose is to be invisible:

    t+2.030 before replaceCurrentItem: isReadyForDisplay=true
    t+2.069 KVO isReadyForDisplay=false
    t+2.106 KVO isReadyForDisplay=true
    

    As a level that would make a cover-lifting host re-cover the media fallback, the AirPlay master swap, the #93 recovery reload and that handover. So the flag rises once per load and holds through the seams that reuse the running host; a rebuild that goes back through load() (reloadAtCurrentPosition() included) and stop() reset it.

    The seek half of the report is a different signal. A seek keeps the previous frame up, so the layer never stops being ready for display and no first-frame flag can carry that edge. seekEvents already does: every .began is terminated by .landed(renderedTime:), .stalled or .superseded under the same seek id, which is the acknowledgement to gate a scrub UI on.

    On the software path the signal is AVSampleBufferDisplayLayer.isReadyForDisplay. That property is explicitly not KVO-observable, AVFoundation posts AVSampleBufferDisplayLayerReadyForDisplayDidChangeNotification for it instead, and it exists from tvOS/iOS 17.4 and macOS 14.4. Below that the fallback is the first frame handed to the renderer, one hop earlier than presentation, documented on the property.

    One correction to the premise, for future reference rather than as a reason not to do this: nativePlayerLayer has been public since #288, and softwarePiPSource.layer before that, so both layers are reachable today. The reason to publish the flag anyway is the one the issue gives, that a host would otherwise carry two lifetimes and two observation mechanisms, plus a trap that is easy to miss: on a host reused across a reload, the layer still reads true for the outgoing item at the moment a host would attach, so a naive read latches the new session on the old item's frame.

    aetherctl play now prints FIRSTFRAME hasFirstFrameReadyForDisplay=<v> t+<s> and carries rfd=y/n on every telemetry tick, if it is useful to compare against what the host sees.

    Both consumers are on 6.13.0. Leaving this open for a report from an actual slow-origin tvOS session: the case that matters is the paused mount, where readyToPlay arrives and nothing is on screen, and only a device can show whether the cover now comes off at the right moment.

  3. edde746 commented on Aug 8, 2026

    @edde746
    Author

    Adopted on 6.15.2, shaped by all three of the deviations you called out:

    • Our cover-lift edge now requires hasFirstFrameReadyForDisplay for video sessions.
      Audio-only keeps readiness — the flag can never latch with no layer, and waiting for it
      would hang the edge, exactly as you noted.
    • The latch-not-level shape is what let us key the edge once per session without
      re-covering the in-place handover or a recovery reload.
    • The seek half stayed out of it: scrubs are acknowledged by the engine's own landing
      (seek return / SeekEvent.landed), and the re-anchor of the subtitle clock rides the
      landed event's rendered time.

    The paused-mount case is pinned in unit form (the presentation decision refuses to lift on
    readyToPlay alone), and the suites are green against 6.15.2. The report you're waiting on
    — a real slow-origin paused mount on an Apple TV, watching the cover come off on the right
    edge — is still owed; I'll attach it from a device session against our transcode servers.

  4. superuser404notfound commented on Aug 9, 2026

    @superuser404notfound
    Owner

    Good to see it adopted, and the audio-only carve-out is the right read: the video signal is wired only on the three video host paths, so an audio session leaves the flag false forever and a gate waiting on it there would never open.

    Re-reading the contract against the code after this note turned up one entry in it that is wrong, and it is one of the four seams the latch decision was justified with.

    The AE#158 in-place handover does NOT keep the latch. The property doc listed it next to the media fallback, the AirPlay master swap and the #93 recovery reload, and contradicted itself two lines earlier by saying "false at every load()". The discriminator is the entry point, not the inPlaceSwap flag the swap is made with:

    The behaviour is the one worth keeping (holding the latch there would lift a cover onto the previous episode's frozen frame, the same mistake dropFirst() exists to prevent one seam earlier), so what changed is the documentation, in f639cb8, plus a test that pins the handover's stopInternal shape. The old suite only exercised the fold operator, which is why nothing could catch a mis-stated entry point.

    For an adoption that keys the edge once per session this changes nothing. It would matter to a host re-covering per load: that host was re-covering a PiP next-episode handover and the contract said it would not.

    Two follow-ons for a 6.15.2 host, both about that same reset rule:

    reloadAtCurrentPosition() resets the latch, and since 6.16.1 it covers one seam more than it did at 6.15.2. The wireless-AirPlay route change reloads, and that reload now also fires for the remote-HLS bypass while it stands on its own #316 subtitle origin (previously the bypass sat the route change out and kept serving a loopback URL the receiver cannot reach). So an AirPlay route change is a load, and the flag falls and rises with it.

    The one edge I have not measured, and the one that could hang a video-session gate: whether the local AVPlayerLayer reaches isReadyForDisplay at all while isExternalPlaybackActive is true. Nothing renders locally during external playback, and if the layer never rises there, a gate that requires the flag for every video session waits forever for the whole AirPlay session, exactly the shape of the audio-only case. The engine observes external playback but deliberately does not fold it into this flag today. Every load logs

    [NativeAVPlayerHost] #<sid> layer.isReadyForDisplay=<v> t+<s>
    

    so one AirPlay session log settles it. If the layer does stay dark there, the honest fix is engine-side (latch on the external-playback edge, since there is no local first frame to wait for) rather than a host-side special case, and that is worth its own issue.

    Still open for the paused mount on a slow origin from a device, which is the case that motivated the signal.

  5. superuser404notfound commented on Aug 9, 2026

    @superuser404notfound
    Owner

    Measured the external-playback edge I flagged above, and it is real: a video-session gate on this flag hangs for the whole AirPlay session. Fixed in 6.16.2.

    iPhone 17 Pro to Apple TV 4K, one session, seven loads, the engine's own per-load layer log:

    Load Origin External playback layer.isReadyForDisplay
    #1 loopback inactive true after 0.22 s
    #2 LAN active never, 14 s
    #3 loopback inactive true after 0.16 s
    #4 loopback inactive true after 0.17 s
    #5 LAN active never
    #6 LAN, fresh title active never
    #7 LAN, fresh title active never

    The local AVPlayerLayer never reaches isReadyForDisplay while the receiver holds the picture, so the fold had nothing to fold and hasFirstFrameReadyForDisplay stayed false from the route change until AirPlay ended. Loads #6 and #7 are the ones that would hurt an adopting host most: fresh titles started while the receiver already held the route, so there is no earlier local frame anywhere in the session to have latched on.

    What changed: while an external screen holds the picture, the latch is the item's readiness instead of the layer. There is no local first frame coming and no way to observe the receiver's screen, so past readiness the picture is the receiver's business. Deliberately not the clock advancing, which would hang the paused mount this signal exists for. Audio-only still never arms it, so the video-only shape of the gate is intact, and the seam rules stay the layer's: this only ever adds a rise, never a fall. Wired HDMI takes the same latch, since it flips isExternalPlaybackActive too and keeps no local picture either.

    Device-verified rather than inferred. On the rebuilt LAN load the latch now lands 84 ms after the load starts, where before it never landed at all:

    280.788  #2 load url=http://10.20.30.34:54276/master.m3u8
    280.788  #2 layer.isReadyForDisplay=false t+0.00s
    280.863  #315: an external screen holds the picture, so no local first frame
             is coming; latching hasFirstFrameReadyForDisplay at readiness
    280.872  hasFirstFrameReadyForDisplay=true
    

    For an adoption that already ships: on 6.15.2 the cover stays down for any AirPlay session, and no host-side change is needed on 6.16.2, the flag simply rises where it previously did not. Worth a look only if the gate treats a long wait as a failure and does something about it, since that timeout would have been firing.

    Still open for the paused mount on a slow origin from a device, which is the case the signal was published for.

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