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
;
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
;
113 UINT32 bufsize_frames
, held_frames
, tmp_buffer_frames
;
114 UINT32 lcl_offs_frames
; /* offs into local_buffer where valid data starts */
117 BYTE
*local_buffer
, *tmp_buffer
;
120 CRITICAL_SECTION lock
;
122 AudioSession
*session
;
123 AudioSessionWrapper
*session_wrapper
;
130 LOCKED_NORMAL
, /* public buffer piece is from local_buffer */
131 LOCKED_WRAPPED
/* public buffer piece is wrapped around, in tmp_buffer */
134 typedef struct _SessionMgr
{
135 IAudioSessionManager2 IAudioSessionManager2_iface
;
142 static HANDLE g_timer_q
;
144 static CRITICAL_SECTION g_sessions_lock
;
145 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug
=
147 0, 0, &g_sessions_lock
,
148 { &g_sessions_lock_debug
.ProcessLocksList
, &g_sessions_lock_debug
.ProcessLocksList
},
149 0, 0, { (DWORD_PTR
)(__FILE__
": g_sessions_lock") }
151 static CRITICAL_SECTION g_sessions_lock
= { &g_sessions_lock_debug
, -1, 0, 0, 0, 0 };
152 static struct list g_sessions
= LIST_INIT(g_sessions
);
154 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
155 static const char defname
[] = "default";
157 static const IAudioClientVtbl AudioClient_Vtbl
;
158 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
159 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
160 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
161 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
162 static const IAudioClockVtbl AudioClock_Vtbl
;
163 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
164 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
165 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
166 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
168 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
170 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
172 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
175 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
177 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
180 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
182 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
185 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
187 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
190 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
192 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
195 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
197 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
200 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
202 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
205 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
207 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
210 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
212 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
215 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
217 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
220 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
224 case DLL_PROCESS_ATTACH
:
225 g_timer_q
= CreateTimerQueue();
230 case DLL_PROCESS_DETACH
:
231 g_sessions_lock
.DebugInfo
->Spare
[0] = 0;
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 void session_init_vols(AudioSession
*session
, UINT channels
)
724 if(session
->channel_count
< channels
){
727 if(session
->channel_vols
)
728 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
729 session
->channel_vols
, sizeof(float) * channels
);
731 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
732 sizeof(float) * channels
);
733 if(!session
->channel_vols
)
736 for(i
= session
->channel_count
; i
< channels
; ++i
)
737 session
->channel_vols
[i
] = 1.f
;
739 session
->channel_count
= channels
;
743 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
748 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
752 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
754 ret
->device
= device
;
756 list_init(&ret
->clients
);
758 list_add_head(&g_sessions
, &ret
->entry
);
760 InitializeCriticalSection(&ret
->lock
);
761 ret
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AudioSession.lock");
763 session_init_vols(ret
, num_channels
);
765 ret
->master_vol
= 1.f
;
770 /* if channels == 0, then this will return or create a session with
771 * matching dataflow and GUID. otherwise, channels must also match */
772 static HRESULT
get_audio_session(const GUID
*sessionguid
,
773 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
775 AudioSession
*session
;
777 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
778 *out
= create_session(&GUID_NULL
, device
, channels
);
780 return E_OUTOFMEMORY
;
786 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
787 if(session
->device
== device
&&
788 IsEqualGUID(sessionguid
, &session
->guid
)){
789 session_init_vols(session
, channels
);
796 *out
= create_session(sessionguid
, device
, channels
);
798 return E_OUTOFMEMORY
;
804 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
805 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
806 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
807 const GUID
*sessionguid
)
809 ACImpl
*This
= impl_from_IAudioClient(iface
);
810 snd_pcm_sw_params_t
*sw_params
= NULL
;
811 snd_pcm_format_t format
;
812 snd_pcm_uframes_t alsa_period_frames
;
813 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
814 unsigned int rate
, mmdev_period_frames
, alsa_period_us
;
818 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
819 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
824 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
825 return AUDCLNT_E_NOT_INITIALIZED
;
827 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
828 AUDCLNT_STREAMFLAGS_LOOPBACK
|
829 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
830 AUDCLNT_STREAMFLAGS_NOPERSIST
|
831 AUDCLNT_STREAMFLAGS_RATEADJUST
|
832 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
833 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
834 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
835 TRACE("Unknown flags: %08x\n", flags
);
840 duration
= 300000; /* 0.03s */
842 EnterCriticalSection(&This
->lock
);
845 LeaveCriticalSection(&This
->lock
);
846 return AUDCLNT_E_ALREADY_INITIALIZED
;
851 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
852 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
857 if((err
= snd_pcm_hw_params_set_access(This
->pcm_handle
, This
->hw_params
,
858 SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0){
859 WARN("Unable to set access: %d (%s)\n", err
, snd_strerror(err
));
864 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
865 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
866 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
867 if(fmt
->wBitsPerSample
== 8)
868 format
= SND_PCM_FORMAT_U8
;
869 else if(fmt
->wBitsPerSample
== 16)
870 format
= SND_PCM_FORMAT_S16_LE
;
871 else if(fmt
->wBitsPerSample
== 24)
872 format
= SND_PCM_FORMAT_S24_3LE
;
873 else if(fmt
->wBitsPerSample
== 32)
874 format
= SND_PCM_FORMAT_S32_LE
;
876 WARN("Unsupported bit depth: %u\n", fmt
->wBitsPerSample
);
877 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
880 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
881 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
882 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
883 if(fmt
->wBitsPerSample
== 32)
884 format
= SND_PCM_FORMAT_FLOAT_LE
;
885 else if(fmt
->wBitsPerSample
== 64)
886 format
= SND_PCM_FORMAT_FLOAT64_LE
;
888 WARN("Unsupported float size: %u\n", fmt
->wBitsPerSample
);
889 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
893 WARN("Unknown wave format: %04x\n", fmt
->wFormatTag
);
894 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
898 if((err
= snd_pcm_hw_params_set_format(This
->pcm_handle
, This
->hw_params
,
900 WARN("Unable to set ALSA format to %u: %d (%s)\n", format
, err
,
906 This
->alsa_format
= format
;
908 rate
= fmt
->nSamplesPerSec
;
909 if((err
= snd_pcm_hw_params_set_rate_near(This
->pcm_handle
, This
->hw_params
,
911 WARN("Unable to set rate to %u: %d (%s)\n", rate
, err
,
917 if((err
= snd_pcm_hw_params_set_channels(This
->pcm_handle
, This
->hw_params
,
918 fmt
->nChannels
)) < 0){
919 WARN("Unable to set channels to %u: %d (%s)\n", fmt
->nChannels
, err
,
925 alsa_period_us
= duration
/ 100; /* duration / 10 converted to us */
926 if((err
= snd_pcm_hw_params_set_period_time_near(This
->pcm_handle
,
927 This
->hw_params
, &alsa_period_us
, NULL
)) < 0){
928 WARN("Unable to set period time near %u: %d (%s)\n", alsa_period_us
,
929 err
, snd_strerror(err
));
934 if((err
= snd_pcm_hw_params(This
->pcm_handle
, This
->hw_params
)) < 0){
935 WARN("Unable to set hw params: %d (%s)\n", err
, snd_strerror(err
));
940 if((err
= snd_pcm_hw_params_current(This
->pcm_handle
, This
->hw_params
)) < 0){
941 WARN("Unable to get current hw params: %d (%s)\n", err
, snd_strerror(err
));
946 if((err
= snd_pcm_hw_params_get_period_size(This
->hw_params
,
947 &alsa_period_frames
, NULL
)) < 0){
948 WARN("Unable to get period size: %d (%s)\n", err
, snd_strerror(err
));
952 TRACE("alsa_period_frames: %lu\n", alsa_period_frames
);
954 if((err
= snd_pcm_hw_params_get_buffer_size(This
->hw_params
,
955 &This
->alsa_bufsize_frames
)) < 0){
956 WARN("Unable to get buffer size: %d (%s)\n", err
, snd_strerror(err
));
961 sw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_sw_params_sizeof());
967 if((err
= snd_pcm_sw_params_current(This
->pcm_handle
, sw_params
)) < 0){
968 WARN("Unable to get sw_params: %d (%s)\n", err
, snd_strerror(err
));
973 if((err
= snd_pcm_sw_params_set_start_threshold(This
->pcm_handle
,
975 WARN("Unable set start threshold to 0: %d (%s)\n", err
, snd_strerror(err
));
980 if((err
= snd_pcm_sw_params_set_stop_threshold(This
->pcm_handle
,
981 sw_params
, This
->alsa_bufsize_frames
)) < 0){
982 WARN("Unable set stop threshold to %lu: %d (%s)\n",
983 This
->alsa_bufsize_frames
, err
, snd_strerror(err
));
988 if((err
= snd_pcm_sw_params(This
->pcm_handle
, sw_params
)) < 0){
989 WARN("Unable set sw params: %d (%s)\n", err
, snd_strerror(err
));
994 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0){
995 WARN("Unable to prepare device: %d (%s)\n", err
, snd_strerror(err
));
1001 This
->mmdev_period_rt
= period
;
1003 This
->mmdev_period_rt
= DefaultPeriod
;
1005 /* Check if the ALSA buffer is so small that it will run out before
1006 * the next MMDevAPI period tick occurs. Allow a little wiggle room
1007 * with 120% of the period time. */
1008 mmdev_period_frames
= fmt
->nSamplesPerSec
* (This
->mmdev_period_rt
/ 10000000.);
1009 if(This
->alsa_bufsize_frames
< 1.2 * mmdev_period_frames
)
1010 FIXME("ALSA buffer time is smaller than our period time. Expect underruns. (%lu < %u)\n",
1011 This
->alsa_bufsize_frames
, mmdev_period_frames
);
1013 This
->bufsize_frames
= ceil((duration
/ 10000000.) * fmt
->nSamplesPerSec
);
1014 This
->local_buffer
= HeapAlloc(GetProcessHeap(), 0,
1015 This
->bufsize_frames
* fmt
->nBlockAlign
);
1016 if(!This
->local_buffer
){
1020 if (fmt
->wBitsPerSample
== 8)
1021 memset(This
->local_buffer
, 128, This
->bufsize_frames
* fmt
->nBlockAlign
);
1023 memset(This
->local_buffer
, 0, This
->bufsize_frames
* fmt
->nBlockAlign
);
1025 This
->fmt
= clone_format(fmt
);
1031 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
1037 for(i
= 0; i
< fmt
->nChannels
; ++i
)
1038 This
->vols
[i
] = 1.f
;
1041 This
->flags
= flags
;
1043 EnterCriticalSection(&g_sessions_lock
);
1045 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
1048 LeaveCriticalSection(&g_sessions_lock
);
1052 list_add_tail(&This
->session
->clients
, &This
->entry
);
1054 LeaveCriticalSection(&g_sessions_lock
);
1056 This
->initted
= TRUE
;
1059 HeapFree(GetProcessHeap(), 0, sw_params
);
1061 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
1062 This
->local_buffer
= NULL
;
1063 CoTaskMemFree(This
->fmt
);
1065 HeapFree(GetProcessHeap(), 0, This
->vols
);
1069 LeaveCriticalSection(&This
->lock
);
1074 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
1077 ACImpl
*This
= impl_from_IAudioClient(iface
);
1079 TRACE("(%p)->(%p)\n", This
, out
);
1084 EnterCriticalSection(&This
->lock
);
1087 LeaveCriticalSection(&This
->lock
);
1088 return AUDCLNT_E_NOT_INITIALIZED
;
1091 *out
= This
->bufsize_frames
;
1093 LeaveCriticalSection(&This
->lock
);
1098 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1099 REFERENCE_TIME
*latency
)
1101 ACImpl
*This
= impl_from_IAudioClient(iface
);
1103 TRACE("(%p)->(%p)\n", This
, latency
);
1108 EnterCriticalSection(&This
->lock
);
1111 LeaveCriticalSection(&This
->lock
);
1112 return AUDCLNT_E_NOT_INITIALIZED
;
1115 LeaveCriticalSection(&This
->lock
);
1122 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1125 ACImpl
*This
= impl_from_IAudioClient(iface
);
1127 TRACE("(%p)->(%p)\n", This
, out
);
1132 EnterCriticalSection(&This
->lock
);
1135 LeaveCriticalSection(&This
->lock
);
1136 return AUDCLNT_E_NOT_INITIALIZED
;
1139 if(This
->dataflow
== eRender
){
1140 snd_pcm_sframes_t avail_frames
;
1142 avail_frames
= snd_pcm_avail_update(This
->pcm_handle
);
1143 if(This
->alsa_bufsize_frames
< avail_frames
||
1144 snd_pcm_state(This
->pcm_handle
) == SND_PCM_STATE_XRUN
)
1145 *out
= This
->held_frames
;
1147 *out
= This
->alsa_bufsize_frames
- avail_frames
+ This
->held_frames
;
1148 TRACE("PCM state: %u, avail: %ld, pad: %u\n",
1149 snd_pcm_state(This
->pcm_handle
), avail_frames
, *out
);
1150 }else if(This
->dataflow
== eCapture
){
1151 *out
= This
->held_frames
;
1153 LeaveCriticalSection(&This
->lock
);
1154 return E_UNEXPECTED
;
1157 LeaveCriticalSection(&This
->lock
);
1162 static DWORD
get_channel_mask(unsigned int channels
)
1168 return KSAUDIO_SPEAKER_MONO
;
1170 return KSAUDIO_SPEAKER_STEREO
;
1172 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
1174 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
1176 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
1178 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
1180 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
1182 return KSAUDIO_SPEAKER_7POINT1
; /* not 7POINT1_SURROUND */
1184 FIXME("Unknown speaker configuration: %u\n", channels
);
1188 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1189 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*fmt
,
1192 ACImpl
*This
= impl_from_IAudioClient(iface
);
1193 snd_pcm_format_mask_t
*formats
= NULL
;
1195 WAVEFORMATEX
*closest
= NULL
;
1196 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
1197 unsigned int max
= 0, min
= 0;
1200 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, fmt
, out
);
1202 if(!fmt
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !out
))
1205 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1206 return E_INVALIDARG
;
1208 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1209 fmt
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1210 return E_INVALIDARG
;
1216 if(mode
!= AUDCLNT_SHAREMODE_SHARED
)
1220 EnterCriticalSection(&This
->lock
);
1222 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1227 formats
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1228 snd_pcm_format_mask_sizeof());
1234 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1236 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1237 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1238 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
1239 switch(fmt
->wBitsPerSample
){
1241 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1242 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1247 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1248 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1253 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1254 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1259 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1260 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1265 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1268 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
1269 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1270 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
1271 switch(fmt
->wBitsPerSample
){
1273 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1274 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1279 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT64_LE
)){
1280 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1285 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1289 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1293 closest
= clone_format(fmt
);
1299 if((err
= snd_pcm_hw_params_get_rate_min(This
->hw_params
, &min
, NULL
)) < 0){
1301 WARN("Unable to get min rate: %d (%s)\n", err
, snd_strerror(err
));
1305 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max
, NULL
)) < 0){
1307 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1311 if(fmt
->nSamplesPerSec
< min
|| fmt
->nSamplesPerSec
> max
){
1312 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1316 if((err
= snd_pcm_hw_params_get_channels_min(This
->hw_params
, &min
)) < 0){
1318 WARN("Unable to get min channels: %d (%s)\n", err
, snd_strerror(err
));
1322 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
, &max
)) < 0){
1324 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1329 if(fmt
->nChannels
> max
){
1331 closest
->nChannels
= max
;
1332 }else if(fmt
->nChannels
< min
){
1334 closest
->nChannels
= min
;
1337 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1338 DWORD mask
= get_channel_mask(closest
->nChannels
);
1340 ((WAVEFORMATEXTENSIBLE
*)closest
)->dwChannelMask
= mask
;
1342 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1343 fmtex
->dwChannelMask
!= 0 &&
1344 fmtex
->dwChannelMask
!= mask
)
1349 LeaveCriticalSection(&This
->lock
);
1350 HeapFree(GetProcessHeap(), 0, formats
);
1352 if(hr
== S_FALSE
&& !out
)
1353 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1355 if(hr
== S_FALSE
&& out
) {
1356 closest
->nBlockAlign
=
1357 closest
->nChannels
* closest
->wBitsPerSample
/ 8;
1358 closest
->nAvgBytesPerSec
=
1359 closest
->nBlockAlign
* closest
->nSamplesPerSec
;
1362 CoTaskMemFree(closest
);
1364 TRACE("returning: %08x\n", hr
);
1368 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1369 WAVEFORMATEX
**pwfx
)
1371 ACImpl
*This
= impl_from_IAudioClient(iface
);
1372 WAVEFORMATEXTENSIBLE
*fmt
;
1373 snd_pcm_format_mask_t
*formats
;
1374 unsigned int max_rate
, max_channels
;
1378 TRACE("(%p)->(%p)\n", This
, pwfx
);
1384 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1386 return E_OUTOFMEMORY
;
1388 formats
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_format_mask_sizeof());
1391 return E_OUTOFMEMORY
;
1394 EnterCriticalSection(&This
->lock
);
1396 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1397 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
1402 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1404 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1405 if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1406 fmt
->Format
.wBitsPerSample
= 32;
1407 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1408 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1409 fmt
->Format
.wBitsPerSample
= 16;
1410 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1411 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1412 fmt
->Format
.wBitsPerSample
= 8;
1413 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1414 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1415 fmt
->Format
.wBitsPerSample
= 32;
1416 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1417 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1418 fmt
->Format
.wBitsPerSample
= 24;
1419 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1421 ERR("Didn't recognize any available ALSA formats\n");
1426 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
,
1427 &max_channels
)) < 0){
1428 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1433 if(max_channels
> 2){
1434 FIXME("Don't know what to do with %u channels, pretending there's "
1435 "only 2 channels\n", max_channels
);
1436 fmt
->Format
.nChannels
= 2;
1438 fmt
->Format
.nChannels
= max_channels
;
1440 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1442 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max_rate
,
1444 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1449 if(max_rate
>= 48000)
1450 fmt
->Format
.nSamplesPerSec
= 48000;
1451 else if(max_rate
>= 44100)
1452 fmt
->Format
.nSamplesPerSec
= 44100;
1453 else if(max_rate
>= 22050)
1454 fmt
->Format
.nSamplesPerSec
= 22050;
1455 else if(max_rate
>= 11025)
1456 fmt
->Format
.nSamplesPerSec
= 11025;
1457 else if(max_rate
>= 8000)
1458 fmt
->Format
.nSamplesPerSec
= 8000;
1460 ERR("Unknown max rate: %u\n", max_rate
);
1465 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1466 fmt
->Format
.nChannels
) / 8;
1467 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1468 fmt
->Format
.nBlockAlign
;
1470 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1471 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1473 dump_fmt((WAVEFORMATEX
*)fmt
);
1474 *pwfx
= (WAVEFORMATEX
*)fmt
;
1477 LeaveCriticalSection(&This
->lock
);
1480 HeapFree(GetProcessHeap(), 0, formats
);
1485 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1486 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1488 ACImpl
*This
= impl_from_IAudioClient(iface
);
1490 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1492 if(!defperiod
&& !minperiod
)
1496 *defperiod
= DefaultPeriod
;
1498 *minperiod
= MinimumPeriod
;
1503 static snd_pcm_sframes_t
alsa_write_best_effort(snd_pcm_t
*handle
, BYTE
*buf
,
1504 snd_pcm_uframes_t frames
, ACImpl
*This
)
1506 snd_pcm_sframes_t written
;
1508 if(This
->session
->mute
){
1510 if((err
= snd_pcm_format_set_silence(This
->alsa_format
, buf
,
1511 frames
* This
->fmt
->nChannels
)) < 0)
1512 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1516 written
= snd_pcm_writei(handle
, buf
, frames
);
1520 if(written
== -EAGAIN
)
1524 WARN("writei failed, recovering: %ld (%s)\n", written
,
1525 snd_strerror(written
));
1527 ret
= snd_pcm_recover(handle
, written
, 0);
1529 WARN("Could not recover: %d (%s)\n", ret
, snd_strerror(ret
));
1533 written
= snd_pcm_writei(handle
, buf
, frames
);
1539 static void alsa_write_data(ACImpl
*This
)
1541 snd_pcm_sframes_t written
;
1542 snd_pcm_uframes_t to_write
, avail
;
1545 This
->local_buffer
+ (This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
);
1547 /* this call seems to be required to get an accurate snd_pcm_state() */
1548 avail
= snd_pcm_avail_update(This
->pcm_handle
);
1550 if(snd_pcm_state(This
->pcm_handle
) == SND_PCM_STATE_XRUN
||
1551 avail
> This
->alsa_bufsize_frames
){
1552 TRACE("XRun state, recovering\n");
1554 if((err
= snd_pcm_recover(This
->pcm_handle
, -EPIPE
, 1)) < 0)
1555 WARN("snd_pcm_recover failed: %d (%s)\n", err
, snd_strerror(err
));
1557 if((err
= snd_pcm_reset(This
->pcm_handle
)) < 0)
1558 WARN("snd_pcm_reset failed: %d (%s)\n", err
, snd_strerror(err
));
1560 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0)
1561 WARN("snd_pcm_prepare failed: %d (%s)\n", err
, snd_strerror(err
));
1564 if(This
->held_frames
== 0)
1567 if(This
->lcl_offs_frames
+ This
->held_frames
> This
->bufsize_frames
)
1568 to_write
= This
->bufsize_frames
- This
->lcl_offs_frames
;
1570 to_write
= This
->held_frames
;
1572 written
= alsa_write_best_effort(This
->pcm_handle
, buf
, to_write
, This
);
1574 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
1578 This
->lcl_offs_frames
+= written
;
1579 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1580 This
->held_frames
-= written
;
1582 if(written
< to_write
){
1583 /* ALSA buffer probably full */
1587 if(This
->held_frames
){
1588 /* wrapped and have some data back at the start to write */
1589 written
= alsa_write_best_effort(This
->pcm_handle
, This
->local_buffer
,
1590 This
->held_frames
, This
);
1592 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
1596 This
->lcl_offs_frames
+= written
;
1597 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1598 This
->held_frames
-= written
;
1602 static void alsa_read_data(ACImpl
*This
)
1604 snd_pcm_sframes_t pos
, readable
, nread
;
1606 pos
= (This
->held_frames
+ This
->lcl_offs_frames
) % This
->bufsize_frames
;
1607 readable
= This
->bufsize_frames
- pos
;
1609 nread
= snd_pcm_readi(This
->pcm_handle
,
1610 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, readable
);
1614 WARN("read failed, recovering: %ld (%s)\n", nread
, snd_strerror(nread
));
1616 ret
= snd_pcm_recover(This
->pcm_handle
, nread
, 0);
1618 WARN("Recover failed: %d (%s)\n", ret
, snd_strerror(ret
));
1622 nread
= snd_pcm_readi(This
->pcm_handle
,
1623 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, readable
);
1625 WARN("read failed: %ld (%s)\n", nread
, snd_strerror(nread
));
1630 if(This
->session
->mute
){
1632 if((err
= snd_pcm_format_set_silence(This
->alsa_format
,
1633 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
,
1635 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1639 This
->held_frames
+= nread
;
1641 if(This
->held_frames
> This
->bufsize_frames
){
1642 WARN("Overflow of unread data\n");
1643 This
->lcl_offs_frames
+= This
->held_frames
;
1644 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1645 This
->held_frames
= This
->bufsize_frames
;
1649 static void CALLBACK
alsa_push_buffer_data(void *user
, BOOLEAN timer
)
1651 ACImpl
*This
= user
;
1653 EnterCriticalSection(&This
->lock
);
1656 if(This
->dataflow
== eRender
)
1657 alsa_write_data(This
);
1658 else if(This
->dataflow
== eCapture
)
1659 alsa_read_data(This
);
1662 SetEvent(This
->event
);
1665 LeaveCriticalSection(&This
->lock
);
1668 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1670 ACImpl
*This
= impl_from_IAudioClient(iface
);
1672 TRACE("(%p)\n", This
);
1674 EnterCriticalSection(&This
->lock
);
1677 LeaveCriticalSection(&This
->lock
);
1678 return AUDCLNT_E_NOT_INITIALIZED
;
1681 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1682 LeaveCriticalSection(&This
->lock
);
1683 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1687 LeaveCriticalSection(&This
->lock
);
1688 return AUDCLNT_E_NOT_STOPPED
;
1691 if(This
->dataflow
== eCapture
){
1692 /* dump any data that might be leftover in the ALSA capture buffer */
1693 snd_pcm_readi(This
->pcm_handle
, This
->local_buffer
,
1694 This
->bufsize_frames
);
1697 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
, alsa_push_buffer_data
,
1698 This
, 0, This
->mmdev_period_rt
/ 10000, WT_EXECUTEINTIMERTHREAD
)){
1699 LeaveCriticalSection(&This
->lock
);
1700 WARN("Unable to create timer: %u\n", GetLastError());
1704 This
->started
= TRUE
;
1706 LeaveCriticalSection(&This
->lock
);
1711 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1713 ACImpl
*This
= impl_from_IAudioClient(iface
);
1717 TRACE("(%p)\n", This
);
1719 EnterCriticalSection(&This
->lock
);
1722 LeaveCriticalSection(&This
->lock
);
1723 return AUDCLNT_E_NOT_INITIALIZED
;
1727 LeaveCriticalSection(&This
->lock
);
1731 if(snd_pcm_drop(This
->pcm_handle
) < 0)
1732 WARN("snd_pcm_drop failed\n");
1734 if(snd_pcm_reset(This
->pcm_handle
) < 0)
1735 WARN("snd_pcm_reset failed\n");
1737 if(snd_pcm_prepare(This
->pcm_handle
) < 0)
1738 WARN("snd_pcm_prepare failed\n");
1740 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1741 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
1743 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1744 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
1746 This
->started
= FALSE
;
1748 LeaveCriticalSection(&This
->lock
);
1751 WaitForSingleObject(event
, INFINITE
);
1757 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1759 ACImpl
*This
= impl_from_IAudioClient(iface
);
1761 TRACE("(%p)\n", This
);
1763 EnterCriticalSection(&This
->lock
);
1766 LeaveCriticalSection(&This
->lock
);
1767 return AUDCLNT_E_NOT_INITIALIZED
;
1771 LeaveCriticalSection(&This
->lock
);
1772 return AUDCLNT_E_NOT_STOPPED
;
1775 if(This
->buf_state
!= NOT_LOCKED
){
1776 LeaveCriticalSection(&This
->lock
);
1777 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
1780 if(snd_pcm_drop(This
->pcm_handle
) < 0)
1781 WARN("snd_pcm_drop failed\n");
1783 if(snd_pcm_reset(This
->pcm_handle
) < 0)
1784 WARN("snd_pcm_reset failed\n");
1786 if(snd_pcm_prepare(This
->pcm_handle
) < 0)
1787 WARN("snd_pcm_prepare failed\n");
1789 This
->held_frames
= 0;
1790 This
->written_frames
= 0;
1791 This
->lcl_offs_frames
= 0;
1793 LeaveCriticalSection(&This
->lock
);
1798 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1801 ACImpl
*This
= impl_from_IAudioClient(iface
);
1803 TRACE("(%p)->(%p)\n", This
, event
);
1806 return E_INVALIDARG
;
1808 EnterCriticalSection(&This
->lock
);
1811 LeaveCriticalSection(&This
->lock
);
1812 return AUDCLNT_E_NOT_INITIALIZED
;
1815 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1816 LeaveCriticalSection(&This
->lock
);
1817 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1820 This
->event
= event
;
1822 LeaveCriticalSection(&This
->lock
);
1827 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1830 ACImpl
*This
= impl_from_IAudioClient(iface
);
1832 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1838 EnterCriticalSection(&This
->lock
);
1841 LeaveCriticalSection(&This
->lock
);
1842 return AUDCLNT_E_NOT_INITIALIZED
;
1845 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1846 if(This
->dataflow
!= eRender
){
1847 LeaveCriticalSection(&This
->lock
);
1848 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1850 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
1851 *ppv
= &This
->IAudioRenderClient_iface
;
1852 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1853 if(This
->dataflow
!= eCapture
){
1854 LeaveCriticalSection(&This
->lock
);
1855 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1857 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
1858 *ppv
= &This
->IAudioCaptureClient_iface
;
1859 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1860 IAudioClock_AddRef(&This
->IAudioClock_iface
);
1861 *ppv
= &This
->IAudioClock_iface
;
1862 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1863 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
1864 *ppv
= &This
->IAudioStreamVolume_iface
;
1865 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1866 if(!This
->session_wrapper
){
1867 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1868 if(!This
->session_wrapper
){
1869 LeaveCriticalSection(&This
->lock
);
1870 return E_OUTOFMEMORY
;
1873 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
1875 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1876 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1877 if(!This
->session_wrapper
){
1878 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1879 if(!This
->session_wrapper
){
1880 LeaveCriticalSection(&This
->lock
);
1881 return E_OUTOFMEMORY
;
1884 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
1886 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1887 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1888 if(!This
->session_wrapper
){
1889 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1890 if(!This
->session_wrapper
){
1891 LeaveCriticalSection(&This
->lock
);
1892 return E_OUTOFMEMORY
;
1895 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
1897 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1901 LeaveCriticalSection(&This
->lock
);
1905 LeaveCriticalSection(&This
->lock
);
1907 FIXME("stub %s\n", debugstr_guid(riid
));
1908 return E_NOINTERFACE
;
1911 static const IAudioClientVtbl AudioClient_Vtbl
=
1913 AudioClient_QueryInterface
,
1915 AudioClient_Release
,
1916 AudioClient_Initialize
,
1917 AudioClient_GetBufferSize
,
1918 AudioClient_GetStreamLatency
,
1919 AudioClient_GetCurrentPadding
,
1920 AudioClient_IsFormatSupported
,
1921 AudioClient_GetMixFormat
,
1922 AudioClient_GetDevicePeriod
,
1926 AudioClient_SetEventHandle
,
1927 AudioClient_GetService
1930 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1931 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1933 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1939 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1940 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1943 IUnknown_AddRef((IUnknown
*)*ppv
);
1947 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1948 return E_NOINTERFACE
;
1951 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1953 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1954 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1957 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1959 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1960 return AudioClient_Release(&This
->IAudioClient_iface
);
1963 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1964 UINT32 frames
, BYTE
**data
)
1966 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1971 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1976 EnterCriticalSection(&This
->lock
);
1978 if(This
->buf_state
!= NOT_LOCKED
){
1979 LeaveCriticalSection(&This
->lock
);
1980 return AUDCLNT_E_OUT_OF_ORDER
;
1984 This
->buf_state
= LOCKED_NORMAL
;
1985 LeaveCriticalSection(&This
->lock
);
1989 hr
= IAudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, &pad
);
1991 LeaveCriticalSection(&This
->lock
);
1995 if(pad
+ frames
> This
->bufsize_frames
){
1996 LeaveCriticalSection(&This
->lock
);
1997 return AUDCLNT_E_BUFFER_TOO_LARGE
;
2001 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
2002 if(write_pos
+ frames
> This
->bufsize_frames
){
2003 if(This
->tmp_buffer_frames
< frames
){
2004 if(This
->tmp_buffer
)
2005 This
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0,
2006 This
->tmp_buffer
, frames
* This
->fmt
->nBlockAlign
);
2008 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2009 frames
* This
->fmt
->nBlockAlign
);
2010 if(!This
->tmp_buffer
){
2011 LeaveCriticalSection(&This
->lock
);
2012 return E_OUTOFMEMORY
;
2014 This
->tmp_buffer_frames
= frames
;
2016 *data
= This
->tmp_buffer
;
2017 This
->buf_state
= LOCKED_WRAPPED
;
2019 *data
= This
->local_buffer
+ write_pos
* This
->fmt
->nBlockAlign
;
2020 This
->buf_state
= LOCKED_NORMAL
;
2023 LeaveCriticalSection(&This
->lock
);
2028 static void alsa_wrap_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 written_frames
)
2030 snd_pcm_uframes_t write_offs_frames
=
2031 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
2032 UINT32 write_offs_bytes
= write_offs_frames
* This
->fmt
->nBlockAlign
;
2033 snd_pcm_uframes_t chunk_frames
= This
->bufsize_frames
- write_offs_frames
;
2034 UINT32 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
2035 UINT32 written_bytes
= written_frames
* This
->fmt
->nBlockAlign
;
2037 if(written_bytes
<= chunk_bytes
){
2038 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, written_bytes
);
2040 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, chunk_bytes
);
2041 memcpy(This
->local_buffer
, buffer
+ chunk_bytes
,
2042 written_bytes
- chunk_bytes
);
2046 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
2047 IAudioRenderClient
*iface
, UINT32 written_frames
, DWORD flags
)
2049 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2052 TRACE("(%p)->(%u, %x)\n", This
, written_frames
, flags
);
2054 EnterCriticalSection(&This
->lock
);
2056 if(This
->buf_state
== NOT_LOCKED
|| !written_frames
){
2057 This
->buf_state
= NOT_LOCKED
;
2058 LeaveCriticalSection(&This
->lock
);
2059 return written_frames
? AUDCLNT_E_OUT_OF_ORDER
: S_OK
;
2062 if(This
->buf_state
== LOCKED_NORMAL
)
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
->buf_state
== LOCKED_WRAPPED
)
2076 alsa_wrap_buffer(This
, buffer
, written_frames
);
2078 This
->held_frames
+= written_frames
;
2079 This
->written_frames
+= written_frames
;
2080 This
->buf_state
= NOT_LOCKED
;
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 if(This
->tmp_buffer
)
2160 This
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0,
2161 This
->tmp_buffer
, *frames
* This
->fmt
->nBlockAlign
);
2163 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2164 *frames
* This
->fmt
->nBlockAlign
);
2165 if(!This
->tmp_buffer
){
2166 LeaveCriticalSection(&This
->lock
);
2167 return E_OUTOFMEMORY
;
2169 This
->tmp_buffer_frames
= *frames
;
2172 *data
= This
->tmp_buffer
;
2173 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) *
2174 This
->fmt
->nBlockAlign
;
2175 offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2176 frames_bytes
= *frames
* This
->fmt
->nBlockAlign
;
2177 memcpy(This
->tmp_buffer
, This
->local_buffer
+ offs_bytes
, chunk_bytes
);
2178 memcpy(This
->tmp_buffer
+ chunk_bytes
, This
->local_buffer
,
2179 frames_bytes
- chunk_bytes
);
2181 *data
= This
->local_buffer
+
2182 This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2184 This
->buf_state
= LOCKED_NORMAL
;
2186 if(devpos
|| qpcpos
)
2187 IAudioClock_GetPosition(&This
->IAudioClock_iface
, devpos
, qpcpos
);
2189 LeaveCriticalSection(&This
->lock
);
2191 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
2194 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2195 IAudioCaptureClient
*iface
, UINT32 done
)
2197 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2199 TRACE("(%p)->(%u)\n", This
, done
);
2201 EnterCriticalSection(&This
->lock
);
2203 if(This
->buf_state
== NOT_LOCKED
){
2204 LeaveCriticalSection(&This
->lock
);
2205 return AUDCLNT_E_OUT_OF_ORDER
;
2208 This
->held_frames
-= done
;
2209 This
->lcl_offs_frames
+= done
;
2210 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2212 This
->buf_state
= NOT_LOCKED
;
2214 LeaveCriticalSection(&This
->lock
);
2219 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2220 IAudioCaptureClient
*iface
, UINT32
*frames
)
2222 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2224 TRACE("(%p)->(%p)\n", This
, frames
);
2226 return AudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, frames
);
2229 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2231 AudioCaptureClient_QueryInterface
,
2232 AudioCaptureClient_AddRef
,
2233 AudioCaptureClient_Release
,
2234 AudioCaptureClient_GetBuffer
,
2235 AudioCaptureClient_ReleaseBuffer
,
2236 AudioCaptureClient_GetNextPacketSize
2239 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2240 REFIID riid
, void **ppv
)
2242 ACImpl
*This
= impl_from_IAudioClock(iface
);
2244 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2250 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2252 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2253 *ppv
= &This
->IAudioClock2_iface
;
2255 IUnknown_AddRef((IUnknown
*)*ppv
);
2259 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2260 return E_NOINTERFACE
;
2263 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2265 ACImpl
*This
= impl_from_IAudioClock(iface
);
2266 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2269 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2271 ACImpl
*This
= impl_from_IAudioClock(iface
);
2272 return IAudioClient_Release(&This
->IAudioClient_iface
);
2275 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2277 ACImpl
*This
= impl_from_IAudioClock(iface
);
2279 TRACE("(%p)->(%p)\n", This
, freq
);
2281 *freq
= This
->fmt
->nSamplesPerSec
;
2286 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2289 ACImpl
*This
= impl_from_IAudioClock(iface
);
2293 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2298 EnterCriticalSection(&This
->lock
);
2300 hr
= IAudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, &pad
);
2302 LeaveCriticalSection(&This
->lock
);
2306 if(This
->dataflow
== eRender
)
2307 *pos
= This
->written_frames
- pad
;
2308 else if(This
->dataflow
== eCapture
)
2309 *pos
= This
->written_frames
+ pad
;
2311 LeaveCriticalSection(&This
->lock
);
2314 LARGE_INTEGER stamp
, freq
;
2315 QueryPerformanceCounter(&stamp
);
2316 QueryPerformanceFrequency(&freq
);
2317 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2323 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2326 ACImpl
*This
= impl_from_IAudioClock(iface
);
2328 TRACE("(%p)->(%p)\n", This
, chars
);
2333 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2338 static const IAudioClockVtbl AudioClock_Vtbl
=
2340 AudioClock_QueryInterface
,
2343 AudioClock_GetFrequency
,
2344 AudioClock_GetPosition
,
2345 AudioClock_GetCharacteristics
2348 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2349 REFIID riid
, void **ppv
)
2351 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2352 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2355 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2357 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2358 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2361 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2363 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2364 return IAudioClient_Release(&This
->IAudioClient_iface
);
2367 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2368 UINT64
*pos
, UINT64
*qpctime
)
2370 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2372 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2377 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2379 AudioClock2_QueryInterface
,
2381 AudioClock2_Release
,
2382 AudioClock2_GetDevicePosition
2385 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2387 AudioSessionWrapper
*ret
;
2389 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2390 sizeof(AudioSessionWrapper
));
2394 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2395 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2396 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2400 ret
->client
= client
;
2402 ret
->session
= client
->session
;
2403 AudioClient_AddRef(&client
->IAudioClient_iface
);
2409 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2410 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2412 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2418 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2419 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2420 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2423 IUnknown_AddRef((IUnknown
*)*ppv
);
2427 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2428 return E_NOINTERFACE
;
2431 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2433 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2435 ref
= InterlockedIncrement(&This
->ref
);
2436 TRACE("(%p) Refcount now %u\n", This
, ref
);
2440 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2442 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2444 ref
= InterlockedDecrement(&This
->ref
);
2445 TRACE("(%p) Refcount now %u\n", This
, ref
);
2448 EnterCriticalSection(&This
->client
->lock
);
2449 This
->client
->session_wrapper
= NULL
;
2450 LeaveCriticalSection(&This
->client
->lock
);
2451 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2453 HeapFree(GetProcessHeap(), 0, This
);
2458 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2459 AudioSessionState
*state
)
2461 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2464 TRACE("(%p)->(%p)\n", This
, state
);
2467 return NULL_PTR_ERR
;
2469 EnterCriticalSection(&g_sessions_lock
);
2471 if(list_empty(&This
->session
->clients
)){
2472 *state
= AudioSessionStateExpired
;
2473 LeaveCriticalSection(&g_sessions_lock
);
2477 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2478 EnterCriticalSection(&client
->lock
);
2479 if(client
->started
){
2480 *state
= AudioSessionStateActive
;
2481 LeaveCriticalSection(&client
->lock
);
2482 LeaveCriticalSection(&g_sessions_lock
);
2485 LeaveCriticalSection(&client
->lock
);
2488 LeaveCriticalSection(&g_sessions_lock
);
2490 *state
= AudioSessionStateInactive
;
2495 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2496 IAudioSessionControl2
*iface
, WCHAR
**name
)
2498 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2500 FIXME("(%p)->(%p) - stub\n", This
, name
);
2505 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2506 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2508 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2510 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2515 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2516 IAudioSessionControl2
*iface
, WCHAR
**path
)
2518 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2520 FIXME("(%p)->(%p) - stub\n", This
, path
);
2525 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2526 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2528 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2530 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2535 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2536 IAudioSessionControl2
*iface
, GUID
*group
)
2538 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2540 FIXME("(%p)->(%p) - stub\n", This
, group
);
2545 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2546 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2548 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2550 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2551 debugstr_guid(session
));
2556 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2557 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2559 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2561 FIXME("(%p)->(%p) - stub\n", This
, events
);
2566 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2567 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2569 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2571 FIXME("(%p)->(%p) - stub\n", This
, events
);
2576 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2577 IAudioSessionControl2
*iface
, WCHAR
**id
)
2579 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2581 FIXME("(%p)->(%p) - stub\n", This
, id
);
2586 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2587 IAudioSessionControl2
*iface
, WCHAR
**id
)
2589 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2591 FIXME("(%p)->(%p) - stub\n", This
, id
);
2596 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2597 IAudioSessionControl2
*iface
, DWORD
*pid
)
2599 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2601 TRACE("(%p)->(%p)\n", This
, pid
);
2606 *pid
= GetCurrentProcessId();
2611 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2612 IAudioSessionControl2
*iface
)
2614 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2616 TRACE("(%p)\n", This
);
2621 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2622 IAudioSessionControl2
*iface
, BOOL optout
)
2624 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2626 TRACE("(%p)->(%d)\n", This
, optout
);
2631 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2633 AudioSessionControl_QueryInterface
,
2634 AudioSessionControl_AddRef
,
2635 AudioSessionControl_Release
,
2636 AudioSessionControl_GetState
,
2637 AudioSessionControl_GetDisplayName
,
2638 AudioSessionControl_SetDisplayName
,
2639 AudioSessionControl_GetIconPath
,
2640 AudioSessionControl_SetIconPath
,
2641 AudioSessionControl_GetGroupingParam
,
2642 AudioSessionControl_SetGroupingParam
,
2643 AudioSessionControl_RegisterAudioSessionNotification
,
2644 AudioSessionControl_UnregisterAudioSessionNotification
,
2645 AudioSessionControl_GetSessionIdentifier
,
2646 AudioSessionControl_GetSessionInstanceIdentifier
,
2647 AudioSessionControl_GetProcessId
,
2648 AudioSessionControl_IsSystemSoundsSession
,
2649 AudioSessionControl_SetDuckingPreference
2652 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2653 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2655 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2661 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2662 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2665 IUnknown_AddRef((IUnknown
*)*ppv
);
2669 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2670 return E_NOINTERFACE
;
2673 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2675 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2676 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2679 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2681 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2682 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2685 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2686 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2688 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2689 AudioSession
*session
= This
->session
;
2691 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2693 if(level
< 0.f
|| level
> 1.f
)
2694 return E_INVALIDARG
;
2697 FIXME("Notifications not supported yet\n");
2699 TRACE("ALSA does not support volume control\n");
2701 EnterCriticalSection(&session
->lock
);
2703 session
->master_vol
= level
;
2705 LeaveCriticalSection(&session
->lock
);
2710 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2711 ISimpleAudioVolume
*iface
, float *level
)
2713 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2714 AudioSession
*session
= This
->session
;
2716 TRACE("(%p)->(%p)\n", session
, level
);
2719 return NULL_PTR_ERR
;
2721 *level
= session
->master_vol
;
2726 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2727 BOOL mute
, const GUID
*context
)
2729 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2730 AudioSession
*session
= This
->session
;
2732 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2735 FIXME("Notifications not supported yet\n");
2737 session
->mute
= mute
;
2742 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2745 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2746 AudioSession
*session
= This
->session
;
2748 TRACE("(%p)->(%p)\n", session
, mute
);
2751 return NULL_PTR_ERR
;
2753 *mute
= session
->mute
;
2758 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2760 SimpleAudioVolume_QueryInterface
,
2761 SimpleAudioVolume_AddRef
,
2762 SimpleAudioVolume_Release
,
2763 SimpleAudioVolume_SetMasterVolume
,
2764 SimpleAudioVolume_GetMasterVolume
,
2765 SimpleAudioVolume_SetMute
,
2766 SimpleAudioVolume_GetMute
2769 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2770 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2772 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2778 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2779 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2782 IUnknown_AddRef((IUnknown
*)*ppv
);
2786 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2787 return E_NOINTERFACE
;
2790 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2792 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2793 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2796 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2798 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2799 return IAudioClient_Release(&This
->IAudioClient_iface
);
2802 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2803 IAudioStreamVolume
*iface
, UINT32
*out
)
2805 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2807 TRACE("(%p)->(%p)\n", This
, out
);
2812 *out
= This
->fmt
->nChannels
;
2817 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2818 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2820 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2822 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2824 if(level
< 0.f
|| level
> 1.f
)
2825 return E_INVALIDARG
;
2827 if(index
>= This
->fmt
->nChannels
)
2828 return E_INVALIDARG
;
2830 TRACE("ALSA does not support volume control\n");
2832 EnterCriticalSection(&This
->lock
);
2834 This
->vols
[index
] = level
;
2836 LeaveCriticalSection(&This
->lock
);
2841 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2842 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2844 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2846 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2851 if(index
>= This
->fmt
->nChannels
)
2852 return E_INVALIDARG
;
2854 *level
= This
->vols
[index
];
2859 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2860 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2862 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2865 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2870 if(count
!= This
->fmt
->nChannels
)
2871 return E_INVALIDARG
;
2873 TRACE("ALSA does not support volume control\n");
2875 EnterCriticalSection(&This
->lock
);
2877 for(i
= 0; i
< count
; ++i
)
2878 This
->vols
[i
] = levels
[i
];
2880 LeaveCriticalSection(&This
->lock
);
2885 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2886 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2888 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2891 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2896 if(count
!= This
->fmt
->nChannels
)
2897 return E_INVALIDARG
;
2899 EnterCriticalSection(&This
->lock
);
2901 for(i
= 0; i
< count
; ++i
)
2902 levels
[i
] = This
->vols
[i
];
2904 LeaveCriticalSection(&This
->lock
);
2909 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2911 AudioStreamVolume_QueryInterface
,
2912 AudioStreamVolume_AddRef
,
2913 AudioStreamVolume_Release
,
2914 AudioStreamVolume_GetChannelCount
,
2915 AudioStreamVolume_SetChannelVolume
,
2916 AudioStreamVolume_GetChannelVolume
,
2917 AudioStreamVolume_SetAllVolumes
,
2918 AudioStreamVolume_GetAllVolumes
2921 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2922 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2924 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2930 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2931 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2934 IUnknown_AddRef((IUnknown
*)*ppv
);
2938 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2939 return E_NOINTERFACE
;
2942 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2944 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2945 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2948 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2950 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2951 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2954 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
2955 IChannelAudioVolume
*iface
, UINT32
*out
)
2957 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2958 AudioSession
*session
= This
->session
;
2960 TRACE("(%p)->(%p)\n", session
, out
);
2963 return NULL_PTR_ERR
;
2965 *out
= session
->channel_count
;
2970 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
2971 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
2972 const GUID
*context
)
2974 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2975 AudioSession
*session
= This
->session
;
2977 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
2978 wine_dbgstr_guid(context
));
2980 if(level
< 0.f
|| level
> 1.f
)
2981 return E_INVALIDARG
;
2983 if(index
>= session
->channel_count
)
2984 return E_INVALIDARG
;
2987 FIXME("Notifications not supported yet\n");
2989 TRACE("ALSA does not support volume control\n");
2991 EnterCriticalSection(&session
->lock
);
2993 session
->channel_vols
[index
] = level
;
2995 LeaveCriticalSection(&session
->lock
);
3000 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
3001 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
3003 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3004 AudioSession
*session
= This
->session
;
3006 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
3009 return NULL_PTR_ERR
;
3011 if(index
>= session
->channel_count
)
3012 return E_INVALIDARG
;
3014 *level
= session
->channel_vols
[index
];
3019 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
3020 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
3021 const GUID
*context
)
3023 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3024 AudioSession
*session
= This
->session
;
3027 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
3028 wine_dbgstr_guid(context
));
3031 return NULL_PTR_ERR
;
3033 if(count
!= session
->channel_count
)
3034 return E_INVALIDARG
;
3037 FIXME("Notifications not supported yet\n");
3039 TRACE("ALSA does not support volume control\n");
3041 EnterCriticalSection(&session
->lock
);
3043 for(i
= 0; i
< count
; ++i
)
3044 session
->channel_vols
[i
] = levels
[i
];
3046 LeaveCriticalSection(&session
->lock
);
3051 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
3052 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
3054 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3055 AudioSession
*session
= This
->session
;
3058 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
3061 return NULL_PTR_ERR
;
3063 if(count
!= session
->channel_count
)
3064 return E_INVALIDARG
;
3066 for(i
= 0; i
< count
; ++i
)
3067 levels
[i
] = session
->channel_vols
[i
];
3072 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
3074 ChannelAudioVolume_QueryInterface
,
3075 ChannelAudioVolume_AddRef
,
3076 ChannelAudioVolume_Release
,
3077 ChannelAudioVolume_GetChannelCount
,
3078 ChannelAudioVolume_SetChannelVolume
,
3079 ChannelAudioVolume_GetChannelVolume
,
3080 ChannelAudioVolume_SetAllVolumes
,
3081 ChannelAudioVolume_GetAllVolumes
3084 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
3085 REFIID riid
, void **ppv
)
3087 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3093 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3094 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
3095 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
3098 IUnknown_AddRef((IUnknown
*)*ppv
);
3102 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3103 return E_NOINTERFACE
;
3106 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
3108 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3110 ref
= InterlockedIncrement(&This
->ref
);
3111 TRACE("(%p) Refcount now %u\n", This
, ref
);
3115 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
3117 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3119 ref
= InterlockedDecrement(&This
->ref
);
3120 TRACE("(%p) Refcount now %u\n", This
, ref
);
3122 HeapFree(GetProcessHeap(), 0, This
);
3126 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3127 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3128 IAudioSessionControl
**out
)
3130 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3131 AudioSession
*session
;
3132 AudioSessionWrapper
*wrapper
;
3135 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3138 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3142 wrapper
= AudioSessionWrapper_Create(NULL
);
3144 return E_OUTOFMEMORY
;
3146 wrapper
->session
= session
;
3148 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3153 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3154 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3155 ISimpleAudioVolume
**out
)
3157 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3158 AudioSession
*session
;
3159 AudioSessionWrapper
*wrapper
;
3162 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3165 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3169 wrapper
= AudioSessionWrapper_Create(NULL
);
3171 return E_OUTOFMEMORY
;
3173 wrapper
->session
= session
;
3175 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3180 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3181 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3183 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3184 FIXME("(%p)->(%p) - stub\n", This
, out
);
3188 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3189 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3191 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3192 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3196 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3197 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3199 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3200 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3204 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3205 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3206 IAudioVolumeDuckNotification
*notification
)
3208 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3209 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3213 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3214 IAudioSessionManager2
*iface
,
3215 IAudioVolumeDuckNotification
*notification
)
3217 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3218 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3222 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3224 AudioSessionManager_QueryInterface
,
3225 AudioSessionManager_AddRef
,
3226 AudioSessionManager_Release
,
3227 AudioSessionManager_GetAudioSessionControl
,
3228 AudioSessionManager_GetSimpleAudioVolume
,
3229 AudioSessionManager_GetSessionEnumerator
,
3230 AudioSessionManager_RegisterSessionNotification
,
3231 AudioSessionManager_UnregisterSessionNotification
,
3232 AudioSessionManager_RegisterDuckNotification
,
3233 AudioSessionManager_UnregisterDuckNotification
3236 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3237 IAudioSessionManager2
**out
)
3241 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3243 return E_OUTOFMEMORY
;
3245 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3246 This
->device
= device
;
3249 *out
= &This
->IAudioSessionManager2_iface
;