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 <mmdeviceapi.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
36 static int Open(vlc_object_t *);
37 static void Close(vlc_object_t *);
40 set_shortname("WASAPI")
41 set_description(N_("Windows Audio Session output") )
42 set_capability("audio output", 150)
43 set_category(CAT_AUDIO)
44 set_subcategory(SUBCAT_AUDIO_AOUT)
45 add_shortcut("was", "audioclient")
46 set_callbacks(Open, Close)
52 IAudioRenderClient *render;
56 static void Play(audio_output_t *aout, block_t *block)
58 aout_sys_t *sys = aout->sys;
61 while (block->i_nb_samples > 0)
64 hr = IAudioClient_GetCurrentPadding(sys->client, &frames);
67 msg_Err(aout, "cannot get current padding (error 0x%lx)", hr);
71 assert(frames <= sys->frames);
72 frames = sys->frames - frames;
73 if (frames > block->i_nb_samples)
74 frames = block->i_nb_samples;
77 hr = IAudioRenderClient_GetBuffer(sys->render, frames, &dst);
80 msg_Err(aout, "cannot get buffer (error 0x%lx)", hr);
84 const size_t copy = frames * (size_t)aout->format.i_bytes_per_frame;
86 memcpy(dst, block->p_buffer, copy);
87 hr = IAudioRenderClient_ReleaseBuffer(sys->render, frames, 0);
90 msg_Err(aout, "cannot release buffer (error 0x%lx)", hr);
94 block->p_buffer += copy;
95 block->i_buffer -= copy;
96 block->i_nb_samples -= frames;
98 /* FIXME: implement synchro */
99 IAudioClient_Start(sys->client);
100 Sleep(AOUT_MIN_PREPARE_TIME / 1000);
103 block_Release(block);
106 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
108 aout_sys_t *sys = aout->sys;
114 hr = IAudioClient_Stop(sys->client);
116 msg_Warn(aout, "cannot stop stream (error 0x%lx)", hr);
120 static void Flush(audio_output_t *aout, bool wait)
122 aout_sys_t *sys = aout->sys;
128 hr = IAudioClient_Reset(sys->client);
130 msg_Warn(aout, "cannot reset stream (error 0x%lx)", hr);
133 /*static int VolumeSet(audio_output_t *aout, float vol, bool mute)
135 aout_sys_t *sys = aout->sys;
140 static void vlc_ToWave(WAVEFORMATEXTENSIBLE *restrict wf,
141 audio_sample_format_t *restrict audio)
143 switch (audio->i_format)
148 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
155 wf->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
159 audio->i_format = VLC_CODEC_FL32;
160 audio->i_rate = 48000;
161 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
164 aout_FormatPrepare (audio);
166 wf->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
167 wf->Format.nChannels = audio->i_channels;
168 wf->Format.nSamplesPerSec = audio->i_rate;
169 wf->Format.nAvgBytesPerSec = audio->i_bytes_per_frame * audio->i_rate;
170 wf->Format.nBlockAlign = audio->i_bytes_per_frame;
171 wf->Format.wBitsPerSample = audio->i_bitspersample;
172 wf->Format.cbSize = sizeof (*wf) - sizeof (wf->Format);
174 wf->Samples.wValidBitsPerSample = audio->i_bitspersample;
176 wf->dwChannelMask = 0;
177 if (audio->i_physical_channels & AOUT_CHAN_LEFT)
178 wf->dwChannelMask |= SPEAKER_FRONT_LEFT;
179 if (audio->i_physical_channels & AOUT_CHAN_RIGHT)
180 wf->dwChannelMask |= SPEAKER_FRONT_RIGHT;
181 if (audio->i_physical_channels & AOUT_CHAN_CENTER)
182 wf->dwChannelMask |= SPEAKER_FRONT_CENTER;
183 if (audio->i_physical_channels & AOUT_CHAN_LFE)
184 wf->dwChannelMask |= SPEAKER_LOW_FREQUENCY;
186 if (audio->i_physical_channels & AOUT_CHAN_REARLEFT)
187 wf->dwChannelMask |= SPEAKER_BACK_LEFT;
188 if (audio->i_physical_channels & AOUT_CHAN_REARRIGHT)
189 wf->dwChannelMask |= SPEAKER_BACK_RIGHT;
191 if (audio->i_physical_channels & AOUT_CHAN_REARCENTER)
192 wf->dwChannelMask |= SPEAKER_BACK_CENTER;
193 if (audio->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
194 wf->dwChannelMask |= SPEAKER_SIDE_LEFT;
195 if (audio->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
196 wf->dwChannelMask |= SPEAKER_SIDE_RIGHT;
200 static int vlc_FromWave(const WAVEFORMATEX *restrict wf,
201 audio_sample_format_t *restrict audio)
203 /* FIXME? different sample format? possible? */
204 audio->i_rate = wf->nSamplesPerSec;
206 if (wf->nChannels != audio->i_channels)
209 aout_FormatPrepare(audio);
213 static int Open(vlc_object_t *obj)
215 audio_output_t *aout = (audio_output_t *)obj;
218 aout_sys_t *sys = malloc(sizeof (*sys));
219 if (unlikely(sys == NULL))
226 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
233 /* Select audio device */
234 IMMDeviceEnumerator *devs;
235 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
236 &IID_IMMDeviceEnumerator, (void **)&devs);
239 msg_Dbg(aout, "cannot create device enumerator (error 0x%lx)", hr);
243 /* TODO: support selecting a device from config? */
245 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devs, eRender,
247 IMMDeviceEnumerator_Release(devs);
250 msg_Err(aout, "cannot get audio endpoint (error 0x%lx)", hr);
255 hr = IMMDevice_GetId(dev, &str);
258 msg_Dbg(aout, "using device %ls", str);
262 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_ALL, NULL,
263 (void **)&sys->client);
264 IMMDevice_Release(dev);
267 msg_Err(aout, "cannot activate audio client (error 0x%lx)", hr);
271 /* Configure audio stream */
272 audio_sample_format_t format = aout->format;
273 WAVEFORMATEXTENSIBLE wf;
276 vlc_ToWave(&wf, &format);
277 hr = IAudioClient_IsFormatSupported(sys->client, AUDCLNT_SHAREMODE_SHARED,
279 // TODO: deal with (hr == AUDCLNT_E_DEVICE_INVALIDATED) ?
282 msg_Err(aout, "cannot negotiate audio format (error 0x%lx)", hr);
289 if (vlc_FromWave(pwf, &format))
292 msg_Err(aout, "unsupported audio format");
295 msg_Dbg(aout, "modified format");
299 hr = IAudioClient_Initialize(sys->client, AUDCLNT_SHAREMODE_SHARED, 0,
300 AOUT_MAX_PREPARE_TIME * 10, 0,
301 (hr == S_OK) ? &wf.Format : pwf, NULL);
305 msg_Err(aout, "cannot initialize audio client (error 0x%lx)", hr);
309 hr = IAudioClient_GetBufferSize(sys->client, &sys->frames);
312 msg_Err(aout, "cannot get buffer size (error 0x%lx)", hr);
316 hr = IAudioClient_GetService(sys->client, &IID_IAudioRenderClient,
317 (void **)&sys->render);
320 msg_Err(aout, "cannot get audio render service (error 0x%lx)", hr);
324 aout->format = format;
326 aout->pf_play = Play;
327 aout->pf_pause = Pause;
328 aout->pf_flush = Flush;
329 aout_VolumeNoneInit (aout);
336 static void Close (vlc_object_t *obj)
338 audio_output_t *aout = (audio_output_t *)obj;
339 aout_sys_t *sys = aout->sys;
341 if (sys->render != NULL)
342 IAudioRenderClient_Release(sys->render);
343 if (sys->client != NULL)
344 IAudioClient_Release(sys->client);
345 /* FIXME: CoUninitialize(); */