-
Notifications
You must be signed in to change notification settings - Fork 6
/
obs-visca-control.lua
1622 lines (1409 loc) · 73.5 KB
/
obs-visca-control.lua
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local obs = obslua
local bit = require("bit")
local Visca = require("libvisca")
local plugin_info = {
name = "Visca Camera Control",
version = "2.6",
url = "https://github.com/vwout/obs-visca-control",
description = "Camera control via Visca over IP",
author = "vwout"
}
local plugin_def = {
id = "Visca_Control",
type = obs.OBS_SOURCE_TYPE_INPUT,
output_flags = bit.bor(obs.OBS_SOURCE_CUSTOM_DRAW),
}
local plugin_settings = {}
local plugin_data = {
debug = false,
active_scene = nil,
preview_scene = nil,
program_scene = {}, -- List containing typically 1 scene name that is on program.
-- This datastructure is a list because the signals activate and deactivate are triggered
-- respectively before and after change to/from program. During the transition there are
-- thus two scenes that could be active on program.
connections = {},
reply_data = {},
hotkeys = {},
callback_queue = {}, -- List of callbacks: [camera_id][type][] = table(expire, f)
suppress_scene_actions = false,
}
local plugin_actions = {
Suppress_Scene_Actions = 0,
}
local scene_action_at = {
Start = true,
Stop = false,
}
local plugin_scene_type = {
Program = 1,
Preview = 2,
}
local camera_actions = {
Camera_Off = 0,
Camera_On = 1,
Preset_Recall = 2,
PanTilt = 3,
Zoom_In = 4,
Zoom_Out = 5,
Focus_Auto = 6,
Focus_Manual = 7,
Focus_Near = 8,
Focus_Far = 9,
Focus_Refocus = 10,
Focus_Infinity = 11,
PanTiltZoom_Position = 12,
PanTilt_Speed_Increase = 13,
PanTilt_Speed_Decrease = 14,
ZoomFocus_Speed_Increase = 15,
ZoomFocus_Speed_Decrease = 16,
Image_Settings = 17,
ColorGain_Reset = 18,
ColorGain_Increase = 19,
ColorGain_Decrease = 20,
Brightness_Increase = 21,
Brightness_Decrease = 22,
PanTilt_Stop = 23, -- This action is a shorthand of action PanTilt, with direction 'stop', with the
-- difference that the action is immediately executed (on keydown instead of keyup)
Zoom_Stop = 24,
Focus_Stop = 25,
Custom_Command = 26,
}
local camera_action_active = {
Program = 1,
Preview = 2,
Always = 3,
}
local logging_levels = {
None = 0,
-- Leave some room for future addition of Error, Warning, ..
Info = 7,
Debug = 9
}
local function get_context(level)
level = level or 2
local info = debug.getinfo(level + 1, "nl")
local func = info.name or "?"
local line = info.currentline
local nesting = 0
while true do
if not debug.getinfo(nesting + level + 2) then
break
else
nesting = nesting + 1
end
end
return func, line, nesting
end
local function log_with_context(context, func, line, nesting, fmt, ...)
local args = {}
for i, a in ipairs(arg or { ... }) do
if type(a) == "table" then
local kvs
for k, v in pairs(a) do
if kvs then
kvs = string.format("%s, %s=%s", kvs, k, tostring(v))
else
kvs = string.format("%s=%s", k, tostring(v))
end
end
args[i] = kvs or "-"
else
args[i] = a
end
end
local status, msg = pcall(string.format, fmt, unpack(args or {}))
if not status then
if fmt then
msg = string.format("Error formatting log message '%s': %s", fmt, msg)
else
msg = "-"
end
end
print(string.format("%s:%-33s %s%s", context or "?", string.format("%s (%d):", func, line),
string.rep(" ", nesting), msg or "-"))
end
local function log(fmt, ...)
if plugin_data.debug or obs.obs_data_get_bool(plugin_settings, "debug_logging") or
(obs.obs_data_get_int(plugin_settings, "debug_logging") >= logging_levels.Info) then
local func, line, nesting = get_context(2)
log_with_context("script", func, line, nesting, fmt, unpack(arg or { ... }))
end
end
local function log_libvisca(fmt, ...)
if obs.obs_data_get_int(plugin_settings, "debug_logging") >= logging_levels.Debug then
local func, line, nesting = get_context(4)
log_with_context("libvisca", func, line, nesting, fmt, unpack(arg or { ... }))
end
end
local function parse_preset_value(preset_value)
local preset_name
local preset_id
local regex_patterns = {
"^(%g+)%s*[:=-]%s*(%d+)$",
"^(%d+)%s*[:=-]%s*(%g+)$"
}
for _, pattern in pairs(regex_patterns) do
local v1, v2 = string.match(preset_value, pattern)
if (v1 ~= nil) and (v2 ~= nil) then
if (tonumber(v1) == nil) and (tonumber(v2) ~= nil) then
preset_name = v1
preset_id = tonumber(v2)
break
elseif (tonumber(v2) == nil) and (tonumber(v1) ~= nil) then
preset_name = v2
preset_id = tonumber(v1)
break
end
end
end
if (preset_id ~= nil) and ((preset_id < 0) or (preset_id > 254)) then
preset_name = nil
preset_id = nil
end
return preset_name, preset_id
end
local function parse_custom_action(action)
local action_cmd = nil
local regex_pattern = "([0-9A-F][0-9A-F])"
if action and #action > 0 then
action_cmd = {}
for b in string.gmatch(action, regex_pattern) do
table.insert(action_cmd, tonumber(b, 16))
end
if bit.band(action_cmd[1], 0xF0) == 0x80 and action_cmd[#action_cmd] == 0xFF then
table.remove(action_cmd,1)
table.remove(action_cmd)
end
end
return action_cmd
end
local function plugin_callback_queue_add(camera_id, id, f, validity_seconds)
if type(plugin_data.callback_queue[camera_id]) ~= 'table' then
plugin_data.callback_queue[camera_id] = {}
end
if type(plugin_data.callback_queue[camera_id][id]) ~= 'table' then
plugin_data.callback_queue[camera_id][id] = {}
end
local expire = os.time() + (validity_seconds or 3)
table.insert(plugin_data.callback_queue[camera_id][id], {expire=expire, f=f})
end
local function plugin_callback_queue_invoke_one(camera_id, id, val)
if plugin_data.callback_queue[camera_id] then
local valid_callback = false
repeat
local callback = table.remove(plugin_data.callback_queue[camera_id][id] or {})
if not callback then
break
end
if callback.expire >= os.time() and type(callback.f) == 'function' then
valid_callback = true
local status,result_or_error = pcall(callback.f, val)
if not status then
log("Callback '%s' for camera %d failed: %s", id, camera_id, result_or_error)
end
end
until valid_callback
end
end
local function prop_presets_validate(props, property, settings)
local presets = obs.obs_data_get_array(settings, obs.obs_property_name(property))
local num_presets = obs.obs_data_array_count(presets)
log("prop_presets_validate %s %d", obs.obs_property_name(property), num_presets)
if num_presets > 0 then
for i = 0, num_presets - 1 do
local preset = obs.obs_data_array_item(presets, i)
--log(obs.obs_data_get_json(preset))
local preset_value = obs.obs_data_get_string(preset, "value")
--log("check %s", preset_value)
local preset_name, preset_id = parse_preset_value(preset_value)
if (preset_name == nil) or (preset_id == nil) then
print("Warning: preset '" .. preset_value .. "' has an unsupported syntax and cannot be used.")
end
obs.obs_data_release(preset)
end
end
obs.obs_data_array_release(presets)
end
local function create_camera_controls(cam_props, camera_id, settings)
local cams = obs.obs_properties_get(cam_props, "cameras")
if cams then
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local cam_name_suffix = string.format(" (cam %d)", camera_id)
local cam_name = obs.obs_data_get_string(settings, cam_prop_prefix .. "name")
if #cam_name == 0 then
cam_name = string.format("Camera %d", camera_id)
end
obs.obs_property_list_add_int(cams, cam_name, camera_id)
local prop_grp = obs.obs_properties_get(cam_props, cam_prop_prefix .. "grp")
if prop_grp == nil then
local props = obs.obs_properties_create()
local prop_name = obs.obs_properties_get(props, cam_prop_prefix .. "name")
if prop_name == nil then
obs.obs_properties_add_text(props, cam_prop_prefix .. "name", "Name", obs.OBS_TEXT_DEFAULT)
obs.obs_data_set_default_string(settings, cam_prop_prefix .. "name", cam_name)
end
local prop_version_info = obs.obs_properties_get(props, cam_prop_prefix .. "version_info")
if prop_version_info == nil then
prop_version_info = obs.obs_properties_add_text(props, cam_prop_prefix .. "version_info",
"Version Info", obs.OBS_TEXT_DEFAULT)
obs.obs_property_set_enabled(prop_version_info, false)
obs.obs_data_set_default_string(settings, cam_prop_prefix .. "version_info", "Unknown (not detected)")
end
local prop_address = obs.obs_properties_get(props, cam_prop_prefix .. "address")
if prop_address == nil then
obs.obs_properties_add_text(props, cam_prop_prefix .. "address", "IP Address", obs.OBS_TEXT_DEFAULT)
end
local prop_port = obs.obs_properties_get(props, cam_prop_prefix .. "port")
if prop_port == nil then
obs.obs_properties_add_int(props, cam_prop_prefix .. "port", "UDP Port", 1025, 65535, 1)
obs.obs_data_set_default_int(settings, cam_prop_prefix .. "port", Visca.default_port)
end
local prop_mode = obs.obs_properties_get(props, cam_prop_prefix .. "mode")
if prop_mode == nil then
prop_mode = obs.obs_properties_add_list(props, cam_prop_prefix .. "mode", "Mode",
obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT)
obs.obs_property_list_add_int(prop_mode, "Generic", Visca.modes.generic)
obs.obs_property_list_add_int(prop_mode, "PTZOptics", Visca.modes.ptzoptics)
obs.obs_data_set_default_int(settings, cam_prop_prefix .. "mode", Visca.modes.generic)
end
local prop_hk_pt_speed = obs.obs_properties_get(props, cam_prop_prefix .. "hk_pt_speed")
if prop_hk_pt_speed == nil then
obs.obs_properties_add_int_slider(props, cam_prop_prefix .. "hk_pt_speed", "Hotkey Pan/Tilt Speed",
Visca.limits.PAN_MIN_SPEED, Visca.limits.PAN_MAX_SPEED, 1)
obs.obs_data_set_default_int(settings, cam_prop_prefix .. "hk_pt_speed", 0x07)
end
local prop_hk_zf_speed = obs.obs_properties_get(props, cam_prop_prefix .. "hk_zf_speed")
if prop_hk_zf_speed == nil then
obs.obs_properties_add_int_slider(props, cam_prop_prefix .. "hk_zf_speed", "Hotkey Zoom/Focus Speed",
Visca.limits.ZOOM_MIN_SPEED, Visca.limits.ZOOM_MAX_SPEED, 1)
obs.obs_data_set_default_int(settings, cam_prop_prefix .. "hk_zf_speed", 0x02)
end
local prop_presets = obs.obs_properties_get(props, cam_prop_prefix .. "presets")
if prop_presets == nil then
prop_presets = obs.obs_properties_add_editable_list(props, cam_prop_prefix .. "presets", "Presets",
obs.OBS_EDITABLE_LIST_TYPE_STRINGS, "", "")
end
obs.obs_property_set_modified_callback(prop_presets, prop_presets_validate)
obs.obs_properties_add_group(cam_props, cam_prop_prefix .. "grp", "Camera configuration" .. cam_name_suffix,
obs.OBS_GROUP_NORMAL, props)
end
end
end
local function prop_set_attrs_values(props, property, settings)
local changed = false
local num_cameras = obs.obs_data_get_int(plugin_settings, "num_cameras")
local cam_idx = obs.obs_data_get_int(settings, "cameras")
if num_cameras == 0 then
cam_idx = 0
end
for camera_id = 1, num_cameras do
local visible = cam_idx == camera_id
log("%d %d %d", camera_id, cam_idx, visible and 1 or 0)
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local cam_props = { "grp", "name", "version_info", "address", "port", "mode", "presets", "preset_info" }
for _, cam_prop_name in pairs(cam_props) do
local cam_prop = obs.obs_properties_get(props, cam_prop_prefix .. cam_prop_name)
if cam_prop then
if obs.obs_property_visible(cam_prop) ~= visible then
obs.obs_property_set_visible(cam_prop, visible)
changed = true
end
end
end
end
return changed
end
local function prop_num_cams(props, property, settings)
local cam_added = false
local num_cameras = obs.obs_data_get_int(plugin_settings, "num_cameras")
log("num_cameras %d", num_cameras)
local cams = obs.obs_properties_get(props, "cameras")
if cams then
local camera_count = obs.obs_property_list_item_count(cams)
if num_cameras > camera_count then
for camera_id = camera_count + 1, num_cameras do
create_camera_controls(props, camera_id, settings)
end
cam_added = true
end
end
return cam_added
end
local function get_plugin_settings_from_scene(scene_type, camera_id)
scene_type = scene_type or plugin_scene_type.Preview
local p_settings = {}
local scene_source = nil
if type(scene_type) == "number" then
scene_source = (scene_type == plugin_scene_type.Program) and obs.obs_frontend_get_current_scene() or
obs.obs_frontend_get_current_preview_scene()
elseif type(scene_type) == "string" then
local scenes = obs.obs_frontend_get_scenes()
if scenes ~= nil then
for _, eval_scene in pairs(scenes) do
if scene_type == obs.obs_source_get_name(eval_scene) then
scene_source = obs.obs_source_get_ref(eval_scene)
break
end
end
obs.source_list_release(scenes)
end
end
if scene_source ~= nil then
local scene_name = obs.obs_source_get_name(scene_source)
local scene = obs.obs_scene_from_source(scene_source)
local scene_items = obs.obs_scene_enum_items(scene)
if scene_items ~= nil then
for _, scene_item in pairs(scene_items) do
local scene_item_source = obs.obs_sceneitem_get_source(scene_item)
local scene_item_source_id = obs.obs_source_get_unversioned_id(scene_item_source)
if scene_item_source_id == plugin_def.id then
local source_name = obs.obs_source_get_name(scene_item_source)
local source_settings = obs.obs_source_get_settings(scene_item_source)
local source_is_visible = obs.obs_source_showing(scene_item_source)
if source_settings then
local scene_camera_id = obs.obs_data_get_int(source_settings, "scene_camera")
if (camera_id == nil) or (camera_id == scene_camera_id) then
table.insert(p_settings, {scene_name, source_name, source_settings, source_is_visible})
else
obs.obs_data_release(source_settings)
end
end
end
end
obs.sceneitem_list_release(scene_items)
end
obs.obs_source_release(scene_source)
end
local plugins_visitor = coroutine.create(function()
for _,plugin_setting in pairs(p_settings) do
coroutine.yield(unpack(plugin_setting))
end
end)
return function()
local result, scene_name, source_name, source_settings, source_is_visible = coroutine.resume(plugins_visitor)
if result and scene_name and source_name then
return scene_name, source_name, source_settings, source_is_visible
else
return nil, nil, nil, nil
end
end
end
local function close_visca_connection(camera_id)
local connection = plugin_data.connections[camera_id]
if connection ~= nil then
connection:close()
connection:unregister_on_ack_callback(camera_id)
connection:unregister_on_completion_callback(camera_id)
connection:unregister_on_error_callback(camera_id)
connection:unregister_on_timeout_callback(camera_id)
connection = nil
plugin_data.connections[camera_id] = connection
end
end
--- @return Connection
local function open_visca_connection(camera_id)
local connection = plugin_data.connections[camera_id]
if connection == nil then
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local camera_address = obs.obs_data_get_string(plugin_settings, cam_prop_prefix .. "address")
local camera_port = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. "port")
local camera_mode = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. "mode")
log("Setup new connection for cam %d at %s:%d", camera_id, camera_address, camera_port)
local new_connection, connection_error = Visca.connect(camera_address, camera_port)
if new_connection then
connection = new_connection
if camera_mode then
connection:set_mode(camera_mode)
end
connection:register_on_completion_callback(camera_id, function(t)
log("Connection Completion received for camera %d (seq_nr %d)", camera_id, t and t.send.seq_nr or -1)
local t_data = t:inquiry_data()
if t_data and type(t_data) == 'table' then
local reply_data = plugin_data.reply_data[camera_id] or {}
for k,v in pairs(t_data) do
reply_data[k] = v
end
plugin_data.reply_data[camera_id] = reply_data
if t_data.vendor_id or t_data.model_code or t_data.rom_version then
local version_info = string.format("Vendor: %s (%04X), Model: %s (%04X), Firmware: %04X",
Visca.CameraVendor[reply_data.vendor_id] or "Unknown",
reply_data.vendor_id or 0,
Visca.CameraModel[reply_data.vendor_id][reply_data.model_code] or "Unknown",
reply_data.model_code or 0,
reply_data.rom_version or 0)
local version_info_setting = string.format("cam_%d_version_info", camera_id)
obs.obs_data_set_string(plugin_settings, version_info_setting, version_info)
log("Set camera %d version info to %s", camera_id, version_info)
local compatibility = {}
if t_data.vendor_id == 0x0001 and t_data.model_code == 0x0513 then
-- NewTek PTZ1 NDI
compatibility = { fixed_sequence_number = 1 }
end
if next(compatibility) then
connection:set_compatibility(compatibility)
local compat_a = {}
for k, v in pairs (compatibility) do
table.insert(compat_a, string.format('%s = %s', k, v))
end
print(string.format("Set compatibility mode for camera %d: %s", camera_id,
table.concat(compat_a, ',')))
end
end
if t_data.zoom or t_data.pan or t_data.tilt then
local ptz_vals = {}
if reply_data.pan then
table.insert(ptz_vals, string.format("Pan %d (%04X)", reply_data.pan, reply_data.pan))
else
table.insert(ptz_vals, "Pan: n/a (-)")
end
if reply_data.tilt then
table.insert(ptz_vals, string.format("Tilt: %d (%04X)", reply_data.tilt, reply_data.tilt))
else
table.insert(ptz_vals, "Tilt: n/a (-)")
end
if reply_data.zoom then
table.insert(ptz_vals, string.format("Zoom: %d (%04X)", reply_data.zoom, reply_data.zoom))
else
table.insert(ptz_vals, "Zoom: n/a (-)")
end
if reply_data.pantilt_pan_bytes then
connection:set_compatibility({pantilt_pan_bytes = reply_data.pantilt_pan_bytes})
end
for scene_name, source_name, source_settings, _ in
get_plugin_settings_from_scene(plugin_scene_type.Preview, camera_id) do
if source_settings then
local scene_camera_id = obs.obs_data_get_int(source_settings, "scene_camera")
if scene_camera_id == camera_id then
local ptz_str = table.concat(ptz_vals, ", ")
obs.obs_data_set_string(source_settings, "scene_ptz_position", ptz_str)
log("PTZ values set for camera %d: %s", camera_id, ptz_str)
else
print(string.format("Error setting PTZ values: callback camera %d does not match" ..
" source '%s' camera %d in scene %s",
camera_id, source_name, scene_camera_id, scene_name))
end
obs.obs_data_release(source_settings)
else
print(string.format("Error setting PTZ values: unable to find plugin settings for " ..
"camera %d in scene %s", camera_id, scene_name))
end
end
end
if t_data.brightness then
plugin_callback_queue_invoke_one(camera_id, 'brightness', t_data.brightness)
end
if t_data.color_level then
plugin_callback_queue_invoke_one(camera_id, 'color_level', t_data.color_level)
end
end
end)
connection:register_on_ack_callback(camera_id, function(t)
log("Connection ACK received for camera %d (seq_nr %d)", camera_id, t and t.send.seq_nr or -1)
end)
connection:register_on_error_callback(camera_id, function(t)
local error_msg = Visca.error_type_names[t.error.error_type] or 'Unknown'
log("Connection ERROR received for camera %d (seq_nr %d): %s",
camera_id, t and t.send.seq_nr or -1, error_msg)
end)
connection:register_on_timeout_callback(camera_id, function(t)
log("Connection Timeout for camera %d (seq_nr %d)", camera_id, t and t.send.seq_nr or -1)
end)
plugin_data.connections[camera_id] = connection
connection:Cam_Software_Version_Inquiry()
else
log(connection_error)
end
end
return connection
end
local function cb_plugin_hotkey(pressed, hotkey_data)
if hotkey_data.action == plugin_actions.Suppress_Scene_Actions then
plugin_data.suppress_scene_actions = pressed and true or false
end
end
local function do_cam_action_start(camera_id, camera_action, action_args_in)
local action_args = {}
for k,v in pairs(action_args_in or {}) do
action_args[k] = v
end
-- Force close connection before sending On-command to prevent usage of a dead connection
if camera_action == camera_actions.Camera_On then
close_visca_connection(camera_id)
end
log("Start cam %d action %d (args %s)", camera_id, camera_action, action_args)
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local connection = open_visca_connection(camera_id)
if connection then
if camera_action == camera_actions.Camera_Off then
connection:Cam_Power(false)
-- Force close connection after sending Off-command.
connection:close()
plugin_data.connections[camera_id] = nil
elseif camera_action == camera_actions.Camera_On then
connection:Cam_Power(true)
elseif camera_action == camera_actions.Preset_Recall and action_args.preset then
connection:Cam_Preset_Recall(action_args.preset)
elseif camera_action == camera_actions.PanTilt then
if not action_args.speed then
action_args.speed = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. "hk_pt_speed") or
Visca.limits.PAN_MIN_SPEED
end
connection:Cam_PanTilt(action_args.direction or Visca.PanTilt_directions.stop, action_args.speed,
action_args.speed)
elseif camera_action == camera_actions.Zoom_In then
if not action_args.speed then
action_args.speed = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. "hk_zf_speed") or
Visca.limits.ZOOM_MIN_SPEED
end
connection:Cam_Zoom_Tele(action_args.speed)
elseif camera_action == camera_actions.Zoom_Out then
if not action_args.speed then
action_args.speed = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. "hk_zf_speed") or
Visca.limits.ZOOM_MIN_SPEED
end
connection:Cam_Zoom_Wide(action_args.speed)
elseif camera_action == camera_actions.Focus_Auto then
connection:Cam_Focus_Mode(Visca.Focus_modes.auto)
elseif camera_action == camera_actions.Focus_Manual then
connection:Cam_Focus_Mode(Visca.Focus_modes.manual)
elseif camera_action == camera_actions.Focus_Refocus then
connection:Cam_Focus_Mode(Visca.Focus_modes.manual)
connection:Cam_Focus_Mode(Visca.Focus_modes.one_push_trigger)
elseif camera_action == camera_actions.Focus_Infinity then
connection:Cam_Focus_Mode(Visca.Focus_modes.manual)
connection:Cam_Focus_Mode(Visca.Focus_modes.infinity)
elseif camera_action == camera_actions.Focus_Near then
connection:Cam_Focus_Mode(Visca.Focus_modes.manual)
connection:Cam_Focus_Near()
elseif camera_action == camera_actions.Focus_Far then
connection:Cam_Focus_Mode(Visca.Focus_modes.manual)
connection:Cam_Focus_Far()
elseif camera_action == camera_actions.PanTiltZoom_Position then
if action_args.pan_position ~= nil and action_args.tilt_position ~= nil then
if not action_args.speed then
action_args.speed = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. "hk_pt_speed") or
Visca.limits.PAN_MIN_SPEED
end
connection:Cam_PanTilt_Absolute(action_args.speed, action_args.pan_position, action_args.tilt_position)
end
if action_args.zoom_position ~= nil then
connection:Cam_Zoom_To(action_args.zoom_position)
end
elseif camera_action == camera_actions.ColorGain_Reset then
connection:Cam_Color_Gain_Reset()
elseif camera_action == camera_actions.ColorGain_Increase then
plugin_callback_queue_add(camera_id, 'color_level', function()
local reply_data = plugin_data.reply_data[camera_id] or {}
if reply_data.color_level then
connection:Cam_Color_Gain(reply_data.color_level + 1)
end
end)
connection:Cam_Color_Gain_Inquiry()
elseif camera_action == camera_actions.ColorGain_Decrease then
plugin_callback_queue_add(camera_id, 'color_level', function()
local reply_data = plugin_data.reply_data[camera_id] or {}
if reply_data.color_level then
connection:Cam_Color_Gain(reply_data.color_level - 1)
end
end)
connection:Cam_Color_Gain_Inquiry()
elseif camera_action == camera_actions.Brightness_Increase then
plugin_callback_queue_add(camera_id, 'brightness', function()
local reply_data = plugin_data.reply_data[camera_id] or {}
if reply_data.brightness then
connection:Cam_Color_Gain(reply_data.brightness + 1)
end
end)
connection:Cam_Brightness_Inquiry()
elseif camera_action == camera_actions.Brightness_Decrease then
plugin_callback_queue_add(camera_id, 'brightness', function()
local reply_data = plugin_data.reply_data[camera_id] or {}
if reply_data.brightness then
connection:Cam_Color_Gain(reply_data.brightness - 1)
end
end)
connection:Cam_Brightness_Inquiry()
elseif camera_action == camera_actions.Image_Settings then
if action_args.color_level then
connection:Cam_Color_Gain(action_args.color_level)
end
if action_args.brightness then
connection:Cam_Brightness(action_args.brightness)
end
elseif camera_action == camera_actions.PanTilt_Stop then
connection:Cam_PanTilt(Visca.PanTilt_directions.stop)
elseif camera_action == camera_actions.Zoom_Stop then
connection:Cam_Zoom_Stop()
elseif camera_action == camera_actions.Focus_Stop then
connection:Cam_Focus_Stop()
elseif camera_action == camera_actions.Custom_Command and action_args.custom_start then
connection:Send_Raw_Command(action_args.custom_start)
end
end
end
local function do_cam_action_stop(camera_id, camera_action, action_args)
action_args = action_args or {}
log("Stop cam %d action %d (arg %s)", camera_id, camera_action, action_args)
local connection = open_visca_connection(camera_id)
if connection then
if camera_action == camera_actions.PanTilt then
connection:Cam_PanTilt(Visca.PanTilt_directions.stop)
elseif camera_action == camera_actions.Zoom_In then
connection:Cam_Zoom_Stop()
elseif camera_action == camera_actions.Zoom_Out then
connection:Cam_Zoom_Stop()
elseif camera_action == camera_actions.Focus_Near then
connection:Cam_Focus_Stop()
elseif camera_action == camera_actions.Focus_Far then
connection:Cam_Focus_Stop()
elseif camera_action == camera_actions.Custom_Command and action_args.custom_stop then
connection:Send_Raw_Command(action_args.custom_stop)
end
end
end
local function cb_camera_hotkey(pressed, hotkey_data)
local camera_id = hotkey_data.camera_id
local camera_action = hotkey_data.action
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local function _change_cam_data_value(data_name, value_delta, value_min, value_max)
local value = obs.obs_data_get_int(plugin_settings, cam_prop_prefix .. data_name) or value_min
value = math.min(math.max(value + value_delta, value_min), value_max)
obs.obs_data_set_int(plugin_settings, cam_prop_prefix .. data_name, value)
end
if pressed then
if camera_action == camera_actions.PanTilt_Speed_Increase then
_change_cam_data_value("hk_pt_speed", 1, Visca.limits.PAN_MIN_SPEED, Visca.limits.PAN_MAX_SPEED)
elseif camera_action == camera_actions.PanTilt_Speed_Decrease then
_change_cam_data_value("hk_pt_speed", -1, Visca.limits.PAN_MIN_SPEED, Visca.limits.PAN_MAX_SPEED)
elseif camera_action == camera_actions.ZoomFocus_Speed_Increase then
_change_cam_data_value("hk_zf_speed", 1, Visca.limits.ZOOM_MIN_SPEED, Visca.limits.ZOOM_MAX_SPEED)
elseif camera_action == camera_actions.ZoomFocus_Speed_Decrease then
_change_cam_data_value("hk_zf_speed", -1, Visca.limits.ZOOM_MIN_SPEED, Visca.limits.ZOOM_MAX_SPEED)
else
do_cam_action_start(camera_id, camera_action, hotkey_data.action_args)
end
else
if not (camera_action == camera_actions.PanTilt_Speed_Increase or
camera_action == camera_actions.PanTilt_Speed_Decrease or
camera_action == camera_actions.ZoomFocus_Speed_Increase or
camera_action == camera_actions.ZoomFocus_Speed_Decrease) then
do_cam_action_stop(camera_id, camera_action, hotkey_data.action_args)
end
end
end
local function cb_backup_restore(props, __property, __settings)
local backup_file = obs.obs_data_get_string(plugin_settings, "backup_file") or ""
if #backup_file > 0 then
local backup_settings = obs.obs_data_create_from_json_file_safe(backup_file, "bak")
if backup_settings ~= nil then
local num_cameras = obs.obs_data_get_int(backup_settings, "num_cameras")
for camera_id = 1, num_cameras do
create_camera_controls(props, camera_id, backup_settings)
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local cam_name = obs.obs_data_get_string(backup_settings, cam_prop_prefix .. "name")
if cam_name then
obs.obs_data_set_string(plugin_settings, cam_prop_prefix .. "name", cam_name)
end
local cam_address = obs.obs_data_get_string(backup_settings, cam_prop_prefix .. "address")
if cam_address then
obs.obs_data_set_string(plugin_settings, cam_prop_prefix .. "address", cam_address)
end
local cam_port = obs.obs_data_get_int(backup_settings, cam_prop_prefix .. "port")
if cam_port then
obs.obs_data_set_int(plugin_settings, cam_prop_prefix .. "port", cam_port)
end
local cam_mode = obs.obs_data_get_int(backup_settings, cam_prop_prefix .. "mode")
if cam_mode then
obs.obs_data_set_int(plugin_settings, cam_prop_prefix .. "mode", cam_mode)
end
local cam_settings = obs.obs_data_get_array(backup_settings, cam_prop_prefix .. "presets")
if obs.obs_data_array_count(cam_settings) > 0 then
obs.obs_data_set_array(plugin_settings, cam_prop_prefix .. "presets", cam_settings)
end
obs.obs_data_array_release(cam_settings)
end
obs.obs_data_release(backup_settings)
log("Settings restored from %s", backup_file)
return true
end
else
log("Unable to restore, 'backup_file' is not set.")
end
end
local function cb_backup_save(__props, __property, __settings)
local backup_file = obs.obs_data_get_string(plugin_settings, "backup_file")
if #backup_file > 0 then
obs.obs_data_set_string(plugin_settings, "backup_file", nil)
obs.obs_data_save_json_safe(plugin_settings, backup_file, "tmp", "bak")
log("Settings saved to %s", backup_file)
obs.obs_data_set_string(plugin_settings, "backup_file", backup_file)
return true
else
log("Unable to save, 'backup_file' property is not set.")
end
end
local function handleViscaResponses()
for camera_id, connection in pairs(plugin_data.connections) do
local success, msg, err, num = pcall(function() return connection:receive() end)
if not success then
log("Poll camera %d failed: %s", camera_id, msg)
else
if msg then
log("Poll camera %d (%s): %s", camera_id, tostring(connection), msg:as_string(connection.mode))
if plugin_data.debug then
msg:dump()
end
elseif err ~= "timeout" then
log("Poll camera %d (%s) failed: %s (%d)", camera_id, tostring(connection), err, num)
if num == 22 or num == 10022 then
close_visca_connection(camera_id)
end
end
end
end
end
function script_description()
return "<b>" .. plugin_info.description .. "</b><br>" ..
"Version: " .. plugin_info.version .. "<br>" ..
"<a href=\"" .. plugin_info.url .. "\">" .. plugin_info.url .. "</a><br><br>" ..
"Usage:<br>" ..
"To add a preset in the list, use one the following naming conventions:<ul>" ..
"<li><name><separator><preset id>, e.g. 'Stage: 6'</li>" ..
"<li><preset id><separator><name>, e.g. '5 = Pastor'</li>" ..
"</ul>where <separator> is one of ':', '=' or '-'."
end
function script_update(settings)
plugin_settings = settings
end
function script_save(settings)
for _, hotkey in pairs(plugin_data.hotkeys) do
local a = obs.obs_hotkey_save(hotkey.id)
obs.obs_data_set_array(settings, hotkey.name .. "_hotkey", a)
obs.obs_data_array_release(a)
end
end
function script_load(settings)
plugin_settings = settings
print(string.format("%s version %s", plugin_info.name, plugin_info.version))
if obs.obs_data_get_int(plugin_settings, "debug_logging") >= logging_levels.Debug then
Visca.set_log_function(log_libvisca)
end
local plugin_hotkey_actions = {
{ name = "suppress_scene_actions", descr = "Suppress actions on scenes",
action = plugin_actions.Suppress_Scene_Actions },
}
for _, v in pairs(plugin_hotkey_actions) do
local hotkey_name = "visca_" .. v.name
local hotkey_id = obs.obs_hotkey_register_frontend(hotkey_name, v.descr .. " for Visca cams",
function(pressed)
cb_plugin_hotkey(pressed, { name = hotkey_name, action = v.action, action_args = v.action_args })
end)
local a = obs.obs_data_get_array(settings, hotkey_name .. "_hotkey")
obs.obs_hotkey_load(hotkey_id, a)
obs.obs_data_array_release(a)
table.insert(plugin_data.hotkeys, {
name = hotkey_name,
id = hotkey_id,
action = v.action
})
end
local camera_hotkey_actions = {
{ name = "pan_left", descr = "Pan Left", action = camera_actions.PanTilt,
action_args = { direction = Visca.PanTilt_directions.left } },
{ name = "pan_right", descr = "Pan Right", action = camera_actions.PanTilt,
action_args = { direction = Visca.PanTilt_directions.right } },
{ name = "tilt_up", descr = "Tilt Up", action = camera_actions.PanTilt,
action_args = { direction = Visca.PanTilt_directions.up } },
{ name = "tilt_down", descr = "Tilt Down", action = camera_actions.PanTilt,
action_args = { direction = Visca.PanTilt_directions.down } },
{ name = "pantilt_speed_incr", descr = "Increase Pan/Tilt speed",
action = camera_actions.PanTilt_Speed_Increase },
{ name = "pantilt_speed_decr", descr = "Decrease Pan/Tilt speed",
action = camera_actions.PanTilt_Speed_Decrease },
{ name = "pantilt_stop", descr = "Stop Pan/Tilt motion", action = camera_actions.PanTilt_Stop },
{ name = "zoom_in", descr = "Zoom In", action = camera_actions.Zoom_In },
{ name = "zoom_out", descr = "Zoom Out", action = camera_actions.Zoom_Out },
{ name = "zoom_stop", descr = "Stop Zoom change", action = camera_actions.Zoom_Stop },
{ name = "color_gain_reset", descr = "Color Gain (Saturation) Reset", action = camera_actions.ColorGain_Reset },
{ name = "color_gain_increment", descr = "Color Gain (Saturation) Increment",
action = camera_actions.ColorGain_Increase },
{ name = "color_gain_decrement", descr = "Color Gain (Saturation) Decrement",
action = camera_actions.ColorGain_Decrease },
{ name = "brightness_increment", descr = "Brightness Increment", action = camera_actions.Brightness_Increase },
{ name = "brightness_decrement", descr = "Brightness Decrement", action = camera_actions.Brightness_Decrease },
{ name = "focus_auto", descr = "Focus mode Automatic", action = camera_actions.Focus_Auto },
{ name = "focus_manual", descr = "Focus mode Manual", action = camera_actions.Focus_Manual },
{ name = "focus_trigger", descr = "Focus trigger Refocus", action = camera_actions.Focus_Refocus },
{ name = "focus_near", descr = "Focus to Near", action = camera_actions.Focus_Near },
{ name = "focus_far", descr = "Focus to Far", action = camera_actions.Focus_Far },
{ name = "focus_infinity", descr = "Focus to Infinity", action = camera_actions.Focus_Infinity },
{ name = "zoomfocus_speed_incr", descr = "Increase Zoom/Focus speed",
action = camera_actions.ZoomFocus_Speed_Increase },
{ name = "zoomfocus_speed_decr", descr = "Decrease Zoom/Focus speed",
action = camera_actions.ZoomFocus_Speed_Decrease },
{ name = "focus_stop", descr = "Stop Focus change", action = camera_actions.Focus_Stop },
{ name = "preset_0", descr = "Preset 0", action = camera_actions.Preset_Recall, action_args = { preset = 0 } },
{ name = "preset_1", descr = "Preset 1", action = camera_actions.Preset_Recall, action_args = { preset = 1 } },
{ name = "preset_2", descr = "Preset 2", action = camera_actions.Preset_Recall, action_args = { preset = 2 } },
{ name = "preset_3", descr = "Preset 3", action = camera_actions.Preset_Recall, action_args = { preset = 3 } },
{ name = "preset_4", descr = "Preset 4", action = camera_actions.Preset_Recall, action_args = { preset = 4 } },
{ name = "preset_5", descr = "Preset 5", action = camera_actions.Preset_Recall, action_args = { preset = 5 } },
{ name = "preset_6", descr = "Preset 6", action = camera_actions.Preset_Recall, action_args = { preset = 6 } },
{ name = "preset_7", descr = "Preset 7", action = camera_actions.Preset_Recall, action_args = { preset = 7 } },
{ name = "preset_8", descr = "Preset 8", action = camera_actions.Preset_Recall, action_args = { preset = 8 } },
{ name = "preset_9", descr = "Preset 9", action = camera_actions.Preset_Recall, action_args = { preset = 9 } },
}
local num_cameras = obs.obs_data_get_int(settings, "num_cameras")
for camera_id = 1, num_cameras do
local cam_prop_prefix = string.format("cam_%d_", camera_id)
local cam_name = obs.obs_data_get_string(settings, cam_prop_prefix .. "name")
if #cam_name == 0 then
cam_name = string.format("Camera %d", camera_id)
end
obs.obs_data_set_default_string(settings, cam_prop_prefix .. "name", cam_name)
obs.obs_data_set_default_int(settings, cam_prop_prefix .. "port", Visca.default_port)
obs.obs_data_set_default_int(settings, cam_prop_prefix .. "mode", Visca.modes.generic)
for _, v in pairs(camera_hotkey_actions) do
local hotkey_name = cam_prop_prefix .. v.name
local hotkey_id = obs.obs_hotkey_register_frontend(hotkey_name, v.descr .. " on " .. cam_name,
function(pressed)
cb_camera_hotkey(pressed, { name = hotkey_name, camera_id = camera_id, action = v.action,
action_args = v.action_args })
end)
local a = obs.obs_data_get_array(settings, hotkey_name .. "_hotkey")
obs.obs_hotkey_load(hotkey_id, a)
obs.obs_data_array_release(a)
table.insert(plugin_data.hotkeys, {
name = hotkey_name,
id = hotkey_id,
camera_id = camera_id,
action = v.action