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