-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsfu.go
464 lines (372 loc) · 11.2 KB
/
sfu.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
package sfu
import (
"context"
"sync"
"time"
"github.com/pion/logging"
"github.com/pion/rtp"
"github.com/pion/webrtc/v4"
"golang.org/x/exp/slices"
)
// BitrateConfigs is the configuration for the bitrate that will be used for adaptive bitrates controller
// The paramenter is in bps (bit per second) for non pixels parameters.
// For pixels parameters, it is total pixels (width * height) of the video.
// High, Mid, and Low are the references for bitrate controller to decide the max bitrate to send to the client.
type BitrateConfigs struct {
AudioRed uint32 `json:"audio_red" example:"75000"`
Audio uint32 `json:"audio" example:"48000"`
Video uint32 `json:"video" example:"1200000"`
VideoHigh uint32 `json:"video_high" example:"1200000"`
VideoHighPixels uint32 `json:"video_high_pixels" example:"921600"`
VideoMid uint32 `json:"video_mid" example:"500000"`
VideoMidPixels uint32 `json:"video_mid_pixels" example:"259200"`
VideoLow uint32 `json:"video_low" example:"150000"`
VideoLowPixels uint32 `json:"video_low_pixels" example:"64800"`
InitialBandwidth uint32 `json:"initial_bandwidth" example:"1000000"`
}
func DefaultBitrates() BitrateConfigs {
return BitrateConfigs{
AudioRed: 75_000,
Audio: 48_000,
Video: 700_000,
VideoHigh: 700_000,
VideoHighPixels: 720 * 360,
VideoMid: 300_000,
VideoMidPixels: 360 * 180,
VideoLow: 90_000,
VideoLowPixels: 180 * 90,
InitialBandwidth: 1_000_000,
}
}
type SFUClients struct {
clients map[string]*Client
mu sync.Mutex
}
func (s *SFUClients) GetClients() map[string]*Client {
s.mu.Lock()
defer s.mu.Unlock()
clients := make(map[string]*Client)
for k, v := range s.clients {
clients[k] = v
}
return clients
}
func (s *SFUClients) GetClient(id string) (*Client, error) {
s.mu.Lock()
defer s.mu.Unlock()
if client, ok := s.clients[id]; ok {
return client, nil
}
return nil, ErrClientNotFound
}
func (s *SFUClients) Length() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.clients)
}
func (s *SFUClients) Add(client *Client) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.clients[client.ID()]; ok {
return ErrClientExists
}
s.clients[client.ID()] = client
return nil
}
func (s *SFUClients) Remove(client *Client) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.clients[client.ID()]; !ok {
return ErrClientNotFound
}
delete(s.clients, client.ID())
return nil
}
type SFU struct {
bitrateConfigs BitrateConfigs
clients *SFUClients
context context.Context
cancel context.CancelFunc
codecs []string
dataChannels *SFUDataChannelList
iceServers []webrtc.ICEServer
mu sync.Mutex
onStop func()
pliInterval time.Duration
onTrackAvailableCallbacks []func(tracks []ITrack)
onClientRemovedCallbacks []func(*Client)
onClientAddedCallbacks []func(*Client)
relayTracks map[string]ITrack
clientStats map[string]*ClientStats
log logging.LeveledLogger
defaultSettingEngine *webrtc.SettingEngine
}
type PublishedTrack struct {
ClientID string
Track webrtc.TrackLocal
}
type sfuOptions struct {
IceServers []webrtc.ICEServer
Bitrates BitrateConfigs
QualityLevels []QualityLevel
Codecs []string
PLIInterval time.Duration
Log logging.LeveledLogger
SettingEngine *webrtc.SettingEngine
}
// @Param muxPort: port for udp mux
func New(ctx context.Context, opts sfuOptions) *SFU {
localCtx, cancel := context.WithCancel(ctx)
sfu := &SFU{
clients: &SFUClients{clients: make(map[string]*Client), mu: sync.Mutex{}},
context: localCtx,
cancel: cancel,
codecs: opts.Codecs,
dataChannels: NewSFUDataChannelList(),
mu: sync.Mutex{},
iceServers: opts.IceServers,
bitrateConfigs: opts.Bitrates,
pliInterval: opts.PLIInterval,
relayTracks: make(map[string]ITrack),
onTrackAvailableCallbacks: make([]func(tracks []ITrack), 0),
onClientRemovedCallbacks: make([]func(*Client), 0),
onClientAddedCallbacks: make([]func(*Client), 0),
log: opts.Log,
defaultSettingEngine: opts.SettingEngine,
}
return sfu
}
func (s *SFU) addClient(client *Client) {
if err := s.clients.Add(client); err != nil {
s.log.Errorf("sfu: failed to add client ", err)
return
}
s.onClientAdded(client)
}
func (s *SFU) createClient(id string, name string, peerConnectionConfig webrtc.Configuration, opts ClientOptions) *Client {
opts.settingEngine = *s.defaultSettingEngine
client := NewClient(s, id, name, peerConnectionConfig, opts)
// Get the LocalDescription and take it to base64 so we can paste in browser
return client
}
func (s *SFU) NewClient(id, name string, opts ClientOptions) *Client {
peerConnectionConfig := webrtc.Configuration{}
if len(s.iceServers) > 0 {
peerConnectionConfig.ICEServers = s.iceServers
}
opts.Log = s.log
client := s.createClient(id, name, peerConnectionConfig, opts)
s.addClient(client)
return client
}
func (s *SFU) AvailableTracks() []ITrack {
tracks := make([]ITrack, 0)
for _, client := range s.clients.GetClients() {
tracks = append(tracks, client.publishedTracks.GetTracks()...)
}
return tracks
}
// Syncs track from connected client to other clients
func (s *SFU) syncTrack(client *Client) {
publishedTrackIDs := make([]string, 0)
for _, track := range client.publishedTracks.GetTracks() {
publishedTrackIDs = append(publishedTrackIDs, track.ID())
}
subscribes := make([]SubscribeTrackRequest, 0)
for _, clientPeer := range s.clients.GetClients() {
for _, track := range clientPeer.tracks.GetTracks() {
if client.ID() != clientPeer.ID() {
if !slices.Contains(publishedTrackIDs, track.ID()) {
subscribes = append(subscribes, SubscribeTrackRequest{
ClientID: clientPeer.ID(),
TrackID: track.ID(),
})
}
}
}
}
if len(subscribes) > 0 {
err := client.SubscribeTracks(subscribes)
if err != nil {
s.log.Errorf("client: failed to subscribe tracks ", err)
}
}
}
func (s *SFU) Stop() {
for _, client := range s.clients.GetClients() {
client.PeerConnection().Close()
}
if s.onStop != nil {
s.onStop()
}
s.cancel()
}
func (s *SFU) OnStopped(callback func()) {
s.mu.Lock()
defer s.mu.Unlock()
s.onStop = callback
}
func (s *SFU) OnClientAdded(callback func(*Client)) {
s.mu.Lock()
defer s.mu.Unlock()
s.onClientAddedCallbacks = append(s.onClientAddedCallbacks, callback)
}
func (s *SFU) OnClientRemoved(callback func(*Client)) {
s.mu.Lock()
defer s.mu.Unlock()
s.onClientRemovedCallbacks = append(s.onClientRemovedCallbacks, callback)
}
func (s *SFU) onAfterClientStopped(client *Client) {
if err := s.removeClient(client); err != nil {
s.log.Errorf("sfu: failed to remove client ", err)
}
}
func (s *SFU) onClientAdded(client *Client) {
for _, callback := range s.onClientAddedCallbacks {
callback(client)
}
}
func (s *SFU) onClientRemoved(client *Client) {
for _, callback := range s.onClientRemovedCallbacks {
callback(client)
}
}
func (s *SFU) onTracksAvailable(clientId string, tracks []ITrack) {
for _, client := range s.clients.GetClients() {
if client.ID() != clientId {
client.onTracksAvailable(tracks)
s.log.Infof("sfu: client %s have %d tracks available ", client.ID(), len(tracks))
}
}
for _, callback := range s.onTrackAvailableCallbacks {
if callback != nil {
callback(tracks)
}
}
}
func (s *SFU) GetClient(id string) (*Client, error) {
return s.clients.GetClient(id)
}
func (s *SFU) GetClients() map[string]*Client {
return s.clients.GetClients()
}
func (s *SFU) removeClient(client *Client) error {
if err := s.clients.Remove(client); err != nil {
s.log.Errorf("sfu: failed to remove client ", err)
return err
}
s.onClientRemoved(client)
return nil
}
func (s *SFU) CreateDataChannel(label string, opts DataChannelOptions) error {
dc := s.dataChannels.Get(label)
if dc != nil {
return ErrDataChannelExists
}
s.dataChannels.Add(label, opts)
errors := []error{}
initOpts := &webrtc.DataChannelInit{
Ordered: &opts.Ordered,
}
for _, client := range s.clients.GetClients() {
if len(opts.ClientIDs) > 0 {
if !slices.Contains(opts.ClientIDs, client.ID()) {
continue
}
}
err := client.createDataChannel(label, initOpts)
if err != nil {
errors = append(errors, err)
}
}
return FlattenErrors(errors)
}
func (s *SFU) setupMessageForwarder(clientID string, d *webrtc.DataChannel) {
d.OnMessage(func(msg webrtc.DataChannelMessage) {
// broadcast to all clients
s.mu.Lock()
defer s.mu.Unlock()
for _, client := range s.clients.GetClients() {
// skip the sender
if client.id == clientID {
continue
}
dc := client.dataChannels.Get(d.Label())
if dc == nil {
continue
}
if dc.ReadyState() != webrtc.DataChannelStateOpen {
dc.OnOpen(func() {
dc.Send(msg.Data)
})
} else {
dc.Send(msg.Data)
}
}
})
}
func (s *SFU) createExistingDataChannels(c *Client) {
for _, dc := range s.dataChannels.dataChannels {
initOpts := &webrtc.DataChannelInit{
Ordered: &dc.isOrdered,
}
if len(dc.clientIDs) > 0 {
if !slices.Contains(dc.clientIDs, c.id) {
continue
}
}
if err := c.createDataChannel(dc.label, initOpts); err != nil {
s.log.Errorf("datachanel: error on create existing data channel %s, error %s", dc.label, err.Error())
}
}
}
func (s *SFU) TotalActiveSessions() int {
count := 0
for _, c := range s.clients.GetClients() {
if c.PeerConnection().PC().ConnectionState() == webrtc.PeerConnectionStateConnected {
count++
}
}
return count
}
func (s *SFU) PLIInterval() time.Duration {
s.mu.Lock()
defer s.mu.Unlock()
return s.pliInterval
}
func (s *SFU) OnTracksAvailable(callback func(tracks []ITrack)) {
s.mu.Lock()
defer s.mu.Unlock()
s.onTrackAvailableCallbacks = append(s.onTrackAvailableCallbacks, callback)
}
func (s *SFU) AddRelayTrack(ctx context.Context, id, streamid, rid string, client *Client, kind webrtc.RTPCodecType, ssrc webrtc.SSRC, mimeType string, rtpChan chan *rtp.Packet) error {
var track ITrack
relayTrack := NewTrackRelay(id, streamid, rid, kind, ssrc, mimeType, rtpChan)
onPLI := func() {}
if rid == "" {
// not simulcast
track = newTrack(ctx, client, relayTrack, 0, 0, s.pliInterval, onPLI, nil, nil)
s.mu.Lock()
s.relayTracks[relayTrack.ID()] = track
s.mu.Unlock()
} else {
// simulcast
var simulcast *SimulcastTrack
var ok bool
s.mu.Lock()
track, ok := s.relayTracks[relayTrack.ID()]
if !ok {
// if track not found, add it
track = newSimulcastTrack(client, relayTrack, 0, 0, s.pliInterval, onPLI, nil, nil)
s.relayTracks[relayTrack.ID()] = track
} else if simulcast, ok = track.(*SimulcastTrack); ok {
simulcast.AddRemoteTrack(relayTrack, 0, 0, nil, nil, onPLI)
}
s.mu.Unlock()
}
// TODO: replace to with subscribe to all available tracks
// s.broadcastTracksToAutoSubscribeClients(client.ID(), []ITrack{track})
// notify the local clients that a relay track is available
s.onTracksAvailable(client.ID(), []ITrack{track})
return nil
}