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
23 #define LoadResource __carbon_LoadResource
24 #define CompareString __carbon_CompareString
25 #define GetCurrentThread __carbon_GetCurrentThread
26 #define GetCurrentProcess __carbon_GetCurrentProcess
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
41 #include <libkern/OSAtomic.h>
42 #include <CoreAudio/CoreAudio.h>
43 #include <AudioToolbox/AudioFormat.h>
44 #include <AudioToolbox/AudioConverter.h>
45 #include <AudioUnit/AudioUnit.h>
49 #undef GetCurrentThread
50 #undef GetCurrentProcess
58 #include "wine/debug.h"
59 #include "wine/unicode.h"
60 #include "wine/list.h"
63 #include "mmdeviceapi.h"
69 #include "endpointvolume.h"
70 #include "audioclient.h"
71 #include "audiopolicy.h"
73 WINE_DEFAULT_DEBUG_CHANNEL(coreaudio
);
75 #ifndef HAVE_AUDIOUNIT_AUDIOCOMPONENT_H
76 /* Define new AudioComponent Manager functions for OSX 10.5 */
77 typedef Component AudioComponent
;
78 typedef ComponentDescription AudioComponentDescription
;
79 typedef ComponentInstance AudioComponentInstance
;
81 static inline AudioComponent
AudioComponentFindNext(AudioComponent ac
, AudioComponentDescription
*desc
)
83 return FindNextComponent(ac
, desc
);
86 static inline OSStatus
AudioComponentInstanceNew(AudioComponent ac
, AudioComponentInstance
*aci
)
88 return OpenAComponent(ac
, aci
);
91 static inline OSStatus
AudioComponentInstanceDispose(AudioComponentInstance aci
)
93 return CloseComponent(aci
);
97 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
99 static const REFERENCE_TIME DefaultPeriod
= 100000;
100 static const REFERENCE_TIME MinimumPeriod
= 50000;
103 typedef struct ACImpl ACImpl
;
105 typedef struct _AudioSession
{
112 UINT32 channel_count
;
116 CRITICAL_SECTION lock
;
121 typedef struct _AudioSessionWrapper
{
122 IAudioSessionControl2 IAudioSessionControl2_iface
;
123 IChannelAudioVolume IChannelAudioVolume_iface
;
124 ISimpleAudioVolume ISimpleAudioVolume_iface
;
129 AudioSession
*session
;
130 } AudioSessionWrapper
;
133 IAudioClient IAudioClient_iface
;
134 IAudioRenderClient IAudioRenderClient_iface
;
135 IAudioCaptureClient IAudioCaptureClient_iface
;
136 IAudioClock IAudioClock_iface
;
137 IAudioClock2 IAudioClock2_iface
;
138 IAudioStreamVolume IAudioStreamVolume_iface
;
143 IUnknown
*pUnkFTMarshal
;
149 AUDCLNT_SHAREMODE share
;
154 AudioDeviceID adevid
;
155 AudioObjectPropertyScope scope
;
156 AudioConverterRef converter
;
157 AudioComponentInstance unit
;
158 AudioStreamBasicDescription dev_desc
; /* audio unit format, not necessarily the same as fmt */
160 UINT32 period_ms
, bufsize_frames
, period_frames
;
161 UINT64 written_frames
;
162 UINT32 lcl_offs_frames
, wri_offs_frames
, held_frames
, tmp_buffer_frames
;
163 UINT32 cap_bufsize_frames
, cap_offs_frames
, cap_held_frames
, wrap_bufsize_frames
, resamp_bufsize_frames
;
166 BYTE
*cap_buffer
, *wrap_buffer
, *resamp_buffer
, *local_buffer
, *tmp_buffer
;
168 AudioSession
*session
;
169 AudioSessionWrapper
*session_wrapper
;
176 static const IAudioClientVtbl AudioClient_Vtbl
;
177 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
178 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
179 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
180 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
181 static const IAudioClockVtbl AudioClock_Vtbl
;
182 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
183 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
184 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
185 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
187 typedef struct _SessionMgr
{
188 IAudioSessionManager2 IAudioSessionManager2_iface
;
195 static const WCHAR drv_key_devicesW
[] = {'S','o','f','t','w','a','r','e','\\',
196 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
197 'w','i','n','e','c','o','r','e','a','u','d','i','o','.','d','r','v','\\','d','e','v','i','c','e','s',0};
198 static const WCHAR guidW
[] = {'g','u','i','d',0};
200 static HANDLE g_timer_q
;
202 static CRITICAL_SECTION g_sessions_lock
;
203 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug
=
205 0, 0, &g_sessions_lock
,
206 { &g_sessions_lock_debug
.ProcessLocksList
, &g_sessions_lock_debug
.ProcessLocksList
},
207 0, 0, { (DWORD_PTR
)(__FILE__
": g_sessions_lock") }
209 static CRITICAL_SECTION g_sessions_lock
= { &g_sessions_lock_debug
, -1, 0, 0, 0, 0 };
210 static struct list g_sessions
= LIST_INIT(g_sessions
);
212 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
213 static HRESULT
ca_setvol(ACImpl
*This
, UINT32 index
);
215 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
217 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
220 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
222 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
225 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
227 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
230 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
232 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
235 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
237 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
240 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
242 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
245 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
247 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
250 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
252 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
255 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
257 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
260 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
262 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
265 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
269 case DLL_PROCESS_ATTACH
:
270 g_timer_q
= CreateTimerQueue();
275 case DLL_PROCESS_DETACH
:
277 DeleteCriticalSection(&g_sessions_lock
);
283 /* From <dlls/mmdevapi/mmdevapi.h> */
284 enum DriverPriority
{
285 Priority_Unavailable
= 0,
291 int WINAPI
AUDDRV_GetPriority(void)
293 return Priority_Neutral
;
296 static HRESULT
osstatus_to_hresult(OSStatus sc
)
299 case kAudioFormatUnsupportedDataFormatError
:
300 case kAudioFormatUnknownFormatError
:
301 case kAudioDeviceUnsupportedFormatError
:
302 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
303 case kAudioHardwareBadDeviceError
:
304 return AUDCLNT_E_DEVICE_INVALIDATED
;
309 static void set_device_guid(EDataFlow flow
, HKEY drv_key
, const WCHAR
*key_name
,
317 lr
= RegCreateKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, NULL
, 0, KEY_WRITE
,
318 NULL
, &drv_key
, NULL
);
319 if(lr
!= ERROR_SUCCESS
){
320 ERR("RegCreateKeyEx(drv_key) failed: %u\n", lr
);
326 lr
= RegCreateKeyExW(drv_key
, key_name
, 0, NULL
, 0, KEY_WRITE
,
328 if(lr
!= ERROR_SUCCESS
){
329 ERR("RegCreateKeyEx(%s) failed: %u\n", wine_dbgstr_w(key_name
), lr
);
333 lr
= RegSetValueExW(key
, guidW
, 0, REG_BINARY
, (BYTE
*)guid
,
335 if(lr
!= ERROR_SUCCESS
)
336 ERR("RegSetValueEx(%s\\guid) failed: %u\n", wine_dbgstr_w(key_name
), lr
);
341 RegCloseKey(drv_key
);
344 static void get_device_guid(EDataFlow flow
, AudioDeviceID device
, GUID
*guid
)
346 HKEY key
= NULL
, dev_key
;
347 DWORD type
, size
= sizeof(*guid
);
350 static const WCHAR key_fmt
[] = {'%','u',0};
358 sprintfW(key_name
+ 2, key_fmt
, device
);
360 if(RegOpenKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, KEY_WRITE
|KEY_READ
, &key
) == ERROR_SUCCESS
){
361 if(RegOpenKeyExW(key
, key_name
, 0, KEY_READ
, &dev_key
) == ERROR_SUCCESS
){
362 if(RegQueryValueExW(dev_key
, guidW
, 0, &type
,
363 (BYTE
*)guid
, &size
) == ERROR_SUCCESS
){
364 if(type
== REG_BINARY
){
365 RegCloseKey(dev_key
);
369 ERR("Invalid type for device %s GUID: %u; ignoring and overwriting\n",
370 wine_dbgstr_w(key_name
), type
);
372 RegCloseKey(dev_key
);
378 set_device_guid(flow
, key
, key_name
, guid
);
384 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
,
385 GUID
**guids
, UINT
*num
, UINT
*def_index
)
387 UInt32 devsize
, size
;
388 AudioDeviceID
*devices
;
389 AudioDeviceID default_id
;
390 AudioObjectPropertyAddress addr
;
394 TRACE("%d %p %p %p\n", flow
, ids
, num
, def_index
);
396 addr
.mScope
= kAudioObjectPropertyScopeGlobal
;
397 addr
.mElement
= kAudioObjectPropertyElementMaster
;
399 addr
.mSelector
= kAudioHardwarePropertyDefaultOutputDevice
;
400 else if(flow
== eCapture
)
401 addr
.mSelector
= kAudioHardwarePropertyDefaultInputDevice
;
405 size
= sizeof(default_id
);
406 sc
= AudioObjectGetPropertyData(kAudioObjectSystemObject
, &addr
, 0,
407 NULL
, &size
, &default_id
);
409 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc
);
413 addr
.mSelector
= kAudioHardwarePropertyDevices
;
414 sc
= AudioObjectGetPropertyDataSize(kAudioObjectSystemObject
, &addr
, 0,
417 WARN("Getting _Devices property size failed: %lx\n", sc
);
418 return osstatus_to_hresult(sc
);
421 devices
= HeapAlloc(GetProcessHeap(), 0, devsize
);
423 return E_OUTOFMEMORY
;
425 sc
= AudioObjectGetPropertyData(kAudioObjectSystemObject
, &addr
, 0, NULL
,
428 WARN("Getting _Devices property failed: %lx\n", sc
);
429 HeapFree(GetProcessHeap(), 0, devices
);
430 return osstatus_to_hresult(sc
);
433 ndevices
= devsize
/ sizeof(AudioDeviceID
);
435 *ids
= HeapAlloc(GetProcessHeap(), 0, ndevices
* sizeof(WCHAR
*));
437 HeapFree(GetProcessHeap(), 0, devices
);
438 return E_OUTOFMEMORY
;
441 *guids
= HeapAlloc(GetProcessHeap(), 0, ndevices
* sizeof(GUID
));
443 HeapFree(GetProcessHeap(), 0, *ids
);
444 HeapFree(GetProcessHeap(), 0, devices
);
445 return E_OUTOFMEMORY
;
449 *def_index
= (UINT
)-1;
450 for(i
= 0; i
< ndevices
; ++i
){
451 AudioBufferList
*buffers
;
456 addr
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
458 addr
.mScope
= kAudioDevicePropertyScopeOutput
;
460 addr
.mScope
= kAudioDevicePropertyScopeInput
;
462 sc
= AudioObjectGetPropertyDataSize(devices
[i
], &addr
, 0, NULL
, &size
);
464 WARN("Unable to get _StreamConfiguration property size for "
465 "device %lu: %lx\n", devices
[i
], sc
);
469 buffers
= HeapAlloc(GetProcessHeap(), 0, size
);
471 HeapFree(GetProcessHeap(), 0, devices
);
472 for(j
= 0; j
< *num
; ++j
)
473 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
474 HeapFree(GetProcessHeap(), 0, *guids
);
475 HeapFree(GetProcessHeap(), 0, *ids
);
476 return E_OUTOFMEMORY
;
479 sc
= AudioObjectGetPropertyData(devices
[i
], &addr
, 0, NULL
,
482 WARN("Unable to get _StreamConfiguration property for "
483 "device %lu: %lx\n", devices
[i
], sc
);
484 HeapFree(GetProcessHeap(), 0, buffers
);
488 /* check that there's at least one channel in this device before
489 * we claim it as usable */
490 for(j
= 0; j
< buffers
->mNumberBuffers
; ++j
)
491 if(buffers
->mBuffers
[j
].mNumberChannels
> 0)
493 if(j
>= buffers
->mNumberBuffers
){
494 HeapFree(GetProcessHeap(), 0, buffers
);
498 HeapFree(GetProcessHeap(), 0, buffers
);
501 addr
.mSelector
= kAudioObjectPropertyName
;
502 sc
= AudioObjectGetPropertyData(devices
[i
], &addr
, 0, NULL
,
505 WARN("Unable to get _Name property for device %lu: %lx\n",
510 len
= CFStringGetLength(name
) + 1;
511 (*ids
)[*num
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
514 HeapFree(GetProcessHeap(), 0, devices
);
515 for(j
= 0; j
< *num
; ++j
)
516 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
517 HeapFree(GetProcessHeap(), 0, *ids
);
518 HeapFree(GetProcessHeap(), 0, *guids
);
519 return E_OUTOFMEMORY
;
521 CFStringGetCharacters(name
, CFRangeMake(0, len
- 1), (UniChar
*)(*ids
)[*num
]);
522 ((*ids
)[*num
])[len
- 1] = 0;
525 get_device_guid(flow
, devices
[i
], &(*guids
)[*num
]);
527 if(*def_index
== (UINT
)-1 && devices
[i
] == default_id
)
530 TRACE("device %u: id %s key %u%s\n", *num
, debugstr_w((*ids
)[*num
]),
531 (unsigned int)devices
[i
], (*def_index
== *num
) ? " (default)" : "");
536 if(*def_index
== (UINT
)-1)
539 HeapFree(GetProcessHeap(), 0, devices
);
544 static BOOL
get_deviceid_by_guid(GUID
*guid
, AudioDeviceID
*id
, EDataFlow
*flow
)
551 if(RegOpenKeyExW(HKEY_CURRENT_USER
, drv_key_devicesW
, 0, KEY_READ
, &devices_key
) != ERROR_SUCCESS
){
552 ERR("No devices in registry?\n");
561 key_name_size
= sizeof(key_name
);
562 if(RegEnumKeyExW(devices_key
, i
++, key_name
, &key_name_size
, NULL
,
563 NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
566 if(RegOpenKeyExW(devices_key
, key_name
, 0, KEY_READ
, &key
) != ERROR_SUCCESS
){
567 WARN("Couldn't open key: %s\n", wine_dbgstr_w(key_name
));
571 size
= sizeof(reg_guid
);
572 if(RegQueryValueExW(key
, guidW
, 0, &type
,
573 (BYTE
*)®_guid
, &size
) == ERROR_SUCCESS
){
574 if(IsEqualGUID(®_guid
, guid
)){
576 RegCloseKey(devices_key
);
578 TRACE("Found matching device key: %s\n", wine_dbgstr_w(key_name
));
580 if(key_name
[0] == '0')
582 else if(key_name
[0] == '1')
585 ERR("Unknown device type: %c\n", key_name
[0]);
589 *id
= strtoulW(key_name
+ 2, NULL
, 10);
598 RegCloseKey(devices_key
);
600 WARN("No matching device in registry for GUID %s\n", debugstr_guid(guid
));
605 static AudioComponentInstance
get_audiounit(EDataFlow dataflow
, AudioDeviceID adevid
)
607 AudioComponentInstance unit
;
609 AudioComponentDescription desc
;
612 memset(&desc
, 0, sizeof(desc
));
613 desc
.componentType
= kAudioUnitType_Output
;
614 desc
.componentSubType
= kAudioUnitSubType_HALOutput
;
615 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
617 if(!(comp
= AudioComponentFindNext(NULL
, &desc
))){
618 WARN("AudioComponentFindNext failed\n");
622 sc
= AudioComponentInstanceNew(comp
, &unit
);
624 WARN("AudioComponentInstanceNew failed: %lx\n", sc
);
628 if(dataflow
== eCapture
){
632 sc
= AudioUnitSetProperty(unit
, kAudioOutputUnitProperty_EnableIO
,
633 kAudioUnitScope_Input
, 1, &enableio
, sizeof(enableio
));
635 WARN("Couldn't enable I/O on input element: %lx\n", sc
);
636 AudioComponentInstanceDispose(unit
);
641 sc
= AudioUnitSetProperty(unit
, kAudioOutputUnitProperty_EnableIO
,
642 kAudioUnitScope_Output
, 0, &enableio
, sizeof(enableio
));
644 WARN("Couldn't disable I/O on output element: %lx\n", sc
);
645 AudioComponentInstanceDispose(unit
);
650 sc
= AudioUnitSetProperty(unit
, kAudioOutputUnitProperty_CurrentDevice
,
651 kAudioUnitScope_Global
, 0, &adevid
, sizeof(adevid
));
653 WARN("Couldn't set audio unit device\n");
654 AudioComponentInstanceDispose(unit
);
661 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(GUID
*guid
, IMMDevice
*dev
, IAudioClient
**out
)
664 AudioDeviceID adevid
;
668 TRACE("%s %p %p\n", debugstr_guid(guid
), dev
, out
);
670 if(!get_deviceid_by_guid(guid
, &adevid
, &dataflow
))
671 return AUDCLNT_E_DEVICE_INVALIDATED
;
673 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
675 return E_OUTOFMEMORY
;
677 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
678 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
679 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
680 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
681 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
682 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
684 This
->dataflow
= dataflow
;
686 if(dataflow
== eRender
)
687 This
->scope
= kAudioDevicePropertyScopeOutput
;
688 else if(dataflow
== eCapture
)
689 This
->scope
= kAudioDevicePropertyScopeInput
;
691 HeapFree(GetProcessHeap(), 0, This
);
697 hr
= CoCreateFreeThreadedMarshaler((IUnknown
*)&This
->IAudioClient_iface
,
698 (IUnknown
**)&This
->pUnkFTMarshal
);
700 HeapFree(GetProcessHeap(), 0, This
);
705 IMMDevice_AddRef(This
->parent
);
707 This
->adevid
= adevid
;
709 if(!(This
->unit
= get_audiounit(This
->dataflow
, This
->adevid
))){
710 HeapFree(GetProcessHeap(), 0, This
);
711 return AUDCLNT_E_DEVICE_INVALIDATED
;
714 *out
= &This
->IAudioClient_iface
;
715 IAudioClient_AddRef(&This
->IAudioClient_iface
);
720 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
721 REFIID riid
, void **ppv
)
723 ACImpl
*This
= impl_from_IAudioClient(iface
);
724 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
729 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
731 else if(IsEqualIID(riid
, &IID_IMarshal
))
732 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
735 IUnknown_AddRef((IUnknown
*)*ppv
);
738 WARN("Unknown interface %s\n", debugstr_guid(riid
));
739 return E_NOINTERFACE
;
742 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
744 ACImpl
*This
= impl_from_IAudioClient(iface
);
746 ref
= InterlockedIncrement(&This
->ref
);
747 TRACE("(%p) Refcount now %u\n", This
, ref
);
751 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
753 ACImpl
*This
= impl_from_IAudioClient(iface
);
755 ref
= InterlockedDecrement(&This
->ref
);
756 TRACE("(%p) Refcount now %u\n", This
, ref
);
761 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
762 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
763 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
765 WaitForSingleObject(event
, INFINITE
);
768 AudioOutputUnitStop(This
->unit
);
769 AudioComponentInstanceDispose(This
->unit
);
771 AudioConverterDispose(This
->converter
);
773 EnterCriticalSection(&g_sessions_lock
);
774 list_remove(&This
->entry
);
775 LeaveCriticalSection(&g_sessions_lock
);
777 HeapFree(GetProcessHeap(), 0, This
->vols
);
778 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
779 HeapFree(GetProcessHeap(), 0, This
->cap_buffer
);
780 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
781 free(This
->wrap_buffer
);
782 HeapFree(GetProcessHeap(), 0, This
->resamp_buffer
);
783 CoTaskMemFree(This
->fmt
);
784 IMMDevice_Release(This
->parent
);
785 IUnknown_Release(This
->pUnkFTMarshal
);
786 HeapFree(GetProcessHeap(), 0, This
);
791 static void dump_fmt(const WAVEFORMATEX
*fmt
)
793 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
794 switch(fmt
->wFormatTag
){
795 case WAVE_FORMAT_PCM
:
796 TRACE("WAVE_FORMAT_PCM");
798 case WAVE_FORMAT_IEEE_FLOAT
:
799 TRACE("WAVE_FORMAT_IEEE_FLOAT");
801 case WAVE_FORMAT_EXTENSIBLE
:
802 TRACE("WAVE_FORMAT_EXTENSIBLE");
810 TRACE("nChannels: %u\n", fmt
->nChannels
);
811 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
812 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
813 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
814 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
815 TRACE("cbSize: %u\n", fmt
->cbSize
);
817 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
818 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
819 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
820 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
821 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
825 static DWORD
get_channel_mask(unsigned int channels
)
831 return KSAUDIO_SPEAKER_MONO
;
833 return KSAUDIO_SPEAKER_STEREO
;
835 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
837 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
839 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
841 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
843 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
845 return KSAUDIO_SPEAKER_7POINT1_SURROUND
; /* Vista deprecates 7POINT1 */
847 FIXME("Unknown speaker configuration: %u\n", channels
);
851 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
856 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
857 size
= sizeof(WAVEFORMATEXTENSIBLE
);
859 size
= sizeof(WAVEFORMATEX
);
861 ret
= CoTaskMemAlloc(size
);
865 memcpy(ret
, fmt
, size
);
867 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
872 static HRESULT
ca_get_audiodesc(AudioStreamBasicDescription
*desc
,
873 const WAVEFORMATEX
*fmt
)
875 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
877 desc
->mFormatFlags
= 0;
879 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
880 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
881 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
882 desc
->mFormatID
= kAudioFormatLinearPCM
;
883 if(fmt
->wBitsPerSample
> 8)
884 desc
->mFormatFlags
= kAudioFormatFlagIsSignedInteger
;
885 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
886 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
887 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
888 desc
->mFormatID
= kAudioFormatLinearPCM
;
889 desc
->mFormatFlags
= kAudioFormatFlagIsFloat
;
890 }else if(fmt
->wFormatTag
== WAVE_FORMAT_MULAW
||
891 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
892 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_MULAW
))){
893 desc
->mFormatID
= kAudioFormatULaw
;
894 }else if(fmt
->wFormatTag
== WAVE_FORMAT_ALAW
||
895 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
896 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_ALAW
))){
897 desc
->mFormatID
= kAudioFormatALaw
;
899 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
901 desc
->mSampleRate
= fmt
->nSamplesPerSec
;
902 desc
->mBytesPerPacket
= fmt
->nBlockAlign
;
903 desc
->mFramesPerPacket
= 1;
904 desc
->mBytesPerFrame
= fmt
->nBlockAlign
;
905 desc
->mChannelsPerFrame
= fmt
->nChannels
;
906 desc
->mBitsPerChannel
= fmt
->wBitsPerSample
;
912 static void session_init_vols(AudioSession
*session
, UINT channels
)
914 if(session
->channel_count
< channels
){
917 if(session
->channel_vols
)
918 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
919 session
->channel_vols
, sizeof(float) * channels
);
921 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
922 sizeof(float) * channels
);
923 if(!session
->channel_vols
)
926 for(i
= session
->channel_count
; i
< channels
; ++i
)
927 session
->channel_vols
[i
] = 1.f
;
929 session
->channel_count
= channels
;
933 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
938 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
942 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
944 ret
->device
= device
;
946 list_init(&ret
->clients
);
948 list_add_head(&g_sessions
, &ret
->entry
);
950 InitializeCriticalSection(&ret
->lock
);
951 ret
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AudioSession.lock");
953 session_init_vols(ret
, num_channels
);
955 ret
->master_vol
= 1.f
;
960 /* if channels == 0, then this will return or create a session with
961 * matching dataflow and GUID. otherwise, channels must also match */
962 static HRESULT
get_audio_session(const GUID
*sessionguid
,
963 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
965 AudioSession
*session
;
967 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
968 *out
= create_session(&GUID_NULL
, device
, channels
);
970 return E_OUTOFMEMORY
;
976 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
977 if(session
->device
== device
&&
978 IsEqualGUID(sessionguid
, &session
->guid
)){
979 session_init_vols(session
, channels
);
986 *out
= create_session(sessionguid
, device
, channels
);
988 return E_OUTOFMEMORY
;
994 static void ca_wrap_buffer(BYTE
*dst
, UINT32 dst_offs
, UINT32 dst_bytes
,
995 BYTE
*src
, UINT32 src_bytes
)
997 UINT32 chunk_bytes
= dst_bytes
- dst_offs
;
999 if(chunk_bytes
< src_bytes
){
1000 memcpy(dst
+ dst_offs
, src
, chunk_bytes
);
1001 memcpy(dst
, src
+ chunk_bytes
, src_bytes
- chunk_bytes
);
1003 memcpy(dst
+ dst_offs
, src
, src_bytes
);
1006 static void silence_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 frames
)
1008 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
1009 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1010 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1011 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
1012 This
->fmt
->wBitsPerSample
== 8)
1013 memset(buffer
, 128, frames
* This
->fmt
->nBlockAlign
);
1015 memset(buffer
, 0, frames
* This
->fmt
->nBlockAlign
);
1018 /* CA is pulling data from us */
1019 static OSStatus
ca_render_cb(void *user
, AudioUnitRenderActionFlags
*flags
,
1020 const AudioTimeStamp
*ts
, UInt32 bus
, UInt32 nframes
,
1021 AudioBufferList
*data
)
1023 ACImpl
*This
= user
;
1024 UINT32 to_copy_bytes
, to_copy_frames
, chunk_bytes
, lcl_offs_bytes
;
1026 OSSpinLockLock(&This
->lock
);
1029 lcl_offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
1030 to_copy_frames
= min(nframes
, This
->held_frames
);
1031 to_copy_bytes
= to_copy_frames
* This
->fmt
->nBlockAlign
;
1033 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) * This
->fmt
->nBlockAlign
;
1035 if(to_copy_bytes
> chunk_bytes
){
1036 memcpy(data
->mBuffers
[0].mData
, This
->local_buffer
+ lcl_offs_bytes
, chunk_bytes
);
1037 memcpy(((BYTE
*)data
->mBuffers
[0].mData
) + chunk_bytes
, This
->local_buffer
, to_copy_bytes
- chunk_bytes
);
1039 memcpy(data
->mBuffers
[0].mData
, This
->local_buffer
+ lcl_offs_bytes
, to_copy_bytes
);
1041 This
->lcl_offs_frames
+= to_copy_frames
;
1042 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1043 This
->held_frames
-= to_copy_frames
;
1045 to_copy_bytes
= to_copy_frames
= 0;
1047 if(nframes
> to_copy_frames
)
1048 silence_buffer(This
, ((BYTE
*)data
->mBuffers
[0].mData
) + to_copy_bytes
, nframes
- to_copy_frames
);
1050 OSSpinLockUnlock(&This
->lock
);
1055 static UINT
buf_ptr_diff(UINT left
, UINT right
, UINT bufsize
)
1058 return right
- left
;
1059 return bufsize
- (left
- right
);
1062 /* place data from cap_buffer into provided AudioBufferList */
1063 static OSStatus
feed_cb(AudioConverterRef converter
, UInt32
*nframes
, AudioBufferList
*data
,
1064 AudioStreamPacketDescription
**packets
, void *user
)
1066 ACImpl
*This
= user
;
1068 *nframes
= min(*nframes
, This
->cap_held_frames
);
1070 data
->mBuffers
[0].mData
= NULL
;
1071 data
->mBuffers
[0].mDataByteSize
= 0;
1072 data
->mBuffers
[0].mNumberChannels
= This
->fmt
->nChannels
;
1076 data
->mBuffers
[0].mDataByteSize
= *nframes
* This
->fmt
->nBlockAlign
;
1077 data
->mBuffers
[0].mNumberChannels
= This
->fmt
->nChannels
;
1079 if(This
->cap_offs_frames
+ *nframes
> This
->cap_bufsize_frames
){
1080 UINT32 chunk_frames
= This
->cap_bufsize_frames
- This
->cap_offs_frames
;
1082 if(This
->wrap_bufsize_frames
< *nframes
){
1083 free(This
->wrap_buffer
);
1084 This
->wrap_buffer
= malloc(data
->mBuffers
[0].mDataByteSize
);
1085 This
->wrap_bufsize_frames
= *nframes
;
1088 memcpy(This
->wrap_buffer
, This
->cap_buffer
+ This
->cap_offs_frames
* This
->fmt
->nBlockAlign
,
1089 chunk_frames
* This
->fmt
->nBlockAlign
);
1090 memcpy(This
->wrap_buffer
+ chunk_frames
* This
->fmt
->nBlockAlign
, This
->cap_buffer
,
1091 (*nframes
- chunk_frames
) * This
->fmt
->nBlockAlign
);
1093 data
->mBuffers
[0].mData
= This
->wrap_buffer
;
1095 data
->mBuffers
[0].mData
= This
->cap_buffer
+ This
->cap_offs_frames
* This
->fmt
->nBlockAlign
;
1097 This
->cap_offs_frames
+= *nframes
;
1098 This
->cap_offs_frames
%= This
->cap_bufsize_frames
;
1099 This
->cap_held_frames
-= *nframes
;
1107 static void capture_resample(ACImpl
*This
)
1109 UINT32 resamp_period_frames
= MulDiv(This
->period_frames
, This
->dev_desc
.mSampleRate
, This
->fmt
->nSamplesPerSec
);
1112 /* the resampling process often needs more source frames than we'd
1113 * guess from a straight conversion using the sample rate ratio. so
1114 * only convert if we have extra source data. */
1115 while(This
->cap_held_frames
> resamp_period_frames
* 2){
1116 AudioBufferList converted_list
;
1117 UInt32 wanted_frames
= This
->period_frames
;
1119 converted_list
.mNumberBuffers
= 1;
1120 converted_list
.mBuffers
[0].mNumberChannels
= This
->fmt
->nChannels
;
1121 converted_list
.mBuffers
[0].mDataByteSize
= wanted_frames
* This
->fmt
->nBlockAlign
;
1123 if(This
->resamp_bufsize_frames
< wanted_frames
){
1124 HeapFree(GetProcessHeap(), 0, This
->resamp_buffer
);
1125 This
->resamp_buffer
= HeapAlloc(GetProcessHeap(), 0, converted_list
.mBuffers
[0].mDataByteSize
);
1126 This
->resamp_bufsize_frames
= wanted_frames
;
1129 converted_list
.mBuffers
[0].mData
= This
->resamp_buffer
;
1131 sc
= AudioConverterFillComplexBuffer(This
->converter
, feed_cb
,
1132 This
, &wanted_frames
, &converted_list
, NULL
);
1134 WARN("AudioConverterFillComplexBuffer failed: %lx\n", sc
);
1138 ca_wrap_buffer(This
->local_buffer
,
1139 This
->wri_offs_frames
* This
->fmt
->nBlockAlign
,
1140 This
->bufsize_frames
* This
->fmt
->nBlockAlign
,
1141 This
->resamp_buffer
, wanted_frames
* This
->fmt
->nBlockAlign
);
1143 This
->wri_offs_frames
+= wanted_frames
;
1144 This
->wri_offs_frames
%= This
->bufsize_frames
;
1145 if(This
->held_frames
+ wanted_frames
> This
->bufsize_frames
){
1146 This
->lcl_offs_frames
+= buf_ptr_diff(This
->lcl_offs_frames
,
1147 This
->wri_offs_frames
, This
->bufsize_frames
);
1148 This
->held_frames
= This
->bufsize_frames
;
1150 This
->held_frames
+= wanted_frames
;
1154 /* we need to trigger CA to pull data from the device and give it to us
1156 * raw data from CA is stored in cap_buffer, possibly via wrap_buffer
1158 * raw data is resampled from cap_buffer into resamp_buffer in period-size
1159 * chunks and copied to local_buffer
1161 static OSStatus
ca_capture_cb(void *user
, AudioUnitRenderActionFlags
*flags
,
1162 const AudioTimeStamp
*ts
, UInt32 bus
, UInt32 nframes
,
1163 AudioBufferList
*data
)
1165 ACImpl
*This
= user
;
1166 AudioBufferList list
;
1168 UINT32 cap_wri_offs_frames
;
1170 OSSpinLockLock(&This
->lock
);
1172 cap_wri_offs_frames
= (This
->cap_offs_frames
+ This
->cap_held_frames
) % This
->cap_bufsize_frames
;
1174 list
.mNumberBuffers
= 1;
1175 list
.mBuffers
[0].mNumberChannels
= This
->fmt
->nChannels
;
1176 list
.mBuffers
[0].mDataByteSize
= nframes
* This
->fmt
->nBlockAlign
;
1178 if(!This
->playing
|| cap_wri_offs_frames
+ nframes
> This
->cap_bufsize_frames
){
1179 if(This
->wrap_bufsize_frames
< nframes
){
1180 free(This
->wrap_buffer
);
1181 This
->wrap_buffer
= malloc(list
.mBuffers
[0].mDataByteSize
);
1182 This
->wrap_bufsize_frames
= nframes
;
1185 list
.mBuffers
[0].mData
= This
->wrap_buffer
;
1187 list
.mBuffers
[0].mData
= This
->cap_buffer
+ cap_wri_offs_frames
* This
->fmt
->nBlockAlign
;
1189 sc
= AudioUnitRender(This
->unit
, flags
, ts
, bus
, nframes
, &list
);
1191 OSSpinLockUnlock(&This
->lock
);
1196 if(list
.mBuffers
[0].mData
== This
->wrap_buffer
){
1197 ca_wrap_buffer(This
->cap_buffer
,
1198 cap_wri_offs_frames
* This
->fmt
->nBlockAlign
,
1199 This
->cap_bufsize_frames
* This
->fmt
->nBlockAlign
,
1200 This
->wrap_buffer
, list
.mBuffers
[0].mDataByteSize
);
1203 This
->cap_held_frames
+= list
.mBuffers
[0].mDataByteSize
/ This
->fmt
->nBlockAlign
;
1204 if(This
->cap_held_frames
> This
->cap_bufsize_frames
){
1205 This
->cap_offs_frames
+= This
->cap_held_frames
% This
->cap_bufsize_frames
;
1206 This
->cap_offs_frames
%= This
->cap_bufsize_frames
;
1207 This
->cap_held_frames
= This
->cap_bufsize_frames
;
1211 OSSpinLockUnlock(&This
->lock
);
1215 static void dump_adesc(const char *aux
, AudioStreamBasicDescription
*desc
)
1217 TRACE("%s: mSampleRate: %f\n", aux
, desc
->mSampleRate
);
1218 TRACE("%s: mBytesPerPacket: %ld\n", aux
, desc
->mBytesPerPacket
);
1219 TRACE("%s: mFramesPerPacket: %ld\n", aux
, desc
->mFramesPerPacket
);
1220 TRACE("%s: mBytesPerFrame: %ld\n", aux
, desc
->mBytesPerFrame
);
1221 TRACE("%s: mChannelsPerFrame: %ld\n", aux
, desc
->mChannelsPerFrame
);
1222 TRACE("%s: mBitsPerChannel: %ld\n", aux
, desc
->mBitsPerChannel
);
1225 static HRESULT
ca_setup_audiounit(EDataFlow dataflow
, AudioComponentInstance unit
,
1226 const WAVEFORMATEX
*fmt
, AudioStreamBasicDescription
*dev_desc
,
1227 AudioConverterRef
*converter
)
1232 if(dataflow
== eCapture
){
1233 AudioStreamBasicDescription desc
;
1237 hr
= ca_get_audiodesc(&desc
, fmt
);
1240 dump_adesc("requested", &desc
);
1242 /* input-only units can't perform sample rate conversion, so we have to
1243 * set up our own AudioConverter to support arbitrary sample rates. */
1244 size
= sizeof(*dev_desc
);
1245 sc
= AudioUnitGetProperty(unit
, kAudioUnitProperty_StreamFormat
,
1246 kAudioUnitScope_Input
, 1, dev_desc
, &size
);
1248 WARN("Couldn't get unit format: %lx\n", sc
);
1249 return osstatus_to_hresult(sc
);
1251 dump_adesc("hardware", dev_desc
);
1253 rate
= dev_desc
->mSampleRate
;
1255 dev_desc
->mSampleRate
= rate
;
1257 dump_adesc("final", dev_desc
);
1258 sc
= AudioUnitSetProperty(unit
, kAudioUnitProperty_StreamFormat
,
1259 kAudioUnitScope_Output
, 1, dev_desc
, sizeof(*dev_desc
));
1261 WARN("Couldn't set unit format: %lx\n", sc
);
1262 return osstatus_to_hresult(sc
);
1265 sc
= AudioConverterNew(dev_desc
, &desc
, converter
);
1267 WARN("Couldn't create audio converter: %lx\n", sc
);
1268 return osstatus_to_hresult(sc
);
1271 hr
= ca_get_audiodesc(dev_desc
, fmt
);
1275 dump_adesc("final", dev_desc
);
1276 sc
= AudioUnitSetProperty(unit
, kAudioUnitProperty_StreamFormat
,
1277 kAudioUnitScope_Input
, 0, dev_desc
, sizeof(*dev_desc
));
1279 WARN("Couldn't set format: %lx\n", sc
);
1280 return osstatus_to_hresult(sc
);
1287 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
1288 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
1289 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
1290 const GUID
*sessionguid
)
1292 ACImpl
*This
= impl_from_IAudioClient(iface
);
1297 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
1298 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
1305 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1306 return AUDCLNT_E_NOT_INITIALIZED
;
1308 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
1309 AUDCLNT_STREAMFLAGS_LOOPBACK
|
1310 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
1311 AUDCLNT_STREAMFLAGS_NOPERSIST
|
1312 AUDCLNT_STREAMFLAGS_RATEADJUST
|
1313 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
1314 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
1315 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
1316 TRACE("Unknown flags: %08x\n", flags
);
1317 return E_INVALIDARG
;
1320 if(mode
== AUDCLNT_SHAREMODE_SHARED
){
1321 period
= DefaultPeriod
;
1322 if( duration
< 3 * period
)
1323 duration
= 3 * period
;
1325 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1326 if(((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
== 0 ||
1327 ((WAVEFORMATEXTENSIBLE
*)fmt
)->dwChannelMask
& SPEAKER_RESERVED
)
1328 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1332 period
= DefaultPeriod
; /* not minimum */
1333 if(period
< MinimumPeriod
|| period
> 5000000)
1334 return AUDCLNT_E_INVALID_DEVICE_PERIOD
;
1335 if(duration
> 20000000) /* the smaller the period, the lower this limit */
1336 return AUDCLNT_E_BUFFER_SIZE_ERROR
;
1337 if(flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
){
1338 if(duration
!= period
)
1339 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL
;
1340 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
1341 return AUDCLNT_E_DEVICE_IN_USE
;
1343 if( duration
< 8 * period
)
1344 duration
= 8 * period
; /* may grow above 2s */
1348 OSSpinLockLock(&This
->lock
);
1351 OSSpinLockUnlock(&This
->lock
);
1352 return AUDCLNT_E_ALREADY_INITIALIZED
;
1355 This
->fmt
= clone_format(fmt
);
1357 OSSpinLockUnlock(&This
->lock
);
1358 return E_OUTOFMEMORY
;
1361 This
->period_ms
= period
/ 10000;
1362 This
->period_frames
= MulDiv(period
, This
->fmt
->nSamplesPerSec
, 10000000);
1364 This
->bufsize_frames
= MulDiv(duration
, fmt
->nSamplesPerSec
, 10000000);
1365 if(mode
== AUDCLNT_SHAREMODE_EXCLUSIVE
)
1366 This
->bufsize_frames
-= This
->bufsize_frames
% This
->period_frames
;
1368 hr
= ca_setup_audiounit(This
->dataflow
, This
->unit
, This
->fmt
, &This
->dev_desc
, &This
->converter
);
1370 CoTaskMemFree(This
->fmt
);
1372 OSSpinLockUnlock(&This
->lock
);
1376 if(This
->dataflow
== eCapture
){
1377 AURenderCallbackStruct input
;
1379 memset(&input
, 0, sizeof(input
));
1380 input
.inputProc
= &ca_capture_cb
;
1381 input
.inputProcRefCon
= This
;
1383 sc
= AudioUnitSetProperty(This
->unit
, kAudioOutputUnitProperty_SetInputCallback
,
1384 kAudioUnitScope_Output
, 1, &input
, sizeof(input
));
1386 WARN("Couldn't set callback: %lx\n", sc
);
1387 AudioConverterDispose(This
->converter
);
1388 This
->converter
= NULL
;
1389 CoTaskMemFree(This
->fmt
);
1391 OSSpinLockUnlock(&This
->lock
);
1392 return osstatus_to_hresult(sc
);
1395 AURenderCallbackStruct input
;
1397 memset(&input
, 0, sizeof(input
));
1398 input
.inputProc
= &ca_render_cb
;
1399 input
.inputProcRefCon
= This
;
1401 sc
= AudioUnitSetProperty(This
->unit
, kAudioUnitProperty_SetRenderCallback
,
1402 kAudioUnitScope_Input
, 0, &input
, sizeof(input
));
1404 WARN("Couldn't set callback: %lx\n", sc
);
1405 CoTaskMemFree(This
->fmt
);
1407 OSSpinLockUnlock(&This
->lock
);
1408 return osstatus_to_hresult(sc
);
1412 sc
= AudioUnitInitialize(This
->unit
);
1414 WARN("Couldn't initialize: %lx\n", sc
);
1415 if(This
->converter
){
1416 AudioConverterDispose(This
->converter
);
1417 This
->converter
= NULL
;
1419 CoTaskMemFree(This
->fmt
);
1421 OSSpinLockUnlock(&This
->lock
);
1422 return osstatus_to_hresult(sc
);
1425 /* we play audio continuously because AudioOutputUnitStart sometimes takes
1426 * a while to return */
1427 sc
= AudioOutputUnitStart(This
->unit
);
1429 WARN("Unit failed to start: %lx\n", sc
);
1430 if(This
->converter
){
1431 AudioConverterDispose(This
->converter
);
1432 This
->converter
= NULL
;
1434 CoTaskMemFree(This
->fmt
);
1436 OSSpinLockUnlock(&This
->lock
);
1437 return osstatus_to_hresult(sc
);
1440 This
->local_buffer
= HeapAlloc(GetProcessHeap(), 0, This
->bufsize_frames
* fmt
->nBlockAlign
);
1441 silence_buffer(This
, This
->local_buffer
, This
->bufsize_frames
);
1443 if(This
->dataflow
== eCapture
){
1444 This
->cap_bufsize_frames
= MulDiv(duration
, This
->dev_desc
.mSampleRate
, 10000000);
1445 This
->cap_buffer
= HeapAlloc(GetProcessHeap(), 0, This
->cap_bufsize_frames
* This
->fmt
->nBlockAlign
);
1448 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
1450 CoTaskMemFree(This
->fmt
);
1452 OSSpinLockUnlock(&This
->lock
);
1453 return E_OUTOFMEMORY
;
1456 for(i
= 0; i
< fmt
->nChannels
; ++i
)
1457 This
->vols
[i
] = 1.f
;
1460 This
->flags
= flags
;
1462 EnterCriticalSection(&g_sessions_lock
);
1464 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
1467 LeaveCriticalSection(&g_sessions_lock
);
1468 CoTaskMemFree(This
->fmt
);
1470 HeapFree(GetProcessHeap(), 0, This
->vols
);
1472 OSSpinLockUnlock(&This
->lock
);
1473 return E_INVALIDARG
;
1476 list_add_tail(&This
->session
->clients
, &This
->entry
);
1478 LeaveCriticalSection(&g_sessions_lock
);
1480 ca_setvol(This
, -1);
1482 This
->initted
= TRUE
;
1484 OSSpinLockUnlock(&This
->lock
);
1489 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
1492 ACImpl
*This
= impl_from_IAudioClient(iface
);
1494 TRACE("(%p)->(%p)\n", This
, frames
);
1499 OSSpinLockLock(&This
->lock
);
1502 OSSpinLockUnlock(&This
->lock
);
1503 return AUDCLNT_E_NOT_INITIALIZED
;
1506 *frames
= This
->bufsize_frames
;
1508 OSSpinLockUnlock(&This
->lock
);
1513 static HRESULT
ca_get_max_stream_latency(ACImpl
*This
, UInt32
*max
)
1515 AudioObjectPropertyAddress addr
;
1521 addr
.mScope
= This
->scope
;
1523 addr
.mSelector
= kAudioDevicePropertyStreams
;
1525 sc
= AudioObjectGetPropertyDataSize(This
->adevid
, &addr
, 0, NULL
,
1528 WARN("Unable to get size for _Streams property: %lx\n", sc
);
1529 return osstatus_to_hresult(sc
);
1532 ids
= HeapAlloc(GetProcessHeap(), 0, size
);
1534 return E_OUTOFMEMORY
;
1536 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
, &size
, ids
);
1538 WARN("Unable to get _Streams property: %lx\n", sc
);
1539 HeapFree(GetProcessHeap(), 0, ids
);
1540 return osstatus_to_hresult(sc
);
1543 nstreams
= size
/ sizeof(AudioStreamID
);
1546 addr
.mSelector
= kAudioStreamPropertyLatency
;
1547 for(i
= 0; i
< nstreams
; ++i
){
1550 size
= sizeof(latency
);
1551 sc
= AudioObjectGetPropertyData(ids
[i
], &addr
, 0, NULL
,
1554 WARN("Unable to get _Latency property: %lx\n", sc
);
1562 HeapFree(GetProcessHeap(), 0, ids
);
1567 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1568 REFERENCE_TIME
*out
)
1570 ACImpl
*This
= impl_from_IAudioClient(iface
);
1571 UInt32 latency
, stream_latency
, size
;
1572 AudioObjectPropertyAddress addr
;
1576 TRACE("(%p)->(%p)\n", This
, out
);
1581 OSSpinLockLock(&This
->lock
);
1584 OSSpinLockUnlock(&This
->lock
);
1585 return AUDCLNT_E_NOT_INITIALIZED
;
1588 addr
.mScope
= This
->scope
;
1589 addr
.mSelector
= kAudioDevicePropertyLatency
;
1592 size
= sizeof(latency
);
1593 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
,
1596 WARN("Couldn't get _Latency property: %lx\n", sc
);
1597 OSSpinLockUnlock(&This
->lock
);
1598 return osstatus_to_hresult(sc
);
1601 hr
= ca_get_max_stream_latency(This
, &stream_latency
);
1603 OSSpinLockUnlock(&This
->lock
);
1607 latency
+= stream_latency
;
1608 /* pretend we process audio in Period chunks, so max latency includes
1609 * the period time */
1610 *out
= MulDiv(latency
, 10000000, This
->fmt
->nSamplesPerSec
)
1611 + This
->period_ms
* 10000;
1613 OSSpinLockUnlock(&This
->lock
);
1618 static HRESULT
AudioClient_GetCurrentPadding_nolock(ACImpl
*This
,
1622 return AUDCLNT_E_NOT_INITIALIZED
;
1624 if(This
->dataflow
== eCapture
)
1625 capture_resample(This
);
1627 *numpad
= This
->held_frames
;
1632 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1635 ACImpl
*This
= impl_from_IAudioClient(iface
);
1638 TRACE("(%p)->(%p)\n", This
, numpad
);
1643 OSSpinLockLock(&This
->lock
);
1645 hr
= AudioClient_GetCurrentPadding_nolock(This
, numpad
);
1647 OSSpinLockUnlock(&This
->lock
);
1652 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1653 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*pwfx
,
1654 WAVEFORMATEX
**outpwfx
)
1656 ACImpl
*This
= impl_from_IAudioClient(iface
);
1657 AudioStreamBasicDescription dev_desc
;
1658 AudioConverterRef converter
;
1659 AudioComponentInstance unit
;
1660 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)pwfx
;
1663 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, pwfx
, outpwfx
);
1665 if(!pwfx
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !outpwfx
))
1668 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1669 return E_INVALIDARG
;
1671 if(pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1672 pwfx
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1673 return E_INVALIDARG
;
1679 if(mode
!= AUDCLNT_SHAREMODE_SHARED
)
1683 if(pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1684 if(pwfx
->nAvgBytesPerSec
== 0 ||
1685 pwfx
->nBlockAlign
== 0 ||
1686 fmtex
->Samples
.wValidBitsPerSample
> pwfx
->wBitsPerSample
)
1687 return E_INVALIDARG
;
1688 if(fmtex
->Samples
.wValidBitsPerSample
< pwfx
->wBitsPerSample
)
1690 if(mode
== AUDCLNT_SHAREMODE_EXCLUSIVE
){
1691 if(fmtex
->dwChannelMask
== 0 ||
1692 fmtex
->dwChannelMask
& SPEAKER_RESERVED
)
1697 if(pwfx
->nBlockAlign
!= pwfx
->nChannels
* pwfx
->wBitsPerSample
/ 8 ||
1698 pwfx
->nAvgBytesPerSec
!= pwfx
->nBlockAlign
* pwfx
->nSamplesPerSec
)
1701 if(pwfx
->nChannels
== 0)
1702 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1704 unit
= get_audiounit(This
->dataflow
, This
->adevid
);
1707 hr
= ca_setup_audiounit(This
->dataflow
, unit
, pwfx
, &dev_desc
, &converter
);
1708 AudioComponentInstanceDispose(unit
);
1713 AudioConverterDispose(converter
);
1719 hr
= IAudioClient_GetMixFormat(&This
->IAudioClient_iface
, outpwfx
);
1725 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1728 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1729 WAVEFORMATEX
**pwfx
)
1731 ACImpl
*This
= impl_from_IAudioClient(iface
);
1732 WAVEFORMATEXTENSIBLE
*fmt
;
1736 AudioBufferList
*buffers
;
1737 AudioObjectPropertyAddress addr
;
1740 TRACE("(%p)->(%p)\n", This
, pwfx
);
1746 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1748 return E_OUTOFMEMORY
;
1750 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1752 addr
.mScope
= This
->scope
;
1754 addr
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
1756 sc
= AudioObjectGetPropertyDataSize(This
->adevid
, &addr
, 0, NULL
, &size
);
1759 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc
);
1760 return osstatus_to_hresult(sc
);
1763 buffers
= HeapAlloc(GetProcessHeap(), 0, size
);
1766 return E_OUTOFMEMORY
;
1769 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
,
1773 HeapFree(GetProcessHeap(), 0, buffers
);
1774 WARN("Unable to get _StreamConfiguration property: %lx\n", sc
);
1775 return osstatus_to_hresult(sc
);
1778 fmt
->Format
.nChannels
= 0;
1779 for(i
= 0; i
< buffers
->mNumberBuffers
; ++i
)
1780 fmt
->Format
.nChannels
+= buffers
->mBuffers
[i
].mNumberChannels
;
1782 HeapFree(GetProcessHeap(), 0, buffers
);
1784 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1786 addr
.mSelector
= kAudioDevicePropertyNominalSampleRate
;
1787 size
= sizeof(Float64
);
1788 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
, &size
, &rate
);
1791 WARN("Unable to get _NominalSampleRate property: %lx\n", sc
);
1792 return osstatus_to_hresult(sc
);
1794 fmt
->Format
.nSamplesPerSec
= rate
;
1796 fmt
->Format
.wBitsPerSample
= 32;
1797 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1799 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1800 fmt
->Format
.nChannels
) / 8;
1801 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1802 fmt
->Format
.nBlockAlign
;
1804 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1805 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1807 *pwfx
= (WAVEFORMATEX
*)fmt
;
1813 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1814 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1816 ACImpl
*This
= impl_from_IAudioClient(iface
);
1818 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1820 if(!defperiod
&& !minperiod
)
1824 *defperiod
= DefaultPeriod
;
1826 *minperiod
= MinimumPeriod
;
1831 void CALLBACK
ca_period_cb(void *user
, BOOLEAN timer
)
1833 ACImpl
*This
= user
;
1836 SetEvent(This
->event
);
1839 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1841 ACImpl
*This
= impl_from_IAudioClient(iface
);
1843 TRACE("(%p)\n", This
);
1845 OSSpinLockLock(&This
->lock
);
1848 OSSpinLockUnlock(&This
->lock
);
1849 return AUDCLNT_E_NOT_INITIALIZED
;
1853 OSSpinLockUnlock(&This
->lock
);
1854 return AUDCLNT_E_NOT_STOPPED
;
1857 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1858 OSSpinLockUnlock(&This
->lock
);
1859 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1862 if(This
->event
&& !This
->timer
)
1863 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
, ca_period_cb
,
1864 This
, 0, This
->period_ms
, WT_EXECUTEINTIMERTHREAD
)){
1866 OSSpinLockUnlock(&This
->lock
);
1867 WARN("Unable to create timer: %u\n", GetLastError());
1868 return E_OUTOFMEMORY
;
1871 This
->playing
= TRUE
;
1873 OSSpinLockUnlock(&This
->lock
);
1878 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1880 ACImpl
*This
= impl_from_IAudioClient(iface
);
1882 TRACE("(%p)\n", This
);
1884 OSSpinLockLock(&This
->lock
);
1887 OSSpinLockUnlock(&This
->lock
);
1888 return AUDCLNT_E_NOT_INITIALIZED
;
1892 OSSpinLockUnlock(&This
->lock
);
1896 This
->playing
= FALSE
;
1898 OSSpinLockUnlock(&This
->lock
);
1903 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1905 ACImpl
*This
= impl_from_IAudioClient(iface
);
1907 TRACE("(%p)\n", This
);
1909 OSSpinLockLock(&This
->lock
);
1912 OSSpinLockUnlock(&This
->lock
);
1913 return AUDCLNT_E_NOT_INITIALIZED
;
1917 OSSpinLockUnlock(&This
->lock
);
1918 return AUDCLNT_E_NOT_STOPPED
;
1921 if(This
->getbuf_last
){
1922 OSSpinLockUnlock(&This
->lock
);
1923 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
1926 if(This
->dataflow
== eRender
){
1927 This
->written_frames
= 0;
1929 This
->written_frames
+= This
->held_frames
;
1932 This
->held_frames
= 0;
1933 This
->lcl_offs_frames
= 0;
1934 This
->wri_offs_frames
= 0;
1935 This
->cap_offs_frames
= 0;
1936 This
->cap_held_frames
= 0;
1938 OSSpinLockUnlock(&This
->lock
);
1943 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1946 ACImpl
*This
= impl_from_IAudioClient(iface
);
1948 TRACE("(%p)->(%p)\n", This
, event
);
1951 return E_INVALIDARG
;
1953 OSSpinLockLock(&This
->lock
);
1956 OSSpinLockUnlock(&This
->lock
);
1957 return AUDCLNT_E_NOT_INITIALIZED
;
1960 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1961 OSSpinLockUnlock(&This
->lock
);
1962 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1966 OSSpinLockUnlock(&This
->lock
);
1967 FIXME("called twice\n");
1968 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
1971 This
->event
= event
;
1973 OSSpinLockUnlock(&This
->lock
);
1978 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1981 ACImpl
*This
= impl_from_IAudioClient(iface
);
1983 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1989 OSSpinLockLock(&This
->lock
);
1992 OSSpinLockUnlock(&This
->lock
);
1993 return AUDCLNT_E_NOT_INITIALIZED
;
1996 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1997 if(This
->dataflow
!= eRender
){
1998 OSSpinLockUnlock(&This
->lock
);
1999 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
2001 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
2002 *ppv
= &This
->IAudioRenderClient_iface
;
2003 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
2004 if(This
->dataflow
!= eCapture
){
2005 OSSpinLockUnlock(&This
->lock
);
2006 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
2008 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
2009 *ppv
= &This
->IAudioCaptureClient_iface
;
2010 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
2011 IAudioClock_AddRef(&This
->IAudioClock_iface
);
2012 *ppv
= &This
->IAudioClock_iface
;
2013 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
2014 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
2015 *ppv
= &This
->IAudioStreamVolume_iface
;
2016 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
2017 if(!This
->session_wrapper
){
2018 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
2019 if(!This
->session_wrapper
){
2020 OSSpinLockUnlock(&This
->lock
);
2021 return E_OUTOFMEMORY
;
2024 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
2026 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
2027 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
2028 if(!This
->session_wrapper
){
2029 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
2030 if(!This
->session_wrapper
){
2031 OSSpinLockUnlock(&This
->lock
);
2032 return E_OUTOFMEMORY
;
2035 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
2037 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
2038 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
2039 if(!This
->session_wrapper
){
2040 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
2041 if(!This
->session_wrapper
){
2042 OSSpinLockUnlock(&This
->lock
);
2043 return E_OUTOFMEMORY
;
2046 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
2048 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
2052 OSSpinLockUnlock(&This
->lock
);
2056 OSSpinLockUnlock(&This
->lock
);
2058 FIXME("stub %s\n", debugstr_guid(riid
));
2059 return E_NOINTERFACE
;
2062 static const IAudioClientVtbl AudioClient_Vtbl
=
2064 AudioClient_QueryInterface
,
2066 AudioClient_Release
,
2067 AudioClient_Initialize
,
2068 AudioClient_GetBufferSize
,
2069 AudioClient_GetStreamLatency
,
2070 AudioClient_GetCurrentPadding
,
2071 AudioClient_IsFormatSupported
,
2072 AudioClient_GetMixFormat
,
2073 AudioClient_GetDevicePeriod
,
2077 AudioClient_SetEventHandle
,
2078 AudioClient_GetService
2081 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
2082 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
2084 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2085 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2091 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2092 IsEqualIID(riid
, &IID_IAudioRenderClient
))
2094 else if(IsEqualIID(riid
, &IID_IMarshal
))
2095 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
2098 IUnknown_AddRef((IUnknown
*)*ppv
);
2102 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2103 return E_NOINTERFACE
;
2106 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
2108 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2109 return AudioClient_AddRef(&This
->IAudioClient_iface
);
2112 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
2114 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2115 return AudioClient_Release(&This
->IAudioClient_iface
);
2118 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
2119 UINT32 frames
, BYTE
**data
)
2121 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2125 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
2131 OSSpinLockLock(&This
->lock
);
2133 if(This
->getbuf_last
){
2134 OSSpinLockUnlock(&This
->lock
);
2135 return AUDCLNT_E_OUT_OF_ORDER
;
2139 OSSpinLockUnlock(&This
->lock
);
2143 hr
= AudioClient_GetCurrentPadding_nolock(This
, &pad
);
2145 OSSpinLockUnlock(&This
->lock
);
2149 if(pad
+ frames
> This
->bufsize_frames
){
2150 OSSpinLockUnlock(&This
->lock
);
2151 return AUDCLNT_E_BUFFER_TOO_LARGE
;
2154 if(This
->wri_offs_frames
+ frames
> This
->bufsize_frames
){
2155 if(This
->tmp_buffer_frames
< frames
){
2156 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
2157 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0, frames
* This
->fmt
->nBlockAlign
);
2158 if(!This
->tmp_buffer
){
2159 OSSpinLockUnlock(&This
->lock
);
2160 return E_OUTOFMEMORY
;
2162 This
->tmp_buffer_frames
= frames
;
2164 *data
= This
->tmp_buffer
;
2165 This
->getbuf_last
= -frames
;
2167 *data
= This
->local_buffer
+ This
->wri_offs_frames
* This
->fmt
->nBlockAlign
;
2168 This
->getbuf_last
= frames
;
2171 silence_buffer(This
, *data
, frames
);
2173 OSSpinLockUnlock(&This
->lock
);
2178 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
2179 IAudioRenderClient
*iface
, UINT32 frames
, DWORD flags
)
2181 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2184 TRACE("(%p)->(%u, %x)\n", This
, frames
, flags
);
2186 OSSpinLockLock(&This
->lock
);
2189 This
->getbuf_last
= 0;
2190 OSSpinLockUnlock(&This
->lock
);
2194 if(!This
->getbuf_last
){
2195 OSSpinLockUnlock(&This
->lock
);
2196 return AUDCLNT_E_OUT_OF_ORDER
;
2199 if(frames
> (This
->getbuf_last
>= 0 ? This
->getbuf_last
: -This
->getbuf_last
)){
2200 OSSpinLockUnlock(&This
->lock
);
2201 return AUDCLNT_E_INVALID_SIZE
;
2204 if(This
->getbuf_last
>= 0)
2205 buffer
= This
->local_buffer
+ This
->wri_offs_frames
* This
->fmt
->nBlockAlign
;
2207 buffer
= This
->tmp_buffer
;
2209 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
)
2210 silence_buffer(This
, buffer
, frames
);
2212 if(This
->getbuf_last
< 0)
2213 ca_wrap_buffer(This
->local_buffer
,
2214 This
->wri_offs_frames
* This
->fmt
->nBlockAlign
,
2215 This
->bufsize_frames
* This
->fmt
->nBlockAlign
,
2216 buffer
, frames
* This
->fmt
->nBlockAlign
);
2219 This
->wri_offs_frames
+= frames
;
2220 This
->wri_offs_frames
%= This
->bufsize_frames
;
2221 This
->held_frames
+= frames
;
2222 This
->written_frames
+= frames
;
2223 This
->getbuf_last
= 0;
2225 OSSpinLockUnlock(&This
->lock
);
2230 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
2231 AudioRenderClient_QueryInterface
,
2232 AudioRenderClient_AddRef
,
2233 AudioRenderClient_Release
,
2234 AudioRenderClient_GetBuffer
,
2235 AudioRenderClient_ReleaseBuffer
2238 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
2239 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
2241 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2242 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2248 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2249 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
2251 else if(IsEqualIID(riid
, &IID_IMarshal
))
2252 return IUnknown_QueryInterface(This
->pUnkFTMarshal
, riid
, ppv
);
2255 IUnknown_AddRef((IUnknown
*)*ppv
);
2259 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2260 return E_NOINTERFACE
;
2263 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
2265 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2266 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2269 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
2271 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2272 return IAudioClient_Release(&This
->IAudioClient_iface
);
2275 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
2276 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
2279 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2280 UINT32 chunk_bytes
, chunk_frames
;
2282 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
2285 if(!data
|| !frames
|| !flags
)
2288 OSSpinLockLock(&This
->lock
);
2290 if(This
->getbuf_last
){
2291 OSSpinLockUnlock(&This
->lock
);
2292 return AUDCLNT_E_OUT_OF_ORDER
;
2295 capture_resample(This
);
2297 if(This
->held_frames
< This
->period_frames
){
2299 OSSpinLockUnlock(&This
->lock
);
2300 return AUDCLNT_S_BUFFER_EMPTY
;
2305 chunk_frames
= This
->bufsize_frames
- This
->lcl_offs_frames
;
2306 if(chunk_frames
< This
->period_frames
){
2307 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
2308 if(!This
->tmp_buffer
)
2309 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0, This
->period_frames
* This
->fmt
->nBlockAlign
);
2310 *data
= This
->tmp_buffer
;
2311 memcpy(*data
, This
->local_buffer
+ This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
, chunk_bytes
);
2312 memcpy((*data
) + chunk_bytes
, This
->local_buffer
, This
->period_frames
* This
->fmt
->nBlockAlign
- chunk_bytes
);
2314 *data
= This
->local_buffer
+ This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2316 This
->getbuf_last
= *frames
= This
->period_frames
;
2319 *devpos
= This
->written_frames
;
2320 if(qpcpos
){ /* fixme: qpc of recording time */
2321 LARGE_INTEGER stamp
, freq
;
2322 QueryPerformanceCounter(&stamp
);
2323 QueryPerformanceFrequency(&freq
);
2324 *qpcpos
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2327 OSSpinLockUnlock(&This
->lock
);
2332 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2333 IAudioCaptureClient
*iface
, UINT32 done
)
2335 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2337 TRACE("(%p)->(%u)\n", This
, done
);
2339 OSSpinLockLock(&This
->lock
);
2342 This
->getbuf_last
= 0;
2343 OSSpinLockUnlock(&This
->lock
);
2347 if(!This
->getbuf_last
){
2348 OSSpinLockUnlock(&This
->lock
);
2349 return AUDCLNT_E_OUT_OF_ORDER
;
2352 if(This
->getbuf_last
!= done
){
2353 OSSpinLockUnlock(&This
->lock
);
2354 return AUDCLNT_E_INVALID_SIZE
;
2357 This
->written_frames
+= done
;
2358 This
->held_frames
-= done
;
2359 This
->lcl_offs_frames
+= done
;
2360 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2361 This
->getbuf_last
= 0;
2363 OSSpinLockUnlock(&This
->lock
);
2368 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2369 IAudioCaptureClient
*iface
, UINT32
*frames
)
2371 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2373 TRACE("(%p)->(%p)\n", This
, frames
);
2378 OSSpinLockLock(&This
->lock
);
2380 capture_resample(This
);
2382 if(This
->held_frames
>= This
->period_frames
)
2383 *frames
= This
->period_frames
;
2387 OSSpinLockUnlock(&This
->lock
);
2392 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2394 AudioCaptureClient_QueryInterface
,
2395 AudioCaptureClient_AddRef
,
2396 AudioCaptureClient_Release
,
2397 AudioCaptureClient_GetBuffer
,
2398 AudioCaptureClient_ReleaseBuffer
,
2399 AudioCaptureClient_GetNextPacketSize
2402 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2403 REFIID riid
, void **ppv
)
2405 ACImpl
*This
= impl_from_IAudioClock(iface
);
2407 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2413 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2415 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2416 *ppv
= &This
->IAudioClock2_iface
;
2418 IUnknown_AddRef((IUnknown
*)*ppv
);
2422 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2423 return E_NOINTERFACE
;
2426 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2428 ACImpl
*This
= impl_from_IAudioClock(iface
);
2429 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2432 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2434 ACImpl
*This
= impl_from_IAudioClock(iface
);
2435 return IAudioClient_Release(&This
->IAudioClient_iface
);
2438 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2440 ACImpl
*This
= impl_from_IAudioClock(iface
);
2442 TRACE("(%p)->(%p)\n", This
, freq
);
2444 if(This
->share
== AUDCLNT_SHAREMODE_SHARED
)
2445 *freq
= (UINT64
)This
->fmt
->nSamplesPerSec
* This
->fmt
->nBlockAlign
;
2447 *freq
= This
->fmt
->nSamplesPerSec
;
2452 static HRESULT
AudioClock_GetPosition_nolock(ACImpl
*This
,
2453 UINT64
*pos
, UINT64
*qpctime
)
2455 *pos
= This
->written_frames
- This
->held_frames
;
2457 if(This
->share
== AUDCLNT_SHAREMODE_SHARED
)
2458 *pos
*= This
->fmt
->nBlockAlign
;
2461 LARGE_INTEGER stamp
, freq
;
2462 QueryPerformanceCounter(&stamp
);
2463 QueryPerformanceFrequency(&freq
);
2464 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2470 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2473 ACImpl
*This
= impl_from_IAudioClock(iface
);
2476 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2481 OSSpinLockLock(&This
->lock
);
2483 hr
= AudioClock_GetPosition_nolock(This
, pos
, qpctime
);
2485 OSSpinLockUnlock(&This
->lock
);
2490 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2493 ACImpl
*This
= impl_from_IAudioClock(iface
);
2495 TRACE("(%p)->(%p)\n", This
, chars
);
2500 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2505 static const IAudioClockVtbl AudioClock_Vtbl
=
2507 AudioClock_QueryInterface
,
2510 AudioClock_GetFrequency
,
2511 AudioClock_GetPosition
,
2512 AudioClock_GetCharacteristics
2515 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2516 REFIID riid
, void **ppv
)
2518 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2519 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2522 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2524 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2525 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2528 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2530 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2531 return IAudioClient_Release(&This
->IAudioClient_iface
);
2534 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2535 UINT64
*pos
, UINT64
*qpctime
)
2537 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2539 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2544 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2546 AudioClock2_QueryInterface
,
2548 AudioClock2_Release
,
2549 AudioClock2_GetDevicePosition
2552 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2554 AudioSessionWrapper
*ret
;
2556 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2557 sizeof(AudioSessionWrapper
));
2561 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2562 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2563 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2567 ret
->client
= client
;
2569 ret
->session
= client
->session
;
2570 AudioClient_AddRef(&client
->IAudioClient_iface
);
2576 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2577 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2579 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2585 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2586 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2587 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2590 IUnknown_AddRef((IUnknown
*)*ppv
);
2594 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2595 return E_NOINTERFACE
;
2598 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2600 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2602 ref
= InterlockedIncrement(&This
->ref
);
2603 TRACE("(%p) Refcount now %u\n", This
, ref
);
2607 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2609 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2611 ref
= InterlockedDecrement(&This
->ref
);
2612 TRACE("(%p) Refcount now %u\n", This
, ref
);
2615 OSSpinLockLock(&This
->client
->lock
);
2616 This
->client
->session_wrapper
= NULL
;
2617 OSSpinLockUnlock(&This
->client
->lock
);
2618 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2620 HeapFree(GetProcessHeap(), 0, This
);
2625 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2626 AudioSessionState
*state
)
2628 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2631 TRACE("(%p)->(%p)\n", This
, state
);
2634 return NULL_PTR_ERR
;
2636 EnterCriticalSection(&g_sessions_lock
);
2638 if(list_empty(&This
->session
->clients
)){
2639 *state
= AudioSessionStateExpired
;
2640 LeaveCriticalSection(&g_sessions_lock
);
2644 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2645 OSSpinLockLock(&client
->lock
);
2646 if(client
->playing
){
2647 *state
= AudioSessionStateActive
;
2648 OSSpinLockUnlock(&client
->lock
);
2649 LeaveCriticalSection(&g_sessions_lock
);
2652 OSSpinLockUnlock(&client
->lock
);
2655 LeaveCriticalSection(&g_sessions_lock
);
2657 *state
= AudioSessionStateInactive
;
2662 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2663 IAudioSessionControl2
*iface
, WCHAR
**name
)
2665 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2667 FIXME("(%p)->(%p) - stub\n", This
, name
);
2672 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2673 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2675 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2677 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2682 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2683 IAudioSessionControl2
*iface
, WCHAR
**path
)
2685 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2687 FIXME("(%p)->(%p) - stub\n", This
, path
);
2692 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2693 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2695 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2697 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2702 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2703 IAudioSessionControl2
*iface
, GUID
*group
)
2705 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2707 FIXME("(%p)->(%p) - stub\n", This
, group
);
2712 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2713 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2715 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2717 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2718 debugstr_guid(session
));
2723 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2724 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2726 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2728 FIXME("(%p)->(%p) - stub\n", This
, events
);
2733 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2734 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2736 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2738 FIXME("(%p)->(%p) - stub\n", This
, events
);
2743 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2744 IAudioSessionControl2
*iface
, WCHAR
**id
)
2746 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2748 FIXME("(%p)->(%p) - stub\n", This
, id
);
2753 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2754 IAudioSessionControl2
*iface
, WCHAR
**id
)
2756 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2758 FIXME("(%p)->(%p) - stub\n", This
, id
);
2763 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2764 IAudioSessionControl2
*iface
, DWORD
*pid
)
2766 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2768 TRACE("(%p)->(%p)\n", This
, pid
);
2773 *pid
= GetCurrentProcessId();
2778 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2779 IAudioSessionControl2
*iface
)
2781 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2783 TRACE("(%p)\n", This
);
2788 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2789 IAudioSessionControl2
*iface
, BOOL optout
)
2791 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2793 TRACE("(%p)->(%d)\n", This
, optout
);
2798 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2800 AudioSessionControl_QueryInterface
,
2801 AudioSessionControl_AddRef
,
2802 AudioSessionControl_Release
,
2803 AudioSessionControl_GetState
,
2804 AudioSessionControl_GetDisplayName
,
2805 AudioSessionControl_SetDisplayName
,
2806 AudioSessionControl_GetIconPath
,
2807 AudioSessionControl_SetIconPath
,
2808 AudioSessionControl_GetGroupingParam
,
2809 AudioSessionControl_SetGroupingParam
,
2810 AudioSessionControl_RegisterAudioSessionNotification
,
2811 AudioSessionControl_UnregisterAudioSessionNotification
,
2812 AudioSessionControl_GetSessionIdentifier
,
2813 AudioSessionControl_GetSessionInstanceIdentifier
,
2814 AudioSessionControl_GetProcessId
,
2815 AudioSessionControl_IsSystemSoundsSession
,
2816 AudioSessionControl_SetDuckingPreference
2819 /* index == -1 means set all channels, otherwise sets only the given channel */
2820 static HRESULT
ca_setvol(ACImpl
*This
, UINT32 index
)
2825 if(This
->session
->mute
)
2828 if(index
== (UINT32
)-1){
2831 for(i
= 0; i
< This
->fmt
->nChannels
; ++i
){
2833 tmp
= This
->session
->master_vol
*
2834 This
->session
->channel_vols
[i
] * This
->vols
[i
];
2835 level
= tmp
< level
? tmp
: level
;
2838 level
= This
->session
->master_vol
*
2839 This
->session
->channel_vols
[index
] * This
->vols
[index
];
2842 sc
= AudioUnitSetParameter(This
->unit
, kHALOutputParam_Volume
,
2843 kAudioUnitScope_Global
, 0, level
, 0);
2845 WARN("Couldn't set volume: %lx\n", sc
);
2850 static HRESULT
ca_session_setvol(AudioSession
*session
, UINT32 index
)
2855 LIST_FOR_EACH_ENTRY(client
, &session
->clients
, ACImpl
, entry
){
2857 hr
= ca_setvol(client
, index
);
2865 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2866 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2868 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2874 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2875 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2878 IUnknown_AddRef((IUnknown
*)*ppv
);
2882 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2883 return E_NOINTERFACE
;
2886 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2888 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2889 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2892 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2894 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2895 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2898 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2899 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2901 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2902 AudioSession
*session
= This
->session
;
2905 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2907 if(level
< 0.f
|| level
> 1.f
)
2908 return E_INVALIDARG
;
2911 FIXME("Notifications not supported yet\n");
2913 EnterCriticalSection(&session
->lock
);
2915 session
->master_vol
= level
;
2917 ret
= ca_session_setvol(session
, -1);
2919 LeaveCriticalSection(&session
->lock
);
2924 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2925 ISimpleAudioVolume
*iface
, float *level
)
2927 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2928 AudioSession
*session
= This
->session
;
2930 TRACE("(%p)->(%p)\n", session
, level
);
2933 return NULL_PTR_ERR
;
2935 *level
= session
->master_vol
;
2940 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2941 BOOL mute
, const GUID
*context
)
2943 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2944 AudioSession
*session
= This
->session
;
2946 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2949 FIXME("Notifications not supported yet\n");
2951 EnterCriticalSection(&session
->lock
);
2953 session
->mute
= mute
;
2955 ca_session_setvol(session
, -1);
2957 LeaveCriticalSection(&session
->lock
);
2962 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2965 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2966 AudioSession
*session
= This
->session
;
2968 TRACE("(%p)->(%p)\n", session
, mute
);
2971 return NULL_PTR_ERR
;
2973 *mute
= session
->mute
;
2978 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2980 SimpleAudioVolume_QueryInterface
,
2981 SimpleAudioVolume_AddRef
,
2982 SimpleAudioVolume_Release
,
2983 SimpleAudioVolume_SetMasterVolume
,
2984 SimpleAudioVolume_GetMasterVolume
,
2985 SimpleAudioVolume_SetMute
,
2986 SimpleAudioVolume_GetMute
2989 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2990 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2992 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2998 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2999 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
3002 IUnknown_AddRef((IUnknown
*)*ppv
);
3006 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3007 return E_NOINTERFACE
;
3010 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
3012 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3013 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
3016 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
3018 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3019 return IAudioClient_Release(&This
->IAudioClient_iface
);
3022 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
3023 IAudioStreamVolume
*iface
, UINT32
*out
)
3025 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3027 TRACE("(%p)->(%p)\n", This
, out
);
3032 *out
= This
->fmt
->nChannels
;
3037 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
3038 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
3040 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3043 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
3045 if(level
< 0.f
|| level
> 1.f
)
3046 return E_INVALIDARG
;
3048 if(index
>= This
->fmt
->nChannels
)
3049 return E_INVALIDARG
;
3051 OSSpinLockLock(&This
->lock
);
3053 This
->vols
[index
] = level
;
3055 WARN("CoreAudio doesn't support per-channel volume control\n");
3056 ret
= ca_setvol(This
, index
);
3058 OSSpinLockUnlock(&This
->lock
);
3063 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
3064 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
3066 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3068 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
3073 if(index
>= This
->fmt
->nChannels
)
3074 return E_INVALIDARG
;
3076 *level
= This
->vols
[index
];
3081 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
3082 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
3084 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3088 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
3093 if(count
!= This
->fmt
->nChannels
)
3094 return E_INVALIDARG
;
3096 OSSpinLockLock(&This
->lock
);
3098 for(i
= 0; i
< count
; ++i
)
3099 This
->vols
[i
] = levels
[i
];
3101 ret
= ca_setvol(This
, -1);
3103 OSSpinLockUnlock(&This
->lock
);
3108 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
3109 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
3111 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
3114 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
3119 if(count
!= This
->fmt
->nChannels
)
3120 return E_INVALIDARG
;
3122 OSSpinLockLock(&This
->lock
);
3124 for(i
= 0; i
< count
; ++i
)
3125 levels
[i
] = This
->vols
[i
];
3127 OSSpinLockUnlock(&This
->lock
);
3132 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
3134 AudioStreamVolume_QueryInterface
,
3135 AudioStreamVolume_AddRef
,
3136 AudioStreamVolume_Release
,
3137 AudioStreamVolume_GetChannelCount
,
3138 AudioStreamVolume_SetChannelVolume
,
3139 AudioStreamVolume_GetChannelVolume
,
3140 AudioStreamVolume_SetAllVolumes
,
3141 AudioStreamVolume_GetAllVolumes
3144 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
3145 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
3147 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3153 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3154 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
3157 IUnknown_AddRef((IUnknown
*)*ppv
);
3161 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3162 return E_NOINTERFACE
;
3165 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
3167 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3168 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
3171 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
3173 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3174 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
3177 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
3178 IChannelAudioVolume
*iface
, UINT32
*out
)
3180 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3181 AudioSession
*session
= This
->session
;
3183 TRACE("(%p)->(%p)\n", session
, out
);
3186 return NULL_PTR_ERR
;
3188 *out
= session
->channel_count
;
3193 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
3194 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
3195 const GUID
*context
)
3197 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3198 AudioSession
*session
= This
->session
;
3201 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
3202 wine_dbgstr_guid(context
));
3204 if(level
< 0.f
|| level
> 1.f
)
3205 return E_INVALIDARG
;
3207 if(index
>= session
->channel_count
)
3208 return E_INVALIDARG
;
3211 FIXME("Notifications not supported yet\n");
3213 EnterCriticalSection(&session
->lock
);
3215 session
->channel_vols
[index
] = level
;
3217 WARN("CoreAudio doesn't support per-channel volume control\n");
3218 ret
= ca_session_setvol(session
, index
);
3220 LeaveCriticalSection(&session
->lock
);
3225 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
3226 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
3228 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3229 AudioSession
*session
= This
->session
;
3231 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
3234 return NULL_PTR_ERR
;
3236 if(index
>= session
->channel_count
)
3237 return E_INVALIDARG
;
3239 *level
= session
->channel_vols
[index
];
3244 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
3245 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
3246 const GUID
*context
)
3248 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3249 AudioSession
*session
= This
->session
;
3253 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
3254 wine_dbgstr_guid(context
));
3257 return NULL_PTR_ERR
;
3259 if(count
!= session
->channel_count
)
3260 return E_INVALIDARG
;
3263 FIXME("Notifications not supported yet\n");
3265 EnterCriticalSection(&session
->lock
);
3267 for(i
= 0; i
< count
; ++i
)
3268 session
->channel_vols
[i
] = levels
[i
];
3270 ret
= ca_session_setvol(session
, -1);
3272 LeaveCriticalSection(&session
->lock
);
3277 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
3278 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
3280 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3281 AudioSession
*session
= This
->session
;
3284 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
3287 return NULL_PTR_ERR
;
3289 if(count
!= session
->channel_count
)
3290 return E_INVALIDARG
;
3292 for(i
= 0; i
< count
; ++i
)
3293 levels
[i
] = session
->channel_vols
[i
];
3298 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
3300 ChannelAudioVolume_QueryInterface
,
3301 ChannelAudioVolume_AddRef
,
3302 ChannelAudioVolume_Release
,
3303 ChannelAudioVolume_GetChannelCount
,
3304 ChannelAudioVolume_SetChannelVolume
,
3305 ChannelAudioVolume_GetChannelVolume
,
3306 ChannelAudioVolume_SetAllVolumes
,
3307 ChannelAudioVolume_GetAllVolumes
3310 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
3311 REFIID riid
, void **ppv
)
3313 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3319 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3320 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
3321 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
3324 IUnknown_AddRef((IUnknown
*)*ppv
);
3328 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3329 return E_NOINTERFACE
;
3332 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
3334 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3336 ref
= InterlockedIncrement(&This
->ref
);
3337 TRACE("(%p) Refcount now %u\n", This
, ref
);
3341 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
3343 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3345 ref
= InterlockedDecrement(&This
->ref
);
3346 TRACE("(%p) Refcount now %u\n", This
, ref
);
3348 HeapFree(GetProcessHeap(), 0, This
);
3352 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3353 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3354 IAudioSessionControl
**out
)
3356 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3357 AudioSession
*session
;
3358 AudioSessionWrapper
*wrapper
;
3361 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3364 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3368 wrapper
= AudioSessionWrapper_Create(NULL
);
3370 return E_OUTOFMEMORY
;
3372 wrapper
->session
= session
;
3374 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3379 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3380 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3381 ISimpleAudioVolume
**out
)
3383 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3384 AudioSession
*session
;
3385 AudioSessionWrapper
*wrapper
;
3388 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3391 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3395 wrapper
= AudioSessionWrapper_Create(NULL
);
3397 return E_OUTOFMEMORY
;
3399 wrapper
->session
= session
;
3401 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3406 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3407 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3409 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3410 FIXME("(%p)->(%p) - stub\n", This
, out
);
3414 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3415 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3417 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3418 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3422 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3423 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3425 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3426 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3430 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3431 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3432 IAudioVolumeDuckNotification
*notification
)
3434 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3435 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3439 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3440 IAudioSessionManager2
*iface
,
3441 IAudioVolumeDuckNotification
*notification
)
3443 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3444 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3448 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3450 AudioSessionManager_QueryInterface
,
3451 AudioSessionManager_AddRef
,
3452 AudioSessionManager_Release
,
3453 AudioSessionManager_GetAudioSessionControl
,
3454 AudioSessionManager_GetSimpleAudioVolume
,
3455 AudioSessionManager_GetSessionEnumerator
,
3456 AudioSessionManager_RegisterSessionNotification
,
3457 AudioSessionManager_UnregisterSessionNotification
,
3458 AudioSessionManager_RegisterDuckNotification
,
3459 AudioSessionManager_UnregisterDuckNotification
3462 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3463 IAudioSessionManager2
**out
)
3467 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3469 return E_OUTOFMEMORY
;
3471 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3472 This
->device
= device
;
3475 *out
= &This
->IAudioSessionManager2_iface
;