react-native-audio-browser 0.2.0-next.83 → 0.2.0-next.85
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/player/InterceptingPlayer.kt +31 -9
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +39 -46
- package/android/src/main/java/com/audiobrowser/player/Player.kt +220 -54
- package/android/src/main/java/com/audiobrowser/player/PlayerEngineFactory.kt +5 -6
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +94 -79
- package/android/src/main/java/com/audiobrowser/player/ReconnectPacer.kt +8 -9
- package/android/src/main/java/com/audiobrowser/player/RetryFocus.kt +126 -0
- package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +55 -121
- package/android/src/main/java/com/audiobrowser/player/RetryLocks.kt +52 -0
- package/android/src/main/java/com/audiobrowser/player/RetryLoop.kt +310 -0
- package/android/src/main/java/com/audiobrowser/player/RetryView.kt +31 -0
- package/android/src/main/java/com/audiobrowser/player/SessionViewListener.kt +148 -0
- package/android/src/main/java/com/audiobrowser/remote/RemoteOutputsController.kt +4 -1
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +15 -22
- package/android/src/main/java/com/audiobrowser/util/NetworkConnectivityMonitor.kt +4 -6
- package/android/src/test/java/androidx/media3/session/SessionViews.kt +9 -0
- package/android/src/test/java/com/audiobrowser/NetworkFixtures.kt +25 -0
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +15 -53
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +207 -53
- package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +23 -0
- package/android/src/test/java/com/audiobrowser/player/RetryFocusTest.kt +97 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicyTest.kt +90 -152
- package/android/src/test/java/com/audiobrowser/player/RetryLocksTest.kt +48 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoopCaseTest.kt +181 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoopTest.kt +221 -0
- package/android/src/test/java/com/audiobrowser/player/RetryMaskingTest.kt +176 -0
- package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +2 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewListenerTest.kt +21 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +2 -0
- package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +2 -1
- package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +37 -0
- package/android/src/test/java/com/audiobrowser/util/NetworkConnectivityMonitorTest.kt +13 -4
- package/lib/commonjs/features/player/setup.js +5 -4
- package/lib/commonjs/features/player/setup.js.map +1 -1
- package/lib/module/features/player/setup.js +5 -4
- package/lib/module/features/player/setup.js.map +1 -1
- package/lib/typescript/src/features/player/setup.d.ts +19 -13
- package/lib/typescript/src/features/player/setup.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/features/player/setup.ts +19 -13
- package/website/guide/errors.md +42 -38
- package/website/guide/network.md +4 -3
- package/android/src/main/java/com/audiobrowser/player/StuckRecoveryPolicy.kt +0 -37
- package/android/src/test/java/com/audiobrowser/player/StuckRecoveryPolicyTest.kt +0 -40
|
@@ -55,20 +55,32 @@ internal class InterceptingPlayer(
|
|
|
55
55
|
private val optionsProvider: () -> PlayerUpdateOptions,
|
|
56
56
|
private val keepSessionAliveOnError: Boolean,
|
|
57
57
|
private val playerState: () -> PlaybackState,
|
|
58
|
+
/** The engine state shown for the engine's own; see [Player.retryView]. */
|
|
59
|
+
private val engineView: (Int) -> Int,
|
|
60
|
+
/** Whether the engine's playing is a blip the retry loop hides. */
|
|
61
|
+
private val masksPlaying: () -> Boolean,
|
|
58
62
|
private val playPlayer: () -> Unit,
|
|
59
63
|
private val stopPlayer: () -> Unit,
|
|
60
64
|
) : ForwardingPlayer(player) {
|
|
61
65
|
|
|
62
|
-
private val
|
|
66
|
+
private val engineListeners = mutableSetOf<MediaPlayer.Listener>()
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
/** Each listener the session added, and the [SessionViewListener] it hears the engine through. */
|
|
69
|
+
private val views = mutableMapOf<MediaPlayer.Listener, SessionViewListener>()
|
|
70
|
+
|
|
71
|
+
/** Adds the player's own listener, which hears the engine's events as they are. */
|
|
72
|
+
fun addEngineListener(listener: MediaPlayer.Listener) {
|
|
73
|
+
engineListeners += listener
|
|
66
74
|
super.addListener(listener)
|
|
67
75
|
}
|
|
68
76
|
|
|
77
|
+
override fun addListener(listener: MediaPlayer.Listener) {
|
|
78
|
+
super.addListener(views.getOrPut(listener) { SessionViewListener(listener, this) })
|
|
79
|
+
}
|
|
80
|
+
|
|
69
81
|
override fun removeListener(listener: MediaPlayer.Listener) {
|
|
70
|
-
|
|
71
|
-
super.removeListener(listener)
|
|
82
|
+
engineListeners -= listener
|
|
83
|
+
super.removeListener(views.remove(listener) ?: listener)
|
|
72
84
|
}
|
|
73
85
|
|
|
74
86
|
/**
|
|
@@ -79,12 +91,13 @@ internal class InterceptingPlayer(
|
|
|
79
91
|
val view = playbackState
|
|
80
92
|
val was =
|
|
81
93
|
SessionView.playbackState(
|
|
82
|
-
super.getPlaybackState(),
|
|
94
|
+
engineView(super.getPlaybackState()),
|
|
83
95
|
old,
|
|
84
96
|
engineFailed,
|
|
85
97
|
keepSessionAliveOnError,
|
|
86
98
|
)
|
|
87
|
-
if (view != was)
|
|
99
|
+
if (view != was) engineListeners.toList().forEach { it.onPlaybackStateChanged(view) }
|
|
100
|
+
refreshViews()
|
|
88
101
|
}
|
|
89
102
|
|
|
90
103
|
// Media3 maps a player error to the legacy STATE_ERROR, which Android Auto and the notification
|
|
@@ -94,16 +107,25 @@ internal class InterceptingPlayer(
|
|
|
94
107
|
private val engineFailed: Boolean
|
|
95
108
|
get() = super.getPlayerError() != null
|
|
96
109
|
|
|
110
|
+
/** Tells the session's listeners where the view moved with no event of the engine's. */
|
|
111
|
+
fun refreshViews() = views.values.toList().forEach { it.refresh() }
|
|
112
|
+
|
|
113
|
+
override fun isPlaying(): Boolean = super.isPlaying() && !masksPlaying()
|
|
114
|
+
|
|
97
115
|
override fun getPlaybackState(): Int =
|
|
98
116
|
SessionView.playbackState(
|
|
99
|
-
super.getPlaybackState(),
|
|
117
|
+
engineView(super.getPlaybackState()),
|
|
100
118
|
playerState(),
|
|
101
119
|
engineFailed,
|
|
102
120
|
keepSessionAliveOnError,
|
|
103
121
|
)
|
|
104
122
|
|
|
105
123
|
override fun getPlayWhenReady(): Boolean =
|
|
106
|
-
SessionView.playWhenReady(
|
|
124
|
+
SessionView.playWhenReady(
|
|
125
|
+
super.getPlayWhenReady(),
|
|
126
|
+
engineView(super.getPlaybackState()),
|
|
127
|
+
playbackState,
|
|
128
|
+
)
|
|
107
129
|
|
|
108
130
|
// After a terminal error, a cancelled retry or a rebuild in ENDED the engine is idle, and the
|
|
109
131
|
// default skips only move the index. An ended queue that stayed put stays ended, as on an ended
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
+
import androidx.media3.common.C
|
|
4
5
|
import androidx.media3.common.MediaItem
|
|
6
|
+
import androidx.media3.common.util.Util
|
|
5
7
|
import androidx.media3.datasource.DataSource
|
|
6
8
|
import androidx.media3.datasource.DefaultDataSource
|
|
7
9
|
import androidx.media3.datasource.TransferListener
|
|
@@ -13,7 +15,6 @@ import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
|
|
|
13
15
|
import androidx.media3.exoplayer.source.MediaSource
|
|
14
16
|
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
|
|
15
17
|
import androidx.media3.extractor.DefaultExtractorsFactory
|
|
16
|
-
import androidx.media3.extractor.ExtractorsFactory
|
|
17
18
|
import com.audiobrowser.model.RetryPolicy
|
|
18
19
|
import com.audiobrowser.util.TrackFactory
|
|
19
20
|
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
@@ -26,9 +27,13 @@ class MediaFactory(
|
|
|
26
27
|
private val context: Context,
|
|
27
28
|
private val cache: SimpleCache?,
|
|
28
29
|
private val retryPolicy: RetryPolicy,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Each recoverable load failure of the playing item, on ExoPlayer's playback thread; see
|
|
32
|
+
* [RetryLoadErrorHandlingPolicy].
|
|
33
|
+
*/
|
|
34
|
+
private val onLoadFailure: (exception: IOException, inPlace: Boolean) -> Unit = { _, _ -> },
|
|
35
|
+
/** Whether an item is the one playing; read on ExoPlayer's playback thread. */
|
|
36
|
+
private val isCurrent: (MediaItem) -> Boolean = { true },
|
|
32
37
|
private val transferListener: TransferListener? = null,
|
|
33
38
|
private val getRequestConfig: (originalUrl: String, track: Track) -> MediaRequestConfig?,
|
|
34
39
|
) : MediaSource.Factory {
|
|
@@ -39,8 +44,8 @@ class MediaFactory(
|
|
|
39
44
|
/**
|
|
40
45
|
* The client media requests go out on, one per process so connections are reused. Not
|
|
41
46
|
* `HttpClient`'s browse client, whose read timeout is sized for JSON and would cut a long media
|
|
42
|
-
* read short. The timeouts are media3's own defaults, which stall detection and
|
|
43
|
-
*
|
|
47
|
+
* read short. The timeouts are media3's own defaults, which stall detection and [RetryLoop] are
|
|
48
|
+
* tuned to.
|
|
44
49
|
*/
|
|
45
50
|
private val mediaHttpClient: OkHttpClient by lazy {
|
|
46
51
|
OkHttpClient.Builder()
|
|
@@ -53,55 +58,30 @@ class MediaFactory(
|
|
|
53
58
|
|
|
54
59
|
private val extractorsFactory = DefaultExtractorsFactory().setConstantBitrateSeekingEnabled(true)
|
|
55
60
|
|
|
56
|
-
private val
|
|
57
|
-
when (retryPolicy) {
|
|
58
|
-
is RetryPolicy.Default -> null
|
|
59
|
-
is RetryPolicy.Infinite ->
|
|
60
|
-
RetryLoadErrorHandlingPolicy(
|
|
61
|
-
maxRetries = null,
|
|
62
|
-
maxRetryDurationMs = retryPolicy.maxRetryDurationMs,
|
|
63
|
-
firstConnectMaxRetryDurationMs = retryPolicy.firstConnectMaxRetryDurationMs,
|
|
64
|
-
isOnline = isOnline,
|
|
65
|
-
hasPlayed = hasPlayed,
|
|
66
|
-
onRetryPending = onRetryPending,
|
|
67
|
-
)
|
|
68
|
-
is RetryPolicy.Limited ->
|
|
69
|
-
RetryLoadErrorHandlingPolicy(
|
|
70
|
-
maxRetries = retryPolicy.maxRetries,
|
|
71
|
-
maxRetryDurationMs = retryPolicy.maxRetryDurationMs,
|
|
72
|
-
firstConnectMaxRetryDurationMs = retryPolicy.firstConnectMaxRetryDurationMs,
|
|
73
|
-
isOnline = isOnline,
|
|
74
|
-
hasPlayed = hasPlayed,
|
|
75
|
-
onRetryPending = onRetryPending,
|
|
76
|
-
)
|
|
77
|
-
}
|
|
61
|
+
private val mediaFactory = DefaultMediaSourceFactory(context, extractorsFactory)
|
|
78
62
|
|
|
79
|
-
private
|
|
80
|
-
DefaultMediaSourceFactory(context,
|
|
81
|
-
customRetryPolicy?.let { setLoadErrorHandlingPolicy(it) }
|
|
82
|
-
}
|
|
63
|
+
private val liveMediaFactory =
|
|
64
|
+
DefaultMediaSourceFactory(context, LiveStreamExtractor.Factory(extractorsFactory))
|
|
83
65
|
|
|
84
|
-
|
|
66
|
+
/** A policy set from outside, which replaces the retry policy for every item. */
|
|
67
|
+
private var externalPolicy: LoadErrorHandlingPolicy? = null
|
|
85
68
|
|
|
86
|
-
private val
|
|
87
|
-
|
|
88
|
-
/** Restarts the retry policy's time budgets. */
|
|
89
|
-
fun resetRetryTimer() {
|
|
90
|
-
customRetryPolicy?.reset()
|
|
91
|
-
}
|
|
69
|
+
private val factories
|
|
70
|
+
get() = listOf(mediaFactory, liveMediaFactory)
|
|
92
71
|
|
|
93
72
|
override fun setDrmSessionManagerProvider(
|
|
94
73
|
drmSessionManagerProvider: DrmSessionManagerProvider
|
|
95
74
|
): MediaSource.Factory {
|
|
96
|
-
|
|
97
|
-
return
|
|
75
|
+
factories.forEach { it.setDrmSessionManagerProvider(drmSessionManagerProvider) }
|
|
76
|
+
return this
|
|
98
77
|
}
|
|
99
78
|
|
|
100
79
|
override fun setLoadErrorHandlingPolicy(
|
|
101
80
|
loadErrorHandlingPolicy: LoadErrorHandlingPolicy
|
|
102
81
|
): MediaSource.Factory {
|
|
103
|
-
|
|
104
|
-
|
|
82
|
+
externalPolicy = loadErrorHandlingPolicy
|
|
83
|
+
factories.forEach { it.setLoadErrorHandlingPolicy(loadErrorHandlingPolicy) }
|
|
84
|
+
return this
|
|
105
85
|
}
|
|
106
86
|
|
|
107
87
|
override fun getSupportedTypes(): IntArray {
|
|
@@ -139,8 +119,21 @@ class MediaFactory(
|
|
|
139
119
|
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
|
|
140
120
|
} ?: transformingFactory
|
|
141
121
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
122
|
+
val local = mediaItem.localConfiguration
|
|
123
|
+
val (factory, sources) =
|
|
124
|
+
when {
|
|
125
|
+
// As DefaultMediaSourceFactory picks the source, so the policy matches it.
|
|
126
|
+
local != null &&
|
|
127
|
+
Util.inferContentTypeForUriAndMimeType(local.uri, local.mimeType) !=
|
|
128
|
+
C.CONTENT_TYPE_OTHER -> mediaFactory to RetryLoadErrorHandlingPolicy.Sources.ADAPTIVE
|
|
129
|
+
track.live == true -> liveMediaFactory to RetryLoadErrorHandlingPolicy.Sources.LIVE
|
|
130
|
+
else -> mediaFactory to RetryLoadErrorHandlingPolicy.Sources.PROGRESSIVE
|
|
131
|
+
}
|
|
132
|
+
// One policy per item: it tells the playing item's failures from a preload's.
|
|
133
|
+
if (externalPolicy == null && retryPolicy != RetryPolicy.Default)
|
|
134
|
+
factory.setLoadErrorHandlingPolicy(
|
|
135
|
+
RetryLoadErrorHandlingPolicy(sources, { isCurrent(mediaItem) }, onLoadFailure)
|
|
136
|
+
)
|
|
137
|
+
return factory.setDataSourceFactory(dataSourceFactory).createMediaSource(mediaItem)
|
|
145
138
|
}
|
|
146
139
|
}
|
|
@@ -2,12 +2,15 @@ package com.audiobrowser.player
|
|
|
2
2
|
|
|
3
3
|
import android.annotation.SuppressLint
|
|
4
4
|
import android.content.Context
|
|
5
|
+
import android.media.AudioManager
|
|
5
6
|
import android.os.Handler
|
|
6
7
|
import android.os.Looper
|
|
8
|
+
import android.os.SystemClock
|
|
7
9
|
import androidx.core.net.toUri
|
|
8
10
|
import androidx.media3.common.C
|
|
9
11
|
import androidx.media3.common.MediaItem
|
|
10
12
|
import androidx.media3.common.MediaMetadata
|
|
13
|
+
import androidx.media3.common.PlaybackException
|
|
11
14
|
import androidx.media3.common.Player as MediaPlayer
|
|
12
15
|
import androidx.media3.database.StandaloneDatabaseProvider
|
|
13
16
|
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor
|
|
@@ -26,6 +29,7 @@ import com.audiobrowser.extension.NumberExt.Companion.toSeconds
|
|
|
26
29
|
import com.audiobrowser.extension.identity
|
|
27
30
|
import com.audiobrowser.model.PlayerSetupOptions
|
|
28
31
|
import com.audiobrowser.model.PlayerUpdateOptions
|
|
32
|
+
import com.audiobrowser.model.RetryPolicy
|
|
29
33
|
import com.audiobrowser.remote.RemoteOutputsController
|
|
30
34
|
import com.audiobrowser.remote.RemotePlayback
|
|
31
35
|
import com.audiobrowser.remote.RemoteUrls
|
|
@@ -89,6 +93,26 @@ internal constructor(
|
|
|
89
93
|
|
|
90
94
|
private var options = PlayerUpdateOptions()
|
|
91
95
|
private val mainHandler = Handler(Looper.getMainLooper())
|
|
96
|
+
|
|
97
|
+
/** Audio focus while [retryLoop] keeps the engine idle; see [RetryFocus]. */
|
|
98
|
+
internal val retryFocus: RetryFocus =
|
|
99
|
+
RetryFocus(
|
|
100
|
+
RetryFocus.Platform(
|
|
101
|
+
context.getSystemService(Context.AUDIO_SERVICE) as AudioManager,
|
|
102
|
+
mainHandler,
|
|
103
|
+
) {
|
|
104
|
+
exoPlayer.audioAttributes.audioAttributesV21.audioAttributes
|
|
105
|
+
},
|
|
106
|
+
// A permanent loss pauses, as media3 does while playing.
|
|
107
|
+
onLoss = { playWhenReady = false },
|
|
108
|
+
onSuppressionChanged = {
|
|
109
|
+
// Posted: it changes the play intent the loop is reporting from.
|
|
110
|
+
mainHandler.post {
|
|
111
|
+
retryLoop?.playIntentChanged(shouldPlayWhenReady)
|
|
112
|
+
refreshPlayingState()
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
)
|
|
92
116
|
internal var callbacks: Callbacks? = null
|
|
93
117
|
private lateinit var mediaSession: MediaSession
|
|
94
118
|
val networkMonitor: NetworkConnectivityMonitor = NetworkConnectivityMonitor(context)
|
|
@@ -123,19 +147,65 @@ internal constructor(
|
|
|
123
147
|
internal lateinit var forwardingPlayer: InterceptingPlayer
|
|
124
148
|
|
|
125
149
|
private val remoteOutputs =
|
|
126
|
-
RemoteOutputsController(
|
|
150
|
+
RemoteOutputsController(
|
|
151
|
+
context,
|
|
152
|
+
::resolveRemoteUrls,
|
|
153
|
+
castRemote,
|
|
154
|
+
sonosRemote,
|
|
155
|
+
localHeld = { retryLoop?.reconnecting == true },
|
|
156
|
+
)
|
|
127
157
|
|
|
128
158
|
internal val localEngine =
|
|
129
159
|
LocalEngine({ exoPlayer }, isRemote = { remoteOutputs.player?.isRemote == true })
|
|
130
160
|
|
|
131
161
|
private var keepSessionAliveOnError = true
|
|
132
162
|
|
|
133
|
-
/**
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
163
|
+
/** Automatic retry for the local engine; null when the setup turns retry off. */
|
|
164
|
+
internal var retryLoop: RetryLoop? = null
|
|
165
|
+
private set
|
|
166
|
+
|
|
167
|
+
/** What the wake mode holds while [retryLoop] keeps the engine idle; see [RetryLocks]. */
|
|
168
|
+
internal var retryLocks: RetryLocks? = null
|
|
169
|
+
private set
|
|
170
|
+
|
|
171
|
+
/** The playing item's id, for the load error policy on ExoPlayer's playback thread. */
|
|
172
|
+
@Volatile internal var playingMediaId: String? = null
|
|
173
|
+
|
|
174
|
+
/** The last recoverable load failure, the cause of a give-up that has no player error in hand. */
|
|
175
|
+
@Volatile private var lastLoadFailure: IOException? = null
|
|
176
|
+
|
|
177
|
+
private val retryEngine =
|
|
178
|
+
object : RetryLoop.Engine {
|
|
179
|
+
override fun reconnect() =
|
|
180
|
+
retryFocus.handOver { localEngine.retry(toLiveEdge = isCurrentItemLive) }
|
|
181
|
+
|
|
182
|
+
// A play after a pause over a retry connects nothing until then: no failure shows it.
|
|
183
|
+
override fun holdChanged(held: Boolean, kept: Boolean) {
|
|
184
|
+
retryLocks?.hold(held)
|
|
185
|
+
// A suppressed request outlives the intent it dropped: it waits for the focus to return.
|
|
186
|
+
if (held) retryFocus.hold() else if (!kept || !retryFocus.suppressed) retryFocus.release()
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
override fun awaitNetwork() =
|
|
190
|
+
showRetryingError(
|
|
191
|
+
PlaybackErrorClassifier.retryingLoadError(lastLoadFailure ?: IOException(), false)
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
override fun giveUp(cause: PlaybackException?) {
|
|
195
|
+
// A remote output took over before the listener heard it: the loop is done either way.
|
|
196
|
+
if (!localEngine.isActive) return
|
|
197
|
+
// ERROR first: the state machine holds it through the IDLE the stop brings.
|
|
198
|
+
playerListener.surfaceTerminal(
|
|
199
|
+
cause
|
|
200
|
+
?: PlaybackException(
|
|
201
|
+
"No audio within the retry budget",
|
|
202
|
+
lastLoadFailure,
|
|
203
|
+
PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED,
|
|
204
|
+
)
|
|
205
|
+
)
|
|
206
|
+
exoPlayer.stop()
|
|
207
|
+
}
|
|
208
|
+
}
|
|
139
209
|
|
|
140
210
|
private var lastEmittedPlayWhenReady: Boolean? = null
|
|
141
211
|
|
|
@@ -311,8 +381,13 @@ internal constructor(
|
|
|
311
381
|
sessionPlayer.playWhenReady = value
|
|
312
382
|
// No-op unless the engine is idle, so this reconnects only after a stop, an error, a
|
|
313
383
|
// 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 (
|
|
384
|
+
// how a live stream rejoins its live edge. Not while the retry loop paces the reconnect.
|
|
385
|
+
if (
|
|
386
|
+
value &&
|
|
387
|
+
currentTrack != null &&
|
|
388
|
+
retryView(sessionPlayer.playbackState) == MediaPlayer.STATE_IDLE
|
|
389
|
+
)
|
|
390
|
+
sessionPlayer.prepare()
|
|
316
391
|
}
|
|
317
392
|
|
|
318
393
|
/**
|
|
@@ -358,10 +433,11 @@ internal constructor(
|
|
|
358
433
|
* False while ExoPlayer suppresses output (a transient audio focus loss) though [playWhenReady]
|
|
359
434
|
* stays raised to resume on; mirrors media3's `ExoPlayerImplInternal.shouldPlayWhenReady`.
|
|
360
435
|
*/
|
|
361
|
-
internal val shouldPlayWhenReady
|
|
436
|
+
internal val shouldPlayWhenReady: Boolean
|
|
362
437
|
get() =
|
|
363
438
|
sessionPlayer.playWhenReady &&
|
|
364
|
-
sessionPlayer.playbackSuppressionReason == MediaPlayer.PLAYBACK_SUPPRESSION_REASON_NONE
|
|
439
|
+
sessionPlayer.playbackSuppressionReason == MediaPlayer.PLAYBACK_SUPPRESSION_REASON_NONE &&
|
|
440
|
+
!retryFocus.suppressed
|
|
365
441
|
|
|
366
442
|
var repeatMode: RepeatMode
|
|
367
443
|
get() = RepeatModeFactory.fromMedia3(sessionPlayer.repeatMode)
|
|
@@ -504,14 +580,16 @@ internal constructor(
|
|
|
504
580
|
context,
|
|
505
581
|
setupOptions,
|
|
506
582
|
cache,
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
onRetryPending = ::reportRetryingError,
|
|
583
|
+
onLoadFailure = ::onLoadFailure,
|
|
584
|
+
isCurrent = { it.mediaId == playingMediaId },
|
|
510
585
|
resolveMediaConfig = { url, track -> browser?.getMediaRequestConfig(url, track) },
|
|
511
586
|
)
|
|
512
587
|
loadControl = engine.loadControl
|
|
513
588
|
mediaFactory = engine.mediaFactory
|
|
514
589
|
exoPlayer = engine.exoPlayer
|
|
590
|
+
// A reconnect the old loop held between connections: the engine is carried idle and unprepared.
|
|
591
|
+
val heldIdle = retryLoop?.reconnecting == true && localEngine.isActive
|
|
592
|
+
retryLoop?.suspend()
|
|
515
593
|
// Before the listener and the session are attached, so neither hears the playback replayed.
|
|
516
594
|
carried?.restoreTo(exoPlayer)
|
|
517
595
|
remoteOutputs.setLocal(exoPlayer)
|
|
@@ -519,6 +597,11 @@ internal constructor(
|
|
|
519
597
|
|
|
520
598
|
if (isInitialSetup) playerListener = PlayerListener(this)
|
|
521
599
|
attachSessionPlayer()
|
|
600
|
+
if (heldIdle && exoPlayer.playbackState == ExoPlayer.STATE_IDLE && exoPlayer.mediaItemCount > 0)
|
|
601
|
+
exoPlayer.prepare()
|
|
602
|
+
playerListener.engineAttached()
|
|
603
|
+
retryLoop = newRetryLoop(setupOptions)
|
|
604
|
+
playingMediaId = exoPlayer.currentMediaItem?.mediaId
|
|
522
605
|
|
|
523
606
|
if (isInitialSetup) {
|
|
524
607
|
callbacks?.onPlaybackChanged(Playback(PlaybackState.NONE, null))
|
|
@@ -545,10 +628,12 @@ internal constructor(
|
|
|
545
628
|
{ options },
|
|
546
629
|
keepSessionAliveOnError,
|
|
547
630
|
playerState = { playbackState },
|
|
631
|
+
engineView = ::retryView,
|
|
632
|
+
masksPlaying = { retryMasksPlaying },
|
|
548
633
|
playPlayer = ::play,
|
|
549
634
|
stopPlayer = ::stop,
|
|
550
635
|
)
|
|
551
|
-
forwardingPlayer.
|
|
636
|
+
forwardingPlayer.addEngineListener(playerListener)
|
|
552
637
|
if (::mediaSession.isInitialized) mediaSession.player = forwardingPlayer
|
|
553
638
|
}
|
|
554
639
|
|
|
@@ -578,6 +663,7 @@ internal constructor(
|
|
|
578
663
|
fun observeNetworkConnectivity(scope: kotlinx.coroutines.CoroutineScope) {
|
|
579
664
|
networkMonitor.observeOnline(scope) { isOnline ->
|
|
580
665
|
callbacks?.onOnlineChanged(isOnline)
|
|
666
|
+
retryLoop?.onlineChanged(isOnline)
|
|
581
667
|
|
|
582
668
|
// The formatter tells a stall from being offline, so it renders on a connectivity change too.
|
|
583
669
|
nowPlaying.render()
|
|
@@ -827,20 +913,88 @@ internal constructor(
|
|
|
827
913
|
.setTag(track)
|
|
828
914
|
.build()
|
|
829
915
|
|
|
916
|
+
private fun newRetryLoop(options: PlayerSetupOptions): RetryLoop? {
|
|
917
|
+
val (maxRetries, recoveryMs, firstConnectMs) =
|
|
918
|
+
when (val policy = options.retryPolicy) {
|
|
919
|
+
RetryPolicy.Default -> return null
|
|
920
|
+
is RetryPolicy.Infinite ->
|
|
921
|
+
Triple(null, policy.maxRetryDurationMs, policy.firstConnectMaxRetryDurationMs)
|
|
922
|
+
is RetryPolicy.Limited ->
|
|
923
|
+
Triple(
|
|
924
|
+
policy.maxRetries,
|
|
925
|
+
policy.maxRetryDurationMs,
|
|
926
|
+
policy.firstConnectMaxRetryDurationMs,
|
|
927
|
+
)
|
|
928
|
+
}
|
|
929
|
+
val recovery = recoveryMs ?: 120_000
|
|
930
|
+
val firstConnect = firstConnectMs ?: 12_000
|
|
931
|
+
retryLocks =
|
|
932
|
+
RetryLocks(context, options.wakeMode, maxHoldMs = recovery + firstConnect, mainHandler)
|
|
933
|
+
return RetryLoop(
|
|
934
|
+
clock = SystemClock::elapsedRealtime,
|
|
935
|
+
timer = { delayMs, action ->
|
|
936
|
+
val runnable = Runnable(action)
|
|
937
|
+
mainHandler.postDelayed(runnable, delayMs)
|
|
938
|
+
val cancel: () -> Unit = { mainHandler.removeCallbacks(runnable) }
|
|
939
|
+
cancel
|
|
940
|
+
},
|
|
941
|
+
engine = retryEngine,
|
|
942
|
+
maxRetries = maxRetries,
|
|
943
|
+
recoveryMs = recovery,
|
|
944
|
+
firstConnectMs = firstConnect,
|
|
945
|
+
online = networkMonitor.getOnline(),
|
|
946
|
+
)
|
|
947
|
+
.also {
|
|
948
|
+
it.playIntentChanged(shouldPlayWhenReady)
|
|
949
|
+
if (localEngine.isActive && exoPlayer.playbackState != MediaPlayer.STATE_IDLE) {
|
|
950
|
+
it.loadStarted()
|
|
951
|
+
it.isPlayingChanged(exoPlayer.isPlaying)
|
|
952
|
+
} else {
|
|
953
|
+
it.suspend()
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
830
958
|
/**
|
|
831
|
-
*
|
|
832
|
-
* goes terminal, since whatever restarts playback from it starts a new load and nothing loads
|
|
833
|
-
* until then.
|
|
959
|
+
* A new Load for the retry loop (ADR 0014): a track change, or the phone taking over playback.
|
|
834
960
|
*/
|
|
835
|
-
internal fun
|
|
836
|
-
|
|
837
|
-
|
|
961
|
+
internal fun startRetryLoad() {
|
|
962
|
+
playingMediaId = exoPlayer.currentMediaItem?.mediaId
|
|
963
|
+
val loop = retryLoop ?: return
|
|
964
|
+
if (!localEngine.isActive) return loop.suspend()
|
|
965
|
+
lastLoadFailure = null
|
|
966
|
+
if (exoPlayer.playbackState == MediaPlayer.STATE_IDLE) {
|
|
967
|
+
// Nothing connects an idle engine's new track but a prepare, and leaving idle starts its
|
|
968
|
+
// Load.
|
|
969
|
+
val held = loop.reconnecting
|
|
970
|
+
loop.suspend()
|
|
971
|
+
if (held) exoPlayer.prepare()
|
|
972
|
+
return
|
|
973
|
+
}
|
|
974
|
+
loop.loadStarted()
|
|
975
|
+
loop.isPlayingChanged(exoPlayer.isPlaying)
|
|
838
976
|
}
|
|
839
977
|
|
|
840
|
-
|
|
841
|
-
|
|
978
|
+
/**
|
|
979
|
+
* The engine state the listener and the session are shown: the retry loop's idle engine reads
|
|
980
|
+
* buffering, and so does a READY blip before a failed connection's error surfaces.
|
|
981
|
+
*/
|
|
982
|
+
internal fun retryView(@MediaPlayer.State engineState: Int): Int {
|
|
983
|
+
val loop = retryLoop?.takeIf { localEngine.isActive } ?: return engineState
|
|
984
|
+
return RetryView.engineState(
|
|
985
|
+
engineState,
|
|
986
|
+
loop.reconnecting,
|
|
987
|
+
loop.failurePending,
|
|
988
|
+
exoPlayer.totalBufferedDuration,
|
|
989
|
+
)
|
|
842
990
|
}
|
|
843
991
|
|
|
992
|
+
/** Whether the engine's playing is a blip the retry loop hides; see [retryView]. */
|
|
993
|
+
internal val retryMasksPlaying: Boolean
|
|
994
|
+
get() =
|
|
995
|
+
localEngine.isActive &&
|
|
996
|
+
RetryView.isBlip(retryLoop?.failurePending == true, exoPlayer.totalBufferedDuration)
|
|
997
|
+
|
|
844
998
|
/** Main thread only (ExoPlayer access). */
|
|
845
999
|
private fun findQueueTrackByArtworkUri(uri: String): Track? {
|
|
846
1000
|
for (i in 0 until sessionPlayer.mediaItemCount) {
|
|
@@ -978,7 +1132,10 @@ internal constructor(
|
|
|
978
1132
|
setPlaybackState(PlaybackState.STOPPED)
|
|
979
1133
|
val ended = sessionPlayer.playbackState == MediaPlayer.STATE_ENDED
|
|
980
1134
|
playWhenReady = false
|
|
1135
|
+
// The next play is a new Load.
|
|
1136
|
+
retryLoop?.suspend()
|
|
981
1137
|
sessionPlayer.stop()
|
|
1138
|
+
playerListener.fenceOutgoingMetadata()
|
|
982
1139
|
// Idle at its end, the next prepare would end it again at once.
|
|
983
1140
|
if (ended) sessionPlayer.seekToDefaultPosition()
|
|
984
1141
|
}
|
|
@@ -1023,46 +1180,54 @@ internal constructor(
|
|
|
1023
1180
|
}
|
|
1024
1181
|
|
|
1025
1182
|
/**
|
|
1026
|
-
* Bumped on track change so
|
|
1027
|
-
*
|
|
1183
|
+
* Bumped on track change so an [onLoadFailure] post for the outgoing track is dropped. Written on
|
|
1184
|
+
* main, read on ExoPlayer's playback thread.
|
|
1028
1185
|
*/
|
|
1029
1186
|
@Volatile private var retryingErrorGeneration = 0
|
|
1030
1187
|
|
|
1031
1188
|
/**
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1189
|
+
* A recoverable load failure, on ExoPlayer's playback thread. Posted to main ahead of the state
|
|
1190
|
+
* changes it causes, so the retry loop holds the engine before its READY blip or error arrives.
|
|
1191
|
+
* It also attaches the failure to the current state, for UIs to show over the spinner: not an
|
|
1192
|
+
* ERROR and not [Callbacks.onPlaybackError], which stays terminal.
|
|
1035
1193
|
*/
|
|
1036
|
-
|
|
1194
|
+
internal fun onLoadFailure(exception: IOException, inPlace: Boolean) {
|
|
1195
|
+
val atMs = SystemClock.elapsedRealtime()
|
|
1196
|
+
lastLoadFailure = exception
|
|
1037
1197
|
val error = PlaybackErrorClassifier.retryingLoadError(exception, networkMonitor.getOnline())
|
|
1038
1198
|
val generation = retryingErrorGeneration
|
|
1039
1199
|
mainHandler.post {
|
|
1040
1200
|
if (generation != retryingErrorGeneration) return@post
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
if (
|
|
1044
|
-
!playWhenReady &&
|
|
1045
|
-
playbackState !in setOf(PlaybackState.STOPPED, PlaybackState.ERROR, PlaybackState.ENDED)
|
|
1046
|
-
) {
|
|
1047
|
-
return@post stopRetrying()
|
|
1048
|
-
}
|
|
1049
|
-
// Only over a spinner: while PLAYING, buffered audio still plays and ExoPlayer's in-place
|
|
1050
|
-
// retry can succeed with no state change, so an error attached there would never clear.
|
|
1051
|
-
if (
|
|
1052
|
-
playbackState != PlaybackState.LOADING &&
|
|
1053
|
-
playbackState != PlaybackState.BUFFERING &&
|
|
1054
|
-
playbackState != PlaybackState.PAUSED
|
|
1055
|
-
) {
|
|
1056
|
-
return@post
|
|
1057
|
-
}
|
|
1058
|
-
// Each backoff tick re-reports the same error.
|
|
1059
|
-
if (playbackError == error) return@post
|
|
1060
|
-
playbackError = error
|
|
1061
|
-
callbacks?.onPlaybackChanged(Playback(playbackState, error))
|
|
1062
|
-
nowPlaying.render()
|
|
1201
|
+
if (localEngine.isActive) retryLoop?.connectionFailed(inPlace, atMs)
|
|
1202
|
+
showRetryingError(error)
|
|
1063
1203
|
}
|
|
1064
1204
|
}
|
|
1065
1205
|
|
|
1206
|
+
private fun showRetryingError(error: PlaybackError) {
|
|
1207
|
+
// Paused, the retry would run unseen and its failure be terminal. STOPPED, ERROR and ENDED
|
|
1208
|
+
// reach here only from a post that raced them.
|
|
1209
|
+
if (
|
|
1210
|
+
!playWhenReady &&
|
|
1211
|
+
playbackState !in setOf(PlaybackState.STOPPED, PlaybackState.ERROR, PlaybackState.ENDED)
|
|
1212
|
+
) {
|
|
1213
|
+
return stopRetrying()
|
|
1214
|
+
}
|
|
1215
|
+
// Only over a spinner: while PLAYING, buffered audio still plays and ExoPlayer's in-place
|
|
1216
|
+
// retry can succeed with no state change, so an error attached there would never clear.
|
|
1217
|
+
if (
|
|
1218
|
+
playbackState != PlaybackState.LOADING &&
|
|
1219
|
+
playbackState != PlaybackState.BUFFERING &&
|
|
1220
|
+
playbackState != PlaybackState.PAUSED
|
|
1221
|
+
) {
|
|
1222
|
+
return
|
|
1223
|
+
}
|
|
1224
|
+
// Each backoff tick re-reports the same error.
|
|
1225
|
+
if (playbackError == error) return
|
|
1226
|
+
playbackError = error
|
|
1227
|
+
callbacks?.onPlaybackChanged(Playback(playbackState, error))
|
|
1228
|
+
nowPlaying.render()
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1066
1231
|
/**
|
|
1067
1232
|
* Cancels the retry a dropped play intent leaves scheduled. No-op unless a retrying error shows.
|
|
1068
1233
|
*/
|
|
@@ -1081,7 +1246,8 @@ internal constructor(
|
|
|
1081
1246
|
setPlaybackState(PlaybackState.PAUSED)
|
|
1082
1247
|
clearRetryingError()
|
|
1083
1248
|
exoPlayer.stop()
|
|
1084
|
-
|
|
1249
|
+
// An engine the loop already holds idle leaves no IDLE for the listener to see.
|
|
1250
|
+
retryLoop?.engineStopped()
|
|
1085
1251
|
}
|
|
1086
1252
|
|
|
1087
1253
|
/**
|
|
@@ -1102,9 +1268,9 @@ internal constructor(
|
|
|
1102
1268
|
val oldState = playbackState
|
|
1103
1269
|
playbackState = state
|
|
1104
1270
|
|
|
1105
|
-
if (state == PlaybackState.PLAYING) hasPlayedCache = true
|
|
1106
1271
|
forwardingPlayer.onStateChanged(oldState)
|
|
1107
|
-
|
|
1272
|
+
// Whatever restarts playback from it starts a new Load.
|
|
1273
|
+
if (state == PlaybackState.ERROR) retryLoop?.suspend()
|
|
1108
1274
|
|
|
1109
1275
|
// A natural end exhausts the play intent; held, it inverts the toggle, keeps audio focus and
|
|
1110
1276
|
// the periodic save. Emitted before the state and queue-ended callbacks, matching iOS. The
|
|
@@ -3,6 +3,7 @@ package com.audiobrowser.player
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import androidx.media3.common.AudioAttributes
|
|
5
5
|
import androidx.media3.common.C
|
|
6
|
+
import androidx.media3.common.MediaItem
|
|
6
7
|
import androidx.media3.common.TrackSelectionParameters
|
|
7
8
|
import androidx.media3.datasource.cache.SimpleCache
|
|
8
9
|
import androidx.media3.exoplayer.DefaultRenderersFactory
|
|
@@ -49,9 +50,8 @@ internal fun buildPlayerEngine(
|
|
|
49
50
|
context: Context,
|
|
50
51
|
options: PlayerSetupOptions,
|
|
51
52
|
cache: SimpleCache?,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
onRetryPending: (exception: IOException) -> Unit,
|
|
53
|
+
onLoadFailure: (exception: IOException, inPlace: Boolean) -> Unit,
|
|
54
|
+
isCurrent: (MediaItem) -> Boolean,
|
|
55
55
|
resolveMediaConfig: (url: String, track: Track) -> MediaRequestConfig?,
|
|
56
56
|
): PlayerEngine {
|
|
57
57
|
val renderer = DefaultRenderersFactory(context)
|
|
@@ -66,9 +66,8 @@ internal fun buildPlayerEngine(
|
|
|
66
66
|
context,
|
|
67
67
|
cache,
|
|
68
68
|
options.retryPolicy,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
onRetryPending = onRetryPending,
|
|
69
|
+
onLoadFailure = onLoadFailure,
|
|
70
|
+
isCurrent = isCurrent,
|
|
72
71
|
transferListener = bandwidthMeter,
|
|
73
72
|
getRequestConfig = resolveMediaConfig,
|
|
74
73
|
)
|