BlueGit

ict rusty-teams

Files

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

remote_command.rs

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