• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 use std::fmt;
6 
7 use audio_streams::StreamSourceGenerator;
8 #[cfg(feature = "audio_cras")]
9 use libcras::CrasClientType;
10 #[cfg(feature = "audio_cras")]
11 use libcras::CrasSocketType;
12 #[cfg(feature = "audio_cras")]
13 use libcras::CrasStreamSourceGenerator;
14 use serde::Serialize;
15 
16 use crate::args::*;
17 use crate::error::Error;
18 
19 #[derive(Debug, Clone, Copy, PartialEq, Serialize)]
20 pub enum StreamSource {
21     #[cfg(feature = "audio_cras")]
22     CRAS,
23 }
24 
25 impl TryFrom<&str> for StreamSource {
26     type Error = Error;
27 
try_from(s: &str) -> Result<Self, Self::Error>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 
37 impl fmt::Display for StreamSource {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result38     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")]
create_cras_stream_source_generator(args: &Args) -> Box<dyn StreamSourceGenerator>49 fn 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)]
create_stream_source_generator( stream_source: StreamSource, args: &Args, ) -> Box<dyn StreamSourceGenerator>58 pub(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 }
67