Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(enc-ffmpeg): add bounded write-behind muxing for segmented and audio outputs
Richie McIlroy5ed06562be06
diff --git a/crates/enc-ffmpeg/src/mux/dash_audio.rs b/crates/enc-ffmpeg/src/mux/dash_audio.rsindex b3dfcc007..83bfb4f30 100644--- a/crates/enc-ffmpeg/src/mux/dash_audio.rs+++ b/crates/enc-ffmpeg/src/mux/dash_audio.rs@@ -1,6 +1,7 @@use super::fragment_metadata::read_fragment_metadata;use crate::audio::aac::{AACEncoder, AACEncoderError};use crate::mux::segmented_stream::{SegmentCompletedEvent, SegmentMediaType};+use crate::mux::write_behind::{AUDIO_BUDGET_BYTES, MuxSink, MuxTarget};use cap_media_info::AudioInfo;use ffmpeg::{format, frame};use serde::Serialize;@@ -47,6 +48,11 @@ fn sync_file(path: &Path) {pub struct DashAudioSegmentEncoder {base_path: PathBuf,encoder: AACEncoder,+ mux: MuxSink<AudioSegmentMux>,+}++struct AudioSegmentMux {+ base_path: PathBuf,output: format::context::Output,initial_padding: Duration,@@ -60,6 +66,26 @@ pub struct DashAudioSegmentEncoder {init_notified: bool,}+impl MuxTarget for AudioSegmentMux {+ fn output(&self) -> &format::context::Output {+ &self.output+ }++ fn output_mut(&mut self) -> &mut format::context::Output {+ &mut self.output+ }++ fn packet_written(&mut self) {+ self.try_notify_init_segment();++ self.frames_since_segment_scan += 1;+ if self.frames_since_segment_scan >= 10 {+ self.frames_since_segment_scan = 0;+ self.flush_pending_segments();+ }+ }+}+#[derive(Debug, Clone)]pub struct AudioSegmentInfo {pub path: PathBuf,@@ -197,9 +223,8 @@ impl DashAudioSegmentEncoder {"Initialized DASH audio segment encoder (init.mp4 + m4s segments)");- let instance = Self {- base_path,- encoder,+ let mux = AudioSegmentMux {+ base_path: base_path.clone(),output,initial_padding,current_index: 1,@@ -209,21 +234,83 @@ impl DashAudioSegmentEncoder {init_notified: false,};- instance.write_in_progress_manifest();+ mux.write_in_progress_manifest();++ Ok(Self {+ base_path,+ encoder,+ mux: MuxSink::Direct(mux),+ })+ }++ /// Moves muxing, segment publication and manifest writes onto a+ /// dedicated writer thread so a stalled output volume no longer blocks+ /// the caller. Bytes on disk and their order are unchanged.+ pub fn enable_write_behind(&mut self) -> std::io::Result<()> {+ self.mux+ .enable_write_behind("audio-segments", AUDIO_BUDGET_BYTES)+ }- Ok(instance)+ pub fn is_write_behind(&self) -> bool {+ self.mux.is_write_behind()}pub fn set_segment_callback(&mut self, tx: std::sync::mpsc::Sender<SegmentCompletedEvent>) {- self.segment_tx = Some(tx);- self.try_notify_init_segment();+ self.mux.with_target(|mux| {+ mux.segment_tx = Some(tx);+ mux.try_notify_init_segment();+ });}+ pub fn queue_frame(+ &mut self,+ frame: frame::Audio,+ timestamp: Duration,+ ) -> Result<(), QueueFrameError> {+ self.encoder+ .send_frame_to(frame, timestamp, &mut self.mux)?;++ self.mux.frame_queued();++ Ok(())+ }++ pub fn finish(&mut self) -> Result<(), FinishError> {+ let encoder = &mut self.encoder;+ let flush_result = self+ .mux+ .finish_flush(|mux| encoder.flush_to(mux))+ .map_err(std::io::Error::from)?+ .inspect_err(|error| {+ tracing::warn!(%error, "Audio encoder flush failed");+ });+ self.mux.with_target(|mux| mux.finish(flush_result))+ }++ pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {+ self.finish()+ }++ pub fn completed_segments(&self) -> Vec<AudioSegmentInfo> {+ self.mux+ .with_target_ref(|mux| mux.completed_segments.clone())+ }++ pub fn base_path(&self) -> &Path {+ &self.base_path+ }++ pub fn init_segment_path(&self) -> PathBuf {+ self.base_path.join(INIT_SEGMENT_NAME)+ }+}++impl AudioSegmentMux {fn try_notify_init_segment(&mut self) {if self.init_notified {return;}- let init_path = self.init_segment_path();+ let init_path = self.base_path.join(INIT_SEGMENT_NAME);if let Ok(meta) = std::fs::metadata(&init_path)&& meta.len() > 0{@@ -247,25 +334,6 @@ impl DashAudioSegmentEncoder {}}- pub fn queue_frame(- &mut self,- frame: frame::Audio,- timestamp: Duration,- ) -> Result<(), QueueFrameError> {- self.encoder- .send_frame(frame, timestamp, &mut self.output)?;-- self.try_notify_init_segment();-- self.frames_since_segment_scan += 1;- if self.frames_since_segment_scan >= 10 {- self.frames_since_segment_scan = 0;- self.flush_pending_segments();- }-- Ok(())- }-fn flush_pending_segments(&mut self) {let first_index = self.current_index;loop {@@ -367,10 +435,7 @@ impl DashAudioSegmentEncoder {}}- pub fn finish(&mut self) -> Result<(), FinishError> {- let flush_result = self.encoder.flush(&mut self.output).inspect_err(|error| {- tracing::warn!(%error, "Audio encoder flush failed");- });+ fn finish(&mut self, flush_result: Result<(), ffmpeg::Error>) -> Result<(), FinishError> {let trailer_result = self.output.write_trailer().inspect_err(|error| {tracing::warn!(%error, "Audio trailer publication failed");});@@ -394,10 +459,6 @@ impl DashAudioSegmentEncoder {Ok(())}- pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {- self.finish()- }-fn finalize_pending_tmp_files(&self) {let Ok(entries) = std::fs::read_dir(&self.base_path) else {return;@@ -523,18 +584,6 @@ impl DashAudioSegmentEncoder {let manifest_path = self.base_path.join("manifest.json");atomic_write_json(&manifest_path, &manifest)}-- pub fn completed_segments(&self) -> &[AudioSegmentInfo] {- &self.completed_segments- }-- pub fn base_path(&self) -> &Path {- &self.base_path- }-- pub fn init_segment_path(&self) -> PathBuf {- self.base_path.join(INIT_SEGMENT_NAME)- }}#[cfg(test)]@@ -570,6 +619,62 @@ mod tests {frame}+ fn record_audio_segments(base_path: &Path, write_behind: bool) -> Vec<(u32, u64, bool)> {+ let mut encoder = DashAudioSegmentEncoder::init(+ base_path.to_path_buf(),+ test_audio_info(),+ DashAudioSegmentEncoderConfig::default(),+ )+ .unwrap();+ if write_behind {+ encoder.enable_write_behind().unwrap();+ }+ let (tx, rx) = mpsc::channel();+ encoder.set_segment_callback(tx);+ for i in 0..500u64 {+ encoder+ .queue_frame(+ create_test_audio_frame(1024, i * 1024),+ Duration::from_secs_f64(i as f64 * 1024.0 / 48_000.0),+ )+ .unwrap();+ }+ encoder.finish().unwrap();+ drop(encoder);+ rx.try_iter()+ .map(|event| (event.index, event.file_size, event.is_init))+ .collect()+ }++ #[test]+ fn write_behind_publishes_the_same_audio_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_audio_segments(&inline, false);+ let behind_events = record_audio_segments(&behind, true);++ assert!(inline_events.len() >= 3);+ assert_eq!(inline_events, behind_events);+ let files = |path: &Path| {+ let mut files: Vec<(String, Vec<u8>)> = std::fs::read_dir(path)+ .unwrap()+ .flatten()+ .map(|entry| {+ (+ entry.file_name().to_string_lossy().into_owned(),+ std::fs::read(entry.path()).unwrap(),+ )+ })+ .collect();+ files.sort();+ files+ };+ assert_eq!(files(&inline), files(&behind));+ }+#[test]fn audio_manifest_and_events_match_muxer_timeline() {ffmpeg::init().unwrap();diff --git a/crates/enc-ffmpeg/src/mux/fragmented_audio.rs b/crates/enc-ffmpeg/src/mux/fragmented_audio.rsindex 1203ad72c..a6d575f29 100644--- a/crates/enc-ffmpeg/src/mux/fragmented_audio.rs+++ b/crates/enc-ffmpeg/src/mux/fragmented_audio.rs@@ -2,15 +2,28 @@ use cap_media_info::AudioInfo;use ffmpeg::{format, frame};use std::{path::PathBuf, time::Duration};+use super::write_behind::{AUDIO_BUDGET_BYTES, MuxSink, MuxTarget};use crate::audio::aac::{AACEncoder, AACEncoderError};pub struct FragmentedAudioFile {encoder: AACEncoder,- output: format::context::Output,+ output: MuxSink<AudioOutput>,finished: bool,has_frames: bool,}+struct AudioOutput(format::context::Output);++impl MuxTarget for AudioOutput {+ fn output(&self) -> &format::context::Output {+ &self.0+ }++ fn output_mut(&mut self) -> &mut format::context::Output {+ &mut self.0+ }+}+#[derive(thiserror::Error, Debug)]pub enum InitError {#[error("FFmpeg: {0}")]@@ -81,12 +94,19 @@ impl FragmentedAudioFile {Ok(Self {encoder,- output,+ output: MuxSink::Direct(AudioOutput(output)),finished: false,has_frames: false,})}+ /// Hands encoded packets to a dedicated writer thread so a stalled+ /// output volume no longer blocks the caller (an async runtime worker in+ /// the recording pipeline). Bytes on disk and their order are unchanged.+ pub fn enable_write_behind(&mut self) -> std::io::Result<()> {+ self.output.enable_write_behind("audio", AUDIO_BUDGET_BYTES)+ }+pub fn encoder(&self) -> &AACEncoder {&self.encoder}@@ -96,8 +116,16 @@ impl FragmentedAudioFile {frame: frame::Audio,timestamp: Duration,) -> Result<(), ffmpeg::Error> {+ if self.finished && self.output.is_write_behind() {+ return Err(ffmpeg::Error::Exit);+ }self.has_frames = true;- self.encoder.send_frame(frame, timestamp, &mut self.output)+ self.encoder+ .send_frame_to(frame, timestamp, &mut self.output)+ }++ pub fn is_write_behind(&self) -> bool {+ self.output.is_write_behind()}pub fn finish(&mut self) -> Result<Result<(), ffmpeg::Error>, FinishError> {@@ -114,12 +142,19 @@ impl FragmentedAudioFile {self.finished = true;- let flush_result = if self.has_frames {- self.encoder.flush(&mut self.output)- } else {- Ok(())- };- let trailer_result = self.output.write_trailer();+ let has_frames = self.has_frames;+ let encoder = &mut self.encoder;+ let flush_result = self+ .output+ .finish_flush(|output| {+ if has_frames {+ encoder.flush_to(output)+ } else {+ Ok(())+ }+ })+ .map_err(|stalled| FinishError::WriteTrailerFailed(stalled.into()))?;+ let trailer_result = self.output.with_target(|output| output.0.write_trailer());finish_result(flush_result, trailer_result)}}@@ -220,6 +255,48 @@ mod publication_tests {output.finish().unwrap().unwrap();}+ #[test]+ fn write_behind_audio_matches_inline_bytes() {+ let directory = tempfile::tempdir().unwrap();+ let info = AudioInfo::new_raw(Sample::F32(Type::Packed), 48_000, 1);+ let inline_path = directory.path().join("inline.m4a");+ let mut inline = FragmentedAudioFile::init(inline_path.clone(), info).unwrap();+ queue_tone(&mut inline, 300);+ inline.finish().unwrap().unwrap();++ let behind_path = directory.path().join("behind.m4a");+ let mut behind = FragmentedAudioFile::init(behind_path.clone(), info).unwrap();+ behind.enable_write_behind().unwrap();+ queue_tone(&mut behind, 300);+ behind.output.drain();+ let mut input = format::input(&behind_path).unwrap();+ assert!(input.packets().next().is_some());+ drop(input);+ behind.finish().unwrap().unwrap();++ assert_eq!(+ std::fs::read(&inline_path).unwrap(),+ std::fs::read(&behind_path).unwrap()+ );+ }++ #[test]+ fn write_behind_audio_rejects_frames_after_finish() {+ let directory = tempfile::tempdir().unwrap();+ let info = AudioInfo::new_raw(Sample::F32(Type::Packed), 48_000, 1);+ let mut output =+ FragmentedAudioFile::init(directory.path().join("finished.m4a"), info).unwrap();+ output.enable_write_behind().unwrap();+ queue_tone(&mut output, 10);+ output.finish().unwrap().unwrap();+ let frame =+ ffmpeg::frame::Audio::new(Sample::F32(Type::Packed), 1024, ffmpeg::ChannelLayout::MONO);+ assert_eq!(+ output.queue_frame(frame, Duration::from_secs(1)),+ Err(ffmpeg::Error::Exit)+ );+ }+#[test]fn short_fragmented_audio_remains_readable_after_finish() {let directory = tempfile::tempdir().unwrap();@@ -284,7 +361,7 @@ mod publication_tests {fn set_output_error(output: &mut FragmentedAudioFile) {unsafe {- let context = output.output.as_mut_ptr();+ let context = output.output.with_target(|output| output.0.as_mut_ptr());assert!(!(*context).pb.is_null());(*(*context).pb).error = ffmpeg::Error::Other {errno: ffmpeg::error::EIO,diff --git a/crates/enc-ffmpeg/src/mux/mod.rs b/crates/enc-ffmpeg/src/mux/mod.rsindex 24e53ac50..c0eb94e3a 100644--- a/crates/enc-ffmpeg/src/mux/mod.rs+++ b/crates/enc-ffmpeg/src/mux/mod.rs@@ -9,3 +9,4 @@ pub mod mp4;pub mod ogg;pub mod segmented_audio;pub mod segmented_stream;+pub(crate) mod write_behind;diff --git a/crates/enc-ffmpeg/src/mux/ogg.rs b/crates/enc-ffmpeg/src/mux/ogg.rsindex c8d700c66..e7a532777 100644--- a/crates/enc-ffmpeg/src/mux/ogg.rs+++ b/crates/enc-ffmpeg/src/mux/ogg.rs@@ -1,14 +1,27 @@use ffmpeg::{format, frame};use std::{path::PathBuf, time::Duration};+use super::write_behind::{AUDIO_BUDGET_BYTES, MuxSink, MuxTarget};use crate::audio::opus::{OpusEncoder, OpusEncoderError};pub struct OggFile {encoder: OpusEncoder,- output: format::context::Output,+ output: MuxSink<OggOutput>,finished: bool,}+struct OggOutput(format::context::Output);++impl MuxTarget for OggOutput {+ fn output(&self) -> &format::context::Output {+ &self.0+ }++ fn output_mut(&mut self) -> &mut format::context::Output {+ &mut self.0+ }+}+#[derive(thiserror::Error, Debug)]pub enum FinishError {#[error("Already finished")]@@ -32,11 +45,18 @@ impl OggFile {Ok(Self {encoder,- output,+ output: MuxSink::Direct(OggOutput(output)),finished: false,})}+ /// Hands encoded packets to a dedicated writer thread so a stalled+ /// output volume no longer blocks the caller. Bytes on disk and their+ /// order are unchanged.+ pub fn enable_write_behind(&mut self) -> std::io::Result<()> {+ self.output.enable_write_behind("ogg", AUDIO_BUDGET_BYTES)+ }+pub fn encoder(&self) -> &OpusEncoder {&self.encoder}@@ -46,7 +66,15 @@ impl OggFile {frame: frame::Audio,timestamp: Duration,) -> Result<(), ffmpeg::Error> {- self.encoder.queue_frame(frame, timestamp, &mut self.output)+ if self.finished && self.output.is_write_behind() {+ return Err(ffmpeg::Error::Exit);+ }+ self.encoder+ .queue_frame_to(frame, timestamp, &mut self.output)+ }++ pub fn is_write_behind(&self) -> bool {+ self.output.is_write_behind()}pub fn finish(&mut self) -> Result<Result<(), ffmpeg::Error>, FinishError> {@@ -56,9 +84,13 @@ impl OggFile {self.finished = true;- let flush_result = self.encoder.flush(&mut self.output);+ let encoder = &mut self.encoder;+ let flush_result = self+ .output+ .finish_flush(|output| encoder.flush_to(output))+ .map_err(|stalled| FinishError::WriteTrailerFailed(stalled.into()))?;self.output- .write_trailer()+ .with_target(|output| output.0.write_trailer()).map_err(FinishError::WriteTrailerFailed)?;Ok(flush_result)@@ -79,6 +111,15 @@ mod tests {use ffmpeg::ChannelLayout;fn write_recording(path: std::path::PathBuf, input_rate: u32, total_samples: usize) {+ write_recording_with(path, input_rate, total_samples, false);+ }++ fn write_recording_with(+ path: std::path::PathBuf,+ input_rate: u32,+ total_samples: usize,+ write_behind: bool,+ ) {let info = AudioInfo::new_raw(format::Sample::F32(format::sample::Type::Packed),input_rate,@@ -86,6 +127,9 @@ mod tests {);let mut file = OggFile::init(path, OpusEncoder::factory(info)).unwrap();+ if write_behind {+ file.enable_write_behind().unwrap();+ }let chunk = 1024usize;let mut written = 0usize;@@ -142,6 +186,22 @@ mod tests {);}+ #[test]+ fn write_behind_output_has_the_same_duration_and_size() {+ let dir = tempfile::tempdir().unwrap();+ let inline = dir.path().join("inline.ogg");+ let behind = dir.path().join("behind.ogg");++ write_recording_with(inline.clone(), 48_000, 48_000 * 5, false);+ write_recording_with(behind.clone(), 48_000, 48_000 * 5, true);++ assert_eq!(+ std::fs::metadata(&inline).unwrap().len(),+ std::fs::metadata(&behind).unwrap().len()+ );+ assert!((probed_duration_secs(&behind) - probed_duration_secs(&inline)).abs() < 1e-6);+ }+#[test]fn sub_frame_input_is_flushed_not_dropped() {// Less input than one opus frame (20ms): finish() must drain thediff --git a/crates/enc-ffmpeg/src/mux/segmented_stream.rs b/crates/enc-ffmpeg/src/mux/segmented_stream.rsindex 3b6091ceb..f32d6110e 100644--- a/crates/enc-ffmpeg/src/mux/segmented_stream.rs+++ b/crates/enc-ffmpeg/src/mux/segmented_stream.rs@@ -1,4 +1,5 @@use super::fragment_metadata::read_fragment_metadata;+use super::write_behind::{MuxSink, MuxTarget};use cap_media_info::VideoInfo;use ffmpeg::{format, frame};use serde::Serialize;@@ -76,10 +77,18 @@ pub struct SegmentedVideoEncoder {base_path: PathBuf,encoder: H264Encoder,+ mux: MuxSink<SegmentMux>,++ segment_duration: Duration,++ disk_space_callback: Option<DiskSpaceCallback>,+}++struct SegmentMux {+ base_path: PathBuf,output: format::context::Output,current_index: u32,- segment_duration: Duration,completed_segments: Vec<VideoSegmentInfo>,@@ -87,9 +96,36 @@ pub struct SegmentedVideoEncoder {codec_info: CodecInfo,- disk_space_callback: Option<DiskSpaceCallback>,segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,init_notified: bool,+ initial_manifest_pending: bool,+}++impl MuxTarget for SegmentMux {+ fn output(&self) -> &format::context::Output {+ &self.output+ }++ fn output_mut(&mut self) -> &mut format::context::Output {+ &mut self.output+ }++ fn writer_started(&mut self) {+ self.write_initial_manifest();+ }++ fn packet_written(&mut self) {+ // The encoder holds one packet back to stamp real durations, so the+ // init segment materializes a frame later than the first queue call;+ // keep trying until it lands (no-op once notified).+ self.try_notify_init_segment();++ self.frames_since_segment_scan += 1;+ if self.frames_since_segment_scan >= 10 {+ self.frames_since_segment_scan = 0;+ self.flush_pending_segments();+ }+ }}#[derive(Debug, Clone)]@@ -197,6 +233,28 @@ impl SegmentedVideoEncoder {base_path: PathBuf,video_config: VideoInfo,config: SegmentedVideoEncoderConfig,+ ) -> Result<Self, InitError> {+ Self::init_with_output_mode(base_path, video_config, config, 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_write_behind(+ base_path: PathBuf,+ video_config: VideoInfo,+ config: SegmentedVideoEncoderConfig,+ ) -> Result<Self, InitError> {+ Self::init_with_output_mode(base_path, video_config, config, true)+ }++ fn init_with_output_mode(+ base_path: PathBuf,+ video_config: VideoInfo,+ config: SegmentedVideoEncoderConfig,+ write_behind: bool,) -> Result<Self, InitError> {std::fs::create_dir_all(&base_path)?;@@ -295,21 +353,38 @@ impl SegmentedVideoEncoder {"Initialized segmented video encoder with FFmpeg DASH muxer (init.mp4 + m4s segments)");- let instance = Self {- base_path,- encoder,+ let mux = SegmentMux {+ base_path: base_path.clone(),output,current_index: 1,- segment_duration: config.segment_duration,completed_segments: Vec::new(),frames_since_segment_scan: 0,codec_info,- disk_space_callback: None,segment_tx: None,init_notified: false,+ initial_manifest_pending: true,};- instance.write_in_progress_manifest();+ let mut instance = Self {+ base_path,+ encoder,+ mux: MuxSink::Direct(mux),+ segment_duration: config.segment_duration,+ disk_space_callback: None,+ };++ if write_behind {+ let budget = super::write_behind::video_budget_bytes(instance.encoder.bit_rate());+ match instance.mux.enable_write_behind("video", budget) {+ Ok(()) => tracing::debug!(budget_bytes = budget, "Video write-behind enabled"),+ Err(error) => {+ tracing::warn!(%error, "Video write-behind unavailable, muxing inline");+ }+ }+ }+ if !instance.mux.is_write_behind() {+ instance.mux.with_target(SegmentMux::write_initial_manifest);+ }Ok(instance)}@@ -319,28 +394,10 @@ impl SegmentedVideoEncoder {}pub fn set_segment_callback(&mut self, tx: std::sync::mpsc::Sender<SegmentCompletedEvent>) {- self.segment_tx = Some(tx);- self.try_notify_init_segment();- }-- fn try_notify_init_segment(&mut self) {- if self.init_notified {- return;- }- let init_path = self.init_segment_path();- if let Ok(meta) = std::fs::metadata(&init_path)- && meta.len() > 0- {- self.init_notified = true;- self.notify_segment(SegmentCompletedEvent {- path: init_path,- index: 0,- duration: 0.0,- file_size: meta.len(),- is_init: true,- media_type: SegmentMediaType::Video,- });- }+ self.mux.with_target(|mux| {+ mux.segment_tx = Some(tx);+ mux.try_notify_init_segment();+ });}/// Whether frames should be queued as CVPixelBuffers via@@ -377,20 +434,114 @@ impl SegmentedVideoEncoder {// stream restarts, dropped frames) stay in the timeline instead of// compressing it and drifting video ahead of audio.self.encoder- .queue_frame(frame, timestamp, &mut self.output)?;+ .queue_frame_to(frame, timestamp, &mut self.mux)?;- // The encoder holds one packet back to stamp real durations, so the- // init segment materializes a frame later than the first queue call;- // keep trying until it lands (no-op once notified).- self.try_notify_init_segment();+ self.mux.frame_queued();- self.frames_since_segment_scan += 1;- if self.frames_since_segment_scan >= 10 {- self.frames_since_segment_scan = 0;- self.flush_pending_segments();+ Ok(())+ }++ pub fn finish(&mut self) -> Result<(), FinishError> {+ let encoder = &mut self.encoder;+ let flush_result = self+ .mux+ .finish_flush(|mux| encoder.flush_to(mux))+ .map_err(std::io::Error::from)?+ .inspect_err(|error| {+ tracing::warn!(%error, "Video encoder flush failed");+ });+ self.mux.with_target(|mux| mux.finish(flush_result))+ }++ pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {+ self.finish()+ }++ pub fn completed_segments(&self) -> Vec<VideoSegmentInfo> {+ self.mux+ .with_target_ref(|mux| mux.completed_segments.clone())+ }++ pub fn current_encoder(&self) -> Option<&H264Encoder> {+ Some(&self.encoder)+ }++ pub fn current_encoder_mut(&mut self) -> Option<&mut H264Encoder> {+ Some(&mut self.encoder)+ }++ pub fn base_path(&self) -> &Path {+ &self.base_path+ }++ pub fn segment_duration(&self) -> Duration {+ self.segment_duration+ }++ pub fn current_index(&self) -> u32 {+ self.mux.with_target_ref(|mux| mux.current_index)+ }++ pub fn init_segment_path(&self) -> PathBuf {+ self.base_path.join(INIT_SEGMENT_NAME)+ }++ pub fn validate_init_segment(&self) -> Result<(), String> {+ let init_path = self.init_segment_path();++ if !init_path.exists() {+ return Err(format!(+ "init.mp4 is missing at {}. M4S segments will be unplayable without it.",+ init_path.display()+ ));}- Ok(())+ match std::fs::metadata(&init_path) {+ Ok(metadata) => {+ let size = metadata.len();+ if size < 100 {+ return Err(format!(+ "init.mp4 at {} is too small ({} bytes). It may be corrupted.",+ init_path.display(),+ size+ ));+ }+ Ok(())+ }+ Err(e) => Err(format!(+ "Cannot read init.mp4 metadata at {}: {}",+ init_path.display(),+ e+ )),+ }+ }+}++impl SegmentMux {+ fn write_initial_manifest(&mut self) {+ if std::mem::take(&mut self.initial_manifest_pending) {+ self.write_in_progress_manifest();+ }+ }++ fn try_notify_init_segment(&mut self) {+ if self.init_notified {+ return;+ }+ let init_path = self.base_path.join(INIT_SEGMENT_NAME);+ if let Ok(meta) = std::fs::metadata(&init_path)+ && meta.len() > 0+ {+ self.init_notified = true;+ self.notify_segment(SegmentCompletedEvent {+ path: init_path,+ index: 0,+ duration: 0.0,+ file_size: meta.len(),+ is_init: true,+ media_type: SegmentMediaType::Video,+ });+ }}fn notify_segment(&self, event: SegmentCompletedEvent) {@@ -493,10 +644,7 @@ impl SegmentedVideoEncoder {}}- pub fn finish(&mut self) -> Result<(), FinishError> {- let flush_result = self.encoder.flush(&mut self.output).inspect_err(|error| {- tracing::warn!(%error, "Video encoder flush failed");- });+ fn finish(&mut self, flush_result: Result<(), ffmpeg::Error>) -> Result<(), FinishError> {let trailer_result = self.output.write_trailer().inspect_err(|error| {tracing::warn!(%error, "Video trailer publication failed");});@@ -520,10 +668,6 @@ impl SegmentedVideoEncoder {Ok(())}- pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {- self.finish()- }-fn finalize_pending_tmp_files(&self) {let Ok(entries) = std::fs::read_dir(&self.base_path) else {return;@@ -688,64 +832,6 @@ impl SegmentedVideoEncoder {let manifest_path = self.base_path.join("manifest.json");atomic_write_json(&manifest_path, &manifest)}-- pub fn completed_segments(&self) -> &[VideoSegmentInfo] {- &self.completed_segments- }-- pub fn current_encoder(&self) -> Option<&H264Encoder> {- Some(&self.encoder)- }-- pub fn current_encoder_mut(&mut self) -> Option<&mut H264Encoder> {- Some(&mut self.encoder)- }-- pub fn base_path(&self) -> &Path {- &self.base_path- }-- pub fn segment_duration(&self) -> Duration {- self.segment_duration- }-- pub fn current_index(&self) -> u32 {- self.current_index- }-- pub fn init_segment_path(&self) -> PathBuf {- self.base_path.join(INIT_SEGMENT_NAME)- }-- pub fn validate_init_segment(&self) -> Result<(), String> {- let init_path = self.init_segment_path();-- if !init_path.exists() {- return Err(format!(- "init.mp4 is missing at {}. M4S segments will be unplayable without it.",- init_path.display()- ));- }-- match std::fs::metadata(&init_path) {- Ok(metadata) => {- let size = metadata.len();- if size < 100 {- return Err(format!(- "init.mp4 at {} is too small ({} bytes). It may be corrupted.",- init_path.display(),- size- ));- }- Ok(())- }- Err(e) => Err(format!(- "Cannot read init.mp4 metadata at {}: {}",- init_path.display(),- e- )),- }- }}#[cfg(test)]@@ -838,6 +924,100 @@ mod tests {);}+ fn record_segments(base_path: &Path, write_behind: bool) -> Vec<(u32, bool, u64)> {+ let init = if write_behind {+ SegmentedVideoEncoder::init_write_behind+ } else {+ SegmentedVideoEncoder::init+ };+ let mut encoder = 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(),+ )+ .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_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();diff --git a/crates/enc-ffmpeg/src/mux/write_behind.rs b/crates/enc-ffmpeg/src/mux/write_behind.rsnew file mode 100644index 000000000..d2260cf27--- /dev/null+++ b/crates/enc-ffmpeg/src/mux/write_behind.rs@@ -0,0 +1,892 @@+//! Write-behind muxing: encoders hand finished packets to a dedicated writer+//! thread that owns the `AVFormatContext`, so a stalled output volume (slow+//! SD card, external or network drive) blocks only that thread instead of+//! the encoder or the async runtime driving it.+//!+//! Packets are written in exactly the order they were produced, so the+//! on-disk bytes are identical to writing inline. Memory is bounded by a byte+//! budget; once it is full the producer blocks until the writer catches up,+//! which is the inline behaviour.+//!+//! Finishing drains the queue while storage keeps making progress, bounded+//! by [`DRAIN_NO_PROGRESS_LIMIT`] without a completed write and+//! [`DRAIN_TOTAL_LIMIT`] overall. Past the total limit the unwritten tail is+//! discarded and the output is finalized normally ([`DrainOutcome::Truncated`]).+//! When no write completes for the no-progress limit, finishing returns+//! [`DrainOutcome::Stuck`] without the trailer or final manifest, leaving the+//! files in the state recovery expects from an interrupted recording. As with+//! an encoder blocked in an inline write, the queued packets still reach disk+//! once storage recovers. Dropping a writer that was never finished discards+//! its queue instead and touches nothing after its current call returns.++use crate::base::PacketSink;+use ffmpeg::{Packet, Rational, format};+use std::{+ collections::VecDeque,+ sync::{+ Arc, Condvar, Mutex, MutexGuard,+ atomic::{AtomicBool, Ordering},+ },+ thread::JoinHandle,+ time::{Duration, Instant},+};++const VIDEO_BUFFER_WINDOW_SECS: u64 = 40;+const MIN_VIDEO_BUDGET_BYTES: usize = 32 * 1024 * 1024;+const MAX_VIDEO_BUDGET_BYTES: usize = 512 * 1024 * 1024;+const VIDEO_BUDGET_MEMORY_DIVISOR: u64 = 32;+pub(crate) const AUDIO_BUDGET_BYTES: usize = 16 * 1024 * 1024;+const STALL_LOG_THRESHOLD: Duration = Duration::from_secs(1);+const WRITE_ERROR_LOG_INTERVAL: u64 = 100;+// Muxers join their encoder thread for up to 5 s before finishing, and an+// unjoined pipeline stop gives up after 10 s; draining for at most 4 s keeps+// the trailer and manifest inside that window.+pub(crate) const DRAIN_NO_PROGRESS_LIMIT: Duration = Duration::from_secs(4);+pub(crate) const DRAIN_TOTAL_LIMIT: Duration = Duration::from_secs(4);+const DROP_JOIN_LIMIT: Duration = Duration::from_millis(250);+const WAIT_POLL: Duration = Duration::from_millis(50);++pub(crate) fn video_budget_bytes(bits_per_second: u64) -> usize {+ video_budget_for(bits_per_second, total_memory_bytes())+}++fn video_budget_for(bits_per_second: u64, total_memory: Option<u64>) -> usize {+ let ceiling = total_memory+ .map(|memory| usize::try_from(memory / VIDEO_BUDGET_MEMORY_DIVISOR).unwrap_or(usize::MAX))+ .unwrap_or(MAX_VIDEO_BUDGET_BYTES)+ .clamp(MIN_VIDEO_BUDGET_BYTES, MAX_VIDEO_BUDGET_BYTES);+ let bytes = (bits_per_second / 8).saturating_mul(VIDEO_BUFFER_WINDOW_SECS);+ usize::try_from(bytes)+ .unwrap_or(usize::MAX)+ .clamp(MIN_VIDEO_BUDGET_BYTES, ceiling)+}++fn total_memory_bytes() -> Option<u64> {+ let system = sysinfo::System::new_with_specifics(+ sysinfo::RefreshKind::new().with_memory(sysinfo::MemoryRefreshKind::new().with_ram()),+ );+ Some(system.total_memory()).filter(|memory| *memory > 0)+}++pub(crate) trait MuxTarget: Send + 'static {+ fn output(&self) -> &format::context::Output;++ fn output_mut(&mut self) -> &mut format::context::Output;++ fn writer_started(&mut self) {}++ fn packet_written(&mut self) {}+}++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(crate) struct StorageStalled;++impl From<StorageStalled> for std::io::Error {+ fn from(_: StorageStalled) -> Self {+ std::io::Error::new(+ std::io::ErrorKind::TimedOut,+ "output storage stalled while finishing; recording left for recovery",+ )+ }+}++impl From<StorageStalled> for ffmpeg::Error {+ fn from(_: StorageStalled) -> Self {+ ffmpeg::Error::Other {+ errno: ffmpeg::error::EIO,+ }+ }+}++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(crate) enum DrainOutcome {+ Complete,+ Truncated { packets: usize, bytes: usize },+ Stuck,+}++pub(crate) enum MuxSink<M: MuxTarget> {+ Direct(M),+ Behind(WriteBehind<M>),+ Detached,+}++impl<M: MuxTarget> MuxSink<M> {+ pub fn enable_write_behind(+ &mut self,+ label: &'static str,+ budget: usize,+ ) -> std::io::Result<()> {+ if !matches!(self, Self::Direct(_)) {+ return Ok(());+ }+ let Self::Direct(target) = std::mem::replace(self, Self::Detached) else {+ unreachable!("checked above");+ };+ match WriteBehind::spawn(label, target, budget, DrainLimits::DEFAULT) {+ Ok(writer) => {+ *self = Self::Behind(writer);+ Ok(())+ }+ Err((target, error)) => {+ *self = Self::Direct(target);+ Err(error)+ }+ }+ }++ pub fn is_write_behind(&self) -> bool {+ matches!(self, Self::Behind(_))+ }++ pub fn with_target<R>(&mut self, f: impl FnOnce(&mut M) -> R) -> R {+ match self {+ Self::Direct(target) => f(target),+ Self::Behind(writer) => f(&mut writer.target()),+ Self::Detached => unreachable!("mux sink detached"),+ }+ }++ pub fn with_target_ref<R>(&self, f: impl FnOnce(&M) -> R) -> R {+ match self {+ Self::Direct(target) => f(target),+ Self::Behind(writer) => f(&writer.target()),+ Self::Detached => unreachable!("mux sink detached"),+ }+ }++ pub fn frame_queued(&mut self) {+ if let Self::Direct(target) = self {+ target.packet_written();+ }+ }++ /// Marks the following packets as the end-of-stream flush: they bypass+ /// the budget and their write errors are reported by [`Self::close`],+ /// as an inline flush would report them.+ pub fn begin_finish(&mut self) {+ if let Self::Behind(writer) = self {+ writer.begin_finish();+ }+ }++ /// Drains the queued packets within the drain limits. An `Err` is the+ /// first write error among the end-of-stream packets.+ pub fn close(&mut self) -> Result<DrainOutcome, ffmpeg::Error> {+ match self {+ Self::Behind(writer) => writer.close(),+ Self::Direct(_) | Self::Detached => Ok(DrainOutcome::Complete),+ }+ }++ /// Runs the encoder's end-of-stream `flush` into this sink and drains+ /// the writer. `Err` means storage stalled: the output was abandoned and+ /// must not be finalized.+ pub fn finish_flush(+ &mut self,+ flush: impl FnOnce(&mut Self) -> Result<(), ffmpeg::Error>,+ ) -> Result<Result<(), ffmpeg::Error>, StorageStalled> {+ self.begin_finish();+ let flush_result = flush(self);+ match self.close() {+ Ok(DrainOutcome::Stuck) => Err(StorageStalled),+ Ok(DrainOutcome::Complete | DrainOutcome::Truncated { .. }) => Ok(flush_result),+ Err(error) => Ok(flush_result.and(Err(error))),+ }+ }++ #[cfg(test)]+ pub fn drain(&self) {+ if let Self::Behind(writer) = self {+ writer.drain();+ }+ }+}++impl<M: MuxTarget> PacketSink for MuxSink<M> {+ fn stream_time_base(&self, stream_index: usize) -> Rational {+ match self {+ Self::Direct(target) => target.output().stream_time_base(stream_index),+ Self::Behind(writer) => writer.time_bases[stream_index],+ Self::Detached => unreachable!("mux sink detached"),+ }+ }++ fn write_packet(&mut self, packet: Packet) -> Result<(), ffmpeg::Error> {+ match self {+ Self::Direct(target) => packet.write_interleaved(target.output_mut()),+ Self::Behind(writer) => writer.write_packet(packet),+ Self::Detached => unreachable!("mux sink detached"),+ }+ }+}++#[derive(Clone, Copy)]+struct DrainLimits {+ no_progress: Duration,+ total: Duration,+}++impl DrainLimits {+ const DEFAULT: Self = Self {+ no_progress: DRAIN_NO_PROGRESS_LIMIT,+ total: DRAIN_TOTAL_LIMIT,+ };+}++struct Queue {+ packets: VecDeque<(Packet, bool)>,+ bytes: usize,+ in_flight: bool,+ closed: bool,+ finishing: bool,+ exited: bool,+ completed_writes: u64,+ write_errors: u64,+ final_error: Option<ffmpeg::Error>,+}++struct Shared<M> {+ queue: Mutex<Queue>,+ changed: Condvar,+ abandoned: AtomicBool,+ target: Mutex<M>,+}++fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {+ mutex+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner())+}++impl<M> Shared<M> {+ fn wait<'a>(&self, queue: MutexGuard<'a, Queue>, timeout: Duration) -> MutexGuard<'a, Queue> {+ self.changed+ .wait_timeout(queue, timeout)+ .unwrap_or_else(|poisoned| poisoned.into_inner())+ .0+ }++ fn abandon(&self, queue: &mut Queue) -> (usize, usize) {+ self.abandoned.store(true, Ordering::Release);+ let packets = queue.packets.len();+ let bytes = queue+ .packets+ .drain(..)+ .map(|(packet, _)| packet.size())+ .sum::<usize>();+ queue.bytes -= bytes;+ self.changed.notify_all();+ (packets, bytes)+ }+}++pub(crate) struct WriteBehind<M: MuxTarget> {+ shared: Arc<Shared<M>>,+ time_bases: Vec<Rational>,+ budget: usize,+ label: &'static str,+ limits: DrainLimits,+ thread: Option<JoinHandle<()>>,+}++impl<M: MuxTarget> WriteBehind<M> {+ fn spawn(+ label: &'static str,+ target: M,+ budget: usize,+ limits: DrainLimits,+ ) -> Result<Self, (M, std::io::Error)> {+ let time_bases = target+ .output()+ .streams()+ .map(|stream| stream.time_base())+ .collect();+ let shared = Arc::new(Shared {+ queue: Mutex::new(Queue {+ packets: VecDeque::new(),+ bytes: 0,+ in_flight: false,+ closed: false,+ finishing: false,+ exited: false,+ completed_writes: 0,+ write_errors: 0,+ final_error: None,+ }),+ changed: Condvar::new(),+ abandoned: AtomicBool::new(false),+ target: Mutex::new(target),+ });+ let spawned = std::thread::Builder::new()+ .name(format!("mux-writer-{label}"))+ .spawn({+ let shared = shared.clone();+ move || run_writer(&shared, label)+ });+ let thread = match spawned {+ Ok(thread) => thread,+ Err(error) => {+ let target = Arc::try_unwrap(shared)+ .ok()+ .expect("writer thread never started")+ .target+ .into_inner()+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ return Err((target, error));+ }+ };+ Ok(Self {+ shared,+ time_bases,+ budget,+ label,+ limits,+ thread: Some(thread),+ })+ }++ fn target(&self) -> MutexGuard<'_, M> {+ lock(&self.shared.target)+ }++ fn begin_finish(&mut self) {+ lock(&self.shared.queue).finishing = true;+ }++ fn write_packet(&mut self, packet: Packet) -> Result<(), ffmpeg::Error> {+ let size = packet.size();+ let mut queue = lock(&self.shared.queue);+ let mut blocked_since = None;+ loop {+ if queue.exited {+ return Err(ffmpeg::Error::Bug);+ }+ if queue.finishing || queue.bytes == 0 || queue.bytes + size <= self.budget {+ break;+ }+ if blocked_since.is_none() {+ blocked_since = Some(Instant::now());+ tracing::warn!(+ writer = self.label,+ queued_bytes = queue.bytes,+ budget_bytes = self.budget,+ "Output write-behind budget exhausted; encoder now waits for storage"+ );+ }+ queue = self.shared.wait(queue, WAIT_POLL);+ }+ queue.bytes += size;+ let final_packet = queue.finishing;+ queue.packets.push_back((packet, final_packet));+ drop(queue);+ self.shared.changed.notify_all();++ if let Some(since) = blocked_since {+ tracing::warn!(+ writer = self.label,+ waited_ms = since.elapsed().as_millis() as u64,+ "Output write-behind budget recovered"+ );+ }++ Ok(())+ }++ #[cfg(test)]+ fn drain(&self) {+ let mut queue = lock(&self.shared.queue);+ while (!queue.packets.is_empty() || queue.in_flight) && !queue.exited {+ queue = self.shared.wait(queue, WAIT_POLL);+ }+ }++ fn close(&mut self) -> Result<DrainOutcome, ffmpeg::Error> {+ let Some(thread) = self.thread.take() else {+ return Ok(DrainOutcome::Complete);+ };+ let started = Instant::now();+ let mut last_progress = started;+ let mut queue = lock(&self.shared.queue);+ queue.closed = true;+ self.shared.changed.notify_all();+ let mut seen_writes = queue.completed_writes;+ let mut truncated = None;+ while !queue.exited {+ if queue.completed_writes != seen_writes {+ seen_writes = queue.completed_writes;+ last_progress = Instant::now();+ }+ if last_progress.elapsed() >= self.limits.no_progress {+ tracing::error!(+ writer = self.label,+ stalled_ms = last_progress.elapsed().as_millis() as u64,+ queued_packets = queue.packets.len(),+ queued_bytes = queue.bytes,+ "Output storage made no progress while finishing; leaving the recording for recovery and writing the buffer once storage recovers"+ );+ return Ok(DrainOutcome::Stuck);+ }+ if truncated.is_none() && started.elapsed() >= self.limits.total {+ let (packets, bytes) = self.shared.abandon(&mut queue);+ tracing::warn!(+ writer = self.label,+ discarded_packets = packets,+ discarded_bytes = bytes,+ "Output storage too slow to drain the write-behind buffer; finishing without its tail"+ );+ truncated = Some(DrainOutcome::Truncated { packets, bytes });+ }+ queue = self.shared.wait(queue, WAIT_POLL);+ }+ let final_error = queue.final_error.take();+ drop(queue);+ let panicked = thread.join().is_err();+ match final_error {+ Some(error) => Err(error),+ None if panicked => Err(ffmpeg::Error::Bug),+ None => Ok(truncated.unwrap_or(DrainOutcome::Complete)),+ }+ }+}++impl<M: MuxTarget> Drop for WriteBehind<M> {+ fn drop(&mut self) {+ let Some(thread) = self.thread.take() else {+ return;+ };+ let mut queue = lock(&self.shared.queue);+ queue.closed = true;+ self.shared.abandon(&mut queue);+ let deadline = Instant::now() + DROP_JOIN_LIMIT;+ while !queue.exited && Instant::now() < deadline {+ queue = self.shared.wait(queue, WAIT_POLL.min(DROP_JOIN_LIMIT));+ }+ let exited = queue.exited;+ drop(queue);+ if exited {+ let _ = thread.join();+ }+ }+}++struct WriterExit<'a, M>(&'a Shared<M>);++impl<M> Drop for WriterExit<'_, M> {+ fn drop(&mut self) {+ let mut queue = lock(&self.0.queue);+ queue.exited = true;+ queue.in_flight = false;+ drop(queue);+ self.0.changed.notify_all();+ }+}++fn run_writer<M: MuxTarget>(shared: &Shared<M>, label: &'static str) {+ let _exit = WriterExit(shared);+ {+ let mut target = lock(&shared.target);+ if !shared.abandoned.load(Ordering::Acquire) {+ target.writer_started();+ }+ }+ loop {+ let next = {+ let mut queue = lock(&shared.queue);+ loop {+ if shared.abandoned.load(Ordering::Acquire) {+ break None;+ }+ if let Some(next) = queue.packets.pop_front() {+ queue.in_flight = true;+ break Some(next);+ }+ if queue.closed {+ break None;+ }+ queue = shared+ .changed+ .wait(queue)+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ }+ };+ let Some((packet, final_packet)) = next else {+ return;+ };++ let size = packet.size();+ let started = Instant::now();+ let result = {+ let mut target = lock(&shared.target);+ if shared.abandoned.load(Ordering::Acquire) {+ None+ } else {+ let result = packet.write_interleaved(target.output_mut());+ if !shared.abandoned.load(Ordering::Acquire) {+ target.packet_written();+ }+ Some(result)+ }+ };+ let elapsed = started.elapsed();++ let (backlog, error_count) = {+ let mut queue = lock(&shared.queue);+ queue.bytes -= size;+ queue.in_flight = false;+ queue.completed_writes += 1;+ if let Some(Err(error)) = &result {+ queue.write_errors += 1;+ if final_packet && queue.final_error.is_none() {+ queue.final_error = Some(*error);+ }+ }+ (queue.bytes, queue.write_errors)+ };+ shared.changed.notify_all();++ if let Some(Err(error)) = result+ && (error_count == 1 || error_count.is_multiple_of(WRITE_ERROR_LOG_INTERVAL))+ {+ tracing::warn!(+ writer = label,+ %error,+ failed_writes = error_count,+ "Failed to write output packet"+ );+ }++ if elapsed >= STALL_LOG_THRESHOLD {+ tracing::warn!(+ writer = label,+ stalled_ms = elapsed.as_millis() as u64,+ backlog_bytes = backlog,+ "Output storage write stalled; capture continued into the write-behind buffer"+ );+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ struct NutTarget {+ output: format::context::Output,+ write_delay: Duration,+ panic_on_write: bool,+ }++ impl MuxTarget for NutTarget {+ fn output(&self) -> &format::context::Output {+ &self.output+ }++ fn output_mut(&mut self) -> &mut format::context::Output {+ assert!(!self.panic_on_write, "simulated writer failure");+ std::thread::sleep(self.write_delay);+ &mut self.output+ }+ }++ fn nut_target(path: &std::path::Path) -> NutTarget {+ ffmpeg::init().unwrap();+ let mut output = format::output_as(path, "nut").unwrap();+ let codec = ffmpeg::encoder::find(ffmpeg::codec::Id::PCM_S16LE).unwrap();+ let mut stream = output.add_stream(codec).unwrap();+ stream.set_time_base(Rational::new(1, 48_000));+ unsafe {+ let parameters = (*stream.as_mut_ptr()).codecpar;+ (*parameters).codec_type = ffmpeg::ffi::AVMediaType::AVMEDIA_TYPE_AUDIO;+ (*parameters).codec_id = ffmpeg::ffi::AVCodecID::AV_CODEC_ID_PCM_S16LE;+ (*parameters).sample_rate = 48_000;+ ffmpeg::ffi::av_channel_layout_default(&mut (*parameters).ch_layout, 1);+ }+ output.write_header().unwrap();+ NutTarget {+ output,+ write_delay: Duration::ZERO,+ panic_on_write: false,+ }+ }++ fn packet(pts: i64, size: usize) -> Packet {+ let mut packet = Packet::copy(&vec![0u8; size]);+ packet.set_stream(0);+ packet.set_pts(Some(pts));+ packet.set_dts(Some(pts));+ packet.set_duration(1024);+ packet+ }++ fn invalid_packet(pts: i64) -> Packet {+ let mut packet = packet(pts, 16);+ packet.set_stream(7);+ packet+ }++ fn behind(target: NutTarget, budget: usize, limits: DrainLimits) -> MuxSink<NutTarget> {+ MuxSink::Behind(+ WriteBehind::spawn("test", target, budget, limits)+ .ok()+ .unwrap(),+ )+ }++ fn shared(sink: &MuxSink<NutTarget>) -> Arc<Shared<NutTarget>> {+ match sink {+ MuxSink::Behind(writer) => writer.shared.clone(),+ _ => panic!("write-behind not enabled"),+ }+ }++ fn header_only_len(directory: &std::path::Path) -> u64 {+ let path = directory.join("header-only.nut");+ drop(nut_target(&path));+ std::fs::metadata(path).unwrap().len()+ }++ fn written_pts(path: &std::path::Path) -> Vec<i64> {+ let mut input = format::input(path).unwrap();+ input+ .packets()+ .filter_map(|(_, packet)| packet.pts())+ .collect()+ }++ #[test]+ fn producer_runs_ahead_of_a_stalled_writer_and_order_is_preserved() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("out.nut");+ let mut sink = MuxSink::Direct(nut_target(&path));+ sink.enable_write_behind("test", 1 << 20).unwrap();+ assert!(sink.is_write_behind());++ let shared = shared(&sink);+ let stalled = lock(&shared.target);+ let started = Instant::now();+ for pts in 0..64 {+ sink.write_packet(packet(pts * 1024, 2048)).unwrap();+ }+ assert!(started.elapsed() < Duration::from_secs(1));+ drop(stalled);++ assert_eq!(sink.close(), Ok(DrainOutcome::Complete));+ sink.with_target(|target| target.output.write_trailer().unwrap());+ assert_eq!(+ written_pts(&path),+ (0..64).map(|pts| pts * 1024).collect::<Vec<_>>()+ );+ }++ #[test]+ fn exhausted_budget_falls_back_to_waiting_for_storage() {+ let directory = tempfile::tempdir().unwrap();+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));+ sink.enable_write_behind("test", 8 * 1024).unwrap();+ let shared = shared(&sink);++ let stalled = lock(&shared.target);+ for pts in 0..4 {+ sink.write_packet(packet(pts * 1024, 2048)).unwrap();+ }+ let producer = std::thread::spawn(move || {+ let started = Instant::now();+ sink.write_packet(packet(4 * 1024, 2048)).unwrap();+ (sink, started.elapsed())+ });+ std::thread::sleep(Duration::from_millis(300));+ drop(stalled);+ let (mut sink, waited) = producer.join().unwrap();+ assert!(waited >= Duration::from_millis(250));+ assert_eq!(sink.close(), Ok(DrainOutcome::Complete));+ }++ #[test]+ fn oversized_packet_is_accepted_into_an_empty_queue() {+ let directory = tempfile::tempdir().unwrap();+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));+ sink.enable_write_behind("test", 1024).unwrap();+ sink.write_packet(packet(0, 4096)).unwrap();+ assert_eq!(sink.close(), Ok(DrainOutcome::Complete));+ }++ #[test]+ fn earlier_write_errors_never_abort_the_end_of_stream_flush() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("out.nut");+ let mut sink = MuxSink::Direct(nut_target(&path));+ sink.enable_write_behind("test", 1 << 20).unwrap();++ sink.write_packet(packet(0, 64)).unwrap();+ sink.write_packet(invalid_packet(1024)).unwrap();+ sink.drain();+ sink.write_packet(packet(2048, 64)).unwrap();+ sink.begin_finish();+ sink.write_packet(packet(3072, 64)).unwrap();++ assert_eq!(sink.close(), Ok(DrainOutcome::Complete));+ sink.with_target(|target| target.output.write_trailer().unwrap());+ assert_eq!(written_pts(&path), vec![0, 2048, 3072]);+ }++ #[test]+ fn end_of_stream_write_errors_are_reported_like_an_inline_flush() {+ let directory = tempfile::tempdir().unwrap();+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));+ sink.enable_write_behind("test", 1 << 20).unwrap();+ sink.write_packet(packet(0, 64)).unwrap();+ sink.begin_finish();+ sink.write_packet(invalid_packet(1024)).unwrap();+ assert!(sink.close().is_err());+ }++ #[test]+ fn finish_returns_on_stuck_storage_and_keeps_the_buffer_for_when_it_recovers() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("out.nut");+ let packets = 8usize;+ let packet_size = 64 * 1024;+ {+ let target = nut_target(&path);+ let limits = DrainLimits {+ no_progress: Duration::from_millis(200),+ total: Duration::from_secs(30),+ };+ let mut sink = behind(target, 1 << 20, limits);+ let shared = shared(&sink);+ let stalled = lock(&shared.target);+ for pts in 0..packets {+ sink.write_packet(packet(pts as i64 * 1024, packet_size))+ .unwrap();+ }+ let started = Instant::now();+ assert_eq!(sink.close(), Ok(DrainOutcome::Stuck));+ assert!(started.elapsed() < Duration::from_secs(2));+ drop(sink);+ drop(stalled);+ let deadline = Instant::now() + Duration::from_secs(2);+ while Arc::strong_count(&shared) > 1 && Instant::now() < deadline {+ std::thread::sleep(Duration::from_millis(10));+ }+ assert_eq!(Arc::strong_count(&shared), 1);+ }+ assert!(+ std::fs::metadata(&path).unwrap().len()+ >= header_only_len(directory.path()) + (packets * packet_size) as u64+ );+ }++ #[test]+ fn slow_storage_drains_within_the_total_limit_then_finishes_without_the_tail() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("out.nut");+ let mut target = nut_target(&path);+ target.write_delay = Duration::from_millis(60);+ let limits = DrainLimits {+ no_progress: Duration::from_secs(2),+ total: Duration::from_millis(300),+ };+ let mut sink = behind(target, 1 << 20, limits);+ for pts in 0..40 {+ sink.write_packet(packet(pts * 1024, 64)).unwrap();+ }+ let started = Instant::now();+ let outcome = sink.close().unwrap();+ assert!(started.elapsed() < Duration::from_secs(2));+ let DrainOutcome::Truncated { packets, .. } = outcome else {+ panic!("expected a truncated drain, got {outcome:?}");+ };+ assert!(packets > 0);+ sink.with_target(|target| {+ target.write_delay = Duration::ZERO;+ target.output.write_trailer().unwrap();+ });+ let pts = written_pts(&path);+ assert_eq!(pts.len() + packets, 40);+ assert_eq!(+ pts,+ (0..pts.len() as i64)+ .map(|pts| pts * 1024)+ .collect::<Vec<_>>()+ );+ }++ #[test]+ fn dropping_without_finish_stops_the_writer_promptly() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("out.nut");+ let shared;+ {+ let mut sink = MuxSink::Direct(nut_target(&path));+ sink.enable_write_behind("test", 1 << 20).unwrap();+ shared = self::shared(&sink);+ let stalled = lock(&shared.target);+ for pts in 0..16 {+ sink.write_packet(packet(pts * 1024, 64 * 1024)).unwrap();+ }+ let dropping = std::thread::spawn(move || {+ let started = Instant::now();+ drop(sink);+ started.elapsed()+ });+ std::thread::sleep(Duration::from_millis(50));+ let drop_time = dropping.join().unwrap();+ assert!(drop_time < Duration::from_millis(600));+ drop(stalled);+ }+ let deadline = Instant::now() + Duration::from_secs(2);+ while Arc::strong_count(&shared) > 1 && Instant::now() < deadline {+ std::thread::sleep(Duration::from_millis(10));+ }+ assert_eq!(Arc::strong_count(&shared), 1);+ drop(shared);+ assert_eq!(+ std::fs::metadata(&path).unwrap().len(),+ header_only_len(directory.path())+ );+ }++ #[test]+ fn idle_writer_drop_joins_the_thread() {+ let directory = tempfile::tempdir().unwrap();+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));+ sink.enable_write_behind("test", 1 << 20).unwrap();+ let shared = shared(&sink);+ drop(sink);+ assert_eq!(Arc::strong_count(&shared), 1);+ }++ #[test]+ fn writer_panic_is_reported_on_the_next_enqueue() {+ let directory = tempfile::tempdir().unwrap();+ let mut target = nut_target(&directory.path().join("out.nut"));+ target.panic_on_write = true;+ let mut sink = behind(target, 1 << 20, DrainLimits::DEFAULT);+ sink.write_packet(packet(0, 64)).unwrap();+ let shared = shared(&sink);+ let deadline = Instant::now() + Duration::from_secs(2);+ while !lock(&shared.queue).exited && Instant::now() < deadline {+ std::thread::sleep(Duration::from_millis(10));+ }+ assert_eq!(sink.write_packet(packet(1024, 64)), Err(ffmpeg::Error::Bug));+ assert_eq!(sink.close(), Err(ffmpeg::Error::Bug));+ }++ #[test]+ fn video_budget_scales_with_bitrate_and_machine_memory() {+ const GIB: u64 = 1024 * 1024 * 1024;+ assert_eq!(video_budget_for(0, Some(16 * GIB)), MIN_VIDEO_BUDGET_BYTES);+ assert_eq!(video_budget_for(20_000_000, Some(16 * GIB)), 100_000_000);+ assert_eq!(+ video_budget_for(u64::MAX, Some(16 * GIB)),+ MAX_VIDEO_BUDGET_BYTES+ );+ assert_eq!(video_budget_for(u64::MAX, Some(8 * GIB)), 256 * 1024 * 1024);+ assert_eq!(+ video_budget_for(u64::MAX, Some(GIB)),+ MIN_VIDEO_BUDGET_BYTES+ );+ assert_eq!(video_budget_for(u64::MAX, None), MAX_VIDEO_BUDGET_BYTES);+ }+}