livekit_client: Spawn audio input with high priority (#49887)

Jakub Konka created

Use `BackgroundExecutor::spawn_with_priority(Priority::RealtimeAudio,
fut)` to spawn the audio input thread with correct thread priority much
like we do for audio output. Under-the-hood, `spawn_with_priority +
Priority::RealtimeAudio` boils down to calling `std::thread::spawn` and
setting appropriate thread priority.

Release Notes:

- N/A

Change summary

crates/livekit_client/src/livekit_client/playback.rs | 20 ++++---------
1 file changed, 6 insertions(+), 14 deletions(-)

Detailed changes

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

@@ -5,8 +5,8 @@ use cpal::traits::{DeviceTrait, StreamTrait as _};
 use futures::channel::mpsc::UnboundedSender;
 use futures::{Stream, StreamExt as _};
 use gpui::{
-    AsyncApp, BackgroundExecutor, ScreenCaptureFrame, ScreenCaptureSource, ScreenCaptureStream,
-    Task,
+    AsyncApp, BackgroundExecutor, Priority, ScreenCaptureFrame, ScreenCaptureSource,
+    ScreenCaptureStream, Task,
 };
 use libwebrtc::native::{apm, audio_mixer, audio_resampler};
 use livekit::track;
@@ -211,21 +211,13 @@ impl AudioStack {
             let voip_parts = audio::VoipParts::new(cx)?;
             // Audio needs to run real-time and should never be paused. That is
             // why we are using a normal std::thread and not a background task
-            thread::Builder::new()
-                .name("MicrophoneToLivekit".to_string())
-                .spawn(move || {
+            self.executor
+                .spawn_with_priority(Priority::RealtimeAudio, async move {
                     // microphone is non send on mac
-                    let microphone = match audio::Audio::open_microphone(voip_parts) {
-                        Ok(m) => m,
-                        Err(e) => {
-                            log::error!("Could not open microphone: {e}");
-                            return;
-                        }
-                    };
+                    let microphone = audio::Audio::open_microphone(voip_parts)?;
                     send_to_livekit(frame_tx, microphone);
+                    Ok(())
                 })
-                .expect("should be able to spawn threads");
-            Task::ready(Ok(()))
         } else {
             self.executor.spawn(async move {
                 Self::capture_input(