metadata processing

This commit is contained in:
Muaz Ahmad 2023-10-06 11:58:45 +05:00
parent f1300de5a0
commit d3e291d676
2 changed files with 30 additions and 1 deletions

View file

@ -86,12 +86,29 @@ pub fn new_reader(stdin: io::Stdin) -> Result<FLVReader, Box<dyn Error>> {
}
fn process_metadata(mut data: Vec<u8>) -> Result<util::Metadata, Box<dyn Error>> {
let mut metadata = util::Metadata { ..Default::default() };
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);
let arr_len = u32::fetch_amf_val(&mut data) as usize;
for _ in 0..arr_len {
let key = String::fetch_amf_val(&mut data);
let amf_marker = u8::fetch_amf_val(&mut data);
if !handle_relevant_keys(key, &mut data, &amf_marker, &mut metadata) {
match amf_marker {
0 => {let _ = f64::fetch_amf_val(&mut data);},
1 => {let _ = bool::fetch_amf_val(&mut data);},
2 => {let _ = String::fetch_amf_val(&mut data);},
_ => {return Err(Box::new(util::DemuxerError::FLVMetadataAMFMarkerError));}
}
}
}
return Ok(metadata);
}
fn handle_relevant_keys(key: String, data: &mut Vec<u8>, marker: &u8, metadata: &mut util::Metadata) -> bool {
todo!();
}
@ -118,6 +135,13 @@ impl FetchAMFVal for u16 {
}
}
impl FetchAMFVal for u8 {
type Primitive = u8;
fn fetch_amf_val(data: &mut Vec<u8>) -> Self::Primitive {
return data.drain(..1).next().unwrap();
}
}
impl FetchAMFVal for f64 {
type Primitive = f64;
fn fetch_amf_val(data: &mut Vec<u8>) -> Self::Primitive {

View file

@ -3,6 +3,7 @@ use std::fmt;
// Data structs/enums
#[derive(Default)]
pub struct VideoMetadata {
pub width: u32,
pub height: u32,
@ -14,6 +15,7 @@ pub enum VideoCodec {
H264,
}
#[derive(Default)]
pub struct AudioMetadata {
pub samplerate: u32,
pub channels: u8,
@ -24,6 +26,7 @@ pub enum AudioCodec {
AAC,
}
#[derive(Default)]
pub struct Metadata {
pub video: VideoMetadata,
pub audio: AudioMetadata,
@ -62,6 +65,7 @@ pub enum DemuxerError {
FLVCheckFail,
FLVPacketNotMetadata,
FLVMetadataSyncFail,
FLVMetadataAMFMarkerError,
}
impl Error for DemuxerError {}
@ -74,6 +78,7 @@ impl fmt::Debug for DemuxerError {
DemuxerError::FLVCheckFail => write!(f, "Input does not contain assumed FLV signature"),
DemuxerError::FLVPacketNotMetadata => write!(f, "Did not find expected metadata tag"),
DemuxerError::FLVMetadataSyncFail => write!(f, "ECMA array not where expected"),
DemuxerError::FLVMetadataAMFMarkerError => write!(f, "Marker value not recognized/implemented"),
_ => write!(f, "Error not described yet")
}
}