Skip to content

feat(native): let a host request the in-place item handover for a foreground replacement - #483

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
Quick104:feat/host-requested-item-handover
Sep 5, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
Quick104:feat/host-requested-item-handover

Conversation

@Quick104

@Quick104 Quick104 commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Lets a host that mounts the engine's own AVPlayerLayer request the AE#158 in-place item handover on a foreground episode change, instead of taking the nil-item gap that only PiP is protected from today. Also makes the nativeRemoteHLS bypass honour the handover, which it never did, even under PiP.

Refs #158. The host-side mitigations recorded there as failing are the ones we hit too.

What changed

  • prepareForItemReplacement(): a one-shot public request. The next load() consumes it, stop() cancels it, and a non-native outgoing session ignores it. shouldHandOverItemInPlace gains a hostRequested input; PiP behaviour is unchanged.
  • When the item is kept across the load, NativeAVPlayerHost.prepareForItemHandover() retires the outgoing session's publishers before the engine subscribes for the successor, so the previous item's EOF, readiness, rate and clock are not replayed into the new session. The same-content Loopback-HLS: a single backward seek on heavy 4K can wedge the segment producer (video stalls, audio continues -> A/V desync) #93 recovery swap keeps its clock as before.
  • Main-actor hops queued by the outgoing item's KVO drop on the session guard rather than writing into the successor. unloadCurrentItem retires the session id up front for that reason.
  • The nativeRemoteHLS bypass callsite now reads pendingInPlaceItemHandover like the loopback callsite does. Before this, a native->native remote-HLS load dropped the item to nil even while a PiP window was active.
  • CHANGELOG.md entry, docs/api.md row, and four new cases in PiPItemHandoverTests (one-shot consume, non-native ignore, stop() cancel, PiP independence).

Why the host cannot solve this on its own side: the gap is between stopInternal detaching the item and the successor's replaceCurrentItem, and the engine owns both ends. We tried keeping the surface and layer stable through the gap and presenting the successor only after first frame; the layer still came back black on Apple TV. That matches the three host-side mitigations recorded as failing in #158.

Test plan

  • Device / OS: Apple TV simulator (tvOS 26) and iPhone 17 Pro simulator (iOS 26) through Silo; iPhone (iOS 26) on a signed build. Physical Apple TV 4K verification is scheduled and will be added to this PR.
  • Source media: H.264 / AAC 5.1 MKV served as HLS remux and as HLS transcode (nativeRemoteHLS), 1080p SDR. Direct-play MP4 through the loopback path.
  • Result: next-episode autoplay through a host-requested handover shows one AVPlayerItem swap and zero nil items on the shared player, same view and layer, successor first frame ready. Silo's regression test asserts exactly that on both routes; before the bypass change it recorded 2 item changes and 1 nil item on the remote-HLS route.
  • swift test: 2634 tests, 360 suites; the one failure is Issue458AudioLanguageMetadataTests "three-letter labels ICU cannot name", which fails identically on unmodified main with Xcode 27 beta (ICU locale data). PublicAPIDocumentationTests, DocumentedConstantsTests, PiPItemHandoverTests pass. Scripts/check-doc-links.py clean. tvOS and iOS Simulator xcodebuild builds pass.

Checklist

  • CHANGELOG.md updated
  • Commit messages follow Conventional Commits (feat(...), fix(...), chore(...))
  • The fix lives in the engine, not in a host-side workaround
  • Public API changes are intentional and documented

Built for Silo, a self-hosted media server client. Drafted with AI assistance (Claude Code); reviewed and tested by the author.

…eground replacement

The AE#158 handover that keeps a PiP window alive across a native->native
load() was gated on pictureInPictureActive alone. A host that mounts the
engine's own AVPlayerLayer takes the nil-item gap on every next-episode
transition, and on tvOS that gap can leave the layer black while the
successor's audio and clock run.

prepareForItemReplacement() arms a one-shot request that the next load()
consumes; stop() cancels it and a non-native outgoing session ignores it.
When the item is kept, NativeAVPlayerHost.prepareForItemHandover() retires
the outgoing session's publishers before the engine subscribes for the
successor, so the previous episode's EOF, readiness, rate and clock are not
replayed. Main-actor hops queued by the outgoing item's KVO drop on the
session guard instead of writing into the successor.

The nativeRemoteHLS bypass now consumes the handover too; its callsite never
read the flag, so even an active PiP window took the nil-item gap across a
native->native remote-HLS load.
@superuser404notfound
superuser404notfound merged commit 145d3c8 into superuser404notfound:main Sep 5, 2026
superuser404notfound pushed a commit that referenced this pull request Sep 5, 2026
…e layer (#158)

The in-place handover leaves the outgoing item mounted on the player layer
through the load gap, which makes `isReadyForDisplay` the one observation that
goes on firing there while every other one belongs to an item that is already
being replaced. #483 put the session guard on the five item observers and left
this one, so a hop queued by the previous episode's layer could still land after
the successor subscribed and read as the successor's first frame.

Same `sessionID == sid` guard the item observers carry.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4QpZDsHPJfbz5gkxxPXF7
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged as 145d3c8. Thanks for this, and for writing up the reasoning rather than only the patch.

Verified here before merging: both swift test runners are green on the branch (606 XCTest with 0 failures, 2634 swift-testing in 360 suites), and the Issue458AudioLanguageMetadataTests case you flagged does not fail on this toolchain, which supports your reading that it is Xcode 27 beta ICU locale data rather than anything in the change.

The nativeRemoteHLS finding is worth calling out, because it was two defects rather than one. The bypass never passed inPlaceSwap, so the item went to nil as you describe. It also never consumed pendingInPlaceItemHandover, so the flag stayed armed and the next loopback load() would have spent it on an unrelated transition. Your consume-and-reset closes both.

One follow-up pushed on top (bb36f22): layerReadyObservation was the one observer left without the session guard the others now carry. That matters specifically on this path, since the handover leaves the outgoing item mounted on the layer through the gap, so it is the observer that keeps reporting there.

One thing to aim your Apple TV round at, from reading rather than measuring. unloadCurrentItem(inPlaceSwap: true) returns before the avPlayer.pause() that precedes the item swap. That pause is the issue #15 fix for audio leading video on episode autoplay: a reused player carries rate = 1.0 across replaceCurrentItem, so the successor auto-resumes and beats the post-load waitForSwitch gate. On an iOS PiP window that costs nothing, because no panel switches. On tvOS with Match Frame Rate it is exactly the configuration the handover is for, and the gate is what keeps the first frame off a mid-transition panel. So the case to watch is a transition where the two episodes differ in frame rate or dynamic range: if the successor's audio starts while the panel is still dark, that is this, not the nil-item gap. If it reproduces, the fix belongs on the engine side and we will take it.

Rhainland pushed a commit to Rhainland/AetherEngine that referenced this pull request Oct 9, 2026
Takes upstream main through dd1d5f8: 7.14.0 to 7.28.3 plus the
unreleased transport, audio-selection and subtitle-delay changes.

Retired fork changes, now covered upstream:
- In-place handover (dfe11a0, 745de1c): upstream superuser404notfound#483, already in
  the merge base.
- Media fallback position (a48b2bb): upstream superuser404notfound#621.
- Item-death and media-fallback pause handling (fork Silo-Server#10, Silo-Server#11):
  upstream superuser404notfound#623's play intent. The fork's AVPlayer rate stamps are
  removed. A Play or Pause pressed in iOS PiP while an item dies is no
  longer counted.

Kept, and fitted onto upstream's code:
- Refreshable HTTPRequestAuthorization on upstream's sealed relay and
  CredentialScope. Static headers follow upstream's anchor scoping.
  Resolver output stays the host's per-URL decision.
- Bounded resource fetches use upstream's held-body limit, and the
  resolver preflight keeps the 2 MB playlist cap.
- Native subtitle 503 while extraction is pending,
  nativeTimelineOffsetSeconds, ASS for local primary rendering, and
  sidecar decode off the cooperative pool.
- 16-thread software decode cap; TrueHD Atmos to APAC.

New:
- The premature-end recovery keeps a pause made during its re-seek.
- An overlay-drawn injected ASS track keeps its rendition selected
  behind a suppressing legible output, so AE#616 keeps measuring
  sourceTime.
- The APAC bridge reports an object decoder that produces nothing
  (AE#641).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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