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;
126 return; /* Not drain implemented */
128 IAudioClient_Stop(sys->client);
129 hr = IAudioClient_Reset(sys->client);
131 msg_Warn(aout, "cannot reset stream (error 0x%lx)", hr);
134 /*static int VolumeSet(audio_output_t *aout, float vol, bool mute)
136 aout_sys_t *sys = aout->sys;
141 static void vlc_ToWave(WAVEFORMATEXTENSIBLE *restrict wf,
142 audio_sample_format_t *restrict audio)
144 switch (audio->i_format)
149 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
156 wf->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
160 audio->i_format = VLC_CODEC_FL32;
161 audio->i_rate = 48000;
162 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
165 aout_FormatPrepare (audio);
167 wf->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
168 wf->Format.nChannels = audio->i_channels;
169 wf->Format.nSamplesPerSec = audio->i_rate;
170 wf->Format.nAvgBytesPerSec = audio->i_bytes_per_frame * audio->i_rate;
171 wf->Format.nBlockAlign = audio->i_bytes_per_frame;
172 wf->Format.wBitsPerSample = audio->i_bitspersample;
173 wf->Format.cbSize = sizeof (*wf) - sizeof (wf->Format);
175 wf->Samples.wValidBitsPerSample = audio->i_bitspersample;
177 wf->dwChannelMask = 0;
178 if (audio->i_physical_channels & AOUT_CHAN_LEFT)
179 wf->dwChannelMask |= SPEAKER_FRONT_LEFT;
180 if (audio->i_physical_channels & AOUT_CHAN_RIGHT)
181 wf->dwChannelMask |= SPEAKER_FRONT_RIGHT;
182 if (audio->i_physical_channels & AOUT_CHAN_CENTER)
183 wf->dwChannelMask |= SPEAKER_FRONT_CENTER;
184 if (audio->i_physical_channels & AOUT_CHAN_LFE)
185 wf->dwChannelMask |= SPEAKER_LOW_FREQUENCY;
187 if (audio->i_physical_channels & AOUT_CHAN_REARLEFT)
188 wf->dwChannelMask |= SPEAKER_BACK_LEFT;
189 if (audio->i_physical_channels & AOUT_CHAN_REARRIGHT)
190 wf->dwChannelMask |= SPEAKER_BACK_RIGHT;
192 if (audio->i_physical_channels & AOUT_CHAN_REARCENTER)
193 wf->dwChannelMask |= SPEAKER_BACK_CENTER;
194 if (audio->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
195 wf->dwChannelMask |= SPEAKER_SIDE_LEFT;
196 if (audio->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
197 wf->dwChannelMask |= SPEAKER_SIDE_RIGHT;
201 static int vlc_FromWave(const WAVEFORMATEX *restrict wf,
202 audio_sample_format_t *restrict audio)
204 /* FIXME? different sample format? possible? */
205 audio->i_rate = wf->nSamplesPerSec;
207 if (wf->nChannels != audio->i_channels)
210 aout_FormatPrepare(audio);
214 static int Open(vlc_object_t *obj)
216 audio_output_t *aout = (audio_output_t *)obj;
219 aout_sys_t *sys = malloc(sizeof (*sys));
220 if (unlikely(sys == NULL))
227 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
234 /* Select audio device */
235 IMMDeviceEnumerator *devs;
236 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
237 &IID_IMMDeviceEnumerator, (void **)&devs);
240 msg_Dbg(aout, "cannot create device enumerator (error 0x%lx)", hr);
244 /* TODO: support selecting a device from config? */
246 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devs, eRender,
248 IMMDeviceEnumerator_Release(devs);
251 msg_Err(aout, "cannot get audio endpoint (error 0x%lx)", hr);
256 hr = IMMDevice_GetId(dev, &str);
259 msg_Dbg(aout, "using device %ls", str);
263 hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_ALL, NULL,
264 (void **)&sys->client);
265 IMMDevice_Release(dev);
268 msg_Err(aout, "cannot activate audio client (error 0x%lx)", hr);
272 /* Configure audio stream */
273 audio_sample_format_t format = aout->format;
274 WAVEFORMATEXTENSIBLE wf;
277 vlc_ToWave(&wf, &format);
278 hr = IAudioClient_IsFormatSupported(sys->client, AUDCLNT_SHAREMODE_SHARED,
280 // TODO: deal with (hr == AUDCLNT_E_DEVICE_INVALIDATED) ?
283 msg_Err(aout, "cannot negotiate audio format (error 0x%lx)", hr);
290 if (vlc_FromWave(pwf, &format))
293 msg_Err(aout, "unsupported audio format");
296 msg_Dbg(aout, "modified format");
300 hr = IAudioClient_Initialize(sys->client, AUDCLNT_SHAREMODE_SHARED, 0,
301 AOUT_MAX_PREPARE_TIME * 10, 0,
302 (hr == S_OK) ? &wf.Format : pwf, NULL);
306 msg_Err(aout, "cannot initialize audio client (error 0x%lx)", hr);
310 hr = IAudioClient_GetBufferSize(sys->client, &sys->frames);
313 msg_Err(aout, "cannot get buffer size (error 0x%lx)", hr);
317 hr = IAudioClient_GetService(sys->client, &IID_IAudioRenderClient,
318 (void **)&sys->render);
321 msg_Err(aout, "cannot get audio render service (error 0x%lx)", hr);
325 aout->format = format;
327 aout->pf_play = Play;
328 aout->pf_pause = Pause;
329 aout->pf_flush = Flush;
330 aout_VolumeNoneInit (aout);
337 static void Close (vlc_object_t *obj)
339 audio_output_t *aout = (audio_output_t *)obj;
340 aout_sys_t *sys = aout->sys;
342 if (sys->render != NULL)
343 IAudioRenderClient_Release(sys->render);
344 if (sys->client != NULL)
345 IAudioClient_Release(sys->client);
346 /* FIXME: CoUninitialize(); */