react-native-audio-browser 1.0.0-next.5 → 1.0.0-next.6
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/cast/java/com/audiobrowser/cast/CastSupport.kt +75 -10
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +14 -2
- package/android/src/main/java/com/audiobrowser/browser/BrowseArtworkRegistry.kt +25 -9
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +183 -122
- package/android/src/main/java/com/audiobrowser/http/HttpClient.kt +2 -59
- package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +3 -3
- package/android/src/main/java/com/audiobrowser/player/MediaSessionCallback.kt +201 -107
- package/android/src/main/java/com/audiobrowser/player/Player.kt +117 -20
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +14 -1
- package/android/src/main/java/com/audiobrowser/player/RetryLoop.kt +1 -1
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +52 -7
- package/android/src/main/java/com/audiobrowser/util/ArtworkContentProvider.kt +2 -3
- package/android/src/main/java/com/audiobrowser/util/ArtworkRoutes.kt +5 -5
- package/android/src/main/java/com/audiobrowser/util/ArtworkUris.kt +5 -0
- package/android/src/main/java/com/audiobrowser/util/AwaitCancellable.kt +35 -11
- package/android/src/main/java/com/audiobrowser/util/BrowserPathHelper.kt +11 -0
- package/android/src/main/java/com/audiobrowser/util/TrackFactory.kt +10 -7
- package/android/src/main/res/values/strings.xml +5 -2
- package/android/src/test/java/androidx/media3/session/SessionViews.kt +15 -0
- package/android/src/test/java/com/audiobrowser/ContentRefreshNavigationErrorTest.kt +1 -0
- package/android/src/test/java/com/audiobrowser/NavigateTrackTest.kt +1 -0
- package/android/src/test/java/com/audiobrowser/ResolveFormattedErrorTest.kt +1 -0
- package/android/src/test/java/com/audiobrowser/TestFixtures.kt +9 -6
- package/android/src/test/java/com/audiobrowser/browser/BrowseArtworkRegistryTest.kt +19 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerSearchFailureTest.kt +168 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerTest.kt +32 -0
- package/android/src/test/java/com/audiobrowser/http/RequestConfigBuilderTest.kt +43 -18
- package/android/src/test/java/com/audiobrowser/player/HandleTrackLoadSeekTest.kt +1 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerCastTest.kt +40 -1
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +115 -2
- package/android/src/test/java/com/audiobrowser/player/SessionBrowserNeverConfiguredTest.kt +98 -0
- package/android/src/test/java/com/audiobrowser/player/SessionUncachedIdTest.kt +396 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +6 -2
- package/android/src/test/java/com/audiobrowser/player/SessionVoiceSearchTest.kt +116 -14
- package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +12 -1
- package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +241 -0
- package/android/src/test/java/com/audiobrowser/util/ArtworkContentProviderTest.kt +11 -0
- package/android/src/test/java/com/audiobrowser/util/ArtworkRoutesTest.kt +25 -0
- package/android/src/test/java/com/audiobrowser/util/AwaitCancellableTest.kt +43 -2
- package/android/src/test/java/com/audiobrowser/util/CoilArtworkLoaderTest.kt +38 -0
- package/android/src/test/java/com/audiobrowser/util/TrackFactoryRatingTest.kt +11 -0
- package/android/src/testCast/java/com/audiobrowser/cast/SdkCastRemoteTest.kt +187 -2
- package/ios/AVPlayerEngine.swift +2 -2
- package/ios/Browser/BrowserPathHelper.swift +3 -2
- package/ios/Browser/PageResolver.swift +1 -1
- package/ios/Extension/Promise+AwaitCallback.swift +13 -4
- package/ios/HybridAudioBrowser+Player.swift +3 -2
- package/ios/HybridAudioBrowser.swift +1 -1
- package/ios/Player/Player.swift +4 -2
- package/ios/Tests/AwaitCallbackTests.swift +8 -0
- package/ios/Tests/DataURLDecodingTests.swift +25 -0
- package/ios/Tests/GenerationMemoTests.swift +136 -0
- package/ios/Tests/JSCallbackErrorTests.swift +33 -0
- package/ios/Tests/PlayerPolicyTests.swift +19 -28
- package/ios/Tests/PlayerProgressTests.swift +0 -2
- package/ios/Tests/RequestLayerTests.swift +5 -0
- package/ios/Tests/RoutePickerSourceFrameTests.swift +30 -0
- package/ios/Tests/RoutePickerSourceLifetimeTests.swift +79 -0
- package/ios/Tests/Support/TestTypes.swift +14 -0
- package/ios/Util/ArtworkImageFetcher.swift +40 -16
- package/ios/Util/AwaitCallback.swift +16 -10
- package/ios/Util/GenerationMemo.swift +81 -17
- package/ios/Util/JSCallbackError.swift +23 -0
- package/ios/Util/OutputPicker.swift +25 -5
- package/ios/Util/RoutePickerSourceFrame.swift +17 -0
- package/ios/Util/RoutePickerSourceLifetime.swift +34 -0
- package/lib/commonjs/features/browser-config.js +3 -2
- package/lib/commonjs/features/browser-config.js.map +1 -1
- package/lib/commonjs/features/browser.js +7 -8
- package/lib/commonjs/features/browser.js.map +1 -1
- package/lib/commonjs/features/output.js +7 -3
- package/lib/commonjs/features/output.js.map +1 -1
- package/lib/commonjs/web/NativeAudioBrowser.js +43 -2
- package/lib/commonjs/web/NativeAudioBrowser.js.map +1 -1
- package/lib/commonjs/web/browser/BrowserManager.js +37 -65
- package/lib/commonjs/web/browser/BrowserManager.js.map +1 -1
- package/lib/commonjs/web/browser/SearchManager.js +1 -4
- package/lib/commonjs/web/browser/SearchManager.js.map +1 -1
- package/lib/commonjs/web/http/RequestConfigBuilder.js +53 -55
- package/lib/commonjs/web/http/RequestConfigBuilder.js.map +1 -1
- package/lib/commonjs/web/player/NowPlayingManager.js +3 -22
- package/lib/commonjs/web/player/NowPlayingManager.js.map +1 -1
- package/lib/commonjs/web/util/BrowserPathHelper.js +5 -7
- package/lib/commonjs/web/util/BrowserPathHelper.js.map +1 -1
- package/lib/commonjs/web/util/GenerationMemo.js +92 -0
- package/lib/commonjs/web/util/GenerationMemo.js.map +1 -0
- package/lib/module/features/browser-config.js +3 -2
- package/lib/module/features/browser-config.js.map +1 -1
- package/lib/module/features/browser.js +7 -8
- package/lib/module/features/browser.js.map +1 -1
- package/lib/module/features/output.js +7 -3
- package/lib/module/features/output.js.map +1 -1
- package/lib/module/web/NativeAudioBrowser.js +43 -2
- package/lib/module/web/NativeAudioBrowser.js.map +1 -1
- package/lib/module/web/browser/BrowserManager.js +37 -66
- package/lib/module/web/browser/BrowserManager.js.map +1 -1
- package/lib/module/web/browser/SearchManager.js +1 -5
- package/lib/module/web/browser/SearchManager.js.map +1 -1
- package/lib/module/web/http/RequestConfigBuilder.js +53 -55
- package/lib/module/web/http/RequestConfigBuilder.js.map +1 -1
- package/lib/module/web/player/NowPlayingManager.js +3 -22
- package/lib/module/web/player/NowPlayingManager.js.map +1 -1
- package/lib/module/web/util/BrowserPathHelper.js +5 -7
- package/lib/module/web/util/BrowserPathHelper.js.map +1 -1
- package/lib/module/web/util/GenerationMemo.js +87 -0
- package/lib/module/web/util/GenerationMemo.js.map +1 -0
- package/lib/typescript/src/features/browser-config.d.ts.map +1 -1
- package/lib/typescript/src/features/browser.d.ts +7 -8
- package/lib/typescript/src/features/browser.d.ts.map +1 -1
- package/lib/typescript/src/features/output.d.ts +8 -4
- package/lib/typescript/src/features/output.d.ts.map +1 -1
- package/lib/typescript/src/specs/audio-browser.nitro.d.ts +11 -0
- package/lib/typescript/src/specs/audio-browser.nitro.d.ts.map +1 -1
- package/lib/typescript/src/types/browser-native.d.ts +2 -1
- package/lib/typescript/src/types/browser-native.d.ts.map +1 -1
- package/lib/typescript/src/types/browser.d.ts +55 -8
- package/lib/typescript/src/types/browser.d.ts.map +1 -1
- package/nitrogen/generated/android/AudioBrowserOnLoad.cpp +2 -0
- package/nitrogen/generated/android/c++/JArtworkRequestConfig.hpp +3 -0
- package/nitrogen/generated/android/c++/JFindTrackParams.hpp +57 -0
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_RequestConfig___MediaTransformParams.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_RequestConfig___RequestConfig_std__optional_std__unordered_map_std__string__std__string__.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_RequestConfig___Track.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__optional_FormattedNavigationError____FormatNavigationErrorParams.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__optional_NowPlayingUpdate____FormatNowPlayingParams.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__optional_std__string____Track.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_GateDecision_____NativeGateRequest.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_RequestConfig_____MediaTransformParams.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_RequestConfig_____RequestConfig_std__optional_std__unordered_map_std__string__std__string__.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_RequestConfig_____Track.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_TransformableRequestConfig____.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__optional_Track______FindTrackParams.hpp +171 -0
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__vector_Track______SearchParams.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_void_____TrackLoadEvent.hpp +23 -2
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__string__.hpp +13 -1
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__variant_ResolvedTrack__BrowseError__std__shared_ptr_Promise_std__variant_ResolvedTrack__BrowseError_______BrowserSourceCallbackParam.hpp +13 -1
- package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.cpp +15 -3
- package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.hpp +1 -1
- package/nitrogen/generated/android/c++/JMediaRequestConfig.hpp +3 -0
- package/nitrogen/generated/android/c++/JNativeBrowserConfiguration.hpp +20 -2
- package/nitrogen/generated/android/c++/JNativeRouteEntry.hpp +3 -0
- package/nitrogen/generated/android/c++/JNativeSetupPlayerOptions.hpp +3 -0
- package/nitrogen/generated/android/c++/JOutputPickerAnchor.hpp +69 -0
- package/nitrogen/generated/android/c++/JTransformableRequestConfig.hpp +3 -0
- package/nitrogen/generated/android/c++/JVariant_ResolvedTrack_BrowseError_Promise_BrowseResult_.hpp +13 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/FindTrackParams.kt +51 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__optional_Track______FindTrackParams.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/HybridAudioBrowserSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/NativeBrowserConfiguration.kt +11 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/OutputPickerAnchor.kt +66 -0
- package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Bridge.cpp +35 -1
- package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Bridge.hpp +144 -15
- package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Umbrella.hpp +8 -0
- package/nitrogen/generated/ios/c++/HybridAudioBrowserSpecSwift.hpp +8 -2
- package/nitrogen/generated/ios/swift/FindTrackParams.swift +29 -0
- package/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__optional_Track______FindTrackParams.swift +67 -0
- package/nitrogen/generated/ios/swift/Func_void_std__optional_Track_.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_std__optional_Track___.swift +66 -0
- package/nitrogen/generated/ios/swift/HybridAudioBrowserSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridAudioBrowserSpec_cxx.swift +2 -2
- package/nitrogen/generated/ios/swift/NativeBrowserConfiguration.swift +48 -1
- package/nitrogen/generated/ios/swift/OutputPickerAnchor.swift +44 -0
- package/nitrogen/generated/shared/c++/FindTrackParams.hpp +83 -0
- package/nitrogen/generated/shared/c++/HybridAudioBrowserSpec.hpp +4 -1
- package/nitrogen/generated/shared/c++/NativeBrowserConfiguration.hpp +10 -3
- package/nitrogen/generated/shared/c++/OutputPickerAnchor.hpp +95 -0
- package/package.json +1 -1
- package/src/codegen-js-error-message.test.ts +78 -0
- package/src/features/browser-config.test.ts +7 -0
- package/src/features/browser-config.ts +3 -2
- package/src/features/browser.ts +7 -8
- package/src/features/output.ts +13 -5
- package/src/specs/audio-browser.nitro.ts +16 -3
- package/src/types/browser-native.ts +4 -1
- package/src/types/browser.ts +60 -8
- package/src/web/NativeAudioBrowser.ts +43 -3
- package/src/web/__tests__/nowPlayingArtwork.test.ts +191 -0
- package/src/web/browser/BrowserManager.ts +50 -79
- package/src/web/browser/SearchManager.ts +1 -8
- package/src/web/browser/__tests__/artwork-resolution.test.ts +33 -0
- package/src/web/browser/__tests__/layer-resolver.test.ts +47 -1
- package/src/web/http/RequestConfigBuilder.test.ts +151 -1
- package/src/web/http/RequestConfigBuilder.ts +72 -57
- package/src/web/player/NowPlayingManager.test.ts +29 -0
- package/src/web/player/NowPlayingManager.ts +4 -27
- package/src/web/util/BrowserPathHelper.test.ts +23 -0
- package/src/web/util/BrowserPathHelper.ts +6 -7
- package/src/web/util/GenerationMemo.test.ts +148 -0
- package/src/web/util/GenerationMemo.ts +103 -0
- package/website/guide/audio-output.md +34 -7
- package/website/guide/browser.md +21 -0
- package/website/guide/gate.md +1 -1
|
@@ -13,6 +13,7 @@ import com.audiobrowser.util.SearchParamsFactory
|
|
|
13
13
|
import com.audiobrowser.util.TrackFactory
|
|
14
14
|
import com.audiobrowser.util.artworkOf
|
|
15
15
|
import com.audiobrowser.util.awaitCancellable
|
|
16
|
+
import com.audiobrowser.util.awaitJsPromise
|
|
16
17
|
import com.audiobrowser.util.awaitNested
|
|
17
18
|
import com.audiobrowser.util.awaitOrTimeout
|
|
18
19
|
import com.margelo.nitro.audiobrowser.ArtworkRequestConfig
|
|
@@ -30,8 +31,14 @@ import com.margelo.nitro.audiobrowser.StyleDisplay
|
|
|
30
31
|
import com.margelo.nitro.audiobrowser.Track
|
|
31
32
|
import com.margelo.nitro.audiobrowser.TransformableRequestConfig
|
|
32
33
|
import com.margelo.nitro.core.Promise
|
|
34
|
+
import java.io.IOException
|
|
35
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
33
36
|
import kotlinx.coroutines.CancellationException
|
|
37
|
+
import kotlinx.coroutines.CoroutineScope
|
|
38
|
+
import kotlinx.coroutines.CoroutineStart
|
|
39
|
+
import kotlinx.coroutines.Deferred
|
|
34
40
|
import kotlinx.coroutines.Dispatchers
|
|
41
|
+
import kotlinx.coroutines.SupervisorJob
|
|
35
42
|
import kotlinx.coroutines.async
|
|
36
43
|
import kotlinx.coroutines.awaitAll
|
|
37
44
|
import kotlinx.coroutines.coroutineScope
|
|
@@ -293,59 +300,115 @@ class BrowserManager {
|
|
|
293
300
|
return getCachedTrack(mediaId)?.path?.takeIf { BrowserPathHelper.isContextual(it) }
|
|
294
301
|
}
|
|
295
302
|
|
|
303
|
+
/**
|
|
304
|
+
* The track for [id]: the cached one, else the one `findTrack` answers (see [findUncachedTrack]).
|
|
305
|
+
*/
|
|
306
|
+
suspend fun trackFor(id: String): Track? = getCachedTrack(id) ?: findUncachedTrack(id)
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Whether [id] is `findTrack`'s alone to answer: a bare id while `findTrack` is set. Such an id
|
|
310
|
+
* is never browsed as a path.
|
|
311
|
+
*/
|
|
312
|
+
fun answersOnlyByFindTrack(id: String): Boolean =
|
|
313
|
+
config.findTrack != null && BrowserPathHelper.isBareId(id)
|
|
314
|
+
|
|
315
|
+
private val findScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
316
|
+
private val findsInFlight = ConcurrentHashMap<String, Deferred<Track?>>()
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The playable track the consumer's `findTrack` answers for [id], carrying [id] as its own, and
|
|
320
|
+
* cached under [id] alone. Concurrent calls for one id share one `findTrack` call, which a
|
|
321
|
+
* caller's cancellation leaves running for the others. Null when [id] is a path or http(s) URL,
|
|
322
|
+
* or when `findTrack` is unset, answers nothing, answers a track without `src`, throws, or
|
|
323
|
+
* outruns [FALLBACK_TIMEOUT].
|
|
324
|
+
*/
|
|
325
|
+
suspend fun findUncachedTrack(id: String): Track? {
|
|
326
|
+
if (!BrowserPathHelper.isBareId(id)) return null
|
|
327
|
+
val findTrack = config.findTrack ?: return null
|
|
328
|
+
val found =
|
|
329
|
+
findsInFlight
|
|
330
|
+
.computeIfAbsent(id) {
|
|
331
|
+
findScope
|
|
332
|
+
.async(start = CoroutineStart.LAZY) { find(findTrack, id) }
|
|
333
|
+
.also { lookup -> lookup.invokeOnCompletion { findsInFlight.remove(id, lookup) } }
|
|
334
|
+
}
|
|
335
|
+
.await()
|
|
336
|
+
return found?.let(::hydrateFavorite)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private suspend fun find(findTrack: suspend (String) -> Track?, id: String): Track? {
|
|
340
|
+
val track =
|
|
341
|
+
try {
|
|
342
|
+
findTrack(id)
|
|
343
|
+
} catch (e: CancellationException) {
|
|
344
|
+
throw e
|
|
345
|
+
} catch (e: Exception) {
|
|
346
|
+
Timber.e(e, "findTrack failed for id='$id'")
|
|
347
|
+
return null
|
|
348
|
+
}
|
|
349
|
+
if (track?.src == null) {
|
|
350
|
+
Timber.w("findTrack found no playable track for id='$id'")
|
|
351
|
+
return null
|
|
352
|
+
}
|
|
353
|
+
// A controller matches its now-playing row by the id it sent. Not under its path or src: a
|
|
354
|
+
// found track must not answer for a browse path it names.
|
|
355
|
+
return track.copy(id = id).also { trackCache.put(id, it) }
|
|
356
|
+
}
|
|
357
|
+
|
|
296
358
|
/**
|
|
297
359
|
* A cache miss is reachable: a controller can replay a mediaId this process never browsed (a
|
|
298
|
-
* search result after process death). When the mediaId is a playable URL, or
|
|
299
|
-
* requestMetadata carries the playable uri (stamped by TrackFactory), it builds a
|
|
300
|
-
* from the item's metadata.
|
|
360
|
+
* search result or the last track after process death). When the mediaId is a playable URL, or
|
|
361
|
+
* the item's requestMetadata carries the playable uri (stamped by TrackFactory), it builds a
|
|
362
|
+
* minimal track from the item's metadata on a cache miss; otherwise it asks [trackFor].
|
|
301
363
|
*
|
|
302
|
-
* @throws IllegalStateException when the mediaId is
|
|
364
|
+
* @throws IllegalStateException when the mediaId is neither cached, a playable URL, nor found
|
|
303
365
|
*/
|
|
304
|
-
private fun resolveMediaItemToTrack(mediaItem: MediaItem): Track {
|
|
366
|
+
private suspend fun resolveMediaItemToTrack(mediaItem: MediaItem): Track {
|
|
305
367
|
val mediaId = mediaItem.mediaId
|
|
306
368
|
|
|
307
|
-
getCachedTrack(mediaId)?.let { cachedTrack ->
|
|
308
|
-
Timber.d("Resolved mediaId='$mediaId' from cache: '${cachedTrack.title}'")
|
|
309
|
-
return cachedTrack
|
|
310
|
-
}
|
|
311
|
-
|
|
312
369
|
val fallbackSrc =
|
|
313
|
-
if (
|
|
370
|
+
if (BrowserPathHelper.isHttpUrl(mediaId)) {
|
|
314
371
|
mediaId
|
|
315
372
|
} else {
|
|
316
373
|
mediaItem.requestMetadata.mediaUri?.toString()
|
|
317
374
|
}
|
|
318
|
-
if (fallbackSrc
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
return hydrateFavorite(
|
|
322
|
-
Track(
|
|
323
|
-
// A mediaId distinct from the playable uri is the stable id; keeping it lets the car's
|
|
324
|
-
// now-playing row match.
|
|
325
|
-
id = mediaId.takeIf { it != fallbackSrc },
|
|
326
|
-
path = null,
|
|
327
|
-
src = fallbackSrc,
|
|
328
|
-
artwork = artworkOf(metadata.artworkUri?.toString()),
|
|
329
|
-
artworkSource = null,
|
|
330
|
-
request = null,
|
|
331
|
-
title = metadata.title?.toString() ?: mediaId,
|
|
332
|
-
subtitle = metadata.artist?.toString(),
|
|
333
|
-
artist = null,
|
|
334
|
-
albumPath = null,
|
|
335
|
-
album = metadata.albumTitle?.toString(),
|
|
336
|
-
description = metadata.description?.toString(),
|
|
337
|
-
genre = metadata.genre?.toString(),
|
|
338
|
-
duration = null,
|
|
339
|
-
style = null,
|
|
340
|
-
disabled = null,
|
|
341
|
-
favorited = null,
|
|
342
|
-
live = null,
|
|
343
|
-
icyCharset = null,
|
|
344
|
-
)
|
|
345
|
-
)
|
|
375
|
+
if (fallbackSrc == null) {
|
|
376
|
+
return trackFor(mediaId)
|
|
377
|
+
?: throw IllegalStateException("MediaItem neither cached nor found: $mediaId")
|
|
346
378
|
}
|
|
347
379
|
|
|
348
|
-
|
|
380
|
+
getCachedTrack(mediaId)?.let { cachedTrack ->
|
|
381
|
+
Timber.d("Resolved mediaId='$mediaId' from cache: '${cachedTrack.title}'")
|
|
382
|
+
return cachedTrack
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
Timber.w("Cache MISS for mediaId='$mediaId' - building minimal track from media item")
|
|
386
|
+
val metadata = mediaItem.mediaMetadata
|
|
387
|
+
return hydrateFavorite(
|
|
388
|
+
Track(
|
|
389
|
+
// A mediaId distinct from the playable uri is the stable id; keeping it lets the car's
|
|
390
|
+
// now-playing row match.
|
|
391
|
+
id = mediaId.takeIf { it != fallbackSrc },
|
|
392
|
+
path = null,
|
|
393
|
+
src = fallbackSrc,
|
|
394
|
+
artwork = artworkOf(metadata.artworkUri?.toString()),
|
|
395
|
+
artworkSource = null,
|
|
396
|
+
request = null,
|
|
397
|
+
title = metadata.title?.toString() ?: mediaId,
|
|
398
|
+
subtitle = metadata.artist?.toString(),
|
|
399
|
+
artist = null,
|
|
400
|
+
albumPath = null,
|
|
401
|
+
album = metadata.albumTitle?.toString(),
|
|
402
|
+
description = metadata.description?.toString(),
|
|
403
|
+
genre = metadata.genre?.toString(),
|
|
404
|
+
duration = null,
|
|
405
|
+
style = null,
|
|
406
|
+
disabled = null,
|
|
407
|
+
favorited = null,
|
|
408
|
+
live = null,
|
|
409
|
+
icyCharset = null,
|
|
410
|
+
)
|
|
411
|
+
)
|
|
349
412
|
}
|
|
350
413
|
|
|
351
414
|
/**
|
|
@@ -355,7 +418,7 @@ class BrowserManager {
|
|
|
355
418
|
* are dropped and the start index shifts to match.
|
|
356
419
|
*
|
|
357
420
|
* @throws IllegalStateException when the start track is disabled, every track is disabled, or a
|
|
358
|
-
* mediaId is neither cached
|
|
421
|
+
* mediaId is neither cached, a playable URL, nor found by `findTrack`
|
|
359
422
|
*/
|
|
360
423
|
suspend fun resolveMediaItemsForPlayback(
|
|
361
424
|
mediaItems: List<MediaItem>,
|
|
@@ -369,9 +432,10 @@ class BrowserManager {
|
|
|
369
432
|
if (searchQuery != null) {
|
|
370
433
|
Timber.d("Handling search playback request for query: $searchQuery")
|
|
371
434
|
|
|
372
|
-
val searchResults = search(searchQuery)
|
|
373
435
|
val searchTracks =
|
|
374
|
-
|
|
436
|
+
(getCachedSearchResults(searchQuery) ?: searchedOrNull(searchQuery))
|
|
437
|
+
?.filterNot { it.disabled == true }
|
|
438
|
+
?.toTypedArray()
|
|
375
439
|
|
|
376
440
|
if (searchTracks != null && searchTracks.isNotEmpty()) {
|
|
377
441
|
val mediaId = mediaItem.mediaId
|
|
@@ -426,7 +490,7 @@ class BrowserManager {
|
|
|
426
490
|
|
|
427
491
|
val resolvedTracks = mediaItems.map { resolveMediaItemToTrack(it) }
|
|
428
492
|
|
|
429
|
-
// Refusal fails the future
|
|
493
|
+
// Refusal fails the future, which the session reports to the controller, and current playback
|
|
430
494
|
// stays untouched, where an empty item list would clear the timeline. Android Auto and
|
|
431
495
|
// Assistant request playback through Media3's legacy path, which passes C.INDEX_UNSET for
|
|
432
496
|
// "the first item": normalize before the guard, or the refusal misses the cars.
|
|
@@ -764,6 +828,15 @@ class BrowserManager {
|
|
|
764
828
|
}
|
|
765
829
|
}
|
|
766
830
|
|
|
831
|
+
/** A fresh search's results, or null when it fails. */
|
|
832
|
+
private suspend fun searchedOrNull(query: String): Array<Track>? =
|
|
833
|
+
try {
|
|
834
|
+
search(query).flattenedChildren?.toTypedArray()
|
|
835
|
+
} catch (e: Exception) {
|
|
836
|
+
if (e is CancellationException) throw e
|
|
837
|
+
null
|
|
838
|
+
}
|
|
839
|
+
|
|
767
840
|
/** Returns the results of the most recent search when [query] matches it, else null. */
|
|
768
841
|
fun getCachedSearchResults(query: String): Array<Track>? {
|
|
769
842
|
val (cachedQuery, cachedResults) = lastSearch ?: return null
|
|
@@ -779,7 +852,7 @@ class BrowserManager {
|
|
|
779
852
|
* Returns the playable results, or null when there are none. A browsable first result plays its
|
|
780
853
|
* children instead.
|
|
781
854
|
*
|
|
782
|
-
* @throws Exception when the search
|
|
855
|
+
* @throws Exception when the search fails
|
|
783
856
|
*/
|
|
784
857
|
suspend fun searchPlayable(params: SearchParams): Array<Track>? {
|
|
785
858
|
val searchResults = search(params)
|
|
@@ -815,7 +888,7 @@ class BrowserManager {
|
|
|
815
888
|
/**
|
|
816
889
|
* Runs a fresh search and keeps its results for [getCachedSearchResults].
|
|
817
890
|
*
|
|
818
|
-
* @throws Exception when the search
|
|
891
|
+
* @throws Exception when the search fails
|
|
819
892
|
*/
|
|
820
893
|
suspend fun search(params: SearchParams): ResolvedTrack {
|
|
821
894
|
Timber.d("Executing fresh search for: ${params.query} (mode=${params.mode})")
|
|
@@ -989,7 +1062,7 @@ class BrowserManager {
|
|
|
989
1062
|
second = { browseError -> throw CallbackException(browseError.error) },
|
|
990
1063
|
third = { promise ->
|
|
991
1064
|
promise
|
|
992
|
-
.
|
|
1065
|
+
.awaitJsPromise()
|
|
993
1066
|
.match(
|
|
994
1067
|
first = { resolvedTrack -> resolvedTrack },
|
|
995
1068
|
second = { browseError -> throw CallbackException(browseError.error) },
|
|
@@ -1154,39 +1227,14 @@ class BrowserManager {
|
|
|
1154
1227
|
ensureLayersResolved()
|
|
1155
1228
|
// apiConfig is null for the implicit default.
|
|
1156
1229
|
val httpRequest = buildApiRequest(resolvedBrowseLayer, apiConfig, path, routeParams)
|
|
1157
|
-
val
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
val jsonResolvedTrack =
|
|
1163
|
-
try {
|
|
1164
|
-
json.decodeFromString<JsonResolvedTrack>(httpResponse.body).also {
|
|
1165
|
-
// A browse page needs its title (BrowserSource); the tabs page does not.
|
|
1166
|
-
if (it.title == null && path != TABS_ROUTE_PATH) {
|
|
1167
|
-
throw SerializationException("Field 'title' is required for a browse page")
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
} catch (e: Exception) {
|
|
1171
|
-
throw HttpStatusException(
|
|
1172
|
-
httpResponse.code,
|
|
1173
|
-
"Server returned ${httpResponse.code} with a response that is not a page: ${e.message}",
|
|
1174
|
-
e,
|
|
1175
|
-
)
|
|
1176
|
-
}
|
|
1177
|
-
jsonResolvedTrack.toNitro(requestedPath = path)
|
|
1178
|
-
} else {
|
|
1179
|
-
Timber.w(
|
|
1180
|
-
"HTTP request failed with status ${httpResponse.code} for ${httpRequest.url}: ${httpResponse.body}"
|
|
1181
|
-
)
|
|
1182
|
-
throw HttpStatusException(httpResponse.code, "Server returned ${httpResponse.code}")
|
|
1230
|
+
val jsonResolvedTrack =
|
|
1231
|
+
fetchJson<JsonResolvedTrack>(httpRequest, "a page") {
|
|
1232
|
+
// A browse page needs its title (BrowserSource); the tabs page does not.
|
|
1233
|
+
if (it.title == null && path != TABS_ROUTE_PATH) {
|
|
1234
|
+
throw SerializationException("Field 'title' is required for a browse page")
|
|
1183
1235
|
}
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
Timber.e(exception, "HTTP request failed")
|
|
1187
|
-
throw NetworkException("Network request failed: ${exception.message}", exception)
|
|
1188
|
-
},
|
|
1189
|
-
)
|
|
1236
|
+
}
|
|
1237
|
+
jsonResolvedTrack.toNitro(requestedPath = path)
|
|
1190
1238
|
}
|
|
1191
1239
|
}
|
|
1192
1240
|
|
|
@@ -1195,52 +1243,64 @@ class BrowserManager {
|
|
|
1195
1243
|
params: SearchParams,
|
|
1196
1244
|
): Array<Track> {
|
|
1197
1245
|
return withContext(Dispatchers.IO) {
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
}
|
|
1246
|
+
val searchQueryParams = buildMap {
|
|
1247
|
+
put("q", params.query)
|
|
1248
|
+
params.mode?.let { put("mode", it.toString().lowercase()) }
|
|
1249
|
+
if (params.reference == MediaReference.MY) put("reference", "my")
|
|
1250
|
+
params.genre?.let { put("genre", it) }
|
|
1251
|
+
params.artist?.let { put("artist", it) }
|
|
1252
|
+
params.album?.let { put("album", it) }
|
|
1253
|
+
params.title?.let { put("title", it) }
|
|
1254
|
+
params.playlist?.let { put("playlist", it) }
|
|
1255
|
+
}
|
|
1209
1256
|
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
)
|
|
1220
|
-
val response = httpClient.request(httpRequest)
|
|
1221
|
-
|
|
1222
|
-
response.fold(
|
|
1223
|
-
onSuccess = { httpResponse ->
|
|
1224
|
-
if (httpResponse.isSuccessful) {
|
|
1225
|
-
// A bare Track array, unlike browse's page object.
|
|
1226
|
-
val jsonTracks = json.decodeFromString<List<JsonTrack>>(httpResponse.body)
|
|
1227
|
-
jsonTracks.map { it.toNitro() }.toTypedArray()
|
|
1228
|
-
} else {
|
|
1229
|
-
Timber.w(
|
|
1230
|
-
"Search HTTP request failed with status ${httpResponse.code}: ${httpResponse.body}"
|
|
1231
|
-
)
|
|
1232
|
-
emptyArray()
|
|
1233
|
-
}
|
|
1234
|
-
},
|
|
1235
|
-
onFailure = { exception ->
|
|
1236
|
-
Timber.e(exception, "Search HTTP request failed")
|
|
1237
|
-
emptyArray()
|
|
1238
|
-
},
|
|
1257
|
+
// No browse layer and no route: search is its own kind.
|
|
1258
|
+
val httpRequest =
|
|
1259
|
+
buildApiRequest(
|
|
1260
|
+
kindConfig = apiConfig,
|
|
1261
|
+
routeConfig = null,
|
|
1262
|
+
// A layer's static path never applies, so the search config's path seeds the base.
|
|
1263
|
+
path = apiConfig.path,
|
|
1264
|
+
params = emptyMap(),
|
|
1265
|
+
initialQuery = searchQueryParams,
|
|
1239
1266
|
)
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1267
|
+
// A bare Track array, unlike browse's page object.
|
|
1268
|
+
fetchJson<List<JsonTrack>>(httpRequest, "a track list").map { it.toNitro() }.toTypedArray()
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* The body of [httpRequest]'s response decoded as [T], which [validate] may still refuse by
|
|
1274
|
+
* throwing.
|
|
1275
|
+
*
|
|
1276
|
+
* @throws HttpStatusException for a non-2xx response, or a 2xx body that is not [expected]
|
|
1277
|
+
* @throws NetworkException when the request fails in transit; a request [HttpClient] refuses (an
|
|
1278
|
+
* invalid URL, an unsupported method) throws as it is
|
|
1279
|
+
*/
|
|
1280
|
+
private suspend inline fun <reified T> fetchJson(
|
|
1281
|
+
httpRequest: HttpClient.HttpRequest,
|
|
1282
|
+
expected: String,
|
|
1283
|
+
validate: (T) -> Unit = {},
|
|
1284
|
+
): T {
|
|
1285
|
+
val httpResponse =
|
|
1286
|
+
httpClient.request(httpRequest).getOrElse { exception ->
|
|
1287
|
+
if (exception !is IOException) throw exception
|
|
1288
|
+
throw NetworkException("Network request failed: ${exception.message}", exception)
|
|
1243
1289
|
}
|
|
1290
|
+
if (!httpResponse.isSuccessful) {
|
|
1291
|
+
Timber.w(
|
|
1292
|
+
"HTTP request failed with status ${httpResponse.code} for ${httpRequest.url}: ${httpResponse.body}"
|
|
1293
|
+
)
|
|
1294
|
+
throw HttpStatusException(httpResponse.code, "Server returned ${httpResponse.code}")
|
|
1295
|
+
}
|
|
1296
|
+
return try {
|
|
1297
|
+
json.decodeFromString<T>(httpResponse.body).also(validate)
|
|
1298
|
+
} catch (e: Exception) {
|
|
1299
|
+
throw HttpStatusException(
|
|
1300
|
+
httpResponse.code,
|
|
1301
|
+
"Server returned ${httpResponse.code} with a response that is not $expected: ${e.message}",
|
|
1302
|
+
e,
|
|
1303
|
+
)
|
|
1244
1304
|
}
|
|
1245
1305
|
}
|
|
1246
1306
|
}
|
|
@@ -1276,6 +1336,7 @@ data class BrowserConfig(
|
|
|
1276
1336
|
val singleTrack: Boolean = false,
|
|
1277
1337
|
val androidControllerOfflineError: Boolean = true,
|
|
1278
1338
|
val viewMoreTitle: (() -> Promise<String>)? = null,
|
|
1339
|
+
val findTrack: (suspend (id: String) -> Track?)? = null,
|
|
1279
1340
|
) {
|
|
1280
1341
|
val hasSearch: Boolean
|
|
1281
1342
|
get() {
|
|
@@ -5,8 +5,6 @@ import java.io.IOException
|
|
|
5
5
|
import java.util.concurrent.TimeUnit
|
|
6
6
|
import kotlinx.coroutines.Dispatchers
|
|
7
7
|
import kotlinx.coroutines.withContext
|
|
8
|
-
import kotlinx.serialization.json.Json
|
|
9
|
-
import kotlinx.serialization.json.JsonElement
|
|
10
8
|
import okhttp3.Headers
|
|
11
9
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
12
10
|
import okhttp3.MediaType.Companion.toMediaType
|
|
@@ -23,11 +21,6 @@ class HttpClient {
|
|
|
23
21
|
private const val TIMEOUT_SECONDS = 30L
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
val json = Json {
|
|
27
|
-
ignoreUnknownKeys = true
|
|
28
|
-
isLenient = true
|
|
29
|
-
}
|
|
30
|
-
|
|
31
24
|
/**
|
|
32
25
|
* Debug builds only: Timber is an `api` dependency, so these lines reach the host app's trees
|
|
33
26
|
* (crash-reporter breadcrumbs included), and `BODY` buffers every response whether or not
|
|
@@ -61,13 +54,7 @@ class HttpClient {
|
|
|
61
54
|
val userAgent: String = DEFAULT_USER_AGENT,
|
|
62
55
|
)
|
|
63
56
|
|
|
64
|
-
data class HttpResponse(
|
|
65
|
-
val code: Int,
|
|
66
|
-
val body: String,
|
|
67
|
-
val headers: Map<String, String>,
|
|
68
|
-
/** HTTP reason phrase, e.g. "Not Found"; OkHttp leaves it empty over HTTP/2. */
|
|
69
|
-
val message: String,
|
|
70
|
-
) {
|
|
57
|
+
data class HttpResponse(val code: Int, val body: String, val headers: Map<String, String>) {
|
|
71
58
|
val isSuccessful: Boolean
|
|
72
59
|
get() = code in 200..299
|
|
73
60
|
}
|
|
@@ -123,12 +110,7 @@ class HttpClient {
|
|
|
123
110
|
val responseHeaders = response.headers.toMap()
|
|
124
111
|
|
|
125
112
|
Result.success(
|
|
126
|
-
HttpResponse(
|
|
127
|
-
code = response.code,
|
|
128
|
-
body = responseBody,
|
|
129
|
-
headers = responseHeaders,
|
|
130
|
-
message = response.message,
|
|
131
|
-
)
|
|
113
|
+
HttpResponse(code = response.code, body = responseBody, headers = responseHeaders)
|
|
132
114
|
)
|
|
133
115
|
} catch (e: IOException) {
|
|
134
116
|
Timber.e(e, "HTTP request failed")
|
|
@@ -138,43 +120,4 @@ class HttpClient {
|
|
|
138
120
|
Result.failure(e)
|
|
139
121
|
}
|
|
140
122
|
}
|
|
141
|
-
|
|
142
|
-
/** Fails with [HttpException] on a non-2xx response. */
|
|
143
|
-
suspend inline fun <reified T> requestJson(httpRequest: HttpRequest): Result<T> {
|
|
144
|
-
return request(httpRequest).mapCatching { response ->
|
|
145
|
-
if (!response.isSuccessful) {
|
|
146
|
-
throw HttpException(response.code, response.body, response.message)
|
|
147
|
-
}
|
|
148
|
-
json.decodeFromString<T>(response.body)
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** Fails with [HttpException] on a non-2xx response. */
|
|
153
|
-
suspend fun requestJsonElement(httpRequest: HttpRequest): Result<JsonElement> {
|
|
154
|
-
return request(httpRequest).mapCatching { response ->
|
|
155
|
-
if (!response.isSuccessful) {
|
|
156
|
-
throw HttpException(response.code, response.body, response.message)
|
|
157
|
-
}
|
|
158
|
-
json.parseToJsonElement(response.body)
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** Its message is the JSON body's `error` field when there is one, else [reasonPhrase]. */
|
|
163
|
-
class HttpException(val code: Int, val responseBody: String, val reasonPhrase: String) :
|
|
164
|
-
Exception(formatMessage(code, responseBody, reasonPhrase)) {
|
|
165
|
-
companion object {
|
|
166
|
-
private fun formatMessage(code: Int, responseBody: String, reasonPhrase: String): String {
|
|
167
|
-
try {
|
|
168
|
-
val json = org.json.JSONObject(responseBody)
|
|
169
|
-
val errorMessage = json.optString("error", null)
|
|
170
|
-
if (!errorMessage.isNullOrBlank()) {
|
|
171
|
-
return errorMessage
|
|
172
|
-
}
|
|
173
|
-
} catch (_: Exception) {
|
|
174
|
-
// Not JSON: fall back to the reason phrase.
|
|
175
|
-
}
|
|
176
|
-
return reasonPhrase
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
123
|
}
|
|
@@ -113,8 +113,7 @@ object RequestConfigBuilder {
|
|
|
113
113
|
} catch (e: CancellationException) {
|
|
114
114
|
throw e
|
|
115
115
|
} catch (e: Exception) {
|
|
116
|
-
|
|
117
|
-
throw CallbackException(e.message?.substringBefore("\n\n") ?: e.toString(), e)
|
|
116
|
+
throw CallbackException(e.message ?: e.toString(), e)
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
119
|
|
|
@@ -240,6 +239,7 @@ object RequestConfigBuilder {
|
|
|
240
239
|
}
|
|
241
240
|
}
|
|
242
241
|
|
|
242
|
+
/** Base URL, path, then query; a `data:` URL takes no query, which would land in its payload. */
|
|
243
243
|
internal fun buildUrl(config: RequestConfig): String {
|
|
244
244
|
val path = config.path ?: ""
|
|
245
245
|
val baseUrl = config.baseUrl
|
|
@@ -247,7 +247,7 @@ object RequestConfigBuilder {
|
|
|
247
247
|
val url = BrowserPathHelper.buildUrl(baseUrl, path)
|
|
248
248
|
|
|
249
249
|
val query = config.query
|
|
250
|
-
if (query.isNullOrEmpty()) return url
|
|
250
|
+
if (query.isNullOrEmpty() || BrowserPathHelper.isDataUrl(url)) return url
|
|
251
251
|
|
|
252
252
|
val queryString =
|
|
253
253
|
query.entries.joinToString("&") { (key, value) ->
|