1 /*****************************************************************************
2 * wasapi.c : Windows Audio Session API output plugin for VLC
3 *****************************************************************************
4 * Copyright (C) 2012 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program 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.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
29 #include <audioclient.h>
30 #include <audiopolicy.h>
31 #include <mmdeviceapi.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_charset.h>
38 static int Open(vlc_object_t *);
39 static void Close(vlc_object_t *);
42 set_shortname("WASAPI")
43 set_description(N_("Windows Audio Session output") )
44 set_capability("audio output", 150)
45 set_category(CAT_AUDIO)
46 set_subcategory(SUBCAT_AUDIO_AOUT)
47 add_shortcut("was", "audioclient")
48 set_callbacks(Open, Close)
51 static int TryEnter(vlc_object_t *obj)
53 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
54 if (unlikely(FAILED(hr)))
56 msg_Err (obj, "cannot initialize COM (error 0x%lx)", hr);
61 #define TryEnter(o) TryEnter(VLC_OBJECT(o))
63 static void Enter(void)
65 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
66 if (unlikely(FAILED(hr)))
70 static void Leave(void)
78 IAudioRenderClient *render;
82 ISimpleAudioVolume *simple;
84 IAudioSessionControl *control;
85 UINT32 frames; /**< Total buffer size (frames) */
86 HANDLE ready; /**< Semaphore from MTA thread */
87 HANDLE done; /**< Semaphore to MTA thread */
90 static void Play(audio_output_t *aout, block_t *block, mtime_t *restrict drift)
92 aout_sys_t *sys = aout->sys;
96 if (likely(sys->clock != NULL))
100 IAudioClock_GetPosition(sys->clock, &pos, &qpcpos);
101 qpcpos = (qpcpos + 5) / 10; /* 100ns -> 1µs */
102 /* NOTE: this assumes mdate() uses QPC() (which it currently does). */
103 *drift = mdate() - qpcpos;
109 hr = IAudioClient_GetCurrentPadding(sys->client, &frames);
112 msg_Err(aout, "cannot get current padding (error 0x%lx)", hr);
116 assert(frames <= sys->frames);
117 frames = sys->frames - frames;
118 if (frames > block->i_nb_samples)
119 frames = block->i_nb_samples;
122 hr = IAudioRenderClient_GetBuffer(sys->render, frames, &dst);
125 msg_Err(aout, "cannot get buffer (error 0x%lx)", hr);
129 const size_t copy = frames * (size_t)aout->format.i_bytes_per_frame;
131 memcpy(dst, block->p_buffer, copy);
132 hr = IAudioRenderClient_ReleaseBuffer(sys->render, frames, 0);
135 msg_Err(aout, "cannot release buffer (error 0x%lx)", hr);
138 IAudioClient_Start(sys->client);
140 block->p_buffer += copy;
141 block->i_buffer -= copy;
142 block->i_nb_samples -= frames;
143 if (block->i_nb_samples == 0)
146 /* Out of buffer space, sleep */
147 msleep(AOUT_MIN_PREPARE_TIME
148 + block->i_nb_samples * CLOCK_FREQ / aout->format.i_rate);
152 block_Release(block);
155 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
157 aout_sys_t *sys = aout->sys;
162 hr = IAudioClient_Stop(sys->client);
164 hr = IAudioClient_Start(sys->client);
166 msg_Warn(aout, "cannot %s stream (error 0x%lx)",
167 paused ? "stop" : "start", hr);
172 static void Flush(audio_output_t *aout, bool wait)
174 aout_sys_t *sys = aout->sys;
178 return; /* Drain not implemented */
181 IAudioClient_Stop(sys->client);
182 hr = IAudioClient_Reset(sys->client);
184 msg_Warn(aout, "cannot reset stream (error 0x%lx)", hr);
188 static int SimpleVolumeSet(audio_output_t *aout, float vol)
190 aout_sys_t *sys = aout->sys;
197 hr = ISimpleAudioVolume_SetMasterVolume(sys->volume.simple, vol, NULL);
199 msg_Warn(aout, "cannot set session volume (error 0x%lx)", hr);
201 return FAILED(hr) ? -1 : 0;
204 static int SimpleMuteSet(audio_output_t *aout, bool mute)
206 aout_sys_t *sys = aout->sys;
210 hr = ISimpleAudioVolume_SetMute(sys->volume.simple, mute, NULL);
212 msg_Warn(aout, "cannot mute session (error 0x%lx)", hr);
214 return FAILED(hr) ? -1 : 0;
217 static int DeviceChanged(vlc_object_t *obj, const char *varname,
218 vlc_value_t prev, vlc_value_t cur, void *data)
220 aout_ChannelsRestart(obj, varname, prev, cur, data);
222 if (!var_Type (obj, "wasapi-audio-device"))
223 var_Create (obj, "wasapi-audio-device", VLC_VAR_STRING);
224 var_SetString (obj, "wasapi-audio-device", cur.psz_string);
228 static void GetDevices(vlc_object_t *obj, IMMDeviceEnumerator *it)
231 vlc_value_t val, text;
233 text.psz_string = _("Audio Device");
234 var_Change (obj, "audio-device", VLC_VAR_SETTEXT, &text, NULL);
236 IMMDeviceCollection *devs;
237 hr = IMMDeviceEnumerator_EnumAudioEndpoints(it, eRender,
238 DEVICE_STATE_ACTIVE, &devs);
241 msg_Warn (obj, "cannot enumerate audio endpoints (error 0x%lx)", hr);
246 hr = IMMDeviceCollection_GetCount(devs, &n);
253 hr = IMMDeviceCollection_Item(devs, --n, &dev);
257 /* Unique device ID */
259 hr = IMMDevice_GetId(dev, &devid);
262 IMMDevice_Release(dev);
265 val.psz_string = FromWide(devid);
266 CoTaskMemFree(devid);
267 text.psz_string = val.psz_string;
269 /* User-readable device name */
270 IPropertyStore *props;
271 hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, &props);
278 hr = IPropertyStore_GetValue(props, PKEY_Device_FriendlyName, &v);
280 text.psz_string = FromWide(v.pwszVal);
282 PropVariantClear(&v);
283 IPropertyStore_Release(props);
285 IMMDevice_Release(dev);
287 var_Change(obj, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
288 if (likely(text.psz_string != val.psz_string))
289 free(text.psz_string);
290 free(val.psz_string);
292 IMMDeviceCollection_Release(devs);
296 static void vlc_ToWave(WAVEFORMATEXTENSIBLE *restrict wf,
297 audio_sample_format_t *restrict audio)
299 switch (audio->i_format)
302 audio->i_format = VLC_CODEC_FL32;
304 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
309 audio->i_format = VLC_CODEC_S16N;
311 wf->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
315 audio->i_format = VLC_CODEC_FL32;
316 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
319 aout_FormatPrepare (audio);
321 wf->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
322 wf->Format.nChannels = audio->i_channels;
323 wf->Format.nSamplesPerSec = audio->i_rate;
324 wf->Format.nAvgBytesPerSec = audio->i_bytes_per_frame * audio->i_rate;
325 wf->Format.nBlockAlign = audio->i_bytes_per_frame;
326 wf->Format.wBitsPerSample = audio->i_bitspersample;
327 wf->Format.cbSize = sizeof (*wf) - sizeof (wf->Format);
329 wf->Samples.wValidBitsPerSample = audio->i_bitspersample;
331 wf->dwChannelMask = 0;
332 if (audio->i_physical_channels & AOUT_CHAN_LEFT)
333 wf->dwChannelMask |= SPEAKER_FRONT_LEFT;
334 if (audio->i_physical_channels & AOUT_CHAN_RIGHT)
335 wf->dwChannelMask |= SPEAKER_FRONT_RIGHT;
336 if (audio->i_physical_channels & AOUT_CHAN_CENTER)
337 wf->dwChannelMask |= SPEAKER_FRONT_CENTER;
338 if (audio->i_physical_channels & AOUT_CHAN_LFE)
339 wf->dwChannelMask |= SPEAKER_LOW_FREQUENCY;
341 if (audio->i_physical_channels & AOUT_CHAN_REARLEFT)
342 wf->dwChannelMask |= SPEAKER_BACK_LEFT;
343 if (audio->i_physical_channels & AOUT_CHAN_REARRIGHT)
344 wf->dwChannelMask |= SPEAKER_BACK_RIGHT;
346 if (audio->i_physical_channels & AOUT_CHAN_REARCENTER)
347 wf->dwChannelMask |= SPEAKER_BACK_CENTER;
348 if (audio->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
349 wf->dwChannelMask |= SPEAKER_SIDE_LEFT;
350 if (audio->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
351 wf->dwChannelMask |= SPEAKER_SIDE_RIGHT;
355 static int vlc_FromWave(const WAVEFORMATEX *restrict wf,
356 audio_sample_format_t *restrict audio)
358 /* FIXME? different sample format? possible? */
359 audio->i_rate = wf->nSamplesPerSec;
361 if (wf->nChannels != audio->i_channels)
364 aout_FormatPrepare(audio);
368 static wchar_t *var_InheritWide(vlc_object_t *obj, const char *name)
370 char *v8 = var_InheritString(obj, name);
374 wchar_t *v16 = ToWide(v8);
378 #define var_InheritWide(o,n) var_InheritWide(VLC_OBJECT(o),n)
380 static int var_SetWide(vlc_object_t *obj, const char *name, const wchar_t *val)
382 char *str = FromWide(val);
383 if (unlikely(str == NULL))
386 int ret = var_SetString(obj, name, str);
390 #define var_SetWide(o,n,v) var_SetWide(VLC_OBJECT(o),n,v)
392 /* Dummy thread to create and release COM interfaces when needed. */
393 static void MTAThread(void *data)
395 audio_output_t *aout = data;
396 aout_sys_t *sys = aout->sys;
401 hr = IAudioClient_GetService(sys->client, &IID_IAudioRenderClient,
402 (void **)&sys->render);
405 msg_Err(aout, "cannot get audio render service (error 0x%lx)", hr);
409 hr = IAudioClient_GetService(sys->client, &IID_IAudioClock,
410 (void **)&sys->clock);
412 msg_Warn(aout, "cannot get audio clock (error 0x%lx)", hr);
414 /*if (AOUT_FMT_LINEAR(&format) && !exclusive)*/
416 hr = IAudioClient_GetService(sys->client, &IID_ISimpleAudioVolume,
417 (void **)&sys->volume.simple);
420 hr = IAudioClient_GetService(sys->client, &IID_IAudioSessionControl,
421 (void **)&sys->control);
423 msg_Warn(aout, "cannot get audio session control (error 0x%lx)", hr);
426 wchar_t *ua = var_InheritWide(aout, "user-agent");
427 IAudioSessionControl_SetDisplayName(sys->control, ua, NULL);
431 /* do nothing until the audio session terminates */
432 ReleaseSemaphore(sys->ready, 1, NULL);
433 WaitForSingleObject(sys->done, INFINITE);
435 if (sys->control != NULL)
436 IAudioSessionControl_Release(sys->control);
437 /*if (AOUT_FMT_LINEAR(&format) && !exclusive)*/
439 if (sys->volume.simple != NULL)
440 ISimpleAudioVolume_Release(sys->volume.simple);
442 if (sys->clock != NULL)
443 IAudioClock_Release(sys->clock);
444 IAudioRenderClient_Release(sys->render);
447 ReleaseSemaphore(sys->ready, 1, NULL);
450 static int Open(vlc_object_t *obj)
452 audio_output_t *aout = (audio_output_t *)obj;
455 if (AOUT_FMT_SPDIF(&aout->format) && !aout->b_force
456 && var_InheritBool(aout, "spdif"))
457 /* Fallback to other plugin until pass-through is implemented */
460 aout_sys_t *sys = malloc(sizeof (*sys));
461 if (unlikely(sys == NULL))
476 /* Get audio device according to policy */
477 var_Create (aout, "audio-device", VLC_VAR_STRING|VLC_VAR_HASCHOICE);
479 IMMDeviceEnumerator *devs;
480 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
481 &IID_IMMDeviceEnumerator, (void **)&devs);
484 msg_Dbg(aout, "cannot create device enumerator (error 0x%lx)", hr);
488 // Without configuration item, the variable must be created explicitly.
489 var_Create (aout, "wasapi-audio-device", VLC_VAR_STRING);
490 LPWSTR devid = var_InheritWide (aout, "wasapi-audio-device");
491 var_Destroy (aout, "wasapi-audio-device");
493 IMMDevice *dev = NULL;
496 msg_Dbg (aout, "using selected device %ls", devid);
497 hr = IMMDeviceEnumerator_GetDevice (devs, devid, &dev);
499 msg_Warn(aout, "cannot get audio endpoint (error 0x%lx)", hr);
504 msg_Dbg (aout, "using default device");
505 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devs, eRender,
509 GetDevices(VLC_OBJECT(aout), devs);
510 IMMDeviceEnumerator_Release(devs);
513 msg_Err(aout, "cannot get audio endpoint (error 0x%lx)", hr);
517 hr = IMMDevice_GetId(dev, &devid);
520 msg_Dbg(aout, "using device %ls", devid);
521 var_SetWide (aout, "audio-device", devid);
522 CoTaskMemFree(devid);
525 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_ALL, NULL,
526 (void **)&sys->client);
527 IMMDevice_Release(dev);
530 msg_Err(aout, "cannot activate audio client (error 0x%lx)", hr);
534 /* Configure audio stream */
535 audio_sample_format_t format = aout->format;
536 WAVEFORMATEXTENSIBLE wf;
539 vlc_ToWave(&wf, &format);
540 hr = IAudioClient_IsFormatSupported(sys->client, AUDCLNT_SHAREMODE_SHARED,
542 // TODO: deal with (hr == AUDCLNT_E_DEVICE_INVALIDATED) ?
545 msg_Err(aout, "cannot negotiate audio format (error 0x%lx)", hr);
552 if (vlc_FromWave(pwf, &format))
555 msg_Err(aout, "unsupported audio format");
558 msg_Dbg(aout, "modified format");
562 hr = IAudioClient_Initialize(sys->client, AUDCLNT_SHAREMODE_SHARED, 0,
563 AOUT_MAX_PREPARE_TIME * 10, 0,
564 (hr == S_OK) ? &wf.Format : pwf, NULL);
568 msg_Err(aout, "cannot initialize audio client (error 0x%lx)", hr);
572 hr = IAudioClient_GetBufferSize(sys->client, &sys->frames);
575 msg_Err(aout, "cannot get buffer size (error 0x%lx)", hr);
579 sys->ready = CreateSemaphore(NULL, 0, 1, NULL);
580 sys->done = CreateSemaphore(NULL, 0, 1, NULL);
581 if (unlikely(sys->ready == NULL || sys->done == NULL))
583 /* Note: thread handle released by CRT, ignore it. */
584 if (_beginthread(MTAThread, 0, aout) == (uintptr_t)-1)
587 WaitForSingleObject(sys->ready, INFINITE);
588 if (sys->render == NULL)
593 aout->format = format;
594 aout->pf_play = Play;
595 aout->pf_pause = Pause;
596 aout->pf_flush = Flush;
597 /*if (AOUT_FMT_LINEAR(&format) && !exclusive)*/
599 aout->volume_set = SimpleVolumeSet;
600 aout->mute_set = SimpleMuteSet;
602 var_AddCallback (aout, "audio-device", DeviceChanged, NULL);
606 if (sys->done != NULL)
607 CloseHandle(sys->done);
608 if (sys->ready != NULL)
609 CloseHandle(sys->done);
610 if (sys->client != NULL)
611 IAudioClient_Release(sys->client);
613 var_Destroy (aout, "audio-device");
618 static void Close (vlc_object_t *obj)
620 audio_output_t *aout = (audio_output_t *)obj;
621 aout_sys_t *sys = aout->sys;
624 ReleaseSemaphore(sys->done, 1, NULL); /* tell MTA thread to finish */
625 WaitForSingleObject(sys->ready, INFINITE); /* wait for that ^ */
626 IAudioClient_Stop(sys->client); /* should not be needed */
627 IAudioClient_Release(sys->client);
630 var_DelCallback (aout, "audio-device", DeviceChanged, NULL);
631 var_Destroy (aout, "audio-device");
633 CloseHandle(sys->done);
634 CloseHandle(sys->ready);