BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / remote_command.rs

remote_command.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
remote_command.rs 9.1 KiB Download Raw
1use calling::{CallCommand, DeviceChoice, MuteCommand, ShareKind};
2use gpui_kit::*;
3use serde_json::json;
4
5use super::join::JoinMode;
6use crate::app_state::AppState;
7
8pub const JOIN_FLAG: &str = "--join";
9pub const HANG_UP_FLAG: &str = "--hang-up";
10pub const ADMIT_ALL_FLAG: &str = "--admit-all";
11pub const UNMUTE_FLAG: &str = "--unmute";
12pub const CALL_FLAG: &str = "--call";
13const JOIN_COMMAND: &str = "join ";
14const HANG_UP_COMMAND: &str = "hang-up";
15const ADMIT_ALL_COMMAND: &str = "admit-all";
16const UNMUTE_COMMAND: &str = "unmute";
17const MUTE_COMMAND: &str = "mute";
18const CAMERA_ON_COMMAND: &str = "camera on";
19const CAMERA_OFF_COMMAND: &str = "camera off";
20const SHARE_SOURCES_COMMAND: &str = "share-sources";
21const SHARE_COMMAND: &str = "share";
22const STOP_SHARE_COMMAND: &str = "stop-share";
23const STATS_COMMAND: &str = "stats ";
24const INPUT_COMMAND: &str = "input ";
25const ECHO_COMMAND: &str = "echo";
26const HOLD_COMMAND: &str = "hold";
27const RESUME_COMMAND: &str = "resume";
28const DEFAULT_DEVICE: &str = "default";
29
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub enum RemoteCommand {
32 Join(String),
33 HangUp,
34 AdmitAll,
35 Unmute,
36 Mute,
37 Camera(bool),
38 RefreshShareSources,
39 ShareScreen,
40 StopShare,
41 Stats(String),
42 Input(String),
43 EchoCall,
44 Hold(bool),
45}
46
47impl RemoteCommand {
48 pub fn encode(&self) -> String {
49 match self {
50 RemoteCommand::Join(link) => format!("{JOIN_COMMAND}{link}"),
51 RemoteCommand::HangUp => HANG_UP_COMMAND.to_owned(),
52 RemoteCommand::AdmitAll => ADMIT_ALL_COMMAND.to_owned(),
53 RemoteCommand::Unmute => UNMUTE_COMMAND.to_owned(),
54 RemoteCommand::Mute => MUTE_COMMAND.to_owned(),
55 RemoteCommand::Camera(true) => CAMERA_ON_COMMAND.to_owned(),
56 RemoteCommand::Camera(false) => CAMERA_OFF_COMMAND.to_owned(),
57 RemoteCommand::RefreshShareSources => SHARE_SOURCES_COMMAND.to_owned(),
58 RemoteCommand::ShareScreen => SHARE_COMMAND.to_owned(),
59 RemoteCommand::StopShare => STOP_SHARE_COMMAND.to_owned(),
60 RemoteCommand::Stats(path) => format!("{STATS_COMMAND}{path}"),
61 RemoteCommand::Input(name) => format!("{INPUT_COMMAND}{name}"),
62 RemoteCommand::EchoCall => ECHO_COMMAND.to_owned(),
63 RemoteCommand::Hold(true) => HOLD_COMMAND.to_owned(),
64 RemoteCommand::Hold(false) => RESUME_COMMAND.to_owned(),
65 }
66 }
67
68 pub fn decode(text: &str) -> Option<RemoteCommand> {
69 match text {
70 HANG_UP_COMMAND => return Some(RemoteCommand::HangUp),
71 ADMIT_ALL_COMMAND => return Some(RemoteCommand::AdmitAll),
72 UNMUTE_COMMAND => return Some(RemoteCommand::Unmute),
73 MUTE_COMMAND => return Some(RemoteCommand::Mute),
74 CAMERA_ON_COMMAND => return Some(RemoteCommand::Camera(true)),
75 CAMERA_OFF_COMMAND => return Some(RemoteCommand::Camera(false)),
76 SHARE_SOURCES_COMMAND => return Some(RemoteCommand::RefreshShareSources),
77 SHARE_COMMAND => return Some(RemoteCommand::ShareScreen),
78 STOP_SHARE_COMMAND => return Some(RemoteCommand::StopShare),
79 ECHO_COMMAND => return Some(RemoteCommand::EchoCall),
80 HOLD_COMMAND => return Some(RemoteCommand::Hold(true)),
81 RESUME_COMMAND => return Some(RemoteCommand::Hold(false)),
82 _ => {}
83 }
84 if let Some(name) = text.strip_prefix(INPUT_COMMAND).map(str::trim) {
85 return (!name.is_empty()).then(|| RemoteCommand::Input(name.to_owned()));
86 }
87 if let Some(path) = text.strip_prefix(STATS_COMMAND).map(str::trim) {
88 return (!path.is_empty()).then(|| RemoteCommand::Stats(path.to_owned()));
89 }
90 let link = text.strip_prefix(JOIN_COMMAND)?.trim();
91 (!link.is_empty()).then(|| RemoteCommand::Join(link.to_owned()))
92 }
93}
94
95impl AppState {
96 /// Commands a second `teams` process forwards, used by the call end-to-end test.
97 pub fn run_remote_command(&mut self, text: &str, cx: &mut Context<Self>) {
98 match RemoteCommand::decode(text) {
99 Some(RemoteCommand::Join(link)) => {
100 self.join_link(&link, JoinMode::Direct, cx);
101 }
102 Some(RemoteCommand::HangUp) => self.leave_call(cx),
103 Some(RemoteCommand::AdmitAll) => self.admit_all_call_guests(cx),
104 Some(RemoteCommand::Unmute) => {
105 self.send_call_command(CallCommand::Mute(MuteCommand::Unmute))
106 }
107 Some(RemoteCommand::Mute) => {
108 self.send_call_command(CallCommand::Mute(MuteCommand::Mute))
109 }
110 Some(RemoteCommand::Camera(on)) => self.send_call_command(CallCommand::SetCamera(on)),
111 Some(RemoteCommand::RefreshShareSources) => {
112 self.send_call_command(CallCommand::RefreshShareSources)
113 }
114 Some(RemoteCommand::ShareScreen) => self.share_first_screen(),
115 Some(RemoteCommand::StopShare) => self.send_call_command(CallCommand::StopShare),
116 Some(RemoteCommand::Input(name)) => self.switch_input_for_test(&name),
117 Some(RemoteCommand::EchoCall) => self.start_test_call(cx),
118 Some(RemoteCommand::Hold(hold)) => self.send_call_command(CallCommand::Hold(hold)),
119 Some(RemoteCommand::Stats(path)) => {
120 let _ = std::fs::write(path, self.call_stats().to_string());
121 }
122 None => {}
123 }
124 }
125
126 fn share_first_screen(&self) {
127 let screen = self.call.as_ref().and_then(|call| {
128 call.model
129 .share_sources
130 .iter()
131 .find(|source| source.kind == ShareKind::Screen)
132 .cloned()
133 });
134 if let Some(screen) = screen {
135 self.send_call_command(CallCommand::StartShare(screen));
136 }
137 }
138
139 /// Switches the call's microphone without saving it as the user's choice.
140 fn switch_input_for_test(&self, name: &str) {
141 if name == DEFAULT_DEVICE {
142 self.send_call_command(CallCommand::SelectInput(DeviceChoice::SystemDefault));
143 return;
144 }
145 let wanted = name.to_lowercase();
146 let device = self.call.as_ref().and_then(|call| {
147 call.model
148 .devices
149 .inputs
150 .iter()
151 .find(|device| device.name.to_lowercase().contains(&wanted))
152 .map(|device| device.key())
153 });
154 if let Some(key) = device {
155 self.send_call_command(CallCommand::SelectInput(DeviceChoice::Device(key)));
156 }
157 }
158
159 fn call_stats(&self) -> serde_json::Value {
160 let Some(call) = self.call.as_ref() else {
161 return json!({ "inCall": false });
162 };
163 let model = &call.model;
164 let sample = model.last_sample.clone().unwrap_or_default();
165 json!({
166 "inCall": true,
167 "state": format!("{:?}", model.state),
168 "muted": model.muted,
169 "input": format!("{:?}", model.input),
170 "hold": format!("{:?}", model.hold),
171 "cameraOn": model.camera_on,
172 "localShare": model.local_share,
173 "screenSharer": model.screen_sharer,
174 "shareSources": model.share_sources.len(),
175 "tiles": model.tiles.len(),
176 "audioInPackets": sample.audio_in_packets,
177 "audioOutPackets": sample.audio_out_packets,
178 "audioInLevel": sample.audio_in_level,
179 "audioOutLevel": sample.audio_out_level,
180 "videoInPackets": sample.video.inbound_packets,
181 "videoInFramesDecoded": sample.video.inbound_frames_decoded,
182 "videoInSize": [sample.video.inbound_width, sample.video.inbound_height],
183 "videoFramesReceived": sample.video.frames_received,
184 "videoFramesPublished": sample.video.frames_published,
185 "videoOutPackets": sample.video.outbound_packets,
186 "videoOutFramesEncoded": sample.video.outbound_frames_encoded,
187 "videoOutSize": [sample.video.outbound_width, sample.video.outbound_height],
188 "shareInPackets": sample.share_in_packets,
189 "shareOutPackets": sample.share_out_packets,
190 })
191 }
192}
193
194#[cfg(test)]
195mod tests {
196 use super::RemoteCommand;
197
198 #[test]
199 fn commands_survive_the_trip_through_the_pipe() {
200 for command in [
201 RemoteCommand::Join("https://teams.microsoft.com/l/meetup-join/x".to_owned()),
202 RemoteCommand::HangUp,
203 RemoteCommand::AdmitAll,
204 RemoteCommand::Unmute,
205 RemoteCommand::Mute,
206 RemoteCommand::Camera(true),
207 RemoteCommand::Camera(false),
208 RemoteCommand::RefreshShareSources,
209 RemoteCommand::ShareScreen,
210 RemoteCommand::StopShare,
211 RemoteCommand::Stats("C:\\temp\\stats.json".to_owned()),
212 RemoteCommand::Input("cable output".to_owned()),
213 RemoteCommand::EchoCall,
214 RemoteCommand::Hold(true),
215 RemoteCommand::Hold(false),
216 ] {
217 assert_eq!(RemoteCommand::decode(&command.encode()), Some(command));
218 }
219 assert_eq!(RemoteCommand::decode("join "), None);
220 assert_eq!(RemoteCommand::decode("reboot"), None);
221 }
222}