react-native-audio-browser 0.2.0-next.81 → 0.2.0-next.83
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/Browser/BrowseGate.swift +14 -1
- package/ios/CarPlay/CarPlayNowPlayingManager.swift +23 -19
- package/ios/CarPlay/CarPlaySession+TabBar.swift +3 -5
- package/ios/CarPlay/MediaIntentHandler.swift +7 -2
- package/ios/CarPlay/MediaIntentPlayback.swift +23 -12
- package/ios/HybridAudioBrowser+Options.swift +1 -1
- package/ios/Model/NitroTypeStubs.swift +25 -0
- package/ios/Player/ErrorStatus.swift +4 -0
- package/ios/Player/PlaybackStateMachine.swift +4 -3
- package/ios/Player/Player.swift +53 -26
- package/ios/Player/RetryLoop.swift +198 -201
- package/ios/Tests/BrowseGateTests.swift +50 -0
- package/ios/Tests/ConnectivityTests.swift +13 -0
- package/ios/Tests/MediaIntentDeadlineTests.swift +75 -0
- package/ios/Tests/PlaybackStateMachineTests.swift +6 -6
- package/ios/Tests/PlayerPolicyTests.swift +273 -51
- package/ios/Tests/PlayerTests.swift +43 -85
- package/ios/Tests/RetryLoopTests.swift +299 -120
- package/ios/Tests/SupersedingTaskTests.swift +148 -0
- package/ios/Tests/Support/TestTypes.swift +99 -8
- package/ios/Util/Connectivity.swift +12 -0
- package/ios/Util/Deadline.swift +38 -0
- package/ios/Util/NetworkMonitor.swift +2 -2
- package/ios/Util/SupersedingTask.swift +45 -0
- package/package.json +1 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
#if canImport(NitroModules)
|
|
2
|
+
import NitroModules
|
|
3
|
+
#endif
|
|
2
4
|
|
|
3
5
|
/// The Browse Gate: the app's block on browsing and searching from external surfaces,
|
|
4
6
|
/// decided per request. It never blocks the Player, the Queue or Now Playing.
|
|
@@ -54,4 +56,15 @@ final class BrowseGate {
|
|
|
54
56
|
return decision.gate ?? defaultChrome ?? Self.builtInChrome
|
|
55
57
|
}
|
|
56
58
|
}
|
|
59
|
+
|
|
60
|
+
/// The chrome to show if `request` is gated, as `decide(_:)` answers, and the gate passed to
|
|
61
|
+
/// `report`, unless the caller was cancelled: a cancelled decision fails closed, but the user
|
|
62
|
+
/// never met this gate.
|
|
63
|
+
func decide(_ request: NativeGateRequest, report: (GateEvent) -> Void) async -> Gate? {
|
|
64
|
+
let chrome = await decide(request)
|
|
65
|
+
if chrome != nil, !Task.isCancelled {
|
|
66
|
+
report(GateEvent(reason: request.reason))
|
|
67
|
+
}
|
|
68
|
+
return chrome
|
|
69
|
+
}
|
|
57
70
|
}
|
|
@@ -30,8 +30,7 @@ final class CarPlayNowPlayingManager {
|
|
|
30
30
|
|
|
31
31
|
/// Resolved on track change, not on tap, so the album line shows only when a destination exists.
|
|
32
32
|
private var albumArtistDestination: String?
|
|
33
|
-
|
|
34
|
-
private var albumArtistGeneration: UInt = 0
|
|
33
|
+
private let albumPathResolution = SupersedingTask<String, String>()
|
|
35
34
|
/// The tapped track while its load runs, which wins over `currentTrack`: CarPlay reads
|
|
36
35
|
/// `isAlbumArtistButtonEnabled` only when Now Playing is displayed, and that push comes first.
|
|
37
36
|
private var pendingAlbumArtistTrack: Track?
|
|
@@ -61,7 +60,11 @@ final class CarPlayNowPlayingManager {
|
|
|
61
60
|
|
|
62
61
|
let browserManager = audioBrowser.browserManager
|
|
63
62
|
subscriptions = [
|
|
64
|
-
browserManager.configChanged.subscribeOnMain { [weak self] _ in
|
|
63
|
+
browserManager.configChanged.subscribeOnMain { [weak self] _ in
|
|
64
|
+
// A new config may bring a new `resolveAlbumPath`, so the current track is asked again.
|
|
65
|
+
self?.albumPathResolution.cancel()
|
|
66
|
+
self?.refresh()
|
|
67
|
+
},
|
|
65
68
|
audioBrowser.playerOptionsChangedEmitter.subscribeOnMain { [weak self] in self?.refresh() },
|
|
66
69
|
browserManager.favoritesChanged.subscribeOnMain { [weak self] in self?.refreshButtons() },
|
|
67
70
|
audioBrowser.favoriteChangedEmitter.subscribeOnMain { [weak self] _ in self?.refreshButtons() },
|
|
@@ -84,7 +87,7 @@ final class CarPlayNowPlayingManager {
|
|
|
84
87
|
subscriptions = []
|
|
85
88
|
pendingAlbumArtistTrack = nil
|
|
86
89
|
// A resolve in flight must not set the shared template for the next session.
|
|
87
|
-
|
|
90
|
+
albumPathResolution.cancel()
|
|
88
91
|
}
|
|
89
92
|
|
|
90
93
|
func refresh() {
|
|
@@ -182,36 +185,37 @@ final class CarPlayNowPlayingManager {
|
|
|
182
185
|
// MARK: - Album Line
|
|
183
186
|
|
|
184
187
|
private func updateAlbumArtistDestination() {
|
|
185
|
-
albumArtistGeneration &+= 1
|
|
186
|
-
let generation = albumArtistGeneration
|
|
187
|
-
|
|
188
188
|
guard let track = pendingAlbumArtistTrack ?? player?.currentTrack else {
|
|
189
189
|
logger.debug("AlbumArtist: no current track → disabled")
|
|
190
|
-
|
|
190
|
+
settleAlbumArtistDestination(nil)
|
|
191
191
|
return
|
|
192
192
|
}
|
|
193
193
|
logger.info("AlbumArtist: track=\(track.title) albumPath=\(track.albumPath ?? "nil") resolver=\(self.config.resolveAlbumPath != nil)")
|
|
194
194
|
if let albumPath = track.albumPath {
|
|
195
|
-
|
|
195
|
+
settleAlbumArtistDestination(albumPath)
|
|
196
196
|
return
|
|
197
197
|
}
|
|
198
198
|
guard let resolver = config.resolveAlbumPath else {
|
|
199
|
-
|
|
199
|
+
settleAlbumArtistDestination(nil)
|
|
200
200
|
return
|
|
201
201
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
path = nil
|
|
202
|
+
albumPathResolution.run(key: track.identity) {
|
|
203
|
+
try await resolver(track).awaitCancellable()
|
|
204
|
+
} deliver: { [weak self] result in
|
|
205
|
+
guard let self else { return }
|
|
206
|
+
if case let .failure(error) = result {
|
|
207
|
+
logger.error("resolveAlbumPath failed: \(error.localizedDescription)")
|
|
209
208
|
}
|
|
210
|
-
|
|
211
|
-
self.setAlbumArtistDestination(path)
|
|
209
|
+
setAlbumArtistDestination(try? result.get())
|
|
212
210
|
}
|
|
213
211
|
}
|
|
214
212
|
|
|
213
|
+
/// Sets the destination without a resolve, ending any in flight so it cannot overwrite this.
|
|
214
|
+
private func settleAlbumArtistDestination(_ path: String?) {
|
|
215
|
+
albumPathResolution.cancel()
|
|
216
|
+
setAlbumArtistDestination(path)
|
|
217
|
+
}
|
|
218
|
+
|
|
215
219
|
private func setAlbumArtistDestination(_ path: String?) {
|
|
216
220
|
albumArtistDestination = path
|
|
217
221
|
let template = CPNowPlayingTemplate.shared
|
|
@@ -118,11 +118,9 @@ extension CarPlaySession {
|
|
|
118
118
|
|
|
119
119
|
/// The chrome to show if the Browse Gate gates this request, reported to JS; nil when allowed.
|
|
120
120
|
func gate(_ reason: GateReason, path: String?, search: SearchParams? = nil) async -> Gate? {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
reportGates([chrome], reason: reason)
|
|
125
|
-
return chrome
|
|
121
|
+
await audioBrowser.browseGate.decide(NativeGateRequest(reason: reason, path: path, search: search)) {
|
|
122
|
+
audioBrowser.onGate($0)
|
|
123
|
+
}
|
|
126
124
|
}
|
|
127
125
|
|
|
128
126
|
/// One decision per path, made concurrently, in path order. Reports nothing: the caller
|
|
@@ -23,8 +23,13 @@ final class MediaIntentHandler: NSObject, INPlayMediaIntentHandling {
|
|
|
23
23
|
Self.logger.info("Play media intent — query=\(criteria.query) matchesApp=\(criteria.matchesAppName) resume=\(criteria.isResume)")
|
|
24
24
|
|
|
25
25
|
Task { @MainActor in
|
|
26
|
-
let success = await MediaIntentPlayback.
|
|
27
|
-
|
|
26
|
+
let success = await withDeadline(MediaIntentPlayback.budget) { deadline in
|
|
27
|
+
await MediaIntentPlayback.play(criteria: criteria, deadline: deadline)
|
|
28
|
+
}
|
|
29
|
+
if success == nil {
|
|
30
|
+
Self.logger.error("Play media intent — no answer within \(MediaIntentPlayback.budget)")
|
|
31
|
+
}
|
|
32
|
+
completion(INPlayMediaIntentResponse(code: success == true ? .success : .failure, userActivity: nil))
|
|
28
33
|
}
|
|
29
34
|
}
|
|
30
35
|
|
|
@@ -7,14 +7,18 @@ private let logger = Logger(subsystem: "com.audiobrowser", category: "MediaInten
|
|
|
7
7
|
/// Plays a Siri play-media intent through the single voice play funnel (ADR 0002): resume, or
|
|
8
8
|
/// search, queue and play, then ask for Now Playing.
|
|
9
9
|
enum MediaIntentPlayback {
|
|
10
|
-
///
|
|
11
|
-
///
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
///
|
|
10
|
+
/// Siri gives the handle phase 10 s from its connection, and the app's launch counts against it
|
|
11
|
+
/// (WWDC20, "Empower your intents").
|
|
12
|
+
static let budget: Duration = .seconds(8)
|
|
13
|
+
|
|
14
|
+
/// Callable before React Native has booted: waits, until `deadline`, for the player and a
|
|
15
|
+
/// configured browser. Each action looks the player up again through `livePlayer(of:)`, so the
|
|
16
|
+
/// intent fails once a reload has replaced the bridge or the deadline has cancelled it. Returns
|
|
17
|
+
/// whether playback was requested; true does not mean audio has started, and a failed cold
|
|
18
|
+
/// resume still returns true.
|
|
15
19
|
@MainActor
|
|
16
|
-
static func play(criteria: MediaIntentCriteria) async -> Bool {
|
|
17
|
-
guard let (browser, player) = await playerAndConfiguredBrowser.wait(timeout: .
|
|
20
|
+
static func play(criteria: MediaIntentCriteria, deadline: ContinuousClock.Instant) async -> Bool {
|
|
21
|
+
guard let (browser, player) = await playerAndConfiguredBrowser.wait(timeout: ContinuousClock.now.duration(to: deadline)) else {
|
|
18
22
|
logger.error("browser/player not ready within budget")
|
|
19
23
|
return false
|
|
20
24
|
}
|
|
@@ -22,6 +26,7 @@ enum MediaIntentPlayback {
|
|
|
22
26
|
// persisted track.
|
|
23
27
|
if criteria.isResume {
|
|
24
28
|
if player.currentTrack != nil {
|
|
29
|
+
guard let player = livePlayer(of: browser) else { return false }
|
|
25
30
|
logger.info("resume: warm — playing current track")
|
|
26
31
|
player.play()
|
|
27
32
|
} else {
|
|
@@ -49,9 +54,11 @@ enum MediaIntentPlayback {
|
|
|
49
54
|
playlist: criteria.playlist,
|
|
50
55
|
reference: criteria.reference == .my ? .my : .unknown,
|
|
51
56
|
)
|
|
52
|
-
guard await browser.browseGate.decide(
|
|
57
|
+
guard await browser.browseGate.decide(
|
|
58
|
+
NativeGateRequest(reason: .search, path: nil, search: params),
|
|
59
|
+
report: { browser.onGate($0) },
|
|
60
|
+
) == nil else {
|
|
53
61
|
logger.info("search refused — gated")
|
|
54
|
-
browser.onGate(GateEvent(reason: .search))
|
|
55
62
|
return false
|
|
56
63
|
}
|
|
57
64
|
do {
|
|
@@ -60,16 +67,20 @@ enum MediaIntentPlayback {
|
|
|
60
67
|
try player.setQueue(tracks, initialIndex: 0, playWhenReady: true)
|
|
61
68
|
browser.showNowPlayingRequestedEmitter.emit(())
|
|
62
69
|
return true
|
|
70
|
+
} catch is CancellationError {
|
|
71
|
+
return false
|
|
63
72
|
} catch {
|
|
64
73
|
logger.error("search failed: \(error.localizedDescription)")
|
|
65
74
|
return false
|
|
66
75
|
}
|
|
67
76
|
}
|
|
68
77
|
|
|
69
|
-
/// `browser`'s player while `browser` is still the live bridge
|
|
70
|
-
/// destroyed player
|
|
78
|
+
/// `browser`'s player while `browser` is still the live bridge, as a replaced bridge keeps its
|
|
79
|
+
/// destroyed player, and while the intent is not cancelled: once `withDeadline` has answered
|
|
80
|
+
/// Siri, nothing plays.
|
|
71
81
|
@MainActor
|
|
72
82
|
private static func livePlayer(of browser: HybridAudioBrowser) -> Player? {
|
|
73
|
-
HybridAudioBrowser.shared === browser
|
|
83
|
+
guard !Task.isCancelled, HybridAudioBrowser.shared === browser else { return nil }
|
|
84
|
+
return browser.player
|
|
74
85
|
}
|
|
75
86
|
}
|
|
@@ -29,7 +29,7 @@ public extension HybridAudioBrowser {
|
|
|
29
29
|
nowPlayingEnabled: options.autoUpdateNowPlayingMetadata ?? true,
|
|
30
30
|
nowPlayingFormatter: options.nowPlayingMetadataFormatter,
|
|
31
31
|
)
|
|
32
|
-
let newPlayer = Player(engine: newEngine, callbacks: self, retry: RetryLoop(
|
|
32
|
+
let newPlayer = Player(engine: newEngine, callbacks: self, retry: RetryLoop(online: networkMonitor.isOnline))
|
|
33
33
|
if let retry = options.retry { newPlayer.retry.updatePolicy(from: retry) }
|
|
34
34
|
newEngine.nowPlayingUpdater.source = newPlayer
|
|
35
35
|
engine = newEngine
|
|
@@ -177,6 +177,31 @@
|
|
|
177
177
|
var reference: MediaReference
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
+
enum GateReason {
|
|
181
|
+
case browse
|
|
182
|
+
case search
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
struct Gate {
|
|
186
|
+
var title: String
|
|
187
|
+
var message: String?
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
struct GateDecision {
|
|
191
|
+
var gated: Bool
|
|
192
|
+
var gate: Gate?
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
struct NativeGateRequest {
|
|
196
|
+
var reason: GateReason
|
|
197
|
+
var path: String?
|
|
198
|
+
var search: SearchParams?
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
struct GateEvent {
|
|
202
|
+
var reason: GateReason
|
|
203
|
+
}
|
|
204
|
+
|
|
180
205
|
struct ImageContext {
|
|
181
206
|
var width: Double?
|
|
182
207
|
var height: Double?
|
|
@@ -8,9 +8,10 @@ func nextPlaybackState(from current: PlaybackState, on event: PlaybackEvent) ->
|
|
|
8
8
|
case .trackLoading: return .loading
|
|
9
9
|
case .trackUnloaded: return PlaybackState.none
|
|
10
10
|
case .trackEndedNaturally: return .ended
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
case .
|
|
11
|
+
// .error holds against the AVPlayer's own events: an item still connecting when the retry gave
|
|
12
|
+
// up can report them late. Leaving .error takes an explicit reload, through .loading.
|
|
13
|
+
case .avPlayerWaiting: return current == .error ? nil : .buffering
|
|
14
|
+
case .avPlayerPlaying, .audioFrameDecoded: return current == .error ? nil : .playing
|
|
14
15
|
case .errorOccurred: return .error
|
|
15
16
|
case .retryCancelled:
|
|
16
17
|
guard current != .stopped, current != .ended, current != .error else { return nil }
|
package/ios/Player/Player.swift
CHANGED
|
@@ -49,8 +49,6 @@ final class Player {
|
|
|
49
49
|
/// One track's playback session, which automatic retry reloads keep (glossary: Load).
|
|
50
50
|
struct Load: Equatable {
|
|
51
51
|
let id: UInt
|
|
52
|
-
/// Audio has rendered: retries get the recovery budget, not the first-connect one (ADR 0004).
|
|
53
|
-
var hasPlayed = false
|
|
54
52
|
/// The silent primer has run for this load's first failure (ADR 0005).
|
|
55
53
|
var primedNowPlaying = false
|
|
56
54
|
/// One of this load's items is in the engine; until then it reports the previous one's position.
|
|
@@ -73,7 +71,7 @@ final class Player {
|
|
|
73
71
|
private func beginLoad() {
|
|
74
72
|
loadCount &+= 1
|
|
75
73
|
load = Load(id: loadCount)
|
|
76
|
-
retry.
|
|
74
|
+
retry.loadStarted()
|
|
77
75
|
deferredSeek.reset()
|
|
78
76
|
}
|
|
79
77
|
|
|
@@ -88,7 +86,8 @@ final class Player {
|
|
|
88
86
|
rate = oldValue
|
|
89
87
|
return
|
|
90
88
|
}
|
|
91
|
-
|
|
89
|
+
// The intent stays up in .error, over an item the retry gave up on.
|
|
90
|
+
engine.setRate(rate, playing: playWhenReady && state != .error)
|
|
92
91
|
persistence.save()
|
|
93
92
|
}
|
|
94
93
|
}
|
|
@@ -179,6 +178,7 @@ final class Player {
|
|
|
179
178
|
func setPlayWhenReady(_ newValue: Bool) {
|
|
180
179
|
let changed = newValue != playWhenReady
|
|
181
180
|
playWhenReady = newValue
|
|
181
|
+
retry.playIntentChanged(newValue)
|
|
182
182
|
// A play during an interruption owns the intent: a later pause must not be auto-resumed.
|
|
183
183
|
if newValue { shouldResumeAfterInterruption = false }
|
|
184
184
|
|
|
@@ -192,8 +192,9 @@ final class Player {
|
|
|
192
192
|
if !newValue { engine.pause() } else if state != .loading { engine.play() }
|
|
193
193
|
|
|
194
194
|
guard changed else { return }
|
|
195
|
-
// A resume can find the state still .buffering (a pause near the end leaves it)
|
|
196
|
-
|
|
195
|
+
// A resume can find the state still .buffering (a pause near the end leaves it) or .loading,
|
|
196
|
+
// with no transition to start the watch.
|
|
197
|
+
if newValue, state.awaitsData { retry.startStallWatch() }
|
|
197
198
|
// Left to run, the retry would give up on the dropped intent and surface .error.
|
|
198
199
|
if !newValue, retry.pendingTask != nil { parkForReloadOnPlay() }
|
|
199
200
|
// A pause during the sleep fade ends the timer early, restoring the pre-fade volume.
|
|
@@ -251,13 +252,22 @@ final class Player {
|
|
|
251
252
|
sleepTimerManager.onComplete = { [weak self] in self?.pause() }
|
|
252
253
|
sleepTimerManager.onChanged = { [weak self] in self?.callbacks?.playerDidChangeSleepTimer($0) }
|
|
253
254
|
|
|
254
|
-
retry.onError = { [weak self] in
|
|
255
|
+
retry.onError = { [weak self] error in
|
|
256
|
+
guard let self else { return }
|
|
257
|
+
// The deadline can pass with a connect in flight: its item must neither play nor fail
|
|
258
|
+
// under the error.
|
|
259
|
+
engine.cancelLoading()
|
|
260
|
+
engine.stopObservingItem()
|
|
261
|
+
transition(.errorOccurred(error))
|
|
262
|
+
}
|
|
255
263
|
retry.onRetryingError = { [weak self] in self?.reportRetryingError($0) }
|
|
256
264
|
retry.onFailedWithoutIntent = { [weak self] in self?.parkForReloadOnPlay() }
|
|
257
265
|
retry.onRetry = { [weak self] in self?.reloadResolving(startFromCurrentTime: $0) }
|
|
258
|
-
|
|
259
|
-
retry.
|
|
260
|
-
|
|
266
|
+
// Until the connect's item is prepared, the engine still holds the item it replaces.
|
|
267
|
+
retry.bufferedPosition = { [weak self] in
|
|
268
|
+
guard let self, connectionItemPrepared else { return 0 }
|
|
269
|
+
return engine.bufferedPosition
|
|
270
|
+
}
|
|
261
271
|
}
|
|
262
272
|
|
|
263
273
|
/// Shows a failure the retry loop is still working on over the current state: not a
|
|
@@ -348,9 +358,10 @@ final class Player {
|
|
|
348
358
|
} else if old == .playing {
|
|
349
359
|
retry.playbackStoppedBeingAudible()
|
|
350
360
|
}
|
|
361
|
+
// A reload within .loading is no transition: the retry loop watches each connect itself.
|
|
351
362
|
if new == .buffering, playWhenReady {
|
|
352
363
|
retry.startStallWatch()
|
|
353
|
-
} else if old
|
|
364
|
+
} else if old.awaitsData, !new.awaitsData {
|
|
354
365
|
retry.stopStallWatch()
|
|
355
366
|
}
|
|
356
367
|
|
|
@@ -367,6 +378,7 @@ final class Player {
|
|
|
367
378
|
// A stall or a lost connection can fail an item that is still alive: data arriving later
|
|
368
379
|
// must not play it under the error.
|
|
369
380
|
engine.pause()
|
|
381
|
+
retry.cancel()
|
|
370
382
|
default: break
|
|
371
383
|
}
|
|
372
384
|
|
|
@@ -390,13 +402,14 @@ final class Player {
|
|
|
390
402
|
|
|
391
403
|
func stop() {
|
|
392
404
|
// A pending retry would turn the stop into .error, and the next play must start over rather
|
|
393
|
-
// than resume where it failed.
|
|
405
|
+
// than resume where it failed. Cancelled first, or the lowered intent parks it for a reload.
|
|
394
406
|
retry.cancel()
|
|
395
407
|
load?.reloadOnPlay = false
|
|
396
408
|
shouldResumeAfterInterruption = false
|
|
397
409
|
engine.clearTimedMetadata()
|
|
398
|
-
transition
|
|
410
|
+
// Before the transition, whose render the formatter reads the intent from.
|
|
399
411
|
setPlayWhenReady(false)
|
|
412
|
+
transition(.stopped)
|
|
400
413
|
deferredSeek.reset()
|
|
401
414
|
if currentTrack?.live != true { seek(to: 0) }
|
|
402
415
|
}
|
|
@@ -627,7 +640,7 @@ final class Player {
|
|
|
627
640
|
persistence.save()
|
|
628
641
|
|
|
629
642
|
if let src = currentTrack.src {
|
|
630
|
-
|
|
643
|
+
connect(src: src, track: currentTrack)
|
|
631
644
|
} else {
|
|
632
645
|
logger.error("No src for track \(currentTrack.path ?? currentTrack.title)")
|
|
633
646
|
engine.clearItem()
|
|
@@ -731,6 +744,14 @@ final class Player {
|
|
|
731
744
|
return
|
|
732
745
|
}
|
|
733
746
|
prepareForReload(resumingAt: startFromCurrentTime ? reloadPosition : nil)
|
|
747
|
+
connect(src: src, track: track)
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
private var connectionItemPrepared = false
|
|
751
|
+
|
|
752
|
+
private func connect(src: String, track: Track) {
|
|
753
|
+
connectionItemPrepared = false
|
|
754
|
+
retry.connectionStarted()
|
|
734
755
|
engine.resolveAndLoad(src: src, track: track)
|
|
735
756
|
}
|
|
736
757
|
|
|
@@ -741,7 +762,7 @@ final class Player {
|
|
|
741
762
|
}
|
|
742
763
|
|
|
743
764
|
private func parkForReloadOnPlay() {
|
|
744
|
-
retry.
|
|
765
|
+
retry.cancel()
|
|
745
766
|
if let position = reloadPosition { deferredSeek.capture(position) }
|
|
746
767
|
load?.reloadOnPlay = true
|
|
747
768
|
transition(.retryCancelled)
|
|
@@ -762,7 +783,7 @@ final class Player {
|
|
|
762
783
|
engine.recreate()
|
|
763
784
|
} else {
|
|
764
785
|
engine.cancelLoading()
|
|
765
|
-
engine.
|
|
786
|
+
engine.stopObservingItem()
|
|
766
787
|
engine.pause()
|
|
767
788
|
}
|
|
768
789
|
if let target { deferredSeek.capture(target) }
|
|
@@ -797,9 +818,12 @@ final class Player {
|
|
|
797
818
|
switch event {
|
|
798
819
|
case let .itemPrepared(likelyToKeepUp):
|
|
799
820
|
load?.itemPrepared = true
|
|
821
|
+
connectionItemPrepared = true
|
|
800
822
|
if playWhenReady { engine.play() }
|
|
801
823
|
if !beginDeferredSeek(), likelyToKeepUp { bufferBecameSufficient() }
|
|
802
824
|
case let .loadFailed(error):
|
|
825
|
+
// A failure after the terminal error is the same connection's, reported late.
|
|
826
|
+
guard state != .error else { return }
|
|
803
827
|
primeNowPlayingIfNeeded()
|
|
804
828
|
retry.handleError(error, context: .mediaLoad)
|
|
805
829
|
case .unplayable:
|
|
@@ -809,6 +833,7 @@ final class Player {
|
|
|
809
833
|
primeNowPlayingIfNeeded()
|
|
810
834
|
transition(.errorOccurred(.invalidSourceUrl(url)))
|
|
811
835
|
case let .failed(error, httpStatusCode):
|
|
836
|
+
guard state != .error else { return }
|
|
812
837
|
retry.handleError(error, context: .playback, httpStatusCode: httpStatusCode)
|
|
813
838
|
case let .likelyToKeepUp(isLikely):
|
|
814
839
|
guard isLikely, engine.hasItem, !beginDeferredSeek() else { return }
|
|
@@ -817,12 +842,11 @@ final class Player {
|
|
|
817
842
|
recoverIfAirPlayStalled(transitioningTo: status, waitingForNoItem: waitingForNoItem)
|
|
818
843
|
timeControlStatusDidChange(status)
|
|
819
844
|
case .audioDidStart:
|
|
820
|
-
load?.hasPlayed = true
|
|
821
845
|
if state != .loading { transition(.audioFrameDecoded) }
|
|
822
846
|
case .playedToEnd:
|
|
823
847
|
itemDidPlayToEnd()
|
|
824
848
|
case .stalled:
|
|
825
|
-
// A nudge only: this also arrives while audio plays, so the stall watch keys on .
|
|
849
|
+
// A nudge only: this also arrives while audio plays, so the stall watch keys on the state.
|
|
826
850
|
if playWhenReady, state != .error { engine.play() }
|
|
827
851
|
case let .timedMetadata(metadata):
|
|
828
852
|
callbacks?.playerDidReceiveTimedMetadata(metadata)
|
|
@@ -900,16 +924,19 @@ final class Player {
|
|
|
900
924
|
|
|
901
925
|
// MARK: - Connectivity
|
|
902
926
|
|
|
903
|
-
/// Re-renders the now-playing lines, whose stall line tells offline from buffering, and
|
|
904
|
-
/// connectivity returns
|
|
905
|
-
///
|
|
906
|
-
///
|
|
907
|
-
///
|
|
927
|
+
/// Re-renders the now-playing lines, whose stall line tells offline from buffering, and passes
|
|
928
|
+
/// the change to the retry loop. When connectivity returns to a stalled stream, its connection
|
|
929
|
+
/// is given up first, as one that ended offline, so the reconnect goes out at once rather than
|
|
930
|
+
/// wait for AVPlayer to fail it. Only while stalled (.buffering) with play intent, so a pause or
|
|
931
|
+
/// a retry that gave up isn't resumed; not while a retry is pending, which the loop paces itself;
|
|
932
|
+
/// and not with retry off, which leaves the stall to AVPlayer.
|
|
908
933
|
func networkStatusDidChange(isOnline: Bool) {
|
|
909
934
|
engine.renderNowPlaying()
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
935
|
+
if isOnline, playWhenReady, state == .buffering, retry.isEnabled, retry.pendingTask == nil {
|
|
936
|
+
logger.info("Connectivity restored while stalled — retrying now")
|
|
937
|
+
retry.handleFailure(.stall)
|
|
938
|
+
}
|
|
939
|
+
retry.onlineChanged(isOnline)
|
|
913
940
|
}
|
|
914
941
|
|
|
915
942
|
/// Time playback must have progressed before a buffer dip counts as a stall, so the initial
|