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
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.net.wifi.WifiManager
|
|
5
|
+
import android.os.Handler
|
|
6
|
+
import android.os.PowerManager
|
|
7
|
+
import com.margelo.nitro.audiobrowser.AndroidPlayerWakeMode
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The locks the engine's wake mode holds while it loads, held instead while [RetryLoop] keeps the
|
|
11
|
+
* engine idle between connections: ExoPlayer releases its own in IDLE, and with the CPU asleep the
|
|
12
|
+
* loop's reconnect and deadline would not run.
|
|
13
|
+
*/
|
|
14
|
+
internal class RetryLocks(
|
|
15
|
+
context: Context,
|
|
16
|
+
private val wakeMode: AndroidPlayerWakeMode,
|
|
17
|
+
/** A backstop past the loop's deadline, which releases the locks first. */
|
|
18
|
+
private val maxHoldMs: Long,
|
|
19
|
+
private val handler: Handler,
|
|
20
|
+
) {
|
|
21
|
+
private val wakeLock =
|
|
22
|
+
(context.applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager)
|
|
23
|
+
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AudioBrowser:retry")
|
|
24
|
+
.apply { setReferenceCounted(false) }
|
|
25
|
+
|
|
26
|
+
@Suppress("DEPRECATION")
|
|
27
|
+
private val wifiLock =
|
|
28
|
+
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?)
|
|
29
|
+
?.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "AudioBrowser:retry")
|
|
30
|
+
?.apply { setReferenceCounted(false) }
|
|
31
|
+
|
|
32
|
+
/** The Wi-Fi lock has no timeout of its own. */
|
|
33
|
+
private val releaseWifiLock = Runnable { if (wifiLock?.isHeld == true) wifiLock.release() }
|
|
34
|
+
|
|
35
|
+
val wakeLockHeld: Boolean
|
|
36
|
+
get() = wakeLock.isHeld
|
|
37
|
+
|
|
38
|
+
val wifiLockHeld: Boolean
|
|
39
|
+
get() = wifiLock?.isHeld == true
|
|
40
|
+
|
|
41
|
+
fun hold(held: Boolean) {
|
|
42
|
+
if (held && wakeMode != AndroidPlayerWakeMode.NONE) wakeLock.acquire(maxHoldMs)
|
|
43
|
+
else if (wakeLock.isHeld) wakeLock.release()
|
|
44
|
+
handler.removeCallbacks(releaseWifiLock)
|
|
45
|
+
if (held && wakeMode == AndroidPlayerWakeMode.NETWORK) {
|
|
46
|
+
wifiLock?.acquire()
|
|
47
|
+
handler.postDelayed(releaseWifiLock, maxHoldMs)
|
|
48
|
+
} else {
|
|
49
|
+
releaseWifiLock.run()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import androidx.media3.common.PlaybackException
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Automatic retry for the local engine (docs/adr/0014): reconnects after a connection ends, paced
|
|
7
|
+
* by [ReconnectPacer], and gives up once [SilenceBudget]'s deadline passes. Main thread only.
|
|
8
|
+
*
|
|
9
|
+
* A connection the load error policy retries in place (an HLS segment, a file of known length) is
|
|
10
|
+
* never paced here: its failures only reach the budget, whose deadline gives up on it as well.
|
|
11
|
+
*/
|
|
12
|
+
internal class RetryLoop(
|
|
13
|
+
private val clock: () -> Long,
|
|
14
|
+
private val timer: Timer,
|
|
15
|
+
private val engine: Engine,
|
|
16
|
+
maxRetries: Int?,
|
|
17
|
+
recoveryMs: Long,
|
|
18
|
+
firstConnectMs: Long,
|
|
19
|
+
online: Boolean,
|
|
20
|
+
) {
|
|
21
|
+
fun interface Timer {
|
|
22
|
+
/** Runs [action] after [delayMs] on the main thread; the returned function cancels it. */
|
|
23
|
+
fun schedule(delayMs: Long, action: () -> Unit): () -> Unit
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Engine {
|
|
27
|
+
/** Prepares the engine again after a connection ended; it is idle when this is called. */
|
|
28
|
+
fun reconnect()
|
|
29
|
+
|
|
30
|
+
/** The next reconnect waits for the network. */
|
|
31
|
+
fun awaitNetwork()
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The loop starts or stops keeping the idle engine ([kept]), and [held] while the play intent
|
|
35
|
+
* is up as well: what keeps the device awake and the audio focus for its reconnect is the
|
|
36
|
+
* loop's to hold.
|
|
37
|
+
*/
|
|
38
|
+
fun holdChanged(held: Boolean, kept: Boolean)
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Stops the engine and surfaces the terminal error, caused by the last connection's failure, if
|
|
42
|
+
* the loop holds one.
|
|
43
|
+
*/
|
|
44
|
+
fun giveUp(cause: PlaybackException?)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private val budget = SilenceBudget(recoveryMs, firstConnectMs, online)
|
|
48
|
+
private val pacer = ReconnectPacer(maxRetries, online)
|
|
49
|
+
|
|
50
|
+
private var online = online
|
|
51
|
+
private var playIntent = false
|
|
52
|
+
private var audible = false
|
|
53
|
+
private var connectionStartedAtMs: Long? = null
|
|
54
|
+
private var deliveredAudio = false
|
|
55
|
+
|
|
56
|
+
/** When the policy reported that the current connection ended; its error has yet to surface. */
|
|
57
|
+
private var failedAtMs: Long? = null
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether the policy reported the current connection's end. Outlives [failedAtMs] when the engine
|
|
61
|
+
* is stopped, as its error may already be on its way.
|
|
62
|
+
*/
|
|
63
|
+
private var endReported = false
|
|
64
|
+
|
|
65
|
+
/** Whether the loop took a connection's error and left the engine idle until it reconnects. */
|
|
66
|
+
private var holdsIdleEngine = false
|
|
67
|
+
|
|
68
|
+
/** The error that ended the current connection: the cause the loop gives up with. */
|
|
69
|
+
private var lastError: PlaybackException? = null
|
|
70
|
+
|
|
71
|
+
private var waitingForNetwork = false
|
|
72
|
+
private var holding = false
|
|
73
|
+
private var keeping = false
|
|
74
|
+
private var pendingReconnect: (() -> Unit)? = null
|
|
75
|
+
private var cancelDeadline: (() -> Unit)? = null
|
|
76
|
+
|
|
77
|
+
/** Gave up, or suspended: nothing runs until the next [loadStarted]. */
|
|
78
|
+
var stopped = false
|
|
79
|
+
private set
|
|
80
|
+
|
|
81
|
+
/** When the pending reconnect goes out; null while none is scheduled. */
|
|
82
|
+
var reconnectDueMs: Long? = null
|
|
83
|
+
private set
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Whether the loop holds the engine between connections: a failed connection whose error has not
|
|
87
|
+
* surfaced, or an idle engine it will reconnect. Its idle engine and error are the loop's, not
|
|
88
|
+
* the listener's.
|
|
89
|
+
*/
|
|
90
|
+
val reconnecting: Boolean
|
|
91
|
+
get() = failedAtMs != null || (holdsIdleEngine && playIntent)
|
|
92
|
+
|
|
93
|
+
/** Whether the current connection failed and its error has not surfaced yet. */
|
|
94
|
+
val failurePending: Boolean
|
|
95
|
+
get() = failedAtMs != null
|
|
96
|
+
|
|
97
|
+
/** When the loop gives up unless audio plays first; null while none is due. */
|
|
98
|
+
val deadlineMs: Long?
|
|
99
|
+
get() = budget.deadlineMs
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A new Load: a track change, a restart from the terminal error, or the local engine taking over
|
|
103
|
+
* from a remote output. Its first connection is starting. Report [isPlayingChanged] after it when
|
|
104
|
+
* the engine already plays, as on a gapless track change.
|
|
105
|
+
*/
|
|
106
|
+
fun loadStarted() {
|
|
107
|
+
val now = clock()
|
|
108
|
+
cancelAll()
|
|
109
|
+
stopped = false
|
|
110
|
+
failedAtMs = null
|
|
111
|
+
endReported = false
|
|
112
|
+
holdsIdleEngine = false
|
|
113
|
+
lastError = null
|
|
114
|
+
budget.loadStarted(now)
|
|
115
|
+
pacer.reset()
|
|
116
|
+
// The audio that plays is the last Load's: the wiring reports this one's when it plays.
|
|
117
|
+
audible = false
|
|
118
|
+
deliveredAudio = false
|
|
119
|
+
connectionStartedAtMs = now
|
|
120
|
+
rearmDeadline()
|
|
121
|
+
syncHold()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Stops the loop until the next [loadStarted]: a terminal error, a stop, or a remote output. */
|
|
125
|
+
fun suspend() {
|
|
126
|
+
cancelAll()
|
|
127
|
+
failedAtMs = null
|
|
128
|
+
holdsIdleEngine = false
|
|
129
|
+
stopped = true
|
|
130
|
+
syncHold()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A play intent that comes back up over an idle engine the loop holds reconnects it: a pause
|
|
135
|
+
* prepares the engine itself, but a transient focus loss ends without one.
|
|
136
|
+
*/
|
|
137
|
+
fun playIntentChanged(up: Boolean) {
|
|
138
|
+
if (up == playIntent) return
|
|
139
|
+
playIntent = up
|
|
140
|
+
budget.playIntentChanged(up, clock())
|
|
141
|
+
pacer.playIntentChanged(up)
|
|
142
|
+
if (stopped) return
|
|
143
|
+
if (up) {
|
|
144
|
+
if (holdsIdleEngine) scheduleReconnect()
|
|
145
|
+
} else {
|
|
146
|
+
cancelReconnect()
|
|
147
|
+
}
|
|
148
|
+
rearmDeadline()
|
|
149
|
+
syncHold()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The local engine plays audio, or stops; a READY blip the wiring hides is not audio. */
|
|
153
|
+
fun isPlayingChanged(playing: Boolean) {
|
|
154
|
+
if (playing == audible) return
|
|
155
|
+
audible = playing
|
|
156
|
+
if (stopped) return
|
|
157
|
+
if (playing) {
|
|
158
|
+
deliveredAudio = true
|
|
159
|
+
budget.audioStarted(clock())
|
|
160
|
+
} else {
|
|
161
|
+
budget.audioStopped(clock())
|
|
162
|
+
}
|
|
163
|
+
rearmDeadline()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
fun onlineChanged(online: Boolean) {
|
|
167
|
+
if (online == this.online) return
|
|
168
|
+
this.online = online
|
|
169
|
+
budget.onlineChanged(online)
|
|
170
|
+
pacer.onlineChanged(online)
|
|
171
|
+
if (stopped) return
|
|
172
|
+
if (!online && pendingReconnect != null) {
|
|
173
|
+
cancelReconnect()
|
|
174
|
+
waitingForNetwork = true
|
|
175
|
+
}
|
|
176
|
+
if (online && waitingForNetwork) {
|
|
177
|
+
waitingForNetwork = false
|
|
178
|
+
scheduleReconnect()
|
|
179
|
+
}
|
|
180
|
+
rearmDeadline()
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The engine left idle: a connection starts, whoever prepared it. */
|
|
184
|
+
fun connectionStarted() {
|
|
185
|
+
if (stopped) return
|
|
186
|
+
cancelReconnect()
|
|
187
|
+
holdsIdleEngine = false
|
|
188
|
+
connectionStartedAtMs = clock()
|
|
189
|
+
deliveredAudio = audible
|
|
190
|
+
failedAtMs = null
|
|
191
|
+
endReported = false
|
|
192
|
+
lastError = null
|
|
193
|
+
syncHold()
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The engine was stopped, not failed: a pause over a retrying error. What the loop held of it is
|
|
198
|
+
* dropped; the next prepare is a connection like any other.
|
|
199
|
+
*/
|
|
200
|
+
fun engineStopped() {
|
|
201
|
+
cancelReconnect()
|
|
202
|
+
failedAtMs = null
|
|
203
|
+
holdsIdleEngine = false
|
|
204
|
+
syncHold()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The load error policy saw a recoverable failure, at [atMs]. [inPlace] when media3 retries it
|
|
209
|
+
* within its connection; otherwise the connection is over, and its error surfaces once the buffer
|
|
210
|
+
* has played out.
|
|
211
|
+
*/
|
|
212
|
+
fun connectionFailed(inPlace: Boolean, atMs: Long) {
|
|
213
|
+
if (stopped) return
|
|
214
|
+
budget.connectionEnded(atMs)
|
|
215
|
+
if (!inPlace && !endReported) {
|
|
216
|
+
endReported = true
|
|
217
|
+
failedAtMs = atMs
|
|
218
|
+
}
|
|
219
|
+
rearmDeadline()
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* An error the engine surfaced. True when the loop takes it: the end of a connection it was told
|
|
224
|
+
* of, or a stuck engine, which it reconnects as a plain connection end. A connection's end is
|
|
225
|
+
* paced by all the audio it delivered, the buffer it left included.
|
|
226
|
+
*/
|
|
227
|
+
fun onPlayerError(error: PlaybackException?, stuck: Boolean): Boolean {
|
|
228
|
+
if (stopped || (!endReported && !stuck)) return false
|
|
229
|
+
val now = clock()
|
|
230
|
+
val endedAtMs = failedAtMs ?: now
|
|
231
|
+
if (!endReported) budget.connectionEnded(now)
|
|
232
|
+
endReported = true
|
|
233
|
+
failedAtMs = null
|
|
234
|
+
lastError = error
|
|
235
|
+
if (audible) isPlayingChanged(false)
|
|
236
|
+
pacer.connectionEnded(deliveredAudio, connectionStartedAtMs ?: endedAtMs, endedAtMs)
|
|
237
|
+
holdsIdleEngine = true
|
|
238
|
+
if (playIntent) scheduleReconnect()
|
|
239
|
+
rearmDeadline()
|
|
240
|
+
syncHold()
|
|
241
|
+
return true
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private fun scheduleReconnect() {
|
|
245
|
+
cancelReconnect()
|
|
246
|
+
when (val wait = pacer.wait(clock())) {
|
|
247
|
+
ReconnectWait.GiveUp -> giveUp()
|
|
248
|
+
ReconnectWait.UntilOnline -> {
|
|
249
|
+
waitingForNetwork = true
|
|
250
|
+
engine.awaitNetwork()
|
|
251
|
+
}
|
|
252
|
+
is ReconnectWait.After -> {
|
|
253
|
+
lateinit var cancel: () -> Unit
|
|
254
|
+
cancel =
|
|
255
|
+
timer.schedule(wait.ms) {
|
|
256
|
+
if (pendingReconnect === cancel) {
|
|
257
|
+
pendingReconnect = null
|
|
258
|
+
reconnectDueMs = null
|
|
259
|
+
engine.reconnect()
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
pendingReconnect = cancel
|
|
263
|
+
reconnectDueMs = clock() + wait.ms
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private fun cancelReconnect() {
|
|
269
|
+
pendingReconnect?.invoke()
|
|
270
|
+
pendingReconnect = null
|
|
271
|
+
reconnectDueMs = null
|
|
272
|
+
waitingForNetwork = false
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private fun rearmDeadline() {
|
|
276
|
+
cancelDeadline?.invoke()
|
|
277
|
+
cancelDeadline = null
|
|
278
|
+
if (stopped) return
|
|
279
|
+
val deadline = budget.deadlineMs ?: return
|
|
280
|
+
cancelDeadline =
|
|
281
|
+
timer.schedule(maxOf(0, deadline - clock())) {
|
|
282
|
+
cancelDeadline = null
|
|
283
|
+
giveUp()
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private fun giveUp() {
|
|
288
|
+
cancelAll()
|
|
289
|
+
failedAtMs = null
|
|
290
|
+
holdsIdleEngine = false
|
|
291
|
+
stopped = true
|
|
292
|
+
engine.giveUp(lastError)
|
|
293
|
+
syncHold()
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private fun syncHold() {
|
|
297
|
+
val kept = holdsIdleEngine && !stopped
|
|
298
|
+
val held = kept && playIntent
|
|
299
|
+
if (held == holding && kept == keeping) return
|
|
300
|
+
holding = held
|
|
301
|
+
keeping = kept
|
|
302
|
+
engine.holdChanged(held, kept)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private fun cancelAll() {
|
|
306
|
+
cancelReconnect()
|
|
307
|
+
cancelDeadline?.invoke()
|
|
308
|
+
cancelDeadline = null
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import androidx.media3.common.Player as MediaPlayer
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What the listener and the session are shown of the local engine while [RetryLoop] holds it: its
|
|
7
|
+
* idle engine reads buffering, and so does media3's READY blip. Once a load fails, media3 stops
|
|
8
|
+
* loading, and with audio still in the sink it reads ready for a moment before the error surfaces,
|
|
9
|
+
* with nothing buffered. A READY with buffered audio is real.
|
|
10
|
+
*/
|
|
11
|
+
internal object RetryView {
|
|
12
|
+
/** Buffered audio below which a READY over a failed connection is the blip. */
|
|
13
|
+
const val BLIP_BUFFERED_MS = 100L
|
|
14
|
+
|
|
15
|
+
@MediaPlayer.State
|
|
16
|
+
fun engineState(
|
|
17
|
+
@MediaPlayer.State engineState: Int,
|
|
18
|
+
reconnecting: Boolean,
|
|
19
|
+
failurePending: Boolean,
|
|
20
|
+
bufferedMs: Long,
|
|
21
|
+
): Int =
|
|
22
|
+
when {
|
|
23
|
+
engineState == MediaPlayer.STATE_IDLE && reconnecting -> MediaPlayer.STATE_BUFFERING
|
|
24
|
+
engineState == MediaPlayer.STATE_READY && isBlip(failurePending, bufferedMs) ->
|
|
25
|
+
MediaPlayer.STATE_BUFFERING
|
|
26
|
+
else -> engineState
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fun isBlip(failurePending: Boolean, bufferedMs: Long): Boolean =
|
|
30
|
+
failurePending && bufferedMs < BLIP_BUFFERED_MS
|
|
31
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import androidx.media3.common.AudioAttributes
|
|
4
|
+
import androidx.media3.common.DeviceInfo
|
|
5
|
+
import androidx.media3.common.MediaItem
|
|
6
|
+
import androidx.media3.common.MediaMetadata
|
|
7
|
+
import androidx.media3.common.Metadata
|
|
8
|
+
import androidx.media3.common.PlaybackException
|
|
9
|
+
import androidx.media3.common.PlaybackParameters
|
|
10
|
+
import androidx.media3.common.Player as MediaPlayer
|
|
11
|
+
import androidx.media3.common.Timeline
|
|
12
|
+
import androidx.media3.common.TrackSelectionParameters
|
|
13
|
+
import androidx.media3.common.Tracks
|
|
14
|
+
import androidx.media3.common.VideoSize
|
|
15
|
+
import androidx.media3.common.text.Cue
|
|
16
|
+
import androidx.media3.common.text.CueGroup
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A session listener hearing the engine's events with [view]'s state and playing, and never its
|
|
20
|
+
* errors, which [InterceptingPlayer.getPlayerError] hides: the session copies an event's own
|
|
21
|
+
* arguments, so the view has to be in them. Every other event passes as it is.
|
|
22
|
+
*/
|
|
23
|
+
internal class SessionViewListener(
|
|
24
|
+
private val target: MediaPlayer.Listener,
|
|
25
|
+
private val view: MediaPlayer,
|
|
26
|
+
) : MediaPlayer.Listener {
|
|
27
|
+
private var toldState = view.playbackState
|
|
28
|
+
private var toldPlaying = view.isPlaying
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Tells the session the view's state and playing where they moved since it was last told: the
|
|
32
|
+
* view can move with no engine event, as when the retry loop lets go of an idle engine.
|
|
33
|
+
*/
|
|
34
|
+
fun refresh() {
|
|
35
|
+
if (view.playbackState != toldState) onPlaybackStateChanged(view.playbackState)
|
|
36
|
+
if (view.isPlaying != toldPlaying) onIsPlayingChanged(view.isPlaying)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
override fun onPlaybackStateChanged(playbackState: Int) {
|
|
40
|
+
toldState = view.playbackState
|
|
41
|
+
target.onPlaybackStateChanged(toldState)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Deprecated("Deprecated in Java")
|
|
45
|
+
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) =
|
|
46
|
+
@Suppress("DEPRECATION") target.onPlayerStateChanged(view.playWhenReady, view.playbackState)
|
|
47
|
+
|
|
48
|
+
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
|
49
|
+
toldPlaying = view.isPlaying
|
|
50
|
+
target.onIsPlayingChanged(toldPlaying)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
override fun onPlayerError(error: PlaybackException) = Unit
|
|
54
|
+
|
|
55
|
+
override fun onPlayerErrorChanged(error: PlaybackException?) = Unit
|
|
56
|
+
|
|
57
|
+
override fun onEvents(player: MediaPlayer, events: MediaPlayer.Events) =
|
|
58
|
+
target.onEvents(player, events)
|
|
59
|
+
|
|
60
|
+
override fun onTimelineChanged(timeline: Timeline, reason: Int) =
|
|
61
|
+
target.onTimelineChanged(timeline, reason)
|
|
62
|
+
|
|
63
|
+
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) =
|
|
64
|
+
target.onMediaItemTransition(mediaItem, reason)
|
|
65
|
+
|
|
66
|
+
override fun onTracksChanged(tracks: Tracks) = target.onTracksChanged(tracks)
|
|
67
|
+
|
|
68
|
+
override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) =
|
|
69
|
+
target.onMediaMetadataChanged(mediaMetadata)
|
|
70
|
+
|
|
71
|
+
override fun onPlaylistMetadataChanged(mediaMetadata: MediaMetadata) =
|
|
72
|
+
target.onPlaylistMetadataChanged(mediaMetadata)
|
|
73
|
+
|
|
74
|
+
override fun onIsLoadingChanged(isLoading: Boolean) = target.onIsLoadingChanged(isLoading)
|
|
75
|
+
|
|
76
|
+
@Deprecated("Deprecated in Java")
|
|
77
|
+
override fun onLoadingChanged(isLoading: Boolean) =
|
|
78
|
+
@Suppress("DEPRECATION") target.onLoadingChanged(isLoading)
|
|
79
|
+
|
|
80
|
+
override fun onAvailableCommandsChanged(availableCommands: MediaPlayer.Commands) =
|
|
81
|
+
target.onAvailableCommandsChanged(availableCommands)
|
|
82
|
+
|
|
83
|
+
override fun onTrackSelectionParametersChanged(parameters: TrackSelectionParameters) =
|
|
84
|
+
target.onTrackSelectionParametersChanged(parameters)
|
|
85
|
+
|
|
86
|
+
override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) =
|
|
87
|
+
target.onPlayWhenReadyChanged(playWhenReady, reason)
|
|
88
|
+
|
|
89
|
+
override fun onPlaybackSuppressionReasonChanged(playbackSuppressionReason: Int) =
|
|
90
|
+
target.onPlaybackSuppressionReasonChanged(playbackSuppressionReason)
|
|
91
|
+
|
|
92
|
+
override fun onRepeatModeChanged(repeatMode: Int) = target.onRepeatModeChanged(repeatMode)
|
|
93
|
+
|
|
94
|
+
override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) =
|
|
95
|
+
target.onShuffleModeEnabledChanged(shuffleModeEnabled)
|
|
96
|
+
|
|
97
|
+
@Deprecated("Deprecated in Java")
|
|
98
|
+
override fun onPositionDiscontinuity(reason: Int) =
|
|
99
|
+
@Suppress("DEPRECATION") target.onPositionDiscontinuity(reason)
|
|
100
|
+
|
|
101
|
+
override fun onPositionDiscontinuity(
|
|
102
|
+
oldPosition: MediaPlayer.PositionInfo,
|
|
103
|
+
newPosition: MediaPlayer.PositionInfo,
|
|
104
|
+
reason: Int,
|
|
105
|
+
) = target.onPositionDiscontinuity(oldPosition, newPosition, reason)
|
|
106
|
+
|
|
107
|
+
override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) =
|
|
108
|
+
target.onPlaybackParametersChanged(playbackParameters)
|
|
109
|
+
|
|
110
|
+
override fun onSeekBackIncrementChanged(seekBackIncrementMs: Long) =
|
|
111
|
+
target.onSeekBackIncrementChanged(seekBackIncrementMs)
|
|
112
|
+
|
|
113
|
+
override fun onSeekForwardIncrementChanged(seekForwardIncrementMs: Long) =
|
|
114
|
+
target.onSeekForwardIncrementChanged(seekForwardIncrementMs)
|
|
115
|
+
|
|
116
|
+
override fun onMaxSeekToPreviousPositionChanged(maxSeekToPreviousPositionMs: Long) =
|
|
117
|
+
target.onMaxSeekToPreviousPositionChanged(maxSeekToPreviousPositionMs)
|
|
118
|
+
|
|
119
|
+
override fun onAudioSessionIdChanged(audioSessionId: Int) =
|
|
120
|
+
target.onAudioSessionIdChanged(audioSessionId)
|
|
121
|
+
|
|
122
|
+
override fun onAudioAttributesChanged(audioAttributes: AudioAttributes) =
|
|
123
|
+
target.onAudioAttributesChanged(audioAttributes)
|
|
124
|
+
|
|
125
|
+
override fun onVolumeChanged(volume: Float) = target.onVolumeChanged(volume)
|
|
126
|
+
|
|
127
|
+
override fun onSkipSilenceEnabledChanged(skipSilenceEnabled: Boolean) =
|
|
128
|
+
target.onSkipSilenceEnabledChanged(skipSilenceEnabled)
|
|
129
|
+
|
|
130
|
+
override fun onDeviceInfoChanged(deviceInfo: DeviceInfo) = target.onDeviceInfoChanged(deviceInfo)
|
|
131
|
+
|
|
132
|
+
override fun onDeviceVolumeChanged(volume: Int, muted: Boolean) =
|
|
133
|
+
target.onDeviceVolumeChanged(volume, muted)
|
|
134
|
+
|
|
135
|
+
override fun onVideoSizeChanged(videoSize: VideoSize) = target.onVideoSizeChanged(videoSize)
|
|
136
|
+
|
|
137
|
+
override fun onSurfaceSizeChanged(width: Int, height: Int) =
|
|
138
|
+
target.onSurfaceSizeChanged(width, height)
|
|
139
|
+
|
|
140
|
+
override fun onRenderedFirstFrame() = target.onRenderedFirstFrame()
|
|
141
|
+
|
|
142
|
+
@Deprecated("Deprecated in Java")
|
|
143
|
+
override fun onCues(cues: List<Cue>) = @Suppress("DEPRECATION") target.onCues(cues)
|
|
144
|
+
|
|
145
|
+
override fun onCues(cueGroup: CueGroup) = target.onCues(cueGroup)
|
|
146
|
+
|
|
147
|
+
override fun onMetadata(metadata: Metadata) = target.onMetadata(metadata)
|
|
148
|
+
}
|
|
@@ -29,6 +29,8 @@ internal class RemoteOutputsController(
|
|
|
29
29
|
private val resolveUrls: suspend (Track) -> RemoteUrls,
|
|
30
30
|
private val castRemote: (Context, String?) -> RemotePlayback?,
|
|
31
31
|
private val sonosRemote: (Context, CoroutineScope) -> RemotePlayback,
|
|
32
|
+
/** Whether the phone's idle engine is the retry loop's, between connections of a load. */
|
|
33
|
+
private val localHeld: () -> Boolean = { false },
|
|
32
34
|
) {
|
|
33
35
|
private val scope = MainScope()
|
|
34
36
|
private var options: RemoteOutputsOptions? = null
|
|
@@ -67,7 +69,8 @@ internal class RemoteOutputsController(
|
|
|
67
69
|
player =
|
|
68
70
|
when {
|
|
69
71
|
remotes.isEmpty() -> null.also { previous?.release() }
|
|
70
|
-
previous == null ->
|
|
72
|
+
previous == null ->
|
|
73
|
+
RemoteSwitchingPlayer(local, remotes, ::resolve, scope, localHeld = localHeld)
|
|
71
74
|
else -> previous.also { it.setRemotes(remotes) }
|
|
72
75
|
}
|
|
73
76
|
return player !== previous
|
|
@@ -38,6 +38,8 @@ internal class RemoteSwitchingPlayer(
|
|
|
38
38
|
private val resolve: suspend (MediaItem) -> MediaItem,
|
|
39
39
|
private val scope: CoroutineScope,
|
|
40
40
|
private val startTimeoutMs: Long = 30_000,
|
|
41
|
+
/** Whether [local], though idle, holds a load the retry loop will reconnect. */
|
|
42
|
+
private val localHeld: () -> Boolean = { false },
|
|
41
43
|
) : ForwardingSimpleBasePlayer(local) {
|
|
42
44
|
|
|
43
45
|
var local: MediaPlayer = local
|
|
@@ -96,6 +98,13 @@ internal class RemoteSwitchingPlayer(
|
|
|
96
98
|
class Ended(val positionMs: Long) : Load
|
|
97
99
|
}
|
|
98
100
|
|
|
101
|
+
/**
|
|
102
|
+
* The phone's metadata last reported. A block is reported once, and as none from the next state
|
|
103
|
+
* on, so that an equal block after it goes out as ExoPlayer sends it: the state emits only a
|
|
104
|
+
* change, and a stream's first title after a reconnect often equals its last.
|
|
105
|
+
*/
|
|
106
|
+
private var reportedMetadata: Metadata? = null
|
|
107
|
+
|
|
99
108
|
init {
|
|
100
109
|
setRemotes(remotes)
|
|
101
110
|
}
|
|
@@ -135,22 +144,9 @@ internal class RemoteSwitchingPlayer(
|
|
|
135
144
|
private val remoteRoute: Route.Remote?
|
|
136
145
|
get() = route as? Route.Remote
|
|
137
146
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* The phone's metadata as it stopped, reported as none until the phone sends more. The state
|
|
143
|
-
* emits only a change, and a stream's first title after a reconnect often equals its last.
|
|
144
|
-
*/
|
|
145
|
-
private var titleAtStop: Metadata? = null
|
|
146
|
-
|
|
147
|
-
private fun withoutStoppedTitle(state: State): State {
|
|
148
|
-
if (localStopPending) {
|
|
149
|
-
localStopPending = false
|
|
150
|
-
titleAtStop = state.timedMetadata
|
|
151
|
-
}
|
|
152
|
-
if (state.timedMetadata !== titleAtStop) {
|
|
153
|
-
titleAtStop = null
|
|
147
|
+
private fun withEachBlockOnce(state: State): State {
|
|
148
|
+
if (state.timedMetadata !== reportedMetadata) {
|
|
149
|
+
reportedMetadata = state.timedMetadata
|
|
154
150
|
return state
|
|
155
151
|
}
|
|
156
152
|
return state.buildUpon().setTimedMetadata(NO_TIMED_METADATA).build()
|
|
@@ -235,7 +231,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
235
231
|
playIntent = local.playWhenReady
|
|
236
232
|
repeat = local.repeatMode
|
|
237
233
|
shuffle = local.shuffleModeEnabled
|
|
238
|
-
val prepared = local.playbackState != STATE_IDLE && items.isNotEmpty()
|
|
234
|
+
val prepared = (local.playbackState != STATE_IDLE || localHeld()) && items.isNotEmpty()
|
|
239
235
|
val localPositionMs = local.currentPosition
|
|
240
236
|
val live = local.isCurrentMediaItemLive
|
|
241
237
|
val remote = Route.Remote(target, target.player)
|
|
@@ -462,7 +458,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
462
458
|
|
|
463
459
|
override fun getState(): State {
|
|
464
460
|
val state = super.getState()
|
|
465
|
-
val remote = remoteRoute ?: return
|
|
461
|
+
val remote = remoteRoute ?: return withEachBlockOnce(state)
|
|
466
462
|
val load = remote.load
|
|
467
463
|
val holds = load is Load.Playing && receiverHolds(remote, load.item)
|
|
468
464
|
// Duration, seekability and liveness come from the receiver's data for the item it plays.
|
|
@@ -563,10 +559,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
563
559
|
|
|
564
560
|
override fun handleStop(): ListenableFuture<*> {
|
|
565
561
|
val remote = remoteRoute
|
|
566
|
-
if (remote == null)
|
|
567
|
-
localStopPending = true
|
|
568
|
-
return super.handleStop()
|
|
569
|
-
}
|
|
562
|
+
if (remote == null) return super.handleStop()
|
|
570
563
|
val positionMs = positionOn(remote)
|
|
571
564
|
cancelLoad(remote)
|
|
572
565
|
stopReceiver(remote.receiver)
|
|
@@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.launchIn
|
|
|
12
12
|
import kotlinx.coroutines.flow.onEach
|
|
13
13
|
import timber.log.Timber
|
|
14
14
|
|
|
15
|
-
/** Whether the default network has INTERNET
|
|
15
|
+
/** Whether the default network has INTERNET. Monitoring starts on construction. */
|
|
16
16
|
class NetworkConnectivityMonitor(private val context: Context) {
|
|
17
17
|
private val connectivityManager =
|
|
18
18
|
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
@@ -34,12 +34,10 @@ class NetworkConnectivityMonitor(private val context: Context) {
|
|
|
34
34
|
private fun capabilitiesOf(network: Network): NetworkCapabilities? =
|
|
35
35
|
connectivityManager.getNetworkCapabilities(network)
|
|
36
36
|
|
|
37
|
-
//
|
|
38
|
-
//
|
|
37
|
+
// Not VALIDATED: some networks never validate (a failing Private DNS, some hotspots and
|
|
38
|
+
// corporate Wi-Fi) yet reach the stream, and the retry loop makes no connection while offline.
|
|
39
39
|
private fun isOnline(capabilities: NetworkCapabilities?): Boolean =
|
|
40
|
-
capabilities
|
|
41
|
-
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
|
|
42
|
-
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
40
|
+
capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
|
|
43
41
|
|
|
44
42
|
private val networkCallback =
|
|
45
43
|
object : ConnectivityManager.NetworkCallback() {
|
|
@@ -8,3 +8,12 @@ internal fun MediaSession.legacyState() =
|
|
|
8
8
|
|
|
9
9
|
/** The current title media3 controllers were last told. */
|
|
10
10
|
internal fun MediaSession.controllerTitle() = impl.playerInfo.mediaMetadata.title?.toString()
|
|
11
|
+
|
|
12
|
+
/** The playback state media3 controllers (the notification among them) were last told. */
|
|
13
|
+
internal fun MediaSession.controllerState() = impl.playerInfo.playbackState
|
|
14
|
+
|
|
15
|
+
/** The player error media3 controllers were last told. */
|
|
16
|
+
internal fun MediaSession.controllerError() = impl.playerInfo.playerError
|
|
17
|
+
|
|
18
|
+
/** Whether media3 controllers were last told it plays. */
|
|
19
|
+
internal fun MediaSession.controllerPlaying() = impl.playerInfo.isPlaying
|