react-native-audio-browser 0.2.0-next.90 → 0.2.0-next.92
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 +4 -0
- package/android/src/main/java/com/audiobrowser/RemoteOutputs.kt +60 -1
- package/android/src/main/java/com/audiobrowser/browser/TrackLoadHandler.kt +2 -1
- package/android/src/main/java/com/audiobrowser/player/AutomaticBufferManager.kt +80 -7
- package/android/src/main/java/com/audiobrowser/player/ConnectionEndSource.kt +145 -0
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +7 -2
- package/android/src/main/java/com/audiobrowser/player/MediaSessionCallback.kt +10 -2
- package/android/src/main/java/com/audiobrowser/sonos/SonosRouteProviderService.kt +13 -10
- package/android/src/test/java/com/audiobrowser/RemoteOutputRoutesTest.kt +29 -0
- package/android/src/test/java/com/audiobrowser/RemoteOutputsTest.kt +46 -4
- package/android/src/test/java/com/audiobrowser/browser/TrackLoadHandlerTest.kt +1 -0
- package/android/src/test/java/com/audiobrowser/player/AutomaticBufferManagerTest.kt +208 -0
- package/android/src/test/java/com/audiobrowser/player/HandleTrackLoadSeekTest.kt +70 -9
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +183 -6
- package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +27 -16
- package/android/src/test/java/com/audiobrowser/player/RetryMaskingTest.kt +3 -1
- package/android/src/test/java/com/audiobrowser/sonos/SonosRoutesTest.kt +32 -0
- package/ios/Model/NitroTypeStubs.swift +1 -0
- package/ios/TrackSelector.swift +6 -1
- package/lib/typescript/src/types/browser.d.ts +6 -0
- package/lib/typescript/src/types/browser.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JTrackLoadEvent.hpp +7 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/TrackLoadEvent.kt +9 -4
- package/nitrogen/generated/ios/swift/TrackLoadEvent.swift +20 -2
- package/nitrogen/generated/shared/c++/TrackLoadEvent.hpp +7 -2
- package/package.json +2 -1
- package/src/types/browser.ts +6 -0
- package/website/guide/audio-output.md +27 -2
- package/website/guide/browser.md +2 -2
|
@@ -592,6 +592,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
592
592
|
track,
|
|
593
593
|
player.tracks,
|
|
594
594
|
index.toDouble(),
|
|
595
|
+
startPosition = null,
|
|
595
596
|
intercepted = {},
|
|
596
597
|
defaultBehavior = {
|
|
597
598
|
player.skipTo(index)
|
|
@@ -614,6 +615,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
614
615
|
track,
|
|
615
616
|
tracks,
|
|
616
617
|
startIndex.toDouble(),
|
|
618
|
+
startPosition = null,
|
|
617
619
|
intercepted = {},
|
|
618
620
|
defaultBehavior = {
|
|
619
621
|
// The Player itself: this class's overrides block on main, where this runs.
|
|
@@ -629,6 +631,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
629
631
|
track,
|
|
630
632
|
arrayOf(track),
|
|
631
633
|
0.0,
|
|
634
|
+
startPosition = null,
|
|
632
635
|
intercepted = {},
|
|
633
636
|
defaultBehavior = { player.load(track) },
|
|
634
637
|
)
|
|
@@ -645,6 +648,7 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
645
648
|
track,
|
|
646
649
|
arrayOf(track),
|
|
647
650
|
0.0,
|
|
651
|
+
startPosition = null,
|
|
648
652
|
intercepted = {},
|
|
649
653
|
defaultBehavior = { player.load(track) },
|
|
650
654
|
)
|
|
@@ -2,12 +2,17 @@ package com.audiobrowser
|
|
|
2
2
|
|
|
3
3
|
import android.content.ComponentName
|
|
4
4
|
import android.content.Context
|
|
5
|
+
import android.content.IntentFilter
|
|
5
6
|
import android.content.pm.PackageManager
|
|
7
|
+
import android.os.Handler
|
|
8
|
+
import android.os.Looper
|
|
6
9
|
import androidx.annotation.MainThread
|
|
7
10
|
import androidx.annotation.VisibleForTesting
|
|
11
|
+
import androidx.mediarouter.media.MediaRouteDescriptor
|
|
8
12
|
import androidx.mediarouter.media.MediaRouteSelector
|
|
9
13
|
import androidx.mediarouter.media.MediaRouter
|
|
10
14
|
import androidx.mediarouter.media.MediaTransferReceiver
|
|
15
|
+
import com.audiobrowser.sonos.SonosOutputs
|
|
11
16
|
import com.audiobrowser.util.OutputSwitcher
|
|
12
17
|
import com.margelo.nitro.audiobrowser.Output
|
|
13
18
|
import com.margelo.nitro.audiobrowser.OutputType
|
|
@@ -26,8 +31,30 @@ object RemoteOutputs {
|
|
|
26
31
|
/** The control category a media route provider's routes declare to be offered as outputs. */
|
|
27
32
|
const val CATEGORY = "com.audiobrowser.CATEGORY_REMOTE_OUTPUT"
|
|
28
33
|
|
|
34
|
+
/**
|
|
35
|
+
* A descriptor for a route offered as a remote output, in [CATEGORY] and each of [categories],
|
|
36
|
+
* visible from Android 14 only to this app: another app on the library would list it too, and
|
|
37
|
+
* selecting it there would leave that app's playback local.
|
|
38
|
+
*/
|
|
39
|
+
@JvmStatic
|
|
40
|
+
fun routeDescriptorBuilder(
|
|
41
|
+
id: String,
|
|
42
|
+
name: String,
|
|
43
|
+
vararg categories: String,
|
|
44
|
+
): MediaRouteDescriptor.Builder =
|
|
45
|
+
MediaRouteDescriptor.Builder(id, name)
|
|
46
|
+
.addControlFilter(
|
|
47
|
+
IntentFilter().apply {
|
|
48
|
+
addCategory(CATEGORY)
|
|
49
|
+
categories.forEach(::addCategory)
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
.setVisibilityRestricted(emptySet())
|
|
53
|
+
|
|
29
54
|
private var types: Map<String, OutputType> = emptyMap()
|
|
30
55
|
|
|
56
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
57
|
+
|
|
31
58
|
private var selector = selectorFor(types)
|
|
32
59
|
|
|
33
60
|
// MediaRouter holds routers weakly: let this one go and the Output Switcher stops listing the
|
|
@@ -36,6 +63,8 @@ object RemoteOutputs {
|
|
|
36
63
|
|
|
37
64
|
private val listeners = CopyOnWriteArraySet<() -> Unit>()
|
|
38
65
|
|
|
66
|
+
private val onSonosSelectionChanged: () -> Unit = { router?.let(::update) }
|
|
67
|
+
|
|
39
68
|
/** The selected remote output, or null while playback stays local. Read from any thread. */
|
|
40
69
|
@Volatile
|
|
41
70
|
internal var current: Output? = null
|
|
@@ -97,6 +126,7 @@ object RemoteOutputs {
|
|
|
97
126
|
warnWithoutTransferReceiver(context)
|
|
98
127
|
val router = MediaRouter.getInstance(context.applicationContext)
|
|
99
128
|
this.router = router
|
|
129
|
+
SonosOutputs.addListener(onSonosSelectionChanged)
|
|
100
130
|
listen(router)
|
|
101
131
|
}
|
|
102
132
|
|
|
@@ -109,6 +139,7 @@ object RemoteOutputs {
|
|
|
109
139
|
private fun stop() {
|
|
110
140
|
val router = router ?: return
|
|
111
141
|
router.removeCallback(callback)
|
|
142
|
+
SonosOutputs.removeListener(onSonosSelectionChanged)
|
|
112
143
|
if (router.selectedRoute.matchesSelector(selector)) {
|
|
113
144
|
router.unselect(MediaRouter.UNSELECT_REASON_STOPPED)
|
|
114
145
|
}
|
|
@@ -133,7 +164,22 @@ object RemoteOutputs {
|
|
|
133
164
|
|
|
134
165
|
private fun update(router: MediaRouter) {
|
|
135
166
|
val route = router.selectedRoute
|
|
136
|
-
|
|
167
|
+
val sonosSelected = SonosOutputs.selected != null
|
|
168
|
+
setCurrent(remoteOutputOf(route.name, types, sonosSelected, route::supportsControlCategory))
|
|
169
|
+
// Posted: this runs inside the router's callbacks, which unselect would re-enter.
|
|
170
|
+
if (isUnservedSonosRoute(sonosSelected, route::supportsControlCategory)) {
|
|
171
|
+
mainHandler.post(::releaseUnservedSonosRoute)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Unselecting ends the system's session on another app's provider, which plays nothing of ours
|
|
176
|
+
// but keeps the switcher offering Stop casting.
|
|
177
|
+
private fun releaseUnservedSonosRoute() {
|
|
178
|
+
val router = router ?: return
|
|
179
|
+
val route = router.selectedRoute
|
|
180
|
+
if (!isUnservedSonosRoute(SonosOutputs.selected != null, route::supportsControlCategory)) return
|
|
181
|
+
Timber.d("RemoteOutputs: releasing %s, which another app's provider serves", route.name)
|
|
182
|
+
router.unselect(MediaRouter.UNSELECT_REASON_STOPPED)
|
|
137
183
|
}
|
|
138
184
|
|
|
139
185
|
@VisibleForTesting
|
|
@@ -149,15 +195,28 @@ object RemoteOutputs {
|
|
|
149
195
|
/**
|
|
150
196
|
* The [Output] a selected route stands for, typed by the first category in [types] it [supports],
|
|
151
197
|
* else [OutputType.REMOTE] for [RemoteOutputs.CATEGORY]; null for a route the outputs don't offer.
|
|
198
|
+
* A Sonos room's route [another app's provider serves][isUnservedSonosRoute] is not an output.
|
|
152
199
|
*/
|
|
153
200
|
internal fun remoteOutputOf(
|
|
154
201
|
name: String,
|
|
155
202
|
types: Map<String, OutputType>,
|
|
203
|
+
sonosSelected: Boolean,
|
|
156
204
|
supports: (category: String) -> Boolean,
|
|
157
205
|
): Output? {
|
|
206
|
+
if (isUnservedSonosRoute(sonosSelected, supports)) return null
|
|
158
207
|
val type =
|
|
159
208
|
types.entries.firstOrNull { supports(it.key) }?.value
|
|
160
209
|
?: OutputType.REMOTE.takeIf { supports(RemoteOutputs.CATEGORY) }
|
|
161
210
|
?: return null
|
|
162
211
|
return Output(type = type, name = name, external = true)
|
|
163
212
|
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Whether a selected route is a Sonos room's that this app's provider doesn't serve, as it has no
|
|
216
|
+
* room selected: another app on the library publishes the same rooms, and selecting one of its
|
|
217
|
+
* routes leaves playback here. Holds even while this app has Sonos turned off.
|
|
218
|
+
*/
|
|
219
|
+
internal fun isUnservedSonosRoute(
|
|
220
|
+
sonosSelected: Boolean,
|
|
221
|
+
supports: (category: String) -> Boolean,
|
|
222
|
+
): Boolean = supports(SonosOutputs.CATEGORY) && !sonosSelected
|
|
@@ -19,11 +19,12 @@ suspend fun <T> handleTrackLoad(
|
|
|
19
19
|
track: Track,
|
|
20
20
|
queue: Array<Track>,
|
|
21
21
|
startIndex: Double,
|
|
22
|
+
startPosition: Double?,
|
|
22
23
|
intercepted: suspend () -> T,
|
|
23
24
|
defaultBehavior: suspend () -> T,
|
|
24
25
|
): T {
|
|
25
26
|
if (handler != null) {
|
|
26
|
-
val event = TrackLoadEvent(track, queue, startIndex)
|
|
27
|
+
val event = TrackLoadEvent(track, queue, startIndex, startPosition)
|
|
27
28
|
try {
|
|
28
29
|
handler(event).awaitNested()
|
|
29
30
|
} catch (e: CancellationException) {
|
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
|
+
import android.os.SystemClock
|
|
3
4
|
import androidx.media3.common.Player
|
|
4
5
|
import androidx.media3.exoplayer.ExoPlayer
|
|
5
6
|
import androidx.media3.exoplayer.analytics.AnalyticsListener
|
|
7
|
+
import androidx.media3.exoplayer.source.LoadEventInfo
|
|
8
|
+
import androidx.media3.exoplayer.source.MediaLoadData
|
|
9
|
+
import java.io.IOException
|
|
6
10
|
import timber.log.Timber
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
|
-
* Raises [DynamicLoadControl]'s `bufferForPlaybackAfterRebufferMs` after each
|
|
13
|
+
* Raises [DynamicLoadControl]'s `bufferForPlaybackAfterRebufferMs` after each underrun, to what the
|
|
10
14
|
* measured drain rate says would last [targetPlaybackDurationMs]. The threshold only rises, up to
|
|
11
15
|
* [maxRebufferBufferMs], until a playlist change or a seek to another item resets it to
|
|
12
16
|
* [defaultConfig].
|
|
17
|
+
*
|
|
18
|
+
* An underrun is a rebuffer while every load delivers, however slowly: a connection that stays open
|
|
19
|
+
* and stalls counts. A rebuffer while media3 retries a failed load in place does not, nor does a
|
|
20
|
+
* reconnect's first buffering, which starts from idle.
|
|
13
21
|
*/
|
|
14
22
|
class AutomaticBufferManager(
|
|
15
23
|
private val loadControl: DynamicLoadControl,
|
|
16
24
|
private val defaultConfig: BufferConfig = BufferConfig(),
|
|
25
|
+
private val clock: () -> Long = SystemClock::elapsedRealtime,
|
|
17
26
|
) : AnalyticsListener, Player.Listener {
|
|
18
27
|
|
|
19
28
|
private var exoPlayer: ExoPlayer? = null
|
|
@@ -21,6 +30,16 @@ class AutomaticBufferManager(
|
|
|
21
30
|
private var wasReady = false
|
|
22
31
|
private var rebufferCount = 0
|
|
23
32
|
|
|
33
|
+
/** When each of the current item's loads last failed or retried, until it completes. */
|
|
34
|
+
private val failingLoads = mutableMapOf<Long, Long>()
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How long a load counts as failing after its last error or retry: longer than one attempt can
|
|
38
|
+
* take to fail, a connect and then a read, each with an 8 s timeout. A progressive load that
|
|
39
|
+
* recovers never completes until its file ends.
|
|
40
|
+
*/
|
|
41
|
+
private val failingLoadWindowMs = 20_000L
|
|
42
|
+
|
|
24
43
|
private var playbackStartTimeMs: Long = 0
|
|
25
44
|
private var bufferAtPlaybackStartMs: Long = 0
|
|
26
45
|
private var bufferingStartTimeMs: Long = 0
|
|
@@ -68,16 +87,18 @@ class AutomaticBufferManager(
|
|
|
68
87
|
when (playbackState) {
|
|
69
88
|
Player.STATE_BUFFERING -> {
|
|
70
89
|
if (wasReady) {
|
|
71
|
-
onRebuffer()
|
|
90
|
+
if (loadsFailing()) onFailedLoadRebuffer() else onRebuffer()
|
|
72
91
|
}
|
|
73
92
|
if (bufferingStartTimeMs == 0L) {
|
|
74
|
-
bufferingStartTimeMs =
|
|
93
|
+
bufferingStartTimeMs = clock()
|
|
75
94
|
Timber.d("STATE_BUFFERING entered - starting buffer timer")
|
|
76
95
|
}
|
|
77
96
|
}
|
|
78
97
|
Player.STATE_READY -> {
|
|
98
|
+
// Playing again: the loads that failed deliver.
|
|
99
|
+
failingLoads.clear()
|
|
79
100
|
if (!wasReady) {
|
|
80
|
-
playbackStartTimeMs =
|
|
101
|
+
playbackStartTimeMs = clock()
|
|
81
102
|
bufferAtPlaybackStartMs = exoPlayer?.totalBufferedDuration ?: 0
|
|
82
103
|
lastBufferLogTimeMs = playbackStartTimeMs
|
|
83
104
|
|
|
@@ -104,6 +125,49 @@ class AutomaticBufferManager(
|
|
|
104
125
|
}
|
|
105
126
|
}
|
|
106
127
|
|
|
128
|
+
override fun onLoadError(
|
|
129
|
+
eventTime: AnalyticsListener.EventTime,
|
|
130
|
+
loadEventInfo: LoadEventInfo,
|
|
131
|
+
mediaLoadData: MediaLoadData,
|
|
132
|
+
error: IOException,
|
|
133
|
+
wasCanceled: Boolean,
|
|
134
|
+
) {
|
|
135
|
+
// A preload of the next item failing does not starve this one. A canceled load counts too:
|
|
136
|
+
// media3 cancels the load of an HLS variant it excludes, and loads another.
|
|
137
|
+
if (eventTime.windowIndex != eventTime.currentWindowIndex) return
|
|
138
|
+
failingLoads[loadEventInfo.loadTaskId] = clock()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
override fun onLoadStarted(
|
|
142
|
+
eventTime: AnalyticsListener.EventTime,
|
|
143
|
+
loadEventInfo: LoadEventInfo,
|
|
144
|
+
mediaLoadData: MediaLoadData,
|
|
145
|
+
retryCount: Int,
|
|
146
|
+
) {
|
|
147
|
+
if (loadEventInfo.loadTaskId in failingLoads) failingLoads[loadEventInfo.loadTaskId] = clock()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private fun loadsFailing(): Boolean {
|
|
151
|
+
val now = clock()
|
|
152
|
+
return failingLoads.values.any { now - it < failingLoadWindowMs }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
override fun onLoadCompleted(
|
|
156
|
+
eventTime: AnalyticsListener.EventTime,
|
|
157
|
+
loadEventInfo: LoadEventInfo,
|
|
158
|
+
mediaLoadData: MediaLoadData,
|
|
159
|
+
) {
|
|
160
|
+
failingLoads -= loadEventInfo.loadTaskId
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
override fun onLoadCanceled(
|
|
164
|
+
eventTime: AnalyticsListener.EventTime,
|
|
165
|
+
loadEventInfo: LoadEventInfo,
|
|
166
|
+
mediaLoadData: MediaLoadData,
|
|
167
|
+
) {
|
|
168
|
+
failingLoads -= loadEventInfo.loadTaskId
|
|
169
|
+
}
|
|
170
|
+
|
|
107
171
|
override fun onIsPlayingChanged(
|
|
108
172
|
eventTime: androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime,
|
|
109
173
|
isPlaying: Boolean,
|
|
@@ -121,7 +185,7 @@ class AutomaticBufferManager(
|
|
|
121
185
|
override fun run() {
|
|
122
186
|
if (wasReady && exoPlayer?.isPlaying == true) {
|
|
123
187
|
val bufferedMs = exoPlayer?.totalBufferedDuration ?: 0
|
|
124
|
-
val playedMs =
|
|
188
|
+
val playedMs = clock() - playbackStartTimeMs
|
|
125
189
|
val config = loadControl.getBufferConfig()
|
|
126
190
|
Timber.d(
|
|
127
191
|
"Buffer status: %dms buffered, played %dms (rebuffer threshold: %dms)",
|
|
@@ -160,9 +224,14 @@ class AutomaticBufferManager(
|
|
|
160
224
|
|
|
161
225
|
// region Buffer adjustment logic
|
|
162
226
|
|
|
227
|
+
private fun onFailedLoadRebuffer() {
|
|
228
|
+
Timber.d("Rebuffer while a failed load retries, threshold kept")
|
|
229
|
+
restartMeasurement()
|
|
230
|
+
}
|
|
231
|
+
|
|
163
232
|
private fun onRebuffer() {
|
|
164
233
|
rebufferCount++
|
|
165
|
-
val now =
|
|
234
|
+
val now = clock()
|
|
166
235
|
val playbackDurationMs = if (playbackStartTimeMs > 0) now - playbackStartTimeMs else 0
|
|
167
236
|
|
|
168
237
|
Timber.d(
|
|
@@ -189,7 +258,11 @@ class AutomaticBufferManager(
|
|
|
189
258
|
Timber.d("Skipping adjustment - insufficient timing data")
|
|
190
259
|
}
|
|
191
260
|
|
|
192
|
-
|
|
261
|
+
restartMeasurement()
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** The next rebuffer counts only after STATE_READY records a fresh start. */
|
|
265
|
+
private fun restartMeasurement() {
|
|
193
266
|
wasReady = false
|
|
194
267
|
playbackStartTimeMs = 0
|
|
195
268
|
bufferAtPlaybackStartMs = 0
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import androidx.media3.common.C
|
|
4
|
+
import androidx.media3.common.StreamKey
|
|
5
|
+
import androidx.media3.decoder.DecoderInputBuffer
|
|
6
|
+
import androidx.media3.exoplayer.FormatHolder
|
|
7
|
+
import androidx.media3.exoplayer.LoadingInfo
|
|
8
|
+
import androidx.media3.exoplayer.SeekParameters
|
|
9
|
+
import androidx.media3.exoplayer.source.MediaPeriod
|
|
10
|
+
import androidx.media3.exoplayer.source.MediaSource
|
|
11
|
+
import androidx.media3.exoplayer.source.SampleStream
|
|
12
|
+
import androidx.media3.exoplayer.source.TrackGroupArray
|
|
13
|
+
import androidx.media3.exoplayer.source.WrappingMediaSource
|
|
14
|
+
import androidx.media3.exoplayer.trackselection.ExoTrackSelection
|
|
15
|
+
import androidx.media3.exoplayer.upstream.Allocator
|
|
16
|
+
import java.io.IOException
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A progressive source whose fatal load error, such as a connection's end that
|
|
20
|
+
* [RetryLoadErrorHandlingPolicy] does not retry in place, surfaces only once the audio the
|
|
21
|
+
* connection delivered has been read. media3 surfaces a load error whenever it is buffering or a
|
|
22
|
+
* renderer is not ready, and before it has handled the period's preparation, so a burst that
|
|
23
|
+
* arrives with its close would otherwise never play. Once the connection has ended, the period has
|
|
24
|
+
* nothing more to load: playback starts on what it holds, short of the play buffer.
|
|
25
|
+
*/
|
|
26
|
+
class ConnectionEndSource(source: MediaSource) : WrappingMediaSource(source) {
|
|
27
|
+
|
|
28
|
+
override fun createPeriod(
|
|
29
|
+
id: MediaSource.MediaPeriodId,
|
|
30
|
+
allocator: Allocator,
|
|
31
|
+
startPositionUs: Long,
|
|
32
|
+
): MediaPeriod = Period(super.createPeriod(id, allocator, startPositionUs))
|
|
33
|
+
|
|
34
|
+
override fun releasePeriod(mediaPeriod: MediaPeriod) {
|
|
35
|
+
super.releasePeriod((mediaPeriod as Period).period)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private class Period(val period: MediaPeriod) : MediaPeriod {
|
|
39
|
+
/** Read on the playback thread only, where media3 calls back. */
|
|
40
|
+
private var prepared = false
|
|
41
|
+
|
|
42
|
+
private val connectionEnded: Boolean
|
|
43
|
+
get() =
|
|
44
|
+
try {
|
|
45
|
+
period.maybeThrowPrepareError()
|
|
46
|
+
false
|
|
47
|
+
} catch (_: IOException) {
|
|
48
|
+
true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
override fun prepare(callback: MediaPeriod.Callback, positionUs: Long) {
|
|
52
|
+
period.prepare(
|
|
53
|
+
object : MediaPeriod.Callback {
|
|
54
|
+
override fun onPrepared(mediaPeriod: MediaPeriod) {
|
|
55
|
+
prepared = true
|
|
56
|
+
callback.onPrepared(this@Period)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
override fun onContinueLoadingRequested(source: MediaPeriod) {
|
|
60
|
+
callback.onContinueLoadingRequested(this@Period)
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
positionUs,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
override fun maybeThrowPrepareError() {
|
|
68
|
+
if (!prepared) period.maybeThrowPrepareError()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
override fun getTrackGroups(): TrackGroupArray = period.trackGroups
|
|
72
|
+
|
|
73
|
+
override fun getStreamKeys(trackSelections: List<ExoTrackSelection>): List<StreamKey> =
|
|
74
|
+
period.getStreamKeys(trackSelections)
|
|
75
|
+
|
|
76
|
+
override fun selectTracks(
|
|
77
|
+
selections: Array<out ExoTrackSelection?>,
|
|
78
|
+
mayRetainStreamFlags: BooleanArray,
|
|
79
|
+
streams: Array<SampleStream?>,
|
|
80
|
+
streamResetFlags: BooleanArray,
|
|
81
|
+
positionUs: Long,
|
|
82
|
+
): Long {
|
|
83
|
+
val held = Array(streams.size) { streams[it] as Stream? }
|
|
84
|
+
val inner = Array(streams.size) { held[it]?.stream }
|
|
85
|
+
val enabledAtUs =
|
|
86
|
+
period.selectTracks(selections, mayRetainStreamFlags, inner, streamResetFlags, positionUs)
|
|
87
|
+
for (i in streams.indices) {
|
|
88
|
+
val stream = inner[i]
|
|
89
|
+
held[i] =
|
|
90
|
+
when {
|
|
91
|
+
stream == null -> null
|
|
92
|
+
held[i]?.stream === stream -> held[i]
|
|
93
|
+
else -> Stream(stream)
|
|
94
|
+
}
|
|
95
|
+
streams[i] = held[i]
|
|
96
|
+
}
|
|
97
|
+
return enabledAtUs
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
override fun discardBuffer(positionUs: Long, toKeyframe: Boolean) =
|
|
101
|
+
period.discardBuffer(positionUs, toKeyframe)
|
|
102
|
+
|
|
103
|
+
override fun readDiscontinuity(): Long = period.readDiscontinuity()
|
|
104
|
+
|
|
105
|
+
override fun setUsesStreamPrerollFlags() = period.setUsesStreamPrerollFlags()
|
|
106
|
+
|
|
107
|
+
override fun seekToUs(positionUs: Long): Long = period.seekToUs(positionUs)
|
|
108
|
+
|
|
109
|
+
override fun getAdjustedSeekPositionUs(positionUs: Long, seekParameters: SeekParameters): Long =
|
|
110
|
+
period.getAdjustedSeekPositionUs(positionUs, seekParameters)
|
|
111
|
+
|
|
112
|
+
override fun getBufferedPositionUs(): Long = period.bufferedPositionUs
|
|
113
|
+
|
|
114
|
+
override fun getNextLoadPositionUs(): Long =
|
|
115
|
+
if (connectionEnded) C.TIME_END_OF_SOURCE else period.nextLoadPositionUs
|
|
116
|
+
|
|
117
|
+
override fun continueLoading(loadingInfo: LoadingInfo): Boolean =
|
|
118
|
+
period.continueLoading(loadingInfo)
|
|
119
|
+
|
|
120
|
+
override fun isLoading(): Boolean = period.isLoading
|
|
121
|
+
|
|
122
|
+
override fun reevaluateBuffer(positionUs: Long) = period.reevaluateBuffer(positionUs)
|
|
123
|
+
|
|
124
|
+
override fun setEndPositionUs(endPositionUs: Long): Long =
|
|
125
|
+
period.setEndPositionUs(endPositionUs)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private class Stream(val stream: SampleStream) : SampleStream {
|
|
129
|
+
override fun isReady(): Boolean = stream.isReady
|
|
130
|
+
|
|
131
|
+
override fun maybeThrowError() {
|
|
132
|
+
if (!stream.isReady) stream.maybeThrowError()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
override fun readData(
|
|
136
|
+
formatHolder: FormatHolder,
|
|
137
|
+
buffer: DecoderInputBuffer,
|
|
138
|
+
readFlags: Int,
|
|
139
|
+
): Int = stream.readData(formatHolder, buffer, readFlags)
|
|
140
|
+
|
|
141
|
+
override fun skipData(positionUs: Long): Int = stream.skipData(positionUs)
|
|
142
|
+
|
|
143
|
+
override fun getFlags(): Int = stream.flags
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -129,11 +129,16 @@ class MediaFactory(
|
|
|
129
129
|
track.live == true -> liveMediaFactory to RetryLoadErrorHandlingPolicy.Sources.LIVE
|
|
130
130
|
else -> mediaFactory to RetryLoadErrorHandlingPolicy.Sources.PROGRESSIVE
|
|
131
131
|
}
|
|
132
|
+
val retrying = externalPolicy == null && retryPolicy != RetryPolicy.Default
|
|
132
133
|
// One policy per item: it tells the playing item's failures from a preload's.
|
|
133
|
-
if (
|
|
134
|
+
if (retrying)
|
|
134
135
|
factory.setLoadErrorHandlingPolicy(
|
|
135
136
|
RetryLoadErrorHandlingPolicy(sources, { isCurrent(mediaItem) }, onLoadFailure)
|
|
136
137
|
)
|
|
137
|
-
|
|
138
|
+
val source = factory.setDataSourceFactory(dataSourceFactory).createMediaSource(mediaItem)
|
|
139
|
+
// Adaptive sources retry in place: the policy never ends their connection.
|
|
140
|
+
return if (retrying && sources != RetryLoadErrorHandlingPolicy.Sources.ADAPTIVE)
|
|
141
|
+
ConnectionEndSource(source)
|
|
142
|
+
else source
|
|
138
143
|
}
|
|
139
144
|
}
|
|
@@ -2,6 +2,7 @@ package com.audiobrowser.player
|
|
|
2
2
|
|
|
3
3
|
import android.os.Bundle
|
|
4
4
|
import androidx.core.content.ContextCompat
|
|
5
|
+
import androidx.media3.common.C
|
|
5
6
|
import androidx.media3.common.MediaItem
|
|
6
7
|
import androidx.media3.common.MediaMetadata
|
|
7
8
|
import androidx.media3.common.Rating
|
|
@@ -805,6 +806,8 @@ class MediaSessionCallback(private val player: Player) :
|
|
|
805
806
|
)
|
|
806
807
|
}
|
|
807
808
|
|
|
809
|
+
val startPosition = startPositionMs.takeIf { it != C.TIME_UNSET }?.let { it / 1000.0 }
|
|
810
|
+
|
|
808
811
|
return scope.future {
|
|
809
812
|
val audioBrowser = player.awaitBrowser()
|
|
810
813
|
|
|
@@ -847,6 +850,7 @@ class MediaSessionCallback(private val player: Player) :
|
|
|
847
850
|
track,
|
|
848
851
|
queueTracks,
|
|
849
852
|
index.toDouble(),
|
|
853
|
+
startPosition,
|
|
850
854
|
::currentPlayerState,
|
|
851
855
|
) {
|
|
852
856
|
val existingItems = queueTracks.map { TrackFactory.toMedia3(it) }
|
|
@@ -860,13 +864,17 @@ class MediaSessionCallback(private val player: Player) :
|
|
|
860
864
|
browserManager.resolveMediaItemsForPlayback(mediaItems, startIndex, startPositionMs)
|
|
861
865
|
|
|
862
866
|
val tracks = result.mediaItems.map { TrackFactory.fromMedia3(it) }.toTypedArray()
|
|
863
|
-
|
|
867
|
+
// setQueue reads an unset index as "keep the current one". Media3 still gets it unset on the
|
|
868
|
+
// default branch, where it means a reset.
|
|
869
|
+
val handlerIndex = result.startIndex.takeIf { it != C.INDEX_UNSET } ?: 0
|
|
870
|
+
val selectedTrack = tracks.getOrElse(handlerIndex) { tracks.first() }
|
|
864
871
|
|
|
865
872
|
handleTrackLoad(
|
|
866
873
|
audioBrowser.configuration.handleTrackLoad,
|
|
867
874
|
selectedTrack,
|
|
868
875
|
tracks,
|
|
869
|
-
|
|
876
|
+
handlerIndex.toDouble(),
|
|
877
|
+
startPosition,
|
|
870
878
|
::currentPlayerState,
|
|
871
879
|
) {
|
|
872
880
|
// Only on handleTrackLoad's default branch: a consumer that handles the load owns the
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
package com.audiobrowser.sonos
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
-
import android.content.IntentFilter
|
|
5
4
|
import android.net.wifi.WifiManager
|
|
6
5
|
import androidx.annotation.VisibleForTesting
|
|
7
|
-
import androidx.mediarouter.media.MediaRouteDescriptor
|
|
8
6
|
import androidx.mediarouter.media.MediaRouteDiscoveryRequest
|
|
9
7
|
import androidx.mediarouter.media.MediaRouteProvider
|
|
10
8
|
import androidx.mediarouter.media.MediaRouteProviderDescriptor
|
|
@@ -86,7 +84,18 @@ internal class SonosRouteProvider(context: Context) : MediaRouteProvider(context
|
|
|
86
84
|
scope.cancel()
|
|
87
85
|
}
|
|
88
86
|
|
|
89
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Serves only this app's own sessions: [SonosOutputs.selected] moves this app's playback, so
|
|
89
|
+
* another app's session would play this app's queue in its place.
|
|
90
|
+
*/
|
|
91
|
+
override fun onCreateRouteController(
|
|
92
|
+
routeId: String,
|
|
93
|
+
options: RouteControllerOptions,
|
|
94
|
+
): RouteController? {
|
|
95
|
+
if (options.clientPackageName != context.packageName) {
|
|
96
|
+
Timber.d("Sonos: refusing a session for %s", options.clientPackageName)
|
|
97
|
+
return null
|
|
98
|
+
}
|
|
90
99
|
val room = rooms.firstOrNull { it.id == routeId } ?: return null
|
|
91
100
|
return SonosRouteController(room)
|
|
92
101
|
}
|
|
@@ -158,17 +167,11 @@ internal fun sonosRouteDescriptors(
|
|
|
158
167
|
selectedId: String?,
|
|
159
168
|
volumeOf: (String) -> Int?,
|
|
160
169
|
): MediaRouteProviderDescriptor {
|
|
161
|
-
val filter =
|
|
162
|
-
IntentFilter().apply {
|
|
163
|
-
addCategory(RemoteOutputs.CATEGORY)
|
|
164
|
-
addCategory(SonosOutputs.CATEGORY)
|
|
165
|
-
}
|
|
166
170
|
val builder = MediaRouteProviderDescriptor.Builder()
|
|
167
171
|
for (room in rooms) {
|
|
168
172
|
builder.addRoute(
|
|
169
|
-
|
|
173
|
+
RemoteOutputs.routeDescriptorBuilder(room.id, room.name, SonosOutputs.CATEGORY)
|
|
170
174
|
.setDescription("Sonos")
|
|
171
|
-
.addControlFilter(filter)
|
|
172
175
|
.setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE)
|
|
173
176
|
.setDeviceType(MediaRouter.RouteInfo.DEVICE_TYPE_SPEAKER)
|
|
174
177
|
.setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import org.junit.Assert.assertEquals
|
|
4
|
+
import org.junit.Assert.assertFalse
|
|
5
|
+
import org.junit.Assert.assertTrue
|
|
6
|
+
import org.junit.Test
|
|
7
|
+
import org.junit.runner.RunWith
|
|
8
|
+
import org.robolectric.RobolectricTestRunner
|
|
9
|
+
|
|
10
|
+
@RunWith(RobolectricTestRunner::class)
|
|
11
|
+
class RemoteOutputRoutesTest {
|
|
12
|
+
@Test
|
|
13
|
+
fun `a remote output's route is in the category and visible only to this app`() {
|
|
14
|
+
val route = RemoteOutputs.routeDescriptorBuilder("speaker", "Speaker").build()
|
|
15
|
+
|
|
16
|
+
assertTrue(route.controlFilters.single().hasCategory(RemoteOutputs.CATEGORY))
|
|
17
|
+
assertFalse(route.isVisibilityPublic)
|
|
18
|
+
assertEquals(emptySet<String>(), route.allowedPackages)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@Test
|
|
22
|
+
fun `a remote output's route carries the further categories given`() {
|
|
23
|
+
val route = RemoteOutputs.routeDescriptorBuilder("speaker", "Speaker", "other").build()
|
|
24
|
+
|
|
25
|
+
val filter = route.controlFilters.single()
|
|
26
|
+
assertTrue(filter.hasCategory(RemoteOutputs.CATEGORY))
|
|
27
|
+
assertTrue(filter.hasCategory("other"))
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
package com.audiobrowser
|
|
2
2
|
|
|
3
|
+
import com.audiobrowser.sonos.SonosOutputs
|
|
3
4
|
import com.margelo.nitro.audiobrowser.Output
|
|
4
5
|
import com.margelo.nitro.audiobrowser.OutputType
|
|
5
6
|
import org.junit.Assert.assertEquals
|
|
7
|
+
import org.junit.Assert.assertFalse
|
|
6
8
|
import org.junit.Assert.assertNull
|
|
9
|
+
import org.junit.Assert.assertTrue
|
|
7
10
|
import org.junit.Test
|
|
8
11
|
|
|
9
12
|
class RemoteOutputsTest {
|
|
10
|
-
private val types = mapOf("cast" to OutputType.CAST,
|
|
13
|
+
private val types = mapOf("cast" to OutputType.CAST, SonosOutputs.CATEGORY to OutputType.SONOS)
|
|
11
14
|
|
|
12
|
-
private fun outputOf(name: String, vararg categories: String) =
|
|
13
|
-
remoteOutputOf(name, types) { it in categories }
|
|
15
|
+
private fun outputOf(name: String, vararg categories: String, sonosSelected: Boolean = true) =
|
|
16
|
+
remoteOutputOf(name, types, sonosSelected) { it in categories }
|
|
14
17
|
|
|
15
18
|
@Test
|
|
16
19
|
fun `a selected route in the category is a remote output`() {
|
|
@@ -33,7 +36,46 @@ class RemoteOutputsTest {
|
|
|
33
36
|
)
|
|
34
37
|
assertEquals(
|
|
35
38
|
Output(type = OutputType.SONOS, name = "Dining Room", external = true),
|
|
36
|
-
outputOf("Dining Room", RemoteOutputs.CATEGORY,
|
|
39
|
+
outputOf("Dining Room", RemoteOutputs.CATEGORY, SonosOutputs.CATEGORY),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@Test
|
|
44
|
+
fun `a Sonos route while this app's provider has no room selected stays local`() {
|
|
45
|
+
assertNull(
|
|
46
|
+
outputOf("Dining Room", RemoteOutputs.CATEGORY, SonosOutputs.CATEGORY, sonosSelected = false)
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Test
|
|
51
|
+
fun `a Sonos route with Sonos off here and no room selected stays local`() {
|
|
52
|
+
assertNull(
|
|
53
|
+
remoteOutputOf("Dining Room", mapOf("cast" to OutputType.CAST), sonosSelected = false) {
|
|
54
|
+
it == RemoteOutputs.CATEGORY || it == SonosOutputs.CATEGORY
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun `a Sonos route is unserved while this app's provider has no room selected`() {
|
|
61
|
+
assertTrue(isUnservedSonosRoute(sonosSelected = false) { it == SonosOutputs.CATEGORY })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@Test
|
|
65
|
+
fun `a Sonos route while this app's provider has a room selected, and any other route, are served`() {
|
|
66
|
+
assertFalse(isUnservedSonosRoute(sonosSelected = true) { it == SonosOutputs.CATEGORY })
|
|
67
|
+
assertFalse(isUnservedSonosRoute(sonosSelected = false) { it == RemoteOutputs.CATEGORY })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@Test
|
|
71
|
+
fun `other routes are remote outputs while no Sonos room is selected`() {
|
|
72
|
+
assertEquals(
|
|
73
|
+
Output(type = OutputType.CAST, name = "Living room TV", external = true),
|
|
74
|
+
outputOf("Living room TV", "cast", sonosSelected = false),
|
|
75
|
+
)
|
|
76
|
+
assertEquals(
|
|
77
|
+
Output(type = OutputType.REMOTE, name = "Kitchen", external = true),
|
|
78
|
+
outputOf("Kitchen", RemoteOutputs.CATEGORY, sonosSelected = false),
|
|
37
79
|
)
|
|
38
80
|
}
|
|
39
81
|
}
|