react-native-audio-browser 0.2.0-next.84 → 0.2.0-next.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1135,6 +1135,7 @@ internal constructor(
1135
1135
  // The next play is a new Load.
1136
1136
  retryLoop?.suspend()
1137
1137
  sessionPlayer.stop()
1138
+ playerListener.fenceOutgoingMetadata()
1138
1139
  // Idle at its end, the next prepare would end it again at once.
1139
1140
  if (ended) sessionPlayer.seekToDefaultPosition()
1140
1141
  }
@@ -31,17 +31,17 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
31
31
  @MediaPlayer.State private var lastEngineState = MediaPlayer.STATE_IDLE
32
32
 
33
33
  /**
34
- * Set from an app-started track change until the playback thread has handled it. Until then every
35
- * metadata delivery is the outgoing item's: its renderer posted them to the main looper before
36
- * the change, and nothing removes them.
34
+ * Set from an app-started command that ends the stream until the playback thread has handled it.
35
+ * Until then every metadata delivery is the outgoing stream's: its renderer posted them to the
36
+ * main looper before the command, and nothing removes them.
37
37
  */
38
38
  private var metadataFence: Any? = null
39
39
 
40
40
  /**
41
- * Its target runs on the playback thread right after the command that changed the item. A
42
- * main-looper target goes to the back of that thread's queue, where a release would drop it.
41
+ * Call right after the command. Its target runs on the playback thread right after that command.
42
+ * A main-looper target goes to the back of that thread's queue, where a release would drop it.
43
43
  */
44
- private fun fenceOutgoingMetadata() {
44
+ internal fun fenceOutgoingMetadata() {
45
45
  val fence = Any()
46
46
  metadataFence = fence
47
47
  player.exoPlayer
@@ -98,6 +98,13 @@ internal class RemoteSwitchingPlayer(
98
98
  class Ended(val positionMs: Long) : Load
99
99
  }
100
100
 
101
+ /**
102
+ * The phone's metadata last reported. A block is reported once, and as none from the next state
103
+ * on, so that an equal block after it goes out as ExoPlayer sends it: the state emits only a
104
+ * change, and a stream's first title after a reconnect often equals its last.
105
+ */
106
+ private var reportedMetadata: Metadata? = null
107
+
101
108
  init {
102
109
  setRemotes(remotes)
103
110
  }
@@ -137,22 +144,9 @@ internal class RemoteSwitchingPlayer(
137
144
  private val remoteRoute: Route.Remote?
138
145
  get() = route as? Route.Remote
139
146
 
140
- /** For the next [getState] to take [titleAtStop]: super.getState() consumes pending events. */
141
- private var localStopPending = false
142
-
143
- /**
144
- * The phone's metadata as it stopped, reported as none until the phone sends more. The state
145
- * emits only a change, and a stream's first title after a reconnect often equals its last.
146
- */
147
- private var titleAtStop: Metadata? = null
148
-
149
- private fun withoutStoppedTitle(state: State): State {
150
- if (localStopPending) {
151
- localStopPending = false
152
- titleAtStop = state.timedMetadata
153
- }
154
- if (state.timedMetadata !== titleAtStop) {
155
- titleAtStop = null
147
+ private fun withEachBlockOnce(state: State): State {
148
+ if (state.timedMetadata !== reportedMetadata) {
149
+ reportedMetadata = state.timedMetadata
156
150
  return state
157
151
  }
158
152
  return state.buildUpon().setTimedMetadata(NO_TIMED_METADATA).build()
@@ -464,7 +458,7 @@ internal class RemoteSwitchingPlayer(
464
458
 
465
459
  override fun getState(): State {
466
460
  val state = super.getState()
467
- val remote = remoteRoute ?: return withoutStoppedTitle(state)
461
+ val remote = remoteRoute ?: return withEachBlockOnce(state)
468
462
  val load = remote.load
469
463
  val holds = load is Load.Playing && receiverHolds(remote, load.item)
470
464
  // Duration, seekability and liveness come from the receiver's data for the item it plays.
@@ -565,10 +559,7 @@ internal class RemoteSwitchingPlayer(
565
559
 
566
560
  override fun handleStop(): ListenableFuture<*> {
567
561
  val remote = remoteRoute
568
- if (remote == null) {
569
- localStopPending = true
570
- return super.handleStop()
571
- }
562
+ if (remote == null) return super.handleStop()
572
563
  val positionMs = positionOn(remote)
573
564
  cancelLoad(remote)
574
565
  stopReceiver(remote.receiver)
@@ -7,9 +7,11 @@ import android.net.NetworkCapabilities
7
7
  import android.net.Uri
8
8
  import android.os.Looper
9
9
  import androidx.media3.common.C
10
+ import androidx.media3.common.Metadata
10
11
  import androidx.media3.common.PlaybackException
11
12
  import androidx.media3.common.Player as MediaPlayer
12
13
  import androidx.media3.common.util.StuckPlayerException
14
+ import androidx.media3.extractor.metadata.icy.IcyInfo
13
15
  import com.audiobrowser.Callbacks
14
16
  import com.audiobrowser.TestFixtures
15
17
  import com.audiobrowser.goOnline
@@ -517,6 +519,21 @@ class PlayerRetryTest {
517
519
  assertNotEquals(PlaybackState.ERROR, player.playbackState)
518
520
  }
519
521
 
522
+ @Test
523
+ fun `a title after a reconnect inside the retry loop shows`() {
524
+ goOnline(context)
525
+ play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
526
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
527
+ player.exoPlayer.stop()
528
+ player.playerListener.onPlayerError(stuck())
529
+ advanceSeconds(2)
530
+ assertNotEquals("no reconnect", MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
531
+
532
+ player.playerListener.onMetadata(Metadata(IcyInfo(ByteArray(0), "Song", null)))
533
+
534
+ assertEquals("Song", player.nowPlaying.timedMetadata?.title)
535
+ }
536
+
520
537
  @Test
521
538
  fun `with retry off, a stuck engine ends in the stalled error`() {
522
539
  play(Uri.fromFile(silentWav()).toString())
@@ -182,6 +182,29 @@ class PlayerTrackChangeTest {
182
182
  assertEquals(listOf("A's city", "A's city"), shownSecondLines())
183
183
  }
184
184
 
185
+ @Test
186
+ fun `a title the stream had already posted does not survive a stop`() {
187
+ playStationWithTitle()
188
+ val heard = mutableListOf<TimedMetadata>()
189
+ player.callbacks =
190
+ mock(Callbacks::class.java) { invocation ->
191
+ if (invocation.method.name == "onTimedMetadata") heard.add(invocation.getArgument(0))
192
+ Answers.RETURNS_DEFAULTS.answer(invocation)
193
+ }
194
+ val playbackLooper = shadowOf(player.exoPlayer.playbackLooper)
195
+ playbackLooper.pause()
196
+ Handler(player.exoPlayer.playbackLooper).post {
197
+ Handler(Looper.getMainLooper()).post { player.playerListener.onMetadata(icy("A song 1")) }
198
+ }
199
+ player.stop()
200
+ playbackLooper.unPause()
201
+ idle()
202
+
203
+ assertEquals(emptyList<TimedMetadata>(), heard)
204
+ assertNull(player.nowPlaying.timedMetadata)
205
+ assertEquals(listOf("A's city", "A's city"), shownSecondLines())
206
+ }
207
+
185
208
  @Test
186
209
  fun `a play after a stop shows the track's own lines until the stream sends a title`() {
187
210
  playStationWithTitle()
@@ -30,7 +30,7 @@ internal class FakeLocalPlayer(looper: Looper) : SimpleBasePlayer(looper) {
30
30
 
31
31
  /**
32
32
  * Sends a stream title. ExoPlayer sends each one it decodes; this state emits only a change, so a
33
- * stop clears it for the stream's first title after a reconnect to go out again.
33
+ * stop and a prepare clear it for the first title of each connection to go out.
34
34
  */
35
35
  fun title(title: String) {
36
36
  metadata = Metadata(0, IcyInfo(title.toByteArray(), title, null))
@@ -91,6 +91,7 @@ internal class FakeLocalPlayer(looper: Looper) : SimpleBasePlayer(looper) {
91
91
 
92
92
  override fun handlePrepare(): ListenableFuture<*> {
93
93
  readiness = STATE_READY
94
+ metadata = Metadata(C.TIME_UNSET)
94
95
  prepares++
95
96
  return done()
96
97
  }
@@ -927,6 +927,43 @@ class RemoteSwitchingPlayerTest {
927
927
  assertEquals(listOf("Song"), seen)
928
928
  }
929
929
 
930
+ @Test
931
+ fun `each title the phone sends after a stop goes out, though it equals the last`() {
932
+ playLocally(listOf("a"))
933
+ local.title("Song")
934
+ idle()
935
+ val seen = transitionsAndTitles()
936
+
937
+ player.stop()
938
+ idle()
939
+ // A title that runs after the stop: passed on here, and dropped by the player's listener.
940
+ local.title("Song")
941
+ idle()
942
+ player.prepare()
943
+ idle()
944
+ local.title("Song")
945
+ idle()
946
+
947
+ assertEquals(listOf("Song", "Song"), seen)
948
+ }
949
+
950
+ @Test
951
+ fun `a title after the phone reconnects without a stop goes out, though it equals the last`() {
952
+ playLocally(listOf("a"))
953
+ local.title("Song")
954
+ idle()
955
+ val seen = transitionsAndTitles()
956
+
957
+ // As the retry loop reconnects: on the phone, not through this player.
958
+ local.stop()
959
+ local.prepare()
960
+ idle()
961
+ local.title("Song")
962
+ idle()
963
+
964
+ assertEquals(listOf("Song"), seen)
965
+ }
966
+
930
967
  @Test
931
968
  fun `release brings playback back and lets every remote go`() {
932
969
  playLocally(listOf("a"))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.84",
3
+ "version": "0.2.0-next.85",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",