react-native-audio-browser 0.2.0-next.76 → 0.2.0-next.78

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.
Files changed (50) hide show
  1. package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +1 -1
  2. package/android/src/main/java/com/audiobrowser/player/EnginePlayback.kt +9 -5
  3. package/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +71 -39
  4. package/android/src/main/java/com/audiobrowser/player/LiveStreamExtractor.kt +65 -0
  5. package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +12 -3
  6. package/android/src/main/java/com/audiobrowser/player/NowPlayingUpdater.kt +5 -0
  7. package/android/src/main/java/com/audiobrowser/player/Player.kt +78 -36
  8. package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +6 -0
  9. package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +1 -1
  10. package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +48 -2
  11. package/android/src/test/java/androidx/media3/session/SessionViews.kt +10 -0
  12. package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +372 -0
  13. package/android/src/test/java/com/audiobrowser/player/PlayerSetupEndedTest.kt +221 -0
  14. package/android/src/test/java/com/audiobrowser/player/PlayerSetupTest.kt +17 -7
  15. package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +183 -7
  16. package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +48 -0
  17. package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +236 -0
  18. package/android/src/test/java/com/audiobrowser/player/SessionViewRulesTest.kt +62 -0
  19. package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +188 -0
  20. package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +23 -0
  21. package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +74 -0
  22. package/ios/AVPlayerEngine.swift +5 -1
  23. package/ios/Browser/BrowserManager.swift +2 -2
  24. package/ios/Browser/PageResolver.swift +14 -19
  25. package/ios/Browser/RequestPipeline.swift +6 -4
  26. package/ios/CarPlay/CarPlayNowPlayingManager.swift +1 -1
  27. package/ios/CarPlay/CarPlaySession+PageLoader.swift +1 -0
  28. package/ios/CarPlay/CarPlaySession+TabBar.swift +2 -0
  29. package/ios/Extension/Promise+AwaitCallback.swift +33 -0
  30. package/ios/HybridAudioBrowser+Player.swift +1 -1
  31. package/ios/HybridAudioBrowser.swift +1 -1
  32. package/ios/Player/MediaLoader.swift +1 -1
  33. package/ios/Player/PlaybackFailure.swift +3 -3
  34. package/ios/Player/Player.swift +67 -49
  35. package/ios/Player/PlayerEngine.swift +2 -0
  36. package/ios/Player/RetryLoop.swift +72 -12
  37. package/ios/Tests/AwaitCallbackTests.swift +210 -0
  38. package/ios/Tests/GenerationMemoTests.swift +57 -0
  39. package/ios/Tests/PlaybackFailureTests.swift +7 -0
  40. package/ios/Tests/PlayerPolicyTests.swift +346 -6
  41. package/ios/Tests/PlayerTests.swift +79 -1
  42. package/ios/Tests/RetryLoopTests.swift +30 -5
  43. package/ios/Tests/Support/TestTypes.swift +15 -7
  44. package/ios/Tests/Support/TimeLimit.swift +43 -0
  45. package/ios/Util/AwaitCallback.swift +80 -0
  46. package/ios/Util/GenerationMemo.swift +11 -4
  47. package/package.json +1 -1
  48. package/android/src/test/java/com/audiobrowser/player/SessionErrorMaskTest.kt +0 -63
  49. package/ios/Player/EndOfTrackJudgement.swift +0 -25
  50. package/ios/Tests/EndOfTrackJudgementTests.swift +0 -30
@@ -0,0 +1,80 @@
1
+ import Foundation
2
+
3
+ /// How long a JS callback with a fallback may stay silent before its caller falls back.
4
+ let callbackFallbackTimeout: Duration = .seconds(10)
5
+
6
+ /// Thrown when a JS callback stays silent past its timeout. Not a `CancellationError`: the
7
+ /// caller is still running and falls back.
8
+ struct CallbackTimeoutError: Error {}
9
+
10
+ /// Hands the subscriber the closure to call with the callback's result. Calls after the first,
11
+ /// and any after the wait has ended, are ignored.
12
+ typealias CallbackSubscription<T> = (_ deliver: @escaping @Sendable (Result<T, Error>) -> Void) -> Void
13
+
14
+ /// The result `subscribe` delivers. Unlike Nitro's `Promise.await`, a cancelled caller throws
15
+ /// `CancellationError` at once, and with a `timeout`, a silent callback throws
16
+ /// `CallbackTimeoutError` once it elapses.
17
+ nonisolated(nonsending) func awaitCallback<T>(timeout: Duration? = nil, _ subscribe: CallbackSubscription<T>) async throws -> T {
18
+ let once = ResumeOnce<T>()
19
+ let timer = timeout.map { timeout in
20
+ Task {
21
+ try await Task.sleep(for: timeout)
22
+ once.resume(.failure(CallbackTimeoutError()))
23
+ }
24
+ }
25
+ defer { timer?.cancel() }
26
+ return try await withTaskCancellationHandler {
27
+ try await withCheckedThrowingContinuation { continuation in
28
+ once.start(continuation)
29
+ subscribe { once.resume($0) }
30
+ }
31
+ } onCancel: {
32
+ once.resume(.failure(CancellationError()))
33
+ }
34
+ }
35
+
36
+ /// Resumes its continuation with the first result only, whichever thread sends it and whether it
37
+ /// comes before or after the continuation exists. A result is handed over once and never read
38
+ /// again, hence `nonisolated(unsafe)`.
39
+ private final class ResumeOnce<T>: @unchecked Sendable {
40
+ private enum State {
41
+ case waiting
42
+ case early(Result<T, Error>)
43
+ case started(CheckedContinuation<T, Error>)
44
+ case done
45
+ }
46
+
47
+ private let lock = NSLock()
48
+ private var state = State.waiting
49
+
50
+ func start(_ continuation: CheckedContinuation<T, Error>) {
51
+ let early: Result<T, Error>? = lock.withLock {
52
+ guard case let .early(result) = state else {
53
+ state = .started(continuation)
54
+ return nil
55
+ }
56
+ state = .done
57
+ return result
58
+ }
59
+ guard let early else { return }
60
+ nonisolated(unsafe) let result = early
61
+ continuation.resume(with: result)
62
+ }
63
+
64
+ func resume(_ result: Result<T, Error>) {
65
+ let continuation: CheckedContinuation<T, Error>? = lock.withLock {
66
+ switch state {
67
+ case .waiting:
68
+ state = .early(result)
69
+ return nil
70
+ case let .started(continuation):
71
+ state = .done
72
+ return continuation
73
+ case .early, .done:
74
+ return nil
75
+ }
76
+ }
77
+ nonisolated(unsafe) let result = result
78
+ continuation?.resume(with: result)
79
+ }
80
+ }
@@ -3,7 +3,8 @@
3
3
  /// `invalidate()` starts a new generation. A caller whose computation finishes or fails after
4
4
  /// an invalidation computes again, so `value(_:)` never returns a stale result; `compute` must
5
5
  /// therefore read the state it depends on when it runs, not capture it. A failure is not kept:
6
- /// the next caller of the same generation tries again. A cancelled caller stops retrying.
6
+ /// the next caller of the same generation tries again. A cancelled caller leaves at once and stops
7
+ /// retrying; the computation runs on for the others.
7
8
  @MainActor
8
9
  final class GenerationMemo<Value: Sendable> {
9
10
  private var generation = 0
@@ -21,14 +22,20 @@ final class GenerationMemo<Value: Sendable> {
21
22
  if let current, current.generation == generation {
22
23
  task = current.task
23
24
  } else {
24
- task = Task { try await compute() }
25
+ task = Task {
26
+ do {
27
+ return try await compute()
28
+ } catch {
29
+ if self.current?.generation == generation { self.current = nil }
30
+ throw error
31
+ }
32
+ }
25
33
  current = (generation, task)
26
34
  }
27
35
  do {
28
- let value = try await task.value
36
+ let value = try await awaitCallback { deliver in Task { await deliver(task.result) } }
29
37
  if generation == self.generation { return value }
30
38
  } catch {
31
- if current?.task == task { current = nil }
32
39
  if generation == self.generation { throw error }
33
40
  }
34
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.76",
3
+ "version": "0.2.0-next.78",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",
@@ -1,63 +0,0 @@
1
- package com.audiobrowser.player
2
-
3
- import androidx.media3.common.Player as MediaPlayer
4
- import org.junit.Assert.assertEquals
5
- import org.junit.Test
6
-
7
- class SessionErrorMaskTest {
8
-
9
- @Test
10
- fun `idle with an error masks to ready while keep-alive is on`() {
11
- assertEquals(
12
- MediaPlayer.STATE_READY,
13
- SessionErrorMask.playbackState(MediaPlayer.STATE_IDLE, hasError = true, keepAlive = true),
14
- )
15
- assertEquals(
16
- false,
17
- SessionErrorMask.playWhenReady(
18
- raw = true,
19
- state = MediaPlayer.STATE_IDLE,
20
- hasError = true,
21
- keepAlive = true,
22
- ),
23
- )
24
- }
25
-
26
- @Test
27
- fun `no masking without keep-alive`() {
28
- assertEquals(
29
- MediaPlayer.STATE_IDLE,
30
- SessionErrorMask.playbackState(MediaPlayer.STATE_IDLE, hasError = true, keepAlive = false),
31
- )
32
- assertEquals(
33
- true,
34
- SessionErrorMask.playWhenReady(
35
- raw = true,
36
- state = MediaPlayer.STATE_IDLE,
37
- hasError = true,
38
- keepAlive = false,
39
- ),
40
- )
41
- }
42
-
43
- @Test
44
- fun `no masking without an error or outside idle`() {
45
- assertEquals(
46
- MediaPlayer.STATE_IDLE,
47
- SessionErrorMask.playbackState(MediaPlayer.STATE_IDLE, hasError = false, keepAlive = true),
48
- )
49
- assertEquals(
50
- MediaPlayer.STATE_BUFFERING,
51
- SessionErrorMask.playbackState(MediaPlayer.STATE_BUFFERING, hasError = true, keepAlive = true),
52
- )
53
- assertEquals(
54
- true,
55
- SessionErrorMask.playWhenReady(
56
- raw = true,
57
- state = MediaPlayer.STATE_READY,
58
- hasError = true,
59
- keepAlive = true,
60
- ),
61
- )
62
- }
63
- }
@@ -1,25 +0,0 @@
1
- import Foundation
2
-
3
- enum EndOfTrackOutcome: Equatable {
4
- case ended
5
- /// A stall reported as an end: recover rather than advance.
6
- case stalled
7
- }
8
-
9
- /// Judges an `AVPlayerItemDidPlayToEndTime`: iOS reports a dropped live connection, and
10
- /// sometimes a mid-stream buffer underrun, as a played-to-end, which would stop the station or
11
- /// skip the track. Free of AVFoundation so it tests off-device.
12
- struct EndOfTrackJudgement {
13
- let isLive: Bool
14
- let currentTime: Double
15
- let duration: Double
16
-
17
- var outcome: EndOfTrackOutcome {
18
- if isLive { return .stalled }
19
- // No known duration to check the position against: trust iOS.
20
- guard duration.isFinite, duration > 0 else { return .ended }
21
- // More than 5% short of the end is a stall, not a finish.
22
- if currentTime + duration * 0.05 < duration { return .stalled }
23
- return .ended
24
- }
25
- }
@@ -1,30 +0,0 @@
1
- import Testing
2
-
3
- @testable import AudioBrowserTestable
4
-
5
- @Suite("EndOfTrackJudgement")
6
- struct EndOfTrackJudgementTests {
7
- @Test func live_isAlwaysStalled() {
8
- #expect(EndOfTrackJudgement(isLive: true, currentTime: 0, duration: 0).outcome == .stalled)
9
- #expect(EndOfTrackJudgement(isLive: true, currentTime: 100, duration: 100).outcome == .stalled)
10
- #expect(EndOfTrackJudgement(isLive: true, currentTime: 5, duration: .nan).outcome == .stalled)
11
- }
12
-
13
- @Test func onDemand_atEnd_isEnded() {
14
- #expect(EndOfTrackJudgement(isLive: false, currentTime: 300, duration: 300).outcome == .ended)
15
- }
16
-
17
- // The position at a genuine end rarely lands exactly on the duration.
18
- @Test func onDemand_withinFivePercentOfEnd_isEnded() {
19
- #expect(EndOfTrackJudgement(isLive: false, currentTime: 291, duration: 300).outcome == .ended)
20
- }
21
-
22
- @Test func onDemand_wellShortOfEnd_isStalled() {
23
- #expect(EndOfTrackJudgement(isLive: false, currentTime: 120, duration: 300).outcome == .stalled)
24
- }
25
-
26
- @Test func onDemand_indefiniteDuration_isEnded() {
27
- #expect(EndOfTrackJudgement(isLive: false, currentTime: 0, duration: .nan).outcome == .ended)
28
- #expect(EndOfTrackJudgement(isLive: false, currentTime: 0, duration: 0).outcome == .ended)
29
- }
30
- }