contrib: soxr: enable by default
[vlc.git] / modules / access / wasapi.c
blobbc320d9943576d14fc7dea82a3dad97152fe9606
1 /**
2 * \file wasapi.c
3 * \brief Windows Audio Session API capture plugin for VLC
4 */
5 /*****************************************************************************
6 * Copyright (C) 2014-2015 RĂ©mi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #define INITGUID
28 #define COBJMACROS
29 #define CONST_VTABLE
31 #include <assert.h>
32 #include <stdlib.h>
34 #include <vlc_common.h>
35 #include <vlc_aout.h>
36 #include <vlc_demux.h>
37 #include <vlc_plugin.h>
38 #include <mmdeviceapi.h>
39 #include <audioclient.h>
41 static LARGE_INTEGER freq; /* performance counters frequency */
43 BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); /* avoid warning */
45 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
47 (void) dll;
48 (void) reserved;
50 switch (reason)
52 case DLL_PROCESS_ATTACH:
53 if (!QueryPerformanceFrequency(&freq))
54 return FALSE;
55 break;
57 return TRUE;
60 static UINT64 GetQPC(void)
62 LARGE_INTEGER counter;
64 if (!QueryPerformanceCounter(&counter))
65 abort();
67 lldiv_t d = lldiv(counter.QuadPart, freq.QuadPart);
68 return (d.quot * 10000000) + ((d.rem * 10000000) / freq.QuadPart);
71 static_assert(CLOCK_FREQ * 10 == 10000000,
72 "REFERENCE_TIME conversion broken");
74 static EDataFlow GetDeviceFlow(IMMDevice *dev)
76 void *pv;
78 if (FAILED(IMMDevice_QueryInterface(dev, &IID_IMMEndpoint, &pv)))
79 return false;
81 IMMEndpoint *ep = pv;
82 EDataFlow flow;
84 if (SUCCEEDED(IMMEndpoint_GetDataFlow(ep, &flow)))
85 flow = eAll;
86 IMMEndpoint_Release(ep);
87 return flow;
90 static IAudioClient *GetClient(demux_t *demux, bool *restrict loopbackp)
92 IMMDeviceEnumerator *e;
93 IMMDevice *dev;
94 void *pv;
95 HRESULT hr;
97 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
98 &IID_IMMDeviceEnumerator, &pv);
99 if (FAILED(hr))
101 msg_Err(demux, "cannot create device enumerator (error 0x%lx)", hr);
102 return NULL;
104 e = pv;
106 bool loopback = var_InheritBool(demux, "wasapi-loopback");
107 EDataFlow flow = loopback ? eRender : eCapture;
108 ERole role = loopback ? eConsole : eCommunications;
110 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(e, flow, role, &dev);
111 IMMDeviceEnumerator_Release(e);
112 if (FAILED(hr))
114 msg_Err(demux, "cannot get default device (error 0x%lx)", hr);
115 return NULL;
118 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_ALL, NULL, &pv);
119 *loopbackp = GetDeviceFlow(dev) == eRender;
120 IMMDevice_Release(dev);
121 if (FAILED(hr))
122 msg_Err(demux, "cannot activate device (error 0x%lx)", hr);
123 return pv;
126 static int vlc_FromWave(const WAVEFORMATEX *restrict wf,
127 audio_sample_format_t *restrict fmt)
129 fmt->i_rate = wf->nSamplesPerSec;
131 /* As per MSDN, IAudioClient::GetMixFormat() always uses this format. */
132 assert(wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE);
134 const WAVEFORMATEXTENSIBLE *wfe = (void *)wf;
136 fmt->i_physical_channels = 0;
137 if (wfe->dwChannelMask & SPEAKER_FRONT_LEFT)
138 fmt->i_physical_channels |= AOUT_CHAN_LEFT;
139 if (wfe->dwChannelMask & SPEAKER_FRONT_RIGHT)
140 fmt->i_physical_channels |= AOUT_CHAN_RIGHT;
141 if (wfe->dwChannelMask & SPEAKER_FRONT_CENTER)
142 fmt->i_physical_channels |= AOUT_CHAN_CENTER;
143 if (wfe->dwChannelMask & SPEAKER_LOW_FREQUENCY)
144 fmt->i_physical_channels |= AOUT_CHAN_LFE;
146 assert(vlc_popcount(wfe->dwChannelMask) == wf->nChannels);
148 if (IsEqualIID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))
150 switch (wf->wBitsPerSample)
152 case 32:
153 switch (wfe->Samples.wValidBitsPerSample)
155 case 32:
156 fmt->i_format = VLC_CODEC_S32N;
157 break;
158 case 24:
159 #ifdef WORDS_BIGENDIAN
160 fmt->i_format = VLC_CODEC_S24B32;
161 #else
162 fmt->i_format = VLC_CODEC_S24L32;
163 #endif
164 break;
165 default:
166 return -1;
168 break;
169 case 24:
170 if (wfe->Samples.wValidBitsPerSample == 24)
171 fmt->i_format = VLC_CODEC_S24N;
172 else
173 return -1;
174 break;
175 case 16:
176 if (wfe->Samples.wValidBitsPerSample == 16)
177 fmt->i_format = VLC_CODEC_S16N;
178 else
179 return -1;
180 break;
181 case 8:
182 if (wfe->Samples.wValidBitsPerSample == 8)
183 fmt->i_format = VLC_CODEC_S8;
184 else
185 return -1;
186 break;
187 default:
188 return -1;
191 else if (IsEqualIID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
193 if (wf->wBitsPerSample != wfe->Samples.wValidBitsPerSample)
194 return -1;
196 switch (wf->wBitsPerSample)
198 case 64:
199 fmt->i_format = VLC_CODEC_FL64;
200 break;
201 case 32:
202 fmt->i_format = VLC_CODEC_FL32;
203 break;
204 default:
205 return -1;
208 /*else if (IsEqualIID(&wfe->Subformat, &KSDATAFORMAT_SUBTYPE_DRM)) {} */
209 else if (IsEqualIID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_ALAW))
210 fmt->i_format = VLC_CODEC_ALAW;
211 else if (IsEqualIID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_MULAW))
212 fmt->i_format = VLC_CODEC_MULAW;
213 else if (IsEqualIID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_ADPCM))
214 fmt->i_format = VLC_CODEC_ADPCM_MS;
215 else
216 return -1;
218 aout_FormatPrepare(fmt);
219 if (wf->nChannels != fmt->i_channels)
220 return -1;
222 return 0;
225 static es_out_id_t *CreateES(demux_t *demux, IAudioClient *client, bool loop,
226 mtime_t caching, size_t *restrict frame_size)
228 es_format_t fmt;
229 WAVEFORMATEX *pwf;
230 HRESULT hr;
232 hr = IAudioClient_GetMixFormat(client, &pwf);
233 if (FAILED(hr))
235 msg_Err(demux, "cannot get mix format (error 0x%lx)", hr);
236 return NULL;
239 es_format_Init(&fmt, AUDIO_ES, 0);
240 if (vlc_FromWave(pwf, &fmt.audio))
242 msg_Err(demux, "unsupported mix format");
243 CoTaskMemFree(pwf);
244 return NULL;
247 fmt.i_codec = fmt.audio.i_format;
248 fmt.i_bitrate = fmt.audio.i_bitspersample * fmt.audio.i_channels
249 * fmt.audio.i_rate;
250 *frame_size = fmt.audio.i_bitspersample * fmt.audio.i_channels / 8;
252 DWORD flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
253 if (loop)
254 flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
256 /* Request at least thrice the PTS delay */
257 REFERENCE_TIME bufsize = caching * INT64_C(10) * 3;
259 hr = IAudioClient_Initialize(client, AUDCLNT_SHAREMODE_SHARED, flags,
260 bufsize, 0, pwf, NULL);
261 CoTaskMemFree(pwf);
262 if (FAILED(hr))
264 msg_Err(demux, "cannot initialize audio client (error 0x%lx)", hr);
265 return NULL;
267 return es_out_Add(demux->out, &fmt);
270 struct demux_sys_t
272 IAudioClient *client;
273 es_out_id_t *es;
275 size_t frame_size;
276 mtime_t caching;
277 mtime_t start_time;
279 HANDLE events[2];
280 union {
281 HANDLE thread;
282 HANDLE ready;
286 static unsigned __stdcall Thread(void *data)
288 demux_t *demux = data;
289 demux_sys_t *sys = demux->p_sys;
290 IAudioCaptureClient *capture = NULL;
291 void *pv;
292 HRESULT hr;
294 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
295 assert(SUCCEEDED(hr)); /* COM already allocated by parent thread */
296 SetEvent(sys->ready);
298 hr = IAudioClient_GetService(sys->client, &IID_IAudioCaptureClient, &pv);
299 if (FAILED(hr))
301 msg_Err(demux, "cannot get capture client (error 0x%lx)", hr);
302 goto out;
304 capture = pv;
306 hr = IAudioClient_Start(sys->client);
307 if (FAILED(hr))
309 msg_Err(demux, "cannot start client (error 0x%lx)", hr);
310 IAudioCaptureClient_Release(capture);
311 goto out;
314 while (WaitForMultipleObjects(2, sys->events, FALSE, INFINITE)
315 != WAIT_OBJECT_0)
317 BYTE *data;
318 UINT32 frames;
319 DWORD flags;
320 UINT64 qpc;
321 mtime_t pts;
323 hr = IAudioCaptureClient_GetBuffer(capture, &data, &frames, &flags,
324 NULL, &qpc);
325 if (hr != S_OK)
326 continue;
328 pts = mdate() - ((GetQPC() - qpc) / 10);
330 es_out_SetPCR(demux->out, pts);
332 size_t bytes = frames * sys->frame_size;
333 block_t *block = block_Alloc(bytes);
335 if (likely(block != NULL)) {
336 memcpy(block->p_buffer, data, bytes);
337 block->i_nb_samples = frames;
338 block->i_pts = block->i_dts = pts;
339 es_out_Send(demux->out, sys->es, block);
342 IAudioCaptureClient_ReleaseBuffer(capture, frames);
345 IAudioClient_Stop(sys->client);
346 IAudioCaptureClient_Release(capture);
347 out:
348 CoUninitialize();
349 return 0;
352 static int Control(demux_t *demux, int query, va_list ap)
354 demux_sys_t *sys = demux->p_sys;
356 switch (query)
358 case DEMUX_GET_TIME:
359 *(va_arg(ap, int64_t *)) = mdate() - sys->start_time;
360 break;
362 case DEMUX_GET_PTS_DELAY:
363 *(va_arg(ap, int64_t *)) = sys->caching;
364 break;
366 case DEMUX_HAS_UNSUPPORTED_META:
367 case DEMUX_CAN_RECORD:
368 case DEMUX_CAN_PAUSE:
369 case DEMUX_CAN_CONTROL_PACE:
370 case DEMUX_CAN_CONTROL_RATE:
371 case DEMUX_CAN_SEEK:
372 *(va_arg(ap, bool *)) = false;
373 break;
375 default:
376 return VLC_EGENERIC;
379 return VLC_SUCCESS;
382 static int Open(vlc_object_t *obj)
384 demux_t *demux = (demux_t *)obj;
385 HRESULT hr;
387 if (demux->psz_location != NULL && *demux->psz_location != '\0')
388 return VLC_EGENERIC; /* TODO non-default device */
390 demux_sys_t *sys = vlc_obj_malloc(obj, sizeof (*sys));
391 if (unlikely(sys == NULL))
392 return VLC_ENOMEM;
394 sys->client = NULL;
395 sys->es = NULL;
396 sys->caching = INT64_C(1000) * var_InheritInteger(obj, "live-caching");
397 sys->start_time = mdate();
398 for (unsigned i = 0; i < 2; i++)
399 sys->events[i] = NULL;
401 for (unsigned i = 0; i < 2; i++) {
402 sys->events[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
403 if (sys->events[i] == NULL)
404 goto error;
407 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
408 if (unlikely(FAILED(hr))) {
409 msg_Err(demux, "cannot initialize COM (error 0x%lx)", hr);
410 goto error;
413 bool loopback;
414 sys->client = GetClient(demux, &loopback);
415 if (sys->client == NULL) {
416 CoUninitialize();
417 goto error;
420 sys->es = CreateES(demux, sys->client, loopback, sys->caching,
421 &sys->frame_size);
422 if (sys->es == NULL)
423 goto error;
425 hr = IAudioClient_SetEventHandle(sys->client, sys->events[1]);
426 if (FAILED(hr)) {
427 msg_Err(demux, "cannot set event handle (error 0x%lx)", hr);
428 goto error;
431 demux->p_sys = sys;
433 sys->ready = CreateEvent(NULL, FALSE, FALSE, NULL);
434 if (sys->ready == NULL)
435 goto error;
437 uintptr_t h = _beginthreadex(NULL, 0, Thread, demux, 0, NULL);
438 if (h != 0)
439 WaitForSingleObject(sys->ready, INFINITE);
440 CloseHandle(sys->ready);
442 sys->thread = (HANDLE)h;
443 if (sys->thread == NULL)
444 goto error;
445 CoUninitialize();
447 demux->pf_demux = NULL;
448 demux->pf_control = Control;
449 return VLC_SUCCESS;
451 error:
452 if (sys->es != NULL)
453 es_out_Del(demux->out, sys->es);
454 if (sys->client != NULL)
456 IAudioClient_Release(sys->client);
457 CoUninitialize();
459 for (unsigned i = 0; i < 2; i++)
460 if (sys->events[i] != NULL)
461 CloseHandle(sys->events[i]);
462 return VLC_ENOMEM;
465 static void Close (vlc_object_t *obj)
467 demux_t *demux = (demux_t *)obj;
468 demux_sys_t *sys = demux->p_sys;
469 HRESULT hr;
471 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
472 assert(SUCCEEDED(hr));
474 SetEvent(sys->events[0]);
475 WaitForSingleObject(sys->thread, INFINITE);
476 CloseHandle(sys->thread);
478 es_out_Del(demux->out, sys->es);
479 IAudioClient_Release(sys->client);
480 CoUninitialize();
481 for (unsigned i = 0; i < 2; i++)
482 CloseHandle(sys->events[i]);
485 #define LOOPBACK_TEXT N_("Loopback mode")
486 #define LOOPBACK_LONGTEXT N_("Record an audio rendering endpoint.")
488 vlc_module_begin()
489 set_shortname(N_("WASAPI"))
490 set_description(N_("Windows Audio Session API input"))
491 set_capability("access_demux", 0)
492 set_category(CAT_INPUT)
493 set_subcategory(SUBCAT_INPUT_ACCESS)
495 add_bool("wasapi-loopback", false, LOOPBACK_TEXT, LOOPBACK_LONGTEXT, true)
497 add_shortcut("wasapi")
498 set_callbacks(Open, Close)
499 vlc_module_end()