RUSTDOCFLAGS=-D warnings failed with twelve errors, none of them new behaviour: public docs linking to the private server_lines and PRESETS (plain code spans now), Event::... links in state.rs that name voelin_core's Event without importing it (voelin_core::Event::..), an unclosed <id> tag in a bridge field comment (a code span), and three explicit link targets that the label already resolves to. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
413 lines
15 KiB
Rust
413 lines
15 KiB
Rust
//! The audio settings page and the local volume of clients: the device
|
|
//! lists and the form, what a change of the form sends to the engine, the
|
|
//! microphone test, the push-to-talk and H.264 switches, the crash notice
|
|
//! and the volume dialog. The functions change the
|
|
//! [`AppState`] and say in the [`Cx`]'s update what
|
|
//! to send and keep; the devices, the hotkey, the store and the window are
|
|
//! the front end's.
|
|
|
|
use voelin_core::Command;
|
|
|
|
use crate::bridge::{AudioForm, ClientAudio};
|
|
use crate::host::{Cx, Persist};
|
|
use crate::settings::{ClientPlayback, DeviceChoices, apply_audio_form, audio_form};
|
|
use crate::state::AppState;
|
|
|
|
/// What the settings page shows when it opens ([`open_settings`]).
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct SettingsView {
|
|
/// The input devices' names, the system default first.
|
|
pub input_devices: Vec<String>,
|
|
/// The output devices' names, the system default first.
|
|
pub output_devices: Vec<String>,
|
|
/// The form of the audio settings.
|
|
pub audio: AudioForm,
|
|
/// The push-to-talk key works in every window.
|
|
pub global_ptt: bool,
|
|
/// The push-to-talk key.
|
|
pub ptt_key: String,
|
|
/// H.264 through the downloaded OpenH264 is on.
|
|
pub h264_enabled: bool,
|
|
}
|
|
|
|
/// An audio device the system lists: its id, its name and whether it is the
|
|
/// system's default of its direction.
|
|
pub type Device = (String, String, bool);
|
|
|
|
/// The settings page opens: the devices the system lists become the choices
|
|
/// (a device that is stored but not there stays in the list), and the form
|
|
/// and the switches are those of the state.
|
|
pub fn open_settings<L>(
|
|
state: &mut AppState<L>,
|
|
inputs: &[Device],
|
|
outputs: &[Device],
|
|
) -> SettingsView {
|
|
state.inputs = DeviceChoices::new(inputs, state.audio.input_device.as_deref());
|
|
state.outputs = DeviceChoices::new(outputs, state.audio.output_device.as_deref());
|
|
SettingsView {
|
|
input_devices: state.inputs.names.clone(),
|
|
output_devices: state.outputs.names.clone(),
|
|
audio: audio_form(&state.audio, &state.inputs, &state.outputs),
|
|
global_ptt: state.settings.global_ptt,
|
|
ptt_key: state.settings.ptt_key.clone(),
|
|
h264_enabled: state.settings.openh264,
|
|
}
|
|
}
|
|
|
|
/// The form changed: the engine gets the new audio settings, and they are
|
|
/// kept later ([`save_audio`]), if the form changed anything.
|
|
pub fn audio_changed<L>(state: &mut AppState<L>, cx: &mut Cx<'_>, form: &AudioForm) {
|
|
let audio = apply_audio_form(form, &state.audio, &state.inputs, &state.outputs);
|
|
if audio == state.audio {
|
|
return;
|
|
}
|
|
state.audio = audio;
|
|
state.audio_dirty = true;
|
|
cx.send(Command::SetAudioSettings(Box::new(state.audio.clone())));
|
|
}
|
|
|
|
/// Keep the audio settings, if they changed since they were kept.
|
|
pub fn save_audio<L>(state: &mut AppState<L>, cx: &mut Cx<'_>) {
|
|
if state.audio_dirty {
|
|
state.audio_dirty = false;
|
|
cx.persist(Persist::Audio);
|
|
}
|
|
}
|
|
|
|
/// The settings page closes: the microphone test stops and the audio
|
|
/// settings are kept. Whether the test was on.
|
|
pub fn close_settings<L>(state: &mut AppState<L>, cx: &mut Cx<'_>) -> bool {
|
|
let was_on = set_mic_test(state, cx, false);
|
|
save_audio(state, cx);
|
|
was_on
|
|
}
|
|
|
|
/// Start or stop the microphone test: whether that changed anything.
|
|
pub fn set_mic_test<L>(state: &mut AppState<L>, cx: &mut Cx<'_>, on: bool) -> bool {
|
|
if state.mic_test == on {
|
|
return false;
|
|
}
|
|
state.mic_test = on;
|
|
cx.send(Command::TestMicrophone { on });
|
|
true
|
|
}
|
|
|
|
/// The push-to-talk switch and key: kept, and the key to register as a
|
|
/// hotkey of the whole desktop, none to unregister it. A blank key leaves
|
|
/// the one that was set.
|
|
pub fn apply_ptt<L>(
|
|
state: &mut AppState<L>,
|
|
cx: &mut Cx<'_>,
|
|
enabled: bool,
|
|
key: &str,
|
|
) -> Option<String> {
|
|
let key = key.trim().to_owned();
|
|
state.settings.global_ptt = enabled;
|
|
if !key.is_empty() {
|
|
state.settings.ptt_key = key.clone();
|
|
}
|
|
cx.persist(Persist::Settings);
|
|
(enabled && !key.is_empty()).then_some(key)
|
|
}
|
|
|
|
/// The H.264 switch: kept; the front end tells the encoder.
|
|
pub fn set_h264<L>(state: &mut AppState<L>, cx: &mut Cx<'_>, enabled: bool) {
|
|
state.settings.openh264 = enabled;
|
|
cx.persist(Persist::Settings);
|
|
}
|
|
|
|
/// What the H.264 rows show ([`h264_view`]).
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct H264View {
|
|
/// The library is loaded.
|
|
pub loaded: bool,
|
|
/// It is being downloaded.
|
|
pub busy: bool,
|
|
/// What the front end's codec says.
|
|
pub status: String,
|
|
}
|
|
|
|
/// The H.264 rows for the codec's state: `loaded`, and its `status` text
|
|
/// ("Downloading …" while it is being fetched).
|
|
pub fn h264_view(loaded: bool, status: String) -> H264View {
|
|
H264View { loaded, busy: status.starts_with("Downloading"), status }
|
|
}
|
|
|
|
/// The crash reports switch: kept (the front end also tells the crash
|
|
/// handler and the settings service).
|
|
pub fn set_crash_reports<L>(state: &mut AppState<L>, cx: &mut Cx<'_>, enabled: bool) {
|
|
state.settings.crash_reports = enabled;
|
|
// Also in the blob, for older versions.
|
|
cx.persist(Persist::Settings);
|
|
}
|
|
|
|
/// The notice while crash reports are on disk: `summaries` are the first
|
|
/// lines of their panic messages, oldest first. Empty without reports.
|
|
pub fn crash_notice(summaries: &[String]) -> String {
|
|
let Some(newest) = summaries.last() else { return String::new() };
|
|
let saved = match summaries.len() {
|
|
1 => "1 crash report is saved".to_owned(),
|
|
n => format!("{n} crash reports are saved"),
|
|
};
|
|
format!("The app crashed earlier ({newest}). {saved} on this device.")
|
|
}
|
|
|
|
// Local volume and mute of clients.
|
|
|
|
/// Fill the volume dialog for `client` of the current session; none when
|
|
/// there is no such client.
|
|
pub fn open_client<L>(state: &mut AppState<L>, client: u16) -> Option<ClientAudio> {
|
|
let id = state.current?;
|
|
let info = state.sessions.get(&id)?.presence.clients.get(&client)?.clone();
|
|
let playback =
|
|
info.uid.as_ref().and_then(|uid| state.playback.get(uid)).copied().unwrap_or_default();
|
|
state.playback_dialog = Some((id, client, info.uid.clone()));
|
|
Some(ClientAudio {
|
|
id: i32::from(client),
|
|
name: info.nickname.into(),
|
|
volume: playback.volume * 100.0,
|
|
muted: playback.muted,
|
|
streaming: info.streaming == Some(true),
|
|
})
|
|
}
|
|
|
|
/// The volume dialog changed: the engine plays the client so, and the
|
|
/// choice is kept by the client's unique id (a client without one only for
|
|
/// this connection), the default as no choice. The tree shows who is muted.
|
|
pub fn client_playback_changed<L>(state: &mut AppState<L>, cx: &mut Cx<'_>, form: &ClientAudio) {
|
|
let Some((session, client, uid)) = state.playback_dialog.clone() else { return };
|
|
let playback =
|
|
ClientPlayback { volume: (form.volume / 100.0).clamp(0.0, 4.0), muted: form.muted };
|
|
let session = session as u64;
|
|
cx.send(Command::SetClientVolume { session, client, volume: playback.volume });
|
|
cx.send(Command::SetClientMuted { session, client, muted: playback.muted });
|
|
if let Some(uid) = uid {
|
|
if playback.is_default() {
|
|
state.playback.remove(&uid);
|
|
} else {
|
|
state.playback.insert(uid, playback);
|
|
}
|
|
cx.persist(Persist::Playback);
|
|
}
|
|
cx.update.refresh.tree = true;
|
|
}
|
|
|
|
/// The first client of the current session that is not us nor a query
|
|
/// client (`VOELIN_OPEN=client`): its id, when we are in the session.
|
|
pub fn first_client<L>(state: &AppState<L>) -> Option<u16> {
|
|
let view = state.current.and_then(|id| state.sessions.get(&id))?;
|
|
let own = view.state.own_client?;
|
|
view.presence.clients.values().filter(|c| c.id != own && !c.is_query).map(|c| c.id).min()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use voelin_core::VoiceState;
|
|
|
|
use super::*;
|
|
use crate::host::testing::{Fake, State, presence, session, state};
|
|
|
|
fn device(id: &str, name: &str, default: bool) -> Device {
|
|
(id.into(), name.into(), default)
|
|
}
|
|
|
|
#[test]
|
|
fn the_page_opens_with_the_devices_and_a_stored_one_stays() {
|
|
let mut st = state();
|
|
st.audio.input_device = Some("gone".into());
|
|
st.settings.global_ptt = true;
|
|
st.settings.ptt_key = "F9".into();
|
|
let inputs = [device("a", "Built-in", true), device("b", "Headset", false)];
|
|
let outputs = [device("a", "Built-in", false), device("b", "Headset", true)];
|
|
let view = open_settings(&mut st, &inputs, &outputs);
|
|
// The system default first, named after the one that is the default.
|
|
assert_eq!(
|
|
view.input_devices,
|
|
["System default (Built-in)", "Built-in", "Headset", "gone (not connected)"]
|
|
);
|
|
// Outputs have the other as the default, and no stored device.
|
|
assert_eq!(view.output_devices, ["System default (Headset)", "Built-in", "Headset"]);
|
|
assert_eq!(view.audio.input_index, 3, "the stored device is selected");
|
|
assert_eq!(view.audio.output_index, 0, "no stored output: the default");
|
|
assert!(view.global_ptt && view.ptt_key == "F9" && !view.h264_enabled);
|
|
assert_eq!(st.inputs.names, view.input_devices);
|
|
}
|
|
|
|
#[test]
|
|
fn a_change_of_the_form_goes_to_the_engine_once_and_is_kept_later() {
|
|
let mut st = state();
|
|
let host = Fake::default();
|
|
open_settings(&mut st, &[], &[]);
|
|
let mut form = audio_form(&st.audio, &st.inputs, &st.outputs);
|
|
// Nothing changed: nothing sent.
|
|
let mut cx = Cx::new(&host);
|
|
audio_changed(&mut st, &mut cx, &form);
|
|
assert!(cx.update.commands.is_empty() && !st.audio_dirty);
|
|
form.output_volume = 50.0;
|
|
audio_changed(&mut st, &mut cx, &form);
|
|
assert!(matches!(
|
|
cx.update.commands.as_slice(),
|
|
[Command::SetAudioSettings(audio)] if audio.output_volume == 0.5
|
|
));
|
|
assert!(st.audio_dirty);
|
|
// The same form again changes nothing.
|
|
audio_changed(&mut st, &mut cx, &form);
|
|
assert_eq!(cx.update.commands.len(), 1);
|
|
// Kept once, when the page closes or the clock ticks.
|
|
save_audio(&mut st, &mut cx);
|
|
save_audio(&mut st, &mut cx);
|
|
assert_eq!(cx.update.persist, [Persist::Audio]);
|
|
assert!(!st.audio_dirty);
|
|
}
|
|
|
|
#[test]
|
|
fn the_microphone_test_stops_with_the_page() {
|
|
let mut st = state();
|
|
let host = Fake::default();
|
|
let mut cx = Cx::new(&host);
|
|
assert!(!close_settings(&mut st, &mut cx), "it was not on");
|
|
assert!(cx.update.commands.is_empty());
|
|
assert!(set_mic_test(&mut st, &mut cx, true));
|
|
assert!(!set_mic_test(&mut st, &mut cx, true), "already on");
|
|
st.audio_dirty = true;
|
|
assert!(close_settings(&mut st, &mut cx), "it was on");
|
|
assert!(!st.mic_test);
|
|
assert!(matches!(
|
|
cx.update.commands.as_slice(),
|
|
[Command::TestMicrophone { on: true }, Command::TestMicrophone { on: false }]
|
|
));
|
|
assert_eq!(cx.update.persist, [Persist::Audio]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_push_to_talk_key_is_registered_when_it_is_on_and_set() {
|
|
let mut st = state();
|
|
let host = Fake::default();
|
|
let mut cx = Cx::new(&host);
|
|
assert_eq!(apply_ptt(&mut st, &mut cx, true, " F9 ").as_deref(), Some("F9"));
|
|
assert!(st.settings.global_ptt);
|
|
assert_eq!(st.settings.ptt_key, "F9");
|
|
// Off: unregistered, the key stays for later.
|
|
assert_eq!(apply_ptt(&mut st, &mut cx, false, "F10"), None);
|
|
assert_eq!(st.settings.ptt_key, "F10");
|
|
// A blank key leaves the one that was set, and registers nothing.
|
|
assert_eq!(apply_ptt(&mut st, &mut cx, true, " "), None);
|
|
assert_eq!(st.settings.ptt_key, "F10");
|
|
assert_eq!(cx.update.persist, [Persist::Settings, Persist::Settings, Persist::Settings]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_switches_are_kept() {
|
|
let mut st = state();
|
|
let host = Fake::default();
|
|
let mut cx = Cx::new(&host);
|
|
set_h264(&mut st, &mut cx, true);
|
|
set_crash_reports(&mut st, &mut cx, true);
|
|
assert!(st.settings.openh264 && st.settings.crash_reports);
|
|
assert_eq!(cx.update.persist, [Persist::Settings, Persist::Settings]);
|
|
assert_eq!(
|
|
h264_view(false, "Downloading OpenH264… 40%".into()),
|
|
H264View { loaded: false, busy: true, status: "Downloading OpenH264… 40%".into() }
|
|
);
|
|
assert!(!h264_view(true, "Loaded".into()).busy);
|
|
}
|
|
|
|
#[test]
|
|
fn the_crash_notice_counts_the_reports() {
|
|
assert_eq!(crash_notice(&[]), "");
|
|
assert_eq!(
|
|
crash_notice(&["index out of bounds".into()]),
|
|
"The app crashed earlier (index out of bounds). 1 crash report is saved on this device."
|
|
);
|
|
assert_eq!(
|
|
crash_notice(&["old".into(), "new".into()]),
|
|
"The app crashed earlier (new). 2 crash reports are saved on this device."
|
|
);
|
|
}
|
|
|
|
/// Connected, with Alice (11) in Games next to us (10), a query client
|
|
/// (12) and Zed (9, a lower id).
|
|
fn with_clients() -> State {
|
|
let mut st = state();
|
|
let mut p = (*presence()).clone();
|
|
p.clients.get_mut(&11).unwrap().streaming = Some(true);
|
|
for (id, name, query) in [(12, "serveradmin", true), (9, "Zed", false)] {
|
|
p.clients.insert(
|
|
id,
|
|
voelin_model::ClientInfo {
|
|
id,
|
|
channel: 2,
|
|
nickname: name.into(),
|
|
is_query: query,
|
|
..Default::default()
|
|
},
|
|
);
|
|
}
|
|
let view = st.sessions.entry(1).or_default();
|
|
view.presence = std::sync::Arc::new(p);
|
|
view.state = session(VoiceState::Connected, Some(2));
|
|
st
|
|
}
|
|
|
|
#[test]
|
|
fn the_volume_dialog_fills_from_what_was_kept() {
|
|
let mut st = with_clients();
|
|
assert_eq!(open_client(&mut st, 99), None, "no such client");
|
|
let form = open_client(&mut st, 11).unwrap();
|
|
assert_eq!((form.id, &*form.name, form.volume, form.muted), (11, "Alice", 100.0, false));
|
|
assert!(form.streaming);
|
|
assert_eq!(st.playback_dialog, Some((1, 11, Some("alice=".into()))));
|
|
st.playback.insert("alice=".into(), ClientPlayback { volume: 0.5, muted: true });
|
|
let form = open_client(&mut st, 11).unwrap();
|
|
assert_eq!((form.volume, form.muted), (50.0, true));
|
|
st.current = None;
|
|
assert_eq!(open_client(&mut st, 11), None, "no server shown");
|
|
}
|
|
|
|
#[test]
|
|
fn a_client_is_played_as_the_dialog_says_and_the_default_is_forgotten() {
|
|
let mut st = with_clients();
|
|
let host = Fake::default();
|
|
let mut cx = Cx::new(&host);
|
|
// No dialog open: nothing.
|
|
client_playback_changed(&mut st, &mut cx, &ClientAudio::default());
|
|
assert!(cx.update.commands.is_empty());
|
|
let mut form = open_client(&mut st, 11).unwrap();
|
|
form.volume = 900.0;
|
|
form.muted = true;
|
|
client_playback_changed(&mut st, &mut cx, &form);
|
|
// Clamped to four times, sent, kept by unique id, the tree shows it.
|
|
assert!(matches!(
|
|
cx.update.commands.as_slice(),
|
|
[
|
|
Command::SetClientVolume { session: 1, client: 11, volume: 4.0 },
|
|
Command::SetClientMuted { session: 1, client: 11, muted: true },
|
|
]
|
|
));
|
|
assert_eq!(st.playback["alice="], ClientPlayback { volume: 4.0, muted: true });
|
|
assert_eq!(cx.update.persist, [Persist::Playback]);
|
|
assert!(cx.update.refresh.tree);
|
|
// Back to the default: no entry.
|
|
form.volume = 100.0;
|
|
form.muted = false;
|
|
client_playback_changed(&mut st, &mut cx, &form);
|
|
assert!(st.playback.is_empty());
|
|
// A client without a unique id is played so only for now.
|
|
st.playback_dialog = Some((1, 9, None));
|
|
let before = cx.update.persist.len();
|
|
client_playback_changed(&mut st, &mut cx, &form);
|
|
assert!(st.playback.is_empty());
|
|
assert_eq!(cx.update.persist.len(), before);
|
|
}
|
|
|
|
#[test]
|
|
fn the_first_client_is_the_lowest_id_that_is_not_us() {
|
|
let mut st = with_clients();
|
|
assert_eq!(first_client(&st), Some(9), "not us (10), not the query client");
|
|
st.sessions.get_mut(&1).unwrap().state.own_client = None;
|
|
assert_eq!(first_client(&st), None, "not in the session");
|
|
st.current = None;
|
|
assert_eq!(first_client(&st), None);
|
|
}
|
|
}
|