forked from DjMomo/sonos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonos.class.php
executable file
·676 lines (578 loc) · 18.3 KB
/
sonos.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?php
class SonosPHPController
{
protected $Sonos_IP;
protected $_raw = [];
/**
* Constructeur
* @param string Sonos IP adress
* @param string Sonos port (optional)
*/
public function __construct($Sonos_IP,$Sonos_Port = '1400')
{
// On assigne les param�tres aux variables d'instance.
$this->IP = $Sonos_IP;
$this->PORT = $Sonos_Port;
}
protected function Upnp($url,$SOAP_service,$SOAP_action,$SOAP_arguments = '',$XML_filter = '')
{
$POST_xml = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
$POST_xml .= '<s:Body>';
$POST_xml .= '<u:'.$SOAP_action.' xmlns:u="'.$SOAP_service.'">';
$POST_xml .= $SOAP_arguments;
$POST_xml .= '</u:'.$SOAP_action.'>';
$POST_xml .= '</s:Body>';
$POST_xml .= '</s:Envelope>';
$POST_url = $this->IP.":".$this->PORT.$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $POST_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "SOAPAction: ".$SOAP_service."#".$SOAP_action));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST_xml);
$r = curl_exec($ch);
curl_close($ch);
if ($XML_filter != '')
return $this->Filter($r,$XML_filter);
else
return $r;
}
protected function Filter($subject,$pattern)
{
preg_match('/\<'.$pattern.'\>(.+)\<\/'.$pattern.'\>/',$subject,$matches); ///'/\<'.$pattern.'\>(.+)\<\/'.$pattern.'\>/'
return $matches[1];
}
/**
* Play
*/
public function Play()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'Play';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID><Speed>1</Speed>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Pause
*/
public function Pause()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'Pause';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Stop
*/
public function Stop()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'Stop';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Next
*/
public function Next()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'Next';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Previous
*/
public function Previous()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'Previous';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Seek to position xx:xx:xx or track number x
* @param string 'REL_TIME' for time position (xx:xx:xx) or 'TRACK_NR' for track in actual queue
* @param string
*/
public function Seek($type,$position)
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'Seek';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID><Unit>'.$type.'</Unit><Target>'.$position.'</Target>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Seek to time xx:xx:xx
*/
public function SeekTime($time)
{
return $this->Seek("REL_TIME",$time);
}
/**
* Change to track number
*/
public function ChangeTrack($number)
{
return $this->Seek("TRACK_NR",$number);
}
/**
* Restart actual track
*/
public function RestartTrack()
{
return $this->Seek("REL_TIME","00:00:00");
}
/**
* Restart actual queue
*/
public function RestartQueue()
{
return $this->Seek("TRACK_NR","1");
}
/**
* Get volume value (0-100)
*/
public function GetVolume()
{
$url = '/MediaRenderer/RenderingControl/Control';
$action = 'GetVolume';
$service = 'urn:schemas-upnp-org:service:RenderingControl:1';
$args = '<InstanceID>0</InstanceID><Channel>Master</Channel>';
$filter = 'CurrentVolume';
return $this->Upnp($url,$service,$action,$args,$filter);
}
/**
* Set volume value (0-100)
*/
public function SetVolume($volume)
{
$url = '/MediaRenderer/RenderingControl/Control';
$action = 'SetVolume';
$service = 'urn:schemas-upnp-org:service:RenderingControl:1';
$args = '<InstanceID>0</InstanceID><Channel>Master</Channel><DesiredVolume>'.$volume.'</DesiredVolume>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Get mute status
*/
public function GetMute()
{
$url = '/MediaRenderer/RenderingControl/Control';
$action = 'GetMute';
$service = 'urn:schemas-upnp-org:service:RenderingControl:1';
$args = '<InstanceID>0</InstanceID><Channel>Master</Channel>';
$filter = 'CurrentMute';
return $this->Upnp($url,$service,$action,$args,$filter);
}
/**
* Set mute
* @param integer mute active=1
*/
public function SetMute($mute = 0)
{
$url = '/MediaRenderer/RenderingControl/Control';
$action = 'SetMute';
$service = 'urn:schemas-upnp-org:service:RenderingControl:1';
$args = '<InstanceID>0</InstanceID><Channel>Master</Channel><DesiredMute>'.$mute.'</DesiredMute>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Get Transport Info : get status about player
*/
public function GetTransportInfo()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'GetTransportInfo';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
$filter = 'CurrentTransportState';
return $this->Upnp($url,$service,$action,$args,$filter);
}
/**
* Get Media Info : get informations about media
*/
public function GetMediaInfo()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'GetMediaInfo';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
$filter = 'CurrentURI';
return $this->Upnp($url,$service,$action,$args,$filter);
}
/**
* Get Position Info : get some informations about track
*/
public function GetPositionInfo()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'GetPositionInfo';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
$xml = $this->Upnp($url,$service,$action,$args);
$data["TrackNumberInQueue"] = $this->Filter($xml,"Track");
$data["TrackURI"] = $this->Filter($xml,"TrackURI");
$data["TrackDuration"] = $this->Filter($xml,"TrackDuration");
$data["RelTime"] = $this->Filter($xml,"RelTime");
$TrackMetaData = $this->Filter($xml,"TrackMetaData");
$xml = substr($xml, stripos($TrackMetaData, '<'));
$xml = substr($xml, 0, strrpos($xml, '>') + 4);
$xml = str_replace(array("<", ">", """, "&", "%3a", "%2f", "%25"), array("<", ">", "\"", "&", ":", "/", "%"), $xml);
$data["Title"] = $this->Filter($xml,"dc:title"); // Track Title
$data["AlbumArtist"] = $this->Filter($xml,"r:albumArtist"); // Album Artist
$data["Album"] = $this->Filter($xml,"upnp:album"); // Album Title
$data["TitleArtist"] = $this->Filter($xml,"dc:creator"); // Track Artist
return $data;
}
/**
* Add URI to Queue
* @param string track/radio URI
* @param bool added next (=1) or end queue (=0)
*/
public function AddURIToQueue($URI,$next=0)
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'AddURIToQueue';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$next = (int)$next;
$args = '<InstanceID>0</InstanceID><EnqueuedURI>'.$URI.'</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>'.$next.'</EnqueueAsNext>';
$filter = 'FirstTrackNumberEnqueued';
return $this->Upnp($url,$service,$action,$args,$filter);
}
/**
* Remove a track from Queue
*
*/
public function RemoveTrackFromQueue($tracknumber)
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'RemoveTrackFromQueue';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID><ObjectID>Q:0/'.$tracknumber.'</ObjectID>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Clear Queue
*
*/
public function RemoveAllTracksFromQueue()
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'RemoveAllTracksFromQueue';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Set Queue
* @param string URI of new track
*/
public function SetQueue($URI)
{
$url = '/MediaRenderer/AVTransport/Control';
$action = 'SetAVTransportURI';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$args = '<InstanceID>0</InstanceID><CurrentURI>'.$URI.'</CurrentURI><CurrentURIMetaData></CurrentURIMetaData>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Refresh music library
*
*/
public function RefreshShareIndex()
{
$url = '/MediaServer/ContentDirectory/Control';
$action = 'RefreshShareIndex';
$service = 'urn:schemas-upnp-org:service:ContentDirectory:1';
$args = '<AlbumArtistDisplayOption/>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Add music library (Samba share)
*
*/
public function AddMusicLibrary($libraryLocation)
{
$url = '/MediaServer/ContentDirectory/Control';
$action = 'CreateObject';
$service = 'urn:schemas-upnp-org:service:ContentDirectory:1';
$meta = '<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"><container id="" restricted="false"><dc:title>'.$libraryLocation.'</dc:title><r:usernameX/><r:passwordX/></container></DIDL-Lite>';
$meta = htmlentities($meta);
$args = '<ContainerID>S:</ContainerID><Elements>'.$meta.'</Elements>';
return $this->Upnp($url,$service,$action,$args);
}
/**
* Split string in several strings
*
*/
protected function CutString($string,$intmax)
{
$i = 0;
while (strlen($string) > $intmax)
{
$string_cut = substr($string, 0, $intmax);
$last_space = strrpos($string_cut, "+");
$strings[$i] = substr($string, 0, $last_space);
$string = substr($string, $last_space, strlen($string));
$i++;
}
$strings[$i] = $string;
return $strings;
}
/**
* Convert Words (text) to Speech (MP3)
*
*/
protected function TTSToMp3($words,$lang)
{
// Directory
$folder = "audio/".$lang;
// Replace the non-alphanumeric characters
// The spaces in the sentence are replaced with the Plus symbol
$words = urlencode($words);
// Name of the MP3 file generated using the MD5 hash
$file = md5($words);
// If folder doesn't exists, create it
if (!file_exists($folder))
mkdir($folder, 0755, true);
// Save the MP3 file in this folder with the .mp3 extension
$file = $folder."/TTS-".$file.".mp3";
// If the MP3 file exists, do not create a new request
if (!file_exists($file))
{
// Google Translate API cannot handle strings > 100 characters
$words = $this->CutString($words,100);
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0');
$mp3 = "";
for ($i = 0; $i < count($words); $i++)
$mp3[$i] = file_get_contents('http://translate.google.com/translate_tts?q='.$words[$i].'&tl='.$lang);
file_put_contents($file, $mp3);
}
return $file;
}
/**
* Say song name via TTS message
* @param string message
* @param string radio name display on sonos controller
* @param int volume
* @param string language
*/
public function SongNameTTS($directory,$volume=0,$unmute=0,$lang='fr')
{
$ThisSong = "Cette chanson s'appelle ";
$By = " de ";
$actual['track'] = $this->GetPositionInfo();
$SongName = $actual['track']['Title'];
$Artist = $actual['track']['TitleArtist'];
$message = $ThisSong . $SongName . $By . $Artist ;
$this->PlayTTS($message,$directory,$volume,$unmute,$lang);
return true;
}
/**
* Play a TTS message
* @param string message
* @param string radio name display on sonos controller
* @param int volume
* @param string language
*/
public function PlayTTS($message,$directory,$volume=0,$unmute=0,$lang='fr')
{
$actual['track'] = $this->GetPositionInfo();
$actual['volume'] = $this->GetVolume();
$actual['mute'] = $this->GetMute();
$actual['status'] = $this->GetTransportInfo();
$this->Pause();
if ($unmute == 1)
$this->SetMute(0);
if ($volume != 0)
$this->SetVolume($volume);
$file = 'x-file-cifs://'.$directory.'/'.$this->TTSToMp3($message,$lang);
if (((stripos($actual['track']["TrackURI"],"x-file-cifs://")) !== false) or ((stripos($actual['track']["TrackURI"],".mp3")) !== false))
{
// It's a MP3 file
$TrackNumber = $this->AddURIToQueue($file);
$this->ChangeTrack($TrackNumber);
$this->Play();
while (true) {
@$ttsFile=$this->GetPositionInfo();
if($ttsFile["TrackNumberInQueue"]!=$TrackNumber)
break;
usleep(10000);
}
$this->Pause();
$this->SetVolume($actual['volume']);
$this->SetMute($actual['mute']);
$this->ChangeTrack($actual['track']["TrackNumberInQueue"]);
$this->SeekTime($actual['track']["RelTime"]);
$this->RemoveTrackFromQueue($TrackNumber);
}
else
{
//It's a radio / or TV (playbar) / or nothing
$this->SetQueue($file);
$this->Play();
sleep(2);
while ($this->GetTransportInfo() == "PLAYING") {}
$this->Pause();
$this->SetVolume($actual['volume']);
$this->SetMute($actual['mute']);
$this->SetQueue($actual['track']["TrackURI"]);
}
if (strcmp($actual['status'],"PLAYING") == 0)
$this->Play();
return true;
}
public function AddSpotifyToQueue($spotify_id, $next = false) {
$rand = mt_rand(10000000, 99999999);
$meta = '<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
<item id="'.$rand.'spotify%3atrack%3a'.$spotify_id.'" restricted="true">
<dc:title></dc:title>
<upnp:class>object.item.audioItem.musicTrack</upnp:class>
<desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON2311_X_#Svc2311-0-Token</desc>
</item>
</DIDL-Lite>';
$meta = htmlentities($meta);
$url = '/MediaRenderer/AVTransport/Control';
$action = 'AddURIToQueue';
$service = 'urn:schemas-upnp-org:service:AVTransport:1';
$next = (int)$next;
$args = "
<InstanceID>0</InstanceID>
<EnqueuedURI>x-sonos-spotify:spotify%3atrack%3a{$spotify_id}</EnqueuedURI>
<EnqueuedURIMetaData>{$meta}</EnqueuedURIMetaData>
<DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued>
<EnqueueAsNext>{$next}</EnqueueAsNext>
";
$filter = 'FirstTrackNumberEnqueued';
return $this->Upnp($url, $service, $action, $args, $filter);
}
public function device_info() {
$xml = $this->_device_info_raw('/xml/device_description.xml');
$out = [
'friendlyName' => (string)$xml->device->friendlyName,
'modelNumber' => (string)$xml->device->modelNumber,
'modelName' => (string)$xml->device->modelName,
'softwareVersion' => (string)$xml->device->softwareVersion,
'hardwareVersion' => (string)$xml->device->hardwareVersion,
'roomName' => (string)$xml->device->roomName,
];
return $out;
}
public function get_coordinator() {
$topology = $this->_device_info_raw('/status/topology');
$myself = null;
$coordinators = [];
// Loop players, build map of coordinators and find myself
foreach ($topology->ZonePlayers->ZonePlayer as $player) {
$player_data = $player->attributes();
$ip = parse_url((string)$player_data->location)['host'];
if ($ip == $this->IP) {
$myself = $player_data;
}
if ((string)$player_data->coordinator == 'true') {
$coordinators[(string)$player_data->group] = $ip;
}
}
$coordinator = $coordinators[(string)$myself->group];
return new static($coordinator);
}
protected function _device_info_raw($url) {
$url = "http://{$this->IP}:{$this->PORT}{$url}";
if (!isset($this->_raw[$url])) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$this->_raw[$url] = simplexml_load_string($data);
}
return $this->_raw[$url];
}
public static function detect($ip = '239.255.255.250', $port = 1900) {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($sock, getprotobyname('ip'), IP_MULTICAST_TTL, 2);
$data = <<<DATA
M-SEARCH * HTTP/1.1
HOST: {$ip}:reservedSSDPport
MAN: ssdp:discover
MX: 1
ST: urn:schemas-upnp-org:device:ZonePlayer:1
DATA;
socket_sendto($sock, $data, strlen($data), null, $ip, $port);
// All passed by ref
$read = [$sock];
$write = $except = [];
$name = $port = null;
$tmp = '';
// Read buffer
$buff = '';
// Loop until there's nothing more to read
while (socket_select($read, $write, $except, 1) && $read) {
socket_recvfrom($sock, $tmp, 2048, null, $name, $port);
$buff .= $tmp;
}
// Parse buffer into devices
$data = static::_parse_detection_replies($buff);
// Make an array of myselfs
$devices = [];
$unique = [];
foreach ($data as $datum) {
if(in_array($datum['usn'],$unique)) {
continue;
}
$url = parse_url($datum['location']);
$devices[] = new static($url['host'], $url['port']);
$unique[] = $datum['usn'];
}
return $devices;
}
protected static function _parse_detection_replies($replies) {
$out = [];
// Loop each reply
foreach (explode("\r\n\r\n", $replies) as $reply) {
if ( ! $reply) {
continue;
}
// New array entry
$arr =& $out[];
// Loop each line
foreach (explode("\r\n", $reply) as $line) {
// End of header name
if (($colon = strpos($line, ':')) !== false) {
$name = strtolower(substr($line, 0, $colon));
$val = trim(substr($line, $colon + 1));
$arr[$name] = $val;
}
}
}
return $out;
}
public static function get_room_coordinator($room_name) {
// Detect devices. Sometimes takes a few goes.
do {
$devices = static::detect();
if (!$devices) {
sleep(1);
}
} while (!$devices);
foreach ($devices as $device) {
if ($device->device_info()['roomName'] == $room_name) {
return $device->get_coordinator();
}
}
return false;
}
}