Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): switch to software decoding on the first timed-out initial frame request

Richie McIlroy61ca5851808d
diff --git a/crates/rendering/src/decoder/mod.rs b/crates/rendering/src/decoder/mod.rs
index fcbdf6def..186ad71f1 100644
--- a/crates/rendering/src/decoder/mod.rs
+++ b/crates/rendering/src/decoder/mod.rs
@@ -727,6 +727,10 @@ pub(crate) const EXPORT_STALL_LIMIT: Duration = Duration::from_secs(120);
enum FrameDeadline {
/// Playback: a missed deadline returns no frame so the caller can move on.
Drop,
+ /// The first frame after a seek or for a paused preview. Nothing asks for
+ /// it again, so on macOS a missed deadline switches to software decoding
+ /// at once, which then answers it.
+ Initial,
/// Offline export: a missed deadline keeps waiting for the same reply.
Wait,
}
@@ -766,9 +770,9 @@ struct PrimaryStall {
#[cfg(target_os = "macos")]
impl MacOsDecoderFallback {
- /// A single timeout is a busy machine, not a dead reader: only a reader
- /// that misses this many request rounds in a row without delivering any
- /// frame is replaced by software decoding.
+ /// A single playback timeout is a busy machine, not a dead reader: only a
+ /// reader that misses this many playback rounds in a row without
+ /// delivering any frame is replaced by software decoding.
const MAX_TIMED_OUT_ROUNDS: u32 = 3;
fn new(name: &'static str, path: PathBuf, fps: u32) -> Self {
@@ -843,7 +847,7 @@ impl AsyncVideoDecoderHandle {
Self::INITIAL_SEEK_TIMEOUT_MS,
self.max_fallback_distance
.min(Self::INITIAL_MAX_FALLBACK_DISTANCE),
- FrameDeadline::Drop,
+ FrameDeadline::Initial,
)
.await
.ok()
@@ -899,7 +903,9 @@ impl AsyncVideoDecoderHandle {
fallback.primary_answered();
return Ok(Some(frame));
}
- Ok(FrameReply::TimedOut) if !fallback.primary_stalled(sent_at) => {
+ Ok(FrameReply::TimedOut)
+ if deadline == FrameDeadline::Drop && !fallback.primary_stalled(sent_at) =>
+ {
return Ok(None);
}
Ok(FrameReply::TimedOut) => ("AVAssetReader stopped answering", Ok(None)),
diff --git a/crates/rendering/src/decoder/model/mod.rs b/crates/rendering/src/decoder/model/mod.rs
index 0df81b813..d3be6de1c 100644
--- a/crates/rendering/src/decoder/model/mod.rs
+++ b/crates/rendering/src/decoder/model/mod.rs
@@ -1157,10 +1157,11 @@ fn avassetreader_answers_stay_exact_when_its_reader_fails() {
}
}
-/// A busy machine can miss one decode deadline without anything being
+/// A busy machine can miss one playback deadline without anything being
/// wrong with the reader: that request gets no frame, and hardware decoding
-/// stays. Only a reader that answers without a frame, or misses every
-/// deadline round after round, is replaced by software decoding.
+/// stays. A reader that answers without a frame, misses every playback
+/// deadline round after round, or misses the deadline of a first frame that
+/// nothing will ask for again is replaced by software decoding.
#[cfg(target_os = "macos")]
#[test]
fn avassetreader_switches_to_software_only_when_its_reader_stops_answering() {
@@ -1264,6 +1265,48 @@ fn avassetreader_switches_to_software_only_when_its_reader_stops_answering() {
);
});
+ let registration = scripted::register(script.clone());
+ 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);
+ assert_eq!(
+ handle
+ .get_frame_initial(time(5))
+ .await
+ .as_ref()
+ .and_then(frame_identity),
+ Some(5)
+ );
+ registration.set_vend_delay(slow_vend);
+ let late = handle
+ .get_frame_with_timeout(time(6), deadline_ms, 2, FrameDeadline::Drop)
+ .await;
+ assert!(
+ matches!(late, Ok(None)),
+ "request 6 beat a {deadline_ms} ms deadline"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::AVAssetReader);
+ let seek = handle.get_frame_with_timeout(time(10), deadline_ms, 2, FrameDeadline::Initial);
+ let lift_delay = async {
+ tokio::time::sleep(Duration::from_millis(deadline_ms / 2)).await;
+ registration.set_vend_delay(Duration::ZERO);
+ };
+ let (answered, ()) = tokio::join!(seek, lift_delay);
+ assert_eq!(
+ answered.unwrap().as_ref().and_then(frame_identity),
+ Some(10),
+ "the first timed-out initial request"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::FFmpegSoftware);
+ assert_eq!(
+ handle.fallback_reason(),
+ Some("AVAssetReader stopped answering")
+ );
+ });
+
let registration = scripted::register(script);
registration.fail_avf_read_at(40);
registration.fail_avf_read_at(40);