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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::num::ParseIntError;
use std::str::ParseBoolError;

use audio_streams::StreamEffect;
#[cfg(all(unix, feature = "audio_cras"))]
use libcras::CrasClientType;
#[cfg(all(unix, feature = "audio_cras"))]
use libcras::CrasSocketType;
#[cfg(all(unix, feature = "audio_cras"))]
use libcras::CrasStreamType;
use serde::Deserialize;
use serde::Serialize;
use serde_keyvalue::FromKeyValues;
use thiserror::Error as ThisError;

use crate::virtio::snd::constants::*;
use crate::virtio::snd::layout::*;
use crate::virtio::snd::sys::StreamSourceBackend as SysStreamSourceBackend;

#[derive(ThisError, Debug, PartialEq, Eq)]
pub enum Error {
    /// Unknown snd parameter value.
    #[error("Invalid snd parameter value ({0}): {1}")]
    InvalidParameterValue(String, String),
    /// Failed to parse bool value.
    #[error("Invalid bool value: {0}")]
    InvalidBoolValue(ParseBoolError),
    /// Failed to parse int value.
    #[error("Invalid int value: {0}")]
    InvalidIntValue(ParseIntError),
    // Invalid backend.
    #[error("Backend is not implemented")]
    InvalidBackend,
    /// Failed to parse parameters.
    #[error("Invalid snd parameter: {0}")]
    UnknownParameter(String),
    /// Invalid PCM device config index. Happens when the length of PCM device config is less than
    /// the number of PCM devices.
    #[error("Invalid PCM device config index: {0}")]
    InvalidPCMDeviceConfigIndex(usize),
    /// Invalid PCM info direction (VIRTIO_SND_D_OUTPUT = 0, VIRTIO_SND_D_INPUT = 1)
    #[error("Invalid PCM Info direction: {0}")]
    InvalidPCMInfoDirection(u8),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(into = "String", try_from = "&str")]
pub enum StreamSourceBackend {
    NULL,
    FILE,
    Sys(SysStreamSourceBackend),
}

// Implemented to make backend serialization possible, since we deserialize from str.
impl From<StreamSourceBackend> for String {
    fn from(backend: StreamSourceBackend) -> Self {
        match backend {
            StreamSourceBackend::NULL => "null".to_owned(),
            StreamSourceBackend::FILE => "file".to_owned(),
            StreamSourceBackend::Sys(sys_backend) => sys_backend.into(),
        }
    }
}

impl TryFrom<&str> for StreamSourceBackend {
    type Error = Error;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s {
            "null" => Ok(StreamSourceBackend::NULL),
            "file" => Ok(StreamSourceBackend::FILE),
            _ => SysStreamSourceBackend::try_from(s).map(StreamSourceBackend::Sys),
        }
    }
}

/// Holds the parameters for each PCM device
#[derive(Debug, Clone, Default, Deserialize, Serialize, FromKeyValues, PartialEq, Eq)]
#[serde(deny_unknown_fields, default)]
pub struct PCMDeviceParameters {
    #[cfg(all(unix, feature = "audio_cras"))]
    pub client_type: Option<CrasClientType>,
    #[cfg(all(unix, feature = "audio_cras"))]
    pub stream_type: Option<CrasStreamType>,
    pub effects: Option<Vec<StreamEffect>>,
}

/// Holds the parameters for a cras sound device
#[derive(Debug, Clone, Deserialize, Serialize, FromKeyValues)]
#[serde(deny_unknown_fields, default)]
pub struct Parameters {
    pub capture: bool,
    pub num_output_devices: u32,
    pub num_input_devices: u32,
    pub backend: StreamSourceBackend,
    pub num_output_streams: u32,
    pub num_input_streams: u32,
    pub playback_path: String,
    pub playback_size: usize,
    #[cfg(all(unix, feature = "audio_cras"))]
    #[serde(deserialize_with = "libcras::deserialize_cras_client_type")]
    pub client_type: CrasClientType,
    #[cfg(all(unix, feature = "audio_cras"))]
    pub socket_type: CrasSocketType,
    pub output_device_config: Vec<PCMDeviceParameters>,
    pub input_device_config: Vec<PCMDeviceParameters>,
    pub card_index: usize,
}

impl Default for Parameters {
    fn default() -> Self {
        Parameters {
            capture: false,
            num_output_devices: 1,
            num_input_devices: 1,
            backend: StreamSourceBackend::NULL,
            num_output_streams: 1,
            num_input_streams: 1,
            playback_path: "".to_string(),
            playback_size: 0,
            #[cfg(all(unix, feature = "audio_cras"))]
            client_type: CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            #[cfg(all(unix, feature = "audio_cras"))]
            socket_type: CrasSocketType::Unified,
            output_device_config: vec![],
            input_device_config: vec![],
            card_index: 0,
        }
    }
}

impl Parameters {
    pub(crate) fn get_total_output_streams(&self) -> u32 {
        self.num_output_devices * self.num_output_streams
    }

    pub(crate) fn get_total_input_streams(&self) -> u32 {
        self.num_input_devices * self.num_input_streams
    }

    pub(crate) fn get_total_streams(&self) -> u32 {
        self.get_total_output_streams() + self.get_total_input_streams()
    }

    #[allow(dead_code)]
    pub(crate) fn get_device_params(
        &self,
        pcm_info: &virtio_snd_pcm_info,
    ) -> Result<PCMDeviceParameters, Error> {
        let device_config = match pcm_info.direction {
            VIRTIO_SND_D_OUTPUT => &self.output_device_config,
            VIRTIO_SND_D_INPUT => &self.input_device_config,
            _ => return Err(Error::InvalidPCMInfoDirection(pcm_info.direction)),
        };
        let device_idx = u32::from(pcm_info.hdr.hda_fn_nid) as usize;
        device_config
            .get(device_idx)
            .cloned()
            .ok_or(Error::InvalidPCMDeviceConfigIndex(device_idx))
    }
}

#[cfg(test)]
#[allow(clippy::needless_update)]
mod tests {
    use super::*;

    fn check_failure(s: &str) {
        serde_keyvalue::from_key_values::<Parameters>(s).expect_err("parse should have failed");
    }

    fn check_success(
        s: &str,
        capture: bool,
        backend: StreamSourceBackend,
        num_output_devices: u32,
        num_input_devices: u32,
        num_output_streams: u32,
        num_input_streams: u32,
        output_device_config: Vec<PCMDeviceParameters>,
        input_device_config: Vec<PCMDeviceParameters>,
    ) {
        let params: Parameters =
            serde_keyvalue::from_key_values(s).expect("parse should have succeded");
        assert_eq!(params.capture, capture);
        assert_eq!(params.backend, backend);
        assert_eq!(params.num_output_devices, num_output_devices);
        assert_eq!(params.num_input_devices, num_input_devices);
        assert_eq!(params.num_output_streams, num_output_streams);
        assert_eq!(params.num_input_streams, num_input_streams);
        assert_eq!(params.output_device_config, output_device_config);
        assert_eq!(params.input_device_config, input_device_config);
    }

    #[test]
    fn parameters_fromstr() {
        check_failure("capture=none");
        check_success(
            "capture=false",
            false,
            StreamSourceBackend::NULL,
            1,
            1,
            1,
            1,
            vec![],
            vec![],
        );
        check_success(
            "capture=true,num_output_streams=2,num_input_streams=3",
            true,
            StreamSourceBackend::NULL,
            1,
            1,
            2,
            3,
            vec![],
            vec![],
        );
        check_success(
            "capture=true,num_output_devices=3,num_input_devices=2",
            true,
            StreamSourceBackend::NULL,
            3,
            2,
            1,
            1,
            vec![],
            vec![],
        );
        check_success(
            "capture=true,num_output_devices=2,num_input_devices=3,\
            num_output_streams=3,num_input_streams=2",
            true,
            StreamSourceBackend::NULL,
            2,
            3,
            3,
            2,
            vec![],
            vec![],
        );
        check_success(
            "capture=true,backend=null,num_output_devices=2,num_input_devices=3,\
            num_output_streams=3,num_input_streams=2",
            true,
            StreamSourceBackend::NULL,
            2,
            3,
            3,
            2,
            vec![],
            vec![],
        );
        check_success(
            "output_device_config=[[effects=[aec]],[]]",
            false,
            StreamSourceBackend::NULL,
            1,
            1,
            1,
            1,
            vec![
                PCMDeviceParameters {
                    effects: Some(vec![StreamEffect::EchoCancellation]),
                    ..Default::default()
                },
                Default::default(),
            ],
            vec![],
        );
        check_success(
            "input_device_config=[[effects=[aec]],[]]",
            false,
            StreamSourceBackend::NULL,
            1,
            1,
            1,
            1,
            vec![],
            vec![
                PCMDeviceParameters {
                    effects: Some(vec![StreamEffect::EchoCancellation]),
                    ..Default::default()
                },
                Default::default(),
            ],
        );

        // Invalid effect in device config
        check_failure("output_device_config=[[effects=[none]]]");
    }

    #[test]
    #[cfg(all(unix, feature = "audio_cras"))]
    fn cras_parameters_fromstr() {
        fn cras_check_success(
            s: &str,
            backend: StreamSourceBackend,
            client_type: CrasClientType,
            socket_type: CrasSocketType,
            output_device_config: Vec<PCMDeviceParameters>,
            input_device_config: Vec<PCMDeviceParameters>,
        ) {
            let params: Parameters =
                serde_keyvalue::from_key_values(s).expect("parse should have succeded");
            assert_eq!(params.backend, backend);
            assert_eq!(params.client_type, client_type);
            assert_eq!(params.socket_type, socket_type);
            assert_eq!(params.output_device_config, output_device_config);
            assert_eq!(params.input_device_config, input_device_config);
        }

        cras_check_success(
            "backend=cras",
            StreamSourceBackend::Sys(SysStreamSourceBackend::CRAS),
            CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            CrasSocketType::Unified,
            vec![],
            vec![],
        );
        cras_check_success(
            "backend=cras,client_type=crosvm",
            StreamSourceBackend::Sys(SysStreamSourceBackend::CRAS),
            CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            CrasSocketType::Unified,
            vec![],
            vec![],
        );
        cras_check_success(
            "backend=cras,client_type=arcvm",
            StreamSourceBackend::Sys(SysStreamSourceBackend::CRAS),
            CrasClientType::CRAS_CLIENT_TYPE_ARCVM,
            CrasSocketType::Unified,
            vec![],
            vec![],
        );
        check_failure("backend=cras,client_type=none");
        cras_check_success(
            "backend=cras,socket_type=legacy",
            StreamSourceBackend::Sys(SysStreamSourceBackend::CRAS),
            CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            CrasSocketType::Legacy,
            vec![],
            vec![],
        );
        cras_check_success(
            "backend=cras,socket_type=unified",
            StreamSourceBackend::Sys(SysStreamSourceBackend::CRAS),
            CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            CrasSocketType::Unified,
            vec![],
            vec![],
        );
        cras_check_success(
            "output_device_config=[[client_type=crosvm],[client_type=arcvm,stream_type=pro_audio],[]]",
            StreamSourceBackend::NULL,
            CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            CrasSocketType::Unified,
            vec![
                PCMDeviceParameters{
                    client_type: Some(CrasClientType::CRAS_CLIENT_TYPE_CROSVM),
                    stream_type: None,
                    effects: None,
                },
                PCMDeviceParameters{
                    client_type: Some(CrasClientType::CRAS_CLIENT_TYPE_ARCVM),
                    stream_type: Some(CrasStreamType::CRAS_STREAM_TYPE_PRO_AUDIO),
                    effects: None,
                },
                Default::default(),
                ],
            vec![],
        );
        cras_check_success(
            "input_device_config=[[client_type=crosvm],[client_type=arcvm,effects=[aec],stream_type=pro_audio],[effects=[EchoCancellation]],[]]",
            StreamSourceBackend::NULL,
            CrasClientType::CRAS_CLIENT_TYPE_CROSVM,
            CrasSocketType::Unified,
            vec![],
            vec![
                PCMDeviceParameters{
                    client_type: Some(CrasClientType::CRAS_CLIENT_TYPE_CROSVM),
                    stream_type: None,
                    effects: None,
                },
                PCMDeviceParameters{
                    client_type: Some(CrasClientType::CRAS_CLIENT_TYPE_ARCVM),
                    stream_type: Some(CrasStreamType::CRAS_STREAM_TYPE_PRO_AUDIO),
                    effects: Some(vec![StreamEffect::EchoCancellation]),
                },
                PCMDeviceParameters{
                    client_type: None,
                    stream_type: None,
                    effects: Some(vec![StreamEffect::EchoCancellation]),
                },
                Default::default(),
                ],
        );

        // Invalid client_type in device config
        check_failure("output_device_config=[[client_type=none]]");

        // Invalid stream type in device config
        check_failure("output_device_config=[[stream_type=none]]");
    }

    #[test]
    fn get_device_params_output() {
        let params = Parameters {
            output_device_config: vec![
                PCMDeviceParameters {
                    effects: Some(vec![StreamEffect::EchoCancellation]),
                    ..Default::default()
                },
                PCMDeviceParameters {
                    effects: Some(vec![
                        StreamEffect::EchoCancellation,
                        StreamEffect::EchoCancellation,
                    ]),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let default_pcm_info = virtio_snd_pcm_info {
            hdr: virtio_snd_info {
                hda_fn_nid: 0.into(),
            },
            features: 0.into(),
            formats: 0.into(),
            rates: 0.into(),
            direction: VIRTIO_SND_D_OUTPUT, // Direction is OUTPUT
            channels_min: 1,
            channels_max: 6,
            padding: [0; 5],
        };

        let mut pcm_info = default_pcm_info;
        pcm_info.hdr.hda_fn_nid = 0.into();
        assert_eq!(
            params.get_device_params(&pcm_info),
            Ok(params.output_device_config[0].clone())
        );

        let mut pcm_info = default_pcm_info;
        pcm_info.hdr.hda_fn_nid = 1.into();
        assert_eq!(
            params.get_device_params(&pcm_info),
            Ok(params.output_device_config[1].clone())
        );

        let mut pcm_info = default_pcm_info;
        pcm_info.hdr.hda_fn_nid = 2.into();
        assert_eq!(
            params.get_device_params(&pcm_info),
            Err(Error::InvalidPCMDeviceConfigIndex(2))
        );
    }

    #[test]
    fn get_device_params_input() {
        let params = Parameters {
            input_device_config: vec![
                PCMDeviceParameters {
                    effects: Some(vec![
                        StreamEffect::EchoCancellation,
                        StreamEffect::EchoCancellation,
                    ]),
                    ..Default::default()
                },
                PCMDeviceParameters {
                    effects: Some(vec![StreamEffect::EchoCancellation]),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let default_pcm_info = virtio_snd_pcm_info {
            hdr: virtio_snd_info {
                hda_fn_nid: 0.into(),
            },
            features: 0.into(),
            formats: 0.into(),
            rates: 0.into(),
            direction: VIRTIO_SND_D_INPUT, // Direction is INPUT
            channels_min: 1,
            channels_max: 6,
            padding: [0; 5],
        };

        let mut pcm_info = default_pcm_info;
        pcm_info.hdr.hda_fn_nid = 0.into();
        assert_eq!(
            params.get_device_params(&pcm_info),
            Ok(params.input_device_config[0].clone())
        );

        let mut pcm_info = default_pcm_info;
        pcm_info.hdr.hda_fn_nid = 1.into();
        assert_eq!(
            params.get_device_params(&pcm_info),
            Ok(params.input_device_config[1].clone())
        );

        let mut pcm_info = default_pcm_info;
        pcm_info.hdr.hda_fn_nid = 2.into();
        assert_eq!(
            params.get_device_params(&pcm_info),
            Err(Error::InvalidPCMDeviceConfigIndex(2))
        );
    }

    #[test]
    fn get_device_params_invalid_direction() {
        let params = Parameters::default();

        let pcm_info = virtio_snd_pcm_info {
            hdr: virtio_snd_info {
                hda_fn_nid: 0.into(),
            },
            features: 0.into(),
            formats: 0.into(),
            rates: 0.into(),
            direction: 2, // Invalid direction
            channels_min: 1,
            channels_max: 6,
            padding: [0; 5],
        };

        assert_eq!(
            params.get_device_params(&pcm_info),
            Err(Error::InvalidPCMInfoDirection(2))
        );
    }
}