diff --git a/src/muxer/hls/handler.rs b/src/muxer/hls/handler.rs index f645d71..ab2d4da 100644 --- a/src/muxer/hls/handler.rs +++ b/src/muxer/hls/handler.rs @@ -3,6 +3,7 @@ use std::sync::{mpsc, Arc}; use std::time; use std::thread; use std::fs; +use std::cmp::max; use crate::util; use crate::muxer::hls::mp4; @@ -36,7 +37,7 @@ impl HLSHandler { 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) + self.curr_segments.drain(..max(self.curr_segments.len() - self.args.max_segments, 0)) } diff --git a/src/muxer/hls/mp4/atoms.rs b/src/muxer/hls/mp4/atoms.rs index 6fa86b7..735b742 100644 --- a/src/muxer/hls/mp4/atoms.rs +++ b/src/muxer/hls/mp4/atoms.rs @@ -1145,9 +1145,9 @@ impl MP4Atom for SampleData { } } -struct MDAT { - v_samples: Vec, - a_samples: Vec, +pub struct MDAT { + pub v_samples: Vec, + pub a_samples: Vec, } impl MP4Atom for MDAT { diff --git a/src/muxer/hls/mp4/mod.rs b/src/muxer/hls/mp4/mod.rs index 1a26208..d7ab478 100644 --- a/src/muxer/hls/mp4/mod.rs +++ b/src/muxer/hls/mp4/mod.rs @@ -1,5 +1,5 @@ mod mp4muxer; -mod atoms; +pub mod atoms; pub mod samples; use std::sync::{mpsc, Arc, Mutex}; diff --git a/src/muxer/hls/mp4/mp4muxer.rs b/src/muxer/hls/mp4/mp4muxer.rs index fb4216b..a4d6cc2 100644 --- a/src/muxer/hls/mp4/mp4muxer.rs +++ b/src/muxer/hls/mp4/mp4muxer.rs @@ -73,6 +73,7 @@ impl mp4::MP4Muxer { if self.v_samples.segment_ready(time) && self.a_samples.segment_ready(time) { return Some(segments::Segment { filename: idx.to_string() + ".m4s", + idx: idx, segment_video: self.v_samples.get_segment_samples(time), sample_duration_v: self.v_samples.default_duration, segment_audio: self.a_samples.get_segment_samples(time), diff --git a/src/muxer/hls/segments.rs b/src/muxer/hls/segments.rs index 18f8683..402ef16 100644 --- a/src/muxer/hls/segments.rs +++ b/src/muxer/hls/segments.rs @@ -1,8 +1,14 @@ use std::time; +use std::fs::File; +use std::io::Write; + use crate::muxer::hls::mp4::samples; +use crate::muxer::hls::mp4::atoms; +use crate::muxer::hls::mp4::atoms::MP4Atom; pub struct Segment { pub filename: String, + pub idx: usize, pub segment_video: Vec, pub sample_duration_v: u32, pub segment_audio: Vec, @@ -11,6 +17,26 @@ pub struct Segment { impl Segment { pub fn dump(&self) { - todo!(); + let mdat = self.new_mdat(); + let moof = atoms::new_moof(&self.segment_video, self.sample_duration_v, &self.segment_audio, self.sample_duration_a, 0, mdat.v_samples.len(), self.idx); + let mut segment_bytes = moof.marshall(); + segment_bytes.append(&mut mdat.marshall()); + + let mut f = File::create(&self.filename).unwrap(); + f.write_all(&segment_bytes); + } + + fn new_mdat(&self) -> atoms::MDAT { + let (mut v_samples, mut a_samples) = (Vec::new(), Vec::new()); + for sample in &self.segment_video { + v_samples.extend_from_slice(sample.data.as_slice()); + } + for sample in &self.segment_audio { + a_samples.extend_from_slice(sample.data.as_slice()); + } + atoms::MDAT { + v_samples: v_samples, + a_samples: a_samples, + } } }