react-native-audio-browser 0.2.0-next.50 → 0.2.0-next.51

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.
Files changed (90) hide show
  1. package/android/build.gradle +15 -0
  2. package/android/src/cast/java/com/audiobrowser/cast/CastSupport.kt +215 -0
  3. package/android/src/main/AndroidManifest.xml +11 -0
  4. package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +0 -4
  5. package/android/src/main/java/com/audiobrowser/RemoteOutputs.kt +47 -10
  6. package/android/src/main/java/com/audiobrowser/model/PlayerSetupOptions.kt +12 -3
  7. package/android/src/main/java/com/audiobrowser/player/EqualizerController.kt +4 -1
  8. package/android/src/main/java/com/audiobrowser/player/LocalEngine.kt +49 -0
  9. package/android/src/main/java/com/audiobrowser/player/Player.kt +170 -119
  10. package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +18 -13
  11. package/android/src/main/java/com/audiobrowser/player/TransformingDataSource.kt +10 -13
  12. package/android/src/main/java/com/audiobrowser/remote/RemoteItem.kt +64 -0
  13. package/android/src/main/java/com/audiobrowser/remote/RemoteOutputsController.kt +98 -0
  14. package/android/src/main/java/com/audiobrowser/remote/RemotePlayback.kt +42 -0
  15. package/android/src/main/java/com/audiobrowser/remote/RemoteQueue.kt +110 -0
  16. package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +696 -0
  17. package/android/src/main/java/com/audiobrowser/sonos/DeviceDescriptionParser.kt +113 -0
  18. package/android/src/main/java/com/audiobrowser/sonos/DidlLite.kt +39 -0
  19. package/android/src/main/java/com/audiobrowser/sonos/SoapClient.kt +59 -0
  20. package/android/src/main/java/com/audiobrowser/sonos/SoapEnvelopes.kt +92 -0
  21. package/android/src/main/java/com/audiobrowser/sonos/SoapResponseParser.kt +105 -0
  22. package/android/src/main/java/com/audiobrowser/sonos/SonosDiscoverer.kt +69 -0
  23. package/android/src/main/java/com/audiobrowser/sonos/SonosOutputs.kt +128 -0
  24. package/android/src/main/java/com/audiobrowser/sonos/SonosPlayer.kt +550 -0
  25. package/android/src/main/java/com/audiobrowser/sonos/SonosRemote.kt +99 -0
  26. package/android/src/main/java/com/audiobrowser/sonos/SonosRouteProviderService.kt +201 -0
  27. package/android/src/main/java/com/audiobrowser/sonos/SonosStreamUri.kt +15 -0
  28. package/android/src/main/java/com/audiobrowser/sonos/SonosTransport.kt +66 -0
  29. package/android/src/main/java/com/audiobrowser/sonos/SpeakerXml.kt +26 -0
  30. package/android/src/main/java/com/audiobrowser/sonos/SsdpDiscovery.kt +63 -0
  31. package/android/src/main/java/com/audiobrowser/sonos/SsdpMessages.kt +63 -0
  32. package/android/src/main/java/com/audiobrowser/sonos/ZoneGroupState.kt +68 -0
  33. package/android/src/main/java/com/audiobrowser/util/OutputMonitor.kt +2 -1
  34. package/android/src/nocast/java/com/audiobrowser/cast/CastSupport.kt +18 -0
  35. package/android/src/test/java/com/audiobrowser/RemoteOutputsTest.kt +20 -3
  36. package/android/src/test/java/com/audiobrowser/model/PlayerSetupOptionsTest.kt +35 -0
  37. package/android/src/test/java/com/audiobrowser/player/EqualizerControllerTest.kt +12 -0
  38. package/android/src/test/java/com/audiobrowser/player/PlayerCastTest.kt +136 -0
  39. package/android/src/test/java/com/audiobrowser/player/PlayerSonosTest.kt +213 -0
  40. package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +275 -0
  41. package/android/src/test/java/com/audiobrowser/remote/RemoteItemTest.kt +61 -0
  42. package/android/src/test/java/com/audiobrowser/remote/RemoteOutputsControllerTest.kt +77 -0
  43. package/android/src/test/java/com/audiobrowser/remote/RemoteQueueTest.kt +101 -0
  44. package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +815 -0
  45. package/android/src/test/java/com/audiobrowser/sonos/DeviceDescriptionParserTest.kt +203 -0
  46. package/android/src/test/java/com/audiobrowser/sonos/DidlLiteTest.kt +90 -0
  47. package/android/src/test/java/com/audiobrowser/sonos/SoapClientTest.kt +73 -0
  48. package/android/src/test/java/com/audiobrowser/sonos/SoapEnvelopesTest.kt +135 -0
  49. package/android/src/test/java/com/audiobrowser/sonos/SoapResponseParserTest.kt +170 -0
  50. package/android/src/test/java/com/audiobrowser/sonos/SonosDiscovererTest.kt +174 -0
  51. package/android/src/test/java/com/audiobrowser/sonos/SonosPlayerTest.kt +691 -0
  52. package/android/src/test/java/com/audiobrowser/sonos/SonosRemoteTest.kt +129 -0
  53. package/android/src/test/java/com/audiobrowser/sonos/SonosRoutesTest.kt +165 -0
  54. package/android/src/test/java/com/audiobrowser/sonos/SonosStreamUriTest.kt +64 -0
  55. package/android/src/test/java/com/audiobrowser/sonos/SonosTransportTest.kt +204 -0
  56. package/android/src/test/java/com/audiobrowser/sonos/SsdpMessagesTest.kt +99 -0
  57. package/android/src/test/java/com/audiobrowser/sonos/ZoneGroupStateTest.kt +139 -0
  58. package/android/src/test/java/com/audiobrowser/util/OutputMonitorTest.kt +12 -10
  59. package/android/src/test/resources/sonos/device_description.xml +78 -0
  60. package/android/src/testCast/java/com/audiobrowser/cast/CastEndsTest.kt +56 -0
  61. package/android/src/testCast/java/com/audiobrowser/cast/SendingMediaItemConverterTest.kt +79 -0
  62. package/lib/commonjs/features/player/setup.js +2 -0
  63. package/lib/commonjs/features/player/setup.js.map +1 -1
  64. package/lib/module/features/player/setup.js +2 -0
  65. package/lib/module/features/player/setup.js.map +1 -1
  66. package/lib/typescript/src/features/player/setup.d.ts +37 -7
  67. package/lib/typescript/src/features/player/setup.d.ts.map +1 -1
  68. package/lib/typescript/src/specs/audio-browser.nitro.d.ts +3 -3
  69. package/lib/typescript/src/specs/audio-browser.nitro.d.ts.map +1 -1
  70. package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.cpp +4 -0
  71. package/nitrogen/generated/android/c++/JNativeSetupPlayerOptions.hpp +7 -5
  72. package/nitrogen/generated/android/c++/JOutputType.hpp +3 -0
  73. package/nitrogen/generated/android/c++/JRemoteOutputsOptions.hpp +66 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/NativeSetupPlayerOptions.kt +3 -3
  75. package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/OutputType.kt +3 -2
  76. package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/RemoteOutputsOptions.kt +61 -0
  77. package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Bridge.hpp +18 -0
  78. package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Umbrella.hpp +3 -0
  79. package/nitrogen/generated/ios/c++/HybridAudioBrowserSpecSwift.hpp +3 -0
  80. package/nitrogen/generated/ios/swift/NativeSetupPlayerOptions.swift +5 -12
  81. package/nitrogen/generated/ios/swift/OutputType.swift +4 -0
  82. package/nitrogen/generated/ios/swift/RemoteOutputsOptions.swift +78 -0
  83. package/nitrogen/generated/shared/c++/NativeSetupPlayerOptions.hpp +8 -5
  84. package/nitrogen/generated/shared/c++/OutputType.hpp +6 -2
  85. package/nitrogen/generated/shared/c++/RemoteOutputsOptions.hpp +92 -0
  86. package/package.json +1 -1
  87. package/src/features/player/setup.ts +38 -7
  88. package/src/specs/audio-browser.nitro.ts +4 -3
  89. package/website/guide/audio-output.md +185 -4
  90. package/website/guide/configuration.md +6 -0
@@ -0,0 +1,815 @@
1
+ package com.audiobrowser.remote
2
+
3
+ import android.os.Looper
4
+ import androidx.media3.common.C
5
+ import androidx.media3.common.MediaItem
6
+ import androidx.media3.common.PlaybackException
7
+ import androidx.media3.common.Player as MediaPlayer
8
+ import java.time.Duration
9
+ import kotlinx.coroutines.CompletableDeferred
10
+ import kotlinx.coroutines.MainScope
11
+ import kotlinx.coroutines.cancel
12
+ import org.junit.After
13
+ import org.junit.Assert.assertEquals
14
+ import org.junit.Assert.assertFalse
15
+ import org.junit.Assert.assertNull
16
+ import org.junit.Assert.assertTrue
17
+ import org.junit.Test
18
+ import org.junit.runner.RunWith
19
+ import org.robolectric.RobolectricTestRunner
20
+ import org.robolectric.Shadows.shadowOf
21
+
22
+ @RunWith(RobolectricTestRunner::class)
23
+ class RemoteSwitchingPlayerTest {
24
+ private val looper = Looper.getMainLooper()
25
+ private val scope = MainScope()
26
+ private val local = FakeLocalPlayer(looper)
27
+ private val remote = FakeRemote(looper)
28
+ private val other = FakeRemote(looper)
29
+ private val resolved = mutableListOf<String>()
30
+ private val live = mutableSetOf<String>()
31
+ private val unresolvable = mutableSetOf<String>()
32
+
33
+ /** Holds a resolution until completed, for the steps that must wait on one. */
34
+ private var gate: CompletableDeferred<Unit>? = null
35
+
36
+ private val player =
37
+ RemoteSwitchingPlayer(
38
+ local,
39
+ listOf(remote, other),
40
+ resolve = { item ->
41
+ gate?.await()
42
+ check(item.mediaId !in unresolvable) { "no URL" }
43
+ resolved += item.mediaId
44
+ remoteItemOf(item, "https://cdn.example/${item.mediaId}", null, item.mediaId in live)
45
+ },
46
+ scope = scope,
47
+ )
48
+
49
+ @After fun cancel() = scope.cancel()
50
+
51
+ @Test
52
+ fun `a session takes the current item to the receiver at its position and play intent`() {
53
+ playLocally(listOf("a", "b", "c"), index = 1, positionMs = 5_000)
54
+
55
+ remote.startSession()
56
+ idle()
57
+
58
+ assertTrue(player.isRemote)
59
+ assertEquals(listOf(Triple("b", 5_000L, true)), remote.player.loads.map { it.short() })
60
+ assertEquals(
61
+ "https://cdn.example/b",
62
+ remote.player.loads.single().item.localConfiguration?.uri.toString(),
63
+ )
64
+ assertEquals(listOf("b"), resolved)
65
+ assertEquals(1, local.stops)
66
+ assertEquals(listOf("a", "b", "c"), queue())
67
+ assertEquals(1, player.currentMediaItemIndex)
68
+ assertEquals("b", player.currentMediaItem?.mediaId)
69
+ }
70
+
71
+ @Test
72
+ fun `the phone plays on until the receiver's item is resolved`() {
73
+ playLocally(listOf("a"))
74
+ gate = CompletableDeferred()
75
+
76
+ remote.startSession()
77
+ idle()
78
+
79
+ assertFalse(player.isRemote)
80
+ assertEquals(0, local.stops)
81
+ assertTrue(remote.player.loads.isEmpty())
82
+
83
+ gate!!.complete(Unit)
84
+ idle()
85
+ assertTrue(player.isRemote)
86
+ }
87
+
88
+ @Test
89
+ fun `an item the phone moves on to while resolving is the one handed over`() {
90
+ playLocally(listOf("a", "b"))
91
+ gate = CompletableDeferred()
92
+ remote.startSession()
93
+ idle()
94
+
95
+ local.moveTo(1)
96
+ gate!!.complete(Unit)
97
+ idle()
98
+
99
+ assertEquals(listOf("b"), remote.player.loads.map { it.item.mediaId })
100
+ assertEquals(1, player.currentMediaItemIndex)
101
+ }
102
+
103
+ @Test
104
+ fun `a live item starts at the receiver's live edge`() {
105
+ live += "a"
106
+ playLocally(listOf("a"), positionMs = 90_000)
107
+
108
+ remote.startSession()
109
+ idle()
110
+
111
+ assertEquals(C.TIME_UNSET, remote.player.loads.single().positionMs)
112
+ }
113
+
114
+ @Test
115
+ fun `an unprepared queue goes over unloaded, and prepare loads it at its position`() {
116
+ local.setMediaItems(items("a"), 0, 3_000)
117
+ idle()
118
+
119
+ remote.startSession()
120
+ idle()
121
+ assertTrue(player.isRemote)
122
+ assertTrue(remote.player.loads.isEmpty())
123
+ assertEquals(MediaPlayer.STATE_IDLE, player.playbackState)
124
+
125
+ player.prepare()
126
+ idle()
127
+ assertEquals(3_000, remote.player.loads.single().positionMs)
128
+ }
129
+
130
+ @Test
131
+ fun `the session ending brings the queue, its position and the play intent back`() {
132
+ playLocally(listOf("a", "b"))
133
+ remote.startSession()
134
+ idle()
135
+ val receiver = remote.player
136
+ receiver.positionMs = 7_000
137
+ receiver.update()
138
+ idle()
139
+
140
+ remote.endSession()
141
+ idle()
142
+
143
+ assertFalse(player.isRemote)
144
+ assertEquals(listOf("a", "b"), local.items.map { it.mediaId })
145
+ assertEquals(0, local.index)
146
+ assertEquals(7_000, local.positionMs)
147
+ assertTrue(local.playWhenReady)
148
+ assertEquals(2, local.prepares)
149
+ assertEquals(1, receiver.stops)
150
+ }
151
+
152
+ @Test
153
+ fun `the receiver's end plays the next item from its start`() {
154
+ playLocally(listOf("a", "b"), positionMs = 4_000)
155
+ remote.startSession()
156
+ idle()
157
+
158
+ remote.end()
159
+ idle()
160
+
161
+ assertEquals(listOf("a", "b"), remote.player.loads.map { it.item.mediaId })
162
+ assertEquals(C.TIME_UNSET, remote.player.loads.last().positionMs)
163
+ assertEquals(1, player.currentMediaItemIndex)
164
+ }
165
+
166
+ @Test
167
+ fun `the last item's end ends playback`() {
168
+ playLocally(listOf("a"))
169
+ remote.startSession()
170
+ idle()
171
+
172
+ remote.end()
173
+ idle()
174
+
175
+ assertEquals(MediaPlayer.STATE_ENDED, player.playbackState)
176
+ assertEquals(1, remote.player.loads.size)
177
+ }
178
+
179
+ @Test
180
+ fun `repeat one plays the item again, and repeat all goes back to the first`() {
181
+ playLocally(listOf("a", "b"), index = 1)
182
+ remote.startSession()
183
+ idle()
184
+
185
+ player.repeatMode = MediaPlayer.REPEAT_MODE_ONE
186
+ remote.end()
187
+ idle()
188
+ player.repeatMode = MediaPlayer.REPEAT_MODE_ALL
189
+ remote.end()
190
+ idle()
191
+
192
+ assertEquals(listOf("b", "b", "a"), remote.player.loads.map { it.item.mediaId })
193
+ assertEquals(MediaPlayer.REPEAT_MODE_ALL, player.repeatMode)
194
+ }
195
+
196
+ @Test
197
+ fun `repeat and shuffle go over and come back`() {
198
+ playLocally(listOf("a", "b"))
199
+ local.repeatMode = MediaPlayer.REPEAT_MODE_ALL
200
+ local.shuffleModeEnabled = true
201
+ idle()
202
+
203
+ remote.startSession()
204
+ idle()
205
+ assertEquals(MediaPlayer.REPEAT_MODE_ALL, player.repeatMode)
206
+ assertTrue(player.shuffleModeEnabled)
207
+
208
+ player.shuffleModeEnabled = false
209
+ player.repeatMode = MediaPlayer.REPEAT_MODE_ONE
210
+ remote.endSession()
211
+ idle()
212
+
213
+ assertFalse(local.shuffleModeEnabled)
214
+ assertEquals(MediaPlayer.REPEAT_MODE_ONE, local.repeatMode)
215
+ }
216
+
217
+ @Test
218
+ fun `edits that leave the current item in place don't reload the receiver`() {
219
+ playLocally(listOf("a", "b", "c"), index = 1)
220
+ remote.startSession()
221
+ idle()
222
+
223
+ player.addMediaItems(0, items("x"))
224
+ player.removeMediaItem(3)
225
+ player.moveMediaItem(0, 2)
226
+ player.replaceMediaItem(0, item("a").buildUpon().setMediaMetadata(titled("now")).build())
227
+ idle()
228
+
229
+ assertEquals(1, remote.player.loads.size)
230
+ assertEquals(listOf("a", "b", "x"), queue())
231
+ assertEquals("b", player.currentMediaItem?.mediaId)
232
+ assertEquals(MediaPlayer.STATE_READY, player.playbackState)
233
+ }
234
+
235
+ @Test
236
+ fun `a metadata restamp of the current item doesn't reload the receiver`() {
237
+ playLocally(listOf("a"))
238
+ remote.startSession()
239
+ idle()
240
+
241
+ player.replaceMediaItem(0, item("a").buildUpon().setMediaMetadata(titled("now")).build())
242
+ idle()
243
+
244
+ assertEquals(1, remote.player.loads.size)
245
+ assertEquals("now", player.currentMediaItem?.mediaMetadata?.title)
246
+ }
247
+
248
+ @Test
249
+ fun `removing the current item plays the one in its place`() {
250
+ playLocally(listOf("a", "b"))
251
+ remote.startSession()
252
+ idle()
253
+
254
+ player.removeMediaItem(0)
255
+ idle()
256
+
257
+ assertEquals(listOf("a", "b"), remote.player.loads.map { it.item.mediaId })
258
+ }
259
+
260
+ @Test
261
+ fun `a new queue plays its start item from its start position`() {
262
+ playLocally(listOf("a"))
263
+ remote.startSession()
264
+ idle()
265
+
266
+ player.setMediaItems(items("x", "y"), 1, 2_000)
267
+ idle()
268
+
269
+ assertEquals(Triple("y", 2_000L, true), remote.player.loads.last().short())
270
+ assertEquals(listOf("x", "y"), queue())
271
+ }
272
+
273
+ @Test
274
+ fun `a seek within the playing item seeks the receiver`() {
275
+ playLocally(listOf("a", "b"))
276
+ remote.startSession()
277
+ idle()
278
+
279
+ player.seekTo(30_000)
280
+ idle()
281
+
282
+ assertEquals(listOf(30_000L), remote.player.seeks)
283
+ assertEquals(1, remote.player.loads.size)
284
+ }
285
+
286
+ @Test
287
+ fun `a skip loads the next item`() {
288
+ playLocally(listOf("a", "b"))
289
+ remote.startSession()
290
+ idle()
291
+
292
+ player.seekToNextMediaItem()
293
+ idle()
294
+
295
+ assertEquals(listOf("a", "b"), remote.player.loads.map { it.item.mediaId })
296
+ assertEquals(1, player.currentMediaItemIndex)
297
+ }
298
+
299
+ @Test
300
+ fun `a receiver's failure is the player error until prepare loads the item again`() {
301
+ playLocally(listOf("a"))
302
+ remote.startSession()
303
+ idle()
304
+ remote.player.positionMs = 12_000
305
+ remote.player.update()
306
+ remote.fail()
307
+ idle()
308
+
309
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
310
+ assertEquals(MediaPlayer.STATE_IDLE, player.playbackState)
311
+
312
+ player.prepare()
313
+ idle()
314
+ assertNull(player.playerError)
315
+ assertEquals(Triple("a", 12_000L, true), remote.player.loads.last().short())
316
+ }
317
+
318
+ @Test
319
+ fun `an item that doesn't resolve is the player error`() {
320
+ unresolvable += "b"
321
+ playLocally(listOf("a", "b"))
322
+ remote.startSession()
323
+ idle()
324
+
325
+ player.seekToNextMediaItem()
326
+ idle()
327
+
328
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
329
+ assertEquals(1, remote.player.loads.size)
330
+ }
331
+
332
+ @Test
333
+ fun `a pause after a failure still reaches the receiver`() {
334
+ playLocally(listOf("a"))
335
+ remote.startSession()
336
+ idle()
337
+ remote.fail()
338
+ idle()
339
+
340
+ player.pause()
341
+ idle()
342
+
343
+ assertFalse(remote.player.playWhenReady)
344
+ }
345
+
346
+ @Test
347
+ fun `a pause while the next item resolves reaches the receiver, and the item loads paused`() {
348
+ playLocally(listOf("a", "b"))
349
+ remote.startSession()
350
+ idle()
351
+ gate = CompletableDeferred()
352
+ player.seekToNextMediaItem()
353
+ idle()
354
+
355
+ player.pause()
356
+ idle()
357
+ assertFalse(remote.player.playWhenReady)
358
+
359
+ gate!!.complete(Unit)
360
+ idle()
361
+ assertEquals(Triple("b", C.TIME_UNSET, false), remote.player.loads.last().short())
362
+ }
363
+
364
+ @Test
365
+ fun `a play while the next item resolves doesn't play the last one`() {
366
+ playLocally(listOf("a", "b"))
367
+ remote.startSession()
368
+ idle()
369
+ player.pause()
370
+ gate = CompletableDeferred()
371
+ player.seekToNextMediaItem()
372
+ idle()
373
+
374
+ player.play()
375
+ idle()
376
+ assertFalse(remote.player.playWhenReady)
377
+
378
+ gate!!.complete(Unit)
379
+ idle()
380
+ assertEquals(Triple("b", C.TIME_UNSET, true), remote.player.loads.last().short())
381
+ }
382
+
383
+ @Test
384
+ fun `the end of an item doesn't tell the receiver to play what it holds`() {
385
+ playLocally(listOf("a", "b"))
386
+ remote.startSession()
387
+ idle()
388
+ val intentsBefore = remote.player.intents.size
389
+
390
+ remote.end()
391
+ idle()
392
+
393
+ assertEquals(intentsBefore, remote.player.intents.size)
394
+ assertTrue(remote.player.loads.last().autoplay)
395
+ }
396
+
397
+ @Test
398
+ fun `stop stops the receiver, and prepare loads the item again where it was`() {
399
+ playLocally(listOf("a"))
400
+ remote.startSession()
401
+ idle()
402
+ remote.player.positionMs = 8_000
403
+ remote.player.update()
404
+
405
+ player.stop()
406
+ idle()
407
+ assertEquals(1, remote.player.stops)
408
+ assertEquals(MediaPlayer.STATE_IDLE, player.playbackState)
409
+
410
+ player.prepare()
411
+ idle()
412
+ assertEquals(8_000, remote.player.loads.last().positionMs)
413
+ }
414
+
415
+ @Test
416
+ fun `a pause from elsewhere is the play intent`() {
417
+ playLocally(listOf("a"))
418
+ remote.startSession()
419
+ idle()
420
+
421
+ remote.player.setIntentFromElsewhere(false)
422
+ idle()
423
+
424
+ assertFalse(player.playWhenReady)
425
+ remote.endSession()
426
+ idle()
427
+ assertFalse(local.playWhenReady)
428
+ }
429
+
430
+ @Test
431
+ fun `a receiver still reporting the last item shows the sent one buffering`() {
432
+ playLocally(listOf("a", "b"))
433
+ remote.startSession()
434
+ idle()
435
+ remote.player.lagging = true
436
+
437
+ player.seekToNextMediaItem()
438
+ idle()
439
+
440
+ assertEquals("b", player.currentMediaItem?.mediaId)
441
+ assertEquals(MediaPlayer.STATE_BUFFERING, player.playbackState)
442
+
443
+ remote.player.catchUp()
444
+ idle()
445
+ assertEquals(MediaPlayer.STATE_READY, player.playbackState)
446
+ }
447
+
448
+ @Test
449
+ fun `the volume is the receiver's device volume`() {
450
+ playLocally(listOf("a"))
451
+ remote.startSession()
452
+ idle()
453
+
454
+ player.volume = 0.5f
455
+ idle()
456
+
457
+ assertEquals(FakeReceiver.MAX_VOLUME / 2, remote.player.volume)
458
+ assertEquals(0.5f, player.volume)
459
+ }
460
+
461
+ @Test
462
+ fun `another remote's session moves playback straight there`() {
463
+ playLocally(listOf("a", "b"), index = 1)
464
+ remote.startSession()
465
+ idle()
466
+ remote.player.positionMs = 6_000
467
+ remote.player.update()
468
+
469
+ other.startSession()
470
+ remote.endSession()
471
+ idle()
472
+
473
+ assertEquals(Triple("b", 6_000L, true), other.player.loads.single().short())
474
+ assertEquals(1, local.stops)
475
+ assertEquals(1, local.prepares)
476
+ }
477
+
478
+ @Test
479
+ fun `a new session of the playing remote takes over from the last`() {
480
+ playLocally(listOf("a"))
481
+ remote.startSession()
482
+ idle()
483
+ val first = remote.player
484
+ first.positionMs = 9_000
485
+ first.update()
486
+
487
+ remote.startSession()
488
+ idle()
489
+
490
+ assertEquals(1, first.stops)
491
+ assertEquals(Triple("a", 9_000L, true), remote.player.loads.single().short())
492
+ assertTrue(player.isRemote)
493
+ }
494
+
495
+ @Test
496
+ fun `a remote that isn't playing does not fail playback`() {
497
+ playLocally(listOf("a"))
498
+ remote.startSession()
499
+ idle()
500
+ other.sessionUp()
501
+
502
+ other.fail()
503
+ idle()
504
+
505
+ assertNull(player.playerError)
506
+ }
507
+
508
+ @Test
509
+ fun `a session ending while its handover resolves leaves the next remote to take over`() {
510
+ playLocally(listOf("a"))
511
+ other.sessionUp()
512
+ gate = CompletableDeferred()
513
+ remote.startSession()
514
+ idle()
515
+
516
+ remote.endSession()
517
+ gate!!.complete(Unit)
518
+ idle()
519
+
520
+ assertEquals(1, other.player.loads.size)
521
+ assertTrue(player.isRemote)
522
+ }
523
+
524
+ @Test
525
+ fun `a remote turned off is released, and one that stays keeps playing`() {
526
+ playLocally(listOf("a"))
527
+ remote.startSession()
528
+ idle()
529
+
530
+ player.setRemotes(listOf(remote))
531
+ idle()
532
+
533
+ assertTrue(other.released)
534
+ assertFalse(remote.released)
535
+ assertTrue(player.isRemote)
536
+ assertEquals(1, remote.player.loads.size)
537
+ }
538
+
539
+ @Test
540
+ fun `turning off the remote that plays brings playback back before letting it go`() {
541
+ playLocally(listOf("a"))
542
+ remote.startSession()
543
+ idle()
544
+ val receiver = remote.player
545
+
546
+ player.setRemotes(listOf(other))
547
+ idle()
548
+
549
+ assertFalse(player.isRemote)
550
+ assertTrue(remote.released)
551
+ assertEquals(1, receiver.stops)
552
+ assertEquals(listOf("a"), local.items.map { it.mediaId })
553
+ assertTrue(local.playWhenReady)
554
+ }
555
+
556
+ @Test
557
+ fun `an item that doesn't resolve at the handover is the player error at once`() {
558
+ unresolvable += "a"
559
+ playLocally(listOf("a"))
560
+ val errors = mutableListOf<PlaybackException>()
561
+ player.addListener(
562
+ object : MediaPlayer.Listener {
563
+ override fun onPlayerError(error: PlaybackException) {
564
+ errors += error
565
+ }
566
+ }
567
+ )
568
+
569
+ remote.startSession()
570
+ idle()
571
+
572
+ assertEquals(listOf(PlaybackException.ERROR_CODE_REMOTE_ERROR), errors.map { it.errorCode })
573
+ }
574
+
575
+ @Test
576
+ fun `an end reported before the receiver holds the item sent is the last item's`() {
577
+ playLocally(listOf("a", "b", "c"))
578
+ remote.startSession()
579
+ idle()
580
+ remote.player.lagging = true
581
+ player.seekToNextMediaItem()
582
+ idle()
583
+
584
+ remote.end()
585
+ idle()
586
+
587
+ assertEquals(listOf("a", "b"), remote.player.loads.map { it.item.mediaId })
588
+ assertEquals(1, player.currentMediaItemIndex)
589
+ }
590
+
591
+ @Test
592
+ fun `an item the receiver doesn't start in time is the player error`() {
593
+ playLocally(listOf("a"))
594
+ remote.startSession()
595
+ idle()
596
+ remote.player.lagging = true
597
+ player.seekToDefaultPosition()
598
+ idle()
599
+
600
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
601
+
602
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
603
+ }
604
+
605
+ @Test
606
+ fun `an item the receiver started is not timed out`() {
607
+ playLocally(listOf("a"))
608
+ remote.startSession()
609
+ idle()
610
+ remote.playing()
611
+
612
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
613
+
614
+ assertNull(player.playerError)
615
+ }
616
+
617
+ @Test
618
+ fun `an item that started and then buffers is not timed out`() {
619
+ playLocally(listOf("a"))
620
+ remote.startSession()
621
+ idle()
622
+ remote.playing()
623
+
624
+ remote.player.readiness = MediaPlayer.STATE_BUFFERING
625
+ remote.player.update()
626
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
627
+
628
+ assertNull(player.playerError)
629
+ }
630
+
631
+ @Test
632
+ fun `a play the receiver doesn't start in time is the player error`() {
633
+ playLocally(listOf("a"))
634
+ remote.startSession()
635
+ idle()
636
+ player.pause()
637
+ remote.player.readiness = MediaPlayer.STATE_BUFFERING
638
+ remote.player.update()
639
+ idle()
640
+
641
+ player.play()
642
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
643
+
644
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
645
+ }
646
+
647
+ @Test
648
+ fun `a play after a paused handover that the receiver never reports playing is the player error`() {
649
+ local.setMediaItems(items("a"))
650
+ local.prepare()
651
+ idle()
652
+ remote.startSession()
653
+ idle()
654
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
655
+ assertNull(player.playerError)
656
+
657
+ player.play()
658
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
659
+
660
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
661
+ }
662
+
663
+ @Test
664
+ fun `a play after a paused handover that the receiver reports playing is not timed out`() {
665
+ local.setMediaItems(items("a"))
666
+ local.prepare()
667
+ idle()
668
+ remote.startSession()
669
+ idle()
670
+
671
+ player.play()
672
+ remote.playing()
673
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
674
+
675
+ assertNull(player.playerError)
676
+ }
677
+
678
+ @Test
679
+ fun `a playing report before the receiver shows any item counts for the item sent`() {
680
+ playLocally(listOf("a", "b"))
681
+ remote.startSession()
682
+ idle()
683
+ remote.playing()
684
+ remote.player.lagging = true
685
+ player.seekToNextMediaItem()
686
+ idle()
687
+
688
+ remote.player.item = null
689
+ remote.player.update()
690
+ remote.playing()
691
+ remote.player.catchUp()
692
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
693
+
694
+ assertNull(player.playerError)
695
+ }
696
+
697
+ @Test
698
+ fun `a playing report while the receiver shows the last item doesn't count for the item sent`() {
699
+ playLocally(listOf("a", "b"))
700
+ remote.startSession()
701
+ idle()
702
+ remote.playing()
703
+ remote.player.lagging = true
704
+ player.seekToNextMediaItem()
705
+ idle()
706
+
707
+ remote.playing()
708
+ remote.player.catchUp()
709
+ shadowOf(looper).idleFor(Duration.ofSeconds(31))
710
+
711
+ assertEquals(PlaybackException.ERROR_CODE_REMOTE_ERROR, player.playerError?.errorCode)
712
+ }
713
+
714
+ @Test
715
+ fun `removing the current item when it is the last ends playback`() {
716
+ playLocally(listOf("a", "b"), index = 1)
717
+ remote.startSession()
718
+ idle()
719
+
720
+ player.removeMediaItem(1)
721
+ idle()
722
+
723
+ assertEquals(MediaPlayer.STATE_ENDED, player.playbackState)
724
+ assertEquals(listOf("b"), remote.player.loads.map { it.item.mediaId })
725
+ assertEquals(1, remote.player.stops)
726
+ }
727
+
728
+ @Test
729
+ fun `removing the current item when it is the last goes back to the first under repeat all`() {
730
+ playLocally(listOf("a", "b"), index = 1)
731
+ local.repeatMode = MediaPlayer.REPEAT_MODE_ALL
732
+ remote.startSession()
733
+ idle()
734
+
735
+ player.removeMediaItem(1)
736
+ idle()
737
+
738
+ assertEquals(listOf("b", "a"), remote.player.loads.map { it.item.mediaId })
739
+ assertEquals(0, player.currentMediaItemIndex)
740
+ }
741
+
742
+ @Test
743
+ fun `the phone lets go of its queue while remote`() {
744
+ playLocally(listOf("a", "b"))
745
+
746
+ remote.startSession()
747
+ idle()
748
+
749
+ assertTrue(local.items.isEmpty())
750
+ }
751
+
752
+ @Test
753
+ fun `the last item's end stands where the receiver ended it`() {
754
+ playLocally(listOf("a"))
755
+ remote.startSession()
756
+ idle()
757
+ remote.player.positionMs = 200_000
758
+ remote.player.update()
759
+
760
+ remote.end()
761
+ idle()
762
+
763
+ assertEquals(MediaPlayer.STATE_ENDED, player.playbackState)
764
+ assertEquals(200_000, player.currentPosition)
765
+ }
766
+
767
+ @Test
768
+ fun `release brings playback back and lets every remote go`() {
769
+ playLocally(listOf("a"))
770
+ remote.startSession()
771
+ idle()
772
+
773
+ player.release()
774
+ idle()
775
+
776
+ assertEquals(listOf("a"), local.items.map { it.mediaId })
777
+ assertTrue(remote.released)
778
+ assertTrue(other.released)
779
+ }
780
+
781
+ @Test
782
+ fun `release while a handover resolves keeps playback local`() {
783
+ playLocally(listOf("a"))
784
+ gate = CompletableDeferred()
785
+ remote.startSession()
786
+ idle()
787
+
788
+ player.release()
789
+ gate!!.complete(Unit)
790
+ idle()
791
+
792
+ assertEquals(0, local.stops)
793
+ assertTrue(remote.player.loads.isEmpty())
794
+ }
795
+
796
+ private fun playLocally(ids: List<String>, index: Int = 0, positionMs: Long = 0) {
797
+ local.setMediaItems(items(*ids.toTypedArray()), index, positionMs)
798
+ local.playWhenReady = true
799
+ local.prepare()
800
+ idle()
801
+ }
802
+
803
+ private fun queue() = (0 until player.mediaItemCount).map { player.getMediaItemAt(it).mediaId }
804
+
805
+ private fun SentLoad.short() = Triple(item.mediaId, positionMs, autoplay)
806
+
807
+ private fun items(vararg ids: String) = ids.map(::item)
808
+
809
+ private fun item(id: String) = MediaItem.Builder().setMediaId(id).setUri("https://s/$id").build()
810
+
811
+ private fun titled(title: String) =
812
+ androidx.media3.common.MediaMetadata.Builder().setTitle(title).build()
813
+
814
+ private fun idle() = shadowOf(looper).idle()
815
+ }