Skip to content

Commit ab3a4f4

Browse files
rogurotusrainliu
authored andcommitted
sync set onmute
1 parent 6291cba commit ab3a4f4

File tree

1 file changed

+18
-17
lines changed
  • webrtc/src/track/track_remote

1 file changed

+18
-17
lines changed

webrtc/src/track/track_remote/mod.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::rtp_transceiver::{PayloadType, SSRC};
66
use crate::rtp_transceiver::rtp_receiver::RTPReceiverInternal;
77

88
use crate::track::RTP_PAYLOAD_TYPE_BITMASK;
9+
use arc_swap::ArcSwapOption;
910
use bytes::{Bytes, BytesMut};
1011
use interceptor::{Attributes, Interceptor};
1112
use std::collections::VecDeque;
@@ -27,8 +28,8 @@ pub type OnMuteHdlrFn = Box<
2728

2829
#[derive(Default)]
2930
struct Handlers {
30-
on_mute: Option<OnMuteHdlrFn>,
31-
on_unmute: Option<OnMuteHdlrFn>,
31+
on_mute: ArcSwapOption<Mutex<OnMuteHdlrFn>>,
32+
on_unmute: ArcSwapOption<Mutex<OnMuteHdlrFn>>,
3233
}
3334

3435
#[derive(Default)]
@@ -54,7 +55,7 @@ pub struct TrackRemote {
5455
media_engine: Arc<MediaEngine>,
5556
interceptor: Arc<dyn Interceptor + Send + Sync>,
5657

57-
handlers: Mutex<Handlers>,
58+
handlers: Arc<Handlers>,
5859

5960
receiver: Option<Weak<RTPReceiverInternal>>,
6061
internal: Mutex<TrackRemoteInternal>,
@@ -193,20 +194,22 @@ impl TrackRemote {
193194
*p = params;
194195
}
195196

196-
pub async fn onmute<F>(&self, handler: F)
197+
pub fn onmute<F>(&self, handler: F)
197198
where
198199
F: FnMut() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + 'static + Sync,
199200
{
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)))));
202204
}
203205

204-
pub async fn onunmute<F>(&self, handler: F)
206+
pub fn onunmute<F>(&self, handler: F)
205207
where
206208
F: FnMut() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + 'static + Sync,
207209
{
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)))));
210213
}
211214

212215
/// Reads data from the track.
@@ -311,20 +314,18 @@ impl TrackRemote {
311314
}
312315

313316
pub(crate) async fn fire_onmute(&self) {
314-
let mut handlers = self.handlers.lock().await;
317+
let on_mute = self.handlers.on_mute.load();
315318

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
319321
};
320322
}
321323

322324
pub(crate) async fn fire_onunmute(&self) {
323-
let mut handlers = self.handlers.lock().await;
325+
let on_unmute = self.handlers.on_unmute.load();
324326

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
328329
};
329330
}
330331
}

0 commit comments

Comments
 (0)