livekit_client: Apply correct priority for audio threads (#51178)

Jakub Konka and Piotr Osiewicz created

Release Notes:

- N/A

Co-authored-by: Piotr Osiewicz <piotr@zed.dev>

Change summary

crates/livekit_client/src/livekit_client/playback.rs | 22 ++++++++-----
1 file changed, 13 insertions(+), 9 deletions(-)

Detailed changes

crates/livekit_client/src/livekit_client/playback.rs 🔗

@@ -29,7 +29,7 @@ use std::cell::RefCell;
 use std::sync::Weak;
 use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
 use std::time::Duration;
-use std::{borrow::Cow, collections::VecDeque, sync::Arc, thread};
+use std::{borrow::Cow, collections::VecDeque, sync::Arc};
 use util::{ResultExt as _, maybe};
 
 mod source;
@@ -139,8 +139,10 @@ impl AudioStack {
         let task = Arc::new(self.executor.spawn({
             let apm = self.apm.clone();
             let mixer = self.mixer.clone();
+            let executor = self.executor.clone();
             async move {
                 Self::play_output(
+                    executor,
                     apm,
                     mixer,
                     LEGACY_SAMPLE_RATE.get(),
@@ -225,8 +227,10 @@ impl AudioStack {
             let input_audio_device =
                 AudioSettings::try_read_global(cx, |settings| settings.input_audio_device.clone())
                     .flatten();
+            let executor = self.executor.clone();
             self.executor.spawn(async move {
                 Self::capture_input(
+                    executor,
                     apm,
                     frame_tx,
                     LEGACY_SAMPLE_RATE.get(),
@@ -250,6 +254,7 @@ impl AudioStack {
     }
 
     async fn play_output(
+        executor: BackgroundExecutor,
         apm: Arc<Mutex<apm::AudioProcessingModule>>,
         mixer: Arc<Mutex<audio_mixer::AudioMixer>>,
         sample_rate: u32,
@@ -271,9 +276,8 @@ impl AudioStack {
             let mut resampler = audio_resampler::AudioResampler::default();
             let mut buf = Vec::new();
 
-            thread::Builder::new()
-                .name("AudioPlayback".to_owned())
-                .spawn(move || {
+            executor
+                .spawn_with_priority(Priority::RealtimeAudio, async move {
                     let output_stream = output_device.build_output_stream(
                         &output_config.config(),
                         {
@@ -324,7 +328,7 @@ impl AudioStack {
                     // Block forever to keep the output stream alive
                     end_on_drop_rx.recv().ok();
                 })
-                .unwrap();
+                .detach();
 
             device_change_listener.next().await;
             drop(end_on_drop_tx)
@@ -332,6 +336,7 @@ impl AudioStack {
     }
 
     async fn capture_input(
+        executor: BackgroundExecutor,
         apm: Arc<Mutex<apm::AudioProcessingModule>>,
         frame_tx: UnboundedSender<AudioFrame<'static>>,
         sample_rate: u32,
@@ -346,9 +351,8 @@ impl AudioStack {
             let frame_tx = frame_tx.clone();
             let mut resampler = audio_resampler::AudioResampler::default();
 
-            thread::Builder::new()
-                .name("AudioCapture".to_owned())
-                .spawn(move || {
+            executor
+                .spawn_with_priority(Priority::RealtimeAudio, async move {
                     maybe!({
                         if let Some(desc) = device.description().ok() {
                             log::info!("Using microphone: {}", desc.name())
@@ -420,7 +424,7 @@ impl AudioStack {
                     })
                     .log_err();
                 })
-                .unwrap();
+                .detach();
 
             device_change_listener.next().await;
             drop(end_on_drop_tx)