audio_streams_conformance_test/
args.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;
6use std::str::FromStr;
7
8use argh::FromArgs;
9use audio_streams::*;
10use serde::Serialize;
11
12use super::error::Error;
13use super::sys::StreamSource as SysStreamSource;
14
15// maybe use StreamSourceGenerator directly
16#[derive(Copy, Clone, Debug, PartialEq, Serialize)]
17pub enum StreamSourceEnum {
18    NoopStream,
19    Sys(SysStreamSource),
20}
21
22impl fmt::Display for StreamSourceEnum {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self {
25            StreamSourceEnum::NoopStream => write!(f, "noop"),
26            StreamSourceEnum::Sys(stream_source) => stream_source.fmt(f),
27        }
28    }
29}
30
31impl FromStr for StreamSourceEnum {
32    type Err = Error;
33    fn from_str(s: &str) -> ::std::result::Result<StreamSourceEnum, Self::Err> {
34        match s {
35            "noop" => Ok(StreamSourceEnum::NoopStream),
36            _ => SysStreamSource::try_from(s).map(StreamSourceEnum::Sys),
37        }
38    }
39}
40
41fn default_channels() -> usize {
42    2
43}
44
45fn default_sample_format() -> SampleFormat {
46    SampleFormat::S16LE
47}
48
49fn default_rate() -> u32 {
50    48000
51}
52
53fn default_buffer_frames() -> usize {
54    240
55}
56
57fn default_iterations() -> usize {
58    10
59}
60
61fn default_stream_source() -> StreamSourceEnum {
62    StreamSourceEnum::NoopStream
63}
64
65fn parse_stream_source(value: &str) -> Result<StreamSourceEnum, String> {
66    StreamSourceEnum::from_str(value).map_err(|e| e.to_string())
67}
68
69fn parse_format(value: &str) -> Result<SampleFormat, String> {
70    SampleFormat::from_str(value).map_err(|e| e.to_string())
71}
72
73#[derive(Copy, Clone, Debug, FromArgs, Serialize)]
74/// audio_streams_conformance_test
75pub struct Args {
76    /// the StreamSource to use for playback. (default: noop).
77    #[argh(
78        option,
79        short = 'P',
80        default = "default_stream_source()",
81        from_str_fn(parse_stream_source)
82    )]
83    pub stream_source: StreamSourceEnum,
84    /// the channel numbers. (default: 2)
85    #[argh(option, short = 'c', default = "default_channels()")]
86    pub channels: usize,
87    /// format. Must be in [U8, S16_LE, S24_LE, S32_LE]. (default:S16_LE)
88    #[argh(
89        option,
90        short = 'f',
91        default = "default_sample_format()",
92        from_str_fn(parse_format)
93    )]
94    pub format: SampleFormat,
95    /// sample rate. (default: 48000)
96    #[argh(option, short = 'r', default = "default_rate()")]
97    pub rate: u32,
98    /// block buffer size (frames) of each write. (default: 240).
99    #[argh(option, short = 'b', default = "default_buffer_frames()")]
100    pub buffer_frames: usize,
101    /// the iterations to fill in the audio buffer. default: 10)
102    #[argh(option, default = "default_iterations()")]
103    pub iterations: usize,
104    /// whether or not to print in json format
105    #[argh(switch)]
106    pub json: bool,
107    /// whether or not to print the debug messages
108    #[argh(switch)]
109    pub debug: bool,
110}
111
112impl fmt::Display for Args {
113    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114        write!(
115            f,
116            r#"
117Playback Source: {:?}
118Channels: {}
119Format: {:?}
120Sample rate: {} frames/s
121Buffer size: {} frames
122Iterations: {}
123          "#,
124            self.stream_source,
125            self.channels,
126            self.format,
127            self.rate,
128            self.buffer_frames,
129            self.iterations
130        )
131    }
132}