msi: Don't install services without an image path.
[wine/multimedia.git] / dlls / wineoss.drv / mmdevdrv.c
blobeab4723e2344d1dbf55a9608e53badf03d14b0f7
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"
56 WINE_DEFAULT_DEBUG_CHANNEL(oss);
58 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
60 static const REFERENCE_TIME DefaultPeriod = 200000;
61 static const REFERENCE_TIME MinimumPeriod = 100000;
63 struct ACImpl;
64 typedef struct ACImpl ACImpl;
66 typedef struct _AudioSession {
67 GUID guid;
68 struct list clients;
70 IMMDevice *device;
72 float master_vol;
73 UINT32 channel_count;
74 float *channel_vols;
75 BOOL mute;
77 CRITICAL_SECTION lock;
79 struct list entry;
80 } AudioSession;
82 typedef struct _AudioSessionWrapper {
83 IAudioSessionControl2 IAudioSessionControl2_iface;
84 IChannelAudioVolume IChannelAudioVolume_iface;
85 ISimpleAudioVolume ISimpleAudioVolume_iface;
87 LONG ref;
89 ACImpl *client;
90 AudioSession *session;
91 } AudioSessionWrapper;
93 struct ACImpl {
94 IAudioClient IAudioClient_iface;
95 IAudioRenderClient IAudioRenderClient_iface;
96 IAudioCaptureClient IAudioCaptureClient_iface;
97 IAudioClock IAudioClock_iface;
98 IAudioClock2 IAudioClock2_iface;
99 IAudioStreamVolume IAudioStreamVolume_iface;
101 LONG ref;
103 IMMDevice *parent;
104 IUnknown *pUnkFTMarshal;
106 WAVEFORMATEX *fmt;
108 EDataFlow dataflow;
109 DWORD flags;
110 AUDCLNT_SHAREMODE share;
111 HANDLE event;
112 float *vols;
114 int fd;
115 oss_audioinfo ai;
116 char devnode[OSS_DEVNODE_SIZE];
118 BOOL initted, playing;
119 UINT64 written_frames, last_pos_frames;
120 UINT32 period_us, period_frames, bufsize_frames, held_frames, tmp_buffer_frames;
121 UINT32 oss_bufsize_bytes, lcl_offs_frames; /* offs into local_buffer where valid data starts */
123 BYTE *local_buffer, *tmp_buffer;
124 LONG32 getbuf_last; /* <0 when using tmp_buffer */
125 HANDLE timer;
127 CRITICAL_SECTION lock;
129 AudioSession *session;
130 AudioSessionWrapper *session_wrapper;
132 struct list entry;
135 typedef struct _SessionMgr {
136 IAudioSessionManager2 IAudioSessionManager2_iface;
138 LONG ref;
140 IMMDevice *device;
141 } SessionMgr;
143 typedef struct _OSSDevice {
144 EDataFlow flow;
145 char devnode[OSS_DEVNODE_SIZE];
146 GUID guid;
148 struct list entry;
149 } OSSDevice;
151 static struct list g_devices = LIST_INIT(g_devices);
153 static const WCHAR drv_keyW[] = {'S','o','f','t','w','a','r','e','\\',
154 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
155 'w','i','n','e','o','s','s','.','d','r','v',0};
156 static const WCHAR drv_key_devicesW[] = {'S','o','f','t','w','a','r','e','\\',
157 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',
158 'w','i','n','e','o','s','s','.','d','r','v','\\','d','e','v','i','c','e','s',0};
159 static const WCHAR guidW[] = {'g','u','i','d',0};
161 static HANDLE g_timer_q;
163 static CRITICAL_SECTION g_sessions_lock;
164 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug =
166 0, 0, &g_sessions_lock,
167 { &g_sessions_lock_debug.ProcessLocksList, &g_sessions_lock_debug.ProcessLocksList },
168 0, 0, { (DWORD_PTR)(__FILE__ ": g_sessions_lock") }
170 static CRITICAL_SECTION g_sessions_lock = { &g_sessions_lock_debug, -1, 0, 0, 0, 0 };
171 static struct list g_sessions = LIST_INIT(g_sessions);
173 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
175 static const IAudioClientVtbl AudioClient_Vtbl;
176 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
177 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
178 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
179 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
180 static const IAudioClockVtbl AudioClock_Vtbl;
181 static const IAudioClock2Vtbl AudioClock2_Vtbl;
182 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
183 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
184 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl;
186 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
188 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
191 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
193 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
196 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
198 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
201 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
203 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
206 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
208 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
211 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
213 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
216 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
218 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
221 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
223 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
226 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
228 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
231 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
233 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
236 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
238 switch (reason)
240 case DLL_PROCESS_ATTACH:
241 g_timer_q = CreateTimerQueue();
242 if(!g_timer_q)
243 return FALSE;
244 break;
246 case DLL_PROCESS_DETACH:
247 if (!reserved)
249 OSSDevice *iter, *iter2;
251 DeleteCriticalSection(&g_sessions_lock);
253 LIST_FOR_EACH_ENTRY_SAFE(iter, iter2, &g_devices, OSSDevice, entry){
254 HeapFree(GetProcessHeap(), 0, iter);
257 break;
259 return TRUE;
262 /* From <dlls/mmdevapi/mmdevapi.h> */
263 enum DriverPriority {
264 Priority_Unavailable = 0,
265 Priority_Low,
266 Priority_Neutral,
267 Priority_Preferred
270 int WINAPI AUDDRV_GetPriority(void)
272 int mixer_fd;
273 oss_sysinfo sysinfo;
275 /* Attempt to determine if we are running on OSS or ALSA's OSS
276 * compatibility layer. There is no official way to do that, so just check
277 * for validity as best as possible, without rejecting valid OSS
278 * implementations. */
280 mixer_fd = open("/dev/mixer", O_RDONLY, 0);
281 if(mixer_fd < 0){
282 TRACE("Priority_Unavailable: open failed\n");
283 return Priority_Unavailable;
286 sysinfo.version[0] = 0xFF;
287 sysinfo.versionnum = ~0;
288 if(ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) < 0){
289 TRACE("Priority_Unavailable: ioctl failed\n");
290 close(mixer_fd);
291 return Priority_Unavailable;
294 close(mixer_fd);
296 if(sysinfo.version[0] < '4' || sysinfo.version[0] > '9'){
297 TRACE("Priority_Low: sysinfo.version[0]: %x\n", sysinfo.version[0]);
298 return Priority_Low;
300 if(sysinfo.versionnum & 0x80000000){
301 TRACE("Priority_Low: sysinfo.versionnum: %x\n", sysinfo.versionnum);
302 return Priority_Low;
305 TRACE("Priority_Preferred: Seems like valid OSS!\n");
307 return Priority_Preferred;
310 static void set_device_guid(EDataFlow flow, HKEY drv_key, const WCHAR *key_name,
311 GUID *guid)
313 HKEY key;
314 BOOL opened = FALSE;
315 LONG lr;
317 if(!drv_key){
318 lr = RegCreateKeyExW(HKEY_CURRENT_USER, drv_key_devicesW, 0, NULL, 0, KEY_WRITE,
319 NULL, &drv_key, NULL);
320 if(lr != ERROR_SUCCESS){
321 ERR("RegCreateKeyEx(drv_key) failed: %u\n", lr);
322 return;
324 opened = TRUE;
327 lr = RegCreateKeyExW(drv_key, key_name, 0, NULL, 0, KEY_WRITE,
328 NULL, &key, NULL);
329 if(lr != ERROR_SUCCESS){
330 ERR("RegCreateKeyEx(%s) failed: %u\n", wine_dbgstr_w(key_name), lr);
331 goto exit;
334 lr = RegSetValueExW(key, guidW, 0, REG_BINARY, (BYTE*)guid,
335 sizeof(GUID));
336 if(lr != ERROR_SUCCESS)
337 ERR("RegSetValueEx(%s\\guid) failed: %u\n", wine_dbgstr_w(key_name), lr);
339 RegCloseKey(key);
340 exit:
341 if(opened)
342 RegCloseKey(drv_key);
345 static void get_device_guid(EDataFlow flow, const char *device, GUID *guid)
347 HKEY key = NULL, dev_key;
348 DWORD type, size = sizeof(*guid);
349 WCHAR key_name[256];
351 if(flow == eCapture)
352 key_name[0] = '1';
353 else
354 key_name[0] = '0';
355 key_name[1] = ',';
356 MultiByteToWideChar(CP_UNIXCP, 0, device, -1, key_name + 2,
357 (sizeof(key_name) / sizeof(*key_name)) - 2);
359 if(RegOpenKeyExW(HKEY_CURRENT_USER, drv_key_devicesW, 0, KEY_WRITE|KEY_READ, &key) == ERROR_SUCCESS){
360 if(RegOpenKeyExW(key, key_name, 0, KEY_READ, &dev_key) == ERROR_SUCCESS){
361 if(RegQueryValueExW(dev_key, guidW, 0, &type,
362 (BYTE*)guid, &size) == ERROR_SUCCESS){
363 if(type == REG_BINARY){
364 RegCloseKey(dev_key);
365 RegCloseKey(key);
366 return;
368 ERR("Invalid type for device %s GUID: %u; ignoring and overwriting\n",
369 wine_dbgstr_w(key_name), type);
371 RegCloseKey(dev_key);
375 CoCreateGuid(guid);
377 set_device_guid(flow, key, key_name, guid);
379 if(key)
380 RegCloseKey(key);
383 static const char *oss_clean_devnode(const char *devnode)
385 static char ret[OSS_DEVNODE_SIZE];
387 const char *dot, *slash;
388 size_t len;
390 dot = strrchr(devnode, '.');
391 if(!dot)
392 return devnode;
394 slash = strrchr(devnode, '/');
395 if(slash && dot < slash)
396 return devnode;
398 len = dot - devnode;
400 memcpy(ret, devnode, len);
401 ret[len] = '\0';
403 return ret;
406 static UINT get_default_index(EDataFlow flow)
408 int fd = -1, err;
409 UINT i;
410 oss_audioinfo ai;
411 const char *devnode;
412 OSSDevice *dev_item;
414 if(flow == eRender)
415 fd = open("/dev/dsp", O_WRONLY | O_NONBLOCK);
416 else
417 fd = open("/dev/dsp", O_RDONLY | O_NONBLOCK);
419 if(fd < 0){
420 WARN("Couldn't open default device!\n");
421 return 0;
424 ai.dev = -1;
425 if((err = ioctl(fd, SNDCTL_ENGINEINFO, &ai)) < 0){
426 WARN("SNDCTL_ENGINEINFO failed: %d (%s)\n", err, strerror(errno));
427 close(fd);
428 return 0;
431 close(fd);
433 TRACE("Default devnode: %s\n", ai.devnode);
434 devnode = oss_clean_devnode(ai.devnode);
435 i = 0;
436 LIST_FOR_EACH_ENTRY(dev_item, &g_devices, OSSDevice, entry){
437 if(dev_item->flow == flow){
438 if(!strcmp(devnode, dev_item->devnode))
439 return i;
440 ++i;
444 WARN("Couldn't find default device! Choosing first.\n");
445 return 0;
448 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, GUID **guids,
449 UINT *num, UINT *def_index)
451 int i, mixer_fd;
452 oss_sysinfo sysinfo;
453 static int print_once = 0;
455 static const WCHAR outW[] = {'O','u','t',':',' ',0};
456 static const WCHAR inW[] = {'I','n',':',' ',0};
458 TRACE("%d %p %p %p %p\n", flow, ids, guids, num, def_index);
460 mixer_fd = open("/dev/mixer", O_RDONLY, 0);
461 if(mixer_fd < 0){
462 ERR("OSS /dev/mixer doesn't seem to exist\n");
463 return AUDCLNT_E_SERVICE_NOT_RUNNING;
466 if(ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) < 0){
467 close(mixer_fd);
469 if(errno == EINVAL){
470 ERR("OSS version too old, need at least OSSv4\n");
471 return AUDCLNT_E_SERVICE_NOT_RUNNING;
474 ERR("Error getting SNDCTL_SYSINFO: %d (%s)\n", errno, strerror(errno));
475 return E_FAIL;
478 if(!print_once){
479 TRACE("OSS sysinfo:\n");
480 TRACE("product: %s\n", sysinfo.product);
481 TRACE("version: %s\n", sysinfo.version);
482 TRACE("versionnum: %x\n", sysinfo.versionnum);
483 TRACE("numaudios: %d\n", sysinfo.numaudios);
484 TRACE("nummixers: %d\n", sysinfo.nummixers);
485 TRACE("numcards: %d\n", sysinfo.numcards);
486 TRACE("numaudioengines: %d\n", sysinfo.numaudioengines);
487 print_once = 1;
490 if(sysinfo.numaudios <= 0){
491 WARN("No audio devices!\n");
492 close(mixer_fd);
493 return AUDCLNT_E_SERVICE_NOT_RUNNING;
496 *ids = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(WCHAR *));
497 *guids = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(GUID));
499 *num = 0;
500 for(i = 0; i < sysinfo.numaudios; ++i){
501 oss_audioinfo ai = {0};
502 const char *devnode;
503 OSSDevice *dev_item;
504 int fd;
506 ai.dev = i;
507 if(ioctl(mixer_fd, SNDCTL_AUDIOINFO, &ai) < 0){
508 WARN("Error getting AUDIOINFO for dev %d: %d (%s)\n", i, errno,
509 strerror(errno));
510 continue;
513 devnode = oss_clean_devnode(ai.devnode);
515 /* check for duplicates */
516 LIST_FOR_EACH_ENTRY(dev_item, &g_devices, OSSDevice, entry){
517 if(dev_item->flow == flow && !strcmp(devnode, dev_item->devnode))
518 break;
520 if(&dev_item->entry != &g_devices)
521 continue;
523 if(flow == eRender)
524 fd = open(devnode, O_WRONLY | O_NONBLOCK, 0);
525 else
526 fd = open(devnode, O_RDONLY | O_NONBLOCK, 0);
527 if(fd < 0){
528 WARN("Opening device \"%s\" failed, pretending it doesn't exist: %d (%s)\n",
529 devnode, errno, strerror(errno));
530 continue;
532 close(fd);
534 if((flow == eCapture && (ai.caps & PCM_CAP_INPUT)) ||
535 (flow == eRender && (ai.caps & PCM_CAP_OUTPUT))){
536 size_t len, prefix_len;
537 const WCHAR *prefix;
539 dev_item = HeapAlloc(GetProcessHeap(), 0, sizeof(*dev_item));
541 dev_item->flow = flow;
542 get_device_guid(flow, devnode, &dev_item->guid);
543 strcpy(dev_item->devnode, devnode);
545 (*guids)[*num] = dev_item->guid;
547 len = MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1, NULL, 0);
548 if(flow == eRender){
549 prefix = outW;
550 prefix_len = (sizeof(outW) / sizeof(*outW)) - 1;
551 len += prefix_len;
552 }else{
553 prefix = inW;
554 prefix_len = (sizeof(inW) / sizeof(*inW)) - 1;
555 len += prefix_len;
557 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0,
558 len * sizeof(WCHAR));
559 if(!(*ids)[*num]){
560 for(i = 0; i < *num; ++i)
561 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
562 HeapFree(GetProcessHeap(), 0, *ids);
563 HeapFree(GetProcessHeap(), 0, *guids);
564 HeapFree(GetProcessHeap(), 0, dev_item);
565 close(mixer_fd);
566 return E_OUTOFMEMORY;
568 memcpy((*ids)[*num], prefix, prefix_len * sizeof(WCHAR));
569 MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1,
570 (*ids)[*num] + prefix_len, len - prefix_len);
572 list_add_tail(&g_devices, &dev_item->entry);
574 (*num)++;
578 close(mixer_fd);
580 *def_index = get_default_index(flow);
582 return S_OK;
585 static const OSSDevice *get_ossdevice_from_guid(const GUID *guid)
587 OSSDevice *dev_item;
588 LIST_FOR_EACH_ENTRY(dev_item, &g_devices, OSSDevice, entry)
589 if(IsEqualGUID(guid, &dev_item->guid))
590 return dev_item;
591 return NULL;
594 HRESULT WINAPI AUDDRV_GetAudioEndpoint(GUID *guid, IMMDevice *dev,
595 IAudioClient **out)
597 ACImpl *This;
598 const OSSDevice *oss_dev;
599 HRESULT hr;
601 TRACE("%s %p %p\n", debugstr_guid(guid), dev, out);
603 oss_dev = get_ossdevice_from_guid(guid);
604 if(!oss_dev){
605 WARN("Unknown GUID: %s\n", debugstr_guid(guid));
606 return AUDCLNT_E_DEVICE_INVALIDATED;
609 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
610 if(!This)
611 return E_OUTOFMEMORY;
613 hr = CoCreateFreeThreadedMarshaler((IUnknown *)&This->IAudioClient_iface,
614 (IUnknown **)&This->pUnkFTMarshal);
615 if (FAILED(hr)) {
616 HeapFree(GetProcessHeap(), 0, This);
617 return hr;
620 if(oss_dev->flow == eRender)
621 This->fd = open(oss_dev->devnode, O_WRONLY | O_NONBLOCK, 0);
622 else if(oss_dev->flow == eCapture)
623 This->fd = open(oss_dev->devnode, O_RDONLY | O_NONBLOCK, 0);
624 else{
625 HeapFree(GetProcessHeap(), 0, This);
626 return E_INVALIDARG;
628 if(This->fd < 0){
629 WARN("Unable to open device %s: %d (%s)\n", oss_dev->devnode, errno,
630 strerror(errno));
631 HeapFree(GetProcessHeap(), 0, This);
632 return AUDCLNT_E_DEVICE_INVALIDATED;
635 This->dataflow = oss_dev->flow;
637 This->ai.dev = -1;
638 if(ioctl(This->fd, SNDCTL_ENGINEINFO, &This->ai) < 0){
639 WARN("Unable to get audio info for device %s: %d (%s)\n", oss_dev->devnode,
640 errno, strerror(errno));
641 close(This->fd);
642 HeapFree(GetProcessHeap(), 0, This);
643 return E_FAIL;
646 strcpy(This->devnode, oss_dev->devnode);
648 TRACE("OSS audioinfo:\n");
649 TRACE("devnode: %s\n", This->ai.devnode);
650 TRACE("name: %s\n", This->ai.name);
651 TRACE("busy: %x\n", This->ai.busy);
652 TRACE("caps: %x\n", This->ai.caps);
653 TRACE("iformats: %x\n", This->ai.iformats);
654 TRACE("oformats: %x\n", This->ai.oformats);
655 TRACE("enabled: %d\n", This->ai.enabled);
656 TRACE("min_rate: %d\n", This->ai.min_rate);
657 TRACE("max_rate: %d\n", This->ai.max_rate);
658 TRACE("min_channels: %d\n", This->ai.min_channels);
659 TRACE("max_channels: %d\n", This->ai.max_channels);
661 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
662 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
663 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
664 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
665 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
666 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
668 InitializeCriticalSection(&This->lock);
669 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ACImpl.lock");
671 This->parent = dev;
672 IMMDevice_AddRef(This->parent);
674 IAudioClient_AddRef(&This->IAudioClient_iface);
676 *out = &This->IAudioClient_iface;
678 return S_OK;
681 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
682 REFIID riid, void **ppv)
684 ACImpl *This = impl_from_IAudioClient(iface);
685 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
687 if(!ppv)
688 return E_POINTER;
689 *ppv = NULL;
690 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
691 *ppv = iface;
692 else if(IsEqualIID(riid, &IID_IMarshal))
693 return IUnknown_QueryInterface(This->pUnkFTMarshal, riid, ppv);
694 if(*ppv){
695 IUnknown_AddRef((IUnknown*)*ppv);
696 return S_OK;
698 WARN("Unknown interface %s\n", debugstr_guid(riid));
699 return E_NOINTERFACE;
702 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
704 ACImpl *This = impl_from_IAudioClient(iface);
705 ULONG ref;
706 ref = InterlockedIncrement(&This->ref);
707 TRACE("(%p) Refcount now %u\n", This, ref);
708 return ref;
711 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
713 ACImpl *This = impl_from_IAudioClient(iface);
714 ULONG ref;
715 ref = InterlockedDecrement(&This->ref);
716 TRACE("(%p) Refcount now %u\n", This, ref);
717 if(!ref){
718 IAudioClient_Stop(iface);
719 IMMDevice_Release(This->parent);
720 IUnknown_Release(This->pUnkFTMarshal);
721 This->lock.DebugInfo->Spare[0] = 0;
722 DeleteCriticalSection(&This->lock);
723 close(This->fd);
724 if(This->initted){
725 EnterCriticalSection(&g_sessions_lock);
726 list_remove(&This->entry);
727 LeaveCriticalSection(&g_sessions_lock);
729 HeapFree(GetProcessHeap(), 0, This->vols);
730 HeapFree(GetProcessHeap(), 0, This->local_buffer);
731 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
732 CoTaskMemFree(This->fmt);
733 HeapFree(GetProcessHeap(), 0, This);
735 return ref;
738 static void dump_fmt(const WAVEFORMATEX *fmt)
740 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
741 switch(fmt->wFormatTag){
742 case WAVE_FORMAT_PCM:
743 TRACE("WAVE_FORMAT_PCM");
744 break;
745 case WAVE_FORMAT_IEEE_FLOAT:
746 TRACE("WAVE_FORMAT_IEEE_FLOAT");
747 break;
748 case WAVE_FORMAT_EXTENSIBLE:
749 TRACE("WAVE_FORMAT_EXTENSIBLE");
750 break;
751 default:
752 TRACE("Unknown");
753 break;
755 TRACE(")\n");
757 TRACE("nChannels: %u\n", fmt->nChannels);
758 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
759 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
760 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
761 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
762 TRACE("cbSize: %u\n", fmt->cbSize);
764 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
765 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
766 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
767 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
768 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
772 static DWORD get_channel_mask(unsigned int channels)
774 switch(channels){
775 case 0:
776 return 0;
777 case 1:
778 return KSAUDIO_SPEAKER_MONO;
779 case 2:
780 return KSAUDIO_SPEAKER_STEREO;
781 case 3:
782 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
783 case 4:
784 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
785 case 5:
786 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
787 case 6:
788 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
789 case 7:
790 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
791 case 8:
792 return KSAUDIO_SPEAKER_7POINT1_SURROUND; /* Vista deprecates 7POINT1 */
794 FIXME("Unknown speaker configuration: %u\n", channels);
795 return 0;
798 static int get_oss_format(const WAVEFORMATEX *fmt)
800 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)fmt;
802 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
803 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
804 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
805 switch(fmt->wBitsPerSample){
806 case 8:
807 return AFMT_U8;
808 case 16:
809 return AFMT_S16_LE;
810 case 24:
811 return AFMT_S24_LE;
812 case 32:
813 return AFMT_S32_LE;
815 return -1;
818 #ifdef AFMT_FLOAT
819 if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
820 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
821 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
822 if(fmt->wBitsPerSample != 32)
823 return -1;
825 return AFMT_FLOAT;
827 #endif
829 return -1;
832 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
834 WAVEFORMATEX *ret;
835 size_t size;
837 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
838 size = sizeof(WAVEFORMATEXTENSIBLE);
839 else
840 size = sizeof(WAVEFORMATEX);
842 ret = CoTaskMemAlloc(size);
843 if(!ret)
844 return NULL;
846 memcpy(ret, fmt, size);
848 ret->cbSize = size - sizeof(WAVEFORMATEX);
850 return ret;
853 static HRESULT setup_oss_device(AUDCLNT_SHAREMODE mode, int fd,
854 const WAVEFORMATEX *fmt, WAVEFORMATEX **out)
856 int tmp, oss_format;
857 double tenth;
858 HRESULT ret = S_OK;
859 WAVEFORMATEX *closest = NULL;
861 tmp = oss_format = get_oss_format(fmt);
862 if(oss_format < 0)
863 return AUDCLNT_E_UNSUPPORTED_FORMAT;
864 if(ioctl(fd, SNDCTL_DSP_SETFMT, &tmp) < 0){
865 WARN("SETFMT failed: %d (%s)\n", errno, strerror(errno));
866 return E_FAIL;
868 if(tmp != oss_format){
869 TRACE("Format unsupported by this OSS version: %x\n", oss_format);
870 return AUDCLNT_E_UNSUPPORTED_FORMAT;
873 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
874 (fmt->nAvgBytesPerSec == 0 ||
875 fmt->nBlockAlign == 0 ||
876 ((WAVEFORMATEXTENSIBLE*)fmt)->Samples.wValidBitsPerSample > fmt->wBitsPerSample))
877 return E_INVALIDARG;
879 if(fmt->nChannels == 0)
880 return AUDCLNT_E_UNSUPPORTED_FORMAT;
882 closest = clone_format(fmt);
883 if(!closest)
884 return E_OUTOFMEMORY;
886 tmp = fmt->nSamplesPerSec;
887 if(ioctl(fd, SNDCTL_DSP_SPEED, &tmp) < 0){
888 WARN("SPEED failed: %d (%s)\n", errno, strerror(errno));
889 CoTaskMemFree(closest);
890 return E_FAIL;
892 tenth = fmt->nSamplesPerSec * 0.1;
893 if(tmp > fmt->nSamplesPerSec + tenth || tmp < fmt->nSamplesPerSec - tenth){
894 ret = S_FALSE;
895 closest->nSamplesPerSec = tmp;
898 tmp = fmt->nChannels;
899 if(ioctl(fd, SNDCTL_DSP_CHANNELS, &tmp) < 0){
900 WARN("CHANNELS failed: %d (%s)\n", errno, strerror(errno));
901 CoTaskMemFree(closest);
902 return E_FAIL;
904 if(tmp != fmt->nChannels){
905 ret = S_FALSE;
906 closest->nChannels = tmp;
909 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
910 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = get_channel_mask(closest->nChannels);
912 if(fmt->nBlockAlign != fmt->nChannels * fmt->wBitsPerSample / 8 ||
913 fmt->nAvgBytesPerSec != fmt->nBlockAlign * fmt->nSamplesPerSec ||
914 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
915 ((WAVEFORMATEXTENSIBLE*)fmt)->Samples.wValidBitsPerSample < fmt->wBitsPerSample))
916 ret = S_FALSE;
918 if(mode == AUDCLNT_SHAREMODE_EXCLUSIVE &&
919 fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
920 if(((WAVEFORMATEXTENSIBLE*)fmt)->dwChannelMask == 0 ||
921 ((WAVEFORMATEXTENSIBLE*)fmt)->dwChannelMask & SPEAKER_RESERVED)
922 ret = S_FALSE;
925 if(ret == S_FALSE && !out)
926 ret = AUDCLNT_E_UNSUPPORTED_FORMAT;
928 if(ret == S_FALSE && out){
929 closest->nBlockAlign =
930 closest->nChannels * closest->wBitsPerSample / 8;
931 closest->nAvgBytesPerSec =
932 closest->nBlockAlign * closest->nSamplesPerSec;
933 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
934 ((WAVEFORMATEXTENSIBLE*)closest)->Samples.wValidBitsPerSample = closest->wBitsPerSample;
935 *out = closest;
936 } else
937 CoTaskMemFree(closest);
939 TRACE("returning: %08x\n", ret);
940 return ret;
943 static void session_init_vols(AudioSession *session, UINT channels)
945 if(session->channel_count < channels){
946 UINT i;
948 if(session->channel_vols)
949 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
950 session->channel_vols, sizeof(float) * channels);
951 else
952 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
953 sizeof(float) * channels);
954 if(!session->channel_vols)
955 return;
957 for(i = session->channel_count; i < channels; ++i)
958 session->channel_vols[i] = 1.f;
960 session->channel_count = channels;
964 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
965 UINT num_channels)
967 AudioSession *ret;
969 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
970 if(!ret)
971 return NULL;
973 memcpy(&ret->guid, guid, sizeof(GUID));
975 ret->device = device;
977 list_init(&ret->clients);
979 list_add_head(&g_sessions, &ret->entry);
981 InitializeCriticalSection(&ret->lock);
982 ret->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AudioSession.lock");
984 session_init_vols(ret, num_channels);
986 ret->master_vol = 1.f;
988 return ret;
991 /* if channels == 0, then this will return or create a session with
992 * matching dataflow and GUID. otherwise, channels must also match */
993 static HRESULT get_audio_session(const GUID *sessionguid,
994 IMMDevice *device, UINT channels, AudioSession **out)
996 AudioSession *session;
998 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
999 *out = create_session(&GUID_NULL, device, channels);
1000 if(!*out)
1001 return E_OUTOFMEMORY;
1003 return S_OK;
1006 *out = NULL;
1007 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
1008 if(session->device == device &&
1009 IsEqualGUID(sessionguid, &session->guid)){
1010 session_init_vols(session, channels);
1011 *out = session;
1012 break;
1016 if(!*out){
1017 *out = create_session(sessionguid, device, channels);
1018 if(!*out)
1019 return E_OUTOFMEMORY;
1022 return S_OK;
1025 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
1026 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
1027 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
1028 const GUID *sessionguid)
1030 ACImpl *This = impl_from_IAudioClient(iface);
1031 int i;
1032 HRESULT hr;
1034 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
1035 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
1037 if(!fmt)
1038 return E_POINTER;
1040 dump_fmt(fmt);
1042 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1043 return AUDCLNT_E_NOT_INITIALIZED;
1045 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
1046 AUDCLNT_STREAMFLAGS_LOOPBACK |
1047 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
1048 AUDCLNT_STREAMFLAGS_NOPERSIST |
1049 AUDCLNT_STREAMFLAGS_RATEADJUST |
1050 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
1051 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
1052 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
1053 TRACE("Unknown flags: %08x\n", flags);
1054 return E_INVALIDARG;
1057 if(mode == AUDCLNT_SHAREMODE_SHARED){
1058 period = DefaultPeriod;
1059 if( duration < 3 * period)
1060 duration = 3 * period;
1061 }else{
1062 if(!period)
1063 period = DefaultPeriod; /* not minimum */
1064 if(period < MinimumPeriod || period > 5000000)
1065 return AUDCLNT_E_INVALID_DEVICE_PERIOD;
1066 if(duration > 20000000) /* the smaller the period, the lower this limit */
1067 return AUDCLNT_E_BUFFER_SIZE_ERROR;
1068 if(flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK){
1069 if(duration != period)
1070 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL;
1071 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
1072 return AUDCLNT_E_DEVICE_IN_USE;
1073 }else{
1074 if( duration < 8 * period)
1075 duration = 8 * period; /* may grow above 2s */
1079 EnterCriticalSection(&This->lock);
1081 if(This->initted){
1082 LeaveCriticalSection(&This->lock);
1083 return AUDCLNT_E_ALREADY_INITIALIZED;
1086 hr = setup_oss_device(mode, This->fd, fmt, NULL);
1087 if(FAILED(hr)){
1088 LeaveCriticalSection(&This->lock);
1089 return hr;
1092 This->fmt = clone_format(fmt);
1093 if(!This->fmt){
1094 LeaveCriticalSection(&This->lock);
1095 return E_OUTOFMEMORY;
1098 This->period_us = period / 10;
1099 This->period_frames = MulDiv(fmt->nSamplesPerSec, period, 10000000);
1101 This->bufsize_frames = MulDiv(duration, fmt->nSamplesPerSec, 10000000);
1102 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
1103 This->bufsize_frames * fmt->nBlockAlign);
1104 if(!This->local_buffer){
1105 CoTaskMemFree(This->fmt);
1106 This->fmt = NULL;
1107 LeaveCriticalSection(&This->lock);
1108 return E_OUTOFMEMORY;
1111 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1112 if(!This->vols){
1113 CoTaskMemFree(This->fmt);
1114 This->fmt = NULL;
1115 LeaveCriticalSection(&This->lock);
1116 return E_OUTOFMEMORY;
1119 for(i = 0; i < fmt->nChannels; ++i)
1120 This->vols[i] = 1.f;
1122 This->share = mode;
1123 This->flags = flags;
1124 This->oss_bufsize_bytes = 0;
1126 EnterCriticalSection(&g_sessions_lock);
1128 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
1129 &This->session);
1130 if(FAILED(hr)){
1131 LeaveCriticalSection(&g_sessions_lock);
1132 HeapFree(GetProcessHeap(), 0, This->vols);
1133 This->vols = NULL;
1134 CoTaskMemFree(This->fmt);
1135 This->fmt = NULL;
1136 LeaveCriticalSection(&This->lock);
1137 return hr;
1140 list_add_tail(&This->session->clients, &This->entry);
1142 LeaveCriticalSection(&g_sessions_lock);
1144 This->initted = TRUE;
1146 LeaveCriticalSection(&This->lock);
1148 return S_OK;
1151 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1152 UINT32 *frames)
1154 ACImpl *This = impl_from_IAudioClient(iface);
1156 TRACE("(%p)->(%p)\n", This, frames);
1158 if(!frames)
1159 return E_POINTER;
1161 EnterCriticalSection(&This->lock);
1163 if(!This->initted){
1164 LeaveCriticalSection(&This->lock);
1165 return AUDCLNT_E_NOT_INITIALIZED;
1168 *frames = This->bufsize_frames;
1170 TRACE("buffer size: %u\n", *frames);
1172 LeaveCriticalSection(&This->lock);
1174 return S_OK;
1177 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1178 REFERENCE_TIME *latency)
1180 ACImpl *This = impl_from_IAudioClient(iface);
1182 TRACE("(%p)->(%p)\n", This, latency);
1184 if(!latency)
1185 return E_POINTER;
1187 EnterCriticalSection(&This->lock);
1189 if(!This->initted){
1190 LeaveCriticalSection(&This->lock);
1191 return AUDCLNT_E_NOT_INITIALIZED;
1194 /* pretend we process audio in Period chunks, so max latency includes
1195 * the period time. Some native machines add .6666ms in shared mode. */
1196 *latency = This->period_us * 10 + 6666;
1198 LeaveCriticalSection(&This->lock);
1200 return S_OK;
1203 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1204 UINT32 *numpad)
1206 ACImpl *This = impl_from_IAudioClient(iface);
1208 TRACE("(%p)->(%p)\n", This, numpad);
1210 if(!numpad)
1211 return E_POINTER;
1213 EnterCriticalSection(&This->lock);
1215 if(!This->initted){
1216 LeaveCriticalSection(&This->lock);
1217 return AUDCLNT_E_NOT_INITIALIZED;
1220 *numpad = This->held_frames;
1222 TRACE("padding: %u\n", *numpad);
1224 LeaveCriticalSection(&This->lock);
1226 return S_OK;
1229 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1230 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1231 WAVEFORMATEX **outpwfx)
1233 ACImpl *This = impl_from_IAudioClient(iface);
1234 int fd = -1;
1235 HRESULT ret;
1237 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1239 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1240 return E_POINTER;
1242 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1243 return E_INVALIDARG;
1245 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1246 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1247 return E_INVALIDARG;
1249 dump_fmt(pwfx);
1251 if(outpwfx){
1252 *outpwfx = NULL;
1253 if(mode != AUDCLNT_SHAREMODE_SHARED)
1254 outpwfx = NULL;
1257 if(This->dataflow == eRender)
1258 fd = open(This->devnode, O_WRONLY | O_NONBLOCK, 0);
1259 else if(This->dataflow == eCapture)
1260 fd = open(This->devnode, O_RDONLY | O_NONBLOCK, 0);
1262 if(fd < 0){
1263 WARN("Unable to open device %s: %d (%s)\n", This->devnode, errno,
1264 strerror(errno));
1265 return AUDCLNT_E_DEVICE_INVALIDATED;
1268 ret = setup_oss_device(mode, fd, pwfx, outpwfx);
1270 close(fd);
1272 return ret;
1275 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1276 WAVEFORMATEX **pwfx)
1278 ACImpl *This = impl_from_IAudioClient(iface);
1279 WAVEFORMATEXTENSIBLE *fmt;
1280 int formats;
1282 TRACE("(%p)->(%p)\n", This, pwfx);
1284 if(!pwfx)
1285 return E_POINTER;
1286 *pwfx = NULL;
1288 if(This->dataflow == eRender)
1289 formats = This->ai.oformats;
1290 else if(This->dataflow == eCapture)
1291 formats = This->ai.iformats;
1292 else
1293 return E_UNEXPECTED;
1295 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1296 if(!fmt)
1297 return E_OUTOFMEMORY;
1299 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1300 if(formats & AFMT_S16_LE){
1301 fmt->Format.wBitsPerSample = 16;
1302 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1303 #ifdef AFMT_FLOAT
1304 }else if(formats & AFMT_FLOAT){
1305 fmt->Format.wBitsPerSample = 32;
1306 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1307 #endif
1308 }else if(formats & AFMT_U8){
1309 fmt->Format.wBitsPerSample = 8;
1310 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1311 }else if(formats & AFMT_S32_LE){
1312 fmt->Format.wBitsPerSample = 32;
1313 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1314 }else if(formats & AFMT_S24_LE){
1315 fmt->Format.wBitsPerSample = 24;
1316 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1317 }else{
1318 WARN("Didn't recognize any available OSS formats: %x\n", formats);
1319 CoTaskMemFree(fmt);
1320 return E_FAIL;
1323 /* some OSS drivers are buggy, so set reasonable defaults if
1324 * the reported values seem wacky */
1325 fmt->Format.nChannels = max(This->ai.max_channels, This->ai.min_channels);
1326 if(fmt->Format.nChannels == 0 || fmt->Format.nChannels > 8)
1327 fmt->Format.nChannels = 2;
1329 if(This->ai.max_rate == 0)
1330 fmt->Format.nSamplesPerSec = 44100;
1331 else
1332 fmt->Format.nSamplesPerSec = min(This->ai.max_rate, 44100);
1333 if(fmt->Format.nSamplesPerSec < This->ai.min_rate)
1334 fmt->Format.nSamplesPerSec = This->ai.min_rate;
1336 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1338 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1339 fmt->Format.nChannels) / 8;
1340 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1341 fmt->Format.nBlockAlign;
1343 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1344 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1346 *pwfx = (WAVEFORMATEX*)fmt;
1347 dump_fmt(*pwfx);
1349 return S_OK;
1352 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1353 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1355 ACImpl *This = impl_from_IAudioClient(iface);
1357 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1359 if(!defperiod && !minperiod)
1360 return E_POINTER;
1362 if(defperiod)
1363 *defperiod = DefaultPeriod;
1364 if(minperiod)
1365 *minperiod = MinimumPeriod;
1367 return S_OK;
1370 static void silence_buffer(ACImpl *This, BYTE *buffer, UINT32 frames)
1372 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1373 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1374 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1375 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1376 This->fmt->wBitsPerSample == 8)
1377 memset(buffer, 128, frames * This->fmt->nBlockAlign);
1378 else
1379 memset(buffer, 0, frames * This->fmt->nBlockAlign);
1382 static void oss_write_data(ACImpl *This)
1384 ssize_t written_bytes;
1385 UINT32 written_frames, in_oss_frames, write_limit, max_period;
1386 size_t to_write_frames, to_write_bytes;
1387 audio_buf_info bi;
1388 BYTE *buf =
1389 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1391 if(This->held_frames == 0)
1392 return;
1394 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1395 to_write_frames = This->bufsize_frames - This->lcl_offs_frames;
1396 else
1397 to_write_frames = This->held_frames;
1399 if(ioctl(This->fd, SNDCTL_DSP_GETOSPACE, &bi) < 0){
1400 WARN("GETOSPACE failed: %d (%s)\n", errno, strerror(errno));
1401 return;
1404 max_period = max(bi.fragsize / This->fmt->nBlockAlign, This->period_frames);
1406 if(bi.bytes > This->oss_bufsize_bytes){
1407 TRACE("New buffer size (%u) is larger than old buffer size (%u)\n",
1408 bi.bytes, This->oss_bufsize_bytes);
1409 This->oss_bufsize_bytes = bi.bytes;
1410 in_oss_frames = 0;
1411 }else if(This->oss_bufsize_bytes - bi.bytes <= bi.fragsize)
1412 in_oss_frames = 0;
1413 else
1414 in_oss_frames = (This->oss_bufsize_bytes - bi.bytes) / This->fmt->nBlockAlign;
1416 write_limit = 0;
1417 while(write_limit + in_oss_frames < max_period * 3)
1418 write_limit += max_period;
1419 if(write_limit == 0)
1420 return;
1422 to_write_frames = min(to_write_frames, write_limit);
1423 to_write_bytes = to_write_frames * This->fmt->nBlockAlign;
1425 if(This->session->mute)
1426 silence_buffer(This, buf, to_write_frames);
1428 written_bytes = write(This->fd, buf, to_write_bytes);
1429 if(written_bytes < 0){
1430 /* EAGAIN is OSS buffer full, log that too */
1431 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1432 return;
1434 written_frames = written_bytes / This->fmt->nBlockAlign;
1436 This->lcl_offs_frames += written_frames;
1437 This->lcl_offs_frames %= This->bufsize_frames;
1438 This->held_frames -= written_frames;
1440 if(written_frames < to_write_frames){
1441 /* OSS buffer probably full */
1442 return;
1445 if(This->held_frames && written_frames < write_limit){
1446 /* wrapped and have some data back at the start to write */
1448 to_write_frames = min(write_limit - written_frames, This->held_frames);
1449 to_write_bytes = to_write_frames * This->fmt->nBlockAlign;
1451 if(This->session->mute)
1452 silence_buffer(This, This->local_buffer, to_write_frames);
1454 written_bytes = write(This->fd, This->local_buffer, to_write_bytes);
1455 if(written_bytes < 0){
1456 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1457 return;
1459 written_frames = written_bytes / This->fmt->nBlockAlign;
1461 This->lcl_offs_frames += written_frames;
1462 This->lcl_offs_frames %= This->bufsize_frames;
1463 This->held_frames -= written_frames;
1467 static void oss_read_data(ACImpl *This)
1469 UINT64 pos, readable;
1470 audio_buf_info bi;
1471 ssize_t nread;
1473 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
1474 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
1475 return;
1478 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1479 readable = (This->bufsize_frames - pos) * This->fmt->nBlockAlign;
1481 if(bi.bytes < readable)
1482 readable = bi.bytes;
1484 nread = read(This->fd, This->local_buffer + pos * This->fmt->nBlockAlign,
1485 readable);
1486 if(nread < 0){
1487 WARN("read failed: %d (%s)\n", errno, strerror(errno));
1488 return;
1491 This->held_frames += nread / This->fmt->nBlockAlign;
1493 if(This->held_frames > This->bufsize_frames){
1494 WARN("Overflow of unread data\n");
1495 This->lcl_offs_frames += This->held_frames;
1496 This->lcl_offs_frames %= This->bufsize_frames;
1497 This->held_frames = This->bufsize_frames;
1501 static void CALLBACK oss_period_callback(void *user, BOOLEAN timer)
1503 ACImpl *This = user;
1505 EnterCriticalSection(&This->lock);
1507 if(This->playing){
1508 if(This->dataflow == eRender && This->held_frames)
1509 oss_write_data(This);
1510 else if(This->dataflow == eCapture)
1511 oss_read_data(This);
1514 LeaveCriticalSection(&This->lock);
1516 if(This->event)
1517 SetEvent(This->event);
1520 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1522 ACImpl *This = impl_from_IAudioClient(iface);
1524 TRACE("(%p)\n", This);
1526 EnterCriticalSection(&This->lock);
1528 if(!This->initted){
1529 LeaveCriticalSection(&This->lock);
1530 return AUDCLNT_E_NOT_INITIALIZED;
1533 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1534 LeaveCriticalSection(&This->lock);
1535 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1538 if(This->playing){
1539 LeaveCriticalSection(&This->lock);
1540 return AUDCLNT_E_NOT_STOPPED;
1543 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1544 oss_period_callback, This, 0, This->period_us / 1000,
1545 WT_EXECUTEINTIMERTHREAD))
1546 ERR("Unable to create period timer: %u\n", GetLastError());
1548 This->playing = TRUE;
1550 LeaveCriticalSection(&This->lock);
1552 return S_OK;
1555 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1557 ACImpl *This = impl_from_IAudioClient(iface);
1558 HANDLE event;
1559 DWORD wait;
1561 TRACE("(%p)\n", This);
1563 EnterCriticalSection(&This->lock);
1565 if(!This->initted){
1566 LeaveCriticalSection(&This->lock);
1567 return AUDCLNT_E_NOT_INITIALIZED;
1570 if(!This->playing){
1571 LeaveCriticalSection(&This->lock);
1572 return S_FALSE;
1575 event = CreateEventW(NULL, TRUE, FALSE, NULL);
1576 wait = !DeleteTimerQueueTimer(g_timer_q, This->timer, event);
1577 if(wait)
1578 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1579 wait = wait && GetLastError() == ERROR_IO_PENDING;
1581 This->playing = FALSE;
1583 LeaveCriticalSection(&This->lock);
1585 if(event && wait)
1586 WaitForSingleObject(event, INFINITE);
1587 CloseHandle(event);
1589 return S_OK;
1592 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1594 ACImpl *This = impl_from_IAudioClient(iface);
1596 TRACE("(%p)\n", This);
1598 EnterCriticalSection(&This->lock);
1600 if(!This->initted){
1601 LeaveCriticalSection(&This->lock);
1602 return AUDCLNT_E_NOT_INITIALIZED;
1605 if(This->playing){
1606 LeaveCriticalSection(&This->lock);
1607 return AUDCLNT_E_NOT_STOPPED;
1610 if(This->getbuf_last){
1611 LeaveCriticalSection(&This->lock);
1612 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1615 if(This->dataflow == eRender){
1616 This->written_frames = 0;
1617 This->last_pos_frames = 0;
1618 }else{
1619 This->written_frames += This->held_frames;
1621 This->held_frames = 0;
1622 This->lcl_offs_frames = 0;
1624 LeaveCriticalSection(&This->lock);
1626 return S_OK;
1629 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1630 HANDLE event)
1632 ACImpl *This = impl_from_IAudioClient(iface);
1634 TRACE("(%p)->(%p)\n", This, event);
1636 if(!event)
1637 return E_INVALIDARG;
1639 EnterCriticalSection(&This->lock);
1641 if(!This->initted){
1642 LeaveCriticalSection(&This->lock);
1643 return AUDCLNT_E_NOT_INITIALIZED;
1646 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1647 LeaveCriticalSection(&This->lock);
1648 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1651 if (This->event){
1652 LeaveCriticalSection(&This->lock);
1653 FIXME("called twice\n");
1654 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
1657 This->event = event;
1659 LeaveCriticalSection(&This->lock);
1661 return S_OK;
1664 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1665 void **ppv)
1667 ACImpl *This = impl_from_IAudioClient(iface);
1669 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1671 if(!ppv)
1672 return E_POINTER;
1673 *ppv = NULL;
1675 EnterCriticalSection(&This->lock);
1677 if(!This->initted){
1678 LeaveCriticalSection(&This->lock);
1679 return AUDCLNT_E_NOT_INITIALIZED;
1682 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1683 if(This->dataflow != eRender){
1684 LeaveCriticalSection(&This->lock);
1685 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1687 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1688 *ppv = &This->IAudioRenderClient_iface;
1689 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1690 if(This->dataflow != eCapture){
1691 LeaveCriticalSection(&This->lock);
1692 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1694 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1695 *ppv = &This->IAudioCaptureClient_iface;
1696 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1697 IAudioClock_AddRef(&This->IAudioClock_iface);
1698 *ppv = &This->IAudioClock_iface;
1699 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1700 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1701 *ppv = &This->IAudioStreamVolume_iface;
1702 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1703 if(!This->session_wrapper){
1704 This->session_wrapper = AudioSessionWrapper_Create(This);
1705 if(!This->session_wrapper){
1706 LeaveCriticalSection(&This->lock);
1707 return E_OUTOFMEMORY;
1709 }else
1710 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1712 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1713 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1714 if(!This->session_wrapper){
1715 This->session_wrapper = AudioSessionWrapper_Create(This);
1716 if(!This->session_wrapper){
1717 LeaveCriticalSection(&This->lock);
1718 return E_OUTOFMEMORY;
1720 }else
1721 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1723 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1724 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1725 if(!This->session_wrapper){
1726 This->session_wrapper = AudioSessionWrapper_Create(This);
1727 if(!This->session_wrapper){
1728 LeaveCriticalSection(&This->lock);
1729 return E_OUTOFMEMORY;
1731 }else
1732 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1734 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1737 if(*ppv){
1738 LeaveCriticalSection(&This->lock);
1739 return S_OK;
1742 LeaveCriticalSection(&This->lock);
1744 FIXME("stub %s\n", debugstr_guid(riid));
1745 return E_NOINTERFACE;
1748 static const IAudioClientVtbl AudioClient_Vtbl =
1750 AudioClient_QueryInterface,
1751 AudioClient_AddRef,
1752 AudioClient_Release,
1753 AudioClient_Initialize,
1754 AudioClient_GetBufferSize,
1755 AudioClient_GetStreamLatency,
1756 AudioClient_GetCurrentPadding,
1757 AudioClient_IsFormatSupported,
1758 AudioClient_GetMixFormat,
1759 AudioClient_GetDevicePeriod,
1760 AudioClient_Start,
1761 AudioClient_Stop,
1762 AudioClient_Reset,
1763 AudioClient_SetEventHandle,
1764 AudioClient_GetService
1767 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1768 IAudioRenderClient *iface, REFIID riid, void **ppv)
1770 ACImpl *This = impl_from_IAudioRenderClient(iface);
1771 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1773 if(!ppv)
1774 return E_POINTER;
1775 *ppv = NULL;
1777 if(IsEqualIID(riid, &IID_IUnknown) ||
1778 IsEqualIID(riid, &IID_IAudioRenderClient))
1779 *ppv = iface;
1780 else if(IsEqualIID(riid, &IID_IMarshal))
1781 return IUnknown_QueryInterface(This->pUnkFTMarshal, riid, ppv);
1782 if(*ppv){
1783 IUnknown_AddRef((IUnknown*)*ppv);
1784 return S_OK;
1787 WARN("Unknown interface %s\n", debugstr_guid(riid));
1788 return E_NOINTERFACE;
1791 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1793 ACImpl *This = impl_from_IAudioRenderClient(iface);
1794 return AudioClient_AddRef(&This->IAudioClient_iface);
1797 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1799 ACImpl *This = impl_from_IAudioRenderClient(iface);
1800 return AudioClient_Release(&This->IAudioClient_iface);
1803 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1804 UINT32 frames, BYTE **data)
1806 ACImpl *This = impl_from_IAudioRenderClient(iface);
1807 UINT32 write_pos;
1809 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1811 if(!data)
1812 return E_POINTER;
1814 *data = NULL;
1816 EnterCriticalSection(&This->lock);
1818 if(This->getbuf_last){
1819 LeaveCriticalSection(&This->lock);
1820 return AUDCLNT_E_OUT_OF_ORDER;
1823 if(!frames){
1824 LeaveCriticalSection(&This->lock);
1825 return S_OK;
1828 if(This->held_frames + frames > This->bufsize_frames){
1829 LeaveCriticalSection(&This->lock);
1830 return AUDCLNT_E_BUFFER_TOO_LARGE;
1833 write_pos =
1834 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1835 if(write_pos + frames > This->bufsize_frames){
1836 if(This->tmp_buffer_frames < frames){
1837 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
1838 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1839 frames * This->fmt->nBlockAlign);
1840 if(!This->tmp_buffer){
1841 LeaveCriticalSection(&This->lock);
1842 return E_OUTOFMEMORY;
1844 This->tmp_buffer_frames = frames;
1846 *data = This->tmp_buffer;
1847 This->getbuf_last = -frames;
1848 }else{
1849 *data = This->local_buffer + write_pos * This->fmt->nBlockAlign;
1850 This->getbuf_last = frames;
1853 silence_buffer(This, *data, frames);
1855 LeaveCriticalSection(&This->lock);
1857 return S_OK;
1860 static void oss_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_frames)
1862 UINT32 write_offs_frames =
1863 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1864 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
1865 UINT32 chunk_frames = This->bufsize_frames - write_offs_frames;
1866 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
1867 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
1869 if(written_bytes <= chunk_bytes){
1870 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
1871 }else{
1872 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
1873 memcpy(This->local_buffer, buffer + chunk_bytes,
1874 written_bytes - chunk_bytes);
1878 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1879 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
1881 ACImpl *This = impl_from_IAudioRenderClient(iface);
1882 BYTE *buffer;
1884 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
1886 EnterCriticalSection(&This->lock);
1888 if(!written_frames){
1889 This->getbuf_last = 0;
1890 LeaveCriticalSection(&This->lock);
1891 return S_OK;
1894 if(!This->getbuf_last){
1895 LeaveCriticalSection(&This->lock);
1896 return AUDCLNT_E_OUT_OF_ORDER;
1899 if(written_frames > (This->getbuf_last >= 0 ? This->getbuf_last : -This->getbuf_last)){
1900 LeaveCriticalSection(&This->lock);
1901 return AUDCLNT_E_INVALID_SIZE;
1904 if(This->getbuf_last >= 0)
1905 buffer = This->local_buffer + This->fmt->nBlockAlign *
1906 ((This->lcl_offs_frames + This->held_frames) % This->bufsize_frames);
1907 else
1908 buffer = This->tmp_buffer;
1910 if(flags & AUDCLNT_BUFFERFLAGS_SILENT)
1911 silence_buffer(This, buffer, written_frames);
1913 if(This->getbuf_last < 0)
1914 oss_wrap_buffer(This, buffer, written_frames);
1916 This->held_frames += written_frames;
1917 This->written_frames += written_frames;
1918 This->getbuf_last = 0;
1920 LeaveCriticalSection(&This->lock);
1922 return S_OK;
1925 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1926 AudioRenderClient_QueryInterface,
1927 AudioRenderClient_AddRef,
1928 AudioRenderClient_Release,
1929 AudioRenderClient_GetBuffer,
1930 AudioRenderClient_ReleaseBuffer
1933 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1934 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1936 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1937 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1939 if(!ppv)
1940 return E_POINTER;
1941 *ppv = NULL;
1943 if(IsEqualIID(riid, &IID_IUnknown) ||
1944 IsEqualIID(riid, &IID_IAudioCaptureClient))
1945 *ppv = iface;
1946 else if(IsEqualIID(riid, &IID_IMarshal))
1947 return IUnknown_QueryInterface(This->pUnkFTMarshal, riid, ppv);
1948 if(*ppv){
1949 IUnknown_AddRef((IUnknown*)*ppv);
1950 return S_OK;
1953 WARN("Unknown interface %s\n", debugstr_guid(riid));
1954 return E_NOINTERFACE;
1957 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1959 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1960 return IAudioClient_AddRef(&This->IAudioClient_iface);
1963 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1965 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1966 return IAudioClient_Release(&This->IAudioClient_iface);
1969 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1970 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1971 UINT64 *qpcpos)
1973 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1975 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1976 devpos, qpcpos);
1978 if(!data || !frames || !flags)
1979 return E_POINTER;
1981 EnterCriticalSection(&This->lock);
1983 if(This->getbuf_last){
1984 LeaveCriticalSection(&This->lock);
1985 return AUDCLNT_E_OUT_OF_ORDER;
1988 if(This->held_frames < This->period_frames){
1989 *frames = 0;
1990 LeaveCriticalSection(&This->lock);
1991 return AUDCLNT_S_BUFFER_EMPTY;
1994 *flags = 0;
1996 *frames = This->period_frames;
1998 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
1999 UINT32 chunk_bytes, offs_bytes, frames_bytes;
2000 if(This->tmp_buffer_frames < *frames){
2001 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
2002 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
2003 *frames * This->fmt->nBlockAlign);
2004 if(!This->tmp_buffer){
2005 LeaveCriticalSection(&This->lock);
2006 return E_OUTOFMEMORY;
2008 This->tmp_buffer_frames = *frames;
2011 *data = This->tmp_buffer;
2012 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
2013 This->fmt->nBlockAlign;
2014 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
2015 frames_bytes = *frames * This->fmt->nBlockAlign;
2016 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
2017 memcpy(This->tmp_buffer + chunk_bytes, This->local_buffer,
2018 frames_bytes - chunk_bytes);
2019 }else
2020 *data = This->local_buffer +
2021 This->lcl_offs_frames * This->fmt->nBlockAlign;
2023 This->getbuf_last = *frames;
2025 if(devpos)
2026 *devpos = This->written_frames;
2027 if(qpcpos){
2028 LARGE_INTEGER stamp, freq;
2029 QueryPerformanceCounter(&stamp);
2030 QueryPerformanceFrequency(&freq);
2031 *qpcpos = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2034 LeaveCriticalSection(&This->lock);
2036 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
2039 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
2040 IAudioCaptureClient *iface, UINT32 done)
2042 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2044 TRACE("(%p)->(%u)\n", This, done);
2046 EnterCriticalSection(&This->lock);
2048 if(!done){
2049 This->getbuf_last = 0;
2050 LeaveCriticalSection(&This->lock);
2051 return S_OK;
2054 if(!This->getbuf_last){
2055 LeaveCriticalSection(&This->lock);
2056 return AUDCLNT_E_OUT_OF_ORDER;
2059 if(This->getbuf_last != done){
2060 LeaveCriticalSection(&This->lock);
2061 return AUDCLNT_E_INVALID_SIZE;
2064 This->written_frames += done;
2065 This->held_frames -= done;
2066 This->lcl_offs_frames += done;
2067 This->lcl_offs_frames %= This->bufsize_frames;
2068 This->getbuf_last = 0;
2070 LeaveCriticalSection(&This->lock);
2072 return S_OK;
2075 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2076 IAudioCaptureClient *iface, UINT32 *frames)
2078 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2080 TRACE("(%p)->(%p)\n", This, frames);
2082 if(!frames)
2083 return E_POINTER;
2085 EnterCriticalSection(&This->lock);
2087 *frames = This->held_frames < This->period_frames ? 0 : This->period_frames;
2089 LeaveCriticalSection(&This->lock);
2091 return S_OK;
2094 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2096 AudioCaptureClient_QueryInterface,
2097 AudioCaptureClient_AddRef,
2098 AudioCaptureClient_Release,
2099 AudioCaptureClient_GetBuffer,
2100 AudioCaptureClient_ReleaseBuffer,
2101 AudioCaptureClient_GetNextPacketSize
2104 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2105 REFIID riid, void **ppv)
2107 ACImpl *This = impl_from_IAudioClock(iface);
2109 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2111 if(!ppv)
2112 return E_POINTER;
2113 *ppv = NULL;
2115 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2116 *ppv = iface;
2117 else if(IsEqualIID(riid, &IID_IAudioClock2))
2118 *ppv = &This->IAudioClock2_iface;
2119 if(*ppv){
2120 IUnknown_AddRef((IUnknown*)*ppv);
2121 return S_OK;
2124 WARN("Unknown interface %s\n", debugstr_guid(riid));
2125 return E_NOINTERFACE;
2128 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2130 ACImpl *This = impl_from_IAudioClock(iface);
2131 return IAudioClient_AddRef(&This->IAudioClient_iface);
2134 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2136 ACImpl *This = impl_from_IAudioClock(iface);
2137 return IAudioClient_Release(&This->IAudioClient_iface);
2140 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2142 ACImpl *This = impl_from_IAudioClock(iface);
2144 TRACE("(%p)->(%p)\n", This, freq);
2146 *freq = This->fmt->nSamplesPerSec;
2148 return S_OK;
2151 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2152 UINT64 *qpctime)
2154 ACImpl *This = impl_from_IAudioClock(iface);
2155 int delay;
2157 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2159 if(!pos)
2160 return E_POINTER;
2162 EnterCriticalSection(&This->lock);
2164 if(This->dataflow == eRender){
2165 if(!This->playing || !This->held_frames ||
2166 ioctl(This->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
2167 delay = 0;
2168 else
2169 delay /= This->fmt->nBlockAlign;
2170 if(This->held_frames + delay >= This->written_frames)
2171 *pos = This->last_pos_frames;
2172 else{
2173 *pos = This->written_frames - This->held_frames - delay;
2174 if(*pos < This->last_pos_frames)
2175 *pos = This->last_pos_frames;
2177 }else if(This->dataflow == eCapture){
2178 audio_buf_info bi;
2179 UINT32 held;
2181 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
2182 TRACE("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
2183 held = 0;
2184 }else{
2185 if(bi.bytes <= bi.fragsize)
2186 held = 0;
2187 else
2188 held = bi.bytes / This->fmt->nBlockAlign;
2191 *pos = This->written_frames + held;
2194 This->last_pos_frames = *pos;
2196 LeaveCriticalSection(&This->lock);
2198 if(qpctime){
2199 LARGE_INTEGER stamp, freq;
2200 QueryPerformanceCounter(&stamp);
2201 QueryPerformanceFrequency(&freq);
2202 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2205 return S_OK;
2208 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2209 DWORD *chars)
2211 ACImpl *This = impl_from_IAudioClock(iface);
2213 TRACE("(%p)->(%p)\n", This, chars);
2215 if(!chars)
2216 return E_POINTER;
2218 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2220 return S_OK;
2223 static const IAudioClockVtbl AudioClock_Vtbl =
2225 AudioClock_QueryInterface,
2226 AudioClock_AddRef,
2227 AudioClock_Release,
2228 AudioClock_GetFrequency,
2229 AudioClock_GetPosition,
2230 AudioClock_GetCharacteristics
2233 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2234 REFIID riid, void **ppv)
2236 ACImpl *This = impl_from_IAudioClock2(iface);
2237 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2240 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2242 ACImpl *This = impl_from_IAudioClock2(iface);
2243 return IAudioClient_AddRef(&This->IAudioClient_iface);
2246 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2248 ACImpl *This = impl_from_IAudioClock2(iface);
2249 return IAudioClient_Release(&This->IAudioClient_iface);
2252 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2253 UINT64 *pos, UINT64 *qpctime)
2255 ACImpl *This = impl_from_IAudioClock2(iface);
2257 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2259 return E_NOTIMPL;
2262 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2264 AudioClock2_QueryInterface,
2265 AudioClock2_AddRef,
2266 AudioClock2_Release,
2267 AudioClock2_GetDevicePosition
2270 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2272 AudioSessionWrapper *ret;
2274 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2275 sizeof(AudioSessionWrapper));
2276 if(!ret)
2277 return NULL;
2279 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2280 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2281 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2283 ret->ref = 1;
2285 ret->client = client;
2286 if(client){
2287 ret->session = client->session;
2288 AudioClient_AddRef(&client->IAudioClient_iface);
2291 return ret;
2294 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2295 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2297 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2299 if(!ppv)
2300 return E_POINTER;
2301 *ppv = NULL;
2303 if(IsEqualIID(riid, &IID_IUnknown) ||
2304 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2305 IsEqualIID(riid, &IID_IAudioSessionControl2))
2306 *ppv = iface;
2307 if(*ppv){
2308 IUnknown_AddRef((IUnknown*)*ppv);
2309 return S_OK;
2312 WARN("Unknown interface %s\n", debugstr_guid(riid));
2313 return E_NOINTERFACE;
2316 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2318 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2319 ULONG ref;
2320 ref = InterlockedIncrement(&This->ref);
2321 TRACE("(%p) Refcount now %u\n", This, ref);
2322 return ref;
2325 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2327 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2328 ULONG ref;
2329 ref = InterlockedDecrement(&This->ref);
2330 TRACE("(%p) Refcount now %u\n", This, ref);
2331 if(!ref){
2332 if(This->client){
2333 EnterCriticalSection(&This->client->lock);
2334 This->client->session_wrapper = NULL;
2335 LeaveCriticalSection(&This->client->lock);
2336 AudioClient_Release(&This->client->IAudioClient_iface);
2338 HeapFree(GetProcessHeap(), 0, This);
2340 return ref;
2343 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2344 AudioSessionState *state)
2346 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2347 ACImpl *client;
2349 TRACE("(%p)->(%p)\n", This, state);
2351 if(!state)
2352 return NULL_PTR_ERR;
2354 EnterCriticalSection(&g_sessions_lock);
2356 if(list_empty(&This->session->clients)){
2357 *state = AudioSessionStateExpired;
2358 LeaveCriticalSection(&g_sessions_lock);
2359 return S_OK;
2362 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2363 EnterCriticalSection(&client->lock);
2364 if(client->playing){
2365 *state = AudioSessionStateActive;
2366 LeaveCriticalSection(&client->lock);
2367 LeaveCriticalSection(&g_sessions_lock);
2368 return S_OK;
2370 LeaveCriticalSection(&client->lock);
2373 LeaveCriticalSection(&g_sessions_lock);
2375 *state = AudioSessionStateInactive;
2377 return S_OK;
2380 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2381 IAudioSessionControl2 *iface, WCHAR **name)
2383 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2385 FIXME("(%p)->(%p) - stub\n", This, name);
2387 return E_NOTIMPL;
2390 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2391 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2393 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2395 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2397 return E_NOTIMPL;
2400 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2401 IAudioSessionControl2 *iface, WCHAR **path)
2403 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2405 FIXME("(%p)->(%p) - stub\n", This, path);
2407 return E_NOTIMPL;
2410 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2411 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2413 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2415 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2417 return E_NOTIMPL;
2420 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2421 IAudioSessionControl2 *iface, GUID *group)
2423 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2425 FIXME("(%p)->(%p) - stub\n", This, group);
2427 return E_NOTIMPL;
2430 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2431 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2433 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2435 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2436 debugstr_guid(session));
2438 return E_NOTIMPL;
2441 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2442 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2444 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2446 FIXME("(%p)->(%p) - stub\n", This, events);
2448 return S_OK;
2451 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2452 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2454 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2456 FIXME("(%p)->(%p) - stub\n", This, events);
2458 return S_OK;
2461 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2462 IAudioSessionControl2 *iface, WCHAR **id)
2464 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2466 FIXME("(%p)->(%p) - stub\n", This, id);
2468 return E_NOTIMPL;
2471 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2472 IAudioSessionControl2 *iface, WCHAR **id)
2474 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2476 FIXME("(%p)->(%p) - stub\n", This, id);
2478 return E_NOTIMPL;
2481 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2482 IAudioSessionControl2 *iface, DWORD *pid)
2484 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2486 TRACE("(%p)->(%p)\n", This, pid);
2488 if(!pid)
2489 return E_POINTER;
2491 *pid = GetCurrentProcessId();
2493 return S_OK;
2496 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2497 IAudioSessionControl2 *iface)
2499 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2501 TRACE("(%p)\n", This);
2503 return S_FALSE;
2506 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2507 IAudioSessionControl2 *iface, BOOL optout)
2509 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2511 TRACE("(%p)->(%d)\n", This, optout);
2513 return S_OK;
2516 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2518 AudioSessionControl_QueryInterface,
2519 AudioSessionControl_AddRef,
2520 AudioSessionControl_Release,
2521 AudioSessionControl_GetState,
2522 AudioSessionControl_GetDisplayName,
2523 AudioSessionControl_SetDisplayName,
2524 AudioSessionControl_GetIconPath,
2525 AudioSessionControl_SetIconPath,
2526 AudioSessionControl_GetGroupingParam,
2527 AudioSessionControl_SetGroupingParam,
2528 AudioSessionControl_RegisterAudioSessionNotification,
2529 AudioSessionControl_UnregisterAudioSessionNotification,
2530 AudioSessionControl_GetSessionIdentifier,
2531 AudioSessionControl_GetSessionInstanceIdentifier,
2532 AudioSessionControl_GetProcessId,
2533 AudioSessionControl_IsSystemSoundsSession,
2534 AudioSessionControl_SetDuckingPreference
2537 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2538 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2540 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2542 if(!ppv)
2543 return E_POINTER;
2544 *ppv = NULL;
2546 if(IsEqualIID(riid, &IID_IUnknown) ||
2547 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2548 *ppv = iface;
2549 if(*ppv){
2550 IUnknown_AddRef((IUnknown*)*ppv);
2551 return S_OK;
2554 WARN("Unknown interface %s\n", debugstr_guid(riid));
2555 return E_NOINTERFACE;
2558 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2560 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2561 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2564 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2566 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2567 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2570 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2571 ISimpleAudioVolume *iface, float level, const GUID *context)
2573 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2574 AudioSession *session = This->session;
2576 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2578 if(level < 0.f || level > 1.f)
2579 return E_INVALIDARG;
2581 if(context)
2582 FIXME("Notifications not supported yet\n");
2584 EnterCriticalSection(&session->lock);
2586 session->master_vol = level;
2588 TRACE("OSS doesn't support setting volume\n");
2590 LeaveCriticalSection(&session->lock);
2592 return S_OK;
2595 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2596 ISimpleAudioVolume *iface, float *level)
2598 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2599 AudioSession *session = This->session;
2601 TRACE("(%p)->(%p)\n", session, level);
2603 if(!level)
2604 return NULL_PTR_ERR;
2606 *level = session->master_vol;
2608 return S_OK;
2611 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2612 BOOL mute, const GUID *context)
2614 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2615 AudioSession *session = This->session;
2617 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2619 EnterCriticalSection(&session->lock);
2621 session->mute = mute;
2623 LeaveCriticalSection(&session->lock);
2625 return S_OK;
2628 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2629 BOOL *mute)
2631 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2632 AudioSession *session = This->session;
2634 TRACE("(%p)->(%p)\n", session, mute);
2636 if(!mute)
2637 return NULL_PTR_ERR;
2639 *mute = This->session->mute;
2641 return S_OK;
2644 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2646 SimpleAudioVolume_QueryInterface,
2647 SimpleAudioVolume_AddRef,
2648 SimpleAudioVolume_Release,
2649 SimpleAudioVolume_SetMasterVolume,
2650 SimpleAudioVolume_GetMasterVolume,
2651 SimpleAudioVolume_SetMute,
2652 SimpleAudioVolume_GetMute
2655 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2656 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2658 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2660 if(!ppv)
2661 return E_POINTER;
2662 *ppv = NULL;
2664 if(IsEqualIID(riid, &IID_IUnknown) ||
2665 IsEqualIID(riid, &IID_IAudioStreamVolume))
2666 *ppv = iface;
2667 if(*ppv){
2668 IUnknown_AddRef((IUnknown*)*ppv);
2669 return S_OK;
2672 WARN("Unknown interface %s\n", debugstr_guid(riid));
2673 return E_NOINTERFACE;
2676 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2678 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2679 return IAudioClient_AddRef(&This->IAudioClient_iface);
2682 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2684 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2685 return IAudioClient_Release(&This->IAudioClient_iface);
2688 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2689 IAudioStreamVolume *iface, UINT32 *out)
2691 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2693 TRACE("(%p)->(%p)\n", This, out);
2695 if(!out)
2696 return E_POINTER;
2698 *out = This->fmt->nChannels;
2700 return S_OK;
2703 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2704 IAudioStreamVolume *iface, UINT32 index, float level)
2706 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2708 TRACE("(%p)->(%d, %f)\n", This, index, level);
2710 if(level < 0.f || level > 1.f)
2711 return E_INVALIDARG;
2713 if(index >= This->fmt->nChannels)
2714 return E_INVALIDARG;
2716 EnterCriticalSection(&This->lock);
2718 This->vols[index] = level;
2720 TRACE("OSS doesn't support setting volume\n");
2722 LeaveCriticalSection(&This->lock);
2724 return S_OK;
2727 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2728 IAudioStreamVolume *iface, UINT32 index, float *level)
2730 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2732 TRACE("(%p)->(%d, %p)\n", This, index, level);
2734 if(!level)
2735 return E_POINTER;
2737 if(index >= This->fmt->nChannels)
2738 return E_INVALIDARG;
2740 *level = This->vols[index];
2742 return S_OK;
2745 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2746 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2748 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2749 int i;
2751 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2753 if(!levels)
2754 return E_POINTER;
2756 if(count != This->fmt->nChannels)
2757 return E_INVALIDARG;
2759 EnterCriticalSection(&This->lock);
2761 for(i = 0; i < count; ++i)
2762 This->vols[i] = levels[i];
2764 TRACE("OSS doesn't support setting volume\n");
2766 LeaveCriticalSection(&This->lock);
2768 return S_OK;
2771 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2772 IAudioStreamVolume *iface, UINT32 count, float *levels)
2774 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2775 int i;
2777 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2779 if(!levels)
2780 return E_POINTER;
2782 if(count != This->fmt->nChannels)
2783 return E_INVALIDARG;
2785 EnterCriticalSection(&This->lock);
2787 for(i = 0; i < count; ++i)
2788 levels[i] = This->vols[i];
2790 LeaveCriticalSection(&This->lock);
2792 return S_OK;
2795 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2797 AudioStreamVolume_QueryInterface,
2798 AudioStreamVolume_AddRef,
2799 AudioStreamVolume_Release,
2800 AudioStreamVolume_GetChannelCount,
2801 AudioStreamVolume_SetChannelVolume,
2802 AudioStreamVolume_GetChannelVolume,
2803 AudioStreamVolume_SetAllVolumes,
2804 AudioStreamVolume_GetAllVolumes
2807 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2808 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2810 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2812 if(!ppv)
2813 return E_POINTER;
2814 *ppv = NULL;
2816 if(IsEqualIID(riid, &IID_IUnknown) ||
2817 IsEqualIID(riid, &IID_IChannelAudioVolume))
2818 *ppv = iface;
2819 if(*ppv){
2820 IUnknown_AddRef((IUnknown*)*ppv);
2821 return S_OK;
2824 WARN("Unknown interface %s\n", debugstr_guid(riid));
2825 return E_NOINTERFACE;
2828 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2830 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2831 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2834 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2836 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2837 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2840 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2841 IChannelAudioVolume *iface, UINT32 *out)
2843 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2844 AudioSession *session = This->session;
2846 TRACE("(%p)->(%p)\n", session, out);
2848 if(!out)
2849 return NULL_PTR_ERR;
2851 *out = session->channel_count;
2853 return S_OK;
2856 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2857 IChannelAudioVolume *iface, UINT32 index, float level,
2858 const GUID *context)
2860 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2861 AudioSession *session = This->session;
2863 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2864 wine_dbgstr_guid(context));
2866 if(level < 0.f || level > 1.f)
2867 return E_INVALIDARG;
2869 if(index >= session->channel_count)
2870 return E_INVALIDARG;
2872 if(context)
2873 FIXME("Notifications not supported yet\n");
2875 EnterCriticalSection(&session->lock);
2877 session->channel_vols[index] = level;
2879 TRACE("OSS doesn't support setting volume\n");
2881 LeaveCriticalSection(&session->lock);
2883 return S_OK;
2886 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2887 IChannelAudioVolume *iface, UINT32 index, float *level)
2889 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2890 AudioSession *session = This->session;
2892 TRACE("(%p)->(%d, %p)\n", session, index, level);
2894 if(!level)
2895 return NULL_PTR_ERR;
2897 if(index >= session->channel_count)
2898 return E_INVALIDARG;
2900 *level = session->channel_vols[index];
2902 return S_OK;
2905 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2906 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2907 const GUID *context)
2909 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2910 AudioSession *session = This->session;
2911 int i;
2913 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2914 wine_dbgstr_guid(context));
2916 if(!levels)
2917 return NULL_PTR_ERR;
2919 if(count != session->channel_count)
2920 return E_INVALIDARG;
2922 if(context)
2923 FIXME("Notifications not supported yet\n");
2925 EnterCriticalSection(&session->lock);
2927 for(i = 0; i < count; ++i)
2928 session->channel_vols[i] = levels[i];
2930 TRACE("OSS doesn't support setting volume\n");
2932 LeaveCriticalSection(&session->lock);
2934 return S_OK;
2937 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2938 IChannelAudioVolume *iface, UINT32 count, float *levels)
2940 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2941 AudioSession *session = This->session;
2942 int i;
2944 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2946 if(!levels)
2947 return NULL_PTR_ERR;
2949 if(count != session->channel_count)
2950 return E_INVALIDARG;
2952 for(i = 0; i < count; ++i)
2953 levels[i] = session->channel_vols[i];
2955 return S_OK;
2958 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2960 ChannelAudioVolume_QueryInterface,
2961 ChannelAudioVolume_AddRef,
2962 ChannelAudioVolume_Release,
2963 ChannelAudioVolume_GetChannelCount,
2964 ChannelAudioVolume_SetChannelVolume,
2965 ChannelAudioVolume_GetChannelVolume,
2966 ChannelAudioVolume_SetAllVolumes,
2967 ChannelAudioVolume_GetAllVolumes
2970 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2971 REFIID riid, void **ppv)
2973 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2975 if(!ppv)
2976 return E_POINTER;
2977 *ppv = NULL;
2979 if(IsEqualIID(riid, &IID_IUnknown) ||
2980 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2981 IsEqualIID(riid, &IID_IAudioSessionManager2))
2982 *ppv = iface;
2983 if(*ppv){
2984 IUnknown_AddRef((IUnknown*)*ppv);
2985 return S_OK;
2988 WARN("Unknown interface %s\n", debugstr_guid(riid));
2989 return E_NOINTERFACE;
2992 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2994 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2995 ULONG ref;
2996 ref = InterlockedIncrement(&This->ref);
2997 TRACE("(%p) Refcount now %u\n", This, ref);
2998 return ref;
3001 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
3003 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3004 ULONG ref;
3005 ref = InterlockedDecrement(&This->ref);
3006 TRACE("(%p) Refcount now %u\n", This, ref);
3007 if(!ref)
3008 HeapFree(GetProcessHeap(), 0, This);
3009 return ref;
3012 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
3013 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3014 IAudioSessionControl **out)
3016 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3017 AudioSession *session;
3018 AudioSessionWrapper *wrapper;
3019 HRESULT hr;
3021 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3022 flags, out);
3024 hr = get_audio_session(session_guid, This->device, 0, &session);
3025 if(FAILED(hr))
3026 return hr;
3028 wrapper = AudioSessionWrapper_Create(NULL);
3029 if(!wrapper)
3030 return E_OUTOFMEMORY;
3032 wrapper->session = session;
3034 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3036 return S_OK;
3039 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
3040 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3041 ISimpleAudioVolume **out)
3043 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3044 AudioSession *session;
3045 AudioSessionWrapper *wrapper;
3046 HRESULT hr;
3048 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3049 flags, out);
3051 hr = get_audio_session(session_guid, This->device, 0, &session);
3052 if(FAILED(hr))
3053 return hr;
3055 wrapper = AudioSessionWrapper_Create(NULL);
3056 if(!wrapper)
3057 return E_OUTOFMEMORY;
3059 wrapper->session = session;
3061 *out = &wrapper->ISimpleAudioVolume_iface;
3063 return S_OK;
3066 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
3067 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
3069 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3070 FIXME("(%p)->(%p) - stub\n", This, out);
3071 return E_NOTIMPL;
3074 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
3075 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3077 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3078 FIXME("(%p)->(%p) - stub\n", This, notification);
3079 return E_NOTIMPL;
3082 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
3083 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3085 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3086 FIXME("(%p)->(%p) - stub\n", This, notification);
3087 return E_NOTIMPL;
3090 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
3091 IAudioSessionManager2 *iface, const WCHAR *session_id,
3092 IAudioVolumeDuckNotification *notification)
3094 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3095 FIXME("(%p)->(%p) - stub\n", This, notification);
3096 return E_NOTIMPL;
3099 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
3100 IAudioSessionManager2 *iface,
3101 IAudioVolumeDuckNotification *notification)
3103 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3104 FIXME("(%p)->(%p) - stub\n", This, notification);
3105 return E_NOTIMPL;
3108 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
3110 AudioSessionManager_QueryInterface,
3111 AudioSessionManager_AddRef,
3112 AudioSessionManager_Release,
3113 AudioSessionManager_GetAudioSessionControl,
3114 AudioSessionManager_GetSimpleAudioVolume,
3115 AudioSessionManager_GetSessionEnumerator,
3116 AudioSessionManager_RegisterSessionNotification,
3117 AudioSessionManager_UnregisterSessionNotification,
3118 AudioSessionManager_RegisterDuckNotification,
3119 AudioSessionManager_UnregisterDuckNotification
3122 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3123 IAudioSessionManager2 **out)
3125 SessionMgr *This;
3127 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3128 if(!This)
3129 return E_OUTOFMEMORY;
3131 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3132 This->device = device;
3133 This->ref = 1;
3135 *out = &This->IAudioSessionManager2_iface;
3137 return S_OK;