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 oss_silence_buffer(ACImpl
*This
, BYTE
*buf
, UINT32 frames
)
1372 if(This
->fmt
->wBitsPerSample
== 8)
1373 memset(buf
, 128, frames
* This
->fmt
->nBlockAlign
);
1375 memset(buf
, 0, frames
* This
->fmt
->nBlockAlign
);
1378 static void oss_write_data(ACImpl
*This
)
1380 ssize_t written_bytes
;
1381 UINT32 written_frames
, in_oss_frames
, write_limit
, max_period
;
1382 size_t to_write_frames
, to_write_bytes
;
1385 This
->local_buffer
+ (This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
);
1387 if(This
->held_frames
== 0)
1390 if(This
->lcl_offs_frames
+ This
->held_frames
> This
->bufsize_frames
)
1391 to_write_frames
= This
->bufsize_frames
- This
->lcl_offs_frames
;
1393 to_write_frames
= This
->held_frames
;
1395 if(ioctl(This
->fd
, SNDCTL_DSP_GETOSPACE
, &bi
) < 0){
1396 WARN("GETOSPACE failed: %d (%s)\n", errno
, strerror(errno
));
1400 max_period
= max(bi
.fragsize
/ This
->fmt
->nBlockAlign
, This
->period_frames
);
1402 if(bi
.bytes
> This
->oss_bufsize_bytes
){
1403 TRACE("New buffer size (%u) is larger than old buffer size (%u)\n",
1404 bi
.bytes
, This
->oss_bufsize_bytes
);
1405 This
->oss_bufsize_bytes
= bi
.bytes
;
1407 }else if(This
->oss_bufsize_bytes
- bi
.bytes
<= bi
.fragsize
)
1410 in_oss_frames
= (This
->oss_bufsize_bytes
- bi
.bytes
) / This
->fmt
->nBlockAlign
;
1413 while(write_limit
+ in_oss_frames
< max_period
* 3)
1414 write_limit
+= max_period
;
1415 if(write_limit
== 0)
1418 to_write_frames
= min(to_write_frames
, write_limit
);
1419 to_write_bytes
= to_write_frames
* This
->fmt
->nBlockAlign
;
1421 if(This
->session
->mute
)
1422 oss_silence_buffer(This
, buf
, to_write_frames
);
1424 written_bytes
= write(This
->fd
, buf
, to_write_bytes
);
1425 if(written_bytes
< 0){
1426 /* EAGAIN is OSS buffer full, log that too */
1427 WARN("write failed: %d (%s)\n", errno
, strerror(errno
));
1430 written_frames
= written_bytes
/ This
->fmt
->nBlockAlign
;
1432 This
->lcl_offs_frames
+= written_frames
;
1433 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1434 This
->held_frames
-= written_frames
;
1436 if(written_frames
< to_write_frames
){
1437 /* OSS buffer probably full */
1441 if(This
->held_frames
&& written_frames
< write_limit
){
1442 /* wrapped and have some data back at the start to write */
1444 to_write_frames
= min(write_limit
- written_frames
, This
->held_frames
);
1445 to_write_bytes
= to_write_frames
* This
->fmt
->nBlockAlign
;
1447 if(This
->session
->mute
)
1448 oss_silence_buffer(This
, This
->local_buffer
, to_write_frames
);
1450 written_bytes
= write(This
->fd
, This
->local_buffer
, to_write_bytes
);
1451 if(written_bytes
< 0){
1452 WARN("write failed: %d (%s)\n", errno
, strerror(errno
));
1455 written_frames
= written_bytes
/ This
->fmt
->nBlockAlign
;
1457 This
->lcl_offs_frames
+= written_frames
;
1458 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1459 This
->held_frames
-= written_frames
;
1463 static void oss_read_data(ACImpl
*This
)
1465 UINT64 pos
, readable
;
1469 if(ioctl(This
->fd
, SNDCTL_DSP_GETISPACE
, &bi
) < 0){
1470 WARN("GETISPACE failed: %d (%s)\n", errno
, strerror(errno
));
1474 pos
= (This
->held_frames
+ This
->lcl_offs_frames
) % This
->bufsize_frames
;
1475 readable
= (This
->bufsize_frames
- pos
) * This
->fmt
->nBlockAlign
;
1477 if(bi
.bytes
< readable
)
1478 readable
= bi
.bytes
;
1480 nread
= read(This
->fd
, This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
,
1483 WARN("read failed: %d (%s)\n", errno
, strerror(errno
));
1487 This
->held_frames
+= nread
/ This
->fmt
->nBlockAlign
;
1489 if(This
->held_frames
> This
->bufsize_frames
){
1490 WARN("Overflow of unread data\n");
1491 This
->lcl_offs_frames
+= This
->held_frames
;
1492 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1493 This
->held_frames
= This
->bufsize_frames
;
1497 static void CALLBACK
oss_period_callback(void *user
, BOOLEAN timer
)
1499 ACImpl
*This
= user
;
1501 EnterCriticalSection(&This
->lock
);
1504 if(This
->dataflow
== eRender
&& This
->held_frames
)
1505 oss_write_data(This
);
1506 else if(This
->dataflow
== eCapture
)
1507 oss_read_data(This
);
1510 LeaveCriticalSection(&This
->lock
);
1513 SetEvent(This
->event
);
1516 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1518 ACImpl
*This
= impl_from_IAudioClient(iface
);
1520 TRACE("(%p)\n", This
);
1522 EnterCriticalSection(&This
->lock
);
1525 LeaveCriticalSection(&This
->lock
);
1526 return AUDCLNT_E_NOT_INITIALIZED
;
1529 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1530 LeaveCriticalSection(&This
->lock
);
1531 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1535 LeaveCriticalSection(&This
->lock
);
1536 return AUDCLNT_E_NOT_STOPPED
;
1539 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
,
1540 oss_period_callback
, This
, 0, This
->period_us
/ 1000,
1541 WT_EXECUTEINTIMERTHREAD
))
1542 ERR("Unable to create period timer: %u\n", GetLastError());
1544 This
->playing
= TRUE
;
1546 LeaveCriticalSection(&This
->lock
);
1551 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1553 ACImpl
*This
= impl_from_IAudioClient(iface
);
1557 TRACE("(%p)\n", This
);
1559 EnterCriticalSection(&This
->lock
);
1562 LeaveCriticalSection(&This
->lock
);
1563 return AUDCLNT_E_NOT_INITIALIZED
;
1567 LeaveCriticalSection(&This
->lock
);
1571 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1572 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
1574 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1575 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
1577 This
->playing
= FALSE
;
1579 LeaveCriticalSection(&This
->lock
);
1582 WaitForSingleObject(event
, INFINITE
);
1588 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1590 ACImpl
*This
= impl_from_IAudioClient(iface
);
1592 TRACE("(%p)\n", This
);
1594 EnterCriticalSection(&This
->lock
);
1597 LeaveCriticalSection(&This
->lock
);
1598 return AUDCLNT_E_NOT_INITIALIZED
;
1602 LeaveCriticalSection(&This
->lock
);
1603 return AUDCLNT_E_NOT_STOPPED
;
1606 if(This
->getbuf_last
){
1607 LeaveCriticalSection(&This
->lock
);
1608 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
1611 if(This
->dataflow
== eRender
){
1612 This
->written_frames
= 0;
1613 This
->last_pos_frames
= 0;
1615 This
->written_frames
+= This
->held_frames
;
1617 This
->held_frames
= 0;
1618 This
->lcl_offs_frames
= 0;
1620 LeaveCriticalSection(&This
->lock
);
1625 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1628 ACImpl
*This
= impl_from_IAudioClient(iface
);
1630 TRACE("(%p)->(%p)\n", This
, event
);
1633 return E_INVALIDARG
;
1635 EnterCriticalSection(&This
->lock
);
1638 LeaveCriticalSection(&This
->lock
);
1639 return AUDCLNT_E_NOT_INITIALIZED
;
1642 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1643 LeaveCriticalSection(&This
->lock
);
1644 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1648 LeaveCriticalSection(&This
->lock
);
1649 FIXME("called twice\n");
1650 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
1653 This
->event
= event
;
1655 LeaveCriticalSection(&This
->lock
);
1660 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1663 ACImpl
*This
= impl_from_IAudioClient(iface
);
1665 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1671 EnterCriticalSection(&This
->lock
);
1674 LeaveCriticalSection(&This
->lock
);
1675 return AUDCLNT_E_NOT_INITIALIZED
;
1678 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1679 if(This
->dataflow
!= eRender
){
1680 LeaveCriticalSection(&This
->lock
);
1681 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1683 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
1684 *ppv
= &This
->IAudioRenderClient_iface
;
1685 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1686 if(This
->dataflow
!= eCapture
){
1687 LeaveCriticalSection(&This
->lock
);
1688 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1690 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
1691 *ppv
= &This
->IAudioCaptureClient_iface
;
1692 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1693 IAudioClock_AddRef(&This
->IAudioClock_iface
);
1694 *ppv
= &This
->IAudioClock_iface
;
1695 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1696 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
1697 *ppv
= &This
->IAudioStreamVolume_iface
;
1698 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1699 if(!This
->session_wrapper
){
1700 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1701 if(!This
->session_wrapper
){
1702 LeaveCriticalSection(&This
->lock
);
1703 return E_OUTOFMEMORY
;
1706 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
1708 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1709 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1710 if(!This
->session_wrapper
){
1711 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1712 if(!This
->session_wrapper
){
1713 LeaveCriticalSection(&This
->lock
);
1714 return E_OUTOFMEMORY
;
1717 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
1719 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1720 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1721 if(!This
->session_wrapper
){
1722 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1723 if(!This
->session_wrapper
){
1724 LeaveCriticalSection(&This
->lock
);
1725 return E_OUTOFMEMORY
;
1728 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
1730 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1734 LeaveCriticalSection(&This
->lock
);
1738 LeaveCriticalSection(&This
->lock
);
1740 FIXME("stub %s\n", debugstr_guid(riid
));
1741 return E_NOINTERFACE
;
1744 static const IAudioClientVtbl AudioClient_Vtbl
=
1746 AudioClient_QueryInterface
,
1748 AudioClient_Release
,
1749 AudioClient_Initialize
,
1750 AudioClient_GetBufferSize
,
1751 AudioClient_GetStreamLatency
,
1752 AudioClient_GetCurrentPadding
,
1753 AudioClient_IsFormatSupported
,
1754 AudioClient_GetMixFormat
,
1755 AudioClient_GetDevicePeriod
,
1759 AudioClient_SetEventHandle
,
1760 AudioClient_GetService
1763 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1764 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1766 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1767 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1773 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1774 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1776 else if(IsEqualIID(riid
, &IID_IMarshal
))
1777 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
1779 IUnknown_AddRef((IUnknown
*)*ppv
);
1783 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1784 return E_NOINTERFACE
;
1787 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1789 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1790 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1793 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1795 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1796 return AudioClient_Release(&This
->IAudioClient_iface
);
1799 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1800 UINT32 frames
, BYTE
**data
)
1802 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1805 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1812 EnterCriticalSection(&This
->lock
);
1814 if(This
->getbuf_last
){
1815 LeaveCriticalSection(&This
->lock
);
1816 return AUDCLNT_E_OUT_OF_ORDER
;
1820 LeaveCriticalSection(&This
->lock
);
1824 if(This
->held_frames
+ frames
> This
->bufsize_frames
){
1825 LeaveCriticalSection(&This
->lock
);
1826 return AUDCLNT_E_BUFFER_TOO_LARGE
;
1830 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
1831 if(write_pos
+ frames
> This
->bufsize_frames
){
1832 if(This
->tmp_buffer_frames
< frames
){
1833 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
1834 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
1835 frames
* This
->fmt
->nBlockAlign
);
1836 if(!This
->tmp_buffer
){
1837 LeaveCriticalSection(&This
->lock
);
1838 return E_OUTOFMEMORY
;
1840 This
->tmp_buffer_frames
= frames
;
1842 *data
= This
->tmp_buffer
;
1843 This
->getbuf_last
= -frames
;
1845 *data
= This
->local_buffer
+ write_pos
* This
->fmt
->nBlockAlign
;
1846 This
->getbuf_last
= frames
;
1849 LeaveCriticalSection(&This
->lock
);
1854 static void oss_wrap_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 written_frames
)
1856 UINT32 write_offs_frames
=
1857 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
1858 UINT32 write_offs_bytes
= write_offs_frames
* This
->fmt
->nBlockAlign
;
1859 UINT32 chunk_frames
= This
->bufsize_frames
- write_offs_frames
;
1860 UINT32 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
1861 UINT32 written_bytes
= written_frames
* This
->fmt
->nBlockAlign
;
1863 if(written_bytes
<= chunk_bytes
){
1864 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, written_bytes
);
1866 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, chunk_bytes
);
1867 memcpy(This
->local_buffer
, buffer
+ chunk_bytes
,
1868 written_bytes
- chunk_bytes
);
1872 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
1873 IAudioRenderClient
*iface
, UINT32 written_frames
, DWORD flags
)
1875 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1878 TRACE("(%p)->(%u, %x)\n", This
, written_frames
, flags
);
1880 EnterCriticalSection(&This
->lock
);
1882 if(!written_frames
){
1883 This
->getbuf_last
= 0;
1884 LeaveCriticalSection(&This
->lock
);
1888 if(!This
->getbuf_last
){
1889 LeaveCriticalSection(&This
->lock
);
1890 return AUDCLNT_E_OUT_OF_ORDER
;
1893 if(written_frames
> (This
->getbuf_last
>= 0 ? This
->getbuf_last
: -This
->getbuf_last
)){
1894 LeaveCriticalSection(&This
->lock
);
1895 return AUDCLNT_E_INVALID_SIZE
;
1898 if(This
->getbuf_last
>= 0)
1899 buffer
= This
->local_buffer
+ This
->fmt
->nBlockAlign
*
1900 ((This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
);
1902 buffer
= This
->tmp_buffer
;
1904 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
)
1905 oss_silence_buffer(This
, buffer
, written_frames
);
1907 if(This
->getbuf_last
< 0)
1908 oss_wrap_buffer(This
, buffer
, written_frames
);
1910 This
->held_frames
+= written_frames
;
1911 This
->written_frames
+= written_frames
;
1912 This
->getbuf_last
= 0;
1914 LeaveCriticalSection(&This
->lock
);
1919 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
1920 AudioRenderClient_QueryInterface
,
1921 AudioRenderClient_AddRef
,
1922 AudioRenderClient_Release
,
1923 AudioRenderClient_GetBuffer
,
1924 AudioRenderClient_ReleaseBuffer
1927 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
1928 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
1930 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1931 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1937 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1938 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
1940 else if(IsEqualIID(riid
, &IID_IMarshal
))
1941 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
1943 IUnknown_AddRef((IUnknown
*)*ppv
);
1947 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1948 return E_NOINTERFACE
;
1951 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
1953 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1954 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
1957 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
1959 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1960 return IAudioClient_Release(&This
->IAudioClient_iface
);
1963 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
1964 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
1967 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1969 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
1972 if(!data
|| !frames
|| !flags
)
1975 EnterCriticalSection(&This
->lock
);
1977 if(This
->getbuf_last
){
1978 LeaveCriticalSection(&This
->lock
);
1979 return AUDCLNT_E_OUT_OF_ORDER
;
1982 if(This
->held_frames
< This
->period_frames
){
1984 LeaveCriticalSection(&This
->lock
);
1985 return AUDCLNT_S_BUFFER_EMPTY
;
1990 *frames
= This
->period_frames
;
1992 if(This
->lcl_offs_frames
+ *frames
> This
->bufsize_frames
){
1993 UINT32 chunk_bytes
, offs_bytes
, frames_bytes
;
1994 if(This
->tmp_buffer_frames
< *frames
){
1995 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
1996 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
1997 *frames
* This
->fmt
->nBlockAlign
);
1998 if(!This
->tmp_buffer
){
1999 LeaveCriticalSection(&This
->lock
);
2000 return E_OUTOFMEMORY
;
2002 This
->tmp_buffer_frames
= *frames
;
2005 *data
= This
->tmp_buffer
;
2006 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) *
2007 This
->fmt
->nBlockAlign
;
2008 offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2009 frames_bytes
= *frames
* This
->fmt
->nBlockAlign
;
2010 memcpy(This
->tmp_buffer
, This
->local_buffer
+ offs_bytes
, chunk_bytes
);
2011 memcpy(This
->tmp_buffer
+ chunk_bytes
, This
->local_buffer
,
2012 frames_bytes
- chunk_bytes
);
2014 *data
= This
->local_buffer
+
2015 This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2017 This
->getbuf_last
= *frames
;
2020 *devpos
= This
->written_frames
;
2022 LARGE_INTEGER stamp
, freq
;
2023 QueryPerformanceCounter(&stamp
);
2024 QueryPerformanceFrequency(&freq
);
2025 *qpcpos
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2028 LeaveCriticalSection(&This
->lock
);
2030 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
2033 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2034 IAudioCaptureClient
*iface
, UINT32 done
)
2036 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2038 TRACE("(%p)->(%u)\n", This
, done
);
2040 EnterCriticalSection(&This
->lock
);
2043 This
->getbuf_last
= 0;
2044 LeaveCriticalSection(&This
->lock
);
2048 if(!This
->getbuf_last
){
2049 LeaveCriticalSection(&This
->lock
);
2050 return AUDCLNT_E_OUT_OF_ORDER
;
2053 if(This
->getbuf_last
!= done
){
2054 LeaveCriticalSection(&This
->lock
);
2055 return AUDCLNT_E_INVALID_SIZE
;
2058 This
->written_frames
+= done
;
2059 This
->held_frames
-= done
;
2060 This
->lcl_offs_frames
+= done
;
2061 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2062 This
->getbuf_last
= 0;
2064 LeaveCriticalSection(&This
->lock
);
2069 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2070 IAudioCaptureClient
*iface
, UINT32
*frames
)
2072 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2074 TRACE("(%p)->(%p)\n", This
, frames
);
2079 EnterCriticalSection(&This
->lock
);
2081 *frames
= This
->held_frames
< This
->period_frames
? 0 : This
->period_frames
;
2083 LeaveCriticalSection(&This
->lock
);
2088 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2090 AudioCaptureClient_QueryInterface
,
2091 AudioCaptureClient_AddRef
,
2092 AudioCaptureClient_Release
,
2093 AudioCaptureClient_GetBuffer
,
2094 AudioCaptureClient_ReleaseBuffer
,
2095 AudioCaptureClient_GetNextPacketSize
2098 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2099 REFIID riid
, void **ppv
)
2101 ACImpl
*This
= impl_from_IAudioClock(iface
);
2103 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2109 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2111 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2112 *ppv
= &This
->IAudioClock2_iface
;
2114 IUnknown_AddRef((IUnknown
*)*ppv
);
2118 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2119 return E_NOINTERFACE
;
2122 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2124 ACImpl
*This
= impl_from_IAudioClock(iface
);
2125 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2128 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2130 ACImpl
*This
= impl_from_IAudioClock(iface
);
2131 return IAudioClient_Release(&This
->IAudioClient_iface
);
2134 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2136 ACImpl
*This
= impl_from_IAudioClock(iface
);
2138 TRACE("(%p)->(%p)\n", This
, freq
);
2140 *freq
= This
->fmt
->nSamplesPerSec
;
2145 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2148 ACImpl
*This
= impl_from_IAudioClock(iface
);
2151 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2156 EnterCriticalSection(&This
->lock
);
2158 if(This
->dataflow
== eRender
){
2159 if(!This
->playing
|| !This
->held_frames
||
2160 ioctl(This
->fd
, SNDCTL_DSP_GETODELAY
, &delay
) < 0)
2163 delay
/= This
->fmt
->nBlockAlign
;
2164 if(This
->held_frames
+ delay
>= This
->written_frames
)
2165 *pos
= This
->last_pos_frames
;
2167 *pos
= This
->written_frames
- This
->held_frames
- delay
;
2168 if(*pos
< This
->last_pos_frames
)
2169 *pos
= This
->last_pos_frames
;
2171 }else if(This
->dataflow
== eCapture
){
2175 if(ioctl(This
->fd
, SNDCTL_DSP_GETISPACE
, &bi
) < 0){
2176 TRACE("GETISPACE failed: %d (%s)\n", errno
, strerror(errno
));
2179 if(bi
.bytes
<= bi
.fragsize
)
2182 held
= bi
.bytes
/ This
->fmt
->nBlockAlign
;
2185 *pos
= This
->written_frames
+ held
;
2188 This
->last_pos_frames
= *pos
;
2190 LeaveCriticalSection(&This
->lock
);
2193 LARGE_INTEGER stamp
, freq
;
2194 QueryPerformanceCounter(&stamp
);
2195 QueryPerformanceFrequency(&freq
);
2196 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2202 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2205 ACImpl
*This
= impl_from_IAudioClock(iface
);
2207 TRACE("(%p)->(%p)\n", This
, chars
);
2212 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2217 static const IAudioClockVtbl AudioClock_Vtbl
=
2219 AudioClock_QueryInterface
,
2222 AudioClock_GetFrequency
,
2223 AudioClock_GetPosition
,
2224 AudioClock_GetCharacteristics
2227 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2228 REFIID riid
, void **ppv
)
2230 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2231 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2234 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2236 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2237 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2240 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2242 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2243 return IAudioClient_Release(&This
->IAudioClient_iface
);
2246 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2247 UINT64
*pos
, UINT64
*qpctime
)
2249 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2251 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2256 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2258 AudioClock2_QueryInterface
,
2260 AudioClock2_Release
,
2261 AudioClock2_GetDevicePosition
2264 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2266 AudioSessionWrapper
*ret
;
2268 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2269 sizeof(AudioSessionWrapper
));
2273 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2274 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2275 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2279 ret
->client
= client
;
2281 ret
->session
= client
->session
;
2282 AudioClient_AddRef(&client
->IAudioClient_iface
);
2288 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2289 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2291 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2297 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2298 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2299 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2302 IUnknown_AddRef((IUnknown
*)*ppv
);
2306 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2307 return E_NOINTERFACE
;
2310 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2312 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2314 ref
= InterlockedIncrement(&This
->ref
);
2315 TRACE("(%p) Refcount now %u\n", This
, ref
);
2319 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2321 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2323 ref
= InterlockedDecrement(&This
->ref
);
2324 TRACE("(%p) Refcount now %u\n", This
, ref
);
2327 EnterCriticalSection(&This
->client
->lock
);
2328 This
->client
->session_wrapper
= NULL
;
2329 LeaveCriticalSection(&This
->client
->lock
);
2330 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2332 HeapFree(GetProcessHeap(), 0, This
);
2337 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2338 AudioSessionState
*state
)
2340 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2343 TRACE("(%p)->(%p)\n", This
, state
);
2346 return NULL_PTR_ERR
;
2348 EnterCriticalSection(&g_sessions_lock
);
2350 if(list_empty(&This
->session
->clients
)){
2351 *state
= AudioSessionStateExpired
;
2352 LeaveCriticalSection(&g_sessions_lock
);
2356 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2357 EnterCriticalSection(&client
->lock
);
2358 if(client
->playing
){
2359 *state
= AudioSessionStateActive
;
2360 LeaveCriticalSection(&client
->lock
);
2361 LeaveCriticalSection(&g_sessions_lock
);
2364 LeaveCriticalSection(&client
->lock
);
2367 LeaveCriticalSection(&g_sessions_lock
);
2369 *state
= AudioSessionStateInactive
;
2374 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2375 IAudioSessionControl2
*iface
, WCHAR
**name
)
2377 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2379 FIXME("(%p)->(%p) - stub\n", This
, name
);
2384 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2385 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2387 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2389 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2394 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2395 IAudioSessionControl2
*iface
, WCHAR
**path
)
2397 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2399 FIXME("(%p)->(%p) - stub\n", This
, path
);
2404 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2405 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2407 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2409 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2414 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2415 IAudioSessionControl2
*iface
, GUID
*group
)
2417 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2419 FIXME("(%p)->(%p) - stub\n", This
, group
);
2424 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2425 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2427 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2429 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2430 debugstr_guid(session
));
2435 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2436 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2438 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2440 FIXME("(%p)->(%p) - stub\n", This
, events
);
2445 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2446 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2448 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2450 FIXME("(%p)->(%p) - stub\n", This
, events
);
2455 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2456 IAudioSessionControl2
*iface
, WCHAR
**id
)
2458 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2460 FIXME("(%p)->(%p) - stub\n", This
, id
);
2465 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2466 IAudioSessionControl2
*iface
, WCHAR
**id
)
2468 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2470 FIXME("(%p)->(%p) - stub\n", This
, id
);
2475 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2476 IAudioSessionControl2
*iface
, DWORD
*pid
)
2478 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2480 TRACE("(%p)->(%p)\n", This
, pid
);
2485 *pid
= GetCurrentProcessId();
2490 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2491 IAudioSessionControl2
*iface
)
2493 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2495 TRACE("(%p)\n", This
);
2500 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2501 IAudioSessionControl2
*iface
, BOOL optout
)
2503 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2505 TRACE("(%p)->(%d)\n", This
, optout
);
2510 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2512 AudioSessionControl_QueryInterface
,
2513 AudioSessionControl_AddRef
,
2514 AudioSessionControl_Release
,
2515 AudioSessionControl_GetState
,
2516 AudioSessionControl_GetDisplayName
,
2517 AudioSessionControl_SetDisplayName
,
2518 AudioSessionControl_GetIconPath
,
2519 AudioSessionControl_SetIconPath
,
2520 AudioSessionControl_GetGroupingParam
,
2521 AudioSessionControl_SetGroupingParam
,
2522 AudioSessionControl_RegisterAudioSessionNotification
,
2523 AudioSessionControl_UnregisterAudioSessionNotification
,
2524 AudioSessionControl_GetSessionIdentifier
,
2525 AudioSessionControl_GetSessionInstanceIdentifier
,
2526 AudioSessionControl_GetProcessId
,
2527 AudioSessionControl_IsSystemSoundsSession
,
2528 AudioSessionControl_SetDuckingPreference
2531 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2532 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2534 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2540 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2541 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2544 IUnknown_AddRef((IUnknown
*)*ppv
);
2548 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2549 return E_NOINTERFACE
;
2552 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2554 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2555 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2558 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2560 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2561 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2564 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2565 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2567 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2568 AudioSession
*session
= This
->session
;
2570 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2572 if(level
< 0.f
|| level
> 1.f
)
2573 return E_INVALIDARG
;
2576 FIXME("Notifications not supported yet\n");
2578 EnterCriticalSection(&session
->lock
);
2580 session
->master_vol
= level
;
2582 TRACE("OSS doesn't support setting volume\n");
2584 LeaveCriticalSection(&session
->lock
);
2589 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2590 ISimpleAudioVolume
*iface
, float *level
)
2592 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2593 AudioSession
*session
= This
->session
;
2595 TRACE("(%p)->(%p)\n", session
, level
);
2598 return NULL_PTR_ERR
;
2600 *level
= session
->master_vol
;
2605 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2606 BOOL mute
, const GUID
*context
)
2608 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2609 AudioSession
*session
= This
->session
;
2611 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2613 EnterCriticalSection(&session
->lock
);
2615 session
->mute
= mute
;
2617 LeaveCriticalSection(&session
->lock
);
2622 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2625 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2626 AudioSession
*session
= This
->session
;
2628 TRACE("(%p)->(%p)\n", session
, mute
);
2631 return NULL_PTR_ERR
;
2633 *mute
= This
->session
->mute
;
2638 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2640 SimpleAudioVolume_QueryInterface
,
2641 SimpleAudioVolume_AddRef
,
2642 SimpleAudioVolume_Release
,
2643 SimpleAudioVolume_SetMasterVolume
,
2644 SimpleAudioVolume_GetMasterVolume
,
2645 SimpleAudioVolume_SetMute
,
2646 SimpleAudioVolume_GetMute
2649 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2650 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2652 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2658 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2659 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2662 IUnknown_AddRef((IUnknown
*)*ppv
);
2666 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2667 return E_NOINTERFACE
;
2670 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2672 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2673 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2676 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2678 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2679 return IAudioClient_Release(&This
->IAudioClient_iface
);
2682 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2683 IAudioStreamVolume
*iface
, UINT32
*out
)
2685 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2687 TRACE("(%p)->(%p)\n", This
, out
);
2692 *out
= This
->fmt
->nChannels
;
2697 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2698 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2700 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2702 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2704 if(level
< 0.f
|| level
> 1.f
)
2705 return E_INVALIDARG
;
2707 if(index
>= This
->fmt
->nChannels
)
2708 return E_INVALIDARG
;
2710 EnterCriticalSection(&This
->lock
);
2712 This
->vols
[index
] = level
;
2714 TRACE("OSS doesn't support setting volume\n");
2716 LeaveCriticalSection(&This
->lock
);
2721 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2722 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2724 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2726 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2731 if(index
>= This
->fmt
->nChannels
)
2732 return E_INVALIDARG
;
2734 *level
= This
->vols
[index
];
2739 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2740 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2742 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2745 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2750 if(count
!= This
->fmt
->nChannels
)
2751 return E_INVALIDARG
;
2753 EnterCriticalSection(&This
->lock
);
2755 for(i
= 0; i
< count
; ++i
)
2756 This
->vols
[i
] = levels
[i
];
2758 TRACE("OSS doesn't support setting volume\n");
2760 LeaveCriticalSection(&This
->lock
);
2765 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2766 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2768 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2771 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2776 if(count
!= This
->fmt
->nChannels
)
2777 return E_INVALIDARG
;
2779 EnterCriticalSection(&This
->lock
);
2781 for(i
= 0; i
< count
; ++i
)
2782 levels
[i
] = This
->vols
[i
];
2784 LeaveCriticalSection(&This
->lock
);
2789 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2791 AudioStreamVolume_QueryInterface
,
2792 AudioStreamVolume_AddRef
,
2793 AudioStreamVolume_Release
,
2794 AudioStreamVolume_GetChannelCount
,
2795 AudioStreamVolume_SetChannelVolume
,
2796 AudioStreamVolume_GetChannelVolume
,
2797 AudioStreamVolume_SetAllVolumes
,
2798 AudioStreamVolume_GetAllVolumes
2801 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2802 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2804 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2810 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2811 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2814 IUnknown_AddRef((IUnknown
*)*ppv
);
2818 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2819 return E_NOINTERFACE
;
2822 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2824 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2825 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2828 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2830 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2831 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2834 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
2835 IChannelAudioVolume
*iface
, UINT32
*out
)
2837 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2838 AudioSession
*session
= This
->session
;
2840 TRACE("(%p)->(%p)\n", session
, out
);
2843 return NULL_PTR_ERR
;
2845 *out
= session
->channel_count
;
2850 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
2851 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
2852 const GUID
*context
)
2854 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2855 AudioSession
*session
= This
->session
;
2857 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
2858 wine_dbgstr_guid(context
));
2860 if(level
< 0.f
|| level
> 1.f
)
2861 return E_INVALIDARG
;
2863 if(index
>= session
->channel_count
)
2864 return E_INVALIDARG
;
2867 FIXME("Notifications not supported yet\n");
2869 EnterCriticalSection(&session
->lock
);
2871 session
->channel_vols
[index
] = level
;
2873 TRACE("OSS doesn't support setting volume\n");
2875 LeaveCriticalSection(&session
->lock
);
2880 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
2881 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
2883 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2884 AudioSession
*session
= This
->session
;
2886 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
2889 return NULL_PTR_ERR
;
2891 if(index
>= session
->channel_count
)
2892 return E_INVALIDARG
;
2894 *level
= session
->channel_vols
[index
];
2899 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
2900 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
2901 const GUID
*context
)
2903 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2904 AudioSession
*session
= This
->session
;
2907 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
2908 wine_dbgstr_guid(context
));
2911 return NULL_PTR_ERR
;
2913 if(count
!= session
->channel_count
)
2914 return E_INVALIDARG
;
2917 FIXME("Notifications not supported yet\n");
2919 EnterCriticalSection(&session
->lock
);
2921 for(i
= 0; i
< count
; ++i
)
2922 session
->channel_vols
[i
] = levels
[i
];
2924 TRACE("OSS doesn't support setting volume\n");
2926 LeaveCriticalSection(&session
->lock
);
2931 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
2932 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
2934 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2935 AudioSession
*session
= This
->session
;
2938 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
2941 return NULL_PTR_ERR
;
2943 if(count
!= session
->channel_count
)
2944 return E_INVALIDARG
;
2946 for(i
= 0; i
< count
; ++i
)
2947 levels
[i
] = session
->channel_vols
[i
];
2952 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
2954 ChannelAudioVolume_QueryInterface
,
2955 ChannelAudioVolume_AddRef
,
2956 ChannelAudioVolume_Release
,
2957 ChannelAudioVolume_GetChannelCount
,
2958 ChannelAudioVolume_SetChannelVolume
,
2959 ChannelAudioVolume_GetChannelVolume
,
2960 ChannelAudioVolume_SetAllVolumes
,
2961 ChannelAudioVolume_GetAllVolumes
2964 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
2965 REFIID riid
, void **ppv
)
2967 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2973 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2974 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
2975 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
2978 IUnknown_AddRef((IUnknown
*)*ppv
);
2982 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2983 return E_NOINTERFACE
;
2986 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
2988 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2990 ref
= InterlockedIncrement(&This
->ref
);
2991 TRACE("(%p) Refcount now %u\n", This
, ref
);
2995 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
2997 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2999 ref
= InterlockedDecrement(&This
->ref
);
3000 TRACE("(%p) Refcount now %u\n", This
, ref
);
3002 HeapFree(GetProcessHeap(), 0, This
);
3006 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3007 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3008 IAudioSessionControl
**out
)
3010 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3011 AudioSession
*session
;
3012 AudioSessionWrapper
*wrapper
;
3015 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3018 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3022 wrapper
= AudioSessionWrapper_Create(NULL
);
3024 return E_OUTOFMEMORY
;
3026 wrapper
->session
= session
;
3028 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3033 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3034 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3035 ISimpleAudioVolume
**out
)
3037 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3038 AudioSession
*session
;
3039 AudioSessionWrapper
*wrapper
;
3042 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3045 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3049 wrapper
= AudioSessionWrapper_Create(NULL
);
3051 return E_OUTOFMEMORY
;
3053 wrapper
->session
= session
;
3055 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3060 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3061 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3063 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3064 FIXME("(%p)->(%p) - stub\n", This
, out
);
3068 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3069 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3071 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3072 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3076 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3077 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3079 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3080 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3084 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3085 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3086 IAudioVolumeDuckNotification
*notification
)
3088 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3089 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3093 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3094 IAudioSessionManager2
*iface
,
3095 IAudioVolumeDuckNotification
*notification
)
3097 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3098 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3102 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3104 AudioSessionManager_QueryInterface
,
3105 AudioSessionManager_AddRef
,
3106 AudioSessionManager_Release
,
3107 AudioSessionManager_GetAudioSessionControl
,
3108 AudioSessionManager_GetSimpleAudioVolume
,
3109 AudioSessionManager_GetSessionEnumerator
,
3110 AudioSessionManager_RegisterSessionNotification
,
3111 AudioSessionManager_UnregisterSessionNotification
,
3112 AudioSessionManager_RegisterDuckNotification
,
3113 AudioSessionManager_UnregisterDuckNotification
3116 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3117 IAudioSessionManager2
**out
)
3121 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3123 return E_OUTOFMEMORY
;
3125 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3126 This
->device
= device
;
3129 *out
= &This
->IAudioSessionManager2_iface
;