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>
37 static LARGE_INTEGER freq; /* performance counters frequency */
39 BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); /* avoid warning */
41 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
48 case DLL_PROCESS_ATTACH:
49 if (!QueryPerformanceFrequency(&freq))
56 static UINT64 GetQPC(void)
58 LARGE_INTEGER counter;
60 if (!QueryPerformanceCounter(&counter))
63 lldiv_t d = lldiv(counter.QuadPart, freq.QuadPart);
64 return (d.quot * 10000000) + ((d.rem * 10000000) / freq.QuadPart);
67 typedef struct aout_stream_sys
71 uint8_t chans_table[AOUT_CHAN_MAX];
72 uint8_t chans_to_reorder;
74 vlc_fourcc_t format; /**< Sample format */
75 unsigned rate; /**< Sample rate */
76 unsigned bytes_per_frame;
77 UINT32 written; /**< Frames written to the buffer */
78 UINT32 frames; /**< Total buffer size (frames) */
82 /*** VLC audio output callbacks ***/
83 static HRESULT TimeGet(aout_stream_t *s, mtime_t *restrict delay)
85 aout_stream_sys_t *sys = s->sys;
90 hr = IAudioClient_GetService(sys->client, &IID_IAudioClock, &pv);
93 IAudioClock *clock = pv;
95 hr = IAudioClock_GetPosition(clock, &pos, &qpcpos);
97 msg_Err(s, "cannot get position (error 0x%lx)", hr);
98 IAudioClock_Release(clock);
101 msg_Err(s, "cannot get clock (error 0x%lx)", hr);
107 *delay = ((GetQPC() - qpcpos) / (10000000 / CLOCK_FREQ));
108 static_assert((10000000 % CLOCK_FREQ) == 0,
109 "Frequency conversion broken");
113 *delay = sys->written * CLOCK_FREQ / sys->rate;
114 msg_Dbg(s, "extrapolating position: still propagating buffers");
120 static HRESULT Play(aout_stream_t *s, block_t *block)
122 aout_stream_sys_t *sys = s->sys;
126 if (sys->chans_to_reorder)
127 aout_ChannelReorder(block->p_buffer, block->i_buffer,
128 sys->chans_to_reorder, sys->chans_table, sys->format);
130 hr = IAudioClient_GetService(sys->client, &IID_IAudioRenderClient, &pv);
133 msg_Err(s, "cannot get render client (error 0x%lx)", hr);
137 IAudioRenderClient *render = pv;
141 hr = IAudioClient_GetCurrentPadding(sys->client, &frames);
144 msg_Err(s, "cannot get current padding (error 0x%lx)", hr);
148 assert(frames <= sys->frames);
149 frames = sys->frames - frames;
150 if (frames > block->i_nb_samples)
151 frames = block->i_nb_samples;
154 hr = IAudioRenderClient_GetBuffer(render, frames, &dst);
157 msg_Err(s, "cannot get buffer (error 0x%lx)", hr);
161 const size_t copy = frames * sys->bytes_per_frame;
163 memcpy(dst, block->p_buffer, copy);
164 hr = IAudioRenderClient_ReleaseBuffer(render, frames, 0);
167 msg_Err(s, "cannot release buffer (error 0x%lx)", hr);
170 IAudioClient_Start(sys->client);
172 block->p_buffer += copy;
173 block->i_buffer -= copy;
174 block->i_nb_samples -= frames;
175 sys->written += frames;
176 if (block->i_nb_samples == 0)
179 /* Out of buffer space, sleep */
180 msleep(AOUT_MIN_PREPARE_TIME
181 + block->i_nb_samples * CLOCK_FREQ / sys->rate);
183 IAudioRenderClient_Release(render);
185 block_Release(block);
190 static HRESULT Pause(aout_stream_t *s, bool paused)
192 aout_stream_sys_t *sys = s->sys;
196 hr = IAudioClient_Stop(sys->client);
198 hr = IAudioClient_Start(sys->client);
200 msg_Warn(s, "cannot %s stream (error 0x%lx)",
201 paused ? "stop" : "start", hr);
205 static HRESULT Flush(aout_stream_t *s)
207 aout_stream_sys_t *sys = s->sys;
210 IAudioClient_Stop(sys->client);
212 hr = IAudioClient_Reset(sys->client);
214 msg_Warn(s, "cannot reset stream (error 0x%lx)", hr);
221 /*** Initialization / deinitialization **/
222 static const uint32_t chans_out[] = {
223 SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
224 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
225 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER,
226 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0
228 static const uint32_t chans_in[] = {
229 SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
230 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
231 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER,
232 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0
235 static void vlc_ToWave(WAVEFORMATEXTENSIBLE *restrict wf,
236 audio_sample_format_t *restrict audio)
238 switch (audio->i_format)
241 audio->i_format = VLC_CODEC_FL32;
243 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
247 audio->i_format = VLC_CODEC_S16N;
249 wf->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
253 audio->i_format = VLC_CODEC_FL32;
254 wf->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
257 aout_FormatPrepare (audio);
259 wf->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
260 wf->Format.nChannels = audio->i_channels;
261 wf->Format.nSamplesPerSec = audio->i_rate;
262 wf->Format.nAvgBytesPerSec = audio->i_bytes_per_frame * audio->i_rate;
263 wf->Format.nBlockAlign = audio->i_bytes_per_frame;
264 wf->Format.wBitsPerSample = audio->i_bitspersample;
265 wf->Format.cbSize = sizeof (*wf) - sizeof (wf->Format);
267 wf->Samples.wValidBitsPerSample = audio->i_bitspersample;
269 wf->dwChannelMask = 0;
270 for (unsigned i = 0; pi_vlc_chan_order_wg4[i]; i++)
271 if (audio->i_physical_channels & pi_vlc_chan_order_wg4[i])
272 wf->dwChannelMask |= chans_in[i];
275 static int vlc_FromWave(const WAVEFORMATEX *restrict wf,
276 audio_sample_format_t *restrict audio)
278 audio->i_rate = wf->nSamplesPerSec;
279 audio->i_physical_channels = 0;
281 if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
283 const WAVEFORMATEXTENSIBLE *wfe = (void *)wf;
285 for (unsigned i = 0; chans_in[i]; i++)
286 if (wfe->dwChannelMask & chans_in[i])
287 audio->i_physical_channels |= pi_vlc_chan_order_wg4[i];
290 audio->i_original_channels = audio->i_physical_channels;
291 aout_FormatPrepare (audio);
293 if (wf->nChannels != audio->i_channels)
298 static unsigned vlc_CheckWaveOrder (const WAVEFORMATEX *restrict wf,
299 uint8_t *restrict table)
303 if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
305 const WAVEFORMATEXTENSIBLE *wfe = (void *)wf;
307 mask = wfe->dwChannelMask;
309 return aout_CheckChannelReorder(chans_in, chans_out, mask, table);
312 static HRESULT Start(aout_stream_t *s, audio_sample_format_t *restrict fmt,
315 aout_stream_sys_t *sys = malloc(sizeof (*sys));
316 if (unlikely(sys == NULL))
317 return E_OUTOFMEMORY;
321 HRESULT hr = aout_stream_Activate(s, &IID_IAudioClient, NULL, &pv);
324 msg_Err(s, "cannot activate client (error 0x%lx)", hr);
329 /* Configure audio stream */
330 WAVEFORMATEXTENSIBLE wf;
333 vlc_ToWave(&wf, fmt);
334 hr = IAudioClient_IsFormatSupported(sys->client, AUDCLNT_SHAREMODE_SHARED,
338 msg_Err(s, "cannot negotiate audio format (error 0x%lx)", hr);
345 if (vlc_FromWave(pwf, fmt))
348 msg_Err(s, "unsupported audio format");
352 msg_Dbg(s, "modified format");
357 sys->chans_to_reorder = vlc_CheckWaveOrder((hr == S_OK) ? &wf.Format : pwf,
359 sys->format = fmt->i_format;
361 hr = IAudioClient_Initialize(sys->client, AUDCLNT_SHAREMODE_SHARED, 0,
362 AOUT_MAX_PREPARE_TIME * 10, 0,
363 (hr == S_OK) ? &wf.Format : pwf, sid);
367 msg_Err(s, "cannot initialize audio client (error 0x%lx)", hr);
371 hr = IAudioClient_GetBufferSize(sys->client, &sys->frames);
374 msg_Err(s, "cannot get buffer size (error 0x%lx)", hr);
378 sys->rate = fmt->i_rate;
379 sys->bytes_per_frame = fmt->i_bytes_per_frame;
382 s->time_get = TimeGet;
388 if (sys->client != NULL)
389 IAudioClient_Release(sys->client);
394 static void Stop(aout_stream_t *s)
396 aout_stream_sys_t *sys = s->sys;
398 IAudioClient_Stop(sys->client); /* should not be needed */
399 IAudioClient_Release(sys->client);
402 HRESULT aout_stream_Start(aout_stream_t *s,
403 audio_sample_format_t *restrict fmt, const GUID *sid)
405 return Start(s, fmt, sid);
408 void aout_stream_Stop(aout_stream_t *s)