//! The UI's settings keys (besides the engine's, in //! `voelin_core::settings`), and their conversion to and from the forms of //! the settings page. use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; use voelin_audio::process::NoiseLevel; use voelin_core::settings::{Key, Kind}; use voelin_core::{AudioSettings, TransmitMode}; use crate::app::AudioForm; /// The window's settings, one value (the `ui` blob of earlier versions). pub static UI: Key = Key::new( "ui", Kind::Json, "The window's settings: push-to-talk key, H.264, the share dialog's choices.", UiSettings::default, ); /// Volume and mute of clients, by unique id. pub static CLIENT_PLAYBACK: Key = Key::new( "client_playback", Kind::Json, "Volume and mute of clients, by unique id.", ClientPlaybackMap::new, ); /// Colours of the window. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum ThemeChoice { #[default] Dark, Light, /// The system's light or dark scheme. System, } impl ThemeChoice { pub fn as_str(self) -> &'static str { match self { Self::Dark => "dark", Self::Light => "light", Self::System => "system", } } pub fn parse(text: &str) -> Self { match text { "light" => Self::Light, "system" => Self::System, _ => Self::Dark, } } } /// `ui.theme`. (In a config file write `"ui.theme" = "light"` at the top: /// a `[ui]` table would be read as the `ui` blob.) pub static UI_THEME: Key = Key::new( "ui.theme", Kind::Choice(&["dark", "light", "system"]), "Colours of the window: dark, light, or the system's scheme.", ThemeChoice::default, ); fn positive_scale(v: &f32) -> Result<(), String> { if v.is_finite() && *v > 0.0 { Ok(()) } else { Err("must be a number above 0".into()) } } /// `ui.font_scale`: text size, 1 = normal (no upper limit). pub static UI_FONT_SCALE: Key = Key::new("ui.font_scale", Kind::Json, "Text size as a factor (a number; 1 = normal).", || 1.0) .validated(positive_scale); /// `ui.narrow_breakpoint`: below this window width (logical pixels) the /// phone layout is used. pub static UI_NARROW_BREAKPOINT: Key = Key::new( "ui.narrow_breakpoint", Kind::UInt { min: 0 }, "Window width in pixels below which the phone layout is used.", || 800, ); /// `ui.image_cache_mb`: memory for decoded images (avatars, icons, emoji). pub static UI_IMAGE_CACHE_MB: Key = Key::new( "ui.image_cache_mb", Kind::UInt { min: 0 }, "Memory in MB for decoded images (avatars, icons, emoji); 0 keeps none.", || 64, ); /// `ui.members_width`: how wide the members and streams panel is, in /// pixels. Dragging its handle stores it; there is no built-in maximum. pub static UI_MEMBERS_WIDTH: Key = Key::new( "ui.members_width", Kind::UInt { min: 0 }, "Width of the members and streams panel in pixels.", || 280, ); /// The UI's keys besides [`UI`] and [`CLIENT_PLAYBACK`], for registering. pub fn appearance_keys() -> [&'static dyn voelin_core::settings::Setting; 5] { [&UI_THEME, &UI_FONT_SCALE, &UI_NARROW_BREAKPOINT, &UI_IMAGE_CACHE_MB, &UI_MEMBERS_WIDTH] } /// Frame rates and bitrates (kbit/s) the share dialog offers; any other /// value can be typed in. pub const FPS_CHOICES: [u32; 3] = [15, 30, 60]; pub const BITRATE_CHOICES: [u32; 4] = [2500, 4608, 8000, 10_000]; /// The index of the choice closest to `value`. pub fn nearest_choice(choices: &[u32], value: u32) -> usize { (0..choices.len()).min_by_key(|&i| choices[i].abs_diff(value)).unwrap_or(0) } /// A whole number above 0 typed into a form field. pub fn parse_positive(text: &str) -> Option { text.trim().parse().ok().filter(|v| *v > 0) } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct UiSettings { /// Global push-to-talk hotkey (besides the button in the window). pub global_ptt: bool, /// E.g. `Ctrl+Shift+T` (`voelin_platform::Hotkey` syntax). pub ptt_key: String, /// H.264 through Cisco's OpenH264, downloaded on request. pub openh264: bool, /// The ScreenCast portal's token for the last shared screen. pub portal_restore_token: Option, pub share: ShareDefaults, /// Write crash reports to disk (opt-in; never uploaded). pub crash_reports: bool, } impl Default for UiSettings { fn default() -> Self { Self { global_ptt: true, ptt_key: "Ctrl+Shift+T".into(), openh264: false, portal_restore_token: None, share: ShareDefaults::default(), crash_reports: false, } } } /// The last choices in the share dialog. Frame rate and bitrate are the /// settings `stream.fps` and `stream.bitrate_kbps`; the indices of the /// closest choices are kept for older versions. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct ShareDefaults { pub fps_index: usize, pub bitrate_index: usize, pub audio: bool, pub auto_accept: bool, } impl Default for ShareDefaults { fn default() -> Self { Self { fps_index: 1, bitrate_index: 1, audio: true, auto_accept: false } } } /// Local playback of one client, kept by its unique id. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub struct ClientPlayback { /// Linear, 1 = unchanged. pub volume: f32, pub muted: bool, } impl Default for ClientPlayback { fn default() -> Self { Self { volume: 1.0, muted: false } } } impl ClientPlayback { pub fn is_default(&self) -> bool { *self == Self::default() } } pub type ClientPlaybackMap = BTreeMap; /// Audio devices as listed in the settings: the system default first. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct DeviceChoices { pub names: Vec, /// Device ids by index; `None` for the system default. pub ids: Vec>, } impl DeviceChoices { /// `devices` as `(id, name, is_default)`. A selected device that is not /// connected stays in the list, so the choice survives. pub fn new(devices: &[(String, String, bool)], selected: Option<&str>) -> Self { let default_name = devices.iter().find(|d| d.2).map(|d| d.1.as_str()); let mut choices = Self { names: vec![match default_name { Some(name) => format!("System default ({name})"), None => "System default".into(), }], ids: vec![None], }; for (id, name, _) in devices { choices.names.push(name.clone()); choices.ids.push(Some(id.clone())); } if let Some(id) = selected && !choices.ids.iter().any(|i| i.as_deref() == Some(id)) { choices.names.push(format!("{id} (not connected)")); choices.ids.push(Some(id.to_owned())); } choices } pub fn index_of(&self, id: Option<&str>) -> usize { self.ids.iter().position(|i| i.as_deref() == id).unwrap_or(0) } pub fn id_at(&self, index: i32) -> Option { usize::try_from(index).ok().and_then(|i| self.ids.get(i).cloned()).flatten() } } fn noise_index(level: NoiseLevel) -> i32 { match level { NoiseLevel::Low => 0, NoiseLevel::Moderate => 1, NoiseLevel::High => 2, NoiseLevel::VeryHigh => 3, } } fn noise_level(index: i32) -> NoiseLevel { match index { 0 => NoiseLevel::Low, 2 => NoiseLevel::High, 3 => NoiseLevel::VeryHigh, _ => NoiseLevel::Moderate, } } /// The settings page's form for `settings`. pub fn audio_form( settings: &AudioSettings, inputs: &DeviceChoices, outputs: &DeviceChoices, ) -> AudioForm { let p = &settings.processing; AudioForm { input_index: inputs.index_of(settings.input_device.as_deref()) as i32, output_index: outputs.index_of(settings.output_device.as_deref()) as i32, transmit: match settings.transmit { TransmitMode::PushToTalk => 0, TransmitMode::VoiceActivation => 1, TransmitMode::Continuous => 2, }, vad_threshold: settings.vad.threshold_db, echo_cancellation: p.echo_cancellation, noise_suppression: p.noise_suppression, noise_level: noise_index(p.noise_level), auto_gain: p.auto_gain, input_gain: p.input_gain_db, output_volume: settings.output_volume * 100.0, playback_buffer: settings.playback_buffer_ms as f32, } } /// `settings` changed as the form says; what the form does not show stays. pub fn apply_audio_form( form: &AudioForm, settings: &AudioSettings, inputs: &DeviceChoices, outputs: &DeviceChoices, ) -> AudioSettings { let mut s = settings.clone(); s.input_device = inputs.id_at(form.input_index); s.output_device = outputs.id_at(form.output_index); s.transmit = match form.transmit { 1 => TransmitMode::VoiceActivation, 2 => TransmitMode::Continuous, _ => TransmitMode::PushToTalk, }; s.vad.threshold_db = form.vad_threshold; s.processing.echo_cancellation = form.echo_cancellation; s.processing.noise_suppression = form.noise_suppression; s.processing.noise_level = noise_level(form.noise_level); s.processing.auto_gain = form.auto_gain; s.processing.input_gain_db = form.input_gain; s.output_volume = (form.output_volume / 100.0).clamp(0.0, 4.0); s.playback_buffer_ms = form.playback_buffer.round().clamp(20.0, 180.0) as u32; s } #[cfg(test)] mod tests { use super::*; fn devices() -> Vec<(String, String, bool)> { vec![ ("alsa:hw:0,0".into(), "Built-in".into(), true), ("alsa:hw:1,0".into(), "Headset".into(), false), ] } #[test] fn device_choices() { let choices = DeviceChoices::new(&devices(), None); assert_eq!(choices.names, ["System default (Built-in)", "Built-in", "Headset"]); assert_eq!(choices.index_of(Some("alsa:hw:1,0")), 2); assert_eq!(choices.id_at(0), None); assert_eq!(choices.id_at(2).as_deref(), Some("alsa:hw:1,0")); assert_eq!(choices.id_at(9), None); // An unplugged device stays selectable. let choices = DeviceChoices::new(&devices(), Some("usb:mic")); assert_eq!(choices.names.last().unwrap(), "usb:mic (not connected)"); assert_eq!(choices.index_of(Some("usb:mic")), 3); } #[test] fn audio_form_roundtrip() { let inputs = DeviceChoices::new(&devices(), None); let outputs = DeviceChoices::new(&devices(), None); let mut settings = AudioSettings { transmit: TransmitMode::VoiceActivation, input_device: Some("alsa:hw:1,0".into()), output_volume: 0.5, playback_buffer_ms: 80, ..Default::default() }; settings.vad.threshold_db = -35.0; settings.processing.noise_level = NoiseLevel::High; settings.processing.input_gain_db = 6.0; let form = audio_form(&settings, &inputs, &outputs); assert_eq!((form.input_index, form.output_index, form.transmit), (2, 0, 1)); assert_eq!(form.output_volume, 50.0); assert_eq!(apply_audio_form(&form, &settings, &inputs, &outputs), settings); let form = AudioForm { transmit: 2, echo_cancellation: false, playback_buffer: 500.0, ..form }; let changed = apply_audio_form(&form, &settings, &inputs, &outputs); assert_eq!(changed.transmit, TransmitMode::Continuous); assert!(!changed.processing.echo_cancellation); assert_eq!(changed.playback_buffer_ms, 180); } #[test] fn free_values() { assert_eq!(nearest_choice(&FPS_CHOICES, 60), 2); assert_eq!(nearest_choice(&FPS_CHOICES, 144), 2); assert_eq!(nearest_choice(&BITRATE_CHOICES, 3000), 0); assert_eq!(nearest_choice(&BITRATE_CHOICES, 50_000), 3); assert_eq!(parse_positive(" 144 "), Some(144)); assert_eq!(parse_positive("0"), None); assert_eq!(parse_positive("fast"), None); } #[test] fn ui_settings_defaults() { let parsed: UiSettings = serde_json::from_str(r#"{"openh264":true}"#).unwrap(); assert!(parsed.openh264); assert!(!parsed.crash_reports, "crash reports are opt-in"); assert_eq!(parsed.ptt_key, "Ctrl+Shift+T"); assert_eq!(parsed.share, ShareDefaults::default()); assert_eq!(serde_json::to_value(ThemeChoice::System).unwrap(), "system"); assert_eq!(ThemeChoice::parse("light"), ThemeChoice::Light); assert_eq!(ThemeChoice::parse("??"), ThemeChoice::Dark); assert!(UI_FONT_SCALE.validate(&0.0).is_err()); assert!(UI_FONT_SCALE.validate(&3.5).is_ok()); let map: ClientPlaybackMap = serde_json::from_str(r#"{"uid=":{"volume":0.5,"muted":false}}"#).unwrap(); assert_eq!(map["uid="].volume, 0.5); } }