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,201 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.IntentFilter
|
|
5
|
+
import android.net.wifi.WifiManager
|
|
6
|
+
import androidx.annotation.VisibleForTesting
|
|
7
|
+
import androidx.mediarouter.media.MediaRouteDescriptor
|
|
8
|
+
import androidx.mediarouter.media.MediaRouteDiscoveryRequest
|
|
9
|
+
import androidx.mediarouter.media.MediaRouteProvider
|
|
10
|
+
import androidx.mediarouter.media.MediaRouteProviderDescriptor
|
|
11
|
+
import androidx.mediarouter.media.MediaRouteProviderService
|
|
12
|
+
import androidx.mediarouter.media.MediaRouter
|
|
13
|
+
import com.audiobrowser.RemoteOutputs
|
|
14
|
+
import kotlinx.coroutines.CoroutineScope
|
|
15
|
+
import kotlinx.coroutines.Dispatchers
|
|
16
|
+
import kotlinx.coroutines.Job
|
|
17
|
+
import kotlinx.coroutines.SupervisorJob
|
|
18
|
+
import kotlinx.coroutines.cancel
|
|
19
|
+
import kotlinx.coroutines.delay
|
|
20
|
+
import kotlinx.coroutines.isActive
|
|
21
|
+
import kotlinx.coroutines.launch
|
|
22
|
+
import kotlinx.coroutines.withContext
|
|
23
|
+
import timber.log.Timber
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Serves the Sonos rooms as media routes, so the system Output Switcher lists them. Bound by the
|
|
27
|
+
* system, not the app: [SonosOutputs.setEnabled] turns the component on.
|
|
28
|
+
*/
|
|
29
|
+
class SonosRouteProviderService : MediaRouteProviderService() {
|
|
30
|
+
private var provider: SonosRouteProvider? = null
|
|
31
|
+
|
|
32
|
+
override fun onCreateMediaRouteProvider(): MediaRouteProvider =
|
|
33
|
+
SonosRouteProvider(this).also { provider = it }
|
|
34
|
+
|
|
35
|
+
override fun onDestroy() {
|
|
36
|
+
provider?.release()
|
|
37
|
+
super.onDestroy()
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@VisibleForTesting
|
|
42
|
+
internal class SonosRouteProvider(context: Context) : MediaRouteProvider(context) {
|
|
43
|
+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
|
44
|
+
private val discoverer =
|
|
45
|
+
SonosDiscoverer(sonosHttpClient, SsdpDiscovery(wifiMulticastLock(context.applicationContext)))
|
|
46
|
+
private val soap = SoapClient(sonosHttpClient)
|
|
47
|
+
|
|
48
|
+
private var rooms: List<SonosRoom> = listOfNotNull(SonosOutputs.selected)
|
|
49
|
+
private var lastScanMs = 0L
|
|
50
|
+
private var scanJob: Job? = null
|
|
51
|
+
private val onVolumeChanged: () -> Unit = ::publish
|
|
52
|
+
|
|
53
|
+
init {
|
|
54
|
+
SonosOutputs.addVolumeListener(onVolumeChanged)
|
|
55
|
+
// A new provider, after the system rebound the service, lists the room still selected, which
|
|
56
|
+
// MediaRouter2 looks up in the descriptor before it creates the room's session.
|
|
57
|
+
publish()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
override fun onDiscoveryRequestChanged(request: MediaRouteDiscoveryRequest?) {
|
|
61
|
+
scanJob?.cancel()
|
|
62
|
+
scanJob = null
|
|
63
|
+
if (request == null || !request.selector.hasControlCategory(RemoteOutputs.CATEGORY)) return
|
|
64
|
+
scanJob =
|
|
65
|
+
scope.launch {
|
|
66
|
+
// Passive discovery keeps what a recent scan found; the open switcher asks for active.
|
|
67
|
+
if (!request.isActiveScan && System.currentTimeMillis() - lastScanMs < PASSIVE_RESCAN_MS) {
|
|
68
|
+
return@launch
|
|
69
|
+
}
|
|
70
|
+
while (isActive) {
|
|
71
|
+
val found = withContext(Dispatchers.IO) { discoverer.discover() }
|
|
72
|
+
lastScanMs = System.currentTimeMillis()
|
|
73
|
+
// A room selected now stays listed when one scan misses it: SSDP is lossy.
|
|
74
|
+
val selected = SonosOutputs.selected
|
|
75
|
+
rooms =
|
|
76
|
+
if (selected != null && found.none { it.id == selected.id }) found + selected else found
|
|
77
|
+
publish()
|
|
78
|
+
if (!request.isActiveScan) break
|
|
79
|
+
delay(ACTIVE_RESCAN_MS)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fun release() {
|
|
85
|
+
SonosOutputs.removeVolumeListener(onVolumeChanged)
|
|
86
|
+
scope.cancel()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
override fun onCreateRouteController(routeId: String): RouteController? {
|
|
90
|
+
val room = rooms.firstOrNull { it.id == routeId } ?: return null
|
|
91
|
+
return SonosRouteController(room)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private fun publish() {
|
|
95
|
+
descriptor = sonosRouteDescriptors(rooms, SonosOutputs.selected?.id, SonosOutputs::volumeOf)
|
|
96
|
+
Timber.d("Sonos: %d room(s)", rooms.size)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Reads [room]'s volume itself, for a room no session plays on, which would read it instead. */
|
|
100
|
+
private suspend fun readVolume(room: SonosRoom): Int? {
|
|
101
|
+
val volume =
|
|
102
|
+
withContext(Dispatchers.IO) {
|
|
103
|
+
runCatching { SonosTransport(room.device, soap).getVolume() }.getOrNull()
|
|
104
|
+
} ?: return null
|
|
105
|
+
SonosOutputs.reportVolume(room.id, volume)
|
|
106
|
+
return volume
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private fun setVolume(room: SonosRoom, volume: Int) {
|
|
110
|
+
if (SonosOutputs.setVolumeThroughSession(room.id, volume)) return
|
|
111
|
+
SonosOutputs.reportVolume(room.id, volume)
|
|
112
|
+
scope.launch(Dispatchers.IO) {
|
|
113
|
+
runCatching { SonosTransport(room.device, soap).setVolume(volume) }
|
|
114
|
+
.onFailure { Timber.w(it, "Sonos: failed to set the volume of %s", room.name) }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private inner class SonosRouteController(private val room: SonosRoom) : RouteController() {
|
|
119
|
+
override fun onSelect() {
|
|
120
|
+
SonosOutputs.select(room)
|
|
121
|
+
publish()
|
|
122
|
+
if (!SonosOutputs.hasSession(room.id)) scope.launch { readVolume(room) }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
override fun onUnselect(reason: Int) {
|
|
126
|
+
SonosOutputs.unselect(room)
|
|
127
|
+
publish()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
override fun onSetVolume(volume: Int) = setVolume(room, volume.coerceIn(0, MAX_VOLUME))
|
|
131
|
+
|
|
132
|
+
override fun onUpdateVolume(delta: Int) {
|
|
133
|
+
scope.launch {
|
|
134
|
+
// From the speaker's own volume: the Sonos app or the speaker's buttons may have moved it.
|
|
135
|
+
// A session reads it as it plays.
|
|
136
|
+
val current =
|
|
137
|
+
(if (SonosOutputs.hasSession(room.id)) null else readVolume(room))
|
|
138
|
+
?: SonosOutputs.volumeOf(room.id)
|
|
139
|
+
?: return@launch
|
|
140
|
+
setVolume(room, (current + delta).coerceIn(0, MAX_VOLUME))
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
override fun onRelease() {}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private companion object {
|
|
148
|
+
const val ACTIVE_RESCAN_MS = 10_000L
|
|
149
|
+
const val PASSIVE_RESCAN_MS = 60_000L
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private const val MAX_VOLUME = 100
|
|
154
|
+
|
|
155
|
+
/** The routes for [rooms]: the selected one connected, each in both output categories. */
|
|
156
|
+
@VisibleForTesting
|
|
157
|
+
internal fun sonosRouteDescriptors(
|
|
158
|
+
rooms: List<SonosRoom>,
|
|
159
|
+
selectedId: String?,
|
|
160
|
+
volumeOf: (String) -> Int?,
|
|
161
|
+
): MediaRouteProviderDescriptor {
|
|
162
|
+
val filter =
|
|
163
|
+
IntentFilter().apply {
|
|
164
|
+
addCategory(RemoteOutputs.CATEGORY)
|
|
165
|
+
addCategory(SonosOutputs.CATEGORY)
|
|
166
|
+
}
|
|
167
|
+
val builder = MediaRouteProviderDescriptor.Builder()
|
|
168
|
+
for (room in rooms) {
|
|
169
|
+
builder.addRoute(
|
|
170
|
+
MediaRouteDescriptor.Builder(room.id, room.name)
|
|
171
|
+
.setDescription("Sonos")
|
|
172
|
+
.addControlFilter(filter)
|
|
173
|
+
.setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE)
|
|
174
|
+
.setDeviceType(MediaRouter.RouteInfo.DEVICE_TYPE_SPEAKER)
|
|
175
|
+
.setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE)
|
|
176
|
+
.setVolumeMax(MAX_VOLUME)
|
|
177
|
+
.setVolume(volumeOf(room.id) ?: 0)
|
|
178
|
+
.setConnectionState(
|
|
179
|
+
if (room.id == selectedId) MediaRouter.RouteInfo.CONNECTION_STATE_CONNECTED
|
|
180
|
+
else MediaRouter.RouteInfo.CONNECTION_STATE_DISCONNECTED
|
|
181
|
+
)
|
|
182
|
+
.setEnabled(true)
|
|
183
|
+
.build()
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
return builder.build()
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
@VisibleForTesting
|
|
190
|
+
internal fun wifiMulticastLock(context: Context): MulticastLockHandle? {
|
|
191
|
+
if (!SonosOutputs.canHoldMulticastLock(context)) return null
|
|
192
|
+
val lock =
|
|
193
|
+
(context.getSystemService(Context.WIFI_SERVICE) as WifiManager)
|
|
194
|
+
.createMulticastLock("audiobrowser-sonos")
|
|
195
|
+
.apply { setReferenceCounted(true) }
|
|
196
|
+
return object : MulticastLockHandle {
|
|
197
|
+
override fun acquire() = lock.acquire()
|
|
198
|
+
|
|
199
|
+
override fun release() = lock.release()
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `CurrentURI` a Sonos speaker plays a URL from. A live http(s) stream goes as
|
|
5
|
+
* `x-rincon-mp3radio://` followed by the whole URL, scheme included: the speaker sniffs the codec
|
|
6
|
+
* itself (MP3, AAC, HLS) and follows redirects. A track goes as its URL, since under the radio
|
|
7
|
+
* scheme the speaker reports no duration and refuses `Seek`. Any other scheme passes through
|
|
8
|
+
* unchanged.
|
|
9
|
+
*/
|
|
10
|
+
internal object SonosStreamUri {
|
|
11
|
+
fun forTransport(url: String, live: Boolean): String {
|
|
12
|
+
val scheme = url.substringBefore("://", missingDelimiterValue = "").lowercase()
|
|
13
|
+
return if (live && (scheme == "http" || scheme == "https")) "x-rincon-mp3radio://$url" else url
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* UPnP control of one Sonos device. Every call blocks on a SOAP round-trip, so call it off the main
|
|
5
|
+
* thread, and throws [SoapException] on failure.
|
|
6
|
+
*/
|
|
7
|
+
internal class SonosTransport(private val device: SonosDevice, private val soap: SoapClient) :
|
|
8
|
+
SonosControl {
|
|
9
|
+
/** Loads the stream on the device (`SetAVTransportURI`) without starting playback. */
|
|
10
|
+
override fun setUri(
|
|
11
|
+
streamUrl: String,
|
|
12
|
+
title: String,
|
|
13
|
+
artist: String?,
|
|
14
|
+
album: String?,
|
|
15
|
+
artworkUri: String?,
|
|
16
|
+
live: Boolean,
|
|
17
|
+
) {
|
|
18
|
+
val uri = SonosStreamUri.forTransport(streamUrl, live)
|
|
19
|
+
val didl =
|
|
20
|
+
DidlLite.build(
|
|
21
|
+
title = title,
|
|
22
|
+
creator = artist,
|
|
23
|
+
album = album,
|
|
24
|
+
albumArtUri = artworkUri,
|
|
25
|
+
live = live,
|
|
26
|
+
)
|
|
27
|
+
avt(SoapEnvelopes.setAvTransportUri(uri, didl))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override fun play() = avt(SoapEnvelopes.play())
|
|
31
|
+
|
|
32
|
+
override fun pause() = avt(SoapEnvelopes.pause())
|
|
33
|
+
|
|
34
|
+
override fun stop() = avt(SoapEnvelopes.stop())
|
|
35
|
+
|
|
36
|
+
override fun seek(positionMs: Long) = avt(SoapEnvelopes.seek(positionMs))
|
|
37
|
+
|
|
38
|
+
/** The device's current UPnP `CurrentTransportState`, or null if unreadable. */
|
|
39
|
+
override fun getTransportState(): String? =
|
|
40
|
+
SoapResponseParser.transportState(
|
|
41
|
+
soap.execute(device.avTransportControlUrl, SoapEnvelopes.getTransportInfo())
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
override fun getPosition(): SonosPosition? =
|
|
45
|
+
SoapResponseParser.position(
|
|
46
|
+
soap.execute(device.avTransportControlUrl, SoapEnvelopes.getPositionInfo())
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
override fun getMediaUri(): String? =
|
|
50
|
+
SoapResponseParser.currentUri(
|
|
51
|
+
soap.execute(device.avTransportControlUrl, SoapEnvelopes.getMediaInfo())
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
override fun setVolume(volume: Int) {
|
|
55
|
+
soap.execute(device.groupRenderingControlUrl, SoapEnvelopes.setGroupVolume(volume))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
override fun getVolume(): Int? =
|
|
59
|
+
SoapResponseParser.volume(
|
|
60
|
+
soap.execute(device.groupRenderingControlUrl, SoapEnvelopes.getGroupVolume())
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
private fun avt(action: SoapAction) {
|
|
64
|
+
soap.execute(device.avTransportControlUrl, action)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
import java.io.ByteArrayInputStream
|
|
4
|
+
import javax.xml.parsers.DocumentBuilderFactory
|
|
5
|
+
import okhttp3.Response
|
|
6
|
+
import org.w3c.dom.Document
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Parses XML from a device on the network, or null if it isn't XML. A document type declaration,
|
|
10
|
+
* which a speaker never sends, is refused, so no entity it declares can expand.
|
|
11
|
+
*/
|
|
12
|
+
internal fun parseSpeakerXml(xml: String): Document? {
|
|
13
|
+
if (xml.contains("<!DOCTYPE", ignoreCase = true)) return null
|
|
14
|
+
return runCatching {
|
|
15
|
+
DocumentBuilderFactory.newInstance()
|
|
16
|
+
.apply { isNamespaceAware = false }
|
|
17
|
+
.newDocumentBuilder()
|
|
18
|
+
.parse(ByteArrayInputStream(xml.toByteArray(Charsets.UTF_8)))
|
|
19
|
+
}
|
|
20
|
+
.getOrNull()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The response's body, cut at [MAX_SPEAKER_BODY_BYTES]: a longer one fails to parse. */
|
|
24
|
+
internal fun Response.speakerBody(): String = peekBody(MAX_SPEAKER_BODY_BYTES).string()
|
|
25
|
+
|
|
26
|
+
internal const val MAX_SPEAKER_BODY_BYTES = 1L shl 20
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
import java.net.DatagramPacket
|
|
4
|
+
import java.net.DatagramSocket
|
|
5
|
+
import java.net.InetAddress
|
|
6
|
+
import java.net.SocketTimeoutException
|
|
7
|
+
import timber.log.Timber
|
|
8
|
+
|
|
9
|
+
/** A Wi-Fi multicast lock, held while discovery listens for replies. */
|
|
10
|
+
internal interface MulticastLockHandle {
|
|
11
|
+
fun acquire()
|
|
12
|
+
|
|
13
|
+
fun release()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sends the SSDP `M-SEARCH` datagram to the UPnP multicast group and collects the unicast replies
|
|
18
|
+
* for a bounded window, holding the multicast lock meanwhile. Blocking: run it off the main thread.
|
|
19
|
+
*/
|
|
20
|
+
internal class SsdpDiscovery(private val multicastLock: MulticastLockHandle? = null) : SsdpScanner {
|
|
21
|
+
|
|
22
|
+
override fun search(timeoutMs: Int, repeats: Int): List<SsdpResponse> {
|
|
23
|
+
multicastLock?.acquire()
|
|
24
|
+
val responsesByUsn = LinkedHashMap<String, SsdpResponse>()
|
|
25
|
+
try {
|
|
26
|
+
DatagramSocket().use { socket ->
|
|
27
|
+
val group = InetAddress.getByName(SsdpMessages.MULTICAST_HOST)
|
|
28
|
+
val datagram = SsdpMessages.buildMSearch()
|
|
29
|
+
// Send a few times — UDP is lossy and a single probe is easily missed on busy Wi-Fi.
|
|
30
|
+
repeat(repeats.coerceAtLeast(1)) {
|
|
31
|
+
runCatching {
|
|
32
|
+
socket.send(DatagramPacket(datagram, datagram.size, group, SsdpMessages.MULTICAST_PORT))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
val deadline = System.currentTimeMillis() + timeoutMs
|
|
37
|
+
val buffer = ByteArray(RECEIVE_BUFFER_BYTES)
|
|
38
|
+
while (true) {
|
|
39
|
+
val remaining = deadline - System.currentTimeMillis()
|
|
40
|
+
if (remaining <= 0) break
|
|
41
|
+
socket.soTimeout = remaining.toInt().coerceAtLeast(1)
|
|
42
|
+
try {
|
|
43
|
+
val packet = DatagramPacket(buffer, buffer.size)
|
|
44
|
+
socket.receive(packet)
|
|
45
|
+
val raw = String(packet.data, 0, packet.length, Charsets.UTF_8)
|
|
46
|
+
SsdpMessages.parseResponse(raw)?.let { responsesByUsn.putIfAbsent(it.usn, it) }
|
|
47
|
+
} catch (_: SocketTimeoutException) {
|
|
48
|
+
break
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} catch (t: Throwable) {
|
|
53
|
+
Timber.w(t, "SSDP discovery failed")
|
|
54
|
+
} finally {
|
|
55
|
+
multicastLock?.release()
|
|
56
|
+
}
|
|
57
|
+
return responsesByUsn.values.toList()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private companion object {
|
|
61
|
+
const val RECEIVE_BUFFER_BYTES = 2048
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
/** The fields of an SSDP search response that discovery reads. */
|
|
4
|
+
internal data class SsdpResponse(
|
|
5
|
+
val location: String,
|
|
6
|
+
val usn: String,
|
|
7
|
+
val searchTarget: String?,
|
|
8
|
+
val server: String?,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
/** The two SSDP messages Sonos discovery needs: the `M-SEARCH` it sends and the replies. */
|
|
12
|
+
internal object SsdpMessages {
|
|
13
|
+
const val MULTICAST_HOST = "239.255.255.250"
|
|
14
|
+
const val MULTICAST_PORT = 1900
|
|
15
|
+
|
|
16
|
+
/** Sonos players answer M-SEARCH for the ZonePlayer device type. */
|
|
17
|
+
const val SONOS_SEARCH_TARGET = "urn:schemas-upnp-org:device:ZonePlayer:1"
|
|
18
|
+
|
|
19
|
+
private const val USER_AGENT = "AudioBrowser/1.0 UPnP/1.1"
|
|
20
|
+
|
|
21
|
+
/** Builds the SSDP `M-SEARCH` request datagram, in CRLF framing. */
|
|
22
|
+
fun buildMSearch(searchTarget: String = SONOS_SEARCH_TARGET, mx: Int = 2): ByteArray =
|
|
23
|
+
("M-SEARCH * HTTP/1.1\r\n" +
|
|
24
|
+
"HOST: $MULTICAST_HOST:$MULTICAST_PORT\r\n" +
|
|
25
|
+
"MAN: \"ssdp:discover\"\r\n" +
|
|
26
|
+
"MX: $mx\r\n" +
|
|
27
|
+
"ST: $searchTarget\r\n" +
|
|
28
|
+
"USER-AGENT: $USER_AGENT\r\n" +
|
|
29
|
+
"\r\n")
|
|
30
|
+
.toByteArray(Charsets.UTF_8)
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Parses a unicast SSDP search response. Returns null unless it is an HTTP `200` reply carrying
|
|
34
|
+
* both a `LOCATION` (the device description URL) and a `USN` (the unique identity used to
|
|
35
|
+
* dedupe). Header names are case-insensitive; values are trimmed. Tolerates bare-LF framing.
|
|
36
|
+
*/
|
|
37
|
+
fun parseResponse(raw: String): SsdpResponse? {
|
|
38
|
+
val lines = raw.replace("\r\n", "\n").split("\n")
|
|
39
|
+
val statusLine = lines.firstOrNull()?.trim() ?: return null
|
|
40
|
+
// A response status line (HTTP/x 200 ...), not a NOTIFY or M-SEARCH request.
|
|
41
|
+
val statusTokens = statusLine.split(' ')
|
|
42
|
+
if (!statusLine.startsWith("HTTP/") || statusTokens.getOrNull(1) != "200") return null
|
|
43
|
+
|
|
44
|
+
val headers = HashMap<String, String>()
|
|
45
|
+
for (line in lines.drop(1)) {
|
|
46
|
+
if (line.isBlank()) continue
|
|
47
|
+
val colon = line.indexOf(':')
|
|
48
|
+
if (colon <= 0) continue
|
|
49
|
+
val key = line.substring(0, colon).trim().lowercase()
|
|
50
|
+
val value = line.substring(colon + 1).trim()
|
|
51
|
+
headers.putIfAbsent(key, value)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
val location = headers["location"]?.takeIf { it.isNotEmpty() } ?: return null
|
|
55
|
+
val usn = headers["usn"]?.takeIf { it.isNotEmpty() } ?: return null
|
|
56
|
+
return SsdpResponse(
|
|
57
|
+
location = location,
|
|
58
|
+
usn = usn,
|
|
59
|
+
searchTarget = headers["st"],
|
|
60
|
+
server = headers["server"],
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
package com.audiobrowser.sonos
|
|
2
|
+
|
|
3
|
+
import org.w3c.dom.Element
|
|
4
|
+
|
|
5
|
+
/** A group of Sonos rooms playing together, controlled through its [coordinator]. */
|
|
6
|
+
internal data class SonosGroup(
|
|
7
|
+
/** The coordinator's `RINCON_…` UUID. */
|
|
8
|
+
val coordinator: String,
|
|
9
|
+
/** The coordinator's device description URL. */
|
|
10
|
+
val location: String,
|
|
11
|
+
val name: String,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
/** A [SonosGroup] whose coordinator's control URLs are known: what a route stands for. */
|
|
15
|
+
internal data class SonosRoom(val id: String, val name: String, val device: SonosDevice)
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parses a `ZoneGroupTopology.GetZoneGroupState` response into the groups a user can play to. A
|
|
19
|
+
* group is listed once, at its coordinator, the only member that accepts transport commands: the
|
|
20
|
+
* hidden half of a stereo pair answers SSDP too but refuses them, and can be reported as the
|
|
21
|
+
* coordinator of a group of its own, which is skipped. A home theatre's satellites are not members.
|
|
22
|
+
* A group of several rooms is named after its coordinator plus the count of the others, as the
|
|
23
|
+
* Sonos app does.
|
|
24
|
+
*/
|
|
25
|
+
internal object ZoneGroupState {
|
|
26
|
+
/**
|
|
27
|
+
* Whether the speaker [uuid] is reported hidden: the hidden half of a stereo pair, whose own
|
|
28
|
+
* topology can leave out the group of its pair.
|
|
29
|
+
*/
|
|
30
|
+
fun isHidden(xml: String, uuid: String): Boolean {
|
|
31
|
+
val doc = SoapResponseParser.embeddedDocument(xml, "ZoneGroupState") ?: return false
|
|
32
|
+
val members = doc.getElementsByTagName("ZoneGroupMember")
|
|
33
|
+
return (0 until members.length)
|
|
34
|
+
.map { members.item(it) as Element }
|
|
35
|
+
.any { it.getAttribute("UUID") == uuid && it.getAttribute("Invisible") == "1" }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The groups, or null when [xml] holds no readable `ZoneGroupState`. */
|
|
39
|
+
fun parse(xml: String): List<SonosGroup>? {
|
|
40
|
+
val doc = SoapResponseParser.embeddedDocument(xml, "ZoneGroupState") ?: return null
|
|
41
|
+
val groups = doc.getElementsByTagName("ZoneGroup")
|
|
42
|
+
return (0 until groups.length).mapNotNull { groupOf(groups.item(it) as Element) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private fun groupOf(group: Element): SonosGroup? {
|
|
46
|
+
val members =
|
|
47
|
+
(0 until group.childNodes.length)
|
|
48
|
+
.map { group.childNodes.item(it) }
|
|
49
|
+
.filterIsInstance<Element>()
|
|
50
|
+
.filter { it.tagName == "ZoneGroupMember" }
|
|
51
|
+
val coordinator =
|
|
52
|
+
members.firstOrNull { it.getAttribute("UUID") == group.getAttribute("Coordinator") }
|
|
53
|
+
?: return null
|
|
54
|
+
if (coordinator.getAttribute("Invisible") == "1") return null
|
|
55
|
+
val name = coordinator.getAttribute("ZoneName")
|
|
56
|
+
val otherRooms =
|
|
57
|
+
members
|
|
58
|
+
.filter { it.getAttribute("Invisible") != "1" }
|
|
59
|
+
.map { it.getAttribute("ZoneName") }
|
|
60
|
+
.distinct()
|
|
61
|
+
.count { it != name }
|
|
62
|
+
return SonosGroup(
|
|
63
|
+
coordinator = coordinator.getAttribute("UUID"),
|
|
64
|
+
location = coordinator.getAttribute("Location"),
|
|
65
|
+
name = if (otherRooms == 0) name else "$name + $otherRooms",
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -136,7 +136,7 @@ class OutputMonitor(private val context: Context) : DefaultLifecycleObserver {
|
|
|
136
136
|
// speaker stands in, so listeners hear that the remote output is gone.
|
|
137
137
|
val output =
|
|
138
138
|
computeOutput()
|
|
139
|
-
?: if (cached?.type
|
|
139
|
+
?: if (cached?.type in REMOTE_TYPES)
|
|
140
140
|
Output(
|
|
141
141
|
type = OutputType.SPEAKER,
|
|
142
142
|
name = defaultName(OutputType.SPEAKER),
|
|
@@ -200,6 +200,7 @@ class OutputMonitor(private val context: Context) : DefaultLifecycleObserver {
|
|
|
200
200
|
|
|
201
201
|
companion object {
|
|
202
202
|
private const val POLL_INTERVAL_MS = 1000L
|
|
203
|
+
private val REMOTE_TYPES = setOf(OutputType.REMOTE, OutputType.CAST, OutputType.SONOS)
|
|
203
204
|
|
|
204
205
|
/** Maps an [AudioDeviceInfo] type to a cross-platform [OutputType]. */
|
|
205
206
|
fun outputTypeOf(deviceType: Int): OutputType =
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
package com.audiobrowser.cast
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import com.audiobrowser.remote.RemotePlayback
|
|
5
|
+
import timber.log.Timber
|
|
6
|
+
|
|
7
|
+
/** The library is built without Cast: no Cast routes. */
|
|
8
|
+
@Suppress("UNUSED_PARAMETER")
|
|
9
|
+
internal fun castRouteCategory(receiverApplicationId: String?): String? = null
|
|
10
|
+
|
|
11
|
+
/** The library is built without Cast: always null, with a warning. */
|
|
12
|
+
@Suppress("UNUSED_PARAMETER")
|
|
13
|
+
internal fun createCastRemote(context: Context, receiverApplicationId: String?): RemotePlayback? {
|
|
14
|
+
Timber.w(
|
|
15
|
+
"Cast: remoteOutputs.cast is on, but the library was built without AudioBrowser_castEnabled=true, so playback stays local"
|
|
16
|
+
)
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
@@ -7,16 +7,33 @@ import org.junit.Assert.assertNull
|
|
|
7
7
|
import org.junit.Test
|
|
8
8
|
|
|
9
9
|
class RemoteOutputsTest {
|
|
10
|
+
private val types = mapOf("cast" to OutputType.CAST, "sonos" to OutputType.SONOS)
|
|
11
|
+
|
|
12
|
+
private fun outputOf(name: String, vararg categories: String) =
|
|
13
|
+
remoteOutputOf(name, types) { it in categories }
|
|
14
|
+
|
|
10
15
|
@Test
|
|
11
16
|
fun `a selected route in the category is a remote output`() {
|
|
12
17
|
assertEquals(
|
|
13
18
|
Output(type = OutputType.REMOTE, name = "Kitchen", external = true),
|
|
14
|
-
|
|
19
|
+
outputOf("Kitchen", RemoteOutputs.CATEGORY),
|
|
15
20
|
)
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
@Test
|
|
19
|
-
fun `a selected route outside
|
|
20
|
-
assertNull(
|
|
24
|
+
fun `a selected route outside every category leaves the output to the local device`() {
|
|
25
|
+
assertNull(outputOf("Living room TV", "other"))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@Test
|
|
29
|
+
fun `a route of a category turned on has that category's type`() {
|
|
30
|
+
assertEquals(
|
|
31
|
+
Output(type = OutputType.CAST, name = "Living room TV", external = true),
|
|
32
|
+
outputOf("Living room TV", "cast"),
|
|
33
|
+
)
|
|
34
|
+
assertEquals(
|
|
35
|
+
Output(type = OutputType.SONOS, name = "Dining Room", external = true),
|
|
36
|
+
outputOf("Dining Room", RemoteOutputs.CATEGORY, "sonos"),
|
|
37
|
+
)
|
|
21
38
|
}
|
|
22
39
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package com.audiobrowser.model
|
|
2
|
+
|
|
3
|
+
import com.margelo.nitro.audiobrowser.NativeSetupPlayerOptions
|
|
4
|
+
import com.margelo.nitro.audiobrowser.RemoteOutputsOptions
|
|
5
|
+
import org.junit.Assert.assertEquals
|
|
6
|
+
import org.junit.Assert.assertNull
|
|
7
|
+
import org.junit.Test
|
|
8
|
+
|
|
9
|
+
class PlayerSetupOptionsTest {
|
|
10
|
+
private fun setup(remoteOutputs: RemoteOutputsOptions?) =
|
|
11
|
+
NativeSetupPlayerOptions(
|
|
12
|
+
android = null,
|
|
13
|
+
ios = null,
|
|
14
|
+
options = null,
|
|
15
|
+
autoUpdateNowPlayingMetadata = null,
|
|
16
|
+
nowPlayingMetadataFormatter = null,
|
|
17
|
+
playWhenReady = null,
|
|
18
|
+
repeatMode = null,
|
|
19
|
+
retry = null,
|
|
20
|
+
keepSessionAliveOnError = null,
|
|
21
|
+
remoteOutputs = remoteOutputs,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
@Test
|
|
25
|
+
fun `a setup that leaves remote outputs out turns them off`() {
|
|
26
|
+
val options = PlayerSetupOptions()
|
|
27
|
+
val sonos = RemoteOutputsOptions(cast = null, castReceiverApplicationId = null, sonos = true)
|
|
28
|
+
|
|
29
|
+
options.update(setup(sonos))
|
|
30
|
+
assertEquals(sonos, options.remoteOutputs)
|
|
31
|
+
|
|
32
|
+
options.update(setup(null))
|
|
33
|
+
assertNull(options.remoteOutputs)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -140,6 +140,18 @@ class EqualizerControllerTest {
|
|
|
140
140
|
assertEquals("enabled", listOf(true), effect.enabledCalls)
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
@Test
|
|
144
|
+
fun `playback leaving for a receiver keeps the effect on the local session`() {
|
|
145
|
+
val fixture = Fixture()
|
|
146
|
+
fixture.controller.initialize(42)
|
|
147
|
+
|
|
148
|
+
fixture.controller.onAudioSessionChanged(C.AUDIO_SESSION_ID_UNSET)
|
|
149
|
+
|
|
150
|
+
val effect = fixture.created.single()
|
|
151
|
+
assertFalse("released", effect.released)
|
|
152
|
+
assertEquals("session", 42, effect.audioSessionId)
|
|
153
|
+
}
|
|
154
|
+
|
|
143
155
|
@Test
|
|
144
156
|
fun `a session change carries the active preset to the new effect`() {
|
|
145
157
|
val fixture = Fixture()
|