devices/virtio/vhost_user_backend/snd/sys/
linux.rs

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
5use anyhow::Context;
6use argh::FromArgs;
7use base::RawDescriptor;
8use cros_async::Executor;
9
10use crate::virtio::snd::parameters::Parameters;
11use crate::virtio::vhost_user_backend::snd::SndBackend;
12use crate::virtio::vhost_user_backend::BackendConnection;
13
14#[derive(FromArgs)]
15#[argh(subcommand, name = "snd")]
16/// Snd device
17pub struct Options {
18    #[argh(option, arg_name = "PATH", hidden_help)]
19    /// deprecated - please use --socket-path instead
20    socket: Option<String>,
21    #[argh(option, arg_name = "PATH")]
22    /// path to the vhost-user socket to bind to.
23    /// If this flag is set, --fd cannot be specified.
24    socket_path: Option<String>,
25    #[argh(option, arg_name = "FD")]
26    /// file descriptor of a connected vhost-user socket.
27    /// If this flag is set, --socket-path cannot be specified.
28    fd: Option<RawDescriptor>,
29
30    #[argh(
31        option,
32        arg_name = "CONFIG",
33        from_str_fn(snd_parameters_from_str),
34        default = "Default::default()",
35        long = "config"
36    )]
37    /// comma separated key=value pairs for setting up cras snd devices.
38    /// Possible key values:
39    /// capture - Enable audio capture. Default to false.
40    /// backend - Which backend to use for vhost-snd (null|cras).
41    /// client_type - Set specific client type for cras backend.
42    /// socket_type - Set socket type for cras backend.
43    /// num_output_devices - Set number of output PCM devices.
44    /// num_input_devices - Set number of input PCM devices.
45    /// num_output_streams - Set number of output PCM streams per device.
46    /// num_input_streams - Set number of input PCM streams per device.
47    /// Example: [capture=true,backend=BACKEND,
48    /// num_output_devices=1,num_input_devices=1,num_output_streams=1,num_input_streams=1]
49    params: Parameters,
50}
51
52fn snd_parameters_from_str(input: &str) -> Result<Parameters, String> {
53    serde_keyvalue::from_key_values(input).map_err(|e| e.to_string())
54}
55
56/// Starts a vhost-user snd device.
57/// Returns an error if the given `args` is invalid or the device fails to run.
58pub fn run_snd_device(opts: Options) -> anyhow::Result<()> {
59    let ex = Executor::new().context("Failed to create executor")?;
60    let snd_device = Box::new(SndBackend::new(&ex, opts.params, 0)?);
61
62    let conn =
63        BackendConnection::from_opts(opts.socket.as_deref(), opts.socket_path.as_deref(), opts.fd)?;
64
65    conn.run_device(ex, snd_device)
66}