BlueGit

ict rusty-teams

Files

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

remote_command.rs

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