use std::fmt;
use shared::error::{Error, Result};
use crate::description::common::Attribute;
mod grouping;
mod ice;
mod informational;
pub mod mux;
mod rtcp;
mod rtp;
mod sctp;
mod security;
mod simulcast;
mod source;
mod validate;
#[cfg(test)]
mod attribute_test;
pub use grouping::{Depend, Dependency, DependencyType, Group, Mid, Msid, MsidSemantic};
pub use ice::{
Candidate, IceOptions, IcePacing, IcePwd, IceUfrag, RemoteCandidate, RemoteCandidates,
};
pub use informational::{ConferenceType, Orient, Quality, Tool};
pub use rtcp::{
Rtcp, RtcpAddress, RtcpFb, RtcpFbKind, RtcpFbPayloadType, RtcpXr, RtcpXrFormat, RtcpXrRttMode,
RtcpXrStatFlag,
};
pub use rtp::{
Content, Fmtp, FrameRate, ImageAttr, ImageAttrList, ImageAttrRange, ImageAttrSet, MaxPtime,
Ptime, RtpMap,
};
pub use sctp::{MaxMessageSize, SctpMap, SctpPort};
pub use security::{Fingerprint, Identity, Setup, TlsId};
pub use simulcast::{Rid, RidDirection, Simulcast, SimulcastId};
pub use source::{Ssrc, SsrcGroup};
pub use validate::check_attribute;
pub trait TypedAttribute: Sized + fmt::Display {
const NAME: &'static str;
fn parse(value: &str) -> Result<Self>;
fn to_attribute(&self) -> Attribute {
Attribute::new(Self::NAME.to_owned(), Some(self.to_string()))
}
}
fn no_value(name: &str) -> Error {
Error::SdpInvalidSyntax(format!("`a={name}` has no value"))
}
pub(crate) fn first<T: TypedAttribute>(attributes: &[Attribute]) -> Result<Option<T>> {
attributes
.iter()
.map(Attribute::parts)
.find(|(name, _)| *name == T::NAME)
.map(|(_, value)| T::parse(value.ok_or_else(|| no_value(T::NAME))?))
.transpose()
}
pub(crate) fn all<T: TypedAttribute>(attributes: &[Attribute]) -> Result<Vec<T>> {
attributes
.iter()
.map(Attribute::parts)
.filter(|(name, _)| *name == T::NAME)
.map(|(_, value)| T::parse(value.ok_or_else(|| no_value(T::NAME))?))
.collect()
}
pub(crate) fn flag(attributes: &[Attribute], name: &str) -> Result<bool> {
match attributes
.iter()
.map(Attribute::parts)
.find(|(n, _)| *n == name)
{
None => Ok(false),
Some((_, None)) => Ok(true),
Some(_) => Err(Error::SdpInvalidSyntax(format!(
"`a={name}` takes no value"
))),
}
}
macro_rules! string_attribute {
($(#[$doc:meta])* $name:ident, $key:literal, $what:literal, |$v:ident| $check:expr) => {
$(#[$doc])*
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct $name(String);
impl $name {
pub fn new(value: impl Into<String>) -> shared::error::Result<Self> {
let value = value.into();
<Self as $crate::attribute::TypedAttribute>::parse(&value)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl $crate::attribute::TypedAttribute for $name {
const NAME: &'static str = $key;
fn parse(value: &str) -> shared::error::Result<Self> {
let $v = value;
if $check {
Ok($name(value.to_owned()))
} else {
Err($crate::grammar::syntax($what, value))
}
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
};
}
pub(crate) use string_attribute;
macro_rules! common_accessors {
() => {
pub fn typed<T: $crate::attribute::TypedAttribute>(
&self,
) -> shared::error::Result<Option<T>> {
$crate::attribute::first(&self.attributes)
}
pub fn typed_all<T: $crate::attribute::TypedAttribute>(
&self,
) -> shared::error::Result<Vec<T>> {
$crate::attribute::all(&self.attributes)
}
pub fn flag(&self, name: &str) -> shared::error::Result<bool> {
$crate::attribute::flag(&self.attributes, name)
}
pub fn with_typed<T: $crate::attribute::TypedAttribute>(mut self, value: &T) -> Self {
self.attributes.push(value.to_attribute());
self
}
pub fn direction(&self) -> shared::error::Result<Option<$crate::direction::Direction>> {
let mut found = None;
for attribute in &self.attributes {
let (name, value) = attribute.parts();
let direction = $crate::direction::Direction::new(name);
if direction == $crate::direction::Direction::Unspecified {
continue;
}
if value.is_some() || found.is_some() {
return Err(shared::error::Error::SdpInvalidSyntax(format!(
"direction attribute `{name}`"
)));
}
found = Some(direction);
}
Ok(found)
}
pub fn extmaps(&self) -> shared::error::Result<Vec<$crate::extmap::ExtMap>> {
self.typed_all()
}
pub fn extmap_allow_mixed(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_EXTMAP_ALLOW_MIXED)
}
pub fn ice_ufrag(&self) -> shared::error::Result<Option<$crate::attribute::IceUfrag>> {
self.typed()
}
pub fn ice_pwd(&self) -> shared::error::Result<Option<$crate::attribute::IcePwd>> {
self.typed()
}
pub fn ice_options(&self) -> shared::error::Result<Option<$crate::attribute::IceOptions>> {
self.typed()
}
pub fn fingerprints(&self) -> shared::error::Result<Vec<$crate::attribute::Fingerprint>> {
self.typed_all()
}
pub fn setup(&self) -> shared::error::Result<Option<$crate::attribute::Setup>> {
self.typed()
}
pub fn tls_id(&self) -> shared::error::Result<Option<$crate::attribute::TlsId>> {
self.typed()
}
pub fn rtcp_xr(&self) -> shared::error::Result<Option<$crate::attribute::RtcpXr>> {
self.typed()
}
pub fn cryptex(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_CRYPTEX)
}
};
}
pub(crate) use common_accessors;
macro_rules! session_accessors {
() => {
pub fn groups(&self) -> shared::error::Result<Vec<$crate::attribute::Group>> {
self.typed_all()
}
pub fn bundle_groups(&self) -> shared::error::Result<Vec<$crate::attribute::Group>> {
Ok(self
.groups()?
.into_iter()
.filter(|g| g.is_bundle())
.collect())
}
pub fn ice_lite(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_ICELITE)
}
pub fn ice_pacing(&self) -> shared::error::Result<Option<$crate::attribute::IcePacing>> {
self.typed()
}
pub fn identity(&self) -> shared::error::Result<Option<$crate::attribute::Identity>> {
self.typed()
}
pub fn tool(&self) -> shared::error::Result<Option<$crate::attribute::Tool>> {
self.typed()
}
pub fn conference_type(
&self,
) -> shared::error::Result<Option<$crate::attribute::ConferenceType>> {
self.typed()
}
pub fn msid_semantic(
&self,
) -> shared::error::Result<Option<$crate::attribute::MsidSemantic>> {
self.typed()
}
};
}
pub(crate) use session_accessors;
macro_rules! media_accessors {
() => {
pub fn mid(&self) -> shared::error::Result<Option<$crate::attribute::Mid>> {
self.typed()
}
pub fn msids(&self) -> shared::error::Result<Vec<$crate::attribute::Msid>> {
self.typed_all()
}
pub fn ssrcs(&self) -> shared::error::Result<Vec<$crate::attribute::Ssrc>> {
self.typed_all()
}
pub fn ssrc_groups(&self) -> shared::error::Result<Vec<$crate::attribute::SsrcGroup>> {
self.typed_all()
}
pub fn rids(&self) -> shared::error::Result<Vec<$crate::attribute::Rid>> {
self.typed_all()
}
pub fn simulcast(&self) -> shared::error::Result<Option<$crate::attribute::Simulcast>> {
self.typed()
}
pub fn rtcp(&self) -> shared::error::Result<Option<$crate::attribute::Rtcp>> {
self.typed()
}
pub fn rtcp_mux(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_RTCPMUX)
}
pub fn rtcp_mux_only(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_RTCPMUX_ONLY)
}
pub fn rtcp_rsize(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_RTCPRSIZE)
}
pub fn rtcp_fbs(&self) -> shared::error::Result<Vec<$crate::attribute::RtcpFb>> {
self.typed_all()
}
pub fn rtpmaps(&self) -> shared::error::Result<Vec<$crate::attribute::RtpMap>> {
self.typed_all()
}
pub fn fmtps(&self) -> shared::error::Result<Vec<$crate::attribute::Fmtp>> {
self.typed_all()
}
pub fn ptime(&self) -> shared::error::Result<Option<$crate::attribute::Ptime>> {
self.typed()
}
pub fn maxptime(&self) -> shared::error::Result<Option<$crate::attribute::MaxPtime>> {
self.typed()
}
pub fn framerate(&self) -> shared::error::Result<Option<$crate::attribute::FrameRate>> {
self.typed()
}
pub fn imageattrs(&self) -> shared::error::Result<Vec<$crate::attribute::ImageAttr>> {
self.typed_all()
}
pub fn content(&self) -> shared::error::Result<Option<$crate::attribute::Content>> {
self.typed()
}
pub fn candidates(&self) -> shared::error::Result<Vec<$crate::attribute::Candidate>> {
self.typed_all()
}
pub fn end_of_candidates(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_END_OF_CANDIDATES)
}
pub fn remote_candidates(
&self,
) -> shared::error::Result<Option<$crate::attribute::RemoteCandidates>> {
self.typed()
}
pub fn sctp_port(&self) -> shared::error::Result<Option<$crate::attribute::SctpPort>> {
self.typed()
}
pub fn max_message_size(
&self,
) -> shared::error::Result<Option<$crate::attribute::MaxMessageSize>> {
self.typed()
}
pub fn sctpmap(&self) -> shared::error::Result<Option<$crate::attribute::SctpMap>> {
self.typed()
}
pub fn orient(&self) -> shared::error::Result<Option<$crate::attribute::Orient>> {
self.typed()
}
pub fn quality(&self) -> shared::error::Result<Option<$crate::attribute::Quality>> {
self.typed()
}
pub fn bundle_only(&self) -> shared::error::Result<bool> {
self.flag($crate::description::session::ATTR_KEY_BUNDLE_ONLY)
}
};
}
pub(crate) use media_accessors;