feat: new protocol client implementation

This commit is contained in:
Ahmet Kaan Gümüş 2025-06-14 06:04:48 +03:00
parent 51c29f7921
commit 5ad6184f13
9 changed files with 497 additions and 267 deletions

View file

@ -1,10 +1,17 @@
use std::sync::Arc;
use bincode::{Decode, Encode};
use tokio::sync::broadcast;
use crate::Error;
const SIGNAL_DATA_LENGTH: usize = 4;
const NETWORK_DATA_LENGTH: usize = 6;
pub const NETWORK_DATA_LENGTH: usize = 6;
pub const SPEAKER_ACTION_CHANNEL_LENGTH: usize = 1024;
pub const AUDIO_DATA_SENDER_CHANNEL_LENGTH: usize = 1024 * 16 * 4;
pub const DEFAULT_SAMPLE_RATE: u32 = 44100;
pub type SpeakerWithDataAndAction = (SpeakerWithData, SpeakerAction);
type SignalBufferReturn = [u8; SIGNAL_DATA_LENGTH];
type NetworkBufferReturn = [u8; NETWORK_DATA_LENGTH];
@ -26,20 +33,88 @@ impl Speaker {
}
}
#[derive(Debug, Encode, Decode)]
#[derive(Debug, Clone, Copy)]
pub enum SpeakerAction {
Join,
Left,
}
#[derive(Debug, Clone)]
pub struct SpeakerWithData {
speaker: Speaker,
speaker_action_sender: Arc<broadcast::Sender<(SpeakerWithData, SpeakerAction)>>,
audio_data_sender: Arc<broadcast::Sender<f32>>,
}
impl SpeakerWithData {
pub fn new(speaker: Speaker) -> Self {
let speaker_action_sender = broadcast::channel(SPEAKER_ACTION_CHANNEL_LENGTH).0.into();
let audio_data_sender = broadcast::channel(AUDIO_DATA_SENDER_CHANNEL_LENGTH)
.0
.into();
Self {
speaker,
speaker_action_sender,
audio_data_sender,
}
}
pub fn get_speaker_id(&self) -> u8 {
self.speaker.get_id()
}
pub fn get_speaker(&self) -> Speaker {
self.speaker
}
pub fn send_speaker_action(&self, speaker_with_data_and_action: SpeakerWithDataAndAction) {
let _ = self
.speaker_action_sender
.send(speaker_with_data_and_action);
}
pub fn clone_audio_data_sender(&self) -> Arc<broadcast::Sender<f32>> {
self.audio_data_sender.clone()
}
pub fn subscribe_speaker_action_channel(
&self,
) -> broadcast::Receiver<(SpeakerWithData, SpeakerAction)> {
self.speaker_action_sender.subscribe()
}
pub fn subscribe_audio_data_channel(&self) -> broadcast::Receiver<f32> {
self.audio_data_sender.subscribe()
}
}
#[derive(Debug, Clone, Copy, Encode, Decode)]
pub enum SignalType {
AudioDatum,
SpeakerLeft,
}
#[derive(Debug, Encode, Decode)]
#[derive(Debug, Clone, Copy, Encode, Decode)]
pub struct Signal {
signal_type: SignalType,
speaker: Speaker,
data: [u8; SIGNAL_DATA_LENGTH],
data: SignalBufferReturn,
}
impl Signal {
pub fn get_signal_type(&self) -> SignalType {
self.signal_type
}
pub fn get_speaker_id(&self) -> u8 {
self.speaker.get_id()
}
pub fn get_audio_datum(&self) -> f32 {
f32::from_be_bytes(self.data)
}
pub fn unpack(data: NetworkBufferReturn) -> Result<Self, Error> {
Ok(bincode::decode_from_slice::<Self, _>(&data, BINCODE_CONFIG)
.map_err(|inner| Error::Deserialization(inner.to_string()))?