use shared::error::{Error, Result};
use super::mux::{self, AttributeLevel};
use super::*;
use crate::description::common::Attribute;
use crate::description::media::MediaDescription;
use crate::description::session::*;
use crate::extmap::ExtMap;
use crate::grammar;
const FLAGS: [&str; 12] = [
ATTR_KEY_SEND_RECV,
ATTR_KEY_SEND_ONLY,
ATTR_KEY_RECV_ONLY,
ATTR_KEY_INACTIVE,
ATTR_KEY_RTCPMUX,
ATTR_KEY_RTCPMUX_ONLY,
ATTR_KEY_RTCPRSIZE,
ATTR_KEY_ICELITE,
ATTR_KEY_END_OF_CANDIDATES,
ATTR_KEY_BUNDLE_ONLY,
ATTR_KEY_EXTMAP_ALLOW_MIXED,
ATTR_KEY_CRYPTEX,
];
fn check_value(name: &str, value: Option<&str>) -> Result<()> {
fn check<T: TypedAttribute>(value: Option<&str>) -> Result<()> {
let value = value
.ok_or_else(|| Error::SdpInvalidSyntax(format!("`a={}` has no value", T::NAME)))?;
T::parse(value).map(|_| ())
}
if FLAGS.contains(&name) {
return match value {
None => Ok(()),
Some(_) => Err(Error::SdpInvalidSyntax(format!(
"`a={name}` takes no value"
))),
};
}
match name {
"group" => check::<Group>(value),
"mid" => check::<Mid>(value),
"msid" => check::<Msid>(value),
"msid-semantic" => check::<MsidSemantic>(value),
"ssrc" => check::<Ssrc>(value),
"ssrc-group" => check::<SsrcGroup>(value),
"rid" => check::<Rid>(value),
"simulcast" => check::<Simulcast>(value),
"extmap" => check::<ExtMap>(value),
"rtcp" => check::<Rtcp>(value),
"rtcp-fb" => check::<RtcpFb>(value),
"rtcp-xr" => value.map_or(Ok(()), |v| RtcpXr::parse(v).map(|_| ())),
"rtpmap" => check::<RtpMap>(value),
"fmtp" => check::<Fmtp>(value),
"ptime" => check::<Ptime>(value),
"maxptime" => check::<MaxPtime>(value),
"framerate" => check::<FrameRate>(value),
"tool" => check::<Tool>(value),
"type" => check::<ConferenceType>(value),
"orient" => check::<Orient>(value),
"quality" => check::<Quality>(value),
"imageattr" => check::<ImageAttr>(value),
"content" => check::<Content>(value),
"candidate" => check::<Candidate>(value),
"remote-candidates" => check::<RemoteCandidates>(value),
"ice-ufrag" => check::<IceUfrag>(value),
"ice-pwd" => check::<IcePwd>(value),
"ice-options" => check::<IceOptions>(value),
"ice-pacing" => check::<IcePacing>(value),
"fingerprint" => check::<Fingerprint>(value),
"setup" => check::<Setup>(value),
"tls-id" => check::<TlsId>(value),
"identity" => check::<Identity>(value),
"sctp-port" => check::<SctpPort>(value),
"max-message-size" => check::<MaxMessageSize>(value),
"sctpmap" => check::<SctpMap>(value),
_ => {
if grammar::is_token(name)
&& value.is_none_or(|v| v.is_empty() || grammar::is_byte_string(v))
{
Ok(())
} else {
Err(Error::SdpInvalidSyntax(format!(
"attribute `{}`",
name.escape_debug()
)))
}
}
}
}
pub fn check_attribute(attribute: &Attribute) -> Result<()> {
let (name, value) = attribute.parts();
check_value(name, value)
}
fn check_level(name: &str, media: bool) -> Result<()> {
let Some(level) = mux::attribute(name).and_then(|row| row.level) else {
return Ok(());
};
let allowed = match (level, media) {
(AttributeLevel::Source, _) => false,
(level, true) => level.allows_media(),
(level, false) => level.allows_session(),
};
if allowed {
Ok(())
} else {
Err(Error::SdpInvalidValue(format!(
"`a={name}` at the {} level",
if media { "media" } else { "session" }
)))
}
}
const SINGLE: [&str; 15] = [
"mid",
"setup",
"ice-ufrag",
"ice-pwd",
"ice-options",
"ice-pacing",
"tls-id",
"simulcast",
"sctp-port",
"max-message-size",
"rtcp",
"ptime",
"maxptime",
"framerate",
"remote-candidates",
];
fn check_attributes(attributes: &[Attribute], media: bool) -> Result<()> {
let mut directions = 0usize;
for (index, attribute) in attributes.iter().enumerate() {
let (name, value) = attribute.parts();
check_value(name, value)?;
check_level(name, media)?;
if matches!(
name,
ATTR_KEY_SEND_RECV | ATTR_KEY_SEND_ONLY | ATTR_KEY_RECV_ONLY | ATTR_KEY_INACTIVE
) {
directions += 1;
}
if SINGLE.contains(&name) && attributes[..index].iter().any(|a| a.parts().0 == name) {
return Err(Error::SdpInvalidValue(format!(
"more than one `a={name}` at one level"
)));
}
}
if directions > 1 {
return Err(Error::SdpInvalidValue(
"more than one direction attribute at one level".to_owned(),
));
}
Ok(())
}
fn check_media(media: &MediaDescription, session_has_connection: bool) -> Result<()> {
if media.connection_information.is_empty() && !session_has_connection {
return Err(Error::SdpInvalidSyntax(format!(
"the `m={}` section has no `c=` line and the session has none",
media.media_name.media
)));
}
check_attributes(&media.attributes, true)?;
let rids = media.rids()?;
for (index, rid) in rids.iter().enumerate() {
if rids[..index].iter().any(|other| other.id == rid.id) {
return Err(Error::SdpInvalidValue(format!(
"`a=rid:{}` occurs twice in one media section",
rid.id
)));
}
}
if let Some(simulcast) = media.simulcast()? {
for id in simulcast.rids() {
if !rids.iter().any(|rid| rid.id == id) {
return Err(Error::SdpInvalidValue(format!(
"`a=simulcast` names the rid `{id}`, which has no `a=rid` line"
)));
}
}
}
if media.media_name.protos.iter().any(|p| p == "RTP") {
for format in &media.media_name.formats {
if !grammar::is_digits(format) || !format.parse::<u8>().is_ok_and(|pt| pt <= 127) {
return Err(Error::SdpInvalidValue(format!(
"`{format}` is not an RTP payload type"
)));
}
}
}
Ok(())
}
impl SessionDescription {
pub fn validate(&self) -> Result<()> {
check_attributes(&self.attributes, false)?;
let mut mids: Vec<String> = Vec::with_capacity(self.media_descriptions.len());
for media in &self.media_descriptions {
check_media(media, self.connection_information.is_some())?;
if let Some(mid) = media.mid()? {
if mids.iter().any(|m| m == mid.as_str()) {
return Err(Error::SdpInvalidValue(format!(
"`a=mid:{mid}` occurs twice"
)));
}
mids.push(mid.as_str().to_owned());
}
}
Ok(())
}
}