segment writing success

This commit is contained in:
Muaz Ahmad 2023-10-25 11:52:33 +05:00
parent fc7ecd62bd
commit 8849808c1e
5 changed files with 34 additions and 6 deletions

View file

@ -3,6 +3,7 @@ use std::sync::{mpsc, Arc};
use std::time; use std::time;
use std::thread; use std::thread;
use std::fs; use std::fs;
use std::cmp::max;
use crate::util; use crate::util;
use crate::muxer::hls::mp4; use crate::muxer::hls::mp4;
@ -36,7 +37,7 @@ impl HLSHandler {
fn new_segment(&mut self, segment: segments::Segment) -> std::vec::Drain<String> { fn new_segment(&mut self, segment: segments::Segment) -> std::vec::Drain<String> {
segment.dump(); segment.dump();
self.curr_segments.push(segment.filename); 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))
} }

View file

@ -1145,9 +1145,9 @@ impl MP4Atom for SampleData {
} }
} }
struct MDAT { pub struct MDAT {
v_samples: Vec<u8>, pub v_samples: Vec<u8>,
a_samples: Vec<u8>, pub a_samples: Vec<u8>,
} }
impl MP4Atom for MDAT { impl MP4Atom for MDAT {

View file

@ -1,5 +1,5 @@
mod mp4muxer; mod mp4muxer;
mod atoms; pub mod atoms;
pub mod samples; pub mod samples;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc, Arc, Mutex};

View file

@ -73,6 +73,7 @@ impl mp4::MP4Muxer {
if self.v_samples.segment_ready(time) && self.a_samples.segment_ready(time) { if self.v_samples.segment_ready(time) && self.a_samples.segment_ready(time) {
return Some(segments::Segment { return Some(segments::Segment {
filename: idx.to_string() + ".m4s", filename: idx.to_string() + ".m4s",
idx: idx,
segment_video: self.v_samples.get_segment_samples(time), segment_video: self.v_samples.get_segment_samples(time),
sample_duration_v: self.v_samples.default_duration, sample_duration_v: self.v_samples.default_duration,
segment_audio: self.a_samples.get_segment_samples(time), segment_audio: self.a_samples.get_segment_samples(time),

View file

@ -1,8 +1,14 @@
use std::time; use std::time;
use std::fs::File;
use std::io::Write;
use crate::muxer::hls::mp4::samples; use crate::muxer::hls::mp4::samples;
use crate::muxer::hls::mp4::atoms;
use crate::muxer::hls::mp4::atoms::MP4Atom;
pub struct Segment { pub struct Segment {
pub filename: String, pub filename: String,
pub idx: usize,
pub segment_video: Vec<samples::Sample>, pub segment_video: Vec<samples::Sample>,
pub sample_duration_v: u32, pub sample_duration_v: u32,
pub segment_audio: Vec<samples::Sample>, pub segment_audio: Vec<samples::Sample>,
@ -11,6 +17,26 @@ pub struct Segment {
impl Segment { impl Segment {
pub fn dump(&self) { 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,
}
} }
} }