From b4d2f509a6d3e0f5c5d6779b995f7b01159cd5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=CC=81o=20Monnom?= Date: Mon, 4 Sep 2023 16:44:36 -0700 Subject: [PATCH] fix: create audio_source interval in an async context --- livekit-webrtc/src/native/audio_source.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/livekit-webrtc/src/native/audio_source.rs b/livekit-webrtc/src/native/audio_source.rs index cf507d77..1783726a 100644 --- a/livekit-webrtc/src/native/audio_source.rs +++ b/livekit-webrtc/src/native/audio_source.rs @@ -57,7 +57,7 @@ impl NativeAudioSource { buf: vec![0; samples_10ms].into_boxed_slice(), len: 0, read_offset: 0, - interval: Some(interval(Duration::from_millis(10))), + interval: None, // interval must be created from a tokio runtime context })), sample_rate, num_channels, @@ -131,11 +131,14 @@ impl NativeAudioSource { } let mut inner = self.inner.lock().await; - let mut interval = inner.interval.take().unwrap(); // How can I avoid double mut borrow? + let mut interval = inner + .interval + .take() + .unwrap_or(interval(Duration::from_millis(10))); loop { let Some(data) = self.next_frame(&mut inner, frame) else { - inner.interval = Some(interval); + inner.interval = Some(interval); // Is there a better way to avoid double mut reference than taking the Option? break; };