BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / tests / sdp_golden.rs

sdp_golden.rs

sdp_golden.rs 7.6 KiB Download Raw
1use calling::sdp::{self, OfferPlan, SessionDescription, from_teams_offer, to_browser_answer, to_teams_answer, to_teams_offer};
2
3const BROWSER_OFFER: &str = include_str!("fixtures/browser_offer.sdp");
4const TEAMS_OFFER: &str = include_str!("fixtures/teams_offer.sdp");
5const TEAMS_ANSWER: &str = include_str!("fixtures/teams_answer.sdp");
6const BROWSER_ANSWER: &str = include_str!("fixtures/browser_answer.sdp");
7
8fn captured_plan() -> OfferPlan {
9 OfferPlan {
10 screen_share_mids: vec!["18".into()],
11 gallery_mids: (10..=17).map(|mid| mid.to_string()).collect(),
12 }
13}
14
15fn counter() -> impl FnMut() -> u32 {
16 let mut next = 1_000_000u32;
17 move || {
18 next += 1;
19 next
20 }
21}
22
23fn is_number(token: &str) -> bool {
24 !token.is_empty() && token.bytes().all(|byte| byte.is_ascii_digit())
25}
26
27fn is_address(token: &str) -> bool {
28 let dotted_quad = token.split('.').count() == 4 && token.split('.').all(|part| part == "x" || is_number(part));
29 token == "IP4" || token == "IP6" || dotted_quad || token.matches(':').count() >= 2
30}
31
32/// Masks what differs per call or capture run: SSRCs, ports, addresses, candidate foundations, session ids.
33fn normalize(line: &str) -> String {
34 let line = line.trim_end_matches('\r');
35 if line.starts_with("o=") {
36 return "o=<origin>".into();
37 }
38 if let Some(rest) = line.strip_prefix("a=x-ssrc-range:") {
39 return format!("a=x-ssrc-range:{}", if rest == "1-1" { "1-1" } else { "<range>" });
40 }
41 let masked: Vec<String> = line
42 .split(' ')
43 .map(|token| {
44 let (prefix, value) = token.split_once(':').filter(|(prefix, _)| prefix.starts_with("a=")).unwrap_or(("", token));
45 let value_masked = if prefix.ends_with("cname") || token.starts_with("cname:") {
46 "<C>".to_owned()
47 } else if is_address(value) {
48 "<addr>".to_owned()
49 } else if is_number(value) && value.len() >= 4 && value != "8100" {
50 "<n>".to_owned()
51 } else if let Some((stream, _)) = value.rsplit_once('-').filter(|(_, number)| is_number(number)) {
52 format!("{stream}-<n>")
53 } else {
54 value.to_owned()
55 };
56 if prefix.is_empty() { value_masked } else { format!("{prefix}:{value_masked}") }
57 })
58 .collect();
59 masked.join(" ")
60}
61
62fn sections(description: &SessionDescription) -> Vec<Vec<String>> {
63 let text = sdp::write(description);
64 let mut sections = vec![Vec::new()];
65 for line in text.split("\r\n").filter(|line| !line.is_empty()) {
66 if line.starts_with("m=") {
67 sections.push(Vec::new());
68 }
69 sections.last_mut().unwrap().push(normalize(line));
70 }
71 sections
72}
73
74fn assert_same(actual: Vec<Vec<String>>, expected: Vec<Vec<String>>, skip: impl Fn(usize, &str) -> bool) {
75 assert_eq!(actual.len(), expected.len(), "section count");
76 for (index, (actual, expected)) in actual.iter().zip(&expected).enumerate() {
77 let actual: Vec<&String> = actual.iter().filter(|line| !skip(index, line)).collect();
78 let expected: Vec<&String> = expected.iter().filter(|line| !skip(index, line)).collect();
79 assert_eq!(actual, expected, "section {index}");
80 }
81}
82
83/// Teams pins the first H264 payload of each video line to its own High-profile fmtp; that is codec policy, not dialect.
84fn is_teams_codec_policy(line: &str) -> bool {
85 line.starts_with("a=fmtp:102 ")
86}
87
88#[test]
89fn captured_browser_offer_translates_to_the_captured_teams_offer() {
90 let offer = to_teams_offer(BROWSER_OFFER, &captured_plan(), &mut counter()).unwrap();
91 let actual = sections(&sdp::parse(&offer.sdp).unwrap());
92 let expected = sections(&sdp::parse(TEAMS_OFFER).unwrap());
93 assert_same(actual, expected, |_, line| is_teams_codec_policy(line));
94 assert_eq!(offer.lines.len(), 13);
95 assert_eq!(offer.lines[10].browser_mids.len(), 8);
96}
97
98/// The browser answer was captured in another run than the Teams answer, so per-run values are masked too:
99/// bandwidth, video fmtp levels and the per-run `x-mediabw` session lines.
100fn differs_per_run(index: usize, line: &str) -> bool {
101 line.starts_with("b=AS:")
102 || line.starts_with("a=x-mediabw:")
103 || (index > 1 && line.starts_with("a=fmtp:") && !line.contains("apt="))
104}
105
106#[test]
107fn captured_teams_answer_translates_to_the_captured_browser_answer() {
108 let offer = to_teams_offer(BROWSER_OFFER, &captured_plan(), &mut counter()).unwrap();
109 let answer = to_browser_answer(TEAMS_ANSWER, &offer, BROWSER_OFFER).unwrap();
110 let actual = sections(&sdp::parse(&answer).unwrap());
111 let expected = sections(&sdp::parse(BROWSER_ANSWER).unwrap());
112 assert_same(actual, expected, differs_per_run);
113}
114
115fn kinds(description: &SessionDescription) -> Vec<String> {
116 description.media.iter().map(|media| media.kind.clone()).collect()
117}
118
119#[test]
120fn captured_teams_offer_translates_to_a_browser_offer() {
121 let remote = from_teams_offer(TEAMS_OFFER).unwrap();
122 let browser = sdp::parse(&remote.browser_sdp).unwrap();
123 assert_eq!(browser.media.len(), 20);
124 let mids: Vec<&str> = browser.media.iter().filter_map(|media| media.mid()).collect();
125 assert_eq!(mids, (0..20).map(|mid| mid.to_string()).collect::<Vec<_>>().iter().map(String::as_str).collect::<Vec<_>>());
126 let kinds = kinds(&browser);
127 assert_eq!(kinds.iter().filter(|kind| *kind == "audio").count(), 1);
128 assert_eq!(kinds.iter().filter(|kind| *kind == "video").count(), 18);
129 assert_eq!(kinds.last().map(String::as_str), Some("application"));
130 assert!(browser.media.iter().all(|media| media.port == "9"));
131 assert!(browser.media[..19].iter().all(|media| media.proto == "UDP/TLS/RTP/SAVPF"));
132 assert!(!remote.browser_sdp.contains("x-ssrc-range") && !remote.browser_sdp.contains("a=label"));
133 assert!(!remote.browser_sdp.contains("10.10.10.10") && !remote.browser_sdp.contains("1234"));
134 assert!(remote.browser_sdp.contains("a=group:BUNDLE 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19"));
135 assert_eq!(remote.lines.len(), 13);
136 assert_eq!(remote.lines[10].browser_mids.len(), 8);
137}
138
139#[test]
140fn a_teams_offer_survives_the_trip_through_libwebrtc_form_and_back() {
141 let remote = from_teams_offer(TEAMS_OFFER).unwrap();
142 let back = to_teams_offer(&remote.browser_sdp, &remote.plan(), &mut counter()).unwrap();
143 let actual = sections(&sdp::parse(&back.sdp).unwrap());
144 let expected = sections(&sdp::parse(TEAMS_OFFER).unwrap());
145 let per_run_ssrcs = |_: usize, line: &str| {
146 is_teams_codec_policy(line) || line.starts_with("a=ssrc:") || line.starts_with("a=x-ssrc-range:")
147 };
148 assert_same(actual, expected, per_run_ssrcs);
149 assert_eq!(back.lines, remote.lines);
150}
151
152#[test]
153fn an_answer_to_a_teams_offer_comes_back_in_the_teams_dialect() {
154 let remote = from_teams_offer(TEAMS_OFFER).unwrap();
155 let answer = to_teams_answer(BROWSER_ANSWER, &remote, &mut counter()).unwrap();
156 let described = sdp::parse(&answer.sdp).unwrap();
157 let captured = sdp::parse(TEAMS_ANSWER).unwrap();
158 assert_eq!(described.media.len(), captured.media.len());
159 for (ours, theirs) in described.media.iter().zip(&captured.media) {
160 assert_eq!(ours.kind, theirs.kind);
161 assert_eq!(ours.value("label"), theirs.value("label"));
162 assert_eq!(ours.port, "1234");
163 assert_eq!(ours.proto, "RTP/SAVP");
164 }
165 assert_eq!(answer.lines, remote.lines);
166 let bundle = described
167 .session
168 .attributes
169 .iter()
170 .find(|attribute| attribute.name == "group")
171 .and_then(|attribute| attribute.value.clone())
172 .unwrap();
173 assert_eq!(bundle, "BUNDLE 0 1 2 3 4 5 6 7 8 9 17 18 19");
174}