diff --git a/crates/sound/Cargo.toml b/crates/sound/Cargo.toml index c387a36..0d36703 100644 --- a/crates/sound/Cargo.toml +++ b/crates/sound/Cargo.toml @@ -9,6 +9,10 @@ keywords = ["vhost", "sound", "virtio-sound"] license = "Apache-2.0 OR BSD-3-Clause" edition = "2018" +[features] +default = ["null-backend"] +null-backend = [] + [dependencies] clap = { version = "4.1", features = ["derive"] } env_logger = "0.10" diff --git a/crates/sound/src/audio_backends.rs b/crates/sound/src/audio_backends.rs index 3c1f4ce..6a14b82 100644 --- a/crates/sound/src/audio_backends.rs +++ b/crates/sound/src/audio_backends.rs @@ -1,5 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause +#[cfg(feature = "null-backend")] +mod null; + +#[cfg(feature = "null-backend")] +use self::null::NullBackend; use crate::{Error, Result, SoundRequest}; pub trait AudioBackend { @@ -10,6 +15,8 @@ pub trait AudioBackend { pub fn allocate_audio_backend(name: String) -> Result> { match name.as_str() { + #[cfg(feature = "null-backend")] + "null" => Ok(Box::new(NullBackend::new())), _ => Err(Error::AudioBackendNotSupported), } } diff --git a/crates/sound/src/audio_backends/null.rs b/crates/sound/src/audio_backends/null.rs new file mode 100644 index 0000000..e12c36d --- /dev/null +++ b/crates/sound/src/audio_backends/null.rs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause + +use super::AudioBackend; +use crate::{Error, Result, SoundRequest}; + +pub struct NullBackend {} + +impl NullBackend { + pub fn new() -> Self { + NullBackend {} + } +} + +impl AudioBackend for NullBackend { + fn write(&self, _req: &SoundRequest) -> Result<()> { + Ok(()) + } + + fn read(&self, req: &mut SoundRequest) -> Result<()> { + let buf = req.data_slice().ok_or(Error::SoundReqMissingData)?; + let zero_mem = vec![0u8; buf.len()]; + + buf.copy_from(&zero_mem); + + Ok(()) + } +}