fuchsia_audio/
format_set.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::format::{Format, SampleSize, SampleType};
use itertools::iproduct;
use std::num::NonZeroU32;
use {fidl_fuchsia_audio_device as fadevice, fidl_fuchsia_hardware_audio as fhaudio};

#[derive(Default, Debug, Clone, PartialEq)]
pub struct PcmFormatSet {
    pub channel_sets: Vec<ChannelSet>,
    pub sample_types: Vec<SampleType>,
    pub frame_rates: Vec<u32>,
}

impl PcmFormatSet {
    pub fn supports(&self, format: &Format) -> bool {
        let channels_ok = self
            .channel_sets
            .iter()
            .any(|channel_set| channel_set.channels() as u32 == format.channels);
        let sample_type_ok = self.sample_types.contains(&format.sample_type);
        let frame_rate_ok = self.frame_rates.contains(&format.frames_per_second);
        channels_ok && sample_type_ok && frame_rate_ok
    }
}

impl TryFrom<fhaudio::PcmSupportedFormats> for PcmFormatSet {
    type Error = String;

    fn try_from(value: fhaudio::PcmSupportedFormats) -> Result<Self, Self::Error> {
        let channel_sets = value.channel_sets.ok_or_else(|| "missing channel_sets".to_string())?;
        let sample_formats =
            value.sample_formats.ok_or_else(|| "missing sample_formats".to_string())?;
        let bytes_per_sample =
            value.bytes_per_sample.ok_or_else(|| "missing bytes_per_sample".to_string())?;
        let valid_bits_per_sample = value
            .valid_bits_per_sample
            .ok_or_else(|| "missing valid_bits_per_sample".to_string())?;
        let frame_rates = value.frame_rates.ok_or_else(|| "missing frame_rates".to_string())?;

        let channel_sets: Vec<ChannelSet> =
            channel_sets.into_iter().map(ChannelSet::try_from).collect::<Result<Vec<_>, _>>()?;

        // Convert each combination of values from `bytes_per_sample` and `valid_bits_per_sample`
        // into a [SampleSize], ignoring any invalid combinations, e.g. when:
        //     * A value is zero, so [NonZeroU32::new] returns `None`
        //     * Valid bits per sample is greater than bytes (total bits) per sample, so
        //       [SampleSize::from_partial_bits] returns `None`.
        let sample_sizes = iproduct!(bytes_per_sample.iter(), valid_bits_per_sample.iter())
            .filter_map(|(bytes, valid_bits)| {
                let total_bits = NonZeroU32::new(*bytes as u32 * 8)?;
                let valid_bits = NonZeroU32::new(*valid_bits as u32)?;
                SampleSize::from_partial_bits(valid_bits, total_bits)
            });
        // Convert each combination of sample format and [SampleSize] into a [SampleType].
        let sample_types: Vec<SampleType> = iproduct!(sample_formats, sample_sizes)
            .map(SampleType::try_from)
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Self { channel_sets, sample_types, frame_rates })
    }
}

impl TryFrom<fhaudio::SupportedFormats> for PcmFormatSet {
    type Error = String;

    fn try_from(value: fhaudio::SupportedFormats) -> Result<Self, Self::Error> {
        let pcm_supported_formats = value
            .pcm_supported_formats
            .ok_or_else(|| "missing pcm_supported_formats".to_string())?;
        Self::try_from(pcm_supported_formats)
    }
}

impl From<PcmFormatSet> for fhaudio::PcmSupportedFormats {
    fn from(value: PcmFormatSet) -> Self {
        let channel_sets = value.channel_sets.into_iter().map(Into::into).collect();
        let sample_formats = value.sample_types.iter().copied().map(Into::into).collect();

        let sample_sizes: Vec<SampleSize> =
            value.sample_types.iter().map(|sample_type| sample_type.size()).collect();

        let mut bytes_per_sample: Vec<u8> =
            sample_sizes.iter().map(|sample_size| sample_size.total_bytes().get() as u8).collect();
        bytes_per_sample.sort();

        let mut valid_bits_per_sample: Vec<u8> =
            sample_sizes.iter().map(|sample_size| sample_size.valid_bits().get() as u8).collect();
        valid_bits_per_sample.sort();

        let mut frame_rates = value.frame_rates;
        frame_rates.sort();

        Self {
            channel_sets: Some(channel_sets),
            sample_formats: Some(sample_formats),
            bytes_per_sample: Some(bytes_per_sample),
            valid_bits_per_sample: Some(valid_bits_per_sample),
            frame_rates: Some(frame_rates),
            ..Default::default()
        }
    }
}

impl From<PcmFormatSet> for fhaudio::SupportedFormats {
    fn from(value: PcmFormatSet) -> Self {
        Self { pcm_supported_formats: Some(value.into()), ..Default::default() }
    }
}

impl TryFrom<fadevice::PcmFormatSet> for PcmFormatSet {
    type Error = String;

    fn try_from(value: fadevice::PcmFormatSet) -> Result<Self, Self::Error> {
        let channel_sets = value.channel_sets.ok_or_else(|| "missing channel_sets".to_string())?;
        let sample_types = value.sample_types.ok_or_else(|| "missing sample_types".to_string())?;
        let frame_rates = value.frame_rates.ok_or_else(|| "missing frame_rates".to_string())?;

        let channel_sets: Vec<ChannelSet> =
            channel_sets.into_iter().map(ChannelSet::try_from).collect::<Result<Vec<_>, _>>()?;
        let sample_types: Vec<SampleType> =
            sample_types.into_iter().map(SampleType::try_from).collect::<Result<Vec<_>, _>>()?;

        Ok(Self { channel_sets, sample_types, frame_rates })
    }
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ChannelAttributes {
    /// Minimum frequency, in hertz.
    pub min_frequency: Option<u32>,
    /// Maximum frequency, in hertz.
    pub max_frequency: Option<u32>,
}

impl From<fadevice::ChannelAttributes> for ChannelAttributes {
    fn from(value: fadevice::ChannelAttributes) -> Self {
        Self { min_frequency: value.min_frequency, max_frequency: value.max_frequency }
    }
}

impl From<ChannelAttributes> for fadevice::ChannelAttributes {
    fn from(value: ChannelAttributes) -> Self {
        fadevice::ChannelAttributes {
            min_frequency: value.min_frequency,
            max_frequency: value.max_frequency,
            ..Default::default()
        }
    }
}

impl From<fhaudio::ChannelAttributes> for ChannelAttributes {
    fn from(value: fhaudio::ChannelAttributes) -> Self {
        Self { min_frequency: value.min_frequency, max_frequency: value.max_frequency }
    }
}

impl From<ChannelAttributes> for fhaudio::ChannelAttributes {
    fn from(value: ChannelAttributes) -> Self {
        Self {
            min_frequency: value.min_frequency,
            max_frequency: value.max_frequency,
            ..Default::default()
        }
    }
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ChannelSet {
    pub attributes: Vec<ChannelAttributes>,
}

impl ChannelSet {
    /// Returns the number of channels supported by this ChannelSet.
    pub fn channels(&self) -> usize {
        self.attributes.len()
    }
}

impl TryFrom<Vec<ChannelAttributes>> for ChannelSet {
    type Error = String;

    fn try_from(value: Vec<ChannelAttributes>) -> Result<Self, Self::Error> {
        if value.is_empty() {
            return Err("channel attributes must contain at least one entry".to_string());
        }
        Ok(Self { attributes: value })
    }
}

impl TryFrom<fadevice::ChannelSet> for ChannelSet {
    type Error = String;

    fn try_from(value: fadevice::ChannelSet) -> Result<Self, Self::Error> {
        let attributes: Vec<ChannelAttributes> = value
            .attributes
            .ok_or_else(|| "missing attributes".to_string())?
            .into_iter()
            .map(Into::into)
            .collect();
        Self::try_from(attributes)
    }
}

impl From<ChannelSet> for fadevice::ChannelSet {
    fn from(value: ChannelSet) -> Self {
        Self {
            attributes: Some(value.attributes.into_iter().map(Into::into).collect()),
            ..Default::default()
        }
    }
}

impl TryFrom<fhaudio::ChannelSet> for ChannelSet {
    type Error = String;

    fn try_from(value: fhaudio::ChannelSet) -> Result<Self, Self::Error> {
        let attributes: Vec<ChannelAttributes> = value
            .attributes
            .ok_or_else(|| "missing attributes".to_string())?
            .into_iter()
            .map(Into::into)
            .collect();
        Self::try_from(attributes)
    }
}

impl From<ChannelSet> for fhaudio::ChannelSet {
    fn from(value: ChannelSet) -> Self {
        Self {
            attributes: Some(value.attributes.into_iter().map(Into::into).collect()),
            ..Default::default()
        }
    }
}

#[cfg(test)]
pub mod test {
    use super::*;
    use test_case::test_case;

    #[test_case(
        PcmFormatSet {
            channel_sets: vec![ChannelSet::try_from(vec![
                ChannelAttributes::default(),
                ChannelAttributes::default(),
            ])
            .unwrap()],
            sample_types: vec![SampleType::Uint8],
            frame_rates: vec![48000],
        };
        "exact"
    )]
    #[test_case(
        PcmFormatSet {
            channel_sets: vec![
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                ]).unwrap(),
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                    ChannelAttributes::default(),
                ]).unwrap(),
            ],
            sample_types: vec![SampleType::Uint8, SampleType::Int16],
            frame_rates: vec![16000, 22050, 32000, 44100, 48000, 88200, 96000],
        };
        "multiple"
    )]
    fn test_pcm_format_set_supports(format_set: PcmFormatSet) {
        let format =
            Format { frames_per_second: 48000, sample_type: SampleType::Uint8, channels: 2 };
        assert!(format_set.supports(&format));
    }

    #[test_case(PcmFormatSet::default(); "empty set")]
    #[test_case(
        PcmFormatSet {
            // No channel set with two channels
            channel_sets: vec![
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                ]).unwrap(),
            ],
            sample_types: vec![SampleType::Uint8, SampleType::Int16],
            frame_rates: vec![16000, 22050, 32000, 44100, 48000, 88200, 96000],
        };
        "missing channel set"
    )]
    #[test_case(
        PcmFormatSet {
            channel_sets: vec![
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                ]).unwrap(),
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                    ChannelAttributes::default(),
                ]).unwrap(),
            ],
            // No SampleType:Uint8
            sample_types: vec![SampleType::Int16],
            frame_rates: vec![16000, 22050, 32000, 44100, 48000, 88200, 96000],
        };
        "missing sample type"
    )]
    #[test_case(
        PcmFormatSet {
            channel_sets: vec![
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                ]).unwrap(),
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                    ChannelAttributes::default(),
                ]).unwrap(),
            ],
            sample_types: vec![SampleType::Uint8, SampleType::Int16],
            // No 48000
            frame_rates: vec![16000, 22050, 32000, 44100, 88200, 96000],
        };
        "missing frame rate"
    )]
    fn test_pcm_format_set_does_not_support(format_set: PcmFormatSet) {
        let format =
            Format { frames_per_second: 48000, sample_type: SampleType::Uint8, channels: 2 };
        assert!(!format_set.supports(&format));
    }

    #[test]
    fn test_pcm_format_set_from_hw_supported_formats() {
        let hw_supported_formats = fhaudio::SupportedFormats {
            pcm_supported_formats: Some(fhaudio::PcmSupportedFormats {
                channel_sets: Some(vec![
                    fhaudio::ChannelSet {
                        attributes: Some(vec![fhaudio::ChannelAttributes::default()]),
                        ..Default::default()
                    },
                    fhaudio::ChannelSet {
                        attributes: Some(vec![
                            fhaudio::ChannelAttributes::default(),
                            fhaudio::ChannelAttributes::default(),
                        ]),
                        ..Default::default()
                    },
                ]),
                sample_formats: Some(vec![fhaudio::SampleFormat::PcmSigned]),
                bytes_per_sample: Some(vec![2]),
                valid_bits_per_sample: Some(vec![16]),
                frame_rates: Some(vec![16000, 22050, 32000, 44100, 48000, 88200, 96000]),
                ..Default::default()
            }),
            ..Default::default()
        };
        let format_set = PcmFormatSet {
            channel_sets: vec![
                ChannelSet::try_from(vec![ChannelAttributes::default()]).unwrap(),
                ChannelSet::try_from(vec![
                    ChannelAttributes::default(),
                    ChannelAttributes::default(),
                ])
                .unwrap(),
            ],
            sample_types: vec![SampleType::Int16],
            frame_rates: vec![16000, 22050, 32000, 44100, 48000, 88200, 96000],
        };
        assert_eq!(format_set, PcmFormatSet::try_from(hw_supported_formats).unwrap());
    }
}