forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localsampletrack.go
454 lines (399 loc) · 10.8 KB
/
localsampletrack.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
package lksdk
import (
"context"
"io"
"strings"
"sync"
"time"
"github.com/pion/interceptor"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"go.uber.org/atomic"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils"
)
const (
rtpOutboundMTU = 1200
rtpInboundMTU = 1500
)
type SampleWriteOptions struct {
AudioLevel *uint8
}
// LocalSampleTrack is a local track that simplifies writing samples.
// It handles timing and publishing of things, so as long as a SampleProvider is provided, the class takes care of
// publishing tracks at the right frequency
// This extends webrtc.TrackLocalStaticSample, and adds the ability to write RTP extensions
type LocalSampleTrack struct {
packetizer rtp.Packetizer
sequencer rtp.Sequencer
transceiver *webrtc.RTPTransceiver
rtpTrack *webrtc.TrackLocalStaticRTP
ssrc webrtc.SSRC
ssrcAcked bool
clockRate float64
bound atomic.Bool
lock sync.RWMutex
audioLevelID uint8
sdesMidID uint8
sdesRtpStreamID uint8
lastTS time.Time
simulcastID string
videoLayer *livekit.VideoLayer
onRTCP func(rtcp.Packet)
cancelWrite func()
provider SampleProvider
onBind func()
onUnbind func()
// notify when sample provider responds with EOF
onWriteComplete func()
}
type LocalSampleTrackOptions func(s *LocalSampleTrack)
// WithSimulcast marks the current track for simulcasting.
// In order to use simulcast, simulcastID must be identical across all layers
func WithSimulcast(simulcastID string, layer *livekit.VideoLayer) LocalSampleTrackOptions {
return func(s *LocalSampleTrack) {
s.videoLayer = layer
s.simulcastID = simulcastID
}
}
func WithRTCPHandler(cb func(rtcp.Packet)) LocalSampleTrackOptions {
return func(s *LocalSampleTrack) {
s.onRTCP = cb
}
}
func NewLocalSampleTrack(c webrtc.RTPCodecCapability, opts ...LocalSampleTrackOptions) (*LocalSampleTrack, error) {
s := &LocalSampleTrack{}
for _, o := range opts {
o(s)
}
rid := ""
if s.videoLayer != nil {
switch s.videoLayer.Quality {
case livekit.VideoQuality_HIGH:
rid = "f"
case livekit.VideoQuality_MEDIUM:
rid = "h"
case livekit.VideoQuality_LOW:
rid = "q"
}
}
trackID := utils.NewGuid("TR_")
streamID := utils.NewGuid("ST_")
if s.simulcastID != "" {
trackID = s.simulcastID
streamID = s.simulcastID
}
rtpTrack, err := webrtc.NewTrackLocalStaticRTP(c, trackID, streamID, webrtc.WithRTPStreamID(rid))
if err != nil {
return nil, err
}
s.rtpTrack = rtpTrack
return s, nil
}
func (s *LocalSampleTrack) SetTransceiver(transceiver *webrtc.RTPTransceiver) {
s.lock.Lock()
defer s.lock.Unlock()
s.transceiver = transceiver
}
// ID is the unique identifier for this Track. This should be unique for the
// stream, but doesn't have to globally unique. A common example would be 'audio' or 'video'
// and StreamID would be 'desktop' or 'webcam'
func (s *LocalSampleTrack) ID() string { return s.rtpTrack.ID() }
// RID is the RTP stream identifier.
func (s *LocalSampleTrack) RID() string {
return s.rtpTrack.RID()
}
// StreamID is the group this track belongs too. This must be unique
func (s *LocalSampleTrack) StreamID() string { return s.rtpTrack.StreamID() }
// Kind controls if this TrackLocal is audio or video
func (s *LocalSampleTrack) Kind() webrtc.RTPCodecType { return s.rtpTrack.Kind() }
// Codec gets the Codec of the track
func (s *LocalSampleTrack) Codec() webrtc.RTPCodecCapability {
return s.rtpTrack.Codec()
}
func (s *LocalSampleTrack) IsBound() bool {
return s.bound.Load()
}
// Bind is an interface for TrackLocal, not for external consumption
func (s *LocalSampleTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) {
codec, err := s.rtpTrack.Bind(t)
if err != nil {
return codec, err
}
payloader, err := payloaderForCodec(codec.RTPCodecCapability)
if err != nil {
return codec, err
}
s.lock.Lock()
s.ssrc = t.SSRC()
for _, ext := range t.HeaderExtensions() {
if ext.URI == sdp.AudioLevelURI {
s.audioLevelID = uint8(ext.ID)
}
if ext.URI == sdp.SDESMidURI {
s.sdesMidID = uint8(ext.ID)
}
if ext.URI == sdp.SDESRTPStreamIDURI {
s.sdesRtpStreamID = uint8(ext.ID)
}
}
s.sequencer = rtp.NewRandomSequencer()
s.packetizer = rtp.NewPacketizer(
rtpOutboundMTU,
0, // Value is handled when writing
0, // Value is handled when writing
payloader,
s.sequencer,
codec.ClockRate,
)
s.clockRate = float64(codec.RTPCodecCapability.ClockRate)
onBind := s.onBind
provider := s.provider
onWriteComplete := s.onWriteComplete
s.bound.Store(true)
s.lock.Unlock()
if provider != nil {
err = provider.OnBind()
go s.writeWorker(provider, onWriteComplete)
}
go s.rtcpWorker(t.RTCPReader())
// notify callbacks last
if onBind != nil {
go onBind()
}
return codec, err
}
// Unbind is an interface for TrackLocal, not for external consumption
func (s *LocalSampleTrack) Unbind(t webrtc.TrackLocalContext) error {
s.lock.Lock()
provider := s.provider
onUnbind := s.onUnbind
s.bound.Store(false)
cancel := s.cancelWrite
s.lock.Unlock()
var err error
if provider != nil {
err = provider.OnUnbind()
}
if cancel != nil {
cancel()
}
if onUnbind != nil {
go onUnbind()
}
unbindErr := s.rtpTrack.Unbind(t)
if unbindErr != nil {
return unbindErr
}
return err
}
func (s *LocalSampleTrack) StartWrite(provider SampleProvider, onComplete func()) error {
s.lock.Lock()
defer s.lock.Unlock()
if s.provider == provider {
return nil
}
// when bound and already writing, ignore
if s.IsBound() {
// unbind previous provider
if s.provider != nil {
if err := s.provider.OnUnbind(); err != nil {
return err
}
}
if err := provider.OnBind(); err != nil {
return err
}
// start new writer
go s.writeWorker(provider, onComplete)
}
s.provider = provider
s.onWriteComplete = onComplete
return nil
}
// OnBind sets a callback to be called when the track has been negotiated for publishing and bound to a peer connection
func (s *LocalSampleTrack) OnBind(f func()) {
s.lock.Lock()
s.onBind = f
s.lock.Unlock()
}
// OnUnbind sets a callback to be called after the track is removed from a peer connection
func (s *LocalSampleTrack) OnUnbind(f func()) {
s.lock.Lock()
s.onUnbind = f
s.lock.Unlock()
}
func (s *LocalSampleTrack) WriteSample(sample media.Sample, opts *SampleWriteOptions) error {
s.lock.RLock()
p := s.packetizer
clockRate := s.clockRate
transceiver := s.transceiver
ssrcAcked := s.ssrcAcked
s.lock.RUnlock()
if p == nil {
return nil
}
// skip packets by the number of previously dropped packets
for i := uint16(0); i < sample.PrevDroppedPackets; i++ {
s.sequencer.NextSequenceNumber()
}
// calculate / interpolate duration when supplied duration is invalid
if sample.Duration.Nanoseconds() < 0 {
sample.Duration = sample.Timestamp.Sub(s.lastTS)
s.lastTS = sample.Timestamp
}
samples := uint32(sample.Duration.Seconds() * clockRate)
if sample.PrevDroppedPackets > 0 {
p.SkipSamples(samples * uint32(sample.PrevDroppedPackets))
}
packets := p.Packetize(sample.Data, samples)
var writeErrs []error
for _, p := range packets {
if s.audioLevelID != 0 && opts != nil && opts.AudioLevel != nil {
ext := rtp.AudioLevelExtension{
Level: *opts.AudioLevel,
}
data, err := ext.Marshal()
if err != nil {
writeErrs = append(writeErrs, err)
continue
}
if err := p.Header.SetExtension(s.audioLevelID, data); err != nil {
writeErrs = append(writeErrs, err)
continue
}
}
if s.RID() != "" && transceiver != nil && transceiver.Mid() != "" && !ssrcAcked {
if s.sdesMidID != 0 {
midValue := transceiver.Mid()
if err := p.Header.SetExtension(s.sdesMidID, []byte(midValue)); err != nil {
writeErrs = append(writeErrs, err)
continue
}
}
if s.sdesRtpStreamID != 0 {
ridValue := s.RID()
if err := p.Header.SetExtension(s.sdesRtpStreamID, []byte(ridValue)); err != nil {
writeErrs = append(writeErrs, err)
continue
}
}
}
if err := s.rtpTrack.WriteRTP(p); err != nil {
writeErrs = append(writeErrs, err)
}
}
if len(writeErrs) > 0 {
return writeErrs[0]
}
return nil
}
func (s *LocalSampleTrack) rtcpWorker(rtcpReader interceptor.RTCPReader) {
// read incoming rtcp packets, interceptors require this
b := make([]byte, rtpInboundMTU)
rtcpCB := s.onRTCP
for {
var a interceptor.Attributes
i, _, err := rtcpReader.Read(b, a)
if err != nil {
// pipe closed
return
}
pkts, err := rtcp.Unmarshal(b[:i])
if err != nil {
return
}
for _, packet := range pkts {
s.lock.Lock()
if !s.ssrcAcked {
switch p := packet.(type) {
case *rtcp.ReceiverReport:
for _, r := range p.Reports {
if webrtc.SSRC(r.SSRC) == s.ssrc {
s.ssrcAcked = true
break
}
}
}
}
s.lock.Unlock()
if rtcpCB != nil {
rtcpCB(packet)
}
}
}
}
func (s *LocalSampleTrack) writeWorker(provider SampleProvider, onComplete func()) {
if s.cancelWrite != nil {
s.cancelWrite()
}
var ctx context.Context
s.lock.Lock()
ctx, s.cancelWrite = context.WithCancel(context.Background())
s.lock.Unlock()
if onComplete != nil {
defer onComplete()
}
audioProvider, isAudioProvider := provider.(AudioSampleProvider)
nextSampleTime := time.Now()
ticker := time.NewTicker(10 * time.Millisecond)
for {
sample, err := provider.NextSample()
if err == io.EOF {
return
}
if err != nil {
logger.Errorw("could not get sample from provider", err)
return
}
var opts *SampleWriteOptions
if isAudioProvider {
level := audioProvider.CurrentAudioLevel()
opts = &SampleWriteOptions{
AudioLevel: &level,
}
}
if err := s.WriteSample(sample, opts); err != nil {
logger.Errorw("could not write sample", err)
return
}
// account for clock drift
nextSampleTime = nextSampleTime.Add(sample.Duration)
sleepDuration := time.Until(nextSampleTime)
if sleepDuration <= 0 {
continue
}
ticker.Reset(sleepDuration)
select {
case <-ticker.C:
continue
case <-ctx.Done():
return
}
}
}
// duplicated from pion mediaengine.go
func payloaderForCodec(codec webrtc.RTPCodecCapability) (rtp.Payloader, error) {
switch strings.ToLower(codec.MimeType) {
case strings.ToLower(webrtc.MimeTypeH264):
return &codecs.H264Payloader{}, nil
case strings.ToLower(webrtc.MimeTypeOpus):
return &codecs.OpusPayloader{}, nil
case strings.ToLower(webrtc.MimeTypeVP8):
return &codecs.VP8Payloader{
EnablePictureID: true,
}, nil
case strings.ToLower(webrtc.MimeTypeVP9):
return &codecs.VP9Payloader{}, nil
case strings.ToLower(webrtc.MimeTypeG722):
return &codecs.G722Payloader{}, nil
case strings.ToLower(webrtc.MimeTypePCMU), strings.ToLower(webrtc.MimeTypePCMA):
return &codecs.G711Payloader{}, nil
default:
return nil, webrtc.ErrNoPayloaderForCodec
}
}