Delay starting dynamic casts until there's a target
Browse files at this commit3672e79369d72297abda8878245ea4ec327062c6Parent: 2d16abd1 changed file +120 -10
- src/screencasting/mod.rs +120 -10
src/screencasting/mod.rsmodified+120 -10
| @@ -14,8 +14,9 @@ use smithay::desktop::Window; | |||
| 14 | use smithay::output::Output; | 14 | use smithay::output::Output; |
| 15 | use smithay::reexports::gbm::Modifier; | 15 | use smithay::reexports::gbm::Modifier; |
| 16 | use smithay::utils::{Physical, Point, Scale, Size}; | 16 | use smithay::utils::{Physical, Point, Scale, Size}; |
| 17 | +use zbus::object_server::SignalEmitter; | ||
| 17 | 18 | ||
| 18 | -use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri, StreamTargetId}; | 19 | +use crate::dbus::mutter_screen_cast::{self, CursorMode, ScreenCastToNiri, StreamTargetId}; |
| 19 | use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; | 20 | use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; |
| 20 | use crate::niri_render_elements; | 21 | use crate::niri_render_elements; |
| 21 | use crate::render_helpers::RenderTarget; | 22 | use crate::render_helpers::RenderTarget; |
| @@ -27,6 +28,10 @@ use pw_utils::{Cast, CastSizeChange, CursorData, PipeWire, PwToNiri}; | |||
| 27 | 28 | ||
| 28 | pub struct Screencasting { | 29 | pub struct Screencasting { |
| 29 | pub casts: Vec<Cast>, | 30 | pub casts: Vec<Cast>, |
| 31 | + | ||
| 32 | + /// Dynamic-target casts waiting for their first target to start. | ||
| 33 | + pub pending_dynamic_casts: Vec<PendingCast>, | ||
| 34 | + | ||
| 30 | pub pw_to_niri: calloop::channel::Sender<PwToNiri>, | 35 | pub pw_to_niri: calloop::channel::Sender<PwToNiri>, |
| 31 | 36 | ||
| 32 | /// Screencast output for each mapped window. | 37 | /// Screencast output for each mapped window. |
| @@ -39,6 +44,14 @@ pub struct Screencasting { | |||
| 39 | pub pipewire: Option<PipeWire>, | 44 | pub pipewire: Option<PipeWire>, |
| 40 | } | 45 | } |
| 41 | 46 | ||
| 47 | +/// A screencast request that hasn't been started yet. | ||
| 48 | +pub struct PendingCast { | ||
| 49 | + pub session_id: usize, | ||
| 50 | + pub stream_id: usize, | ||
| 51 | + pub cursor_mode: CursorMode, | ||
| 52 | + pub signal_ctx: SignalEmitter<'static>, | ||
| 53 | +} | ||
| 54 | + | ||
| 42 | impl Screencasting { | 55 | impl Screencasting { |
| 43 | pub fn new(event_loop: &LoopHandle<'static, State>) -> Self { | 56 | pub fn new(event_loop: &LoopHandle<'static, State>) -> Self { |
| 44 | let pw_to_niri = { | 57 | let pw_to_niri = { |
| @@ -54,6 +67,7 @@ impl Screencasting { | |||
| 54 | 67 | ||
| 55 | Self { | 68 | Self { |
| 56 | casts: vec![], | 69 | casts: vec![], |
| 70 | + pending_dynamic_casts: vec![], | ||
| 57 | pw_to_niri, | 71 | pw_to_niri, |
| 58 | mapped_cast_output: HashMap::new(), | 72 | mapped_cast_output: HashMap::new(), |
| 59 | dynamic_cast_id_for_portal: MappedId::next(), | 73 | dynamic_cast_id_for_portal: MappedId::next(), |
| @@ -107,7 +121,13 @@ impl State { | |||
| 107 | warn!("stopping PipeWire due to fatal error"); | 121 | warn!("stopping PipeWire due to fatal error"); |
| 108 | let casting = &mut self.niri.casting; | 122 | let casting = &mut self.niri.casting; |
| 109 | if let Some(pw) = casting.pipewire.take() { | 123 | if let Some(pw) = casting.pipewire.take() { |
| 110 | - let ids: Vec<_> = casting.casts.iter().map(|cast| cast.session_id).collect(); | 124 | + let mut ids = HashSet::new(); |
| 125 | + for cast in &casting.pending_dynamic_casts { | ||
| 126 | + ids.insert(cast.session_id); | ||
| 127 | + } | ||
| 128 | + for cast in &casting.casts { | ||
| 129 | + ids.insert(cast.session_id); | ||
| 130 | + } | ||
| 111 | for id in ids { | 131 | for id in ids { |
| 112 | self.niri.stop_cast(id); | 132 | self.niri.stop_cast(id); |
| 113 | } | 133 | } |
| @@ -277,6 +297,88 @@ impl State { | |||
| 277 | for id in to_redraw { | 297 | for id in to_redraw { |
| 278 | self.redraw_cast(id); | 298 | self.redraw_cast(id); |
| 279 | } | 299 | } |
| 300 | + | ||
| 301 | + // Start any pending dynamic casts if we have a real target. | ||
| 302 | + if !matches!(target, CastTarget::Nothing) { | ||
| 303 | + self.start_pending_dynamic_casts(&target); | ||
| 304 | + } | ||
| 305 | + } | ||
| 306 | + | ||
| 307 | + fn start_pending_dynamic_casts(&mut self, target: &CastTarget) { | ||
| 308 | + let pending = &self.niri.casting.pending_dynamic_casts; | ||
| 309 | + if pending.is_empty() { | ||
| 310 | + return; | ||
| 311 | + } | ||
| 312 | + debug!("starting {} pending dynamic cast(s)", pending.len()); | ||
| 313 | + | ||
| 314 | + let _span = tracy_client::span!("State::start_pending_dynamic_casts"); | ||
| 315 | + | ||
| 316 | + // We don't stop dynamic casts on missing output/window. | ||
| 317 | + let (size, refresh) = match target { | ||
| 318 | + CastTarget::Nothing => panic!("dynamic cast starting target must not be Nothing"), | ||
| 319 | + CastTarget::Output(weak) => { | ||
| 320 | + let Some(output) = weak.upgrade() else { | ||
| 321 | + return; | ||
| 322 | + }; | ||
| 323 | + cast_params_for_output(&output) | ||
| 324 | + } | ||
| 325 | + CastTarget::Window { id } => { | ||
| 326 | + let Some((size, refresh)) = self.niri.cast_params_for_window(*id) else { | ||
| 327 | + return; | ||
| 328 | + }; | ||
| 329 | + (size, refresh) | ||
| 330 | + } | ||
| 331 | + }; | ||
| 332 | + | ||
| 333 | + let (gbm, render_formats) = match self.prepare_pw_cast() { | ||
| 334 | + Ok(x) => x, | ||
| 335 | + Err(err) => { | ||
| 336 | + warn!("error starting pending screencasts: {err:?}"); | ||
| 337 | + let mut ids = HashSet::new(); | ||
| 338 | + for pending in self.niri.casting.pending_dynamic_casts.drain(..) { | ||
| 339 | + ids.insert(pending.session_id); | ||
| 340 | + } | ||
| 341 | + for id in ids { | ||
| 342 | + self.niri.stop_cast(id); | ||
| 343 | + } | ||
| 344 | + return; | ||
| 345 | + } | ||
| 346 | + }; | ||
| 347 | + let pw = self.niri.casting.pipewire.as_ref().unwrap(); | ||
| 348 | + | ||
| 349 | + // Alpha is always true since the dynamic target can change between window & output. | ||
| 350 | + let alpha = true; | ||
| 351 | + | ||
| 352 | + // Start each pending cast. | ||
| 353 | + let mut to_stop = HashSet::new(); | ||
| 354 | + for pending in self.niri.casting.pending_dynamic_casts.drain(..) { | ||
| 355 | + let res = pw.start_cast( | ||
| 356 | + gbm.clone(), | ||
| 357 | + render_formats.clone(), | ||
| 358 | + pending.session_id, | ||
| 359 | + pending.stream_id, | ||
| 360 | + target.clone(), | ||
| 361 | + size, | ||
| 362 | + refresh, | ||
| 363 | + alpha, | ||
| 364 | + pending.cursor_mode, | ||
| 365 | + pending.signal_ctx, | ||
| 366 | + ); | ||
| 367 | + match res { | ||
| 368 | + Ok(mut cast) => { | ||
| 369 | + cast.dynamic_target = true; | ||
| 370 | + self.niri.casting.casts.push(cast); | ||
| 371 | + } | ||
| 372 | + Err(err) => { | ||
| 373 | + warn!("error starting pending screencast: {err:?}"); | ||
| 374 | + to_stop.insert(pending.session_id); | ||
| 375 | + } | ||
| 376 | + } | ||
| 377 | + } | ||
| 378 | + | ||
| 379 | + for session_id in to_stop { | ||
| 380 | + self.niri.stop_cast(session_id); | ||
| 381 | + } | ||
| 280 | } | 382 | } |
| 281 | 383 | ||
| 282 | pub fn on_screen_cast_msg(&mut self, msg: ScreenCastToNiri) { | 384 | pub fn on_screen_cast_msg(&mut self, msg: ScreenCastToNiri) { |
| @@ -292,7 +394,6 @@ impl State { | |||
| 292 | 394 | ||
| 293 | debug!(session_id, stream_id, "StartCast"); | 395 | debug!(session_id, stream_id, "StartCast"); |
| 294 | 396 | ||
| 295 | - let mut dynamic_target = false; | ||
| 296 | let (target, size, refresh, alpha) = match target { | 397 | let (target, size, refresh, alpha) = match target { |
| 297 | StreamTargetId::Output { name } => { | 398 | StreamTargetId::Output { name } => { |
| 298 | let global_space = &self.niri.global_space; | 399 | let global_space = &self.niri.global_space; |
| @@ -309,11 +410,17 @@ impl State { | |||
| 309 | StreamTargetId::Window { id } | 410 | StreamTargetId::Window { id } |
| 310 | if id == self.niri.casting.dynamic_cast_id_for_portal.get() => | 411 | if id == self.niri.casting.dynamic_cast_id_for_portal.get() => |
| 311 | { | 412 | { |
| 312 | - dynamic_target = true; | 413 | + debug!( |
| 313 | - | 414 | + session_id, |
| 314 | - // All dynamic casts start as Nothing to avoid surprises and exposing | 415 | + stream_id, "delaying dynamic cast until target is set" |
| 315 | - // sensitive info. | 416 | + ); |
| 316 | - (CastTarget::Nothing, Size::from((1, 1)), 1000, true) | 417 | + self.niri.casting.pending_dynamic_casts.push(PendingCast { |
| 418 | + session_id, | ||
| 419 | + stream_id, | ||
| 420 | + cursor_mode, | ||
| 421 | + signal_ctx, | ||
| 422 | + }); | ||
| 423 | + return; | ||
| 317 | } | 424 | } |
| 318 | StreamTargetId::Window { id } => { | 425 | StreamTargetId::Window { id } => { |
| 319 | let Some((size, refresh)) = self.niri.cast_params_for_window(id) else { | 426 | let Some((size, refresh)) = self.niri.cast_params_for_window(id) else { |
| @@ -348,8 +455,7 @@ impl State { | |||
| 348 | signal_ctx, | 455 | signal_ctx, |
| 349 | ); | 456 | ); |
| 350 | match res { | 457 | match res { |
| 351 | - Ok(mut cast) => { | 458 | + Ok(cast) => { |
| 352 | - cast.dynamic_target = dynamic_target; | ||
| 353 | self.niri.casting.casts.push(cast); | 459 | self.niri.casting.casts.push(cast); |
| 354 | } | 460 | } |
| 355 | Err(err) => { | 461 | Err(err) => { |
| @@ -597,6 +703,10 @@ impl Niri { | |||
| 597 | 703 | ||
| 598 | debug!(session_id, "StopCast"); | 704 | debug!(session_id, "StopCast"); |
| 599 | 705 | ||
| 706 | + self.casting | ||
| 707 | + .pending_dynamic_casts | ||
| 708 | + .retain(|p| p.session_id != session_id); | ||
| 709 | + | ||
| 600 | for i in (0..self.casting.casts.len()).rev() { | 710 | for i in (0..self.casting.casts.len()).rev() { |
| 601 | let cast = &self.casting.casts[i]; | 711 | let cast = &self.casting.casts[i]; |
| 602 | if cast.session_id != session_id { | 712 | if cast.session_id != session_id { |