fix stack overflow in Source impl

David Kleingeld created

Change summary

crates/audio/src/audio.rs                            |  3 +
crates/audio/src/rodio_ext.rs                        |  5 +-
crates/denoise/src/lib.rs                            |  2 
crates/livekit_client/src/livekit_client/playback.rs | 25 +++++++------
4 files changed, 19 insertions(+), 16 deletions(-)

Detailed changes

crates/audio/src/audio.rs 🔗

@@ -168,7 +168,8 @@ impl Audio {
                 SAMPLE_RATE.saturating_mul(nz!(3)),
                 SAMPLE_RATE.saturating_mul(nz!(4)),
             ])
-            .prefer_channel_counts([CHANNEL_COUNT, CHANNEL_COUNT.saturating_mul(nz!(2))])
+            .prefer_channel_counts([nz!(2)])
+            // .prefer_channel_counts([CHANNEL_COUNT, CHANNEL_COUNT.saturating_mul(nz!(2))])
             .prefer_buffer_sizes(512..)
             .open_stream()?;
         info!("Opened microphone: {:?}", stream.config());

crates/audio/src/rodio_ext.rs 🔗

@@ -112,7 +112,6 @@ impl<S: Source> RodioExt for S {
     }
     fn denoise(self) -> Result<Denoiser<Self>, DenoiserError> {
         let res = Denoiser::try_new(self);
-        log::info!("result of new: {res:?}");
         res
     }
     fn constant_params(
@@ -176,8 +175,8 @@ impl<S: Source> Iterator for ToMono<S> {
         match self.input_channel_count.get() {
             1 => self.next(),
             2 => {
-                let first_channel = self.next()?;
-                let second_channel = self.next()?;
+                let first_channel = self.inner.next().unwrap();
+                let second_channel = self.inner.next()?;
                 self.update_mean(second_channel);
 
                 if self.real_stereo() {

crates/denoise/src/lib.rs 🔗

@@ -84,7 +84,7 @@ impl<S: Source> Denoiser<S> {
             .spawn(move || {
                 run_neural_denoiser(denoised_tx, input_rx);
             })
-            .unwrap();
+            .expect("Should be a able to spawn threads");
 
         Ok(Self {
             inner: source,

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

@@ -204,17 +204,20 @@ 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::spawn(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;
-                    }
-                };
-                send_to_livekit(frame_tx, microphone);
-            });
+            thread::Builder::new()
+                .name("MicrophoneToLivekit".to_string())
+                .spawn(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;
+                        }
+                    };
+                    send_to_livekit(frame_tx, microphone);
+                })
+                .expect("should be able to spawn threads");
             Task::ready(Ok(()))
         } else {
             self.executor.spawn(async move {