-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathrtpsender.go
521 lines (423 loc) · 12.9 KB
/
rtpsender.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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package webrtc
import (
"fmt"
"io"
"sync"
"time"
"github.com/pion/interceptor"
"github.com/pion/randutil"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v4/internal/util"
)
type trackEncoding struct {
track TrackLocal
srtpStream *srtpWriterFuture
rtcpInterceptor interceptor.RTCPReader
streamInfo interceptor.StreamInfo
context *baseTrackLocalContext
ssrc, ssrcRTX, ssrcFEC SSRC
}
// RTPSender allows an application to control how a given Track is encoded and transmitted to a remote peer.
type RTPSender struct {
trackEncodings []*trackEncoding
transport *DTLSTransport
payloadType PayloadType
kind RTPCodecType
// nolint:godox
// TODO(sgotti) remove this when in future we'll avoid replacing
// a transceiver sender since we can just check the
// transceiver negotiation status
negotiated bool
// A reference to the associated api object
api *API
id string
rtpTransceiver *RTPTransceiver
mu sync.RWMutex
sendCalled, stopCalled chan struct{}
}
// NewRTPSender constructs a new RTPSender.
func (api *API) NewRTPSender(track TrackLocal, transport *DTLSTransport) (*RTPSender, error) {
if track == nil {
return nil, errRTPSenderTrackNil
} else if transport == nil {
return nil, errRTPSenderDTLSTransportNil
}
id, err := randutil.GenerateCryptoRandomString(32, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
if err != nil {
return nil, err
}
r := &RTPSender{
transport: transport,
api: api,
sendCalled: make(chan struct{}),
stopCalled: make(chan struct{}),
id: id,
kind: track.Kind(),
}
r.addEncoding(track)
return r, nil
}
func (r *RTPSender) isNegotiated() bool {
r.mu.RLock()
defer r.mu.RUnlock()
return r.negotiated
}
func (r *RTPSender) setNegotiated() {
r.mu.Lock()
defer r.mu.Unlock()
r.negotiated = true
}
func (r *RTPSender) setRTPTransceiver(rtpTransceiver *RTPTransceiver) {
r.mu.Lock()
defer r.mu.Unlock()
r.rtpTransceiver = rtpTransceiver
}
// Transport returns the currently-configured *DTLSTransport or nil
// if one has not yet been configured.
func (r *RTPSender) Transport() *DTLSTransport {
r.mu.RLock()
defer r.mu.RUnlock()
return r.transport
}
// GetParameters describes the current configuration for the encoding and
// transmission of media on the sender's track.
func (r *RTPSender) GetParameters() RTPSendParameters {
r.mu.RLock()
defer r.mu.RUnlock()
var encodings []RTPEncodingParameters
for _, trackEncoding := range r.trackEncodings {
var rid string
if trackEncoding.track != nil {
rid = trackEncoding.track.RID()
}
encodings = append(encodings, RTPEncodingParameters{
RTPCodingParameters: RTPCodingParameters{
RID: rid,
SSRC: trackEncoding.ssrc,
RTX: RTPRtxParameters{SSRC: trackEncoding.ssrcRTX},
FEC: RTPFecParameters{SSRC: trackEncoding.ssrcFEC},
PayloadType: r.payloadType,
},
})
}
sendParameters := RTPSendParameters{
RTPParameters: r.api.mediaEngine.getRTPParametersByKind(
r.kind,
[]RTPTransceiverDirection{RTPTransceiverDirectionSendonly},
),
Encodings: encodings,
}
if r.rtpTransceiver != nil {
sendParameters.Codecs = r.rtpTransceiver.getCodecs()
} else {
sendParameters.Codecs = r.api.mediaEngine.getCodecsByKind(r.kind)
}
return sendParameters
}
// AddEncoding adds an encoding to RTPSender. Used by simulcast senders.
func (r *RTPSender) AddEncoding(track TrackLocal) error { //nolint:cyclop
r.mu.Lock()
defer r.mu.Unlock()
if track == nil {
return errRTPSenderTrackNil
}
if track.RID() == "" {
return errRTPSenderRidNil
}
if r.hasStopped() {
return errRTPSenderStopped
}
if r.hasSent() {
return errRTPSenderSendAlreadyCalled
}
var refTrack TrackLocal
if len(r.trackEncodings) != 0 {
refTrack = r.trackEncodings[0].track
}
if refTrack == nil || refTrack.RID() == "" {
return errRTPSenderNoBaseEncoding
}
if refTrack.ID() != track.ID() || refTrack.StreamID() != track.StreamID() || refTrack.Kind() != track.Kind() {
return errRTPSenderBaseEncodingMismatch
}
for _, encoding := range r.trackEncodings {
if encoding.track == nil {
continue
}
if encoding.track.RID() == track.RID() {
return errRTPSenderRIDCollision
}
}
r.addEncoding(track)
return nil
}
func (r *RTPSender) addEncoding(track TrackLocal) {
trackEncoding := &trackEncoding{
track: track,
ssrc: SSRC(util.RandUint32()),
}
if r.api.mediaEngine.isRTXEnabled(r.kind, []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}) {
trackEncoding.ssrcRTX = SSRC(util.RandUint32())
}
if r.api.mediaEngine.isFECEnabled(r.kind, []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}) {
trackEncoding.ssrcFEC = SSRC(util.RandUint32())
}
r.trackEncodings = append(r.trackEncodings, trackEncoding)
}
// Track returns the RTCRtpTransceiver track, or nil.
func (r *RTPSender) Track() TrackLocal {
r.mu.RLock()
defer r.mu.RUnlock()
if len(r.trackEncodings) == 0 {
return nil
}
return r.trackEncodings[0].track
}
// ReplaceTrack replaces the track currently being used as the sender's source with a new TrackLocal.
// The new track must be of the same media kind (audio, video, etc) and switching the track should not
// require negotiation.
func (r *RTPSender) ReplaceTrack(track TrackLocal) error { //nolint:cyclop
r.mu.Lock()
defer r.mu.Unlock()
if track != nil && r.kind != track.Kind() {
return ErrRTPSenderNewTrackHasIncorrectKind
}
// cannot replace simulcast envelope
if track != nil && len(r.trackEncodings) > 1 {
return ErrRTPSenderNewTrackHasIncorrectEnvelope
}
var replacedTrack TrackLocal
var context *baseTrackLocalContext
for _, e := range r.trackEncodings {
replacedTrack = e.track
context = e.context
if r.hasSent() && replacedTrack != nil {
if err := replacedTrack.Unbind(context); err != nil {
return err
}
}
if !r.hasSent() || track == nil {
e.track = track
}
}
if !r.hasSent() || track == nil {
return nil
}
params := r.api.mediaEngine.getRTPParametersByKind(
track.Kind(),
[]RTPTransceiverDirection{RTPTransceiverDirectionSendonly},
)
// If we reach this point in the routine, there is only 1 track encoding
codec, err := track.Bind(&baseTrackLocalContext{
id: context.ID(),
params: params,
ssrc: context.SSRC(),
ssrcRTX: context.SSRCRetransmission(),
ssrcFEC: context.SSRCForwardErrorCorrection(),
writeStream: context.WriteStream(),
rtcpInterceptor: context.RTCPReader(),
})
if err != nil {
// Re-bind the original track
if _, reBindErr := replacedTrack.Bind(context); reBindErr != nil {
return reBindErr
}
return err
}
// Codec has changed
if r.payloadType != codec.PayloadType {
context.params.Codecs = []RTPCodecParameters{codec}
}
r.trackEncodings[0].track = track
return nil
}
// Send Attempts to set the parameters controlling the sending of media.
func (r *RTPSender) Send(parameters RTPSendParameters) error {
r.mu.Lock()
defer r.mu.Unlock()
switch {
case r.hasSent():
return errRTPSenderSendAlreadyCalled
case r.trackEncodings[0].track == nil:
return errRTPSenderTrackRemoved
}
for idx := range r.trackEncodings {
trackEncoding := r.trackEncodings[idx]
srtpStream := &srtpWriterFuture{ssrc: parameters.Encodings[idx].SSRC, rtpSender: r}
writeStream := &interceptorToTrackLocalWriter{}
rtpParameters := r.api.mediaEngine.getRTPParametersByKind(
trackEncoding.track.Kind(),
[]RTPTransceiverDirection{RTPTransceiverDirectionSendonly},
)
trackEncoding.srtpStream = srtpStream
trackEncoding.ssrc = parameters.Encodings[idx].SSRC
trackEncoding.ssrcRTX = parameters.Encodings[idx].RTX.SSRC
trackEncoding.ssrcFEC = parameters.Encodings[idx].FEC.SSRC
trackEncoding.rtcpInterceptor = r.api.interceptor.BindRTCPReader(
interceptor.RTCPReaderFunc(
func(in []byte, a interceptor.Attributes) (n int, attributes interceptor.Attributes, err error) {
n, err = trackEncoding.srtpStream.Read(in)
return n, a, err
},
),
)
trackEncoding.context = &baseTrackLocalContext{
id: r.id,
params: rtpParameters,
ssrc: parameters.Encodings[idx].SSRC,
ssrcFEC: parameters.Encodings[idx].FEC.SSRC,
ssrcRTX: parameters.Encodings[idx].RTX.SSRC,
writeStream: writeStream,
rtcpInterceptor: trackEncoding.rtcpInterceptor,
}
codec, err := trackEncoding.track.Bind(trackEncoding.context)
if err != nil {
return err
}
trackEncoding.context.params.Codecs = []RTPCodecParameters{codec}
trackEncoding.streamInfo = *createStreamInfo(
r.id,
parameters.Encodings[idx].SSRC,
parameters.Encodings[idx].RTX.SSRC,
parameters.Encodings[idx].FEC.SSRC,
codec.PayloadType,
findRTXPayloadType(codec.PayloadType, rtpParameters.Codecs),
0,
codec.RTPCodecCapability,
parameters.HeaderExtensions,
)
rtpInterceptor := r.api.interceptor.BindLocalStream(
&trackEncoding.streamInfo,
interceptor.RTPWriterFunc(func(header *rtp.Header, payload []byte, _ interceptor.Attributes) (int, error) {
return srtpStream.WriteRTP(header, payload)
}),
)
writeStream.interceptor.Store(rtpInterceptor)
}
close(r.sendCalled)
return nil
}
// Stop irreversibly stops the RTPSender.
func (r *RTPSender) Stop() error {
r.mu.Lock()
if stopped := r.hasStopped(); stopped {
r.mu.Unlock()
return nil
}
close(r.stopCalled)
r.mu.Unlock()
if !r.hasSent() {
return nil
}
if err := r.ReplaceTrack(nil); err != nil {
return err
}
errs := []error{}
for _, trackEncoding := range r.trackEncodings {
r.api.interceptor.UnbindLocalStream(&trackEncoding.streamInfo)
if trackEncoding.srtpStream != nil {
errs = append(errs, trackEncoding.srtpStream.Close())
}
}
return util.FlattenErrs(errs)
}
// Read reads incoming RTCP for this RTPSender.
func (r *RTPSender) Read(b []byte) (n int, a interceptor.Attributes, err error) {
select {
case <-r.sendCalled:
return r.trackEncodings[0].rtcpInterceptor.Read(b, a)
case <-r.stopCalled:
return 0, nil, io.ErrClosedPipe
}
}
// ReadRTCP is a convenience method that wraps Read and unmarshals for you.
func (r *RTPSender) ReadRTCP() ([]rtcp.Packet, interceptor.Attributes, error) {
b := make([]byte, r.api.settingEngine.getReceiveMTU())
i, attributes, err := r.Read(b)
if err != nil {
return nil, nil, err
}
pkts, err := rtcp.Unmarshal(b[:i])
if err != nil {
return nil, nil, err
}
return pkts, attributes, nil
}
// ReadSimulcast reads incoming RTCP for this RTPSender for given rid.
func (r *RTPSender) ReadSimulcast(b []byte, rid string) (n int, a interceptor.Attributes, err error) {
select {
case <-r.sendCalled:
for _, t := range r.trackEncodings {
if t.track != nil && t.track.RID() == rid {
return t.rtcpInterceptor.Read(b, a)
}
}
return 0, nil, fmt.Errorf("%w: %s", errRTPSenderNoTrackForRID, rid)
case <-r.stopCalled:
return 0, nil, io.ErrClosedPipe
}
}
// ReadSimulcastRTCP is a convenience method that wraps ReadSimulcast and unmarshal for you.
func (r *RTPSender) ReadSimulcastRTCP(rid string) ([]rtcp.Packet, interceptor.Attributes, error) {
b := make([]byte, r.api.settingEngine.getReceiveMTU())
i, attributes, err := r.ReadSimulcast(b, rid)
if err != nil {
return nil, nil, err
}
pkts, err := rtcp.Unmarshal(b[:i])
return pkts, attributes, err
}
// SetReadDeadline sets the deadline for the Read operation.
// Setting to zero means no deadline.
func (r *RTPSender) SetReadDeadline(t time.Time) error {
return r.trackEncodings[0].srtpStream.SetReadDeadline(t)
}
// SetReadDeadlineSimulcast sets the max amount of time the RTCP stream for a given rid
// will block before returning. 0 is forever.
func (r *RTPSender) SetReadDeadlineSimulcast(deadline time.Time, rid string) error {
r.mu.RLock()
defer r.mu.RUnlock()
for _, t := range r.trackEncodings {
if t.track != nil && t.track.RID() == rid {
return t.srtpStream.SetReadDeadline(deadline)
}
}
return fmt.Errorf("%w: %s", errRTPSenderNoTrackForRID, rid)
}
// hasSent tells if data has been ever sent for this instance.
func (r *RTPSender) hasSent() bool {
select {
case <-r.sendCalled:
return true
default:
return false
}
}
// hasStopped tells if stop has been called.
func (r *RTPSender) hasStopped() bool {
select {
case <-r.stopCalled:
return true
default:
return false
}
}
// Set a SSRC for FEC and RTX if MediaEngine has them enabled
// If the remote doesn't support FEC or RTX we disable locally.
func (r *RTPSender) configureRTXAndFEC() {
r.mu.RLock()
defer r.mu.RUnlock()
for _, trackEncoding := range r.trackEncodings {
if !r.api.mediaEngine.isRTXEnabled(r.kind, []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}) {
trackEncoding.ssrcRTX = SSRC(0)
}
if !r.api.mediaEngine.isFECEnabled(r.kind, []RTPTransceiverDirection{RTPTransceiverDirectionSendonly}) {
trackEncoding.ssrcFEC = SSRC(0)
}
}
}