summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cubeb-coreaudio/src/backend/tests/utils.rs
blob: ef07aeeeb4156b6470218f4d9575595c3ee120c2 (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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
use super::*;

// Common Utils
// ------------------------------------------------------------------------------------------------
pub extern "C" fn noop_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());

    // Feed silence data to output buffer
    if !output_buffer.is_null() {
        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
}

#[derive(Clone, Debug, PartialEq)]
pub enum Scope {
    Input,
    Output,
}

impl From<Scope> for DeviceType {
    fn from(scope: Scope) -> Self {
        match scope {
            Scope::Input => DeviceType::INPUT,
            Scope::Output => DeviceType::OUTPUT,
        }
    }
}

#[derive(Clone)]
pub enum PropertyScope {
    Input,
    Output,
}

pub fn test_get_default_device(scope: Scope) -> Option<AudioObjectID> {
    let address = AudioObjectPropertyAddress {
        mSelector: match scope {
            Scope::Input => kAudioHardwarePropertyDefaultInputDevice,
            Scope::Output => kAudioHardwarePropertyDefaultOutputDevice,
        },
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };

    let mut devid: AudioObjectID = kAudioObjectUnknown;
    let mut size = mem::size_of::<AudioObjectID>();
    let status = unsafe {
        AudioObjectGetPropertyData(
            kAudioObjectSystemObject,
            &address,
            0,
            ptr::null(),
            &mut size as *mut usize as *mut UInt32,
            &mut devid as *mut AudioObjectID as *mut c_void,
        )
    };
    if status != NO_ERR || devid == kAudioObjectUnknown {
        return None;
    }
    Some(devid)
}

// TODO: Create a GetProperty trait and add a default implementation for it, then implement it
//       for TestAudioUnit so the member method like `get_buffer_frame_size` can reuse the trait
//       method get_property_data.
#[derive(Debug)]
pub struct TestAudioUnit(AudioUnit);

impl TestAudioUnit {
    fn new(unit: AudioUnit) -> Self {
        assert!(!unit.is_null());
        Self(unit)
    }
    pub fn get_inner(&self) -> AudioUnit {
        self.0
    }
    pub fn get_buffer_frame_size(
        &self,
        scope: Scope,
        prop_scope: PropertyScope,
    ) -> std::result::Result<u32, OSStatus> {
        test_audiounit_get_buffer_frame_size(self.0, scope, prop_scope)
    }
}

impl Drop for TestAudioUnit {
    fn drop(&mut self) {
        unsafe {
            AudioUnitUninitialize(self.0);
            AudioComponentInstanceDispose(self.0);
        }
    }
}

// TODO: 1. Return Result with custom errors.
//       2. Allow to create a in-out unit.
pub fn test_get_default_audiounit(scope: Scope) -> Option<TestAudioUnit> {
    let device = test_get_default_device(scope.clone());
    let unit = test_create_audiounit(ComponentSubType::HALOutput);
    if device.is_none() || unit.is_none() {
        return None;
    }
    let unit = unit.unwrap();
    let device = device.unwrap();
    match scope {
        Scope::Input => {
            if test_enable_audiounit_in_scope(unit.get_inner(), Scope::Input, true).is_err()
                || test_enable_audiounit_in_scope(unit.get_inner(), Scope::Output, false).is_err()
            {
                return None;
            }
        }
        Scope::Output => {
            if test_enable_audiounit_in_scope(unit.get_inner(), Scope::Input, false).is_err()
                || test_enable_audiounit_in_scope(unit.get_inner(), Scope::Output, true).is_err()
            {
                return None;
            }
        }
    }

    let status = unsafe {
        AudioUnitSetProperty(
            unit.get_inner(),
            kAudioOutputUnitProperty_CurrentDevice,
            kAudioUnitScope_Global,
            0, // Global bus
            &device as *const AudioObjectID as *const c_void,
            mem::size_of::<AudioObjectID>() as u32,
        )
    };
    if status == NO_ERR {
        Some(unit)
    } else {
        None
    }
}

pub enum ComponentSubType {
    HALOutput,
    DefaultOutput,
}

// TODO: Return Result with custom errors.
// Surprisingly the AudioUnit can be created even when there is no any device on the platform,
// no matter its subtype is HALOutput or DefaultOutput.
pub fn test_create_audiounit(unit_type: ComponentSubType) -> Option<TestAudioUnit> {
    let desc = AudioComponentDescription {
        componentType: kAudioUnitType_Output,
        componentSubType: match unit_type {
            ComponentSubType::HALOutput => kAudioUnitSubType_HALOutput,
            ComponentSubType::DefaultOutput => kAudioUnitSubType_DefaultOutput,
        },
        componentManufacturer: kAudioUnitManufacturer_Apple,
        componentFlags: 0,
        componentFlagsMask: 0,
    };
    let comp = unsafe { AudioComponentFindNext(ptr::null_mut(), &desc) };
    if comp.is_null() {
        return None;
    }
    let mut unit: AudioUnit = ptr::null_mut();
    let status = unsafe { AudioComponentInstanceNew(comp, &mut unit) };
    // TODO: Is unit possible to be null when no error returns ?
    if status != NO_ERR || unit.is_null() {
        None
    } else {
        Some(TestAudioUnit::new(unit))
    }
}

fn test_enable_audiounit_in_scope(
    unit: AudioUnit,
    scope: Scope,
    enable: bool,
) -> std::result::Result<(), OSStatus> {
    assert!(!unit.is_null());
    let (scope, element) = match scope {
        Scope::Input => (kAudioUnitScope_Input, AU_IN_BUS),
        Scope::Output => (kAudioUnitScope_Output, AU_OUT_BUS),
    };
    let on_off: u32 = if enable { 1 } else { 0 };
    let status = unsafe {
        AudioUnitSetProperty(
            unit,
            kAudioOutputUnitProperty_EnableIO,
            scope,
            element,
            &on_off as *const u32 as *const c_void,
            mem::size_of::<u32>() as u32,
        )
    };
    if status == NO_ERR {
        Ok(())
    } else {
        Err(status)
    }
}

pub enum DeviceFilter {
    ExcludeCubebAggregateAndVPIO,
    IncludeAll,
}
pub fn test_get_all_devices(filter: DeviceFilter) -> Vec<AudioObjectID> {
    let mut devices = Vec::new();
    let address = AudioObjectPropertyAddress {
        mSelector: kAudioHardwarePropertyDevices,
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };
    let mut size: usize = 0;
    let status = unsafe {
        AudioObjectGetPropertyDataSize(
            kAudioObjectSystemObject,
            &address,
            0,
            ptr::null(),
            &mut size as *mut usize as *mut u32,
        )
    };
    // size will be 0 if there is no device at all.
    if status != NO_ERR || size == 0 {
        return devices;
    }
    assert_eq!(size % mem::size_of::<AudioObjectID>(), 0);
    let elements = size / mem::size_of::<AudioObjectID>();
    devices.resize(elements, kAudioObjectUnknown);
    let status = unsafe {
        AudioObjectGetPropertyData(
            kAudioObjectSystemObject,
            &address,
            0,
            ptr::null(),
            &mut size as *mut usize as *mut u32,
            devices.as_mut_ptr() as *mut c_void,
        )
    };
    if status != NO_ERR {
        devices.clear();
        return devices;
    }
    for device in devices.iter() {
        assert_ne!(*device, kAudioObjectUnknown);
    }

    match filter {
        DeviceFilter::ExcludeCubebAggregateAndVPIO => {
            devices.retain(|&device| {
                if let Ok(uid) = get_device_global_uid(device) {
                    let uid = uid.into_string();
                    !uid.contains(PRIVATE_AGGREGATE_DEVICE_NAME)
                        && !uid.contains(VOICEPROCESSING_AGGREGATE_DEVICE_NAME)
                } else {
                    true
                }
            });
        }
        _ => {}
    }

    devices
}

pub fn test_get_devices_in_scope(scope: Scope) -> Vec<AudioObjectID> {
    let mut devices = test_get_all_devices(DeviceFilter::ExcludeCubebAggregateAndVPIO);
    devices.retain(|device| test_device_in_scope(*device, scope.clone()));
    devices
}

pub fn get_devices_info_in_scope(scope: Scope) -> Vec<TestDeviceInfo> {
    fn print_info(info: &TestDeviceInfo) {
        println!("{:>4}: {}\n\tuid: {}", info.id, info.label, info.uid);
    }

    println!(
        "\n{:?} devices\n\
         --------------------",
        scope
    );

    let mut infos = vec![];
    let devices = test_get_devices_in_scope(scope.clone());
    for device in devices {
        infos.push(TestDeviceInfo::new(device, scope.clone()));
        print_info(infos.last().unwrap());
    }
    println!();

    infos
}

#[derive(Debug)]
pub struct TestDeviceInfo {
    pub id: AudioObjectID,
    pub label: String,
    pub uid: String,
}
impl TestDeviceInfo {
    pub fn new(id: AudioObjectID, scope: Scope) -> Self {
        Self {
            id,
            label: Self::get_label(id, scope.clone()),
            uid: Self::get_uid(id, scope),
        }
    }

    fn get_label(id: AudioObjectID, scope: Scope) -> String {
        match get_device_uid(id, scope.into()) {
            Ok(uid) => uid.into_string(),
            Err(status) => format!("Unknow. Error: {}", status).to_string(),
        }
    }

    fn get_uid(id: AudioObjectID, scope: Scope) -> String {
        match get_device_label(id, scope.into()) {
            Ok(label) => label.into_string(),
            Err(status) => format!("Unknown. Error: {}", status).to_string(),
        }
    }
}

pub fn test_device_channels_in_scope(
    id: AudioObjectID,
    scope: Scope,
) -> std::result::Result<u32, OSStatus> {
    let address = AudioObjectPropertyAddress {
        mSelector: kAudioDevicePropertyStreamConfiguration,
        mScope: match scope {
            Scope::Input => kAudioDevicePropertyScopeInput,
            Scope::Output => kAudioDevicePropertyScopeOutput,
        },
        mElement: kAudioObjectPropertyElementMaster,
    };
    let mut size: usize = 0;
    let status = unsafe {
        AudioObjectGetPropertyDataSize(
            id,
            &address,
            0,
            ptr::null(),
            &mut size as *mut usize as *mut u32,
        )
    };
    if status != NO_ERR {
        return Err(status);
    }
    if size == 0 {
        return Ok(0);
    }
    let byte_len = size / mem::size_of::<u8>();
    let mut bytes = vec![0u8; byte_len];
    let status = unsafe {
        AudioObjectGetPropertyData(
            id,
            &address,
            0,
            ptr::null(),
            &mut size as *mut usize as *mut u32,
            bytes.as_mut_ptr() as *mut c_void,
        )
    };
    if status != NO_ERR {
        return Err(status);
    }
    let buf_list = unsafe { &*(bytes.as_mut_ptr() as *mut AudioBufferList) };
    let buf_len = buf_list.mNumberBuffers as usize;
    if buf_len == 0 {
        return Ok(0);
    }
    let buf_ptr = buf_list.mBuffers.as_ptr() as *const AudioBuffer;
    let buffers = unsafe { slice::from_raw_parts(buf_ptr, buf_len) };
    let mut channels: u32 = 0;
    for buffer in buffers {
        channels += buffer.mNumberChannels;
    }
    Ok(channels)
}

pub fn test_device_in_scope(id: AudioObjectID, scope: Scope) -> bool {
    let channels = test_device_channels_in_scope(id, scope);
    channels.is_ok() && channels.unwrap() > 0
}

pub fn test_get_all_onwed_devices(id: AudioDeviceID) -> Vec<AudioObjectID> {
    assert_ne!(id, kAudioObjectUnknown);

    let address = AudioObjectPropertyAddress {
        mSelector: kAudioObjectPropertyOwnedObjects,
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };

    let qualifier_data_size = mem::size_of::<AudioObjectID>();
    let class_id: AudioClassID = kAudioSubDeviceClassID;
    let qualifier_data = &class_id;
    let mut size: usize = 0;

    unsafe {
        assert_eq!(
            AudioObjectGetPropertyDataSize(
                id,
                &address,
                qualifier_data_size as u32,
                qualifier_data as *const u32 as *const c_void,
                &mut size as *mut usize as *mut u32
            ),
            NO_ERR
        );
    }
    assert_ne!(size, 0);

    let elements = size / mem::size_of::<AudioObjectID>();
    let mut devices: Vec<AudioObjectID> = allocate_array(elements);

    unsafe {
        assert_eq!(
            AudioObjectGetPropertyData(
                id,
                &address,
                qualifier_data_size as u32,
                qualifier_data as *const u32 as *const c_void,
                &mut size as *mut usize as *mut u32,
                devices.as_mut_ptr() as *mut c_void
            ),
            NO_ERR
        );
    }

    devices
}

pub fn test_get_master_device(id: AudioObjectID) -> String {
    assert_ne!(id, kAudioObjectUnknown);

    let address = AudioObjectPropertyAddress {
        mSelector: kAudioAggregateDevicePropertyMasterSubDevice,
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };

    let mut master: CFStringRef = ptr::null_mut();
    let mut size = mem::size_of::<CFStringRef>();
    assert_eq!(
        audio_object_get_property_data(id, &address, &mut size, &mut master),
        NO_ERR
    );
    assert!(!master.is_null());

    let master = StringRef::new(master as _);
    master.into_string()
}

pub fn test_get_drift_compensations(id: AudioObjectID) -> std::result::Result<u32, OSStatus> {
    let address = AudioObjectPropertyAddress {
        mSelector: kAudioSubDevicePropertyDriftCompensation,
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };
    let mut size = mem::size_of::<u32>();
    let mut compensation = u32::max_value();
    let status = unsafe {
        AudioObjectGetPropertyData(
            id,
            &address,
            0,
            ptr::null(),
            &mut size as *mut usize as *mut u32,
            &mut compensation as *mut u32 as *mut c_void,
        )
    };
    if status == NO_ERR {
        Ok(compensation)
    } else {
        Err(status)
    }
}

pub fn test_audiounit_scope_is_enabled(unit: AudioUnit, scope: Scope) -> bool {
    assert!(!unit.is_null());
    let mut has_io: UInt32 = 0;
    let (scope, element) = match scope {
        Scope::Input => (kAudioUnitScope_Input, AU_IN_BUS),
        Scope::Output => (kAudioUnitScope_Output, AU_OUT_BUS),
    };
    let mut size = mem::size_of::<UInt32>();
    assert_eq!(
        audio_unit_get_property(
            unit,
            kAudioOutputUnitProperty_HasIO,
            scope,
            element,
            &mut has_io,
            &mut size
        ),
        NO_ERR
    );
    has_io != 0
}

pub fn test_audiounit_get_buffer_frame_size(
    unit: AudioUnit,
    scope: Scope,
    prop_scope: PropertyScope,
) -> std::result::Result<u32, OSStatus> {
    let element = match scope {
        Scope::Input => AU_IN_BUS,
        Scope::Output => AU_OUT_BUS,
    };
    let prop_scope = match prop_scope {
        PropertyScope::Input => kAudioUnitScope_Input,
        PropertyScope::Output => kAudioUnitScope_Output,
    };
    let mut buffer_frames: u32 = 0;
    let mut size = mem::size_of::<u32>();
    let status = unsafe {
        AudioUnitGetProperty(
            unit,
            kAudioDevicePropertyBufferFrameSize,
            prop_scope,
            element,
            &mut buffer_frames as *mut u32 as *mut c_void,
            &mut size as *mut usize as *mut u32,
        )
    };
    if status == NO_ERR {
        Ok(buffer_frames)
    } else {
        Err(status)
    }
}

// Surprisingly it's ok to set
//   1. a unknown device
//   2. a non-input/non-output device
//   3. the current default input/output device
// as the new default input/output device by apple's API. We need to check the above things by ourselves.
// This function returns an Ok containing the previous default device id on success.
// Otherwise, it returns an Err containing the error code with OSStatus type
pub fn test_set_default_device(
    device: AudioObjectID,
    scope: Scope,
) -> std::result::Result<AudioObjectID, OSStatus> {
    assert!(test_device_in_scope(device, scope.clone()));
    let default = test_get_default_device(scope.clone()).unwrap();
    if default == device {
        // Do nothing if device is already the default device
        return Ok(device);
    }

    let address = AudioObjectPropertyAddress {
        mSelector: match scope {
            Scope::Input => kAudioHardwarePropertyDefaultInputDevice,
            Scope::Output => kAudioHardwarePropertyDefaultOutputDevice,
        },
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };
    let size = mem::size_of::<AudioObjectID>();
    let status = unsafe {
        AudioObjectSetPropertyData(
            kAudioObjectSystemObject,
            &address,
            0,
            ptr::null(),
            size as u32,
            &device as *const AudioObjectID as *const c_void,
        )
    };
    let new_default = test_get_default_device(scope.clone()).unwrap();
    if new_default == default {
        Err(-1)
    } else if status == NO_ERR {
        Ok(default)
    } else {
        Err(status)
    }
}

pub struct TestDeviceSwitcher {
    scope: Scope,
    devices: Vec<AudioObjectID>,
    current_device_index: usize,
}

impl TestDeviceSwitcher {
    pub fn new(scope: Scope) -> Self {
        let infos = get_devices_info_in_scope(scope.clone());
        let devices: Vec<AudioObjectID> = infos.into_iter().map(|info| info.id).collect();
        let current = test_get_default_device(scope.clone()).unwrap();
        let index = devices
            .iter()
            .position(|device| *device == current)
            .unwrap();
        Self {
            scope,
            devices,
            current_device_index: index,
        }
    }

    pub fn next(&mut self) {
        let current = self.devices[self.current_device_index];
        let next_index = (self.current_device_index + 1) % self.devices.len();
        let next = self.devices[next_index];
        println!(
            "Switch device for {:?}: {} -> {}",
            self.scope, current, next
        );
        match self.set_device(next) {
            Ok(prev) => {
                assert_eq!(prev, current);
                self.current_device_index = next_index;
            }
            _ => {
                self.devices.remove(next_index);
                if next_index < self.current_device_index {
                    self.current_device_index -= 1;
                }
                self.next();
            }
        }
    }

    fn set_device(&self, device: AudioObjectID) -> std::result::Result<AudioObjectID, OSStatus> {
        test_set_default_device(device, self.scope.clone())
    }
}

pub fn test_create_device_change_listener<F>(scope: Scope, listener: F) -> TestPropertyListener<F>
where
    F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus,
{
    let address = AudioObjectPropertyAddress {
        mSelector: match scope {
            Scope::Input => kAudioHardwarePropertyDefaultInputDevice,
            Scope::Output => kAudioHardwarePropertyDefaultOutputDevice,
        },
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMaster,
    };
    TestPropertyListener::new(kAudioObjectSystemObject, address, listener)
}

pub struct TestPropertyListener<F>
where
    F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus,
{
    device: AudioObjectID,
    property: AudioObjectPropertyAddress,
    callback: F,
}

impl<F> TestPropertyListener<F>
where
    F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus,
{
    pub fn new(device: AudioObjectID, property: AudioObjectPropertyAddress, callback: F) -> Self {
        Self {
            device,
            property,
            callback,
        }
    }

    pub fn start(&self) -> std::result::Result<(), OSStatus> {
        let status = unsafe {
            AudioObjectAddPropertyListener(
                self.device,
                &self.property,
                Some(Self::render),
                self as *const Self as *mut c_void,
            )
        };
        if status == NO_ERR {
            Ok(())
        } else {
            Err(status)
        }
    }

    pub fn stop(&self) -> std::result::Result<(), OSStatus> {
        let status = unsafe {
            AudioObjectRemovePropertyListener(
                self.device,
                &self.property,
                Some(Self::render),
                self as *const Self as *mut c_void,
            )
        };
        if status == NO_ERR {
            Ok(())
        } else {
            Err(status)
        }
    }

    extern "C" fn render(
        id: AudioObjectID,
        number_of_addresses: u32,
        addresses: *const AudioObjectPropertyAddress,
        data: *mut c_void,
    ) -> OSStatus {
        let listener = unsafe { &*(data as *mut Self) };
        assert_eq!(id, listener.device);
        let addrs = unsafe { slice::from_raw_parts(addresses, number_of_addresses as usize) };
        (listener.callback)(addrs)
    }
}

impl<F> Drop for TestPropertyListener<F>
where
    F: Fn(&[AudioObjectPropertyAddress]) -> OSStatus,
{
    fn drop(&mut self) {
        self.stop();
    }
}

// TODO: It doesn't work if default input or output is an aggregate device! Probably we need to do
//       the same thing as what audiounit_set_aggregate_sub_device_list does.
#[derive(Debug)]
pub struct TestDevicePlugger {
    scope: Scope,
    plugin_id: AudioObjectID,
    device_id: AudioObjectID,
}

impl TestDevicePlugger {
    pub fn new(scope: Scope) -> std::result::Result<Self, OSStatus> {
        let plugin_id = Self::get_system_plugin_id()?;
        Ok(Self {
            scope,
            plugin_id,
            device_id: kAudioObjectUnknown,
        })
    }

    pub fn get_device_id(&self) -> AudioObjectID {
        self.device_id
    }

    pub fn plug(&mut self) -> std::result::Result<(), OSStatus> {
        self.device_id = self.create_aggregate_device()?;
        Ok(())
    }

    pub fn unplug(&mut self) -> std::result::Result<(), OSStatus> {
        self.destroy_aggregate_device()
    }

    fn is_plugging(&self) -> bool {
        self.device_id != kAudioObjectUnknown
    }

    fn destroy_aggregate_device(&mut self) -> std::result::Result<(), OSStatus> {
        assert_ne!(self.plugin_id, kAudioObjectUnknown);
        assert_ne!(self.device_id, kAudioObjectUnknown);

        let address = AudioObjectPropertyAddress {
            mSelector: kAudioPlugInDestroyAggregateDevice,
            mScope: kAudioObjectPropertyScopeGlobal,
            mElement: kAudioObjectPropertyElementMaster,
        };

        let mut size: usize = 0;
        let status = unsafe {
            AudioObjectGetPropertyDataSize(
                self.plugin_id,
                &address,
                0,
                ptr::null(),
                &mut size as *mut usize as *mut u32,
            )
        };
        if status != NO_ERR {
            return Err(status);
        }
        assert_ne!(size, 0);

        let status = unsafe {
            // This call can simulate removing a device.
            AudioObjectGetPropertyData(
                self.plugin_id,
                &address,
                0,
                ptr::null(),
                &mut size as *mut usize as *mut u32,
                &mut self.device_id as *mut AudioDeviceID as *mut c_void,
            )
        };
        if status == NO_ERR {
            self.device_id = kAudioObjectUnknown;
            Ok(())
        } else {
            Err(status)
        }
    }

    fn create_aggregate_device(&self) -> std::result::Result<AudioObjectID, OSStatus> {
        use std::time::{SystemTime, UNIX_EPOCH};

        const TEST_AGGREGATE_DEVICE_NAME: &str = "TestAggregateDevice";

        assert_ne!(self.plugin_id, kAudioObjectUnknown);

        let sub_devices = Self::get_sub_devices(self.scope.clone());
        if sub_devices.is_none() {
            return Err(kAudioCodecUnspecifiedError as OSStatus);
        }
        let sub_devices = sub_devices.unwrap();

        let address = AudioObjectPropertyAddress {
            mSelector: kAudioPlugInCreateAggregateDevice,
            mScope: kAudioObjectPropertyScopeGlobal,
            mElement: kAudioObjectPropertyElementMaster,
        };

        let mut size: usize = 0;
        let status = unsafe {
            AudioObjectGetPropertyDataSize(
                self.plugin_id,
                &address,
                0,
                ptr::null(),
                &mut size as *mut usize as *mut u32,
            )
        };
        if status != NO_ERR {
            return Err(status);
        }
        assert_ne!(size, 0);

        let sys_time = SystemTime::now();
        let time_id = sys_time.duration_since(UNIX_EPOCH).unwrap().as_nanos();
        let device_name = format!("{}_{}", TEST_AGGREGATE_DEVICE_NAME, time_id);
        let device_uid = format!("org.mozilla.{}", device_name);

        let mut device_id = kAudioObjectUnknown;
        let status = unsafe {
            let device_dict = CFDictionaryCreateMutable(
                kCFAllocatorDefault,
                0,
                &kCFTypeDictionaryKeyCallBacks,
                &kCFTypeDictionaryValueCallBacks,
            );

            // Set the name of this device.
            let device_name = cfstringref_from_string(&device_name);
            CFDictionaryAddValue(
                device_dict,
                cfstringref_from_static_string(AGGREGATE_DEVICE_NAME_KEY) as *const c_void,
                device_name as *const c_void,
            );
            CFRelease(device_name as *const c_void);

            // Set the uid of this device.
            let device_uid = cfstringref_from_string(&device_uid);
            CFDictionaryAddValue(
                device_dict,
                cfstringref_from_static_string(AGGREGATE_DEVICE_UID_KEY) as *const c_void,
                device_uid as *const c_void,
            );
            CFRelease(device_uid as *const c_void);

            // Make this device NOT private to the process creating it.
            // On MacOS 14 devicechange events are not triggered when it is private.
            let private_value: i32 = 0;
            let device_private_key = CFNumberCreate(
                kCFAllocatorDefault,
                i64::from(kCFNumberIntType),
                &private_value as *const i32 as *const c_void,
            );
            CFDictionaryAddValue(
                device_dict,
                cfstringref_from_static_string(AGGREGATE_DEVICE_PRIVATE_KEY) as *const c_void,
                device_private_key as *const c_void,
            );
            CFRelease(device_private_key as *const c_void);

            // Set this device to be a stacked aggregate (i.e. multi-output device).
            let stacked_value: i32 = 0; // 1 for normal aggregate device.
            let device_stacked_key = CFNumberCreate(
                kCFAllocatorDefault,
                i64::from(kCFNumberIntType),
                &stacked_value as *const i32 as *const c_void,
            );
            CFDictionaryAddValue(
                device_dict,
                cfstringref_from_static_string(AGGREGATE_DEVICE_STACKED_KEY) as *const c_void,
                device_stacked_key as *const c_void,
            );
            CFRelease(device_stacked_key as *const c_void);

            // Set sub devices for this device.
            CFDictionaryAddValue(
                device_dict,
                cfstringref_from_static_string(AGGREGATE_DEVICE_SUB_DEVICE_LIST_KEY)
                    as *const c_void,
                sub_devices as *const c_void,
            );
            CFRelease(sub_devices as *const c_void);

            // This call can simulate adding a device.
            let status = AudioObjectGetPropertyData(
                self.plugin_id,
                &address,
                mem::size_of_val(&device_dict) as u32,
                &device_dict as *const CFMutableDictionaryRef as *const c_void,
                &mut size as *mut usize as *mut u32,
                &mut device_id as *mut AudioDeviceID as *mut c_void,
            );
            CFRelease(device_dict as *const c_void);
            status
        };
        if status == NO_ERR {
            assert_ne!(device_id, kAudioObjectUnknown);
            Ok(device_id)
        } else {
            Err(status)
        }
    }

    fn get_system_plugin_id() -> std::result::Result<AudioObjectID, OSStatus> {
        let address = AudioObjectPropertyAddress {
            mSelector: kAudioHardwarePropertyPlugInForBundleID,
            mScope: kAudioObjectPropertyScopeGlobal,
            mElement: kAudioObjectPropertyElementMaster,
        };

        let mut size: usize = 0;
        let status = unsafe {
            AudioObjectGetPropertyDataSize(
                kAudioObjectSystemObject,
                &address,
                0,
                ptr::null(),
                &mut size as *mut usize as *mut u32,
            )
        };
        if status != NO_ERR {
            return Err(status);
        }
        assert_ne!(size, 0);

        let mut plugin_id = kAudioObjectUnknown;
        let mut in_bundle_ref = cfstringref_from_static_string("com.apple.audio.CoreAudio");
        let mut translation_value = AudioValueTranslation {
            mInputData: &mut in_bundle_ref as *mut CFStringRef as *mut c_void,
            mInputDataSize: mem::size_of::<CFStringRef>() as u32,
            mOutputData: &mut plugin_id as *mut AudioObjectID as *mut c_void,
            mOutputDataSize: mem::size_of::<AudioObjectID>() as u32,
        };
        assert_eq!(size, mem::size_of_val(&translation_value));

        let status = unsafe {
            let status = AudioObjectGetPropertyData(
                kAudioObjectSystemObject,
                &address,
                0,
                ptr::null(),
                &mut size as *mut usize as *mut u32,
                &mut translation_value as *mut AudioValueTranslation as *mut c_void,
            );
            CFRelease(in_bundle_ref as *const c_void);
            status
        };
        if status == NO_ERR {
            assert_ne!(plugin_id, kAudioObjectUnknown);
            Ok(plugin_id)
        } else {
            Err(status)
        }
    }

    // TODO: This doesn't work as what we expect when the default deivce in the scope is an
    //       aggregate device. We should get the list of all the active sub devices and put
    //       them into the array, if the device is an aggregate device. See the code in
    //       AggregateDevice::get_sub_devices and audiounit_set_aggregate_sub_device_list.
    fn get_sub_devices(scope: Scope) -> Option<CFArrayRef> {
        let device = test_get_default_device(scope);
        device?;
        let device = device.unwrap();
        let uid = get_device_global_uid(device);
        if uid.is_err() {
            return None;
        }
        let uid = uid.unwrap();
        unsafe {
            let list = CFArrayCreateMutable(ptr::null(), 0, &kCFTypeArrayCallBacks);
            let sub_device_dict = CFDictionaryCreateMutable(
                ptr::null(),
                0,
                &kCFTypeDictionaryKeyCallBacks,
                &kCFTypeDictionaryValueCallBacks,
            );
            CFDictionaryAddValue(
                sub_device_dict,
                cfstringref_from_static_string(SUB_DEVICE_UID_KEY) as *const c_void,
                uid.get_raw() as *const c_void,
            );
            CFArrayAppendValue(list, sub_device_dict as *const c_void);
            CFRelease(sub_device_dict as *const c_void);
            Some(list)
        }
    }
}

impl Drop for TestDevicePlugger {
    fn drop(&mut self) {
        if self.is_plugging() {
            self.unplug();
        }
    }
}

// Test Templates
// ------------------------------------------------------------------------------------------------
pub fn test_ops_context_operation<F>(name: &'static str, operation: F)
where
    F: FnOnce(*mut ffi::cubeb),
{
    let name_c_string = CString::new(name).expect("Failed to create context name");
    let mut context = ptr::null_mut::<ffi::cubeb>();
    assert_eq!(
        unsafe { OPS.init.unwrap()(&mut context, name_c_string.as_ptr()) },
        ffi::CUBEB_OK
    );
    assert!(!context.is_null());
    operation(context);
    unsafe { OPS.destroy.unwrap()(context) }
}

// The in-out stream initializeed with different device will create an aggregate_device and
// result in firing device-collection-changed callbacks. Run in-out streams with tests
// capturing device-collection-changed callbacks may cause troubles.
pub fn test_ops_stream_operation<F>(
    name: &'static str,
    input_device: ffi::cubeb_devid,
    input_stream_params: *mut ffi::cubeb_stream_params,
    output_device: ffi::cubeb_devid,
    output_stream_params: *mut ffi::cubeb_stream_params,
    latency_frames: u32,
    data_callback: ffi::cubeb_data_callback,
    state_callback: ffi::cubeb_state_callback,
    user_ptr: *mut c_void,
    operation: F,
) where
    F: FnOnce(*mut ffi::cubeb_stream),
{
    test_ops_context_operation("context: stream operation", |context_ptr| {
        // Do nothing if there is no input/output device to perform input/output tests.
        if !input_stream_params.is_null() && test_get_default_device(Scope::Input).is_none() {
            println!("No input device to perform input tests for \"{}\".", name);
            return;
        }

        if !output_stream_params.is_null() && test_get_default_device(Scope::Output).is_none() {
            println!("No output device to perform output tests for \"{}\".", name);
            return;
        }

        let mut stream: *mut ffi::cubeb_stream = ptr::null_mut();
        let stream_name = CString::new(name).expect("Failed to create stream name");
        assert_eq!(
            unsafe {
                OPS.stream_init.unwrap()(
                    context_ptr,
                    &mut stream,
                    stream_name.as_ptr(),
                    input_device,
                    input_stream_params,
                    output_device,
                    output_stream_params,
                    latency_frames,
                    data_callback,
                    state_callback,
                    user_ptr,
                )
            },
            ffi::CUBEB_OK
        );
        assert!(!stream.is_null());
        operation(stream);
        unsafe {
            OPS.stream_destroy.unwrap()(stream);
        }
    });
}

pub fn test_get_raw_context<F>(operation: F)
where
    F: FnOnce(&mut AudioUnitContext),
{
    let mut context = AudioUnitContext::new();
    operation(&mut context);
}

pub fn test_get_default_raw_stream<F>(operation: F)
where
    F: FnOnce(&mut AudioUnitStream),
{
    test_get_raw_stream(ptr::null_mut(), None, None, 0, operation);
}

fn test_get_raw_stream<F>(
    user_ptr: *mut c_void,
    data_callback: ffi::cubeb_data_callback,
    state_callback: ffi::cubeb_state_callback,
    latency_frames: u32,
    operation: F,
) where
    F: FnOnce(&mut AudioUnitStream),
{
    let mut context = AudioUnitContext::new();

    // Add a stream to the context since we are about to create one.
    // AudioUnitStream::drop() will check the context has at least one stream.
    let global_latency_frames = context.update_latency_by_adding_stream(latency_frames);

    let mut stream = AudioUnitStream::new(
        &mut context,
        user_ptr,
        data_callback,
        state_callback,
        global_latency_frames.unwrap(),
    );
    stream.core_stream_data = CoreStreamData::new(&stream, None, None);

    operation(&mut stream);
}

pub fn test_get_stream_with_default_data_callback_by_type<F>(
    name: &'static str,
    stm_type: StreamType,
    input_device: Option<AudioObjectID>,
    output_device: Option<AudioObjectID>,
    state_callback: extern "C" fn(*mut ffi::cubeb_stream, *mut c_void, ffi::cubeb_state),
    data: *mut c_void,
    operation: F,
) where
    F: FnOnce(&mut AudioUnitStream),
{
    let mut input_params = get_dummy_stream_params(Scope::Input);
    let mut output_params = get_dummy_stream_params(Scope::Output);

    let in_params = if stm_type.contains(StreamType::INPUT) {
        &mut input_params as *mut ffi::cubeb_stream_params
    } else {
        ptr::null_mut()
    };
    let out_params = if stm_type.contains(StreamType::OUTPUT) {
        &mut output_params as *mut ffi::cubeb_stream_params
    } else {
        ptr::null_mut()
    };
    let in_device = if let Some(id) = input_device {
        id as ffi::cubeb_devid
    } else {
        ptr::null_mut()
    };
    let out_device = if let Some(id) = output_device {
        id as ffi::cubeb_devid
    } else {
        ptr::null_mut()
    };

    test_ops_stream_operation_with_default_data_callback(
        name,
        in_device,
        in_params,
        out_device,
        out_params,
        state_callback,
        data,
        |stream| {
            let stm = unsafe { &mut *(stream as *mut AudioUnitStream) };
            operation(stm);
        },
    );
}

bitflags! {
    pub struct StreamType: u8 {
        const INPUT = 0x01;
        const OUTPUT = 0x02;
        const DUPLEX = 0x03;
    }
}

fn get_dummy_stream_params(scope: Scope) -> 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 = ffi::CUBEB_STREAM_PREF_NONE;
    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
}

fn test_ops_stream_operation_with_default_data_callback<F>(
    name: &'static str,
    input_device: ffi::cubeb_devid,
    input_stream_params: *mut ffi::cubeb_stream_params,
    output_device: ffi::cubeb_devid,
    output_stream_params: *mut ffi::cubeb_stream_params,
    state_callback: extern "C" fn(*mut ffi::cubeb_stream, *mut c_void, ffi::cubeb_state),
    data: *mut c_void,
    operation: F,
) where
    F: FnOnce(*mut ffi::cubeb_stream),
{
    test_ops_stream_operation(
        name,
        input_device,
        input_stream_params,
        output_device,
        output_stream_params,
        4096, // TODO: Get latency by get_min_latency instead ?
        Some(noop_data_callback),
        Some(state_callback),
        data,
        operation,
    );
}