-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtrx.go
482 lines (396 loc) · 10.5 KB
/
trx.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package trx
import (
"fmt"
"log"
"sync"
"time"
"github.com/dh1tw/remoteAudio/audio/nodes/vox"
"github.com/dh1tw/remoteAudio/audio/sinks/pbWriter"
"github.com/dh1tw/remoteAudio/audio/sources/pbReader"
"github.com/asim/go-micro/v3/broker"
"github.com/dh1tw/remoteAudio/audio/chain"
"github.com/dh1tw/remoteAudio/proxy"
)
// Trx is a data structure which holds the components needed for a 2-way
// radio. It holds the available audio servers, the communication means,
// the audio rx and tx chains etc.
type Trx struct {
sync.RWMutex
rx *chain.Chain
tx *chain.Chain
fromNetwork *pbReader.PbReader
toNetwork *pbWriter.PbWriter
broker broker.Broker
servers map[string]*proxy.AudioServer
rxAudioSub broker.Subscriber
curServer *proxy.AudioServer
pttActive bool
voxActive bool
vox *vox.Vox
notifyServerChangeCb func()
}
// Options is the data structure holding the values used for instantiating
// a Trx object. This struct has to be provided the the object constructor.
type Options struct {
Rx *chain.Chain
Tx *chain.Chain
FromNetwork *pbReader.PbReader
ToNetwork *pbWriter.PbWriter
Broker broker.Broker
Vox *vox.Vox
}
// NewTrx is the constructor method of a Trx object.
func NewTrx(opts Options) (*Trx, error) {
if opts.Rx == nil {
return nil, fmt.Errorf("rx variable is nil")
}
if opts.Tx == nil {
return nil, fmt.Errorf("tx variable is nil")
}
if opts.FromNetwork == nil {
return nil, fmt.Errorf("fromNetwork sink is nil")
}
if opts.ToNetwork == nil {
return nil, fmt.Errorf("toNetwork sink is nil")
}
if opts.Broker == nil {
return nil, fmt.Errorf("broker is nil")
}
trx := &Trx{
rx: opts.Rx,
tx: opts.Tx,
fromNetwork: opts.FromNetwork,
toNetwork: opts.ToNetwork,
broker: opts.Broker,
vox: opts.Vox,
servers: make(map[string]*proxy.AudioServer),
}
trx.toNetwork.SetToWireCb(trx.toWireCb)
return trx, nil
}
// SetNotifyServerChangeCb allows to set a callback which get's executed
// when a remote audio server changes / disappears.
func (x *Trx) SetNotifyServerChangeCb(f func()) {
x.Lock()
defer x.Unlock()
x.notifyServerChangeCb = f
}
// AddServer adds a remote audio server, represented through a proxy object.
func (x *Trx) AddServer(asvr *proxy.AudioServer) {
x.Lock()
defer x.Unlock()
if asvr == nil {
return
}
_, ok := x.servers[asvr.Name()]
if ok {
return
}
x.servers[asvr.Name()] = asvr
asvr.SetNotifyCb(x.onAudioServersChanged)
go x.onAudioServersChanged()
log.Println("added audio server", asvr.Name())
}
// onAudioServersChanged will execute a callback to inform the parent
// application that a remote audio server has changed.
func (x *Trx) onAudioServersChanged() {
x.RLock()
defer x.RUnlock()
if x.notifyServerChangeCb != nil {
go x.notifyServerChangeCb()
}
}
// Server returns a particular AudioServer. If no
// AudioServer exists with that name, (nil, false) will be returned.
func (x *Trx) Server(name string) (*proxy.AudioServer, bool) {
x.RLock()
defer x.RUnlock()
svr, ok := x.servers[name]
return svr, ok
}
// Servers returns a list with the names of the registered
// audio servers.
func (x *Trx) Servers() []string {
x.RLock()
defer x.RUnlock()
list := []string{}
for _, aServer := range x.servers {
list = append(list, aServer.Name())
}
return list
}
// RemoveServer removes a remote audio server from the Trx.
func (x *Trx) RemoveServer(asName string) error {
x.Lock()
defer x.Unlock()
as, ok := x.servers[asName]
if !ok {
return fmt.Errorf("unable to remove unknown audio server: %v", asName)
}
delete(x.servers, asName)
if as.Name() == x.curServer.Name() && len(x.servers) > 0 {
for _, svr := range x.servers {
go x.SelectServer(svr.Name()) // must be async to avoid deadlock
}
} else if len(x.servers) == 0 {
x.curServer = nil
if err := x.tx.Enable(false); err != nil {
log.Println(err) // better fatal?
}
}
log.Println("removed audio server", as.Name())
as.Close()
// emit event
go x.onAudioServersChanged()
return nil
}
// SelectServer selects a particular remote audio server from which the
// audio will be received / sent to.
func (x *Trx) SelectServer(name string) error {
x.Lock()
defer x.Unlock()
newSvr, ok := x.servers[name]
if !ok {
return fmt.Errorf("unknown audio server: %v", name)
}
if x.rxAudioSub != nil {
if err := x.rxAudioSub.Unsubscribe(); err != nil {
return fmt.Errorf("select server unsubscribe: %v", err)
}
}
x.curServer = newSvr
sub, err := x.broker.Subscribe(newSvr.RxAddress(), x.fromWireCb)
if err != nil {
return fmt.Errorf("SelectServer subscribe: %v", err)
}
x.rxAudioSub = sub
return nil
}
// SelectedServer returns the name of the currently selected Audio Server.
func (x *Trx) SelectedServer() string {
x.RLock()
defer x.RUnlock()
if x.curServer != nil {
return x.curServer.Name()
}
return ""
}
// SetRxVolume sets the volume of the local speakers.
func (x *Trx) SetRxVolume(vol float32) error {
x.Lock()
defer x.Unlock()
speaker, _, err := x.rx.Sinks.Sink("speaker")
if err != nil {
return err
}
speaker.SetVolume(vol)
return nil
}
// RxVolume returns the currently set volume for the local speakers.
func (x *Trx) RxVolume() (float32, error) {
x.Lock()
defer x.Unlock()
speaker, _, err := x.rx.Sinks.Sink("speaker")
if err != nil {
return 0, err
}
return speaker.Volume(), nil
}
// SetTxVolume sets the volume of the audio sent to the remote audio server.
func (x *Trx) SetTxVolume(vol float32) error {
x.Lock()
defer x.Unlock()
toNetwork, _, err := x.tx.Sinks.Sink("toNetwork")
if err != nil {
return err
}
toNetwork.SetVolume(vol)
return nil
}
// TxVolume returns the current volume level for the audio sent to the remote audio server.
func (x *Trx) TxVolume() (float32, error) {
x.Lock()
defer x.Unlock()
toNetwork, _, err := x.tx.Sinks.Sink("toNetwork")
if err != nil {
return 0, err
}
return toNetwork.Volume(), nil
}
// SetVOX sets the vox. This method should not be exposed through the
// REST API.
func (x *Trx) SetVOX(voxState bool) error {
x.Lock()
defer x.Unlock()
if x.voxActive == voxState {
return nil
}
x.voxActive = voxState
// already sending audio since PTT is active
if x.pttActive && x.voxActive {
return nil
}
if x.voxActive {
return x.setTxState(true)
}
// don't disable the audio stream since PTT is still active
if x.pttActive && !x.voxActive {
return nil
}
// !x.voxEnabled
return x.setTxState(false)
}
// VOX returns if the VOX is currently active
func (x *Trx) VOX() bool {
x.Lock()
defer x.Unlock()
return x.voxActive
}
// VOXEnabled indicates if the vox is enabled
func (x *Trx) VOXEnabled() bool {
x.Lock()
defer x.Unlock()
return x.vox.Enabled()
}
// SetVOXEnabled enables the vox in the tx chain
func (x *Trx) SetVOXEnabled(enable bool) {
x.Lock()
defer x.Unlock()
x.vox.Enable(enable)
}
// VOXThreshold returns the vox threshold level
func (x *Trx) VOXThreshold() float32 {
x.Lock()
defer x.Unlock()
return x.vox.Threshold()
}
// SetVOXThreshold sets the vox threshold level
func (x *Trx) SetVOXThreshold(v float32) {
x.Lock()
defer x.Unlock()
x.vox.SetThreshold(v)
}
// VOXHoldTime returns the vox hold time
func (x *Trx) VOXHoldTime() time.Duration {
x.Lock()
defer x.Unlock()
return x.vox.Holdtime()
}
//SetVOXHoldTime sets the vox hold time
func (x *Trx) SetVOXHoldTime(t time.Duration) {
x.Lock()
defer x.Unlock()
x.vox.SetHoldTime(t)
}
// SetPTT (Push To Talk) turns on/off the audio stream sent to the remote
// audio server. In case VOX is active, the application will continue
// streaming audio to the server.
func (x *Trx) SetPTT(pttState bool) error {
x.Lock()
defer x.Unlock()
if x.pttActive == pttState {
return nil
}
x.pttActive = pttState
// already sending audio since VOX is active
if x.pttActive && x.voxActive {
return nil
}
if x.pttActive {
return x.setTxState(true)
}
// don't disable the audio stream since VOX is still active
if !x.pttActive && x.voxActive {
return nil
}
// !x.pttEnabled
return x.setTxState(false)
}
// SetTxState turns on/off the audio stream sent to the remote audio server.
// It can be considered PTT (Push To Talk). This method is not safe for
// concurrent access.
func (x *Trx) setTxState(on bool) error {
if x.curServer == nil {
return nil
}
if on {
return x.tx.Enable(true)
}
return x.tx.Enable(false)
}
// SetRxState turns on/off the audio stream sent from the remote audio server.
func (x *Trx) SetRxState(on bool) error {
x.Lock()
defer x.Unlock()
if x.curServer == nil {
return fmt.Errorf("no audio server selected")
}
var err error
if on {
err = x.curServer.StartRxStream()
} else {
err = x.curServer.StopRxStream()
}
return err
}
// TxState returns a boolean if audio is currently sent to the remote
// audio server, or not.
func (x *Trx) TxState() (bool, error) {
x.Lock()
defer x.Unlock()
_, state, err := x.tx.Sinks.Sink("toNetwork")
if err != nil {
return false, err
}
return state, nil
}
// RxState returns a boolean if the remote audio server is streaming
// audio or not.
func (x *Trx) RxState() (bool, error) {
x.Lock()
defer x.Unlock()
if x.curServer == nil {
return false, fmt.Errorf("no audio server selected")
}
return x.curServer.RxOn(), nil
}
// TxUser returns the current user from which the remote audio server
// is receiving audio. If nobody is transmitting / sending audio to the
// remote audio server, an empty string will be returned.
func (x *Trx) TxUser() (string, error) {
x.Lock()
defer x.Unlock()
if x.curServer == nil {
return "", fmt.Errorf("no audio server selected")
}
return x.curServer.TxUser(), nil
}
// fromWireCb is a callback that is executed when audio is received
// from the network. It will typically then enqueue the received data
// into an audio source / chain.
func (x *Trx) fromWireCb(pub broker.Event) error {
x.RLock()
defer x.RUnlock()
if x.fromNetwork == nil {
return nil
}
return x.fromNetwork.Enqueue(pub.Message().Body)
}
// toWireCb is a callback that is executed when audio is ready to
// be sent to the audio server. Typically this callback is called from
// an audio sync (e.g. pbWriter).
func (x *Trx) toWireCb(data []byte) {
// Callback which is called by pbWriter to push the audio
// packets to the network
// NO MUTEX! (causes deadlock)
msg := &broker.Message{
Body: data,
}
if x.curServer == nil {
return
}
err := x.broker.Publish(x.curServer.TxAddress(), msg)
if err != nil {
log.Fatal("toWireCb:", err)
}
}