forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
557 lines (494 loc) · 14.1 KB
/
engine.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lksdk
import (
"sync"
"time"
"github.com/pion/webrtc/v3"
"go.uber.org/atomic"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit"
)
const (
reliableDataChannelName = "_reliable"
lossyDataChannelName = "_lossy"
maxReconnectCount = 10
initialReconnectInterval = 300 * time.Millisecond
maxReconnectInterval = 60 * time.Second
)
type RTCEngine struct {
publisher *PCTransport
subscriber *PCTransport
client *SignalClient
dclock sync.RWMutex
reliableDC *webrtc.DataChannel
lossyDC *webrtc.DataChannel
reliableDCSub *webrtc.DataChannel
lossyDCSub *webrtc.DataChannel
trackPublishedChan chan *livekit.TrackPublishedResponse
subscriberPrimary bool
hasConnected atomic.Bool
hasPublish atomic.Bool
closed atomic.Bool
reconnecting atomic.Bool
requiresFullReconnect atomic.Bool
url string
token atomic.String
connParams *ConnectParams
JoinTimeout time.Duration
// callbacks
OnDisconnected func()
OnMediaTrack func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver)
OnParticipantUpdate func([]*livekit.ParticipantInfo)
OnActiveSpeakersChanged func([]*livekit.SpeakerInfo)
OnSpeakersChanged func([]*livekit.SpeakerInfo)
OnDataReceived func(userPacket *livekit.UserPacket)
OnConnectionQuality func([]*livekit.ConnectionQualityInfo)
OnRoomUpdate func(room *livekit.Room)
OnRestarting func()
OnRestarted func(*livekit.JoinResponse)
OnResuming func()
OnResumed func()
}
func NewRTCEngine() *RTCEngine {
e := &RTCEngine{
client: NewSignalClient(),
trackPublishedChan: make(chan *livekit.TrackPublishedResponse, 1),
JoinTimeout: 15 * time.Second,
}
e.client.OnParticipantUpdate = func(info []*livekit.ParticipantInfo) {
if f := e.OnParticipantUpdate; f != nil {
f(info)
}
}
e.client.OnSpeakersChanged = func(si []*livekit.SpeakerInfo) {
if f := e.OnSpeakersChanged; f != nil {
f(si)
}
}
e.client.OnLocalTrackPublished = e.handleLocalTrackPublished
e.client.OnConnectionQuality = func(cqi []*livekit.ConnectionQualityInfo) {
if f := e.OnConnectionQuality; f != nil {
f(cqi)
}
}
e.client.OnRoomUpdate = func(room *livekit.Room) {
if f := e.OnRoomUpdate; f != nil {
f(room)
}
}
e.client.OnLeave = e.handleLeave
e.client.OnTokenRefresh = func(refreshToken string) {
e.token.Store(refreshToken)
}
e.client.OnClose = func() { e.handleDisconnect(false) }
return e
}
func (e *RTCEngine) Join(url string, token string, params *ConnectParams) (*livekit.JoinResponse, error) {
res, err := e.client.Join(url, token, params)
if err != nil {
return nil, err
}
e.url = url
e.token.Store(token)
e.connParams = params
if err = e.configure(res); err != nil {
return nil, err
}
e.client.Start()
// send offer
if !res.SubscriberPrimary {
e.publisher.Negotiate()
}
if err = e.waitUntilConnected(); err != nil {
return nil, err
}
e.hasConnected.Store(true)
return res, err
}
func (e *RTCEngine) Close() {
if !e.closed.CompareAndSwap(false, true) {
return
}
go func() {
for e.reconnecting.Load() {
time.Sleep(50 * time.Millisecond)
}
if e.publisher != nil {
_ = e.publisher.Close()
}
if e.subscriber != nil {
_ = e.subscriber.Close()
}
e.client.Close()
}()
}
func (e *RTCEngine) IsConnected() bool {
if e.publisher == nil || e.subscriber == nil {
return false
}
if e.subscriberPrimary {
return e.subscriber.IsConnected()
}
return e.publisher.IsConnected()
}
func (e *RTCEngine) TrackPublishedChan() <-chan *livekit.TrackPublishedResponse {
return e.trackPublishedChan
}
func (e *RTCEngine) setRTT(rtt uint32) {
if pc := e.publisher; pc != nil {
pc.SetRTT(rtt)
}
if pc := e.subscriber; pc != nil {
pc.SetRTT(rtt)
}
}
func (e *RTCEngine) configure(res *livekit.JoinResponse) error {
iceServers := FromProtoIceServers(res.IceServers)
configuration := webrtc.Configuration{ICEServers: iceServers}
if res.GetClientConfiguration().GetForceRelay() == livekit.ClientConfigSetting_ENABLED {
configuration.ICETransportPolicy = webrtc.ICETransportPolicyRelay
}
var err error
if e.publisher, err = NewPCTransport(PCTransportParams{
Configuration: configuration,
RetransmitBufferSize: e.connParams.RetransmitBufferSize,
Pacer: e.connParams.Pacer,
}); err != nil {
return err
}
if e.subscriber, err = NewPCTransport(PCTransportParams{
Configuration: configuration,
RetransmitBufferSize: e.connParams.RetransmitBufferSize,
}); err != nil {
return err
}
logger.Debugw("Using ICE servers", "servers", iceServers)
e.subscriberPrimary = res.SubscriberPrimary
e.subscriber.OnRemoteDescriptionSettled(e.createPublisherAnswerAndSend)
e.publisher.pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil {
// done
return
}
if err := e.client.SendICECandidate(candidate.ToJSON(), livekit.SignalTarget_PUBLISHER); err != nil {
logger.Errorw("could not send ICE candidates for publisher", err)
}
})
e.subscriber.pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil {
// done
return
}
if err := e.client.SendICECandidate(candidate.ToJSON(), livekit.SignalTarget_SUBSCRIBER); err != nil {
logger.Errorw("could not send ICE candidates for subscriber", err)
}
})
primaryTransport := e.publisher
if res.SubscriberPrimary {
primaryTransport = e.subscriber
}
primaryTransport.pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
switch state {
case webrtc.ICEConnectionStateConnected:
var fields []interface{}
if pair, err := primaryTransport.GetSelectedCandidatePair(); err == nil {
fields = append(fields, "iceCandidatePair", pair)
}
logger.Debugw("ICE connected", fields...)
case webrtc.ICEConnectionStateDisconnected:
logger.Debugw("ICE disconnected")
case webrtc.ICEConnectionStateFailed:
logger.Debugw("ICE failed")
e.handleDisconnect(false)
}
})
e.subscriber.pc.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
if e.OnMediaTrack != nil {
e.OnMediaTrack(remote, receiver)
}
})
e.subscriber.pc.OnDataChannel(func(c *webrtc.DataChannel) {
e.dclock.Lock()
defer e.dclock.Unlock()
if c.Label() == reliableDataChannelName {
e.reliableDCSub = c
} else if c.Label() == lossyDataChannelName {
e.lossyDCSub = c
} else {
return
}
c.OnMessage(e.handleDataPacket)
})
e.publisher.OnOffer = func(offer webrtc.SessionDescription) {
e.hasPublish.Store(true)
if err := e.client.SendOffer(offer); err != nil {
logger.Errorw("could not send offer", err)
}
}
trueVal := true
maxRetries := uint16(1)
e.dclock.Lock()
e.lossyDC, err = e.publisher.PeerConnection().CreateDataChannel(lossyDataChannelName, &webrtc.DataChannelInit{
Ordered: &trueVal,
MaxRetransmits: &maxRetries,
})
if err != nil {
e.dclock.Unlock()
return err
}
e.lossyDC.OnMessage(e.handleDataPacket)
e.reliableDC, err = e.publisher.PeerConnection().CreateDataChannel(reliableDataChannelName, &webrtc.DataChannelInit{
Ordered: &trueVal,
})
if err != nil {
e.dclock.Unlock()
return err
}
e.reliableDC.OnMessage(e.handleDataPacket)
e.dclock.Unlock()
// configure client
e.client.OnAnswer = func(sd webrtc.SessionDescription) {
if err := e.publisher.SetRemoteDescription(sd); err != nil {
logger.Errorw("could not set remote description", err)
} else {
logger.Debugw("successfully set publisher answer")
}
}
e.client.OnTrickle = func(init webrtc.ICECandidateInit, target livekit.SignalTarget) {
var err error
if target == livekit.SignalTarget_PUBLISHER {
err = e.publisher.AddICECandidate(init)
} else if target == livekit.SignalTarget_SUBSCRIBER {
err = e.subscriber.AddICECandidate(init)
}
if err != nil {
logger.Errorw("could not add ICE candidate", err)
}
}
e.client.OnOffer = func(sd webrtc.SessionDescription) {
logger.Debugw("received offer for subscriber")
if err := e.subscriber.SetRemoteDescription(sd); err != nil {
logger.Errorw("could not set remote description", err)
return
}
}
return nil
}
func (e *RTCEngine) GetDataChannel(kind livekit.DataPacket_Kind) *webrtc.DataChannel {
e.dclock.RLock()
defer e.dclock.RUnlock()
if kind == livekit.DataPacket_RELIABLE {
return e.reliableDC
}
return e.lossyDC
}
func (e *RTCEngine) GetDataChannelSub(kind livekit.DataPacket_Kind) *webrtc.DataChannel {
e.dclock.RLock()
defer e.dclock.RUnlock()
if kind == livekit.DataPacket_RELIABLE {
return e.reliableDCSub
}
return e.lossyDCSub
}
func (e *RTCEngine) waitUntilConnected() error {
timeout := time.After(e.JoinTimeout)
for {
select {
case <-timeout:
return ErrConnectionTimeout
case <-time.After(10 * time.Millisecond):
if e.IsConnected() {
e.requiresFullReconnect.Store(false)
return nil
}
}
}
}
func (e *RTCEngine) ensurePublisherConnected(ensureDataReady bool) error {
if !e.subscriberPrimary {
return e.waitUntilConnected()
}
if e.publisher.IsConnected() && (!ensureDataReady || e.dataPubChannelReady()) {
return nil
}
e.publisher.Negotiate()
timeout := time.After(e.JoinTimeout)
for {
select {
case <-timeout:
return ErrConnectionTimeout
case <-time.After(10 * time.Millisecond):
if e.publisher.IsConnected() {
if !ensureDataReady || e.dataPubChannelReady() {
return nil
}
}
}
}
}
func (e *RTCEngine) dataPubChannelReady() bool {
e.dclock.RLock()
defer e.dclock.RUnlock()
return e.reliableDC.ReadyState() == webrtc.DataChannelStateOpen && e.lossyDC.ReadyState() == webrtc.DataChannelStateOpen
}
func (e *RTCEngine) handleLocalTrackPublished(res *livekit.TrackPublishedResponse) {
e.trackPublishedChan <- res
}
func (e *RTCEngine) handleDataPacket(msg webrtc.DataChannelMessage) {
packet, err := e.readDataPacket(msg)
if err != nil {
return
}
switch msg := packet.Value.(type) {
case *livekit.DataPacket_Speaker:
if e.OnActiveSpeakersChanged != nil {
e.OnActiveSpeakersChanged(msg.Speaker.Speakers)
}
case *livekit.DataPacket_User:
if e.OnDataReceived != nil {
e.OnDataReceived(msg.User)
}
}
}
func (e *RTCEngine) readDataPacket(msg webrtc.DataChannelMessage) (*livekit.DataPacket, error) {
dataPacket := &livekit.DataPacket{}
if msg.IsString {
err := protojson.Unmarshal(msg.Data, dataPacket)
return dataPacket, err
}
err := proto.Unmarshal(msg.Data, dataPacket)
return dataPacket, err
}
func (e *RTCEngine) handleDisconnect(fullReconnect bool) {
// do not retry until fully connected
if e.closed.Load() || !e.hasConnected.Load() {
return
}
if !e.reconnecting.CompareAndSwap(false, true) {
if fullReconnect {
e.requiresFullReconnect.Store(true)
}
return
}
go func() {
defer e.reconnecting.Store(false)
for reconnectCount := 0; reconnectCount < maxReconnectCount && !e.closed.Load(); reconnectCount++ {
if e.requiresFullReconnect.Load() {
fullReconnect = true
}
if fullReconnect {
if reconnectCount == 0 && e.OnRestarting != nil {
e.OnRestarting()
}
logger.Infow("restarting connection...", "reconnectCount", reconnectCount)
if err := e.restartConnection(); err != nil {
logger.Errorw("restart connection failed", err)
} else {
return
}
} else {
if reconnectCount == 0 && e.OnResuming != nil {
e.OnResuming()
}
logger.Infow("resuming connection...", "reconnectCount", reconnectCount)
if err := e.resumeConnection(); err != nil {
logger.Errorw("resume connection failed", err)
} else {
return
}
}
delay := time.Duration(reconnectCount*reconnectCount) * initialReconnectInterval
if delay > maxReconnectInterval {
break
}
if reconnectCount < maxReconnectCount-1 {
time.Sleep(delay)
}
}
if e.OnDisconnected != nil {
e.OnDisconnected()
}
}()
}
func (e *RTCEngine) resumeConnection() error {
_, err := e.client.Join(e.url, e.token.Load(), &ConnectParams{Reconnect: true})
if err != nil {
return err
}
e.client.Start()
// send offer if publisher enabled
if !e.subscriberPrimary || e.hasPublish.Load() {
e.publisher.createAndSendOffer(&webrtc.OfferOptions{
ICERestart: true,
})
}
if err = e.waitUntilConnected(); err != nil {
return err
}
if e.OnResumed != nil {
e.OnResumed()
}
return nil
}
func (e *RTCEngine) restartConnection() error {
if e.client.IsStarted() {
e.client.SendLeave()
}
e.client.Close()
if e.publisher != nil {
e.publisher.Close()
}
if e.subscriber != nil {
e.subscriber.Close()
}
res, err := e.Join(e.url, e.token.Load(), e.connParams)
if err != nil {
return err
}
if e.OnRestarted != nil {
e.OnRestarted(res)
}
return nil
}
func (e *RTCEngine) createPublisherAnswerAndSend() error {
answer, err := e.subscriber.pc.CreateAnswer(nil)
if err != nil {
logger.Errorw("could not create answer", err)
return err
}
if err := e.subscriber.pc.SetLocalDescription(answer); err != nil {
logger.Errorw("could not set subscriber local description", err)
return err
}
if err := e.client.SendAnswer(answer); err != nil {
logger.Errorw("could not send answer for subscriber", err)
return err
}
return nil
}
func (e *RTCEngine) handleLeave(leave *livekit.LeaveRequest) {
if leave.GetCanReconnect() {
e.handleDisconnect(true)
} else {
logger.Infow("server initiated leave",
"reason", leave.GetReason(),
"canReconnect", leave.GetCanReconnect(),
)
if e.OnDisconnected != nil {
e.OnDisconnected()
}
}
}