forge
Explore
Log in

Don't you dare go Hollow

Restart PipeWire on errors

Browse files at this commit

b2ca280c49d7dc804fc76da6ec9f7f1cbe8f9de5Ivan MolodetskikhParent: bf6995f
This lets you restart pipewire and then get a screencast successfully.
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 use std::collections::{HashMap, HashSet};2 use std::collections::{HashMap, HashSet};
3 use std::ffi::OsString;3 use std::ffi::OsString;
4 use std::path::PathBuf;4 use std::path::PathBuf;
@@ -345,7 +345,9 @@ pub struct Niri {
345 345
346 // Casts are dropped before PipeWire to prevent a double-free (yay).346 // Casts are dropped before PipeWire to prevent a double-free (yay).
347 pub casts: Vec<Cast>,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 // Screencast output for each mapped window.352 // Screencast output for each mapped window.
351 #[cfg(feature = "xdp-gnome-screencast")]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 {1548+ let pw = if let Some(pw) = &self.niri.pipewire {
1537- warn!("error starting screencast: PipeWire failed to initialize");1549+ pw
1538- self.niri.stop_cast(session_id);1550+ } else {
1539- return;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 let (target, size, refresh, alpha) = match target {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();1935+ #[cfg(feature = "xdp-gnome-screencast")]
1915- let pipewire = LazyCell::new(Box::new(move || match PipeWire::new(&loop_handle) {1936+ let pw_to_niri = {
1916- Ok(pipewire) => Some(pipewire),1937+ let (pw_to_niri, from_pipewire) = calloop::channel::channel();
1917- Err(err) => {1938+ event_loop
1918- warn!("error connecting to PipeWire, screencasting will not work: {err:?}");1939+ .insert_source(from_pipewire, move |event, _, state| match event {
1919- None1940+ calloop::channel::Event::Msg(msg) => state.on_pw_msg(msg),
1920- }1941+ calloop::channel::Event::Closed => (),
1921- }) as _);1942+ })
1943+ .unwrap();
1944+ pw_to_niri
1945+ };
1922 1946
1923 let display_source = Generic::new(display, Interest::READ, Mode::Level);1947 let display_source = Generic::new(display, Interest::READ, Mode::Level);
1924 event_loop1948 event_loop
@@ -2061,8 +2085,10 @@ impl Niri {
2061 ipc_server,2085 ipc_server,
2062 ipc_outputs_changed: false,2086 ipc_outputs_changed: false,
2063 2087
2064- pipewire,2088+ pipewire: None,
2065 casts: vec![],2089 casts: vec![],
2090+ #[cfg(feature = "xdp-gnome-screencast")]
2091+ pw_to_niri,
2066 2092
2067 #[cfg(feature = "xdp-gnome-screencast")]2093 #[cfg(feature = "xdp-gnome-screencast")]
2068 mapped_cast_output: HashMap::new(),2094 mapped_cast_output: HashMap::new(),
src/pw_utils.rsmodified+18 -12
@@ -11,7 +11,7 @@ use anyhow::Context as _;
11 use calloop::timer::{TimeoutAction, Timer};11 use calloop::timer::{TimeoutAction, Timer};
12 use calloop::RegistrationToken;12 use calloop::RegistrationToken;
13 use pipewire::context::Context;13 use pipewire::context::Context;
14-use pipewire::core::Core;14+use pipewire::core::{Core, PW_ID_CORE};
15 use pipewire::main_loop::MainLoop;15 use pipewire::main_loop::MainLoop;
16 use pipewire::properties::Properties;16 use pipewire::properties::Properties;
17 use pipewire::spa::buffer::DataType;17 use pipewire::spa::buffer::DataType;
@@ -54,12 +54,14 @@ const CAST_DELAY_ALLOWANCE: Duration = Duration::from_micros(100);
54 pub struct PipeWire {54 pub struct PipeWire {
55 _context: Context,55 _context: Context,
56 pub core: Core,56 pub core: Core,
57+ pub token: RegistrationToken,
57 to_niri: calloop::channel::Sender<PwToNiri>,58 to_niri: calloop::channel::Sender<PwToNiri>,
58 }59 }
59 60
60 pub enum PwToNiri {61 pub enum PwToNiri {
61 StopCast { session_id: usize },62 StopCast { session_id: usize },
62 Redraw(CastTarget),63 Redraw(CastTarget),
64+ FatalError,
63 }65 }
64 66
65 pub struct Cast {67 pub struct Cast {
@@ -134,15 +136,26 @@ macro_rules! make_params {
134 }136 }
135 137
136 impl PipeWire {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 let main_loop = MainLoop::new(None).context("error creating MainLoop")?;143 let main_loop = MainLoop::new(None).context("error creating MainLoop")?;
139 let context = Context::new(&main_loop).context("error creating Context")?;144 let context = Context::new(&main_loop).context("error creating Context")?;
140 let core = context.connect(None).context("error creating Core")?;145 let core = context.connect(None).context("error creating Core")?;
141 146
147+ let to_niri_ = to_niri.clone();
142 let listener = core148 let listener = core
143 .add_listener_local()149 .add_listener_local()
144- .error(|id, seq, res, message| {150+ .error(move |id, seq, res, message| {
145 warn!(id, seq, res, message, "pw error");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 .register();160 .register();
148 mem::forget(listener);161 mem::forget(listener);
@@ -154,7 +167,7 @@ impl PipeWire {
154 }167 }
155 }168 }
156 let generic = Generic::new(AsFdWrapper(main_loop), Interest::READ, Mode::Level);169 let generic = Generic::new(AsFdWrapper(main_loop), Interest::READ, Mode::Level);
157- event_loop170+ let token = event_loop
158 .insert_source(generic, move |_, wrapper, _| {171 .insert_source(generic, move |_, wrapper, _| {
159 let _span = tracy_client::span!("pipewire iteration");172 let _span = tracy_client::span!("pipewire iteration");
160 wrapper.0.loop_().iterate(Duration::ZERO);173 wrapper.0.loop_().iterate(Duration::ZERO);
@@ -162,17 +175,10 @@ impl PipeWire {
162 })175 })
163 .unwrap();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 Ok(Self {178 Ok(Self {
174 _context: context,179 _context: context,
175 core,180 core,
181+ token,
176 to_niri,182 to_niri,
177 })183 })
178 }184 }