Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): read again from a fresh AVAssetReader when one fails instead of answering with a stale frame

Richie McIlroy3f26bc8f2539
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index fff82508e..a6077345d 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -1218,6 +1218,32 @@ impl AVAssetReaderDecoder {
}
decoder.is_done = true;
+
+ // A failed reader (a decode error, seen with many decode
+ // sessions on a loaded machine) vends nothing more, and the
+ // newest cached frame is stale for every request still
+ // waiting. Read them again from a fresh reader; when that one
+ // fails too, leave them unanswered so the handle switches to
+ // software decoding instead of showing an older frame.
+ if !pending_requests.is_empty()
+ && let Some(error) = decoder.inner.failure()
+ {
+ tracing::warn!(
+ decoder_idx,
+ requested_frame,
+ %error,
+ "Video reader failed; reading the requests again from a fresh reader"
+ );
+ retry_requests.extend(
+ pending_requests
+ .drain(..)
+ .filter(|request| !request.retried)
+ .map(|request| PendingRequest {
+ retried: true,
+ ..request
+ }),
+ );
+ }
}
for request in retry_requests.into_iter().rev() {
diff --git a/crates/rendering/src/decoder/model/mod.rs b/crates/rendering/src/decoder/model/mod.rs
index 6e97159a7..7dafcf6d6 100644
--- a/crates/rendering/src/decoder/model/mod.rs
+++ b/crates/rendering/src/decoder/model/mod.rs
@@ -1095,6 +1095,63 @@ fn avassetreader_answers_match_the_reference_model() {
run_suite(Decoder::Avf);
}
+/// A native reader can fail mid-stream with a decode error and vend nothing
+/// more. Every later answer must still be the frame on screen: read from a
+/// fresh reader, or from software decoding when that one fails too.
+#[cfg(target_os = "macos")]
+#[test]
+fn avassetreader_answers_stay_exact_when_its_reader_fails() {
+ let script = Script {
+ time_base: (1, 30),
+ frames: (0..120)
+ .map(|pts| scripted::ScriptFrame {
+ pts,
+ keyframe: pts % 30 == 0,
+ })
+ .collect(),
+ last_duration: 1,
+ };
+ let runtime = runtime();
+ let patterns: [(&str, Vec<u32>, bool); 2] = [
+ ("playback", (0..90).collect(), false),
+ ("scrub", (0..112).step_by(7).collect(), true),
+ ];
+ for failed_reads in [1, 2] {
+ for (pattern, frames, initial) in &patterns {
+ let registration = scripted::register(script.clone());
+ for _ in 0..failed_reads {
+ registration.fail_avf_read_at(40);
+ }
+ let path = registration.path().to_path_buf();
+ runtime.block_on(async {
+ let handle = spawn_decoder("model", path, 30, 0.0, false)
+ .await
+ .unwrap()
+ .with_max_fallback_distance(2);
+ for &frame in frames {
+ let time = frame as f32 / 30.0;
+ let decoded = if *initial {
+ handle.get_frame_initial(time).await
+ } else {
+ handle.get_frame(time).await
+ };
+ assert_eq!(
+ decoded.as_ref().and_then(frame_identity),
+ Some(frame),
+ "{pattern} with {failed_reads} failed reads at sample 40: request {frame}"
+ );
+ }
+ assert_eq!(
+ handle.decoder_type() == DecoderType::AVAssetReader,
+ failed_reads == 1,
+ "{pattern} with {failed_reads} failed reads ended on {}",
+ handle.decoder_type()
+ );
+ });
+ }
+ }
+}
+
#[test]
fn ffmpeg_answers_match_the_reference_model() {
run_suite(Decoder::Ffmpeg);
diff --git a/crates/video-decode/src/avassetreader.rs b/crates/video-decode/src/avassetreader.rs
index c1dc1f7ef..b148929d4 100644
--- a/crates/video-decode/src/avassetreader.rs
+++ b/crates/video-decode/src/avassetreader.rs
@@ -664,6 +664,25 @@ impl AVAssetReaderDecoder {
}
}
+ /// Why the reader stopped vending, when it failed instead of reaching the
+ /// end of its range. A failed reader never vends again.
+ pub fn failure(&self) -> Option<String> {
+ match &self.source {
+ ReaderSource::Native { reader, .. } => (reader.status()
+ == av::AssetReaderStatus::Failed)
+ .then(|| {
+ reader.error().map_or_else(
+ || "unknown error".to_string(),
+ |error| format!("{} ({})", error.localized_description(), error.code()),
+ )
+ }),
+ #[cfg(feature = "scripted")]
+ ReaderSource::Scripted(reader) => reader
+ .failed()
+ .then(|| "scripted decode failure".to_string()),
+ }
+ }
+
pub fn width(&self) -> u32 {
self.width
}
diff --git a/crates/video-decode/src/scripted.rs b/crates/video-decode/src/scripted.rs
index a44e2a929..2e48c1bbe 100644
--- a/crates/video-decode/src/scripted.rs
+++ b/crates/video-decode/src/scripted.rs
@@ -13,7 +13,9 @@
//! - AVAssetReader: a reader started at time `T` vends the sample on screen at
//! `T` (the newest at or before it) and every later one. When that sample
//! starts before `T` its presentation time is trimmed to `T`. A reader started
-//! at or after the end of the track vends nothing.
+//! at or after the end of the track vends nothing. A read set up with
+//! [`Registration::fail_avf_read_at`] fails like a decode error: the reader
+//! vends nothing more and reports the failure.
//!
//! Every vended frame carries its sample index in its first luma row, see
//! [`frame_id`].
@@ -96,6 +98,7 @@ pub(crate) struct ScriptState {
pub(crate) script: Script,
counters: Counters,
vended: Mutex<Vec<u32>>,
+ failing_samples: Mutex<Vec<usize>>,
#[cfg(target_os = "macos")]
samples: Mutex<HashMap<usize, avf::SharedSample>>,
}
@@ -105,6 +108,16 @@ impl ScriptState {
self.vended.lock().unwrap().push(index as u32);
}
+ #[cfg(target_os = "macos")]
+ fn take_failure(&self, index: usize) -> bool {
+ let mut failing = self.failing_samples.lock().unwrap();
+ let Some(position) = failing.iter().position(|&sample| sample == index) else {
+ return false;
+ };
+ failing.swap_remove(position);
+ true
+ }
+
/// Whether `pts` (in stream ticks) is at or before `value / scale` seconds.
#[cfg(target_os = "macos")]
fn at_or_before(&self, pts: i64, value: i64, scale: i32) -> bool {
@@ -165,6 +178,13 @@ impl Registration {
pub fn seeks(&self) -> u64 {
self.state.counters.seeks.load(Ordering::Relaxed)
}
+
+ /// The next AVAssetReader that reaches `index` fails there instead of
+ /// vending it, as a native reader does with a decode error: it vends
+ /// nothing more and reports the failure. Once per call.
+ pub fn fail_avf_read_at(&self, index: usize) {
+ self.state.failing_samples.lock().unwrap().push(index);
+ }
}
impl Drop for Registration {
@@ -185,6 +205,7 @@ pub fn register(script: Script) -> Registration {
script,
counters: Counters::default(),
vended: Mutex::new(Vec::new()),
+ failing_samples: Mutex::new(Vec::new()),
#[cfg(target_os = "macos")]
samples: Mutex::new(HashMap::new()),
});
@@ -314,6 +335,7 @@ mod avf {
state: Arc<ScriptState>,
next: usize,
trimmed_start: Option<cm::Time>,
+ failed: bool,
}
impl AvfReader {
@@ -339,12 +361,24 @@ mod avf {
state,
next,
trimmed_start,
+ failed: false,
}
}
+ pub(crate) fn failed(&self) -> bool {
+ self.failed
+ }
+
pub(crate) fn next_sample_buf(&mut self) -> Option<R<cm::SampleBuf>> {
+ if self.failed {
+ return None;
+ }
let script = &self.state.script;
let frame = script.frames.get(self.next)?;
+ if self.state.take_failure(self.next) {
+ self.failed = true;
+ return None;
+ }
let index = self.next;
self.next += 1;
self.state.record_vend(index);