Skip to content
89 changes: 78 additions & 11 deletions crates/recording/src/sources/screen_capture/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2256,32 +2256,79 @@ 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(());
}

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() {
if current_process_source_outputs().is_ok_and(|outputs| {
source_output_matches_target(&outputs, source_output, 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(
&current_process_source_outputs()?,
source_output,
source_index,
)? {
// 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 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(20));
std::thread::sleep(Duration::from_millis(50));
}

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(());
}

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"
)
}

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,
Expand Down Expand Up @@ -3271,9 +3318,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;

Expand Down Expand Up @@ -3663,6 +3710,26 @@ 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!(source_output_matches_target(&outputs, 1128, 47));
assert!(!source_output_matches_target(&outputs, 1128, 61));
assert!(!source_output_matches_target(&outputs, 1130, 47));
Comment thread
devpryan7792 marked this conversation as resolved.
Resolved
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).is_err());
}

#[test]
fn only_generic_pulse_devices_follow_the_default_input() {
assert!(uses_default_pulse_input("default"));
Expand Down