From f70f8c05cb9a3f0c1b27e5c794af0a821782eeee Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 24 Oct 2023 12:59:57 +0500 Subject: [PATCH] more segment impl, removed EOF error to continue execution for now --- src/muxer/hls/handler.rs | 2 +- src/muxer/hls/mp4/mp4muxer.rs | 16 +++++++++++----- src/muxer/hls/mp4/samples.rs | 11 +++++++++++ src/muxer/hls/segments.rs | 5 ++--- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/muxer/hls/handler.rs b/src/muxer/hls/handler.rs index 4242bb0..f645d71 100644 --- a/src/muxer/hls/handler.rs +++ b/src/muxer/hls/handler.rs @@ -20,7 +20,7 @@ impl HLSHandler { pub fn data_loop(&mut self) { self.muxer.spawn_read_loops(); loop { - match self.muxer.get_segment(self.curr_segment_idx) { + match self.muxer.get_segment(self.curr_segment_idx, self.args.segment_time) { None => (), Some(segment) => { let del_list = self.new_segment(segment); diff --git a/src/muxer/hls/mp4/mp4muxer.rs b/src/muxer/hls/mp4/mp4muxer.rs index 2466c08..b93d26e 100644 --- a/src/muxer/hls/mp4/mp4muxer.rs +++ b/src/muxer/hls/mp4/mp4muxer.rs @@ -42,7 +42,6 @@ impl mp4::MP4Muxer { let (mut v, mut a) = (self.v.take().unwrap(), self.a.take().unwrap()); let v_queue = self.v_samples.clone(); let a_queue = self.a_samples.clone(); - let (err_in1, err_in2) = (self.err_in.clone(), self.err_in.clone()); thread::spawn(move || { let mut i = 1; @@ -50,7 +49,7 @@ impl mp4::MP4Muxer { match v.recv() { Ok(x) => {v_queue.push(x.packet_data, if i % 30 == 0 {0x02000000} else {0x01010000});}, Err(_) => { - util::thread_freeze(err_in1, Box::new(util::MuxerError::EOF)); + thread::park(); return } } @@ -62,7 +61,7 @@ impl mp4::MP4Muxer { match a.recv() { Ok(x) => {a_queue.push(x.packet_data, 0x01010000);}, Err(_) => { - util::thread_freeze(err_in2, Box::new(util::MuxerError::EOF)); + thread::park(); return } } @@ -70,8 +69,15 @@ impl mp4::MP4Muxer { }); } - pub fn get_segment(&mut self, idx: usize) -> Option { - todo!(); + pub fn get_segment(&mut self, idx: usize, time: usize) -> Option { + if self.v_samples.segment_ready(time) && self.a_samples.segment_ready(time) { + return Some(segments::Segment { + filename: idx.to_string() + ".m4s", + segment_video: self.v_samples.get_segment_samples(time), + segment_audio: self.a_samples.get_segment_samples(time), + }) + } + return None } } diff --git a/src/muxer/hls/mp4/samples.rs b/src/muxer/hls/mp4/samples.rs index d450b27..d22f386 100644 --- a/src/muxer/hls/mp4/samples.rs +++ b/src/muxer/hls/mp4/samples.rs @@ -1,5 +1,7 @@ use std::sync::{Arc, Mutex}; +use crate::muxer::hls::segments; + pub struct Sample { pub data: Vec, pub size: u32, @@ -20,4 +22,13 @@ impl SampleQueue { flags: flags, }) } + + pub fn segment_ready(&self, time: usize) -> bool { + self.queue.lock().unwrap().len() >= time * self.samples_per_sec + } + + pub fn get_segment_samples(&self, time: usize) -> Vec { + let samples_to_drain = time * self.samples_per_sec; + self.queue.lock().unwrap().drain(..samples_to_drain).collect() + } } diff --git a/src/muxer/hls/segments.rs b/src/muxer/hls/segments.rs index 1c24125..2abbb7e 100644 --- a/src/muxer/hls/segments.rs +++ b/src/muxer/hls/segments.rs @@ -2,10 +2,9 @@ use std::time; use crate::muxer::hls::mp4::samples; pub struct Segment { - segment_idx: usize, pub filename: String, - segment_video: Vec, - segment_audio: Vec, + pub segment_video: Vec, + pub segment_audio: Vec, } impl Segment {