pw_utils: Preserve the screencast frame cadence
Browse files at this commit4fa7fdfc216b0a67fbe210394bae42e396623210Parent: d5bc4842 changed files +25 -5
src/screencasting/mod.rsmodified+4 -4
| @@ -154,7 +154,7 @@ impl State { | |||
| 154 | CastTarget::Nothing => { | 154 | CastTarget::Nothing => { |
| 155 | self.backend.with_primary_renderer(|renderer| { | 155 | self.backend.with_primary_renderer(|renderer| { |
| 156 | if cast.dequeue_buffer_and_clear(renderer) { | 156 | if cast.dequeue_buffer_and_clear(renderer) { |
| 157 | - cast.last_frame_time = get_monotonic_time(); | 157 | + cast.record_frame_time(get_monotonic_time()); |
| 158 | } | 158 | } |
| 159 | }); | 159 | }); |
| 160 | return; | 160 | return; |
| @@ -243,7 +243,7 @@ impl State { | |||
| 243 | bbox.size, | 243 | bbox.size, |
| 244 | scale, | 244 | scale, |
| 245 | ) { | 245 | ) { |
| 246 | - cast.last_frame_time = get_monotonic_time(); | 246 | + cast.record_frame_time(get_monotonic_time()); |
| 247 | } | 247 | } |
| 248 | }); | 248 | }); |
| 249 | 249 | ||
| @@ -611,7 +611,7 @@ impl Niri { | |||
| 611 | let cursor_data = cursor_data.as_ref().unwrap(); | 611 | let cursor_data = cursor_data.as_ref().unwrap(); |
| 612 | 612 | ||
| 613 | if cast.dequeue_buffer_and_render(renderer, &elements, cursor_data, size, scale) { | 613 | if cast.dequeue_buffer_and_render(renderer, &elements, cursor_data, size, scale) { |
| 614 | - cast.last_frame_time = target_presentation_time; | 614 | + cast.record_frame_time(target_presentation_time); |
| 615 | } | 615 | } |
| 616 | } | 616 | } |
| 617 | self.casting.casts = casts; | 617 | self.casting.casts = casts; |
| @@ -696,7 +696,7 @@ impl Niri { | |||
| 696 | let cursor_data = CursorData::compute(&elements, main_start, pointer_location, scale); | 696 | let cursor_data = CursorData::compute(&elements, main_start, pointer_location, scale); |
| 697 | 697 | ||
| 698 | if cast.dequeue_buffer_and_render(renderer, &elements, &cursor_data, bbox.size, scale) { | 698 | if cast.dequeue_buffer_and_render(renderer, &elements, &cursor_data, bbox.size, scale) { |
| 699 | - cast.last_frame_time = target_presentation_time; | 699 | + cast.record_frame_time(target_presentation_time); |
| 700 | } | 700 | } |
| 701 | } | 701 | } |
| 702 | self.casting.casts = casts; | 702 | self.casting.casts = casts; |
src/screencasting/pw_utils.rsmodified+21 -1
| @@ -106,7 +106,9 @@ pub struct Cast { | |||
| 106 | formats: FormatSet, | 106 | formats: FormatSet, |
| 107 | offer_alpha: bool, | 107 | offer_alpha: bool, |
| 108 | cursor_mode: CursorMode, | 108 | cursor_mode: CursorMode, |
| 109 | - pub last_frame_time: Duration, | 109 | + // The last slot in the capture cadence, rather than the time a late frame was rendered. |
| 110 | + last_frame_time: Duration, | ||
| 111 | + last_frame_interval: Duration, | ||
| 110 | scheduled_redraw: Option<RegistrationToken>, | 112 | scheduled_redraw: Option<RegistrationToken>, |
| 111 | // Incremented once per successful frame, stored in buffer meta. | 113 | // Incremented once per successful frame, stored in buffer meta. |
| 112 | sequence_counter: u64, | 114 | sequence_counter: u64, |
| @@ -946,6 +948,7 @@ impl PipeWire { | |||
| 946 | offer_alpha: alpha, | 948 | offer_alpha: alpha, |
| 947 | cursor_mode, | 949 | cursor_mode, |
| 948 | last_frame_time: Duration::ZERO, | 950 | last_frame_time: Duration::ZERO, |
| 951 | + last_frame_interval: Duration::ZERO, | ||
| 949 | scheduled_redraw: None, | 952 | scheduled_redraw: None, |
| 950 | sequence_counter: 0, | 953 | sequence_counter: 0, |
| 951 | inner, | 954 | inner, |
| @@ -1019,6 +1022,23 @@ impl Cast { | |||
| 1019 | Ok(()) | 1022 | Ok(()) |
| 1020 | } | 1023 | } |
| 1021 | 1024 | ||
| 1025 | + pub fn record_frame_time(&mut self, time: Duration) { | ||
| 1026 | + let interval = self.inner.borrow().min_time_between_frames; | ||
| 1027 | + let next = self.last_frame_time + interval; | ||
| 1028 | + | ||
| 1029 | + // Absorb small scheduling delays without moving every subsequent frame back. | ||
| 1030 | + // After a missed interval or a rate change, restart instead of catching up in a burst. | ||
| 1031 | + self.last_frame_time = if self.last_frame_interval == interval | ||
| 1032 | + && time >= self.last_frame_time | ||
| 1033 | + && time < next + interval | ||
| 1034 | + { | ||
| 1035 | + next | ||
| 1036 | + } else { | ||
| 1037 | + time | ||
| 1038 | + }; | ||
| 1039 | + self.last_frame_interval = interval; | ||
| 1040 | + } | ||
| 1041 | + | ||
| 1022 | fn compute_extra_delay(&self, target_frame_time: Duration) -> Duration { | 1042 | fn compute_extra_delay(&self, target_frame_time: Duration) -> Duration { |
| 1023 | let inner = self.inner.borrow(); | 1043 | let inner = self.inner.borrow(); |
| 1024 | 1044 | ||