rust_voice_chat_room/protocol/src/protocol.rs

164 lines
4.1 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use bincode::{Decode, Encode};
use tokio::sync::broadcast;
use crate::Error;
const SIGNAL_DATA_LENGTH: usize = 4;
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];
static BINCODE_CONFIG: bincode::config::Configuration<bincode::config::BigEndian> =
bincode::config::standard().with_big_endian();
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
pub struct Speaker {
id: u8,
}
impl Speaker {
pub fn new(id: u8) -> Self {
Self { id }
}
pub fn get_id(&self) -> u8 {
self.id
}
}
#[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, Clone, Copy, Encode, Decode)]
pub struct Signal {
signal_type: SignalType,
speaker: Speaker,
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()))?
.0)
}
fn pack(self) -> NetworkBufferReturn {
let encoded = serialize(self);
assert_eq!(encoded.len(), NETWORK_DATA_LENGTH);
let mut buffer = NetworkBufferReturn::default();
buffer.copy_from_slice(&encoded);
buffer
}
pub fn pack_audio_datum(speaker: Speaker, audio_datum: f32) -> NetworkBufferReturn {
let signal = Self {
signal_type: SignalType::AudioDatum,
speaker,
data: audio_datum.to_be_bytes(),
};
Self::pack(signal)
}
pub fn pack_speaker_left(speaker: Speaker) -> NetworkBufferReturn {
let signal = Self {
signal_type: SignalType::SpeakerLeft,
speaker,
data: SignalBufferReturn::default(),
};
signal.pack()
}
}
fn serialize<E>(value: E) -> Vec<u8>
where
E: bincode::enc::Encode,
{
bincode::encode_to_vec(value, BINCODE_CONFIG)
.map_err(|inner| Error::Serialization(inner.to_string()))
.unwrap()
}