/
action_tap.go
461 lines (340 loc) · 8.95 KB
/
action_tap.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
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
package signalwire
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
)
// TapState keeps the state of a tap action
type TapState int
// Tap state constants
const (
TapTapping TapState = iota
TapFinished
)
func (s TapState) String() string {
return [...]string{"Tappping", "Finished"}[s]
}
// TapDeviceType TODO DESCRIPTION
type TapDeviceType int
// Tap state constants
const (
TapRTP TapDeviceType = iota
TapWS
)
func (s TapDeviceType) String() string {
return [...]string{"rtp", "ws"}[s]
}
// TapDirection TODO DESCRIPTION
type TapDirection int
// Tap state constants
const (
TapDirectionListen TapDirection = iota
TapDirectionSpeak
TapDirectionBoth
)
func (s TapDirection) String() string {
return [...]string{"listen", "speak", "both"}[s]
}
// TapType TODO DESCRIPTION
type TapType int
// Tap state constants
const (
TapAudio TapType = iota
)
func (s TapType) String() string {
return [...]string{"audio"}[s]
}
// Tap TODO DESCRIPTION
type Tap struct {
TapType TapType
TapDirection TapDirection
TapDeviceType TapDeviceType
}
// TapResult TODO DESCRIPTION
type TapResult struct {
Successful bool
SourceDevice TapDevice
DestinationDevice TapDevice
Tap Tap
Event json.RawMessage
}
// TapAction TODO DESCRIPTION
type TapAction struct {
CallObj *CallObj
ControlID string
Completed bool
Result TapResult
State TapState
Payload *json.RawMessage
err error
done chan bool
sync.RWMutex
}
// ITapAction TODO DESCRIPTION
type ITapAction interface {
tapAsyncStop() error
Stop()
GetCompleted() bool
GetResult() TapResult
GetTap() Tap
GetSourceDevice() TapDevice
GetDestinationDevice() TapDevice
}
// TapAudio TODO DESCRIPTION
func (callobj *CallObj) TapAudio(direction fmt.Stringer, tapdev *TapDevice) (*TapResult, error) {
a := new(TapAction)
if callobj.Calling == nil {
return &a.Result, errors.New("nil Calling object")
}
if callobj.Calling.Relay == nil {
return &a.Result, errors.New("nil Relay object")
}
ctrlID, _ := GenUUIDv4()
var err error
a.Result.SourceDevice, err = callobj.Calling.Relay.RelayTapAudio(callobj.Calling.Ctx, callobj.call, ctrlID, direction.String(), tapdev, nil)
if err != nil {
return &a.Result, err
}
callobj.callbacksRunTap(callobj.Calling.Ctx, ctrlID, a, true)
return &a.Result, nil
}
// TapStop TODO DESCRIPTION
func (callobj *CallObj) TapStop(ctrlID *string) error {
if callobj.Calling == nil {
return errors.New("nil Calling object")
}
if callobj.Calling.Relay == nil {
return errors.New("nil Relay object")
}
return callobj.Calling.Relay.RelayTapStop(callobj.Calling.Ctx, callobj.call, ctrlID, nil)
}
// callbacksRunTap TODO DESCRIPTION
func (callobj *CallObj) callbacksRunTap(ctx context.Context, ctrlID string, res *TapAction, norunCB bool) {
var out bool
timer := time.NewTimer(BroadcastEventTimeout * time.Second)
for {
select {
case <-timer.C:
out = true
// get tap states
case tapstate := <-callobj.call.CallTapChans[ctrlID]:
res.RLock()
prevstate := res.State
res.RUnlock()
switch tapstate {
case TapFinished:
res.Lock()
res.State = tapstate
res.Result.Successful = true
res.Completed = true
res.Unlock()
Log.Debug("Tap finished. ctrlID: %s res [%p] Completed [%v] Successful [%v]\n", ctrlID, res, res.Completed, res.Result.Successful)
out = true
if callobj.OnTapFinished != nil && !norunCB {
callobj.OnTapFinished(res)
}
case TapTapping:
timer.Reset(MaxCallDuration * time.Second)
res.Lock()
res.State = tapstate
res.Unlock()
Log.Debug("Tapping. ctrlID: %s\n", ctrlID)
if callobj.OnTapTapping != nil && !norunCB {
callobj.OnTapTapping(res)
}
default:
Log.Debug("Unknown state. ctrlID: %s\n", ctrlID)
}
if prevstate != tapstate && callobj.OnTapStateChange != nil && !norunCB {
callobj.OnTapStateChange(res)
}
case params := <-callobj.call.CallTapEventChans[ctrlID]:
Log.Debug("got params for ctrlID : %s %v\n", ctrlID, params)
res.Lock()
switch params.Tap.Type {
case "audio":
res.Result.Tap.TapType = TapAudio
switch params.Tap.Params.Direction {
case "listen":
res.Result.Tap.TapDirection = TapDirectionListen
case "speak":
res.Result.Tap.TapDirection = TapDirectionSpeak
default:
res.err = errors.New("invalid tap direction")
res.Completed = true
out = true
goto ready
}
default:
res.err = errors.New("invalid tap type")
res.Completed = true
out = true
goto ready
}
switch params.Device.Type {
case "rtp":
res.Result.Tap.TapDeviceType = TapRTP
case "ws":
res.Result.Tap.TapDeviceType = TapWS
default:
res.err = errors.New("invalid tap device type")
res.Completed = true
out = true
goto ready
}
res.Result.DestinationDevice.Params.Addr = params.Device.Params.Addr
res.Result.DestinationDevice.Params.Port = params.Device.Params.Port
res.Result.DestinationDevice.Params.Codec = params.Device.Params.Codec
res.Result.DestinationDevice.Params.Ptime = params.Device.Params.Ptime
res.Result.DestinationDevice.Params.Rate = params.Device.Params.Rate
ready:
res.Unlock()
callobj.call.CallTapReadyChans[ctrlID] <- struct{}{}
case rawEvent := <-callobj.call.CallTapRawEventChans[ctrlID]:
res.Lock()
res.Result.Event = *rawEvent
res.Unlock()
callobj.call.CallTapReadyChans[ctrlID] <- struct{}{}
case <-callobj.call.Hangup:
out = true
case <-ctx.Done():
out = true
}
if out {
if !norunCB {
res.done <- res.Result.Successful
}
break
}
}
}
// TapAudioAsync TODO DESCRIPTION
func (callobj *CallObj) TapAudioAsync(direction fmt.Stringer, tapdev *TapDevice) (*TapAction, error) {
res := new(TapAction)
if callobj.Calling == nil {
return res, errors.New("nil Calling object")
}
if callobj.Calling.Relay == nil {
return res, errors.New("nil Relay object")
}
res.CallObj = callobj
done := make(chan struct{}, 1)
go func() {
go func() {
res.done = make(chan bool, 2)
// wait to get control ID (buffered channel)
ctrlID := <-callobj.call.CallTapControlIDs
callobj.callbacksRunTap(callobj.Calling.Ctx, ctrlID, res, false)
}()
newCtrlID, _ := GenUUIDv4()
res.Lock()
res.ControlID = newCtrlID
res.Unlock()
srcDevice, err := callobj.Calling.Relay.RelayTapAudio(callobj.Calling.Ctx, callobj.call, newCtrlID, direction.String(), tapdev, &res.Payload)
if err != nil {
res.Lock()
res.err = err
res.Completed = true
res.Unlock()
} else {
res.Lock()
res.Result.SourceDevice = srcDevice
res.Unlock()
}
done <- struct{}{}
}()
<-done
return res, res.err
}
// ctrlIDCopy TODO DESCRIPTION
func (tapaction *TapAction) ctrlIDCopy() (string, error) {
tapaction.RLock()
if len(tapaction.ControlID) == 0 {
tapaction.RUnlock()
return "", errors.New("no controlID")
}
c := tapaction.ControlID
tapaction.RUnlock()
return c, nil
}
// tapAsyncStop TODO DESCRIPTION
func (tapaction *TapAction) tapAsyncStop() error {
if tapaction.CallObj.Calling == nil {
return errors.New("nil Calling object")
}
if tapaction.CallObj.Calling.Relay == nil {
return errors.New("nil Relay object")
}
c, err := tapaction.ctrlIDCopy()
if err != nil {
return err
}
call := tapaction.CallObj.call
return tapaction.CallObj.Calling.Relay.RelayTapStop(tapaction.CallObj.Calling.Ctx, call, &c, &tapaction.Payload)
}
// Stop TODO DESCRIPTION
func (tapaction *TapAction) Stop() StopResult {
res := new(StopResult)
tapaction.err = tapaction.tapAsyncStop()
if tapaction.err == nil {
waitStop(res, tapaction.done)
}
return *res
}
// GetCompleted TODO DESCRIPTION
func (tapaction *TapAction) GetCompleted() bool {
tapaction.RLock()
ret := tapaction.Completed
tapaction.RUnlock()
return ret
}
// GetResult TODO DESCRIPTION
func (tapaction *TapAction) GetResult() TapResult {
tapaction.RLock()
ret := tapaction.Result
tapaction.RUnlock()
return ret
}
// GetState TODO DESCRIPTION
func (tapaction *TapAction) GetState() TapState {
tapaction.RLock()
ret := tapaction.State
tapaction.RUnlock()
return ret
}
// GetTap TODO DESCRIPTION
func (tapaction *TapAction) GetTap() *Tap {
return &tapaction.Result.Tap
}
// GetSourceDevice TODO DESCRIPTION
func (tapaction *TapAction) GetSourceDevice() *TapDevice {
return &tapaction.Result.SourceDevice
}
// GetDestinationDevice TODO DESCRIPTION
func (tapaction *TapAction) GetDestinationDevice() *TapDevice {
return &tapaction.Result.DestinationDevice
}
// GetEvent TODO DESCRIPTION
func (tapaction *TapAction) GetEvent() *json.RawMessage {
tapaction.RLock()
ret := &tapaction.Result.Event
tapaction.RUnlock()
return ret
}
// GetPayload TODO DESCRIPTION
func (tapaction *TapAction) GetPayload() *json.RawMessage {
tapaction.RLock()
ret := tapaction.Payload
tapaction.RUnlock()
return ret
}
// GetControlID TODO DESCRIPTION
func (tapaction *TapAction) GetControlID() string {
tapaction.RLock()
ret := tapaction.ControlID
tapaction.RUnlock()
return ret
}