| 1 | use crate::sdp::{self, Attribute}; |
| 2 | |
| 3 | pub fn teams_candidate(browser: &str) -> Option<String> { |
| 4 | let line = browser |
| 5 | .trim() |
| 6 | .trim_start_matches("a=") |
| 7 | .trim_start_matches("candidate:"); |
| 8 | let tokens: Vec<&str> = line.split_whitespace().collect(); |
| 9 | let [ |
| 10 | foundation, |
| 11 | component, |
| 12 | transport, |
| 13 | priority, |
| 14 | address, |
| 15 | port, |
| 16 | "typ", |
| 17 | kind, |
| 18 | rest @ .., |
| 19 | ] = tokens.as_slice() |
| 20 | else { |
| 21 | return None; |
| 22 | }; |
| 23 | if address.ends_with(".local") { |
| 24 | return None; |
| 25 | } |
| 26 | let mut related = Vec::new(); |
| 27 | let mut tcp_type = None; |
| 28 | for pair in rest.chunks(2) { |
| 29 | match pair { |
| 30 | ["raddr", value] | ["rport", value] => related.push(format!("{} {value}", pair[0])), |
| 31 | ["tcptype", value] => tcp_type = Some(*value), |
| 32 | _ => {} |
| 33 | } |
| 34 | } |
| 35 | let transport = match (transport.to_ascii_lowercase().as_str(), tcp_type) { |
| 36 | ("udp", _) => "UDP", |
| 37 | ("tcp", Some("active")) => "TCP-ACT", |
| 38 | ("tcp", Some("passive")) => "TCP-PASS", |
| 39 | ("tcp", Some("so")) => "TCP-SO", |
| 40 | _ => return None, |
| 41 | }; |
| 42 | let mut text = |
| 43 | format!("{foundation} {component} {transport} {priority} {address} {port} typ {kind}"); |
| 44 | for part in related { |
| 45 | text.push(' '); |
| 46 | text.push_str(&part); |
| 47 | } |
| 48 | Some(text) |
| 49 | } |
| 50 | |
| 51 | fn candidate_ufrag(browser: &str) -> Option<&str> { |
| 52 | let tokens: Vec<&str> = browser.split_whitespace().collect(); |
| 53 | tokens |
| 54 | .windows(2) |
| 55 | .find(|pair| pair[0] == "ufrag") |
| 56 | .map(|pair| pair[1]) |
| 57 | } |
| 58 | |
| 59 | pub fn is_udp_relay(browser: &str) -> bool { |
| 60 | let lower = browser.to_ascii_lowercase(); |
| 61 | lower.contains(" udp ") && lower.contains(" typ relay") |
| 62 | } |
| 63 | |
| 64 | struct DefaultAddress { |
| 65 | address: String, |
| 66 | port: String, |
| 67 | } |
| 68 | |
| 69 | fn default_address(candidates: &[String]) -> Option<DefaultAddress> { |
| 70 | let rank = |kind: &str| match kind { |
| 71 | "relay" => 0, |
| 72 | "srflx" => 1, |
| 73 | _ => 2, |
| 74 | }; |
| 75 | candidates |
| 76 | .iter() |
| 77 | .filter_map(|candidate| { |
| 78 | let tokens: Vec<&str> = candidate.split_whitespace().collect(); |
| 79 | (tokens.get(1) == Some(&"1")).then_some(())?; |
| 80 | let transport_rank = if tokens.get(2) == Some(&"UDP") { 0 } else { 1 }; |
| 81 | Some(( |
| 82 | (transport_rank, rank(tokens.get(7)?)), |
| 83 | tokens.get(4)?.to_string(), |
| 84 | tokens.get(5)?.to_string(), |
| 85 | )) |
| 86 | }) |
| 87 | .min_by_key(|(rank, _, _)| *rank) |
| 88 | .map(|(_, address, port)| DefaultAddress { address, port }) |
| 89 | } |
| 90 | |
| 91 | fn connection_for(address: &str) -> String { |
| 92 | if address.contains(':') { |
| 93 | format!("IN IP6 {address}") |
| 94 | } else { |
| 95 | format!("IN IP4 {address}") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Replaces the placeholder transport of a signaled SDP with the gathered candidates, for a peer that is not the mixer. |
| 100 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 101 | pub enum SdpKind { |
| 102 | Offer, |
| 103 | Answer, |
| 104 | } |
| 105 | |
| 106 | fn settled_role(sdp: &str) -> Option<&str> { |
| 107 | sdp.lines() |
| 108 | .filter_map(|line| line.trim_end_matches('\r').strip_prefix("a=setup:")) |
| 109 | .find(|role| matches!(*role, "active" | "passive")) |
| 110 | } |
| 111 | |
| 112 | /// The DTLS role this side took in the last negotiation; Teams keeps it in re-offers instead of `actpass`. |
| 113 | pub fn negotiated_setup(local_sdp: Option<&str>, remote_sdp: Option<&str>) -> Option<String> { |
| 114 | if let Some(role) = local_sdp.and_then(settled_role) { |
| 115 | return Some(role.to_owned()); |
| 116 | } |
| 117 | match remote_sdp.and_then(settled_role)? { |
| 118 | "active" => Some("passive".to_owned()), |
| 119 | _ => Some("active".to_owned()), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pub fn with_candidates( |
| 124 | teams_sdp: &str, |
| 125 | browser_candidates: &[String], |
| 126 | kind: SdpKind, |
| 127 | ) -> crate::error::Result<String> { |
| 128 | let mut description = sdp::parse(teams_sdp)?; |
| 129 | let ufrag = description |
| 130 | .media |
| 131 | .first() |
| 132 | .and_then(|media| media.value("ice-ufrag")) |
| 133 | .map(str::to_owned); |
| 134 | let candidates: Vec<String> = browser_candidates |
| 135 | .iter() |
| 136 | .filter(|candidate| { |
| 137 | ufrag |
| 138 | .as_deref() |
| 139 | .is_none_or(|ufrag| candidate_ufrag(candidate).is_none_or(|own| own == ufrag)) |
| 140 | }) |
| 141 | .filter_map(|candidate| teams_candidate(candidate)) |
| 142 | .collect(); |
| 143 | let Some(default) = default_address(&candidates) else { |
| 144 | return Ok(teams_sdp.to_owned()); |
| 145 | }; |
| 146 | let connection = connection_for(&default.address); |
| 147 | if description.session.connection.is_some() { |
| 148 | description.session.connection = Some(connection.clone()); |
| 149 | } |
| 150 | for media in &mut description.media { |
| 151 | if media.port == "0" { |
| 152 | continue; |
| 153 | } |
| 154 | media.port = default.port.clone(); |
| 155 | media.connection = Some(connection.clone()); |
| 156 | let carried_candidates = media |
| 157 | .attributes |
| 158 | .iter() |
| 159 | .any(|attribute| attribute.name == "candidate"); |
| 160 | let mut attributes = Vec::with_capacity(media.attributes.len() + candidates.len()); |
| 161 | for attribute in media.attributes.drain(..) { |
| 162 | match attribute.name.as_str() { |
| 163 | "candidate" => {} |
| 164 | "rtcp" if kind == SdpKind::Offer => { |
| 165 | attributes.push(Attribute::with_value("rtcp", &default.port)) |
| 166 | } |
| 167 | "rtcp" => {} |
| 168 | _ => attributes.push(attribute), |
| 169 | } |
| 170 | } |
| 171 | if carried_candidates { |
| 172 | let position = attributes |
| 173 | .iter() |
| 174 | .position(|attribute| attribute.name == "ssrc" || attribute.name == "label") |
| 175 | .unwrap_or(attributes.len()); |
| 176 | for (offset, candidate) in candidates.iter().enumerate() { |
| 177 | attributes.insert( |
| 178 | position + offset, |
| 179 | Attribute::with_value("candidate", candidate), |
| 180 | ); |
| 181 | } |
| 182 | } |
| 183 | media.attributes = attributes; |
| 184 | } |
| 185 | Ok(sdp::write(&description)) |
| 186 | } |
| 187 | |
| 188 | #[cfg(test)] |
| 189 | mod tests { |
| 190 | use super::*; |
| 191 | |
| 192 | #[test] |
| 193 | fn browser_candidates_take_the_teams_form() { |
| 194 | assert_eq!( |
| 195 | teams_candidate("candidate:842163049 1 udp 1677729535 203.0.113.7 61001 typ srflx raddr 10.0.0.2 rport 61001 generation 0 ufrag abc network-id 1 network-cost 10").as_deref(), |
| 196 | Some("842163049 1 UDP 1677729535 203.0.113.7 61001 typ srflx raddr 10.0.0.2 rport 61001") |
| 197 | ); |
| 198 | assert_eq!( |
| 199 | teams_candidate( |
| 200 | "a=candidate:1 1 tcp 1518280447 10.0.0.2 9 typ host tcptype active generation 0" |
| 201 | ) |
| 202 | .as_deref(), |
| 203 | Some("1 1 TCP-ACT 1518280447 10.0.0.2 9 typ host") |
| 204 | ); |
| 205 | assert_eq!( |
| 206 | teams_candidate( |
| 207 | "candidate:2 1 udp 2122260223 abcd-1234.local 50000 typ host generation 0" |
| 208 | ), |
| 209 | None |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn the_relay_candidate_becomes_the_default_transport() { |
| 215 | let teams = "v=0\r\no=- 1 2 IN IP4 127.0.0.1\r\ns=-\r\nc=IN IP4 10.10.10.10\r\nt=0 0\r\nm=audio 1234 RTP/SAVP 111\r\nc=IN IP4 10.10.10.10\r\na=rtcp:1234\r\na=candidate:1755259772 1 UDP 2122197247 10.10.10.10 1234 typ host\r\na=rtpmap:111 opus/48000/2\r\na=label:main-audio\r\na=mid:0\r\nm=video 1234 RTP/SAVP 102\r\nc=IN IP4 10.10.10.10\r\na=rtcp:1234\r\na=rtpmap:102 H264/90000\r\na=label:main-video\r\na=mid:1\r\nm=video 0 RTP/SAVP 102\r\na=mid:2\r\n"; |
| 216 | let gathered = vec![ |
| 217 | "candidate:1 1 udp 2122260223 10.0.0.2 50000 typ host generation 0".to_owned(), |
| 218 | "candidate:2 1 udp 1677729535 203.0.113.7 61001 typ srflx raddr 10.0.0.2 rport 50000" |
| 219 | .to_owned(), |
| 220 | "candidate:3 1 udp 41885439 52.114.0.9 3480 typ relay raddr 203.0.113.7 rport 61001" |
| 221 | .to_owned(), |
| 222 | ]; |
| 223 | let text = with_candidates(teams, &gathered, SdpKind::Offer).unwrap(); |
| 224 | assert!(text.contains("m=audio 3480 RTP/SAVP 111")); |
| 225 | assert!(text.contains("m=video 3480 RTP/SAVP 102")); |
| 226 | assert!(text.contains("m=video 0 RTP/SAVP 102")); |
| 227 | assert!(text.contains("c=IN IP4 52.114.0.9")); |
| 228 | assert!(text.contains("a=rtcp:3480")); |
| 229 | assert!(!text.contains("10.10.10.10")); |
| 230 | assert_eq!(text.matches("a=candidate:").count(), 3); |
| 231 | assert!(text.contains( |
| 232 | "a=candidate:3 1 UDP 41885439 52.114.0.9 3480 typ relay raddr 203.0.113.7 rport 61001" |
| 233 | )); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn old_ice_generations_and_tcp_only_gathering_are_handled() { |
| 238 | let teams = "v=0\r\no=- 1 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\nm=audio 1234 RTP/SAVP 111\r\nc=IN IP4 10.10.10.10\r\na=ice-ufrag:new\r\na=candidate:1755259772 1 UDP 2122197247 10.10.10.10 1234 typ host\r\na=mid:0\r\n"; |
| 239 | let gathered = vec![ |
| 240 | "candidate:1 1 udp 2122260223 10.0.0.2 50000 typ host generation 0 ufrag old".to_owned(), |
| 241 | "candidate:2 1 tcp 1518280447 10.0.0.2 9 typ host tcptype active generation 0 ufrag new".to_owned(), |
| 242 | ]; |
| 243 | let text = with_candidates(teams, &gathered, SdpKind::Offer).unwrap(); |
| 244 | assert!(!text.contains("50000")); |
| 245 | assert!(text.contains("m=audio 9 RTP/SAVP 111")); |
| 246 | assert!(text.contains("a=candidate:2 1 TCP-ACT 1518280447 10.0.0.2 9 typ host")); |
| 247 | assert!(is_udp_relay( |
| 248 | "candidate:3 1 udp 41885439 52.114.0.9 3480 typ relay raddr 1.2.3.4 rport 5" |
| 249 | )); |
| 250 | assert!(!is_udp_relay( |
| 251 | "candidate:3 1 tcp 41885439 52.114.0.9 443 typ relay tcptype passive" |
| 252 | )); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn without_candidates_the_sdp_stays_as_it_is() { |
| 257 | assert_eq!( |
| 258 | with_candidates("v=0\r\n", &[], SdpKind::Offer).unwrap(), |
| 259 | "v=0\r\n" |
| 260 | ); |
| 261 | assert_eq!( |
| 262 | negotiated_setup(Some("v=0\r\na=setup:actpass\r\na=setup:active\r\n"), None).as_deref(), |
| 263 | Some("active") |
| 264 | ); |
| 265 | assert_eq!( |
| 266 | negotiated_setup(Some("a=setup:actpass\r\n"), Some("a=setup:active\r\n")).as_deref(), |
| 267 | Some("passive") |
| 268 | ); |
| 269 | assert_eq!(negotiated_setup(Some("a=setup:actpass\r\n"), None), None); |
| 270 | } |
| 271 | } |