prep to handle segmentation

This commit is contained in:
Muaz Ahmad 2023-10-23 16:29:19 +05:00
parent 89fc3c63db
commit da1149ec56
4 changed files with 38 additions and 6 deletions

View file

@ -1,5 +1,7 @@
use std::error::Error; use std::error::Error;
use std::sync::{mpsc, Arc}; use std::sync::{mpsc, Arc};
use std::time;
use std::thread;
use crate::util; use crate::util;
use crate::muxer::hls::mp4; use crate::muxer::hls::mp4;
@ -8,14 +10,25 @@ pub struct HLSHandler {
muxer: mp4::MP4Muxer, muxer: mp4::MP4Muxer,
err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>, err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>,
args: util::HLSArgs, args: util::HLSArgs,
curr_segment_idx: usize,
} }
impl HLSHandler { 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!(); todo!();
} }
} }
pub fn spawn(v: mpsc::Receiver<util::NALUPacket>, a: mpsc::Receiver<util::NALUPacket>, err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>, metadata: Arc<util::Metadata>, args: util::HLSArgs) -> Result<HLSHandler, Box<dyn Error>> { pub fn spawn(v: mpsc::Receiver<util::NALUPacket>, a: mpsc::Receiver<util::NALUPacket>, err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>, metadata: Arc<util::Metadata>, args: util::HLSArgs) -> Result<HLSHandler, Box<dyn Error>> {
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})
} }

View file

@ -9,9 +9,9 @@ use crate::util;
pub struct MP4Muxer { pub struct MP4Muxer {
v: Option<mpsc::Receiver<util::NALUPacket>>, v: Option<mpsc::Receiver<util::NALUPacket>>,
v_samples: Arc<samples::SampleQueue>, pub v_samples: Arc<samples::SampleQueue>,
a: Option<mpsc::Receiver<util::NALUPacket>>, a: Option<mpsc::Receiver<util::NALUPacket>>,
a_samples: Arc<samples::SampleQueue>, pub a_samples: Arc<samples::SampleQueue>,
metadata: Arc<util::Metadata>, metadata: Arc<util::Metadata>,
err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>, err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>,
} }
@ -20,15 +20,17 @@ pub fn new_muxer(v: mpsc::Receiver<util::NALUPacket>, a: mpsc::Receiver<util::NA
let mut muxer = MP4Muxer { let mut muxer = MP4Muxer {
v: Some(v), v: Some(v),
a: Some(a), a: Some(a),
metadata: metadata, metadata: metadata.clone(),
err_in: err_in, err_in: err_in,
v_samples: Arc::new(samples::SampleQueue { v_samples: Arc::new(samples::SampleQueue {
queue: Mutex::new(Vec::new()), queue: Mutex::new(Vec::new()),
default_duration: 1000, default_duration: 1000,
samples_per_sec: metadata.video.framerate as usize,
}), }),
a_samples: Arc::new(samples::SampleQueue { a_samples: Arc::new(samples::SampleQueue {
queue: Mutex::new(Vec::new()), queue: Mutex::new(Vec::new()),
default_duration: 960, default_duration: metadata.audio.samplerate * 20 / 1000,
samples_per_sec: 50,
}), }),
}; };
muxer.gen_init()?; muxer.gen_init()?;

View file

@ -9,6 +9,7 @@ pub struct Sample {
pub struct SampleQueue { pub struct SampleQueue {
pub queue: Mutex<Vec<Sample>>, pub queue: Mutex<Vec<Sample>>,
pub default_duration: u32, pub default_duration: u32,
pub samples_per_sec: usize,
} }
impl SampleQueue { impl SampleQueue {
@ -19,4 +20,12 @@ impl SampleQueue {
flags: flags, 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<Sample> {
return self.queue.lock().unwrap().drain(..self.samples_per_sec * secs).collect();
}
} }

View file

@ -0,0 +1,8 @@
use std::time;
use crate::muxer::hls::mp4::samples;
pub struct Segment {
segment_idx: usize,
segment_video: Vec<samples::Sample>,
segment_audio: Vec<samples::Sample>,
}