From 5e8fd650e91f6afe7256ea69e83f30e522e1867d Mon Sep 17 00:00:00 2001 From: Stefano Garzarella Date: Fri, 19 May 2023 16:23:15 +0200 Subject: [PATCH] sound: add AudioBackend trait --- crates/sound/src/audio_backends.rs | 15 +++++++++++++++ crates/sound/src/lib.rs | 25 +++++++++++++++++++++++-- crates/sound/src/vhu_sound.rs | 8 +++++++- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 crates/sound/src/audio_backends.rs diff --git a/crates/sound/src/audio_backends.rs b/crates/sound/src/audio_backends.rs new file mode 100644 index 0000000..3c1f4ce --- /dev/null +++ b/crates/sound/src/audio_backends.rs @@ -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> { + match name.as_str() { + _ => Err(Error::AudioBackendNotSupported), + } +} diff --git a/crates/sound/src/lib.rs b/crates/sound/src/lib.rs index 325266e..3c73338 100644 --- a/crates/sound/src/lib.rs +++ b/crates/sound/src/lib.rs @@ -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 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>, +} + +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::::new()), ) .unwrap(); diff --git a/crates/sound/src/vhu_sound.rs b/crates/sound/src/vhu_sound.rs index 9a91b68..0e3434d 100644 --- a/crates/sound/src/vhu_sound.rs +++ b/crates/sound/src/vhu_sound.rs @@ -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>, virtio_cfg: VirtioSoundConfig, exit_event: EventFd, + _audio_backend: RwLock>, } 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), }) }