#[cfg(test)]
mod audio_level_extension_test;
use serde::{Deserialize, Serialize};
use shared::{
error::{Error, Result},
marshal::{Marshal, MarshalSize, Unmarshal},
};
use bytes::{Buf, BufMut};
pub const AUDIO_LEVEL_EXTENSION_SIZE: usize = 1;
#[derive(PartialEq, Eq, Debug, Default, Copy, Clone, Serialize, Deserialize)]
pub struct AudioLevelExtension {
pub level: u8,
pub voice: bool,
}
impl Unmarshal for AudioLevelExtension {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
if raw_packet.remaining() < AUDIO_LEVEL_EXTENSION_SIZE {
return Err(Error::ErrBufferTooSmall);
}
let b = raw_packet.get_u8();
Ok(AudioLevelExtension {
level: b & 0x7F,
voice: (b & 0x80) != 0,
})
}
}
impl MarshalSize for AudioLevelExtension {
fn marshal_size(&self) -> usize {
AUDIO_LEVEL_EXTENSION_SIZE
}
}
impl Marshal for AudioLevelExtension {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if buf.remaining_mut() < AUDIO_LEVEL_EXTENSION_SIZE {
return Err(Error::ErrBufferTooSmall);
}
if self.level > 127 {
return Err(Error::AudioLevelOverflow);
}
let voice = if self.voice { 0x80u8 } else { 0u8 };
buf.put_u8(voice | self.level);
Ok(AUDIO_LEVEL_EXTENSION_SIZE)
}
}