react-native-audio-browser 0.2.0-next.36 → 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/build.gradle +1 -0
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +2 -2
- package/android/src/main/java/com/audiobrowser/ForegroundStart.kt +131 -0
- package/android/src/main/java/com/audiobrowser/Service.kt +8 -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/MediaSessionCallback.kt +21 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +4 -1
- package/android/src/main/java/com/audiobrowser/player/PlayerEngineFactory.kt +2 -1
- package/android/src/test/java/com/audiobrowser/ForegroundStartTest.kt +152 -0
- package/android/src/test/java/com/audiobrowser/ServiceBindingTest.kt +4 -3
- 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
package/android/build.gradle
CHANGED
|
@@ -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
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.app.ForegroundServiceStartNotAllowedException
|
|
4
|
+
import android.app.Notification
|
|
5
|
+
import android.app.NotificationManager
|
|
6
|
+
import android.content.Intent
|
|
7
|
+
import android.content.pm.ServiceInfo
|
|
8
|
+
import android.os.Build
|
|
9
|
+
import android.view.KeyEvent
|
|
10
|
+
import androidx.annotation.OptIn
|
|
11
|
+
import androidx.core.app.NotificationChannelCompat
|
|
12
|
+
import androidx.core.app.NotificationCompat
|
|
13
|
+
import androidx.core.app.NotificationManagerCompat
|
|
14
|
+
import androidx.core.app.ServiceCompat
|
|
15
|
+
import androidx.core.content.IntentCompat
|
|
16
|
+
import androidx.core.os.bundleOf
|
|
17
|
+
import androidx.media3.common.util.UnstableApi
|
|
18
|
+
import androidx.media3.session.DefaultMediaNotificationProvider
|
|
19
|
+
import androidx.media3.session.MediaSessionService
|
|
20
|
+
import kotlinx.coroutines.CoroutineScope
|
|
21
|
+
import kotlinx.coroutines.launch
|
|
22
|
+
import timber.log.Timber
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Runs [play] for a start sent with `startForegroundService`, holding the service in the foreground
|
|
26
|
+
* meanwhile ([holdForeground]). When [play] returns false the placeholder is released, and the
|
|
27
|
+
* service stops unless [hasMedia] says the player still has something to keep it for. [play]
|
|
28
|
+
* reports failure by returning false, not by throwing.
|
|
29
|
+
*/
|
|
30
|
+
internal fun <S> S.playInForeground(
|
|
31
|
+
scope: CoroutineScope,
|
|
32
|
+
startId: Int,
|
|
33
|
+
hasMedia: () -> Boolean,
|
|
34
|
+
play: suspend () -> Boolean,
|
|
35
|
+
) where S : MediaSessionService, S : MediaSessionService.Listener {
|
|
36
|
+
holdForeground()
|
|
37
|
+
scope.launch {
|
|
38
|
+
if (play()) return@launch
|
|
39
|
+
releasePlaceholder()
|
|
40
|
+
if (!hasMedia()) stopSelf(startId)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Satisfies a `startForegroundService` start, which Android kills the app for unless the service
|
|
46
|
+
* calls `startForeground()` within seconds, by going foreground at once under Media3's notification
|
|
47
|
+
* id and channel (valid while the library sets no notification provider of its own): with the media
|
|
48
|
+
* notification already showing there, else with a placeholder. Media3's next notification update
|
|
49
|
+
* replaces or removes either; [releasePlaceholder] removes the placeholder when no update will
|
|
50
|
+
* come. Holds nothing below API 26, when the service is already foreground (below API 29, where
|
|
51
|
+
* that can't be read, it holds Media3's notification again), or when Android refuses the start,
|
|
52
|
+
* which is reported to [MediaSessionService.Listener.onForegroundServiceStartNotAllowedException].
|
|
53
|
+
*/
|
|
54
|
+
internal fun <S> S.holdForeground()
|
|
55
|
+
where S : MediaSessionService, S : MediaSessionService.Listener {
|
|
56
|
+
if (Build.VERSION.SDK_INT < 26 || isForeground()) return
|
|
57
|
+
try {
|
|
58
|
+
startForeground(mediaNotification()?.notification ?: placeholder())
|
|
59
|
+
} catch (e: IllegalStateException) {
|
|
60
|
+
if (Build.VERSION.SDK_INT < 31 || e !is ForegroundServiceStartNotAllowedException) throw e
|
|
61
|
+
Timber.w(e, "Foreground start refused")
|
|
62
|
+
onForegroundServiceStartNotAllowedException()
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Removes the placeholder [holdForeground] posted, if it is still up, and has Media3 post the
|
|
68
|
+
* notification the player warrants. For a start whose play request changed nothing in the player:
|
|
69
|
+
* Media3 tracks the session only once a controller has connected, so its update alone may never
|
|
70
|
+
* come.
|
|
71
|
+
*/
|
|
72
|
+
@OptIn(UnstableApi::class)
|
|
73
|
+
internal fun MediaSessionService.releasePlaceholder() {
|
|
74
|
+
if (mediaNotification()?.notification?.extras?.getBoolean(PLACEHOLDER_EXTRA) == true) {
|
|
75
|
+
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
|
76
|
+
}
|
|
77
|
+
triggerNotificationUpdate()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether [this] is a press `MediaButtonReceiver.onReceive` starts the service in the foreground
|
|
82
|
+
* for.
|
|
83
|
+
*/
|
|
84
|
+
internal fun Intent.isForegroundStartKey(): Boolean =
|
|
85
|
+
action == Intent.ACTION_MEDIA_BUTTON &&
|
|
86
|
+
IntentCompat.getParcelableExtra(this, Intent.EXTRA_KEY_EVENT, KeyEvent::class.java)?.keyCode in
|
|
87
|
+
PLAY_KEYS
|
|
88
|
+
|
|
89
|
+
private val PLAY_KEYS =
|
|
90
|
+
setOf(
|
|
91
|
+
KeyEvent.KEYCODE_MEDIA_PLAY,
|
|
92
|
+
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
|
|
93
|
+
KeyEvent.KEYCODE_HEADSETHOOK,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
private const val PLACEHOLDER_EXTRA = "com.audiobrowser.placeholder"
|
|
97
|
+
|
|
98
|
+
private fun MediaSessionService.mediaNotification() =
|
|
99
|
+
getSystemService(NotificationManager::class.java).activeNotifications.firstOrNull {
|
|
100
|
+
it.id == DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private fun MediaSessionService.isForeground(): Boolean =
|
|
104
|
+
Build.VERSION.SDK_INT >= 29 && foregroundServiceType != ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE
|
|
105
|
+
|
|
106
|
+
private fun MediaSessionService.startForeground(notification: Notification) =
|
|
107
|
+
ServiceCompat.startForeground(
|
|
108
|
+
this,
|
|
109
|
+
DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID,
|
|
110
|
+
notification,
|
|
111
|
+
if (Build.VERSION.SDK_INT >= 29) ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK else 0,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
@OptIn(UnstableApi::class)
|
|
115
|
+
private fun MediaSessionService.placeholder(): Notification {
|
|
116
|
+
NotificationManagerCompat.from(this)
|
|
117
|
+
.createNotificationChannel(
|
|
118
|
+
NotificationChannelCompat.Builder(
|
|
119
|
+
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID,
|
|
120
|
+
NotificationManagerCompat.IMPORTANCE_LOW,
|
|
121
|
+
)
|
|
122
|
+
.setName(getString(DefaultMediaNotificationProvider.DEFAULT_CHANNEL_NAME_RESOURCE_ID))
|
|
123
|
+
.build()
|
|
124
|
+
)
|
|
125
|
+
return NotificationCompat.Builder(this, DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID)
|
|
126
|
+
.setSmallIcon(androidx.media3.session.R.drawable.media3_notification_small_icon)
|
|
127
|
+
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
|
128
|
+
.setCategory(Notification.CATEGORY_TRANSPORT)
|
|
129
|
+
.addExtras(bundleOf(PLACEHOLDER_EXTRA to true))
|
|
130
|
+
.build()
|
|
131
|
+
}
|
|
@@ -37,7 +37,6 @@ import kotlinx.coroutines.Dispatchers
|
|
|
37
37
|
import kotlinx.coroutines.MainScope
|
|
38
38
|
import kotlinx.coroutines.SupervisorJob
|
|
39
39
|
import kotlinx.coroutines.cancel
|
|
40
|
-
import kotlinx.coroutines.launch
|
|
41
40
|
import timber.log.Timber
|
|
42
41
|
|
|
43
42
|
/**
|
|
@@ -120,6 +119,7 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
120
119
|
// Store references in Player for artwork URL transformation and SVG pre-rendering
|
|
121
120
|
player.coilBitmapLoader = coilBitmapLoader
|
|
122
121
|
player.imageLoader = imageLoader
|
|
122
|
+
player.onPlaybackResumptionFailed = { releasePlaceholder() }
|
|
123
123
|
|
|
124
124
|
// Populate the artwork content provider holder so ArtworkContentProvider can serve
|
|
125
125
|
// browse artwork to Android Auto / AAOS over content:// URIs.
|
|
@@ -183,8 +183,14 @@ class Service : MediaLibraryService(), MediaSessionService.Listener {
|
|
|
183
183
|
?.let { searchIntent ->
|
|
184
184
|
val searchParams = parseSearchIntent(searchIntent)
|
|
185
185
|
Timber.d("MEDIA_PLAY_FROM_SEARCH intent received: $searchParams")
|
|
186
|
-
|
|
186
|
+
// A search that plays has waited for AudioBrowser, whose MediaBrowser connecting hands
|
|
187
|
+
// the notification to Media3.
|
|
188
|
+
playInForeground(scope, startId, hasMedia = { player.currentTrack != null }) {
|
|
189
|
+
player.playFromSearch(searchParams)
|
|
190
|
+
}
|
|
187
191
|
}
|
|
192
|
+
// Released by Media3's update after the play request, or by a failed resumption.
|
|
193
|
+
if (intent?.isForegroundStartKey() == true) holdForeground()
|
|
188
194
|
|
|
189
195
|
return super.onStartCommand(intent, flags, startId)
|
|
190
196
|
}
|
|
@@ -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 =
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
3
|
import android.os.Bundle
|
|
4
|
+
import androidx.core.content.ContextCompat
|
|
4
5
|
import androidx.media3.common.MediaItem
|
|
5
6
|
import androidx.media3.common.MediaMetadata
|
|
6
7
|
import androidx.media3.common.Rating
|
|
@@ -23,6 +24,7 @@ import com.audiobrowser.util.BrowserPathHelper
|
|
|
23
24
|
import com.audiobrowser.util.RatingFavorites
|
|
24
25
|
import com.audiobrowser.util.TrackFactory
|
|
25
26
|
import com.google.common.collect.ImmutableList
|
|
27
|
+
import com.google.common.util.concurrent.FutureCallback
|
|
26
28
|
import com.google.common.util.concurrent.Futures
|
|
27
29
|
import com.google.common.util.concurrent.ListenableFuture
|
|
28
30
|
import com.margelo.nitro.audiobrowser.FormattedNavigationError
|
|
@@ -927,6 +929,25 @@ class MediaSessionCallback(private val player: Player) :
|
|
|
927
929
|
mediaSession: MediaSession,
|
|
928
930
|
controller: MediaSession.ControllerInfo,
|
|
929
931
|
isForPlayback: Boolean,
|
|
932
|
+
): ListenableFuture<MediaSession.MediaItemsWithStartPosition> {
|
|
933
|
+
val resumption = resume(controller, isForPlayback)
|
|
934
|
+
if (isForPlayback) {
|
|
935
|
+
Futures.addCallback(
|
|
936
|
+
resumption,
|
|
937
|
+
object : FutureCallback<MediaSession.MediaItemsWithStartPosition> {
|
|
938
|
+
override fun onSuccess(result: MediaSession.MediaItemsWithStartPosition) {}
|
|
939
|
+
|
|
940
|
+
override fun onFailure(t: Throwable) = player.onPlaybackResumptionFailed()
|
|
941
|
+
},
|
|
942
|
+
ContextCompat.getMainExecutor(player.context),
|
|
943
|
+
)
|
|
944
|
+
}
|
|
945
|
+
return resumption
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
private fun resume(
|
|
949
|
+
controller: MediaSession.ControllerInfo,
|
|
950
|
+
isForPlayback: Boolean,
|
|
930
951
|
): ListenableFuture<MediaSession.MediaItemsWithStartPosition> {
|
|
931
952
|
Timber.d("${controller.packageName}")
|
|
932
953
|
|
|
@@ -127,6 +127,9 @@ class Player(internal val context: Context) {
|
|
|
127
127
|
*/
|
|
128
128
|
var imageLoader: coil3.ImageLoader? = null
|
|
129
129
|
|
|
130
|
+
/** Called on the main thread when a resumption meant to start playback fails. Set by Service. */
|
|
131
|
+
var onPlaybackResumptionFailed: () -> Unit = {}
|
|
132
|
+
|
|
130
133
|
/** Registry that maps content:// tokens to artwork URLs for the browse content provider. */
|
|
131
134
|
val browseArtworkRegistry = com.audiobrowser.browser.BrowseArtworkRegistry()
|
|
132
135
|
|
|
@@ -434,7 +437,7 @@ class Player(internal val context: Context) {
|
|
|
434
437
|
}
|
|
435
438
|
reportRetryingError(exception)
|
|
436
439
|
},
|
|
437
|
-
resolveMediaConfig = { url -> browser?.getMediaRequestConfig(url) },
|
|
440
|
+
resolveMediaConfig = { url, track -> browser?.getMediaRequestConfig(url, track) },
|
|
438
441
|
)
|
|
439
442
|
loadControl = engine.loadControl
|
|
440
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)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.app.Notification
|
|
4
|
+
import android.app.NotificationManager
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import android.content.pm.ServiceInfo
|
|
7
|
+
import android.view.KeyEvent
|
|
8
|
+
import androidx.annotation.OptIn
|
|
9
|
+
import androidx.core.app.NotificationCompat
|
|
10
|
+
import androidx.core.app.ServiceCompat
|
|
11
|
+
import androidx.media3.common.util.UnstableApi
|
|
12
|
+
import androidx.media3.session.DefaultMediaNotificationProvider
|
|
13
|
+
import androidx.media3.session.MediaSession
|
|
14
|
+
import androidx.media3.session.MediaSessionService
|
|
15
|
+
import kotlinx.coroutines.CoroutineScope
|
|
16
|
+
import kotlinx.coroutines.Dispatchers
|
|
17
|
+
import org.junit.Assert.assertEquals
|
|
18
|
+
import org.junit.Assert.assertFalse
|
|
19
|
+
import org.junit.Assert.assertTrue
|
|
20
|
+
import org.junit.Test
|
|
21
|
+
import org.junit.runner.RunWith
|
|
22
|
+
import org.robolectric.Robolectric
|
|
23
|
+
import org.robolectric.RobolectricTestRunner
|
|
24
|
+
import org.robolectric.Shadows.shadowOf
|
|
25
|
+
import org.robolectric.annotation.Config
|
|
26
|
+
|
|
27
|
+
// Robolectric: startForeground/stopForeground/stopSelf only exist under the framework, and
|
|
28
|
+
// ShadowService records each.
|
|
29
|
+
@OptIn(UnstableApi::class)
|
|
30
|
+
@RunWith(RobolectricTestRunner::class)
|
|
31
|
+
class ForegroundStartTest {
|
|
32
|
+
|
|
33
|
+
class SessionlessService : MediaSessionService(), MediaSessionService.Listener {
|
|
34
|
+
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? = null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private val service = Robolectric.buildService(SessionlessService::class.java).create().get()
|
|
38
|
+
private val scope = CoroutineScope(Dispatchers.Unconfined)
|
|
39
|
+
private val mediaNotificationId = DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID
|
|
40
|
+
|
|
41
|
+
private fun notification(title: String): Notification =
|
|
42
|
+
NotificationCompat.Builder(service, DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID)
|
|
43
|
+
.setContentTitle(title)
|
|
44
|
+
.build()
|
|
45
|
+
|
|
46
|
+
@Test
|
|
47
|
+
fun `a cold search goes foreground before the search runs`() {
|
|
48
|
+
var foregroundIdDuringSearch: Int? = null
|
|
49
|
+
|
|
50
|
+
service.playInForeground(scope, startId = 1, hasMedia = { false }) {
|
|
51
|
+
foregroundIdDuringSearch = shadowOf(service).lastForegroundNotificationId
|
|
52
|
+
true
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Media3's id, so its own notification replaces the placeholder once playback starts.
|
|
56
|
+
assertEquals(mediaNotificationId, foregroundIdDuringSearch)
|
|
57
|
+
assertFalse(shadowOf(service).isForegroundStopped)
|
|
58
|
+
assertEquals(NEVER_STOPPED, shadowOf(service).stopSelfId)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Test
|
|
62
|
+
fun `a cold search that finds nothing stops the service`() {
|
|
63
|
+
service.playInForeground(scope, startId = 7, hasMedia = { false }) { false }
|
|
64
|
+
|
|
65
|
+
assertTrue(shadowOf(service).isForegroundStopped)
|
|
66
|
+
assertTrue(shadowOf(service).notificationShouldRemoved)
|
|
67
|
+
assertEquals(7, shadowOf(service).stopSelfId)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@Test
|
|
71
|
+
fun `a search that finds nothing leaves the service running while the player has media`() {
|
|
72
|
+
service.playInForeground(scope, startId = 2, hasMedia = { true }) { false }
|
|
73
|
+
|
|
74
|
+
assertTrue(shadowOf(service).isForegroundStopped)
|
|
75
|
+
assertEquals(NEVER_STOPPED, shadowOf(service).stopSelfId)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Test
|
|
79
|
+
fun `a search while Media3's notification shows holds that notification and leaves it to Media3`() {
|
|
80
|
+
val media = notification("Now playing")
|
|
81
|
+
service.getSystemService(NotificationManager::class.java).notify(mediaNotificationId, media)
|
|
82
|
+
|
|
83
|
+
service.playInForeground(scope, startId = 3, hasMedia = { true }) {
|
|
84
|
+
assertEquals(
|
|
85
|
+
"Now playing",
|
|
86
|
+
shadowOf(service).lastForegroundNotification.extras.getString(Notification.EXTRA_TITLE),
|
|
87
|
+
)
|
|
88
|
+
false
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
assertFalse(shadowOf(service).isForegroundStopped)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Test
|
|
95
|
+
fun `a release while Media3's notification is in the foreground leaves it there`() {
|
|
96
|
+
val media = notification("Now playing")
|
|
97
|
+
ServiceCompat.startForeground(
|
|
98
|
+
service,
|
|
99
|
+
mediaNotificationId,
|
|
100
|
+
media,
|
|
101
|
+
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK,
|
|
102
|
+
)
|
|
103
|
+
service.getSystemService(NotificationManager::class.java).notify(mediaNotificationId, media)
|
|
104
|
+
|
|
105
|
+
service.releasePlaceholder()
|
|
106
|
+
|
|
107
|
+
assertFalse(shadowOf(service).isForegroundStopped)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Test
|
|
111
|
+
fun `the foreground start keys are the ones MediaButtonReceiver starts the service for`() {
|
|
112
|
+
fun press(keyCode: Int) =
|
|
113
|
+
Intent(Intent.ACTION_MEDIA_BUTTON)
|
|
114
|
+
.putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, keyCode))
|
|
115
|
+
|
|
116
|
+
assertTrue(press(KeyEvent.KEYCODE_MEDIA_PLAY).isForegroundStartKey())
|
|
117
|
+
assertTrue(press(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE).isForegroundStartKey())
|
|
118
|
+
assertTrue(press(KeyEvent.KEYCODE_HEADSETHOOK).isForegroundStartKey())
|
|
119
|
+
assertFalse(press(KeyEvent.KEYCODE_MEDIA_PAUSE).isForegroundStartKey())
|
|
120
|
+
assertFalse(press(KeyEvent.KEYCODE_MEDIA_NEXT).isForegroundStartKey())
|
|
121
|
+
assertFalse(Intent(Intent.ACTION_MEDIA_BUTTON).isForegroundStartKey())
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@Test
|
|
125
|
+
@Config(sdk = [28])
|
|
126
|
+
fun `below API 29 a playing service holds Media3's notification again and keeps it`() {
|
|
127
|
+
val media = notification("Now playing")
|
|
128
|
+
ServiceCompat.startForeground(service, mediaNotificationId, media, 0)
|
|
129
|
+
service.getSystemService(NotificationManager::class.java).notify(mediaNotificationId, media)
|
|
130
|
+
|
|
131
|
+
service.playInForeground(scope, startId = 5, hasMedia = { true }) { false }
|
|
132
|
+
|
|
133
|
+
assertEquals(
|
|
134
|
+
"Now playing",
|
|
135
|
+
shadowOf(service).lastForegroundNotification.extras.getString(Notification.EXTRA_TITLE),
|
|
136
|
+
)
|
|
137
|
+
assertFalse(shadowOf(service).isForegroundStopped)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@Test
|
|
141
|
+
@Config(sdk = [25])
|
|
142
|
+
fun `below API 26 nothing is held, as startForegroundService does not exist`() {
|
|
143
|
+
service.holdForeground()
|
|
144
|
+
|
|
145
|
+
assertEquals(0, shadowOf(service).lastForegroundNotificationId)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private companion object {
|
|
149
|
+
// ShadowService's stopSelfId before any stopSelf(startId); Android's start ids begin at 1.
|
|
150
|
+
const val NEVER_STOPPED = 0
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -20,6 +20,7 @@ class ServiceBindingTest {
|
|
|
20
20
|
|
|
21
21
|
private val app = RuntimeEnvironment.getApplication()
|
|
22
22
|
private val intent = Intent(app, Service::class.java)
|
|
23
|
+
private val unbindableIntent = Intent("unbindable").setPackage(app.packageName)
|
|
23
24
|
|
|
24
25
|
private val connection =
|
|
25
26
|
object : ServiceConnection {
|
|
@@ -65,7 +66,7 @@ class ServiceBindingTest {
|
|
|
65
66
|
val binding = ServiceBinding(app)
|
|
66
67
|
shadowOf(app).declareActionUnbindable("unbindable")
|
|
67
68
|
|
|
68
|
-
assertFalse(binding.bind(
|
|
69
|
+
assertFalse(binding.bind(unbindableIntent, connection))
|
|
69
70
|
|
|
70
71
|
assertTrue(binding.isBound)
|
|
71
72
|
|
|
@@ -83,8 +84,8 @@ class ServiceBindingTest {
|
|
|
83
84
|
val binding = ServiceBinding(app)
|
|
84
85
|
shadowOf(app).declareActionUnbindable("unbindable")
|
|
85
86
|
|
|
86
|
-
assertFalse(binding.bind(
|
|
87
|
-
assertFalse(binding.bind(
|
|
87
|
+
assertFalse(binding.bind(unbindableIntent, connection))
|
|
88
|
+
assertFalse(binding.bind(unbindableIntent, connection))
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
@Test
|
|
@@ -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,
|