Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(recording): preserve stereo system audio on Linux
Aditya Garud4832500b2a63
diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rsindex ce214d905..b685ef123 100644--- a/crates/recording/src/sources/microphone.rs+++ b/crates/recording/src/sources/microphone.rs@@ -363,18 +363,13 @@ impl RequiredMicrophoneHealth {}}-impl AudioSource for Microphone {- type Config = Arc<MicrophoneFeedLock>;-- #[allow(clippy::manual_async_fn)]- fn setup(- feed_lock: Self::Config,+impl Microphone {+ pub(crate) fn setup_with_channels(+ feed_lock: Arc<MicrophoneFeedLock>,mut audio_tx: mpsc::Sender<AudioFrame>,ctx: &mut crate::SetupCtx,- ) -> impl Future<Output = anyhow::Result<Self>> + 'static- where- Self: Sized,- {+ target_channels: usize,+ ) -> impl Future<Output = anyhow::Result<Self>> + 'static {let health_tx = ctx.health_tx().clone();let digital_silence_ms = ctx.digital_silence_ms();#[cfg(windows)]@@ -409,10 +404,9 @@ impl AudioSource for Microphone {let setup = async move {let source_info = feed_lock.audio_info();let audio_info = source_info- .with_max_channels(MICROPHONE_TARGET_CHANNELS).with_sample_rate(AudioMixer::INFO.sample_rate).with_sample_format(AudioMixer::INFO.sample_format)- .with_channels(MICROPHONE_TARGET_CHANNELS as usize);+ .with_channels(target_channels);let is_wireless = source_info.is_wireless_transport;let device_name = feed_lock.device_name().to_string();let reconnect_settings = microphone::MicrophoneDeviceSettings {@@ -497,7 +491,9 @@ impl AudioSource for Microphone {#[cfg(target_os = "linux")]let required_health = required_health.clone();async move {- let mut digital_silence = DigitalSilenceDetector::new(audio_info.sample_rate);+ let mut digital_silence = DigitalSilenceDetector::new(+ audio_info.sample_rate * audio_info.channels as u32,+ );let mut resampler: Option<MicResampler> = None;let mut silence_mode = false;let mut silence_start: Option<Instant> = None;@@ -961,6 +957,23 @@ impl AudioSource for Microphone {setup}}+}++impl AudioSource for Microphone {+ type Config = Arc<MicrophoneFeedLock>;++ fn setup(+ feed_lock: Self::Config,+ audio_tx: mpsc::Sender<AudioFrame>,+ ctx: &mut crate::SetupCtx,+ ) -> impl Future<Output = anyhow::Result<Self>> + 'static {+ Self::setup_with_channels(+ feed_lock,+ audio_tx,+ ctx,+ usize::from(MICROPHONE_TARGET_CHANNELS),+ )+ }fn audio_info(&self) -> AudioInfo {self.infodiff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rsindex c82a28d19..129a67096 100644--- a/crates/recording/src/sources/screen_capture/linux.rs+++ b/crates/recording/src/sources/screen_capture/linux.rs@@ -38,6 +38,9 @@ use x11rb::rust_connection::RustConnection;#[cfg(test)]mod x11_tests;+#[cfg(test)]+mod system_audio_channel_tests;+#[derive(Debug)]pub struct X11Capture;@@ -1348,7 +1351,12 @@ impl AudioSource for SystemAudioSource {Self: Sized,{let device_name = config.device_name.clone();- let setup = <crate::sources::Microphone as AudioSource>::setup(config.feed_lock, tx, ctx);+ let setup = crate::sources::Microphone::setup_with_channels(+ config.feed_lock,+ tx,+ ctx,+ X11Capture::audio_info().channels,+ );async move {let inner = setup.awaitdiff --git a/crates/recording/src/sources/screen_capture/linux/system_audio_channel_tests.rs b/crates/recording/src/sources/screen_capture/linux/system_audio_channel_tests.rsnew file mode 100644index 000000000..d7bb7570d--- /dev/null+++ b/crates/recording/src/sources/screen_capture/linux/system_audio_channel_tests.rs@@ -0,0 +1,88 @@+use super::*;+use crate::{+ feeds::microphone::synthetic::SyntheticMicrophone,+ output_pipeline::{AudioMuxer, Muxer, OutputPipeline, TaskPool},+};+use std::ops::Deref;++struct CaptureMuxer {+ info: AudioInfo,+ frames: flume::Sender<(AudioInfo, AudioFrame)>,+}++impl Muxer for CaptureMuxer {+ type Config = flume::Sender<(AudioInfo, AudioFrame)>;++ async fn setup(+ frames: Self::Config,+ _: std::path::PathBuf,+ _: Option<VideoInfo>,+ audio: Option<AudioInfo>,+ _: Arc<AtomicBool>,+ _: &mut TaskPool,+ ) -> anyhow::Result<Self> {+ Ok(Self {+ info: audio.expect("audio source configured"),+ frames,+ })+ }++ fn finish(&mut self, _: Duration) -> anyhow::Result<anyhow::Result<()>> {+ Ok(Ok(()))+ }+}++impl AudioMuxer for CaptureMuxer {+ fn send_audio_frame(&mut self, frame: AudioFrame, _: Duration) -> anyhow::Result<()> {+ self.frames.send((self.info, frame))?;+ Ok(())+ }+}++#[tokio::test]+async fn linux_system_audio_preserves_stereo_channels() {+ for rate in [44_100, 48_000, 96_000] {+ let input = SyntheticMicrophone::spawn("stereo-monitor", rate, 2);+ let lock = input.lock().await.unwrap();+ let actor = lock.deref().clone();+ let config = SystemAudioSourceConfig {+ feed_lock: Arc::new(lock),+ device_name: "stereo-monitor".into(),+ monitor_route: None,+ feed: OwnedSystemAudioFeed {+ actor,+ build_scope: None,+ stopped: false,+ },+ };+ let (frames, received) = flume::unbounded();+ let temp = tempfile::tempdir().unwrap();+ let pipeline = OutputPipeline::builder(temp.path().join("system.ogg"))+ .with_audio_source::<SystemAudioSource>(config)+ .build::<CaptureMuxer>(frames)+ .await+ .unwrap();+ let samples: Vec<f32> = (0..rate / 100).flat_map(|_| [0.25, -0.25]).collect();+ input+ .push(&samples, Timestamp::Instant(Instant::now()))+ .await;+ let (info, frame) = tokio::time::timeout(Duration::from_secs(2), received.recv_async())+ .await+ .expect("system audio reached the muxer")+ .unwrap();+ pipeline.stop().await.unwrap();++ assert_eq!(info.channels, 2, "system audio at {rate} Hz became mono");+ assert_eq!(info.sample_rate, 48_000);+ assert_eq!(frame.inner.channels(), 2);+ assert!(frame.inner.samples() > 100);+ let samples = frame.inner.samples();+ let bytes = &frame.inner.data(0)[..samples * 2 * size_of::<f32>()];+ for pair in bytes.chunks_exact(2 * size_of::<f32>()).skip(32) {+ let left = f32::from_ne_bytes(pair[..4].try_into().unwrap());+ let right = f32::from_ne_bytes(pair[4..].try_into().unwrap());+ assert!((left - 0.25).abs() < 0.001, "left channel: {left}");+ assert!((right + 0.25).abs() < 0.001, "right channel: {right}");+ }+ }+}