Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Merge remote-tracking branch 'origin/main' into fix/recording-storage-stall-write-behind

Richie McIlroy12d44b8bd57f
diff --cc crates/enc-ffmpeg/src/mux/segmented_stream.rs
index fc72df9d1,d252a921c..0ea15ad15
--- a/crates/enc-ffmpeg/src/mux/segmented_stream.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
@@@ -251,27 -198,14 +251,39 @@@ impl SegmentedVideoEncoder
video_config: VideoInfo,
config: SegmentedVideoEncoderConfig,
) -> Result<Self, InitError> {
- Self::init_with_output_mode(base_path, video_config, config, false)
- Self::init_with_timed_fragments(base_path, video_config, config, false)
++ Self::init_with_options(base_path, video_config, config, false, false)
}
- /// Like [`Self::init`], but muxing, segment publication and manifest
- /// writes (including the initial manifest) run on a dedicated writer
- /// thread with a memory budget sized from the stream bitrate, so storage
- /// stalls stop blocking the encoder. Output bytes and their order are
- /// unchanged. Falls back to inline muxing if the thread cannot start.
+ pub fn init_with_timed_fragments(
+ base_path: PathBuf,
+ video_config: VideoInfo,
+ config: SegmentedVideoEncoderConfig,
+ timed_fragments: bool,
++ ) -> Result<Self, InitError> {
++ Self::init_with_options(base_path, video_config, config, timed_fragments, false)
++ }
++
++ /// Like [`Self::init_with_timed_fragments`], but muxing, segment
++ /// publication and manifest writes (including the initial manifest) run on
++ /// a dedicated writer thread with a memory budget sized from the stream
++ /// bitrate, so storage stalls stop blocking the encoder. Output bytes and
++ /// their order are unchanged. Falls back to inline muxing if the thread
++ /// cannot start.
+ pub fn init_write_behind(
+ base_path: PathBuf,
+ video_config: VideoInfo,
+ config: SegmentedVideoEncoderConfig,
++ timed_fragments: bool,
+ ) -> Result<Self, InitError> {
- Self::init_with_output_mode(base_path, video_config, config, true)
++ Self::init_with_options(base_path, video_config, config, timed_fragments, true)
+ }
+
- fn init_with_output_mode(
++ fn init_with_options(
+ base_path: PathBuf,
+ video_config: VideoInfo,
+ config: SegmentedVideoEncoderConfig,
++ timed_fragments: bool,
+ write_behind: bool,
) -> Result<Self, InitError> {
std::fs::create_dir_all(&base_path)?;
@@@ -945,122 -852,6 +962,128 @@@ mod tests
);
}
+ fn record_segments(base_path: &Path, write_behind: bool) -> Vec<(u32, bool, u64)> {
- let init = if write_behind {
- SegmentedVideoEncoder::init_write_behind
++ let mut encoder = if write_behind {
++ SegmentedVideoEncoder::init_write_behind(
++ base_path.to_path_buf(),
++ test_video_info(),
++ SegmentedVideoEncoderConfig::default(),
++ false,
++ )
+ } else {
- SegmentedVideoEncoder::init
- };
- let mut encoder = init(
- base_path.to_path_buf(),
- test_video_info(),
- SegmentedVideoEncoderConfig::default(),
- )
++ SegmentedVideoEncoder::init(
++ base_path.to_path_buf(),
++ test_video_info(),
++ SegmentedVideoEncoderConfig::default(),
++ )
++ }
+ .unwrap();
+ assert_eq!(encoder.mux.is_write_behind(), write_behind);
+ let (tx, rx) = mpsc::channel();
+ encoder.set_segment_callback(tx);
+ for i in 0..200 {
+ encoder
+ .queue_frame(
+ create_test_frame(320, 240),
+ Duration::from_micros(i * 33_320),
+ )
+ .unwrap();
+ }
+ encoder
+ .finish_with_timestamp(Duration::from_secs(7))
+ .unwrap();
+ drop(encoder);
+ rx.try_iter()
+ .map(|event| {
+ assert_eq!(
+ event.file_size,
+ std::fs::metadata(&event.path).unwrap().len()
+ );
+ (event.index, event.is_init, (event.duration * 1e6) as u64)
+ })
+ .collect()
+ }
+
+ fn published_index(base_path: &Path) -> (Vec<String>, String, serde_json::Value) {
+ let mut names: Vec<String> = std::fs::read_dir(base_path)
+ .unwrap()
+ .flatten()
+ .map(|entry| entry.file_name().to_string_lossy().into_owned())
+ .collect();
+ names.sort();
+ let mpd = std::fs::read_to_string(base_path.join("dash_manifest.mpd")).unwrap();
+ let mut manifest: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(base_path.join("manifest.json")).unwrap())
+ .unwrap();
+ for segment in manifest["segments"].as_array_mut().unwrap() {
+ segment.as_object_mut().unwrap().remove("file_size");
+ }
+ (names, mpd, manifest)
+ }
+
+ #[test]
+ fn write_behind_publishes_the_initial_manifest_from_the_writer_thread() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let encoder = SegmentedVideoEncoder::init_write_behind(
+ temp.path().to_path_buf(),
+ test_video_info(),
+ SegmentedVideoEncoderConfig::default(),
++ false,
+ )
+ .unwrap();
+ let manifest_path = temp.path().join("manifest.json");
+ let deadline = std::time::Instant::now() + Duration::from_secs(2);
+ while !manifest_path.exists() && std::time::Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ let manifest: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(&manifest_path).unwrap()).unwrap();
+ assert_eq!(manifest["is_complete"], false);
+ assert_eq!(manifest["segments"][0]["index"], 1);
+ drop(encoder);
+ }
+
+ #[test]
+ fn write_behind_segment_callback_does_not_wait_for_stalled_storage() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let mut encoder = SegmentedVideoEncoder::init_write_behind(
+ temp.path().to_path_buf(),
+ test_video_info(),
+ SegmentedVideoEncoderConfig::default(),
++ false,
+ )
+ .unwrap();
+ encoder.mux.drain();
+ let stall = encoder.mux.stall_storage_for(Duration::from_millis(800));
+ let started = std::time::Instant::now();
+ let (tx, _rx) = mpsc::channel();
+ encoder.set_segment_callback(tx);
+ encoder
+ .queue_frame(create_test_frame(320, 240), Duration::ZERO)
+ .unwrap();
+ assert!(started.elapsed() < Duration::from_millis(400));
+ stall.join().unwrap();
+ }
+
+ #[test]
+ fn write_behind_publishes_the_same_segments_as_inline_muxing() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let inline = temp.path().join("inline");
+ let behind = temp.path().join("behind");
+
+ let inline_events = record_segments(&inline, false);
+ let behind_events = record_segments(&behind, true);
+
+ // VideoToolbox payload sizes are not reproducible across sessions, so
+ // the published timeline is compared rather than media bytes.
+ assert!(inline_events.len() >= 3);
+ assert_eq!(inline_events, behind_events);
+ assert_eq!(published_index(&inline), published_index(&behind));
+ }
+
#[test]
fn failed_dash_manifest_publication_retains_video_without_claiming_completion() {
ffmpeg::init().unwrap();