| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use crate::error::Result; |
| 4 | use crate::sdp::parse; |
| 5 | |
| 6 | const DIRECTIONS: [&str; 4] = ["sendrecv", "sendonly", "recvonly", "inactive"]; |
| 7 | pub const INACTIVE: &str = "inactive"; |
| 8 | const REJECTED_PORT: &str = "0"; |
| 9 | const DATA_KINDS: [&str; 2] = ["x-data", "application"]; |
| 10 | const MAIN_VIDEO_LABEL: &str = "main-video"; |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 13 | pub enum HoldState { |
| 14 | #[default] |
| 15 | Active, |
| 16 | Local, |
| 17 | Remote, |
| 18 | } |
| 19 | |
| 20 | impl HoldState { |
| 21 | pub fn from_flags(local: bool, remote: bool) -> HoldState { |
| 22 | if local { |
| 23 | HoldState::Local |
| 24 | } else if remote { |
| 25 | HoldState::Remote |
| 26 | } else { |
| 27 | HoldState::Active |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug, Clone, Copy)] |
| 33 | pub struct MediaLine<'a> { |
| 34 | pub kind: &'a str, |
| 35 | pub mid: &'a str, |
| 36 | } |
| 37 | |
| 38 | struct Section { |
| 39 | kind: String, |
| 40 | mid: String, |
| 41 | direction_line: Option<usize>, |
| 42 | } |
| 43 | |
| 44 | fn sections(lines: &[String]) -> Vec<Section> { |
| 45 | let mut found: Vec<Section> = Vec::new(); |
| 46 | for (index, line) in lines.iter().enumerate() { |
| 47 | if let Some(rest) = line.strip_prefix("m=") { |
| 48 | let kind = rest.split(' ').next().unwrap_or_default().to_owned(); |
| 49 | found.push(Section { kind, mid: String::new(), direction_line: None }); |
| 50 | } else if let Some(section) = found.last_mut() { |
| 51 | if let Some(mid) = line.strip_prefix("a=mid:") { |
| 52 | section.mid = mid.to_owned(); |
| 53 | } else if line.strip_prefix("a=").is_some_and(|name| DIRECTIONS.contains(&name)) { |
| 54 | section.direction_line = Some(index); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | found |
| 59 | } |
| 60 | |
| 61 | pub fn rewrite_directions(sdp: &str, pick: impl Fn(MediaLine<'_>) -> Option<&'static str>) -> String { |
| 62 | let newline = if sdp.contains("\r\n") { "\r\n" } else { "\n" }; |
| 63 | let mut lines: Vec<String> = sdp.split('\n').map(|line| line.trim_end_matches('\r').to_owned()).collect(); |
| 64 | for section in sections(&lines) { |
| 65 | let Some(direction_line) = section.direction_line else { continue }; |
| 66 | if let Some(direction) = pick(MediaLine { kind: §ion.kind, mid: §ion.mid }) { |
| 67 | lines[direction_line] = format!("a={direction}"); |
| 68 | } |
| 69 | } |
| 70 | lines.join(newline) |
| 71 | } |
| 72 | |
| 73 | pub fn direction_of(sdp: &str, mid: &str) -> Option<String> { |
| 74 | let lines: Vec<String> = sdp.split('\n').map(|line| line.trim_end_matches('\r').to_owned()).collect(); |
| 75 | sections(&lines) |
| 76 | .into_iter() |
| 77 | .find(|section| section.mid == mid) |
| 78 | .and_then(|section| section.direction_line) |
| 79 | .and_then(|line| lines[line].strip_prefix("a=").map(str::to_owned)) |
| 80 | } |
| 81 | |
| 82 | pub fn stop_sending_on(sdp: &str, mid: &str) -> String { |
| 83 | let newline = if sdp.contains("\r\n") { "\r\n" } else { "\n" }; |
| 84 | let mut lines: Vec<String> = sdp.split('\n').map(|line| line.trim_end_matches('\r').to_owned()).collect(); |
| 85 | for section in sections(&lines) { |
| 86 | let Some(direction_line) = section.direction_line else { continue }; |
| 87 | if section.mid != mid { |
| 88 | continue; |
| 89 | } |
| 90 | let receive_only = match lines[direction_line].as_str() { |
| 91 | "a=sendrecv" => "recvonly", |
| 92 | "a=sendonly" => INACTIVE, |
| 93 | _ => continue, |
| 94 | }; |
| 95 | lines[direction_line] = format!("a={receive_only}"); |
| 96 | } |
| 97 | lines.join(newline) |
| 98 | } |
| 99 | |
| 100 | fn mirrored(direction: &str) -> &'static str { |
| 101 | match direction { |
| 102 | "sendonly" => "recvonly", |
| 103 | "recvonly" => "sendonly", |
| 104 | "inactive" => INACTIVE, |
| 105 | _ => "sendrecv", |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | pub fn keep_media_running(browser_answer: &str, browser_offer: &str) -> String { |
| 110 | let offer_lines: Vec<String> = browser_offer.split('\n').map(|line| line.trim_end_matches('\r').to_owned()).collect(); |
| 111 | let offered: HashMap<String, &'static str> = sections(&offer_lines) |
| 112 | .into_iter() |
| 113 | .filter_map(|section| { |
| 114 | let direction = offer_lines[section.direction_line?].strip_prefix("a=")?; |
| 115 | Some((section.mid, mirrored(direction))) |
| 116 | }) |
| 117 | .collect(); |
| 118 | rewrite_directions(browser_answer, |line| offered.get(line.mid).copied()) |
| 119 | } |
| 120 | |
| 121 | pub fn all_inactive(sdp: &str) -> String { |
| 122 | rewrite_directions(sdp, |_| Some(INACTIVE)) |
| 123 | } |
| 124 | |
| 125 | pub fn is_hold(teams_sdp: &str) -> Result<bool> { |
| 126 | let description = parse(teams_sdp)?; |
| 127 | let mut media = description |
| 128 | .media |
| 129 | .iter() |
| 130 | .filter(|line| line.port != REJECTED_PORT && !DATA_KINDS.contains(&line.kind.as_str())) |
| 131 | .peekable(); |
| 132 | if media.peek().is_none() { |
| 133 | return Ok(false); |
| 134 | } |
| 135 | Ok(media.all(|line| line.direction() == Some(INACTIVE))) |
| 136 | } |
| 137 | |
| 138 | pub fn offer_sends_video(teams_sdp: &str) -> bool { |
| 139 | parse(teams_sdp).is_ok_and(|description| { |
| 140 | description.media.iter().any(|line| { |
| 141 | line.kind == "video" |
| 142 | && line.port != REJECTED_PORT |
| 143 | && line.value("label").is_none_or(|label| label == MAIN_VIDEO_LABEL) |
| 144 | && matches!(line.direction(), None | Some("sendrecv" | "sendonly")) |
| 145 | }) |
| 146 | }) |
| 147 | } |
| 148 | |
| 149 | pub fn offer_modalities(video: bool, sharing: bool) -> Vec<String> { |
| 150 | let mut modalities = accepted_modalities(video); |
| 151 | if sharing { |
| 152 | modalities.push(SCREEN_SHARER.to_owned()); |
| 153 | } |
| 154 | modalities |
| 155 | } |
| 156 | |
| 157 | const SCREEN_SHARER: &str = "ScreenSharer"; |
| 158 | |
| 159 | pub fn accepted_modalities(video: bool) -> Vec<String> { |
| 160 | let mut modalities = vec!["Audio".to_owned()]; |
| 161 | if video { |
| 162 | modalities.push("Video".to_owned()); |
| 163 | } |
| 164 | modalities |
| 165 | } |
| 166 | |
| 167 | #[cfg(test)] |
| 168 | mod tests { |
| 169 | #[test] |
| 170 | fn the_direction_of_a_line_is_read_by_mid() { |
| 171 | let sdp = "v=0\r\nm=audio 9 RTP 0\r\na=mid:0\r\na=sendrecv\r\nm=video 9 RTP 96\r\na=mid:2\r\na=recvonly\r\n"; |
| 172 | assert_eq!(super::direction_of(sdp, "2").as_deref(), Some("recvonly")); |
| 173 | assert_eq!(super::direction_of(sdp, "7"), None); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn a_line_that_stops_sending_keeps_receiving() { |
| 178 | let sdp = "v=0\r\nm=audio 9 RTP 0\r\na=mid:0\r\na=sendrecv\r\nm=video 9 RTP 96\r\na=mid:1\r\na=sendrecv\r\nm=video 9 RTP 96\r\na=mid:2\r\na=sendonly\r\n"; |
| 179 | let camera_off = super::stop_sending_on(sdp, "1"); |
| 180 | assert!(camera_off.contains("a=mid:1\r\na=recvonly")); |
| 181 | assert!(camera_off.contains("a=mid:0\r\na=sendrecv")); |
| 182 | assert!(super::stop_sending_on(sdp, "2").contains("a=mid:2\r\na=inactive")); |
| 183 | } |
| 184 | |
| 185 | use super::*; |
| 186 | |
| 187 | const OFFER: &str = "v=0\r\no=- 1 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1 2\r\nm=audio 9 UDP/TLS/RTP/SAVPF 111\r\nc=IN IP4 0.0.0.0\r\na=mid:0\r\na=sendrecv\r\na=rtpmap:111 opus/48000/2\r\nm=video 9 UDP/TLS/RTP/SAVPF 102\r\nc=IN IP4 0.0.0.0\r\na=mid:1\r\na=sendrecv\r\na=rtpmap:102 H264/90000\r\nm=video 9 UDP/TLS/RTP/SAVPF 102\r\nc=IN IP4 0.0.0.0\r\na=mid:2\r\na=recvonly\r\na=rtpmap:102 H264/90000\r\n"; |
| 188 | |
| 189 | fn directions(sdp: &str) -> Vec<String> { |
| 190 | parse(sdp).unwrap().media.iter().map(|line| line.direction().unwrap_or("none").to_owned()).collect() |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn holding_puts_every_line_on_inactive_and_nothing_else_changes() { |
| 195 | let held = all_inactive(OFFER); |
| 196 | assert_eq!(directions(&held), vec!["inactive", "inactive", "inactive"]); |
| 197 | assert_eq!(held.lines().count(), OFFER.lines().count()); |
| 198 | assert!(held.contains("a=mid:2\r\na=inactive\r\n")); |
| 199 | assert!(held.ends_with("\r\n")); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn resuming_keeps_the_offer_as_libwebrtc_wrote_it() { |
| 204 | let resumed = rewrite_directions(OFFER, |_| None); |
| 205 | assert_eq!(resumed, OFFER); |
| 206 | assert_eq!(directions(&resumed), vec!["sendrecv", "sendrecv", "recvonly"]); |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn single_lines_can_be_picked_by_kind_and_mid() { |
| 211 | let picked = rewrite_directions(OFFER, |line| match (line.kind, line.mid) { |
| 212 | ("video", "1") => Some("inactive"), |
| 213 | ("video", "2") => Some("sendonly"), |
| 214 | _ => None, |
| 215 | }); |
| 216 | assert_eq!(directions(&picked), vec!["sendrecv", "inactive", "sendonly"]); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn plain_line_feeds_survive() { |
| 221 | let unix = OFFER.replace("\r\n", "\n"); |
| 222 | let held = all_inactive(&unix); |
| 223 | assert!(!held.contains('\r')); |
| 224 | assert_eq!(directions(&held), vec!["inactive", "inactive", "inactive"]); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn a_hold_answer_mirrors_our_offer_so_nothing_stops_locally() { |
| 229 | let answer = all_inactive(&OFFER.replace("a=setup:actpass", "a=setup:passive")); |
| 230 | let kept = keep_media_running(&answer, OFFER); |
| 231 | assert_eq!(directions(&kept), vec!["sendrecv", "sendrecv", "sendonly"]); |
| 232 | assert_eq!(kept.lines().count(), answer.lines().count()); |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn a_hold_is_every_audio_and_video_line_inactive() { |
| 237 | assert!(is_hold(&all_inactive(OFFER)).unwrap()); |
| 238 | assert!(!is_hold(OFFER).unwrap()); |
| 239 | let sendonly_audio = rewrite_directions(&all_inactive(OFFER), |line| (line.kind == "audio").then_some("sendonly")); |
| 240 | assert!(!is_hold(&sendonly_audio).unwrap()); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn rejected_and_data_lines_do_not_stop_a_hold() { |
| 245 | 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\na=mid:0\r\na=inactive\r\nm=video 0 RTP/SAVP 102\r\na=mid:1\r\na=inactive\r\nm=x-data 1234 RTP/SAVP 127\r\na=mid:2\r\na=sendrecv\r\n"; |
| 246 | assert!(is_hold(teams).unwrap()); |
| 247 | assert!(!is_hold("v=0\r\ns=-\r\nt=0 0\r\n").unwrap()); |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn an_offer_with_a_sending_main_video_line_is_a_video_call() { |
| 252 | let teams = |direction: &str, label: &str| format!("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\na=mid:0\r\na=sendrecv\r\nm=video 1234 RTP/SAVP 102\r\na=mid:1\r\na={direction}\r\na=label:{label}\r\n"); |
| 253 | assert!(offer_sends_video(&teams("sendrecv", "main-video"))); |
| 254 | assert!(offer_sends_video(&teams("sendonly", "main-video"))); |
| 255 | assert!(!offer_sends_video(&teams("recvonly", "main-video"))); |
| 256 | assert!(!offer_sends_video(&teams("inactive", "main-video"))); |
| 257 | assert!(!offer_sends_video(&teams("sendrecv", "applicationsharing-video"))); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn the_hold_state_prefers_your_own_hold() { |
| 262 | assert_eq!(HoldState::from_flags(false, false), HoldState::Active); |
| 263 | assert_eq!(HoldState::from_flags(true, true), HoldState::Local); |
| 264 | assert_eq!(HoldState::from_flags(false, true), HoldState::Remote); |
| 265 | } |
| 266 | |
| 267 | #[test] |
| 268 | fn accepting_with_video_adds_the_modality() { |
| 269 | assert_eq!(accepted_modalities(false), vec!["Audio"]); |
| 270 | assert_eq!(accepted_modalities(true), vec!["Audio", "Video"]); |
| 271 | assert_eq!(super::offer_modalities(false, true), vec!["Audio", "ScreenSharer"]); |
| 272 | assert_eq!(super::offer_modalities(true, true), vec!["Audio", "Video", "ScreenSharer"]); |
| 273 | } |
| 274 | } |