2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
3 * Copyright 2011 Andrew Eikum for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define NONAMELESSUNION
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "wine/list.h"
37 #include "mmdeviceapi.h"
40 #include "audioclient.h"
41 #include "endpointvolume.h"
42 #include "audiopolicy.h"
46 #include <alsa/asoundlib.h>
48 WINE_DEFAULT_DEBUG_CHANNEL(alsa);
50 static const REFERENCE_TIME DefaultPeriod = 200000;
51 static const REFERENCE_TIME MinimumPeriod = 100000;
53 typedef struct ACImpl {
54 IAudioClient IAudioClient_iface;
55 IAudioRenderClient IAudioRenderClient_iface;
56 IAudioCaptureClient IAudioCaptureClient_iface;
57 IAudioSessionControl2 IAudioSessionControl2_iface;
58 ISimpleAudioVolume ISimpleAudioVolume_iface;
59 IAudioClock IAudioClock_iface;
60 IAudioClock2 IAudioClock2_iface;
64 snd_pcm_t *pcm_handle;
65 snd_pcm_uframes_t period_alsa, bufsize_alsa;
66 snd_pcm_hw_params_t *hw_params; /* does not hold state between calls */
73 AUDCLNT_SHAREMODE share;
76 BOOL initted, started;
77 UINT64 written_frames, held_frames, tmp_buffer_frames;
78 UINT32 bufsize_frames, period_us;
79 UINT32 lcl_offs_frames; /* offs into local_buffer where valid data starts */
82 BYTE *local_buffer, *tmp_buffer;
85 CRITICAL_SECTION lock;
90 LOCKED_NORMAL, /* public buffer piece is from local_buffer */
91 LOCKED_WRAPPED /* public buffer piece is wrapped around, in tmp_buffer */
94 static HANDLE g_timer_q;
96 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
98 static const IAudioClientVtbl AudioClient_Vtbl;
99 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
100 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
101 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
102 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
103 static const IAudioClockVtbl AudioClock_Vtbl;
104 static const IAudioClock2Vtbl AudioClock2_Vtbl;
106 int wine_snd_pcm_recover(snd_pcm_t *pcm, int err, int silent);
108 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
110 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
113 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
115 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
118 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
120 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
123 static inline ACImpl *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
125 return CONTAINING_RECORD(iface, ACImpl, IAudioSessionControl2_iface);
128 static inline ACImpl *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
130 return CONTAINING_RECORD(iface, ACImpl, ISimpleAudioVolume_iface);
133 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
135 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
138 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
140 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
143 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, void ***keys,
144 UINT *num, UINT *def_index)
146 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
151 *ids = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *));
153 return E_OUTOFMEMORY;
155 (*ids)[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW));
157 HeapFree(GetProcessHeap(), 0, *ids);
158 return E_OUTOFMEMORY;
161 lstrcpyW((*ids)[0], defaultW);
163 *keys = HeapAlloc(GetProcessHeap(), 0, sizeof(void *));
169 HRESULT WINAPI AUDDRV_GetAudioEndpoint(void *key, IMMDevice *dev,
170 EDataFlow dataflow, IAudioClient **out)
174 snd_pcm_stream_t stream;
176 TRACE("%p %p %d %p\n", key, dev, dataflow, out);
179 g_timer_q = CreateTimerQueue();
184 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
186 return E_OUTOFMEMORY;
188 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
189 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
190 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
191 This->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
192 This->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
193 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
194 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
196 if(dataflow == eRender)
197 stream = SND_PCM_STREAM_PLAYBACK;
198 else if(dataflow == eCapture)
199 stream = SND_PCM_STREAM_CAPTURE;
201 HeapFree(GetProcessHeap(), 0, This);
205 This->dataflow = dataflow;
206 if((err = snd_pcm_open(&This->pcm_handle, "default", stream,
207 SND_PCM_NONBLOCK)) < 0){
208 HeapFree(GetProcessHeap(), 0, This);
209 WARN("Unable to open PCM \"default\": %d (%s)\n", err,
214 This->hw_params = HeapAlloc(GetProcessHeap(), 0,
215 snd_pcm_hw_params_sizeof());
216 if(!This->hw_params){
217 HeapFree(GetProcessHeap(), 0, This);
218 snd_pcm_close(This->pcm_handle);
219 return E_OUTOFMEMORY;
222 InitializeCriticalSection(&This->lock);
225 IMMDevice_AddRef(This->parent);
227 *out = &This->IAudioClient_iface;
228 IAudioClient_AddRef(&This->IAudioClient_iface);
233 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
234 REFIID riid, void **ppv)
236 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
241 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
244 IUnknown_AddRef((IUnknown*)*ppv);
247 WARN("Unknown interface %s\n", debugstr_guid(riid));
248 return E_NOINTERFACE;
251 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
253 ACImpl *This = impl_from_IAudioClient(iface);
255 ref = InterlockedIncrement(&This->ref);
256 TRACE("(%p) Refcount now %u\n", This, ref);
260 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
262 ACImpl *This = impl_from_IAudioClient(iface);
264 ref = InterlockedDecrement(&This->ref);
265 TRACE("(%p) Refcount now %u\n", This, ref);
267 IAudioClient_Stop(iface);
268 IMMDevice_Release(This->parent);
269 DeleteCriticalSection(&This->lock);
270 snd_pcm_drop(This->pcm_handle);
271 snd_pcm_close(This->pcm_handle);
272 HeapFree(GetProcessHeap(), 0, This->local_buffer);
273 HeapFree(GetProcessHeap(), 0, This->hw_params);
274 HeapFree(GetProcessHeap(), 0, This->fmt);
275 HeapFree(GetProcessHeap(), 0, This);
280 static void dump_fmt(const WAVEFORMATEX *fmt)
282 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
283 switch(fmt->wFormatTag){
284 case WAVE_FORMAT_PCM:
285 TRACE("WAVE_FORMAT_PCM");
287 case WAVE_FORMAT_IEEE_FLOAT:
288 TRACE("WAVE_FORMAT_IEEE_FLOAT");
290 case WAVE_FORMAT_EXTENSIBLE:
291 TRACE("WAVE_FORMAT_EXTENSIBLE");
299 TRACE("nChannels: %u\n", fmt->nChannels);
300 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
301 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
302 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
303 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
304 TRACE("cbSize: %u\n", fmt->cbSize);
306 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
307 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
308 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
309 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
310 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
314 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
319 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
320 size = sizeof(WAVEFORMATEXTENSIBLE);
322 size = sizeof(WAVEFORMATEX);
324 ret = HeapAlloc(GetProcessHeap(), 0, size);
328 memcpy(ret, fmt, size);
330 ret->cbSize = size - sizeof(WAVEFORMATEX);
335 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
336 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
337 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
338 const GUID *sessionguid)
340 ACImpl *This = impl_from_IAudioClient(iface);
341 snd_pcm_sw_params_t *sw_params = NULL;
342 snd_pcm_format_t format;
343 snd_pcm_uframes_t boundary;
344 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
345 unsigned int time_us, rate;
349 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
350 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
355 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
356 return AUDCLNT_E_NOT_INITIALIZED;
358 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
359 AUDCLNT_STREAMFLAGS_LOOPBACK |
360 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
361 AUDCLNT_STREAMFLAGS_NOPERSIST |
362 AUDCLNT_STREAMFLAGS_RATEADJUST |
363 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
364 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
365 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
366 TRACE("Unknown flags: %08x\n", flags);
370 EnterCriticalSection(&This->lock);
373 LeaveCriticalSection(&This->lock);
374 return AUDCLNT_E_ALREADY_INITIALIZED;
379 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
380 WARN("Unable to get hw_params: %d (%s)\n", err, snd_strerror(err));
385 if((err = snd_pcm_hw_params_set_access(This->pcm_handle, This->hw_params,
386 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0){
387 WARN("Unable to set access: %d (%s)\n", err, snd_strerror(err));
392 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
393 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
394 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
395 if(fmt->wBitsPerSample == 8)
396 format = SND_PCM_FORMAT_U8;
397 else if(fmt->wBitsPerSample == 16)
398 format = SND_PCM_FORMAT_S16_LE;
399 else if(fmt->wBitsPerSample == 24)
400 format = SND_PCM_FORMAT_S24_3LE;
401 else if(fmt->wBitsPerSample == 32)
402 format = SND_PCM_FORMAT_S32_LE;
404 WARN("Unsupported bit depth: %u\n", fmt->wBitsPerSample);
405 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
408 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
409 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
410 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
411 if(fmt->wBitsPerSample != 32){
412 WARN("Unsupported float size: %u\n", fmt->wBitsPerSample);
413 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
416 format = SND_PCM_FORMAT_FLOAT_LE;
418 WARN("Unknown wave format: %04x\n", fmt->wFormatTag);
419 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
423 if((err = snd_pcm_hw_params_set_format(This->pcm_handle, This->hw_params,
425 WARN("Unable to set ALSA format to %u: %d (%s)\n", format, err,
431 rate = fmt->nSamplesPerSec;
432 if((err = snd_pcm_hw_params_set_rate_near(This->pcm_handle, This->hw_params,
434 WARN("Unable to set rate to %u: %d (%s)\n", rate, err,
440 if((err = snd_pcm_hw_params_set_channels(This->pcm_handle, This->hw_params,
441 fmt->nChannels)) < 0){
442 WARN("Unable to set channels to %u: %d (%s)\n", fmt->nChannels, err,
448 time_us = MinimumPeriod / 10;
449 if((err = snd_pcm_hw_params_set_period_time_near(This->pcm_handle,
450 This->hw_params, &time_us, NULL)) < 0){
451 WARN("Unable to set max period time to %u: %d (%s)\n", time_us,
452 err, snd_strerror(err));
457 if((err = snd_pcm_hw_params(This->pcm_handle, This->hw_params)) < 0){
458 WARN("Unable to set hw params: %d (%s)\n", err, snd_strerror(err));
463 sw_params = HeapAlloc(GetProcessHeap(), 0, snd_pcm_sw_params_sizeof());
469 if((err = snd_pcm_sw_params_current(This->pcm_handle, sw_params)) < 0){
470 WARN("Unable to get sw_params: %d (%s)\n", err, snd_strerror(err));
475 This->bufsize_frames = ceil((duration / 10000000.) * fmt->nSamplesPerSec);
476 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
477 This->bufsize_frames * fmt->nBlockAlign);
478 if(!This->local_buffer){
482 if (fmt->wBitsPerSample == 8)
483 memset(This->local_buffer, 128, This->bufsize_frames * fmt->nBlockAlign);
485 memset(This->local_buffer, 0, This->bufsize_frames * fmt->nBlockAlign);
487 if((err = snd_pcm_sw_params_get_boundary(sw_params, &boundary)) < 0){
488 WARN("Unable to get boundary: %d (%s)\n", err, snd_strerror(err));
493 if((err = snd_pcm_sw_params_set_start_threshold(This->pcm_handle,
494 sw_params, boundary)) < 0){
495 WARN("Unable to set start threshold to %lx: %d (%s)\n", boundary, err,
501 if((err = snd_pcm_sw_params_set_stop_threshold(This->pcm_handle,
502 sw_params, boundary)) < 0){
503 WARN("Unable to set stop threshold to %lx: %d (%s)\n", boundary, err,
509 if((err = snd_pcm_sw_params_set_avail_min(This->pcm_handle,
511 WARN("Unable to set avail min to 0: %d (%s)\n", err, snd_strerror(err));
516 if((err = snd_pcm_sw_params(This->pcm_handle, sw_params)) < 0){
517 WARN("Unable to set sw params: %d (%s)\n", err, snd_strerror(err));
522 if((err = snd_pcm_prepare(This->pcm_handle)) < 0){
523 WARN("Unable to prepare device: %d (%s)\n", err, snd_strerror(err));
528 if((err = snd_pcm_hw_params_get_buffer_size(This->hw_params,
529 &This->bufsize_alsa)) < 0){
530 WARN("Unable to get buffer size: %d (%s)\n", err, snd_strerror(err));
535 if((err = snd_pcm_hw_params_get_period_size(This->hw_params,
536 &This->period_alsa, NULL)) < 0){
537 WARN("Unable to get period size: %d (%s)\n", err, snd_strerror(err));
542 if((err = snd_pcm_hw_params_get_period_time(This->hw_params,
543 &This->period_us, NULL)) < 0){
544 WARN("Unable to get period time: %d (%s)\n", err, snd_strerror(err));
549 This->fmt = clone_format(fmt);
555 This->initted = TRUE;
560 HeapFree(GetProcessHeap(), 0, sw_params);
562 if(This->local_buffer){
563 HeapFree(GetProcessHeap(), 0, This->local_buffer);
564 This->local_buffer = NULL;
568 LeaveCriticalSection(&This->lock);
573 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
576 ACImpl *This = impl_from_IAudioClient(iface);
578 TRACE("(%p)->(%p)\n", This, out);
583 EnterCriticalSection(&This->lock);
586 LeaveCriticalSection(&This->lock);
587 return AUDCLNT_E_NOT_INITIALIZED;
590 *out = This->bufsize_frames;
592 LeaveCriticalSection(&This->lock);
597 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
598 REFERENCE_TIME *latency)
600 ACImpl *This = impl_from_IAudioClient(iface);
602 TRACE("(%p)->(%p)\n", This, latency);
607 EnterCriticalSection(&This->lock);
610 LeaveCriticalSection(&This->lock);
611 return AUDCLNT_E_NOT_INITIALIZED;
614 LeaveCriticalSection(&This->lock);
621 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
624 ACImpl *This = impl_from_IAudioClient(iface);
626 TRACE("(%p)->(%p)\n", This, out);
631 EnterCriticalSection(&This->lock);
634 LeaveCriticalSection(&This->lock);
635 return AUDCLNT_E_NOT_INITIALIZED;
638 if(This->dataflow == eRender){
639 snd_pcm_sframes_t avail_frames;
641 avail_frames = snd_pcm_avail_update(This->pcm_handle);
643 if(This->bufsize_alsa < avail_frames){
644 WARN("Xrun detected\n");
645 *out = This->held_frames;
647 *out = This->bufsize_alsa - avail_frames + This->held_frames;
648 }else if(This->dataflow == eCapture){
649 *out = This->held_frames;
651 LeaveCriticalSection(&This->lock);
655 LeaveCriticalSection(&This->lock);
660 static DWORD get_channel_mask(unsigned int channels)
666 return SPEAKER_FRONT_CENTER;
668 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
670 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
671 SPEAKER_LOW_FREQUENCY;
673 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
676 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
677 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY;
679 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
680 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER;
682 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
683 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER |
686 FIXME("Unknown speaker configuration: %u\n", channels);
690 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
691 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *fmt,
694 ACImpl *This = impl_from_IAudioClient(iface);
695 snd_pcm_format_mask_t *formats = NULL;
697 WAVEFORMATEX *closest = NULL;
698 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
699 unsigned int max = 0, min = 0;
702 TRACE("(%p)->(%x, %p, %p)\n", This, mode, fmt, out);
704 if(!fmt || (mode == AUDCLNT_SHAREMODE_SHARED && !out))
707 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
710 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
711 fmt->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
716 EnterCriticalSection(&This->lock);
718 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
723 formats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
724 snd_pcm_format_mask_sizeof());
730 snd_pcm_hw_params_get_format_mask(This->hw_params, formats);
732 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
733 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
734 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
735 switch(fmt->wBitsPerSample){
737 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_U8)){
738 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
743 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S16_LE)){
744 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
749 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S24_3LE)){
750 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
755 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S32_LE)){
756 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
761 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
764 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
765 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
766 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
767 switch(fmt->wBitsPerSample){
769 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT_LE)){
770 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
775 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT64_LE)){
776 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
781 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
785 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
789 closest = clone_format(fmt);
795 if((err = snd_pcm_hw_params_get_rate_min(This->hw_params, &min, NULL)) < 0){
797 WARN("Unable to get min rate: %d (%s)\n", err, snd_strerror(err));
801 if((err = snd_pcm_hw_params_get_rate_max(This->hw_params, &max, NULL)) < 0){
803 WARN("Unable to get max rate: %d (%s)\n", err, snd_strerror(err));
807 if(fmt->nSamplesPerSec < min || fmt->nSamplesPerSec > max ||
808 (fmt->nSamplesPerSec != 48000 &&
809 fmt->nSamplesPerSec != 41100 &&
810 fmt->nSamplesPerSec != 22050 &&
811 fmt->nSamplesPerSec != 11025 &&
812 fmt->nSamplesPerSec != 8000)){
813 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
817 if((err = snd_pcm_hw_params_get_channels_min(This->hw_params, &min)) < 0){
819 WARN("Unable to get min channels: %d (%s)\n", err, snd_strerror(err));
823 if((err = snd_pcm_hw_params_get_channels_max(This->hw_params, &max)) < 0){
825 WARN("Unable to get max channels: %d (%s)\n", err, snd_strerror(err));
830 if(fmt->nChannels > max){
832 closest->nChannels = max;
833 }else if(fmt->nChannels < min){
835 closest->nChannels = min;
838 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
839 DWORD mask = get_channel_mask(closest->nChannels);
841 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = mask;
843 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
844 fmtex->dwChannelMask != mask)
849 LeaveCriticalSection(&This->lock);
850 HeapFree(GetProcessHeap(), 0, formats);
852 if(hr == S_OK || !out){
853 HeapFree(GetProcessHeap(), 0, closest);
857 closest->nBlockAlign =
858 closest->nChannels * closest->wBitsPerSample / 8;
859 closest->nAvgBytesPerSec =
860 closest->nBlockAlign * closest->nSamplesPerSec;
864 TRACE("returning: %08x\n", hr);
868 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
871 ACImpl *This = impl_from_IAudioClient(iface);
872 WAVEFORMATEXTENSIBLE *fmt;
873 snd_pcm_format_mask_t *formats;
874 unsigned int max_rate, max_channels;
878 TRACE("(%p)->(%p)\n", This, pwfx);
883 *pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
885 return E_OUTOFMEMORY;
887 fmt = (WAVEFORMATEXTENSIBLE*)*pwfx;
889 formats = HeapAlloc(GetProcessHeap(), 0, snd_pcm_format_mask_sizeof());
891 HeapFree(GetProcessHeap(), 0, *pwfx);
892 return E_OUTOFMEMORY;
895 EnterCriticalSection(&This->lock);
897 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
898 WARN("Unable to get hw_params: %d (%s)\n", err, snd_strerror(err));
903 snd_pcm_hw_params_get_format_mask(This->hw_params, formats);
905 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
906 if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT_LE)){
907 fmt->Format.wBitsPerSample = 32;
908 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
909 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S16_LE)){
910 fmt->Format.wBitsPerSample = 16;
911 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
912 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_U8)){
913 fmt->Format.wBitsPerSample = 8;
914 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
915 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S32_LE)){
916 fmt->Format.wBitsPerSample = 32;
917 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
918 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S24_3LE)){
919 fmt->Format.wBitsPerSample = 24;
920 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
922 ERR("Didn't recognize any available ALSA formats\n");
927 if((err = snd_pcm_hw_params_get_channels_max(This->hw_params,
928 &max_channels)) < 0){
929 WARN("Unable to get max channels: %d (%s)\n", err, snd_strerror(err));
934 if(max_channels > 2){
935 FIXME("Don't know what to do with %u channels, pretending there's "
936 "only 2 channels\n", max_channels);
937 fmt->Format.nChannels = 2;
939 fmt->Format.nChannels = max_channels;
941 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
943 if((err = snd_pcm_hw_params_get_rate_max(This->hw_params, &max_rate,
945 WARN("Unable to get max rate: %d (%s)\n", err, snd_strerror(err));
950 if(max_rate >= 48000)
951 fmt->Format.nSamplesPerSec = 48000;
952 else if(max_rate >= 41100)
953 fmt->Format.nSamplesPerSec = 41100;
954 else if(max_rate >= 22050)
955 fmt->Format.nSamplesPerSec = 22050;
956 else if(max_rate >= 11025)
957 fmt->Format.nSamplesPerSec = 11025;
958 else if(max_rate >= 8000)
959 fmt->Format.nSamplesPerSec = 8000;
961 ERR("Unknown max rate: %u\n", max_rate);
966 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
967 fmt->Format.nChannels) / 8;
968 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
969 fmt->Format.nBlockAlign;
971 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
972 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
974 dump_fmt((WAVEFORMATEX*)fmt);
977 LeaveCriticalSection(&This->lock);
979 HeapFree(GetProcessHeap(), 0, *pwfx);
980 HeapFree(GetProcessHeap(), 0, formats);
985 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
986 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
988 ACImpl *This = impl_from_IAudioClient(iface);
990 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
992 if(!defperiod && !minperiod)
996 *defperiod = DefaultPeriod;
998 *minperiod = MinimumPeriod;
1003 static snd_pcm_sframes_t alsa_write_best_effort(snd_pcm_t *handle, BYTE *buf,
1004 snd_pcm_uframes_t frames)
1006 snd_pcm_sframes_t written;
1008 written = snd_pcm_writei(handle, buf, frames);
1012 if(written == -EAGAIN)
1016 WARN("writei failed, recovering: %ld (%s)\n", written,
1017 snd_strerror(written));
1019 ret = wine_snd_pcm_recover(handle, written, 0);
1021 WARN("Could not recover: %d (%s)\n", ret, snd_strerror(ret));
1025 written = snd_pcm_writei(handle, buf, frames);
1031 static void alsa_write_data(ACImpl *This)
1033 snd_pcm_sframes_t written;
1034 snd_pcm_uframes_t to_write;
1036 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1038 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1039 to_write = This->bufsize_frames - This->lcl_offs_frames;
1041 to_write = This->held_frames;
1043 written = alsa_write_best_effort(This->pcm_handle, buf, to_write);
1045 WARN("Couldn't write: %ld (%s)\n", written, snd_strerror(written));
1049 This->lcl_offs_frames += written;
1050 This->lcl_offs_frames %= This->bufsize_frames;
1051 This->held_frames -= written;
1053 if(written < to_write){
1054 /* ALSA buffer probably full */
1058 if(This->held_frames){
1059 /* wrapped and have some data back at the start to write */
1060 written = alsa_write_best_effort(This->pcm_handle, This->local_buffer,
1063 WARN("Couldn't write: %ld (%s)\n", written, snd_strerror(written));
1067 This->lcl_offs_frames += written;
1068 This->lcl_offs_frames %= This->bufsize_frames;
1069 This->held_frames -= written;
1073 static void alsa_read_data(ACImpl *This)
1075 snd_pcm_sframes_t pos, readable, nread;
1077 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1078 readable = This->bufsize_frames - pos;
1080 nread = snd_pcm_readi(This->pcm_handle,
1081 This->local_buffer + pos * This->fmt->nBlockAlign, readable);
1085 WARN("read failed, recovering: %ld (%s)\n", nread, snd_strerror(nread));
1087 ret = wine_snd_pcm_recover(This->pcm_handle, nread, 0);
1089 WARN("Recover failed: %d (%s)\n", ret, snd_strerror(ret));
1093 nread = snd_pcm_readi(This->pcm_handle,
1094 This->local_buffer + pos * This->fmt->nBlockAlign, readable);
1096 WARN("read failed: %ld (%s)\n", nread, snd_strerror(nread));
1101 This->held_frames += nread;
1103 if(This->held_frames > This->bufsize_frames){
1104 WARN("Overflow of unread data\n");
1105 This->lcl_offs_frames += This->held_frames;
1106 This->lcl_offs_frames %= This->bufsize_frames;
1107 This->held_frames = This->bufsize_frames;
1111 static void CALLBACK alsa_push_buffer_data(void *user, BOOLEAN timer)
1113 ACImpl *This = user;
1115 EnterCriticalSection(&This->lock);
1117 if(This->dataflow == eRender && This->held_frames)
1118 alsa_write_data(This);
1119 else if(This->dataflow == eCapture)
1120 alsa_read_data(This);
1123 SetEvent(This->event);
1125 LeaveCriticalSection(&This->lock);
1128 static HRESULT alsa_consider_start(ACImpl *This)
1130 snd_pcm_sframes_t avail;
1133 avail = snd_pcm_avail_update(This->pcm_handle);
1135 WARN("Unable to get avail_update: %ld (%s)\n", avail,
1136 snd_strerror(avail));
1140 if(This->period_alsa < This->bufsize_alsa - avail){
1141 if((err = snd_pcm_start(This->pcm_handle)) < 0){
1142 WARN("Start failed: %d (%s), state: %d\n", err, snd_strerror(err),
1143 snd_pcm_state(This->pcm_handle));
1153 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1155 ACImpl *This = impl_from_IAudioClient(iface);
1159 TRACE("(%p)\n", This);
1161 EnterCriticalSection(&This->lock);
1164 LeaveCriticalSection(&This->lock);
1165 return AUDCLNT_E_NOT_INITIALIZED;
1168 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1169 LeaveCriticalSection(&This->lock);
1170 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1174 LeaveCriticalSection(&This->lock);
1175 return AUDCLNT_E_NOT_STOPPED;
1178 hr = alsa_consider_start(This);
1180 LeaveCriticalSection(&This->lock);
1184 period_ms = This->period_us / 1000;
1188 if(This->dataflow == eCapture){
1189 /* dump any data that might be leftover in the ALSA capture buffer */
1190 snd_pcm_readi(This->pcm_handle, This->local_buffer,
1191 This->bufsize_frames);
1194 if(!CreateTimerQueueTimer(&This->timer, g_timer_q, alsa_push_buffer_data,
1195 This, 0, period_ms, WT_EXECUTEINTIMERTHREAD)){
1196 LeaveCriticalSection(&This->lock);
1197 WARN("Unable to create timer: %u\n", GetLastError());
1201 This->started = TRUE;
1203 LeaveCriticalSection(&This->lock);
1208 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1210 ACImpl *This = impl_from_IAudioClient(iface);
1213 TRACE("(%p)\n", This);
1215 EnterCriticalSection(&This->lock);
1218 LeaveCriticalSection(&This->lock);
1219 return AUDCLNT_E_NOT_INITIALIZED;
1223 LeaveCriticalSection(&This->lock);
1227 DeleteTimerQueueTimer(g_timer_q, This->timer, INVALID_HANDLE_VALUE);
1229 if((err = snd_pcm_drop(This->pcm_handle)) < 0){
1230 LeaveCriticalSection(&This->lock);
1231 WARN("Drop failed: %d (%s)\n", err, snd_strerror(err));
1235 if((err = snd_pcm_prepare(This->pcm_handle)) < 0){
1236 LeaveCriticalSection(&This->lock);
1237 WARN("Prepare failed: %d (%s)\n", err, snd_strerror(err));
1241 This->started = FALSE;
1243 LeaveCriticalSection(&This->lock);
1248 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1250 ACImpl *This = impl_from_IAudioClient(iface);
1252 TRACE("(%p)\n", This);
1254 EnterCriticalSection(&This->lock);
1257 LeaveCriticalSection(&This->lock);
1258 return AUDCLNT_E_NOT_INITIALIZED;
1262 LeaveCriticalSection(&This->lock);
1263 return AUDCLNT_E_NOT_STOPPED;
1266 This->held_frames = 0;
1267 This->written_frames = 0;
1268 This->lcl_offs_frames = 0;
1270 LeaveCriticalSection(&This->lock);
1275 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1278 ACImpl *This = impl_from_IAudioClient(iface);
1280 TRACE("(%p)->(%p)\n", This, event);
1283 return E_INVALIDARG;
1285 EnterCriticalSection(&This->lock);
1288 LeaveCriticalSection(&This->lock);
1289 return AUDCLNT_E_NOT_INITIALIZED;
1292 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1293 LeaveCriticalSection(&This->lock);
1294 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1297 This->event = event;
1299 LeaveCriticalSection(&This->lock);
1304 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1307 ACImpl *This = impl_from_IAudioClient(iface);
1309 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1315 EnterCriticalSection(&This->lock);
1318 LeaveCriticalSection(&This->lock);
1319 return AUDCLNT_E_NOT_INITIALIZED;
1322 LeaveCriticalSection(&This->lock);
1324 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1325 if(This->dataflow != eRender)
1326 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1327 *ppv = &This->IAudioRenderClient_iface;
1328 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1329 if(This->dataflow != eCapture)
1330 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1331 *ppv = &This->IAudioCaptureClient_iface;
1332 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1333 *ppv = &This->IAudioSessionControl2_iface;
1334 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1335 *ppv = &This->ISimpleAudioVolume_iface;
1336 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1337 *ppv = &This->IAudioClock_iface;
1341 IUnknown_AddRef((IUnknown*)*ppv);
1345 FIXME("stub %s\n", debugstr_guid(riid));
1346 return E_NOINTERFACE;
1349 static const IAudioClientVtbl AudioClient_Vtbl =
1351 AudioClient_QueryInterface,
1353 AudioClient_Release,
1354 AudioClient_Initialize,
1355 AudioClient_GetBufferSize,
1356 AudioClient_GetStreamLatency,
1357 AudioClient_GetCurrentPadding,
1358 AudioClient_IsFormatSupported,
1359 AudioClient_GetMixFormat,
1360 AudioClient_GetDevicePeriod,
1364 AudioClient_SetEventHandle,
1365 AudioClient_GetService
1368 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1369 IAudioRenderClient *iface, REFIID riid, void **ppv)
1371 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1377 if(IsEqualIID(riid, &IID_IUnknown) ||
1378 IsEqualIID(riid, &IID_IAudioRenderClient))
1381 IUnknown_AddRef((IUnknown*)*ppv);
1385 WARN("Unknown interface %s\n", debugstr_guid(riid));
1386 return E_NOINTERFACE;
1389 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1391 ACImpl *This = impl_from_IAudioRenderClient(iface);
1392 return AudioClient_AddRef(&This->IAudioClient_iface);
1395 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1397 ACImpl *This = impl_from_IAudioRenderClient(iface);
1398 return AudioClient_Release(&This->IAudioClient_iface);
1401 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1402 UINT32 frames, BYTE **data)
1404 ACImpl *This = impl_from_IAudioRenderClient(iface);
1409 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1414 EnterCriticalSection(&This->lock);
1416 if(This->buf_state != NOT_LOCKED){
1417 LeaveCriticalSection(&This->lock);
1418 return AUDCLNT_E_OUT_OF_ORDER;
1422 This->buf_state = LOCKED_NORMAL;
1423 LeaveCriticalSection(&This->lock);
1427 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1429 LeaveCriticalSection(&This->lock);
1433 if(pad + frames > This->bufsize_frames){
1434 LeaveCriticalSection(&This->lock);
1435 return AUDCLNT_E_BUFFER_TOO_LARGE;
1439 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1440 if(write_pos + frames > This->bufsize_frames){
1441 if(This->tmp_buffer_frames < frames){
1442 if(This->tmp_buffer)
1443 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1444 This->tmp_buffer, frames * This->fmt->nBlockAlign);
1446 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1447 frames * This->fmt->nBlockAlign);
1448 if(!This->tmp_buffer){
1449 LeaveCriticalSection(&This->lock);
1450 return E_OUTOFMEMORY;
1452 This->tmp_buffer_frames = frames;
1454 *data = This->tmp_buffer;
1455 This->buf_state = LOCKED_WRAPPED;
1457 *data = This->local_buffer +
1458 This->lcl_offs_frames * This->fmt->nBlockAlign;
1459 This->buf_state = LOCKED_NORMAL;
1462 LeaveCriticalSection(&This->lock);
1467 static void alsa_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_bytes)
1469 snd_pcm_uframes_t write_offs_frames =
1470 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1471 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
1472 snd_pcm_uframes_t chunk_frames = This->bufsize_frames - write_offs_frames;
1473 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
1475 if(written_bytes < chunk_bytes){
1476 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
1478 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
1479 memcpy(This->local_buffer, buffer + chunk_bytes,
1480 written_bytes - chunk_bytes);
1484 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1485 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
1487 ACImpl *This = impl_from_IAudioRenderClient(iface);
1488 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
1492 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
1494 EnterCriticalSection(&This->lock);
1496 if(This->buf_state == NOT_LOCKED || !written_frames){
1497 This->buf_state = NOT_LOCKED;
1498 LeaveCriticalSection(&This->lock);
1499 return written_frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK;
1502 if(This->buf_state == LOCKED_NORMAL)
1503 buffer = This->local_buffer +
1504 This->lcl_offs_frames * This->fmt->nBlockAlign;
1506 buffer = This->tmp_buffer;
1508 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1509 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1510 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1511 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1512 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1513 This->fmt->wBitsPerSample == 8)
1514 memset(buffer, 128, written_frames * This->fmt->nBlockAlign);
1516 memset(buffer, 0, written_frames * This->fmt->nBlockAlign);
1519 if(This->held_frames){
1520 if(This->buf_state == LOCKED_WRAPPED)
1521 alsa_wrap_buffer(This, buffer, written_bytes);
1523 This->held_frames += written_frames;
1525 snd_pcm_sframes_t written;
1527 written = alsa_write_best_effort(This->pcm_handle, buffer,
1530 LeaveCriticalSection(&This->lock);
1531 WARN("write failed: %ld (%s)\n", written, snd_strerror(written));
1535 if(written < written_frames){
1536 if(This->buf_state == LOCKED_WRAPPED)
1537 alsa_wrap_buffer(This,
1538 This->tmp_buffer + written * This->fmt->nBlockAlign,
1539 written_frames - written);
1541 This->held_frames = written_frames - written;
1546 snd_pcm_state(This->pcm_handle) == SND_PCM_STATE_PREPARED){
1547 hr = alsa_consider_start(This);
1549 LeaveCriticalSection(&This->lock);
1554 This->written_frames += written_frames;
1555 This->buf_state = NOT_LOCKED;
1557 LeaveCriticalSection(&This->lock);
1562 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1563 AudioRenderClient_QueryInterface,
1564 AudioRenderClient_AddRef,
1565 AudioRenderClient_Release,
1566 AudioRenderClient_GetBuffer,
1567 AudioRenderClient_ReleaseBuffer
1570 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1571 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1573 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1579 if(IsEqualIID(riid, &IID_IUnknown) ||
1580 IsEqualIID(riid, &IID_IAudioCaptureClient))
1583 IUnknown_AddRef((IUnknown*)*ppv);
1587 WARN("Unknown interface %s\n", debugstr_guid(riid));
1588 return E_NOINTERFACE;
1591 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1593 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1594 return IAudioClient_AddRef(&This->IAudioClient_iface);
1597 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1599 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1600 return IAudioClient_Release(&This->IAudioClient_iface);
1603 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1604 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1607 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1610 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1613 if(!data || !frames || !flags)
1616 EnterCriticalSection(&This->lock);
1618 if(This->buf_state != NOT_LOCKED){
1619 LeaveCriticalSection(&This->lock);
1620 return AUDCLNT_E_OUT_OF_ORDER;
1623 hr = IAudioCaptureClient_GetNextPacketSize(iface, frames);
1625 LeaveCriticalSection(&This->lock);
1631 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
1632 UINT32 chunk_bytes, offs_bytes, frames_bytes;
1633 if(This->tmp_buffer_frames < *frames){
1634 if(This->tmp_buffer)
1635 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1636 This->tmp_buffer, *frames * This->fmt->nBlockAlign);
1638 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1639 *frames * This->fmt->nBlockAlign);
1640 if(!This->tmp_buffer){
1641 LeaveCriticalSection(&This->lock);
1642 return E_OUTOFMEMORY;
1644 This->tmp_buffer_frames = *frames;
1647 *data = This->tmp_buffer;
1648 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
1649 This->fmt->nBlockAlign;
1650 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
1651 frames_bytes = *frames * This->fmt->nBlockAlign;
1652 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
1653 memcpy(This->tmp_buffer + chunk_bytes, This->local_buffer,
1654 frames_bytes - chunk_bytes);
1656 *data = This->local_buffer +
1657 This->lcl_offs_frames * This->fmt->nBlockAlign;
1659 This->buf_state = LOCKED_NORMAL;
1661 if(devpos || qpcpos)
1662 IAudioClock_GetPosition(&This->IAudioClock_iface, devpos, qpcpos);
1664 LeaveCriticalSection(&This->lock);
1666 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1669 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1670 IAudioCaptureClient *iface, UINT32 done)
1672 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1674 TRACE("(%p)->(%u)\n", This, done);
1676 EnterCriticalSection(&This->lock);
1678 if(This->buf_state == NOT_LOCKED){
1679 LeaveCriticalSection(&This->lock);
1680 return AUDCLNT_E_OUT_OF_ORDER;
1683 This->held_frames -= done;
1684 This->lcl_offs_frames += done;
1685 This->lcl_offs_frames %= This->bufsize_frames;
1687 This->buf_state = NOT_LOCKED;
1689 LeaveCriticalSection(&This->lock);
1694 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1695 IAudioCaptureClient *iface, UINT32 *frames)
1697 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1699 TRACE("(%p)->(%p)\n", This, frames);
1701 return AudioClient_GetCurrentPadding(&This->IAudioClient_iface, frames);
1704 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1706 AudioCaptureClient_QueryInterface,
1707 AudioCaptureClient_AddRef,
1708 AudioCaptureClient_Release,
1709 AudioCaptureClient_GetBuffer,
1710 AudioCaptureClient_ReleaseBuffer,
1711 AudioCaptureClient_GetNextPacketSize
1714 static HRESULT WINAPI AudioSessionControl_QueryInterface(
1715 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
1717 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1723 if(IsEqualIID(riid, &IID_IUnknown) ||
1724 IsEqualIID(riid, &IID_IAudioSessionControl) ||
1725 IsEqualIID(riid, &IID_IAudioSessionControl2))
1728 IUnknown_AddRef((IUnknown*)*ppv);
1732 WARN("Unknown interface %s\n", debugstr_guid(riid));
1733 return E_NOINTERFACE;
1736 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
1738 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1739 return IAudioClient_AddRef(&This->IAudioClient_iface);
1742 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
1744 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1745 return IAudioClient_Release(&This->IAudioClient_iface);
1748 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
1749 AudioSessionState *state)
1751 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1753 FIXME("(%p)->(%p) - stub\n", This, state);
1761 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
1762 IAudioSessionControl2 *iface, WCHAR **name)
1764 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1766 FIXME("(%p)->(%p) - stub\n", This, name);
1771 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
1772 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
1774 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1776 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
1781 static HRESULT WINAPI AudioSessionControl_GetIconPath(
1782 IAudioSessionControl2 *iface, WCHAR **path)
1784 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1786 FIXME("(%p)->(%p) - stub\n", This, path);
1791 static HRESULT WINAPI AudioSessionControl_SetIconPath(
1792 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
1794 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1796 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
1801 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
1802 IAudioSessionControl2 *iface, GUID *group)
1804 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1806 FIXME("(%p)->(%p) - stub\n", This, group);
1811 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
1812 IAudioSessionControl2 *iface, GUID *group, const GUID *session)
1814 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1816 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
1817 debugstr_guid(session));
1822 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
1823 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
1825 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1827 FIXME("(%p)->(%p) - stub\n", This, events);
1832 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
1833 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
1835 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1837 FIXME("(%p)->(%p) - stub\n", This, events);
1842 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
1843 IAudioSessionControl2 *iface, WCHAR **id)
1845 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1847 FIXME("(%p)->(%p) - stub\n", This, id);
1852 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
1853 IAudioSessionControl2 *iface, WCHAR **id)
1855 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1857 FIXME("(%p)->(%p) - stub\n", This, id);
1862 static HRESULT WINAPI AudioSessionControl_GetProcessId(
1863 IAudioSessionControl2 *iface, DWORD *pid)
1865 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1867 TRACE("(%p)->(%p)\n", This, pid);
1872 *pid = GetCurrentProcessId();
1877 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
1878 IAudioSessionControl2 *iface)
1880 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1882 TRACE("(%p)\n", This);
1887 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
1888 IAudioSessionControl2 *iface, BOOL optout)
1890 ACImpl *This = impl_from_IAudioSessionControl2(iface);
1892 TRACE("(%p)->(%d)\n", This, optout);
1897 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
1899 AudioSessionControl_QueryInterface,
1900 AudioSessionControl_AddRef,
1901 AudioSessionControl_Release,
1902 AudioSessionControl_GetState,
1903 AudioSessionControl_GetDisplayName,
1904 AudioSessionControl_SetDisplayName,
1905 AudioSessionControl_GetIconPath,
1906 AudioSessionControl_SetIconPath,
1907 AudioSessionControl_GetGroupingParam,
1908 AudioSessionControl_SetGroupingParam,
1909 AudioSessionControl_RegisterAudioSessionNotification,
1910 AudioSessionControl_UnregisterAudioSessionNotification,
1911 AudioSessionControl_GetSessionIdentifier,
1912 AudioSessionControl_GetSessionInstanceIdentifier,
1913 AudioSessionControl_GetProcessId,
1914 AudioSessionControl_IsSystemSoundsSession,
1915 AudioSessionControl_SetDuckingPreference
1918 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
1919 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
1921 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1927 if(IsEqualIID(riid, &IID_IUnknown) ||
1928 IsEqualIID(riid, &IID_ISimpleAudioVolume))
1931 IUnknown_AddRef((IUnknown*)*ppv);
1935 WARN("Unknown interface %s\n", debugstr_guid(riid));
1936 return E_NOINTERFACE;
1939 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
1941 ACImpl *This = impl_from_ISimpleAudioVolume(iface);
1942 return IAudioClient_AddRef(&This->IAudioClient_iface);
1945 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
1947 ACImpl *This = impl_from_ISimpleAudioVolume(iface);
1948 return IAudioClient_Release(&This->IAudioClient_iface);
1951 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
1952 ISimpleAudioVolume *iface, float level, const GUID *context)
1954 ACImpl *This = impl_from_ISimpleAudioVolume(iface);
1956 FIXME("(%p)->(%f, %p) - stub\n", This, level, context);
1961 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
1962 ISimpleAudioVolume *iface, float *level)
1964 ACImpl *This = impl_from_ISimpleAudioVolume(iface);
1966 FIXME("(%p)->(%p) - stub\n", This, level);
1971 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
1972 BOOL mute, const GUID *context)
1974 ACImpl *This = impl_from_ISimpleAudioVolume(iface);
1976 FIXME("(%p)->(%u, %p) - stub\n", This, mute, context);
1981 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
1984 ACImpl *This = impl_from_ISimpleAudioVolume(iface);
1986 FIXME("(%p)->(%p) - stub\n", This, mute);
1991 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
1993 SimpleAudioVolume_QueryInterface,
1994 SimpleAudioVolume_AddRef,
1995 SimpleAudioVolume_Release,
1996 SimpleAudioVolume_SetMasterVolume,
1997 SimpleAudioVolume_GetMasterVolume,
1998 SimpleAudioVolume_SetMute,
1999 SimpleAudioVolume_GetMute
2002 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2003 REFIID riid, void **ppv)
2005 ACImpl *This = impl_from_IAudioClock(iface);
2007 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2013 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2015 else if(IsEqualIID(riid, &IID_IAudioClock2))
2016 *ppv = &This->IAudioClock2_iface;
2018 IUnknown_AddRef((IUnknown*)*ppv);
2022 WARN("Unknown interface %s\n", debugstr_guid(riid));
2023 return E_NOINTERFACE;
2026 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2028 ACImpl *This = impl_from_IAudioClock(iface);
2029 return IAudioClient_AddRef(&This->IAudioClient_iface);
2032 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2034 ACImpl *This = impl_from_IAudioClock(iface);
2035 return IAudioClient_Release(&This->IAudioClient_iface);
2038 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2040 ACImpl *This = impl_from_IAudioClock(iface);
2042 TRACE("(%p)->(%p)\n", This, freq);
2044 *freq = This->fmt->nSamplesPerSec;
2049 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2052 ACImpl *This = impl_from_IAudioClock(iface);
2056 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2061 EnterCriticalSection(&This->lock);
2063 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
2065 LeaveCriticalSection(&This->lock);
2069 if(This->dataflow == eRender)
2070 *pos = This->written_frames - pad;
2071 else if(This->dataflow == eCapture)
2072 *pos = This->written_frames + pad;
2074 LeaveCriticalSection(&This->lock);
2077 LARGE_INTEGER stamp, freq;
2078 QueryPerformanceCounter(&stamp);
2079 QueryPerformanceFrequency(&freq);
2080 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2086 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2089 ACImpl *This = impl_from_IAudioClock(iface);
2091 TRACE("(%p)->(%p)\n", This, chars);
2096 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2101 static const IAudioClockVtbl AudioClock_Vtbl =
2103 AudioClock_QueryInterface,
2106 AudioClock_GetFrequency,
2107 AudioClock_GetPosition,
2108 AudioClock_GetCharacteristics
2111 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2112 REFIID riid, void **ppv)
2114 ACImpl *This = impl_from_IAudioClock2(iface);
2115 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2118 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2120 ACImpl *This = impl_from_IAudioClock2(iface);
2121 return IAudioClient_AddRef(&This->IAudioClient_iface);
2124 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2126 ACImpl *This = impl_from_IAudioClock2(iface);
2127 return IAudioClient_Release(&This->IAudioClient_iface);
2130 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2131 UINT64 *pos, UINT64 *qpctime)
2133 ACImpl *This = impl_from_IAudioClock2(iface);
2135 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2140 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2142 AudioClock2_QueryInterface,
2144 AudioClock2_Release,
2145 AudioClock2_GetDevicePosition