2 * Copyright 2011 Andrew Eikum for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
35 #include <sys/soundcard.h>
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.h"
46 #include "mmdeviceapi.h"
52 #include "endpointvolume.h"
53 #include "audiopolicy.h"
54 #include "audioclient.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(oss
);
58 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
60 static const REFERENCE_TIME DefaultPeriod
= 200000;
61 static const REFERENCE_TIME MinimumPeriod
= 100000;
64 typedef struct ACImpl ACImpl
;
66 typedef struct _AudioSession
{
77 CRITICAL_SECTION lock
;
82 typedef struct _AudioSessionWrapper
{
83 IAudioSessionControl2 IAudioSessionControl2_iface
;
84 IChannelAudioVolume IChannelAudioVolume_iface
;
85 ISimpleAudioVolume ISimpleAudioVolume_iface
;
90 AudioSession
*session
;
91 } AudioSessionWrapper
;
94 IAudioClient IAudioClient_iface
;
95 IAudioRenderClient IAudioRenderClient_iface
;
96 IAudioCaptureClient IAudioCaptureClient_iface
;
97 IAudioClock IAudioClock_iface
;
98 IAudioClock2 IAudioClock2_iface
;
99 IAudioStreamVolume IAudioStreamVolume_iface
;
104 IUnknown
*pUnkFTMarshal
;
110 AUDCLNT_SHAREMODE share
;
116 char devnode
[OSS_DEVNODE_SIZE
];
118 BOOL initted
, playing
;
119 UINT64 written_frames
, last_pos_frames
;
120 UINT32 period_us
, period_frames
, bufsize_frames
, held_frames
, tmp_buffer_frames
;
121 UINT32 oss_bufsize_bytes
, lcl_offs_frames
; /* offs into local_buffer where valid data starts */
123 BYTE
*local_buffer
, *tmp_buffer
;
124 LONG32 getbuf_last
; /* <0 when using tmp_buffer */
127 CRITICAL_SECTION lock
;
129 AudioSession
*session
;
130 AudioSessionWrapper
*session_wrapper
;
135 typedef struct _SessionMgr
{
136 IAudioSessionManager2 IAudioSessionManager2_iface
;
143 typedef struct _OSSDevice
{
145 char devnode
[OSS_DEVNODE_SIZE
];
151 static struct list g_devices
= LIST_INIT(g_devices
);
153 static const WCHAR drv_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
154 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
155 'w','i','n','e','o','s','s','.','d','r','v',0};
156 static const WCHAR drv_key_devicesW
[] = {'S','o','f','t','w','a','r','e','\\',
157 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
158 'w','i','n','e','o','s','s','.','d','r','v','\\','d','e','v','i','c','e','s',0};
159 static const WCHAR guidW
[] = {'g','u','i','d',0};
161 static HANDLE g_timer_q
;
163 static CRITICAL_SECTION g_sessions_lock
;
164 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug
=
166 0, 0, &g_sessions_lock
,
167 { &g_sessions_lock_debug
.ProcessLocksList
, &g_sessions_lock_debug
.ProcessLocksList
},
168 0, 0, { (DWORD_PTR
)(__FILE__
": g_sessions_lock") }
170 static CRITICAL_SECTION g_sessions_lock
= { &g_sessions_lock_debug
, -1, 0, 0, 0, 0 };
171 static struct list g_sessions
= LIST_INIT(g_sessions
);
173 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
175 static const IAudioClientVtbl AudioClient_Vtbl
;
176 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
177 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
178 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
179 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
180 static const IAudioClockVtbl AudioClock_Vtbl
;
181 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
182 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
183 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
184 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
186 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
188 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
191 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
193 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
196 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
198 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
201 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
203 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
206 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
208 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
211 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
213 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
216 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
218 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
221 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
223 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
226 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
228 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
231 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
233 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
236 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
240 case DLL_PROCESS_ATTACH
:
241 g_timer_q
= CreateTimerQueue();
246 case DLL_PROCESS_DETACH
:
249 OSSDevice
*iter
, *iter2
;
251 DeleteCriticalSection(&g_sessions_lock
);
253 LIST_FOR_EACH_ENTRY_SAFE(iter
, iter2
, &g_devices
, OSSDevice
, entry
){
254 HeapFree(GetProcessHeap(), 0, iter
);
262 /* From <dlls/mmdevapi/mmdevapi.h> */
263 enum DriverPriority
{
264 Priority_Unavailable
= 0,
270 int WINAPI
AUDDRV_GetPriority(void)
275 /* Attempt to determine if we are running on OSS or ALSA's OSS
276 * compatibility layer. There is no official way to do that, so just check
277 * for validity as best as possible, without rejecting valid OSS
278 * implementations. */
280 mixer_fd
= open("/dev/mixer", O_RDONLY
, 0);
282 TRACE("Priority_Unavailable: open failed\n");
283 return Priority_Unavailable
;
286 sysinfo
.version
[0] = 0xFF;
287 sysinfo
.versionnum
= ~0;
288 if(ioctl(mixer_fd
, SNDCTL_SYSINFO
, &sysinfo
) < 0){
289 TRACE("Priority_Unavailable: ioctl failed\n");
291 return Priority_Unavailable
;
296 if(sysinfo
.version
[0] < '4' || sysinfo
.version
[0] > '9'){
297 TRACE("Priority_Low: sysinfo.version[0]: %x\n", sysinfo
.version
[0]);
300 if(sysinfo
.versionnum
& 0x80000000){
301 TRACE("Priority_Low: sysinfo.versionnum: %x\n", sysinfo
.versionnum
);
305 TRACE("Priority_Preferred: Seems like valid OSS!\n");
307 return Priority_Preferred
;
310 static void set_device_guid(EDataFlow flow
, HKEY drv_key
, const WCHAR
*key_name
,
318 lr
= RegCreateKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, NULL
, 0, KEY_WRITE
,
319 NULL
, &drv_key
, NULL
);
320 if(lr
!= ERROR_SUCCESS
){
321 ERR("RegCreateKeyEx(drv_key) failed: %u\n", lr
);
327 lr
= RegCreateKeyExW(drv_key
, key_name
, 0, NULL
, 0, KEY_WRITE
,
329 if(lr
!= ERROR_SUCCESS
){
330 ERR("RegCreateKeyEx(%s) failed: %u\n", wine_dbgstr_w(key_name
), lr
);
334 lr
= RegSetValueExW(key
, guidW
, 0, REG_BINARY
, (BYTE
*)guid
,
336 if(lr
!= ERROR_SUCCESS
)
337 ERR("RegSetValueEx(%s\\guid) failed: %u\n", wine_dbgstr_w(key_name
), lr
);
342 RegCloseKey(drv_key
);
345 static void get_device_guid(EDataFlow flow
, const char *device
, GUID
*guid
)
347 HKEY key
= NULL
, dev_key
;
348 DWORD type
, size
= sizeof(*guid
);
356 MultiByteToWideChar(CP_UNIXCP
, 0, device
, -1, key_name
+ 2,
357 (sizeof(key_name
) / sizeof(*key_name
)) - 2);
359 if(RegOpenKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, KEY_WRITE
|KEY_READ
, &key
) == ERROR_SUCCESS
){
360 if(RegOpenKeyExW(key
, key_name
, 0, KEY_READ
, &dev_key
) == ERROR_SUCCESS
){
361 if(RegQueryValueExW(dev_key
, guidW
, 0, &type
,
362 (BYTE
*)guid
, &size
) == ERROR_SUCCESS
){
363 if(type
== REG_BINARY
){
364 RegCloseKey(dev_key
);
368 ERR("Invalid type for device %s GUID: %u; ignoring and overwriting\n",
369 wine_dbgstr_w(key_name
), type
);
371 RegCloseKey(dev_key
);
377 set_device_guid(flow
, key
, key_name
, guid
);
383 static const char *oss_clean_devnode(const char *devnode
)
385 static char ret
[OSS_DEVNODE_SIZE
];
387 const char *dot
, *slash
;
390 dot
= strrchr(devnode
, '.');
394 slash
= strrchr(devnode
, '/');
395 if(slash
&& dot
< slash
)
400 memcpy(ret
, devnode
, len
);
406 static UINT
get_default_index(EDataFlow flow
)
415 fd
= open("/dev/dsp", O_WRONLY
| O_NONBLOCK
);
417 fd
= open("/dev/dsp", O_RDONLY
| O_NONBLOCK
);
420 WARN("Couldn't open default device!\n");
425 if((err
= ioctl(fd
, SNDCTL_ENGINEINFO
, &ai
)) < 0){
426 WARN("SNDCTL_ENGINEINFO failed: %d (%s)\n", err
, strerror(errno
));
433 TRACE("Default devnode: %s\n", ai
.devnode
);
434 devnode
= oss_clean_devnode(ai
.devnode
);
436 LIST_FOR_EACH_ENTRY(dev_item
, &g_devices
, OSSDevice
, entry
){
437 if(dev_item
->flow
== flow
){
438 if(!strcmp(devnode
, dev_item
->devnode
))
444 WARN("Couldn't find default device! Choosing first.\n");
448 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
, GUID
**guids
,
449 UINT
*num
, UINT
*def_index
)
453 static int print_once
= 0;
455 static const WCHAR outW
[] = {'O','u','t',':',' ',0};
456 static const WCHAR inW
[] = {'I','n',':',' ',0};
458 TRACE("%d %p %p %p %p\n", flow
, ids
, guids
, num
, def_index
);
460 mixer_fd
= open("/dev/mixer", O_RDONLY
, 0);
462 ERR("OSS /dev/mixer doesn't seem to exist\n");
463 return AUDCLNT_E_SERVICE_NOT_RUNNING
;
466 if(ioctl(mixer_fd
, SNDCTL_SYSINFO
, &sysinfo
) < 0){
470 ERR("OSS version too old, need at least OSSv4\n");
471 return AUDCLNT_E_SERVICE_NOT_RUNNING
;
474 ERR("Error getting SNDCTL_SYSINFO: %d (%s)\n", errno
, strerror(errno
));
479 TRACE("OSS sysinfo:\n");
480 TRACE("product: %s\n", sysinfo
.product
);
481 TRACE("version: %s\n", sysinfo
.version
);
482 TRACE("versionnum: %x\n", sysinfo
.versionnum
);
483 TRACE("numaudios: %d\n", sysinfo
.numaudios
);
484 TRACE("nummixers: %d\n", sysinfo
.nummixers
);
485 TRACE("numcards: %d\n", sysinfo
.numcards
);
486 TRACE("numaudioengines: %d\n", sysinfo
.numaudioengines
);
490 if(sysinfo
.numaudios
<= 0){
491 WARN("No audio devices!\n");
493 return AUDCLNT_E_SERVICE_NOT_RUNNING
;
496 *ids
= HeapAlloc(GetProcessHeap(), 0, sysinfo
.numaudios
* sizeof(WCHAR
*));
497 *guids
= HeapAlloc(GetProcessHeap(), 0, sysinfo
.numaudios
* sizeof(GUID
));
500 for(i
= 0; i
< sysinfo
.numaudios
; ++i
){
501 oss_audioinfo ai
= {0};
507 if(ioctl(mixer_fd
, SNDCTL_AUDIOINFO
, &ai
) < 0){
508 WARN("Error getting AUDIOINFO for dev %d: %d (%s)\n", i
, errno
,
513 devnode
= oss_clean_devnode(ai
.devnode
);
515 /* check for duplicates */
516 LIST_FOR_EACH_ENTRY(dev_item
, &g_devices
, OSSDevice
, entry
){
517 if(dev_item
->flow
== flow
&& !strcmp(devnode
, dev_item
->devnode
))
520 if(&dev_item
->entry
!= &g_devices
)
524 fd
= open(devnode
, O_WRONLY
| O_NONBLOCK
, 0);
526 fd
= open(devnode
, O_RDONLY
| O_NONBLOCK
, 0);
528 WARN("Opening device \"%s\" failed, pretending it doesn't exist: %d (%s)\n",
529 devnode
, errno
, strerror(errno
));
534 if((flow
== eCapture
&& (ai
.caps
& PCM_CAP_INPUT
)) ||
535 (flow
== eRender
&& (ai
.caps
& PCM_CAP_OUTPUT
))){
536 size_t len
, prefix_len
;
539 dev_item
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dev_item
));
541 dev_item
->flow
= flow
;
542 get_device_guid(flow
, devnode
, &dev_item
->guid
);
543 strcpy(dev_item
->devnode
, devnode
);
545 (*guids
)[*num
] = dev_item
->guid
;
547 len
= MultiByteToWideChar(CP_UNIXCP
, 0, ai
.name
, -1, NULL
, 0);
550 prefix_len
= (sizeof(outW
) / sizeof(*outW
)) - 1;
554 prefix_len
= (sizeof(inW
) / sizeof(*inW
)) - 1;
557 (*ids
)[*num
] = HeapAlloc(GetProcessHeap(), 0,
558 len
* sizeof(WCHAR
));
560 for(i
= 0; i
< *num
; ++i
)
561 HeapFree(GetProcessHeap(), 0, (*ids
)[i
]);
562 HeapFree(GetProcessHeap(), 0, *ids
);
563 HeapFree(GetProcessHeap(), 0, *guids
);
564 HeapFree(GetProcessHeap(), 0, dev_item
);
566 return E_OUTOFMEMORY
;
568 memcpy((*ids
)[*num
], prefix
, prefix_len
* sizeof(WCHAR
));
569 MultiByteToWideChar(CP_UNIXCP
, 0, ai
.name
, -1,
570 (*ids
)[*num
] + prefix_len
, len
- prefix_len
);
572 list_add_tail(&g_devices
, &dev_item
->entry
);
580 *def_index
= get_default_index(flow
);
585 static const OSSDevice
*get_ossdevice_from_guid(const GUID
*guid
)
588 LIST_FOR_EACH_ENTRY(dev_item
, &g_devices
, OSSDevice
, entry
)
589 if(IsEqualGUID(guid
, &dev_item
->guid
))
594 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(GUID
*guid
, IMMDevice
*dev
,
598 const OSSDevice
*oss_dev
;
601 TRACE("%s %p %p\n", debugstr_guid(guid
), dev
, out
);
603 oss_dev
= get_ossdevice_from_guid(guid
);
605 WARN("Unknown GUID: %s\n", debugstr_guid(guid
));
606 return AUDCLNT_E_DEVICE_INVALIDATED
;
609 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
611 return E_OUTOFMEMORY
;
613 hr
= CoCreateFreeThreadedMarshaler((IUnknown
*)&This
->IAudioClient_iface
,
614 (IUnknown
**)&This
->pUnkFTMarshal
);
616 HeapFree(GetProcessHeap(), 0, This
);
620 if(oss_dev
->flow
== eRender
)
621 This
->fd
= open(oss_dev
->devnode
, O_WRONLY
| O_NONBLOCK
, 0);
622 else if(oss_dev
->flow
== eCapture
)
623 This
->fd
= open(oss_dev
->devnode
, O_RDONLY
| O_NONBLOCK
, 0);
625 HeapFree(GetProcessHeap(), 0, This
);
629 WARN("Unable to open device %s: %d (%s)\n", oss_dev
->devnode
, errno
,
631 HeapFree(GetProcessHeap(), 0, This
);
632 return AUDCLNT_E_DEVICE_INVALIDATED
;
635 This
->dataflow
= oss_dev
->flow
;
638 if(ioctl(This
->fd
, SNDCTL_ENGINEINFO
, &This
->ai
) < 0){
639 WARN("Unable to get audio info for device %s: %d (%s)\n", oss_dev
->devnode
,
640 errno
, strerror(errno
));
642 HeapFree(GetProcessHeap(), 0, This
);
646 strcpy(This
->devnode
, oss_dev
->devnode
);
648 TRACE("OSS audioinfo:\n");
649 TRACE("devnode: %s\n", This
->ai
.devnode
);
650 TRACE("name: %s\n", This
->ai
.name
);
651 TRACE("busy: %x\n", This
->ai
.busy
);
652 TRACE("caps: %x\n", This
->ai
.caps
);
653 TRACE("iformats: %x\n", This
->ai
.iformats
);
654 TRACE("oformats: %x\n", This
->ai
.oformats
);
655 TRACE("enabled: %d\n", This
->ai
.enabled
);
656 TRACE("min_rate: %d\n", This
->ai
.min_rate
);
657 TRACE("max_rate: %d\n", This
->ai
.max_rate
);
658 TRACE("min_channels: %d\n", This
->ai
.min_channels
);
659 TRACE("max_channels: %d\n", This
->ai
.max_channels
);
661 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
662 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
663 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
664 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
665 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
666 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
668 InitializeCriticalSection(&This
->lock
);
669 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ACImpl.lock");
672 IMMDevice_AddRef(This
->parent
);
674 IAudioClient_AddRef(&This
->IAudioClient_iface
);
676 *out
= &This
->IAudioClient_iface
;
681 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
682 REFIID riid
, void **ppv
)
684 ACImpl
*This
= impl_from_IAudioClient(iface
);
685 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
690 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
692 else if(IsEqualIID(riid
, &IID_IMarshal
))
693 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
695 IUnknown_AddRef((IUnknown
*)*ppv
);
698 WARN("Unknown interface %s\n", debugstr_guid(riid
));
699 return E_NOINTERFACE
;
702 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
704 ACImpl
*This
= impl_from_IAudioClient(iface
);
706 ref
= InterlockedIncrement(&This
->ref
);
707 TRACE("(%p) Refcount now %u\n", This
, ref
);
711 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
713 ACImpl
*This
= impl_from_IAudioClient(iface
);
715 ref
= InterlockedDecrement(&This
->ref
);
716 TRACE("(%p) Refcount now %u\n", This
, ref
);
718 IAudioClient_Stop(iface
);
719 IMMDevice_Release(This
->parent
);
720 IUnknown_Release(This
->pUnkFTMarshal
);
721 This
->lock
.DebugInfo
->Spare
[0] = 0;
722 DeleteCriticalSection(&This
->lock
);
725 EnterCriticalSection(&g_sessions_lock
);
726 list_remove(&This
->entry
);
727 LeaveCriticalSection(&g_sessions_lock
);
729 HeapFree(GetProcessHeap(), 0, This
->vols
);
730 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
731 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
732 CoTaskMemFree(This
->fmt
);
733 HeapFree(GetProcessHeap(), 0, This
);
738 static void dump_fmt(const WAVEFORMATEX
*fmt
)
740 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
741 switch(fmt
->wFormatTag
){
742 case WAVE_FORMAT_PCM
:
743 TRACE("WAVE_FORMAT_PCM");
745 case WAVE_FORMAT_IEEE_FLOAT
:
746 TRACE("WAVE_FORMAT_IEEE_FLOAT");
748 case WAVE_FORMAT_EXTENSIBLE
:
749 TRACE("WAVE_FORMAT_EXTENSIBLE");
757 TRACE("nChannels: %u\n", fmt
->nChannels
);
758 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
759 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
760 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
761 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
762 TRACE("cbSize: %u\n", fmt
->cbSize
);
764 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
765 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
766 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
767 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
768 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
772 static DWORD
get_channel_mask(unsigned int channels
)
778 return KSAUDIO_SPEAKER_MONO
;
780 return KSAUDIO_SPEAKER_STEREO
;
782 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
784 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
786 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
788 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
790 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
792 return KSAUDIO_SPEAKER_7POINT1_SURROUND
; /* Vista deprecates 7POINT1 */
794 FIXME("Unknown speaker configuration: %u\n", channels
);
798 static int get_oss_format(const WAVEFORMATEX
*fmt
)
800 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)fmt
;
802 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
803 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
804 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
805 switch(fmt
->wBitsPerSample
){
819 if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
820 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
821 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
822 if(fmt
->wBitsPerSample
!= 32)
832 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
837 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
838 size
= sizeof(WAVEFORMATEXTENSIBLE
);
840 size
= sizeof(WAVEFORMATEX
);
842 ret
= CoTaskMemAlloc(size
);
846 memcpy(ret
, fmt
, size
);
848 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
853 static HRESULT
setup_oss_device(AUDCLNT_SHAREMODE mode
, int fd
,
854 const WAVEFORMATEX
*fmt
, WAVEFORMATEX
**out
)
859 WAVEFORMATEX
*closest
= NULL
;
861 tmp
= oss_format
= get_oss_format(fmt
);
863 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
864 if(ioctl(fd
, SNDCTL_DSP_SETFMT
, &tmp
) < 0){
865 WARN("SETFMT failed: %d (%s)\n", errno
, strerror(errno
));
868 if(tmp
!= oss_format
){
869 TRACE("Format unsupported by this OSS version: %x\n", oss_format
);
870 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
873 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
874 (fmt
->nAvgBytesPerSec
== 0 ||
875 fmt
->nBlockAlign
== 0 ||
876 ((WAVEFORMATEXTENSIBLE
*)fmt
)->Samples
.wValidBitsPerSample
> fmt
->wBitsPerSample
))
879 if(fmt
->nChannels
== 0)
880 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
882 closest
= clone_format(fmt
);
884 return E_OUTOFMEMORY
;
886 tmp
= fmt
->nSamplesPerSec
;
887 if(ioctl(fd
, SNDCTL_DSP_SPEED
, &tmp
) < 0){
888 WARN("SPEED failed: %d (%s)\n", errno
, strerror(errno
));
889 CoTaskMemFree(closest
);
892 tenth
= fmt
->nSamplesPerSec
* 0.1;
893 if(tmp
> fmt
->nSamplesPerSec
+ tenth
|| tmp
< fmt
->nSamplesPerSec
- tenth
){
895 closest
->nSamplesPerSec
= tmp
;
898 tmp
= fmt
->nChannels
;
899 if(ioctl(fd
, SNDCTL_DSP_CHANNELS
, &tmp
) < 0){
900 WARN("CHANNELS failed: %d (%s)\n", errno
, strerror(errno
));
901 CoTaskMemFree(closest
);
904 if(tmp
!= fmt
->nChannels
){
906 closest
->nChannels
= tmp
;
909 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
910 ((WAVEFORMATEXTENSIBLE
*)closest
)->dwChannelMask
= get_channel_mask(closest
->nChannels
);
912 if(fmt
->nBlockAlign
!= fmt
->nChannels
* fmt
->wBitsPerSample
/ 8 ||
913 fmt
->nAvgBytesPerSec
!= fmt
->nBlockAlign
* fmt
->nSamplesPerSec
||
914 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
915 ((WAVEFORMATEXTENSIBLE
*)fmt
)->Samples
.wValidBitsPerSample
< fmt
->wBitsPerSample
))
918 if(mode
== AUDCLNT_SHAREMODE_EXCLUSIVE
&&
919 fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
920 if(((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
== 0 ||
921 ((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
& SPEAKER_RESERVED
)
925 if(ret
== S_FALSE
&& !out
)
926 ret
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
928 if(ret
== S_FALSE
&& out
){
929 closest
->nBlockAlign
=
930 closest
->nChannels
* closest
->wBitsPerSample
/ 8;
931 closest
->nAvgBytesPerSec
=
932 closest
->nBlockAlign
* closest
->nSamplesPerSec
;
933 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
934 ((WAVEFORMATEXTENSIBLE
*)closest
)->Samples
.wValidBitsPerSample
= closest
->wBitsPerSample
;
937 CoTaskMemFree(closest
);
939 TRACE("returning: %08x\n", ret
);
943 static void session_init_vols(AudioSession
*session
, UINT channels
)
945 if(session
->channel_count
< channels
){
948 if(session
->channel_vols
)
949 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
950 session
->channel_vols
, sizeof(float) * channels
);
952 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
953 sizeof(float) * channels
);
954 if(!session
->channel_vols
)
957 for(i
= session
->channel_count
; i
< channels
; ++i
)
958 session
->channel_vols
[i
] = 1.f
;
960 session
->channel_count
= channels
;
964 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
969 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
973 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
975 ret
->device
= device
;
977 list_init(&ret
->clients
);
979 list_add_head(&g_sessions
, &ret
->entry
);
981 InitializeCriticalSection(&ret
->lock
);
982 ret
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AudioSession.lock");
984 session_init_vols(ret
, num_channels
);
986 ret
->master_vol
= 1.f
;
991 /* if channels == 0, then this will return or create a session with
992 * matching dataflow and GUID. otherwise, channels must also match */
993 static HRESULT
get_audio_session(const GUID
*sessionguid
,
994 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
996 AudioSession
*session
;
998 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
999 *out
= create_session(&GUID_NULL
, device
, channels
);
1001 return E_OUTOFMEMORY
;
1007 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
1008 if(session
->device
== device
&&
1009 IsEqualGUID(sessionguid
, &session
->guid
)){
1010 session_init_vols(session
, channels
);
1017 *out
= create_session(sessionguid
, device
, channels
);
1019 return E_OUTOFMEMORY
;
1025 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
1026 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
1027 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
1028 const GUID
*sessionguid
)
1030 ACImpl
*This
= impl_from_IAudioClient(iface
);
1034 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
1035 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
1042 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1043 return AUDCLNT_E_NOT_INITIALIZED
;
1045 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
1046 AUDCLNT_STREAMFLAGS_LOOPBACK
|
1047 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
1048 AUDCLNT_STREAMFLAGS_NOPERSIST
|
1049 AUDCLNT_STREAMFLAGS_RATEADJUST
|
1050 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
1051 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
1052 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
1053 TRACE("Unknown flags: %08x\n", flags
);
1054 return E_INVALIDARG
;
1057 if(mode
== AUDCLNT_SHAREMODE_SHARED
){
1058 period
= DefaultPeriod
;
1059 if( duration
< 3 * period
)
1060 duration
= 3 * period
;
1063 period
= DefaultPeriod
; /* not minimum */
1064 if(period
< MinimumPeriod
|| period
> 5000000)
1065 return AUDCLNT_E_INVALID_DEVICE_PERIOD
;
1066 if(duration
> 20000000) /* the smaller the period, the lower this limit */
1067 return AUDCLNT_E_BUFFER_SIZE_ERROR
;
1068 if(flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
){
1069 if(duration
!= period
)
1070 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL
;
1071 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
1072 return AUDCLNT_E_DEVICE_IN_USE
;
1074 if( duration
< 8 * period
)
1075 duration
= 8 * period
; /* may grow above 2s */
1079 EnterCriticalSection(&This
->lock
);
1082 LeaveCriticalSection(&This
->lock
);
1083 return AUDCLNT_E_ALREADY_INITIALIZED
;
1086 hr
= setup_oss_device(mode
, This
->fd
, fmt
, NULL
);
1088 LeaveCriticalSection(&This
->lock
);
1092 This
->fmt
= clone_format(fmt
);
1094 LeaveCriticalSection(&This
->lock
);
1095 return E_OUTOFMEMORY
;
1098 This
->period_us
= period
/ 10;
1099 This
->period_frames
= MulDiv(fmt
->nSamplesPerSec
, period
, 10000000);
1101 This
->bufsize_frames
= MulDiv(duration
, fmt
->nSamplesPerSec
, 10000000);
1102 This
->local_buffer
= HeapAlloc(GetProcessHeap(), 0,
1103 This
->bufsize_frames
* fmt
->nBlockAlign
);
1104 if(!This
->local_buffer
){
1105 CoTaskMemFree(This
->fmt
);
1107 LeaveCriticalSection(&This
->lock
);
1108 return E_OUTOFMEMORY
;
1111 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
1113 CoTaskMemFree(This
->fmt
);
1115 LeaveCriticalSection(&This
->lock
);
1116 return E_OUTOFMEMORY
;
1119 for(i
= 0; i
< fmt
->nChannels
; ++i
)
1120 This
->vols
[i
] = 1.f
;
1123 This
->flags
= flags
;
1124 This
->oss_bufsize_bytes
= 0;
1126 EnterCriticalSection(&g_sessions_lock
);
1128 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
1131 LeaveCriticalSection(&g_sessions_lock
);
1132 HeapFree(GetProcessHeap(), 0, This
->vols
);
1134 CoTaskMemFree(This
->fmt
);
1136 LeaveCriticalSection(&This
->lock
);
1140 list_add_tail(&This
->session
->clients
, &This
->entry
);
1142 LeaveCriticalSection(&g_sessions_lock
);
1144 This
->initted
= TRUE
;
1146 LeaveCriticalSection(&This
->lock
);
1151 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
1154 ACImpl
*This
= impl_from_IAudioClient(iface
);
1156 TRACE("(%p)->(%p)\n", This
, frames
);
1161 EnterCriticalSection(&This
->lock
);
1164 LeaveCriticalSection(&This
->lock
);
1165 return AUDCLNT_E_NOT_INITIALIZED
;
1168 *frames
= This
->bufsize_frames
;
1170 TRACE("buffer size: %u\n", *frames
);
1172 LeaveCriticalSection(&This
->lock
);
1177 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1178 REFERENCE_TIME
*latency
)
1180 ACImpl
*This
= impl_from_IAudioClient(iface
);
1182 TRACE("(%p)->(%p)\n", This
, latency
);
1187 EnterCriticalSection(&This
->lock
);
1190 LeaveCriticalSection(&This
->lock
);
1191 return AUDCLNT_E_NOT_INITIALIZED
;
1194 /* pretend we process audio in Period chunks, so max latency includes
1195 * the period time. Some native machines add .6666ms in shared mode. */
1196 *latency
= This
->period_us
* 10 + 6666;
1198 LeaveCriticalSection(&This
->lock
);
1203 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1206 ACImpl
*This
= impl_from_IAudioClient(iface
);
1208 TRACE("(%p)->(%p)\n", This
, numpad
);
1213 EnterCriticalSection(&This
->lock
);
1216 LeaveCriticalSection(&This
->lock
);
1217 return AUDCLNT_E_NOT_INITIALIZED
;
1220 *numpad
= This
->held_frames
;
1222 TRACE("padding: %u\n", *numpad
);
1224 LeaveCriticalSection(&This
->lock
);
1229 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1230 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*pwfx
,
1231 WAVEFORMATEX
**outpwfx
)
1233 ACImpl
*This
= impl_from_IAudioClient(iface
);
1237 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, pwfx
, outpwfx
);
1239 if(!pwfx
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !outpwfx
))
1242 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1243 return E_INVALIDARG
;
1245 if(pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1246 pwfx
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1247 return E_INVALIDARG
;
1253 if(mode
!= AUDCLNT_SHAREMODE_SHARED
)
1257 if(This
->dataflow
== eRender
)
1258 fd
= open(This
->devnode
, O_WRONLY
| O_NONBLOCK
, 0);
1259 else if(This
->dataflow
== eCapture
)
1260 fd
= open(This
->devnode
, O_RDONLY
| O_NONBLOCK
, 0);
1263 WARN("Unable to open device %s: %d (%s)\n", This
->devnode
, errno
,
1265 return AUDCLNT_E_DEVICE_INVALIDATED
;
1268 ret
= setup_oss_device(mode
, fd
, pwfx
, outpwfx
);
1275 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1276 WAVEFORMATEX
**pwfx
)
1278 ACImpl
*This
= impl_from_IAudioClient(iface
);
1279 WAVEFORMATEXTENSIBLE
*fmt
;
1282 TRACE("(%p)->(%p)\n", This
, pwfx
);
1288 if(This
->dataflow
== eRender
)
1289 formats
= This
->ai
.oformats
;
1290 else if(This
->dataflow
== eCapture
)
1291 formats
= This
->ai
.iformats
;
1293 return E_UNEXPECTED
;
1295 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1297 return E_OUTOFMEMORY
;
1299 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1300 if(formats
& AFMT_S16_LE
){
1301 fmt
->Format
.wBitsPerSample
= 16;
1302 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1304 }else if(formats
& AFMT_FLOAT
){
1305 fmt
->Format
.wBitsPerSample
= 32;
1306 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1308 }else if(formats
& AFMT_U8
){
1309 fmt
->Format
.wBitsPerSample
= 8;
1310 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1311 }else if(formats
& AFMT_S32_LE
){
1312 fmt
->Format
.wBitsPerSample
= 32;
1313 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1314 }else if(formats
& AFMT_S24_LE
){
1315 fmt
->Format
.wBitsPerSample
= 24;
1316 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1318 WARN("Didn't recognize any available OSS formats: %x\n", formats
);
1323 /* some OSS drivers are buggy, so set reasonable defaults if
1324 * the reported values seem wacky */
1325 fmt
->Format
.nChannels
= max(This
->ai
.max_channels
, This
->ai
.min_channels
);
1326 if(fmt
->Format
.nChannels
== 0 || fmt
->Format
.nChannels
> 8)
1327 fmt
->Format
.nChannels
= 2;
1329 if(This
->ai
.max_rate
== 0)
1330 fmt
->Format
.nSamplesPerSec
= 44100;
1332 fmt
->Format
.nSamplesPerSec
= min(This
->ai
.max_rate
, 44100);
1333 if(fmt
->Format
.nSamplesPerSec
< This
->ai
.min_rate
)
1334 fmt
->Format
.nSamplesPerSec
= This
->ai
.min_rate
;
1336 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1338 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1339 fmt
->Format
.nChannels
) / 8;
1340 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1341 fmt
->Format
.nBlockAlign
;
1343 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1344 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1346 *pwfx
= (WAVEFORMATEX
*)fmt
;
1352 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1353 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1355 ACImpl
*This
= impl_from_IAudioClient(iface
);
1357 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1359 if(!defperiod
&& !minperiod
)
1363 *defperiod
= DefaultPeriod
;
1365 *minperiod
= MinimumPeriod
;
1370 static void silence_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 frames
)
1372 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
1373 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1374 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1375 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
1376 This
->fmt
->wBitsPerSample
== 8)
1377 memset(buffer
, 128, frames
* This
->fmt
->nBlockAlign
);
1379 memset(buffer
, 0, frames
* This
->fmt
->nBlockAlign
);
1382 static void oss_write_data(ACImpl
*This
)
1384 ssize_t written_bytes
;
1385 UINT32 written_frames
, in_oss_frames
, write_limit
, max_period
;
1386 size_t to_write_frames
, to_write_bytes
;
1389 This
->local_buffer
+ (This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
);
1391 if(This
->held_frames
== 0)
1394 if(This
->lcl_offs_frames
+ This
->held_frames
> This
->bufsize_frames
)
1395 to_write_frames
= This
->bufsize_frames
- This
->lcl_offs_frames
;
1397 to_write_frames
= This
->held_frames
;
1399 if(ioctl(This
->fd
, SNDCTL_DSP_GETOSPACE
, &bi
) < 0){
1400 WARN("GETOSPACE failed: %d (%s)\n", errno
, strerror(errno
));
1404 max_period
= max(bi
.fragsize
/ This
->fmt
->nBlockAlign
, This
->period_frames
);
1406 if(bi
.bytes
> This
->oss_bufsize_bytes
){
1407 TRACE("New buffer size (%u) is larger than old buffer size (%u)\n",
1408 bi
.bytes
, This
->oss_bufsize_bytes
);
1409 This
->oss_bufsize_bytes
= bi
.bytes
;
1411 }else if(This
->oss_bufsize_bytes
- bi
.bytes
<= bi
.fragsize
)
1414 in_oss_frames
= (This
->oss_bufsize_bytes
- bi
.bytes
) / This
->fmt
->nBlockAlign
;
1417 while(write_limit
+ in_oss_frames
< max_period
* 3)
1418 write_limit
+= max_period
;
1419 if(write_limit
== 0)
1422 to_write_frames
= min(to_write_frames
, write_limit
);
1423 to_write_bytes
= to_write_frames
* This
->fmt
->nBlockAlign
;
1425 if(This
->session
->mute
)
1426 silence_buffer(This
, buf
, to_write_frames
);
1428 written_bytes
= write(This
->fd
, buf
, to_write_bytes
);
1429 if(written_bytes
< 0){
1430 /* EAGAIN is OSS buffer full, log that too */
1431 WARN("write failed: %d (%s)\n", errno
, strerror(errno
));
1434 written_frames
= written_bytes
/ This
->fmt
->nBlockAlign
;
1436 This
->lcl_offs_frames
+= written_frames
;
1437 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1438 This
->held_frames
-= written_frames
;
1440 if(written_frames
< to_write_frames
){
1441 /* OSS buffer probably full */
1445 if(This
->held_frames
&& written_frames
< write_limit
){
1446 /* wrapped and have some data back at the start to write */
1448 to_write_frames
= min(write_limit
- written_frames
, This
->held_frames
);
1449 to_write_bytes
= to_write_frames
* This
->fmt
->nBlockAlign
;
1451 if(This
->session
->mute
)
1452 silence_buffer(This
, This
->local_buffer
, to_write_frames
);
1454 written_bytes
= write(This
->fd
, This
->local_buffer
, to_write_bytes
);
1455 if(written_bytes
< 0){
1456 WARN("write failed: %d (%s)\n", errno
, strerror(errno
));
1459 written_frames
= written_bytes
/ This
->fmt
->nBlockAlign
;
1461 This
->lcl_offs_frames
+= written_frames
;
1462 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1463 This
->held_frames
-= written_frames
;
1467 static void oss_read_data(ACImpl
*This
)
1469 UINT64 pos
, readable
;
1473 if(ioctl(This
->fd
, SNDCTL_DSP_GETISPACE
, &bi
) < 0){
1474 WARN("GETISPACE failed: %d (%s)\n", errno
, strerror(errno
));
1478 pos
= (This
->held_frames
+ This
->lcl_offs_frames
) % This
->bufsize_frames
;
1479 readable
= (This
->bufsize_frames
- pos
) * This
->fmt
->nBlockAlign
;
1481 if(bi
.bytes
< readable
)
1482 readable
= bi
.bytes
;
1484 nread
= read(This
->fd
, This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
,
1487 WARN("read failed: %d (%s)\n", errno
, strerror(errno
));
1491 This
->held_frames
+= nread
/ This
->fmt
->nBlockAlign
;
1493 if(This
->held_frames
> This
->bufsize_frames
){
1494 WARN("Overflow of unread data\n");
1495 This
->lcl_offs_frames
+= This
->held_frames
;
1496 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1497 This
->held_frames
= This
->bufsize_frames
;
1501 static void CALLBACK
oss_period_callback(void *user
, BOOLEAN timer
)
1503 ACImpl
*This
= user
;
1505 EnterCriticalSection(&This
->lock
);
1508 if(This
->dataflow
== eRender
&& This
->held_frames
)
1509 oss_write_data(This
);
1510 else if(This
->dataflow
== eCapture
)
1511 oss_read_data(This
);
1514 LeaveCriticalSection(&This
->lock
);
1517 SetEvent(This
->event
);
1520 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1522 ACImpl
*This
= impl_from_IAudioClient(iface
);
1524 TRACE("(%p)\n", This
);
1526 EnterCriticalSection(&This
->lock
);
1529 LeaveCriticalSection(&This
->lock
);
1530 return AUDCLNT_E_NOT_INITIALIZED
;
1533 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1534 LeaveCriticalSection(&This
->lock
);
1535 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1539 LeaveCriticalSection(&This
->lock
);
1540 return AUDCLNT_E_NOT_STOPPED
;
1543 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
,
1544 oss_period_callback
, This
, 0, This
->period_us
/ 1000,
1545 WT_EXECUTEINTIMERTHREAD
))
1546 ERR("Unable to create period timer: %u\n", GetLastError());
1548 This
->playing
= TRUE
;
1550 LeaveCriticalSection(&This
->lock
);
1555 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1557 ACImpl
*This
= impl_from_IAudioClient(iface
);
1561 TRACE("(%p)\n", This
);
1563 EnterCriticalSection(&This
->lock
);
1566 LeaveCriticalSection(&This
->lock
);
1567 return AUDCLNT_E_NOT_INITIALIZED
;
1571 LeaveCriticalSection(&This
->lock
);
1575 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1576 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
1578 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1579 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
1581 This
->playing
= FALSE
;
1583 LeaveCriticalSection(&This
->lock
);
1586 WaitForSingleObject(event
, INFINITE
);
1592 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1594 ACImpl
*This
= impl_from_IAudioClient(iface
);
1596 TRACE("(%p)\n", This
);
1598 EnterCriticalSection(&This
->lock
);
1601 LeaveCriticalSection(&This
->lock
);
1602 return AUDCLNT_E_NOT_INITIALIZED
;
1606 LeaveCriticalSection(&This
->lock
);
1607 return AUDCLNT_E_NOT_STOPPED
;
1610 if(This
->getbuf_last
){
1611 LeaveCriticalSection(&This
->lock
);
1612 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
1615 if(This
->dataflow
== eRender
){
1616 This
->written_frames
= 0;
1617 This
->last_pos_frames
= 0;
1619 This
->written_frames
+= This
->held_frames
;
1621 This
->held_frames
= 0;
1622 This
->lcl_offs_frames
= 0;
1624 LeaveCriticalSection(&This
->lock
);
1629 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1632 ACImpl
*This
= impl_from_IAudioClient(iface
);
1634 TRACE("(%p)->(%p)\n", This
, event
);
1637 return E_INVALIDARG
;
1639 EnterCriticalSection(&This
->lock
);
1642 LeaveCriticalSection(&This
->lock
);
1643 return AUDCLNT_E_NOT_INITIALIZED
;
1646 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1647 LeaveCriticalSection(&This
->lock
);
1648 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1652 LeaveCriticalSection(&This
->lock
);
1653 FIXME("called twice\n");
1654 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
1657 This
->event
= event
;
1659 LeaveCriticalSection(&This
->lock
);
1664 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1667 ACImpl
*This
= impl_from_IAudioClient(iface
);
1669 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1675 EnterCriticalSection(&This
->lock
);
1678 LeaveCriticalSection(&This
->lock
);
1679 return AUDCLNT_E_NOT_INITIALIZED
;
1682 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1683 if(This
->dataflow
!= eRender
){
1684 LeaveCriticalSection(&This
->lock
);
1685 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1687 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
1688 *ppv
= &This
->IAudioRenderClient_iface
;
1689 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1690 if(This
->dataflow
!= eCapture
){
1691 LeaveCriticalSection(&This
->lock
);
1692 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1694 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
1695 *ppv
= &This
->IAudioCaptureClient_iface
;
1696 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1697 IAudioClock_AddRef(&This
->IAudioClock_iface
);
1698 *ppv
= &This
->IAudioClock_iface
;
1699 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1700 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
1701 *ppv
= &This
->IAudioStreamVolume_iface
;
1702 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1703 if(!This
->session_wrapper
){
1704 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1705 if(!This
->session_wrapper
){
1706 LeaveCriticalSection(&This
->lock
);
1707 return E_OUTOFMEMORY
;
1710 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
1712 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1713 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1714 if(!This
->session_wrapper
){
1715 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1716 if(!This
->session_wrapper
){
1717 LeaveCriticalSection(&This
->lock
);
1718 return E_OUTOFMEMORY
;
1721 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
1723 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1724 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1725 if(!This
->session_wrapper
){
1726 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1727 if(!This
->session_wrapper
){
1728 LeaveCriticalSection(&This
->lock
);
1729 return E_OUTOFMEMORY
;
1732 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
1734 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1738 LeaveCriticalSection(&This
->lock
);
1742 LeaveCriticalSection(&This
->lock
);
1744 FIXME("stub %s\n", debugstr_guid(riid
));
1745 return E_NOINTERFACE
;
1748 static const IAudioClientVtbl AudioClient_Vtbl
=
1750 AudioClient_QueryInterface
,
1752 AudioClient_Release
,
1753 AudioClient_Initialize
,
1754 AudioClient_GetBufferSize
,
1755 AudioClient_GetStreamLatency
,
1756 AudioClient_GetCurrentPadding
,
1757 AudioClient_IsFormatSupported
,
1758 AudioClient_GetMixFormat
,
1759 AudioClient_GetDevicePeriod
,
1763 AudioClient_SetEventHandle
,
1764 AudioClient_GetService
1767 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1768 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1770 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1771 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1777 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1778 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1780 else if(IsEqualIID(riid
, &IID_IMarshal
))
1781 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
1783 IUnknown_AddRef((IUnknown
*)*ppv
);
1787 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1788 return E_NOINTERFACE
;
1791 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1793 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1794 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1797 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1799 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1800 return AudioClient_Release(&This
->IAudioClient_iface
);
1803 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1804 UINT32 frames
, BYTE
**data
)
1806 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1809 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1816 EnterCriticalSection(&This
->lock
);
1818 if(This
->getbuf_last
){
1819 LeaveCriticalSection(&This
->lock
);
1820 return AUDCLNT_E_OUT_OF_ORDER
;
1824 LeaveCriticalSection(&This
->lock
);
1828 if(This
->held_frames
+ frames
> This
->bufsize_frames
){
1829 LeaveCriticalSection(&This
->lock
);
1830 return AUDCLNT_E_BUFFER_TOO_LARGE
;
1834 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
1835 if(write_pos
+ frames
> This
->bufsize_frames
){
1836 if(This
->tmp_buffer_frames
< frames
){
1837 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
1838 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
1839 frames
* This
->fmt
->nBlockAlign
);
1840 if(!This
->tmp_buffer
){
1841 LeaveCriticalSection(&This
->lock
);
1842 return E_OUTOFMEMORY
;
1844 This
->tmp_buffer_frames
= frames
;
1846 *data
= This
->tmp_buffer
;
1847 This
->getbuf_last
= -frames
;
1849 *data
= This
->local_buffer
+ write_pos
* This
->fmt
->nBlockAlign
;
1850 This
->getbuf_last
= frames
;
1853 silence_buffer(This
, *data
, frames
);
1855 LeaveCriticalSection(&This
->lock
);
1860 static void oss_wrap_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 written_frames
)
1862 UINT32 write_offs_frames
=
1863 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
1864 UINT32 write_offs_bytes
= write_offs_frames
* This
->fmt
->nBlockAlign
;
1865 UINT32 chunk_frames
= This
->bufsize_frames
- write_offs_frames
;
1866 UINT32 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
1867 UINT32 written_bytes
= written_frames
* This
->fmt
->nBlockAlign
;
1869 if(written_bytes
<= chunk_bytes
){
1870 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, written_bytes
);
1872 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, chunk_bytes
);
1873 memcpy(This
->local_buffer
, buffer
+ chunk_bytes
,
1874 written_bytes
- chunk_bytes
);
1878 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
1879 IAudioRenderClient
*iface
, UINT32 written_frames
, DWORD flags
)
1881 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1884 TRACE("(%p)->(%u, %x)\n", This
, written_frames
, flags
);
1886 EnterCriticalSection(&This
->lock
);
1888 if(!written_frames
){
1889 This
->getbuf_last
= 0;
1890 LeaveCriticalSection(&This
->lock
);
1894 if(!This
->getbuf_last
){
1895 LeaveCriticalSection(&This
->lock
);
1896 return AUDCLNT_E_OUT_OF_ORDER
;
1899 if(written_frames
> (This
->getbuf_last
>= 0 ? This
->getbuf_last
: -This
->getbuf_last
)){
1900 LeaveCriticalSection(&This
->lock
);
1901 return AUDCLNT_E_INVALID_SIZE
;
1904 if(This
->getbuf_last
>= 0)
1905 buffer
= This
->local_buffer
+ This
->fmt
->nBlockAlign
*
1906 ((This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
);
1908 buffer
= This
->tmp_buffer
;
1910 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
)
1911 silence_buffer(This
, buffer
, written_frames
);
1913 if(This
->getbuf_last
< 0)
1914 oss_wrap_buffer(This
, buffer
, written_frames
);
1916 This
->held_frames
+= written_frames
;
1917 This
->written_frames
+= written_frames
;
1918 This
->getbuf_last
= 0;
1920 LeaveCriticalSection(&This
->lock
);
1925 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
1926 AudioRenderClient_QueryInterface
,
1927 AudioRenderClient_AddRef
,
1928 AudioRenderClient_Release
,
1929 AudioRenderClient_GetBuffer
,
1930 AudioRenderClient_ReleaseBuffer
1933 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
1934 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
1936 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1937 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1943 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1944 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
1946 else if(IsEqualIID(riid
, &IID_IMarshal
))
1947 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
1949 IUnknown_AddRef((IUnknown
*)*ppv
);
1953 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1954 return E_NOINTERFACE
;
1957 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
1959 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1960 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
1963 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
1965 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1966 return IAudioClient_Release(&This
->IAudioClient_iface
);
1969 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
1970 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
1973 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1975 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
1978 if(!data
|| !frames
|| !flags
)
1981 EnterCriticalSection(&This
->lock
);
1983 if(This
->getbuf_last
){
1984 LeaveCriticalSection(&This
->lock
);
1985 return AUDCLNT_E_OUT_OF_ORDER
;
1988 if(This
->held_frames
< This
->period_frames
){
1990 LeaveCriticalSection(&This
->lock
);
1991 return AUDCLNT_S_BUFFER_EMPTY
;
1996 *frames
= This
->period_frames
;
1998 if(This
->lcl_offs_frames
+ *frames
> This
->bufsize_frames
){
1999 UINT32 chunk_bytes
, offs_bytes
, frames_bytes
;
2000 if(This
->tmp_buffer_frames
< *frames
){
2001 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
2002 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2003 *frames
* This
->fmt
->nBlockAlign
);
2004 if(!This
->tmp_buffer
){
2005 LeaveCriticalSection(&This
->lock
);
2006 return E_OUTOFMEMORY
;
2008 This
->tmp_buffer_frames
= *frames
;
2011 *data
= This
->tmp_buffer
;
2012 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) *
2013 This
->fmt
->nBlockAlign
;
2014 offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2015 frames_bytes
= *frames
* This
->fmt
->nBlockAlign
;
2016 memcpy(This
->tmp_buffer
, This
->local_buffer
+ offs_bytes
, chunk_bytes
);
2017 memcpy(This
->tmp_buffer
+ chunk_bytes
, This
->local_buffer
,
2018 frames_bytes
- chunk_bytes
);
2020 *data
= This
->local_buffer
+
2021 This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2023 This
->getbuf_last
= *frames
;
2026 *devpos
= This
->written_frames
;
2028 LARGE_INTEGER stamp
, freq
;
2029 QueryPerformanceCounter(&stamp
);
2030 QueryPerformanceFrequency(&freq
);
2031 *qpcpos
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2034 LeaveCriticalSection(&This
->lock
);
2036 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
2039 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2040 IAudioCaptureClient
*iface
, UINT32 done
)
2042 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2044 TRACE("(%p)->(%u)\n", This
, done
);
2046 EnterCriticalSection(&This
->lock
);
2049 This
->getbuf_last
= 0;
2050 LeaveCriticalSection(&This
->lock
);
2054 if(!This
->getbuf_last
){
2055 LeaveCriticalSection(&This
->lock
);
2056 return AUDCLNT_E_OUT_OF_ORDER
;
2059 if(This
->getbuf_last
!= done
){
2060 LeaveCriticalSection(&This
->lock
);
2061 return AUDCLNT_E_INVALID_SIZE
;
2064 This
->written_frames
+= done
;
2065 This
->held_frames
-= done
;
2066 This
->lcl_offs_frames
+= done
;
2067 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2068 This
->getbuf_last
= 0;
2070 LeaveCriticalSection(&This
->lock
);
2075 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2076 IAudioCaptureClient
*iface
, UINT32
*frames
)
2078 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2080 TRACE("(%p)->(%p)\n", This
, frames
);
2085 EnterCriticalSection(&This
->lock
);
2087 *frames
= This
->held_frames
< This
->period_frames
? 0 : This
->period_frames
;
2089 LeaveCriticalSection(&This
->lock
);
2094 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2096 AudioCaptureClient_QueryInterface
,
2097 AudioCaptureClient_AddRef
,
2098 AudioCaptureClient_Release
,
2099 AudioCaptureClient_GetBuffer
,
2100 AudioCaptureClient_ReleaseBuffer
,
2101 AudioCaptureClient_GetNextPacketSize
2104 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2105 REFIID riid
, void **ppv
)
2107 ACImpl
*This
= impl_from_IAudioClock(iface
);
2109 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2115 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2117 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2118 *ppv
= &This
->IAudioClock2_iface
;
2120 IUnknown_AddRef((IUnknown
*)*ppv
);
2124 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2125 return E_NOINTERFACE
;
2128 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2130 ACImpl
*This
= impl_from_IAudioClock(iface
);
2131 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2134 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2136 ACImpl
*This
= impl_from_IAudioClock(iface
);
2137 return IAudioClient_Release(&This
->IAudioClient_iface
);
2140 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2142 ACImpl
*This
= impl_from_IAudioClock(iface
);
2144 TRACE("(%p)->(%p)\n", This
, freq
);
2146 *freq
= This
->fmt
->nSamplesPerSec
;
2151 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2154 ACImpl
*This
= impl_from_IAudioClock(iface
);
2157 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2162 EnterCriticalSection(&This
->lock
);
2164 if(This
->dataflow
== eRender
){
2165 if(!This
->playing
|| !This
->held_frames
||
2166 ioctl(This
->fd
, SNDCTL_DSP_GETODELAY
, &delay
) < 0)
2169 delay
/= This
->fmt
->nBlockAlign
;
2170 if(This
->held_frames
+ delay
>= This
->written_frames
)
2171 *pos
= This
->last_pos_frames
;
2173 *pos
= This
->written_frames
- This
->held_frames
- delay
;
2174 if(*pos
< This
->last_pos_frames
)
2175 *pos
= This
->last_pos_frames
;
2177 }else if(This
->dataflow
== eCapture
){
2181 if(ioctl(This
->fd
, SNDCTL_DSP_GETISPACE
, &bi
) < 0){
2182 TRACE("GETISPACE failed: %d (%s)\n", errno
, strerror(errno
));
2185 if(bi
.bytes
<= bi
.fragsize
)
2188 held
= bi
.bytes
/ This
->fmt
->nBlockAlign
;
2191 *pos
= This
->written_frames
+ held
;
2194 This
->last_pos_frames
= *pos
;
2196 LeaveCriticalSection(&This
->lock
);
2199 LARGE_INTEGER stamp
, freq
;
2200 QueryPerformanceCounter(&stamp
);
2201 QueryPerformanceFrequency(&freq
);
2202 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2208 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2211 ACImpl
*This
= impl_from_IAudioClock(iface
);
2213 TRACE("(%p)->(%p)\n", This
, chars
);
2218 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2223 static const IAudioClockVtbl AudioClock_Vtbl
=
2225 AudioClock_QueryInterface
,
2228 AudioClock_GetFrequency
,
2229 AudioClock_GetPosition
,
2230 AudioClock_GetCharacteristics
2233 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2234 REFIID riid
, void **ppv
)
2236 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2237 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2240 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2242 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2243 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2246 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2248 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2249 return IAudioClient_Release(&This
->IAudioClient_iface
);
2252 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2253 UINT64
*pos
, UINT64
*qpctime
)
2255 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2257 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2262 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2264 AudioClock2_QueryInterface
,
2266 AudioClock2_Release
,
2267 AudioClock2_GetDevicePosition
2270 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2272 AudioSessionWrapper
*ret
;
2274 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2275 sizeof(AudioSessionWrapper
));
2279 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2280 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2281 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2285 ret
->client
= client
;
2287 ret
->session
= client
->session
;
2288 AudioClient_AddRef(&client
->IAudioClient_iface
);
2294 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2295 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2297 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2303 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2304 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2305 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2308 IUnknown_AddRef((IUnknown
*)*ppv
);
2312 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2313 return E_NOINTERFACE
;
2316 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2318 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2320 ref
= InterlockedIncrement(&This
->ref
);
2321 TRACE("(%p) Refcount now %u\n", This
, ref
);
2325 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2327 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2329 ref
= InterlockedDecrement(&This
->ref
);
2330 TRACE("(%p) Refcount now %u\n", This
, ref
);
2333 EnterCriticalSection(&This
->client
->lock
);
2334 This
->client
->session_wrapper
= NULL
;
2335 LeaveCriticalSection(&This
->client
->lock
);
2336 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2338 HeapFree(GetProcessHeap(), 0, This
);
2343 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2344 AudioSessionState
*state
)
2346 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2349 TRACE("(%p)->(%p)\n", This
, state
);
2352 return NULL_PTR_ERR
;
2354 EnterCriticalSection(&g_sessions_lock
);
2356 if(list_empty(&This
->session
->clients
)){
2357 *state
= AudioSessionStateExpired
;
2358 LeaveCriticalSection(&g_sessions_lock
);
2362 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2363 EnterCriticalSection(&client
->lock
);
2364 if(client
->playing
){
2365 *state
= AudioSessionStateActive
;
2366 LeaveCriticalSection(&client
->lock
);
2367 LeaveCriticalSection(&g_sessions_lock
);
2370 LeaveCriticalSection(&client
->lock
);
2373 LeaveCriticalSection(&g_sessions_lock
);
2375 *state
= AudioSessionStateInactive
;
2380 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2381 IAudioSessionControl2
*iface
, WCHAR
**name
)
2383 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2385 FIXME("(%p)->(%p) - stub\n", This
, name
);
2390 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2391 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2393 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2395 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2400 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2401 IAudioSessionControl2
*iface
, WCHAR
**path
)
2403 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2405 FIXME("(%p)->(%p) - stub\n", This
, path
);
2410 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2411 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2413 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2415 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2420 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2421 IAudioSessionControl2
*iface
, GUID
*group
)
2423 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2425 FIXME("(%p)->(%p) - stub\n", This
, group
);
2430 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2431 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2433 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2435 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2436 debugstr_guid(session
));
2441 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2442 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2444 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2446 FIXME("(%p)->(%p) - stub\n", This
, events
);
2451 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2452 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2454 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2456 FIXME("(%p)->(%p) - stub\n", This
, events
);
2461 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2462 IAudioSessionControl2
*iface
, WCHAR
**id
)
2464 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2466 FIXME("(%p)->(%p) - stub\n", This
, id
);
2471 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2472 IAudioSessionControl2
*iface
, WCHAR
**id
)
2474 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2476 FIXME("(%p)->(%p) - stub\n", This
, id
);
2481 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2482 IAudioSessionControl2
*iface
, DWORD
*pid
)
2484 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2486 TRACE("(%p)->(%p)\n", This
, pid
);
2491 *pid
= GetCurrentProcessId();
2496 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2497 IAudioSessionControl2
*iface
)
2499 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2501 TRACE("(%p)\n", This
);
2506 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2507 IAudioSessionControl2
*iface
, BOOL optout
)
2509 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2511 TRACE("(%p)->(%d)\n", This
, optout
);
2516 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2518 AudioSessionControl_QueryInterface
,
2519 AudioSessionControl_AddRef
,
2520 AudioSessionControl_Release
,
2521 AudioSessionControl_GetState
,
2522 AudioSessionControl_GetDisplayName
,
2523 AudioSessionControl_SetDisplayName
,
2524 AudioSessionControl_GetIconPath
,
2525 AudioSessionControl_SetIconPath
,
2526 AudioSessionControl_GetGroupingParam
,
2527 AudioSessionControl_SetGroupingParam
,
2528 AudioSessionControl_RegisterAudioSessionNotification
,
2529 AudioSessionControl_UnregisterAudioSessionNotification
,
2530 AudioSessionControl_GetSessionIdentifier
,
2531 AudioSessionControl_GetSessionInstanceIdentifier
,
2532 AudioSessionControl_GetProcessId
,
2533 AudioSessionControl_IsSystemSoundsSession
,
2534 AudioSessionControl_SetDuckingPreference
2537 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2538 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2540 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2546 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2547 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2550 IUnknown_AddRef((IUnknown
*)*ppv
);
2554 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2555 return E_NOINTERFACE
;
2558 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2560 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2561 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2564 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2566 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2567 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2570 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2571 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2573 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2574 AudioSession
*session
= This
->session
;
2576 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2578 if(level
< 0.f
|| level
> 1.f
)
2579 return E_INVALIDARG
;
2582 FIXME("Notifications not supported yet\n");
2584 EnterCriticalSection(&session
->lock
);
2586 session
->master_vol
= level
;
2588 TRACE("OSS doesn't support setting volume\n");
2590 LeaveCriticalSection(&session
->lock
);
2595 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2596 ISimpleAudioVolume
*iface
, float *level
)
2598 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2599 AudioSession
*session
= This
->session
;
2601 TRACE("(%p)->(%p)\n", session
, level
);
2604 return NULL_PTR_ERR
;
2606 *level
= session
->master_vol
;
2611 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2612 BOOL mute
, const GUID
*context
)
2614 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2615 AudioSession
*session
= This
->session
;
2617 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2619 EnterCriticalSection(&session
->lock
);
2621 session
->mute
= mute
;
2623 LeaveCriticalSection(&session
->lock
);
2628 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2631 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2632 AudioSession
*session
= This
->session
;
2634 TRACE("(%p)->(%p)\n", session
, mute
);
2637 return NULL_PTR_ERR
;
2639 *mute
= This
->session
->mute
;
2644 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2646 SimpleAudioVolume_QueryInterface
,
2647 SimpleAudioVolume_AddRef
,
2648 SimpleAudioVolume_Release
,
2649 SimpleAudioVolume_SetMasterVolume
,
2650 SimpleAudioVolume_GetMasterVolume
,
2651 SimpleAudioVolume_SetMute
,
2652 SimpleAudioVolume_GetMute
2655 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2656 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2658 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2664 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2665 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2668 IUnknown_AddRef((IUnknown
*)*ppv
);
2672 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2673 return E_NOINTERFACE
;
2676 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2678 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2679 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2682 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2684 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2685 return IAudioClient_Release(&This
->IAudioClient_iface
);
2688 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2689 IAudioStreamVolume
*iface
, UINT32
*out
)
2691 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2693 TRACE("(%p)->(%p)\n", This
, out
);
2698 *out
= This
->fmt
->nChannels
;
2703 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2704 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2706 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2708 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2710 if(level
< 0.f
|| level
> 1.f
)
2711 return E_INVALIDARG
;
2713 if(index
>= This
->fmt
->nChannels
)
2714 return E_INVALIDARG
;
2716 EnterCriticalSection(&This
->lock
);
2718 This
->vols
[index
] = level
;
2720 TRACE("OSS doesn't support setting volume\n");
2722 LeaveCriticalSection(&This
->lock
);
2727 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2728 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2730 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2732 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2737 if(index
>= This
->fmt
->nChannels
)
2738 return E_INVALIDARG
;
2740 *level
= This
->vols
[index
];
2745 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2746 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2748 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2751 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2756 if(count
!= This
->fmt
->nChannels
)
2757 return E_INVALIDARG
;
2759 EnterCriticalSection(&This
->lock
);
2761 for(i
= 0; i
< count
; ++i
)
2762 This
->vols
[i
] = levels
[i
];
2764 TRACE("OSS doesn't support setting volume\n");
2766 LeaveCriticalSection(&This
->lock
);
2771 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2772 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2774 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2777 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2782 if(count
!= This
->fmt
->nChannels
)
2783 return E_INVALIDARG
;
2785 EnterCriticalSection(&This
->lock
);
2787 for(i
= 0; i
< count
; ++i
)
2788 levels
[i
] = This
->vols
[i
];
2790 LeaveCriticalSection(&This
->lock
);
2795 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2797 AudioStreamVolume_QueryInterface
,
2798 AudioStreamVolume_AddRef
,
2799 AudioStreamVolume_Release
,
2800 AudioStreamVolume_GetChannelCount
,
2801 AudioStreamVolume_SetChannelVolume
,
2802 AudioStreamVolume_GetChannelVolume
,
2803 AudioStreamVolume_SetAllVolumes
,
2804 AudioStreamVolume_GetAllVolumes
2807 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2808 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2810 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2816 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2817 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2820 IUnknown_AddRef((IUnknown
*)*ppv
);
2824 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2825 return E_NOINTERFACE
;
2828 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2830 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2831 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2834 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2836 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2837 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2840 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
2841 IChannelAudioVolume
*iface
, UINT32
*out
)
2843 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2844 AudioSession
*session
= This
->session
;
2846 TRACE("(%p)->(%p)\n", session
, out
);
2849 return NULL_PTR_ERR
;
2851 *out
= session
->channel_count
;
2856 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
2857 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
2858 const GUID
*context
)
2860 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2861 AudioSession
*session
= This
->session
;
2863 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
2864 wine_dbgstr_guid(context
));
2866 if(level
< 0.f
|| level
> 1.f
)
2867 return E_INVALIDARG
;
2869 if(index
>= session
->channel_count
)
2870 return E_INVALIDARG
;
2873 FIXME("Notifications not supported yet\n");
2875 EnterCriticalSection(&session
->lock
);
2877 session
->channel_vols
[index
] = level
;
2879 TRACE("OSS doesn't support setting volume\n");
2881 LeaveCriticalSection(&session
->lock
);
2886 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
2887 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
2889 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2890 AudioSession
*session
= This
->session
;
2892 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
2895 return NULL_PTR_ERR
;
2897 if(index
>= session
->channel_count
)
2898 return E_INVALIDARG
;
2900 *level
= session
->channel_vols
[index
];
2905 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
2906 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
2907 const GUID
*context
)
2909 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2910 AudioSession
*session
= This
->session
;
2913 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
2914 wine_dbgstr_guid(context
));
2917 return NULL_PTR_ERR
;
2919 if(count
!= session
->channel_count
)
2920 return E_INVALIDARG
;
2923 FIXME("Notifications not supported yet\n");
2925 EnterCriticalSection(&session
->lock
);
2927 for(i
= 0; i
< count
; ++i
)
2928 session
->channel_vols
[i
] = levels
[i
];
2930 TRACE("OSS doesn't support setting volume\n");
2932 LeaveCriticalSection(&session
->lock
);
2937 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
2938 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
2940 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2941 AudioSession
*session
= This
->session
;
2944 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
2947 return NULL_PTR_ERR
;
2949 if(count
!= session
->channel_count
)
2950 return E_INVALIDARG
;
2952 for(i
= 0; i
< count
; ++i
)
2953 levels
[i
] = session
->channel_vols
[i
];
2958 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
2960 ChannelAudioVolume_QueryInterface
,
2961 ChannelAudioVolume_AddRef
,
2962 ChannelAudioVolume_Release
,
2963 ChannelAudioVolume_GetChannelCount
,
2964 ChannelAudioVolume_SetChannelVolume
,
2965 ChannelAudioVolume_GetChannelVolume
,
2966 ChannelAudioVolume_SetAllVolumes
,
2967 ChannelAudioVolume_GetAllVolumes
2970 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
2971 REFIID riid
, void **ppv
)
2973 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2979 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2980 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
2981 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
2984 IUnknown_AddRef((IUnknown
*)*ppv
);
2988 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2989 return E_NOINTERFACE
;
2992 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
2994 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2996 ref
= InterlockedIncrement(&This
->ref
);
2997 TRACE("(%p) Refcount now %u\n", This
, ref
);
3001 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
3003 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3005 ref
= InterlockedDecrement(&This
->ref
);
3006 TRACE("(%p) Refcount now %u\n", This
, ref
);
3008 HeapFree(GetProcessHeap(), 0, This
);
3012 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3013 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3014 IAudioSessionControl
**out
)
3016 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3017 AudioSession
*session
;
3018 AudioSessionWrapper
*wrapper
;
3021 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3024 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3028 wrapper
= AudioSessionWrapper_Create(NULL
);
3030 return E_OUTOFMEMORY
;
3032 wrapper
->session
= session
;
3034 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3039 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3040 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3041 ISimpleAudioVolume
**out
)
3043 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3044 AudioSession
*session
;
3045 AudioSessionWrapper
*wrapper
;
3048 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3051 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3055 wrapper
= AudioSessionWrapper_Create(NULL
);
3057 return E_OUTOFMEMORY
;
3059 wrapper
->session
= session
;
3061 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3066 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3067 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3069 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3070 FIXME("(%p)->(%p) - stub\n", This
, out
);
3074 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3075 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3077 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3078 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3082 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3083 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3085 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3086 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3090 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3091 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3092 IAudioVolumeDuckNotification
*notification
)
3094 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3095 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3099 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3100 IAudioSessionManager2
*iface
,
3101 IAudioVolumeDuckNotification
*notification
)
3103 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3104 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3108 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3110 AudioSessionManager_QueryInterface
,
3111 AudioSessionManager_AddRef
,
3112 AudioSessionManager_Release
,
3113 AudioSessionManager_GetAudioSessionControl
,
3114 AudioSessionManager_GetSimpleAudioVolume
,
3115 AudioSessionManager_GetSessionEnumerator
,
3116 AudioSessionManager_RegisterSessionNotification
,
3117 AudioSessionManager_UnregisterSessionNotification
,
3118 AudioSessionManager_RegisterDuckNotification
,
3119 AudioSessionManager_UnregisterDuckNotification
3122 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3123 IAudioSessionManager2
**out
)
3127 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3129 return E_OUTOFMEMORY
;
3131 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3132 This
->device
= device
;
3135 *out
= &This
->IAudioSessionManager2_iface
;