use alloc::string::{String, ToString};
use alloc::vec::Vec;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SdpMediaType {
Video,
Audio,
Text,
Application,
Message,
Other(OtherMediaType),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OtherMediaType(String);
impl OtherMediaType {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidSdpToken;
impl core::fmt::Display for InvalidSdpToken {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("not an RFC 4566 token")
}
}
impl core::error::Error for InvalidSdpToken {}
const fn is_token_char(byte: u8) -> bool {
matches!(byte, 0x21 | 0x23..=0x27 | 0x2A | 0x2B | 0x2D | 0x2E | 0x30..=0x39 | 0x41..=0x5A | 0x5E..=0x7E)
}
impl SdpMediaType {
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::Video => "video",
Self::Audio => "audio",
Self::Text => "text",
Self::Application => "application",
Self::Message => "message",
Self::Other(token) => token.as_str(),
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::Video => "video",
Self::Audio => "audio",
Self::Text => "text",
Self::Application => "application",
Self::Message => "message",
Self::Other(_) => "other",
}
}
}
impl SdpMediaType {
pub fn parse(token: &str) -> Result<Self, InvalidSdpToken> {
if token.is_empty() || !token.bytes().all(is_token_char) {
return Err(InvalidSdpToken);
}
Ok(match token {
"video" => Self::Video,
"audio" => Self::Audio,
"text" => Self::Text,
"application" => Self::Application,
"message" => Self::Message,
other => Self::Other(OtherMediaType(other.to_string())),
})
}
}
impl core::fmt::Display for SdpMediaType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SdpAttribute {
pub name: String,
pub value: Option<String>,
}
impl SdpAttribute {
#[must_use]
pub fn with_value(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: Some(value.into()),
}
}
#[must_use]
pub fn property(name: impl Into<String>) -> Self {
Self {
name: name.into(),
value: None,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SdpMedia {
pub media_type: SdpMediaType,
pub port: u16,
pub protocol: String,
pub formats: String,
pub attributes: Vec<SdpAttribute>,
}
pub const RTP_AVP: &str = "RTP/AVP";
impl SdpMedia {
#[must_use]
pub fn rtp_avp(media_type: SdpMediaType, formats: impl Into<String>) -> Self {
Self {
media_type,
port: 0,
protocol: RTP_AVP.to_string(),
formats: formats.into(),
attributes: Vec::new(),
}
}
pub fn add_attribute_with_value(&mut self, name: impl Into<String>, value: impl Into<String>) {
self.attributes.push(SdpAttribute::with_value(name, value));
}
#[must_use]
pub fn get_first_attribute_value(&self, name: &str) -> Option<Option<&str>> {
self.attributes
.iter()
.find(|attribute| attribute.name == name)
.map(|attribute| attribute.value.as_deref())
}
pub fn attribute_values<'a, 'n>(
&'a self,
name: &'n str,
) -> impl Iterator<Item = &'a str> + use<'a, 'n> {
self.attributes
.iter()
.filter(move |attribute| attribute.name == name)
.filter_map(|attribute| attribute.value.as_deref())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn media_type_tokens_round_trip() {
for token in [
"video",
"audio",
"text",
"application",
"message",
"control",
] {
let media_type = SdpMediaType::parse(token).expect("a token");
assert_eq!(media_type.as_str(), token);
assert_eq!(SdpMediaType::parse(media_type.as_str()), Ok(media_type));
}
assert_eq!(
SdpMediaType::parse("control").expect("a token").name(),
"other"
);
}
#[test]
fn a_registered_token_has_one_value_and_a_non_token_is_refused() {
assert_eq!(SdpMediaType::parse("video"), Ok(SdpMediaType::Video));
for bad in ["", "vid eo", "video\r\nm=audio", "a=b", "x\u{e9}"] {
assert_eq!(SdpMediaType::parse(bad), Err(InvalidSdpToken), "{bad:?}");
}
}
#[test]
fn attribute_lookup_keeps_wire_order() {
let mut media = SdpMedia::rtp_avp(SdpMediaType::Video, "96");
media.add_attribute_with_value("rtpmap", "96 H264/90000");
media.attributes.push(SdpAttribute::property("recvonly"));
media.add_attribute_with_value("fmtp", "96 a=b");
media.add_attribute_with_value("fmtp", "96 c=d");
assert_eq!(
media.get_first_attribute_value("rtpmap"),
Some(Some("96 H264/90000"))
);
assert_eq!(media.get_first_attribute_value("recvonly"), Some(None));
assert_eq!(media.get_first_attribute_value("control"), None);
assert_eq!(
media.attribute_values("fmtp").collect::<Vec<_>>(),
["96 a=b", "96 c=d"]
);
}
}