react-native-audio-browser 0.2.0-next.37 → 0.2.0-next.38
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.
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +2 -2
- package/android/src/main/java/com/audiobrowser/browser/BrowserUrlResolution.kt +4 -14
- package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +4 -5
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +6 -2
- package/android/src/main/java/com/audiobrowser/player/Player.kt +1 -1
- package/android/src/main/java/com/audiobrowser/player/PlayerEngineFactory.kt +2 -1
- package/android/src/test/java/com/audiobrowser/TestFixtures.kt +4 -1
- package/android/src/test/java/com/audiobrowser/browser/BrowserUrlResolutionTest.kt +30 -3
- package/android/src/test/java/com/audiobrowser/player/MediaFactoryTest.kt +71 -0
- package/lib/commonjs/web/NativeAudioBrowser.js +1 -1
- package/lib/module/web/NativeAudioBrowser.js +1 -1
- package/package.json +1 -1
- package/src/web/NativeAudioBrowser.ts +1 -1
|
@@ -389,9 +389,9 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
389
389
|
* bridge: it runs on ExoPlayer's IO thread (TransformingDataSource.open), so blocking here is
|
|
390
390
|
* safe and intentional.
|
|
391
391
|
*/
|
|
392
|
-
fun getMediaRequestConfig(originalUrl: String): MediaRequestConfig? {
|
|
392
|
+
fun getMediaRequestConfig(originalUrl: String, track: Track): MediaRequestConfig? {
|
|
393
393
|
return try {
|
|
394
|
-
runBlocking { browserManager.resolveMediaUrl(originalUrl) }
|
|
394
|
+
runBlocking { browserManager.resolveMediaUrl(originalUrl, track) }
|
|
395
395
|
} catch (e: Exception) {
|
|
396
396
|
Timber.e(e, "Failed to transform media URL: $originalUrl")
|
|
397
397
|
null
|
|
@@ -20,11 +20,11 @@ import timber.log.Timber
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Builds the media request config for [originalUrl]
|
|
24
|
-
*
|
|
25
|
-
* and iOS's `resolveMediaUrl`.
|
|
23
|
+
* Builds the media request config for [originalUrl], [track]'s src, with `media.resolve` run for
|
|
24
|
+
* [track]. Returns null only when neither a request layer nor a media config is set (the caller
|
|
25
|
+
* then uses the original URL as-is). Mirrors the web stub's and iOS's `resolveMediaUrl`.
|
|
26
26
|
*/
|
|
27
|
-
suspend fun BrowserManager.resolveMediaUrl(originalUrl: String): MediaRequestConfig? {
|
|
27
|
+
suspend fun BrowserManager.resolveMediaUrl(originalUrl: String, track: Track): MediaRequestConfig? {
|
|
28
28
|
val mediaConfig = config.media
|
|
29
29
|
// The request layer counts as present when a static `request` OR a `requestResolver`
|
|
30
30
|
// is set — a resolver-only consumer still needs its baseUrl/headers/transform
|
|
@@ -67,16 +67,6 @@ suspend fun BrowserManager.resolveMediaUrl(originalUrl: String): MediaRequestCon
|
|
|
67
67
|
userAgent = base.userAgent,
|
|
68
68
|
)
|
|
69
69
|
}
|
|
70
|
-
// Final, most-specific layer: media.resolve(track). The cached Track carries any
|
|
71
|
-
// per-track `request` override (e.g. a strict-UA sentinel); resolve reads it and
|
|
72
|
-
// returns the winning config. Only look up the track when a resolve callback
|
|
73
|
-
// exists, to avoid a needless cache lookup (and its miss-log) otherwise — async
|
|
74
|
-
// OR sync (the old code checked only `resolve`, silently breaking
|
|
75
|
-
// resolveSync-only consumers).
|
|
76
|
-
val track =
|
|
77
|
-
if (mediaConfig?.resolve != null || mediaConfig?.resolveSync != null) {
|
|
78
|
-
getCachedTrack(originalUrl)
|
|
79
|
-
} else null
|
|
80
70
|
return RequestConfigBuilder.applyMediaResolve(mediaLayered, track)
|
|
81
71
|
}
|
|
82
72
|
|
|
@@ -153,12 +153,11 @@ object RequestConfigBuilder {
|
|
|
153
153
|
* per-track `request` override (e.g. a strict-UA sentinel the consumer swaps for a clean
|
|
154
154
|
* User-Agent) — and returns the config whose fields win.
|
|
155
155
|
*
|
|
156
|
-
* No-op when the media config has no `resolve` callback
|
|
157
|
-
*
|
|
158
|
-
*
|
|
156
|
+
* No-op when the media config has no `resolve` callback (so the request/media layers stand). The
|
|
157
|
+
* callback may return its config synchronously (variant `first`) or via a Promise (`second`).
|
|
158
|
+
* Mirrors iOS resolveMediaTrackConfig + applyMediaResolveLayer.
|
|
159
159
|
*/
|
|
160
|
-
suspend fun applyMediaResolve(layered: MediaRequestConfig, track: Track
|
|
161
|
-
if (track == null) return layered
|
|
160
|
+
suspend fun applyMediaResolve(layered: MediaRequestConfig, track: Track): MediaRequestConfig {
|
|
162
161
|
if (layered.resolve == null && layered.resolveSync == null) return layered
|
|
163
162
|
val resolved: RequestConfig =
|
|
164
163
|
try {
|
|
@@ -14,7 +14,9 @@ import androidx.media3.exoplayer.source.MediaSource
|
|
|
14
14
|
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
|
|
15
15
|
import androidx.media3.extractor.DefaultExtractorsFactory
|
|
16
16
|
import com.audiobrowser.model.RetryPolicy
|
|
17
|
+
import com.audiobrowser.util.TrackFactory
|
|
17
18
|
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
19
|
+
import com.margelo.nitro.audiobrowser.Track
|
|
18
20
|
import java.io.IOException
|
|
19
21
|
import java.util.concurrent.TimeUnit
|
|
20
22
|
import okhttp3.OkHttpClient
|
|
@@ -28,7 +30,7 @@ class MediaFactory(
|
|
|
28
30
|
private val hasPlayed: () -> Boolean = { false },
|
|
29
31
|
private val onRetryPending: ((exception: IOException, isNetworkError: Boolean) -> Unit)? = null,
|
|
30
32
|
private val transferListener: TransferListener? = null,
|
|
31
|
-
private val getRequestConfig: (originalUrl: String) -> MediaRequestConfig?,
|
|
33
|
+
private val getRequestConfig: (originalUrl: String, track: Track) -> MediaRequestConfig?,
|
|
32
34
|
) : MediaSource.Factory {
|
|
33
35
|
|
|
34
36
|
companion object {
|
|
@@ -130,7 +132,9 @@ class MediaFactory(
|
|
|
130
132
|
val baseFactory: DataSource.Factory = DefaultDataSource.Factory(context, httpFactory)
|
|
131
133
|
|
|
132
134
|
// Wrap with TransformingDataSource to resolve URLs on the IO thread
|
|
133
|
-
val
|
|
135
|
+
val track = TrackFactory.fromMedia3(mediaItem)
|
|
136
|
+
val transformingFactory =
|
|
137
|
+
TransformingDataSource.Factory(baseFactory) { url -> getRequestConfig(url, track) }
|
|
134
138
|
|
|
135
139
|
// Configure data source factory with optional caching
|
|
136
140
|
val dataSourceFactory =
|
|
@@ -437,7 +437,7 @@ class Player(internal val context: Context) {
|
|
|
437
437
|
}
|
|
438
438
|
reportRetryingError(exception)
|
|
439
439
|
},
|
|
440
|
-
resolveMediaConfig = { url -> browser?.getMediaRequestConfig(url) },
|
|
440
|
+
resolveMediaConfig = { url, track -> browser?.getMediaRequestConfig(url, track) },
|
|
441
441
|
)
|
|
442
442
|
loadControl = engine.loadControl
|
|
443
443
|
mediaFactory = engine.mediaFactory
|
|
@@ -12,6 +12,7 @@ import com.audiobrowser.model.PlayerSetupOptions
|
|
|
12
12
|
import com.audiobrowser.util.AndroidAudioContentTypeFactory
|
|
13
13
|
import com.margelo.nitro.audiobrowser.AndroidPlayerWakeMode
|
|
14
14
|
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
15
|
+
import com.margelo.nitro.audiobrowser.Track
|
|
15
16
|
import java.io.IOException
|
|
16
17
|
|
|
17
18
|
// media3's configurable stuck-buffering (type-1, STUCK_BUFFERING_NO_PROGRESS) timeout. Fires only
|
|
@@ -55,7 +56,7 @@ internal fun buildPlayerEngine(
|
|
|
55
56
|
isOnline: () -> Boolean,
|
|
56
57
|
hasPlayed: () -> Boolean,
|
|
57
58
|
onRetryPending: (exception: IOException, isNetworkError: Boolean) -> Unit,
|
|
58
|
-
resolveMediaConfig: (url: String) -> MediaRequestConfig?,
|
|
59
|
+
resolveMediaConfig: (url: String, track: Track) -> MediaRequestConfig?,
|
|
59
60
|
): PlayerEngine {
|
|
60
61
|
val renderer = DefaultRenderersFactory(context)
|
|
61
62
|
renderer.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER)
|
|
@@ -10,6 +10,7 @@ import com.margelo.nitro.audiobrowser.ImageShape
|
|
|
10
10
|
import com.margelo.nitro.audiobrowser.ImageSource
|
|
11
11
|
import com.margelo.nitro.audiobrowser.MediaRequestConfig
|
|
12
12
|
import com.margelo.nitro.audiobrowser.NativeRouteEntry
|
|
13
|
+
import com.margelo.nitro.audiobrowser.RequestConfig
|
|
13
14
|
import com.margelo.nitro.audiobrowser.ResolvedTrack
|
|
14
15
|
import com.margelo.nitro.audiobrowser.Section
|
|
15
16
|
import com.margelo.nitro.audiobrowser.SectionStyle
|
|
@@ -17,6 +18,7 @@ import com.margelo.nitro.audiobrowser.StyleDisplay
|
|
|
17
18
|
import com.margelo.nitro.audiobrowser.Track
|
|
18
19
|
import com.margelo.nitro.audiobrowser.TrackStyle
|
|
19
20
|
import com.margelo.nitro.audiobrowser.TransformableRequestConfig
|
|
21
|
+
import com.margelo.nitro.core.Promise
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
24
|
* Shared builders for Nitro structs, whose generated constructors require every field. Tests set
|
|
@@ -90,10 +92,11 @@ object TestFixtures {
|
|
|
90
92
|
path: String? = null,
|
|
91
93
|
query: Map<String, String>? = null,
|
|
92
94
|
headers: Map<String, String>? = null,
|
|
95
|
+
resolveSync: ((Track) -> Promise<RequestConfig>)? = null,
|
|
93
96
|
) =
|
|
94
97
|
MediaRequestConfig(
|
|
95
98
|
resolve = null,
|
|
96
|
-
resolveSync =
|
|
99
|
+
resolveSync = resolveSync,
|
|
97
100
|
transform = null,
|
|
98
101
|
transformSync = null,
|
|
99
102
|
method = null,
|
|
@@ -6,6 +6,7 @@ import com.audiobrowser.TestFixtures.track
|
|
|
6
6
|
import com.audiobrowser.TestFixtures.transformableConfig
|
|
7
7
|
import com.margelo.nitro.audiobrowser.ImageContext
|
|
8
8
|
import com.margelo.nitro.audiobrowser.ImageQueryParams
|
|
9
|
+
import com.margelo.nitro.audiobrowser.Track
|
|
9
10
|
import kotlinx.coroutines.test.runTest
|
|
10
11
|
import org.junit.Assert.assertEquals
|
|
11
12
|
import org.junit.Assert.assertNull
|
|
@@ -31,13 +32,18 @@ class BrowserUrlResolutionTest {
|
|
|
31
32
|
fun `resolveMediaUrl returns null when neither media nor request layer is configured`() =
|
|
32
33
|
runTest {
|
|
33
34
|
bm.config = BrowserConfig()
|
|
34
|
-
assertNull(
|
|
35
|
+
assertNull(
|
|
36
|
+
bm.resolveMediaUrl(
|
|
37
|
+
"https://cdn.example.com/a.mp3",
|
|
38
|
+
track(src = "https://cdn.example.com/a.mp3"),
|
|
39
|
+
)
|
|
40
|
+
)
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
@Test
|
|
38
44
|
fun `resolveMediaUrl applies the request layer to a relative src`() = runTest {
|
|
39
45
|
bm.config = BrowserConfig(request = transformableConfig(baseUrl = "https://api.example.com"))
|
|
40
|
-
val resolved = bm.resolveMediaUrl("/stream/123")
|
|
46
|
+
val resolved = bm.resolveMediaUrl("/stream/123", track(src = "/stream/123"))
|
|
41
47
|
assertEquals("https://api.example.com", resolved?.baseUrl)
|
|
42
48
|
assertEquals("/stream/123", resolved?.path)
|
|
43
49
|
}
|
|
@@ -50,12 +56,33 @@ class BrowserUrlResolutionTest {
|
|
|
50
56
|
media =
|
|
51
57
|
mediaConfig(baseUrl = "https://media.example.com", headers = mapOf("x-token" to "abc")),
|
|
52
58
|
)
|
|
53
|
-
val resolved = bm.resolveMediaUrl("/stream/123")
|
|
59
|
+
val resolved = bm.resolveMediaUrl("/stream/123", track(src = "/stream/123"))
|
|
54
60
|
assertEquals("https://media.example.com", resolved?.baseUrl)
|
|
55
61
|
assertEquals("abc", resolved?.headers?.get("x-token"))
|
|
56
62
|
assertEquals("/stream/123", resolved?.path)
|
|
57
63
|
}
|
|
58
64
|
|
|
65
|
+
@Test
|
|
66
|
+
fun `resolveMediaUrl runs media resolveSync for the track it is given`() = runTest {
|
|
67
|
+
val resolvedFor = mutableListOf<Track>()
|
|
68
|
+
bm.config =
|
|
69
|
+
BrowserConfig(
|
|
70
|
+
media =
|
|
71
|
+
mediaConfig(
|
|
72
|
+
// Records the track and stops there: a Nitro Promise cannot be built off-device.
|
|
73
|
+
resolveSync = { track ->
|
|
74
|
+
resolvedFor.add(track)
|
|
75
|
+
throw IllegalStateException("recorded")
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
val queued = track(id = "queued", src = "https://cdn.example.com/queued.mp3")
|
|
80
|
+
|
|
81
|
+
bm.resolveMediaUrl("https://cdn.example.com/queued.mp3", queued)
|
|
82
|
+
|
|
83
|
+
assertEquals(listOf(queued), resolvedFor)
|
|
84
|
+
}
|
|
85
|
+
|
|
59
86
|
@Test
|
|
60
87
|
fun `resolveArtworkUrl returns original artwork untouched when no artwork config`() = runTest {
|
|
61
88
|
bm.config = BrowserConfig()
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import androidx.media3.common.C
|
|
4
|
+
import androidx.media3.exoplayer.analytics.PlayerId
|
|
5
|
+
import androidx.media3.exoplayer.source.MediaPeriod
|
|
6
|
+
import androidx.media3.exoplayer.source.MediaSource
|
|
7
|
+
import androidx.media3.exoplayer.upstream.DefaultAllocator
|
|
8
|
+
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter
|
|
9
|
+
import com.audiobrowser.TestFixtures.track
|
|
10
|
+
import com.audiobrowser.model.RetryPolicy
|
|
11
|
+
import com.audiobrowser.util.TrackFactory
|
|
12
|
+
import com.margelo.nitro.audiobrowser.Track
|
|
13
|
+
import java.util.concurrent.LinkedBlockingQueue
|
|
14
|
+
import java.util.concurrent.TimeUnit
|
|
15
|
+
import org.junit.Assert.assertEquals
|
|
16
|
+
import org.junit.Test
|
|
17
|
+
import org.junit.runner.RunWith
|
|
18
|
+
import org.robolectric.RobolectricTestRunner
|
|
19
|
+
import org.robolectric.RuntimeEnvironment
|
|
20
|
+
|
|
21
|
+
@RunWith(RobolectricTestRunner::class)
|
|
22
|
+
class MediaFactoryTest {
|
|
23
|
+
|
|
24
|
+
@Test
|
|
25
|
+
fun `a queued track is handed to the media request config when its media url opens`() {
|
|
26
|
+
val context = RuntimeEnvironment.getApplication()
|
|
27
|
+
val requests = LinkedBlockingQueue<Pair<String, Track>>()
|
|
28
|
+
val factory =
|
|
29
|
+
MediaFactory(
|
|
30
|
+
context,
|
|
31
|
+
cache = null,
|
|
32
|
+
RetryPolicy.Default,
|
|
33
|
+
shouldRetry = { false },
|
|
34
|
+
getRequestConfig = { url, track ->
|
|
35
|
+
requests.add(url to track)
|
|
36
|
+
null
|
|
37
|
+
},
|
|
38
|
+
)
|
|
39
|
+
val queued = track(id = "queued", src = "http://127.0.0.1:9/queued.mp3")
|
|
40
|
+
val source = factory.createMediaSource(TrackFactory.toMedia3(queued))
|
|
41
|
+
source.prepareSource(
|
|
42
|
+
{ _, _ -> },
|
|
43
|
+
PlayerId.UNSET,
|
|
44
|
+
DefaultBandwidthMeter.Builder(context).build(),
|
|
45
|
+
)
|
|
46
|
+
val period =
|
|
47
|
+
source.createPeriod(
|
|
48
|
+
MediaSource.MediaPeriodId(Any()),
|
|
49
|
+
DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE),
|
|
50
|
+
0,
|
|
51
|
+
)
|
|
52
|
+
try {
|
|
53
|
+
period.prepare(
|
|
54
|
+
object : MediaPeriod.Callback {
|
|
55
|
+
override fun onPrepared(mediaPeriod: MediaPeriod) {}
|
|
56
|
+
|
|
57
|
+
override fun onContinueLoadingRequested(source: MediaPeriod) {}
|
|
58
|
+
},
|
|
59
|
+
0,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
val (url, track) =
|
|
63
|
+
checkNotNull(requests.poll(5, TimeUnit.SECONDS)) { "the media url never opened" }
|
|
64
|
+
assertEquals("http://127.0.0.1:9/queued.mp3", url)
|
|
65
|
+
assertEquals(queued, track)
|
|
66
|
+
} finally {
|
|
67
|
+
source.releasePeriod(period)
|
|
68
|
+
source.releaseSource { _, _ -> }
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -460,7 +460,7 @@ class NativeAudioBrowser extends _TrackPlayer.QueuePlayer {
|
|
|
460
460
|
* Resolves a media URL using the browser's media configuration.
|
|
461
461
|
* Combines baseUrl with relative paths to create full URLs.
|
|
462
462
|
* Supports the transform callback for URL manipulation.
|
|
463
|
-
* Mirrors Android's
|
|
463
|
+
* Mirrors Android's and iOS's `resolveMediaUrl`.
|
|
464
464
|
*/
|
|
465
465
|
async resolveMediaRequest(src, track) {
|
|
466
466
|
// The *resolved* request layer, not `configuration.request`: a
|
|
@@ -457,7 +457,7 @@ export class NativeAudioBrowser extends QueuePlayer {
|
|
|
457
457
|
* Resolves a media URL using the browser's media configuration.
|
|
458
458
|
* Combines baseUrl with relative paths to create full URLs.
|
|
459
459
|
* Supports the transform callback for URL manipulation.
|
|
460
|
-
* Mirrors Android's
|
|
460
|
+
* Mirrors Android's and iOS's `resolveMediaUrl`.
|
|
461
461
|
*/
|
|
462
462
|
async resolveMediaRequest(src, track) {
|
|
463
463
|
// The *resolved* request layer, not `configuration.request`: a
|
package/package.json
CHANGED
|
@@ -621,7 +621,7 @@ export class NativeAudioBrowser
|
|
|
621
621
|
* Resolves a media URL using the browser's media configuration.
|
|
622
622
|
* Combines baseUrl with relative paths to create full URLs.
|
|
623
623
|
* Supports the transform callback for URL manipulation.
|
|
624
|
-
* Mirrors Android's
|
|
624
|
+
* Mirrors Android's and iOS's `resolveMediaUrl`.
|
|
625
625
|
*/
|
|
626
626
|
private async resolveMediaRequest(
|
|
627
627
|
src: string,
|