Default Pod serialization added

This commit is contained in:
Muaz Ahmad 2024-12-03 15:13:50 +05:00
parent 7e56d50898
commit 2d142b43fa
5 changed files with 41 additions and 51 deletions

5
src/audio/avcodec.rs Normal file
View file

@ -0,0 +1,5 @@
use crate::utils::Error;
pub fn init() -> Result<(), Error> {
unimplemented!()
}

View file

@ -57,5 +57,6 @@ pub fn init(
Ok(audio_control_in)
}
mod avcodec;
mod pw;
mod sound_mgr;

View file

@ -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<Vec<u8>>,
@ -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();

View file

@ -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<u8> = 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())?;
}
}
}
}

View file

@ -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<dyn std::error::Error + Send + Sync>;
@ -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<u8> {
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()
}