Skip to content

Commit 02ea209

Browse files
committed
get typed attribute for particular pt optionally
1 parent 017b97f commit 02ea209

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,40 @@ impl Media {
942942

943943
/// Gets an iterator over all attribute values of the given name.
944944
/// Each item is a `Result` with the inferred type in `Ok` and `AttributeError` in `Err`
945+
/// The payload type can also be passed optionally to filter an Attribute for a particular payload format
945946
pub fn get_attribute_values_typed<'a, T: FromStr<Err = AttributeErr>>(
946947
&'a self,
947948
name: &'a str,
949+
pt: Option<&'a str>,
948950
) -> impl Iterator<Item = Result<T, AttributeErr>> + 'a {
949951
self.attributes
950952
.iter()
951953
.filter(move |a| a.attribute == name)
954+
.filter(move |a| {
955+
if let Some(pt) = pt {
956+
let mut iter = self.fmt.split_ascii_whitespace();
957+
let Some(value) = a.value.as_ref() else {
958+
// does not have a value for the attribute
959+
return false;
960+
};
961+
962+
let attr_pt = value.split_ascii_whitespace().next();
963+
964+
let Some(attr_pt) = attr_pt else {
965+
// does not have a payload type in the attribute value
966+
return false;
967+
};
968+
969+
iter.any(|fmt| {
970+
if pt == fmt && attr_pt == pt {
971+
return true;
972+
}
973+
false
974+
})
975+
} else {
976+
true
977+
}
978+
})
952979
.map(|a| {
953980
let Some(s) = &a.value else {
954981
// does not have a value for the attribute
@@ -1126,13 +1153,19 @@ a=fingerprint:sha-256 3A:96:6D:57:B2:C2:C7:61:A0:46:3E:1C:97:39:D3:F7:0A:88:A0:B
11261153
);
11271154

11281155
let v = media
1129-
.get_attribute_values_typed("rtpmap")
1156+
.get_attribute_values_typed("rtpmap", None)
11301157
.collect::<Vec<Result<RtpMap, AttributeErr>>>();
11311158
assert_eq!(v[0].as_ref().unwrap().clock_rate, 90000);
11321159
assert_eq!(v[1].as_ref().unwrap().encoding, "h264");
11331160
assert_eq!(v[2], Err(AttributeErr("No value for the attribute")));
11341161
assert_eq!(v[3].as_ref().unwrap().params.as_ref().unwrap(), "2");
11351162

1163+
let v = media
1164+
.get_attribute_values_typed("rtpmap", Some("99"))
1165+
.collect::<Vec<Result<RtpMap, AttributeErr>>>();
1166+
assert_eq!(v.len(), 1);
1167+
assert_eq!(v[0].as_ref().unwrap().encoding, "h263-1998");
1168+
11361169
assert_eq!(
11371170
media.get_first_attribute_value("fmtp"),
11381171
Ok(Some(
@@ -1141,7 +1174,7 @@ a=fingerprint:sha-256 3A:96:6D:57:B2:C2:C7:61:A0:46:3E:1C:97:39:D3:F7:0A:88:A0:B
11411174
);
11421175

11431176
let r = media
1144-
.get_attribute_values_typed("rtcp")
1177+
.get_attribute_values_typed("rtcp", None)
11451178
.collect::<Vec<Result<Rtcp, AttributeErr>>>();
11461179
assert_eq!(r[0].as_ref().unwrap().addrtype, AddrType::Ip4);
11471180
assert_eq!(r[0].as_ref().unwrap().port, 53020);

0 commit comments

Comments
 (0)