Skip to content

Commit adeadc7

Browse files
committed
muvm-guest: Add support for running as muvm-pwbridge
Support running the PipeWire bridge as a separate process, optionally with systemd socket activation. Signed-off-by: Val Packett <[email protected]>
1 parent 03bb375 commit adeadc7

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

Cargo.lock

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/muvm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tokio = { version = "1.38.0", default-features = false, features = ["io-util", "
2929
tokio-stream = { version = "0.1.15", default-features = false, features = ["net", "sync"] }
3030
udev = { version = "0.9.0", default-features = false, features = [] }
3131
uuid = { version = "1.10.0", default-features = false, features = ["serde", "std", "v7"] }
32+
listenfd = "1.0.2"
3233

3334
[[bin]]
3435
name = "muvm"

crates/muvm/src/guest/bin/muvm-guest.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use std::{cmp, env, fs, thread};
88

99
use anyhow::{anyhow, Context, Result};
1010
use muvm::guest::box64::setup_box;
11-
use muvm::guest::bridge::pipewire::start_pwbridge;
11+
use muvm::guest::bridge::common::{bridge_loop, bridge_loop_with_listenfd};
12+
use muvm::guest::bridge::pipewire::{pipewire_sock_path, PipeWireProtocolHandler};
1213
use muvm::guest::bridge::x11::start_x11bridge;
1314
use muvm::guest::fex::setup_fex;
1415
use muvm::guest::hidpipe::start_hidpipe;
@@ -31,6 +32,10 @@ fn main() -> Result<()> {
3132
let bb = binary_path.split('/').next_back().context("arg0 split")?;
3233
match bb {
3334
"muvm-configure-network" => return configure_network(),
35+
"muvm-pwbridge" => {
36+
bridge_loop_with_listenfd::<PipeWireProtocolHandler>(pipewire_sock_path);
37+
return Ok(());
38+
},
3439
"muvm-remote" => {
3540
let rt = tokio::runtime::Runtime::new().unwrap();
3641
let mut command_args = env::args().skip(1);
@@ -153,7 +158,7 @@ fn main() -> Result<()> {
153158
});
154159

155160
thread::spawn(|| {
156-
if catch_unwind(start_pwbridge).is_err() {
161+
if catch_unwind(|| bridge_loop::<PipeWireProtocolHandler>(&pipewire_sock_path())).is_err() {
157162
eprintln!("pwbridge thread crashed, pipewire passthrough will no longer function");
158163
}
159164
});

crates/muvm/src/guest/bridge/common.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,24 @@ impl<'a, T: ProtocolHandler> SubPoll<'a, T> {
960960
}
961961
}
962962

963+
pub fn bridge_loop_with_listenfd<T: ProtocolHandler>(fallback_sock_path: impl Fn() -> String) {
964+
if let Some(listen_sock) = listenfd::ListenFd::from_env()
965+
.take_unix_listener(0)
966+
.unwrap()
967+
{
968+
bridge_loop_sock::<T>(listen_sock)
969+
} else {
970+
bridge_loop::<T>(&fallback_sock_path())
971+
}
972+
}
973+
963974
pub fn bridge_loop<T: ProtocolHandler>(sock_path: &str) {
964-
let epoll = Epoll::new(EpollCreateFlags::empty()).unwrap();
965975
_ = fs::remove_file(sock_path);
966-
let listen_sock = UnixListener::bind(sock_path).unwrap();
976+
bridge_loop_sock::<T>(UnixListener::bind(sock_path).unwrap());
977+
}
978+
979+
pub fn bridge_loop_sock<T: ProtocolHandler>(listen_sock: UnixListener) {
980+
let epoll = Epoll::new(EpollCreateFlags::empty()).unwrap();
967981
epoll
968982
.add(
969983
&listen_sock,

crates/muvm/src/guest/bridge/pipewire.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use nix::errno::Errno;
99
use nix::sys::epoll::EpollFlags;
1010
use nix::sys::eventfd::{EfdFlags, EventFd};
1111

12-
use crate::guest::bridge::common;
1312
use crate::guest::bridge::common::{
1413
Client, CrossDomainHeader, CrossDomainResource, MessageResourceFinalizer, ProtocolHandler,
1514
StreamRecvResult, StreamSendResult,
@@ -139,7 +138,7 @@ impl PipeWireHeader {
139138
}
140139
}
141140

142-
struct PipeWireResourceFinalizer;
141+
pub struct PipeWireResourceFinalizer;
143142

144143
impl MessageResourceFinalizer for PipeWireResourceFinalizer {
145144
type Handler = PipeWireProtocolHandler;
@@ -168,7 +167,7 @@ impl ClientNodeData {
168167
}
169168
}
170169

171-
struct PipeWireProtocolHandler {
170+
pub struct PipeWireProtocolHandler {
172171
client_nodes: HashMap<u32, ClientNodeData>,
173172
guest_to_host_eventfds: HashMap<u64, CrossDomainEventFd>,
174173
host_to_guest_eventfds: HashMap<u32, CrossDomainEventFd>,
@@ -368,8 +367,6 @@ impl ProtocolHandler for PipeWireProtocolHandler {
368367
}
369368
}
370369

371-
pub fn start_pwbridge() {
372-
let sock_path = format!("{}/pipewire-0", env::var("XDG_RUNTIME_DIR").unwrap());
373-
374-
common::bridge_loop::<PipeWireProtocolHandler>(&sock_path)
370+
pub fn pipewire_sock_path() -> String {
371+
format!("{}/pipewire-0", env::var("XDG_RUNTIME_DIR").unwrap())
375372
}

0 commit comments

Comments
 (0)