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.
Files changed (90) hide show
  1. package/android/build.gradle +15 -0
  2. package/android/src/cast/java/com/audiobrowser/cast/CastSupport.kt +215 -0
  3. package/android/src/main/AndroidManifest.xml +11 -0
  4. package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +0 -4
  5. package/android/src/main/java/com/audiobrowser/RemoteOutputs.kt +47 -10
  6. package/android/src/main/java/com/audiobrowser/model/PlayerSetupOptions.kt +12 -3
  7. package/android/src/main/java/com/audiobrowser/player/EqualizerController.kt +4 -1
  8. package/android/src/main/java/com/audiobrowser/player/LocalEngine.kt +49 -0
  9. package/android/src/main/java/com/audiobrowser/player/Player.kt +170 -119
  10. package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +18 -13
  11. package/android/src/main/java/com/audiobrowser/player/TransformingDataSource.kt +10 -13
  12. package/android/src/main/java/com/audiobrowser/remote/RemoteItem.kt +64 -0
  13. package/android/src/main/java/com/audiobrowser/remote/RemoteOutputsController.kt +98 -0
  14. package/android/src/main/java/com/audiobrowser/remote/RemotePlayback.kt +42 -0
  15. package/android/src/main/java/com/audiobrowser/remote/RemoteQueue.kt +110 -0
  16. package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +696 -0
  17. package/android/src/main/java/com/audiobrowser/sonos/DeviceDescriptionParser.kt +113 -0
  18. package/android/src/main/java/com/audiobrowser/sonos/DidlLite.kt +39 -0
  19. package/android/src/main/java/com/audiobrowser/sonos/SoapClient.kt +59 -0
  20. package/android/src/main/java/com/audiobrowser/sonos/SoapEnvelopes.kt +92 -0
  21. package/android/src/main/java/com/audiobrowser/sonos/SoapResponseParser.kt +105 -0
  22. package/android/src/main/java/com/audiobrowser/sonos/SonosDiscoverer.kt +69 -0
  23. package/android/src/main/java/com/audiobrowser/sonos/SonosOutputs.kt +128 -0
  24. package/android/src/main/java/com/audiobrowser/sonos/SonosPlayer.kt +550 -0
  25. package/android/src/main/java/com/audiobrowser/sonos/SonosRemote.kt +99 -0
  26. package/android/src/main/java/com/audiobrowser/sonos/SonosRouteProviderService.kt +201 -0
  27. package/android/src/main/java/com/audiobrowser/sonos/SonosStreamUri.kt +15 -0
  28. package/android/src/main/java/com/audiobrowser/sonos/SonosTransport.kt +66 -0
  29. package/android/src/main/java/com/audiobrowser/sonos/SpeakerXml.kt +26 -0
  30. package/android/src/main/java/com/audiobrowser/sonos/SsdpDiscovery.kt +63 -0
  31. package/android/src/main/java/com/audiobrowser/sonos/SsdpMessages.kt +63 -0
  32. package/android/src/main/java/com/audiobrowser/sonos/ZoneGroupState.kt +68 -0
  33. package/android/src/main/java/com/audiobrowser/util/OutputMonitor.kt +2 -1
  34. package/android/src/nocast/java/com/audiobrowser/cast/CastSupport.kt +18 -0
  35. package/android/src/test/java/com/audiobrowser/RemoteOutputsTest.kt +20 -3
  36. package/android/src/test/java/com/audiobrowser/model/PlayerSetupOptionsTest.kt +35 -0
  37. package/android/src/test/java/com/audiobrowser/player/EqualizerControllerTest.kt +12 -0
  38. package/android/src/test/java/com/audiobrowser/player/PlayerCastTest.kt +136 -0
  39. package/android/src/test/java/com/audiobrowser/player/PlayerSonosTest.kt +213 -0
  40. package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +275 -0
  41. package/android/src/test/java/com/audiobrowser/remote/RemoteItemTest.kt +61 -0
  42. package/android/src/test/java/com/audiobrowser/remote/RemoteOutputsControllerTest.kt +77 -0
  43. package/android/src/test/java/com/audiobrowser/remote/RemoteQueueTest.kt +101 -0
  44. package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +815 -0
  45. package/android/src/test/java/com/audiobrowser/sonos/DeviceDescriptionParserTest.kt +203 -0
  46. package/android/src/test/java/com/audiobrowser/sonos/DidlLiteTest.kt +90 -0
  47. package/android/src/test/java/com/audiobrowser/sonos/SoapClientTest.kt +73 -0
  48. package/android/src/test/java/com/audiobrowser/sonos/SoapEnvelopesTest.kt +135 -0
  49. package/android/src/test/java/com/audiobrowser/sonos/SoapResponseParserTest.kt +170 -0
  50. package/android/src/test/java/com/audiobrowser/sonos/SonosDiscovererTest.kt +174 -0
  51. package/android/src/test/java/com/audiobrowser/sonos/SonosPlayerTest.kt +691 -0
  52. package/android/src/test/java/com/audiobrowser/sonos/SonosRemoteTest.kt +129 -0
  53. package/android/src/test/java/com/audiobrowser/sonos/SonosRoutesTest.kt +165 -0
  54. package/android/src/test/java/com/audiobrowser/sonos/SonosStreamUriTest.kt +64 -0
  55. package/android/src/test/java/com/audiobrowser/sonos/SonosTransportTest.kt +204 -0
  56. package/android/src/test/java/com/audiobrowser/sonos/SsdpMessagesTest.kt +99 -0
  57. package/android/src/test/java/com/audiobrowser/sonos/ZoneGroupStateTest.kt +139 -0
  58. package/android/src/test/java/com/audiobrowser/util/OutputMonitorTest.kt +12 -10
  59. package/android/src/test/resources/sonos/device_description.xml +78 -0
  60. package/android/src/testCast/java/com/audiobrowser/cast/CastEndsTest.kt +56 -0
  61. package/android/src/testCast/java/com/audiobrowser/cast/SendingMediaItemConverterTest.kt +79 -0
  62. package/lib/commonjs/features/player/setup.js +2 -0
  63. package/lib/commonjs/features/player/setup.js.map +1 -1
  64. package/lib/module/features/player/setup.js +2 -0
  65. package/lib/module/features/player/setup.js.map +1 -1
  66. package/lib/typescript/src/features/player/setup.d.ts +37 -7
  67. package/lib/typescript/src/features/player/setup.d.ts.map +1 -1
  68. package/lib/typescript/src/specs/audio-browser.nitro.d.ts +3 -3
  69. package/lib/typescript/src/specs/audio-browser.nitro.d.ts.map +1 -1
  70. package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.cpp +4 -0
  71. package/nitrogen/generated/android/c++/JNativeSetupPlayerOptions.hpp +7 -5
  72. package/nitrogen/generated/android/c++/JOutputType.hpp +3 -0
  73. package/nitrogen/generated/android/c++/JRemoteOutputsOptions.hpp +66 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/NativeSetupPlayerOptions.kt +3 -3
  75. package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/OutputType.kt +3 -2
  76. package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/RemoteOutputsOptions.kt +61 -0
  77. package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Bridge.hpp +18 -0
  78. package/nitrogen/generated/ios/AudioBrowser-Swift-Cxx-Umbrella.hpp +3 -0
  79. package/nitrogen/generated/ios/c++/HybridAudioBrowserSpecSwift.hpp +3 -0
  80. package/nitrogen/generated/ios/swift/NativeSetupPlayerOptions.swift +5 -12
  81. package/nitrogen/generated/ios/swift/OutputType.swift +4 -0
  82. package/nitrogen/generated/ios/swift/RemoteOutputsOptions.swift +78 -0
  83. package/nitrogen/generated/shared/c++/NativeSetupPlayerOptions.hpp +8 -5
  84. package/nitrogen/generated/shared/c++/OutputType.hpp +6 -2
  85. package/nitrogen/generated/shared/c++/RemoteOutputsOptions.hpp +92 -0
  86. package/package.json +1 -1
  87. package/src/features/player/setup.ts +38 -7
  88. package/src/specs/audio-browser.nitro.ts +4 -3
  89. package/website/guide/audio-output.md +185 -4
  90. package/website/guide/configuration.md +6 -0
@@ -0,0 +1,203 @@
1
+ package com.audiobrowser.sonos
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertNull
5
+ import org.junit.Test
6
+
7
+ /** Parsing a Sonos `device_description.xml` into a [SonosDevice] with absolute control URLs. */
8
+ class DeviceDescriptionParserTest {
9
+
10
+ private fun fixture(name: String): String = javaClass.getResource("/sonos/$name")!!.readText()
11
+
12
+ private val location = "http://192.168.1.50:1400/xml/device_description.xml"
13
+
14
+ @Test
15
+ fun `parses udn, name and resolves nested control URLs against the base`() {
16
+ val device = DeviceDescriptionParser.parse(fixture("device_description.xml"), location)!!
17
+ assertEquals("uuid:RINCON_949F3EC213E001400", device.udn)
18
+ // AVTransport + GroupRenderingControl live on the embedded MediaRenderer, not the root device.
19
+ assertEquals(
20
+ "http://192.168.1.50:1400/MediaRenderer/AVTransport/Control",
21
+ device.avTransportControlUrl,
22
+ )
23
+ assertEquals(
24
+ "http://192.168.1.50:1400/MediaRenderer/GroupRenderingControl/Control",
25
+ device.groupRenderingControlUrl,
26
+ )
27
+ assertEquals("http://192.168.1.50:1400", device.baseUrl)
28
+ }
29
+
30
+ @Test
31
+ fun `reads the root device's ZoneGroupTopology control URL`() {
32
+ val device = DeviceDescriptionParser.parse(fixture("device_description.xml"), location)!!
33
+ assertEquals(
34
+ "http://192.168.1.50:1400/ZoneGroupTopology/Control",
35
+ device.zoneGroupTopologyControlUrl,
36
+ )
37
+ }
38
+
39
+ @Test
40
+ fun `leaves the ZoneGroupTopology control URL null when the description has none`() {
41
+ val xml =
42
+ """
43
+ <root xmlns="urn:schemas-upnp-org:device-1-0"><device>
44
+ <manufacturer>Sonos, Inc.</manufacturer>
45
+ <UDN>uuid:RINCON_NOZGT</UDN>
46
+ <serviceList>
47
+ <service>
48
+ <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
49
+ <controlURL>/AVT/Control</controlURL>
50
+ </service>
51
+ <service>
52
+ <serviceType>urn:schemas-upnp-org:service:GroupRenderingControl:1</serviceType>
53
+ <controlURL>/RC/Control</controlURL>
54
+ </service>
55
+ </serviceList>
56
+ </device></root>
57
+ """
58
+ .trimIndent()
59
+ assertNull(DeviceDescriptionParser.parse(xml, location)!!.zoneGroupTopologyControlUrl)
60
+ }
61
+
62
+ @Test
63
+ fun `prefers the Sonos roomName as the friendly name`() {
64
+ val device = DeviceDescriptionParser.parse(fixture("device_description.xml"), location)!!
65
+ assertEquals("Kitchen", device.name)
66
+ }
67
+
68
+ @Test
69
+ fun `resolves an absolute controlURL as-is`() {
70
+ val xml =
71
+ """
72
+ <root xmlns="urn:schemas-upnp-org:device-1-0"><device>
73
+ <deviceType>urn:schemas-upnp-org:device:ZonePlayer:1</deviceType>
74
+ <friendlyName>Office</friendlyName>
75
+ <manufacturer>Sonos, Inc.</manufacturer>
76
+ <UDN>uuid:RINCON_ABS</UDN>
77
+ <serviceList>
78
+ <service>
79
+ <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
80
+ <controlURL>http://10.0.0.5:1400/AVT/Control</controlURL>
81
+ </service>
82
+ <service>
83
+ <serviceType>urn:schemas-upnp-org:service:GroupRenderingControl:1</serviceType>
84
+ <controlURL>/RC/Control</controlURL>
85
+ </service>
86
+ </serviceList>
87
+ </device></root>
88
+ """
89
+ .trimIndent()
90
+ val device = DeviceDescriptionParser.parse(xml, "http://10.0.0.5:1400/desc.xml")!!
91
+ assertEquals("http://10.0.0.5:1400/AVT/Control", device.avTransportControlUrl)
92
+ assertEquals("http://10.0.0.5:1400/RC/Control", device.groupRenderingControlUrl)
93
+ }
94
+
95
+ @Test
96
+ fun `honours an explicit URLBase over the location for relative control URLs`() {
97
+ val xml =
98
+ """
99
+ <root xmlns="urn:schemas-upnp-org:device-1-0">
100
+ <URLBase>http://10.0.0.99:1400/</URLBase>
101
+ <device>
102
+ <deviceType>urn:schemas-upnp-org:device:ZonePlayer:1</deviceType>
103
+ <friendlyName>Patio</friendlyName>
104
+ <manufacturer>Sonos, Inc.</manufacturer>
105
+ <UDN>uuid:RINCON_BASE</UDN>
106
+ <serviceList>
107
+ <service>
108
+ <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
109
+ <controlURL>/AVT/Control</controlURL>
110
+ </service>
111
+ <service>
112
+ <serviceType>urn:schemas-upnp-org:service:GroupRenderingControl:1</serviceType>
113
+ <controlURL>/RC/Control</controlURL>
114
+ </service>
115
+ </serviceList>
116
+ </device>
117
+ </root>
118
+ """
119
+ .trimIndent()
120
+ // Description was retrieved from .50 but URLBase points control at .99.
121
+ val device = DeviceDescriptionParser.parse(xml, location)!!
122
+ assertEquals("http://10.0.0.99:1400/AVT/Control", device.avTransportControlUrl)
123
+ assertEquals("http://10.0.0.99:1400/RC/Control", device.groupRenderingControlUrl)
124
+ }
125
+
126
+ @Test
127
+ fun `resolves control URLs against an IPv6 location`() {
128
+ val xml =
129
+ """
130
+ <root xmlns="urn:schemas-upnp-org:device-1-0"><device>
131
+ <deviceType>urn:schemas-upnp-org:device:ZonePlayer:1</deviceType>
132
+ <friendlyName>Loft</friendlyName>
133
+ <manufacturer>Sonos, Inc.</manufacturer>
134
+ <UDN>uuid:RINCON_V6</UDN>
135
+ <serviceList>
136
+ <service>
137
+ <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
138
+ <controlURL>/AVT/Control</controlURL>
139
+ </service>
140
+ <service>
141
+ <serviceType>urn:schemas-upnp-org:service:GroupRenderingControl:1</serviceType>
142
+ <controlURL>/RC/Control</controlURL>
143
+ </service>
144
+ </serviceList>
145
+ </device></root>
146
+ """
147
+ .trimIndent()
148
+ val device =
149
+ DeviceDescriptionParser.parse(xml, "http://[fe80::1]:1400/xml/device_description.xml")!!
150
+ assertEquals("http://[fe80::1]:1400/AVT/Control", device.avTransportControlUrl)
151
+ }
152
+
153
+ @Test
154
+ fun `rejects a non-Sonos device`() {
155
+ val xml =
156
+ """
157
+ <root xmlns="urn:schemas-upnp-org:device-1-0"><device>
158
+ <deviceType>urn:schemas-upnp-org:device:MediaRenderer:1</deviceType>
159
+ <friendlyName>Some DLNA TV</friendlyName>
160
+ <manufacturer>Acme Corp</manufacturer>
161
+ <UDN>uuid:NOT_SONOS</UDN>
162
+ <serviceList>
163
+ <service>
164
+ <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
165
+ <controlURL>/AVT/Control</controlURL>
166
+ </service>
167
+ <service>
168
+ <serviceType>urn:schemas-upnp-org:service:GroupRenderingControl:1</serviceType>
169
+ <controlURL>/RC/Control</controlURL>
170
+ </service>
171
+ </serviceList>
172
+ </device></root>
173
+ """
174
+ .trimIndent()
175
+ assertNull(DeviceDescriptionParser.parse(xml, "http://10.0.0.6:1400/desc.xml"))
176
+ }
177
+
178
+ @Test
179
+ fun `returns null when AVTransport service is absent`() {
180
+ val xml =
181
+ """
182
+ <root xmlns="urn:schemas-upnp-org:device-1-0"><device>
183
+ <deviceType>urn:schemas-upnp-org:device:ZonePlayer:1</deviceType>
184
+ <friendlyName>Bedroom</friendlyName>
185
+ <manufacturer>Sonos, Inc.</manufacturer>
186
+ <UDN>uuid:RINCON_NOAVT</UDN>
187
+ <serviceList>
188
+ <service>
189
+ <serviceType>urn:schemas-upnp-org:service:GroupRenderingControl:1</serviceType>
190
+ <controlURL>/RC/Control</controlURL>
191
+ </service>
192
+ </serviceList>
193
+ </device></root>
194
+ """
195
+ .trimIndent()
196
+ assertNull(DeviceDescriptionParser.parse(xml, "http://10.0.0.7:1400/desc.xml"))
197
+ }
198
+
199
+ @Test
200
+ fun `returns null on malformed XML`() {
201
+ assertNull(DeviceDescriptionParser.parse("<root><device", location))
202
+ }
203
+ }
@@ -0,0 +1,90 @@
1
+ package com.audiobrowser.sonos
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Test
5
+
6
+ /** Deterministic DIDL-Lite metadata for `SetAVTransportURI`. */
7
+ class DidlLiteTest {
8
+
9
+ private val header =
10
+ "<DIDL-Lite xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" " +
11
+ "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " +
12
+ "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\">"
13
+
14
+ @Test
15
+ fun `builds a minimal live audioBroadcast item`() {
16
+ val didl = DidlLite.build(title = "Radio Swiss Jazz", live = true)
17
+ assertEquals(
18
+ header +
19
+ "<item id=\"R:0/0/0\" parentID=\"R:0/0\" restricted=\"true\">" +
20
+ "<dc:title>Radio Swiss Jazz</dc:title>" +
21
+ "<upnp:class>object.item.audioItem.audioBroadcast</upnp:class>" +
22
+ "</item></DIDL-Lite>",
23
+ didl,
24
+ )
25
+ }
26
+
27
+ @Test
28
+ fun `includes creator, album and artwork in a fixed order when present`() {
29
+ val didl =
30
+ DidlLite.build(
31
+ title = "Now Playing",
32
+ creator = "The Artist",
33
+ album = "The Album",
34
+ albumArtUri = "http://art/cover.jpg",
35
+ live = true,
36
+ )
37
+ assertEquals(
38
+ header +
39
+ "<item id=\"R:0/0/0\" parentID=\"R:0/0\" restricted=\"true\">" +
40
+ "<dc:title>Now Playing</dc:title>" +
41
+ "<upnp:class>object.item.audioItem.audioBroadcast</upnp:class>" +
42
+ "<dc:creator>The Artist</dc:creator>" +
43
+ "<upnp:album>The Album</upnp:album>" +
44
+ "<upnp:albumArtURI>http://art/cover.jpg</upnp:albumArtURI>" +
45
+ "</item></DIDL-Lite>",
46
+ didl,
47
+ )
48
+ }
49
+
50
+ @Test
51
+ fun `uses the music-track class when not live`() {
52
+ val didl = DidlLite.build(title = "A Song", live = false)
53
+ assertEquals(
54
+ header +
55
+ "<item id=\"R:0/0/0\" parentID=\"R:0/0\" restricted=\"true\">" +
56
+ "<dc:title>A Song</dc:title>" +
57
+ "<upnp:class>object.item.audioItem.musicTrack</upnp:class>" +
58
+ "</item></DIDL-Lite>",
59
+ didl,
60
+ )
61
+ }
62
+
63
+ @Test
64
+ fun `escapes XML special characters in text fields`() {
65
+ val didl = DidlLite.build(title = "Rock & Roll <\"Live\">", creator = "A & B", live = true)
66
+ assertEquals(
67
+ header +
68
+ "<item id=\"R:0/0/0\" parentID=\"R:0/0\" restricted=\"true\">" +
69
+ "<dc:title>Rock &amp; Roll &lt;&quot;Live&quot;&gt;</dc:title>" +
70
+ "<upnp:class>object.item.audioItem.audioBroadcast</upnp:class>" +
71
+ "<dc:creator>A &amp; B</dc:creator>" +
72
+ "</item></DIDL-Lite>",
73
+ didl,
74
+ )
75
+ }
76
+
77
+ @Test
78
+ fun `does not emit empty optional fields`() {
79
+ val didl =
80
+ DidlLite.build(title = "T", creator = "", album = null, albumArtUri = " ", live = true)
81
+ assertEquals(
82
+ header +
83
+ "<item id=\"R:0/0/0\" parentID=\"R:0/0\" restricted=\"true\">" +
84
+ "<dc:title>T</dc:title>" +
85
+ "<upnp:class>object.item.audioItem.audioBroadcast</upnp:class>" +
86
+ "</item></DIDL-Lite>",
87
+ didl,
88
+ )
89
+ }
90
+ }
@@ -0,0 +1,73 @@
1
+ package com.audiobrowser.sonos
2
+
3
+ import okhttp3.OkHttpClient
4
+ import okhttp3.mockwebserver.MockResponse
5
+ import okhttp3.mockwebserver.MockWebServer
6
+ import org.junit.After
7
+ import org.junit.Assert.assertEquals
8
+ import org.junit.Assert.assertThrows
9
+ import org.junit.Assert.assertTrue
10
+ import org.junit.Before
11
+ import org.junit.Test
12
+
13
+ /**
14
+ * Real HTTP round-trip for [SoapClient] using a local MockWebServer in place of a Sonos speaker.
15
+ */
16
+ class SoapClientTest {
17
+
18
+ private lateinit var server: MockWebServer
19
+ private val client = SoapClient(OkHttpClient())
20
+
21
+ @Before
22
+ fun setUp() {
23
+ server = MockWebServer()
24
+ server.start()
25
+ }
26
+
27
+ @After
28
+ fun tearDown() {
29
+ server.shutdown()
30
+ }
31
+
32
+ @Test
33
+ fun `posts the SOAP body with a quoted SOAPACTION and returns the response body`() {
34
+ val responseBody =
35
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
36
+ "<s:Body><u:GetTransportInfoResponse><CurrentTransportState>PLAYING" +
37
+ "</CurrentTransportState></u:GetTransportInfoResponse></s:Body></s:Envelope>"
38
+ server.enqueue(MockResponse().setResponseCode(200).setBody(responseBody))
39
+
40
+ val controlUrl = server.url("/MediaRenderer/AVTransport/Control").toString()
41
+ val action = SoapEnvelopes.getTransportInfo()
42
+ val result = client.execute(controlUrl, action)
43
+
44
+ assertEquals(responseBody, result)
45
+ val recorded = server.takeRequest()
46
+ assertEquals("POST", recorded.method)
47
+ assertEquals("/MediaRenderer/AVTransport/Control", recorded.path)
48
+ assertEquals(
49
+ "\"urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo\"",
50
+ recorded.getHeader("SOAPACTION"),
51
+ )
52
+ assertTrue(recorded.getHeader("Content-Type")!!.startsWith("text/xml"))
53
+ assertEquals(action.body, recorded.body.readUtf8())
54
+ }
55
+
56
+ @Test
57
+ fun `throws SoapException carrying the parsed fault on HTTP 500`() {
58
+ val faultBody =
59
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
60
+ "<s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring>" +
61
+ "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">" +
62
+ "<errorCode>718</errorCode><errorDescription>No such object</errorDescription>" +
63
+ "</UPnPError></detail></s:Fault></s:Body></s:Envelope>"
64
+ server.enqueue(MockResponse().setResponseCode(500).setBody(faultBody))
65
+
66
+ val controlUrl = server.url("/ctrl").toString()
67
+ val e =
68
+ assertThrows(SoapException::class.java) { client.execute(controlUrl, SoapEnvelopes.play()) }
69
+ assertEquals(500, e.httpCode)
70
+ assertEquals(718, e.fault?.errorCode)
71
+ assertEquals("No such object", e.fault?.errorDescription)
72
+ }
73
+ }
@@ -0,0 +1,135 @@
1
+ package com.audiobrowser.sonos
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Test
5
+
6
+ /** Byte-exact SOAP request bodies + SOAPAction headers for the UPnP actions we issue. */
7
+ class SoapEnvelopesTest {
8
+
9
+ private val avt = "urn:schemas-upnp-org:service:AVTransport:1"
10
+ private val grc = "urn:schemas-upnp-org:service:GroupRenderingControl:1"
11
+
12
+ private fun envelope(service: String, action: String, args: String): String =
13
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
14
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
15
+ "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
16
+ "<s:Body>" +
17
+ "<u:$action xmlns:u=\"$service\">$args</u:$action>" +
18
+ "</s:Body></s:Envelope>"
19
+
20
+ @Test
21
+ fun `play`() {
22
+ val a = SoapEnvelopes.play()
23
+ assertEquals("$avt#Play", a.soapAction)
24
+ assertEquals(envelope(avt, "Play", "<InstanceID>0</InstanceID><Speed>1</Speed>"), a.body)
25
+ }
26
+
27
+ @Test
28
+ fun `pause`() {
29
+ val a = SoapEnvelopes.pause()
30
+ assertEquals("$avt#Pause", a.soapAction)
31
+ assertEquals(envelope(avt, "Pause", "<InstanceID>0</InstanceID>"), a.body)
32
+ }
33
+
34
+ @Test
35
+ fun `stop`() {
36
+ val a = SoapEnvelopes.stop()
37
+ assertEquals("$avt#Stop", a.soapAction)
38
+ assertEquals(envelope(avt, "Stop", "<InstanceID>0</InstanceID>"), a.body)
39
+ }
40
+
41
+ @Test
42
+ fun `getTransportInfo`() {
43
+ val a = SoapEnvelopes.getTransportInfo()
44
+ assertEquals("$avt#GetTransportInfo", a.soapAction)
45
+ assertEquals(envelope(avt, "GetTransportInfo", "<InstanceID>0</InstanceID>"), a.body)
46
+ }
47
+
48
+ @Test
49
+ fun `getMediaInfo`() {
50
+ val a = SoapEnvelopes.getMediaInfo()
51
+ assertEquals("$avt#GetMediaInfo", a.soapAction)
52
+ assertEquals(envelope(avt, "GetMediaInfo", "<InstanceID>0</InstanceID>"), a.body)
53
+ }
54
+
55
+ @Test
56
+ fun `getPositionInfo`() {
57
+ val a = SoapEnvelopes.getPositionInfo()
58
+ assertEquals("$avt#GetPositionInfo", a.soapAction)
59
+ assertEquals(envelope(avt, "GetPositionInfo", "<InstanceID>0</InstanceID>"), a.body)
60
+ }
61
+
62
+ @Test
63
+ fun `getZoneGroupState`() {
64
+ val zgt = "urn:schemas-upnp-org:service:ZoneGroupTopology:1"
65
+ val a = SoapEnvelopes.getZoneGroupState()
66
+ assertEquals("$zgt#GetZoneGroupState", a.soapAction)
67
+ assertEquals(envelope(zgt, "GetZoneGroupState", ""), a.body)
68
+ }
69
+
70
+ @Test
71
+ fun `setAvTransportUri escapes the url and double-escapes the didl metadata`() {
72
+ val a = SoapEnvelopes.setAvTransportUri("http://h/s?a=1&b=2", "<DIDL-Lite>x</DIDL-Lite>")
73
+ assertEquals("$avt#SetAVTransportURI", a.soapAction)
74
+ assertEquals(
75
+ envelope(
76
+ avt,
77
+ "SetAVTransportURI",
78
+ "<InstanceID>0</InstanceID>" +
79
+ "<CurrentURI>http://h/s?a=1&amp;b=2</CurrentURI>" +
80
+ "<CurrentURIMetaData>&lt;DIDL-Lite&gt;x&lt;/DIDL-Lite&gt;</CurrentURIMetaData>",
81
+ ),
82
+ a.body,
83
+ )
84
+ }
85
+
86
+ @Test
87
+ fun `xmlEscape leaves out the characters XML has no place for`() {
88
+ assertEquals("Live &amp; Loud", xmlEscape("Live\u0001 &\u000B Loud\uFFFF"))
89
+ assertEquals("a\tb\nc", xmlEscape("a\tb\nc"))
90
+ }
91
+
92
+ @Test
93
+ fun `setGroupVolume clamps to 0 through 100`() {
94
+ assertEquals(
95
+ envelope(
96
+ grc,
97
+ "SetGroupVolume",
98
+ "<InstanceID>0</InstanceID><DesiredVolume>50</DesiredVolume>",
99
+ ),
100
+ SoapEnvelopes.setGroupVolume(50).body,
101
+ )
102
+ assertEquals("$grc#SetGroupVolume", SoapEnvelopes.setGroupVolume(50).soapAction)
103
+ assertEquals(
104
+ "<DesiredVolume>100</DesiredVolume>",
105
+ Regex("<DesiredVolume>.*?</DesiredVolume>")
106
+ .find(SoapEnvelopes.setGroupVolume(150).body)!!
107
+ .value,
108
+ )
109
+ assertEquals(
110
+ "<DesiredVolume>0</DesiredVolume>",
111
+ Regex("<DesiredVolume>.*?</DesiredVolume>")
112
+ .find(SoapEnvelopes.setGroupVolume(-5).body)!!
113
+ .value,
114
+ )
115
+ }
116
+
117
+ @Test
118
+ fun `getGroupVolume`() {
119
+ val a = SoapEnvelopes.getGroupVolume()
120
+ assertEquals("$grc#GetGroupVolume", a.soapAction)
121
+ assertEquals(envelope(grc, "GetGroupVolume", "<InstanceID>0</InstanceID>"), a.body)
122
+ }
123
+
124
+ @Test
125
+ fun `seek goes to a relative time, to the second`() {
126
+ assertEquals(
127
+ envelope(
128
+ "urn:schemas-upnp-org:service:AVTransport:1",
129
+ "Seek",
130
+ "<InstanceID>0</InstanceID><Unit>REL_TIME</Unit><Target>1:01:01</Target>",
131
+ ),
132
+ SoapEnvelopes.seek(3_661_500).body,
133
+ )
134
+ }
135
+ }
@@ -0,0 +1,170 @@
1
+ package com.audiobrowser.sonos
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertNull
5
+ import org.junit.Test
6
+
7
+ /** Parsing UPnP SOAP responses: transport state, volume, and faults. */
8
+ class SoapResponseParserTest {
9
+
10
+ private fun transportInfo(state: String): String =
11
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
12
+ "<s:Body><u:GetTransportInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">" +
13
+ "<CurrentTransportState>$state</CurrentTransportState>" +
14
+ "<CurrentTransportStatus>OK</CurrentTransportStatus>" +
15
+ "<CurrentSpeed>1</CurrentSpeed>" +
16
+ "</u:GetTransportInfoResponse></s:Body></s:Envelope>"
17
+
18
+ private val fault =
19
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
20
+ "<s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring>" +
21
+ "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">" +
22
+ "<errorCode>701</errorCode><errorDescription>Transition not available</errorDescription>" +
23
+ "</UPnPError></detail></s:Fault></s:Body></s:Envelope>"
24
+
25
+ @Test
26
+ fun `a response with a document type declaration is refused`() {
27
+ val withEntity =
28
+ transportInfo("&state;")
29
+ .replace(
30
+ "<?xml version=\"1.0\"?>",
31
+ "<?xml version=\"1.0\"?><!DOCTYPE s [<!ENTITY state \"PLAYING\">]>",
32
+ )
33
+
34
+ assertNull(SoapResponseParser.transportState(withEntity))
35
+ }
36
+
37
+ @Test
38
+ fun `transportState reads CurrentTransportState`() {
39
+ assertEquals("PLAYING", SoapResponseParser.transportState(transportInfo("PLAYING")))
40
+ assertEquals(
41
+ "PAUSED_PLAYBACK",
42
+ SoapResponseParser.transportState(transportInfo("PAUSED_PLAYBACK")),
43
+ )
44
+ assertEquals("STOPPED", SoapResponseParser.transportState(transportInfo("STOPPED")))
45
+ }
46
+
47
+ @Test
48
+ fun `volume reads CurrentVolume as an int`() {
49
+ val xml =
50
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
51
+ "<s:Body><u:GetVolumeResponse xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\">" +
52
+ "<CurrentVolume>42</CurrentVolume></u:GetVolumeResponse></s:Body></s:Envelope>"
53
+ assertEquals(42, SoapResponseParser.volume(xml))
54
+ }
55
+
56
+ @Test
57
+ fun `fault extracts the UPnP error code and description`() {
58
+ val f = SoapResponseParser.fault(fault)!!
59
+ assertEquals(701, f.errorCode)
60
+ assertEquals("Transition not available", f.errorDescription)
61
+ }
62
+
63
+ @Test
64
+ fun `fault returns null for a normal response`() {
65
+ assertNull(SoapResponseParser.fault(transportInfo("PLAYING")))
66
+ }
67
+
68
+ @Test
69
+ fun `volume returns null on a soft fault response`() {
70
+ assertNull(SoapResponseParser.volume(fault))
71
+ }
72
+
73
+ @Test
74
+ fun `transportState returns null on a fault or garbage`() {
75
+ assertNull(SoapResponseParser.transportState(fault))
76
+ assertNull(SoapResponseParser.transportState("not xml at all"))
77
+ }
78
+
79
+ private fun positionInfo(trackMetaData: String): String =
80
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
81
+ "<s:Body><u:GetPositionInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">" +
82
+ "<Track>1</Track><TrackDuration>0:00:00</TrackDuration>" +
83
+ "<TrackMetaData>${escape(trackMetaData)}</TrackMetaData>" +
84
+ "<TrackURI>x-rincon-mp3radio://https://h/stream</TrackURI>" +
85
+ "</u:GetPositionInfoResponse></s:Body></s:Envelope>"
86
+
87
+ private fun escape(s: String) =
88
+ s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;")
89
+
90
+ private fun trackDidl(streamContent: String) =
91
+ "<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " +
92
+ "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" " +
93
+ "xmlns:r=\"urn:schemas-rinconnetworks-com:metadata-1-0/\" " +
94
+ "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\">" +
95
+ "<item id=\"-1\" parentID=\"-1\" restricted=\"true\">" +
96
+ "<res protocolInfo=\"sonos.com-http:*:audio/mpeg:*\">x-rincon-mp3radio://https://h/stream</res>" +
97
+ "<r:streamContent>$streamContent</r:streamContent><r:radioShowMd></r:radioShowMd>" +
98
+ "<upnp:class>object.item</upnp:class></item></DIDL-Lite>"
99
+
100
+ @Test
101
+ fun `streamContent reads the title the speaker took from the stream`() {
102
+ assertEquals(
103
+ "Santana - Smooth [3OSN]",
104
+ SoapResponseParser.streamContent(positionInfo(trackDidl("Santana - Smooth [3OSN]"))),
105
+ )
106
+ }
107
+
108
+ @Test
109
+ fun `streamContent unescapes the title once more than the envelope`() {
110
+ assertEquals(
111
+ "Simon & Garfunkel - Cecilia",
112
+ SoapResponseParser.streamContent(positionInfo(trackDidl("Simon &amp; Garfunkel - Cecilia"))),
113
+ )
114
+ }
115
+
116
+ @Test
117
+ fun `streamContent is null when the stream sends no title`() {
118
+ assertNull(SoapResponseParser.streamContent(positionInfo(trackDidl(""))))
119
+ }
120
+
121
+ @Test
122
+ fun `streamContent is null when the speaker has no track metadata`() {
123
+ assertNull(SoapResponseParser.streamContent(positionInfo("NOT_IMPLEMENTED")))
124
+ assertNull(SoapResponseParser.streamContent(positionInfo("")))
125
+ assertNull(SoapResponseParser.streamContent(fault))
126
+ }
127
+
128
+ private fun timing(duration: String, relTime: String): String =
129
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
130
+ "<s:Body><u:GetPositionInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">" +
131
+ "<Track>1</Track><TrackDuration>$duration</TrackDuration><TrackMetaData></TrackMetaData>" +
132
+ "<RelTime>$relTime</RelTime></u:GetPositionInfoResponse></s:Body></s:Envelope>"
133
+
134
+ @Test
135
+ fun `position reads a track's position and duration`() {
136
+ assertEquals(
137
+ SonosPosition(streamContent = null, positionMs = 3_723_000, durationMs = 201_500),
138
+ SoapResponseParser.position(timing("0:03:21.500", "1:02:03")),
139
+ )
140
+ }
141
+
142
+ @Test
143
+ fun `position has no duration for a live stream, and no time the speaker doesn't keep`() {
144
+ assertEquals(
145
+ SonosPosition(streamContent = null, positionMs = null, durationMs = null),
146
+ SoapResponseParser.position(timing("0:00:00", "NOT_IMPLEMENTED")),
147
+ )
148
+ }
149
+
150
+ @Test
151
+ fun `position carries the stream title, and is null for a fault`() {
152
+ val position = SoapResponseParser.position(positionInfo(trackDidl("Santana - Smooth")))
153
+ assertEquals("Santana - Smooth", position?.streamContent)
154
+ assertNull(SoapResponseParser.position(fault))
155
+ }
156
+
157
+ @Test
158
+ fun `currentUri reads GetMediaInfo's CurrentURI, and is null for a fault`() {
159
+ val mediaInfo =
160
+ "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
161
+ "<s:Body><u:GetMediaInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">" +
162
+ "<NrTracks>1</NrTracks><CurrentURI>x-rincon-mp3radio://https://h/listen?a=1&amp;b=2</CurrentURI>" +
163
+ "</u:GetMediaInfoResponse></s:Body></s:Envelope>"
164
+ assertEquals(
165
+ "x-rincon-mp3radio://https://h/listen?a=1&b=2",
166
+ SoapResponseParser.currentUri(mediaInfo),
167
+ )
168
+ assertNull(SoapResponseParser.currentUri(fault))
169
+ }
170
+ }