From f1300de5a01e7ac9aebcdd0ac5b844c2cd958d7c Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Fri, 6 Oct 2023 11:44:47 +0500 Subject: [PATCH] helper functions to fetch amf values --- src/demux/flv.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/demux/flv.rs b/src/demux/flv.rs index 5516c72..6927311 100644 --- a/src/demux/flv.rs +++ b/src/demux/flv.rs @@ -71,7 +71,7 @@ impl FLVReader { impl input::FileReader for FLVReader { fn init(&mut self) -> Result> { - self.skip_init_header(); + self.skip_init_header()?; return self.read_metadata() } fn read_nalu(&mut self) -> Result> { @@ -85,6 +85,64 @@ pub fn new_reader(stdin: io::Stdin) -> Result> { return Ok(reader); } -fn process_metadata(data: Vec) -> Result> { +fn process_metadata(mut data: Vec) -> Result> { + data.drain(..13); + let ecma_sync_byte = data.drain(..1).next().unwrap(); + if ecma_sync_byte != 8 { + return Err(Box::new(util::DemuxerError::FLVMetadataSyncFail)); + } + let arr_len = u32::fetch_amf_val(&mut data); todo!(); } + +trait FetchAMFVal { + type Primitive; + fn fetch_amf_val(data: &mut Vec) -> Self::Primitive; +} + +impl FetchAMFVal for u32 { + type Primitive = u32; + fn fetch_amf_val(data: &mut Vec) -> Self::Primitive { + let mut buff = [0u8; 4]; + buff.copy_from_slice(data.drain(..4).as_slice()); + return u32::from_be_bytes(buff); + } +} + +impl FetchAMFVal for u16 { + type Primitive = u16; + fn fetch_amf_val(data: &mut Vec) -> Self::Primitive { + let mut buff = [0u8; 2]; + buff.copy_from_slice(data.drain(..2).as_slice()); + return u16::from_be_bytes(buff); + } +} + +impl FetchAMFVal for f64 { + type Primitive = f64; + fn fetch_amf_val(data: &mut Vec) -> Self::Primitive { + let mut buff = [0u8; 8]; + buff.copy_from_slice(data.drain(..8).as_slice()); + return f64::from_be_bytes(buff); + } +} + +impl FetchAMFVal for bool { + type Primitive = bool; + fn fetch_amf_val(data: &mut Vec) -> Self::Primitive { + let mut buff = [0u8; 1]; + buff.copy_from_slice(data.drain(..1).as_slice()); + return !(buff[0] == 0) + } +} + +impl FetchAMFVal for String { + type Primitive = String; + fn fetch_amf_val(data: &mut Vec) -> Self::Primitive { + let mut len_buff = [0u8; 2]; + len_buff.copy_from_slice(data.drain(..2).as_slice()); + let str_len = u16::from_be_bytes(len_buff) as usize; + let str_buff = data.drain(..str_len).collect::>(); + return String::from_utf8(str_buff).unwrap(); + } +}