@@ -25,9 +25,7 @@ use crate::rtp_transceiver::{
25
25
create_stream_info, PayloadType , RTCRtpEncodingParameters , RTCRtpSendParameters ,
26
26
RTCRtpTransceiver , SSRC ,
27
27
} ;
28
- use crate :: track:: track_local:: {
29
- InterceptorToTrackLocalWriter , TrackLocal , TrackLocalContext , TrackLocalWriter ,
30
- } ;
28
+ use crate :: track:: track_local:: { InterceptorToTrackLocalWriter , TrackLocal , TrackLocalContext } ;
31
29
32
30
pub ( crate ) struct RTPSenderInternal {
33
31
pub ( crate ) stop_called_rx : Arc < Notify > ,
@@ -38,8 +36,8 @@ pub(crate) struct TrackEncoding {
38
36
pub ( crate ) track : Arc < dyn TrackLocal + Send + Sync > ,
39
37
pub ( crate ) srtp_stream : Arc < SrtpWriterFuture > ,
40
38
pub ( crate ) rtcp_interceptor : Arc < dyn RTCPReader + Send + Sync > ,
41
- pub ( crate ) stream_info : Mutex < StreamInfo > ,
42
- pub ( crate ) context : Mutex < TrackLocalContext > ,
39
+ pub ( crate ) stream_info : StreamInfo ,
40
+ pub ( crate ) context : TrackLocalContext ,
43
41
44
42
pub ( crate ) ssrc : SSRC ,
45
43
@@ -275,12 +273,21 @@ impl RTCRtpSender {
275
273
None
276
274
} ;
277
275
276
+ let write_stream = Arc :: new ( InterceptorToTrackLocalWriter :: new ( self . paused . clone ( ) ) ) ;
277
+ let context = TrackLocalContext {
278
+ id : self . id . clone ( ) ,
279
+ params : super :: RTCRtpParameters :: default ( ) ,
280
+ ssrc : 0 ,
281
+ write_stream,
282
+ paused : self . paused . clone ( ) ,
283
+ mid : None ,
284
+ } ;
278
285
let encoding = TrackEncoding {
279
286
track,
280
287
srtp_stream,
281
288
rtcp_interceptor,
282
- stream_info : Mutex :: new ( StreamInfo :: default ( ) ) ,
283
- context : Mutex :: new ( TrackLocalContext :: default ( ) ) ,
289
+ stream_info : StreamInfo :: default ( ) ,
290
+ context,
284
291
ssrc,
285
292
rtx,
286
293
} ;
@@ -390,9 +397,8 @@ impl RTCRtpSender {
390
397
. first_mut ( )
391
398
. ok_or ( Error :: ErrRTPSenderNewTrackHasIncorrectEnvelope ) ?;
392
399
393
- let mut context = encoding. context . lock ( ) . await ;
394
400
if self . has_sent ( ) {
395
- encoding. track . unbind ( & context) . await ?;
401
+ encoding. track . unbind ( & encoding . context ) . await ?;
396
402
}
397
403
398
404
self . seq_trans . reset_offset ( ) ;
@@ -406,35 +412,34 @@ impl RTCRtpSender {
406
412
. and_then ( |t| t. mid ( ) ) ;
407
413
408
414
let new_context = TrackLocalContext {
409
- id : context. id . clone ( ) ,
415
+ id : encoding . context . id . clone ( ) ,
410
416
params : self
411
417
. media_engine
412
418
. get_rtp_parameters_by_kind ( t. kind ( ) , RTCRtpTransceiverDirection :: Sendonly ) ,
413
- ssrc : context. ssrc ,
414
- write_stream : context. write_stream . clone ( ) ,
419
+ ssrc : encoding . context . ssrc ,
420
+ write_stream : encoding . context . write_stream . clone ( ) ,
415
421
paused : self . paused . clone ( ) ,
416
422
mid,
417
423
} ;
418
424
419
425
match t. bind ( & new_context) . await {
420
426
Err ( err) => {
421
427
// Re-bind the original track
422
- encoding. track . bind ( & context) . await ?;
428
+ encoding. track . bind ( & encoding . context ) . await ?;
423
429
424
430
Err ( err)
425
431
}
426
432
Ok ( codec) => {
427
433
// Codec has changed
428
- context. params . codecs = vec ! [ codec] ;
434
+ encoding . context . params . codecs = vec ! [ codec] ;
429
435
encoding. track = Arc :: clone ( t) ;
430
436
Ok ( ( ) )
431
437
}
432
438
}
433
439
} else {
434
440
if self . has_sent ( ) {
435
441
for encoding in track_encodings. drain ( ..) {
436
- let context = encoding. context . lock ( ) . await ;
437
- encoding. track . unbind ( & context) . await ?;
442
+ encoding. track . unbind ( & encoding. context ) . await ?;
438
443
}
439
444
} else {
440
445
track_encodings. clear ( ) ;
@@ -449,7 +454,7 @@ impl RTCRtpSender {
449
454
if self . has_sent ( ) {
450
455
return Err ( Error :: ErrRTPSenderSendAlreadyCalled ) ;
451
456
}
452
- let track_encodings = self . track_encodings . lock ( ) . await ;
457
+ let mut track_encodings = self . track_encodings . lock ( ) . await ;
453
458
if track_encodings. is_empty ( ) {
454
459
return Err ( Error :: ErrRTPSenderTrackRemoved ) ;
455
460
}
@@ -461,41 +466,33 @@ impl RTCRtpSender {
461
466
. and_then ( |t| t. upgrade ( ) )
462
467
. and_then ( |t| t. mid ( ) ) ;
463
468
464
- for ( idx, encoding) in track_encodings. iter ( ) . enumerate ( ) {
469
+ for ( idx, encoding) in track_encodings. iter_mut ( ) . enumerate ( ) {
465
470
let write_stream = Arc :: new ( InterceptorToTrackLocalWriter :: new ( self . paused . clone ( ) ) ) ;
466
- let mut context = TrackLocalContext {
467
- id : self . id . clone ( ) ,
468
- params : self . media_engine . get_rtp_parameters_by_kind (
469
- encoding. track . kind ( ) ,
470
- RTCRtpTransceiverDirection :: Sendonly ,
471
- ) ,
472
- ssrc : parameters. encodings [ idx] . ssrc ,
473
- write_stream : Some (
474
- Arc :: clone ( & write_stream) as Arc < dyn TrackLocalWriter + Send + Sync >
475
- ) ,
476
- paused : self . paused . clone ( ) ,
477
- mid : mid. to_owned ( ) ,
478
- } ;
471
+ encoding. context . params = self . media_engine . get_rtp_parameters_by_kind (
472
+ encoding. track . kind ( ) ,
473
+ RTCRtpTransceiverDirection :: Sendonly ,
474
+ ) ;
475
+ encoding. context . ssrc = parameters. encodings [ idx] . ssrc ;
476
+ encoding. context . write_stream = Arc :: clone ( & write_stream) as _ ;
477
+ encoding. context . mid = mid. to_owned ( ) ;
479
478
480
- let codec = encoding. track . bind ( & context) . await ?;
481
- let stream_info = create_stream_info (
479
+ let codec = encoding. track . bind ( & encoding . context ) . await ?;
480
+ encoding . stream_info = create_stream_info (
482
481
self . id . clone ( ) ,
483
482
parameters. encodings [ idx] . ssrc ,
484
483
codec. payload_type ,
485
484
codec. capability . clone ( ) ,
486
485
& parameters. rtp_parameters . header_extensions ,
487
486
None ,
488
487
) ;
489
- context. params . codecs = vec ! [ codec. clone( ) ] ;
488
+ encoding . context . params . codecs = vec ! [ codec. clone( ) ] ;
490
489
491
490
let srtp_writer = Arc :: clone ( & encoding. srtp_stream ) as Arc < dyn RTPWriter + Send + Sync > ;
492
491
let rtp_writer = self
493
492
. interceptor
494
- . bind_local_stream ( & stream_info, srtp_writer)
493
+ . bind_local_stream ( & encoding . stream_info , srtp_writer)
495
494
. await ;
496
495
497
- * encoding. context . lock ( ) . await = context;
498
- * encoding. stream_info . lock ( ) . await = stream_info;
499
496
* write_stream. interceptor_rtp_writer . lock ( ) . await = Some ( rtp_writer) ;
500
497
501
498
if let ( Some ( rtx) , Some ( rtx_codec) ) = (
@@ -573,8 +570,9 @@ impl RTCRtpSender {
573
570
574
571
let track_encodings = self . track_encodings . lock ( ) . await ;
575
572
for encoding in track_encodings. iter ( ) {
576
- let stream_info = encoding. stream_info . lock ( ) . await ;
577
- self . interceptor . unbind_local_stream ( & stream_info) . await ;
573
+ self . interceptor
574
+ . unbind_local_stream ( & encoding. stream_info )
575
+ . await ;
578
576
579
577
encoding. srtp_stream . close ( ) . await ?;
580
578
0 commit comments