From 60b5f2ed60134a05660ca605a5ac8efcb89b0b8b Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Fri, 29 Nov 2024 16:47:32 +0500 Subject: [PATCH] Run pwire loop on separate thread --- src/audio/mod.rs | 12 ++++++++---- src/audio/sound_mgr.rs | 32 +++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/audio/mod.rs b/src/audio/mod.rs index 8ec48ea..c35b4c6 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -8,8 +8,6 @@ use std::{ use std::sync::mpsc::Receiver; -use pipewire::PipewireContext; - use crate::{config::Settings, player::PlayerEvent, utils::Error}; pub enum AudioEvent { @@ -21,7 +19,6 @@ pub struct SoundManager { error_chan: Sender, audio_controls: Receiver, player_chan: Sender, - pwire: PipewireContext, } pub fn init( @@ -44,7 +41,14 @@ pub fn init( } Ok(x) => x, }; - sound_mgr.begin() + match sound_mgr.begin() { + Err(err) => { + error_chan.send(err).unwrap(); + thread::park(); + return; + } + Ok(_) => (), + }; }); Ok(audio_control_in) } diff --git a/src/audio/sound_mgr.rs b/src/audio/sound_mgr.rs index b6baf91..648ab2e 100644 --- a/src/audio/sound_mgr.rs +++ b/src/audio/sound_mgr.rs @@ -1,6 +1,9 @@ -use std::sync::{ - mpsc::{Receiver, Sender}, - Arc, +use std::{ + sync::{ + mpsc::{Receiver, Sender}, + Arc, + }, + thread, }; use crate::{config::Settings, player::PlayerEvent, utils::Error}; @@ -14,16 +17,31 @@ impl SoundManager { audio_control_chan: Receiver, player_chan: Sender, ) -> Result { - let pwire_context = pipewire::init()?; + let error_chan_clone = error_chan.clone(); + thread::spawn(move || { + let pwire_context = match pipewire::init() { + Err(err) => { + error_chan_clone.send(err).unwrap(); + thread::park(); + return; + } + Ok(x) => x, + }; + pwire_context.mainloop.run(); + }); Ok(SoundManager { settings, error_chan, audio_controls: audio_control_chan, player_chan, - pwire: pwire_context, }) } - pub fn begin(&mut self) { - self.pwire.mainloop.run(); + pub fn begin(&mut self) -> Result<(), Error> { + loop { + match self.audio_controls.recv()? { + AudioEvent::DecodeChunk(chunk) => (), + _ => unimplemented!(), + } + } } }