#[cfg(test)]
mod source_description_items_test;
#[cfg(test)]
mod source_description_test;
use crate::{header::*, packet::*, util::*};
use shared::{
error::{Error, Result},
marshal::{Marshal, MarshalSize, Unmarshal},
};
use bytes::{Buf, BufMut, Bytes};
use std::any::Any;
use std::fmt;
const SDES_SOURCE_LEN: usize = 4;
const SDES_TYPE_LEN: usize = 1;
const SDES_TYPE_OFFSET: usize = 0;
const SDES_OCTET_COUNT_LEN: usize = 1;
const SDES_OCTET_COUNT_OFFSET: usize = 1;
const SDES_MAX_OCTET_COUNT: usize = (1 << 8) - 1;
const SDES_TEXT_OFFSET: usize = 2;
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SdesType {
#[default]
SdesEnd, SdesCname, SdesName, SdesEmail, SdesPhone, SdesLocation, SdesTool, SdesNote, SdesPrivate, SdesRtpStreamId, SdesRepairedRtpStreamId, SdesMid, Other(u8),
}
impl fmt::Display for SdesType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
SdesType::SdesEnd => "END",
SdesType::SdesCname => "CNAME",
SdesType::SdesName => "NAME",
SdesType::SdesEmail => "EMAIL",
SdesType::SdesPhone => "PHONE",
SdesType::SdesLocation => "LOC",
SdesType::SdesTool => "TOOL",
SdesType::SdesNote => "NOTE",
SdesType::SdesPrivate => "PRIV",
SdesType::SdesRtpStreamId => "RtpStreamId",
SdesType::SdesRepairedRtpStreamId => "RepairedRtpStreamId",
SdesType::SdesMid => "MID",
SdesType::Other(t) => return write!(f, "SDES{t}"),
};
write!(f, "{s}")
}
}
impl From<u8> for SdesType {
fn from(b: u8) -> Self {
match b {
0 => SdesType::SdesEnd,
1 => SdesType::SdesCname,
2 => SdesType::SdesName,
3 => SdesType::SdesEmail,
4 => SdesType::SdesPhone,
5 => SdesType::SdesLocation,
6 => SdesType::SdesTool,
7 => SdesType::SdesNote,
8 => SdesType::SdesPrivate,
12 => SdesType::SdesRtpStreamId,
13 => SdesType::SdesRepairedRtpStreamId,
15 => SdesType::SdesMid,
other => SdesType::Other(other),
}
}
}
impl From<SdesType> for u8 {
fn from(t: SdesType) -> Self {
match t {
SdesType::SdesEnd => 0,
SdesType::SdesCname => 1,
SdesType::SdesName => 2,
SdesType::SdesEmail => 3,
SdesType::SdesPhone => 4,
SdesType::SdesLocation => 5,
SdesType::SdesTool => 6,
SdesType::SdesNote => 7,
SdesType::SdesPrivate => 8,
SdesType::SdesRtpStreamId => 12,
SdesType::SdesRepairedRtpStreamId => 13,
SdesType::SdesMid => 15,
SdesType::Other(other) => other,
}
}
}
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct SourceDescriptionChunk {
pub source: u32,
pub items: Vec<SourceDescriptionItem>,
}
impl SourceDescriptionChunk {
fn raw_size(&self) -> usize {
let mut len = SDES_SOURCE_LEN;
for it in &self.items {
len += it.marshal_size();
}
len += SDES_TYPE_LEN; len
}
}
impl MarshalSize for SourceDescriptionChunk {
fn marshal_size(&self) -> usize {
let l = self.raw_size();
l + get_padding_size(l)
}
}
impl Marshal for SourceDescriptionChunk {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if buf.remaining_mut() < self.marshal_size() {
return Err(Error::BufferTooShort);
}
buf.put_u32(self.source);
for it in &self.items {
let n = it.marshal_to(buf)?;
buf = &mut buf[n..];
}
buf.put_u8(SdesType::SdesEnd.into());
buf.put_bytes(0, get_padding_size(self.raw_size()));
Ok(self.marshal_size())
}
}
impl Unmarshal for SourceDescriptionChunk {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
if raw_packet.remaining() < (SDES_SOURCE_LEN + SDES_TYPE_LEN) {
return Err(Error::PacketTooShort);
}
let source = raw_packet.get_u32();
let mut offset = SDES_SOURCE_LEN;
let mut items = vec![];
loop {
let item = SourceDescriptionItem::unmarshal(raw_packet)?;
if item.sdes_type == SdesType::SdesEnd {
let padding_len = get_padding_size(offset + 1);
if raw_packet.remaining() < padding_len {
return Err(Error::PacketTooShort);
}
raw_packet.advance(padding_len);
return Ok(SourceDescriptionChunk { source, items });
}
offset += item.marshal_size();
items.push(item);
}
}
}
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct SourceDescriptionItem {
pub sdes_type: SdesType,
pub text: Bytes,
}
impl SourceDescriptionItem {
pub fn private(prefix: &[u8], value: &[u8]) -> Result<Self> {
if 1 + prefix.len() + value.len() > SDES_MAX_OCTET_COUNT {
return Err(Error::SdesTextTooLong);
}
let mut text = Vec::with_capacity(1 + prefix.len() + value.len());
text.push(prefix.len() as u8);
text.extend_from_slice(prefix);
text.extend_from_slice(value);
Ok(SourceDescriptionItem {
sdes_type: SdesType::SdesPrivate,
text: Bytes::from(text),
})
}
pub fn as_private(&self) -> Option<(Bytes, Bytes)> {
if self.sdes_type != SdesType::SdesPrivate {
return None;
}
let prefix_len = *self.text.first()? as usize;
if 1 + prefix_len > self.text.len() {
return None;
}
Some((
self.text.slice(1..1 + prefix_len),
self.text.slice(1 + prefix_len..),
))
}
}
impl MarshalSize for SourceDescriptionItem {
fn marshal_size(&self) -> usize {
SDES_TYPE_LEN + SDES_OCTET_COUNT_LEN + self.text.len()
}
}
impl Marshal for SourceDescriptionItem {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
let sdes_type = u8::from(self.sdes_type);
if sdes_type == 0 {
return Err(Error::SdesMissingType);
}
if buf.remaining_mut() < self.marshal_size() {
return Err(Error::BufferTooShort);
}
buf.put_u8(sdes_type);
if self.text.len() > SDES_MAX_OCTET_COUNT {
return Err(Error::SdesTextTooLong);
}
buf.put_u8(self.text.len() as u8);
buf.put(self.text.clone());
Ok(self.marshal_size())
}
}
impl Unmarshal for SourceDescriptionItem {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
let raw_packet_len = raw_packet.remaining();
if raw_packet_len < SDES_TYPE_LEN {
return Err(Error::PacketTooShort);
}
let sdes_type = SdesType::from(raw_packet.get_u8());
if sdes_type == SdesType::SdesEnd {
return Ok(SourceDescriptionItem {
sdes_type,
text: Bytes::new(),
});
}
if raw_packet_len < (SDES_TYPE_LEN + SDES_OCTET_COUNT_LEN) {
return Err(Error::PacketTooShort);
}
let octet_count = raw_packet.get_u8() as usize;
if SDES_TEXT_OFFSET + octet_count > raw_packet_len {
return Err(Error::PacketTooShort);
}
let text = raw_packet.copy_to_bytes(octet_count);
Ok(SourceDescriptionItem { sdes_type, text })
}
}
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct SourceDescription {
pub chunks: Vec<SourceDescriptionChunk>,
}
impl fmt::Display for SourceDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut out = "Source Description:\n".to_string();
for c in &self.chunks {
out += format!("\t{:x}\n", c.source).as_str();
for it in &c.items {
out += format!("\t\t{it:?}\n").as_str();
}
}
write!(f, "{out}")
}
}
impl Packet for SourceDescription {
fn header(&self) -> Header {
Header {
padding: false,
count: self.chunks.len() as u8,
packet_type: PacketType::SourceDescription,
length: ((self.marshal_size() / 4) - 1) as u16,
}
}
fn destination_ssrc(&self) -> Vec<u32> {
self.chunks.iter().map(|x| x.source).collect()
}
fn raw_size(&self) -> usize {
let mut chunks_length = 0;
for c in &self.chunks {
chunks_length += c.marshal_size();
}
HEADER_LENGTH + chunks_length
}
fn as_any(&self) -> &dyn Any {
self
}
fn equal(&self, other: &dyn Packet) -> bool {
other.as_any().downcast_ref::<SourceDescription>() == Some(self)
}
fn cloned(&self) -> Box<dyn Packet> {
Box::new(self.clone())
}
}
impl MarshalSize for SourceDescription {
fn marshal_size(&self) -> usize {
let l = self.raw_size();
l + get_padding_size(l)
}
}
impl Marshal for SourceDescription {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if self.chunks.len() > COUNT_MAX {
return Err(Error::TooManyChunks);
}
if buf.remaining_mut() < self.marshal_size() {
return Err(Error::BufferTooShort);
}
let h = self.header();
let n = h.marshal_to(buf)?;
buf = &mut buf[n..];
for c in &self.chunks {
let n = c.marshal_to(buf)?;
buf = &mut buf[n..];
}
if h.padding {
put_padding(buf, self.raw_size());
}
Ok(self.marshal_size())
}
}
impl Unmarshal for SourceDescription {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
let (h, mut body) = read_packet(raw_packet)?;
if h.packet_type != PacketType::SourceDescription {
return Err(Error::WrongType);
}
let mut chunks = Vec::with_capacity(h.count as usize);
for _ in 0..h.count {
chunks.push(SourceDescriptionChunk::unmarshal(&mut body)?);
}
if body.has_remaining() {
return Err(Error::InvalidHeader);
}
Ok(SourceDescription { chunks })
}
}