Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): answer requests before a track's first frame with that frame

Richie McIlroy990d1d35280e
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index f8acb4d56..730d7fb37 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -544,10 +544,9 @@ impl AVAssetReaderDecoder {
fn select_best_decoder(
&mut self,
- requested_frame: u32,
requested_time: f32,
is_scrubbing: bool,
- must_reset: bool,
+ must_reset: impl Fn(&DecoderInstance) -> bool,
) -> (usize, bool) {
let decoder_count = self.decoders.len();
let (best_id, _distance, needs_reset) = if is_scrubbing {
@@ -563,16 +562,7 @@ impl AVAssetReaderDecoder {
};
let decoder_idx = best_id.min(decoder_count.saturating_sub(1));
- // Pool positions are capped to the newest request, so a reader that
- // decoded ahead can look usable for a request it has already passed.
- // 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)
- .is_some_and(|decoder| reader_passed_request(decoder.prev_vended, requested_frame));
+ let needs_reset = needs_reset || self.decoders.get(decoder_idx).is_some_and(must_reset);
let max_decoder_count = self
.pool_manager
@@ -689,7 +679,7 @@ impl AVAssetReaderDecoder {
}
};
let mut this = None::<Self>;
- let mut keyframe_index = None;
+ let mut keyframe_index: Option<Arc<cap_video_decode::avassetreader::KeyframeIndex>> = None;
let init_result = DecoderInitResult {
width,
@@ -883,6 +873,28 @@ impl AVAssetReaderDecoder {
}
pending_requests = still_unfulfilled;
+ // A track can start after the recording does. Before its first
+ // frame, that frame is the answer; serving it from the cache keeps
+ // the reader from stepping ahead on every such request.
+ let stream_start_frame = keyframe_index
+ .as_ref()
+ .and_then(|index| index.keyframes().first())
+ .map(|&(_, secs)| (secs * f64::from(fps)).round() as u32);
+ if let Some(start) = stream_start_frame
+ && let Some(first) = cache.get(&start).map(|cached| cached.data().clone())
+ {
+ let mut after_start = Vec::with_capacity(pending_requests.len());
+ for req in pending_requests.drain(..) {
+ if req.frame < start {
+ *last_sent_frame.borrow_mut() = Some(first.clone());
+ let _ = req.sender.send(first.to_decoded_frame());
+ } else {
+ after_start.push(req);
+ }
+ }
+ pending_requests = after_start;
+ }
+
if pending_requests.is_empty() {
continue;
}
@@ -922,12 +934,20 @@ 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,
- pending_requests.iter().any(|request| request.retried),
- );
+ // Pool positions are capped to the newest request, so a reader that
+ // decoded ahead can look usable for a request it has already passed.
+ // 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. Before
+ // the stream's first frame nothing better exists, so a seek is wasted.
+ let retrying = pending_requests.iter().any(|request| request.retried);
+ let before_stream_start =
+ stream_start_frame.is_some_and(|start| requested_frame < start);
+ let (decoder_idx, was_reset) =
+ this.select_best_decoder(requested_time, is_scrubbing, |decoder| {
+ retrying
+ || (!before_stream_start
+ && reader_passed_request(decoder.prev_vended, requested_frame))
+ });
let cache_min = if was_reset {
min_requested_frame.saturating_sub(FRAME_CACHE_SIZE as u32 * 2)