forked from i8beef/node-red-contrib-castv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcastv2-sender.js
762 lines (670 loc) · 28.6 KB
/
castv2-sender.js
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
module.exports = function(RED) {
"use strict";
const util = require('util');
const net = require('net');
const Client = require('castv2-client').Client;
const Bonjour = require('bonjour');
const MediaReceiverBase = require('./lib/MediaReceiverBase');
const DefaultMediaReceiver = require('./lib/DefaultMediaReceiver');
const DefaultMediaReceiverAdapter = require('./lib/DefaultMediaReceiverAdapter');
const DashCastReceiver = require('./lib/DashCastReceiver');
const DashCastReceiverAdapter = require('./lib/DashCastReceiverAdapter');
const GenericMediaReceiver = require('./lib/GenericMediaReceiver');
const GenericMediaReceiverAdapter = require('./lib/GenericMediaReceiverAdapter');
const YouTubeReceiver = require('./lib/YouTubeReceiver');
const YouTubeReceiverAdapter = require('./lib/YouTubeReceiverAdapter');
function CastV2ConnectionNode(config) {
RED.nodes.createNode(this, config);
let node = this;
// Settings
this.name = config.name;
this.target = config.target;
this.host = config.host;
this.port = config.port;
// Connection state
this.connected = false;
this.connecting = false;
this.closing = false;
// Nodes subscribed to this connection
this.registeredNodes = {};
this.platformStatus = null;
// Platform commands handled by client directly
this.platformCommands = [
"CLOSE",
"GET_VOLUME",
"GET_CAST_STATUS",
"MUTE",
"UNMUTE",
"VOLUME"
];
/*
* Launches session
*/
this.launchAsync = function(castV2App) {
if (!node.connected) {
throw new Error("Not connected");
}
return node.client.launchAsync(castV2App);
};
/*
* Join session
*/
this.joinSessionAsync = function(activeSession, castv2App) {
if (!node.connected) {
throw new Error("Not connected");
}
return node.client.joinAsync(activeSession, castv2App)
};
/*
* Registers a node
*/
this.register = function(castV2Node) {
node.registeredNodes[castV2Node.id] = castV2Node;
if (Object.keys(node.registeredNodes).length === 1) {
node.connect();
}
};
/*
* Deregisters a node
*/
this.deregister = function(castV2Node, done) {
delete node.registeredNodes[castV2Node.id];
if (node.closing) {
return done();
}
if (Object.keys(node.registeredNodes).length === 0) {
if (node.connected || node.connecting) {
node.disconnect();
}
}
done();
};
/*
* Call status() on all registered nodes
*/
this.setStatusOfRegisteredNodes = function(status) {
for (let id in node.registeredNodes) {
if (node.registeredNodes.hasOwnProperty(id)) {
node.registeredNodes[id].status(status);
}
}
}
/*
* Call send() on all registered nodes
*/
this.sendToRegisteredNodes = function(msg) {
for (let id in node.registeredNodes) {
if (node.registeredNodes.hasOwnProperty(id)) {
node.registeredNodes[id].send(msg);
}
}
}
/*
* Joins all nodes matching current sessions
*/
this.joinNodes = function() {
if (!node.connected || !node.platformStatus) {
throw new Error("Not connected");
}
// Update all registered nodes
for (let id in node.registeredNodes) {
if (node.registeredNodes.hasOwnProperty(id)) {
let activeSession = null;
let mediaSupportingApp = null;
let castV2App = null;
if (node.platformStatus.applications) {
activeSession = node.platformStatus.applications
.find(session => node.registeredNodes[id].supportedApplications.some(supportedApp => supportedApp.APP_ID === session.appId));
if (activeSession) {
castV2App = node.registeredNodes[id].supportedApplications.find(supportedApp => supportedApp.APP_ID === activeSession.appId);
} else {
mediaSupportingApp = node.platformStatus.applications
.find(session => session.namespaces != null && session.namespaces.some(namespace => namespace.name === 'urn:x-cast:com.google.cast.media'));
}
}
if (activeSession && castV2App) {
node.registeredNodes[id].join(activeSession, castV2App);
} else if (mediaSupportingApp) {
node.registeredNodes[id].join(mediaSupportingApp, GenericMediaReceiver);
} else {
node.registeredNodes[id].unjoin();
}
}
}
};
/*
* Disconnect handler
*/
this.disconnect = function() {
if (node.connected || node.connecting) {
// Ignore errors
try { node.client.close(); } catch (exception) { }
}
// Reset client
node.client = null;
node.platformStatus = null;
node.connected = false;
node.connecting = false;
// Disconnect all active sessions
for (let id in node.registeredNodes) {
if (node.registeredNodes.hasOwnProperty(id)) {
node.registeredNodes[id].unjoin();
}
}
node.setStatusOfRegisteredNodes({ fill: "red", shape: "ring", text: "disconnected" });
};
/*
* Reconnect handler
*/
this.reconnect = function() {
node.connected = false;
node.connecting = false;
if (!node.closing && Object.keys(node.registeredNodes).length > 0) {
clearTimeout(node.reconnectTimeOut);
node.reconnectTimeOut = setTimeout(() => { node.connect(); }, 3000);
}
};
/*
* Connect handler
*/
this.connect = function() {
if (!node.connected && !node.connecting) {
node.reconnectTimeOut = null;
node.connecting = true;
try {
node.client = new Client();
// The underlying castv2 client emits for all channels of all clients
// This is typically 3 channels for the actual connection, and at least 2 per receiver
node.client.client.setMaxListeners(100);
// Setup promisified methods
node.client.connectAsync = connectOptions => new Promise(resolve => node.client.connect(connectOptions, resolve));
node.client.getAppAvailabilityAsync = util.promisify(node.client.getAppAvailability);
node.client.getSessionsAsync = util.promisify(node.client.getSessions);
node.client.joinAsync = util.promisify(node.client.join);
node.client.launchAsync = util.promisify(node.client.launch);
node.client.getStatusAsync = util.promisify(node.client.getStatus);
node.client.getVolumeAsync = util.promisify(node.client.getVolume);
node.client.setVolumeAsync = util.promisify(node.client.setVolume);
node.client.stopAsync = util.promisify(node.client.stop);
// Register error handler
node.client.once("error", function(error) {
node.disconnect();
node.reconnect();
});
// Register disconnect handlers
node.client.client.once("close", function() {
node.disconnect();
node.reconnect();
});
// Register platform status handler
node.client.on("status", function(status) {
node.platformStatus = status;
node.joinNodes();
node.sendToRegisteredNodes({ platform: status });
});
// Alert connecting state
node.setStatusOfRegisteredNodes({ fill: "yellow", shape: "ring", text: "connecting" });
// Connect
discoverCastTargetsAsync()
.then(castTargets => {
if (node.target) {
// Use target if supplied
let discoveredTarget = castTargets.find(x => x.name === node.target);
return {
host: discoveredTarget != null ? discoveredTarget.address : "0.0.0.0",
port: discoveredTarget != null ? discoveredTarget.port : 8009
};
} else {
return {
host: node.host,
port: node.port || 8009
};
}
})
.then(connectOptions => {
return node.client.connectAsync(connectOptions);
})
.then(() => {
node.connected = true;
node.connecting = false;
// Set registered node status
node.setStatusOfRegisteredNodes({ fill: "green", shape: "ring", text: "connected" });
return node.client.getStatusAsync();
})
.then(status => {
node.platformStatus = status;
// Send initial cast device platform status
node.sendToRegisteredNodes({ platform: status });
// Join all nodes
node.joinNodes();
})
.catch(error => {
console.log(error);
node.disconnect();
node.reconnect();
});
} catch (exception) { console.log(exception); }
}
};
/*
* Close handler
*/
this.on('close', function(done) {
try {
node.closing = true;
node.disconnect();
done();
} catch(error) {
// Swallow any failures here
done();
}
});
/*
* Cast command handler
*/
this.sendPlatformCommandAsync = function(command, receiver) {
if (!node.connected) {
throw new Error("Not connected");
}
// Check for platform commands first
switch (command.type) {
case "CLOSE":
if (receiver) {
return node.client.stopAsync(receiver)
.then(applications => {
return node.client.getStatusAsync();
});
} else {
return node.client.getStatusAsync();
}
break;
case "GET_VOLUME":
return node.client.getVolumeAsync()
.then(volume => node.client.getStatusAsync());
break;
case "GET_CAST_STATUS":
return node.client.getStatusAsync();
break;
case "MUTE":
return node.client.setVolumeAsync({ muted: true })
.then(volume => node.client.getStatusAsync());
break;
case "UNMUTE":
return node.client.setVolumeAsync({ muted: false })
.then(volume => node.client.getStatusAsync());
break;
case "VOLUME":
if (command.volume && command.volume >= 0 && command.volume <= 100) {
return node.client.setVolumeAsync({ level: command.volume / 100 })
.then(volume => node.client.getStatusAsync());
} else {
throw new Error("Malformed command");
}
break;
default:
// If it got this far just error
throw new Error("Malformed command");
break;
}
};
}
RED.nodes.registerType("castv2-connection", CastV2ConnectionNode);
function CastV2SenderNode(config) {
RED.nodes.createNode(this, config);
// Settings
this.name = config.name;
this.connection = config.connection;
this.clientNode = RED.nodes.getNode(this.connection);
// Internal state
this.supportedApplications = [
DefaultMediaReceiver,
DashCastReceiver,
YouTubeReceiver
];
this.receiver = null;
this.adapter = null;
this.launching = false;
// Media control commands handled by any active receiver
this.mediaCommands = [
"GET_STATUS",
"PAUSE",
"PLAY",
"QUEUE_NEXT",
"QUEUE_PREV",
"SEEK",
"STOP"
];
let node = this;
/*
* Joins this node to the active receiver on the client connection
*/
this.join = function(activeSession, castV2App) {
// Ignore launches triggered by self launching in sendCommandAsync
if (node.launching) return;
// Only join if not already joined up
if (node.receiver == null || !(node.receiver instanceof castV2App)) {
node.clientNode.joinSessionAsync(activeSession, castV2App)
.then(receiver => node.initReceiver(receiver, castV2App));
}
};
/*
* Disconnects this node from the active receiver on the client connection
*/
this.unjoin = function() {
node.adapter = null;
if (node.receiver != null) {
// Ignore errors
try { node.receiver.close(); } catch (e) { }
node.receiver = null;
}
node.status({ fill: "green", shape: "ring", text: "connected" });
};
/*
* Initializes a receiver after launch or join
*/
this.initReceiver = function(receiver, castV2App) {
node.adapter = node.getAdapter(castV2App);
node.receiver = node.adapter.initReceiver(node, receiver);
node.receiver.on("status", function(status) {
if (status) {
node.send({ payload: status });
}
});
node.receiver.once("close", function() {
node.adapter = null;
node.receiver = null;
node.status({ fill: "green", shape: "ring", text: "connected" });
});
node.status({ fill: "green", shape: "dot", text: "joined" });
// Send initial receiver state
if (typeof node.receiver.getStatusAsync === "function") {
node.receiver.getStatusAsync()
.then(status => {
if (status) {
node.send({ payload: status });
}
});
}
};
/*
* Gets adapter for specified application
*/
this.getAdapter = function(castV2App) {
switch (castV2App.APP_ID) {
case DefaultMediaReceiver.APP_ID:
return DefaultMediaReceiverAdapter;
break;
case GenericMediaReceiver.APP_ID:
return GenericMediaReceiverAdapter;
break;
case DashCastReceiver.APP_ID:
return DashCastReceiverAdapter;
break;
case YouTubeReceiver.APP_ID:
return YouTubeReceiverAdapter;
break;
default:
return null;
break;
}
}
/*
* Gets application for command
*/
this.getCommandApp = function(command) {
switch (command.app) {
case "DefaultMediaReceiver":
return DefaultMediaReceiver;
break;
case "DashCast":
return DashCastReceiver;
break;
case "YouTube":
return YouTubeReceiver;
break;
default:
return null;
break;
}
}
/*
* General command handler
*/
this.sendCommandAsync = function(command) {
let isPlatformCommand = node.clientNode.platformCommands.includes(command.type);
let isMediaCommand = node.mediaCommands.includes(command.type);
if (isPlatformCommand) {
return node.clientNode.sendPlatformCommandAsync(command, node.receiver);
} else if (isMediaCommand) {
// If no active receiver, error
if (!node.receiver || !node.adapter) {
// Calling GET_STATUS without a receiver should just return null
if (command.type === "GET_STATUS") {
return Promise.resolve(null);
}
throw new Error("No active receiver application");
}
// Ensure the receiver supports media commands
if (!(node.receiver instanceof MediaReceiverBase)) {
throw new Error("Receiver does not support media commands");
}
return node.sendMediaCommandAsync(command);
} else {
// App specific command, determine app
let castV2App = node.getCommandApp(command);
// If no active receiver, launch and try again
if (!node.receiver || !node.adapter || node.receiver.APP_ID !== castV2App.APP_ID) {
node.launching = true;
return node.clientNode.launchAsync(castV2App)
.then(receiver => {
node.initReceiver(receiver, castV2App);
node.launching = false;
return node.adapter.sendAppCommandAsync(node.receiver, command);
})
.catch(error => {
// Ensure on failure we cleanup launching lock
node.launching = false;
throw error;
});
}
return node.adapter.sendAppCommandAsync(node.receiver, command);
}
};
/*
* Media command handler
*/
this.sendMediaCommandAsync = function(command) {
if (command.type === "GET_STATUS") {
return node.receiver.getStatusAsync();
} else {
// Initialize media controller by calling getStatus first
return node.receiver.getStatusAsync()
.then(status => {
// Theres not actually anything playing, exit gracefully
if (!status) throw new Error("not playing");
/*
* Execute media control command
* status.supportedMediaCommands bitmask
* 1 Pause
* 2 Seek
* 4 Stream volume
* 8 Stream mute
* 16 Skip forward
* 32 Skip backward
* 64 Queue Next
* 128 Queue Prev
* 256 Queue Shuffle
* 1024 Queue Repeat All
* 2048 Queue Repeat One
* 3072 Queue Repeat
*/
switch (command.type) {
case "PAUSE":
if (status.supportedMediaCommands & 1) {
return node.receiver.pauseAsync();
}
break;
case "PLAY":
return node.receiver.playAsync();
break;
case "SEEK":
if (status.supportedMediaCommands & 2 && command.time) {
return node.receiver.seekAsync(command.time);
}
break;
case "STOP":
return node.receiver.stopAsync();
break;
case "QUEUE_NEXT":
// bypass check as workaround of a google issue that does not add
// the QUEUE_NEXT bit in supportedMediaCommands for DefaultMediaReceiver
// https://issuetracker.google.com/issues/139939455
let bypassCheckNext =
node.receiver.session.appId === DefaultMediaReceiver.APP_ID
&& status.items.length > 1;
if (bypassCheckNext || status.supportedMediaCommands & 64) {
return node.receiver.queueNextAsync();
}
break;
case "QUEUE_PREV":
// bypass check as workaround of a google issue that does not add
// the QUEUE_PREV bit in supportedMediaCommands for DefaultMediaReceiver
// https://issuetracker.google.com/issues/139939455
let bypassCheckPrev =
node.receiver.session.appId === DefaultMediaReceiver.APP_ID
&& status.items.length > 1;
if (bypassCheckPrev || status.supportedMediaCommands & 128) {
return node.receiver.queuePrevAsync();
}
break;
default:
throw new Error("Malformed media control command");
break;
}
});
}
};
if (node.clientNode) {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
node.clientNode.register(node);
if (node.clientNode.connected) {
node.status({ fill: "green", shape: "ring", text: "connected" });
}
/*
* Node-red input handler
*/
this.on("input", function(msg, send, done) {
// For maximum backwards compatibility, check that send exists.
// If this node is installed in Node-RED 0.x, it will need to
// fallback to using `node.send`
send = send || function() { node.send.apply(node, arguments); };
// Reset the node status
if (node.receiver != null && node.adapter != null) {
node.status({ fill: "green", shape: "dot", text: "joined" });
} else if (node.clientNode.connected) {
node.status({ fill: "green", shape: "ring", text: "connected" });
} else {
node.status({ fill: "red", shape: "ring", text: "disconnected" });
}
const errorHandler = function(error) {
node.status({ fill: "red", shape: "ring", text: "error" });
if (done) {
done(error);
} else {
node.error(error, error.message);
}
};
try {
// Validate incoming message
if (msg.payload == null || typeof msg.payload !== "object") {
msg.payload = { type: "GET_CAST_STATUS" };
}
if (msg.payload.app == null) {
msg.payload.app = "DefaultMediaReceiver";
}
node.sendCommandAsync(msg.payload)
.then(status => {
// Handle solicited messages
status = status || null;
if (msg.payload.type === "GET_CAST_STATUS" || msg.payload.type === "GET_VOLUME") {
node.send({ platform: status });
} else if (msg.payload.type === "GET_STATUS") {
node.send({ payload: status });
}
if (done) done();
})
.catch(error => errorHandler(error));
} catch (exception) { errorHandler(exception); }
});
/*
* Node-red close handler
*/
this.on('close', function(done) {
try {
if (node.clientNode) {
node.clientNode.deregister(node, function() {
node.adapter = null;
if (node.receiver != null) {
// Ignore errors
try { node.receiver.close(); } catch (e) { }
node.receiver = null;
}
node.launching = false;
done();
});
} else {
done();
}
} catch(error) {
// swallow any errors here
done();
}
});
} else {
node.status({ fill: "red", shape: "ring", text: "unconfigured" });
}
}
RED.nodes.registerType("castv2-sender", CastV2SenderNode);
/*
* Expose discover endpoint for connection targets
*/
RED.httpAdmin.get('/googleCastDevices', (req, res) => {
discoverCastTargetsAsync()
.then(castTargets => {
res.json(castTargets);
})
.catch(error => res.send(500));
});
/*
* Discover cast targets
*/
function discoverCastTargetsAsync() {
return new Promise((resolve, reject) => {
try {
const bonjour = require('bonjour')();
const castTargets = [];
const bonjourBrowser = bonjour.find(
{ type: 'googlecast' },
service => {
castTargets.push({
name: service.txt.fn,
address: service.addresses.find(address => net.isIPv4(address)),
port: service.port
});
});
// await responses
setTimeout(() => {
try {
bonjourBrowser.stop();
bonjour.destroy();
resolve(castTargets);
} catch (error) {
reject(error);
}
}, 3000);
} catch (error) {
reject(error);
}
});
};
}