react-native-audio-browser 0.2.0-next.82 → 0.2.0-next.84
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 +219 -54
- package/android/src/main/java/com/audiobrowser/player/PlayerEngineFactory.kt +5 -6
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +88 -73
- 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 +3 -1
- 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 +190 -53
- 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/util/NetworkConnectivityMonitorTest.kt +13 -4
- package/ios/Player/ErrorStatus.swift +4 -0
- package/ios/Player/Player.swift +23 -9
- package/ios/Player/RetryLoop.swift +13 -9
- package/ios/Tests/PlayerPolicyTests.swift +114 -5
- package/ios/Tests/PlayerTests.swift +32 -0
- package/ios/Tests/RetryLoopTests.swift +8 -5
- 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
|
@@ -3,6 +3,7 @@ package com.audiobrowser.player
|
|
|
3
3
|
import android.os.Looper
|
|
4
4
|
import androidx.media3.common.C
|
|
5
5
|
import androidx.media3.common.ParserException
|
|
6
|
+
import androidx.media3.datasource.DataSourceException
|
|
6
7
|
import androidx.media3.decoder.DecoderInputBuffer
|
|
7
8
|
import androidx.media3.exoplayer.FormatHolder
|
|
8
9
|
import androidx.media3.exoplayer.LoadingInfo
|
|
@@ -26,7 +27,6 @@ import java.util.concurrent.atomic.AtomicInteger
|
|
|
26
27
|
import kotlin.concurrent.thread
|
|
27
28
|
import org.junit.After
|
|
28
29
|
import org.junit.Assert.assertEquals
|
|
29
|
-
import org.junit.Assert.assertNotNull
|
|
30
30
|
import org.junit.Assert.assertNull
|
|
31
31
|
import org.junit.Assert.assertTrue
|
|
32
32
|
import org.junit.Assert.fail
|
|
@@ -56,42 +56,15 @@ class LiveStreamEndTest {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
@Test
|
|
59
|
-
fun `a live stream
|
|
60
|
-
serve(framesFor = { request -> if (request == 1) SECONDS_1_5 else SECONDS_60 })
|
|
61
|
-
|
|
62
|
-
val outcome =
|
|
63
|
-
play(
|
|
64
|
-
live = true,
|
|
65
|
-
RetryPolicy.Infinite(),
|
|
66
|
-
stopAfterFrames = SECONDS_1_5 * 3,
|
|
67
|
-
hasPlayed = false,
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
assertNull(outcome.error)
|
|
71
|
-
assertEquals(2, requests.get())
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
@Test
|
|
75
|
-
fun `a live stream that closes once reconnects once and plays on`() {
|
|
76
|
-
serve(framesFor = { request -> if (request == 1) SECONDS_1_5 else SECONDS_60 })
|
|
77
|
-
|
|
78
|
-
val outcome = play(live = true, RetryPolicy.Infinite(), stopAfterFrames = SECONDS_1_5 * 3)
|
|
79
|
-
|
|
80
|
-
assertNull(outcome.error)
|
|
81
|
-
assertTrue("the stream played on", !outcome.ended)
|
|
82
|
-
assertEquals(2, requests.get())
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
@Test
|
|
86
|
-
fun `a live stream that closes after every second and a half ends in an error`() {
|
|
59
|
+
fun `a live stream's close surfaces once its audio has been read`() {
|
|
87
60
|
serve(framesFor = { SECONDS_1_5 })
|
|
88
61
|
|
|
89
62
|
val outcome = play(live = true, RetryPolicy.Infinite())
|
|
90
63
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
assertEquals(
|
|
64
|
+
assertTrue("not the close: ${outcome.error}", outcome.error is DataSourceException)
|
|
65
|
+
assertEquals(SECONDS_1_5, played.get())
|
|
66
|
+
// The retry loop reconnects, not media3.
|
|
67
|
+
assertEquals(1, requests.get())
|
|
95
68
|
}
|
|
96
69
|
|
|
97
70
|
@Test
|
|
@@ -219,20 +192,9 @@ class LiveStreamEndTest {
|
|
|
219
192
|
|
|
220
193
|
private class Outcome(val ended: Boolean, val error: IOException?)
|
|
221
194
|
|
|
222
|
-
private fun play(
|
|
223
|
-
live: Boolean,
|
|
224
|
-
retryPolicy: RetryPolicy,
|
|
225
|
-
stopAfterFrames: Int = Int.MAX_VALUE,
|
|
226
|
-
hasPlayed: Boolean = true,
|
|
227
|
-
): Outcome {
|
|
195
|
+
private fun play(live: Boolean, retryPolicy: RetryPolicy): Outcome {
|
|
228
196
|
val factory =
|
|
229
|
-
MediaFactory(
|
|
230
|
-
context,
|
|
231
|
-
cache = null,
|
|
232
|
-
retryPolicy,
|
|
233
|
-
hasPlayed = { hasPlayed },
|
|
234
|
-
getRequestConfig = { _, _ -> null },
|
|
235
|
-
)
|
|
197
|
+
MediaFactory(context, cache = null, retryPolicy, getRequestConfig = { _, _ -> null })
|
|
236
198
|
val item =
|
|
237
199
|
TrackFactory.toMedia3(
|
|
238
200
|
track(src = "http://127.0.0.1:${server.localPort}/stream.mp3")
|
|
@@ -286,14 +248,9 @@ class LiveStreamEndTest {
|
|
|
286
248
|
val stream = checkNotNull(streams[0])
|
|
287
249
|
val buffer = DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DISABLED)
|
|
288
250
|
val deadline = System.currentTimeMillis() + 60_000
|
|
289
|
-
while (
|
|
251
|
+
while (true) {
|
|
290
252
|
if (System.currentTimeMillis() > deadline)
|
|
291
253
|
fail("still playing after ${played.get()} frames")
|
|
292
|
-
try {
|
|
293
|
-
stream.maybeThrowError()
|
|
294
|
-
} catch (e: IOException) {
|
|
295
|
-
return Outcome(ended = false, error = e)
|
|
296
|
-
}
|
|
297
254
|
buffer.clear()
|
|
298
255
|
val result = stream.readData(FormatHolder(), buffer, SampleStream.FLAG_OMIT_SAMPLE_DATA)
|
|
299
256
|
period.readDiscontinuity()
|
|
@@ -304,13 +261,18 @@ class LiveStreamEndTest {
|
|
|
304
261
|
ShadowSystemClock.advanceBy(Duration.ofNanos(FRAME_NS))
|
|
305
262
|
}
|
|
306
263
|
C.RESULT_NOTHING_READ -> {
|
|
264
|
+
// As a renderer that has run out does: an error surfaces once the buffer is empty.
|
|
265
|
+
try {
|
|
266
|
+
stream.maybeThrowError()
|
|
267
|
+
} catch (e: IOException) {
|
|
268
|
+
return Outcome(ended = false, error = e)
|
|
269
|
+
}
|
|
307
270
|
period.continueLoading(LoadingInfo.Builder().build())
|
|
308
271
|
Thread.sleep(1)
|
|
309
272
|
}
|
|
310
273
|
}
|
|
311
274
|
shadowOf(Looper.getMainLooper()).idle()
|
|
312
275
|
}
|
|
313
|
-
return Outcome(ended = false, error = null)
|
|
314
276
|
} finally {
|
|
315
277
|
source.releasePeriod(period)
|
|
316
278
|
source.releaseSource { _, _ -> }
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
+
import android.media.AudioManager
|
|
4
5
|
import android.net.ConnectivityManager
|
|
5
6
|
import android.net.NetworkCapabilities
|
|
6
7
|
import android.net.Uri
|
|
@@ -11,18 +12,23 @@ import androidx.media3.common.Player as MediaPlayer
|
|
|
11
12
|
import androidx.media3.common.util.StuckPlayerException
|
|
12
13
|
import com.audiobrowser.Callbacks
|
|
13
14
|
import com.audiobrowser.TestFixtures
|
|
15
|
+
import com.audiobrowser.goOnline
|
|
14
16
|
import com.audiobrowser.model.PlayerSetupOptions
|
|
15
17
|
import com.audiobrowser.model.RetryPolicy
|
|
16
18
|
import com.audiobrowser.util.TrackFactory
|
|
19
|
+
import com.margelo.nitro.audiobrowser.AndroidPlayerWakeMode
|
|
17
20
|
import com.margelo.nitro.audiobrowser.Playback
|
|
18
21
|
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
19
22
|
import java.io.File
|
|
23
|
+
import java.io.IOException
|
|
20
24
|
import java.net.ServerSocket
|
|
21
25
|
import java.nio.ByteBuffer
|
|
22
26
|
import java.nio.ByteOrder
|
|
23
27
|
import java.time.Duration
|
|
28
|
+
import java.util.concurrent.CopyOnWriteArrayList
|
|
24
29
|
import java.util.concurrent.CountDownLatch
|
|
25
30
|
import java.util.concurrent.TimeUnit
|
|
31
|
+
import java.util.concurrent.atomic.AtomicInteger
|
|
26
32
|
import kotlin.concurrent.thread
|
|
27
33
|
import kotlinx.coroutines.CoroutineScope
|
|
28
34
|
import kotlinx.coroutines.Dispatchers
|
|
@@ -31,6 +37,7 @@ import org.junit.After
|
|
|
31
37
|
import org.junit.Assert.assertEquals
|
|
32
38
|
import org.junit.Assert.assertFalse
|
|
33
39
|
import org.junit.Assert.assertNotEquals
|
|
40
|
+
import org.junit.Assert.assertNotSame
|
|
34
41
|
import org.junit.Assert.assertTrue
|
|
35
42
|
import org.junit.Assert.fail
|
|
36
43
|
import org.junit.Rule
|
|
@@ -64,26 +71,16 @@ class PlayerRetryTest {
|
|
|
64
71
|
private val scope = CoroutineScope(Dispatchers.Unconfined)
|
|
65
72
|
private val callbacks = mock(Callbacks::class.java)
|
|
66
73
|
|
|
74
|
+
private val servers = mutableListOf<ServerSocket>()
|
|
75
|
+
|
|
67
76
|
@After
|
|
68
77
|
fun release() {
|
|
78
|
+
servers.forEach { it.close() }
|
|
69
79
|
scope.cancel()
|
|
70
80
|
player.networkMonitor.destroy()
|
|
71
81
|
player.exoPlayer.release()
|
|
72
82
|
}
|
|
73
83
|
|
|
74
|
-
@Test
|
|
75
|
-
fun `a recovery keeps a load that has played`() {
|
|
76
|
-
play(Uri.fromFile(silentWav()).toString())
|
|
77
|
-
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
78
|
-
|
|
79
|
-
player.playerListener.onPlayerError(
|
|
80
|
-
PlaybackException("behind", null, PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW)
|
|
81
|
-
)
|
|
82
|
-
player.playerListener.onPlayerErrorChanged(null)
|
|
83
|
-
|
|
84
|
-
assertTrue(player.hasPlayedCache)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
84
|
@Test
|
|
88
85
|
fun `a terminal error ends the load`() {
|
|
89
86
|
play(Uri.fromFile(silentWav()).toString())
|
|
@@ -94,7 +91,6 @@ class PlayerRetryTest {
|
|
|
94
91
|
)
|
|
95
92
|
|
|
96
93
|
assertEquals(PlaybackState.ERROR, player.playbackState)
|
|
97
|
-
assertFalse(player.hasPlayedCache)
|
|
98
94
|
}
|
|
99
95
|
|
|
100
96
|
@Test
|
|
@@ -329,7 +325,7 @@ class PlayerRetryTest {
|
|
|
329
325
|
|
|
330
326
|
@Test
|
|
331
327
|
fun `a player that gave up stays stopped when the network returns`() {
|
|
332
|
-
connectivity.setNetworkCapabilities(wifi, capabilities(
|
|
328
|
+
connectivity.setNetworkCapabilities(wifi, capabilities(internet = false))
|
|
333
329
|
connectivity.networkCallbacks.forEach { it.onAvailable(wifi) }
|
|
334
330
|
// A closed port: the load fails at once, the retry is pending, and the next failure is
|
|
335
331
|
// terminal.
|
|
@@ -348,9 +344,9 @@ class PlayerRetryTest {
|
|
|
348
344
|
},
|
|
349
345
|
)
|
|
350
346
|
|
|
351
|
-
connectivity.setNetworkCapabilities(wifi, capabilities(
|
|
347
|
+
connectivity.setNetworkCapabilities(wifi, capabilities(internet = true))
|
|
352
348
|
connectivity.networkCallbacks.forEach {
|
|
353
|
-
it.onCapabilitiesChanged(wifi, capabilities(
|
|
349
|
+
it.onCapabilitiesChanged(wifi, capabilities(internet = true))
|
|
354
350
|
}
|
|
355
351
|
shadowOf(Looper.getMainLooper()).idle()
|
|
356
352
|
|
|
@@ -359,48 +355,186 @@ class PlayerRetryTest {
|
|
|
359
355
|
}
|
|
360
356
|
|
|
361
357
|
@Test
|
|
362
|
-
fun `
|
|
363
|
-
|
|
358
|
+
fun `the wake mode's lock is held while the loop keeps the idle engine, and let go on pause`() {
|
|
359
|
+
goOnline(context)
|
|
360
|
+
play(
|
|
361
|
+
"https://127.0.0.1:9/stream",
|
|
362
|
+
retryPolicy = RetryPolicy.Infinite(),
|
|
363
|
+
wakeMode = AndroidPlayerWakeMode.NETWORK,
|
|
364
|
+
)
|
|
365
|
+
idleUntil("the loop holding the idle engine") {
|
|
366
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
367
|
+
player.retryLoop!!.reconnecting && player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE
|
|
368
|
+
}
|
|
369
|
+
assertTrue("no wake lock while held", player.retryLocks!!.wakeLockHeld)
|
|
364
370
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
371
|
+
player.pause()
|
|
372
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
373
|
+
|
|
374
|
+
assertFalse("a wake lock over a pause", player.retryLocks!!.wakeLockHeld)
|
|
368
375
|
}
|
|
369
376
|
|
|
370
377
|
@Test
|
|
371
|
-
fun `
|
|
372
|
-
|
|
378
|
+
fun `a track change on a stopped engine starts no Load until it is prepared`() {
|
|
379
|
+
goOnline(context)
|
|
380
|
+
val wav = Uri.fromFile(silentWav()).toString()
|
|
381
|
+
play(wav, wav, retryPolicy = RetryPolicy.Infinite())
|
|
382
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
383
|
+
player.exoPlayer.stop()
|
|
384
|
+
|
|
385
|
+
player.exoPlayer.seekToNextMediaItem()
|
|
386
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
373
387
|
|
|
374
|
-
|
|
388
|
+
assertTrue("a deadline runs for a track nothing connects", player.retryLoop!!.stopped)
|
|
375
389
|
}
|
|
376
390
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
* not idle, so the recovery's re-prepare does nothing: this covers the budget refill only.
|
|
381
|
-
*/
|
|
382
|
-
private fun stuckRecoveryMidBudget(online: Boolean) {
|
|
383
|
-
connectivity.setNetworkCapabilities(wifi, capabilities(validated = online))
|
|
384
|
-
connectivity.networkCallbacks.forEach { it.onAvailable(wifi) }
|
|
385
|
-
play(
|
|
386
|
-
"https://127.0.0.1:9/stream",
|
|
387
|
-
retryPolicy = RetryPolicy.Infinite(maxRetryDurationMs = 30_000),
|
|
388
|
-
)
|
|
389
|
-
awaitRetry("a pending retry")
|
|
390
|
-
player.hasPlayedCache = true
|
|
391
|
-
advanceSeconds(25)
|
|
392
|
-
assertEquals(true, player.playbackError?.retrying)
|
|
391
|
+
@Test
|
|
392
|
+
fun `another app taking the focus during a reconnect wait pauses, and nothing takes it back`() {
|
|
393
|
+
val connections = holdIdleEngine()
|
|
393
394
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
395
|
+
focusListener().onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS)
|
|
396
|
+
val after = connections.get()
|
|
397
|
+
advanceSeconds(10)
|
|
398
|
+
|
|
399
|
+
assertFalse(player.playWhenReady)
|
|
400
|
+
assertEquals("a reconnect took the focus back", after, connections.get())
|
|
401
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
@Test
|
|
405
|
+
fun `a transient focus loss during a reconnect wait holds the reconnect until the focus returns`() {
|
|
406
|
+
val connections = holdIdleEngine()
|
|
407
|
+
|
|
408
|
+
focusListener().onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
409
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
410
|
+
val after = connections.get()
|
|
411
|
+
advanceSeconds(10)
|
|
412
|
+
assertEquals("a reconnect during the loss", after, connections.get())
|
|
413
|
+
assertTrue(player.playWhenReady)
|
|
414
|
+
|
|
415
|
+
focusListener().onAudioFocusChange(AudioManager.AUDIOFOCUS_GAIN)
|
|
416
|
+
idleUntil("a reconnect once the focus returned") {
|
|
417
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
418
|
+
connections.get() > after
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Plays from a server that ends each connection unanswered, until the loop holds the engine. */
|
|
423
|
+
private fun holdIdleEngine(): AtomicInteger {
|
|
424
|
+
goOnline(context)
|
|
425
|
+
val server = ServerSocket(0)
|
|
426
|
+
servers += server
|
|
427
|
+
val connections = AtomicInteger()
|
|
428
|
+
thread(isDaemon = true) {
|
|
429
|
+
while (!server.isClosed) {
|
|
430
|
+
try {
|
|
431
|
+
server.accept().close()
|
|
432
|
+
connections.incrementAndGet()
|
|
433
|
+
} catch (_: IOException) {
|
|
434
|
+
return@thread
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
play("http://127.0.0.1:${server.localPort}/live", retryPolicy = RetryPolicy.Infinite())
|
|
439
|
+
idleUntil("the loop holding the engine") {
|
|
440
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
441
|
+
player.retryLoop!!.reconnecting && player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE
|
|
442
|
+
}
|
|
443
|
+
val audio = shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
|
|
444
|
+
assertNotSame(
|
|
445
|
+
"no focus held during the wait",
|
|
446
|
+
audio.lastAbandonedAudioFocusRequest,
|
|
447
|
+
audio.lastAudioFocusRequest.audioFocusRequest,
|
|
400
448
|
)
|
|
401
|
-
|
|
449
|
+
return connections
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** The listener of the last focus request: the retry loop's, while it holds the engine. */
|
|
453
|
+
private fun focusListener() =
|
|
454
|
+
shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
|
|
455
|
+
.lastAudioFocusRequest
|
|
456
|
+
.listener
|
|
457
|
+
|
|
458
|
+
@Test
|
|
459
|
+
fun `a skip while the loop holds the idle engine connects the next track`() {
|
|
460
|
+
goOnline(context)
|
|
461
|
+
val server = ServerSocket(0)
|
|
462
|
+
val paths = CopyOnWriteArrayList<String>()
|
|
463
|
+
// Reads each request line and ends the connection unanswered.
|
|
464
|
+
thread(isDaemon = true) {
|
|
465
|
+
while (!server.isClosed) {
|
|
466
|
+
try {
|
|
467
|
+
server.accept().use { paths += it.getInputStream().bufferedReader().readLine() }
|
|
468
|
+
} catch (_: IOException) {
|
|
469
|
+
return@thread
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
val base = "http://127.0.0.1:${server.localPort}"
|
|
474
|
+
play("$base/one", "$base/two", retryPolicy = RetryPolicy.Infinite())
|
|
475
|
+
idleUntil("the loop holding the engine") {
|
|
476
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
477
|
+
player.retryLoop!!.reconnecting && player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
player.forwardingPlayer.seekToNext()
|
|
481
|
+
|
|
482
|
+
idleUntil("a request for the next track") {
|
|
483
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
484
|
+
paths.any { it.contains("/two") }
|
|
485
|
+
}
|
|
486
|
+
server.close()
|
|
402
487
|
}
|
|
403
488
|
|
|
489
|
+
@Test
|
|
490
|
+
fun `a failure report that arrives after a reconnect is not the new connection's`() {
|
|
491
|
+
goOnline(context)
|
|
492
|
+
play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
|
|
493
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
494
|
+
|
|
495
|
+
// Posted from the playback thread before the go-live reconnect, run after it.
|
|
496
|
+
player.onLoadFailure(IOException("closed"), inPlace = false)
|
|
497
|
+
player.localEngine.reconnectAtLiveEdge()
|
|
498
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
499
|
+
|
|
500
|
+
assertFalse(player.retryLoop!!.failurePending)
|
|
501
|
+
assertEquals(null, player.playbackError)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
@Test
|
|
505
|
+
fun `with retry, a stuck engine reconnects and never ends in an error`() {
|
|
506
|
+
goOnline(context)
|
|
507
|
+
play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
|
|
508
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
509
|
+
|
|
510
|
+
// As media3 leaves a stuck engine: idle, with the error.
|
|
511
|
+
player.exoPlayer.stop()
|
|
512
|
+
player.playerListener.onPlayerError(stuck())
|
|
513
|
+
advanceSeconds(2)
|
|
514
|
+
|
|
515
|
+
assertNotEquals("no reconnect", MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
516
|
+
assertEquals(null, player.playbackError)
|
|
517
|
+
assertNotEquals(PlaybackState.ERROR, player.playbackState)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
@Test
|
|
521
|
+
fun `with retry off, a stuck engine ends in the stalled error`() {
|
|
522
|
+
play(Uri.fromFile(silentWav()).toString())
|
|
523
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
524
|
+
|
|
525
|
+
player.playerListener.onPlayerError(stuck())
|
|
526
|
+
|
|
527
|
+
assertEquals(PlaybackState.ERROR, player.playbackState)
|
|
528
|
+
assertEquals("playback-stalled", player.playbackError?.code)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
private fun stuck() =
|
|
532
|
+
PlaybackException(
|
|
533
|
+
"stuck",
|
|
534
|
+
StuckPlayerException(StuckPlayerException.STUCK_BUFFERING_NO_PROGRESS, 60_000),
|
|
535
|
+
PlaybackException.ERROR_CODE_TIMEOUT,
|
|
536
|
+
)
|
|
537
|
+
|
|
404
538
|
private fun advanceSeconds(seconds: Int) =
|
|
405
539
|
repeat(seconds) {
|
|
406
540
|
ShadowSystemClock.advanceBy(Duration.ofSeconds(1))
|
|
@@ -417,8 +551,12 @@ class PlayerRetryTest {
|
|
|
417
551
|
}
|
|
418
552
|
}
|
|
419
553
|
|
|
420
|
-
private fun play(
|
|
421
|
-
|
|
554
|
+
private fun play(
|
|
555
|
+
vararg srcs: String,
|
|
556
|
+
retryPolicy: RetryPolicy = RetryPolicy.Default,
|
|
557
|
+
wakeMode: AndroidPlayerWakeMode = AndroidPlayerWakeMode.NONE,
|
|
558
|
+
) {
|
|
559
|
+
player.setup(PlayerSetupOptions(retryPolicy = retryPolicy, wakeMode = wakeMode))
|
|
422
560
|
player.setCallbacks(callbacks)
|
|
423
561
|
player.exoPlayer.trackSelectionParameters =
|
|
424
562
|
player.exoPlayer.trackSelectionParameters
|
|
@@ -434,10 +572,9 @@ class PlayerRetryTest {
|
|
|
434
572
|
player.play()
|
|
435
573
|
}
|
|
436
574
|
|
|
437
|
-
private fun capabilities(
|
|
575
|
+
private fun capabilities(internet: Boolean) =
|
|
438
576
|
ShadowNetworkCapabilities.newInstance().also {
|
|
439
|
-
shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
440
|
-
if (validated) shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
577
|
+
if (internet) shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
441
578
|
}
|
|
442
579
|
|
|
443
580
|
private fun idleUntil(what: String, condition: () -> Boolean) {
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.media.AudioManager
|
|
4
|
+
import org.junit.Assert.assertEquals
|
|
5
|
+
import org.junit.Assert.assertFalse
|
|
6
|
+
import org.junit.Assert.assertTrue
|
|
7
|
+
import org.junit.Test
|
|
8
|
+
|
|
9
|
+
class RetryFocusTest {
|
|
10
|
+
private val log = mutableListOf<String>()
|
|
11
|
+
private var answer = AudioManager.AUDIOFOCUS_REQUEST_GRANTED
|
|
12
|
+
private var listener: AudioManager.OnAudioFocusChangeListener? = null
|
|
13
|
+
private var losses = 0
|
|
14
|
+
|
|
15
|
+
private val focus: RetryFocus =
|
|
16
|
+
RetryFocus(
|
|
17
|
+
object : RetryFocus.Port {
|
|
18
|
+
override fun request(listener: AudioManager.OnAudioFocusChangeListener): Int {
|
|
19
|
+
log += "request"
|
|
20
|
+
this@RetryFocusTest.listener = listener
|
|
21
|
+
return answer
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override fun abandon() {
|
|
25
|
+
log += "abandon"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
onLoss = { losses++ },
|
|
29
|
+
onSuppressionChanged = { log += "suppressed=${focus.suppressed}" },
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
@Test
|
|
33
|
+
fun `the reconnect's prepare requests the engine's focus before this one is let go`() {
|
|
34
|
+
focus.hold()
|
|
35
|
+
val held = listener!!
|
|
36
|
+
|
|
37
|
+
focus.handOver { log += "prepare" }
|
|
38
|
+
// ExoPlayer's request in the prepare takes the focus from this one's listener.
|
|
39
|
+
held.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS)
|
|
40
|
+
|
|
41
|
+
assertEquals(listOf("request", "prepare", "abandon"), log)
|
|
42
|
+
assertEquals("the engine's own request read as a loss", 0, losses)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@Test
|
|
46
|
+
fun `another app taking the focus during the wait is a loss`() {
|
|
47
|
+
focus.hold()
|
|
48
|
+
|
|
49
|
+
listener!!.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS)
|
|
50
|
+
|
|
51
|
+
assertEquals(1, losses)
|
|
52
|
+
assertEquals(listOf("request", "abandon"), log)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@Test
|
|
56
|
+
fun `a transient loss suppresses until the focus returns`() {
|
|
57
|
+
focus.hold()
|
|
58
|
+
|
|
59
|
+
listener!!.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
60
|
+
assertTrue(focus.suppressed)
|
|
61
|
+
listener!!.onAudioFocusChange(AudioManager.AUDIOFOCUS_GAIN)
|
|
62
|
+
|
|
63
|
+
assertFalse(focus.suppressed)
|
|
64
|
+
assertEquals(0, losses)
|
|
65
|
+
assertEquals(listOf("request", "suppressed=true", "suppressed=false"), log)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Test
|
|
69
|
+
fun `a request that waits, as during a call, suppresses until it is granted`() {
|
|
70
|
+
answer = AudioManager.AUDIOFOCUS_REQUEST_DELAYED
|
|
71
|
+
focus.hold()
|
|
72
|
+
assertTrue(focus.suppressed)
|
|
73
|
+
|
|
74
|
+
listener!!.onAudioFocusChange(AudioManager.AUDIOFOCUS_GAIN)
|
|
75
|
+
|
|
76
|
+
assertFalse(focus.suppressed)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Test
|
|
80
|
+
fun `a refused request is a loss`() {
|
|
81
|
+
answer = AudioManager.AUDIOFOCUS_REQUEST_FAILED
|
|
82
|
+
|
|
83
|
+
focus.hold()
|
|
84
|
+
|
|
85
|
+
assertEquals(1, losses)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@Test
|
|
89
|
+
fun `a ducking loss changes nothing, as no audio plays`() {
|
|
90
|
+
focus.hold()
|
|
91
|
+
|
|
92
|
+
listener!!.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
|
|
93
|
+
|
|
94
|
+
assertFalse(focus.suppressed)
|
|
95
|
+
assertEquals(0, losses)
|
|
96
|
+
}
|
|
97
|
+
}
|