helper functions to fetch amf values

This commit is contained in:
Muaz Ahmad 2023-10-06 11:44:47 +05:00
parent 895ae2d4e1
commit f1300de5a0

View file

@ -71,7 +71,7 @@ impl FLVReader {
impl input::FileReader for FLVReader {
fn init(&mut self) -> Result<util::Metadata, Box<dyn Error>> {
self.skip_init_header();
self.skip_init_header()?;
return self.read_metadata()
}
fn read_nalu(&mut self) -> Result<util::NALUPacket, Box<dyn Error>> {
@ -85,6 +85,64 @@ pub fn new_reader(stdin: io::Stdin) -> Result<FLVReader, Box<dyn Error>> {
return Ok(reader);
}
fn process_metadata(data: Vec<u8>) -> Result<util::Metadata, Box<dyn Error>> {
fn process_metadata(mut data: Vec<u8>) -> Result<util::Metadata, Box<dyn Error>> {
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<u8>) -> Self::Primitive;
}
impl FetchAMFVal for u32 {
type Primitive = u32;
fn fetch_amf_val(data: &mut Vec<u8>) -> 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<u8>) -> 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<u8>) -> 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<u8>) -> 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<u8>) -> 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::<Vec<u8>>();
return String::from_utf8(str_buff).unwrap();
}
}