TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / winecoreaudio.drv / mmdevdrv.c
blob60ff3d95e13508ee9ebe2cf81735ba6133d17497
1 /*
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
20 #define COBJMACROS
21 #include "config.h"
23 #define LoadResource __carbon_LoadResource
24 #define CompareString __carbon_CompareString
25 #define GetCurrentThread __carbon_GetCurrentThread
26 #define GetCurrentProcess __carbon_GetCurrentProcess
28 #include <stdarg.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <fcntl.h>
39 #include <unistd.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>
47 #undef LoadResource
48 #undef CompareString
49 #undef GetCurrentThread
50 #undef GetCurrentProcess
51 #undef _CDECL
52 #undef DPRINTF
54 #include "windef.h"
55 #include "winbase.h"
56 #include "winnls.h"
57 #include "winreg.h"
58 #include "wine/debug.h"
59 #include "wine/unicode.h"
60 #include "wine/list.h"
62 #include "ole2.h"
63 #include "mmdeviceapi.h"
64 #include "devpkey.h"
65 #include "dshow.h"
66 #include "dsound.h"
68 #include "initguid.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);
95 #endif
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;
102 struct ACImpl;
103 typedef struct ACImpl ACImpl;
105 typedef struct _AudioSession {
106 GUID guid;
107 struct list clients;
109 IMMDevice *device;
111 float master_vol;
112 UINT32 channel_count;
113 float *channel_vols;
114 BOOL mute;
116 CRITICAL_SECTION lock;
118 struct list entry;
119 } AudioSession;
121 typedef struct _AudioSessionWrapper {
122 IAudioSessionControl2 IAudioSessionControl2_iface;
123 IChannelAudioVolume IChannelAudioVolume_iface;
124 ISimpleAudioVolume ISimpleAudioVolume_iface;
126 LONG ref;
128 ACImpl *client;
129 AudioSession *session;
130 } AudioSessionWrapper;
132 struct ACImpl {
133 IAudioClient IAudioClient_iface;
134 IAudioRenderClient IAudioRenderClient_iface;
135 IAudioCaptureClient IAudioCaptureClient_iface;
136 IAudioClock IAudioClock_iface;
137 IAudioClock2 IAudioClock2_iface;
138 IAudioStreamVolume IAudioStreamVolume_iface;
140 LONG ref;
142 IMMDevice *parent;
143 IUnknown *pUnkFTMarshal;
145 WAVEFORMATEX *fmt;
147 EDataFlow dataflow;
148 DWORD flags;
149 AUDCLNT_SHAREMODE share;
150 HANDLE event;
151 float *vols;
153 BOOL initted;
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 */
159 HANDLE timer;
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;
164 INT32 getbuf_last;
165 BOOL playing;
166 BYTE *cap_buffer, *wrap_buffer, *resamp_buffer, *local_buffer, *tmp_buffer;
168 AudioSession *session;
169 AudioSessionWrapper *session_wrapper;
171 struct list entry;
173 OSSpinLock lock;
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;
190 LONG ref;
192 IMMDevice *device;
193 } SessionMgr;
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)
267 switch (reason)
269 case DLL_PROCESS_ATTACH:
270 g_timer_q = CreateTimerQueue();
271 if(!g_timer_q)
272 return FALSE;
273 break;
275 case DLL_PROCESS_DETACH:
276 if (reserved) break;
277 DeleteCriticalSection(&g_sessions_lock);
278 break;
280 return TRUE;
283 /* From <dlls/mmdevapi/mmdevapi.h> */
284 enum DriverPriority {
285 Priority_Unavailable = 0,
286 Priority_Low,
287 Priority_Neutral,
288 Priority_Preferred
291 int WINAPI AUDDRV_GetPriority(void)
293 return Priority_Neutral;
296 static HRESULT osstatus_to_hresult(OSStatus sc)
298 switch(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;
306 return E_FAIL;
309 static void set_device_guid(EDataFlow flow, HKEY drv_key, const WCHAR *key_name,
310 GUID *guid)
312 HKEY key;
313 BOOL opened = FALSE;
314 LONG lr;
316 if(!drv_key){
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);
321 return;
323 opened = TRUE;
326 lr = RegCreateKeyExW(drv_key, key_name, 0, NULL, 0, KEY_WRITE,
327 NULL, &key, NULL);
328 if(lr != ERROR_SUCCESS){
329 ERR("RegCreateKeyEx(%s) failed: %u\n", wine_dbgstr_w(key_name), lr);
330 goto exit;
333 lr = RegSetValueExW(key, guidW, 0, REG_BINARY, (BYTE*)guid,
334 sizeof(GUID));
335 if(lr != ERROR_SUCCESS)
336 ERR("RegSetValueEx(%s\\guid) failed: %u\n", wine_dbgstr_w(key_name), lr);
338 RegCloseKey(key);
339 exit:
340 if(opened)
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);
348 WCHAR key_name[256];
350 static const WCHAR key_fmt[] = {'%','u',0};
352 if(flow == eCapture)
353 key_name[0] = '1';
354 else
355 key_name[0] = '0';
356 key_name[1] = ',';
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);
366 RegCloseKey(key);
367 return;
369 ERR("Invalid type for device %s GUID: %u; ignoring and overwriting\n",
370 wine_dbgstr_w(key_name), type);
372 RegCloseKey(dev_key);
376 CoCreateGuid(guid);
378 set_device_guid(flow, key, key_name, guid);
380 if(key)
381 RegCloseKey(key);
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;
391 OSStatus sc;
392 int i, ndevices;
394 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
396 addr.mScope = kAudioObjectPropertyScopeGlobal;
397 addr.mElement = kAudioObjectPropertyElementMaster;
398 if(flow == eRender)
399 addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
400 else if(flow == eCapture)
401 addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
402 else
403 return E_INVALIDARG;
405 size = sizeof(default_id);
406 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
407 NULL, &size, &default_id);
408 if(sc != noErr){
409 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc);
410 default_id = -1;
413 addr.mSelector = kAudioHardwarePropertyDevices;
414 sc = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0,
415 NULL, &devsize);
416 if(sc != noErr){
417 WARN("Getting _Devices property size failed: %lx\n", sc);
418 return osstatus_to_hresult(sc);
421 devices = HeapAlloc(GetProcessHeap(), 0, devsize);
422 if(!devices)
423 return E_OUTOFMEMORY;
425 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
426 &devsize, devices);
427 if(sc != noErr){
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 *));
436 if(!*ids){
437 HeapFree(GetProcessHeap(), 0, devices);
438 return E_OUTOFMEMORY;
441 *guids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(GUID));
442 if(!*guids){
443 HeapFree(GetProcessHeap(), 0, *ids);
444 HeapFree(GetProcessHeap(), 0, devices);
445 return E_OUTOFMEMORY;
448 *num = 0;
449 *def_index = (UINT)-1;
450 for(i = 0; i < ndevices; ++i){
451 AudioBufferList *buffers;
452 CFStringRef name;
453 SIZE_T len;
454 int j;
456 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
457 if(flow == eRender)
458 addr.mScope = kAudioDevicePropertyScopeOutput;
459 else
460 addr.mScope = kAudioDevicePropertyScopeInput;
461 addr.mElement = 0;
462 sc = AudioObjectGetPropertyDataSize(devices[i], &addr, 0, NULL, &size);
463 if(sc != noErr){
464 WARN("Unable to get _StreamConfiguration property size for "
465 "device %lu: %lx\n", devices[i], sc);
466 continue;
469 buffers = HeapAlloc(GetProcessHeap(), 0, size);
470 if(!buffers){
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,
480 &size, buffers);
481 if(sc != noErr){
482 WARN("Unable to get _StreamConfiguration property for "
483 "device %lu: %lx\n", devices[i], sc);
484 HeapFree(GetProcessHeap(), 0, buffers);
485 continue;
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)
492 break;
493 if(j >= buffers->mNumberBuffers){
494 HeapFree(GetProcessHeap(), 0, buffers);
495 continue;
498 HeapFree(GetProcessHeap(), 0, buffers);
500 size = sizeof(name);
501 addr.mSelector = kAudioObjectPropertyName;
502 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
503 &size, &name);
504 if(sc != noErr){
505 WARN("Unable to get _Name property for device %lu: %lx\n",
506 devices[i], sc);
507 continue;
510 len = CFStringGetLength(name) + 1;
511 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
512 if(!(*ids)[*num]){
513 CFRelease(name);
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;
523 CFRelease(name);
525 get_device_guid(flow, devices[i], &(*guids)[*num]);
527 if(*def_index == (UINT)-1 && devices[i] == default_id)
528 *def_index = *num;
530 TRACE("device %u: id %s key %u%s\n", *num, debugstr_w((*ids)[*num]),
531 (unsigned int)devices[i], (*def_index == *num) ? " (default)" : "");
533 (*num)++;
536 if(*def_index == (UINT)-1)
537 *def_index = 0;
539 HeapFree(GetProcessHeap(), 0, devices);
541 return S_OK;
544 static BOOL get_deviceid_by_guid(GUID *guid, AudioDeviceID *id, EDataFlow *flow)
546 HKEY devices_key;
547 UINT i = 0;
548 WCHAR key_name[256];
549 DWORD key_name_size;
551 if(RegOpenKeyExW(HKEY_CURRENT_USER, drv_key_devicesW, 0, KEY_READ, &devices_key) != ERROR_SUCCESS){
552 ERR("No devices in registry?\n");
553 return FALSE;
556 while(1){
557 HKEY key;
558 DWORD size, type;
559 GUID reg_guid;
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)
564 break;
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));
568 continue;
571 size = sizeof(reg_guid);
572 if(RegQueryValueExW(key, guidW, 0, &type,
573 (BYTE*)&reg_guid, &size) == ERROR_SUCCESS){
574 if(IsEqualGUID(&reg_guid, guid)){
575 RegCloseKey(key);
576 RegCloseKey(devices_key);
578 TRACE("Found matching device key: %s\n", wine_dbgstr_w(key_name));
580 if(key_name[0] == '0')
581 *flow = eRender;
582 else if(key_name[0] == '1')
583 *flow = eCapture;
584 else{
585 ERR("Unknown device type: %c\n", key_name[0]);
586 return FALSE;
589 *id = strtoulW(key_name + 2, NULL, 10);
591 return TRUE;
595 RegCloseKey(key);
598 RegCloseKey(devices_key);
600 WARN("No matching device in registry for GUID %s\n", debugstr_guid(guid));
602 return FALSE;
605 static AudioComponentInstance get_audiounit(EDataFlow dataflow, AudioDeviceID adevid)
607 AudioComponentInstance unit;
608 AudioComponent comp;
609 AudioComponentDescription desc;
610 OSStatus sc;
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");
619 return NULL;
622 sc = AudioComponentInstanceNew(comp, &unit);
623 if(sc != noErr){
624 WARN("AudioComponentInstanceNew failed: %lx\n", sc);
625 return NULL;
628 if(dataflow == eCapture){
629 UInt32 enableio;
631 enableio = 1;
632 sc = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO,
633 kAudioUnitScope_Input, 1, &enableio, sizeof(enableio));
634 if(sc != noErr){
635 WARN("Couldn't enable I/O on input element: %lx\n", sc);
636 AudioComponentInstanceDispose(unit);
637 return NULL;
640 enableio = 0;
641 sc = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO,
642 kAudioUnitScope_Output, 0, &enableio, sizeof(enableio));
643 if(sc != noErr){
644 WARN("Couldn't disable I/O on output element: %lx\n", sc);
645 AudioComponentInstanceDispose(unit);
646 return NULL;
650 sc = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_CurrentDevice,
651 kAudioUnitScope_Global, 0, &adevid, sizeof(adevid));
652 if(sc != noErr){
653 WARN("Couldn't set audio unit device\n");
654 AudioComponentInstanceDispose(unit);
655 return NULL;
658 return unit;
661 HRESULT WINAPI AUDDRV_GetAudioEndpoint(GUID *guid, IMMDevice *dev, IAudioClient **out)
663 ACImpl *This;
664 AudioDeviceID adevid;
665 EDataFlow dataflow;
666 HRESULT hr;
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));
674 if(!This)
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;
690 else{
691 HeapFree(GetProcessHeap(), 0, This);
692 return E_INVALIDARG;
695 This->lock = 0;
697 hr = CoCreateFreeThreadedMarshaler((IUnknown *)&This->IAudioClient_iface,
698 (IUnknown **)&This->pUnkFTMarshal);
699 if (FAILED(hr)) {
700 HeapFree(GetProcessHeap(), 0, This);
701 return hr;
704 This->parent = dev;
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);
717 return S_OK;
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);
726 if(!ppv)
727 return E_POINTER;
728 *ppv = NULL;
729 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
730 *ppv = iface;
731 else if(IsEqualIID(riid, &IID_IMarshal))
732 return IUnknown_QueryInterface(This->pUnkFTMarshal, riid, ppv);
734 if(*ppv){
735 IUnknown_AddRef((IUnknown*)*ppv);
736 return S_OK;
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);
745 ULONG ref;
746 ref = InterlockedIncrement(&This->ref);
747 TRACE("(%p) Refcount now %u\n", This, ref);
748 return ref;
751 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
753 ACImpl *This = impl_from_IAudioClient(iface);
754 ULONG ref;
755 ref = InterlockedDecrement(&This->ref);
756 TRACE("(%p) Refcount now %u\n", This, ref);
757 if(!ref){
758 if(This->timer){
759 HANDLE event;
760 BOOL wait;
761 event = CreateEventW(NULL, TRUE, FALSE, NULL);
762 wait = !DeleteTimerQueueTimer(g_timer_q, This->timer, event);
763 wait = wait && GetLastError() == ERROR_IO_PENDING;
764 if(event && wait)
765 WaitForSingleObject(event, INFINITE);
766 CloseHandle(event);
768 AudioOutputUnitStop(This->unit);
769 AudioComponentInstanceDispose(This->unit);
770 if(This->converter)
771 AudioConverterDispose(This->converter);
772 if(This->session){
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);
788 return ref;
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");
797 break;
798 case WAVE_FORMAT_IEEE_FLOAT:
799 TRACE("WAVE_FORMAT_IEEE_FLOAT");
800 break;
801 case WAVE_FORMAT_EXTENSIBLE:
802 TRACE("WAVE_FORMAT_EXTENSIBLE");
803 break;
804 default:
805 TRACE("Unknown");
806 break;
808 TRACE(")\n");
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)
827 switch(channels){
828 case 0:
829 return 0;
830 case 1:
831 return KSAUDIO_SPEAKER_MONO;
832 case 2:
833 return KSAUDIO_SPEAKER_STEREO;
834 case 3:
835 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
836 case 4:
837 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
838 case 5:
839 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
840 case 6:
841 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
842 case 7:
843 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
844 case 8:
845 return KSAUDIO_SPEAKER_7POINT1_SURROUND; /* Vista deprecates 7POINT1 */
847 FIXME("Unknown speaker configuration: %u\n", channels);
848 return 0;
851 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
853 WAVEFORMATEX *ret;
854 size_t size;
856 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
857 size = sizeof(WAVEFORMATEXTENSIBLE);
858 else
859 size = sizeof(WAVEFORMATEX);
861 ret = CoTaskMemAlloc(size);
862 if(!ret)
863 return NULL;
865 memcpy(ret, fmt, size);
867 ret->cbSize = size - sizeof(WAVEFORMATEX);
869 return ret;
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;
898 }else
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;
907 desc->mReserved = 0;
909 return S_OK;
912 static void session_init_vols(AudioSession *session, UINT channels)
914 if(session->channel_count < channels){
915 UINT i;
917 if(session->channel_vols)
918 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
919 session->channel_vols, sizeof(float) * channels);
920 else
921 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
922 sizeof(float) * channels);
923 if(!session->channel_vols)
924 return;
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,
934 UINT num_channels)
936 AudioSession *ret;
938 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
939 if(!ret)
940 return NULL;
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;
957 return ret;
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);
969 if(!*out)
970 return E_OUTOFMEMORY;
972 return S_OK;
975 *out = NULL;
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);
980 *out = session;
981 break;
985 if(!*out){
986 *out = create_session(sessionguid, device, channels);
987 if(!*out)
988 return E_OUTOFMEMORY;
991 return S_OK;
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);
1002 }else
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);
1014 else
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);
1028 if(This->playing){
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);
1038 }else
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;
1044 }else
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);
1052 return noErr;
1055 static UINT buf_ptr_diff(UINT left, UINT right, UINT bufsize)
1057 if(left <= right)
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);
1069 if(!*nframes){
1070 data->mBuffers[0].mData = NULL;
1071 data->mBuffers[0].mDataByteSize = 0;
1072 data->mBuffers[0].mNumberChannels = This->fmt->nChannels;
1073 return noErr;
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;
1094 }else
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;
1101 if(packets)
1102 *packets = NULL;
1104 return noErr;
1107 static void capture_resample(ACImpl *This)
1109 UINT32 resamp_period_frames = MulDiv(This->period_frames, This->dev_desc.mSampleRate, This->fmt->nSamplesPerSec);
1110 OSStatus sc;
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);
1133 if(sc != noErr){
1134 WARN("AudioConverterFillComplexBuffer failed: %lx\n", sc);
1135 break;
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;
1149 }else
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;
1167 OSStatus sc;
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;
1186 }else
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);
1190 if(sc != noErr){
1191 OSSpinLockUnlock(&This->lock);
1192 return sc;
1195 if(This->playing){
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);
1212 return noErr;
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)
1229 OSStatus sc;
1230 HRESULT hr;
1232 if(dataflow == eCapture){
1233 AudioStreamBasicDescription desc;
1234 UInt32 size;
1235 Float64 rate;
1237 hr = ca_get_audiodesc(&desc, fmt);
1238 if(FAILED(hr))
1239 return hr;
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);
1247 if(sc != noErr){
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;
1254 *dev_desc = desc;
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));
1260 if(sc != noErr){
1261 WARN("Couldn't set unit format: %lx\n", sc);
1262 return osstatus_to_hresult(sc);
1265 sc = AudioConverterNew(dev_desc, &desc, converter);
1266 if(sc != noErr){
1267 WARN("Couldn't create audio converter: %lx\n", sc);
1268 return osstatus_to_hresult(sc);
1270 }else{
1271 hr = ca_get_audiodesc(dev_desc, fmt);
1272 if(FAILED(hr))
1273 return hr;
1275 dump_adesc("final", dev_desc);
1276 sc = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat,
1277 kAudioUnitScope_Input, 0, dev_desc, sizeof(*dev_desc));
1278 if(sc != noErr){
1279 WARN("Couldn't set format: %lx\n", sc);
1280 return osstatus_to_hresult(sc);
1284 return S_OK;
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);
1293 HRESULT hr;
1294 OSStatus sc;
1295 int i;
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));
1300 if(!fmt)
1301 return E_POINTER;
1303 dump_fmt(fmt);
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;
1324 }else{
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;
1331 if(!period)
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;
1342 }else{
1343 if( duration < 8 * period)
1344 duration = 8 * period; /* may grow above 2s */
1348 OSSpinLockLock(&This->lock);
1350 if(This->initted){
1351 OSSpinLockUnlock(&This->lock);
1352 return AUDCLNT_E_ALREADY_INITIALIZED;
1355 This->fmt = clone_format(fmt);
1356 if(!This->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);
1369 if(FAILED(hr)){
1370 CoTaskMemFree(This->fmt);
1371 This->fmt = NULL;
1372 OSSpinLockUnlock(&This->lock);
1373 return hr;
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));
1385 if(sc != noErr){
1386 WARN("Couldn't set callback: %lx\n", sc);
1387 AudioConverterDispose(This->converter);
1388 This->converter = NULL;
1389 CoTaskMemFree(This->fmt);
1390 This->fmt = NULL;
1391 OSSpinLockUnlock(&This->lock);
1392 return osstatus_to_hresult(sc);
1394 }else{
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));
1403 if(sc != noErr){
1404 WARN("Couldn't set callback: %lx\n", sc);
1405 CoTaskMemFree(This->fmt);
1406 This->fmt = NULL;
1407 OSSpinLockUnlock(&This->lock);
1408 return osstatus_to_hresult(sc);
1412 sc = AudioUnitInitialize(This->unit);
1413 if(sc != noErr){
1414 WARN("Couldn't initialize: %lx\n", sc);
1415 if(This->converter){
1416 AudioConverterDispose(This->converter);
1417 This->converter = NULL;
1419 CoTaskMemFree(This->fmt);
1420 This->fmt = NULL;
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);
1428 if(sc != noErr){
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);
1435 This->fmt = NULL;
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));
1449 if(!This->vols){
1450 CoTaskMemFree(This->fmt);
1451 This->fmt = NULL;
1452 OSSpinLockUnlock(&This->lock);
1453 return E_OUTOFMEMORY;
1456 for(i = 0; i < fmt->nChannels; ++i)
1457 This->vols[i] = 1.f;
1459 This->share = mode;
1460 This->flags = flags;
1462 EnterCriticalSection(&g_sessions_lock);
1464 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
1465 &This->session);
1466 if(FAILED(hr)){
1467 LeaveCriticalSection(&g_sessions_lock);
1468 CoTaskMemFree(This->fmt);
1469 This->fmt = NULL;
1470 HeapFree(GetProcessHeap(), 0, This->vols);
1471 This->vols = NULL;
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);
1486 return S_OK;
1489 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1490 UINT32 *frames)
1492 ACImpl *This = impl_from_IAudioClient(iface);
1494 TRACE("(%p)->(%p)\n", This, frames);
1496 if(!frames)
1497 return E_POINTER;
1499 OSSpinLockLock(&This->lock);
1501 if(!This->initted){
1502 OSSpinLockUnlock(&This->lock);
1503 return AUDCLNT_E_NOT_INITIALIZED;
1506 *frames = This->bufsize_frames;
1508 OSSpinLockUnlock(&This->lock);
1510 return S_OK;
1513 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
1515 AudioObjectPropertyAddress addr;
1516 AudioStreamID *ids;
1517 UInt32 size;
1518 OSStatus sc;
1519 int nstreams, i;
1521 addr.mScope = This->scope;
1522 addr.mElement = 0;
1523 addr.mSelector = kAudioDevicePropertyStreams;
1525 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
1526 &size);
1527 if(sc != noErr){
1528 WARN("Unable to get size for _Streams property: %lx\n", sc);
1529 return osstatus_to_hresult(sc);
1532 ids = HeapAlloc(GetProcessHeap(), 0, size);
1533 if(!ids)
1534 return E_OUTOFMEMORY;
1536 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
1537 if(sc != noErr){
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);
1544 *max = 0;
1546 addr.mSelector = kAudioStreamPropertyLatency;
1547 for(i = 0; i < nstreams; ++i){
1548 UInt32 latency;
1550 size = sizeof(latency);
1551 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1552 &size, &latency);
1553 if(sc != noErr){
1554 WARN("Unable to get _Latency property: %lx\n", sc);
1555 continue;
1558 if(latency > *max)
1559 *max = latency;
1562 HeapFree(GetProcessHeap(), 0, ids);
1564 return S_OK;
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;
1573 OSStatus sc;
1574 HRESULT hr;
1576 TRACE("(%p)->(%p)\n", This, out);
1578 if(!out)
1579 return E_POINTER;
1581 OSSpinLockLock(&This->lock);
1583 if(!This->initted){
1584 OSSpinLockUnlock(&This->lock);
1585 return AUDCLNT_E_NOT_INITIALIZED;
1588 addr.mScope = This->scope;
1589 addr.mSelector = kAudioDevicePropertyLatency;
1590 addr.mElement = 0;
1592 size = sizeof(latency);
1593 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1594 &size, &latency);
1595 if(sc != noErr){
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);
1602 if(FAILED(hr)){
1603 OSSpinLockUnlock(&This->lock);
1604 return hr;
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);
1615 return S_OK;
1618 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1619 UINT32 *numpad)
1621 if(!This->initted)
1622 return AUDCLNT_E_NOT_INITIALIZED;
1624 if(This->dataflow == eCapture)
1625 capture_resample(This);
1627 *numpad = This->held_frames;
1629 return S_OK;
1632 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1633 UINT32 *numpad)
1635 ACImpl *This = impl_from_IAudioClient(iface);
1636 HRESULT hr;
1638 TRACE("(%p)->(%p)\n", This, numpad);
1640 if(!numpad)
1641 return E_POINTER;
1643 OSSpinLockLock(&This->lock);
1645 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1647 OSSpinLockUnlock(&This->lock);
1649 return hr;
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;
1661 HRESULT hr;
1663 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1665 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1666 return E_POINTER;
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;
1675 dump_fmt(pwfx);
1677 if(outpwfx){
1678 *outpwfx = NULL;
1679 if(mode != AUDCLNT_SHAREMODE_SHARED)
1680 outpwfx = NULL;
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)
1689 goto unsupported;
1690 if(mode == AUDCLNT_SHAREMODE_EXCLUSIVE){
1691 if(fmtex->dwChannelMask == 0 ||
1692 fmtex->dwChannelMask & SPEAKER_RESERVED)
1693 goto unsupported;
1697 if(pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample / 8 ||
1698 pwfx->nAvgBytesPerSec != pwfx->nBlockAlign * pwfx->nSamplesPerSec)
1699 goto unsupported;
1701 if(pwfx->nChannels == 0)
1702 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1704 unit = get_audiounit(This->dataflow, This->adevid);
1706 converter = NULL;
1707 hr = ca_setup_audiounit(This->dataflow, unit, pwfx, &dev_desc, &converter);
1708 AudioComponentInstanceDispose(unit);
1709 if(FAILED(hr))
1710 goto unsupported;
1712 if(converter)
1713 AudioConverterDispose(converter);
1715 return S_OK;
1717 unsupported:
1718 if(outpwfx){
1719 hr = IAudioClient_GetMixFormat(&This->IAudioClient_iface, outpwfx);
1720 if(FAILED(hr))
1721 return hr;
1722 return S_FALSE;
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;
1733 OSStatus sc;
1734 UInt32 size;
1735 Float64 rate;
1736 AudioBufferList *buffers;
1737 AudioObjectPropertyAddress addr;
1738 int i;
1740 TRACE("(%p)->(%p)\n", This, pwfx);
1742 if(!pwfx)
1743 return E_POINTER;
1744 *pwfx = NULL;
1746 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1747 if(!fmt)
1748 return E_OUTOFMEMORY;
1750 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1752 addr.mScope = This->scope;
1753 addr.mElement = 0;
1754 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1756 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1757 if(sc != noErr){
1758 CoTaskMemFree(fmt);
1759 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1760 return osstatus_to_hresult(sc);
1763 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1764 if(!buffers){
1765 CoTaskMemFree(fmt);
1766 return E_OUTOFMEMORY;
1769 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1770 &size, buffers);
1771 if(sc != noErr){
1772 CoTaskMemFree(fmt);
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);
1789 if(sc != noErr){
1790 CoTaskMemFree(fmt);
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;
1808 dump_fmt(*pwfx);
1810 return S_OK;
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)
1821 return E_POINTER;
1823 if(defperiod)
1824 *defperiod = DefaultPeriod;
1825 if(minperiod)
1826 *minperiod = MinimumPeriod;
1828 return S_OK;
1831 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1833 ACImpl *This = user;
1835 if(This->event)
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);
1847 if(!This->initted){
1848 OSSpinLockUnlock(&This->lock);
1849 return AUDCLNT_E_NOT_INITIALIZED;
1852 if(This->playing){
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)){
1865 This->timer = NULL;
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);
1875 return S_OK;
1878 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1880 ACImpl *This = impl_from_IAudioClient(iface);
1882 TRACE("(%p)\n", This);
1884 OSSpinLockLock(&This->lock);
1886 if(!This->initted){
1887 OSSpinLockUnlock(&This->lock);
1888 return AUDCLNT_E_NOT_INITIALIZED;
1891 if(!This->playing){
1892 OSSpinLockUnlock(&This->lock);
1893 return S_FALSE;
1896 This->playing = FALSE;
1898 OSSpinLockUnlock(&This->lock);
1900 return S_OK;
1903 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1905 ACImpl *This = impl_from_IAudioClient(iface);
1907 TRACE("(%p)\n", This);
1909 OSSpinLockLock(&This->lock);
1911 if(!This->initted){
1912 OSSpinLockUnlock(&This->lock);
1913 return AUDCLNT_E_NOT_INITIALIZED;
1916 if(This->playing){
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;
1928 }else{
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);
1940 return S_OK;
1943 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1944 HANDLE event)
1946 ACImpl *This = impl_from_IAudioClient(iface);
1948 TRACE("(%p)->(%p)\n", This, event);
1950 if(!event)
1951 return E_INVALIDARG;
1953 OSSpinLockLock(&This->lock);
1955 if(!This->initted){
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;
1965 if (This->event){
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);
1975 return S_OK;
1978 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1979 void **ppv)
1981 ACImpl *This = impl_from_IAudioClient(iface);
1983 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1985 if(!ppv)
1986 return E_POINTER;
1987 *ppv = NULL;
1989 OSSpinLockLock(&This->lock);
1991 if(!This->initted){
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;
2023 }else
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;
2034 }else
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;
2045 }else
2046 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
2048 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
2051 if(*ppv){
2052 OSSpinLockUnlock(&This->lock);
2053 return S_OK;
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,
2065 AudioClient_AddRef,
2066 AudioClient_Release,
2067 AudioClient_Initialize,
2068 AudioClient_GetBufferSize,
2069 AudioClient_GetStreamLatency,
2070 AudioClient_GetCurrentPadding,
2071 AudioClient_IsFormatSupported,
2072 AudioClient_GetMixFormat,
2073 AudioClient_GetDevicePeriod,
2074 AudioClient_Start,
2075 AudioClient_Stop,
2076 AudioClient_Reset,
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);
2087 if(!ppv)
2088 return E_POINTER;
2089 *ppv = NULL;
2091 if(IsEqualIID(riid, &IID_IUnknown) ||
2092 IsEqualIID(riid, &IID_IAudioRenderClient))
2093 *ppv = iface;
2094 else if(IsEqualIID(riid, &IID_IMarshal))
2095 return IUnknown_QueryInterface(This->pUnkFTMarshal, riid, ppv);
2097 if(*ppv){
2098 IUnknown_AddRef((IUnknown*)*ppv);
2099 return S_OK;
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);
2122 UINT32 pad;
2123 HRESULT hr;
2125 TRACE("(%p)->(%u, %p)\n", This, frames, data);
2127 if(!data)
2128 return E_POINTER;
2129 *data = NULL;
2131 OSSpinLockLock(&This->lock);
2133 if(This->getbuf_last){
2134 OSSpinLockUnlock(&This->lock);
2135 return AUDCLNT_E_OUT_OF_ORDER;
2138 if(!frames){
2139 OSSpinLockUnlock(&This->lock);
2140 return S_OK;
2143 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
2144 if(FAILED(hr)){
2145 OSSpinLockUnlock(&This->lock);
2146 return hr;
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;
2166 }else{
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);
2175 return S_OK;
2178 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
2179 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
2181 ACImpl *This = impl_from_IAudioRenderClient(iface);
2182 BYTE *buffer;
2184 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
2186 OSSpinLockLock(&This->lock);
2188 if(!frames){
2189 This->getbuf_last = 0;
2190 OSSpinLockUnlock(&This->lock);
2191 return S_OK;
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;
2206 else
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);
2227 return S_OK;
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);
2244 if(!ppv)
2245 return E_POINTER;
2246 *ppv = NULL;
2248 if(IsEqualIID(riid, &IID_IUnknown) ||
2249 IsEqualIID(riid, &IID_IAudioCaptureClient))
2250 *ppv = iface;
2251 else if(IsEqualIID(riid, &IID_IMarshal))
2252 return IUnknown_QueryInterface(This->pUnkFTMarshal, riid, ppv);
2254 if(*ppv){
2255 IUnknown_AddRef((IUnknown*)*ppv);
2256 return S_OK;
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,
2277 UINT64 *qpcpos)
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,
2283 devpos, qpcpos);
2285 if(!data || !frames || !flags)
2286 return E_POINTER;
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){
2298 *frames = 0;
2299 OSSpinLockUnlock(&This->lock);
2300 return AUDCLNT_S_BUFFER_EMPTY;
2303 *flags = 0;
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);
2313 }else
2314 *data = This->local_buffer + This->lcl_offs_frames * This->fmt->nBlockAlign;
2316 This->getbuf_last = *frames = This->period_frames;
2318 if(devpos)
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);
2329 return S_OK;
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);
2341 if(!done){
2342 This->getbuf_last = 0;
2343 OSSpinLockUnlock(&This->lock);
2344 return S_OK;
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);
2365 return S_OK;
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);
2375 if(!frames)
2376 return E_POINTER;
2378 OSSpinLockLock(&This->lock);
2380 capture_resample(This);
2382 if(This->held_frames >= This->period_frames)
2383 *frames = This->period_frames;
2384 else
2385 *frames = 0;
2387 OSSpinLockUnlock(&This->lock);
2389 return S_OK;
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);
2409 if(!ppv)
2410 return E_POINTER;
2411 *ppv = NULL;
2413 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2414 *ppv = iface;
2415 else if(IsEqualIID(riid, &IID_IAudioClock2))
2416 *ppv = &This->IAudioClock2_iface;
2417 if(*ppv){
2418 IUnknown_AddRef((IUnknown*)*ppv);
2419 return S_OK;
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;
2446 else
2447 *freq = This->fmt->nSamplesPerSec;
2449 return S_OK;
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;
2460 if(qpctime){
2461 LARGE_INTEGER stamp, freq;
2462 QueryPerformanceCounter(&stamp);
2463 QueryPerformanceFrequency(&freq);
2464 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2467 return S_OK;
2470 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2471 UINT64 *qpctime)
2473 ACImpl *This = impl_from_IAudioClock(iface);
2474 HRESULT hr;
2476 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2478 if(!pos)
2479 return E_POINTER;
2481 OSSpinLockLock(&This->lock);
2483 hr = AudioClock_GetPosition_nolock(This, pos, qpctime);
2485 OSSpinLockUnlock(&This->lock);
2487 return hr;
2490 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2491 DWORD *chars)
2493 ACImpl *This = impl_from_IAudioClock(iface);
2495 TRACE("(%p)->(%p)\n", This, chars);
2497 if(!chars)
2498 return E_POINTER;
2500 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2502 return S_OK;
2505 static const IAudioClockVtbl AudioClock_Vtbl =
2507 AudioClock_QueryInterface,
2508 AudioClock_AddRef,
2509 AudioClock_Release,
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);
2541 return E_NOTIMPL;
2544 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2546 AudioClock2_QueryInterface,
2547 AudioClock2_AddRef,
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));
2558 if(!ret)
2559 return NULL;
2561 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2562 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2563 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2565 ret->ref = 1;
2567 ret->client = client;
2568 if(client){
2569 ret->session = client->session;
2570 AudioClient_AddRef(&client->IAudioClient_iface);
2573 return ret;
2576 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2577 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2579 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2581 if(!ppv)
2582 return E_POINTER;
2583 *ppv = NULL;
2585 if(IsEqualIID(riid, &IID_IUnknown) ||
2586 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2587 IsEqualIID(riid, &IID_IAudioSessionControl2))
2588 *ppv = iface;
2589 if(*ppv){
2590 IUnknown_AddRef((IUnknown*)*ppv);
2591 return S_OK;
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);
2601 ULONG ref;
2602 ref = InterlockedIncrement(&This->ref);
2603 TRACE("(%p) Refcount now %u\n", This, ref);
2604 return ref;
2607 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2609 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2610 ULONG ref;
2611 ref = InterlockedDecrement(&This->ref);
2612 TRACE("(%p) Refcount now %u\n", This, ref);
2613 if(!ref){
2614 if(This->client){
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);
2622 return ref;
2625 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2626 AudioSessionState *state)
2628 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2629 ACImpl *client;
2631 TRACE("(%p)->(%p)\n", This, state);
2633 if(!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);
2641 return S_OK;
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);
2650 return S_OK;
2652 OSSpinLockUnlock(&client->lock);
2655 LeaveCriticalSection(&g_sessions_lock);
2657 *state = AudioSessionStateInactive;
2659 return S_OK;
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);
2669 return E_NOTIMPL;
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));
2679 return E_NOTIMPL;
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);
2689 return E_NOTIMPL;
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));
2699 return E_NOTIMPL;
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);
2709 return E_NOTIMPL;
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));
2720 return E_NOTIMPL;
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);
2730 return S_OK;
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);
2740 return S_OK;
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);
2750 return E_NOTIMPL;
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);
2760 return E_NOTIMPL;
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);
2770 if(!pid)
2771 return E_POINTER;
2773 *pid = GetCurrentProcessId();
2775 return S_OK;
2778 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2779 IAudioSessionControl2 *iface)
2781 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2783 TRACE("(%p)\n", This);
2785 return S_FALSE;
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);
2795 return S_OK;
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)
2822 Float32 level;
2823 OSStatus sc;
2825 if(This->session->mute)
2826 level = 0.;
2827 else{
2828 if(index == (UINT32)-1){
2829 UINT32 i;
2830 level = 1.;
2831 for(i = 0; i < This->fmt->nChannels; ++i){
2832 Float32 tmp;
2833 tmp = This->session->master_vol *
2834 This->session->channel_vols[i] * This->vols[i];
2835 level = tmp < level ? tmp : level;
2837 }else
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);
2844 if(sc != noErr)
2845 WARN("Couldn't set volume: %lx\n", sc);
2847 return S_OK;
2850 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2852 HRESULT ret = S_OK;
2853 ACImpl *client;
2855 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2856 HRESULT hr;
2857 hr = ca_setvol(client, index);
2858 if(FAILED(hr))
2859 ret = hr;
2862 return ret;
2865 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2866 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2868 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2870 if(!ppv)
2871 return E_POINTER;
2872 *ppv = NULL;
2874 if(IsEqualIID(riid, &IID_IUnknown) ||
2875 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2876 *ppv = iface;
2877 if(*ppv){
2878 IUnknown_AddRef((IUnknown*)*ppv);
2879 return S_OK;
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;
2903 HRESULT ret;
2905 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2907 if(level < 0.f || level > 1.f)
2908 return E_INVALIDARG;
2910 if(context)
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);
2921 return ret;
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);
2932 if(!level)
2933 return NULL_PTR_ERR;
2935 *level = session->master_vol;
2937 return S_OK;
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);
2948 if(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);
2959 return S_OK;
2962 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2963 BOOL *mute)
2965 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2966 AudioSession *session = This->session;
2968 TRACE("(%p)->(%p)\n", session, mute);
2970 if(!mute)
2971 return NULL_PTR_ERR;
2973 *mute = session->mute;
2975 return S_OK;
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);
2994 if(!ppv)
2995 return E_POINTER;
2996 *ppv = NULL;
2998 if(IsEqualIID(riid, &IID_IUnknown) ||
2999 IsEqualIID(riid, &IID_IAudioStreamVolume))
3000 *ppv = iface;
3001 if(*ppv){
3002 IUnknown_AddRef((IUnknown*)*ppv);
3003 return S_OK;
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);
3029 if(!out)
3030 return E_POINTER;
3032 *out = This->fmt->nChannels;
3034 return S_OK;
3037 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
3038 IAudioStreamVolume *iface, UINT32 index, float level)
3040 ACImpl *This = impl_from_IAudioStreamVolume(iface);
3041 HRESULT ret;
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);
3060 return ret;
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);
3070 if(!level)
3071 return E_POINTER;
3073 if(index >= This->fmt->nChannels)
3074 return E_INVALIDARG;
3076 *level = This->vols[index];
3078 return S_OK;
3081 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
3082 IAudioStreamVolume *iface, UINT32 count, const float *levels)
3084 ACImpl *This = impl_from_IAudioStreamVolume(iface);
3085 int i;
3086 HRESULT ret;
3088 TRACE("(%p)->(%d, %p)\n", This, count, levels);
3090 if(!levels)
3091 return E_POINTER;
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);
3105 return ret;
3108 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
3109 IAudioStreamVolume *iface, UINT32 count, float *levels)
3111 ACImpl *This = impl_from_IAudioStreamVolume(iface);
3112 int i;
3114 TRACE("(%p)->(%d, %p)\n", This, count, levels);
3116 if(!levels)
3117 return E_POINTER;
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);
3129 return S_OK;
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);
3149 if(!ppv)
3150 return E_POINTER;
3151 *ppv = NULL;
3153 if(IsEqualIID(riid, &IID_IUnknown) ||
3154 IsEqualIID(riid, &IID_IChannelAudioVolume))
3155 *ppv = iface;
3156 if(*ppv){
3157 IUnknown_AddRef((IUnknown*)*ppv);
3158 return S_OK;
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);
3185 if(!out)
3186 return NULL_PTR_ERR;
3188 *out = session->channel_count;
3190 return S_OK;
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;
3199 HRESULT ret;
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;
3210 if(context)
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);
3222 return ret;
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);
3233 if(!level)
3234 return NULL_PTR_ERR;
3236 if(index >= session->channel_count)
3237 return E_INVALIDARG;
3239 *level = session->channel_vols[index];
3241 return S_OK;
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;
3250 int i;
3251 HRESULT ret;
3253 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
3254 wine_dbgstr_guid(context));
3256 if(!levels)
3257 return NULL_PTR_ERR;
3259 if(count != session->channel_count)
3260 return E_INVALIDARG;
3262 if(context)
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);
3274 return ret;
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;
3282 int i;
3284 TRACE("(%p)->(%d, %p)\n", session, count, levels);
3286 if(!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];
3295 return S_OK;
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);
3315 if(!ppv)
3316 return E_POINTER;
3317 *ppv = NULL;
3319 if(IsEqualIID(riid, &IID_IUnknown) ||
3320 IsEqualIID(riid, &IID_IAudioSessionManager) ||
3321 IsEqualIID(riid, &IID_IAudioSessionManager2))
3322 *ppv = iface;
3323 if(*ppv){
3324 IUnknown_AddRef((IUnknown*)*ppv);
3325 return S_OK;
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);
3335 ULONG ref;
3336 ref = InterlockedIncrement(&This->ref);
3337 TRACE("(%p) Refcount now %u\n", This, ref);
3338 return ref;
3341 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
3343 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3344 ULONG ref;
3345 ref = InterlockedDecrement(&This->ref);
3346 TRACE("(%p) Refcount now %u\n", This, ref);
3347 if(!ref)
3348 HeapFree(GetProcessHeap(), 0, This);
3349 return ref;
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;
3359 HRESULT hr;
3361 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3362 flags, out);
3364 hr = get_audio_session(session_guid, This->device, 0, &session);
3365 if(FAILED(hr))
3366 return hr;
3368 wrapper = AudioSessionWrapper_Create(NULL);
3369 if(!wrapper)
3370 return E_OUTOFMEMORY;
3372 wrapper->session = session;
3374 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3376 return S_OK;
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;
3386 HRESULT hr;
3388 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3389 flags, out);
3391 hr = get_audio_session(session_guid, This->device, 0, &session);
3392 if(FAILED(hr))
3393 return hr;
3395 wrapper = AudioSessionWrapper_Create(NULL);
3396 if(!wrapper)
3397 return E_OUTOFMEMORY;
3399 wrapper->session = session;
3401 *out = &wrapper->ISimpleAudioVolume_iface;
3403 return S_OK;
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);
3411 return E_NOTIMPL;
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);
3419 return E_NOTIMPL;
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);
3427 return E_NOTIMPL;
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);
3436 return E_NOTIMPL;
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);
3445 return E_NOTIMPL;
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)
3465 SessionMgr *This;
3467 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3468 if(!This)
3469 return E_OUTOFMEMORY;
3471 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3472 This->device = device;
3473 This->ref = 1;
3475 *out = &This->IAudioSessionManager2_iface;
3477 return S_OK;