fix(audio): recover the suspended DAC before changing routes

Only acknowledge the recorded sink, retaining failed recovery for reset retries. Restore the Slint device refresh settle delay and release before resetting routing settings. Abort an ALSA backend change when release fails.

cx
This commit is contained in:
vicrodh committed 2026-09-16 09:33:56 -06:00
1 parent ca5f13966e
commit 41e365efab
2 files changed
+76 -54

No files matched your search

+67 -54
View File
@@ -89,68 +89,81 @@ pub fn resume_suspended_sink() -> Result<(), String> {
return Ok(());
};
// The PCM and reservation have already been dropped by every caller, but
// PipeWire may need a short moment to recreate the parked sink. Retry the
// exact recorded name, then the default alias as a compatibility fallback.
// Keep SUSPENDED_SINK populated until one command actually succeeds: the
// old take-before-command implementation permanently lost the recovery
// target on one transient `pactl` failure while logging success anyway.
resume_sink_with(&sink, |target| {
let output = std::process::Command::new("pactl")
.args(["suspend-sink", target, "0"])
.output()
.map_err(|error| error.to_string())?;
if output.status.success() {
Ok(())
} else {
Err(String::from_utf8_lossy(&output.stderr).trim().to_string())
}
})?;
if let Ok(mut guard) = SUSPENDED_SINK.lock() {
if guard.as_deref() == Some(sink.as_str()) {
*guard = None;
}
}
log::info!("[ALSA Backend] Resumed PipeWire sink '{sink}'");
Ok(())
}
fn resume_sink_with(
sink: &str,
mut resume: impl FnMut(&str) -> Result<(), String>,
) -> Result<(), String> {
// Only success on the recorded sink counts. The default may now be HDMI
// or another DAC; waking it must not discard the pending recovery target.
let mut last_error = String::new();
for delay_ms in [0u64, 25, 75, 150] {
if delay_ms > 0 {
std::thread::sleep(std::time::Duration::from_millis(delay_ms));
}
let mut targets = vec![sink.as_str()];
if sink != "@DEFAULT_SINK@" {
targets.push("@DEFAULT_SINK@");
}
for target in targets {
match std::process::Command::new("pactl")
.args(["suspend-sink", target, "0"])
.output()
{
Ok(output) if output.status.success() => {
if let Ok(mut guard) = SUSPENDED_SINK.lock() {
if guard.as_deref() == Some(sink.as_str()) {
*guard = None;
}
}
log::info!(
"[ALSA Backend] Resumed PipeWire sink '{}' via '{}'",
sink,
target
);
return Ok(());
}
Ok(output) => {
last_error = String::from_utf8_lossy(&output.stderr).trim().to_string();
}
Err(error) => {
last_error = error.to_string();
if error.kind() == std::io::ErrorKind::NotFound {
let message = format!(
"cannot resume PipeWire sink '{}': pactl is unavailable ({error})",
sink
);
log::warn!("[ALSA Backend] {message}");
return Err(message);
}
}
}
match resume(sink) {
Ok(()) => return Ok(()),
Err(error) => last_error = error,
}
}
Err(format!(
"cannot resume PipeWire sink '{sink}' after releasing ALSA: {last_error}"
))
}
let message = format!(
"cannot resume PipeWire sink '{}' after releasing ALSA: {}",
sink,
if last_error.is_empty() {
"pactl failed without a diagnostic"
} else {
last_error.as_str()
}
);
log::warn!("[ALSA Backend] {message}");
Err(message)
#[cfg(test)]
mod sink_recovery_tests {
use super::resume_sink_with;
#[test]
fn another_default_sink_cannot_acknowledge_dac_recovery() {
let mut attempts = Vec::new();
let result = resume_sink_with("usb-dac", |target| {
attempts.push(target.to_string());
if target == "@DEFAULT_SINK@" {
Ok(())
} else {
Err("not recreated yet".into())
}
});
assert!(result.is_err());
assert_eq!(attempts, vec!["usb-dac"; 4]);
}
#[test]
fn retries_the_recorded_sink_until_it_reappears() {
let mut attempts = 0;
assert!(resume_sink_with("usb-dac", |target| {
assert_eq!(target, "usb-dac");
attempts += 1;
if attempts == 2 {
Ok(())
} else {
Err("not recreated yet".into())
}
})
.is_ok());
assert_eq!(attempts, 2);
}
}
/// Common audio sample rates to check for device support
+9
View File
@@ -4170,6 +4170,8 @@ pub async fn settings_select(runtime: &Arc<AppRuntime<LoggingAdapter>>, key: &st
// release and raced its own re-enumeration.
if let Err(error) = release_output_device_with_owner(runtime).await {
report_release_failure(&error);
publish_snapshot().await;
return;
}
}
if let Err(e) = with_audio(|s| s.set_backend_type(Some(backend))) {
@@ -4725,6 +4727,10 @@ pub async fn settings_reset(runtime: &Arc<AppRuntime<LoggingAdapter>>) {
let Some(_owner_action) = begin_audio_owner_action("reset live audio settings", false) else {
return;
};
if let Err(error) = release_output_device_with_owner(runtime).await {
report_release_failure(&error);
return;
}
if let Err(e) = with_audio(|s| s.reset_all().map(|_| ())) {
log::error!("[qbz-qt] audio settings reset failed: {e}");
}
@@ -4757,6 +4763,9 @@ pub async fn refresh_devices(runtime: &Arc<AppRuntime<LoggingAdapter>>) {
if let Err(error) = release_output_device_with_owner(runtime).await {
report_release_failure(&error);
}
// As in Slint's release button, allow WirePlumber to recreate the sink
// after PCM/reservation release before publishing the fresh device list.
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
invalidate_device_cache();
// #638 fix 3, trigger 6 — Qt-only, and the easiest of the six to miss.
// This button exists precisely for hotplug: the hardware behind the