diff --git a/src/demux/flv.rs b/src/demux/flv.rs index 612d5e0..f62b45b 100644 --- a/src/demux/flv.rs +++ b/src/demux/flv.rs @@ -5,10 +5,43 @@ use std::io::Read; use crate::util; use crate::demux::input; +enum FLVTagType { + Metadata, + Audio, + Video, +} + +impl TryFrom for FLVTagType { + type Error = util::GenericError; + fn try_from(type_byte: u8) -> Result { + match type_byte { + 8 => Ok(FLVTagType::Audio), + 9 => Ok(FLVTagType::Video), + 18 => Ok(FLVTagType::Metadata), + _ => Err(Self::Error::FLVUnknownTagType) + } + } +} + pub struct FLVReader { stdin: io::Stdin } +impl FLVReader { + fn read_packet(&mut self) -> Result<(FLVTagType, Vec), Box> { + let mut tag_head = [0u8; 11]; + self.stdin.read_exact(&mut tag_head)?; + let tag_type = FLVTagType::try_from(tag_head[0])?; + let tag_data_len = (u32::from_be_bytes(tag_head[..4].try_into()?) & 0xffffff) as usize; + let mut packet_data = vec![0u8; tag_data_len]; + self.stdin.read_exact(packet_data.as_mut_slice())?; + let mut packet_foot = [0u8; 4]; + self.stdin.read_exact(&mut packet_foot)?; + + return Ok((tag_type, packet_data)); + } +} + impl input::FileReader for FLVReader { fn init(&mut self) -> Result> { todo!();