BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / prejoin.rs

prejoin.rs

prejoin.rs 7.4 KiB Download Raw
1use calling::{CallCommand, CallSpec, DeviceChoice, MeetingTarget, MuteCommand};
2use gpui_kit::Context;
3
4use super::demo::{DemoCall, demo_guests};
5use super::model::CallModel;
6use crate::app_state::AppState;
7
8const OPTIONS_META_KEY: &str = "prejoin_options";
9const DEFAULT_DEVICE_META: &str = "default";
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
12pub enum AudioChoice {
13 #[default]
14 Computer,
15 NoAudio,
16}
17
18impl AudioChoice {
19 pub fn label(self) -> &'static str {
20 match self {
21 AudioChoice::Computer => "Computer audio",
22 AudioChoice::NoAudio => "Don't use audio",
23 }
24 }
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct JoinOptions {
29 pub camera_on: bool,
30 pub mic_on: bool,
31 pub audio: AudioChoice,
32 pub input: DeviceChoice,
33 pub output: DeviceChoice,
34}
35
36impl Default for JoinOptions {
37 fn default() -> Self {
38 JoinOptions {
39 camera_on: false,
40 mic_on: true,
41 audio: AudioChoice::Computer,
42 input: DeviceChoice::SystemDefault,
43 output: DeviceChoice::SystemDefault,
44 }
45 }
46}
47
48fn device_meta(choice: &DeviceChoice) -> &str {
49 match choice {
50 DeviceChoice::SystemDefault => DEFAULT_DEVICE_META,
51 DeviceChoice::Device(key) => key,
52 }
53}
54
55fn device_from_meta(value: &str) -> DeviceChoice {
56 match value {
57 "" | DEFAULT_DEVICE_META => DeviceChoice::SystemDefault,
58 key => DeviceChoice::Device(key.to_owned()),
59 }
60}
61
62impl JoinOptions {
63 pub fn to_meta(&self) -> String {
64 format!(
65 "camera={}\nmic={}\ninput={}\noutput={}",
66 u8::from(self.camera_on),
67 u8::from(self.mic_on),
68 device_meta(&self.input),
69 device_meta(&self.output)
70 )
71 }
72
73 pub fn from_meta(text: &str) -> Self {
74 let mut options = JoinOptions::default();
75 for (key, value) in text.lines().filter_map(|line| line.split_once('=')) {
76 match key {
77 "camera" => options.camera_on = value == "1",
78 "mic" => options.mic_on = value == "1",
79 "input" => options.input = device_from_meta(value),
80 "output" => options.output = device_from_meta(value),
81 _ => {}
82 }
83 }
84 options
85 }
86
87 pub fn commands_after_join(&self) -> Vec<CallCommand> {
88 if self.audio == AudioChoice::NoAudio {
89 return vec![CallCommand::DisableAudio];
90 }
91 let mut commands = Vec::new();
92 if self.mic_on {
93 commands.push(CallCommand::Mute(MuteCommand::Unmute));
94 }
95 if self.input != DeviceChoice::SystemDefault {
96 commands.push(CallCommand::SelectInput(self.input.clone()));
97 }
98 if self.output != DeviceChoice::SystemDefault {
99 commands.push(CallCommand::SelectOutput(self.output.clone()));
100 }
101 commands
102 }
103}
104
105pub fn load_join_options(store: &store::Store) -> JoinOptions {
106 store
107 .meta(OPTIONS_META_KEY)
108 .ok()
109 .flatten()
110 .map(|text| JoinOptions::from_meta(&text))
111 .unwrap_or_default()
112}
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub struct DemoMeeting {
116 pub guests: usize,
117 pub lobby: bool,
118 pub organizer: bool,
119}
120
121#[derive(Debug, Clone)]
122pub struct MeetingJoin {
123 pub target: MeetingTarget,
124 pub title: String,
125 pub organizer: bool,
126 pub conversation_id: Option<String>,
127 pub demo: DemoMeeting,
128}
129
130#[derive(Debug, Clone)]
131pub struct PendingJoin {
132 pub id: u64,
133 pub join: MeetingJoin,
134}
135
136impl AppState {
137 pub(super) fn stage_meeting_join(&mut self, join: MeetingJoin, cx: &mut Context<Self>) {
138 if self.call.is_some() {
139 self.show_call(true, cx);
140 return;
141 }
142 self.prejoin_count += 1;
143 self.prejoin = Some(PendingJoin {
144 id: self.prejoin_count,
145 join,
146 });
147 self.new_chat = false;
148 cx.notify();
149 }
150
151 pub fn confirm_prejoin(&mut self, options: JoinOptions, cx: &mut Context<Self>) {
152 let Some(pending) = self.prejoin.take() else {
153 return;
154 };
155 let _ = self.store.set_meta(OPTIONS_META_KEY, &options.to_meta());
156 self.join_options = options.clone();
157 self.start_meeting(pending.join, Some(&options), cx);
158 }
159
160 pub fn cancel_prejoin(&mut self, cx: &mut Context<Self>) {
161 if self.prejoin.take().is_some() {
162 cx.notify();
163 }
164 }
165
166 pub(super) fn start_meeting(
167 &mut self,
168 join: MeetingJoin,
169 options: Option<&JoinOptions>,
170 cx: &mut Context<Self>,
171 ) {
172 if self.call.is_some() {
173 self.show_call(true, cx);
174 return;
175 }
176 let demo = DemoCall::Meeting {
177 guests: demo_guests(join.demo.guests),
178 lobby: join.demo.lobby,
179 organizer: join.demo.organizer,
180 };
181 let with_video = options.is_some_and(|options| options.camera_on);
182 let Some(handle) =
183 self.place_call(CallSpec::Meeting(join.target.clone()), demo, with_video, cx)
184 else {
185 return;
186 };
187 let model = CallModel::meeting(&join.title, join.organizer);
188 self.begin_call(handle, model, join.conversation_id, cx);
189 if let Some(call) = self.call.as_mut() {
190 call.meeting_target = Some(join.target);
191 call.model.audio_off =
192 options.is_some_and(|options| options.audio == AudioChoice::NoAudio);
193 }
194 for command in options
195 .map(JoinOptions::commands_after_join)
196 .unwrap_or_default()
197 {
198 self.send_call_command(command);
199 }
200 }
201}
202
203#[cfg(test)]
204mod tests {
205 use super::*;
206
207 #[test]
208 fn options_survive_a_round_trip_through_the_store_text() {
209 let options = JoinOptions {
210 camera_on: true,
211 mic_on: false,
212 audio: AudioChoice::Computer,
213 input: DeviceChoice::Device("usb-mic".into()),
214 output: DeviceChoice::SystemDefault,
215 };
216 assert_eq!(JoinOptions::from_meta(&options.to_meta()), options);
217 }
218
219 #[test]
220 fn empty_or_garbage_text_gives_the_defaults() {
221 assert_eq!(JoinOptions::from_meta(""), JoinOptions::default());
222 assert_eq!(JoinOptions::from_meta("junk\nmic"), JoinOptions::default());
223 }
224
225 #[test]
226 fn computer_audio_unmutes_and_selects_chosen_devices_only() {
227 let default = JoinOptions::default();
228 assert_eq!(
229 default.commands_after_join(),
230 vec![CallCommand::Mute(MuteCommand::Unmute)]
231 );
232 let chosen = JoinOptions {
233 mic_on: false,
234 input: DeviceChoice::Device("mic".into()),
235 output: DeviceChoice::Device("speaker".into()),
236 ..default
237 };
238 assert_eq!(
239 chosen.commands_after_join(),
240 vec![
241 CallCommand::SelectInput(DeviceChoice::Device("mic".into())),
242 CallCommand::SelectOutput(DeviceChoice::Device("speaker".into())),
243 ]
244 );
245 }
246
247 #[test]
248 fn without_audio_only_the_audio_is_switched_off() {
249 let options = JoinOptions {
250 audio: AudioChoice::NoAudio,
251 input: DeviceChoice::Device("mic".into()),
252 ..JoinOptions::default()
253 };
254 assert_eq!(
255 options.commands_after_join(),
256 vec![CallCommand::DisableAudio]
257 );
258 }
259}