sound: add NullBackend skeleton

This commit is contained in:
Stefano Garzarella 2023-05-19 16:23:38 +02:00
parent 5e8fd650e9
commit 82660840ca
3 changed files with 38 additions and 0 deletions

View File

@ -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"

View File

@ -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<Box<dyn AudioBackend + Send + Sync>> {
match name.as_str() {
#[cfg(feature = "null-backend")]
"null" => Ok(Box::new(NullBackend::new())),
_ => Err(Error::AudioBackendNotSupported),
}
}

View File

@ -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(())
}
}