widl: Strip last separator in append_namespaces if suffix is NULL.
[wine.git] / dlls / winmm / waveform.c
blob37dfc91917473f9a10bf3cce80119bcd69f613b3
1 /*
2 * Copyright 1993 Martin Ayotte
3 * 1998-2002 Eric Pouech
4 * 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "mmsystem.h"
32 #include "mmreg.h"
33 #include "msacm.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "winternl.h"
38 #include "winemm.h"
39 #include "resource.h"
41 #include "ole2.h"
42 #include "initguid.h"
43 #include "devpkey.h"
44 #include "mmdeviceapi.h"
45 #include "audioclient.h"
46 #include "audiopolicy.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
52 /* HWAVE (and HMIXER) format:
54 * XXXX... 1FDD DDDD IIII IIII
55 * X = unused (must be 0)
56 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
57 * F = flow direction (0 = IN, 1 = OUT)
58 * D = index into g_out_mmdevices, or all 1s for the MAPPER device
59 * I = index in the mmdevice's devices array
61 * Two reasons that we don't just use pointers:
62 * - HWAVEs must fit into 16 bits for compatibility with old applications.
63 * - We must be able to identify bad devices without crashing.
66 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
67 #define AC_BUFLEN (10 * 100000)
68 #define MAX_DEVICES 256
69 #define MAPPER_INDEX 0x3F
71 typedef struct _WINMM_CBInfo {
72 DWORD_PTR callback;
73 DWORD_PTR user;
74 DWORD flags;
75 HWAVE hwave;
76 } WINMM_CBInfo;
78 struct _WINMM_MMDevice;
79 typedef struct _WINMM_MMDevice WINMM_MMDevice;
81 typedef struct _WINMM_Device {
82 WINMM_CBInfo cb_info;
84 HWAVE handle;
86 BOOL open;
88 IMMDevice *device;
89 IAudioClient *client;
90 IAudioRenderClient *render;
91 IAudioCaptureClient *capture;
92 IAudioClock *clock;
93 IAudioStreamVolume *volume;
95 WAVEFORMATEX *orig_fmt;
96 HACMSTREAM acm_handle;
97 ACMSTREAMHEADER acm_hdr;
98 UINT32 acm_offs;
100 WAVEHDR *first, *last, *playing, *loop_start;
102 BOOL stopped;
103 DWORD loop_counter;
104 UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
105 UINT32 remainder_frames; /* header chunk frames already played when a device switch occurred */
107 /* stored in frames of sample rate, *not* AC::GetFrequency */
108 UINT64 last_clock_pos;
110 HANDLE event;
111 CRITICAL_SECTION lock;
113 WINMM_MMDevice *parent;
114 } WINMM_Device;
116 struct _WINMM_MMDevice {
117 WAVEOUTCAPSW out_caps; /* must not be modified outside of WINMM_InitMMDevices*/
118 WAVEINCAPSW in_caps; /* must not be modified outside of WINMM_InitMMDevices*/
119 WCHAR *dev_id;
120 EDataFlow dataflow;
122 ISimpleAudioVolume *volume;
124 GUID session;
126 UINT index;
128 /* HMIXER format is the same as the HWAVE format, but the I bits are
129 * replaced by the value of this counter, to keep each HMIXER unique */
130 UINT mixer_count;
132 CRITICAL_SECTION lock;
134 WINMM_Device *devices[MAX_DEVICES];
137 static WINMM_MMDevice *g_out_mmdevices;
138 static WINMM_MMDevice **g_out_map;
139 static UINT g_outmmdevices_count;
140 static WINMM_Device *g_out_mapper_devices[MAX_DEVICES];
142 static WINMM_MMDevice *g_in_mmdevices;
143 static WINMM_MMDevice **g_in_map;
144 static UINT g_inmmdevices_count;
145 static WINMM_Device *g_in_mapper_devices[MAX_DEVICES];
147 static IMMDeviceEnumerator *g_devenum;
149 static CRITICAL_SECTION g_devthread_lock;
150 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug =
152 0, 0, &g_devthread_lock,
153 { &g_devthread_lock_debug.ProcessLocksList, &g_devthread_lock_debug.ProcessLocksList },
154 0, 0, { (DWORD_PTR)(__FILE__ ": g_devthread_lock") }
156 static CRITICAL_SECTION g_devthread_lock = { &g_devthread_lock_debug, -1, 0, 0, 0, 0 };
157 static LONG g_devthread_token;
158 static HANDLE g_devices_thread;
159 static HWND g_devices_hwnd;
160 static HMODULE g_devthread_module;
162 static UINT g_devhandle_count;
163 static HANDLE *g_device_handles;
164 static WINMM_Device **g_handle_devices;
166 typedef struct _WINMM_OpenInfo {
167 HWAVE handle;
168 UINT req_device;
169 WAVEFORMATEX *format;
170 DWORD_PTR callback;
171 DWORD_PTR cb_user;
172 DWORD flags;
173 BOOL reset;
174 } WINMM_OpenInfo;
176 typedef struct _WINMM_ControlDetails {
177 HMIXEROBJ hmix;
178 MIXERCONTROLDETAILS *details;
179 DWORD flags;
180 } WINMM_ControlDetails;
182 typedef struct _WINMM_QueryInterfaceInfo {
183 BOOL is_out;
184 UINT index;
185 WCHAR *str;
186 UINT *len_bytes;
187 } WINMM_QueryInterfaceInfo;
189 static LRESULT WOD_Open(WINMM_OpenInfo *info);
190 static LRESULT WOD_Close(HWAVEOUT hwave);
191 static LRESULT WID_Open(WINMM_OpenInfo *info);
192 static LRESULT WID_Close(HWAVEIN hwave);
193 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device);
195 void WINMM_DeleteWaveform(void)
197 UINT i, j;
199 if(g_devices_thread)
200 CloseHandle(g_devices_thread);
202 for(i = 0; i < g_outmmdevices_count; ++i){
203 WINMM_MMDevice *mmdevice = &g_out_mmdevices[i];
205 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
206 WINMM_Device *device = mmdevice->devices[j];
207 if(device->handle)
208 CloseHandle(device->handle);
209 device->lock.DebugInfo->Spare[0] = 0;
210 DeleteCriticalSection(&device->lock);
213 if(mmdevice->volume)
214 ISimpleAudioVolume_Release(mmdevice->volume);
215 CoTaskMemFree(mmdevice->dev_id);
216 mmdevice->lock.DebugInfo->Spare[0] = 0;
217 DeleteCriticalSection(&mmdevice->lock);
220 for(i = 0; i < g_inmmdevices_count; ++i){
221 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
223 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
224 WINMM_Device *device = mmdevice->devices[j];
225 if(device->handle)
226 CloseHandle(device->handle);
227 device->lock.DebugInfo->Spare[0] = 0;
228 DeleteCriticalSection(&device->lock);
231 if(mmdevice->volume)
232 ISimpleAudioVolume_Release(mmdevice->volume);
233 CoTaskMemFree(mmdevice->dev_id);
234 mmdevice->lock.DebugInfo->Spare[0] = 0;
235 DeleteCriticalSection(&mmdevice->lock);
238 HeapFree(GetProcessHeap(), 0, g_out_mmdevices);
239 HeapFree(GetProcessHeap(), 0, g_in_mmdevices);
241 HeapFree(GetProcessHeap(), 0, g_device_handles);
242 HeapFree(GetProcessHeap(), 0, g_handle_devices);
244 DeleteCriticalSection(&g_devthread_lock);
247 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
249 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
250 (mmdevice << 8) | device);
253 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
254 BOOL *is_out, UINT *device_index, UINT *junk)
256 ULONG32 l = HandleToULong(hwave);
257 *device_index = l & 0xFF;
258 *mmdevice_index = (l >> 8) & 0x3F;
259 *is_out = (l >> 14) & 0x1;
260 *junk = l >> 15;
263 static void WINMM_InitDevice(WINMM_Device *device)
265 InitializeCriticalSection(&device->lock);
266 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
269 static inline WINMM_MMDevice *read_map(WINMM_MMDevice **map, UINT index)
271 WINMM_MMDevice *ret;
272 EnterCriticalSection(&g_devthread_lock);
273 ret = map[index];
274 LeaveCriticalSection(&g_devthread_lock);
275 return ret;
278 /* finds the first unused Device, marks it as "open", and returns
279 * a pointer to the device
281 * IMPORTANT: it is the caller's responsibility to release the device's lock
282 * on success
284 static WINMM_Device *WINMM_FindUnusedDevice(WINMM_Device **devices,
285 WINMM_MMDevice *parent, UINT internal_index, BOOL is_out)
287 UINT i;
289 for(i = 0; i < MAX_DEVICES; ++i){
290 WINMM_Device *device = devices[i];
292 if(!device){
293 device = devices[i] = HeapAlloc(GetProcessHeap(),
294 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
295 if(!device)
296 return NULL;
298 WINMM_InitDevice(device);
299 EnterCriticalSection(&device->lock);
300 }else
301 EnterCriticalSection(&device->lock);
303 if(!device->open){
304 device->handle = WINMM_MakeHWAVE(internal_index, is_out, i);
305 device->parent = parent;
306 device->open = TRUE;
308 return device;
311 LeaveCriticalSection(&device->lock);
314 TRACE("All devices in use: mmdevice: %u\n", internal_index);
316 return NULL;
319 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
321 if(!device)
322 return FALSE;
324 EnterCriticalSection(&device->lock);
326 if(!device->open){
327 LeaveCriticalSection(&device->lock);
328 return FALSE;
331 return TRUE;
334 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
336 WINMM_MMDevice *mmdevice;
337 WINMM_Device *device;
338 UINT mmdevice_index, device_index, junk;
339 BOOL is_out;
341 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
343 if(junk != 0x1)
344 return NULL;
346 if(mmdevice_index == MAPPER_INDEX){
347 EnterCriticalSection(&g_devthread_lock);
348 if(is_out)
349 device = g_out_mapper_devices[device_index];
350 else
351 device = g_in_mapper_devices[device_index];
352 LeaveCriticalSection(&g_devthread_lock);
353 return device;
356 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
357 return NULL;
359 if(is_out)
360 mmdevice = &g_out_mmdevices[mmdevice_index];
361 else
362 mmdevice = &g_in_mmdevices[mmdevice_index];
364 EnterCriticalSection(&mmdevice->lock);
366 device = mmdevice->devices[device_index];
368 LeaveCriticalSection(&mmdevice->lock);
370 return device;
373 /* Note: NotifyClient should never be called while holding the device lock
374 * since the client may call wave* functions from within the callback. */
375 static inline void WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
376 DWORD_PTR param2)
378 DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
379 msg, info->user, param1, param2);
382 static MMRESULT hr2mmr(HRESULT hr)
384 switch(hr){
385 case S_OK:
386 case AUDCLNT_E_NOT_STOPPED:
387 return MMSYSERR_NOERROR;
388 case AUDCLNT_E_UNSUPPORTED_FORMAT:
389 return WAVERR_BADFORMAT;
390 case AUDCLNT_E_DEVICE_IN_USE:
391 return MMSYSERR_ALLOCATED;
392 case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
393 return MMSYSERR_NOTENABLED;
394 case E_OUTOFMEMORY:
395 return MMSYSERR_NOMEM;
396 case E_POINTER:
397 case E_INVALIDARG:
398 return MMSYSERR_INVALPARAM;
399 case AUDCLNT_E_DEVICE_INVALIDATED: /* DSERR_BUFFERLOST */
400 default:
401 return FAILED(hr) ? MMSYSERR_ERROR : MMSYSERR_NOERROR;
405 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
406 UINT outlen)
408 IPropertyStore *ps;
409 PROPVARIANT var;
410 HRESULT hr;
412 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
413 if(FAILED(hr))
414 return hr;
416 PropVariantInit(&var);
418 hr = IPropertyStore_GetValue(ps,
419 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
420 if(FAILED(hr)){
421 IPropertyStore_Release(ps);
422 return hr;
425 lstrcpynW(out, var.u.pwszVal, outlen);
427 PropVariantClear(&var);
429 IPropertyStore_Release(ps);
431 return S_OK;
434 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
435 WORD channels)
437 WAVEFORMATEX fmt, *junk;
438 HRESULT hr;
440 fmt.wFormatTag = WAVE_FORMAT_PCM;
441 fmt.nChannels = channels;
442 fmt.nSamplesPerSec = rate;
443 fmt.wBitsPerSample = depth;
444 fmt.nBlockAlign = (channels * depth) / 8;
445 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
446 fmt.cbSize = 0;
448 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
449 &fmt, &junk);
450 if(SUCCEEDED(hr))
451 CoTaskMemFree(junk);
453 return hr;
456 static struct _TestFormat {
457 DWORD flag;
458 DWORD rate;
459 DWORD depth;
460 WORD channels;
461 } formats_to_test[] = {
462 { WAVE_FORMAT_1M08, 11025, 8, 1 },
463 { WAVE_FORMAT_1M16, 11025, 16, 1 },
464 { WAVE_FORMAT_1S08, 11025, 8, 2 },
465 { WAVE_FORMAT_1S16, 11025, 16, 2 },
466 { WAVE_FORMAT_2M08, 22050, 8, 1 },
467 { WAVE_FORMAT_2M16, 22050, 16, 1 },
468 { WAVE_FORMAT_2S08, 22050, 8, 2 },
469 { WAVE_FORMAT_2S16, 22050, 16, 2 },
470 { WAVE_FORMAT_4M08, 44100, 8, 1 },
471 { WAVE_FORMAT_4M16, 44100, 16, 1 },
472 { WAVE_FORMAT_4S08, 44100, 8, 2 },
473 { WAVE_FORMAT_4S16, 44100, 16, 2 },
474 { WAVE_FORMAT_48M08, 48000, 8, 1 },
475 { WAVE_FORMAT_48M16, 48000, 16, 1 },
476 { WAVE_FORMAT_48S08, 48000, 8, 2 },
477 { WAVE_FORMAT_48S16, 48000, 16, 2 },
478 { WAVE_FORMAT_96M08, 96000, 8, 1 },
479 { WAVE_FORMAT_96M16, 96000, 16, 1 },
480 { WAVE_FORMAT_96S08, 96000, 8, 2 },
481 { WAVE_FORMAT_96S16, 96000, 16, 2 },
485 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
487 DWORD flags = 0;
488 HRESULT hr;
489 struct _TestFormat *fmt;
490 IAudioClient *client;
492 hr = IMMDevice_Activate(device, &IID_IAudioClient,
493 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
494 if(FAILED(hr))
495 return 0;
497 for(fmt = formats_to_test; fmt->flag; ++fmt){
498 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
499 if(hr == S_OK)
500 flags |= fmt->flag;
503 IAudioClient_Release(client);
505 return flags;
508 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
509 WINMM_MMDevice *dev, UINT index)
511 HRESULT hr;
513 dev->dataflow = flow;
514 if(flow == eRender){
515 dev->out_caps.wMid = 0xFF;
516 dev->out_caps.wPid = 0xFF;
517 dev->out_caps.vDriverVersion = 0x00010001;
518 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
519 dev->out_caps.wReserved1 = 0;
520 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
521 WAVECAPS_SAMPLEACCURATE;
522 dev->out_caps.wChannels = 2;
523 dev->out_caps.szPname[0] = '\0';
525 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
526 sizeof(dev->out_caps.szPname) /
527 sizeof(*dev->out_caps.szPname));
528 if(FAILED(hr))
529 return hr;
530 }else{
531 dev->in_caps.wMid = 0xFF;
532 dev->in_caps.wPid = 0xFF;
533 dev->in_caps.vDriverVersion = 0x00010001;
534 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
535 dev->in_caps.wReserved1 = 0;
536 dev->in_caps.wChannels = 2;
537 dev->in_caps.szPname[0] = '\0';
539 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
540 sizeof(dev->in_caps.szPname) /
541 sizeof(*dev->in_caps.szPname));
542 if(FAILED(hr))
543 return hr;
546 hr = IMMDevice_GetId(device, &dev->dev_id);
547 if(FAILED(hr))
548 return hr;
550 CoCreateGuid(&dev->session);
552 dev->index = index;
554 InitializeCriticalSection(&dev->lock);
555 dev->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
557 return S_OK;
560 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices,
561 WINMM_MMDevice ***map, UINT *devcount, EDataFlow flow,
562 IMMDeviceEnumerator *devenum)
564 IMMDeviceCollection *devcoll;
565 HRESULT hr;
567 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
568 DEVICE_STATE_ACTIVE, &devcoll);
569 if(FAILED(hr))
570 return hr;
572 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
573 if(FAILED(hr)){
574 IMMDeviceCollection_Release(devcoll);
575 return hr;
578 if(*devcount > 0){
579 UINT n, count = 1;
580 IMMDevice *def_dev = NULL;
582 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
583 sizeof(WINMM_MMDevice) * (*devcount));
584 if(!*devices){
585 IMMDeviceCollection_Release(devcoll);
586 return E_OUTOFMEMORY;
589 *map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
590 sizeof(WINMM_MMDevice *) * (*devcount));
591 if(!*map){
592 IMMDeviceCollection_Release(devcoll);
593 HeapFree(GetProcessHeap(), 0, *devices);
594 return E_OUTOFMEMORY;
597 /* make sure that device 0 is the default device */
598 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
599 flow, eConsole, &def_dev);
601 for(n = 0; n < *devcount; ++n){
602 IMMDevice *device;
604 hr = IMMDeviceCollection_Item(devcoll, n, &device);
605 if(SUCCEEDED(hr)){
606 WINMM_InitMMDevice(flow, device, &(*devices)[n], n);
608 if(device == def_dev)
609 (*map)[0] = &(*devices)[n];
610 else{
611 (*map)[count] = &(*devices)[n];
612 ++count;
615 IMMDevice_Release(device);
619 IMMDevice_Release(def_dev);
621 *devcount = count;
624 IMMDeviceCollection_Release(devcoll);
626 return S_OK;
629 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
630 const GUID *riid, void **obj)
632 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid));
633 return E_NOINTERFACE;
636 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
638 return 2;
641 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
643 return 1;
646 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
647 const WCHAR *device_id, DWORD new_state)
649 TRACE("Ignoring OnDeviceStateChanged callback\n");
650 return S_OK;
653 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
654 const WCHAR *device_id)
656 TRACE("Ignoring OnDeviceAdded callback\n");
657 return S_OK;
660 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
661 const WCHAR *device_id)
663 TRACE("Ignoring OnDeviceRemoved callback\n");
664 return S_OK;
667 static HRESULT update_mapping(WINMM_MMDevice ***map, UINT count,
668 const WCHAR *default_id)
670 WINMM_MMDevice *prev;
671 UINT i;
673 prev = (*map)[0];
674 for(i = 0; i < count; ++i){
675 WINMM_MMDevice *tmp;
677 if(!wcscmp((*map)[i]->dev_id, default_id)){
678 (*map)[0] = (*map)[i];
679 (*map)[i] = prev;
681 return S_OK;
684 tmp = (*map)[i];
685 (*map)[i] = prev;
686 prev = tmp;
689 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
690 (*map)[0] = prev;
692 return S_FALSE;
695 static HRESULT reroute_mapper_device(WINMM_Device *device, BOOL is_out)
697 WINMM_OpenInfo info;
698 BOOL stopped;
699 MMRESULT mr;
700 HRESULT hr;
701 UINT64 clock_freq, clock_pos;
703 TRACE("rerouting device %p\n", device->handle);
705 EnterCriticalSection(&device->lock);
707 if(!device->open || device->acm_handle){
708 /* Windows 7 doesn't re-route ACM devices, so we don't either.
709 * Seems to be because of the data waveXxxPrepareHeader allocates. */
710 LeaveCriticalSection(&device->lock);
711 return S_FALSE;
714 stopped = device->stopped;
716 info.handle = 0;
717 info.req_device = WAVE_MAPPER;
718 info.format = device->orig_fmt;
719 info.callback = device->cb_info.callback;
720 info.cb_user = device->cb_info.user;
721 /* We have to use direct here so that we don't suddenly introduce ACM
722 * into a playing stream that hasn't been Prepared for it */
723 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT_QUERY;
724 info.reset = FALSE;
726 if(is_out)
727 mr = WOD_Open(&info);
728 else
729 mr = WID_Open(&info);
731 if(mr != MMSYSERR_NOERROR){
732 TRACE("New default device doesn't support this stream: %p\n", device->handle);
733 LeaveCriticalSection(&device->lock);
734 return S_FALSE;
737 hr = IAudioClient_Stop(device->client);
738 if(FAILED(hr))
739 WARN("Stop failed: %08x\n", hr);
741 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
742 if(FAILED(hr)){
743 WARN("GetFrequency failed: %08x\n", hr);
744 LeaveCriticalSection(&device->lock);
745 return hr;
748 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
749 if(FAILED(hr)){
750 WARN("GetPosition failed: %08x\n", hr);
751 LeaveCriticalSection(&device->lock);
752 return hr;
755 device->remainder_frames = MulDiv(clock_pos, device->samples_per_sec, clock_freq) - device->last_clock_pos;
757 info.handle = device->handle;
758 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT;
760 if(is_out){
761 WOD_Close((HWAVEOUT)device->handle);
762 device->parent = read_map(g_out_map, 0);
763 mr = WOD_Open(&info);
764 }else{
765 WID_Close((HWAVEIN)device->handle);
766 device->parent = read_map(g_in_map, 0);
767 mr = WID_Open(&info);
770 if(mr != MMSYSERR_NOERROR){
771 ERR("Opening new default device failed! %u\n", mr);
772 LeaveCriticalSection(&device->lock);
773 return E_FAIL;
776 HeapFree(GetProcessHeap(), 0, info.format);
778 if(!stopped)
779 WINMM_BeginPlaying(device);
781 LeaveCriticalSection(&device->lock);
783 return S_OK;
786 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
787 EDataFlow flow, ERole role, const WCHAR *device_id)
789 UINT i;
791 TRACE("%u %u %s\n", flow, role, wine_dbgstr_w(device_id));
793 if(role != eConsole)
794 return S_OK;
796 EnterCriticalSection(&g_devthread_lock);
798 if(flow == eRender)
799 update_mapping(&g_out_map, g_outmmdevices_count, device_id);
800 else
801 update_mapping(&g_in_map, g_inmmdevices_count, device_id);
803 for(i = 0; i < MAX_DEVICES && g_out_mapper_devices[i]; ++i)
804 reroute_mapper_device(g_out_mapper_devices[i], TRUE);
806 for(i = 0; i < MAX_DEVICES && g_in_mapper_devices[i]; ++i)
807 reroute_mapper_device(g_in_mapper_devices[i], FALSE);
809 LeaveCriticalSection(&g_devthread_lock);
811 return S_OK;
814 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
815 const WCHAR *device_id, const PROPERTYKEY key)
817 TRACE("Ignoring OnPropertyValueChanged callback\n");
818 return S_OK;
821 static IMMNotificationClientVtbl g_notif_vtbl = {
822 notif_QueryInterface,
823 notif_AddRef,
824 notif_Release,
825 notif_OnDeviceStateChanged,
826 notif_OnDeviceAdded,
827 notif_OnDeviceRemoved,
828 notif_OnDefaultDeviceChanged,
829 notif_OnPropertyValueChanged
832 static IMMNotificationClient g_notif = { &g_notif_vtbl };
834 static HRESULT WINMM_InitMMDevices(void)
836 HRESULT hr, init_hr;
837 IMMDeviceEnumerator *devenum = NULL;
839 if(g_outmmdevices_count || g_inmmdevices_count)
840 return S_FALSE;
842 init_hr = CoInitialize(NULL);
844 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
845 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
846 if(FAILED(hr))
847 goto exit;
849 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum, &g_notif);
850 if(FAILED(hr))
851 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr);
853 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_out_map, &g_outmmdevices_count,
854 eRender, devenum);
855 if(FAILED(hr)){
856 g_outmmdevices_count = 0;
857 g_inmmdevices_count = 0;
858 goto exit;
861 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_in_map, &g_inmmdevices_count,
862 eCapture, devenum);
863 if(FAILED(hr)){
864 g_inmmdevices_count = 0;
865 goto exit;
868 exit:
869 if(devenum)
870 IMMDeviceEnumerator_Release(devenum);
871 if(SUCCEEDED(init_hr))
872 CoUninitialize();
874 return hr;
877 static inline BOOL WINMM_IsMapper(UINT device)
879 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
882 static MMRESULT WINMM_TryDeviceMapping(WINMM_Device *device, WAVEFORMATEX *fmt,
883 WORD channels, DWORD freq, DWORD bits_per_samp, BOOL is_query, BOOL is_out)
885 WAVEFORMATEX target, *closer_fmt = NULL;
886 HRESULT hr;
887 MMRESULT mr;
889 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
890 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
892 target.wFormatTag = WAVE_FORMAT_PCM;
893 target.nChannels = channels;
894 target.nSamplesPerSec = freq;
895 target.wBitsPerSample = bits_per_samp;
896 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
897 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
898 target.cbSize = 0;
900 hr = IAudioClient_IsFormatSupported(device->client,
901 AUDCLNT_SHAREMODE_SHARED, &target, &closer_fmt);
902 CoTaskMemFree(closer_fmt);
903 if(hr != S_OK)
904 return WAVERR_BADFORMAT;
906 /* device supports our target format, so see if MSACM can
907 * do the conversion */
908 if(is_out)
909 mr = acmStreamOpen(&device->acm_handle, NULL, fmt, &target, NULL,
910 0, 0, 0);
911 else
912 mr = acmStreamOpen(&device->acm_handle, NULL, &target, fmt, NULL,
913 0, 0, 0);
914 if(mr != MMSYSERR_NOERROR)
915 return mr;
917 /* yes it can. initialize the audioclient and return success */
918 if(is_query){
919 acmStreamClose(device->acm_handle, 0);
920 device->acm_handle = NULL;
921 return MMSYSERR_NOERROR;
924 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
925 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
926 AC_BUFLEN, 0, &target, &device->parent->session);
927 if(hr != S_OK){
928 WARN("Initialize failed: %08x\n", hr);
929 acmStreamClose(device->acm_handle, 0);
930 device->acm_handle = NULL;
931 return MMSYSERR_ERROR;
934 device->bytes_per_frame = target.nBlockAlign;
935 device->samples_per_sec = target.nSamplesPerSec;
937 TRACE("Success!\n");
939 return MMSYSERR_NOERROR;
942 static MMRESULT WINMM_MapDevice(WINMM_Device *device, BOOL is_query, BOOL is_out)
944 MMRESULT mr;
945 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)device->orig_fmt;
947 TRACE("(%p, %u)\n", device, is_out);
949 /* set up the ACM stream */
950 if(device->orig_fmt->wFormatTag != WAVE_FORMAT_PCM &&
951 !(device->orig_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
952 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
953 /* convert to PCM format if it's not already */
954 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
955 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
956 16, is_query, is_out);
957 if(mr == MMSYSERR_NOERROR)
958 return mr;
960 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
961 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
962 8, is_query, is_out);
963 if(mr == MMSYSERR_NOERROR)
964 return mr;
965 }else{
966 WORD channels;
968 /* first try just changing bit depth and channels */
969 channels = device->orig_fmt->nChannels;
970 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
971 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
972 if(mr == MMSYSERR_NOERROR)
973 return mr;
974 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
975 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
976 if(mr == MMSYSERR_NOERROR)
977 return mr;
979 channels = (channels == 2) ? 1 : 2;
980 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
981 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
982 if(mr == MMSYSERR_NOERROR)
983 return mr;
984 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
985 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
986 if(mr == MMSYSERR_NOERROR)
987 return mr;
989 /* that didn't work, so now try different sample rates */
990 channels = device->orig_fmt->nChannels;
991 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
992 if(mr == MMSYSERR_NOERROR)
993 return mr;
994 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
995 if(mr == MMSYSERR_NOERROR)
996 return mr;
997 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
998 if(mr == MMSYSERR_NOERROR)
999 return mr;
1000 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1001 if(mr == MMSYSERR_NOERROR)
1002 return mr;
1003 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1004 if(mr == MMSYSERR_NOERROR)
1005 return mr;
1007 channels = (channels == 2) ? 1 : 2;
1008 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
1009 if(mr == MMSYSERR_NOERROR)
1010 return mr;
1011 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
1012 if(mr == MMSYSERR_NOERROR)
1013 return mr;
1014 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
1015 if(mr == MMSYSERR_NOERROR)
1016 return mr;
1017 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1018 if(mr == MMSYSERR_NOERROR)
1019 return mr;
1020 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1021 if(mr == MMSYSERR_NOERROR)
1022 return mr;
1024 channels = device->orig_fmt->nChannels;
1025 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1026 if(mr == MMSYSERR_NOERROR)
1027 return mr;
1028 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1029 if(mr == MMSYSERR_NOERROR)
1030 return mr;
1031 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1032 if(mr == MMSYSERR_NOERROR)
1033 return mr;
1034 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1035 if(mr == MMSYSERR_NOERROR)
1036 return mr;
1037 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1038 if(mr == MMSYSERR_NOERROR)
1039 return mr;
1041 channels = (channels == 2) ? 1 : 2;
1042 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1043 if(mr == MMSYSERR_NOERROR)
1044 return mr;
1045 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1046 if(mr == MMSYSERR_NOERROR)
1047 return mr;
1048 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1049 if(mr == MMSYSERR_NOERROR)
1050 return mr;
1051 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1052 if(mr == MMSYSERR_NOERROR)
1053 return mr;
1054 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1055 if(mr == MMSYSERR_NOERROR)
1056 return mr;
1059 WARN("Unable to find compatible device!\n");
1060 return WAVERR_BADFORMAT;
1063 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_OpenInfo *info,
1064 BOOL is_out)
1066 LRESULT ret = MMSYSERR_NOMEM;
1067 HRESULT hr;
1069 hr = IMMDeviceEnumerator_GetDevice(g_devenum, device->parent->dev_id,
1070 &device->device);
1071 if(FAILED(hr)){
1072 WARN("Device %s (%s) unavailable: %08x\n",
1073 wine_dbgstr_w(device->parent->dev_id),
1074 wine_dbgstr_w(device->parent->out_caps.szPname), hr);
1075 ret = MMSYSERR_NODRIVER;
1076 goto error;
1079 /* this is where winexyz.drv opens the audio device */
1080 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
1081 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
1082 if(FAILED(hr)){
1083 WARN("Activate failed: %08x\n", hr);
1084 ret = hr2mmr(hr);
1085 if(ret == MMSYSERR_ERROR)
1086 ret = MMSYSERR_NOTENABLED;
1087 goto error;
1090 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
1092 if (info->format->nSamplesPerSec == 0)
1094 ret = MMSYSERR_INVALPARAM;
1095 goto error;
1098 /* we aren't guaranteed that the struct in lpFormat is a full
1099 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1100 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
1101 memcpy(device->orig_fmt, info->format, sizeof(PCMWAVEFORMAT));
1102 device->orig_fmt->cbSize = 0;
1103 if(device->orig_fmt->wBitsPerSample % 8 != 0){
1104 WARN("Fixing bad wBitsPerSample (%u)\n", device->orig_fmt->wBitsPerSample);
1105 device->orig_fmt->wBitsPerSample = (device->orig_fmt->wBitsPerSample + 7) & ~7;
1107 /* winmm ignores broken blockalign and avgbytes */
1108 if(device->orig_fmt->nBlockAlign != device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8){
1109 WARN("Fixing bad nBlockAlign (%u)\n", device->orig_fmt->nBlockAlign);
1110 device->orig_fmt->nBlockAlign = device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8;
1112 if (device->orig_fmt->nAvgBytesPerSec != device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign) {
1113 WARN("Fixing bad nAvgBytesPerSec (%u)\n", device->orig_fmt->nAvgBytesPerSec);
1114 device->orig_fmt->nAvgBytesPerSec = device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign;
1116 }else{
1117 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0,
1118 sizeof(WAVEFORMATEX) + info->format->cbSize);
1119 memcpy(device->orig_fmt, info->format,
1120 sizeof(WAVEFORMATEX) + info->format->cbSize);
1123 if(info->flags & WAVE_FORMAT_QUERY){
1124 WAVEFORMATEX *closer_fmt = NULL;
1126 hr = IAudioClient_IsFormatSupported(device->client,
1127 AUDCLNT_SHAREMODE_SHARED, device->orig_fmt, &closer_fmt);
1128 CoTaskMemFree(closer_fmt);
1129 if((hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT) && !(info->flags & WAVE_FORMAT_DIRECT))
1130 ret = WINMM_MapDevice(device, TRUE, is_out);
1131 else
1132 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1133 goto error;
1136 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1137 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1138 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1139 if(FAILED(hr)){
1140 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1141 ret = WINMM_MapDevice(device, FALSE, is_out);
1142 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1143 goto error;
1144 }else{
1145 WARN("Initialize failed: %08x\n", hr);
1146 ret = hr2mmr(hr);
1147 goto error;
1149 }else{
1150 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1151 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1154 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1155 (void**)&device->clock);
1156 if(FAILED(hr)){
1157 WARN("GetService failed: %08x\n", hr);
1158 goto error;
1161 if(!device->event){
1162 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1163 if(!device->event){
1164 WARN("CreateEvent failed: %08x\n", hr);
1165 goto error;
1168 /* As the devices thread is waiting on g_device_handles, it can
1169 * only be modified from within this same thread. */
1170 if(g_device_handles){
1171 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1172 sizeof(HANDLE) * (g_devhandle_count + 1));
1173 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1174 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1175 }else{
1176 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1177 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1178 sizeof(WINMM_Device *));
1180 g_device_handles[g_devhandle_count] = device->event;
1181 g_handle_devices[g_devhandle_count] = device;
1182 ++g_devhandle_count;
1185 hr = IAudioClient_SetEventHandle(device->client, device->event);
1186 if(FAILED(hr)){
1187 WARN("SetEventHandle failed: %08x\n", hr);
1188 goto error;
1191 if(info->reset){
1192 device->played_frames = 0;
1193 device->ofs_bytes = 0;
1194 device->loop_counter = 0;
1195 device->first = device->last = device->playing = device->loop_start = NULL;
1198 device->stopped = TRUE;
1199 device->last_clock_pos = 0;
1201 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1202 device->cb_info.callback = info->callback;
1203 device->cb_info.user = info->cb_user;
1204 device->cb_info.hwave = device->handle;
1206 info->handle = device->handle;
1208 return MMSYSERR_NOERROR;
1210 error:
1211 if(device->client){
1212 IAudioClient_Release(device->client);
1213 device->client = NULL;
1215 if(device->device){
1216 IMMDevice_Release(device->device);
1217 device->device = NULL;
1220 return ret;
1223 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1225 WINMM_Device *device;
1226 LRESULT ret = MMSYSERR_ERROR;
1227 HRESULT hr;
1229 if(info->handle != 0){
1230 device = WINMM_GetDeviceFromHWAVE(info->handle);
1231 if(!device){
1232 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1233 return MMSYSERR_ERROR;
1236 EnterCriticalSection(&device->lock);
1238 device->open = TRUE;
1239 }else{
1240 CRITICAL_SECTION *lock;
1241 UINT internal_index;
1242 WINMM_Device **devices;
1243 WINMM_MMDevice *mmdevice;
1245 if(WINMM_IsMapper(info->req_device)){
1246 if (g_outmmdevices_count == 0)
1247 return MMSYSERR_BADDEVICEID;
1248 devices = g_out_mapper_devices;
1249 mmdevice = read_map(g_out_map, 0);
1250 lock = &g_devthread_lock;
1251 internal_index = MAPPER_INDEX;
1252 }else{
1253 if(info->req_device >= g_outmmdevices_count)
1254 return MMSYSERR_BADDEVICEID;
1256 mmdevice = read_map(g_out_map, info->req_device);
1258 if(!mmdevice->out_caps.szPname[0])
1259 return MMSYSERR_NOTENABLED;
1261 devices = mmdevice->devices;
1262 lock = &mmdevice->lock;
1263 internal_index = mmdevice->index;
1266 EnterCriticalSection(lock);
1268 device = WINMM_FindUnusedDevice(devices, mmdevice,
1269 internal_index, TRUE);
1270 if(!device){
1271 LeaveCriticalSection(lock);
1272 return MMSYSERR_ALLOCATED;
1275 LeaveCriticalSection(lock);
1278 ret = WINMM_OpenDevice(device, info, TRUE);
1279 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1280 goto error;
1281 ret = MMSYSERR_ERROR;
1283 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1284 (void**)&device->render);
1285 if(FAILED(hr)){
1286 ERR("GetService failed: %08x\n", hr);
1287 goto error;
1290 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1291 (void**)&device->volume);
1292 if(FAILED(hr)){
1293 ERR("GetService failed: %08x\n", hr);
1294 goto error;
1297 LeaveCriticalSection(&device->lock);
1299 return MMSYSERR_NOERROR;
1301 error:
1302 if(device->device){
1303 IMMDevice_Release(device->device);
1304 device->device = NULL;
1306 if(device->client){
1307 IAudioClient_Release(device->client);
1308 device->client = NULL;
1310 if(device->render){
1311 IAudioRenderClient_Release(device->render);
1312 device->render = NULL;
1314 if(device->volume){
1315 IAudioStreamVolume_Release(device->volume);
1316 device->volume = NULL;
1318 if(device->clock){
1319 IAudioClock_Release(device->clock);
1320 device->clock = NULL;
1322 device->open = FALSE;
1323 LeaveCriticalSection(&device->lock);
1324 return ret;
1327 static LRESULT WID_Open(WINMM_OpenInfo *info)
1329 WINMM_Device *device, **devices;
1330 WINMM_MMDevice *mmdevice;
1331 UINT internal_index;
1332 CRITICAL_SECTION *lock;
1333 LRESULT ret = MMSYSERR_ERROR;
1334 HRESULT hr;
1336 if(WINMM_IsMapper(info->req_device)){
1337 if (g_inmmdevices_count == 0)
1338 return MMSYSERR_BADDEVICEID;
1339 devices = g_in_mapper_devices;
1340 mmdevice = read_map(g_in_map, 0);
1341 lock = &g_devthread_lock;
1342 internal_index = MAPPER_INDEX;
1343 }else{
1344 if(info->req_device >= g_inmmdevices_count)
1345 return MMSYSERR_BADDEVICEID;
1347 mmdevice = read_map(g_in_map, info->req_device);
1349 if(!mmdevice->in_caps.szPname[0])
1350 return MMSYSERR_NOTENABLED;
1352 devices = mmdevice->devices;
1353 lock = &mmdevice->lock;
1354 internal_index = mmdevice->index;
1357 EnterCriticalSection(lock);
1359 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1360 if(!device){
1361 LeaveCriticalSection(lock);
1362 return MMSYSERR_ALLOCATED;
1365 LeaveCriticalSection(lock);
1367 ret = WINMM_OpenDevice(device, info, FALSE);
1368 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1369 goto error;
1370 ret = MMSYSERR_ERROR;
1372 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1373 (void**)&device->capture);
1374 if(FAILED(hr)){
1375 WARN("GetService failed: %08x\n", hr);
1376 goto error;
1379 LeaveCriticalSection(&device->lock);
1381 return MMSYSERR_NOERROR;
1383 error:
1384 if(device->device){
1385 IMMDevice_Release(device->device);
1386 device->device = NULL;
1388 if(device->client){
1389 IAudioClient_Release(device->client);
1390 device->client = NULL;
1392 if(device->capture){
1393 IAudioCaptureClient_Release(device->capture);
1394 device->capture = NULL;
1396 if(device->clock){
1397 IAudioClock_Release(device->clock);
1398 device->clock = NULL;
1400 device->open = FALSE;
1401 LeaveCriticalSection(&device->lock);
1402 return ret;
1405 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1407 device->open = FALSE;
1409 if(!device->stopped){
1410 IAudioClient_Stop(device->client);
1411 device->stopped = TRUE;
1414 if(device->acm_handle){
1415 acmStreamClose(device->acm_handle, 0);
1416 device->acm_handle = NULL;
1419 IMMDevice_Release(device->device);
1420 device->device = NULL;
1422 IAudioClient_Release(device->client);
1423 device->client = NULL;
1425 IAudioClock_Release(device->clock);
1426 device->clock = NULL;
1428 HeapFree(GetProcessHeap(), 0, device->orig_fmt);
1430 return S_OK;
1433 static LRESULT WOD_Close(HWAVEOUT hwave)
1435 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1437 TRACE("(%p)\n", hwave);
1439 if(!WINMM_ValidateAndLock(device))
1440 return MMSYSERR_INVALHANDLE;
1442 WINMM_CloseDevice(device);
1444 IAudioRenderClient_Release(device->render);
1445 device->render = NULL;
1447 IAudioStreamVolume_Release(device->volume);
1448 device->volume = NULL;
1450 LeaveCriticalSection(&device->lock);
1452 return MMSYSERR_NOERROR;
1455 static LRESULT WID_Close(HWAVEIN hwave)
1457 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1459 TRACE("(%p)\n", hwave);
1461 if(!WINMM_ValidateAndLock(device))
1462 return MMSYSERR_INVALHANDLE;
1464 WINMM_CloseDevice(device);
1466 IAudioCaptureClient_Release(device->capture);
1467 device->capture = NULL;
1469 LeaveCriticalSection(&device->lock);
1471 return MMSYSERR_NOERROR;
1474 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1476 return length - length % device->bytes_per_frame;
1479 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1481 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1483 TRACE("(%p, %p)\n", hwave, header);
1485 if(!WINMM_ValidateAndLock(device))
1486 return MMSYSERR_INVALHANDLE;
1488 if(device->render && device->acm_handle){
1489 ACMSTREAMHEADER *ash;
1490 DWORD size;
1491 MMRESULT mr;
1493 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1494 ACM_STREAMSIZEF_SOURCE);
1495 if(mr != MMSYSERR_NOERROR){
1496 LeaveCriticalSection(&device->lock);
1497 return mr;
1500 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1501 if(!ash){
1502 LeaveCriticalSection(&device->lock);
1503 return MMSYSERR_NOMEM;
1506 ash->cbStruct = sizeof(*ash);
1507 ash->fdwStatus = 0;
1508 ash->dwUser = (DWORD_PTR)header;
1509 ash->pbSrc = (BYTE*)header->lpData;
1510 ash->cbSrcLength = header->dwBufferLength;
1511 ash->dwSrcUser = header->dwUser;
1512 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1513 ash->cbDstLength = size;
1514 ash->dwDstUser = 0;
1516 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1517 if(mr != MMSYSERR_NOERROR){
1518 HeapFree(GetProcessHeap(), 0, ash);
1519 LeaveCriticalSection(&device->lock);
1520 return mr;
1523 header->reserved = (DWORD_PTR)ash;
1526 LeaveCriticalSection(&device->lock);
1528 header->dwFlags |= WHDR_PREPARED;
1529 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1531 return MMSYSERR_NOERROR;
1534 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1536 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1538 TRACE("(%p, %p)\n", hwave, header);
1540 if(!WINMM_ValidateAndLock(device))
1541 return MMSYSERR_INVALHANDLE;
1543 if(device->render && device->acm_handle){
1544 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1546 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1548 HeapFree(GetProcessHeap(), 0, ash);
1551 LeaveCriticalSection(&device->lock);
1553 header->dwFlags &= ~WHDR_PREPARED;
1555 return MMSYSERR_NOERROR;
1558 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1560 if(device->acm_handle){
1561 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1562 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1565 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1568 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1570 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1573 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1575 HRESULT hr;
1576 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1577 UINT64 clock_freq, clock_pos, clock_frames;
1578 UINT32 nloops, queue_frames = 0;
1580 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1581 if(FAILED(hr)){
1582 WARN("GetFrequency failed: %08x\n", hr);
1583 return NULL;
1586 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1587 if(FAILED(hr)){
1588 WARN("GetPosition failed: %08x\n", hr);
1589 return NULL;
1592 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1594 nloops = device->loop_counter;
1595 while(queue &&
1596 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1597 clock_frames - device->last_clock_pos + device->remainder_frames){
1598 if(!nloops){
1599 last = queue;
1600 device->last_clock_pos += queue_frames;
1601 device->remainder_frames = 0;
1602 queue_frames = 0;
1603 queue = device->first = queue->lpNext;
1604 }else{
1605 if(queue->dwFlags & WHDR_BEGINLOOP){
1606 if(queue->dwFlags & WHDR_ENDLOOP)
1607 --nloops;
1608 else
1609 queue = queue->lpNext;
1610 }else if(queue->dwFlags & WHDR_ENDLOOP){
1611 queue = device->loop_start;
1612 --nloops;
1617 if(last){
1618 last->lpNext = NULL;
1619 return first;
1620 }else
1621 return NULL;
1624 static void WOD_PushData(WINMM_Device *device)
1626 WINMM_CBInfo cb_info;
1627 HRESULT hr;
1628 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1629 UINT32 queue_bytes, nloops;
1630 BYTE *data;
1631 WAVEHDR *queue, *first = NULL;
1633 TRACE("(%p)\n", device->handle);
1635 EnterCriticalSection(&device->lock);
1637 if(!device->device)
1638 goto exit;
1640 if(!device->first){
1641 if (device->stopped)
1642 goto exit;
1643 device->stopped = TRUE;
1644 device->last_clock_pos = 0;
1645 IAudioClient_Stop(device->client);
1646 IAudioClient_Reset(device->client);
1647 goto exit;
1650 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1651 if(FAILED(hr)){
1652 WARN("GetBufferSize failed: %08x\n", hr);
1653 goto exit;
1656 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1657 if(FAILED(hr)){
1658 WARN("GetCurrentPadding failed: %08x\n", hr);
1659 goto exit;
1662 first = WOD_MarkDoneHeaders(device);
1664 /* determine which is larger between the available buffer size and
1665 * the amount of data left in the queue */
1666 avail_frames = bufsize - pad;
1668 queue = device->playing;
1669 ofs = device->ofs_bytes;
1670 queue_frames = 0;
1671 nloops = 0;
1672 while(queue && queue_frames < avail_frames){
1673 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1674 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1675 ofs = 0;
1677 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1678 queue = device->loop_start;
1679 ++nloops;
1680 }else
1681 queue = queue->lpNext;
1684 if(queue_frames < avail_frames)
1685 avail_frames = queue_frames;
1686 if(avail_frames == 0)
1687 goto exit;
1689 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1690 if(FAILED(hr)){
1691 WARN("GetBuffer failed: %08x\n", hr);
1692 goto exit;
1695 written = 0;
1696 while(device->playing && written < avail_frames){
1697 UINT32 copy_frames, copy_bytes;
1698 BYTE *queue_data;
1700 queue = device->playing;
1702 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1703 if(device->acm_handle)
1704 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1705 else
1706 queue_data = (BYTE*)queue->lpData;
1708 queue_frames = (queue_bytes - device->ofs_bytes) /
1709 device->bytes_per_frame;
1711 copy_frames = queue_frames < (avail_frames - written) ?
1712 queue_frames : avail_frames - written;
1713 copy_bytes = copy_frames * device->bytes_per_frame;
1715 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1717 data += copy_bytes;
1718 written += copy_frames;
1719 device->ofs_bytes += copy_bytes;
1721 if(device->ofs_bytes >= queue_bytes){
1722 device->ofs_bytes = 0;
1724 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1725 device->playing = queue->lpNext;
1726 else{
1727 if(queue->dwFlags & WHDR_BEGINLOOP){
1728 device->loop_start = device->playing;
1729 device->playing = queue->lpNext;
1730 device->loop_counter = queue->dwLoops;
1732 if(queue->dwFlags & WHDR_ENDLOOP){
1733 --device->loop_counter;
1734 if(device->loop_counter)
1735 device->playing = device->loop_start;
1736 else
1737 device->loop_start = device->playing = queue->lpNext;
1743 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1744 if(FAILED(hr)){
1745 WARN("ReleaseBuffer failed: %08x\n", hr);
1746 goto exit;
1749 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1750 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1751 else
1752 device->played_frames += avail_frames;
1754 exit:
1755 cb_info = device->cb_info;
1757 LeaveCriticalSection(&device->lock);
1759 while(first){
1760 WAVEHDR *next = first->lpNext;
1761 first->dwFlags &= ~WHDR_INQUEUE;
1762 first->dwFlags |= WHDR_DONE;
1763 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1764 first = next;
1768 static void WID_PullACMData(WINMM_Device *device)
1770 UINT32 packet, packet_bytes;
1771 DWORD flags;
1772 BYTE *data;
1773 WAVEHDR *queue;
1774 HRESULT hr;
1775 MMRESULT mr;
1777 if(device->acm_hdr.cbDstLength == 0){
1778 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1779 &flags, NULL, NULL);
1780 if(hr != S_OK){
1781 if(FAILED(hr))
1782 WARN("GetBuffer failed: %08x\n", hr);
1783 return;
1786 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1787 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1789 device->acm_offs = 0;
1791 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1792 device->acm_hdr.fdwStatus = 0;
1793 device->acm_hdr.dwUser = 0;
1794 device->acm_hdr.pbSrc = data;
1795 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1796 device->acm_hdr.cbSrcLengthUsed = 0;
1797 device->acm_hdr.dwSrcUser = 0;
1798 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1799 device->acm_hdr.cbDstLength = packet_bytes;
1800 device->acm_hdr.cbDstLengthUsed = 0;
1801 device->acm_hdr.dwDstUser = 0;
1803 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1804 if(mr != MMSYSERR_NOERROR){
1805 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1806 return;
1809 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1810 if(mr != MMSYSERR_NOERROR){
1811 WARN("acmStreamConvert failed: %d\n", mr);
1812 return;
1815 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1816 if(FAILED(hr))
1817 WARN("ReleaseBuffer failed: %08x\n", hr);
1819 device->played_frames += packet;
1822 queue = device->first;
1823 while(queue){
1824 UINT32 to_copy_bytes;
1826 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1827 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1829 memcpy(queue->lpData + queue->dwBytesRecorded,
1830 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1832 queue->dwBytesRecorded += to_copy_bytes;
1833 device->acm_offs += to_copy_bytes;
1835 if(queue->dwBufferLength - queue->dwBytesRecorded <
1836 device->bytes_per_frame){
1837 queue->dwFlags &= ~WHDR_INQUEUE;
1838 queue->dwFlags |= WHDR_DONE;
1839 device->first = queue = queue->lpNext;
1842 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1843 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1844 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1845 device->acm_hdr.cbDstLength = 0;
1846 device->acm_hdr.cbDstLengthUsed = 0;
1848 /* done with this ACM Header, so try to pull more data */
1849 WID_PullACMData(device);
1850 return;
1854 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1855 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1856 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1857 device->acm_hdr.cbDstLength = 0;
1858 device->acm_hdr.cbDstLengthUsed = 0;
1861 static void WID_PullData(WINMM_Device *device)
1863 WINMM_CBInfo cb_info;
1864 WAVEHDR *queue, *first = NULL, *last = NULL;
1865 HRESULT hr;
1867 TRACE("(%p)\n", device->handle);
1869 EnterCriticalSection(&device->lock);
1871 if(!device->device || !device->first)
1872 goto exit;
1874 first = device->first;
1876 if(device->acm_handle){
1877 WID_PullACMData(device);
1878 goto exit;
1881 while(device->first){
1882 BYTE *data;
1883 UINT32 packet_len, packet;
1884 DWORD flags;
1886 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1887 &flags, NULL, NULL);
1888 if(hr != S_OK){
1889 if(FAILED(hr))
1890 WARN("GetBuffer failed: %08x\n", hr);
1891 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1892 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1893 goto exit;
1896 packet = packet_len;
1897 queue = device->first;
1898 while(queue && packet > 0){
1899 UINT32 to_copy_bytes;
1901 to_copy_bytes = min(packet * device->bytes_per_frame,
1902 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1904 memcpy(queue->lpData + queue->dwBytesRecorded,
1905 data + (packet_len - packet) * device->bytes_per_frame,
1906 to_copy_bytes);
1908 queue->dwBytesRecorded += to_copy_bytes;
1910 if(queue->dwBufferLength - queue->dwBytesRecorded <
1911 device->bytes_per_frame){
1912 last = queue;
1913 device->first = queue = queue->lpNext;
1916 packet -= to_copy_bytes / device->bytes_per_frame;
1919 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1920 if(FAILED(hr))
1921 WARN("ReleaseBuffer failed: %08x\n", hr);
1923 if(packet > 0)
1924 WARN("losing %u frames\n", packet);
1925 device->played_frames += packet_len - packet;
1928 exit:
1929 cb_info = device->cb_info;
1931 LeaveCriticalSection(&device->lock);
1933 if(last){
1934 last->lpNext = NULL;
1935 while(first){
1936 WAVEHDR *next = first->lpNext;
1937 first->dwFlags &= ~WHDR_INQUEUE;
1938 first->dwFlags |= WHDR_DONE;
1939 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1940 first = next;
1945 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1947 HRESULT hr;
1949 TRACE("(%p)\n", device->handle);
1951 if(device->render)
1952 /* prebuffer data before starting */
1953 WOD_PushData(device);
1955 if(device->stopped){
1956 device->stopped = FALSE;
1958 hr = IAudioClient_Start(device->client);
1959 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1960 device->stopped = TRUE;
1961 WARN("Start failed: %08x\n", hr);
1962 return MMSYSERR_ERROR;
1966 return MMSYSERR_NOERROR;
1969 static LRESULT WINMM_Pause(WINMM_Device *device)
1971 HRESULT hr;
1973 TRACE("(%p)\n", device->handle);
1975 hr = IAudioClient_Stop(device->client);
1976 if(FAILED(hr)){
1977 WARN("Stop failed: %08x\n", hr);
1978 return MMSYSERR_ERROR;
1981 device->stopped = FALSE;
1983 return MMSYSERR_NOERROR;
1986 static LRESULT WINMM_Reset(HWAVE hwave)
1988 WINMM_CBInfo cb_info;
1989 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1990 BOOL is_out;
1991 WAVEHDR *first;
1992 HRESULT hr;
1994 TRACE("(%p)\n", hwave);
1996 if(!WINMM_ValidateAndLock(device))
1997 return MMSYSERR_INVALHANDLE;
1999 hr = IAudioClient_Stop(device->client);
2000 if(FAILED(hr)){
2001 LeaveCriticalSection(&device->lock);
2002 WARN("Stop failed: %08x\n", hr);
2003 return MMSYSERR_ERROR;
2005 device->stopped = TRUE;
2007 first = device->first;
2008 device->first = device->last = device->playing = NULL;
2009 device->ofs_bytes = 0;
2010 device->played_frames = 0;
2011 device->loop_counter = 0;
2012 device->last_clock_pos = 0;
2013 IAudioClient_Reset(device->client);
2015 cb_info = device->cb_info;
2016 is_out = device->render != NULL;
2018 LeaveCriticalSection(&device->lock);
2020 while(first){
2021 WAVEHDR *next = first->lpNext;
2022 first->dwFlags &= ~WHDR_INQUEUE;
2023 first->dwFlags |= WHDR_DONE;
2024 if(is_out)
2025 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2026 else
2027 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2028 first = next;
2031 return MMSYSERR_NOERROR;
2034 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2035 UINT32 sample_rate, UINT32 bytes_per_sec)
2037 switch(time->wType){
2038 case TIME_SAMPLES:
2039 time->u.sample = played_frames;
2040 return MMSYSERR_NOERROR;
2041 default:
2042 time->wType = TIME_BYTES;
2043 /* fall through */
2044 case TIME_BYTES:
2045 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2046 return MMSYSERR_NOERROR;
2050 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2052 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2053 UINT32 played_frames, sample_rate, bytes_per_sec;
2055 TRACE("(%p, %p)\n", hwave, time);
2057 if(!WINMM_ValidateAndLock(device))
2058 return MMSYSERR_INVALHANDLE;
2060 played_frames = device->played_frames;
2061 sample_rate = device->orig_fmt->nSamplesPerSec;
2062 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2064 LeaveCriticalSection(&device->lock);
2066 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2069 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2070 UINT *mmdev_index)
2072 UINT mmdev, dev, junk, *out;
2073 BOOL is_out;
2075 if(!mmdev_index)
2076 out = &mmdev;
2077 else
2078 out = mmdev_index;
2080 switch(flags & 0xF0000000){
2081 case MIXER_OBJECTF_MIXER: /* == 0 */
2082 *out = HandleToULong(hmix);
2083 if(*out < g_outmmdevices_count)
2084 return read_map(g_out_map, *out);
2085 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2086 *out -= g_outmmdevices_count;
2087 return read_map(g_in_map, *out);
2089 /* fall through -- if it's not a valid mixer device, then
2090 * it could be a valid mixer handle. windows seems to do
2091 * this as well. */
2092 case MIXER_OBJECTF_HMIXER:
2093 case MIXER_OBJECTF_HWAVEOUT:
2094 case MIXER_OBJECTF_HWAVEIN:
2095 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2096 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2097 (!is_out && *out >= g_inmmdevices_count))
2098 return NULL;
2099 if(is_out)
2100 return read_map(g_out_map, *out);
2101 return read_map(g_in_map, *out);
2102 case MIXER_OBJECTF_WAVEOUT:
2103 *out = HandleToULong(hmix);
2104 if(*out < g_outmmdevices_count)
2105 return read_map(g_out_map, *out);
2106 return NULL;
2107 case MIXER_OBJECTF_WAVEIN:
2108 *out = HandleToULong(hmix);
2109 if(*out < g_inmmdevices_count)
2110 return read_map(g_in_map, *out);
2111 return NULL;
2114 return NULL;
2117 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2119 IAudioSessionManager *sesman;
2120 IMMDevice *device;
2121 HRESULT hr;
2123 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2124 if(FAILED(hr)){
2125 WARN("Device %s (%s) unavailable: %08x\n",
2126 wine_dbgstr_w(mmdevice->dev_id),
2127 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2128 return MMSYSERR_ERROR;
2131 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2132 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2133 if(FAILED(hr)){
2134 WARN("Activate failed: %08x\n", hr);
2135 IMMDevice_Release(device);
2136 return MMSYSERR_ERROR;
2139 IMMDevice_Release(device);
2141 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2142 FALSE, &mmdevice->volume);
2143 IAudioSessionManager_Release(sesman);
2144 if(FAILED(hr)){
2145 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2146 return MMSYSERR_ERROR;
2149 return MMSYSERR_NOERROR;
2152 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2154 WINMM_MMDevice *mmdevice;
2155 MIXERCONTROLDETAILS *control = details->details;
2156 HRESULT hr;
2158 TRACE("(%p)\n", details->hmix);
2160 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2161 if(!mmdevice)
2162 return MMSYSERR_INVALHANDLE;
2164 EnterCriticalSection(&mmdevice->lock);
2166 if(!mmdevice->volume){
2167 MMRESULT mr;
2169 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2170 if(mr != MMSYSERR_NOERROR){
2171 LeaveCriticalSection(&mmdevice->lock);
2172 return mr;
2176 if(control->dwControlID == 0){
2177 float vol;
2178 MIXERCONTROLDETAILS_UNSIGNED *udet;
2180 if(!control->paDetails ||
2181 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2182 LeaveCriticalSection(&mmdevice->lock);
2183 return MMSYSERR_INVALPARAM;
2186 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2187 if(FAILED(hr)){
2188 WARN("GetMasterVolume failed: %08x\n", hr);
2189 LeaveCriticalSection(&mmdevice->lock);
2190 return MMSYSERR_ERROR;
2193 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2194 udet->dwValue = vol * ((unsigned int)0xFFFF);
2195 }else if(control->dwControlID == 1){
2196 BOOL mute;
2197 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2199 if(!control->paDetails ||
2200 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2201 LeaveCriticalSection(&mmdevice->lock);
2202 return MMSYSERR_INVALPARAM;
2205 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2206 if(FAILED(hr)){
2207 WARN("GetMute failed: %08x\n", hr);
2208 LeaveCriticalSection(&mmdevice->lock);
2209 return MMSYSERR_ERROR;
2212 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2213 bdet->fValue = mute;
2214 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2215 FIXME("What should the sw-side mixer controls map to?\n");
2216 }else{
2217 LeaveCriticalSection(&mmdevice->lock);
2218 return MIXERR_INVALCONTROL;
2221 LeaveCriticalSection(&mmdevice->lock);
2223 return MMSYSERR_NOERROR;
2226 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2228 WINMM_MMDevice *mmdevice;
2229 MIXERCONTROLDETAILS *control = details->details;
2230 HRESULT hr;
2232 TRACE("(%p)\n", details->hmix);
2234 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2235 if(!mmdevice)
2236 return MMSYSERR_INVALHANDLE;
2238 EnterCriticalSection(&mmdevice->lock);
2240 if(!mmdevice->volume){
2241 MMRESULT mr;
2243 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2244 if(mr != MMSYSERR_NOERROR){
2245 LeaveCriticalSection(&mmdevice->lock);
2246 return mr;
2250 if(control->dwControlID == 0){
2251 float vol;
2252 MIXERCONTROLDETAILS_UNSIGNED *udet;
2254 if(!control->paDetails ||
2255 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2256 LeaveCriticalSection(&mmdevice->lock);
2257 return MMSYSERR_INVALPARAM;
2260 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2262 if(udet->dwValue > 65535){
2263 LeaveCriticalSection(&mmdevice->lock);
2264 return MMSYSERR_INVALPARAM;
2267 vol = udet->dwValue / 65535.f;
2269 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2270 if(FAILED(hr)){
2271 WARN("SetMasterVolume failed: %08x\n", hr);
2272 LeaveCriticalSection(&mmdevice->lock);
2273 return MMSYSERR_ERROR;
2275 }else if(control->dwControlID == 1){
2276 BOOL mute;
2277 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2279 if(!control->paDetails ||
2280 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2281 LeaveCriticalSection(&mmdevice->lock);
2282 return MMSYSERR_INVALPARAM;
2285 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2286 mute = bdet->fValue;
2288 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2289 if(FAILED(hr)){
2290 WARN("SetMute failed: %08x\n", hr);
2291 LeaveCriticalSection(&mmdevice->lock);
2292 return MMSYSERR_ERROR;
2294 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2295 FIXME("What should the sw-side mixer controls map to?\n");
2296 }else{
2297 LeaveCriticalSection(&mmdevice->lock);
2298 return MIXERR_INVALCONTROL;
2301 LeaveCriticalSection(&mmdevice->lock);
2303 return MMSYSERR_NOERROR;
2306 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2308 WINMM_MMDevice *mmdevice;
2309 IMMDevice *device;
2310 IPropertyStore *ps;
2311 PROPVARIANT pv;
2312 DWORD len_bytes;
2313 HRESULT hr;
2315 static const PROPERTYKEY deviceinterface_key = {
2316 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2319 if(WINMM_IsMapper(info->index)){
2320 if(info->str){
2321 if(*info->len_bytes < sizeof(WCHAR))
2322 return MMSYSERR_INVALPARAM;
2323 *info->str = 0;
2324 }else
2325 *info->len_bytes = sizeof(WCHAR);
2326 return MMSYSERR_NOERROR;
2329 if(info->is_out){
2330 if(info->index >= g_outmmdevices_count)
2331 return MMSYSERR_INVALHANDLE;
2333 mmdevice = &g_out_mmdevices[info->index];
2334 }else{
2335 if(info->index >= g_inmmdevices_count)
2336 return MMSYSERR_INVALHANDLE;
2338 mmdevice = &g_in_mmdevices[info->index];
2341 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2342 &device);
2343 if(FAILED(hr)){
2344 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2345 return MMSYSERR_ERROR;
2348 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2349 if(FAILED(hr)){
2350 WARN("OpenPropertyStore failed: %08x\n", hr);
2351 IMMDevice_Release(device);
2352 return MMSYSERR_ERROR;
2355 PropVariantInit(&pv);
2356 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2357 if(FAILED(hr)){
2358 WARN("GetValue failed: %08x\n", hr);
2359 IPropertyStore_Release(ps);
2360 IMMDevice_Release(device);
2361 return MMSYSERR_ERROR;
2363 if(pv.vt != VT_LPWSTR){
2364 WARN("Got unexpected property type: %u\n", pv.vt);
2365 PropVariantClear(&pv);
2366 IPropertyStore_Release(ps);
2367 IMMDevice_Release(device);
2368 return MMSYSERR_ERROR;
2371 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2373 if(info->str){
2374 if(len_bytes > *info->len_bytes){
2375 PropVariantClear(&pv);
2376 IPropertyStore_Release(ps);
2377 IMMDevice_Release(device);
2378 return MMSYSERR_INVALPARAM;
2381 memcpy(info->str, pv.u.pwszVal, len_bytes);
2382 }else
2383 *info->len_bytes = len_bytes;
2385 PropVariantClear(&pv);
2386 IPropertyStore_Release(ps);
2387 IMMDevice_Release(device);
2389 return MMSYSERR_NOERROR;
2392 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2393 LPARAM lparam)
2395 switch(msg){
2396 case WODM_OPEN:
2397 return WOD_Open((WINMM_OpenInfo*)wparam);
2398 case WODM_CLOSE:
2399 return WOD_Close((HWAVEOUT)wparam);
2400 case WIDM_OPEN:
2401 return WID_Open((WINMM_OpenInfo*)wparam);
2402 case WIDM_CLOSE:
2403 return WID_Close((HWAVEIN)wparam);
2404 case MXDM_GETCONTROLDETAILS:
2405 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2406 case MXDM_SETCONTROLDETAILS:
2407 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2408 case DRV_QUERYDEVICEINTERFACESIZE:
2409 case DRV_QUERYDEVICEINTERFACE:
2410 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2412 return DefWindowProcW(hwnd, msg, wparam, lparam);
2415 static BOOL WINMM_DevicesThreadDone(void)
2417 UINT i;
2419 EnterCriticalSection(&g_devthread_lock);
2421 if(g_devthread_token > 0){
2422 LeaveCriticalSection(&g_devthread_lock);
2423 return FALSE;
2426 for(i = 0; i < g_devhandle_count; ++i){
2427 if(g_handle_devices[i]->open){
2428 LeaveCriticalSection(&g_devthread_lock);
2429 return FALSE;
2433 DestroyWindow(g_devices_hwnd);
2434 g_devices_hwnd = NULL;
2435 IMMDeviceEnumerator_Release(g_devenum);
2436 g_devenum = NULL;
2437 CoUninitialize();
2439 LeaveCriticalSection(&g_devthread_lock);
2441 return TRUE;
2444 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2446 HANDLE evt = arg;
2447 HRESULT hr;
2449 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2450 if(FAILED(hr)){
2451 WARN("CoInitializeEx failed: %08x\n", hr);
2452 FreeLibraryAndExitThread(g_devthread_module, 1);
2455 hr = WINMM_InitMMDevices();
2456 if(FAILED(hr)){
2457 CoUninitialize();
2458 FreeLibraryAndExitThread(g_devthread_module, 1);
2461 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2462 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2463 if(FAILED(hr)){
2464 WARN("CoCreateInstance failed: %08x\n", hr);
2465 CoUninitialize();
2466 FreeLibraryAndExitThread(g_devthread_module, 1);
2469 g_devices_hwnd = CreateWindowW(L"Message", NULL, 0, 0, 0, 0, 0,
2470 HWND_MESSAGE, NULL, NULL, NULL);
2471 if(!g_devices_hwnd){
2472 WARN("CreateWindow failed: %d\n", GetLastError());
2473 CoUninitialize();
2474 FreeLibraryAndExitThread(g_devthread_module, 1);
2477 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2478 (LONG_PTR)WINMM_DevicesMsgProc);
2480 /* inform caller that the thread is ready to process messages */
2481 SetEvent(evt);
2482 evt = NULL; /* do not use after this point */
2484 while(1){
2485 DWORD wait;
2486 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2487 FALSE, INFINITE, QS_ALLINPUT);
2488 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2489 MSG msg;
2490 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2491 WARN("Unexpected message: 0x%x\n", msg.message);
2492 if(!g_devices_hwnd)
2493 break;
2494 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2495 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2496 if(device->render)
2497 WOD_PushData(device);
2498 else
2499 WID_PullData(device);
2500 }else
2501 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2502 GetLastError());
2503 if(WINMM_DevicesThreadDone()){
2504 TRACE("Quitting devices thread\n");
2505 FreeLibraryAndExitThread(g_devthread_module, 0);
2509 FreeLibraryAndExitThread(g_devthread_module, 0);
2512 /* on success, increments g_devthread_token to prevent
2513 * device thread shutdown. caller must decrement. */
2514 static BOOL WINMM_StartDevicesThread(void)
2516 HANDLE events[2];
2517 DWORD wait;
2519 EnterCriticalSection(&g_devthread_lock);
2521 if(g_devices_hwnd){
2522 wait = WaitForSingleObject(g_devices_thread, 0);
2523 if(wait == WAIT_TIMEOUT){
2524 /* thread still running */
2525 InterlockedIncrement(&g_devthread_token);
2526 LeaveCriticalSection(&g_devthread_lock);
2527 return TRUE;
2529 if(wait != WAIT_OBJECT_0){
2530 /* error */
2531 LeaveCriticalSection(&g_devthread_lock);
2532 return FALSE;
2534 TRACE("Devices thread left dangling message window?\n");
2535 g_devices_hwnd = NULL;
2536 CloseHandle(g_devices_thread);
2537 g_devices_thread = NULL;
2538 }else if(g_devices_thread){
2539 WaitForSingleObject(g_devices_thread, INFINITE);
2540 CloseHandle(g_devices_thread);
2541 g_devices_thread = NULL;
2544 TRACE("Starting up devices thread\n");
2546 /* The devices thread holds a reference to the winmm module
2547 * to prevent it from unloading while it's running. */
2548 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2549 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2551 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2553 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2554 events[0], 0, NULL);
2555 if(!g_devices_thread){
2556 LeaveCriticalSection(&g_devthread_lock);
2557 CloseHandle(events[0]);
2558 FreeLibrary(g_devthread_module);
2559 return FALSE;
2562 events[1] = g_devices_thread;
2563 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2564 CloseHandle(events[0]);
2565 if(wait != WAIT_OBJECT_0){
2566 if(wait == 1 + WAIT_OBJECT_0){
2567 CloseHandle(g_devices_thread);
2568 g_devices_thread = NULL;
2569 g_devices_hwnd = NULL;
2571 LeaveCriticalSection(&g_devthread_lock);
2572 return FALSE;
2575 InterlockedIncrement(&g_devthread_token);
2577 LeaveCriticalSection(&g_devthread_lock);
2579 return TRUE;
2582 /**************************************************************************
2583 * waveOutGetNumDevs [WINMM.@]
2585 UINT WINAPI waveOutGetNumDevs(void)
2587 HRESULT hr = WINMM_InitMMDevices();
2588 if(FAILED(hr))
2589 return 0;
2591 TRACE("count: %u\n", g_outmmdevices_count);
2593 return g_outmmdevices_count;
2596 /**************************************************************************
2597 * waveOutGetDevCapsA [WINMM.@]
2599 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2600 UINT uSize)
2602 WAVEOUTCAPSW wocW;
2603 UINT ret;
2605 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2607 if(!lpCaps)
2608 return MMSYSERR_INVALPARAM;
2610 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2612 if (ret == MMSYSERR_NOERROR) {
2613 WAVEOUTCAPSA wocA;
2614 wocA.wMid = wocW.wMid;
2615 wocA.wPid = wocW.wPid;
2616 wocA.vDriverVersion = wocW.vDriverVersion;
2617 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2618 sizeof(wocA.szPname), NULL, NULL );
2619 wocA.dwFormats = wocW.dwFormats;
2620 wocA.wChannels = wocW.wChannels;
2621 wocA.dwSupport = wocW.dwSupport;
2622 wocA.wReserved1 = wocW.wReserved1;
2623 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2625 return ret;
2628 /**************************************************************************
2629 * waveOutGetDevCapsW [WINMM.@]
2631 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2632 UINT uSize)
2634 WAVEOUTCAPSW mapper_caps, *caps;
2635 HRESULT hr;
2637 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2639 hr = WINMM_InitMMDevices();
2640 if(FAILED(hr))
2641 return MMSYSERR_NODRIVER;
2643 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2645 if(WINMM_IsMapper(uDeviceID)){
2646 mapper_caps.wMid = 0xFF;
2647 mapper_caps.wPid = 0xFF;
2648 mapper_caps.vDriverVersion = 0x00010001;
2649 mapper_caps.dwFormats = 0xFFFFFFFF;
2650 mapper_caps.wReserved1 = 0;
2651 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2652 WAVECAPS_SAMPLEACCURATE;
2653 mapper_caps.wChannels = 2;
2654 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
2656 caps = &mapper_caps;
2657 }else{
2658 if(uDeviceID >= g_outmmdevices_count){
2659 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)uDeviceID);
2661 if(!WINMM_ValidateAndLock(device))
2662 return MMSYSERR_BADDEVICEID;
2664 caps = &device->parent->out_caps;
2666 LeaveCriticalSection(&device->lock);
2667 }else{
2668 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2672 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2674 return MMSYSERR_NOERROR;
2677 /**************************************************************************
2678 * waveOutGetErrorTextA [WINMM.@]
2679 * waveInGetErrorTextA [WINMM.@]
2681 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2683 UINT ret;
2685 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2686 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2687 else
2689 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2690 if (!xstr) ret = MMSYSERR_NOMEM;
2691 else
2693 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2694 if (ret == MMSYSERR_NOERROR)
2695 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2696 HeapFree(GetProcessHeap(), 0, xstr);
2699 return ret;
2702 /**************************************************************************
2703 * waveOutGetErrorTextW [WINMM.@]
2704 * waveInGetErrorTextW [WINMM.@]
2706 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2708 UINT ret = MMSYSERR_BADERRNUM;
2710 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2711 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2712 else if (
2713 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2714 * a warning for the test was always true */
2715 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2716 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2717 if (LoadStringW(hWinMM32Instance,
2718 uError, lpText, uSize) > 0) {
2719 ret = MMSYSERR_NOERROR;
2722 return ret;
2725 /**************************************************************************
2726 * waveOutOpen [WINMM.@]
2727 * All the args/structs have the same layout as the win16 equivalents
2729 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2730 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2731 DWORD_PTR dwInstance, DWORD dwFlags)
2733 LRESULT res;
2734 WINMM_OpenInfo info;
2735 WINMM_CBInfo cb_info;
2737 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2738 dwCallback, dwInstance, dwFlags);
2740 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2741 return MMSYSERR_INVALPARAM;
2743 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2744 if(res != MMSYSERR_NOERROR)
2745 return res;
2747 if(!WINMM_StartDevicesThread())
2748 return MMSYSERR_NODRIVER;
2750 info.handle = 0;
2751 info.format = (WAVEFORMATEX*)lpFormat;
2752 info.callback = dwCallback;
2753 info.cb_user = dwInstance;
2754 info.req_device = uDeviceID;
2755 info.flags = dwFlags;
2756 info.reset = TRUE;
2758 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2759 InterlockedDecrement(&g_devthread_token);
2760 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2761 return res;
2763 if(lphWaveOut)
2764 *lphWaveOut = (HWAVEOUT)info.handle;
2766 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2767 cb_info.callback = dwCallback;
2768 cb_info.user = dwInstance;
2769 cb_info.hwave = info.handle;
2771 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2773 return res;
2776 /**************************************************************************
2777 * waveOutClose [WINMM.@]
2779 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2781 UINT res;
2782 WINMM_Device *device;
2783 WINMM_CBInfo cb_info;
2785 TRACE("(%p)\n", hWaveOut);
2787 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2789 if(!WINMM_ValidateAndLock(device))
2790 return MMSYSERR_INVALHANDLE;
2792 cb_info = device->cb_info;
2794 LeaveCriticalSection(&device->lock);
2796 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2798 if(res == MMSYSERR_NOERROR)
2799 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2801 return res;
2804 /**************************************************************************
2805 * waveOutPrepareHeader [WINMM.@]
2807 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2808 WAVEHDR* lpWaveOutHdr, UINT uSize)
2810 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2812 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2813 return MMSYSERR_INVALPARAM;
2815 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2816 return MMSYSERR_NOERROR;
2818 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2821 /**************************************************************************
2822 * waveOutUnprepareHeader [WINMM.@]
2824 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2825 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2827 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2829 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2830 return MMSYSERR_INVALPARAM;
2832 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2833 return WAVERR_STILLPLAYING;
2835 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2836 return MMSYSERR_NOERROR;
2838 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2841 /**************************************************************************
2842 * waveOutWrite [WINMM.@]
2844 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2846 WINMM_Device *device;
2847 MMRESULT mr;
2849 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2851 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2853 if(!WINMM_ValidateAndLock(device))
2854 return MMSYSERR_INVALHANDLE;
2856 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2857 LeaveCriticalSection(&device->lock);
2858 return WAVERR_UNPREPARED;
2861 if(header->dwFlags & WHDR_INQUEUE){
2862 LeaveCriticalSection(&device->lock);
2863 return WAVERR_STILLPLAYING;
2866 TRACE("dwBufferLength: %u\n", header->dwBufferLength);
2868 if(device->acm_handle){
2869 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2871 ash->cbSrcLength = header->dwBufferLength;
2872 mr = acmStreamConvert(device->acm_handle, ash, 0);
2873 if(mr != MMSYSERR_NOERROR){
2874 LeaveCriticalSection(&device->lock);
2875 return mr;
2879 if(device->first){
2880 device->last->lpNext = header;
2881 device->last = header;
2882 if(!device->playing)
2883 device->playing = header;
2884 }else{
2885 device->playing = device->first = device->last = header;
2886 if(header->dwFlags & WHDR_BEGINLOOP){
2887 device->loop_counter = header->dwLoops;
2888 device->loop_start = header;
2892 header->lpNext = NULL;
2893 header->dwFlags &= ~WHDR_DONE;
2894 header->dwFlags |= WHDR_INQUEUE;
2896 mr = WINMM_BeginPlaying(device);
2898 LeaveCriticalSection(&device->lock);
2900 return mr;
2903 /**************************************************************************
2904 * waveOutBreakLoop [WINMM.@]
2906 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2908 WINMM_Device *device;
2910 TRACE("(%p)\n", hWaveOut);
2912 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2914 if(!WINMM_ValidateAndLock(device))
2915 return MMSYSERR_INVALHANDLE;
2917 device->loop_counter = 0;
2919 LeaveCriticalSection(&device->lock);
2921 return MMSYSERR_NOERROR;
2924 /**************************************************************************
2925 * waveOutPause [WINMM.@]
2927 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2929 WINMM_Device *device;
2930 MMRESULT mr;
2932 TRACE("(%p)\n", hWaveOut);
2934 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2936 if(!WINMM_ValidateAndLock(device))
2937 return MMSYSERR_INVALHANDLE;
2939 mr = WINMM_Pause(device);
2941 LeaveCriticalSection(&device->lock);
2943 return mr;
2946 /**************************************************************************
2947 * waveOutReset [WINMM.@]
2949 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2951 TRACE("(%p)\n", hWaveOut);
2953 return WINMM_Reset((HWAVE)hWaveOut);
2956 /**************************************************************************
2957 * waveOutRestart [WINMM.@]
2959 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2961 WINMM_Device *device;
2962 MMRESULT mr;
2964 TRACE("(%p)\n", hWaveOut);
2966 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2968 if(!WINMM_ValidateAndLock(device))
2969 return MMSYSERR_INVALHANDLE;
2971 device->stopped = TRUE;
2973 mr = WINMM_BeginPlaying(device);
2975 LeaveCriticalSection(&device->lock);
2977 return mr;
2980 /**************************************************************************
2981 * waveOutGetPosition [WINMM.@]
2983 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2984 UINT uSize)
2986 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2988 if(!uSize || !lpTime)
2989 return MMSYSERR_INVALPARAM;
2991 if(uSize < sizeof(MMTIME))
2992 return MMSYSERR_ERROR;
2994 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2997 /**************************************************************************
2998 * waveOutGetPitch [WINMM.@]
3000 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
3002 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3003 return MMSYSERR_NOTSUPPORTED;
3006 /**************************************************************************
3007 * waveOutSetPitch [WINMM.@]
3009 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3011 TRACE("(%p, %08x)\n", hWaveOut, dw);
3013 return MMSYSERR_NOTSUPPORTED;
3016 /**************************************************************************
3017 * waveOutGetPlaybackRate [WINMM.@]
3019 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3021 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3023 return MMSYSERR_NOTSUPPORTED;
3026 /**************************************************************************
3027 * waveOutSetPlaybackRate [WINMM.@]
3029 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3031 TRACE("(%p, %08x)\n", hWaveOut, dw);
3033 return MMSYSERR_NOTSUPPORTED;
3036 /**************************************************************************
3037 * waveOutGetVolume [WINMM.@]
3039 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3041 WINMM_Device *device;
3042 UINT32 channels;
3043 float *vols;
3044 HRESULT hr;
3046 TRACE("(%p, %p)\n", hWaveOut, out);
3048 if(!out)
3049 return MMSYSERR_INVALPARAM;
3051 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3053 if(!WINMM_ValidateAndLock(device))
3054 return MMSYSERR_INVALHANDLE;
3056 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3057 if(FAILED(hr)){
3058 LeaveCriticalSection(&device->lock);
3059 WARN("GetChannelCount failed: %08x\n", hr);
3060 return MMSYSERR_ERROR;
3063 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3064 if(!vols){
3065 LeaveCriticalSection(&device->lock);
3066 return MMSYSERR_NOMEM;
3069 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3070 if(FAILED(hr)){
3071 LeaveCriticalSection(&device->lock);
3072 HeapFree(GetProcessHeap(), 0, vols);
3073 WARN("GetAllVolumes failed: %08x\n", hr);
3074 return MMSYSERR_ERROR;
3077 LeaveCriticalSection(&device->lock);
3079 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3080 if(channels > 1)
3081 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3083 HeapFree(GetProcessHeap(), 0, vols);
3085 return MMSYSERR_NOERROR;
3088 /**************************************************************************
3089 * waveOutSetVolume [WINMM.@]
3091 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3093 WINMM_Device *device;
3094 UINT32 channels;
3095 float *vols;
3096 HRESULT hr;
3098 TRACE("(%p, %08x)\n", hWaveOut, in);
3100 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3102 if(!WINMM_ValidateAndLock(device))
3103 return MMSYSERR_INVALHANDLE;
3105 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3106 if(FAILED(hr)){
3107 LeaveCriticalSection(&device->lock);
3108 WARN("GetChannelCount failed: %08x\n", hr);
3109 return MMSYSERR_ERROR;
3112 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3113 if(!vols){
3114 LeaveCriticalSection(&device->lock);
3115 return MMSYSERR_NOMEM;
3118 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3119 if(FAILED(hr)){
3120 LeaveCriticalSection(&device->lock);
3121 HeapFree(GetProcessHeap(), 0, vols);
3122 WARN("GetAllVolumes failed: %08x\n", hr);
3123 return MMSYSERR_ERROR;
3126 vols[0] = (float)((in & 0xFFFF) / (float)0xFFFF);
3127 if(channels > 1)
3128 vols[1] = (float)((in >> 16) / (float)0xFFFF);
3130 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3131 if(FAILED(hr)){
3132 LeaveCriticalSection(&device->lock);
3133 HeapFree(GetProcessHeap(), 0, vols);
3134 WARN("SetAllVolumes failed: %08x\n", hr);
3135 return MMSYSERR_ERROR;
3138 LeaveCriticalSection(&device->lock);
3140 HeapFree(GetProcessHeap(), 0, vols);
3142 return MMSYSERR_NOERROR;
3145 /**************************************************************************
3146 * waveOutGetID [WINMM.@]
3148 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3150 WINMM_Device *device;
3151 UINT dev, junk;
3152 BOOL is_out;
3154 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3156 if(!lpuDeviceID)
3157 return MMSYSERR_INVALPARAM;
3159 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3160 if(!WINMM_ValidateAndLock(device))
3161 return MMSYSERR_INVALHANDLE;
3163 LeaveCriticalSection(&device->lock);
3165 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3167 return MMSYSERR_NOERROR;
3170 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3172 UINT count;
3173 WINMM_MMDevice **devices;
3175 TRACE("(%u, %p, %d)\n", device, len, is_out);
3177 if(is_out){
3178 count = g_outmmdevices_count;
3179 devices = g_out_map;
3180 }else{
3181 count = g_inmmdevices_count;
3182 devices = g_in_map;
3185 if(device >= count)
3186 return MMSYSERR_INVALHANDLE;
3188 EnterCriticalSection(&g_devthread_lock);
3189 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3190 LeaveCriticalSection(&g_devthread_lock);
3192 return MMSYSERR_NOERROR;
3195 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3196 BOOL is_out)
3198 UINT count, id_len;
3199 WINMM_MMDevice **devices;
3201 TRACE("(%u, %p, %d)\n", device, str, is_out);
3203 if(is_out){
3204 count = g_outmmdevices_count;
3205 devices = g_out_map;
3206 }else{
3207 count = g_inmmdevices_count;
3208 devices = g_in_map;
3211 if(device >= count)
3212 return MMSYSERR_INVALHANDLE;
3214 EnterCriticalSection(&g_devthread_lock);
3215 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3216 if(len < id_len){
3217 LeaveCriticalSection(&g_devthread_lock);
3218 return MMSYSERR_ERROR;
3221 memcpy(str, devices[device]->dev_id, id_len);
3222 LeaveCriticalSection(&g_devthread_lock);
3224 return MMSYSERR_NOERROR;
3227 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3229 WINMM_QueryInterfaceInfo info;
3230 UINT ret;
3232 if(!WINMM_StartDevicesThread())
3233 return MMSYSERR_NODRIVER;
3235 info.is_out = is_out;
3236 info.index = index;
3237 info.str = out;
3238 info.len_bytes = out_len;
3240 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3241 InterlockedDecrement(&g_devthread_token);
3242 return ret;
3245 /**************************************************************************
3246 * waveOutMessage [WINMM.@]
3248 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3249 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3251 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3253 switch(uMessage){
3254 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3255 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3256 (DWORD_PTR*)dwParam1, TRUE);
3257 case DRV_QUERYFUNCTIONINSTANCEID:
3258 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3259 case DRV_QUERYDEVICEINTERFACESIZE:
3260 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3261 NULL, (ULONG*)dwParam1);
3262 case DRV_QUERYDEVICEINTERFACE:
3264 ULONG size = dwParam2;
3265 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3266 (WCHAR*)dwParam1, &size);
3268 case DRV_QUERYMAPPABLE:
3269 return MMSYSERR_NOERROR;
3270 case DRVM_MAPPER_PREFERRED_GET:
3271 if(!dwParam1 || !dwParam2)
3272 return MMSYSERR_INVALPARAM;
3274 if(g_outmmdevices_count > 0)
3275 /* Device 0 is always the default device */
3276 *(DWORD *)dwParam1 = 0;
3277 else
3278 *(DWORD *)dwParam1 = -1;
3280 /* Status flags */
3281 *(DWORD *)dwParam2 = 0;
3283 return MMSYSERR_NOERROR;
3286 TRACE("Message not supported: %u\n", uMessage);
3288 return MMSYSERR_NOTSUPPORTED;
3291 /**************************************************************************
3292 * waveInGetNumDevs [WINMM.@]
3294 UINT WINAPI waveInGetNumDevs(void)
3296 HRESULT hr = WINMM_InitMMDevices();
3297 if(FAILED(hr))
3298 return 0;
3300 TRACE("count: %u\n", g_inmmdevices_count);
3302 return g_inmmdevices_count;
3305 /**************************************************************************
3306 * waveInGetDevCapsW [WINMM.@]
3308 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3310 WAVEINCAPSW mapper_caps, *caps;
3311 HRESULT hr;
3313 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3315 hr = WINMM_InitMMDevices();
3316 if(FAILED(hr))
3317 return MMSYSERR_NODRIVER;
3319 if(!lpCaps)
3320 return MMSYSERR_INVALPARAM;
3322 if(WINMM_IsMapper(uDeviceID)){
3323 mapper_caps.wMid = 0xFF;
3324 mapper_caps.wPid = 0xFF;
3325 mapper_caps.vDriverVersion = 0x00010001;
3326 mapper_caps.dwFormats = 0xFFFFFFFF;
3327 mapper_caps.wReserved1 = 0;
3328 mapper_caps.wChannels = 2;
3329 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
3331 caps = &mapper_caps;
3332 }else{
3333 if(uDeviceID >= g_inmmdevices_count){
3334 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)uDeviceID);
3336 if(!WINMM_ValidateAndLock(device))
3337 return MMSYSERR_BADDEVICEID;
3339 caps = &device->parent->in_caps;
3341 LeaveCriticalSection(&device->lock);
3342 }else{
3343 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3347 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3349 return MMSYSERR_NOERROR;
3352 /**************************************************************************
3353 * waveInGetDevCapsA [WINMM.@]
3355 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3357 UINT ret;
3358 WAVEINCAPSW wicW;
3360 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3362 if(!lpCaps)
3363 return MMSYSERR_INVALPARAM;
3365 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3367 if (ret == MMSYSERR_NOERROR) {
3368 WAVEINCAPSA wicA;
3369 wicA.wMid = wicW.wMid;
3370 wicA.wPid = wicW.wPid;
3371 wicA.vDriverVersion = wicW.vDriverVersion;
3372 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3373 sizeof(wicA.szPname), NULL, NULL );
3374 wicA.dwFormats = wicW.dwFormats;
3375 wicA.wChannels = wicW.wChannels;
3376 wicA.wReserved1 = wicW.wReserved1;
3377 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3379 return ret;
3382 /**************************************************************************
3383 * waveInOpen [WINMM.@]
3385 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3386 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3387 DWORD_PTR dwInstance, DWORD dwFlags)
3389 LRESULT res;
3390 WINMM_OpenInfo info;
3391 WINMM_CBInfo cb_info;
3393 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3394 dwCallback, dwInstance, dwFlags);
3396 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3397 return MMSYSERR_INVALPARAM;
3399 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3400 if(res != MMSYSERR_NOERROR)
3401 return res;
3403 if(!WINMM_StartDevicesThread())
3404 return MMSYSERR_NODRIVER;
3406 info.handle = 0;
3407 info.format = (WAVEFORMATEX*)lpFormat;
3408 info.callback = dwCallback;
3409 info.cb_user = dwInstance;
3410 info.req_device = uDeviceID;
3411 info.flags = dwFlags;
3412 info.reset = TRUE;
3414 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3415 InterlockedDecrement(&g_devthread_token);
3416 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3417 return res;
3419 if(lphWaveIn)
3420 *lphWaveIn = (HWAVEIN)info.handle;
3422 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3423 cb_info.callback = dwCallback;
3424 cb_info.user = dwInstance;
3425 cb_info.hwave = info.handle;
3427 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3429 return res;
3432 /**************************************************************************
3433 * waveInClose [WINMM.@]
3435 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3437 WINMM_Device *device;
3438 WINMM_CBInfo cb_info;
3439 UINT res;
3441 TRACE("(%p)\n", hWaveIn);
3443 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3445 if(!WINMM_ValidateAndLock(device))
3446 return MMSYSERR_INVALHANDLE;
3448 cb_info = device->cb_info;
3450 LeaveCriticalSection(&device->lock);
3452 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3454 if(res == MMSYSERR_NOERROR)
3455 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3457 return res;
3460 /**************************************************************************
3461 * waveInPrepareHeader [WINMM.@]
3463 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3464 UINT uSize)
3466 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3468 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3469 return MMSYSERR_INVALPARAM;
3471 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3472 return MMSYSERR_NOERROR;
3474 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3477 /**************************************************************************
3478 * waveInUnprepareHeader [WINMM.@]
3480 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3481 UINT uSize)
3483 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3485 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3486 return MMSYSERR_INVALPARAM;
3488 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3489 return WAVERR_STILLPLAYING;
3491 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3492 return MMSYSERR_NOERROR;
3494 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3497 /**************************************************************************
3498 * waveInAddBuffer [WINMM.@]
3500 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3502 WINMM_Device *device;
3504 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3506 if(!header || uSize < sizeof(WAVEHDR))
3507 return MMSYSERR_INVALPARAM;
3509 if(!(header->dwFlags & WHDR_PREPARED))
3510 return WAVERR_UNPREPARED;
3512 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3514 if(!WINMM_ValidateAndLock(device))
3515 return MMSYSERR_INVALHANDLE;
3517 if(!device->first)
3518 device->first = device->last = header;
3519 else{
3520 device->last->lpNext = header;
3521 device->last = header;
3524 header->dwBytesRecorded = 0;
3525 header->lpNext = NULL;
3526 header->dwFlags &= ~WHDR_DONE;
3527 header->dwFlags |= WHDR_INQUEUE;
3529 LeaveCriticalSection(&device->lock);
3531 return MMSYSERR_NOERROR;
3534 /**************************************************************************
3535 * waveInReset [WINMM.@]
3537 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3539 TRACE("(%p)\n", hWaveIn);
3541 return WINMM_Reset((HWAVE)hWaveIn);
3544 /**************************************************************************
3545 * waveInStart [WINMM.@]
3547 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3549 WINMM_Device *device;
3550 MMRESULT mr;
3552 TRACE("(%p)\n", hWaveIn);
3554 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3556 if(!WINMM_ValidateAndLock(device))
3557 return MMSYSERR_INVALHANDLE;
3559 mr = WINMM_BeginPlaying(device);
3561 LeaveCriticalSection(&device->lock);
3563 return mr;
3566 /**************************************************************************
3567 * waveInStop [WINMM.@]
3569 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3571 WINMM_CBInfo cb_info;
3572 WINMM_Device *device;
3573 WAVEHDR *buf;
3574 HRESULT hr;
3576 TRACE("(%p)\n", hWaveIn);
3578 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3580 if(!WINMM_ValidateAndLock(device))
3581 return MMSYSERR_INVALHANDLE;
3583 hr = WINMM_Pause(device);
3584 if(FAILED(hr)){
3585 LeaveCriticalSection(&device->lock);
3586 return MMSYSERR_ERROR;
3588 device->stopped = TRUE;
3590 buf = device->first;
3591 if(buf && buf->dwBytesRecorded > 0){
3592 device->first = buf->lpNext;
3593 }else
3594 buf = NULL;
3596 cb_info = device->cb_info;
3598 LeaveCriticalSection(&device->lock);
3600 if(buf){
3601 buf->dwFlags &= ~WHDR_INQUEUE;
3602 buf->dwFlags |= WHDR_DONE;
3603 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3606 return MMSYSERR_NOERROR;
3609 /**************************************************************************
3610 * waveInGetPosition [WINMM.@]
3612 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3613 UINT uSize)
3615 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3617 if(!uSize || !lpTime)
3618 return MMSYSERR_INVALPARAM;
3620 if(uSize < sizeof(MMTIME))
3621 return MMSYSERR_ERROR;
3623 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3626 /**************************************************************************
3627 * waveInGetID [WINMM.@]
3629 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3631 UINT dev, junk;
3632 BOOL is_out;
3633 WINMM_Device *device;
3635 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3637 if(!lpuDeviceID)
3638 return MMSYSERR_INVALPARAM;
3640 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3641 if(!WINMM_ValidateAndLock(device))
3642 return MMSYSERR_INVALHANDLE;
3644 LeaveCriticalSection(&device->lock);
3646 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3648 return MMSYSERR_NOERROR;
3651 /**************************************************************************
3652 * waveInMessage [WINMM.@]
3654 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3655 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3657 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3659 switch(uMessage){
3660 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3661 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3662 (DWORD_PTR*)dwParam1, FALSE);
3663 case DRV_QUERYFUNCTIONINSTANCEID:
3664 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3665 case DRV_QUERYDEVICEINTERFACESIZE:
3666 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3667 NULL, (ULONG*)dwParam1);
3668 case DRV_QUERYDEVICEINTERFACE:
3670 ULONG size = dwParam2;
3671 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3672 (WCHAR*)dwParam1, &size);
3674 case DRV_QUERYMAPPABLE:
3675 return MMSYSERR_NOERROR;
3676 case DRVM_MAPPER_PREFERRED_GET:
3677 if(!dwParam1 || !dwParam2)
3678 return MMSYSERR_INVALPARAM;
3680 if(g_inmmdevices_count > 0)
3681 /* Device 0 is always the default device */
3682 *(DWORD *)dwParam1 = 0;
3683 else
3684 *(DWORD *)dwParam1 = -1;
3686 /* Status flags */
3687 *(DWORD *)dwParam2 = 0;
3689 return MMSYSERR_NOERROR;
3692 TRACE("Message not supported: %u\n", uMessage);
3694 return MMSYSERR_NOTSUPPORTED;
3697 UINT WINAPI mixerGetNumDevs(void)
3699 HRESULT hr;
3701 TRACE("\n");
3703 hr = WINMM_InitMMDevices();
3704 if(FAILED(hr))
3705 return 0;
3707 return g_outmmdevices_count + g_inmmdevices_count;
3710 /**************************************************************************
3711 * mixerGetDevCapsA [WINMM.@]
3713 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3715 MIXERCAPSW micW;
3716 UINT ret;
3718 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3720 if(!lpCaps)
3721 return MMSYSERR_INVALPARAM;
3723 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3725 if (ret == MMSYSERR_NOERROR) {
3726 MIXERCAPSA micA;
3727 micA.wMid = micW.wMid;
3728 micA.wPid = micW.wPid;
3729 micA.vDriverVersion = micW.vDriverVersion;
3730 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3731 sizeof(micA.szPname), NULL, NULL );
3732 micA.fdwSupport = micW.fdwSupport;
3733 micA.cDestinations = micW.cDestinations;
3734 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3736 return ret;
3739 /**************************************************************************
3740 * mixerGetDevCapsW [WINMM.@]
3742 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3744 WINMM_MMDevice *mmdevice;
3745 MIXERCAPSW caps;
3746 HRESULT hr;
3748 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3750 hr = WINMM_InitMMDevices();
3751 if(FAILED(hr))
3752 return MMSYSERR_NODRIVER;
3754 if(!lpCaps)
3755 return MMSYSERR_INVALPARAM;
3757 if(!uSize)
3758 return MMSYSERR_NOERROR;
3760 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3761 mmdevice = WINMM_GetMixerMMDevice((HMIXEROBJ)uDeviceID,
3762 MIXER_OBJECTF_MIXER, NULL);
3763 else if(uDeviceID < g_outmmdevices_count)
3764 mmdevice = read_map(g_out_map, uDeviceID);
3765 else
3766 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3768 if(!mmdevice)
3769 return MMSYSERR_BADDEVICEID;
3771 if(mmdevice->dataflow == eRender)
3772 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3773 else
3774 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3776 caps.wMid = 0xFF;
3777 caps.wPid = 0xFF;
3778 caps.vDriverVersion = 0x00010001;
3779 caps.fdwSupport = 0;
3780 caps.cDestinations = 1;
3782 memcpy(lpCaps, &caps, uSize);
3784 return MMSYSERR_NOERROR;
3787 /**************************************************************************
3788 * mixerOpen [WINMM.@]
3790 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3791 DWORD_PTR dwInstance, DWORD fdwOpen)
3793 WINMM_MMDevice *mmdevice;
3794 MMRESULT mr;
3795 HRESULT hr;
3797 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3798 dwInstance, fdwOpen);
3800 hr = WINMM_InitMMDevices();
3801 if(FAILED(hr))
3802 return MMSYSERR_NODRIVER;
3804 if(!lphMix)
3805 return MMSYSERR_INVALPARAM;
3807 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3808 if(mr != MMSYSERR_NOERROR)
3809 return mr;
3811 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3812 return MMSYSERR_BADDEVICEID;
3814 if(uDeviceID < g_outmmdevices_count){
3815 mmdevice = read_map(g_out_map, uDeviceID);
3816 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3817 mmdevice->mixer_count);
3818 }else{
3819 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3820 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3821 FALSE, mmdevice->mixer_count);
3824 ++mmdevice->mixer_count;
3826 return MMSYSERR_NOERROR;
3829 /**************************************************************************
3830 * mixerClose [WINMM.@]
3832 UINT WINAPI mixerClose(HMIXER hMix)
3834 TRACE("(%p)\n", hMix);
3836 return MMSYSERR_NOERROR;
3839 /**************************************************************************
3840 * mixerGetID [WINMM.@]
3842 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3844 WINMM_MMDevice *mmdevice;
3845 HRESULT hr;
3847 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3849 hr = WINMM_InitMMDevices();
3850 if(FAILED(hr))
3851 return MMSYSERR_NODRIVER;
3853 if(!lpid)
3854 return MMSYSERR_INVALPARAM;
3856 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3857 if(!mmdevice)
3858 return MMSYSERR_INVALHANDLE;
3860 if(mmdevice->in_caps.szPname[0] != '\0')
3861 *lpid += g_outmmdevices_count;
3863 return MMSYSERR_NOERROR;
3866 /**************************************************************************
3867 * mixerGetControlDetailsW [WINMM.@]
3869 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3870 DWORD fdwDetails)
3872 WINMM_ControlDetails details;
3874 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3876 if(!WINMM_StartDevicesThread())
3877 return MMSYSERR_NODRIVER;
3879 if(!lpmcdW || !lpmcdW->paDetails)
3880 return MMSYSERR_INVALPARAM;
3882 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3884 details.hmix = hmix;
3885 details.details = lpmcdW;
3886 details.flags = fdwDetails;
3888 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3889 (DWORD_PTR)&details, 0);
3892 /**************************************************************************
3893 * mixerGetControlDetailsA [WINMM.@]
3895 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3896 DWORD fdwDetails)
3898 UINT ret = MMSYSERR_NOTSUPPORTED;
3900 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3902 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3903 return MMSYSERR_INVALPARAM;
3905 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3906 case MIXER_GETCONTROLDETAILSF_VALUE:
3907 /* can safely use A structure as it is, no string inside */
3908 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3909 break;
3910 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3912 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3913 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3914 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3915 unsigned int i;
3917 if (lpmcdA->u.cMultipleItems != 0) {
3918 size *= lpmcdA->u.cMultipleItems;
3920 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3921 lpmcdA->paDetails = pDetailsW;
3922 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3923 /* set up lpmcd->paDetails */
3924 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3925 /* copy from lpmcd->paDetails back to paDetailsW; */
3926 if (ret == MMSYSERR_NOERROR) {
3927 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3928 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3929 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3930 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3931 pDetailsA->szName,
3932 sizeof(pDetailsA->szName), NULL, NULL );
3933 pDetailsA++;
3934 pDetailsW++;
3936 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3937 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3939 HeapFree(GetProcessHeap(), 0, pDetailsW);
3940 lpmcdA->paDetails = pDetailsA;
3941 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3943 break;
3944 default:
3945 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3948 return ret;
3951 /**************************************************************************
3952 * mixerGetLineControlsA [WINMM.@]
3954 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3955 DWORD fdwControls)
3957 MIXERLINECONTROLSW mlcW;
3958 DWORD ret;
3959 unsigned int i;
3961 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3963 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3964 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3965 return MMSYSERR_INVALPARAM;
3967 mlcW.cbStruct = sizeof(mlcW);
3968 mlcW.dwLineID = lpmlcA->dwLineID;
3969 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3970 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3972 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3973 the control count is assumed to be 1 - This is relied upon by a game,
3974 "Dynomite Deluze" */
3975 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3976 mlcW.cControls = 1;
3977 } else {
3978 mlcW.cControls = lpmlcA->cControls;
3980 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3981 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3982 mlcW.cControls * mlcW.cbmxctrl);
3984 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3986 if (ret == MMSYSERR_NOERROR) {
3987 lpmlcA->dwLineID = mlcW.dwLineID;
3988 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3989 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3991 for (i = 0; i < mlcW.cControls; i++) {
3992 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3993 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3994 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3995 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3996 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3997 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3998 lpmlcA->pamxctrl[i].szShortName,
3999 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
4000 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
4001 lpmlcA->pamxctrl[i].szName,
4002 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
4003 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
4004 * sizeof(mlcW.pamxctrl[i].Bounds) */
4005 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
4006 sizeof(mlcW.pamxctrl[i].Bounds));
4007 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
4008 * sizeof(mlcW.pamxctrl[i].Metrics) */
4009 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
4010 sizeof(mlcW.pamxctrl[i].Metrics));
4014 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
4016 return ret;
4019 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4020 MIXERCONTROLW *ctl, DWORD flags)
4022 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4023 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4024 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4025 ctl->cMultipleItems = 0;
4026 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4027 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szName, MIXER_LONG_NAME_CHARS);
4028 ctl->Bounds.s1.dwMinimum = 0;
4029 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4030 ctl->Metrics.cSteps = 192;
4032 return MMSYSERR_NOERROR;
4035 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4036 MIXERCONTROLW *ctl, DWORD flags)
4038 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4039 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4040 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4041 ctl->cMultipleItems = 0;
4042 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4043 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szName, MIXER_LONG_NAME_CHARS);
4044 ctl->Bounds.s1.dwMinimum = 0;
4045 ctl->Bounds.s1.dwMaximum = 1;
4046 ctl->Metrics.cSteps = 0;
4048 return MMSYSERR_NOERROR;
4051 /**************************************************************************
4052 * mixerGetLineControlsW [WINMM.@]
4054 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4055 DWORD fdwControls)
4057 WINMM_MMDevice *mmdevice;
4058 HRESULT hr;
4060 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4062 hr = WINMM_InitMMDevices();
4063 if(FAILED(hr))
4064 return MMSYSERR_NODRIVER;
4066 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4067 MIXER_GETLINECONTROLSF_ONEBYID |
4068 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4069 MIXER_OBJECTF_HMIXER |
4070 MIXER_OBJECTF_MIXER)){
4071 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4072 return MMSYSERR_INVALFLAG;
4075 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4076 return MMSYSERR_INVALPARAM;
4078 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4079 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4080 TRACE("cControls: %u\n", lpmlcW->cControls);
4082 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4083 if(!mmdevice)
4084 return MMSYSERR_INVALHANDLE;
4086 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4087 case MIXER_GETLINECONTROLSF_ALL:
4088 if(lpmlcW->cControls != 2)
4089 return MMSYSERR_INVALPARAM;
4090 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4091 return MMSYSERR_INVALPARAM;
4092 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4093 return MIXERR_INVALLINE;
4094 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4095 &lpmlcW->pamxctrl[0], fdwControls);
4096 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4097 &lpmlcW->pamxctrl[1], fdwControls);
4098 return MMSYSERR_NOERROR;
4099 case MIXER_GETLINECONTROLSF_ONEBYID:
4100 if(lpmlcW->cControls != 1)
4101 return MMSYSERR_INVALPARAM;
4102 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4103 return MMSYSERR_INVALPARAM;
4104 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4105 return MIXERR_INVALLINE;
4106 if(lpmlcW->u.dwControlID == 0)
4107 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4108 lpmlcW->pamxctrl, fdwControls);
4109 if(lpmlcW->u.dwControlID == 1)
4110 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4111 lpmlcW->pamxctrl, fdwControls);
4112 return MMSYSERR_NOTSUPPORTED;
4113 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4114 if(lpmlcW->cControls != 1)
4115 return MMSYSERR_INVALPARAM;
4116 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4117 return MMSYSERR_INVALPARAM;
4118 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4119 return MIXERR_INVALLINE;
4120 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4121 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4122 lpmlcW->pamxctrl, fdwControls);
4123 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4124 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4125 lpmlcW->pamxctrl, fdwControls);
4126 return MMSYSERR_NOTSUPPORTED;
4129 return MMSYSERR_NOTSUPPORTED;
4132 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4133 MIXERLINEW *info, DWORD flags)
4135 BOOL is_out = TRUE;
4136 if(mmdevice->in_caps.szPname[0] != '\0')
4137 is_out = FALSE;
4139 if(info->dwSource != 0)
4140 return MIXERR_INVALLINE;
4142 info->dwDestination = 0;
4143 info->dwLineID = 0;
4144 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4145 info->cConnections = 0;
4146 info->cControls = 2;
4147 /* volume & mute always affect all channels, so claim 1 channel */
4148 info->cChannels = 1;
4149 info->Target.dwDeviceID = mmdev_index;
4150 info->Target.wMid = ~0;
4151 info->Target.wPid = ~0;
4152 info->Target.vDriverVersion = 0;
4154 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4155 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4157 if(is_out){
4158 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4159 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4160 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4161 sizeof(info->Target.szPname));
4162 }else{
4163 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4164 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4165 info->Target.szPname[0] = '\0';
4168 return MMSYSERR_NOERROR;
4171 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4172 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4174 BOOL is_out = TRUE;
4175 if(mmdevice->in_caps.szPname[0] != '\0')
4176 is_out = FALSE;
4178 if(info->dwDestination != 0)
4179 return MIXERR_INVALLINE;
4181 info->dwSource = 0xFFFFFFFF;
4182 info->dwLineID = 0xFFFF0000;
4183 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4184 info->cConnections = 1;
4185 info->cControls = 2;
4187 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4188 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4190 info->Target.dwDeviceID = mmdev_index;
4191 info->Target.wMid = ~0;
4192 info->Target.wPid = ~0;
4193 info->Target.vDriverVersion = 0;
4195 if(is_out){
4196 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4197 info->cChannels = mmdevice->out_caps.wChannels;
4198 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4199 info->Target.szPname[0] = '\0';
4200 }else{
4201 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4202 info->cChannels = mmdevice->in_caps.wChannels;
4203 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4204 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4205 sizeof(info->Target.szPname));
4208 return MMSYSERR_NOERROR;
4211 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4212 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4214 BOOL is_out = TRUE;
4215 if(mmdevice->in_caps.szPname[0] != '\0')
4216 is_out = FALSE;
4218 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4219 if(is_out)
4220 return MIXERR_INVALLINE;
4221 info->dwDestination = 0;
4222 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4225 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4226 if(!is_out)
4227 return MIXERR_INVALLINE;
4228 info->dwDestination = 0;
4229 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4232 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4233 if(is_out)
4234 return MIXERR_INVALLINE;
4235 info->dwSource = 0;
4236 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4239 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4240 if(!is_out)
4241 return MIXERR_INVALLINE;
4242 info->dwSource = 0;
4243 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4246 TRACE("Returning INVALLINE on this component type: %u\n",
4247 info->dwComponentType);
4249 return MIXERR_INVALLINE;
4252 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4253 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4255 if(info->dwLineID == 0xFFFF0000){
4256 info->dwDestination = 0;
4257 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4260 if(info->dwLineID == 0){
4261 info->dwSource = 0;
4262 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4265 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4266 return MIXERR_INVALLINE;
4269 /**************************************************************************
4270 * mixerGetLineInfoW [WINMM.@]
4272 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4274 UINT mmdev_index;
4275 WINMM_MMDevice *mmdevice;
4276 HRESULT hr;
4278 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4280 hr = WINMM_InitMMDevices();
4281 if(FAILED(hr))
4282 return MMSYSERR_NODRIVER;
4284 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4285 return MMSYSERR_INVALPARAM;
4287 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4288 TRACE("dwSource: %u\n", lpmliW->dwSource);
4289 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4290 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4291 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4293 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4294 MIXER_GETLINEINFOF_DESTINATION |
4295 MIXER_GETLINEINFOF_LINEID |
4296 MIXER_GETLINEINFOF_SOURCE |
4297 MIXER_GETLINEINFOF_TARGETTYPE |
4298 MIXER_OBJECTF_HMIXER |
4299 MIXER_OBJECTF_MIXER)){
4300 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4301 return MMSYSERR_INVALFLAG;
4304 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4305 if(!mmdevice)
4306 return MMSYSERR_INVALHANDLE;
4308 lpmliW->dwUser = 0;
4310 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4311 case MIXER_GETLINEINFOF_DESTINATION:
4312 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4313 fdwInfo);
4314 case MIXER_GETLINEINFOF_SOURCE:
4315 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4316 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4317 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4318 fdwInfo);
4319 case MIXER_GETLINEINFOF_LINEID:
4320 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4321 case MIXER_GETLINEINFOF_TARGETTYPE:
4322 FIXME("TARGETTYPE flag not implemented!\n");
4323 return MIXERR_INVALLINE;
4326 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo);
4327 return MMSYSERR_INVALFLAG;
4330 /**************************************************************************
4331 * mixerGetLineInfoA [WINMM.@]
4333 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4334 DWORD fdwInfo)
4336 MIXERLINEW mliW;
4337 UINT ret;
4339 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4341 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4342 return MMSYSERR_INVALPARAM;
4344 mliW.cbStruct = sizeof(mliW);
4345 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4346 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4347 mliW.dwComponentType = lpmliA->dwComponentType;
4348 break;
4349 case MIXER_GETLINEINFOF_DESTINATION:
4350 mliW.dwDestination = lpmliA->dwDestination;
4351 break;
4352 case MIXER_GETLINEINFOF_LINEID:
4353 mliW.dwLineID = lpmliA->dwLineID;
4354 break;
4355 case MIXER_GETLINEINFOF_SOURCE:
4356 mliW.dwDestination = lpmliA->dwDestination;
4357 mliW.dwSource = lpmliA->dwSource;
4358 break;
4359 case MIXER_GETLINEINFOF_TARGETTYPE:
4360 mliW.Target.dwType = lpmliA->Target.dwType;
4361 mliW.Target.wMid = lpmliA->Target.wMid;
4362 mliW.Target.wPid = lpmliA->Target.wPid;
4363 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4364 MultiByteToWideChar(CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, ARRAY_SIZE(mliW.Target.szPname));
4365 break;
4366 default:
4367 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4368 return MMSYSERR_INVALFLAG;
4371 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4373 if(ret == MMSYSERR_NOERROR)
4375 lpmliA->dwDestination = mliW.dwDestination;
4376 lpmliA->dwSource = mliW.dwSource;
4377 lpmliA->dwLineID = mliW.dwLineID;
4378 lpmliA->fdwLine = mliW.fdwLine;
4379 lpmliA->dwUser = mliW.dwUser;
4380 lpmliA->dwComponentType = mliW.dwComponentType;
4381 lpmliA->cChannels = mliW.cChannels;
4382 lpmliA->cConnections = mliW.cConnections;
4383 lpmliA->cControls = mliW.cControls;
4384 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4385 sizeof(lpmliA->szShortName), NULL, NULL);
4386 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4387 sizeof(lpmliA->szName), NULL, NULL );
4388 lpmliA->Target.dwType = mliW.Target.dwType;
4389 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4390 lpmliA->Target.wMid = mliW.Target.wMid;
4391 lpmliA->Target.wPid = mliW.Target.wPid;
4392 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4393 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4394 sizeof(lpmliA->Target.szPname), NULL, NULL );
4396 return ret;
4399 /**************************************************************************
4400 * mixerSetControlDetails [WINMM.@]
4402 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4403 DWORD fdwDetails)
4405 WINMM_ControlDetails details;
4406 UINT ret;
4408 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4410 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4411 MIXER_SETCONTROLDETAILSF_CUSTOM)
4412 return MMSYSERR_NOTSUPPORTED;
4414 if(!lpmcd)
4415 return MMSYSERR_INVALPARAM;
4417 if(!WINMM_StartDevicesThread())
4418 return MMSYSERR_NODRIVER;
4420 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4422 details.hmix = hmix;
4423 details.details = lpmcd;
4424 details.flags = fdwDetails;
4426 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4427 (DWORD_PTR)&details, 0);
4428 InterlockedDecrement(&g_devthread_token);
4429 return ret;
4432 /**************************************************************************
4433 * mixerMessage [WINMM.@]
4435 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4437 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4439 return MMSYSERR_NOTSUPPORTED;