BlueGit

ict rusty-teams

Files

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

remote_command.rs

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) => self.send_call_command(CallCommand::Mute(MuteCommand::Unmute)),
105 Some(RemoteCommand::Mute) => self.send_call_command(CallCommand::Mute(MuteCommand::Mute)),
106 Some(RemoteCommand::Camera(on)) => self.send_call_command(CallCommand::SetCamera(on)),
107 Some(RemoteCommand::RefreshShareSources) => self.send_call_command(CallCommand::RefreshShareSources),
108 Some(RemoteCommand::ShareScreen) => self.share_first_screen(),
109 Some(RemoteCommand::StopShare) => self.send_call_command(CallCommand::StopShare),
110 Some(RemoteCommand::Input(name)) => self.switch_input_for_test(&name),
111 Some(RemoteCommand::EchoCall) => self.start_test_call(cx),
112 Some(RemoteCommand::Hold(hold)) => self.send_call_command(CallCommand::Hold(hold)),
113 Some(RemoteCommand::Stats(path)) => {
114 let _ = std::fs::write(path, self.call_stats().to_string());
115 }
116 None => {}
117 }
118 }
119
120 fn share_first_screen(&self) {
121 let screen = self.call.as_ref().and_then(|call| {
122 call.model
123 .share_sources
124 .iter()
125 .find(|source| source.kind == ShareKind::Screen)
126 .cloned()
127 });
128 if let Some(screen) = screen {
129 self.send_call_command(CallCommand::StartShare(screen));
130 }
131 }
132
133 /// Switches the call's microphone without saving it as the user's choice.
134 fn switch_input_for_test(&self, name: &str) {
135 if name == DEFAULT_DEVICE {
136 self.send_call_command(CallCommand::SelectInput(DeviceChoice::SystemDefault));
137 return;
138 }
139 let wanted = name.to_lowercase();
140 let device = self.call.as_ref().and_then(|call| {
141 call.model
142 .devices
143 .inputs
144 .iter()
145 .find(|device| device.name.to_lowercase().contains(&wanted))
146 .map(|device| device.key())
147 });
148 if let Some(key) = device {
149 self.send_call_command(CallCommand::SelectInput(DeviceChoice::Device(key)));
150 }
151 }
152
153 fn call_stats(&self) -> serde_json::Value {
154 let Some(call) = self.call.as_ref() else {
155 return json!({ "inCall": false });
156 };
157 let model = &call.model;
158 let sample = model.last_sample.clone().unwrap_or_default();
159 json!({
160 "inCall": true,
161 "state": format!("{:?}", model.state),
162 "muted": model.muted,
163 "input": format!("{:?}", model.input),
164 "hold": format!("{:?}", model.hold),
165 "cameraOn": model.camera_on,
166 "localShare": model.local_share,
167 "screenSharer": model.screen_sharer,
168 "shareSources": model.share_sources.len(),
169 "tiles": model.tiles.len(),
170 "audioInPackets": sample.audio_in_packets,
171 "audioOutPackets": sample.audio_out_packets,
172 "audioInLevel": sample.audio_in_level,
173 "audioOutLevel": sample.audio_out_level,
174 "videoInPackets": sample.video.inbound_packets,
175 "videoInFramesDecoded": sample.video.inbound_frames_decoded,
176 "videoInSize": [sample.video.inbound_width, sample.video.inbound_height],
177 "videoFramesReceived": sample.video.frames_received,
178 "videoFramesPublished": sample.video.frames_published,
179 "videoOutPackets": sample.video.outbound_packets,
180 "videoOutFramesEncoded": sample.video.outbound_frames_encoded,
181 "videoOutSize": [sample.video.outbound_width, sample.video.outbound_height],
182 "shareInPackets": sample.share_in_packets,
183 "shareOutPackets": sample.share_out_packets,
184 })
185 }
186}
187
188#[cfg(test)]
189mod tests {
190 use super::RemoteCommand;
191
192 #[test]
193 fn commands_survive_the_trip_through_the_pipe() {
194 for command in [
195 RemoteCommand::Join("https://teams.microsoft.com/l/meetup-join/x".to_owned()),
196 RemoteCommand::HangUp,
197 RemoteCommand::AdmitAll,
198 RemoteCommand::Unmute,
199 RemoteCommand::Mute,
200 RemoteCommand::Camera(true),
201 RemoteCommand::Camera(false),
202 RemoteCommand::RefreshShareSources,
203 RemoteCommand::ShareScreen,
204 RemoteCommand::StopShare,
205 RemoteCommand::Stats("C:\\temp\\stats.json".to_owned()),
206 RemoteCommand::Input("cable output".to_owned()),
207 RemoteCommand::EchoCall,
208 RemoteCommand::Hold(true),
209 RemoteCommand::Hold(false),
210 ] {
211 assert_eq!(RemoteCommand::decode(&command.encode()), Some(command));
212 }
213 assert_eq!(RemoteCommand::decode("join "), None);
214 assert_eq!(RemoteCommand::decode("reboot"), None);
215 }
216}