BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / hold.rs

hold.rs

hold.rs 7.0 KiB Download Raw
1use crate::error::Result;
2use crate::sdp::parse;
3
4const DIRECTIONS: [&str; 4] = ["sendrecv", "sendonly", "recvonly", "inactive"];
5pub const INACTIVE: &str = "inactive";
6const REJECTED_PORT: &str = "0";
7const DATA_KINDS: [&str; 2] = ["x-data", "application"];
8const MAIN_VIDEO_LABEL: &str = "main-video";
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum HoldState {
12 #[default]
13 Active,
14 Local,
15 Remote,
16}
17
18impl HoldState {
19 pub fn from_flags(local: bool, remote: bool) -> HoldState {
20 if local {
21 HoldState::Local
22 } else if remote {
23 HoldState::Remote
24 } else {
25 HoldState::Active
26 }
27 }
28}
29
30#[derive(Debug, Clone, Copy)]
31pub struct MediaLine<'a> {
32 pub kind: &'a str,
33 pub mid: &'a str,
34}
35
36struct Section {
37 kind: String,
38 mid: String,
39 direction_line: Option<usize>,
40}
41
42fn sections(lines: &[String]) -> Vec<Section> {
43 let mut found: Vec<Section> = Vec::new();
44 for (index, line) in lines.iter().enumerate() {
45 if let Some(rest) = line.strip_prefix("m=") {
46 let kind = rest.split(' ').next().unwrap_or_default().to_owned();
47 found.push(Section { kind, mid: String::new(), direction_line: None });
48 } else if let Some(section) = found.last_mut() {
49 if let Some(mid) = line.strip_prefix("a=mid:") {
50 section.mid = mid.to_owned();
51 } else if line.strip_prefix("a=").is_some_and(|name| DIRECTIONS.contains(&name)) {
52 section.direction_line = Some(index);
53 }
54 }
55 }
56 found
57}
58
59pub fn rewrite_directions(sdp: &str, pick: impl Fn(MediaLine<'_>) -> Option<&'static str>) -> String {
60 let newline = if sdp.contains("\r\n") { "\r\n" } else { "\n" };
61 let mut lines: Vec<String> = sdp.split('\n').map(|line| line.trim_end_matches('\r').to_owned()).collect();
62 for section in sections(&lines) {
63 let Some(direction_line) = section.direction_line else { continue };
64 if let Some(direction) = pick(MediaLine { kind: &section.kind, mid: &section.mid }) {
65 lines[direction_line] = format!("a={direction}");
66 }
67 }
68 lines.join(newline)
69}
70
71pub fn all_inactive(sdp: &str) -> String {
72 rewrite_directions(sdp, |_| Some(INACTIVE))
73}
74
75pub fn is_hold(teams_sdp: &str) -> Result<bool> {
76 let description = parse(teams_sdp)?;
77 let mut media = description
78 .media
79 .iter()
80 .filter(|line| line.port != REJECTED_PORT && !DATA_KINDS.contains(&line.kind.as_str()))
81 .peekable();
82 if media.peek().is_none() {
83 return Ok(false);
84 }
85 Ok(media.all(|line| line.direction() == Some(INACTIVE)))
86}
87
88pub fn offer_sends_video(teams_sdp: &str) -> bool {
89 parse(teams_sdp).is_ok_and(|description| {
90 description.media.iter().any(|line| {
91 line.kind == "video"
92 && line.port != REJECTED_PORT
93 && line.value("label").is_none_or(|label| label == MAIN_VIDEO_LABEL)
94 && matches!(line.direction(), None | Some("sendrecv" | "sendonly"))
95 })
96 })
97}
98
99pub fn accepted_modalities(video: bool) -> Vec<String> {
100 let mut modalities = vec!["Audio".to_owned()];
101 if video {
102 modalities.push("Video".to_owned());
103 }
104 modalities
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 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";
112
113 fn directions(sdp: &str) -> Vec<String> {
114 parse(sdp).unwrap().media.iter().map(|line| line.direction().unwrap_or("none").to_owned()).collect()
115 }
116
117 #[test]
118 fn holding_puts_every_line_on_inactive_and_nothing_else_changes() {
119 let held = all_inactive(OFFER);
120 assert_eq!(directions(&held), vec!["inactive", "inactive", "inactive"]);
121 assert_eq!(held.lines().count(), OFFER.lines().count());
122 assert!(held.contains("a=mid:2\r\na=inactive\r\n"));
123 assert!(held.ends_with("\r\n"));
124 }
125
126 #[test]
127 fn resuming_keeps_the_offer_as_libwebrtc_wrote_it() {
128 let resumed = rewrite_directions(OFFER, |_| None);
129 assert_eq!(resumed, OFFER);
130 assert_eq!(directions(&resumed), vec!["sendrecv", "sendrecv", "recvonly"]);
131 }
132
133 #[test]
134 fn single_lines_can_be_picked_by_kind_and_mid() {
135 let picked = rewrite_directions(OFFER, |line| match (line.kind, line.mid) {
136 ("video", "1") => Some("inactive"),
137 ("video", "2") => Some("sendonly"),
138 _ => None,
139 });
140 assert_eq!(directions(&picked), vec!["sendrecv", "inactive", "sendonly"]);
141 }
142
143 #[test]
144 fn plain_line_feeds_survive() {
145 let unix = OFFER.replace("\r\n", "\n");
146 let held = all_inactive(&unix);
147 assert!(!held.contains('\r'));
148 assert_eq!(directions(&held), vec!["inactive", "inactive", "inactive"]);
149 }
150
151 #[test]
152 fn a_hold_is_every_audio_and_video_line_inactive() {
153 assert!(is_hold(&all_inactive(OFFER)).unwrap());
154 assert!(!is_hold(OFFER).unwrap());
155 let sendonly_audio = rewrite_directions(&all_inactive(OFFER), |line| (line.kind == "audio").then_some("sendonly"));
156 assert!(!is_hold(&sendonly_audio).unwrap());
157 }
158
159 #[test]
160 fn rejected_and_data_lines_do_not_stop_a_hold() {
161 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";
162 assert!(is_hold(teams).unwrap());
163 assert!(!is_hold("v=0\r\ns=-\r\nt=0 0\r\n").unwrap());
164 }
165
166 #[test]
167 fn an_offer_with_a_sending_main_video_line_is_a_video_call() {
168 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");
169 assert!(offer_sends_video(&teams("sendrecv", "main-video")));
170 assert!(offer_sends_video(&teams("sendonly", "main-video")));
171 assert!(!offer_sends_video(&teams("recvonly", "main-video")));
172 assert!(!offer_sends_video(&teams("inactive", "main-video")));
173 assert!(!offer_sends_video(&teams("sendrecv", "applicationsharing-video")));
174 }
175
176 #[test]
177 fn the_hold_state_prefers_your_own_hold() {
178 assert_eq!(HoldState::from_flags(false, false), HoldState::Active);
179 assert_eq!(HoldState::from_flags(true, true), HoldState::Local);
180 assert_eq!(HoldState::from_flags(false, true), HoldState::Remote);
181 }
182
183 #[test]
184 fn accepting_with_video_adds_the_modality() {
185 assert_eq!(accepted_modalities(false), vec!["Audio"]);
186 assert_eq!(accepted_modalities(true), vec!["Audio", "Video"]);
187 }
188}