Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): keep each pooled reader's last vended frame, and retry from a fresh seek when it is gone

Richie McIlroyb710ca9034d7
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index 6decb4b7c..f8acb4d56 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -547,6 +547,7 @@ impl AVAssetReaderDecoder {
requested_frame: u32,
requested_time: f32,
is_scrubbing: bool,
+ must_reset: bool,
) -> (usize, bool) {
let decoder_count = self.decoders.len();
let (best_id, _distance, needs_reset) = if is_scrubbing {
@@ -567,11 +568,11 @@ impl AVAssetReaderDecoder {
// It can never vend that frame again, and reading on would answer
// with a stale or later frame, so it has to seek back first.
let needs_reset = needs_reset
+ || must_reset
|| self
.decoders
.get(decoder_idx)
- .and_then(|decoder| decoder.prev_vended)
- .is_some_and(|prev| prev >= requested_frame);
+ .is_some_and(|decoder| reader_passed_request(decoder.prev_vended, requested_frame));
let max_decoder_count = self
.pool_manager
@@ -722,6 +723,7 @@ impl AVAssetReaderDecoder {
frame: u32,
max_fallback_distance: u32,
sender: oneshot::Sender<DecodedFrame>,
+ retried: bool,
}
let mut deferred_requests = VecDeque::<PendingRequest>::new();
@@ -779,6 +781,7 @@ impl AVAssetReaderDecoder {
frame,
max_fallback_distance,
sender,
+ retried: false,
});
}
}
@@ -797,6 +800,7 @@ impl AVAssetReaderDecoder {
frame,
max_fallback_distance,
sender,
+ retried: false,
});
}
}
@@ -918,7 +922,12 @@ impl AVAssetReaderDecoder {
}
let this = this.as_mut().expect("Video reader initialized above");
- let (decoder_idx, was_reset) = this.select_best_decoder(requested_frame, requested_time, is_scrubbing);
+ let (decoder_idx, was_reset) = this.select_best_decoder(
+ requested_frame,
+ requested_time,
+ is_scrubbing,
+ pending_requests.iter().any(|request| request.retried),
+ );
let cache_min = if was_reset {
min_requested_frame.saturating_sub(FRAME_CACHE_SIZE as u32 * 2)
@@ -931,9 +940,23 @@ impl AVAssetReaderDecoder {
max_requested_frame + FRAME_CACHE_SIZE as u32
};
+ // The last frame each other pooled reader vended is the answer for a
+ // request that lands in the gap after it, and the reader can never
+ // vend it again; keep it cached while requests alternate between
+ // readers.
+ let pinned_frames: Vec<u32> = this
+ .decoders
+ .iter()
+ .enumerate()
+ .filter(|&(index, _)| index != decoder_idx)
+ .filter_map(|(_, decoder)| decoder.prev_vended)
+ .collect();
+
if was_reset {
*last_sent_frame.borrow_mut() = None;
- cache.retain(|&f, _| f >= cache_min && f <= cache_max);
+ cache.retain(|f, _| {
+ (*f >= cache_min && *f <= cache_max) || pinned_frames.contains(f)
+ });
cache_bytes = cache
.values()
.map(CachedFrame::byte_len)
@@ -951,6 +974,7 @@ impl AVAssetReaderDecoder {
// reader re-vends from the keyframe at-or-before the request, and
// that frame is the true VFR hold for requests inside a pts hole.
let mut hold_candidate: Option<(u32, R<cv::ImageBuf>)> = None;
+ let mut retry_requests = Vec::new();
{
let decoder = &mut this.decoders[decoder_idx];
@@ -978,7 +1002,8 @@ impl AVAssetReaderDecoder {
let current_frame =
pts_to_frame(frame.pts().value, Rational::new(1, frame.pts().scale), fps);
- if let Some(prev) = decoder.prev_vended
+ let vended_before = decoder.prev_vended;
+ if let Some(prev) = vended_before
&& current_frame > prev + 1
{
record_pts_hole(&mut pts_holes, prev, current_frame);
@@ -1030,16 +1055,18 @@ impl AVAssetReaderDecoder {
)
{
if let Some(last_active) = &last_active_frame {
- let frame_to_remove = if requested_frame > *last_active {
- *cache.keys().next().unwrap()
+ let from_oldest = if requested_frame > *last_active {
+ true
} else if requested_frame < *last_active {
- *cache.keys().next_back().unwrap()
+ false
} else {
- let min = *cache.keys().min().unwrap();
- let max = *cache.keys().max().unwrap();
- if current_frame > max { min } else { max }
+ current_frame > *cache.keys().next_back().unwrap()
};
- if let Some(removed) = cache.remove(&frame_to_remove) {
+ let victim = cache_eviction_victim(&cache, from_oldest, |frame| {
+ Some(frame) == vended_before || pinned_frames.contains(&frame)
+ });
+ if let Some(removed) = victim.and_then(|frame| cache.remove(&frame))
+ {
cache_bytes = cache_bytes.saturating_sub(removed.byte_len());
}
} else {
@@ -1093,6 +1120,26 @@ impl AVAssetReaderDecoder {
.flatten()
.max_by_key(|d| d.number);
+ // The reader vends every sample in pts order, so the
+ // frame it vended just before this one is the newest
+ // at or before the request. When that frame is no
+ // longer held anywhere, any other answer is stale or
+ // from the future: retry once from a fresh seek, which
+ // re-vends and keeps it.
+ if !req.retried
+ && answer_misses_previous_vend(
+ vended_before,
+ req.frame,
+ best_before.as_ref().map(|d| d.number),
+ )
+ {
+ retry_requests.push(PendingRequest {
+ retried: true,
+ ..req
+ });
+ continue;
+ }
+
if let Some(data) = best_before {
gap_hold = Some(data.clone());
*last_sent_frame.borrow_mut() = Some(data.clone());
@@ -1133,6 +1180,10 @@ impl AVAssetReaderDecoder {
decoder.is_done = true;
}
+ for request in retry_requests.into_iter().rev() {
+ deferred_requests.push_front(request);
+ }
+
let last_frame_in_cache = cache.keys().max().copied().unwrap_or(0);
if frames_iterated > 0 {
this.decoders[decoder_idx]
@@ -1306,6 +1357,45 @@ impl AVAssetReaderDecoder {
}
}
+fn reader_passed_request(prev_vended: Option<u32>, requested_frame: u32) -> bool {
+ prev_vended.is_some_and(|prev| prev >= requested_frame)
+}
+
+// A request the reader steps over is answered by the frame it vended just
+// before, the newest one at or before the request. Any other answer is stale.
+fn answer_misses_previous_vend(
+ vended_before: Option<u32>,
+ requested_frame: u32,
+ answer: Option<u32>,
+) -> bool {
+ vended_before
+ .filter(|&number| number <= requested_frame)
+ .is_some_and(|expected| answer != Some(expected))
+}
+
+// Drops from the end the cache is trimmed from, skipping pinned frames unless
+// every frame is pinned.
+fn cache_eviction_victim<V>(
+ cache: &BTreeMap<u32, V>,
+ from_oldest: bool,
+ is_pinned: impl Fn(u32) -> bool,
+) -> Option<u32> {
+ if from_oldest {
+ cache
+ .keys()
+ .copied()
+ .find(|&frame| !is_pinned(frame))
+ .or_else(|| cache.keys().next().copied())
+ } else {
+ cache
+ .keys()
+ .rev()
+ .copied()
+ .find(|&frame| !is_pinned(frame))
+ .or_else(|| cache.keys().next_back().copied())
+ }
+}
+
fn should_grow_decoder_pool(
is_scrubbing: bool,
needs_reset: bool,
@@ -1385,6 +1475,44 @@ mod tests {
));
}
+ #[test]
+ fn a_reader_that_vended_the_request_cannot_serve_it_again() {
+ assert!(!reader_passed_request(None, 10));
+ assert!(!reader_passed_request(Some(9), 10));
+ assert!(reader_passed_request(Some(10), 10));
+ assert!(reader_passed_request(Some(57), 54));
+ }
+
+ #[test]
+ fn only_the_frame_vended_before_a_gap_answers_requests_inside_it() {
+ assert!(!answer_misses_previous_vend(Some(1054), 1055, Some(1054)));
+ assert!(answer_misses_previous_vend(Some(1054), 1055, Some(1052)));
+ assert!(answer_misses_previous_vend(Some(751), 752, Some(421)));
+ assert!(answer_misses_previous_vend(Some(751), 752, None));
+ assert!(!answer_misses_previous_vend(None, 752, Some(421)));
+ assert!(!answer_misses_previous_vend(Some(760), 752, Some(751)));
+ }
+
+ #[test]
+ fn cache_eviction_skips_pinned_frames() {
+ let cache: BTreeMap<u32, ()> = [415, 419, 421, 749, 751]
+ .into_iter()
+ .map(|f| (f, ()))
+ .collect();
+ assert_eq!(cache_eviction_victim(&cache, true, |_| false), Some(415));
+ assert_eq!(cache_eviction_victim(&cache, false, |_| false), Some(751));
+ assert_eq!(
+ cache_eviction_victim(&cache, false, |f| f == 751),
+ Some(749)
+ );
+ assert_eq!(cache_eviction_victim(&cache, true, |f| f < 420), Some(421));
+ assert_eq!(cache_eviction_victim(&cache, true, |_| true), Some(415));
+ assert_eq!(
+ cache_eviction_victim(&BTreeMap::<u32, ()>::new(), true, |_| false),
+ None
+ );
+ }
+
#[test]
fn decoder_pool_grows_only_for_unserved_non_sequential_access() {
assert!(should_grow_decoder_pool(true, true, 1, 5));