From 6b9427aec1036e632f386a3cc14dc2e0a8dd3c73 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 10 Oct 2023 14:38:14 +0500 Subject: [PATCH] switch to arc of mutex to allow mut ref to be shared over threads --- src/decode/cmds.rs | 6 +++--- src/decode/mod.rs | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/decode/cmds.rs b/src/decode/cmds.rs index e851ce1..23c73a6 100644 --- a/src/decode/cmds.rs +++ b/src/decode/cmds.rs @@ -1,11 +1,11 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::error::Error; use crate::util; use crate::decode::codecs::Decoder; use crate::decode::codecs; -pub fn spawn(metadata: Arc) -> Result<(impl Decoder, impl Decoder), Box> { +pub fn spawn(metadata: Arc) -> Result<(impl Decoder + Clone + Send + 'static, impl Decoder + Clone + Send + 'static), Box> { let v = match metadata.video.codec { Some(util::VideoCodec::H264) => codecs::video::new_h264(metadata.clone())?, _ => {return Err(Box::new(util::DecoderError::CodecNotImplemented));} @@ -14,5 +14,5 @@ pub fn spawn(metadata: Arc) -> Result<(impl Decoder, impl Decode Some(util::AudioCodec::AAC) => codecs::audio::new_aac(metadata)?, _ => {return Err(Box::new(util::DecoderError::CodecNotImplemented));} }; - return Ok((v, a)); + return Ok((Arc::new(Mutex::new(v)), Arc::new(Mutex::new(a)))); } diff --git a/src/decode/mod.rs b/src/decode/mod.rs index 1b92f8c..69f6f42 100644 --- a/src/decode/mod.rs +++ b/src/decode/mod.rs @@ -1,7 +1,7 @@ mod cmds; mod codecs; -use std::sync::{mpsc, Arc}; +use std::sync::{mpsc, Arc, Mutex}; use std::error::Error; use std::thread; @@ -31,11 +31,19 @@ pub fn spawn( fn spawn_threads( v: mpsc::Receiver, raw_v_in: mpsc::Sender, - v_decoder: impl Decoder, + v_decoder: impl Decoder + Clone + Send + 'static, a: mpsc::Receiver, raw_a_in: mpsc::Sender, - a_decoder: impl Decoder, + a_decoder: impl Decoder + Clone + Send + 'static, err_in: mpsc::Sender> ) { - 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); + }); }