Add support for RFC4566 framerate

This commit is contained in:
João Antônio Cardoso 2024-02-15 01:03:23 -03:00
Родитель e7badc269e
Коммит 3ff6f75a45
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 2FE226A6824DFA7F
2 изменённых файлов: 52 добавлений и 0 удалений

Просмотреть файл

@ -1263,6 +1263,7 @@ pub enum SdpAttribute {
ExtmapAllowMixed,
Fingerprint(SdpAttributeFingerprint),
Fmtp(SdpAttributeFmtp),
FrameRate(f64),
Group(SdpAttributeGroup),
IceLite,
IceMismatch,
@ -1305,6 +1306,7 @@ impl SdpAttribute {
SdpAttribute::BundleOnly
| SdpAttribute::Candidate(..)
| SdpAttribute::Fmtp(..)
| SdpAttribute::FrameRate(..)
| SdpAttribute::IceMismatch
| SdpAttribute::ImageAttr(..)
| SdpAttribute::Label(..)
@ -1364,6 +1366,7 @@ impl SdpAttribute {
| SdpAttribute::ExtmapAllowMixed
| SdpAttribute::Fingerprint(..)
| SdpAttribute::Fmtp(..)
| SdpAttribute::FrameRate(..)
| SdpAttribute::IceMismatch
| SdpAttribute::IceOptions(..)
| SdpAttribute::IcePwd(..)
@ -1451,6 +1454,7 @@ impl FromStr for SdpAttribute {
"extmap" => parse_extmap(val),
"fingerprint" => parse_fingerprint(val),
"fmtp" => parse_fmtp(val),
"framerate" => parse_framerate(val),
"group" => parse_group(val),
"ice-options" => parse_ice_options(val),
"msid" => parse_msid(val),
@ -1482,6 +1486,7 @@ impl fmt::Display for SdpAttribute {
SdpAttribute::ExtmapAllowMixed => SdpAttributeType::ExtmapAllowMixed.to_string(),
SdpAttribute::Fingerprint(ref a) => attr_to_string(a.to_string()),
SdpAttribute::Fmtp(ref a) => attr_to_string(a.to_string()),
SdpAttribute::FrameRate(ref a) => attr_to_string(a.to_string()),
SdpAttribute::Group(ref a) => attr_to_string(a.to_string()),
SdpAttribute::IceLite => SdpAttributeType::IceLite.to_string(),
SdpAttribute::IceMismatch => SdpAttributeType::IceMismatch.to_string(),
@ -1549,6 +1554,7 @@ pub enum SdpAttributeType {
ExtmapAllowMixed,
Fingerprint,
Fmtp,
FrameRate,
Group,
IceLite,
IceMismatch,
@ -1596,6 +1602,7 @@ impl<'a> From<&'a SdpAttribute> for SdpAttributeType {
SdpAttribute::ExtmapAllowMixed { .. } => SdpAttributeType::ExtmapAllowMixed,
SdpAttribute::Fingerprint { .. } => SdpAttributeType::Fingerprint,
SdpAttribute::Fmtp { .. } => SdpAttributeType::Fmtp,
SdpAttribute::FrameRate { .. } => SdpAttributeType::FrameRate,
SdpAttribute::Group { .. } => SdpAttributeType::Group,
SdpAttribute::IceLite { .. } => SdpAttributeType::IceLite,
SdpAttribute::IceMismatch { .. } => SdpAttributeType::IceMismatch,
@ -1645,6 +1652,7 @@ impl fmt::Display for SdpAttributeType {
SdpAttributeType::ExtmapAllowMixed => "extmap-allow-mixed",
SdpAttributeType::Fingerprint => "fingerprint",
SdpAttributeType::Fmtp => "fmtp",
SdpAttributeType::FrameRate => "framerate",
SdpAttributeType::Group => "group",
SdpAttributeType::IceLite => "ice-lite",
SdpAttributeType::IceMismatch => "ice-mismatch",
@ -2265,6 +2273,34 @@ fn parse_fmtp(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
}))
}
///////////////////////////////////////////////////////////////////////////
// a=framerate, RFC4566, RFC8866
//-------------------------------------------------------------------------
// a=framerate:<framerate-value>
fn parse_framerate(to_parse: &str) -> Result<SdpAttribute, SdpParserInternalError> {
let framerate: f64 = to_parse.parse()?;
if framerate.is_nan() {
return Err(SdpParserInternalError::Generic(
"FrameRate attribute must be a number".to_string(),
));
}
if framerate.is_infinite() {
return Err(SdpParserInternalError::Generic(
"FrameRate attribute cannot be finite".to_string(),
));
}
if framerate == 0.0 {
return Err(SdpParserInternalError::Generic(
"FrameRate attribute cannot be zero".to_string(),
));
}
Ok(SdpAttribute::FrameRate(framerate))
}
///////////////////////////////////////////////////////////////////////////
// a=group, RFC5888
//-------------------------------------------------------------------------

Просмотреть файл

@ -376,6 +376,22 @@ fn test_parse_attribute_fmtp() {
);
}
#[test]
fn test_parse_attribute_framerate() {
let check_parse = make_check_parse!(f64, SdpAttribute::FrameRate);
let check_parse_and_serialize =
make_check_parse_and_serialize!(check_parse, SdpAttribute::FrameRate);
check_parse_and_serialize("framerate:1");
check_parse_and_serialize("framerate:60");
check_parse_and_serialize("framerate:23.5");
check_parse_and_serialize("framerate:4294967297");
assert!(parse_attribute("framerate:").is_err());
assert!(parse_attribute("framerate:abc").is_err());
assert!(parse_attribute("framerate:0").is_err());
}
#[test]
fn test_anonymize_attribute_fingerprint() -> Result<(), SdpParserInternalError> {
let mut anon = StatefulSdpAnonymizer::new();