@@ -6,6 +6,7 @@ use crate::rtp_transceiver::{PayloadType, SSRC};
6
6
use crate :: rtp_transceiver:: rtp_receiver:: RTPReceiverInternal ;
7
7
8
8
use crate :: track:: RTP_PAYLOAD_TYPE_BITMASK ;
9
+ use arc_swap:: ArcSwapOption ;
9
10
use bytes:: { Bytes , BytesMut } ;
10
11
use interceptor:: { Attributes , Interceptor } ;
11
12
use std:: collections:: VecDeque ;
@@ -27,8 +28,8 @@ pub type OnMuteHdlrFn = Box<
27
28
28
29
#[ derive( Default ) ]
29
30
struct Handlers {
30
- on_mute : Option < OnMuteHdlrFn > ,
31
- on_unmute : Option < OnMuteHdlrFn > ,
31
+ on_mute : ArcSwapOption < Mutex < OnMuteHdlrFn > > ,
32
+ on_unmute : ArcSwapOption < Mutex < OnMuteHdlrFn > > ,
32
33
}
33
34
34
35
#[ derive( Default ) ]
@@ -54,7 +55,7 @@ pub struct TrackRemote {
54
55
media_engine : Arc < MediaEngine > ,
55
56
interceptor : Arc < dyn Interceptor + Send + Sync > ,
56
57
57
- handlers : Mutex < Handlers > ,
58
+ handlers : Arc < Handlers > ,
58
59
59
60
receiver : Option < Weak < RTPReceiverInternal > > ,
60
61
internal : Mutex < TrackRemoteInternal > ,
@@ -193,20 +194,22 @@ impl TrackRemote {
193
194
* p = params;
194
195
}
195
196
196
- pub async fn onmute < F > ( & self , handler : F )
197
+ pub fn onmute < F > ( & self , handler : F )
197
198
where
198
199
F : FnMut ( ) -> Pin < Box < dyn Future < Output = ( ) > + Send + ' static > > + Send + ' static + Sync ,
199
200
{
200
- let mut handlers = self . handlers . lock ( ) . await ;
201
- handlers. on_mute = Some ( Box :: new ( handler) ) ;
201
+ self . handlers
202
+ . on_mute
203
+ . store ( Some ( Arc :: new ( Mutex :: new ( Box :: new ( handler) ) ) ) ) ;
202
204
}
203
205
204
- pub async fn onunmute < F > ( & self , handler : F )
206
+ pub fn onunmute < F > ( & self , handler : F )
205
207
where
206
208
F : FnMut ( ) -> Pin < Box < dyn Future < Output = ( ) > + Send + ' static > > + Send + ' static + Sync ,
207
209
{
208
- let mut handlers = self . handlers . lock ( ) . await ;
209
- handlers. on_unmute = Some ( Box :: new ( handler) ) ;
210
+ self . handlers
211
+ . on_unmute
212
+ . store ( Some ( Arc :: new ( Mutex :: new ( Box :: new ( handler) ) ) ) ) ;
210
213
}
211
214
212
215
/// Reads data from the track.
@@ -311,20 +314,18 @@ impl TrackRemote {
311
314
}
312
315
313
316
pub ( crate ) async fn fire_onmute ( & self ) {
314
- let mut handlers = self . handlers . lock ( ) . await ;
317
+ let on_mute = self . handlers . on_mute . load ( ) ;
315
318
316
- match & mut handlers. on_mute {
317
- Some ( f) => f ( ) . await ,
318
- None => { }
319
+ if let Some ( f) = on_mute. as_ref ( ) {
320
+ ( f. lock ( ) . await ) ( ) . await
319
321
} ;
320
322
}
321
323
322
324
pub ( crate ) async fn fire_onunmute ( & self ) {
323
- let mut handlers = self . handlers . lock ( ) . await ;
325
+ let on_unmute = self . handlers . on_unmute . load ( ) ;
324
326
325
- match & mut handlers. on_unmute {
326
- Some ( f) => f ( ) . await ,
327
- None => { }
327
+ if let Some ( f) = on_unmute. as_ref ( ) {
328
+ ( f. lock ( ) . await ) ( ) . await
328
329
} ;
329
330
}
330
331
}
0 commit comments