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,25 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.net.ConnectivityManager
|
|
5
|
+
import android.net.NetworkCapabilities
|
|
6
|
+
import org.robolectric.Shadows.shadowOf
|
|
7
|
+
import org.robolectric.shadows.ShadowNetwork
|
|
8
|
+
import org.robolectric.shadows.ShadowNetworkCapabilities
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Brings Robolectric's network up, with internet, for every connectivity monitor already
|
|
12
|
+
* registered. Robolectric starts offline, where the retry loop waits for the network instead of
|
|
13
|
+
* failing.
|
|
14
|
+
*/
|
|
15
|
+
fun goOnline(context: Context) {
|
|
16
|
+
val connectivity =
|
|
17
|
+
shadowOf(context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
|
|
18
|
+
val wifi = ShadowNetwork.newInstance(103)
|
|
19
|
+
val capabilities =
|
|
20
|
+
ShadowNetworkCapabilities.newInstance().also {
|
|
21
|
+
shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
22
|
+
}
|
|
23
|
+
connectivity.setNetworkCapabilities(wifi, capabilities)
|
|
24
|
+
connectivity.networkCallbacks.forEach { it.onAvailable(wifi) }
|
|
25
|
+
}
|
|
@@ -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,28 +1,36 @@
|
|
|
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
|
|
7
8
|
import android.os.Looper
|
|
8
9
|
import androidx.media3.common.C
|
|
10
|
+
import androidx.media3.common.Metadata
|
|
9
11
|
import androidx.media3.common.PlaybackException
|
|
10
12
|
import androidx.media3.common.Player as MediaPlayer
|
|
11
13
|
import androidx.media3.common.util.StuckPlayerException
|
|
14
|
+
import androidx.media3.extractor.metadata.icy.IcyInfo
|
|
12
15
|
import com.audiobrowser.Callbacks
|
|
13
16
|
import com.audiobrowser.TestFixtures
|
|
17
|
+
import com.audiobrowser.goOnline
|
|
14
18
|
import com.audiobrowser.model.PlayerSetupOptions
|
|
15
19
|
import com.audiobrowser.model.RetryPolicy
|
|
16
20
|
import com.audiobrowser.util.TrackFactory
|
|
21
|
+
import com.margelo.nitro.audiobrowser.AndroidPlayerWakeMode
|
|
17
22
|
import com.margelo.nitro.audiobrowser.Playback
|
|
18
23
|
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
19
24
|
import java.io.File
|
|
25
|
+
import java.io.IOException
|
|
20
26
|
import java.net.ServerSocket
|
|
21
27
|
import java.nio.ByteBuffer
|
|
22
28
|
import java.nio.ByteOrder
|
|
23
29
|
import java.time.Duration
|
|
30
|
+
import java.util.concurrent.CopyOnWriteArrayList
|
|
24
31
|
import java.util.concurrent.CountDownLatch
|
|
25
32
|
import java.util.concurrent.TimeUnit
|
|
33
|
+
import java.util.concurrent.atomic.AtomicInteger
|
|
26
34
|
import kotlin.concurrent.thread
|
|
27
35
|
import kotlinx.coroutines.CoroutineScope
|
|
28
36
|
import kotlinx.coroutines.Dispatchers
|
|
@@ -31,6 +39,7 @@ import org.junit.After
|
|
|
31
39
|
import org.junit.Assert.assertEquals
|
|
32
40
|
import org.junit.Assert.assertFalse
|
|
33
41
|
import org.junit.Assert.assertNotEquals
|
|
42
|
+
import org.junit.Assert.assertNotSame
|
|
34
43
|
import org.junit.Assert.assertTrue
|
|
35
44
|
import org.junit.Assert.fail
|
|
36
45
|
import org.junit.Rule
|
|
@@ -64,26 +73,16 @@ class PlayerRetryTest {
|
|
|
64
73
|
private val scope = CoroutineScope(Dispatchers.Unconfined)
|
|
65
74
|
private val callbacks = mock(Callbacks::class.java)
|
|
66
75
|
|
|
76
|
+
private val servers = mutableListOf<ServerSocket>()
|
|
77
|
+
|
|
67
78
|
@After
|
|
68
79
|
fun release() {
|
|
80
|
+
servers.forEach { it.close() }
|
|
69
81
|
scope.cancel()
|
|
70
82
|
player.networkMonitor.destroy()
|
|
71
83
|
player.exoPlayer.release()
|
|
72
84
|
}
|
|
73
85
|
|
|
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
86
|
@Test
|
|
88
87
|
fun `a terminal error ends the load`() {
|
|
89
88
|
play(Uri.fromFile(silentWav()).toString())
|
|
@@ -94,7 +93,6 @@ class PlayerRetryTest {
|
|
|
94
93
|
)
|
|
95
94
|
|
|
96
95
|
assertEquals(PlaybackState.ERROR, player.playbackState)
|
|
97
|
-
assertFalse(player.hasPlayedCache)
|
|
98
96
|
}
|
|
99
97
|
|
|
100
98
|
@Test
|
|
@@ -329,7 +327,7 @@ class PlayerRetryTest {
|
|
|
329
327
|
|
|
330
328
|
@Test
|
|
331
329
|
fun `a player that gave up stays stopped when the network returns`() {
|
|
332
|
-
connectivity.setNetworkCapabilities(wifi, capabilities(
|
|
330
|
+
connectivity.setNetworkCapabilities(wifi, capabilities(internet = false))
|
|
333
331
|
connectivity.networkCallbacks.forEach { it.onAvailable(wifi) }
|
|
334
332
|
// A closed port: the load fails at once, the retry is pending, and the next failure is
|
|
335
333
|
// terminal.
|
|
@@ -348,9 +346,9 @@ class PlayerRetryTest {
|
|
|
348
346
|
},
|
|
349
347
|
)
|
|
350
348
|
|
|
351
|
-
connectivity.setNetworkCapabilities(wifi, capabilities(
|
|
349
|
+
connectivity.setNetworkCapabilities(wifi, capabilities(internet = true))
|
|
352
350
|
connectivity.networkCallbacks.forEach {
|
|
353
|
-
it.onCapabilitiesChanged(wifi, capabilities(
|
|
351
|
+
it.onCapabilitiesChanged(wifi, capabilities(internet = true))
|
|
354
352
|
}
|
|
355
353
|
shadowOf(Looper.getMainLooper()).idle()
|
|
356
354
|
|
|
@@ -359,48 +357,201 @@ class PlayerRetryTest {
|
|
|
359
357
|
}
|
|
360
358
|
|
|
361
359
|
@Test
|
|
362
|
-
fun `
|
|
363
|
-
|
|
360
|
+
fun `the wake mode's lock is held while the loop keeps the idle engine, and let go on pause`() {
|
|
361
|
+
goOnline(context)
|
|
362
|
+
play(
|
|
363
|
+
"https://127.0.0.1:9/stream",
|
|
364
|
+
retryPolicy = RetryPolicy.Infinite(),
|
|
365
|
+
wakeMode = AndroidPlayerWakeMode.NETWORK,
|
|
366
|
+
)
|
|
367
|
+
idleUntil("the loop holding the idle engine") {
|
|
368
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
369
|
+
player.retryLoop!!.reconnecting && player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE
|
|
370
|
+
}
|
|
371
|
+
assertTrue("no wake lock while held", player.retryLocks!!.wakeLockHeld)
|
|
364
372
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
373
|
+
player.pause()
|
|
374
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
375
|
+
|
|
376
|
+
assertFalse("a wake lock over a pause", player.retryLocks!!.wakeLockHeld)
|
|
368
377
|
}
|
|
369
378
|
|
|
370
379
|
@Test
|
|
371
|
-
fun `
|
|
372
|
-
|
|
380
|
+
fun `a track change on a stopped engine starts no Load until it is prepared`() {
|
|
381
|
+
goOnline(context)
|
|
382
|
+
val wav = Uri.fromFile(silentWav()).toString()
|
|
383
|
+
play(wav, wav, retryPolicy = RetryPolicy.Infinite())
|
|
384
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
385
|
+
player.exoPlayer.stop()
|
|
373
386
|
|
|
374
|
-
|
|
387
|
+
player.exoPlayer.seekToNextMediaItem()
|
|
388
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
389
|
+
|
|
390
|
+
assertTrue("a deadline runs for a track nothing connects", player.retryLoop!!.stopped)
|
|
375
391
|
}
|
|
376
392
|
|
|
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)
|
|
393
|
+
@Test
|
|
394
|
+
fun `another app taking the focus during a reconnect wait pauses, and nothing takes it back`() {
|
|
395
|
+
val connections = holdIdleEngine()
|
|
393
396
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
397
|
+
focusListener().onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS)
|
|
398
|
+
val after = connections.get()
|
|
399
|
+
advanceSeconds(10)
|
|
400
|
+
|
|
401
|
+
assertFalse(player.playWhenReady)
|
|
402
|
+
assertEquals("a reconnect took the focus back", after, connections.get())
|
|
403
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
@Test
|
|
407
|
+
fun `a transient focus loss during a reconnect wait holds the reconnect until the focus returns`() {
|
|
408
|
+
val connections = holdIdleEngine()
|
|
409
|
+
|
|
410
|
+
focusListener().onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
411
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
412
|
+
val after = connections.get()
|
|
413
|
+
advanceSeconds(10)
|
|
414
|
+
assertEquals("a reconnect during the loss", after, connections.get())
|
|
415
|
+
assertTrue(player.playWhenReady)
|
|
416
|
+
|
|
417
|
+
focusListener().onAudioFocusChange(AudioManager.AUDIOFOCUS_GAIN)
|
|
418
|
+
idleUntil("a reconnect once the focus returned") {
|
|
419
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
420
|
+
connections.get() > after
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Plays from a server that ends each connection unanswered, until the loop holds the engine. */
|
|
425
|
+
private fun holdIdleEngine(): AtomicInteger {
|
|
426
|
+
goOnline(context)
|
|
427
|
+
val server = ServerSocket(0)
|
|
428
|
+
servers += server
|
|
429
|
+
val connections = AtomicInteger()
|
|
430
|
+
thread(isDaemon = true) {
|
|
431
|
+
while (!server.isClosed) {
|
|
432
|
+
try {
|
|
433
|
+
server.accept().close()
|
|
434
|
+
connections.incrementAndGet()
|
|
435
|
+
} catch (_: IOException) {
|
|
436
|
+
return@thread
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
play("http://127.0.0.1:${server.localPort}/live", retryPolicy = RetryPolicy.Infinite())
|
|
441
|
+
idleUntil("the loop holding the engine") {
|
|
442
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
443
|
+
player.retryLoop!!.reconnecting && player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE
|
|
444
|
+
}
|
|
445
|
+
val audio = shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
|
|
446
|
+
assertNotSame(
|
|
447
|
+
"no focus held during the wait",
|
|
448
|
+
audio.lastAbandonedAudioFocusRequest,
|
|
449
|
+
audio.lastAudioFocusRequest.audioFocusRequest,
|
|
400
450
|
)
|
|
401
|
-
|
|
451
|
+
return connections
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** The listener of the last focus request: the retry loop's, while it holds the engine. */
|
|
455
|
+
private fun focusListener() =
|
|
456
|
+
shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
|
|
457
|
+
.lastAudioFocusRequest
|
|
458
|
+
.listener
|
|
459
|
+
|
|
460
|
+
@Test
|
|
461
|
+
fun `a skip while the loop holds the idle engine connects the next track`() {
|
|
462
|
+
goOnline(context)
|
|
463
|
+
val server = ServerSocket(0)
|
|
464
|
+
val paths = CopyOnWriteArrayList<String>()
|
|
465
|
+
// Reads each request line and ends the connection unanswered.
|
|
466
|
+
thread(isDaemon = true) {
|
|
467
|
+
while (!server.isClosed) {
|
|
468
|
+
try {
|
|
469
|
+
server.accept().use { paths += it.getInputStream().bufferedReader().readLine() }
|
|
470
|
+
} catch (_: IOException) {
|
|
471
|
+
return@thread
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
val base = "http://127.0.0.1:${server.localPort}"
|
|
476
|
+
play("$base/one", "$base/two", retryPolicy = RetryPolicy.Infinite())
|
|
477
|
+
idleUntil("the loop holding the engine") {
|
|
478
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
479
|
+
player.retryLoop!!.reconnecting && player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
player.forwardingPlayer.seekToNext()
|
|
483
|
+
|
|
484
|
+
idleUntil("a request for the next track") {
|
|
485
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
486
|
+
paths.any { it.contains("/two") }
|
|
487
|
+
}
|
|
488
|
+
server.close()
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
@Test
|
|
492
|
+
fun `a failure report that arrives after a reconnect is not the new connection's`() {
|
|
493
|
+
goOnline(context)
|
|
494
|
+
play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
|
|
495
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
496
|
+
|
|
497
|
+
// Posted from the playback thread before the go-live reconnect, run after it.
|
|
498
|
+
player.onLoadFailure(IOException("closed"), inPlace = false)
|
|
499
|
+
player.localEngine.reconnectAtLiveEdge()
|
|
500
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
501
|
+
|
|
502
|
+
assertFalse(player.retryLoop!!.failurePending)
|
|
503
|
+
assertEquals(null, player.playbackError)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
@Test
|
|
507
|
+
fun `with retry, a stuck engine reconnects and never ends in an error`() {
|
|
508
|
+
goOnline(context)
|
|
509
|
+
play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
|
|
510
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
511
|
+
|
|
512
|
+
// As media3 leaves a stuck engine: idle, with the error.
|
|
513
|
+
player.exoPlayer.stop()
|
|
514
|
+
player.playerListener.onPlayerError(stuck())
|
|
515
|
+
advanceSeconds(2)
|
|
516
|
+
|
|
517
|
+
assertNotEquals("no reconnect", MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
518
|
+
assertEquals(null, player.playbackError)
|
|
519
|
+
assertNotEquals(PlaybackState.ERROR, player.playbackState)
|
|
402
520
|
}
|
|
403
521
|
|
|
522
|
+
@Test
|
|
523
|
+
fun `a title after a reconnect inside the retry loop shows`() {
|
|
524
|
+
goOnline(context)
|
|
525
|
+
play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
|
|
526
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
527
|
+
player.exoPlayer.stop()
|
|
528
|
+
player.playerListener.onPlayerError(stuck())
|
|
529
|
+
advanceSeconds(2)
|
|
530
|
+
assertNotEquals("no reconnect", MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
531
|
+
|
|
532
|
+
player.playerListener.onMetadata(Metadata(IcyInfo(ByteArray(0), "Song", null)))
|
|
533
|
+
|
|
534
|
+
assertEquals("Song", player.nowPlaying.timedMetadata?.title)
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
@Test
|
|
538
|
+
fun `with retry off, a stuck engine ends in the stalled error`() {
|
|
539
|
+
play(Uri.fromFile(silentWav()).toString())
|
|
540
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
541
|
+
|
|
542
|
+
player.playerListener.onPlayerError(stuck())
|
|
543
|
+
|
|
544
|
+
assertEquals(PlaybackState.ERROR, player.playbackState)
|
|
545
|
+
assertEquals("playback-stalled", player.playbackError?.code)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
private fun stuck() =
|
|
549
|
+
PlaybackException(
|
|
550
|
+
"stuck",
|
|
551
|
+
StuckPlayerException(StuckPlayerException.STUCK_BUFFERING_NO_PROGRESS, 60_000),
|
|
552
|
+
PlaybackException.ERROR_CODE_TIMEOUT,
|
|
553
|
+
)
|
|
554
|
+
|
|
404
555
|
private fun advanceSeconds(seconds: Int) =
|
|
405
556
|
repeat(seconds) {
|
|
406
557
|
ShadowSystemClock.advanceBy(Duration.ofSeconds(1))
|
|
@@ -417,8 +568,12 @@ class PlayerRetryTest {
|
|
|
417
568
|
}
|
|
418
569
|
}
|
|
419
570
|
|
|
420
|
-
private fun play(
|
|
421
|
-
|
|
571
|
+
private fun play(
|
|
572
|
+
vararg srcs: String,
|
|
573
|
+
retryPolicy: RetryPolicy = RetryPolicy.Default,
|
|
574
|
+
wakeMode: AndroidPlayerWakeMode = AndroidPlayerWakeMode.NONE,
|
|
575
|
+
) {
|
|
576
|
+
player.setup(PlayerSetupOptions(retryPolicy = retryPolicy, wakeMode = wakeMode))
|
|
422
577
|
player.setCallbacks(callbacks)
|
|
423
578
|
player.exoPlayer.trackSelectionParameters =
|
|
424
579
|
player.exoPlayer.trackSelectionParameters
|
|
@@ -434,10 +589,9 @@ class PlayerRetryTest {
|
|
|
434
589
|
player.play()
|
|
435
590
|
}
|
|
436
591
|
|
|
437
|
-
private fun capabilities(
|
|
592
|
+
private fun capabilities(internet: Boolean) =
|
|
438
593
|
ShadowNetworkCapabilities.newInstance().also {
|
|
439
|
-
shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
440
|
-
if (validated) shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
594
|
+
if (internet) shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
441
595
|
}
|
|
442
596
|
|
|
443
597
|
private fun idleUntil(what: String, condition: () -> Boolean) {
|
|
@@ -182,6 +182,29 @@ class PlayerTrackChangeTest {
|
|
|
182
182
|
assertEquals(listOf("A's city", "A's city"), shownSecondLines())
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
+
@Test
|
|
186
|
+
fun `a title the stream had already posted does not survive a stop`() {
|
|
187
|
+
playStationWithTitle()
|
|
188
|
+
val heard = mutableListOf<TimedMetadata>()
|
|
189
|
+
player.callbacks =
|
|
190
|
+
mock(Callbacks::class.java) { invocation ->
|
|
191
|
+
if (invocation.method.name == "onTimedMetadata") heard.add(invocation.getArgument(0))
|
|
192
|
+
Answers.RETURNS_DEFAULTS.answer(invocation)
|
|
193
|
+
}
|
|
194
|
+
val playbackLooper = shadowOf(player.exoPlayer.playbackLooper)
|
|
195
|
+
playbackLooper.pause()
|
|
196
|
+
Handler(player.exoPlayer.playbackLooper).post {
|
|
197
|
+
Handler(Looper.getMainLooper()).post { player.playerListener.onMetadata(icy("A song 1")) }
|
|
198
|
+
}
|
|
199
|
+
player.stop()
|
|
200
|
+
playbackLooper.unPause()
|
|
201
|
+
idle()
|
|
202
|
+
|
|
203
|
+
assertEquals(emptyList<TimedMetadata>(), heard)
|
|
204
|
+
assertNull(player.nowPlaying.timedMetadata)
|
|
205
|
+
assertEquals(listOf("A's city", "A's city"), shownSecondLines())
|
|
206
|
+
}
|
|
207
|
+
|
|
185
208
|
@Test
|
|
186
209
|
fun `a play after a stop shows the track's own lines until the stream sends a title`() {
|
|
187
210
|
playStationWithTitle()
|
|
@@ -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
|
+
}
|