device_virtio_snd/
lib.rs

1// Copyright 2020 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
5pub mod common;
6pub mod constants;
7pub mod layout;
8pub mod parameters;
9pub mod sys;
10
11pub mod common_backend;
12pub mod file_backend;
13pub mod null_backend;
14pub mod vhost_user;
15
16pub use vhost_user::run_snd_device;
17pub use vhost_user::Options as SndOptions;
18
19cfg_if::cfg_if! {
20    if #[cfg(any(target_os = "android", target_os = "linux"))] {
21        pub mod vios_backend;
22
23        pub use vios_backend::new_sound;
24        pub use vios_backend::SoundError;
25    }
26}
27
28#[cfg(any(target_os = "android", target_os = "linux"))]
29use anyhow::Context;
30use devices::virtio::VirtioDevice;
31use devices::VirtioDeviceArgs;
32use devices::VirtioDeviceModule;
33use serde::Deserialize;
34use serde::Serialize;
35#[cfg(any(target_os = "android", target_os = "linux"))]
36use vm_control::AnyControlTube;
37
38use self::parameters::Parameters;
39
40#[derive(Serialize, Deserialize)]
41pub struct VirtioSndModule {
42    pub params: Parameters,
43}
44
45impl VirtioSndModule {
46    pub fn new(params: Parameters) -> Self {
47        Self { params }
48    }
49}
50
51impl VirtioDeviceModule for VirtioSndModule {
52    fn sort_name(&self) -> &'static str {
53        "snd"
54    }
55
56    fn create(&self, args: &mut VirtioDeviceArgs<'_>) -> anyhow::Result<Box<dyn VirtioDevice>> {
57        #[cfg(any(target_os = "android", target_os = "linux"))]
58        {
59            let (snd_host_tube, snd_device_tube) =
60                base::Tube::pair().context("failed to create tube for snd")?;
61
62            let dev = common_backend::VirtioSnd::new(
63                devices::virtio::base_features(args.protection_type),
64                self.params.clone(),
65                snd_device_tube,
66            )
67            .context("failed to create cras sound device")?;
68
69            (args.add_control_tube)(AnyControlTube::Snd(snd_host_tube));
70
71            Ok(Box::new(dev))
72        }
73        #[cfg(windows)]
74        {
75            let _ = args;
76            anyhow::bail!("snd device not supported on Windows")
77        }
78    }
79
80    #[cfg(any(target_os = "android", target_os = "linux"))]
81    fn create_jail(
82        &self,
83        jail_config: Option<&jail::JailConfig>,
84    ) -> anyhow::Result<Option<minijail::Minijail>> {
85        let Some(jail_config) = jail_config else {
86            return Ok(None);
87        };
88        sys::linux::create_jail(&self.params, jail_config)
89    }
90}