switch to arc of mutex to allow mut ref to be shared over threads

This commit is contained in:
Muaz Ahmad 2023-10-10 14:38:14 +05:00
parent ad5d954edb
commit 6b9427aec1
2 changed files with 15 additions and 7 deletions

View file

@ -1,11 +1,11 @@
use std::sync::Arc; use std::sync::{Arc, Mutex};
use std::error::Error; use std::error::Error;
use crate::util; use crate::util;
use crate::decode::codecs::Decoder; use crate::decode::codecs::Decoder;
use crate::decode::codecs; use crate::decode::codecs;
pub fn spawn(metadata: Arc<util::Metadata>) -> Result<(impl Decoder, impl Decoder), Box<dyn Error>> { pub fn spawn(metadata: Arc<util::Metadata>) -> Result<(impl Decoder + Clone + Send + 'static, impl Decoder + Clone + Send + 'static), Box<dyn Error>> {
let v = match metadata.video.codec { let v = match metadata.video.codec {
Some(util::VideoCodec::H264) => codecs::video::new_h264(metadata.clone())?, Some(util::VideoCodec::H264) => codecs::video::new_h264(metadata.clone())?,
_ => {return Err(Box::new(util::DecoderError::CodecNotImplemented));} _ => {return Err(Box::new(util::DecoderError::CodecNotImplemented));}
@ -14,5 +14,5 @@ pub fn spawn(metadata: Arc<util::Metadata>) -> Result<(impl Decoder, impl Decode
Some(util::AudioCodec::AAC) => codecs::audio::new_aac(metadata)?, Some(util::AudioCodec::AAC) => codecs::audio::new_aac(metadata)?,
_ => {return Err(Box::new(util::DecoderError::CodecNotImplemented));} _ => {return Err(Box::new(util::DecoderError::CodecNotImplemented));}
}; };
return Ok((v, a)); return Ok((Arc::new(Mutex::new(v)), Arc::new(Mutex::new(a))));
} }

View file

@ -1,7 +1,7 @@
mod cmds; mod cmds;
mod codecs; mod codecs;
use std::sync::{mpsc, Arc}; use std::sync::{mpsc, Arc, Mutex};
use std::error::Error; use std::error::Error;
use std::thread; use std::thread;
@ -31,11 +31,19 @@ pub fn spawn(
fn spawn_threads( fn spawn_threads(
v: mpsc::Receiver<util::NALUPacket>, v: mpsc::Receiver<util::NALUPacket>,
raw_v_in: mpsc::Sender<util::RawMedia>, raw_v_in: mpsc::Sender<util::RawMedia>,
v_decoder: impl Decoder, v_decoder: impl Decoder + Clone + Send + 'static,
a: mpsc::Receiver<util::NALUPacket>, a: mpsc::Receiver<util::NALUPacket>,
raw_a_in: mpsc::Sender<util::RawMedia>, raw_a_in: mpsc::Sender<util::RawMedia>,
a_decoder: impl Decoder, a_decoder: impl Decoder + Clone + Send + 'static,
err_in: mpsc::Sender<Box<dyn Error + Send + Sync>> err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>
) { ) {
todo!(); let err_clone = err_in.clone();
let tmp_v = v_decoder.clone();
let tmp_a = a_decoder.clone();
thread::spawn(move || {
tmp_v.write_loop(v, err_clone);
});
thread::spawn(move || {
tmp_a.write_loop(a, err_in);
});
} }