diff --git a/src/muxer/hls/handler.rs b/src/muxer/hls/handler.rs index c4d346b..4242bb0 100644 --- a/src/muxer/hls/handler.rs +++ b/src/muxer/hls/handler.rs @@ -2,33 +2,58 @@ use std::error::Error; use std::sync::{mpsc, Arc}; use std::time; use std::thread; +use std::fs; use crate::util; use crate::muxer::hls::mp4; +use crate::muxer::hls::segments; pub struct HLSHandler { muxer: mp4::MP4Muxer, err_in: mpsc::Sender>, args: util::HLSArgs, curr_segment_idx: usize, + curr_segments: Vec, } impl HLSHandler { 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!(); + match self.muxer.get_segment(self.curr_segment_idx) { + None => (), + Some(segment) => { + let del_list = self.new_segment(segment); + delete_files(del_list); + self.dump_playlist(); + self.curr_segment_idx += 1; + } } - if self.muxer.a_samples.segment_ready(self.args.segment_time as usize) { - todo!(); - } - thread::sleep(time::Duration::from_millis(100)) + thread::sleep(time::Duration::from_millis(100)); } + } + + fn new_segment(&mut self, segment: segments::Segment) -> std::vec::Drain { + segment.dump(); + self.curr_segments.push(segment.filename); + self.curr_segments.drain(..self.curr_segments.len() - self.args.max_segments) + } + + + fn dump_playlist(&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, curr_segment_idx: 0}) + Ok(HLSHandler {muxer: mp4::new_muxer(v, a, metadata, err_in.clone())?, err_in: err_in, args: args, curr_segment_idx: 0, curr_segments: Vec::new()}) +} + +fn delete_files(mut del_list: std::vec::Drain) { + loop { + match del_list.next() { + Some(filename) => fs::remove_file(filename), + None => return + }; + } } diff --git a/src/muxer/hls/mod.rs b/src/muxer/hls/mod.rs index bba533e..cd18216 100644 --- a/src/muxer/hls/mod.rs +++ b/src/muxer/hls/mod.rs @@ -1,5 +1,6 @@ mod handler; mod mp4; +mod segments; use std::sync::{Arc, mpsc}; use std::error::Error; diff --git a/src/muxer/hls/mp4/mod.rs b/src/muxer/hls/mp4/mod.rs index b504690..1a26208 100644 --- a/src/muxer/hls/mp4/mod.rs +++ b/src/muxer/hls/mp4/mod.rs @@ -1,6 +1,6 @@ mod mp4muxer; mod atoms; -mod samples; +pub mod samples; use std::sync::{mpsc, Arc, Mutex}; use std::error::Error; diff --git a/src/muxer/hls/mp4/mp4muxer.rs b/src/muxer/hls/mp4/mp4muxer.rs index ae18955..2466c08 100644 --- a/src/muxer/hls/mp4/mp4muxer.rs +++ b/src/muxer/hls/mp4/mp4muxer.rs @@ -5,6 +5,7 @@ use std::thread; use crate::util; use crate::muxer::hls::mp4; use crate::muxer::hls::mp4::atoms::MP4Atom; +use crate::muxer::hls::segments; impl mp4::MP4Muxer { pub fn gen_init(&mut self) -> Result<(), Box> { @@ -68,6 +69,10 @@ impl mp4::MP4Muxer { } }); } + + pub fn get_segment(&mut self, idx: usize) -> Option { + todo!(); + } } fn get_av1_stsd(mut sample: Vec, metadata: &Arc) -> mp4::atoms::STSD { diff --git a/src/muxer/hls/mp4/samples.rs b/src/muxer/hls/mp4/samples.rs index b8ae431..d450b27 100644 --- a/src/muxer/hls/mp4/samples.rs +++ b/src/muxer/hls/mp4/samples.rs @@ -20,12 +20,4 @@ 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 index 5e2b81d..1c24125 100644 --- a/src/muxer/hls/segments.rs +++ b/src/muxer/hls/segments.rs @@ -3,6 +3,13 @@ use crate::muxer::hls::mp4::samples; pub struct Segment { segment_idx: usize, + pub filename: String, segment_video: Vec, segment_audio: Vec, } + +impl Segment { + pub fn dump(&self) { + todo!(); + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index a70aaab..31c0c70 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -75,9 +75,9 @@ pub struct RawMedia { } pub struct HLSArgs { - pub segment_time: u32, + pub segment_time: usize, pub segment_prepend: String, - pub max_segments: u8, + pub max_segments: usize, } // Errors