1 /*
2  * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
3  * Copyright (C) 2018 Centricular Ltd.
4  *   Author: Nirbheek Chauhan <nirbheek@centricular.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24 
25 /* Note: initguid.h can not be included in gstwasapiutil.h, otherwise a
26  * symbol redefinition error will be raised.
27  * initguid.h must be included in the C file before mmdeviceapi.h
28  * which is included in gstwasapiutil.h.
29  */
30 #ifdef _MSC_VER
31 #include <initguid.h>
32 #endif
33 #include "gstwasapiutil.h"
34 #include "gstwasapidevice.h"
35 
36 GST_DEBUG_CATEGORY_EXTERN (gst_wasapi_debug);
37 #define GST_CAT_DEFAULT gst_wasapi_debug
38 
39 /* This was only added to MinGW in ~2015 and our Cerbero toolchain is too old */
40 #if defined(_MSC_VER)
41 #include <functiondiscoverykeys_devpkey.h>
42 #elif !defined(PKEY_Device_FriendlyName)
43 #include <initguid.h>
44 #include <propkey.h>
45 DEFINE_PROPERTYKEY (PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,
46     0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
47 DEFINE_PROPERTYKEY (PKEY_AudioEngine_DeviceFormat, 0xf19f064d, 0x82c, 0x4e27,
48     0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c, 0);
49 #endif
50 
51 /* __uuidof is only available in C++, so we hard-code the GUID values for all
52  * these. This is ok because these are ABI. */
53 const CLSID CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c,
54   {0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e}
55 };
56 
57 const IID IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35,
58   {0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6}
59 };
60 
61 const IID IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,
62   {0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5}
63 };
64 
65 const IID IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32,
66   {0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2}
67 };
68 
69 const IID IID_IAudioClient3 = { 0x7ed4ee07, 0x8e67, 0x4cd4,
70   {0x8c, 0x1a, 0x2b, 0x7a, 0x59, 0x87, 0xad, 0x42}
71 };
72 
73 const IID IID_IAudioClock = { 0xcd63314f, 0x3fba, 0x4a1b,
74   {0x81, 0x2c, 0xef, 0x96, 0x35, 0x87, 0x28, 0xe7}
75 };
76 
77 const IID IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0,
78   {0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17}
79 };
80 
81 const IID IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483,
82   {0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2}
83 };
84 
85 /* *INDENT-OFF* */
86 static struct
87 {
88   guint64 wasapi_pos;
89   GstAudioChannelPosition gst_pos;
90 } wasapi_to_gst_pos[] = {
91   {SPEAKER_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT},
92   {SPEAKER_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
93   {SPEAKER_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER},
94   {SPEAKER_LOW_FREQUENCY, GST_AUDIO_CHANNEL_POSITION_LFE1},
95   {SPEAKER_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT},
96   {SPEAKER_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
97   {SPEAKER_FRONT_LEFT_OF_CENTER,
98       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER},
99   {SPEAKER_FRONT_RIGHT_OF_CENTER,
100       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
101   {SPEAKER_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER},
102   /* Enum values diverge from this point onwards */
103   {SPEAKER_SIDE_LEFT, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT},
104   {SPEAKER_SIDE_RIGHT, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT},
105   {SPEAKER_TOP_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_CENTER},
106   {SPEAKER_TOP_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT},
107   {SPEAKER_TOP_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER},
108   {SPEAKER_TOP_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT},
109   {SPEAKER_TOP_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT},
110   {SPEAKER_TOP_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER},
111   {SPEAKER_TOP_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT}
112 };
113 /* *INDENT-ON* */
114 
115 static int windows_major_version = 0;
116 
117 gboolean
gst_wasapi_util_have_audioclient3(void)118 gst_wasapi_util_have_audioclient3 (void)
119 {
120   if (windows_major_version > 0)
121     return windows_major_version == 10;
122 
123   if (g_getenv ("GST_WASAPI_DISABLE_AUDIOCLIENT3") != NULL) {
124     windows_major_version = 6;
125     return FALSE;
126   }
127 
128   /* https://msdn.microsoft.com/en-us/library/windows/desktop/ms724834(v=vs.85).aspx */
129   windows_major_version = 6;
130   if (g_win32_check_windows_version (10, 0, 0, G_WIN32_OS_ANY))
131     windows_major_version = 10;
132 
133   return windows_major_version == 10;
134 }
135 
136 GType
gst_wasapi_device_role_get_type(void)137 gst_wasapi_device_role_get_type (void)
138 {
139   static const GEnumValue values[] = {
140     {GST_WASAPI_DEVICE_ROLE_CONSOLE,
141         "Games, system notifications, voice commands", "console"},
142     {GST_WASAPI_DEVICE_ROLE_MULTIMEDIA, "Music, movies, recorded media",
143         "multimedia"},
144     {GST_WASAPI_DEVICE_ROLE_COMMS, "Voice communications", "comms"},
145     {0, NULL, NULL}
146   };
147   static volatile GType id = 0;
148 
149   if (g_once_init_enter ((gsize *) & id)) {
150     GType _id;
151 
152     _id = g_enum_register_static ("GstWasapiDeviceRole", values);
153 
154     g_once_init_leave ((gsize *) & id, _id);
155   }
156 
157   return id;
158 }
159 
160 gint
gst_wasapi_device_role_to_erole(gint role)161 gst_wasapi_device_role_to_erole (gint role)
162 {
163   switch (role) {
164     case GST_WASAPI_DEVICE_ROLE_CONSOLE:
165       return eConsole;
166     case GST_WASAPI_DEVICE_ROLE_MULTIMEDIA:
167       return eMultimedia;
168     case GST_WASAPI_DEVICE_ROLE_COMMS:
169       return eCommunications;
170     default:
171       g_assert_not_reached ();
172   }
173 
174   return -1;
175 }
176 
177 gint
gst_wasapi_erole_to_device_role(gint erole)178 gst_wasapi_erole_to_device_role (gint erole)
179 {
180   switch (erole) {
181     case eConsole:
182       return GST_WASAPI_DEVICE_ROLE_CONSOLE;
183     case eMultimedia:
184       return GST_WASAPI_DEVICE_ROLE_MULTIMEDIA;
185     case eCommunications:
186       return GST_WASAPI_DEVICE_ROLE_COMMS;
187     default:
188       g_assert_not_reached ();
189   }
190 
191   return -1;
192 }
193 
194 static const gchar *
hresult_to_string_fallback(HRESULT hr)195 hresult_to_string_fallback (HRESULT hr)
196 {
197   const gchar *s = "unknown error";
198 
199   switch (hr) {
200     case AUDCLNT_E_NOT_INITIALIZED:
201       s = "AUDCLNT_E_NOT_INITIALIZED";
202       break;
203     case AUDCLNT_E_ALREADY_INITIALIZED:
204       s = "AUDCLNT_E_ALREADY_INITIALIZED";
205       break;
206     case AUDCLNT_E_WRONG_ENDPOINT_TYPE:
207       s = "AUDCLNT_E_WRONG_ENDPOINT_TYPE";
208       break;
209     case AUDCLNT_E_DEVICE_INVALIDATED:
210       s = "AUDCLNT_E_DEVICE_INVALIDATED";
211       break;
212     case AUDCLNT_E_NOT_STOPPED:
213       s = "AUDCLNT_E_NOT_STOPPED";
214       break;
215     case AUDCLNT_E_BUFFER_TOO_LARGE:
216       s = "AUDCLNT_E_BUFFER_TOO_LARGE";
217       break;
218     case AUDCLNT_E_OUT_OF_ORDER:
219       s = "AUDCLNT_E_OUT_OF_ORDER";
220       break;
221     case AUDCLNT_E_UNSUPPORTED_FORMAT:
222       s = "AUDCLNT_E_UNSUPPORTED_FORMAT";
223       break;
224     case AUDCLNT_E_INVALID_DEVICE_PERIOD:
225       s = "AUDCLNT_E_INVALID_DEVICE_PERIOD";
226       break;
227     case AUDCLNT_E_INVALID_SIZE:
228       s = "AUDCLNT_E_INVALID_SIZE";
229       break;
230     case AUDCLNT_E_DEVICE_IN_USE:
231       s = "AUDCLNT_E_DEVICE_IN_USE";
232       break;
233     case AUDCLNT_E_BUFFER_OPERATION_PENDING:
234       s = "AUDCLNT_E_BUFFER_OPERATION_PENDING";
235       break;
236     case AUDCLNT_E_BUFFER_SIZE_ERROR:
237       s = "AUDCLNT_E_BUFFER_SIZE_ERROR";
238       break;
239     case AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED:
240       s = "AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED";
241       break;
242     case AUDCLNT_E_THREAD_NOT_REGISTERED:
243       s = "AUDCLNT_E_THREAD_NOT_REGISTERED";
244       break;
245     case AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED:
246       s = "AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED";
247       break;
248     case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
249       s = "AUDCLNT_E_ENDPOINT_CREATE_FAILED";
250       break;
251     case AUDCLNT_E_SERVICE_NOT_RUNNING:
252       s = "AUDCLNT_E_SERVICE_NOT_RUNNING";
253       break;
254     case AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED:
255       s = "AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED";
256       break;
257     case AUDCLNT_E_EXCLUSIVE_MODE_ONLY:
258       s = "AUDCLNT_E_EXCLUSIVE_MODE_ONLY";
259       break;
260     case AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL:
261       s = "AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL";
262       break;
263     case AUDCLNT_E_EVENTHANDLE_NOT_SET:
264       s = "AUDCLNT_E_EVENTHANDLE_NOT_SET";
265       break;
266     case AUDCLNT_E_INCORRECT_BUFFER_SIZE:
267       s = "AUDCLNT_E_INCORRECT_BUFFER_SIZE";
268       break;
269     case AUDCLNT_E_CPUUSAGE_EXCEEDED:
270       s = "AUDCLNT_E_CPUUSAGE_EXCEEDED";
271       break;
272     case AUDCLNT_S_BUFFER_EMPTY:
273       s = "AUDCLNT_S_BUFFER_EMPTY";
274       break;
275     case AUDCLNT_S_THREAD_ALREADY_REGISTERED:
276       s = "AUDCLNT_S_THREAD_ALREADY_REGISTERED";
277       break;
278     case AUDCLNT_S_POSITION_STALLED:
279       s = "AUDCLNT_S_POSITION_STALLED";
280       break;
281     case E_POINTER:
282       s = "E_POINTER";
283       break;
284     case E_INVALIDARG:
285       s = "E_INVALIDARG";
286       break;
287   }
288 
289   return s;
290 }
291 
292 gchar *
gst_wasapi_util_hresult_to_string(HRESULT hr)293 gst_wasapi_util_hresult_to_string (HRESULT hr)
294 {
295   DWORD flags;
296   gchar *ret_text;
297   LPTSTR error_text = NULL;
298 
299   flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
300       | FORMAT_MESSAGE_IGNORE_INSERTS;
301   FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
302       (LPTSTR) & error_text, 0, NULL);
303 
304   /* If we couldn't get the error msg, try the fallback switch statement */
305   if (error_text == NULL)
306     return g_strdup (hresult_to_string_fallback (hr));
307 
308 #ifdef UNICODE
309   /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
310   ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
311 #else
312   ret_text = g_strdup (error_text);
313 #endif
314 
315   LocalFree (error_text);
316   return ret_text;
317 }
318 
319 static IMMDeviceEnumerator *
gst_wasapi_util_get_device_enumerator(GstObject * self)320 gst_wasapi_util_get_device_enumerator (GstObject * self)
321 {
322   HRESULT hr;
323   IMMDeviceEnumerator *enumerator = NULL;
324 
325   hr = CoCreateInstance (&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
326       &IID_IMMDeviceEnumerator, (void **) &enumerator);
327   HR_FAILED_RET (hr, CoCreateInstance (MMDeviceEnumerator), NULL);
328 
329   return enumerator;
330 }
331 
332 gboolean
gst_wasapi_util_get_devices(GstObject * self,gboolean active,GList ** devices)333 gst_wasapi_util_get_devices (GstObject * self, gboolean active,
334     GList ** devices)
335 {
336   gboolean res = FALSE;
337   static GstStaticCaps scaps = GST_STATIC_CAPS (GST_WASAPI_STATIC_CAPS);
338   DWORD dwStateMask = active ? DEVICE_STATE_ACTIVE : DEVICE_STATEMASK_ALL;
339   IMMDeviceCollection *device_collection = NULL;
340   IMMDeviceEnumerator *enumerator = NULL;
341   const gchar *device_class, *element_name;
342   guint ii, count;
343   HRESULT hr;
344 
345   *devices = NULL;
346 
347   enumerator = gst_wasapi_util_get_device_enumerator (self);
348   if (!enumerator)
349     return FALSE;
350 
351   hr = IMMDeviceEnumerator_EnumAudioEndpoints (enumerator, eAll, dwStateMask,
352       &device_collection);
353   HR_FAILED_GOTO (hr, IMMDeviceEnumerator::EnumAudioEndpoints, err);
354 
355   hr = IMMDeviceCollection_GetCount (device_collection, &count);
356   HR_FAILED_GOTO (hr, IMMDeviceCollection::GetCount, err);
357 
358   /* Create a GList of GstDevices* to return */
359   for (ii = 0; ii < count; ii++) {
360     IMMDevice *item = NULL;
361     IMMEndpoint *endpoint = NULL;
362     IAudioClient *client = NULL;
363     IPropertyStore *prop_store = NULL;
364     WAVEFORMATEX *format = NULL;
365     gchar *description = NULL;
366     gchar *strid = NULL;
367     EDataFlow dataflow;
368     PROPVARIANT var;
369     wchar_t *wstrid;
370     GstDevice *device;
371     GstStructure *props;
372     GstCaps *caps;
373 
374     hr = IMMDeviceCollection_Item (device_collection, ii, &item);
375     if (hr != S_OK)
376       continue;
377 
378     hr = IMMDevice_QueryInterface (item, &IID_IMMEndpoint, (void **) &endpoint);
379     if (hr != S_OK)
380       goto next;
381 
382     hr = IMMEndpoint_GetDataFlow (endpoint, &dataflow);
383     if (hr != S_OK)
384       goto next;
385 
386     if (dataflow == eRender) {
387       device_class = "Audio/Sink";
388       element_name = "wasapisink";
389     } else {
390       device_class = "Audio/Source";
391       element_name = "wasapisrc";
392     }
393 
394     PropVariantInit (&var);
395 
396     hr = IMMDevice_GetId (item, &wstrid);
397     if (hr != S_OK)
398       goto next;
399     strid = g_utf16_to_utf8 (wstrid, -1, NULL, NULL, NULL);
400     CoTaskMemFree (wstrid);
401 
402     hr = IMMDevice_OpenPropertyStore (item, STGM_READ, &prop_store);
403     if (hr != S_OK)
404       goto next;
405 
406     /* NOTE: More properties can be added as needed from here:
407      * https://msdn.microsoft.com/en-us/library/windows/desktop/dd370794(v=vs.85).aspx */
408     hr = IPropertyStore_GetValue (prop_store, &PKEY_Device_FriendlyName, &var);
409     if (hr != S_OK)
410       goto next;
411     description = g_utf16_to_utf8 (var.pwszVal, -1, NULL, NULL, NULL);
412     PropVariantClear (&var);
413 
414     /* Get the audio client so we can fetch the mix format for shared mode
415      * to get the device format for exclusive mode (or something close to that)
416      * fetch PKEY_AudioEngine_DeviceFormat from the property store. */
417     hr = IMMDevice_Activate (item, &IID_IAudioClient, CLSCTX_ALL, NULL,
418         (void **) &client);
419     if (hr != S_OK) {
420       gchar *msg = gst_wasapi_util_hresult_to_string (hr);
421       GST_ERROR_OBJECT (self, "IMMDevice::Activate (IID_IAudioClient) failed"
422           "on %s: %s", strid, msg);
423       g_free (msg);
424       goto next;
425     }
426 
427     hr = IAudioClient_GetMixFormat (client, &format);
428     if (hr != S_OK || format == NULL) {
429       gchar *msg = gst_wasapi_util_hresult_to_string (hr);
430       GST_ERROR_OBJECT (self, "GetMixFormat failed on %s: %s", strid, msg);
431       g_free (msg);
432       goto next;
433     }
434 
435     if (!gst_wasapi_util_parse_waveformatex ((WAVEFORMATEXTENSIBLE *) format,
436             gst_static_caps_get (&scaps), &caps, NULL))
437       goto next;
438 
439     /* Set some useful properties */
440     props = gst_structure_new ("wasapi-proplist",
441         "device.api", G_TYPE_STRING, "wasapi",
442         "device.strid", G_TYPE_STRING, GST_STR_NULL (strid),
443         "wasapi.device.description", G_TYPE_STRING, description, NULL);
444 
445     device = g_object_new (GST_TYPE_WASAPI_DEVICE, "device", strid,
446         "display-name", description, "caps", caps,
447         "device-class", device_class, "properties", props, NULL);
448     GST_WASAPI_DEVICE (device)->element = element_name;
449 
450     gst_structure_free (props);
451     gst_caps_unref (caps);
452     *devices = g_list_prepend (*devices, device);
453 
454   next:
455     PropVariantClear (&var);
456     if (prop_store)
457       IUnknown_Release (prop_store);
458     if (endpoint)
459       IUnknown_Release (endpoint);
460     if (client)
461       IUnknown_Release (client);
462     if (item)
463       IUnknown_Release (item);
464     if (description)
465       g_free (description);
466     if (strid)
467       g_free (strid);
468   }
469 
470   res = TRUE;
471 
472 err:
473   if (enumerator)
474     IUnknown_Release (enumerator);
475   if (device_collection)
476     IUnknown_Release (device_collection);
477   return res;
478 }
479 
480 gboolean
gst_wasapi_util_get_device_format(GstElement * self,gint device_mode,IMMDevice * device,IAudioClient * client,WAVEFORMATEX ** ret_format)481 gst_wasapi_util_get_device_format (GstElement * self,
482     gint device_mode, IMMDevice * device, IAudioClient * client,
483     WAVEFORMATEX ** ret_format)
484 {
485   WAVEFORMATEX *format;
486   HRESULT hr;
487 
488   *ret_format = NULL;
489 
490   hr = IAudioClient_GetMixFormat (client, &format);
491   HR_FAILED_RET (hr, IAudioClient::GetMixFormat, FALSE);
492 
493   /* WASAPI always accepts the format returned by GetMixFormat in shared mode */
494   if (device_mode == AUDCLNT_SHAREMODE_SHARED)
495     goto out;
496 
497   /* WASAPI may or may not support this format in exclusive mode */
498   hr = IAudioClient_IsFormatSupported (client, AUDCLNT_SHAREMODE_EXCLUSIVE,
499       format, NULL);
500   if (hr == S_OK)
501     goto out;
502 
503   CoTaskMemFree (format);
504 
505   /* Open the device property store, and get the format that WASAPI has been
506    * using for sending data to the device */
507   {
508     PROPVARIANT var;
509     IPropertyStore *prop_store = NULL;
510 
511     hr = IMMDevice_OpenPropertyStore (device, STGM_READ, &prop_store);
512     HR_FAILED_RET (hr, IMMDevice::OpenPropertyStore, FALSE);
513 
514     hr = IPropertyStore_GetValue (prop_store, &PKEY_AudioEngine_DeviceFormat,
515         &var);
516     if (hr != S_OK) {
517       gchar *msg = gst_wasapi_util_hresult_to_string (hr);
518       GST_ERROR_OBJECT (self, "GetValue failed: %s", msg);
519       g_free (msg);
520       IUnknown_Release (prop_store);
521       return FALSE;
522     }
523 
524     format = malloc (var.blob.cbSize);
525     memcpy (format, var.blob.pBlobData, var.blob.cbSize);
526 
527     PropVariantClear (&var);
528     IUnknown_Release (prop_store);
529   }
530 
531   /* WASAPI may or may not support this format in exclusive mode */
532   hr = IAudioClient_IsFormatSupported (client, AUDCLNT_SHAREMODE_EXCLUSIVE,
533       format, NULL);
534   if (hr == S_OK)
535     goto out;
536 
537   GST_ERROR_OBJECT (self, "AudioEngine DeviceFormat not supported");
538   free (format);
539   return FALSE;
540 
541 out:
542   *ret_format = format;
543   return TRUE;
544 }
545 
546 gboolean
gst_wasapi_util_get_device_client(GstElement * self,gint data_flow,gint role,const wchar_t * device_strid,IMMDevice ** ret_device,IAudioClient ** ret_client)547 gst_wasapi_util_get_device_client (GstElement * self,
548     gint data_flow, gint role, const wchar_t * device_strid,
549     IMMDevice ** ret_device, IAudioClient ** ret_client)
550 {
551   gboolean res = FALSE;
552   HRESULT hr;
553   IMMDeviceEnumerator *enumerator = NULL;
554   IMMDevice *device = NULL;
555   IAudioClient *client = NULL;
556 
557   if (!(enumerator = gst_wasapi_util_get_device_enumerator (GST_OBJECT (self))))
558     goto beach;
559 
560   if (!device_strid) {
561     hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint (enumerator, data_flow,
562         role, &device);
563     HR_FAILED_GOTO (hr, IMMDeviceEnumerator::GetDefaultAudioEndpoint, beach);
564   } else {
565     hr = IMMDeviceEnumerator_GetDevice (enumerator, device_strid, &device);
566     if (hr != S_OK) {
567       gchar *msg = gst_wasapi_util_hresult_to_string (hr);
568       GST_ERROR_OBJECT (self, "IMMDeviceEnumerator::GetDevice (%S) failed"
569           ": %s", device_strid, msg);
570       g_free (msg);
571       goto beach;
572     }
573   }
574 
575   if (gst_wasapi_util_have_audioclient3 ())
576     hr = IMMDevice_Activate (device, &IID_IAudioClient3, CLSCTX_ALL, NULL,
577         (void **) &client);
578   else
579     hr = IMMDevice_Activate (device, &IID_IAudioClient, CLSCTX_ALL, NULL,
580         (void **) &client);
581   HR_FAILED_GOTO (hr, IMMDevice::Activate (IID_IAudioClient), beach);
582 
583   IUnknown_AddRef (client);
584   IUnknown_AddRef (device);
585   *ret_client = client;
586   *ret_device = device;
587 
588   res = TRUE;
589 
590 beach:
591   if (client != NULL)
592     IUnknown_Release (client);
593 
594   if (device != NULL)
595     IUnknown_Release (device);
596 
597   if (enumerator != NULL)
598     IUnknown_Release (enumerator);
599 
600   return res;
601 }
602 
603 gboolean
gst_wasapi_util_get_render_client(GstElement * self,IAudioClient * client,IAudioRenderClient ** ret_render_client)604 gst_wasapi_util_get_render_client (GstElement * self, IAudioClient * client,
605     IAudioRenderClient ** ret_render_client)
606 {
607   gboolean res = FALSE;
608   HRESULT hr;
609   IAudioRenderClient *render_client = NULL;
610 
611   hr = IAudioClient_GetService (client, &IID_IAudioRenderClient,
612       (void **) &render_client);
613   HR_FAILED_GOTO (hr, IAudioClient::GetService, beach);
614 
615   *ret_render_client = render_client;
616   res = TRUE;
617 
618 beach:
619   return res;
620 }
621 
622 gboolean
gst_wasapi_util_get_capture_client(GstElement * self,IAudioClient * client,IAudioCaptureClient ** ret_capture_client)623 gst_wasapi_util_get_capture_client (GstElement * self, IAudioClient * client,
624     IAudioCaptureClient ** ret_capture_client)
625 {
626   gboolean res = FALSE;
627   HRESULT hr;
628   IAudioCaptureClient *capture_client = NULL;
629 
630   hr = IAudioClient_GetService (client, &IID_IAudioCaptureClient,
631       (void **) &capture_client);
632   HR_FAILED_GOTO (hr, IAudioClient::GetService, beach);
633 
634   *ret_capture_client = capture_client;
635   res = TRUE;
636 
637 beach:
638   return res;
639 }
640 
641 gboolean
gst_wasapi_util_get_clock(GstElement * self,IAudioClient * client,IAudioClock ** ret_clock)642 gst_wasapi_util_get_clock (GstElement * self, IAudioClient * client,
643     IAudioClock ** ret_clock)
644 {
645   gboolean res = FALSE;
646   HRESULT hr;
647   IAudioClock *clock = NULL;
648 
649   hr = IAudioClient_GetService (client, &IID_IAudioClock, (void **) &clock);
650   HR_FAILED_GOTO (hr, IAudioClient::GetService, beach);
651 
652   *ret_clock = clock;
653   res = TRUE;
654 
655 beach:
656   return res;
657 }
658 
659 static const gchar *
gst_waveformatex_to_audio_format(WAVEFORMATEXTENSIBLE * format)660 gst_waveformatex_to_audio_format (WAVEFORMATEXTENSIBLE * format)
661 {
662   const gchar *fmt_str = NULL;
663   GstAudioFormat fmt = GST_AUDIO_FORMAT_UNKNOWN;
664 
665   if (format->Format.wFormatTag == WAVE_FORMAT_PCM) {
666     fmt = gst_audio_format_build_integer (TRUE, G_LITTLE_ENDIAN,
667         format->Format.wBitsPerSample, format->Format.wBitsPerSample);
668   } else if (format->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
669     if (format->Format.wBitsPerSample == 32)
670       fmt = GST_AUDIO_FORMAT_F32LE;
671     else if (format->Format.wBitsPerSample == 64)
672       fmt = GST_AUDIO_FORMAT_F64LE;
673   } else if (format->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
674     if (IsEqualGUID (&format->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
675       fmt = gst_audio_format_build_integer (TRUE, G_LITTLE_ENDIAN,
676           format->Format.wBitsPerSample, format->Samples.wValidBitsPerSample);
677     } else if (IsEqualGUID (&format->SubFormat,
678             &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
679       if (format->Format.wBitsPerSample == 32
680           && format->Samples.wValidBitsPerSample == 32)
681         fmt = GST_AUDIO_FORMAT_F32LE;
682       else if (format->Format.wBitsPerSample == 64 &&
683           format->Samples.wValidBitsPerSample == 64)
684         fmt = GST_AUDIO_FORMAT_F64LE;
685     }
686   }
687 
688   if (fmt != GST_AUDIO_FORMAT_UNKNOWN)
689     fmt_str = gst_audio_format_to_string (fmt);
690 
691   return fmt_str;
692 }
693 
694 static void
gst_wasapi_util_channel_position_all_none(guint channels,GstAudioChannelPosition * position)695 gst_wasapi_util_channel_position_all_none (guint channels,
696     GstAudioChannelPosition * position)
697 {
698   int ii;
699   for (ii = 0; ii < channels; ii++)
700     position[ii] = GST_AUDIO_CHANNEL_POSITION_NONE;
701 }
702 
703 /* Parse WAVEFORMATEX to get the gstreamer channel mask, and the wasapi channel
704  * positions so GstAudioRingbuffer can reorder the audio data to match the
705  * gstreamer channel order. */
706 static guint64
gst_wasapi_util_waveformatex_to_channel_mask(WAVEFORMATEXTENSIBLE * format,GstAudioChannelPosition ** out_position)707 gst_wasapi_util_waveformatex_to_channel_mask (WAVEFORMATEXTENSIBLE * format,
708     GstAudioChannelPosition ** out_position)
709 {
710   int ii, ch;
711   guint64 mask = 0;
712   WORD nChannels = format->Format.nChannels;
713   DWORD dwChannelMask = format->dwChannelMask;
714   GstAudioChannelPosition *pos = NULL;
715 
716   pos = g_new (GstAudioChannelPosition, nChannels);
717   gst_wasapi_util_channel_position_all_none (nChannels, pos);
718 
719   /* Too many channels, have to assume that they are all non-positional */
720   if (nChannels > G_N_ELEMENTS (wasapi_to_gst_pos)) {
721     GST_INFO ("Got too many (%i) channels, assuming non-positional", nChannels);
722     goto out;
723   }
724 
725   /* Too many bits in the channel mask, and the bits don't match nChannels */
726   if (dwChannelMask >> (G_N_ELEMENTS (wasapi_to_gst_pos) + 1) != 0) {
727     GST_WARNING ("Too many bits in channel mask (%lu), assuming "
728         "non-positional", dwChannelMask);
729     goto out;
730   }
731 
732   /* Map WASAPI's channel mask to Gstreamer's channel mask and positions.
733    * If the no. of bits in the mask > nChannels, we will ignore the extra. */
734   for (ii = 0, ch = 0; ii < G_N_ELEMENTS (wasapi_to_gst_pos) && ch < nChannels;
735       ii++) {
736     if (!(dwChannelMask & wasapi_to_gst_pos[ii].wasapi_pos))
737       /* no match, try next */
738       continue;
739     mask |= G_GUINT64_CONSTANT (1) << wasapi_to_gst_pos[ii].gst_pos;
740     pos[ch++] = wasapi_to_gst_pos[ii].gst_pos;
741   }
742 
743   /* XXX: Warn if some channel masks couldn't be mapped? */
744 
745   GST_DEBUG ("Converted WASAPI mask 0x%" G_GINT64_MODIFIER "x -> 0x%"
746       G_GINT64_MODIFIER "x", (guint64) dwChannelMask, (guint64) mask);
747 
748 out:
749   if (out_position)
750     *out_position = pos;
751   return mask;
752 }
753 
754 gboolean
gst_wasapi_util_parse_waveformatex(WAVEFORMATEXTENSIBLE * format,GstCaps * template_caps,GstCaps ** out_caps,GstAudioChannelPosition ** out_positions)755 gst_wasapi_util_parse_waveformatex (WAVEFORMATEXTENSIBLE * format,
756     GstCaps * template_caps, GstCaps ** out_caps,
757     GstAudioChannelPosition ** out_positions)
758 {
759   int ii;
760   const gchar *afmt;
761   guint64 channel_mask;
762 
763   *out_caps = NULL;
764 
765   /* TODO: handle SPDIF and other encoded formats */
766 
767   /* 1 or 2 channels <= 16 bits sample size OR
768    * 1 or 2 channels > 16 bits sample size or >2 channels */
769   if (format->Format.wFormatTag != WAVE_FORMAT_PCM &&
770       format->Format.wFormatTag != WAVE_FORMAT_IEEE_FLOAT &&
771       format->Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE)
772     /* Unhandled format tag */
773     return FALSE;
774 
775   /* WASAPI can only tell us one canonical mix format that it will accept. The
776    * alternative is calling IsFormatSupported on all combinations of formats.
777    * Instead, it's simpler and faster to require conversion inside gstreamer */
778   afmt = gst_waveformatex_to_audio_format (format);
779   if (afmt == NULL)
780     return FALSE;
781 
782   *out_caps = gst_caps_copy (template_caps);
783 
784   /* This will always return something that might be usable */
785   channel_mask =
786       gst_wasapi_util_waveformatex_to_channel_mask (format, out_positions);
787 
788   for (ii = 0; ii < gst_caps_get_size (*out_caps); ii++) {
789     GstStructure *s = gst_caps_get_structure (*out_caps, ii);
790 
791     gst_structure_set (s,
792         "format", G_TYPE_STRING, afmt,
793         "channels", G_TYPE_INT, format->Format.nChannels,
794         "rate", G_TYPE_INT, format->Format.nSamplesPerSec, NULL);
795 
796     if (channel_mask) {
797       gst_structure_set (s,
798           "channel-mask", GST_TYPE_BITMASK, channel_mask, NULL);
799     }
800   }
801 
802   return TRUE;
803 }
804 
805 void
gst_wasapi_util_get_best_buffer_sizes(GstAudioRingBufferSpec * spec,gboolean exclusive,REFERENCE_TIME default_period,REFERENCE_TIME min_period,REFERENCE_TIME * ret_period,REFERENCE_TIME * ret_buffer_duration)806 gst_wasapi_util_get_best_buffer_sizes (GstAudioRingBufferSpec * spec,
807     gboolean exclusive, REFERENCE_TIME default_period,
808     REFERENCE_TIME min_period, REFERENCE_TIME * ret_period,
809     REFERENCE_TIME * ret_buffer_duration)
810 {
811   REFERENCE_TIME use_period, use_buffer;
812 
813   /* Figure out what integral device period to use as the base */
814   if (exclusive) {
815     /* Exclusive mode can run at multiples of either the minimum period or the
816      * default period; these are on the hardware ringbuffer */
817     if (spec->latency_time * 10 > default_period)
818       use_period = default_period;
819     else
820       use_period = min_period;
821   } else {
822     /* Shared mode always runs at the default period, so if we want a larger
823      * period (for lower CPU usage), we do it as a multiple of that */
824     use_period = default_period;
825   }
826 
827   /* Ensure that the period (latency_time) used is an integral multiple of
828    * either the default period or the minimum period */
829   use_period = use_period * MAX ((spec->latency_time * 10) / use_period, 1);
830 
831   if (exclusive) {
832     /* Buffer duration is the same as the period in exclusive mode. The
833      * hardware is always writing out one buffer (of size *ret_period), and
834      * we're writing to the other one. */
835     use_buffer = use_period;
836   } else {
837     /* Ask WASAPI to create a software ringbuffer of at least this size; it may
838      * be larger so the actual buffer time may be different, which is why after
839      * initialization we read the buffer duration actually in-use and set
840      * segsize/segtotal from that. */
841     use_buffer = spec->buffer_time * 10;
842     /* Has to be at least twice the period */
843     if (use_buffer < 2 * use_period)
844       use_buffer = 2 * use_period;
845   }
846 
847   *ret_period = use_period;
848   *ret_buffer_duration = use_buffer;
849 }
850 
851 gboolean
gst_wasapi_util_initialize_audioclient(GstElement * self,GstAudioRingBufferSpec * spec,IAudioClient * client,WAVEFORMATEX * format,guint sharemode,gboolean low_latency,gboolean loopback,guint * ret_devicep_frames)852 gst_wasapi_util_initialize_audioclient (GstElement * self,
853     GstAudioRingBufferSpec * spec, IAudioClient * client,
854     WAVEFORMATEX * format, guint sharemode, gboolean low_latency,
855     gboolean loopback, guint * ret_devicep_frames)
856 {
857   REFERENCE_TIME default_period, min_period;
858   REFERENCE_TIME device_period, device_buffer_duration;
859   guint rate, stream_flags;
860   guint32 n_frames;
861   HRESULT hr;
862 
863   hr = IAudioClient_GetDevicePeriod (client, &default_period, &min_period);
864   HR_FAILED_RET (hr, IAudioClient::GetDevicePeriod, FALSE);
865 
866   GST_INFO_OBJECT (self, "wasapi default period: %" G_GINT64_FORMAT
867       ", min period: %" G_GINT64_FORMAT, default_period, min_period);
868 
869   rate = GST_AUDIO_INFO_RATE (&spec->info);
870 
871   if (low_latency) {
872     if (sharemode == AUDCLNT_SHAREMODE_SHARED) {
873       device_period = default_period;
874       device_buffer_duration = 0;
875     } else {
876       device_period = min_period;
877       device_buffer_duration = min_period;
878     }
879   } else {
880     /* Clamp values to integral multiples of an appropriate period */
881     gst_wasapi_util_get_best_buffer_sizes (spec,
882         sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE, default_period,
883         min_period, &device_period, &device_buffer_duration);
884   }
885 
886   stream_flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
887   if (loopback)
888     stream_flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
889 
890   hr = IAudioClient_Initialize (client, sharemode, stream_flags,
891       device_buffer_duration,
892       /* This must always be 0 in shared mode */
893       sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : device_period, format, NULL);
894 
895   if (hr == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED &&
896       sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
897     GST_WARNING_OBJECT (self, "initialize failed due to unaligned period %i",
898         (int) device_period);
899 
900     /* Calculate a new aligned period. First get the aligned buffer size. */
901     hr = IAudioClient_GetBufferSize (client, &n_frames);
902     HR_FAILED_RET (hr, IAudioClient::GetBufferSize, FALSE);
903 
904     device_period = (GST_SECOND / 100) * n_frames / rate;
905 
906     GST_WARNING_OBJECT (self, "trying to re-initialize with period %i "
907         "(%i frames, %i rate)", (int) device_period, n_frames, rate);
908 
909     hr = IAudioClient_Initialize (client, sharemode, stream_flags,
910         device_period, device_period, format, NULL);
911   }
912   HR_FAILED_RET (hr, IAudioClient::Initialize, FALSE);
913 
914   if (sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
915     /* We use the device period for the segment size and that needs to match
916      * the buffer size exactly when we write into it */
917     hr = IAudioClient_GetBufferSize (client, &n_frames);
918     HR_FAILED_RET (hr, IAudioClient::GetBufferSize, FALSE);
919 
920     *ret_devicep_frames = n_frames;
921   } else {
922     /* device_period can be a non-power-of-10 value so round while converting */
923     *ret_devicep_frames =
924         gst_util_uint64_scale_round (device_period, rate * 100, GST_SECOND);
925   }
926 
927   return TRUE;
928 }
929 
930 gboolean
gst_wasapi_util_initialize_audioclient3(GstElement * self,GstAudioRingBufferSpec * spec,IAudioClient3 * client,WAVEFORMATEX * format,gboolean low_latency,gboolean loopback,guint * ret_devicep_frames)931 gst_wasapi_util_initialize_audioclient3 (GstElement * self,
932     GstAudioRingBufferSpec * spec, IAudioClient3 * client,
933     WAVEFORMATEX * format, gboolean low_latency, gboolean loopback,
934     guint * ret_devicep_frames)
935 {
936   HRESULT hr;
937   gint stream_flags;
938   guint devicep_frames;
939   guint defaultp_frames, fundp_frames, minp_frames, maxp_frames;
940   WAVEFORMATEX *tmpf;
941 
942   hr = IAudioClient3_GetSharedModeEnginePeriod (client, format,
943       &defaultp_frames, &fundp_frames, &minp_frames, &maxp_frames);
944   HR_FAILED_RET (hr, IAudioClient3::GetSharedModeEnginePeriod, FALSE);
945 
946   GST_INFO_OBJECT (self, "Using IAudioClient3, default period %i frames, "
947       "fundamental period %i frames, minimum period %i frames, maximum period "
948       "%i frames", defaultp_frames, fundp_frames, minp_frames, maxp_frames);
949 
950   if (low_latency)
951     devicep_frames = minp_frames;
952   else
953     /* Just pick the max period, because lower values can cause glitches
954      * https://bugzilla.gnome.org/show_bug.cgi?id=794497 */
955     devicep_frames = maxp_frames;
956 
957   stream_flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
958   if (loopback)
959     stream_flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
960 
961   hr = IAudioClient3_InitializeSharedAudioStream (client, stream_flags,
962       devicep_frames, format, NULL);
963   HR_FAILED_RET (hr, IAudioClient3::InitializeSharedAudioStream, FALSE);
964 
965   hr = IAudioClient3_GetCurrentSharedModeEnginePeriod (client, &tmpf,
966       &devicep_frames);
967   CoTaskMemFree (tmpf);
968   HR_FAILED_RET (hr, IAudioClient3::GetCurrentSharedModeEnginePeriod, FALSE);
969 
970   *ret_devicep_frames = devicep_frames;
971   return TRUE;
972 }
973