react-native-audio-browser 0.2.0-next.87 → 0.2.0-next.88

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.
@@ -14,11 +14,14 @@ import androidx.media3.common.Tracks
14
14
  import androidx.media3.common.VideoSize
15
15
  import androidx.media3.common.text.Cue
16
16
  import androidx.media3.common.text.CueGroup
17
+ import androidx.media3.exoplayer.source.ForwardingTimeline
17
18
 
18
19
  /**
19
20
  * A session listener hearing the engine's events with [view]'s state and playing, and never its
20
21
  * errors, which [InterceptingPlayer.getPlayerError] hides: the session copies an event's own
21
- * arguments, so the view has to be in them. Every other event passes as it is.
22
+ * arguments, so the view has to be in them. A state, playing, timeline or position it was already
23
+ * told is not told again: the session pushes each one to legacy controllers whether or not it
24
+ * changed. Every other event passes as it is.
22
25
  */
23
26
  internal class SessionViewListener(
24
27
  private val target: MediaPlayer.Listener,
@@ -26,17 +29,19 @@ internal class SessionViewListener(
26
29
  ) : MediaPlayer.Listener {
27
30
  private var toldState = view.playbackState
28
31
  private var toldPlaying = view.isPlaying
32
+ private var toldTimeline = view.currentTimeline
29
33
 
30
34
  /**
31
35
  * Tells the session the view's state and playing where they moved since it was last told: the
32
36
  * view can move with no engine event, as when the retry loop lets go of an idle engine.
33
37
  */
34
38
  fun refresh() {
35
- if (view.playbackState != toldState) onPlaybackStateChanged(view.playbackState)
36
- if (view.isPlaying != toldPlaying) onIsPlayingChanged(view.isPlaying)
39
+ onPlaybackStateChanged(view.playbackState)
40
+ onIsPlayingChanged(view.isPlaying)
37
41
  }
38
42
 
39
43
  override fun onPlaybackStateChanged(playbackState: Int) {
44
+ if (view.playbackState == toldState) return
40
45
  toldState = view.playbackState
41
46
  target.onPlaybackStateChanged(toldState)
42
47
  }
@@ -46,6 +51,7 @@ internal class SessionViewListener(
46
51
  @Suppress("DEPRECATION") target.onPlayerStateChanged(view.playWhenReady, view.playbackState)
47
52
 
48
53
  override fun onIsPlayingChanged(isPlaying: Boolean) {
54
+ if (view.isPlaying == toldPlaying) return
49
55
  toldPlaying = view.isPlaying
50
56
  target.onIsPlayingChanged(toldPlaying)
51
57
  }
@@ -57,8 +63,11 @@ internal class SessionViewListener(
57
63
  override fun onEvents(player: MediaPlayer, events: MediaPlayer.Events) =
58
64
  target.onEvents(player, events)
59
65
 
60
- override fun onTimelineChanged(timeline: Timeline, reason: Int) =
66
+ override fun onTimelineChanged(timeline: Timeline, reason: Int) {
67
+ if (IgnoringPlaceholders(timeline) == IgnoringPlaceholders(toldTimeline)) return
68
+ toldTimeline = timeline
61
69
  target.onTimelineChanged(timeline, reason)
70
+ }
62
71
 
63
72
  override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) =
64
73
  target.onMediaItemTransition(mediaItem, reason)
@@ -102,7 +111,10 @@ internal class SessionViewListener(
102
111
  oldPosition: MediaPlayer.PositionInfo,
103
112
  newPosition: MediaPlayer.PositionInfo,
104
113
  reason: Int,
105
- ) = target.onPositionDiscontinuity(oldPosition, newPosition, reason)
114
+ ) {
115
+ if (oldPosition == newPosition) return
116
+ target.onPositionDiscontinuity(oldPosition, newPosition, reason)
117
+ }
106
118
 
107
119
  override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) =
108
120
  target.onPlaybackParametersChanged(playbackParameters)
@@ -146,3 +158,11 @@ internal class SessionViewListener(
146
158
 
147
159
  override fun onMetadata(metadata: Metadata) = target.onMetadata(metadata)
148
160
  }
161
+
162
+ /**
163
+ * Reads every period as real: a re-prepare marks the same period a placeholder, then real again.
164
+ */
165
+ private class IgnoringPlaceholders(timeline: Timeline) : ForwardingTimeline(timeline) {
166
+ override fun getPeriod(periodIndex: Int, period: Period, setIds: Boolean): Period =
167
+ super.getPeriod(periodIndex, period, setIds).apply { isPlaceholder = false }
168
+ }
@@ -1,10 +1,51 @@
1
1
  package com.audiobrowser.player
2
2
 
3
+ import androidx.media3.common.C
4
+ import androidx.media3.common.MediaItem
3
5
  import androidx.media3.common.Player as MediaPlayer
6
+ import androidx.media3.common.Timeline
7
+ import androidx.media3.exoplayer.source.ForwardingTimeline
8
+ import androidx.media3.exoplayer.source.SinglePeriodTimeline
4
9
  import org.junit.Assert.assertEquals
5
10
  import org.junit.Test
11
+ import org.junit.runner.RunWith
12
+ import org.mockito.Mockito.mock
13
+ import org.mockito.Mockito.`when`
14
+ import org.robolectric.RobolectricTestRunner
6
15
 
16
+ @RunWith(RobolectricTestRunner::class)
7
17
  class SessionViewListenerTest {
18
+
19
+ private val view = mock(MediaPlayer::class.java)
20
+ private val told = mutableListOf<String>()
21
+ private val target =
22
+ object : MediaPlayer.Listener {
23
+ override fun onPlaybackStateChanged(playbackState: Int) {
24
+ told += "state $playbackState"
25
+ }
26
+
27
+ override fun onIsPlayingChanged(isPlaying: Boolean) {
28
+ told += "playing $isPlaying"
29
+ }
30
+
31
+ override fun onTimelineChanged(timeline: Timeline, reason: Int) {
32
+ told += "timeline"
33
+ }
34
+
35
+ override fun onPositionDiscontinuity(
36
+ oldPosition: MediaPlayer.PositionInfo,
37
+ newPosition: MediaPlayer.PositionInfo,
38
+ reason: Int,
39
+ ) {
40
+ told += "position ${newPosition.positionMs}"
41
+ }
42
+ }
43
+
44
+ private fun listener(): SessionViewListener {
45
+ `when`(view.currentTimeline).thenReturn(Timeline.EMPTY)
46
+ return SessionViewListener(target, view)
47
+ }
48
+
8
49
  @Test
9
50
  fun `forwards every callback media3's listener declares`() {
10
51
  val declared =
@@ -18,4 +59,167 @@ class SessionViewListenerTest {
18
59
 
19
60
  assertEquals(emptyList<Any>(), declared.filterNot { it in overridden })
20
61
  }
62
+
63
+ @Test
64
+ fun `tells the view's state once each time it moves`() {
65
+ `when`(view.playbackState).thenReturn(MediaPlayer.STATE_BUFFERING)
66
+ val listener = listener()
67
+
68
+ listener.onPlaybackStateChanged(MediaPlayer.STATE_IDLE)
69
+ `when`(view.playbackState).thenReturn(MediaPlayer.STATE_READY)
70
+ listener.onPlaybackStateChanged(MediaPlayer.STATE_READY)
71
+ listener.onPlaybackStateChanged(MediaPlayer.STATE_READY)
72
+
73
+ assertEquals(listOf("state ${MediaPlayer.STATE_READY}"), told)
74
+ }
75
+
76
+ @Test
77
+ fun `tells the view's playing once each time it moves`() {
78
+ val listener = listener()
79
+
80
+ listener.onIsPlayingChanged(true)
81
+ `when`(view.isPlaying).thenReturn(true)
82
+ listener.onIsPlayingChanged(true)
83
+ listener.onIsPlayingChanged(true)
84
+
85
+ assertEquals(listOf("playing true"), told)
86
+ }
87
+
88
+ @Test
89
+ fun `tells a timeline that changes, and not its period turning placeholder and back`() {
90
+ val listener = listener()
91
+ val a = timeline("a", durationUs = 60_000_000)
92
+
93
+ listener.onTimelineChanged(a, MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED)
94
+ listener.onTimelineChanged(
95
+ PlaceholderPeriods(a),
96
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
97
+ )
98
+ listener.onTimelineChanged(
99
+ timeline("a", durationUs = 60_000_000),
100
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
101
+ )
102
+ assertEquals(listOf("timeline"), told)
103
+
104
+ listener.onTimelineChanged(
105
+ PeriodAt(a, positionInWindowUs = -1_000_000),
106
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
107
+ )
108
+ listener.onTimelineChanged(
109
+ timeline("a", durationUs = 30_000_000),
110
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
111
+ )
112
+ listener.onTimelineChanged(
113
+ timeline("b", durationUs = 30_000_000),
114
+ MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
115
+ )
116
+ listener.onTimelineChanged(Timeline.EMPTY, MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED)
117
+ assertEquals(List(5) { "timeline" }, told)
118
+ }
119
+
120
+ @Test
121
+ fun `tells a queue whose shuffle order changes`() {
122
+ val listener = listener()
123
+
124
+ listener.onTimelineChanged(
125
+ TwoItems(shuffled = false),
126
+ MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
127
+ )
128
+ listener.onTimelineChanged(
129
+ TwoItems(shuffled = true),
130
+ MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
131
+ )
132
+
133
+ assertEquals(listOf("timeline", "timeline"), told)
134
+ }
135
+
136
+ @Test
137
+ fun `tells a position that moves, and not a seek to where it already is`() {
138
+ val listener = listener()
139
+
140
+ listener.onPositionDiscontinuity(
141
+ position(120_000),
142
+ position(0),
143
+ MediaPlayer.DISCONTINUITY_REASON_SEEK,
144
+ )
145
+ listener.onPositionDiscontinuity(
146
+ position(0),
147
+ position(0),
148
+ MediaPlayer.DISCONTINUITY_REASON_SEEK,
149
+ )
150
+
151
+ assertEquals(listOf("position 0"), told)
152
+ }
153
+
154
+ private fun position(positionMs: Long) =
155
+ MediaPlayer.PositionInfo(UID, 0, UID, 0, positionMs, positionMs, C.INDEX_UNSET, C.INDEX_UNSET)
156
+
157
+ private fun timeline(mediaId: String, durationUs: Long): Timeline =
158
+ SinglePeriodTimeline(
159
+ durationUs,
160
+ /* isSeekable= */ true,
161
+ /* isDynamic= */ false,
162
+ /* useLiveConfiguration= */ false,
163
+ /* manifest= */ null,
164
+ MediaItem.Builder().setMediaId(mediaId).build(),
165
+ )
166
+
167
+ private companion object {
168
+ val UID = Any()
169
+ }
170
+
171
+ /** Two items, in reverse when shuffled. */
172
+ private class TwoItems(private val shuffled: Boolean) : Timeline() {
173
+ override fun getWindowCount() = 2
174
+
175
+ override fun getWindow(index: Int, window: Window, defaultPositionProjectionUs: Long) =
176
+ window.set(
177
+ index,
178
+ MediaItem.Builder().setMediaId("$index").build(),
179
+ null,
180
+ C.TIME_UNSET,
181
+ C.TIME_UNSET,
182
+ C.TIME_UNSET,
183
+ true,
184
+ false,
185
+ null,
186
+ 0,
187
+ C.TIME_UNSET,
188
+ index,
189
+ index,
190
+ 0,
191
+ )
192
+
193
+ override fun getPeriodCount() = 2
194
+
195
+ override fun getPeriod(index: Int, period: Period, setIds: Boolean): Period =
196
+ period.set(index, index, index, C.TIME_UNSET, 0)
197
+
198
+ override fun getIndexOfPeriod(uid: Any) = uid as? Int ?: C.INDEX_UNSET
199
+
200
+ override fun getUidOfPeriod(index: Int): Any = index
201
+
202
+ override fun getFirstWindowIndex(shuffleModeEnabled: Boolean) =
203
+ if (shuffleModeEnabled && shuffled) 1 else 0
204
+
205
+ override fun getLastWindowIndex(shuffleModeEnabled: Boolean) =
206
+ if (shuffleModeEnabled && shuffled) 0 else 1
207
+
208
+ override fun getNextWindowIndex(index: Int, repeatMode: Int, shuffleModeEnabled: Boolean) =
209
+ if (index == getLastWindowIndex(shuffleModeEnabled)) C.INDEX_UNSET
210
+ else if (shuffleModeEnabled && shuffled) index - 1 else index + 1
211
+ }
212
+
213
+ private class PeriodAt(timeline: Timeline, private val positionInWindowUs: Long) :
214
+ ForwardingTimeline(timeline) {
215
+ override fun getPeriod(periodIndex: Int, period: Period, setIds: Boolean): Period =
216
+ super.getPeriod(periodIndex, period, setIds).apply {
217
+ positionInWindowUs = this@PeriodAt.positionInWindowUs
218
+ }
219
+ }
220
+
221
+ private class PlaceholderPeriods(timeline: Timeline) : ForwardingTimeline(timeline) {
222
+ override fun getPeriod(periodIndex: Int, period: Period, setIds: Boolean): Period =
223
+ super.getPeriod(periodIndex, period, setIds).apply { isPlaceholder = true }
224
+ }
21
225
  }
@@ -3,8 +3,10 @@ package com.audiobrowser.player
3
3
  import android.os.Looper
4
4
  import androidx.media3.common.C
5
5
  import androidx.media3.common.Player as MediaPlayer
6
+ import androidx.media3.common.Timeline
6
7
  import androidx.media3.session.MediaSession
7
8
  import androidx.media3.session.controllerTitle
9
+ import androidx.media3.session.countLegacyStateUpdates
8
10
  import androidx.media3.session.legacy.PlaybackStateCompat
9
11
  import androidx.media3.session.legacyState
10
12
  import com.audiobrowser.Callbacks
@@ -12,10 +14,17 @@ import com.audiobrowser.TestFixtures
12
14
  import com.audiobrowser.goOnline
13
15
  import com.audiobrowser.model.PlayerSetupOptions
14
16
  import com.audiobrowser.model.RetryPolicy
17
+ import com.audiobrowser.silentWavBytes
15
18
  import com.audiobrowser.silentWavUri
16
19
  import com.audiobrowser.util.TrackFactory
17
20
  import com.margelo.nitro.audiobrowser.PlaybackState
21
+ import java.io.IOException
22
+ import java.net.ServerSocket
18
23
  import java.time.Duration
24
+ import java.util.concurrent.CountDownLatch
25
+ import java.util.concurrent.TimeUnit
26
+ import java.util.concurrent.atomic.AtomicInteger
27
+ import kotlin.concurrent.thread
19
28
  import org.junit.After
20
29
  import org.junit.Assert.assertEquals
21
30
  import org.junit.Assert.fail
@@ -128,12 +137,71 @@ class SessionViewTest {
128
137
  assertEquals("T", session.controllerTitle())
129
138
  }
130
139
 
140
+ @Test
141
+ fun `a reconnect attempt tells the session nothing it was already told`() {
142
+ val server = ServerSocket(0)
143
+ val connections = AtomicInteger()
144
+ val drop = CountDownLatch(1)
145
+ // Serves five seconds of a stream until the drop, then ends each connection unanswered.
146
+ thread(isDaemon = true) {
147
+ server.accept().use { socket ->
148
+ val request = socket.getInputStream().bufferedReader()
149
+ while (!request.readLine().isNullOrEmpty()) Unit
150
+ socket.getOutputStream().run {
151
+ write("HTTP/1.1 200 OK\r\nContent-Type: audio/wav\r\n\r\n".toByteArray())
152
+ write(silentWavBytes(Duration.ofMinutes(1)), 0, 40_000)
153
+ flush()
154
+ }
155
+ drop.await(30, TimeUnit.SECONDS)
156
+ }
157
+ while (!server.isClosed) {
158
+ try {
159
+ server.accept().close()
160
+ connections.incrementAndGet()
161
+ } catch (_: IOException) {
162
+ return@thread
163
+ }
164
+ }
165
+ }
166
+ try {
167
+ play(RetryPolicy.Infinite(), "http://127.0.0.1:${server.localPort}/live", live = true)
168
+ idleUntil("PLAYING") { player.playbackState == PlaybackState.PLAYING }
169
+ drop.countDown()
170
+ // Stands in for the drop, which the engine never reads: its disabled audio track stops it
171
+ // reading the stream.
172
+ player.exoPlayer.stop()
173
+ player.exoPlayer.prepare()
174
+ idleUntil("the first reconnect") { connections.get() >= 1 }
175
+ val states = session.countLegacyStateUpdates()
176
+ val timelines = AtomicInteger()
177
+ player.forwardingPlayer.addListener(
178
+ object : MediaPlayer.Listener {
179
+ override fun onTimelineChanged(timeline: Timeline, reason: Int) {
180
+ timelines.incrementAndGet()
181
+ }
182
+ }
183
+ )
184
+
185
+ idleUntil("three more reconnects") { connections.get() >= 4 }
186
+
187
+ assertEquals(PlaybackState.BUFFERING, player.playbackState)
188
+ assertEquals(0, states())
189
+ assertEquals(0, timelines.get())
190
+ } finally {
191
+ server.close()
192
+ }
193
+ }
194
+
131
195
  private fun failTerminally() {
132
196
  play(RetryPolicy.Limited(maxRetries = 0))
133
197
  idleUntil("a terminal error") { player.playbackState == PlaybackState.ERROR }
134
198
  }
135
199
 
136
- private fun play(retryPolicy: RetryPolicy, src: String = "https://127.0.0.1:9/stream") {
200
+ private fun play(
201
+ retryPolicy: RetryPolicy,
202
+ src: String = "https://127.0.0.1:9/stream",
203
+ live: Boolean? = null,
204
+ ) {
137
205
  goOnline(context)
138
206
  player.setup(PlayerSetupOptions(retryPolicy = retryPolicy))
139
207
  player.setCallbacks(callbacks)
@@ -144,7 +212,9 @@ class SessionViewTest {
144
212
  .buildUpon()
145
213
  .setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
146
214
  .build()
147
- player.exoPlayer.setMediaItems(listOf(TrackFactory.toMedia3(TestFixtures.track(src = src))))
215
+ player.exoPlayer.setMediaItems(
216
+ listOf(TrackFactory.toMedia3(TestFixtures.track(src = src).copy(live = live)))
217
+ )
148
218
  player.exoPlayer.prepare()
149
219
  player.play()
150
220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.87",
3
+ "version": "0.2.0-next.88",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",