aboutsummaryrefslogtreecommitdiff
path: root/win_audio/src/win_audio_impl/wave_format.rs
blob: 0e6ed694849d03c3866615d1745b4458f57d18b3 (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
// 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::convert::TryInto;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;

use base::error;
use base::info;
use base::warn;
use base::Error;
use metrics::sys::WaveFormat as WaveFormatMetric;
use metrics::sys::WaveFormatDetails as WaveFormatDetailsMetric;
use metrics::sys::WaveFormatSubFormat as WaveFormatSubFormatMetric;
use metrics::MetricEventType;
use winapi::shared::guiddef::IsEqualGUID;
use winapi::shared::guiddef::GUID;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_ADPCM;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_ALAW;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_ANALOG;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_DRM;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_MPEG;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_MULAW;
use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_PCM;
use winapi::shared::mmreg::SPEAKER_FRONT_CENTER;
use winapi::shared::mmreg::SPEAKER_FRONT_LEFT;
use winapi::shared::mmreg::SPEAKER_FRONT_RIGHT;
use winapi::shared::mmreg::WAVEFORMATEX;
use winapi::shared::mmreg::WAVEFORMATEXTENSIBLE;
use winapi::shared::mmreg::WAVE_FORMAT_EXTENSIBLE;
use winapi::shared::mmreg::WAVE_FORMAT_IEEE_FLOAT;
use winapi::shared::winerror::S_FALSE;
use winapi::shared::winerror::S_OK;
use winapi::um::audioclient::IAudioClient;
use winapi::um::audiosessiontypes::AUDCLNT_SHAREMODE_SHARED;
#[cfg(not(test))]
use winapi::um::combaseapi::CoTaskMemFree;
use wio::com::ComPtr;

use crate::AudioSharedFormat;
use crate::WinAudioError;
use crate::MONO_CHANNEL_COUNT;
use crate::STEREO_CHANNEL_COUNT;

/// Wrapper around `WAVEFORMATEX` and `WAVEFORMATEXTENSIBLE` to hide some of the unsafe calls
/// that could be made.
pub enum WaveAudioFormat {
    /// Format where channels are capped at 2.
    WaveFormat(WAVEFORMATEX),
    /// Format where channels can be >2. (It can still have <=2 channels)
    WaveFormatExtensible(WAVEFORMATEXTENSIBLE),
}

pub(crate) enum AudioFormatEventType {
    RequestOk,
    ModifiedOk,
    Failed,
}

impl WaveAudioFormat {
    /// Wraps a WAVEFORMATEX pointer to make it's use more safe.
    ///
    /// # Safety
    /// Unsafe if `wave_format_ptr` is pointing to null. This function will assume it's not null
    /// and dereference it.
    /// Also `format_ptr` will be deallocated after this function completes, so it cannot be used.
    #[allow(clippy::let_and_return)]
    pub unsafe fn new(format_ptr: *mut WAVEFORMATEX) -> Self {
        let format_tag = { (*format_ptr).wFormatTag };
        let result = if format_tag != WAVE_FORMAT_EXTENSIBLE {
            warn!(
                "Default Mix Format does not have format_tag WAVE_FORMAT_EXTENSIBLE. It is: {}",
                format_tag
            );
            WaveAudioFormat::WaveFormat(*format_ptr)
        } else {
            WaveAudioFormat::WaveFormatExtensible(*(format_ptr as *const WAVEFORMATEXTENSIBLE))
        };

        // WAVEFORMATEX and WAVEFORMATEXTENSIBLE both implement the Copy trait, so they have been
        // copied to the WaveAudioFormat enum. Therefore, it is safe to free the memory
        // `format_ptr` is pointing to.
        // In a test, WAVEFORMATEX is initiated by us, not by Windows, so calling this function
        // could cause a STATUS_HEAP_CORRUPTION exception.
        #[cfg(not(test))]
        CoTaskMemFree(format_ptr as *mut std::ffi::c_void);

        result
    }

    pub fn get_num_channels(&self) -> u16 {
        match self {
            WaveAudioFormat::WaveFormat(wave_format) => wave_format.nChannels,
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                wave_format_extensible.Format.nChannels
            }
        }
    }

    // Modifies `WAVEFORMATEXTENSIBLE` to have the values passed into the function params.
    // Currently it should only modify the bit_depth if it's != 32 and the data format if it's not
    // float.
    pub fn modify_mix_format(&mut self, target_bit_depth: usize, ks_data_format: GUID) {
        let default_num_channels = self.get_num_channels();

        fn calc_avg_bytes_per_sec(num_channels: u16, bit_depth: u16, samples_per_sec: u32) -> u32 {
            num_channels as u32 * (bit_depth as u32 / 8) * samples_per_sec
        }

        fn calc_block_align(num_channels: u16, bit_depth: u16) -> u16 {
            (bit_depth / 8) * num_channels
        }

        match self {
            WaveAudioFormat::WaveFormat(wave_format) => {
                if default_num_channels > STEREO_CHANNEL_COUNT {
                    warn!("WAVEFORMATEX shouldn't have >2 channels.");
                }

                // Force the format to be the only supported format (32 bit float)
                if wave_format.wBitsPerSample != target_bit_depth as u16
                    || wave_format.wFormatTag != WAVE_FORMAT_IEEE_FLOAT
                {
                    wave_format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
                    wave_format.nChannels =
                        std::cmp::min(STEREO_CHANNEL_COUNT, default_num_channels);
                    wave_format.wBitsPerSample = target_bit_depth as u16;
                    wave_format.nAvgBytesPerSec = calc_avg_bytes_per_sec(
                        wave_format.nChannels,
                        wave_format.wBitsPerSample,
                        wave_format.nSamplesPerSec,
                    );
                    wave_format.nBlockAlign =
                        calc_block_align(wave_format.nChannels, wave_format.wBitsPerSample);
                }
            }
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                // WAVE_FORMAT_EXTENSIBLE uses the #[repr(packed)] flag so the compiler might
                // unalign the fields. Thus, the fields will be copied to a local variable to
                // prevent segfaults. For more information:
                // https://github.com/rust-lang/rust/issues/46043
                let sub_format = wave_format_extensible.SubFormat;

                if wave_format_extensible.Format.wBitsPerSample != target_bit_depth as u16
                    || !IsEqualGUID(&sub_format, &ks_data_format)
                {
                    // wFormatTag won't be changed
                    wave_format_extensible.Format.nChannels = default_num_channels;
                    wave_format_extensible.Format.wBitsPerSample = target_bit_depth as u16;
                    // nSamplesPerSec should stay the same
                    // Calculated with a bit depth of 32bits
                    wave_format_extensible.Format.nAvgBytesPerSec = calc_avg_bytes_per_sec(
                        wave_format_extensible.Format.nChannels,
                        wave_format_extensible.Format.wBitsPerSample,
                        wave_format_extensible.Format.nSamplesPerSec,
                    );
                    wave_format_extensible.Format.nBlockAlign = calc_block_align(
                        wave_format_extensible.Format.nChannels,
                        wave_format_extensible.Format.wBitsPerSample,
                    );
                    // 22 is the size typically used when the format tag is WAVE_FORMAT_EXTENSIBLE.
                    // Since the `Initialize` syscall takes in a WAVEFORMATEX, this tells Windows
                    // how many bytes are left after the `Format` field
                    // (ie. Samples, dwChannelMask, SubFormat) so that it can cast to
                    // WAVEFORMATEXTENSIBLE safely.
                    wave_format_extensible.Format.cbSize = 22;
                    wave_format_extensible.Samples = target_bit_depth as u16;
                    let n_channels = wave_format_extensible.Format.nChannels;
                    // The channel masks are defined here:
                    // https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible#remarks
                    wave_format_extensible.dwChannelMask = match n_channels {
                        STEREO_CHANNEL_COUNT => SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
                        MONO_CHANNEL_COUNT => SPEAKER_FRONT_CENTER,
                        _ => {
                            // Don't change channel mask if it's >2 channels.
                            wave_format_extensible.dwChannelMask
                        }
                    };
                    wave_format_extensible.SubFormat = ks_data_format;
                }
            }
        }
    }

    pub fn as_ptr(&self) -> *const WAVEFORMATEX {
        match self {
            WaveAudioFormat::WaveFormat(wave_format) => wave_format as *const WAVEFORMATEX,
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                wave_format_extensible as *const _ as *const WAVEFORMATEX
            }
        }
    }

    pub fn get_shared_audio_engine_period_in_frames(
        &self,
        shared_default_size_in_100nanoseconds: f64,
    ) -> usize {
        // a 100 nanosecond unit is 1 * 10^-7 seconds
        //
        // To convert a 100nanoseconds value to # of frames in a period, we multiple by the
        // frame rate (nSamplesPerSec. Sample rate == Frame rate) and then divide by 10000000
        // in order to convert 100nanoseconds to seconds.
        let samples_per_sec = match self {
            WaveAudioFormat::WaveFormat(wave_format) => wave_format.nSamplesPerSec,
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                wave_format_extensible.Format.nSamplesPerSec
            }
        };

        ((samples_per_sec as f64 * shared_default_size_in_100nanoseconds) / 10000000.0).ceil()
            as usize
    }

    pub fn create_audio_shared_format(
        &self,
        shared_audio_engine_period_in_frames: usize,
    ) -> AudioSharedFormat {
        match self {
            WaveAudioFormat::WaveFormat(wave_format) => AudioSharedFormat {
                bit_depth: wave_format.wBitsPerSample as usize,
                frame_rate: wave_format.nSamplesPerSec as usize,
                shared_audio_engine_period_in_frames,
                channels: wave_format.nChannels as usize,
                channel_mask: None,
            },
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => AudioSharedFormat {
                bit_depth: wave_format_extensible.Format.wBitsPerSample as usize,
                frame_rate: wave_format_extensible.Format.nSamplesPerSec as usize,
                shared_audio_engine_period_in_frames,
                channels: wave_format_extensible.Format.nChannels as usize,
                channel_mask: Some(wave_format_extensible.dwChannelMask),
            },
        }
    }

    #[cfg(test)]
    fn take_waveformatex(self) -> WAVEFORMATEX {
        match self {
            WaveAudioFormat::WaveFormat(wave_format) => wave_format,
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                // SAFETY: `wave_format_extensible` can't be a null pointer, otherwise the
                // constructor would've failed. This will also give the caller ownership of this
                // struct.
                unsafe { *(&wave_format_extensible as *const _ as *const WAVEFORMATEX) }
            }
        }
    }

    #[cfg(test)]
    fn take_waveformatextensible(self) -> WAVEFORMATEXTENSIBLE {
        match self {
            WaveAudioFormat::WaveFormat(_wave_format) => {
                panic!("Format is WAVEFORMATEX. Can't convert to WAVEFORMATEXTENSBILE.")
            }
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => wave_format_extensible,
        }
    }
}

impl Debug for WaveAudioFormat {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let res = match self {
            WaveAudioFormat::WaveFormat(wave_format) => {
                format!(
                    "wFormatTag: {}, \nnChannels: {}, \nnSamplesPerSec: {}, \nnAvgBytesPerSec: \
                    {}, \nnBlockAlign: {}, \nwBitsPerSample: {}, \ncbSize: {}",
                    { wave_format.wFormatTag },
                    { wave_format.nChannels },
                    { wave_format.nSamplesPerSec },
                    { wave_format.nAvgBytesPerSec },
                    { wave_format.nBlockAlign },
                    { wave_format.wBitsPerSample },
                    { wave_format.cbSize },
                )
            }
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                let audio_engine_format = format!(
                    "wFormatTag: {}, \nnChannels: {}, \nnSamplesPerSec: {}, \nnAvgBytesPerSec: \
                    {}, \nnBlockAlign: {}, \nwBitsPerSample: {}, \ncbSize: {}",
                    { wave_format_extensible.Format.wFormatTag },
                    { wave_format_extensible.Format.nChannels },
                    { wave_format_extensible.Format.nSamplesPerSec },
                    { wave_format_extensible.Format.nAvgBytesPerSec },
                    { wave_format_extensible.Format.nBlockAlign },
                    { wave_format_extensible.Format.wBitsPerSample },
                    { wave_format_extensible.Format.cbSize },
                );

                let subformat = wave_format_extensible.SubFormat;

                // TODO(b/240186720): Passing in `KSDATAFORMAT_SUBTYPE_PCM` will cause a
                // freeze. IsEqualGUID is unsafe even though it isn't marked as such. Look into
                // fixing or possibily write our own, that works.
                //
                // This check would be a nice to have, but not necessary. Right now, the subformat
                // used will always be `IEEE_FLOAT`, so this check will be useless if nothing
                // changes.
                //
                // if !IsEqualGUID(
                //     &{ wave_format_extensible.SubFormat },
                //     &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
                // ) {
                //     warn!("Audio Engine format is NOT IEEE FLOAT");
                // }

                let audio_engine_extensible_format = format!(
                    "\nSamples: {}, \ndwChannelMask: {}, \nSubFormat: {}-{}-{}-{:?}",
                    { wave_format_extensible.Samples },
                    { wave_format_extensible.dwChannelMask },
                    subformat.Data1,
                    subformat.Data2,
                    subformat.Data3,
                    subformat.Data4,
                );

                format!("{}{}", audio_engine_format, audio_engine_extensible_format)
            }
        };
        write!(f, "{}", res)
    }
}

#[cfg(test)]
impl PartialEq for WaveAudioFormat {
    fn eq(&self, other: &Self) -> bool {
        if std::mem::discriminant(self) != std::mem::discriminant(other) {
            return false;
        }

        fn are_formats_same(
            wave_format_pointer: *const u8,
            other_format_pointer: *const u8,
            cb_size: usize,
        ) -> bool {
            // SAFETY: wave_format_pointer is valid for the given size.
            let wave_format_bytes: &[u8] = unsafe {
                std::slice::from_raw_parts(
                    wave_format_pointer,
                    std::mem::size_of::<WAVEFORMATEX>() + cb_size,
                )
            };
            // SAFETY: other_format_pointer is valid for the given size.
            let other_bytes: &[u8] = unsafe {
                std::slice::from_raw_parts(
                    other_format_pointer,
                    std::mem::size_of::<WAVEFORMATEX>() + cb_size,
                )
            };

            !wave_format_bytes
                .iter()
                .zip(other_bytes)
                .map(|(x, y)| x.cmp(y))
                .any(|ord| ord != std::cmp::Ordering::Equal)
        }

        match self {
            WaveAudioFormat::WaveFormat(wave_format) => match other {
                WaveAudioFormat::WaveFormat(other_wave_format) => {
                    if wave_format.cbSize != other_wave_format.cbSize {
                        return false;
                    }
                    are_formats_same(
                        wave_format as *const _ as *const u8,
                        other_wave_format as *const _ as *const u8,
                        wave_format.cbSize as usize,
                    )
                }
                WaveAudioFormat::WaveFormatExtensible(_) => unreachable!(),
            },
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => match other {
                WaveAudioFormat::WaveFormatExtensible(other_wave_format_extensible) => {
                    if wave_format_extensible.Format.cbSize
                        != other_wave_format_extensible.Format.cbSize
                    {
                        return false;
                    }
                    are_formats_same(
                        wave_format_extensible as *const _ as *const u8,
                        other_wave_format_extensible as *const _ as *const u8,
                        wave_format_extensible.Format.cbSize as usize,
                    )
                }
                WaveAudioFormat::WaveFormat(_) => unreachable!(),
            },
        }
    }
}

impl From<&WaveAudioFormat> for WaveFormatMetric {
    fn from(format: &WaveAudioFormat) -> WaveFormatMetric {
        match format {
            WaveAudioFormat::WaveFormat(wave_format) => WaveFormatMetric {
                format_tag: wave_format.wFormatTag.into(),
                channels: wave_format.nChannels.into(),
                samples_per_sec: wave_format
                    .nSamplesPerSec
                    .try_into()
                    .expect("Failed to cast nSamplesPerSec to i32"),
                avg_bytes_per_sec: wave_format
                    .nAvgBytesPerSec
                    .try_into()
                    .expect("Failed to cast nAvgBytesPerSec"),
                block_align: wave_format.nBlockAlign.into(),
                bits_per_sample: wave_format.wBitsPerSample.into(),
                size_bytes: wave_format.cbSize.into(),
                samples: None,
                channel_mask: None,
                sub_format: None,
            },
            WaveAudioFormat::WaveFormatExtensible(wave_format_extensible) => {
                let sub_format = wave_format_extensible.SubFormat;
                WaveFormatMetric {
                    format_tag: wave_format_extensible.Format.wFormatTag.into(),
                    channels: wave_format_extensible.Format.nChannels.into(),
                    samples_per_sec: wave_format_extensible
                        .Format
                        .nSamplesPerSec
                        .try_into()
                        .expect("Failed to cast nSamplesPerSec to i32"),
                    avg_bytes_per_sec: wave_format_extensible
                        .Format
                        .nAvgBytesPerSec
                        .try_into()
                        .expect("Failed to cast nAvgBytesPerSec"),
                    block_align: wave_format_extensible.Format.nBlockAlign.into(),
                    bits_per_sample: wave_format_extensible.Format.wBitsPerSample.into(),
                    size_bytes: wave_format_extensible.Format.cbSize.into(),
                    samples: Some(wave_format_extensible.Samples.into()),
                    channel_mask: Some(wave_format_extensible.dwChannelMask.into()),
                    sub_format: Some(GuidWrapper(&sub_format).into()),
                }
            }
        }
    }
}

/// Get an audio format that will be accepted by the audio client. In terms of bit depth, the goal
/// is to always get a 32bit float format.
pub(crate) fn get_valid_mix_format(
    audio_client: &ComPtr<IAudioClient>,
) -> Result<WaveAudioFormat, WinAudioError> {
    // SAFETY: `format_ptr` is owned by this unsafe block. `format_ptr` is guarenteed to
    // be not null by the time it reached `WaveAudioFormat::new` (check_hresult! should make
    // sure of that), which is also release the pointer passed in.
    let mut format = unsafe {
        let mut format_ptr: *mut WAVEFORMATEX = std::ptr::null_mut();
        let hr = audio_client.GetMixFormat(&mut format_ptr);
        check_hresult!(
            hr,
            WinAudioError::from(hr),
            "Failed to retrieve audio engine's shared format"
        )?;

        WaveAudioFormat::new(format_ptr)
    };

    let mut wave_format_details = WaveFormatDetailsMetric::default();
    let mut event_code = AudioFormatEventType::RequestOk;
    wave_format_details.requested = Some(WaveFormatMetric::from(&format));

    info!("Printing mix format from `GetMixFormat`:\n{:?}", format);
    const BIT_DEPTH: usize = 32;
    format.modify_mix_format(BIT_DEPTH, KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);

    let modified_wave_format = Some(WaveFormatMetric::from(&format));
    if modified_wave_format != wave_format_details.requested {
        wave_format_details.modified = modified_wave_format;
        event_code = AudioFormatEventType::ModifiedOk;
    }

    info!("Audio Engine Mix Format Used: \n{:?}", format);
    check_format(audio_client, &format, wave_format_details, event_code)?;

    Ok(format)
}

/// Checks to see if `format` is accepted by the audio client.
///
/// Exposed as crate public for testing.
pub(crate) fn check_format(
    audio_client: &IAudioClient,
    format: &WaveAudioFormat,
    mut wave_format_details: WaveFormatDetailsMetric,
    event_code: AudioFormatEventType,
) -> Result<(), WinAudioError> {
    let mut closest_match_format: *mut WAVEFORMATEX = std::ptr::null_mut();
    // SAFETY: All values passed into `IsFormatSupport` is owned by us and we will
    // guarentee they won't be dropped and are valid.
    let hr = unsafe {
        audio_client.IsFormatSupported(
            AUDCLNT_SHAREMODE_SHARED,
            format.as_ptr(),
            &mut closest_match_format,
        )
    };

    // If the audio engine does not support the format.
    if hr != S_OK {
        if hr == S_FALSE {
            // SAFETY: If the `hr` value is `S_FALSE`, then `IsFormatSupported` must've
            // given us a closest match.
            let closest_match_enum = unsafe { WaveAudioFormat::new(closest_match_format) };
            wave_format_details.closest_matched = Some(WaveFormatMetric::from(&closest_match_enum));

            error!(
                "Current audio format not supported, the closest format is:\n{:?}",
                closest_match_enum
            );
        } else {
            error!("IsFormatSupported failed with hr: {}", hr);
        }

        // Get last error here just incase `upload_metrics` causes an error.
        let last_error = Error::last();
        // TODO:(b/253509368): Only upload for audio rendering, since these metrics can't
        // differentiate between rendering and capture.
        upload_metrics(wave_format_details, AudioFormatEventType::Failed);

        Err(WinAudioError::WindowsError(hr, last_error))
    } else {
        upload_metrics(wave_format_details, event_code);

        Ok(())
    }
}

fn upload_metrics(details: WaveFormatDetailsMetric, event_type: AudioFormatEventType) {
    let event = match event_type {
        AudioFormatEventType::RequestOk => MetricEventType::AudioFormatRequestOk(details),
        AudioFormatEventType::ModifiedOk => MetricEventType::AudioFormatModifiedOk(details),
        AudioFormatEventType::Failed => MetricEventType::AudioFormatFailed(details),
    };
    metrics::log_event(event);
}

struct GuidWrapper<'a>(&'a GUID);

impl<'a> From<GuidWrapper<'a>> for WaveFormatSubFormatMetric {
    fn from(guid: GuidWrapper) -> WaveFormatSubFormatMetric {
        let guid = guid.0;
        if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_ANALOG) {
            WaveFormatSubFormatMetric::Analog
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_PCM) {
            WaveFormatSubFormatMetric::Pcm
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
            WaveFormatSubFormatMetric::IeeeFloat
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_DRM) {
            WaveFormatSubFormatMetric::Drm
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_ALAW) {
            WaveFormatSubFormatMetric::ALaw
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_MULAW) {
            WaveFormatSubFormatMetric::MuLaw
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_ADPCM) {
            WaveFormatSubFormatMetric::Adpcm
        } else if IsEqualGUID(guid, &KSDATAFORMAT_SUBTYPE_MPEG) {
            WaveFormatSubFormatMetric::Mpeg
        } else {
            WaveFormatSubFormatMetric::Invalid
        }
    }
}

#[cfg(test)]
mod tests {
    use winapi::shared::ksmedia::KSDATAFORMAT_SUBTYPE_PCM;
    use winapi::shared::mmreg::SPEAKER_BACK_LEFT;
    use winapi::shared::mmreg::SPEAKER_BACK_RIGHT;
    use winapi::shared::mmreg::SPEAKER_LOW_FREQUENCY;
    use winapi::shared::mmreg::SPEAKER_SIDE_LEFT;
    use winapi::shared::mmreg::SPEAKER_SIDE_RIGHT;
    use winapi::shared::mmreg::WAVE_FORMAT_PCM;

    use super::*;

    #[test]
    fn test_modify_mix_format() {
        // A typical 7.1 surround sound channel mask.
        const channel_mask_7_1: u32 = SPEAKER_FRONT_LEFT
            | SPEAKER_FRONT_RIGHT
            | SPEAKER_FRONT_CENTER
            | SPEAKER_LOW_FREQUENCY
            | SPEAKER_BACK_LEFT
            | SPEAKER_BACK_RIGHT
            | SPEAKER_SIDE_LEFT
            | SPEAKER_SIDE_RIGHT;

        let surround_sound_format = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 8,
                nSamplesPerSec: 44100,
                nAvgBytesPerSec: 1411200,
                nBlockAlign: 32,
                wBitsPerSample: 32,
                cbSize: 22,
            },
            Samples: 32,
            dwChannelMask: channel_mask_7_1,
            SubFormat: KSDATAFORMAT_SUBTYPE_PCM,
        };

        // SAFETY: `GetMixFormat` casts `WAVEFORMATEXTENSIBLE` into a `WAVEFORMATEX` like so.
        // Also this is casting from a bigger to a smaller struct, so it shouldn't be possible for
        // this contructor to access memory it shouldn't.
        let mut format = unsafe {
            WaveAudioFormat::new((&surround_sound_format) as *const _ as *mut WAVEFORMATEX)
        };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        // SAFETY: We know the format is originally a `WAVEFORMATEXTENSIBLE`.
        let surround_sound_format = format.take_waveformatextensible();

        // WAVE_FORMAT_EXTENSIBLE uses the #[repr(packed)] flag so the compiler might unalign
        // the fields. Thus, the fields will be copied to a local variable to prevent segfaults.
        // For more information: https://github.com/rust-lang/rust/issues/46043
        let format_tag = surround_sound_format.Format.wFormatTag;
        // We expect `SubFormat` to be IEEE float instead of PCM.
        // Everything else should remain the same.
        assert_eq!(format_tag, WAVE_FORMAT_EXTENSIBLE);
        let channels = surround_sound_format.Format.nChannels;
        assert_eq!(channels, 8);
        let samples_per_sec = surround_sound_format.Format.nSamplesPerSec;
        assert_eq!(samples_per_sec, 44100);
        let avg_bytes_per_sec = surround_sound_format.Format.nAvgBytesPerSec;
        assert_eq!(avg_bytes_per_sec, 1411200);
        let block_align = surround_sound_format.Format.nBlockAlign;
        assert_eq!(block_align, 32);
        let bits_per_samples = surround_sound_format.Format.wBitsPerSample;
        assert_eq!(bits_per_samples, 32);
        let size = surround_sound_format.Format.cbSize;
        assert_eq!(size, 22);
        let samples = surround_sound_format.Samples;
        assert_eq!(samples, 32);
        let channel_mask = surround_sound_format.dwChannelMask;
        assert_eq!(channel_mask, channel_mask_7_1);
        let sub_format = surround_sound_format.SubFormat;
        assert!(IsEqualGUID(&sub_format, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT));
    }

    #[test]
    fn test_waveformatex_ieee_modify_same_format() {
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_IEEE_FLOAT,
            nChannels: 2,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 384000,
            nBlockAlign: 8,
            wBitsPerSample: 32,
            cbSize: 0,
        };

        let mut format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        let result_format = format.take_waveformatex();

        assert_waveformatex_ieee(&result_format);
    }

    #[test]
    fn test_waveformatex_ieee_modify_different_format() {
        // I don't expect this format to show up ever, but it's possible so it's good to test.
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_IEEE_FLOAT,
            nChannels: 2,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 192000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        let mut format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        let result_format = format.take_waveformatex();

        assert_waveformatex_ieee(&result_format);
    }

    #[test]
    fn test_format_comparison_waveformatex_pass() {
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 1,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 4 * 48000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        let format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        let expected = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 1,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 4 * 48000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let expected = unsafe {
            WaveAudioFormat::new((&expected) as *const WAVEFORMATEX as *mut WAVEFORMATEX)
        };

        assert_eq!(expected, format);
    }

    #[test]
    fn test_format_comparison_waveformatextensible_pass() {
        let format = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 1,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 4 * 48000,
                nBlockAlign: 4,
                wBitsPerSample: 16,
                cbSize: 22,
            },
            Samples: 16,
            dwChannelMask: SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let format = unsafe {
            WaveAudioFormat::new((&format) as *const WAVEFORMATEXTENSIBLE as *mut WAVEFORMATEX)
        };

        let expected = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 1,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 4 * 48000,
                nBlockAlign: 4,
                wBitsPerSample: 16,
                cbSize: 22,
            },
            Samples: 16,
            dwChannelMask: SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let expected = unsafe {
            WaveAudioFormat::new((&expected) as *const WAVEFORMATEXTENSIBLE as *mut WAVEFORMATEX)
        };

        assert_eq!(expected, format);
    }

    #[test]
    fn test_format_comparison_waveformatex_fail() {
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 1,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 4 * 48000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        let format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        let expected = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            // The field below is the difference
            nChannels: 6,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 4 * 48000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let expected = unsafe {
            WaveAudioFormat::new((&expected) as *const WAVEFORMATEX as *mut WAVEFORMATEX)
        };

        assert_ne!(expected, format);
    }

    #[test]
    fn test_format_comparison_waveformatextensible_fail() {
        let format = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 1,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 4 * 48000,
                nBlockAlign: 4,
                wBitsPerSample: 16,
                cbSize: 22,
            },
            Samples: 16,
            dwChannelMask: SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let format = unsafe {
            WaveAudioFormat::new((&format) as *const WAVEFORMATEXTENSIBLE as *mut WAVEFORMATEX)
        };

        let expected = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 1,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 4 * 48000,
                nBlockAlign: 4,
                wBitsPerSample: 16,
                cbSize: 22,
            },
            Samples: 16,
            // The field below is the difference.
            dwChannelMask: SPEAKER_FRONT_CENTER | SPEAKER_BACK_LEFT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let expected = unsafe {
            WaveAudioFormat::new((&expected) as *const WAVEFORMATEXTENSIBLE as *mut WAVEFORMATEX)
        };

        assert_ne!(expected, format);
    }

    #[test]
    fn test_modify_mix_mono_channel_different_bit_depth_wave_format_extensible() {
        // Start with a mono channel and 16 bit depth format.
        let format = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 1,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 2 * 48000,
                nBlockAlign: 2,
                wBitsPerSample: 16,
                cbSize: 22,
            },
            Samples: 16,
            // Probably will never see a mask like this for two channels, but this is just testing
            // that it will get changed.
            dwChannelMask: SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let mut format = unsafe {
            WaveAudioFormat::new((&format) as *const WAVEFORMATEXTENSIBLE as *mut WAVEFORMATEX)
        };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        // The format should be converted to 32 bit depth and retain mono channel.
        let expected = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 1,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 4 * 48000, // Changed
                nBlockAlign: 4,             // Changed
                wBitsPerSample: 32,         // Changed
                cbSize: 22,
            },
            Samples: 32,
            dwChannelMask: SPEAKER_FRONT_CENTER, // Changed
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let expected = unsafe {
            WaveAudioFormat::new((&expected) as *const WAVEFORMATEXTENSIBLE as *mut WAVEFORMATEX)
        };

        assert_eq!(format, expected);
    }

    #[test]
    fn test_modify_mix_mono_channel_different_bit_depth_wave_format() {
        // Start with a mono channel and 16 bit depth format.
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 1,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 2 * 48000,
            nBlockAlign: 2,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        let mut format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        // The format should be converted to 32 bit depth and retain mono channel.
        let expected = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_IEEE_FLOAT, // Changed
            nChannels: 1,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 4 * 48000, // Changed
            nBlockAlign: 4,             // Changed
            wBitsPerSample: 32,         // Changed
            cbSize: 0,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let expected = unsafe {
            WaveAudioFormat::new((&expected) as *const WAVEFORMATEX as *mut WAVEFORMATEX)
        };

        assert_eq!(format, expected);
    }

    #[test]
    fn test_waveformatex_non_ieee_modify_format() {
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 2,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 192000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        let mut format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        let result_format = format.take_waveformatex();

        assert_waveformatex_ieee(&result_format);
    }

    #[test]
    fn test_waveformatex_non_ieee_32_bit_modify_format() {
        let format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 2,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 384000,
            nBlockAlign: 8,
            wBitsPerSample: 32,
            cbSize: 0,
        };

        let mut format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&format) as *const WAVEFORMATEX as *mut WAVEFORMATEX) };

        format.modify_mix_format(
            /* bit_depth= */ 32,
            /* ks_data_format= */ KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        );

        let result_format = format.take_waveformatex();

        assert_waveformatex_ieee(&result_format);
    }

    fn assert_waveformatex_ieee(result_format: &WAVEFORMATEX) {
        let format_tag = result_format.wFormatTag;
        assert_eq!(format_tag, WAVE_FORMAT_IEEE_FLOAT);
        let channels = result_format.nChannels;
        assert_eq!(channels, 2);
        let samples_per_sec = result_format.nSamplesPerSec;
        assert_eq!(samples_per_sec, 48000);
        let avg_bytes_per_sec = result_format.nAvgBytesPerSec;
        assert_eq!(avg_bytes_per_sec, 384000);
        let block_align = result_format.nBlockAlign;
        assert_eq!(block_align, 8);
        let bits_per_samples = result_format.wBitsPerSample;
        assert_eq!(bits_per_samples, 32);
        let size = result_format.cbSize;
        assert_eq!(size, 0);
    }

    #[test]
    fn test_create_audio_shared_format_wave_format_ex() {
        let wave_format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 2,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 192000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let format = unsafe {
            WaveAudioFormat::new((&wave_format) as *const WAVEFORMATEX as *mut WAVEFORMATEX)
        };

        // The period will most likely never be 123, but this is ok for testing.
        let audio_shared_format =
            format.create_audio_shared_format(/* shared_audio_engine_period_in_frames= */ 123);

        assert_eq!(
            audio_shared_format.bit_depth,
            wave_format.wBitsPerSample as usize
        );
        assert_eq!(audio_shared_format.channels, wave_format.nChannels as usize);
        assert_eq!(
            audio_shared_format.frame_rate,
            wave_format.nSamplesPerSec as usize
        );
        assert_eq!(
            audio_shared_format.shared_audio_engine_period_in_frames,
            123
        );
    }

    #[test]
    fn test_create_audio_shared_format_wave_format_extensible() {
        let wave_format_extensible = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 2,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 8 * 48000,
                nBlockAlign: 8,
                wBitsPerSample: 32,
                cbSize: 22,
            },
            Samples: 32,
            dwChannelMask: SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the
        // pointer can be safely deferenced.
        let format = unsafe {
            WaveAudioFormat::new((&wave_format_extensible) as *const _ as *mut WAVEFORMATEX)
        };

        // The period will most likely never be 123, but this is ok for testing.
        let audio_shared_format =
            format.create_audio_shared_format(/* shared_audio_engine_period_in_frames= */ 123);

        assert_eq!(
            audio_shared_format.bit_depth,
            wave_format_extensible.Format.wBitsPerSample as usize
        );
        assert_eq!(
            audio_shared_format.channels,
            wave_format_extensible.Format.nChannels as usize
        );
        assert_eq!(
            audio_shared_format.frame_rate,
            wave_format_extensible.Format.nSamplesPerSec as usize
        );
        assert_eq!(
            audio_shared_format.shared_audio_engine_period_in_frames,
            123
        );
        assert_eq!(
            audio_shared_format.channel_mask,
            Some(SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT)
        );
    }

    #[test]
    fn test_wave_format_to_proto_convertion() {
        let wave_format = WAVEFORMATEX {
            wFormatTag: WAVE_FORMAT_PCM,
            nChannels: 2,
            nSamplesPerSec: 48000,
            nAvgBytesPerSec: 192000,
            nBlockAlign: 4,
            wBitsPerSample: 16,
            cbSize: 0,
        };

        let wave_audio_format =
            // SAFETY: We can convert a struct to a pointer declared above. Also that means the
            // pointer can be safely deferenced.
            unsafe { WaveAudioFormat::new((&wave_format) as *const _ as *mut WAVEFORMATEX) };

        // Testing the `into`.
        let wave_format_metric = WaveFormatMetric::from(&wave_audio_format);

        let expected = WaveFormatMetric {
            format_tag: WAVE_FORMAT_PCM.into(),
            channels: 2,
            samples_per_sec: 48000,
            avg_bytes_per_sec: 192000,
            block_align: 4,
            bits_per_sample: 16,
            size_bytes: 0,
            samples: None,
            channel_mask: None,
            sub_format: None,
        };

        assert_eq!(wave_format_metric, expected);
    }

    #[test]
    fn test_wave_format_extensible_to_proto_convertion() {
        let wave_format_extensible = WAVEFORMATEXTENSIBLE {
            Format: WAVEFORMATEX {
                wFormatTag: WAVE_FORMAT_EXTENSIBLE,
                nChannels: 2,
                nSamplesPerSec: 48000,
                nAvgBytesPerSec: 8 * 48000,
                nBlockAlign: 8,
                wBitsPerSample: 32,
                cbSize: 22,
            },
            Samples: 32,
            dwChannelMask: SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT,
            SubFormat: KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
        };

        // SAFETY: We can convert a struct to a pointer declared above. Also that means the pointer
        // can be safely deferenced.
        let wave_audio_format = unsafe {
            WaveAudioFormat::new((&wave_format_extensible) as *const _ as *mut WAVEFORMATEX)
        };

        // Testing the `into`.
        let wave_format_metric = WaveFormatMetric::from(&wave_audio_format);

        let expected = WaveFormatMetric {
            format_tag: WAVE_FORMAT_EXTENSIBLE.into(),
            channels: 2,
            samples_per_sec: 48000,
            avg_bytes_per_sec: 8 * 48000,
            block_align: 8,
            bits_per_sample: 32,
            size_bytes: 22,
            samples: Some(32),
            channel_mask: Some((SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT) as i64),
            sub_format: Some(WaveFormatSubFormatMetric::IeeeFloat),
        };

        assert_eq!(wave_format_metric, expected);
    }
}