summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cubeb-coreaudio/src/backend/tests/manual.rs
blob: 3bf96c65c631d42fa670c6b45717eabc7ba38535 (plain)
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
use super::utils::{
    test_get_devices_in_scope, test_ops_context_operation, test_ops_stream_operation, Scope,
    StreamType, TestDeviceInfo, TestDeviceSwitcher,
};
use super::*;
use std::io;

#[ignore]
#[test]
fn test_switch_output_device() {
    use std::f32::consts::PI;

    const SAMPLE_FREQUENCY: u32 = 48_000;

    // Do nothing if there is no 2 available output devices at least.
    let devices = test_get_devices_in_scope(Scope::Output);
    if devices.len() < 2 {
        println!("Need 2 output devices at least.");
        return;
    }

    let mut output_device_switcher = TestDeviceSwitcher::new(Scope::Output);

    // Make sure the parameters meet the requirements of AudioUnitContext::stream_init
    // (in the comments).
    let mut output_params = ffi::cubeb_stream_params::default();
    output_params.format = ffi::CUBEB_SAMPLE_S16NE;
    output_params.rate = SAMPLE_FREQUENCY;
    output_params.channels = 1;
    output_params.layout = ffi::CUBEB_LAYOUT_MONO;
    output_params.prefs = ffi::CUBEB_STREAM_PREF_NONE;

    // Used to calculate the tone's wave.
    let mut position: i64 = 0; // TODO: Use Atomic instead.

    test_ops_stream_operation(
        "stream: North American dial tone",
        ptr::null_mut(), // Use default input device.
        ptr::null_mut(), // No input parameters.
        ptr::null_mut(), // Use default output device.
        &mut output_params,
        4096, // TODO: Get latency by get_min_latency instead ?
        Some(data_callback),
        Some(state_callback),
        &mut position as *mut i64 as *mut c_void,
        |stream| {
            assert_eq!(unsafe { OPS.stream_start.unwrap()(stream) }, ffi::CUBEB_OK);
            println!("Start playing! Enter 's' to switch device. Enter 'q' to quit.");
            loop {
                let mut input = String::new();
                let _ = io::stdin().read_line(&mut input);
                assert_eq!(input.pop().unwrap(), '\n');
                match input.as_str() {
                    "s" => {
                        output_device_switcher.next();
                    }
                    "q" => {
                        println!("Quit.");
                        break;
                    }
                    x => {
                        println!("Unknown command: {}", x);
                    }
                }
            }
            assert_eq!(unsafe { OPS.stream_stop.unwrap()(stream) }, ffi::CUBEB_OK);
        },
    );

    extern "C" fn state_callback(
        stream: *mut ffi::cubeb_stream,
        user_ptr: *mut c_void,
        state: ffi::cubeb_state,
    ) {
        assert!(!stream.is_null());
        assert!(!user_ptr.is_null());
        assert_ne!(state, ffi::CUBEB_STATE_ERROR);
    }

    extern "C" fn data_callback(
        stream: *mut ffi::cubeb_stream,
        user_ptr: *mut c_void,
        _input_buffer: *const c_void,
        output_buffer: *mut c_void,
        nframes: i64,
    ) -> i64 {
        assert!(!stream.is_null());
        assert!(!user_ptr.is_null());
        assert!(!output_buffer.is_null());

        let buffer = unsafe {
            let ptr = output_buffer as *mut i16;
            let len = nframes as usize;
            slice::from_raw_parts_mut(ptr, len)
        };

        let position = unsafe { &mut *(user_ptr as *mut i64) };

        // Generate tone on the fly.
        for data in buffer.iter_mut() {
            let t1 = (2.0 * PI * 350.0 * (*position) as f32 / SAMPLE_FREQUENCY as f32).sin();
            let t2 = (2.0 * PI * 440.0 * (*position) as f32 / SAMPLE_FREQUENCY as f32).sin();
            *data = f32_to_i16_sample(0.5 * (t1 + t2));
            *position += 1;
        }

        nframes
    }

    fn f32_to_i16_sample(x: f32) -> i16 {
        (x * f32::from(i16::max_value())) as i16
    }
}

#[ignore]
#[test]
fn test_device_collection_change() {
    const DUMMY_PTR: *mut c_void = 0xDEAD_BEEF as *mut c_void;
    let mut context = AudioUnitContext::new();
    println!("Context allocated @ {:p}", &context);

    extern "C" fn input_changed_callback(context: *mut ffi::cubeb, data: *mut c_void) {
        println!(
            "Input device collection @ {:p} is changed. Data @ {:p}",
            context, data
        );
        assert_eq!(data, DUMMY_PTR);
    }

    extern "C" fn output_changed_callback(context: *mut ffi::cubeb, data: *mut c_void) {
        println!(
            "output device collection @ {:p} is changed. Data @ {:p}",
            context, data
        );
        assert_eq!(data, DUMMY_PTR);
    }

    context.register_device_collection_changed(
        DeviceType::INPUT,
        Some(input_changed_callback),
        DUMMY_PTR,
    );

    context.register_device_collection_changed(
        DeviceType::OUTPUT,
        Some(output_changed_callback),
        DUMMY_PTR,
    );

    println!("Unplug/Plug device to see the event log.\nEnter anything to finish.");
    let mut input = String::new();
    let _ = std::io::stdin().read_line(&mut input);
}

struct StreamData {
    stream_ptr: *mut ffi::cubeb_stream,
    enable_loopback: AtomicBool,
}

impl StreamData {
    fn new() -> Self {
        Self {
            stream_ptr: ptr::null_mut(),
            enable_loopback: AtomicBool::new(false),
        }
    }
}

struct StreamsData {
    streams: Vec<StreamData>,
    current_idx: Option<usize>,
}

impl StreamsData {
    fn new() -> Self {
        Self {
            streams: Vec::new(),
            current_idx: None,
        }
    }

    fn len(&self) -> usize {
        self.streams.len()
    }

    fn current_mut(&mut self) -> &mut StreamData {
        &mut self.streams[self.current_idx.unwrap()]
    }

    fn current(&self) -> &StreamData {
        &self.streams[self.current_idx.unwrap()]
    }

    fn select(&mut self, idx: usize) {
        assert!(idx < self.len());
        self.current_idx = Some(idx);
    }

    fn push(&mut self, stream: StreamData) {
        self.streams.push(stream)
    }
}

#[ignore]
#[test]
fn test_stream_tester() {
    test_ops_context_operation("context: stream tester", |context_ptr| {
        let mut input_prefs = StreamPrefs::NONE;
        let mut output_prefs = StreamPrefs::NONE;
        let mut streams = StreamsData::new();
        loop {
            println!(
                "Current stream: {} (of {}). Commands:\n\
                 \t'q': quit\n\
                 \t'b': change current stream\n\
                 \t'i': set input stream prefs to be used for creating streams\n\
                 \t'o': set output stream prefs to be used for creating streams\n\
                 \t'c': create a stream\n\
                 Commands on the current stream:\n\
                 \t'd': destroy\n\
                 \t's': start\n\
                 \t't': stop\n\
                 \t'r': register a device changed callback\n\
                 \t'l': set loopback (DUPLEX-only)\n\
                 \t'v': set volume\n\
                 \t'm': set input mute\n\
                 \t'p': set input processing",
                streams
                    .current_idx
                    .map(|i| format!("{}", i + 1 as usize))
                    .unwrap_or(String::from("N/A")),
                streams.len(),
            );

            let mut command = String::new();
            let _ = io::stdin().read_line(&mut command);
            assert_eq!(command.pop().unwrap(), '\n');

            match command.as_str() {
                "q" => {
                    println!("Quit.");
                    for mut stream in streams.streams {
                        if !stream.stream_ptr.is_null() {
                            destroy_stream(&mut stream);
                        }
                    }
                    break;
                }
                "i" => set_prefs(&mut input_prefs),
                "o" => set_prefs(&mut output_prefs),
                "c" => create_stream(context_ptr, &mut streams, input_prefs, output_prefs),
                _ if streams.current_idx.is_none() => {
                    println!("There are no streams! Create a stream first.")
                }
                cmd => match cmd {
                    "b" => select_stream(&mut streams),
                    "d" => destroy_stream(streams.current_mut()),
                    "s" => start_stream(streams.current()),
                    "t" => stop_stream(streams.current()),
                    "r" => register_device_change_callback(streams.current()),
                    "l" => set_loopback(streams.current()),
                    "v" => set_volume(streams.current()),
                    "m" => set_input_mute(streams.current()),
                    "p" => set_input_processing(streams.current()),
                    x => println!("Unknown command: {}", x),
                },
            }
        }
    });

    fn set_prefs(prefs: &mut StreamPrefs) {
        let mut done = false;
        while !done {
            println!(
                "Current prefs: {:?}\nSelect action:\n\
                 \t1) Set None\n\
                 \t2) Toggle Loopback\n\
                 \t3) Toggle Disable Device Switching\n\
                 \t4) Toggle Voice\n\
                 \t5) Set All\n\
                 \t0) Done",
                prefs
            );
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            assert_eq!(input.pop().unwrap(), '\n');
            match input.as_str() {
                "1" => *prefs = StreamPrefs::NONE,
                "2" => prefs.toggle(StreamPrefs::LOOPBACK),
                "3" => prefs.toggle(StreamPrefs::DISABLE_DEVICE_SWITCHING),
                "4" => prefs.toggle(StreamPrefs::VOICE),
                "5" => *prefs = StreamPrefs::all(),
                "0" => done = true,
                _ => println!("Invalid action. Select again.\n"),
            }
        }
    }

    fn select_stream(streams: &mut StreamsData) {
        let num_streams = streams.len();
        let current_idx = streams.current_idx.unwrap();
        println!(
            "Current stream is {}. Select stream 1 to {} on which to apply commands:",
            current_idx + 1 as usize,
            num_streams
        );
        let mut selection: Option<usize> = None;
        while selection.is_none() {
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            assert_eq!(input.pop().unwrap(), '\n');
            selection = match input.parse::<usize>() {
                Ok(i) if (1..=num_streams).contains((&i).into()) => Some(i),
                _ => {
                    println!("Invalid stream. Select again.\n");
                    None
                }
            }
        }
        streams.select(selection.unwrap() - 1)
    }

    fn start_stream(stream: &StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No stream can start.");
            return;
        }
        assert_eq!(
            unsafe { OPS.stream_start.unwrap()(stream.stream_ptr) },
            ffi::CUBEB_OK
        );
        println!("Stream {:p} started.", stream.stream_ptr);
    }

    fn stop_stream(stream: &StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No stream can stop.");
            return;
        }
        assert_eq!(
            unsafe { OPS.stream_stop.unwrap()(stream.stream_ptr) },
            ffi::CUBEB_OK
        );
        println!("Stream {:p} stopped.", stream.stream_ptr);
    }

    fn set_volume(stream: &StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No stream can set volume.");
            return;
        }
        const VOL: f32 = 0.5;
        assert_eq!(
            unsafe { OPS.stream_set_volume.unwrap()(stream.stream_ptr, VOL) },
            ffi::CUBEB_OK
        );
        println!("Set stream {:p} volume to {}", stream.stream_ptr, VOL);
    }

    fn set_loopback(stream: &StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No stream can set loopback.");
            return;
        }
        let stm = unsafe { &mut *(stream.stream_ptr as *mut AudioUnitStream) };
        if !stm.core_stream_data.has_input() || !stm.core_stream_data.has_output() {
            println!("Duplex stream needed to set loopback");
            return;
        }
        let mut loopback: Option<bool> = None;
        while loopback.is_none() {
            println!("Select action:\n1) Enable loopback, 2) Disable loopback");
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            assert_eq!(input.pop().unwrap(), '\n');
            loopback = match input.as_str() {
                "1" => Some(true),
                "2" => Some(false),
                _ => {
                    println!("Invalid action. Select again.\n");
                    None
                }
            }
        }
        let loopback = loopback.unwrap();
        stream.enable_loopback.store(loopback, Ordering::SeqCst);
        println!(
            "Loopback {} for stream {:p}",
            if loopback { "enabled" } else { "disabled" },
            stream.stream_ptr
        );
    }

    fn set_input_mute(stream: &StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No stream can set input mute.");
            return;
        }
        let stm = unsafe { &mut *(stream.stream_ptr as *mut AudioUnitStream) };
        if !stm.core_stream_data.has_input() {
            println!("Input stream needed to set loopback");
            return;
        }
        let mut mute: Option<bool> = None;
        while mute.is_none() {
            println!("Select action:\n1) Mute, 2) Unmute");
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            assert_eq!(input.pop().unwrap(), '\n');
            mute = match input.as_str() {
                "1" => Some(true),
                "2" => Some(false),
                _ => {
                    println!("Invalid action. Select again.\n");
                    None
                }
            }
        }
        let mute = mute.unwrap();
        let res = unsafe { OPS.stream_set_input_mute.unwrap()(stream.stream_ptr, mute.into()) };
        println!(
            "{} set stream {:p} input {}",
            if res == ffi::CUBEB_OK {
                "Successfully"
            } else {
                "Failed to"
            },
            stream.stream_ptr,
            if mute { "mute" } else { "unmute" }
        );
    }

    fn set_input_processing(stream: &StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No stream can set input processing.");
            return;
        }
        let stm = unsafe { &mut *(stream.stream_ptr as *mut AudioUnitStream) };
        if !stm.core_stream_data.using_voice_processing_unit() {
            println!("Duplex stream with voice processing needed to set input processing params");
            return;
        }
        let mut params = InputProcessingParams::NONE;
        {
            let mut bypass = u32::from(true);
            let mut size: usize = mem::size_of::<u32>();
            assert_eq!(
                audio_unit_get_property(
                    stm.core_stream_data.input_unit,
                    kAudioUnitProperty_BypassEffect,
                    kAudioUnitScope_Global,
                    AU_IN_BUS,
                    &mut bypass,
                    &mut size,
                ),
                NO_ERR
            );
            assert_eq!(size, mem::size_of::<u32>());
            if bypass == 0 {
                params.set(InputProcessingParams::ECHO_CANCELLATION, true);
                params.set(InputProcessingParams::NOISE_SUPPRESSION, true);
            }
            let mut agc = u32::from(false);
            let mut size: usize = mem::size_of::<u32>();
            assert_eq!(
                audio_unit_get_property(
                    stm.core_stream_data.input_unit,
                    kAUVoiceIOProperty_VoiceProcessingEnableAGC,
                    kAudioUnitScope_Global,
                    AU_IN_BUS,
                    &mut agc,
                    &mut size,
                ),
                NO_ERR
            );
            assert_eq!(size, mem::size_of::<u32>());
            if agc == 1 {
                params.set(InputProcessingParams::AUTOMATIC_GAIN_CONTROL, true);
            }
        }
        let mut done = false;
        while !done {
            println!(
                "Supported params: {:?}\nCurrent params: {:?}\nSelect action:\n\
                 \t1) Set None\n\
                 \t2) Toggle Echo Cancellation\n\
                 \t3) Toggle Noise Suppression\n\
                 \t4) Toggle Automatic Gain Control\n\
                 \t5) Toggle Voice Isolation\n\
                 \t6) Set All\n\
                 \t0) Done",
                stm.context.supported_input_processing_params().unwrap(),
                params
            );
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            assert_eq!(input.pop().unwrap(), '\n');
            match input.as_str() {
                "1" => params = InputProcessingParams::NONE,
                "2" => params.toggle(InputProcessingParams::ECHO_CANCELLATION),
                "3" => params.toggle(InputProcessingParams::NOISE_SUPPRESSION),
                "4" => params.toggle(InputProcessingParams::AUTOMATIC_GAIN_CONTROL),
                "5" => params.toggle(InputProcessingParams::VOICE_ISOLATION),
                "6" => params = InputProcessingParams::all(),
                "0" => done = true,
                _ => println!("Invalid action. Select again.\n"),
            }
        }
        let res = unsafe {
            OPS.stream_set_input_processing_params.unwrap()(stream.stream_ptr, params.bits())
        };
        println!(
            "{} set stream {:p} input processing params to {:?}",
            if res == ffi::CUBEB_OK {
                "Successfully"
            } else {
                "Failed to"
            },
            stream.stream_ptr,
            params,
        );
    }

    fn register_device_change_callback(stream: &StreamData) {
        extern "C" fn callback(user_ptr: *mut c_void) {
            println!("user pointer @ {:p}", user_ptr);
            assert!(user_ptr.is_null());
        }

        if stream.stream_ptr.is_null() {
            println!("No stream for registering the callback.");
            return;
        }
        assert_eq!(
            unsafe {
                OPS.stream_register_device_changed_callback.unwrap()(
                    stream.stream_ptr,
                    Some(callback),
                )
            },
            ffi::CUBEB_OK
        );
        println!(
            "Stream {:p} now has a device change callback.",
            stream.stream_ptr
        );
    }

    fn destroy_stream(stream: &mut StreamData) {
        if stream.stream_ptr.is_null() {
            println!("No need to destroy stream.");
            return;
        }
        unsafe {
            OPS.stream_destroy.unwrap()((*stream).stream_ptr);
        }
        println!("Stream {:p} destroyed.", stream.stream_ptr);
        stream.stream_ptr = ptr::null_mut();
    }

    fn create_stream(
        context_ptr: *mut ffi::cubeb,
        streams: &mut StreamsData,
        input_prefs: StreamPrefs,
        output_prefs: StreamPrefs,
    ) {
        if streams.len() == 0 || !streams.current().stream_ptr.is_null() {
            println!("Allocating stream {}.", streams.len() + 1);
            streams.push(StreamData::new());
            streams.select(streams.len() - 1);
        }

        let stream = streams.current_mut();
        if !stream.stream_ptr.is_null() {
            println!("Stream has been created.");
            return;
        }

        let mut stream_type = StreamType::empty();
        while stream_type.is_empty() {
            println!("Select stream type:\n1) Input 2) Output 3) In-Out Duplex 4) Back");
            let mut input = String::new();
            let _ = io::stdin().read_line(&mut input);
            assert_eq!(input.pop().unwrap(), '\n');
            stream_type = match input.as_str() {
                "1" => StreamType::INPUT,
                "2" => StreamType::OUTPUT,
                "3" => StreamType::DUPLEX,
                "4" => {
                    println!("Do nothing.");
                    return;
                }
                _ => {
                    println!("Invalid type. Select again.\n");
                    StreamType::empty()
                }
            }
        }

        let device_selector = |scope: Scope| -> AudioObjectID {
            loop {
                println!(
                    "Select {} device:\n",
                    if scope == Scope::Input {
                        "input"
                    } else {
                        "output"
                    }
                );
                let mut list = vec![];
                list.push(kAudioObjectUnknown);
                println!("{:>4}: System default", 0);
                let devices = test_get_devices_in_scope(scope.clone());
                for (idx, device) in devices.iter().enumerate() {
                    list.push(*device);
                    let info = TestDeviceInfo::new(*device, scope.clone());
                    println!(
                        "{:>4}: {}\n\tAudioObjectID: {}\n\tuid: {}",
                        idx + 1,
                        info.label,
                        device,
                        info.uid
                    );
                }

                let mut input = String::new();
                io::stdin().read_line(&mut input).unwrap();
                let n: usize = match input.trim().parse() {
                    Err(_) => {
                        println!("Invalid option. Try again.\n");
                        continue;
                    }
                    Ok(n) => n,
                };
                if n >= list.len() {
                    println!("Invalid option. Try again.\n");
                    continue;
                }
                return list[n];
            }
        };

        let mut input_params = get_dummy_stream_params(Scope::Input, input_prefs);
        let mut output_params = get_dummy_stream_params(Scope::Output, output_prefs);

        let (input_device, input_stream_params) = if stream_type.contains(StreamType::INPUT) {
            (
                device_selector(Scope::Input),
                &mut input_params as *mut ffi::cubeb_stream_params,
            )
        } else {
            (
                kAudioObjectUnknown, /* default input device */
                ptr::null_mut(),
            )
        };

        let (output_device, output_stream_params) = if stream_type.contains(StreamType::OUTPUT) {
            (
                device_selector(Scope::Output),
                &mut output_params as *mut ffi::cubeb_stream_params,
            )
        } else {
            (
                kAudioObjectUnknown, /* default output device */
                ptr::null_mut(),
            )
        };

        let stream_name = CString::new("stream tester").unwrap();

        assert_eq!(
            unsafe {
                OPS.stream_init.unwrap()(
                    context_ptr,
                    &mut stream.stream_ptr,
                    stream_name.as_ptr(),
                    input_device as ffi::cubeb_devid,
                    input_stream_params,
                    output_device as ffi::cubeb_devid,
                    output_stream_params,
                    4096, // latency
                    Some(data_callback),
                    Some(state_callback),
                    &stream.enable_loopback as *const AtomicBool as *mut c_void, // user pointer
                )
            },
            ffi::CUBEB_OK
        );
        assert!(!stream.stream_ptr.is_null());
        println!("Stream {:p} created.", stream.stream_ptr);

        extern "C" fn state_callback(
            stream: *mut ffi::cubeb_stream,
            _user_ptr: *mut c_void,
            state: ffi::cubeb_state,
        ) {
            assert!(!stream.is_null());
            let s = State::from(state);
            println!("state: {:?}", s);
        }

        extern "C" fn data_callback(
            stream: *mut ffi::cubeb_stream,
            user_ptr: *mut c_void,
            input_buffer: *const c_void,
            output_buffer: *mut c_void,
            nframes: i64,
        ) -> i64 {
            assert!(!stream.is_null());

            let enable_loopback = unsafe { &mut *(user_ptr as *mut AtomicBool) };
            let loopback = enable_loopback.load(Ordering::SeqCst);
            if loopback && !input_buffer.is_null() && !output_buffer.is_null() {
                // Dupe the mono input to stereo
                let stm = unsafe { &mut *(stream as *mut AudioUnitStream) };
                assert_eq!(stm.core_stream_data.input_stream_params.channels(), 1);
                let channels = stm.core_stream_data.output_stream_params.channels() as usize;
                let sample_size =
                    cubeb_sample_size(stm.core_stream_data.output_stream_params.format());
                for f in 0..(nframes as usize) {
                    let input_offset = f * sample_size;
                    let output_offset = input_offset * channels;
                    for c in 0..channels {
                        unsafe {
                            ptr::copy(
                                input_buffer.add(input_offset) as *const u8,
                                output_buffer.add(output_offset + (sample_size * c)) as *mut u8,
                                sample_size,
                            )
                        };
                    }
                }
            } else if !output_buffer.is_null() {
                // Feed silence data to output buffer
                let stm = unsafe { &mut *(stream as *mut AudioUnitStream) };
                let channels = stm.core_stream_data.output_stream_params.channels();
                let samples = nframes as usize * channels as usize;
                let sample_size =
                    cubeb_sample_size(stm.core_stream_data.output_stream_params.format());
                unsafe {
                    ptr::write_bytes(output_buffer, 0, samples * sample_size);
                }
            }

            nframes
        }

        fn get_dummy_stream_params(scope: Scope, prefs: StreamPrefs) -> ffi::cubeb_stream_params {
            // The stream format for input and output must be same.
            const STREAM_FORMAT: u32 = ffi::CUBEB_SAMPLE_FLOAT32NE;

            // Make sure the parameters meet the requirements of AudioUnitContext::stream_init
            // (in the comments).
            let mut stream_params = ffi::cubeb_stream_params::default();
            stream_params.prefs = prefs.bits();
            let (format, rate, channels, layout) = match scope {
                Scope::Input => (STREAM_FORMAT, 48000, 1, ffi::CUBEB_LAYOUT_MONO),
                Scope::Output => (STREAM_FORMAT, 44100, 2, ffi::CUBEB_LAYOUT_STEREO),
            };
            stream_params.format = format;
            stream_params.rate = rate;
            stream_params.channels = channels;
            stream_params.layout = layout;
            stream_params
        }
    }
}