Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(mediafoundation-ffmpeg): keep muxer pts strictly monotonic in stream ticks

Cursor Agent257d12b8216e
diff --git a/crates/mediafoundation-ffmpeg/src/h264.rs b/crates/mediafoundation-ffmpeg/src/h264.rs
index 55e8a5c0a..d9fd9faf3 100644
--- a/crates/mediafoundation-ffmpeg/src/h264.rs
+++ b/crates/mediafoundation-ffmpeg/src/h264.rs
@@ -32,6 +32,7 @@ pub struct H264StreamMuxer {
time_base: ffmpeg::Rational,
is_finished: bool,
frame_count: u64,
+ last_written_pts: Option<i64>,
}
impl H264StreamMuxer {
@@ -83,6 +84,7 @@ impl H264StreamMuxer {
time_base,
is_finished: false,
frame_count: 0,
+ last_written_pts: None,
})
}
@@ -102,6 +104,24 @@ impl H264StreamMuxer {
output.stream(self.stream_index).unwrap().time_base(),
);
+ // MediaFoundation stamps samples in 100ns ticks, but this stream's
+ // time base is ~333x coarser (1/(fps*1000)): two strictly increasing
+ // sample times can land on the same output tick, and the mov muxer
+ // rejects the duplicate pts/dts — the same unit-mismatch class as the
+ // AVFoundation -16364 failures. A tie carries no time: advance one
+ // tick in the writer-visible unit and let real timestamps take over,
+ // like normalize_input_pts in cap-enc-ffmpeg. pts==dts here (MF
+ // encoders are configured without B-frames).
+ if let Some(pts) = packet.pts() {
+ let pts = match self.last_written_pts {
+ Some(last) if pts <= last => last + 1,
+ _ => pts,
+ };
+ self.last_written_pts = Some(pts);
+ packet.set_pts(Some(pts));
+ packet.set_dts(Some(pts));
+ }
+
packet.write_interleaved(output)?;
Ok(())