audio_streams_conformance_test/sys/
linux.rs

1// Copyright 2022 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 std::fmt;
6
7use audio_streams::StreamSourceGenerator;
8#[cfg(feature = "audio_cras")]
9use libcras::CrasClientType;
10#[cfg(feature = "audio_cras")]
11use libcras::CrasSocketType;
12#[cfg(feature = "audio_cras")]
13use libcras::CrasStreamSourceGenerator;
14use serde::Serialize;
15
16use crate::args::*;
17use crate::error::Error;
18
19#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
20pub enum StreamSource {
21    #[cfg(feature = "audio_cras")]
22    CRAS,
23}
24
25impl TryFrom<&str> for StreamSource {
26    type Error = Error;
27
28    fn try_from(s: &str) -> Result<Self, Self::Error> {
29        match s {
30            #[cfg(feature = "audio_cras")]
31            "cras" => Ok(StreamSource::CRAS),
32            _ => Err(Error::InvalidStreamSuorce(s.to_owned())),
33        }
34    }
35}
36
37impl fmt::Display for StreamSource {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        match self {
40            #[cfg(feature = "audio_cras")]
41            StreamSource::CRAS => write!(f, "cras"),
42            _ => write!(f, "unknow stream source"),
43        }
44    }
45}
46
47#[allow(unused_variables)]
48#[cfg(feature = "audio_cras")]
49fn create_cras_stream_source_generator(args: &Args) -> Box<dyn StreamSourceGenerator> {
50    Box::new(CrasStreamSourceGenerator::new(
51        false,
52        CrasClientType::CRAS_CLIENT_TYPE_TEST,
53        CrasSocketType::Legacy,
54    ))
55}
56
57#[allow(unused_variables)]
58pub(crate) fn create_stream_source_generator(
59    stream_source: StreamSource,
60    args: &Args,
61) -> Box<dyn StreamSourceGenerator> {
62    match stream_source {
63        #[cfg(feature = "audio_cras")]
64        StreamSource::CRAS => create_cras_stream_source_generator(args),
65    }
66}