diff --git a/src/screencasting/mod.rs b/src/screencasting/mod.rs index 8f8a54cd..232b584b 100644 --- a/src/screencasting/mod.rs +++ b/src/screencasting/mod.rs @@ -154,7 +154,7 @@ impl State { CastTarget::Nothing => { self.backend.with_primary_renderer(|renderer| { if cast.dequeue_buffer_and_clear(renderer) { - cast.last_frame_time = get_monotonic_time(); + cast.record_frame_time(get_monotonic_time()); } }); return; @@ -243,7 +243,7 @@ impl State { bbox.size, scale, ) { - cast.last_frame_time = get_monotonic_time(); + cast.record_frame_time(get_monotonic_time()); } }); @@ -611,7 +611,7 @@ impl Niri { let cursor_data = cursor_data.as_ref().unwrap(); if cast.dequeue_buffer_and_render(renderer, &elements, cursor_data, size, scale) { - cast.last_frame_time = target_presentation_time; + cast.record_frame_time(target_presentation_time); } } self.casting.casts = casts; @@ -696,7 +696,7 @@ impl Niri { let cursor_data = CursorData::compute(&elements, main_start, pointer_location, scale); if cast.dequeue_buffer_and_render(renderer, &elements, &cursor_data, bbox.size, scale) { - cast.last_frame_time = target_presentation_time; + cast.record_frame_time(target_presentation_time); } } self.casting.casts = casts; diff --git a/src/screencasting/pw_utils.rs b/src/screencasting/pw_utils.rs index 6f1d3592..b0b1d5bd 100644 --- a/src/screencasting/pw_utils.rs +++ b/src/screencasting/pw_utils.rs @@ -106,7 +106,9 @@ pub struct Cast { formats: FormatSet, offer_alpha: bool, cursor_mode: CursorMode, - pub last_frame_time: Duration, + // The last slot in the capture cadence, rather than the time a late frame was rendered. + last_frame_time: Duration, + last_frame_interval: Duration, scheduled_redraw: Option, // Incremented once per successful frame, stored in buffer meta. sequence_counter: u64, @@ -946,6 +948,7 @@ impl PipeWire { offer_alpha: alpha, cursor_mode, last_frame_time: Duration::ZERO, + last_frame_interval: Duration::ZERO, scheduled_redraw: None, sequence_counter: 0, inner, @@ -1019,6 +1022,23 @@ impl Cast { Ok(()) } + pub fn record_frame_time(&mut self, time: Duration) { + let interval = self.inner.borrow().min_time_between_frames; + let next = self.last_frame_time + interval; + + // Absorb small scheduling delays without moving every subsequent frame back. + // After a missed interval or a rate change, restart instead of catching up in a burst. + self.last_frame_time = if self.last_frame_interval == interval + && time >= self.last_frame_time + && time < next + interval + { + next + } else { + time + }; + self.last_frame_interval = interval; + } + fn compute_extra_delay(&self, target_frame_time: Duration) -> Duration { let inner = self.inner.borrow();