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
local cfg = UI.Schema({
scene_a_btn = UI.Keybind("Mouse4", { label = "Scene A Key", tab = "Scenes" }),
scene_a_name = UI.Text("Starting Soon", { label = "Scene A Name", tab = "Scenes" }),
scene_b_btn = UI.Keybind("Mouse5", { label = "Scene B Key", tab = "Scenes" }),
scene_b_name = UI.Text("Gameplay", { label = "Scene B Name", tab = "Scenes" }),
scene_c_btn = UI.Keybind("F1", { label = "Scene C Key", tab = "Scenes" }),
scene_c_name = UI.Text("Just Chatting", { label = "Scene C Name", tab = "Scenes" }),
scene_d_btn = UI.Keybind("F2", { label = "Scene D Key", tab = "Scenes" }),
scene_d_name = UI.Text("BRB", { label = "Scene D Name", tab = "Scenes" }),
scene_e_btn = UI.Keybind("F3", { label = "Scene E Key", tab = "Scenes" }),
scene_e_name = UI.Text("Ending", { label = "Scene E Name", tab = "Scenes" }),
mute_btn = UI.Keybind("F9", { label = "Toggle Mic Mute Key", group = "Audio", tab = "Controls" }),
mic_source = UI.Text("Mic/Aux", { label = "Mic Source Name", group = "Audio", tab = "Controls" }),
stream_start_btn = UI.Keybind("F5", { label = "Start Stream Key", group = "Stream", tab = "Controls" }),
stream_stop_btn = UI.Keybind("F6", { label = "Stop Stream Key", group = "Stream", tab = "Controls" }),
stream_toggle_btn = UI.Keybind("F10", { label = "Toggle Stream Key", group = "Stream", tab = "Controls" }),
record_toggle_btn = UI.Keybind("F11", { label = "Toggle Record Key", group = "Output", tab = "Controls" }),
replay_save_btn = UI.Keybind("F8", { label = "Save Replay Buffer Key", group = "Output", tab = "Controls" }),
cam_toggle_btn = UI.Keybind("F7", { label = "Toggle Virtual Cam Key", group = "Camera", tab = "Controls" }),
ws_url = UI.Text("ws://127.0.0.1:4455", { label = "OBS WebSocket URL", tab = "Connection" }),
password = UI.Text("", { label = "Server Password (Optional)", tab = "Connection" }),
beep = UI.Toggle(true, { label = "Audio Feedback on Actions", tab = "Connection" }),
})
local ws_conn = nil
local is_ready = false
local reconnect_timer = nil
local req_counter = 0
local function play_feedback()
if cfg.beep then
Audio.Beep()
end
end
local function connect()
if ws_conn then
pcall(function() ws_conn:Close() end)
ws_conn = nil
end
is_ready = false
local conn = nil
local ok, err = pcall(function()
conn = Net.WSConnect(cfg.ws_url, {
OnOpen = function()
Log.Info("Connecting to OBS WebSocket at " .. cfg.ws_url)
end,
OnMessage = function(payload, is_binary)
if is_binary then return end
local msg = JSON.Parse(payload)
if not msg or type(msg.op) ~= "number" then return end
local client = conn or ws_conn
if not client then return end
if msg.op == 0 then
local d = msg.d or {}
local auth_data = d.authentication
local identify_payload = {
rpcVersion = 1,
}
if auth_data and auth_data.challenge and auth_data.salt then
local pw = cfg.password or ""
local secret = Codec.Base64(Codec.HexDecode(Hash.SHA256(pw .. auth_data.salt)))
local auth_str = Codec.Base64(Codec.HexDecode(Hash.SHA256(secret .. auth_data.challenge)))
identify_payload.authentication = auth_str
end
local send_ok, send_err = pcall(function()
client:Send(JSON.Stringify({
op = 1,
d = identify_payload,
}))
end)
if not send_ok then
Log.Error("Failed to send Identify to OBS: " .. tostring(send_err))
end
elseif msg.op == 2 then
is_ready = true
Log.Info("OBS WebSocket authenticated and ready")
UI.Notify("Connected to OBS", "success")
elseif msg.op == 7 then
local d = msg.d or {}
local status = d.requestStatus or {}
if status.result then
play_feedback()
UI.Notify("OBS: " .. (d.requestType or "Command") .. " OK", "info")
else
UI.Notify("OBS Error: " .. (status.comment or "Request failed"), "warning")
end
end
end,
OnClose = function()
if is_ready then
Log.Warn("OBS WebSocket disconnected")
UI.Notify("OBS Disconnected", "warning")
else
Log.Warn("OBS WebSocket closed before identification")
end
is_ready = false
ws_conn = nil
end,
})
end)
if ok and conn then
ws_conn = conn
else
ws_conn = nil
is_ready = false
if not ok then
Log.Error("Failed to connect to OBS WebSocket: " .. tostring(err))
end
end
end
local function send_command(request_type, request_data)
if not ws_conn or not is_ready then
connect()
UI.Notify("Connecting to OBS... Try again in a moment.", "warning")
return
end
req_counter = req_counter + 1
local msg = {
op = 6,
d = {
requestType = request_type,
requestId = tostring(req_counter),
requestData = request_data,
},
}
local ok = pcall(function()
ws_conn:Send(JSON.Stringify(msg))
end)
if not ok then
UI.Notify("Failed to send command to OBS", "error")
end
end
function OnStart()
Log.Info("OBS Stream Deck Controller starting")
connect()
reconnect_timer = Timer.Every(5000, function()
if not ws_conn or not is_ready then
connect()
end
end)
end
function OnStop()
if reconnect_timer then
reconnect_timer:Cancel()
reconnect_timer = nil
end
if ws_conn then
pcall(function() ws_conn:Close() end)
ws_conn = nil
end
is_ready = false
Audio.StopAll()
end
Bind(cfg.scene_a_btn, function()
send_command("SetCurrentProgramScene", { sceneName = cfg.scene_a_name })
end)
Bind(cfg.scene_b_btn, function()
send_command("SetCurrentProgramScene", { sceneName = cfg.scene_b_name })
end)
Bind(cfg.scene_c_btn, function()
send_command("SetCurrentProgramScene", { sceneName = cfg.scene_c_name })
end)
Bind(cfg.scene_d_btn, function()
send_command("SetCurrentProgramScene", { sceneName = cfg.scene_d_name })
end)
Bind(cfg.scene_e_btn, function()
send_command("SetCurrentProgramScene", { sceneName = cfg.scene_e_name })
end)
Bind(cfg.mute_btn, function()
send_command("ToggleInputMute", { inputName = cfg.mic_source })
end)
Bind(cfg.stream_start_btn, function()
send_command("StartStream")
end)
Bind(cfg.stream_stop_btn, function()
send_command("StopStream")
end)
Bind(cfg.stream_toggle_btn, function()
send_command("ToggleStream")
end)
Bind(cfg.record_toggle_btn, function()
send_command("ToggleRecord")
end)
Bind(cfg.replay_save_btn, function()
send_command("SaveReplayBuffer")
end)
Bind(cfg.cam_toggle_btn, function()
send_command("ToggleVirtualCam")
end)