react-native-audio-browser 0.2.0-next.67 → 0.2.0-next.68

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.
@@ -250,7 +250,9 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
250
250
  "Stuck player recovery: type=${error.stuckType} timeoutMs=${error.timeoutMs} " +
251
251
  "live=${player.isCurrentItemLive}"
252
252
  )
253
- player.resetRetryTimer()
253
+ // Offline time counts against the recovery budget, and offline media3 reports stuck every
254
+ // minute: a refill here would keep the budget from ever running out.
255
+ if (player.networkMonitor.getOnline()) player.resetRetryTimer()
254
256
  player.localEngine.retry(toLiveEdge = player.isCurrentItemLive)
255
257
  }
256
258
  StuckRecoveryPolicy.Decision.GIVE_UP -> {
@@ -1,5 +1,6 @@
1
1
  package com.audiobrowser.player
2
2
 
3
+ import android.os.SystemClock
3
4
  import androidx.media3.common.C
4
5
  import androidx.media3.common.ParserException
5
6
  import androidx.media3.datasource.DataSourceException
@@ -100,7 +101,7 @@ class RetryLoadErrorHandlingPolicy(
100
101
  override fun getRetryDelayMsFor(loadErrorInfo: LoadErrorHandlingPolicy.LoadErrorInfo): Long {
101
102
  val exception = loadErrorInfo.exception
102
103
  val errorCount = loadErrorInfo.errorCount
103
- val currentTime = System.currentTimeMillis()
104
+ val currentTime = SystemClock.elapsedRealtime()
104
105
 
105
106
  if (!shouldRetry()) {
106
107
  Timber.d("Retry skipped: shouldRetry() returned false (player likely paused)")
@@ -126,14 +127,11 @@ class RetryLoadErrorHandlingPolicy(
126
127
  return C.TIME_UNSET
127
128
  }
128
129
 
129
- val errorClassification = classifyError(exception)
130
-
131
- return if (errorClassification.isRecoverable) {
130
+ return if (isRecoverable(exception)) {
132
131
  val currentlyOffline = !isOnline()
133
- val isNetworkError = errorClassification.isNetworkError
134
132
 
135
133
  val delay =
136
- if (currentlyOffline && isNetworkError) {
134
+ if (currentlyOffline) {
137
135
  Timber.d("Device is offline, using short retry delay")
138
136
  OFFLINE_RETRY_DELAY_MS
139
137
  } else {
@@ -155,38 +153,28 @@ class RetryLoadErrorHandlingPolicy(
155
153
  }
156
154
  }
157
155
 
158
- private data class ErrorClassification(val isRecoverable: Boolean, val isNetworkError: Boolean)
159
-
160
156
  /**
161
157
  * Walks the cause chain: load errors arrive as raw IOExceptions (a Wi-Fi drop is a socket or DNS
162
- * error), never as a PlaybackException. Anything not known to be fatal counts as a network error.
158
+ * error), never as a PlaybackException. Anything not known to be fatal is recoverable.
163
159
  */
164
- private fun classifyError(exception: IOException): ErrorClassification {
165
- val nonRecoverable = ErrorClassification(isRecoverable = false, isNetworkError = false)
166
-
160
+ private fun isRecoverable(exception: IOException): Boolean {
167
161
  var current: Throwable? = exception
168
162
  while (current != null) {
169
163
  when (current) {
170
- is HttpDataSource.InvalidResponseCodeException -> {
171
- val retryable = current.responseCode in RETRYABLE_HTTP_STATUS_CODES
172
- return ErrorClassification(isRecoverable = retryable, isNetworkError = retryable)
173
- }
164
+ is HttpDataSource.InvalidResponseCodeException ->
165
+ return current.responseCode in RETRYABLE_HTTP_STATUS_CODES
174
166
  // Mirrors DefaultLoadErrorHandlingPolicy's fatal set.
175
167
  is HttpDataSource.CleartextNotPermittedException,
176
168
  is ParserException,
177
169
  is FileNotFoundException,
178
- is Loader.UnexpectedLoaderException -> return nonRecoverable
170
+ is Loader.UnexpectedLoaderException -> return false
179
171
  }
180
172
  current = current.cause
181
173
  }
182
174
 
183
175
  // A position the source can no longer serve (e.g. behind a live window) won't come back on an
184
176
  // in-place retry.
185
- if (DataSourceException.isCausedByPositionOutOfRange(exception)) {
186
- return nonRecoverable
187
- }
188
-
189
- return ErrorClassification(isRecoverable = true, isNetworkError = true)
177
+ return !DataSourceException.isCausedByPositionOutOfRange(exception)
190
178
  }
191
179
 
192
180
  override fun getMinimumLoadableRetryCount(dataType: Int): Int {
@@ -8,6 +8,7 @@ import android.os.Looper
8
8
  import androidx.media3.common.C
9
9
  import androidx.media3.common.PlaybackException
10
10
  import androidx.media3.common.Player as MediaPlayer
11
+ import androidx.media3.common.util.StuckPlayerException
11
12
  import com.audiobrowser.Callbacks
12
13
  import com.audiobrowser.TestFixtures
13
14
  import com.audiobrowser.model.PlayerSetupOptions
@@ -232,6 +233,56 @@ class PlayerRetryTest {
232
233
  assertEquals(PlaybackState.ERROR, player.playbackState)
233
234
  }
234
235
 
236
+ @Test
237
+ fun `offline, a stuck recovery leaves the recovery budget running`() {
238
+ stuckRecoveryMidBudget(online = false)
239
+
240
+ assertEquals(PlaybackState.ERROR, player.playbackState)
241
+ // The load error, not a stuck give-up.
242
+ assertEquals("not-connected-to-internet", player.playbackError?.code)
243
+ }
244
+
245
+ @Test
246
+ fun `online, a stuck recovery refills the recovery budget`() {
247
+ stuckRecoveryMidBudget(online = true)
248
+
249
+ assertEquals(true, player.playbackError?.retrying)
250
+ }
251
+
252
+ /**
253
+ * A load marked as played retries a closed port under a 30 s recovery budget, takes a stuck
254
+ * report at 25 s, and runs on to 45 s, short of media3's own 60 s stuck detection. The player is
255
+ * not idle, so the recovery's re-prepare does nothing: this covers the budget refill only.
256
+ */
257
+ private fun stuckRecoveryMidBudget(online: Boolean) {
258
+ connectivity.setNetworkCapabilities(wifi, capabilities(validated = online))
259
+ connectivity.networkCallbacks.forEach { it.onAvailable(wifi) }
260
+ play(
261
+ "https://127.0.0.1:9/stream",
262
+ retryPolicy = RetryPolicy.Infinite(maxRetryDurationMs = 30_000),
263
+ )
264
+ awaitRetry("a pending retry")
265
+ player.hasPlayedCache = true
266
+ advanceSeconds(25)
267
+ assertEquals(true, player.playbackError?.retrying)
268
+
269
+ player.playerListener.onPlayerError(
270
+ PlaybackException(
271
+ "stuck",
272
+ StuckPlayerException(StuckPlayerException.STUCK_BUFFERING_NO_PROGRESS, 60_000),
273
+ PlaybackException.ERROR_CODE_TIMEOUT,
274
+ )
275
+ )
276
+ advanceSeconds(20)
277
+ }
278
+
279
+ private fun advanceSeconds(seconds: Int) =
280
+ repeat(seconds) {
281
+ ShadowSystemClock.advanceBy(Duration.ofSeconds(1))
282
+ Thread.sleep(10)
283
+ shadowOf(Looper.getMainLooper()).idle()
284
+ }
285
+
235
286
  /** A closed port: the load fails at once, and with no retries the failure is terminal. */
236
287
  private fun failTerminally(vararg next: String) {
237
288
  play("https://127.0.0.1:9/stream", *next, retryPolicy = RetryPolicy.Limited(maxRetries = 0))
@@ -13,13 +13,18 @@ import java.io.FileNotFoundException
13
13
  import java.io.IOException
14
14
  import java.net.SocketTimeoutException
15
15
  import java.net.UnknownHostException
16
+ import java.time.Duration
16
17
  import org.junit.Assert.assertEquals
17
18
  import org.junit.Assert.assertFalse
18
19
  import org.junit.Assert.assertNotEquals
19
20
  import org.junit.Assert.assertTrue
20
21
  import org.junit.Test
22
+ import org.junit.runner.RunWith
21
23
  import org.mockito.Mockito.mock
24
+ import org.robolectric.RobolectricTestRunner
25
+ import org.robolectric.shadows.ShadowSystemClock
22
26
 
27
+ @RunWith(RobolectricTestRunner::class)
23
28
  class RetryLoadErrorHandlingPolicyTest {
24
29
  // LoadEventInfo needs an Android Uri to construct; the policy reads only the exception and the
25
30
  // error count.
@@ -138,7 +143,7 @@ class RetryLoadErrorHandlingPolicyTest {
138
143
  }
139
144
  }
140
145
 
141
- /** Clock-based: 100ms budgets and 150ms sleeps between errors leave margin both ways. */
146
+ @RunWith(RobolectricTestRunner::class)
142
147
  class RetryLoadErrorHandlingPolicyBudgetTest {
143
148
  private val loadEventInfo = org.mockito.Mockito.mock(LoadEventInfo::class.java)
144
149
  private val mediaLoadData = MediaLoadData(C.DATA_TYPE_MEDIA)
@@ -157,7 +162,7 @@ class RetryLoadErrorHandlingPolicyBudgetTest {
157
162
  RetryLoadErrorHandlingPolicy(firstConnectMaxRetryDurationMs = 100, hasPlayed = { false })
158
163
 
159
164
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
160
- Thread.sleep(150)
165
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
161
166
  assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
162
167
  }
163
168
 
@@ -167,7 +172,7 @@ class RetryLoadErrorHandlingPolicyBudgetTest {
167
172
  RetryLoadErrorHandlingPolicy(firstConnectMaxRetryDurationMs = 100, hasPlayed = { true })
168
173
 
169
174
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
170
- Thread.sleep(150)
175
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
171
176
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
172
177
  }
173
178
 
@@ -182,12 +187,12 @@ class RetryLoadErrorHandlingPolicyBudgetTest {
182
187
  )
183
188
 
184
189
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
185
- Thread.sleep(150)
190
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
186
191
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
187
192
 
188
193
  online = true
189
194
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 3)))
190
- Thread.sleep(150)
195
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
191
196
  assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 4)))
192
197
  }
193
198
 
@@ -202,13 +207,13 @@ class RetryLoadErrorHandlingPolicyBudgetTest {
202
207
  )
203
208
 
204
209
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
205
- Thread.sleep(150)
210
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
206
211
  online = false
207
212
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
208
213
 
209
214
  online = true
210
215
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 3)))
211
- Thread.sleep(150)
216
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
212
217
  assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 4)))
213
218
  }
214
219
 
@@ -218,7 +223,7 @@ class RetryLoadErrorHandlingPolicyBudgetTest {
218
223
  RetryLoadErrorHandlingPolicy(firstConnectMaxRetryDurationMs = 100, hasPlayed = { false })
219
224
 
220
225
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
221
- Thread.sleep(150)
226
+ ShadowSystemClock.advanceBy(Duration.ofMillis(150))
222
227
  policy.reset()
223
228
  assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
224
229
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.67",
3
+ "version": "0.2.0-next.68",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",
@@ -172,7 +172,8 @@ restarted from a terminal error_ (`retry()`, or any play command while in
172
172
  `'error'`), and it survives every automatic retry of that track.
173
173
 
174
174
  A new load starts fresh budgets. Within a load, the recovery budget refills
175
- after a stretch of uninterrupted playback (see below).
175
+ after a stretch of uninterrupted playback, and on Android after a recovery
176
+ (see below).
176
177
 
177
178
  How long the player keeps trying depends on one piece of evidence: **has this
178
179
  load ever produced audio?**
@@ -204,7 +205,9 @@ on.
204
205
 
205
206
  After 20 seconds of uninterrupted audible playback, the stream counts as
206
207
  recovered and the recovery budget refills (on iOS, the attempt count too), so
207
- each later dropout of a long-lived stream gets a full window.
208
+ each later dropout of a long-lived stream gets a full window. On Android, a
209
+ recovery from falling behind a live window refills it too, and so does one
210
+ from a stalled player while the device is online.
208
211
 
209
212
  The load stays the same, and so does its evidence that it has played: the
210
213
  first-connect budget does not come back.