-
Notifications
You must be signed in to change notification settings - Fork 0
/
video-matrix-macro.js
377 lines (311 loc) · 10.8 KB
/
video-matrix-macro.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
/********************************************************
*
* Macro Author: William Mills
* Technical Solutions Specialist
* Cisco Systems
*
* Version: 1-0-0
* Released: 10/14/24
*
* This is an example macro which automatically applies a video
* matrix on specific display outputs from a Cisco Codec.
*
*
* Full Readme, source code and license details for this macro are available
* on Github: https://github.com/wxsd-sales/video-matrix-macro
*
********************************************************/
import xapi from 'xapi';
/*********************************************************
* Configure the settings below
**********************************************************/
const config = {
button: {
name: 'Display Controls',
color: '#f58142',
icon: 'Sliders'
},
layouts: [
{
id: 1,
output: 2,
inputs: [2, 2, 2, 2],
layout: 'equal',
name: 'Four Equal'
},
{
id: 2,
output: 2,
inputs: [2, 2],
layout: 'equal',
name: 'Two Equal'
},
{
id: 3,
output: 1,
inputs: [2, 2],
layout: 'prominent',
name: 'Two Prominent'
},
{
id: 4,
output: 1,
inputs: [2, 2, 2],
layout: 'equal',
name: 'Three Equal'
}
],
defaultLayouts: [1, 3],
showPanel: true,
panelId: 'videoMatrixAutoLayout'
}
/*********************************************************
* Do not change below
**********************************************************/
let videoMatrix = {};
let callId = null;
// Update Panel as Display Connection state changes
xapi.Status.Video.Output.Connector.Connected.on(async () => {
await createPanel();
syncUI();
})
xapi.Status.Call.on(({ ghost, AnswerState, id }) => {
if (AnswerState && AnswerState == 'Answered' && callId != id) return processCallStart(id);
if (ghost && callId) return processCallEnd(id);
})
xapi.Event.UserInterface.Extensions.Widget.Action.on(async ({ Type, WidgetId, Value }) => {
console.log('Widget Action, type', Type, 'widgetid', WidgetId, 'value', Value)
if (!WidgetId.startsWith(config.panelId)) return
const [_panelId, option] = WidgetId.split('-');
if (Type == 'pressed' && option == 'resetAll') return resetAllMatix()
const call = await xapi.Status.Call.get();
const inCall = call?.[0]?.Status == 'Connected';
if (!inCall) return
if (Type == 'released' && option == 'layout') return applyMatix(Value)
if (Type == 'changed' && option == 'autoApply') return applyMatix(config.layout[Value])
});
init();
async function init() {
// Identify Video Outputs
const outputs = await xapi.Status.Video.Output.Connector.get();
// Initialize videoMatrix object to track assign/resets
outputs.forEach(output => videoMatrix[output.id] = null)
console.log('Video Out', videoMatrix)
if (config.showPanel) {
await createPanel();
return syncUI()
} else {
deletePanel();
}
}
async function processCallStart(id) {
console.log('Processing Call Start - CallId:', id);
callId = id;
// Make sure the default layout string is valid
const defaultLayouts = config?.defaultLayouts;
if (!defaultLayouts) return
if (defaultLayouts.length == 0) return
const validDefaults = defaultLayouts.length > 1;
const selectedControlWidget = await getWidgetValue(`${config.panelId}-newCallBehaviour`);
console.log('Currently Selected Behaviour:', selectedControlWidget)
if (selectedControlWidget == 'defaults' && validDefaults) {
console.log('Applying Default Layouts -', defaultLayouts)
const matchedLayouts = config.layouts.filter(layout => defaultLayouts.includes(layout.id))
matchedLayouts.forEach( layout => {
applyMatix(layout.id);
syncUI();
})
return
}
if (selectedControlWidget == 'selected') {
console.log('Applying Currently Selected')
const connected = await getConnectedVideoOutputs();
for (let i = 0; i < connected.length; i++) {
const selected = await getWidgetValue(`${config.panelId}-layout-${connected[i]}`)
console.log('Display', connected[i], 'selected layout', selected)
if (selected != '') applyMatix(selected)
}
return
}
if (selectedControlWidget == 'reset') {
console.log('Restting Layouts')
resetAllMatix();
syncUI();
return
}
}
async function processCallEnd() {
console.log('Processing Call End');
callId = null;
resetAllMatix();
}
async function applyMatix(layoutId) {
const matchedLayout = config.layouts.find(layout => layoutId == layout.id)
const { inputs, output, layout, id } = matchedLayout;
if (videoMatrix[output] == id) return resetMatix(output)
const outputStatus = await xapi.Status.Video.Output.Connector[output].Connected.get()
console.log('Video Output:', output, ' Connected:', outputStatus)
if(outputStatus != 'True') return
console.log('Applying Video Matrix - Inputs:', inputs, 'Outputs:', output, 'Layout:', layout)
videoMatrix[output] = id;
xapi.Command.Video.Matrix.Assign({ SourceId: inputs, Output: output, Layout: layout })
}
function resetAllMatix() {
for (const [key, value] of Object.entries(videoMatrix)) {
if (value != null) resetMatix(key)
}
}
function resetMatix(output) {
console.log('Resetting Video Matrix - Output:', output)
videoMatrix[output] = null;
xapi.Command.Video.Matrix.Reset({ Output: output })
if (!config.showPanel) return
console.log('Resetting Widget', `${config.panelId}-layout-${output}`)
xapi.Command.UserInterface.Extensions.Widget.UnsetValue({ WidgetId: `${config.panelId}-layout-${output}` });
}
async function getConnectedVideoOutputs() {
const outputs = await xapi.Status.Video.Output.Connector.get();
const connected = outputs.filter(output => output.Connected == 'True')
return connected.map(output => parseInt(output.id))
}
async function getWidgetValue(widgetId) {
const widgets = await xapi.Status.UserInterface.Extensions.Widget.get();
const matchedWidget = widgets.find(widget => widget.WidgetId == widgetId);
if (!matchedWidget) return
return matchedWidget.Value;
}
async function syncUI() {
const defaultLayouts = config?.defaultLayouts;
if (!defaultLayouts) return
if (defaultLayouts.length == 0) return
const connected = await getConnectedVideoOutputs();
defaultLayouts.forEach(layoutId => {
const matchedLayout = config.layouts.find(layout => layoutId == layout.id)
if (matchedLayout && connected.includes(matchedLayout.output)) {
xapi.Command.UserInterface.Extensions.Widget.SetValue({
WidgetId: `${config.panelId}-layout-${matchedLayout.output}`,
Value: layoutId
});
}
})
}
function createLayoutRow(layouts, output) {
console.log('Creating layout row', output)
const panelId = config.panelId;
const defaultLayouts = config?.defaultLayouts;
const filteredLayouts = layouts.filter(layout => layout.output == output);
if (filteredLayouts.length == 0) return
console.log('Default Layouts:', defaultLayouts)
const values = filteredLayouts.map((layout) => {
const name = defaultLayouts.find(id => id == layout.id) ? `⍟ ${layout.name}` : layout.name;
return `<Value><Key>${layout.id}</Key><Name>${name}</Name></Value>`
}).join('')
return `
<Row>
<Name>Display ${output}</Name>
<Widget>
<WidgetId>${panelId}-layout-${output}</WidgetId>
<Type>GroupButton</Type>
<Options>size=4;columns=4</Options>
<ValueSpace>
${values}
</ValueSpace>
</Widget>
</Row>`
}
function createCallBehaviourGroup(hasDefaults) {
const panelId = config.panelId;
const allowDefaults = (hasDefaults) ?
`<Value><Key>defaults</Key><Name>Apply Defaults</Name></Value>`
: '';
return `<Row>
<Name>New Call Behaviour</Name>
<Widget>
<WidgetId>${panelId}-newCallBehaviour</WidgetId>
<Type>GroupButton</Type>
<Options>size=4</Options>
<ValueSpace>
${allowDefaults}
<Value>
<Key>selected</Key>
<Name>Apply Selected</Name>
</Value>
<Value>
<Key>reset</Key>
<Name>Reset Displays</Name>
</Value>
</ValueSpace>
</Widget>
</Row>`
}
function createResetRow(hasDefaults) {
const panelId = config.panelId;
const defaultText = (hasDefaults) ?
`<Widget>
<WidgetId>${panelId}-defaultText</WidgetId>
<Name>⍟ = Default For Display</Name>
<Type>Text</Type>
<Options>size=2;fontSize=normal;align=center</Options>
</Widget>
`
: '';
return `<Row>
${defaultText}
<Widget>
<WidgetId>${panelId}-resetAll</WidgetId>
<Name>Reset All Outputs</Name>
<Type>Button</Type>
<Options>size=2</Options>
</Widget>
</Row>`
}
async function createPanel() {
const panelId = config.panelId;
const button = config.button;
const order = await panelOrder(panelId)
const outputs = await getConnectedVideoOutputs()
console.log('Creating panel with', outputs.length, 'displays')
const layoutRows = outputs.map(output => createLayoutRow(config.layouts, output)).join('')
const defaultLayouts = config?.defaultLayouts;
const hasDefaults = defaultLayouts.length > 0;
const callBehaviourRow = createCallBehaviourGroup(hasDefaults);
const resetRow = createResetRow(hasDefaults);
const panel = `
<Extensions>
<Panel>
<Origin>local</Origin>
<Location>HomeScreenAndCallControls</Location>
<Icon>${button.icon}</Icon>
<Color>${button.color}</Color>
<Name>${button.name}</Name>
${order}
<ActivityType>Custom</ActivityType>
<Page>
<Name>${button.name}</Name>
${layoutRows}
${callBehaviourRow}
${resetRow}
<PageId>${panelId}-main</PageId>
<Options>hideRowNames=0</Options>
</Page>
</Panel>
</Extensions>`;
return xapi.Command.UserInterface.Extensions.Panel.Save({ PanelId: panelId }, panel);
}
async function panelOrder(panelId) {
const list = await xapi.Command.UserInterface.Extensions.List({ ActivityType: "Custom" });
const panels = list?.Extensions?.Panel
if (!panels) return ''
const existingPanel = panels.find(panel => panel.PanelId == panelId)
if (!existingPanel) return ''
return `<Order>${existingPanel.Order}</Order>`
}
async function deletePanel() {
const panelId = config.panelId;
const list = await xapi.Command.UserInterface.Extensions.List({ ActivityType: "Custom" });
const panels = list?.Extensions?.Panel
if (!panels) return
const existingPanel = panels.find(panel => panel.PanelId == panelId)
if (existingPanel) xapi.Command.UserInterface.Extensions.Panel.Remove({ PanelId: panelId });
}