react-native-audio-browser 0.2.0-next.76 → 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 +1 -1
- 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 +5 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +78 -36
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +6 -0
- 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/test/java/androidx/media3/session/SessionViews.kt +10 -0
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +372 -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 +48 -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/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 +1 -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
|
@@ -1159,7 +1159,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
1159
1159
|
pendingRepeatMode = null
|
|
1160
1160
|
}
|
|
1161
1161
|
pendingPlayWhenReady?.let {
|
|
1162
|
-
player.
|
|
1162
|
+
player.applySetupPlayWhenReady(it)
|
|
1163
1163
|
pendingPlayWhenReady = null
|
|
1164
1164
|
}
|
|
1165
1165
|
}
|
|
@@ -15,8 +15,8 @@ internal data class EnginePlayback(
|
|
|
15
15
|
val mediaItems: List<MediaItem>,
|
|
16
16
|
val index: Int,
|
|
17
17
|
/**
|
|
18
|
-
* [C.TIME_UNSET] for a live item, so it resumes at the live edge,
|
|
19
|
-
* live item reports before it has loaded.
|
|
18
|
+
* [C.TIME_UNSET] for a live item, so it resumes at the live edge, for 0, which is also what a
|
|
19
|
+
* live item reports before it has loaded, and after the end, so a play restarts the track.
|
|
20
20
|
*/
|
|
21
21
|
val positionMs: Long,
|
|
22
22
|
val prepared: Boolean,
|
|
@@ -48,9 +48,13 @@ internal data class EnginePlayback(
|
|
|
48
48
|
mediaItems = (0 until exoPlayer.mediaItemCount).map { exoPlayer.getMediaItemAt(it) },
|
|
49
49
|
index = exoPlayer.currentMediaItemIndex,
|
|
50
50
|
positionMs =
|
|
51
|
-
exoPlayer.currentPosition.takeUnless {
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
exoPlayer.currentPosition.takeUnless {
|
|
52
|
+
exoPlayer.isCurrentMediaItemLive ||
|
|
53
|
+
it == 0L ||
|
|
54
|
+
exoPlayer.playbackState == MediaPlayer.STATE_ENDED
|
|
55
|
+
} ?: C.TIME_UNSET,
|
|
56
|
+
prepared =
|
|
57
|
+
exoPlayer.playbackState !in setOf(MediaPlayer.STATE_IDLE, MediaPlayer.STATE_ENDED),
|
|
54
58
|
playWhenReady = exoPlayer.playWhenReady,
|
|
55
59
|
repeatMode = exoPlayer.repeatMode,
|
|
56
60
|
shuffleModeEnabled = exoPlayer.shuffleModeEnabled,
|
|
@@ -1,40 +1,61 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
3
|
import androidx.media3.common.ForwardingPlayer
|
|
4
|
+
import androidx.media3.common.PlaybackException
|
|
4
5
|
import androidx.media3.common.Player as MediaPlayer
|
|
5
6
|
import com.audiobrowser.Callbacks
|
|
6
7
|
import com.audiobrowser.model.PlayerUpdateOptions
|
|
8
|
+
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
7
9
|
import com.margelo.nitro.audiobrowser.RemoteJumpBackwardEvent
|
|
8
10
|
import com.margelo.nitro.audiobrowser.RemoteJumpForwardEvent
|
|
9
11
|
import com.margelo.nitro.audiobrowser.RemoteSeekEvent
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* What the platform session reads of an idle engine. Android Auto takes STATE_IDLE for "nothing
|
|
15
|
+
* playing": it closes the now-playing screen and its next/previous buttons with it. So an ended
|
|
16
|
+
* queue reads ended, as on an ended engine (a rebuild carries an ended engine idle), and with
|
|
17
|
+
* `keepAlive` a terminal error (the engine's, or ERROR once a rebuild dropped it) and a pause that
|
|
18
|
+
* cancelled a retry (PAUSED) read as paused and ready.
|
|
16
19
|
*/
|
|
17
|
-
internal object
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
internal object SessionView {
|
|
21
|
+
private val PARKED_STATES = setOf(PlaybackState.PAUSED, PlaybackState.ERROR)
|
|
22
|
+
private val SETTLED_STATES = setOf(PlaybackState.STOPPED, PlaybackState.NONE)
|
|
23
|
+
|
|
24
|
+
fun playbackState(
|
|
25
|
+
engineState: Int,
|
|
26
|
+
state: PlaybackState,
|
|
27
|
+
engineFailed: Boolean,
|
|
28
|
+
keepAlive: Boolean,
|
|
29
|
+
): Int =
|
|
30
|
+
when {
|
|
31
|
+
engineState != MediaPlayer.STATE_IDLE -> engineState
|
|
32
|
+
state == PlaybackState.ENDED -> MediaPlayer.STATE_ENDED
|
|
33
|
+
!keepAlive -> engineState
|
|
34
|
+
state in PARKED_STATES -> MediaPlayer.STATE_READY
|
|
35
|
+
// The engine keeps its error through a stop or an emptied queue, until its next prepare. It
|
|
36
|
+
// counts until the player's state reaches ERROR, which follows the engine's IDLE event.
|
|
37
|
+
engineFailed && state !in SETTLED_STATES -> MediaPlayer.STATE_READY
|
|
38
|
+
else -> engineState
|
|
39
|
+
}
|
|
20
40
|
|
|
21
|
-
|
|
22
|
-
|
|
41
|
+
/** An idle engine read as anything else isn't loading, so it can't be about to play. */
|
|
42
|
+
fun playWhenReady(raw: Boolean, engineState: Int, view: Int): Boolean =
|
|
43
|
+
raw && (engineState != MediaPlayer.STATE_IDLE || view == MediaPlayer.STATE_IDLE)
|
|
23
44
|
}
|
|
24
45
|
|
|
25
46
|
/**
|
|
26
47
|
* The player the MediaSession sees. Offers each transport command from an external surface (lock
|
|
27
48
|
* screen, notification, Bluetooth, Android Auto) to the app's `handleRemote*` callback first: true
|
|
28
|
-
* consumes it, anything else runs the default.
|
|
29
|
-
*
|
|
30
|
-
* `playbackState`.
|
|
49
|
+
* consumes it, anything else runs the default. Shows the session the [SessionView] of an idle
|
|
50
|
+
* engine; the error still reaches JS through `onPlaybackError` and `playbackState`.
|
|
31
51
|
*/
|
|
32
52
|
internal class InterceptingPlayer(
|
|
33
53
|
player: MediaPlayer,
|
|
34
54
|
private val callbacksProvider: () -> Callbacks?,
|
|
35
55
|
private val optionsProvider: () -> PlayerUpdateOptions,
|
|
36
56
|
private val keepSessionAliveOnError: Boolean,
|
|
37
|
-
private val
|
|
57
|
+
private val playerState: () -> PlaybackState,
|
|
58
|
+
private val playPlayer: () -> Unit,
|
|
38
59
|
private val stopPlayer: () -> Unit,
|
|
39
60
|
) : ForwardingPlayer(player) {
|
|
40
61
|
|
|
@@ -50,40 +71,50 @@ internal class InterceptingPlayer(
|
|
|
50
71
|
super.removeListener(listener)
|
|
51
72
|
}
|
|
52
73
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Call when the player's state changes from [old]: on an idle engine the view can move with no
|
|
76
|
+
* engine event.
|
|
77
|
+
*/
|
|
78
|
+
fun onStateChanged(old: PlaybackState) {
|
|
79
|
+
val view = playbackState
|
|
80
|
+
val was =
|
|
81
|
+
SessionView.playbackState(
|
|
82
|
+
super.getPlaybackState(),
|
|
83
|
+
old,
|
|
84
|
+
engineFailed,
|
|
85
|
+
keepSessionAliveOnError,
|
|
86
|
+
)
|
|
87
|
+
if (view != was) listeners.toList().forEach { it.onPlaybackStateChanged(view) }
|
|
58
88
|
}
|
|
59
89
|
|
|
60
|
-
private val parked: Boolean
|
|
61
|
-
get() = super.getPlayerError() != null || isPaused()
|
|
62
|
-
|
|
63
90
|
// Media3 maps a player error to the legacy STATE_ERROR, which Android Auto and the notification
|
|
64
91
|
// show as a disruptive error. Hidden whatever keepSessionAliveOnError says.
|
|
65
|
-
override fun getPlayerError():
|
|
92
|
+
override fun getPlayerError(): PlaybackException? = null
|
|
93
|
+
|
|
94
|
+
private val engineFailed: Boolean
|
|
95
|
+
get() = super.getPlayerError() != null
|
|
66
96
|
|
|
67
|
-
// The masks read super's error: this player's getPlayerError() is always null.
|
|
68
97
|
override fun getPlaybackState(): Int =
|
|
69
|
-
|
|
98
|
+
SessionView.playbackState(
|
|
70
99
|
super.getPlaybackState(),
|
|
71
|
-
|
|
72
|
-
|
|
100
|
+
playerState(),
|
|
101
|
+
engineFailed,
|
|
102
|
+
keepSessionAliveOnError,
|
|
73
103
|
)
|
|
74
104
|
|
|
75
105
|
override fun getPlayWhenReady(): Boolean =
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
106
|
+
SessionView.playWhenReady(super.getPlayWhenReady(), super.getPlaybackState(), playbackState)
|
|
107
|
+
|
|
108
|
+
// After a terminal error, a cancelled retry or a rebuild in ENDED the engine is idle, and the
|
|
109
|
+
// default skips only move the index. An ended queue that stayed put stays ended, as on an ended
|
|
110
|
+
// engine.
|
|
111
|
+
private fun loadIdleEngine(fromIndex: Int) {
|
|
112
|
+
if (super.getPlaybackState() != MediaPlayer.STATE_IDLE) return
|
|
113
|
+
val view = playbackState
|
|
114
|
+
if (
|
|
115
|
+
view == MediaPlayer.STATE_READY ||
|
|
116
|
+
view == MediaPlayer.STATE_ENDED && currentMediaItemIndex != fromIndex
|
|
117
|
+
) {
|
|
87
118
|
prepare()
|
|
88
119
|
}
|
|
89
120
|
}
|
|
@@ -94,11 +125,12 @@ internal class InterceptingPlayer(
|
|
|
94
125
|
fallback: () -> Unit,
|
|
95
126
|
) {
|
|
96
127
|
if (callbacksProvider()?.let(handler) == true) return
|
|
128
|
+
val index = currentMediaItemIndex
|
|
97
129
|
fallback()
|
|
98
|
-
if (recover)
|
|
130
|
+
if (recover) loadIdleEngine(index)
|
|
99
131
|
}
|
|
100
132
|
|
|
101
|
-
override fun play() = intercept({ it.handleRemotePlay() }
|
|
133
|
+
override fun play() = intercept({ it.handleRemotePlay() }) { playPlayer() }
|
|
102
134
|
|
|
103
135
|
override fun pause() = intercept({ it.handleRemotePause() }) { super.pause() }
|
|
104
136
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.net.Uri
|
|
4
|
+
import androidx.media3.common.C
|
|
5
|
+
import androidx.media3.common.PlaybackException
|
|
6
|
+
import androidx.media3.datasource.DataSourceException
|
|
7
|
+
import androidx.media3.extractor.Extractor
|
|
8
|
+
import androidx.media3.extractor.ExtractorInput
|
|
9
|
+
import androidx.media3.extractor.ExtractorOutput
|
|
10
|
+
import androidx.media3.extractor.ExtractorsFactory
|
|
11
|
+
import androidx.media3.extractor.ForwardingExtractor
|
|
12
|
+
import androidx.media3.extractor.ForwardingExtractorOutput
|
|
13
|
+
import androidx.media3.extractor.ForwardingExtractorsFactory
|
|
14
|
+
import androidx.media3.extractor.PositionHolder
|
|
15
|
+
import androidx.media3.extractor.SeekMap
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Extracts a live track's progressive stream, which never ends: once its format is recognised, an
|
|
19
|
+
* end of a stream with neither a length nor a duration (media3's own test for live) is a dropped
|
|
20
|
+
* connection, thrown for the load error policy to reconnect. An end during the sniff stays the
|
|
21
|
+
* unrecognised format error, and a stream with a length or a declared end ends there.
|
|
22
|
+
*/
|
|
23
|
+
class LiveStreamExtractor(extractor: Extractor) : ForwardingExtractor(extractor) {
|
|
24
|
+
@Volatile private var durationKnown = false
|
|
25
|
+
|
|
26
|
+
override fun init(output: ExtractorOutput) {
|
|
27
|
+
super.init(
|
|
28
|
+
object : ForwardingExtractorOutput(output) {
|
|
29
|
+
override fun seekMap(seekMap: SeekMap) {
|
|
30
|
+
durationKnown = seekMap.durationUs != C.TIME_UNSET
|
|
31
|
+
super.seekMap(seekMap)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override fun read(input: ExtractorInput, seekPosition: PositionHolder): Int {
|
|
38
|
+
val result = super.read(input, seekPosition)
|
|
39
|
+
if (
|
|
40
|
+
result == Extractor.RESULT_END_OF_INPUT &&
|
|
41
|
+
input.length == C.LENGTH_UNSET.toLong() &&
|
|
42
|
+
!durationKnown
|
|
43
|
+
) {
|
|
44
|
+
throw DataSourceException(
|
|
45
|
+
"The server ended a live stream",
|
|
46
|
+
null,
|
|
47
|
+
PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED,
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
return result
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Wraps every extractor [factory] makes. */
|
|
54
|
+
class Factory(factory: ExtractorsFactory) : ForwardingExtractorsFactory(factory) {
|
|
55
|
+
override fun createExtractors(): Array<Extractor> = wrap(super.createExtractors())
|
|
56
|
+
|
|
57
|
+
override fun createExtractors(
|
|
58
|
+
uri: Uri,
|
|
59
|
+
responseHeaders: Map<String, List<String>>,
|
|
60
|
+
): Array<Extractor> = wrap(super.createExtractors(uri, responseHeaders))
|
|
61
|
+
|
|
62
|
+
private fun wrap(extractors: Array<Extractor>): Array<Extractor> =
|
|
63
|
+
Array(extractors.size) { LiveStreamExtractor(extractors[it]) }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -13,6 +13,7 @@ import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
|
|
|
13
13
|
import androidx.media3.exoplayer.source.MediaSource
|
|
14
14
|
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
|
|
15
15
|
import androidx.media3.extractor.DefaultExtractorsFactory
|
|
16
|
+
import androidx.media3.extractor.ExtractorsFactory
|
|
16
17
|
import com.audiobrowser.model.RetryPolicy
|
|
17
18
|
import com.audiobrowser.util.TrackFactory
|
|
18
19
|
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
@@ -75,11 +76,15 @@ class MediaFactory(
|
|
|
75
76
|
)
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
private
|
|
79
|
-
DefaultMediaSourceFactory(context,
|
|
79
|
+
private fun mediaSourceFactory(extractors: ExtractorsFactory) =
|
|
80
|
+
DefaultMediaSourceFactory(context, extractors).apply {
|
|
80
81
|
customRetryPolicy?.let { setLoadErrorHandlingPolicy(it) }
|
|
81
82
|
}
|
|
82
83
|
|
|
84
|
+
private val mediaFactory = mediaSourceFactory(extractorsFactory)
|
|
85
|
+
|
|
86
|
+
private val liveMediaFactory = mediaSourceFactory(LiveStreamExtractor.Factory(extractorsFactory))
|
|
87
|
+
|
|
83
88
|
/** Restarts the retry policy's time budgets. */
|
|
84
89
|
fun resetRetryTimer() {
|
|
85
90
|
customRetryPolicy?.reset()
|
|
@@ -88,12 +93,14 @@ class MediaFactory(
|
|
|
88
93
|
override fun setDrmSessionManagerProvider(
|
|
89
94
|
drmSessionManagerProvider: DrmSessionManagerProvider
|
|
90
95
|
): MediaSource.Factory {
|
|
96
|
+
liveMediaFactory.setDrmSessionManagerProvider(drmSessionManagerProvider)
|
|
91
97
|
return mediaFactory.setDrmSessionManagerProvider(drmSessionManagerProvider)
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
override fun setLoadErrorHandlingPolicy(
|
|
95
101
|
loadErrorHandlingPolicy: LoadErrorHandlingPolicy
|
|
96
102
|
): MediaSource.Factory {
|
|
103
|
+
liveMediaFactory.setLoadErrorHandlingPolicy(loadErrorHandlingPolicy)
|
|
97
104
|
return mediaFactory.setLoadErrorHandlingPolicy(loadErrorHandlingPolicy)
|
|
98
105
|
}
|
|
99
106
|
|
|
@@ -132,6 +139,8 @@ class MediaFactory(
|
|
|
132
139
|
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
|
|
133
140
|
} ?: transformingFactory
|
|
134
141
|
|
|
135
|
-
return
|
|
142
|
+
return (if (track.live == true) liveMediaFactory else mediaFactory)
|
|
143
|
+
.setDataSourceFactory(dataSourceFactory)
|
|
144
|
+
.createMediaSource(mediaItem)
|
|
136
145
|
}
|
|
137
146
|
}
|
|
@@ -190,6 +190,11 @@ class NowPlayingUpdater(private val surface: NowPlayingSurface, private val scop
|
|
|
190
190
|
forgetPublished()
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
+
/** Call when the stream stops: its title no longer applies. The caller renders. */
|
|
194
|
+
fun clearTimedMetadata() {
|
|
195
|
+
timedMetadata = null
|
|
196
|
+
}
|
|
197
|
+
|
|
193
198
|
/** Records live (ICY/ID3) metadata; re-renders only when a formatter would use it. */
|
|
194
199
|
fun onTimedMetadataReceived(timed: TimedMetadata) {
|
|
195
200
|
timedMetadata = timed
|
|
@@ -302,15 +302,27 @@ internal constructor(
|
|
|
302
302
|
var playWhenReady: Boolean
|
|
303
303
|
get() = sessionPlayer.playWhenReady
|
|
304
304
|
set(value) {
|
|
305
|
-
// ExoPlayer's setPlayWhenReady(true)
|
|
306
|
-
// its media-button path). Here in the setter so every writer
|
|
307
|
-
// 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.
|
|
308
308
|
if (value && sessionPlayer.playbackState == MediaPlayer.STATE_ENDED) {
|
|
309
309
|
sessionPlayer.seekToDefaultPosition()
|
|
310
310
|
}
|
|
311
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()
|
|
312
316
|
}
|
|
313
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
|
+
|
|
314
326
|
val duration: Long
|
|
315
327
|
get() = if (sessionPlayer.duration == C.TIME_UNSET) 0 else sessionPlayer.duration
|
|
316
328
|
|
|
@@ -406,9 +418,13 @@ internal constructor(
|
|
|
406
418
|
|
|
407
419
|
private var engineOptions: PlayerSetupOptions? = null
|
|
408
420
|
|
|
421
|
+
private var deferredSetup: PlayerSetupOptions? = null
|
|
422
|
+
|
|
409
423
|
/**
|
|
410
424
|
* Builds the ExoPlayer on the first call, and rebuilds it only when the engine options changed. A
|
|
411
|
-
* 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.
|
|
412
428
|
*/
|
|
413
429
|
fun setup(setupOptions: PlayerSetupOptions) {
|
|
414
430
|
nowPlaying.enabled = setupOptions.autoUpdateNowPlayingMetadata
|
|
@@ -418,16 +434,43 @@ internal constructor(
|
|
|
418
434
|
{ params -> f.invoke(params).awaitOrTimeout() }
|
|
419
435
|
}
|
|
420
436
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
Timber.Forest.d("Setup options unchanged: keeping the player")
|
|
424
|
-
} else {
|
|
425
|
-
buildEngine(setupOptions)
|
|
426
|
-
engineOptions = newEngineOptions
|
|
427
|
-
}
|
|
437
|
+
deferredSetup = setupOptions.takeIf { it.engineOptions() != engineOptions }
|
|
438
|
+
rebuildIfDue()
|
|
428
439
|
if (remoteOutputs.apply(setupOptions.remoteOutputs, exoPlayer)) attachSessionPlayer()
|
|
429
440
|
}
|
|
430
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
|
+
|
|
431
474
|
private fun buildEngine(setupOptions: PlayerSetupOptions) {
|
|
432
475
|
Timber.Forest.d("Setting up player with new options")
|
|
433
476
|
val isInitialSetup = engineOptions == null
|
|
@@ -481,12 +524,9 @@ internal constructor(
|
|
|
481
524
|
callbacks?.onPlaybackChanged(Playback(PlaybackState.NONE, null))
|
|
482
525
|
equalizer.initialize(exoPlayer.audioSessionId)
|
|
483
526
|
} else if (localEngine.isActive) {
|
|
484
|
-
// No listener heard the restore
|
|
485
|
-
// the receiver plays on unchanged.
|
|
486
|
-
setPlaybackState(
|
|
487
|
-
if (exoPlayer.playbackState == ExoPlayer.STATE_IDLE) PlaybackState.NONE
|
|
488
|
-
else PlaybackState.BUFFERING
|
|
489
|
-
)
|
|
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)
|
|
490
530
|
// The new engine starts unsuppressed: an unchanged state would not re-derive it.
|
|
491
531
|
refreshPlayingState()
|
|
492
532
|
}
|
|
@@ -504,7 +544,8 @@ internal constructor(
|
|
|
504
544
|
{ callbacks },
|
|
505
545
|
{ options },
|
|
506
546
|
keepSessionAliveOnError,
|
|
507
|
-
|
|
547
|
+
playerState = { playbackState },
|
|
548
|
+
playPlayer = ::play,
|
|
508
549
|
stopPlayer = ::stop,
|
|
509
550
|
)
|
|
510
551
|
forwardingPlayer.addListener(playerListener)
|
|
@@ -585,13 +626,18 @@ internal constructor(
|
|
|
585
626
|
if (removesCurrent && playWhenReady) prepare()
|
|
586
627
|
}
|
|
587
628
|
|
|
588
|
-
fun next() {
|
|
589
|
-
sessionPlayer.seekToNextMediaItem()
|
|
590
|
-
sessionPlayer.prepare()
|
|
591
|
-
}
|
|
629
|
+
fun next() = loadAfter { sessionPlayer.seekToNextMediaItem() }
|
|
592
630
|
|
|
593
|
-
fun previous() {
|
|
594
|
-
|
|
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
|
|
595
641
|
sessionPlayer.prepare()
|
|
596
642
|
}
|
|
597
643
|
|
|
@@ -852,13 +898,7 @@ internal constructor(
|
|
|
852
898
|
}
|
|
853
899
|
|
|
854
900
|
fun play() {
|
|
855
|
-
// Through the setter for its replay of an ended player.
|
|
856
901
|
playWhenReady = true
|
|
857
|
-
if (currentTrack != null) {
|
|
858
|
-
// No-op unless the player is idle, so this reconnects only after stop() or an error and never
|
|
859
|
-
// re-buffers a healthy stream. Reconnecting is also how a live stream rejoins its live edge.
|
|
860
|
-
sessionPlayer.prepare()
|
|
861
|
-
}
|
|
862
902
|
}
|
|
863
903
|
|
|
864
904
|
/**
|
|
@@ -912,10 +952,9 @@ internal constructor(
|
|
|
912
952
|
}
|
|
913
953
|
}
|
|
914
954
|
|
|
955
|
+
/** An ended queue stays ended, as on an ended engine. */
|
|
915
956
|
fun prepare() {
|
|
916
|
-
if (currentTrack != null)
|
|
917
|
-
sessionPlayer.prepare()
|
|
918
|
-
}
|
|
957
|
+
if (currentTrack != null && playbackState != PlaybackState.ENDED) sessionPlayer.prepare()
|
|
919
958
|
}
|
|
920
959
|
|
|
921
960
|
fun pause() {
|
|
@@ -934,10 +973,14 @@ internal constructor(
|
|
|
934
973
|
|
|
935
974
|
/** Keeps the queue; the state becomes STOPPED. */
|
|
936
975
|
fun stop() {
|
|
976
|
+
nowPlaying.clearTimedMetadata()
|
|
937
977
|
// State first, so the state machine's STOPPED guard swallows the PAUSED the drop below causes.
|
|
938
978
|
setPlaybackState(PlaybackState.STOPPED)
|
|
979
|
+
val ended = sessionPlayer.playbackState == MediaPlayer.STATE_ENDED
|
|
939
980
|
playWhenReady = false
|
|
940
981
|
sessionPlayer.stop()
|
|
982
|
+
// Idle at its end, the next prepare would end it again at once.
|
|
983
|
+
if (ended) sessionPlayer.seekToDefaultPosition()
|
|
941
984
|
}
|
|
942
985
|
|
|
943
986
|
fun clear() {
|
|
@@ -949,6 +992,7 @@ internal constructor(
|
|
|
949
992
|
fun destroy() {
|
|
950
993
|
if (destroyed) return
|
|
951
994
|
destroyed = true
|
|
995
|
+
deferredSetup = null
|
|
952
996
|
// stop() saves only if playing; before it, so its STOPPED doesn't defeat the ENDED guard.
|
|
953
997
|
playbackStateStore.stopPeriodicSave()
|
|
954
998
|
// An armed timer would fire pause() into the released player. Cleared before the release,
|
|
@@ -1059,9 +1103,7 @@ internal constructor(
|
|
|
1059
1103
|
playbackState = state
|
|
1060
1104
|
|
|
1061
1105
|
if (state == PlaybackState.PLAYING) hasPlayedCache = true
|
|
1062
|
-
|
|
1063
|
-
forwardingPlayer.onPausedChanged()
|
|
1064
|
-
}
|
|
1106
|
+
forwardingPlayer.onStateChanged(oldState)
|
|
1065
1107
|
if (state == PlaybackState.ERROR) resetForNewLoad()
|
|
1066
1108
|
|
|
1067
1109
|
// A natural end exhausts the play intent; held, it inverts the toggle, keeps audio focus and
|
|
@@ -155,6 +155,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
155
155
|
// Deduped: the ENDED path in setPlaybackState may already have emitted it, before queue-ended.
|
|
156
156
|
player.emitPlayWhenReadyChanged(playWhenReady)
|
|
157
157
|
player.refreshPlayingState()
|
|
158
|
+
player.rebuildIfDueLater()
|
|
158
159
|
|
|
159
160
|
if (playWhenReady) {
|
|
160
161
|
player.playbackStateStore.startPeriodicSave()
|
|
@@ -165,6 +166,11 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
165
166
|
|
|
166
167
|
override fun onPlaybackSuppressionReasonChanged(playbackSuppressionReason: Int) {
|
|
167
168
|
player.refreshPlayingState()
|
|
169
|
+
player.rebuildIfDueLater()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
override fun onPlaybackStateChanged(playbackState: Int) {
|
|
173
|
+
player.rebuildIfDueLater()
|
|
168
174
|
}
|
|
169
175
|
|
|
170
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,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()
|