react-native-audio-browser 0.2.0-next.89 → 0.2.0-next.90
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/Player/Player.swift
CHANGED
|
@@ -704,6 +704,7 @@ final class Player {
|
|
|
704
704
|
/// without touching the new one's deferred seek.
|
|
705
705
|
private func seekItem(to seconds: TimeInterval, completion: @escaping @MainActor (Bool) -> Void) {
|
|
706
706
|
let generation = deferredSeek.generation
|
|
707
|
+
retry.seekStarted()
|
|
707
708
|
engine.seek(to: seconds) { [weak self] finished in
|
|
708
709
|
guard let self, deferredSeek.generation == generation else {
|
|
709
710
|
completion(false)
|
|
@@ -925,17 +926,9 @@ final class Player {
|
|
|
925
926
|
// MARK: - Connectivity
|
|
926
927
|
|
|
927
928
|
/// Re-renders the now-playing lines, whose stall line tells offline from buffering, and passes
|
|
928
|
-
/// the change to the retry loop.
|
|
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.
|
|
929
|
+
/// the change to the retry loop.
|
|
933
930
|
func networkStatusDidChange(isOnline: Bool) {
|
|
934
931
|
engine.renderNowPlaying()
|
|
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
932
|
retry.onlineChanged(isOnline)
|
|
940
933
|
}
|
|
941
934
|
|
|
@@ -57,6 +57,7 @@ final class RetryLoop {
|
|
|
57
57
|
|
|
58
58
|
private(set) var isEnabled = false
|
|
59
59
|
private var playIntent = false
|
|
60
|
+
private var online: Bool
|
|
60
61
|
private var budget: SilenceBudget
|
|
61
62
|
private var pacer: ReconnectPacer
|
|
62
63
|
private var connectionStartedAtMs: Int?
|
|
@@ -64,6 +65,13 @@ final class RetryLoop {
|
|
|
64
65
|
/// AVFoundation can report one connection's failure more than once; the units hear its end once.
|
|
65
66
|
private var connectionEndReported = false
|
|
66
67
|
private var replacedConnectionCounts = false
|
|
68
|
+
/// The current connection was open while offline, and the network has not returned since.
|
|
69
|
+
/// Offline can kill a connection without AVFoundation ever failing it.
|
|
70
|
+
private var connectionWentOffline = false
|
|
71
|
+
/// The buffered position when the network last returned to a connection that went offline; nil
|
|
72
|
+
/// once it plays again online. Audio still draining in from before the drop must not count as
|
|
73
|
+
/// the connection being alive.
|
|
74
|
+
private var bufferedAtReturn: Double?
|
|
67
75
|
/// True from a connection's start until a cancel or a terminal error, so a give-up reports one
|
|
68
76
|
/// error, and a stop, or a play with nothing loaded, leaves nothing to give up on.
|
|
69
77
|
private var deadlineArmed = false
|
|
@@ -103,6 +111,7 @@ final class RetryLoop {
|
|
|
103
111
|
clock: any RetryClock = ContinuousRetryClock(),
|
|
104
112
|
sleep: (@Sendable (Int) async throws -> Void)? = nil,
|
|
105
113
|
) {
|
|
114
|
+
self.online = online
|
|
106
115
|
budget = SilenceBudget(online: online)
|
|
107
116
|
pacer = ReconnectPacer(online: online)
|
|
108
117
|
self.clock = clock
|
|
@@ -157,6 +166,8 @@ final class RetryLoop {
|
|
|
157
166
|
replacedConnectionCounts = true
|
|
158
167
|
connectionDeliveredAudio = false
|
|
159
168
|
connectionEndReported = false
|
|
169
|
+
connectionWentOffline = !online
|
|
170
|
+
bufferedAtReturn = nil
|
|
160
171
|
deadlineArmed = true
|
|
161
172
|
armDeadline()
|
|
162
173
|
if playIntent { startStallWatch() }
|
|
@@ -175,17 +186,40 @@ final class RetryLoop {
|
|
|
175
186
|
armDeadline()
|
|
176
187
|
}
|
|
177
188
|
|
|
189
|
+
/// A connection that went offline and awaits data when the network returns is given up before
|
|
190
|
+
/// the units hear of the return: it ended offline.
|
|
178
191
|
func onlineChanged(_ online: Bool) {
|
|
192
|
+
guard online != self.online else { return }
|
|
193
|
+
if !online, connectionStartedAtMs != nil, !connectionEndReported {
|
|
194
|
+
connectionWentOffline = true
|
|
195
|
+
}
|
|
196
|
+
if online, connectionWentOffline {
|
|
197
|
+
connectionWentOffline = false
|
|
198
|
+
if stallTask != nil, pendingTask == nil, playIntent, isEnabled {
|
|
199
|
+
logger.info("Back online on a stalled connection that was offline: reconnecting now")
|
|
200
|
+
handleFailure(.stall)
|
|
201
|
+
} else {
|
|
202
|
+
bufferedAtReturn = bufferedPosition()
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
self.online = online
|
|
179
206
|
budget.onlineChanged(online)
|
|
180
207
|
pacer.onlineChanged(online)
|
|
181
208
|
armDeadline()
|
|
182
209
|
}
|
|
183
210
|
|
|
211
|
+
/// A seek's wait for data is the seek's, not the drop's: its data lies outside the range the drop
|
|
212
|
+
/// was judged by.
|
|
213
|
+
func seekStarted() {
|
|
214
|
+
bufferedAtReturn = nil
|
|
215
|
+
}
|
|
216
|
+
|
|
184
217
|
/// Audio clears the failure the deadline would report: a silence after it that ends with no
|
|
185
218
|
/// failure is a stall.
|
|
186
219
|
func playbackBecameAudible() {
|
|
187
220
|
stopStallWatch()
|
|
188
221
|
connectionDeliveredAudio = true
|
|
222
|
+
if online { bufferedAtReturn = nil }
|
|
189
223
|
lastFailure = nil
|
|
190
224
|
budget.audioStarted(atMs: clock.nowMs)
|
|
191
225
|
armDeadline()
|
|
@@ -308,14 +342,23 @@ final class RetryLoop {
|
|
|
308
342
|
|
|
309
343
|
// MARK: - Stalls
|
|
310
344
|
|
|
345
|
+
private var offlineKilledConnection: Bool {
|
|
346
|
+
guard let bufferedAtReturn else { return false }
|
|
347
|
+
return bufferedPosition() <= bufferedAtReturn
|
|
348
|
+
}
|
|
349
|
+
|
|
311
350
|
/// Starts the stall watch, over a connect or a stall: a `stallTimeout` in which `bufferedPosition`
|
|
312
351
|
/// does not advance makes it a `.stall` failure, provided the play intent holds and no retry is
|
|
313
|
-
/// pending; one in which it advances starts another. Any failure ends it.
|
|
352
|
+
/// pending; one in which it advances starts another. Any failure ends it. Online, a connection
|
|
353
|
+
/// the drop killed gets no timeout, unless it delivered nothing: that end counts toward
|
|
354
|
+
/// `maxRetries`, so it waits as any connect does.
|
|
314
355
|
func startStallWatch() {
|
|
315
356
|
stallTask?.cancel()
|
|
316
357
|
let buffered = bufferedPosition()
|
|
317
|
-
|
|
318
|
-
|
|
358
|
+
let killed = online && isEnabled && connectionDeliveredAudio && offlineKilledConnection
|
|
359
|
+
let timeout = killed ? .zero : stallTimeout
|
|
360
|
+
stallTask = Task { [weak self, clock] in
|
|
361
|
+
guard await (try? clock.sleep(ms: Int(timeout / .milliseconds(1)))) != nil else { return }
|
|
319
362
|
guard let self, !Task.isCancelled else { return }
|
|
320
363
|
stallTask = nil
|
|
321
364
|
if bufferedPosition() > buffered { return startStallWatch() }
|
|
@@ -702,6 +702,173 @@ struct PlayerRecoveringTests {
|
|
|
702
702
|
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
703
703
|
}
|
|
704
704
|
|
|
705
|
+
/// A short drop leaves the connection dead with audio still buffered: the stall comes after the
|
|
706
|
+
/// network is back, and AVPlayer never fails it.
|
|
707
|
+
@Test func aStallAfterAShortDropReconnectsAtOnce() async throws {
|
|
708
|
+
let (player, engine, _, clock) = try makeVirtual()
|
|
709
|
+
engine.bufferedPosition = 20
|
|
710
|
+
player.transition(.avPlayerPlaying)
|
|
711
|
+
await clock.run(until: 10_000)
|
|
712
|
+
player.networkStatusDidChange(isOnline: false)
|
|
713
|
+
await clock.run(until: 13_000)
|
|
714
|
+
player.networkStatusDidChange(isOnline: true)
|
|
715
|
+
await clock.run(until: 15_000)
|
|
716
|
+
|
|
717
|
+
player.transition(.avPlayerWaiting)
|
|
718
|
+
await clock.run(until: 15_000)
|
|
719
|
+
|
|
720
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
@Test func aConnectionThatBuffersAfterTheDropWaitsOutTheWatch() async throws {
|
|
724
|
+
let (player, engine, _, clock) = try makeVirtual()
|
|
725
|
+
engine.bufferedPosition = 20
|
|
726
|
+
player.transition(.avPlayerPlaying)
|
|
727
|
+
player.networkStatusDidChange(isOnline: false)
|
|
728
|
+
player.networkStatusDidChange(isOnline: true)
|
|
729
|
+
await clock.run(until: 5_000)
|
|
730
|
+
engine.bufferedPosition = 25
|
|
731
|
+
|
|
732
|
+
player.transition(.avPlayerWaiting)
|
|
733
|
+
await clock.run(until: 34_999)
|
|
734
|
+
#expect(engine.resolveAndLoadCalls.count == 1)
|
|
735
|
+
await clock.run(until: 35_000)
|
|
736
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
@Test func aConnectionStartedAfterTheDropWaitsOutTheWatch() async throws {
|
|
740
|
+
let (player, engine, _, clock) = try makeVirtual()
|
|
741
|
+
player.networkStatusDidChange(isOnline: false)
|
|
742
|
+
player.networkStatusDidChange(isOnline: true)
|
|
743
|
+
player.retryNow()
|
|
744
|
+
engine.emit(.itemPrepared(likelyToKeepUp: true))
|
|
745
|
+
player.transition(.avPlayerPlaying)
|
|
746
|
+
await clock.run(until: 5_000)
|
|
747
|
+
|
|
748
|
+
player.transition(.avPlayerWaiting)
|
|
749
|
+
await clock.run(until: 34_999)
|
|
750
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
751
|
+
await clock.run(until: 35_000)
|
|
752
|
+
#expect(engine.resolveAndLoadCalls.count == 3)
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/// What arrives after the drop is reported was on its way before it.
|
|
756
|
+
@Test func audioDrainingInDuringTheDropLeavesTheConnectionDead() async throws {
|
|
757
|
+
let (player, engine, _, clock) = try makeVirtual()
|
|
758
|
+
engine.bufferedPosition = 20
|
|
759
|
+
player.transition(.avPlayerPlaying)
|
|
760
|
+
await clock.run(until: 10_000)
|
|
761
|
+
player.networkStatusDidChange(isOnline: false)
|
|
762
|
+
engine.bufferedPosition = 20.4
|
|
763
|
+
player.networkStatusDidChange(isOnline: true)
|
|
764
|
+
|
|
765
|
+
player.transition(.avPlayerWaiting)
|
|
766
|
+
await clock.run(until: 10_000)
|
|
767
|
+
|
|
768
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
@Test func aSecondDropKillsAConnectionThatLivedThroughTheFirst() async throws {
|
|
772
|
+
let (player, engine, _, clock) = try makeVirtual()
|
|
773
|
+
engine.bufferedPosition = 20
|
|
774
|
+
player.transition(.avPlayerPlaying)
|
|
775
|
+
await clock.run(until: 10_000)
|
|
776
|
+
player.networkStatusDidChange(isOnline: false)
|
|
777
|
+
player.networkStatusDidChange(isOnline: true)
|
|
778
|
+
engine.bufferedPosition = 80
|
|
779
|
+
await clock.run(until: 60_000)
|
|
780
|
+
player.networkStatusDidChange(isOnline: false)
|
|
781
|
+
player.networkStatusDidChange(isOnline: true)
|
|
782
|
+
|
|
783
|
+
player.transition(.avPlayerWaiting)
|
|
784
|
+
await clock.run(until: 60_000)
|
|
785
|
+
|
|
786
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
@Test func aSeekAfterTheDropWaitsForItsData_andPlayingAgainClearsTheDrop() async throws {
|
|
790
|
+
let (player, engine, _, clock) = try makeVirtual()
|
|
791
|
+
player.transition(.avPlayerPlaying)
|
|
792
|
+
player.networkStatusDidChange(isOnline: false)
|
|
793
|
+
player.networkStatusDidChange(isOnline: true)
|
|
794
|
+
player.transition(.avPlayerWaiting)
|
|
795
|
+
// The 1 s floor from the first connect's start.
|
|
796
|
+
await clock.run(until: 1_000)
|
|
797
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
798
|
+
engine.emit(.itemPrepared(likelyToKeepUp: true))
|
|
799
|
+
player.transition(.avPlayerPlaying)
|
|
800
|
+
player.networkStatusDidChange(isOnline: false)
|
|
801
|
+
player.networkStatusDidChange(isOnline: true)
|
|
802
|
+
player.seek(to: 300)
|
|
803
|
+
player.transition(.avPlayerWaiting)
|
|
804
|
+
await clock.run(until: 2_000)
|
|
805
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
806
|
+
player.transition(.avPlayerPlaying)
|
|
807
|
+
await clock.run(until: 5_000)
|
|
808
|
+
|
|
809
|
+
player.transition(.avPlayerWaiting)
|
|
810
|
+
await clock.run(until: 34_999)
|
|
811
|
+
#expect(engine.resolveAndLoadCalls.count == 2)
|
|
812
|
+
await clock.run(until: 35_000)
|
|
813
|
+
#expect(engine.resolveAndLoadCalls.count == 3)
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/// A connect that delivered nothing ends online, and counts toward `maxRetries`: only the watch
|
|
817
|
+
/// gives it up, as without the drop.
|
|
818
|
+
@Test func aSilentConnectFromBeforeTheDropWaitsOutTheWatch() async throws {
|
|
819
|
+
let clock = VirtualClock()
|
|
820
|
+
let retry = RetryLoop(online: false, clock: clock)
|
|
821
|
+
retry.updatePolicy(from: .second(RetryConfig(
|
|
822
|
+
maxRetries: 0, maxRetryDurationMs: nil, firstConnectMaxRetryDurationMs: nil,
|
|
823
|
+
)))
|
|
824
|
+
let engine = MockPlayerEngine()
|
|
825
|
+
let player = makePlayer(engine: engine, retry: retry)
|
|
826
|
+
try player.setQueue([Track(id: "t1", src: "https://example.com/a.mp3", title: "T", live: true)])
|
|
827
|
+
player.networkStatusDidChange(isOnline: true)
|
|
828
|
+
|
|
829
|
+
player.play()
|
|
830
|
+
await clock.run(until: 29_999)
|
|
831
|
+
|
|
832
|
+
#expect(player.state == .loading)
|
|
833
|
+
#expect(engine.resolveAndLoadCalls.count == 1)
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
@Test func aStallAfterAShortDropWithRetryOffIsLeftToAVPlayer() async throws {
|
|
837
|
+
let (player, engine, callbacks, clock) = try makeVirtual()
|
|
838
|
+
player.retry.updatePolicy(from: nil)
|
|
839
|
+
player.transition(.avPlayerPlaying)
|
|
840
|
+
player.networkStatusDidChange(isOnline: false)
|
|
841
|
+
player.networkStatusDidChange(isOnline: true)
|
|
842
|
+
|
|
843
|
+
player.transition(.avPlayerWaiting)
|
|
844
|
+
await clock.run(until: 1_000)
|
|
845
|
+
|
|
846
|
+
#expect(player.state == .buffering)
|
|
847
|
+
#expect(engine.resolveAndLoadCalls.count == 1)
|
|
848
|
+
#expect(callbacks.errorEvents.isEmpty)
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/// The stall that follows a short drop ends at the deadline, 120 s into its silence, when no
|
|
852
|
+
/// connection after it delivers audio.
|
|
853
|
+
@Test func aStallAfterAShortDropStillEndsAtTheDeadline() async throws {
|
|
854
|
+
let (player, engine, callbacks, clock) = try makeVirtual()
|
|
855
|
+
player.transition(.avPlayerPlaying)
|
|
856
|
+
await clock.run(until: 10_000)
|
|
857
|
+
player.networkStatusDidChange(isOnline: false)
|
|
858
|
+
await clock.run(until: 13_000)
|
|
859
|
+
player.networkStatusDidChange(isOnline: true)
|
|
860
|
+
await clock.run(until: 15_000)
|
|
861
|
+
player.transition(.avPlayerWaiting)
|
|
862
|
+
|
|
863
|
+
await clock.run(until: 134_999)
|
|
864
|
+
#expect(player.state == .loading)
|
|
865
|
+
#expect(engine.resolveAndLoadCalls.count > 1)
|
|
866
|
+
await clock.run(until: 135_000)
|
|
867
|
+
#expect(player.state == .error)
|
|
868
|
+
#expect(player.errorStatus.error == .playbackStalled)
|
|
869
|
+
#expect(callbacks.errorEvents.count == 1)
|
|
870
|
+
}
|
|
871
|
+
|
|
705
872
|
/// The watch replaces the stall at 40 s at once, having played, then each silent connect 30 s
|
|
706
873
|
/// after it starts, 1 s and then 1.5 s later: the deadline still falls 120 s into the silence.
|
|
707
874
|
@Test func playbackThatStopsWithNoFailureEndsAtTheDeadline() async throws {
|
|
@@ -774,6 +941,7 @@ struct PlayerRecoveringTests {
|
|
|
774
941
|
)))
|
|
775
942
|
for _ in 1 ... 5 {
|
|
776
943
|
player.transition(.avPlayerWaiting)
|
|
944
|
+
player.networkStatusDidChange(isOnline: false)
|
|
777
945
|
player.networkStatusDidChange(isOnline: true)
|
|
778
946
|
await player.retry.pendingTask?.value
|
|
779
947
|
engine.emit(.itemPrepared(likelyToKeepUp: true))
|
|
@@ -788,6 +956,7 @@ struct PlayerRecoveringTests {
|
|
|
788
956
|
let (player, engine, callbacks) = try makePlaying()
|
|
789
957
|
player.retry.updatePolicy(from: nil)
|
|
790
958
|
player.transition(.avPlayerWaiting)
|
|
959
|
+
player.networkStatusDidChange(isOnline: false)
|
|
791
960
|
player.networkStatusDidChange(isOnline: true)
|
|
792
961
|
#expect(player.state == .buffering)
|
|
793
962
|
#expect(engine.resolveAndLoadCalls.count == 1)
|
|
@@ -796,8 +965,10 @@ struct PlayerRecoveringTests {
|
|
|
796
965
|
|
|
797
966
|
@Test func theNetworkReturningLeavesAPausedOrPlayingStreamAlone() throws {
|
|
798
967
|
let (player, engine, _) = try makePlaying()
|
|
968
|
+
player.networkStatusDidChange(isOnline: false)
|
|
799
969
|
player.networkStatusDidChange(isOnline: true)
|
|
800
970
|
player.pause()
|
|
971
|
+
player.networkStatusDidChange(isOnline: false)
|
|
801
972
|
player.networkStatusDidChange(isOnline: true)
|
|
802
973
|
#expect(engine.resolveAndLoadCalls.count == 1)
|
|
803
974
|
}
|
|
@@ -812,6 +983,7 @@ struct PlayerRecoveringTests {
|
|
|
812
983
|
let retrying = player.retry.pendingTask
|
|
813
984
|
let loadsBefore = engine.resolveAndLoadCalls.count
|
|
814
985
|
|
|
986
|
+
player.networkStatusDidChange(isOnline: false)
|
|
815
987
|
player.networkStatusDidChange(isOnline: true)
|
|
816
988
|
await retrying?.value
|
|
817
989
|
|
|
@@ -833,6 +1005,7 @@ struct PlayerRecoveringTests {
|
|
|
833
1005
|
@Test func theNetworkReturningReRendersBeforeReconnecting() async throws {
|
|
834
1006
|
let (player, engine, _) = try makePlaying()
|
|
835
1007
|
player.transition(.avPlayerWaiting)
|
|
1008
|
+
player.networkStatusDidChange(isOnline: false)
|
|
836
1009
|
var order: [String] = []
|
|
837
1010
|
engine.onSeamCall = { order.append($0) }
|
|
838
1011
|
let loadsBefore = engine.resolveAndLoadCalls.count
|