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.
This commit is contained in:
parent
7d488faf59
commit
bf485720f5
4 changed files with 90 additions and 46 deletions
|
|
@ -21,6 +21,7 @@ impl Default for UiState {
|
||||||
browsing_dms: false,
|
browsing_dms: false,
|
||||||
showing_voice: false,
|
showing_voice: false,
|
||||||
voice_target: None,
|
voice_target: None,
|
||||||
|
pending_voice_join: None,
|
||||||
vault_enabled: false,
|
vault_enabled: false,
|
||||||
scroll_bottom: false,
|
scroll_bottom: false,
|
||||||
voice: VoiceUiState::Off,
|
voice: VoiceUiState::Off,
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,6 @@ use std::time::Instant;
|
||||||
pub enum VoiceUiState {
|
pub enum VoiceUiState {
|
||||||
#[default]
|
#[default]
|
||||||
Off,
|
Off,
|
||||||
/// A join was requested; audio is not running yet.
|
|
||||||
Joining {
|
|
||||||
channel: String,
|
|
||||||
},
|
|
||||||
Active {
|
Active {
|
||||||
channel: String,
|
channel: String,
|
||||||
},
|
},
|
||||||
|
|
@ -64,6 +60,9 @@ pub struct UiState {
|
||||||
pub browsing_dms: bool,
|
pub browsing_dms: bool,
|
||||||
pub showing_voice: bool,
|
pub showing_voice: bool,
|
||||||
pub voice_target: Option<String>,
|
pub voice_target: Option<String>,
|
||||||
|
/// 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<String>,
|
||||||
pub vault_enabled: bool,
|
pub vault_enabled: bool,
|
||||||
pub scroll_bottom: bool,
|
pub scroll_bottom: bool,
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,45 +14,40 @@ pub enum VoiceStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UiState {
|
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 {
|
pub fn voice_status(&self) -> VoiceStatus {
|
||||||
if !matches!(self.conn, ConnState::Connected { .. }) {
|
if !matches!(self.conn, ConnState::Connected { .. }) {
|
||||||
return VoiceStatus::Off;
|
return VoiceStatus::Off;
|
||||||
}
|
}
|
||||||
match &self.voice {
|
match &self.voice {
|
||||||
VoiceUiState::Off => VoiceStatus::Off,
|
|
||||||
VoiceUiState::Joining { .. } => VoiceStatus::Joining,
|
|
||||||
VoiceUiState::Active { .. } => VoiceStatus::Connected,
|
VoiceUiState::Active { .. } => VoiceStatus::Connected,
|
||||||
|
_ if self.pending_voice_join.is_some() => VoiceStatus::Joining,
|
||||||
|
VoiceUiState::Off => VoiceStatus::Off,
|
||||||
VoiceUiState::Error(message) => VoiceStatus::Failed(message.clone()),
|
VoiceUiState::Error(message) => VoiceStatus::Failed(message.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles a click on a voice channel. Returns true when a join request must be sent to
|
/// 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
|
/// the server. Clicking the room you are already in (or already joining) only shows it:
|
||||||
/// answer a repeated join, so waiting for a reply would leave "joining" on screen forever.
|
/// 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 {
|
pub fn request_voice_join(&mut self, channel_id: &str) -> bool {
|
||||||
self.showing_voice = true;
|
self.showing_voice = true;
|
||||||
let current = match &self.voice {
|
let in_room =
|
||||||
VoiceUiState::Joining { channel } | VoiceUiState::Active { channel } => {
|
matches!(&self.voice, VoiceUiState::Active { channel } if channel == channel_id);
|
||||||
Some(channel.as_str())
|
if in_room || self.pending_voice_join.as_deref() == Some(channel_id) {
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
if current == Some(channel_id) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self.voice = VoiceUiState::Joining {
|
self.pending_voice_join = Some(channel_id.to_string());
|
||||||
channel: channel_id.to_string(),
|
|
||||||
};
|
|
||||||
true
|
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) {
|
pub fn cancel_pending_voice_join(&mut self) {
|
||||||
if matches!(self.voice, VoiceUiState::Joining { .. }) {
|
self.pending_voice_join = None;
|
||||||
self.voice = VoiceUiState::Off;
|
|
||||||
self.voice_target = None;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,55 +65,66 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn active(channel: &str) -> VoiceUiState {
|
||||||
|
VoiceUiState::Active {
|
||||||
|
channel: channel.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn joining_becomes_connected_only_when_audio_is_active() {
|
fn joining_becomes_connected_only_when_audio_is_active() {
|
||||||
let mut ui = connected();
|
let mut ui = connected();
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
||||||
assert!(ui.request_voice_join("lounge"));
|
assert!(ui.request_voice_join("lounge"));
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Joining);
|
assert_eq!(ui.voice_status(), VoiceStatus::Joining);
|
||||||
ui.voice = VoiceUiState::Active {
|
// The acknowledgement clears the request; audio start makes it connected.
|
||||||
channel: "lounge".into(),
|
ui.pending_voice_join = None;
|
||||||
};
|
ui.voice = active("lounge");
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn clicking_the_current_room_never_restarts_joining() {
|
fn clicking_the_current_or_pending_room_never_restarts_joining() {
|
||||||
let mut ui = connected();
|
let mut ui = connected();
|
||||||
ui.voice = VoiceUiState::Active {
|
ui.voice = active("lounge");
|
||||||
channel: "lounge".into(),
|
|
||||||
};
|
|
||||||
ui.showing_voice = false;
|
ui.showing_voice = false;
|
||||||
assert!(!ui.request_voice_join("lounge"));
|
assert!(!ui.request_voice_join("lounge"));
|
||||||
assert!(ui.showing_voice);
|
assert!(ui.showing_voice);
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
||||||
// A different room is a real join.
|
|
||||||
assert!(ui.request_voice_join("other"));
|
assert!(ui.request_voice_join("other"));
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Joining);
|
assert!(!ui.request_voice_join("other"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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();
|
let mut ui = connected();
|
||||||
ui.voice = VoiceUiState::Error("no mic".into());
|
ui.voice = VoiceUiState::Error("no mic".into());
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into()));
|
assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into()));
|
||||||
assert!(ui.request_voice_join("lounge"));
|
assert!(ui.request_voice_join("lounge"));
|
||||||
|
assert_eq!(ui.voice_status(), VoiceStatus::Joining);
|
||||||
ui.cancel_pending_voice_join();
|
ui.cancel_pending_voice_join();
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into()));
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn status_is_off_while_the_control_connection_is_down() {
|
fn status_is_off_while_the_control_connection_is_down() {
|
||||||
let ui = UiState {
|
let ui = UiState {
|
||||||
voice: VoiceUiState::Active {
|
voice: active("lounge"),
|
||||||
channel: "lounge".into(),
|
|
||||||
},
|
|
||||||
..UiState::default()
|
..UiState::default()
|
||||||
};
|
};
|
||||||
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
||||||
|
|
|
||||||
|
|
@ -453,6 +453,9 @@ fn chat_handle(ui: &mut UiState, net: &NetHandle, event: NetEvent) {
|
||||||
}
|
}
|
||||||
if kind == "voice" {
|
if kind == "voice" {
|
||||||
ui.voice_target = Some(channel_id.clone());
|
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.
|
// A newly acknowledged join is also an explicit retry after an audio error.
|
||||||
if matches!(ui.voice, VoiceUiState::Error(_)) {
|
if matches!(ui.voice, VoiceUiState::Error(_)) {
|
||||||
ui.voice = VoiceUiState::Off;
|
ui.voice = VoiceUiState::Off;
|
||||||
|
|
@ -930,10 +933,16 @@ pub fn sync_voice_audio(state: &mut AppState) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let active_voice = state.ui.voice_target.clone();
|
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 {
|
match active_voice {
|
||||||
Some(id) => start_voice(state, &id),
|
Some(id) => start_voice(state, &id),
|
||||||
// A pending join has no target until the server acknowledges it.
|
// No target (room deleted, revoked, left): the capture pipeline must not outlive it,
|
||||||
None if matches!(state.ui.voice, VoiceUiState::Joining { .. }) => {}
|
// whatever a pending join is waiting for.
|
||||||
None => stop_voice(state),
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue