• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use anyhow::Context;
6 use argh::FromArgs;
7 use cros_async::Executor;
8 
9 use crate::virtio::snd::parameters::Parameters;
10 use crate::virtio::vhost::user::device::listener::sys::VhostUserListener;
11 use crate::virtio::vhost::user::device::listener::VhostUserListenerTrait;
12 use crate::virtio::vhost::user::device::snd::SndBackend;
13 use crate::virtio::vhost::user::device::snd::SND_EXECUTOR;
14 
15 #[derive(FromArgs)]
16 #[argh(subcommand, name = "snd")]
17 /// Snd device
18 pub struct Options {
19     #[argh(option, arg_name = "PATH")]
20     /// path to bind a listening vhost-user socket
21     socket: String,
22     #[argh(
23         option,
24         arg_name = "CONFIG",
25         from_str_fn(snd_parameters_from_str),
26         default = "Default::default()",
27         long = "config"
28     )]
29     /// comma separated key=value pairs for setting up cras snd devices.
30     /// Possible key values:
31     /// capture - Enable audio capture. Default to false.
32     /// backend - Which backend to use for vhost-snd (null|cras).
33     /// client_type - Set specific client type for cras backend.
34     /// socket_type - Set socket type for cras backend.
35     /// num_output_devices - Set number of output PCM devices.
36     /// num_input_devices - Set number of input PCM devices.
37     /// num_output_streams - Set number of output PCM streams per device.
38     /// num_input_streams - Set number of input PCM streams per device.
39     /// Example: [capture=true,backend=BACKEND,
40     /// num_output_devices=1,num_input_devices=1,num_output_streams=1,num_input_streams=1]
41     params: Parameters,
42 }
43 
snd_parameters_from_str(input: &str) -> Result<Parameters, String>44 fn snd_parameters_from_str(input: &str) -> Result<Parameters, String> {
45     serde_keyvalue::from_key_values(input).map_err(|e| e.to_string())
46 }
47 
48 /// Starts a vhost-user snd device.
49 /// Returns an error if the given `args` is invalid or the device fails to run.
run_snd_device(opts: Options) -> anyhow::Result<()>50 pub fn run_snd_device(opts: Options) -> anyhow::Result<()> {
51     let snd_device = Box::new(SndBackend::new(opts.params)?);
52 
53     let ex = Executor::new().context("Failed to create executor")?;
54     let _ = SND_EXECUTOR.set(ex.clone());
55 
56     let listener = VhostUserListener::new_socket(&opts.socket, None)?;
57 
58     listener.run_device(ex, snd_device)
59 }
60