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
|
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#define COBJMACROS 1
#define _WIN32_WINNT 0x600
#include <stdlib.h>
#include <inttypes.h>
#include <process.h>
#include <initguid.h>
#include <audioclient.h>
#include <endpointvolume.h>
#include <mmdeviceapi.h>
#include <avrt.h>
#include "config.h"
#include "core/subopt-helper.h"
#include "core/m_option.h"
#include "core/m_config.h"
#include "audio/format.h"
#include "core/mp_msg.h"
#include "core/mp_ring.h"
#include "ao.h"
#ifndef BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE
#define BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE 0x00000001
#endif
#ifndef PKEY_Device_FriendlyName
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName,
0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20,
0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
#endif
#define RING_BUFFER_COUNT 64
/* 20 millisecond buffer? */
#define BUFFER_TIME 20000000.0
#define EXIT_ON_ERROR(hres) \
if (FAILED(hres)) { goto exit_label; }
#define SAFE_RELEASE(unk, release) \
if ((unk) != NULL) { release; (unk) = NULL; }
/* Supposed to use __uuidof, but it is C++ only, declare our own */
static const GUID local_KSDATAFORMAT_SUBTYPE_PCM = {
0x1, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
};
static const GUID local_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = {
0x3, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
};
union WAVEFMT {
WAVEFORMATEX *ex;
WAVEFORMATEXTENSIBLE *extensible;
};
typedef struct wasapi0_state {
HANDLE threadLoop;
/* Init phase */
int init_ret;
HANDLE init_done;
HANDLE fatal_error; /* signal to indicate unrecoverable error */
int share_mode;
/* Events */
HANDLE hPause;
/* Play */
HANDLE hPlay;
int is_playing;
/* Reset */
HANDLE hReset;
/* uninit */
HANDLE hUninit;
LONG immed;
/* volume control */
HANDLE hGetvol, hSetvol, hDoneVol;
DWORD vol_hw_support, status;
float audio_volume;
/* Prints, for in case line buffers are disabled */
CRITICAL_SECTION print_lock;
/* Buffers */
struct mp_ring *ringbuff;
size_t buffer_block_size; /* Size of each block in bytes */
REFERENCE_TIME
minRequestedDuration; /* minimum wasapi buffer block size, in 100-nanosecond units */
REFERENCE_TIME
defaultRequestedDuration; /* default wasapi default block size, in 100-nanosecond units */
UINT32 bufferFrameCount; /* wasapi buffer block size, number of frames, frame size at format.nBlockAlign */
/* WASAPI handles, owned by other thread */
IMMDeviceEnumerator *pEnumerator;
IMMDevice *pDevice;
IAudioClient *pAudioClient;
IAudioRenderClient *pRenderClient;
IAudioEndpointVolume *pEndpointVolume;
HANDLE hFeed; /* wasapi event */
HANDLE hTask; /* AV thread */
DWORD taskIndex; /* AV task ID */
WAVEFORMATEXTENSIBLE format;
int opt_exclusive;
int opt_list;
char *opt_device;
/* We still need to support XP, don't use these functions directly, blob owned by main thread */
struct {
HMODULE hAvrt;
HANDLE (WINAPI *pAvSetMmThreadCharacteristicsW)(LPCWSTR, LPDWORD);
WINBOOL (WINAPI *pAvRevertMmThreadCharacteristics)(HANDLE);
} VistaBlob;
} wasapi0_state;
static int fill_VistaBlob(wasapi0_state *state)
{
if (!state)
return 1;
HMODULE hkernel32 = GetModuleHandleW(L"kernel32.dll");
if (!hkernel32)
return 1;
WINBOOL (WINAPI *pSetDllDirectory)(LPCWSTR lpPathName) =
(WINBOOL (WINAPI *)(LPCWSTR))GetProcAddress(hkernel32, "SetDllDirectoryW");
WINBOOL (WINAPI *pSetSearchPathMode)(DWORD Flags) =
(WINBOOL (WINAPI *)(DWORD))GetProcAddress(hkernel32, "SetSearchPathMode");
if (pSetSearchPathMode)
pSetDllDirectory(L""); /* Attempt to use safe search paths */
if (pSetSearchPathMode)
pSetSearchPathMode(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE);
state->VistaBlob.hAvrt = LoadLibraryW(L"avrt.dll");
if (!state->VistaBlob.hAvrt)
goto exit_label;
state->VistaBlob.pAvSetMmThreadCharacteristicsW =
(HANDLE (WINAPI *)(LPCWSTR, LPDWORD))
GetProcAddress(state->VistaBlob.hAvrt, "AvSetMmThreadCharacteristicsW");
state->VistaBlob.pAvRevertMmThreadCharacteristics =
(WINBOOL (WINAPI *)(HANDLE))
GetProcAddress(state->VistaBlob.hAvrt, "AvRevertMmThreadCharacteristics");
return 0;
exit_label:
if (state->VistaBlob.hAvrt)
FreeLibrary(state->VistaBlob.hAvrt);
if (pSetSearchPathMode)
pSetDllDirectory(NULL);
return 1;
}
static const char *explain_err(const HRESULT hr)
{
switch (hr) {
case S_OK:
return "S_OK";
case AUDCLNT_E_NOT_INITIALIZED:
return "AUDCLNT_E_NOT_INITIALIZED";
case AUDCLNT_E_ALREADY_INITIALIZED:
return "AUDCLNT_E_ALREADY_INITIALIZED";
case AUDCLNT_E_WRONG_ENDPOINT_TYPE:
return "AUDCLNT_E_WRONG_ENDPOINT_TYPE";
case AUDCLNT_E_DEVICE_INVALIDATED:
return "AUDCLNT_E_DEVICE_INVALIDATED";
case AUDCLNT_E_NOT_STOPPED:
return "AUDCLNT_E_NOT_STOPPED";
case AUDCLNT_E_BUFFER_TOO_LARGE:
return "AUDCLNT_E_BUFFER_TOO_LARGE";
case AUDCLNT_E_OUT_OF_ORDER:
return "AUDCLNT_E_OUT_OF_ORDER";
case AUDCLNT_E_UNSUPPORTED_FORMAT:
return "AUDCLNT_E_UNSUPPORTED_FORMAT";
case AUDCLNT_E_INVALID_SIZE:
return "AUDCLNT_E_INVALID_SIZE";
case AUDCLNT_E_DEVICE_IN_USE:
return "AUDCLNT_E_DEVICE_IN_USE";
case AUDCLNT_E_BUFFER_OPERATION_PENDING:
return "AUDCLNT_E_BUFFER_OPERATION_PENDING";
case AUDCLNT_E_THREAD_NOT_REGISTERED:
return "AUDCLNT_E_THREAD_NOT_REGISTERED";
case AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED:
return "AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED";
case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
return "AUDCLNT_E_ENDPOINT_CREATE_FAILED";
case AUDCLNT_E_SERVICE_NOT_RUNNING:
return "AUDCLNT_E_SERVICE_NOT_RUNNING";
case AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED:
return "AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED";
case AUDCLNT_E_EXCLUSIVE_MODE_ONLY:
return "AUDCLNT_E_EXCLUSIVE_MODE_ONLY";
case AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL:
return "AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL";
case AUDCLNT_E_EVENTHANDLE_NOT_SET:
return "AUDCLNT_E_EVENTHANDLE_NOT_SET";
case AUDCLNT_E_INCORRECT_BUFFER_SIZE:
return "AUDCLNT_E_INCORRECT_BUFFER_SIZE";
case AUDCLNT_E_BUFFER_SIZE_ERROR:
return "AUDCLNT_E_BUFFER_SIZE_ERROR";
case AUDCLNT_E_CPUUSAGE_EXCEEDED:
return "AUDCLNT_E_CPUUSAGE_EXCEEDED";
case AUDCLNT_E_BUFFER_ERROR:
return "AUDCLNT_E_BUFFER_ERROR";
case AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED:
return "AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED";
case AUDCLNT_E_INVALID_DEVICE_PERIOD:
return "AUDCLNT_E_INVALID_DEVICE_PERIOD";
case AUDCLNT_E_INVALID_STREAM_FLAG:
return "AUDCLNT_E_INVALID_STREAM_FLAG";
case AUDCLNT_E_ENDPOINT_OFFLOAD_NOT_CAPABLE:
return "AUDCLNT_E_ENDPOINT_OFFLOAD_NOT_CAPABLE";
case AUDCLNT_E_RESOURCES_INVALIDATED:
return "AUDCLNT_E_RESOURCES_INVALIDATED";
case AUDCLNT_S_BUFFER_EMPTY:
return "AUDCLNT_S_BUFFER_EMPTY";
case AUDCLNT_S_THREAD_ALREADY_REGISTERED:
return "AUDCLNT_S_THREAD_ALREADY_REGISTERED";
case AUDCLNT_S_POSITION_STALLED:
return "AUDCLNT_S_POSITION_STALLED";
default:
return "<Unknown>";
}
}
static void set_format(WAVEFORMATEXTENSIBLE *wformat, WORD bytepersample,
DWORD samplerate, WORD channels, DWORD chanmask)
{
wformat->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; /* Only PCM is supported */
wformat->Format.nChannels = channels;
wformat->Format.nSamplesPerSec = samplerate;
wformat->Format.nAvgBytesPerSec = wformat->Format.nChannels *
bytepersample *
wformat->Format.nSamplesPerSec;
wformat->Format.nBlockAlign = wformat->Format.nChannels * bytepersample;
wformat->Format.wBitsPerSample = bytepersample * 8;
wformat->Format.cbSize =
22; /* must be at least 22 for WAVE_FORMAT_EXTENSIBLE */
if (bytepersample == 4)
wformat->SubFormat = local_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
else
wformat->SubFormat = local_KSDATAFORMAT_SUBTYPE_PCM;
wformat->Samples.wValidBitsPerSample = wformat->Format.wBitsPerSample;
wformat->dwChannelMask = chanmask;
}
static int format_set_bits(int old_format, int bits, int fp) {
int format = old_format;
format &= (~AF_FORMAT_BITS_MASK) & (~AF_FORMAT_POINT_MASK) & (~AF_FORMAT_SIGN_MASK);
format |= AF_FORMAT_SI;
switch (bits) {
case 32:
format |= AF_FORMAT_32BIT;
break;
case 24:
format |= AF_FORMAT_24BIT;
break;
case 16:
format |= AF_FORMAT_16BIT;
break;
default:
abort(); // (should be) unreachable
}
if (fp) {
format |= AF_FORMAT_F;
} else {
format |= AF_FORMAT_I;
}
return format;
}
static int set_ao_format(struct wasapi0_state *state,
struct ao *const ao,
WAVEFORMATEXTENSIBLE wformat) {
// .Data1 == 1 is PCM, .Data1 == 3 is IEEE_FLOAT
int format = format_set_bits(ao->format,
wformat.Format.wBitsPerSample, wformat.SubFormat.Data1 == 3);
if (wformat.SubFormat.Data1 != 1 && wformat.SubFormat.Data1 != 3) {
mp_msg(MSGT_AO, MSGL_ERR, "ao-wasapi: unknown SubFormat %d\n",
wformat.SubFormat.Data1);
return 0;
}
ao->samplerate = wformat.Format.nSamplesPerSec;
ao->bps = wformat.Format.nAvgBytesPerSec;
ao->format = format;
if (ao->channels.num != wformat.Format.nChannels) {
mp_chmap_from_channels(&ao->channels, wformat.Format.nChannels);
}
state->format = wformat;
return 1;
}
static int try_format(struct wasapi0_state *state,
struct ao *const ao,
int bits, int samplerate,
const struct mp_chmap channels)
{
WAVEFORMATEXTENSIBLE wformat;
set_format(&wformat, bits / 8, samplerate, channels.num, mp_chmap_to_waveext(&channels));
int af_format = format_set_bits(ao->format, bits, bits == 32);
EnterCriticalSection(&state->print_lock);
mp_msg(MSGT_AO, MSGL_V, "ao-wasapi: trying %dch %s @ %dhz\n",
channels.num, af_fmt2str_short(af_format), samplerate);
LeaveCriticalSection(&state->print_lock);
union WAVEFMT u;
u.extensible = &wformat;
WAVEFORMATEX *closestMatch;
HRESULT hr = IAudioClient_IsFormatSupported(state->pAudioClient,
state->share_mode,
u.ex, &closestMatch);
if (closestMatch) {
if (closestMatch->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
u.ex = closestMatch;
wformat = *u.extensible;
} else {
wformat.Format = *closestMatch;
}
CoTaskMemFree(closestMatch);
}
if (hr == S_FALSE) {
if (set_ao_format(state, ao, wformat)) {
EnterCriticalSection(&state->print_lock);
mp_msg(MSGT_AO, MSGL_V, "ao-wasapi: accepted as %dch %s @ %dhz\n",
ao->channels.num, af_fmt2str_short(ao->format), ao->samplerate);
LeaveCriticalSection(&state->print_lock);
return 1;
}
} if (hr == S_OK || (!state->opt_exclusive && hr == AUDCLNT_E_UNSUPPORTED_FORMAT)) {
// AUDCLNT_E_UNSUPPORTED_FORMAT here means "works in shared, doesn't in exclusive"
if (set_ao_format(state, ao, wformat)) {
EnterCriticalSection(&state->print_lock);
mp_msg(MSGT_AO, MSGL_V, "ao-wasapi: %dch %s @ %dhz accepted\n",
ao->channels.num, af_fmt2str_short(af_format), samplerate);
LeaveCriticalSection(&state->print_lock);
return 1;
}
}
return 0;
}
static int try_mix_format(struct wasapi0_state *state,
struct ao *const ao)
{
WAVEFORMATEX *deviceFormat = NULL;
WAVEFORMATEX *closestMatch = NULL;
int ret = 0;
HRESULT hr = IAudioClient_GetMixFormat(state->pAudioClient, &deviceFormat);
EXIT_ON_ERROR(hr)
union WAVEFMT u;
u.ex = deviceFormat;
WAVEFORMATEXTENSIBLE wformat = *u.extensible;
ret = try_format(state, ao, wformat.Format.wBitsPerSample,
wformat.Format.nSamplesPerSec, ao->channels);
exit_label:
SAFE_RELEASE(deviceFormat, CoTaskMemFree(deviceFormat));
SAFE_RELEASE(closestMatch, CoTaskMemFree(closestMatch));
return ret;
}
static int find_formats(struct ao *const ao)
{
struct wasapi0_state *state = (struct wasapi0_state *)ao->priv;
/* See if the format works as-is */
int bits = af_fmt2bits(ao->format);
/* don't try 8bits -- there are various 8bit modes other than PCM (*-law et al);
let's just stick to PCM or float here. */
if (bits == 8) {
bits = 16;
} else if (try_format(state, ao, bits, ao->samplerate, ao->channels)) {
return 0;
}
if (!state->opt_exclusive) {
/* shared mode, we can use the system default mix format. */
if (try_mix_format(state, ao)) {
return 0;
}
EnterCriticalSection(&state->print_lock);
mp_msg(MSGT_AO, MSGL_WARN, "ao-wasapi: couldn't use default mix format!\n");
LeaveCriticalSection(&state->print_lock);
}
/* Exclusive mode, we have to guess. */
/* as far as testing shows, only PCM 16/24LE (44100Hz - 192kHz) is supported
* Tested on Realtek High Definition Audio, (Realtek Semiconductor Corp. 6.0.1.6312)
* Drivers dated 2/18/2011
*/
/* try float first for non-16bit audio */
if (bits != 16) {
bits = 32;
}
int start_bits = bits;
while (1) { // not infinite -- returns at bottom
for (; bits > 8; bits -= 8) {
int samplerate = ao->samplerate;
if (try_format(state, ao, bits, samplerate, ao->channels)) {
return 0;
}
// make samplerate fit in [44100 192000]
// we check for samplerate > 96k so that we can upsample instead of downsampling later
if (samplerate < 44100 || samplerate > 96000) {
if (samplerate < 44100)
samplerate = 44100;
if (samplerate > 96000)
samplerate = 192000;
if (try_format(state, ao, bits, samplerate, ao->channels)) {
return 0;
}
}
// try bounding to 96kHz
if (samplerate > 48000) {
samplerate = 96000;
if (try_format(state, ao, bits, samplerate, ao->channels)) {
return 0;
}
}
// try bounding to 48kHz
if (samplerate > 44100) {
samplerate = 48000;
if (try_format(state, ao, bits, samplerate, ao->channels)) {
return 0;
}
}
/* How bad is this? try 44100hz, but only on 16bit */
if (bits == 16 && samplerate != 44100) {
samplerate = 44100;
if (try_format(state, ao, bits, samplerate, ao->channels)) {
return 0;
}
}
}
if (ao->channels.num > 6) {
/* Maybe this is 5.1 hardware with no support for more. */
bits = start_bits;
mp_chmap_from_channels(&ao->channels, 6);
} else if (ao->channels.num != 2) {
/* Poor quality hardware? Try stereo mode, go through the list again. */
bits = start_bits;
mp_chmap_from_channels(&ao->channels, 2);
} else {
EnterCriticalSection(&state->print_lock);
mp_msg(MSGT_AO, MSGL_ERR, "ao-wasapi: couldn't find acceptable audio format!\n");
LeaveCriticalSection(&state->print_lock);
return -1;
}
}
}
static int fix_format(struct wasapi0_state *state)
{
HRESULT hr;
double offset = 0.5;
/* cargo cult code to negotiate buffer block size, affected by hardware/drivers combinations,
gradually grow it to 10s, by 0.5s, consider failure if it still doesn't work
*/
hr = IAudioClient_GetDevicePeriod(state->pAudioClient,
&state->defaultRequestedDuration,
&state->minRequestedDuration);
reinit:
hr = IAudioClient_Initialize(state->pAudioClient,
state->share_mode,
AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
state->defaultRequestedDuration,
state->defaultRequestedDuration,
&(state->format.Format),
NULL);
/* something about buffer sizes on Win7, fixme might loop forever */
if (hr == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED) {
EnterCriticalSection(&state->print_lock);
mp_msg(
MSGT_AO, MSGL_V,
"ao-wasapi: IAudioClient::Initialize negotiation failed with %s, used %lld * 100ns\n",
explain_err(hr), state->defaultRequestedDuration);
LeaveCriticalSection(&state->print_lock);
if (offset > 10.0)
goto exit_label; /* is 10 enough to break out of the loop?*/
IAudioClient_GetBufferSize(state->pAudioClient, &state->bufferFrameCount);
state->defaultRequestedDuration =
(REFERENCE_TIME)((BUFFER_TIME / state->format.Format.nSamplesPerSec *
state->bufferFrameCount) + offset);
offset += 0.5;
IAudioClient_Release(state->pAudioClient);
state->pAudioClient = NULL;
hr = IMMDeviceActivator_Activate(state->pDevice,
&IID_IAudioClient, CLSCTX_ALL,
NULL, (void **)&state->pAudioClient);
goto reinit;
}
EXIT_ON_ERROR(hr)
hr = IAudioClient_GetService(
|