diff --git a/app/src/app.rs b/app/src/app.rs index 4752070..29956fa 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -69,7 +69,6 @@ pub struct Encoders { #[derive(Debug, Clone, serde::Deserialize)] pub struct EncoderInfo { pub id: String, - pub name: String, #[serde(default)] pub is_hardware: bool, #[serde(default)] @@ -217,7 +216,6 @@ impl App { self.encoders = Encoders { available: vec![EncoderInfo { id: "jim_nvenc".into(), - name: "NVIDIA NVENC".into(), available: true, is_hardware: true, }], @@ -281,6 +279,47 @@ impl App { self.daemon.get_settings(); } + /// The encoders this machine can run, as the daemon found them, hardware first. + pub fn runnable_encoders(&self) -> Vec<&'static str> { + let mut found: Vec<(&'static str, bool)> = self + .encoders + .available + .iter() + .filter(|encoder| encoder.available) + .filter_map(|encoder| { + let label = crate::settings::EncoderPreset::encoder_name(&encoder.id)?; + Some((label, encoder.is_hardware)) + }) + .collect(); + // Hardware first: it is the choice most people want, and the fallback below uses the + // first entry. + found.sort_by_key(|(_, hardware)| !hardware); + found.into_iter().map(|(label, _)| label).collect() + } + + /// Point the draft at an encoder this machine has when the stored one is missing. + /// + /// A config written elsewhere can name NVENC on a card that has none. The daemon's encoder + /// list is this machine's own answer, so the draft follows it, and saving keeps it. + fn settle_encoder(&mut self) { + if !self.form.loaded || self.form.dirty { + return; + } + let runnable = self.runnable_encoders(); + let Some(pick) = runnable.first() else { + return; + }; + let current = self.form.draft.video.encoder_preset.label(); + if runnable.contains(¤t) { + return; + } + self.form.draft.video.encoder_preset = self.form.draft.video.encoder_preset.with_kind(pick); + self.form.dirty = true; + self.notify(format!( + "{current} is not on this machine; recording will use {pick}" + )); + } + /// The process names the audio sources refer to, plus whatever is being typed into the /// add dialog. fn watch_names(&self) -> Vec { @@ -450,19 +489,27 @@ impl App { self.connected = false; self.notify_error(e); } - ( - ipc::Request::Settings - | ipc::Request::SaveSettings - | ipc::Request::ResetSettings, - Ok(data), - ) => match crate::settings::from_response(&data) { - Ok(settings) => { - self.form.apply(&settings); - self.settings = Some(settings); + (ipc::Request::Settings | ipc::Request::ResetSettings, Ok(data)) => { + match crate::settings::from_response(&data) { + Ok(settings) => { + self.form.apply(&settings); + self.settings = Some(settings); + self.settle_encoder(); + } + Err(e) => self.notify_error(e), } - Err(e) => self.notify_error(e), - }, + } + // The daemon answers a save with `{"success": true}`; the draft is already what + // it holds, so there is nothing to read back. + (ipc::Request::SaveSettings, Ok(_)) => { + self.settings = Some(self.form.settings()); + self.form.saving = false; + self.form.dirty = false; + self.form.loaded = true; + self.notify("Settings saved"); + } (ipc::Request::SaveSettings, Err(e)) => { + self.form.saving = false; self.notify_error(format!("Settings were not saved: {e}")) } (ipc::Request::ResetSettings, Err(e)) => { @@ -483,6 +530,7 @@ impl App { (ipc::Request::Encoders, Ok(data)) => { if let Ok(list) = serde_json::from_value(data) { self.encoders = list; + self.settle_encoder(); } } (ipc::Request::AudioDevices, Ok(data)) => { @@ -653,12 +701,13 @@ fn status_color(p: Paint, app: &App) -> (rune::Color, String) { /// The thin bar under the screens: daemon state on the left, the encoder on the right. fn statusbar(p: Paint, app: &App) -> El { let (dot_color, status) = status_color(p, app); - let name = crate::settings::EncoderPreset::encoder_name(&app.encoders.current) - .map(str::to_string) + // The draft carries the encoder this machine settled on, which is what a save keeps. + let name = app + .settings + .as_ref() + .map(|_| app.form.draft.video.encoder_preset.label().to_string()) .or_else(|| { - app.settings - .as_ref() - .map(|settings| settings.video.encoder_preset.label().to_string()) + crate::settings::EncoderPreset::encoder_name(&app.encoders.current).map(str::to_string) }); let recording = match (name, app.settings.as_ref().map(|s| s.video.frame_rate)) { (Some(name), Some(fps)) => Some(format!("{name} ยท {fps} fps")), diff --git a/app/src/screens/settings.rs b/app/src/screens/settings.rs index de20dd0..bbc80e5 100644 --- a/app/src/screens/settings.rs +++ b/app/src/screens/settings.rs @@ -17,7 +17,6 @@ pub struct Form { pub open_select: Option<&'static str>, pub path: TextEdit, pub naming: TextEdit, - pub frame_rate: TextEdit, pub split_size: TextEdit, pub split_time: TextEdit, pub poll_interval: TextEdit, @@ -33,7 +32,6 @@ impl Form { Self { path: TextEdit::new(&draft.output.path), naming: TextEdit::new(&draft.output.naming_pattern), - frame_rate: TextEdit::new(draft.video.frame_rate.to_string()), split_size: TextEdit::new(draft.output.split_size_mb.to_string()), split_time: TextEdit::new(draft.output.split_time_minutes.to_string()), poll_interval: TextEdit::new(draft.daemon.poll_interval_ms.to_string()), @@ -57,8 +55,6 @@ impl Form { self.draft = settings.clone(); self.path.replace_text(&settings.output.path); self.naming.replace_text(&settings.output.naming_pattern); - self.frame_rate - .replace_text(settings.video.frame_rate.to_string()); self.split_size .replace_text(settings.output.split_size_mb.to_string()); self.split_time @@ -77,13 +73,6 @@ impl Form { let mut out = self.draft.clone(); out.output.path = self.path.text.trim().to_string(); out.output.naming_pattern = self.naming.text.trim().to_string(); - out.video.frame_rate = self - .frame_rate - .text - .trim() - .parse() - .unwrap_or(60) - .clamp(1, 240); out.output.split_size_mb = self.split_size.text.trim().parse().unwrap_or(0); out.output.split_time_minutes = self.split_time.text.trim().parse().unwrap_or(0); out.daemon.poll_interval_ms = self @@ -271,6 +260,7 @@ pub fn view(app: &App, p: Paint) -> El { if !app.form.loaded { return; } + app.form.saving = true; let settings = app.form.settings(); match serde_json::to_value(&settings) { Ok(value) => app.daemon.save_settings(value), @@ -303,6 +293,8 @@ pub fn view(app: &App, p: Paint) -> El { fn save_label(app: &App, p: Paint) -> (String, rune::Color) { if !app.connected { ("Daemon offline".into(), p.danger) + } else if app.form.saving { + ("Saving\u{2026}".into(), p.muted()) } else if app.form.dirty { ("Unsaved changes".into(), p.warning) } else if app.form.loaded { @@ -397,37 +389,30 @@ fn quick(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: }, )], )) - .child(ui::card( + .child(ui::card_action( p, Some("Files"), - vec![ - ui::setting_row( - p, - "recordings-folder", - "Recordings folder", - text(short_path(&app.form.draft.output.path)) - .key("path") - .font(body(12.0)) - .color(p.muted()), - ), - row() - .key("files-actions") - .px(space::S3) - .pb(space::S2) - .child(ui::button( - p, - "open-recordings", - "Open folder", - false, - |_app: &mut App| { - crate::daemon::open_folder(&crate::daemon::recordings_dir()); - }, - )), - ], + Some(ui::button( + p, + "open-recordings", + "Open folder", + false, + |_app: &mut App| { + crate::daemon::open_folder(&crate::daemon::recordings_dir()); + }, + )), + vec![ui::setting_row( + p, + "recordings-folder", + "Recordings folder", + text(short_path(&app.form.draft.output.path)) + .key("path") + .font(body(12.0)) + .color(p.muted()), + )], )) } -/// A path as it fits on a row: the last two segments, with the rest elided. /// A path as it fits on a row: the last two segments, with the rest elided. fn short_path(path: &str) -> String { let sep = std::path::MAIN_SEPARATOR; @@ -442,83 +427,62 @@ fn short_path(path: &str) -> String { fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { let v = &app.form.draft.video; + let a = &app.form.draft.audio; let o = &app.form.draft.output; - let available = if app.encoders.available.is_empty() { - String::new() + + // Only the encoders this machine reports are offered. The static list stands in until the + // daemon has answered. + let offered = app.runnable_encoders(); + let encoders: Vec<&str> = if offered.is_empty() { + ENCODERS.to_vec() } else { - app.encoders - .available - .iter() - .filter(|e| e.available) - .map(|e| { - let kind = if e.is_hardware { - "hardware" - } else { - "software" - }; - format!("{} ({}, {kind})", e.name, e.id) - }) - .collect::>() - .join(", ") + offered }; - let mut video_rows = vec![ - select_row( - p, - kit, - layer, - "encoder", - "Encoder", - "Hardware encoding is faster; x264 uses the CPU", - &ENCODERS, - Some(v.encoder_preset.label().to_string()), - app, - ), - select_row( - p, - kit, - layer, - "quality", - "Quality", - v.quality.detail(), - &QUALITIES, - Some(v.quality.label().to_string()), - app, - ), - text_row( - p, - "frame-rate", - "Frame rate", - "Frames per second the daemon records", - &app.form.frame_rate, - |app| &mut app.form.frame_rate, - ), - ui::toggle_row( - p, - "hardware", - "Hardware acceleration", - v.hardware_acceleration, - |app: &mut App| { - app.form.draft.video.hardware_acceleration ^= true; - app.form.dirty = true; - }, - ), - ]; - if !available.is_empty() { - video_rows.push( - text(format!("Available on this machine: {available}")) - .key("encoder-note") - .font(body(11.0)) - .color(p.muted()) - .ml(space::S3) - .mb(6.0), - ); - } + // The stored encoder is shown as it is, even when this machine cannot run it, so the row + // never claims something the daemon does not hold. + let encoder = Some(v.encoder_preset.label().to_string()); content - .child(ui::card(p, Some("Video"), video_rows)) .child(ui::card( + p, + Some("Video"), + vec![ + select_row( + p, + kit, + layer, + "encoder", + "Encoder", + "The encoders this machine can run", + &encoders, + encoder, + app, + ), + select_row( + p, + kit, + layer, + "quality", + "Quality", + v.quality.detail(), + &QUALITIES, + Some(v.quality.label().to_string()), + app, + ), + ], + )) + .child(ui::card_action( p, Some("Output"), + Some(ui::button( + p, + "open-output", + "Open folder", + false, + |_app: &mut App| { + crate::daemon::open_folder(&crate::daemon::recordings_dir()); + }, + )), vec![ text_row( p, @@ -565,15 +529,55 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont ), ], )) - .child(row().key("recording-actions").gap(8.0).child(ui::button( + .child(ui::card( p, - "open-output", - "Open recordings folder", - false, - |_app: &mut App| { - crate::daemon::open_folder(&crate::daemon::recordings_dir()); - }, - ))) + Some("Audio"), + vec![ + ui::toggle_row( + p, + "rec-game", + "Game audio", + a.enabled && a.capture_game, + |app: &mut App| { + let on = + !(app.form.draft.audio.enabled && app.form.draft.audio.capture_game); + app.form.draft.audio.enabled = on; + app.form.draft.audio.capture_game = on; + app.form.dirty = true; + }, + ), + ui::toggle_row( + p, + "rec-mic", + "Microphone", + a.capture_mic, + |app: &mut App| { + app.form.draft.audio.capture_mic ^= true; + app.form.dirty = true; + }, + ), + mic_row(p, kit, layer, app, "rec-mic-device"), + ui::setting_row_with( + p, + "rec-apps", + "Applications", + &source_summary(&a.capture_apps), + ui::button(p, "edit-apps", "Edit", false, |app: &mut App| { + app.settings_tab = SettingsTab::Audio; + }), + ), + ], + )) +} + +/// The applications a clip takes sound from, as one line. +fn source_summary(sources: &[AudioSource]) -> String { + match sources.len() { + 0 => "None yet".to_string(), + 1 => sources[0].name.clone(), + 2 => format!("{} and {}", sources[0].name, sources[1].name), + n => format!("{} and {} more", sources[0].name, n - 1), + } } /// The microphone picker: the system default plus every input device the daemon reports. diff --git a/app/src/settings.rs b/app/src/settings.rs index 16560e6..c3160f9 100644 --- a/app/src/settings.rs +++ b/app/src/settings.rs @@ -116,7 +116,7 @@ impl EncoderPreset { | EncoderPreset::X264 { bitrate, .. } => *bitrate, }; match kind { - "amf" | "AMD AMF" => EncoderPreset::Amf { + "amf" | "AMD AMF" | "amd_amf_h264" => EncoderPreset::Amf { bitrate, quality: AmfQuality::Balanced, }, @@ -362,12 +362,17 @@ fn volume() -> u32 { } /// Split the daemon's `{ settings: {...} }` wrapper. +/// +/// Every field of [`Settings`] has a default, so a reply that carries no settings at all +/// (`{"success": true}`, the answer to `updateSettings`) would parse into the defaults and +/// quietly replace what the user has. The daemon always writes the `video` key, and a reply +/// without it is an error. pub fn from_response(data: &serde_json::Value) -> Result { - let inner = data - .get("settings") - .cloned() - .unwrap_or_else(|| data.clone()); - serde_json::from_value(inner).map_err(|e| format!("settings did not parse: {e}")) + let inner = data.get("settings").unwrap_or(data); + if inner.get("video").is_none() { + return Err("the daemon's reply carries no settings".to_string()); + } + serde_json::from_value(inner.clone()).map_err(|e| format!("settings did not parse: {e}")) } #[cfg(test)] @@ -406,6 +411,12 @@ mod tests { assert!(from_response(&bare).is_ok()); } + #[test] + fn a_reply_without_settings_is_an_error() { + let replied = json!({"success": true}); + assert!(from_response(&replied).is_err()); + } + #[test] fn an_encoder_keeps_its_bitrate_when_it_changes_kind() { let preset = EncoderPreset::X264 { @@ -417,6 +428,11 @@ mod tests { assert_eq!(nvenc.encoder_id(), "jim_nvenc"); assert_eq!(nvenc.bitrate(), 9000); assert_eq!(preset.with_kind("amf").encoder_id(), "amd_amf_h264"); + assert_eq!( + preset.with_kind("amd_amf_h264").encoder_id(), + "amd_amf_h264" + ); + assert_eq!(preset.with_kind("x264").encoder_id(), "x264"); } #[test]