react-native-audio-browser 0.2.0-next.50 → 0.2.0-next.51
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 +15 -0
- package/android/src/cast/java/com/audiobrowser/cast/CastSupport.kt +215 -0
- package/android/src/main/AndroidManifest.xml +11 -0
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +0 -4
- package/android/src/main/java/com/audiobrowser/RemoteOutputs.kt +47 -10
- package/android/src/main/java/com/audiobrowser/model/PlayerSetupOptions.kt +12 -3
- package/android/src/main/java/com/audiobrowser/player/EqualizerController.kt +4 -1
- package/android/src/main/java/com/audiobrowser/player/LocalEngine.kt +49 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +170 -119
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +18 -13
- package/android/src/main/java/com/audiobrowser/player/TransformingDataSource.kt +10 -13
- package/android/src/main/java/com/audiobrowser/remote/RemoteItem.kt +64 -0
- package/android/src/main/java/com/audiobrowser/remote/RemoteOutputsController.kt +98 -0
- package/android/src/main/java/com/audiobrowser/remote/RemotePlayback.kt +42 -0
- package/android/src/main/java/com/audiobrowser/remote/RemoteQueue.kt +110 -0
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +696 -0
- package/android/src/main/java/com/audiobrowser/sonos/DeviceDescriptionParser.kt +113 -0
- package/android/src/main/java/com/audiobrowser/sonos/DidlLite.kt +39 -0
- package/android/src/main/java/com/audiobrowser/sonos/SoapClient.kt +59 -0
- package/android/src/main/java/com/audiobrowser/sonos/SoapEnvelopes.kt +92 -0
- package/android/src/main/java/com/audiobrowser/sonos/SoapResponseParser.kt +105 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosDiscoverer.kt +69 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosOutputs.kt +128 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosPlayer.kt +550 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosRemote.kt +99 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosRouteProviderService.kt +201 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosStreamUri.kt +15 -0
- package/android/src/main/java/com/audiobrowser/sonos/SonosTransport.kt +66 -0
- package/android/src/main/java/com/audiobrowser/sonos/SpeakerXml.kt +26 -0
- package/android/src/main/java/com/audiobrowser/sonos/SsdpDiscovery.kt +63 -0
- package/android/src/main/java/com/audiobrowser/sonos/SsdpMessages.kt +63 -0
- package/android/src/main/java/com/audiobrowser/sonos/ZoneGroupState.kt +68 -0
- package/android/src/main/java/com/audiobrowser/util/OutputMonitor.kt +2 -1
- package/android/src/nocast/java/com/audiobrowser/cast/CastSupport.kt +18 -0
- package/android/src/test/java/com/audiobrowser/RemoteOutputsTest.kt +20 -3
- package/android/src/test/java/com/audiobrowser/model/PlayerSetupOptionsTest.kt +35 -0
- package/android/src/test/java/com/audiobrowser/player/EqualizerControllerTest.kt +12 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerCastTest.kt +136 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerSonosTest.kt +213 -0
- package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +275 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteItemTest.kt +61 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteOutputsControllerTest.kt +77 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteQueueTest.kt +101 -0
- package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +815 -0
- package/android/src/test/java/com/audiobrowser/sonos/DeviceDescriptionParserTest.kt +203 -0
- package/android/src/test/java/com/audiobrowser/sonos/DidlLiteTest.kt +90 -0
- package/android/src/test/java/com/audiobrowser/sonos/SoapClientTest.kt +73 -0
- package/android/src/test/java/com/audiobrowser/sonos/SoapEnvelopesTest.kt +135 -0
- package/android/src/test/java/com/audiobrowser/sonos/SoapResponseParserTest.kt +170 -0
- package/android/src/test/java/com/audiobrowser/sonos/SonosDiscovererTest.kt +174 -0
- package/android/src/test/java/com/audiobrowser/sonos/SonosPlayerTest.kt +691 -0
- package/android/src/test/java/com/audiobrowser/sonos/SonosRemoteTest.kt +129 -0
- package/android/src/test/java/com/audiobrowser/sonos/SonosRoutesTest.kt +165 -0
- package/android/src/test/java/com/audiobrowser/sonos/SonosStreamUriTest.kt +64 -0
- package/android/src/test/java/com/audiobrowser/sonos/SonosTransportTest.kt +204 -0
- package/android/src/test/java/com/audiobrowser/sonos/SsdpMessagesTest.kt +99 -0
- package/android/src/test/java/com/audiobrowser/sonos/ZoneGroupStateTest.kt +139 -0
- package/android/src/test/java/com/audiobrowser/util/OutputMonitorTest.kt +12 -10
- package/android/src/test/resources/sonos/device_description.xml +78 -0
- package/android/src/testCast/java/com/audiobrowser/cast/CastEndsTest.kt +56 -0
- package/android/src/testCast/java/com/audiobrowser/cast/SendingMediaItemConverterTest.kt +79 -0
- package/lib/commonjs/features/player/setup.js +2 -0
- package/lib/commonjs/features/player/setup.js.map +1 -1
- package/lib/module/features/player/setup.js +2 -0
- package/lib/module/features/player/setup.js.map +1 -1
- package/lib/typescript/src/features/player/setup.d.ts +37 -7
- package/lib/typescript/src/features/player/setup.d.ts.map +1 -1
- package/lib/typescript/src/specs/audio-browser.nitro.d.ts +3 -3
- package/lib/typescript/src/specs/audio-browser.nitro.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.cpp +4 -0
- package/nitrogen/generated/android/c++/JNativeSetupPlayerOptions.hpp +7 -5
- package/nitrogen/generated/android/c++/JOutputType.hpp +3 -0
- package/nitrogen/generated/android/c++/JRemoteOutputsOptions.hpp +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/NativeSetupPlayerOptions.kt +3 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/OutputType.kt +3 -2
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/RemoteOutputsOptions.kt +61 -0
- package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Bridge.hpp +18 -0
- package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Umbrella.hpp +3 -0
- package/nitrogen/generated/ios/c++/HybridAudioBrowserSpecSwift.hpp +3 -0
- package/nitrogen/generated/ios/swift/NativeSetupPlayerOptions.swift +5 -12
- package/nitrogen/generated/ios/swift/OutputType.swift +4 -0
- package/nitrogen/generated/ios/swift/RemoteOutputsOptions.swift +78 -0
- package/nitrogen/generated/shared/c++/NativeSetupPlayerOptions.hpp +8 -5
- package/nitrogen/generated/shared/c++/OutputType.hpp +6 -2
- package/nitrogen/generated/shared/c++/RemoteOutputsOptions.hpp +92 -0
- package/package.json +1 -1
- package/src/features/player/setup.ts +38 -7
- package/src/specs/audio-browser.nitro.ts +4 -3
- package/website/guide/audio-output.md +185 -4
- package/website/guide/configuration.md +6 -0
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
import android.os.Looper
|
|
4
|
+
import android.os.SystemClock
|
|
5
|
+
import androidx.media3.common.C
|
|
6
|
+
import androidx.media3.common.DeviceInfo
|
|
7
|
+
import androidx.media3.common.MediaItem
|
|
8
|
+
import androidx.media3.common.Metadata
|
|
9
|
+
import androidx.media3.common.PlaybackException
|
|
10
|
+
import androidx.media3.common.Player
|
|
11
|
+
import androidx.media3.common.SimpleBasePlayer
|
|
12
|
+
import androidx.media3.common.util.Util
|
|
13
|
+
import androidx.media3.extractor.metadata.icy.IcyInfo
|
|
14
|
+
import com.audiobrowser.remote.isRemoteLive
|
|
15
|
+
import com.google.common.util.concurrent.Futures
|
|
16
|
+
import com.google.common.util.concurrent.ListenableFuture
|
|
17
|
+
import kotlinx.coroutines.CancellationException
|
|
18
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
19
|
+
import kotlinx.coroutines.CoroutineScope
|
|
20
|
+
import kotlinx.coroutines.Job
|
|
21
|
+
import kotlinx.coroutines.NonCancellable
|
|
22
|
+
import kotlinx.coroutines.delay
|
|
23
|
+
import kotlinx.coroutines.isActive
|
|
24
|
+
import kotlinx.coroutines.launch
|
|
25
|
+
import kotlinx.coroutines.sync.Mutex
|
|
26
|
+
import kotlinx.coroutines.sync.withLock
|
|
27
|
+
import kotlinx.coroutines.withContext
|
|
28
|
+
import timber.log.Timber
|
|
29
|
+
|
|
30
|
+
/** Where a room is in what it plays, from `GetPositionInfo`; a field it doesn't report is null. */
|
|
31
|
+
internal data class SonosPosition(
|
|
32
|
+
val streamContent: String?,
|
|
33
|
+
val positionMs: Long?,
|
|
34
|
+
val durationMs: Long?,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
/** The UPnP control of one Sonos room, as [SonosPlayer] drives it. Every call blocks. */
|
|
38
|
+
internal interface SonosControl {
|
|
39
|
+
fun setUri(
|
|
40
|
+
streamUrl: String,
|
|
41
|
+
title: String,
|
|
42
|
+
artist: String?,
|
|
43
|
+
album: String?,
|
|
44
|
+
artworkUri: String?,
|
|
45
|
+
live: Boolean,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
fun play()
|
|
49
|
+
|
|
50
|
+
fun pause()
|
|
51
|
+
|
|
52
|
+
fun stop()
|
|
53
|
+
|
|
54
|
+
fun seek(positionMs: Long)
|
|
55
|
+
|
|
56
|
+
fun getTransportState(): String?
|
|
57
|
+
|
|
58
|
+
fun getPosition(): SonosPosition?
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The URI the room was last told to play, as it was sent; the track it plays may be another, the
|
|
62
|
+
* one a redirect led to.
|
|
63
|
+
*/
|
|
64
|
+
fun getMediaUri(): String?
|
|
65
|
+
|
|
66
|
+
fun getVolume(): Int?
|
|
67
|
+
|
|
68
|
+
fun setVolume(volume: Int)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Plays one item at a time on a Sonos room: Media3 commands in, UPnP out, and the room's transport
|
|
73
|
+
* state, position, stream title and volume polled back in. As a
|
|
74
|
+
* [com.audiobrowser.remote.RemotePlayback]'s player, setting its item loads it on the speaker from
|
|
75
|
+
* the start position given, without [prepare]. Each poll that reads it playing calls [onPlaying]. A
|
|
76
|
+
* track that plays to its end calls [onEnded]; one stopped there early reads as paused. Once
|
|
77
|
+
* another app or the Sonos app plays something else there, it reads as paused and leaves the room
|
|
78
|
+
* alone until played again. A title the speaker reads from the stream arrives as an [IcyInfo] in
|
|
79
|
+
* the timed metadata, where the local player reports its own. The room's volume is read when the
|
|
80
|
+
* player starts and with each poll, and goes to [onVolume].
|
|
81
|
+
*
|
|
82
|
+
* Commands run in the order they are made, one at a time, off the main thread, and run to the end
|
|
83
|
+
* even once the player is released, so its last stop reaches the speaker. A failed command that no
|
|
84
|
+
* later one replaced goes to [onError] and leaves the player idle; so does a speaker that stops
|
|
85
|
+
* answering [unreachableAfterPolls] polls in a row. An item the speaker never starts is the
|
|
86
|
+
* switch's to time out. A room that played on after a failure still hears pause and stop. A failed
|
|
87
|
+
* seek or volume change is only logged.
|
|
88
|
+
*/
|
|
89
|
+
internal class SonosPlayer(
|
|
90
|
+
looper: Looper,
|
|
91
|
+
private val control: SonosControl,
|
|
92
|
+
private val onEnded: () -> Unit,
|
|
93
|
+
private val onPlaying: () -> Unit,
|
|
94
|
+
private val onError: (PlaybackException) -> Unit,
|
|
95
|
+
private val onVolume: (Int) -> Unit,
|
|
96
|
+
private val routingControllerId: () -> String?,
|
|
97
|
+
private val scope: CoroutineScope,
|
|
98
|
+
private val io: CoroutineDispatcher,
|
|
99
|
+
private val pollIntervalMs: Long = 2_000,
|
|
100
|
+
private val unreachableAfterPolls: Int = 3,
|
|
101
|
+
private val nowMs: () -> Long = SystemClock::elapsedRealtime,
|
|
102
|
+
) : SimpleBasePlayer(looper) {
|
|
103
|
+
|
|
104
|
+
private var item: MediaItem? = null
|
|
105
|
+
private var playWhenReady = false
|
|
106
|
+
private var playWhenReadyReason = Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST
|
|
107
|
+
private var playbackState = Player.STATE_IDLE
|
|
108
|
+
private var volume: Int? = null
|
|
109
|
+
private var timedMetadata: Metadata? = null
|
|
110
|
+
|
|
111
|
+
// The track's duration and position as last read; while it reads as playing, the time of that
|
|
112
|
+
// reading, from which its position runs on.
|
|
113
|
+
private var durationMs = C.TIME_UNSET
|
|
114
|
+
private var position: PositionSupplier = PositionSupplier.getConstant(0)
|
|
115
|
+
private var readPositionMs = 0L
|
|
116
|
+
private var playingSinceReadAtMs: Long? = null
|
|
117
|
+
|
|
118
|
+
// The URL the speaker holds for us, until stopped or taken over by another app; whether it holds
|
|
119
|
+
// it ready to play without loading again; when it was asked to play; whether it has since.
|
|
120
|
+
private var loadedUrl: String? = null
|
|
121
|
+
private var ready = false
|
|
122
|
+
private var seenPlaying = false
|
|
123
|
+
|
|
124
|
+
// A poll that overlapped a command is dropped: its reading may predate it. A failed command is an
|
|
125
|
+
// error only while no later one has been made.
|
|
126
|
+
private var generation = 0
|
|
127
|
+
private var pendingCommands = 0
|
|
128
|
+
private val commands = Mutex()
|
|
129
|
+
private var polling: Job? = null
|
|
130
|
+
private var failedPolls = 0
|
|
131
|
+
|
|
132
|
+
init {
|
|
133
|
+
command(fatal = false) {
|
|
134
|
+
val read = control.getVolume() ?: return@command
|
|
135
|
+
scope.launch { if (volume == null) setVolumeRead(read) }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
override fun getState(): State {
|
|
140
|
+
val current = item
|
|
141
|
+
val live = current?.isRemoteLive == true
|
|
142
|
+
return State.Builder()
|
|
143
|
+
.setAvailableCommands(if (volume == null) COMMANDS else COMMANDS_WITH_VOLUME)
|
|
144
|
+
.setPlayWhenReady(playWhenReady, playWhenReadyReason)
|
|
145
|
+
.setPlaybackState(if (current == null) Player.STATE_IDLE else playbackState)
|
|
146
|
+
.setPlaylist(
|
|
147
|
+
listOfNotNull(current).map {
|
|
148
|
+
MediaItemData.Builder(UID)
|
|
149
|
+
.setMediaItem(it)
|
|
150
|
+
.setIsSeekable(!live)
|
|
151
|
+
.setIsDynamic(live)
|
|
152
|
+
.apply { if (live) setLiveConfiguration(it.liveConfiguration) }
|
|
153
|
+
.setDurationUs(
|
|
154
|
+
if (!live && durationMs != C.TIME_UNSET) Util.msToUs(durationMs) else C.TIME_UNSET
|
|
155
|
+
)
|
|
156
|
+
.build()
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
.setCurrentMediaItemIndex(if (current == null) C.INDEX_UNSET else 0)
|
|
160
|
+
.setContentPositionMs(position)
|
|
161
|
+
.setDeviceInfo(
|
|
162
|
+
DeviceInfo.Builder(DeviceInfo.PLAYBACK_TYPE_REMOTE)
|
|
163
|
+
.setMaxVolume(MAX_VOLUME)
|
|
164
|
+
.setRoutingControllerId(routingControllerId())
|
|
165
|
+
.build()
|
|
166
|
+
)
|
|
167
|
+
.setDeviceVolume(volume ?: 0)
|
|
168
|
+
.apply { timedMetadata?.let(::setTimedMetadata) }
|
|
169
|
+
.build()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
override fun handlePrepare(): ListenableFuture<*> {
|
|
173
|
+
if (!isPrepared) load(C.TIME_UNSET)
|
|
174
|
+
return done()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
override fun handleSetPlayWhenReady(playWhenReady: Boolean): ListenableFuture<*> {
|
|
178
|
+
this.playWhenReady = playWhenReady
|
|
179
|
+
playWhenReadyReason = Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST
|
|
180
|
+
if (!playWhenReady) {
|
|
181
|
+
freezePosition()
|
|
182
|
+
if (loadedUrl != null) command { pauseOrStop() }
|
|
183
|
+
return done()
|
|
184
|
+
}
|
|
185
|
+
if (!isPrepared) return done()
|
|
186
|
+
if (ready) {
|
|
187
|
+
command { control.play() }
|
|
188
|
+
} else {
|
|
189
|
+
load(C.TIME_UNSET)
|
|
190
|
+
}
|
|
191
|
+
return done()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
override fun handleSetMediaItems(
|
|
195
|
+
mediaItems: List<MediaItem>,
|
|
196
|
+
startIndex: Int,
|
|
197
|
+
startPositionMs: Long,
|
|
198
|
+
): ListenableFuture<*> {
|
|
199
|
+
item = mediaItems.getOrNull(if (startIndex == C.INDEX_UNSET) 0 else startIndex)
|
|
200
|
+
load(startPositionMs)
|
|
201
|
+
return done()
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
override fun handleSeek(
|
|
205
|
+
mediaItemIndex: Int,
|
|
206
|
+
positionMs: Long,
|
|
207
|
+
seekCommand: Int,
|
|
208
|
+
): ListenableFuture<*> {
|
|
209
|
+
val current = item ?: return done()
|
|
210
|
+
if (!isPrepared) return done()
|
|
211
|
+
if (current.isRemoteLive || positionMs == C.TIME_UNSET || !ready) {
|
|
212
|
+
load(positionMs)
|
|
213
|
+
return done()
|
|
214
|
+
}
|
|
215
|
+
readPositionMs = positionMs
|
|
216
|
+
if (playingSinceReadAtMs != null) {
|
|
217
|
+
playingSinceReadAtMs = nowMs()
|
|
218
|
+
position = PositionSupplier.getExtrapolating(positionMs, 1f)
|
|
219
|
+
} else {
|
|
220
|
+
position = PositionSupplier.getConstant(positionMs)
|
|
221
|
+
}
|
|
222
|
+
command(fatal = false) { control.seek(positionMs) }
|
|
223
|
+
return done()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
override fun handleStop(): ListenableFuture<*> {
|
|
227
|
+
stopPolling()
|
|
228
|
+
freezePosition()
|
|
229
|
+
if (loadedUrl != null) command { control.stop() }
|
|
230
|
+
loadedUrl = null
|
|
231
|
+
ready = false
|
|
232
|
+
playbackState = Player.STATE_IDLE
|
|
233
|
+
timedMetadata = null
|
|
234
|
+
return done()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
override fun handleRelease(): ListenableFuture<*> {
|
|
238
|
+
stopPolling()
|
|
239
|
+
generation++
|
|
240
|
+
return done()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
override fun handleSetDeviceVolume(deviceVolume: Int, flags: Int): ListenableFuture<*> {
|
|
244
|
+
val target = deviceVolume.coerceIn(0, MAX_VOLUME)
|
|
245
|
+
volume = target
|
|
246
|
+
onVolume(target)
|
|
247
|
+
command(fatal = false) { control.setVolume(target) }
|
|
248
|
+
return done()
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
override fun handleIncreaseDeviceVolume(flags: Int): ListenableFuture<*> =
|
|
252
|
+
volume?.let { handleSetDeviceVolume(it + 1, flags) } ?: done()
|
|
253
|
+
|
|
254
|
+
override fun handleDecreaseDeviceVolume(flags: Int): ListenableFuture<*> =
|
|
255
|
+
volume?.let { handleSetDeviceVolume(it - 1, flags) } ?: done()
|
|
256
|
+
|
|
257
|
+
private val isPrepared
|
|
258
|
+
get() = playbackState != Player.STATE_IDLE
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Loads the item on the speaker from [startPositionMs], playing it if asked, and polls the room.
|
|
262
|
+
*/
|
|
263
|
+
private fun load(startPositionMs: Long) {
|
|
264
|
+
val current = item ?: return
|
|
265
|
+
val url = current.localConfiguration?.uri?.toString() ?: return
|
|
266
|
+
val metadata = current.mediaMetadata
|
|
267
|
+
val live = current.isRemoteLive
|
|
268
|
+
val play = playWhenReady
|
|
269
|
+
val seekMs = startPositionMs.takeIf { !live && it != C.TIME_UNSET && it > 0 }
|
|
270
|
+
playbackState = Player.STATE_BUFFERING
|
|
271
|
+
loadedUrl = url
|
|
272
|
+
ready = true
|
|
273
|
+
seenPlaying = false
|
|
274
|
+
timedMetadata = null
|
|
275
|
+
durationMs = C.TIME_UNSET
|
|
276
|
+
readPositionMs = seekMs ?: 0
|
|
277
|
+
position = PositionSupplier.getConstant(readPositionMs)
|
|
278
|
+
playingSinceReadAtMs = null
|
|
279
|
+
command {
|
|
280
|
+
control.setUri(
|
|
281
|
+
streamUrl = url,
|
|
282
|
+
title = metadata.station?.toString() ?: metadata.title?.toString() ?: "",
|
|
283
|
+
artist = metadata.artist?.toString(),
|
|
284
|
+
album = metadata.albumTitle?.toString(),
|
|
285
|
+
artworkUri = metadata.artworkUri?.toString(),
|
|
286
|
+
live = live,
|
|
287
|
+
)
|
|
288
|
+
if (seekMs != null) {
|
|
289
|
+
try {
|
|
290
|
+
control.seek(seekMs)
|
|
291
|
+
} catch (e: SoapException) {
|
|
292
|
+
Timber.w(e, "Sonos: could not start the track at %d ms", seekMs)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (play) control.play()
|
|
296
|
+
}
|
|
297
|
+
startPolling()
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** A radio stream often refuses Pause (UPnP 701): it is stopped instead, and Play restarts it. */
|
|
301
|
+
private fun pauseOrStop() {
|
|
302
|
+
try {
|
|
303
|
+
control.pause()
|
|
304
|
+
} catch (e: SoapException) {
|
|
305
|
+
control.stop()
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Runs [block] off the main thread after the commands before it. A [fatal] command's failure is
|
|
311
|
+
* the player's while no later fatal command has been made.
|
|
312
|
+
*/
|
|
313
|
+
private fun command(fatal: Boolean = true, block: () -> Unit) {
|
|
314
|
+
val issued = if (fatal) ++generation else generation
|
|
315
|
+
pendingCommands++
|
|
316
|
+
scope.launch(NonCancellable) {
|
|
317
|
+
commands.withLock {
|
|
318
|
+
try {
|
|
319
|
+
withContext(io) { block() }
|
|
320
|
+
} catch (e: Exception) {
|
|
321
|
+
Timber.w(e, "Sonos: command failed")
|
|
322
|
+
if (fatal && generation == issued) {
|
|
323
|
+
fail(
|
|
324
|
+
PlaybackException("Sonos: ${e.message}", e, PlaybackException.ERROR_CODE_REMOTE_ERROR)
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
} finally {
|
|
328
|
+
pendingCommands--
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Idle, with the room still held: what played on there still hears pause and stop. */
|
|
335
|
+
private fun fail(e: PlaybackException) {
|
|
336
|
+
playbackState = Player.STATE_IDLE
|
|
337
|
+
ready = false
|
|
338
|
+
freezePosition()
|
|
339
|
+
stopPolling()
|
|
340
|
+
invalidateState()
|
|
341
|
+
onError(e)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private fun setVolumeRead(read: Int) {
|
|
345
|
+
if (read == volume) return
|
|
346
|
+
volume = read
|
|
347
|
+
onVolume(read)
|
|
348
|
+
invalidateState()
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private fun startPolling() {
|
|
352
|
+
if (polling?.isActive == true) return
|
|
353
|
+
failedPolls = 0
|
|
354
|
+
polling =
|
|
355
|
+
scope.launch {
|
|
356
|
+
while (isActive) {
|
|
357
|
+
poll()
|
|
358
|
+
delay(pollIntervalMs)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private fun stopPolling() {
|
|
364
|
+
polling?.cancel()
|
|
365
|
+
polling = null
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
private suspend fun poll() {
|
|
369
|
+
val reading =
|
|
370
|
+
try {
|
|
371
|
+
commands.withLock {
|
|
372
|
+
withContext(io) {
|
|
373
|
+
Reading(
|
|
374
|
+
control.getTransportState(),
|
|
375
|
+
control.getPosition(),
|
|
376
|
+
control.getMediaUri(),
|
|
377
|
+
control.getVolume(),
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
} catch (e: CancellationException) {
|
|
382
|
+
throw e
|
|
383
|
+
} catch (e: Exception) {
|
|
384
|
+
Timber.w(e, "Sonos: poll failed")
|
|
385
|
+
if (++failedPolls >= unreachableAfterPolls && isPrepared) {
|
|
386
|
+
fail(
|
|
387
|
+
PlaybackException(
|
|
388
|
+
"Sonos: the speaker doesn't answer",
|
|
389
|
+
e,
|
|
390
|
+
PlaybackException.ERROR_CODE_REMOTE_ERROR,
|
|
391
|
+
)
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
return
|
|
395
|
+
}
|
|
396
|
+
failedPolls = 0
|
|
397
|
+
if (pendingCommands > 0 || !isPrepared) return
|
|
398
|
+
apply(reading)
|
|
399
|
+
invalidateState()
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private class Reading(
|
|
403
|
+
val transportState: String?,
|
|
404
|
+
val position: SonosPosition?,
|
|
405
|
+
val mediaUri: String?,
|
|
406
|
+
val volume: Int?,
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
private fun apply(reading: Reading) {
|
|
410
|
+
reading.volume?.let(::setVolumeRead)
|
|
411
|
+
val url = loadedUrl ?: return
|
|
412
|
+
val sent = SonosStreamUri.forTransport(url, item?.isRemoteLive == true)
|
|
413
|
+
if (!reading.mediaUri.isNullOrEmpty() && reading.mediaUri != sent) {
|
|
414
|
+
onTakenOver()
|
|
415
|
+
return
|
|
416
|
+
}
|
|
417
|
+
reading.position?.streamContent?.let(::streamTitle)?.let { title ->
|
|
418
|
+
timedMetadata = Metadata(0, IcyInfo(title.toByteArray(), title, null))
|
|
419
|
+
}
|
|
420
|
+
val state = reading.transportState?.uppercase() ?: return
|
|
421
|
+
val live = item?.isRemoteLive ?: true
|
|
422
|
+
if (!live) reading.position?.durationMs?.let { durationMs = it }
|
|
423
|
+
val positionMs = if (live) 0 else reading.position?.positionMs ?: 0
|
|
424
|
+
when (state) {
|
|
425
|
+
"PLAYING" -> {
|
|
426
|
+
seenPlaying = true
|
|
427
|
+
playbackState = Player.STATE_READY
|
|
428
|
+
if (!playWhenReady) setPlayWhenReadyFromSpeaker(true)
|
|
429
|
+
readPositionMs = positionMs
|
|
430
|
+
playingSinceReadAtMs = nowMs()
|
|
431
|
+
position = PositionSupplier.getExtrapolating(positionMs, 1f)
|
|
432
|
+
onPlaying()
|
|
433
|
+
}
|
|
434
|
+
"PAUSED_PLAYBACK",
|
|
435
|
+
"PAUSED_RECORDING" -> {
|
|
436
|
+
playbackState = Player.STATE_READY
|
|
437
|
+
if (playWhenReady && seenPlaying) setPlayWhenReadyFromSpeaker(false)
|
|
438
|
+
readPositionMs = positionMs
|
|
439
|
+
playingSinceReadAtMs = null
|
|
440
|
+
position = PositionSupplier.getConstant(positionMs)
|
|
441
|
+
}
|
|
442
|
+
"TRANSITIONING" -> {
|
|
443
|
+
playbackState = Player.STATE_BUFFERING
|
|
444
|
+
freezePosition()
|
|
445
|
+
}
|
|
446
|
+
else -> onStopped()
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** Another app plays on the room: this player reads as paused and leaves the room alone. */
|
|
451
|
+
private fun onTakenOver() {
|
|
452
|
+
loadedUrl = null
|
|
453
|
+
ready = false
|
|
454
|
+
timedMetadata = null
|
|
455
|
+
playbackState = Player.STATE_READY
|
|
456
|
+
position = PositionSupplier.getConstant(0)
|
|
457
|
+
playingSinceReadAtMs = null
|
|
458
|
+
if (playWhenReady) setPlayWhenReadyFromSpeaker(false)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private fun freezePosition() {
|
|
462
|
+
position = PositionSupplier.getConstant(position.get())
|
|
463
|
+
playingSinceReadAtMs = null
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** The speaker is stopped: not started yet, done with the item, or stopped from elsewhere. */
|
|
467
|
+
private fun onStopped() {
|
|
468
|
+
if (playbackState == Player.STATE_ENDED) return
|
|
469
|
+
if (!playWhenReady) {
|
|
470
|
+
playbackState = Player.STATE_READY
|
|
471
|
+
return
|
|
472
|
+
}
|
|
473
|
+
if (!seenPlaying) return
|
|
474
|
+
ready = false
|
|
475
|
+
if (trackEnded()) {
|
|
476
|
+
playbackState = Player.STATE_ENDED
|
|
477
|
+
position = PositionSupplier.getConstant(durationMs)
|
|
478
|
+
playingSinceReadAtMs = null
|
|
479
|
+
onEnded()
|
|
480
|
+
return
|
|
481
|
+
}
|
|
482
|
+
playbackState = Player.STATE_READY
|
|
483
|
+
position = PositionSupplier.getConstant(0)
|
|
484
|
+
setPlayWhenReadyFromSpeaker(false)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Whether the track played to its end, as its last position read says. */
|
|
488
|
+
private fun trackEnded(): Boolean {
|
|
489
|
+
if (item?.isRemoteLive != false || durationMs == C.TIME_UNSET) return false
|
|
490
|
+
val reachedMs = readPositionMs + (playingSinceReadAtMs?.let { nowMs() - it } ?: 0)
|
|
491
|
+
return reachedMs >= durationMs - END_TOLERANCE_MS
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
private fun setPlayWhenReadyFromSpeaker(value: Boolean) {
|
|
495
|
+
playWhenReady = value
|
|
496
|
+
playWhenReadyReason = Player.PLAY_WHEN_READY_CHANGE_REASON_REMOTE
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
private fun done() = Futures.immediateVoidFuture()
|
|
500
|
+
|
|
501
|
+
private companion object {
|
|
502
|
+
const val MAX_VOLUME = 100
|
|
503
|
+
const val END_TOLERANCE_MS = 1_000L
|
|
504
|
+
val UID = Any()
|
|
505
|
+
val COMMANDS: Player.Commands =
|
|
506
|
+
Player.Commands.Builder()
|
|
507
|
+
.addAll(
|
|
508
|
+
Player.COMMAND_PLAY_PAUSE,
|
|
509
|
+
Player.COMMAND_PREPARE,
|
|
510
|
+
Player.COMMAND_STOP,
|
|
511
|
+
Player.COMMAND_RELEASE,
|
|
512
|
+
Player.COMMAND_SEEK_TO_DEFAULT_POSITION,
|
|
513
|
+
Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM,
|
|
514
|
+
Player.COMMAND_SET_MEDIA_ITEM,
|
|
515
|
+
Player.COMMAND_GET_CURRENT_MEDIA_ITEM,
|
|
516
|
+
Player.COMMAND_GET_TIMELINE,
|
|
517
|
+
Player.COMMAND_GET_METADATA,
|
|
518
|
+
Player.COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS,
|
|
519
|
+
)
|
|
520
|
+
.build()
|
|
521
|
+
// Reading and stepping the volume wait for it to be known.
|
|
522
|
+
val COMMANDS_WITH_VOLUME: Player.Commands =
|
|
523
|
+
COMMANDS.buildUpon()
|
|
524
|
+
.addAll(Player.COMMAND_GET_DEVICE_VOLUME, Player.COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS)
|
|
525
|
+
.build()
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* The song line in a speaker's `streamContent`: an ICY title as it is, or an HLS stream's ID3 form,
|
|
531
|
+
* `TYPE=SNG|TITLE <title>|ARTIST <artist>|…`, as `<artist> - <title>`. Null when blank, or for the
|
|
532
|
+
* speaker's own status (`ZPSTR_CONNECTING`, `ZPSTR_BUFFERING`) while it connects.
|
|
533
|
+
*/
|
|
534
|
+
internal fun streamTitle(streamContent: String): String? {
|
|
535
|
+
val content = streamContent.trim()
|
|
536
|
+
if (content.isEmpty() || content.startsWith("ZPSTR_")) return null
|
|
537
|
+
if (!content.startsWith("TYPE=")) return content
|
|
538
|
+
val fields =
|
|
539
|
+
content.split('|').associate { field ->
|
|
540
|
+
field.substringBefore(' ') to field.substringAfter(' ', "").trim()
|
|
541
|
+
}
|
|
542
|
+
val title = fields["TITLE"].orEmpty()
|
|
543
|
+
val artist = fields["ARTIST"].orEmpty()
|
|
544
|
+
return when {
|
|
545
|
+
title.isEmpty() && artist.isEmpty() -> null
|
|
546
|
+
artist.isEmpty() -> title
|
|
547
|
+
title.isEmpty() -> artist
|
|
548
|
+
else -> "$artist - $title"
|
|
549
|
+
}
|
|
550
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.media.MediaRouter2
|
|
5
|
+
import android.os.Build
|
|
6
|
+
import com.audiobrowser.remote.RemotePlayback
|
|
7
|
+
import kotlinx.coroutines.CoroutineScope
|
|
8
|
+
import kotlinx.coroutines.Dispatchers
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The Sonos room selected in the Output Switcher ([SonosOutputs.selected]) as a remote output: a
|
|
12
|
+
* session while one is selected, with a [SonosPlayer] of its own. Selecting another room starts its
|
|
13
|
+
* session in place of the last, so playback moves from room to room.
|
|
14
|
+
*/
|
|
15
|
+
internal class SonosRemote(
|
|
16
|
+
private val context: Context,
|
|
17
|
+
private val scope: CoroutineScope,
|
|
18
|
+
private val controlOf: (SonosRoom) -> SonosControl = defaultControl(),
|
|
19
|
+
private val routingControllerId: () -> String? = systemRoutingControllerId(context),
|
|
20
|
+
) : RemotePlayback {
|
|
21
|
+
private var room: SonosRoom? = null
|
|
22
|
+
private var session: SonosPlayer? = null
|
|
23
|
+
private var listener: RemotePlayback.Listener? = null
|
|
24
|
+
private val onSelectionChanged: () -> Unit = ::follow
|
|
25
|
+
|
|
26
|
+
init {
|
|
27
|
+
SonosOutputs.addListener(onSelectionChanged)
|
|
28
|
+
follow()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
override val player: SonosPlayer
|
|
32
|
+
get() = checkNotNull(session) { "No Sonos room is selected" }
|
|
33
|
+
|
|
34
|
+
override val isSessionAvailable: Boolean
|
|
35
|
+
get() = session != null
|
|
36
|
+
|
|
37
|
+
override fun setListener(listener: RemotePlayback.Listener?) {
|
|
38
|
+
this.listener = listener
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override fun release() {
|
|
42
|
+
SonosOutputs.removeListener(onSelectionChanged)
|
|
43
|
+
end(session, room)
|
|
44
|
+
session = null
|
|
45
|
+
room = null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private fun follow() {
|
|
49
|
+
val selected = SonosOutputs.selected
|
|
50
|
+
if (selected == room) return
|
|
51
|
+
val ended = session
|
|
52
|
+
val endedRoom = room
|
|
53
|
+
room = selected
|
|
54
|
+
session = selected?.let(::start)
|
|
55
|
+
listener?.onSessionChanged()
|
|
56
|
+
end(ended, endedRoom)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun start(room: SonosRoom): SonosPlayer {
|
|
60
|
+
lateinit var started: SonosPlayer
|
|
61
|
+
started =
|
|
62
|
+
SonosPlayer(
|
|
63
|
+
context.mainLooper,
|
|
64
|
+
controlOf(room),
|
|
65
|
+
onEnded = { if (session === started) listener?.onEnded() },
|
|
66
|
+
onPlaying = { if (session === started) listener?.onPlaying() },
|
|
67
|
+
onError = { if (session === started) listener?.onFailed(it) },
|
|
68
|
+
onVolume = { SonosOutputs.reportVolume(room.id, it) },
|
|
69
|
+
routingControllerId = routingControllerId,
|
|
70
|
+
scope = scope,
|
|
71
|
+
io = Dispatchers.IO,
|
|
72
|
+
)
|
|
73
|
+
SonosOutputs.setSession(room.id) { started.setDeviceVolume(it, 0) }
|
|
74
|
+
return started
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private fun end(player: SonosPlayer?, room: SonosRoom?) {
|
|
78
|
+
room?.let { SonosOutputs.setSession(it.id, null) }
|
|
79
|
+
player?.release()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private companion object {
|
|
83
|
+
/**
|
|
84
|
+
* The system's routing session for the selected room, which ties the media session to it:
|
|
85
|
+
* without it, the notification's output chip can't open the Output Switcher to move playback
|
|
86
|
+
* back.
|
|
87
|
+
*/
|
|
88
|
+
fun systemRoutingControllerId(context: Context): () -> String? {
|
|
89
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return { null }
|
|
90
|
+
val router by lazy { MediaRouter2.getInstance(context) }
|
|
91
|
+
return { router.controllers.firstOrNull { it != router.systemController }?.id }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fun defaultControl(): (SonosRoom) -> SonosControl {
|
|
95
|
+
val soap = SoapClient(sonosHttpClient)
|
|
96
|
+
return { room -> SonosTransport(room.device, soap) }
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|