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 use std::str::FromStr;
7
8 use argh::FromArgs;
9 use audio_streams::*;
10 use serde::Serialize;
11
12 use super::error::Error;
13 use super::sys::StreamSource as SysStreamSource;
14
15 // maybe use StreamSourceGenerator directly
16 #[derive(Copy, Clone, Debug, PartialEq, Serialize)]
17 pub enum StreamSourceEnum {
18 NoopStream,
19 Sys(SysStreamSource),
20 }
21
22 impl fmt::Display for StreamSourceEnum {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result23 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
31 impl FromStr for StreamSourceEnum {
32 type Err = Error;
from_str(s: &str) -> ::std::result::Result<StreamSourceEnum, Self::Err>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
default_channels() -> usize41 fn default_channels() -> usize {
42 2
43 }
44
default_sample_format() -> SampleFormat45 fn default_sample_format() -> SampleFormat {
46 SampleFormat::S16LE
47 }
48
default_rate() -> u3249 fn default_rate() -> u32 {
50 48000
51 }
52
default_buffer_frames() -> usize53 fn default_buffer_frames() -> usize {
54 240
55 }
56
default_iterations() -> usize57 fn default_iterations() -> usize {
58 10
59 }
60
default_stream_source() -> StreamSourceEnum61 fn default_stream_source() -> StreamSourceEnum {
62 StreamSourceEnum::NoopStream
63 }
64
parse_stream_source(value: &str) -> Result<StreamSourceEnum, String>65 fn parse_stream_source(value: &str) -> Result<StreamSourceEnum, String> {
66 StreamSourceEnum::from_str(value).map_err(|e| e.to_string())
67 }
68
parse_format(value: &str) -> Result<SampleFormat, String>69 fn 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
75 pub 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
112 impl fmt::Display for Args {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 write!(
115 f,
116 r#"
117 Playback Source: {:?}
118 Channels: {}
119 Format: {:?}
120 Sample rate: {} frames/s
121 Buffer size: {} frames
122 Iterations: {}
123 "#,
124 self.stream_source,
125 self.channels,
126 self.format,
127 self.rate,
128 self.buffer_frames,
129 self.iterations
130 )
131 }
132 }
133