winecoreaudio: Set AudioQueue volume, not AudioDevice volume.
[wine.git] / dlls / winecoreaudio.drv / mmdevdrv.c
blobfd5e5ad125f4a6542bc8bdfc2ebe8aad8c025633
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 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "wine/list.h"
33 #include "ole2.h"
34 #include "mmdeviceapi.h"
35 #include "devpkey.h"
36 #include "dshow.h"
37 #include "dsound.h"
38 #include "endpointvolume.h"
40 #include "initguid.h"
41 #include "audioclient.h"
42 #include "audiopolicy.h"
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <fcntl.h>
53 #include <unistd.h>
55 #include <libkern/OSAtomic.h>
56 #include <CoreAudio/CoreAudio.h>
57 #include <AudioToolbox/AudioQueue.h>
59 WINE_DEFAULT_DEBUG_CHANNEL(coreaudio);
61 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
63 #define CAPTURE_BUFFERS 5
65 static const REFERENCE_TIME DefaultPeriod = 200000;
66 static const REFERENCE_TIME MinimumPeriod = 100000;
68 typedef struct _AQBuffer {
69 AudioQueueBufferRef buf;
70 struct list entry;
71 } AQBuffer;
73 struct ACImpl;
74 typedef struct ACImpl ACImpl;
76 typedef struct _AudioSession {
77 GUID guid;
78 struct list clients;
80 IMMDevice *device;
82 float master_vol;
83 UINT32 channel_count;
84 float *channel_vols;
86 CRITICAL_SECTION lock;
88 struct list entry;
89 } AudioSession;
91 typedef struct _AudioSessionWrapper {
92 IAudioSessionControl2 IAudioSessionControl2_iface;
93 IChannelAudioVolume IChannelAudioVolume_iface;
94 ISimpleAudioVolume ISimpleAudioVolume_iface;
96 LONG ref;
98 ACImpl *client;
99 AudioSession *session;
100 } AudioSessionWrapper;
102 struct ACImpl {
103 IAudioClient IAudioClient_iface;
104 IAudioRenderClient IAudioRenderClient_iface;
105 IAudioCaptureClient IAudioCaptureClient_iface;
106 IAudioClock IAudioClock_iface;
107 IAudioClock2 IAudioClock2_iface;
108 IAudioStreamVolume IAudioStreamVolume_iface;
110 LONG ref;
112 IMMDevice *parent;
114 WAVEFORMATEX *fmt;
116 EDataFlow dataflow;
117 DWORD flags;
118 AUDCLNT_SHAREMODE share;
119 HANDLE event;
120 float *vols;
122 AudioDeviceID adevid;
123 AudioQueueRef aqueue;
124 AudioObjectPropertyScope scope;
125 HANDLE timer;
126 UINT32 period_ms, bufsize_frames, inbuf_frames, written_frames;
127 UINT64 last_time;
128 AudioQueueBufferRef public_buffer;
129 BOOL getbuf_last;
130 int playing;
132 AudioSession *session;
133 AudioSessionWrapper *session_wrapper;
135 struct list entry;
137 struct list avail_buffers;
139 /* We can't use debug printing or {Enter,Leave}CriticalSection from
140 * OSX callback threads, so we use OSX's OSSpinLock for synchronization
141 * instead. OSSpinLock is not a recursive lock, so don't call
142 * synchronized functions while holding the lock. */
143 OSSpinLock lock;
146 enum PlayingStates {
147 StateStopped = 0,
148 StatePlaying,
149 StateInTransition
152 static const IAudioClientVtbl AudioClient_Vtbl;
153 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
154 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
155 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
156 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
157 static const IAudioClockVtbl AudioClock_Vtbl;
158 static const IAudioClock2Vtbl AudioClock2_Vtbl;
159 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
160 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
161 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl;
163 typedef struct _SessionMgr {
164 IAudioSessionManager2 IAudioSessionManager2_iface;
166 LONG ref;
168 IMMDevice *device;
169 } SessionMgr;
171 static HANDLE g_timer_q;
173 static CRITICAL_SECTION g_sessions_lock;
174 static struct list g_sessions = LIST_INIT(g_sessions);
176 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This, UINT64 *pos,
177 UINT64 *qpctime, BOOL raw);
178 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
179 static HRESULT ca_setvol(ACImpl *This, UINT32 index);
181 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
183 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
186 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
188 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
191 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
193 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
196 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
198 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
201 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
203 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
206 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
208 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
211 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
213 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
216 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
218 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
221 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
223 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
226 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
228 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
231 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
233 if(reason == DLL_PROCESS_ATTACH){
234 g_timer_q = CreateTimerQueue();
235 if(!g_timer_q)
236 return FALSE;
238 InitializeCriticalSection(&g_sessions_lock);
241 return TRUE;
244 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids,
245 AudioDeviceID ***keys, UINT *num, UINT *def_index)
247 UInt32 devsize, size;
248 AudioDeviceID *devices;
249 AudioDeviceID default_id;
250 AudioObjectPropertyAddress addr;
251 OSStatus sc;
252 int i, ndevices;
254 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
256 addr.mScope = kAudioObjectPropertyScopeGlobal;
257 addr.mElement = kAudioObjectPropertyElementMaster;
258 if(flow == eRender)
259 addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
260 else if(flow == eCapture)
261 addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
262 else
263 return E_INVALIDARG;
265 size = sizeof(default_id);
266 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
267 NULL, &size, &default_id);
268 if(sc != noErr){
269 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc);
270 default_id = -1;
273 addr.mSelector = kAudioHardwarePropertyDevices;
274 sc = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0,
275 NULL, &devsize);
276 if(sc != noErr){
277 WARN("Getting _Devices property size failed: %lx\n", sc);
278 return E_FAIL;
281 devices = HeapAlloc(GetProcessHeap(), 0, devsize);
282 if(!devices)
283 return E_OUTOFMEMORY;
285 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
286 &devsize, devices);
287 if(sc != noErr){
288 WARN("Getting _Devices property failed: %lx\n", sc);
289 HeapFree(GetProcessHeap(), 0, devices);
290 return E_FAIL;
293 ndevices = devsize / sizeof(AudioDeviceID);
295 *ids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(WCHAR *));
296 if(!*ids){
297 HeapFree(GetProcessHeap(), 0, devices);
298 return E_OUTOFMEMORY;
301 *keys = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(AudioDeviceID *));
302 if(!*ids){
303 HeapFree(GetProcessHeap(), 0, *ids);
304 HeapFree(GetProcessHeap(), 0, devices);
305 return E_OUTOFMEMORY;
308 *num = 0;
309 *def_index = (UINT)-1;
310 for(i = 0; i < ndevices; ++i){
311 AudioBufferList *buffers;
312 CFStringRef name;
313 SIZE_T len;
314 char nameA[256];
315 int j;
317 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
318 if(flow == eRender)
319 addr.mScope = kAudioDevicePropertyScopeOutput;
320 else
321 addr.mScope = kAudioDevicePropertyScopeInput;
322 addr.mElement = 0;
323 sc = AudioObjectGetPropertyDataSize(devices[i], &addr, 0, NULL, &size);
324 if(sc != noErr){
325 WARN("Unable to get _StreamConfiguration property size for "
326 "device %lu: %lx\n", devices[i], sc);
327 continue;
330 buffers = HeapAlloc(GetProcessHeap(), 0, size);
331 if(!buffers){
332 HeapFree(GetProcessHeap(), 0, devices);
333 for(j = 0; j < *num; ++j)
334 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
335 HeapFree(GetProcessHeap(), 0, *keys);
336 HeapFree(GetProcessHeap(), 0, *ids);
337 return E_OUTOFMEMORY;
340 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
341 &size, buffers);
342 if(sc != noErr){
343 WARN("Unable to get _StreamConfiguration property for "
344 "device %lu: %lx\n", devices[i], sc);
345 HeapFree(GetProcessHeap(), 0, buffers);
346 continue;
349 /* check that there's at least one channel in this device before
350 * we claim it as usable */
351 for(j = 0; j < buffers->mNumberBuffers; ++j)
352 if(buffers->mBuffers[j].mNumberChannels > 0)
353 break;
354 if(j >= buffers->mNumberBuffers){
355 HeapFree(GetProcessHeap(), 0, buffers);
356 continue;
359 HeapFree(GetProcessHeap(), 0, buffers);
361 size = sizeof(name);
362 addr.mSelector = kAudioObjectPropertyName;
363 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
364 &size, &name);
365 if(sc != noErr){
366 WARN("Unable to get _Name property for device %lu: %lx\n",
367 devices[i], sc);
368 continue;
371 if(!CFStringGetCString(name, nameA, sizeof(nameA),
372 kCFStringEncodingUTF8)){
373 WARN("Error converting string to UTF8\n");
374 CFRelease(name);
375 continue;
378 CFRelease(name);
380 len = MultiByteToWideChar(CP_UNIXCP, 0, nameA, -1, NULL, 0);
381 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
382 if(!(*ids)[*num]){
383 HeapFree(GetProcessHeap(), 0, devices);
384 for(j = 0; j < *num; ++j){
385 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
386 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
388 HeapFree(GetProcessHeap(), 0, *ids);
389 HeapFree(GetProcessHeap(), 0, *keys);
390 return E_OUTOFMEMORY;
392 MultiByteToWideChar(CP_UNIXCP, 0, nameA, -1, (*ids)[*num], len);
394 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0, sizeof(AudioDeviceID));
395 if(!(*ids)[*num]){
396 HeapFree(GetProcessHeap(), 0, devices);
397 HeapFree(GetProcessHeap(), 0, (*ids)[*num]);
398 for(j = 0; j < *num; ++j){
399 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
400 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
402 HeapFree(GetProcessHeap(), 0, *ids);
403 HeapFree(GetProcessHeap(), 0, *keys);
404 return E_OUTOFMEMORY;
406 *(*keys)[*num] = devices[i];
408 if(*def_index == (UINT)-1 && devices[i] == default_id)
409 *def_index = *num;
411 (*num)++;
414 if(*def_index == (UINT)-1)
415 *def_index = 0;
417 HeapFree(GetProcessHeap(), 0, devices);
419 return S_OK;
422 HRESULT WINAPI AUDDRV_GetAudioEndpoint(AudioDeviceID *adevid, IMMDevice *dev,
423 EDataFlow dataflow, IAudioClient **out)
425 ACImpl *This;
427 TRACE("%p %d %p\n", dev, dataflow, out);
429 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
430 if(!This)
431 return E_OUTOFMEMORY;
433 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
434 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
435 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
436 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
437 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
438 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
440 This->dataflow = dataflow;
442 if(dataflow == eRender)
443 This->scope = kAudioDevicePropertyScopeOutput;
444 else if(dataflow == eCapture)
445 This->scope = kAudioDevicePropertyScopeInput;
446 else{
447 HeapFree(GetProcessHeap(), 0, This);
448 return E_INVALIDARG;
451 This->lock = 0;
453 This->parent = dev;
454 IMMDevice_AddRef(This->parent);
456 list_init(&This->avail_buffers);
458 This->adevid = *adevid;
460 *out = &This->IAudioClient_iface;
461 IAudioClient_AddRef(&This->IAudioClient_iface);
463 return S_OK;
466 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
467 REFIID riid, void **ppv)
469 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
471 if(!ppv)
472 return E_POINTER;
473 *ppv = NULL;
474 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
475 *ppv = iface;
476 if(*ppv){
477 IUnknown_AddRef((IUnknown*)*ppv);
478 return S_OK;
480 WARN("Unknown interface %s\n", debugstr_guid(riid));
481 return E_NOINTERFACE;
484 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
486 ACImpl *This = impl_from_IAudioClient(iface);
487 ULONG ref;
488 ref = InterlockedIncrement(&This->ref);
489 TRACE("(%p) Refcount now %u\n", This, ref);
490 return ref;
493 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
495 ACImpl *This = impl_from_IAudioClient(iface);
496 ULONG ref;
497 ref = InterlockedDecrement(&This->ref);
498 TRACE("(%p) Refcount now %u\n", This, ref);
499 if(!ref){
500 IAudioClient_Stop(iface);
501 if(This->aqueue)
502 AudioQueueDispose(This->aqueue, 1);
503 if(This->session){
504 EnterCriticalSection(&g_sessions_lock);
505 list_remove(&This->entry);
506 LeaveCriticalSection(&g_sessions_lock);
508 HeapFree(GetProcessHeap(), 0, This->vols);
509 HeapFree(GetProcessHeap(), 0, This->public_buffer);
510 CoTaskMemFree(This->fmt);
511 IMMDevice_Release(This->parent);
512 HeapFree(GetProcessHeap(), 0, This);
514 return ref;
517 static void dump_fmt(const WAVEFORMATEX *fmt)
519 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
520 switch(fmt->wFormatTag){
521 case WAVE_FORMAT_PCM:
522 TRACE("WAVE_FORMAT_PCM");
523 break;
524 case WAVE_FORMAT_IEEE_FLOAT:
525 TRACE("WAVE_FORMAT_IEEE_FLOAT");
526 break;
527 case WAVE_FORMAT_EXTENSIBLE:
528 TRACE("WAVE_FORMAT_EXTENSIBLE");
529 break;
530 default:
531 TRACE("Unknown");
532 break;
534 TRACE(")\n");
536 TRACE("nChannels: %u\n", fmt->nChannels);
537 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
538 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
539 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
540 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
541 TRACE("cbSize: %u\n", fmt->cbSize);
543 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
544 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
545 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
546 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
547 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
551 static DWORD get_channel_mask(unsigned int channels)
553 switch(channels){
554 case 0:
555 return 0;
556 case 1:
557 return SPEAKER_FRONT_CENTER;
558 case 2:
559 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
560 case 3:
561 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
562 SPEAKER_LOW_FREQUENCY;
563 case 4:
564 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
565 SPEAKER_BACK_RIGHT;
566 case 5:
567 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
568 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY;
569 case 6:
570 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
571 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER;
572 case 7:
573 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
574 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER |
575 SPEAKER_BACK_CENTER;
577 FIXME("Unknown speaker configuration: %u\n", channels);
578 return 0;
581 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
583 WAVEFORMATEX *ret;
584 size_t size;
586 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
587 size = sizeof(WAVEFORMATEXTENSIBLE);
588 else
589 size = sizeof(WAVEFORMATEX);
591 ret = CoTaskMemAlloc(size);
592 if(!ret)
593 return NULL;
595 memcpy(ret, fmt, size);
597 ret->cbSize = size - sizeof(WAVEFORMATEX);
599 return ret;
602 static HRESULT ca_get_audiodesc(AudioStreamBasicDescription *desc,
603 const WAVEFORMATEX *fmt)
605 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
607 desc->mFormatFlags = 0;
609 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
610 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
611 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
612 desc->mFormatID = kAudioFormatLinearPCM;
613 if(fmt->wBitsPerSample > 8)
614 desc->mFormatFlags = kAudioFormatFlagIsSignedInteger;
615 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
616 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
617 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
618 desc->mFormatID = kAudioFormatLinearPCM;
619 desc->mFormatFlags = kAudioFormatFlagIsFloat;
620 }else if(fmt->wFormatTag == WAVE_FORMAT_MULAW ||
621 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
622 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_MULAW))){
623 desc->mFormatID = kAudioFormatULaw;
624 }else if(fmt->wFormatTag == WAVE_FORMAT_ALAW ||
625 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
626 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_ALAW))){
627 desc->mFormatID = kAudioFormatALaw;
628 }else
629 return AUDCLNT_E_UNSUPPORTED_FORMAT;
631 desc->mSampleRate = fmt->nSamplesPerSec;
632 desc->mBytesPerPacket = fmt->nBlockAlign;
633 desc->mFramesPerPacket = 1;
634 desc->mBytesPerFrame = fmt->nBlockAlign;
635 desc->mChannelsPerFrame = fmt->nChannels;
636 desc->mBitsPerChannel = fmt->wBitsPerSample;
637 desc->mReserved = 0;
639 return S_OK;
642 static void ca_out_buffer_cb(void *user, AudioQueueRef aqueue,
643 AudioQueueBufferRef buffer)
645 ACImpl *This = user;
646 AQBuffer *buf = buffer->mUserData;
648 OSSpinLockLock(&This->lock);
649 list_add_tail(&This->avail_buffers, &buf->entry);
650 This->inbuf_frames -= buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
651 OSSpinLockUnlock(&This->lock);
654 static void ca_in_buffer_cb(void *user, AudioQueueRef aqueue,
655 AudioQueueBufferRef buffer, const AudioTimeStamp *start,
656 UInt32 ndesc, const AudioStreamPacketDescription *descs)
658 ACImpl *This = user;
659 AQBuffer *buf = buffer->mUserData;
661 OSSpinLockLock(&This->lock);
662 list_add_tail(&This->avail_buffers, &buf->entry);
663 This->inbuf_frames += buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
664 OSSpinLockUnlock(&This->lock);
667 static HRESULT ca_setup_aqueue(AudioDeviceID did, EDataFlow flow,
668 const WAVEFORMATEX *fmt, void *user, AudioQueueRef *aqueue)
670 AudioStreamBasicDescription desc;
671 AudioObjectPropertyAddress addr;
672 CFStringRef uid;
673 OSStatus sc;
674 HRESULT hr;
675 UInt32 size;
677 addr.mScope = kAudioObjectPropertyScopeGlobal;
678 addr.mElement = 0;
679 addr.mSelector = kAudioDevicePropertyDeviceUID;
681 size = sizeof(uid);
682 sc = AudioObjectGetPropertyData(did, &addr, 0, NULL, &size, &uid);
683 if(sc != noErr){
684 WARN("Unable to get _DeviceUID property: %lx\n", sc);
685 return E_FAIL;
688 hr = ca_get_audiodesc(&desc, fmt);
689 if(FAILED(hr)){
690 CFRelease(uid);
691 return hr;
694 if(flow == eRender)
695 sc = AudioQueueNewOutput(&desc, ca_out_buffer_cb, user, NULL, NULL, 0,
696 aqueue);
697 else if(flow == eCapture)
698 sc = AudioQueueNewInput(&desc, ca_in_buffer_cb, user, NULL, NULL, 0,
699 aqueue);
700 else{
701 CFRelease(uid);
702 return E_UNEXPECTED;
704 if(sc != noErr){
705 WARN("Unable to create AudioQueue: %lx\n", sc);
706 CFRelease(uid);
707 return E_FAIL;
710 sc = AudioQueueSetProperty(*aqueue, kAudioQueueProperty_CurrentDevice,
711 &uid, sizeof(uid));
712 if(sc != noErr){
713 CFRelease(uid);
714 return E_FAIL;
717 CFRelease(uid);
719 return S_OK;
722 static void session_init_vols(AudioSession *session, UINT channels)
724 if(session->channel_count < channels){
725 UINT i;
727 if(session->channel_vols)
728 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
729 session->channel_vols, sizeof(float) * channels);
730 else
731 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
732 sizeof(float) * channels);
733 if(!session->channel_vols)
734 return;
736 for(i = session->channel_count; i < channels; ++i)
737 session->channel_vols[i] = 1.f;
739 session->channel_count = channels;
743 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
744 UINT num_channels)
746 AudioSession *ret;
748 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
749 if(!ret)
750 return NULL;
752 memcpy(&ret->guid, guid, sizeof(GUID));
754 ret->device = device;
756 list_init(&ret->clients);
758 list_add_head(&g_sessions, &ret->entry);
760 InitializeCriticalSection(&ret->lock);
762 session_init_vols(ret, num_channels);
764 ret->master_vol = 1.f;
766 return ret;
769 /* if channels == 0, then this will return or create a session with
770 * matching dataflow and GUID. otherwise, channels must also match */
771 static HRESULT get_audio_session(const GUID *sessionguid,
772 IMMDevice *device, UINT channels, AudioSession **out)
774 AudioSession *session;
776 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
777 *out = create_session(&GUID_NULL, device, channels);
778 if(!*out)
779 return E_OUTOFMEMORY;
781 return S_OK;
784 *out = NULL;
785 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
786 if(session->device == device &&
787 IsEqualGUID(sessionguid, &session->guid)){
788 session_init_vols(session, channels);
789 *out = session;
790 break;
794 if(!*out){
795 *out = create_session(sessionguid, device, channels);
796 if(!*out)
797 return E_OUTOFMEMORY;
800 return S_OK;
803 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
804 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
805 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
806 const GUID *sessionguid)
808 ACImpl *This = impl_from_IAudioClient(iface);
809 HRESULT hr;
810 OSStatus sc;
811 int i;
813 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
814 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
816 if(!fmt)
817 return E_POINTER;
819 dump_fmt(fmt);
821 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
822 return AUDCLNT_E_NOT_INITIALIZED;
824 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
825 AUDCLNT_STREAMFLAGS_LOOPBACK |
826 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
827 AUDCLNT_STREAMFLAGS_NOPERSIST |
828 AUDCLNT_STREAMFLAGS_RATEADJUST |
829 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
830 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
831 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
832 TRACE("Unknown flags: %08x\n", flags);
833 return E_INVALIDARG;
836 OSSpinLockLock(&This->lock);
838 if(This->aqueue){
839 OSSpinLockUnlock(&This->lock);
840 return AUDCLNT_E_ALREADY_INITIALIZED;
843 hr = ca_setup_aqueue(This->adevid, This->dataflow, fmt, This, &This->aqueue);
844 if(FAILED(hr)){
845 OSSpinLockUnlock(&This->lock);
846 return hr;
849 This->fmt = clone_format(fmt);
850 if(!This->fmt){
851 AudioQueueDispose(This->aqueue, 1);
852 This->aqueue = NULL;
853 OSSpinLockUnlock(&This->lock);
854 return E_OUTOFMEMORY;
857 if(period){
858 This->period_ms = period / 10000;
859 if(This->period_ms == 0)
860 This->period_ms = 1;
861 }else
862 This->period_ms = MinimumPeriod / 10000;
864 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
866 if(This->dataflow == eCapture){
867 int i;
868 UInt32 bsize = ceil((This->bufsize_frames / (double)CAPTURE_BUFFERS) *
869 This->fmt->nBlockAlign);
870 for(i = 0; i < CAPTURE_BUFFERS; ++i){
871 AQBuffer *buf;
873 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
874 if(!buf){
875 AudioQueueDispose(This->aqueue, 1);
876 This->aqueue = NULL;
877 CoTaskMemFree(This->fmt);
878 This->fmt = NULL;
879 OSSpinLockUnlock(&This->lock);
880 return E_OUTOFMEMORY;
883 sc = AudioQueueAllocateBuffer(This->aqueue, bsize, &buf->buf);
884 if(sc != noErr){
885 AudioQueueDispose(This->aqueue, 1);
886 This->aqueue = NULL;
887 CoTaskMemFree(This->fmt);
888 This->fmt = NULL;
889 OSSpinLockUnlock(&This->lock);
890 WARN("Couldn't allocate buffer: %lx\n", sc);
891 return E_FAIL;
894 buf->buf->mUserData = buf;
896 sc = AudioQueueEnqueueBuffer(This->aqueue, buf->buf, 0, NULL);
897 if(sc != noErr){
898 ERR("Couldn't enqueue buffer: %lx\n", sc);
899 break;
904 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
905 if(!This->vols){
906 AudioQueueDispose(This->aqueue, 1);
907 This->aqueue = NULL;
908 CoTaskMemFree(This->fmt);
909 This->fmt = NULL;
910 OSSpinLockUnlock(&This->lock);
911 return E_OUTOFMEMORY;
914 for(i = 0; i < fmt->nChannels; ++i)
915 This->vols[i] = 1.f;
917 This->share = mode;
918 This->flags = flags;
920 EnterCriticalSection(&g_sessions_lock);
922 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
923 &This->session);
924 if(FAILED(hr)){
925 LeaveCriticalSection(&g_sessions_lock);
926 AudioQueueDispose(This->aqueue, 1);
927 This->aqueue = NULL;
928 CoTaskMemFree(This->fmt);
929 This->fmt = NULL;
930 HeapFree(GetProcessHeap(), 0, This->vols);
931 This->vols = NULL;
932 OSSpinLockUnlock(&This->lock);
933 return E_INVALIDARG;
936 list_add_tail(&This->session->clients, &This->entry);
938 LeaveCriticalSection(&g_sessions_lock);
940 ca_setvol(This, -1);
942 OSSpinLockUnlock(&This->lock);
944 return S_OK;
947 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
948 UINT32 *frames)
950 ACImpl *This = impl_from_IAudioClient(iface);
952 TRACE("(%p)->(%p)\n", This, frames);
954 if(!frames)
955 return E_POINTER;
957 OSSpinLockLock(&This->lock);
959 if(!This->aqueue){
960 OSSpinLockUnlock(&This->lock);
961 return AUDCLNT_E_NOT_INITIALIZED;
964 *frames = This->bufsize_frames;
966 OSSpinLockUnlock(&This->lock);
968 return S_OK;
971 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
973 AudioObjectPropertyAddress addr;
974 AudioStreamID *ids;
975 UInt32 size;
976 OSStatus sc;
977 int nstreams, i;
979 addr.mScope = This->scope;
980 addr.mElement = 0;
981 addr.mSelector = kAudioDevicePropertyStreams;
983 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
984 &size);
985 if(sc != noErr){
986 WARN("Unable to get size for _Streams property: %lx\n", sc);
987 return E_FAIL;
990 ids = HeapAlloc(GetProcessHeap(), 0, size);
991 if(!ids)
992 return E_OUTOFMEMORY;
994 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
995 if(sc != noErr){
996 WARN("Unable to get _Streams property: %lx\n", sc);
997 HeapFree(GetProcessHeap(), 0, ids);
998 return E_FAIL;
1001 nstreams = size / sizeof(AudioStreamID);
1002 *max = 0;
1004 addr.mSelector = kAudioStreamPropertyLatency;
1005 for(i = 0; i < nstreams; ++i){
1006 UInt32 latency;
1008 size = sizeof(latency);
1009 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1010 &size, &latency);
1011 if(sc != noErr){
1012 WARN("Unable to get _Latency property: %lx\n", sc);
1013 continue;
1016 if(latency > *max)
1017 *max = latency;
1020 HeapFree(GetProcessHeap(), 0, ids);
1022 return S_OK;
1025 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1026 REFERENCE_TIME *out)
1028 ACImpl *This = impl_from_IAudioClient(iface);
1029 UInt32 latency, stream_latency, size;
1030 AudioObjectPropertyAddress addr;
1031 OSStatus sc;
1032 HRESULT hr;
1034 TRACE("(%p)->(%p)\n", This, out);
1036 if(!out)
1037 return E_POINTER;
1039 OSSpinLockLock(&This->lock);
1041 if(!This->aqueue){
1042 OSSpinLockUnlock(&This->lock);
1043 return AUDCLNT_E_NOT_INITIALIZED;
1046 addr.mScope = This->scope;
1047 addr.mSelector = kAudioDevicePropertyLatency;
1048 addr.mElement = 0;
1050 size = sizeof(latency);
1051 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1052 &size, &latency);
1053 if(sc != noErr){
1054 WARN("Couldn't get _Latency property: %lx\n", sc);
1055 OSSpinLockUnlock(&This->lock);
1056 return E_FAIL;
1059 hr = ca_get_max_stream_latency(This, &stream_latency);
1060 if(FAILED(hr)){
1061 OSSpinLockUnlock(&This->lock);
1062 return hr;
1065 latency += stream_latency;
1066 *out = (latency / (double)This->fmt->nSamplesPerSec) * 10000000;
1068 OSSpinLockUnlock(&This->lock);
1070 return S_OK;
1073 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1074 UINT32 *numpad)
1076 if(!This->aqueue)
1077 return AUDCLNT_E_NOT_INITIALIZED;
1079 *numpad = This->inbuf_frames;
1081 return S_OK;
1084 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1085 UINT32 *numpad)
1087 ACImpl *This = impl_from_IAudioClient(iface);
1088 HRESULT hr;
1090 TRACE("(%p)->(%p)\n", This, numpad);
1092 if(!numpad)
1093 return E_POINTER;
1095 OSSpinLockLock(&This->lock);
1097 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1099 OSSpinLockUnlock(&This->lock);
1101 return hr;
1104 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1105 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1106 WAVEFORMATEX **outpwfx)
1108 ACImpl *This = impl_from_IAudioClient(iface);
1109 AudioQueueRef aqueue;
1110 HRESULT hr;
1112 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1114 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1115 return E_POINTER;
1117 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1118 return E_INVALIDARG;
1120 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1121 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1122 return E_INVALIDARG;
1124 dump_fmt(pwfx);
1126 OSSpinLockLock(&This->lock);
1128 hr = ca_setup_aqueue(This->adevid, This->dataflow, pwfx, NULL, &aqueue);
1129 if(SUCCEEDED(hr)){
1130 AudioQueueDispose(aqueue, 1);
1131 OSSpinLockUnlock(&This->lock);
1132 if(outpwfx)
1133 *outpwfx = NULL;
1134 TRACE("returning %08x\n", S_OK);
1135 return S_OK;
1138 OSSpinLockUnlock(&This->lock);
1140 if(outpwfx)
1141 *outpwfx = NULL;
1143 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT);
1144 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1147 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1148 WAVEFORMATEX **pwfx)
1150 ACImpl *This = impl_from_IAudioClient(iface);
1151 WAVEFORMATEXTENSIBLE *fmt;
1152 OSStatus sc;
1153 UInt32 size;
1154 Float64 rate;
1155 AudioBufferList *buffers;
1156 AudioObjectPropertyAddress addr;
1157 int i;
1159 TRACE("(%p)->(%p)\n", This, pwfx);
1161 if(!pwfx)
1162 return E_POINTER;
1163 *pwfx = NULL;
1165 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1166 if(!fmt)
1167 return E_OUTOFMEMORY;
1169 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1171 addr.mScope = This->scope;
1172 addr.mElement = 0;
1173 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1175 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1176 if(sc != noErr){
1177 CoTaskMemFree(fmt);
1178 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1179 return E_FAIL;
1182 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1183 if(!buffers){
1184 CoTaskMemFree(fmt);
1185 return E_OUTOFMEMORY;
1188 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1189 &size, buffers);
1190 if(sc != noErr){
1191 CoTaskMemFree(fmt);
1192 HeapFree(GetProcessHeap(), 0, buffers);
1193 WARN("Unable to get _StreamConfiguration property: %lx\n", sc);
1194 return E_FAIL;
1197 fmt->Format.nChannels = 0;
1198 for(i = 0; i < buffers->mNumberBuffers; ++i)
1199 fmt->Format.nChannels += buffers->mBuffers[i].mNumberChannels;
1201 HeapFree(GetProcessHeap(), 0, buffers);
1203 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1205 addr.mSelector = kAudioDevicePropertyNominalSampleRate;
1206 size = sizeof(Float64);
1207 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, &rate);
1208 if(sc != noErr){
1209 CoTaskMemFree(fmt);
1210 WARN("Unable to get _NominalSampleRate property: %lx\n", sc);
1211 return E_FAIL;
1213 fmt->Format.nSamplesPerSec = rate;
1215 fmt->Format.wBitsPerSample = 32;
1216 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1218 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1219 fmt->Format.nChannels) / 8;
1220 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1221 fmt->Format.nBlockAlign;
1223 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1224 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1226 *pwfx = (WAVEFORMATEX*)fmt;
1227 dump_fmt(*pwfx);
1229 return S_OK;
1232 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1233 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1235 ACImpl *This = impl_from_IAudioClient(iface);
1237 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1239 if(!defperiod && !minperiod)
1240 return E_POINTER;
1242 OSSpinLockLock(&This->lock);
1244 if(This->period_ms){
1245 if(defperiod)
1246 *defperiod = This->period_ms * 10000;
1247 if(minperiod)
1248 *minperiod = This->period_ms * 10000;
1249 }else{
1250 if(defperiod)
1251 *defperiod = DefaultPeriod;
1252 if(minperiod)
1253 *minperiod = MinimumPeriod;
1256 OSSpinLockUnlock(&This->lock);
1258 return S_OK;
1261 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1263 ACImpl *This = user;
1265 OSSpinLockLock(&This->lock);
1266 if(This->event)
1267 SetEvent(This->event);
1268 OSSpinLockUnlock(&This->lock);
1271 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1273 ACImpl *This = impl_from_IAudioClient(iface);
1274 OSStatus sc;
1276 TRACE("(%p)\n", This);
1278 OSSpinLockLock(&This->lock);
1280 if(!This->aqueue){
1281 OSSpinLockUnlock(&This->lock);
1282 return AUDCLNT_E_NOT_INITIALIZED;
1285 if(This->playing != StateStopped){
1286 OSSpinLockUnlock(&This->lock);
1287 return AUDCLNT_E_NOT_STOPPED;
1290 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1291 OSSpinLockUnlock(&This->lock);
1292 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1295 if(This->event)
1296 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1297 ca_period_cb, This, 0, This->period_ms, 0))
1298 ERR("Unable to create timer: %u\n", GetLastError());
1300 This->playing = StateInTransition;
1302 OSSpinLockUnlock(&This->lock);
1304 sc = AudioQueueStart(This->aqueue, NULL);
1305 if(sc != noErr){
1306 WARN("Unable to start audio queue: %lx\n", sc);
1307 return E_FAIL;
1310 OSSpinLockLock(&This->lock);
1312 This->playing = StatePlaying;
1314 OSSpinLockUnlock(&This->lock);
1316 return S_OK;
1319 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1321 ACImpl *This = impl_from_IAudioClient(iface);
1322 OSStatus sc;
1324 TRACE("(%p)\n", This);
1326 OSSpinLockLock(&This->lock);
1328 if(!This->aqueue){
1329 OSSpinLockUnlock(&This->lock);
1330 return AUDCLNT_E_NOT_INITIALIZED;
1333 if(This->playing == StateStopped){
1334 OSSpinLockUnlock(&This->lock);
1335 return S_FALSE;
1338 if(This->playing == StateInTransition){
1339 OSSpinLockUnlock(&This->lock);
1340 return S_OK;
1343 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1344 DeleteTimerQueueTimer(g_timer_q, This->timer, INVALID_HANDLE_VALUE);
1345 This->timer = NULL;
1348 This->playing = StateInTransition;
1350 OSSpinLockUnlock(&This->lock);
1352 sc = AudioQueueFlush(This->aqueue);
1353 if(sc != noErr)
1354 WARN("Unable to flush audio queue: %lx\n", sc);
1356 sc = AudioQueuePause(This->aqueue);
1357 if(sc != noErr){
1358 WARN("Unable to pause audio queue: %lx\n", sc);
1359 return E_FAIL;
1362 OSSpinLockLock(&This->lock);
1364 This->playing = StateStopped;
1366 OSSpinLockUnlock(&This->lock);
1368 return S_OK;
1371 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1373 ACImpl *This = impl_from_IAudioClient(iface);
1374 HRESULT hr;
1375 OSStatus sc;
1377 TRACE("(%p)\n", This);
1379 OSSpinLockLock(&This->lock);
1381 if(!This->aqueue){
1382 OSSpinLockUnlock(&This->lock);
1383 return AUDCLNT_E_NOT_INITIALIZED;
1386 if(This->playing != StateStopped){
1387 OSSpinLockUnlock(&This->lock);
1388 return AUDCLNT_E_NOT_STOPPED;
1391 This->written_frames = 0;
1393 hr = AudioClock_GetPosition_nolock(This, &This->last_time, NULL, TRUE);
1394 if(FAILED(hr)){
1395 OSSpinLockUnlock(&This->lock);
1396 return hr;
1399 OSSpinLockUnlock(&This->lock);
1401 sc = AudioQueueReset(This->aqueue);
1402 if(sc != noErr){
1403 WARN("Unable to reset audio queue: %lx\n", sc);
1404 return E_FAIL;
1407 return S_OK;
1410 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1411 HANDLE event)
1413 ACImpl *This = impl_from_IAudioClient(iface);
1415 TRACE("(%p)->(%p)\n", This, event);
1417 if(!event)
1418 return E_INVALIDARG;
1420 OSSpinLockLock(&This->lock);
1422 if(!This->aqueue){
1423 OSSpinLockUnlock(&This->lock);
1424 return AUDCLNT_E_NOT_INITIALIZED;
1427 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1428 OSSpinLockUnlock(&This->lock);
1429 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1432 This->event = event;
1434 OSSpinLockUnlock(&This->lock);
1436 return S_OK;
1439 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1440 void **ppv)
1442 ACImpl *This = impl_from_IAudioClient(iface);
1444 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1446 if(!ppv)
1447 return E_POINTER;
1448 *ppv = NULL;
1450 OSSpinLockLock(&This->lock);
1452 if(!This->aqueue){
1453 OSSpinLockUnlock(&This->lock);
1454 return AUDCLNT_E_NOT_INITIALIZED;
1457 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1458 if(This->dataflow != eRender){
1459 OSSpinLockUnlock(&This->lock);
1460 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1462 *ppv = &This->IAudioRenderClient_iface;
1463 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1464 if(This->dataflow != eCapture){
1465 OSSpinLockUnlock(&This->lock);
1466 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1468 *ppv = &This->IAudioCaptureClient_iface;
1469 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1470 *ppv = &This->IAudioClock_iface;
1471 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1472 *ppv = &This->IAudioStreamVolume_iface;
1473 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1474 if(!This->session_wrapper){
1475 This->session_wrapper = AudioSessionWrapper_Create(This);
1476 if(!This->session_wrapper){
1477 OSSpinLockUnlock(&This->lock);
1478 return E_OUTOFMEMORY;
1482 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1483 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1484 if(!This->session_wrapper){
1485 This->session_wrapper = AudioSessionWrapper_Create(This);
1486 if(!This->session_wrapper){
1487 OSSpinLockUnlock(&This->lock);
1488 return E_OUTOFMEMORY;
1492 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1493 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1494 if(!This->session_wrapper){
1495 This->session_wrapper = AudioSessionWrapper_Create(This);
1496 if(!This->session_wrapper){
1497 OSSpinLockUnlock(&This->lock);
1498 return E_OUTOFMEMORY;
1502 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1505 if(*ppv){
1506 IUnknown_AddRef((IUnknown*)*ppv);
1507 OSSpinLockUnlock(&This->lock);
1508 return S_OK;
1511 OSSpinLockUnlock(&This->lock);
1513 FIXME("stub %s\n", debugstr_guid(riid));
1514 return E_NOINTERFACE;
1517 static const IAudioClientVtbl AudioClient_Vtbl =
1519 AudioClient_QueryInterface,
1520 AudioClient_AddRef,
1521 AudioClient_Release,
1522 AudioClient_Initialize,
1523 AudioClient_GetBufferSize,
1524 AudioClient_GetStreamLatency,
1525 AudioClient_GetCurrentPadding,
1526 AudioClient_IsFormatSupported,
1527 AudioClient_GetMixFormat,
1528 AudioClient_GetDevicePeriod,
1529 AudioClient_Start,
1530 AudioClient_Stop,
1531 AudioClient_Reset,
1532 AudioClient_SetEventHandle,
1533 AudioClient_GetService
1536 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1537 IAudioRenderClient *iface, REFIID riid, void **ppv)
1539 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1541 if(!ppv)
1542 return E_POINTER;
1543 *ppv = NULL;
1545 if(IsEqualIID(riid, &IID_IUnknown) ||
1546 IsEqualIID(riid, &IID_IAudioRenderClient))
1547 *ppv = iface;
1548 if(*ppv){
1549 IUnknown_AddRef((IUnknown*)*ppv);
1550 return S_OK;
1553 WARN("Unknown interface %s\n", debugstr_guid(riid));
1554 return E_NOINTERFACE;
1557 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1559 ACImpl *This = impl_from_IAudioRenderClient(iface);
1560 return AudioClient_AddRef(&This->IAudioClient_iface);
1563 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1565 ACImpl *This = impl_from_IAudioRenderClient(iface);
1566 return AudioClient_Release(&This->IAudioClient_iface);
1569 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1570 UINT32 frames, BYTE **data)
1572 ACImpl *This = impl_from_IAudioRenderClient(iface);
1573 AQBuffer *buf;
1574 UINT32 pad, bytes = frames * This->fmt->nBlockAlign;
1575 HRESULT hr;
1576 OSStatus sc;
1578 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1580 if(!data)
1581 return E_POINTER;
1583 OSSpinLockLock(&This->lock);
1585 if(This->getbuf_last){
1586 OSSpinLockUnlock(&This->lock);
1587 return AUDCLNT_E_OUT_OF_ORDER;
1590 if(!frames){
1591 This->getbuf_last = TRUE;
1592 OSSpinLockUnlock(&This->lock);
1593 return S_OK;
1596 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
1597 if(FAILED(hr)){
1598 OSSpinLockUnlock(&This->lock);
1599 return hr;
1602 if(pad + frames > This->bufsize_frames){
1603 OSSpinLockUnlock(&This->lock);
1604 return AUDCLNT_E_BUFFER_TOO_LARGE;
1607 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry){
1608 if(buf->buf->mAudioDataBytesCapacity >= bytes){
1609 This->public_buffer = buf->buf;
1610 list_remove(&buf->entry);
1611 break;
1615 if(&buf->entry == &This->avail_buffers){
1616 sc = AudioQueueAllocateBuffer(This->aqueue, bytes,
1617 &This->public_buffer);
1618 if(sc != noErr){
1619 OSSpinLockUnlock(&This->lock);
1620 WARN("Unable to allocate buffer: %lx\n", sc);
1621 return E_FAIL;
1623 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1624 if(!buf){
1625 AudioQueueFreeBuffer(This->aqueue, This->public_buffer);
1626 This->public_buffer = NULL;
1627 OSSpinLockUnlock(&This->lock);
1628 return E_OUTOFMEMORY;
1630 buf->buf = This->public_buffer;
1631 This->public_buffer->mUserData = buf;
1634 *data = This->public_buffer->mAudioData;
1636 This->getbuf_last = TRUE;
1638 OSSpinLockUnlock(&This->lock);
1640 return S_OK;
1643 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1644 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
1646 ACImpl *This = impl_from_IAudioRenderClient(iface);
1647 OSStatus sc;
1649 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
1651 OSSpinLockLock(&This->lock);
1653 if(!This->getbuf_last){
1654 OSSpinLockUnlock(&This->lock);
1655 return AUDCLNT_E_OUT_OF_ORDER;
1658 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1659 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1660 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1661 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1662 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1663 This->fmt->wBitsPerSample == 8)
1664 memset(This->public_buffer->mAudioData, 128,
1665 frames * This->fmt->nBlockAlign);
1666 else
1667 memset(This->public_buffer->mAudioData, 0,
1668 frames * This->fmt->nBlockAlign);
1671 This->public_buffer->mAudioDataByteSize = frames * This->fmt->nBlockAlign;
1673 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer, 0, NULL);
1674 if(sc != noErr){
1675 OSSpinLockUnlock(&This->lock);
1676 WARN("Unable to enqueue buffer: %lx\n", sc);
1677 return E_FAIL;
1680 if(This->playing == StateStopped)
1681 AudioQueuePrime(This->aqueue, 0, NULL);
1683 This->public_buffer = NULL;
1684 This->getbuf_last = FALSE;
1685 This->written_frames += frames;
1686 This->inbuf_frames += frames;
1688 OSSpinLockUnlock(&This->lock);
1690 return S_OK;
1693 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1694 AudioRenderClient_QueryInterface,
1695 AudioRenderClient_AddRef,
1696 AudioRenderClient_Release,
1697 AudioRenderClient_GetBuffer,
1698 AudioRenderClient_ReleaseBuffer
1701 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1702 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1704 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1706 if(!ppv)
1707 return E_POINTER;
1708 *ppv = NULL;
1710 if(IsEqualIID(riid, &IID_IUnknown) ||
1711 IsEqualIID(riid, &IID_IAudioCaptureClient))
1712 *ppv = iface;
1713 if(*ppv){
1714 IUnknown_AddRef((IUnknown*)*ppv);
1715 return S_OK;
1718 WARN("Unknown interface %s\n", debugstr_guid(riid));
1719 return E_NOINTERFACE;
1722 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1724 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1725 return IAudioClient_AddRef(&This->IAudioClient_iface);
1728 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1730 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1731 return IAudioClient_Release(&This->IAudioClient_iface);
1734 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1735 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1736 UINT64 *qpcpos)
1738 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1740 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1741 devpos, qpcpos);
1743 if(!data || !frames || !flags)
1744 return E_POINTER;
1746 OSSpinLockLock(&This->lock);
1748 if(This->getbuf_last){
1749 OSSpinLockUnlock(&This->lock);
1750 return AUDCLNT_E_OUT_OF_ORDER;
1753 if(This->public_buffer){
1754 *data = This->public_buffer->mAudioData;
1755 *frames =
1756 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1757 }else{
1758 struct list *head = list_head(&This->avail_buffers);
1759 if(!head){
1760 *data = NULL;
1761 *frames = 0;
1762 }else{
1763 AQBuffer *buf = LIST_ENTRY(head, AQBuffer, entry);
1764 This->public_buffer = buf->buf;
1765 *data = This->public_buffer->mAudioData;
1766 *frames =
1767 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1768 list_remove(&buf->entry);
1772 *flags = 0;
1773 This->written_frames += *frames;
1774 This->inbuf_frames -= *frames;
1775 This->getbuf_last = TRUE;
1777 if(devpos || qpcpos)
1778 AudioClock_GetPosition_nolock(This, devpos, qpcpos, FALSE);
1780 OSSpinLockUnlock(&This->lock);
1782 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1785 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1786 IAudioCaptureClient *iface, UINT32 done)
1788 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1789 UINT32 pbuf_frames =
1790 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1791 OSStatus sc;
1793 TRACE("(%p)->(%u)\n", This, done);
1795 OSSpinLockLock(&This->lock);
1797 if(!This->getbuf_last){
1798 OSSpinLockUnlock(&This->lock);
1799 return AUDCLNT_E_OUT_OF_ORDER;
1802 if(done != 0 && done != pbuf_frames){
1803 OSSpinLockUnlock(&This->lock);
1804 return AUDCLNT_E_INVALID_SIZE;
1807 if(done){
1808 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer,
1809 0, NULL);
1810 if(sc != noErr)
1811 WARN("Unable to enqueue buffer: %lx\n", sc);
1812 This->public_buffer = NULL;
1815 This->getbuf_last = FALSE;
1817 OSSpinLockUnlock(&This->lock);
1819 return S_OK;
1822 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1823 IAudioCaptureClient *iface, UINT32 *frames)
1825 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1826 struct list *head;
1827 AQBuffer *buf;
1829 TRACE("(%p)->(%p)\n", This, frames);
1831 if(!frames)
1832 return E_POINTER;
1834 OSSpinLockLock(&This->lock);
1836 head = list_head(&This->avail_buffers);
1838 if(!head){
1839 *frames = This->bufsize_frames / CAPTURE_BUFFERS;
1840 OSSpinLockUnlock(&This->lock);
1841 return S_OK;
1844 buf = LIST_ENTRY(head, AQBuffer, entry);
1845 *frames = buf->buf->mAudioDataByteSize / This->fmt->nBlockAlign;
1847 OSSpinLockUnlock(&This->lock);
1849 return S_OK;
1852 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1854 AudioCaptureClient_QueryInterface,
1855 AudioCaptureClient_AddRef,
1856 AudioCaptureClient_Release,
1857 AudioCaptureClient_GetBuffer,
1858 AudioCaptureClient_ReleaseBuffer,
1859 AudioCaptureClient_GetNextPacketSize
1862 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
1863 REFIID riid, void **ppv)
1865 ACImpl *This = impl_from_IAudioClock(iface);
1867 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1869 if(!ppv)
1870 return E_POINTER;
1871 *ppv = NULL;
1873 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
1874 *ppv = iface;
1875 else if(IsEqualIID(riid, &IID_IAudioClock2))
1876 *ppv = &This->IAudioClock2_iface;
1877 if(*ppv){
1878 IUnknown_AddRef((IUnknown*)*ppv);
1879 return S_OK;
1882 WARN("Unknown interface %s\n", debugstr_guid(riid));
1883 return E_NOINTERFACE;
1886 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
1888 ACImpl *This = impl_from_IAudioClock(iface);
1889 return IAudioClient_AddRef(&This->IAudioClient_iface);
1892 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
1894 ACImpl *This = impl_from_IAudioClock(iface);
1895 return IAudioClient_Release(&This->IAudioClient_iface);
1898 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
1900 ACImpl *This = impl_from_IAudioClock(iface);
1902 TRACE("(%p)->(%p)\n", This, freq);
1904 *freq = This->fmt->nSamplesPerSec;
1906 return S_OK;
1909 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This,
1910 UINT64 *pos, UINT64 *qpctime, BOOL raw)
1912 AudioTimeStamp time;
1913 OSStatus sc;
1915 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &time, NULL);
1916 if(sc == kAudioQueueErr_InvalidRunState){
1917 *pos = 0;
1918 }else if(sc == noErr){
1919 if(!(time.mFlags & kAudioTimeStampSampleTimeValid)){
1920 FIXME("Sample time not valid, should calculate from something else\n");
1921 return E_FAIL;
1924 if(raw)
1925 *pos = time.mSampleTime;
1926 else
1927 *pos = time.mSampleTime - This->last_time;
1928 }else{
1929 WARN("Unable to get current time: %lx\n", sc);
1930 return E_FAIL;
1933 if(qpctime){
1934 LARGE_INTEGER stamp, freq;
1935 QueryPerformanceCounter(&stamp);
1936 QueryPerformanceFrequency(&freq);
1937 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
1940 return S_OK;
1943 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
1944 UINT64 *qpctime)
1946 ACImpl *This = impl_from_IAudioClock(iface);
1947 HRESULT hr;
1949 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
1951 if(!pos)
1952 return E_POINTER;
1954 OSSpinLockLock(&This->lock);
1956 hr = AudioClock_GetPosition_nolock(This, pos, qpctime, FALSE);
1958 OSSpinLockUnlock(&This->lock);
1960 return hr;
1963 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
1964 DWORD *chars)
1966 ACImpl *This = impl_from_IAudioClock(iface);
1968 TRACE("(%p)->(%p)\n", This, chars);
1970 if(!chars)
1971 return E_POINTER;
1973 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
1975 return S_OK;
1978 static const IAudioClockVtbl AudioClock_Vtbl =
1980 AudioClock_QueryInterface,
1981 AudioClock_AddRef,
1982 AudioClock_Release,
1983 AudioClock_GetFrequency,
1984 AudioClock_GetPosition,
1985 AudioClock_GetCharacteristics
1988 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
1989 REFIID riid, void **ppv)
1991 ACImpl *This = impl_from_IAudioClock2(iface);
1992 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
1995 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
1997 ACImpl *This = impl_from_IAudioClock2(iface);
1998 return IAudioClient_AddRef(&This->IAudioClient_iface);
2001 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2003 ACImpl *This = impl_from_IAudioClock2(iface);
2004 return IAudioClient_Release(&This->IAudioClient_iface);
2007 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2008 UINT64 *pos, UINT64 *qpctime)
2010 ACImpl *This = impl_from_IAudioClock2(iface);
2012 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2014 return E_NOTIMPL;
2017 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2019 AudioClock2_QueryInterface,
2020 AudioClock2_AddRef,
2021 AudioClock2_Release,
2022 AudioClock2_GetDevicePosition
2025 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2027 AudioSessionWrapper *ret;
2029 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2030 sizeof(AudioSessionWrapper));
2031 if(!ret)
2032 return NULL;
2034 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2035 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2036 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2038 ret->client = client;
2039 if(client){
2040 ret->session = client->session;
2041 AudioClient_AddRef(&client->IAudioClient_iface);
2044 return ret;
2047 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2048 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2050 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2052 if(!ppv)
2053 return E_POINTER;
2054 *ppv = NULL;
2056 if(IsEqualIID(riid, &IID_IUnknown) ||
2057 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2058 IsEqualIID(riid, &IID_IAudioSessionControl2))
2059 *ppv = iface;
2060 if(*ppv){
2061 IUnknown_AddRef((IUnknown*)*ppv);
2062 return S_OK;
2065 WARN("Unknown interface %s\n", debugstr_guid(riid));
2066 return E_NOINTERFACE;
2069 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2071 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2072 ULONG ref;
2073 ref = InterlockedIncrement(&This->ref);
2074 TRACE("(%p) Refcount now %u\n", This, ref);
2075 return ref;
2078 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2080 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2081 ULONG ref;
2082 ref = InterlockedDecrement(&This->ref);
2083 TRACE("(%p) Refcount now %u\n", This, ref);
2084 if(!ref){
2085 OSSpinLockLock(&This->client->lock);
2086 This->client->session_wrapper = NULL;
2087 OSSpinLockUnlock(&This->client->lock);
2088 AudioClient_Release(&This->client->IAudioClient_iface);
2089 HeapFree(GetProcessHeap(), 0, This);
2091 return ref;
2094 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2095 AudioSessionState *state)
2097 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2098 ACImpl *client;
2100 TRACE("(%p)->(%p)\n", This, state);
2102 if(!state)
2103 return NULL_PTR_ERR;
2105 EnterCriticalSection(&g_sessions_lock);
2107 if(list_empty(&This->session->clients)){
2108 *state = AudioSessionStateExpired;
2109 LeaveCriticalSection(&g_sessions_lock);
2110 return S_OK;
2113 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2114 OSSpinLockLock(&client->lock);
2115 if(client->playing == StatePlaying ||
2116 client->playing == StateInTransition){
2117 *state = AudioSessionStateActive;
2118 OSSpinLockUnlock(&client->lock);
2119 LeaveCriticalSection(&g_sessions_lock);
2120 return S_OK;
2122 OSSpinLockUnlock(&client->lock);
2125 LeaveCriticalSection(&g_sessions_lock);
2127 *state = AudioSessionStateInactive;
2129 return S_OK;
2132 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2133 IAudioSessionControl2 *iface, WCHAR **name)
2135 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2137 FIXME("(%p)->(%p) - stub\n", This, name);
2139 return E_NOTIMPL;
2142 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2143 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2145 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2147 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2149 return E_NOTIMPL;
2152 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2153 IAudioSessionControl2 *iface, WCHAR **path)
2155 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2157 FIXME("(%p)->(%p) - stub\n", This, path);
2159 return E_NOTIMPL;
2162 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2163 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2165 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2167 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2169 return E_NOTIMPL;
2172 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2173 IAudioSessionControl2 *iface, GUID *group)
2175 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2177 FIXME("(%p)->(%p) - stub\n", This, group);
2179 return E_NOTIMPL;
2182 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2183 IAudioSessionControl2 *iface, GUID *group, const GUID *session)
2185 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2187 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2188 debugstr_guid(session));
2190 return E_NOTIMPL;
2193 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2194 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2196 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2198 FIXME("(%p)->(%p) - stub\n", This, events);
2200 return S_OK;
2203 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2204 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2206 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2208 FIXME("(%p)->(%p) - stub\n", This, events);
2210 return S_OK;
2213 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2214 IAudioSessionControl2 *iface, WCHAR **id)
2216 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2218 FIXME("(%p)->(%p) - stub\n", This, id);
2220 return E_NOTIMPL;
2223 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2224 IAudioSessionControl2 *iface, WCHAR **id)
2226 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2228 FIXME("(%p)->(%p) - stub\n", This, id);
2230 return E_NOTIMPL;
2233 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2234 IAudioSessionControl2 *iface, DWORD *pid)
2236 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2238 TRACE("(%p)->(%p)\n", This, pid);
2240 if(!pid)
2241 return E_POINTER;
2243 *pid = GetCurrentProcessId();
2245 return S_OK;
2248 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2249 IAudioSessionControl2 *iface)
2251 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2253 TRACE("(%p)\n", This);
2255 return S_FALSE;
2258 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2259 IAudioSessionControl2 *iface, BOOL optout)
2261 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2263 TRACE("(%p)->(%d)\n", This, optout);
2265 return S_OK;
2268 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2270 AudioSessionControl_QueryInterface,
2271 AudioSessionControl_AddRef,
2272 AudioSessionControl_Release,
2273 AudioSessionControl_GetState,
2274 AudioSessionControl_GetDisplayName,
2275 AudioSessionControl_SetDisplayName,
2276 AudioSessionControl_GetIconPath,
2277 AudioSessionControl_SetIconPath,
2278 AudioSessionControl_GetGroupingParam,
2279 AudioSessionControl_SetGroupingParam,
2280 AudioSessionControl_RegisterAudioSessionNotification,
2281 AudioSessionControl_UnregisterAudioSessionNotification,
2282 AudioSessionControl_GetSessionIdentifier,
2283 AudioSessionControl_GetSessionInstanceIdentifier,
2284 AudioSessionControl_GetProcessId,
2285 AudioSessionControl_IsSystemSoundsSession,
2286 AudioSessionControl_SetDuckingPreference
2289 /* index == -1 means set all channels, otherwise sets only the given channel */
2290 static HRESULT ca_setvol(ACImpl *This, UINT32 index)
2292 float level;
2293 OSStatus sc;
2295 if(index == (UINT32)-1){
2296 HRESULT ret = S_OK;
2297 UINT32 i;
2298 for(i = 0; i < This->fmt->nChannels; ++i){
2299 HRESULT hr;
2300 hr = ca_setvol(This, i);
2301 if(FAILED(hr))
2302 ret = hr;
2304 return ret;
2307 level = This->session->master_vol * This->session->channel_vols[index] *
2308 This->vols[index];
2310 sc = AudioQueueSetParameter(This->aqueue, kAudioQueueParam_Volume, level);
2311 if(sc != noErr){
2312 WARN("Setting _Volume property failed: %lx\n", sc);
2313 return E_FAIL;
2316 return S_OK;
2319 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2321 HRESULT ret = S_OK;
2322 ACImpl *client;
2324 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2325 HRESULT hr;
2326 hr = ca_setvol(client, index);
2327 if(FAILED(hr))
2328 ret = hr;
2331 return ret;
2334 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2335 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2337 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2339 if(!ppv)
2340 return E_POINTER;
2341 *ppv = NULL;
2343 if(IsEqualIID(riid, &IID_IUnknown) ||
2344 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2345 *ppv = iface;
2346 if(*ppv){
2347 IUnknown_AddRef((IUnknown*)*ppv);
2348 return S_OK;
2351 WARN("Unknown interface %s\n", debugstr_guid(riid));
2352 return E_NOINTERFACE;
2355 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2357 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2358 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2361 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2363 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2364 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2367 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2368 ISimpleAudioVolume *iface, float level, const GUID *context)
2370 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2371 AudioSession *session = This->session;
2372 HRESULT ret;
2374 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2376 if(level < 0.f || level > 1.f)
2377 return E_INVALIDARG;
2379 if(context)
2380 FIXME("Notifications not supported yet\n");
2382 EnterCriticalSection(&session->lock);
2384 session->master_vol = level;
2386 ret = ca_session_setvol(session, -1);
2388 LeaveCriticalSection(&session->lock);
2390 return ret;
2393 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2394 ISimpleAudioVolume *iface, float *level)
2396 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2397 AudioSession *session = This->session;
2399 TRACE("(%p)->(%p)\n", session, level);
2401 if(!level)
2402 return NULL_PTR_ERR;
2404 *level = session->master_vol;
2406 return S_OK;
2409 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2410 BOOL mute, const GUID *context)
2412 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2413 AudioSession *session = This->session;
2415 FIXME("(%p)->(%u, %p) - stub\n", session, mute, context);
2417 return E_NOTIMPL;
2420 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2421 BOOL *mute)
2423 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2424 AudioSession *session = This->session;
2426 FIXME("(%p)->(%p) - stub\n", session, mute);
2428 if(!mute)
2429 return NULL_PTR_ERR;
2431 return E_NOTIMPL;
2434 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2436 SimpleAudioVolume_QueryInterface,
2437 SimpleAudioVolume_AddRef,
2438 SimpleAudioVolume_Release,
2439 SimpleAudioVolume_SetMasterVolume,
2440 SimpleAudioVolume_GetMasterVolume,
2441 SimpleAudioVolume_SetMute,
2442 SimpleAudioVolume_GetMute
2445 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2446 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2448 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2450 if(!ppv)
2451 return E_POINTER;
2452 *ppv = NULL;
2454 if(IsEqualIID(riid, &IID_IUnknown) ||
2455 IsEqualIID(riid, &IID_IAudioStreamVolume))
2456 *ppv = iface;
2457 if(*ppv){
2458 IUnknown_AddRef((IUnknown*)*ppv);
2459 return S_OK;
2462 WARN("Unknown interface %s\n", debugstr_guid(riid));
2463 return E_NOINTERFACE;
2466 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2468 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2469 return IAudioClient_AddRef(&This->IAudioClient_iface);
2472 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2474 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2475 return IAudioClient_Release(&This->IAudioClient_iface);
2478 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2479 IAudioStreamVolume *iface, UINT32 *out)
2481 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2483 TRACE("(%p)->(%p)\n", This, out);
2485 if(!out)
2486 return E_POINTER;
2488 *out = This->fmt->nChannels;
2490 return S_OK;
2493 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2494 IAudioStreamVolume *iface, UINT32 index, float level)
2496 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2497 HRESULT ret;
2499 TRACE("(%p)->(%d, %f)\n", This, index, level);
2501 if(level < 0.f || level > 1.f)
2502 return E_INVALIDARG;
2504 if(index >= This->fmt->nChannels)
2505 return E_INVALIDARG;
2507 OSSpinLockLock(&This->lock);
2509 This->vols[index] = level;
2511 WARN("AudioQueue doesn't support per-channel volume control\n");
2512 ret = ca_setvol(This, index);
2514 OSSpinLockUnlock(&This->lock);
2516 return ret;
2519 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2520 IAudioStreamVolume *iface, UINT32 index, float *level)
2522 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2524 TRACE("(%p)->(%d, %p)\n", This, index, level);
2526 if(!level)
2527 return E_POINTER;
2529 if(index >= This->fmt->nChannels)
2530 return E_INVALIDARG;
2532 *level = This->vols[index];
2534 return S_OK;
2537 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2538 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2540 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2541 int i;
2542 HRESULT ret;
2544 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2546 if(!levels)
2547 return E_POINTER;
2549 if(count != This->fmt->nChannels)
2550 return E_INVALIDARG;
2552 OSSpinLockLock(&This->lock);
2554 for(i = 0; i < count; ++i)
2555 This->vols[i] = levels[i];
2557 ret = ca_setvol(This, -1);
2559 OSSpinLockUnlock(&This->lock);
2561 return ret;
2564 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2565 IAudioStreamVolume *iface, UINT32 count, float *levels)
2567 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2568 int i;
2570 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2572 if(!levels)
2573 return E_POINTER;
2575 if(count != This->fmt->nChannels)
2576 return E_INVALIDARG;
2578 OSSpinLockLock(&This->lock);
2580 for(i = 0; i < count; ++i)
2581 levels[i] = This->vols[i];
2583 OSSpinLockUnlock(&This->lock);
2585 return S_OK;
2588 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2590 AudioStreamVolume_QueryInterface,
2591 AudioStreamVolume_AddRef,
2592 AudioStreamVolume_Release,
2593 AudioStreamVolume_GetChannelCount,
2594 AudioStreamVolume_SetChannelVolume,
2595 AudioStreamVolume_GetChannelVolume,
2596 AudioStreamVolume_SetAllVolumes,
2597 AudioStreamVolume_GetAllVolumes
2600 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2601 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2603 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2605 if(!ppv)
2606 return E_POINTER;
2607 *ppv = NULL;
2609 if(IsEqualIID(riid, &IID_IUnknown) ||
2610 IsEqualIID(riid, &IID_IChannelAudioVolume))
2611 *ppv = iface;
2612 if(*ppv){
2613 IUnknown_AddRef((IUnknown*)*ppv);
2614 return S_OK;
2617 WARN("Unknown interface %s\n", debugstr_guid(riid));
2618 return E_NOINTERFACE;
2621 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2623 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2624 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2627 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2629 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2630 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2633 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2634 IChannelAudioVolume *iface, UINT32 *out)
2636 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2637 AudioSession *session = This->session;
2639 TRACE("(%p)->(%p)\n", session, out);
2641 if(!out)
2642 return NULL_PTR_ERR;
2644 *out = session->channel_count;
2646 return S_OK;
2649 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2650 IChannelAudioVolume *iface, UINT32 index, float level,
2651 const GUID *context)
2653 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2654 AudioSession *session = This->session;
2655 HRESULT ret;
2657 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2658 wine_dbgstr_guid(context));
2660 if(level < 0.f || level > 1.f)
2661 return E_INVALIDARG;
2663 if(index >= session->channel_count)
2664 return E_INVALIDARG;
2666 if(context)
2667 FIXME("Notifications not supported yet\n");
2669 EnterCriticalSection(&session->lock);
2671 session->channel_vols[index] = level;
2673 WARN("AudioQueue doesn't support per-channel volume control\n");
2674 ret = ca_session_setvol(session, index);
2676 LeaveCriticalSection(&session->lock);
2678 return ret;
2681 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2682 IChannelAudioVolume *iface, UINT32 index, float *level)
2684 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2685 AudioSession *session = This->session;
2687 TRACE("(%p)->(%d, %p)\n", session, index, level);
2689 if(!level)
2690 return NULL_PTR_ERR;
2692 if(index >= session->channel_count)
2693 return E_INVALIDARG;
2695 *level = session->channel_vols[index];
2697 return S_OK;
2700 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2701 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2702 const GUID *context)
2704 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2705 AudioSession *session = This->session;
2706 int i;
2707 HRESULT ret;
2709 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2710 wine_dbgstr_guid(context));
2712 if(!levels)
2713 return NULL_PTR_ERR;
2715 if(count != session->channel_count)
2716 return E_INVALIDARG;
2718 if(context)
2719 FIXME("Notifications not supported yet\n");
2721 EnterCriticalSection(&session->lock);
2723 for(i = 0; i < count; ++i)
2724 session->channel_vols[i] = levels[i];
2726 ret = ca_session_setvol(session, -1);
2728 LeaveCriticalSection(&session->lock);
2730 return ret;
2733 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2734 IChannelAudioVolume *iface, UINT32 count, float *levels)
2736 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2737 AudioSession *session = This->session;
2738 int i;
2740 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2742 if(!levels)
2743 return NULL_PTR_ERR;
2745 if(count != session->channel_count)
2746 return E_INVALIDARG;
2748 for(i = 0; i < count; ++i)
2749 levels[i] = session->channel_vols[i];
2751 return S_OK;
2754 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2756 ChannelAudioVolume_QueryInterface,
2757 ChannelAudioVolume_AddRef,
2758 ChannelAudioVolume_Release,
2759 ChannelAudioVolume_GetChannelCount,
2760 ChannelAudioVolume_SetChannelVolume,
2761 ChannelAudioVolume_GetChannelVolume,
2762 ChannelAudioVolume_SetAllVolumes,
2763 ChannelAudioVolume_GetAllVolumes
2766 HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2767 REFIID riid, void **ppv)
2769 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2771 if(!ppv)
2772 return E_POINTER;
2773 *ppv = NULL;
2775 if(IsEqualIID(riid, &IID_IUnknown) ||
2776 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2777 IsEqualIID(riid, &IID_IAudioSessionManager2))
2778 *ppv = iface;
2779 if(*ppv){
2780 IUnknown_AddRef((IUnknown*)*ppv);
2781 return S_OK;
2784 WARN("Unknown interface %s\n", debugstr_guid(riid));
2785 return E_NOINTERFACE;
2788 ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2790 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2791 ULONG ref;
2792 ref = InterlockedIncrement(&This->ref);
2793 TRACE("(%p) Refcount now %u\n", This, ref);
2794 return ref;
2797 ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
2799 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2800 ULONG ref;
2801 ref = InterlockedDecrement(&This->ref);
2802 TRACE("(%p) Refcount now %u\n", This, ref);
2803 if(!ref)
2804 HeapFree(GetProcessHeap(), 0, This);
2805 return ref;
2808 HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
2809 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2810 IAudioSessionControl **out)
2812 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2813 AudioSession *session;
2814 AudioSessionWrapper *wrapper;
2815 HRESULT hr;
2817 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2818 flags, out);
2820 hr = get_audio_session(session_guid, This->device, 0, &session);
2821 if(FAILED(hr))
2822 return hr;
2824 wrapper = AudioSessionWrapper_Create(NULL);
2825 if(!wrapper)
2826 return E_OUTOFMEMORY;
2828 wrapper->session = session;
2830 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
2832 return S_OK;
2835 HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
2836 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2837 ISimpleAudioVolume **out)
2839 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2840 AudioSession *session;
2841 AudioSessionWrapper *wrapper;
2842 HRESULT hr;
2844 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2845 flags, out);
2847 hr = get_audio_session(session_guid, This->device, 0, &session);
2848 if(FAILED(hr))
2849 return hr;
2851 wrapper = AudioSessionWrapper_Create(NULL);
2852 if(!wrapper)
2853 return E_OUTOFMEMORY;
2855 wrapper->session = session;
2857 *out = &wrapper->ISimpleAudioVolume_iface;
2859 return S_OK;
2862 HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
2863 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
2865 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2866 FIXME("(%p)->(%p) - stub\n", This, out);
2867 return E_NOTIMPL;
2870 HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
2871 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2873 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2874 FIXME("(%p)->(%p) - stub\n", This, notification);
2875 return E_NOTIMPL;
2878 HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
2879 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2881 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2882 FIXME("(%p)->(%p) - stub\n", This, notification);
2883 return E_NOTIMPL;
2886 HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
2887 IAudioSessionManager2 *iface, const WCHAR *session_id,
2888 IAudioVolumeDuckNotification *notification)
2890 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2891 FIXME("(%p)->(%p) - stub\n", This, notification);
2892 return E_NOTIMPL;
2895 HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
2896 IAudioSessionManager2 *iface,
2897 IAudioVolumeDuckNotification *notification)
2899 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2900 FIXME("(%p)->(%p) - stub\n", This, notification);
2901 return E_NOTIMPL;
2904 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
2906 AudioSessionManager_QueryInterface,
2907 AudioSessionManager_AddRef,
2908 AudioSessionManager_Release,
2909 AudioSessionManager_GetAudioSessionControl,
2910 AudioSessionManager_GetSimpleAudioVolume,
2911 AudioSessionManager_GetSessionEnumerator,
2912 AudioSessionManager_RegisterSessionNotification,
2913 AudioSessionManager_UnregisterSessionNotification,
2914 AudioSessionManager_RegisterDuckNotification,
2915 AudioSessionManager_UnregisterDuckNotification
2918 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
2919 IAudioSessionManager2 **out)
2921 SessionMgr *This;
2923 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
2924 if(!This)
2925 return E_OUTOFMEMORY;
2927 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
2928 This->device = device;
2929 This->ref = 1;
2931 *out = &This->IAudioSessionManager2_iface;
2933 return S_OK;