Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

monitors: show the unreadable slate for persistently failing frames

FredApps5ed23c942464
diff --git a/crates/engine/src/media_pool.rs b/crates/engine/src/media_pool.rs
index ac849710c..b14ea959a 100644
--- a/crates/engine/src/media_pool.rs
+++ b/crates/engine/src/media_pool.rs
@@ -38,6 +38,8 @@ pub struct MediaPool {
proxies: RwLock<HashMap<ItemId, (String, SharedSource)>>,
offline: Arc<RwLock<HashMap<ItemId, OfflineStatus>>>,
use_proxies: AtomicBool,
+ /// Per item: video frames that failed in a row in the monitors (see [`Self::monitor_source`]).
+ failures: Arc<Mutex<HashMap<ItemId, u32>>>,
/// Openers tried before the built-in ones (MP4/MOV + codecs register here).
pub openers: RwLock<Vec<Opener>>,
}
@@ -50,6 +52,7 @@ impl Default for MediaPool {
proxies: RwLock::new(HashMap::new()),
offline: Arc::new(RwLock::new(HashMap::new())),
use_proxies: AtomicBool::new(false),
+ failures: Arc::default(),
openers: RwLock::new(filmcraft_codecs::openers()),
}
}
@@ -183,6 +186,19 @@ impl MediaPool {
self.use_proxies.store(on, Ordering::Relaxed);
}
+ /// `src` (item `item`) as the monitors show it: once [`DAMAGED_AFTER`] of its video frames in a
+ /// row fail to decode or read (a damaged file), its failing frames show the unreadable slate
+ /// instead of nothing, until a frame decodes again (#626, follow-up to #762). Only for preview:
+ /// exports and other renders use the pool's sources directly, so they never contain the slate.
+ pub fn monitor_source(&self, item: ItemId, src: SharedSource) -> SharedSource {
+ Arc::new(DamagedAsSlate {
+ slate: SlateSource::new(src.info().clone(), &src.info().name, OfflineReason::Unreadable),
+ inner: src,
+ item,
+ failures: self.failures.clone(),
+ })
+ }
+
/// Why an item last rendered offline (None = online or not opened yet).
pub fn offline_status(&self, item: ItemId) -> Option<OfflineStatus> {
self.offline.read().unwrap_or_else(|e| e.into_inner()).get(&item).cloned()
@@ -419,6 +435,54 @@ impl MediaSource for UnsupportedAsSlate {
}
}
+/// Consecutive failed video frames after which a monitor treats a source as damaged. A single
+/// failure (one bad slice, a read hiccup) and the decoder's retry of it stay as they were; three
+/// in a row (an eighth of a second at 24 fps) mean the file itself is damaged.
+pub const DAMAGED_AFTER: u32 = 3;
+
+/// See [`MediaPool::monitor_source`]. Counts `Decode` / `Io` failures per item; a successful frame
+/// resets the count. Data still on its way (the web build's pending reads, see
+/// [`filmcraft_media::pending`]) neither counts nor resets, and cancellation, absent video and
+/// unsupported frames (already the slate, #762) pass through.
+struct DamagedAsSlate {
+ inner: SharedSource,
+ slate: SlateSource,
+ item: ItemId,
+ failures: Arc<Mutex<HashMap<ItemId, u32>>>,
+}
+
+impl MediaSource for DamagedAsSlate {
+ fn info(&self) -> &MediaInfo {
+ self.inner.info()
+ }
+ fn video_frame(&self, req: FrameRequest) -> filmcraft_media::Result<Arc<VideoFrame>> {
+ let r = self.inner.video_frame(req);
+ let mut failures = self.failures.lock().unwrap_or_else(|e| e.into_inner());
+ match r {
+ Ok(f) => {
+ failures.remove(&self.item);
+ Ok(f)
+ }
+ Err(MediaError::Decode(_) | MediaError::Io(_)) if !filmcraft_media::pending::is_set() => {
+ let n = failures.entry(self.item).or_insert(0);
+ *n = n.saturating_add(1);
+ if *n >= DAMAGED_AFTER {
+ drop(failures);
+ return self.slate.video_frame(req);
+ }
+ r
+ }
+ r => r,
+ }
+ }
+ fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> filmcraft_media::Result<AudioBuffer> {
+ self.inner.audio(start, frames, sample_rate)
+ }
+ fn audio_stream(&self, stream: usize, start: i64, frames: usize, sample_rate: u32) -> filmcraft_media::Result<AudioBuffer> {
+ self.inner.audio_stream(stream, start, frames, sample_rate)
+ }
+}
+
/// A proxy standing in for its full-resolution media: reports the full-resolution info and hands
/// out the proxy's (smaller) frames. A proxy whose aspect ratio differs from the original is
/// resampled to the original's aspect so it lines up exactly.
@@ -592,6 +656,123 @@ mod tests {
assert!(pool.offline_status(item).is_none());
}
+ /// A source whose video decodes (`OK`) or fails as its mode says, switchable at run time.
+ struct Flaky(MediaInfo, std::sync::atomic::AtomicU8);
+ const OK: u8 = 0;
+ const DAMAGED: u8 = 1;
+ const IO: u8 = 2;
+ const LOADING: u8 = 3;
+ const CANCELLED: u8 = 4;
+ const NO_VIDEO: u8 = 5;
+ impl Flaky {
+ fn set(&self, mode: u8) {
+ self.1.store(mode, Ordering::Relaxed);
+ }
+ }
+ impl MediaSource for Flaky {
+ fn info(&self) -> &MediaInfo {
+ &self.0
+ }
+ fn video_frame(&self, _: FrameRequest) -> filmcraft_media::Result<Arc<VideoFrame>> {
+ Err(match self.1.load(Ordering::Relaxed) {
+ OK => return Ok(Arc::new(VideoFrame::rgba8(2, 2, vec![7; 16]))),
+ DAMAGED => MediaError::Decode("damaged slice".into()),
+ IO => MediaError::Io("short read".into()),
+ LOADING => {
+ // as the web build's WebCodecs / Blob sources do
+ filmcraft_media::pending::mark();
+ MediaError::Decode("still decoding".into())
+ }
+ CANCELLED => MediaError::Cancelled,
+ _ => MediaError::NoStream("video"),
+ })
+ }
+ fn audio(&self, _start: i64, frames: usize, sample_rate: u32) -> filmcraft_media::Result<AudioBuffer> {
+ Ok(AudioBuffer::silence(sample_rate, 1, frames))
+ }
+ }
+
+ fn flaky() -> (MediaPool, ItemId, Arc<Flaky>) {
+ let info = GeneratorSource::new(filmcraft_media::Generator::ColorMatte { color: [0.0; 4] }, 16, 8, Default::default(), filmcraft_time::Tick(100))
+ .info()
+ .clone();
+ (MediaPool::default(), ItemId(1), Arc::new(Flaky(info, Default::default())))
+ }
+
+ fn frame(src: &SharedSource) -> filmcraft_media::Result<Arc<VideoFrame>> {
+ src.video_frame(FrameRequest { time: filmcraft_time::Tick(0), scale: 1.0 })
+ }
+
+ fn is_slate(r: &filmcraft_media::Result<Arc<VideoFrame>>, name: &str) -> bool {
+ r.as_ref().is_ok_and(|f| f.to_rgba8() == filmcraft_render::offline::slate_rgba8(16, 8, name, OfflineReason::Unreadable))
+ }
+
+ /// #626 follow-up to #762: a damaged file (frames failing again and again) shows the
+ /// unreadable slate in the monitors; one failure does not, and a decoded frame clears it.
+ #[test]
+ fn monitors_show_the_slate_for_persistently_failing_frames_only() {
+ let (pool, item, src) = flaky();
+ let name = src.info().name.clone();
+ let mon = pool.monitor_source(item, src.clone());
+ for mode in [DAMAGED, IO] {
+ src.set(mode);
+ for _ in 1..DAMAGED_AFTER {
+ assert!(frame(&mon).is_err(), "a transient failure stays as it was");
+ }
+ assert!(is_slate(&frame(&mon), &name), "persistent failure shows the slate");
+ assert!(is_slate(&frame(&mon), &name));
+ // recovery clears it, and one failure after that is transient again
+ src.set(OK);
+ assert_eq!(frame(&mon).unwrap().to_rgba8(), vec![7; 16]);
+ src.set(DAMAGED);
+ assert!(frame(&mon).is_err());
+ src.set(OK);
+ assert!(frame(&mon).is_ok());
+ }
+ // cancellation and absent video never become the slate
+ for mode in [CANCELLED, NO_VIDEO] {
+ src.set(mode);
+ for _ in 0..DAMAGED_AFTER * 2 {
+ assert!(frame(&mon).is_err());
+ }
+ }
+ }
+
+ /// Pictures still on their way (the web build's pending reads) never count as damage.
+ #[test]
+ fn still_decoding_frames_never_show_the_slate() {
+ let (pool, item, src) = flaky();
+ let mon = pool.monitor_source(item, src.clone());
+ src.set(LOADING);
+ for _ in 0..DAMAGED_AFTER * 4 {
+ assert!(frame(&mon).is_err());
+ assert!(filmcraft_media::pending::take());
+ }
+ // and they do not interrupt a run of real failures either way: a following damaged frame
+ // is still only the first
+ src.set(DAMAGED);
+ assert!(frame(&mon).is_err());
+ }
+
+ /// Exports read the pool's own sources: a damaged file never burns the slate into them,
+ /// however often the monitors have shown it.
+ #[test]
+ fn export_sources_never_show_the_damaged_slate() {
+ let (pool, item, src) = flaky();
+ let name = src.info().name.clone();
+ pool.insert(item, src.clone());
+ let mon = pool.monitor_source(item, pool.cached(item).unwrap());
+ src.set(DAMAGED);
+ for _ in 0..DAMAGED_AFTER {
+ let _ = frame(&mon);
+ }
+ assert!(is_slate(&frame(&mon), &name));
+ let export = pool.cached(item).unwrap();
+ for _ in 0..DAMAGED_AFTER * 2 {
+ assert!(matches!(frame(&export), Err(MediaError::Decode(_))));
+ }
+ }
+
/// #626 end to end: an H.264 profile FilmCraft does not decode yet (4:4:4) imports and shows
/// the unreadable slate with the decoder's reason, instead of a black picture.
#[test]
diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs
index c8db19cdd..9a6f52b76 100644
--- a/crates/ui-egui/src/frames.rs
+++ b/crates/ui-egui/src/frames.rs
@@ -263,7 +263,7 @@ struct JobProvider {
impl filmcraft_render::SourceProvider for JobProvider {
fn source(&self, item: ItemId) -> Option<filmcraft_media::SharedSource> {
- let s = self.inner.source(item)?;
+ let s = self.inner.pool.monitor_source(item, self.inner.source(item)?);
Some(Arc::new(TimedSource(s)))
}
}