mmdevapi: Don't fail if dwChannelMask is not set correctly.
[wine.git] / dlls / wineoss.drv / mmdevdrv.c
blob8d1a99747f89ac624c53c871771e8d29926a90b4
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>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <math.h>
35 #include <sys/soundcard.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnls.h"
40 #include "winreg.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.h"
45 #include "ole2.h"
46 #include "mmdeviceapi.h"
47 #include "devpkey.h"
48 #include "dshow.h"
49 #include "dsound.h"
51 #include "initguid.h"
52 #include "endpointvolume.h"
53 #include "audiopolicy.h"
54 #include "audioclient.h"
57 /* Some implementations of OSS, such as FreeBSD older than 9.0, lack
58 SNDCTL_DSP_HALT which is just a synonym for the older SNDCTL_DSP_RESET. */
59 #ifndef SNDCTL_DSP_HALT
60 #define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
61 #endif
63 WINE_DEFAULT_DEBUG_CHANNEL(oss);
65 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
67 static const REFERENCE_TIME DefaultPeriod = 200000;
68 static const REFERENCE_TIME MinimumPeriod = 100000;
70 struct ACImpl;
71 typedef struct ACImpl ACImpl;
73 typedef struct _AudioSession {
74 GUID guid;
75 struct list clients;
77 IMMDevice *device;
79 float master_vol;
80 UINT32 channel_count;
81 float *channel_vols;
82 BOOL mute;
84 CRITICAL_SECTION lock;
86 struct list entry;
87 } AudioSession;
89 typedef struct _AudioSessionWrapper {
90 IAudioSessionControl2 IAudioSessionControl2_iface;
91 IChannelAudioVolume IChannelAudioVolume_iface;
92 ISimpleAudioVolume ISimpleAudioVolume_iface;
94 LONG ref;
96 ACImpl *client;
97 AudioSession *session;
98 } AudioSessionWrapper;
100 struct ACImpl {
101 IAudioClient IAudioClient_iface;
102 IAudioRenderClient IAudioRenderClient_iface;
103 IAudioCaptureClient IAudioCaptureClient_iface;
104 IAudioClock IAudioClock_iface;
105 IAudioClock2 IAudioClock2_iface;
106 IAudioStreamVolume IAudioStreamVolume_iface;
108 LONG ref;
110 IMMDevice *parent;
112 WAVEFORMATEX *fmt;
114 EDataFlow dataflow;
115 DWORD flags;
116 AUDCLNT_SHAREMODE share;
117 HANDLE event;
118 float *vols;
120 int fd;
121 oss_audioinfo ai;
123 BOOL initted, playing;
124 UINT64 written_frames;
125 UINT32 period_us, bufsize_frames, held_frames, tmp_buffer_frames, inbuf_frames;
126 UINT32 lcl_offs_frames; /* offs into local_buffer where valid data starts */
128 BYTE *local_buffer, *tmp_buffer;
129 int buf_state;
130 HANDLE timer;
132 CRITICAL_SECTION lock;
134 AudioSession *session;
135 AudioSessionWrapper *session_wrapper;
137 struct list entry;
140 enum BufferStates {
141 NOT_LOCKED = 0,
142 LOCKED_NORMAL, /* public buffer piece is from local_buffer */
143 LOCKED_WRAPPED /* public buffer piece is in tmp_buffer */
146 typedef struct _SessionMgr {
147 IAudioSessionManager2 IAudioSessionManager2_iface;
149 LONG ref;
151 IMMDevice *device;
152 } SessionMgr;
154 static HANDLE g_timer_q;
156 static CRITICAL_SECTION g_sessions_lock;
157 static struct list g_sessions = LIST_INIT(g_sessions);
159 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
160 static HRESULT oss_setvol(ACImpl *This, UINT32 index);
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 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
175 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
178 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
180 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
183 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
185 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
188 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
190 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
193 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
195 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
198 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
200 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
203 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
205 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
208 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
210 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
213 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
215 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
218 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
220 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
223 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
225 if(reason == DLL_PROCESS_ATTACH){
226 g_timer_q = CreateTimerQueue();
227 if(!g_timer_q)
228 return FALSE;
230 InitializeCriticalSection(&g_sessions_lock);
233 return TRUE;
236 /* From <dlls/mmdevapi/mmdevapi.h> */
237 enum DriverPriority {
238 Priority_Unavailable = 0,
239 Priority_Low,
240 Priority_Neutral,
241 Priority_Preferred
244 int WINAPI AUDDRV_GetPriority(void)
246 int mixer_fd;
247 oss_sysinfo sysinfo;
249 /* Attempt to determine if we are running on OSS or ALSA's OSS
250 * compatibility layer. There is no official way to do that, so just check
251 * for validity as best as possible, without rejecting valid OSS
252 * implementations. */
254 mixer_fd = open("/dev/mixer", O_RDONLY, 0);
255 if(mixer_fd < 0){
256 TRACE("Priority_Unavailable: open failed\n");
257 return Priority_Unavailable;
260 sysinfo.version[0] = 0xFF;
261 sysinfo.versionnum = ~0;
262 if(ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) < 0){
263 TRACE("Priority_Unavailable: ioctl failed\n");
264 close(mixer_fd);
265 return Priority_Unavailable;
268 close(mixer_fd);
270 if(sysinfo.version[0] < '4' || sysinfo.version[0] > '9'){
271 TRACE("Priority_Low: sysinfo.version[0]: %x\n", sysinfo.version[0]);
272 return Priority_Low;
274 if(sysinfo.versionnum & 0x80000000){
275 TRACE("Priority_Low: sysinfo.versionnum: %x\n", sysinfo.versionnum);
276 return Priority_Low;
279 TRACE("Priority_Preferred: Seems like valid OSS!\n");
281 return Priority_Preferred;
284 static UINT get_default_index(EDataFlow flow, char **keys, UINT num)
286 int fd = -1, err, i;
287 oss_audioinfo ai;
289 if(flow == eRender)
290 fd = open("/dev/dsp", O_WRONLY);
291 else
292 fd = open("/dev/dsp", O_RDONLY);
294 if(fd < 0){
295 WARN("Couldn't open default device!\n");
296 return 0;
299 ai.dev = -1;
300 if((err = ioctl(fd, SNDCTL_ENGINEINFO, &ai)) < 0){
301 WARN("SNDCTL_ENGINEINFO failed: %d (%s)\n", err, strerror(errno));
302 close(fd);
303 return 0;
306 close(fd);
308 TRACE("Default devnode: %s\n", ai.devnode);
309 for(i = 0; i < num; ++i)
310 if(!strcmp(ai.devnode, keys[i]))
311 return i;
313 WARN("Couldn't find default device! Choosing first.\n");
314 return 0;
317 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, char ***keys,
318 UINT *num, UINT *def_index)
320 int i, mixer_fd;
321 oss_sysinfo sysinfo;
322 static int print_once = 0;
324 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
326 mixer_fd = open("/dev/mixer", O_RDONLY, 0);
327 if(mixer_fd < 0){
328 ERR("OSS /dev/mixer doesn't seem to exist\n");
329 return AUDCLNT_E_SERVICE_NOT_RUNNING;
332 if(ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) < 0){
333 close(mixer_fd);
335 if(errno == EINVAL){
336 ERR("OSS version too old, need at least OSSv4\n");
337 return AUDCLNT_E_SERVICE_NOT_RUNNING;
340 ERR("Error getting SNDCTL_SYSINFO: %d (%s)\n", errno, strerror(errno));
341 return E_FAIL;
344 if(!print_once){
345 TRACE("OSS sysinfo:\n");
346 TRACE("product: %s\n", sysinfo.product);
347 TRACE("version: %s\n", sysinfo.version);
348 TRACE("versionnum: %x\n", sysinfo.versionnum);
349 TRACE("numaudios: %d\n", sysinfo.numaudios);
350 TRACE("nummixers: %d\n", sysinfo.nummixers);
351 TRACE("numcards: %d\n", sysinfo.numcards);
352 TRACE("numaudioengines: %d\n", sysinfo.numaudioengines);
353 print_once = 1;
356 if(sysinfo.numaudios <= 0){
357 WARN("No audio devices!\n");
358 close(mixer_fd);
359 return AUDCLNT_E_SERVICE_NOT_RUNNING;
362 *ids = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(WCHAR *));
363 *keys = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(char *));
365 *num = 0;
366 for(i = 0; i < sysinfo.numaudios; ++i){
367 oss_audioinfo ai = {0};
368 int fd;
370 ai.dev = i;
371 if(ioctl(mixer_fd, SNDCTL_AUDIOINFO, &ai) < 0){
372 WARN("Error getting AUDIOINFO for dev %d: %d (%s)\n", i, errno,
373 strerror(errno));
374 continue;
377 if(flow == eRender)
378 fd = open(ai.devnode, O_WRONLY, 0);
379 else
380 fd = open(ai.devnode, O_RDONLY, 0);
381 if(fd < 0){
382 WARN("Opening device \"%s\" failed, pretending it doesn't exist: %d (%s)\n",
383 ai.devnode, errno, strerror(errno));
384 continue;
386 close(fd);
388 if((flow == eCapture && (ai.caps & PCM_CAP_INPUT)) ||
389 (flow == eRender && (ai.caps & PCM_CAP_OUTPUT))){
390 size_t len;
392 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0,
393 strlen(ai.devnode) + 1);
394 if(!(*keys)[*num]){
395 for(i = 0; i < *num; ++i){
396 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
397 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
399 HeapFree(GetProcessHeap(), 0, *ids);
400 HeapFree(GetProcessHeap(), 0, *keys);
401 close(mixer_fd);
402 return E_OUTOFMEMORY;
404 strcpy((*keys)[*num], ai.devnode);
406 len = MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1, NULL, 0);
407 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0,
408 len * sizeof(WCHAR));
409 if(!(*ids)[*num]){
410 HeapFree(GetProcessHeap(), 0, (*keys)[*num]);
411 for(i = 0; i < *num; ++i){
412 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
413 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
415 HeapFree(GetProcessHeap(), 0, *ids);
416 HeapFree(GetProcessHeap(), 0, *keys);
417 close(mixer_fd);
418 return E_OUTOFMEMORY;
420 MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1,
421 (*ids)[*num], len);
423 (*num)++;
427 close(mixer_fd);
429 *def_index = get_default_index(flow, *keys, *num);
431 return S_OK;
434 HRESULT WINAPI AUDDRV_GetAudioEndpoint(char *devnode, IMMDevice *dev,
435 EDataFlow dataflow, IAudioClient **out)
437 ACImpl *This;
439 TRACE("%s %p %d %p\n", devnode, dev, dataflow, out);
441 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
442 if(!This)
443 return E_OUTOFMEMORY;
445 if(dataflow == eRender)
446 This->fd = open(devnode, O_WRONLY, 0);
447 else if(dataflow == eCapture)
448 This->fd = open(devnode, O_RDONLY, 0);
449 else{
450 HeapFree(GetProcessHeap(), 0, This);
451 return E_INVALIDARG;
453 if(This->fd < 0){
454 ERR("Unable to open device %s: %d (%s)\n", devnode, errno,
455 strerror(errno));
456 HeapFree(GetProcessHeap(), 0, This);
457 return AUDCLNT_E_DEVICE_INVALIDATED;
460 This->dataflow = dataflow;
462 This->ai.dev = -1;
463 if(ioctl(This->fd, SNDCTL_ENGINEINFO, &This->ai) < 0){
464 ERR("Unable to get audio info for device %s: %d (%s)\n", devnode,
465 errno, strerror(errno));
466 close(This->fd);
467 HeapFree(GetProcessHeap(), 0, This);
468 return E_FAIL;
471 TRACE("OSS audioinfo:\n");
472 TRACE("devnode: %s\n", This->ai.devnode);
473 TRACE("name: %s\n", This->ai.name);
474 TRACE("busy: %x\n", This->ai.busy);
475 TRACE("caps: %x\n", This->ai.caps);
476 TRACE("iformats: %x\n", This->ai.iformats);
477 TRACE("oformats: %x\n", This->ai.oformats);
478 TRACE("enabled: %d\n", This->ai.enabled);
479 TRACE("min_rate: %d\n", This->ai.min_rate);
480 TRACE("max_rate: %d\n", This->ai.max_rate);
481 TRACE("min_channels: %d\n", This->ai.min_channels);
482 TRACE("max_channels: %d\n", This->ai.max_channels);
484 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
485 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
486 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
487 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
488 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
489 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
491 InitializeCriticalSection(&This->lock);
493 This->parent = dev;
494 IMMDevice_AddRef(This->parent);
496 IAudioClient_AddRef(&This->IAudioClient_iface);
498 *out = &This->IAudioClient_iface;
500 return S_OK;
503 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
504 REFIID riid, void **ppv)
506 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
508 if(!ppv)
509 return E_POINTER;
510 *ppv = NULL;
511 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
512 *ppv = iface;
513 if(*ppv){
514 IUnknown_AddRef((IUnknown*)*ppv);
515 return S_OK;
517 WARN("Unknown interface %s\n", debugstr_guid(riid));
518 return E_NOINTERFACE;
521 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
523 ACImpl *This = impl_from_IAudioClient(iface);
524 ULONG ref;
525 ref = InterlockedIncrement(&This->ref);
526 TRACE("(%p) Refcount now %u\n", This, ref);
527 return ref;
530 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
532 ACImpl *This = impl_from_IAudioClient(iface);
533 ULONG ref;
534 ref = InterlockedDecrement(&This->ref);
535 TRACE("(%p) Refcount now %u\n", This, ref);
536 if(!ref){
537 IAudioClient_Stop(iface);
538 IMMDevice_Release(This->parent);
539 DeleteCriticalSection(&This->lock);
540 close(This->fd);
541 if(This->initted){
542 EnterCriticalSection(&g_sessions_lock);
543 list_remove(&This->entry);
544 LeaveCriticalSection(&g_sessions_lock);
546 HeapFree(GetProcessHeap(), 0, This->vols);
547 HeapFree(GetProcessHeap(), 0, This->local_buffer);
548 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
549 CoTaskMemFree(This->fmt);
550 HeapFree(GetProcessHeap(), 0, This);
552 return ref;
555 static void dump_fmt(const WAVEFORMATEX *fmt)
557 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
558 switch(fmt->wFormatTag){
559 case WAVE_FORMAT_PCM:
560 TRACE("WAVE_FORMAT_PCM");
561 break;
562 case WAVE_FORMAT_IEEE_FLOAT:
563 TRACE("WAVE_FORMAT_IEEE_FLOAT");
564 break;
565 case WAVE_FORMAT_EXTENSIBLE:
566 TRACE("WAVE_FORMAT_EXTENSIBLE");
567 break;
568 default:
569 TRACE("Unknown");
570 break;
572 TRACE(")\n");
574 TRACE("nChannels: %u\n", fmt->nChannels);
575 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
576 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
577 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
578 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
579 TRACE("cbSize: %u\n", fmt->cbSize);
581 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
582 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
583 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
584 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
585 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
589 static DWORD get_channel_mask(unsigned int channels)
591 switch(channels){
592 case 0:
593 return 0;
594 case 1:
595 return KSAUDIO_SPEAKER_MONO;
596 case 2:
597 return KSAUDIO_SPEAKER_STEREO;
598 case 3:
599 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
600 case 4:
601 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
602 case 5:
603 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
604 case 6:
605 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
606 case 7:
607 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
608 case 8:
609 return KSAUDIO_SPEAKER_7POINT1; /* not 7POINT1_SURROUND */
611 FIXME("Unknown speaker configuration: %u\n", channels);
612 return 0;
615 static int get_oss_format(const WAVEFORMATEX *fmt)
617 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)fmt;
619 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
620 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
621 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
622 switch(fmt->wBitsPerSample){
623 case 8:
624 return AFMT_U8;
625 case 16:
626 return AFMT_S16_LE;
627 case 24:
628 return AFMT_S24_LE;
629 case 32:
630 return AFMT_S32_LE;
632 return -1;
635 #ifdef AFMT_FLOAT
636 if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
637 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
638 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
639 if(fmt->wBitsPerSample != 32)
640 return -1;
642 return AFMT_FLOAT;
644 #endif
646 return -1;
649 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
651 WAVEFORMATEX *ret;
652 size_t size;
654 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
655 size = sizeof(WAVEFORMATEXTENSIBLE);
656 else
657 size = sizeof(WAVEFORMATEX);
659 ret = CoTaskMemAlloc(size);
660 if(!ret)
661 return NULL;
663 memcpy(ret, fmt, size);
665 ret->cbSize = size - sizeof(WAVEFORMATEX);
667 return ret;
670 static HRESULT setup_oss_device(ACImpl *This, const WAVEFORMATEX *fmt,
671 WAVEFORMATEX **out, BOOL query)
673 int tmp, oss_format;
674 double tenth;
675 HRESULT ret = S_OK;
676 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
677 WAVEFORMATEX *closest = NULL;
679 if(out)
680 *out = NULL;
682 tmp = oss_format = get_oss_format(fmt);
683 if(oss_format < 0)
684 return AUDCLNT_E_UNSUPPORTED_FORMAT;
685 if(ioctl(This->fd, SNDCTL_DSP_SETFMT, &tmp) < 0){
686 WARN("SETFMT failed: %d (%s)\n", errno, strerror(errno));
687 return E_FAIL;
689 if(tmp != oss_format){
690 TRACE("Format unsupported by this OSS version: %x\n", oss_format);
691 return AUDCLNT_E_UNSUPPORTED_FORMAT;
694 closest = clone_format(fmt);
695 if(!closest)
696 return E_OUTOFMEMORY;
698 tmp = fmt->nSamplesPerSec;
699 if(ioctl(This->fd, SNDCTL_DSP_SPEED, &tmp) < 0){
700 WARN("SPEED failed: %d (%s)\n", errno, strerror(errno));
701 CoTaskMemFree(closest);
702 return E_FAIL;
704 tenth = fmt->nSamplesPerSec * 0.1;
705 if(tmp > fmt->nSamplesPerSec + tenth || tmp < fmt->nSamplesPerSec - tenth){
706 ret = S_FALSE;
707 closest->nSamplesPerSec = tmp;
710 tmp = fmt->nChannels;
711 if(ioctl(This->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0){
712 WARN("CHANNELS failed: %d (%s)\n", errno, strerror(errno));
713 CoTaskMemFree(closest);
714 return E_FAIL;
716 if(tmp != fmt->nChannels){
717 ret = S_FALSE;
718 closest->nChannels = tmp;
721 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
722 DWORD mask = get_channel_mask(closest->nChannels);
724 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = mask;
726 if(query && fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
727 fmtex->dwChannelMask != 0 &&
728 fmtex->dwChannelMask != mask)
729 ret = S_FALSE;
732 if(ret == S_FALSE && out){
733 closest->nBlockAlign =
734 closest->nChannels * closest->wBitsPerSample / 8;
735 closest->nAvgBytesPerSec =
736 closest->nBlockAlign * closest->nSamplesPerSec;
737 *out = closest;
738 } else
739 CoTaskMemFree(closest);
741 TRACE("returning: %08x\n", ret);
742 return ret;
745 static void session_init_vols(AudioSession *session, UINT channels)
747 if(session->channel_count < channels){
748 UINT i;
750 if(session->channel_vols)
751 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
752 session->channel_vols, sizeof(float) * channels);
753 else
754 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
755 sizeof(float) * channels);
756 if(!session->channel_vols)
757 return;
759 for(i = session->channel_count; i < channels; ++i)
760 session->channel_vols[i] = 1.f;
762 session->channel_count = channels;
766 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
767 UINT num_channels)
769 AudioSession *ret;
771 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
772 if(!ret)
773 return NULL;
775 memcpy(&ret->guid, guid, sizeof(GUID));
777 ret->device = device;
779 list_init(&ret->clients);
781 list_add_head(&g_sessions, &ret->entry);
783 InitializeCriticalSection(&ret->lock);
785 session_init_vols(ret, num_channels);
787 ret->master_vol = 1.f;
789 return ret;
792 /* if channels == 0, then this will return or create a session with
793 * matching dataflow and GUID. otherwise, channels must also match */
794 static HRESULT get_audio_session(const GUID *sessionguid,
795 IMMDevice *device, UINT channels, AudioSession **out)
797 AudioSession *session;
799 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
800 *out = create_session(&GUID_NULL, device, channels);
801 if(!*out)
802 return E_OUTOFMEMORY;
804 return S_OK;
807 *out = NULL;
808 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
809 if(session->device == device &&
810 IsEqualGUID(sessionguid, &session->guid)){
811 session_init_vols(session, channels);
812 *out = session;
813 break;
817 if(!*out){
818 *out = create_session(sessionguid, device, channels);
819 if(!*out)
820 return E_OUTOFMEMORY;
823 return S_OK;
826 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
827 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
828 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
829 const GUID *sessionguid)
831 ACImpl *This = impl_from_IAudioClient(iface);
832 int mask, i;
833 HRESULT hr;
835 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
836 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
838 if(!fmt)
839 return E_POINTER;
841 dump_fmt(fmt);
843 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
844 return AUDCLNT_E_NOT_INITIALIZED;
846 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
847 AUDCLNT_STREAMFLAGS_LOOPBACK |
848 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
849 AUDCLNT_STREAMFLAGS_NOPERSIST |
850 AUDCLNT_STREAMFLAGS_RATEADJUST |
851 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
852 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
853 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
854 TRACE("Unknown flags: %08x\n", flags);
855 return E_INVALIDARG;
858 EnterCriticalSection(&This->lock);
860 if(This->initted){
861 LeaveCriticalSection(&This->lock);
862 return AUDCLNT_E_ALREADY_INITIALIZED;
865 hr = setup_oss_device(This, fmt, NULL, FALSE);
866 if(hr == S_FALSE){
867 LeaveCriticalSection(&This->lock);
868 return AUDCLNT_E_UNSUPPORTED_FORMAT;
870 if(FAILED(hr)){
871 LeaveCriticalSection(&This->lock);
872 return hr;
875 mask = 0;
876 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
877 LeaveCriticalSection(&This->lock);
878 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
879 return E_FAIL;
882 mask = (100 << 8) | 100;
883 if(ioctl(This->fd, SNDCTL_DSP_SETPLAYVOL, &mask) < 0)
884 WARN("SETPLAYVOL failed: %d (%s)\n", errno, strerror(errno));
886 This->fmt = clone_format(fmt);
887 if(!This->fmt){
888 LeaveCriticalSection(&This->lock);
889 return E_OUTOFMEMORY;
892 if(period)
893 This->period_us = period / 10;
894 else
895 This->period_us = DefaultPeriod / 10;
897 if(!duration)
898 duration = 300000; /* 0.03s */
899 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
900 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
901 This->bufsize_frames * fmt->nBlockAlign);
902 if(!This->local_buffer){
903 CoTaskMemFree(This->fmt);
904 This->fmt = NULL;
905 LeaveCriticalSection(&This->lock);
906 return E_OUTOFMEMORY;
909 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
910 if(!This->vols){
911 CoTaskMemFree(This->fmt);
912 This->fmt = NULL;
913 LeaveCriticalSection(&This->lock);
914 return E_OUTOFMEMORY;
917 for(i = 0; i < fmt->nChannels; ++i)
918 This->vols[i] = 1.f;
920 This->share = mode;
921 This->flags = flags;
923 EnterCriticalSection(&g_sessions_lock);
925 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
926 &This->session);
927 if(FAILED(hr)){
928 LeaveCriticalSection(&g_sessions_lock);
929 HeapFree(GetProcessHeap(), 0, This->vols);
930 This->vols = NULL;
931 CoTaskMemFree(This->fmt);
932 This->fmt = NULL;
933 LeaveCriticalSection(&This->lock);
934 return hr;
937 list_add_tail(&This->session->clients, &This->entry);
939 LeaveCriticalSection(&g_sessions_lock);
941 This->initted = TRUE;
943 oss_setvol(This, -1);
945 LeaveCriticalSection(&This->lock);
947 return S_OK;
950 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
951 UINT32 *frames)
953 ACImpl *This = impl_from_IAudioClient(iface);
955 TRACE("(%p)->(%p)\n", This, frames);
957 if(!frames)
958 return E_POINTER;
960 EnterCriticalSection(&This->lock);
962 if(!This->initted){
963 LeaveCriticalSection(&This->lock);
964 return AUDCLNT_E_NOT_INITIALIZED;
967 *frames = This->bufsize_frames;
969 LeaveCriticalSection(&This->lock);
971 return S_OK;
974 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
975 REFERENCE_TIME *latency)
977 ACImpl *This = impl_from_IAudioClient(iface);
979 TRACE("(%p)->(%p)\n", This, latency);
981 if(!latency)
982 return E_POINTER;
984 EnterCriticalSection(&This->lock);
986 if(!This->initted){
987 LeaveCriticalSection(&This->lock);
988 return AUDCLNT_E_NOT_INITIALIZED;
991 if(This->dataflow == eRender){
992 int delay_bytes;
993 double delay_s;
995 if(ioctl(This->fd, SNDCTL_DSP_GETODELAY, &delay_bytes) < 0){
996 LeaveCriticalSection(&This->lock);
997 WARN("GETODELAY failed: %d (%s)\n", errno, strerror(errno));
998 return E_FAIL;
1001 delay_s = delay_bytes / (double)(This->fmt->nSamplesPerSec *
1002 This->fmt->nBlockAlign);
1004 *latency = delay_s * 10000000;
1005 }else
1006 *latency = 10000; /* OSS doesn't provide input latency */
1008 LeaveCriticalSection(&This->lock);
1010 return S_OK;
1013 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1014 UINT32 *numpad)
1016 ACImpl *This = impl_from_IAudioClient(iface);
1017 audio_buf_info bi;
1019 TRACE("(%p)->(%p)\n", This, numpad);
1021 if(!numpad)
1022 return E_POINTER;
1024 EnterCriticalSection(&This->lock);
1026 if(!This->initted){
1027 LeaveCriticalSection(&This->lock);
1028 return AUDCLNT_E_NOT_INITIALIZED;
1031 if(This->dataflow == eRender){
1032 if(ioctl(This->fd, SNDCTL_DSP_GETOSPACE, &bi) < 0){
1033 LeaveCriticalSection(&This->lock);
1034 WARN("GETOSPACE failed: %d (%s)\n", errno, strerror(errno));
1035 return E_FAIL;
1038 *numpad = (bi.fragstotal * bi.fragsize - bi.bytes) /
1039 This->fmt->nBlockAlign;
1041 /* when the OSS buffer has less than one fragment of data, including
1042 * no data, it often reports it as some non-zero portion of a
1043 * fragment. when it has more than one fragment of data, it reports
1044 * it as some multiple of that portion of the fragment size.
1046 * so, we have to do some ugly workarounds to report the timing
1047 * as accurately as possible */
1048 if(*numpad < bi.fragsize / This->fmt->nBlockAlign){
1049 *numpad = This->inbuf_frames;
1050 This->inbuf_frames = 0;
1051 }else{
1052 if(*numpad < This->inbuf_frames)
1053 This->inbuf_frames = *numpad;
1054 else
1055 *numpad = This->inbuf_frames;
1057 }else if(This->dataflow == eCapture){
1058 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
1059 LeaveCriticalSection(&This->lock);
1060 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
1061 return E_FAIL;
1064 if(bi.bytes <= bi.fragsize)
1065 *numpad = 0;
1066 else
1067 *numpad = bi.bytes / This->fmt->nBlockAlign;
1068 }else{
1069 LeaveCriticalSection(&This->lock);
1070 return E_UNEXPECTED;
1073 *numpad += This->held_frames;
1075 LeaveCriticalSection(&This->lock);
1077 return S_OK;
1080 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1081 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1082 WAVEFORMATEX **outpwfx)
1084 ACImpl *This = impl_from_IAudioClient(iface);
1085 HRESULT ret;
1087 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1089 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1090 return E_POINTER;
1092 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1093 return E_INVALIDARG;
1095 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1096 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1097 return E_INVALIDARG;
1099 dump_fmt(pwfx);
1101 EnterCriticalSection(&This->lock);
1103 ret = setup_oss_device(This, pwfx, outpwfx, TRUE);
1105 LeaveCriticalSection(&This->lock);
1107 return ret;
1110 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1111 WAVEFORMATEX **pwfx)
1113 ACImpl *This = impl_from_IAudioClient(iface);
1114 WAVEFORMATEXTENSIBLE *fmt;
1115 int formats;
1117 TRACE("(%p)->(%p)\n", This, pwfx);
1119 if(!pwfx)
1120 return E_POINTER;
1121 *pwfx = NULL;
1123 if(This->dataflow == eRender)
1124 formats = This->ai.oformats;
1125 else if(This->dataflow == eCapture)
1126 formats = This->ai.iformats;
1127 else
1128 return E_UNEXPECTED;
1130 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1131 if(!fmt)
1132 return E_OUTOFMEMORY;
1134 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1135 if(formats & AFMT_S16_LE){
1136 fmt->Format.wBitsPerSample = 16;
1137 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1138 #ifdef AFMT_FLOAT
1139 }else if(formats & AFMT_FLOAT){
1140 fmt->Format.wBitsPerSample = 32;
1141 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1142 #endif
1143 }else if(formats & AFMT_U8){
1144 fmt->Format.wBitsPerSample = 8;
1145 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1146 }else if(formats & AFMT_S32_LE){
1147 fmt->Format.wBitsPerSample = 32;
1148 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1149 }else if(formats & AFMT_S24_LE){
1150 fmt->Format.wBitsPerSample = 24;
1151 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1152 }else{
1153 ERR("Didn't recognize any available OSS formats: %x\n", formats);
1154 CoTaskMemFree(fmt);
1155 return E_FAIL;
1158 fmt->Format.nChannels = This->ai.max_channels;
1159 fmt->Format.nSamplesPerSec = This->ai.max_rate;
1160 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1162 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1163 fmt->Format.nChannels) / 8;
1164 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1165 fmt->Format.nBlockAlign;
1167 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1168 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1170 *pwfx = (WAVEFORMATEX*)fmt;
1171 dump_fmt(*pwfx);
1173 return S_OK;
1176 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1177 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1179 ACImpl *This = impl_from_IAudioClient(iface);
1181 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1183 if(!defperiod && !minperiod)
1184 return E_POINTER;
1186 EnterCriticalSection(&This->lock);
1188 if(defperiod)
1189 *defperiod = DefaultPeriod;
1190 if(minperiod)
1191 *minperiod = MinimumPeriod;
1193 LeaveCriticalSection(&This->lock);
1195 return S_OK;
1198 static void oss_silence_buffer(ACImpl *This, BYTE *buf, UINT32 frames)
1200 if(This->fmt->wBitsPerSample == 8)
1201 memset(buf, 128, frames * This->fmt->nBlockAlign);
1202 else
1203 memset(buf, 0, frames * This->fmt->nBlockAlign);
1206 static void oss_write_data(ACImpl *This)
1208 ssize_t written;
1209 UINT32 written_frames;
1210 size_t to_write;
1211 BYTE *buf =
1212 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1214 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1215 to_write = This->bufsize_frames - This->lcl_offs_frames;
1216 else
1217 to_write = This->held_frames;
1219 if(This->session->mute)
1220 oss_silence_buffer(This, buf, to_write);
1222 written = write(This->fd, buf, to_write * This->fmt->nBlockAlign);
1223 if(written < 0){
1224 /* EAGAIN is OSS buffer full, log that too */
1225 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1226 return;
1228 written_frames = written / This->fmt->nBlockAlign;
1230 This->lcl_offs_frames += written_frames;
1231 This->lcl_offs_frames %= This->bufsize_frames;
1232 This->held_frames -= written_frames;
1233 This->inbuf_frames += written_frames;
1235 if(written_frames < to_write){
1236 /* OSS buffer probably full */
1237 return;
1240 if(This->held_frames){
1241 /* wrapped and have some data back at the start to write */
1243 if(This->session->mute)
1244 oss_silence_buffer(This, This->local_buffer, This->held_frames);
1246 written = write(This->fd, This->local_buffer,
1247 This->held_frames * This->fmt->nBlockAlign);
1248 if(written < 0){
1249 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1250 return;
1252 written_frames = written / This->fmt->nBlockAlign;
1254 This->lcl_offs_frames += written_frames;
1255 This->lcl_offs_frames %= This->bufsize_frames;
1256 This->held_frames -= written_frames;
1257 This->inbuf_frames += written_frames;
1261 static void oss_read_data(ACImpl *This)
1263 UINT64 pos, readable;
1264 audio_buf_info bi;
1265 ssize_t nread;
1267 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
1268 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
1269 return;
1272 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1273 readable = (This->bufsize_frames - pos) * This->fmt->nBlockAlign;
1275 if(bi.bytes < readable)
1276 readable = bi.bytes;
1278 nread = read(This->fd, This->local_buffer + pos * This->fmt->nBlockAlign,
1279 readable);
1280 if(nread < 0){
1281 WARN("read failed: %d (%s)\n", errno, strerror(errno));
1282 return;
1285 This->held_frames += nread / This->fmt->nBlockAlign;
1287 if(This->held_frames > This->bufsize_frames){
1288 WARN("Overflow of unread data\n");
1289 This->lcl_offs_frames += This->held_frames;
1290 This->lcl_offs_frames %= This->bufsize_frames;
1291 This->held_frames = This->bufsize_frames;
1295 static void CALLBACK oss_period_callback(void *user, BOOLEAN timer)
1297 ACImpl *This = user;
1299 EnterCriticalSection(&This->lock);
1301 if(This->dataflow == eRender && This->held_frames)
1302 oss_write_data(This);
1303 else if(This->dataflow == eCapture)
1304 oss_read_data(This);
1306 if(This->event)
1307 SetEvent(This->event);
1309 LeaveCriticalSection(&This->lock);
1312 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1314 ACImpl *This = impl_from_IAudioClient(iface);
1315 int mask;
1317 TRACE("(%p)\n", This);
1319 EnterCriticalSection(&This->lock);
1321 if(!This->initted){
1322 LeaveCriticalSection(&This->lock);
1323 return AUDCLNT_E_NOT_INITIALIZED;
1326 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1327 LeaveCriticalSection(&This->lock);
1328 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1331 if(This->playing){
1332 LeaveCriticalSection(&This->lock);
1333 return AUDCLNT_E_NOT_STOPPED;
1336 if(This->dataflow == eRender)
1337 mask = PCM_ENABLE_OUTPUT;
1338 else if(This->dataflow == eCapture)
1339 mask = PCM_ENABLE_INPUT;
1340 else{
1341 LeaveCriticalSection(&This->lock);
1342 return E_UNEXPECTED;
1345 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
1346 LeaveCriticalSection(&This->lock);
1347 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
1348 return E_FAIL;
1351 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1352 oss_period_callback, This, 0, This->period_us / 1000,
1353 WT_EXECUTEINTIMERTHREAD))
1354 ERR("Unable to create period timer: %u\n", GetLastError());
1356 This->playing = TRUE;
1358 LeaveCriticalSection(&This->lock);
1360 return S_OK;
1363 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1365 ACImpl *This = impl_from_IAudioClient(iface);
1366 int mask;
1368 TRACE("(%p)\n", This);
1370 EnterCriticalSection(&This->lock);
1372 if(!This->initted){
1373 LeaveCriticalSection(&This->lock);
1374 return AUDCLNT_E_NOT_INITIALIZED;
1377 if(!This->playing){
1378 LeaveCriticalSection(&This->lock);
1379 return S_FALSE;
1382 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1383 DeleteTimerQueueTimer(g_timer_q, This->timer,
1384 INVALID_HANDLE_VALUE);
1385 This->timer = NULL;
1388 if(ioctl(This->fd, SNDCTL_DSP_HALT, NULL) < 0){
1389 LeaveCriticalSection(&This->lock);
1390 WARN("HALT failed: %d (%s)\n", errno, strerror(errno));
1391 return E_FAIL;
1394 mask = 0;
1395 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
1396 LeaveCriticalSection(&This->lock);
1397 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
1398 return E_FAIL;
1401 This->playing = FALSE;
1403 LeaveCriticalSection(&This->lock);
1405 return S_OK;
1408 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1410 ACImpl *This = impl_from_IAudioClient(iface);
1412 TRACE("(%p)\n", This);
1414 EnterCriticalSection(&This->lock);
1416 if(!This->initted){
1417 LeaveCriticalSection(&This->lock);
1418 return AUDCLNT_E_NOT_INITIALIZED;
1421 if(This->playing){
1422 LeaveCriticalSection(&This->lock);
1423 return AUDCLNT_E_NOT_STOPPED;
1426 if(This->buf_state != NOT_LOCKED){
1427 LeaveCriticalSection(&This->lock);
1428 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1431 This->written_frames = 0;
1432 This->inbuf_frames = 0;
1433 This->held_frames = 0;
1435 if(ioctl(This->fd, SNDCTL_DSP_SKIP, NULL) < 0)
1436 WARN("SKIP failed: %d (%s)\n", errno, strerror(errno));
1438 LeaveCriticalSection(&This->lock);
1440 return S_OK;
1443 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1444 HANDLE event)
1446 ACImpl *This = impl_from_IAudioClient(iface);
1448 TRACE("(%p)->(%p)\n", This, event);
1450 if(!event)
1451 return E_INVALIDARG;
1453 EnterCriticalSection(&This->lock);
1455 if(!This->initted){
1456 LeaveCriticalSection(&This->lock);
1457 return AUDCLNT_E_NOT_INITIALIZED;
1460 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1461 LeaveCriticalSection(&This->lock);
1462 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1465 This->event = event;
1467 LeaveCriticalSection(&This->lock);
1469 return S_OK;
1472 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1473 void **ppv)
1475 ACImpl *This = impl_from_IAudioClient(iface);
1477 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1479 if(!ppv)
1480 return E_POINTER;
1481 *ppv = NULL;
1483 EnterCriticalSection(&This->lock);
1485 if(!This->initted){
1486 LeaveCriticalSection(&This->lock);
1487 return AUDCLNT_E_NOT_INITIALIZED;
1490 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1491 if(This->dataflow != eRender){
1492 LeaveCriticalSection(&This->lock);
1493 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1495 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1496 *ppv = &This->IAudioRenderClient_iface;
1497 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1498 if(This->dataflow != eCapture){
1499 LeaveCriticalSection(&This->lock);
1500 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1502 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1503 *ppv = &This->IAudioCaptureClient_iface;
1504 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1505 IAudioClock_AddRef(&This->IAudioClock_iface);
1506 *ppv = &This->IAudioClock_iface;
1507 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1508 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1509 *ppv = &This->IAudioStreamVolume_iface;
1510 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1511 if(!This->session_wrapper){
1512 This->session_wrapper = AudioSessionWrapper_Create(This);
1513 if(!This->session_wrapper){
1514 LeaveCriticalSection(&This->lock);
1515 return E_OUTOFMEMORY;
1517 }else
1518 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1520 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1521 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1522 if(!This->session_wrapper){
1523 This->session_wrapper = AudioSessionWrapper_Create(This);
1524 if(!This->session_wrapper){
1525 LeaveCriticalSection(&This->lock);
1526 return E_OUTOFMEMORY;
1528 }else
1529 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1531 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1532 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1533 if(!This->session_wrapper){
1534 This->session_wrapper = AudioSessionWrapper_Create(This);
1535 if(!This->session_wrapper){
1536 LeaveCriticalSection(&This->lock);
1537 return E_OUTOFMEMORY;
1539 }else
1540 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1542 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1545 if(*ppv){
1546 LeaveCriticalSection(&This->lock);
1547 return S_OK;
1550 LeaveCriticalSection(&This->lock);
1552 FIXME("stub %s\n", debugstr_guid(riid));
1553 return E_NOINTERFACE;
1556 static const IAudioClientVtbl AudioClient_Vtbl =
1558 AudioClient_QueryInterface,
1559 AudioClient_AddRef,
1560 AudioClient_Release,
1561 AudioClient_Initialize,
1562 AudioClient_GetBufferSize,
1563 AudioClient_GetStreamLatency,
1564 AudioClient_GetCurrentPadding,
1565 AudioClient_IsFormatSupported,
1566 AudioClient_GetMixFormat,
1567 AudioClient_GetDevicePeriod,
1568 AudioClient_Start,
1569 AudioClient_Stop,
1570 AudioClient_Reset,
1571 AudioClient_SetEventHandle,
1572 AudioClient_GetService
1575 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1576 IAudioRenderClient *iface, REFIID riid, void **ppv)
1578 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1580 if(!ppv)
1581 return E_POINTER;
1582 *ppv = NULL;
1584 if(IsEqualIID(riid, &IID_IUnknown) ||
1585 IsEqualIID(riid, &IID_IAudioRenderClient))
1586 *ppv = iface;
1587 if(*ppv){
1588 IUnknown_AddRef((IUnknown*)*ppv);
1589 return S_OK;
1592 WARN("Unknown interface %s\n", debugstr_guid(riid));
1593 return E_NOINTERFACE;
1596 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1598 ACImpl *This = impl_from_IAudioRenderClient(iface);
1599 return AudioClient_AddRef(&This->IAudioClient_iface);
1602 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1604 ACImpl *This = impl_from_IAudioRenderClient(iface);
1605 return AudioClient_Release(&This->IAudioClient_iface);
1608 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1609 UINT32 frames, BYTE **data)
1611 ACImpl *This = impl_from_IAudioRenderClient(iface);
1612 UINT32 pad, write_pos;
1613 HRESULT hr;
1615 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1617 if(!data)
1618 return E_POINTER;
1620 EnterCriticalSection(&This->lock);
1622 if(This->buf_state != NOT_LOCKED){
1623 LeaveCriticalSection(&This->lock);
1624 return AUDCLNT_E_OUT_OF_ORDER;
1627 if(!frames){
1628 This->buf_state = LOCKED_NORMAL;
1629 LeaveCriticalSection(&This->lock);
1630 return S_OK;
1633 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1634 if(FAILED(hr)){
1635 LeaveCriticalSection(&This->lock);
1636 return hr;
1639 if(pad + frames > This->bufsize_frames){
1640 LeaveCriticalSection(&This->lock);
1641 return AUDCLNT_E_BUFFER_TOO_LARGE;
1644 write_pos =
1645 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1646 if(write_pos + frames > This->bufsize_frames){
1647 if(This->tmp_buffer_frames < frames){
1648 if(This->tmp_buffer)
1649 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1650 This->tmp_buffer, frames * This->fmt->nBlockAlign);
1651 else
1652 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1653 frames * This->fmt->nBlockAlign);
1654 if(!This->tmp_buffer){
1655 LeaveCriticalSection(&This->lock);
1656 return E_OUTOFMEMORY;
1658 This->tmp_buffer_frames = frames;
1660 *data = This->tmp_buffer;
1661 This->buf_state = LOCKED_WRAPPED;
1662 }else{
1663 *data = This->local_buffer + write_pos * This->fmt->nBlockAlign;
1664 This->buf_state = LOCKED_NORMAL;
1667 LeaveCriticalSection(&This->lock);
1669 return S_OK;
1672 static void oss_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_frames)
1674 UINT32 write_offs_frames =
1675 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1676 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
1677 UINT32 chunk_frames = This->bufsize_frames - write_offs_frames;
1678 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
1679 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
1681 if(written_bytes <= chunk_bytes){
1682 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
1683 }else{
1684 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
1685 memcpy(This->local_buffer, buffer + chunk_bytes,
1686 written_bytes - chunk_bytes);
1690 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1691 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
1693 ACImpl *This = impl_from_IAudioRenderClient(iface);
1694 BYTE *buffer;
1696 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
1698 EnterCriticalSection(&This->lock);
1700 if(This->buf_state == NOT_LOCKED || !written_frames){
1701 This->buf_state = NOT_LOCKED;
1702 LeaveCriticalSection(&This->lock);
1703 return written_frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK;
1706 if(This->buf_state == LOCKED_NORMAL)
1707 buffer = This->local_buffer + This->fmt->nBlockAlign *
1708 ((This->lcl_offs_frames + This->held_frames) % This->bufsize_frames);
1709 else
1710 buffer = This->tmp_buffer;
1712 if(flags & AUDCLNT_BUFFERFLAGS_SILENT)
1713 oss_silence_buffer(This, buffer, written_frames);
1715 if(This->held_frames){
1716 if(This->buf_state == LOCKED_WRAPPED)
1717 oss_wrap_buffer(This, buffer, written_frames);
1719 This->held_frames += written_frames;
1720 }else{
1721 ssize_t w_bytes;
1722 UINT32 w_frames;
1724 if(This->session->mute)
1725 oss_silence_buffer(This, buffer, written_frames);
1727 w_bytes = write(This->fd, buffer,
1728 written_frames * This->fmt->nBlockAlign);
1729 if(w_bytes < 0){
1730 if(errno != EAGAIN){
1731 This->buf_state = NOT_LOCKED;
1732 LeaveCriticalSection(&This->lock);
1733 ERR("write failed: %d (%s)\n", errno, strerror(errno));
1734 return E_FAIL;
1735 }else /* OSS buffer full */
1736 w_bytes = 0;
1738 w_frames = w_bytes / This->fmt->nBlockAlign;
1739 This->inbuf_frames += w_frames;
1741 if(w_frames < written_frames){
1742 if(This->buf_state == LOCKED_WRAPPED)
1743 oss_wrap_buffer(This, This->tmp_buffer + w_bytes,
1744 written_frames - w_frames);
1745 else
1746 This->lcl_offs_frames += w_frames;
1747 This->held_frames = written_frames - w_frames;
1751 This->written_frames += written_frames;
1752 This->buf_state = NOT_LOCKED;
1754 LeaveCriticalSection(&This->lock);
1756 return S_OK;
1759 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1760 AudioRenderClient_QueryInterface,
1761 AudioRenderClient_AddRef,
1762 AudioRenderClient_Release,
1763 AudioRenderClient_GetBuffer,
1764 AudioRenderClient_ReleaseBuffer
1767 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1768 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1770 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1772 if(!ppv)
1773 return E_POINTER;
1774 *ppv = NULL;
1776 if(IsEqualIID(riid, &IID_IUnknown) ||
1777 IsEqualIID(riid, &IID_IAudioCaptureClient))
1778 *ppv = iface;
1779 if(*ppv){
1780 IUnknown_AddRef((IUnknown*)*ppv);
1781 return S_OK;
1784 WARN("Unknown interface %s\n", debugstr_guid(riid));
1785 return E_NOINTERFACE;
1788 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1790 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1791 return IAudioClient_AddRef(&This->IAudioClient_iface);
1794 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1796 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1797 return IAudioClient_Release(&This->IAudioClient_iface);
1800 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1801 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1802 UINT64 *qpcpos)
1804 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1805 HRESULT hr;
1807 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1808 devpos, qpcpos);
1810 if(!data || !frames || !flags)
1811 return E_POINTER;
1813 EnterCriticalSection(&This->lock);
1815 if(This->buf_state != NOT_LOCKED){
1816 LeaveCriticalSection(&This->lock);
1817 return AUDCLNT_E_OUT_OF_ORDER;
1820 hr = IAudioCaptureClient_GetNextPacketSize(iface, frames);
1821 if(FAILED(hr)){
1822 LeaveCriticalSection(&This->lock);
1823 return hr;
1826 *flags = 0;
1828 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
1829 UINT32 chunk_bytes, offs_bytes, frames_bytes;
1830 if(This->tmp_buffer_frames < *frames){
1831 if(This->tmp_buffer)
1832 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1833 This->tmp_buffer, *frames * This->fmt->nBlockAlign);
1834 else
1835 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1836 *frames * This->fmt->nBlockAlign);
1837 if(!This->tmp_buffer){
1838 LeaveCriticalSection(&This->lock);
1839 return E_OUTOFMEMORY;
1841 This->tmp_buffer_frames = *frames;
1844 *data = This->tmp_buffer;
1845 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
1846 This->fmt->nBlockAlign;
1847 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
1848 frames_bytes = *frames * This->fmt->nBlockAlign;
1849 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
1850 memcpy(This->tmp_buffer, This->local_buffer,
1851 frames_bytes - chunk_bytes);
1852 }else
1853 *data = This->local_buffer +
1854 This->lcl_offs_frames * This->fmt->nBlockAlign;
1856 This->buf_state = LOCKED_NORMAL;
1858 if(devpos || qpcpos)
1859 IAudioClock_GetPosition(&This->IAudioClock_iface, devpos, qpcpos);
1861 LeaveCriticalSection(&This->lock);
1863 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1866 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1867 IAudioCaptureClient *iface, UINT32 done)
1869 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1871 TRACE("(%p)->(%u)\n", This, done);
1873 EnterCriticalSection(&This->lock);
1875 if(This->buf_state == NOT_LOCKED){
1876 LeaveCriticalSection(&This->lock);
1877 return AUDCLNT_E_OUT_OF_ORDER;
1880 This->held_frames -= done;
1881 This->lcl_offs_frames += done;
1882 This->lcl_offs_frames %= This->bufsize_frames;
1884 This->buf_state = NOT_LOCKED;
1886 LeaveCriticalSection(&This->lock);
1888 return S_OK;
1891 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1892 IAudioCaptureClient *iface, UINT32 *frames)
1894 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1896 TRACE("(%p)->(%p)\n", This, frames);
1898 return AudioClient_GetCurrentPadding(&This->IAudioClient_iface, frames);
1901 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1903 AudioCaptureClient_QueryInterface,
1904 AudioCaptureClient_AddRef,
1905 AudioCaptureClient_Release,
1906 AudioCaptureClient_GetBuffer,
1907 AudioCaptureClient_ReleaseBuffer,
1908 AudioCaptureClient_GetNextPacketSize
1911 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
1912 REFIID riid, void **ppv)
1914 ACImpl *This = impl_from_IAudioClock(iface);
1916 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1918 if(!ppv)
1919 return E_POINTER;
1920 *ppv = NULL;
1922 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
1923 *ppv = iface;
1924 else if(IsEqualIID(riid, &IID_IAudioClock2))
1925 *ppv = &This->IAudioClock2_iface;
1926 if(*ppv){
1927 IUnknown_AddRef((IUnknown*)*ppv);
1928 return S_OK;
1931 WARN("Unknown interface %s\n", debugstr_guid(riid));
1932 return E_NOINTERFACE;
1935 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
1937 ACImpl *This = impl_from_IAudioClock(iface);
1938 return IAudioClient_AddRef(&This->IAudioClient_iface);
1941 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
1943 ACImpl *This = impl_from_IAudioClock(iface);
1944 return IAudioClient_Release(&This->IAudioClient_iface);
1947 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
1949 ACImpl *This = impl_from_IAudioClock(iface);
1951 TRACE("(%p)->(%p)\n", This, freq);
1953 *freq = This->fmt->nSamplesPerSec;
1955 return S_OK;
1958 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
1959 UINT64 *qpctime)
1961 ACImpl *This = impl_from_IAudioClock(iface);
1962 UINT32 pad;
1963 HRESULT hr;
1965 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
1967 if(!pos)
1968 return E_POINTER;
1970 EnterCriticalSection(&This->lock);
1972 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1973 if(FAILED(hr)){
1974 LeaveCriticalSection(&This->lock);
1975 return hr;
1978 if(This->dataflow == eRender)
1979 *pos = This->written_frames - pad;
1980 else if(This->dataflow == eCapture)
1981 *pos = This->written_frames + pad;
1983 LeaveCriticalSection(&This->lock);
1985 if(qpctime){
1986 LARGE_INTEGER stamp, freq;
1987 QueryPerformanceCounter(&stamp);
1988 QueryPerformanceFrequency(&freq);
1989 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
1992 return S_OK;
1995 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
1996 DWORD *chars)
1998 ACImpl *This = impl_from_IAudioClock(iface);
2000 TRACE("(%p)->(%p)\n", This, chars);
2002 if(!chars)
2003 return E_POINTER;
2005 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2007 return S_OK;
2010 static const IAudioClockVtbl AudioClock_Vtbl =
2012 AudioClock_QueryInterface,
2013 AudioClock_AddRef,
2014 AudioClock_Release,
2015 AudioClock_GetFrequency,
2016 AudioClock_GetPosition,
2017 AudioClock_GetCharacteristics
2020 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2021 REFIID riid, void **ppv)
2023 ACImpl *This = impl_from_IAudioClock2(iface);
2024 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2027 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2029 ACImpl *This = impl_from_IAudioClock2(iface);
2030 return IAudioClient_AddRef(&This->IAudioClient_iface);
2033 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2035 ACImpl *This = impl_from_IAudioClock2(iface);
2036 return IAudioClient_Release(&This->IAudioClient_iface);
2039 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2040 UINT64 *pos, UINT64 *qpctime)
2042 ACImpl *This = impl_from_IAudioClock2(iface);
2044 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2046 return E_NOTIMPL;
2049 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2051 AudioClock2_QueryInterface,
2052 AudioClock2_AddRef,
2053 AudioClock2_Release,
2054 AudioClock2_GetDevicePosition
2057 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2059 AudioSessionWrapper *ret;
2061 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2062 sizeof(AudioSessionWrapper));
2063 if(!ret)
2064 return NULL;
2066 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2067 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2068 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2070 ret->ref = 1;
2072 ret->client = client;
2073 if(client){
2074 ret->session = client->session;
2075 AudioClient_AddRef(&client->IAudioClient_iface);
2078 return ret;
2081 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2082 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2084 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2086 if(!ppv)
2087 return E_POINTER;
2088 *ppv = NULL;
2090 if(IsEqualIID(riid, &IID_IUnknown) ||
2091 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2092 IsEqualIID(riid, &IID_IAudioSessionControl2))
2093 *ppv = iface;
2094 if(*ppv){
2095 IUnknown_AddRef((IUnknown*)*ppv);
2096 return S_OK;
2099 WARN("Unknown interface %s\n", debugstr_guid(riid));
2100 return E_NOINTERFACE;
2103 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2105 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2106 ULONG ref;
2107 ref = InterlockedIncrement(&This->ref);
2108 TRACE("(%p) Refcount now %u\n", This, ref);
2109 return ref;
2112 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2114 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2115 ULONG ref;
2116 ref = InterlockedDecrement(&This->ref);
2117 TRACE("(%p) Refcount now %u\n", This, ref);
2118 if(!ref){
2119 if(This->client){
2120 EnterCriticalSection(&This->client->lock);
2121 This->client->session_wrapper = NULL;
2122 LeaveCriticalSection(&This->client->lock);
2123 AudioClient_Release(&This->client->IAudioClient_iface);
2125 HeapFree(GetProcessHeap(), 0, This);
2127 return ref;
2130 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2131 AudioSessionState *state)
2133 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2134 ACImpl *client;
2136 TRACE("(%p)->(%p)\n", This, state);
2138 if(!state)
2139 return NULL_PTR_ERR;
2141 EnterCriticalSection(&g_sessions_lock);
2143 if(list_empty(&This->session->clients)){
2144 *state = AudioSessionStateExpired;
2145 LeaveCriticalSection(&g_sessions_lock);
2146 return S_OK;
2149 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2150 EnterCriticalSection(&client->lock);
2151 if(client->playing){
2152 *state = AudioSessionStateActive;
2153 LeaveCriticalSection(&client->lock);
2154 LeaveCriticalSection(&g_sessions_lock);
2155 return S_OK;
2157 LeaveCriticalSection(&client->lock);
2160 LeaveCriticalSection(&g_sessions_lock);
2162 *state = AudioSessionStateInactive;
2164 return S_OK;
2167 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2168 IAudioSessionControl2 *iface, WCHAR **name)
2170 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2172 FIXME("(%p)->(%p) - stub\n", This, name);
2174 return E_NOTIMPL;
2177 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2178 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2180 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2182 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2184 return E_NOTIMPL;
2187 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2188 IAudioSessionControl2 *iface, WCHAR **path)
2190 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2192 FIXME("(%p)->(%p) - stub\n", This, path);
2194 return E_NOTIMPL;
2197 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2198 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2200 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2202 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2204 return E_NOTIMPL;
2207 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2208 IAudioSessionControl2 *iface, GUID *group)
2210 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2212 FIXME("(%p)->(%p) - stub\n", This, group);
2214 return E_NOTIMPL;
2217 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2218 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2220 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2222 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2223 debugstr_guid(session));
2225 return E_NOTIMPL;
2228 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2229 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2231 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2233 FIXME("(%p)->(%p) - stub\n", This, events);
2235 return S_OK;
2238 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2239 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2241 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2243 FIXME("(%p)->(%p) - stub\n", This, events);
2245 return S_OK;
2248 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2249 IAudioSessionControl2 *iface, WCHAR **id)
2251 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2253 FIXME("(%p)->(%p) - stub\n", This, id);
2255 return E_NOTIMPL;
2258 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2259 IAudioSessionControl2 *iface, WCHAR **id)
2261 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2263 FIXME("(%p)->(%p) - stub\n", This, id);
2265 return E_NOTIMPL;
2268 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2269 IAudioSessionControl2 *iface, DWORD *pid)
2271 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2273 TRACE("(%p)->(%p)\n", This, pid);
2275 if(!pid)
2276 return E_POINTER;
2278 *pid = GetCurrentProcessId();
2280 return S_OK;
2283 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2284 IAudioSessionControl2 *iface)
2286 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2288 TRACE("(%p)\n", This);
2290 return S_FALSE;
2293 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2294 IAudioSessionControl2 *iface, BOOL optout)
2296 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2298 TRACE("(%p)->(%d)\n", This, optout);
2300 return S_OK;
2303 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2305 AudioSessionControl_QueryInterface,
2306 AudioSessionControl_AddRef,
2307 AudioSessionControl_Release,
2308 AudioSessionControl_GetState,
2309 AudioSessionControl_GetDisplayName,
2310 AudioSessionControl_SetDisplayName,
2311 AudioSessionControl_GetIconPath,
2312 AudioSessionControl_SetIconPath,
2313 AudioSessionControl_GetGroupingParam,
2314 AudioSessionControl_SetGroupingParam,
2315 AudioSessionControl_RegisterAudioSessionNotification,
2316 AudioSessionControl_UnregisterAudioSessionNotification,
2317 AudioSessionControl_GetSessionIdentifier,
2318 AudioSessionControl_GetSessionInstanceIdentifier,
2319 AudioSessionControl_GetProcessId,
2320 AudioSessionControl_IsSystemSoundsSession,
2321 AudioSessionControl_SetDuckingPreference
2324 /* index == -1 means set all channels, otherwise sets only the given channel */
2325 static HRESULT oss_setvol(ACImpl *This, UINT32 index)
2327 int setreq, getreq;
2328 unsigned int vol;
2329 unsigned short l;
2330 float level;
2332 if(index == (UINT32)-1){
2333 HRESULT ret = S_OK;
2334 UINT32 i;
2335 for(i = 0; i < This->fmt->nChannels; ++i){
2336 HRESULT hr;
2337 hr = oss_setvol(This, i);
2338 if(FAILED(hr))
2339 ret = hr;
2341 return ret;
2344 if(index > 1)
2345 /* OSS doesn't support volume control past the first two channels */
2346 return S_OK;
2348 if(This->dataflow == eRender){
2349 setreq = SNDCTL_DSP_SETPLAYVOL;
2350 getreq = SNDCTL_DSP_GETPLAYVOL;
2351 }else if(This->dataflow == eCapture){
2352 setreq = SNDCTL_DSP_SETRECVOL;
2353 getreq = SNDCTL_DSP_GETRECVOL;
2354 }else
2355 return E_UNEXPECTED;
2357 if(ioctl(This->fd, getreq, &vol) < 0){
2358 if(errno == EINVAL)
2359 /* device doesn't support this call */
2360 return S_OK;
2362 WARN("GET[REC|PLAY]VOL failed: %d (%s)\n", errno, strerror(errno));
2363 return E_FAIL;
2366 level = This->session->master_vol * This->session->channel_vols[index] *
2367 This->vols[index];
2368 l = level * 100;
2369 if(index == 0)
2370 vol = l | (vol & 0xFF00);
2371 else
2372 vol = (vol & 0xFF) | (l << 8);
2374 if(ioctl(This->fd, setreq, &vol) < 0){
2375 if(errno == EINVAL)
2376 /* device doesn't support this call */
2377 return S_OK;
2379 WARN("SET[REC|PLAY]VOL failed: %d (%s)\n", errno, strerror(errno));
2380 return E_FAIL;
2383 return S_OK;
2386 static HRESULT oss_session_setvol(AudioSession *session, UINT32 index)
2388 HRESULT ret = S_OK;
2389 ACImpl *client;
2391 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2392 HRESULT hr;
2393 hr = oss_setvol(client, index);
2394 if(FAILED(hr))
2395 ret = hr;
2398 return ret;
2401 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2402 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2404 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2406 if(!ppv)
2407 return E_POINTER;
2408 *ppv = NULL;
2410 if(IsEqualIID(riid, &IID_IUnknown) ||
2411 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2412 *ppv = iface;
2413 if(*ppv){
2414 IUnknown_AddRef((IUnknown*)*ppv);
2415 return S_OK;
2418 WARN("Unknown interface %s\n", debugstr_guid(riid));
2419 return E_NOINTERFACE;
2422 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2424 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2425 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2428 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2430 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2431 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2434 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2435 ISimpleAudioVolume *iface, float level, const GUID *context)
2437 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2438 AudioSession *session = This->session;
2439 HRESULT ret;
2441 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2443 if(level < 0.f || level > 1.f)
2444 return E_INVALIDARG;
2446 if(context)
2447 FIXME("Notifications not supported yet\n");
2449 EnterCriticalSection(&session->lock);
2451 session->master_vol = level;
2453 ret = oss_session_setvol(session, -1);
2455 LeaveCriticalSection(&session->lock);
2457 return ret;
2460 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2461 ISimpleAudioVolume *iface, float *level)
2463 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2464 AudioSession *session = This->session;
2466 TRACE("(%p)->(%p)\n", session, level);
2468 if(!level)
2469 return NULL_PTR_ERR;
2471 *level = session->master_vol;
2473 return S_OK;
2476 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2477 BOOL mute, const GUID *context)
2479 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2480 AudioSession *session = This->session;
2482 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2484 EnterCriticalSection(&session->lock);
2486 if(!mute && session->mute){
2487 ACImpl *client;
2489 session->mute = mute;
2491 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2492 EnterCriticalSection(&client->lock);
2493 if(ioctl(client->fd, SNDCTL_DSP_SKIP) < 0)
2494 WARN("Error calling DSP_SKIP: %d (%s)\n", errno,
2495 strerror(errno));
2496 oss_write_data(client);
2497 LeaveCriticalSection(&client->lock);
2499 }else
2500 session->mute = mute;
2502 LeaveCriticalSection(&session->lock);
2504 return S_OK;
2507 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2508 BOOL *mute)
2510 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2511 AudioSession *session = This->session;
2513 TRACE("(%p)->(%p)\n", session, mute);
2515 if(!mute)
2516 return NULL_PTR_ERR;
2518 *mute = This->session->mute;
2520 return S_OK;
2523 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2525 SimpleAudioVolume_QueryInterface,
2526 SimpleAudioVolume_AddRef,
2527 SimpleAudioVolume_Release,
2528 SimpleAudioVolume_SetMasterVolume,
2529 SimpleAudioVolume_GetMasterVolume,
2530 SimpleAudioVolume_SetMute,
2531 SimpleAudioVolume_GetMute
2534 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2535 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2537 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2539 if(!ppv)
2540 return E_POINTER;
2541 *ppv = NULL;
2543 if(IsEqualIID(riid, &IID_IUnknown) ||
2544 IsEqualIID(riid, &IID_IAudioStreamVolume))
2545 *ppv = iface;
2546 if(*ppv){
2547 IUnknown_AddRef((IUnknown*)*ppv);
2548 return S_OK;
2551 WARN("Unknown interface %s\n", debugstr_guid(riid));
2552 return E_NOINTERFACE;
2555 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2557 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2558 return IAudioClient_AddRef(&This->IAudioClient_iface);
2561 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2563 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2564 return IAudioClient_Release(&This->IAudioClient_iface);
2567 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2568 IAudioStreamVolume *iface, UINT32 *out)
2570 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2572 TRACE("(%p)->(%p)\n", This, out);
2574 if(!out)
2575 return E_POINTER;
2577 *out = This->fmt->nChannels;
2579 return S_OK;
2582 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2583 IAudioStreamVolume *iface, UINT32 index, float level)
2585 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2586 HRESULT ret;
2588 TRACE("(%p)->(%d, %f)\n", This, index, level);
2590 if(level < 0.f || level > 1.f)
2591 return E_INVALIDARG;
2593 if(index >= This->fmt->nChannels)
2594 return E_INVALIDARG;
2596 EnterCriticalSection(&This->lock);
2598 This->vols[index] = level;
2600 ret = oss_setvol(This, index);
2602 LeaveCriticalSection(&This->lock);
2604 return ret;
2607 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2608 IAudioStreamVolume *iface, UINT32 index, float *level)
2610 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2612 TRACE("(%p)->(%d, %p)\n", This, index, level);
2614 if(!level)
2615 return E_POINTER;
2617 if(index >= This->fmt->nChannels)
2618 return E_INVALIDARG;
2620 *level = This->vols[index];
2622 return S_OK;
2625 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2626 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2628 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2629 int i;
2630 HRESULT ret;
2632 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2634 if(!levels)
2635 return E_POINTER;
2637 if(count != This->fmt->nChannels)
2638 return E_INVALIDARG;
2640 EnterCriticalSection(&This->lock);
2642 for(i = 0; i < count; ++i)
2643 This->vols[i] = levels[i];
2645 ret = oss_setvol(This, -1);
2647 LeaveCriticalSection(&This->lock);
2649 return ret;
2652 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2653 IAudioStreamVolume *iface, UINT32 count, float *levels)
2655 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2656 int i;
2658 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2660 if(!levels)
2661 return E_POINTER;
2663 if(count != This->fmt->nChannels)
2664 return E_INVALIDARG;
2666 EnterCriticalSection(&This->lock);
2668 for(i = 0; i < count; ++i)
2669 levels[i] = This->vols[i];
2671 LeaveCriticalSection(&This->lock);
2673 return S_OK;
2676 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2678 AudioStreamVolume_QueryInterface,
2679 AudioStreamVolume_AddRef,
2680 AudioStreamVolume_Release,
2681 AudioStreamVolume_GetChannelCount,
2682 AudioStreamVolume_SetChannelVolume,
2683 AudioStreamVolume_GetChannelVolume,
2684 AudioStreamVolume_SetAllVolumes,
2685 AudioStreamVolume_GetAllVolumes
2688 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2689 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2691 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2693 if(!ppv)
2694 return E_POINTER;
2695 *ppv = NULL;
2697 if(IsEqualIID(riid, &IID_IUnknown) ||
2698 IsEqualIID(riid, &IID_IChannelAudioVolume))
2699 *ppv = iface;
2700 if(*ppv){
2701 IUnknown_AddRef((IUnknown*)*ppv);
2702 return S_OK;
2705 WARN("Unknown interface %s\n", debugstr_guid(riid));
2706 return E_NOINTERFACE;
2709 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2711 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2712 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2715 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2717 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2718 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2721 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2722 IChannelAudioVolume *iface, UINT32 *out)
2724 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2725 AudioSession *session = This->session;
2727 TRACE("(%p)->(%p)\n", session, out);
2729 if(!out)
2730 return NULL_PTR_ERR;
2732 *out = session->channel_count;
2734 return S_OK;
2737 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2738 IChannelAudioVolume *iface, UINT32 index, float level,
2739 const GUID *context)
2741 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2742 AudioSession *session = This->session;
2743 HRESULT ret;
2745 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2746 wine_dbgstr_guid(context));
2748 if(level < 0.f || level > 1.f)
2749 return E_INVALIDARG;
2751 if(index >= session->channel_count)
2752 return E_INVALIDARG;
2754 if(context)
2755 FIXME("Notifications not supported yet\n");
2757 EnterCriticalSection(&session->lock);
2759 session->channel_vols[index] = level;
2761 ret = oss_session_setvol(session, index);
2763 LeaveCriticalSection(&session->lock);
2765 return ret;
2768 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2769 IChannelAudioVolume *iface, UINT32 index, float *level)
2771 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2772 AudioSession *session = This->session;
2774 TRACE("(%p)->(%d, %p)\n", session, index, level);
2776 if(!level)
2777 return NULL_PTR_ERR;
2779 if(index >= session->channel_count)
2780 return E_INVALIDARG;
2782 *level = session->channel_vols[index];
2784 return S_OK;
2787 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2788 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2789 const GUID *context)
2791 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2792 AudioSession *session = This->session;
2793 int i;
2794 HRESULT ret;
2796 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2797 wine_dbgstr_guid(context));
2799 if(!levels)
2800 return NULL_PTR_ERR;
2802 if(count != session->channel_count)
2803 return E_INVALIDARG;
2805 if(context)
2806 FIXME("Notifications not supported yet\n");
2808 EnterCriticalSection(&session->lock);
2810 for(i = 0; i < count; ++i)
2811 session->channel_vols[i] = levels[i];
2813 ret = oss_session_setvol(session, -1);
2815 LeaveCriticalSection(&session->lock);
2817 return ret;
2820 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2821 IChannelAudioVolume *iface, UINT32 count, float *levels)
2823 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2824 AudioSession *session = This->session;
2825 int i;
2827 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2829 if(!levels)
2830 return NULL_PTR_ERR;
2832 if(count != session->channel_count)
2833 return E_INVALIDARG;
2835 for(i = 0; i < count; ++i)
2836 levels[i] = session->channel_vols[i];
2838 return S_OK;
2841 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2843 ChannelAudioVolume_QueryInterface,
2844 ChannelAudioVolume_AddRef,
2845 ChannelAudioVolume_Release,
2846 ChannelAudioVolume_GetChannelCount,
2847 ChannelAudioVolume_SetChannelVolume,
2848 ChannelAudioVolume_GetChannelVolume,
2849 ChannelAudioVolume_SetAllVolumes,
2850 ChannelAudioVolume_GetAllVolumes
2853 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2854 REFIID riid, void **ppv)
2856 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2858 if(!ppv)
2859 return E_POINTER;
2860 *ppv = NULL;
2862 if(IsEqualIID(riid, &IID_IUnknown) ||
2863 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2864 IsEqualIID(riid, &IID_IAudioSessionManager2))
2865 *ppv = iface;
2866 if(*ppv){
2867 IUnknown_AddRef((IUnknown*)*ppv);
2868 return S_OK;
2871 WARN("Unknown interface %s\n", debugstr_guid(riid));
2872 return E_NOINTERFACE;
2875 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2877 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2878 ULONG ref;
2879 ref = InterlockedIncrement(&This->ref);
2880 TRACE("(%p) Refcount now %u\n", This, ref);
2881 return ref;
2884 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
2886 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2887 ULONG ref;
2888 ref = InterlockedDecrement(&This->ref);
2889 TRACE("(%p) Refcount now %u\n", This, ref);
2890 if(!ref)
2891 HeapFree(GetProcessHeap(), 0, This);
2892 return ref;
2895 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
2896 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2897 IAudioSessionControl **out)
2899 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2900 AudioSession *session;
2901 AudioSessionWrapper *wrapper;
2902 HRESULT hr;
2904 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2905 flags, out);
2907 hr = get_audio_session(session_guid, This->device, 0, &session);
2908 if(FAILED(hr))
2909 return hr;
2911 wrapper = AudioSessionWrapper_Create(NULL);
2912 if(!wrapper)
2913 return E_OUTOFMEMORY;
2915 wrapper->session = session;
2917 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
2919 return S_OK;
2922 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
2923 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2924 ISimpleAudioVolume **out)
2926 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2927 AudioSession *session;
2928 AudioSessionWrapper *wrapper;
2929 HRESULT hr;
2931 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2932 flags, out);
2934 hr = get_audio_session(session_guid, This->device, 0, &session);
2935 if(FAILED(hr))
2936 return hr;
2938 wrapper = AudioSessionWrapper_Create(NULL);
2939 if(!wrapper)
2940 return E_OUTOFMEMORY;
2942 wrapper->session = session;
2944 *out = &wrapper->ISimpleAudioVolume_iface;
2946 return S_OK;
2949 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
2950 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
2952 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2953 FIXME("(%p)->(%p) - stub\n", This, out);
2954 return E_NOTIMPL;
2957 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
2958 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2960 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2961 FIXME("(%p)->(%p) - stub\n", This, notification);
2962 return E_NOTIMPL;
2965 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
2966 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2968 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2969 FIXME("(%p)->(%p) - stub\n", This, notification);
2970 return E_NOTIMPL;
2973 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
2974 IAudioSessionManager2 *iface, const WCHAR *session_id,
2975 IAudioVolumeDuckNotification *notification)
2977 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2978 FIXME("(%p)->(%p) - stub\n", This, notification);
2979 return E_NOTIMPL;
2982 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
2983 IAudioSessionManager2 *iface,
2984 IAudioVolumeDuckNotification *notification)
2986 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2987 FIXME("(%p)->(%p) - stub\n", This, notification);
2988 return E_NOTIMPL;
2991 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
2993 AudioSessionManager_QueryInterface,
2994 AudioSessionManager_AddRef,
2995 AudioSessionManager_Release,
2996 AudioSessionManager_GetAudioSessionControl,
2997 AudioSessionManager_GetSimpleAudioVolume,
2998 AudioSessionManager_GetSessionEnumerator,
2999 AudioSessionManager_RegisterSessionNotification,
3000 AudioSessionManager_UnregisterSessionNotification,
3001 AudioSessionManager_RegisterDuckNotification,
3002 AudioSessionManager_UnregisterDuckNotification
3005 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3006 IAudioSessionManager2 **out)
3008 SessionMgr *This;
3010 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3011 if(!This)
3012 return E_OUTOFMEMORY;
3014 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3015 This->device = device;
3016 This->ref = 1;
3018 *out = &This->IAudioSessionManager2_iface;
3020 return S_OK;