Skip to content

Commit 5982627

Browse files
committed
Refactor DASH muxer init segment validation logic
1 parent d3d4c50 commit 5982627

1 file changed

Lines changed: 24 additions & 70 deletions

File tree

crates/enc-ffmpeg/src/mux/segmented_stream.rs

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ impl SegmentedVideoEncoder {
182182

183183
let manifest_path = base_path.join("dash_manifest.mpd");
184184

185-
let mut output = format::output_as(&manifest_path, "dash")?;
185+
#[cfg(target_os = "windows")]
186+
let manifest_path_str = manifest_path.to_string_lossy().replace('\\', "/");
187+
#[cfg(not(target_os = "windows"))]
188+
let manifest_path_str = manifest_path.to_string_lossy().to_string();
189+
190+
let mut output = format::output_as(&manifest_path_str, "dash")?;
186191

187192
let init_seg_str = INIT_SEGMENT_NAME;
188193
let media_seg_str = "segment_$Number%03d$.m4s";
@@ -219,6 +224,17 @@ impl SegmentedVideoEncoder {
219224

220225
output.write_header()?;
221226

227+
let init_path = base_path.join(INIT_SEGMENT_NAME);
228+
let manifest_exists = manifest_path.exists();
229+
let init_exists = init_path.exists();
230+
tracing::debug!(
231+
manifest_path = %manifest_path.display(),
232+
manifest_exists = manifest_exists,
233+
init_path = %init_path.display(),
234+
init_exists = init_exists,
235+
"FFmpeg DASH muxer state after write_header()"
236+
);
237+
222238
let codec_info = CodecInfo {
223239
width: video_config.width,
224240
height: video_config.height,
@@ -329,11 +345,6 @@ impl SegmentedVideoEncoder {
329345
.queue_frame(frame, timestamp, &mut self.output)?;
330346
self.frames_in_segment += 1;
331347

332-
if !self.init_segment_validated {
333-
self.validate_init_segment_early()?;
334-
self.init_segment_validated = true;
335-
}
336-
337348
let new_segment_index = self.detect_current_segment_index();
338349

339350
if new_segment_index > prev_segment_index {
@@ -390,8 +401,13 @@ impl SegmentedVideoEncoder {
390401
self.segment_start_time = Some(timestamp);
391402
self.frames_in_segment = 0;
392403

393-
self.validate_init_segment()
394-
.map_err(QueueFrameError::InitSegmentInvalid)?;
404+
if !self.init_segment_validated && self.init_segment_path().exists() {
405+
self.init_segment_validated = true;
406+
tracing::debug!(
407+
segment = completed_index,
408+
"init.mp4 now exists after segment completion"
409+
);
410+
}
395411

396412
self.write_manifest();
397413
self.write_in_progress_manifest();
@@ -748,66 +764,4 @@ impl SegmentedVideoEncoder {
748764
)),
749765
}
750766
}
751-
752-
fn validate_init_segment_early(&self) -> Result<(), QueueFrameError> {
753-
let init_path = self.init_segment_path();
754-
755-
if !init_path.exists() {
756-
return Err(QueueFrameError::InitSegmentInvalid(format!(
757-
"init.mp4 missing at {} after first frame. Segments will be unplayable!",
758-
init_path.display()
759-
)));
760-
}
761-
762-
let metadata = std::fs::metadata(&init_path).map_err(|e| {
763-
QueueFrameError::InitSegmentInvalid(format!(
764-
"Cannot read init.mp4 metadata at {}: {}",
765-
init_path.display(),
766-
e
767-
))
768-
})?;
769-
770-
let size = metadata.len();
771-
if size < 8 {
772-
return Err(QueueFrameError::InitSegmentInvalid(format!(
773-
"init.mp4 at {} is too small ({} bytes) to contain valid ftyp box",
774-
init_path.display(),
775-
size
776-
)));
777-
}
778-
779-
let mut file = std::fs::File::open(&init_path).map_err(|e| {
780-
QueueFrameError::InitSegmentInvalid(format!(
781-
"Cannot open init.mp4 at {}: {}",
782-
init_path.display(),
783-
e
784-
))
785-
})?;
786-
787-
let mut header = [0u8; 8];
788-
std::io::Read::read_exact(&mut file, &mut header).map_err(|e| {
789-
QueueFrameError::InitSegmentInvalid(format!(
790-
"Cannot read init.mp4 header at {}: {}",
791-
init_path.display(),
792-
e
793-
))
794-
})?;
795-
796-
let ftyp_signature = &header[4..8];
797-
if ftyp_signature != b"ftyp" {
798-
return Err(QueueFrameError::InitSegmentInvalid(format!(
799-
"init.mp4 at {} has invalid ftyp box (got {:?}, expected 'ftyp'). File may be corrupted!",
800-
init_path.display(),
801-
String::from_utf8_lossy(ftyp_signature)
802-
)));
803-
}
804-
805-
tracing::debug!(
806-
path = %init_path.display(),
807-
size_bytes = size,
808-
"init.mp4 validated successfully with valid ftyp box"
809-
);
810-
811-
Ok(())
812-
}
813767
}

0 commit comments

Comments
 (0)