From ac0f2bf2c6dbf3860c8b30d8a6ec9654cbf5a62c Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Thu, 15 Oct 2026 10:47:31 +0800 Subject: [PATCH] Route muted sessions through a recoverable Pulse null sink --- AGENTS.md | 13 +++++ README.en.md | 13 +++++ README.md | 13 +++++ src/audio.rs | 5 +- src/main.rs | 17 +++++- src/system_actions.rs | 9 ++- src/virtual_audio.rs | 129 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 src/virtual_audio.rs diff --git a/AGENTS.md b/AGENTS.md index ba97282..2674352 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -233,3 +233,16 @@ Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI fra VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1, AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264 with a warning. Intel/AMD hardware acceptance is still required (未实机验证). + +### Virtual audio / 虚拟声卡 + +The viewer's 静音运行 switch creates `uu-remote-sink` with `pactl module-null-sink`, +moves streams from the original default output and captures `uu-remote-sink.monitor`. +New output goes to this sink; streams explicitly pinned to other outputs are untouched. +The last desktop session restores the previous default output and unloads the module. +`mute_permanent` retains the physical mute policy, but still releases the virtual sink. +A private recovery record beside the config and a process lock recover crash leftovers on startup. +PulseAudio and PipeWire-pulse use the same `pactl` interface (install `pulseaudio-utils` on Debian, +`libpulse` on Arch). When routing is unavailable, the existing mute/monitor path remains. +Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged. +`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic). diff --git a/README.en.md b/README.en.md index 1904087..eabeed3 100644 --- a/README.en.md +++ b/README.en.md @@ -271,3 +271,16 @@ Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI fra VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1, AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264 with a warning. Intel/AMD hardware acceptance is still required (未实机验证). + +### Virtual audio / 虚拟声卡 + +The viewer's 静音运行 switch creates `uu-remote-sink` with `pactl module-null-sink`, +moves streams from the original default output and captures `uu-remote-sink.monitor`. +New output goes to this sink; streams explicitly pinned to other outputs are untouched. +The last desktop session restores the previous default output and unloads the module. +`mute_permanent` retains the physical mute policy, but still releases the virtual sink. +A private recovery record beside the config and a process lock recover crash leftovers on startup. +PulseAudio and PipeWire-pulse use the same `pactl` interface (install `pulseaudio-utils` on Debian, +`libpulse` on Arch). When routing is unavailable, the existing mute/monitor path remains. +Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged. +`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic). diff --git a/README.md b/README.md index 776027e..d32041c 100644 --- a/README.md +++ b/README.md @@ -253,3 +253,16 @@ Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI fra VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1, AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264 with a warning. Intel/AMD hardware acceptance is still required (未实机验证). + +### Virtual audio / 虚拟声卡 + +The viewer's 静音运行 switch creates `uu-remote-sink` with `pactl module-null-sink`, +moves streams from the original default output and captures `uu-remote-sink.monitor`. +New output goes to this sink; streams explicitly pinned to other outputs are untouched. +The last desktop session restores the previous default output and unloads the module. +`mute_permanent` retains the physical mute policy, but still releases the virtual sink. +A private recovery record beside the config and a process lock recover crash leftovers on startup. +PulseAudio and PipeWire-pulse use the same `pactl` interface (install `pulseaudio-utils` on Debian, +`libpulse` on Arch). When routing is unavailable, the existing mute/monitor path remains. +Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged. +`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic). diff --git a/src/audio.rs b/src/audio.rs index a848917..21f9619 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -81,9 +81,9 @@ pub async fn stream_audio(pc: Arc, track: Arc(25); let (stopper, job) = match start(&source, tx) { Ok(started) => started, @@ -101,7 +101,7 @@ pub async fn stream_audio(pc: Arc, track: Arc if closed(&pc) { break }, + _ = health.tick() => if closed(&pc) || crate::virtual_audio::source() != source { break }, } } // Dropping the receiver and the stopper ends the encoder at its next packet. @@ -109,6 +109,7 @@ pub async fn stream_audio(pc: Arc, track: Arc 250 { 0 } else { failures + 1 }; if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; } diff --git a/src/main.rs b/src/main.rs index 46b10f9..2ab9c59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ mod damage_capture; mod inproc_encoder; mod capture_setting; mod audio; +mod virtual_audio; mod system_actions; mod capability; mod protocol; @@ -55,7 +56,7 @@ use webrtc::WebRtcSession; async fn main() -> Result<(), Box> { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); // An unknown argument must never fall through to starting a second full host server. - const SUBCOMMANDS: [&str; 9] = ["login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture", + const SUBCOMMANDS: [&str; 10] = ["virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture", "wayland-input-probe", "clipboard-acceptance", "portal-acceptance"]; if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) { let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | ")); @@ -196,6 +197,16 @@ async fn main() -> Result<(), Box> { if std::env::args().nth(1).as_deref() == Some("unlock-check") { return session_lock::check_from_stdin().map_err(|e| e.into()); } + if std::env::args().nth(1).as_deref() == Some("virtual-audio-probe") { + virtual_audio::init(); + if !virtual_audio::enable() { return Err("Virtual audio probe setup failed".into()); } + println!("Virtual audio probe active"); + tokio::time::sleep(std::time::Duration::from_secs(5)).await; + virtual_audio::disable(); + if virtual_audio::active() { return Err("Virtual audio probe restore incomplete".into()); } + println!("Virtual audio probe ended"); + return Ok(()); + } if std::env::args().nth(1).as_deref() == Some("login") { // Interactive: register this host and bind it to an account via QR scan // (GTK window when available; `login --no-gui` keeps it terminal-only). @@ -203,6 +214,8 @@ async fn main() -> Result<(), Box> { if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown return Ok(()); } + virtual_audio::init(); + let _audio_cleanup=virtual_audio::Cleanup; log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION")); let mut capture = Arc::new((if desktop_backend::headless() { @@ -221,7 +234,7 @@ async fn main() -> Result<(), Box> { let shutdown_input=input.clone(); tokio::spawn(async move { if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { - term.recv().await; shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0); + term.recv().await; virtual_audio::disable(); shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0); } }); } diff --git a/src/system_actions.rs b/src/system_actions.rs index 62cd13e..364ce59 100644 --- a/src/system_actions.rs +++ b/src/system_actions.rs @@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String { } fn mute_for_session(state: &mut HostState) { - if state.restore.is_none() { state.restore = host_muted(); } - set_host_muted(true); + if state.restore.is_none() { + state.restore = host_muted(); + set_host_muted(true); + } + crate::virtual_audio::enable(); } fn restore_mute(state: &mut HostState) { + crate::virtual_audio::disable(); if let Some(previous) = state.restore.take() { set_host_muted(previous); } } @@ -188,6 +192,7 @@ impl PeerActions { let mut state = state().lock().unwrap(); state.desktop_sessions = state.desktop_sessions.saturating_sub(1); if state.desktop_sessions > 0 { return; } + crate::virtual_audio::disable(); if !state.settings.mute_permanent { restore_mute(&mut state); } if state.settings.auto_lock { log::info!("Auto lock after remote session: ok={}", lock_session()); diff --git a/src/virtual_audio.rs b/src/virtual_audio.rs new file mode 100644 index 0000000..e6fa7b0 --- /dev/null +++ b/src/virtual_audio.rs @@ -0,0 +1,129 @@ +//! Scoped PulseAudio / PipeWire-pulse routing. Never inspect or log audio payloads. +use std::process::{Command, Stdio}; +use std::sync::{Mutex, OnceLock}; +use std::os::fd::AsRawFd; +const SINK: &str = "uu-remote-sink"; +const MONITOR: &str = "uu-remote-sink.monitor"; +#[derive(serde::Serialize, serde::Deserialize)] +struct Route { previous: String, module: Option } +struct State { route: Option, _lock: std::fs::File } +static STATE: OnceLock>> = OnceLock::new(); +fn path() -> std::path::PathBuf { crate::config::path().with_file_name("virtual_audio.json") } +fn pactl(args: &[&str]) -> Result { + let out = Command::new("pactl").args(args).env("LC_ALL","C").stdin(Stdio::null()).stderr(Stdio::null()).output().map_err(|_| ())?; + if !out.status.success() { return Err(()); } + String::from_utf8(out.stdout).map(|s|s.trim().to_owned()).map_err(|_| ()) +} +fn owned_modules(list: &str) -> Vec { + list.lines().filter_map(|line| { + let mut fields = line.split('\t'); + let id = fields.next()?.parse().ok()?; + if fields.next()? != "module-null-sink" { return None; } + fields.next()?.split_whitespace().any(|arg| arg == "sink_name=uu-remote-sink").then_some(id) + }).collect() +} +fn sink_id(name: &str) -> Option { + pactl(&["list","short","sinks"]).ok()?.lines().find_map(|line| { + let mut f=line.split_whitespace(); let id=f.next()?; (f.next()? == name).then(|| id.to_owned()) + }) +} +fn move_inputs(from: &str, to: &str) { + let Some(id)=sink_id(from) else { return }; + if let Ok(list)=pactl(&["list","short","sink-inputs"]) { + for line in list.lines() { + let mut f=line.split_whitespace(); + if let (Some(input),Some(sink))=(f.next(),f.next()) { + if sink==id { let _=pactl(&["move-sink-input",input,to]); } + } + } + } +} +fn persist(route: &Route) -> Result<(), ()> { + use std::os::unix::fs::OpenOptionsExt; + use std::io::Write; + let temp=path().with_extension("json.new"); + let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&temp).map_err(|_| ())?; + file.write_all(&serde_json::to_vec(route).map_err(|_| ())?).map_err(|_| ())?; + file.sync_all().map_err(|_| ())?; + std::fs::rename(temp,path()).map_err(|_| ()) +} +fn recover(route: &Route) -> Result<(), ()> { + // Do not overwrite a default explicitly changed by the user during remote control. + if pactl(&["get-default-sink"])? == SINK { pactl(&["set-default-sink",&route.previous])?; } + move_inputs(SINK,&route.previous); + let modules=owned_modules(&pactl(&["list","short","modules"])?); + for id in modules { pactl(&["unload-module",&id.to_string()])?; } + let _=std::fs::remove_file(path()); + Ok(()) +} +fn state() -> &'static Mutex> { + STATE.get_or_init(|| { + use std::os::unix::fs::OpenOptionsExt; + let init=(|| { + std::fs::create_dir_all(path().parent().ok_or(())?).map_err(|_| ())?; + let lock=std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).mode(0o600) + .open(path().with_extension("lock")).map_err(|_| ())?; + if unsafe { libc::flock(lock.as_raw_fd(),libc::LOCK_EX|libc::LOCK_NB) } != 0 { return Err(()); } + let route=if path().exists() { + let old:Route=serde_json::from_slice(&std::fs::read(path()).map_err(|_| ())?).map_err(|_| ())?; + if recover(&old).is_err() { Some(old) } else { None } + } else { None }; + Ok(State { route, _lock:lock }) + })(); + Mutex::new(init.ok()) + }) +} +/// Called on startup even if mute is disabled, to recover a previous crash. +pub fn init() { let _=state(); } +pub fn enable() -> bool { + let mut guard=state().lock().unwrap(); + let Some(state)=guard.as_mut() else { return false }; + if state.route.is_some() { return pactl(&["get-default-sink"]).is_ok_and(|s|s==SINK); } + let result=(|| { + let previous=pactl(&["get-default-sink"])?; + if previous==SINK { return Err(()); } + // Refuse to claim another instance's or manually-created sink. + if sink_id(SINK).is_some() { return Err(()); } + let mut route=Route { previous, module:None }; + persist(&route)?; // Save before loading: also covers a crash between load and ID persistence. + state.route=Some(Route { previous:route.previous.clone(), module:None }); + let id=pactl(&["load-module","module-null-sink","sink_name=uu-remote-sink","sink_properties=device.description=UU-Remote"])?; + route.module=Some(id.parse().map_err(|_| ())?); + persist(&route)?; + state.route=Some(route); + pactl(&["set-default-sink",SINK])?; + move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK); + Ok(()) + })(); + if result.is_err() { + if let Some(route)=state.route.as_ref() { if recover(route).is_ok() { state.route=None; } } + log::warn!("Virtual audio routing unavailable; using the existing mute/monitor path"); + } + result.is_ok() +} +pub fn disable() { + let mut guard=state().lock().unwrap(); + if let Some(state)=guard.as_mut() { + if let Some(route)=state.route.as_ref() { + if recover(route).is_ok() { state.route=None; } + else { log::warn!("Virtual audio restore incomplete; recovery record retained"); } + } + } +} +pub fn active() -> bool { state().lock().unwrap().as_ref().is_some_and(|s|s.route.is_some()) } +pub fn source() -> String { + if state().lock().unwrap().as_ref().is_some_and(|s|s.route.as_ref().is_some_and(|r|r.module.is_some())) { MONITOR.into() } + else { std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into()) } +} +#[cfg(test)] +mod tests { + use super::*; + #[test] fn recovery_only_claims_exact_null_sink() { + let modules="1\tmodule-null-sink\tsink_name=uu-remote-sink\t0\n2\tmodule-null-sink\tsink_name=uu-remote-sink-other\t0\n3\tmodule-other\tsink_name=uu-remote-sink\t0\n"; + assert_eq!(owned_modules(modules),vec![1]); + } +} + +/// Also restore on ordinary returns from the host; SIGKILL is recovered next startup. +pub struct Cleanup; +impl Drop for Cleanup { fn drop(&mut self) { disable(); } }