forked from bitfocus/companion-module-obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
904 lines (820 loc) · 20.6 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
var instance_skel = require('../../instance_skel');
var tcp = require("../../tcp");
const OBSWebSocket = require('obs-websocket-js');
var debug;
var log;
function instance(system, id, config) {
var self = this;
// super-constructor
instance_skel.apply(this, arguments);
self.actions();
return self;
}
instance.prototype.updateConfig = function(config) {
var self = this;
self.config = config;
self.log('debug','updateConfig() destroying and reiniting..');
self.obs.disconnect();
self.init();
};
instance.prototype.init = function() {
var self = this;
self.disable = false;
self.status(self.STATUS_WARN, "Connecting");
if (self.obs !== undefined) {
self.obs.disconnect();
self.obs = undefined;
}
// Connecting on init not neccesary for OBSWebSocket. But during init try to tcp connect
// to get the status of the module right and automatically try reconnecting. Which is
// implemented in ../../tcp by Companion core developers.
self.tcp = new tcp((self.config.host !== '' ? self.config.host : '127.0.0.1'), (self.config.port !== '' ? self.config.port : '4444'));
self.tcp.on('status_change', function (status, message) {
self.status(status, message);
});
self.tcp.on('error', function () {
// Ignore
});
self.tcp.on('connect', function () {
// disconnect immediately because further comm takes place via OBSWebSocket and not
// via this tcp sockets.
self.tcp.destroy();
delete self.tcp;
// original init procedure continuing. Use OBSWebSocket to make the real connection.
self.obs = new OBSWebSocket();
self.states = {};
self.scenes = {};
self.transitions = {};
self.obs.connect({
address: (self.config.host !== '' ? self.config.host : '127.0.0.1') + ':' + (self.config.port !== '' ? self.config.port : '4444'),
password: self.config.pass
}).then(() => {
self.status(self.STATUS_OK);
self.log('info','Success! Connected to OBS.');
self.getStreamStatus();
self.updateScenes();
self.updateSources();
self.updateInfo();
}).catch(err => {
self.status(self.STATUS_ERROR, err);
});
self.obs.on('error', err => {
self.log('debug','Error received: ' + err);
self.status(self.STATUS_ERROR, err);
});
self.obs.on('ConnectionClosed', function() {
if (self.disable != true) {
self.log('error','Connection lost to OBS.');
self.status(self.STATUS_ERROR);
self.init();
} else {
}
});
self.obs.on('SwitchScenes', function(data) {
self.states['scene_active'] = data['scene-name'];
self.setVariable('scene_active', data['scene-name']);
self.checkFeedbacks('scene_active');
self.updateSources();
});
self.obs.on('PreviewSceneChanged', function(data) {
self.states['scene_preview'] = data['scene-name'];
self.setVariable('scene_preview', data['scene-name']);
self.checkFeedbacks('scene_active');
});
self.obs.on('ScenesChanged', function() {
self.updateScenes();
});
self.obs.on('SceneItemAdded', function() {
self.updateSources();
});
self.obs.on('SourceDestroyed', function() {
self.updateSources();
});
self.obs.on('StreamStarted', function(data) {
self.process_stream_vars(data);
self.checkFeedbacks('streaming');
});
self.obs.on('StreamStopped', function() {
self.setVariable('streaming', false);
self.states['streaming'] = false;
self.checkFeedbacks('streaming');
});
self.obs.on('StreamStatus', function(data) {
self.process_stream_vars(data);
});
self.obs.on('RecordingStarted', function() {
self.setVariable('recording', true);
self.states['recording'] = true;
self.checkFeedbacks('recording');
});
self.obs.on('RecordingStopped', function() {
self.setVariable('recording', false);
self.states['recording'] = false;
self.checkFeedbacks('recording');
});
self.obs.on('StudioModeSwitched', function(data) {
if (data['new-state'] == true) {
self.states['studio_mode'] = true;
} else {
self.states['studio_mode'] = false;
}
});
self.obs.on('SceneItemVisibilityChanged', function(data) {
if (self.states['studio_mode'] == true) {
//Item in preview, no change
} else {
if ((data['item-visible'] == true) && (data['scene-name'] == self.states['scene_active'])) {
self.states[data['item-name']] = true;
//self.setVariable('item_visible', data['item-name']);
} else {
self.states[data['item-name']] = false;
//self.setVariable('item_visible', 'xxxxx');
}
}
self.checkFeedbacks('scene_item_active');
});
});
debug = self.debug;
log = self.log;
};
instance.prototype.process_stream_vars = function(data) {
var self = this;
for (var s in data) {
self.states[s] = data[s];
}
self.setVariable('bytes_per_sec', data['bytes-per-sec']);
self.setVariable('fps', data['fps']);
self.setVariable('kbits_per_sec', data['kbits-per-sec']);
self.setVariable('num_dropped_frames', data['num-dropped-frames']);
self.setVariable('num_total_frames', data['num-total-frames']);
self.setVariable('preview_only', data['preview-only']);
self.setVariable('recording', data['recording']);
self.setVariable('strain', data['strain']);
self.setVariable('stream_timecode', data['stream-timecode']);
self.setVariable('streaming', data['streaming']);
self.setVariable('total_stream_time', data['total-stream-time']);
self.checkFeedbacks('streaming');
};
// Return config fields for web config
instance.prototype.config_fields = function() {
var self = this;
return [
{
type: 'textinput',
id: 'host',
label: 'Target IP',
width: 8,
regex: self.REGEX_IP
},
{
type: 'textinput',
id: 'port',
label: 'Target Port',
width: 4,
default: 4449,
regex: self.REGEX_PORT
},
{
type: 'textinput',
id: 'pass',
label: 'Password',
width: 4,
}
]
};
instance.prototype.getStreamStatus = function() {
var self = this;
self.obs.send('GetStreamingStatus').then(data => {
self.setVariable('recording', data['recording']);
self.states['recording'] = data['recording'];
self.checkFeedbacks('recording');
self.setVariable('streaming', data['streaming']);
self.states['streaming'] = data['streaming'];
self.checkFeedbacks('streaming');
self.actions();
self.init_presets();
self.init_feedbacks();
self.init_variables();
});
};
instance.prototype.updateScenes = function() {
var self = this;
self.obs.send('GetTransitionList').then(data => {
self.transitions = {};
self.states['current_transition'] = data['current-transition'];
for (var s in data.transitions) {
var transition = data.transitions[s];
self.transitions[transition.name] = transition;
}
self.actions();
self.init_presets();
self.init_feedbacks();
self.init_variables();
});
self.obs.send('GetSceneList').then(data => {
self.scenes = {};
self.states['scene_active'] = data['current-scene'];
for (var s in data.scenes) {
var scene = data.scenes[s];
self.scenes[scene.name] = scene;
}
self.actions();
self.init_presets();
self.init_feedbacks();
self.init_variables();
});
};
instance.prototype.updateSources = function() {
var self = this;
self.obs.send('GetSourcesList').then(data => {
self.sources = {};
for (var s in data.sources) {
var source = data.sources[s];
self.sources[source.name] = source;
}
self.actions();
self.init_feedbacks();
data.sources.forEach(source => {
self.states[source.name] = false;
});
});
self.obs.send('GetCurrentScene').then(data => {
data.sources.forEach(name => {
self.obs.send('GetSceneItemProperties', {item: name}).then(data => {
if (data['visible'] == true) {
self.states[data['name']] = true;
} else {
self.states[data['name']] = false;
}
self.checkFeedbacks('scene_item_active');
});
});
});
};
instance.prototype.updateInfo = function() {
var self = this;
self.obs.send('GetStudioModeStatus').then(data => {
if (data['studio-mode'] == true) {
self.states['studio_mode'] = true;
} else {
self.states['studio_mode'] = false;
}
});
};
// When module gets deleted
instance.prototype.destroy = function() {
var self = this;
self.scenes = [];
self.transitions = [];
self.states = {};
self.scenelist = [];
self.sourcelist = [];
self.feedbacks = {};
if (self.obs !== undefined) {
self.obs.disconnect();
}
if (self.tcp !== undefined) {
self.tcp.destroy();
}
self.disable = true;
};
instance.prototype.actions = function() {
var self = this;
self.scenelist = [];
self.sourcelist = [];
self.transitionlist = [];
var s;
if (self.scenes !== undefined) {
for (s in self.scenes) {
self.scenelist.push({ id: s, label: s });
}
}
if (self.sources !== undefined) {
for (s in self.sources) {
self.sourcelist.push({ id: s, label: s });
}
}
if (self.transitions !== undefined) {
for (s in self.transitions) {
self.transitionlist.push({ id: s, label: s });
}
}
self.setActions({
'set_scene': {
label: 'Change scene',
options: [
{
type: 'dropdown',
label: 'Scene',
id: 'scene',
default: '0',
choices: self.scenelist
}
]
},
'preview_scene': {
label: 'Preview scene',
options: [
{
type: 'dropdown',
label: 'Scene',
id: 'scene',
default: '0',
choices: self.scenelist
}
]
},
'do_transition': {
label: 'Transition preview to program',
options: [
{
type: 'dropdown',
label: 'Transition to use',
id: 'transition',
default: null,
choices: self.transitionlist,
required: false
},
{
type: 'number',
label: 'Transition time (in ms)',
id: 'transition_time',
default: null,
min: 0,
max: 60 * 1000, //max is required by api
range: false,
required: false
}
]
},
'set_transition': {
label: 'Change transition type',
options: [
{
type: 'dropdown',
label: 'Transitions',
id: 'transitions',
default: '0',
choices: self.transitionlist
}
]
},
'StartStopStreaming': {
label: 'Start and Stop Streaming'
},
'StartStopRecording': {
label: 'Start and Stop Recording'
},
'set_source_mute' : {
label: 'Set Source Mute',
options: [
{
type: 'textinput',
label: 'Source',
id: 'source',
default: '',
},
{
type: 'dropdown',
label: 'Mute',
id: 'mute',
default: 'true',
choices: [ { id: 'false', label: 'False' }, { id: 'true', label: 'True' } ]
}
]
},
'toggle_source_mute' : {
label: 'Toggle Source Mute',
options: [
{
type: 'textinput',
label: 'Source',
id: 'source',
default: '',
}
]
},
'toggle_scene_item' : {
label: 'Toggle visibility scene item',
options: [
{
type: 'textinput',
label: 'Scene (optional, defaults to current scene)',
id: 'scene'
},
{
type: 'textinput',
label: 'Source',
id: 'source',
default: '',
},
{
type: 'dropdown',
label: 'Visible',
id: 'visible',
default: 'true',
choices: [ { id: 'false', label: 'False' }, { id: 'true', label: 'True' } ]
}
]
},
'reconnect' : {
label: 'reconnect to OBS'
},
'set-freetype-text': {
label: 'Set Source Text (FreeType 2)',
options: [
{
type: 'textinput',
label: 'Source Name',
id: 'source',
required: true
},
{
type: 'textinput',
label: 'Text',
id: 'text',
required: true
}
]
},
'set-gdi-text': {
label: 'Set Source Text (GDI+)',
options: [
{
type: 'textinput',
label: 'Source Name',
id: 'source',
required: true
},
{
type: 'textinput',
label: 'Text',
id: 'text',
required: true
}
]
}
});
};
instance.prototype.action = function(action) {
var self = this;
var handle;
if (action.action == 'reconnect') {
self.log('debug','reconnecting, destroying and reiniting..');
self.obs.disconnect();
self.init();
return;
}
if (self.obs == null || self.obs.OBSWebSocket) {
self.log('warn', 'OBS action not possible, connection lost');
return;
}
switch (action.action) {
case 'set_scene':
handle = self.obs.send('SetCurrentScene', {
'scene-name': action.options.scene
})
break;
case 'preview_scene':
handle = self.obs.send('SetPreviewScene', {
'scene-name': action.options.scene
});
break;
case 'do_transition':
var options = {};
if (action.options && action.options.transition) {
options['with-transition'] = {
name: action.options.transition
};
if (action.options.transition_time > 0) {
options['with-transition']['duration'] = action.options.transition_time;
}
}
handle = self.obs.send('TransitionToProgram', options);
break;
case 'set_source_mute':
handle = self.obs.send('SetMute', {
'source': action.options.source,
'mute': (action.options.mute == 'true' ? true : false)
});
break;
case 'toggle_source_mute':
handle = self.obs.send('ToggleMute', {
'source': action.options.source
});
break;
case 'set_transition':
handle = self.obs.send('SetCurrentTransition', {
'transition-name': action.options.transitions
});
break;
case 'StartStopStreaming':
handle = self.obs.send('StartStopStreaming');
break;
case 'StartStopRecording':
handle = self.obs.send('StartStopRecording');
break;
case 'toggle_scene_item':
handle = self.obs.send('SetSceneItemProperties', {
'item': action.options.source,
'visible': (action.options.visible == 'true' ? true : false),
'scene-name': action.options.scene && action.options.scene != "" ? action.options.scene : null
});
break;
case 'set-freetype-text':
handle = self.obs.send('SetTextFreetype2Properties', {
'source': action.options.source,
'text': action.options.text
})
break;
case 'set-gdi-text':
handle = self.obs.send('SetTextGDIPlusProperties', {
'source': action.options.source,
'text': action.options.text
})
break;
}
handle.catch(error => {
if (error.code == "NOT_CONNECTED") {
self.log('warn', 'Send to OBS failed. Re-start OBS manually. Starting re-init');
self.obs.disconnect();
self.init();
} else {
self.log('debug', error.error);
}
});
};
instance.prototype.init_feedbacks = function() {
var self = this;
// feedbacks
var feedbacks = {};
feedbacks['streaming'] = {
label: 'Stream is running',
description: 'If the stream is running, change colors of the bank',
options: [
{
type: 'colorpicker',
label: 'Foreground color',
id: 'fg',
default: self.rgb(255, 255, 255)
},
{
type: 'colorpicker',
label: 'Background color',
id: 'bg',
default: self.rgb(100, 255, 0)
},
]
};
feedbacks['recording'] = {
label: 'Recording is active',
description: 'If the recording is active, change colors of the bank',
options: [
{
type: 'colorpicker',
label: 'Foreground color',
id: 'fg',
default: self.rgb(255, 255, 255)
},
{
type: 'colorpicker',
label: 'Background color',
id: 'bg',
default: self.rgb(100, 255, 0)
},
]
};
feedbacks['scene_active'] = {
label: 'Change colors from active/previewed scene',
description: 'If the scene specified is active or previewed in OBS, change colors of the bank',
options: [
{
type: 'colorpicker',
label: 'Foreground color (program)',
id: 'fg',
default: self.rgb(255, 255, 255)
},
{
type: 'colorpicker',
label: 'Background color (program)',
id: 'bg',
default: self.rgb(255, 0, 0)
},
{
type: 'colorpicker',
label: 'Foreground color (preview)',
id: 'fg_preview',
default: self.rgb(255, 255, 255)
},
{
type: 'colorpicker',
label: 'Background color (preview)',
id: 'bg_preview',
default: self.rgb(0, 200, 0)
},
{
type: 'dropdown',
label: 'Scene',
id: 'scene',
default: '',
choices: self.scenelist
}
]
};
feedbacks['scene_item_active'] = {
label: 'Change colors when source visible',
description: 'If a source become visible or invisible in current scene, change color',
options: [
{
type: 'colorpicker',
label: 'Foreground color',
id: 'fg',
default: self.rgb(255, 255, 255)
},
{
type: 'colorpicker',
label: 'Background color',
id: 'bg',
default: self.rgb(255, 0, 0)
},
{
type: 'dropdown',
label: 'Source name',
id: 'source',
default: '',
choices: self.sourcelist
}
]
};
self.setFeedbackDefinitions(feedbacks);
};
instance.prototype.feedback = function(feedback) {
var self = this;
if (self.states === undefined) {
return;
}
if (feedback.type === 'scene_active') {
if (self.states['scene_active'] === feedback.options.scene) {
return { color: feedback.options.fg, bgcolor: feedback.options.bg };
} else if (self.states['scene_preview'] === feedback.options.scene && typeof feedback.options.fg_preview === 'number') {
//FIXME fg_preview/bg_preview is undefined when updating from an older version of the module
return { color: feedback.options.fg_preview, bgcolor: feedback.options.bg_preview };
}
}
if (feedback.type === 'streaming') {
if (self.states['streaming'] === true) {
return { color: feedback.options.fg, bgcolor: feedback.options.bg };
}
}
if (feedback.type === 'recording') {
if (self.states['recording'] === true) {
return { color: feedback.options.fg, bgcolor: feedback.options.bg };
}
}
if (feedback.type === 'scene_item_active') {
if ((self.states[feedback.options.source] === true)) {
return { color: feedback.options.fg, bgcolor: feedback.options.bg };
}
}
return {};
};
instance.prototype.init_presets = function() {
var self = this;
var presets = [];
for (var s in self.scenes) {
var scene = self.scenes[s];
let baseObj = {
category: 'Scene to program',
label: scene.name,
bank: {
style: 'text',
text: scene.name,
size: 'auto',
color: self.rgb(255, 255, 255),
bgcolor: 0
},
feedbacks: [
{
type: 'scene_active',
options: {
bg: self.rgb(255, 0, 0),
fg: self.rgb(255, 255, 255),
bg_preview: self.rgb(0, 200, 0),
fg_preview: self.rgb(255, 255, 255),
scene: scene.name,
}
}
],
actions: [
{
action: 'set_scene',
options: {
scene: scene.name
}
}
]
};
presets.push(baseObj);
let toPreview = {};
presets.push(Object.assign(toPreview, baseObj, {
category: 'Scene to preview',
actions: [
{
action: 'preview_scene',
options: {
scene: scene.name
}
}
]
}));
}
presets.push({
category: 'Transitions',
label: 'Send previewed scene to program',
bank: {
style: 'text',
text: 'AUTO',
size: 'auto',
color: self.rgb(255, 255, 255),
bgcolor: 0
},
actions: [
{
action: 'do_transition'
}
]
});
// Preset for Start Streaming button with colors indicating streaming status
presets.push({
category: 'Streaming',
label: 'OBS Streaming',
bank: {
style: 'text',
text: 'OBS STREAM',
size: 'auto',
color: self.rgb(255, 255, 255),
bgcolor: 0
},
feedbacks: [
{
type: 'streaming',
options: {
bg: self.rgb(51, 204, 51),
fg: self.rgb(255, 255, 255),
}
}
],
actions: [
{
action: 'StartStopStreaming',
}
]
});
// Preset for Start Recording button with colors indicating recording status
presets.push({
category: 'Recording',
label: 'OBS Recording',
bank: {
style: 'text',
text: 'OBS RECORD',
size: 'auto',
color: self.rgb(255, 255, 255),
bgcolor: 0
},
feedbacks: [
{
type: 'recording',
options: {
bg: self.rgb(51, 204, 51),
fg: self.rgb(255, 255, 255),
}
}
],
actions: [
{
action: 'StartStopRecording',
}
]
});
self.setPresetDefinitions(presets);
};
instance.prototype.init_variables = function() {
var self = this;
var variables = [];
variables.push({ name: 'bytes_per_sec', label: 'Stream is active' });
variables.push({ name: 'fps', label: 'Frames per second' });
variables.push({ name: 'kbits_per_sec', label: 'Kilobits per second' });
variables.push({ name: 'num_dropped_frames', label: 'Number of dropped frames' });
variables.push({ name: 'num_total_frames', label: 'Number of total frames' });
variables.push({ name: 'preview_only', label: 'Preview only' });
variables.push({ name: 'recording', label: 'Recording State' });
variables.push({ name: 'strain', label: 'Strain' });
variables.push({ name: 'stream_timecode', label: 'Stream Timecode' });
variables.push({ name: 'streaming', label: 'Streaming State' });
variables.push({ name: 'total_stream_time', label: 'Total streaming time' });
variables.push({ name: 'scene_active', label: 'Current active scene' });
variables.push({ name: 'scene_preview', label: 'Current preview scene' });
self.setVariableDefinitions(variables);
};
instance_skel.extendedBy(instance);
exports = module.exports = instance;