-
Notifications
You must be signed in to change notification settings - Fork 8
/
ArduCastControl.cpp
580 lines (495 loc) · 20 KB
/
ArduCastControl.cpp
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
#include "ArduCastControl.h"
#include "cast_channel.pb.h"
#include "authority_keys.pb.h"
#include "logging.pb.h"
#include "pb_common.h"
#include "pb.h"
#include "pb_encode.h"
#include "string.h"
const char CC_SOURCEID[] = "sender-0";
const char CC_MAIN_DESTIID[] = "receiver-0";
const char CC_NS_CONNECTION[] = "urn:x-cast:com.google.cast.tp.connection";
const char CC_NS_RECEIVER[] = "urn:x-cast:com.google.cast.receiver";
const char CC_NS_HEARTBEAT[] = "urn:x-cast:com.google.cast.tp.heartbeat";
const char CC_NS_MEDIA[] = "urn:x-cast:com.google.cast.media";
const char CC_MSG_CONNECT[] = "{\"type\": \"CONNECT\"}";
const char CC_MSG_PING[] = "{\"type\": \"PING\"}";
const char CC_MSG_GET_STATUS[] = "{\"type\": \"GET_STATUS\", \"requestId\": 1}";
//incomplet msgs as these need some args to the end
const char CC_MSG_PLAY[] = "{\"type\": \"PLAY\", \"requestId\": 2, \"mediaSessionId\": ";
const char CC_MSG_PAUSE[] = "{\"type\": \"PAUSE\", \"requestId\": 2, \"mediaSessionId\": ";
const char CC_MSG_NEXT[] = "{\"type\": \"QUEUE_NEXT\", \"requestId\": 2, \"mediaSessionId\": ";
const char CC_MSG_PREV[] = "{\"type\": \"QUEUE_PREV\", \"requestId\": 2, \"mediaSessionId\": ";
const char CC_MSG_SET_VOL[] = "{\"type\": \"SET_VOLUME\", \"requestId\": 2, \"volume\": {\"level\": ";//this need double braces!
const char CC_MSG_VOL_MUTE[] = "{\"type\": \"SET_VOLUME\", \"requestId\": 2, \"volume\": {\"muted\": ";//this need double braces!
static char cc_msg_ctrl[128];
// void ArduCastConnection::init(WiFiClientSecure& client, int keepAlive, uint8_t *writeBuffer, int writeBufferSize){
// //this->client = client;
// this->keepAlive = keepAlive;
// this->writeBuffer = writeBuffer;
// this->writeBufferSize = writeBufferSize;
// connected = false;
// }
int ArduCastConnection::connect(const char* destinationId){
// Serial.printf("Connect to %s\n", destinationId);
strncpy(destId, destinationId, sizeof(destId));
int err = writeMsg(CC_NS_CONNECTION, CC_MSG_CONNECT);
pinged(); //do not ping immediately - we probably want to check status anyway
connected = true;
return err;
}
void ArduCastConnection::pinged(){
lastMsgAt = millis();
}
void ArduCastConnection::setDisconnect(){
connected = false;
}
channelConnection_t ArduCastConnection::getConnectionStatus(){
if ( !client.connected() || !connected )
return CH_DISCONNECTED;
if ( (unsigned long)(lastMsgAt + 3*keepAlive) < millis() ){
connected = false;
return CH_DISCONNECTED;
}
if ( (unsigned long)(lastMsgAt + keepAlive) < millis())
return CH_NEEDS_PING;
else
return CH_CONNECTED;
}
const char* ArduCastConnection::getDestinationId(){
return destId;
}
bool ArduCastConnection::encode_string(pb_ostream_t *stream, const pb_field_iter_t *field, void * const *arg)
{
const char *str = (const char*)(*arg);
// Serial.printf("Encode: %s (%d)\n",str, strlen(str));
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
int ArduCastConnection::writeMsg(const char* nameSpace, const char* payload){
if ( !client.connected() )
return -1;
extensions_api_cast_channel_CastMessage newMsg = extensions_api_cast_channel_CastMessage_init_zero;
pb_ostream_t stream = pb_ostream_from_buffer(writeBuffer+4, writeBufferSize-4);
newMsg.source_id.arg = (void*)CC_SOURCEID;
newMsg.source_id.funcs.encode = encode_string;
newMsg.destination_id.arg = (void*)destId;
newMsg.destination_id.funcs.encode = encode_string;
newMsg.namespace_fix.arg = (void*)nameSpace;
newMsg.namespace_fix.funcs.encode = encode_string;
newMsg.payload_type = extensions_api_cast_channel_CastMessage_PayloadType_STRING;
newMsg.payload_utf8.arg = (void*)payload;
newMsg.payload_utf8.funcs.encode = encode_string;
bool status = pb_encode(&stream, extensions_api_cast_channel_CastMessage_fields, &newMsg);
if (!status)
return -2;
writeBuffer[0] = (stream.bytes_written>>24) & 0xFF;
writeBuffer[1] = (stream.bytes_written>>16) & 0xFF;
writeBuffer[2] = (stream.bytes_written>>8) & 0xFF;
writeBuffer[3] = (stream.bytes_written>>0) & 0xFF;
uint32_t len = client.write(writeBuffer, stream.bytes_written+4);
if (len < stream.bytes_written+4)
return -3;
return 0;
}
////////////////////////
uint32_t ArduCastControl::getIncomingMessageLength(WiFiClientSecure &client){
uint8_t buffer[4];
client.peekBytes(buffer, 4);
uint32_t len = (buffer[0]<<24) + (buffer[1]<<16) + (buffer[2]<<8) + buffer[3];
return len;
}
uint32_t ArduCastControl::getRawMessage(uint8_t *buffer, uint16_t bufSize, WiFiClientSecure &client, uint32_t timeout){
unsigned long start = millis();
// Serial.printf("Checking read %d\n", client.available());
if (bufSize <= 4 || client.available() < 4 ){
return 0;
}
uint16_t len = getIncomingMessageLength(client);
while ( client.available() < len+4 ){
if (millis() - start > timeout ){
// Serial.println("timeout");
while(client.available())
client.read();
return 0;
}
}
uint32_t dump = 0;
if ( bufSize < len + 4 ){
dump = len - (bufSize-4);
len = bufSize-4;
}
client.readBytes(buffer, len+4);
while ( dump > 0){
client.read();
dump--;
}
return len+4;
}
int ArduCastControl::connect(const char* host){
client.allowSelfSignedCerts(); //chromecast seems to use self signed cert
int err = client.connect(host, 8009);
if ( !err ){
return -10;
}
connectionStatus = TCPALIVE;
// deviceConnection.init(client, PING_TIMEOUT, connBuffer, CONNBUFFER_SIZE);
// applicationConnection.init(client, PING_TIMEOUT, connBuffer, CONNBUFFER_SIZE);
err = deviceConnection.connect(CC_MAIN_DESTIID);
if ( err == 0 )
connectionStatus = CONNECTED;
return err;
}
void ArduCastControl::printRawMsg(int64_t len, uint8_t *buffer){
Serial.printf("Message Length: %lld\n", len);
Serial.print("Message: ");
//python style print for compare
for(int64_t i = 0; i<len; i++){
if ( buffer[i] < 127 && buffer[i] > 31){
Serial.printf("%c",buffer[i]);
} else {
Serial.printf("\\x%02X",buffer[i]);
}
}
//hex stream print for https://protogen.marcgravell.com/decode
// for(int64_t i = 0; i<len; i++){
// Serial.printf("%02X",buffer[i]);
// }
Serial.println();
}
uint8_t ArduCastControl::pbDecodeVarint(uint8_t *bufferStart, uint32_t *decodedInt){
*decodedInt = 0;
int8_t decoded = -1;
do {
decoded++;
//Serial.printf("curr=0x%02x, in=0x%02x, d=%d\n", *decodedInt, bufferStart[decoded], decoded);
*decodedInt |= (bufferStart[decoded] & 0x7f) << (decoded * 7);
} while ( bufferStart[decoded] & 0x80 );
return decoded+1;
}
uint8_t ArduCastControl::pbDecodeHeader(uint8_t *bufferStart, uint8_t *tag, uint8_t *wire, uint32_t *lengthOrValue){
*wire = bufferStart[0] & 0x07;
*tag = bufferStart[0] >> 3;
uint8_t processedBytes = 1;
//Serial.printf("desc=0x%02x\n", bufferStart[0]);
processedBytes += pbDecodeVarint(bufferStart+1, lengthOrValue);
return processedBytes;
}
connection_t ArduCastControl::loop(){
if ( !client.connected() ){
client.stopAll();
connectionStatus = DISCONNECTED;
return DISCONNECTED;
}
uint32_t read;
bool rxProcessed = false;
//--------------------- RX code -----------------------------
do {
//download the msg to connBuffer (only accept what we expect)
read = getRawMessage(connBuffer, CONNBUFFER_SIZE, client, 100);
if ( read > 0){
rxProcessed = true; //this will disable tx operations in this loop
msgSent = false; //we assume this is a response to the message we sent
errorCount = 5; //connection is alive, reset errorCount
uint8_t processPayload = 0; //assume no need to process it
//printRawMsg(read-4, connBuffer+4);
uint32_t offset = 4; //skip the length field, it's not pb
//iterate through protobuf
do {
uint8_t tag, wire;
uint32_t lengthOrValue;
offset += pbDecodeHeader(connBuffer+offset, &tag, &wire, &lengthOrValue);
//check which device responded, accept it as pong
if ( tag == extensions_api_cast_channel_CastMessage_source_id_tag ){
if( 0 == memcmp(connBuffer+offset, deviceConnection.getDestinationId(), lengthOrValue))
{
//main device, process the payload of main RECEIVER_STATUS
// Serial.println("Pong from device");
deviceConnection.pinged();
processPayload = 1;
}
if ( applicationConnection.getConnectionStatus() != CH_DISCONNECTED &&
0 == memcmp(connBuffer+offset, applicationConnection.getDestinationId(), lengthOrValue))
{
//application, process the payload as MEDIA_STATUS
// Serial.println("Pong from app");
processPayload = 2;
applicationConnection.pinged();
}
}
//check the namespace, we're only process receiver and media
if ( tag == extensions_api_cast_channel_CastMessage_namespace_fix_tag ){
if( 0 == memcmp(connBuffer+offset, CC_NS_HEARTBEAT, lengthOrValue)){ //pong message, no need to process the payload
processPayload = 0;
}
if( 0 == memcmp(connBuffer+offset, CC_NS_CONNECTION, lengthOrValue)){ //must be a close message
if ( processPayload == 1 ){
applicationConnection.setDisconnect();
deviceConnection.setDisconnect();
connectionStatus = TCPALIVE;
processPayload = false;
} else if ( processPayload == 2) {
applicationConnection.setDisconnect();
processPayload = false;
}
}
}
if ( processPayload > 0 && tag == extensions_api_cast_channel_CastMessage_payload_utf8_tag ){
DynamicJsonDocument doc(JSONBUFFER_SIZE);
DeserializationError error = deserializeJson(doc, connBuffer+offset, lengthOrValue);
if ( !error && doc.containsKey("type") && doc.containsKey("status") ){ //it pretty much must contain it
if ( processPayload == 1 && strcmp("RECEIVER_STATUS", doc["type"].as<char*>()) == 0 ) {
//save the generic info
if ( doc["status"].containsKey("volume") ){
if( doc["status"]["volume"].containsKey("level")){
volume = doc["status"]["volume"]["level"];
} else
volume = -1.0;
if( doc["status"]["volume"].containsKey("muted"))
isMuted = doc["status"]["volume"]["muted"].as<bool>();
else
isMuted = false;
} else {
volume = -1.0;
isMuted = false;
}
if ( doc["status"].containsKey("applications") ){
if ( doc["status"]["applications"][0].containsKey("sessionId") ){
strncpy(sessionId, doc["status"]["applications"][0]["sessionId"].as<char*>() ,sizeof(sessionId));
sessionId[sizeof(sessionId)-1] = '\n';
connectionStatus = CONNECT_TO_APPLICATION;
} else
sessionId[0] = '\n';
if ( doc["status"]["applications"][0].containsKey("statusText") ){
strncpy(statusText, doc["status"]["applications"][0]["statusText"].as<char*>() ,sizeof(statusText));
statusText[sizeof(statusText)-1] = '\n';
} else
statusText[0] = '\n';
if ( doc["status"]["applications"][0].containsKey("displayName") ){
strncpy(displayName, doc["status"]["applications"][0]["displayName"].as<char*>() ,sizeof(displayName));
displayName[sizeof(displayName)-1] = '\n';
} else
displayName[0] = '\n';
} else {
sessionId[0] = '\0';
statusText[0] = '\0';
displayName[0] = '\0';
}
}
if ( processPayload == 2 && strcmp("MEDIA_STATUS", doc["type"].as<char*>()) == 0 ) {
if ( doc["status"][0].containsKey("mediaSessionId") )
mediaSessionId = doc["status"][0]["mediaSessionId"];
else
mediaSessionId = -1;
if ( doc["status"][0].containsKey("currentTime") )
currentTime = doc["status"][0]["currentTime"];
else
currentTime = 0.0;
if ( doc["status"][0].containsKey("playerState") ){
if ( strcmp("IDLE", doc["status"][0]["playerState"].as<char*>()) == 0 ){
playerState = IDLE;
} else if ( strcmp("BUFFERING", doc["status"][0]["playerState"].as<char*>()) == 0 ){
playerState = BUFFERING;
} else if ( strcmp("PLAYING", doc["status"][0]["playerState"].as<char*>()) == 0 ){
playerState = PLAYING;
} else if ( strcmp("PAUSED", doc["status"][0]["playerState"].as<char*>()) == 0 ){
playerState = PAUSED;
} else {
playerState = IDLE;
}
} else
playerState = IDLE;
if ( doc["status"][0].containsKey("media")){
if ( doc["status"][0]["media"].containsKey("duration") ){
duration = doc["status"][0]["media"]["duration"];
} else {
duration = 0.0;
}
if ( doc["status"][0]["media"].containsKey("metadata") ){
if ( doc["status"][0]["media"]["metadata"].containsKey("title") ){
strncpy(title, doc["status"][0]["media"]["metadata"]["title"].as<char*>() ,sizeof(title));
title[sizeof(title)-1] = '\n';
} else {
title[0] = '\0';
}
if ( doc["status"][0]["media"]["metadata"].containsKey("artist") ){
strncpy(artist, doc["status"][0]["media"]["metadata"]["artist"].as<char*>() ,sizeof(artist));
artist[sizeof(artist)-1] = '\n';
} else {
artist[0] = '\0';
}
} else {
title[0] = '\n';
artist[0] = '\n';
}
} else {
//CC seems to skip sending this when it's busy, so we ignore the error
// duration = 0.0;
// title[0] = '\n';
// artist[0] = '\n';
}
}
}
// serializeJsonPretty(doc, Serial);
// Serial.println();
}
//DEBUG code -- this does not work when DynamicJson is used, probably not enough heap
// Serial.printf("T:%d, W:%d", tag, wire);
// if ( wire == 2 ){
// Serial.printf(" (%d): ", lengthOrValue);
// Serial.printf("%.*s\n", lengthOrValue, connBuffer+offset);
// } else {
// Serial.printf(": %d\n", lengthOrValue);
// }
//end of debug code
//for length delimited stuff, add the decoded length to the offset
if ( wire == 2)
offset+=lengthOrValue;
} while(offset<read);
}
} while ( read > 0);
// ---------------- TX code ------------------------
//don't send msg if we just received one; wait 500ms for an answer
if ( !rxProcessed && (!msgSent || ((millis() - msgSentAt) > 500 ))){
//handle broken links
if ( msgSent ){
Serial.printf("EC:%d\n", errorCount);
if (--errorCount == 0 ){
client.stopAll();
connectionStatus = DISCONNECTED;
msgSent = false;
return DISCONNECTED;
}
}
// Serial.println("Preparing for msg");
msgSent = false;
int err = 0;
if ( connectionStatus == CONNECT_TO_APPLICATION ){
// Serial.print("CA");
err = applicationConnection.connect(sessionId);
if ( err == 0 )
connectionStatus = CONNECTED;
} else if ( applicationConnection.getConnectionStatus() == CH_DISCONNECTED ){
// Serial.print("GS");
err = deviceConnection.writeMsg(CC_NS_RECEIVER, CC_MSG_GET_STATUS);
if ( err == 0 ) {
msgSentAt = millis();
msgSent = true;
}
} else if ( deviceConnection.getConnectionStatus() == CH_NEEDS_PING ){
// Serial.print("ping main");
err = deviceConnection.writeMsg(CC_NS_HEARTBEAT, CC_MSG_PING);
if ( err == 0 ) {
msgSentAt = millis();
msgSent = true;
}
} else if ( applicationConnection.getConnectionStatus() == CH_CONNECTED ){
// Serial.print("GSA");
err = applicationConnection.writeMsg(CC_NS_MEDIA, CC_MSG_GET_STATUS);
if ( err == 0 ) {
msgSentAt = millis();
msgSent = true;
}
} else if ( applicationConnection.getConnectionStatus() == CH_NEEDS_PING ){ //this will never happen, unless loop is called rarely
// Serial.print("ping app");
err = applicationConnection.writeMsg(CC_NS_HEARTBEAT, CC_MSG_PING);
if ( err == 0 ) {
msgSentAt = millis();
msgSent = true;
}
}
// if (msgSent ){
// Serial.printf("Res: %d\n", err);
// }
}
return getConnection();
}
connection_t ArduCastControl::getConnection(){
if ( msgSent )
return WAIT_FOR_RESPONSE;
if ( applicationConnection.getConnectionStatus() != CH_DISCONNECTED )
return APPLICATION_RUNNING;
return connectionStatus;
}
void ArduCastControl::dumpStatus(){
if ( getConnection() != DISCONNECTED && getConnection() != TCPALIVE ){
Serial.printf("V:%f%c\n", volume, isMuted?'M':' ');
if ( applicationConnection.getConnectionStatus() != CH_DISCONNECTED ){
Serial.printf("D:%s\n", displayName);
Serial.printf("S:%s\n", statusText);
Serial.printf("A/T:%s/%s\n", artist, title);
Serial.printf("S:%d %f/%f\n", playerState, duration, currentTime);
}
}
}
int ArduCastControl::play(){
if ( msgSent )
return -10;
if ( mediaSessionId < 0 )
return -9;
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "%s%d}", CC_MSG_PLAY, mediaSessionId );
return applicationConnection.writeMsg(CC_NS_MEDIA, cc_msg_ctrl);
}
int ArduCastControl::pause(bool toggle){
if ( msgSent )
return -10;
if ( mediaSessionId < 0 )
return -9;
if ( toggle && playerState == PAUSED )
return play();
else {
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "%s%d}", CC_MSG_PAUSE, mediaSessionId );
return applicationConnection.writeMsg(CC_NS_MEDIA, cc_msg_ctrl);
}
}
int ArduCastControl::prev(){
if ( msgSent )
return -10;
if ( mediaSessionId < 0 )
return -9;
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "%s%d}", CC_MSG_PREV, mediaSessionId );
return applicationConnection.writeMsg(CC_NS_MEDIA, cc_msg_ctrl);
}
int ArduCastControl::next(){
if ( msgSent )
return -10;
if ( mediaSessionId < 0 )
return -9;
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "%s%d}", CC_MSG_NEXT, mediaSessionId );
return applicationConnection.writeMsg(CC_NS_MEDIA, cc_msg_ctrl);
}
int ArduCastControl::seek(bool relative, float seekTo){
if ( msgSent )
return -10;
if ( mediaSessionId < 0 )
return -9;
if ( relative )
seekTo += currentTime;
if ( seekTo < 0 )
seekTo = 0;
if ( seekTo > duration )
seekTo = duration;
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "{\"type\": \"SEEK\", \"requestId\": 2, \"mediaSessionId\": %d, \"currentTime\": %f}", mediaSessionId, seekTo);
return applicationConnection.writeMsg(CC_NS_MEDIA, cc_msg_ctrl);
}
int ArduCastControl::setVolume(bool relative, float volumeTo){
if ( msgSent )
return -10;
if ( relative )
volumeTo += volume;
if ( volumeTo < 0 )
volumeTo = 0;
if ( volumeTo > 1 )
volumeTo = 1;
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "%s%f}}", CC_MSG_SET_VOL, volumeTo );
return deviceConnection.writeMsg(CC_NS_RECEIVER, cc_msg_ctrl);
}
int ArduCastControl::setMute(bool newMute, bool toggle){
if ( msgSent )
return -10;
if ( toggle )
newMute = !isMuted;
snprintf(cc_msg_ctrl, sizeof(cc_msg_ctrl), "%s%s}}", CC_MSG_VOL_MUTE, newMute?"true":"false" );
return deviceConnection.writeMsg(CC_NS_RECEIVER, cc_msg_ctrl);
}