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
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "wine/list.h"
36 #include "mmdeviceapi.h"
42 #include "endpointvolume.h"
43 #include "audioclient.h"
44 #include "audiopolicy.h"
46 #include <alsa/asoundlib.h>
48 WINE_DEFAULT_DEBUG_CHANNEL(alsa);
49 WINE_DECLARE_DEBUG_CHANNEL(winediag);
51 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
53 static const REFERENCE_TIME DefaultPeriod = 100000;
54 static const REFERENCE_TIME MinimumPeriod = 50000;
57 typedef struct ACImpl ACImpl;
59 typedef struct _AudioSession {
70 CRITICAL_SECTION lock;
75 typedef struct _AudioSessionWrapper {
76 IAudioSessionControl2 IAudioSessionControl2_iface;
77 IChannelAudioVolume IChannelAudioVolume_iface;
78 ISimpleAudioVolume ISimpleAudioVolume_iface;
83 AudioSession *session;
84 } AudioSessionWrapper;
87 IAudioClient IAudioClient_iface;
88 IAudioRenderClient IAudioRenderClient_iface;
89 IAudioCaptureClient IAudioCaptureClient_iface;
90 IAudioClock IAudioClock_iface;
91 IAudioClock2 IAudioClock2_iface;
92 IAudioStreamVolume IAudioStreamVolume_iface;
96 snd_pcm_t *pcm_handle;
97 snd_pcm_uframes_t alsa_bufsize_frames, alsa_period_frames;
98 snd_pcm_hw_params_t *hw_params; /* does not hold state between calls */
99 snd_pcm_format_t alsa_format;
106 AUDCLNT_SHAREMODE share;
110 BOOL initted, started;
111 REFERENCE_TIME mmdev_period_rt;
112 UINT64 written_frames, last_pos_frames;
113 UINT32 bufsize_frames, held_frames, tmp_buffer_frames, mmdev_period_frames;
114 UINT32 lcl_offs_frames; /* offs into local_buffer where valid data starts */
117 BYTE *local_buffer, *tmp_buffer;
119 long getbuf_last; /* <0 when using tmp_buffer */
121 CRITICAL_SECTION lock;
123 AudioSession *session;
124 AudioSessionWrapper *session_wrapper;
131 LOCKED_NORMAL, /* public buffer piece is from local_buffer */
132 LOCKED_WRAPPED /* public buffer piece is wrapped around, in tmp_buffer */
135 typedef struct _SessionMgr {
136 IAudioSessionManager2 IAudioSessionManager2_iface;
143 static HANDLE g_timer_q;
145 static CRITICAL_SECTION g_sessions_lock;
146 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug =
148 0, 0, &g_sessions_lock,
149 { &g_sessions_lock_debug.ProcessLocksList, &g_sessions_lock_debug.ProcessLocksList },
150 0, 0, { (DWORD_PTR)(__FILE__ ": g_sessions_lock") }
152 static CRITICAL_SECTION g_sessions_lock = { &g_sessions_lock_debug, -1, 0, 0, 0, 0 };
153 static struct list g_sessions = LIST_INIT(g_sessions);
155 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
156 static const char defname[] = "default";
158 static const IAudioClientVtbl AudioClient_Vtbl;
159 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
160 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
161 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
162 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
163 static const IAudioClockVtbl AudioClock_Vtbl;
164 static const IAudioClock2Vtbl AudioClock2_Vtbl;
165 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
166 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
167 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl;
169 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
171 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
173 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
176 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
178 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
181 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
183 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
186 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
188 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
191 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
193 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
196 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
198 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
201 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
203 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
206 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
208 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
211 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
213 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
216 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
218 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
221 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
225 case DLL_PROCESS_ATTACH:
226 g_timer_q = CreateTimerQueue();
231 case DLL_PROCESS_DETACH:
232 DeleteCriticalSection(&g_sessions_lock);
238 /* From <dlls/mmdevapi/mmdevapi.h> */
239 enum DriverPriority {
240 Priority_Unavailable = 0,
246 int WINAPI AUDDRV_GetPriority(void)
248 return Priority_Neutral;
251 static BOOL alsa_try_open(const char *devnode, snd_pcm_stream_t stream)
256 if((err = snd_pcm_open(&handle, devnode, stream, SND_PCM_NONBLOCK)) < 0){
257 WARN("The device \"%s\" failed to open: %d (%s).\n",
258 devnode, err, snd_strerror(err));
262 snd_pcm_close(handle);
266 static HRESULT alsa_get_card_devices(snd_pcm_stream_t stream, WCHAR **ids, char **keys,
267 UINT *num, snd_ctl_t *ctl, int card, const WCHAR *cardnameW)
269 static const WCHAR dashW[] = {' ','-',' ',0};
271 snd_pcm_info_t *info;
273 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_info_sizeof());
275 return E_OUTOFMEMORY;
277 snd_pcm_info_set_subdevice(info, 0);
278 snd_pcm_info_set_stream(info, stream);
281 for(err = snd_ctl_pcm_next_device(ctl, &device); device != -1 && err >= 0;
282 err = snd_ctl_pcm_next_device(ctl, &device)){
286 snd_pcm_info_set_device(info, device);
288 if((err = snd_ctl_pcm_info(ctl, info)) < 0){
290 /* This device doesn't have the right stream direction */
293 WARN("Failed to get info for card %d, device %d: %d (%s)\n",
294 card, device, err, snd_strerror(err));
298 sprintf(devnode, "plughw:%d,%d", card, device);
299 if(!alsa_try_open(devnode, stream))
305 devname = snd_pcm_info_get_name(info);
307 WARN("Unable to get device name for card %d, device %d\n", card,
312 cardlen = lstrlenW(cardnameW);
313 len = MultiByteToWideChar(CP_UNIXCP, 0, devname, -1, NULL, 0);
314 len += lstrlenW(dashW);
316 ids[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
318 HeapFree(GetProcessHeap(), 0, info);
319 return E_OUTOFMEMORY;
321 memcpy(ids[*num], cardnameW, cardlen * sizeof(WCHAR));
322 memcpy(ids[*num] + cardlen, dashW, lstrlenW(dashW) * sizeof(WCHAR));
323 cardlen += lstrlenW(dashW);
324 MultiByteToWideChar(CP_UNIXCP, 0, devname, -1, ids[*num] + cardlen,
327 keys[*num] = HeapAlloc(GetProcessHeap(), 0, 32);
329 HeapFree(GetProcessHeap(), 0, info);
330 HeapFree(GetProcessHeap(), 0, ids[*num]);
331 return E_OUTOFMEMORY;
333 memcpy(keys[*num], devnode, sizeof(devnode));
339 HeapFree(GetProcessHeap(), 0, info);
342 WARN("Got a failure during device enumeration on card %d: %d (%s)\n",
343 card, err, snd_strerror(err));
348 static HRESULT alsa_enum_devices(EDataFlow flow, WCHAR **ids, char **keys,
351 snd_pcm_stream_t stream = (flow == eRender ? SND_PCM_STREAM_PLAYBACK :
352 SND_PCM_STREAM_CAPTURE);
358 if(alsa_try_open(defname, stream)){
360 *ids = HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW));
361 memcpy(*ids, defaultW, sizeof(defaultW));
362 *keys = HeapAlloc(GetProcessHeap(), 0, sizeof(defname));
363 memcpy(*keys, defname, sizeof(defname));
368 for(err = snd_card_next(&card); card != -1 && err >= 0;
369 err = snd_card_next(&card)){
371 const char *cardname;
376 sprintf(cardpath, "hw:%u", card);
378 if((err = snd_ctl_open(&ctl, cardpath, 0)) < 0){
379 WARN("Unable to open ctl for ALSA device %s: %d (%s)\n", cardpath,
380 err, snd_strerror(err));
384 if((err = snd_card_get_name(card, (char **)&cardname)) < 0){
385 WARN("Unable to get card name for ALSA device %s: %d (%s)\n",
386 cardpath, err, snd_strerror(err));
387 /* FIXME: Should be localized */
388 cardname = "Unknown soundcard";
391 len = MultiByteToWideChar(CP_UNIXCP, 0, cardname, -1, NULL, 0);
392 cardnameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
395 return E_OUTOFMEMORY;
397 MultiByteToWideChar(CP_UNIXCP, 0, cardname, -1, cardnameW, len);
399 alsa_get_card_devices(stream, ids, keys, num, ctl, card, cardnameW);
401 HeapFree(GetProcessHeap(), 0, cardnameW);
407 WARN("Got a failure during card enumeration: %d (%s)\n",
408 err, snd_strerror(err));
413 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, char ***keys,
414 UINT *num, UINT *def_index)
418 TRACE("%d %p %p %p %p\n", flow, ids, keys, num, def_index);
420 hr = alsa_enum_devices(flow, NULL, NULL, num);
431 *ids = HeapAlloc(GetProcessHeap(), 0, *num * sizeof(WCHAR *));
432 *keys = HeapAlloc(GetProcessHeap(), 0, *num * sizeof(char *));
434 HeapFree(GetProcessHeap(), 0, *ids);
435 HeapFree(GetProcessHeap(), 0, *keys);
436 return E_OUTOFMEMORY;
441 hr = alsa_enum_devices(flow, *ids, *keys, num);
444 for(i = 0; i < *num; ++i){
445 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
446 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
448 HeapFree(GetProcessHeap(), 0, *ids);
449 HeapFree(GetProcessHeap(), 0, *keys);
450 return E_OUTOFMEMORY;
456 /* Using the pulse PCM device from alsa-plugins 1.0.24 triggers a bug
457 * which causes audio to cease playing after a few minutes of playback.
458 * Setting handle_underrun=1 on pulse-backed ALSA devices seems to work
459 * around this issue. */
460 static snd_config_t *make_handle_underrun_config(const char *name)
462 snd_config_t *lconf, *dev_node, *hu_node, *type_node;
463 char dev_node_name[64];
464 const char *type_str;
469 if((err = snd_config_copy(&lconf, snd_config)) < 0){
470 WARN("snd_config_copy failed: %d (%s)\n", err, snd_strerror(err));
474 sprintf(dev_node_name, "pcm.%s", name);
475 err = snd_config_search(lconf, dev_node_name, &dev_node);
477 snd_config_delete(lconf);
481 snd_config_delete(lconf);
482 WARN("snd_config_search failed: %d (%s)\n", err, snd_strerror(err));
486 /* ALSA is extremely fragile. If it runs into a config setting it doesn't
487 * recognize, it tends to fail or assert. So we only want to inject
488 * handle_underrun=1 on devices that we know will recognize it. */
489 err = snd_config_search(dev_node, "type", &type_node);
491 snd_config_delete(lconf);
495 snd_config_delete(lconf);
496 WARN("snd_config_search failed: %d (%s)\n", err, snd_strerror(err));
500 if((err = snd_config_get_string(type_node, &type_str)) < 0){
501 snd_config_delete(lconf);
505 if(strcmp(type_str, "pulse") != 0){
506 snd_config_delete(lconf);
510 err = snd_config_search(dev_node, "handle_underrun", &hu_node);
512 /* user already has an explicit handle_underrun setting, so don't
513 * use a local config */
514 snd_config_delete(lconf);
518 snd_config_delete(lconf);
519 WARN("snd_config_search failed: %d (%s)\n", err, snd_strerror(err));
523 if((err = snd_config_imake_integer(&hu_node, "handle_underrun", 1)) < 0){
524 snd_config_delete(lconf);
525 WARN("snd_config_imake_integer failed: %d (%s)\n", err,
530 if((err = snd_config_add(dev_node, hu_node)) < 0){
531 snd_config_delete(lconf);
532 WARN("snd_config_add failed: %d (%s)\n", err, snd_strerror(err));
539 HRESULT WINAPI AUDDRV_GetAudioEndpoint(const char *key, IMMDevice *dev,
540 EDataFlow dataflow, IAudioClient **out)
544 snd_pcm_stream_t stream;
546 static int handle_underrun = 1;
548 TRACE("\"%s\" %p %d %p\n", key, dev, dataflow, out);
550 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
552 return E_OUTOFMEMORY;
554 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
555 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
556 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
557 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
558 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
559 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
561 if(dataflow == eRender)
562 stream = SND_PCM_STREAM_PLAYBACK;
563 else if(dataflow == eCapture)
564 stream = SND_PCM_STREAM_CAPTURE;
566 HeapFree(GetProcessHeap(), 0, This);
570 This->dataflow = dataflow;
571 if(handle_underrun && ((lconf = make_handle_underrun_config(key)))){
572 err = snd_pcm_open_lconf(&This->pcm_handle, key, stream, SND_PCM_NONBLOCK, lconf);
573 TRACE("Opening PCM device \"%s\" with handle_underrun: %d\n", key, err);
574 snd_config_delete(lconf);
575 /* Pulse <= 2010 returns EINVAL, it does not know handle_underrun. */
577 ERR_(winediag)("PulseAudio \"%s\" %d without handle_underrun. Audio may hang."
578 " Please upgrade to alsa_plugins >= 1.0.24\n", key, err);
584 err = snd_pcm_open(&This->pcm_handle, key, stream, SND_PCM_NONBLOCK);
587 HeapFree(GetProcessHeap(), 0, This);
588 WARN("Unable to open PCM \"%s\": %d (%s)\n", key, err, snd_strerror(err));
592 This->hw_params = HeapAlloc(GetProcessHeap(), 0,
593 snd_pcm_hw_params_sizeof());
594 if(!This->hw_params){
595 snd_pcm_close(This->pcm_handle);
596 HeapFree(GetProcessHeap(), 0, This);
597 return E_OUTOFMEMORY;
600 InitializeCriticalSection(&This->lock);
601 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ACImpl.lock");
604 IMMDevice_AddRef(This->parent);
606 *out = &This->IAudioClient_iface;
607 IAudioClient_AddRef(&This->IAudioClient_iface);
612 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
613 REFIID riid, void **ppv)
615 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
620 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
623 IUnknown_AddRef((IUnknown*)*ppv);
626 WARN("Unknown interface %s\n", debugstr_guid(riid));
627 return E_NOINTERFACE;
630 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
632 ACImpl *This = impl_from_IAudioClient(iface);
634 ref = InterlockedIncrement(&This->ref);
635 TRACE("(%p) Refcount now %u\n", This, ref);
639 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
641 ACImpl *This = impl_from_IAudioClient(iface);
643 ref = InterlockedDecrement(&This->ref);
644 TRACE("(%p) Refcount now %u\n", This, ref);
646 IAudioClient_Stop(iface);
647 IMMDevice_Release(This->parent);
648 This->lock.DebugInfo->Spare[0] = 0;
649 DeleteCriticalSection(&This->lock);
650 snd_pcm_drop(This->pcm_handle);
651 snd_pcm_close(This->pcm_handle);
653 EnterCriticalSection(&g_sessions_lock);
654 list_remove(&This->entry);
655 LeaveCriticalSection(&g_sessions_lock);
657 HeapFree(GetProcessHeap(), 0, This->vols);
658 HeapFree(GetProcessHeap(), 0, This->local_buffer);
659 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
660 HeapFree(GetProcessHeap(), 0, This->hw_params);
661 CoTaskMemFree(This->fmt);
662 HeapFree(GetProcessHeap(), 0, This);
667 static void dump_fmt(const WAVEFORMATEX *fmt)
669 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
670 switch(fmt->wFormatTag){
671 case WAVE_FORMAT_PCM:
672 TRACE("WAVE_FORMAT_PCM");
674 case WAVE_FORMAT_IEEE_FLOAT:
675 TRACE("WAVE_FORMAT_IEEE_FLOAT");
677 case WAVE_FORMAT_EXTENSIBLE:
678 TRACE("WAVE_FORMAT_EXTENSIBLE");
686 TRACE("nChannels: %u\n", fmt->nChannels);
687 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
688 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
689 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
690 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
691 TRACE("cbSize: %u\n", fmt->cbSize);
693 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
694 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
695 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
696 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
697 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
701 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
706 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
707 size = sizeof(WAVEFORMATEXTENSIBLE);
709 size = sizeof(WAVEFORMATEX);
711 ret = CoTaskMemAlloc(size);
715 memcpy(ret, fmt, size);
717 ret->cbSize = size - sizeof(WAVEFORMATEX);
722 static snd_pcm_format_t alsa_format(const WAVEFORMATEX *fmt)
724 snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;
725 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
727 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
728 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
729 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
730 if(fmt->wBitsPerSample == 8)
731 format = SND_PCM_FORMAT_U8;
732 else if(fmt->wBitsPerSample == 16)
733 format = SND_PCM_FORMAT_S16_LE;
734 else if(fmt->wBitsPerSample == 24)
735 format = SND_PCM_FORMAT_S24_3LE;
736 else if(fmt->wBitsPerSample == 32)
737 format = SND_PCM_FORMAT_S32_LE;
739 WARN("Unsupported bit depth: %u\n", fmt->wBitsPerSample);
740 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
741 fmt->wBitsPerSample != fmtex->Samples.wValidBitsPerSample){
742 if(fmtex->Samples.wValidBitsPerSample == 20 && fmt->wBitsPerSample == 24)
743 format = SND_PCM_FORMAT_S20_3LE;
745 WARN("Unsupported ValidBits: %u\n", fmtex->Samples.wValidBitsPerSample);
746 format = SND_PCM_FORMAT_UNKNOWN;
749 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
750 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
751 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
752 if(fmt->wBitsPerSample == 32)
753 format = SND_PCM_FORMAT_FLOAT_LE;
754 else if(fmt->wBitsPerSample == 64)
755 format = SND_PCM_FORMAT_FLOAT64_LE;
757 WARN("Unsupported float size: %u\n", fmt->wBitsPerSample);
759 WARN("Unknown wave format: %04x\n", fmt->wFormatTag);
763 static void session_init_vols(AudioSession *session, UINT channels)
765 if(session->channel_count < channels){
768 if(session->channel_vols)
769 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
770 session->channel_vols, sizeof(float) * channels);
772 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
773 sizeof(float) * channels);
774 if(!session->channel_vols)
777 for(i = session->channel_count; i < channels; ++i)
778 session->channel_vols[i] = 1.f;
780 session->channel_count = channels;
784 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
789 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
793 memcpy(&ret->guid, guid, sizeof(GUID));
795 ret->device = device;
797 list_init(&ret->clients);
799 list_add_head(&g_sessions, &ret->entry);
801 InitializeCriticalSection(&ret->lock);
802 ret->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AudioSession.lock");
804 session_init_vols(ret, num_channels);
806 ret->master_vol = 1.f;
811 /* if channels == 0, then this will return or create a session with
812 * matching dataflow and GUID. otherwise, channels must also match */
813 static HRESULT get_audio_session(const GUID *sessionguid,
814 IMMDevice *device, UINT channels, AudioSession **out)
816 AudioSession *session;
818 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
819 *out = create_session(&GUID_NULL, device, channels);
821 return E_OUTOFMEMORY;
827 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
828 if(session->device == device &&
829 IsEqualGUID(sessionguid, &session->guid)){
830 session_init_vols(session, channels);
837 *out = create_session(sessionguid, device, channels);
839 return E_OUTOFMEMORY;
845 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
846 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
847 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
848 const GUID *sessionguid)
850 ACImpl *This = impl_from_IAudioClient(iface);
851 snd_pcm_sw_params_t *sw_params = NULL;
852 snd_pcm_format_t format;
853 unsigned int rate, alsa_period_us;
857 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
858 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
863 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
864 return AUDCLNT_E_NOT_INITIALIZED;
866 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
867 AUDCLNT_STREAMFLAGS_LOOPBACK |
868 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
869 AUDCLNT_STREAMFLAGS_NOPERSIST |
870 AUDCLNT_STREAMFLAGS_RATEADJUST |
871 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
872 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
873 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
874 TRACE("Unknown flags: %08x\n", flags);
878 if(mode == AUDCLNT_SHAREMODE_SHARED){
879 period = DefaultPeriod;
880 if( duration < 3 * period)
881 duration = 3 * period;
884 period = DefaultPeriod; /* not minimum */
885 if(period < MinimumPeriod || period > 5000000)
886 return AUDCLNT_E_INVALID_DEVICE_PERIOD;
887 if(duration > 20000000) /* the smaller the period, the lower this limit */
888 return AUDCLNT_E_BUFFER_SIZE_ERROR;
889 if(flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK){
890 if(duration != period)
891 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL;
892 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
893 return AUDCLNT_E_DEVICE_IN_USE;
895 if( duration < 8 * period)
896 duration = 8 * period; /* may grow above 2s */
900 EnterCriticalSection(&This->lock);
903 LeaveCriticalSection(&This->lock);
904 return AUDCLNT_E_ALREADY_INITIALIZED;
909 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
910 WARN("Unable to get hw_params: %d (%s)\n", err, snd_strerror(err));
915 if((err = snd_pcm_hw_params_set_access(This->pcm_handle, This->hw_params,
916 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0){
917 WARN("Unable to set access: %d (%s)\n", err, snd_strerror(err));
922 format = alsa_format(fmt);
923 if (format == SND_PCM_FORMAT_UNKNOWN){
924 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
928 if((err = snd_pcm_hw_params_set_format(This->pcm_handle, This->hw_params,
930 WARN("Unable to set ALSA format to %u: %d (%s)\n", format, err,
936 This->alsa_format = format;
938 rate = fmt->nSamplesPerSec;
939 if((err = snd_pcm_hw_params_set_rate_near(This->pcm_handle, This->hw_params,
941 WARN("Unable to set rate to %u: %d (%s)\n", rate, err,
947 if((err = snd_pcm_hw_params_set_channels(This->pcm_handle, This->hw_params,
948 fmt->nChannels)) < 0){
949 WARN("Unable to set channels to %u: %d (%s)\n", fmt->nChannels, err,
955 This->mmdev_period_rt = period;
956 alsa_period_us = This->mmdev_period_rt / 10;
957 if((err = snd_pcm_hw_params_set_period_time_near(This->pcm_handle,
958 This->hw_params, &alsa_period_us, NULL)) < 0)
959 WARN("Unable to set period time near %u: %d (%s)\n", alsa_period_us,
960 err, snd_strerror(err));
961 /* ALSA updates the output variable alsa_period_us */
963 This->mmdev_period_frames = MulDiv(fmt->nSamplesPerSec,
964 This->mmdev_period_rt, 10000000);
966 This->bufsize_frames = MulDiv(duration, fmt->nSamplesPerSec, 10000000);
968 /* Buffer 4 ALSA periods if large enough, else 4 mmdevapi periods */
969 This->alsa_bufsize_frames = This->mmdev_period_frames * 4;
970 if(err < 0 || alsa_period_us < period / 10)
971 err = snd_pcm_hw_params_set_buffer_size_near(This->pcm_handle,
972 This->hw_params, &This->alsa_bufsize_frames);
974 unsigned int periods = 4;
975 err = snd_pcm_hw_params_set_periods_near(This->pcm_handle, This->hw_params, &periods, NULL);
978 WARN("Unable to set buffer size: %d (%s)\n", err, snd_strerror(err));
980 if((err = snd_pcm_hw_params(This->pcm_handle, This->hw_params)) < 0){
981 WARN("Unable to set hw params: %d (%s)\n", err, snd_strerror(err));
986 if((err = snd_pcm_hw_params_get_period_size(This->hw_params,
987 &This->alsa_period_frames, NULL)) < 0){
988 WARN("Unable to get period size: %d (%s)\n", err, snd_strerror(err));
993 if((err = snd_pcm_hw_params_get_buffer_size(This->hw_params,
994 &This->alsa_bufsize_frames)) < 0){
995 WARN("Unable to get buffer size: %d (%s)\n", err, snd_strerror(err));
1000 sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
1006 if((err = snd_pcm_sw_params_current(This->pcm_handle, sw_params)) < 0){
1007 WARN("Unable to get sw_params: %d (%s)\n", err, snd_strerror(err));
1012 if((err = snd_pcm_sw_params_set_start_threshold(This->pcm_handle,
1013 sw_params, 1)) < 0){
1014 WARN("Unable set start threshold to 0: %d (%s)\n", err, snd_strerror(err));
1019 if((err = snd_pcm_sw_params_set_stop_threshold(This->pcm_handle,
1020 sw_params, This->alsa_bufsize_frames)) < 0){
1021 WARN("Unable set stop threshold to %lu: %d (%s)\n",
1022 This->alsa_bufsize_frames, err, snd_strerror(err));
1027 if((err = snd_pcm_sw_params(This->pcm_handle, sw_params)) < 0){
1028 WARN("Unable set sw params: %d (%s)\n", err, snd_strerror(err));
1033 if((err = snd_pcm_prepare(This->pcm_handle)) < 0){
1034 WARN("Unable to prepare device: %d (%s)\n", err, snd_strerror(err));
1039 /* Check if the ALSA buffer is so small that it will run out before
1040 * the next MMDevAPI period tick occurs. Allow a little wiggle room
1041 * with 120% of the period time. */
1042 if(This->alsa_bufsize_frames < 1.2 * This->mmdev_period_frames)
1043 FIXME("ALSA buffer time is too small. Expect underruns. (%lu < %u * 1.2)\n",
1044 This->alsa_bufsize_frames, This->mmdev_period_frames);
1046 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
1047 This->bufsize_frames * fmt->nBlockAlign);
1048 if(!This->local_buffer){
1052 if (fmt->wBitsPerSample == 8)
1053 memset(This->local_buffer, 128, This->bufsize_frames * fmt->nBlockAlign);
1055 memset(This->local_buffer, 0, This->bufsize_frames * fmt->nBlockAlign);
1057 This->fmt = clone_format(fmt);
1063 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1069 for(i = 0; i < fmt->nChannels; ++i)
1070 This->vols[i] = 1.f;
1073 This->flags = flags;
1075 EnterCriticalSection(&g_sessions_lock);
1077 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
1080 LeaveCriticalSection(&g_sessions_lock);
1084 list_add_tail(&This->session->clients, &This->entry);
1086 LeaveCriticalSection(&g_sessions_lock);
1088 This->initted = TRUE;
1090 TRACE("ALSA period: %lu frames\n", This->alsa_period_frames);
1091 TRACE("ALSA buffer: %lu frames\n", This->alsa_bufsize_frames);
1092 TRACE("MMDevice period: %u frames\n", This->mmdev_period_frames);
1093 TRACE("MMDevice buffer: %u frames\n", This->bufsize_frames);
1096 HeapFree(GetProcessHeap(), 0, sw_params);
1098 HeapFree(GetProcessHeap(), 0, This->local_buffer);
1099 This->local_buffer = NULL;
1100 CoTaskMemFree(This->fmt);
1102 HeapFree(GetProcessHeap(), 0, This->vols);
1106 LeaveCriticalSection(&This->lock);
1111 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1114 ACImpl *This = impl_from_IAudioClient(iface);
1116 TRACE("(%p)->(%p)\n", This, out);
1121 EnterCriticalSection(&This->lock);
1124 LeaveCriticalSection(&This->lock);
1125 return AUDCLNT_E_NOT_INITIALIZED;
1128 *out = This->bufsize_frames;
1130 LeaveCriticalSection(&This->lock);
1135 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1136 REFERENCE_TIME *latency)
1138 ACImpl *This = impl_from_IAudioClient(iface);
1140 TRACE("(%p)->(%p)\n", This, latency);
1145 EnterCriticalSection(&This->lock);
1148 LeaveCriticalSection(&This->lock);
1149 return AUDCLNT_E_NOT_INITIALIZED;
1152 LeaveCriticalSection(&This->lock);
1154 /* one mmdevapi period plus one period we hide in the ALSA buffer */
1155 *latency = MulDiv(This->alsa_period_frames, 10000000, This->fmt->nSamplesPerSec)
1156 + This->mmdev_period_rt;
1161 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1164 ACImpl *This = impl_from_IAudioClient(iface);
1166 TRACE("(%p)->(%p)\n", This, out);
1171 EnterCriticalSection(&This->lock);
1174 LeaveCriticalSection(&This->lock);
1175 return AUDCLNT_E_NOT_INITIALIZED;
1178 *out = This->held_frames;
1180 /* call required to get accurate snd_pcm_state() */
1181 snd_pcm_avail_update(This->pcm_handle);
1182 TRACE("pad: %u, state: %u\n", *out, snd_pcm_state(This->pcm_handle));
1184 LeaveCriticalSection(&This->lock);
1189 static DWORD get_channel_mask(unsigned int channels)
1195 return KSAUDIO_SPEAKER_MONO;
1197 return KSAUDIO_SPEAKER_STEREO;
1199 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
1201 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
1203 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
1205 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
1207 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
1209 return KSAUDIO_SPEAKER_7POINT1; /* not 7POINT1_SURROUND */
1211 FIXME("Unknown speaker configuration: %u\n", channels);
1215 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1216 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *fmt,
1219 ACImpl *This = impl_from_IAudioClient(iface);
1220 snd_pcm_format_mask_t *formats = NULL;
1221 snd_pcm_format_t format;
1223 WAVEFORMATEX *closest = NULL;
1224 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
1225 unsigned int max = 0, min = 0;
1228 TRACE("(%p)->(%x, %p, %p)\n", This, mode, fmt, out);
1230 if(!fmt || (mode == AUDCLNT_SHAREMODE_SHARED && !out))
1233 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1234 return E_INVALIDARG;
1236 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1237 fmt->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1238 return E_INVALIDARG;
1244 if(mode != AUDCLNT_SHAREMODE_SHARED)
1248 EnterCriticalSection(&This->lock);
1250 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
1255 formats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1256 snd_pcm_format_mask_sizeof());
1262 snd_pcm_hw_params_get_format_mask(This->hw_params, formats);
1263 format = alsa_format(fmt);
1264 if (format == SND_PCM_FORMAT_UNKNOWN ||
1265 !snd_pcm_format_mask_test(formats, format)){
1266 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1270 closest = clone_format(fmt);
1276 if((err = snd_pcm_hw_params_get_rate_min(This->hw_params, &min, NULL)) < 0){
1278 WARN("Unable to get min rate: %d (%s)\n", err, snd_strerror(err));
1282 if((err = snd_pcm_hw_params_get_rate_max(This->hw_params, &max, NULL)) < 0){
1284 WARN("Unable to get max rate: %d (%s)\n", err, snd_strerror(err));
1288 if(fmt->nSamplesPerSec < min || fmt->nSamplesPerSec > max){
1289 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1293 if((err = snd_pcm_hw_params_get_channels_min(This->hw_params, &min)) < 0){
1295 WARN("Unable to get min channels: %d (%s)\n", err, snd_strerror(err));
1299 if((err = snd_pcm_hw_params_get_channels_max(This->hw_params, &max)) < 0){
1301 WARN("Unable to get max channels: %d (%s)\n", err, snd_strerror(err));
1306 if(fmt->nChannels > max){
1308 closest->nChannels = max;
1309 }else if(fmt->nChannels < min){
1311 closest->nChannels = min;
1314 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
1315 DWORD mask = get_channel_mask(closest->nChannels);
1317 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = mask;
1319 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1320 fmtex->dwChannelMask != 0 &&
1321 fmtex->dwChannelMask != mask)
1326 LeaveCriticalSection(&This->lock);
1327 HeapFree(GetProcessHeap(), 0, formats);
1329 if(hr == S_FALSE && !out)
1330 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1332 if(hr == S_FALSE && out) {
1333 closest->nBlockAlign =
1334 closest->nChannels * closest->wBitsPerSample / 8;
1335 closest->nAvgBytesPerSec =
1336 closest->nBlockAlign * closest->nSamplesPerSec;
1339 CoTaskMemFree(closest);
1341 TRACE("returning: %08x\n", hr);
1345 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1346 WAVEFORMATEX **pwfx)
1348 ACImpl *This = impl_from_IAudioClient(iface);
1349 WAVEFORMATEXTENSIBLE *fmt;
1350 snd_pcm_format_mask_t *formats;
1351 unsigned int max_rate, max_channels;
1355 TRACE("(%p)->(%p)\n", This, pwfx);
1361 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1363 return E_OUTOFMEMORY;
1365 formats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_format_mask_sizeof());
1368 return E_OUTOFMEMORY;
1371 EnterCriticalSection(&This->lock);
1373 if((err = snd_pcm_hw_params_any(This->pcm_handle, This->hw_params)) < 0){
1374 WARN("Unable to get hw_params: %d (%s)\n", err, snd_strerror(err));
1379 snd_pcm_hw_params_get_format_mask(This->hw_params, formats);
1381 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1382 if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_FLOAT_LE)){
1383 fmt->Format.wBitsPerSample = 32;
1384 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1385 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S16_LE)){
1386 fmt->Format.wBitsPerSample = 16;
1387 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1388 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_U8)){
1389 fmt->Format.wBitsPerSample = 8;
1390 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1391 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S32_LE)){
1392 fmt->Format.wBitsPerSample = 32;
1393 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1394 }else if(snd_pcm_format_mask_test(formats, SND_PCM_FORMAT_S24_3LE)){
1395 fmt->Format.wBitsPerSample = 24;
1396 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1398 ERR("Didn't recognize any available ALSA formats\n");
1403 if((err = snd_pcm_hw_params_get_channels_max(This->hw_params,
1404 &max_channels)) < 0){
1405 WARN("Unable to get max channels: %d (%s)\n", err, snd_strerror(err));
1410 if(max_channels > 2){
1411 FIXME("Don't know what to do with %u channels, pretending there's "
1412 "only 2 channels\n", max_channels);
1413 fmt->Format.nChannels = 2;
1415 fmt->Format.nChannels = max_channels;
1417 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1419 if((err = snd_pcm_hw_params_get_rate_max(This->hw_params, &max_rate,
1421 WARN("Unable to get max rate: %d (%s)\n", err, snd_strerror(err));
1426 if(max_rate >= 48000)
1427 fmt->Format.nSamplesPerSec = 48000;
1428 else if(max_rate >= 44100)
1429 fmt->Format.nSamplesPerSec = 44100;
1430 else if(max_rate >= 22050)
1431 fmt->Format.nSamplesPerSec = 22050;
1432 else if(max_rate >= 11025)
1433 fmt->Format.nSamplesPerSec = 11025;
1434 else if(max_rate >= 8000)
1435 fmt->Format.nSamplesPerSec = 8000;
1437 ERR("Unknown max rate: %u\n", max_rate);
1442 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1443 fmt->Format.nChannels) / 8;
1444 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1445 fmt->Format.nBlockAlign;
1447 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1448 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1450 dump_fmt((WAVEFORMATEX*)fmt);
1451 *pwfx = (WAVEFORMATEX*)fmt;
1454 LeaveCriticalSection(&This->lock);
1457 HeapFree(GetProcessHeap(), 0, formats);
1462 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1463 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1465 ACImpl *This = impl_from_IAudioClient(iface);
1467 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1469 if(!defperiod && !minperiod)
1473 *defperiod = DefaultPeriod;
1475 *minperiod = MinimumPeriod;
1480 static snd_pcm_sframes_t alsa_write_best_effort(snd_pcm_t *handle, BYTE *buf,
1481 snd_pcm_uframes_t frames, ACImpl *This)
1483 snd_pcm_sframes_t written;
1485 if(This->session->mute){
1487 if((err = snd_pcm_format_set_silence(This->alsa_format, buf,
1488 frames * This->fmt->nChannels)) < 0)
1489 WARN("Setting buffer to silence failed: %d (%s)\n", err,
1493 written = snd_pcm_writei(handle, buf, frames);
1497 if(written == -EAGAIN)
1501 WARN("writei failed, recovering: %ld (%s)\n", written,
1502 snd_strerror(written));
1504 ret = snd_pcm_recover(handle, written, 0);
1506 WARN("Could not recover: %d (%s)\n", ret, snd_strerror(ret));
1510 written = snd_pcm_writei(handle, buf, frames);
1516 static void alsa_write_data(ACImpl *This)
1518 snd_pcm_sframes_t written;
1519 snd_pcm_uframes_t to_write, avail, write_limit, max_period, in_alsa;
1522 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1524 /* this call seems to be required to get an accurate snd_pcm_state() */
1525 avail = snd_pcm_avail_update(This->pcm_handle);
1527 if(snd_pcm_state(This->pcm_handle) == SND_PCM_STATE_XRUN ||
1528 avail > This->alsa_bufsize_frames){
1529 TRACE("XRun state, recovering\n");
1531 avail = This->alsa_bufsize_frames;
1533 if((err = snd_pcm_recover(This->pcm_handle, -EPIPE, 1)) < 0)
1534 WARN("snd_pcm_recover failed: %d (%s)\n", err, snd_strerror(err));
1536 if((err = snd_pcm_reset(This->pcm_handle)) < 0)
1537 WARN("snd_pcm_reset failed: %d (%s)\n", err, snd_strerror(err));
1539 if((err = snd_pcm_prepare(This->pcm_handle)) < 0)
1540 WARN("snd_pcm_prepare failed: %d (%s)\n", err, snd_strerror(err));
1543 if(This->held_frames == 0)
1546 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1547 to_write = This->bufsize_frames - This->lcl_offs_frames;
1549 to_write = This->held_frames;
1551 max_period = max(This->mmdev_period_frames, This->alsa_period_frames);
1553 /* try to keep 3 ALSA periods or 3 MMDevAPI periods in the ALSA buffer and
1556 in_alsa = This->alsa_bufsize_frames - avail;
1557 while(in_alsa + write_limit < max_period * 3)
1558 write_limit += max_period;
1559 if(write_limit == 0)
1562 to_write = min(to_write, write_limit);
1564 written = alsa_write_best_effort(This->pcm_handle, buf, to_write, This);
1566 WARN("Couldn't write: %ld (%s)\n", written, snd_strerror(written));
1570 This->lcl_offs_frames += written;
1571 This->lcl_offs_frames %= This->bufsize_frames;
1572 This->held_frames -= written;
1574 if(written < to_write){
1575 /* ALSA buffer probably full */
1579 if(This->held_frames && (written < write_limit)){
1580 /* wrapped and have some data back at the start to write */
1581 written = alsa_write_best_effort(This->pcm_handle, This->local_buffer,
1582 min(This->held_frames, write_limit - written), This);
1584 WARN("Couldn't write: %ld (%s)\n", written, snd_strerror(written));
1588 This->lcl_offs_frames += written;
1589 This->lcl_offs_frames %= This->bufsize_frames;
1590 This->held_frames -= written;
1594 static void alsa_read_data(ACImpl *This)
1596 snd_pcm_sframes_t pos, readable, nread;
1598 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1599 readable = This->bufsize_frames - pos;
1601 nread = snd_pcm_readi(This->pcm_handle,
1602 This->local_buffer + pos * This->fmt->nBlockAlign, readable);
1606 WARN("read failed, recovering: %ld (%s)\n", nread, snd_strerror(nread));
1608 ret = snd_pcm_recover(This->pcm_handle, nread, 0);
1610 WARN("Recover failed: %d (%s)\n", ret, snd_strerror(ret));
1614 nread = snd_pcm_readi(This->pcm_handle,
1615 This->local_buffer + pos * This->fmt->nBlockAlign, readable);
1617 WARN("read failed: %ld (%s)\n", nread, snd_strerror(nread));
1622 if(This->session->mute){
1624 if((err = snd_pcm_format_set_silence(This->alsa_format,
1625 This->local_buffer + pos * This->fmt->nBlockAlign,
1627 WARN("Setting buffer to silence failed: %d (%s)\n", err,
1631 This->held_frames += nread;
1633 if(This->held_frames > This->bufsize_frames){
1634 WARN("Overflow of unread data\n");
1635 This->lcl_offs_frames += This->held_frames;
1636 This->lcl_offs_frames %= This->bufsize_frames;
1637 This->held_frames = This->bufsize_frames;
1641 static void CALLBACK alsa_push_buffer_data(void *user, BOOLEAN timer)
1643 ACImpl *This = user;
1645 EnterCriticalSection(&This->lock);
1648 if(This->dataflow == eRender)
1649 alsa_write_data(This);
1650 else if(This->dataflow == eCapture)
1651 alsa_read_data(This);
1654 SetEvent(This->event);
1657 LeaveCriticalSection(&This->lock);
1660 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1662 ACImpl *This = impl_from_IAudioClient(iface);
1664 TRACE("(%p)\n", This);
1666 EnterCriticalSection(&This->lock);
1669 LeaveCriticalSection(&This->lock);
1670 return AUDCLNT_E_NOT_INITIALIZED;
1673 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1674 LeaveCriticalSection(&This->lock);
1675 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1679 LeaveCriticalSection(&This->lock);
1680 return AUDCLNT_E_NOT_STOPPED;
1683 if(This->dataflow == eCapture){
1684 /* dump any data that might be leftover in the ALSA capture buffer */
1685 snd_pcm_readi(This->pcm_handle, This->local_buffer,
1686 This->bufsize_frames);
1689 if(!CreateTimerQueueTimer(&This->timer, g_timer_q, alsa_push_buffer_data,
1690 This, 0, This->mmdev_period_rt / 10000, WT_EXECUTEINTIMERTHREAD)){
1691 LeaveCriticalSection(&This->lock);
1692 WARN("Unable to create timer: %u\n", GetLastError());
1696 This->started = TRUE;
1698 LeaveCriticalSection(&This->lock);
1703 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1705 ACImpl *This = impl_from_IAudioClient(iface);
1709 TRACE("(%p)\n", This);
1711 EnterCriticalSection(&This->lock);
1714 LeaveCriticalSection(&This->lock);
1715 return AUDCLNT_E_NOT_INITIALIZED;
1719 LeaveCriticalSection(&This->lock);
1723 if(snd_pcm_drop(This->pcm_handle) < 0)
1724 WARN("snd_pcm_drop failed\n");
1726 if(snd_pcm_reset(This->pcm_handle) < 0)
1727 WARN("snd_pcm_reset failed\n");
1729 if(snd_pcm_prepare(This->pcm_handle) < 0)
1730 WARN("snd_pcm_prepare failed\n");
1732 event = CreateEventW(NULL, TRUE, FALSE, NULL);
1733 wait = !DeleteTimerQueueTimer(g_timer_q, This->timer, event);
1735 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1736 wait = wait && GetLastError() == ERROR_IO_PENDING;
1738 This->started = FALSE;
1740 LeaveCriticalSection(&This->lock);
1743 WaitForSingleObject(event, INFINITE);
1749 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1751 ACImpl *This = impl_from_IAudioClient(iface);
1753 TRACE("(%p)\n", This);
1755 EnterCriticalSection(&This->lock);
1758 LeaveCriticalSection(&This->lock);
1759 return AUDCLNT_E_NOT_INITIALIZED;
1763 LeaveCriticalSection(&This->lock);
1764 return AUDCLNT_E_NOT_STOPPED;
1767 if(This->buf_state != NOT_LOCKED || This->getbuf_last){
1768 LeaveCriticalSection(&This->lock);
1769 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1772 if(snd_pcm_drop(This->pcm_handle) < 0)
1773 WARN("snd_pcm_drop failed\n");
1775 if(snd_pcm_reset(This->pcm_handle) < 0)
1776 WARN("snd_pcm_reset failed\n");
1778 if(snd_pcm_prepare(This->pcm_handle) < 0)
1779 WARN("snd_pcm_prepare failed\n");
1781 This->last_pos_frames = 0;
1782 This->held_frames = 0;
1783 This->written_frames = 0;
1784 This->lcl_offs_frames = 0;
1786 LeaveCriticalSection(&This->lock);
1791 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1794 ACImpl *This = impl_from_IAudioClient(iface);
1796 TRACE("(%p)->(%p)\n", This, event);
1799 return E_INVALIDARG;
1801 EnterCriticalSection(&This->lock);
1804 LeaveCriticalSection(&This->lock);
1805 return AUDCLNT_E_NOT_INITIALIZED;
1808 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1809 LeaveCriticalSection(&This->lock);
1810 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1813 This->event = event;
1815 LeaveCriticalSection(&This->lock);
1820 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1823 ACImpl *This = impl_from_IAudioClient(iface);
1825 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1831 EnterCriticalSection(&This->lock);
1834 LeaveCriticalSection(&This->lock);
1835 return AUDCLNT_E_NOT_INITIALIZED;
1838 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1839 if(This->dataflow != eRender){
1840 LeaveCriticalSection(&This->lock);
1841 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1843 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1844 *ppv = &This->IAudioRenderClient_iface;
1845 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1846 if(This->dataflow != eCapture){
1847 LeaveCriticalSection(&This->lock);
1848 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1850 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1851 *ppv = &This->IAudioCaptureClient_iface;
1852 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1853 IAudioClock_AddRef(&This->IAudioClock_iface);
1854 *ppv = &This->IAudioClock_iface;
1855 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1856 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1857 *ppv = &This->IAudioStreamVolume_iface;
1858 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1859 if(!This->session_wrapper){
1860 This->session_wrapper = AudioSessionWrapper_Create(This);
1861 if(!This->session_wrapper){
1862 LeaveCriticalSection(&This->lock);
1863 return E_OUTOFMEMORY;
1866 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1868 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1869 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1870 if(!This->session_wrapper){
1871 This->session_wrapper = AudioSessionWrapper_Create(This);
1872 if(!This->session_wrapper){
1873 LeaveCriticalSection(&This->lock);
1874 return E_OUTOFMEMORY;
1877 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1879 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1880 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1881 if(!This->session_wrapper){
1882 This->session_wrapper = AudioSessionWrapper_Create(This);
1883 if(!This->session_wrapper){
1884 LeaveCriticalSection(&This->lock);
1885 return E_OUTOFMEMORY;
1888 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1890 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1894 LeaveCriticalSection(&This->lock);
1898 LeaveCriticalSection(&This->lock);
1900 FIXME("stub %s\n", debugstr_guid(riid));
1901 return E_NOINTERFACE;
1904 static const IAudioClientVtbl AudioClient_Vtbl =
1906 AudioClient_QueryInterface,
1908 AudioClient_Release,
1909 AudioClient_Initialize,
1910 AudioClient_GetBufferSize,
1911 AudioClient_GetStreamLatency,
1912 AudioClient_GetCurrentPadding,
1913 AudioClient_IsFormatSupported,
1914 AudioClient_GetMixFormat,
1915 AudioClient_GetDevicePeriod,
1919 AudioClient_SetEventHandle,
1920 AudioClient_GetService
1923 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1924 IAudioRenderClient *iface, REFIID riid, void **ppv)
1926 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1932 if(IsEqualIID(riid, &IID_IUnknown) ||
1933 IsEqualIID(riid, &IID_IAudioRenderClient))
1936 IUnknown_AddRef((IUnknown*)*ppv);
1940 WARN("Unknown interface %s\n", debugstr_guid(riid));
1941 return E_NOINTERFACE;
1944 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1946 ACImpl *This = impl_from_IAudioRenderClient(iface);
1947 return AudioClient_AddRef(&This->IAudioClient_iface);
1950 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1952 ACImpl *This = impl_from_IAudioRenderClient(iface);
1953 return AudioClient_Release(&This->IAudioClient_iface);
1956 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1957 UINT32 frames, BYTE **data)
1959 ACImpl *This = impl_from_IAudioRenderClient(iface);
1964 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1970 EnterCriticalSection(&This->lock);
1972 if(This->getbuf_last){
1973 LeaveCriticalSection(&This->lock);
1974 return AUDCLNT_E_OUT_OF_ORDER;
1978 LeaveCriticalSection(&This->lock);
1982 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1984 LeaveCriticalSection(&This->lock);
1988 if(pad + frames > This->bufsize_frames){
1989 LeaveCriticalSection(&This->lock);
1990 return AUDCLNT_E_BUFFER_TOO_LARGE;
1994 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1995 if(write_pos + frames > This->bufsize_frames){
1996 if(This->tmp_buffer_frames < frames){
1997 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
1998 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1999 frames * This->fmt->nBlockAlign);
2000 if(!This->tmp_buffer){
2001 LeaveCriticalSection(&This->lock);
2002 return E_OUTOFMEMORY;
2004 This->tmp_buffer_frames = frames;
2006 *data = This->tmp_buffer;
2007 This->getbuf_last = -frames;
2009 *data = This->local_buffer + write_pos * This->fmt->nBlockAlign;
2010 This->getbuf_last = frames;
2013 LeaveCriticalSection(&This->lock);
2018 static void alsa_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_frames)
2020 snd_pcm_uframes_t write_offs_frames =
2021 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
2022 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
2023 snd_pcm_uframes_t chunk_frames = This->bufsize_frames - write_offs_frames;
2024 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
2025 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
2027 if(written_bytes <= chunk_bytes){
2028 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
2030 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
2031 memcpy(This->local_buffer, buffer + chunk_bytes,
2032 written_bytes - chunk_bytes);
2036 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
2037 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
2039 ACImpl *This = impl_from_IAudioRenderClient(iface);
2042 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
2044 EnterCriticalSection(&This->lock);
2046 if(!written_frames){
2047 This->getbuf_last = 0;
2048 LeaveCriticalSection(&This->lock);
2052 if(!This->getbuf_last){
2053 LeaveCriticalSection(&This->lock);
2054 return AUDCLNT_E_OUT_OF_ORDER;
2057 if(written_frames > (This->getbuf_last >= 0 ? This->getbuf_last : -This->getbuf_last)){
2058 LeaveCriticalSection(&This->lock);
2059 return AUDCLNT_E_INVALID_SIZE;
2062 if(This->getbuf_last >= 0)
2063 buffer = This->local_buffer + This->fmt->nBlockAlign *
2064 ((This->lcl_offs_frames + This->held_frames) % This->bufsize_frames);
2066 buffer = This->tmp_buffer;
2068 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
2069 if(This->fmt->wBitsPerSample == 8)
2070 memset(buffer, 128, written_frames * This->fmt->nBlockAlign);
2072 memset(buffer, 0, written_frames * This->fmt->nBlockAlign);
2075 if(This->getbuf_last < 0)
2076 alsa_wrap_buffer(This, buffer, written_frames);
2078 This->held_frames += written_frames;
2079 This->written_frames += written_frames;
2080 This->getbuf_last = 0;
2082 LeaveCriticalSection(&This->lock);
2087 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
2088 AudioRenderClient_QueryInterface,
2089 AudioRenderClient_AddRef,
2090 AudioRenderClient_Release,
2091 AudioRenderClient_GetBuffer,
2092 AudioRenderClient_ReleaseBuffer
2095 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
2096 IAudioCaptureClient *iface, REFIID riid, void **ppv)
2098 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2104 if(IsEqualIID(riid, &IID_IUnknown) ||
2105 IsEqualIID(riid, &IID_IAudioCaptureClient))
2108 IUnknown_AddRef((IUnknown*)*ppv);
2112 WARN("Unknown interface %s\n", debugstr_guid(riid));
2113 return E_NOINTERFACE;
2116 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
2118 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2119 return IAudioClient_AddRef(&This->IAudioClient_iface);
2122 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
2124 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2125 return IAudioClient_Release(&This->IAudioClient_iface);
2128 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
2129 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
2132 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2135 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
2138 if(!data || !frames || !flags)
2141 EnterCriticalSection(&This->lock);
2143 if(This->buf_state != NOT_LOCKED){
2144 LeaveCriticalSection(&This->lock);
2145 return AUDCLNT_E_OUT_OF_ORDER;
2148 hr = IAudioCaptureClient_GetNextPacketSize(iface, frames);
2150 LeaveCriticalSection(&This->lock);
2156 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
2157 UINT32 chunk_bytes, offs_bytes, frames_bytes;
2158 if(This->tmp_buffer_frames < *frames){
2159 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
2160 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
2161 *frames * This->fmt->nBlockAlign);
2162 if(!This->tmp_buffer){
2163 LeaveCriticalSection(&This->lock);
2164 return E_OUTOFMEMORY;
2166 This->tmp_buffer_frames = *frames;
2169 *data = This->tmp_buffer;
2170 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
2171 This->fmt->nBlockAlign;
2172 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
2173 frames_bytes = *frames * This->fmt->nBlockAlign;
2174 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
2175 memcpy(This->tmp_buffer + chunk_bytes, This->local_buffer,
2176 frames_bytes - chunk_bytes);
2178 *data = This->local_buffer +
2179 This->lcl_offs_frames * This->fmt->nBlockAlign;
2181 This->buf_state = LOCKED_NORMAL;
2183 if(devpos || qpcpos)
2184 IAudioClock_GetPosition(&This->IAudioClock_iface, devpos, qpcpos);
2186 LeaveCriticalSection(&This->lock);
2188 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
2191 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
2192 IAudioCaptureClient *iface, UINT32 done)
2194 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2196 TRACE("(%p)->(%u)\n", This, done);
2198 EnterCriticalSection(&This->lock);
2200 if(This->buf_state == NOT_LOCKED){
2201 LeaveCriticalSection(&This->lock);
2202 return AUDCLNT_E_OUT_OF_ORDER;
2205 This->held_frames -= done;
2206 This->lcl_offs_frames += done;
2207 This->lcl_offs_frames %= This->bufsize_frames;
2209 This->buf_state = NOT_LOCKED;
2211 LeaveCriticalSection(&This->lock);
2216 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2217 IAudioCaptureClient *iface, UINT32 *frames)
2219 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2221 TRACE("(%p)->(%p)\n", This, frames);
2223 return AudioClient_GetCurrentPadding(&This->IAudioClient_iface, frames);
2226 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2228 AudioCaptureClient_QueryInterface,
2229 AudioCaptureClient_AddRef,
2230 AudioCaptureClient_Release,
2231 AudioCaptureClient_GetBuffer,
2232 AudioCaptureClient_ReleaseBuffer,
2233 AudioCaptureClient_GetNextPacketSize
2236 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2237 REFIID riid, void **ppv)
2239 ACImpl *This = impl_from_IAudioClock(iface);
2241 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2247 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2249 else if(IsEqualIID(riid, &IID_IAudioClock2))
2250 *ppv = &This->IAudioClock2_iface;
2252 IUnknown_AddRef((IUnknown*)*ppv);
2256 WARN("Unknown interface %s\n", debugstr_guid(riid));
2257 return E_NOINTERFACE;
2260 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2262 ACImpl *This = impl_from_IAudioClock(iface);
2263 return IAudioClient_AddRef(&This->IAudioClient_iface);
2266 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2268 ACImpl *This = impl_from_IAudioClock(iface);
2269 return IAudioClient_Release(&This->IAudioClient_iface);
2272 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2274 ACImpl *This = impl_from_IAudioClock(iface);
2276 TRACE("(%p)->(%p)\n", This, freq);
2278 *freq = This->fmt->nSamplesPerSec;
2283 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2286 ACImpl *This = impl_from_IAudioClock(iface);
2287 UINT64 written_frames, position;
2290 snd_pcm_state_t alsa_state;
2291 snd_pcm_uframes_t avail_frames;
2292 snd_pcm_sframes_t delay_frames;
2294 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2299 EnterCriticalSection(&This->lock);
2301 /* call required to get accurate snd_pcm_state() */
2302 avail_frames = snd_pcm_avail_update(This->pcm_handle);
2303 alsa_state = snd_pcm_state(This->pcm_handle);
2304 written_frames = This->written_frames;
2305 held_frames = This->held_frames;
2307 err = snd_pcm_delay(This->pcm_handle, &delay_frames);
2309 /* old Pulse, shortly after start */
2310 WARN("snd_pcm_delay failed in state %u: %d (%s)\n", alsa_state, err, snd_strerror(err));
2313 if(This->dataflow == eRender){
2314 position = written_frames - held_frames; /* maximum */
2315 if(!This->started || alsa_state > SND_PCM_STATE_RUNNING)
2316 ; /* mmdevapi stopped or ALSA underrun: pretend everything was played */
2317 else if(err<0 || delay_frames > position - This->last_pos_frames)
2318 /* Pulse bug: past underrun, despite recovery, avail_frames & delay
2319 * may be larger than alsa_bufsize_frames, as if cumulating frames. */
2320 /* Pulse bug: EIO(-5) shortly after starting: nothing played */
2321 position = This->last_pos_frames;
2322 else if(delay_frames > 0)
2323 position -= delay_frames;
2325 position = written_frames + held_frames;
2327 /* ensure monotic growth */
2328 This->last_pos_frames = position;
2330 LeaveCriticalSection(&This->lock);
2332 TRACE("frames written: %u, held: %u, avail: %ld, delay: %ld state %d, pos: %u\n",
2333 (UINT32)(written_frames%1000000000), held_frames,
2334 avail_frames, delay_frames, alsa_state, (UINT32)(position%1000000000));
2338 LARGE_INTEGER stamp, freq;
2339 QueryPerformanceCounter(&stamp);
2340 QueryPerformanceFrequency(&freq);
2341 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2347 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2350 ACImpl *This = impl_from_IAudioClock(iface);
2352 TRACE("(%p)->(%p)\n", This, chars);
2357 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2362 static const IAudioClockVtbl AudioClock_Vtbl =
2364 AudioClock_QueryInterface,
2367 AudioClock_GetFrequency,
2368 AudioClock_GetPosition,
2369 AudioClock_GetCharacteristics
2372 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2373 REFIID riid, void **ppv)
2375 ACImpl *This = impl_from_IAudioClock2(iface);
2376 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2379 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2381 ACImpl *This = impl_from_IAudioClock2(iface);
2382 return IAudioClient_AddRef(&This->IAudioClient_iface);
2385 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2387 ACImpl *This = impl_from_IAudioClock2(iface);
2388 return IAudioClient_Release(&This->IAudioClient_iface);
2391 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2392 UINT64 *pos, UINT64 *qpctime)
2394 ACImpl *This = impl_from_IAudioClock2(iface);
2396 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2401 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2403 AudioClock2_QueryInterface,
2405 AudioClock2_Release,
2406 AudioClock2_GetDevicePosition
2409 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2411 AudioSessionWrapper *ret;
2413 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2414 sizeof(AudioSessionWrapper));
2418 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2419 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2420 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2424 ret->client = client;
2426 ret->session = client->session;
2427 AudioClient_AddRef(&client->IAudioClient_iface);
2433 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2434 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2436 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2442 if(IsEqualIID(riid, &IID_IUnknown) ||
2443 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2444 IsEqualIID(riid, &IID_IAudioSessionControl2))
2447 IUnknown_AddRef((IUnknown*)*ppv);
2451 WARN("Unknown interface %s\n", debugstr_guid(riid));
2452 return E_NOINTERFACE;
2455 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2457 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2459 ref = InterlockedIncrement(&This->ref);
2460 TRACE("(%p) Refcount now %u\n", This, ref);
2464 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2466 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2468 ref = InterlockedDecrement(&This->ref);
2469 TRACE("(%p) Refcount now %u\n", This, ref);
2472 EnterCriticalSection(&This->client->lock);
2473 This->client->session_wrapper = NULL;
2474 LeaveCriticalSection(&This->client->lock);
2475 AudioClient_Release(&This->client->IAudioClient_iface);
2477 HeapFree(GetProcessHeap(), 0, This);
2482 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2483 AudioSessionState *state)
2485 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2488 TRACE("(%p)->(%p)\n", This, state);
2491 return NULL_PTR_ERR;
2493 EnterCriticalSection(&g_sessions_lock);
2495 if(list_empty(&This->session->clients)){
2496 *state = AudioSessionStateExpired;
2497 LeaveCriticalSection(&g_sessions_lock);
2501 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2502 EnterCriticalSection(&client->lock);
2503 if(client->started){
2504 *state = AudioSessionStateActive;
2505 LeaveCriticalSection(&client->lock);
2506 LeaveCriticalSection(&g_sessions_lock);
2509 LeaveCriticalSection(&client->lock);
2512 LeaveCriticalSection(&g_sessions_lock);
2514 *state = AudioSessionStateInactive;
2519 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2520 IAudioSessionControl2 *iface, WCHAR **name)
2522 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2524 FIXME("(%p)->(%p) - stub\n", This, name);
2529 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2530 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2532 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2534 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2539 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2540 IAudioSessionControl2 *iface, WCHAR **path)
2542 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2544 FIXME("(%p)->(%p) - stub\n", This, path);
2549 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2550 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2552 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2554 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2559 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2560 IAudioSessionControl2 *iface, GUID *group)
2562 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2564 FIXME("(%p)->(%p) - stub\n", This, group);
2569 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2570 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2572 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2574 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2575 debugstr_guid(session));
2580 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2581 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2583 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2585 FIXME("(%p)->(%p) - stub\n", This, events);
2590 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2591 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2593 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2595 FIXME("(%p)->(%p) - stub\n", This, events);
2600 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2601 IAudioSessionControl2 *iface, WCHAR **id)
2603 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2605 FIXME("(%p)->(%p) - stub\n", This, id);
2610 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2611 IAudioSessionControl2 *iface, WCHAR **id)
2613 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2615 FIXME("(%p)->(%p) - stub\n", This, id);
2620 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2621 IAudioSessionControl2 *iface, DWORD *pid)
2623 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2625 TRACE("(%p)->(%p)\n", This, pid);
2630 *pid = GetCurrentProcessId();
2635 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2636 IAudioSessionControl2 *iface)
2638 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2640 TRACE("(%p)\n", This);
2645 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2646 IAudioSessionControl2 *iface, BOOL optout)
2648 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2650 TRACE("(%p)->(%d)\n", This, optout);
2655 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2657 AudioSessionControl_QueryInterface,
2658 AudioSessionControl_AddRef,
2659 AudioSessionControl_Release,
2660 AudioSessionControl_GetState,
2661 AudioSessionControl_GetDisplayName,
2662 AudioSessionControl_SetDisplayName,
2663 AudioSessionControl_GetIconPath,
2664 AudioSessionControl_SetIconPath,
2665 AudioSessionControl_GetGroupingParam,
2666 AudioSessionControl_SetGroupingParam,
2667 AudioSessionControl_RegisterAudioSessionNotification,
2668 AudioSessionControl_UnregisterAudioSessionNotification,
2669 AudioSessionControl_GetSessionIdentifier,
2670 AudioSessionControl_GetSessionInstanceIdentifier,
2671 AudioSessionControl_GetProcessId,
2672 AudioSessionControl_IsSystemSoundsSession,
2673 AudioSessionControl_SetDuckingPreference
2676 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2677 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2679 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2685 if(IsEqualIID(riid, &IID_IUnknown) ||
2686 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2689 IUnknown_AddRef((IUnknown*)*ppv);
2693 WARN("Unknown interface %s\n", debugstr_guid(riid));
2694 return E_NOINTERFACE;
2697 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2699 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2700 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2703 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2705 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2706 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2709 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2710 ISimpleAudioVolume *iface, float level, const GUID *context)
2712 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2713 AudioSession *session = This->session;
2715 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2717 if(level < 0.f || level > 1.f)
2718 return E_INVALIDARG;
2721 FIXME("Notifications not supported yet\n");
2723 TRACE("ALSA does not support volume control\n");
2725 EnterCriticalSection(&session->lock);
2727 session->master_vol = level;
2729 LeaveCriticalSection(&session->lock);
2734 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2735 ISimpleAudioVolume *iface, float *level)
2737 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2738 AudioSession *session = This->session;
2740 TRACE("(%p)->(%p)\n", session, level);
2743 return NULL_PTR_ERR;
2745 *level = session->master_vol;
2750 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2751 BOOL mute, const GUID *context)
2753 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2754 AudioSession *session = This->session;
2756 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2759 FIXME("Notifications not supported yet\n");
2761 session->mute = mute;
2766 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2769 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2770 AudioSession *session = This->session;
2772 TRACE("(%p)->(%p)\n", session, mute);
2775 return NULL_PTR_ERR;
2777 *mute = session->mute;
2782 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2784 SimpleAudioVolume_QueryInterface,
2785 SimpleAudioVolume_AddRef,
2786 SimpleAudioVolume_Release,
2787 SimpleAudioVolume_SetMasterVolume,
2788 SimpleAudioVolume_GetMasterVolume,
2789 SimpleAudioVolume_SetMute,
2790 SimpleAudioVolume_GetMute
2793 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2794 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2796 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2802 if(IsEqualIID(riid, &IID_IUnknown) ||
2803 IsEqualIID(riid, &IID_IAudioStreamVolume))
2806 IUnknown_AddRef((IUnknown*)*ppv);
2810 WARN("Unknown interface %s\n", debugstr_guid(riid));
2811 return E_NOINTERFACE;
2814 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2816 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2817 return IAudioClient_AddRef(&This->IAudioClient_iface);
2820 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2822 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2823 return IAudioClient_Release(&This->IAudioClient_iface);
2826 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2827 IAudioStreamVolume *iface, UINT32 *out)
2829 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2831 TRACE("(%p)->(%p)\n", This, out);
2836 *out = This->fmt->nChannels;
2841 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2842 IAudioStreamVolume *iface, UINT32 index, float level)
2844 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2846 TRACE("(%p)->(%d, %f)\n", This, index, level);
2848 if(level < 0.f || level > 1.f)
2849 return E_INVALIDARG;
2851 if(index >= This->fmt->nChannels)
2852 return E_INVALIDARG;
2854 TRACE("ALSA does not support volume control\n");
2856 EnterCriticalSection(&This->lock);
2858 This->vols[index] = level;
2860 LeaveCriticalSection(&This->lock);
2865 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2866 IAudioStreamVolume *iface, UINT32 index, float *level)
2868 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2870 TRACE("(%p)->(%d, %p)\n", This, index, level);
2875 if(index >= This->fmt->nChannels)
2876 return E_INVALIDARG;
2878 *level = This->vols[index];
2883 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2884 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2886 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2889 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2894 if(count != This->fmt->nChannels)
2895 return E_INVALIDARG;
2897 TRACE("ALSA does not support volume control\n");
2899 EnterCriticalSection(&This->lock);
2901 for(i = 0; i < count; ++i)
2902 This->vols[i] = levels[i];
2904 LeaveCriticalSection(&This->lock);
2909 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2910 IAudioStreamVolume *iface, UINT32 count, float *levels)
2912 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2915 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2920 if(count != This->fmt->nChannels)
2921 return E_INVALIDARG;
2923 EnterCriticalSection(&This->lock);
2925 for(i = 0; i < count; ++i)
2926 levels[i] = This->vols[i];
2928 LeaveCriticalSection(&This->lock);
2933 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2935 AudioStreamVolume_QueryInterface,
2936 AudioStreamVolume_AddRef,
2937 AudioStreamVolume_Release,
2938 AudioStreamVolume_GetChannelCount,
2939 AudioStreamVolume_SetChannelVolume,
2940 AudioStreamVolume_GetChannelVolume,
2941 AudioStreamVolume_SetAllVolumes,
2942 AudioStreamVolume_GetAllVolumes
2945 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2946 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2948 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2954 if(IsEqualIID(riid, &IID_IUnknown) ||
2955 IsEqualIID(riid, &IID_IChannelAudioVolume))
2958 IUnknown_AddRef((IUnknown*)*ppv);
2962 WARN("Unknown interface %s\n", debugstr_guid(riid));
2963 return E_NOINTERFACE;
2966 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2968 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2969 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2972 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2974 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2975 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2978 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2979 IChannelAudioVolume *iface, UINT32 *out)
2981 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2982 AudioSession *session = This->session;
2984 TRACE("(%p)->(%p)\n", session, out);
2987 return NULL_PTR_ERR;
2989 *out = session->channel_count;
2994 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2995 IChannelAudioVolume *iface, UINT32 index, float level,
2996 const GUID *context)
2998 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2999 AudioSession *session = This->session;
3001 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
3002 wine_dbgstr_guid(context));
3004 if(level < 0.f || level > 1.f)
3005 return E_INVALIDARG;
3007 if(index >= session->channel_count)
3008 return E_INVALIDARG;
3011 FIXME("Notifications not supported yet\n");
3013 TRACE("ALSA does not support volume control\n");
3015 EnterCriticalSection(&session->lock);
3017 session->channel_vols[index] = level;
3019 LeaveCriticalSection(&session->lock);
3024 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
3025 IChannelAudioVolume *iface, UINT32 index, float *level)
3027 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3028 AudioSession *session = This->session;
3030 TRACE("(%p)->(%d, %p)\n", session, index, level);
3033 return NULL_PTR_ERR;
3035 if(index >= session->channel_count)
3036 return E_INVALIDARG;
3038 *level = session->channel_vols[index];
3043 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
3044 IChannelAudioVolume *iface, UINT32 count, const float *levels,
3045 const GUID *context)
3047 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3048 AudioSession *session = This->session;
3051 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
3052 wine_dbgstr_guid(context));
3055 return NULL_PTR_ERR;
3057 if(count != session->channel_count)
3058 return E_INVALIDARG;
3061 FIXME("Notifications not supported yet\n");
3063 TRACE("ALSA does not support volume control\n");
3065 EnterCriticalSection(&session->lock);
3067 for(i = 0; i < count; ++i)
3068 session->channel_vols[i] = levels[i];
3070 LeaveCriticalSection(&session->lock);
3075 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
3076 IChannelAudioVolume *iface, UINT32 count, float *levels)
3078 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3079 AudioSession *session = This->session;
3082 TRACE("(%p)->(%d, %p)\n", session, count, levels);
3085 return NULL_PTR_ERR;
3087 if(count != session->channel_count)
3088 return E_INVALIDARG;
3090 for(i = 0; i < count; ++i)
3091 levels[i] = session->channel_vols[i];
3096 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
3098 ChannelAudioVolume_QueryInterface,
3099 ChannelAudioVolume_AddRef,
3100 ChannelAudioVolume_Release,
3101 ChannelAudioVolume_GetChannelCount,
3102 ChannelAudioVolume_SetChannelVolume,
3103 ChannelAudioVolume_GetChannelVolume,
3104 ChannelAudioVolume_SetAllVolumes,
3105 ChannelAudioVolume_GetAllVolumes
3108 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
3109 REFIID riid, void **ppv)
3111 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
3117 if(IsEqualIID(riid, &IID_IUnknown) ||
3118 IsEqualIID(riid, &IID_IAudioSessionManager) ||
3119 IsEqualIID(riid, &IID_IAudioSessionManager2))
3122 IUnknown_AddRef((IUnknown*)*ppv);
3126 WARN("Unknown interface %s\n", debugstr_guid(riid));
3127 return E_NOINTERFACE;
3130 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
3132 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3134 ref = InterlockedIncrement(&This->ref);
3135 TRACE("(%p) Refcount now %u\n", This, ref);
3139 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
3141 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3143 ref = InterlockedDecrement(&This->ref);
3144 TRACE("(%p) Refcount now %u\n", This, ref);
3146 HeapFree(GetProcessHeap(), 0, This);
3150 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
3151 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3152 IAudioSessionControl **out)
3154 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3155 AudioSession *session;
3156 AudioSessionWrapper *wrapper;
3159 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3162 hr = get_audio_session(session_guid, This->device, 0, &session);
3166 wrapper = AudioSessionWrapper_Create(NULL);
3168 return E_OUTOFMEMORY;
3170 wrapper->session = session;
3172 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3177 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
3178 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3179 ISimpleAudioVolume **out)
3181 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3182 AudioSession *session;
3183 AudioSessionWrapper *wrapper;
3186 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3189 hr = get_audio_session(session_guid, This->device, 0, &session);
3193 wrapper = AudioSessionWrapper_Create(NULL);
3195 return E_OUTOFMEMORY;
3197 wrapper->session = session;
3199 *out = &wrapper->ISimpleAudioVolume_iface;
3204 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
3205 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
3207 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3208 FIXME("(%p)->(%p) - stub\n", This, out);
3212 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
3213 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3215 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3216 FIXME("(%p)->(%p) - stub\n", This, notification);
3220 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
3221 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3223 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3224 FIXME("(%p)->(%p) - stub\n", This, notification);
3228 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
3229 IAudioSessionManager2 *iface, const WCHAR *session_id,
3230 IAudioVolumeDuckNotification *notification)
3232 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3233 FIXME("(%p)->(%p) - stub\n", This, notification);
3237 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
3238 IAudioSessionManager2 *iface,
3239 IAudioVolumeDuckNotification *notification)
3241 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3242 FIXME("(%p)->(%p) - stub\n", This, notification);
3246 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
3248 AudioSessionManager_QueryInterface,
3249 AudioSessionManager_AddRef,
3250 AudioSessionManager_Release,
3251 AudioSessionManager_GetAudioSessionControl,
3252 AudioSessionManager_GetSimpleAudioVolume,
3253 AudioSessionManager_GetSessionEnumerator,
3254 AudioSessionManager_RegisterSessionNotification,
3255 AudioSessionManager_UnregisterSessionNotification,
3256 AudioSessionManager_RegisterDuckNotification,
3257 AudioSessionManager_UnregisterDuckNotification
3260 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3261 IAudioSessionManager2 **out)
3265 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3267 return E_OUTOFMEMORY;
3269 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3270 This->device = device;
3273 *out = &This->IAudioSessionManager2_iface;