react-native-audio-browser 0.2.0-next.75 → 0.2.0-next.77
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/android/src/main/java/com/audiobrowser/AudioBrowser.kt +20 -27
- package/android/src/main/java/com/audiobrowser/Service.kt +3 -0
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +16 -8
- package/android/src/main/java/com/audiobrowser/browser/TrackLoadHandler.kt +5 -3
- package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +10 -2
- package/android/src/main/java/com/audiobrowser/player/EnginePlayback.kt +9 -5
- package/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +71 -39
- package/android/src/main/java/com/audiobrowser/player/LiveStreamExtractor.kt +65 -0
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +12 -3
- package/android/src/main/java/com/audiobrowser/player/NowPlayingUpdater.kt +8 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +85 -37
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +38 -11
- package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +1 -1
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +21 -0
- package/android/src/main/java/com/audiobrowser/util/AwaitCancellable.kt +45 -0
- package/android/src/test/java/androidx/media3/session/SessionViews.kt +10 -0
- package/android/src/test/java/com/audiobrowser/GateDecisionTest.kt +128 -0
- package/android/src/test/java/com/audiobrowser/ResolveFormattedErrorTest.kt +93 -0
- package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +35 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerLayerResolutionTest.kt +75 -5
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerTest.kt +42 -0
- package/android/src/test/java/com/audiobrowser/browser/TrackLoadHandlerTest.kt +43 -0
- package/android/src/test/java/com/audiobrowser/http/RequestConfigBuilderTest.kt +62 -0
- package/android/src/test/java/com/audiobrowser/player/BrowseTilesTest.kt +31 -32
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +372 -0
- package/android/src/test/java/com/audiobrowser/player/NowPlayingFormatterTest.kt +75 -0
- package/android/src/test/java/com/audiobrowser/player/NowPlayingUpdaterTest.kt +25 -0
- package/android/src/test/java/com/audiobrowser/player/PlayFromSearchTest.kt +108 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerSetupEndedTest.kt +221 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerSetupTest.kt +17 -7
- package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +183 -7
- package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +153 -0
- package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +236 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewRulesTest.kt +62 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +188 -0
- package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +11 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +58 -0
- package/android/src/test/java/com/audiobrowser/util/AwaitCancellableTest.kt +136 -0
- package/android/src/test/java/com/audiobrowser/util/JsPromise.kt +44 -0
- package/ios/AVPlayerEngine.swift +5 -1
- package/ios/Browser/BrowserManager.swift +2 -2
- package/ios/Browser/PageResolver.swift +14 -19
- package/ios/Browser/RequestPipeline.swift +6 -4
- package/ios/CarPlay/CarPlayNowPlayingManager.swift +1 -1
- package/ios/CarPlay/CarPlaySession+PageLoader.swift +1 -0
- package/ios/CarPlay/CarPlaySession+TabBar.swift +2 -0
- package/ios/Extension/Promise+AwaitCallback.swift +33 -0
- package/ios/HybridAudioBrowser+Player.swift +1 -1
- package/ios/HybridAudioBrowser.swift +1 -1
- package/ios/Player/MediaLoader.swift +1 -1
- package/ios/Player/PlaybackFailure.swift +3 -3
- package/ios/Player/Player.swift +67 -49
- package/ios/Player/PlayerEngine.swift +2 -0
- package/ios/Player/RetryLoop.swift +72 -12
- package/ios/Tests/AwaitCallbackTests.swift +210 -0
- package/ios/Tests/GenerationMemoTests.swift +57 -0
- package/ios/Tests/PlaybackFailureTests.swift +7 -0
- package/ios/Tests/PlayerPolicyTests.swift +346 -6
- package/ios/Tests/PlayerTests.swift +79 -1
- package/ios/Tests/RetryLoopTests.swift +30 -5
- package/ios/Tests/Support/TestTypes.swift +15 -7
- package/ios/Tests/Support/TimeLimit.swift +43 -0
- package/ios/Util/AwaitCallback.swift +80 -0
- package/ios/Util/GenerationMemo.swift +11 -4
- package/package.json +2 -1
- package/android/src/test/java/com/audiobrowser/player/SessionErrorMaskTest.kt +0 -63
- package/ios/Player/EndOfTrackJudgement.swift +0 -25
- package/ios/Tests/EndOfTrackJudgementTests.swift +0 -30
|
@@ -40,6 +40,7 @@ import com.audiobrowser.util.RepeatModeFactory
|
|
|
40
40
|
import com.audiobrowser.util.SvgArtworkRenderer
|
|
41
41
|
import com.audiobrowser.util.TrackFactory
|
|
42
42
|
import com.audiobrowser.util.TrackFactory.setFavorite
|
|
43
|
+
import com.audiobrowser.util.awaitOrTimeout
|
|
43
44
|
import com.audiobrowser.util.isAutomotive
|
|
44
45
|
import com.margelo.nitro.audiobrowser.AppKilledPlaybackBehavior
|
|
45
46
|
import com.margelo.nitro.audiobrowser.FavoriteChangedEvent
|
|
@@ -68,6 +69,8 @@ import kotlinx.coroutines.CoroutineScope
|
|
|
68
69
|
import kotlinx.coroutines.Dispatchers
|
|
69
70
|
import kotlinx.coroutines.MainScope
|
|
70
71
|
import kotlinx.coroutines.TimeoutCancellationException
|
|
72
|
+
import kotlinx.coroutines.currentCoroutineContext
|
|
73
|
+
import kotlinx.coroutines.ensureActive
|
|
71
74
|
import kotlinx.coroutines.withContext
|
|
72
75
|
import kotlinx.coroutines.withTimeout
|
|
73
76
|
import timber.log.Timber
|
|
@@ -299,15 +302,27 @@ internal constructor(
|
|
|
299
302
|
var playWhenReady: Boolean
|
|
300
303
|
get() = sessionPlayer.playWhenReady
|
|
301
304
|
set(value) {
|
|
302
|
-
// ExoPlayer's setPlayWhenReady(true)
|
|
303
|
-
// its media-button path). Here in the setter so every writer
|
|
304
|
-
// togglePlayback(), JS.
|
|
305
|
+
// ExoPlayer's setPlayWhenReady(true) neither restarts an ended engine (media3 replays only on
|
|
306
|
+
// its media-button path) nor loads an idle one. Here in the setter so every writer plays
|
|
307
|
+
// alike: play(), togglePlayback(), JS, the session.
|
|
305
308
|
if (value && sessionPlayer.playbackState == MediaPlayer.STATE_ENDED) {
|
|
306
309
|
sessionPlayer.seekToDefaultPosition()
|
|
307
310
|
}
|
|
308
311
|
sessionPlayer.playWhenReady = value
|
|
312
|
+
// No-op unless the engine is idle, so this reconnects only after a stop, an error, a
|
|
313
|
+
// cancelled retry or a rebuild, and never re-buffers a healthy stream. Reconnecting is also
|
|
314
|
+
// how a live stream rejoins its live edge.
|
|
315
|
+
if (value && currentTrack != null) sessionPlayer.prepare()
|
|
309
316
|
}
|
|
310
317
|
|
|
318
|
+
/**
|
|
319
|
+
* The intent a setup carries, raised without loading: a setup repeats, as on a JS reload, and
|
|
320
|
+
* would otherwise start a stopped or failed queue. A play loads.
|
|
321
|
+
*/
|
|
322
|
+
internal fun applySetupPlayWhenReady(value: Boolean) {
|
|
323
|
+
sessionPlayer.playWhenReady = value
|
|
324
|
+
}
|
|
325
|
+
|
|
311
326
|
val duration: Long
|
|
312
327
|
get() = if (sessionPlayer.duration == C.TIME_UNSET) 0 else sessionPlayer.duration
|
|
313
328
|
|
|
@@ -403,26 +418,59 @@ internal constructor(
|
|
|
403
418
|
|
|
404
419
|
private var engineOptions: PlayerSetupOptions? = null
|
|
405
420
|
|
|
421
|
+
private var deferredSetup: PlayerSetupOptions? = null
|
|
422
|
+
|
|
406
423
|
/**
|
|
407
424
|
* Builds the ExoPlayer on the first call, and rebuilds it only when the engine options changed. A
|
|
408
|
-
* rebuild carries over the queue, the position and the play intent.
|
|
425
|
+
* rebuild carries over the queue, the position and the play intent. During a transient audio
|
|
426
|
+
* focus loss with the intent up, the rebuild waits until focus returns, the intent drops, or a
|
|
427
|
+
* play asks for focus again.
|
|
409
428
|
*/
|
|
410
429
|
fun setup(setupOptions: PlayerSetupOptions) {
|
|
411
430
|
nowPlaying.enabled = setupOptions.autoUpdateNowPlayingMetadata
|
|
412
431
|
// Unwrapped from the Nitro Func so the updater never touches bridge types.
|
|
413
432
|
nowPlaying.formatter =
|
|
414
|
-
setupOptions.nowPlayingMetadataFormatter?.let { f ->
|
|
433
|
+
setupOptions.nowPlayingMetadataFormatter?.let { f ->
|
|
434
|
+
{ params -> f.invoke(params).awaitOrTimeout() }
|
|
435
|
+
}
|
|
415
436
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
Timber.Forest.d("Setup options unchanged: keeping the player")
|
|
419
|
-
} else {
|
|
420
|
-
buildEngine(setupOptions)
|
|
421
|
-
engineOptions = newEngineOptions
|
|
422
|
-
}
|
|
437
|
+
deferredSetup = setupOptions.takeIf { it.engineOptions() != engineOptions }
|
|
438
|
+
rebuildIfDue()
|
|
423
439
|
if (remoteOutputs.apply(setupOptions.remoteOutputs, exoPlayer)) attachSessionPlayer()
|
|
424
440
|
}
|
|
425
441
|
|
|
442
|
+
/**
|
|
443
|
+
* Runs a [deferredSetup] rebuild unless the engine waits out a transient focus loss with the
|
|
444
|
+
* intent up: the new engine would ask for focus at once, taking it from the app that holds it, or
|
|
445
|
+
* lose the intent when refused, as during a call. Call when the engine's state, intent or
|
|
446
|
+
* suppression changes.
|
|
447
|
+
*/
|
|
448
|
+
internal fun rebuildIfDue() {
|
|
449
|
+
val options = deferredSetup ?: return
|
|
450
|
+
// A stopped engine keeps the suppression but holds no focus, and is carried unprepared.
|
|
451
|
+
if (
|
|
452
|
+
engineOptions != null &&
|
|
453
|
+
exoPlayer.playbackState != MediaPlayer.STATE_IDLE &&
|
|
454
|
+
exoPlayer.playWhenReady &&
|
|
455
|
+
exoPlayer.playbackSuppressionReason ==
|
|
456
|
+
MediaPlayer.PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS
|
|
457
|
+
) {
|
|
458
|
+
return
|
|
459
|
+
}
|
|
460
|
+
deferredSetup = null
|
|
461
|
+
buildEngine(options)
|
|
462
|
+
engineOptions = options.engineOptions()
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* [rebuildIfDue] once the current command returns: ExoPlayer reports an intent change from inside
|
|
467
|
+
* the command, and a rebuild there would land between its steps, as between stop()'s drop of the
|
|
468
|
+
* intent and its stop.
|
|
469
|
+
*/
|
|
470
|
+
internal fun rebuildIfDueLater() {
|
|
471
|
+
if (deferredSetup != null) mainHandler.post(::rebuildIfDue)
|
|
472
|
+
}
|
|
473
|
+
|
|
426
474
|
private fun buildEngine(setupOptions: PlayerSetupOptions) {
|
|
427
475
|
Timber.Forest.d("Setting up player with new options")
|
|
428
476
|
val isInitialSetup = engineOptions == null
|
|
@@ -476,12 +524,9 @@ internal constructor(
|
|
|
476
524
|
callbacks?.onPlaybackChanged(Playback(PlaybackState.NONE, null))
|
|
477
525
|
equalizer.initialize(exoPlayer.audioSessionId)
|
|
478
526
|
} else if (localEngine.isActive) {
|
|
479
|
-
// No listener heard the restore
|
|
480
|
-
// the receiver plays on unchanged.
|
|
481
|
-
setPlaybackState(
|
|
482
|
-
if (exoPlayer.playbackState == ExoPlayer.STATE_IDLE) PlaybackState.NONE
|
|
483
|
-
else PlaybackState.BUFFERING
|
|
484
|
-
)
|
|
527
|
+
// No listener heard the restore. Its prepare() leaves the engine buffering; an unprepared one
|
|
528
|
+
// keeps its state. While remote, the receiver plays on unchanged.
|
|
529
|
+
if (exoPlayer.playbackState != ExoPlayer.STATE_IDLE) setPlaybackState(PlaybackState.BUFFERING)
|
|
485
530
|
// The new engine starts unsuppressed: an unchanged state would not re-derive it.
|
|
486
531
|
refreshPlayingState()
|
|
487
532
|
}
|
|
@@ -499,7 +544,8 @@ internal constructor(
|
|
|
499
544
|
{ callbacks },
|
|
500
545
|
{ options },
|
|
501
546
|
keepSessionAliveOnError,
|
|
502
|
-
|
|
547
|
+
playerState = { playbackState },
|
|
548
|
+
playPlayer = ::play,
|
|
503
549
|
stopPlayer = ::stop,
|
|
504
550
|
)
|
|
505
551
|
forwardingPlayer.addListener(playerListener)
|
|
@@ -580,13 +626,18 @@ internal constructor(
|
|
|
580
626
|
if (removesCurrent && playWhenReady) prepare()
|
|
581
627
|
}
|
|
582
628
|
|
|
583
|
-
fun next() {
|
|
584
|
-
sessionPlayer.seekToNextMediaItem()
|
|
585
|
-
sessionPlayer.prepare()
|
|
586
|
-
}
|
|
629
|
+
fun next() = loadAfter { sessionPlayer.seekToNextMediaItem() }
|
|
587
630
|
|
|
588
|
-
fun previous() {
|
|
589
|
-
|
|
631
|
+
fun previous() = loadAfter { sessionPlayer.seekToPreviousMediaItem() }
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Runs [move] and prepares an idle engine, unless the queue ended and stays where it was: a
|
|
635
|
+
* rebuild carries an ended engine idle, and it stays ended as an ended engine does.
|
|
636
|
+
*/
|
|
637
|
+
private inline fun loadAfter(move: () -> Unit) {
|
|
638
|
+
val index = sessionPlayer.currentMediaItemIndex
|
|
639
|
+
move()
|
|
640
|
+
if (playbackState == PlaybackState.ENDED && sessionPlayer.currentMediaItemIndex == index) return
|
|
590
641
|
sessionPlayer.prepare()
|
|
591
642
|
}
|
|
592
643
|
|
|
@@ -847,13 +898,7 @@ internal constructor(
|
|
|
847
898
|
}
|
|
848
899
|
|
|
849
900
|
fun play() {
|
|
850
|
-
// Through the setter for its replay of an ended player.
|
|
851
901
|
playWhenReady = true
|
|
852
|
-
if (currentTrack != null) {
|
|
853
|
-
// No-op unless the player is idle, so this reconnects only after stop() or an error and never
|
|
854
|
-
// re-buffers a healthy stream. Reconnecting is also how a live stream rejoins its live edge.
|
|
855
|
-
sessionPlayer.prepare()
|
|
856
|
-
}
|
|
857
902
|
}
|
|
858
903
|
|
|
859
904
|
/**
|
|
@@ -901,15 +946,15 @@ internal constructor(
|
|
|
901
946
|
false
|
|
902
947
|
}
|
|
903
948
|
} catch (e: Exception) {
|
|
949
|
+
currentCoroutineContext().ensureActive()
|
|
904
950
|
Timber.e(e, "Error handling voice search: ${params}")
|
|
905
951
|
false
|
|
906
952
|
}
|
|
907
953
|
}
|
|
908
954
|
|
|
955
|
+
/** An ended queue stays ended, as on an ended engine. */
|
|
909
956
|
fun prepare() {
|
|
910
|
-
if (currentTrack != null)
|
|
911
|
-
sessionPlayer.prepare()
|
|
912
|
-
}
|
|
957
|
+
if (currentTrack != null && playbackState != PlaybackState.ENDED) sessionPlayer.prepare()
|
|
913
958
|
}
|
|
914
959
|
|
|
915
960
|
fun pause() {
|
|
@@ -928,10 +973,14 @@ internal constructor(
|
|
|
928
973
|
|
|
929
974
|
/** Keeps the queue; the state becomes STOPPED. */
|
|
930
975
|
fun stop() {
|
|
976
|
+
nowPlaying.clearTimedMetadata()
|
|
931
977
|
// State first, so the state machine's STOPPED guard swallows the PAUSED the drop below causes.
|
|
932
978
|
setPlaybackState(PlaybackState.STOPPED)
|
|
979
|
+
val ended = sessionPlayer.playbackState == MediaPlayer.STATE_ENDED
|
|
933
980
|
playWhenReady = false
|
|
934
981
|
sessionPlayer.stop()
|
|
982
|
+
// Idle at its end, the next prepare would end it again at once.
|
|
983
|
+
if (ended) sessionPlayer.seekToDefaultPosition()
|
|
935
984
|
}
|
|
936
985
|
|
|
937
986
|
fun clear() {
|
|
@@ -943,6 +992,7 @@ internal constructor(
|
|
|
943
992
|
fun destroy() {
|
|
944
993
|
if (destroyed) return
|
|
945
994
|
destroyed = true
|
|
995
|
+
deferredSetup = null
|
|
946
996
|
// stop() saves only if playing; before it, so its STOPPED doesn't defeat the ENDED guard.
|
|
947
997
|
playbackStateStore.stopPeriodicSave()
|
|
948
998
|
// An armed timer would fire pause() into the released player. Cleared before the release,
|
|
@@ -1053,9 +1103,7 @@ internal constructor(
|
|
|
1053
1103
|
playbackState = state
|
|
1054
1104
|
|
|
1055
1105
|
if (state == PlaybackState.PLAYING) hasPlayedCache = true
|
|
1056
|
-
|
|
1057
|
-
forwardingPlayer.onPausedChanged()
|
|
1058
|
-
}
|
|
1106
|
+
forwardingPlayer.onStateChanged(oldState)
|
|
1059
1107
|
if (state == PlaybackState.ERROR) resetForNewLoad()
|
|
1060
1108
|
|
|
1061
1109
|
// A natural end exhausts the play intent; held, it inverts the toggle, keeps audio focus and
|
|
@@ -29,7 +29,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
29
29
|
private val stuckRecoveryPolicy = StuckRecoveryPolicy()
|
|
30
30
|
|
|
31
31
|
// Main looper: Player.Listener callbacks arrive on the main thread.
|
|
32
|
-
private val
|
|
32
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
33
33
|
private val healthyPlaybackRunnable =
|
|
34
34
|
object : Runnable {
|
|
35
35
|
override fun run() {
|
|
@@ -39,12 +39,34 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
39
39
|
player.resetRetryTimer()
|
|
40
40
|
// Re-arm while playing: errors the buffer absorbs never flip isPlaying.
|
|
41
41
|
if (player.isPlaying) {
|
|
42
|
-
|
|
42
|
+
mainHandler.postDelayed(this, HEALTHY_PLAYBACK_MS)
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Set from an app-started track change until the playback thread has handled it. Until then every
|
|
49
|
+
* metadata delivery is the outgoing item's: its renderer posted them to the main looper before
|
|
50
|
+
* the change, and nothing removes them.
|
|
51
|
+
*/
|
|
52
|
+
private var metadataFence: Any? = null
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Its target runs on the playback thread right after the command that changed the item. A
|
|
56
|
+
* main-looper target goes to the back of that thread's queue, where a release would drop it.
|
|
57
|
+
*/
|
|
58
|
+
private fun fenceOutgoingMetadata() {
|
|
59
|
+
val fence = Any()
|
|
60
|
+
metadataFence = fence
|
|
61
|
+
player.exoPlayer
|
|
62
|
+
.createMessage { _, _ ->
|
|
63
|
+
mainHandler.post { if (metadataFence === fence) metadataFence = null }
|
|
64
|
+
}
|
|
65
|
+
.send()
|
|
66
|
+
}
|
|
67
|
+
|
|
47
68
|
override fun onMetadata(metadata: Metadata) {
|
|
69
|
+
if (metadataFence != null) return
|
|
48
70
|
val chapters = MetadataAdapter.extractChapters(metadata)
|
|
49
71
|
if (chapters.isNotEmpty()) {
|
|
50
72
|
player.callbacks?.onChapterMetadata(chapters)
|
|
@@ -81,15 +103,14 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
81
103
|
index = player.currentIndex?.toDouble(),
|
|
82
104
|
track = player.currentTrack,
|
|
83
105
|
)
|
|
106
|
+
val ended =
|
|
107
|
+
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_AUTO ||
|
|
108
|
+
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_REPEAT
|
|
84
109
|
player.nowPlaying.clearOverride()
|
|
110
|
+
if (!ended) fenceOutgoingMetadata()
|
|
85
111
|
player.callbacks?.onPlaybackActiveTrackChanged(event)
|
|
86
112
|
|
|
87
|
-
if (
|
|
88
|
-
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_AUTO ||
|
|
89
|
-
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_REPEAT
|
|
90
|
-
) {
|
|
91
|
-
player.sleepTimer.onTrackEnd()
|
|
92
|
-
}
|
|
113
|
+
if (ended) player.sleepTimer.onTrackEnd()
|
|
93
114
|
|
|
94
115
|
player.lastTrack = player.currentTrack
|
|
95
116
|
player.lastIndex = player.currentIndex
|
|
@@ -107,7 +128,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
107
128
|
// Emits on its own: the incoming track may hold LOADING/BUFFERING and emit no state change.
|
|
108
129
|
player.clearRetryingError()
|
|
109
130
|
|
|
110
|
-
|
|
131
|
+
mainHandler.removeCallbacks(healthyPlaybackRunnable)
|
|
111
132
|
stuckRecoveryPolicy.reset()
|
|
112
133
|
|
|
113
134
|
player.playbackStateStore.save()
|
|
@@ -115,9 +136,9 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
115
136
|
}
|
|
116
137
|
|
|
117
138
|
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
|
118
|
-
|
|
139
|
+
mainHandler.removeCallbacks(healthyPlaybackRunnable)
|
|
119
140
|
if (isPlaying) {
|
|
120
|
-
|
|
141
|
+
mainHandler.postDelayed(healthyPlaybackRunnable, HEALTHY_PLAYBACK_MS)
|
|
121
142
|
}
|
|
122
143
|
}
|
|
123
144
|
|
|
@@ -134,6 +155,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
134
155
|
// Deduped: the ENDED path in setPlaybackState may already have emitted it, before queue-ended.
|
|
135
156
|
player.emitPlayWhenReadyChanged(playWhenReady)
|
|
136
157
|
player.refreshPlayingState()
|
|
158
|
+
player.rebuildIfDueLater()
|
|
137
159
|
|
|
138
160
|
if (playWhenReady) {
|
|
139
161
|
player.playbackStateStore.startPeriodicSave()
|
|
@@ -144,6 +166,11 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
144
166
|
|
|
145
167
|
override fun onPlaybackSuppressionReasonChanged(playbackSuppressionReason: Int) {
|
|
146
168
|
player.refreshPlayingState()
|
|
169
|
+
player.rebuildIfDueLater()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
override fun onPlaybackStateChanged(playbackState: Int) {
|
|
173
|
+
player.rebuildIfDueLater()
|
|
147
174
|
}
|
|
148
175
|
|
|
149
176
|
override fun onEvents(media3Player: MediaPlayer, events: MediaPlayer.Events) {
|
|
@@ -132,7 +132,7 @@ class RetryLoadErrorHandlingPolicy(
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
Timber.d(
|
|
135
|
-
"Retrying after IO error (
|
|
135
|
+
"Retrying after IO error (load error $errorCount, delay ${delay}ms unless deferred, offline=$currentlyOffline): ${exception.message}"
|
|
136
136
|
)
|
|
137
137
|
|
|
138
138
|
onRetryPending?.invoke(exception)
|
|
@@ -4,6 +4,7 @@ import android.os.Looper
|
|
|
4
4
|
import androidx.media3.common.C
|
|
5
5
|
import androidx.media3.common.ForwardingSimpleBasePlayer
|
|
6
6
|
import androidx.media3.common.MediaItem
|
|
7
|
+
import androidx.media3.common.Metadata
|
|
7
8
|
import androidx.media3.common.PlaybackException
|
|
8
9
|
import androidx.media3.common.Player as MediaPlayer
|
|
9
10
|
import androidx.media3.common.SimpleBasePlayer
|
|
@@ -69,6 +70,9 @@ internal class RemoteSwitchingPlayer(
|
|
|
69
70
|
|
|
70
71
|
/** Whether the receiver reported the current item playing since the start timeout armed. */
|
|
71
72
|
var reportedPlaying = false
|
|
73
|
+
|
|
74
|
+
/** The receiver's last timed metadata for the current item; none until it holds that item. */
|
|
75
|
+
var timedMetadata = NO_TIMED_METADATA
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
|
|
@@ -133,6 +137,11 @@ internal class RemoteSwitchingPlayer(
|
|
|
133
137
|
|
|
134
138
|
private val receiverListener =
|
|
135
139
|
object : MediaPlayer.Listener {
|
|
140
|
+
override fun onMetadata(metadata: Metadata) {
|
|
141
|
+
val remote = remoteRoute ?: return
|
|
142
|
+
if (holdsCurrent(remote)) remote.timedMetadata = metadata
|
|
143
|
+
}
|
|
144
|
+
|
|
136
145
|
override fun onEvents(player: MediaPlayer, events: MediaPlayer.Events) {
|
|
137
146
|
val remote = remoteRoute ?: return
|
|
138
147
|
if (player !== remote.receiver) return
|
|
@@ -301,6 +310,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
301
310
|
|
|
302
311
|
private fun load(remote: Route.Remote, positionMs: Long) {
|
|
303
312
|
cancelLoad(remote)
|
|
313
|
+
remote.timedMetadata = NO_TIMED_METADATA
|
|
304
314
|
val item = queue.current
|
|
305
315
|
if (item == null) {
|
|
306
316
|
remote.load = Load.Idle(C.TIME_UNSET)
|
|
@@ -421,6 +431,14 @@ internal class RemoteSwitchingPlayer(
|
|
|
421
431
|
private fun receiverHolds(remote: Route.Remote, item: MediaItem) =
|
|
422
432
|
remote.receiver.currentMediaItem === item
|
|
423
433
|
|
|
434
|
+
/** Counts [Load.Sent] too: a receiver's onMetadata comes before onEvents moves it to Playing. */
|
|
435
|
+
private fun holdsCurrent(remote: Route.Remote): Boolean =
|
|
436
|
+
when (val load = remote.load) {
|
|
437
|
+
is Load.Sent -> receiverHolds(remote, load.item)
|
|
438
|
+
is Load.Playing -> receiverHolds(remote, load.item)
|
|
439
|
+
else -> false
|
|
440
|
+
}
|
|
441
|
+
|
|
424
442
|
override fun getState(): State {
|
|
425
443
|
val state = super.getState()
|
|
426
444
|
val remote = remoteRoute ?: return state
|
|
@@ -445,6 +463,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
445
463
|
.setVolume(if (maxVolume > 0) state.deviceVolume.toFloat() / maxVolume else 0f)
|
|
446
464
|
.setAvailableCommands(state.availableCommands.buildUpon().addAll(*QUEUE_COMMANDS).build())
|
|
447
465
|
.setPlayerError(null)
|
|
466
|
+
.setTimedMetadata(remote.timedMetadata)
|
|
448
467
|
.apply {
|
|
449
468
|
if (!holds) setPlayWhenReady(playIntent, PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST)
|
|
450
469
|
when (load) {
|
|
@@ -658,6 +677,8 @@ internal class RemoteSwitchingPlayer(
|
|
|
658
677
|
private fun done() = Futures.immediateVoidFuture()
|
|
659
678
|
|
|
660
679
|
private companion object {
|
|
680
|
+
val NO_TIMED_METADATA = Metadata(C.TIME_UNSET)
|
|
681
|
+
|
|
661
682
|
// Served from the queue this side keeps, whatever the receiver offers.
|
|
662
683
|
val QUEUE_COMMANDS =
|
|
663
684
|
intArrayOf(
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package com.audiobrowser.util
|
|
2
|
+
|
|
3
|
+
import com.margelo.nitro.core.Promise
|
|
4
|
+
import java.util.concurrent.TimeoutException
|
|
5
|
+
import kotlin.coroutines.resume
|
|
6
|
+
import kotlin.coroutines.resumeWithException
|
|
7
|
+
import kotlin.time.Duration.Companion.seconds
|
|
8
|
+
import kotlinx.coroutines.TimeoutCancellationException
|
|
9
|
+
import kotlinx.coroutines.currentCoroutineContext
|
|
10
|
+
import kotlinx.coroutines.ensureActive
|
|
11
|
+
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
12
|
+
import kotlinx.coroutines.withTimeout
|
|
13
|
+
|
|
14
|
+
/** How long a JS callback may take where its caller falls back on a failure. */
|
|
15
|
+
internal val FALLBACK_TIMEOUT = 10.seconds
|
|
16
|
+
|
|
17
|
+
/** Awaits this JS promise. Unlike Nitro's `await`, a cancelled caller resumes at once. */
|
|
18
|
+
internal suspend fun <T> Promise<T>.awaitCancellable(): T = settled()
|
|
19
|
+
|
|
20
|
+
/** [awaitCancellable] for a JS callback that returns a promise, behind Nitro's bridge promise. */
|
|
21
|
+
internal suspend fun <T> Promise<Promise<T>>.awaitNested(): T = settled().settled()
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* [awaitCancellable], failing with a [TimeoutException], which is not a cancellation, after
|
|
25
|
+
* [FALLBACK_TIMEOUT].
|
|
26
|
+
*/
|
|
27
|
+
internal suspend fun <T> Promise<T>.awaitOrTimeout(): T = withTimeoutAsFailure { settled() }
|
|
28
|
+
|
|
29
|
+
/** [awaitNested], failing as [awaitOrTimeout] does; the timeout covers both promises. */
|
|
30
|
+
internal suspend fun <T> Promise<Promise<T>>.awaitNestedOrTimeout(): T = withTimeoutAsFailure {
|
|
31
|
+
settled().settled()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private suspend fun <T> withTimeoutAsFailure(block: suspend () -> T): T =
|
|
35
|
+
try {
|
|
36
|
+
withTimeout(FALLBACK_TIMEOUT) { block() }
|
|
37
|
+
} catch (e: TimeoutCancellationException) {
|
|
38
|
+
currentCoroutineContext().ensureActive()
|
|
39
|
+
throw TimeoutException("JS callback outran $FALLBACK_TIMEOUT")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private suspend fun <T> Promise<T>.settled(): T = suspendCancellableCoroutine { c ->
|
|
43
|
+
then { c.resume(it) }
|
|
44
|
+
catch { c.resumeWithException(it) }
|
|
45
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
package androidx.media3.session
|
|
2
|
+
|
|
3
|
+
// Read in place: Robolectric can't connect a controller to the session.
|
|
4
|
+
|
|
5
|
+
/** The state legacy controllers (Android Auto, Bluetooth, the system UI) were last told. */
|
|
6
|
+
internal fun MediaSession.legacyState() =
|
|
7
|
+
impl.mediaSessionLegacyStub.sessionCompat.controller.playbackState!!.state
|
|
8
|
+
|
|
9
|
+
/** The current title media3 controllers were last told. */
|
|
10
|
+
internal fun MediaSession.controllerTitle() = impl.playerInfo.mediaMetadata.title?.toString()
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.util.FALLBACK_TIMEOUT
|
|
4
|
+
import com.audiobrowser.util.JsPromise
|
|
5
|
+
import com.margelo.nitro.audiobrowser.Gate
|
|
6
|
+
import com.margelo.nitro.audiobrowser.GateDecision
|
|
7
|
+
import com.margelo.nitro.audiobrowser.GateReason
|
|
8
|
+
import com.margelo.nitro.audiobrowser.NativeGateRequest
|
|
9
|
+
import com.margelo.nitro.core.Promise
|
|
10
|
+
import kotlin.time.Duration.Companion.seconds
|
|
11
|
+
import kotlinx.coroutines.CancellationException
|
|
12
|
+
import kotlinx.coroutines.Job
|
|
13
|
+
import kotlinx.coroutines.async
|
|
14
|
+
import kotlinx.coroutines.launch
|
|
15
|
+
import kotlinx.coroutines.test.advanceTimeBy
|
|
16
|
+
import kotlinx.coroutines.test.runCurrent
|
|
17
|
+
import kotlinx.coroutines.test.runTest
|
|
18
|
+
import org.junit.Assert.assertFalse
|
|
19
|
+
import org.junit.Assert.assertNull
|
|
20
|
+
import org.junit.Assert.assertTrue
|
|
21
|
+
import org.junit.Test
|
|
22
|
+
import org.mockito.Mockito.CALLS_REAL_METHODS
|
|
23
|
+
import org.mockito.Mockito.mock
|
|
24
|
+
|
|
25
|
+
class GateDecisionTest {
|
|
26
|
+
|
|
27
|
+
// AudioBrowser's constructor needs JNI, so the browser is a mock that runs its real methods,
|
|
28
|
+
// with an active gate written to the field setGate writes.
|
|
29
|
+
private fun gatedBrowser(
|
|
30
|
+
resolveGate: (NativeGateRequest) -> Promise<Promise<GateDecision>>
|
|
31
|
+
): AudioBrowser {
|
|
32
|
+
val browser = mock(AudioBrowser::class.java, CALLS_REAL_METHODS)
|
|
33
|
+
val gateState =
|
|
34
|
+
Class.forName("com.audiobrowser.AudioBrowser\$GateState")
|
|
35
|
+
.getDeclaredConstructor(Boolean::class.java, Gate::class.java, Boolean::class.java)
|
|
36
|
+
.apply { isAccessible = true }
|
|
37
|
+
.newInstance(true, Gate("Gated", null), true)
|
|
38
|
+
AudioBrowser::class
|
|
39
|
+
.java
|
|
40
|
+
.getDeclaredField("gateState")
|
|
41
|
+
.apply { isAccessible = true }
|
|
42
|
+
.set(browser, gateState)
|
|
43
|
+
browser.resolveGate = resolveGate
|
|
44
|
+
return browser
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private val request = NativeGateRequest(GateReason.SEARCH, null, null)
|
|
48
|
+
|
|
49
|
+
@Test
|
|
50
|
+
fun `a decision whose caller is cancelled while it waits returns nothing`() = runTest {
|
|
51
|
+
var outcome: AudioBrowser.GateOutcome? = null
|
|
52
|
+
lateinit var caller: Job
|
|
53
|
+
val browser = gatedBrowser {
|
|
54
|
+
caller.cancel()
|
|
55
|
+
// Cancelled while the resolver runs, which then fails.
|
|
56
|
+
throw RuntimeException("Rejected")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
caller = launch { outcome = browser.gateDecision(request) }
|
|
60
|
+
caller.join()
|
|
61
|
+
|
|
62
|
+
assertNull(outcome)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Test
|
|
66
|
+
fun `a resolver's own cancellation fails closed while its caller is active`() = runTest {
|
|
67
|
+
val browser = gatedBrowser { throw CancellationException("Timed out") }
|
|
68
|
+
|
|
69
|
+
assertTrue(browser.gateDecision(request).gated)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A gated browser whose resolver's JS promise the test settles. */
|
|
73
|
+
private fun pendingResolver(): Pair<AudioBrowser, JsPromise<GateDecision>> {
|
|
74
|
+
val decision = JsPromise<GateDecision>()
|
|
75
|
+
val bridge = JsPromise<Promise<GateDecision>>().apply { resolve(decision.promise) }
|
|
76
|
+
return gatedBrowser { bridge.promise } to decision
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Test
|
|
80
|
+
fun `a resolver answering inside the timeout decides`() =
|
|
81
|
+
runTest(timeout = 5.seconds) {
|
|
82
|
+
val (browser, decision) = pendingResolver()
|
|
83
|
+
val outcome = backgroundScope.async { browser.gateDecision(request) }
|
|
84
|
+
runCurrent()
|
|
85
|
+
|
|
86
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
87
|
+
decision.resolve(GateDecision(false, null))
|
|
88
|
+
runCurrent()
|
|
89
|
+
|
|
90
|
+
assertTrue(outcome.isCompleted)
|
|
91
|
+
assertFalse(outcome.await().gated)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Test
|
|
95
|
+
fun `a resolver that never answers fails closed at the timeout`() =
|
|
96
|
+
runTest(timeout = 5.seconds) {
|
|
97
|
+
val (browser, _) = pendingResolver()
|
|
98
|
+
val outcome = backgroundScope.async { browser.gateDecision(request) }
|
|
99
|
+
|
|
100
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
101
|
+
runCurrent()
|
|
102
|
+
assertFalse(outcome.isCompleted)
|
|
103
|
+
advanceTimeBy(2.seconds)
|
|
104
|
+
runCurrent()
|
|
105
|
+
|
|
106
|
+
assertTrue(outcome.isCompleted)
|
|
107
|
+
assertTrue(outcome.await().gated)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Test
|
|
111
|
+
fun `a caller cancelled while the resolver is silent stops waiting`() =
|
|
112
|
+
runTest(timeout = 5.seconds) {
|
|
113
|
+
val (browser, _) = pendingResolver()
|
|
114
|
+
var returned = false
|
|
115
|
+
val outcome =
|
|
116
|
+
backgroundScope.launch {
|
|
117
|
+
browser.gateDecision(request)
|
|
118
|
+
returned = true
|
|
119
|
+
}
|
|
120
|
+
runCurrent()
|
|
121
|
+
|
|
122
|
+
outcome.cancel()
|
|
123
|
+
runCurrent()
|
|
124
|
+
|
|
125
|
+
assertTrue(outcome.isCompleted)
|
|
126
|
+
assertFalse(returned)
|
|
127
|
+
}
|
|
128
|
+
}
|