Path: blob/main/devices/src/virtio/vhost_user_backend/snd/sys/linux.rs
5394 views
// Copyright 2023 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34use anyhow::Context;5use argh::FromArgs;6use base::RawDescriptor;7use cros_async::Executor;89use crate::virtio::snd::parameters::Parameters;10use crate::virtio::vhost_user_backend::snd::SndBackend;11use crate::virtio::vhost_user_backend::BackendConnection;1213#[derive(FromArgs)]14#[argh(subcommand, name = "snd")]15/// Snd device16pub struct Options {17#[argh(option, arg_name = "PATH", hidden_help)]18/// deprecated - please use --socket-path instead19socket: Option<String>,20#[argh(option, arg_name = "PATH")]21/// path to the vhost-user socket to bind to.22/// If this flag is set, --fd cannot be specified.23socket_path: Option<String>,24#[argh(option, arg_name = "FD")]25/// file descriptor of a connected vhost-user socket.26/// If this flag is set, --socket-path cannot be specified.27fd: Option<RawDescriptor>,2829#[argh(30option,31arg_name = "CONFIG",32from_str_fn(snd_parameters_from_str),33default = "Default::default()",34long = "config"35)]36/// comma separated key=value pairs for setting up cras snd devices.37/// Possible key values:38/// capture - Enable audio capture. Default to false.39/// backend - Which backend to use for vhost-snd (null|cras).40/// client_type - Set specific client type for cras backend.41/// socket_type - Set socket type for cras backend.42/// num_output_devices - Set number of output PCM devices.43/// num_input_devices - Set number of input PCM devices.44/// num_output_streams - Set number of output PCM streams per device.45/// num_input_streams - Set number of input PCM streams per device.46/// Example: [capture=true,backend=BACKEND,47/// num_output_devices=1,num_input_devices=1,num_output_streams=1,num_input_streams=1]48params: Parameters,49}5051fn snd_parameters_from_str(input: &str) -> Result<Parameters, String> {52serde_keyvalue::from_key_values(input).map_err(|e| e.to_string())53}5455/// Starts a vhost-user snd device.56/// Returns an error if the given `args` is invalid or the device fails to run.57pub fn run_snd_device(opts: Options) -> anyhow::Result<()> {58let ex = Executor::new().context("Failed to create executor")?;59let snd_device = Box::new(SndBackend::new(&ex, opts.params, 0)?);6061let conn =62BackendConnection::from_opts(opts.socket.as_deref(), opts.socket_path.as_deref(), opts.fd)?;6364conn.run_device(ex, snd_device)65}666768