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