Skip to content

Expose the native AVPlayerLayer (PiP) and the software backend's enqueued-frame count #288

Closed

Description

@kskchaitanya1993

Two internals a tvOS host ends up needing read access to. We carry both as local patches against every release (currently 6.5.5) purely to widen visibility — no behaviour change in either, which is why I'm raising them together.

Context: tvOS host, AetherEngine as rung 1 of a two-engine playback ladder (Aether → VLC), IPTV live + VOD.


1. nativeHost.playerLayer — needed to offer PiP on the native path

pictureInPictureActive and currentAVPlayer are already public, but AVPictureInPictureController needs the layer, not the player. nativeHost and its playerLayer are internal, so a host that wants to present PiP on the native remote-HLS path has no way to reach it.

Our patch is one computed property:

public var nativePlayerLayer: AVPlayerLayer? { nativeHost?.playerLayer }

The host then attaches that layer to its own AVPictureInPictureController and forwards start/stop. Nothing else is required, and it only applies to the native path — a software-decode session has no AVPlayerLayer, so the host correctly hides the PiP affordance there.


2. softwareFramesEnqueued — needed to answer "is there video?" on the software backend

This one cost us a shipped regression, so the motivation is worth spelling out.

A host has to be able to distinguish "playing" from "playing with a picture" — audio-only-and-black is a real failure mode on HEVC-in-MPEG-TS, and .playing alone doesn't distinguish it. On the native path the honest signal is AVPlayerItemVideoOutput.hasNewPixelBuffer, which works well. (presentationSize and isReadyForDisplay are not usable for this: AVFoundation latches presentationSize from the init segment before anything composites, so both read positive on a black mount.)

But when the engine dispatches to the software path there is no AVPlayer at all, so that probe returns false forever. Our no-video watchdog therefore fired on healthy, already-playing streams and dropped them to the fallback engine roughly 18s in — on a source that was decoding and presenting perfectly. Any codec the box can't hardware-decode takes that path ([VTProbe] hwSupported=false → dispatch: … → software), so it isn't an edge case.

The fix on our side was to fall back to the decoder's own enqueued-frame count when currentAVPlayer == nil, which required exposing:

public var softwareFramesEnqueued: Int { /* SoftwareVideoDecoder's counter */ }

A monotonic count is all that's needed — the host only asks "has it moved". The general shape of the request is: every "is there video?" check has to be answerable on both backends, and today only the native one is.


Happy to send either as a PR if you'd prefer that to a patch on your side — they're both a couple of lines. Raising as an issue first per your preference on API surface.


This issue was auto-generated by Claude on behalf of @kskchaitanya1993.

Activity

  1. superuser404notfound commented on Aug 3, 2026

    @superuser404notfound
    Owner

    Both are on main in abc2341e, no behaviour change in either, and they go out with the next release.

    1. nativePlayerLayer landed as proposed:

    public var nativePlayerLayer: AVPlayerLayer? { nativeHost?.playerLayer }

    Your framing convinced me because it is an asymmetry, not a new capability: the software path has published its layer since SoftwarePiPSource in 5.13.0, for exactly this reason, while the native path published only currentAVPlayer. A host rendering through bind(view:) then has no route to the layer that is already on screen and has to mount a second one to get there. Two things worth pinning down in the doc comment, since a PiP window is unforgiving about both: read it per session rather than caching it (the layer survives item swaps and the #158 in-place handover, but a stop() tears the host down and the next load() builds a fresh one), and keep setting pictureInPictureActive, which is what arms the keepalive policy and that handover. nil outside a native session is also the honest signal for hiding the affordance, as you noted.

    2. The software frame count is public now, under the name it already had internally:

    public var softwareHostFramesEnqueued: Int    // AetherEngine+Diagnostics.swift

    Worth knowing when you drop your patch: this counter is what LiveTelemetrySampler has been reading at 1 Hz for observed FPS all along, and the bgaudio CLI harness reaches it through a DEBUG-only hook, so nothing about it was new except its visibility in a Release build. One property of it your watchdog needs to survive: it belongs to the current software host, so a load() that builds a new one restarts it at zero. A delta-based watchdog has to read a decrease as a new session rather than as a stall.

    On the general shape of the request, "every is-there-video check has to be answerable on both backends": agreed as a principle, and this is as far as I can take it today without changing behaviour. The engine has no frame counter of its own on the native path, since it attaches no AVPlayerItemVideoOutput, and adding one to synthesize a backend-neutral signal would cost pixel buffer traffic on the path that is otherwise untouched. So the backend split stays host-side for now: currentAVPlayer == nil picks which probe to run. If a single published signal turns out to be worth it, that is its own issue and I would rather design it deliberately than fold it into these two.

    Thanks for raising both with the motivation spelled out, and in particular for the 18 s watchdog regression: "playing" versus "playing with a picture" is precisely the distinction the public surface was failing to support, and that is a much better argument than the two lines of diff suggest. Carrying them as patches across every bump was not a reasonable thing to have to do.

  2. kskchaitanya1993 commented on Aug 3, 2026

    @kskchaitanya1993
    Author

    Thanks — both look right, and the softwareHostFramesEnqueued name is better than ours since it says whose counter it is, which is exactly the property that matters below.

    Your per-session warning found a real bug on our side. We don't cache the layer — pipPlayerLayer is a computed passthrough — but we were doing something with the same root error at teardown:

    .onDisappear {
        if let layer = aetherEngine.pipPlayerLayer { PipController.shared.detach(layer: layer) }
    }

    Our detach(layer:) guards on controller?.playerLayer === layer, so handing it a re-read of the current layer only works while the layer is stable for the session. After any reload it isn't: we attach layer A, the engine rebuilds, onDisappear re-reads and passes layer B, the identity guard fails, and an AVPictureInPictureController holding a dead layer outlives the player — with available still true, so the next surface inherits a PiP affordance pointing at nothing.

    Fixed by not naming the layer at teardown at all: a detachCurrent() that tears down whatever is attached, still refusing while PiP is active because there the system owns playback. So "read per session" turned out to mean more than "don't store it in a property" — thanks for spelling it out rather than just landing the accessor.

    On the counter restarting at zero: checked, and we survive it, though closer to luck than design. The > 0 no-video probe is unaffected. The 1 Hz stats delta already refuses to compute across a decrease and re-baselines instead:

    if swFramesSample > 0, dt > 0.3, dt < 3.0, frames >= swFramesSample {
        snap.renderedFps = Double(frames - swFramesSample) / dt
    }
    swFramesSample = frames

    so a new host reads as one skipped sample rather than a negative FPS. Your framing is the correct one to hold though — a decrease is a new session, not a stall — and that is now written down on our side rather than being an accident of a bounds check.

    On the backend-neutral signal: agreed, and I'd rather it stay host-side than have the engine attach an AVPlayerItemVideoOutput it doesn't otherwise need. We pay that cost deliberately in one place — a QA-only pixel sampler that copies a buffer to prove the frame isn't black, since arrival is not the same question as content — and it is expensive enough that it is off by default and capped below 4K. Not something to put on the native playback path for a signal the host can already assemble from currentAVPlayer == nil.

    We'll drop both patches when the release lands and confirm on device then.


    This issue was auto-generated by Claude on behalf of @kskchaitanya1993.

  3. superuser404notfound commented on Aug 3, 2026

    @superuser404notfound
    Owner

    Shipped in 6.6.0 (abc2341e). Both properties are read-only with no behaviour attached, so the downstream patches can be dropped rather than rebased. nativePlayerLayer is nil outside a native session, which doubles as the signal for hiding a PiP button, and softwareHostFramesEnqueued restarts at zero when a load() builds a new host, which a delta-based watchdog has to read as a new session rather than a stall.

  4. kskchaitanya1993 commented on Aug 3, 2026

    @kskchaitanya1993
    Author

    Confirmed on device. We're on 6.6.0 and the local patch set is now zero — aether-patches/ is empty for the first time since we started vendoring this engine. Regression on the Apple TV after the bump: 11/11, all channels on Aether.

    Both notes landed where they mattered:

    • nativePlayerLayer was deleted rather than rebased, as you said — re-applying ours would have been a duplicate declaration, not a no-op.
    • Our three call sites take softwareHostFramesEnqueued. Your restart-at-zero warning is handled: the > 0 no-video probe is indifferent to it, and the 1 Hz stats delta refuses to compute across a decrease and re-baselines, so a new host reads as one skipped sample rather than a negative FPS.

    The per-session point on the layer also found a real bug on our side, which I described in my earlier comment — we were re-reading the current layer at teardown and handing it to an identity-guarded detach, so after any reload a controller holding a dead layer outlived the player. Fixed.

    Nothing outstanding from us here; happy for this to close.


    This comment was drafted and posted automatically by Claude, working on behalf of @kskchaitanya1993.

  5. superuser404notfound commented on Aug 4, 2026

    @superuser404notfound
    Owner

    Closing on that confirmation. 6.6.0 on device, 11/11 on the Apple TV, and aether-patches/ empty.

    Deleting nativePlayerLayer rather than rebasing it is the right read: it is the same declaration, so re-applying would have collided rather than no-op'd, which is a nicer failure than two accessors silently disagreeing about which host owns the layer.

    The teardown bug is the more valuable outcome of the two properties, and worth leaving here for anyone who arrives from a search. "Read per session" is a statement about the accessor being honest at the moment it is called, not about the value being stable across the session, and an identity-guarded detach(layer:) fed from a fresh read quietly depends on the second. detachCurrent() is the right shape because it takes the layer's identity out of the teardown path entirely instead of re-deriving it at the one moment the engine may already have rebuilt it. The counter has the same character: the re-baseline on a decrease now holds because it is intended rather than because frames >= swFramesSample happened to bound it.

    The backend-neutral "is there video" signal stays out of scope here rather than declined outright. If the QA pixel sampler ever makes a published signal worth its cost on the native path, that is its own issue, and arrival versus content is a distinction worth designing around deliberately.

    Thanks for both patch descriptions, and in particular for the 18 s watchdog regression that motivated them. "Playing" versus "playing with a picture" was a gap in the public surface that two lines of diff do not advertise on their own.

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