forked from bitfocus/companion-module-kiloview-ndi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
396 lines (357 loc) · 8.45 KB
/
index.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
const instance_skel = require('../../instance_skel')
const { initVariables } = require('./variables')
const { initFeedbacks } = require('./feedbacks')
const { initPresets } = require('./presets')
const kiloviewNDI = require('kiloview-ndi')
let debug = () => {}
const INTERVAL = 1000
/**
* Companion instance class for the kiloview ndi endpoint.
*
* @extends instance_skel
* @version 1.1.0
* @since 1.0.0
* @author Håkon Nessjøen <[email protected]>>
*/
class instance extends instance_skel {
counter = 0
sources = [{ id: 'null', url: '', label: '- No sources available -' }]
state = {
mode: 'N/A',
}
converterModes = [
{
id: 'encoder',
label: 'Encoder',
},
{
id: 'decoder',
label: 'Decoder',
},
]
/**
* Create an instance of a ndi module
*
* @param {EventEmitter} system - the brains of the operation
* @param {string} id - the instance ID
* @param {Object} config - saved user configuration parameters
* @since 1.0.0
*/
constructor(system, id, config) {
super(system, id, config)
this.actions() // export actions
}
/**
* Setup the actions.
*
* @param {EventEmitter} system - the brains of the operation
* @access public
* @since 1.0.0
*/
actions(system) {
const actions = {}
actions['modeSwitch'] = {
label: 'Switch mode',
options: [
{
type: 'dropdown',
label: 'Mode',
id: 'mode',
default: 'decoder',
choices: this.converterModes,
},
],
}
actions['setPreset'] = {
label: 'Activate preset',
options: [
{
type: 'number',
label: 'Number',
id: 'id',
default: 1,
min: 1,
max: 10,
},
],
}
actions['setURL'] = {
label: 'Set NDI source',
options: [
{
type: 'dropdown',
label: 'Source',
id: 'url',
choices: this.sources,
default: this.sources[0].id,
},
],
}
this.setActions(actions)
}
/**
* Executes the provided action.
*
* @param {Object} action - the action to be executed
* @access public
* @since 1.0.0
*/
async action(action) {
const opt = action.options
if (!this.converter) {
return
}
try {
switch (action.action) {
case 'modeSwitch':
this.setVariable('mode', 'N/A')
this.setVariable('online', 'N/A')
this.setVariable('video_signal', 'N/A')
this.state.online = undefined
this.state.mode = undefined
this.state.video_signal = undefined
await this.converter.modeSwitch(opt.mode)
this.checkFeedbacks()
break
case 'setPreset':
await this.converter.decoderCurrentSetPreset(opt.id)
break
case 'setURL':
const [name, ip, port] = Buffer.from(opt.url, 'base64').toString().split(/:/)
await this.converter.decoderCurrentSetUrl(name, ip + ':' + port)
break
}
} catch (e) {
this.log('error', 'Error running action ' + action.action + ': ' + e.message)
}
}
/**
* Creates the configuration fields for web config.
*
* @returns {Array} the config fields
* @access public
* @since 1.0.0
*/
config_fields() {
return [
{
type: 'text',
id: 'info',
width: 12,
label: 'Information',
value:
'This modules currently supports Kiloview N40 devices. Make sure to enable HTTP API access on the user you are using, this is default off!',
},
{
type: 'textinput',
label: 'Target IP',
id: 'address',
width: 6,
regex: this.REGEX_IP,
required: true,
},
{
type: 'textinput',
label: 'Username',
id: 'username',
width: 3,
default: 'admin',
required: true,
},
{
type: 'textinput',
label: 'Password',
id: 'password',
width: 3,
default: 'admin',
required: true,
},
{
type: 'checkbox',
label: 'Periodically fetch available NDI sources from device',
id: 'discovery',
width: 12,
default: false,
},
]
}
/**
* Clean up the instance before it is destroyed.
*
* @access public
* @since 1.0.0
*/
destroy() {
if (this.pollTimer !== undefined) {
clearInterval(this.pollTimer)
}
debug('destroy', this.id)
}
/**
* Main initialization function called once the module
* is OK to start doing things.
*
* @access public
* @since 1.0.0
*/
init() {
debug = this.debug
try {
this.status(this.STATUS_WARNING, 'Connecting')
this.initFeedbacks()
this.initPresets()
this.initVariables()
} catch (e) {
console.error(e)
}
this.initConnection()
}
/**
* INTERNAL: initialize feedbacks.
*
* @access protected
* @since 1.1.0
*/
initFeedbacks() {
const feedbacks = initFeedbacks.bind(this)()
this.setFeedbackDefinitions(feedbacks)
}
/**
* INTERNAL: initialize presets.
*
* @access protected
* @since 1.1.0
*/
initPresets() {
this.setPresetDefinitions(initPresets.bind(this)())
}
/**
* INTERNAL: initialize variables.
*
* @access protected
* @since 1.1.0
*/
initVariables() {
initVariables(this)
}
/**
* INTERNAL: initalize the ndi connection.
*
* @access protected
* @since 1.0.0
*/
initConnection() {
this.pollTimer = setInterval(() => this.checkState(), 2000)
this.updateConfig(this.config)
}
async checkState() {
if (!this.converter) {
return
}
try {
const mode = await this.converter.modeGet()
this.setVariable('mode', mode.mode)
this.state.mode = mode.mode
this.status(this.STATUS_OK, 'Connected')
this.checkFeedbacks('mode')
} catch (e) {
debug('Mode get error: %o', e.message)
this.status(this.STATUS_ERROR, 'Error connecting to device')
this.setVariable('mode', 'N/A')
this.state.mode = 'N/A'
return
}
try {
if (this.state.mode === 'decoder') {
const info = await this.converter.decoderCurrentStatus()
let bitrate = info.bitrate
if (bitrate < 1024) {
bitrate = bitrate + ' Kbps'
} else {
bitrate = Math.round(bitrate / 1024) + ' Mbps'
}
this.setVariable('resolution', info.resolution || 'N/A')
this.setVariable('bitrate', info.bitrate ? bitrate : 'N/A')
this.setVariable('streamname', info.name || 'N/A')
this.setVariable('online', info.online ? 'Yes' : 'No')
if (this.state.online !== info.online) {
this.state.online = info.online
}
this.checkFeedbacks('online')
this.state.resolution = info.resolution
this.state.bitrate = info.bitrate
this.state.streamname = info.name
} else if (this.state.mode === 'encoder') {
const info = await this.converter.encoderNdiStatus()
let bitrate = info.bitrate
if (bitrate < 1024) {
bitrate = bitrate + ' Kbps'
} else {
bitrate = Math.round(bitrate / 1024) + ' Mbps'
}
this.setVariable('resolution', info.resolution || 'N/A')
this.setVariable('bitrate', info.bitrate ? bitrate : 'N/A')
this.setVariable('video_signal', info.video_signal ? 'Yes' : 'No')
if (this.state.video_signal !== info.video_signal) {
this.state.video_signal = info.video_signal
this.checkFeedbacks('video_signal')
}
this.state.resolution = info.resolution
this.state.bitrate = info.bitrate
}
if (this.config.discovery && this.state.mode === 'decoder' && this.counter++ % 5 == 0) {
// every 10 seconds
const sources = await this.converter.decoderDiscoveryGet()
this.sources = []
if (sources && sources instanceof Array) {
sources.forEach((source) => {
this.sources.push({
id: Buffer.from(source.name + ':' + source.url).toString('base64'),
label: source.name,
})
if (source.children?.length) {
source.children.forEach((subsource) => {
this.sources.push({
id: Buffer.from(subsource.name + ':' + subsource.url).toString('base64'),
label: subsource.name,
})
})
}
})
} else {
this.sources = [{ id: 'null', url: '', label: '- No sources available -' }]
}
this.actions()
}
} catch (e) {
debug('Error with info: ' + e.message)
//
}
}
/**
* Process an updated configuration array.
*
* @param {Object} config - the new configuration
* @access public
* @since 1.0.0
*/
updateConfig(config) {
if (
this.config.address !== config.address ||
this.config.username !== config.username ||
this.config.password !== config.password
) {
this.status(this.STATUS_WARNING, 'Connecting')
}
this.config = config
if (config.address && config.username) {
this.converter = new kiloviewNDI(config.address, config.username, config.password)
} else {
this.converter = null
}
this.actions()
this.initFeedbacks()
this.initVariables()
}
}
exports = module.exports = instance