generated from bitfocus/companion-module-template-js
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
305 lines (275 loc) · 7.24 KB
/
index.js
File metadata and controls
305 lines (275 loc) · 7.24 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
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
import { InstanceBase, InstanceStatus, runEntrypoint } from '@companion-module/base'
import { getActions } from './actions.js'
import { getPresets } from './presets.js'
import { getVariables, updateVariables } from './variables.js'
import { getFeedbacks } from './feedbacks.js'
import { upgradeScripts } from './upgrades.js'
import WebSocket from 'ws'
class VDONinjaInstance extends InstanceBase {
constructor(internal) {
super(internal)
this.updateVariables = updateVariables
}
async init(config) {
this.updateStatus(InstanceStatus.Connecting)
this.config = config
this.connected = null
this.states = {}
this.streams = []
this.initWebSocket()
this.initActions()
this.initFeedbacks()
this.initVariables()
}
async destroy() {
this.states = {}
this.streams = []
if (this.ws !== undefined) {
this.ws.close(1000)
delete this.ws
}
if (this.reconnect) {
clearInterval(this.reconnect)
}
}
getConfigFields() {
return [
{
type: 'textinput',
id: 'apiID',
label: 'API ID',
width: 6,
},
{
type: 'checkbox',
id: 'wsCustom',
label: 'Custom Websockets Server (Advanced)',
default: false,
width: 6,
},
{
type: 'textinput',
id: 'wsServer',
label: 'Custom Websockets Server Domain',
default: 'api.vdo.ninja',
tooltip:
'Only use this if you are self-hosting your own websockets server. Only include the domain, not "wss://" or port',
width: 6,
isVisible: (options) => options.wsCustom,
},
]
}
async configUpdated(config) {
this.config = config
if (this.reconnect) {
clearInterval(this.reconnect)
}
this.initWebSocket()
}
initVariables() {
const variables = getVariables.bind(this)()
this.setVariableDefinitions(variables)
}
initFeedbacks() {
const feedbacks = getFeedbacks.bind(this)()
this.setFeedbackDefinitions(feedbacks)
}
initPresets() {
const presets = getPresets.bind(this)()
this.setPresetDefinitions(presets)
}
initActions() {
const actions = getActions.bind(this)()
this.setActionDefinitions(actions)
}
initWebSocket() {
if (this.reconnect) {
clearInterval(this.reconnect)
}
if (this.config.apiID) {
if (this.ws !== undefined) {
this.ws.close(1000)
delete this.ws
}
let serverUrl = 'api.vdo.ninja'
if (this.config.wsCustom && this.config.wsServer) {
serverUrl = this.config.wsServer
}
this.ws = new WebSocket(`wss://${serverUrl}:443`)
this.ws.on('open', () => {
if (!this.connected) {
this.log('info', `Connection opened to VDO.Ninja`)
this.connected = true
}
this.updateStatus(InstanceStatus.Ok)
this.ws.send(`{"join": "${this.config.apiID}" }`)
this.ws.send(`{"action": "getDetails"}`)
})
this.ws.on('close', (code) => {
if (code !== 1000 && code !== 1006) {
this.connected = false
this.log('debug', `Websocket closed: ${code}`)
}
this.reconnect = setInterval(() => {
this.initWebSocket()
}, 1000)
})
this.ws.on('message', this.messageReceivedFromWebSocket.bind(this))
this.ws.on('error', (data) => {
if (this.connected !== false) {
this.connected = false
this.updateStatus(InstanceStatus.ConnectionFailure)
if (data?.code == 'ENOTFOUND') {
this.log('error', `Unable to reach ${serverUrl}`)
} else {
this.log('error', `WebSocket ${data}`)
}
}
if (this.ws !== undefined) {
this.ws.close()
}
})
} else {
this.log('warn', `API ID required to connect to VDO.Ninja, please add one in the module settings`)
this.updateStatus(InstanceStatus.BadConfig, 'Missing API ID')
}
}
messageReceivedFromWebSocket(data) {
let message = JSON.parse(data)
if (message?.callback) {
let callback = message.callback
let result = callback.result
if (callback.action == 'getDetails') {
for (let x in result) {
let data = result[x]
this.processGetDetails(data)
}
} else if (callback.local) {
for (let x in this.states) {
if (this.states[x].localStream) {
let result = callback.result
if (callback.value !== 'toggle') {
this.processUpdate({ streamID: this.states[x].streamID, action: callback.action, value: result })
}
}
}
} else {
//console.log(callback)
}
} else if (message?.update) {
let data = message.update
this.processUpdate(data)
}
}
processGetDetails(data) {
this.states[data.streamID] = data
let label = data.streamID
let name = data.label ? `(${data.label})` : ''
if (data.director) {
label = 'Director'
} else if (data.position) {
label = `Guest ${data.position} ${name}`
} else if (data.label) {
label = data.label
}
if (data.localStream) {
label = `${label} (Local)`
}
let streamObject = { id: data.streamID, label: label }
if (this.streams.find((o) => o.id === data.streamID)) {
let index = this.streams.findIndex((o) => {
return o.id === data.streamID
})
this.streams.splice(index, 1, streamObject)
this.initFeedbacks()
this.initVariables()
this.initPresets()
} else {
this.streams.push(streamObject)
this.initFeedbacks()
this.initVariables()
this.updateVariables()
this.initPresets()
}
this.checkFeedbacks()
this.updateVariables()
}
processUpdate(data) {
if (this.states[data.streamID]) {
let action = data.action
switch (action) {
case 'hangup':
{
delete this.states[data.streamID]
let hangup = this.streams.findIndex((o) => {
return o.id === data.streamID
})
this.streams.splice(hangup, 1)
this.initActions()
this.initVariables()
this.initFeedbacks()
}
break
case 'newViewConnection':
this.ws.send(`{"action": "getDetails"}`)
break
case 'director':
this.states[data.streamID][data.action] = data.value
this.initActions()
this.initVariables()
this.initFeedbacks()
break
case 'endViewConnection':
{
delete this.states[data.value]
let endView = this.streams.findIndex((o) => {
return o.id === data.value
})
this.streams.splice(endView, 1)
this.initActions()
this.initVariables()
this.initFeedbacks()
}
break
case 'positionChange':
this.ws.send(`{"action": "getDetails"}`)
break
case 'directorMuted':
this.states[data.streamID].others['mute-guest'] = data.value ? '1' : '0'
break
case 'directorVideoHide':
this.states[data.streamID].others['hide-guest'] = data.value ? '1' : '0'
break
case 'remoteMuted':
case 'mic':
this.states[data.streamID].muted = data.value
break
case 'remoteVideoMuted':
case 'camera':
this.states[data.streamID].videoMuted = data.value
break
case 'details':
this.ws.send(`{"action": "getDetails"}`)
break
default:
this.states[data.streamID][data.action] = data.value
break
}
this.updateVariables()
this.checkFeedbacks()
}
if (data.action === 'seeding' || (data.action === 'tracksAdded' && data.value)) {
this.ws.send(`{"action": "getDetails"}`)
}
}
sendRequest(action, target, value, local) {
let object = {
action: action ?? 'null',
target: target ?? 'null',
value: value ?? 'null',
local: local ?? false,
}
this.ws.send(JSON.stringify(object))
}
}
runEntrypoint(VDONinjaInstance, upgradeScripts)