diff --git a/src/demux/flv.rs b/src/demux/flv.rs index 6927311..ffb8dae 100644 --- a/src/demux/flv.rs +++ b/src/demux/flv.rs @@ -86,12 +86,29 @@ pub fn new_reader(stdin: io::Stdin) -> Result> { } fn process_metadata(mut data: Vec) -> Result> { + 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, 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) -> Self::Primitive { + return data.drain(..1).next().unwrap(); + } +} + impl FetchAMFVal for f64 { type Primitive = f64; fn fetch_amf_val(data: &mut Vec) -> Self::Primitive { diff --git a/src/util/mod.rs b/src/util/mod.rs index 3d940a8..6801cf9 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -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") } }