2 * Copyright 2011 Andrew Eikum for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "wine/list.h"
34 #include "mmdeviceapi.h"
40 #include "endpointvolume.h"
41 #include "audioclient.h"
42 #include "audiopolicy.h"
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
55 #include <libkern/OSAtomic.h>
56 #include <CoreAudio/CoreAudio.h>
57 #include <AudioToolbox/AudioQueue.h>
58 #include <AudioToolbox/AudioFormat.h>
60 WINE_DEFAULT_DEBUG_CHANNEL(coreaudio);
62 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
64 #define CAPTURE_BUFFERS 5
66 static const REFERENCE_TIME DefaultPeriod = 200000;
67 static const REFERENCE_TIME MinimumPeriod = 100000;
69 typedef struct _QueuedBufInfo {
70 Float64 start_sampletime;
76 typedef struct _AQBuffer {
77 AudioQueueBufferRef buf;
83 typedef struct ACImpl ACImpl;
85 typedef struct _AudioSession {
96 CRITICAL_SECTION lock;
101 typedef struct _AudioSessionWrapper {
102 IAudioSessionControl2 IAudioSessionControl2_iface;
103 IChannelAudioVolume IChannelAudioVolume_iface;
104 ISimpleAudioVolume ISimpleAudioVolume_iface;
109 AudioSession *session;
110 } AudioSessionWrapper;
113 IAudioClient IAudioClient_iface;
114 IAudioRenderClient IAudioRenderClient_iface;
115 IAudioCaptureClient IAudioCaptureClient_iface;
116 IAudioClock IAudioClock_iface;
117 IAudioClock2 IAudioClock2_iface;
118 IAudioStreamVolume IAudioStreamVolume_iface;
128 AUDCLNT_SHAREMODE share;
132 AudioDeviceID adevid;
133 AudioQueueRef aqueue;
134 AudioObjectPropertyScope scope;
136 UINT32 period_ms, bufsize_frames, inbuf_frames;
137 UINT64 last_time, written_frames;
138 AudioQueueBufferRef public_buffer;
142 Float64 highest_sampletime, next_sampletime;
144 AudioSession *session;
145 AudioSessionWrapper *session_wrapper;
149 struct list avail_buffers;
150 struct list queued_buffers; /* either in avail, queued or public_buffer */
151 struct list queued_bufinfos;
162 static const IAudioClientVtbl AudioClient_Vtbl;
163 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
164 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
165 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
166 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
167 static const IAudioClockVtbl AudioClock_Vtbl;
168 static const IAudioClock2Vtbl AudioClock2_Vtbl;
169 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
170 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
171 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl;
173 typedef struct _SessionMgr {
174 IAudioSessionManager2 IAudioSessionManager2_iface;
181 static const WCHAR drv_keyW[] = {'S','o','f','t','w','a','r','e','\\',
182 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
183 'w','i','n','e','c','o','r','e','a','u','d','i','o','.','d','r','v',0};
184 static const WCHAR drv_key_devicesW[] = {'S','o','f','t','w','a','r','e','\\',
185 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
186 'w','i','n','e','c','o','r','e','a','u','d','i','o','.','d','r','v','\\','d','e','v','i','c','e','s',0};
187 static const WCHAR guidW[] = {'g','u','i','d',0};
189 static HANDLE g_timer_q;
191 static CRITICAL_SECTION g_sessions_lock;
192 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug =
194 0, 0, &g_sessions_lock,
195 { &g_sessions_lock_debug.ProcessLocksList, &g_sessions_lock_debug.ProcessLocksList },
196 0, 0, { (DWORD_PTR)(__FILE__ ": g_sessions_lock") }
198 static CRITICAL_SECTION g_sessions_lock = { &g_sessions_lock_debug, -1, 0, 0, 0, 0 };
199 static struct list g_sessions = LIST_INIT(g_sessions);
201 static HRESULT AudioCaptureClient_GetNextPacket(ACImpl *This, UINT32 *frames);
202 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
203 static HRESULT ca_setvol(ACImpl *This, UINT32 index);
205 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
207 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
210 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
212 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
215 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
217 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
220 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
222 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
225 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
227 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
230 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
232 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
235 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
237 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
240 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
242 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
245 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
247 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
250 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
252 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
255 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
259 case DLL_PROCESS_ATTACH:
260 g_timer_q = CreateTimerQueue();
265 case DLL_PROCESS_DETACH:
266 DeleteCriticalSection(&g_sessions_lock);
272 /* From <dlls/mmdevapi/mmdevapi.h> */
273 enum DriverPriority {
274 Priority_Unavailable = 0,
280 int WINAPI AUDDRV_GetPriority(void)
282 return Priority_Neutral;
285 static HRESULT osstatus_to_hresult(OSStatus sc)
288 case kAudioFormatUnsupportedDataFormatError:
289 case kAudioFormatUnknownFormatError:
290 case kAudioDeviceUnsupportedFormatError:
291 return AUDCLNT_E_UNSUPPORTED_FORMAT;
292 case kAudioHardwareBadDeviceError:
293 return AUDCLNT_E_DEVICE_INVALIDATED;
298 static void set_device_guid(EDataFlow flow, HKEY drv_key, const WCHAR *key_name,
306 lr = RegCreateKeyExW(HKEY_CURRENT_USER, drv_key_devicesW, 0, NULL, 0, KEY_WRITE,
307 NULL, &drv_key, NULL);
308 if(lr != ERROR_SUCCESS){
309 ERR("RegCreateKeyEx(drv_key) failed: %u\n", lr);
315 lr = RegCreateKeyExW(drv_key, key_name, 0, NULL, 0, KEY_WRITE,
317 if(lr != ERROR_SUCCESS){
318 ERR("RegCreateKeyEx(%s) failed: %u\n", wine_dbgstr_w(key_name), lr);
322 lr = RegSetValueExW(key, guidW, 0, REG_BINARY, (BYTE*)guid,
324 if(lr != ERROR_SUCCESS)
325 ERR("RegSetValueEx(%s\\guid) failed: %u\n", wine_dbgstr_w(key_name), lr);
330 RegCloseKey(drv_key);
333 static void get_device_guid(EDataFlow flow, AudioDeviceID device, GUID *guid)
335 HKEY key = NULL, dev_key;
336 DWORD type, size = sizeof(*guid);
339 static const WCHAR key_fmt[] = {'%','u',0};
347 sprintfW(key_name + 2, key_fmt, device);
349 if(RegOpenKeyExW(HKEY_CURRENT_USER, drv_key_devicesW, 0, KEY_WRITE|KEY_READ, &key) == ERROR_SUCCESS){
350 if(RegOpenKeyExW(key, key_name, 0, KEY_READ, &dev_key) == ERROR_SUCCESS){
351 if(RegQueryValueExW(dev_key, guidW, 0, &type,
352 (BYTE*)guid, &size) == ERROR_SUCCESS){
353 if(type == REG_BINARY){
354 RegCloseKey(dev_key);
358 ERR("Invalid type for device %s GUID: %u; ignoring and overwriting\n",
359 wine_dbgstr_w(key_name), type);
361 RegCloseKey(dev_key);
367 set_device_guid(flow, key, key_name, guid);
373 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids,
374 GUID **guids, UINT *num, UINT *def_index)
376 UInt32 devsize, size;
377 AudioDeviceID *devices;
378 AudioDeviceID default_id;
379 AudioObjectPropertyAddress addr;
383 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
385 addr.mScope = kAudioObjectPropertyScopeGlobal;
386 addr.mElement = kAudioObjectPropertyElementMaster;
388 addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
389 else if(flow == eCapture)
390 addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
394 size = sizeof(default_id);
395 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
396 NULL, &size, &default_id);
398 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc);
402 addr.mSelector = kAudioHardwarePropertyDevices;
403 sc = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0,
406 WARN("Getting _Devices property size failed: %lx\n", sc);
407 return osstatus_to_hresult(sc);
410 devices = HeapAlloc(GetProcessHeap(), 0, devsize);
412 return E_OUTOFMEMORY;
414 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
417 WARN("Getting _Devices property failed: %lx\n", sc);
418 HeapFree(GetProcessHeap(), 0, devices);
419 return osstatus_to_hresult(sc);
422 ndevices = devsize / sizeof(AudioDeviceID);
424 *ids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(WCHAR *));
426 HeapFree(GetProcessHeap(), 0, devices);
427 return E_OUTOFMEMORY;
430 *guids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(GUID));
432 HeapFree(GetProcessHeap(), 0, *ids);
433 HeapFree(GetProcessHeap(), 0, devices);
434 return E_OUTOFMEMORY;
438 *def_index = (UINT)-1;
439 for(i = 0; i < ndevices; ++i){
440 AudioBufferList *buffers;
445 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
447 addr.mScope = kAudioDevicePropertyScopeOutput;
449 addr.mScope = kAudioDevicePropertyScopeInput;
451 sc = AudioObjectGetPropertyDataSize(devices[i], &addr, 0, NULL, &size);
453 WARN("Unable to get _StreamConfiguration property size for "
454 "device %lu: %lx\n", devices[i], sc);
458 buffers = HeapAlloc(GetProcessHeap(), 0, size);
460 HeapFree(GetProcessHeap(), 0, devices);
461 for(j = 0; j < *num; ++j)
462 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
463 HeapFree(GetProcessHeap(), 0, *guids);
464 HeapFree(GetProcessHeap(), 0, *ids);
465 return E_OUTOFMEMORY;
468 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
471 WARN("Unable to get _StreamConfiguration property for "
472 "device %lu: %lx\n", devices[i], sc);
473 HeapFree(GetProcessHeap(), 0, buffers);
477 /* check that there's at least one channel in this device before
478 * we claim it as usable */
479 for(j = 0; j < buffers->mNumberBuffers; ++j)
480 if(buffers->mBuffers[j].mNumberChannels > 0)
482 if(j >= buffers->mNumberBuffers){
483 HeapFree(GetProcessHeap(), 0, buffers);
487 HeapFree(GetProcessHeap(), 0, buffers);
490 addr.mSelector = kAudioObjectPropertyName;
491 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
494 WARN("Unable to get _Name property for device %lu: %lx\n",
499 len = CFStringGetLength(name) + 1;
500 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
503 HeapFree(GetProcessHeap(), 0, devices);
504 for(j = 0; j < *num; ++j)
505 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
506 HeapFree(GetProcessHeap(), 0, *ids);
507 HeapFree(GetProcessHeap(), 0, *guids);
508 return E_OUTOFMEMORY;
510 CFStringGetCharacters(name, CFRangeMake(0, len - 1), (UniChar*)(*ids)[*num]);
511 ((*ids)[*num])[len - 1] = 0;
514 get_device_guid(flow, devices[i], &(*guids)[*num]);
516 if(*def_index == (UINT)-1 && devices[i] == default_id)
519 TRACE("device %u: id %s key %u%s\n", *num, debugstr_w((*ids)[*num]),
520 (unsigned int)devices[i], (*def_index == *num) ? " (default)" : "");
525 if(*def_index == (UINT)-1)
528 HeapFree(GetProcessHeap(), 0, devices);
533 static BOOL get_deviceid_by_guid(GUID *guid, AudioDeviceID *id, EDataFlow *flow)
540 if(RegOpenKeyExW(HKEY_CURRENT_USER, drv_key_devicesW, 0, KEY_READ, &devices_key) != ERROR_SUCCESS){
541 ERR("No devices in registry?\n");
550 key_name_size = sizeof(key_name);
551 if(RegEnumKeyExW(devices_key, i, key_name, &key_name_size, NULL,
552 NULL, NULL, NULL) != ERROR_SUCCESS)
555 if(RegOpenKeyExW(devices_key, key_name, 0, KEY_READ, &key) != ERROR_SUCCESS){
556 WARN("Couldn't open key: %s\n", wine_dbgstr_w(key_name));
560 size = sizeof(reg_guid);
561 if(RegQueryValueExW(key, guidW, 0, &type,
562 (BYTE*)®_guid, &size) == ERROR_SUCCESS){
563 if(IsEqualGUID(®_guid, guid)){
565 RegCloseKey(devices_key);
567 TRACE("Found matching device key: %s\n", wine_dbgstr_w(key_name));
569 if(key_name[0] == '0')
571 else if(key_name[0] == '1')
574 ERR("Unknown device type: %c\n", key_name[0]);
578 *id = strtoulW(key_name + 2, NULL, 10);
589 RegCloseKey(devices_key);
591 WARN("No matching device in registry for GUID %s\n", debugstr_guid(guid));
596 HRESULT WINAPI AUDDRV_GetAudioEndpoint(GUID *guid, IMMDevice *dev, IAudioClient **out)
599 AudioDeviceID adevid;
602 TRACE("%s %p %p\n", debugstr_guid(guid), dev, out);
604 if(!get_deviceid_by_guid(guid, &adevid, &dataflow))
605 return AUDCLNT_E_DEVICE_INVALIDATED;
607 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
609 return E_OUTOFMEMORY;
611 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
612 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
613 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
614 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
615 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
616 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
618 This->dataflow = dataflow;
620 if(dataflow == eRender)
621 This->scope = kAudioDevicePropertyScopeOutput;
622 else if(dataflow == eCapture)
623 This->scope = kAudioDevicePropertyScopeInput;
625 HeapFree(GetProcessHeap(), 0, This);
632 IMMDevice_AddRef(This->parent);
634 list_init(&This->avail_buffers);
635 list_init(&This->queued_buffers);
636 list_init(&This->queued_bufinfos);
638 This->adevid = adevid;
640 *out = &This->IAudioClient_iface;
641 IAudioClient_AddRef(&This->IAudioClient_iface);
646 /* current position from start of stream */
647 #define BUFPOS_ABSOLUTE 1
648 /* current position from start of this buffer */
649 #define BUFPOS_RELATIVE 2
651 static UINT64 get_current_aqbuffer_position(ACImpl *This, int mode)
654 QueuedBufInfo *bufinfo;
657 head = list_head(&This->queued_bufinfos);
659 TRACE("No buffers queued\n");
660 if(mode == BUFPOS_ABSOLUTE)
661 return This->written_frames;
664 bufinfo = LIST_ENTRY(head, QueuedBufInfo, entry);
666 if(This->playing == StatePlaying){
667 AudioTimeStamp tstamp;
670 /* AudioQueueGetCurrentTime() is brain damaged. The returned
671 * mSampleTime member jumps backwards seemingly at random, so
672 * we record the highest sampletime and use that during these
675 * It also behaves poorly when the queue is paused, jumping
676 * forwards during the pause and backwards again after resuming.
677 * So we record the sampletime when the queue is paused and use
679 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &tstamp, NULL);
681 if(sc != kAudioQueueErr_InvalidRunState)
682 WARN("Unable to get current time: %lx\n", sc);
686 if(!(tstamp.mFlags & kAudioTimeStampSampleTimeValid)){
687 FIXME("SampleTime not valid: %lx\n", tstamp.mFlags);
691 if(tstamp.mSampleTime > This->highest_sampletime)
692 This->highest_sampletime = tstamp.mSampleTime;
695 while(This->highest_sampletime > bufinfo->start_sampletime + bufinfo->len_frames){
696 This->inbuf_frames -= bufinfo->len_frames;
697 list_remove(&bufinfo->entry);
698 HeapFree(GetProcessHeap(), 0, bufinfo);
700 head = list_head(&This->queued_bufinfos);
702 TRACE("No buffers queued\n");
703 if(mode == BUFPOS_ABSOLUTE)
704 return This->written_frames;
707 bufinfo = LIST_ENTRY(head, QueuedBufInfo, entry);
710 if(This->highest_sampletime < bufinfo->start_sampletime)
713 ret = This->highest_sampletime - bufinfo->start_sampletime;
715 if(mode == BUFPOS_ABSOLUTE){
716 ret = This->written_frames - (bufinfo->len_frames - ret);
717 while((head = list_next(&This->queued_bufinfos, &bufinfo->entry))){
718 bufinfo = LIST_ENTRY(head, QueuedBufInfo, entry);
719 ret -= bufinfo->len_frames;
723 TRACE("%llu frames (%s)\n", ret,
724 mode == BUFPOS_ABSOLUTE ? "absolute" : "relative");
729 static void avail_update(ACImpl *This)
731 AQBuffer *buf, *next;
733 LIST_FOR_EACH_ENTRY_SAFE(buf, next, &This->queued_buffers, AQBuffer, entry){
736 if(This->dataflow == eCapture)
737 This->inbuf_frames += buf->buf->mAudioDataByteSize / This->fmt->nBlockAlign;
738 list_remove(&buf->entry);
739 list_add_tail(&This->avail_buffers, &buf->entry);
743 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
744 REFIID riid, void **ppv)
746 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
751 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
754 IUnknown_AddRef((IUnknown*)*ppv);
757 WARN("Unknown interface %s\n", debugstr_guid(riid));
758 return E_NOINTERFACE;
761 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
763 ACImpl *This = impl_from_IAudioClient(iface);
765 ref = InterlockedIncrement(&This->ref);
766 TRACE("(%p) Refcount now %u\n", This, ref);
770 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
772 ACImpl *This = impl_from_IAudioClient(iface);
774 ref = InterlockedDecrement(&This->ref);
775 TRACE("(%p) Refcount now %u\n", This, ref);
778 AQBuffer *buf, *next;
779 QueuedBufInfo *bufinfo, *bufinfo2;
781 if(This->public_buffer){
782 buf = This->public_buffer->mUserData;
783 list_add_tail(&This->avail_buffers, &buf->entry);
786 IAudioClient_Stop(iface);
787 AudioQueueStop(This->aqueue, 1);
789 /* Stopped synchronously, all buffers returned. */
790 list_move_tail(&This->avail_buffers, &This->queued_buffers);
791 LIST_FOR_EACH_ENTRY_SAFE(buf, next, &This->avail_buffers, AQBuffer, entry){
792 AudioQueueFreeBuffer(This->aqueue, buf->buf);
793 HeapFree(GetProcessHeap(), 0, buf);
796 LIST_FOR_EACH_ENTRY_SAFE(bufinfo, bufinfo2, &This->queued_bufinfos,
797 QueuedBufInfo, entry)
798 HeapFree(GetProcessHeap(), 0, bufinfo);
800 AudioQueueDispose(This->aqueue, 1);
803 EnterCriticalSection(&g_sessions_lock);
804 list_remove(&This->entry);
805 LeaveCriticalSection(&g_sessions_lock);
807 HeapFree(GetProcessHeap(), 0, This->vols);
808 CoTaskMemFree(This->fmt);
809 IMMDevice_Release(This->parent);
810 HeapFree(GetProcessHeap(), 0, This);
815 static void dump_fmt(const WAVEFORMATEX *fmt)
817 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
818 switch(fmt->wFormatTag){
819 case WAVE_FORMAT_PCM:
820 TRACE("WAVE_FORMAT_PCM");
822 case WAVE_FORMAT_IEEE_FLOAT:
823 TRACE("WAVE_FORMAT_IEEE_FLOAT");
825 case WAVE_FORMAT_EXTENSIBLE:
826 TRACE("WAVE_FORMAT_EXTENSIBLE");
834 TRACE("nChannels: %u\n", fmt->nChannels);
835 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
836 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
837 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
838 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
839 TRACE("cbSize: %u\n", fmt->cbSize);
841 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
842 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
843 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
844 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
845 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
849 static DWORD get_channel_mask(unsigned int channels)
855 return KSAUDIO_SPEAKER_MONO;
857 return KSAUDIO_SPEAKER_STEREO;
859 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
861 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
863 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
865 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
867 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
869 return KSAUDIO_SPEAKER_7POINT1_SURROUND; /* Vista deprecates 7POINT1 */
871 FIXME("Unknown speaker configuration: %u\n", channels);
875 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
880 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
881 size = sizeof(WAVEFORMATEXTENSIBLE);
883 size = sizeof(WAVEFORMATEX);
885 ret = CoTaskMemAlloc(size);
889 memcpy(ret, fmt, size);
891 ret->cbSize = size - sizeof(WAVEFORMATEX);
896 static HRESULT ca_get_audiodesc(AudioStreamBasicDescription *desc,
897 const WAVEFORMATEX *fmt)
899 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
901 desc->mFormatFlags = 0;
903 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
904 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
905 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
906 desc->mFormatID = kAudioFormatLinearPCM;
907 if(fmt->wBitsPerSample > 8)
908 desc->mFormatFlags = kAudioFormatFlagIsSignedInteger;
909 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
910 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
911 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
912 desc->mFormatID = kAudioFormatLinearPCM;
913 desc->mFormatFlags = kAudioFormatFlagIsFloat;
914 }else if(fmt->wFormatTag == WAVE_FORMAT_MULAW ||
915 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
916 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_MULAW))){
917 desc->mFormatID = kAudioFormatULaw;
918 }else if(fmt->wFormatTag == WAVE_FORMAT_ALAW ||
919 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
920 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_ALAW))){
921 desc->mFormatID = kAudioFormatALaw;
923 return AUDCLNT_E_UNSUPPORTED_FORMAT;
925 desc->mSampleRate = fmt->nSamplesPerSec;
926 desc->mBytesPerPacket = fmt->nBlockAlign;
927 desc->mFramesPerPacket = 1;
928 desc->mBytesPerFrame = fmt->nBlockAlign;
929 desc->mChannelsPerFrame = fmt->nChannels;
930 desc->mBitsPerChannel = fmt->wBitsPerSample;
936 /* We can't use debug printing or {Enter,Leave}CriticalSection from
937 * OSX callback threads. We may use OSSpinLock.
938 * OSSpinLock is not a recursive lock, so don't call
939 * synchronized functions while holding the lock. */
940 static void ca_out_buffer_cb(void *user, AudioQueueRef aqueue,
941 AudioQueueBufferRef buffer)
943 AQBuffer *buf = buffer->mUserData;
948 static void ca_in_buffer_cb(void *user, AudioQueueRef aqueue,
949 AudioQueueBufferRef buffer, const AudioTimeStamp *start,
950 UInt32 ndesc, const AudioStreamPacketDescription *descs)
952 AQBuffer *buf = buffer->mUserData;
955 /* let's update inbuf_frames synchronously without OSAddAtomic */
958 static HRESULT ca_setup_aqueue(AudioDeviceID did, EDataFlow flow,
959 const WAVEFORMATEX *fmt, void *user, AudioQueueRef *aqueue)
961 AudioStreamBasicDescription desc;
962 AudioObjectPropertyAddress addr;
968 addr.mScope = kAudioObjectPropertyScopeGlobal;
970 addr.mSelector = kAudioDevicePropertyDeviceUID;
973 sc = AudioObjectGetPropertyData(did, &addr, 0, NULL, &size, &uid);
975 WARN("Unable to get _DeviceUID property: %lx\n", sc);
976 return osstatus_to_hresult(sc);
979 hr = ca_get_audiodesc(&desc, fmt);
986 sc = AudioQueueNewOutput(&desc, ca_out_buffer_cb, user, NULL, NULL, 0,
988 else if(flow == eCapture)
989 sc = AudioQueueNewInput(&desc, ca_in_buffer_cb, user, NULL, NULL, 0,
996 WARN("Unable to create AudioQueue: %lx\n", sc);
998 return osstatus_to_hresult(sc);
1001 sc = AudioQueueSetProperty(*aqueue, kAudioQueueProperty_CurrentDevice,
1004 WARN("Unable to change AQueue device: %lx\n", sc);
1006 return osstatus_to_hresult(sc);
1014 static void session_init_vols(AudioSession *session, UINT channels)
1016 if(session->channel_count < channels){
1019 if(session->channel_vols)
1020 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
1021 session->channel_vols, sizeof(float) * channels);
1023 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
1024 sizeof(float) * channels);
1025 if(!session->channel_vols)
1028 for(i = session->channel_count; i < channels; ++i)
1029 session->channel_vols[i] = 1.f;
1031 session->channel_count = channels;
1035 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
1040 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
1044 memcpy(&ret->guid, guid, sizeof(GUID));
1046 ret->device = device;
1048 list_init(&ret->clients);
1050 list_add_head(&g_sessions, &ret->entry);
1052 InitializeCriticalSection(&ret->lock);
1053 ret->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AudioSession.lock");
1055 session_init_vols(ret, num_channels);
1057 ret->master_vol = 1.f;
1062 /* if channels == 0, then this will return or create a session with
1063 * matching dataflow and GUID. otherwise, channels must also match */
1064 static HRESULT get_audio_session(const GUID *sessionguid,
1065 IMMDevice *device, UINT channels, AudioSession **out)
1067 AudioSession *session;
1069 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
1070 *out = create_session(&GUID_NULL, device, channels);
1072 return E_OUTOFMEMORY;
1078 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
1079 if(session->device == device &&
1080 IsEqualGUID(sessionguid, &session->guid)){
1081 session_init_vols(session, channels);
1088 *out = create_session(sessionguid, device, channels);
1090 return E_OUTOFMEMORY;
1096 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
1097 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
1098 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
1099 const GUID *sessionguid)
1101 ACImpl *This = impl_from_IAudioClient(iface);
1106 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
1107 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
1114 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1115 return AUDCLNT_E_NOT_INITIALIZED;
1117 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
1118 AUDCLNT_STREAMFLAGS_LOOPBACK |
1119 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
1120 AUDCLNT_STREAMFLAGS_NOPERSIST |
1121 AUDCLNT_STREAMFLAGS_RATEADJUST |
1122 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
1123 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
1124 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
1125 TRACE("Unknown flags: %08x\n", flags);
1126 return E_INVALIDARG;
1129 if(mode == AUDCLNT_SHAREMODE_SHARED){
1130 period = DefaultPeriod;
1131 if( duration < 3 * period)
1132 duration = 3 * period;
1134 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
1135 if(((WAVEFORMATEXTENSIBLE*)fmt)->dwChannelMask == 0 ||
1136 ((WAVEFORMATEXTENSIBLE*)fmt)->dwChannelMask & SPEAKER_RESERVED)
1137 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1141 period = DefaultPeriod; /* not minimum */
1142 if(period < MinimumPeriod || period > 5000000)
1143 return AUDCLNT_E_INVALID_DEVICE_PERIOD;
1144 if(duration > 20000000) /* the smaller the period, the lower this limit */
1145 return AUDCLNT_E_BUFFER_SIZE_ERROR;
1146 if(flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK){
1147 if(duration != period)
1148 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL;
1149 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
1150 return AUDCLNT_E_DEVICE_IN_USE;
1152 if( duration < 8 * period)
1153 duration = 8 * period; /* may grow above 2s */
1157 OSSpinLockLock(&This->lock);
1160 OSSpinLockUnlock(&This->lock);
1161 return AUDCLNT_E_ALREADY_INITIALIZED;
1164 hr = ca_setup_aqueue(This->adevid, This->dataflow, fmt, This, &This->aqueue);
1166 OSSpinLockUnlock(&This->lock);
1170 This->fmt = clone_format(fmt);
1172 AudioQueueDispose(This->aqueue, 1);
1173 This->aqueue = NULL;
1174 OSSpinLockUnlock(&This->lock);
1175 return E_OUTOFMEMORY;
1178 This->period_ms = period / 10000;
1180 This->bufsize_frames = MulDiv(duration, fmt->nSamplesPerSec, 10000000);
1182 if(This->dataflow == eCapture){
1184 UInt32 bsize = ceil((This->bufsize_frames / (double)CAPTURE_BUFFERS) *
1185 This->fmt->nBlockAlign);
1186 for(i = 0; i < CAPTURE_BUFFERS; ++i){
1189 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1191 AudioQueueDispose(This->aqueue, 1);
1192 This->aqueue = NULL;
1193 CoTaskMemFree(This->fmt);
1195 OSSpinLockUnlock(&This->lock);
1196 return E_OUTOFMEMORY;
1199 sc = AudioQueueAllocateBuffer(This->aqueue, bsize, &buf->buf);
1201 AudioQueueDispose(This->aqueue, 1);
1202 This->aqueue = NULL;
1203 CoTaskMemFree(This->fmt);
1205 OSSpinLockUnlock(&This->lock);
1206 WARN("Couldn't allocate buffer: %lx\n", sc);
1207 return osstatus_to_hresult(sc);
1210 buf->buf->mUserData = buf;
1212 sc = AudioQueueEnqueueBuffer(This->aqueue, buf->buf, 0, NULL);
1214 ERR("Couldn't enqueue buffer: %lx\n", sc);
1217 list_add_tail(&This->queued_buffers, &buf->entry);
1221 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1223 AudioQueueDispose(This->aqueue, 1);
1224 This->aqueue = NULL;
1225 CoTaskMemFree(This->fmt);
1227 OSSpinLockUnlock(&This->lock);
1228 return E_OUTOFMEMORY;
1231 for(i = 0; i < fmt->nChannels; ++i)
1232 This->vols[i] = 1.f;
1235 This->flags = flags;
1237 EnterCriticalSection(&g_sessions_lock);
1239 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
1242 LeaveCriticalSection(&g_sessions_lock);
1243 AudioQueueDispose(This->aqueue, 1);
1244 This->aqueue = NULL;
1245 CoTaskMemFree(This->fmt);
1247 HeapFree(GetProcessHeap(), 0, This->vols);
1249 OSSpinLockUnlock(&This->lock);
1250 return E_INVALIDARG;
1253 list_add_tail(&This->session->clients, &This->entry);
1255 LeaveCriticalSection(&g_sessions_lock);
1257 ca_setvol(This, -1);
1259 OSSpinLockUnlock(&This->lock);
1264 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1267 ACImpl *This = impl_from_IAudioClient(iface);
1269 TRACE("(%p)->(%p)\n", This, frames);
1274 OSSpinLockLock(&This->lock);
1277 OSSpinLockUnlock(&This->lock);
1278 return AUDCLNT_E_NOT_INITIALIZED;
1281 *frames = This->bufsize_frames;
1283 OSSpinLockUnlock(&This->lock);
1288 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
1290 AudioObjectPropertyAddress addr;
1296 addr.mScope = This->scope;
1298 addr.mSelector = kAudioDevicePropertyStreams;
1300 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
1303 WARN("Unable to get size for _Streams property: %lx\n", sc);
1304 return osstatus_to_hresult(sc);
1307 ids = HeapAlloc(GetProcessHeap(), 0, size);
1309 return E_OUTOFMEMORY;
1311 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
1313 WARN("Unable to get _Streams property: %lx\n", sc);
1314 HeapFree(GetProcessHeap(), 0, ids);
1315 return osstatus_to_hresult(sc);
1318 nstreams = size / sizeof(AudioStreamID);
1321 addr.mSelector = kAudioStreamPropertyLatency;
1322 for(i = 0; i < nstreams; ++i){
1325 size = sizeof(latency);
1326 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1329 WARN("Unable to get _Latency property: %lx\n", sc);
1337 HeapFree(GetProcessHeap(), 0, ids);
1342 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1343 REFERENCE_TIME *out)
1345 ACImpl *This = impl_from_IAudioClient(iface);
1346 UInt32 latency, stream_latency, size;
1347 AudioObjectPropertyAddress addr;
1351 TRACE("(%p)->(%p)\n", This, out);
1356 OSSpinLockLock(&This->lock);
1359 OSSpinLockUnlock(&This->lock);
1360 return AUDCLNT_E_NOT_INITIALIZED;
1363 addr.mScope = This->scope;
1364 addr.mSelector = kAudioDevicePropertyLatency;
1367 size = sizeof(latency);
1368 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1371 WARN("Couldn't get _Latency property: %lx\n", sc);
1372 OSSpinLockUnlock(&This->lock);
1373 return osstatus_to_hresult(sc);
1376 hr = ca_get_max_stream_latency(This, &stream_latency);
1378 OSSpinLockUnlock(&This->lock);
1382 latency += stream_latency;
1383 /* pretend we process audio in Period chunks, so max latency includes
1384 * the period time */
1385 *out = MulDiv(latency, 10000000, This->fmt->nSamplesPerSec)
1386 + This->period_ms * 10000;
1388 OSSpinLockUnlock(&This->lock);
1393 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1397 return AUDCLNT_E_NOT_INITIALIZED;
1401 if(This->dataflow == eRender){
1403 bufpos = get_current_aqbuffer_position(This, BUFPOS_RELATIVE);
1404 *numpad = This->inbuf_frames - bufpos;
1406 *numpad = This->inbuf_frames;
1411 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1414 ACImpl *This = impl_from_IAudioClient(iface);
1417 TRACE("(%p)->(%p)\n", This, numpad);
1422 OSSpinLockLock(&This->lock);
1424 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1426 OSSpinLockUnlock(&This->lock);
1431 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1432 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1433 WAVEFORMATEX **outpwfx)
1435 ACImpl *This = impl_from_IAudioClient(iface);
1436 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
1437 AudioQueueRef aqueue;
1440 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1442 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1445 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1446 return E_INVALIDARG;
1448 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1449 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1450 return E_INVALIDARG;
1456 if(mode != AUDCLNT_SHAREMODE_SHARED)
1460 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
1461 if(pwfx->nAvgBytesPerSec == 0 ||
1462 pwfx->nBlockAlign == 0 ||
1463 fmtex->Samples.wValidBitsPerSample > pwfx->wBitsPerSample)
1464 return E_INVALIDARG;
1465 if(fmtex->Samples.wValidBitsPerSample < pwfx->wBitsPerSample)
1467 if(mode == AUDCLNT_SHAREMODE_EXCLUSIVE){
1468 if(fmtex->dwChannelMask == 0 ||
1469 fmtex->dwChannelMask & SPEAKER_RESERVED)
1474 if(pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample / 8 ||
1475 pwfx->nAvgBytesPerSec != pwfx->nBlockAlign * pwfx->nSamplesPerSec)
1478 if(pwfx->nChannels == 0)
1479 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1481 OSSpinLockLock(&This->lock);
1483 hr = ca_setup_aqueue(This->adevid, This->dataflow, pwfx, NULL, &aqueue);
1485 AudioQueueDispose(aqueue, 1);
1486 OSSpinLockUnlock(&This->lock);
1487 TRACE("returning %08x\n", S_OK);
1490 OSSpinLockUnlock(&This->lock);
1491 if(hr != AUDCLNT_E_UNSUPPORTED_FORMAT){
1492 TRACE("returning %08x\n", hr);
1498 hr = IAudioClient_GetMixFormat(&This->IAudioClient_iface, outpwfx);
1504 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1507 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1508 WAVEFORMATEX **pwfx)
1510 ACImpl *This = impl_from_IAudioClient(iface);
1511 WAVEFORMATEXTENSIBLE *fmt;
1515 AudioBufferList *buffers;
1516 AudioObjectPropertyAddress addr;
1519 TRACE("(%p)->(%p)\n", This, pwfx);
1525 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1527 return E_OUTOFMEMORY;
1529 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1531 addr.mScope = This->scope;
1533 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1535 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1538 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1539 return osstatus_to_hresult(sc);
1542 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1545 return E_OUTOFMEMORY;
1548 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1552 HeapFree(GetProcessHeap(), 0, buffers);
1553 WARN("Unable to get _StreamConfiguration property: %lx\n", sc);
1554 return osstatus_to_hresult(sc);
1557 fmt->Format.nChannels = 0;
1558 for(i = 0; i < buffers->mNumberBuffers; ++i)
1559 fmt->Format.nChannels += buffers->mBuffers[i].mNumberChannels;
1561 HeapFree(GetProcessHeap(), 0, buffers);
1563 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1565 addr.mSelector = kAudioDevicePropertyNominalSampleRate;
1566 size = sizeof(Float64);
1567 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, &rate);
1570 WARN("Unable to get _NominalSampleRate property: %lx\n", sc);
1571 return osstatus_to_hresult(sc);
1573 fmt->Format.nSamplesPerSec = rate;
1575 fmt->Format.wBitsPerSample = 32;
1576 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1578 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1579 fmt->Format.nChannels) / 8;
1580 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1581 fmt->Format.nBlockAlign;
1583 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1584 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1586 *pwfx = (WAVEFORMATEX*)fmt;
1592 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1593 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1595 ACImpl *This = impl_from_IAudioClient(iface);
1597 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1599 if(!defperiod && !minperiod)
1603 *defperiod = DefaultPeriod;
1605 *minperiod = MinimumPeriod;
1610 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1612 ACImpl *This = user;
1615 SetEvent(This->event);
1618 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1620 ACImpl *This = impl_from_IAudioClient(iface);
1623 TRACE("(%p)\n", This);
1625 OSSpinLockLock(&This->lock);
1628 OSSpinLockUnlock(&This->lock);
1629 return AUDCLNT_E_NOT_INITIALIZED;
1632 if(This->playing != StateStopped){
1633 OSSpinLockUnlock(&This->lock);
1634 return AUDCLNT_E_NOT_STOPPED;
1637 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1638 OSSpinLockUnlock(&This->lock);
1639 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1643 if(!CreateTimerQueueTimer(&This->timer, g_timer_q, ca_period_cb,
1644 This, 0, This->period_ms, WT_EXECUTEINTIMERTHREAD)){
1646 OSSpinLockUnlock(&This->lock);
1647 WARN("Unable to create timer: %u\n", GetLastError());
1648 return E_OUTOFMEMORY;
1651 if(This->dataflow == eCapture){
1652 UINT32 frames; /* enqueue packets */
1653 AudioCaptureClient_GetNextPacket(This, &frames);
1656 This->playing = StateInTransition;
1658 sc = AudioQueueStart(This->aqueue, NULL);
1660 OSSpinLockUnlock(&This->lock);
1661 WARN("Unable to start audio queue: %lx\n", sc);
1662 return osstatus_to_hresult(sc);
1665 This->playing = StatePlaying;
1667 OSSpinLockUnlock(&This->lock);
1672 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1674 ACImpl *This = impl_from_IAudioClient(iface);
1675 AudioTimeStamp tstamp;
1677 HANDLE event = NULL;
1680 TRACE("(%p)\n", This);
1682 OSSpinLockLock(&This->lock);
1685 OSSpinLockUnlock(&This->lock);
1686 return AUDCLNT_E_NOT_INITIALIZED;
1689 if(This->playing == StateStopped){
1690 OSSpinLockUnlock(&This->lock);
1694 if(This->playing == StateInTransition){
1695 OSSpinLockUnlock(&This->lock);
1700 event = CreateEventW(NULL, TRUE, FALSE, NULL);
1701 wait = !DeleteTimerQueueTimer(g_timer_q, This->timer, event);
1704 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1705 wait = wait && GetLastError() == ERROR_IO_PENDING;
1708 This->playing = StateInTransition;
1710 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &tstamp, NULL);
1712 if(tstamp.mFlags & kAudioTimeStampSampleTimeValid){
1713 if(tstamp.mSampleTime > This->highest_sampletime)
1714 This->highest_sampletime = tstamp.mSampleTime;
1716 WARN("Returned tstamp mSampleTime not valid: %lx\n", tstamp.mFlags);
1718 WARN("GetCurrentTime failed: %lx\n", sc);
1720 /* Mac OS bug? Our capture callback is no more called past AQStop */
1721 sc = AudioQueuePause(This->aqueue);
1723 OSSpinLockUnlock(&This->lock);
1724 WARN("Unable to pause audio queue: %lx\n", sc);
1725 return osstatus_to_hresult(sc);
1728 This->playing = StateStopped;
1730 OSSpinLockUnlock(&This->lock);
1733 WaitForSingleObject(event, INFINITE);
1739 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1741 ACImpl *This = impl_from_IAudioClient(iface);
1743 QueuedBufInfo *bufinfo, *bufinfo2;
1746 TRACE("(%p)\n", This);
1748 OSSpinLockLock(&This->lock);
1751 OSSpinLockUnlock(&This->lock);
1752 return AUDCLNT_E_NOT_INITIALIZED;
1755 if(This->playing != StateStopped){
1756 OSSpinLockUnlock(&This->lock);
1757 return AUDCLNT_E_NOT_STOPPED;
1760 if(This->getbuf_last){
1761 OSSpinLockUnlock(&This->lock);
1762 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1765 avail_update(This); /* going to skip over inbuf_frames */
1767 LIST_FOR_EACH_ENTRY_SAFE(bufinfo, bufinfo2, &This->queued_bufinfos,
1768 QueuedBufInfo, entry){
1769 list_remove(&bufinfo->entry);
1770 HeapFree(GetProcessHeap(), 0, bufinfo);
1773 sc = AudioQueueReset(This->aqueue);
1775 OSSpinLockUnlock(&This->lock);
1776 WARN("Unable to reset audio queue: %lx\n", sc);
1777 return osstatus_to_hresult(sc);
1780 /* AQReset is synchronous */
1781 list_move_tail(&This->avail_buffers, &This->queued_buffers);
1783 if(This->dataflow == eRender){
1784 This->written_frames = 0;
1786 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry)
1787 buf->buf->mAudioDataByteSize = 0;
1788 This->written_frames += This->inbuf_frames;
1790 This->inbuf_frames = 0;
1792 OSSpinLockUnlock(&This->lock);
1797 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1800 ACImpl *This = impl_from_IAudioClient(iface);
1802 TRACE("(%p)->(%p)\n", This, event);
1805 return E_INVALIDARG;
1807 OSSpinLockLock(&This->lock);
1810 OSSpinLockUnlock(&This->lock);
1811 return AUDCLNT_E_NOT_INITIALIZED;
1814 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1815 OSSpinLockUnlock(&This->lock);
1816 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1819 This->event = event;
1821 OSSpinLockUnlock(&This->lock);
1826 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1829 ACImpl *This = impl_from_IAudioClient(iface);
1831 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1837 OSSpinLockLock(&This->lock);
1840 OSSpinLockUnlock(&This->lock);
1841 return AUDCLNT_E_NOT_INITIALIZED;
1844 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1845 if(This->dataflow != eRender){
1846 OSSpinLockUnlock(&This->lock);
1847 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1849 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1850 *ppv = &This->IAudioRenderClient_iface;
1851 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1852 if(This->dataflow != eCapture){
1853 OSSpinLockUnlock(&This->lock);
1854 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1856 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1857 *ppv = &This->IAudioCaptureClient_iface;
1858 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1859 IAudioClock_AddRef(&This->IAudioClock_iface);
1860 *ppv = &This->IAudioClock_iface;
1861 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1862 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1863 *ppv = &This->IAudioStreamVolume_iface;
1864 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1865 if(!This->session_wrapper){
1866 This->session_wrapper = AudioSessionWrapper_Create(This);
1867 if(!This->session_wrapper){
1868 OSSpinLockUnlock(&This->lock);
1869 return E_OUTOFMEMORY;
1872 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1874 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1875 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1876 if(!This->session_wrapper){
1877 This->session_wrapper = AudioSessionWrapper_Create(This);
1878 if(!This->session_wrapper){
1879 OSSpinLockUnlock(&This->lock);
1880 return E_OUTOFMEMORY;
1883 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1885 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1886 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1887 if(!This->session_wrapper){
1888 This->session_wrapper = AudioSessionWrapper_Create(This);
1889 if(!This->session_wrapper){
1890 OSSpinLockUnlock(&This->lock);
1891 return E_OUTOFMEMORY;
1894 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1896 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1900 OSSpinLockUnlock(&This->lock);
1904 OSSpinLockUnlock(&This->lock);
1906 FIXME("stub %s\n", debugstr_guid(riid));
1907 return E_NOINTERFACE;
1910 static const IAudioClientVtbl AudioClient_Vtbl =
1912 AudioClient_QueryInterface,
1914 AudioClient_Release,
1915 AudioClient_Initialize,
1916 AudioClient_GetBufferSize,
1917 AudioClient_GetStreamLatency,
1918 AudioClient_GetCurrentPadding,
1919 AudioClient_IsFormatSupported,
1920 AudioClient_GetMixFormat,
1921 AudioClient_GetDevicePeriod,
1925 AudioClient_SetEventHandle,
1926 AudioClient_GetService
1929 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1930 IAudioRenderClient *iface, REFIID riid, void **ppv)
1932 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1938 if(IsEqualIID(riid, &IID_IUnknown) ||
1939 IsEqualIID(riid, &IID_IAudioRenderClient))
1942 IUnknown_AddRef((IUnknown*)*ppv);
1946 WARN("Unknown interface %s\n", debugstr_guid(riid));
1947 return E_NOINTERFACE;
1950 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1952 ACImpl *This = impl_from_IAudioRenderClient(iface);
1953 return AudioClient_AddRef(&This->IAudioClient_iface);
1956 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1958 ACImpl *This = impl_from_IAudioRenderClient(iface);
1959 return AudioClient_Release(&This->IAudioClient_iface);
1962 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1963 UINT32 frames, BYTE **data)
1965 ACImpl *This = impl_from_IAudioRenderClient(iface);
1971 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1977 OSSpinLockLock(&This->lock);
1979 if(This->getbuf_last){
1980 OSSpinLockUnlock(&This->lock);
1981 return AUDCLNT_E_OUT_OF_ORDER;
1985 OSSpinLockUnlock(&This->lock);
1989 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
1991 OSSpinLockUnlock(&This->lock);
1995 if(pad + frames > This->bufsize_frames){
1996 OSSpinLockUnlock(&This->lock);
1997 return AUDCLNT_E_BUFFER_TOO_LARGE;
2000 bytes = frames * This->fmt->nBlockAlign;
2001 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry){
2002 if(buf->buf->mAudioDataBytesCapacity >= bytes){
2003 This->public_buffer = buf->buf;
2004 list_remove(&buf->entry);
2009 if(&buf->entry == &This->avail_buffers){
2010 sc = AudioQueueAllocateBuffer(This->aqueue, bytes,
2011 &This->public_buffer);
2013 This->public_buffer = NULL;
2014 OSSpinLockUnlock(&This->lock);
2015 WARN("Unable to allocate buffer: %lx\n", sc);
2016 return E_OUTOFMEMORY;
2018 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
2020 AudioQueueFreeBuffer(This->aqueue, This->public_buffer);
2021 This->public_buffer = NULL;
2022 OSSpinLockUnlock(&This->lock);
2023 return E_OUTOFMEMORY;
2026 buf->buf = This->public_buffer;
2027 This->public_buffer->mUserData = buf;
2030 This->getbuf_last = frames;
2031 *data = This->public_buffer->mAudioData;
2033 OSSpinLockUnlock(&This->lock);
2038 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
2039 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
2041 ACImpl *This = impl_from_IAudioRenderClient(iface);
2043 AudioTimeStamp start_time, req_time = {0}, *passed_time = NULL;
2046 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
2048 OSSpinLockLock(&This->lock);
2051 This->getbuf_last = 0;
2052 if(This->public_buffer){
2053 buf = This->public_buffer->mUserData;
2054 list_add_head(&This->avail_buffers, &buf->entry);
2055 This->public_buffer = NULL;
2057 OSSpinLockUnlock(&This->lock);
2061 if(!This->getbuf_last){
2062 OSSpinLockUnlock(&This->lock);
2063 return AUDCLNT_E_OUT_OF_ORDER;
2066 if(frames > This->getbuf_last){
2067 OSSpinLockUnlock(&This->lock);
2068 return AUDCLNT_E_INVALID_SIZE;
2071 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
2072 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
2073 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
2074 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
2075 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
2076 This->fmt->wBitsPerSample == 8)
2077 memset(This->public_buffer->mAudioData, 128,
2078 frames * This->fmt->nBlockAlign);
2080 memset(This->public_buffer->mAudioData, 0,
2081 frames * This->fmt->nBlockAlign);
2084 This->public_buffer->mAudioDataByteSize = frames * This->fmt->nBlockAlign;
2086 buf = This->public_buffer->mUserData;
2089 if(list_empty(&This->queued_bufinfos)){
2090 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &req_time, NULL);
2092 passed_time = &req_time;
2094 TRACE("AudioQueueGetCurrentTime failed: %lx\n", sc);
2096 req_time.mSampleTime = This->next_sampletime;
2097 req_time.mFlags = kAudioTimeStampSampleTimeValid;
2098 passed_time = &req_time;
2101 sc = AudioQueueEnqueueBufferWithParameters(This->aqueue,
2102 This->public_buffer, 0, NULL, 0, 0, 0, NULL, passed_time,
2105 OSSpinLockUnlock(&This->lock);
2106 ERR("Unable to enqueue buffer: %lx\n", sc);
2107 return AUDCLNT_E_DEVICE_INVALIDATED;
2109 list_add_tail(&This->queued_buffers, &buf->entry);
2111 if(start_time.mFlags & kAudioTimeStampSampleTimeValid){
2112 QueuedBufInfo *bufinfo;
2114 bufinfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*bufinfo));
2115 bufinfo->start_sampletime = start_time.mSampleTime;
2116 bufinfo->start_pos = This->written_frames;
2117 bufinfo->len_frames = frames;
2119 list_add_tail(&This->queued_bufinfos, &bufinfo->entry);
2121 This->next_sampletime = start_time.mSampleTime + bufinfo->len_frames;
2123 WARN("Start time didn't contain valid SampleTime member\n");
2125 if(This->playing == StateStopped)
2126 AudioQueuePrime(This->aqueue, 0, NULL);
2128 This->public_buffer = NULL;
2129 This->getbuf_last = 0;
2130 This->written_frames += frames;
2131 This->inbuf_frames += frames;
2133 OSSpinLockUnlock(&This->lock);
2138 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
2139 AudioRenderClient_QueryInterface,
2140 AudioRenderClient_AddRef,
2141 AudioRenderClient_Release,
2142 AudioRenderClient_GetBuffer,
2143 AudioRenderClient_ReleaseBuffer
2146 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
2147 IAudioCaptureClient *iface, REFIID riid, void **ppv)
2149 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2155 if(IsEqualIID(riid, &IID_IUnknown) ||
2156 IsEqualIID(riid, &IID_IAudioCaptureClient))
2159 IUnknown_AddRef((IUnknown*)*ppv);
2163 WARN("Unknown interface %s\n", debugstr_guid(riid));
2164 return E_NOINTERFACE;
2167 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
2169 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2170 return IAudioClient_AddRef(&This->IAudioClient_iface);
2173 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
2175 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2176 return IAudioClient_Release(&This->IAudioClient_iface);
2179 static HRESULT AudioCaptureClient_GetNextPacket(ACImpl *This, UINT32 *frames)
2184 avail_update(This); /* once, not inside loop */
2187 if(!This->public_buffer){
2188 struct list *head = list_head(&This->avail_buffers);
2194 buf = LIST_ENTRY(head, AQBuffer, entry);
2195 This->public_buffer = buf->buf;
2196 list_remove(&buf->entry);
2198 buf = This->public_buffer->mUserData;
2199 *frames = This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
2203 WARN("empty packet\n");
2205 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer, 0, NULL);
2207 ERR("Unable to enqueue buffer: %lx\n", sc);
2208 /* Release will free This->public_buffer */
2209 return AUDCLNT_E_DEVICE_INVALIDATED;
2211 list_add_tail(&This->queued_buffers, &buf->entry);
2212 This->public_buffer = NULL;
2216 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
2217 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
2220 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2223 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
2226 if(!data || !frames || !flags)
2229 OSSpinLockLock(&This->lock);
2231 if(This->getbuf_last){
2232 OSSpinLockUnlock(&This->lock);
2233 return AUDCLNT_E_OUT_OF_ORDER;
2236 hr = AudioCaptureClient_GetNextPacket(This, frames);
2238 OSSpinLockUnlock(&This->lock);
2242 if((This->getbuf_last = *frames)){
2244 *data = This->public_buffer->mAudioData;
2247 *devpos = This->written_frames;
2248 if(qpcpos){ /* fixme: qpc of recording time */
2249 LARGE_INTEGER stamp, freq;
2250 QueryPerformanceCounter(&stamp);
2251 QueryPerformanceFrequency(&freq);
2252 *qpcpos = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2255 OSSpinLockUnlock(&This->lock);
2257 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
2260 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
2261 IAudioCaptureClient *iface, UINT32 done)
2263 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2267 TRACE("(%p)->(%u)\n", This, done);
2269 OSSpinLockLock(&This->lock);
2272 This->getbuf_last = 0;
2273 OSSpinLockUnlock(&This->lock);
2277 if(!This->getbuf_last){
2278 OSSpinLockUnlock(&This->lock);
2279 return AUDCLNT_E_OUT_OF_ORDER;
2282 if(This->getbuf_last != done){
2283 OSSpinLockUnlock(&This->lock);
2284 return AUDCLNT_E_INVALID_SIZE;
2287 This->written_frames += done;
2288 This->inbuf_frames -= done;
2289 This->getbuf_last = 0;
2291 buf = This->public_buffer->mUserData;
2293 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer, 0, NULL);
2295 OSSpinLockUnlock(&This->lock);
2296 /* fixme: can't zero public_buffer or we lose memory, but then
2297 * GetBuffer will see that packet again and again. */
2298 ERR("Unable to enqueue buffer: %lx\n", sc);
2299 return AUDCLNT_E_DEVICE_INVALIDATED;
2301 list_add_tail(&This->queued_buffers, &buf->entry);
2302 This->public_buffer = NULL;
2304 OSSpinLockUnlock(&This->lock);
2309 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2310 IAudioCaptureClient *iface, UINT32 *frames)
2312 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2315 TRACE("(%p)->(%p)\n", This, frames);
2320 OSSpinLockLock(&This->lock);
2322 hr = AudioCaptureClient_GetNextPacket(This, frames);
2324 OSSpinLockUnlock(&This->lock);
2329 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2331 AudioCaptureClient_QueryInterface,
2332 AudioCaptureClient_AddRef,
2333 AudioCaptureClient_Release,
2334 AudioCaptureClient_GetBuffer,
2335 AudioCaptureClient_ReleaseBuffer,
2336 AudioCaptureClient_GetNextPacketSize
2339 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2340 REFIID riid, void **ppv)
2342 ACImpl *This = impl_from_IAudioClock(iface);
2344 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2350 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2352 else if(IsEqualIID(riid, &IID_IAudioClock2))
2353 *ppv = &This->IAudioClock2_iface;
2355 IUnknown_AddRef((IUnknown*)*ppv);
2359 WARN("Unknown interface %s\n", debugstr_guid(riid));
2360 return E_NOINTERFACE;
2363 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2365 ACImpl *This = impl_from_IAudioClock(iface);
2366 return IAudioClient_AddRef(&This->IAudioClient_iface);
2369 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2371 ACImpl *This = impl_from_IAudioClock(iface);
2372 return IAudioClient_Release(&This->IAudioClient_iface);
2375 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2377 ACImpl *This = impl_from_IAudioClock(iface);
2379 TRACE("(%p)->(%p)\n", This, freq);
2381 *freq = This->fmt->nSamplesPerSec;
2386 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This,
2387 UINT64 *pos, UINT64 *qpctime)
2391 if(This->dataflow == eRender)
2392 *pos = get_current_aqbuffer_position(This, BUFPOS_ABSOLUTE);
2394 *pos = This->inbuf_frames + This->written_frames;
2397 LARGE_INTEGER stamp, freq;
2398 QueryPerformanceCounter(&stamp);
2399 QueryPerformanceFrequency(&freq);
2400 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2406 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2409 ACImpl *This = impl_from_IAudioClock(iface);
2412 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2417 OSSpinLockLock(&This->lock);
2419 hr = AudioClock_GetPosition_nolock(This, pos, qpctime);
2421 OSSpinLockUnlock(&This->lock);
2426 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2429 ACImpl *This = impl_from_IAudioClock(iface);
2431 TRACE("(%p)->(%p)\n", This, chars);
2436 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2441 static const IAudioClockVtbl AudioClock_Vtbl =
2443 AudioClock_QueryInterface,
2446 AudioClock_GetFrequency,
2447 AudioClock_GetPosition,
2448 AudioClock_GetCharacteristics
2451 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2452 REFIID riid, void **ppv)
2454 ACImpl *This = impl_from_IAudioClock2(iface);
2455 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2458 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2460 ACImpl *This = impl_from_IAudioClock2(iface);
2461 return IAudioClient_AddRef(&This->IAudioClient_iface);
2464 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2466 ACImpl *This = impl_from_IAudioClock2(iface);
2467 return IAudioClient_Release(&This->IAudioClient_iface);
2470 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2471 UINT64 *pos, UINT64 *qpctime)
2473 ACImpl *This = impl_from_IAudioClock2(iface);
2475 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2480 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2482 AudioClock2_QueryInterface,
2484 AudioClock2_Release,
2485 AudioClock2_GetDevicePosition
2488 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2490 AudioSessionWrapper *ret;
2492 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2493 sizeof(AudioSessionWrapper));
2497 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2498 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2499 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2503 ret->client = client;
2505 ret->session = client->session;
2506 AudioClient_AddRef(&client->IAudioClient_iface);
2512 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2513 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2515 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2521 if(IsEqualIID(riid, &IID_IUnknown) ||
2522 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2523 IsEqualIID(riid, &IID_IAudioSessionControl2))
2526 IUnknown_AddRef((IUnknown*)*ppv);
2530 WARN("Unknown interface %s\n", debugstr_guid(riid));
2531 return E_NOINTERFACE;
2534 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2536 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2538 ref = InterlockedIncrement(&This->ref);
2539 TRACE("(%p) Refcount now %u\n", This, ref);
2543 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2545 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2547 ref = InterlockedDecrement(&This->ref);
2548 TRACE("(%p) Refcount now %u\n", This, ref);
2551 OSSpinLockLock(&This->client->lock);
2552 This->client->session_wrapper = NULL;
2553 OSSpinLockUnlock(&This->client->lock);
2554 AudioClient_Release(&This->client->IAudioClient_iface);
2556 HeapFree(GetProcessHeap(), 0, This);
2561 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2562 AudioSessionState *state)
2564 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2567 TRACE("(%p)->(%p)\n", This, state);
2570 return NULL_PTR_ERR;
2572 EnterCriticalSection(&g_sessions_lock);
2574 if(list_empty(&This->session->clients)){
2575 *state = AudioSessionStateExpired;
2576 LeaveCriticalSection(&g_sessions_lock);
2580 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2581 OSSpinLockLock(&client->lock);
2582 if(client->playing == StatePlaying ||
2583 client->playing == StateInTransition){
2584 *state = AudioSessionStateActive;
2585 OSSpinLockUnlock(&client->lock);
2586 LeaveCriticalSection(&g_sessions_lock);
2589 OSSpinLockUnlock(&client->lock);
2592 LeaveCriticalSection(&g_sessions_lock);
2594 *state = AudioSessionStateInactive;
2599 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2600 IAudioSessionControl2 *iface, WCHAR **name)
2602 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2604 FIXME("(%p)->(%p) - stub\n", This, name);
2609 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2610 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2612 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2614 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2619 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2620 IAudioSessionControl2 *iface, WCHAR **path)
2622 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2624 FIXME("(%p)->(%p) - stub\n", This, path);
2629 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2630 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2632 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2634 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2639 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2640 IAudioSessionControl2 *iface, GUID *group)
2642 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2644 FIXME("(%p)->(%p) - stub\n", This, group);
2649 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2650 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2652 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2654 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2655 debugstr_guid(session));
2660 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2661 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2663 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2665 FIXME("(%p)->(%p) - stub\n", This, events);
2670 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2671 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2673 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2675 FIXME("(%p)->(%p) - stub\n", This, events);
2680 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2681 IAudioSessionControl2 *iface, WCHAR **id)
2683 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2685 FIXME("(%p)->(%p) - stub\n", This, id);
2690 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2691 IAudioSessionControl2 *iface, WCHAR **id)
2693 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2695 FIXME("(%p)->(%p) - stub\n", This, id);
2700 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2701 IAudioSessionControl2 *iface, DWORD *pid)
2703 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2705 TRACE("(%p)->(%p)\n", This, pid);
2710 *pid = GetCurrentProcessId();
2715 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2716 IAudioSessionControl2 *iface)
2718 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2720 TRACE("(%p)\n", This);
2725 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2726 IAudioSessionControl2 *iface, BOOL optout)
2728 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2730 TRACE("(%p)->(%d)\n", This, optout);
2735 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2737 AudioSessionControl_QueryInterface,
2738 AudioSessionControl_AddRef,
2739 AudioSessionControl_Release,
2740 AudioSessionControl_GetState,
2741 AudioSessionControl_GetDisplayName,
2742 AudioSessionControl_SetDisplayName,
2743 AudioSessionControl_GetIconPath,
2744 AudioSessionControl_SetIconPath,
2745 AudioSessionControl_GetGroupingParam,
2746 AudioSessionControl_SetGroupingParam,
2747 AudioSessionControl_RegisterAudioSessionNotification,
2748 AudioSessionControl_UnregisterAudioSessionNotification,
2749 AudioSessionControl_GetSessionIdentifier,
2750 AudioSessionControl_GetSessionInstanceIdentifier,
2751 AudioSessionControl_GetProcessId,
2752 AudioSessionControl_IsSystemSoundsSession,
2753 AudioSessionControl_SetDuckingPreference
2756 /* index == -1 means set all channels, otherwise sets only the given channel */
2757 static HRESULT ca_setvol(ACImpl *This, UINT32 index)
2762 if(index == (UINT32)-1){
2765 for(i = 0; i < This->fmt->nChannels; ++i){
2767 hr = ca_setvol(This, i);
2774 if(This->session->mute)
2777 level = This->session->master_vol *
2778 This->session->channel_vols[index] * This->vols[index];
2780 sc = AudioQueueSetParameter(This->aqueue, kAudioQueueParam_Volume, level);
2782 WARN("Setting _Volume property failed: %lx\n", sc);
2787 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2792 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2794 hr = ca_setvol(client, index);
2802 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2803 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2805 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2811 if(IsEqualIID(riid, &IID_IUnknown) ||
2812 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2815 IUnknown_AddRef((IUnknown*)*ppv);
2819 WARN("Unknown interface %s\n", debugstr_guid(riid));
2820 return E_NOINTERFACE;
2823 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2825 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2826 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2829 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2831 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2832 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2835 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2836 ISimpleAudioVolume *iface, float level, const GUID *context)
2838 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2839 AudioSession *session = This->session;
2842 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2844 if(level < 0.f || level > 1.f)
2845 return E_INVALIDARG;
2848 FIXME("Notifications not supported yet\n");
2850 EnterCriticalSection(&session->lock);
2852 session->master_vol = level;
2854 ret = ca_session_setvol(session, -1);
2856 LeaveCriticalSection(&session->lock);
2861 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2862 ISimpleAudioVolume *iface, float *level)
2864 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2865 AudioSession *session = This->session;
2867 TRACE("(%p)->(%p)\n", session, level);
2870 return NULL_PTR_ERR;
2872 *level = session->master_vol;
2877 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2878 BOOL mute, const GUID *context)
2880 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2881 AudioSession *session = This->session;
2883 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2886 FIXME("Notifications not supported yet\n");
2888 EnterCriticalSection(&session->lock);
2890 session->mute = mute;
2892 ca_session_setvol(session, -1);
2894 LeaveCriticalSection(&session->lock);
2899 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2902 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2903 AudioSession *session = This->session;
2905 TRACE("(%p)->(%p)\n", session, mute);
2908 return NULL_PTR_ERR;
2910 *mute = session->mute;
2915 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2917 SimpleAudioVolume_QueryInterface,
2918 SimpleAudioVolume_AddRef,
2919 SimpleAudioVolume_Release,
2920 SimpleAudioVolume_SetMasterVolume,
2921 SimpleAudioVolume_GetMasterVolume,
2922 SimpleAudioVolume_SetMute,
2923 SimpleAudioVolume_GetMute
2926 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2927 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2929 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2935 if(IsEqualIID(riid, &IID_IUnknown) ||
2936 IsEqualIID(riid, &IID_IAudioStreamVolume))
2939 IUnknown_AddRef((IUnknown*)*ppv);
2943 WARN("Unknown interface %s\n", debugstr_guid(riid));
2944 return E_NOINTERFACE;
2947 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2949 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2950 return IAudioClient_AddRef(&This->IAudioClient_iface);
2953 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2955 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2956 return IAudioClient_Release(&This->IAudioClient_iface);
2959 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2960 IAudioStreamVolume *iface, UINT32 *out)
2962 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2964 TRACE("(%p)->(%p)\n", This, out);
2969 *out = This->fmt->nChannels;
2974 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2975 IAudioStreamVolume *iface, UINT32 index, float level)
2977 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2980 TRACE("(%p)->(%d, %f)\n", This, index, level);
2982 if(level < 0.f || level > 1.f)
2983 return E_INVALIDARG;
2985 if(index >= This->fmt->nChannels)
2986 return E_INVALIDARG;
2988 OSSpinLockLock(&This->lock);
2990 This->vols[index] = level;
2992 WARN("AudioQueue doesn't support per-channel volume control\n");
2993 ret = ca_setvol(This, index);
2995 OSSpinLockUnlock(&This->lock);
3000 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
3001 IAudioStreamVolume *iface, UINT32 index, float *level)
3003 ACImpl *This = impl_from_IAudioStreamVolume(iface);
3005 TRACE("(%p)->(%d, %p)\n", This, index, level);
3010 if(index >= This->fmt->nChannels)
3011 return E_INVALIDARG;
3013 *level = This->vols[index];
3018 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
3019 IAudioStreamVolume *iface, UINT32 count, const float *levels)
3021 ACImpl *This = impl_from_IAudioStreamVolume(iface);
3025 TRACE("(%p)->(%d, %p)\n", This, count, levels);
3030 if(count != This->fmt->nChannels)
3031 return E_INVALIDARG;
3033 OSSpinLockLock(&This->lock);
3035 for(i = 0; i < count; ++i)
3036 This->vols[i] = levels[i];
3038 ret = ca_setvol(This, -1);
3040 OSSpinLockUnlock(&This->lock);
3045 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
3046 IAudioStreamVolume *iface, UINT32 count, float *levels)
3048 ACImpl *This = impl_from_IAudioStreamVolume(iface);
3051 TRACE("(%p)->(%d, %p)\n", This, count, levels);
3056 if(count != This->fmt->nChannels)
3057 return E_INVALIDARG;
3059 OSSpinLockLock(&This->lock);
3061 for(i = 0; i < count; ++i)
3062 levels[i] = This->vols[i];
3064 OSSpinLockUnlock(&This->lock);
3069 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
3071 AudioStreamVolume_QueryInterface,
3072 AudioStreamVolume_AddRef,
3073 AudioStreamVolume_Release,
3074 AudioStreamVolume_GetChannelCount,
3075 AudioStreamVolume_SetChannelVolume,
3076 AudioStreamVolume_GetChannelVolume,
3077 AudioStreamVolume_SetAllVolumes,
3078 AudioStreamVolume_GetAllVolumes
3081 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
3082 IChannelAudioVolume *iface, REFIID riid, void **ppv)
3084 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
3090 if(IsEqualIID(riid, &IID_IUnknown) ||
3091 IsEqualIID(riid, &IID_IChannelAudioVolume))
3094 IUnknown_AddRef((IUnknown*)*ppv);
3098 WARN("Unknown interface %s\n", debugstr_guid(riid));
3099 return E_NOINTERFACE;
3102 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
3104 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3105 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
3108 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
3110 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3111 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
3114 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
3115 IChannelAudioVolume *iface, UINT32 *out)
3117 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3118 AudioSession *session = This->session;
3120 TRACE("(%p)->(%p)\n", session, out);
3123 return NULL_PTR_ERR;
3125 *out = session->channel_count;
3130 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
3131 IChannelAudioVolume *iface, UINT32 index, float level,
3132 const GUID *context)
3134 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3135 AudioSession *session = This->session;
3138 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
3139 wine_dbgstr_guid(context));
3141 if(level < 0.f || level > 1.f)
3142 return E_INVALIDARG;
3144 if(index >= session->channel_count)
3145 return E_INVALIDARG;
3148 FIXME("Notifications not supported yet\n");
3150 EnterCriticalSection(&session->lock);
3152 session->channel_vols[index] = level;
3154 WARN("AudioQueue doesn't support per-channel volume control\n");
3155 ret = ca_session_setvol(session, index);
3157 LeaveCriticalSection(&session->lock);
3162 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
3163 IChannelAudioVolume *iface, UINT32 index, float *level)
3165 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3166 AudioSession *session = This->session;
3168 TRACE("(%p)->(%d, %p)\n", session, index, level);
3171 return NULL_PTR_ERR;
3173 if(index >= session->channel_count)
3174 return E_INVALIDARG;
3176 *level = session->channel_vols[index];
3181 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
3182 IChannelAudioVolume *iface, UINT32 count, const float *levels,
3183 const GUID *context)
3185 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3186 AudioSession *session = This->session;
3190 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
3191 wine_dbgstr_guid(context));
3194 return NULL_PTR_ERR;
3196 if(count != session->channel_count)
3197 return E_INVALIDARG;
3200 FIXME("Notifications not supported yet\n");
3202 EnterCriticalSection(&session->lock);
3204 for(i = 0; i < count; ++i)
3205 session->channel_vols[i] = levels[i];
3207 ret = ca_session_setvol(session, -1);
3209 LeaveCriticalSection(&session->lock);
3214 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
3215 IChannelAudioVolume *iface, UINT32 count, float *levels)
3217 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3218 AudioSession *session = This->session;
3221 TRACE("(%p)->(%d, %p)\n", session, count, levels);
3224 return NULL_PTR_ERR;
3226 if(count != session->channel_count)
3227 return E_INVALIDARG;
3229 for(i = 0; i < count; ++i)
3230 levels[i] = session->channel_vols[i];
3235 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
3237 ChannelAudioVolume_QueryInterface,
3238 ChannelAudioVolume_AddRef,
3239 ChannelAudioVolume_Release,
3240 ChannelAudioVolume_GetChannelCount,
3241 ChannelAudioVolume_SetChannelVolume,
3242 ChannelAudioVolume_GetChannelVolume,
3243 ChannelAudioVolume_SetAllVolumes,
3244 ChannelAudioVolume_GetAllVolumes
3247 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
3248 REFIID riid, void **ppv)
3250 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
3256 if(IsEqualIID(riid, &IID_IUnknown) ||
3257 IsEqualIID(riid, &IID_IAudioSessionManager) ||
3258 IsEqualIID(riid, &IID_IAudioSessionManager2))
3261 IUnknown_AddRef((IUnknown*)*ppv);
3265 WARN("Unknown interface %s\n", debugstr_guid(riid));
3266 return E_NOINTERFACE;
3269 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
3271 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3273 ref = InterlockedIncrement(&This->ref);
3274 TRACE("(%p) Refcount now %u\n", This, ref);
3278 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
3280 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3282 ref = InterlockedDecrement(&This->ref);
3283 TRACE("(%p) Refcount now %u\n", This, ref);
3285 HeapFree(GetProcessHeap(), 0, This);
3289 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
3290 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3291 IAudioSessionControl **out)
3293 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3294 AudioSession *session;
3295 AudioSessionWrapper *wrapper;
3298 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3301 hr = get_audio_session(session_guid, This->device, 0, &session);
3305 wrapper = AudioSessionWrapper_Create(NULL);
3307 return E_OUTOFMEMORY;
3309 wrapper->session = session;
3311 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3316 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
3317 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3318 ISimpleAudioVolume **out)
3320 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3321 AudioSession *session;
3322 AudioSessionWrapper *wrapper;
3325 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3328 hr = get_audio_session(session_guid, This->device, 0, &session);
3332 wrapper = AudioSessionWrapper_Create(NULL);
3334 return E_OUTOFMEMORY;
3336 wrapper->session = session;
3338 *out = &wrapper->ISimpleAudioVolume_iface;
3343 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
3344 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
3346 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3347 FIXME("(%p)->(%p) - stub\n", This, out);
3351 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
3352 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3354 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3355 FIXME("(%p)->(%p) - stub\n", This, notification);
3359 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
3360 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3362 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3363 FIXME("(%p)->(%p) - stub\n", This, notification);
3367 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
3368 IAudioSessionManager2 *iface, const WCHAR *session_id,
3369 IAudioVolumeDuckNotification *notification)
3371 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3372 FIXME("(%p)->(%p) - stub\n", This, notification);
3376 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
3377 IAudioSessionManager2 *iface,
3378 IAudioVolumeDuckNotification *notification)
3380 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3381 FIXME("(%p)->(%p) - stub\n", This, notification);
3385 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
3387 AudioSessionManager_QueryInterface,
3388 AudioSessionManager_AddRef,
3389 AudioSessionManager_Release,
3390 AudioSessionManager_GetAudioSessionControl,
3391 AudioSessionManager_GetSimpleAudioVolume,
3392 AudioSessionManager_GetSessionEnumerator,
3393 AudioSessionManager_RegisterSessionNotification,
3394 AudioSessionManager_UnregisterSessionNotification,
3395 AudioSessionManager_RegisterDuckNotification,
3396 AudioSessionManager_UnregisterDuckNotification
3399 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3400 IAudioSessionManager2 **out)
3404 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3406 return E_OUTOFMEMORY;
3408 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3409 This->device = device;
3412 *out = &This->IAudioSessionManager2_iface;