forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
299 lines (267 loc) · 7.73 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
package lksdk
import (
"time"
"github.com/livekit/protocol/livekit"
"github.com/pion/webrtc/v3"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
const reliableDataChannelName = "_reliable"
const lossyDataChannelName = "_lossy"
type RTCEngine struct {
publisher *PCTransport
subscriber *PCTransport
client *SignalClient
reliableDC *webrtc.DataChannel
lossyDC *webrtc.DataChannel
reliableDCSub *webrtc.DataChannel
lossyDCSub *webrtc.DataChannel
trackPublishedChan chan *livekit.TrackPublishedResponse
subscriberPrimary bool
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)
}
func NewRTCEngine() *RTCEngine {
return &RTCEngine{
client: NewSignalClient(),
trackPublishedChan: make(chan *livekit.TrackPublishedResponse, 1),
JoinTimeout: 15 * time.Second,
}
}
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
}
if err = e.configure(res); err != nil {
return nil, err
}
// send offer
if !res.SubscriberPrimary {
e.publisher.Negotiate()
}
if err = e.waitUntilConnected(); err != nil {
return nil, err
}
return res, err
}
func (e *RTCEngine) Close() {
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) configure(res *livekit.JoinResponse) error {
iceServers := FromProtoIceServers(res.IceServers)
var err error
if e.publisher, err = NewPCTransport(iceServers); err != nil {
return err
}
if e.subscriber, err = NewPCTransport(iceServers); err != nil {
return err
}
e.subscriberPrimary = res.SubscriberPrimary
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.Error(err, "could not send ICE candidates for publisher")
}
})
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.Error(err, "could not send ICE candidates for subscriber")
}
})
primaryPC := e.publisher.pc
if res.SubscriberPrimary {
primaryPC = e.subscriber.pc
}
primaryPC.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
switch state {
case webrtc.ICEConnectionStateConnected:
logger.Info("ICE connected")
case webrtc.ICEConnectionStateDisconnected:
logger.Info("ICE disconnected")
if e.OnDisconnected != nil {
e.OnDisconnected()
}
}
})
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) {
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) {
if err := e.client.SendOffer(offer); err != nil {
logger.Error(err, "could not send offer")
}
}
trueVal := true
maxRetries := uint16(1)
e.lossyDC, err = e.publisher.PeerConnection().CreateDataChannel(lossyDataChannelName, &webrtc.DataChannelInit{
Ordered: &trueVal,
MaxRetransmits: &maxRetries,
})
if err != nil {
return err
}
e.lossyDC.OnMessage(e.handleDataPacket)
e.reliableDC, err = e.publisher.PeerConnection().CreateDataChannel(reliableDataChannelName, &webrtc.DataChannelInit{
Ordered: &trueVal,
})
if err != nil {
return err
}
e.reliableDC.OnMessage(e.handleDataPacket)
// configure client
e.client.OnAnswer = func(sd webrtc.SessionDescription) {
if err := e.publisher.SetRemoteDescription(sd); err != nil {
logger.Error(err, "could not set remote description")
} else {
logger.Info("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.Error(err, "could not add ICE candidate")
}
}
e.client.OnOffer = func(sd webrtc.SessionDescription) {
logger.Info("received offer for subscriber")
if err := e.subscriber.SetRemoteDescription(sd); err != nil {
logger.Error(err, "could not set remote description")
return
}
answer, err := e.subscriber.pc.CreateAnswer(nil)
if err != nil {
logger.Error(err, "could not create answer")
return
}
if err := e.subscriber.pc.SetLocalDescription(answer); err != nil {
logger.Error(err, "could not set subscriber local description")
return
}
if err := e.client.SendAnswer(answer); err != nil {
logger.Error(err, "could not send answer for subscriber")
}
}
e.client.OnParticipantUpdate = e.OnParticipantUpdate
e.client.OnSpeakersChanged = e.OnSpeakersChanged
e.client.OnLocalTrackPublished = e.handleLocalTrackPublished
e.client.OnConnectionQuality = e.OnConnectionQuality
e.client.OnRoomUpdate = e.OnRoomUpdate
e.client.OnLeave = e.OnDisconnected
e.client.OnClose = func() {
// TODO: implement reconnection logic
logger.Info("signal connection disconnected")
}
return nil
}
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() {
return nil
}
}
}
}
func (e *RTCEngine) ensurePublisherConnected() error {
if !e.subscriberPrimary {
return e.waitUntilConnected()
}
if e.publisher.IsConnected() {
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() {
return nil
}
}
}
}
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
}