From da1149ec566fe421acb1f94e94720c32e4c35534 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 23 Oct 2023 16:29:19 +0500 Subject: [PATCH] prep to handle segmentation --- src/muxer/hls/handler.rs | 17 +++++++++++++++-- src/muxer/hls/mp4/mod.rs | 10 ++++++---- src/muxer/hls/mp4/samples.rs | 9 +++++++++ src/muxer/hls/segments.rs | 8 ++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/muxer/hls/segments.rs diff --git a/src/muxer/hls/handler.rs b/src/muxer/hls/handler.rs index d0094c4..c4d346b 100644 --- a/src/muxer/hls/handler.rs +++ b/src/muxer/hls/handler.rs @@ -1,5 +1,7 @@ use std::error::Error; use std::sync::{mpsc, Arc}; +use std::time; +use std::thread; use crate::util; use crate::muxer::hls::mp4; @@ -8,14 +10,25 @@ pub struct HLSHandler { muxer: mp4::MP4Muxer, err_in: mpsc::Sender>, args: util::HLSArgs, + curr_segment_idx: usize, } impl HLSHandler { - pub fn data_loop(&self) { + pub fn data_loop(&mut self) { + self.muxer.spawn_read_loops(); + loop { + if self.muxer.v_samples.segment_ready(self.args.segment_time as usize) { + todo!(); + } + if self.muxer.a_samples.segment_ready(self.args.segment_time as usize) { + todo!(); + } + thread::sleep(time::Duration::from_millis(100)) + } 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}) + Ok(HLSHandler {muxer: mp4::new_muxer(v, a, metadata, err_in.clone())?, err_in: err_in, args: args, curr_segment_idx: 0}) } diff --git a/src/muxer/hls/mp4/mod.rs b/src/muxer/hls/mp4/mod.rs index b7b6dba..b504690 100644 --- a/src/muxer/hls/mp4/mod.rs +++ b/src/muxer/hls/mp4/mod.rs @@ -9,9 +9,9 @@ use crate::util; pub struct MP4Muxer { v: Option>, - v_samples: Arc, + pub v_samples: Arc, a: Option>, - a_samples: Arc, + pub a_samples: Arc, metadata: Arc, err_in: mpsc::Sender>, } @@ -20,15 +20,17 @@ pub fn new_muxer(v: mpsc::Receiver, a: mpsc::Receiver>, pub default_duration: u32, + pub samples_per_sec: usize, } impl SampleQueue { @@ -19,4 +20,12 @@ impl SampleQueue { flags: flags, }) } + + pub fn segment_ready(&self, secs: usize) -> bool { + return self.queue.lock().unwrap().len() >= self.samples_per_sec * secs; + } + + pub fn get_segment(&self, secs: usize) -> Vec { + return self.queue.lock().unwrap().drain(..self.samples_per_sec * secs).collect(); + } } diff --git a/src/muxer/hls/segments.rs b/src/muxer/hls/segments.rs new file mode 100644 index 0000000..5e2b81d --- /dev/null +++ b/src/muxer/hls/segments.rs @@ -0,0 +1,8 @@ +use std::time; +use crate::muxer::hls::mp4::samples; + +pub struct Segment { + segment_idx: usize, + segment_video: Vec, + segment_audio: Vec, +}