From 9665f859e4a3f78f2e1f18db4101e52bee989d9a Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:27:37 -0500 Subject: [PATCH 01/19] feat(recording): switch the microphone or camera mid-recording from the controls bar --- apps/desktop-gpui/locales/de.toml | 8 + apps/desktop-gpui/locales/en.toml | 8 + apps/desktop-gpui/locales/es.toml | 8 + apps/desktop-gpui/locales/fr.toml | 8 + apps/desktop-gpui/locales/hi.toml | 8 + apps/desktop-gpui/locales/ja.toml | 8 + apps/desktop-gpui/locales/zh-CN.toml | 8 + apps/desktop-gpui/src/app_windows.rs | 75 ++- apps/desktop-gpui/src/controls_window.rs | 543 +++++++++++++++++- apps/desktop-gpui/src/feeds.rs | 36 ++ apps/desktop-gpui/src/i18n/surface_tests.rs | 23 + apps/desktop-gpui/src/recording.rs | 69 +++ apps/desktop-gpui/src/session.rs | 18 +- apps/desktop-gpui/src/session/input_switch.rs | 537 +++++++++++++++++ crates/recording/src/feeds/microphone.rs | 7 + crates/recording/src/instant_recording.rs | 37 +- crates/recording/src/sources/microphone.rs | 421 +++++++++++++- .../src/sources/screen_capture/linux.rs | 1 + crates/recording/src/studio_recording.rs | 45 ++ 19 files changed, 1815 insertions(+), 53 deletions(-) create mode 100644 apps/desktop-gpui/src/session/input_switch.rs diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml index c96d36f22a2..d894b85fef7 100644 --- a/apps/desktop-gpui/locales/de.toml +++ b/apps/desktop-gpui/locales/de.toml @@ -1178,6 +1178,14 @@ starting = "Startet" [controls.marker] add = "Marker hinzufügen" +[controls.settings] +locked = "In dieser Aufnahme nicht änderbar" +not_in_recording = "Nicht in dieser Aufnahme" +open = "Aufnahmeeinstellungen" +show_camera_preview = "Kameravorschau anzeigen" +switch_failed = "Wechsel zu {device} nicht möglich. Die Aufnahme läuft weiter." +switching = "Wird gewechselt…" + [camera] waiting_title = "Warten auf Kamerazugriff" diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml index 29f268aa5c6..eb1f8af0781 100644 --- a/apps/desktop-gpui/locales/en.toml +++ b/apps/desktop-gpui/locales/en.toml @@ -1180,6 +1180,14 @@ starting = { text = "Starting", max_px = 73, size = 14 } [controls.marker] add = "Add marker" +[controls.settings] +locked = "Can't change in this recording" +not_in_recording = "Not in this recording" +open = "Recording settings" +show_camera_preview = "Show camera preview" +switch_failed = "Couldn't switch to {device}. The recording continues." +switching = "Switching…" + [camera] waiting_title = "Waiting for camera access" diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml index 27fbf306d17..32a9a4f67fb 100644 --- a/apps/desktop-gpui/locales/es.toml +++ b/apps/desktop-gpui/locales/es.toml @@ -1178,6 +1178,14 @@ starting = "Iniciando" [controls.marker] add = "Añadir marcador" +[controls.settings] +locked = "No se puede cambiar en esta grabación" +not_in_recording = "No está en esta grabación" +open = "Ajustes de grabación" +show_camera_preview = "Mostrar vista previa de la cámara" +switch_failed = "No se pudo cambiar a {device}. La grabación continúa." +switching = "Cambiando…" + [camera] waiting_title = "Esperando acceso a la cámara" diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml index 87b46c59460..5eafa3fe250 100644 --- a/apps/desktop-gpui/locales/fr.toml +++ b/apps/desktop-gpui/locales/fr.toml @@ -1178,6 +1178,14 @@ starting = "Début" [controls.marker] add = "Ajouter un repère" +[controls.settings] +locked = "Non modifiable dans cet enregistrement" +not_in_recording = "Absent de cet enregistrement" +open = "Réglages d'enregistrement" +show_camera_preview = "Afficher l'aperçu de la caméra" +switch_failed = "Impossible de passer à {device}. L'enregistrement continue." +switching = "Changement…" + [camera] waiting_title = "En attente de l'accès à la caméra" diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml index 319ed6675bb..35629ef033a 100644 --- a/apps/desktop-gpui/locales/hi.toml +++ b/apps/desktop-gpui/locales/hi.toml @@ -1178,6 +1178,14 @@ starting = "शुरू हो रहा" [controls.marker] add = "मार्कर जोड़ें" +[controls.settings] +locked = "इस रिकॉर्डिंग में बदला नहीं जा सकता" +not_in_recording = "इस रिकॉर्डिंग में नहीं" +open = "रिकॉर्डिंग सेटिंग्स" +show_camera_preview = "कैमरा प्रीव्यू दिखाएँ" +switch_failed = "{device} पर स्विच नहीं हो सका। रिकॉर्डिंग जारी है।" +switching = "बदला जा रहा है…" + [camera] waiting_title = "कैमरा एक्सेस का इंतज़ार है" diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml index d007debfd05..159c4ad8ed6 100644 --- a/apps/desktop-gpui/locales/ja.toml +++ b/apps/desktop-gpui/locales/ja.toml @@ -1178,6 +1178,14 @@ starting = "開始中" [controls.marker] add = "マーカーを追加" +[controls.settings] +locked = "この録画では変更できません" +not_in_recording = "この録画では未使用" +open = "録画設定" +show_camera_preview = "カメラプレビューを表示" +switch_failed = "{device} に切り替えられませんでした。録画は続行中です。" +switching = "切り替え中…" + [camera] waiting_title = "カメラへのアクセスを待っています" diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml index 922d4ee4a5d..bd43461c90f 100644 --- a/apps/desktop-gpui/locales/zh-CN.toml +++ b/apps/desktop-gpui/locales/zh-CN.toml @@ -1178,6 +1178,14 @@ starting = "正在开始" [controls.marker] add = "添加标记" +[controls.settings] +locked = "本次录制中无法更改" +not_in_recording = "本次录制未使用" +open = "录制设置" +show_camera_preview = "显示摄像头预览" +switch_failed = "无法切换到 {device}。录制仍在继续。" +switching = "正在切换…" + [camera] waiting_title = "正在等待摄像头权限" diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index b09231375a2..c220b75293b 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -40,7 +40,7 @@ use crate::{ /// Matches the Tauri `InProgressRecording` window and /// `fake_window.rs` position math. -pub const CONTROLS_WIDTH: f32 = 320.; +pub const CONTROLS_WIDTH: f32 = 352.; pub const CONTROLS_HEIGHT: f32 = 150.; /// The bar alone, with its padding. A transparent window takes every click /// inside its frame, so without an issue to show the window is only this @@ -306,6 +306,10 @@ pub struct AppWindows { /// Where the camera bubble sat before a window picker parked it inside the /// highlighted window -- see [`CameraPark`]. pub camera_park: CameraPark, + /// The recording bar's "Show camera preview" turned the bubble off. It + /// is hidden, not closed, so a Studio capture keeps excluding the same + /// window when it comes back. + camera_preview_hidden: bool, #[cfg(target_os = "linux")] clean_capture: Option, #[cfg(target_os = "linux")] @@ -575,6 +579,7 @@ pub fn init(main: WindowHandle, session: Entity, c main_closed_by_user: false, editor_hidden_for_picker: None, camera_park: CameraPark::default(), + camera_preview_hidden: false, #[cfg(target_os = "linux")] clean_capture: None, #[cfg(target_os = "linux")] @@ -630,6 +635,9 @@ pub fn init(main: WindowHandle, session: Entity, c } close_controls(&session, cx); close_target_overlays(cx); + if cx.global::().camera_preview_hidden { + set_camera_preview_shown(true, cx); + } #[cfg(target_os = "linux")] if !restore_clean_capture_ui(cx) { return; @@ -4409,6 +4417,7 @@ pub fn open_camera_window(cx: &mut App) { } pub fn close_camera_window(cx: &mut App) { + cx.global_mut::().camera_preview_hidden = false; if let Some(handle) = cx.global_mut::().camera.take() { #[cfg(not(target_os = "macos"))] if let Some(previous) = handle @@ -7234,6 +7243,55 @@ pub fn arm_target_mode(kind: crate::main_window::TargetType, cx: &mut App) { .ok(); } +#[cfg(target_os = "macos")] +pub(crate) fn camera_window_id(cx: &mut App) -> Option { + camera_window_number(cx).and_then(|number| number.to_string().parse().ok()) +} + +pub(crate) fn camera_preview_shown(cx: &App) -> bool { + let windows = cx.global::(); + windows.camera.is_some() && !windows.camera_preview_hidden +} + +/// The bar's "Show camera preview". Hides and shows the bubble in place; +/// Linux has no native hide for it, so there it closes and reopens. +pub(crate) fn set_camera_preview_shown(shown: bool, cx: &mut App) { + let Some(handle) = cx.global::().camera else { + if shown { + open_camera_window(cx); + } + return; + }; + #[cfg(target_os = "linux")] + { + let _ = handle; + if !shown { + close_camera_window(cx); + } + } + #[cfg(not(target_os = "linux"))] + { + if cx.global::().camera_preview_hidden != shown { + return; + } + cx.global_mut::().camera_preview_hidden = !shown; + let native = handle + .update(cx, |_, window, _| platform::native_window(window)) + .ok() + .flatten(); + if let Some(native) = native { + cx.spawn(async move |_| { + if shown { + platform::order_front_native(&native); + } else { + platform::hide_native(&native); + } + }) + .detach(); + } + } +} + fn camera_window_number(cx: &mut App) -> Option { let handle = cx.global::().camera?; handle @@ -7293,15 +7351,11 @@ fn controls_origin(config: &StartConfig) -> (f64, f64) { } } -/// Grow the bar's window upward to make room for an issue card above the -/// bar, or shrink it back to the bar alone, leaving the bar where it is. -/// `setFrame` re-enters gpui, so call this from a task. -pub(crate) fn fit_controls_to_issue(native: &platform::NativeWindow, issue: bool, scale: f32) { - let height = f64::from(if issue { - CONTROLS_HEIGHT - } else { - CONTROLS_BAR_HEIGHT - }); +/// Grow the bar's window upward to `height` to make room for the cards above +/// the bar (an issue, the settings panel), or shrink it back, leaving the bar +/// where it is. `setFrame` re-enters gpui, so call this from a task. +pub(crate) fn fit_controls(native: &platform::NativeWindow, height: f32, scale: f32) { + let height = f64::from(height); let (x, y, width, current) = platform::window_frame(native); if width <= 0. { return; @@ -8163,6 +8217,7 @@ mod tests { main_closed_by_user: false, editor_hidden_for_picker: None, camera_park: CameraPark::default(), + camera_preview_hidden: false, clean_capture: None, clean_capture_generation: 0, } diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index 02dfb24891f..e5fd8bbcd55 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -18,9 +18,10 @@ use gpui::{ use crate::i18n::tr; use crate::theme::LocaleFont as _; use crate::{ - feeds::Feeds, + devices::CameraOption, + feeds::{DeviceOrModelID, Feeds, SelectedCamera}, recording::RecordingMode, - session::{Phase, RecordingSession}, + session::{InputKind, InputRequest, Phase, RecordingSession}, theme::Theme, ui, }; @@ -36,7 +37,14 @@ pub struct ControlsWindow { _tick: gpui::Task<()>, ticking_live: bool, level_bar: Entity, - issue_room: bool, + /// The window height last asked of the platform: the bar, plus the issue + /// card and the settings panel when they are up. + fitted_height: f32, + settings_open: bool, + settings_dismissed_at: Option, + microphones: Vec, + cameras: Vec, + _enumerate: gpui::Task<()>, /// Repaints the marker button once its acknowledgement has played out. _marker_settle: gpui::Task<()>, marker_seen: Option, @@ -45,6 +53,38 @@ pub struct ControlsWindow { /// How long the marker button stays lit after a marker goes in. const MARKER_ACKNOWLEDGEMENT: Duration = Duration::from_millis(900); +const SETTINGS_ROW_HEIGHT: f32 = 30.; +const SETTINGS_HEADING_HEIGHT: f32 = 26.; +const SETTINGS_DIVIDER_MARGIN: f32 = 4.; +const SETTINGS_PANEL_PADDING: f32 = 4.; +const SETTINGS_PANEL_MAX_HEIGHT: f32 = 360.; +/// The card above the bar keeps this gap to it, like the issue card. +const SETTINGS_PANEL_GAP: f32 = 8.; + +/// The panel's height for its rows: the camera preview switch, then each +/// heading with its rows, the dividers between them, padding and border. +/// Longer lists scroll inside the cap. +fn settings_panel_height(microphone_rows: usize, camera_rows: usize) -> f32 { + let divider = 1. + 2. * SETTINGS_DIVIDER_MARGIN; + let content = SETTINGS_ROW_HEIGHT + + 2. * (divider + SETTINGS_HEADING_HEIGHT) + + (microphone_rows + camera_rows) as f32 * SETTINGS_ROW_HEIGHT; + (content + 2. * (SETTINGS_PANEL_PADDING + 1.)).min(SETTINGS_PANEL_MAX_HEIGHT) +} + +/// The bar window's height: the bar, the issue card when one is up, and the +/// settings panel when it is open. +fn controls_height(issue: bool, settings_panel: Option) -> f32 { + let mut height = crate::app_windows::CONTROLS_BAR_HEIGHT; + if issue { + height += crate::app_windows::CONTROLS_HEIGHT - crate::app_windows::CONTROLS_BAR_HEIGHT; + } + if let Some(panel) = settings_panel { + height += panel + SETTINGS_PANEL_GAP; + } + height +} + #[derive(Clone, Copy)] enum DestructiveAction { Restart, @@ -70,7 +110,12 @@ impl ControlsWindow { crate::theme::bind_window(window, cx); let theme = Theme::for_window(window, cx, false); cx.observe_in(&session, window, |this, _, window, cx| { - this.fit_to_issue(window, cx); + if this.settings_open + && !matches!(this.session.read(cx).phase, Phase::Recording { .. }) + { + this.settings_open = false; + } + this.fit_window(window, cx); this.settle_marker_acknowledgement(window, cx); let live = this.timer_is_live(cx); if live != this.ticking_live { @@ -80,6 +125,11 @@ impl ControlsWindow { cx.notify(); }) .detach(); + cx.observe_in(&Feeds::global(cx), window, |this, _, window, cx| { + this.fit_window(window, cx); + cx.notify(); + }) + .detach(); let mic_level = Feeds::global(cx).read(cx).mic_level().clone(); let level_bar = cx.new(|cx| { ui::MicrophoneLevel::new( @@ -99,14 +149,19 @@ impl ControlsWindow { _tick: gpui::Task::ready(()), ticking_live: false, level_bar, - issue_room: false, + fitted_height: crate::app_windows::CONTROLS_BAR_HEIGHT, + settings_open: false, + settings_dismissed_at: None, + microphones: Vec::new(), + cameras: Vec::new(), + _enumerate: gpui::Task::ready(()), _marker_settle: gpui::Task::ready(()), marker_seen: None, }; this.ticking_live = this.timer_is_live(cx); this._tick = Self::spawn_tick(window, cx); // Once the platform window is finished: an issue may already be up. - cx.defer_in(window, |this, window, cx| this.fit_to_issue(window, cx)); + cx.defer_in(window, |this, window, cx| this.fit_window(window, cx)); this } @@ -115,6 +170,7 @@ impl ControlsWindow { session .error .clone() + .or_else(|| session.input_switch_error().map(str::to_owned)) .or_else(|| { (session.paused_for_sleep && session.is_paused()) .then(|| tr::controls::paused_for_sleep().to_owned()) @@ -126,22 +182,84 @@ impl ControlsWindow { }) } - fn fit_to_issue(&mut self, window: &mut Window, cx: &mut Context) { - let issue = self.issue(cx).is_some(); - if issue == self.issue_room { + fn fit_window(&mut self, window: &mut Window, cx: &mut Context) { + let panel = self.settings_open.then(|| { + let (microphones, cameras) = self.settings_rows(cx); + settings_panel_height(microphones, cameras) + }); + let height = controls_height(self.issue(cx).is_some(), panel); + if height == self.fitted_height { return; } - self.issue_room = issue; + self.fitted_height = height; let Some(native) = crate::platform::native_window(window) else { return; }; let scale = window.scale_factor(); cx.spawn(async move |_, _| { - crate::app_windows::fit_controls_to_issue(&native, issue, scale); + crate::app_windows::fit_controls(&native, height, scale); }) .detach(); } + fn toggle_settings(&mut self, window: &mut Window, cx: &mut Context) { + // The press that dismissed the open panel from outside lands here too. + if !self.settings_open + && self + .settings_dismissed_at + .is_some_and(|at| at.elapsed() < Duration::from_millis(400)) + { + self.settings_dismissed_at = None; + return; + } + self.settings_open = !self.settings_open; + if self.settings_open { + self.refresh_devices(window, cx); + } + self.fit_window(window, cx); + cx.notify(); + } + + fn close_settings(&mut self, window: &mut Window, cx: &mut Context) { + if self.settings_open { + self.settings_open = false; + self.fit_window(window, cx); + cx.notify(); + } + } + + /// `listAudioDevices` / `listCameras` when the menu opens, off the main + /// thread. Until it lands the panel lists what the recording is using. + fn refresh_devices(&mut self, window: &mut Window, cx: &mut Context) { + let previous = self.cameras.clone(); + self._enumerate = cx.spawn_in(window, async move |this, cx| { + let (microphones, cameras) = cx + .background_executor() + .spawn(async move { + let microphones = match crate::devices::InputSnapshot::microphones() { + crate::devices::InputSnapshot::Microphones(microphones) => microphones + .into_iter() + .map(|microphone| microphone.name) + .collect(), + crate::devices::InputSnapshot::Cameras(_) => Vec::new(), + }; + let cameras = match crate::devices::InputSnapshot::cameras(&previous) { + crate::devices::InputSnapshot::Cameras(cameras) => cameras, + crate::devices::InputSnapshot::Microphones(_) => Vec::new(), + }; + (microphones, cameras) + }) + .await; + this.update_in(cx, |this, window, cx| { + this.microphones = microphones; + this.cameras = cameras; + this.fit_window(window, cx); + cx.notify(); + }) + .ok(); + }); + } + fn timer_is_live(&self, cx: &gpui::App) -> bool { matches!( self.session.read(cx).phase, @@ -396,23 +514,28 @@ impl ControlsWindow { let can_mute = session.mode() == Some(RecordingMode::Instant) && session.has_microphone() && matches!(session.phase, Phase::Recording { .. }); + // A Studio recording switched to "No microphone" records no mic from + // then on; Instant keeps the track and shows it muted instead. + let has_microphone = self.has_microphone + && (session.mode() == Some(RecordingMode::Instant) + || Feeds::global(cx).read(cx).microphone.is_some()); self.level_bar.update(cx, |bar, cx| { bar.configure( - self.has_microphone && !muted, + has_microphone && !muted, theme.blue_9.into(), theme.gray_10.into(), cx, ); }); - let icon = if !self.has_microphone || muted { + let icon = if !has_microphone || muted { "icons/mic-off.svg" } else { "icons/microphone.svg" }; let icon_color: gpui::Hsla = if muted { theme.red_9.into() - } else if self.has_microphone { + } else if has_microphone { theme.gray_12.into() } else { // `IconLucideMicOff text-gray-7`. @@ -438,7 +561,7 @@ impl ControlsWindow { })) }) .child(svg().path(icon).size(px(20.)).text_color(icon_color)) - .when(self.has_microphone && !muted, |this| { + .when(has_microphone && !muted, |this| { this.child(self.level_bar.clone()) }) } @@ -526,6 +649,364 @@ impl ControlsWindow { .detach(); } + /// The microphones the menu offers: what was found, plus the one in use + /// if enumeration has not caught up with it yet. + fn microphone_choices(&self, cx: &gpui::App) -> Vec { + let mut choices = self.microphones.clone(); + if let Some(current) = &Feeds::global(cx).read(cx).microphone + && !choices.contains(current) + { + choices.insert(0, current.clone()); + } + choices + } + + fn camera_choices(&self, cx: &gpui::App) -> Vec { + let mut choices: Vec = self + .cameras + .iter() + .map(|camera| SelectedCamera { + id: camera + .model_id + .clone() + .map(DeviceOrModelID::ModelID) + .unwrap_or_else(|| DeviceOrModelID::DeviceID(camera.device_id.clone())), + label: camera.label.clone(), + device_id: camera.device_id.clone(), + model_id: camera.model_id.clone(), + }) + .collect(); + if let Some(current) = &Feeds::global(cx).read(cx).camera + && !choices + .iter() + .any(|choice| choice.device_id == current.device_id) + { + choices.insert(0, current.clone()); + } + choices + } + + /// Rows under each heading, "None" included; a heading whose input this + /// recording cannot change shows no rows. + fn settings_rows(&self, cx: &gpui::App) -> (usize, usize) { + let session = self.session.read(cx); + let microphones = if session.microphone_switch_available() { + self.microphone_choices(cx).len() + 1 + } else { + 0 + }; + let cameras = if session.camera_switch_available() { + self.camera_choices(cx).len() + 1 + } else { + 0 + }; + (microphones, cameras) + } + + fn pick_input(&mut self, request: InputRequest, window: &mut Window, cx: &mut Context) { + self.session + .update(cx, |session, cx| session.switch_input(request, cx)); + self.close_settings(window, cx); + } + + fn render_settings_button(&self, cx: &mut Context) -> gpui::AnyElement { + let theme = self.theme; + let available = matches!(self.session.read(cx).phase, Phase::Recording { .. }); + let open = self.settings_open; + self.action_button("settings", "icons/settings.svg", !available) + .when(open, |this| { + this.bg(Theme::with_alpha(theme.gray_12, 0.08)) + }) + .when(!open, |this| { + this.tooltip(move |_, cx| { + ui::Tooltip::new(&theme, tr::controls::settings::open()).view(cx) + }) + }) + .when(available, |this| { + this.on_click(cx.listener(|this, _, window, cx| { + this.toggle_settings(window, cx); + })) + }) + .into_any_element() + } + + fn settings_row( + &self, + id: SharedString, + icon: &'static str, + label: SharedString, + selected: bool, + enabled: bool, + ) -> gpui::Stateful { + let theme = self.theme; + div() + .id(id) + .h(px(SETTINGS_ROW_HEIGHT)) + .flex_shrink_0() + .flex() + .flex_row() + .items_center() + .gap(px(8.)) + .px(px(8.)) + .rounded(px(8.)) + .when(enabled, |this| { + this.cursor_pointer() + .hover(|style| style.bg(Theme::with_alpha(theme.gray_12, 0.06))) + .active(|style| style.bg(Theme::with_alpha(theme.gray_12, 0.10))) + }) + .when(!enabled, |this| this.opacity(0.5)) + .child( + svg() + .path(icon) + .size(px(16.)) + .flex_shrink_0() + .text_color(theme.gray_11), + ) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .text_size(px(13.)) + .text_color(theme.gray_12) + .child(label), + ) + .when(selected, |this| { + this.child( + svg() + .path("icons/check.svg") + .size(px(14.)) + .flex_shrink_0() + .text_color(theme.blue_9), + ) + }) + } + + fn settings_heading( + &self, + label: &'static str, + note: Option<&'static str>, + ) -> impl IntoElement { + let theme = self.theme; + div() + .h(px(SETTINGS_HEADING_HEIGHT)) + .flex_shrink_0() + .flex() + .flex_row() + .items_center() + .justify_between() + .gap(px(8.)) + .px(px(8.)) + .child( + div() + .flex_shrink_0() + .text_size(px(12.)) + .text_color(theme.gray_11) + .child(label), + ) + .children(note.map(|note| { + div() + .min_w_0() + .truncate() + .text_size(px(12.)) + .text_color(theme.gray_10) + .child(note) + })) + } + + fn settings_divider(&self) -> impl IntoElement { + div() + .h(px(1.)) + .my(px(SETTINGS_DIVIDER_MARGIN)) + .mx(px(4.)) + .flex_shrink_0() + .bg(self.theme.gray_4) + } + + fn render_settings_panel(&self, cx: &mut Context) -> impl IntoElement { + let theme = self.theme; + let session = self.session.read(cx); + let instant = session.mode() == Some(RecordingMode::Instant); + let pending = session.input_switch_pending(); + let microphone_available = session.microphone_switch_available(); + let camera_available = session.camera_switch_available(); + let microphone_muted = instant && session.mic_muted; + let started_with_microphone = session.has_microphone(); + let started_with_camera = session.started_with_camera(); + let feeds = Feeds::global(cx).read(cx); + let selected_microphone = feeds.microphone.clone().filter(|_| !microphone_muted); + let selected_camera = feeds.camera.as_ref().map(|camera| camera.device_id.clone()); + let has_camera = feeds.camera.is_some(); + let preview_shown = crate::app_windows::camera_preview_shown(cx); + let microphones = self.microphone_choices(cx); + let cameras = self.camera_choices(cx); + + let note = |available: bool, started: bool, kind: InputKind| { + if pending == Some(kind) { + Some(tr::controls::settings::switching()) + } else if available { + None + } else if started { + Some(tr::controls::settings::locked()) + } else { + Some(tr::controls::settings::not_in_recording()) + } + }; + + let mut panel = div() + .id("settings-panel") + .w_full() + .max_h(px(SETTINGS_PANEL_MAX_HEIGHT)) + .mb(px(8.)) + .flex() + .flex_col() + .p(px(SETTINGS_PANEL_PADDING)) + .rounded(px(12.)) + .border_1() + .border_color(theme.gray_5) + .bg(theme.gray_1) + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.1), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(3.), + spread_radius: px(0.), + inset: false, + }]) + .overflow_y_scroll() + .on_mouse_down_out(cx.listener(|this, _, window, cx| { + if this.settings_open { + this.settings_dismissed_at = Some(std::time::Instant::now()); + this.close_settings(window, cx); + } + })) + .child( + div() + .id("show-camera-preview") + .h(px(SETTINGS_ROW_HEIGHT)) + .flex_shrink_0() + .flex() + .flex_row() + .items_center() + .justify_between() + .gap(px(8.)) + .px(px(8.)) + .when(!has_camera, |this| this.opacity(0.5)) + .child( + div() + .min_w_0() + .truncate() + .text_size(px(13.)) + .text_color(theme.gray_12) + .child(tr::controls::settings::show_camera_preview()), + ) + .child( + ui::Toggle::glass(&theme, "show-camera-preview-toggle", preview_shown) + .disabled(&theme, !has_camera) + .when(has_camera, |toggle| { + toggle.on_click(cx.listener(move |_, _, _, cx| { + crate::app_windows::set_camera_preview_shown( + !preview_shown, + cx, + ); + cx.notify(); + })) + }), + ), + ) + .child(self.settings_divider()) + .child(self.settings_heading( + tr::main::device::microphone(), + note( + microphone_available, + started_with_microphone, + InputKind::Microphone, + ), + )); + + if microphone_available { + let enabled = pending != Some(InputKind::Microphone); + panel = panel.child( + self.settings_row( + "microphone-none".into(), + "icons/mic-off.svg", + tr::main::device::no_microphone().into(), + selected_microphone.is_none(), + enabled, + ) + .when(enabled, |this| { + this.on_click(cx.listener(|this, _, window, cx| { + this.pick_input(InputRequest::Microphone(None), window, cx); + })) + }), + ); + for (index, name) in microphones.into_iter().enumerate() { + let selected = selected_microphone.as_deref() == Some(name.as_str()); + let chosen = name.clone(); + panel = panel.child( + self.settings_row( + SharedString::from(format!("microphone-{index}")), + "icons/microphone.svg", + name.into(), + selected, + enabled, + ) + .when(enabled, |this| { + this.on_click(cx.listener(move |this, _, window, cx| { + this.pick_input( + InputRequest::Microphone(Some(chosen.clone())), + window, + cx, + ); + })) + }), + ); + } + } + + panel = panel.child(self.settings_divider()).child(self.settings_heading( + tr::main::device::camera(), + note(camera_available, started_with_camera, InputKind::Camera), + )); + + if camera_available { + let enabled = pending != Some(InputKind::Camera); + panel = panel.child( + self.settings_row( + "camera-none".into(), + "icons/circle-x.svg", + tr::main::device::no_camera().into(), + selected_camera.is_none(), + enabled, + ) + .when(enabled, |this| { + this.on_click(cx.listener(|this, _, window, cx| { + this.pick_input(InputRequest::Camera(None), window, cx); + })) + }), + ); + for (index, camera) in cameras.into_iter().enumerate() { + let selected = selected_camera.as_deref() == Some(camera.device_id.as_str()); + let label = SharedString::from(camera.label.clone()); + panel = panel.child( + self.settings_row( + SharedString::from(format!("camera-{index}")), + "icons/camera.svg", + label, + selected, + enabled, + ) + .when(enabled, |this| { + this.on_click(cx.listener(move |this, _, window, cx| { + this.pick_input(InputRequest::Camera(Some(camera.clone())), window, cx); + })) + }), + ); + } + } + + panel + } + fn render_bar(&mut self, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let session = self.session.read(cx); @@ -634,14 +1115,8 @@ impl ControlsWindow { })) }, )) - // Studio's marker button takes the slot of the - // settings menu, which has no popover yet. - .child(if studio { - self.render_marker(cx) - } else { - self.action_button("settings", "icons/settings.svg", true) - .into_any_element() - }), + .when(studio, |this| this.child(self.render_marker(cx))) + .child(self.render_settings_button(cx)), ), ) .child( @@ -686,6 +1161,9 @@ impl Render for ControlsWindow { .locale_font() // `body { font-weight: 500 }` (`ui-solid/src/main.css:189-192`). .font_weight(FontWeight::MEDIUM) + .when(self.settings_open, |this| { + this.child(self.render_settings_panel(cx)) + }) .when_some(issue, |this, issue| { this.child( div() @@ -754,6 +1232,25 @@ fn until_next_second(elapsed: Duration) -> Duration { mod repaint_tests { use super::*; + #[test] + fn the_window_grows_for_the_issue_and_the_settings_panel() { + let bar = crate::app_windows::CONTROLS_BAR_HEIGHT; + let with_issue = crate::app_windows::CONTROLS_HEIGHT; + assert_eq!(controls_height(false, None), bar); + assert_eq!(controls_height(true, None), with_issue); + let panel = settings_panel_height(3, 2); + assert_eq!(controls_height(false, Some(panel)), bar + panel + 8.); + assert_eq!(controls_height(true, Some(panel)), with_issue + panel + 8.); + } + + #[test] + fn the_settings_panel_fits_its_rows_and_scrolls_past_the_cap() { + let empty = settings_panel_height(0, 0); + assert_eq!(empty, 30. + 2. * (9. + 26.) + 10.); + assert_eq!(settings_panel_height(2, 1), empty + 3. * 30.); + assert_eq!(settings_panel_height(40, 40), 360.); + } + #[test] fn the_timer_wakes_once_per_displayed_second() { assert_eq!( diff --git a/apps/desktop-gpui/src/feeds.rs b/apps/desktop-gpui/src/feeds.rs index c8333393972..c1aff41530d 100644 --- a/apps/desktop-gpui/src/feeds.rs +++ b/apps/desktop-gpui/src/feeds.rs @@ -1327,6 +1327,42 @@ impl Feeds { cx.notify(); } + /// An Instant recording moved onto `label`, opened on a feed of its own. + /// Selects it (the main window persists that) and lets the app feed close + /// the microphone the recording left, so the level meter only hears the + /// new one. The app feed reopens the selection after the recording. + pub fn adopt_recording_microphone(&mut self, label: String, cx: &mut Context) { + self.mic_epoch += 1; + self.mic_input_epoch + .store(self.mic_epoch, Ordering::Release); + self.microphone_settings = crate::store::RecordingDeviceSettings::for_microphone(&label); + self.microphone = Some(label); + self.mic_input_pending = false; + self.mic_input_released = true; + self.microphone_error = None; + self.applied_settings.microphone = None; + self.microphone_ready = None; + if let Some(actor) = self.mic_actor.clone() { + let gate = self.mic_input_gate.clone(); + let current_epoch = self.mic_input_epoch.clone(); + let epoch = self.mic_epoch; + gpui_tokio::Tokio::spawn(cx, async move { + if let Err(error) = camera_input_operation(&gate, ¤t_epoch, epoch, async { + actor + .ask(microphone::RemoveInput) + .await + .map_err(|error| error.to_string()) + }) + .await + { + tracing::warn!("releasing the microphone a recording switched away from: {error}"); + } + }) + .detach(); + } + cx.notify(); + } + pub fn mic_level(&self) -> &Entity { &self.mic_level } diff --git a/apps/desktop-gpui/src/i18n/surface_tests.rs b/apps/desktop-gpui/src/i18n/surface_tests.rs index 1ef8b8d6d4f..b9758aaaf51 100644 --- a/apps/desktop-gpui/src/i18n/surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/surface_tests.rs @@ -38,6 +38,7 @@ const FORMATTED: &[&str] = &[ "main.storage.blocked", "main.storage.can_start", "main.storage.check_failed", + "controls.settings.switch_failed", ]; #[test] @@ -291,6 +292,20 @@ fn main_surfaces_match_the_literals_they_replaced() { (tr::controls::pause::pausing(), "Pausing recording…"), (tr::controls::pause::resume(), "Resume recording"), (tr::controls::marker::add(), "Add marker"), + (tr::controls::settings::open(), "Recording settings"), + ( + tr::controls::settings::show_camera_preview(), + "Show camera preview", + ), + (tr::controls::settings::switching(), "Switching…"), + ( + tr::controls::settings::locked(), + "Can't change in this recording", + ), + ( + tr::controls::settings::not_in_recording(), + "Not in this recording", + ), (tr::controls::pause::pause(), "Pause recording"), ( tr::controls::low_storage(), @@ -639,6 +654,14 @@ fn main_surfaces_match_the_literals_they_replaced() { } } +#[test] +fn a_failed_input_switch_names_the_device_and_says_recording_continues() { + assert_eq!( + tr::controls::settings::switch_failed("USB Mic"), + "Couldn't switch to USB Mic. The recording continues." + ); +} + #[test] fn recovery_errors_name_the_folder_and_say_nothing_was_deleted() { let path = "/Users/me/Cap/recording.cap"; diff --git a/apps/desktop-gpui/src/recording.rs b/apps/desktop-gpui/src/recording.rs index 38165838b54..2527045be27 100644 --- a/apps/desktop-gpui/src/recording.rs +++ b/apps/desktop-gpui/src/recording.rs @@ -112,6 +112,34 @@ pub struct ActiveRecording { /// Keeps the library from deleting this folder until the recording, and /// any clone a stop future holds, is gone. _in_progress: Arc, + started_with_camera: bool, + camera_only: bool, + microphone_switch: Option, + /// The feed a mid-recording microphone switch opened. Replacing it drops + /// (and stops) the one before. + switched_microphone: Arc>>, +} + +pub(crate) struct SwitchedMicrophone { + _feed: Arc>, + _errors: flume::Receiver, +} + +pub(crate) struct OpenedMicrophone { + pub lock: Arc, + feed: Arc>, + errors: flume::Receiver, +} + +/// Opens `label` on a feed of its own, outside any recording, so a switch +/// only touches the recording once the device is known to work. +pub(crate) async fn open_microphone( + label: &str, + settings: Option, + meter: Option>, +) -> anyhow::Result { + let (feed, lock, errors) = setup_microphone(label, settings, meter).await?; + Ok(OpenedMicrophone { lock, feed, errors }) } #[cfg(target_os = "linux")] @@ -840,6 +868,39 @@ impl ActiveRecording { /// Marks `at` in a Studio recording and returns the new marker count. /// Instant recordings have no editor timeline to put a marker on, so /// they take none. + pub(crate) fn studio_handle(&self) -> Option { + match &self.handle { + Handle::Studio(handle) => Some(handle.clone()), + Handle::Instant(_) => None, + } + } + + pub(crate) fn microphone_switch(&self) -> Option { + self.microphone_switch.clone() + } + + pub(crate) fn started_with_camera(&self) -> bool { + self.started_with_camera + } + + pub(crate) fn camera_only(&self) -> bool { + self.camera_only + } + + /// Keeps the feed a microphone switch moved the recording onto alive for + /// as long as the recording, releasing the one it replaced. + pub(crate) fn adopt_switched_microphone(&self, opened: OpenedMicrophone) { + let previous = self + .switched_microphone + .lock() + .unwrap() + .replace(SwitchedMicrophone { + _feed: opened.feed, + _errors: opened.errors, + }); + drop(previous); + } + pub fn add_marker(&self, at: std::time::Instant) -> Option { match &self.handle { Handle::Studio(handle) => handle.add_marker(at), @@ -2684,6 +2745,10 @@ async fn start_attempt_with_upload( } }; + let microphone_switch = match &handle { + Handle::Instant(handle) => handle.microphone_switch(), + Handle::Studio(_) => None, + }; #[cfg(target_os = "linux")] let instant_completion = instant_upload .as_ref() @@ -2712,6 +2777,10 @@ async fn start_attempt_with_upload( _camera_feed: camera_feed, _mic_errors: mic_errors, _in_progress: in_progress, + started_with_camera: config.camera.is_some(), + camera_only: matches!(config.target, ScreenCaptureTarget::CameraOnly), + microphone_switch, + switched_microphone: Arc::new(Mutex::new(None)), }) } diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs index e060273d970..9e31b5fa153 100644 --- a/apps/desktop-gpui/src/session.rs +++ b/apps/desktop-gpui/src/session.rs @@ -18,6 +18,9 @@ use crate::app_sounds::AppSound; use crate::i18n::tr; use crate::recording::{self, ActiveRecording, StartConfig}; +mod input_switch; +pub use input_switch::{InputKind, InputRequest}; + /// `Idle -> Starting -> Recording -> Stopping -> Idle`; pause is a flag on /// `Recording`, matching the `recording`/`paused` variants of the Tauri bar. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -480,6 +483,7 @@ pub struct RecordingSession { /// When the last marker went in, for the controls bar's acknowledgement. marker_added_at: Option, pub paused_for_sleep: bool, + input_switch: input_switch::InputSwitchState, #[cfg(target_os = "linux")] instant_attempt: Option, #[cfg(target_os = "linux")] @@ -532,6 +536,7 @@ impl RecordingSession { marker_count: 0, marker_added_at: None, paused_for_sleep: false, + input_switch: input_switch::InputSwitchState::default(), #[cfg(target_os = "linux")] instant_attempt: None, #[cfg(target_os = "linux")] @@ -598,7 +603,10 @@ impl RecordingSession { } pub fn pause_unavailable(&self) -> bool { - self.pause_pending() || self.pause_uncertain() || self.pipeline_failed + self.pause_pending() + || self.pause_uncertain() + || self.pipeline_failed + || self.input_switch_pending().is_some() } /// The live (or last) recording's mode -- the bar exposes mic mute for @@ -676,6 +684,13 @@ impl RecordingSession { .is_some_and(|active| active.mic_mute.is_some()) } + /// Whether the live recording started with a camera. + pub fn started_with_camera(&self) -> bool { + self.active + .as_ref() + .is_some_and(ActiveRecording::started_with_camera) + } + /// Flip the recording-scoped mic mute. No-op without a live mic. pub fn toggle_mic_mute(&mut self, cx: &mut Context) { let Some(mute) = self @@ -1034,6 +1049,7 @@ impl RecordingSession { let project_dir = active.project_dir.clone(); let done = active.done_fut(); self.active = Some(active); + self.input_switch = input_switch::InputSwitchState::default(); self.phase = Phase::Recording { paused: false }; // A fresh mic lock always starts unmuted. self.mic_muted = false; diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs new file mode 100644 index 00000000000..8ff10e23705 --- /dev/null +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -0,0 +1,537 @@ +//! Switching the microphone or camera while a recording runs. +//! +//! Studio records each input as its own track per segment, so a switch cuts +//! the segment: pause, hand the actor the new device's lock, resume. The +//! time in between is counted as paused, so the bar's timer matches what +//! was recorded. Instant has one continuous audio track: the new microphone +//! is opened on a feed of its own first, and only once it works is the live +//! source moved onto it, so a device that fails to open never touches the +//! recording. Instant's camera is the on-screen bubble, which the recording +//! does not hold, so switching it is just a new preview selection. + +use std::{sync::Arc, time::Instant}; + +use cap_recording::feeds::{camera, microphone}; +use gpui::{AsyncApp, Context, WeakEntity}; + +use super::{Phase, RecordingSession}; +use crate::{ + feeds::{Feeds, InputReady, SelectedCamera}, + i18n::tr, + recording::{self, RecordingMode}, +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum InputKind { + Microphone, + Camera, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum InputRequest { + Microphone(Option), + Camera(Option), +} + +impl InputRequest { + pub fn kind(&self) -> InputKind { + match self { + Self::Microphone(_) => InputKind::Microphone, + Self::Camera(_) => InputKind::Camera, + } + } + + fn device_label(&self) -> String { + match self { + Self::Microphone(Some(label)) => label.clone(), + Self::Microphone(None) => tr::main::device::no_microphone().to_owned(), + Self::Camera(Some(camera)) => camera.label.clone(), + Self::Camera(None) => tr::main::device::no_camera().to_owned(), + } + } +} + +/// One switch runs at a time. Requests made while it runs collapse into the +/// newest one per input, which runs next, so rapid clicking ends on the last +/// choice without queueing every intermediate device. +#[derive(Default)] +pub(crate) struct InputSwitchState { + pending: Option, + queued: Vec, + pub(super) error: Option, + timer_frozen_at: Option, +} + +impl InputSwitchState { + fn submit(&mut self, request: InputRequest) -> Option { + if self.pending.is_some() { + self.queued.retain(|queued| queued.kind() != request.kind()); + self.queued.push(request); + return None; + } + self.pending = Some(request.kind()); + Some(request) + } + + fn finish(&mut self) -> Option { + self.pending = None; + let next = (!self.queued.is_empty()).then(|| self.queued.remove(0)); + if let Some(next) = &next { + self.pending = Some(next.kind()); + } + next + } +} + +/// What a Studio switch did, for the session to settle its state on. +struct StudioSwitchOutcome { + error: Option, + /// `Some(true)` when the recording was left paused (a resume failed), + /// `Some(false)` when it runs again, `None` when the actor never answered. + paused: Option, +} + +impl RecordingSession { + pub fn input_switch_pending(&self) -> Option { + self.input_switch.pending + } + + pub fn input_switch_error(&self) -> Option<&str> { + self.input_switch.error.as_deref() + } + + pub fn dismiss_input_switch_error(&mut self, cx: &mut Context) { + if self.input_switch.error.take().is_some() { + cx.notify(); + } + } + + fn input_switch_live(&self) -> bool { + matches!(self.phase, Phase::Recording { .. }) && !self.pipeline_failed + } + + /// The recording started with a microphone and its pipeline can move to + /// another one. One that started without a microphone has no audio track + /// to put one on. + pub fn microphone_switch_available(&self) -> bool { + self.input_switch_live() + && self.active.as_ref().is_some_and(|active| { + active.mic_mute.is_some() + && match self.mode() { + Some(RecordingMode::Studio) => true, + Some(RecordingMode::Instant) => active.microphone_switch().is_some(), + None => false, + } + }) + } + + /// The recording started with a camera that is not its only picture. + /// Linux composites the camera into Instant recordings, which holds it. + pub fn camera_switch_available(&self) -> bool { + self.input_switch_live() + && self.active.as_ref().is_some_and(|active| { + active.started_with_camera() + && !active.camera_only() + && match self.mode() { + Some(RecordingMode::Studio) => true, + Some(RecordingMode::Instant) => cfg!(not(target_os = "linux")), + None => false, + } + }) + } + + pub fn switch_input(&mut self, request: InputRequest, cx: &mut Context) { + let available = match request.kind() { + InputKind::Microphone => self.microphone_switch_available(), + InputKind::Camera => self.camera_switch_available(), + }; + if !available { + return; + } + if let Some(request) = self.input_switch.submit(request) { + self.run_input_switch(request, cx); + } + cx.notify(); + } + + fn finish_input_switch(&mut self, generation: u64, cx: &mut Context) { + if generation != self.recording_generation { + return; + } + if let Some(frozen_at) = self.input_switch.timer_frozen_at.take() + && self.paused_since == Some(frozen_at) + && self.phase == (Phase::Recording { paused: false }) + { + self.paused_since = None; + self.paused_accum += frozen_at.elapsed(); + } + while let Some(next) = self.input_switch.finish() { + let available = match next.kind() { + InputKind::Microphone => self.microphone_switch_available(), + InputKind::Camera => self.camera_switch_available(), + }; + if available { + self.run_input_switch(next, cx); + break; + } + } + cx.notify(); + } + + fn run_input_switch(&mut self, request: InputRequest, cx: &mut Context) { + let generation = self.recording_generation; + match self.mode() { + Some(RecordingMode::Instant) => self.run_instant_switch(request, generation, cx), + Some(RecordingMode::Studio) => self.run_studio_switch(request, generation, cx), + None => self.finish_input_switch(generation, cx), + } + } + + fn run_instant_switch(&mut self, request: InputRequest, generation: u64, cx: &mut Context) { + match request { + InputRequest::Camera(selection) => { + Feeds::global(cx).update(cx, |feeds, cx| { + let selected = selection.is_some(); + feeds.set_camera(selection, cx); + if selected { + feeds.resume_camera_preview(cx); + } + }); + self.input_switch.error = None; + self.finish_input_switch(generation, cx); + } + InputRequest::Microphone(None) => { + self.set_instant_microphone_muted(true); + self.input_switch.error = None; + self.finish_input_switch(generation, cx); + } + InputRequest::Microphone(Some(label)) => { + let current = Feeds::global(cx).read(cx).microphone.clone(); + if current.as_deref() == Some(label.as_str()) { + self.set_instant_microphone_muted(false); + self.input_switch.error = None; + self.finish_input_switch(generation, cx); + return; + } + let Some(switch) = self + .active + .as_ref() + .and_then(|active| active.microphone_switch()) + else { + self.finish_input_switch(generation, cx); + return; + }; + let settings = crate::store::RecordingDeviceSettings::for_microphone(&label); + let meter = Feeds::global(cx).read(cx).mic_meter(); + let opening = label.clone(); + let task = gpui_tokio::Tokio::spawn_result(cx, async move { + let opened = recording::open_microphone(&opening, settings, meter).await?; + switch.switch(opened.lock.clone()).await?; + Ok(opened) + }); + cx.spawn(async move |this, cx| { + let result = task.await; + this.update(cx, |this, cx| { + if generation != this.recording_generation { + return; + } + match result { + Ok(opened) => { + if let Some(active) = &this.active { + active.adopt_switched_microphone(opened); + } + Feeds::global(cx).update(cx, |feeds, cx| { + feeds.adopt_recording_microphone(label.clone(), cx); + }); + this.set_instant_microphone_muted(false); + this.input_switch.error = None; + tracing::info!(microphone = %label, "instant recording switched microphone"); + } + Err(error) => { + tracing::warn!(microphone = %label, "instant microphone switch failed: {error:#}"); + this.input_switch.error = + Some(tr::controls::settings::switch_failed(&label)); + } + } + this.finish_input_switch(generation, cx); + }) + .ok(); + }) + .detach(); + } + } + } + + /// Instant's "No microphone" keeps the track and its timeline and sends + /// silence, the same seam the bar's mute uses. + fn set_instant_microphone_muted(&mut self, muted: bool) { + let Some(mute) = self + .active + .as_ref() + .and_then(|active| active.mic_mute.clone()) + else { + return; + }; + self.mic_muted = muted; + mute.store(muted, std::sync::atomic::Ordering::Relaxed); + } + + fn run_studio_switch(&mut self, request: InputRequest, generation: u64, cx: &mut Context) { + let Some(handle) = self.active.as_ref().and_then(|active| active.studio_handle()) else { + self.finish_input_switch(generation, cx); + return; + }; + let previous = match &request { + InputRequest::Microphone(_) => { + InputRequest::Microphone(Feeds::global(cx).read(cx).microphone.clone()) + } + InputRequest::Camera(_) => { + InputRequest::Camera(Feeds::global(cx).read(cx).camera.clone()) + } + }; + if previous == request { + self.input_switch.error = None; + self.finish_input_switch(generation, cx); + return; + } + let was_paused = self.is_paused(); + if !was_paused && self.paused_since.is_none() { + let now = Instant::now(); + self.paused_since = Some(now); + self.input_switch.timer_frozen_at = Some(now); + } + cx.spawn(async move |this, cx| { + let outcome = + studio_switch(&this, cx, handle, request, previous, was_paused, generation).await; + this.update(cx, |this, cx| { + if generation != this.recording_generation { + return; + } + if let Some(paused) = outcome.paused + && paused + && !was_paused + && this.phase == (Phase::Recording { paused: false }) + { + this.phase = Phase::Recording { paused: true }; + this.input_switch.timer_frozen_at = None; + } + this.input_switch.error = outcome.error; + this.finish_input_switch(generation, cx); + }) + .ok(); + }) + .detach(); + } + + fn still_switching(&self, generation: u64) -> bool { + generation == self.recording_generation && matches!(self.phase, Phase::Recording { .. }) + } +} + +async fn studio_switch( + this: &WeakEntity, + cx: &mut AsyncApp, + handle: cap_recording::studio_recording::ActorHandle, + request: InputRequest, + previous: InputRequest, + was_paused: bool, + generation: u64, +) -> StudioSwitchOutcome { + let current = |cx: &mut AsyncApp| { + this.read_with(cx, |session, _| session.still_switching(generation)) + .unwrap_or(false) + }; + + if !was_paused { + let pause = handle.clone(); + let paused = gpui_tokio::Tokio::spawn_result(cx, async move { pause.pause().await }).await; + if let Err(error) = paused { + tracing::warn!("pausing for an input switch failed: {error:#}"); + return StudioSwitchOutcome { + error: Some(tr::controls::settings::switch_failed(&request.device_label())), + paused: Some(false), + }; + } + } + + let kind = request.kind(); + let detach = handle.clone(); + let detached = gpui_tokio::Tokio::spawn_result(cx, async move { + match kind { + InputKind::Microphone => detach.set_mic_feed(None).await, + InputKind::Camera => detach.set_camera_feed(None).await, + } + }) + .await; + + let mut error = None; + if let Err(detach_error) = detached { + tracing::warn!("detaching the old input failed: {detach_error:#}"); + error = Some(tr::controls::settings::switch_failed(&request.device_label())); + } else if current(cx) { + match attach_selection(this, cx, &handle, &request).await { + Ok(()) => {} + Err(attach_error) => { + tracing::warn!( + device = %request.device_label(), + "studio input switch failed, restoring the previous input: {attach_error:#}" + ); + error = Some(tr::controls::settings::switch_failed(&request.device_label())); + if current(cx) + && let Err(restore_error) = attach_selection(this, cx, &handle, &previous).await + { + tracing::error!("restoring the previous input failed: {restore_error:#}"); + } + } + } + } + + if was_paused || !current(cx) { + return StudioSwitchOutcome { + error, + paused: Some(was_paused), + }; + } + let resume = handle.clone(); + match gpui_tokio::Tokio::spawn_result(cx, async move { resume.resume().await }).await { + Ok(()) => StudioSwitchOutcome { + error, + paused: Some(false), + }, + Err(resume_error) => { + tracing::error!("resuming after an input switch failed: {resume_error:#}"); + let query = handle.clone(); + let paused = gpui_tokio::Tokio::spawn_result(cx, async move { query.is_paused().await }) + .await + .ok(); + StudioSwitchOutcome { + error: Some(format!( + "{resume_error:#}. Use Stop if recording cannot continue safely." + )), + paused, + } + } + } +} + +/// Points the app's feed at `request` and hands the paused Studio actor its +/// lock. `None` leaves the input off for the next segments. +async fn attach_selection( + this: &WeakEntity, + cx: &mut AsyncApp, + handle: &cap_recording::studio_recording::ActorHandle, + request: &InputRequest, +) -> anyhow::Result<()> { + let selection = request.clone(); + let (ready, mic, camera): ( + Option, + Option>, + Option>, + ) = this.update(cx, |_, cx| { + Feeds::global(cx).update(cx, |feeds, cx| match selection { + InputRequest::Microphone(label) => { + feeds.set_microphone(label, cx); + (feeds.input_readiness().microphone, feeds.mic_actor(), None) + } + InputRequest::Camera(camera) => { + let selected = camera.is_some(); + feeds.set_camera(camera, cx); + if selected { + feeds.resume_camera_preview(cx); + } + (feeds.input_readiness().camera, None, feeds.camera_actor()) + } + }) + })?; + + match request { + InputRequest::Microphone(None) | InputRequest::Camera(None) => return Ok(()), + InputRequest::Microphone(Some(label)) => { + let ready = ready.ok_or_else(|| anyhow::anyhow!("Microphone is not opening"))?; + let actor = mic.ok_or_else(|| anyhow::anyhow!("Microphone feed is not running"))?; + let label = label.clone(); + let handle = handle.clone(); + gpui_tokio::Tokio::spawn_result(cx, async move { + ready.await.map_err(anyhow::Error::msg)?; + let lock = actor + .ask(microphone::Lock) + .await + .map_err(|error| anyhow::anyhow!("microphone lock: {error}"))?; + anyhow::ensure!( + lock.device_name() == label, + "The microphone feed opened '{}' instead of '{label}'", + lock.device_name() + ); + handle.set_mic_feed(Some(Arc::new(lock))).await + }) + .await?; + } + InputRequest::Camera(Some(selected)) => { + let ready = ready.ok_or_else(|| anyhow::anyhow!("Camera is not opening"))?; + let actor = camera.ok_or_else(|| anyhow::anyhow!("Camera feed is not running"))?; + let selected = selected.clone(); + let attach = handle.clone(); + gpui_tokio::Tokio::spawn_result(cx, async move { + ready.await.map_err(anyhow::Error::msg)?; + let lock = actor + .ask(camera::Lock) + .await + .map_err(|error| anyhow::anyhow!("camera lock: {error}"))?; + let info = lock.camera_info(); + anyhow::ensure!( + info.device_id() == selected.device_id, + "The camera feed opened a different camera" + ); + attach.set_camera_feed(Some(Arc::new(lock))).await + }) + .await?; + #[cfg(target_os = "macos")] + { + let bubble = cx.update(crate::app_windows::camera_window_id)?; + if let Some(bubble) = bubble { + let exclude = handle.clone(); + gpui_tokio::Tokio::spawn_result(cx, async move { + exclude.add_excluded_windows(vec![bubble]).await + }) + .await?; + } + } + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn mic(label: &str) -> InputRequest { + InputRequest::Microphone(Some(label.to_owned())) + } + + #[test] + fn rapid_switches_collapse_to_the_newest_choice() { + let mut state = InputSwitchState::default(); + assert_eq!(state.submit(mic("a")), Some(mic("a"))); + assert_eq!(state.pending, Some(InputKind::Microphone)); + assert_eq!(state.submit(mic("b")), None); + assert_eq!(state.submit(mic("c")), None); + assert_eq!(state.submit(InputRequest::Microphone(None)), None); + assert_eq!(state.finish(), Some(InputRequest::Microphone(None))); + assert_eq!(state.pending, Some(InputKind::Microphone)); + assert_eq!(state.finish(), None); + assert_eq!(state.pending, None); + } + + #[test] + fn a_camera_request_waits_for_a_running_microphone_switch_and_is_never_dropped() { + let mut state = InputSwitchState::default(); + assert!(state.submit(mic("a")).is_some()); + assert_eq!(state.submit(InputRequest::Camera(None)), None); + assert_eq!(state.submit(mic("b")), None); + assert_eq!(state.finish(), Some(InputRequest::Camera(None))); + assert_eq!(state.pending, Some(InputKind::Camera)); + assert_eq!(state.finish(), Some(mic("b"))); + assert_eq!(state.finish(), None); + } +} diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rs index a3e77414bfa..a198a15f8d5 100644 --- a/crates/recording/src/feeds/microphone.rs +++ b/crates/recording/src/feeds/microphone.rs @@ -3158,6 +3158,13 @@ pub mod synthetic { .map_err(|error| anyhow::anyhow!("synthetic microphone lock: {error}")) } + /// Stops the feed actor, the way a feed whose device process died + /// stops answering. + pub async fn kill(&self) { + self.feed.kill(); + self.feed.wait_for_shutdown().await; + } + /// Takes the device away the way the app does when it disappears or /// is deselected (`RemoveInput`); a locked feed defers it. pub async fn remove_input(&self) { diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs index 3a830d7f267..8ce739f352e 100644 --- a/crates/recording/src/instant_recording.rs +++ b/crates/recording/src/instant_recording.rs @@ -325,9 +325,17 @@ pub struct ActorHandle { >, >, >, + microphone_switch: Option, } impl ActorHandle { + /// Moves the recording's microphone onto another open feed without + /// interrupting the audio track. `None` when the recording has no + /// microphone or its pipeline cannot swap one. + pub fn microphone_switch(&self) -> Option { + self.microphone_switch.clone() + } + #[cfg(target_os = "linux")] pub fn lifecycle(&self) -> InstantLifecycle { self.lifecycle.clone() @@ -751,6 +759,7 @@ async fn create_pipeline( content_dir: PathBuf, screen: ScreenPipelineInput, mic_feed: Option>, + mic_switch_requests: Option>, system_audio_source: Option, max_output_size: Option, start_time: Timestamps, @@ -857,8 +866,19 @@ async fn create_pipeline( .with_audio_source::(sys_audio); } - if let Some(mic) = mic_feed { - builder = builder.with_audio_source::(mic); + match (mic_feed, mic_switch_requests) { + (Some(lock), Some(switch_requests)) => { + builder = builder.with_audio_source::( + crate::sources::SwitchableMicrophoneConfig { + lock, + switch_requests, + }, + ); + } + (Some(mic), None) => { + builder = builder.with_audio_source::(mic); + } + (None, _) => {} } let segment_tx_for_audio = segment_channel.as_ref().map(|(tx, _)| tx.clone()); @@ -1277,6 +1297,15 @@ async fn build_instant_recording_actor( #[cfg(windows)] cap_mediafoundation_utils::thread_init(); + let (microphone_switch, mut mic_switch_requests) = if inputs.mic_feed.is_some() + && !matches!(inputs.capture_target, ScreenCaptureTarget::CameraOnly) + { + let (switch, requests) = crate::sources::MicrophoneSwitch::channel(); + (Some(switch), Some(requests)) + } else { + (None, None) + }; + let (mut pipeline, video_info) = match inputs.capture_target { ScreenCaptureTarget::CameraOnly => { #[cfg(target_os = "linux")] @@ -1462,6 +1491,7 @@ async fn build_instant_recording_actor( prepared_camera, }, inputs.mic_feed.clone(), + mic_switch_requests.take(), system_audio_source, max_output_size, timestamps, @@ -1521,6 +1551,7 @@ async fn build_instant_recording_actor( done_fut: done_fut.clone(), health_rx, segment_rx: segment_rx.map(|rx| std::sync::Mutex::new(Some(rx))), + microphone_switch, }; #[cfg(not(target_os = "linux"))] @@ -2051,6 +2082,7 @@ mod quiescence_tests { done_fut: lifecycle.done_fut(), health_rx: None, segment_rx: None, + microphone_switch: None, lifecycle, }, audio_cancel, @@ -2269,6 +2301,7 @@ mod quiescence_tests { done_fut: lifecycle.done_fut(), health_rx: None, segment_rx: None, + microphone_switch: None, lifecycle, }; if start_video { diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index b685ef123cf..5984aa444c5 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -191,15 +191,36 @@ fn count_nonzero_samples(frame: &ffmpeg::frame::Audio) -> Option<(u64, u64)> { pub struct Microphone { #[cfg(windows)] capture_scope: crate::output_pipeline::PipelineBuildScope, - #[cfg(windows)] - subscription: Arc, #[cfg(target_os = "linux")] required_health: Option, info: AudioInfo, - _lock: Arc, + binding: SharedMicrophoneBinding, cancel: CancellationToken, } +/// The feed a running microphone source reads from. A [`MicrophoneSwitch`] +/// replaces it mid-recording, so teardown always releases the current one. +struct MicrophoneBinding { + lock: Arc, + #[cfg(windows)] + subscription: Arc, +} + +type SharedMicrophoneBinding = Arc>; + +impl Microphone { + #[cfg(windows)] + fn current_subscription( + &self, + ) -> ( + Arc, + Arc, + ) { + let binding = self.binding.lock().unwrap(); + (binding.lock.clone(), binding.subscription.clone()) + } +} + // The detached bridge tasks hold clones of the feed lock and only exit via // this token. If the pipeline is torn down without stop() (wedged muxer, // error paths), a live-but-orphaned lock would keep the feed in State::Locked @@ -208,13 +229,56 @@ pub struct Microphone { impl Drop for Microphone { fn drop(&mut self) { #[cfg(windows)] - if let Some(error) = self.subscription.retire() { + if let Some(error) = self.current_subscription().1.retire() { self.capture_scope.fail_required(error); } self.cancel.cancel(); } } +pub struct MicrophoneSwitchRequest { + lock: Arc, + reply: tokio::sync::oneshot::Sender>, +} + +/// Moves a running microphone source onto another, already open feed. The +/// recording keeps its audio track and timeline; only where the samples come +/// from changes. Opening the new device is the caller's job, so a device that +/// fails to open never touches the recording. +#[derive(Clone)] +pub struct MicrophoneSwitch(flume::Sender); + +impl MicrophoneSwitch { + pub fn channel() -> (Self, flume::Receiver) { + let (tx, rx) = flume::bounded(1); + (Self(tx), rx) + } + + pub async fn switch(&self, lock: Arc) -> anyhow::Result<()> { + let (reply, response) = tokio::sync::oneshot::channel(); + self.0 + .send_async(MicrophoneSwitchRequest { lock, reply }) + .await + .map_err(|_| anyhow::anyhow!("The recording's microphone is no longer running"))?; + response + .await + .map_err(|_| anyhow::anyhow!("The recording's microphone stopped while switching"))? + } +} + +async fn next_switch_request( + requests: &mut Option>, +) -> Option { + let Some(receiver) = requests.as_ref() else { + return std::future::pending().await; + }; + let request = receiver.recv_async().await.ok(); + if request.is_none() { + *requests = None; + } + request +} + #[derive(Debug, Error)] pub enum MicrophoneSourceError { #[error("microphone actor not running")] @@ -369,6 +433,7 @@ impl Microphone { mut audio_tx: mpsc::Sender, ctx: &mut crate::SetupCtx, target_channels: usize, + switch_requests: Option>, ) -> impl Future> + 'static { let health_tx = ctx.health_tx().clone(); let digital_silence_ms = ctx.digital_silence_ms(); @@ -378,15 +443,24 @@ impl Microphone { let source_cancel = ctx.stop_token().child_token(); #[cfg(windows)] let subscription = feed_lock.recording_subscription(source_cancel.clone()); + let binding: SharedMicrophoneBinding = + Arc::new(std::sync::Mutex::new(MicrophoneBinding { + lock: feed_lock.clone(), + #[cfg(windows)] + subscription: subscription.clone(), + })); #[cfg(windows)] { capture_scope.register_token(source_cancel.clone()); let scope = capture_scope.clone(); let cancel = source_cancel.clone(); - let subscription = subscription.clone(); - let feed = feed_lock.clone(); + let binding = binding.clone(); ctx.tasks().spawn("microphone-owned-producers", async move { cancel.cancelled().await; + let (feed, subscription) = { + let binding = binding.lock().unwrap(); + (binding.lock.clone(), binding.subscription.clone()) + }; finish_recording_subscription(&scope, &feed, subscription).await }); } @@ -477,9 +551,9 @@ impl Microphone { let frame_counter = mic_frame_counter.clone(); let drop_counter = mic_drop_counter.clone(); let silence_counter = mic_silence_counter.clone(); - let feed_lock = feed_lock.clone(); + let mut feed_lock = feed_lock.clone(); #[cfg(not(windows))] - let device_name = device_name.clone(); + let mut device_name = device_name.clone(); let health_tx = health_tx.clone(); let cancel = cancel.clone(); let recording_muted = recording_muted.clone(); @@ -487,9 +561,13 @@ impl Microphone { #[cfg(windows)] let capture_scope = capture_scope.clone(); #[cfg(windows)] - let subscription = subscription.clone(); + let mut subscription = subscription.clone(); #[cfg(target_os = "linux")] let required_health = required_health.clone(); + let binding = binding.clone(); + let mut switch_requests = switch_requests; + let mut rx = rx; + let mut reconnect_settings = reconnect_settings; async move { let mut digital_silence = DigitalSilenceDetector::new( audio_info.sample_rate * audio_info.channels as u32, @@ -520,6 +598,91 @@ impl Microphone { let recv_result = tokio::select! { biased; _ = cancel.cancelled() => break, + request = next_switch_request(&mut switch_requests) => { + let Some(MicrophoneSwitchRequest { lock: next, reply }) = request else { + continue; + }; + let (next_tx, next_rx) = flume::bounded(128); + #[cfg(target_os = "linux")] + if required_health.is_some() { + let _ = reply.send(Err(anyhow::anyhow!( + "This recording's microphone cannot be switched" + ))); + continue; + } + #[cfg(windows)] + let next_subscription = next.recording_subscription(cancel.clone()); + #[cfg(windows)] + let attached = next + .attach_recording_subscription( + next_subscription.clone(), + next_tx, + health_tx.clone(), + "microphone-feed:recording".into(), + ) + .await; + #[cfg(not(windows))] + let attached = next + .ask(microphone::AddRecordingSender { + sender: next_tx, + health_tx: health_tx.clone(), + label: "microphone-feed:recording".to_string(), + }) + .await + .map_err(|error| anyhow::anyhow!("{error}")); + if let Err(error) = attached { + warn!(%error, "Microphone switch could not attach to the new feed"); + let _ = reply.send(Err(error)); + continue; + } + let previous = std::mem::replace( + &mut *binding.lock().unwrap(), + MicrophoneBinding { + lock: next.clone(), + #[cfg(windows)] + subscription: next_subscription.clone(), + }, + ); + #[cfg(windows)] + { + let _ = previous.subscription.retire(); + if let Err(error) = previous + .lock + .detach_recording_subscription(previous.subscription) + .await + { + warn!(%error, "Detaching the previous microphone after a switch"); + } + subscription = next_subscription; + } + let previous_name = previous.lock.device_name().to_string(); + drop(previous); + rx = next_rx; + feed_lock = next; + let next_info = feed_lock.audio_info(); + reconnect_settings = microphone::MicrophoneDeviceSettings { + sample_rate: Some(next_info.sample_rate), + channels: u16::try_from(next_info.channels).ok(), + }; + #[cfg(not(windows))] + { + device_name = feed_lock.device_name().to_string(); + } + reconnect_attempts = 0; + next_reconnect_after = MIC_RECONNECT_AFTER; + if silence_mode { + silence_start = Some(Instant::now()); + } + info!( + from = %previous_name, + to = %feed_lock.device_name(), + source_rate = next_info.sample_rate, + source_channels = next_info.channels, + "Microphone source switched" + ); + let _ = reply.send(Ok(())); + continue; + } r = tokio::time::timeout(silence_timeout, rx.recv_async()) => r, }; match recv_result { @@ -939,10 +1102,8 @@ impl Microphone { Ok(Self { #[cfg(windows)] capture_scope, - #[cfg(windows)] - subscription, info: audio_info, - _lock: feed_lock, + binding, #[cfg(target_os = "linux")] required_health, cancel, @@ -972,6 +1133,7 @@ impl AudioSource for Microphone { audio_tx, ctx, usize::from(MICROPHONE_TARGET_CHANNELS), + None, ) } @@ -985,16 +1147,14 @@ impl AudioSource for Microphone { health.stop(); } #[cfg(windows)] - if let Some(error) = self.subscription.retire() { + let (feed, subscription) = self.current_subscription(); + #[cfg(windows)] + if let Some(error) = subscription.retire() { self.capture_scope.fail_required(error); } self.cancel.cancel(); #[cfg(windows)] let scope = self.capture_scope.clone(); - #[cfg(windows)] - let feed = self._lock.clone(); - #[cfg(windows)] - let subscription = self.subscription.clone(); async move { #[cfg(windows)] { @@ -1008,6 +1168,42 @@ impl AudioSource for Microphone { } } +/// A [`Microphone`] that a [`MicrophoneSwitch`] can move onto another feed +/// while it records. +pub struct SwitchableMicrophone(Microphone); + +pub struct SwitchableMicrophoneConfig { + pub lock: Arc, + pub switch_requests: flume::Receiver, +} + +impl AudioSource for SwitchableMicrophone { + type Config = SwitchableMicrophoneConfig; + + fn setup( + config: Self::Config, + audio_tx: mpsc::Sender, + ctx: &mut crate::SetupCtx, + ) -> impl Future> + 'static { + let setup = Microphone::setup_with_channels( + config.lock, + audio_tx, + ctx, + usize::from(MICROPHONE_TARGET_CHANNELS), + Some(config.switch_requests), + ); + async move { setup.await.map(Self) } + } + + fn audio_info(&self) -> AudioInfo { + self.0.audio_info() + } + + fn stop(&mut self) -> impl Future> + Send { + self.0.stop() + } +} + #[cfg(windows)] async fn finish_recording_subscription( scope: &crate::output_pipeline::PipelineBuildScope, @@ -2007,3 +2203,194 @@ mod digital_silence_tests { assert!(digital_silence_ms.is_some_and(|ms| ms >= 5_000)); } } + +#[cfg(test)] +mod switch_tests { + use super::*; + use crate::{ + feeds::microphone::synthetic::SyntheticMicrophone, + output_pipeline::{AudioMuxer, Muxer, OutputPipeline, TaskPool}, + }; + use cap_timestamp::Timestamp; + + type Captured = Arc>>; + + struct CapturingMuxer(Captured); + + impl Muxer for CapturingMuxer { + type Config = Captured; + + async fn setup( + captured: Captured, + _: std::path::PathBuf, + _: Option, + _: Option, + _: Arc, + _: &mut TaskPool, + ) -> anyhow::Result { + Ok(Self(captured)) + } + + fn finish(&mut self, _: Duration) -> anyhow::Result> { + Ok(Ok(())) + } + } + + impl AudioMuxer for CapturingMuxer { + fn send_audio_frame(&mut self, frame: AudioFrame, _: Duration) -> anyhow::Result<()> { + let data = frame.inner.data(0); + let samples = frame.inner.samples(); + let peak = data + .chunks_exact(4) + .take(samples) + .map(|bytes| f32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]).abs()) + .fold(0.0f32, f32::max); + self.0.lock().unwrap().push(peak); + Ok(()) + } + } + + async fn push_for( + mic: &SyntheticMicrophone, + value: f32, + format: (u32, u16), + start: Instant, + offset: Duration, + length: Duration, + ) { + let (rate, channels) = format; + let frames_per_chunk = (rate / 100) as usize; + let chunk = vec![value; frames_per_chunk * usize::from(channels)]; + let chunks = (length.as_secs_f64() * 100.0).round() as usize; + for index in 0..chunks { + let at = start + offset + Duration::from_millis(10 * index as u64); + mic.push(&chunk, Timestamp::Instant(at)).await; + tokio::time::sleep(Duration::from_millis(2)).await; + } + } + + async fn settle(captured: &Captured, predicate: impl Fn(&[f32]) -> bool) { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + if predicate(&captured.lock().unwrap()) { + return; + } + assert!( + Instant::now() < deadline, + "the muxer never saw the expected audio" + ); + tokio::time::sleep(Duration::from_millis(10)).await; + } + } + + #[tokio::test] + async fn a_running_source_moves_to_a_device_with_a_different_format_and_releases_the_old_one() + { + let built_in = SyntheticMicrophone::spawn("built-in", 48_000, 1); + let headset = SyntheticMicrophone::spawn("headset", 44_100, 2); + let (switch, switch_requests) = MicrophoneSwitch::channel(); + let captured = Captured::default(); + let temp = tempfile::tempdir().unwrap(); + let mut pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) + .with_audio_source::(SwitchableMicrophoneConfig { + lock: Arc::new(built_in.lock().await.unwrap()), + switch_requests, + }) + .build::(captured.clone()) + .await + .unwrap(); + + let start = Instant::now(); + push_for( + &built_in, + 0.1, + (48_000, 1), + start, + Duration::ZERO, + Duration::from_millis(300), + ) + .await; + settle(&captured, |peaks| { + peaks.iter().any(|peak| (*peak - 0.1).abs() < 0.02) + }) + .await; + + switch + .switch(Arc::new(headset.lock().await.unwrap())) + .await + .unwrap(); + assert!( + built_in.lock().await.is_ok(), + "the recording must let go of the microphone it switched away from" + ); + + push_for( + &headset, + 0.6, + (44_100, 2), + start, + Duration::from_millis(300), + Duration::from_millis(300), + ) + .await; + settle(&captured, |peaks| peaks.iter().any(|peak| *peak > 0.3)).await; + + built_in + .push(&[0.9; 480], Timestamp::Instant(Instant::now())) + .await; + tokio::time::sleep(Duration::from_millis(100)).await; + pipeline.stop().await.unwrap(); + + let peaks = captured.lock().unwrap().clone(); + let first_switched = peaks.iter().position(|peak| *peak > 0.3).unwrap(); + assert!( + peaks[..first_switched].iter().all(|peak| *peak < 0.2), + "audio before the switch comes from the first microphone" + ); + assert!( + peaks.iter().all(|peak| *peak < 0.8), + "the old microphone's samples never reach the recording after the switch" + ); + } + + #[tokio::test] + async fn a_switch_that_cannot_attach_leaves_the_current_microphone_recording() { + let built_in = SyntheticMicrophone::spawn("built-in", 48_000, 1); + let unplugged = SyntheticMicrophone::spawn("unplugged", 48_000, 1); + let unplugged_lock = Arc::new(unplugged.lock().await.unwrap()); + unplugged.kill().await; + let (switch, switch_requests) = MicrophoneSwitch::channel(); + let captured = Captured::default(); + let temp = tempfile::tempdir().unwrap(); + let mut pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) + .with_audio_source::(SwitchableMicrophoneConfig { + lock: Arc::new(built_in.lock().await.unwrap()), + switch_requests, + }) + .build::(captured.clone()) + .await + .unwrap(); + + assert!(switch.switch(unplugged_lock).await.is_err()); + + let start = Instant::now(); + push_for( + &built_in, + 0.4, + (48_000, 1), + start, + Duration::ZERO, + Duration::from_millis(200), + ) + .await; + settle(&captured, |peaks| { + peaks.iter().any(|peak| (*peak - 0.4).abs() < 0.02) + }) + .await; + assert!( + built_in.lock().await.is_err(), + "the recording still holds the microphone it never left" + ); + pipeline.stop().await.unwrap(); + } +} diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs index 2571e7c870d..ce59f67b8f4 100644 --- a/crates/recording/src/sources/screen_capture/linux.rs +++ b/crates/recording/src/sources/screen_capture/linux.rs @@ -1820,6 +1820,7 @@ impl AudioSource for SystemAudioSource { tx, ctx, X11Capture::audio_info().channels, + None, ); async move { let inner = setup diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index be6d6cfc74a..daa8d8120ca 100644 --- a/crates/recording/src/studio_recording.rs +++ b/crates/recording/src/studio_recording.rs @@ -1256,6 +1256,31 @@ impl Message for Actor { } } +#[cfg(target_os = "macos")] +struct AddExcludedWindows(Vec); + +#[cfg(target_os = "macos")] +impl Message for Actor { + type Reply = anyhow::Result<()>; + + async fn handle( + &mut self, + msg: AddExcludedWindows, + _: &mut Context, + ) -> Self::Reply { + match self.state.as_ref() { + Some(ActorState::Recording { .. }) => { + bail!("Pause the recording before changing excluded windows") + } + Some(ActorState::Paused { .. }) => { + self.segment_factory.add_excluded_windows(msg.0); + Ok(()) + } + None => Err(anyhow!("Recording no longer active")), + } + } +} + pub struct IsPaused; impl Message for Actor { @@ -2175,6 +2200,17 @@ impl ActorHandle { Ok(self.actor_ref.ask(SetCameraFeed { camera_feed }).await?) } + #[cfg(target_os = "macos")] + /// Keeps windows opened mid-recording (a reopened camera bubble) out of + /// the segments recorded after the next resume. + pub async fn add_excluded_windows( + &self, + windows: Vec, + ) -> anyhow::Result<()> { + self.actor_ref.ask(AddExcludedWindows(windows)).await?; + Ok(()) + } + pub async fn is_paused(&self) -> anyhow::Result { Ok(self.actor_ref.ask(IsPaused).await?) } @@ -3296,6 +3332,15 @@ impl SegmentPipelineFactory { self.base_inputs.camera_feed = camera_feed; } + #[cfg(target_os = "macos")] + pub fn add_excluded_windows(&mut self, windows: Vec) { + for window in windows { + if !self.base_inputs.excluded_windows.contains(&window) { + self.base_inputs.excluded_windows.push(window); + } + } + } + pub fn camera_device_id(&self) -> Option { self.base_inputs .camera_feed From f5289cbab5ac7bc20ef161e15134a7ff7d604c6b Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:32:27 -0500 Subject: [PATCH 02/19] test(recording): keep the switched mic test clear of the stereo downmix --- crates/recording/src/sources/microphone.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index 5984aa444c5..44a516253dc 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -194,7 +194,9 @@ pub struct Microphone { #[cfg(target_os = "linux")] required_health: Option, info: AudioInfo, - binding: SharedMicrophoneBinding, + // Holds the current feed lock for the source's lifetime; Windows also + // reads it to retire the current subscription. + _binding: SharedMicrophoneBinding, cancel: CancellationToken, } @@ -216,7 +218,7 @@ impl Microphone { Arc, Arc, ) { - let binding = self.binding.lock().unwrap(); + let binding = self._binding.lock().unwrap(); (binding.lock.clone(), binding.subscription.clone()) } } @@ -1103,7 +1105,7 @@ impl Microphone { #[cfg(windows)] capture_scope, info: audio_info, - binding, + _binding: binding, #[cfg(target_os = "linux")] required_health, cancel, @@ -2291,7 +2293,7 @@ mod switch_tests { let (switch, switch_requests) = MicrophoneSwitch::channel(); let captured = Captured::default(); let temp = tempfile::tempdir().unwrap(); - let mut pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) + let pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) .with_audio_source::(SwitchableMicrophoneConfig { lock: Arc::new(built_in.lock().await.unwrap()), switch_requests, @@ -2326,7 +2328,7 @@ mod switch_tests { push_for( &headset, - 0.6, + 0.35, (44_100, 2), start, Duration::from_millis(300), @@ -2336,7 +2338,7 @@ mod switch_tests { settle(&captured, |peaks| peaks.iter().any(|peak| *peak > 0.3)).await; built_in - .push(&[0.9; 480], Timestamp::Instant(Instant::now())) + .push(&[0.95; 480], Timestamp::Instant(Instant::now())) .await; tokio::time::sleep(Duration::from_millis(100)).await; pipeline.stop().await.unwrap(); @@ -2362,7 +2364,7 @@ mod switch_tests { let (switch, switch_requests) = MicrophoneSwitch::channel(); let captured = Captured::default(); let temp = tempfile::tempdir().unwrap(); - let mut pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) + let pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) .with_audio_source::(SwitchableMicrophoneConfig { lock: Arc::new(built_in.lock().await.unwrap()), switch_requests, From b83d9898404640c30f746d40308e3b8899a11b05 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:35:03 -0500 Subject: [PATCH 03/19] style: rustfmt --- apps/desktop-gpui/src/controls_window.rs | 17 ++++---- apps/desktop-gpui/src/feeds.rs | 4 +- apps/desktop-gpui/src/session/input_switch.rs | 39 ++++++++++++++----- crates/recording/src/sources/microphone.rs | 14 +++---- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index e5fd8bbcd55..368b539434a 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -110,8 +110,7 @@ impl ControlsWindow { crate::theme::bind_window(window, cx); let theme = Theme::for_window(window, cx, false); cx.observe_in(&session, window, |this, _, window, cx| { - if this.settings_open - && !matches!(this.session.read(cx).phase, Phase::Recording { .. }) + if this.settings_open && !matches!(this.session.read(cx).phase, Phase::Recording { .. }) { this.settings_open = false; } @@ -714,9 +713,7 @@ impl ControlsWindow { let available = matches!(self.session.read(cx).phase, Phase::Recording { .. }); let open = self.settings_open; self.action_button("settings", "icons/settings.svg", !available) - .when(open, |this| { - this.bg(Theme::with_alpha(theme.gray_12, 0.08)) - }) + .when(open, |this| this.bg(Theme::with_alpha(theme.gray_12, 0.08))) .when(!open, |this| { this.tooltip(move |_, cx| { ui::Tooltip::new(&theme, tr::controls::settings::open()).view(cx) @@ -963,10 +960,12 @@ impl ControlsWindow { } } - panel = panel.child(self.settings_divider()).child(self.settings_heading( - tr::main::device::camera(), - note(camera_available, started_with_camera, InputKind::Camera), - )); + panel = panel + .child(self.settings_divider()) + .child(self.settings_heading( + tr::main::device::camera(), + note(camera_available, started_with_camera, InputKind::Camera), + )); if camera_available { let enabled = pending != Some(InputKind::Camera); diff --git a/apps/desktop-gpui/src/feeds.rs b/apps/desktop-gpui/src/feeds.rs index c1aff41530d..fe505d3e03e 100644 --- a/apps/desktop-gpui/src/feeds.rs +++ b/apps/desktop-gpui/src/feeds.rs @@ -1355,7 +1355,9 @@ impl Feeds { }) .await { - tracing::warn!("releasing the microphone a recording switched away from: {error}"); + tracing::warn!( + "releasing the microphone a recording switched away from: {error}" + ); } }) .detach(); diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index 8ff10e23705..49e4db6e748 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -187,7 +187,12 @@ impl RecordingSession { } } - fn run_instant_switch(&mut self, request: InputRequest, generation: u64, cx: &mut Context) { + fn run_instant_switch( + &mut self, + request: InputRequest, + generation: u64, + cx: &mut Context, + ) { match request { InputRequest::Camera(selection) => { Feeds::global(cx).update(cx, |feeds, cx| { @@ -276,8 +281,17 @@ impl RecordingSession { mute.store(muted, std::sync::atomic::Ordering::Relaxed); } - fn run_studio_switch(&mut self, request: InputRequest, generation: u64, cx: &mut Context) { - let Some(handle) = self.active.as_ref().and_then(|active| active.studio_handle()) else { + fn run_studio_switch( + &mut self, + request: InputRequest, + generation: u64, + cx: &mut Context, + ) { + let Some(handle) = self + .active + .as_ref() + .and_then(|active| active.studio_handle()) + else { self.finish_input_switch(generation, cx); return; }; @@ -348,7 +362,9 @@ async fn studio_switch( if let Err(error) = paused { tracing::warn!("pausing for an input switch failed: {error:#}"); return StudioSwitchOutcome { - error: Some(tr::controls::settings::switch_failed(&request.device_label())), + error: Some(tr::controls::settings::switch_failed( + &request.device_label(), + )), paused: Some(false), }; } @@ -367,7 +383,9 @@ async fn studio_switch( let mut error = None; if let Err(detach_error) = detached { tracing::warn!("detaching the old input failed: {detach_error:#}"); - error = Some(tr::controls::settings::switch_failed(&request.device_label())); + error = Some(tr::controls::settings::switch_failed( + &request.device_label(), + )); } else if current(cx) { match attach_selection(this, cx, &handle, &request).await { Ok(()) => {} @@ -376,7 +394,9 @@ async fn studio_switch( device = %request.device_label(), "studio input switch failed, restoring the previous input: {attach_error:#}" ); - error = Some(tr::controls::settings::switch_failed(&request.device_label())); + error = Some(tr::controls::settings::switch_failed( + &request.device_label(), + )); if current(cx) && let Err(restore_error) = attach_selection(this, cx, &handle, &previous).await { @@ -401,9 +421,10 @@ async fn studio_switch( Err(resume_error) => { tracing::error!("resuming after an input switch failed: {resume_error:#}"); let query = handle.clone(); - let paused = gpui_tokio::Tokio::spawn_result(cx, async move { query.is_paused().await }) - .await - .ok(); + let paused = + gpui_tokio::Tokio::spawn_result(cx, async move { query.is_paused().await }) + .await + .ok(); StudioSwitchOutcome { error: Some(format!( "{resume_error:#}. Use Stop if recording cannot continue safely." diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index 44a516253dc..08e7c9bde3c 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -445,12 +445,11 @@ impl Microphone { let source_cancel = ctx.stop_token().child_token(); #[cfg(windows)] let subscription = feed_lock.recording_subscription(source_cancel.clone()); - let binding: SharedMicrophoneBinding = - Arc::new(std::sync::Mutex::new(MicrophoneBinding { - lock: feed_lock.clone(), - #[cfg(windows)] - subscription: subscription.clone(), - })); + let binding: SharedMicrophoneBinding = Arc::new(std::sync::Mutex::new(MicrophoneBinding { + lock: feed_lock.clone(), + #[cfg(windows)] + subscription: subscription.clone(), + })); #[cfg(windows)] { capture_scope.register_token(source_cancel.clone()); @@ -2286,8 +2285,7 @@ mod switch_tests { } #[tokio::test] - async fn a_running_source_moves_to_a_device_with_a_different_format_and_releases_the_old_one() - { + async fn a_running_source_moves_to_a_device_with_a_different_format_and_releases_the_old_one() { let built_in = SyntheticMicrophone::spawn("built-in", 48_000, 1); let headset = SyntheticMicrophone::spawn("headset", 44_100, 2); let (switch, switch_requests) = MicrophoneSwitch::channel(); From 5111f534e9968e71d5a83780064265c72bc3987e Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:40:36 -0500 Subject: [PATCH 04/19] fix(desktop): build the input switch against the async app update --- apps/desktop-gpui/src/app_windows.rs | 1 + apps/desktop-gpui/src/session/input_switch.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index c220b75293b..009262bbeb1 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -3102,6 +3102,7 @@ fn own_windows(cx: &mut App) -> Vec { main_closed_by_user: _, editor_hidden_for_picker: _, camera_park: _, + camera_preview_hidden: _, #[cfg(target_os = "linux")] clean_capture: _, #[cfg(target_os = "linux")] diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index 49e4db6e748..7b176090b5c 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -508,7 +508,7 @@ async fn attach_selection( .await?; #[cfg(target_os = "macos")] { - let bubble = cx.update(crate::app_windows::camera_window_id)?; + let bubble = cx.update(crate::app_windows::camera_window_id); if let Some(bubble) = bubble { let exclude = handle.clone(); gpui_tokio::Tokio::spawn_result(cx, async move { From 99f628a1ca863bef4a591d451ab14c81376d867d Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:42:43 -0500 Subject: [PATCH 05/19] test(desktop): debug deeplink to switch the recording microphone --- apps/desktop-gpui/src/deeplink.rs | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/apps/desktop-gpui/src/deeplink.rs b/apps/desktop-gpui/src/deeplink.rs index dd338d1b2fd..c2d90c24761 100644 --- a/apps/desktop-gpui/src/deeplink.rs +++ b/apps/desktop-gpui/src/deeplink.rs @@ -130,6 +130,10 @@ pub enum DeepLinkAction { #[cfg(debug_assertions)] ResumeRecording, #[cfg(debug_assertions)] + SwitchMicrophone { + mic_label: Option, + }, + #[cfg(debug_assertions)] OpenCamera { camera: DeviceOrModelID, }, @@ -481,6 +485,13 @@ impl DeepLinkAction { Ok(()) } #[cfg(debug_assertions)] + DeepLinkAction::SwitchMicrophone { mic_label } => { + RecordingSession::global(cx).update(cx, |session, cx| { + session.switch_input(crate::session::InputRequest::Microphone(mic_label), cx); + }); + Ok(()) + } + #[cfg(debug_assertions)] DeepLinkAction::ResumeRecording => { RecordingSession::global(cx).update(cx, |session, cx| { if session.is_paused() { @@ -763,6 +774,27 @@ mod tests { ); } + #[cfg(debug_assertions)] + #[test] + fn parses_switch_microphone_action_urls() { + let parse = |value: serde_json::Value| { + let mut url = Url::parse("cap-desktop://action").unwrap(); + url.query_pairs_mut() + .append_pair("value", &value.to_string()); + DeepLinkAction::try_from(&url) + }; + assert_eq!( + parse(serde_json::json!({ "switch_microphone": { "mic_label": "USB Mic" } })), + Ok(DeepLinkAction::SwitchMicrophone { + mic_label: Some("USB Mic".into()) + }) + ); + assert_eq!( + parse(serde_json::json!({ "switch_microphone": { "mic_label": null } })), + Ok(DeepLinkAction::SwitchMicrophone { mic_label: None }) + ); + } + #[cfg(debug_assertions)] #[test] fn parses_area_recording_action_url() { From 8b81ccdb63f29e3c2eeb117ae6ba8a03776dbb1f Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:49:55 -0500 Subject: [PATCH 06/19] test(recording): av-sync-record can record a mic and switch it mid-recording --- crates/recording/examples/av-sync-record.rs | 113 ++++++++++++++++---- 1 file changed, 93 insertions(+), 20 deletions(-) diff --git a/crates/recording/examples/av-sync-record.rs b/crates/recording/examples/av-sync-record.rs index 797c5048e2c..c44fecd5ea5 100644 --- a/crates/recording/examples/av-sync-record.rs +++ b/crates/recording/examples/av-sync-record.rs @@ -1,23 +1,56 @@ -//! Continuous instant-mode screen + system-audio recording for the A/V sync -//! hardware test (`scripts/av-sync/`). Records the primary display for a fixed -//! duration with no pause, so `av-sync-check` can measure drift against a -//! flash+beep stimulus playing on screen. +//! Continuous instant-mode screen recording for the A/V sync hardware test +//! (`scripts/av-sync/`). Records the primary display for a fixed duration with +//! no pause, so `av-sync-check` can measure drift against a flash+beep +//! stimulus playing on screen. Audio is system audio by default, or a named +//! microphone, which `--switch-to` moves to another microphone mid-recording. //! -//! Usage: av-sync-record [--duration ] [--out ] +//! Usage: av-sync-record [--duration ] [--out ] [--mic ] +//! [--switch-to --switch-at ] -use cap_recording::{screen_capture::ScreenCaptureTarget, *}; +use cap_recording::{ + feeds::microphone::{self, MicrophoneFeed, MicrophoneFeedLock}, + screen_capture::ScreenCaptureTarget, + *, +}; +use kameo::Actor as _; use scap_targets::Display; -use std::time::Duration; +use std::{sync::Arc, time::Duration}; use tracing::*; +struct OpenMicrophone { + lock: Arc, + _errors: flume::Receiver, +} + +async fn open_microphone(name: &str) -> OpenMicrophone { + let (error_tx, errors) = flume::unbounded(); + let feed = MicrophoneFeed::spawn(MicrophoneFeed::new(error_tx)); + feed.ask(microphone::SetInput { + label: name.to_string(), + settings: None, + }) + .await + .expect("set microphone input") + .await + .expect("open microphone"); + let lock = feed.ask(microphone::Lock).await.expect("lock microphone"); + OpenMicrophone { + lock: Arc::new(lock), + _errors: errors, + } +} + #[tokio::main] pub async fn main() { tracing_subscriber::fmt() - .with_env_filter("warn,cap_recording=info") + .with_env_filter("warn,cap_recording=info,av_sync_record=info") .init(); let mut duration_secs = 45u64; let mut out = std::path::PathBuf::from("/tmp/avsync_rec"); + let mut mic = None::; + let mut switch_to = None::; + let mut switch_at = None::; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { @@ -28,6 +61,15 @@ pub async fn main() { .expect("--duration ") } "--out" => out = args.next().expect("--out ").into(), + "--mic" => mic = Some(args.next().expect("--mic ")), + "--switch-to" => switch_to = Some(args.next().expect("--switch-to ")), + "--switch-at" => { + switch_at = Some( + args.next() + .and_then(|v| v.parse().ok()) + .expect("--switch-at "), + ) + } other => panic!("unexpected argument: {other}"), } } @@ -36,27 +78,58 @@ pub async fn main() { std::fs::create_dir_all(&out).expect("create output dir"); info!( - "Recording {duration_secs}s of screen + system audio to '{}'", + "Recording {duration_secs}s of screen + {} to '{}'", + mic.as_deref().unwrap_or("system audio"), out.display() ); - let handle = instant_recording::Actor::builder( + let first = match &mic { + Some(name) => Some(open_microphone(name).await), + None => None, + }; + let mut builder = instant_recording::Actor::builder( out.clone(), ScreenCaptureTarget::Display { id: Display::primary().id(), }, ) - .with_system_audio(true) - .build( - #[cfg(target_os = "macos")] - Some(cap_recording::SendableShareableContent::from( - cidre::sc::ShareableContent::current().await.unwrap(), - )), - ) - .await - .unwrap(); + .with_system_audio(mic.is_none()); + if let Some(first) = &first { + builder = builder.with_mic_feed(first.lock.clone()); + } + let handle = builder + .build( + #[cfg(target_os = "macos")] + Some(cap_recording::SendableShareableContent::from( + cidre::sc::ShareableContent::current().await.unwrap(), + )), + ) + .await + .unwrap(); - tokio::time::sleep(Duration::from_secs(duration_secs)).await; + let total = Duration::from_secs(duration_secs); + let mut _switched = None; + if let (Some(name), Some(at)) = (&switch_to, switch_at) { + tokio::time::sleep(Duration::from_secs(at)).await; + let next = open_microphone(name).await; + let started = std::time::Instant::now(); + handle + .microphone_switch() + .expect("the recording has a switchable microphone") + .switch(next.lock.clone()) + .await + .expect("switch microphone"); + info!( + to = %name, + at_secs = at, + switch_ms = started.elapsed().as_secs_f64() * 1000.0, + "switched microphone" + ); + _switched = Some(next); + tokio::time::sleep(total.saturating_sub(Duration::from_secs(at))).await; + } else { + tokio::time::sleep(total).await; + } handle.stop().await.unwrap(); info!("Recording finished: {}", out.display()); From 33123e6521a58268ae0d152d8d1070fdbc8317e3 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:56:55 -0500 Subject: [PATCH 07/19] fix(recording): build the microphone switch on Windows and keep clippy's argument limit --- crates/recording/src/instant_recording.rs | 20 ++++++++++++-------- crates/recording/src/sources/microphone.rs | 14 +++++++++----- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs index 8ce739f352e..25778dda386 100644 --- a/crates/recording/src/instant_recording.rs +++ b/crates/recording/src/instant_recording.rs @@ -758,8 +758,10 @@ struct ScreenPipelineInput { async fn create_pipeline( content_dir: PathBuf, screen: ScreenPipelineInput, - mic_feed: Option>, - mic_switch_requests: Option>, + mic_feed: Option<( + Arc, + Option>, + )>, system_audio_source: Option, max_output_size: Option, start_time: Timestamps, @@ -866,8 +868,8 @@ async fn create_pipeline( .with_audio_source::(sys_audio); } - match (mic_feed, mic_switch_requests) { - (Some(lock), Some(switch_requests)) => { + match mic_feed { + Some((lock, Some(switch_requests))) => { builder = builder.with_audio_source::( crate::sources::SwitchableMicrophoneConfig { lock, @@ -875,10 +877,10 @@ async fn create_pipeline( }, ); } - (Some(mic), None) => { + Some((mic, None)) => { builder = builder.with_audio_source::(mic); } - (None, _) => {} + None => {} } let segment_tx_for_audio = segment_channel.as_ref().map(|(tx, _)| tx.clone()); @@ -1490,8 +1492,10 @@ async fn build_instant_recording_actor( #[cfg(target_os = "linux")] prepared_camera, }, - inputs.mic_feed.clone(), - mic_switch_requests.take(), + inputs + .mic_feed + .clone() + .map(|lock| (lock, mic_switch_requests.take())), system_audio_source, max_output_size, timestamps, diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index 08e7c9bde3c..0aa47d7fb49 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -644,19 +644,23 @@ impl Microphone { subscription: next_subscription.clone(), }, ); + let previous_name = previous.lock.device_name().to_string(); #[cfg(windows)] { - let _ = previous.subscription.retire(); - if let Err(error) = previous - .lock - .detach_recording_subscription(previous.subscription) + let MicrophoneBinding { + lock: previous_lock, + subscription: previous_subscription, + } = previous; + let _ = previous_subscription.retire(); + if let Err(error) = previous_lock + .detach_recording_subscription(previous_subscription) .await { warn!(%error, "Detaching the previous microphone after a switch"); } subscription = next_subscription; } - let previous_name = previous.lock.device_name().to_string(); + #[cfg(not(windows))] drop(previous); rx = next_rx; feed_lock = next; From 28d602196de0758732575fc5b9be0007e765df4b Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:14:20 -0500 Subject: [PATCH 08/19] fix(desktop): keep switching and pausing exclusive, size the Linux bar, retire switched Windows subscriptions alone --- apps/desktop-gpui/src/controls_window.rs | 12 ++++++++---- apps/desktop-gpui/src/recording.rs | 6 +++--- apps/desktop-gpui/src/session.rs | 5 +++-- apps/desktop-gpui/src/session/input_switch.rs | 7 ++++++- crates/recording/src/sources/microphone.rs | 7 +++++-- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index 368b539434a..85ea3a4ef88 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -191,6 +191,14 @@ impl ControlsWindow { return; } self.fitted_height = height; + // Linux has no native frame to grow from the bottom, so the bar's + // window takes the panel's height through gpui, never shrinking below + // the height it opened at. + #[cfg(target_os = "linux")] + window.resize(gpui::size( + px(crate::app_windows::CONTROLS_WIDTH), + px(height.max(crate::app_windows::CONTROLS_HEIGHT)), + )); let Some(native) = crate::platform::native_window(window) else { return; }; @@ -648,8 +656,6 @@ impl ControlsWindow { .detach(); } - /// The microphones the menu offers: what was found, plus the one in use - /// if enumeration has not caught up with it yet. fn microphone_choices(&self, cx: &gpui::App) -> Vec { let mut choices = self.microphones.clone(); if let Some(current) = &Feeds::global(cx).read(cx).microphone @@ -685,8 +691,6 @@ impl ControlsWindow { choices } - /// Rows under each heading, "None" included; a heading whose input this - /// recording cannot change shows no rows. fn settings_rows(&self, cx: &gpui::App) -> (usize, usize) { let session = self.session.read(cx); let microphones = if session.microphone_switch_available() { diff --git a/apps/desktop-gpui/src/recording.rs b/apps/desktop-gpui/src/recording.rs index 2527045be27..80571703b99 100644 --- a/apps/desktop-gpui/src/recording.rs +++ b/apps/desktop-gpui/src/recording.rs @@ -865,9 +865,6 @@ impl ActiveRecording { } } - /// Marks `at` in a Studio recording and returns the new marker count. - /// Instant recordings have no editor timeline to put a marker on, so - /// they take none. pub(crate) fn studio_handle(&self) -> Option { match &self.handle { Handle::Studio(handle) => Some(handle.clone()), @@ -901,6 +898,9 @@ impl ActiveRecording { drop(previous); } + /// Marks `at` in a Studio recording and returns the new marker count. + /// Instant recordings have no editor timeline to put a marker on, so + /// they take none. pub fn add_marker(&self, at: std::time::Instant) -> Option { match &self.handle { Handle::Studio(handle) => handle.add_marker(at), diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs index 9e31b5fa153..d6e9ff5096e 100644 --- a/apps/desktop-gpui/src/session.rs +++ b/apps/desktop-gpui/src/session.rs @@ -1510,7 +1510,7 @@ impl RecordingSession { let Some(active) = self.active.as_ref() else { return; }; - if self.pipeline_failed { + if self.pipeline_failed || self.input_switch_pending().is_some() { return; } let Some((ticket, registration)) = @@ -1615,7 +1615,8 @@ impl RecordingSession { /// straight away instead, because its pause keeps capture running. pub fn sleep_should_wait(&self) -> bool { self.phase == Phase::Stopping - || (self.pause_pending() && self.mode() != Some(recording::RecordingMode::Instant)) + || ((self.pause_pending() || self.input_switch_pending().is_some()) + && self.mode() != Some(recording::RecordingMode::Instant)) } pub fn woke_from_sleep(&mut self, cx: &mut Context) { diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index 7b176090b5c..f81d65aa169 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -106,8 +106,13 @@ impl RecordingSession { } } + /// Pause and switching share one guard: a Studio switch pauses and + /// resumes the actor itself, so neither may start while the other runs. fn input_switch_live(&self) -> bool { - matches!(self.phase, Phase::Recording { .. }) && !self.pipeline_failed + matches!(self.phase, Phase::Recording { .. }) + && !self.pipeline_failed + && !self.pause_pending() + && !self.pause_uncertain() } /// The recording started with a microphone and its pipeline can move to diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index 0aa47d7fb49..ee91dcb3079 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -444,7 +444,10 @@ impl Microphone { #[cfg(windows)] let source_cancel = ctx.stop_token().child_token(); #[cfg(windows)] - let subscription = feed_lock.recording_subscription(source_cancel.clone()); + // Retiring a subscription cancels its token. Each one gets a child of the + // source's token so a microphone switch can retire the old subscription + // without stopping the source. + let subscription = feed_lock.recording_subscription(source_cancel.child_token()); let binding: SharedMicrophoneBinding = Arc::new(std::sync::Mutex::new(MicrophoneBinding { lock: feed_lock.clone(), #[cfg(windows)] @@ -612,7 +615,7 @@ impl Microphone { continue; } #[cfg(windows)] - let next_subscription = next.recording_subscription(cancel.clone()); + let next_subscription = next.recording_subscription(cancel.child_token()); #[cfg(windows)] let attached = next .attach_recording_subscription( From 20af7a1f4025388a570583ad05642e5b6898bf47 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:28:31 +0000 Subject: [PATCH 09/19] chore(desktop): drop an unused switch error dismissal --- apps/desktop-gpui/src/session/input_switch.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index f81d65aa169..35cc1642931 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -100,12 +100,6 @@ impl RecordingSession { self.input_switch.error.as_deref() } - pub fn dismiss_input_switch_error(&mut self, cx: &mut Context) { - if self.input_switch.error.take().is_some() { - cx.notify(); - } - } - /// Pause and switching share one guard: a Studio switch pauses and /// resumes the actor itself, so neither may start while the other runs. fn input_switch_live(&self) -> bool { From b3a7adfcb1341844d4804315064c904041950768 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:06:36 +0000 Subject: [PATCH 10/19] fix(desktop): restart with the switched inputs and keep the Linux bar on screen --- apps/desktop-gpui/src/app_windows.rs | 2 +- apps/desktop-gpui/src/controls_window.rs | 51 +++++++++++++++---- apps/desktop-gpui/src/session/input_switch.rs | 34 +++++++++++++ 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index 009262bbeb1..7d689733f7f 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -47,7 +47,7 @@ pub const CONTROLS_HEIGHT: f32 = 150.; /// tall and the app under the empty space above the bar stays clickable. pub const CONTROLS_BAR_HEIGHT: f32 = 64.; const CONTROLS_FIT_TO_ISSUE: bool = cfg!(any(target_os = "macos", windows)); -const CONTROLS_BOTTOM_OFFSET: f64 = 120.; +pub(crate) const CONTROLS_BOTTOM_OFFSET: f64 = 120.; const TARGET_CONTROLS_OFFSET_Y: f64 = 48.; #[cfg(any(target_os = "macos", test))] diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index 85ea3a4ef88..13814e42f74 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -60,16 +60,33 @@ const SETTINGS_PANEL_PADDING: f32 = 4.; const SETTINGS_PANEL_MAX_HEIGHT: f32 = 360.; /// The card above the bar keeps this gap to it, like the issue card. const SETTINGS_PANEL_GAP: f32 = 8.; +const SETTINGS_LINUX_SCREEN_MARGIN: f32 = 8.; /// The panel's height for its rows: the camera preview switch, then each /// heading with its rows, the dividers between them, padding and border. /// Longer lists scroll inside the cap. -fn settings_panel_height(microphone_rows: usize, camera_rows: usize) -> f32 { +fn settings_panel_height(microphone_rows: usize, camera_rows: usize, cap: f32) -> f32 { let divider = 1. + 2. * SETTINGS_DIVIDER_MARGIN; let content = SETTINGS_ROW_HEIGHT + 2. * (divider + SETTINGS_HEADING_HEIGHT) + (microphone_rows + camera_rows) as f32 * SETTINGS_ROW_HEIGHT; - (content + 2. * (SETTINGS_PANEL_PADDING + 1.)).min(SETTINGS_PANEL_MAX_HEIGHT) + (content + 2. * (SETTINGS_PANEL_PADDING + 1.)).min(cap) +} + +/// Linux cannot keep the bar's bottom edge in place while the window grows +/// (X11 resizes from the top-left; Wayland clients cannot place themselves), +/// so there the bar moves down as the panel opens. It opens +/// `CONTROLS_BOTTOM_OFFSET` above the display's bottom, so growth stays +/// within that and the panel scrolls instead of pushing Stop off screen. +fn settings_panel_cap(issue: bool) -> f32 { + if cfg!(target_os = "linux") { + let window = crate::app_windows::CONTROLS_HEIGHT + + crate::app_windows::CONTROLS_BOTTOM_OFFSET as f32 + - SETTINGS_LINUX_SCREEN_MARGIN; + (window - controls_height(issue, None) - SETTINGS_PANEL_GAP).min(SETTINGS_PANEL_MAX_HEIGHT) + } else { + SETTINGS_PANEL_MAX_HEIGHT + } } /// The bar window's height: the bar, the issue card when one is up, and the @@ -182,11 +199,12 @@ impl ControlsWindow { } fn fit_window(&mut self, window: &mut Window, cx: &mut Context) { + let issue = self.issue(cx).is_some(); let panel = self.settings_open.then(|| { let (microphones, cameras) = self.settings_rows(cx); - settings_panel_height(microphones, cameras) + settings_panel_height(microphones, cameras, settings_panel_cap(issue)) }); - let height = controls_height(self.issue(cx).is_some(), panel); + let height = controls_height(issue, panel); if height == self.fitted_height { return; } @@ -857,7 +875,7 @@ impl ControlsWindow { let mut panel = div() .id("settings-panel") .w_full() - .max_h(px(SETTINGS_PANEL_MAX_HEIGHT)) + .max_h(px(settings_panel_cap(self.issue(cx).is_some()))) .mb(px(8.)) .flex() .flex_col() @@ -1241,17 +1259,32 @@ mod repaint_tests { let with_issue = crate::app_windows::CONTROLS_HEIGHT; assert_eq!(controls_height(false, None), bar); assert_eq!(controls_height(true, None), with_issue); - let panel = settings_panel_height(3, 2); + let panel = settings_panel_height(3, 2, SETTINGS_PANEL_MAX_HEIGHT); assert_eq!(controls_height(false, Some(panel)), bar + panel + 8.); assert_eq!(controls_height(true, Some(panel)), with_issue + panel + 8.); } #[test] fn the_settings_panel_fits_its_rows_and_scrolls_past_the_cap() { - let empty = settings_panel_height(0, 0); + let cap = SETTINGS_PANEL_MAX_HEIGHT; + let empty = settings_panel_height(0, 0, cap); assert_eq!(empty, 30. + 2. * (9. + 26.) + 10.); - assert_eq!(settings_panel_height(2, 1), empty + 3. * 30.); - assert_eq!(settings_panel_height(40, 40), 360.); + assert_eq!(settings_panel_height(2, 1, cap), empty + 3. * 30.); + assert_eq!(settings_panel_height(40, 40, cap), 360.); + } + + #[cfg(target_os = "linux")] + #[test] + fn the_linux_bar_never_grows_past_the_room_below_it() { + for issue in [false, true] { + let panel = settings_panel_height(40, 40, settings_panel_cap(issue)); + let window = + controls_height(issue, Some(panel)).max(crate::app_windows::CONTROLS_HEIGHT); + assert!( + window - crate::app_windows::CONTROLS_HEIGHT + <= crate::app_windows::CONTROLS_BOTTOM_OFFSET as f32 + ); + } } #[test] diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index 35cc1642931..00a669686b4 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -194,6 +194,7 @@ impl RecordingSession { ) { match request { InputRequest::Camera(selection) => { + self.remember_input(&InputRequest::Camera(selection.clone())); Feeds::global(cx).update(cx, |feeds, cx| { let selected = selection.is_some(); feeds.set_camera(selection, cx); @@ -248,6 +249,9 @@ impl RecordingSession { feeds.adopt_recording_microphone(label.clone(), cx); }); this.set_instant_microphone_muted(false); + this.remember_input(&InputRequest::Microphone(Some( + label.clone(), + ))); this.input_switch.error = None; tracing::info!(microphone = %label, "instant recording switched microphone"); } @@ -313,6 +317,7 @@ impl RecordingSession { self.paused_since = Some(now); self.input_switch.timer_frozen_at = Some(now); } + let requested = request.clone(); cx.spawn(async move |this, cx| { let outcome = studio_switch(&this, cx, handle, request, previous, was_paused, generation).await; @@ -328,6 +333,9 @@ impl RecordingSession { this.phase = Phase::Recording { paused: true }; this.input_switch.timer_frozen_at = None; } + if outcome.error.is_none() { + this.remember_input(&requested); + } this.input_switch.error = outcome.error; this.finish_input_switch(generation, cx); }) @@ -336,6 +344,32 @@ impl RecordingSession { .detach(); } + /// Restart reuses the config the recording started with, so a switched + /// input must replace the one recorded there, or the restarted recording + /// asks the feed for the device it no longer has. + fn remember_input(&mut self, request: &InputRequest) { + let Some(config) = self.last_config.as_mut() else { + return; + }; + match request { + InputRequest::Microphone(label) => { + config.device_settings.microphone = label + .as_deref() + .and_then(crate::store::RecordingDeviceSettings::for_microphone); + config.microphone = label.clone(); + } + InputRequest::Camera(camera) => { + config.device_settings.camera = camera.as_ref().and_then(|camera| { + crate::store::RecordingDeviceSettings::for_camera( + &camera.device_id, + camera.model_id.as_ref(), + ) + }); + config.camera = camera.as_ref().map(|camera| camera.id.clone()); + } + } + } + fn still_switching(&self, generation: u64) -> bool { generation == self.recording_generation && matches!(self.phase, Phase::Recording { .. }) } From 348c73b738889d40a58945450bfbc8239e8c26b5 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:59:03 +0000 Subject: [PATCH 11/19] fix(desktop): size the Linux settings panel from the room below the bar --- apps/desktop-gpui/src/controls_window.rs | 59 +++++++++++++++++------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index 13814e42f74..342fdb197eb 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -41,6 +41,8 @@ pub struct ControlsWindow { /// card and the settings panel when they are up. fitted_height: f32, settings_open: bool, + /// Linux only: free space under the bar's window when the panel opened. + room_below: f32, settings_dismissed_at: Option, microphones: Vec, cameras: Vec, @@ -75,20 +77,28 @@ fn settings_panel_height(microphone_rows: usize, camera_rows: usize, cap: f32) - /// Linux cannot keep the bar's bottom edge in place while the window grows /// (X11 resizes from the top-left; Wayland clients cannot place themselves), -/// so there the bar moves down as the panel opens. It opens -/// `CONTROLS_BOTTOM_OFFSET` above the display's bottom, so growth stays -/// within that and the panel scrolls instead of pushing Stop off screen. -fn settings_panel_cap(issue: bool) -> f32 { +/// so there the bar moves down as the panel opens. Growth stays within the +/// room measured below the window when the panel opened, and the panel +/// scrolls instead of pushing Stop off screen. +fn settings_panel_cap(issue: bool, room_below: f32) -> f32 { if cfg!(target_os = "linux") { - let window = crate::app_windows::CONTROLS_HEIGHT - + crate::app_windows::CONTROLS_BOTTOM_OFFSET as f32 - - SETTINGS_LINUX_SCREEN_MARGIN; - (window - controls_height(issue, None) - SETTINGS_PANEL_GAP).min(SETTINGS_PANEL_MAX_HEIGHT) + let window = + crate::app_windows::CONTROLS_HEIGHT + room_below.max(0.) - SETTINGS_LINUX_SCREEN_MARGIN; + (window - controls_height(issue, None) - SETTINGS_PANEL_GAP) + .clamp(2. * SETTINGS_ROW_HEIGHT, SETTINGS_PANEL_MAX_HEIGHT) } else { SETTINGS_PANEL_MAX_HEIGHT } } +#[cfg(target_os = "linux")] +fn room_below(window: &Window, cx: &gpui::App) -> f32 { + let Some(display) = window.display(cx) else { + return 0.; + }; + f32::from(display.visible_bounds().bottom() - window.bounds().bottom()).max(0.) +} + /// The bar window's height: the bar, the issue card when one is up, and the /// settings panel when it is open. fn controls_height(issue: bool, settings_panel: Option) -> f32 { @@ -167,6 +177,7 @@ impl ControlsWindow { level_bar, fitted_height: crate::app_windows::CONTROLS_BAR_HEIGHT, settings_open: false, + room_below: crate::app_windows::CONTROLS_BOTTOM_OFFSET as f32, settings_dismissed_at: None, microphones: Vec::new(), cameras: Vec::new(), @@ -202,7 +213,11 @@ impl ControlsWindow { let issue = self.issue(cx).is_some(); let panel = self.settings_open.then(|| { let (microphones, cameras) = self.settings_rows(cx); - settings_panel_height(microphones, cameras, settings_panel_cap(issue)) + settings_panel_height( + microphones, + cameras, + settings_panel_cap(issue, self.room_below), + ) }); let height = controls_height(issue, panel); if height == self.fitted_height { @@ -238,6 +253,10 @@ impl ControlsWindow { return; } self.settings_open = !self.settings_open; + #[cfg(target_os = "linux")] + if self.settings_open { + self.room_below = room_below(window, cx); + } if self.settings_open { self.refresh_devices(window, cx); } @@ -875,7 +894,10 @@ impl ControlsWindow { let mut panel = div() .id("settings-panel") .w_full() - .max_h(px(settings_panel_cap(self.issue(cx).is_some()))) + .max_h(px(settings_panel_cap( + self.issue(cx).is_some(), + self.room_below, + ))) .mb(px(8.)) .flex() .flex_col() @@ -1277,13 +1299,16 @@ mod repaint_tests { #[test] fn the_linux_bar_never_grows_past_the_room_below_it() { for issue in [false, true] { - let panel = settings_panel_height(40, 40, settings_panel_cap(issue)); - let window = - controls_height(issue, Some(panel)).max(crate::app_windows::CONTROLS_HEIGHT); - assert!( - window - crate::app_windows::CONTROLS_HEIGHT - <= crate::app_windows::CONTROLS_BOTTOM_OFFSET as f32 - ); + for room in [48., 120., 400.] { + let panel = settings_panel_height(40, 40, settings_panel_cap(issue, room)); + let grown = controls_height(issue, Some(panel)) + .max(crate::app_windows::CONTROLS_HEIGHT) + - crate::app_windows::CONTROLS_HEIGHT; + assert!( + grown <= room || panel <= 2. * SETTINGS_ROW_HEIGHT, + "issue {issue}, room {room}: grew {grown}" + ); + } } } From 6e7682f8d223edb8f598f948390a874417ada008 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:38:07 +0000 Subject: [PATCH 12/19] test(linux): find the wider recording bar in the desktop harness --- scripts/linux-desktop-harness/scenario-start-stop.sh | 2 +- scripts/linux-desktop-harness/stop-locate.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/linux-desktop-harness/scenario-start-stop.sh b/scripts/linux-desktop-harness/scenario-start-stop.sh index 40dc60dbe9d..ed37f59479d 100644 --- a/scripts/linux-desktop-harness/scenario-start-stop.sh +++ b/scripts/linux-desktop-harness/scenario-start-stop.sh @@ -79,7 +79,7 @@ use_control() { --method org.gnome.Shell.Eval 'Main.overview.hide()' >/dev/null 2>&1 sleep 1 rect=$(gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \ - --method org.gnome.Shell.Eval "(() => { const w = global.display.list_all_windows().find(w => /^(Cap|so\.cap\.Cap)$/.test(w.get_wm_class() || '') && w.showing_on_its_workspace() && w.get_frame_rect().width === 320 && w.get_frame_rect().height === 150); if (!w) return ''; const r = w.get_frame_rect(); return [r.x, r.y, r.width, r.height].join(' '); })()" \ + --method org.gnome.Shell.Eval "(() => { const w = global.display.list_all_windows().find(w => /^(Cap|so\.cap\.Cap)$/.test(w.get_wm_class() || '') && w.showing_on_its_workspace() && w.get_frame_rect().width === 352 && w.get_frame_rect().height === 150); if (!w) return ''; const r = w.get_frame_rect(); return [r.x, r.y, r.width, r.height].join(' '); })()" \ 2>/dev/null | sed -n "s/^(true, '\\(.*\\)')$/\\1/p" | tr -d '"') echo "controls window: ${rect:-not shown}" >>"$out/actions.log" result visible_controls_shown "$([[ -n "$rect" ]] && echo yes || echo no)" diff --git a/scripts/linux-desktop-harness/stop-locate.py b/scripts/linux-desktop-harness/stop-locate.py index 0e816ac2a7a..c76de3447ce 100755 --- a/scripts/linux-desktop-harness/stop-locate.py +++ b/scripts/linux-desktop-harness/stop-locate.py @@ -7,7 +7,7 @@ GNOME Shell writes an empty screenshot when it renders on a GPU or without a DRM device, so with no picture to read this prints where the button sits in -the 320x150 controls. +the 352x150 controls. Usage: stop-locate.py """ From 3bcd343fde220f418ff14b27bb0684c17c430730 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:10:39 +0000 Subject: [PATCH 13/19] fix(desktop): keep the Instant camera until the new one opens --- apps/desktop-gpui/src/session/input_switch.rs | 66 ++++++++++++++++--- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index 00a669686b4..de0964199ca 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -7,7 +7,8 @@ //! is opened on a feed of its own first, and only once it works is the live //! source moved onto it, so a device that fails to open never touches the //! recording. Instant's camera is the on-screen bubble, which the recording -//! does not hold, so switching it is just a new preview selection. +//! does not hold, so switching it is a new preview selection, kept only once +//! the new camera opens. use std::{sync::Arc, time::Instant}; @@ -194,16 +195,52 @@ impl RecordingSession { ) { match request { InputRequest::Camera(selection) => { - self.remember_input(&InputRequest::Camera(selection.clone())); - Feeds::global(cx).update(cx, |feeds, cx| { - let selected = selection.is_some(); - feeds.set_camera(selection, cx); - if selected { - feeds.resume_camera_preview(cx); - } + let previous = Feeds::global(cx).read(cx).camera.clone(); + let ready = Feeds::global(cx).update(cx, |feeds, cx| { + show_camera(feeds, selection.clone(), cx); + feeds.input_readiness().camera }); - self.input_switch.error = None; - self.finish_input_switch(generation, cx); + let Some(ready) = ready.filter(|_| selection.is_some() && previous != selection) + else { + self.remember_input(&InputRequest::Camera(selection)); + self.input_switch.error = None; + self.finish_input_switch(generation, cx); + return; + }; + // The bubble is what Instant records: keep the working camera + // unless the new one actually opens. + let opening = gpui_tokio::Tokio::spawn_result(cx, async move { + ready.await.map_err(anyhow::Error::msg) + }); + cx.spawn(async move |this, cx| { + let result = opening.await; + this.update(cx, |this, cx| { + if generation != this.recording_generation { + return; + } + let request = InputRequest::Camera(selection.clone()); + match result { + Ok(_) => { + this.remember_input(&request); + this.input_switch.error = None; + } + Err(error) => { + tracing::warn!("instant camera switch failed: {error:#}"); + Feeds::global(cx).update(cx, |feeds, cx| { + if feeds.camera == selection { + show_camera(feeds, previous.clone(), cx); + } + }); + this.input_switch.error = Some( + tr::controls::settings::switch_failed(&request.device_label()), + ); + } + } + this.finish_input_switch(generation, cx); + }) + .ok(); + }) + .detach(); } InputRequest::Microphone(None) => { self.set_instant_microphone_muted(true); @@ -375,6 +412,15 @@ impl RecordingSession { } } +/// Points the preview at `selection` and brings the bubble back for a camera. +fn show_camera(feeds: &mut Feeds, selection: Option, cx: &mut Context) { + let selected = selection.is_some(); + feeds.set_camera(selection, cx); + if selected { + feeds.resume_camera_preview(cx); + } +} + async fn studio_switch( this: &WeakEntity, cx: &mut AsyncApp, From f324b3708b4e109036a7394058e2a43e095160f7 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:10:39 +0000 Subject: [PATCH 14/19] fix(desktop): reopen a hidden Linux camera preview after recording --- apps/desktop-gpui/src/app_windows.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index 1244ae3e3e6..bc54307ce05 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -7279,6 +7279,7 @@ pub(crate) fn camera_preview_shown(cx: &App) -> bool { pub(crate) fn set_camera_preview_shown(shown: bool, cx: &mut App) { let Some(handle) = cx.global::().camera else { if shown { + cx.global_mut::().camera_preview_hidden = false; open_camera_window(cx); } return; @@ -7288,6 +7289,9 @@ pub(crate) fn set_camera_preview_shown(shown: bool, cx: &mut App) { let _ = handle; if !shown { close_camera_window(cx); + // Closing clears the flag; keep the choice so the end of the + // recording reopens the preview, as the other platforms do. + cx.global_mut::().camera_preview_hidden = true; } } #[cfg(not(target_os = "linux"))] @@ -8300,6 +8304,23 @@ mod tests { } } + /// Linux hides the bar's camera preview by closing its window. The choice + /// must survive the close, or the end of the recording never reopens it. + #[cfg(target_os = "linux")] + #[gpui::test] + fn a_hidden_linux_camera_preview_is_reopened_after_recording(cx: &mut gpui::TestAppContext) { + cx.update(|cx| { + let mut windows = test_app_windows(Vec::new()); + windows.camera = Some(WindowHandle::new((u64::MAX - 1).into())); + cx.set_global(windows); + assert!(camera_preview_shown(cx)); + set_camera_preview_shown(false, cx); + assert!(!camera_preview_shown(cx)); + assert!(cx.global::().camera.is_none()); + assert!(cx.global::().camera_preview_hidden); + }); + } + #[cfg(target_os = "linux")] struct DeleteOrigin; From 125152146a4aa94f0ac9ddd9f3ee278afeb2948e Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:29:30 +0000 Subject: [PATCH 15/19] fix(recording): keep audio in place across an Instant microphone switch --- crates/recording/src/sources/microphone.rs | 168 +++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index ee91dcb3079..6122d439c89 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -584,6 +584,9 @@ impl Microphone { let mut next_reconnect_after = MIC_RECONNECT_AFTER; let mut last_timestamp: Option = None; let mut last_frame_duration = SILENCE_CHUNK_DURATION; + // Where the previous microphone's audio ended, until the + // switched-to one delivers its first frame. + let mut switched_from: Option = None; let mut logged_current_source: Option<(u32, u16, SampleFormat)> = None; loop { @@ -681,6 +684,7 @@ impl Microphone { if silence_mode { silence_start = Some(Instant::now()); } + switched_from = last_timestamp.map(|ts| ts + last_frame_duration); info!( from = %previous_name, to = %feed_lock.device_name(), @@ -770,6 +774,38 @@ impl Microphone { Some((frame.sample_rate, frame.channels, frame.format)); } + // The muxer lays audio out by sample count and only fills + // gaps longer than its threshold, so the hand-over between + // two microphones (frames still queued for the old one, + // the new one's first buffer) would otherwise pull every + // later sample earlier. Fill it exactly. + if let Some(from) = switched_from.take() { + let reference = cap_timestamp::Timestamps::now(); + let gap_secs = + frame.timestamp.signed_duration_since_secs(reference) + - from.signed_duration_since_secs(reference); + if let Some(samples) = + switch_gap_samples(gap_secs, audio_info.rate()) + { + info!( + gap_ms = gap_secs * 1000.0, + "Filling the microphone switch hand-over with silence" + ); + let silence = AudioFrame::new( + create_silence_frame(&audio_info, samples), + from, + ); + if let Ok(Ok(())) = tokio::time::timeout( + send_timeout, + audio_tx.send(silence), + ) + .await + { + silence_counter.fetch_add(1, Ordering::Relaxed); + } + } + } + let audio_frame = if let Some(ref mut ctx) = resampler { ctx.resample( &frame.data, @@ -1242,6 +1278,18 @@ async fn microphone_producers_join( Ok(()) } +/// Silence that bridges a microphone switch: the time between the end of the +/// previous microphone's audio and the new one's first frame. An overlap or a +/// timestamp far off (a clock the two devices do not share) adds nothing. +fn switch_gap_samples(gap_secs: f64, rate: i32) -> Option { + const MAX_SWITCH_GAP_SECS: f64 = 2.0; + if !gap_secs.is_finite() || gap_secs <= 0.0 || gap_secs > MAX_SWITCH_GAP_SECS { + return None; + } + let samples = (gap_secs * f64::from(rate)).round() as usize; + (samples > 0).then_some(samples) +} + fn create_silence_frame(info: &AudioInfo, sample_count: usize) -> ffmpeg::frame::Audio { let mut frame = ffmpeg::frame::Audio::new(info.sample_format, sample_count, info.channel_layout()); @@ -2291,6 +2339,126 @@ mod switch_tests { } } + type Placed = Arc>>; + + /// Each frame's peak and sample count, in the order the muxer lays them out. + struct PlacingMuxer(Placed); + + impl Muxer for PlacingMuxer { + type Config = Placed; + + async fn setup( + placed: Placed, + _: std::path::PathBuf, + _: Option, + _: Option, + _: Arc, + _: &mut TaskPool, + ) -> anyhow::Result { + Ok(Self(placed)) + } + + fn finish(&mut self, _: Duration) -> anyhow::Result> { + Ok(Ok(())) + } + } + + impl AudioMuxer for PlacingMuxer { + fn send_audio_frame(&mut self, frame: AudioFrame, _: Duration) -> anyhow::Result<()> { + let samples = frame.inner.samples(); + let peak = frame + .inner + .data(0) + .chunks_exact(4) + .take(samples) + .map(|bytes| f32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]).abs()) + .fold(0.0f32, f32::max); + self.0.lock().unwrap().push((peak, samples)); + Ok(()) + } + } + + #[test] + fn switch_gap_samples_bridge_only_a_real_hand_over() { + assert_eq!(switch_gap_samples(0.040, 48_000), Some(1_920)); + assert_eq!(switch_gap_samples(0.0, 48_000), None); + assert_eq!(switch_gap_samples(-0.010, 48_000), None); + assert_eq!(switch_gap_samples(5.0, 48_000), None); + assert_eq!(switch_gap_samples(f64::NAN, 48_000), None); + } + + /// The muxer lays audio out by sample count and only fills gaps above its + /// threshold, so an unfilled hand-over between two microphones would pull + /// everything recorded after a switch earlier, out of sync with the video. + #[tokio::test] + async fn audio_after_a_switch_keeps_its_place_on_the_timeline() { + let built_in = SyntheticMicrophone::spawn("built-in", 48_000, 1); + let headset = SyntheticMicrophone::spawn("headset", 48_000, 1); + let (switch, switch_requests) = MicrophoneSwitch::channel(); + let placed = Placed::default(); + let temp = tempfile::tempdir().unwrap(); + let pipeline = OutputPipeline::builder(temp.path().join("audio.ogg")) + .with_audio_source::(SwitchableMicrophoneConfig { + lock: Arc::new(built_in.lock().await.unwrap()), + switch_requests, + }) + .build::(placed.clone()) + .await + .unwrap(); + + let start = Instant::now(); + push_for( + &built_in, + 0.1, + (48_000, 1), + start, + Duration::ZERO, + Duration::from_millis(300), + ) + .await; + let settle_deadline = Instant::now() + Duration::from_secs(10); + while placed.lock().unwrap().iter().map(|(_, n)| n).sum::() < 14_400 { + assert!( + Instant::now() < settle_deadline, + "the first microphone never arrived" + ); + tokio::time::sleep(Duration::from_millis(10)).await; + } + + switch + .switch(Arc::new(headset.lock().await.unwrap())) + .await + .unwrap(); + // The headset's first buffer is stamped 40 ms after the built-in + // microphone's audio ended: under the muxer's own gap threshold. + push_for( + &headset, + 0.35, + (48_000, 1), + start, + Duration::from_millis(340), + Duration::from_millis(300), + ) + .await; + let deadline = Instant::now() + Duration::from_secs(10); + while !placed.lock().unwrap().iter().any(|(peak, _)| *peak > 0.3) { + assert!( + Instant::now() < deadline, + "the headset never reached the muxer" + ); + tokio::time::sleep(Duration::from_millis(10)).await; + } + pipeline.stop().await.unwrap(); + + let placed = placed.lock().unwrap().clone(); + let first_headset = placed.iter().position(|(peak, _)| *peak > 0.3).unwrap(); + let before: usize = placed[..first_headset].iter().map(|(_, n)| n).sum(); + assert_eq!( + before, 16_320, + "the headset's first sample belongs at 340 ms (48 kHz), not straight after the built-in microphone's last" + ); + } + #[tokio::test] async fn a_running_source_moves_to_a_device_with_a_different_format_and_releases_the_old_one() { let built_in = SyntheticMicrophone::spawn("built-in", 48_000, 1); From f49a886f609072a3b0efee4326d8e5f6e979716d Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:51:01 +0000 Subject: [PATCH 16/19] test(recording): feed the switch timeline test in real time --- crates/recording/src/sources/microphone.rs | 53 +++++++++------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs index 6122d439c89..ca772ab91d5 100644 --- a/crates/recording/src/sources/microphone.rs +++ b/crates/recording/src/sources/microphone.rs @@ -2406,40 +2406,29 @@ mod switch_tests { .await .unwrap(); + // Both microphones deliver in real time, as hardware does: a source + // left idle past its keepalive would pad the timeline itself. + let chunk = vec![0.1f32; 480]; let start = Instant::now(); - push_for( - &built_in, - 0.1, - (48_000, 1), - start, - Duration::ZERO, - Duration::from_millis(300), - ) - .await; - let settle_deadline = Instant::now() + Duration::from_secs(10); - while placed.lock().unwrap().iter().map(|(_, n)| n).sum::() < 14_400 { - assert!( - Instant::now() < settle_deadline, - "the first microphone never arrived" - ); - tokio::time::sleep(Duration::from_millis(10)).await; + for index in 0..30u32 { + let at = start + Duration::from_millis(10) * index; + tokio::time::sleep_until((at + Duration::from_millis(10)).into()).await; + built_in.push(&chunk, Timestamp::Instant(at)).await; } - switch .switch(Arc::new(headset.lock().await.unwrap())) .await .unwrap(); - // The headset's first buffer is stamped 40 ms after the built-in - // microphone's audio ended: under the muxer's own gap threshold. - push_for( - &headset, - 0.35, - (48_000, 1), - start, - Duration::from_millis(340), - Duration::from_millis(300), - ) - .await; + // The headset's first buffer starts 40 ms after the built-in + // microphone's audio ended (later if the switch itself was slow): + // under the muxer's own gap threshold. + let headset_at = Duration::from_millis(start.elapsed().as_millis().max(340) as u64); + let loud = vec![0.35f32; 480]; + for index in 0..30u32 { + let at = start + headset_at + Duration::from_millis(10) * index; + tokio::time::sleep_until((at + Duration::from_millis(10)).into()).await; + headset.push(&loud, Timestamp::Instant(at)).await; + } let deadline = Instant::now() + Duration::from_secs(10); while !placed.lock().unwrap().iter().any(|(peak, _)| *peak > 0.3) { assert!( @@ -2453,9 +2442,11 @@ mod switch_tests { let placed = placed.lock().unwrap().clone(); let first_headset = placed.iter().position(|(peak, _)| *peak > 0.3).unwrap(); let before: usize = placed[..first_headset].iter().map(|(_, n)| n).sum(); - assert_eq!( - before, 16_320, - "the headset's first sample belongs at 340 ms (48 kHz), not straight after the built-in microphone's last" + let expected = (headset_at.as_secs_f64() * 48_000.0).round() as usize; + assert!( + before.abs_diff(expected) <= 48, + "the headset's first sample belongs at {} ms ({expected} samples), not straight after the built-in microphone's last; it landed at {before}", + headset_at.as_millis() ); } From 0253920fd7c19bfbcf0e89498017570dabd31c2b Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:58:42 +0000 Subject: [PATCH 17/19] fix(desktop): leave the camera alone when Stop lands during a switch --- apps/desktop-gpui/src/session/input_switch.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index de0964199ca..7a12f694698 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -218,6 +218,12 @@ impl RecordingSession { if generation != this.recording_generation { return; } + // Stop parks the bubble, which fails a pending open: the + // recording is over, so neither restore nor remember. + if !this.still_switching(generation) { + this.finish_input_switch(generation, cx); + return; + } let request = InputRequest::Camera(selection.clone()); match result { Ok(_) => { From b2729ab436818f24189cc4a6e3b769034b560ef7 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:15:54 +0000 Subject: [PATCH 18/19] fix(desktop): reopen a hidden camera preview after a clean capture ends --- apps/desktop-gpui/src/app_windows.rs | 35 ++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index bc54307ce05..d010dc3cf8d 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -642,13 +642,15 @@ pub fn init(main: WindowHandle, session: Entity, c } close_controls(&session, cx); close_target_overlays(cx); - if cx.global::().camera_preview_hidden { - set_camera_preview_shown(true, cx); - } + // The clean-capture lease keeps the bubble closed until it is + // released, so the hidden preview reopens only after it. #[cfg(target_os = "linux")] if !restore_clean_capture_ui(cx) { return; } + if cx.global::().camera_preview_hidden { + set_camera_preview_shown(true, cx); + } let (finished_studio, studio_behaviour, preparing_editor, finished_instant, deleted) = session.update(cx, |session, _| { ( @@ -7279,8 +7281,12 @@ pub(crate) fn camera_preview_shown(cx: &App) -> bool { pub(crate) fn set_camera_preview_shown(shown: bool, cx: &mut App) { let Some(handle) = cx.global::().camera else { if shown { - cx.global_mut::().camera_preview_hidden = false; open_camera_window(cx); + // A bubble that could not open yet (a clean capture still holds + // it) stays hidden, so the next reopen tries again. + if cx.global::().camera.is_some() { + cx.global_mut::().camera_preview_hidden = false; + } } return; }; @@ -8321,6 +8327,27 @@ mod tests { }); } + /// A clean capture keeps the bubble closed until its lease is released. + /// Asking to show the preview while the lease holds must keep it hidden, + /// so the reopen after the lease is released still brings it back. + #[cfg(target_os = "linux")] + #[gpui::test] + fn a_hidden_camera_preview_stays_hidden_while_a_clean_capture_holds_it( + cx: &mut gpui::TestAppContext, + ) { + cx.update(|cx| { + let mut windows = test_app_windows(Vec::new()); + let mut lease = pending_clean_capture(RecordingMode::Studio, false); + lease.gate.started = true; + windows.clean_capture = Some(lease); + windows.camera_preview_hidden = true; + cx.set_global(windows); + set_camera_preview_shown(true, cx); + assert!(cx.global::().camera.is_none()); + assert!(cx.global::().camera_preview_hidden); + }); + } + #[cfg(target_os = "linux")] struct DeleteOrigin; From bb37a1e4dcfb628c0beec38e121ee111f4535a76 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:15:54 +0000 Subject: [PATCH 19/19] test(desktop): pin that Stop ends a pending camera switch --- apps/desktop-gpui/src/session/input_switch.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/apps/desktop-gpui/src/session/input_switch.rs b/apps/desktop-gpui/src/session/input_switch.rs index 7a12f694698..4c8823948ec 100644 --- a/apps/desktop-gpui/src/session/input_switch.rs +++ b/apps/desktop-gpui/src/session/input_switch.rs @@ -414,10 +414,16 @@ impl RecordingSession { } fn still_switching(&self, generation: u64) -> bool { - generation == self.recording_generation && matches!(self.phase, Phase::Recording { .. }) + switch_is_current(self.phase, self.recording_generation, generation) } } +/// Stop keeps the recording generation, so only the phase tells a switch that +/// its recording ended while it ran. +fn switch_is_current(phase: Phase, current: u64, generation: u64) -> bool { + generation == current && matches!(phase, Phase::Recording { .. }) +} + /// Points the preview at `selection` and brings the bubble back for a camera. fn show_camera(feeds: &mut Feeds, selection: Option, cx: &mut Context) { let selected = selection.is_some(); @@ -640,4 +646,18 @@ mod tests { assert_eq!(state.finish(), Some(mic("b"))); assert_eq!(state.finish(), None); } + + /// A camera open that fails because Stop parked the bubble must neither + /// restore the old camera nor remember the new one: Stop leaves the + /// generation alone, so the phase is what ends the switch. + #[test] + fn a_switch_outlived_by_its_recording_is_not_current() { + for paused in [false, true] { + assert!(switch_is_current(Phase::Recording { paused }, 3, 3)); + assert!(!switch_is_current(Phase::Recording { paused }, 4, 3)); + } + for phase in [Phase::Stopping, Phase::Idle, Phase::Starting] { + assert!(!switch_is_current(phase, 3, 3)); + } + } }