From f287cc2d64f234c252500f8d7393c9129dc1de92 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 17 Oct 2023 14:26:08 +0500 Subject: [PATCH] boilerplate for mp4 muxing --- src/encode/codecs/video.rs | 1 + src/muxer/hls/handler.rs | 18 ++++++++++++++++-- src/muxer/hls/mod.rs | 4 +++- src/muxer/hls/mp4/atoms.rs | 0 src/muxer/hls/mp4/mod.rs | 30 ++++++++++++++++++++++++++++++ src/muxer/hls/mp4/mp4muxer.rs | 20 ++++++++++++++++++++ src/util/mod.rs | 22 ++++++++++++++++++++++ 7 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/muxer/hls/mp4/atoms.rs create mode 100644 src/muxer/hls/mp4/mp4muxer.rs diff --git a/src/encode/codecs/video.rs b/src/encode/codecs/video.rs index c31081f..cdcf0f9 100644 --- a/src/encode/codecs/video.rs +++ b/src/encode/codecs/video.rs @@ -37,6 +37,7 @@ impl Encoder for AV1Encoder { let nalu_len = u32::from_le_bytes(buff[..4].try_into().unwrap()) as usize; let mut data = vec![0u8; nalu_len]; stdout.read_exact(data.as_mut_slice())?; + data.drain(..2); return Ok(util::NALUPacket {packet_type: util::NALUPacketType::Video(util::VideoCodec::AV1), packet_data: data}); } } diff --git a/src/muxer/hls/handler.rs b/src/muxer/hls/handler.rs index 337f968..d0094c4 100644 --- a/src/muxer/hls/handler.rs +++ b/src/muxer/hls/handler.rs @@ -1,7 +1,21 @@ -pub struct HLSHandler; +use std::error::Error; +use std::sync::{mpsc, Arc}; + +use crate::util; +use crate::muxer::hls::mp4; + +pub struct HLSHandler { + muxer: mp4::MP4Muxer, + err_in: mpsc::Sender>, + args: util::HLSArgs, +} impl HLSHandler { - pub fn data_loop(&mut self) { + pub fn data_loop(&self) { todo!(); } } + +pub fn spawn(v: mpsc::Receiver, a: mpsc::Receiver, err_in: mpsc::Sender>, metadata: Arc, args: util::HLSArgs) -> Result> { + Ok(HLSHandler {muxer: mp4::new_muxer(v, a, metadata, err_in.clone())?, err_in: err_in, args: args}) +} diff --git a/src/muxer/hls/mod.rs b/src/muxer/hls/mod.rs index a75d565..bba533e 100644 --- a/src/muxer/hls/mod.rs +++ b/src/muxer/hls/mod.rs @@ -1,4 +1,5 @@ mod handler; +mod mp4; use std::sync::{Arc, mpsc}; use std::error::Error; @@ -6,5 +7,6 @@ use std::error::Error; use crate::util; pub fn spawn(v: mpsc::Receiver, a: mpsc::Receiver, metadata: Arc, err_in: mpsc::Sender>, hls_args: util::HLSArgs) -> Result> { - todo!(); + let hls_handler = handler::spawn(v, a, err_in, metadata, hls_args)?; + Ok(hls_handler) } diff --git a/src/muxer/hls/mp4/atoms.rs b/src/muxer/hls/mp4/atoms.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/muxer/hls/mp4/mod.rs b/src/muxer/hls/mp4/mod.rs index e69de29..29d5e1f 100644 --- a/src/muxer/hls/mp4/mod.rs +++ b/src/muxer/hls/mp4/mod.rs @@ -0,0 +1,30 @@ +mod mp4muxer; + +use std::sync::{mpsc, Arc}; +use std::error::Error; + +use crate::util; + +pub struct MP4Muxer { + v: mpsc::Receiver, + v_samples: Vec, + a: mpsc::Receiver, + a_samples: Vec, + metadata: Arc, + err_in: mpsc::Sender>, +} + +pub fn new_muxer(v: mpsc::Receiver, a: mpsc::Receiver, metadata: Arc, err_in: mpsc::Sender>) -> Result> { + let mut muxer = MP4Muxer { + v: v, + a: a, + metadata: metadata, + err_in: err_in, + v_samples: Vec::new(), + a_samples: Vec::new(), + }; + muxer.gen_init()?; + return Ok(muxer); +} + + diff --git a/src/muxer/hls/mp4/mp4muxer.rs b/src/muxer/hls/mp4/mp4muxer.rs new file mode 100644 index 0000000..63d8a21 --- /dev/null +++ b/src/muxer/hls/mp4/mp4muxer.rs @@ -0,0 +1,20 @@ +use std::error::Error; + +use crate::util; +use crate::muxer::hls::mp4; + +impl mp4::MP4Muxer { + pub fn gen_init(&mut self) -> Result<(), Box> { + let a_CC = self.handle_a_CC()?; + let v_CC = self.handle_v_CC()?; + Ok(()) + } + + fn handle_a_CC(&mut self) -> Result> { + todo!(); + } + + fn handle_v_CC(&mut self) -> Result> { + todo!(); + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index c6ce06a..e024803 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -194,6 +194,28 @@ impl fmt::Display for EncoderError { fmt::Debug::fmt(self, f) } } + +pub enum MuxerError { + InvalidCodec, +} + +impl Error for MuxerError {} + +#[allow(unreachable_patterns)] +impl fmt::Debug for MuxerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + MuxerError::InvalidCodec => write!(f, "Codec not valid, unhandled"), + _ => write!(f, "Error not described yet") + } + } +} + +impl fmt::Display for MuxerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(self, f) + } +} // funcs pub fn thread_freeze(err_sender: mpsc::Sender>, err: Box) {