From f9c59a90deefb74848be639d8ed508a17a574e59 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Thu, 5 Oct 2023 15:56:23 +0500 Subject: [PATCH] basic flv packet reading --- src/demux/flv.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/demux/flv.rs b/src/demux/flv.rs index 612d5e0..f62b45b 100644 --- a/src/demux/flv.rs +++ b/src/demux/flv.rs @@ -5,10 +5,43 @@ use std::io::Read; use crate::util; use crate::demux::input; +enum FLVTagType { + Metadata, + Audio, + Video, +} + +impl TryFrom for FLVTagType { + type Error = util::GenericError; + fn try_from(type_byte: u8) -> Result { + 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), Box> { + 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> { todo!();