From c929260833bb1fb5c7ffbfd18c8a31f6b63dffbc Mon Sep 17 00:00:00 2001 From: Pradyumn jha Date: Thu, 29 Oct 2026 06:19:53 +0530 Subject: [PATCH 1/5] fix(recording): resolve PipeWire system audio routing timeout on Linux (#3514) Signed-off-by: Pradyumn jha --- .../src/sources/screen_capture/linux.rs | 64 ++++++++++++++++--- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs index 7ca0ef3bed0..e37961c1a1a 100644 --- a/crates/recording/src/sources/screen_capture/linux.rs +++ b/crates/recording/src/sources/screen_capture/linux.rs @@ -2252,29 +2252,58 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< )? } }; + // Fast path: if the stream is already routed to the requested source, return immediately. + if let Ok(outputs) = current_process_source_outputs() { + if outputs + .iter() + .any(|output| output.id == source_output && output.source == source_index) + { + return Ok(()); + } + } + let status = Command::new("pactl") .args(["move-source-output", &source_output.to_string(), source]) .status() .context("move PulseAudio/PipeWire system-audio stream")?; if !status.success() { + // Idempotency fallback: if the command failed because the stream is already on + // the target source (or WirePlumber moved it concurrently), accept as success. + if let Ok(outputs) = current_process_source_outputs() { + if outputs + .iter() + .any(|output| output.id == source_output && output.source == source_index) + { + return Ok(()); + } + } bail!( "Could not route PulseAudio/PipeWire system-audio stream {source_output} to '{source}'" ); } - for _ in 0..10 { - if source_output_route_matches( - ¤t_process_source_outputs()?, - source_output, - source_index, - )? { - return Ok(()); + // PipeWire/WirePlumber handles stream moves asynchronously. Wait up to 2 seconds + // with 50ms intervals, tolerating transient states during link renegotiation. + let started = Instant::now(); + let timeout = Duration::from_millis(2000); + while started.elapsed() < timeout { + if let Ok(outputs) = current_process_source_outputs() { + if let Ok(true) = source_output_route_matches(&outputs, source_output, source_index) { + return Ok(()); + } } - std::thread::sleep(Duration::from_millis(20)); + std::thread::sleep(Duration::from_millis(50)); } + + // Final check for precise diagnostic reporting + let final_outputs = current_process_source_outputs()?; + if source_output_route_matches(&final_outputs, source_output, source_index)? { + return Ok(()); + } + bail!( - "PulseAudio/PipeWire acknowledged routing stream {source_output} to '{source}', but the input did not change" + "PulseAudio/PipeWire acknowledged routing stream {source_output} to '{source}', but the input did not change within 2 seconds" ) } @@ -3407,6 +3436,23 @@ mod system_audio_tests { assert!(source_output_route_matches(&routed, 1129, 47).is_err()); } + #[test] + fn already_matching_source_output_is_identified() { + let outputs = [ + PactlSourceOutput { + id: 1128, + source: 47, + }, + PactlSourceOutput { + id: 1129, + source: 61, + }, + ]; + assert!(outputs.iter().any(|o| o.id == 1128 && o.source == 47)); + assert!(!outputs.iter().any(|o| o.id == 1128 && o.source == 61)); + assert!(!outputs.iter().any(|o| o.id == 1130 && o.source == 47)); + } + #[test] fn only_generic_pulse_devices_follow_the_default_input() { assert!(uses_default_pulse_input("default")); From 6798fecf3e45c06c93b11cc840c41d2124603371 Mon Sep 17 00:00:00 2001 From: Pradyumn jha Date: Thu, 29 Oct 2026 07:09:10 +0530 Subject: [PATCH 2/5] refactor(recording): address review feedback on route confirmation logic - Use is_ok_and to eliminate nested conditions and comply with collapsible_if clippy rules - Remove narrative comments per AGENTS.md style guide while preserving PipeWire timing documentation - Update already_matching_source_output_is_identified test to directly verify source_output_route_matches Signed-off-by: Pradyumn jha --- .../src/sources/screen_capture/linux.rs | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs index e37961c1a1a..59d99433dbb 100644 --- a/crates/recording/src/sources/screen_capture/linux.rs +++ b/crates/recording/src/sources/screen_capture/linux.rs @@ -2252,14 +2252,12 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< )? } }; - // Fast path: if the stream is already routed to the requested source, return immediately. - if let Ok(outputs) = current_process_source_outputs() { - if outputs + if current_process_source_outputs().is_ok_and(|outputs| { + outputs .iter() .any(|output| output.id == source_output && output.source == source_index) - { - return Ok(()); - } + }) { + return Ok(()); } let status = Command::new("pactl") @@ -2268,15 +2266,12 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< .context("move PulseAudio/PipeWire system-audio stream")?; if !status.success() { - // Idempotency fallback: if the command failed because the stream is already on - // the target source (or WirePlumber moved it concurrently), accept as success. - if let Ok(outputs) = current_process_source_outputs() { - if outputs + if current_process_source_outputs().is_ok_and(|outputs| { + outputs .iter() .any(|output| output.id == source_output && output.source == source_index) - { - return Ok(()); - } + }) { + return Ok(()); } bail!( "Could not route PulseAudio/PipeWire system-audio stream {source_output} to '{source}'" @@ -2288,15 +2283,14 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< let started = Instant::now(); let timeout = Duration::from_millis(2000); while started.elapsed() < timeout { - if let Ok(outputs) = current_process_source_outputs() { - if let Ok(true) = source_output_route_matches(&outputs, source_output, source_index) { - return Ok(()); - } + if current_process_source_outputs().is_ok_and(|outputs| { + source_output_route_matches(&outputs, source_output, source_index).unwrap_or(false) + }) { + return Ok(()); } std::thread::sleep(Duration::from_millis(50)); } - // Final check for precise diagnostic reporting let final_outputs = current_process_source_outputs()?; if source_output_route_matches(&final_outputs, source_output, source_index)? { return Ok(()); @@ -3448,9 +3442,9 @@ mod system_audio_tests { source: 61, }, ]; - assert!(outputs.iter().any(|o| o.id == 1128 && o.source == 47)); - assert!(!outputs.iter().any(|o| o.id == 1128 && o.source == 61)); - assert!(!outputs.iter().any(|o| o.id == 1130 && o.source == 47)); + assert!(source_output_route_matches(&outputs, 1128, 47).unwrap()); + assert!(!source_output_route_matches(&outputs, 1128, 61).unwrap()); + assert!(!source_output_route_matches(&outputs, 1130, 47).unwrap()); } #[test] From 64886fa097b8062c18fc666b757cad4ae8b6a1da Mon Sep 17 00:00:00 2001 From: Pradyumn jha Date: Tue, 3 Nov 2026 19:38:45 +0530 Subject: [PATCH 3/5] test(recording): fix route verification assertion and extract route match helper - Extract source_output_matches_target helper to share matching logic between early return and unit tests - Assert is_err() on missing stream in already_matching_source_output_is_identified to prevent test panic Signed-off-by: Pradyumn jha --- .../src/sources/screen_capture/linux.rs | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs index 59d99433dbb..b65f14bb387 100644 --- a/crates/recording/src/sources/screen_capture/linux.rs +++ b/crates/recording/src/sources/screen_capture/linux.rs @@ -2252,11 +2252,9 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< )? } }; - if current_process_source_outputs().is_ok_and(|outputs| { - outputs - .iter() - .any(|output| output.id == source_output && output.source == source_index) - }) { + if current_process_source_outputs() + .is_ok_and(|outputs| source_output_matches_target(&outputs, source_output, source_index)) + { return Ok(()); } @@ -2267,9 +2265,7 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< if !status.success() { if current_process_source_outputs().is_ok_and(|outputs| { - outputs - .iter() - .any(|output| output.id == source_output && output.source == source_index) + source_output_matches_target(&outputs, source_output, source_index) }) { return Ok(()); } @@ -2301,6 +2297,16 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< ) } +fn source_output_matches_target( + outputs: &[PactlSourceOutput], + source_output: u32, + source_index: u32, +) -> bool { + outputs + .iter() + .any(|output| output.id == source_output && output.source == source_index) +} + fn source_output_route_matches( outputs: &[PactlSourceOutput], source_output: u32, @@ -3442,9 +3448,12 @@ mod system_audio_tests { source: 61, }, ]; + assert!(source_output_matches_target(&outputs, 1128, 47)); + assert!(!source_output_matches_target(&outputs, 1128, 61)); + assert!(!source_output_matches_target(&outputs, 1130, 47)); assert!(source_output_route_matches(&outputs, 1128, 47).unwrap()); assert!(!source_output_route_matches(&outputs, 1128, 61).unwrap()); - assert!(!source_output_route_matches(&outputs, 1130, 47).unwrap()); + assert!(source_output_route_matches(&outputs, 1130, 47).is_err()); } #[test] From 4a98e93fe4dcbe1b17ae61ccc421807af8e15c4a Mon Sep 17 00:00:00 2001 From: Pradyumn jha Date: Tue, 3 Nov 2026 19:42:20 +0530 Subject: [PATCH 4/5] test(recording): import source_output_matches_target into system_audio_tests Signed-off-by: Pradyumn jha --- crates/recording/src/sources/screen_capture/linux.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs index b65f14bb387..40f4ef30357 100644 --- a/crates/recording/src/sources/screen_capture/linux.rs +++ b/crates/recording/src/sources/screen_capture/linux.rs @@ -3044,9 +3044,9 @@ mod system_audio_tests { OwnedSystemAudioFeed, PactlSourceOutput, PulseInputRole, newly_created_source_output, pactl_monitor_preference, pactl_source_index, preferred_system_audio_device, previous_source_destination, process_source_output_ids, pulse_input_route_destination, - retry_system_audio_connection, source_output_needs_move, source_output_route_matches, - started_input_source_output, uses_default_pulse_input, wait_for_audio_after_route, - wait_for_started_source_output, + retry_system_audio_connection, source_output_matches_target, source_output_needs_move, + source_output_route_matches, started_input_source_output, uses_default_pulse_input, + wait_for_audio_after_route, wait_for_started_source_output, }; use tokio_util::sync::CancellationToken; From 2547e923448e4d85804a919d5ce6342fc5d285ef Mon Sep 17 00:00:00 2001 From: Pradyumn jha Date: Sat, 7 Nov 2026 01:55:39 +0530 Subject: [PATCH 5/5] improve(recording): log system audio routing latency and skip rationale --- .../src/sources/screen_capture/linux.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs index 94131a0971e..86f6a37f9c6 100644 --- a/crates/recording/src/sources/screen_capture/linux.rs +++ b/crates/recording/src/sources/screen_capture/linux.rs @@ -2259,6 +2259,12 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< if current_process_source_outputs() .is_ok_and(|outputs| source_output_matches_target(&outputs, source_output, source_index)) { + tracing::debug!( + stream = source_output, + destination = source, + source_index, + "PulseAudio/PipeWire system-audio stream already routed" + ); return Ok(()); } @@ -2286,6 +2292,12 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< if current_process_source_outputs().is_ok_and(|outputs| { source_output_route_matches(&outputs, source_output, source_index).unwrap_or(false) }) { + tracing::info!( + stream = source_output, + destination = source, + elapsed_ms = started.elapsed().as_millis() as u64, + "PulseAudio/PipeWire system-audio route confirmed" + ); return Ok(()); } std::thread::sleep(Duration::from_millis(50)); @@ -2293,6 +2305,12 @@ fn move_pactl_source_output(source_output: u32, source: &str) -> anyhow::Result< let final_outputs = current_process_source_outputs()?; if source_output_route_matches(&final_outputs, source_output, source_index)? { + tracing::info!( + stream = source_output, + destination = source, + elapsed_ms = started.elapsed().as_millis() as u64, + "PulseAudio/PipeWire system-audio route confirmed" + ); return Ok(()); }