react-native-audio-browser 0.2.0-next.75 → 0.2.0-next.77
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 +20 -27
- package/android/src/main/java/com/audiobrowser/Service.kt +3 -0
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +16 -8
- package/android/src/main/java/com/audiobrowser/browser/TrackLoadHandler.kt +5 -3
- package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +10 -2
- 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 +8 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +85 -37
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +38 -11
- package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +1 -1
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +21 -0
- package/android/src/main/java/com/audiobrowser/util/AwaitCancellable.kt +45 -0
- package/android/src/test/java/androidx/media3/session/SessionViews.kt +10 -0
- package/android/src/test/java/com/audiobrowser/GateDecisionTest.kt +128 -0
- package/android/src/test/java/com/audiobrowser/ResolveFormattedErrorTest.kt +93 -0
- package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +35 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerLayerResolutionTest.kt +75 -5
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerTest.kt +42 -0
- package/android/src/test/java/com/audiobrowser/browser/TrackLoadHandlerTest.kt +43 -0
- package/android/src/test/java/com/audiobrowser/http/RequestConfigBuilderTest.kt +62 -0
- package/android/src/test/java/com/audiobrowser/player/BrowseTilesTest.kt +31 -32
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +372 -0
- package/android/src/test/java/com/audiobrowser/player/NowPlayingFormatterTest.kt +75 -0
- package/android/src/test/java/com/audiobrowser/player/NowPlayingUpdaterTest.kt +25 -0
- package/android/src/test/java/com/audiobrowser/player/PlayFromSearchTest.kt +108 -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 +153 -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 +11 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +58 -0
- package/android/src/test/java/com/audiobrowser/util/AwaitCancellableTest.kt +136 -0
- package/android/src/test/java/com/audiobrowser/util/JsPromise.kt +44 -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 +2 -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
|
@@ -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
|
+
}
|
|
@@ -121,15 +121,25 @@ class PlayerSetupTest {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
@Test
|
|
124
|
-
fun
|
|
124
|
+
fun setupWithChangedOptionsKeepsAnUnpreparedQueueAndItsState() {
|
|
125
125
|
player.setup(PlayerSetupOptions())
|
|
126
126
|
player.exoPlayer.setMediaItems(listOf(item("a")))
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
127
|
+
listOf(PlaybackState.PAUSED, PlaybackState.STOPPED, PlaybackState.ERROR).forEachIndexed {
|
|
128
|
+
i,
|
|
129
|
+
state ->
|
|
130
|
+
player.setPlaybackState(state)
|
|
131
|
+
|
|
132
|
+
// A different maxBuffer each time, so each setup() rebuilds.
|
|
133
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0 + i))
|
|
134
|
+
|
|
135
|
+
assertEquals(listOf("a"), player.tracks.map { it.title })
|
|
136
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
137
|
+
assertEquals(state, player.playbackState)
|
|
138
|
+
assertEquals(
|
|
139
|
+
if (state == PlaybackState.STOPPED) MediaPlayer.STATE_IDLE else MediaPlayer.STATE_READY,
|
|
140
|
+
player.forwardingPlayer.playbackState,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
private fun item(title: String) =
|
|
@@ -9,16 +9,20 @@ import androidx.media3.common.Player as MediaPlayer
|
|
|
9
9
|
import com.audiobrowser.Callbacks
|
|
10
10
|
import com.audiobrowser.TestFixtures
|
|
11
11
|
import com.audiobrowser.model.PlayerSetupOptions
|
|
12
|
+
import com.audiobrowser.remote.FakeRemote
|
|
12
13
|
import com.audiobrowser.util.TrackFactory
|
|
13
14
|
import com.margelo.nitro.audiobrowser.Playback
|
|
14
15
|
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
15
16
|
import com.margelo.nitro.audiobrowser.PlayingState
|
|
17
|
+
import com.margelo.nitro.audiobrowser.RemoteOutputsOptions
|
|
16
18
|
import java.io.File
|
|
17
19
|
import java.nio.ByteBuffer
|
|
18
20
|
import java.nio.ByteOrder
|
|
19
21
|
import org.junit.After
|
|
20
22
|
import org.junit.Assert.assertEquals
|
|
21
23
|
import org.junit.Assert.assertFalse
|
|
24
|
+
import org.junit.Assert.assertNotSame
|
|
25
|
+
import org.junit.Assert.assertSame
|
|
22
26
|
import org.junit.Assert.assertTrue
|
|
23
27
|
import org.junit.Assert.fail
|
|
24
28
|
import org.junit.Before
|
|
@@ -46,7 +50,13 @@ class PlayerSuppressionTest {
|
|
|
46
50
|
@get:Rule val folder = TemporaryFolder()
|
|
47
51
|
|
|
48
52
|
private val context = RuntimeEnvironment.getApplication()
|
|
49
|
-
private val
|
|
53
|
+
private val remote = FakeRemote(Looper.getMainLooper())
|
|
54
|
+
private val player =
|
|
55
|
+
Player(
|
|
56
|
+
context,
|
|
57
|
+
castRemote = { _, _ -> remote },
|
|
58
|
+
sonosRemote = { _, _ -> FakeRemote(Looper.getMainLooper()) },
|
|
59
|
+
)
|
|
50
60
|
private val callbacks = mock(Callbacks::class.java)
|
|
51
61
|
private val audioManager =
|
|
52
62
|
shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
|
|
@@ -144,16 +154,182 @@ class PlayerSuppressionTest {
|
|
|
144
154
|
}
|
|
145
155
|
|
|
146
156
|
@Test
|
|
147
|
-
fun `a rebuild during a transient focus loss
|
|
157
|
+
fun `a rebuild during a transient focus loss stays suppressed until focus returns`() {
|
|
148
158
|
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
159
|
+
val engine = player.exoPlayer
|
|
160
|
+
|
|
161
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
162
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
163
|
+
|
|
164
|
+
assertSame(engine, player.exoPlayer)
|
|
165
|
+
assertSuppressed()
|
|
166
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
167
|
+
idleUntil("playing on a new engine") {
|
|
168
|
+
player.playbackState == PlaybackState.PLAYING && player.exoPlayer !== engine
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@Test
|
|
173
|
+
fun `a rebuild during a call keeps the intent`() {
|
|
174
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
175
|
+
audioManager.setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_FAILED)
|
|
176
|
+
|
|
177
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
178
|
+
// ExoPlayer requests focus on its playback thread and posts a refusal back.
|
|
179
|
+
Thread.sleep(200)
|
|
180
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
181
|
+
|
|
182
|
+
assertSuppressed()
|
|
183
|
+
audioManager.setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
|
|
184
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
185
|
+
idleUntil("playing again") { player.playbackState == PlaybackState.PLAYING }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
@Test
|
|
189
|
+
fun `a rebuild held during a transient focus loss runs when the intent drops`() {
|
|
190
|
+
listOf<() -> Unit>(
|
|
191
|
+
{ focus(AudioManager.AUDIOFOCUS_LOSS) },
|
|
192
|
+
{ player.pause() },
|
|
193
|
+
{ player.stop() },
|
|
194
|
+
)
|
|
195
|
+
.forEachIndexed { i, drop ->
|
|
196
|
+
player.play()
|
|
197
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
198
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
199
|
+
val engine = player.exoPlayer
|
|
200
|
+
// A different maxBuffer each time, so each setup() rebuilds.
|
|
201
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0 + i))
|
|
202
|
+
assertSame(engine, player.exoPlayer)
|
|
203
|
+
drop()
|
|
204
|
+
|
|
205
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
206
|
+
|
|
207
|
+
assertNotSame(engine, player.exoPlayer)
|
|
208
|
+
assertFalse(player.exoPlayer.playWhenReady)
|
|
209
|
+
}
|
|
210
|
+
assertEquals(PlaybackState.STOPPED, player.playbackState)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
@Test
|
|
214
|
+
fun `a play during a transient focus loss runs a held rebuild after it`() {
|
|
215
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
216
|
+
val engine = player.exoPlayer
|
|
217
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
218
|
+
assertSame(engine, player.exoPlayer)
|
|
219
|
+
|
|
220
|
+
// ExoPlayer's play() asks for focus again, so it plays over the transient loss.
|
|
221
|
+
player.play()
|
|
222
|
+
// The rebuild follows the play: it is posted, never run inside it.
|
|
223
|
+
assertSame(engine, player.exoPlayer)
|
|
224
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
225
|
+
|
|
226
|
+
assertNotSame(engine, player.exoPlayer)
|
|
227
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
@Test
|
|
231
|
+
fun `an engine stopped during a transient focus loss holds no rebuild`() {
|
|
232
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
233
|
+
// As a handover to a remote output does: the engine stops but keeps the intent.
|
|
234
|
+
player.exoPlayer.stop()
|
|
235
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
236
|
+
val engine = player.exoPlayer
|
|
237
|
+
|
|
238
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
239
|
+
|
|
240
|
+
assertNotSame(engine, player.exoPlayer)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
@Test
|
|
244
|
+
fun `a held rebuild runs when a remote output takes over`() {
|
|
245
|
+
player.setup(
|
|
246
|
+
PlayerSetupOptions(
|
|
247
|
+
remoteOutputs =
|
|
248
|
+
RemoteOutputsOptions(cast = true, castReceiverApplicationId = null, sonos = null)
|
|
249
|
+
)
|
|
250
|
+
)
|
|
251
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
252
|
+
val engine = player.exoPlayer
|
|
253
|
+
player.setup(
|
|
254
|
+
PlayerSetupOptions(
|
|
255
|
+
maxBuffer = 120_000.0,
|
|
256
|
+
remoteOutputs =
|
|
257
|
+
RemoteOutputsOptions(cast = true, castReceiverApplicationId = null, sonos = null),
|
|
258
|
+
)
|
|
259
|
+
)
|
|
260
|
+
assertSame(engine, player.exoPlayer)
|
|
261
|
+
|
|
262
|
+
remote.startSession()
|
|
263
|
+
idleUntil("remote") { !player.localEngine.isActive }
|
|
264
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
152
265
|
|
|
266
|
+
assertNotSame(engine, player.exoPlayer)
|
|
267
|
+
assertFalse(player.localEngine.isActive)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
@Test
|
|
271
|
+
fun `a held rebuild runs when the engine idles`() {
|
|
272
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
273
|
+
val engine = player.exoPlayer
|
|
153
274
|
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
275
|
+
assertSame(engine, player.exoPlayer)
|
|
276
|
+
|
|
277
|
+
// As a terminal error does: the engine idles with the intent and the suppression unchanged.
|
|
278
|
+
player.exoPlayer.stop()
|
|
279
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
154
280
|
|
|
155
|
-
|
|
156
|
-
|
|
281
|
+
assertNotSame(engine, player.exoPlayer)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
@Test
|
|
285
|
+
fun `the last setup during a transient focus loss wins`() {
|
|
286
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
287
|
+
val engines = mutableSetOf(player.exoPlayer)
|
|
288
|
+
|
|
289
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
290
|
+
engines += player.exoPlayer
|
|
291
|
+
player.setup(PlayerSetupOptions(maxBuffer = 130_000.0))
|
|
292
|
+
engines += player.exoPlayer
|
|
293
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
294
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
295
|
+
val engine = player.exoPlayer
|
|
296
|
+
engines += engine
|
|
297
|
+
|
|
298
|
+
assertEquals("one rebuild", 2, engines.size)
|
|
299
|
+
|
|
300
|
+
player.setup(PlayerSetupOptions(maxBuffer = 130_000.0))
|
|
301
|
+
assertSame(engine, player.exoPlayer)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
@Test
|
|
305
|
+
fun `a setup back to the current options during a transient focus loss cancels the rebuild`() {
|
|
306
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
307
|
+
val engine = player.exoPlayer
|
|
308
|
+
|
|
309
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
310
|
+
player.setup(PlayerSetupOptions())
|
|
311
|
+
focus(AudioManager.AUDIOFOCUS_GAIN)
|
|
312
|
+
idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
|
|
313
|
+
|
|
314
|
+
assertSame(engine, player.exoPlayer)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
@Test
|
|
318
|
+
fun `destroying the player drops a held rebuild`() {
|
|
319
|
+
focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
|
|
320
|
+
val engine = player.exoPlayer
|
|
321
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
322
|
+
|
|
323
|
+
player.destroy()
|
|
324
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
325
|
+
|
|
326
|
+
assertSame(engine, player.exoPlayer)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private fun assertSuppressed() {
|
|
330
|
+
assertTrue(player.exoPlayer.playWhenReady)
|
|
331
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
332
|
+
assertEquals(PlayingState(false, false), player.getPlayingState())
|
|
157
333
|
}
|
|
158
334
|
|
|
159
335
|
private fun focus(change: Int) {
|
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import androidx.media3.common.Metadata
|
|
3
6
|
import androidx.media3.common.Player as MediaPlayer
|
|
7
|
+
import androidx.media3.extractor.metadata.icy.IcyInfo
|
|
4
8
|
import com.audiobrowser.Callbacks
|
|
5
9
|
import com.audiobrowser.TestFixtures
|
|
6
10
|
import com.audiobrowser.model.PlayerSetupOptions
|
|
7
11
|
import com.audiobrowser.util.TrackFactory
|
|
12
|
+
import com.margelo.nitro.audiobrowser.NowPlayingUpdate
|
|
8
13
|
import com.margelo.nitro.audiobrowser.TimedMetadata
|
|
14
|
+
import kotlin.concurrent.thread
|
|
9
15
|
import org.junit.After
|
|
10
16
|
import org.junit.Assert.assertEquals
|
|
17
|
+
import org.junit.Assert.assertNull
|
|
11
18
|
import org.junit.Test
|
|
12
19
|
import org.junit.runner.RunWith
|
|
13
20
|
import org.mockito.Answers
|
|
14
21
|
import org.mockito.Mockito.mock
|
|
15
22
|
import org.robolectric.RobolectricTestRunner
|
|
16
23
|
import org.robolectric.RuntimeEnvironment
|
|
24
|
+
import org.robolectric.Shadows.shadowOf
|
|
17
25
|
|
|
18
26
|
@RunWith(RobolectricTestRunner::class)
|
|
19
27
|
class PlayerTrackChangeTest {
|
|
@@ -43,4 +51,149 @@ class PlayerTrackChangeTest {
|
|
|
43
51
|
|
|
44
52
|
assertEquals(listOf<TimedMetadata?>(null), seenAtTrackChange)
|
|
45
53
|
}
|
|
54
|
+
|
|
55
|
+
private fun icy(title: String) =
|
|
56
|
+
Metadata(IcyInfo("StreamTitle='$title';".toByteArray(), title, null))
|
|
57
|
+
|
|
58
|
+
private fun idle() {
|
|
59
|
+
shadowOf(player.exoPlayer.playbackLooper).idle()
|
|
60
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private fun skip() = player.sessionPlayer.seekTo(1, 0)
|
|
64
|
+
|
|
65
|
+
private fun playTwoTracks() {
|
|
66
|
+
player.setup(PlayerSetupOptions())
|
|
67
|
+
player.sessionPlayer.setMediaItems(
|
|
68
|
+
listOf(
|
|
69
|
+
TrackFactory.toMedia3(TestFixtures.track(title = "A", src = "https://s/a.mp3")),
|
|
70
|
+
TrackFactory.toMedia3(TestFixtures.track(title = "B", src = "https://s/b.mp3")),
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
idle()
|
|
74
|
+
player.playerListener.onMetadata(icy("A song 1"))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Delivers an outgoing item's title the way MetadataRenderer does, posted before [switch]. */
|
|
78
|
+
private fun assertOutgoingTitleDropped(switch: () -> Unit) {
|
|
79
|
+
playTwoTracks()
|
|
80
|
+
val playbackLooper = shadowOf(player.exoPlayer.playbackLooper)
|
|
81
|
+
playbackLooper.pause()
|
|
82
|
+
Handler(player.exoPlayer.playbackLooper).post {
|
|
83
|
+
Handler(Looper.getMainLooper()).post { player.playerListener.onMetadata(icy("A song 2")) }
|
|
84
|
+
}
|
|
85
|
+
switch()
|
|
86
|
+
playbackLooper.unPause()
|
|
87
|
+
idle()
|
|
88
|
+
|
|
89
|
+
assertNull(player.nowPlaying.timedMetadata)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@Test
|
|
93
|
+
fun `a title the outgoing item had already posted does not survive a skip`() =
|
|
94
|
+
assertOutgoingTitleDropped(::skip)
|
|
95
|
+
|
|
96
|
+
@Test
|
|
97
|
+
fun `a title the outgoing item had already posted does not survive a new queue`() =
|
|
98
|
+
assertOutgoingTitleDropped {
|
|
99
|
+
player.sessionPlayer.setMediaItems(
|
|
100
|
+
listOf(TrackFactory.toMedia3(TestFixtures.track(title = "C", src = "https://s/c.mp3")))
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@Test
|
|
105
|
+
fun `the next item's first title is kept when the queue advances on its own`() {
|
|
106
|
+
playTwoTracks()
|
|
107
|
+
player.playerListener.onMediaItemTransition(
|
|
108
|
+
player.sessionPlayer.currentMediaItem,
|
|
109
|
+
MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_AUTO,
|
|
110
|
+
)
|
|
111
|
+
player.playerListener.onMetadata(icy("B song 1"))
|
|
112
|
+
|
|
113
|
+
assertEquals("B song 1", player.nowPlaying.timedMetadata?.title)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@Test
|
|
117
|
+
fun `the new item's first title is kept once the playback thread has caught up`() {
|
|
118
|
+
playTwoTracks()
|
|
119
|
+
skip()
|
|
120
|
+
idle()
|
|
121
|
+
player.playerListener.onMetadata(icy("B song 1"))
|
|
122
|
+
|
|
123
|
+
assertEquals("B song 1", player.nowPlaying.timedMetadata?.title)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@Test
|
|
127
|
+
fun `titles are kept after the engine is rebuilt during a track change`() {
|
|
128
|
+
playTwoTracks()
|
|
129
|
+
// Holds the old engine's playback thread until setup() waits on its release, so the thread
|
|
130
|
+
// handles the fence with the release queued behind it.
|
|
131
|
+
val oldPlaybackLooper = shadowOf(player.exoPlayer.playbackLooper)
|
|
132
|
+
oldPlaybackLooper.pause()
|
|
133
|
+
val testThread = Thread.currentThread()
|
|
134
|
+
val releaser =
|
|
135
|
+
thread(isDaemon = true) {
|
|
136
|
+
val deadline = System.currentTimeMillis() + 10_000
|
|
137
|
+
while (
|
|
138
|
+
testThread.state != Thread.State.TIMED_WAITING && System.currentTimeMillis() < deadline
|
|
139
|
+
) {
|
|
140
|
+
Thread.sleep(1)
|
|
141
|
+
}
|
|
142
|
+
oldPlaybackLooper.unPause()
|
|
143
|
+
}
|
|
144
|
+
skip()
|
|
145
|
+
player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
|
|
146
|
+
releaser.join()
|
|
147
|
+
idle()
|
|
148
|
+
player.playerListener.onMetadata(icy("B song 1"))
|
|
149
|
+
|
|
150
|
+
assertEquals("B song 1", player.nowPlaying.timedMetadata?.title)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Plays a station whose formatter shows the stream title as the second line, as an app does. */
|
|
154
|
+
private fun playStationWithTitle() {
|
|
155
|
+
player.setup(PlayerSetupOptions())
|
|
156
|
+
player.nowPlaying.formatter = { params ->
|
|
157
|
+
NowPlayingUpdate(title = null, artist = params.timedMetadata?.title, album = null)
|
|
158
|
+
}
|
|
159
|
+
player.sessionPlayer.setMediaItems(
|
|
160
|
+
listOf(TrackFactory.toMedia3(TestFixtures.track(title = "A", artist = "A's city")))
|
|
161
|
+
)
|
|
162
|
+
idle()
|
|
163
|
+
player.playerListener.onMetadata(icy("A song 1"))
|
|
164
|
+
idle()
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private fun shownSecondLines() =
|
|
168
|
+
listOf(
|
|
169
|
+
player.sessionPlayer.mediaMetadata.artist?.toString(),
|
|
170
|
+
player.nowPlaying.getNowPlaying()?.artist,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
@Test
|
|
174
|
+
fun `a stop clears the stream title the session and the now-playing info show`() {
|
|
175
|
+
playStationWithTitle()
|
|
176
|
+
assertEquals(listOf("A song 1", "A song 1"), shownSecondLines())
|
|
177
|
+
|
|
178
|
+
player.stop()
|
|
179
|
+
idle()
|
|
180
|
+
|
|
181
|
+
assertNull(player.nowPlaying.timedMetadata)
|
|
182
|
+
assertEquals(listOf("A's city", "A's city"), shownSecondLines())
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
@Test
|
|
186
|
+
fun `a play after a stop shows the track's own lines until the stream sends a title`() {
|
|
187
|
+
playStationWithTitle()
|
|
188
|
+
player.stop()
|
|
189
|
+
idle()
|
|
190
|
+
|
|
191
|
+
player.play()
|
|
192
|
+
idle()
|
|
193
|
+
assertEquals(listOf("A's city", "A's city"), shownSecondLines())
|
|
194
|
+
|
|
195
|
+
player.playerListener.onMetadata(icy("A song 2"))
|
|
196
|
+
idle()
|
|
197
|
+
assertEquals(listOf("A song 2", "A song 2"), shownSecondLines())
|
|
198
|
+
}
|
|
46
199
|
}
|