1 /*
2  * This file is part of mpv.
3  *
4  * Original author: Jonathan Yong <10walls@gmail.com>
5  *
6  * mpv is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * mpv is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <math.h>
21 #include <wchar.h>
22 #include <windows.h>
23 #include <errors.h>
24 #include <ksguid.h>
25 #include <ksmedia.h>
26 #include <avrt.h>
27 
28 #include "audio/format.h"
29 #include "osdep/timer.h"
30 #include "osdep/io.h"
31 #include "osdep/strnlen.h"
32 #include "ao_wasapi.h"
33 
34 #define MIXER_DEFAULT_LABEL L"mpv - video player"
35 
36 DEFINE_PROPERTYKEY(mp_PKEY_Device_FriendlyName,
37                    0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20,
38                    0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
39 DEFINE_PROPERTYKEY(mp_PKEY_Device_DeviceDesc,
40                    0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20,
41                    0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 2);
42 // CEA 861 subformats
43 // should work on vista
44 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_DTS,
45             0x00000008, 0x0000, 0x0010, 0x80, 0x00,
46             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
47 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL,
48             0x00000092, 0x0000, 0x0010, 0x80, 0x00,
49             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
50 // might require 7+
51 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_AAC,
52             0x00000006, 0x0cea, 0x0010, 0x80, 0x00,
53             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
54 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_MPEG3,
55             0x00000004, 0x0cea, 0x0010, 0x80, 0x00,
56             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
57 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL_PLUS,
58             0x0000000a, 0x0cea, 0x0010, 0x80, 0x00,
59             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
60 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD,
61             0x0000000b, 0x0cea, 0x0010, 0x80, 0x00,
62             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
63 DEFINE_GUID(mp_KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_MLP,
64             0x0000000c, 0x0cea, 0x0010, 0x80, 0x00,
65             0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
66 
67 struct wasapi_sample_fmt {
68     int mp_format;  // AF_FORMAT_*
69     int bits;       // aka wBitsPerSample
70     int used_msb;   // aka wValidBitsPerSample
71     const GUID *subtype;
72 };
73 
74 // some common bit depths / container sizes (requests welcome)
75 // Entries that have the same mp_format must be:
76 //  1. consecutive
77 //  2. sorted by preferred format (worst comes last)
78 static const struct wasapi_sample_fmt wasapi_formats[] = {
79     {AF_FORMAT_U8,       8,  8, &KSDATAFORMAT_SUBTYPE_PCM},
80     {AF_FORMAT_S16,     16, 16, &KSDATAFORMAT_SUBTYPE_PCM},
81     {AF_FORMAT_S32,     32, 32, &KSDATAFORMAT_SUBTYPE_PCM},
82     // compatible, assume LSBs are ignored
83     {AF_FORMAT_S32,     32, 24, &KSDATAFORMAT_SUBTYPE_PCM},
84     // aka S24 (with conversion on output)
85     {AF_FORMAT_S32,     24, 24, &KSDATAFORMAT_SUBTYPE_PCM},
86     {AF_FORMAT_FLOAT,   32, 32, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT},
87     {AF_FORMAT_S_AC3,   16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL},
88     {AF_FORMAT_S_DTS,   16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_DTS},
89     {AF_FORMAT_S_AAC,   16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_AAC},
90     {AF_FORMAT_S_MP3,   16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_MPEG3},
91     {AF_FORMAT_S_TRUEHD, 16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_MLP},
92     {AF_FORMAT_S_EAC3,  16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL_PLUS},
93     {AF_FORMAT_S_DTSHD, 16, 16, &mp_KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD},
94     {0},
95 };
96 
wasapi_get_best_sample_formats(int src_format,struct wasapi_sample_fmt * out_formats)97 static void wasapi_get_best_sample_formats(
98     int src_format, struct wasapi_sample_fmt *out_formats)
99 {
100     int mp_formats[AF_FORMAT_COUNT + 1];
101     af_get_best_sample_formats(src_format, mp_formats);
102     for (int n = 0; mp_formats[n]; n++) {
103         for (int i = 0; wasapi_formats[i].mp_format; i++) {
104             if (wasapi_formats[i].mp_format == mp_formats[n])
105                 *out_formats++ = wasapi_formats[i];
106         }
107     }
108     *out_formats = (struct wasapi_sample_fmt) {0};
109 }
110 
format_to_subtype(int format)111 static const GUID *format_to_subtype(int format)
112 {
113     for (int i = 0; wasapi_formats[i].mp_format; i++) {
114         if (format == wasapi_formats[i].mp_format)
115             return wasapi_formats[i].subtype;
116     }
117     return &KSDATAFORMAT_SPECIFIER_NONE;
118 }
119 
mp_PKEY_to_str_buf(char * buf,size_t buf_size,const PROPERTYKEY * pkey)120 char *mp_PKEY_to_str_buf(char *buf, size_t buf_size, const PROPERTYKEY *pkey)
121 {
122     buf = mp_GUID_to_str_buf(buf, buf_size, &pkey->fmtid);
123     size_t guid_len = strnlen(buf, buf_size);
124     snprintf(buf + guid_len, buf_size - guid_len, ",%"PRIu32,
125              (uint32_t) pkey->pid);
126     return buf;
127 }
128 
update_waveformat_datarate(WAVEFORMATEXTENSIBLE * wformat)129 static void update_waveformat_datarate(WAVEFORMATEXTENSIBLE *wformat)
130 {
131     WAVEFORMATEX *wf = &wformat->Format;
132     wf->nBlockAlign     = wf->nChannels      * wf->wBitsPerSample / 8;
133     wf->nAvgBytesPerSec = wf->nSamplesPerSec * wf->nBlockAlign;
134 }
135 
set_waveformat(WAVEFORMATEXTENSIBLE * wformat,struct wasapi_sample_fmt * format,DWORD samplerate,struct mp_chmap * channels)136 static void set_waveformat(WAVEFORMATEXTENSIBLE *wformat,
137                            struct wasapi_sample_fmt *format,
138                            DWORD samplerate, struct mp_chmap *channels)
139 {
140     wformat->Format.wFormatTag     = WAVE_FORMAT_EXTENSIBLE;
141     wformat->Format.nChannels      = channels->num;
142     wformat->Format.nSamplesPerSec = samplerate;
143     wformat->Format.wBitsPerSample = format->bits;
144     wformat->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
145 
146     wformat->SubFormat                   = *format_to_subtype(format->mp_format);
147     wformat->Samples.wValidBitsPerSample = format->used_msb;
148     wformat->dwChannelMask               = mp_chmap_to_waveext(channels);
149     update_waveformat_datarate(wformat);
150 }
151 
152 // other wformat parameters must already be set with set_waveformat
change_waveformat_samplerate(WAVEFORMATEXTENSIBLE * wformat,DWORD samplerate)153 static void change_waveformat_samplerate(WAVEFORMATEXTENSIBLE *wformat,
154                                          DWORD samplerate)
155 {
156     wformat->Format.nSamplesPerSec = samplerate;
157     update_waveformat_datarate(wformat);
158 }
159 
160 // other wformat parameters must already be set with set_waveformat
change_waveformat_channels(WAVEFORMATEXTENSIBLE * wformat,struct mp_chmap * channels)161 static void change_waveformat_channels(WAVEFORMATEXTENSIBLE *wformat,
162                                        struct mp_chmap *channels)
163 {
164     wformat->Format.nChannels = channels->num;
165     wformat->dwChannelMask    = mp_chmap_to_waveext(channels);
166     update_waveformat_datarate(wformat);
167 }
168 
format_from_waveformat(WAVEFORMATEX * wf)169 static struct wasapi_sample_fmt format_from_waveformat(WAVEFORMATEX *wf)
170 {
171     struct wasapi_sample_fmt res = {0};
172 
173     for (int n = 0; wasapi_formats[n].mp_format; n++) {
174         const struct wasapi_sample_fmt *fmt = &wasapi_formats[n];
175         int valid_bits = 0;
176 
177         if (wf->wBitsPerSample != fmt->bits)
178             continue;
179 
180         const GUID *wf_guid = NULL;
181 
182         switch (wf->wFormatTag) {
183         case WAVE_FORMAT_EXTENSIBLE: {
184             WAVEFORMATEXTENSIBLE *wformat = (WAVEFORMATEXTENSIBLE *)wf;
185             wf_guid = &wformat->SubFormat;
186             if (IsEqualGUID(wf_guid, &KSDATAFORMAT_SUBTYPE_PCM))
187                 valid_bits = wformat->Samples.wValidBitsPerSample;
188             break;
189         }
190         case WAVE_FORMAT_PCM:
191             wf_guid = &KSDATAFORMAT_SUBTYPE_PCM;
192             break;
193         case WAVE_FORMAT_IEEE_FLOAT:
194             wf_guid = &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
195             break;
196         }
197 
198         if (!wf_guid || !IsEqualGUID(wf_guid, fmt->subtype))
199             continue;
200 
201         res = *fmt;
202         if (valid_bits > 0 && valid_bits < fmt->bits)
203             res.used_msb = valid_bits;
204         break;
205     }
206 
207     return res;
208 }
209 
chmap_from_waveformat(struct mp_chmap * channels,const WAVEFORMATEX * wf)210 static bool chmap_from_waveformat(struct mp_chmap *channels,
211                                   const WAVEFORMATEX *wf)
212 {
213     if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
214         WAVEFORMATEXTENSIBLE *wformat = (WAVEFORMATEXTENSIBLE *)wf;
215         mp_chmap_from_waveext(channels, wformat->dwChannelMask);
216     } else {
217         mp_chmap_from_channels(channels, wf->nChannels);
218     }
219 
220     if (channels->num != wf->nChannels) {
221         mp_chmap_from_str(channels, bstr0("empty"));
222         return false;
223     }
224 
225     return true;
226 }
227 
waveformat_to_str_buf(char * buf,size_t buf_size,WAVEFORMATEX * wf)228 static char *waveformat_to_str_buf(char *buf, size_t buf_size, WAVEFORMATEX *wf)
229 {
230     struct mp_chmap channels;
231     chmap_from_waveformat(&channels, wf);
232 
233     struct wasapi_sample_fmt format = format_from_waveformat(wf);
234 
235     snprintf(buf, buf_size, "%s %s (%d/%d bits) @ %uhz",
236              mp_chmap_to_str(&channels),
237              af_fmt_to_str(format.mp_format), format.bits, format.used_msb,
238              (unsigned) wf->nSamplesPerSec);
239     return buf;
240 }
241 #define waveformat_to_str(wf) waveformat_to_str_buf((char[64]){0}, 64, (wf))
242 
waveformat_copy(WAVEFORMATEXTENSIBLE * dst,WAVEFORMATEX * src)243 static void waveformat_copy(WAVEFORMATEXTENSIBLE* dst, WAVEFORMATEX* src)
244 {
245     if (src->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
246         *dst = *(WAVEFORMATEXTENSIBLE *)src;
247     } else {
248         dst->Format = *src;
249     }
250 }
251 
set_ao_format(struct ao * ao,WAVEFORMATEX * wf,AUDCLNT_SHAREMODE share_mode)252 static bool set_ao_format(struct ao *ao, WAVEFORMATEX *wf,
253                           AUDCLNT_SHAREMODE share_mode)
254 {
255     struct wasapi_state *state = ao->priv;
256     struct wasapi_sample_fmt format = format_from_waveformat(wf);
257     if (!format.mp_format) {
258         MP_ERR(ao, "Unable to construct sample format from WAVEFORMAT %s\n",
259                waveformat_to_str(wf));
260         return false;
261     }
262 
263     // Do not touch the ao for passthrough, just assume that we set WAVEFORMATEX
264     // correctly.
265     if (af_fmt_is_pcm(format.mp_format)) {
266         struct mp_chmap channels;
267         if (!chmap_from_waveformat(&channels, wf)) {
268             MP_ERR(ao, "Unable to construct channel map from WAVEFORMAT %s\n",
269                    waveformat_to_str(wf));
270             return false;
271         }
272 
273         struct ao_convert_fmt conv = {
274             .src_fmt    = format.mp_format,
275             .channels   = channels.num,
276             .dst_bits   = format.bits,
277             .pad_lsb    = format.bits - format.used_msb,
278         };
279         if (!ao_can_convert_inplace(&conv)) {
280             MP_ERR(ao, "Unable to convert to %s\n", waveformat_to_str(wf));
281             return false;
282         }
283 
284         state->convert_format = conv;
285         ao->samplerate = wf->nSamplesPerSec;
286         ao->format     = format.mp_format;
287         ao->channels   = channels;
288     }
289     waveformat_copy(&state->format, wf);
290     state->share_mode = share_mode;
291 
292     MP_VERBOSE(ao, "Accepted as %s %s @ %dhz -> %s (%s)\n",
293                mp_chmap_to_str(&ao->channels),
294                af_fmt_to_str(ao->format), ao->samplerate,
295                waveformat_to_str(wf),
296                state->share_mode == AUDCLNT_SHAREMODE_EXCLUSIVE
297                ? "exclusive" : "shared");
298     return true;
299 }
300 
301 #define mp_format_res_str(hres) \
302     (SUCCEEDED(hres) ? ((hres) == S_OK) ? "ok" : "close" \
303                      : ((hres) == AUDCLNT_E_UNSUPPORTED_FORMAT) \
304                        ? "unsupported" : mp_HRESULT_to_str(hres))
305 
try_format_exclusive(struct ao * ao,WAVEFORMATEXTENSIBLE * wformat)306 static bool try_format_exclusive(struct ao *ao, WAVEFORMATEXTENSIBLE *wformat)
307 {
308     struct wasapi_state *state = ao->priv;
309     HRESULT hr = IAudioClient_IsFormatSupported(state->pAudioClient,
310                                                 AUDCLNT_SHAREMODE_EXCLUSIVE,
311                                                 &wformat->Format, NULL);
312     MP_VERBOSE(ao, "Trying %s (exclusive) -> %s\n",
313                waveformat_to_str(&wformat->Format), mp_format_res_str(hr));
314     return SUCCEEDED(hr);
315 }
316 
search_sample_formats(struct ao * ao,WAVEFORMATEXTENSIBLE * wformat,int samplerate,struct mp_chmap * channels)317 static bool search_sample_formats(struct ao *ao, WAVEFORMATEXTENSIBLE *wformat,
318                                   int samplerate, struct mp_chmap *channels)
319 {
320     struct wasapi_sample_fmt alt_formats[MP_ARRAY_SIZE(wasapi_formats)];
321     wasapi_get_best_sample_formats(ao->format, alt_formats);
322     for (int n = 0; alt_formats[n].mp_format; n++) {
323         set_waveformat(wformat, &alt_formats[n], samplerate, channels);
324         if (try_format_exclusive(ao, wformat))
325             return true;
326     }
327 
328     wformat->Format.wBitsPerSample = 0;
329     return false;
330 }
331 
search_samplerates(struct ao * ao,WAVEFORMATEXTENSIBLE * wformat,struct mp_chmap * channels)332 static bool search_samplerates(struct ao *ao, WAVEFORMATEXTENSIBLE *wformat,
333                                struct mp_chmap *channels)
334 {
335     // put common samplerates first so that we find format early
336     int try[] = {48000, 44100, 96000, 88200, 192000, 176400,
337                  32000, 22050, 11025, 8000, 16000, 352800, 384000, 0};
338 
339     // get a list of supported rates
340     int n = 0;
341     int supported[MP_ARRAY_SIZE(try)] = {0};
342 
343     wformat->Format.wBitsPerSample = 0;
344     for (int i = 0; try[i]; i++) {
345         if (!wformat->Format.wBitsPerSample) {
346             if (search_sample_formats(ao, wformat, try[i], channels))
347                 supported[n++] = try[i];
348         } else {
349             change_waveformat_samplerate(wformat, try[i]);
350             if (try_format_exclusive(ao, wformat))
351                 supported[n++] = try[i];
352         }
353     }
354 
355     int samplerate = af_select_best_samplerate(ao->samplerate, supported);
356     if (samplerate > 0) {
357         change_waveformat_samplerate(wformat, samplerate);
358         return true;
359     }
360 
361     // otherwise, this is probably an unsupported channel map
362     wformat->Format.nSamplesPerSec = 0;
363     return false;
364 }
365 
search_channels(struct ao * ao,WAVEFORMATEXTENSIBLE * wformat)366 static bool search_channels(struct ao *ao, WAVEFORMATEXTENSIBLE *wformat)
367 {
368     struct wasapi_state *state = ao->priv;
369     struct mp_chmap_sel chmap_sel = {.tmp = state};
370     struct mp_chmap entry;
371     // put common layouts first so that we find sample rate/format early
372     char *channel_layouts[] =
373         {"stereo", "5.1", "7.1", "6.1", "mono", "2.1", "4.0", "5.0",
374          "3.0", "3.0(back)",
375          "quad", "quad(side)", "3.1",
376          "5.0(side)", "4.1",
377          "5.1(side)", "6.0", "6.0(front)", "hexagonal"
378          "6.1(back)", "6.1(front)", "7.0", "7.0(front)",
379          "7.1(wide)", "7.1(wide-side)", "7.1(rear)", "octagonal", NULL};
380 
381     wformat->Format.nSamplesPerSec = 0;
382     for (int j = 0; channel_layouts[j]; j++) {
383         mp_chmap_from_str(&entry, bstr0(channel_layouts[j]));
384         if (!wformat->Format.nSamplesPerSec) {
385             if (search_samplerates(ao, wformat, &entry))
386                 mp_chmap_sel_add_map(&chmap_sel, &entry);
387         } else {
388             change_waveformat_channels(wformat, &entry);
389             if (try_format_exclusive(ao, wformat))
390                 mp_chmap_sel_add_map(&chmap_sel, &entry);
391         }
392     }
393 
394     entry = ao->channels;
395     if (ao_chmap_sel_adjust2(ao, &chmap_sel, &entry, !state->opt_exclusive)){
396         change_waveformat_channels(wformat, &entry);
397         return true;
398     }
399 
400     MP_ERR(ao, "No suitable audio format found\n");
401     return false;
402 }
403 
find_formats_exclusive(struct ao * ao,WAVEFORMATEXTENSIBLE * wformat)404 static bool find_formats_exclusive(struct ao *ao, WAVEFORMATEXTENSIBLE *wformat)
405 {
406     // Try the specified format as is
407     if (try_format_exclusive(ao, wformat))
408         return true;
409 
410     if (af_fmt_is_spdif(ao->format)) {
411         if (ao->format != AF_FORMAT_S_AC3) {
412             // If the requested format failed and it is passthrough, but not
413             // AC3, try lying and saying it is.
414             MP_VERBOSE(ao, "Retrying as AC3.\n");
415             wformat->SubFormat = *format_to_subtype(AF_FORMAT_S_AC3);
416             if (try_format_exclusive(ao, wformat))
417                 return true;
418         }
419         return false;
420     }
421 
422     // Fallback on the PCM format search
423     return search_channels(ao, wformat);
424 }
425 
find_formats_shared(struct ao * ao,WAVEFORMATEXTENSIBLE * wformat)426 static bool find_formats_shared(struct ao *ao, WAVEFORMATEXTENSIBLE *wformat)
427 {
428     struct wasapi_state *state = ao->priv;
429 
430     struct mp_chmap channels;
431     if (!chmap_from_waveformat(&channels, &wformat->Format)) {
432         MP_ERR(ao, "Error converting channel map\n");
433         return false;
434     }
435 
436     HRESULT hr;
437     WAVEFORMATEX *mix_format;
438     hr = IAudioClient_GetMixFormat(state->pAudioClient, &mix_format);
439     EXIT_ON_ERROR(hr);
440 
441     // WASAPI doesn't do any sample rate conversion on its own and
442     // will typically only accept the mix format samplerate. Although
443     // it will accept any PCM sample format, everything gets converted
444     // to the mix format anyway (pretty much always float32), so just
445     // use that.
446     WAVEFORMATEXTENSIBLE try_format;
447     waveformat_copy(&try_format, mix_format);
448     CoTaskMemFree(mix_format);
449 
450     // WASAPI may accept channel maps other than the mix format
451     // if a surround emulator is enabled.
452     change_waveformat_channels(&try_format, &channels);
453 
454     hr = IAudioClient_IsFormatSupported(state->pAudioClient,
455                                         AUDCLNT_SHAREMODE_SHARED,
456                                         &try_format.Format,
457                                         &mix_format);
458     MP_VERBOSE(ao, "Trying %s (shared) -> %s\n",
459                waveformat_to_str(&try_format.Format), mp_format_res_str(hr));
460     if (hr != AUDCLNT_E_UNSUPPORTED_FORMAT)
461         EXIT_ON_ERROR(hr);
462 
463     switch (hr) {
464     case S_OK:
465         waveformat_copy(wformat, &try_format.Format);
466         break;
467     case S_FALSE:
468         waveformat_copy(wformat, mix_format);
469         CoTaskMemFree(mix_format);
470         MP_VERBOSE(ao, "Closest match is %s\n",
471                    waveformat_to_str(&wformat->Format));
472         break;
473     default:
474         hr = IAudioClient_GetMixFormat(state->pAudioClient, &mix_format);
475         EXIT_ON_ERROR(hr);
476         waveformat_copy(wformat, mix_format);
477         CoTaskMemFree(mix_format);
478         MP_VERBOSE(ao, "Fallback to mix format %s\n",
479                    waveformat_to_str(&wformat->Format));
480 
481     }
482 
483     return true;
484 exit_label:
485     MP_ERR(state, "Error finding shared mode format: %s\n",
486            mp_HRESULT_to_str(hr));
487     return false;
488 }
489 
find_formats(struct ao * ao)490 static bool find_formats(struct ao *ao)
491 {
492     struct wasapi_state *state = ao->priv;
493     struct mp_chmap channels = ao->channels;
494 
495     if (mp_chmap_is_unknown(&channels))
496         mp_chmap_from_channels(&channels, channels.num);
497     mp_chmap_reorder_to_waveext(&channels);
498     if (!mp_chmap_is_valid(&channels))
499         mp_chmap_from_channels(&channels, 2);
500 
501     struct wasapi_sample_fmt alt_formats[MP_ARRAY_SIZE(wasapi_formats)];
502     wasapi_get_best_sample_formats(ao->format, alt_formats);
503     struct wasapi_sample_fmt wasapi_format =
504         {AF_FORMAT_S16, 16, 16, &KSDATAFORMAT_SUBTYPE_PCM};;
505     if (alt_formats[0].mp_format)
506         wasapi_format = alt_formats[0];
507 
508     AUDCLNT_SHAREMODE share_mode;
509     WAVEFORMATEXTENSIBLE wformat;
510     set_waveformat(&wformat, &wasapi_format, ao->samplerate, &channels);
511 
512     if (state->opt_exclusive || af_fmt_is_spdif(ao->format)) {
513         share_mode = AUDCLNT_SHAREMODE_EXCLUSIVE;
514         if(!find_formats_exclusive(ao, &wformat))
515             return false;
516     } else {
517         share_mode = AUDCLNT_SHAREMODE_SHARED;
518         if(!find_formats_shared(ao, &wformat))
519             return false;
520     }
521 
522     return set_ao_format(ao, &wformat.Format, share_mode);
523 }
524 
init_clock(struct wasapi_state * state)525 static HRESULT init_clock(struct wasapi_state *state) {
526     HRESULT hr = IAudioClient_GetService(state->pAudioClient,
527                                          &IID_IAudioClock,
528                                          (void **)&state->pAudioClock);
529     EXIT_ON_ERROR(hr);
530     hr = IAudioClock_GetFrequency(state->pAudioClock, &state->clock_frequency);
531     EXIT_ON_ERROR(hr);
532 
533     QueryPerformanceFrequency(&state->qpc_frequency);
534 
535     atomic_store(&state->sample_count, 0);
536 
537     MP_VERBOSE(state,
538                "IAudioClock::GetFrequency gave a frequency of %"PRIu64".\n",
539                (uint64_t) state->clock_frequency);
540 
541     return S_OK;
542 exit_label:
543     MP_ERR(state, "Error obtaining the audio device's timing: %s\n",
544            mp_HRESULT_to_str(hr));
545     return hr;
546 }
547 
init_session_display(struct wasapi_state * state)548 static void init_session_display(struct wasapi_state *state) {
549     HRESULT hr = IAudioClient_GetService(state->pAudioClient,
550                                          &IID_IAudioSessionControl,
551                                          (void **)&state->pSessionControl);
552     EXIT_ON_ERROR(hr);
553 
554     wchar_t path[MAX_PATH] = {0};
555     GetModuleFileNameW(NULL, path, MAX_PATH);
556     hr = IAudioSessionControl_SetIconPath(state->pSessionControl, path, NULL);
557     if (FAILED(hr)) {
558         // don't goto exit_label here since SetDisplayName might still work
559         MP_WARN(state, "Error setting audio session icon: %s\n",
560                 mp_HRESULT_to_str(hr));
561     }
562 
563     hr = IAudioSessionControl_SetDisplayName(state->pSessionControl,
564                                              MIXER_DEFAULT_LABEL, NULL);
565     EXIT_ON_ERROR(hr);
566     return;
567 exit_label:
568     // if we got here then the session control is useless - release it
569     SAFE_RELEASE(state->pSessionControl);
570     MP_WARN(state, "Error setting audio session display name: %s\n",
571             mp_HRESULT_to_str(hr));
572     return;
573 }
574 
init_volume_control(struct wasapi_state * state)575 static void init_volume_control(struct wasapi_state *state)
576 {
577     HRESULT hr;
578     if (state->share_mode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
579         MP_DBG(state, "Activating pEndpointVolume interface\n");
580         hr = IMMDeviceActivator_Activate(state->pDevice,
581                                          &IID_IAudioEndpointVolume,
582                                          CLSCTX_ALL, NULL,
583                                          (void **)&state->pEndpointVolume);
584         EXIT_ON_ERROR(hr);
585 
586         MP_DBG(state, "IAudioEndpointVolume::QueryHardwareSupport\n");
587         hr = IAudioEndpointVolume_QueryHardwareSupport(state->pEndpointVolume,
588                                                        &state->vol_hw_support);
589         EXIT_ON_ERROR(hr);
590     } else {
591         MP_DBG(state, "IAudioClient::Initialize pAudioVolume\n");
592         hr = IAudioClient_GetService(state->pAudioClient,
593                                      &IID_ISimpleAudioVolume,
594                                      (void **)&state->pAudioVolume);
595         EXIT_ON_ERROR(hr);
596     }
597     return;
598 exit_label:
599     state->vol_hw_support = 0;
600     SAFE_RELEASE(state->pEndpointVolume);
601     SAFE_RELEASE(state->pAudioVolume);
602     MP_WARN(state, "Error setting up volume control: %s\n",
603             mp_HRESULT_to_str(hr));
604 }
605 
fix_format(struct ao * ao,bool align_hack)606 static HRESULT fix_format(struct ao *ao, bool align_hack)
607 {
608     struct wasapi_state *state = ao->priv;
609 
610     MP_DBG(state, "IAudioClient::GetDevicePeriod\n");
611     REFERENCE_TIME devicePeriod;
612     HRESULT hr = IAudioClient_GetDevicePeriod(state->pAudioClient,&devicePeriod,
613                                               NULL);
614     MP_VERBOSE(state, "Device period: %.2g ms\n",
615                (double) devicePeriod / 10000.0 );
616 
617     REFERENCE_TIME bufferDuration = devicePeriod;
618     if (state->share_mode == AUDCLNT_SHAREMODE_SHARED) {
619         // for shared mode, use integer multiple of device period close to 50ms
620         bufferDuration = devicePeriod * ceil(50.0 * 10000.0 / devicePeriod);
621     }
622 
623     // handle unsupported buffer size if AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED was
624     // returned in a previous attempt. hopefully this shouldn't happen because
625     // of the above integer device period
626     // http://msdn.microsoft.com/en-us/library/windows/desktop/dd370875%28v=vs.85%29.aspx
627     if (align_hack) {
628         bufferDuration = (REFERENCE_TIME) (0.5 +
629             (10000.0 * 1000 / state->format.Format.nSamplesPerSec
630              * state->bufferFrameCount));
631     }
632 
633     REFERENCE_TIME bufferPeriod =
634         state->share_mode == AUDCLNT_SHAREMODE_EXCLUSIVE ? bufferDuration : 0;
635 
636     MP_DBG(state, "IAudioClient::Initialize\n");
637     hr = IAudioClient_Initialize(state->pAudioClient,
638                                  state->share_mode,
639                                  AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
640                                  bufferDuration,
641                                  bufferPeriod,
642                                  &(state->format.Format),
643                                  NULL);
644     EXIT_ON_ERROR(hr);
645 
646     MP_DBG(state, "IAudioClient::Initialize pRenderClient\n");
647     hr = IAudioClient_GetService(state->pAudioClient,
648                                  &IID_IAudioRenderClient,
649                                  (void **)&state->pRenderClient);
650     EXIT_ON_ERROR(hr);
651 
652     MP_DBG(state, "IAudioClient::Initialize IAudioClient_SetEventHandle\n");
653     hr = IAudioClient_SetEventHandle(state->pAudioClient, state->hWake);
654     EXIT_ON_ERROR(hr);
655 
656     MP_DBG(state, "IAudioClient::Initialize IAudioClient_GetBufferSize\n");
657     hr = IAudioClient_GetBufferSize(state->pAudioClient,
658                                     &state->bufferFrameCount);
659     EXIT_ON_ERROR(hr);
660 
661     ao->device_buffer = state->bufferFrameCount;
662     bufferDuration = (REFERENCE_TIME) (0.5 +
663         (10000.0 * 1000 / state->format.Format.nSamplesPerSec
664          * state->bufferFrameCount));
665     MP_VERBOSE(state, "Buffer frame count: %"PRIu32" (%.2g ms)\n",
666                state->bufferFrameCount, (double) bufferDuration / 10000.0 );
667 
668     hr = init_clock(state);
669     EXIT_ON_ERROR(hr);
670 
671     init_session_display(state);
672     init_volume_control(state);
673 
674 #if !HAVE_UWP
675     state->hTask = AvSetMmThreadCharacteristics(L"Pro Audio", &(DWORD){0});
676     if (!state->hTask) {
677         MP_WARN(state, "Failed to set AV thread to Pro Audio: %s\n",
678                 mp_LastError_to_str());
679     }
680 #endif
681 
682     return S_OK;
683 exit_label:
684     MP_ERR(state, "Error initializing device: %s\n", mp_HRESULT_to_str(hr));
685     return hr;
686 }
687 
688 struct device_desc {
689     LPWSTR deviceID;
690     char *id;
691     char *name;
692 };
693 
get_device_name(struct mp_log * l,void * talloc_ctx,IMMDevice * pDevice)694 static char* get_device_name(struct mp_log *l, void *talloc_ctx, IMMDevice *pDevice)
695 {
696     char *namestr = NULL;
697     IPropertyStore *pProps = NULL;
698     PROPVARIANT devname;
699     PropVariantInit(&devname);
700 
701     HRESULT hr = IMMDevice_OpenPropertyStore(pDevice, STGM_READ, &pProps);
702     EXIT_ON_ERROR(hr);
703 
704     hr = IPropertyStore_GetValue(pProps, &mp_PKEY_Device_FriendlyName,
705                                  &devname);
706     EXIT_ON_ERROR(hr);
707 
708     namestr = mp_to_utf8(talloc_ctx, devname.pwszVal);
709 
710 exit_label:
711     if (FAILED(hr))
712         mp_warn(l, "Failed getting device name: %s\n", mp_HRESULT_to_str(hr));
713     PropVariantClear(&devname);
714     SAFE_RELEASE(pProps);
715     return namestr ? namestr : talloc_strdup(talloc_ctx, "");
716 }
717 
get_device_desc(struct mp_log * l,IMMDevice * pDevice)718 static struct device_desc *get_device_desc(struct mp_log *l, IMMDevice *pDevice)
719 {
720     LPWSTR deviceID;
721     HRESULT hr = IMMDevice_GetId(pDevice, &deviceID);
722     if (FAILED(hr)) {
723         mp_err(l, "Failed getting device id: %s\n", mp_HRESULT_to_str(hr));
724         return NULL;
725     }
726     struct device_desc *d = talloc_zero(NULL, struct device_desc);
727     d->deviceID = talloc_memdup(d, deviceID,
728                                 (wcslen(deviceID) + 1) * sizeof(wchar_t));
729     SAFE_DESTROY(deviceID, CoTaskMemFree(deviceID));
730 
731     char *full_id = mp_to_utf8(NULL, d->deviceID);
732     bstr id = bstr0(full_id);
733     bstr_eatstart0(&id, "{0.0.0.00000000}.");
734     d->id = bstrdup0(d, id);
735     talloc_free(full_id);
736 
737     d->name = get_device_name(l, d, pDevice);
738     return d;
739 }
740 
741 struct enumerator {
742     struct mp_log *log;
743     IMMDeviceEnumerator *pEnumerator;
744     IMMDeviceCollection *pDevices;
745     UINT count;
746 };
747 
destroy_enumerator(struct enumerator * e)748 static void destroy_enumerator(struct enumerator *e)
749 {
750     if (!e)
751         return;
752     SAFE_RELEASE(e->pDevices);
753     SAFE_RELEASE(e->pEnumerator);
754     talloc_free(e);
755 }
756 
create_enumerator(struct mp_log * log)757 static struct enumerator *create_enumerator(struct mp_log *log)
758 {
759     struct enumerator *e = talloc_zero(NULL, struct enumerator);
760     e->log = log;
761     HRESULT hr = CoCreateInstance(
762         &CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &IID_IMMDeviceEnumerator,
763         (void **)&e->pEnumerator);
764     EXIT_ON_ERROR(hr);
765 
766     hr = IMMDeviceEnumerator_EnumAudioEndpoints(
767         e->pEnumerator, eRender, DEVICE_STATE_ACTIVE, &e->pDevices);
768     EXIT_ON_ERROR(hr);
769 
770     hr = IMMDeviceCollection_GetCount(e->pDevices, &e->count);
771     EXIT_ON_ERROR(hr);
772 
773     return e;
774 exit_label:
775     mp_err(log, "Error getting device enumerator: %s\n", mp_HRESULT_to_str(hr));
776     destroy_enumerator(e);
777     return NULL;
778 }
779 
device_desc_for_num(struct enumerator * e,UINT i)780 static struct device_desc *device_desc_for_num(struct enumerator *e, UINT i)
781 {
782     IMMDevice *pDevice = NULL;
783     HRESULT hr = IMMDeviceCollection_Item(e->pDevices, i, &pDevice);
784     if (FAILED(hr)) {
785         MP_ERR(e, "Failed getting device #%d: %s\n", i, mp_HRESULT_to_str(hr));
786         return NULL;
787     }
788     struct device_desc *d = get_device_desc(e->log, pDevice);
789     SAFE_RELEASE(pDevice);
790     return d;
791 }
792 
default_device_desc(struct enumerator * e)793 static struct device_desc *default_device_desc(struct enumerator *e)
794 {
795     IMMDevice *pDevice = NULL;
796     HRESULT hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(
797         e->pEnumerator, eRender, eMultimedia, &pDevice);
798     if (FAILED(hr)) {
799         MP_ERR(e, "Error from GetDefaultAudioEndpoint: %s\n",
800                mp_HRESULT_to_str(hr));
801         return NULL;
802     }
803     struct device_desc *d = get_device_desc(e->log, pDevice);
804     SAFE_RELEASE(pDevice);
805     return d;
806 }
807 
wasapi_list_devs(struct ao * ao,struct ao_device_list * list)808 void wasapi_list_devs(struct ao *ao, struct ao_device_list *list)
809 {
810     struct enumerator *enumerator = create_enumerator(ao->log);
811     if (!enumerator)
812         return;
813 
814     for (UINT i = 0; i < enumerator->count; i++) {
815         struct device_desc *d = device_desc_for_num(enumerator, i);
816         if (!d)
817             goto exit_label;
818         ao_device_list_add(list, ao, &(struct ao_device_desc){d->id, d->name});
819         talloc_free(d);
820     }
821 
822 exit_label:
823     destroy_enumerator(enumerator);
824 }
825 
load_device(struct mp_log * l,IMMDevice ** ppDevice,LPWSTR deviceID)826 static bool load_device(struct mp_log *l,
827                            IMMDevice **ppDevice, LPWSTR deviceID)
828 {
829     IMMDeviceEnumerator *pEnumerator = NULL;
830     HRESULT hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
831                                   &IID_IMMDeviceEnumerator,
832                                   (void **)&pEnumerator);
833     EXIT_ON_ERROR(hr);
834 
835     hr = IMMDeviceEnumerator_GetDevice(pEnumerator, deviceID, ppDevice);
836     EXIT_ON_ERROR(hr);
837 
838 exit_label:
839     if (FAILED(hr))
840         mp_err(l, "Error loading selected device: %s\n", mp_HRESULT_to_str(hr));
841     SAFE_RELEASE(pEnumerator);
842     return SUCCEEDED(hr);
843 }
844 
select_device(struct mp_log * l,struct device_desc * d)845 static LPWSTR select_device(struct mp_log *l, struct device_desc *d)
846 {
847     if (!d)
848         return NULL;
849     mp_verbose(l, "Selecting device \'%s\' (%s)\n", d->id, d->name);
850     return talloc_memdup(NULL, d->deviceID,
851                          (wcslen(d->deviceID) + 1) * sizeof(wchar_t));
852 }
853 
wasapi_get_specified_device_string(struct ao * ao)854 bstr wasapi_get_specified_device_string(struct ao *ao)
855 {
856     return bstr_strip(bstr0(ao->device));
857 }
858 
wasapi_find_deviceID(struct ao * ao)859 LPWSTR wasapi_find_deviceID(struct ao *ao)
860 {
861     LPWSTR deviceID = NULL;
862     bstr device = wasapi_get_specified_device_string(ao);
863     MP_DBG(ao, "Find device \'%.*s\'\n", BSTR_P(device));
864 
865     struct device_desc *d = NULL;
866     struct enumerator *enumerator = create_enumerator(ao->log);
867     if (!enumerator)
868         goto exit_label;
869 
870     if (!enumerator->count) {
871         MP_ERR(ao, "There are no playback devices available\n");
872         goto exit_label;
873     }
874 
875     if (!device.len) {
876         MP_VERBOSE(ao, "No device specified. Selecting default.\n");
877         d = default_device_desc(enumerator);
878         deviceID = select_device(ao->log, d);
879         goto exit_label;
880     }
881 
882     // try selecting by number
883     bstr rest;
884     long long devno = bstrtoll(device, &rest, 10);
885     if (!rest.len && 0 <= devno && devno < (long long)enumerator->count) {
886         MP_VERBOSE(ao, "Selecting device by number: #%lld\n", devno);
887         d = device_desc_for_num(enumerator, devno);
888         deviceID = select_device(ao->log, d);
889         goto exit_label;
890     }
891 
892     // select by id or name
893     bstr_eatstart0(&device, "{0.0.0.00000000}.");
894     for (UINT i = 0; i < enumerator->count; i++) {
895         d = device_desc_for_num(enumerator, i);
896         if (!d)
897             goto exit_label;
898 
899         if (bstrcmp(device, bstr_strip(bstr0(d->id))) == 0) {
900             MP_VERBOSE(ao, "Selecting device by id: \'%.*s\'\n", BSTR_P(device));
901             deviceID = select_device(ao->log, d);
902             goto exit_label;
903         }
904 
905         if (bstrcmp(device, bstr_strip(bstr0(d->name))) == 0) {
906             if (!deviceID) {
907                 MP_VERBOSE(ao, "Selecting device by name: \'%.*s\'\n", BSTR_P(device));
908                 deviceID = select_device(ao->log, d);
909             } else {
910                 MP_WARN(ao, "Multiple devices matched \'%.*s\'."
911                         "Ignoring device \'%s\' (%s).\n",
912                         BSTR_P(device), d->id, d->name);
913             }
914         }
915         SAFE_DESTROY(d, talloc_free(d));
916     }
917 
918     if (!deviceID)
919         MP_ERR(ao, "Failed to find device \'%.*s\'\n", BSTR_P(device));
920 
921 exit_label:
922     talloc_free(d);
923     destroy_enumerator(enumerator);
924     return deviceID;
925 }
926 
wasapi_thread_init(struct ao * ao)927 bool wasapi_thread_init(struct ao *ao)
928 {
929     struct wasapi_state *state = ao->priv;
930     MP_DBG(ao, "Init wasapi thread\n");
931     int64_t retry_wait = 1;
932     bool align_hack = false;
933     HRESULT hr;
934 
935     ao->format = af_fmt_from_planar(ao->format);
936 
937 retry:
938     if (state->deviceID) {
939         if (!load_device(ao->log, &state->pDevice, state->deviceID))
940             return false;
941 
942         MP_DBG(ao, "Activating pAudioClient interface\n");
943         hr = IMMDeviceActivator_Activate(state->pDevice, &IID_IAudioClient,
944                                          CLSCTX_ALL, NULL,
945                                          (void **)&state->pAudioClient);
946         if (FAILED(hr)) {
947             MP_FATAL(ao, "Error activating device: %s\n",
948                      mp_HRESULT_to_str(hr));
949             return false;
950         }
951     } else {
952         MP_VERBOSE(ao, "Trying UWP wrapper.\n");
953 
954         HRESULT (*wuCreateDefaultAudioRenderer)(IUnknown **res) = NULL;
955         HANDLE lib = LoadLibraryW(L"wasapiuwp2.dll");
956         if (!lib) {
957             MP_ERR(ao, "Wrapper not found: %d\n", (int)GetLastError());
958             return false;
959         }
960 
961         wuCreateDefaultAudioRenderer =
962             (void*)GetProcAddress(lib, "wuCreateDefaultAudioRenderer");
963         if (!wuCreateDefaultAudioRenderer) {
964             MP_ERR(ao, "Function not found.\n");
965             return false;
966         }
967         IUnknown *res = NULL;
968         hr = wuCreateDefaultAudioRenderer(&res);
969         MP_VERBOSE(ao, "Device: %s %p\n", mp_HRESULT_to_str(hr), res);
970         if (FAILED(hr)) {
971             MP_FATAL(ao, "Error activating device: %s\n",
972                      mp_HRESULT_to_str(hr));
973             return false;
974         }
975         hr = IUnknown_QueryInterface(res, &IID_IAudioClient,
976                                      (void **)&state->pAudioClient);
977         IUnknown_Release(res);
978         if (FAILED(hr)) {
979             MP_FATAL(ao, "Failed to get UWP audio client: %s\n",
980                      mp_HRESULT_to_str(hr));
981             return false;
982         }
983     }
984 
985     // In the event of an align hack, we've already done this.
986     if (!align_hack) {
987         MP_DBG(ao, "Probing formats\n");
988         if (!find_formats(ao))
989             return false;
990     }
991 
992     MP_DBG(ao, "Fixing format\n");
993     hr = fix_format(ao, align_hack);
994     switch (hr) {
995     case AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED:
996         if (align_hack) {
997             MP_FATAL(ao, "Align hack failed\n");
998             break;
999         }
1000         // According to MSDN, we must use this as base after the failure.
1001         hr = IAudioClient_GetBufferSize(state->pAudioClient,
1002                                         &state->bufferFrameCount);
1003         if (FAILED(hr)) {
1004             MP_FATAL(ao, "Error getting buffer size for align hack: %s\n",
1005                      mp_HRESULT_to_str(hr));
1006             return false;
1007         }
1008         wasapi_thread_uninit(ao);
1009         align_hack = true;
1010         MP_WARN(ao, "This appears to require a weird Windows 7 hack. Retrying.\n");
1011         goto retry;
1012     case AUDCLNT_E_DEVICE_IN_USE:
1013     case AUDCLNT_E_DEVICE_INVALIDATED:
1014         if (retry_wait > 8) {
1015             MP_FATAL(ao, "Bad device retry failed\n");
1016             return false;
1017         }
1018         wasapi_thread_uninit(ao);
1019         MP_WARN(ao, "Retrying in %"PRId64" us\n", retry_wait);
1020         mp_sleep_us(retry_wait);
1021         retry_wait *= 2;
1022         goto retry;
1023     }
1024     return SUCCEEDED(hr);
1025 }
1026 
wasapi_thread_uninit(struct ao * ao)1027 void wasapi_thread_uninit(struct ao *ao)
1028 {
1029     struct wasapi_state *state = ao->priv;
1030     MP_DBG(ao, "Thread shutdown\n");
1031 
1032     if (state->pAudioClient)
1033         IAudioClient_Stop(state->pAudioClient);
1034 
1035     SAFE_RELEASE(state->pRenderClient);
1036     SAFE_RELEASE(state->pAudioClock);
1037     SAFE_RELEASE(state->pAudioVolume);
1038     SAFE_RELEASE(state->pEndpointVolume);
1039     SAFE_RELEASE(state->pSessionControl);
1040     SAFE_RELEASE(state->pAudioClient);
1041     SAFE_RELEASE(state->pDevice);
1042 #if !HAVE_UWP
1043     SAFE_DESTROY(state->hTask, AvRevertMmThreadCharacteristics(state->hTask));
1044 #endif
1045     MP_DBG(ao, "Thread uninit done\n");
1046 }
1047