From 526a4d60685ad1e00904c7a81c57cb2ef5970640 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Thu, 5 Oct 2023 16:30:07 +0500 Subject: [PATCH] some basic implementations, file header skip, metadata read --- src/demux/flv.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/demux/flv.rs b/src/demux/flv.rs index f62b45b..5516c72 100644 --- a/src/demux/flv.rs +++ b/src/demux/flv.rs @@ -12,7 +12,7 @@ enum FLVTagType { } impl TryFrom for FLVTagType { - type Error = util::GenericError; + type Error = util::DemuxerError; fn try_from(type_byte: u8) -> Result { match type_byte { 8 => Ok(FLVTagType::Audio), @@ -28,6 +28,24 @@ pub struct FLVReader { } impl FLVReader { + fn check_signature(&mut self) -> Result<(), Box> { + let mut sig_bytes_rem = [0u8; 3]; + self.stdin.read_exact(&mut sig_bytes_rem)?; + if String::from_utf8(sig_bytes_rem[..2].to_vec())? != "LV" || sig_bytes_rem[2] != 1 { + return Err(Box::new(util::DemuxerError::FLVCheckFail)) + } + Ok(()) + } + + fn skip_init_header(&mut self) -> Result<(), Box> { + let mut rest_of_header = [0u8; 5]; + self.stdin.read_exact(&mut rest_of_header)?; + let head_offset = u32::from_be_bytes(rest_of_header[1..].try_into()?) as usize + 4; + let mut skip_header_extra = vec![0u8; head_offset]; + self.stdin.read_exact(skip_header_extra.as_mut_slice())?; + return Ok(()); + } + fn read_packet(&mut self) -> Result<(FLVTagType, Vec), Box> { let mut tag_head = [0u8; 11]; self.stdin.read_exact(&mut tag_head)?; @@ -40,11 +58,21 @@ impl FLVReader { return Ok((tag_type, packet_data)); } + + fn read_metadata(&mut self) -> Result> { + let (packet_type, packet_data) = self.read_packet()?; + match packet_type { + FLVTagType::Metadata => (), + x => return Err(Box::new(util::DemuxerError::FLVPacketNotMetadata)) + } + return process_metadata(packet_data); + } } impl input::FileReader for FLVReader { fn init(&mut self) -> Result> { - todo!(); + self.skip_init_header(); + return self.read_metadata() } fn read_nalu(&mut self) -> Result> { todo!(); @@ -52,5 +80,11 @@ impl input::FileReader for FLVReader { } pub fn new_reader(stdin: io::Stdin) -> Result> { + let mut reader = FLVReader { stdin: stdin }; + reader.check_signature()?; + return Ok(reader); +} + +fn process_metadata(data: Vec) -> Result> { todo!(); }