From f094604f6e900106aad80485a7a0d1dee6fff8fe Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 26 Nov 2024 14:39:24 +0500 Subject: [PATCH] Basic pipewire stream creation --- src/audio/mod.rs | 23 ++++++++++++++++++----- src/audio/pipewire.rs | 32 ++++++++++++++++++++++++++++++++ src/audio/sound_mgr.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/audio/pipewire.rs create mode 100644 src/audio/sound_mgr.rs diff --git a/src/audio/mod.rs b/src/audio/mod.rs index 558da44..e7cb2ed 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -8,6 +8,8 @@ use std::{ use std::sync::mpsc::Receiver; +use pipewire::PipewireContext; + use crate::{config::Settings, player::PlayerEvent, utils::Error}; pub enum AudioEvent {} @@ -17,6 +19,7 @@ pub struct SoundManager { error_chan: Sender, audio_controls: Receiver, player_chan: Sender, + pwire: PipewireContext, } pub fn init( @@ -25,14 +28,24 @@ pub fn init( player_chan: Sender, ) -> Result, Error> { let (audio_control_in, audio_control_out) = channel(); - thread::spawn(|| { - SoundManager { + thread::spawn(move || { + let mut sound_mgr = match SoundManager::init( settings, - error_chan, - audio_controls: audio_control_out, + error_chan.clone(), + audio_control_out, player_chan, + ) { + Err(err) => { + error_chan.send(err).unwrap(); + thread::park(); + return; + } + Ok(x) => x, }; - loop {} + sound_mgr.begin() }); Ok(audio_control_in) } + +mod pipewire; +mod sound_mgr; diff --git a/src/audio/pipewire.rs b/src/audio/pipewire.rs new file mode 100644 index 0000000..44e4dd3 --- /dev/null +++ b/src/audio/pipewire.rs @@ -0,0 +1,32 @@ +use pipewire::{ + context::Context, core::Core, keys, main_loop::MainLoop, properties::properties, stream::Stream, +}; + +use crate::utils::Error; + +pub struct PipewireContext { + pub mainloop: MainLoop, + context: Context, + core: Core, + stream: Stream, +} + +pub fn init() -> Result { + let mainloop = MainLoop::new(None)?; + let context = Context::new(&mainloop)?; + let core = context.connect(None)?; + let props = properties! { + *keys::MEDIA_TYPE => "Audio", + *keys::MEDIA_ROLE => "Music", + *keys::MEDIA_CATEGORY => "Playback", + *keys::AUDIO_CHANNELS => "2", + }; + let audio_source = Stream::new(&core, "Subtails", props)?; + + Ok(PipewireContext { + mainloop, + context, + core, + stream: audio_source, + }) +} diff --git a/src/audio/sound_mgr.rs b/src/audio/sound_mgr.rs new file mode 100644 index 0000000..b6baf91 --- /dev/null +++ b/src/audio/sound_mgr.rs @@ -0,0 +1,29 @@ +use std::sync::{ + mpsc::{Receiver, Sender}, + Arc, +}; + +use crate::{config::Settings, player::PlayerEvent, utils::Error}; + +use super::{pipewire, AudioEvent, SoundManager}; + +impl SoundManager { + pub fn init( + settings: Arc, + error_chan: Sender, + audio_control_chan: Receiver, + player_chan: Sender, + ) -> Result { + let pwire_context = pipewire::init()?; + Ok(SoundManager { + settings, + error_chan, + audio_controls: audio_control_chan, + player_chan, + pwire: pwire_context, + }) + } + pub fn begin(&mut self) { + self.pwire.mainloop.run(); + } +}