react-native-audio-browser 0.2.0-next.73 → 0.2.0-next.74
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.
|
@@ -2,6 +2,7 @@ package com.audiobrowser.player
|
|
|
2
2
|
|
|
3
3
|
import android.net.Uri
|
|
4
4
|
import androidx.core.net.toUri
|
|
5
|
+
import androidx.media3.common.C
|
|
5
6
|
import androidx.media3.datasource.DataSource
|
|
6
7
|
import androidx.media3.datasource.DataSpec
|
|
7
8
|
import androidx.media3.datasource.TransferListener
|
|
@@ -26,6 +27,19 @@ class TransformingDataSource(private val upstream: DataSource, private val facto
|
|
|
26
27
|
// Matches the default request property MediaFactory sets upstream; a config with this user
|
|
27
28
|
// agent adds no User-Agent header of its own.
|
|
28
29
|
private const val DEFAULT_USER_AGENT = "react-native-audio-browser"
|
|
30
|
+
|
|
31
|
+
private const val OPEN_ENDED_RANGE = "bytes=0-"
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether to send `Range: bytes=0-` on a progressive read from the start, where media3 sends
|
|
35
|
+
* none, as a browser does: some live servers cut a browser User-Agent without it off after a
|
|
36
|
+
* few seconds. Media3 marks every progressive read with `Icy-MetaData`; HLS reads go without.
|
|
37
|
+
*/
|
|
38
|
+
private fun wantsOpenEndedRange(dataSpec: DataSpec, headers: Map<String, String>): Boolean =
|
|
39
|
+
dataSpec.position == 0L &&
|
|
40
|
+
dataSpec.length == C.LENGTH_UNSET.toLong() &&
|
|
41
|
+
headers.keys.any { it.equals("Icy-MetaData", ignoreCase = true) } &&
|
|
42
|
+
headers.keys.none { it.equals("Range", ignoreCase = true) }
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
private var resolvedUri: Uri? = null
|
|
@@ -51,13 +65,19 @@ class TransformingDataSource(private val upstream: DataSource, private val facto
|
|
|
51
65
|
else -> Triple(originalUrl, cached.second, cached.third)
|
|
52
66
|
}
|
|
53
67
|
|
|
54
|
-
val
|
|
68
|
+
val requestHeaders = buildMap {
|
|
55
69
|
putAll(dataSpec.httpRequestHeaders)
|
|
56
70
|
putAll(headers)
|
|
57
71
|
if (userAgent != DEFAULT_USER_AGENT) {
|
|
58
72
|
put("User-Agent", userAgent)
|
|
59
73
|
}
|
|
60
74
|
}
|
|
75
|
+
val mergedHeaders =
|
|
76
|
+
if (wantsOpenEndedRange(dataSpec, requestHeaders)) {
|
|
77
|
+
requestHeaders + ("Range" to OPEN_ENDED_RANGE)
|
|
78
|
+
} else {
|
|
79
|
+
requestHeaders
|
|
80
|
+
}
|
|
61
81
|
|
|
62
82
|
val transformedSpec =
|
|
63
83
|
dataSpec.buildUpon().setUri(finalUrl.toUri()).setHttpRequestHeaders(mergedHeaders).build()
|
|
@@ -9,9 +9,12 @@ import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter
|
|
|
9
9
|
import com.audiobrowser.TestFixtures.track
|
|
10
10
|
import com.audiobrowser.model.RetryPolicy
|
|
11
11
|
import com.audiobrowser.util.TrackFactory
|
|
12
|
+
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
12
13
|
import com.margelo.nitro.audiobrowser.Track
|
|
13
14
|
import java.util.concurrent.LinkedBlockingQueue
|
|
14
15
|
import java.util.concurrent.TimeUnit
|
|
16
|
+
import okhttp3.mockwebserver.MockResponse
|
|
17
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
15
18
|
import org.junit.Assert.assertEquals
|
|
16
19
|
import org.junit.Test
|
|
17
20
|
import org.junit.runner.RunWith
|
|
@@ -21,23 +24,22 @@ import org.robolectric.RuntimeEnvironment
|
|
|
21
24
|
@RunWith(RobolectricTestRunner::class)
|
|
22
25
|
class MediaFactoryTest {
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
val
|
|
40
|
-
val source = factory.createMediaSource(TrackFactory.toMedia3(queued))
|
|
27
|
+
private val context = RuntimeEnvironment.getApplication()
|
|
28
|
+
|
|
29
|
+
private fun mediaFactory(
|
|
30
|
+
getRequestConfig: (String, Track) -> MediaRequestConfig? = { _, _ -> null }
|
|
31
|
+
) =
|
|
32
|
+
MediaFactory(
|
|
33
|
+
context,
|
|
34
|
+
cache = null,
|
|
35
|
+
RetryPolicy.Default,
|
|
36
|
+
shouldRetry = { false },
|
|
37
|
+
getRequestConfig = getRequestConfig,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
/** Starts loading [track]'s media from position 0, runs [block], then releases the source. */
|
|
41
|
+
private fun loading(factory: MediaFactory, track: Track, block: () -> Unit) {
|
|
42
|
+
val source = factory.createMediaSource(TrackFactory.toMedia3(track))
|
|
41
43
|
source.prepareSource(
|
|
42
44
|
{ _, _ -> },
|
|
43
45
|
PlayerId.UNSET,
|
|
@@ -58,14 +60,50 @@ class MediaFactoryTest {
|
|
|
58
60
|
},
|
|
59
61
|
0,
|
|
60
62
|
)
|
|
63
|
+
block()
|
|
64
|
+
} finally {
|
|
65
|
+
source.releasePeriod(period)
|
|
66
|
+
source.releaseSource { _, _ -> }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
61
69
|
|
|
70
|
+
@Test
|
|
71
|
+
fun `a queued track is handed to the media request config when its media url opens`() {
|
|
72
|
+
val requests = LinkedBlockingQueue<Pair<String, Track>>()
|
|
73
|
+
val factory = mediaFactory { url, track ->
|
|
74
|
+
requests.add(url to track)
|
|
75
|
+
null
|
|
76
|
+
}
|
|
77
|
+
val queued = track(id = "queued", src = "http://127.0.0.1:9/queued.mp3")
|
|
78
|
+
|
|
79
|
+
loading(factory, queued) {
|
|
62
80
|
val (url, track) =
|
|
63
81
|
checkNotNull(requests.poll(5, TimeUnit.SECONDS)) { "the media url never opened" }
|
|
64
82
|
assertEquals("http://127.0.0.1:9/queued.mp3", url)
|
|
65
83
|
assertEquals(queued, track)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@Test
|
|
88
|
+
fun `a read from the start asks for an open-ended range, past a cross-origin redirect`() {
|
|
89
|
+
MockWebServer().use { stream ->
|
|
90
|
+
MockWebServer().use { origin ->
|
|
91
|
+
stream.enqueue(MockResponse().setBody("").setHeader("Content-Type", "audio/mpeg"))
|
|
92
|
+
origin.enqueue(
|
|
93
|
+
MockResponse().setResponseCode(302).setHeader("Location", stream.url("/live.mp3"))
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
loading(mediaFactory(), track(id = "live", src = origin.url("/listen.mp3").toString())) {
|
|
97
|
+
val first =
|
|
98
|
+
checkNotNull(origin.takeRequest(5, TimeUnit.SECONDS)) { "the media url never opened" }
|
|
99
|
+
assertEquals("bytes=0-", first.getHeader("Range"))
|
|
100
|
+
val request =
|
|
101
|
+
checkNotNull(stream.takeRequest(5, TimeUnit.SECONDS)) {
|
|
102
|
+
"the redirect was not followed"
|
|
103
|
+
}
|
|
104
|
+
assertEquals("bytes=0-", request.getHeader("Range"))
|
|
105
|
+
}
|
|
106
|
+
}
|
|
69
107
|
}
|
|
70
108
|
}
|
|
71
109
|
}
|
|
@@ -19,6 +19,8 @@ class TransformingDataSourceTest {
|
|
|
19
19
|
|
|
20
20
|
private val openedUrls = mutableListOf<String>()
|
|
21
21
|
|
|
22
|
+
private val openedHeaders = mutableListOf<Map<String, String>>()
|
|
23
|
+
|
|
22
24
|
private var failNextOpen = false
|
|
23
25
|
|
|
24
26
|
private inner class RecordingDataSource(
|
|
@@ -26,6 +28,7 @@ class TransformingDataSourceTest {
|
|
|
26
28
|
) : DataSource {
|
|
27
29
|
override fun open(dataSpec: DataSpec): Long {
|
|
28
30
|
openedUrls.add(dataSpec.uri.toString())
|
|
31
|
+
openedHeaders.add(dataSpec.httpRequestHeaders)
|
|
29
32
|
if (failNextOpen) {
|
|
30
33
|
failNextOpen = false
|
|
31
34
|
throw IOException("simulated load failure")
|
|
@@ -62,6 +65,24 @@ class TransformingDataSourceTest {
|
|
|
62
65
|
userAgent = null,
|
|
63
66
|
)
|
|
64
67
|
|
|
68
|
+
/** Every Range header [spec] opens upstream with, whatever its case. */
|
|
69
|
+
private fun rangesOf(
|
|
70
|
+
spec: DataSpec,
|
|
71
|
+
config: MediaRequestConfig = config("https://cdn.example.com", "/s.mp3"),
|
|
72
|
+
): List<String> {
|
|
73
|
+
TransformingDataSource.Factory(upstreamFactory) { config }.createDataSource().open(spec)
|
|
74
|
+
return openedHeaders.last().filterKeys { it.equals("Range", ignoreCase = true) }.values.toList()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private val icy = mapOf("Icy-MetaData" to "1")
|
|
78
|
+
|
|
79
|
+
private fun progressive(position: Long = 0) =
|
|
80
|
+
DataSpec.Builder()
|
|
81
|
+
.setUri("/listen/abc/channel.mp3".toUri())
|
|
82
|
+
.setPosition(position)
|
|
83
|
+
.setHttpRequestHeaders(icy)
|
|
84
|
+
.build()
|
|
85
|
+
|
|
65
86
|
private fun tokenConfig(token: Int) =
|
|
66
87
|
MediaRequestConfig(
|
|
67
88
|
resolve = null,
|
|
@@ -169,4 +190,42 @@ class TransformingDataSourceTest {
|
|
|
169
190
|
|
|
170
191
|
assertEquals(segment, openedUrls.last())
|
|
171
192
|
}
|
|
193
|
+
|
|
194
|
+
@Test
|
|
195
|
+
fun `a progressive read from the start asks for an open-ended range`() {
|
|
196
|
+
assertEquals(listOf("bytes=0-"), rangesOf(progressive()))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
@Test
|
|
200
|
+
fun `a progressive read from a later position adds no range of its own`() {
|
|
201
|
+
assertEquals(emptyList<String>(), rangesOf(progressive(position = 4096)))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
@Test
|
|
205
|
+
fun `a bounded read adds no range of its own`() {
|
|
206
|
+
val spec =
|
|
207
|
+
DataSpec.Builder()
|
|
208
|
+
.setUri("/listen/abc/channel.mp3".toUri())
|
|
209
|
+
.setLength(1024)
|
|
210
|
+
.setHttpRequestHeaders(icy)
|
|
211
|
+
.build()
|
|
212
|
+
|
|
213
|
+
assertEquals(emptyList<String>(), rangesOf(spec))
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
@Test
|
|
217
|
+
fun `an hls read adds no range`() {
|
|
218
|
+
assertEquals(
|
|
219
|
+
emptyList<String>(),
|
|
220
|
+
rangesOf(DataSpec("https://cdn.example.com/playlist.m3u8".toUri())),
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
@Test
|
|
225
|
+
fun `a range from the request config is kept`() {
|
|
226
|
+
val config =
|
|
227
|
+
config("https://cdn.example.com", "/s.mp3").copy(headers = mapOf("range" to "bytes=5-"))
|
|
228
|
+
|
|
229
|
+
assertEquals(listOf("bytes=5-"), rangesOf(progressive(), config))
|
|
230
|
+
}
|
|
172
231
|
}
|