Run pwire loop on separate thread

This commit is contained in:
Muaz Ahmad 2024-11-29 16:47:32 +05:00
parent 6656727021
commit 60b5f2ed60
2 changed files with 33 additions and 11 deletions

View file

@ -8,8 +8,6 @@ use std::{
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use pipewire::PipewireContext;
use crate::{config::Settings, player::PlayerEvent, utils::Error}; use crate::{config::Settings, player::PlayerEvent, utils::Error};
pub enum AudioEvent { pub enum AudioEvent {
@ -21,7 +19,6 @@ pub struct SoundManager {
error_chan: Sender<Error>, error_chan: Sender<Error>,
audio_controls: Receiver<AudioEvent>, audio_controls: Receiver<AudioEvent>,
player_chan: Sender<PlayerEvent>, player_chan: Sender<PlayerEvent>,
pwire: PipewireContext,
} }
pub fn init( pub fn init(
@ -44,7 +41,14 @@ pub fn init(
} }
Ok(x) => x, 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) Ok(audio_control_in)
} }

View file

@ -1,6 +1,9 @@
use std::sync::{ use std::{
mpsc::{Receiver, Sender}, sync::{
Arc, mpsc::{Receiver, Sender},
Arc,
},
thread,
}; };
use crate::{config::Settings, player::PlayerEvent, utils::Error}; use crate::{config::Settings, player::PlayerEvent, utils::Error};
@ -14,16 +17,31 @@ impl SoundManager {
audio_control_chan: Receiver<AudioEvent>, audio_control_chan: Receiver<AudioEvent>,
player_chan: Sender<PlayerEvent>, player_chan: Sender<PlayerEvent>,
) -> Result<SoundManager, Error> { ) -> Result<SoundManager, Error> {
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 { Ok(SoundManager {
settings, settings,
error_chan, error_chan,
audio_controls: audio_control_chan, audio_controls: audio_control_chan,
player_chan, player_chan,
pwire: pwire_context,
}) })
} }
pub fn begin(&mut self) { pub fn begin(&mut self) -> Result<(), Error> {
self.pwire.mainloop.run(); loop {
match self.audio_controls.recv()? {
AudioEvent::DecodeChunk(chunk) => (),
_ => unimplemented!(),
}
}
} }
} }