Basic pipewire stream creation

This commit is contained in:
Muaz Ahmad 2024-11-26 14:39:24 +05:00
parent b2169abc94
commit f094604f6e
3 changed files with 79 additions and 5 deletions

View file

@ -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<Error>,
audio_controls: Receiver<AudioEvent>,
player_chan: Sender<PlayerEvent>,
pwire: PipewireContext,
}
pub fn init(
@ -25,14 +28,24 @@ pub fn init(
player_chan: Sender<PlayerEvent>,
) -> Result<Sender<AudioEvent>, 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;

32
src/audio/pipewire.rs Normal file
View file

@ -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<PipewireContext, Error> {
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,
})
}

29
src/audio/sound_mgr.rs Normal file
View file

@ -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<Settings>,
error_chan: Sender<Error>,
audio_control_chan: Receiver<AudioEvent>,
player_chan: Sender<PlayerEvent>,
) -> Result<SoundManager, Error> {
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();
}
}