react-native-audio-browser 0.2.0-next.76 → 0.2.0-next.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +1 -1
- package/android/src/main/java/com/audiobrowser/player/EnginePlayback.kt +9 -5
- package/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +71 -39
- package/android/src/main/java/com/audiobrowser/player/LiveStreamExtractor.kt +65 -0
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +12 -3
- package/android/src/main/java/com/audiobrowser/player/NowPlayingUpdater.kt +5 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +78 -36
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +6 -0
- package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +1 -1
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +48 -2
- package/android/src/test/java/androidx/media3/session/SessionViews.kt +10 -0
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +372 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerSetupEndedTest.kt +221 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerSetupTest.kt +17 -7
- package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +183 -7
- package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +48 -0
- package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +236 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewRulesTest.kt +62 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +188 -0
- package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +23 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +74 -0
- package/ios/AVPlayerEngine.swift +5 -1
- package/ios/Browser/BrowserManager.swift +2 -2
- package/ios/Browser/PageResolver.swift +14 -19
- package/ios/Browser/RequestPipeline.swift +6 -4
- package/ios/CarPlay/CarPlayNowPlayingManager.swift +1 -1
- package/ios/CarPlay/CarPlaySession+PageLoader.swift +1 -0
- package/ios/CarPlay/CarPlaySession+TabBar.swift +2 -0
- package/ios/Extension/Promise+AwaitCallback.swift +33 -0
- package/ios/HybridAudioBrowser+Player.swift +1 -1
- package/ios/HybridAudioBrowser.swift +1 -1
- package/ios/Player/MediaLoader.swift +1 -1
- package/ios/Player/PlaybackFailure.swift +3 -3
- package/ios/Player/Player.swift +67 -49
- package/ios/Player/PlayerEngine.swift +2 -0
- package/ios/Player/RetryLoop.swift +72 -12
- package/ios/Tests/AwaitCallbackTests.swift +210 -0
- package/ios/Tests/GenerationMemoTests.swift +57 -0
- package/ios/Tests/PlaybackFailureTests.swift +7 -0
- package/ios/Tests/PlayerPolicyTests.swift +346 -6
- package/ios/Tests/PlayerTests.swift +79 -1
- package/ios/Tests/RetryLoopTests.swift +30 -5
- package/ios/Tests/Support/TestTypes.swift +15 -7
- package/ios/Tests/Support/TimeLimit.swift +43 -0
- package/ios/Util/AwaitCallback.swift +80 -0
- package/ios/Util/GenerationMemo.swift +11 -4
- package/package.json +1 -1
- package/android/src/test/java/com/audiobrowser/player/SessionErrorMaskTest.kt +0 -63
- package/ios/Player/EndOfTrackJudgement.swift +0 -25
- package/ios/Tests/EndOfTrackJudgementTests.swift +0 -30
|
@@ -4,6 +4,7 @@ import android.os.Looper
|
|
|
4
4
|
import androidx.media3.common.C
|
|
5
5
|
import androidx.media3.common.ForwardingSimpleBasePlayer
|
|
6
6
|
import androidx.media3.common.MediaItem
|
|
7
|
+
import androidx.media3.common.Metadata
|
|
7
8
|
import androidx.media3.common.PlaybackException
|
|
8
9
|
import androidx.media3.common.Player as MediaPlayer
|
|
9
10
|
import androidx.media3.common.SimpleBasePlayer
|
|
@@ -69,6 +70,9 @@ internal class RemoteSwitchingPlayer(
|
|
|
69
70
|
|
|
70
71
|
/** Whether the receiver reported the current item playing since the start timeout armed. */
|
|
71
72
|
var reportedPlaying = false
|
|
73
|
+
|
|
74
|
+
/** The receiver's last timed metadata for the current item; none until it holds that item. */
|
|
75
|
+
var timedMetadata = NO_TIMED_METADATA
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
|
|
@@ -131,8 +135,34 @@ internal class RemoteSwitchingPlayer(
|
|
|
131
135
|
private val remoteRoute: Route.Remote?
|
|
132
136
|
get() = route as? Route.Remote
|
|
133
137
|
|
|
138
|
+
/** For the next [getState] to take [titleAtStop]: super.getState() consumes pending events. */
|
|
139
|
+
private var localStopPending = false
|
|
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
|
|
154
|
+
return state
|
|
155
|
+
}
|
|
156
|
+
return state.buildUpon().setTimedMetadata(NO_TIMED_METADATA).build()
|
|
157
|
+
}
|
|
158
|
+
|
|
134
159
|
private val receiverListener =
|
|
135
160
|
object : MediaPlayer.Listener {
|
|
161
|
+
override fun onMetadata(metadata: Metadata) {
|
|
162
|
+
val remote = remoteRoute ?: return
|
|
163
|
+
if (holdsCurrent(remote)) remote.timedMetadata = metadata
|
|
164
|
+
}
|
|
165
|
+
|
|
136
166
|
override fun onEvents(player: MediaPlayer, events: MediaPlayer.Events) {
|
|
137
167
|
val remote = remoteRoute ?: return
|
|
138
168
|
if (player !== remote.receiver) return
|
|
@@ -301,6 +331,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
301
331
|
|
|
302
332
|
private fun load(remote: Route.Remote, positionMs: Long) {
|
|
303
333
|
cancelLoad(remote)
|
|
334
|
+
remote.timedMetadata = NO_TIMED_METADATA
|
|
304
335
|
val item = queue.current
|
|
305
336
|
if (item == null) {
|
|
306
337
|
remote.load = Load.Idle(C.TIME_UNSET)
|
|
@@ -421,9 +452,17 @@ internal class RemoteSwitchingPlayer(
|
|
|
421
452
|
private fun receiverHolds(remote: Route.Remote, item: MediaItem) =
|
|
422
453
|
remote.receiver.currentMediaItem === item
|
|
423
454
|
|
|
455
|
+
/** Counts [Load.Sent] too: a receiver's onMetadata comes before onEvents moves it to Playing. */
|
|
456
|
+
private fun holdsCurrent(remote: Route.Remote): Boolean =
|
|
457
|
+
when (val load = remote.load) {
|
|
458
|
+
is Load.Sent -> receiverHolds(remote, load.item)
|
|
459
|
+
is Load.Playing -> receiverHolds(remote, load.item)
|
|
460
|
+
else -> false
|
|
461
|
+
}
|
|
462
|
+
|
|
424
463
|
override fun getState(): State {
|
|
425
464
|
val state = super.getState()
|
|
426
|
-
val remote = remoteRoute ?: return state
|
|
465
|
+
val remote = remoteRoute ?: return withoutStoppedTitle(state)
|
|
427
466
|
val load = remote.load
|
|
428
467
|
val holds = load is Load.Playing && receiverHolds(remote, load.item)
|
|
429
468
|
// Duration, seekability and liveness come from the receiver's data for the item it plays.
|
|
@@ -445,6 +484,7 @@ internal class RemoteSwitchingPlayer(
|
|
|
445
484
|
.setVolume(if (maxVolume > 0) state.deviceVolume.toFloat() / maxVolume else 0f)
|
|
446
485
|
.setAvailableCommands(state.availableCommands.buildUpon().addAll(*QUEUE_COMMANDS).build())
|
|
447
486
|
.setPlayerError(null)
|
|
487
|
+
.setTimedMetadata(remote.timedMetadata)
|
|
448
488
|
.apply {
|
|
449
489
|
if (!holds) setPlayWhenReady(playIntent, PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST)
|
|
450
490
|
when (load) {
|
|
@@ -522,7 +562,11 @@ internal class RemoteSwitchingPlayer(
|
|
|
522
562
|
}
|
|
523
563
|
|
|
524
564
|
override fun handleStop(): ListenableFuture<*> {
|
|
525
|
-
val remote = remoteRoute
|
|
565
|
+
val remote = remoteRoute
|
|
566
|
+
if (remote == null) {
|
|
567
|
+
localStopPending = true
|
|
568
|
+
return super.handleStop()
|
|
569
|
+
}
|
|
526
570
|
val positionMs = positionOn(remote)
|
|
527
571
|
cancelLoad(remote)
|
|
528
572
|
stopReceiver(remote.receiver)
|
|
@@ -658,6 +702,8 @@ internal class RemoteSwitchingPlayer(
|
|
|
658
702
|
private fun done() = Futures.immediateVoidFuture()
|
|
659
703
|
|
|
660
704
|
private companion object {
|
|
705
|
+
val NO_TIMED_METADATA = Metadata(C.TIME_UNSET)
|
|
706
|
+
|
|
661
707
|
// Served from the queue this side keeps, whatever the receiver offers.
|
|
662
708
|
val QUEUE_COMMANDS =
|
|
663
709
|
intArrayOf(
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
package androidx.media3.session
|
|
2
|
+
|
|
3
|
+
// Read in place: Robolectric can't connect a controller to the session.
|
|
4
|
+
|
|
5
|
+
/** The state legacy controllers (Android Auto, Bluetooth, the system UI) were last told. */
|
|
6
|
+
internal fun MediaSession.legacyState() =
|
|
7
|
+
impl.mediaSessionLegacyStub.sessionCompat.controller.playbackState!!.state
|
|
8
|
+
|
|
9
|
+
/** The current title media3 controllers were last told. */
|
|
10
|
+
internal fun MediaSession.controllerTitle() = impl.playerInfo.mediaMetadata.title?.toString()
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.os.Looper
|
|
4
|
+
import androidx.media3.common.C
|
|
5
|
+
import androidx.media3.common.ParserException
|
|
6
|
+
import androidx.media3.decoder.DecoderInputBuffer
|
|
7
|
+
import androidx.media3.exoplayer.FormatHolder
|
|
8
|
+
import androidx.media3.exoplayer.LoadingInfo
|
|
9
|
+
import androidx.media3.exoplayer.analytics.PlayerId
|
|
10
|
+
import androidx.media3.exoplayer.source.MediaPeriod
|
|
11
|
+
import androidx.media3.exoplayer.source.MediaSource
|
|
12
|
+
import androidx.media3.exoplayer.source.SampleStream
|
|
13
|
+
import androidx.media3.exoplayer.trackselection.ExoTrackSelection
|
|
14
|
+
import androidx.media3.exoplayer.trackselection.FixedTrackSelection
|
|
15
|
+
import androidx.media3.exoplayer.upstream.DefaultAllocator
|
|
16
|
+
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter
|
|
17
|
+
import com.audiobrowser.TestFixtures.track
|
|
18
|
+
import com.audiobrowser.model.RetryPolicy
|
|
19
|
+
import com.audiobrowser.util.TrackFactory
|
|
20
|
+
import java.io.IOException
|
|
21
|
+
import java.net.ServerSocket
|
|
22
|
+
import java.nio.ByteBuffer
|
|
23
|
+
import java.nio.ByteOrder
|
|
24
|
+
import java.time.Duration
|
|
25
|
+
import java.util.concurrent.atomic.AtomicInteger
|
|
26
|
+
import kotlin.concurrent.thread
|
|
27
|
+
import org.junit.After
|
|
28
|
+
import org.junit.Assert.assertEquals
|
|
29
|
+
import org.junit.Assert.assertNotNull
|
|
30
|
+
import org.junit.Assert.assertNull
|
|
31
|
+
import org.junit.Assert.assertTrue
|
|
32
|
+
import org.junit.Assert.fail
|
|
33
|
+
import org.junit.Test
|
|
34
|
+
import org.junit.runner.RunWith
|
|
35
|
+
import org.robolectric.RobolectricTestRunner
|
|
36
|
+
import org.robolectric.RuntimeEnvironment
|
|
37
|
+
import org.robolectric.Shadows.shadowOf
|
|
38
|
+
import org.robolectric.shadows.ShadowSystemClock
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A progressive stream that the server ends cleanly, served without a length, loaded through
|
|
42
|
+
* [MediaFactory] by media3's own media period. The test reads the samples as a renderer would and
|
|
43
|
+
* advances the clock by the audio it reads, since Robolectric plays no audio.
|
|
44
|
+
*/
|
|
45
|
+
@RunWith(RobolectricTestRunner::class)
|
|
46
|
+
class LiveStreamEndTest {
|
|
47
|
+
|
|
48
|
+
private val context = RuntimeEnvironment.getApplication()
|
|
49
|
+
private var server = ServerSocket(0)
|
|
50
|
+
private val requests = AtomicInteger()
|
|
51
|
+
private val played = AtomicInteger()
|
|
52
|
+
|
|
53
|
+
@After
|
|
54
|
+
fun shutdown() {
|
|
55
|
+
server.close()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Test
|
|
59
|
+
fun `a live stream that closes before it has played reconnects`() {
|
|
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`() {
|
|
87
|
+
serve(framesFor = { SECONDS_1_5 })
|
|
88
|
+
|
|
89
|
+
val outcome = play(live = true, RetryPolicy.Infinite())
|
|
90
|
+
|
|
91
|
+
assertNotNull("the retries never ran out", outcome.error)
|
|
92
|
+
// The 120 s recovery budget starts at the first close, at 1.49 s: closes come every 1.49 s,
|
|
93
|
+
// and the first at or past 120 s after it, the 82nd, is not retried.
|
|
94
|
+
assertEquals(82, requests.get())
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@Test
|
|
98
|
+
fun `an on-demand file served without a length ends where the server ends it`() {
|
|
99
|
+
serve(framesFor = { SECONDS_1_5 })
|
|
100
|
+
|
|
101
|
+
val outcome = play(live = false, RetryPolicy.Infinite())
|
|
102
|
+
|
|
103
|
+
assertNull(outcome.error)
|
|
104
|
+
assertTrue("the file ended", outcome.ended)
|
|
105
|
+
assertEquals(1, requests.get())
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@Test
|
|
109
|
+
fun `a live stream served with a length ends where its length ends`() {
|
|
110
|
+
// ADTS, not MP3: the MP3 extractor stops at the end of a known length before the source ends.
|
|
111
|
+
serve(
|
|
112
|
+
framesFor = { SECONDS_1_5 },
|
|
113
|
+
contentType = "audio/aac",
|
|
114
|
+
withLength = true,
|
|
115
|
+
body = ::silentAdts,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
val outcome = play(live = true, RetryPolicy.Infinite())
|
|
119
|
+
|
|
120
|
+
assertNull(outcome.error)
|
|
121
|
+
assertTrue("the stream ended", outcome.ended)
|
|
122
|
+
assertEquals(1, requests.get())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@Test
|
|
126
|
+
fun `a live stream whose container declares its end ends there`() {
|
|
127
|
+
serve(framesFor = { 0 }, contentType = "audio/wav", body = { silentWav() })
|
|
128
|
+
|
|
129
|
+
val outcome = play(live = true, RetryPolicy.Infinite())
|
|
130
|
+
|
|
131
|
+
assertNull(outcome.error)
|
|
132
|
+
assertTrue("the stream ended", outcome.ended)
|
|
133
|
+
assertEquals(1, requests.get())
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@Test
|
|
137
|
+
fun `a live station answering with an empty body fails at once as unplayable`() =
|
|
138
|
+
assertFailsAtOnce("audio/mpeg", "")
|
|
139
|
+
|
|
140
|
+
@Test
|
|
141
|
+
fun `a live station answering with a few bytes fails at once as unplayable`() =
|
|
142
|
+
assertFailsAtOnce("audio/mpeg", "ICY 404")
|
|
143
|
+
|
|
144
|
+
@Test
|
|
145
|
+
fun `a live station answering with a playlist fails at once as unplayable`() {
|
|
146
|
+
assertFailsAtOnce("audio/x-mpegurl", "#EXTM3U\nhttp://127.0.0.1:9/live.mp3\n")
|
|
147
|
+
assertFailsAtOnce("audio/x-scpls", "[playlist]\nFile1=http://127.0.0.1:9/live.mp3\n")
|
|
148
|
+
assertFailsAtOnce("application/pls+xml", "[playlist]\nFile1=http://127.0.0.1:9/live.mp3\n")
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@Test
|
|
152
|
+
fun `a live station answering with a page fails at once as unplayable`() {
|
|
153
|
+
assertFailsAtOnce("text/html", "<html>Stream offline</html>")
|
|
154
|
+
assertFailsAtOnce("application/octet-stream", "<html>Stream offline</html>")
|
|
155
|
+
assertFailsAtOnce(null, "<html>Stream offline</html>")
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A live track whose one reply, [body] as [contentType], ends in the unrecognised format error.
|
|
160
|
+
*/
|
|
161
|
+
private fun assertFailsAtOnce(contentType: String?, body: String) {
|
|
162
|
+
requests.set(0)
|
|
163
|
+
server.close()
|
|
164
|
+
server = ServerSocket(0)
|
|
165
|
+
serve(framesFor = { 0 }, contentType = contentType, body = { body.toByteArray() })
|
|
166
|
+
|
|
167
|
+
val outcome = play(live = true, RetryPolicy.Infinite())
|
|
168
|
+
|
|
169
|
+
assertTrue(
|
|
170
|
+
"$contentType: not a parse error: ${outcome.error}",
|
|
171
|
+
outcome.error is ParserException,
|
|
172
|
+
)
|
|
173
|
+
assertEquals(contentType, 1, requests.get())
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Answers the [n]th request, from 1, with the [body] of [framesFor]`(n)` frames as [contentType],
|
|
178
|
+
* silence by default and [withLength] or not, then ends it cleanly once they have all been read:
|
|
179
|
+
* a server sending in real time ends after its audio, never with it.
|
|
180
|
+
*/
|
|
181
|
+
private fun serve(
|
|
182
|
+
framesFor: (n: Int) -> Int,
|
|
183
|
+
contentType: String? = "audio/mpeg",
|
|
184
|
+
withLength: Boolean = false,
|
|
185
|
+
body: (frames: Int) -> ByteArray = ::silentMp3,
|
|
186
|
+
) {
|
|
187
|
+
val server = server
|
|
188
|
+
thread(isDaemon = true) {
|
|
189
|
+
var sent = 0
|
|
190
|
+
while (!server.isClosed) {
|
|
191
|
+
val socket =
|
|
192
|
+
try {
|
|
193
|
+
server.accept()
|
|
194
|
+
} catch (_: IOException) {
|
|
195
|
+
return@thread
|
|
196
|
+
}
|
|
197
|
+
socket.use {
|
|
198
|
+
val request = it.getInputStream().bufferedReader()
|
|
199
|
+
while (!request.readLine().isNullOrEmpty()) Unit
|
|
200
|
+
val frames = framesFor(requests.incrementAndGet())
|
|
201
|
+
val bytes = body(frames)
|
|
202
|
+
val headers = buildString {
|
|
203
|
+
append("HTTP/1.1 200 OK\r\nConnection: close\r\n")
|
|
204
|
+
contentType?.let { type -> append("Content-Type: $type\r\n") }
|
|
205
|
+
if (withLength) append("Content-Length: ${bytes.size}\r\n")
|
|
206
|
+
append("\r\n")
|
|
207
|
+
}
|
|
208
|
+
it.getOutputStream().run {
|
|
209
|
+
write(headers.toByteArray())
|
|
210
|
+
write(bytes)
|
|
211
|
+
flush()
|
|
212
|
+
}
|
|
213
|
+
sent += frames
|
|
214
|
+
while (played.get() < sent && !server.isClosed) Thread.sleep(1)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private class Outcome(val ended: Boolean, val error: IOException?)
|
|
221
|
+
|
|
222
|
+
private fun play(
|
|
223
|
+
live: Boolean,
|
|
224
|
+
retryPolicy: RetryPolicy,
|
|
225
|
+
stopAfterFrames: Int = Int.MAX_VALUE,
|
|
226
|
+
hasPlayed: Boolean = true,
|
|
227
|
+
): Outcome {
|
|
228
|
+
val factory =
|
|
229
|
+
MediaFactory(
|
|
230
|
+
context,
|
|
231
|
+
cache = null,
|
|
232
|
+
retryPolicy,
|
|
233
|
+
hasPlayed = { hasPlayed },
|
|
234
|
+
getRequestConfig = { _, _ -> null },
|
|
235
|
+
)
|
|
236
|
+
val item =
|
|
237
|
+
TrackFactory.toMedia3(
|
|
238
|
+
track(src = "http://127.0.0.1:${server.localPort}/stream.mp3")
|
|
239
|
+
.copy(live = live.takeIf { it })
|
|
240
|
+
)
|
|
241
|
+
val source = factory.createMediaSource(item)
|
|
242
|
+
source.prepareSource(
|
|
243
|
+
{ _, _ -> },
|
|
244
|
+
PlayerId.UNSET,
|
|
245
|
+
DefaultBandwidthMeter.Builder(context).build(),
|
|
246
|
+
)
|
|
247
|
+
val period =
|
|
248
|
+
source.createPeriod(
|
|
249
|
+
MediaSource.MediaPeriodId(Any()),
|
|
250
|
+
DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE),
|
|
251
|
+
0,
|
|
252
|
+
)
|
|
253
|
+
var prepared = false
|
|
254
|
+
period.prepare(
|
|
255
|
+
object : MediaPeriod.Callback {
|
|
256
|
+
override fun onPrepared(mediaPeriod: MediaPeriod) {
|
|
257
|
+
prepared = true
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
override fun onContinueLoadingRequested(source: MediaPeriod) {
|
|
261
|
+
source.continueLoading(LoadingInfo.Builder().build())
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
0,
|
|
265
|
+
)
|
|
266
|
+
try {
|
|
267
|
+
val prepareDeadline = System.currentTimeMillis() + 5_000
|
|
268
|
+
while (!prepared) {
|
|
269
|
+
if (System.currentTimeMillis() > prepareDeadline) fail("the stream never prepared")
|
|
270
|
+
try {
|
|
271
|
+
period.maybeThrowPrepareError()
|
|
272
|
+
} catch (e: IOException) {
|
|
273
|
+
return Outcome(ended = false, error = e)
|
|
274
|
+
}
|
|
275
|
+
Thread.sleep(5)
|
|
276
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
277
|
+
}
|
|
278
|
+
val streams = arrayOfNulls<SampleStream>(1)
|
|
279
|
+
period.selectTracks(
|
|
280
|
+
arrayOf<ExoTrackSelection>(FixedTrackSelection(period.trackGroups[0], 0)),
|
|
281
|
+
booleanArrayOf(false),
|
|
282
|
+
streams,
|
|
283
|
+
booleanArrayOf(false),
|
|
284
|
+
0,
|
|
285
|
+
)
|
|
286
|
+
val stream = checkNotNull(streams[0])
|
|
287
|
+
val buffer = DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DISABLED)
|
|
288
|
+
val deadline = System.currentTimeMillis() + 60_000
|
|
289
|
+
while (played.get() < stopAfterFrames) {
|
|
290
|
+
if (System.currentTimeMillis() > deadline)
|
|
291
|
+
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
|
+
buffer.clear()
|
|
298
|
+
val result = stream.readData(FormatHolder(), buffer, SampleStream.FLAG_OMIT_SAMPLE_DATA)
|
|
299
|
+
period.readDiscontinuity()
|
|
300
|
+
when (result) {
|
|
301
|
+
C.RESULT_BUFFER_READ -> {
|
|
302
|
+
if (buffer.isEndOfStream) return Outcome(ended = true, error = null)
|
|
303
|
+
played.incrementAndGet()
|
|
304
|
+
ShadowSystemClock.advanceBy(Duration.ofNanos(FRAME_NS))
|
|
305
|
+
}
|
|
306
|
+
C.RESULT_NOTHING_READ -> {
|
|
307
|
+
period.continueLoading(LoadingInfo.Builder().build())
|
|
308
|
+
Thread.sleep(1)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
312
|
+
}
|
|
313
|
+
return Outcome(ended = false, error = null)
|
|
314
|
+
} finally {
|
|
315
|
+
source.releasePeriod(period)
|
|
316
|
+
source.releaseSource { _, _ -> }
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private companion object {
|
|
321
|
+
// MPEG-1 Layer III at 32 kbit/s, 44.1 kHz, mono: 104 bytes and 1152 samples a frame.
|
|
322
|
+
val FRAME_HEADER = byteArrayOf(0xFF.toByte(), 0xFB.toByte(), 0x10, 0xC0.toByte())
|
|
323
|
+
const val FRAME_BYTES = 104
|
|
324
|
+
const val FRAME_NS = 1152 * 1_000_000_000L / 44_100
|
|
325
|
+
const val SECONDS_1_5 = 57
|
|
326
|
+
const val SECONDS_60 = 2_297
|
|
327
|
+
|
|
328
|
+
/** A WAV of a second of 8 kHz mono 8-bit silence, its data size in its header. */
|
|
329
|
+
fun silentWav(): ByteArray {
|
|
330
|
+
val samples = 8_000
|
|
331
|
+
val header =
|
|
332
|
+
ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
|
|
333
|
+
put("RIFF".toByteArray())
|
|
334
|
+
putInt(36 + samples)
|
|
335
|
+
put("WAVEfmt ".toByteArray())
|
|
336
|
+
putInt(16)
|
|
337
|
+
putShort(1)
|
|
338
|
+
putShort(1)
|
|
339
|
+
putInt(8_000)
|
|
340
|
+
putInt(8_000)
|
|
341
|
+
putShort(1)
|
|
342
|
+
putShort(8)
|
|
343
|
+
put("data".toByteArray())
|
|
344
|
+
putInt(samples)
|
|
345
|
+
}
|
|
346
|
+
return header.array() + ByteArray(samples) { 0x80.toByte() }
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** [frames] ADTS frames of AAC LC at 44.1 kHz, mono, with 100 zero bytes of payload each. */
|
|
350
|
+
fun silentAdts(frames: Int): ByteArray {
|
|
351
|
+
val length = 7 + 100
|
|
352
|
+
val header =
|
|
353
|
+
byteArrayOf(
|
|
354
|
+
0xFF.toByte(),
|
|
355
|
+
0xF1.toByte(),
|
|
356
|
+
0x50,
|
|
357
|
+
(0x40 or (length shr 11)).toByte(),
|
|
358
|
+
(length shr 3).toByte(),
|
|
359
|
+
((length and 7) shl 5 or 0x1F).toByte(),
|
|
360
|
+
0xFC.toByte(),
|
|
361
|
+
)
|
|
362
|
+
val frame = header + ByteArray(length - header.size)
|
|
363
|
+
return ByteArray(frames * length) { frame[it % length] }
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** [frames] frames whose side info and main data are zero: silence any decoder accepts. */
|
|
367
|
+
fun silentMp3(frames: Int): ByteArray {
|
|
368
|
+
val frame = FRAME_HEADER + ByteArray(FRAME_BYTES - FRAME_HEADER.size)
|
|
369
|
+
return ByteArray(frames * FRAME_BYTES) { frame[it % FRAME_BYTES] }
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.net.Uri
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import androidx.media3.common.C
|
|
6
|
+
import androidx.media3.common.Player as MediaPlayer
|
|
7
|
+
import androidx.media3.common.util.Util
|
|
8
|
+
import com.audiobrowser.Callbacks
|
|
9
|
+
import com.audiobrowser.TestFixtures
|
|
10
|
+
import com.audiobrowser.model.PlayerSetupOptions
|
|
11
|
+
import com.audiobrowser.util.TrackFactory
|
|
12
|
+
import com.margelo.nitro.audiobrowser.Playback
|
|
13
|
+
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
14
|
+
import java.io.File
|
|
15
|
+
import java.nio.ByteBuffer
|
|
16
|
+
import java.nio.ByteOrder
|
|
17
|
+
import java.time.Duration
|
|
18
|
+
import java.util.concurrent.TimeUnit
|
|
19
|
+
import org.junit.After
|
|
20
|
+
import org.junit.Assert.assertEquals
|
|
21
|
+
import org.junit.Assert.fail
|
|
22
|
+
import org.junit.Rule
|
|
23
|
+
import org.junit.Test
|
|
24
|
+
import org.junit.rules.TemporaryFolder
|
|
25
|
+
import org.junit.runner.RunWith
|
|
26
|
+
import org.mockito.Mockito.mock
|
|
27
|
+
import org.mockito.Mockito.mockingDetails
|
|
28
|
+
import org.robolectric.RobolectricTestRunner
|
|
29
|
+
import org.robolectric.RuntimeEnvironment
|
|
30
|
+
import org.robolectric.Shadows.shadowOf
|
|
31
|
+
import org.robolectric.annotation.Config
|
|
32
|
+
import org.robolectric.shadows.ShadowSystemClock
|
|
33
|
+
|
|
34
|
+
@RunWith(RobolectricTestRunner::class)
|
|
35
|
+
@Config(sdk = [28])
|
|
36
|
+
class PlayerSetupEndedTest {
|
|
37
|
+
|
|
38
|
+
@get:Rule val folder = TemporaryFolder()
|
|
39
|
+
|
|
40
|
+
private val context = RuntimeEnvironment.getApplication()
|
|
41
|
+
private val player = Player(context)
|
|
42
|
+
private val callbacks = mock(Callbacks::class.java)
|
|
43
|
+
|
|
44
|
+
@After fun release() = player.exoPlayer.release()
|
|
45
|
+
|
|
46
|
+
@Test
|
|
47
|
+
fun `a rebuild in ENDED stays ENDED and a later play restarts`() {
|
|
48
|
+
endAndRebuild()
|
|
49
|
+
|
|
50
|
+
// As JS setPlayWhenReady(true) does: the setter alone, without play()'s prepare().
|
|
51
|
+
player.playWhenReady = true
|
|
52
|
+
assertEquals(0L, player.exoPlayer.currentPosition)
|
|
53
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Test
|
|
57
|
+
fun `a session play after a rebuild in ENDED restarts`() {
|
|
58
|
+
endAndRebuild()
|
|
59
|
+
|
|
60
|
+
Util.handlePlayButtonAction(player.forwardingPlayer)
|
|
61
|
+
assertEquals(0L, player.exoPlayer.currentPosition)
|
|
62
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Test
|
|
66
|
+
fun `prepare after a rebuild in ENDED stays ended, as on an ended engine`() {
|
|
67
|
+
endAndRebuild()
|
|
68
|
+
|
|
69
|
+
player.prepare()
|
|
70
|
+
idleFor(1)
|
|
71
|
+
|
|
72
|
+
assertEquals(PlaybackState.ENDED, player.playbackState)
|
|
73
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Test
|
|
77
|
+
fun `next at the end after a rebuild in ENDED stays ended, as on an ended engine`() {
|
|
78
|
+
endAndRebuild()
|
|
79
|
+
|
|
80
|
+
player.next()
|
|
81
|
+
idleFor(1)
|
|
82
|
+
|
|
83
|
+
assertEquals(PlaybackState.ENDED, player.playbackState)
|
|
84
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@Test
|
|
88
|
+
fun `a skip back after a rebuild in ENDED loads, as on an ended engine`() {
|
|
89
|
+
endAndRebuild(tracks = 2)
|
|
90
|
+
|
|
91
|
+
player.previous()
|
|
92
|
+
idleFor(1)
|
|
93
|
+
|
|
94
|
+
assertEquals(0, player.exoPlayer.currentMediaItemIndex)
|
|
95
|
+
assertEquals(MediaPlayer.STATE_READY, player.exoPlayer.playbackState)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@Test
|
|
99
|
+
fun `a session skip back after a rebuild in ENDED loads, as on an ended engine`() {
|
|
100
|
+
endAndRebuild(tracks = 2)
|
|
101
|
+
|
|
102
|
+
player.forwardingPlayer.seekToPreviousMediaItem()
|
|
103
|
+
idleFor(1)
|
|
104
|
+
|
|
105
|
+
assertEquals(0, player.exoPlayer.currentMediaItemIndex)
|
|
106
|
+
assertEquals(MediaPlayer.STATE_READY, player.exoPlayer.playbackState)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@Test
|
|
110
|
+
fun `a play after a rebuild in ENDED keeps a seek`() {
|
|
111
|
+
endAndRebuild()
|
|
112
|
+
|
|
113
|
+
player.seekTo(200, TimeUnit.MILLISECONDS)
|
|
114
|
+
player.playWhenReady = true
|
|
115
|
+
|
|
116
|
+
assertEquals(200L, player.exoPlayer.currentPosition)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Test
|
|
120
|
+
fun `a session play after a session stop on an ended queue restarts`() {
|
|
121
|
+
end()
|
|
122
|
+
player.forwardingPlayer.stop()
|
|
123
|
+
|
|
124
|
+
// What media3 runs for a controller's play.
|
|
125
|
+
Util.handlePlayButtonAction(player.forwardingPlayer)
|
|
126
|
+
assertEquals(0L, player.exoPlayer.currentPosition)
|
|
127
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@Test
|
|
131
|
+
fun `a play after a stop on an ended queue restarts`() {
|
|
132
|
+
end()
|
|
133
|
+
player.stop()
|
|
134
|
+
|
|
135
|
+
player.play()
|
|
136
|
+
assertEquals(0L, player.exoPlayer.currentPosition)
|
|
137
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private fun endAndRebuild(tracks: Int = 1) {
|
|
141
|
+
end(tracks)
|
|
142
|
+
|
|
143
|
+
val before = states().size
|
|
144
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
145
|
+
disableAudio()
|
|
146
|
+
idleFor(2)
|
|
147
|
+
assertEquals(emptyList<PlaybackState>(), states().drop(before))
|
|
148
|
+
assertEquals(PlaybackState.ENDED, player.playbackState)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private fun end(tracks: Int = 1) {
|
|
152
|
+
player.setup(PlayerSetupOptions())
|
|
153
|
+
player.setCallbacks(callbacks)
|
|
154
|
+
disableAudio()
|
|
155
|
+
val src = Uri.fromFile(silentWav()).toString()
|
|
156
|
+
player.exoPlayer.setMediaItems(
|
|
157
|
+
List(tracks) { TrackFactory.toMedia3(TestFixtures.track(title = "$it", src = src)) }
|
|
158
|
+
)
|
|
159
|
+
player.exoPlayer.prepare()
|
|
160
|
+
player.play()
|
|
161
|
+
idleUntil("ENDED") { player.playbackState == PlaybackState.ENDED }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private fun states() =
|
|
165
|
+
mockingDetails(callbacks)
|
|
166
|
+
.invocations
|
|
167
|
+
.filter { it.method.name == "onPlaybackChanged" }
|
|
168
|
+
.map { (it.arguments[0] as Playback).state }
|
|
169
|
+
|
|
170
|
+
private fun disableAudio() {
|
|
171
|
+
player.exoPlayer.trackSelectionParameters =
|
|
172
|
+
player.exoPlayer.trackSelectionParameters
|
|
173
|
+
.buildUpon()
|
|
174
|
+
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
|
|
175
|
+
.build()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private fun idleFor(seconds: Int) =
|
|
179
|
+
repeat(seconds * 10) {
|
|
180
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
181
|
+
Thread.sleep(10)
|
|
182
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private fun idleUntil(what: String, condition: () -> Boolean) {
|
|
186
|
+
val deadline = System.currentTimeMillis() + 5_000
|
|
187
|
+
while (!condition()) {
|
|
188
|
+
if (System.currentTimeMillis() > deadline)
|
|
189
|
+
fail(
|
|
190
|
+
"timed out waiting for $what: ${player.playbackState}, " +
|
|
191
|
+
"exo ${player.exoPlayer.playbackState}, ${player.exoPlayer.playerError}"
|
|
192
|
+
)
|
|
193
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
194
|
+
Thread.sleep(10)
|
|
195
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Half a second of 8 kHz mono 8-bit silence. */
|
|
200
|
+
private fun silentWav(): File {
|
|
201
|
+
val samples = 4_000
|
|
202
|
+
val header =
|
|
203
|
+
ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
|
|
204
|
+
put("RIFF".toByteArray())
|
|
205
|
+
putInt(36 + samples)
|
|
206
|
+
put("WAVEfmt ".toByteArray())
|
|
207
|
+
putInt(16)
|
|
208
|
+
putShort(1)
|
|
209
|
+
putShort(1)
|
|
210
|
+
putInt(8_000)
|
|
211
|
+
putInt(8_000)
|
|
212
|
+
putShort(1)
|
|
213
|
+
putShort(8)
|
|
214
|
+
put("data".toByteArray())
|
|
215
|
+
putInt(samples)
|
|
216
|
+
}
|
|
217
|
+
return folder.newFile("silence.wav").apply {
|
|
218
|
+
writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|