Skip to content

Commit 1386a80

Browse files
committed
fix: preserve native item across foreground replacements
1 parent 52881fd commit 1386a80

4 files changed

Lines changed: 121 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ the public-API contract.
1010

1111
## [Unreleased]
1212

13-
_Nothing yet._
13+
### Fixed
14+
15+
- **Foreground native-to-native replacements can keep the outgoing item attached until the new
16+
item is ready to swap in.** `prepareForItemReplacement()` gives a host the same nil-item-gap
17+
protection that Aether already applies to active Picture in Picture without making the host
18+
pretend PiP is active. The request is consumed by exactly one `load()`, is ignored after a
19+
non-native session, and is cancelled by `stop()`, so it cannot alter an unrelated later load.
1420

1521
## [6.64.0] - 2026-09-02
1622

‎Sources/AetherEngine/AetherEngine.swift‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,13 @@ public final class AetherEngine: ObservableObject {
705705
/// AE#446 round 4: bounded audit after a rejoin swap. See `auditLiveRejoinPlacement`.
706706
var liveRejoinAuditUntil: Date?
707707
var liveRejoinAuditLastEmit: Date?
708+
/// One-shot host request to keep the current native item attached until the next load can
709+
/// replace it atomically. This covers foreground playlist/episode changes that need the same
710+
/// nil-item-gap protection as PiP without pretending that Picture in Picture is active.
711+
private var nextLoadRequestsInPlaceItemHandover = false
708712
/// AE#158: set by load() when the running item must survive until the new master attaches (PiP
709-
/// next-episode handover); consumed and reset by the loopback host.load callsite (inPlaceSwap).
713+
/// or host-requested handover); consumed and reset by the loopback host.load callsite
714+
/// (inPlaceSwap).
710715
var pendingInPlaceItemHandover = false
711716
/// SW-PiP bridge, the software-path analog of `currentAVPlayer`: set when a SW session has its
712717
/// display layer, nil on teardown. Hosts build their sample-buffer PiP ContentSource from it.
@@ -776,11 +781,29 @@ public final class AetherEngine: ObservableObject {
776781
enabled && pipActive
777782
}
778783

779-
/// AE#158: a system PiP window closes the moment its source layer's player drops its item (the #93
780-
/// in-PiP recovery reload hit the same nil-item gap), so a native->native load while PiP is active
781-
/// keeps the old item attached through the load gap and swaps in place once the new master is ready.
782-
nonisolated static func shouldHandOverItemInPlace(pipActive: Bool, priorBackendWasNative: Bool) -> Bool {
783-
pipActive && priorBackendWasNative
784+
/// AE#158: a native AVPlayer can lose its visible picture when a replacement load temporarily
785+
/// drops its item. PiP always requires an in-place handover; a foreground host can explicitly
786+
/// request the same behavior for a playlist or episode transition. Neither applies unless the
787+
/// outgoing backend is native.
788+
nonisolated static func shouldHandOverItemInPlace(
789+
pipActive: Bool,
790+
hostRequested: Bool,
791+
priorBackendWasNative: Bool
792+
) -> Bool {
793+
priorBackendWasNative && (pipActive || hostRequested)
794+
}
795+
796+
/// Consume the one-shot foreground request while folding it with PiP's mandatory handover.
797+
/// Keeping the consume at the decision boundary prevents one episode transition from changing
798+
/// the teardown behavior of a later, unrelated load.
799+
func consumeInPlaceItemHandoverRequest(priorBackendWasNative: Bool) -> Bool {
800+
let hostRequested = nextLoadRequestsInPlaceItemHandover
801+
nextLoadRequestsInPlaceItemHandover = false
802+
return Self.shouldHandOverItemInPlace(
803+
pipActive: pictureInPictureActive,
804+
hostRequested: hostRequested,
805+
priorBackendWasNative: priorBackendWasNative
806+
)
784807
}
785808

786809
/// SW-PiP: playable range for the sample-buffer PiP UI on the PTS axis of the enqueued frames
@@ -3020,10 +3043,12 @@ public final class AetherEngine: ObservableObject {
30203043
// registration survives the seam (issue #15). Captured before stopInternal resets playbackBackend;
30213044
// the SW dispatch branch releases it if this source routes software.
30223045
let priorBackendWasNative = (playbackBackend == .native)
3023-
// AE#158: while a PiP window is live, the running item must survive this load's teardown or the
3024-
// system closes the window; the loopback host.load callsite finishes the handover (inPlaceSwap).
3025-
let handOverInPlace = Self.shouldHandOverItemInPlace(pipActive: pictureInPictureActive,
3026-
priorBackendWasNative: priorBackendWasNative)
3046+
// AE#158: PiP and an explicit foreground replacement request both need the running item to
3047+
// survive this load's teardown. Consume the host request here so it can affect only one load;
3048+
// the loopback host.load callsite finishes the handover (inPlaceSwap).
3049+
let handOverInPlace = consumeInPlaceItemHandoverRequest(
3050+
priorBackendWasNative: priorBackendWasNative
3051+
)
30273052
pendingInPlaceItemHandover = handOverInPlace
30283053
// #128 follow-up: preserve the previous session's display criteria across the load seam. Nil-ing it
30293054
// here bounces the panel through SDR before apply() re-negotiates the same mode on video->video
@@ -4837,6 +4862,16 @@ public final class AetherEngine: ObservableObject {
48374862
await seek(to: seconds)
48384863
}
48394864

4865+
/// Keep the current native AVPlayer item attached until the next `load` replaces it.
4866+
///
4867+
/// Call this immediately before a foreground playlist or episode replacement when a nil-item
4868+
/// gap would blank the existing player layer. The request is consumed by the next `load` and
4869+
/// has no effect when the outgoing session is not using the native backend. It does not pause,
4870+
/// stop, or otherwise change transport state.
4871+
public func prepareForItemReplacement() {
4872+
nextLoadRequestsInPlaceItemHandover = true
4873+
}
4874+
48404875
/// Stop playback and tear the session down.
48414876
///
48424877
/// - Parameter resetDisplayCriteria: `true` (default) returns the panel to its default HDMI mode
@@ -4852,6 +4887,7 @@ public final class AetherEngine: ObservableObject {
48524887
/// pair (`resetDisplayCriteria: false`) and *is* genuinely leaving playback can pass
48534888
/// `finalTeardown: true`. Only a final teardown honours `deactivatesAudioSessionOnStop`.
48544889
public func stop(resetDisplayCriteria: Bool = true, finalTeardown: Bool? = nil) {
4890+
nextLoadRequestsInPlaceItemHandover = false
48554891
stopInternal(resetDisplayCriteria: resetDisplayCriteria,
48564892
finalTeardown: finalTeardown ?? resetDisplayCriteria)
48574893
state = .idle
Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,73 @@
11
import Testing
22
@testable import AetherEngine
33

4-
/// AE#158: a system PiP window closes the moment its source layer's player drops its item, so a
5-
/// native->native load while PiP is active keeps the running item attached until the new master
6-
/// swaps in place. See AetherEngine.shouldHandOverItemInPlace.
7-
@Suite("PiP in-place item handover policy")
4+
/// AE#158: a native player can lose its visible presentation when its source layer's player drops
5+
/// its item, so PiP and explicit foreground replacements keep the running item attached until the
6+
/// new master swaps in place. See AetherEngine.shouldHandOverItemInPlace.
7+
@Suite("Native in-place item handover policy")
88
struct PiPItemHandoverTests {
9-
@Test("hands over in place only while PiP is active on a native session")
10-
func handsOverOnlyForNativePiP() {
11-
#expect(AetherEngine.shouldHandOverItemInPlace(pipActive: true, priorBackendWasNative: true) == true)
12-
#expect(AetherEngine.shouldHandOverItemInPlace(pipActive: false, priorBackendWasNative: true) == false)
13-
#expect(AetherEngine.shouldHandOverItemInPlace(pipActive: true, priorBackendWasNative: false) == false)
14-
#expect(AetherEngine.shouldHandOverItemInPlace(pipActive: false, priorBackendWasNative: false) == false)
9+
@Test("hands over for PiP or an explicit host request only from a native session")
10+
func handsOverForNativePiPOrHostRequest() {
11+
#expect(AetherEngine.shouldHandOverItemInPlace(
12+
pipActive: true, hostRequested: false, priorBackendWasNative: true
13+
))
14+
#expect(AetherEngine.shouldHandOverItemInPlace(
15+
pipActive: false, hostRequested: true, priorBackendWasNative: true
16+
))
17+
#expect(AetherEngine.shouldHandOverItemInPlace(
18+
pipActive: true, hostRequested: true, priorBackendWasNative: true
19+
))
20+
#expect(!AetherEngine.shouldHandOverItemInPlace(
21+
pipActive: false, hostRequested: false, priorBackendWasNative: true
22+
))
23+
#expect(!AetherEngine.shouldHandOverItemInPlace(
24+
pipActive: true, hostRequested: true, priorBackendWasNative: false
25+
))
26+
}
27+
28+
@Test("a foreground request is consumed exactly once")
29+
@MainActor
30+
func foregroundRequestIsOneShot() throws {
31+
let engine = try AetherEngine()
32+
33+
engine.prepareForItemReplacement()
34+
35+
#expect(engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: true))
36+
#expect(!engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: true))
37+
engine.stop()
38+
}
39+
40+
@Test("a non-native load consumes rather than leaks the foreground request")
41+
@MainActor
42+
func nonNativeLoadConsumesRequest() throws {
43+
let engine = try AetherEngine()
44+
45+
engine.prepareForItemReplacement()
46+
47+
#expect(!engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: false))
48+
#expect(!engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: true))
49+
engine.stop()
50+
}
51+
52+
@Test("a final stop cancels a pending foreground request")
53+
@MainActor
54+
func stopCancelsPendingRequest() throws {
55+
let engine = try AetherEngine()
56+
57+
engine.prepareForItemReplacement()
58+
engine.stop()
59+
60+
#expect(!engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: true))
61+
}
62+
63+
@Test("PiP remains mandatory without a foreground request")
64+
@MainActor
65+
func pipHandoverRemainsIndependent() throws {
66+
let engine = try AetherEngine()
67+
engine.pictureInPictureActive = true
68+
69+
#expect(engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: true))
70+
#expect(engine.consumeInPlaceItemHandoverRequest(priorBackendWasNative: true))
71+
engine.stop()
1572
}
1673
}

‎docs/api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ try await player.reloadAtCurrentPosition()
299299
| --- | --- |
300300
| `load(url:startPosition:options:audioSourceStreamIndex:discTitleID:)` | `async throws -> SourceProbe?`. Discardable. Tears down any running session first. |
301301
| `load(source:startPosition:options:audioSourceStreamIndex:discTitleID:)` | Same, for `MediaSource.url` or `.custom(IOReader, formatHint:)`. A custom source whose initial probe fails throws, since it cannot be reopened by URL. |
302+
| `prepareForItemReplacement()` | One-shot request for the next `load` to keep the current native item attached until its successor replaces it atomically. Use for foreground playlist or episode handoffs that must avoid a nil-item video gap. |
302303
| `reloadAtCurrentPosition()` | `async throws`. Background reopen at the current position, preserving options. Session-preserving: it finishes an installed audio tap and keeps the native host where it can. |
303304
| `stop(resetDisplayCriteria:finalTeardown:)` | Ends the session, `state` becomes `.idle`, `startupProgress` becomes nil. `resetDisplayCriteria: false` keeps the panel in its current mode across an item handoff. |
304305
| `AetherEngine.probe(url:options:)` / `probe(source:options:)` | `nonisolated static throws -> SourceProbe`. Demux-only metadata read, no decoders, no session. `options` is read for `httpHeaders` only. For a custom reader the caller keeps ownership, `close()` is not called, and the cursor is left unspecified. |

0 commit comments

Comments
 (0)