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 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "gstwasapi2util.h"
27 #include <audioclient.h>
28 #include <mmdeviceapi.h>
29 #include <winternl.h>
30
31 GST_DEBUG_CATEGORY_EXTERN (gst_wasapi2_debug);
32 #define GST_CAT_DEFAULT gst_wasapi2_debug
33
34 /* Desktop only defines */
35 #ifndef KSAUDIO_SPEAKER_MONO
36 #define KSAUDIO_SPEAKER_MONO (SPEAKER_FRONT_CENTER)
37 #endif
38 #ifndef KSAUDIO_SPEAKER_1POINT1
39 #define KSAUDIO_SPEAKER_1POINT1 (SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY)
40 #endif
41 #ifndef KSAUDIO_SPEAKER_STEREO
42 #define KSAUDIO_SPEAKER_STEREO (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT)
43 #endif
44 #ifndef KSAUDIO_SPEAKER_2POINT1
45 #define KSAUDIO_SPEAKER_2POINT1 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY)
46 #endif
47 #ifndef KSAUDIO_SPEAKER_3POINT0
48 #define KSAUDIO_SPEAKER_3POINT0 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER)
49 #endif
50 #ifndef KSAUDIO_SPEAKER_3POINT1
51 #define KSAUDIO_SPEAKER_3POINT1 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | \
52 SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY)
53 #endif
54 #ifndef KSAUDIO_SPEAKER_QUAD
55 #define KSAUDIO_SPEAKER_QUAD (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | \
56 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT)
57 #endif
58 #define KSAUDIO_SPEAKER_SURROUND (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | \
59 SPEAKER_FRONT_CENTER | SPEAKER_BACK_CENTER)
60 #ifndef KSAUDIO_SPEAKER_5POINT0
61 #define KSAUDIO_SPEAKER_5POINT0 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | \
62 SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT)
63 #endif
64 #define KSAUDIO_SPEAKER_5POINT1 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | \
65 SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | \
66 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT)
67 #ifndef KSAUDIO_SPEAKER_7POINT0
68 #define KSAUDIO_SPEAKER_7POINT0 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | \
69 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | \
70 SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT)
71 #endif
72 #ifndef KSAUDIO_SPEAKER_7POINT1
73 #define KSAUDIO_SPEAKER_7POINT1 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | \
74 SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | \
75 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | \
76 SPEAKER_FRONT_LEFT_OF_CENTER | SPEAKER_FRONT_RIGHT_OF_CENTER)
77 #endif
78
79 /* *INDENT-OFF* */
80 static struct
81 {
82 guint64 wasapi_pos;
83 GstAudioChannelPosition gst_pos;
84 } wasapi_to_gst_pos[] = {
85 {SPEAKER_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT},
86 {SPEAKER_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
87 {SPEAKER_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER},
88 {SPEAKER_LOW_FREQUENCY, GST_AUDIO_CHANNEL_POSITION_LFE1},
89 {SPEAKER_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT},
90 {SPEAKER_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
91 {SPEAKER_FRONT_LEFT_OF_CENTER,
92 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER},
93 {SPEAKER_FRONT_RIGHT_OF_CENTER,
94 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
95 {SPEAKER_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER},
96 /* Enum values diverge from this point onwards */
97 {SPEAKER_SIDE_LEFT, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT},
98 {SPEAKER_SIDE_RIGHT, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT},
99 {SPEAKER_TOP_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_CENTER},
100 {SPEAKER_TOP_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT},
101 {SPEAKER_TOP_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER},
102 {SPEAKER_TOP_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT},
103 {SPEAKER_TOP_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT},
104 {SPEAKER_TOP_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER},
105 {SPEAKER_TOP_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT}
106 };
107
108 static DWORD default_ch_masks[] = {
109 0,
110 KSAUDIO_SPEAKER_MONO,
111 /* 2ch */
112 KSAUDIO_SPEAKER_STEREO,
113 /* 2.1ch */
114 /* KSAUDIO_SPEAKER_3POINT0 ? */
115 KSAUDIO_SPEAKER_2POINT1,
116 /* 4ch */
117 /* KSAUDIO_SPEAKER_3POINT1 or KSAUDIO_SPEAKER_SURROUND ? */
118 KSAUDIO_SPEAKER_QUAD,
119 /* 5ch */
120 KSAUDIO_SPEAKER_5POINT0,
121 /* 5.1ch */
122 KSAUDIO_SPEAKER_5POINT1,
123 /* 7ch */
124 KSAUDIO_SPEAKER_7POINT0,
125 /* 7.1ch */
126 KSAUDIO_SPEAKER_7POINT1,
127 };
128 /* *INDENT-ON* */
129
130 static const gchar *
hresult_to_string_fallback(HRESULT hr)131 hresult_to_string_fallback (HRESULT hr)
132 {
133 const gchar *s = "unknown error";
134
135 switch (hr) {
136 case AUDCLNT_E_NOT_INITIALIZED:
137 s = "AUDCLNT_E_NOT_INITIALIZED";
138 break;
139 case AUDCLNT_E_ALREADY_INITIALIZED:
140 s = "AUDCLNT_E_ALREADY_INITIALIZED";
141 break;
142 case AUDCLNT_E_WRONG_ENDPOINT_TYPE:
143 s = "AUDCLNT_E_WRONG_ENDPOINT_TYPE";
144 break;
145 case AUDCLNT_E_DEVICE_INVALIDATED:
146 s = "AUDCLNT_E_DEVICE_INVALIDATED";
147 break;
148 case AUDCLNT_E_NOT_STOPPED:
149 s = "AUDCLNT_E_NOT_STOPPED";
150 break;
151 case AUDCLNT_E_BUFFER_TOO_LARGE:
152 s = "AUDCLNT_E_BUFFER_TOO_LARGE";
153 break;
154 case AUDCLNT_E_OUT_OF_ORDER:
155 s = "AUDCLNT_E_OUT_OF_ORDER";
156 break;
157 case AUDCLNT_E_UNSUPPORTED_FORMAT:
158 s = "AUDCLNT_E_UNSUPPORTED_FORMAT";
159 break;
160 case AUDCLNT_E_INVALID_DEVICE_PERIOD:
161 s = "AUDCLNT_E_INVALID_DEVICE_PERIOD";
162 break;
163 case AUDCLNT_E_INVALID_SIZE:
164 s = "AUDCLNT_E_INVALID_SIZE";
165 break;
166 case AUDCLNT_E_DEVICE_IN_USE:
167 s = "AUDCLNT_E_DEVICE_IN_USE";
168 break;
169 case AUDCLNT_E_BUFFER_OPERATION_PENDING:
170 s = "AUDCLNT_E_BUFFER_OPERATION_PENDING";
171 break;
172 case AUDCLNT_E_BUFFER_SIZE_ERROR:
173 s = "AUDCLNT_E_BUFFER_SIZE_ERROR";
174 break;
175 case AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED:
176 s = "AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED";
177 break;
178 case AUDCLNT_E_THREAD_NOT_REGISTERED:
179 s = "AUDCLNT_E_THREAD_NOT_REGISTERED";
180 break;
181 case AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED:
182 s = "AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED";
183 break;
184 case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
185 s = "AUDCLNT_E_ENDPOINT_CREATE_FAILED";
186 break;
187 case AUDCLNT_E_SERVICE_NOT_RUNNING:
188 s = "AUDCLNT_E_SERVICE_NOT_RUNNING";
189 break;
190 case AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED:
191 s = "AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED";
192 break;
193 case AUDCLNT_E_EXCLUSIVE_MODE_ONLY:
194 s = "AUDCLNT_E_EXCLUSIVE_MODE_ONLY";
195 break;
196 case AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL:
197 s = "AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL";
198 break;
199 case AUDCLNT_E_EVENTHANDLE_NOT_SET:
200 s = "AUDCLNT_E_EVENTHANDLE_NOT_SET";
201 break;
202 case AUDCLNT_E_INCORRECT_BUFFER_SIZE:
203 s = "AUDCLNT_E_INCORRECT_BUFFER_SIZE";
204 break;
205 case AUDCLNT_E_CPUUSAGE_EXCEEDED:
206 s = "AUDCLNT_E_CPUUSAGE_EXCEEDED";
207 break;
208 case AUDCLNT_S_BUFFER_EMPTY:
209 s = "AUDCLNT_S_BUFFER_EMPTY";
210 break;
211 case AUDCLNT_S_THREAD_ALREADY_REGISTERED:
212 s = "AUDCLNT_S_THREAD_ALREADY_REGISTERED";
213 break;
214 case AUDCLNT_S_POSITION_STALLED:
215 s = "AUDCLNT_S_POSITION_STALLED";
216 break;
217 case E_POINTER:
218 s = "E_POINTER";
219 break;
220 case E_INVALIDARG:
221 s = "E_INVALIDARG";
222 break;
223 }
224
225 return s;
226 }
227
228 gchar *
gst_wasapi2_util_get_error_message(HRESULT hr)229 gst_wasapi2_util_get_error_message (HRESULT hr)
230 {
231 gchar *error_text = NULL;
232
233 error_text = g_win32_error_message ((gint) hr);
234 if (!error_text || strlen (error_text) == 0) {
235 g_free (error_text);
236 error_text = g_strdup (hresult_to_string_fallback (hr));
237 }
238
239 return error_text;
240 }
241
242 gboolean
_gst_wasapi2_result(HRESULT hr,GstDebugCategory * cat,const gchar * file,const gchar * function,gint line)243 _gst_wasapi2_result (HRESULT hr, GstDebugCategory * cat, const gchar * file,
244 const gchar * function, gint line)
245 {
246 #ifndef GST_DISABLE_GST_DEBUG
247 gboolean ret = TRUE;
248
249 if (FAILED (hr)) {
250 gchar *error_text = NULL;
251 gboolean free_string = TRUE;
252
253 error_text = g_win32_error_message ((gint) hr);
254 /* g_win32_error_message() seems to be returning empty string for
255 * AUDCLNT_* cases */
256 if (!error_text || strlen (error_text) == 0) {
257 g_free (error_text);
258 error_text = (gchar *) hresult_to_string_fallback (hr);
259
260 free_string = FALSE;
261 }
262
263 gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
264 NULL, "WASAPI call failed: 0x%x, %s", (guint) hr, error_text);
265
266 if (free_string)
267 g_free (error_text);
268
269 ret = FALSE;
270 }
271
272 return ret;
273 #else
274 return SUCCEEDED (hr);
275 #endif
276 }
277
278 static void
gst_wasapi_util_channel_position_all_none(guint channels,GstAudioChannelPosition * position)279 gst_wasapi_util_channel_position_all_none (guint channels,
280 GstAudioChannelPosition * position)
281 {
282 guint i;
283
284 for (i = 0; i < channels; i++)
285 position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
286 }
287
288 guint64
gst_wasapi2_util_waveformatex_to_channel_mask(WAVEFORMATEX * format,GstAudioChannelPosition ** out_position)289 gst_wasapi2_util_waveformatex_to_channel_mask (WAVEFORMATEX * format,
290 GstAudioChannelPosition ** out_position)
291 {
292 guint i, ch;
293 guint64 mask = 0;
294 GstAudioChannelPosition *pos = NULL;
295 WORD nChannels = 0;
296 DWORD dwChannelMask = 0;
297
298 nChannels = format->nChannels;
299 if (format->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
300 WAVEFORMATEXTENSIBLE *extensible = (WAVEFORMATEXTENSIBLE *) format;
301 dwChannelMask = extensible->dwChannelMask;
302 }
303
304 if (out_position)
305 *out_position = NULL;
306
307 if (nChannels > 2 && !dwChannelMask) {
308 GST_WARNING ("Unknown channel mask value for %d channel stream", nChannels);
309
310 if (nChannels >= G_N_ELEMENTS (default_ch_masks)) {
311 GST_ERROR ("To may channels %d", nChannels);
312 return 0;
313 }
314
315 dwChannelMask = default_ch_masks[nChannels];
316 }
317
318 pos = g_new (GstAudioChannelPosition, nChannels);
319 gst_wasapi_util_channel_position_all_none (nChannels, pos);
320
321 /* Too many channels, have to assume that they are all non-positional */
322 if (nChannels > G_N_ELEMENTS (wasapi_to_gst_pos)) {
323 GST_INFO ("Got too many (%i) channels, assuming non-positional", nChannels);
324 goto out;
325 }
326
327 /* Too many bits in the channel mask, and the bits don't match nChannels */
328 if (dwChannelMask >> (G_N_ELEMENTS (wasapi_to_gst_pos) + 1) != 0) {
329 GST_WARNING ("Too many bits in channel mask (%lu), assuming "
330 "non-positional", dwChannelMask);
331 goto out;
332 }
333
334 /* Map WASAPI's channel mask to Gstreamer's channel mask and positions.
335 * If the no. of bits in the mask > nChannels, we will ignore the extra. */
336 for (i = 0, ch = 0; i < G_N_ELEMENTS (wasapi_to_gst_pos) && ch < nChannels;
337 i++) {
338 if (!(dwChannelMask & wasapi_to_gst_pos[i].wasapi_pos))
339 /* no match, try next */
340 continue;
341 mask |= G_GUINT64_CONSTANT (1) << wasapi_to_gst_pos[i].gst_pos;
342 pos[ch++] = wasapi_to_gst_pos[i].gst_pos;
343 }
344
345 /* XXX: Warn if some channel masks couldn't be mapped? */
346
347 GST_DEBUG ("Converted WASAPI mask 0x%" G_GINT64_MODIFIER "x -> 0x%"
348 G_GINT64_MODIFIER "x", (guint64) dwChannelMask, (guint64) mask);
349
350 out:
351 if (out_position) {
352 *out_position = pos;
353 } else {
354 g_free (pos);
355 }
356
357 return mask;
358 }
359
360 const gchar *
gst_wasapi2_util_waveformatex_to_audio_format(WAVEFORMATEX * format)361 gst_wasapi2_util_waveformatex_to_audio_format (WAVEFORMATEX * format)
362 {
363 const gchar *fmt_str = NULL;
364 GstAudioFormat fmt = GST_AUDIO_FORMAT_UNKNOWN;
365
366 switch (format->wFormatTag) {
367 case WAVE_FORMAT_PCM:
368 fmt = gst_audio_format_build_integer (TRUE, G_LITTLE_ENDIAN,
369 format->wBitsPerSample, format->wBitsPerSample);
370 break;
371 case WAVE_FORMAT_IEEE_FLOAT:
372 if (format->wBitsPerSample == 32)
373 fmt = GST_AUDIO_FORMAT_F32LE;
374 else if (format->wBitsPerSample == 64)
375 fmt = GST_AUDIO_FORMAT_F64LE;
376 break;
377 case WAVE_FORMAT_EXTENSIBLE:
378 {
379 WAVEFORMATEXTENSIBLE *ex = (WAVEFORMATEXTENSIBLE *) format;
380 if (IsEqualGUID (&ex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
381 fmt = gst_audio_format_build_integer (TRUE, G_LITTLE_ENDIAN,
382 format->wBitsPerSample, ex->Samples.wValidBitsPerSample);
383 } else if (IsEqualGUID (&ex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
384 if (format->wBitsPerSample == 32
385 && ex->Samples.wValidBitsPerSample == 32)
386 fmt = GST_AUDIO_FORMAT_F32LE;
387 else if (format->wBitsPerSample == 64 &&
388 ex->Samples.wValidBitsPerSample == 64)
389 fmt = GST_AUDIO_FORMAT_F64LE;
390 }
391 break;
392 }
393 default:
394 break;
395 }
396
397 if (fmt != GST_AUDIO_FORMAT_UNKNOWN)
398 fmt_str = gst_audio_format_to_string (fmt);
399
400 return fmt_str;
401 }
402
403 gboolean
gst_wasapi2_util_parse_waveformatex(WAVEFORMATEX * format,GstCaps * template_caps,GstCaps ** out_caps,GstAudioChannelPosition ** out_positions)404 gst_wasapi2_util_parse_waveformatex (WAVEFORMATEX * format,
405 GstCaps * template_caps, GstCaps ** out_caps,
406 GstAudioChannelPosition ** out_positions)
407 {
408 const gchar *afmt;
409 guint64 channel_mask;
410
411 *out_caps = NULL;
412
413 /* TODO: handle SPDIF and other encoded formats */
414
415 /* 1 or 2 channels <= 16 bits sample size OR
416 * 1 or 2 channels > 16 bits sample size or >2 channels */
417 if (format->wFormatTag != WAVE_FORMAT_PCM &&
418 format->wFormatTag != WAVE_FORMAT_IEEE_FLOAT &&
419 format->wFormatTag != WAVE_FORMAT_EXTENSIBLE)
420 /* Unhandled format tag */
421 return FALSE;
422
423 /* WASAPI can only tell us one canonical mix format that it will accept. The
424 * alternative is calling IsFormatSupported on all combinations of formats.
425 * Instead, it's simpler and faster to require conversion inside gstreamer */
426 afmt = gst_wasapi2_util_waveformatex_to_audio_format (format);
427 if (afmt == NULL)
428 return FALSE;
429
430 *out_caps = gst_caps_copy (template_caps);
431
432 channel_mask = gst_wasapi2_util_waveformatex_to_channel_mask (format,
433 out_positions);
434
435 gst_caps_set_simple (*out_caps,
436 "format", G_TYPE_STRING, afmt,
437 "channels", G_TYPE_INT, format->nChannels,
438 "rate", G_TYPE_INT, format->nSamplesPerSec, NULL);
439
440 if (channel_mask) {
441 gst_caps_set_simple (*out_caps,
442 "channel-mask", GST_TYPE_BITMASK, channel_mask, NULL);
443 }
444
445 return TRUE;
446 }
447
448 gboolean
gst_wasapi2_can_automatic_stream_routing(void)449 gst_wasapi2_can_automatic_stream_routing (void)
450 {
451 #ifdef GST_WASAPI2_WINAPI_ONLY_APP
452 /* Assume we are on very recent OS */
453 return TRUE;
454 #else
455 static gboolean ret = FALSE;
456 static gsize version_once = 0;
457
458 if (g_once_init_enter (&version_once)) {
459 OSVERSIONINFOEXW osverinfo;
460 typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
461 fRtlGetVersion *RtlGetVersion = NULL;
462 HMODULE hmodule = NULL;
463
464 memset (&osverinfo, 0, sizeof (OSVERSIONINFOEXW));
465 osverinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
466
467 hmodule = LoadLibraryW (L"ntdll.dll");
468 if (hmodule)
469 RtlGetVersion =
470 (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
471
472 if (RtlGetVersion) {
473 RtlGetVersion (&osverinfo);
474
475 /* automatic stream routing requires Windows 10
476 * Anniversary Update (version 1607, build number 14393.0) */
477 if (osverinfo.dwMajorVersion > 10 ||
478 (osverinfo.dwMajorVersion == 10 && osverinfo.dwBuildNumber >= 14393))
479 ret = TRUE;
480 }
481
482 if (hmodule)
483 FreeLibrary (hmodule);
484
485 g_once_init_leave (&version_once, 1);
486 }
487
488 GST_INFO ("Automatic stream routing support: %d", ret);
489
490 return ret;
491 #endif
492 }
493