-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathChromecast.php
481 lines (468 loc) · 15.6 KB
/
Chromecast.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
<?php
// Chris Ridings
// www.chrisridings.com
require_once ("CCprotoBuf.php");
require_once ("CCDefaultMediaPlayer.php");
require_once ("CCPlexPlayer.php");
require_once ("mdns.php");
class Chromecast
{
// Sends a picture or a video to a Chromecast using reverse
// engineered castV2 protocol
public $socket;
// Socket to the Chromecast
public $requestId = 1;
// Incrementing request ID parameter
public $transportid = "";
// The transportid of our connection
public $sessionid = "";
// Session id for any media sessions
public $DMP;
// Represents an instance of the Default Media Player.
public $Plex;
// Represents an instance of the Plex player
public $lastip = "";
// Store the last connected IP
public $lastport;
// Store the last connected port
public $lastactivetime;
// store the time we last did something
public function __construct($ip, $port)
{
// Establish Chromecast connection
// Don't pay much attention to the Chromecast's certificate.
// It'll be for the wrong host address anyway if we
// use port forwarding!
$contextOptions = ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false, ]];
$context = stream_context_create($contextOptions);
if ($this->socket = stream_socket_client('ssl://' . $ip . ":" . $port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context)) {
}
else {
throw new Exception("Failed to connect to remote Chromecast");
}
$this->lastip = $ip;
$this->lastport = $port;
$this->lastactivetime = time();
// Create an instance of the DMP for this CCDefaultMediaPlayer
$this->DMP = new CCDefaultMediaPlayer($this);
$this->Plex = new CCPlexPlayer($this);
}
public static function scan($wait = 15)
{
// Wrapper for scan
$result = Chromecast::scansub($wait);
return $result;
}
public static function scansub($wait = 15)
{
// Performs an mdns scan of the network to find chromecasts and returns an array
// Let's test by finding Google Chromecasts
$mdns = new mDNS();
// Search for chromecast devices
// For a bit more surety, send multiple search requests
$firstresponsetime = - 1;
$lastpackettime = - 1;
$starttime = round(microtime(true) * 1000);
$mdns->query("_googlecast._tcp.local", 1, 12, "");
$mdns->query("_googlecast._tcp.local", 1, 12, "");
$mdns->query("_googlecast._tcp.local", 1, 12, "");
$cc = $wait;
$filetoget = 1;
$dontrequery = 0;
set_time_limit($wait * 2);
$chromecasts = array();
while ($cc > 0) {
$inpacket = "";
while ($inpacket == "") {
$inpacket = $mdns->readIncoming();
if ($inpacket <> "") {
if ($inpacket->packetheader->getQuestions() > 0) {
$inpacket = "";
}
}
if ($lastpackettime <> - 1) {
// If we get to here then we have a valid last packet time
$timesincelastpacket = round(microtime(true) * 1000) - $lastpackettime;
if ($timesincelastpacket > ($firstresponsetime * 5) && $firstresponsetime != - 1) {
return $chromecasts;
}
}
if ($inpacket <> "") {
$lastpackettime = round(microtime(true) * 1000);
}
$timetohere = round(microtime(true) * 1000) - $starttime;
// Maximum five second rule
if ($timetohere > 5000) {
return $chromecasts;
}
}
// If our packet has answers, then read them
// $mdns->printPacket($inpacket);
if ($inpacket->packetheader->getAnswerRRs() > 0) {
$dontrequery = 0;
// $mdns->printPacket($inpacket);
for ($x = 0; $x < sizeof($inpacket->answerrrs); $x++) {
if ($inpacket->answerrrs[$x]->qtype == 12) {
// print_r($inpacket->answerrrs[$x]);
if ($inpacket->answerrrs[$x]->name == "_googlecast._tcp.local") {
if ($firstresponsetime == - 1) {
$firstresponsetime = round(microtime(true) * 1000) - $starttime;
}
$name = "";
for ($y = 0; $y < sizeof($inpacket->answerrrs[$x]->data); $y++) {
$name.= chr($inpacket->answerrrs[$x]->data[$y]);
}
// The chromecast itself fills in additional rrs. So if that's there then we have a quicker method of
// processing the results.
// First build any missing entries with any 33 packets we find.
for ($p = 0; $p < sizeof($inpacket->additionalrrs); $p++) {
if ($inpacket->additionalrrs[$p]->qtype == 33) {
$d = $inpacket->additionalrrs[$p]->data;
$port = ($d[4] * 256) + $d[5];
// We need the target from the data
$offset = 6;
$size = $d[$offset];
$offset++;
$target = "";
for ($z = 0; $z < $size; $z++) {
$target.= chr($d[$offset + $z]);
}
$target.= ".local";
if (!isset($chromecasts[$inpacket->additionalrrs[$p]->name])) {
$chromecasts[$inpacket->additionalrrs[$x]->name] = array(
"port" => $port,
"ip" => "",
"target" => "",
"friendlyname" => ""
);
}
$chromecasts[$inpacket->additionalrrs[$x]->name]['target'] = $target;
}
}
// Next repeat the process for 16
for ($p = 0; $p < sizeof($inpacket->additionalrrs); $p++) {
if ($inpacket->additionalrrs[$p]->qtype == 16) {
$fn = "";
for ($q = 0; $q < sizeof($inpacket->additionalrrs[$p]->data); $q++) {
$fn.= chr($inpacket->additionalrrs[$p]->data[$q]);
}
$stp = strpos($fn, "fn=") + 3;
$etp = strpos($fn, "ca=");
$fn = substr($fn, $stp, $etp - $stp - 1);
if (!isset($chromecasts[$inpacket->additionalrrs[$p]->name])) {
$chromecasts[$inpacket->additionalrrs[$x]->name] = array(
"port" => 8009,
"ip" => "",
"target" => "",
"friendlyname" => ""
);
}
$chromecasts[$inpacket->additionalrrs[$x]->name]['friendlyname'] = $fn;
}
}
// And finally repeat again for 1
for ($p = 0; $p < sizeof($inpacket->additionalrrs); $p++) {
if ($inpacket->additionalrrs[$p]->qtype == 1) {
$d = $inpacket->additionalrrs[$p]->data;
$ip = $d[0] . "." . $d[1] . "." . $d[2] . "." . $d[3];
foreach($chromecasts as $key => $value) {
if ($value['target'] == $inpacket->additionalrrs[$p]->name) {
$value['ip'] = $ip;
$chromecasts[$key] = $value;
}
}
}
}
$dontrequery = 1;
// Check our item. If it doesn't exist then it wasn't in the additionals, so send requests.
// If it does exist then check it has all the items. If not, send the requests.
if (isset($chromecasts[$name])) {
$xx = $chromecasts[$name];
if ($xx['target'] == "") {
// Send a 33 request
$mdns->query($name, 1, 33, "");
$dontrequery = 0;
}
if ($xx['friendlyname'] == "") {
// Send a 16 request
$mdns->query($name, 1, 16, "");
$dontrequery = 0;
}
if ($xx['target'] != "" && $xx['friendlyname'] != "" && $xx['ip'] == "") {
// Only missing the ip address for the target.
$mdns->query($xx['target'], 1, 1, "");
$dontrequery = 0;
}
}
else {
// Send queries. These'll trigger a 1 query when we have a target name.
$mdns->query($name, 1, 33, "");
$mdns->query($name, 1, 16, "");
$dontrequery = 0;
}
if ($dontrequery == 0) {
$cc = $wait;
}
set_time_limit($wait * 2);
}
}
if ($inpacket->answerrrs[$x]->qtype == 33) {
$d = $inpacket->answerrrs[$x]->data;
$port = ($d[4] * 256) + $d[5];
// We need the target from the data
$offset = 6;
$size = $d[$offset];
$offset++;
$target = "";
for ($z = 0; $z < $size; $z++) {
$target.= chr($d[$offset + $z]);
}
$target.= ".local";
if (!isset($chromecasts[$inpacket->answerrrs[$x]->name])) {
$chromecasts[$inpacket->answerrrs[$x]->name] = array(
"port" => $port,
"ip" => "",
"target" => $target,
"friendlyname" => ""
);
}
else {
$chromecasts[$inpacket->answerrrs[$x]->name]['target'] = $target;
}
// We know the name and port. Send an A query for the IP address
$mdns->query($target, 1, 1, "");
$cc = $wait;
set_time_limit($wait * 2);
}
if ($inpacket->answerrrs[$x]->qtype == 16) {
$fn = "";
for ($q = 0; $q < sizeof($inpacket->answerrrs[$x]->data); $q++) {
$fn.= chr($inpacket->answerrrs[$x]->data[$q]);
}
$stp = strpos($fn, "fn=") + 3;
$etp = strpos($fn, "ca=");
$fn = substr($fn, $stp, $etp - $stp - 1);
if (!isset($chromecasts[$inpacket->answerrrs[$x]->name])) {
$chromecasts[$inpacket->answerrrs[$x]->name] = array(
"port" => 8009,
"ip" => "",
"target" => "",
"friendlyname" => $fn
);
}
else {
$chromecasts[$inpacket->answerrrs[$x]->name]['friendlyname'] = $fn;
}
$mdns->query($chromecasts[$inpacket->answerrrs[$x]->name]['target'], 1, 1, "");
$cc = $wait;
set_time_limit($wait * 2);
}
if ($inpacket->answerrrs[$x]->qtype == 1) {
$d = $inpacket->answerrrs[$x]->data;
$ip = $d[0] . "." . $d[1] . "." . $d[2] . "." . $d[3];
// Loop through the chromecasts and fill in the ip
foreach($chromecasts as $key => $value) {
if ($value['target'] == $inpacket->answerrrs[$x]->name) {
$value['ip'] = $ip;
$chromecasts[$key] = $value;
// If we have an IP address but no friendly name, try and get the friendly name again!
if (strlen($value['friendlyname']) < 1) {
$mdns->query($key, 1, 16, "");
$cc = $wait;
set_time_limit($wait * 2);
}
}
}
}
}
}
$cc--;
}
return $chromecasts;
}
function testLive()
{
// If there is a difference of 10 seconds or more between $this->lastactivetime and the current time, then we've been kicked off and need to reconnect
if ($this->lastip == "") {
return;
}
$diff = time() - $this->lastactivetime;
if ($diff > 9) {
// Reconnect
$contextOptions = ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false, ]];
$context = stream_context_create($contextOptions);
if ($this->socket = stream_socket_client('ssl://' . $this->lastip . ":" . $this->lastport, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context)) {
}
else {
throw new Exception("Failed to connect to remote Chromecast");
}
$this->cc_connect(1);
$this->connect(1);
}
}
function cc_connect($tl = 0)
{
// CONNECT TO CHROMECAST
// This connects to the chromecast in general.
// Generally this is called by launch($appid) automatically upon launching an app
// but if you want to connect to an existing running application then call this first,
// then call getStatus() to make sure you get a transportid.
if ($tl == 0) {
$this->testLive();
};
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = "receiver-0";
$c->urnnamespace = "urn:x-cast:com.google.cast.tp.connection";
$c->payloadtype = 0;
$c->payloadutf8 = '{"type":"CONNECT"}';
fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
}
public function launch($appid)
{
// Launches the chromecast app on the connected chromecast
// CONNECT
$this->cc_connect();
$this->getStatus();
// LAUNCH
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = "receiver-0";
$c->urnnamespace = "urn:x-cast:com.google.cast.receiver";
$c->payloadtype = 0;
$c->payloadutf8 = '{"type":"LAUNCH","appId":"' . $appid . '","requestId":' . $this->requestId . '}';
fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
$this->requestId++;
$oldtransportid = $this->transportid;
while ($this->transportid == "" || $this->transportid == $oldtransportid) {
$r = $this->getCastMessage();
sleep(1);
}
}
function getStatus()
{
// Get the status of the chromecast in general and return it
// also fills in the transportId of any currently running app
$this->testLive();
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = "receiver-0";
$c->urnnamespace = "urn:x-cast:com.google.cast.receiver";
$c->payloadtype = 0;
$c->payloadutf8 = '{"type":"GET_STATUS","requestId":' . $this->requestId . '}';
$c = fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
$this->requestId++;
$r = "";
while ($this->transportid == "") {
$r = $this->getCastMessage();
}
return $r;
}
function connect($tl = 0)
{
// This connects to the transport of the currently running app
// (you need to have launched it yourself or connected and got the status)
if ($tl == 0) {
$this->testLive();
};
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = $this->transportid;
$c->urnnamespace = "urn:x-cast:com.google.cast.tp.connection";
$c->payloadtype = 0;
$c->payloadutf8 = '{"type":"CONNECT"}';
fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
$this->requestId++;
}
public function getCastMessage()
{
// Get the Chromecast Message/Response
// Later on we could update CCprotoBuf to decode this
// but for now all we need is the transport id and session id if it is
// in the packet and we can read that directly.
$this->testLive();
$response = fread($this->socket, 2000);
while (preg_match("/urn:x-cast:com.google.cast.tp.heartbeat/", $response) && preg_match("/\"PING\"/", $response)) {
$this->pong();
sleep(3);
$response = fread($this->socket, 2000);
// Wait infinitely for a packet.
set_time_limit(30);
}
if (preg_match("/transportId/s", $response)) {
preg_match("/transportId\"\:\"([^\"]*)/", $response, $matches);
$matches = $matches[1];
$this->transportid = $matches;
}
if (preg_match("/sessionId/s", $response)) {
preg_match("/\"sessionId\"\:\"([^\"]*)/", $response, $r);
$this->sessionid = $r[1];
}
return $response;
}
public function sendMessage($urn, $message)
{
// Send the given message to the given urn
$this->testLive();
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = $this->transportid;
// Override - if the $urn is urn:x-cast:com.google.cast.receiver then
// send to receiver-0 and not the running app
if ($urn == "urn:x-cast:com.google.cast.receiver") {
$c->receiver_id = "receiver-0";
}
if ($urn == "urn:x-cast:com.google.cast.tp.connection") {
$c->receiver_id = "receiver-0";
}
$c->urnnamespace = $urn;
$c->payloadtype = 0;
$c->payloadutf8 = $message;
fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
$this->requestId++;
$response = $this->getCastMessage();
return $response;
}
public function pingpong()
{
// Officially you should run this every 5 seconds or so to keep
// the device alive. Doesn't seem to be necessary if an app is running
// that doesn't have a short timeout.
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = "receiver-0";
$c->urnnamespace = "urn:x-cast:com.google.cast.tp.heartbeat";
$c->payloadtype = 0;
$c->payloadutf8 = '{"type":"PING"}';
fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
$this->requestId++;
$response = $this->getCastMessage();
}
public function pong()
{
// To answer a pingpong
$c = new CastMessage();
$c->source_id = "sender-0";
$c->receiver_id = "receiver-0";
$c->urnnamespace = "urn:x-cast:com.google.cast.tp.heartbeat";
$c->payloadtype = 0;
$c->payloadutf8 = '{"type":"PONG"}';
fwrite($this->socket, $c->encode());
fflush($this->socket);
$this->lastactivetime = time();
$this->requestId++;
}
}
?>