forge
Explore
Log in

Don't you dare go Hollow

screencopy: Add timeout to casts considered stopped

Browse files at this commit

b39edf405a5f81af0ee65eaeaa18001d0d60a060Ivan MolodetskikhParent: b98f490
Otherwise xdp-wlr never stops the cast after it first starts.
src/ipc/server.rsmodified+5 -0
@@ -857,6 +857,11 @@ impl State {
857 }857 }
858 858
859 // Check screencopy casts.859 // Check screencopy casts.
860+ //
861+ // First, clear expired casts. Ideally we'd have a deadline timer, but our 1 second frame
862+ // callback timer calls refresh regularly, so that's fine as is.
863+ self.niri.screencopy_state.clear_expired_casts();
864+
860 for queue in self.niri.screencopy_state.queues() {865 for queue in self.niri.screencopy_state.queues() {
861 if let Some(cast_info) = queue.cast() {866 if let Some(cast_info) = queue.cast() {
862 let stream_id = cast_info.stream_id.get();867 let stream_id = cast_info.stream_id.get();
src/protocols/screencopy.rsmodified+71 -9
@@ -27,6 +27,13 @@ use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId};
27 27
28 const VERSION: u32 = 3;28 const VERSION: u32 = 3;
29 29
30+/// Inactivity timeout for considering a screencopy cast as stopped.
31+///
32+/// xdg-desktop-portal-wlr keeps the screencopy manager alive across casts, so there's no way to
33+/// tell that a screencast had stopped. So we use a timeout: if no new with_damage frames are
34+/// requested for this timeout, consider the screencast finished.
35+const CAST_TIMEOUT: Duration = Duration::from_secs(10);
36+
30 pub struct ScreencopyQueue {37 pub struct ScreencopyQueue {
31 damage_tracker: OutputDamageTracker,38 damage_tracker: OutputDamageTracker,
32 /// Frames waiting for the client to call copy or destroy.39 /// Frames waiting for the client to call copy or destroy.
@@ -47,6 +54,8 @@ pub struct ScreencopyCast {
47 pub output: WeakOutput,54 pub output: WeakOutput,
48 /// Cached name of the output.55 /// Cached name of the output.
49 pub output_name: String,56 pub output_name: String,
57+ /// Deadline after which this cast is considered stopped if no new frames arrive.
58+ pub deadline: Duration,
50 }59 }
51 60
52 impl ScreencopyCast {61 impl ScreencopyCast {
@@ -56,9 +65,14 @@ impl ScreencopyCast {
56 stream_id: CastStreamId::next(),65 stream_id: CastStreamId::next(),
57 output: output.downgrade(),66 output: output.downgrade(),
58 output_name: output.name(),67 output_name: output.name(),
68+ deadline: get_monotonic_time() + CAST_TIMEOUT,
59 }69 }
60 }70 }
61 71
72+ fn update_deadline(&mut self) {
73+ self.deadline = get_monotonic_time() + CAST_TIMEOUT;
74+ }
75+
62 fn update_output(&mut self, output: &Output) {76 fn update_output(&mut self, output: &Output) {
63 // Only allocate a new name when the output differs.77 // Only allocate a new name when the output differs.
64 let weak = output.downgrade();78 let weak = output.downgrade();
@@ -126,14 +140,60 @@ impl ScreencopyQueue {
126 pub fn pop(&mut self) -> Screencopy {140 pub fn pop(&mut self) -> Screencopy {
127 let rv = self.screencopies.remove(0);141 let rv = self.screencopies.remove(0);
128 142
129- // Update cast output (most of the time we expect this to be the same).143+ let cast = self.cast.as_mut().unwrap();
130 if let Some(first) = self.screencopies.first() {144 if let Some(first) = self.screencopies.first() {
131- let cast = self.cast.as_mut().unwrap();145+ // Update cast output (most of the time we expect this to be the same).
132 cast.update_output(first.output());146 cast.update_output(first.output());
147+ } else {
148+ // Queue became empty, update deadline for considering the cast stopped.
149+ cast.update_deadline();
133 }150 }
134 151
135 rv152 rv
136 }153 }
154+
155+ pub fn clear_expired_cast(&mut self) {
156+ if let Some(cast) = &self.cast {
157+ // Check deadline if there are no in-flight frames.
158+ if self.screencopies.is_empty() && cast.deadline <= get_monotonic_time() {
159+ self.cast = None;
160+ }
161+ }
162+ }
163+
164+ fn remove_output(&mut self, output: &Output) {
165+ if self.screencopies.is_empty() {
166+ return;
167+ }
168+
169+ self.screencopies
170+ .retain(|screencopy| screencopy.output() != output);
171+
172+ if let Some(cast) = &mut self.cast {
173+ if self.screencopies.is_empty() {
174+ // Queue became empty, update deadline for considering the cast stopped.
175+ cast.update_deadline();
176+ }
177+ }
178+ }
179+
180+ fn remove_frame(&mut self, frame: &ZwlrScreencopyFrameV1) {
181+ self.pending_frames.remove(frame);
182+
183+ if self.screencopies.is_empty() {
184+ return;
185+ }
186+
187+ self.screencopies
188+ .retain(|screencopy| screencopy.frame != *frame);
189+
190+ if let Some(cast) = &mut self.cast {
191+ if self.screencopies.is_empty() {
192+ // Queue became empty, update deadline for considering the cast stopped.
193+ cast.update_deadline();
194+ }
195+ }
196+ }
137 }197 }
138 198
139 #[derive(Default)]199 #[derive(Default)]
@@ -186,9 +246,7 @@ impl ScreencopyManagerState {
186 246
187 pub fn remove_output(&mut self, output: &Output) {247 pub fn remove_output(&mut self, output: &Output) {
188 for queue in self.queues.values_mut() {248 for queue in self.queues.values_mut() {
189- queue249+ queue.remove_output(output);
190- .screencopies
191- .retain(|screencopy| screencopy.output() != output);
192 }250 }
193 251
194 self.cleanup_queues();252 self.cleanup_queues();
@@ -210,6 +268,12 @@ impl ScreencopyManagerState {
210 self.queues268 self.queues
211 .retain(|manager, queue| manager.is_alive() || !queue.is_empty());269 .retain(|manager, queue| manager.is_alive() || !queue.is_empty());
212 }270 }
271+
272+ pub fn clear_expired_casts(&mut self) {
273+ for queue in self.queues.values_mut() {
274+ queue.clear_expired_cast();
275+ }
276+ }
213 }277 }
214 278
215 impl<D> GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData, D>279 impl<D> GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData, D>
@@ -550,10 +614,8 @@ where
550 return;614 return;
551 };615 };
552 616
553- queue.pending_frames.remove(frame);617+ queue.remove_frame(frame);
554- queue618+
555- .screencopies
556- .retain(|screencopy| screencopy.frame != *frame);
557 // Clean up the queue if this was the last object.619 // Clean up the queue if this was the last object.
558 if queue.is_empty() && !manager.is_alive() {620 if queue.is_empty() && !manager.is_alive() {
559 state.queues.remove(manager);621 state.queues.remove(manager);