diff --git a/src/decode/cmds.rs b/src/decode/cmds.rs new file mode 100644 index 0000000..082013e --- /dev/null +++ b/src/decode/cmds.rs @@ -0,0 +1,9 @@ +use std::sync::Arc; +use std::error::Error; + +use crate::util; +use crate::decode::codecs::Decoder; + +pub fn spawn(metadata: Arc) -> Result<(impl Decoder, impl Decoder), Box> { + todo!(); +} diff --git a/src/decode/codecs/mod.rs b/src/decode/codecs/mod.rs new file mode 100644 index 0000000..31f3302 --- /dev/null +++ b/src/decode/codecs/mod.rs @@ -0,0 +1,33 @@ +use std::sync::mpsc; +use std::error::Error; + +use crate::util; + +pub trait Decoder { + fn write_nalu(&mut self, nalu: util::NALUPacket) -> Result<(), Box>; + fn write_loop(&mut self, chan_in: mpsc::Receiver, err_in: mpsc::Sender>) { + loop { + let nalu = match chan_in.recv() { + Ok(x) => x, + Err(err) => {util::thread_freeze(err_in, Box::new(err)); return} + }; + match self.write_nalu(nalu) { + Ok(_) => (), + Err(err) => {util::thread_freeze(err_in, err); return} + } + } + } + fn read_raw(&mut self) -> Result>; + fn read_loop(&mut self, chan_out: mpsc::Sender, err_in: mpsc::Sender>) { + loop { + let raw_sample = match self.read_raw() { + Ok(x) => x, + Err(err) => {util::thread_freeze(err_in, err); return} + }; + match chan_out.send(raw_sample) { + Ok(_) => (), + Err(err) => {util::thread_freeze(err_in, Box::new(err)); return} + } + } + } +} diff --git a/src/decode/mod.rs b/src/decode/mod.rs index 8963386..acc0d44 100644 --- a/src/decode/mod.rs +++ b/src/decode/mod.rs @@ -1,8 +1,12 @@ +mod cmds; +mod codecs; + use std::sync::{mpsc, Arc}; use std::error::Error; use std::thread; use crate::util; +use crate::decode::codecs::Decoder; pub fn spawn( v: mpsc::Receiver, @@ -19,15 +23,27 @@ pub fn spawn( let (raw_v_in, raw_v_out) = mpsc::channel(); let (raw_a_in, raw_a_out) = mpsc::channel(); let (err_in, err_out) = mpsc::channel(); - let metadata_handle = metadata.clone(); + let (v_decoder, a_decoder) = cmds::spawn(metadata.clone())?; + spawn_threads(v, raw_v_in, v_decoder, a, raw_a_in, a_decoder, err_in); thread::spawn(move || { &raw_v_in; &raw_a_in; &err_in; - &metadata_handle; &v; &a; thread::park(); }); return Ok((raw_v_out, raw_a_out, err_out)); } + +fn spawn_threads( + v: mpsc::Receiver, + raw_v_in: mpsc::Sender, + v_decoder: impl Decoder, + a: mpsc::Receiver, + raw_a_in: mpsc::Sender, + a_decoder: impl Decoder, + err_in: mpsc::Sender> +) { + todo!(); +}