react-native-audio-browser 0.2.0-next.93 → 0.2.0-next.94

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.
@@ -133,7 +133,7 @@ class MediaFactory(
133
133
  // One policy per item: it tells the playing item's failures from a preload's.
134
134
  if (retrying)
135
135
  factory.setLoadErrorHandlingPolicy(
136
- RetryLoadErrorHandlingPolicy(sources, { isCurrent(mediaItem) }, onLoadFailure)
136
+ RetryLoadErrorHandlingPolicy(sources, { isCurrent(mediaItem) }, cache, onLoadFailure)
137
137
  )
138
138
  val source = factory.setDataSourceFactory(dataSourceFactory).createMediaSource(mediaItem)
139
139
  // Adaptive sources retry in place: the policy never ends their connection.
@@ -1205,8 +1205,8 @@ internal constructor(
1205
1205
  }
1206
1206
 
1207
1207
  /**
1208
- * Bumped on track change so an [onLoadFailure] post for the outgoing track is dropped. Written on
1209
- * main, read on ExoPlayer's playback thread.
1208
+ * Bumped on track change and seek so an [onLoadFailure] post for an earlier connection is
1209
+ * dropped. Written on main, read on ExoPlayer's playback thread.
1210
1210
  */
1211
1211
  @Volatile private var retryingErrorGeneration = 0
1212
1212
 
@@ -1228,6 +1228,11 @@ internal constructor(
1228
1228
  }
1229
1229
  }
1230
1230
 
1231
+ /** Drops the [onLoadFailure] reports still on their way: they belong to an earlier connection. */
1232
+ internal fun dropFailureReports() {
1233
+ retryingErrorGeneration++
1234
+ }
1235
+
1231
1236
  private fun showRetryingError(error: PlaybackError) {
1232
1237
  // Paused, the retry would run unseen and its failure be terminal. STOPPED, ERROR and ENDED
1233
1238
  // reach here only from a post that raced them.
@@ -1312,7 +1317,7 @@ internal constructor(
1312
1317
  * with no state change to emit the clear.
1313
1318
  */
1314
1319
  internal fun clearRetryingError() {
1315
- retryingErrorGeneration++
1320
+ dropFailureReports()
1316
1321
  if (playbackError?.retrying != true) return
1317
1322
  playbackError = null
1318
1323
  callbacks?.onPlaybackChanged(Playback(playbackState, null))
@@ -79,13 +79,13 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
79
79
  player.oldPosition = oldPosition.positionMs
80
80
  // A seek opens a new connection without passing through idle, clearing a failed one's error.
81
81
  val loop = player.retryLoop
82
- if (
83
- reason == MediaPlayer.DISCONTINUITY_REASON_SEEK &&
84
- loop?.failurePending == true &&
85
- player.localEngine.isActive
86
- ) {
87
- player.clearRetryingError()
88
- loop.connectionStarted()
82
+ if (reason == MediaPlayer.DISCONTINUITY_REASON_SEEK && player.localEngine.isActive) {
83
+ if (loop?.failurePending == true) {
84
+ player.clearRetryingError()
85
+ loop.connectionStarted()
86
+ } else {
87
+ player.dropFailureReports()
88
+ }
89
89
  }
90
90
  }
91
91
 
@@ -3,7 +3,11 @@ package com.audiobrowser.player
3
3
  import androidx.media3.common.C
4
4
  import androidx.media3.common.ParserException
5
5
  import androidx.media3.datasource.DataSourceException
6
+ import androidx.media3.datasource.DataSpec
6
7
  import androidx.media3.datasource.HttpDataSource
8
+ import androidx.media3.datasource.cache.Cache
9
+ import androidx.media3.datasource.cache.CacheKeyFactory
10
+ import androidx.media3.datasource.cache.ContentMetadata
7
11
  import androidx.media3.exoplayer.source.LoadEventInfo
8
12
  import androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy
9
13
  import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
@@ -20,11 +24,13 @@ import java.io.IOException
20
24
  * loop's silence deadline does. Called on ExoPlayer's playback thread.
21
25
  *
22
26
  * @param isCurrent whether the item is the one playing; read on the playback thread
27
+ * @param cache the cache the item's loads read through, if any
23
28
  * @param onFailure each recoverable failure of the current item, and whether it is retried in place
24
29
  */
25
30
  class RetryLoadErrorHandlingPolicy(
26
31
  private val sources: Sources,
27
32
  private val isCurrent: () -> Boolean,
33
+ private val cache: Cache? = null,
28
34
  private val onFailure: (exception: IOException, inPlace: Boolean) -> Unit,
29
35
  ) : DefaultLoadErrorHandlingPolicy() {
30
36
 
@@ -67,10 +73,22 @@ class RetryLoadErrorHandlingPolicy(
67
73
  private val knownLength = mutableSetOf<Long>()
68
74
 
69
75
  private fun hasKnownLength(info: LoadEventInfo): Boolean {
70
- if (declaresLength(info.responseHeaders)) knownLength += info.loadTaskId
76
+ if (
77
+ declaresLength(info.responseHeaders) ||
78
+ (info.responseHeaders.isEmpty() && cachedLengthKnown(info.dataSpec))
79
+ )
80
+ knownLength += info.loadTaskId
71
81
  return info.loadTaskId in knownLength
72
82
  }
73
83
 
84
+ /** A load reading from the cache has no response: the cache keeps the length one declared. */
85
+ private fun cachedLengthKnown(dataSpec: DataSpec): Boolean {
86
+ val cache = cache ?: return false
87
+ val key = CacheKeyFactory.DEFAULT.buildCacheKey(dataSpec)
88
+ return ContentMetadata.getContentLength(cache.getContentMetadata(key)) !=
89
+ C.LENGTH_UNSET.toLong()
90
+ }
91
+
74
92
  /** Never media3's verdict: the in-place path retries until the loop gives up on it. */
75
93
  override fun getMinimumLoadableRetryCount(dataType: Int): Int = Int.MAX_VALUE
76
94
 
@@ -600,6 +600,37 @@ class PlayerRetryTest {
600
600
  assertEquals(null, player.playbackError)
601
601
  }
602
602
 
603
+ @Test
604
+ fun `a seek over a held connection failure is a new connection for the loop`() {
605
+ goOnline(context)
606
+ play(folder.silentWavUri(Duration.ofMinutes(1)), retryPolicy = RetryPolicy.Infinite())
607
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
608
+ player.onLoadFailure(IOException("dropped"), inPlace = false)
609
+ shadowOf(Looper.getMainLooper()).idle()
610
+ assertTrue("the failure is held", player.retryLoop!!.failurePending)
611
+
612
+ player.seekTo(10, TimeUnit.SECONDS)
613
+ shadowOf(Looper.getMainLooper()).idle()
614
+
615
+ assertFalse(player.retryLoop!!.failurePending)
616
+ assertFalse(player.retryLoop!!.reconnecting)
617
+ }
618
+
619
+ @Test
620
+ fun `a failure report that arrives after a seek is not the new connection's`() {
621
+ goOnline(context)
622
+ play(folder.silentWavUri(Duration.ofMinutes(1)), retryPolicy = RetryPolicy.Infinite())
623
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
624
+
625
+ // Posted from the playback thread before the seek, run after it.
626
+ player.onLoadFailure(IOException("dropped"), inPlace = false)
627
+ player.seekTo(10, TimeUnit.SECONDS)
628
+ shadowOf(Looper.getMainLooper()).idle()
629
+
630
+ assertFalse(player.retryLoop!!.failurePending)
631
+ assertEquals(null, player.playbackError)
632
+ }
633
+
603
634
  @Test
604
635
  fun `with retry, a stuck engine reconnects and never ends in an error`() {
605
636
  goOnline(context)
@@ -3,9 +3,14 @@ package com.audiobrowser.player
3
3
  import android.net.Uri
4
4
  import androidx.media3.common.C
5
5
  import androidx.media3.common.ParserException
6
+ import androidx.media3.database.StandaloneDatabaseProvider
6
7
  import androidx.media3.datasource.DataSourceException
7
8
  import androidx.media3.datasource.DataSpec
8
9
  import androidx.media3.datasource.HttpDataSource
10
+ import androidx.media3.datasource.cache.Cache
11
+ import androidx.media3.datasource.cache.ContentMetadataMutations
12
+ import androidx.media3.datasource.cache.NoOpCacheEvictor
13
+ import androidx.media3.datasource.cache.SimpleCache
9
14
  import androidx.media3.exoplayer.source.LoadEventInfo
10
15
  import androidx.media3.exoplayer.source.MediaLoadData
11
16
  import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
@@ -15,20 +20,22 @@ import java.io.FileNotFoundException
15
20
  import java.io.IOException
16
21
  import java.net.SocketTimeoutException
17
22
  import java.net.UnknownHostException
23
+ import java.nio.file.Files
18
24
  import org.junit.Assert.assertEquals
19
25
  import org.junit.Assert.assertTrue
20
26
  import org.junit.Test
21
27
  import org.junit.runner.RunWith
22
28
  import org.mockito.Mockito.mock
23
29
  import org.robolectric.RobolectricTestRunner
30
+ import org.robolectric.RuntimeEnvironment
24
31
 
25
32
  @RunWith(RobolectricTestRunner::class)
26
33
  class RetryLoadErrorHandlingPolicyTest {
27
34
  private val failures = mutableListOf<Boolean>()
28
35
  private var current = true
29
36
 
30
- private fun policy(sources: Sources) =
31
- RetryLoadErrorHandlingPolicy(sources, { current }) { _, inPlace -> failures += inPlace }
37
+ private fun policy(sources: Sources, cache: Cache? = null) =
38
+ RetryLoadErrorHandlingPolicy(sources, { current }, cache) { _, inPlace -> failures += inPlace }
32
39
 
33
40
  private fun errorInfo(
34
41
  exception: IOException = UnknownHostException("dead host"),
@@ -102,6 +109,28 @@ class RetryLoadErrorHandlingPolicyTest {
102
109
  assertEquals(listOf(true, true, false), failures)
103
110
  }
104
111
 
112
+ @Test
113
+ fun `a file of known length read from the cache stays in place when the switch upstream fails`() {
114
+ val cache =
115
+ SimpleCache(
116
+ Files.createTempDirectory("cache").toFile(),
117
+ NoOpCacheEvictor(),
118
+ StandaloneDatabaseProvider(RuntimeEnvironment.getApplication()),
119
+ )
120
+ try {
121
+ cache.applyContentMetadataMutations(
122
+ "https://example.com/stream",
123
+ ContentMetadataMutations().also { ContentMetadataMutations.setContentLength(it, 1234) },
124
+ )
125
+ val policy = policy(Sources.PROGRESSIVE, cache)
126
+
127
+ assertEquals(1000L, policy.getRetryDelayMsFor(errorInfo()))
128
+ assertEquals(listOf(true), failures)
129
+ } finally {
130
+ cache.release()
131
+ }
132
+ }
133
+
105
134
  @Test
106
135
  fun `an item that only preloads retries in place and reports nothing`() {
107
136
  current = false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.93",
3
+ "version": "0.2.0-next.94",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",