Extract cast session/stream ID counters to global scope
Browse files at this commit570ea119bae8e8cd3fa9677b634c173b8f3db8cfParent: df4614e4 changed files +86 -43
- src/dbus/mutter_screen_cast.rs +17 -19
- src/screencasting/mod.rs +8 -8
- src/screencasting/pw_utils.rs +15 -15
- src/utils/mod.rs +46 -1
src/dbus/mutter_screen_cast.rsmodified+17 -19
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | use std::collections::HashMap; |
| 2 | 2 | use std::mem; |
| 3 | -use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; | |
| 3 | +use std::sync::atomic::{AtomicBool, Ordering}; | |
| 4 | 4 | use std::sync::{Arc, Mutex}; |
| 5 | 5 | |
| 6 | 6 | use serde::Deserialize; |
| @@ -11,6 +11,7 @@ use zbus::{fdo, interface, ObjectServer}; | ||
| 11 | 11 | |
| 12 | 12 | use super::Start; |
| 13 | 13 | use crate::backend::IpcOutputMap; |
| 14 | +use crate::utils::{CastSessionId, CastStreamId}; | |
| 14 | 15 | |
| 15 | 16 | #[derive(Clone)] |
| 16 | 17 | pub struct ScreenCast { |
| @@ -22,7 +23,7 @@ pub struct ScreenCast { | ||
| 22 | 23 | |
| 23 | 24 | #[derive(Clone)] |
| 24 | 25 | pub struct Session { |
| 25 | - id: usize, | |
| 26 | + id: CastSessionId, | |
| 26 | 27 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 27 | 28 | to_niri: calloop::channel::Sender<ScreenCastToNiri>, |
| 28 | 29 | #[allow(clippy::type_complexity)] |
| @@ -58,12 +59,10 @@ struct RecordWindowProperties { | ||
| 58 | 59 | _is_recording: Option<bool>, |
| 59 | 60 | } |
| 60 | 61 | |
| 61 | -static STREAM_ID: AtomicUsize = AtomicUsize::new(0); | |
| 62 | - | |
| 63 | 62 | #[derive(Clone)] |
| 64 | 63 | pub struct Stream { |
| 65 | - id: usize, | |
| 66 | - session_id: usize, | |
| 64 | + id: CastStreamId, | |
| 65 | + session_id: CastSessionId, | |
| 67 | 66 | target: StreamTarget, |
| 68 | 67 | cursor_mode: CursorMode, |
| 69 | 68 | was_started: Arc<AtomicBool>, |
| @@ -94,14 +93,14 @@ struct StreamParameters { | ||
| 94 | 93 | |
| 95 | 94 | pub enum ScreenCastToNiri { |
| 96 | 95 | StartCast { |
| 97 | - session_id: usize, | |
| 98 | - stream_id: usize, | |
| 96 | + session_id: CastSessionId, | |
| 97 | + stream_id: CastStreamId, | |
| 99 | 98 | target: StreamTargetId, |
| 100 | 99 | cursor_mode: CursorMode, |
| 101 | 100 | signal_ctx: SignalEmitter<'static>, |
| 102 | 101 | }, |
| 103 | 102 | StopCast { |
| 104 | - session_id: usize, | |
| 103 | + session_id: CastSessionId, | |
| 105 | 104 | }, |
| 106 | 105 | } |
| 107 | 106 | |
| @@ -118,9 +117,8 @@ impl ScreenCast { | ||
| 118 | 117 | )); |
| 119 | 118 | } |
| 120 | 119 | |
| 121 | - static NUMBER: AtomicUsize = AtomicUsize::new(0); | |
| 122 | - let session_id = NUMBER.fetch_add(1, Ordering::SeqCst); | |
| 123 | - let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{session_id}"); | |
| 120 | + let session_id = CastSessionId::next(); | |
| 121 | + let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id.get()); | |
| 124 | 122 | let path = OwnedObjectPath::try_from(path).unwrap(); |
| 125 | 123 | |
| 126 | 124 | let session = Session::new(session_id, self.ipc_outputs.clone(), self.to_niri.clone()); |
| @@ -207,8 +205,8 @@ impl Session { | ||
| 207 | 205 | return Err(fdo::Error::Failed("monitor is disabled".to_owned())); |
| 208 | 206 | } |
| 209 | 207 | |
| 210 | - let stream_id = STREAM_ID.fetch_add(1, Ordering::SeqCst); | |
| 211 | - let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{stream_id}"); | |
| 208 | + let stream_id = CastStreamId::next(); | |
| 209 | + let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{}", stream_id.get()); | |
| 212 | 210 | let path = OwnedObjectPath::try_from(path).unwrap(); |
| 213 | 211 | |
| 214 | 212 | let cursor_mode = properties.cursor_mode.unwrap_or_default(); |
| @@ -244,8 +242,8 @@ impl Session { | ||
| 244 | 242 | ) -> fdo::Result<OwnedObjectPath> { |
| 245 | 243 | debug!(?properties, "record_window"); |
| 246 | 244 | |
| 247 | - let stream_id = STREAM_ID.fetch_add(1, Ordering::SeqCst); | |
| 248 | - let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{stream_id}"); | |
| 245 | + let stream_id = CastStreamId::next(); | |
| 246 | + let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{}", stream_id.get()); | |
| 249 | 247 | let path = OwnedObjectPath::try_from(path).unwrap(); |
| 250 | 248 | |
| 251 | 249 | let cursor_mode = properties.cursor_mode.unwrap_or_default(); |
| @@ -337,7 +335,7 @@ impl Start for ScreenCast { | ||
| 337 | 335 | |
| 338 | 336 | impl Session { |
| 339 | 337 | pub fn new( |
| 340 | - id: usize, | |
| 338 | + id: CastSessionId, | |
| 341 | 339 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 342 | 340 | to_niri: calloop::channel::Sender<ScreenCastToNiri>, |
| 343 | 341 | ) -> Self { |
| @@ -361,8 +359,8 @@ impl Drop for Session { | ||
| 361 | 359 | |
| 362 | 360 | impl Stream { |
| 363 | 361 | fn new( |
| 364 | - id: usize, | |
| 365 | - session_id: usize, | |
| 362 | + id: CastStreamId, | |
| 363 | + session_id: CastSessionId, | |
| 366 | 364 | target: StreamTarget, |
| 367 | 365 | cursor_mode: CursorMode, |
| 368 | 366 | to_niri: calloop::channel::Sender<ScreenCastToNiri>, |
src/screencasting/mod.rsmodified+8 -8
| @@ -20,7 +20,7 @@ use crate::dbus::mutter_screen_cast::{self, CursorMode, ScreenCastToNiri, Stream | ||
| 20 | 20 | use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; |
| 21 | 21 | use crate::niri_render_elements; |
| 22 | 22 | use crate::render_helpers::RenderTarget; |
| 23 | -use crate::utils::get_monotonic_time; | |
| 23 | +use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId}; | |
| 24 | 24 | use crate::window::mapped::{MappedId, WindowCastRenderElements}; |
| 25 | 25 | |
| 26 | 26 | mod pw_utils; |
| @@ -46,8 +46,8 @@ pub struct Screencasting { | ||
| 46 | 46 | |
| 47 | 47 | /// A screencast request that hasn't been started yet. |
| 48 | 48 | pub struct PendingCast { |
| 49 | - pub session_id: usize, | |
| 50 | - pub stream_id: usize, | |
| 49 | + pub session_id: CastSessionId, | |
| 50 | + pub stream_id: CastStreamId, | |
| 51 | 51 | pub cursor_mode: CursorMode, |
| 52 | 52 | pub signal_ctx: SignalEmitter<'static>, |
| 53 | 53 | } |
| @@ -137,7 +137,7 @@ impl State { | ||
| 137 | 137 | } |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - fn redraw_cast(&mut self, stream_id: usize) { | |
| 140 | + fn redraw_cast(&mut self, stream_id: CastStreamId) { | |
| 141 | 141 | let _span = tracy_client::span!("State::redraw_cast"); |
| 142 | 142 | |
| 143 | 143 | let casts = &mut self.niri.casting.casts; |
| @@ -391,7 +391,7 @@ impl State { | ||
| 391 | 391 | signal_ctx, |
| 392 | 392 | } => { |
| 393 | 393 | let _span = tracy_client::span!("StartCast"); |
| 394 | - let _span = debug_span!("StartCast", session_id, stream_id).entered(); | |
| 394 | + let _span = debug_span!("StartCast", %session_id, %stream_id).entered(); | |
| 395 | 395 | |
| 396 | 396 | let (target, size, refresh, alpha) = match target { |
| 397 | 397 | StreamTargetId::Output { name } => { |
| @@ -694,9 +694,9 @@ impl Niri { | ||
| 694 | 694 | } |
| 695 | 695 | } |
| 696 | 696 | |
| 697 | - fn stop_cast(&mut self, session_id: usize) { | |
| 697 | + fn stop_cast(&mut self, session_id: CastSessionId) { | |
| 698 | 698 | let _span = tracy_client::span!("Niri::stop_cast"); |
| 699 | - let _span = debug_span!("stop_cast", session_id).entered(); | |
| 699 | + let _span = debug_span!("stop_cast", %session_id).entered(); | |
| 700 | 700 | |
| 701 | 701 | self.casting |
| 702 | 702 | .pending_dynamic_casts |
| @@ -716,7 +716,7 @@ impl Niri { | ||
| 716 | 716 | |
| 717 | 717 | let dbus = &self.dbus.as_ref().unwrap(); |
| 718 | 718 | let server = dbus.conn_screen_cast.as_ref().unwrap().object_server(); |
| 719 | - let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{session_id}"); | |
| 719 | + let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id.get()); | |
| 720 | 720 | if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) { |
| 721 | 721 | let _span = tracy_client::span!("invoking Session::stop"); |
| 722 | 722 |
src/screencasting/pw_utils.rsmodified+15 -15
| @@ -55,7 +55,7 @@ use crate::render_helpers::{ | ||
| 55 | 55 | clear_dmabuf, encompassing_geo, render_and_download, render_to_dmabuf, |
| 56 | 56 | }; |
| 57 | 57 | use crate::screencasting::CastRenderElement; |
| 58 | -use crate::utils::get_monotonic_time; | |
| 58 | +use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId}; | |
| 59 | 59 | |
| 60 | 60 | // Give a 0.1 ms allowance for presentation time errors. |
| 61 | 61 | const CAST_DELAY_ALLOWANCE: Duration = Duration::from_micros(100); |
| @@ -79,15 +79,15 @@ pub struct PipeWire { | ||
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | pub enum PwToNiri { |
| 82 | - StopCast { session_id: usize }, | |
| 83 | - Redraw { stream_id: usize }, | |
| 82 | + StopCast { session_id: CastSessionId }, | |
| 83 | + Redraw { stream_id: CastStreamId }, | |
| 84 | 84 | FatalError, |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | pub struct Cast { |
| 88 | 88 | event_loop: LoopHandle<'static, State>, |
| 89 | - pub session_id: usize, | |
| 90 | - pub stream_id: usize, | |
| 89 | + pub session_id: CastSessionId, | |
| 90 | + pub stream_id: CastStreamId, | |
| 91 | 91 | // Listener is dropped before Stream to prevent a use-after-free. |
| 92 | 92 | _listener: StreamListener<()>, |
| 93 | 93 | pub stream: StreamRc, |
| @@ -269,8 +269,8 @@ impl PipeWire { | ||
| 269 | 269 | &self, |
| 270 | 270 | gbm: GbmDevice<DrmDeviceFd>, |
| 271 | 271 | formats: FormatSet, |
| 272 | - session_id: usize, | |
| 273 | - stream_id: usize, | |
| 272 | + session_id: CastSessionId, | |
| 273 | + stream_id: CastStreamId, | |
| 274 | 274 | target: CastTarget, |
| 275 | 275 | size: Size<i32, Physical>, |
| 276 | 276 | refresh: u32, |
| @@ -283,13 +283,13 @@ impl PipeWire { | ||
| 283 | 283 | let to_niri_ = self.to_niri.clone(); |
| 284 | 284 | let stop_cast = move || { |
| 285 | 285 | if let Err(err) = to_niri_.send(PwToNiri::StopCast { session_id }) { |
| 286 | - warn!(session_id, "error sending StopCast to niri: {err:?}"); | |
| 286 | + warn!(%session_id, "error sending StopCast to niri: {err:?}"); | |
| 287 | 287 | } |
| 288 | 288 | }; |
| 289 | 289 | let to_niri_ = self.to_niri.clone(); |
| 290 | 290 | let redraw = move || { |
| 291 | 291 | if let Err(err) = to_niri_.send(PwToNiri::Redraw { stream_id }) { |
| 292 | - warn!(stream_id, "error sending Redraw to niri: {err:?}"); | |
| 292 | + warn!(%stream_id, "error sending Redraw to niri: {err:?}"); | |
| 293 | 293 | } |
| 294 | 294 | }; |
| 295 | 295 | let redraw_ = redraw.clone(); |
| @@ -328,7 +328,7 @@ impl PipeWire { | ||
| 328 | 328 | let inner = inner.clone(); |
| 329 | 329 | let stop_cast = stop_cast.clone(); |
| 330 | 330 | move |stream, (), old, new| { |
| 331 | - let _span = debug_span!("state_changed", stream_id).entered(); | |
| 331 | + let _span = debug_span!("state_changed", %stream_id).entered(); | |
| 332 | 332 | debug!("{old:?} -> {new:?}"); |
| 333 | 333 | let mut inner = inner.borrow_mut(); |
| 334 | 334 | |
| @@ -378,7 +378,7 @@ impl PipeWire { | ||
| 378 | 378 | let formats = formats.clone(); |
| 379 | 379 | move |stream, (), id, pod| { |
| 380 | 380 | let id = ParamType::from_raw(id); |
| 381 | - trace!(stream_id, ?id, "param_changed"); | |
| 381 | + trace!(%stream_id, ?id, "param_changed"); | |
| 382 | 382 | let mut inner = inner.borrow_mut(); |
| 383 | 383 | let inner = &mut *inner; |
| 384 | 384 | |
| @@ -386,7 +386,7 @@ impl PipeWire { | ||
| 386 | 386 | return; |
| 387 | 387 | } |
| 388 | 388 | |
| 389 | - let _span = debug_span!("param_changed", stream_id).entered(); | |
| 389 | + let _span = debug_span!("param_changed", %stream_id).entered(); | |
| 390 | 390 | |
| 391 | 391 | let Some(pod) = pod else { return }; |
| 392 | 392 | |
| @@ -678,7 +678,7 @@ impl PipeWire { | ||
| 678 | 678 | let inner = inner.clone(); |
| 679 | 679 | let stop_cast = stop_cast.clone(); |
| 680 | 680 | move |stream, (), buffer| { |
| 681 | - let _span = debug_span!("add_buffer", stream_id).entered(); | |
| 681 | + let _span = debug_span!("add_buffer", %stream_id).entered(); | |
| 682 | 682 | let mut inner = inner.borrow_mut(); |
| 683 | 683 | |
| 684 | 684 | let (size, alpha, modifier) = if let CastState::Ready { |
| @@ -758,7 +758,7 @@ impl PipeWire { | ||
| 758 | 758 | .remove_buffer({ |
| 759 | 759 | let inner = inner.clone(); |
| 760 | 760 | move |_stream, (), buffer| { |
| 761 | - trace!(stream_id, "remove_buffer"); | |
| 761 | + trace!(%stream_id, "remove_buffer"); | |
| 762 | 762 | let mut inner = inner.borrow_mut(); |
| 763 | 763 | |
| 764 | 764 | inner |
| @@ -779,7 +779,7 @@ impl PipeWire { | ||
| 779 | 779 | .unwrap(); |
| 780 | 780 | |
| 781 | 781 | trace!( |
| 782 | - stream_id, | |
| 782 | + %stream_id, | |
| 783 | 783 | "starting pw stream with size={pending_size:?}, refresh={refresh:?}" |
| 784 | 784 | ); |
| 785 | 785 |
src/utils/mod.rsmodified+46 -1
| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | use std::cmp::{max, min}; |
| 2 | -use std::f64; | |
| 3 | 2 | use std::ffi::{CString, OsStr}; |
| 3 | +use std::fmt::Display; | |
| 4 | 4 | use std::io::Write; |
| 5 | 5 | use std::os::unix::prelude::OsStrExt; |
| 6 | 6 | use std::path::{Path, PathBuf}; |
| 7 | 7 | use std::ptr::null_mut; |
| 8 | 8 | use std::sync::atomic::AtomicBool; |
| 9 | 9 | use std::time::Duration; |
| 10 | +use std::{f64, fmt}; | |
| 10 | 11 | |
| 11 | 12 | use anyhow::{ensure, Context}; |
| 12 | 13 | use bitflags::bitflags; |
| @@ -44,6 +45,50 @@ pub mod xwayland; | ||
| 44 | 45 | |
| 45 | 46 | pub static IS_SYSTEMD_SERVICE: AtomicBool = AtomicBool::new(false); |
| 46 | 47 | |
| 48 | +use id::IdCounter; | |
| 49 | + | |
| 50 | +/// Unique ID for a screencast session. | |
| 51 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | |
| 52 | +pub struct CastSessionId(u64); | |
| 53 | + | |
| 54 | +impl CastSessionId { | |
| 55 | + pub fn next() -> Self { | |
| 56 | + static COUNTER: IdCounter = IdCounter::new(); | |
| 57 | + Self(COUNTER.next()) | |
| 58 | + } | |
| 59 | + | |
| 60 | + pub fn get(self) -> u64 { | |
| 61 | + self.0 | |
| 62 | + } | |
| 63 | +} | |
| 64 | + | |
| 65 | +impl Display for CastSessionId { | |
| 66 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 67 | + write!(f, "{}", self.0) | |
| 68 | + } | |
| 69 | +} | |
| 70 | + | |
| 71 | +/// Unique ID for a screencast stream. | |
| 72 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | |
| 73 | +pub struct CastStreamId(u64); | |
| 74 | + | |
| 75 | +impl CastStreamId { | |
| 76 | + pub fn next() -> Self { | |
| 77 | + static COUNTER: IdCounter = IdCounter::new(); | |
| 78 | + Self(COUNTER.next()) | |
| 79 | + } | |
| 80 | + | |
| 81 | + pub fn get(self) -> u64 { | |
| 82 | + self.0 | |
| 83 | + } | |
| 84 | +} | |
| 85 | + | |
| 86 | +impl Display for CastStreamId { | |
| 87 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 88 | + write!(f, "{}", self.0) | |
| 89 | + } | |
| 90 | +} | |
| 91 | + | |
| 47 | 92 | bitflags! { |
| 48 | 93 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 49 | 94 | pub struct ResizeEdge: u32 { |