more segmenter basics

This commit is contained in:
Muaz Ahmad 2023-10-24 12:28:43 +05:00
parent da1149ec56
commit 3889ed32e8
7 changed files with 48 additions and 18 deletions

View file

@ -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<Box<dyn Error + Send + Sync>>,
args: util::HLSArgs,
curr_segment_idx: usize,
curr_segments: Vec<String>,
}
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<String> {
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<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, 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<String>) {
loop {
match del_list.next() {
Some(filename) => fs::remove_file(filename),
None => return
};
}
}

View file

@ -1,5 +1,6 @@
mod handler;
mod mp4;
mod segments;
use std::sync::{Arc, mpsc};
use std::error::Error;

View file

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

View file

@ -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<dyn Error>> {
@ -68,6 +69,10 @@ impl mp4::MP4Muxer {
}
});
}
pub fn get_segment(&mut self, idx: usize) -> Option<segments::Segment> {
todo!();
}
}
fn get_av1_stsd(mut sample: Vec<u8>, metadata: &Arc<util::Metadata>) -> mp4::atoms::STSD {

View file

@ -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<Sample> {
return self.queue.lock().unwrap().drain(..self.samples_per_sec * secs).collect();
}
}

View file

@ -3,6 +3,13 @@ use crate::muxer::hls::mp4::samples;
pub struct Segment {
segment_idx: usize,
pub filename: String,
segment_video: Vec<samples::Sample>,
segment_audio: Vec<samples::Sample>,
}
impl Segment {
pub fn dump(&self) {
todo!();
}
}

View file

@ -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