BlueGit

ict rusty-teams

Files

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

remote_command.rs

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