mirror of
https://github.com/rust-vmm/vhost-device.git
synced 2025-12-28 08:01:04 +00:00
sound: add AudioBackend trait
This commit is contained in:
parent
c3e2f6a4bc
commit
5e8fd650e9
15
crates/sound/src/audio_backends.rs
Normal file
15
crates/sound/src/audio_backends.rs
Normal file
@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
|
||||
|
||||
use crate::{Error, Result, SoundRequest};
|
||||
|
||||
pub trait AudioBackend {
|
||||
fn write(&self, req: &SoundRequest) -> Result<()>;
|
||||
|
||||
fn read(&self, req: &mut SoundRequest) -> Result<()>;
|
||||
}
|
||||
|
||||
pub fn allocate_audio_backend(name: String) -> Result<Box<dyn AudioBackend + Send + Sync>> {
|
||||
match name.as_str() {
|
||||
_ => Err(Error::AudioBackendNotSupported),
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
|
||||
|
||||
mod audio_backends;
|
||||
mod vhu_sound;
|
||||
mod virtio_sound;
|
||||
|
||||
@ -10,7 +11,7 @@ use log::{info, warn};
|
||||
use thiserror::Error as ThisError;
|
||||
use vhost::{vhost_user, vhost_user::Listener};
|
||||
use vhost_user_backend::VhostUserDaemon;
|
||||
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
|
||||
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap, VolatileSlice};
|
||||
|
||||
use crate::vhu_sound::VhostUserSoundBackend;
|
||||
|
||||
@ -25,6 +26,10 @@ pub enum Error {
|
||||
HandleUnknownEvent,
|
||||
#[error("Failed to create a new EventFd")]
|
||||
EventFdCreate(IoError),
|
||||
#[error("Request missing data buffer")]
|
||||
SoundReqMissingData,
|
||||
#[error("Audio backend not supported")]
|
||||
AudioBackendNotSupported,
|
||||
}
|
||||
|
||||
impl std::convert::From<Error> for IoError {
|
||||
@ -41,6 +46,8 @@ pub struct SoundConfig {
|
||||
socket: String,
|
||||
/// use multiple threads to hanlde the virtqueues
|
||||
multi_thread: bool,
|
||||
/// audio backend name
|
||||
audio_backend_name: String,
|
||||
}
|
||||
|
||||
impl SoundConfig {
|
||||
@ -50,6 +57,7 @@ impl SoundConfig {
|
||||
Self {
|
||||
socket,
|
||||
multi_thread,
|
||||
audio_backend_name: "null".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,6 +68,19 @@ impl SoundConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub type SoundBitmap = ();
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SoundRequest<'a> {
|
||||
data_slice: Option<VolatileSlice<'a, SoundBitmap>>,
|
||||
}
|
||||
|
||||
impl<'a> SoundRequest<'a> {
|
||||
pub fn data_slice(&self) -> Option<&VolatileSlice<'a, SoundBitmap>> {
|
||||
self.data_slice.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the public API through which an external program starts the
|
||||
/// vhost-user-sound backend server.
|
||||
pub fn start_backend_server(config: SoundConfig) {
|
||||
@ -69,7 +90,7 @@ pub fn start_backend_server(config: SoundConfig) {
|
||||
let mut daemon = VhostUserDaemon::new(
|
||||
String::from("vhost-user-sound"),
|
||||
backend.clone(),
|
||||
GuestMemoryAtomic::new(GuestMemoryMmap::new()),
|
||||
GuestMemoryAtomic::new(GuestMemoryMmap::<SoundBitmap>::new()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
|
||||
|
||||
use crate::audio_backends::{allocate_audio_backend, AudioBackend};
|
||||
use crate::virtio_sound::*;
|
||||
use crate::{Error, Result, SoundConfig};
|
||||
|
||||
use std::{io::Result as IoResult, sync::RwLock, u16, u32, u64, u8};
|
||||
use std::sync::RwLock;
|
||||
use std::{io::Result as IoResult, u16, u32, u64, u8};
|
||||
use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
|
||||
use vhost_user_backend::{VhostUserBackend, VringRwLock};
|
||||
use virtio_bindings::bindings::{
|
||||
@ -86,6 +88,7 @@ pub struct VhostUserSoundBackend {
|
||||
threads: Vec<RwLock<VhostUserSoundThread>>,
|
||||
virtio_cfg: VirtioSoundConfig,
|
||||
exit_event: EventFd,
|
||||
_audio_backend: RwLock<Box<dyn AudioBackend + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl VhostUserSoundBackend {
|
||||
@ -108,6 +111,8 @@ impl VhostUserSoundBackend {
|
||||
])?)]
|
||||
};
|
||||
|
||||
let audio_backend = allocate_audio_backend(config.audio_backend_name)?;
|
||||
|
||||
Ok(Self {
|
||||
threads,
|
||||
virtio_cfg: VirtioSoundConfig {
|
||||
@ -116,6 +121,7 @@ impl VhostUserSoundBackend {
|
||||
chmpas: 0.into(),
|
||||
},
|
||||
exit_event: EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdCreate)?,
|
||||
_audio_backend: RwLock::new(audio_backend),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user