Restart PipeWire on errors
Browse files at this commitb2ca280c49d7dc804fc76da6ec9f7f1cbe8f9de5Parent: bf6995f2 changed files +59 -27
- src/niri.rs +41 -15
- src/pw_utils.rs +18 -12
src/niri.rsmodified+41 -15
| @@ -1,4 +1,4 @@ | ||
| 1 | -use std::cell::{Cell, LazyCell, OnceCell, RefCell}; | |
| 1 | +use std::cell::{Cell, OnceCell, RefCell}; | |
| 2 | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | 3 | use std::ffi::OsString; |
| 4 | 4 | use std::path::PathBuf; |
| @@ -345,7 +345,9 @@ pub struct Niri { | ||
| 345 | 345 | |
| 346 | 346 | // Casts are dropped before PipeWire to prevent a double-free (yay). |
| 347 | 347 | pub casts: Vec<Cast>, |
| 348 | - pub pipewire: LazyCell<Option<PipeWire>, Box<dyn FnOnce() -> Option<PipeWire>>>, | |
| 348 | + pub pipewire: Option<PipeWire>, | |
| 349 | + #[cfg(feature = "xdp-gnome-screencast")] | |
| 350 | + pub pw_to_niri: calloop::channel::Sender<PwToNiri>, | |
| 349 | 351 | |
| 350 | 352 | // Screencast output for each mapped window. |
| 351 | 353 | #[cfg(feature = "xdp-gnome-screencast")] |
| @@ -1504,6 +1506,16 @@ impl State { | ||
| 1504 | 1506 | }); |
| 1505 | 1507 | } |
| 1506 | 1508 | }, |
| 1509 | + PwToNiri::FatalError => { | |
| 1510 | + warn!("stopping PipeWire due to fatal error"); | |
| 1511 | + if let Some(pw) = self.niri.pipewire.take() { | |
| 1512 | + let ids: Vec<_> = self.niri.casts.iter().map(|cast| cast.session_id).collect(); | |
| 1513 | + for id in ids { | |
| 1514 | + self.niri.stop_cast(id); | |
| 1515 | + } | |
| 1516 | + self.niri.event_loop.remove(pw.token); | |
| 1517 | + } | |
| 1518 | + } | |
| 1507 | 1519 | } |
| 1508 | 1520 | } |
| 1509 | 1521 | |
| @@ -1533,10 +1545,19 @@ impl State { | ||
| 1533 | 1545 | } |
| 1534 | 1546 | }; |
| 1535 | 1547 | |
| 1536 | - let Some(pw) = &*self.niri.pipewire else { | |
| 1537 | - warn!("error starting screencast: PipeWire failed to initialize"); | |
| 1538 | - self.niri.stop_cast(session_id); | |
| 1539 | - return; | |
| 1548 | + let pw = if let Some(pw) = &self.niri.pipewire { | |
| 1549 | + pw | |
| 1550 | + } else { | |
| 1551 | + match PipeWire::new(&self.niri.event_loop, self.niri.pw_to_niri.clone()) { | |
| 1552 | + Ok(pipewire) => self.niri.pipewire.insert(pipewire), | |
| 1553 | + Err(err) => { | |
| 1554 | + warn!( | |
| 1555 | + "error starting screencast: PipeWire failed to initialize: {err:?}" | |
| 1556 | + ); | |
| 1557 | + self.niri.stop_cast(session_id); | |
| 1558 | + return; | |
| 1559 | + } | |
| 1560 | + } | |
| 1540 | 1561 | }; |
| 1541 | 1562 | |
| 1542 | 1563 | let (target, size, refresh, alpha) = match target { |
| @@ -1911,14 +1932,17 @@ impl Niri { | ||
| 1911 | 1932 | } |
| 1912 | 1933 | }; |
| 1913 | 1934 | |
| 1914 | - let loop_handle = event_loop.clone(); | |
| 1915 | - let pipewire = LazyCell::new(Box::new(move || match PipeWire::new(&loop_handle) { | |
| 1916 | - Ok(pipewire) => Some(pipewire), | |
| 1917 | - Err(err) => { | |
| 1918 | - warn!("error connecting to PipeWire, screencasting will not work: {err:?}"); | |
| 1919 | - None | |
| 1920 | - } | |
| 1921 | - }) as _); | |
| 1935 | + #[cfg(feature = "xdp-gnome-screencast")] | |
| 1936 | + let pw_to_niri = { | |
| 1937 | + let (pw_to_niri, from_pipewire) = calloop::channel::channel(); | |
| 1938 | + event_loop | |
| 1939 | + .insert_source(from_pipewire, move |event, _, state| match event { | |
| 1940 | + calloop::channel::Event::Msg(msg) => state.on_pw_msg(msg), | |
| 1941 | + calloop::channel::Event::Closed => (), | |
| 1942 | + }) | |
| 1943 | + .unwrap(); | |
| 1944 | + pw_to_niri | |
| 1945 | + }; | |
| 1922 | 1946 | |
| 1923 | 1947 | let display_source = Generic::new(display, Interest::READ, Mode::Level); |
| 1924 | 1948 | event_loop |
| @@ -2061,8 +2085,10 @@ impl Niri { | ||
| 2061 | 2085 | ipc_server, |
| 2062 | 2086 | ipc_outputs_changed: false, |
| 2063 | 2087 | |
| 2064 | - pipewire, | |
| 2088 | + pipewire: None, | |
| 2065 | 2089 | casts: vec![], |
| 2090 | + #[cfg(feature = "xdp-gnome-screencast")] | |
| 2091 | + pw_to_niri, | |
| 2066 | 2092 | |
| 2067 | 2093 | #[cfg(feature = "xdp-gnome-screencast")] |
| 2068 | 2094 | mapped_cast_output: HashMap::new(), |
src/pw_utils.rsmodified+18 -12
| @@ -11,7 +11,7 @@ use anyhow::Context as _; | ||
| 11 | 11 | use calloop::timer::{TimeoutAction, Timer}; |
| 12 | 12 | use calloop::RegistrationToken; |
| 13 | 13 | use pipewire::context::Context; |
| 14 | -use pipewire::core::Core; | |
| 14 | +use pipewire::core::{Core, PW_ID_CORE}; | |
| 15 | 15 | use pipewire::main_loop::MainLoop; |
| 16 | 16 | use pipewire::properties::Properties; |
| 17 | 17 | use pipewire::spa::buffer::DataType; |
| @@ -54,12 +54,14 @@ const CAST_DELAY_ALLOWANCE: Duration = Duration::from_micros(100); | ||
| 54 | 54 | pub struct PipeWire { |
| 55 | 55 | _context: Context, |
| 56 | 56 | pub core: Core, |
| 57 | + pub token: RegistrationToken, | |
| 57 | 58 | to_niri: calloop::channel::Sender<PwToNiri>, |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | 61 | pub enum PwToNiri { |
| 61 | 62 | StopCast { session_id: usize }, |
| 62 | 63 | Redraw(CastTarget), |
| 64 | + FatalError, | |
| 63 | 65 | } |
| 64 | 66 | |
| 65 | 67 | pub struct Cast { |
| @@ -134,15 +136,26 @@ macro_rules! make_params { | ||
| 134 | 136 | } |
| 135 | 137 | |
| 136 | 138 | impl PipeWire { |
| 137 | - pub fn new(event_loop: &LoopHandle<'static, State>) -> anyhow::Result<Self> { | |
| 139 | + pub fn new( | |
| 140 | + event_loop: &LoopHandle<'static, State>, | |
| 141 | + to_niri: calloop::channel::Sender<PwToNiri>, | |
| 142 | + ) -> anyhow::Result<Self> { | |
| 138 | 143 | let main_loop = MainLoop::new(None).context("error creating MainLoop")?; |
| 139 | 144 | let context = Context::new(&main_loop).context("error creating Context")?; |
| 140 | 145 | let core = context.connect(None).context("error creating Core")?; |
| 141 | 146 | |
| 147 | + let to_niri_ = to_niri.clone(); | |
| 142 | 148 | let listener = core |
| 143 | 149 | .add_listener_local() |
| 144 | - .error(|id, seq, res, message| { | |
| 150 | + .error(move |id, seq, res, message| { | |
| 145 | 151 | warn!(id, seq, res, message, "pw error"); |
| 152 | + | |
| 153 | + // Reset PipeWire on connection errors. | |
| 154 | + if id == PW_ID_CORE && res == -32 { | |
| 155 | + if let Err(err) = to_niri_.send(PwToNiri::FatalError) { | |
| 156 | + warn!("error sending FatalError to niri: {err:?}"); | |
| 157 | + } | |
| 158 | + } | |
| 146 | 159 | }) |
| 147 | 160 | .register(); |
| 148 | 161 | mem::forget(listener); |
| @@ -154,7 +167,7 @@ impl PipeWire { | ||
| 154 | 167 | } |
| 155 | 168 | } |
| 156 | 169 | let generic = Generic::new(AsFdWrapper(main_loop), Interest::READ, Mode::Level); |
| 157 | - event_loop | |
| 170 | + let token = event_loop | |
| 158 | 171 | .insert_source(generic, move |_, wrapper, _| { |
| 159 | 172 | let _span = tracy_client::span!("pipewire iteration"); |
| 160 | 173 | wrapper.0.loop_().iterate(Duration::ZERO); |
| @@ -162,17 +175,10 @@ impl PipeWire { | ||
| 162 | 175 | }) |
| 163 | 176 | .unwrap(); |
| 164 | 177 | |
| 165 | - let (to_niri, from_pipewire) = calloop::channel::channel(); | |
| 166 | - event_loop | |
| 167 | - .insert_source(from_pipewire, move |event, _, state| match event { | |
| 168 | - calloop::channel::Event::Msg(msg) => state.on_pw_msg(msg), | |
| 169 | - calloop::channel::Event::Closed => (), | |
| 170 | - }) | |
| 171 | - .unwrap(); | |
| 172 | - | |
| 173 | 178 | Ok(Self { |
| 174 | 179 | _context: context, |
| 175 | 180 | core, |
| 181 | + token, | |
| 176 | 182 | to_niri, |
| 177 | 183 | }) |
| 178 | 184 | } |