-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpublication.go
More file actions
641 lines (557 loc) · 16.6 KB
/
publication.go
File metadata and controls
641 lines (557 loc) · 16.6 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// 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 (
"errors"
"maps"
"slices"
"strings"
"sync"
"time"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v4"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit"
protoLogger "github.com/livekit/protocol/logger"
)
type TrackPublication interface {
Name() string
SID() string
Source() livekit.TrackSource
Kind() TrackKind
MimeType() string
IsMuted() bool
IsSubscribed() bool
TrackInfo() *livekit.TrackInfo
// Track is either a webrtc.TrackLocal or webrtc.TrackRemote
Track() Track
updateInfo(info *livekit.TrackInfo)
}
type trackPublicationBase struct {
kind atomic.String
track Track
sid atomic.String
name atomic.String
isMuted atomic.Bool
lock sync.RWMutex
info atomic.Value
engine *RTCEngine
}
func (p *trackPublicationBase) Name() string {
return p.name.Load()
}
func (p *trackPublicationBase) SID() string {
return p.sid.Load()
}
func (p *trackPublicationBase) Kind() TrackKind {
return TrackKind(p.kind.Load())
}
func (p *trackPublicationBase) Track() Track {
p.lock.RLock()
defer p.lock.RUnlock()
return p.track
}
func (p *trackPublicationBase) MimeType() string {
// This requires some more work.
// TrackInfo has a top level MimeType which is not set
// on server side till the track is published.
// So, it is not available in the TrackPublishedResponse.
//
// But, if client specified SimulcastCodecs in AddTrackRequest,
// the TrackPublishedResponse will have Codecs populated and
// that will have the MimeType specified in AddTrackRequest.
// Just taking the first one here which has a non-nil MimeType.
// This is okay (for tracks published from here)
// as of 2025-05-12, 1:30 pm Pacific as
// Go SDK does not (yet) support simulcast codec feature.
//
// When simulcast codec feature is added, this struct needs
// to be updated to handle multiple mime types
if info, ok := p.info.Load().(*livekit.TrackInfo); ok {
if info.MimeType != "" {
return info.MimeType
}
for _, codec := range info.Codecs {
if codec.MimeType != "" {
return codec.MimeType
}
}
}
return ""
}
func (p *trackPublicationBase) Source() livekit.TrackSource {
if info, ok := p.info.Load().(*livekit.TrackInfo); ok {
return info.Source
}
return livekit.TrackSource_UNKNOWN
}
func (p *trackPublicationBase) IsMuted() bool {
return p.isMuted.Load()
}
func (p *trackPublicationBase) IsSubscribed() bool {
return p.track != nil
}
func (p *trackPublicationBase) updateInfo(info *livekit.TrackInfo) {
p.name.Store(info.Name)
p.sid.Store(info.Sid)
p.isMuted.Store(info.Muted)
if info.Type == livekit.TrackType_AUDIO {
p.kind.Store(string(TrackKindAudio))
} else if info.Type == livekit.TrackType_VIDEO {
p.kind.Store(string(TrackKindVideo))
}
p.info.Store(info)
}
func (p *trackPublicationBase) TrackInfo() *livekit.TrackInfo {
if info := p.info.Load(); info != nil {
return proto.Clone(info.(*livekit.TrackInfo)).(*livekit.TrackInfo)
}
return nil
}
type RemoteTrackPublication struct {
trackPublicationBase
participantID string
receiver *webrtc.RTPReceiver
onRTCP func(rtcp.Packet)
disabled bool
// preferred video dimensions to subscribe
videoWidth *uint32
videoHeight *uint32
// preferred video quality to subscribe
videoQuality *livekit.VideoQuality
}
// TrackRemote returns the underlying webrtc.TrackRemote if available.
// Returns nil if the track is not subscribed
func (p *RemoteTrackPublication) TrackRemote() *webrtc.TrackRemote {
p.lock.RLock()
defer p.lock.RUnlock()
if t, ok := p.track.(*webrtc.TrackRemote); ok {
return t
}
return nil
}
// Receiver returns the RTP receiver associated with this track publication.
func (p *RemoteTrackPublication) Receiver() *webrtc.RTPReceiver {
p.lock.RLock()
defer p.lock.RUnlock()
return p.receiver
}
// SetSubscribed subscribes or unsubscribes from this track.
// When subscribed, track data will be received from the server.
func (p *RemoteTrackPublication) SetSubscribed(subscribed bool) error {
return p.engine.SendUpdateSubscription(
&livekit.UpdateSubscription{
Subscribe: subscribed,
ParticipantTracks: []*livekit.ParticipantTracks{
{
ParticipantSid: p.participantID,
TrackSids: []string{p.sid.Load()},
},
},
},
)
}
// IsEnabled returns whether the track is enabled (not disabled).
// Disabled tracks will not receive media data even when subscribed.
func (p *RemoteTrackPublication) IsEnabled() bool {
p.lock.RLock()
defer p.lock.RUnlock()
return !p.disabled
}
// SetEnabled enables or disables the track.
// When disabled, the track will not receive media data even when subscribed.
func (p *RemoteTrackPublication) SetEnabled(enabled bool) {
p.lock.Lock()
p.disabled = !enabled
p.lock.Unlock()
p.updateSettings()
}
// SetVideoDimensions sets the preferred video dimensions to receive.
// This is a hint to the server about what resolution to send.
func (p *RemoteTrackPublication) SetVideoDimensions(width uint32, height uint32) {
p.lock.Lock()
p.videoWidth = &width
p.videoHeight = &height
p.lock.Unlock()
p.updateSettings()
}
// SetVideoQuality sets the preferred video quality to receive.
func (p *RemoteTrackPublication) SetVideoQuality(quality livekit.VideoQuality) error {
if quality == livekit.VideoQuality_OFF {
return errors.New("cannot set video quality to OFF")
}
p.lock.Lock()
p.videoQuality = &quality
p.lock.Unlock()
p.updateSettings()
return nil
}
// OnRTCP sets a callback to receive RTCP packets for this track.
func (p *RemoteTrackPublication) OnRTCP(cb func(rtcp.Packet)) {
p.lock.Lock()
p.onRTCP = cb
p.lock.Unlock()
}
func (p *RemoteTrackPublication) updateSettings() {
p.lock.RLock()
settings := &livekit.UpdateTrackSettings{
TrackSids: []string{p.SID()},
Disabled: p.disabled,
// default to high
Quality: livekit.VideoQuality_HIGH,
}
if p.videoWidth != nil && p.videoHeight != nil {
settings.Width = *p.videoWidth
settings.Height = *p.videoHeight
}
if p.videoQuality != nil {
settings.Quality = *p.videoQuality
}
p.lock.RUnlock()
if err := p.engine.SendUpdateTrackSettings(settings); err != nil {
p.engine.log.Errorw("could not send track settings", err, "trackID", p.SID())
}
}
func (p *RemoteTrackPublication) setReceiverAndTrack(r *webrtc.RTPReceiver, t *webrtc.TrackRemote) {
p.lock.Lock()
p.receiver = r
p.track = t
p.lock.Unlock()
if r != nil {
go p.rtcpWorker()
}
}
func (p *RemoteTrackPublication) rtcpWorker() {
receiver := p.Receiver()
if receiver == nil {
return
}
// read incoming rtcp packets so interceptors can handle NACKs
for {
packets, _, err := receiver.ReadRTCP()
if err != nil {
// pipe closed
return
}
p.lock.RLock()
// rtcpCB could have changed along the way
rtcpCB := p.onRTCP
p.lock.RUnlock()
if rtcpCB != nil {
for _, packet := range packets {
rtcpCB(packet)
}
}
}
}
type LocalTrackPublication struct {
trackPublicationBase
// set for simulcasted tracks
simulcastTracks map[livekit.VideoQuality]*LocalTrack
// set for backup codecs
backupCodecTrack TrackLocalWithCodec
backupCodecTracksForSimulcast map[livekit.VideoQuality]*LocalTrack
backupCodecPublished atomic.Bool
opts TrackPublicationOptions
onMuteChanged func(*LocalTrackPublication, bool)
log protoLogger.Logger
}
func NewLocalTrackPublication(kind TrackKind, track Track, opts TrackPublicationOptions, engine *RTCEngine, log protoLogger.Logger) *LocalTrackPublication {
pub := &LocalTrackPublication{
trackPublicationBase: trackPublicationBase{
track: track,
engine: engine,
},
opts: opts,
log: log,
}
pub.kind.Store(string(kind))
pub.name.Store(opts.Name)
return pub
}
func (p *LocalTrackPublication) PublicationOptions() TrackPublicationOptions {
return p.opts
}
func (p *LocalTrackPublication) TrackLocal() webrtc.TrackLocal {
p.lock.RLock()
defer p.lock.RUnlock()
if t, ok := p.track.(webrtc.TrackLocal); ok {
return t
}
return nil
}
func (p *LocalTrackPublication) TrackLocalForSimulcast() []*LocalTrack {
p.lock.RLock()
defer p.lock.RUnlock()
return slices.Collect(maps.Values(p.simulcastTracks))
}
// GetSimulcastTrack returns the simulcast track for a specific quality level.
// Returns nil if simulcast is not enabled or the quality level doesn't exist.
func (p *LocalTrackPublication) GetSimulcastTrack(quality livekit.VideoQuality) *LocalTrack {
p.lock.RLock()
defer p.lock.RUnlock()
if p.simulcastTracks == nil {
return nil
}
return p.simulcastTracks[quality]
}
// SetMuted mutes or unmutes the track.
// When muted, no media data will be sent to other participants.
func (p *LocalTrackPublication) SetMuted(muted bool) {
p.setMuted(muted, false)
}
// SimulateDisconnection simulates a network disconnection for testing purposes.
// If duration is 0, the disconnection persists until manually reconnected.
func (p *LocalTrackPublication) SimulateDisconnection(duration time.Duration) {
if track := p.track; track != nil {
switch t := track.(type) {
case *LocalTrack:
t.setDisconnected(true)
if duration != 0 {
time.AfterFunc(duration, func() {
t.setDisconnected(false)
})
}
}
}
}
func (p *LocalTrackPublication) setMuted(muted bool, byRemote bool) {
if p.isMuted.Swap(muted) == muted {
return
}
if !byRemote {
_ = p.engine.SendMuteTrack(p.sid.Load(), muted)
}
if track := p.track; track != nil {
switch t := track.(type) {
case *LocalTrack:
t.setMuted(muted)
case interface{ GetMuteFunc() Private[MuteFunc] }:
t.GetMuteFunc().v(muted)
}
}
if p.onMuteChanged != nil {
p.onMuteChanged(p, muted)
}
}
func (p *LocalTrackPublication) addSimulcastTrack(st *LocalTrack) {
p.lock.Lock()
defer p.lock.Unlock()
if p.simulcastTracks == nil {
p.simulcastTracks = make(map[livekit.VideoQuality]*LocalTrack)
}
if st != nil {
p.simulcastTracks[st.videoLayer.Quality] = st
}
}
func (p *LocalTrackPublication) setBackupCodecTrack(st TrackLocalWithCodec) {
p.lock.Lock()
defer p.lock.Unlock()
p.backupCodecTrack = st
}
func (p *LocalTrackPublication) setBackupCodecTracksForSimulcast(st []*LocalTrack) {
p.lock.Lock()
defer p.lock.Unlock()
if p.backupCodecTracksForSimulcast == nil {
p.backupCodecTracksForSimulcast = make(map[livekit.VideoQuality]*LocalTrack)
}
for _, track := range st {
if track != nil {
p.backupCodecTracksForSimulcast[track.videoLayer.Quality] = track
}
}
}
func (p *LocalTrackPublication) getBackupCodecTrack() (TrackLocalWithCodec, []*LocalTrack) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.backupCodecTrack, slices.Collect(maps.Values(p.backupCodecTracksForSimulcast))
}
func (p *LocalTrackPublication) setBackupCodecPublished() {
p.backupCodecPublished.Store(true)
}
func (p *LocalTrackPublication) readRTCP(sender *webrtc.RTPSender) {
// consume RTCP packets so interceptors can handle them (rtt, nacks...)
go func() {
for {
_, _, err := sender.ReadRTCP()
if err != nil {
// pipe closed
return
}
}
}()
}
// CloseTrack closes the underlying track and all simulcast tracks.
// This should be called when the track is no longer needed.
func (p *LocalTrackPublication) CloseTrack() {
var tracks []LocalTrackWithClose
p.lock.RLock()
if localTrack, ok := p.track.(LocalTrackWithClose); ok {
tracks = append(tracks, localTrack)
}
for _, st := range p.simulcastTracks {
tracks = append(tracks, st)
}
if localTrackWithClose, ok := p.backupCodecTrack.(LocalTrackWithClose); ok {
tracks = append(tracks, localTrackWithClose)
}
for _, st := range p.backupCodecTracksForSimulcast {
tracks = append(tracks, st)
}
p.lock.RUnlock()
for _, track := range tracks {
track.Close()
}
}
// SetPublishingCodecsQuality sets the publishing codecs based on the subscribed codecs quality, return
// the track(s) to be published for backup codec that is required by subscriber but not published yet.
func (p *LocalTrackPublication) setPublishingCodecsQuality(subscribedCodecs []*livekit.SubscribedCodec) []string {
var codecNeedPub []string
for _, subscribedCodec := range subscribedCodecs {
if strings.HasSuffix(strings.ToLower(p.MimeType()), subscribedCodec.Codec) {
// primary codec
for _, subscribedQuality := range subscribedCodec.Qualities {
track := p.GetSimulcastTrack(subscribedQuality.Quality)
if track != nil {
track.setMuted(!subscribedQuality.Enabled)
p.log.Infow(
"updating layer enable",
"trackID", p.SID(),
"quality", subscribedQuality.Quality,
"enabled", subscribedQuality.Enabled,
"codec", subscribedCodec.Codec,
)
}
}
continue
}
// backup codec
p.lock.RLock()
backupCodecTrack, backupCodecTracksForSimulcast := p.backupCodecTrack, p.backupCodecTracksForSimulcast
p.lock.RUnlock()
mainTrack := backupCodecTrack
if len(backupCodecTracksForSimulcast) > 0 {
iter := maps.Values(backupCodecTracksForSimulcast)
iter(func(l *LocalTrack) bool {
mainTrack = l
return false
})
}
if mainTrack == nil || !strings.HasSuffix(strings.ToLower(mainTrack.Codec().MimeType), subscribedCodec.Codec) {
p.log.Warnw("subscriber requested backup codec but no track found", nil, "trackID", p.SID(), "codec", subscribedCodec.Codec)
continue
}
// check if need to publish backup codec
if !p.backupCodecPublished.Load() {
var needPub bool
for _, subscribedQuality := range subscribedCodec.Qualities {
if subscribedQuality.Enabled {
needPub = true
break
}
}
if needPub {
codecNeedPub = append(codecNeedPub, subscribedCodec.Codec)
}
continue
}
// dynacast for backup codec simulcast
if len(backupCodecTracksForSimulcast) == 0 {
continue
}
for _, subscribedQuality := range subscribedCodec.Qualities {
track := backupCodecTracksForSimulcast[subscribedQuality.Quality]
if track != nil {
track.setMuted(!subscribedQuality.Enabled)
p.log.Infow(
"updating layer enable",
"trackID", p.SID(),
"quality", subscribedQuality.Quality,
"enabled", subscribedQuality.Enabled,
"codec", subscribedCodec.Codec,
)
}
}
}
return codecNeedPub
}
func (p *LocalTrackPublication) setAudioCodecSubscribed(subscribedCodecs []*livekit.SubscribedAudioCodec) []string {
var codecNeedPub []string
for _, subscribedCodec := range subscribedCodecs {
if strings.HasSuffix(strings.ToLower(p.MimeType()), subscribedCodec.Codec) {
// primary codec
continue
}
// backup codec
p.lock.RLock()
backupCodecTrack := p.backupCodecTrack
p.lock.RUnlock()
if backupCodecTrack == nil || !strings.HasSuffix(strings.ToLower(backupCodecTrack.Codec().MimeType), subscribedCodec.Codec) {
p.log.Warnw("subscriber requested backup codec but no backup codec track found", nil, "trackID", p.SID(), "codec", subscribedCodec.Codec)
continue
}
// check if need to publish backup codec
if !p.backupCodecPublished.Load() && subscribedCodec.Enabled {
codecNeedPub = append(codecNeedPub, subscribedCodec.Codec)
continue
}
}
return codecNeedPub
}
func (p *LocalTrackPublication) unpublish(transport *PCTransport) error {
var tracks []webrtc.TrackLocal
p.lock.RLock()
if localTrack, ok := p.track.(webrtc.TrackLocal); ok {
tracks = append(tracks, localTrack)
}
for _, st := range p.simulcastTracks {
tracks = append(tracks, st)
}
tracks = append(tracks, p.backupCodecTrack)
for _, st := range p.backupCodecTracksForSimulcast {
tracks = append(tracks, st)
}
p.lock.RUnlock()
for _, sender := range transport.pc.GetSenders() {
for _, track := range tracks {
if sender.Track() == track {
if err := transport.pc.RemoveTrack(sender); err != nil {
return err
}
}
}
}
return nil
}
func (p *LocalTrackPublication) SetLogger(log protoLogger.Logger) {
p.log = log
}
type TrackPublicationOptions struct {
Name string
Source livekit.TrackSource
// Set dimensions for video
VideoWidth int
VideoHeight int
// Opus only
DisableDTX bool
Stereo bool
// which stream the track belongs to, used to group tracks together.
// if not specified, server will infer it from track source to bundle camera/microphone, screenshare/audio together
Stream string
// encryption type
Encryption livekit.Encryption_Type
BackupCodecPolicy livekit.BackupCodecPolicy
}
type MuteFunc func(muted bool) error