BlueGit

ict rusty-teams

Files

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

sdp_golden.rs

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