@@ -8,7 +8,7 @@ use ice::rand::generate_crypto_random_string;
8
8
use interceptor:: stream_info:: StreamInfo ;
9
9
use interceptor:: { Attributes , Interceptor , RTCPReader , RTPWriter } ;
10
10
use portable_atomic:: AtomicBool ;
11
- use tokio:: sync:: { mpsc , Mutex , Notify } ;
11
+ use tokio:: sync:: { watch , Mutex , Notify } ;
12
12
use util:: sync:: Mutex as SyncMutex ;
13
13
14
14
use super :: srtp_writer_future:: SequenceTransformer ;
@@ -27,7 +27,6 @@ use crate::track::track_local::{
27
27
} ;
28
28
29
29
pub ( crate ) struct RTPSenderInternal {
30
- pub ( crate ) send_called_rx : Mutex < mpsc:: Receiver < ( ) > > ,
31
30
pub ( crate ) stop_called_rx : Arc < Notify > ,
32
31
pub ( crate ) stop_called_signal : Arc < AtomicBool > ,
33
32
}
@@ -71,7 +70,7 @@ pub struct RTCRtpSender {
71
70
72
71
rtp_transceiver : SyncMutex < Option < Weak < RTCRtpTransceiver > > > ,
73
72
74
- send_called_tx : SyncMutex < Option < mpsc :: Sender < ( ) > > > ,
73
+ send_called : watch :: Sender < bool > ,
75
74
stop_called_tx : Arc < Notify > ,
76
75
stop_called_signal : Arc < AtomicBool > ,
77
76
@@ -102,13 +101,12 @@ impl RTCRtpSender {
102
101
32 ,
103
102
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,
104
103
) ;
105
- let ( send_called_tx , send_called_rx ) = mpsc :: channel ( 1 ) ;
104
+ let ( send_called , _ ) = watch :: channel ( false ) ;
106
105
let stop_called_tx = Arc :: new ( Notify :: new ( ) ) ;
107
106
let stop_called_rx = stop_called_tx. clone ( ) ;
108
107
let stop_called_signal = Arc :: new ( AtomicBool :: new ( false ) ) ;
109
108
110
109
let internal = Arc :: new ( RTPSenderInternal {
111
- send_called_rx : Mutex :: new ( send_called_rx) ,
112
110
stop_called_rx,
113
111
stop_called_signal : Arc :: clone ( & stop_called_signal) ,
114
112
} ) ;
@@ -141,7 +139,7 @@ impl RTCRtpSender {
141
139
142
140
rtp_transceiver : SyncMutex :: new ( None ) ,
143
141
144
- send_called_tx : SyncMutex :: new ( Some ( send_called_tx ) ) ,
142
+ send_called ,
145
143
stop_called_tx,
146
144
stop_called_signal,
147
145
@@ -435,7 +433,7 @@ impl RTCRtpSender {
435
433
* write_stream. interceptor_rtp_writer . lock ( ) . await = Some ( rtp_writer) ;
436
434
}
437
435
438
- self . send_called_tx . lock ( ) . take ( ) ;
436
+ self . send_called . send_replace ( true ) ;
439
437
Ok ( ( ) )
440
438
}
441
439
@@ -469,10 +467,8 @@ impl RTCRtpSender {
469
467
& self ,
470
468
b : & mut [ u8 ] ,
471
469
) -> Result < ( Vec < Box < dyn rtcp:: packet:: Packet + Send + Sync > > , Attributes ) > {
472
- let mut send_called_rx = self . internal . send_called_rx . lock ( ) . await ;
473
-
474
470
tokio:: select! {
475
- _ = send_called_rx . recv ( ) => {
471
+ _ = self . wait_for_send ( ) => {
476
472
let rtcp_interceptor = {
477
473
let track_encodings = self . track_encodings. lock( ) . await ;
478
474
track_encodings. first( ) . map( |e|e. rtcp_interceptor. clone( ) )
@@ -503,10 +499,8 @@ impl RTCRtpSender {
503
499
b : & mut [ u8 ] ,
504
500
rid : & str ,
505
501
) -> Result < ( Vec < Box < dyn rtcp:: packet:: Packet + Send + Sync > > , Attributes ) > {
506
- let mut send_called_rx = self . internal . send_called_rx . lock ( ) . await ;
507
-
508
502
tokio:: select! {
509
- _ = send_called_rx . recv ( ) => {
503
+ _ = self . wait_for_send ( ) => {
510
504
let rtcp_interceptor = {
511
505
let track_encodings = self . track_encodings. lock( ) . await ;
512
506
track_encodings. iter( ) . find( |e| e. track. rid( ) == Some ( rid) ) . map( |e| e. rtcp_interceptor. clone( ) )
@@ -544,10 +538,19 @@ impl RTCRtpSender {
544
538
self . seq_trans . enable ( )
545
539
}
546
540
541
+ /// Will asynchronously block/wait until send() has been called
542
+ ///
543
+ /// Note that it could return if underlying channel is closed,
544
+ /// however this shouldn't happen as we have a reference to self
545
+ /// which again owns the underlying channel.
546
+ pub async fn wait_for_send ( & self ) {
547
+ let mut watch = self . send_called . subscribe ( ) ;
548
+ let _ = watch. wait_for ( |r| * r) . await ;
549
+ }
550
+
547
551
/// has_sent tells if data has been ever sent for this instance
548
552
pub ( crate ) fn has_sent ( & self ) -> bool {
549
- let send_called_tx = self . send_called_tx . lock ( ) ;
550
- send_called_tx. is_none ( )
553
+ * self . send_called . borrow ( )
551
554
}
552
555
553
556
/// has_stopped tells if stop has been called
0 commit comments