diff --git a/src/audio/avcodec.rs b/src/audio/avcodec.rs new file mode 100644 index 0000000..2cd1ff1 --- /dev/null +++ b/src/audio/avcodec.rs @@ -0,0 +1,5 @@ +use crate::utils::Error; + +pub fn init() -> Result<(), Error> { + unimplemented!() +} diff --git a/src/audio/mod.rs b/src/audio/mod.rs index 8f960ea..24d4d77 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -57,5 +57,6 @@ pub fn init( Ok(audio_control_in) } +mod avcodec; mod pw; mod sound_mgr; diff --git a/src/audio/pw.rs b/src/audio/pw.rs index 29f1e88..54f7b30 100644 --- a/src/audio/pw.rs +++ b/src/audio/pw.rs @@ -11,7 +11,7 @@ use pipewire::{ stream::{Stream, StreamFlags}, }; -use crate::utils::Error; +use crate::utils::{default_pw_pod, Error}; pub fn init( samples: std::sync::mpsc::Receiver>, @@ -58,18 +58,19 @@ pub fn init( }) .register()?; + let default_audio = default_pw_pod(); + let mut params = [Pod::from_bytes(&default_audio).unwrap()]; + audio_source.connect( + Direction::Output, + None, + StreamFlags::AUTOCONNECT | StreamFlags::ALLOC_BUFFERS | StreamFlags::MAP_BUFFERS, + &mut params, + ); + let audio_source_ref = audio_source.clone(); let _receiver = pw_signal.attach(mainloop.loop_(), move |pod_bytes| { let mut params = [Pod::from_bytes(&pod_bytes).unwrap()]; - audio_source_ref.disconnect().unwrap(); - audio_source_ref - .connect( - Direction::Output, - None, - StreamFlags::AUTOCONNECT | StreamFlags::MAP_BUFFERS | StreamFlags::ALLOC_BUFFERS, - &mut params, - ) - .unwrap(); + audio_source_ref.update_params(&mut params); }); mainloop.run(); diff --git a/src/audio/sound_mgr.rs b/src/audio/sound_mgr.rs index 07a6aa2..9e2df68 100644 --- a/src/audio/sound_mgr.rs +++ b/src/audio/sound_mgr.rs @@ -1,6 +1,4 @@ use std::{ - f64::consts, - io::Cursor, sync::{ mpsc::{channel, Receiver, Sender}, Arc, @@ -8,17 +6,9 @@ use std::{ thread, }; -use pipewire::spa::{ - param::audio::{AudioFormat, AudioInfoRaw, MAX_CHANNELS}, - pod::{serialize::PodSerializer, Object, Pod, Value}, - sys::{ - SPA_PARAM_EnumFormat, SPA_TYPE_OBJECT_Format, SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, - }, -}; - use crate::{config::Settings, player::PlayerEvent, utils::Error}; -use super::{pw, AudioEvent, SoundManager}; +use super::{avcodec, pw, AudioEvent, SoundManager}; impl SoundManager { pub fn init( @@ -40,25 +30,7 @@ impl SoundManager { Ok(_) => (), }; }); - let mut test_info = AudioInfoRaw::new(); - test_info.set_format(AudioFormat::S16LE); - test_info.set_rate(44100); - test_info.set_channels(2); - let mut positions = [0; MAX_CHANNELS]; - (positions[0], positions[1]) = (SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR); - test_info.set_position(positions); - let test_pod = PodSerializer::serialize( - Cursor::new(Vec::new()), - &Value::Object(Object { - type_: SPA_TYPE_OBJECT_Format, - id: SPA_PARAM_EnumFormat, - properties: test_info.into(), - }), - ) - .unwrap() - .0 - .into_inner(); - pw_signal_in.send(test_pod).unwrap(); + let libav_context = avcodec::init()?; Ok(SoundManager { settings, error_chan, @@ -69,22 +41,11 @@ impl SoundManager { }) } pub fn begin(&mut self) -> Result<(), Error> { - let mut test_tone: Vec = vec![0; 1764]; - for i in 0..441 { - let t = i as f64 / 441.0 * consts::PI * 2.0; - let v = (f64::sin(t) * 16767.0) as i16; - test_tone[i * 4..i * 4 + 2].copy_from_slice(&i16::to_le_bytes(v)); - test_tone[i * 4 + 2..i * 4 + 4].copy_from_slice(&i16::to_le_bytes(v)); - } - loop { match self.audio_controls.recv()? { AudioEvent::DecodeChunk(chunk) => (), _ => unimplemented!(), } - for i in 0..100 { - self.sample_in.send(test_tone.clone())?; - } } } } diff --git a/src/utils.rs b/src/utils.rs index 5f2e176..831b56a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,11 @@ +use std::io::Cursor; + use image::DynamicImage; +use pipewire::spa::{ + param::audio::AudioInfoRaw, + pod::{serialize::PodSerializer, Object, Value}, + sys::{SPA_PARAM_EnumFormat, SPA_TYPE_OBJECT_Format}, +}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; pub type Error = Box; @@ -33,3 +40,18 @@ pub fn format_duration(secs: u32) -> String { return format!("{:0>2}:{:0>2}", minutes, rem); } } + +pub fn default_pw_pod() -> Vec { + let audio_info = AudioInfoRaw::new(); + PodSerializer::serialize( + Cursor::new(Vec::new()), + &Value::Object(Object { + type_: SPA_TYPE_OBJECT_Format, + id: SPA_PARAM_EnumFormat, + properties: audio_info.into(), + }), + ) + .unwrap() + .0 + .into_inner() +}