diff --git a/crates/video-streamer/src/streamer/iter.rs b/crates/video-streamer/src/streamer/iter.rs index e9cd4e20..7115fd13 100644 --- a/crates/video-streamer/src/streamer/iter.rs +++ b/crates/video-streamer/src/streamer/iter.rs @@ -207,7 +207,7 @@ where let mut file = inner.into_inner(); file.reopen()?; file.seek(std::io::SeekFrom::Start(self.last_tag_position as u64))?; - self.inner = Some(WebmIterator::new(file, &[MatroskaSpec::BlockGroup(Master::Start)])); + self.new_inner(file); self.rollback_record = Some(self.last_tag_position); if self @@ -248,7 +248,7 @@ where file.seek(std::io::SeekFrom::Start(last_key_frame_position as u64))?; self.rollback_record = Some(last_key_frame_position); self.last_tag_position = last_key_frame_position; - self.inner = Some(WebmIterator::new(file, &[MatroskaSpec::BlockGroup(Master::Start)])); + self.new_inner(file); self.last_cluster_position = Some(cluster_start_position); Ok(self.last_key_frame_info) } @@ -315,6 +315,12 @@ where _ => Ok(false), } } + + fn new_inner(&mut self, reader: R) { + let mut inner = WebmIterator::new(reader, &[MatroskaSpec::BlockGroup(Master::Start)]); + inner.emit_master_end_when_eof(false); + self.inner = Some(inner); + } } pub(crate) fn read_vint(buffer: &[u8]) -> anyhow::Result> { diff --git a/crates/video-streamer/src/streamer/mod.rs b/crates/video-streamer/src/streamer/mod.rs index 16de344a..4013d459 100644 --- a/crates/video-streamer/src/streamer/mod.rs +++ b/crates/video-streamer/src/streamer/mod.rs @@ -82,8 +82,8 @@ pub fn webm_stream( header_writer.write(header)?; } - let mut encode_writer = header_writer.into_encoded_writer(encode_writer_config)?; - + let (mut encode_writer, cut_block_hit_marker) = header_writer.into_encoded_writer(encode_writer_config)?; + let mut cut_block_hit_marker = Some(cut_block_hit_marker); // Start muxing from the last key frame. // The WebM project requires the muxer to ensure the first Block/SimpleBlock is a keyframe. // However, the WebM file emitted by the CaptureStream API in Chrome does not adhere to this requirement. @@ -119,7 +119,9 @@ pub fn webm_stream( } Some(Ok(tag)) => { if webm_itr.last_tag_position() == cut_block_position { - encode_writer.mark_cut_block_hit(); + if let Some(cut_block_hit_marker) = cut_block_hit_marker.take() { + encode_writer.mark_cut_block_hit(cut_block_hit_marker); + } } match encode_writer.write(tag) { diff --git a/crates/video-streamer/src/streamer/tag_writers.rs b/crates/video-streamer/src/streamer/tag_writers.rs index da5eae81..bf4e8360 100644 --- a/crates/video-streamer/src/streamer/tag_writers.rs +++ b/crates/video-streamer/src/streamer/tag_writers.rs @@ -47,8 +47,11 @@ where Ok(()) } - pub(crate) fn into_encoded_writer(self, config: EncodeWriterConfig) -> anyhow::Result> { - let encoded_writer = CutCusterWriter::new(config, self)?; + pub(crate) fn into_encoded_writer( + self, + config: EncodeWriterConfig, + ) -> anyhow::Result<(CutClusterWriter, CutBlockHitMarker)> { + let encoded_writer = CutClusterWriter::new(config, self)?; Ok(encoded_writer) } } @@ -82,7 +85,7 @@ enum CutBlockState { }, } -pub(crate) struct CutCusterWriter +pub(crate) struct CutClusterWriter where T: std::io::Write, { @@ -94,11 +97,13 @@ where cut_block_state: CutBlockState, } -impl CutCusterWriter +pub(crate) struct CutBlockHitMarker; + +impl CutClusterWriter where T: std::io::Write, { - fn new(config: EncodeWriterConfig, writer: HeaderWriter) -> anyhow::Result { + fn new(config: EncodeWriterConfig, writer: HeaderWriter) -> anyhow::Result<(Self, CutBlockHitMarker)> { let decoder = VpxDecoder::builder() .threads(config.threads) .width(config.width) @@ -117,13 +122,16 @@ where .build()?; let HeaderWriter { writer } = writer; - Ok(Self { - writer, - cluster_timestamp: None, - encoder, - decoder, - cut_block_state: CutBlockState::HaventMet, - }) + Ok(( + Self { + writer, + cluster_timestamp: None, + encoder, + decoder, + cut_block_state: CutBlockState::HaventMet, + }, + CutBlockHitMarker, + )) } } @@ -131,7 +139,7 @@ pub(crate) enum WriterResult { Continue, } -impl CutCusterWriter +impl CutClusterWriter where T: std::io::Write, { @@ -296,7 +304,7 @@ where false } - pub(crate) fn mark_cut_block_hit(&mut self) { + pub(crate) fn mark_cut_block_hit(&mut self, _marker: CutBlockHitMarker) { self.cut_block_state = CutBlockState::AtCutBlock; } }