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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
// 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::fmt;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;

use audio_streams::SampleFormat;
use audio_streams::StreamEffect;
use base::error;
use base::warn;
use cros_async::sync::Condvar;
use cros_async::sync::RwLock as AsyncRwLock;
use cros_async::Executor;
use futures::channel::mpsc;
use futures::Future;
use futures::TryFutureExt;
use serde::Deserialize;
use serde::Serialize;

use super::Error;
use super::PcmResponse;
use super::WorkerStatus;
use crate::virtio::snd::common::*;
use crate::virtio::snd::common_backend::async_funcs::*;
use crate::virtio::snd::common_backend::DirectionalStream;
use crate::virtio::snd::common_backend::SysAsyncStreamObjects;
use crate::virtio::snd::constants::*;
use crate::virtio::snd::sys::SysAudioStreamSource;
use crate::virtio::snd::sys::SysAudioStreamSourceGenerator;
use crate::virtio::DescriptorChain;

/// Parameters for setting parameters in StreamInfo
#[derive(Copy, Clone, Debug)]
pub struct SetParams {
    pub channels: u8,
    pub format: SampleFormat,
    pub frame_rate: u32,
    pub buffer_bytes: usize,
    pub period_bytes: usize,
    pub dir: u8,
}

/// StreamInfoBuilder builds a [`StreamInfo`]. It is used when we want to store the parameters to
/// create a [`StreamInfo`] beforehand and actually create it later. (as is the case with VirtioSnd)
///
/// To create a new StreamInfoBuilder, see [`StreamInfo::builder()`].
#[derive(Clone)]
pub struct StreamInfoBuilder {
    stream_source_generator: Arc<SysAudioStreamSourceGenerator>,
    effects: Vec<StreamEffect>,
    card_index: usize,
    #[cfg(windows)]
    audio_client_guid: Option<String>,
}

impl StreamInfoBuilder {
    /// Creates a StreamInfoBuilder with minimal required fields:
    ///
    /// * `stream_source_generator`: Generator which generates stream source in
    ///   [`StreamInfo::prepare()`].
    /// * `card_index`: The ALSA card index.
    pub fn new(
        stream_source_generator: Arc<SysAudioStreamSourceGenerator>,
        card_index: usize,
    ) -> Self {
        StreamInfoBuilder {
            stream_source_generator,
            effects: vec![],
            card_index,
            #[cfg(windows)]
            audio_client_guid: None,
        }
    }

    /// Set the [`StreamEffect`]s to use when creating a stream from the stream source in
    /// [`StreamInfo::prepare()`]. The default value is no effects.
    pub fn effects(mut self, effects: Vec<StreamEffect>) -> Self {
        self.effects = effects;
        self
    }

    #[cfg(windows)]
    pub fn audio_client_guid(mut self, audio_client_guid: Option<String>) -> Self {
        self.audio_client_guid = audio_client_guid;
        self
    }

    /// Builds a [`StreamInfo`].
    pub fn build(self) -> StreamInfo {
        self.into()
    }
}

/// StreamInfo represents a virtio snd stream.
///
/// To create a StreamInfo, see [`StreamInfo::builder()`] and [`StreamInfoBuilder::build()`].
pub struct StreamInfo {
    pub(crate) stream_source: Option<SysAudioStreamSource>,
    stream_source_generator: Arc<SysAudioStreamSourceGenerator>,
    pub(crate) channels: u8,
    pub(crate) format: SampleFormat,
    pub(crate) frame_rate: u32,
    buffer_bytes: usize,
    pub(crate) period_bytes: usize,
    direction: u8,  // VIRTIO_SND_D_*
    pub state: u32, // VIRTIO_SND_R_PCM_SET_PARAMS -> VIRTIO_SND_R_PCM_STOP, or 0 (uninitialized)
    // Stream effects to use when creating a new stream on [`prepare()`].
    pub(crate) effects: Vec<StreamEffect>,

    // just_reset set to true after reset. Make invalid state transition return Ok. Set to false
    // after a valid state transition to SET_PARAMS or PREPARE.
    pub just_reset: bool,

    // Worker related
    pub status_mutex: Rc<AsyncRwLock<WorkerStatus>>,
    pub sender: Option<mpsc::UnboundedSender<DescriptorChain>>,
    worker_future: Option<Box<dyn Future<Output = Result<(), Error>> + Unpin>>,
    release_signal: Option<Rc<(AsyncRwLock<bool>, Condvar)>>, // Signal worker on release
    card_index: usize,
    ex: Option<Executor>, // Executor provided on `prepare()`. Used on `drop()`.
    #[cfg(windows)]
    pub(crate) playback_stream_cache: Option<(
        Arc<AsyncRwLock<Box<dyn audio_streams::AsyncPlaybackBufferStream>>>,
        Rc<AsyncRwLock<Box<dyn PlaybackBufferWriter>>>,
    )>,
    #[cfg(windows)]
    pub(crate) audio_client_guid: Option<String>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct StreamInfoSnapshot {
    pub(crate) channels: u8,
    pub(crate) format: SampleFormat,
    pub(crate) frame_rate: u32,
    buffer_bytes: usize,
    pub(crate) period_bytes: usize,
    direction: u8,  // VIRTIO_SND_D_*
    pub state: u32, // VIRTIO_SND_R_PCM_SET_PARAMS -> VIRTIO_SND_R_PCM_STOP, or 0 (uninitialized)
    effects: Vec<StreamEffect>,
    pub just_reset: bool,
}

impl fmt::Debug for StreamInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StreamInfo")
            .field("channels", &self.channels)
            .field("format", &self.format)
            .field("frame_rate", &self.frame_rate)
            .field("buffer_bytes", &self.buffer_bytes)
            .field("period_bytes", &self.period_bytes)
            .field("direction", &get_virtio_direction_name(self.direction))
            .field("state", &get_virtio_snd_r_pcm_cmd_name(self.state))
            .field("effects", &self.effects)
            .finish()
    }
}

impl Drop for StreamInfo {
    fn drop(&mut self) {
        if let Some(ex) = self.ex.take() {
            if self.state == VIRTIO_SND_R_PCM_START {
                match ex.run_until(self.stop()) {
                    Err(e) => error!("Drop stream error on stop in executor: {}", e),
                    Ok(Err(e)) => error!("Drop stream error on stop: {}", e),
                    _ => {}
                }
            }
            if self.state == VIRTIO_SND_R_PCM_PREPARE || self.state == VIRTIO_SND_R_PCM_STOP {
                match ex.run_until(self.release()) {
                    Err(e) => error!("Drop stream error on release in executor: {}", e),
                    Ok(Err(e)) => error!("Drop stream error on release: {}", e),
                    _ => {}
                }
            }
        }
    }
}

impl From<StreamInfoBuilder> for StreamInfo {
    fn from(builder: StreamInfoBuilder) -> Self {
        StreamInfo {
            stream_source: None,
            stream_source_generator: builder.stream_source_generator,
            channels: 0,
            format: SampleFormat::U8,
            frame_rate: 0,
            buffer_bytes: 0,
            period_bytes: 0,
            direction: 0,
            state: 0,
            effects: builder.effects,
            just_reset: false,
            status_mutex: Rc::new(AsyncRwLock::new(WorkerStatus::Pause)),
            sender: None,
            worker_future: None,
            release_signal: None,
            card_index: builder.card_index,
            ex: None,
            #[cfg(windows)]
            playback_stream_cache: None,
            #[cfg(windows)]
            audio_client_guid: builder.audio_client_guid,
        }
    }
}

impl StreamInfo {
    /// Creates a minimal [`StreamInfoBuilder`]. See [`StreamInfoBuilder::new()`] for
    /// the description of each parameter.
    pub fn builder(
        stream_source_generator: Arc<SysAudioStreamSourceGenerator>,
        card_index: usize,
    ) -> StreamInfoBuilder {
        StreamInfoBuilder::new(stream_source_generator, card_index)
    }

    /// Sets parameters of the stream, putting it into [`VIRTIO_SND_R_PCM_SET_PARAMS`] state.
    ///
    /// * `params`: [`SetParams`] for the pcm stream runtime configuration.
    pub async fn set_params(&mut self, params: SetParams) -> Result<(), Error> {
        if self.state != 0
            && self.state != VIRTIO_SND_R_PCM_SET_PARAMS
            && self.state != VIRTIO_SND_R_PCM_PREPARE
            && self.state != VIRTIO_SND_R_PCM_RELEASE
        {
            error!(
                "[Card {}] Invalid PCM state transition from {} to {}",
                self.card_index,
                get_virtio_snd_r_pcm_cmd_name(self.state),
                get_virtio_snd_r_pcm_cmd_name(VIRTIO_SND_R_PCM_SET_PARAMS)
            );
            return Err(Error::OperationNotSupported);
        }

        // Only required for PREPARE -> SET_PARAMS
        self.release_worker().await;

        self.channels = params.channels;
        self.format = params.format;
        self.frame_rate = params.frame_rate;
        self.buffer_bytes = params.buffer_bytes;
        self.period_bytes = params.period_bytes;
        self.direction = params.dir;
        self.state = VIRTIO_SND_R_PCM_SET_PARAMS;
        self.just_reset = false;
        Ok(())
    }

    /// Prepares the stream, putting it into [`VIRTIO_SND_R_PCM_PREPARE`] state.
    ///
    /// * `ex`: [`Executor`] to run the pcm worker.
    /// * `tx_send`: Sender for sending `PcmResponse` for tx queue. (playback stream)
    /// * `rx_send`: Sender for sending `PcmResponse` for rx queue. (capture stream)
    pub async fn prepare(
        &mut self,
        ex: &Executor,
        tx_send: &mpsc::UnboundedSender<PcmResponse>,
        rx_send: &mpsc::UnboundedSender<PcmResponse>,
    ) -> Result<(), Error> {
        if self.state == 0 && self.just_reset {
            return Ok(());
        }
        if self.state != VIRTIO_SND_R_PCM_SET_PARAMS
            && self.state != VIRTIO_SND_R_PCM_PREPARE
            && self.state != VIRTIO_SND_R_PCM_RELEASE
        {
            error!(
                "[Card {}] Invalid PCM state transition from {} to {}",
                self.card_index,
                get_virtio_snd_r_pcm_cmd_name(self.state),
                get_virtio_snd_r_pcm_cmd_name(VIRTIO_SND_R_PCM_PREPARE)
            );
            return Err(Error::OperationNotSupported);
        }
        self.just_reset = false;
        if self.state == VIRTIO_SND_R_PCM_PREPARE {
            self.release_worker().await;
        }
        let frame_size = self.channels as usize * self.format.sample_bytes();
        if self.period_bytes % frame_size != 0 {
            error!(
                "[Card {}] period_bytes must be divisible by frame size",
                self.card_index
            );
            return Err(Error::OperationNotSupported);
        }
        self.stream_source = Some(
            self.stream_source_generator
                .generate()
                .map_err(Error::GenerateStreamSource)?,
        );
        let stream_objects = match self.direction {
            VIRTIO_SND_D_OUTPUT => SysAsyncStreamObjects {
                stream: self
                    .create_directionstream_output(
                        frame_size,
                        #[cfg(windows)]
                        self.audio_client_guid.clone(),
                        ex,
                    )
                    .await?,
                pcm_sender: tx_send.clone(),
            },
            VIRTIO_SND_D_INPUT => {
                let buffer_reader = self.set_up_async_capture_stream(frame_size, ex).await?;
                SysAsyncStreamObjects {
                    stream: DirectionalStream::Input(self.period_bytes, Box::new(buffer_reader)),
                    pcm_sender: rx_send.clone(),
                }
            }
            _ => unreachable!(),
        };

        let (sender, receiver) = mpsc::unbounded();
        self.sender = Some(sender);
        self.state = VIRTIO_SND_R_PCM_PREPARE;

        self.status_mutex = Rc::new(AsyncRwLock::new(WorkerStatus::Pause));
        let period_dur = Duration::from_secs_f64(
            self.period_bytes as f64 / frame_size as f64 / self.frame_rate as f64,
        );
        let release_signal = Rc::new((AsyncRwLock::new(false), Condvar::new()));
        self.release_signal = Some(release_signal.clone());
        let f = start_pcm_worker(
            ex.clone(),
            stream_objects.stream,
            receiver,
            self.status_mutex.clone(),
            stream_objects.pcm_sender,
            period_dur,
            self.card_index,
            release_signal,
        );
        self.worker_future = Some(Box::new(ex.spawn_local(f).into_future()));
        self.ex = Some(ex.clone());
        Ok(())
    }

    /// Starts the stream, putting it into [`VIRTIO_SND_R_PCM_START`] state.
    pub async fn start(&mut self) -> Result<(), Error> {
        if self.just_reset {
            return Ok(());
        }
        if self.state != VIRTIO_SND_R_PCM_PREPARE && self.state != VIRTIO_SND_R_PCM_STOP {
            error!(
                "[Card {}] Invalid PCM state transition from {} to {}",
                self.card_index,
                get_virtio_snd_r_pcm_cmd_name(self.state),
                get_virtio_snd_r_pcm_cmd_name(VIRTIO_SND_R_PCM_START)
            );
            return Err(Error::OperationNotSupported);
        }
        self.state = VIRTIO_SND_R_PCM_START;
        let mut status = self.status_mutex.lock().await;
        if *status != WorkerStatus::Quit {
            *status = WorkerStatus::Running;
        }
        Ok(())
    }

    /// Stops the stream, putting it into [`VIRTIO_SND_R_PCM_STOP`] state.
    pub async fn stop(&mut self) -> Result<(), Error> {
        if self.just_reset {
            return Ok(());
        }
        if self.state != VIRTIO_SND_R_PCM_START {
            error!(
                "[Card {}] Invalid PCM state transition from {} to {}",
                self.card_index,
                get_virtio_snd_r_pcm_cmd_name(self.state),
                get_virtio_snd_r_pcm_cmd_name(VIRTIO_SND_R_PCM_STOP)
            );
            return Err(Error::OperationNotSupported);
        }
        self.state = VIRTIO_SND_R_PCM_STOP;
        let mut status = self.status_mutex.lock().await;
        if *status != WorkerStatus::Quit {
            *status = WorkerStatus::Pause;
        }
        Ok(())
    }

    /// Releases the stream, putting it into [`VIRTIO_SND_R_PCM_RELEASE`] state.
    pub async fn release(&mut self) -> Result<(), Error> {
        if self.just_reset {
            return Ok(());
        }
        if self.state != VIRTIO_SND_R_PCM_PREPARE && self.state != VIRTIO_SND_R_PCM_STOP {
            error!(
                "[Card {}] Invalid PCM state transition from {} to {}",
                self.card_index,
                get_virtio_snd_r_pcm_cmd_name(self.state),
                get_virtio_snd_r_pcm_cmd_name(VIRTIO_SND_R_PCM_RELEASE)
            );
            return Err(Error::OperationNotSupported);
        }
        self.state = VIRTIO_SND_R_PCM_RELEASE;
        self.stream_source = None;
        self.release_worker().await;
        Ok(())
    }

    async fn release_worker(&mut self) {
        *self.status_mutex.lock().await = WorkerStatus::Quit;
        if let Some(s) = self.sender.take() {
            s.close_channel();
        }

        if let Some(release_signal) = self.release_signal.take() {
            let (lock, cvar) = &*release_signal;
            let mut signalled = lock.lock().await;
            *signalled = true;
            cvar.notify_all();
        }

        if let Some(f) = self.worker_future.take() {
            f.await
                .map_err(|error| {
                    warn!(
                        "[Card {}] Failure on releasing the worker_future: {}",
                        self.card_index, error
                    )
                })
                .ok();
        }
        self.ex.take(); // Remove ex as the worker is finished
    }

    pub fn snapshot(&self) -> StreamInfoSnapshot {
        StreamInfoSnapshot {
            channels: self.channels,
            format: self.format,
            frame_rate: self.frame_rate,
            buffer_bytes: self.buffer_bytes,
            period_bytes: self.period_bytes,
            direction: self.direction, // VIRTIO_SND_D_*
            // VIRTIO_SND_R_PCM_SET_PARAMS -> VIRTIO_SND_R_PCM_STOP, or 0 (uninitialized)
            state: self.state,
            effects: self.effects.clone(),
            just_reset: self.just_reset,
        }
    }

    pub fn restore(&mut self, state: &StreamInfoSnapshot) {
        self.channels = state.channels;
        self.format = state.format;
        self.frame_rate = state.frame_rate;
        self.buffer_bytes = state.buffer_bytes;
        self.period_bytes = state.period_bytes;
        self.direction = state.direction;
        self.effects.clone_from(&state.effects);
        self.just_reset = state.just_reset;
    }
}

#[cfg(test)]
mod tests {
    use audio_streams::NoopStreamSourceGenerator;

    use super::*;

    fn new_stream() -> StreamInfo {
        let card_index = 0;
        StreamInfo::builder(
            Arc::new(Box::new(NoopStreamSourceGenerator::new())),
            card_index,
        )
        .build()
    }

    fn stream_set_params(
        mut stream: StreamInfo,
        ex: &Executor,
        expected_ok: bool,
        expected_state: u32,
    ) -> StreamInfo {
        let result = ex.run_until(stream.set_params(SetParams {
            channels: 2,
            format: SampleFormat::U8,
            frame_rate: 48000,
            buffer_bytes: 1024,
            period_bytes: 512,
            dir: VIRTIO_SND_D_OUTPUT,
        }));
        assert_eq!(result.unwrap().is_ok(), expected_ok);
        assert_eq!(stream.state, expected_state);
        stream
    }

    fn stream_prepare(
        mut stream: StreamInfo,
        ex: &Executor,
        expected_ok: bool,
        expected_state: u32,
    ) -> StreamInfo {
        let (tx_send, _) = mpsc::unbounded();
        let (rx_send, _) = mpsc::unbounded();

        let result = ex.run_until(stream.prepare(ex, &tx_send, &rx_send));
        assert_eq!(result.unwrap().is_ok(), expected_ok);
        assert_eq!(stream.state, expected_state);
        stream
    }

    fn stream_start(
        mut stream: StreamInfo,
        ex: &Executor,
        expected_ok: bool,
        expected_state: u32,
    ) -> StreamInfo {
        let result = ex.run_until(stream.start());
        assert_eq!(result.unwrap().is_ok(), expected_ok);
        assert_eq!(stream.state, expected_state);
        stream
    }

    fn stream_stop(
        mut stream: StreamInfo,
        ex: &Executor,
        expected_ok: bool,
        expected_state: u32,
    ) -> StreamInfo {
        let result = ex.run_until(stream.stop());
        assert_eq!(result.unwrap().is_ok(), expected_ok);
        assert_eq!(stream.state, expected_state);
        stream
    }

    fn stream_release(
        mut stream: StreamInfo,
        ex: &Executor,
        expected_ok: bool,
        expected_state: u32,
    ) -> StreamInfo {
        let result = ex.run_until(stream.release());
        assert_eq!(result.unwrap().is_ok(), expected_ok);
        assert_eq!(stream.state, expected_state);
        stream
    }

    #[test]
    fn test_transitions_from_0() {
        let ex = Executor::new().expect("Failed to create an executor");

        // Valid transition to: {SET_PARAMS}
        stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);

        // Invalid transition to: {PREPARE, START, STOP, RELEASE}
        stream_prepare(new_stream(), &ex, false, 0);
        stream_start(new_stream(), &ex, false, 0);
        stream_stop(new_stream(), &ex, false, 0);
        stream_release(new_stream(), &ex, false, 0);
    }

    #[test]
    fn test_transitions_from_set_params() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_set_params = || -> StreamInfo {
            stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS)
        };

        // Valid transition to: {SET_PARAMS, PREPARE}
        stream_set_params(
            new_stream_set_params(),
            &ex,
            true,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
        stream_prepare(new_stream_set_params(), &ex, true, VIRTIO_SND_R_PCM_PREPARE);

        // Invalid transition to: {START, STOP, RELEASE}
        stream_start(
            new_stream_set_params(),
            &ex,
            false,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
        stream_stop(
            new_stream_set_params(),
            &ex,
            false,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
        stream_release(
            new_stream_set_params(),
            &ex,
            false,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
    }

    #[test]
    fn test_transitions_from_prepare() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_prepare = || -> StreamInfo {
            let stream = stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
            stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE)
        };

        // Valid transition to: {SET_PARAMS, PREPARE, START, RELEASE}
        stream_set_params(new_stream_prepare(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
        stream_prepare(new_stream_prepare(), &ex, true, VIRTIO_SND_R_PCM_PREPARE);
        stream_start(new_stream_prepare(), &ex, true, VIRTIO_SND_R_PCM_START);
        stream_release(new_stream_prepare(), &ex, true, VIRTIO_SND_R_PCM_RELEASE);

        // Invalid transition to: {STOP}
        stream_stop(new_stream_prepare(), &ex, false, VIRTIO_SND_R_PCM_PREPARE);
    }

    #[test]
    fn test_transitions_from_start() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_start = || -> StreamInfo {
            let mut stream =
                stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
            stream = stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE);
            stream_start(stream, &ex, true, VIRTIO_SND_R_PCM_START)
        };

        // Valid transition to: {STOP}
        stream_stop(new_stream_start(), &ex, true, VIRTIO_SND_R_PCM_STOP);

        // Invalid transition to: {SET_PARAMS, PREPARE, START, RELEASE}
        stream_set_params(new_stream_start(), &ex, false, VIRTIO_SND_R_PCM_START);
        stream_prepare(new_stream_start(), &ex, false, VIRTIO_SND_R_PCM_START);
        stream_start(new_stream_start(), &ex, false, VIRTIO_SND_R_PCM_START);
        stream_release(new_stream_start(), &ex, false, VIRTIO_SND_R_PCM_START);
    }

    #[test]
    fn test_transitions_from_stop() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_stop = || -> StreamInfo {
            let mut stream =
                stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
            stream = stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE);
            stream = stream_start(stream, &ex, true, VIRTIO_SND_R_PCM_START);
            stream_stop(stream, &ex, true, VIRTIO_SND_R_PCM_STOP)
        };

        // Valid transition to: {START, RELEASE}
        stream_start(new_stream_stop(), &ex, true, VIRTIO_SND_R_PCM_START);
        stream_release(new_stream_stop(), &ex, true, VIRTIO_SND_R_PCM_RELEASE);

        // Invalid transition to: {SET_PARAMS, PREPARE, STOP}
        stream_set_params(new_stream_stop(), &ex, false, VIRTIO_SND_R_PCM_STOP);
        stream_prepare(new_stream_stop(), &ex, false, VIRTIO_SND_R_PCM_STOP);
        stream_stop(new_stream_stop(), &ex, false, VIRTIO_SND_R_PCM_STOP);
    }

    #[test]
    fn test_transitions_from_release() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_release = || -> StreamInfo {
            let mut stream =
                stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
            stream = stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE);
            stream_release(stream, &ex, true, VIRTIO_SND_R_PCM_RELEASE)
        };

        // Valid transition to: {SET_PARAMS, PREPARE}
        stream_set_params(new_stream_release(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
        stream_prepare(new_stream_release(), &ex, true, VIRTIO_SND_R_PCM_PREPARE);

        // Invalid transition to: {START, STOP, RELEASE}
        stream_start(new_stream_release(), &ex, false, VIRTIO_SND_R_PCM_RELEASE);
        stream_stop(new_stream_release(), &ex, false, VIRTIO_SND_R_PCM_RELEASE);
        stream_release(new_stream_release(), &ex, false, VIRTIO_SND_R_PCM_RELEASE);
    }

    #[test]
    fn test_transitions_from_0_just_reset() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_0 = || -> StreamInfo {
            let mut stream = new_stream();
            stream.just_reset = true;
            stream
        };

        // Valid transition to: {SET_PARAMS}
        // After valid transition, just_reset reset to false
        let mut stream = new_stream_0();
        stream = stream_set_params(stream, &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
        assert_eq!(stream.just_reset, false);

        // Invalid transition to: {PREPARE, START, STOP, RELEASE}
        // Return Ok but state doesn't change
        stream_prepare(new_stream_0(), &ex, true, 0);
        stream_start(new_stream_0(), &ex, true, 0);
        stream_stop(new_stream_0(), &ex, true, 0);
        stream_release(new_stream_0(), &ex, true, 0);
    }

    #[test]
    fn test_transitions_from_set_params_just_reset() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_set_params = || -> StreamInfo {
            let mut stream =
                stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
            stream.just_reset = true;
            stream
        };

        // Valid transition to: {SET_PARAMS, PREPARE}
        // After valid transition, just_reset reset to false
        let mut stream = new_stream_set_params();
        stream = stream_set_params(stream, &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
        assert_eq!(stream.just_reset, false);

        let mut stream = new_stream_set_params();
        stream = stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE);
        assert_eq!(stream.just_reset, false);

        // Invalid transition to: {START, STOP, RELEASE}
        // Return Ok but state doesn't change
        stream_start(
            new_stream_set_params(),
            &ex,
            true,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
        stream_stop(
            new_stream_set_params(),
            &ex,
            true,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
        stream_release(
            new_stream_set_params(),
            &ex,
            true,
            VIRTIO_SND_R_PCM_SET_PARAMS,
        );
    }

    #[test]
    fn test_transitions_from_release_just_reset() {
        let ex = Executor::new().expect("Failed to create an executor");
        let new_stream_release = || -> StreamInfo {
            let mut stream =
                stream_set_params(new_stream(), &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
            stream = stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE);
            stream = stream_release(stream, &ex, true, VIRTIO_SND_R_PCM_RELEASE);
            stream.just_reset = true;
            stream
        };

        // Valid transition to: {SET_PARAMS, PREPARE}
        // After valid transition, just_reset reset to false
        let mut stream = new_stream_release();
        stream = stream_set_params(stream, &ex, true, VIRTIO_SND_R_PCM_SET_PARAMS);
        assert_eq!(stream.just_reset, false);

        let mut stream = new_stream_release();
        stream = stream_prepare(stream, &ex, true, VIRTIO_SND_R_PCM_PREPARE);
        assert_eq!(stream.just_reset, false);

        // Invalid transition to: {START, STOP, RELEASE}
        // Return Ok but state doesn't change
        stream_start(new_stream_release(), &ex, true, VIRTIO_SND_R_PCM_RELEASE);
        stream_stop(new_stream_release(), &ex, true, VIRTIO_SND_R_PCM_RELEASE);
        stream_release(new_stream_release(), &ex, true, VIRTIO_SND_R_PCM_RELEASE);
    }

    #[test]
    fn test_stream_info_builder() {
        let card_index = 0;
        let builder = StreamInfo::builder(
            Arc::new(Box::new(NoopStreamSourceGenerator::new())),
            card_index,
        )
        .effects(vec![StreamEffect::EchoCancellation]);

        let stream = builder.build();
        assert_eq!(stream.effects, vec![StreamEffect::EchoCancellation]);
    }
}