-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatemlib.lua
More file actions
347 lines (310 loc) · 8.04 KB
/
atemlib.lua
File metadata and controls
347 lines (310 loc) · 8.04 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
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
local ATEM = {}
local socket = require("socket")
local CMD = require("atemlib.cmd")
local Events = require("atemlib.events") --This is a collection of functions that takes the raw return from the mixer and returns separated values
local tinsert = table.insert
local tremove = table.remove
local strchar = string.char
local ATEMS = {}
local Connections = {}
-----------String modifiers
function string.FromHex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
function string.hex2bin(str)
str=string.upper(str)
local map = {
['0'] = '0000',
['1'] = '0001',
['2'] = '0010',
['3'] = '0011',
['4'] = '0100',
['5'] = '0101',
['6'] = '0110',
['7'] = '0111',
['8'] = '1000',
['9'] = '1001',
['A'] = '1010',
['B'] = '1011',
['C'] = '1100',
['D'] = '1101',
['E'] = '1110',
['F'] = '1111',
}
return str:gsub('[0-9A-F]', map)
end
function string.CorrectLength(str,len)
for i=#str+1,len do
str="0"..str
end
str = string.sub(str,#str-(len-1))
return str
end
---------------
local function GetHead(hex)
local head = hex:sub(1,4)
local bin = head:hex2bin()
local CmdFlag = bin:sub(1,5)
local Packet = bin:sub(6)
local cmd
if CmdFlag == "10000" then
cmd = "Response"
elseif CmdFlag == "01000" then
cmd = "Hello"
elseif CmdFlag =="00100" then
cmd = "Retransmission"
elseif CmdFlag == "00010" then
cmd = "Init"
elseif CmdFlag =="00001" then
cmd = "ACK"
elseif CmdFlag == "10001" then
cmd = "CMDRET"
end
local len=0
for i=1,11 do
local num = tonumber(string.sub(Packet,i,i))
len = len + num*2^(11-i)
end
return cmd, len
end
function string.tohex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
local function CreateHeader(Type,Mixer,length)
local Flag
if Type=="ACK" then
Flag="00001"
end
--print("length",length)
local len = string.format("%x",length):hex2bin()
--print("binlen",len)
for i=string.len(len)+1,11 do
len = "0"..len
end
--print("förlängd",len)
len=string.sub(len,#len-10)
--print("lendone",len)
local Head=Flag..len
local pktut = string.format("%x",Mixer.Cmdid)
for i=string.len(pktut)+1,4 do
pktut = "0"..pktut
end
Mixer.Cmdid = Mixer.Cmdid+1
local head=0
--print("Head",Head)
head = tonumber(Head,2)
--print(head)
head = string.format("%x",head)
for i=#head+1,4 do
head="0"..head
end
--print("hexhead",head)
return head..Mixer.Session.."0000".."0000".."0000"..pktut
end
function CMD.Close(self,reason)
local ID = self.id
print("LIB self id",self.id)
ATEMS[ID]:close()
for k,v in pairs(self) do
v = nil
end
if reason then
self.Callback("Error",reason)
end
Connections[ATEMS[ID]]=nil
tremove(Connections,ID)
tremove(ATEMS,ID)
self=nil
for _,Conn in pairs(Connections) do
if Conn.id>=ID then
Conn.id = Conn.id - 1
end
end
end
local function RegisterEvent(self,event)
self.Events[event]=1
end
local function UnregisterEvent(self,event)
self.Events[event]=1
end
local function GetEvents(data)--
local Info = {}
local Max = #data
local n=1
while n<Max do
local Size = tonumber(data:sub(n,n+3),16)
n = n + 4
local Name = {}
--print(data:sub(n,n+11))
for i=n+4, n+11,2 do
local Byte = data:sub(i,i+1)
--print(Byte)
if Byte~="00" then
local num = tonumber(Byte,16)
if (65 <= num and num <= 90) or (97 <= num and num <= 122) or num == 95 then
tinsert(Name,strchar(num))
end
end
end
local Data = data:sub(n+12,n+Size*2-5)
n = n + Size*2-4
local Name = table.concat(Name)
tinsert(Info,{Name = Name, Data = Data})
end
return Info
end
local function ACK(Conn,pkt,cpkt)
local Obj = Connections[Conn]
local pktut = string.format("%x",Obj.Cpkt)
for i=string.len(pktut)+1,4 do
pktut = "0"..pktut
end
Obj.Cpkt = Obj.Cpkt +1
Conn:send(string.FromHex("800c"..Obj.Session..pkt.."0000"..pktut.."0000"))
end
local function Reconnect(Mixer)
print("LIB Reconnecting")
local Conn = Connections[Mixer]
Conn.Cpkt = 1
Conn.Cmdid = 1
local Init = "101453AB00000000003A00000100000000000000" --"1014"..session.."00000000003A00000100000000000000"
Mixer:send(Init:FromHex())
Mixer:settimeout(1) --Wait one second for the ATEM to answer
local data = Mixer:receive()
if data then
local hexdata = data:tohex()
local pktid = hexdata:sub(21,24)
local CMD, LEN = GetHead(hexdata)
if CMD == "Init" and LEN == 20 then
ACK(Mixer,"0000")
Conn.Init = 1--Set initiation status 1 so that we know to grab the real session id on the next recieve
end
print("LIB Reconnected sucessfully")
return true
end
return false
end
function ATEM.Connect(ip,callback,forever)
local udp = socket.udp()
Connections[udp] = {}
local Connection = Connections[udp]
Connection.Session="53AB"--Ska dessa värden vara random? Byt till bättre förklarade namn
Connection.Cpkt = 1
Connection.Recon = forever
Connection.Cmdid = 1
Connection.Events = {}
local Init = "101453AB00000000003A00000100000000000000" --"1014"..session.."00000000003A00000100000000000000"
udp:setpeername(ip, 9910)
print(udp:send(Init:FromHex()))
udp:settimeout(1) --Wait one second for the ATEM to answer
local data, err = udp:receive()
if data then
local hexdata = data:tohex()
local pktid = hexdata:sub(21,24)
local Event, LEN = GetHead(hexdata)
if Event == "Init" and LEN == 20 then
ACK(udp,"0000")
Connection.Init = 1--Set initiation status 1 so that we know to grab the real session id on the next recieve
end
tinsert(ATEMS,udp) --Inited with the ATEM store the udp connection and "object"
tinsert(Connections,Connection)
Connection.id = #ATEMS
Connection.Callback = callback
Connection.UnregisterEvent = UnregisterEvent
Connection.RegisterEvent = RegisterEvent
--udp.id = #ATEMS
for k,v in pairs(CMD) do--Insert Commands in every connection
Connection[k]=v
end
Connection.CMDLength = 0
Connection.CMDBuffer = ""
Connection.Lasttime=socket.gettime()
return Connection
else
print(err, udp)
udp:close()
Connections[udp]=nil
return false
end
end
function ATEM.Main()
local canread = socket.select(ATEMS,nil,0)
for _,Mixer in ipairs(canread) do
local data = Mixer:receive()
local Conn = Connections[Mixer]
local hexdata = data:tohex()
local pktid = hexdata:sub(21,24)
Conn.Lasttime = socket:gettime()
if Conn.Init == 1 then
Conn.Session = hexdata:sub(5,8)
Conn.Init = 2
end
local CMD ,LEN = GetHead(hexdata)
if (CMD == "ACK" or CMD == "CMDRET") and LEN == 12 then --Heartbeat
ACK(Mixer,pktid)
elseif (CMD == "ACK" or CMD== "CMDRET") and LEN > 12 then
ACK(Mixer,pktid)
local EventList = GetEvents(hexdata:sub(25))
--print("events")
for i=1, #EventList do
local Event = EventList[i]
local Name = Event.Name
--print(Name)
if Conn.Events[Name] then
Conn.Callback(Name,Events[Name](Event.Data))
end
end
end
--if CMD == "CMDRET" then
--ACK(Mixer,pktid)
--end
end
for _,Mixer in ipairs(ATEMS) do --Look for timeouts
local Conn = Connections[Mixer]
if socket:gettime()-Conn.Lasttime > 5 then ---------Vad är mixerns timeout tid?
local status = Reconnect(Mixer)
if status == false then
Conn.Tries = Conn.Tries or 0
Conn.Tries = Conn.Tries + 1
if Conn.Tries == 1 and Conn.Recon then
Conn.Callback("Error","Reconnecting")
end
if Conn.Tries >=5 and not Conn.Recon then
Conn:Close("LostConnection")
end
else
if Conn.Tries and Conn.Tries > 1 then
Conn.Callback("Error","Connection Resumed")
end
Conn.Tries = 0
Conn.Lasttime = socket:gettime()
end
end
end
for i,Conn in ipairs(Connections) do--Send and clear the buffers.
if Conn.CMDBuffer~="" then
local Mixer = ATEMS[i]
local send = CreateHeader("ACK",Conn,12+Conn.CMDLength)..Conn.CMDBuffer
Mixer:send(send:FromHex())
Conn.CMDBuffer=""
Conn.CMDLength=0
socket.sleep(0.02) --The mixer can get to many messages, just to make sure it dosn't crash.
end
end
if #Connections==0 then
return false
else
return true
end
end
function ATEM.MainLoop()
local Continue = true
while Continue do
Continue = ATEM.Main()
end
end
return ATEM