diff --git a/devices/src/pci/ac97.rs b/devices/src/pci/ac97.rs index e03e918f25..bd6f36410d 100644 --- a/devices/src/pci/ac97.rs +++ b/devices/src/pci/ac97.rs @@ -82,6 +82,8 @@ pub struct Ac97Parameters { pub vios_server_path: Option, #[cfg(feature = "audio_cras")] client_type: Option, + #[cfg(feature = "audio_cras")] + socket_type: Option, } impl Ac97Parameters { @@ -93,6 +95,18 @@ impl Ac97Parameters { self.client_type = Some(client_type.parse()?); Ok(()) } + + /// Set CRAS socket type by given socket type string. + /// + /// `socket_type` - The socket type string. + #[cfg(feature = "audio_cras")] + pub fn set_socket_type( + &mut self, + socket_type: &str, + ) -> std::result::Result<(), libcras::Error> { + self.socket_type = Some(socket_type.parse()?); + Ok(()) + } } pub struct Ac97Dev { @@ -168,7 +182,7 @@ impl Ac97Dev { #[cfg(feature = "audio_cras")] fn create_cras_audio_device(params: Ac97Parameters, mem: GuestMemory) -> Result { let mut server = Box::new( - CrasClient::with_type(CrasSocketType::Unified) + CrasClient::with_type(params.socket_type.unwrap_or(CrasSocketType::Unified)) .map_err(pci_device::Error::CreateCrasClientFailed)?, ); server.set_client_type( diff --git a/src/main.rs b/src/main.rs index 3ecb80d991..5c71afc6a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -546,6 +546,15 @@ fn parse_ac97_options(s: &str) -> argument::Result { expected: e.to_string(), })?; } + #[cfg(feature = "audio_cras")] + "socket_type" => { + ac97_params + .set_socket_type(v) + .map_err(|e| argument::Error::InvalidValue { + value: v.to_string(), + expected: e.to_string(), + })?; + } #[cfg(any(target_os = "linux", target_os = "android"))] "server" => { ac97_params.vios_server_path = @@ -2095,6 +2104,7 @@ fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> { capture - Enable audio capture capture_effects - | separated effects to be enabled for recording. The only supported effect value now is EchoCancellation or aec. client_type - Set specific client type for cras backend. + socket_type - Set specific socket type for cras backend. server - The to the VIOS server (unix socket)."), #[cfg(feature = "audio")] Argument::value("sound", "[PATH]", "Path to the VioS server socket for setting up virtio-snd devices."), @@ -2955,6 +2965,13 @@ mod tests { .expect_err("parse should have failed"); } + #[cfg(feature = "audio_cras")] + #[test] + fn parse_ac97_socket_type() { + parse_ac97_options("socket_type=unified").expect("parse should have succeded"); + parse_ac97_options("socket_type=legacy").expect("parse should have succeded"); + } + #[cfg(feature = "audio")] #[test] fn parse_ac97_vios_valid() {