winealsa.drv: Tell ALSA to play silence during underruns.
[wine.git] / dlls / winealsa.drv / mmdevdrv.c
blobded6b2a4c96c01937e0a5e85f0d21459613ca27a
1 /*
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
21 #define COBJMACROS
22 #include "config.h"
24 #include <stdarg.h>
25 #include <math.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "winreg.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "wine/list.h"
35 #include "ole2.h"
36 #include "mmdeviceapi.h"
37 #include "devpkey.h"
38 #include "dshow.h"
39 #include "dsound.h"
40 #include "endpointvolume.h"
42 #include "initguid.h"
43 #include "audioclient.h"
44 #include "audiopolicy.h"
45 #include "dsdriver.h"
47 #include <alsa/asoundlib.h>
49 WINE_DEFAULT_DEBUG_CHANNEL(alsa);
51 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
53 static const REFERENCE_TIME DefaultPeriod = 200000;
54 static const REFERENCE_TIME MinimumPeriod = 100000;
56 struct ACImpl;
57 typedef struct ACImpl ACImpl;
59 typedef struct _AudioSession {
60 GUID guid;
61 struct list clients;
63 IMMDevice *device;
65 float master_vol;
66 UINT32 channel_count;
67 float *channel_vols;
68 BOOL mute;
70 CRITICAL_SECTION lock;
72 struct list entry;
73 } AudioSession;
75 typedef struct _AudioSessionWrapper {
76 IAudioSessionControl2 IAudioSessionControl2_iface;
77 IChannelAudioVolume IChannelAudioVolume_iface;
78 ISimpleAudioVolume ISimpleAudioVolume_iface;
80 LONG ref;
82 ACImpl *client;
83 AudioSession *session;
84 } AudioSessionWrapper;
86 struct ACImpl {
87 IAudioClient IAudioClient_iface;
88 IAudioRenderClient IAudioRenderClient_iface;
89 IAudioCaptureClient IAudioCaptureClient_iface;
90 IAudioClock IAudioClock_iface;
91 IAudioClock2 IAudioClock2_iface;
92 IAudioStreamVolume IAudioStreamVolume_iface;
94 LONG ref;
96 snd_pcm_t *pcm_handle;
97 snd_pcm_uframes_t period_alsa, bufsize_alsa;
98 snd_pcm_hw_params_t *hw_params; /* does not hold state between calls */
99 snd_pcm_format_t alsa_format;
101 IMMDevice *parent;
103 EDataFlow dataflow;
104 WAVEFORMATEX *fmt;
105 DWORD flags;
106 AUDCLNT_SHAREMODE share;
107 HANDLE event;
108 float *vols;
110 BOOL initted, started;
111 UINT64 written_frames, held_frames, tmp_buffer_frames;
112 UINT32 bufsize_frames, period_us;
113 UINT32 lcl_offs_frames; /* offs into local_buffer where valid data starts */
115 HANDLE timer;
116 BYTE *local_buffer, *tmp_buffer;
117 int buf_state;
119 CRITICAL_SECTION lock;
121 AudioSession *session;
122 AudioSessionWrapper *session_wrapper;
124 struct list entry;
127 enum BufferStates {
128 NOT_LOCKED = 0,
129 LOCKED_NORMAL, /* public buffer piece is from local_buffer */
130 LOCKED_WRAPPED /* public buffer piece is wrapped around, in tmp_buffer */
133 typedef struct _SessionMgr {
134 IAudioSessionManager2 IAudioSessionManager2_iface;
136 LONG ref;
138 IMMDevice *device;
139 } SessionMgr;
141 static HANDLE g_timer_q;
143 static CRITICAL_SECTION g_sessions_lock;
144 static struct list g_sessions = LIST_INIT(g_sessions);
146 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
147 static const char defname[] = "default";
149 static const IAudioClientVtbl AudioClient_Vtbl;
150 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
151 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
152 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
153 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
154 static const IAudioClockVtbl AudioClock_Vtbl;
155 static const IAudioClock2Vtbl AudioClock2_Vtbl;
156 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
157 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
158 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl;
160 int wine_snd_pcm_recover(snd_pcm_t *pcm, int err, int silent);
161 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
163 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
165 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
168 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
170 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
173 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
175 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
178 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
180 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
183 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
185 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
188 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
190 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
193 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
195 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
198 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
200 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
203 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
205 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
208 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
210 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
213 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
215 if(reason == DLL_PROCESS_ATTACH){
216 g_timer_q = CreateTimerQueue();
217 if(!g_timer_q)
218 return FALSE;
220 InitializeCriticalSection(&g_sessions_lock);
223 return TRUE;
226 static HRESULT alsa_get_card_devices(EDataFlow flow, WCHAR **ids, char **keys,
227 UINT *num, snd_ctl_t *ctl, int card, const WCHAR *cardnameW)
229 static const WCHAR dashW[] = {' ','-',' ',0};
230 int err, device;
231 snd_pcm_info_t *info;
232 snd_pcm_stream_t stream = (flow == eRender ? SND_PCM_STREAM_PLAYBACK :
233 SND_PCM_STREAM_CAPTURE);
235 info = HeapAlloc(GetProcessHeap(), 0, snd_pcm_info_sizeof());
236 if(!info)
237 return E_OUTOFMEMORY;
239 snd_pcm_info_set_subdevice(info, 0);
240 snd_pcm_info_set_stream(info, stream);
242 device = -1;
243 for(err = snd_ctl_pcm_next_device(ctl, &device); device != -1 && err >= 0;
244 err = snd_ctl_pcm_next_device(ctl, &device)){
245 const char *devname;
246 char devnode[32];
247 snd_pcm_t *handle;
249 snd_pcm_info_set_device(info, device);
251 if((err = snd_ctl_pcm_info(ctl, info)) < 0){
252 if(err == -ENOENT)
253 /* This device doesn't have the right stream direction */
254 continue;
256 WARN("Failed to get info for card %d, device %d: %d (%s)\n",
257 card, device, err, snd_strerror(err));
258 continue;
261 sprintf(devnode, "hw:%d,%d", card, device);
262 if((err = snd_pcm_open(&handle, devnode, stream, SND_PCM_NONBLOCK)) < 0){
263 WARN("The device \"%s\" failed to open, pretending it doesn't exist: %d (%s)\n",
264 devnode, err, snd_strerror(err));
265 continue;
268 snd_pcm_close(handle);
270 if(ids && keys){
271 DWORD len, cardlen;
273 devname = snd_pcm_info_get_name(info);
274 if(!devname){
275 WARN("Unable to get device name for card %d, device %d\n", card,
276 device);
277 continue;
280 cardlen = lstrlenW(cardnameW);
281 len = MultiByteToWideChar(CP_UNIXCP, 0, devname, -1, NULL, 0);
282 len += lstrlenW(dashW);
283 len += cardlen;
284 ids[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
285 if(!ids[*num]){
286 HeapFree(GetProcessHeap(), 0, info);
287 return E_OUTOFMEMORY;
289 memcpy(ids[*num], cardnameW, cardlen * sizeof(WCHAR));
290 memcpy(ids[*num] + cardlen, dashW, lstrlenW(dashW) * sizeof(WCHAR));
291 cardlen += lstrlenW(dashW);
292 MultiByteToWideChar(CP_UNIXCP, 0, devname, -1, ids[*num] + cardlen,
293 len - cardlen);
295 keys[*num] = HeapAlloc(GetProcessHeap(), 0, 32);
296 if(!keys[*num]){
297 HeapFree(GetProcessHeap(), 0, info);
298 HeapFree(GetProcessHeap(), 0, ids[*num]);
299 return E_OUTOFMEMORY;
301 memcpy(keys[*num], devnode, sizeof(devnode));
304 ++(*num);
307 HeapFree(GetProcessHeap(), 0, info);
309 if(err != 0)
310 WARN("Got a failure during device enumeration on card %d: %d (%s)\n",
311 card, err, snd_strerror(err));
313 return S_OK;
316 static HRESULT alsa_enum_devices(EDataFlow flow, WCHAR **ids, char **keys,
317 UINT *num)
319 int err, card;
321 card = -1;
322 *num = 0;
323 for(err = snd_card_next(&card); card != -1 && err >= 0;
324 err = snd_card_next(&card)){
325 char cardpath[64];
326 const char *cardname;
327 WCHAR *cardnameW;
328 snd_ctl_t *ctl;
329 DWORD len;
331 sprintf(cardpath, "hw:%u", card);
333 if((err = snd_ctl_open(&ctl, cardpath, 0)) < 0){
334 WARN("Unable to open ctl for ALSA device %s: %d (%s)\n", cardpath,
335 err, snd_strerror(err));
336 continue;
339 if((err = snd_card_get_name(card, (char **)&cardname)) < 0){
340 WARN("Unable to get card name for ALSA device %s: %d (%s)\n",
341 cardpath, err, snd_strerror(err));
342 /* FIXME: Should be localized */
343 cardname = "Unknown soundcard";
346 len = MultiByteToWideChar(CP_UNIXCP, 0, cardname, -1, NULL, 0);
347 cardnameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
348 if(!cardnameW){
349 snd_ctl_close(ctl);
350 return E_OUTOFMEMORY;
352 MultiByteToWideChar(CP_UNIXCP, 0, cardname, -1, cardnameW, len);
354 alsa_get_card_devices(flow, ids, keys, num, ctl, card, cardnameW);
356 HeapFree(GetProcessHeap(), 0, cardnameW);
358 snd_ctl_close(ctl);
361 if(err != 0)
362 WARN("Got a failure during card enumeration: %d (%s)\n",
363 err, snd_strerror(err));
365 return S_OK;
368 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, char ***keys,
369 UINT *num, UINT *def_index)
371 HRESULT hr;
373 TRACE("%d %p %p %p %p\n", flow, ids, keys, num, def_index);
375 hr = alsa_enum_devices(flow, NULL, NULL, num);
376 if(FAILED(hr))
377 return hr;
379 *ids = HeapAlloc(GetProcessHeap(), 0, (*num + 1) * sizeof(WCHAR *));
380 *keys = HeapAlloc(GetProcessHeap(), 0, (*num + 1) * sizeof(char *));
381 if(!*ids || !*keys){
382 HeapFree(GetProcessHeap(), 0, *ids);
383 HeapFree(GetProcessHeap(), 0, *keys);
384 return E_OUTOFMEMORY;
387 (*ids)[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW));
388 memcpy((*ids)[0], defaultW, sizeof(defaultW));
389 (*keys)[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(defname));
390 memcpy((*keys)[0], defname, sizeof(defname));
391 *def_index = 0;
393 hr = alsa_enum_devices(flow, (*ids) + 1, (*keys) + 1, num);
394 if(FAILED(hr)){
395 int i;
396 for(i = 0; i < *num; ++i){
397 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
398 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
400 HeapFree(GetProcessHeap(), 0, *ids);
401 HeapFree(GetProcessHeap(), 0, *keys);
402 return E_OUTOFMEMORY;
405 ++(*num); /* for default device */
407 return S_OK;
410 HRESULT WINAPI AUDDRV_GetAudioEndpoint(const char *key, IMMDevice *dev,
411 EDataFlow dataflow, IAudioClient **out)
413 ACImpl *This;
414 int err;
415 snd_pcm_stream_t stream;
417 TRACE("\"%s\" %p %d %p\n", key, dev, dataflow, out);
419 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
420 if(!This)
421 return E_OUTOFMEMORY;
423 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
424 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
425 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
426 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
427 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
428 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
430 if(dataflow == eRender)
431 stream = SND_PCM_STREAM_PLAYBACK;
432 else if(dataflow == eCapture)
433 stream = SND_PCM_STREAM_CAPTURE;
434 else{
435 HeapFree(GetProcessHeap(), 0, This);
436 return E_UNEXPECTED;
439 This->dataflow = dataflow;
440 if((err = snd_pcm_open(&This->pcm_handle, key, stream,
441 SND_PCM_NONBLOCK)) < 0){
442 HeapFree(GetProcessHeap(), 0, This);
443 WARN("Unable to open PCM \"%s\": %d (%s)\n", key, err,
444 snd_strerror(err));
445 return E_FAIL;
448 This->hw_params = HeapAlloc(GetProcessHeap(), 0,
449 snd_pcm_hw_params_sizeof());
450 if(!This->hw_params){
451 HeapFree(GetProcessHeap(), 0, This);
452 snd_pcm_close(This->pcm_handle);
453 return E_OUTOFMEMORY;
456 InitializeCriticalSection(&This->lock);
458 This->parent = dev;
459 IMMDevice_AddRef(This->parent);
461 *out = &This->IAudioClient_iface;
462 IAudioClient_AddRef(&This->IAudioClient_iface);
464 return S_OK;
467 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
468 REFIID riid, void **ppv)
470 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
472 if(!ppv)
473 return E_POINTER;
474 *ppv = NULL;
475 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
476 *ppv = iface;
477 if(*ppv){
478 IUnknown_AddRef((IUnknown*)*ppv);
479 return S_OK;
481 WARN("Unknown interface %s\n", debugstr_guid(riid));
482 return E_NOINTERFACE;
485 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
487 ACImpl *This = impl_from_IAudioClient(iface);
488 ULONG ref;
489 ref = InterlockedIncrement(&This->ref);
490 TRACE("(%p) Refcount now %u\n", This, ref);
491 return ref;
494 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
496 ACImpl *This = impl_from_IAudioClient(iface);
497 ULONG ref;
498 ref = InterlockedDecrement(&This->ref);
499 TRACE("(%p) Refcount now %u\n", This, ref);
500 if(!ref){
501 IAudioClient_Stop(iface);
502 IMMDevice_Release(This->parent);
503 DeleteCriticalSection(&This->lock);
504 snd_pcm_drop(This->pcm_handle);
505 snd_pcm_close(This->pcm_handle);
506 if(This->initted){
507 EnterCriticalSection(&g_sessions_lock);
508 list_remove(&This->entry);
509 LeaveCriticalSection(&g_sessions_lock);
511 HeapFree(GetProcessHeap(), 0, This->vols);
512 HeapFree(GetProcessHeap(), 0, This->local_buffer);
513 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
514 HeapFree(GetProcessHeap(), 0, This->hw_params);
515 CoTaskMemFree(This->fmt);
516 HeapFree(GetProcessHeap(), 0, This);
518 return ref;
521 static void dump_fmt(const WAVEFORMATEX *fmt)
523 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
524 switch(fmt->wFormatTag){
525 case WAVE_FORMAT_PCM:
526 TRACE("WAVE_FORMAT_PCM");
527 break;
528 case WAVE_FORMAT_IEEE_FLOAT:
529 TRACE("WAVE_FORMAT_IEEE_FLOAT");
530 break;
531 case WAVE_FORMAT_EXTENSIBLE:
532 TRACE("WAVE_FORMAT_EXTENSIBLE");
533 break;
534 default:
535 TRACE("Unknown");
536 break;
538 TRACE(")\n");
540 TRACE("nChannels: %u\n", fmt->nChannels);
541 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
542 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
543 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
544 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
545 TRACE("cbSize: %u\n", fmt->cbSize);
547 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
548 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
549 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
550 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
551 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
555 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
557 WAVEFORMATEX *ret;
558 size_t size;
560 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
561 size = sizeof(WAVEFORMATEXTENSIBLE);
562 else
563 size = sizeof(WAVEFORMATEX);
565 ret = CoTaskMemAlloc(size);
566 if(!ret)
567 return NULL;
569 memcpy(ret, fmt, size);
571 ret->cbSize = size - sizeof(WAVEFORMATEX);
573 return ret;
576 static void session_init_vols(AudioSession *session, UINT channels)
578 if(session->channel_count < channels){
579 UINT i;
581 if(session->channel_vols)
582 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
583 session->channel_vols, sizeof(float) * channels);
584 else
585 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
586 sizeof(float) * channels);
587 if(!session->channel_vols)
588 return;
590 for(i = session->channel_count; i < channels; ++i)
591 session->channel_vols[i] = 1.f;
593 session->channel_count = channels;
597 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
598 UINT num_channels)
600 AudioSession *ret;
602 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
603 if(!ret)
604 return NULL;
606 memcpy(&ret->guid, guid, sizeof(GUID));
608 ret->device = device;
610 list_init(&ret->clients);
612 list_add_head(&g_sessions, &ret->entry);
614 InitializeCriticalSection(&ret->lock);
616 session_init_vols(ret, num_channels);
618 ret->master_vol = 1.f;
620 return ret;
623 /* if channels == 0, then this will return or create a session with
624 * matching dataflow and GUID. otherwise, channels must also match */
625 static HRESULT get_audio_session(const GUID *sessionguid,
626 IMMDevice *device, UINT channels, AudioSession **out)
628 AudioSession *session;
630 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
631 *out = create_session(&GUID_NULL, device, channels);
632 if(!*out)
633 return E_OUTOFMEMORY;
635 return S_OK;
638 *out = NULL;
639 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
640 if(session->device == device &&
641 IsEqualGUID(sessionguid, &session->guid)){
642 session_init_vols(session, channels);
643 *out = session;
644 break;
648 if(!*out){
649 *out = create_session(sessionguid, device, channels);
650 if(!*out)
651 return E_OUTOFMEMORY;
654 return S_OK;
657 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
658 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
659 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
660 const GUID *sessionguid)
662 ACImpl *This = impl_from_IAudioClient(iface);
663 snd_pcm_sw_params_t *sw_params = NULL;
664 snd_pcm_format_t format;
665 snd_pcm_uframes_t boundary;
666 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
667 unsigned int time_us, rate;
668 int err, i;
669 HRESULT hr = S_OK;
671 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
672 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
674 if(!fmt)
675 return E_POINTER;
677 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
678 return AUDCLNT_E_NOT_INITIALIZED;
680 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
681 AUDCLNT_STREAMFLAGS_LOOPBACK |
682 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
683 AUDCLNT_STREAMFLAGS_NOPERSIST |
684 AUDCLNT_STREAMFLAGS_RATEADJUST |
685 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
686 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
687 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
688 TRACE("Unknown flags: %08x\n", flags);
689 return E_INVALIDARG;
692 EnterCriticalSection(&This->lock);
694 if(This->initted){
695 LeaveCriticalSection(&This->lock);
696 return AUDCLNT_E_ALREADY_INITIALIZED;
699 dump_fmt(fmt);
701 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
702 WARN("Unable to get hw_params: %d (%s)\n", err, snd_strerror(err));
703 hr = E_FAIL;
704 goto exit;
707 if((err = snd_pcm_hw_params_set_access(This->pcm_handle, This->hw_params,
708 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0){
709 WARN("Unable to set access: %d (%s)\n", err, snd_strerror(err));
710 hr = E_FAIL;
711 goto exit;
714 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
715 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
716 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
717 if(fmt->wBitsPerSample == 8)
718 format = SND_PCM_FORMAT_U8;
719 else if(fmt->wBitsPerSample == 16)
720 format = SND_PCM_FORMAT_S16_LE;
721 else if(fmt->wBitsPerSample == 24)
722 format = SND_PCM_FORMAT_S24_3LE;
723 else if(fmt->wBitsPerSample == 32)
724 format = SND_PCM_FORMAT_S32_LE;
725 else{
726 WARN("Unsupported bit depth: %u\n", fmt->wBitsPerSample);
727 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
728 goto exit;
730 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
731 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
732 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
733 if(fmt->wBitsPerSample == 32)
734 format = SND_PCM_FORMAT_FLOAT_LE;
735 else if(fmt->wBitsPerSample == 64)
736 format = SND_PCM_FORMAT_FLOAT64_LE;
737 else{
738 WARN("Unsupported float size: %u\n", fmt->wBitsPerSample);
739 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
740 goto exit;
742 }else{
743 WARN("Unknown wave format: %04x\n", fmt->wFormatTag);
744 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
745 goto exit;
748 if((err = snd_pcm_hw_params_set_format(This->pcm_handle, This->hw_params,
749 format)) < 0){
750 WARN("Unable to set ALSA format to %u: %d (%s)\n", format, err,
751 snd_strerror(err));
752 hr = E_FAIL;
753 goto exit;
756 This->alsa_format = format;
758 rate = fmt->nSamplesPerSec;
759 if((err = snd_pcm_hw_params_set_rate_near(This->pcm_handle, This->hw_params,
760 &rate, NULL)) < 0){
761 WARN("Unable to set rate to %u: %d (%s)\n", rate, err,
762 snd_strerror(err));
763 hr = E_FAIL;
764 goto exit;
767 if((err = snd_pcm_hw_params_set_channels(This->pcm_handle, This->hw_params,
768 fmt->nChannels)) < 0){
769 WARN("Unable to set channels to %u: %d (%s)\n", fmt->nChannels, err,
770 snd_strerror(err));
771 hr = E_FAIL;
772 goto exit;
775 time_us = MinimumPeriod / 10;
776 if((err = snd_pcm_hw_params_set_period_time_near(This->pcm_handle,
777 This->hw_params, &time_us, NULL)) < 0){
778 WARN("Unable to set max period time to %u: %d (%s)\n", time_us,
779 err, snd_strerror(err));
780 hr = E_FAIL;
781 goto exit;
784 if((err = snd_pcm_hw_params(This->pcm_handle, This->hw_params)) < 0){
785 WARN("Unable to set hw params: %d (%s)\n", err, snd_strerror(err));
786 hr = E_FAIL;
787 goto exit;
790 sw_params = HeapAlloc(GetProcessHeap(), 0, snd_pcm_sw_params_sizeof());
791 if(!sw_params){
792 hr = E_OUTOFMEMORY;
793 goto exit;
796 if((err = snd_pcm_sw_params_current(This->pcm_handle, sw_params)) < 0){
797 WARN("Unable to get sw_params: %d (%s)\n", err, snd_strerror(err));
798 hr = E_FAIL;
799 goto exit;
802 if(!duration)
803 duration = 300000; /* 0.03s */
804 This->bufsize_frames = ceil((duration / 10000000.) * fmt->nSamplesPerSec);
805 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
806 This->bufsize_frames * fmt->nBlockAlign);
807 if(!This->local_buffer){
808 hr = E_OUTOFMEMORY;
809 goto exit;
811 if (fmt->wBitsPerSample == 8)
812 memset(This->local_buffer, 128, This->bufsize_frames * fmt->nBlockAlign);
813 else
814 memset(This->local_buffer, 0, This->bufsize_frames * fmt->nBlockAlign);
816 if((err = snd_pcm_sw_params_get_boundary(sw_params, &boundary)) < 0){
817 WARN("Unable to get boundary: %d (%s)\n", err, snd_strerror(err));
818 hr = E_FAIL;
819 goto exit;
822 if((err = snd_pcm_sw_params_set_start_threshold(This->pcm_handle,
823 sw_params, boundary)) < 0){
824 WARN("Unable to set start threshold to %lx: %d (%s)\n", boundary, err,
825 snd_strerror(err));
826 hr = E_FAIL;
827 goto exit;
830 if((err = snd_pcm_sw_params_set_stop_threshold(This->pcm_handle,
831 sw_params, boundary)) < 0){
832 WARN("Unable to set stop threshold to %lx: %d (%s)\n", boundary, err,
833 snd_strerror(err));
834 hr = E_FAIL;
835 goto exit;
838 if((err = snd_pcm_sw_params_set_avail_min(This->pcm_handle,
839 sw_params, 0)) < 0){
840 WARN("Unable to set avail min to 0: %d (%s)\n", err, snd_strerror(err));
841 hr = E_FAIL;
842 goto exit;
845 if((err = snd_pcm_sw_params_set_silence_size(This->pcm_handle,
846 sw_params, boundary)) < 0){
847 WARN("Unable to set silence size to %lx: %d (%s)\n", boundary, err,
848 snd_strerror(err));
849 hr = E_FAIL;
850 goto exit;
853 if((err = snd_pcm_sw_params(This->pcm_handle, sw_params)) < 0){
854 WARN("Unable to set sw params: %d (%s)\n", err, snd_strerror(err));
855 hr = E_FAIL;
856 goto exit;
859 if((err = snd_pcm_prepare(This->pcm_handle)) < 0){
860 WARN("Unable to prepare device: %d (%s)\n", err, snd_strerror(err));
861 hr = E_FAIL;
862 goto exit;
865 if((err = snd_pcm_hw_params_get_buffer_size(This->hw_params,
866 &This->bufsize_alsa)) < 0){
867 WARN("Unable to get buffer size: %d (%s)\n", err, snd_strerror(err));
868 hr = E_FAIL;
869 goto exit;
872 if((err = snd_pcm_hw_params_get_period_size(This->hw_params,
873 &This->period_alsa, NULL)) < 0){
874 WARN("Unable to get period size: %d (%s)\n", err, snd_strerror(err));
875 hr = E_FAIL;
876 goto exit;
879 if((err = snd_pcm_hw_params_get_period_time(This->hw_params,
880 &This->period_us, NULL)) < 0){
881 WARN("Unable to get period time: %d (%s)\n", err, snd_strerror(err));
882 hr = E_FAIL;
883 goto exit;
886 This->fmt = clone_format(fmt);
887 if(!This->fmt){
888 hr = E_OUTOFMEMORY;
889 goto exit;
892 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
893 if(!This->vols){
894 hr = E_OUTOFMEMORY;
895 goto exit;
898 for(i = 0; i < fmt->nChannels; ++i)
899 This->vols[i] = 1.f;
901 This->share = mode;
902 This->flags = flags;
904 EnterCriticalSection(&g_sessions_lock);
906 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
907 &This->session);
908 if(FAILED(hr)){
909 LeaveCriticalSection(&g_sessions_lock);
910 goto exit;
913 list_add_tail(&This->session->clients, &This->entry);
915 LeaveCriticalSection(&g_sessions_lock);
917 This->initted = TRUE;
919 exit:
920 HeapFree(GetProcessHeap(), 0, sw_params);
921 if(FAILED(hr)){
922 if(This->local_buffer){
923 HeapFree(GetProcessHeap(), 0, This->local_buffer);
924 This->local_buffer = NULL;
926 if(This->fmt){
927 CoTaskMemFree(This->fmt);
928 This->fmt = NULL;
930 if(This->vols){
931 HeapFree(GetProcessHeap(), 0, This->vols);
932 This->vols = NULL;
936 LeaveCriticalSection(&This->lock);
938 return hr;
941 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
942 UINT32 *out)
944 ACImpl *This = impl_from_IAudioClient(iface);
946 TRACE("(%p)->(%p)\n", This, out);
948 if(!out)
949 return E_POINTER;
951 EnterCriticalSection(&This->lock);
953 if(!This->initted){
954 LeaveCriticalSection(&This->lock);
955 return AUDCLNT_E_NOT_INITIALIZED;
958 *out = This->bufsize_frames;
960 LeaveCriticalSection(&This->lock);
962 return S_OK;
965 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
966 REFERENCE_TIME *latency)
968 ACImpl *This = impl_from_IAudioClient(iface);
970 TRACE("(%p)->(%p)\n", This, latency);
972 if(!latency)
973 return E_POINTER;
975 EnterCriticalSection(&This->lock);
977 if(!This->initted){
978 LeaveCriticalSection(&This->lock);
979 return AUDCLNT_E_NOT_INITIALIZED;
982 LeaveCriticalSection(&This->lock);
984 *latency = 500000;
986 return S_OK;
989 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
990 UINT32 *out)
992 ACImpl *This = impl_from_IAudioClient(iface);
994 TRACE("(%p)->(%p)\n", This, out);
996 if(!out)
997 return E_POINTER;
999 EnterCriticalSection(&This->lock);
1001 if(!This->initted){
1002 LeaveCriticalSection(&This->lock);
1003 return AUDCLNT_E_NOT_INITIALIZED;
1006 if(This->dataflow == eRender){
1007 snd_pcm_sframes_t avail_frames;
1009 avail_frames = snd_pcm_avail_update(This->pcm_handle);
1011 if(This->bufsize_alsa < avail_frames){
1012 WARN("Xrun detected\n");
1013 *out = This->held_frames;
1014 }else
1015 *out = This->bufsize_alsa - avail_frames + This->held_frames;
1016 }else if(This->dataflow == eCapture){
1017 *out = This->held_frames;
1018 }else{
1019 LeaveCriticalSection(&This->lock);
1020 return E_UNEXPECTED;
1023 LeaveCriticalSection(&This->lock);
1025 return S_OK;
1028 static DWORD get_channel_mask(unsigned int channels)
1030 switch(channels){
1031 case 0:
1032 return 0;
1033 case 1:
1034 return SPEAKER_FRONT_CENTER;
1035 case 2:
1036 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1037 case 3:
1038 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
1039 SPEAKER_LOW_FREQUENCY;
1040 case 4:
1041 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
1042 SPEAKER_BACK_RIGHT;
1043 case 5:
1044 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
1045 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY;
1046 case 6:
1047 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
1048 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER;
1049 case 7:
1050 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
1051 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER |
1052 SPEAKER_BACK_CENTER;
1054 FIXME("Unknown speaker configuration: %u\n", channels);
1055 return 0;
1058 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1059 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *fmt,
1060 WAVEFORMATEX **out)
1062 ACImpl *This = impl_from_IAudioClient(iface);
1063 snd_pcm_format_mask_t *formats = NULL;
1064 HRESULT hr = S_OK;
1065 WAVEFORMATEX *closest = NULL;
1066 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
1067 unsigned int max = 0, min = 0;
1068 int err;
1070 TRACE("(%p)->(%x, %p, %p)\n", This, mode, fmt, out);
1072 if(!fmt || (mode == AUDCLNT_SHAREMODE_SHARED && !out))
1073 return E_POINTER;
1075 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1076 return E_INVALIDARG;
1078 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1079 fmt->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1080 return E_INVALIDARG;
1082 dump_fmt(fmt);
1084 EnterCriticalSection(&This->lock);
1086 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
1087 hr = E_FAIL;
1088 goto exit;
1091 formats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1092 snd_pcm_format_mask_sizeof());
1093 if(!formats){
1094 hr = E_OUTOFMEMORY;
1095 goto exit;
1098 snd_pcm_hw_params_get_format_mask(This->hw_params, formats);
1100 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
1101 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1102 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
1103 switch(fmt->wBitsPerSample){
1104 case 8:
1105 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_U8)){
1106 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1107 goto exit;
1109 break;
1110 case 16:
1111 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S16_LE)){
1112 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1113 goto exit;
1115 break;
1116 case 24:
1117 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S24_3LE)){
1118 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1119 goto exit;
1121 break;
1122 case 32:
1123 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S32_LE)){
1124 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1125 goto exit;
1127 break;
1128 default:
1129 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1130 goto exit;
1132 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
1133 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1134 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
1135 switch(fmt->wBitsPerSample){
1136 case 32:
1137 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT_LE)){
1138 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1139 goto exit;
1141 break;
1142 case 64:
1143 if(!snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT64_LE)){
1144 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1145 goto exit;
1147 break;
1148 default:
1149 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1150 goto exit;
1152 }else{
1153 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1154 goto exit;
1157 closest = clone_format(fmt);
1158 if(!closest){
1159 hr = E_OUTOFMEMORY;
1160 goto exit;
1163 if((err = snd_pcm_hw_params_get_rate_min(This->hw_params, &min, NULL)) < 0){
1164 hr = E_FAIL;
1165 WARN("Unable to get min rate: %d (%s)\n", err, snd_strerror(err));
1166 goto exit;
1169 if((err = snd_pcm_hw_params_get_rate_max(This->hw_params, &max, NULL)) < 0){
1170 hr = E_FAIL;
1171 WARN("Unable to get max rate: %d (%s)\n", err, snd_strerror(err));
1172 goto exit;
1175 if(fmt->nSamplesPerSec < min || fmt->nSamplesPerSec > max){
1176 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1177 goto exit;
1180 if((err = snd_pcm_hw_params_get_channels_min(This->hw_params, &min)) < 0){
1181 hr = E_FAIL;
1182 WARN("Unable to get min channels: %d (%s)\n", err, snd_strerror(err));
1183 goto exit;
1186 if((err = snd_pcm_hw_params_get_channels_max(This->hw_params, &max)) < 0){
1187 hr = E_FAIL;
1188 WARN("Unable to get max channels: %d (%s)\n", err, snd_strerror(err));
1189 goto exit;
1191 if(max > 7)
1192 max = 2;
1193 if(fmt->nChannels > max){
1194 hr = S_FALSE;
1195 closest->nChannels = max;
1196 }else if(fmt->nChannels < min){
1197 hr = S_FALSE;
1198 closest->nChannels = min;
1201 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
1202 DWORD mask = get_channel_mask(closest->nChannels);
1204 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = mask;
1206 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1207 fmtex->dwChannelMask != mask)
1208 hr = S_FALSE;
1211 exit:
1212 LeaveCriticalSection(&This->lock);
1213 HeapFree(GetProcessHeap(), 0, formats);
1215 if(hr == S_OK || !out){
1216 CoTaskMemFree(closest);
1217 if(out)
1218 *out = NULL;
1219 }else if(closest){
1220 closest->nBlockAlign =
1221 closest->nChannels * closest->wBitsPerSample / 8;
1222 closest->nAvgBytesPerSec =
1223 closest->nBlockAlign * closest->nSamplesPerSec;
1224 *out = closest;
1227 TRACE("returning: %08x\n", hr);
1228 return hr;
1231 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1232 WAVEFORMATEX **pwfx)
1234 ACImpl *This = impl_from_IAudioClient(iface);
1235 WAVEFORMATEXTENSIBLE *fmt;
1236 snd_pcm_format_mask_t *formats;
1237 unsigned int max_rate, max_channels;
1238 int err;
1239 HRESULT hr = S_OK;
1241 TRACE("(%p)->(%p)\n", This, pwfx);
1243 if(!pwfx)
1244 return E_POINTER;
1245 *pwfx = NULL;
1247 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1248 if(!fmt)
1249 return E_OUTOFMEMORY;
1251 formats = HeapAlloc(GetProcessHeap(), 0, snd_pcm_format_mask_sizeof());
1252 if(!formats){
1253 CoTaskMemFree(fmt);
1254 return E_OUTOFMEMORY;
1257 EnterCriticalSection(&This->lock);
1259 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
1260 WARN("Unable to get hw_params: %d (%s)\n", err, snd_strerror(err));
1261 hr = E_FAIL;
1262 goto exit;
1265 snd_pcm_hw_params_get_format_mask(This->hw_params, formats);
1267 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1268 if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT_LE)){
1269 fmt->Format.wBitsPerSample = 32;
1270 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1271 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S16_LE)){
1272 fmt->Format.wBitsPerSample = 16;
1273 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1274 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_U8)){
1275 fmt->Format.wBitsPerSample = 8;
1276 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1277 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S32_LE)){
1278 fmt->Format.wBitsPerSample = 32;
1279 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1280 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S24_3LE)){
1281 fmt->Format.wBitsPerSample = 24;
1282 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1283 }else{
1284 ERR("Didn't recognize any available ALSA formats\n");
1285 hr = E_FAIL;
1286 goto exit;
1289 if((err = snd_pcm_hw_params_get_channels_max(This->hw_params,
1290 &max_channels)) < 0){
1291 WARN("Unable to get max channels: %d (%s)\n", err, snd_strerror(err));
1292 hr = E_FAIL;
1293 goto exit;
1296 if(max_channels > 2){
1297 FIXME("Don't know what to do with %u channels, pretending there's "
1298 "only 2 channels\n", max_channels);
1299 fmt->Format.nChannels = 2;
1300 }else
1301 fmt->Format.nChannels = max_channels;
1303 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1305 if((err = snd_pcm_hw_params_get_rate_max(This->hw_params, &max_rate,
1306 NULL)) < 0){
1307 WARN("Unable to get max rate: %d (%s)\n", err, snd_strerror(err));
1308 hr = E_FAIL;
1309 goto exit;
1312 if(max_rate >= 48000)
1313 fmt->Format.nSamplesPerSec = 48000;
1314 else if(max_rate >= 44100)
1315 fmt->Format.nSamplesPerSec = 44100;
1316 else if(max_rate >= 22050)
1317 fmt->Format.nSamplesPerSec = 22050;
1318 else if(max_rate >= 11025)
1319 fmt->Format.nSamplesPerSec = 11025;
1320 else if(max_rate >= 8000)
1321 fmt->Format.nSamplesPerSec = 8000;
1322 else{
1323 ERR("Unknown max rate: %u\n", max_rate);
1324 hr = E_FAIL;
1325 goto exit;
1328 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1329 fmt->Format.nChannels) / 8;
1330 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1331 fmt->Format.nBlockAlign;
1333 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1334 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1336 dump_fmt((WAVEFORMATEX*)fmt);
1337 *pwfx = (WAVEFORMATEX*)fmt;
1339 exit:
1340 LeaveCriticalSection(&This->lock);
1341 if(FAILED(hr))
1342 CoTaskMemFree(fmt);
1343 HeapFree(GetProcessHeap(), 0, formats);
1345 return hr;
1348 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1349 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1351 ACImpl *This = impl_from_IAudioClient(iface);
1353 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1355 if(!defperiod && !minperiod)
1356 return E_POINTER;
1358 if(defperiod)
1359 *defperiod = DefaultPeriod;
1360 if(minperiod)
1361 *minperiod = MinimumPeriod;
1363 return S_OK;
1366 static snd_pcm_sframes_t alsa_write_best_effort(snd_pcm_t *handle, BYTE *buf,
1367 snd_pcm_uframes_t frames, ACImpl *This)
1369 snd_pcm_sframes_t written;
1371 if(This->session->mute){
1372 int err;
1373 if((err = snd_pcm_format_set_silence(This->alsa_format, buf,
1374 frames * This->fmt->nChannels)) < 0)
1375 WARN("Setting buffer to silence failed: %d (%s)\n", err,
1376 snd_strerror(err));
1379 written = snd_pcm_writei(handle, buf, frames);
1380 if(written < 0){
1381 int ret;
1383 if(written == -EAGAIN)
1384 /* buffer full */
1385 return 0;
1387 WARN("writei failed, recovering: %ld (%s)\n", written,
1388 snd_strerror(written));
1390 ret = wine_snd_pcm_recover(handle, written, 0);
1391 if(ret < 0){
1392 WARN("Could not recover: %d (%s)\n", ret, snd_strerror(ret));
1393 return ret;
1396 written = snd_pcm_writei(handle, buf, frames);
1399 return written;
1402 static void alsa_write_data(ACImpl *This)
1404 snd_pcm_sframes_t written;
1405 snd_pcm_uframes_t to_write;
1406 BYTE *buf =
1407 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1409 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1410 to_write = This->bufsize_frames - This->lcl_offs_frames;
1411 else
1412 to_write = This->held_frames;
1414 written = alsa_write_best_effort(This->pcm_handle, buf, to_write, This);
1415 if(written < 0){
1416 WARN("Couldn't write: %ld (%s)\n", written, snd_strerror(written));
1417 return;
1420 This->lcl_offs_frames += written;
1421 This->lcl_offs_frames %= This->bufsize_frames;
1422 This->held_frames -= written;
1424 if(written < to_write){
1425 /* ALSA buffer probably full */
1426 return;
1429 if(This->held_frames){
1430 /* wrapped and have some data back at the start to write */
1431 written = alsa_write_best_effort(This->pcm_handle, This->local_buffer,
1432 This->held_frames, This);
1433 if(written < 0){
1434 WARN("Couldn't write: %ld (%s)\n", written, snd_strerror(written));
1435 return;
1438 This->lcl_offs_frames += written;
1439 This->lcl_offs_frames %= This->bufsize_frames;
1440 This->held_frames -= written;
1444 static void alsa_read_data(ACImpl *This)
1446 snd_pcm_sframes_t pos, readable, nread;
1448 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1449 readable = This->bufsize_frames - pos;
1451 nread = snd_pcm_readi(This->pcm_handle,
1452 This->local_buffer + pos * This->fmt->nBlockAlign, readable);
1453 if(nread < 0){
1454 int ret;
1456 WARN("read failed, recovering: %ld (%s)\n", nread, snd_strerror(nread));
1458 ret = wine_snd_pcm_recover(This->pcm_handle, nread, 0);
1459 if(ret < 0){
1460 WARN("Recover failed: %d (%s)\n", ret, snd_strerror(ret));
1461 return;
1464 nread = snd_pcm_readi(This->pcm_handle,
1465 This->local_buffer + pos * This->fmt->nBlockAlign, readable);
1466 if(nread < 0){
1467 WARN("read failed: %ld (%s)\n", nread, snd_strerror(nread));
1468 return;
1472 if(This->session->mute){
1473 int err;
1474 if((err = snd_pcm_format_set_silence(This->alsa_format,
1475 This->local_buffer + pos * This->fmt->nBlockAlign,
1476 nread)) < 0)
1477 WARN("Setting buffer to silence failed: %d (%s)\n", err,
1478 snd_strerror(err));
1481 This->held_frames += nread;
1483 if(This->held_frames > This->bufsize_frames){
1484 WARN("Overflow of unread data\n");
1485 This->lcl_offs_frames += This->held_frames;
1486 This->lcl_offs_frames %= This->bufsize_frames;
1487 This->held_frames = This->bufsize_frames;
1491 static void CALLBACK alsa_push_buffer_data(void *user, BOOLEAN timer)
1493 ACImpl *This = user;
1495 EnterCriticalSection(&This->lock);
1497 if(This->started){
1498 if(This->dataflow == eRender && This->held_frames)
1499 alsa_write_data(This);
1500 else if(This->dataflow == eCapture)
1501 alsa_read_data(This);
1503 if(This->event)
1504 SetEvent(This->event);
1507 LeaveCriticalSection(&This->lock);
1510 static HRESULT alsa_consider_start(ACImpl *This)
1512 snd_pcm_sframes_t avail;
1513 int err;
1515 avail = snd_pcm_avail_update(This->pcm_handle);
1516 if(avail < 0){
1517 WARN("Unable to get avail_update: %ld (%s)\n", avail,
1518 snd_strerror(avail));
1519 return E_FAIL;
1522 if(This->period_alsa < This->bufsize_alsa - avail){
1523 if((err = snd_pcm_start(This->pcm_handle)) < 0){
1524 WARN("Start failed: %d (%s), state: %d\n", err, snd_strerror(err),
1525 snd_pcm_state(This->pcm_handle));
1526 return E_FAIL;
1529 return S_OK;
1532 return S_FALSE;
1535 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1537 ACImpl *This = impl_from_IAudioClient(iface);
1538 DWORD period_ms;
1539 HRESULT hr;
1541 TRACE("(%p)\n", This);
1543 EnterCriticalSection(&This->lock);
1545 if(!This->initted){
1546 LeaveCriticalSection(&This->lock);
1547 return AUDCLNT_E_NOT_INITIALIZED;
1550 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1551 LeaveCriticalSection(&This->lock);
1552 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1555 if(This->started){
1556 LeaveCriticalSection(&This->lock);
1557 return AUDCLNT_E_NOT_STOPPED;
1560 hr = alsa_consider_start(This);
1561 if(FAILED(hr)){
1562 LeaveCriticalSection(&This->lock);
1563 return hr;
1566 period_ms = This->period_us / 1000;
1567 if(!period_ms)
1568 period_ms = 10;
1570 if(This->dataflow == eCapture){
1571 /* dump any data that might be leftover in the ALSA capture buffer */
1572 snd_pcm_readi(This->pcm_handle, This->local_buffer,
1573 This->bufsize_frames);
1576 if(!CreateTimerQueueTimer(&This->timer, g_timer_q, alsa_push_buffer_data,
1577 This, 0, period_ms, WT_EXECUTEINTIMERTHREAD)){
1578 LeaveCriticalSection(&This->lock);
1579 WARN("Unable to create timer: %u\n", GetLastError());
1580 return E_FAIL;
1583 This->started = TRUE;
1585 LeaveCriticalSection(&This->lock);
1587 return S_OK;
1590 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1592 ACImpl *This = impl_from_IAudioClient(iface);
1593 int err;
1594 HANDLE event;
1595 BOOL wait;
1597 TRACE("(%p)\n", This);
1599 EnterCriticalSection(&This->lock);
1601 if(!This->initted){
1602 LeaveCriticalSection(&This->lock);
1603 return AUDCLNT_E_NOT_INITIALIZED;
1606 if(!This->started){
1607 LeaveCriticalSection(&This->lock);
1608 return S_FALSE;
1611 event = CreateEventW(NULL, TRUE, FALSE, NULL);
1612 wait = !DeleteTimerQueueTimer(g_timer_q, This->timer, event);
1613 if(wait)
1614 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1615 wait = wait && GetLastError() == ERROR_IO_PENDING;
1617 if((err = snd_pcm_drop(This->pcm_handle)) < 0){
1618 LeaveCriticalSection(&This->lock);
1619 WARN("Drop failed: %d (%s)\n", err, snd_strerror(err));
1620 return E_FAIL;
1623 if((err = snd_pcm_prepare(This->pcm_handle)) < 0){
1624 LeaveCriticalSection(&This->lock);
1625 WARN("Prepare failed: %d (%s)\n", err, snd_strerror(err));
1626 return E_FAIL;
1629 This->started = FALSE;
1631 LeaveCriticalSection(&This->lock);
1633 if(event && wait)
1634 WaitForSingleObject(event, INFINITE);
1635 CloseHandle(event);
1637 return S_OK;
1640 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1642 ACImpl *This = impl_from_IAudioClient(iface);
1644 TRACE("(%p)\n", This);
1646 EnterCriticalSection(&This->lock);
1648 if(!This->initted){
1649 LeaveCriticalSection(&This->lock);
1650 return AUDCLNT_E_NOT_INITIALIZED;
1653 if(This->started){
1654 LeaveCriticalSection(&This->lock);
1655 return AUDCLNT_E_NOT_STOPPED;
1658 This->held_frames = 0;
1659 This->written_frames = 0;
1660 This->lcl_offs_frames = 0;
1662 LeaveCriticalSection(&This->lock);
1664 return S_OK;
1667 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1668 HANDLE event)
1670 ACImpl *This = impl_from_IAudioClient(iface);
1672 TRACE("(%p)->(%p)\n", This, event);
1674 if(!event)
1675 return E_INVALIDARG;
1677 EnterCriticalSection(&This->lock);
1679 if(!This->initted){
1680 LeaveCriticalSection(&This->lock);
1681 return AUDCLNT_E_NOT_INITIALIZED;
1684 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1685 LeaveCriticalSection(&This->lock);
1686 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1689 This->event = event;
1691 LeaveCriticalSection(&This->lock);
1693 return S_OK;
1696 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1697 void **ppv)
1699 ACImpl *This = impl_from_IAudioClient(iface);
1701 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1703 if(!ppv)
1704 return E_POINTER;
1705 *ppv = NULL;
1707 EnterCriticalSection(&This->lock);
1709 if(!This->initted){
1710 LeaveCriticalSection(&This->lock);
1711 return AUDCLNT_E_NOT_INITIALIZED;
1714 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1715 if(This->dataflow != eRender){
1716 LeaveCriticalSection(&This->lock);
1717 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1719 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1720 *ppv = &This->IAudioRenderClient_iface;
1721 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1722 if(This->dataflow != eCapture){
1723 LeaveCriticalSection(&This->lock);
1724 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1726 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1727 *ppv = &This->IAudioCaptureClient_iface;
1728 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1729 IAudioClock_AddRef(&This->IAudioClock_iface);
1730 *ppv = &This->IAudioClock_iface;
1731 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1732 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1733 *ppv = &This->IAudioStreamVolume_iface;
1734 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1735 if(!This->session_wrapper){
1736 This->session_wrapper = AudioSessionWrapper_Create(This);
1737 if(!This->session_wrapper){
1738 LeaveCriticalSection(&This->lock);
1739 return E_OUTOFMEMORY;
1741 }else
1742 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1744 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1745 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1746 if(!This->session_wrapper){
1747 This->session_wrapper = AudioSessionWrapper_Create(This);
1748 if(!This->session_wrapper){
1749 LeaveCriticalSection(&This->lock);
1750 return E_OUTOFMEMORY;
1752 }else
1753 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1755 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1756 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1757 if(!This->session_wrapper){
1758 This->session_wrapper = AudioSessionWrapper_Create(This);
1759 if(!This->session_wrapper){
1760 LeaveCriticalSection(&This->lock);
1761 return E_OUTOFMEMORY;
1763 }else
1764 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1766 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1769 if(*ppv){
1770 LeaveCriticalSection(&This->lock);
1771 return S_OK;
1774 LeaveCriticalSection(&This->lock);
1776 FIXME("stub %s\n", debugstr_guid(riid));
1777 return E_NOINTERFACE;
1780 static const IAudioClientVtbl AudioClient_Vtbl =
1782 AudioClient_QueryInterface,
1783 AudioClient_AddRef,
1784 AudioClient_Release,
1785 AudioClient_Initialize,
1786 AudioClient_GetBufferSize,
1787 AudioClient_GetStreamLatency,
1788 AudioClient_GetCurrentPadding,
1789 AudioClient_IsFormatSupported,
1790 AudioClient_GetMixFormat,
1791 AudioClient_GetDevicePeriod,
1792 AudioClient_Start,
1793 AudioClient_Stop,
1794 AudioClient_Reset,
1795 AudioClient_SetEventHandle,
1796 AudioClient_GetService
1799 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1800 IAudioRenderClient *iface, REFIID riid, void **ppv)
1802 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1804 if(!ppv)
1805 return E_POINTER;
1806 *ppv = NULL;
1808 if(IsEqualIID(riid, &IID_IUnknown) ||
1809 IsEqualIID(riid, &IID_IAudioRenderClient))
1810 *ppv = iface;
1811 if(*ppv){
1812 IUnknown_AddRef((IUnknown*)*ppv);
1813 return S_OK;
1816 WARN("Unknown interface %s\n", debugstr_guid(riid));
1817 return E_NOINTERFACE;
1820 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1822 ACImpl *This = impl_from_IAudioRenderClient(iface);
1823 return AudioClient_AddRef(&This->IAudioClient_iface);
1826 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1828 ACImpl *This = impl_from_IAudioRenderClient(iface);
1829 return AudioClient_Release(&This->IAudioClient_iface);
1832 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1833 UINT32 frames, BYTE **data)
1835 ACImpl *This = impl_from_IAudioRenderClient(iface);
1836 UINT32 write_pos;
1837 UINT32 pad;
1838 HRESULT hr;
1840 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1842 if(!data)
1843 return E_POINTER;
1845 EnterCriticalSection(&This->lock);
1847 if(This->buf_state != NOT_LOCKED){
1848 LeaveCriticalSection(&This->lock);
1849 return AUDCLNT_E_OUT_OF_ORDER;
1852 if(!frames){
1853 This->buf_state = LOCKED_NORMAL;
1854 LeaveCriticalSection(&This->lock);
1855 return S_OK;
1858 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1859 if(FAILED(hr)){
1860 LeaveCriticalSection(&This->lock);
1861 return hr;
1864 if(pad + frames > This->bufsize_frames){
1865 LeaveCriticalSection(&This->lock);
1866 return AUDCLNT_E_BUFFER_TOO_LARGE;
1869 write_pos =
1870 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1871 if(write_pos + frames > This->bufsize_frames){
1872 if(This->tmp_buffer_frames < frames){
1873 if(This->tmp_buffer)
1874 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1875 This->tmp_buffer, frames * This->fmt->nBlockAlign);
1876 else
1877 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1878 frames * This->fmt->nBlockAlign);
1879 if(!This->tmp_buffer){
1880 LeaveCriticalSection(&This->lock);
1881 return E_OUTOFMEMORY;
1883 This->tmp_buffer_frames = frames;
1885 *data = This->tmp_buffer;
1886 This->buf_state = LOCKED_WRAPPED;
1887 }else{
1888 *data = This->local_buffer + write_pos * This->fmt->nBlockAlign;
1889 This->buf_state = LOCKED_NORMAL;
1892 LeaveCriticalSection(&This->lock);
1894 return S_OK;
1897 static void alsa_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_bytes)
1899 snd_pcm_uframes_t write_offs_frames =
1900 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1901 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
1902 snd_pcm_uframes_t chunk_frames = This->bufsize_frames - write_offs_frames;
1903 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
1905 if(written_bytes < chunk_bytes){
1906 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
1907 }else{
1908 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
1909 memcpy(This->local_buffer, buffer + chunk_bytes,
1910 written_bytes - chunk_bytes);
1914 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1915 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
1917 ACImpl *This = impl_from_IAudioRenderClient(iface);
1918 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
1919 BYTE *buffer;
1920 HRESULT hr;
1922 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
1924 EnterCriticalSection(&This->lock);
1926 if(This->buf_state == NOT_LOCKED || !written_frames){
1927 This->buf_state = NOT_LOCKED;
1928 LeaveCriticalSection(&This->lock);
1929 return written_frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK;
1932 if(This->buf_state == LOCKED_NORMAL)
1933 buffer = This->local_buffer +
1934 (This->lcl_offs_frames + This->held_frames) * This->fmt->nBlockAlign;
1935 else
1936 buffer = This->tmp_buffer;
1938 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1939 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1940 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1941 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1942 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1943 This->fmt->wBitsPerSample == 8)
1944 memset(buffer, 128, written_frames * This->fmt->nBlockAlign);
1945 else
1946 memset(buffer, 0, written_frames * This->fmt->nBlockAlign);
1949 if(This->held_frames){
1950 if(This->buf_state == LOCKED_WRAPPED)
1951 alsa_wrap_buffer(This, buffer, written_bytes);
1953 This->held_frames += written_frames;
1954 }else{
1955 snd_pcm_sframes_t written;
1957 written = alsa_write_best_effort(This->pcm_handle, buffer,
1958 written_frames, This);
1959 if(written < 0){
1960 LeaveCriticalSection(&This->lock);
1961 WARN("write failed: %ld (%s)\n", written, snd_strerror(written));
1962 return E_FAIL;
1965 if(written < written_frames){
1966 if(This->buf_state == LOCKED_WRAPPED)
1967 alsa_wrap_buffer(This,
1968 This->tmp_buffer + written * This->fmt->nBlockAlign,
1969 written_frames - written);
1971 This->held_frames = written_frames - written;
1975 if(This->started &&
1976 snd_pcm_state(This->pcm_handle) == SND_PCM_STATE_PREPARED){
1977 hr = alsa_consider_start(This);
1978 if(FAILED(hr)){
1979 LeaveCriticalSection(&This->lock);
1980 return hr;
1984 This->written_frames += written_frames;
1985 This->buf_state = NOT_LOCKED;
1987 LeaveCriticalSection(&This->lock);
1989 return S_OK;
1992 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1993 AudioRenderClient_QueryInterface,
1994 AudioRenderClient_AddRef,
1995 AudioRenderClient_Release,
1996 AudioRenderClient_GetBuffer,
1997 AudioRenderClient_ReleaseBuffer
2000 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
2001 IAudioCaptureClient *iface, REFIID riid, void **ppv)
2003 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2005 if(!ppv)
2006 return E_POINTER;
2007 *ppv = NULL;
2009 if(IsEqualIID(riid, &IID_IUnknown) ||
2010 IsEqualIID(riid, &IID_IAudioCaptureClient))
2011 *ppv = iface;
2012 if(*ppv){
2013 IUnknown_AddRef((IUnknown*)*ppv);
2014 return S_OK;
2017 WARN("Unknown interface %s\n", debugstr_guid(riid));
2018 return E_NOINTERFACE;
2021 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
2023 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2024 return IAudioClient_AddRef(&This->IAudioClient_iface);
2027 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
2029 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2030 return IAudioClient_Release(&This->IAudioClient_iface);
2033 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
2034 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
2035 UINT64 *qpcpos)
2037 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2038 HRESULT hr;
2040 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
2041 devpos, qpcpos);
2043 if(!data || !frames || !flags)
2044 return E_POINTER;
2046 EnterCriticalSection(&This->lock);
2048 if(This->buf_state != NOT_LOCKED){
2049 LeaveCriticalSection(&This->lock);
2050 return AUDCLNT_E_OUT_OF_ORDER;
2053 hr = IAudioCaptureClient_GetNextPacketSize(iface, frames);
2054 if(FAILED(hr)){
2055 LeaveCriticalSection(&This->lock);
2056 return hr;
2059 *flags = 0;
2061 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
2062 UINT32 chunk_bytes, offs_bytes, frames_bytes;
2063 if(This->tmp_buffer_frames < *frames){
2064 if(This->tmp_buffer)
2065 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
2066 This->tmp_buffer, *frames * This->fmt->nBlockAlign);
2067 else
2068 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
2069 *frames * This->fmt->nBlockAlign);
2070 if(!This->tmp_buffer){
2071 LeaveCriticalSection(&This->lock);
2072 return E_OUTOFMEMORY;
2074 This->tmp_buffer_frames = *frames;
2077 *data = This->tmp_buffer;
2078 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
2079 This->fmt->nBlockAlign;
2080 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
2081 frames_bytes = *frames * This->fmt->nBlockAlign;
2082 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
2083 memcpy(This->tmp_buffer + chunk_bytes, This->local_buffer,
2084 frames_bytes - chunk_bytes);
2085 }else
2086 *data = This->local_buffer +
2087 This->lcl_offs_frames * This->fmt->nBlockAlign;
2089 This->buf_state = LOCKED_NORMAL;
2091 if(devpos || qpcpos)
2092 IAudioClock_GetPosition(&This->IAudioClock_iface, devpos, qpcpos);
2094 LeaveCriticalSection(&This->lock);
2096 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
2099 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
2100 IAudioCaptureClient *iface, UINT32 done)
2102 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2104 TRACE("(%p)->(%u)\n", This, done);
2106 EnterCriticalSection(&This->lock);
2108 if(This->buf_state == NOT_LOCKED){
2109 LeaveCriticalSection(&This->lock);
2110 return AUDCLNT_E_OUT_OF_ORDER;
2113 This->held_frames -= done;
2114 This->lcl_offs_frames += done;
2115 This->lcl_offs_frames %= This->bufsize_frames;
2117 This->buf_state = NOT_LOCKED;
2119 LeaveCriticalSection(&This->lock);
2121 return S_OK;
2124 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2125 IAudioCaptureClient *iface, UINT32 *frames)
2127 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2129 TRACE("(%p)->(%p)\n", This, frames);
2131 return AudioClient_GetCurrentPadding(&This->IAudioClient_iface, frames);
2134 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2136 AudioCaptureClient_QueryInterface,
2137 AudioCaptureClient_AddRef,
2138 AudioCaptureClient_Release,
2139 AudioCaptureClient_GetBuffer,
2140 AudioCaptureClient_ReleaseBuffer,
2141 AudioCaptureClient_GetNextPacketSize
2144 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2145 REFIID riid, void **ppv)
2147 ACImpl *This = impl_from_IAudioClock(iface);
2149 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2151 if(!ppv)
2152 return E_POINTER;
2153 *ppv = NULL;
2155 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2156 *ppv = iface;
2157 else if(IsEqualIID(riid, &IID_IAudioClock2))
2158 *ppv = &This->IAudioClock2_iface;
2159 if(*ppv){
2160 IUnknown_AddRef((IUnknown*)*ppv);
2161 return S_OK;
2164 WARN("Unknown interface %s\n", debugstr_guid(riid));
2165 return E_NOINTERFACE;
2168 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2170 ACImpl *This = impl_from_IAudioClock(iface);
2171 return IAudioClient_AddRef(&This->IAudioClient_iface);
2174 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2176 ACImpl *This = impl_from_IAudioClock(iface);
2177 return IAudioClient_Release(&This->IAudioClient_iface);
2180 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2182 ACImpl *This = impl_from_IAudioClock(iface);
2184 TRACE("(%p)->(%p)\n", This, freq);
2186 *freq = This->fmt->nSamplesPerSec;
2188 return S_OK;
2191 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2192 UINT64 *qpctime)
2194 ACImpl *This = impl_from_IAudioClock(iface);
2195 UINT32 pad;
2196 HRESULT hr;
2198 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2200 if(!pos)
2201 return E_POINTER;
2203 EnterCriticalSection(&This->lock);
2205 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
2206 if(FAILED(hr)){
2207 LeaveCriticalSection(&This->lock);
2208 return hr;
2211 if(This->dataflow == eRender)
2212 *pos = This->written_frames - pad;
2213 else if(This->dataflow == eCapture)
2214 *pos = This->written_frames + pad;
2216 LeaveCriticalSection(&This->lock);
2218 if(qpctime){
2219 LARGE_INTEGER stamp, freq;
2220 QueryPerformanceCounter(&stamp);
2221 QueryPerformanceFrequency(&freq);
2222 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2225 return S_OK;
2228 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2229 DWORD *chars)
2231 ACImpl *This = impl_from_IAudioClock(iface);
2233 TRACE("(%p)->(%p)\n", This, chars);
2235 if(!chars)
2236 return E_POINTER;
2238 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2240 return S_OK;
2243 static const IAudioClockVtbl AudioClock_Vtbl =
2245 AudioClock_QueryInterface,
2246 AudioClock_AddRef,
2247 AudioClock_Release,
2248 AudioClock_GetFrequency,
2249 AudioClock_GetPosition,
2250 AudioClock_GetCharacteristics
2253 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2254 REFIID riid, void **ppv)
2256 ACImpl *This = impl_from_IAudioClock2(iface);
2257 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2260 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2262 ACImpl *This = impl_from_IAudioClock2(iface);
2263 return IAudioClient_AddRef(&This->IAudioClient_iface);
2266 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2268 ACImpl *This = impl_from_IAudioClock2(iface);
2269 return IAudioClient_Release(&This->IAudioClient_iface);
2272 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2273 UINT64 *pos, UINT64 *qpctime)
2275 ACImpl *This = impl_from_IAudioClock2(iface);
2277 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2279 return E_NOTIMPL;
2282 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2284 AudioClock2_QueryInterface,
2285 AudioClock2_AddRef,
2286 AudioClock2_Release,
2287 AudioClock2_GetDevicePosition
2290 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2292 AudioSessionWrapper *ret;
2294 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2295 sizeof(AudioSessionWrapper));
2296 if(!ret)
2297 return NULL;
2299 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2300 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2301 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2303 ret->ref = 1;
2305 ret->client = client;
2306 if(client){
2307 ret->session = client->session;
2308 AudioClient_AddRef(&client->IAudioClient_iface);
2311 return ret;
2314 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2315 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2317 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2319 if(!ppv)
2320 return E_POINTER;
2321 *ppv = NULL;
2323 if(IsEqualIID(riid, &IID_IUnknown) ||
2324 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2325 IsEqualIID(riid, &IID_IAudioSessionControl2))
2326 *ppv = iface;
2327 if(*ppv){
2328 IUnknown_AddRef((IUnknown*)*ppv);
2329 return S_OK;
2332 WARN("Unknown interface %s\n", debugstr_guid(riid));
2333 return E_NOINTERFACE;
2336 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2338 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2339 ULONG ref;
2340 ref = InterlockedIncrement(&This->ref);
2341 TRACE("(%p) Refcount now %u\n", This, ref);
2342 return ref;
2345 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2347 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2348 ULONG ref;
2349 ref = InterlockedDecrement(&This->ref);
2350 TRACE("(%p) Refcount now %u\n", This, ref);
2351 if(!ref){
2352 if(This->client){
2353 EnterCriticalSection(&This->client->lock);
2354 This->client->session_wrapper = NULL;
2355 LeaveCriticalSection(&This->client->lock);
2356 AudioClient_Release(&This->client->IAudioClient_iface);
2358 HeapFree(GetProcessHeap(), 0, This);
2360 return ref;
2363 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2364 AudioSessionState *state)
2366 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2367 ACImpl *client;
2369 TRACE("(%p)->(%p)\n", This, state);
2371 if(!state)
2372 return NULL_PTR_ERR;
2374 EnterCriticalSection(&g_sessions_lock);
2376 if(list_empty(&This->session->clients)){
2377 *state = AudioSessionStateExpired;
2378 LeaveCriticalSection(&g_sessions_lock);
2379 return S_OK;
2382 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2383 EnterCriticalSection(&client->lock);
2384 if(client->started){
2385 *state = AudioSessionStateActive;
2386 LeaveCriticalSection(&client->lock);
2387 LeaveCriticalSection(&g_sessions_lock);
2388 return S_OK;
2390 LeaveCriticalSection(&client->lock);
2393 LeaveCriticalSection(&g_sessions_lock);
2395 *state = AudioSessionStateInactive;
2397 return S_OK;
2400 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2401 IAudioSessionControl2 *iface, WCHAR **name)
2403 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2405 FIXME("(%p)->(%p) - stub\n", This, name);
2407 return E_NOTIMPL;
2410 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2411 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2413 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2415 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2417 return E_NOTIMPL;
2420 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2421 IAudioSessionControl2 *iface, WCHAR **path)
2423 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2425 FIXME("(%p)->(%p) - stub\n", This, path);
2427 return E_NOTIMPL;
2430 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2431 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2433 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2435 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2437 return E_NOTIMPL;
2440 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2441 IAudioSessionControl2 *iface, GUID *group)
2443 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2445 FIXME("(%p)->(%p) - stub\n", This, group);
2447 return E_NOTIMPL;
2450 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2451 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2453 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2455 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2456 debugstr_guid(session));
2458 return E_NOTIMPL;
2461 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2462 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2464 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2466 FIXME("(%p)->(%p) - stub\n", This, events);
2468 return S_OK;
2471 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2472 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2474 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2476 FIXME("(%p)->(%p) - stub\n", This, events);
2478 return S_OK;
2481 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2482 IAudioSessionControl2 *iface, WCHAR **id)
2484 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2486 FIXME("(%p)->(%p) - stub\n", This, id);
2488 return E_NOTIMPL;
2491 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2492 IAudioSessionControl2 *iface, WCHAR **id)
2494 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2496 FIXME("(%p)->(%p) - stub\n", This, id);
2498 return E_NOTIMPL;
2501 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2502 IAudioSessionControl2 *iface, DWORD *pid)
2504 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2506 TRACE("(%p)->(%p)\n", This, pid);
2508 if(!pid)
2509 return E_POINTER;
2511 *pid = GetCurrentProcessId();
2513 return S_OK;
2516 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2517 IAudioSessionControl2 *iface)
2519 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2521 TRACE("(%p)\n", This);
2523 return S_FALSE;
2526 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2527 IAudioSessionControl2 *iface, BOOL optout)
2529 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2531 TRACE("(%p)->(%d)\n", This, optout);
2533 return S_OK;
2536 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2538 AudioSessionControl_QueryInterface,
2539 AudioSessionControl_AddRef,
2540 AudioSessionControl_Release,
2541 AudioSessionControl_GetState,
2542 AudioSessionControl_GetDisplayName,
2543 AudioSessionControl_SetDisplayName,
2544 AudioSessionControl_GetIconPath,
2545 AudioSessionControl_SetIconPath,
2546 AudioSessionControl_GetGroupingParam,
2547 AudioSessionControl_SetGroupingParam,
2548 AudioSessionControl_RegisterAudioSessionNotification,
2549 AudioSessionControl_UnregisterAudioSessionNotification,
2550 AudioSessionControl_GetSessionIdentifier,
2551 AudioSessionControl_GetSessionInstanceIdentifier,
2552 AudioSessionControl_GetProcessId,
2553 AudioSessionControl_IsSystemSoundsSession,
2554 AudioSessionControl_SetDuckingPreference
2557 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2558 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2560 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2562 if(!ppv)
2563 return E_POINTER;
2564 *ppv = NULL;
2566 if(IsEqualIID(riid, &IID_IUnknown) ||
2567 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2568 *ppv = iface;
2569 if(*ppv){
2570 IUnknown_AddRef((IUnknown*)*ppv);
2571 return S_OK;
2574 WARN("Unknown interface %s\n", debugstr_guid(riid));
2575 return E_NOINTERFACE;
2578 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2580 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2581 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2584 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2586 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2587 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2590 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2591 ISimpleAudioVolume *iface, float level, const GUID *context)
2593 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2594 AudioSession *session = This->session;
2596 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2598 if(level < 0.f || level > 1.f)
2599 return E_INVALIDARG;
2601 if(context)
2602 FIXME("Notifications not supported yet\n");
2604 TRACE("ALSA does not support volume control\n");
2606 EnterCriticalSection(&session->lock);
2608 session->master_vol = level;
2610 LeaveCriticalSection(&session->lock);
2612 return S_OK;
2615 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2616 ISimpleAudioVolume *iface, float *level)
2618 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2619 AudioSession *session = This->session;
2621 TRACE("(%p)->(%p)\n", session, level);
2623 if(!level)
2624 return NULL_PTR_ERR;
2626 *level = session->master_vol;
2628 return S_OK;
2631 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2632 BOOL mute, const GUID *context)
2634 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2635 AudioSession *session = This->session;
2637 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2639 if(context)
2640 FIXME("Notifications not supported yet\n");
2642 session->mute = mute;
2644 return S_OK;
2647 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2648 BOOL *mute)
2650 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2651 AudioSession *session = This->session;
2653 TRACE("(%p)->(%p)\n", session, mute);
2655 if(!mute)
2656 return NULL_PTR_ERR;
2658 *mute = session->mute;
2660 return S_OK;
2663 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2665 SimpleAudioVolume_QueryInterface,
2666 SimpleAudioVolume_AddRef,
2667 SimpleAudioVolume_Release,
2668 SimpleAudioVolume_SetMasterVolume,
2669 SimpleAudioVolume_GetMasterVolume,
2670 SimpleAudioVolume_SetMute,
2671 SimpleAudioVolume_GetMute
2674 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2675 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2677 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2679 if(!ppv)
2680 return E_POINTER;
2681 *ppv = NULL;
2683 if(IsEqualIID(riid, &IID_IUnknown) ||
2684 IsEqualIID(riid, &IID_IAudioStreamVolume))
2685 *ppv = iface;
2686 if(*ppv){
2687 IUnknown_AddRef((IUnknown*)*ppv);
2688 return S_OK;
2691 WARN("Unknown interface %s\n", debugstr_guid(riid));
2692 return E_NOINTERFACE;
2695 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2697 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2698 return IAudioClient_AddRef(&This->IAudioClient_iface);
2701 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2703 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2704 return IAudioClient_Release(&This->IAudioClient_iface);
2707 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2708 IAudioStreamVolume *iface, UINT32 *out)
2710 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2712 TRACE("(%p)->(%p)\n", This, out);
2714 if(!out)
2715 return E_POINTER;
2717 *out = This->fmt->nChannels;
2719 return S_OK;
2722 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2723 IAudioStreamVolume *iface, UINT32 index, float level)
2725 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2727 TRACE("(%p)->(%d, %f)\n", This, index, level);
2729 if(level < 0.f || level > 1.f)
2730 return E_INVALIDARG;
2732 if(index >= This->fmt->nChannels)
2733 return E_INVALIDARG;
2735 TRACE("ALSA does not support volume control\n");
2737 EnterCriticalSection(&This->lock);
2739 This->vols[index] = level;
2741 LeaveCriticalSection(&This->lock);
2743 return S_OK;
2746 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2747 IAudioStreamVolume *iface, UINT32 index, float *level)
2749 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2751 TRACE("(%p)->(%d, %p)\n", This, index, level);
2753 if(!level)
2754 return E_POINTER;
2756 if(index >= This->fmt->nChannels)
2757 return E_INVALIDARG;
2759 *level = This->vols[index];
2761 return S_OK;
2764 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2765 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2767 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2768 int i;
2770 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2772 if(!levels)
2773 return E_POINTER;
2775 if(count != This->fmt->nChannels)
2776 return E_INVALIDARG;
2778 TRACE("ALSA does not support volume control\n");
2780 EnterCriticalSection(&This->lock);
2782 for(i = 0; i < count; ++i)
2783 This->vols[i] = levels[i];
2785 LeaveCriticalSection(&This->lock);
2787 return S_OK;
2790 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2791 IAudioStreamVolume *iface, UINT32 count, float *levels)
2793 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2794 int i;
2796 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2798 if(!levels)
2799 return E_POINTER;
2801 if(count != This->fmt->nChannels)
2802 return E_INVALIDARG;
2804 EnterCriticalSection(&This->lock);
2806 for(i = 0; i < count; ++i)
2807 levels[i] = This->vols[i];
2809 LeaveCriticalSection(&This->lock);
2811 return S_OK;
2814 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2816 AudioStreamVolume_QueryInterface,
2817 AudioStreamVolume_AddRef,
2818 AudioStreamVolume_Release,
2819 AudioStreamVolume_GetChannelCount,
2820 AudioStreamVolume_SetChannelVolume,
2821 AudioStreamVolume_GetChannelVolume,
2822 AudioStreamVolume_SetAllVolumes,
2823 AudioStreamVolume_GetAllVolumes
2826 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2827 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2829 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2831 if(!ppv)
2832 return E_POINTER;
2833 *ppv = NULL;
2835 if(IsEqualIID(riid, &IID_IUnknown) ||
2836 IsEqualIID(riid, &IID_IChannelAudioVolume))
2837 *ppv = iface;
2838 if(*ppv){
2839 IUnknown_AddRef((IUnknown*)*ppv);
2840 return S_OK;
2843 WARN("Unknown interface %s\n", debugstr_guid(riid));
2844 return E_NOINTERFACE;
2847 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2849 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2850 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2853 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2855 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2856 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2859 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2860 IChannelAudioVolume *iface, UINT32 *out)
2862 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2863 AudioSession *session = This->session;
2865 TRACE("(%p)->(%p)\n", session, out);
2867 if(!out)
2868 return NULL_PTR_ERR;
2870 *out = session->channel_count;
2872 return S_OK;
2875 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2876 IChannelAudioVolume *iface, UINT32 index, float level,
2877 const GUID *context)
2879 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2880 AudioSession *session = This->session;
2882 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2883 wine_dbgstr_guid(context));
2885 if(level < 0.f || level > 1.f)
2886 return E_INVALIDARG;
2888 if(index >= session->channel_count)
2889 return E_INVALIDARG;
2891 if(context)
2892 FIXME("Notifications not supported yet\n");
2894 TRACE("ALSA does not support volume control\n");
2896 EnterCriticalSection(&session->lock);
2898 session->channel_vols[index] = level;
2900 LeaveCriticalSection(&session->lock);
2902 return S_OK;
2905 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2906 IChannelAudioVolume *iface, UINT32 index, float *level)
2908 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2909 AudioSession *session = This->session;
2911 TRACE("(%p)->(%d, %p)\n", session, index, level);
2913 if(!level)
2914 return NULL_PTR_ERR;
2916 if(index >= session->channel_count)
2917 return E_INVALIDARG;
2919 *level = session->channel_vols[index];
2921 return S_OK;
2924 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2925 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2926 const GUID *context)
2928 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2929 AudioSession *session = This->session;
2930 int i;
2932 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2933 wine_dbgstr_guid(context));
2935 if(!levels)
2936 return NULL_PTR_ERR;
2938 if(count != session->channel_count)
2939 return E_INVALIDARG;
2941 if(context)
2942 FIXME("Notifications not supported yet\n");
2944 TRACE("ALSA does not support volume control\n");
2946 EnterCriticalSection(&session->lock);
2948 for(i = 0; i < count; ++i)
2949 session->channel_vols[i] = levels[i];
2951 LeaveCriticalSection(&session->lock);
2953 return S_OK;
2956 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2957 IChannelAudioVolume *iface, UINT32 count, float *levels)
2959 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2960 AudioSession *session = This->session;
2961 int i;
2963 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2965 if(!levels)
2966 return NULL_PTR_ERR;
2968 if(count != session->channel_count)
2969 return E_INVALIDARG;
2971 for(i = 0; i < count; ++i)
2972 levels[i] = session->channel_vols[i];
2974 return S_OK;
2977 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2979 ChannelAudioVolume_QueryInterface,
2980 ChannelAudioVolume_AddRef,
2981 ChannelAudioVolume_Release,
2982 ChannelAudioVolume_GetChannelCount,
2983 ChannelAudioVolume_SetChannelVolume,
2984 ChannelAudioVolume_GetChannelVolume,
2985 ChannelAudioVolume_SetAllVolumes,
2986 ChannelAudioVolume_GetAllVolumes
2989 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2990 REFIID riid, void **ppv)
2992 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2994 if(!ppv)
2995 return E_POINTER;
2996 *ppv = NULL;
2998 if(IsEqualIID(riid, &IID_IUnknown) ||
2999 IsEqualIID(riid, &IID_IAudioSessionManager) ||
3000 IsEqualIID(riid, &IID_IAudioSessionManager2))
3001 *ppv = iface;
3002 if(*ppv){
3003 IUnknown_AddRef((IUnknown*)*ppv);
3004 return S_OK;
3007 WARN("Unknown interface %s\n", debugstr_guid(riid));
3008 return E_NOINTERFACE;
3011 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
3013 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3014 ULONG ref;
3015 ref = InterlockedIncrement(&This->ref);
3016 TRACE("(%p) Refcount now %u\n", This, ref);
3017 return ref;
3020 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
3022 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3023 ULONG ref;
3024 ref = InterlockedDecrement(&This->ref);
3025 TRACE("(%p) Refcount now %u\n", This, ref);
3026 if(!ref)
3027 HeapFree(GetProcessHeap(), 0, This);
3028 return ref;
3031 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
3032 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3033 IAudioSessionControl **out)
3035 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3036 AudioSession *session;
3037 AudioSessionWrapper *wrapper;
3038 HRESULT hr;
3040 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3041 flags, out);
3043 hr = get_audio_session(session_guid, This->device, 0, &session);
3044 if(FAILED(hr))
3045 return hr;
3047 wrapper = AudioSessionWrapper_Create(NULL);
3048 if(!wrapper)
3049 return E_OUTOFMEMORY;
3051 wrapper->session = session;
3053 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3055 return S_OK;
3058 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
3059 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3060 ISimpleAudioVolume **out)
3062 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3063 AudioSession *session;
3064 AudioSessionWrapper *wrapper;
3065 HRESULT hr;
3067 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3068 flags, out);
3070 hr = get_audio_session(session_guid, This->device, 0, &session);
3071 if(FAILED(hr))
3072 return hr;
3074 wrapper = AudioSessionWrapper_Create(NULL);
3075 if(!wrapper)
3076 return E_OUTOFMEMORY;
3078 wrapper->session = session;
3080 *out = &wrapper->ISimpleAudioVolume_iface;
3082 return S_OK;
3085 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
3086 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
3088 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3089 FIXME("(%p)->(%p) - stub\n", This, out);
3090 return E_NOTIMPL;
3093 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
3094 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3096 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3097 FIXME("(%p)->(%p) - stub\n", This, notification);
3098 return E_NOTIMPL;
3101 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
3102 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3104 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3105 FIXME("(%p)->(%p) - stub\n", This, notification);
3106 return E_NOTIMPL;
3109 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
3110 IAudioSessionManager2 *iface, const WCHAR *session_id,
3111 IAudioVolumeDuckNotification *notification)
3113 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3114 FIXME("(%p)->(%p) - stub\n", This, notification);
3115 return E_NOTIMPL;
3118 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
3119 IAudioSessionManager2 *iface,
3120 IAudioVolumeDuckNotification *notification)
3122 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3123 FIXME("(%p)->(%p) - stub\n", This, notification);
3124 return E_NOTIMPL;
3127 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
3129 AudioSessionManager_QueryInterface,
3130 AudioSessionManager_AddRef,
3131 AudioSessionManager_Release,
3132 AudioSessionManager_GetAudioSessionControl,
3133 AudioSessionManager_GetSimpleAudioVolume,
3134 AudioSessionManager_GetSessionEnumerator,
3135 AudioSessionManager_RegisterSessionNotification,
3136 AudioSessionManager_UnregisterSessionNotification,
3137 AudioSessionManager_RegisterDuckNotification,
3138 AudioSessionManager_UnregisterDuckNotification
3141 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3142 IAudioSessionManager2 **out)
3144 SessionMgr *This;
3146 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3147 if(!This)
3148 return E_OUTOFMEMORY;
3150 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3151 This->device = device;
3152 This->ref = 1;
3154 *out = &This->IAudioSessionManager2_iface;
3156 return S_OK;