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 it
7 * 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 *****************************************************************************/
31 #include <audioclient.h>
33 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include "audio_output/mmdevice.h"
38 static LARGE_INTEGER freq; /* performance counters frequency */
40 BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); /* avoid warning */
42 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
49 case DLL_PROCESS_ATTACH:
50 if (!QueryPerformanceFrequency(&freq))
57 static UINT64 GetQPC(void)
59 LARGE_INTEGER counter;
61 if (!QueryPerformanceCounter(&counter))
64 lldiv_t d = lldiv(counter.QuadPart, freq.QuadPart);
65 return (d.quot * 10000000) + ((d.rem * 10000000) / freq.QuadPart);
68 typedef struct aout_stream_sys
72 uint8_t chans_table[AOUT_CHAN_MAX];
73 uint8_t chans_to_reorder;
75 vlc_fourcc_t format; /**< Sample format */
76 unsigned rate; /**< Sample rate */
77 unsigned bytes_per_frame;
78 UINT32 written; /**< Frames written to the buffer */
79 UINT32 frames; /**< Total buffer size (frames) */
83 /*** VLC audio output callbacks ***/
84 static HRESULT TimeGet(aout_stream_t *s, mtime_t *restrict delay)
86 aout_stream_sys_t *sys = s->sys;
91 hr = IAudioClient_GetService(sys->client, &IID_IAudioClock, &pv);
94 IAudioClock *clock = pv;
96 hr = IAudioClock_GetPosition(clock, &pos, &qpcpos);
98 msg_Err(s, "cannot get position (error 0x%lx)", hr);
99 IAudioClock_Release(clock);
102 msg_Err(s, "cannot get clock (error 0x%lx)", hr);
108 *delay = ((GetQPC() - qpcpos) / (10000000 / CLOCK_FREQ));
109 static_assert((10000000 % CLOCK_FREQ) == 0,
110 "Frequency conversion broken");
114 *delay = sys->written * CLOCK_FREQ / sys->rate;
115 msg_Dbg(s, "extrapolating position: still propagating buffers");
121 static HRESULT Play(aout_stream_t *s, block_t *block)
123 aout_stream_sys_t *sys = s->sys;
127 if (sys->chans_to_reorder)
128 aout_ChannelReorder(block->p_buffer, block->i_buffer,
129 sys->chans_to_reorder, sys->chans_table, sys->format);
131 hr = IAudioClient_GetService(sys->client, &IID_IAudioRenderClient, &pv);
134 msg_Err(s, "cannot get render client (error 0x%lx)", hr);
138 IAudioRenderClient *render = pv;
142 hr = IAudioClient_GetCurrentPadding(sys->client, &frames);
145 msg_Err(s, "cannot get current padding (error 0x%lx)", hr);
149 assert(frames <= sys->frames);
150 frames = sys->frames - frames;
151 if (frames > block->i_nb_samples)
152 frames = block->i_nb_samples;
155 hr = IAudioRenderClient_GetBuffer(render, frames, &dst);
158 msg_Err(s, "cannot get buffer (error 0x%lx)", hr);
162 const size_t copy = frames * sys->bytes_per_frame;
164 memcpy(dst, block->p_buffer, copy);
165 hr = IAudioRenderClient_ReleaseBuffer(render, frames, 0);
168 msg_Err(s, "cannot release buffer (error 0x%lx)", hr);
171 IAudioClient_Start(sys->client);
173 block->p_buffer += copy;
174 block->i_buffer -= copy;
175 block->i_nb_samples -= frames;
176 sys->written += frames;
177 if (block->i_nb_samples == 0)
180 /* Out of buffer space, sleep */
181 msleep(AOUT_MIN_PREPARE_TIME
182 + block->i_nb_samples * CLOCK_FREQ / sys->rate);
184 IAudioRenderClient_Release(render);
186 block_Release(block);
191 static HRESULT Pause(aout_stream_t *s, bool paused)
193 aout_stream_sys_t *sys = s->sys;
197 hr = IAudioClient_Stop(sys->client);
199 hr = IAudioClient_Start(sys->client);
201 msg_Warn(s, "cannot %s stream (error 0x%lx)",
202 paused ? "stop" : "start", hr);
206 static HRESULT Flush(aout_stream_t *s)
208 aout_stream_sys_t *sys = s->sys;
211 IAudioClient_Stop(sys->client);
213 hr = IAudioClient_Reset(sys->client);
215 msg_Warn(s, "cannot reset stream (error 0x%lx)", hr);
222 /*** Initialization / deinitialization **/
223 static const uint32_t chans_out[] = {
224 SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
225 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
226 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER,
227 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0
229 static const uint32_t chans_in[] = {
230 SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
231 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
232 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER,
233 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0
236 static void vlc_ToWave(WAVEFORMATEXTENSIBLE *restrict wf,
237 audio_sample_format_t *restrict audio)
239 switch (audio->i_format)
242 audio->i_format = VLC_CODEC_FL32;
244 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
248 audio->i_format = VLC_CODEC_S16N;
250 wf->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
254 audio->i_format = VLC_CODEC_FL32;
255 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
258 aout_FormatPrepare (audio);
260 wf->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
261 wf->Format.nChannels = audio->i_channels;
262 wf->Format.nSamplesPerSec = audio->i_rate;
263 wf->Format.nAvgBytesPerSec = audio->i_bytes_per_frame * audio->i_rate;
264 wf->Format.nBlockAlign = audio->i_bytes_per_frame;
265 wf->Format.wBitsPerSample = audio->i_bitspersample;
266 wf->Format.cbSize = sizeof (*wf) - sizeof (wf->Format);
268 wf->Samples.wValidBitsPerSample = audio->i_bitspersample;
270 wf->dwChannelMask = 0;
271 for (unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++)
272 if (audio->i_physical_channels & pi_vlc_chan_order_wg4[i])
273 wf->dwChannelMask |= chans_in[i];
276 static int vlc_FromWave(const WAVEFORMATEX *restrict wf,
277 audio_sample_format_t *restrict audio)
279 audio->i_rate = wf->nSamplesPerSec;
280 audio->i_physical_channels = 0;
282 if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
284 const WAVEFORMATEXTENSIBLE *wfe = (void *)wf;
286 for (unsigned i = 0; chans_in[i]; i++)
287 if (wfe->dwChannelMask & chans_in[i])
288 audio->i_physical_channels |= pi_vlc_chan_order_wg4[i];
291 audio->i_original_channels = audio->i_physical_channels;
292 aout_FormatPrepare (audio);
294 if (wf->nChannels != audio->i_channels)
299 static unsigned vlc_CheckWaveOrder (const WAVEFORMATEX *restrict wf,
300 uint8_t *restrict table)
304 if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
306 const WAVEFORMATEXTENSIBLE *wfe = (void *)wf;
308 mask = wfe->dwChannelMask;
310 return aout_CheckChannelReorder(chans_in, chans_out, mask, table);
313 static HRESULT Start(aout_stream_t *s, audio_sample_format_t *restrict fmt,
316 if (!s->b_force && var_InheritBool(s, "spdif"))
317 /* Fallback to other plugin until pass-through is implemented */
320 aout_stream_sys_t *sys = malloc(sizeof (*sys));
321 if (unlikely(sys == NULL))
322 return E_OUTOFMEMORY;
326 HRESULT hr = aout_stream_Activate(s, &IID_IAudioClient, NULL, &pv);
329 msg_Err(s, "cannot activate client (error 0x%lx)", hr);
334 /* Configure audio stream */
335 WAVEFORMATEXTENSIBLE wf;
338 vlc_ToWave(&wf, fmt);
339 hr = IAudioClient_IsFormatSupported(sys->client, AUDCLNT_SHAREMODE_SHARED,
343 msg_Err(s, "cannot negotiate audio format (error 0x%lx)", hr);
350 if (vlc_FromWave(pwf, fmt))
353 msg_Err(s, "unsupported audio format");
357 msg_Dbg(s, "modified format");
362 sys->chans_to_reorder = vlc_CheckWaveOrder((hr == S_OK) ? &wf.Format : pwf,
364 sys->format = fmt->i_format;
366 hr = IAudioClient_Initialize(sys->client, AUDCLNT_SHAREMODE_SHARED, 0,
367 AOUT_MAX_PREPARE_TIME * 10, 0,
368 (hr == S_OK) ? &wf.Format : pwf, sid);
372 msg_Err(s, "cannot initialize audio client (error 0x%lx)", hr);
376 hr = IAudioClient_GetBufferSize(sys->client, &sys->frames);
379 msg_Err(s, "cannot get buffer size (error 0x%lx)", hr);
383 sys->rate = fmt->i_rate;
384 sys->bytes_per_frame = fmt->i_bytes_per_frame;
387 s->time_get = TimeGet;
393 if (sys->client != NULL)
394 IAudioClient_Release(sys->client);
399 static void Stop(aout_stream_t *s)
401 aout_stream_sys_t *sys = s->sys;
403 IAudioClient_Stop(sys->client); /* should not be needed */
404 IAudioClient_Release(sys->client);
408 set_shortname("WASAPI")
409 set_description(N_("Windows Audio Session API output"))
410 set_capability("aout stream", /*50*/0)
411 set_category(CAT_AUDIO)
412 set_subcategory(SUBCAT_AUDIO_AOUT)
413 set_callbacks(Start, Stop)