basic flv packet reading

This commit is contained in:
Muaz Ahmad 2023-10-05 15:56:23 +05:00
parent 82deae62f5
commit f9c59a90de

View file

@ -5,10 +5,43 @@ use std::io::Read;
use crate::util;
use crate::demux::input;
enum FLVTagType {
Metadata,
Audio,
Video,
}
impl TryFrom<u8> for FLVTagType {
type Error = util::GenericError;
fn try_from(type_byte: u8) -> Result<Self, Self::Error> {
match type_byte {
8 => Ok(FLVTagType::Audio),
9 => Ok(FLVTagType::Video),
18 => Ok(FLVTagType::Metadata),
_ => Err(Self::Error::FLVUnknownTagType)
}
}
}
pub struct FLVReader {
stdin: io::Stdin
}
impl FLVReader {
fn read_packet(&mut self) -> Result<(FLVTagType, Vec<u8>), Box<dyn Error>> {
let mut tag_head = [0u8; 11];
self.stdin.read_exact(&mut tag_head)?;
let tag_type = FLVTagType::try_from(tag_head[0])?;
let tag_data_len = (u32::from_be_bytes(tag_head[..4].try_into()?) & 0xffffff) as usize;
let mut packet_data = vec![0u8; tag_data_len];
self.stdin.read_exact(packet_data.as_mut_slice())?;
let mut packet_foot = [0u8; 4];
self.stdin.read_exact(&mut packet_foot)?;
return Ok((tag_type, packet_data));
}
}
impl input::FileReader for FLVReader {
fn init(&mut self) -> Result<util::Metadata, Box<dyn Error>> {
todo!();