forked from mraerino/atem-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
367 lines (321 loc) · 8.74 KB
/
client.go
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
package atem
import (
"bytes"
"context"
"math/rand"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/mraerino/atem-go/models"
"github.com/mraerino/atem-go/packet"
"github.com/mraerino/atem-go/packet/cmds"
"github.com/netlify/netlify-commons/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Client struct {
host string
port int
conn *net.UDPConn
log logrus.FieldLogger
started util.AtomicBool
connected util.AtomicBool
sessionID uint32 // atomic value (actually uint16)
seqNum uint32 // atomic counter (actually uint16)
state SwitcherState
stateMutex sync.RWMutex
timeRequests chan chan models.Timecode
}
func NewClient(log logrus.FieldLogger, host string) *Client {
return NewClientWithPort(log, host, 9910)
}
func NewClientWithPort(log logrus.FieldLogger, host string, port int) *Client {
return &Client{
host: host,
port: port,
log: log,
started: util.NewAtomicBool(false),
connected: util.NewAtomicBool(false),
timeRequests: make(chan chan models.Timecode, 100),
}
}
func (c *Client) State() SwitcherState {
c.stateMutex.RLock()
state := c.state
c.stateMutex.RUnlock()
return state
}
var ErrAlreadyStarted = errors.New("Already started")
func (c *Client) Start(ctx context.Context) error {
if c.started.Set(true) {
return ErrAlreadyStarted
}
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(c.host, strconv.Itoa(c.port)))
if err != nil {
return errors.Wrap(err, "Failed to resolve UDP address")
}
c.conn, err = net.DialUDP("udp", nil, addr)
if err != nil {
return errors.Wrap(err, "Failed to open UDP socket")
}
c.log.Debug("Created socket. Starting message processing task...")
connected := c.startHandleMessages(ctx)
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
select {
case err, open := <-connected:
if !open {
return nil
}
return err
case <-ctx.Done():
return ctx.Err()
}
}
var InitPayload = []byte{1, 0, 0, 0, 0, 0, 0, 0}
func (c *Client) sendInit() error {
initMSG := c.makeHeader(packet.FlagInit)
initMSG.Length = 20 // custom well-known length
buf := new(bytes.Buffer)
initMSG.Serialize(buf)
buf.Write(InitPayload)
_, err := c.conn.Write(buf.Bytes())
return err
}
func (c *Client) send(msg *packet.Message) error {
buf := new(bytes.Buffer)
msg.Serialize(buf)
_, err := c.conn.Write(buf.Bytes())
return err
}
func (c *Client) resetSession() {
sessionID := rand.Uint32()
atomic.StoreUint32(&c.sessionID, sessionID)
atomic.StoreUint32(&c.seqNum, 0)
c.connected.Set(false)
// init state
c.state = NewSwitcherState()
}
func (c *Client) startHandleMessages(ctx context.Context) chan error {
errCh := make(chan error)
go func() {
var innerCtx context.Context
var cancel context.CancelFunc
for ctx.Err() == nil {
// cancel previous session
if cancel != nil {
cancel()
}
innerCtx, cancel = context.WithCancel(ctx)
c.resetSession()
var connected bool
c.log.Debug("Sending init packet")
err := c.sendInit()
if err != nil {
errCh <- errors.Wrap(err, "Failed to send init message")
break
}
// ack packets in background
ackQueue := make(chan uint16, 20) // seq nums to ack
go c.ackWorker(innerCtx, ackQueue)
// read loop
var rerr error
for {
raw := make([]byte, 1500) // safe size for expected MTU
// todo: read deadline
n, err := c.conn.Read(raw)
if err != nil {
rerr = err
break
}
c.log.WithField("len", n).Debug("Got packet")
buf := bytes.NewBuffer(raw[:n])
msg, err := packet.Deserialize(c.log, buf)
if err != nil {
rerr = err
break
}
log := c.log.WithField("seq", msg.SeqNum)
log.WithFields(msg.Flags.Debug()).Debug("Read packet")
oldSession := atomic.SwapUint32(&c.sessionID, uint32(msg.SessionID))
if oldSession != uint32(msg.SessionID) {
log.WithField("session", msg.SessionID).Info("Using new session id from switcher")
}
if msg.Flags.Has(packet.FlagRetrans) {
// todo: handle double sends
log.Warn("Retransmission detected")
}
if msg.Flags.Has(packet.FlagInit) {
// always ack init packets
msg.Flags |= packet.FlagNeedACK
}
if msg.Flags.Has(packet.FlagNeedACK) {
log.Debug("queueing packet for ack")
ackQueue <- msg.SeqNum
}
gotInit, err := c.processCommands(log, msg)
if err != nil {
log.WithError(err).Warn("Error while processing commands")
}
if gotInit && !connected {
connected = true
c.connected.Set(true)
errCh <- nil // avoid risk of double-closing
}
}
if rerr != nil {
c.log.WithError(rerr).Warn("Failed to read")
if !connected {
errCh <- errors.Wrap(rerr, "Failed to read message")
}
break
}
}
if cancel != nil {
cancel()
}
_ = c.conn.Close()
c.started.Set(false)
}()
return errCh
}
func (c *Client) processCommands(log logrus.FieldLogger, msg packet.Message) (init bool, err error) {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
for _, cmd := range msg.Commands {
log.WithField("slug", cmd.Slug()).Debug("Starting to process command")
switch t := cmd.(type) {
case *cmds.UnknownCommand:
log.WithField("slug", t.Slug()).Debug("Got unknown command")
case *cmds.IncmCmd:
init = true
case *cmds.VerCmd:
c.state.Version.Major = int(t.Major)
c.state.Version.Minor = int(t.Minor)
log.WithFields(logrus.Fields{"major": t.Major, "minor": t.Minor}).Debug("Got version")
case *cmds.PinCmd:
c.state.Config.ProductName = t.Value()
case *cmds.WarnCmd:
c.state.Warning = t.Value()
case *cmds.TopCmd:
c.state.Config.Topology = models.Topology(*t)
case *cmds.MecCmd:
c.state.Config.MixEffect[int(t.ME)] = int(t.Keyers)
case *cmds.MplCmd:
c.state.Config.MediaPlayer = models.MediaPlayerConfig(*t)
case *cmds.MvcCmd:
c.state.Config.MultiViews = int(*t)
case *cmds.SscCmd:
c.state.Config.SuperSources = int(*t)
case *cmds.TlcCmd:
c.state.Config.TallyChannels = int(*t)
case *cmds.MacCmd:
c.state.Config.MacroBanks = int(*t)
case *cmds.PowrCmd:
c.state.Power = models.PowerStatus(*t)
case *cmds.VidmCmd:
c.state.VideoMode = models.VideoMode(*t)
case *cmds.InprCmd:
c.state.Inputs[t.SourceIndex] = models.InputProperties(*t)
case *cmds.PrgiCmd:
c.state.Program[int(t.Bus)] = t.Source
case *cmds.PrviCmd:
c.state.Preview[int(t.Bus)] = t.Source
case *cmds.AuxsCmd:
c.state.Aux[int(t.Bus)] = t.Source
case *cmds.MpceCmd:
mp, ok := c.state.MediaPlayer[int(t.PlayerIndex)]
if !ok {
mp = &models.MediaPlayer{}
c.state.MediaPlayer[int(t.PlayerIndex)] = mp
}
mp.Type = t.Type
mp.StillIndex = t.StillIndex
mp.ClipIndex = t.ClipIndex
case *cmds.MpfeCmd:
c.state.MediaFiles[int(t.Index)] = models.MediaStillFrame{
Used: t.Used,
Hash: t.Hash,
Filename: t.Filename,
}
case cmds.TlinCmd:
c.state.TallyByIndex = t
case cmds.TlsrCmd:
c.state.TallyBySource = t
case *cmds.TimeCmd:
tc := models.Timecode(*t)
done := false
for !done {
select {
case retCh, open := <-c.timeRequests:
if !open {
done = true
}
retCh <- tc
close(retCh)
default:
// no more in channel
done = true
}
}
c.state.TimeCodeLastChange = tc
default:
log.Debug("Unhandled packet type")
}
}
return
}
const ackDebounceInterval = time.Millisecond * 20
const ackMaxDebounce = time.Millisecond * 100
// since the protocol seems to allow just acking the seq num
// of the packet that was received last, the actual sending
// of the ack is debounced to improve performance
func (c *Client) ackWorker(ctx context.Context, queue <-chan uint16) {
timer := time.NewTimer(ackDebounceInterval)
timer.Stop() // start with a stopped timer
lastSend := time.Now()
var latestNum uint16
for {
log := c.log.WithField("num", latestNum)
select {
case <-ctx.Done():
timer.Stop()
log.WithError(ctx.Err()).Debug("ACK worker stopped")
return
case seqNum, open := <-queue:
if !open {
log.Debug("ACK work channel closed")
return
}
if seqNum > latestNum {
latestNum = seqNum
}
timer.Stop()
if time.Since(lastSend) > ackMaxDebounce {
timer.Reset(0)
} else {
timer.Reset(ackDebounceInterval)
}
case <-timer.C:
log.Debug("Ack timer fired. Sending ack...")
// defer timer fired, actually send ack
msg := c.makeHeader(packet.FlagACK)
msg.AckID = latestNum
err := c.send(msg)
if err != nil {
log.WithError(err).Warn("Failed sending ack")
}
lastSend = time.Now()
}
}
}
func (c *Client) makeHeader(flags ...packet.Flags) *packet.Message {
seqNum := atomic.AddUint32(&c.seqNum, 1) - 1 // allow seq num to start at 0
return &packet.Message{
Flags: packet.FlagsFrom(flags...),
SessionID: uint16(atomic.LoadUint32(&c.sessionID)),
SeqNum: uint16(seqNum),
}
}