use bytes::{Bytes, BytesMut};
use std::any::Any;
use super::traits::PayloadFormat;
pub struct G711UPayloadFormat {
clock_rate: u32,
channels: u8,
preferred_duration: u32,
}
impl G711UPayloadFormat {
pub fn new(clock_rate: u32) -> Self {
Self {
clock_rate,
channels: 1,
preferred_duration: 20, }
}
}
impl PayloadFormat for G711UPayloadFormat {
fn payload_type(&self) -> u8 {
0 }
fn clock_rate(&self) -> u32 {
self.clock_rate
}
fn channels(&self) -> u8 {
self.channels
}
fn preferred_packet_duration(&self) -> u32 {
self.preferred_duration
}
fn packet_size_from_duration(&self, duration_ms: u32) -> usize {
let samples = self.samples_from_duration(duration_ms);
samples as usize * self.channels as usize
}
fn duration_from_packet_size(&self, packet_size: usize) -> u32 {
let samples = packet_size / self.channels as usize;
self.duration_from_samples(samples as u32)
}
fn pack(&self, media_data: &[u8], _timestamp: u32) -> Bytes {
Bytes::copy_from_slice(media_data)
}
fn unpack(&self, payload: &[u8], _timestamp: u32) -> Bytes {
Bytes::copy_from_slice(payload)
}
fn as_any(&self) -> &dyn Any {
self
}
}
pub struct G711APayloadFormat {
clock_rate: u32,
channels: u8,
preferred_duration: u32,
}
impl G711APayloadFormat {
pub fn new(clock_rate: u32) -> Self {
Self {
clock_rate,
channels: 1,
preferred_duration: 20, }
}
}
impl PayloadFormat for G711APayloadFormat {
fn payload_type(&self) -> u8 {
8 }
fn clock_rate(&self) -> u32 {
self.clock_rate
}
fn channels(&self) -> u8 {
self.channels
}
fn preferred_packet_duration(&self) -> u32 {
self.preferred_duration
}
fn packet_size_from_duration(&self, duration_ms: u32) -> usize {
let samples = self.samples_from_duration(duration_ms);
samples as usize * self.channels as usize
}
fn duration_from_packet_size(&self, packet_size: usize) -> u32 {
let samples = packet_size / self.channels as usize;
self.duration_from_samples(samples as u32)
}
fn pack(&self, media_data: &[u8], _timestamp: u32) -> Bytes {
Bytes::copy_from_slice(media_data)
}
fn unpack(&self, payload: &[u8], _timestamp: u32) -> Bytes {
Bytes::copy_from_slice(payload)
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_g711u_payload_format() {
let format = G711UPayloadFormat::new(8000);
assert_eq!(format.payload_type(), 0);
assert_eq!(format.clock_rate(), 8000);
assert_eq!(format.channels(), 1);
assert_eq!(format.preferred_packet_duration(), 20);
assert_eq!(format.samples_from_duration(20), 160);
assert_eq!(format.duration_from_samples(160), 20);
assert_eq!(format.packet_size_from_duration(20), 160);
assert_eq!(format.duration_from_packet_size(160), 20);
}
#[test]
fn test_g711a_payload_format() {
let format = G711APayloadFormat::new(8000);
assert_eq!(format.payload_type(), 8);
assert_eq!(format.clock_rate(), 8000);
assert_eq!(format.channels(), 1);
assert_eq!(format.preferred_packet_duration(), 20);
assert_eq!(format.samples_from_duration(20), 160);
assert_eq!(format.duration_from_samples(160), 20);
assert_eq!(format.packet_size_from_duration(20), 160);
assert_eq!(format.duration_from_packet_size(160), 20);
}
}