From bf485720f51ec855b8796dd98ddb442a1b2560a7 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Sun, 25 Oct 2026 09:54:37 +0100 Subject: [PATCH] fix(client): keep the live voice session visible and stop orphaned microphones Switching from room A to room B replaced the voice state with a joining state while the microphone was still capturing in A, and a deleted room left the capture pipeline running with no visible state. A pending join is now stored separately, so the live session stays shown as connected until the server acknowledges the new room or refuses it. The capture pipeline is stopped whenever it no longer belongs to the current voice target, whatever a pending join is waiting for. --- src/ui/state/default.rs | 1 + src/ui/state/types.rs | 7 ++-- src/ui/state/voice.rs | 86 ++++++++++++++++++++++------------------- src/ui/update.rs | 42 +++++++++++++++++++- 4 files changed, 90 insertions(+), 46 deletions(-) diff --git a/src/ui/state/default.rs b/src/ui/state/default.rs index cb68d9f..9cdf67f 100644 --- a/src/ui/state/default.rs +++ b/src/ui/state/default.rs @@ -21,6 +21,7 @@ impl Default for UiState { browsing_dms: false, showing_voice: false, voice_target: None, + pending_voice_join: None, vault_enabled: false, scroll_bottom: false, voice: VoiceUiState::Off, diff --git a/src/ui/state/types.rs b/src/ui/state/types.rs index 8e1c2c1..7817458 100644 --- a/src/ui/state/types.rs +++ b/src/ui/state/types.rs @@ -5,10 +5,6 @@ use std::time::Instant; pub enum VoiceUiState { #[default] Off, - /// A join was requested; audio is not running yet. - Joining { - channel: String, - }, Active { channel: String, }, @@ -64,6 +60,9 @@ pub struct UiState { pub browsing_dms: bool, pub showing_voice: bool, pub voice_target: Option, + /// Voice room whose join was requested and not yet acknowledged. The current session, if + /// any, keeps running and keeps being shown until the server answers. + pub pending_voice_join: Option, pub vault_enabled: bool, pub scroll_bottom: bool, diff --git a/src/ui/state/voice.rs b/src/ui/state/voice.rs index a3404d5..2299853 100644 --- a/src/ui/state/voice.rs +++ b/src/ui/state/voice.rs @@ -14,45 +14,40 @@ pub enum VoiceStatus { } impl UiState { - /// Derives the displayed voice status from the connection and voice state. + /// Derives the displayed voice status from the connection and voice state. A running + /// session is always shown as connected, also while a switch to another room is pending, + /// because its microphone is still live. pub fn voice_status(&self) -> VoiceStatus { if !matches!(self.conn, ConnState::Connected { .. }) { return VoiceStatus::Off; } match &self.voice { - VoiceUiState::Off => VoiceStatus::Off, - VoiceUiState::Joining { .. } => VoiceStatus::Joining, VoiceUiState::Active { .. } => VoiceStatus::Connected, + _ if self.pending_voice_join.is_some() => VoiceStatus::Joining, + VoiceUiState::Off => VoiceStatus::Off, VoiceUiState::Error(message) => VoiceStatus::Failed(message.clone()), } } /// Handles a click on a voice channel. Returns true when a join request must be sent to - /// the server. Clicking the room you are already in only shows it: the server does not - /// answer a repeated join, so waiting for a reply would leave "joining" on screen forever. + /// the server. Clicking the room you are already in (or already joining) only shows it: + /// the server does not answer a repeated join, so waiting for a reply would leave + /// "joining" on screen forever. pub fn request_voice_join(&mut self, channel_id: &str) -> bool { self.showing_voice = true; - let current = match &self.voice { - VoiceUiState::Joining { channel } | VoiceUiState::Active { channel } => { - Some(channel.as_str()) - } - _ => None, - }; - if current == Some(channel_id) { + let in_room = + matches!(&self.voice, VoiceUiState::Active { channel } if channel == channel_id); + if in_room || self.pending_voice_join.as_deref() == Some(channel_id) { return false; } - self.voice = VoiceUiState::Joining { - channel: channel_id.to_string(), - }; + self.pending_voice_join = Some(channel_id.to_string()); true } - /// The server refused or lost a pending join: stop showing "joining". + /// The server refused or lost a pending join: stop showing "joining". The current session + /// is left untouched. pub fn cancel_pending_voice_join(&mut self) { - if matches!(self.voice, VoiceUiState::Joining { .. }) { - self.voice = VoiceUiState::Off; - self.voice_target = None; - } + self.pending_voice_join = None; } } @@ -70,55 +65,66 @@ mod tests { } } + fn active(channel: &str) -> VoiceUiState { + VoiceUiState::Active { + channel: channel.into(), + } + } + #[test] fn joining_becomes_connected_only_when_audio_is_active() { let mut ui = connected(); assert_eq!(ui.voice_status(), VoiceStatus::Off); assert!(ui.request_voice_join("lounge")); assert_eq!(ui.voice_status(), VoiceStatus::Joining); - ui.voice = VoiceUiState::Active { - channel: "lounge".into(), - }; + // The acknowledgement clears the request; audio start makes it connected. + ui.pending_voice_join = None; + ui.voice = active("lounge"); assert_eq!(ui.voice_status(), VoiceStatus::Connected); } #[test] - fn clicking_the_current_room_never_restarts_joining() { + fn clicking_the_current_or_pending_room_never_restarts_joining() { let mut ui = connected(); - ui.voice = VoiceUiState::Active { - channel: "lounge".into(), - }; + ui.voice = active("lounge"); ui.showing_voice = false; assert!(!ui.request_voice_join("lounge")); assert!(ui.showing_voice); assert_eq!(ui.voice_status(), VoiceStatus::Connected); - // A different room is a real join. assert!(ui.request_voice_join("other")); - assert_eq!(ui.voice_status(), VoiceStatus::Joining); + assert!(!ui.request_voice_join("other")); } #[test] - fn retry_after_failure_and_cancel_of_a_refused_join() { + fn switching_rooms_keeps_showing_the_live_session_until_acknowledged() { + let mut ui = connected(); + ui.voice = active("a"); + ui.voice_target = Some("a".into()); + assert!(ui.request_voice_join("b")); + // The microphone is still live in A, so the UI must say so. + assert_eq!(ui.voice, active("a")); + assert_eq!(ui.voice_status(), VoiceStatus::Connected); + // A refused join leaves the session in A alone. + ui.cancel_pending_voice_join(); + assert_eq!(ui.voice, active("a")); + assert_eq!(ui.voice_status(), VoiceStatus::Connected); + } + + #[test] + fn retry_after_failure_shows_joining_and_cancel_returns_to_failure() { let mut ui = connected(); ui.voice = VoiceUiState::Error("no mic".into()); assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into())); assert!(ui.request_voice_join("lounge")); + assert_eq!(ui.voice_status(), VoiceStatus::Joining); ui.cancel_pending_voice_join(); - assert_eq!(ui.voice_status(), VoiceStatus::Off); - // Cancelling does not touch an established connection. - ui.voice = VoiceUiState::Active { - channel: "lounge".into(), - }; - ui.cancel_pending_voice_join(); - assert_eq!(ui.voice_status(), VoiceStatus::Connected); + assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into())); } #[test] fn status_is_off_while_the_control_connection_is_down() { let ui = UiState { - voice: VoiceUiState::Active { - channel: "lounge".into(), - }, + voice: active("lounge"), ..UiState::default() }; assert_eq!(ui.voice_status(), VoiceStatus::Off); diff --git a/src/ui/update.rs b/src/ui/update.rs index 0fc96ff..4ae73b4 100644 --- a/src/ui/update.rs +++ b/src/ui/update.rs @@ -453,6 +453,9 @@ fn chat_handle(ui: &mut UiState, net: &NetHandle, event: NetEvent) { } if kind == "voice" { ui.voice_target = Some(channel_id.clone()); + if ui.pending_voice_join.as_deref() == Some(channel_id.as_str()) { + ui.pending_voice_join = None; + } // A newly acknowledged join is also an explicit retry after an audio error. if matches!(ui.voice, VoiceUiState::Error(_)) { ui.voice = VoiceUiState::Off; @@ -930,10 +933,16 @@ pub fn sync_voice_audio(state: &mut AppState) { return; } let active_voice = state.ui.voice_target.clone(); + if pipeline_should_stop( + active_voice.as_deref(), + state.audio.as_ref().and(state.voice_channel.as_deref()), + ) { + stop_voice(state); + } match active_voice { Some(id) => start_voice(state, &id), - // A pending join has no target until the server acknowledges it. - None if matches!(state.ui.voice, VoiceUiState::Joining { .. }) => {} + // No target (room deleted, revoked, left): the capture pipeline must not outlive it, + // whatever a pending join is waiting for. None => stop_voice(state), } } @@ -1018,3 +1027,32 @@ fn log_audio_stats(state: &AppState) { ); } } + +/// A live capture pipeline must belong to the room the client is currently targeting. When the +/// target disappears (room deleted, access revoked) or moves elsewhere, the microphone stops +/// immediately, independent of what a pending join is waiting for. +fn pipeline_should_stop(target: Option<&str>, live_channel: Option<&str>) -> bool { + live_channel.is_some() && live_channel != target +} + +#[cfg(test)] +mod voice_pipeline_tests { + use super::pipeline_should_stop; + + #[test] + fn deleted_room_stops_the_microphone() { + assert!(pipeline_should_stop(None, Some("a"))); + } + + #[test] + fn moving_to_another_room_stops_the_old_pipeline() { + assert!(pipeline_should_stop(Some("b"), Some("a"))); + } + + #[test] + fn matching_room_or_no_pipeline_is_left_alone() { + assert!(!pipeline_should_stop(Some("a"), Some("a"))); + assert!(!pipeline_should_stop(None, None)); + assert!(!pipeline_should_stop(Some("a"), None)); + } +}