mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Virtual audio without pactl: PipeWire-native null sink
Default Ubuntu desktops ship PipeWire/WirePlumber without pulseaudio-utils, so the pactl-only route silently fell back to plain muting. When pactl is missing, create the null sink with pw-cli (object.linger), switch the default with wpctl and destroy only our own null-audio-sink node on restore; WirePlumber moves default-following streams itself. Recovery records carry the backend (old records default to pactl). Verified on Ubuntu 24.04 (PipeWire 1.0.5, WirePlumber 0.4.17): a playing stream moves to the sink and back, the monitor is readable, SIGKILL leftovers are recovered on the next start. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9125e51350
commit
09f7169f62
1 file changed
+63
-15
+63
-15
@@ -1,19 +1,43 @@
|
||||
//! Scoped PulseAudio / PipeWire-pulse routing. Never inspect or log audio payloads.
|
||||
//! `pactl` is used when present; plain PipeWire installs without it (default Ubuntu desktop)
|
||||
//! use `pw-cli`/`pw-dump`/`wpctl`, where WirePlumber moves default-following streams itself.
|
||||
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<u32> }
|
||||
struct Route { previous: String, module: Option<u32>, #[serde(default)] pipewire: bool }
|
||||
struct State { route: Option<Route>, _lock: std::fs::File }
|
||||
static STATE: OnceLock<Mutex<Option<State>>> = OnceLock::new();
|
||||
fn path() -> std::path::PathBuf { crate::config::path().with_file_name("virtual_audio.json") }
|
||||
fn pactl(args: &[&str]) -> Result<String, ()> {
|
||||
let out = Command::new("pactl").args(args).env("LC_ALL","C").stdin(Stdio::null()).stderr(Stdio::null()).output().map_err(|_| ())?;
|
||||
fn pactl(args: &[&str]) -> Result<String, ()> { run("pactl", args) }
|
||||
fn run(program: &str, args: &[&str]) -> Result<String, ()> {
|
||||
let out = Command::new(program).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 pipewire_only() -> bool { pactl(&["info"]).is_err() && run("wpctl",&["status"]).is_ok() }
|
||||
/// Node ids whose node.name is `name`; `null_sink` restricts to null-audio-sink nodes (ownership).
|
||||
fn pw_nodes(dump: &str, name: &str, null_sink: bool) -> Vec<u32> {
|
||||
let Ok(serde_json::Value::Array(objects)) = serde_json::from_str(dump) else { return Vec::new() };
|
||||
objects.iter().filter_map(|o| {
|
||||
let props = o.get("info")?.get("props")?;
|
||||
if props.get("node.name")?.as_str()? != name { return None; }
|
||||
if null_sink && props.get("factory.name").and_then(|v| v.as_str()) != Some("support.null-audio-sink") { return None; }
|
||||
u32::try_from(o.get("id")?.as_u64()?).ok()
|
||||
}).collect()
|
||||
}
|
||||
fn pw_node(name: &str, null_sink: bool) -> Option<u32> { pw_nodes(&run("pw-dump",&["Node"]).ok()?, name, null_sink).first().copied() }
|
||||
fn default_sink(pipewire: bool) -> Result<String, ()> {
|
||||
if !pipewire { return pactl(&["get-default-sink"]); }
|
||||
run("wpctl",&["inspect","@DEFAULT_AUDIO_SINK@"])?.lines()
|
||||
.find_map(|l| l.trim().trim_start_matches("* ").strip_prefix("node.name = ").map(|v| v.trim_matches('"').to_owned())).ok_or(())
|
||||
}
|
||||
fn set_default(pipewire: bool, name: &str) -> Result<(), ()> {
|
||||
if !pipewire { return pactl(&["set-default-sink",name]).map(|_| ()); }
|
||||
run("wpctl",&["set-default",&pw_node(name,false).ok_or(())?.to_string()]).map(|_| ())
|
||||
}
|
||||
fn owned_modules(list: &str) -> Vec<u32> {
|
||||
list.lines().filter_map(|line| {
|
||||
let mut fields = line.split('\t');
|
||||
@@ -51,12 +75,17 @@ fn recover(route: &Route) -> Result<(), ()> {
|
||||
// Do not overwrite a default explicitly changed by the user during remote control.
|
||||
// The previous output may be gone (unplugged USB card): still unload our sink so the server
|
||||
// picks another default instead of leaving every stream on a null sink.
|
||||
if pactl(&["get-default-sink"]).is_ok_and(|s| s == SINK) && pactl(&["set-default-sink",&route.previous]).is_err() {
|
||||
let pw=route.pipewire;
|
||||
if default_sink(pw).is_ok_and(|s| s == SINK) && set_default(pw,&route.previous).is_err() {
|
||||
log::warn!("Previous default audio output unavailable; the sound server will choose a new default");
|
||||
}
|
||||
move_inputs(SINK,&route.previous);
|
||||
let modules=owned_modules(&pactl(&["list","short","modules"])?);
|
||||
for id in modules { pactl(&["unload-module",&id.to_string()])?; }
|
||||
if pw {
|
||||
for id in pw_nodes(&run("pw-dump",&["Node"])?,SINK,true) { run("pw-cli",&["destroy",&id.to_string()])?; }
|
||||
} else {
|
||||
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(())
|
||||
}
|
||||
@@ -84,19 +113,29 @@ pub fn enable() -> bool {
|
||||
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"])?;
|
||||
let pipewire=pipewire_only();
|
||||
let previous=default_sink(pipewire)?;
|
||||
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 };
|
||||
if if pipewire { pw_node(SINK,false).is_some() } else { sink_id(SINK).is_some() } { return Err(()); }
|
||||
let mut route=Route { previous, module:None, pipewire };
|
||||
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(|_| ())?);
|
||||
state.route=Some(Route { previous:route.previous.clone(), module:None, pipewire });
|
||||
let id=if pipewire {
|
||||
// object.linger keeps the node after pw-cli exits; recover() destroys it.
|
||||
run("pw-cli",&["create-node","adapter","{ factory.name=support.null-audio-sink node.name=uu-remote-sink \
|
||||
node.description=UU-Remote media.class=Audio/Sink object.linger=true audio.position=[FL FR] }"])?;
|
||||
let mut node=None;
|
||||
for _ in 0..20 { node=pw_node(SINK,true); if node.is_some() { break; } std::thread::sleep(std::time::Duration::from_millis(50)); }
|
||||
node.ok_or(())?
|
||||
} else {
|
||||
pactl(&["load-module","module-null-sink","sink_name=uu-remote-sink","sink_properties=device.description=UU-Remote"])?.parse().map_err(|_| ())?
|
||||
};
|
||||
route.module=Some(id);
|
||||
persist(&route)?;
|
||||
state.route=Some(route);
|
||||
pactl(&["set-default-sink",SINK])?;
|
||||
move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK);
|
||||
set_default(pipewire,SINK)?;
|
||||
if !pipewire { move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK); }
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
@@ -126,6 +165,15 @@ mod tests {
|
||||
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]);
|
||||
}
|
||||
#[test] fn pipewire_recovery_only_claims_owned_null_sink() {
|
||||
let dump=r#"[{"id":5,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"support.null-audio-sink"}}},
|
||||
{"id":6,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"api.alsa.pcm.sink"}}},
|
||||
{"id":7,"info":{"props":{"node.name":"other","factory.name":"support.null-audio-sink"}}},{"id":8}]"#;
|
||||
assert_eq!(pw_nodes(dump,"uu-remote-sink",true),vec![5]);
|
||||
assert_eq!(pw_nodes(dump,"uu-remote-sink",false),vec![5,6]);
|
||||
let old:Route=serde_json::from_str(r#"{"previous":"a","module":3}"#).unwrap();
|
||||
assert!(!old.pipewire);
|
||||
}
|
||||
}
|
||||
|
||||
/// Also restore on ordinary returns from the host; SIGKILL is recovered next startup.
|
||||
|
||||
Reference in new issue
Block a user