From eff5fece913dea38032fae3f2131ccecd0317709 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 14 Oct 2026 21:51:46 +0200 Subject: [PATCH] feat(M4.3): wire profile and session audio choices Expose validated profile audio policy, translate it at the executable boundary, preserve session override precedence without persistence, and add a diagnostic backend probe command with server identity output. Tests: cargo test -p fader_master --test audio_policy --all-features; cargo test -p volume_lib --all-targets --all-features; cargo check --workspace --all-targets --no-default-features; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo run --locked -p volume_lib --example pulse_fixture -- probe auto (PipeWire 1.6.9) --- config/src/profile.rs | 25 ++++++++++++++++ fader_master/src/lib.rs | 30 +++++++++++++++++++ fader_master/tests/audio_policy.rs | 48 ++++++++++++++++++++++++++++++ sound/examples/pulse_fixture.rs | 47 +++++++++++++++++++++++++++-- 4 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 fader_master/tests/audio_policy.rs diff --git a/config/src/profile.rs b/config/src/profile.rs index 4046151..adfbf9c 100644 --- a/config/src/profile.rs +++ b/config/src/profile.rs @@ -244,6 +244,11 @@ impl Profile { self.raw.startup } + /// Validated audio adapter selection and preference order. + pub const fn audio(&self) -> &Audio { + &self.raw.audio + } + /// Applications in declared order. pub fn applications(&self) -> &[Application] { &self.raw.applications @@ -265,6 +270,26 @@ impl Profile { } } +impl Audio { + /// Configured automatic or manual adapter choice. + #[must_use] + pub const fn adapter(&self) -> Adapter { + self.adapter + } + + /// Concrete automatic-probe order; an empty list uses runtime defaults. + #[must_use] + pub fn preference(&self) -> &[Adapter] { + &self.preference + } + + /// Maximum configured gain for outbound writes. + #[must_use] + pub const fn max_gain(&self) -> f64 { + self.max_gain + } +} + fn validate_version(version: u16) -> Result<(), ConfigError> { if version == SCHEMA_VERSION { Ok(()) diff --git a/fader_master/src/lib.rs b/fader_master/src/lib.rs index 48da442..e851da9 100644 --- a/fader_master/src/lib.rs +++ b/fader_master/src/lib.rs @@ -10,6 +10,36 @@ use fader_core::{ }; use volume_lib::{AdapterQueue, Enqueue, FakeAdapter}; +use fader_config::{Adapter as ConfigAdapter, Profile}; +use volume_lib::{AdapterChoice, AudioBackend, SelectionPolicy, SelectionPolicyError}; + +/// Translates a validated profile and optional CLI/session override into the +/// runtime audio selection policy without mutating the profile. +pub fn audio_selection_policy( + profile: &Profile, + session_override: Option, +) -> Result { + let configured = map_adapter(profile.audio().adapter()); + let preference = profile + .audio() + .preference() + .iter() + .filter_map(|adapter| match map_adapter(*adapter) { + AdapterChoice::Auto => None, + AdapterChoice::Manual(backend) => Some(backend), + }) + .collect(); + SelectionPolicy::new(configured, preference, session_override) +} + +const fn map_adapter(adapter: ConfigAdapter) -> AdapterChoice { + match adapter { + ConfigAdapter::Auto => AdapterChoice::Auto, + ConfigAdapter::Pulse => AdapterChoice::Manual(AudioBackend::Pulse), + ConfigAdapter::Pipewire => AdapterChoice::Manual(AudioBackend::PipeWire), + } +} + /// Failure surfaced by the simulated server boundary. #[derive(Debug)] pub enum SliceError { diff --git a/fader_master/tests/audio_policy.rs b/fader_master/tests/audio_policy.rs new file mode 100644 index 0000000..ce85c34 --- /dev/null +++ b/fader_master/tests/audio_policy.rs @@ -0,0 +1,48 @@ +use fader_config::Profile; +use fader_master::audio_selection_policy; +use volume_lib::{AdapterChoice, AudioBackend}; + +fn profile(adapter: &str, preference: &str) -> Profile { + Profile::parse(&format!( + r#" +schema_version = 1 +id = "policy-test" +startup = "adopt" +new_stream = "adopt" + +[audio] +adapter = "{adapter}" +preference = {preference} +max_gain = 1.0 +"# + )) + .expect("valid policy fixture") +} + +#[test] +fn profile_auto_order_reaches_the_runtime_policy() { + let profile = profile("auto", r#"["pipewire", "pulse"]"#); + let policy = audio_selection_policy(&profile, None).unwrap(); + + assert_eq!(policy.effective_choice(), AdapterChoice::Auto); + assert_eq!( + policy.preference(), + &[AudioBackend::PipeWire, AudioBackend::Pulse] + ); +} + +#[test] +fn cli_override_wins_for_the_session_without_changing_the_profile() { + let profile = profile("pulse", "[]"); + let policy = audio_selection_policy( + &profile, + Some(AdapterChoice::Manual(AudioBackend::PipeWire)), + ) + .unwrap(); + + assert_eq!( + policy.effective_choice(), + AdapterChoice::Manual(AudioBackend::PipeWire) + ); + assert_eq!(profile.audio().adapter(), fader_config::Adapter::Pulse); +} diff --git a/sound/examples/pulse_fixture.rs b/sound/examples/pulse_fixture.rs index 71e31bd..108abf0 100644 --- a/sound/examples/pulse_fixture.rs +++ b/sound/examples/pulse_fixture.rs @@ -1,11 +1,18 @@ use std::{error::Error, io, sync::mpsc::RecvTimeoutError, time::Duration}; use fader_core::{BackendEpoch, Gain, MetadataField, Mute}; -use volume_lib::{PulseClient, PulseNotification, PulseStream, PulseWorker, WorkerEvent}; +use volume_lib::{ + AdapterChoice, AudioBackend, CompiledAdapterProbe, PulseClient, PulseNotification, PulseStream, + PulseWorker, SelectionPolicy, TargetRequirements, WorkerEvent, select_adapter, +}; fn main() -> Result<(), Box> { let mut arguments = std::env::args().skip(1); let command = arguments.next().unwrap_or_else(|| "list".to_owned()); + if command == "probe" { + let choice = arguments.next().unwrap_or_else(|| "auto".to_owned()); + return probe(&choice); + } if command == "worker-watch" { let seconds = arguments.next().as_deref().unwrap_or("15").parse::()?; return worker_watch(Duration::from_secs(seconds)); @@ -39,13 +46,49 @@ fn main() -> Result<(), Box> { } _ => { return Err(invalid_input( - "usage: pulse_fixture [list|watch [SECONDS]|worker-watch [SECONDS]|volume INDEX GAIN|mute INDEX on|off]", + "usage: pulse_fixture [probe [auto|pulse|pipewire|alsa-mixer]|list|watch [SECONDS]|worker-watch [SECONDS]|volume INDEX GAIN|mute INDEX on|off]", )); } } Ok(()) } +fn probe(value: &str) -> Result<(), Box> { + let choice = match value { + "auto" => AdapterChoice::Auto, + "pulse" => AdapterChoice::Manual(AudioBackend::Pulse), + "pipewire" => AdapterChoice::Manual(AudioBackend::PipeWire), + "alsa-mixer" => AdapterChoice::Manual(AudioBackend::AlsaMixer), + _ => { + return Err(invalid_input( + "adapter must be auto, pulse, pipewire, or alsa-mixer", + )); + } + }; + let policy = SelectionPolicy::new( + choice, + vec![AudioBackend::Pulse, AudioBackend::PipeWire], + None, + )?; + let selected = select_adapter( + &policy, + TargetRequirements::APPLICATION_MIXER, + &mut CompiledAdapterProbe::default(), + ) + .map_err(|error| invalid_input(&error.to_string()))?; + println!( + "selected={} server={:?} version={:?} capabilities={:?}", + selected.backend, + selected.server_name.as_deref(), + selected.server_version.as_deref(), + selected.capabilities + ); + for diagnostic in selected.diagnostics { + println!("probe={diagnostic:?}"); + } + Ok(()) +} + fn worker_watch(duration: Duration) -> Result<(), Box> { let mut worker = PulseWorker::spawn(BackendEpoch::new(1), 256); let deadline = std::time::Instant::now() + duration;