Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): reset a reused decoder instead of answering with a frame from an older seek

Richie McIlroyb23754a6fa11
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index 730d7fb37..c46b6368a 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -385,6 +385,8 @@ struct DecoderInstance {
/// Lives on the instance because the reader vends in pts order between
/// resets, but request batches may be served by different pool decoders.
prev_vended: Option<u32>,
+ /// First frame of the uninterrupted vend run that ends at `prev_vended`.
+ run_start: Option<u32>,
_permit: OwnedSemaphorePermit,
}
@@ -410,6 +412,7 @@ impl DecoderInstance {
tokio_handle,
keyframe_index,
prev_vended: None,
+ run_start: None,
_permit: permit,
})
}
@@ -421,6 +424,7 @@ impl DecoderInstance {
self.frames_iter_valid = true;
self.health.reset_counters();
self.prev_vended = None;
+ self.run_start = None;
}
Err(e) => {
tracing::error!(
@@ -453,12 +457,21 @@ impl DecoderInstance {
self.frames_iter_valid = true;
self.health = DecoderHealth::new();
self.prev_vended = None;
+ self.run_start = None;
Ok(())
}
fn current_position(&self) -> f32 {
self.inner.current_position_secs()
}
+
+ fn would_serve_stale_frame(&self, frame: u32, cache: &BTreeMap<u32, CachedFrame>) -> bool {
+ reuse_would_serve_stale_frame(
+ self.run_start,
+ self.prev_vended,
+ cache.range(..=frame).next_back().map(|(&cached, _)| cached),
+ )
+ }
}
pub struct AVAssetReaderDecoder {
@@ -945,6 +958,7 @@ impl AVAssetReaderDecoder {
let (decoder_idx, was_reset) =
this.select_best_decoder(requested_time, is_scrubbing, |decoder| {
retrying
+ || decoder.would_serve_stale_frame(requested_frame, &cache)
|| (!before_stream_start
&& reader_passed_request(decoder.prev_vended, requested_frame))
});
@@ -1028,6 +1042,9 @@ impl AVAssetReaderDecoder {
{
record_pts_hole(&mut pts_holes, prev, current_frame);
}
+ if decoder.prev_vended.is_none() {
+ decoder.run_start = Some(current_frame);
+ }
decoder.prev_vended = Some(current_frame);
let position_secs = current_frame as f32 / fps as f32;
@@ -1416,6 +1433,24 @@ fn cache_eviction_victim<V>(
}
}
+// Requests the reader has already passed, or that fall in a gap before its
+// next frame, are answered with the newest cached frame at or before them.
+// That frame is only trustworthy when this reader vended it in its current
+// run; an older one comes from an unrelated seek and shows stale content.
+// With nothing cached at or before the request, whether the reader has
+// passed it or a gap follows its evicted last frame, the only answer left is
+// a later frame instead of the one on screen.
+fn reuse_would_serve_stale_frame(
+ run_start: Option<u32>,
+ prev_vended: Option<u32>,
+ newest_cached_at_or_before: Option<u32>,
+) -> bool {
+ let (Some(run_start), Some(_)) = (run_start, prev_vended) else {
+ return false;
+ };
+ newest_cached_at_or_before.is_none_or(|cached| cached < run_start)
+}
+
fn should_grow_decoder_pool(
is_scrubbing: bool,
needs_reset: bool,
@@ -1533,6 +1568,31 @@ mod tests {
);
}
+ #[test]
+ fn reused_readers_never_answer_with_frames_from_an_unrelated_seek() {
+ assert!(!reuse_would_serve_stale_frame(None, None, Some(10)));
+ assert!(!reuse_would_serve_stale_frame(Some(40), Some(55), Some(48)));
+ assert!(!reuse_would_serve_stale_frame(Some(40), Some(55), Some(40)));
+ assert!(reuse_would_serve_stale_frame(Some(40), Some(55), Some(39)));
+ assert!(reuse_would_serve_stale_frame(Some(40), Some(55), Some(10)));
+ assert!(reuse_would_serve_stale_frame(Some(40), Some(48), None));
+ }
+
+ #[test]
+ fn returning_to_a_passed_hold_after_its_frame_is_evicted_resets_the_reader() {
+ let (run_start, held, vended) = (40, 48, 55);
+ assert!(!reuse_would_serve_stale_frame(
+ Some(run_start),
+ Some(vended),
+ Some(held)
+ ));
+ assert!(reuse_would_serve_stale_frame(
+ Some(run_start),
+ Some(vended),
+ None
+ ));
+ }
+
#[test]
fn decoder_pool_grows_only_for_unserved_non_sequential_access() {
assert!(should_grow_decoder_pool(true, true, 1, 5));