forked from stephenkeep/node-red-contrib-chromecast
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchromecast-play.js
157 lines (120 loc) · 3.29 KB
/
chromecast-play.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
var Client = require('castv2-client').Client;
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
function play(host, url, type, volume, node, msg) {
try {
var client = new Client();
client.connect(host, function () {
if (volume) {
console.log('connected, launching app on %s with url %s, type %s and volume %s', host, url, type, volume);
} else {
console.log('connected, launching app on %s with url %s and type %s', host, url, type);
}
client.launch(DefaultMediaReceiver, function (err, player) {
var media = {
// Here you can plug an URL to any mp4, webm, mp3 or jpg file with the proper contentType.
contentId: url,
contentType: type,
streamType: 'BUFFERED' // or LIVE
};
if (typeof volume !== 'undefined') {
if (volume > 0) {
var obj = {
level: volume / 100
};
} else {
var obj = {
muted: true
};
}
client.setVolume(obj, function (err, newvol) {
if (err) {
console.log("there was an error setting the volume")
node.error("Chromecast error: there was an error setting the volume");
msg.error = "there was an error setting the volume";
node.send(msg);
return node.status({
shape: "dot",
fill: "red",
text: "error: there was an error setting the volume"
});
}
console.log("volume changed to %s", Math.round(newvol.level * 100))
});
}
if (player) {
player.on('status', function (status) {
console.log('status broadcast playerState=%s', status.playerState);
});
player.load(media, {autoplay: true}, function (err, status) {
if (status) {
console.log('media loaded playerState=%s', status.playerState);
node.status({});
}
if (err) {
console.log('media loaded error= %s', err);
msg.error = " " + err;
node.send(msg);
node.error("Chromecast error: " + err);
return node.status({
shape: "dot",
fill: "red",
text: " " + err
});
}
client.close();
});
}
node.send(msg);
});
});
client.on('error', function (err) {
msg.error = " " + err;
node.send(msg);
node.error("Chromecast " + err);
client.close();
return node.status({
shape: "dot",
fill: "red",
text: " " + err
});
});
} catch (err) {
node.status({
shape: "dot",
fill: "red",
text: " " + err
});
node.error("Chromecast error: " + err);
}
}
module.exports = function (RED) {
'use strict';
function Node(n) {
RED.nodes.createNode(this, n);
var node = this;
this.on('input', function (msg) {
var creds = RED.nodes.getNode(n.creds),
payload = typeof msg.payload === 'object' ? msg.payload : {};
var attrs = ['ip', 'url', 'contentType', 'volume'];
for (var attr of attrs) {
if (n[attr]) {
payload[attr] = n[attr];
}
}
if (payload.ip && payload.url && payload.contentType) {
try {
play(payload.ip, payload.url, payload.contentType, payload.volume, node, msg);
} catch (err) {
node.status({
shape: "dot",
fill: "red",
text: " " + err
});
node.error("Chromecast error: " + err);
}
}
//node.send(msg);
});
}
RED.nodes.registerType('chromecast-play', Node);
};