handle relevant keys for metadata struct

This commit is contained in:
Muaz Ahmad 2023-10-06 12:09:43 +05:00
parent d3e291d676
commit 5b57ffb3cb
2 changed files with 23 additions and 3 deletions

View file

@ -96,7 +96,7 @@ fn process_metadata(mut data: Vec<u8>) -> Result<util::Metadata, Box<dyn Error>>
for _ in 0..arr_len { for _ in 0..arr_len {
let key = String::fetch_amf_val(&mut data); let key = String::fetch_amf_val(&mut data);
let amf_marker = u8::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) { if !handle_relevant_keys(key, &mut data, &amf_marker, &mut metadata)? {
match amf_marker { match amf_marker {
0 => {let _ = f64::fetch_amf_val(&mut data);}, 0 => {let _ = f64::fetch_amf_val(&mut data);},
1 => {let _ = bool::fetch_amf_val(&mut data);}, 1 => {let _ = bool::fetch_amf_val(&mut data);},
@ -108,8 +108,26 @@ fn process_metadata(mut data: Vec<u8>) -> Result<util::Metadata, Box<dyn Error>>
return Ok(metadata); return Ok(metadata);
} }
fn handle_relevant_keys(key: String, data: &mut Vec<u8>, marker: &u8, metadata: &mut util::Metadata) -> bool { fn handle_relevant_keys(key: String, data: &mut Vec<u8>, marker: &u8, metadata: &mut util::Metadata) -> Result<bool, Box<dyn Error>> {
todo!(); match key.as_str() {
"width" => {metadata.video.width = f64::fetch_amf_val(data) as u32; Ok(true)},
"height" => {metadata.video.height = f64::fetch_amf_val(data) as u32; Ok(true)},
"framerate" => {metadata.video.framerate = f64::fetch_amf_val(data) as f32; Ok(true)},
"videocodecid" => {
match f64::fetch_amf_val(data) as u8 {
7 => {metadata.video.codec = Some(util::VideoCodec::H264); Ok(true)},
_ => {Err(Box::new(util::DemuxerError::CodecNotImplemented))}
}
},
"stereo" => {metadata.audio.channels = if bool::fetch_amf_val(data) {2} else {1}; Ok(true)},
"audiosamplerate" => {metadata.audio.samplerate = f64::fetch_amf_val(data) as u32; Ok(true)},
"audiocodecid" => {
match f64::fetch_amf_val(data) as u8 {
10 => {metadata.audio.codec = Some(util::AudioCodec::AAC); Ok(true)},
_ => {Err(Box::new(util::DemuxerError::CodecNotImplemented))}
}
}
}
} }
trait FetchAMFVal { trait FetchAMFVal {

View file

@ -66,6 +66,7 @@ pub enum DemuxerError {
FLVPacketNotMetadata, FLVPacketNotMetadata,
FLVMetadataSyncFail, FLVMetadataSyncFail,
FLVMetadataAMFMarkerError, FLVMetadataAMFMarkerError,
CodecNotImplemented,
} }
impl Error for DemuxerError {} impl Error for DemuxerError {}
@ -79,6 +80,7 @@ impl fmt::Debug for DemuxerError {
DemuxerError::FLVPacketNotMetadata => write!(f, "Did not find expected metadata tag"), DemuxerError::FLVPacketNotMetadata => write!(f, "Did not find expected metadata tag"),
DemuxerError::FLVMetadataSyncFail => write!(f, "ECMA array not where expected"), DemuxerError::FLVMetadataSyncFail => write!(f, "ECMA array not where expected"),
DemuxerError::FLVMetadataAMFMarkerError => write!(f, "Marker value not recognized/implemented"), DemuxerError::FLVMetadataAMFMarkerError => write!(f, "Marker value not recognized/implemented"),
DemuxerError::CodecNotImplemented => write!(f, "Input file has unrecognized codec"),
_ => write!(f, "Error not described yet") _ => write!(f, "Error not described yet")
} }
} }