-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
183 lines (143 loc) · 4 KB
/
index.js
File metadata and controls
183 lines (143 loc) · 4 KB
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
const express = require('express');
const { CasparCG, ConnectionOptions, AMCP } = require("casparcg-connection");
const os = require("os");
const app = express();
app.use(express.static('public'))
app.set('view engine', 'pug')
const port = 3030;
const interfaces = os.networkInterfaces();
const addresses = [];
for (let k in interfaces) {
for (let addr of interfaces[k]) {
if (addr.family === 'IPv4' && !addr.internal && addr.address.indexOf("169.254") != 0) {
addresses.push(addr.address);
}
}
}
let info = {};
let decklinks = {};
const server = {
hostname: os.hostname(),
address: addresses.join(", "),
};
const connection = new CasparCG(new ConnectionOptions ({
autoReconnect: true,
autoReconnectInterval: 10000,
//debug: true,
onConnected: loadInfo,
}));
let loaded = 0;
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on("SIGINT", function () {
console.log("Terminating!");
for(let i of Object.keys(info)){
const ch = info[i];
connection.stop(ch.id, 1);
}
process.exit(0);
});
function showCard(){
if (++loaded != 2)
return;
console.log("Showing Template on channels!");
for(let i of Object.keys(info)){
const ch = info[i];
connection.playHtmlPage(ch.id, 1, "http://127.0.0.1:"+port+"/channel/"+ch.id);
}
console.log(" ");
console.log('Press Ctrl-C to clear');
}
function ensureArray(orig){
return Array.isArray(orig) ? orig : [orig];
}
function loadInfo(){
console.log("LOAD INFO");
info = {};
decklinks = {};
connection.infoServer().then(res => {
for (let data of ensureArray(res.response.data.channel)) {
const id = data.index;
if (info[id] === undefined)
info[id] = {};
info[id].id = data.index;
info[id].format = data['video-mode'];
if (data.output === undefined || data.output.consumers === undefined || data.output.consumers.consumer === undefined)
continue;
const consumers = [];
for(let cons of ensureArray(data.output.consumers.consumer)){
if (cons.type == "ogl-consumer")
consumers.push("Screen");
if (cons.type == "decklink-consumer")
consumers.push("decklink" + cons.device);
if (cons.type == "newtek-ivga-consumer")
consumers.push("Newteck NDI");
}
if (consumers.length > 0)
info[id].consumers = consumers;
}
showCard();
});
connection.infoSystem().then(res => {
const data = res.response.data;
if (data.caspar === undefined || data.caspar.decklink === undefined)
return showCard();
const decklink = data.caspar.decklink;
if (decklink === undefined || decklink.device === undefined)
return showCard();
const devices = ensureArray(decklink.device);
for(let id in devices){
decklinks[parseInt(id)+1] = devices[id];
}
showCard();
})
}
function compileChannelInfo(id){
const channelInfo = info[id];
if (channelInfo === undefined)
return null;
const consumers = [];
if (channelInfo.consumers !== undefined){
for(let cons of channelInfo.consumers){
if (cons.indexOf("decklink") != 0){
consumers.push(cons)
continue;
}
const id = cons.substring(8);
const decklink = decklinks[id];
if (decklink === undefined)
consumers.push("Decklink");
else
consumers.push(decklink);
}
}
if (consumers.length == 0)
consumers.push("none");
const channel = Object.assign({}, channelInfo);
return Object.assign(channel, { consumers: consumers.join(", ") });
}
app.get('/channel/:id', function (req, res) {
const id = req.params.id;
const channel = compileChannelInfo(id);
if (channel === null)
res.status(404).send('Not found');
res.render('index', { server, channel});
});
app.get('/', function (req, res) {
const allChannels = [];
for (let k of Object.keys(info)){
allChannels.push(compileChannelInfo(k));
}
res.render('info', { server, allChannels});
});
app.listen(port, function () {
console.log('WhoAmI app listening on port '+port+'!');
});
console.log('Press Ctrl-C to clear');