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;
55 #define EXTRA_SAFE_RT 40000
58 typedef struct ACImpl ACImpl
;
60 typedef struct _AudioSession
{
71 CRITICAL_SECTION lock
;
76 typedef struct _AudioSessionWrapper
{
77 IAudioSessionControl2 IAudioSessionControl2_iface
;
78 IChannelAudioVolume IChannelAudioVolume_iface
;
79 ISimpleAudioVolume ISimpleAudioVolume_iface
;
84 AudioSession
*session
;
85 } AudioSessionWrapper
;
88 IAudioClient IAudioClient_iface
;
89 IAudioRenderClient IAudioRenderClient_iface
;
90 IAudioCaptureClient IAudioCaptureClient_iface
;
91 IAudioClock IAudioClock_iface
;
92 IAudioClock2 IAudioClock2_iface
;
93 IAudioStreamVolume IAudioStreamVolume_iface
;
97 snd_pcm_t
*pcm_handle
;
98 snd_pcm_uframes_t alsa_bufsize_frames
, alsa_period_frames
;
99 snd_pcm_hw_params_t
*hw_params
; /* does not hold state between calls */
100 snd_pcm_format_t alsa_format
;
103 IUnknown
*pUnkFTMarshal
;
108 AUDCLNT_SHAREMODE share
;
114 int alsa_channel_map
[32];
116 BOOL initted
, started
;
117 REFERENCE_TIME mmdev_period_rt
;
118 UINT64 written_frames
, last_pos_frames
;
119 UINT32 bufsize_frames
, held_frames
, tmp_buffer_frames
, mmdev_period_frames
;
120 snd_pcm_uframes_t remapping_buf_frames
;
121 UINT32 lcl_offs_frames
; /* offs into local_buffer where valid data starts */
122 UINT32 wri_offs_frames
; /* where to write fresh data in local_buffer */
123 UINT32 hidden_frames
; /* ALSA reserve to ensure continuous rendering */
126 BYTE
*local_buffer
, *tmp_buffer
, *remapping_buf
;
127 LONG32 getbuf_last
; /* <0 when using tmp_buffer */
129 CRITICAL_SECTION lock
;
131 AudioSession
*session
;
132 AudioSessionWrapper
*session_wrapper
;
137 typedef struct _SessionMgr
{
138 IAudioSessionManager2 IAudioSessionManager2_iface
;
145 static HANDLE g_timer_q
;
147 static CRITICAL_SECTION g_sessions_lock
;
148 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug
=
150 0, 0, &g_sessions_lock
,
151 { &g_sessions_lock_debug
.ProcessLocksList
, &g_sessions_lock_debug
.ProcessLocksList
},
152 0, 0, { (DWORD_PTR
)(__FILE__
": g_sessions_lock") }
154 static CRITICAL_SECTION g_sessions_lock
= { &g_sessions_lock_debug
, -1, 0, 0, 0, 0 };
155 static struct list g_sessions
= LIST_INIT(g_sessions
);
157 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
158 static const char defname
[] = "default";
160 static const WCHAR drv_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
161 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
162 'w','i','n','e','a','l','s','a','.','d','r','v',0};
163 static const WCHAR drv_key_devicesW
[] = {'S','o','f','t','w','a','r','e','\\',
164 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
165 'w','i','n','e','a','l','s','a','.','d','r','v','\\','d','e','v','i','c','e','s',0};
166 static const WCHAR guidW
[] = {'g','u','i','d',0};
168 static const IAudioClientVtbl AudioClient_Vtbl
;
169 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
170 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
171 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
172 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
173 static const IAudioClockVtbl AudioClock_Vtbl
;
174 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
175 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
176 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
177 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
179 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
181 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
183 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
186 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
188 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
191 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
193 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
196 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
198 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
201 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
203 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
206 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
208 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
211 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
213 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
216 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
218 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
221 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
223 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
226 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
228 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
231 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
235 case DLL_PROCESS_ATTACH
:
236 g_timer_q
= CreateTimerQueue();
241 case DLL_PROCESS_DETACH
:
243 DeleteCriticalSection(&g_sessions_lock
);
249 /* From <dlls/mmdevapi/mmdevapi.h> */
250 enum DriverPriority
{
251 Priority_Unavailable
= 0,
257 int WINAPI
AUDDRV_GetPriority(void)
259 return Priority_Neutral
;
262 static void set_device_guid(EDataFlow flow
, HKEY drv_key
, const WCHAR
*key_name
,
270 lr
= RegCreateKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, NULL
, 0, KEY_WRITE
,
271 NULL
, &drv_key
, NULL
);
272 if(lr
!= ERROR_SUCCESS
){
273 ERR("RegCreateKeyEx(drv_key) failed: %u\n", lr
);
279 lr
= RegCreateKeyExW(drv_key
, key_name
, 0, NULL
, 0, KEY_WRITE
,
281 if(lr
!= ERROR_SUCCESS
){
282 ERR("RegCreateKeyEx(%s) failed: %u\n", wine_dbgstr_w(key_name
), lr
);
286 lr
= RegSetValueExW(key
, guidW
, 0, REG_BINARY
, (BYTE
*)guid
,
288 if(lr
!= ERROR_SUCCESS
)
289 ERR("RegSetValueEx(%s\\guid) failed: %u\n", wine_dbgstr_w(key_name
), lr
);
294 RegCloseKey(drv_key
);
297 static void get_device_guid(EDataFlow flow
, const char *device
, GUID
*guid
)
299 HKEY key
= NULL
, dev_key
;
300 DWORD type
, size
= sizeof(*guid
);
308 MultiByteToWideChar(CP_UNIXCP
, 0, device
, -1, key_name
+ 2,
309 (sizeof(key_name
) / sizeof(*key_name
)) - 2);
311 if(RegOpenKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, KEY_WRITE
|KEY_READ
, &key
) == ERROR_SUCCESS
){
312 if(RegOpenKeyExW(key
, key_name
, 0, KEY_READ
, &dev_key
) == ERROR_SUCCESS
){
313 if(RegQueryValueExW(dev_key
, guidW
, 0, &type
,
314 (BYTE
*)guid
, &size
) == ERROR_SUCCESS
){
315 if(type
== REG_BINARY
){
316 RegCloseKey(dev_key
);
320 ERR("Invalid type for device %s GUID: %u; ignoring and overwriting\n",
321 wine_dbgstr_w(key_name
), type
);
323 RegCloseKey(dev_key
);
329 set_device_guid(flow
, key
, key_name
, guid
);
335 static BOOL
alsa_try_open(const char *devnode
, snd_pcm_stream_t stream
)
340 TRACE("devnode: %s, stream: %d\n", devnode
, stream
);
342 if((err
= snd_pcm_open(&handle
, devnode
, stream
, SND_PCM_NONBLOCK
)) < 0){
343 WARN("The device \"%s\" failed to open: %d (%s).\n",
344 devnode
, err
, snd_strerror(err
));
348 snd_pcm_close(handle
);
352 static WCHAR
*construct_device_id(EDataFlow flow
, const WCHAR
*chunk1
, const char *chunk2
)
356 DWORD len_wchars
= 0, chunk1_len
, copied
= 0, prefix_len
;
358 static const WCHAR dashW
[] = {' ','-',' ',0};
359 static const size_t dashW_len
= (sizeof(dashW
) / sizeof(*dashW
)) - 1;
360 static const WCHAR outW
[] = {'O','u','t',':',' ',0};
361 static const WCHAR inW
[] = {'I','n',':',' ',0};
365 prefix_len
= (sizeof(outW
) / sizeof(*outW
)) - 1;
366 len_wchars
+= prefix_len
;
369 prefix_len
= (sizeof(inW
) / sizeof(*inW
)) - 1;
370 len_wchars
+= prefix_len
;
373 chunk1_len
= strlenW(chunk1
);
374 len_wchars
+= chunk1_len
;
377 len_wchars
+= dashW_len
;
379 len_wchars
+= MultiByteToWideChar(CP_UNIXCP
, 0, chunk2
, -1, NULL
, 0) - 1;
380 len_wchars
+= 1; /* NULL byte */
382 ret
= HeapAlloc(GetProcessHeap(), 0, len_wchars
* sizeof(WCHAR
));
384 memcpy(ret
, prefix
, prefix_len
* sizeof(WCHAR
));
385 copied
+= prefix_len
;
387 memcpy(ret
+ copied
, chunk1
, chunk1_len
* sizeof(WCHAR
));
388 copied
+= chunk1_len
;
390 if(chunk1
&& chunk2
){
391 memcpy(ret
+ copied
, dashW
, dashW_len
* sizeof(WCHAR
));
395 MultiByteToWideChar(CP_UNIXCP
, 0, chunk2
, -1, ret
+ copied
, len_wchars
- copied
);
399 TRACE("Enumerated device: %s\n", wine_dbgstr_w(ret
));
404 static HRESULT
alsa_get_card_devices(EDataFlow flow
, snd_pcm_stream_t stream
,
405 WCHAR
***ids
, GUID
**guids
, UINT
*num
, snd_ctl_t
*ctl
, int card
,
406 const WCHAR
*cardnameW
)
409 snd_pcm_info_t
*info
;
411 info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_info_sizeof());
413 return E_OUTOFMEMORY
;
415 snd_pcm_info_set_subdevice(info
, 0);
416 snd_pcm_info_set_stream(info
, stream
);
419 for(err
= snd_ctl_pcm_next_device(ctl
, &device
); device
!= -1 && err
>= 0;
420 err
= snd_ctl_pcm_next_device(ctl
, &device
)){
424 snd_pcm_info_set_device(info
, device
);
426 if((err
= snd_ctl_pcm_info(ctl
, info
)) < 0){
428 /* This device doesn't have the right stream direction */
431 WARN("Failed to get info for card %d, device %d: %d (%s)\n",
432 card
, device
, err
, snd_strerror(err
));
436 sprintf(devnode
, "plughw:%d,%d", card
, device
);
437 if(!alsa_try_open(devnode
, stream
))
441 *ids
= HeapReAlloc(GetProcessHeap(), 0, *ids
, sizeof(WCHAR
*) * (*num
+ 1));
442 *guids
= HeapReAlloc(GetProcessHeap(), 0, *guids
, sizeof(GUID
) * (*num
+ 1));
444 *ids
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*));
445 *guids
= HeapAlloc(GetProcessHeap(), 0, sizeof(GUID
));
448 devname
= snd_pcm_info_get_name(info
);
450 WARN("Unable to get device name for card %d, device %d\n", card
,
455 (*ids
)[*num
] = construct_device_id(flow
, cardnameW
, devname
);
456 get_device_guid(flow
, devnode
, &(*guids
)[*num
]);
461 HeapFree(GetProcessHeap(), 0, info
);
464 WARN("Got a failure during device enumeration on card %d: %d (%s)\n",
465 card
, err
, snd_strerror(err
));
470 static void get_reg_devices(EDataFlow flow
, snd_pcm_stream_t stream
, WCHAR
***ids
,
471 GUID
**guids
, UINT
*num
)
473 static const WCHAR ALSAOutputDevices
[] = {'A','L','S','A','O','u','t','p','u','t','D','e','v','i','c','e','s',0};
474 static const WCHAR ALSAInputDevices
[] = {'A','L','S','A','I','n','p','u','t','D','e','v','i','c','e','s',0};
476 WCHAR reg_devices
[256];
477 DWORD size
= sizeof(reg_devices
), type
;
478 const WCHAR
*value_name
= (stream
== SND_PCM_STREAM_PLAYBACK
) ? ALSAOutputDevices
: ALSAInputDevices
;
480 /* @@ Wine registry key: HKCU\Software\Wine\Drivers\winealsa.drv */
481 if(RegOpenKeyW(HKEY_CURRENT_USER
, drv_keyW
, &key
) == ERROR_SUCCESS
){
482 if(RegQueryValueExW(key
, value_name
, 0, &type
,
483 (BYTE
*)reg_devices
, &size
) == ERROR_SUCCESS
){
484 WCHAR
*p
= reg_devices
;
486 if(type
!= REG_MULTI_SZ
){
487 ERR("Registry ALSA device list value type must be REG_MULTI_SZ\n");
495 WideCharToMultiByte(CP_UNIXCP
, 0, p
, -1, devname
, sizeof(devname
), NULL
, NULL
);
497 if(alsa_try_open(devname
, stream
)){
499 *ids
= HeapReAlloc(GetProcessHeap(), 0, *ids
, sizeof(WCHAR
*) * (*num
+ 1));
500 *guids
= HeapReAlloc(GetProcessHeap(), 0, *guids
, sizeof(GUID
) * (*num
+ 1));
502 *ids
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*));
503 *guids
= HeapAlloc(GetProcessHeap(), 0, sizeof(GUID
));
505 (*ids
)[*num
] = construct_device_id(flow
, p
, NULL
);
506 get_device_guid(flow
, devname
, &(*guids
)[*num
]);
510 p
+= lstrlenW(p
) + 1;
518 static HRESULT
alsa_enum_devices(EDataFlow flow
, WCHAR
***ids
, GUID
**guids
,
521 snd_pcm_stream_t stream
= (flow
== eRender
? SND_PCM_STREAM_PLAYBACK
:
522 SND_PCM_STREAM_CAPTURE
);
528 if(alsa_try_open(defname
, stream
)){
529 *ids
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*));
530 (*ids
)[0] = construct_device_id(flow
, defaultW
, NULL
);
531 *guids
= HeapAlloc(GetProcessHeap(), 0, sizeof(GUID
));
532 get_device_guid(flow
, defname
, &(*guids
)[0]);
536 get_reg_devices(flow
, stream
, ids
, guids
, num
);
538 for(err
= snd_card_next(&card
); card
!= -1 && err
>= 0;
539 err
= snd_card_next(&card
)){
546 sprintf(cardpath
, "hw:%u", card
);
548 if((err
= snd_ctl_open(&ctl
, cardpath
, 0)) < 0){
549 WARN("Unable to open ctl for ALSA device %s: %d (%s)\n", cardpath
,
550 err
, snd_strerror(err
));
554 if(snd_card_get_name(card
, &cardname
) < 0) {
555 /* FIXME: Should be localized */
556 static const WCHAR nameW
[] = {'U','n','k','n','o','w','n',' ','s','o','u','n','d','c','a','r','d',0};
557 WARN("Unable to get card name for ALSA device %s: %d (%s)\n",
558 cardpath
, err
, snd_strerror(err
));
559 alsa_get_card_devices(flow
, stream
, ids
, guids
, num
, ctl
, card
, nameW
);
561 len
= MultiByteToWideChar(CP_UNIXCP
, 0, cardname
, -1, NULL
, 0);
562 cardnameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
567 return E_OUTOFMEMORY
;
569 MultiByteToWideChar(CP_UNIXCP
, 0, cardname
, -1, cardnameW
, len
);
571 alsa_get_card_devices(flow
, stream
, ids
, guids
, num
, ctl
, card
, cardnameW
);
573 HeapFree(GetProcessHeap(), 0, cardnameW
);
581 WARN("Got a failure during card enumeration: %d (%s)\n",
582 err
, snd_strerror(err
));
587 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
, GUID
**guids
,
588 UINT
*num
, UINT
*def_index
)
592 TRACE("%d %p %p %p %p\n", flow
, ids
, guids
, num
, def_index
);
597 hr
= alsa_enum_devices(flow
, ids
, guids
, num
);
600 for(i
= 0; i
< *num
; ++i
)
601 HeapFree(GetProcessHeap(), 0, (*ids
)[i
]);
602 HeapFree(GetProcessHeap(), 0, *ids
);
603 HeapFree(GetProcessHeap(), 0, *guids
);
604 return E_OUTOFMEMORY
;
607 TRACE("Enumerated %u devices\n", *num
);
610 HeapFree(GetProcessHeap(), 0, *ids
);
612 HeapFree(GetProcessHeap(), 0, *guids
);
621 /* Using the pulse PCM device from alsa-plugins 1.0.24 triggers a bug
622 * which causes audio to cease playing after a few minutes of playback.
623 * Setting handle_underrun=1 on pulse-backed ALSA devices seems to work
624 * around this issue. */
625 static snd_config_t
*make_handle_underrun_config(const char *name
)
627 snd_config_t
*lconf
, *dev_node
, *hu_node
, *type_node
;
628 char dev_node_name
[64];
629 const char *type_str
;
634 if((err
= snd_config_copy(&lconf
, snd_config
)) < 0){
635 WARN("snd_config_copy failed: %d (%s)\n", err
, snd_strerror(err
));
639 sprintf(dev_node_name
, "pcm.%s", name
);
640 err
= snd_config_search(lconf
, dev_node_name
, &dev_node
);
642 snd_config_delete(lconf
);
646 snd_config_delete(lconf
);
647 WARN("snd_config_search failed: %d (%s)\n", err
, snd_strerror(err
));
651 /* ALSA is extremely fragile. If it runs into a config setting it doesn't
652 * recognize, it tends to fail or assert. So we only want to inject
653 * handle_underrun=1 on devices that we know will recognize it. */
654 err
= snd_config_search(dev_node
, "type", &type_node
);
656 snd_config_delete(lconf
);
660 snd_config_delete(lconf
);
661 WARN("snd_config_search failed: %d (%s)\n", err
, snd_strerror(err
));
665 if((err
= snd_config_get_string(type_node
, &type_str
)) < 0){
666 snd_config_delete(lconf
);
670 if(strcmp(type_str
, "pulse") != 0){
671 snd_config_delete(lconf
);
675 err
= snd_config_search(dev_node
, "handle_underrun", &hu_node
);
677 /* user already has an explicit handle_underrun setting, so don't
678 * use a local config */
679 snd_config_delete(lconf
);
683 snd_config_delete(lconf
);
684 WARN("snd_config_search failed: %d (%s)\n", err
, snd_strerror(err
));
688 if((err
= snd_config_imake_integer(&hu_node
, "handle_underrun", 1)) < 0){
689 snd_config_delete(lconf
);
690 WARN("snd_config_imake_integer failed: %d (%s)\n", err
,
695 if((err
= snd_config_add(dev_node
, hu_node
)) < 0){
696 snd_config_delete(lconf
);
697 WARN("snd_config_add failed: %d (%s)\n", err
, snd_strerror(err
));
704 static BOOL
get_alsa_name_by_guid(GUID
*guid
, char *name
, DWORD name_size
, EDataFlow
*flow
)
711 if(RegOpenKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, KEY_READ
, &devices_key
) != ERROR_SUCCESS
){
712 ERR("No devices found in registry?\n");
721 key_name_size
= sizeof(key_name
)/sizeof(WCHAR
);
722 if(RegEnumKeyExW(devices_key
, i
, key_name
, &key_name_size
, NULL
,
723 NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
726 if(RegOpenKeyExW(devices_key
, key_name
, 0, KEY_READ
, &key
) != ERROR_SUCCESS
){
727 WARN("Couldn't open key: %s\n", wine_dbgstr_w(key_name
));
731 size
= sizeof(reg_guid
);
732 if(RegQueryValueExW(key
, guidW
, 0, &type
,
733 (BYTE
*)®_guid
, &size
) == ERROR_SUCCESS
){
734 if(IsEqualGUID(®_guid
, guid
)){
736 RegCloseKey(devices_key
);
738 TRACE("Found matching device key: %s\n", wine_dbgstr_w(key_name
));
740 if(key_name
[0] == '0')
742 else if(key_name
[0] == '1')
745 ERR("Unknown device type: %c\n", key_name
[0]);
749 WideCharToMultiByte(CP_UNIXCP
, 0, key_name
+ 2, -1, name
, name_size
, NULL
, NULL
);
760 RegCloseKey(devices_key
);
762 WARN("No matching device in registry for GUID %s\n", debugstr_guid(guid
));
767 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(GUID
*guid
, IMMDevice
*dev
, IAudioClient
**out
)
771 snd_pcm_stream_t stream
;
773 static BOOL handle_underrun
= TRUE
;
778 TRACE("%s %p %p\n", debugstr_guid(guid
), dev
, out
);
780 if(!get_alsa_name_by_guid(guid
, alsa_name
, sizeof(alsa_name
), &dataflow
))
781 return AUDCLNT_E_DEVICE_INVALIDATED
;
783 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
785 return E_OUTOFMEMORY
;
787 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
788 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
789 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
790 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
791 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
792 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
794 if(dataflow
== eRender
)
795 stream
= SND_PCM_STREAM_PLAYBACK
;
796 else if(dataflow
== eCapture
)
797 stream
= SND_PCM_STREAM_CAPTURE
;
799 HeapFree(GetProcessHeap(), 0, This
);
803 hr
= CoCreateFreeThreadedMarshaler((IUnknown
*)&This
->IAudioClient_iface
,
804 (IUnknown
**)&This
->pUnkFTMarshal
);
806 HeapFree(GetProcessHeap(), 0, This
);
810 This
->dataflow
= dataflow
;
811 if(handle_underrun
&& ((lconf
= make_handle_underrun_config(alsa_name
)))){
812 err
= snd_pcm_open_lconf(&This
->pcm_handle
, alsa_name
, stream
, SND_PCM_NONBLOCK
, lconf
);
813 TRACE("Opening PCM device \"%s\" with handle_underrun: %d\n", alsa_name
, err
);
814 snd_config_delete(lconf
);
815 /* Pulse <= 2010 returns EINVAL, it does not know handle_underrun. */
817 ERR_(winediag
)("PulseAudio \"%s\" %d without handle_underrun. Audio may hang."
818 " Please upgrade to alsa_plugins >= 1.0.24\n", alsa_name
, err
);
819 handle_underrun
= FALSE
;
824 err
= snd_pcm_open(&This
->pcm_handle
, alsa_name
, stream
, SND_PCM_NONBLOCK
);
827 HeapFree(GetProcessHeap(), 0, This
);
828 WARN("Unable to open PCM \"%s\": %d (%s)\n", alsa_name
, err
, snd_strerror(err
));
831 return AUDCLNT_E_DEVICE_IN_USE
;
833 return AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
837 This
->hw_params
= HeapAlloc(GetProcessHeap(), 0,
838 snd_pcm_hw_params_sizeof());
839 if(!This
->hw_params
){
840 snd_pcm_close(This
->pcm_handle
);
841 HeapFree(GetProcessHeap(), 0, This
);
842 return E_OUTOFMEMORY
;
845 InitializeCriticalSection(&This
->lock
);
846 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ACImpl.lock");
849 IMMDevice_AddRef(This
->parent
);
851 *out
= &This
->IAudioClient_iface
;
852 IAudioClient_AddRef(&This
->IAudioClient_iface
);
857 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
858 REFIID riid
, void **ppv
)
860 ACImpl
*This
= impl_from_IAudioClient(iface
);
861 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
866 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
868 else if(IsEqualIID(riid
, &IID_IMarshal
))
869 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
872 IUnknown_AddRef((IUnknown
*)*ppv
);
875 WARN("Unknown interface %s\n", debugstr_guid(riid
));
876 return E_NOINTERFACE
;
879 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
881 ACImpl
*This
= impl_from_IAudioClient(iface
);
883 ref
= InterlockedIncrement(&This
->ref
);
884 TRACE("(%p) Refcount now %u\n", This
, ref
);
888 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
890 ACImpl
*This
= impl_from_IAudioClient(iface
);
893 ref
= InterlockedDecrement(&This
->ref
);
894 TRACE("(%p) Refcount now %u\n", This
, ref
);
899 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
900 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
901 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
903 WaitForSingleObject(event
, INFINITE
);
907 IAudioClient_Stop(iface
);
908 IMMDevice_Release(This
->parent
);
909 IUnknown_Release(This
->pUnkFTMarshal
);
910 This
->lock
.DebugInfo
->Spare
[0] = 0;
911 DeleteCriticalSection(&This
->lock
);
912 snd_pcm_drop(This
->pcm_handle
);
913 snd_pcm_close(This
->pcm_handle
);
915 EnterCriticalSection(&g_sessions_lock
);
916 list_remove(&This
->entry
);
917 LeaveCriticalSection(&g_sessions_lock
);
919 HeapFree(GetProcessHeap(), 0, This
->vols
);
920 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
921 HeapFree(GetProcessHeap(), 0, This
->remapping_buf
);
922 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
923 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
924 CoTaskMemFree(This
->fmt
);
925 HeapFree(GetProcessHeap(), 0, This
);
930 static void dump_fmt(const WAVEFORMATEX
*fmt
)
932 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
933 switch(fmt
->wFormatTag
){
934 case WAVE_FORMAT_PCM
:
935 TRACE("WAVE_FORMAT_PCM");
937 case WAVE_FORMAT_IEEE_FLOAT
:
938 TRACE("WAVE_FORMAT_IEEE_FLOAT");
940 case WAVE_FORMAT_EXTENSIBLE
:
941 TRACE("WAVE_FORMAT_EXTENSIBLE");
949 TRACE("nChannels: %u\n", fmt
->nChannels
);
950 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
951 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
952 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
953 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
954 TRACE("cbSize: %u\n", fmt
->cbSize
);
956 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
957 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
958 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
959 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
960 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
964 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
969 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
970 size
= sizeof(WAVEFORMATEXTENSIBLE
);
972 size
= sizeof(WAVEFORMATEX
);
974 ret
= CoTaskMemAlloc(size
);
978 memcpy(ret
, fmt
, size
);
980 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
985 static snd_pcm_format_t
alsa_format(const WAVEFORMATEX
*fmt
)
987 snd_pcm_format_t format
= SND_PCM_FORMAT_UNKNOWN
;
988 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
990 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
991 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
992 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
993 if(fmt
->wBitsPerSample
== 8)
994 format
= SND_PCM_FORMAT_U8
;
995 else if(fmt
->wBitsPerSample
== 16)
996 format
= SND_PCM_FORMAT_S16_LE
;
997 else if(fmt
->wBitsPerSample
== 24)
998 format
= SND_PCM_FORMAT_S24_3LE
;
999 else if(fmt
->wBitsPerSample
== 32)
1000 format
= SND_PCM_FORMAT_S32_LE
;
1002 WARN("Unsupported bit depth: %u\n", fmt
->wBitsPerSample
);
1003 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1004 fmt
->wBitsPerSample
!= fmtex
->Samples
.wValidBitsPerSample
){
1005 if(fmtex
->Samples
.wValidBitsPerSample
== 20 && fmt
->wBitsPerSample
== 24)
1006 format
= SND_PCM_FORMAT_S20_3LE
;
1008 WARN("Unsupported ValidBits: %u\n", fmtex
->Samples
.wValidBitsPerSample
);
1010 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
1011 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1012 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
1013 if(fmt
->wBitsPerSample
== 32)
1014 format
= SND_PCM_FORMAT_FLOAT_LE
;
1015 else if(fmt
->wBitsPerSample
== 64)
1016 format
= SND_PCM_FORMAT_FLOAT64_LE
;
1018 WARN("Unsupported float size: %u\n", fmt
->wBitsPerSample
);
1020 WARN("Unknown wave format: %04x\n", fmt
->wFormatTag
);
1024 static void session_init_vols(AudioSession
*session
, UINT channels
)
1026 if(session
->channel_count
< channels
){
1029 if(session
->channel_vols
)
1030 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
1031 session
->channel_vols
, sizeof(float) * channels
);
1033 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
1034 sizeof(float) * channels
);
1035 if(!session
->channel_vols
)
1038 for(i
= session
->channel_count
; i
< channels
; ++i
)
1039 session
->channel_vols
[i
] = 1.f
;
1041 session
->channel_count
= channels
;
1045 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
1050 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
1054 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
1056 ret
->device
= device
;
1058 list_init(&ret
->clients
);
1060 list_add_head(&g_sessions
, &ret
->entry
);
1062 InitializeCriticalSection(&ret
->lock
);
1063 ret
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AudioSession.lock");
1065 session_init_vols(ret
, num_channels
);
1067 ret
->master_vol
= 1.f
;
1072 /* if channels == 0, then this will return or create a session with
1073 * matching dataflow and GUID. otherwise, channels must also match */
1074 static HRESULT
get_audio_session(const GUID
*sessionguid
,
1075 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
1077 AudioSession
*session
;
1079 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
1080 *out
= create_session(&GUID_NULL
, device
, channels
);
1082 return E_OUTOFMEMORY
;
1088 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
1089 if(session
->device
== device
&&
1090 IsEqualGUID(sessionguid
, &session
->guid
)){
1091 session_init_vols(session
, channels
);
1098 *out
= create_session(sessionguid
, device
, channels
);
1100 return E_OUTOFMEMORY
;
1106 static int alsa_channel_index(DWORD flag
)
1109 case SPEAKER_FRONT_LEFT
:
1111 case SPEAKER_FRONT_RIGHT
:
1113 case SPEAKER_BACK_LEFT
:
1115 case SPEAKER_BACK_RIGHT
:
1117 case SPEAKER_FRONT_CENTER
:
1119 case SPEAKER_LOW_FREQUENCY
:
1121 case SPEAKER_SIDE_LEFT
:
1123 case SPEAKER_SIDE_RIGHT
:
1129 static BOOL
need_remapping(ACImpl
*This
, const WAVEFORMATEX
*fmt
)
1132 for(i
= 0; i
< fmt
->nChannels
; ++i
){
1133 if(This
->alsa_channel_map
[i
] != i
)
1139 static DWORD
get_channel_mask(unsigned int channels
)
1145 return KSAUDIO_SPEAKER_MONO
;
1147 return KSAUDIO_SPEAKER_STEREO
;
1149 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
1151 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
1153 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
1155 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
1157 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
1159 return KSAUDIO_SPEAKER_7POINT1_SURROUND
; /* Vista deprecates 7POINT1 */
1161 FIXME("Unknown speaker configuration: %u\n", channels
);
1165 static HRESULT
map_channels(ACImpl
*This
, const WAVEFORMATEX
*fmt
)
1167 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
|| fmt
->nChannels
> 2){
1168 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
1169 DWORD mask
, flag
= SPEAKER_FRONT_LEFT
;
1172 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1173 fmtex
->dwChannelMask
!= 0)
1174 mask
= fmtex
->dwChannelMask
;
1176 mask
= get_channel_mask(fmt
->nChannels
);
1178 This
->alsa_channels
= 0;
1180 while(i
< fmt
->nChannels
&& !(flag
& SPEAKER_RESERVED
)){
1182 This
->alsa_channel_map
[i
] = alsa_channel_index(flag
);
1183 TRACE("Mapping mmdevapi channel %u (0x%x) to ALSA channel %d\n",
1184 i
, flag
, This
->alsa_channel_map
[i
]);
1185 if(This
->alsa_channel_map
[i
] >= This
->alsa_channels
)
1186 This
->alsa_channels
= This
->alsa_channel_map
[i
] + 1;
1192 while(i
< fmt
->nChannels
){
1193 This
->alsa_channel_map
[i
] = This
->alsa_channels
;
1194 TRACE("Mapping mmdevapi channel %u to ALSA channel %d\n",
1195 i
, This
->alsa_channel_map
[i
]);
1196 ++This
->alsa_channels
;
1200 for(i
= 0; i
< fmt
->nChannels
; ++i
){
1201 if(This
->alsa_channel_map
[i
] == -1){
1202 This
->alsa_channel_map
[i
] = This
->alsa_channels
;
1203 ++This
->alsa_channels
;
1204 TRACE("Remapping mmdevapi channel %u to ALSA channel %d\n",
1205 i
, This
->alsa_channel_map
[i
]);
1209 This
->need_remapping
= need_remapping(This
, fmt
);
1211 TRACE("need_remapping: %u, alsa_channels: %d\n", This
->need_remapping
, This
->alsa_channels
);
1213 This
->need_remapping
= FALSE
;
1214 This
->alsa_channels
= fmt
->nChannels
;
1215 TRACE("need_remapping: %u, alsa_channels: %d\n", This
->need_remapping
, This
->alsa_channels
);
1221 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
1222 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
1223 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
1224 const GUID
*sessionguid
)
1226 ACImpl
*This
= impl_from_IAudioClient(iface
);
1227 snd_pcm_sw_params_t
*sw_params
= NULL
;
1228 snd_pcm_format_t format
;
1229 unsigned int rate
, alsa_period_us
;
1233 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
1234 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
1239 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1240 return AUDCLNT_E_NOT_INITIALIZED
;
1242 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
1243 AUDCLNT_STREAMFLAGS_LOOPBACK
|
1244 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
1245 AUDCLNT_STREAMFLAGS_NOPERSIST
|
1246 AUDCLNT_STREAMFLAGS_RATEADJUST
|
1247 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
1248 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
1249 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
1250 TRACE("Unknown flags: %08x\n", flags
);
1251 return E_INVALIDARG
;
1254 if(mode
== AUDCLNT_SHAREMODE_SHARED
){
1255 period
= DefaultPeriod
;
1256 if( duration
< 3 * period
)
1257 duration
= 3 * period
;
1259 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1260 if(((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
== 0 ||
1261 ((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
& SPEAKER_RESERVED
)
1262 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1266 period
= DefaultPeriod
; /* not minimum */
1267 if(period
< MinimumPeriod
|| period
> 5000000)
1268 return AUDCLNT_E_INVALID_DEVICE_PERIOD
;
1269 if(duration
> 20000000) /* the smaller the period, the lower this limit */
1270 return AUDCLNT_E_BUFFER_SIZE_ERROR
;
1271 if(flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
){
1272 if(duration
!= period
)
1273 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL
;
1274 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
1275 return AUDCLNT_E_DEVICE_IN_USE
;
1277 if( duration
< 8 * period
)
1278 duration
= 8 * period
; /* may grow above 2s */
1282 EnterCriticalSection(&This
->lock
);
1285 LeaveCriticalSection(&This
->lock
);
1286 return AUDCLNT_E_ALREADY_INITIALIZED
;
1291 if(FAILED(map_channels(This
, fmt
))){
1292 WARN("map_channels failed\n");
1293 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1297 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1298 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
1299 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1303 if((err
= snd_pcm_hw_params_set_access(This
->pcm_handle
, This
->hw_params
,
1304 SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0){
1305 WARN("Unable to set access: %d (%s)\n", err
, snd_strerror(err
));
1306 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1310 format
= alsa_format(fmt
);
1311 if (format
== SND_PCM_FORMAT_UNKNOWN
){
1312 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1316 if((err
= snd_pcm_hw_params_set_format(This
->pcm_handle
, This
->hw_params
,
1318 WARN("Unable to set ALSA format to %u: %d (%s)\n", format
, err
,
1320 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1324 This
->alsa_format
= format
;
1326 rate
= fmt
->nSamplesPerSec
;
1327 if((err
= snd_pcm_hw_params_set_rate_near(This
->pcm_handle
, This
->hw_params
,
1329 WARN("Unable to set rate to %u: %d (%s)\n", rate
, err
,
1331 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1335 if((err
= snd_pcm_hw_params_set_channels(This
->pcm_handle
, This
->hw_params
,
1336 This
->alsa_channels
)) < 0){
1337 WARN("Unable to set channels to %u: %d (%s)\n", fmt
->nChannels
, err
,
1339 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1343 This
->mmdev_period_rt
= period
;
1344 alsa_period_us
= This
->mmdev_period_rt
/ 10;
1345 if((err
= snd_pcm_hw_params_set_period_time_near(This
->pcm_handle
,
1346 This
->hw_params
, &alsa_period_us
, NULL
)) < 0)
1347 WARN("Unable to set period time near %u: %d (%s)\n", alsa_period_us
,
1348 err
, snd_strerror(err
));
1349 /* ALSA updates the output variable alsa_period_us */
1351 This
->mmdev_period_frames
= MulDiv(fmt
->nSamplesPerSec
,
1352 This
->mmdev_period_rt
, 10000000);
1354 /* Buffer 4 ALSA periods if large enough, else 4 mmdevapi periods */
1355 This
->alsa_bufsize_frames
= This
->mmdev_period_frames
* 4;
1356 if(err
< 0 || alsa_period_us
< period
/ 10)
1357 err
= snd_pcm_hw_params_set_buffer_size_near(This
->pcm_handle
,
1358 This
->hw_params
, &This
->alsa_bufsize_frames
);
1360 unsigned int periods
= 4;
1361 err
= snd_pcm_hw_params_set_periods_near(This
->pcm_handle
, This
->hw_params
, &periods
, NULL
);
1364 WARN("Unable to set buffer size: %d (%s)\n", err
, snd_strerror(err
));
1366 if((err
= snd_pcm_hw_params(This
->pcm_handle
, This
->hw_params
)) < 0){
1367 WARN("Unable to set hw params: %d (%s)\n", err
, snd_strerror(err
));
1368 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1372 if((err
= snd_pcm_hw_params_get_period_size(This
->hw_params
,
1373 &This
->alsa_period_frames
, NULL
)) < 0){
1374 WARN("Unable to get period size: %d (%s)\n", err
, snd_strerror(err
));
1375 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1379 if((err
= snd_pcm_hw_params_get_buffer_size(This
->hw_params
,
1380 &This
->alsa_bufsize_frames
)) < 0){
1381 WARN("Unable to get buffer size: %d (%s)\n", err
, snd_strerror(err
));
1382 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1386 sw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_sw_params_sizeof());
1392 if((err
= snd_pcm_sw_params_current(This
->pcm_handle
, sw_params
)) < 0){
1393 WARN("Unable to get sw_params: %d (%s)\n", err
, snd_strerror(err
));
1394 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1398 if((err
= snd_pcm_sw_params_set_start_threshold(This
->pcm_handle
,
1399 sw_params
, 1)) < 0){
1400 WARN("Unable set start threshold to 0: %d (%s)\n", err
, snd_strerror(err
));
1401 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1405 if((err
= snd_pcm_sw_params_set_stop_threshold(This
->pcm_handle
,
1406 sw_params
, This
->alsa_bufsize_frames
)) < 0){
1407 WARN("Unable set stop threshold to %lu: %d (%s)\n",
1408 This
->alsa_bufsize_frames
, err
, snd_strerror(err
));
1409 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1413 if((err
= snd_pcm_sw_params(This
->pcm_handle
, sw_params
)) < 0){
1414 WARN("Unable to set sw params: %d (%s)\n", err
, snd_strerror(err
));
1415 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1419 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0){
1420 WARN("Unable to prepare device: %d (%s)\n", err
, snd_strerror(err
));
1421 hr
= AUDCLNT_E_ENDPOINT_CREATE_FAILED
;
1425 /* Bear in mind weird situations where
1426 * ALSA period (50ms) > mmdevapi buffer (3x10ms)
1427 * or surprising rounding as seen with 22050x8x1 with Pulse:
1428 * ALSA period 220 vs. 221 frames in mmdevapi and
1429 * buffer 883 vs. 2205 frames in mmdevapi! */
1430 This
->bufsize_frames
= MulDiv(duration
, fmt
->nSamplesPerSec
, 10000000);
1431 if(mode
== AUDCLNT_SHAREMODE_EXCLUSIVE
)
1432 This
->bufsize_frames
-= This
->bufsize_frames
% This
->mmdev_period_frames
;
1433 This
->hidden_frames
= This
->alsa_period_frames
+ This
->mmdev_period_frames
+
1434 MulDiv(fmt
->nSamplesPerSec
, EXTRA_SAFE_RT
, 10000000);
1436 /* Check if the ALSA buffer is so small that it will run out before
1437 * the next MMDevAPI period tick occurs. Allow a little wiggle room
1438 * with 120% of the period time. */
1439 if(This
->alsa_bufsize_frames
< 1.2 * This
->mmdev_period_frames
)
1440 FIXME("ALSA buffer time is too small. Expect underruns. (%lu < %u * 1.2)\n",
1441 This
->alsa_bufsize_frames
, This
->mmdev_period_frames
);
1443 This
->local_buffer
= HeapAlloc(GetProcessHeap(), 0,
1444 This
->bufsize_frames
* fmt
->nBlockAlign
);
1445 if(!This
->local_buffer
){
1449 if (fmt
->wBitsPerSample
== 8)
1450 memset(This
->local_buffer
, 128, This
->bufsize_frames
* fmt
->nBlockAlign
);
1452 memset(This
->local_buffer
, 0, This
->bufsize_frames
* fmt
->nBlockAlign
);
1454 This
->fmt
= clone_format(fmt
);
1460 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
1466 for(i
= 0; i
< fmt
->nChannels
; ++i
)
1467 This
->vols
[i
] = 1.f
;
1470 This
->flags
= flags
;
1472 EnterCriticalSection(&g_sessions_lock
);
1474 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
1477 LeaveCriticalSection(&g_sessions_lock
);
1481 list_add_tail(&This
->session
->clients
, &This
->entry
);
1483 LeaveCriticalSection(&g_sessions_lock
);
1485 This
->initted
= TRUE
;
1487 TRACE("ALSA period: %lu frames\n", This
->alsa_period_frames
);
1488 TRACE("ALSA buffer: %lu frames\n", This
->alsa_bufsize_frames
);
1489 TRACE("MMDevice period: %u frames\n", This
->mmdev_period_frames
);
1490 TRACE("MMDevice buffer: %u frames\n", This
->bufsize_frames
);
1493 HeapFree(GetProcessHeap(), 0, sw_params
);
1495 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
1496 This
->local_buffer
= NULL
;
1497 CoTaskMemFree(This
->fmt
);
1499 HeapFree(GetProcessHeap(), 0, This
->vols
);
1503 LeaveCriticalSection(&This
->lock
);
1508 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
1511 ACImpl
*This
= impl_from_IAudioClient(iface
);
1513 TRACE("(%p)->(%p)\n", This
, out
);
1518 EnterCriticalSection(&This
->lock
);
1521 LeaveCriticalSection(&This
->lock
);
1522 return AUDCLNT_E_NOT_INITIALIZED
;
1525 *out
= This
->bufsize_frames
;
1527 LeaveCriticalSection(&This
->lock
);
1532 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1533 REFERENCE_TIME
*latency
)
1535 ACImpl
*This
= impl_from_IAudioClient(iface
);
1537 TRACE("(%p)->(%p)\n", This
, latency
);
1542 EnterCriticalSection(&This
->lock
);
1545 LeaveCriticalSection(&This
->lock
);
1546 return AUDCLNT_E_NOT_INITIALIZED
;
1549 /* Hide some frames in the ALSA buffer. Allows us to return GetCurrentPadding=0
1550 * yet have enough data left to play (as if it were in native's mixer). Add:
1551 * + mmdevapi_period such that at the end of it, ALSA still has data;
1552 * + EXTRA_SAFE (~4ms) to allow for late callback invocation / fluctuation;
1553 * + alsa_period such that ALSA always has at least one period to play. */
1554 if(This
->dataflow
== eRender
)
1555 *latency
= MulDiv(This
->hidden_frames
, 10000000, This
->fmt
->nSamplesPerSec
);
1557 *latency
= MulDiv(This
->alsa_period_frames
, 10000000, This
->fmt
->nSamplesPerSec
)
1558 + This
->mmdev_period_rt
;
1560 LeaveCriticalSection(&This
->lock
);
1565 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1568 ACImpl
*This
= impl_from_IAudioClient(iface
);
1570 TRACE("(%p)->(%p)\n", This
, out
);
1575 EnterCriticalSection(&This
->lock
);
1578 LeaveCriticalSection(&This
->lock
);
1579 return AUDCLNT_E_NOT_INITIALIZED
;
1582 /* padding is solely updated at callback time in shared mode */
1583 *out
= This
->held_frames
;
1585 LeaveCriticalSection(&This
->lock
);
1587 TRACE("pad: %u\n", *out
);
1592 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1593 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*fmt
,
1596 ACImpl
*This
= impl_from_IAudioClient(iface
);
1597 snd_pcm_format_mask_t
*formats
= NULL
;
1598 snd_pcm_format_t format
;
1600 WAVEFORMATEX
*closest
= NULL
;
1601 unsigned int max
= 0, min
= 0;
1604 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, fmt
, out
);
1606 if(!fmt
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !out
))
1609 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1610 return E_INVALIDARG
;
1612 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1613 fmt
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1614 return E_INVALIDARG
;
1620 if(mode
!= AUDCLNT_SHAREMODE_SHARED
)
1624 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1625 (fmt
->nAvgBytesPerSec
== 0 ||
1626 fmt
->nBlockAlign
== 0 ||
1627 ((WAVEFORMATEXTENSIBLE
*)fmt
)->Samples
.wValidBitsPerSample
> fmt
->wBitsPerSample
))
1628 return E_INVALIDARG
;
1630 if(fmt
->nChannels
== 0)
1631 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1633 EnterCriticalSection(&This
->lock
);
1635 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1636 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1640 formats
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1641 snd_pcm_format_mask_sizeof());
1647 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1648 format
= alsa_format(fmt
);
1649 if (format
== SND_PCM_FORMAT_UNKNOWN
||
1650 !snd_pcm_format_mask_test(formats
, format
)){
1651 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1655 closest
= clone_format(fmt
);
1661 if((err
= snd_pcm_hw_params_get_rate_min(This
->hw_params
, &min
, NULL
)) < 0){
1662 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1663 WARN("Unable to get min rate: %d (%s)\n", err
, snd_strerror(err
));
1667 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max
, NULL
)) < 0){
1668 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1669 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1673 if(fmt
->nSamplesPerSec
< min
|| fmt
->nSamplesPerSec
> max
){
1674 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1678 if((err
= snd_pcm_hw_params_get_channels_min(This
->hw_params
, &min
)) < 0){
1679 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1680 WARN("Unable to get min channels: %d (%s)\n", err
, snd_strerror(err
));
1684 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
, &max
)) < 0){
1685 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1686 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1689 if(fmt
->nChannels
> max
){
1691 closest
->nChannels
= max
;
1692 }else if(fmt
->nChannels
< min
){
1694 closest
->nChannels
= min
;
1697 if(FAILED(map_channels(This
, fmt
))){
1698 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1699 WARN("map_channels failed\n");
1702 if(This
->alsa_channels
> max
){
1704 closest
->nChannels
= max
;
1707 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
1708 ((WAVEFORMATEXTENSIBLE
*)closest
)->dwChannelMask
= get_channel_mask(closest
->nChannels
);
1710 if(fmt
->nBlockAlign
!= fmt
->nChannels
* fmt
->wBitsPerSample
/ 8 ||
1711 fmt
->nAvgBytesPerSec
!= fmt
->nBlockAlign
* fmt
->nSamplesPerSec
||
1712 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1713 ((WAVEFORMATEXTENSIBLE
*)fmt
)->Samples
.wValidBitsPerSample
< fmt
->wBitsPerSample
))
1716 if(mode
== AUDCLNT_SHAREMODE_EXCLUSIVE
&&
1717 fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1718 if(((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
== 0 ||
1719 ((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
& SPEAKER_RESERVED
)
1724 LeaveCriticalSection(&This
->lock
);
1725 HeapFree(GetProcessHeap(), 0, formats
);
1727 if(hr
== S_FALSE
&& !out
)
1728 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1730 if(hr
== S_FALSE
&& out
) {
1731 closest
->nBlockAlign
=
1732 closest
->nChannels
* closest
->wBitsPerSample
/ 8;
1733 closest
->nAvgBytesPerSec
=
1734 closest
->nBlockAlign
* closest
->nSamplesPerSec
;
1735 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
1736 ((WAVEFORMATEXTENSIBLE
*)closest
)->Samples
.wValidBitsPerSample
= closest
->wBitsPerSample
;
1739 CoTaskMemFree(closest
);
1741 TRACE("returning: %08x\n", hr
);
1745 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1746 WAVEFORMATEX
**pwfx
)
1748 ACImpl
*This
= impl_from_IAudioClient(iface
);
1749 WAVEFORMATEXTENSIBLE
*fmt
;
1750 snd_pcm_format_mask_t
*formats
;
1751 unsigned int max_rate
, max_channels
;
1755 TRACE("(%p)->(%p)\n", This
, pwfx
);
1761 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1763 return E_OUTOFMEMORY
;
1765 formats
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_format_mask_sizeof());
1768 return E_OUTOFMEMORY
;
1771 EnterCriticalSection(&This
->lock
);
1773 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1774 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
1775 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1779 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1781 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1782 if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1783 fmt
->Format
.wBitsPerSample
= 32;
1784 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1785 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1786 fmt
->Format
.wBitsPerSample
= 16;
1787 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1788 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1789 fmt
->Format
.wBitsPerSample
= 8;
1790 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1791 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1792 fmt
->Format
.wBitsPerSample
= 32;
1793 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1794 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1795 fmt
->Format
.wBitsPerSample
= 24;
1796 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1798 ERR("Didn't recognize any available ALSA formats\n");
1799 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1803 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
,
1804 &max_channels
)) < 0){
1805 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1806 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1810 if(max_channels
> 2)
1811 fmt
->Format
.nChannels
= 2;
1813 fmt
->Format
.nChannels
= max_channels
;
1815 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1817 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max_rate
,
1819 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1820 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1824 if(max_rate
>= 48000)
1825 fmt
->Format
.nSamplesPerSec
= 48000;
1826 else if(max_rate
>= 44100)
1827 fmt
->Format
.nSamplesPerSec
= 44100;
1828 else if(max_rate
>= 22050)
1829 fmt
->Format
.nSamplesPerSec
= 22050;
1830 else if(max_rate
>= 11025)
1831 fmt
->Format
.nSamplesPerSec
= 11025;
1832 else if(max_rate
>= 8000)
1833 fmt
->Format
.nSamplesPerSec
= 8000;
1835 ERR("Unknown max rate: %u\n", max_rate
);
1836 hr
= AUDCLNT_E_DEVICE_INVALIDATED
;
1840 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1841 fmt
->Format
.nChannels
) / 8;
1842 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1843 fmt
->Format
.nBlockAlign
;
1845 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1846 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1848 dump_fmt((WAVEFORMATEX
*)fmt
);
1849 *pwfx
= (WAVEFORMATEX
*)fmt
;
1852 LeaveCriticalSection(&This
->lock
);
1855 HeapFree(GetProcessHeap(), 0, formats
);
1860 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1861 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1863 ACImpl
*This
= impl_from_IAudioClient(iface
);
1865 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1867 if(!defperiod
&& !minperiod
)
1871 *defperiod
= DefaultPeriod
;
1873 *minperiod
= MinimumPeriod
;
1878 static BYTE
*remap_channels(ACImpl
*This
, BYTE
*buf
, snd_pcm_uframes_t frames
)
1880 snd_pcm_uframes_t i
;
1882 UINT bytes_per_sample
= This
->fmt
->wBitsPerSample
/ 8;
1884 if(!This
->need_remapping
)
1887 if(!This
->remapping_buf
){
1888 This
->remapping_buf
= HeapAlloc(GetProcessHeap(), 0,
1889 bytes_per_sample
* This
->alsa_channels
* frames
);
1890 This
->remapping_buf_frames
= frames
;
1891 }else if(This
->remapping_buf_frames
< frames
){
1892 This
->remapping_buf
= HeapReAlloc(GetProcessHeap(), 0, This
->remapping_buf
,
1893 bytes_per_sample
* This
->alsa_channels
* frames
);
1894 This
->remapping_buf_frames
= frames
;
1897 snd_pcm_format_set_silence(This
->alsa_format
, This
->remapping_buf
,
1898 frames
* This
->alsa_channels
);
1900 switch(This
->fmt
->wBitsPerSample
){
1902 UINT8
*tgt_buf
, *src_buf
;
1903 tgt_buf
= This
->remapping_buf
;
1905 for(i
= 0; i
< frames
; ++i
){
1906 for(c
= 0; c
< This
->fmt
->nChannels
; ++c
)
1907 tgt_buf
[This
->alsa_channel_map
[c
]] = src_buf
[c
];
1908 tgt_buf
+= This
->alsa_channels
;
1909 src_buf
+= This
->fmt
->nChannels
;
1914 UINT16
*tgt_buf
, *src_buf
;
1915 tgt_buf
= (UINT16
*)This
->remapping_buf
;
1916 src_buf
= (UINT16
*)buf
;
1917 for(i
= 0; i
< frames
; ++i
){
1918 for(c
= 0; c
< This
->fmt
->nChannels
; ++c
)
1919 tgt_buf
[This
->alsa_channel_map
[c
]] = src_buf
[c
];
1920 tgt_buf
+= This
->alsa_channels
;
1921 src_buf
+= This
->fmt
->nChannels
;
1926 UINT32
*tgt_buf
, *src_buf
;
1927 tgt_buf
= (UINT32
*)This
->remapping_buf
;
1928 src_buf
= (UINT32
*)buf
;
1929 for(i
= 0; i
< frames
; ++i
){
1930 for(c
= 0; c
< This
->fmt
->nChannels
; ++c
)
1931 tgt_buf
[This
->alsa_channel_map
[c
]] = src_buf
[c
];
1932 tgt_buf
+= This
->alsa_channels
;
1933 src_buf
+= This
->fmt
->nChannels
;
1938 BYTE
*tgt_buf
, *src_buf
;
1939 tgt_buf
= This
->remapping_buf
;
1941 for(i
= 0; i
< frames
; ++i
){
1942 for(c
= 0; c
< This
->fmt
->nChannels
; ++c
)
1943 memcpy(&tgt_buf
[This
->alsa_channel_map
[c
] * bytes_per_sample
],
1944 &src_buf
[c
* bytes_per_sample
], bytes_per_sample
);
1945 tgt_buf
+= This
->alsa_channels
* bytes_per_sample
;
1946 src_buf
+= This
->fmt
->nChannels
* bytes_per_sample
;
1952 return This
->remapping_buf
;
1955 static snd_pcm_sframes_t
alsa_write_best_effort(snd_pcm_t
*handle
, BYTE
*buf
,
1956 snd_pcm_uframes_t frames
, ACImpl
*This
, BOOL mute
)
1958 snd_pcm_sframes_t written
;
1962 if((err
= snd_pcm_format_set_silence(This
->alsa_format
, buf
,
1963 frames
* This
->fmt
->nChannels
)) < 0)
1964 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1968 buf
= remap_channels(This
, buf
, frames
);
1970 written
= snd_pcm_writei(handle
, buf
, frames
);
1974 if(written
== -EAGAIN
)
1978 WARN("writei failed, recovering: %ld (%s)\n", written
,
1979 snd_strerror(written
));
1981 ret
= snd_pcm_recover(handle
, written
, 0);
1983 WARN("Could not recover: %d (%s)\n", ret
, snd_strerror(ret
));
1987 written
= snd_pcm_writei(handle
, buf
, frames
);
1993 /* The callback and mmdevapi API functions execute concurrently.
1994 * Shared state & life time after Start:
1995 * This constant until _Release
1996 *->pcm_handle likewise
1998 *->alsa_format, hidden_frames likewise
1999 *->local_buffer, bufsize_frames, alsa_bufsize_frames likewise
2000 *->event Read Only, even constant until _Release(!)
2001 *->started Read Only from cb POV, constant if _Stop kills the cb
2003 *->held_frames is the only R/W object.
2004 *->lcl_offs_frames/wri_offs_frames are written by one side exclusively:
2005 * lcl_offs_frames by CaptureClient & write callback
2006 * wri_offs_frames by read callback & RenderClient
2008 static void alsa_write_data(ACImpl
*This
)
2010 snd_pcm_sframes_t written
, in_alsa
;
2011 snd_pcm_uframes_t to_write
, avail
, write_limit
, max_period
;
2014 This
->local_buffer
+ This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2016 /* this call seems to be required to get an accurate snd_pcm_state() */
2017 avail
= snd_pcm_avail_update(This
->pcm_handle
);
2019 if(snd_pcm_state(This
->pcm_handle
) == SND_PCM_STATE_XRUN
||
2020 avail
> This
->alsa_bufsize_frames
){
2021 TRACE("XRun state avail %ld, recovering\n", avail
);
2023 avail
= This
->alsa_bufsize_frames
;
2025 if((err
= snd_pcm_recover(This
->pcm_handle
, -EPIPE
, 1)) < 0)
2026 WARN("snd_pcm_recover failed: %d (%s)\n", err
, snd_strerror(err
));
2028 if((err
= snd_pcm_reset(This
->pcm_handle
)) < 0)
2029 WARN("snd_pcm_reset failed: %d (%s)\n", err
, snd_strerror(err
));
2031 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0)
2032 WARN("snd_pcm_prepare failed: %d (%s)\n", err
, snd_strerror(err
));
2034 TRACE("pad: %ld\n", This
->alsa_bufsize_frames
- avail
);
2036 if(This
->held_frames
== 0)
2039 if(This
->lcl_offs_frames
+ This
->held_frames
> This
->bufsize_frames
)
2040 to_write
= This
->bufsize_frames
- This
->lcl_offs_frames
;
2042 to_write
= This
->held_frames
;
2044 max_period
= max(This
->mmdev_period_frames
, This
->alsa_period_frames
);
2046 /* try to keep 3 ALSA periods or 3 MMDevAPI periods in the ALSA buffer and
2049 in_alsa
= This
->alsa_bufsize_frames
- avail
;
2050 while(in_alsa
+ write_limit
< max_period
* 3)
2051 write_limit
+= max_period
;
2052 if(write_limit
== 0)
2055 to_write
= min(to_write
, write_limit
);
2057 /* Add a lead-in when starting with too few frames to ensure
2058 * continuous rendering. Additional benefit: Force ALSA to start.
2059 * GetPosition continues to reflect the speaker position because
2060 * snd_pcm_delay includes buffered frames in its total delay
2061 * and last_pos_frames prevents moving backwards. */
2062 if(!in_alsa
&& This
->held_frames
< This
->hidden_frames
){
2063 UINT32 s_frames
= This
->hidden_frames
- This
->held_frames
;
2064 BYTE
*silence
= HeapAlloc(GetProcessHeap(), 0,
2065 s_frames
* This
->fmt
->nBlockAlign
);
2068 in_alsa
= alsa_write_best_effort(This
->pcm_handle
,
2069 silence
, s_frames
, This
, TRUE
);
2070 TRACE("lead-in %ld\n", in_alsa
);
2071 HeapFree(GetProcessHeap(), 0, silence
);
2075 WARN("Couldn't allocate lead-in, expect underrun\n");
2078 written
= alsa_write_best_effort(This
->pcm_handle
, buf
, to_write
, This
,
2079 This
->session
->mute
);
2081 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
2085 This
->lcl_offs_frames
+= written
;
2086 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2087 This
->held_frames
-= written
;
2089 if(written
< to_write
){
2090 /* ALSA buffer probably full */
2094 if(This
->held_frames
&& (written
< write_limit
)){
2095 /* wrapped and have some data back at the start to write */
2096 written
= alsa_write_best_effort(This
->pcm_handle
, This
->local_buffer
,
2097 min(This
->held_frames
, write_limit
- written
), This
,
2098 This
->session
->mute
);
2100 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
2104 This
->lcl_offs_frames
+= written
;
2105 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2106 This
->held_frames
-= written
;
2110 static void alsa_read_data(ACImpl
*This
)
2112 snd_pcm_sframes_t nread
;
2113 UINT32 pos
= This
->wri_offs_frames
, limit
= This
->held_frames
;
2115 /* FIXME: Detect overrun and signal DATA_DISCONTINUITY
2116 * How to count overrun frames and report them as position increase? */
2117 limit
= This
->bufsize_frames
- max(limit
, pos
);
2119 nread
= snd_pcm_readi(This
->pcm_handle
,
2120 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, limit
);
2121 TRACE("read %ld from %u limit %u\n", nread
, pos
, limit
);
2125 if(nread
== -EAGAIN
) /* no data yet */
2128 WARN("read failed, recovering: %ld (%s)\n", nread
, snd_strerror(nread
));
2130 ret
= snd_pcm_recover(This
->pcm_handle
, nread
, 0);
2132 WARN("Recover failed: %d (%s)\n", ret
, snd_strerror(ret
));
2136 nread
= snd_pcm_readi(This
->pcm_handle
,
2137 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, limit
);
2139 WARN("read failed: %ld (%s)\n", nread
, snd_strerror(nread
));
2144 if(This
->session
->mute
){
2146 if((err
= snd_pcm_format_set_silence(This
->alsa_format
,
2147 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
,
2149 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
2153 This
->wri_offs_frames
+= nread
;
2154 This
->wri_offs_frames
%= This
->bufsize_frames
;
2155 This
->held_frames
+= nread
;
2158 static void CALLBACK
alsa_push_buffer_data(void *user
, BOOLEAN timer
)
2160 ACImpl
*This
= user
;
2162 EnterCriticalSection(&This
->lock
);
2165 if(This
->dataflow
== eRender
)
2166 alsa_write_data(This
);
2167 else if(This
->dataflow
== eCapture
)
2168 alsa_read_data(This
);
2171 LeaveCriticalSection(&This
->lock
);
2174 SetEvent(This
->event
);
2177 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
2179 ACImpl
*This
= impl_from_IAudioClient(iface
);
2181 TRACE("(%p)\n", This
);
2183 EnterCriticalSection(&This
->lock
);
2186 LeaveCriticalSection(&This
->lock
);
2187 return AUDCLNT_E_NOT_INITIALIZED
;
2190 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
2191 LeaveCriticalSection(&This
->lock
);
2192 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
2196 LeaveCriticalSection(&This
->lock
);
2197 return AUDCLNT_E_NOT_STOPPED
;
2200 if(This
->dataflow
== eCapture
){
2201 /* dump any data that might be leftover in the ALSA capture buffer */
2202 snd_pcm_readi(This
->pcm_handle
, This
->local_buffer
,
2203 This
->bufsize_frames
);
2207 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
, alsa_push_buffer_data
,
2208 This
, 0, This
->mmdev_period_rt
/ 10000, WT_EXECUTEINTIMERTHREAD
)){
2209 LeaveCriticalSection(&This
->lock
);
2210 WARN("Unable to create timer: %u\n", GetLastError());
2211 return E_OUTOFMEMORY
;
2215 This
->started
= TRUE
;
2217 LeaveCriticalSection(&This
->lock
);
2222 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
2224 ACImpl
*This
= impl_from_IAudioClient(iface
);
2226 TRACE("(%p)\n", This
);
2228 EnterCriticalSection(&This
->lock
);
2231 LeaveCriticalSection(&This
->lock
);
2232 return AUDCLNT_E_NOT_INITIALIZED
;
2236 LeaveCriticalSection(&This
->lock
);
2240 This
->started
= FALSE
;
2242 LeaveCriticalSection(&This
->lock
);
2247 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
2249 ACImpl
*This
= impl_from_IAudioClient(iface
);
2251 TRACE("(%p)\n", This
);
2253 EnterCriticalSection(&This
->lock
);
2256 LeaveCriticalSection(&This
->lock
);
2257 return AUDCLNT_E_NOT_INITIALIZED
;
2261 LeaveCriticalSection(&This
->lock
);
2262 return AUDCLNT_E_NOT_STOPPED
;
2265 if(This
->getbuf_last
){
2266 LeaveCriticalSection(&This
->lock
);
2267 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
2270 if(snd_pcm_drop(This
->pcm_handle
) < 0)
2271 WARN("snd_pcm_drop failed\n");
2273 if(snd_pcm_reset(This
->pcm_handle
) < 0)
2274 WARN("snd_pcm_reset failed\n");
2276 if(snd_pcm_prepare(This
->pcm_handle
) < 0)
2277 WARN("snd_pcm_prepare failed\n");
2279 if(This
->dataflow
== eRender
){
2280 This
->written_frames
= 0;
2281 This
->last_pos_frames
= 0;
2283 This
->written_frames
+= This
->held_frames
;
2285 This
->held_frames
= 0;
2286 This
->lcl_offs_frames
= 0;
2287 This
->wri_offs_frames
= 0;
2289 LeaveCriticalSection(&This
->lock
);
2294 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
2297 ACImpl
*This
= impl_from_IAudioClient(iface
);
2299 TRACE("(%p)->(%p)\n", This
, event
);
2302 return E_INVALIDARG
;
2304 EnterCriticalSection(&This
->lock
);
2307 LeaveCriticalSection(&This
->lock
);
2308 return AUDCLNT_E_NOT_INITIALIZED
;
2311 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
2312 LeaveCriticalSection(&This
->lock
);
2313 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
2317 LeaveCriticalSection(&This
->lock
);
2318 FIXME("called twice\n");
2319 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
2322 This
->event
= event
;
2324 LeaveCriticalSection(&This
->lock
);
2329 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
2332 ACImpl
*This
= impl_from_IAudioClient(iface
);
2334 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
2340 EnterCriticalSection(&This
->lock
);
2343 LeaveCriticalSection(&This
->lock
);
2344 return AUDCLNT_E_NOT_INITIALIZED
;
2347 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
2348 if(This
->dataflow
!= eRender
){
2349 LeaveCriticalSection(&This
->lock
);
2350 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
2352 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
2353 *ppv
= &This
->IAudioRenderClient_iface
;
2354 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
2355 if(This
->dataflow
!= eCapture
){
2356 LeaveCriticalSection(&This
->lock
);
2357 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
2359 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
2360 *ppv
= &This
->IAudioCaptureClient_iface
;
2361 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
2362 IAudioClock_AddRef(&This
->IAudioClock_iface
);
2363 *ppv
= &This
->IAudioClock_iface
;
2364 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
2365 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
2366 *ppv
= &This
->IAudioStreamVolume_iface
;
2367 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
2368 if(!This
->session_wrapper
){
2369 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
2370 if(!This
->session_wrapper
){
2371 LeaveCriticalSection(&This
->lock
);
2372 return E_OUTOFMEMORY
;
2375 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
2377 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
2378 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
2379 if(!This
->session_wrapper
){
2380 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
2381 if(!This
->session_wrapper
){
2382 LeaveCriticalSection(&This
->lock
);
2383 return E_OUTOFMEMORY
;
2386 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
2388 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
2389 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
2390 if(!This
->session_wrapper
){
2391 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
2392 if(!This
->session_wrapper
){
2393 LeaveCriticalSection(&This
->lock
);
2394 return E_OUTOFMEMORY
;
2397 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
2399 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
2403 LeaveCriticalSection(&This
->lock
);
2407 LeaveCriticalSection(&This
->lock
);
2409 FIXME("stub %s\n", debugstr_guid(riid
));
2410 return E_NOINTERFACE
;
2413 static const IAudioClientVtbl AudioClient_Vtbl
=
2415 AudioClient_QueryInterface
,
2417 AudioClient_Release
,
2418 AudioClient_Initialize
,
2419 AudioClient_GetBufferSize
,
2420 AudioClient_GetStreamLatency
,
2421 AudioClient_GetCurrentPadding
,
2422 AudioClient_IsFormatSupported
,
2423 AudioClient_GetMixFormat
,
2424 AudioClient_GetDevicePeriod
,
2428 AudioClient_SetEventHandle
,
2429 AudioClient_GetService
2432 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
2433 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
2435 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2436 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2442 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2443 IsEqualIID(riid
, &IID_IAudioRenderClient
))
2445 else if(IsEqualIID(riid
, &IID_IMarshal
))
2446 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
2449 IUnknown_AddRef((IUnknown
*)*ppv
);
2453 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2454 return E_NOINTERFACE
;
2457 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
2459 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2460 return AudioClient_AddRef(&This
->IAudioClient_iface
);
2463 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
2465 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2466 return AudioClient_Release(&This
->IAudioClient_iface
);
2469 static void silence_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 frames
)
2471 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
2472 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
2473 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
2474 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
2475 This
->fmt
->wBitsPerSample
== 8)
2476 memset(buffer
, 128, frames
* This
->fmt
->nBlockAlign
);
2478 memset(buffer
, 0, frames
* This
->fmt
->nBlockAlign
);
2481 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
2482 UINT32 frames
, BYTE
**data
)
2484 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2487 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
2493 EnterCriticalSection(&This
->lock
);
2495 if(This
->getbuf_last
){
2496 LeaveCriticalSection(&This
->lock
);
2497 return AUDCLNT_E_OUT_OF_ORDER
;
2501 LeaveCriticalSection(&This
->lock
);
2505 /* held_frames == GetCurrentPadding_nolock(); */
2506 if(This
->held_frames
+ frames
> This
->bufsize_frames
){
2507 LeaveCriticalSection(&This
->lock
);
2508 return AUDCLNT_E_BUFFER_TOO_LARGE
;
2511 write_pos
= This
->wri_offs_frames
;
2512 if(write_pos
+ frames
> This
->bufsize_frames
){
2513 if(This
->tmp_buffer_frames
< frames
){
2514 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
2515 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2516 frames
* This
->fmt
->nBlockAlign
);
2517 if(!This
->tmp_buffer
){
2518 LeaveCriticalSection(&This
->lock
);
2519 return E_OUTOFMEMORY
;
2521 This
->tmp_buffer_frames
= frames
;
2523 *data
= This
->tmp_buffer
;
2524 This
->getbuf_last
= -frames
;
2526 *data
= This
->local_buffer
+ write_pos
* This
->fmt
->nBlockAlign
;
2527 This
->getbuf_last
= frames
;
2530 silence_buffer(This
, *data
, frames
);
2532 LeaveCriticalSection(&This
->lock
);
2537 static void alsa_wrap_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 written_frames
)
2539 snd_pcm_uframes_t write_offs_frames
= This
->wri_offs_frames
;
2540 UINT32 write_offs_bytes
= write_offs_frames
* This
->fmt
->nBlockAlign
;
2541 snd_pcm_uframes_t chunk_frames
= This
->bufsize_frames
- write_offs_frames
;
2542 UINT32 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
2543 UINT32 written_bytes
= written_frames
* This
->fmt
->nBlockAlign
;
2545 if(written_bytes
<= chunk_bytes
){
2546 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, written_bytes
);
2548 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, chunk_bytes
);
2549 memcpy(This
->local_buffer
, buffer
+ chunk_bytes
,
2550 written_bytes
- chunk_bytes
);
2554 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
2555 IAudioRenderClient
*iface
, UINT32 written_frames
, DWORD flags
)
2557 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2560 TRACE("(%p)->(%u, %x)\n", This
, written_frames
, flags
);
2562 EnterCriticalSection(&This
->lock
);
2564 if(!written_frames
){
2565 This
->getbuf_last
= 0;
2566 LeaveCriticalSection(&This
->lock
);
2570 if(!This
->getbuf_last
){
2571 LeaveCriticalSection(&This
->lock
);
2572 return AUDCLNT_E_OUT_OF_ORDER
;
2575 if(written_frames
> (This
->getbuf_last
>= 0 ? This
->getbuf_last
: -This
->getbuf_last
)){
2576 LeaveCriticalSection(&This
->lock
);
2577 return AUDCLNT_E_INVALID_SIZE
;
2580 if(This
->getbuf_last
>= 0)
2581 buffer
= This
->local_buffer
+ This
->wri_offs_frames
* This
->fmt
->nBlockAlign
;
2583 buffer
= This
->tmp_buffer
;
2585 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
)
2586 silence_buffer(This
, buffer
, written_frames
);
2588 if(This
->getbuf_last
< 0)
2589 alsa_wrap_buffer(This
, buffer
, written_frames
);
2591 This
->wri_offs_frames
+= written_frames
;
2592 This
->wri_offs_frames
%= This
->bufsize_frames
;
2593 This
->held_frames
+= written_frames
;
2594 This
->written_frames
+= written_frames
;
2595 This
->getbuf_last
= 0;
2597 LeaveCriticalSection(&This
->lock
);
2602 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
2603 AudioRenderClient_QueryInterface
,
2604 AudioRenderClient_AddRef
,
2605 AudioRenderClient_Release
,
2606 AudioRenderClient_GetBuffer
,
2607 AudioRenderClient_ReleaseBuffer
2610 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
2611 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
2613 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2614 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2620 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2621 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
2623 else if(IsEqualIID(riid
, &IID_IMarshal
))
2624 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
2627 IUnknown_AddRef((IUnknown
*)*ppv
);
2631 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2632 return E_NOINTERFACE
;
2635 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
2637 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2638 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2641 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
2643 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2644 return IAudioClient_Release(&This
->IAudioClient_iface
);
2647 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
2648 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
2651 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2653 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
2656 if(!data
|| !frames
|| !flags
)
2659 EnterCriticalSection(&This
->lock
);
2661 if(This
->getbuf_last
){
2662 LeaveCriticalSection(&This
->lock
);
2663 return AUDCLNT_E_OUT_OF_ORDER
;
2666 /* hr = GetNextPacketSize(iface, frames); */
2667 if(This
->held_frames
< This
->mmdev_period_frames
){
2669 LeaveCriticalSection(&This
->lock
);
2670 return AUDCLNT_S_BUFFER_EMPTY
;
2672 *frames
= This
->mmdev_period_frames
;
2674 if(This
->lcl_offs_frames
+ *frames
> This
->bufsize_frames
){
2675 UINT32 chunk_bytes
, offs_bytes
, frames_bytes
;
2676 if(This
->tmp_buffer_frames
< *frames
){
2677 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
2678 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2679 *frames
* This
->fmt
->nBlockAlign
);
2680 if(!This
->tmp_buffer
){
2681 LeaveCriticalSection(&This
->lock
);
2682 return E_OUTOFMEMORY
;
2684 This
->tmp_buffer_frames
= *frames
;
2687 *data
= This
->tmp_buffer
;
2688 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) *
2689 This
->fmt
->nBlockAlign
;
2690 offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2691 frames_bytes
= *frames
* This
->fmt
->nBlockAlign
;
2692 memcpy(This
->tmp_buffer
, This
->local_buffer
+ offs_bytes
, chunk_bytes
);
2693 memcpy(This
->tmp_buffer
+ chunk_bytes
, This
->local_buffer
,
2694 frames_bytes
- chunk_bytes
);
2696 *data
= This
->local_buffer
+
2697 This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2699 This
->getbuf_last
= *frames
;
2703 *devpos
= This
->written_frames
;
2704 if(qpcpos
){ /* fixme: qpc of recording time */
2705 LARGE_INTEGER stamp
, freq
;
2706 QueryPerformanceCounter(&stamp
);
2707 QueryPerformanceFrequency(&freq
);
2708 *qpcpos
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2711 LeaveCriticalSection(&This
->lock
);
2713 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
2716 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2717 IAudioCaptureClient
*iface
, UINT32 done
)
2719 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2721 TRACE("(%p)->(%u)\n", This
, done
);
2723 EnterCriticalSection(&This
->lock
);
2726 This
->getbuf_last
= 0;
2727 LeaveCriticalSection(&This
->lock
);
2731 if(!This
->getbuf_last
){
2732 LeaveCriticalSection(&This
->lock
);
2733 return AUDCLNT_E_OUT_OF_ORDER
;
2736 if(This
->getbuf_last
!= done
){
2737 LeaveCriticalSection(&This
->lock
);
2738 return AUDCLNT_E_INVALID_SIZE
;
2741 This
->written_frames
+= done
;
2742 This
->held_frames
-= done
;
2743 This
->lcl_offs_frames
+= done
;
2744 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2745 This
->getbuf_last
= 0;
2747 LeaveCriticalSection(&This
->lock
);
2752 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2753 IAudioCaptureClient
*iface
, UINT32
*frames
)
2755 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2757 TRACE("(%p)->(%p)\n", This
, frames
);
2762 EnterCriticalSection(&This
->lock
);
2764 *frames
= This
->held_frames
< This
->mmdev_period_frames
? 0 : This
->mmdev_period_frames
;
2766 LeaveCriticalSection(&This
->lock
);
2771 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2773 AudioCaptureClient_QueryInterface
,
2774 AudioCaptureClient_AddRef
,
2775 AudioCaptureClient_Release
,
2776 AudioCaptureClient_GetBuffer
,
2777 AudioCaptureClient_ReleaseBuffer
,
2778 AudioCaptureClient_GetNextPacketSize
2781 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2782 REFIID riid
, void **ppv
)
2784 ACImpl
*This
= impl_from_IAudioClock(iface
);
2786 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2792 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2794 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2795 *ppv
= &This
->IAudioClock2_iface
;
2797 IUnknown_AddRef((IUnknown
*)*ppv
);
2801 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2802 return E_NOINTERFACE
;
2805 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2807 ACImpl
*This
= impl_from_IAudioClock(iface
);
2808 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2811 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2813 ACImpl
*This
= impl_from_IAudioClock(iface
);
2814 return IAudioClient_Release(&This
->IAudioClient_iface
);
2817 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2819 ACImpl
*This
= impl_from_IAudioClock(iface
);
2821 TRACE("(%p)->(%p)\n", This
, freq
);
2823 if(This
->share
== AUDCLNT_SHAREMODE_SHARED
)
2824 *freq
= (UINT64
)This
->fmt
->nSamplesPerSec
* This
->fmt
->nBlockAlign
;
2826 *freq
= This
->fmt
->nSamplesPerSec
;
2831 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2834 ACImpl
*This
= impl_from_IAudioClock(iface
);
2835 UINT64 written_frames
, position
;
2838 snd_pcm_state_t alsa_state
;
2839 snd_pcm_uframes_t avail_frames
;
2840 snd_pcm_sframes_t delay_frames
;
2842 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2847 EnterCriticalSection(&This
->lock
);
2849 /* call required to get accurate snd_pcm_state() */
2850 avail_frames
= snd_pcm_avail_update(This
->pcm_handle
);
2851 alsa_state
= snd_pcm_state(This
->pcm_handle
);
2852 written_frames
= This
->written_frames
;
2853 held_frames
= This
->held_frames
;
2855 err
= snd_pcm_delay(This
->pcm_handle
, &delay_frames
);
2857 /* old Pulse, shortly after start */
2858 WARN("snd_pcm_delay failed in state %u: %d (%s)\n", alsa_state
, err
, snd_strerror(err
));
2861 if(This
->dataflow
== eRender
){
2862 position
= written_frames
- held_frames
; /* maximum */
2863 if(!This
->started
|| alsa_state
> SND_PCM_STATE_RUNNING
)
2864 ; /* mmdevapi stopped or ALSA underrun: pretend everything was played */
2865 else if(err
<0 || delay_frames
> position
- This
->last_pos_frames
)
2866 /* Pulse bug: past underrun, despite recovery, avail_frames & delay
2867 * may be larger than alsa_bufsize_frames, as if cumulating frames. */
2868 /* Pulse bug: EIO(-5) shortly after starting: nothing played */
2869 position
= This
->last_pos_frames
;
2870 else if(delay_frames
> 0)
2871 position
-= delay_frames
;
2873 position
= written_frames
+ held_frames
;
2875 /* ensure monotic growth */
2876 This
->last_pos_frames
= position
;
2878 LeaveCriticalSection(&This
->lock
);
2880 TRACE("frames written: %u, held: %u, avail: %ld, delay: %ld state %d, pos: %u\n",
2881 (UINT32
)(written_frames
%1000000000), held_frames
,
2882 avail_frames
, delay_frames
, alsa_state
, (UINT32
)(position
%1000000000));
2883 if(This
->share
== AUDCLNT_SHAREMODE_SHARED
)
2884 *pos
= position
* This
->fmt
->nBlockAlign
;
2889 LARGE_INTEGER stamp
, freq
;
2890 QueryPerformanceCounter(&stamp
);
2891 QueryPerformanceFrequency(&freq
);
2892 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2898 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2901 ACImpl
*This
= impl_from_IAudioClock(iface
);
2903 TRACE("(%p)->(%p)\n", This
, chars
);
2908 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2913 static const IAudioClockVtbl AudioClock_Vtbl
=
2915 AudioClock_QueryInterface
,
2918 AudioClock_GetFrequency
,
2919 AudioClock_GetPosition
,
2920 AudioClock_GetCharacteristics
2923 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2924 REFIID riid
, void **ppv
)
2926 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2927 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2930 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2932 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2933 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2936 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2938 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2939 return IAudioClient_Release(&This
->IAudioClient_iface
);
2942 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2943 UINT64
*pos
, UINT64
*qpctime
)
2945 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2947 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2952 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2954 AudioClock2_QueryInterface
,
2956 AudioClock2_Release
,
2957 AudioClock2_GetDevicePosition
2960 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2962 AudioSessionWrapper
*ret
;
2964 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2965 sizeof(AudioSessionWrapper
));
2969 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2970 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2971 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2975 ret
->client
= client
;
2977 ret
->session
= client
->session
;
2978 AudioClient_AddRef(&client
->IAudioClient_iface
);
2984 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2985 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2987 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2993 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2994 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2995 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2998 IUnknown_AddRef((IUnknown
*)*ppv
);
3002 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3003 return E_NOINTERFACE
;
3006 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
3008 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3010 ref
= InterlockedIncrement(&This
->ref
);
3011 TRACE("(%p) Refcount now %u\n", This
, ref
);
3015 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
3017 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3019 ref
= InterlockedDecrement(&This
->ref
);
3020 TRACE("(%p) Refcount now %u\n", This
, ref
);
3023 EnterCriticalSection(&This
->client
->lock
);
3024 This
->client
->session_wrapper
= NULL
;
3025 LeaveCriticalSection(&This
->client
->lock
);
3026 AudioClient_Release(&This
->client
->IAudioClient_iface
);
3028 HeapFree(GetProcessHeap(), 0, This
);
3033 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
3034 AudioSessionState
*state
)
3036 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3039 TRACE("(%p)->(%p)\n", This
, state
);
3042 return NULL_PTR_ERR
;
3044 EnterCriticalSection(&g_sessions_lock
);
3046 if(list_empty(&This
->session
->clients
)){
3047 *state
= AudioSessionStateExpired
;
3048 LeaveCriticalSection(&g_sessions_lock
);
3052 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
3053 EnterCriticalSection(&client
->lock
);
3054 if(client
->started
){
3055 *state
= AudioSessionStateActive
;
3056 LeaveCriticalSection(&client
->lock
);
3057 LeaveCriticalSection(&g_sessions_lock
);
3060 LeaveCriticalSection(&client
->lock
);
3063 LeaveCriticalSection(&g_sessions_lock
);
3065 *state
= AudioSessionStateInactive
;
3070 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
3071 IAudioSessionControl2
*iface
, WCHAR
**name
)
3073 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3075 FIXME("(%p)->(%p) - stub\n", This
, name
);
3080 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
3081 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
3083 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3085 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
3090 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
3091 IAudioSessionControl2
*iface
, WCHAR
**path
)
3093 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3095 FIXME("(%p)->(%p) - stub\n", This
, path
);
3100 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
3101 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
3103 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3105 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
3110 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
3111 IAudioSessionControl2
*iface
, GUID
*group
)
3113 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3115 FIXME("(%p)->(%p) - stub\n", This
, group
);
3120 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
3121 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
3123 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3125 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
3126 debugstr_guid(session
));
3131 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
3132 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
3134 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3136 FIXME("(%p)->(%p) - stub\n", This
, events
);
3141 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
3142 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
3144 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3146 FIXME("(%p)->(%p) - stub\n", This
, events
);
3151 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
3152 IAudioSessionControl2
*iface
, WCHAR
**id
)
3154 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3156 FIXME("(%p)->(%p) - stub\n", This
, id
);
3161 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
3162 IAudioSessionControl2
*iface
, WCHAR
**id
)
3164 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3166 FIXME("(%p)->(%p) - stub\n", This
, id
);
3171 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
3172 IAudioSessionControl2
*iface
, DWORD
*pid
)
3174 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3176 TRACE("(%p)->(%p)\n", This
, pid
);
3181 *pid
= GetCurrentProcessId();
3186 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
3187 IAudioSessionControl2
*iface
)
3189 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3191 TRACE("(%p)\n", This
);
3196 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
3197 IAudioSessionControl2
*iface
, BOOL optout
)
3199 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
3201 TRACE("(%p)->(%d)\n", This
, optout
);
3206 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
3208 AudioSessionControl_QueryInterface
,
3209 AudioSessionControl_AddRef
,
3210 AudioSessionControl_Release
,
3211 AudioSessionControl_GetState
,
3212 AudioSessionControl_GetDisplayName
,
3213 AudioSessionControl_SetDisplayName
,
3214 AudioSessionControl_GetIconPath
,
3215 AudioSessionControl_SetIconPath
,
3216 AudioSessionControl_GetGroupingParam
,
3217 AudioSessionControl_SetGroupingParam
,
3218 AudioSessionControl_RegisterAudioSessionNotification
,
3219 AudioSessionControl_UnregisterAudioSessionNotification
,
3220 AudioSessionControl_GetSessionIdentifier
,
3221 AudioSessionControl_GetSessionInstanceIdentifier
,
3222 AudioSessionControl_GetProcessId
,
3223 AudioSessionControl_IsSystemSoundsSession
,
3224 AudioSessionControl_SetDuckingPreference
3227 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
3228 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
3230 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3236 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3237 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
3240 IUnknown_AddRef((IUnknown
*)*ppv
);
3244 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3245 return E_NOINTERFACE
;
3248 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
3250 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3251 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
3254 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
3256 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3257 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
3260 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
3261 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
3263 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3264 AudioSession
*session
= This
->session
;
3266 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
3268 if(level
< 0.f
|| level
> 1.f
)
3269 return E_INVALIDARG
;
3272 FIXME("Notifications not supported yet\n");
3274 TRACE("ALSA does not support volume control\n");
3276 EnterCriticalSection(&session
->lock
);
3278 session
->master_vol
= level
;
3280 LeaveCriticalSection(&session
->lock
);
3285 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
3286 ISimpleAudioVolume
*iface
, float *level
)
3288 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3289 AudioSession
*session
= This
->session
;
3291 TRACE("(%p)->(%p)\n", session
, level
);
3294 return NULL_PTR_ERR
;
3296 *level
= session
->master_vol
;
3301 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
3302 BOOL mute
, const GUID
*context
)
3304 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3305 AudioSession
*session
= This
->session
;
3307 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
3310 FIXME("Notifications not supported yet\n");
3312 session
->mute
= mute
;
3317 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
3320 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3321 AudioSession
*session
= This
->session
;
3323 TRACE("(%p)->(%p)\n", session
, mute
);
3326 return NULL_PTR_ERR
;
3328 *mute
= session
->mute
;
3333 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
3335 SimpleAudioVolume_QueryInterface
,
3336 SimpleAudioVolume_AddRef
,
3337 SimpleAudioVolume_Release
,
3338 SimpleAudioVolume_SetMasterVolume
,
3339 SimpleAudioVolume_GetMasterVolume
,
3340 SimpleAudioVolume_SetMute
,
3341 SimpleAudioVolume_GetMute
3344 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
3345 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
3347 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3353 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3354 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
3357 IUnknown_AddRef((IUnknown
*)*ppv
);
3361 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3362 return E_NOINTERFACE
;
3365 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
3367 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3368 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
3371 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
3373 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3374 return IAudioClient_Release(&This
->IAudioClient_iface
);
3377 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
3378 IAudioStreamVolume
*iface
, UINT32
*out
)
3380 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3382 TRACE("(%p)->(%p)\n", This
, out
);
3387 *out
= This
->fmt
->nChannels
;
3392 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
3393 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
3395 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3397 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
3399 if(level
< 0.f
|| level
> 1.f
)
3400 return E_INVALIDARG
;
3402 if(index
>= This
->fmt
->nChannels
)
3403 return E_INVALIDARG
;
3405 TRACE("ALSA does not support volume control\n");
3407 EnterCriticalSection(&This
->lock
);
3409 This
->vols
[index
] = level
;
3411 LeaveCriticalSection(&This
->lock
);
3416 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
3417 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
3419 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3421 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
3426 if(index
>= This
->fmt
->nChannels
)
3427 return E_INVALIDARG
;
3429 *level
= This
->vols
[index
];
3434 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
3435 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
3437 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3440 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
3445 if(count
!= This
->fmt
->nChannels
)
3446 return E_INVALIDARG
;
3448 TRACE("ALSA does not support volume control\n");
3450 EnterCriticalSection(&This
->lock
);
3452 for(i
= 0; i
< count
; ++i
)
3453 This
->vols
[i
] = levels
[i
];
3455 LeaveCriticalSection(&This
->lock
);
3460 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
3461 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
3463 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3466 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
3471 if(count
!= This
->fmt
->nChannels
)
3472 return E_INVALIDARG
;
3474 EnterCriticalSection(&This
->lock
);
3476 for(i
= 0; i
< count
; ++i
)
3477 levels
[i
] = This
->vols
[i
];
3479 LeaveCriticalSection(&This
->lock
);
3484 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
3486 AudioStreamVolume_QueryInterface
,
3487 AudioStreamVolume_AddRef
,
3488 AudioStreamVolume_Release
,
3489 AudioStreamVolume_GetChannelCount
,
3490 AudioStreamVolume_SetChannelVolume
,
3491 AudioStreamVolume_GetChannelVolume
,
3492 AudioStreamVolume_SetAllVolumes
,
3493 AudioStreamVolume_GetAllVolumes
3496 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
3497 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
3499 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3505 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3506 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
3509 IUnknown_AddRef((IUnknown
*)*ppv
);
3513 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3514 return E_NOINTERFACE
;
3517 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
3519 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3520 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
3523 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
3525 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3526 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
3529 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
3530 IChannelAudioVolume
*iface
, UINT32
*out
)
3532 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3533 AudioSession
*session
= This
->session
;
3535 TRACE("(%p)->(%p)\n", session
, out
);
3538 return NULL_PTR_ERR
;
3540 *out
= session
->channel_count
;
3545 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
3546 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
3547 const GUID
*context
)
3549 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3550 AudioSession
*session
= This
->session
;
3552 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
3553 wine_dbgstr_guid(context
));
3555 if(level
< 0.f
|| level
> 1.f
)
3556 return E_INVALIDARG
;
3558 if(index
>= session
->channel_count
)
3559 return E_INVALIDARG
;
3562 FIXME("Notifications not supported yet\n");
3564 TRACE("ALSA does not support volume control\n");
3566 EnterCriticalSection(&session
->lock
);
3568 session
->channel_vols
[index
] = level
;
3570 LeaveCriticalSection(&session
->lock
);
3575 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
3576 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
3578 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3579 AudioSession
*session
= This
->session
;
3581 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
3584 return NULL_PTR_ERR
;
3586 if(index
>= session
->channel_count
)
3587 return E_INVALIDARG
;
3589 *level
= session
->channel_vols
[index
];
3594 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
3595 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
3596 const GUID
*context
)
3598 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3599 AudioSession
*session
= This
->session
;
3602 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
3603 wine_dbgstr_guid(context
));
3606 return NULL_PTR_ERR
;
3608 if(count
!= session
->channel_count
)
3609 return E_INVALIDARG
;
3612 FIXME("Notifications not supported yet\n");
3614 TRACE("ALSA does not support volume control\n");
3616 EnterCriticalSection(&session
->lock
);
3618 for(i
= 0; i
< count
; ++i
)
3619 session
->channel_vols
[i
] = levels
[i
];
3621 LeaveCriticalSection(&session
->lock
);
3626 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
3627 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
3629 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3630 AudioSession
*session
= This
->session
;
3633 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
3636 return NULL_PTR_ERR
;
3638 if(count
!= session
->channel_count
)
3639 return E_INVALIDARG
;
3641 for(i
= 0; i
< count
; ++i
)
3642 levels
[i
] = session
->channel_vols
[i
];
3647 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
3649 ChannelAudioVolume_QueryInterface
,
3650 ChannelAudioVolume_AddRef
,
3651 ChannelAudioVolume_Release
,
3652 ChannelAudioVolume_GetChannelCount
,
3653 ChannelAudioVolume_SetChannelVolume
,
3654 ChannelAudioVolume_GetChannelVolume
,
3655 ChannelAudioVolume_SetAllVolumes
,
3656 ChannelAudioVolume_GetAllVolumes
3659 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
3660 REFIID riid
, void **ppv
)
3662 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3668 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3669 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
3670 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
3673 IUnknown_AddRef((IUnknown
*)*ppv
);
3677 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3678 return E_NOINTERFACE
;
3681 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
3683 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3685 ref
= InterlockedIncrement(&This
->ref
);
3686 TRACE("(%p) Refcount now %u\n", This
, ref
);
3690 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
3692 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3694 ref
= InterlockedDecrement(&This
->ref
);
3695 TRACE("(%p) Refcount now %u\n", This
, ref
);
3697 HeapFree(GetProcessHeap(), 0, This
);
3701 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3702 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3703 IAudioSessionControl
**out
)
3705 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3706 AudioSession
*session
;
3707 AudioSessionWrapper
*wrapper
;
3710 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3713 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3717 wrapper
= AudioSessionWrapper_Create(NULL
);
3719 return E_OUTOFMEMORY
;
3721 wrapper
->session
= session
;
3723 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3728 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3729 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3730 ISimpleAudioVolume
**out
)
3732 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3733 AudioSession
*session
;
3734 AudioSessionWrapper
*wrapper
;
3737 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3740 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3744 wrapper
= AudioSessionWrapper_Create(NULL
);
3746 return E_OUTOFMEMORY
;
3748 wrapper
->session
= session
;
3750 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3755 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3756 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3758 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3759 FIXME("(%p)->(%p) - stub\n", This
, out
);
3763 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3764 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3766 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3767 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3771 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3772 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3774 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3775 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3779 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3780 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3781 IAudioVolumeDuckNotification
*notification
)
3783 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3784 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3788 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3789 IAudioSessionManager2
*iface
,
3790 IAudioVolumeDuckNotification
*notification
)
3792 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3793 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3797 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3799 AudioSessionManager_QueryInterface
,
3800 AudioSessionManager_AddRef
,
3801 AudioSessionManager_Release
,
3802 AudioSessionManager_GetAudioSessionControl
,
3803 AudioSessionManager_GetSimpleAudioVolume
,
3804 AudioSessionManager_GetSessionEnumerator
,
3805 AudioSessionManager_RegisterSessionNotification
,
3806 AudioSessionManager_UnregisterSessionNotification
,
3807 AudioSessionManager_RegisterDuckNotification
,
3808 AudioSessionManager_UnregisterDuckNotification
3811 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3812 IAudioSessionManager2
**out
)
3816 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3818 return E_OUTOFMEMORY
;
3820 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3821 This
->device
= device
;
3824 *out
= &This
->IAudioSessionManager2_iface
;