sound: extend SoundConfig with output/input enabled flags

Add output_enabled and input_enabled boolean fields to SoundConfig,
with has_output() and has_input() accessors. Wire up the --streams CLI
arg so that SoundConfig reflects which directions the user requested.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Davíð Steinn Geirsson 2026-03-17 19:14:40 +00:00
parent 7147a41ec0
commit 8050df66f0
3 changed files with 45 additions and 9 deletions

View file

@ -735,7 +735,7 @@ mod tests {
#[test]
fn test_sound_thread_success() {
crate::init_logger();
let config = SoundConfig::new(false, BackendType::Null);
let config = SoundConfig::new(false, BackendType::Null, true, true);
let chmaps = Arc::new(RwLock::new(vec![]));
let jacks = Arc::new(RwLock::new(vec![]));
@ -845,7 +845,7 @@ mod tests {
#[test]
fn test_sound_thread_failure() {
crate::init_logger();
let config = SoundConfig::new(false, BackendType::Null);
let config = SoundConfig::new(false, BackendType::Null, true, true);
let chmaps = Arc::new(RwLock::new(vec![]));
let jacks = Arc::new(RwLock::new(vec![]));
@ -934,7 +934,7 @@ mod tests {
fn test_sound_backend() {
crate::init_logger();
let test_dir = tempdir().expect("Could not create a temp test directory.");
let config = SoundConfig::new(false, BackendType::Null);
let config = SoundConfig::new(false, BackendType::Null, true, true);
let backend = VhostUserSoundBackend::new(config).expect("Could not create backend.");
assert_eq!(backend.num_queues(), NUM_QUEUES as usize);
@ -1012,7 +1012,7 @@ mod tests {
crate::init_logger();
let test_dir = tempdir().expect("Could not create a temp test directory.");
let config = SoundConfig::new(false, BackendType::Null);
let config = SoundConfig::new(false, BackendType::Null, true, true);
let backend = VhostUserSoundBackend::new(config);
let backend = backend.unwrap();

View file

@ -216,21 +216,40 @@ pub struct SoundConfig {
multi_thread: bool,
/// audio backend variant
audio_backend: BackendType,
/// enable output (playback) streams
output_enabled: bool,
/// enable input (capture) streams
input_enabled: bool,
}
impl SoundConfig {
/// Create a new instance of the SoundConfig struct, containing the
/// parameters to be fed into the sound-backend server.
pub const fn new(multi_thread: bool, audio_backend: BackendType) -> Self {
pub const fn new(
multi_thread: bool,
audio_backend: BackendType,
output_enabled: bool,
input_enabled: bool,
) -> Self {
Self {
multi_thread,
audio_backend,
output_enabled,
input_enabled,
}
}
pub const fn get_audio_backend(&self) -> BackendType {
self.audio_backend
}
pub const fn has_output(&self) -> bool {
self.output_enabled
}
pub const fn has_input(&self) -> bool {
self.input_enabled
}
}
pub struct IOMessage {
@ -332,7 +351,7 @@ mod tests {
fn test_sound_server() {
crate::init_logger();
let config = SoundConfig::new(false, BackendType::Null);
let config = SoundConfig::new(false, BackendType::Null, true, true);
let backend = Arc::new(VhostUserSoundBackend::new(config).unwrap());
let daemon = VhostUserDaemon::new(
@ -433,4 +452,19 @@ mod tests {
let stream_error = stream::Error::DescriptorReadFailed;
let _error: Error = stream_error.into();
}
#[test]
fn test_sound_config_stream_directions() {
let config = SoundConfig::new(false, BackendType::Null, true, true);
assert!(config.has_output());
assert!(config.has_input());
let config = SoundConfig::new(false, BackendType::Null, true, false);
assert!(config.has_output());
assert!(!config.has_input());
let config = SoundConfig::new(false, BackendType::Null, false, true);
assert!(!config.has_output());
assert!(config.has_input());
}
}

View file

@ -7,13 +7,15 @@ use std::os::unix::prelude::*;
use clap::Parser;
use vhost::vhost_user::Listener;
use vhost_device_sound::{args::SoundArgs, start_backend_server, SoundConfig};
use vhost_device_sound::{args, args::SoundArgs, start_backend_server, SoundConfig};
fn main() {
env_logger::init();
let args = SoundArgs::parse();
let config = SoundConfig::new(false, args.backend);
let has_output = args.streams.contains(&args::StreamDirection::Output);
let has_input = args.streams.contains(&args::StreamDirection::Input);
let config = SoundConfig::new(false, args.backend, has_output, has_input);
let mut listener = if let Some(fd) = args.socket_fd {
// SAFETY: user has assured us this is safe.
@ -181,7 +183,7 @@ mod tests {
backend_name,
]);
let config = SoundConfig::new(false, args.backend);
let config = SoundConfig::new(false, args.backend, true, true);
assert_eq!(config.get_audio_backend(), backend);
}
}