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"
40 #include "endpointvolume.h"
43 #include "audioclient.h"
44 #include "audiopolicy.h"
47 #include <alsa/asoundlib.h>
49 WINE_DEFAULT_DEBUG_CHANNEL(alsa
);
51 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
53 static const REFERENCE_TIME DefaultPeriod
= 200000;
54 static const REFERENCE_TIME MinimumPeriod
= 100000;
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 period_alsa
, bufsize_alsa
;
98 snd_pcm_hw_params_t
*hw_params
; /* does not hold state between calls */
99 snd_pcm_format_t alsa_format
;
106 AUDCLNT_SHAREMODE share
;
110 BOOL initted
, started
;
111 UINT64 written_frames
, held_frames
, tmp_buffer_frames
;
112 UINT32 bufsize_frames
, period_us
;
113 UINT32 lcl_offs_frames
; /* offs into local_buffer where valid data starts */
116 BYTE
*local_buffer
, *tmp_buffer
;
119 CRITICAL_SECTION lock
;
121 AudioSession
*session
;
122 AudioSessionWrapper
*session_wrapper
;
129 LOCKED_NORMAL
, /* public buffer piece is from local_buffer */
130 LOCKED_WRAPPED
/* public buffer piece is wrapped around, in tmp_buffer */
133 typedef struct _SessionMgr
{
134 IAudioSessionManager2 IAudioSessionManager2_iface
;
141 static HANDLE g_timer_q
;
143 static CRITICAL_SECTION g_sessions_lock
;
144 static struct list g_sessions
= LIST_INIT(g_sessions
);
146 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
147 static const char defname
[] = "default";
149 static const IAudioClientVtbl AudioClient_Vtbl
;
150 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
151 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
152 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
153 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
154 static const IAudioClockVtbl AudioClock_Vtbl
;
155 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
156 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
157 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
158 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
160 int wine_snd_pcm_recover(snd_pcm_t
*pcm
, int err
, int silent
);
161 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
163 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
165 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
168 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
170 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
173 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
175 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
178 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
180 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
183 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
185 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
188 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
190 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
193 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
195 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
198 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
200 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
203 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
205 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
208 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
210 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
213 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
215 if(reason
== DLL_PROCESS_ATTACH
){
216 g_timer_q
= CreateTimerQueue();
220 InitializeCriticalSection(&g_sessions_lock
);
226 static HRESULT
alsa_get_card_devices(EDataFlow flow
, WCHAR
**ids
, char **keys
,
227 UINT
*num
, snd_ctl_t
*ctl
, int card
, const WCHAR
*cardnameW
)
229 static const WCHAR dashW
[] = {' ','-',' ',0};
231 snd_pcm_info_t
*info
;
232 snd_pcm_stream_t stream
= (flow
== eRender
? SND_PCM_STREAM_PLAYBACK
:
233 SND_PCM_STREAM_CAPTURE
);
235 info
= HeapAlloc(GetProcessHeap(), 0, snd_pcm_info_sizeof());
237 return E_OUTOFMEMORY
;
239 snd_pcm_info_set_subdevice(info
, 0);
240 snd_pcm_info_set_stream(info
, stream
);
243 for(err
= snd_ctl_pcm_next_device(ctl
, &device
); device
!= -1 && err
>= 0;
244 err
= snd_ctl_pcm_next_device(ctl
, &device
)){
249 snd_pcm_info_set_device(info
, device
);
251 if((err
= snd_ctl_pcm_info(ctl
, info
)) < 0){
253 /* This device doesn't have the right stream direction */
256 WARN("Failed to get info for card %d, device %d: %d (%s)\n",
257 card
, device
, err
, snd_strerror(err
));
261 sprintf(devnode
, "hw:%d,%d", card
, device
);
262 if((err
= snd_pcm_open(&handle
, devnode
, stream
, SND_PCM_NONBLOCK
)) < 0){
263 WARN("The device \"%s\" failed to open, pretending it doesn't exist: %d (%s)\n",
264 devnode
, err
, snd_strerror(err
));
268 snd_pcm_close(handle
);
273 devname
= snd_pcm_info_get_name(info
);
275 WARN("Unable to get device name for card %d, device %d\n", card
,
280 cardlen
= lstrlenW(cardnameW
);
281 len
= MultiByteToWideChar(CP_UNIXCP
, 0, devname
, -1, NULL
, 0);
282 len
+= lstrlenW(dashW
);
284 ids
[*num
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
286 HeapFree(GetProcessHeap(), 0, info
);
287 return E_OUTOFMEMORY
;
289 memcpy(ids
[*num
], cardnameW
, cardlen
* sizeof(WCHAR
));
290 memcpy(ids
[*num
] + cardlen
, dashW
, lstrlenW(dashW
) * sizeof(WCHAR
));
291 cardlen
+= lstrlenW(dashW
);
292 MultiByteToWideChar(CP_UNIXCP
, 0, devname
, -1, ids
[*num
] + cardlen
,
295 keys
[*num
] = HeapAlloc(GetProcessHeap(), 0, 32);
297 HeapFree(GetProcessHeap(), 0, info
);
298 HeapFree(GetProcessHeap(), 0, ids
[*num
]);
299 return E_OUTOFMEMORY
;
301 memcpy(keys
[*num
], devnode
, sizeof(devnode
));
307 HeapFree(GetProcessHeap(), 0, info
);
310 WARN("Got a failure during device enumeration on card %d: %d (%s)\n",
311 card
, err
, snd_strerror(err
));
316 static HRESULT
alsa_enum_devices(EDataFlow flow
, WCHAR
**ids
, char **keys
,
323 for(err
= snd_card_next(&card
); card
!= -1 && err
>= 0;
324 err
= snd_card_next(&card
)){
326 const char *cardname
;
331 sprintf(cardpath
, "hw:%u", card
);
333 if((err
= snd_ctl_open(&ctl
, cardpath
, 0)) < 0){
334 WARN("Unable to open ctl for ALSA device %s: %d (%s)\n", cardpath
,
335 err
, snd_strerror(err
));
339 if((err
= snd_card_get_name(card
, (char **)&cardname
)) < 0){
340 WARN("Unable to get card name for ALSA device %s: %d (%s)\n",
341 cardpath
, err
, snd_strerror(err
));
342 /* FIXME: Should be localized */
343 cardname
= "Unknown soundcard";
346 len
= MultiByteToWideChar(CP_UNIXCP
, 0, cardname
, -1, NULL
, 0);
347 cardnameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
350 return E_OUTOFMEMORY
;
352 MultiByteToWideChar(CP_UNIXCP
, 0, cardname
, -1, cardnameW
, len
);
354 alsa_get_card_devices(flow
, ids
, keys
, num
, ctl
, card
, cardnameW
);
356 HeapFree(GetProcessHeap(), 0, cardnameW
);
362 WARN("Got a failure during card enumeration: %d (%s)\n",
363 err
, snd_strerror(err
));
368 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
, char ***keys
,
369 UINT
*num
, UINT
*def_index
)
373 TRACE("%d %p %p %p %p\n", flow
, ids
, keys
, num
, def_index
);
375 hr
= alsa_enum_devices(flow
, NULL
, NULL
, num
);
379 *ids
= HeapAlloc(GetProcessHeap(), 0, (*num
+ 1) * sizeof(WCHAR
*));
380 *keys
= HeapAlloc(GetProcessHeap(), 0, (*num
+ 1) * sizeof(char *));
382 HeapFree(GetProcessHeap(), 0, *ids
);
383 HeapFree(GetProcessHeap(), 0, *keys
);
384 return E_OUTOFMEMORY
;
387 (*ids
)[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW
));
388 memcpy((*ids
)[0], defaultW
, sizeof(defaultW
));
389 (*keys
)[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(defname
));
390 memcpy((*keys
)[0], defname
, sizeof(defname
));
393 hr
= alsa_enum_devices(flow
, (*ids
) + 1, (*keys
) + 1, num
);
396 for(i
= 0; i
< *num
; ++i
){
397 HeapFree(GetProcessHeap(), 0, (*ids
)[i
]);
398 HeapFree(GetProcessHeap(), 0, (*keys
)[i
]);
400 HeapFree(GetProcessHeap(), 0, *ids
);
401 HeapFree(GetProcessHeap(), 0, *keys
);
402 return E_OUTOFMEMORY
;
405 ++(*num
); /* for default device */
410 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(const char *key
, IMMDevice
*dev
,
411 EDataFlow dataflow
, IAudioClient
**out
)
415 snd_pcm_stream_t stream
;
417 TRACE("\"%s\" %p %d %p\n", key
, dev
, dataflow
, out
);
419 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
421 return E_OUTOFMEMORY
;
423 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
424 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
425 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
426 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
427 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
428 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
430 if(dataflow
== eRender
)
431 stream
= SND_PCM_STREAM_PLAYBACK
;
432 else if(dataflow
== eCapture
)
433 stream
= SND_PCM_STREAM_CAPTURE
;
435 HeapFree(GetProcessHeap(), 0, This
);
439 This
->dataflow
= dataflow
;
440 if((err
= snd_pcm_open(&This
->pcm_handle
, key
, stream
,
441 SND_PCM_NONBLOCK
)) < 0){
442 HeapFree(GetProcessHeap(), 0, This
);
443 WARN("Unable to open PCM \"%s\": %d (%s)\n", key
, err
,
448 This
->hw_params
= HeapAlloc(GetProcessHeap(), 0,
449 snd_pcm_hw_params_sizeof());
450 if(!This
->hw_params
){
451 HeapFree(GetProcessHeap(), 0, This
);
452 snd_pcm_close(This
->pcm_handle
);
453 return E_OUTOFMEMORY
;
456 InitializeCriticalSection(&This
->lock
);
459 IMMDevice_AddRef(This
->parent
);
461 *out
= &This
->IAudioClient_iface
;
462 IAudioClient_AddRef(&This
->IAudioClient_iface
);
467 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
468 REFIID riid
, void **ppv
)
470 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
475 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
478 IUnknown_AddRef((IUnknown
*)*ppv
);
481 WARN("Unknown interface %s\n", debugstr_guid(riid
));
482 return E_NOINTERFACE
;
485 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
487 ACImpl
*This
= impl_from_IAudioClient(iface
);
489 ref
= InterlockedIncrement(&This
->ref
);
490 TRACE("(%p) Refcount now %u\n", This
, ref
);
494 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
496 ACImpl
*This
= impl_from_IAudioClient(iface
);
498 ref
= InterlockedDecrement(&This
->ref
);
499 TRACE("(%p) Refcount now %u\n", This
, ref
);
501 IAudioClient_Stop(iface
);
502 IMMDevice_Release(This
->parent
);
503 DeleteCriticalSection(&This
->lock
);
504 snd_pcm_drop(This
->pcm_handle
);
505 snd_pcm_close(This
->pcm_handle
);
507 EnterCriticalSection(&g_sessions_lock
);
508 list_remove(&This
->entry
);
509 LeaveCriticalSection(&g_sessions_lock
);
511 HeapFree(GetProcessHeap(), 0, This
->vols
);
512 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
513 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
514 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
515 CoTaskMemFree(This
->fmt
);
516 HeapFree(GetProcessHeap(), 0, This
);
521 static void dump_fmt(const WAVEFORMATEX
*fmt
)
523 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
524 switch(fmt
->wFormatTag
){
525 case WAVE_FORMAT_PCM
:
526 TRACE("WAVE_FORMAT_PCM");
528 case WAVE_FORMAT_IEEE_FLOAT
:
529 TRACE("WAVE_FORMAT_IEEE_FLOAT");
531 case WAVE_FORMAT_EXTENSIBLE
:
532 TRACE("WAVE_FORMAT_EXTENSIBLE");
540 TRACE("nChannels: %u\n", fmt
->nChannels
);
541 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
542 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
543 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
544 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
545 TRACE("cbSize: %u\n", fmt
->cbSize
);
547 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
548 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
549 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
550 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
551 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
555 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
560 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
561 size
= sizeof(WAVEFORMATEXTENSIBLE
);
563 size
= sizeof(WAVEFORMATEX
);
565 ret
= CoTaskMemAlloc(size
);
569 memcpy(ret
, fmt
, size
);
571 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
576 static void session_init_vols(AudioSession
*session
, UINT channels
)
578 if(session
->channel_count
< channels
){
581 if(session
->channel_vols
)
582 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
583 session
->channel_vols
, sizeof(float) * channels
);
585 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
586 sizeof(float) * channels
);
587 if(!session
->channel_vols
)
590 for(i
= session
->channel_count
; i
< channels
; ++i
)
591 session
->channel_vols
[i
] = 1.f
;
593 session
->channel_count
= channels
;
597 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
602 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
606 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
608 ret
->device
= device
;
610 list_init(&ret
->clients
);
612 list_add_head(&g_sessions
, &ret
->entry
);
614 InitializeCriticalSection(&ret
->lock
);
616 session_init_vols(ret
, num_channels
);
618 ret
->master_vol
= 1.f
;
623 /* if channels == 0, then this will return or create a session with
624 * matching dataflow and GUID. otherwise, channels must also match */
625 static HRESULT
get_audio_session(const GUID
*sessionguid
,
626 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
628 AudioSession
*session
;
630 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
631 *out
= create_session(&GUID_NULL
, device
, channels
);
633 return E_OUTOFMEMORY
;
639 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
640 if(session
->device
== device
&&
641 IsEqualGUID(sessionguid
, &session
->guid
)){
642 session_init_vols(session
, channels
);
649 *out
= create_session(sessionguid
, device
, channels
);
651 return E_OUTOFMEMORY
;
657 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
658 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
659 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
660 const GUID
*sessionguid
)
662 ACImpl
*This
= impl_from_IAudioClient(iface
);
663 snd_pcm_sw_params_t
*sw_params
= NULL
;
664 snd_pcm_format_t format
;
665 snd_pcm_uframes_t boundary
;
666 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
667 unsigned int time_us
, rate
;
671 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
672 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
677 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
678 return AUDCLNT_E_NOT_INITIALIZED
;
680 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
681 AUDCLNT_STREAMFLAGS_LOOPBACK
|
682 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
683 AUDCLNT_STREAMFLAGS_NOPERSIST
|
684 AUDCLNT_STREAMFLAGS_RATEADJUST
|
685 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
686 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
687 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
688 TRACE("Unknown flags: %08x\n", flags
);
692 EnterCriticalSection(&This
->lock
);
695 LeaveCriticalSection(&This
->lock
);
696 return AUDCLNT_E_ALREADY_INITIALIZED
;
701 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
702 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
707 if((err
= snd_pcm_hw_params_set_access(This
->pcm_handle
, This
->hw_params
,
708 SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0){
709 WARN("Unable to set access: %d (%s)\n", err
, snd_strerror(err
));
714 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
715 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
716 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
717 if(fmt
->wBitsPerSample
== 8)
718 format
= SND_PCM_FORMAT_U8
;
719 else if(fmt
->wBitsPerSample
== 16)
720 format
= SND_PCM_FORMAT_S16_LE
;
721 else if(fmt
->wBitsPerSample
== 24)
722 format
= SND_PCM_FORMAT_S24_3LE
;
723 else if(fmt
->wBitsPerSample
== 32)
724 format
= SND_PCM_FORMAT_S32_LE
;
726 WARN("Unsupported bit depth: %u\n", fmt
->wBitsPerSample
);
727 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
730 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
731 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
732 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
733 if(fmt
->wBitsPerSample
== 32)
734 format
= SND_PCM_FORMAT_FLOAT_LE
;
735 else if(fmt
->wBitsPerSample
== 64)
736 format
= SND_PCM_FORMAT_FLOAT64_LE
;
738 WARN("Unsupported float size: %u\n", fmt
->wBitsPerSample
);
739 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
743 WARN("Unknown wave format: %04x\n", fmt
->wFormatTag
);
744 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
748 if((err
= snd_pcm_hw_params_set_format(This
->pcm_handle
, This
->hw_params
,
750 WARN("Unable to set ALSA format to %u: %d (%s)\n", format
, err
,
756 This
->alsa_format
= format
;
758 rate
= fmt
->nSamplesPerSec
;
759 if((err
= snd_pcm_hw_params_set_rate_near(This
->pcm_handle
, This
->hw_params
,
761 WARN("Unable to set rate to %u: %d (%s)\n", rate
, err
,
767 if((err
= snd_pcm_hw_params_set_channels(This
->pcm_handle
, This
->hw_params
,
768 fmt
->nChannels
)) < 0){
769 WARN("Unable to set channels to %u: %d (%s)\n", fmt
->nChannels
, err
,
775 time_us
= MinimumPeriod
/ 10;
776 if((err
= snd_pcm_hw_params_set_period_time_near(This
->pcm_handle
,
777 This
->hw_params
, &time_us
, NULL
)) < 0){
778 WARN("Unable to set max period time to %u: %d (%s)\n", time_us
,
779 err
, snd_strerror(err
));
784 if((err
= snd_pcm_hw_params(This
->pcm_handle
, This
->hw_params
)) < 0){
785 WARN("Unable to set hw params: %d (%s)\n", err
, snd_strerror(err
));
790 sw_params
= HeapAlloc(GetProcessHeap(), 0, snd_pcm_sw_params_sizeof());
796 if((err
= snd_pcm_sw_params_current(This
->pcm_handle
, sw_params
)) < 0){
797 WARN("Unable to get sw_params: %d (%s)\n", err
, snd_strerror(err
));
803 duration
= 300000; /* 0.03s */
804 This
->bufsize_frames
= ceil((duration
/ 10000000.) * fmt
->nSamplesPerSec
);
805 This
->local_buffer
= HeapAlloc(GetProcessHeap(), 0,
806 This
->bufsize_frames
* fmt
->nBlockAlign
);
807 if(!This
->local_buffer
){
811 if (fmt
->wBitsPerSample
== 8)
812 memset(This
->local_buffer
, 128, This
->bufsize_frames
* fmt
->nBlockAlign
);
814 memset(This
->local_buffer
, 0, This
->bufsize_frames
* fmt
->nBlockAlign
);
816 if((err
= snd_pcm_sw_params_get_boundary(sw_params
, &boundary
)) < 0){
817 WARN("Unable to get boundary: %d (%s)\n", err
, snd_strerror(err
));
822 if((err
= snd_pcm_sw_params_set_start_threshold(This
->pcm_handle
,
823 sw_params
, boundary
)) < 0){
824 WARN("Unable to set start threshold to %lx: %d (%s)\n", boundary
, err
,
830 if((err
= snd_pcm_sw_params_set_stop_threshold(This
->pcm_handle
,
831 sw_params
, boundary
)) < 0){
832 WARN("Unable to set stop threshold to %lx: %d (%s)\n", boundary
, err
,
838 if((err
= snd_pcm_sw_params_set_avail_min(This
->pcm_handle
,
840 WARN("Unable to set avail min to 0: %d (%s)\n", err
, snd_strerror(err
));
845 if((err
= snd_pcm_sw_params_set_silence_size(This
->pcm_handle
,
846 sw_params
, boundary
)) < 0){
847 WARN("Unable to set silence size to %lx: %d (%s)\n", boundary
, err
,
853 if((err
= snd_pcm_sw_params(This
->pcm_handle
, sw_params
)) < 0){
854 WARN("Unable to set sw params: %d (%s)\n", err
, snd_strerror(err
));
859 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0){
860 WARN("Unable to prepare device: %d (%s)\n", err
, snd_strerror(err
));
865 if((err
= snd_pcm_hw_params_get_buffer_size(This
->hw_params
,
866 &This
->bufsize_alsa
)) < 0){
867 WARN("Unable to get buffer size: %d (%s)\n", err
, snd_strerror(err
));
872 if((err
= snd_pcm_hw_params_get_period_size(This
->hw_params
,
873 &This
->period_alsa
, NULL
)) < 0){
874 WARN("Unable to get period size: %d (%s)\n", err
, snd_strerror(err
));
879 if((err
= snd_pcm_hw_params_get_period_time(This
->hw_params
,
880 &This
->period_us
, NULL
)) < 0){
881 WARN("Unable to get period time: %d (%s)\n", err
, snd_strerror(err
));
886 This
->fmt
= clone_format(fmt
);
892 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
898 for(i
= 0; i
< fmt
->nChannels
; ++i
)
904 EnterCriticalSection(&g_sessions_lock
);
906 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
909 LeaveCriticalSection(&g_sessions_lock
);
913 list_add_tail(&This
->session
->clients
, &This
->entry
);
915 LeaveCriticalSection(&g_sessions_lock
);
917 This
->initted
= TRUE
;
920 HeapFree(GetProcessHeap(), 0, sw_params
);
922 if(This
->local_buffer
){
923 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
924 This
->local_buffer
= NULL
;
927 CoTaskMemFree(This
->fmt
);
931 HeapFree(GetProcessHeap(), 0, This
->vols
);
936 LeaveCriticalSection(&This
->lock
);
941 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
944 ACImpl
*This
= impl_from_IAudioClient(iface
);
946 TRACE("(%p)->(%p)\n", This
, out
);
951 EnterCriticalSection(&This
->lock
);
954 LeaveCriticalSection(&This
->lock
);
955 return AUDCLNT_E_NOT_INITIALIZED
;
958 *out
= This
->bufsize_frames
;
960 LeaveCriticalSection(&This
->lock
);
965 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
966 REFERENCE_TIME
*latency
)
968 ACImpl
*This
= impl_from_IAudioClient(iface
);
970 TRACE("(%p)->(%p)\n", This
, latency
);
975 EnterCriticalSection(&This
->lock
);
978 LeaveCriticalSection(&This
->lock
);
979 return AUDCLNT_E_NOT_INITIALIZED
;
982 LeaveCriticalSection(&This
->lock
);
989 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
992 ACImpl
*This
= impl_from_IAudioClient(iface
);
994 TRACE("(%p)->(%p)\n", This
, out
);
999 EnterCriticalSection(&This
->lock
);
1002 LeaveCriticalSection(&This
->lock
);
1003 return AUDCLNT_E_NOT_INITIALIZED
;
1006 if(This
->dataflow
== eRender
){
1007 snd_pcm_sframes_t avail_frames
;
1009 avail_frames
= snd_pcm_avail_update(This
->pcm_handle
);
1011 if(This
->bufsize_alsa
< avail_frames
){
1012 WARN("Xrun detected\n");
1013 *out
= This
->held_frames
;
1015 *out
= This
->bufsize_alsa
- avail_frames
+ This
->held_frames
;
1016 }else if(This
->dataflow
== eCapture
){
1017 *out
= This
->held_frames
;
1019 LeaveCriticalSection(&This
->lock
);
1020 return E_UNEXPECTED
;
1023 LeaveCriticalSection(&This
->lock
);
1028 static DWORD
get_channel_mask(unsigned int channels
)
1034 return SPEAKER_FRONT_CENTER
;
1036 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
;
1038 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
|
1039 SPEAKER_LOW_FREQUENCY
;
1041 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
1044 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
1045 SPEAKER_BACK_RIGHT
| SPEAKER_LOW_FREQUENCY
;
1047 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
1048 SPEAKER_BACK_RIGHT
| SPEAKER_LOW_FREQUENCY
| SPEAKER_FRONT_CENTER
;
1050 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
1051 SPEAKER_BACK_RIGHT
| SPEAKER_LOW_FREQUENCY
| SPEAKER_FRONT_CENTER
|
1052 SPEAKER_BACK_CENTER
;
1054 FIXME("Unknown speaker configuration: %u\n", channels
);
1058 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1059 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*fmt
,
1062 ACImpl
*This
= impl_from_IAudioClient(iface
);
1063 snd_pcm_format_mask_t
*formats
= NULL
;
1065 WAVEFORMATEX
*closest
= NULL
;
1066 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
1067 unsigned int max
= 0, min
= 0;
1070 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, fmt
, out
);
1072 if(!fmt
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !out
))
1075 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1076 return E_INVALIDARG
;
1078 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1079 fmt
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1080 return E_INVALIDARG
;
1084 EnterCriticalSection(&This
->lock
);
1086 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1091 formats
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1092 snd_pcm_format_mask_sizeof());
1098 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1100 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1101 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1102 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
1103 switch(fmt
->wBitsPerSample
){
1105 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1106 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1111 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1112 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1117 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1118 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1123 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1124 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1129 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1132 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
1133 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1134 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
1135 switch(fmt
->wBitsPerSample
){
1137 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1138 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1143 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT64_LE
)){
1144 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1149 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1153 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1157 closest
= clone_format(fmt
);
1163 if((err
= snd_pcm_hw_params_get_rate_min(This
->hw_params
, &min
, NULL
)) < 0){
1165 WARN("Unable to get min rate: %d (%s)\n", err
, snd_strerror(err
));
1169 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max
, NULL
)) < 0){
1171 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1175 if(fmt
->nSamplesPerSec
< min
|| fmt
->nSamplesPerSec
> max
){
1176 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1180 if((err
= snd_pcm_hw_params_get_channels_min(This
->hw_params
, &min
)) < 0){
1182 WARN("Unable to get min channels: %d (%s)\n", err
, snd_strerror(err
));
1186 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
, &max
)) < 0){
1188 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1193 if(fmt
->nChannels
> max
){
1195 closest
->nChannels
= max
;
1196 }else if(fmt
->nChannels
< min
){
1198 closest
->nChannels
= min
;
1201 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1202 DWORD mask
= get_channel_mask(closest
->nChannels
);
1204 ((WAVEFORMATEXTENSIBLE
*)closest
)->dwChannelMask
= mask
;
1206 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1207 fmtex
->dwChannelMask
!= mask
)
1212 LeaveCriticalSection(&This
->lock
);
1213 HeapFree(GetProcessHeap(), 0, formats
);
1215 if(hr
== S_OK
|| !out
){
1216 CoTaskMemFree(closest
);
1220 closest
->nBlockAlign
=
1221 closest
->nChannels
* closest
->wBitsPerSample
/ 8;
1222 closest
->nAvgBytesPerSec
=
1223 closest
->nBlockAlign
* closest
->nSamplesPerSec
;
1227 TRACE("returning: %08x\n", hr
);
1231 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1232 WAVEFORMATEX
**pwfx
)
1234 ACImpl
*This
= impl_from_IAudioClient(iface
);
1235 WAVEFORMATEXTENSIBLE
*fmt
;
1236 snd_pcm_format_mask_t
*formats
;
1237 unsigned int max_rate
, max_channels
;
1241 TRACE("(%p)->(%p)\n", This
, pwfx
);
1247 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1249 return E_OUTOFMEMORY
;
1251 formats
= HeapAlloc(GetProcessHeap(), 0, snd_pcm_format_mask_sizeof());
1254 return E_OUTOFMEMORY
;
1257 EnterCriticalSection(&This
->lock
);
1259 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1260 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
1265 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1267 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1268 if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1269 fmt
->Format
.wBitsPerSample
= 32;
1270 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1271 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1272 fmt
->Format
.wBitsPerSample
= 16;
1273 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1274 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1275 fmt
->Format
.wBitsPerSample
= 8;
1276 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1277 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1278 fmt
->Format
.wBitsPerSample
= 32;
1279 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1280 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1281 fmt
->Format
.wBitsPerSample
= 24;
1282 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1284 ERR("Didn't recognize any available ALSA formats\n");
1289 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
,
1290 &max_channels
)) < 0){
1291 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1296 if(max_channels
> 2){
1297 FIXME("Don't know what to do with %u channels, pretending there's "
1298 "only 2 channels\n", max_channels
);
1299 fmt
->Format
.nChannels
= 2;
1301 fmt
->Format
.nChannels
= max_channels
;
1303 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1305 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max_rate
,
1307 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1312 if(max_rate
>= 48000)
1313 fmt
->Format
.nSamplesPerSec
= 48000;
1314 else if(max_rate
>= 44100)
1315 fmt
->Format
.nSamplesPerSec
= 44100;
1316 else if(max_rate
>= 22050)
1317 fmt
->Format
.nSamplesPerSec
= 22050;
1318 else if(max_rate
>= 11025)
1319 fmt
->Format
.nSamplesPerSec
= 11025;
1320 else if(max_rate
>= 8000)
1321 fmt
->Format
.nSamplesPerSec
= 8000;
1323 ERR("Unknown max rate: %u\n", max_rate
);
1328 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1329 fmt
->Format
.nChannels
) / 8;
1330 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1331 fmt
->Format
.nBlockAlign
;
1333 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1334 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1336 dump_fmt((WAVEFORMATEX
*)fmt
);
1337 *pwfx
= (WAVEFORMATEX
*)fmt
;
1340 LeaveCriticalSection(&This
->lock
);
1343 HeapFree(GetProcessHeap(), 0, formats
);
1348 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1349 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1351 ACImpl
*This
= impl_from_IAudioClient(iface
);
1353 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1355 if(!defperiod
&& !minperiod
)
1359 *defperiod
= DefaultPeriod
;
1361 *minperiod
= MinimumPeriod
;
1366 static snd_pcm_sframes_t
alsa_write_best_effort(snd_pcm_t
*handle
, BYTE
*buf
,
1367 snd_pcm_uframes_t frames
, ACImpl
*This
)
1369 snd_pcm_sframes_t written
;
1371 if(This
->session
->mute
){
1373 if((err
= snd_pcm_format_set_silence(This
->alsa_format
, buf
,
1374 frames
* This
->fmt
->nChannels
)) < 0)
1375 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1379 written
= snd_pcm_writei(handle
, buf
, frames
);
1383 if(written
== -EAGAIN
)
1387 WARN("writei failed, recovering: %ld (%s)\n", written
,
1388 snd_strerror(written
));
1390 ret
= wine_snd_pcm_recover(handle
, written
, 0);
1392 WARN("Could not recover: %d (%s)\n", ret
, snd_strerror(ret
));
1396 written
= snd_pcm_writei(handle
, buf
, frames
);
1402 static void alsa_write_data(ACImpl
*This
)
1404 snd_pcm_sframes_t written
;
1405 snd_pcm_uframes_t to_write
;
1407 This
->local_buffer
+ (This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
);
1409 if(This
->lcl_offs_frames
+ This
->held_frames
> This
->bufsize_frames
)
1410 to_write
= This
->bufsize_frames
- This
->lcl_offs_frames
;
1412 to_write
= This
->held_frames
;
1414 written
= alsa_write_best_effort(This
->pcm_handle
, buf
, to_write
, This
);
1416 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
1420 This
->lcl_offs_frames
+= written
;
1421 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1422 This
->held_frames
-= written
;
1424 if(written
< to_write
){
1425 /* ALSA buffer probably full */
1429 if(This
->held_frames
){
1430 /* wrapped and have some data back at the start to write */
1431 written
= alsa_write_best_effort(This
->pcm_handle
, This
->local_buffer
,
1432 This
->held_frames
, This
);
1434 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
1438 This
->lcl_offs_frames
+= written
;
1439 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1440 This
->held_frames
-= written
;
1444 static void alsa_read_data(ACImpl
*This
)
1446 snd_pcm_sframes_t pos
, readable
, nread
;
1448 pos
= (This
->held_frames
+ This
->lcl_offs_frames
) % This
->bufsize_frames
;
1449 readable
= This
->bufsize_frames
- pos
;
1451 nread
= snd_pcm_readi(This
->pcm_handle
,
1452 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, readable
);
1456 WARN("read failed, recovering: %ld (%s)\n", nread
, snd_strerror(nread
));
1458 ret
= wine_snd_pcm_recover(This
->pcm_handle
, nread
, 0);
1460 WARN("Recover failed: %d (%s)\n", ret
, snd_strerror(ret
));
1464 nread
= snd_pcm_readi(This
->pcm_handle
,
1465 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, readable
);
1467 WARN("read failed: %ld (%s)\n", nread
, snd_strerror(nread
));
1472 if(This
->session
->mute
){
1474 if((err
= snd_pcm_format_set_silence(This
->alsa_format
,
1475 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
,
1477 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1481 This
->held_frames
+= nread
;
1483 if(This
->held_frames
> This
->bufsize_frames
){
1484 WARN("Overflow of unread data\n");
1485 This
->lcl_offs_frames
+= This
->held_frames
;
1486 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1487 This
->held_frames
= This
->bufsize_frames
;
1491 static void CALLBACK
alsa_push_buffer_data(void *user
, BOOLEAN timer
)
1493 ACImpl
*This
= user
;
1495 EnterCriticalSection(&This
->lock
);
1498 if(This
->dataflow
== eRender
&& This
->held_frames
)
1499 alsa_write_data(This
);
1500 else if(This
->dataflow
== eCapture
)
1501 alsa_read_data(This
);
1504 SetEvent(This
->event
);
1507 LeaveCriticalSection(&This
->lock
);
1510 static HRESULT
alsa_consider_start(ACImpl
*This
)
1512 snd_pcm_sframes_t avail
;
1515 avail
= snd_pcm_avail_update(This
->pcm_handle
);
1517 WARN("Unable to get avail_update: %ld (%s)\n", avail
,
1518 snd_strerror(avail
));
1522 if(This
->period_alsa
< This
->bufsize_alsa
- avail
){
1523 if((err
= snd_pcm_start(This
->pcm_handle
)) < 0){
1524 WARN("Start failed: %d (%s), state: %d\n", err
, snd_strerror(err
),
1525 snd_pcm_state(This
->pcm_handle
));
1535 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1537 ACImpl
*This
= impl_from_IAudioClient(iface
);
1541 TRACE("(%p)\n", This
);
1543 EnterCriticalSection(&This
->lock
);
1546 LeaveCriticalSection(&This
->lock
);
1547 return AUDCLNT_E_NOT_INITIALIZED
;
1550 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1551 LeaveCriticalSection(&This
->lock
);
1552 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1556 LeaveCriticalSection(&This
->lock
);
1557 return AUDCLNT_E_NOT_STOPPED
;
1560 hr
= alsa_consider_start(This
);
1562 LeaveCriticalSection(&This
->lock
);
1566 period_ms
= This
->period_us
/ 1000;
1570 if(This
->dataflow
== eCapture
){
1571 /* dump any data that might be leftover in the ALSA capture buffer */
1572 snd_pcm_readi(This
->pcm_handle
, This
->local_buffer
,
1573 This
->bufsize_frames
);
1576 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
, alsa_push_buffer_data
,
1577 This
, 0, period_ms
, WT_EXECUTEINTIMERTHREAD
)){
1578 LeaveCriticalSection(&This
->lock
);
1579 WARN("Unable to create timer: %u\n", GetLastError());
1583 This
->started
= TRUE
;
1585 LeaveCriticalSection(&This
->lock
);
1590 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1592 ACImpl
*This
= impl_from_IAudioClient(iface
);
1597 TRACE("(%p)\n", This
);
1599 EnterCriticalSection(&This
->lock
);
1602 LeaveCriticalSection(&This
->lock
);
1603 return AUDCLNT_E_NOT_INITIALIZED
;
1607 LeaveCriticalSection(&This
->lock
);
1611 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1612 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
1614 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1615 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
1617 if((err
= snd_pcm_drop(This
->pcm_handle
)) < 0){
1618 LeaveCriticalSection(&This
->lock
);
1619 WARN("Drop failed: %d (%s)\n", err
, snd_strerror(err
));
1623 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0){
1624 LeaveCriticalSection(&This
->lock
);
1625 WARN("Prepare failed: %d (%s)\n", err
, snd_strerror(err
));
1629 This
->started
= FALSE
;
1631 LeaveCriticalSection(&This
->lock
);
1634 WaitForSingleObject(event
, INFINITE
);
1640 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1642 ACImpl
*This
= impl_from_IAudioClient(iface
);
1644 TRACE("(%p)\n", This
);
1646 EnterCriticalSection(&This
->lock
);
1649 LeaveCriticalSection(&This
->lock
);
1650 return AUDCLNT_E_NOT_INITIALIZED
;
1654 LeaveCriticalSection(&This
->lock
);
1655 return AUDCLNT_E_NOT_STOPPED
;
1658 This
->held_frames
= 0;
1659 This
->written_frames
= 0;
1660 This
->lcl_offs_frames
= 0;
1662 LeaveCriticalSection(&This
->lock
);
1667 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1670 ACImpl
*This
= impl_from_IAudioClient(iface
);
1672 TRACE("(%p)->(%p)\n", This
, event
);
1675 return E_INVALIDARG
;
1677 EnterCriticalSection(&This
->lock
);
1680 LeaveCriticalSection(&This
->lock
);
1681 return AUDCLNT_E_NOT_INITIALIZED
;
1684 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1685 LeaveCriticalSection(&This
->lock
);
1686 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1689 This
->event
= event
;
1691 LeaveCriticalSection(&This
->lock
);
1696 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1699 ACImpl
*This
= impl_from_IAudioClient(iface
);
1701 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1707 EnterCriticalSection(&This
->lock
);
1710 LeaveCriticalSection(&This
->lock
);
1711 return AUDCLNT_E_NOT_INITIALIZED
;
1714 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1715 if(This
->dataflow
!= eRender
){
1716 LeaveCriticalSection(&This
->lock
);
1717 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1719 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
1720 *ppv
= &This
->IAudioRenderClient_iface
;
1721 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1722 if(This
->dataflow
!= eCapture
){
1723 LeaveCriticalSection(&This
->lock
);
1724 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1726 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
1727 *ppv
= &This
->IAudioCaptureClient_iface
;
1728 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1729 IAudioClock_AddRef(&This
->IAudioClock_iface
);
1730 *ppv
= &This
->IAudioClock_iface
;
1731 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1732 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
1733 *ppv
= &This
->IAudioStreamVolume_iface
;
1734 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1735 if(!This
->session_wrapper
){
1736 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1737 if(!This
->session_wrapper
){
1738 LeaveCriticalSection(&This
->lock
);
1739 return E_OUTOFMEMORY
;
1742 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
1744 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1745 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1746 if(!This
->session_wrapper
){
1747 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1748 if(!This
->session_wrapper
){
1749 LeaveCriticalSection(&This
->lock
);
1750 return E_OUTOFMEMORY
;
1753 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
1755 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1756 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1757 if(!This
->session_wrapper
){
1758 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1759 if(!This
->session_wrapper
){
1760 LeaveCriticalSection(&This
->lock
);
1761 return E_OUTOFMEMORY
;
1764 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
1766 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1770 LeaveCriticalSection(&This
->lock
);
1774 LeaveCriticalSection(&This
->lock
);
1776 FIXME("stub %s\n", debugstr_guid(riid
));
1777 return E_NOINTERFACE
;
1780 static const IAudioClientVtbl AudioClient_Vtbl
=
1782 AudioClient_QueryInterface
,
1784 AudioClient_Release
,
1785 AudioClient_Initialize
,
1786 AudioClient_GetBufferSize
,
1787 AudioClient_GetStreamLatency
,
1788 AudioClient_GetCurrentPadding
,
1789 AudioClient_IsFormatSupported
,
1790 AudioClient_GetMixFormat
,
1791 AudioClient_GetDevicePeriod
,
1795 AudioClient_SetEventHandle
,
1796 AudioClient_GetService
1799 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1800 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1802 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1808 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1809 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1812 IUnknown_AddRef((IUnknown
*)*ppv
);
1816 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1817 return E_NOINTERFACE
;
1820 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1822 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1823 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1826 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1828 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1829 return AudioClient_Release(&This
->IAudioClient_iface
);
1832 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1833 UINT32 frames
, BYTE
**data
)
1835 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1840 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1845 EnterCriticalSection(&This
->lock
);
1847 if(This
->buf_state
!= NOT_LOCKED
){
1848 LeaveCriticalSection(&This
->lock
);
1849 return AUDCLNT_E_OUT_OF_ORDER
;
1853 This
->buf_state
= LOCKED_NORMAL
;
1854 LeaveCriticalSection(&This
->lock
);
1858 hr
= IAudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, &pad
);
1860 LeaveCriticalSection(&This
->lock
);
1864 if(pad
+ frames
> This
->bufsize_frames
){
1865 LeaveCriticalSection(&This
->lock
);
1866 return AUDCLNT_E_BUFFER_TOO_LARGE
;
1870 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
1871 if(write_pos
+ frames
> This
->bufsize_frames
){
1872 if(This
->tmp_buffer_frames
< frames
){
1873 if(This
->tmp_buffer
)
1874 This
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0,
1875 This
->tmp_buffer
, frames
* This
->fmt
->nBlockAlign
);
1877 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
1878 frames
* This
->fmt
->nBlockAlign
);
1879 if(!This
->tmp_buffer
){
1880 LeaveCriticalSection(&This
->lock
);
1881 return E_OUTOFMEMORY
;
1883 This
->tmp_buffer_frames
= frames
;
1885 *data
= This
->tmp_buffer
;
1886 This
->buf_state
= LOCKED_WRAPPED
;
1888 *data
= This
->local_buffer
+ write_pos
* This
->fmt
->nBlockAlign
;
1889 This
->buf_state
= LOCKED_NORMAL
;
1892 LeaveCriticalSection(&This
->lock
);
1897 static void alsa_wrap_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 written_bytes
)
1899 snd_pcm_uframes_t write_offs_frames
=
1900 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
1901 UINT32 write_offs_bytes
= write_offs_frames
* This
->fmt
->nBlockAlign
;
1902 snd_pcm_uframes_t chunk_frames
= This
->bufsize_frames
- write_offs_frames
;
1903 UINT32 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
1905 if(written_bytes
< chunk_bytes
){
1906 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, written_bytes
);
1908 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, chunk_bytes
);
1909 memcpy(This
->local_buffer
, buffer
+ chunk_bytes
,
1910 written_bytes
- chunk_bytes
);
1914 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
1915 IAudioRenderClient
*iface
, UINT32 written_frames
, DWORD flags
)
1917 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1918 UINT32 written_bytes
= written_frames
* This
->fmt
->nBlockAlign
;
1922 TRACE("(%p)->(%u, %x)\n", This
, written_frames
, flags
);
1924 EnterCriticalSection(&This
->lock
);
1926 if(This
->buf_state
== NOT_LOCKED
|| !written_frames
){
1927 This
->buf_state
= NOT_LOCKED
;
1928 LeaveCriticalSection(&This
->lock
);
1929 return written_frames
? AUDCLNT_E_OUT_OF_ORDER
: S_OK
;
1932 if(This
->buf_state
== LOCKED_NORMAL
)
1933 buffer
= This
->local_buffer
+
1934 (This
->lcl_offs_frames
+ This
->held_frames
) * This
->fmt
->nBlockAlign
;
1936 buffer
= This
->tmp_buffer
;
1938 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
){
1939 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
1940 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1941 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1942 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
1943 This
->fmt
->wBitsPerSample
== 8)
1944 memset(buffer
, 128, written_frames
* This
->fmt
->nBlockAlign
);
1946 memset(buffer
, 0, written_frames
* This
->fmt
->nBlockAlign
);
1949 if(This
->held_frames
){
1950 if(This
->buf_state
== LOCKED_WRAPPED
)
1951 alsa_wrap_buffer(This
, buffer
, written_bytes
);
1953 This
->held_frames
+= written_frames
;
1955 snd_pcm_sframes_t written
;
1957 written
= alsa_write_best_effort(This
->pcm_handle
, buffer
,
1958 written_frames
, This
);
1960 LeaveCriticalSection(&This
->lock
);
1961 WARN("write failed: %ld (%s)\n", written
, snd_strerror(written
));
1965 if(written
< written_frames
){
1966 if(This
->buf_state
== LOCKED_WRAPPED
)
1967 alsa_wrap_buffer(This
,
1968 This
->tmp_buffer
+ written
* This
->fmt
->nBlockAlign
,
1969 written_frames
- written
);
1971 This
->held_frames
= written_frames
- written
;
1976 snd_pcm_state(This
->pcm_handle
) == SND_PCM_STATE_PREPARED
){
1977 hr
= alsa_consider_start(This
);
1979 LeaveCriticalSection(&This
->lock
);
1984 This
->written_frames
+= written_frames
;
1985 This
->buf_state
= NOT_LOCKED
;
1987 LeaveCriticalSection(&This
->lock
);
1992 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
1993 AudioRenderClient_QueryInterface
,
1994 AudioRenderClient_AddRef
,
1995 AudioRenderClient_Release
,
1996 AudioRenderClient_GetBuffer
,
1997 AudioRenderClient_ReleaseBuffer
2000 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
2001 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
2003 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2009 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2010 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
2013 IUnknown_AddRef((IUnknown
*)*ppv
);
2017 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2018 return E_NOINTERFACE
;
2021 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
2023 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2024 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2027 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
2029 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2030 return IAudioClient_Release(&This
->IAudioClient_iface
);
2033 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
2034 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
2037 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2040 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
2043 if(!data
|| !frames
|| !flags
)
2046 EnterCriticalSection(&This
->lock
);
2048 if(This
->buf_state
!= NOT_LOCKED
){
2049 LeaveCriticalSection(&This
->lock
);
2050 return AUDCLNT_E_OUT_OF_ORDER
;
2053 hr
= IAudioCaptureClient_GetNextPacketSize(iface
, frames
);
2055 LeaveCriticalSection(&This
->lock
);
2061 if(This
->lcl_offs_frames
+ *frames
> This
->bufsize_frames
){
2062 UINT32 chunk_bytes
, offs_bytes
, frames_bytes
;
2063 if(This
->tmp_buffer_frames
< *frames
){
2064 if(This
->tmp_buffer
)
2065 This
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0,
2066 This
->tmp_buffer
, *frames
* This
->fmt
->nBlockAlign
);
2068 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2069 *frames
* This
->fmt
->nBlockAlign
);
2070 if(!This
->tmp_buffer
){
2071 LeaveCriticalSection(&This
->lock
);
2072 return E_OUTOFMEMORY
;
2074 This
->tmp_buffer_frames
= *frames
;
2077 *data
= This
->tmp_buffer
;
2078 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) *
2079 This
->fmt
->nBlockAlign
;
2080 offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2081 frames_bytes
= *frames
* This
->fmt
->nBlockAlign
;
2082 memcpy(This
->tmp_buffer
, This
->local_buffer
+ offs_bytes
, chunk_bytes
);
2083 memcpy(This
->tmp_buffer
+ chunk_bytes
, This
->local_buffer
,
2084 frames_bytes
- chunk_bytes
);
2086 *data
= This
->local_buffer
+
2087 This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2089 This
->buf_state
= LOCKED_NORMAL
;
2091 if(devpos
|| qpcpos
)
2092 IAudioClock_GetPosition(&This
->IAudioClock_iface
, devpos
, qpcpos
);
2094 LeaveCriticalSection(&This
->lock
);
2096 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
2099 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2100 IAudioCaptureClient
*iface
, UINT32 done
)
2102 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2104 TRACE("(%p)->(%u)\n", This
, done
);
2106 EnterCriticalSection(&This
->lock
);
2108 if(This
->buf_state
== NOT_LOCKED
){
2109 LeaveCriticalSection(&This
->lock
);
2110 return AUDCLNT_E_OUT_OF_ORDER
;
2113 This
->held_frames
-= done
;
2114 This
->lcl_offs_frames
+= done
;
2115 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2117 This
->buf_state
= NOT_LOCKED
;
2119 LeaveCriticalSection(&This
->lock
);
2124 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2125 IAudioCaptureClient
*iface
, UINT32
*frames
)
2127 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2129 TRACE("(%p)->(%p)\n", This
, frames
);
2131 return AudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, frames
);
2134 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2136 AudioCaptureClient_QueryInterface
,
2137 AudioCaptureClient_AddRef
,
2138 AudioCaptureClient_Release
,
2139 AudioCaptureClient_GetBuffer
,
2140 AudioCaptureClient_ReleaseBuffer
,
2141 AudioCaptureClient_GetNextPacketSize
2144 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2145 REFIID riid
, void **ppv
)
2147 ACImpl
*This
= impl_from_IAudioClock(iface
);
2149 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2155 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2157 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2158 *ppv
= &This
->IAudioClock2_iface
;
2160 IUnknown_AddRef((IUnknown
*)*ppv
);
2164 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2165 return E_NOINTERFACE
;
2168 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2170 ACImpl
*This
= impl_from_IAudioClock(iface
);
2171 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2174 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2176 ACImpl
*This
= impl_from_IAudioClock(iface
);
2177 return IAudioClient_Release(&This
->IAudioClient_iface
);
2180 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2182 ACImpl
*This
= impl_from_IAudioClock(iface
);
2184 TRACE("(%p)->(%p)\n", This
, freq
);
2186 *freq
= This
->fmt
->nSamplesPerSec
;
2191 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2194 ACImpl
*This
= impl_from_IAudioClock(iface
);
2198 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2203 EnterCriticalSection(&This
->lock
);
2205 hr
= IAudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, &pad
);
2207 LeaveCriticalSection(&This
->lock
);
2211 if(This
->dataflow
== eRender
)
2212 *pos
= This
->written_frames
- pad
;
2213 else if(This
->dataflow
== eCapture
)
2214 *pos
= This
->written_frames
+ pad
;
2216 LeaveCriticalSection(&This
->lock
);
2219 LARGE_INTEGER stamp
, freq
;
2220 QueryPerformanceCounter(&stamp
);
2221 QueryPerformanceFrequency(&freq
);
2222 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2228 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2231 ACImpl
*This
= impl_from_IAudioClock(iface
);
2233 TRACE("(%p)->(%p)\n", This
, chars
);
2238 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2243 static const IAudioClockVtbl AudioClock_Vtbl
=
2245 AudioClock_QueryInterface
,
2248 AudioClock_GetFrequency
,
2249 AudioClock_GetPosition
,
2250 AudioClock_GetCharacteristics
2253 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2254 REFIID riid
, void **ppv
)
2256 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2257 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2260 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2262 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2263 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2266 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2268 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2269 return IAudioClient_Release(&This
->IAudioClient_iface
);
2272 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2273 UINT64
*pos
, UINT64
*qpctime
)
2275 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2277 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2282 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2284 AudioClock2_QueryInterface
,
2286 AudioClock2_Release
,
2287 AudioClock2_GetDevicePosition
2290 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2292 AudioSessionWrapper
*ret
;
2294 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2295 sizeof(AudioSessionWrapper
));
2299 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2300 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2301 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2305 ret
->client
= client
;
2307 ret
->session
= client
->session
;
2308 AudioClient_AddRef(&client
->IAudioClient_iface
);
2314 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2315 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2317 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2323 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2324 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2325 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2328 IUnknown_AddRef((IUnknown
*)*ppv
);
2332 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2333 return E_NOINTERFACE
;
2336 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2338 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2340 ref
= InterlockedIncrement(&This
->ref
);
2341 TRACE("(%p) Refcount now %u\n", This
, ref
);
2345 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2347 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2349 ref
= InterlockedDecrement(&This
->ref
);
2350 TRACE("(%p) Refcount now %u\n", This
, ref
);
2353 EnterCriticalSection(&This
->client
->lock
);
2354 This
->client
->session_wrapper
= NULL
;
2355 LeaveCriticalSection(&This
->client
->lock
);
2356 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2358 HeapFree(GetProcessHeap(), 0, This
);
2363 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2364 AudioSessionState
*state
)
2366 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2369 TRACE("(%p)->(%p)\n", This
, state
);
2372 return NULL_PTR_ERR
;
2374 EnterCriticalSection(&g_sessions_lock
);
2376 if(list_empty(&This
->session
->clients
)){
2377 *state
= AudioSessionStateExpired
;
2378 LeaveCriticalSection(&g_sessions_lock
);
2382 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2383 EnterCriticalSection(&client
->lock
);
2384 if(client
->started
){
2385 *state
= AudioSessionStateActive
;
2386 LeaveCriticalSection(&client
->lock
);
2387 LeaveCriticalSection(&g_sessions_lock
);
2390 LeaveCriticalSection(&client
->lock
);
2393 LeaveCriticalSection(&g_sessions_lock
);
2395 *state
= AudioSessionStateInactive
;
2400 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2401 IAudioSessionControl2
*iface
, WCHAR
**name
)
2403 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2405 FIXME("(%p)->(%p) - stub\n", This
, name
);
2410 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2411 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2413 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2415 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2420 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2421 IAudioSessionControl2
*iface
, WCHAR
**path
)
2423 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2425 FIXME("(%p)->(%p) - stub\n", This
, path
);
2430 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2431 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2433 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2435 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2440 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2441 IAudioSessionControl2
*iface
, GUID
*group
)
2443 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2445 FIXME("(%p)->(%p) - stub\n", This
, group
);
2450 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2451 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2453 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2455 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2456 debugstr_guid(session
));
2461 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2462 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2464 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2466 FIXME("(%p)->(%p) - stub\n", This
, events
);
2471 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2472 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2474 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2476 FIXME("(%p)->(%p) - stub\n", This
, events
);
2481 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2482 IAudioSessionControl2
*iface
, WCHAR
**id
)
2484 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2486 FIXME("(%p)->(%p) - stub\n", This
, id
);
2491 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2492 IAudioSessionControl2
*iface
, WCHAR
**id
)
2494 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2496 FIXME("(%p)->(%p) - stub\n", This
, id
);
2501 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2502 IAudioSessionControl2
*iface
, DWORD
*pid
)
2504 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2506 TRACE("(%p)->(%p)\n", This
, pid
);
2511 *pid
= GetCurrentProcessId();
2516 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2517 IAudioSessionControl2
*iface
)
2519 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2521 TRACE("(%p)\n", This
);
2526 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2527 IAudioSessionControl2
*iface
, BOOL optout
)
2529 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2531 TRACE("(%p)->(%d)\n", This
, optout
);
2536 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2538 AudioSessionControl_QueryInterface
,
2539 AudioSessionControl_AddRef
,
2540 AudioSessionControl_Release
,
2541 AudioSessionControl_GetState
,
2542 AudioSessionControl_GetDisplayName
,
2543 AudioSessionControl_SetDisplayName
,
2544 AudioSessionControl_GetIconPath
,
2545 AudioSessionControl_SetIconPath
,
2546 AudioSessionControl_GetGroupingParam
,
2547 AudioSessionControl_SetGroupingParam
,
2548 AudioSessionControl_RegisterAudioSessionNotification
,
2549 AudioSessionControl_UnregisterAudioSessionNotification
,
2550 AudioSessionControl_GetSessionIdentifier
,
2551 AudioSessionControl_GetSessionInstanceIdentifier
,
2552 AudioSessionControl_GetProcessId
,
2553 AudioSessionControl_IsSystemSoundsSession
,
2554 AudioSessionControl_SetDuckingPreference
2557 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2558 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2560 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2566 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2567 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2570 IUnknown_AddRef((IUnknown
*)*ppv
);
2574 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2575 return E_NOINTERFACE
;
2578 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2580 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2581 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2584 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2586 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2587 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2590 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2591 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2593 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2594 AudioSession
*session
= This
->session
;
2596 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2598 if(level
< 0.f
|| level
> 1.f
)
2599 return E_INVALIDARG
;
2602 FIXME("Notifications not supported yet\n");
2604 TRACE("ALSA does not support volume control\n");
2606 EnterCriticalSection(&session
->lock
);
2608 session
->master_vol
= level
;
2610 LeaveCriticalSection(&session
->lock
);
2615 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2616 ISimpleAudioVolume
*iface
, float *level
)
2618 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2619 AudioSession
*session
= This
->session
;
2621 TRACE("(%p)->(%p)\n", session
, level
);
2624 return NULL_PTR_ERR
;
2626 *level
= session
->master_vol
;
2631 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2632 BOOL mute
, const GUID
*context
)
2634 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2635 AudioSession
*session
= This
->session
;
2637 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2640 FIXME("Notifications not supported yet\n");
2642 session
->mute
= mute
;
2647 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2650 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2651 AudioSession
*session
= This
->session
;
2653 TRACE("(%p)->(%p)\n", session
, mute
);
2656 return NULL_PTR_ERR
;
2658 *mute
= session
->mute
;
2663 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2665 SimpleAudioVolume_QueryInterface
,
2666 SimpleAudioVolume_AddRef
,
2667 SimpleAudioVolume_Release
,
2668 SimpleAudioVolume_SetMasterVolume
,
2669 SimpleAudioVolume_GetMasterVolume
,
2670 SimpleAudioVolume_SetMute
,
2671 SimpleAudioVolume_GetMute
2674 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2675 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2677 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2683 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2684 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2687 IUnknown_AddRef((IUnknown
*)*ppv
);
2691 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2692 return E_NOINTERFACE
;
2695 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2697 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2698 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2701 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2703 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2704 return IAudioClient_Release(&This
->IAudioClient_iface
);
2707 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2708 IAudioStreamVolume
*iface
, UINT32
*out
)
2710 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2712 TRACE("(%p)->(%p)\n", This
, out
);
2717 *out
= This
->fmt
->nChannels
;
2722 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2723 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2725 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2727 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2729 if(level
< 0.f
|| level
> 1.f
)
2730 return E_INVALIDARG
;
2732 if(index
>= This
->fmt
->nChannels
)
2733 return E_INVALIDARG
;
2735 TRACE("ALSA does not support volume control\n");
2737 EnterCriticalSection(&This
->lock
);
2739 This
->vols
[index
] = level
;
2741 LeaveCriticalSection(&This
->lock
);
2746 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2747 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2749 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2751 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2756 if(index
>= This
->fmt
->nChannels
)
2757 return E_INVALIDARG
;
2759 *level
= This
->vols
[index
];
2764 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2765 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2767 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2770 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2775 if(count
!= This
->fmt
->nChannels
)
2776 return E_INVALIDARG
;
2778 TRACE("ALSA does not support volume control\n");
2780 EnterCriticalSection(&This
->lock
);
2782 for(i
= 0; i
< count
; ++i
)
2783 This
->vols
[i
] = levels
[i
];
2785 LeaveCriticalSection(&This
->lock
);
2790 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2791 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2793 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2796 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2801 if(count
!= This
->fmt
->nChannels
)
2802 return E_INVALIDARG
;
2804 EnterCriticalSection(&This
->lock
);
2806 for(i
= 0; i
< count
; ++i
)
2807 levels
[i
] = This
->vols
[i
];
2809 LeaveCriticalSection(&This
->lock
);
2814 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2816 AudioStreamVolume_QueryInterface
,
2817 AudioStreamVolume_AddRef
,
2818 AudioStreamVolume_Release
,
2819 AudioStreamVolume_GetChannelCount
,
2820 AudioStreamVolume_SetChannelVolume
,
2821 AudioStreamVolume_GetChannelVolume
,
2822 AudioStreamVolume_SetAllVolumes
,
2823 AudioStreamVolume_GetAllVolumes
2826 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2827 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2829 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2835 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2836 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2839 IUnknown_AddRef((IUnknown
*)*ppv
);
2843 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2844 return E_NOINTERFACE
;
2847 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2849 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2850 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2853 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2855 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2856 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2859 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
2860 IChannelAudioVolume
*iface
, UINT32
*out
)
2862 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2863 AudioSession
*session
= This
->session
;
2865 TRACE("(%p)->(%p)\n", session
, out
);
2868 return NULL_PTR_ERR
;
2870 *out
= session
->channel_count
;
2875 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
2876 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
2877 const GUID
*context
)
2879 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2880 AudioSession
*session
= This
->session
;
2882 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
2883 wine_dbgstr_guid(context
));
2885 if(level
< 0.f
|| level
> 1.f
)
2886 return E_INVALIDARG
;
2888 if(index
>= session
->channel_count
)
2889 return E_INVALIDARG
;
2892 FIXME("Notifications not supported yet\n");
2894 TRACE("ALSA does not support volume control\n");
2896 EnterCriticalSection(&session
->lock
);
2898 session
->channel_vols
[index
] = level
;
2900 LeaveCriticalSection(&session
->lock
);
2905 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
2906 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
2908 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2909 AudioSession
*session
= This
->session
;
2911 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
2914 return NULL_PTR_ERR
;
2916 if(index
>= session
->channel_count
)
2917 return E_INVALIDARG
;
2919 *level
= session
->channel_vols
[index
];
2924 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
2925 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
2926 const GUID
*context
)
2928 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2929 AudioSession
*session
= This
->session
;
2932 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
2933 wine_dbgstr_guid(context
));
2936 return NULL_PTR_ERR
;
2938 if(count
!= session
->channel_count
)
2939 return E_INVALIDARG
;
2942 FIXME("Notifications not supported yet\n");
2944 TRACE("ALSA does not support volume control\n");
2946 EnterCriticalSection(&session
->lock
);
2948 for(i
= 0; i
< count
; ++i
)
2949 session
->channel_vols
[i
] = levels
[i
];
2951 LeaveCriticalSection(&session
->lock
);
2956 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
2957 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
2959 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2960 AudioSession
*session
= This
->session
;
2963 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
2966 return NULL_PTR_ERR
;
2968 if(count
!= session
->channel_count
)
2969 return E_INVALIDARG
;
2971 for(i
= 0; i
< count
; ++i
)
2972 levels
[i
] = session
->channel_vols
[i
];
2977 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
2979 ChannelAudioVolume_QueryInterface
,
2980 ChannelAudioVolume_AddRef
,
2981 ChannelAudioVolume_Release
,
2982 ChannelAudioVolume_GetChannelCount
,
2983 ChannelAudioVolume_SetChannelVolume
,
2984 ChannelAudioVolume_GetChannelVolume
,
2985 ChannelAudioVolume_SetAllVolumes
,
2986 ChannelAudioVolume_GetAllVolumes
2989 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
2990 REFIID riid
, void **ppv
)
2992 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2998 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2999 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
3000 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
3003 IUnknown_AddRef((IUnknown
*)*ppv
);
3007 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3008 return E_NOINTERFACE
;
3011 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
3013 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3015 ref
= InterlockedIncrement(&This
->ref
);
3016 TRACE("(%p) Refcount now %u\n", This
, ref
);
3020 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
3022 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3024 ref
= InterlockedDecrement(&This
->ref
);
3025 TRACE("(%p) Refcount now %u\n", This
, ref
);
3027 HeapFree(GetProcessHeap(), 0, This
);
3031 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3032 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3033 IAudioSessionControl
**out
)
3035 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3036 AudioSession
*session
;
3037 AudioSessionWrapper
*wrapper
;
3040 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3043 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3047 wrapper
= AudioSessionWrapper_Create(NULL
);
3049 return E_OUTOFMEMORY
;
3051 wrapper
->session
= session
;
3053 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3058 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3059 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3060 ISimpleAudioVolume
**out
)
3062 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3063 AudioSession
*session
;
3064 AudioSessionWrapper
*wrapper
;
3067 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3070 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3074 wrapper
= AudioSessionWrapper_Create(NULL
);
3076 return E_OUTOFMEMORY
;
3078 wrapper
->session
= session
;
3080 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3085 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3086 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3088 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3089 FIXME("(%p)->(%p) - stub\n", This
, out
);
3093 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3094 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3096 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3097 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3101 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3102 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3104 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3105 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3109 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3110 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3111 IAudioVolumeDuckNotification
*notification
)
3113 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3114 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3118 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3119 IAudioSessionManager2
*iface
,
3120 IAudioVolumeDuckNotification
*notification
)
3122 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3123 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3127 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3129 AudioSessionManager_QueryInterface
,
3130 AudioSessionManager_AddRef
,
3131 AudioSessionManager_Release
,
3132 AudioSessionManager_GetAudioSessionControl
,
3133 AudioSessionManager_GetSimpleAudioVolume
,
3134 AudioSessionManager_GetSessionEnumerator
,
3135 AudioSessionManager_RegisterSessionNotification
,
3136 AudioSessionManager_UnregisterSessionNotification
,
3137 AudioSessionManager_RegisterDuckNotification
,
3138 AudioSessionManager_UnregisterDuckNotification
3141 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3142 IAudioSessionManager2
**out
)
3146 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3148 return E_OUTOFMEMORY
;
3150 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3151 This
->device
= device
;
3154 *out
= &This
->IAudioSessionManager2_iface
;