mshtml: Use fire_event_obj to dispatch XHR readystatechange event.
[wine.git] / dlls / winmm / waveform.c
blob730851997e24b3aa69878d4965f77c9b79f0e20e
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(!lstrcmpW((*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){
1091 /* we aren't guaranteed that the struct in lpFormat is a full
1092 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1093 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
1094 memcpy(device->orig_fmt, info->format, sizeof(PCMWAVEFORMAT));
1095 device->orig_fmt->cbSize = 0;
1096 if(device->orig_fmt->wBitsPerSample % 8 != 0){
1097 WARN("Fixing bad wBitsPerSample (%u)\n", device->orig_fmt->wBitsPerSample);
1098 device->orig_fmt->wBitsPerSample = (device->orig_fmt->wBitsPerSample + 7) & ~7;
1100 /* winmm ignores broken blockalign and avgbytes */
1101 if(device->orig_fmt->nBlockAlign != device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8){
1102 WARN("Fixing bad nBlockAlign (%u)\n", device->orig_fmt->nBlockAlign);
1103 device->orig_fmt->nBlockAlign = device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8;
1105 if (device->orig_fmt->nAvgBytesPerSec != device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign) {
1106 WARN("Fixing bad nAvgBytesPerSec (%u)\n", device->orig_fmt->nAvgBytesPerSec);
1107 device->orig_fmt->nAvgBytesPerSec = device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign;
1109 }else{
1110 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0,
1111 sizeof(WAVEFORMATEX) + info->format->cbSize);
1112 memcpy(device->orig_fmt, info->format,
1113 sizeof(WAVEFORMATEX) + info->format->cbSize);
1116 if(info->flags & WAVE_FORMAT_QUERY){
1117 WAVEFORMATEX *closer_fmt = NULL;
1119 hr = IAudioClient_IsFormatSupported(device->client,
1120 AUDCLNT_SHAREMODE_SHARED, device->orig_fmt, &closer_fmt);
1121 CoTaskMemFree(closer_fmt);
1122 if((hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT) && !(info->flags & WAVE_FORMAT_DIRECT))
1123 ret = WINMM_MapDevice(device, TRUE, is_out);
1124 else
1125 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1126 goto error;
1129 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1130 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1131 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1132 if(FAILED(hr)){
1133 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1134 ret = WINMM_MapDevice(device, FALSE, is_out);
1135 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1136 goto error;
1137 }else{
1138 WARN("Initialize failed: %08x\n", hr);
1139 ret = hr2mmr(hr);
1140 goto error;
1142 }else{
1143 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1144 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1147 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1148 (void**)&device->clock);
1149 if(FAILED(hr)){
1150 WARN("GetService failed: %08x\n", hr);
1151 goto error;
1154 if(!device->event){
1155 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1156 if(!device->event){
1157 WARN("CreateEvent failed: %08x\n", hr);
1158 goto error;
1161 /* As the devices thread is waiting on g_device_handles, it can
1162 * only be modified from within this same thread. */
1163 if(g_device_handles){
1164 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1165 sizeof(HANDLE) * (g_devhandle_count + 1));
1166 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1167 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1168 }else{
1169 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1170 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1171 sizeof(WINMM_Device *));
1173 g_device_handles[g_devhandle_count] = device->event;
1174 g_handle_devices[g_devhandle_count] = device;
1175 ++g_devhandle_count;
1178 hr = IAudioClient_SetEventHandle(device->client, device->event);
1179 if(FAILED(hr)){
1180 WARN("SetEventHandle failed: %08x\n", hr);
1181 goto error;
1184 if(info->reset){
1185 device->played_frames = 0;
1186 device->ofs_bytes = 0;
1187 device->loop_counter = 0;
1188 device->first = device->last = device->playing = device->loop_start = NULL;
1191 device->stopped = TRUE;
1192 device->last_clock_pos = 0;
1194 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1195 device->cb_info.callback = info->callback;
1196 device->cb_info.user = info->cb_user;
1197 device->cb_info.hwave = device->handle;
1199 info->handle = device->handle;
1201 return MMSYSERR_NOERROR;
1203 error:
1204 if(device->client){
1205 IAudioClient_Release(device->client);
1206 device->client = NULL;
1208 if(device->device){
1209 IMMDevice_Release(device->device);
1210 device->device = NULL;
1213 return ret;
1216 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1218 WINMM_Device *device;
1219 LRESULT ret = MMSYSERR_ERROR;
1220 HRESULT hr;
1222 if(info->handle != 0){
1223 device = WINMM_GetDeviceFromHWAVE(info->handle);
1224 if(!device){
1225 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1226 return MMSYSERR_ERROR;
1229 EnterCriticalSection(&device->lock);
1231 device->open = TRUE;
1232 }else{
1233 CRITICAL_SECTION *lock;
1234 UINT internal_index;
1235 WINMM_Device **devices;
1236 WINMM_MMDevice *mmdevice;
1238 if(WINMM_IsMapper(info->req_device)){
1239 if (g_outmmdevices_count == 0)
1240 return MMSYSERR_BADDEVICEID;
1241 devices = g_out_mapper_devices;
1242 mmdevice = read_map(g_out_map, 0);
1243 lock = &g_devthread_lock;
1244 internal_index = MAPPER_INDEX;
1245 }else{
1246 if(info->req_device >= g_outmmdevices_count)
1247 return MMSYSERR_BADDEVICEID;
1249 mmdevice = read_map(g_out_map, info->req_device);
1251 if(!mmdevice->out_caps.szPname[0])
1252 return MMSYSERR_NOTENABLED;
1254 devices = mmdevice->devices;
1255 lock = &mmdevice->lock;
1256 internal_index = mmdevice->index;
1259 EnterCriticalSection(lock);
1261 device = WINMM_FindUnusedDevice(devices, mmdevice,
1262 internal_index, TRUE);
1263 if(!device){
1264 LeaveCriticalSection(lock);
1265 return MMSYSERR_ALLOCATED;
1268 LeaveCriticalSection(lock);
1271 ret = WINMM_OpenDevice(device, info, TRUE);
1272 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1273 goto error;
1274 ret = MMSYSERR_ERROR;
1276 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1277 (void**)&device->render);
1278 if(FAILED(hr)){
1279 ERR("GetService failed: %08x\n", hr);
1280 goto error;
1283 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1284 (void**)&device->volume);
1285 if(FAILED(hr)){
1286 ERR("GetService failed: %08x\n", hr);
1287 goto error;
1290 LeaveCriticalSection(&device->lock);
1292 return MMSYSERR_NOERROR;
1294 error:
1295 if(device->device){
1296 IMMDevice_Release(device->device);
1297 device->device = NULL;
1299 if(device->client){
1300 IAudioClient_Release(device->client);
1301 device->client = NULL;
1303 if(device->render){
1304 IAudioRenderClient_Release(device->render);
1305 device->render = NULL;
1307 if(device->volume){
1308 IAudioStreamVolume_Release(device->volume);
1309 device->volume = NULL;
1311 if(device->clock){
1312 IAudioClock_Release(device->clock);
1313 device->clock = NULL;
1315 device->open = FALSE;
1316 LeaveCriticalSection(&device->lock);
1317 return ret;
1320 static LRESULT WID_Open(WINMM_OpenInfo *info)
1322 WINMM_Device *device, **devices;
1323 WINMM_MMDevice *mmdevice;
1324 UINT internal_index;
1325 CRITICAL_SECTION *lock;
1326 LRESULT ret = MMSYSERR_ERROR;
1327 HRESULT hr;
1329 if(WINMM_IsMapper(info->req_device)){
1330 if (g_inmmdevices_count == 0)
1331 return MMSYSERR_BADDEVICEID;
1332 devices = g_in_mapper_devices;
1333 mmdevice = read_map(g_in_map, 0);
1334 lock = &g_devthread_lock;
1335 internal_index = MAPPER_INDEX;
1336 }else{
1337 if(info->req_device >= g_inmmdevices_count)
1338 return MMSYSERR_BADDEVICEID;
1340 mmdevice = read_map(g_in_map, info->req_device);
1342 if(!mmdevice->in_caps.szPname[0])
1343 return MMSYSERR_NOTENABLED;
1345 devices = mmdevice->devices;
1346 lock = &mmdevice->lock;
1347 internal_index = mmdevice->index;
1350 EnterCriticalSection(lock);
1352 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1353 if(!device){
1354 LeaveCriticalSection(lock);
1355 return MMSYSERR_ALLOCATED;
1358 LeaveCriticalSection(lock);
1360 ret = WINMM_OpenDevice(device, info, FALSE);
1361 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1362 goto error;
1363 ret = MMSYSERR_ERROR;
1365 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1366 (void**)&device->capture);
1367 if(FAILED(hr)){
1368 WARN("GetService failed: %08x\n", hr);
1369 goto error;
1372 LeaveCriticalSection(&device->lock);
1374 return MMSYSERR_NOERROR;
1376 error:
1377 if(device->device){
1378 IMMDevice_Release(device->device);
1379 device->device = NULL;
1381 if(device->client){
1382 IAudioClient_Release(device->client);
1383 device->client = NULL;
1385 if(device->capture){
1386 IAudioCaptureClient_Release(device->capture);
1387 device->capture = NULL;
1389 if(device->clock){
1390 IAudioClock_Release(device->clock);
1391 device->clock = NULL;
1393 device->open = FALSE;
1394 LeaveCriticalSection(&device->lock);
1395 return ret;
1398 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1400 device->open = FALSE;
1402 if(!device->stopped){
1403 IAudioClient_Stop(device->client);
1404 device->stopped = TRUE;
1407 if(device->acm_handle){
1408 acmStreamClose(device->acm_handle, 0);
1409 device->acm_handle = NULL;
1412 IMMDevice_Release(device->device);
1413 device->device = NULL;
1415 IAudioClient_Release(device->client);
1416 device->client = NULL;
1418 IAudioClock_Release(device->clock);
1419 device->clock = NULL;
1421 HeapFree(GetProcessHeap(), 0, device->orig_fmt);
1423 return S_OK;
1426 static LRESULT WOD_Close(HWAVEOUT hwave)
1428 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1430 TRACE("(%p)\n", hwave);
1432 if(!WINMM_ValidateAndLock(device))
1433 return MMSYSERR_INVALHANDLE;
1435 WINMM_CloseDevice(device);
1437 IAudioRenderClient_Release(device->render);
1438 device->render = NULL;
1440 IAudioStreamVolume_Release(device->volume);
1441 device->volume = NULL;
1443 LeaveCriticalSection(&device->lock);
1445 return MMSYSERR_NOERROR;
1448 static LRESULT WID_Close(HWAVEIN hwave)
1450 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1452 TRACE("(%p)\n", hwave);
1454 if(!WINMM_ValidateAndLock(device))
1455 return MMSYSERR_INVALHANDLE;
1457 WINMM_CloseDevice(device);
1459 IAudioCaptureClient_Release(device->capture);
1460 device->capture = NULL;
1462 LeaveCriticalSection(&device->lock);
1464 return MMSYSERR_NOERROR;
1467 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1469 return length - length % device->bytes_per_frame;
1472 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1474 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1476 TRACE("(%p, %p)\n", hwave, header);
1478 if(!WINMM_ValidateAndLock(device))
1479 return MMSYSERR_INVALHANDLE;
1481 if(device->render && device->acm_handle){
1482 ACMSTREAMHEADER *ash;
1483 DWORD size;
1484 MMRESULT mr;
1486 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1487 ACM_STREAMSIZEF_SOURCE);
1488 if(mr != MMSYSERR_NOERROR){
1489 LeaveCriticalSection(&device->lock);
1490 return mr;
1493 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1494 if(!ash){
1495 LeaveCriticalSection(&device->lock);
1496 return MMSYSERR_NOMEM;
1499 ash->cbStruct = sizeof(*ash);
1500 ash->fdwStatus = 0;
1501 ash->dwUser = (DWORD_PTR)header;
1502 ash->pbSrc = (BYTE*)header->lpData;
1503 ash->cbSrcLength = header->dwBufferLength;
1504 ash->dwSrcUser = header->dwUser;
1505 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1506 ash->cbDstLength = size;
1507 ash->dwDstUser = 0;
1509 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1510 if(mr != MMSYSERR_NOERROR){
1511 HeapFree(GetProcessHeap(), 0, ash);
1512 LeaveCriticalSection(&device->lock);
1513 return mr;
1516 header->reserved = (DWORD_PTR)ash;
1519 LeaveCriticalSection(&device->lock);
1521 header->dwFlags |= WHDR_PREPARED;
1522 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1524 return MMSYSERR_NOERROR;
1527 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1529 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1531 TRACE("(%p, %p)\n", hwave, header);
1533 if(!WINMM_ValidateAndLock(device))
1534 return MMSYSERR_INVALHANDLE;
1536 if(device->render && device->acm_handle){
1537 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1539 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1541 HeapFree(GetProcessHeap(), 0, ash);
1544 LeaveCriticalSection(&device->lock);
1546 header->dwFlags &= ~WHDR_PREPARED;
1548 return MMSYSERR_NOERROR;
1551 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1553 if(device->acm_handle){
1554 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1555 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1558 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1561 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1563 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1566 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1568 HRESULT hr;
1569 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1570 UINT64 clock_freq, clock_pos, clock_frames;
1571 UINT32 nloops, queue_frames = 0;
1573 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1574 if(FAILED(hr)){
1575 WARN("GetFrequency failed: %08x\n", hr);
1576 return NULL;
1579 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1580 if(FAILED(hr)){
1581 WARN("GetPosition failed: %08x\n", hr);
1582 return NULL;
1585 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1587 nloops = device->loop_counter;
1588 while(queue &&
1589 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1590 clock_frames - device->last_clock_pos + device->remainder_frames){
1591 if(!nloops){
1592 last = queue;
1593 device->last_clock_pos += queue_frames;
1594 device->remainder_frames = 0;
1595 queue_frames = 0;
1596 queue = device->first = queue->lpNext;
1597 }else{
1598 if(queue->dwFlags & WHDR_BEGINLOOP){
1599 if(queue->dwFlags & WHDR_ENDLOOP)
1600 --nloops;
1601 else
1602 queue = queue->lpNext;
1603 }else if(queue->dwFlags & WHDR_ENDLOOP){
1604 queue = device->loop_start;
1605 --nloops;
1610 if(last){
1611 last->lpNext = NULL;
1612 return first;
1613 }else
1614 return NULL;
1617 static void WOD_PushData(WINMM_Device *device)
1619 WINMM_CBInfo cb_info;
1620 HRESULT hr;
1621 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1622 UINT32 queue_bytes, nloops;
1623 BYTE *data;
1624 WAVEHDR *queue, *first = NULL;
1626 TRACE("(%p)\n", device->handle);
1628 EnterCriticalSection(&device->lock);
1630 if(!device->device)
1631 goto exit;
1633 if(!device->first){
1634 if (device->stopped)
1635 goto exit;
1636 device->stopped = TRUE;
1637 device->last_clock_pos = 0;
1638 IAudioClient_Stop(device->client);
1639 IAudioClient_Reset(device->client);
1640 goto exit;
1643 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1644 if(FAILED(hr)){
1645 WARN("GetBufferSize failed: %08x\n", hr);
1646 goto exit;
1649 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1650 if(FAILED(hr)){
1651 WARN("GetCurrentPadding failed: %08x\n", hr);
1652 goto exit;
1655 first = WOD_MarkDoneHeaders(device);
1657 /* determine which is larger between the available buffer size and
1658 * the amount of data left in the queue */
1659 avail_frames = bufsize - pad;
1661 queue = device->playing;
1662 ofs = device->ofs_bytes;
1663 queue_frames = 0;
1664 nloops = 0;
1665 while(queue && queue_frames < avail_frames){
1666 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1667 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1668 ofs = 0;
1670 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1671 queue = device->loop_start;
1672 ++nloops;
1673 }else
1674 queue = queue->lpNext;
1677 if(queue_frames < avail_frames)
1678 avail_frames = queue_frames;
1679 if(avail_frames == 0)
1680 goto exit;
1682 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1683 if(FAILED(hr)){
1684 WARN("GetBuffer failed: %08x\n", hr);
1685 goto exit;
1688 written = 0;
1689 while(device->playing && written < avail_frames){
1690 UINT32 copy_frames, copy_bytes;
1691 BYTE *queue_data;
1693 queue = device->playing;
1695 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1696 if(device->acm_handle)
1697 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1698 else
1699 queue_data = (BYTE*)queue->lpData;
1701 queue_frames = (queue_bytes - device->ofs_bytes) /
1702 device->bytes_per_frame;
1704 copy_frames = queue_frames < (avail_frames - written) ?
1705 queue_frames : avail_frames - written;
1706 copy_bytes = copy_frames * device->bytes_per_frame;
1708 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1710 data += copy_bytes;
1711 written += copy_frames;
1712 device->ofs_bytes += copy_bytes;
1714 if(device->ofs_bytes >= queue_bytes){
1715 device->ofs_bytes = 0;
1717 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1718 device->playing = queue->lpNext;
1719 else{
1720 if(queue->dwFlags & WHDR_BEGINLOOP){
1721 device->loop_start = device->playing;
1722 device->playing = queue->lpNext;
1723 device->loop_counter = queue->dwLoops;
1725 if(queue->dwFlags & WHDR_ENDLOOP){
1726 --device->loop_counter;
1727 if(device->loop_counter)
1728 device->playing = device->loop_start;
1729 else
1730 device->loop_start = device->playing = queue->lpNext;
1736 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1737 if(FAILED(hr)){
1738 WARN("ReleaseBuffer failed: %08x\n", hr);
1739 goto exit;
1742 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1743 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1744 else
1745 device->played_frames += avail_frames;
1747 exit:
1748 cb_info = device->cb_info;
1750 LeaveCriticalSection(&device->lock);
1752 while(first){
1753 WAVEHDR *next = first->lpNext;
1754 first->dwFlags &= ~WHDR_INQUEUE;
1755 first->dwFlags |= WHDR_DONE;
1756 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1757 first = next;
1761 static void WID_PullACMData(WINMM_Device *device)
1763 UINT32 packet, packet_bytes;
1764 DWORD flags;
1765 BYTE *data;
1766 WAVEHDR *queue;
1767 HRESULT hr;
1768 MMRESULT mr;
1770 if(device->acm_hdr.cbDstLength == 0){
1771 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1772 &flags, NULL, NULL);
1773 if(hr != S_OK){
1774 if(FAILED(hr))
1775 WARN("GetBuffer failed: %08x\n", hr);
1776 return;
1779 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1780 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1782 device->acm_offs = 0;
1784 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1785 device->acm_hdr.fdwStatus = 0;
1786 device->acm_hdr.dwUser = 0;
1787 device->acm_hdr.pbSrc = data;
1788 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1789 device->acm_hdr.cbSrcLengthUsed = 0;
1790 device->acm_hdr.dwSrcUser = 0;
1791 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1792 device->acm_hdr.cbDstLength = packet_bytes;
1793 device->acm_hdr.cbDstLengthUsed = 0;
1794 device->acm_hdr.dwDstUser = 0;
1796 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1797 if(mr != MMSYSERR_NOERROR){
1798 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1799 return;
1802 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1803 if(mr != MMSYSERR_NOERROR){
1804 WARN("acmStreamConvert failed: %d\n", mr);
1805 return;
1808 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1809 if(FAILED(hr))
1810 WARN("ReleaseBuffer failed: %08x\n", hr);
1812 device->played_frames += packet;
1815 queue = device->first;
1816 while(queue){
1817 UINT32 to_copy_bytes;
1819 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1820 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1822 memcpy(queue->lpData + queue->dwBytesRecorded,
1823 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1825 queue->dwBytesRecorded += to_copy_bytes;
1826 device->acm_offs += to_copy_bytes;
1828 if(queue->dwBufferLength - queue->dwBytesRecorded <
1829 device->bytes_per_frame){
1830 queue->dwFlags &= ~WHDR_INQUEUE;
1831 queue->dwFlags |= WHDR_DONE;
1832 device->first = queue = queue->lpNext;
1835 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1836 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1837 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1838 device->acm_hdr.cbDstLength = 0;
1839 device->acm_hdr.cbDstLengthUsed = 0;
1841 /* done with this ACM Header, so try to pull more data */
1842 WID_PullACMData(device);
1843 return;
1847 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1848 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1849 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1850 device->acm_hdr.cbDstLength = 0;
1851 device->acm_hdr.cbDstLengthUsed = 0;
1854 static void WID_PullData(WINMM_Device *device)
1856 WINMM_CBInfo cb_info;
1857 WAVEHDR *queue, *first = NULL, *last = NULL;
1858 HRESULT hr;
1860 TRACE("(%p)\n", device->handle);
1862 EnterCriticalSection(&device->lock);
1864 if(!device->device || !device->first)
1865 goto exit;
1867 first = device->first;
1869 if(device->acm_handle){
1870 WID_PullACMData(device);
1871 goto exit;
1874 while(device->first){
1875 BYTE *data;
1876 UINT32 packet_len, packet;
1877 DWORD flags;
1879 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1880 &flags, NULL, NULL);
1881 if(hr != S_OK){
1882 if(FAILED(hr))
1883 WARN("GetBuffer failed: %08x\n", hr);
1884 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1885 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1886 goto exit;
1889 packet = packet_len;
1890 queue = device->first;
1891 while(queue && packet > 0){
1892 UINT32 to_copy_bytes;
1894 to_copy_bytes = min(packet * device->bytes_per_frame,
1895 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1897 memcpy(queue->lpData + queue->dwBytesRecorded,
1898 data + (packet_len - packet) * device->bytes_per_frame,
1899 to_copy_bytes);
1901 queue->dwBytesRecorded += to_copy_bytes;
1903 if(queue->dwBufferLength - queue->dwBytesRecorded <
1904 device->bytes_per_frame){
1905 last = queue;
1906 device->first = queue = queue->lpNext;
1909 packet -= to_copy_bytes / device->bytes_per_frame;
1912 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1913 if(FAILED(hr))
1914 WARN("ReleaseBuffer failed: %08x\n", hr);
1916 if(packet > 0)
1917 WARN("losing %u frames\n", packet);
1918 device->played_frames += packet_len - packet;
1921 exit:
1922 cb_info = device->cb_info;
1924 LeaveCriticalSection(&device->lock);
1926 if(last){
1927 last->lpNext = NULL;
1928 while(first){
1929 WAVEHDR *next = first->lpNext;
1930 first->dwFlags &= ~WHDR_INQUEUE;
1931 first->dwFlags |= WHDR_DONE;
1932 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1933 first = next;
1938 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1940 HRESULT hr;
1942 TRACE("(%p)\n", device->handle);
1944 if(device->render)
1945 /* prebuffer data before starting */
1946 WOD_PushData(device);
1948 if(device->stopped){
1949 device->stopped = FALSE;
1951 hr = IAudioClient_Start(device->client);
1952 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1953 device->stopped = TRUE;
1954 WARN("Start failed: %08x\n", hr);
1955 return MMSYSERR_ERROR;
1959 return MMSYSERR_NOERROR;
1962 static LRESULT WINMM_Pause(HWAVE hwave)
1964 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1965 HRESULT hr;
1967 TRACE("(%p)\n", hwave);
1969 if(!WINMM_ValidateAndLock(device))
1970 return MMSYSERR_INVALHANDLE;
1972 hr = IAudioClient_Stop(device->client);
1973 if(FAILED(hr)){
1974 LeaveCriticalSection(&device->lock);
1975 WARN("Stop failed: %08x\n", hr);
1976 return MMSYSERR_ERROR;
1979 device->stopped = FALSE;
1981 LeaveCriticalSection(&device->lock);
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 case TIME_MS:
2042 time->u.ms = (UINT64)played_frames * 1000 / sample_rate;
2043 return MMSYSERR_NOERROR;
2044 case TIME_SMPTE:
2045 time->u.smpte.fps = 30;
2046 played_frames += sample_rate / time->u.smpte.fps - 1; /* round up */
2047 time->u.smpte.frame = (played_frames % sample_rate) * time->u.smpte.fps / sample_rate;
2048 played_frames /= sample_rate; /* yields seconds */
2049 time->u.smpte.sec = played_frames % 60;
2050 played_frames /= 60;
2051 time->u.smpte.min = played_frames % 60;
2052 time->u.smpte.hour= played_frames / 60;
2053 return MMSYSERR_NOERROR;
2054 default:
2055 time->wType = TIME_BYTES;
2056 /* fall through */
2057 case TIME_BYTES:
2058 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2059 return MMSYSERR_NOERROR;
2063 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2065 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2066 UINT32 played_frames, sample_rate, bytes_per_sec;
2068 TRACE("(%p, %p)\n", hwave, time);
2070 if(!WINMM_ValidateAndLock(device))
2071 return MMSYSERR_INVALHANDLE;
2073 played_frames = device->played_frames;
2074 sample_rate = device->orig_fmt->nSamplesPerSec;
2075 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2077 LeaveCriticalSection(&device->lock);
2079 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2082 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2083 UINT *mmdev_index)
2085 UINT mmdev, dev, junk, *out;
2086 BOOL is_out;
2088 if(!mmdev_index)
2089 out = &mmdev;
2090 else
2091 out = mmdev_index;
2093 switch(flags & 0xF0000000){
2094 case MIXER_OBJECTF_MIXER: /* == 0 */
2095 *out = HandleToULong(hmix);
2096 if(*out < g_outmmdevices_count)
2097 return read_map(g_out_map, *out);
2098 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2099 *out -= g_outmmdevices_count;
2100 return read_map(g_in_map, *out);
2102 /* fall through -- if it's not a valid mixer device, then
2103 * it could be a valid mixer handle. windows seems to do
2104 * this as well. */
2105 case MIXER_OBJECTF_HMIXER:
2106 case MIXER_OBJECTF_HWAVEOUT:
2107 case MIXER_OBJECTF_HWAVEIN:
2108 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2109 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2110 (!is_out && *out >= g_inmmdevices_count))
2111 return NULL;
2112 if(is_out)
2113 return read_map(g_out_map, *out);
2114 return read_map(g_in_map, *out);
2115 case MIXER_OBJECTF_WAVEOUT:
2116 *out = HandleToULong(hmix);
2117 if(*out < g_outmmdevices_count)
2118 return read_map(g_out_map, *out);
2119 return NULL;
2120 case MIXER_OBJECTF_WAVEIN:
2121 *out = HandleToULong(hmix);
2122 if(*out < g_inmmdevices_count)
2123 return read_map(g_in_map, *out);
2124 return NULL;
2127 return NULL;
2130 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2132 IAudioSessionManager *sesman;
2133 IMMDevice *device;
2134 HRESULT hr;
2136 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2137 if(FAILED(hr)){
2138 WARN("Device %s (%s) unavailable: %08x\n",
2139 wine_dbgstr_w(mmdevice->dev_id),
2140 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2141 return MMSYSERR_ERROR;
2144 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2145 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2146 if(FAILED(hr)){
2147 WARN("Activate failed: %08x\n", hr);
2148 IMMDevice_Release(device);
2149 return MMSYSERR_ERROR;
2152 IMMDevice_Release(device);
2154 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2155 FALSE, &mmdevice->volume);
2156 IAudioSessionManager_Release(sesman);
2157 if(FAILED(hr)){
2158 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2159 return MMSYSERR_ERROR;
2162 return MMSYSERR_NOERROR;
2165 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2167 WINMM_MMDevice *mmdevice;
2168 MIXERCONTROLDETAILS *control = details->details;
2169 HRESULT hr;
2171 TRACE("(%p)\n", details->hmix);
2173 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2174 if(!mmdevice)
2175 return MMSYSERR_INVALHANDLE;
2177 EnterCriticalSection(&mmdevice->lock);
2179 if(!mmdevice->volume){
2180 MMRESULT mr;
2182 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2183 if(mr != MMSYSERR_NOERROR){
2184 LeaveCriticalSection(&mmdevice->lock);
2185 return mr;
2189 if(control->dwControlID == 0){
2190 float vol;
2191 MIXERCONTROLDETAILS_UNSIGNED *udet;
2193 if(!control->paDetails ||
2194 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2195 LeaveCriticalSection(&mmdevice->lock);
2196 return MMSYSERR_INVALPARAM;
2199 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2200 if(FAILED(hr)){
2201 WARN("GetMasterVolume failed: %08x\n", hr);
2202 LeaveCriticalSection(&mmdevice->lock);
2203 return MMSYSERR_ERROR;
2206 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2207 udet->dwValue = vol * ((unsigned int)0xFFFF);
2208 }else if(control->dwControlID == 1){
2209 BOOL mute;
2210 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2212 if(!control->paDetails ||
2213 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2214 LeaveCriticalSection(&mmdevice->lock);
2215 return MMSYSERR_INVALPARAM;
2218 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2219 if(FAILED(hr)){
2220 WARN("GetMute failed: %08x\n", hr);
2221 LeaveCriticalSection(&mmdevice->lock);
2222 return MMSYSERR_ERROR;
2225 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2226 bdet->fValue = mute;
2227 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2228 FIXME("What should the sw-side mixer controls map to?\n");
2229 }else{
2230 LeaveCriticalSection(&mmdevice->lock);
2231 return MIXERR_INVALCONTROL;
2234 LeaveCriticalSection(&mmdevice->lock);
2236 return MMSYSERR_NOERROR;
2239 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2241 WINMM_MMDevice *mmdevice;
2242 MIXERCONTROLDETAILS *control = details->details;
2243 HRESULT hr;
2245 TRACE("(%p)\n", details->hmix);
2247 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2248 if(!mmdevice)
2249 return MMSYSERR_INVALHANDLE;
2251 EnterCriticalSection(&mmdevice->lock);
2253 if(!mmdevice->volume){
2254 MMRESULT mr;
2256 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2257 if(mr != MMSYSERR_NOERROR){
2258 LeaveCriticalSection(&mmdevice->lock);
2259 return mr;
2263 if(control->dwControlID == 0){
2264 float vol;
2265 MIXERCONTROLDETAILS_UNSIGNED *udet;
2267 if(!control->paDetails ||
2268 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2269 LeaveCriticalSection(&mmdevice->lock);
2270 return MMSYSERR_INVALPARAM;
2273 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2275 if(udet->dwValue > 65535){
2276 LeaveCriticalSection(&mmdevice->lock);
2277 return MMSYSERR_INVALPARAM;
2280 vol = udet->dwValue / 65535.f;
2282 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2283 if(FAILED(hr)){
2284 WARN("SetMasterVolume failed: %08x\n", hr);
2285 LeaveCriticalSection(&mmdevice->lock);
2286 return MMSYSERR_ERROR;
2288 }else if(control->dwControlID == 1){
2289 BOOL mute;
2290 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2292 if(!control->paDetails ||
2293 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2294 LeaveCriticalSection(&mmdevice->lock);
2295 return MMSYSERR_INVALPARAM;
2298 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2299 mute = bdet->fValue;
2301 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2302 if(FAILED(hr)){
2303 WARN("SetMute failed: %08x\n", hr);
2304 LeaveCriticalSection(&mmdevice->lock);
2305 return MMSYSERR_ERROR;
2307 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2308 FIXME("What should the sw-side mixer controls map to?\n");
2309 }else{
2310 LeaveCriticalSection(&mmdevice->lock);
2311 return MIXERR_INVALCONTROL;
2314 LeaveCriticalSection(&mmdevice->lock);
2316 return MMSYSERR_NOERROR;
2319 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2321 WINMM_MMDevice *mmdevice;
2322 IMMDevice *device;
2323 IPropertyStore *ps;
2324 PROPVARIANT pv;
2325 DWORD len_bytes;
2326 HRESULT hr;
2328 static const PROPERTYKEY deviceinterface_key = {
2329 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2332 if(WINMM_IsMapper(info->index)){
2333 if(info->str){
2334 if(*info->len_bytes < sizeof(WCHAR))
2335 return MMSYSERR_INVALPARAM;
2336 *info->str = 0;
2337 }else
2338 *info->len_bytes = sizeof(WCHAR);
2339 return MMSYSERR_NOERROR;
2342 if(info->is_out){
2343 if(info->index >= g_outmmdevices_count)
2344 return MMSYSERR_INVALHANDLE;
2346 mmdevice = &g_out_mmdevices[info->index];
2347 }else{
2348 if(info->index >= g_inmmdevices_count)
2349 return MMSYSERR_INVALHANDLE;
2351 mmdevice = &g_in_mmdevices[info->index];
2354 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2355 &device);
2356 if(FAILED(hr)){
2357 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2358 return MMSYSERR_ERROR;
2361 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2362 if(FAILED(hr)){
2363 WARN("OpenPropertyStore failed: %08x\n", hr);
2364 IMMDevice_Release(device);
2365 return MMSYSERR_ERROR;
2368 PropVariantInit(&pv);
2369 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2370 if(FAILED(hr)){
2371 WARN("GetValue failed: %08x\n", hr);
2372 IPropertyStore_Release(ps);
2373 IMMDevice_Release(device);
2374 return MMSYSERR_ERROR;
2376 if(pv.vt != VT_LPWSTR){
2377 WARN("Got unexpected property type: %u\n", pv.vt);
2378 PropVariantClear(&pv);
2379 IPropertyStore_Release(ps);
2380 IMMDevice_Release(device);
2381 return MMSYSERR_ERROR;
2384 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2386 if(info->str){
2387 if(len_bytes > *info->len_bytes){
2388 PropVariantClear(&pv);
2389 IPropertyStore_Release(ps);
2390 IMMDevice_Release(device);
2391 return MMSYSERR_INVALPARAM;
2394 memcpy(info->str, pv.u.pwszVal, len_bytes);
2395 }else
2396 *info->len_bytes = len_bytes;
2398 PropVariantClear(&pv);
2399 IPropertyStore_Release(ps);
2400 IMMDevice_Release(device);
2402 return MMSYSERR_NOERROR;
2405 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2406 LPARAM lparam)
2408 switch(msg){
2409 case WODM_OPEN:
2410 return WOD_Open((WINMM_OpenInfo*)wparam);
2411 case WODM_CLOSE:
2412 return WOD_Close((HWAVEOUT)wparam);
2413 case WIDM_OPEN:
2414 return WID_Open((WINMM_OpenInfo*)wparam);
2415 case WIDM_CLOSE:
2416 return WID_Close((HWAVEIN)wparam);
2417 case MXDM_GETCONTROLDETAILS:
2418 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2419 case MXDM_SETCONTROLDETAILS:
2420 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2421 case DRV_QUERYDEVICEINTERFACESIZE:
2422 case DRV_QUERYDEVICEINTERFACE:
2423 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2425 return DefWindowProcW(hwnd, msg, wparam, lparam);
2428 static BOOL WINMM_DevicesThreadDone(void)
2430 UINT i;
2432 EnterCriticalSection(&g_devthread_lock);
2434 if(g_devthread_token > 0){
2435 LeaveCriticalSection(&g_devthread_lock);
2436 return FALSE;
2439 for(i = 0; i < g_devhandle_count; ++i){
2440 if(g_handle_devices[i]->open){
2441 LeaveCriticalSection(&g_devthread_lock);
2442 return FALSE;
2446 DestroyWindow(g_devices_hwnd);
2447 g_devices_hwnd = NULL;
2448 IMMDeviceEnumerator_Release(g_devenum);
2449 g_devenum = NULL;
2450 CoUninitialize();
2452 LeaveCriticalSection(&g_devthread_lock);
2454 return TRUE;
2457 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2459 HANDLE evt = arg;
2460 HRESULT hr;
2461 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2463 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2464 if(FAILED(hr)){
2465 WARN("CoInitializeEx failed: %08x\n", hr);
2466 FreeLibraryAndExitThread(g_devthread_module, 1);
2469 hr = WINMM_InitMMDevices();
2470 if(FAILED(hr)){
2471 CoUninitialize();
2472 FreeLibraryAndExitThread(g_devthread_module, 1);
2475 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2476 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2477 if(FAILED(hr)){
2478 WARN("CoCreateInstance failed: %08x\n", hr);
2479 CoUninitialize();
2480 FreeLibraryAndExitThread(g_devthread_module, 1);
2483 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2484 HWND_MESSAGE, NULL, NULL, NULL);
2485 if(!g_devices_hwnd){
2486 WARN("CreateWindow failed: %d\n", GetLastError());
2487 CoUninitialize();
2488 FreeLibraryAndExitThread(g_devthread_module, 1);
2491 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2492 (LONG_PTR)WINMM_DevicesMsgProc);
2494 /* inform caller that the thread is ready to process messages */
2495 SetEvent(evt);
2496 evt = NULL; /* do not use after this point */
2498 while(1){
2499 DWORD wait;
2500 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2501 FALSE, INFINITE, QS_ALLINPUT);
2502 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2503 MSG msg;
2504 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2505 WARN("Unexpected message: 0x%x\n", msg.message);
2506 if(!g_devices_hwnd)
2507 break;
2508 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2509 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2510 if(device->render)
2511 WOD_PushData(device);
2512 else
2513 WID_PullData(device);
2514 }else
2515 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2516 GetLastError());
2517 if(WINMM_DevicesThreadDone()){
2518 TRACE("Quitting devices thread\n");
2519 FreeLibraryAndExitThread(g_devthread_module, 0);
2523 FreeLibraryAndExitThread(g_devthread_module, 0);
2526 /* on success, increments g_devthread_token to prevent
2527 * device thread shutdown. caller must decrement. */
2528 static BOOL WINMM_StartDevicesThread(void)
2530 HANDLE events[2];
2531 DWORD wait;
2533 EnterCriticalSection(&g_devthread_lock);
2535 if(g_devices_hwnd){
2536 wait = WaitForSingleObject(g_devices_thread, 0);
2537 if(wait == WAIT_TIMEOUT){
2538 /* thread still running */
2539 InterlockedIncrement(&g_devthread_token);
2540 LeaveCriticalSection(&g_devthread_lock);
2541 return TRUE;
2543 if(wait != WAIT_OBJECT_0){
2544 /* error */
2545 LeaveCriticalSection(&g_devthread_lock);
2546 return FALSE;
2548 TRACE("Devices thread left dangling message window?\n");
2549 g_devices_hwnd = NULL;
2550 CloseHandle(g_devices_thread);
2551 g_devices_thread = NULL;
2552 }else if(g_devices_thread){
2553 WaitForSingleObject(g_devices_thread, INFINITE);
2554 CloseHandle(g_devices_thread);
2555 g_devices_thread = NULL;
2558 TRACE("Starting up devices thread\n");
2560 /* The devices thread holds a reference to the winmm module
2561 * to prevent it from unloading while it's running. */
2562 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2563 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2565 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2567 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2568 events[0], 0, NULL);
2569 if(!g_devices_thread){
2570 LeaveCriticalSection(&g_devthread_lock);
2571 CloseHandle(events[0]);
2572 FreeLibrary(g_devthread_module);
2573 return FALSE;
2576 events[1] = g_devices_thread;
2577 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2578 CloseHandle(events[0]);
2579 if(wait != WAIT_OBJECT_0){
2580 if(wait == 1 + WAIT_OBJECT_0){
2581 CloseHandle(g_devices_thread);
2582 g_devices_thread = NULL;
2583 g_devices_hwnd = NULL;
2585 LeaveCriticalSection(&g_devthread_lock);
2586 return FALSE;
2589 InterlockedIncrement(&g_devthread_token);
2591 LeaveCriticalSection(&g_devthread_lock);
2593 return TRUE;
2596 /**************************************************************************
2597 * waveOutGetNumDevs [WINMM.@]
2599 UINT WINAPI waveOutGetNumDevs(void)
2601 HRESULT hr = WINMM_InitMMDevices();
2602 if(FAILED(hr))
2603 return 0;
2605 TRACE("count: %u\n", g_outmmdevices_count);
2607 return g_outmmdevices_count;
2610 /**************************************************************************
2611 * waveOutGetDevCapsA [WINMM.@]
2613 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2614 UINT uSize)
2616 WAVEOUTCAPSW wocW;
2617 UINT ret;
2619 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2621 if(!lpCaps)
2622 return MMSYSERR_INVALPARAM;
2624 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2626 if (ret == MMSYSERR_NOERROR) {
2627 WAVEOUTCAPSA wocA;
2628 wocA.wMid = wocW.wMid;
2629 wocA.wPid = wocW.wPid;
2630 wocA.vDriverVersion = wocW.vDriverVersion;
2631 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2632 sizeof(wocA.szPname), NULL, NULL );
2633 wocA.dwFormats = wocW.dwFormats;
2634 wocA.wChannels = wocW.wChannels;
2635 wocA.dwSupport = wocW.dwSupport;
2636 wocA.wReserved1 = wocW.wReserved1;
2637 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2639 return ret;
2642 /**************************************************************************
2643 * waveOutGetDevCapsW [WINMM.@]
2645 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2646 UINT uSize)
2648 WAVEOUTCAPSW mapper_caps, *caps;
2649 HRESULT hr;
2651 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2653 hr = WINMM_InitMMDevices();
2654 if(FAILED(hr))
2655 return MMSYSERR_NODRIVER;
2657 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2659 if(WINMM_IsMapper(uDeviceID)){
2660 mapper_caps.wMid = 0xFF;
2661 mapper_caps.wPid = 0xFF;
2662 mapper_caps.vDriverVersion = 0x00010001;
2663 mapper_caps.dwFormats = 0xFFFFFFFF;
2664 mapper_caps.wReserved1 = 0;
2665 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2666 WAVECAPS_SAMPLEACCURATE;
2667 mapper_caps.wChannels = 2;
2668 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
2670 caps = &mapper_caps;
2671 }else{
2672 if(uDeviceID >= g_outmmdevices_count)
2673 return MMSYSERR_BADDEVICEID;
2675 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2678 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2680 return MMSYSERR_NOERROR;
2683 /**************************************************************************
2684 * waveOutGetErrorTextA [WINMM.@]
2685 * waveInGetErrorTextA [WINMM.@]
2687 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2689 UINT ret;
2691 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2692 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2693 else
2695 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2696 if (!xstr) ret = MMSYSERR_NOMEM;
2697 else
2699 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2700 if (ret == MMSYSERR_NOERROR)
2701 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2702 HeapFree(GetProcessHeap(), 0, xstr);
2705 return ret;
2708 /**************************************************************************
2709 * waveOutGetErrorTextW [WINMM.@]
2710 * waveInGetErrorTextW [WINMM.@]
2712 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2714 UINT ret = MMSYSERR_BADERRNUM;
2716 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2717 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2718 else if (
2719 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2720 * a warning for the test was always true */
2721 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2722 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2723 if (LoadStringW(hWinMM32Instance,
2724 uError, lpText, uSize) > 0) {
2725 ret = MMSYSERR_NOERROR;
2728 return ret;
2731 /**************************************************************************
2732 * waveOutOpen [WINMM.@]
2733 * All the args/structs have the same layout as the win16 equivalents
2735 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2736 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2737 DWORD_PTR dwInstance, DWORD dwFlags)
2739 LRESULT res;
2740 WINMM_OpenInfo info;
2741 WINMM_CBInfo cb_info;
2743 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2744 dwCallback, dwInstance, dwFlags);
2746 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2747 return MMSYSERR_INVALPARAM;
2749 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2750 if(res != MMSYSERR_NOERROR)
2751 return res;
2753 if(!WINMM_StartDevicesThread())
2754 return MMSYSERR_NODRIVER;
2756 info.handle = 0;
2757 info.format = (WAVEFORMATEX*)lpFormat;
2758 info.callback = dwCallback;
2759 info.cb_user = dwInstance;
2760 info.req_device = uDeviceID;
2761 info.flags = dwFlags;
2762 info.reset = TRUE;
2764 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2765 InterlockedDecrement(&g_devthread_token);
2766 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2767 return res;
2769 if(lphWaveOut)
2770 *lphWaveOut = (HWAVEOUT)info.handle;
2772 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2773 cb_info.callback = dwCallback;
2774 cb_info.user = dwInstance;
2775 cb_info.hwave = info.handle;
2777 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2779 return res;
2782 /**************************************************************************
2783 * waveOutClose [WINMM.@]
2785 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2787 UINT res;
2788 WINMM_Device *device;
2789 WINMM_CBInfo cb_info;
2791 TRACE("(%p)\n", hWaveOut);
2793 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2795 if(!WINMM_ValidateAndLock(device))
2796 return MMSYSERR_INVALHANDLE;
2798 cb_info = device->cb_info;
2800 LeaveCriticalSection(&device->lock);
2802 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2804 if(res == MMSYSERR_NOERROR)
2805 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2807 return res;
2810 /**************************************************************************
2811 * waveOutPrepareHeader [WINMM.@]
2813 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2814 WAVEHDR* lpWaveOutHdr, UINT uSize)
2816 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2818 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2819 return MMSYSERR_INVALPARAM;
2821 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2822 return MMSYSERR_NOERROR;
2824 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2827 /**************************************************************************
2828 * waveOutUnprepareHeader [WINMM.@]
2830 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2831 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2833 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2835 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2836 return MMSYSERR_INVALPARAM;
2838 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2839 return WAVERR_STILLPLAYING;
2841 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2842 return MMSYSERR_NOERROR;
2844 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2847 /**************************************************************************
2848 * waveOutWrite [WINMM.@]
2850 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2852 WINMM_Device *device;
2853 MMRESULT mr;
2855 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2857 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2859 if(!WINMM_ValidateAndLock(device))
2860 return MMSYSERR_INVALHANDLE;
2862 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2863 LeaveCriticalSection(&device->lock);
2864 return WAVERR_UNPREPARED;
2867 if(header->dwFlags & WHDR_INQUEUE){
2868 LeaveCriticalSection(&device->lock);
2869 return WAVERR_STILLPLAYING;
2872 TRACE("dwBufferLength: %u\n", header->dwBufferLength);
2874 if(device->acm_handle){
2875 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2877 ash->cbSrcLength = header->dwBufferLength;
2878 mr = acmStreamConvert(device->acm_handle, ash, 0);
2879 if(mr != MMSYSERR_NOERROR){
2880 LeaveCriticalSection(&device->lock);
2881 return mr;
2885 if(device->first){
2886 device->last->lpNext = header;
2887 device->last = header;
2888 if(!device->playing)
2889 device->playing = header;
2890 }else{
2891 device->playing = device->first = device->last = header;
2892 if(header->dwFlags & WHDR_BEGINLOOP){
2893 device->loop_counter = header->dwLoops;
2894 device->loop_start = header;
2898 header->lpNext = NULL;
2899 header->dwFlags &= ~WHDR_DONE;
2900 header->dwFlags |= WHDR_INQUEUE;
2902 mr = WINMM_BeginPlaying(device);
2904 LeaveCriticalSection(&device->lock);
2906 return mr;
2909 /**************************************************************************
2910 * waveOutBreakLoop [WINMM.@]
2912 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2914 WINMM_Device *device;
2916 TRACE("(%p)\n", hWaveOut);
2918 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2920 if(!WINMM_ValidateAndLock(device))
2921 return MMSYSERR_INVALHANDLE;
2923 device->loop_counter = 0;
2925 LeaveCriticalSection(&device->lock);
2927 return MMSYSERR_NOERROR;
2930 /**************************************************************************
2931 * waveOutPause [WINMM.@]
2933 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2935 TRACE("(%p)\n", hWaveOut);
2937 return WINMM_Pause((HWAVE)hWaveOut);
2940 /**************************************************************************
2941 * waveOutReset [WINMM.@]
2943 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2945 TRACE("(%p)\n", hWaveOut);
2947 return WINMM_Reset((HWAVE)hWaveOut);
2950 /**************************************************************************
2951 * waveOutRestart [WINMM.@]
2953 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2955 WINMM_Device *device;
2956 MMRESULT mr;
2958 TRACE("(%p)\n", hWaveOut);
2960 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2962 if(!WINMM_ValidateAndLock(device))
2963 return MMSYSERR_INVALHANDLE;
2965 device->stopped = TRUE;
2967 mr = WINMM_BeginPlaying(device);
2969 LeaveCriticalSection(&device->lock);
2971 return mr;
2974 /**************************************************************************
2975 * waveOutGetPosition [WINMM.@]
2977 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2978 UINT uSize)
2980 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2982 if(!uSize || !lpTime)
2983 return MMSYSERR_INVALPARAM;
2985 if(uSize < sizeof(MMTIME))
2986 return MMSYSERR_ERROR;
2988 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2991 /**************************************************************************
2992 * waveOutGetPitch [WINMM.@]
2994 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2996 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2997 return MMSYSERR_NOTSUPPORTED;
3000 /**************************************************************************
3001 * waveOutSetPitch [WINMM.@]
3003 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3005 TRACE("(%p, %08x)\n", hWaveOut, dw);
3007 return MMSYSERR_NOTSUPPORTED;
3010 /**************************************************************************
3011 * waveOutGetPlaybackRate [WINMM.@]
3013 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3015 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3017 return MMSYSERR_NOTSUPPORTED;
3020 /**************************************************************************
3021 * waveOutSetPlaybackRate [WINMM.@]
3023 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3025 TRACE("(%p, %08x)\n", hWaveOut, dw);
3027 return MMSYSERR_NOTSUPPORTED;
3030 /**************************************************************************
3031 * waveOutGetVolume [WINMM.@]
3033 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3035 WINMM_Device *device;
3036 UINT32 channels;
3037 float *vols;
3038 HRESULT hr;
3040 TRACE("(%p, %p)\n", hWaveOut, out);
3042 if(!out)
3043 return MMSYSERR_INVALPARAM;
3045 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3047 if(!WINMM_ValidateAndLock(device))
3048 return MMSYSERR_INVALHANDLE;
3050 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3051 if(FAILED(hr)){
3052 LeaveCriticalSection(&device->lock);
3053 WARN("GetChannelCount failed: %08x\n", hr);
3054 return MMSYSERR_ERROR;
3057 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3058 if(!vols){
3059 LeaveCriticalSection(&device->lock);
3060 return MMSYSERR_NOMEM;
3063 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3064 if(FAILED(hr)){
3065 LeaveCriticalSection(&device->lock);
3066 HeapFree(GetProcessHeap(), 0, vols);
3067 WARN("GetAllVolumes failed: %08x\n", hr);
3068 return MMSYSERR_ERROR;
3071 LeaveCriticalSection(&device->lock);
3073 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3074 if(channels > 1)
3075 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3077 HeapFree(GetProcessHeap(), 0, vols);
3079 return MMSYSERR_NOERROR;
3082 /**************************************************************************
3083 * waveOutSetVolume [WINMM.@]
3085 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3087 WINMM_Device *device;
3088 UINT32 channels;
3089 float *vols;
3090 HRESULT hr;
3092 TRACE("(%p, %08x)\n", hWaveOut, in);
3094 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3096 if(!WINMM_ValidateAndLock(device))
3097 return MMSYSERR_INVALHANDLE;
3099 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3100 if(FAILED(hr)){
3101 LeaveCriticalSection(&device->lock);
3102 WARN("GetChannelCount failed: %08x\n", hr);
3103 return MMSYSERR_ERROR;
3106 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3107 if(!vols){
3108 LeaveCriticalSection(&device->lock);
3109 return MMSYSERR_NOMEM;
3112 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3113 if(FAILED(hr)){
3114 LeaveCriticalSection(&device->lock);
3115 HeapFree(GetProcessHeap(), 0, vols);
3116 WARN("GetAllVolumes failed: %08x\n", hr);
3117 return MMSYSERR_ERROR;
3120 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
3121 if(channels > 1)
3122 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
3124 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3125 if(FAILED(hr)){
3126 LeaveCriticalSection(&device->lock);
3127 HeapFree(GetProcessHeap(), 0, vols);
3128 WARN("SetAllVolumes failed: %08x\n", hr);
3129 return MMSYSERR_ERROR;
3132 LeaveCriticalSection(&device->lock);
3134 HeapFree(GetProcessHeap(), 0, vols);
3136 return MMSYSERR_NOERROR;
3139 /**************************************************************************
3140 * waveOutGetID [WINMM.@]
3142 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3144 WINMM_Device *device;
3145 UINT dev, junk;
3146 BOOL is_out;
3148 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3150 if(!lpuDeviceID)
3151 return MMSYSERR_INVALPARAM;
3153 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3154 if(!WINMM_ValidateAndLock(device))
3155 return MMSYSERR_INVALHANDLE;
3157 LeaveCriticalSection(&device->lock);
3159 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3161 return MMSYSERR_NOERROR;
3164 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3166 UINT count;
3167 WINMM_MMDevice **devices;
3169 TRACE("(%u, %p, %d)\n", device, len, is_out);
3171 if(is_out){
3172 count = g_outmmdevices_count;
3173 devices = g_out_map;
3174 }else{
3175 count = g_inmmdevices_count;
3176 devices = g_in_map;
3179 if(device >= count)
3180 return MMSYSERR_INVALHANDLE;
3182 EnterCriticalSection(&g_devthread_lock);
3183 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3184 LeaveCriticalSection(&g_devthread_lock);
3186 return MMSYSERR_NOERROR;
3189 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3190 BOOL is_out)
3192 UINT count, id_len;
3193 WINMM_MMDevice **devices;
3195 TRACE("(%u, %p, %d)\n", device, str, is_out);
3197 if(is_out){
3198 count = g_outmmdevices_count;
3199 devices = g_out_map;
3200 }else{
3201 count = g_inmmdevices_count;
3202 devices = g_in_map;
3205 if(device >= count)
3206 return MMSYSERR_INVALHANDLE;
3208 EnterCriticalSection(&g_devthread_lock);
3209 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3210 if(len < id_len){
3211 LeaveCriticalSection(&g_devthread_lock);
3212 return MMSYSERR_ERROR;
3215 memcpy(str, devices[device]->dev_id, id_len);
3216 LeaveCriticalSection(&g_devthread_lock);
3218 return MMSYSERR_NOERROR;
3221 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3223 WINMM_QueryInterfaceInfo info;
3224 UINT ret;
3226 if(!WINMM_StartDevicesThread())
3227 return MMSYSERR_NODRIVER;
3229 info.is_out = is_out;
3230 info.index = index;
3231 info.str = out;
3232 info.len_bytes = out_len;
3234 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3235 InterlockedDecrement(&g_devthread_token);
3236 return ret;
3239 /**************************************************************************
3240 * waveOutMessage [WINMM.@]
3242 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3243 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3245 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3247 switch(uMessage){
3248 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3249 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3250 (DWORD_PTR*)dwParam1, TRUE);
3251 case DRV_QUERYFUNCTIONINSTANCEID:
3252 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3253 case DRV_QUERYDEVICEINTERFACESIZE:
3254 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3255 NULL, (ULONG*)dwParam1);
3256 case DRV_QUERYDEVICEINTERFACE:
3258 ULONG size = dwParam2;
3259 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3260 (WCHAR*)dwParam1, &size);
3262 case DRV_QUERYMAPPABLE:
3263 return MMSYSERR_NOERROR;
3264 case DRVM_MAPPER_PREFERRED_GET:
3265 if(dwParam1) {
3266 if(g_outmmdevices_count > 0)
3267 /* Device 0 is always the default device */
3268 *(DWORD *)dwParam1 = 0;
3269 else
3270 *(DWORD *)dwParam1 = -1;
3273 if(dwParam2)
3274 /* Status flags */
3275 *(DWORD *)dwParam2 = 0;
3277 return MMSYSERR_NOERROR;
3280 TRACE("Message not supported: %u\n", uMessage);
3282 return MMSYSERR_NOTSUPPORTED;
3285 /**************************************************************************
3286 * waveInGetNumDevs [WINMM.@]
3288 UINT WINAPI waveInGetNumDevs(void)
3290 HRESULT hr = WINMM_InitMMDevices();
3291 if(FAILED(hr))
3292 return 0;
3294 TRACE("count: %u\n", g_inmmdevices_count);
3296 return g_inmmdevices_count;
3299 /**************************************************************************
3300 * waveInGetDevCapsW [WINMM.@]
3302 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3304 WAVEINCAPSW mapper_caps, *caps;
3305 HRESULT hr;
3307 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3309 hr = WINMM_InitMMDevices();
3310 if(FAILED(hr))
3311 return MMSYSERR_NODRIVER;
3313 if(!lpCaps)
3314 return MMSYSERR_INVALPARAM;
3316 if(WINMM_IsMapper(uDeviceID)){
3317 mapper_caps.wMid = 0xFF;
3318 mapper_caps.wPid = 0xFF;
3319 mapper_caps.vDriverVersion = 0x00010001;
3320 mapper_caps.dwFormats = 0xFFFFFFFF;
3321 mapper_caps.wReserved1 = 0;
3322 mapper_caps.wChannels = 2;
3323 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
3325 caps = &mapper_caps;
3326 }else{
3327 if(uDeviceID >= g_inmmdevices_count)
3328 return MMSYSERR_BADDEVICEID;
3330 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3333 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3335 return MMSYSERR_NOERROR;
3338 /**************************************************************************
3339 * waveInGetDevCapsA [WINMM.@]
3341 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3343 UINT ret;
3344 WAVEINCAPSW wicW;
3346 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3348 if(!lpCaps)
3349 return MMSYSERR_INVALPARAM;
3351 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3353 if (ret == MMSYSERR_NOERROR) {
3354 WAVEINCAPSA wicA;
3355 wicA.wMid = wicW.wMid;
3356 wicA.wPid = wicW.wPid;
3357 wicA.vDriverVersion = wicW.vDriverVersion;
3358 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3359 sizeof(wicA.szPname), NULL, NULL );
3360 wicA.dwFormats = wicW.dwFormats;
3361 wicA.wChannels = wicW.wChannels;
3362 wicA.wReserved1 = wicW.wReserved1;
3363 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3365 return ret;
3368 /**************************************************************************
3369 * waveInOpen [WINMM.@]
3371 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3372 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3373 DWORD_PTR dwInstance, DWORD dwFlags)
3375 LRESULT res;
3376 WINMM_OpenInfo info;
3377 WINMM_CBInfo cb_info;
3379 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3380 dwCallback, dwInstance, dwFlags);
3382 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3383 return MMSYSERR_INVALPARAM;
3385 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3386 if(res != MMSYSERR_NOERROR)
3387 return res;
3389 if(!WINMM_StartDevicesThread())
3390 return MMSYSERR_NODRIVER;
3392 info.handle = 0;
3393 info.format = (WAVEFORMATEX*)lpFormat;
3394 info.callback = dwCallback;
3395 info.cb_user = dwInstance;
3396 info.req_device = uDeviceID;
3397 info.flags = dwFlags;
3398 info.reset = TRUE;
3400 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3401 InterlockedDecrement(&g_devthread_token);
3402 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3403 return res;
3405 if(lphWaveIn)
3406 *lphWaveIn = (HWAVEIN)info.handle;
3408 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3409 cb_info.callback = dwCallback;
3410 cb_info.user = dwInstance;
3411 cb_info.hwave = info.handle;
3413 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3415 return res;
3418 /**************************************************************************
3419 * waveInClose [WINMM.@]
3421 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3423 WINMM_Device *device;
3424 WINMM_CBInfo cb_info;
3425 UINT res;
3427 TRACE("(%p)\n", hWaveIn);
3429 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3431 if(!WINMM_ValidateAndLock(device))
3432 return MMSYSERR_INVALHANDLE;
3434 cb_info = device->cb_info;
3436 LeaveCriticalSection(&device->lock);
3438 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3440 if(res == MMSYSERR_NOERROR)
3441 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3443 return res;
3446 /**************************************************************************
3447 * waveInPrepareHeader [WINMM.@]
3449 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3450 UINT uSize)
3452 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3454 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3455 return MMSYSERR_INVALPARAM;
3457 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3458 return MMSYSERR_NOERROR;
3460 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3463 /**************************************************************************
3464 * waveInUnprepareHeader [WINMM.@]
3466 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3467 UINT uSize)
3469 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3471 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3472 return MMSYSERR_INVALPARAM;
3474 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3475 return WAVERR_STILLPLAYING;
3477 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3478 return MMSYSERR_NOERROR;
3480 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3483 /**************************************************************************
3484 * waveInAddBuffer [WINMM.@]
3486 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3488 WINMM_Device *device;
3490 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3492 if(!header || uSize < sizeof(WAVEHDR))
3493 return MMSYSERR_INVALPARAM;
3495 if(!(header->dwFlags & WHDR_PREPARED))
3496 return WAVERR_UNPREPARED;
3498 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3500 if(!WINMM_ValidateAndLock(device))
3501 return MMSYSERR_INVALHANDLE;
3503 if(!device->first)
3504 device->first = device->last = header;
3505 else{
3506 device->last->lpNext = header;
3507 device->last = header;
3510 header->dwBytesRecorded = 0;
3511 header->lpNext = NULL;
3512 header->dwFlags &= ~WHDR_DONE;
3513 header->dwFlags |= WHDR_INQUEUE;
3515 LeaveCriticalSection(&device->lock);
3517 return MMSYSERR_NOERROR;
3520 /**************************************************************************
3521 * waveInReset [WINMM.@]
3523 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3525 TRACE("(%p)\n", hWaveIn);
3527 return WINMM_Reset((HWAVE)hWaveIn);
3530 /**************************************************************************
3531 * waveInStart [WINMM.@]
3533 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3535 WINMM_Device *device;
3536 MMRESULT mr;
3538 TRACE("(%p)\n", hWaveIn);
3540 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3542 if(!WINMM_ValidateAndLock(device))
3543 return MMSYSERR_INVALHANDLE;
3545 mr = WINMM_BeginPlaying(device);
3547 LeaveCriticalSection(&device->lock);
3549 return mr;
3552 /**************************************************************************
3553 * waveInStop [WINMM.@]
3555 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3557 WINMM_CBInfo cb_info;
3558 WINMM_Device *device;
3559 WAVEHDR *buf;
3560 HRESULT hr;
3562 TRACE("(%p)\n", hWaveIn);
3564 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3566 if(!WINMM_ValidateAndLock(device))
3567 return MMSYSERR_INVALHANDLE;
3569 hr = WINMM_Pause((HWAVE)hWaveIn);
3570 if(FAILED(hr)){
3571 LeaveCriticalSection(&device->lock);
3572 return MMSYSERR_ERROR;
3574 device->stopped = TRUE;
3576 buf = device->first;
3577 if(buf && buf->dwBytesRecorded > 0){
3578 device->first = buf->lpNext;
3579 }else
3580 buf = NULL;
3582 cb_info = device->cb_info;
3584 LeaveCriticalSection(&device->lock);
3586 if(buf){
3587 buf->dwFlags &= ~WHDR_INQUEUE;
3588 buf->dwFlags |= WHDR_DONE;
3589 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3592 return MMSYSERR_NOERROR;
3595 /**************************************************************************
3596 * waveInGetPosition [WINMM.@]
3598 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3599 UINT uSize)
3601 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3603 if(!uSize || !lpTime)
3604 return MMSYSERR_INVALPARAM;
3606 if(uSize < sizeof(MMTIME))
3607 return MMSYSERR_ERROR;
3609 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3612 /**************************************************************************
3613 * waveInGetID [WINMM.@]
3615 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3617 UINT dev, junk;
3618 BOOL is_out;
3619 WINMM_Device *device;
3621 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3623 if(!lpuDeviceID)
3624 return MMSYSERR_INVALPARAM;
3626 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3627 if(!WINMM_ValidateAndLock(device))
3628 return MMSYSERR_INVALHANDLE;
3630 LeaveCriticalSection(&device->lock);
3632 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3634 return MMSYSERR_NOERROR;
3637 /**************************************************************************
3638 * waveInMessage [WINMM.@]
3640 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3641 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3643 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3645 switch(uMessage){
3646 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3647 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3648 (DWORD_PTR*)dwParam1, FALSE);
3649 case DRV_QUERYFUNCTIONINSTANCEID:
3650 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3651 case DRV_QUERYDEVICEINTERFACESIZE:
3652 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3653 NULL, (ULONG*)dwParam1);
3654 case DRV_QUERYDEVICEINTERFACE:
3656 ULONG size = dwParam2;
3657 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3658 (WCHAR*)dwParam1, &size);
3660 case DRV_QUERYMAPPABLE:
3661 return MMSYSERR_NOERROR;
3662 case DRVM_MAPPER_PREFERRED_GET:
3663 if(dwParam1) {
3664 if(g_inmmdevices_count > 0)
3665 /* Device 0 is always the default device */
3666 *(DWORD *)dwParam1 = 0;
3667 else
3668 *(DWORD *)dwParam1 = -1;
3671 if(dwParam2)
3672 /* Status flags */
3673 *(DWORD *)dwParam2 = 0;
3675 return MMSYSERR_NOERROR;
3678 TRACE("Message not supported: %u\n", uMessage);
3680 return MMSYSERR_NOTSUPPORTED;
3683 UINT WINAPI mixerGetNumDevs(void)
3685 HRESULT hr;
3687 TRACE("\n");
3689 hr = WINMM_InitMMDevices();
3690 if(FAILED(hr))
3691 return 0;
3693 return g_outmmdevices_count + g_inmmdevices_count;
3696 /**************************************************************************
3697 * mixerGetDevCapsA [WINMM.@]
3699 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3701 MIXERCAPSW micW;
3702 UINT ret;
3704 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3706 if(!lpCaps)
3707 return MMSYSERR_INVALPARAM;
3709 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3711 if (ret == MMSYSERR_NOERROR) {
3712 MIXERCAPSA micA;
3713 micA.wMid = micW.wMid;
3714 micA.wPid = micW.wPid;
3715 micA.vDriverVersion = micW.vDriverVersion;
3716 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3717 sizeof(micA.szPname), NULL, NULL );
3718 micA.fdwSupport = micW.fdwSupport;
3719 micA.cDestinations = micW.cDestinations;
3720 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3722 return ret;
3725 /**************************************************************************
3726 * mixerGetDevCapsW [WINMM.@]
3728 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3730 WINMM_MMDevice *mmdevice;
3731 MIXERCAPSW caps;
3732 HRESULT hr;
3734 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3736 hr = WINMM_InitMMDevices();
3737 if(FAILED(hr))
3738 return MMSYSERR_NODRIVER;
3740 if(!lpCaps)
3741 return MMSYSERR_INVALPARAM;
3743 if(!uSize)
3744 return MMSYSERR_NOERROR;
3746 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3747 mmdevice = WINMM_GetMixerMMDevice((HMIXEROBJ)uDeviceID,
3748 MIXER_OBJECTF_MIXER, NULL);
3749 else if(uDeviceID < g_outmmdevices_count)
3750 mmdevice = read_map(g_out_map, uDeviceID);
3751 else
3752 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3754 if(!mmdevice)
3755 return MMSYSERR_BADDEVICEID;
3757 if(mmdevice->dataflow == eRender)
3758 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3759 else
3760 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3762 caps.wMid = 0xFF;
3763 caps.wPid = 0xFF;
3764 caps.vDriverVersion = 0x00010001;
3765 caps.fdwSupport = 0;
3766 caps.cDestinations = 1;
3768 memcpy(lpCaps, &caps, uSize);
3770 return MMSYSERR_NOERROR;
3773 /**************************************************************************
3774 * mixerOpen [WINMM.@]
3776 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3777 DWORD_PTR dwInstance, DWORD fdwOpen)
3779 WINMM_MMDevice *mmdevice;
3780 MMRESULT mr;
3781 HRESULT hr;
3783 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3784 dwInstance, fdwOpen);
3786 hr = WINMM_InitMMDevices();
3787 if(FAILED(hr))
3788 return MMSYSERR_NODRIVER;
3790 if(!lphMix)
3791 return MMSYSERR_INVALPARAM;
3793 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3794 if(mr != MMSYSERR_NOERROR)
3795 return mr;
3797 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3798 return MMSYSERR_BADDEVICEID;
3800 if(uDeviceID < g_outmmdevices_count){
3801 mmdevice = read_map(g_out_map, uDeviceID);
3802 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3803 mmdevice->mixer_count);
3804 }else{
3805 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3806 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3807 FALSE, mmdevice->mixer_count);
3810 ++mmdevice->mixer_count;
3812 return MMSYSERR_NOERROR;
3815 /**************************************************************************
3816 * mixerClose [WINMM.@]
3818 UINT WINAPI mixerClose(HMIXER hMix)
3820 TRACE("(%p)\n", hMix);
3822 return MMSYSERR_NOERROR;
3825 /**************************************************************************
3826 * mixerGetID [WINMM.@]
3828 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3830 WINMM_MMDevice *mmdevice;
3831 HRESULT hr;
3833 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3835 hr = WINMM_InitMMDevices();
3836 if(FAILED(hr))
3837 return MMSYSERR_NODRIVER;
3839 if(!lpid)
3840 return MMSYSERR_INVALPARAM;
3842 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3843 if(!mmdevice)
3844 return MMSYSERR_INVALHANDLE;
3846 if(mmdevice->in_caps.szPname[0] != '\0')
3847 *lpid += g_outmmdevices_count;
3849 return MMSYSERR_NOERROR;
3852 /**************************************************************************
3853 * mixerGetControlDetailsW [WINMM.@]
3855 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3856 DWORD fdwDetails)
3858 WINMM_ControlDetails details;
3860 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3862 if(!WINMM_StartDevicesThread())
3863 return MMSYSERR_NODRIVER;
3865 if(!lpmcdW || !lpmcdW->paDetails)
3866 return MMSYSERR_INVALPARAM;
3868 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3870 details.hmix = hmix;
3871 details.details = lpmcdW;
3872 details.flags = fdwDetails;
3874 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3875 (DWORD_PTR)&details, 0);
3878 /**************************************************************************
3879 * mixerGetControlDetailsA [WINMM.@]
3881 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3882 DWORD fdwDetails)
3884 UINT ret = MMSYSERR_NOTSUPPORTED;
3886 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3888 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3889 return MMSYSERR_INVALPARAM;
3891 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3892 case MIXER_GETCONTROLDETAILSF_VALUE:
3893 /* can safely use A structure as it is, no string inside */
3894 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3895 break;
3896 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3898 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3899 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3900 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3901 unsigned int i;
3903 if (lpmcdA->u.cMultipleItems != 0) {
3904 size *= lpmcdA->u.cMultipleItems;
3906 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3907 lpmcdA->paDetails = pDetailsW;
3908 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3909 /* set up lpmcd->paDetails */
3910 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3911 /* copy from lpmcd->paDetails back to paDetailsW; */
3912 if (ret == MMSYSERR_NOERROR) {
3913 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3914 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3915 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3916 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3917 pDetailsA->szName,
3918 sizeof(pDetailsA->szName), NULL, NULL );
3919 pDetailsA++;
3920 pDetailsW++;
3922 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3923 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3925 HeapFree(GetProcessHeap(), 0, pDetailsW);
3926 lpmcdA->paDetails = pDetailsA;
3927 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3929 break;
3930 default:
3931 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3934 return ret;
3937 /**************************************************************************
3938 * mixerGetLineControlsA [WINMM.@]
3940 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3941 DWORD fdwControls)
3943 MIXERLINECONTROLSW mlcW;
3944 DWORD ret;
3945 unsigned int i;
3947 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3949 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3950 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3951 return MMSYSERR_INVALPARAM;
3953 mlcW.cbStruct = sizeof(mlcW);
3954 mlcW.dwLineID = lpmlcA->dwLineID;
3955 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3956 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3958 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3959 the control count is assumed to be 1 - This is relied upon by a game,
3960 "Dynomite Deluze" */
3961 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3962 mlcW.cControls = 1;
3963 } else {
3964 mlcW.cControls = lpmlcA->cControls;
3966 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3967 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3968 mlcW.cControls * mlcW.cbmxctrl);
3970 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3972 if (ret == MMSYSERR_NOERROR) {
3973 lpmlcA->dwLineID = mlcW.dwLineID;
3974 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3975 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3977 for (i = 0; i < mlcW.cControls; i++) {
3978 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3979 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3980 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3981 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3982 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3983 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3984 lpmlcA->pamxctrl[i].szShortName,
3985 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3986 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3987 lpmlcA->pamxctrl[i].szName,
3988 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3989 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3990 * sizeof(mlcW.pamxctrl[i].Bounds) */
3991 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3992 sizeof(mlcW.pamxctrl[i].Bounds));
3993 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3994 * sizeof(mlcW.pamxctrl[i].Metrics) */
3995 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3996 sizeof(mlcW.pamxctrl[i].Metrics));
4000 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
4002 return ret;
4005 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4006 MIXERCONTROLW *ctl, DWORD flags)
4008 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4009 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4010 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4011 ctl->cMultipleItems = 0;
4012 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4013 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szName, MIXER_LONG_NAME_CHARS);
4014 ctl->Bounds.s1.dwMinimum = 0;
4015 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4016 ctl->Metrics.cSteps = 192;
4018 return MMSYSERR_NOERROR;
4021 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4022 MIXERCONTROLW *ctl, DWORD flags)
4024 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4025 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4026 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4027 ctl->cMultipleItems = 0;
4028 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4029 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szName, MIXER_LONG_NAME_CHARS);
4030 ctl->Bounds.s1.dwMinimum = 0;
4031 ctl->Bounds.s1.dwMaximum = 1;
4032 ctl->Metrics.cSteps = 0;
4034 return MMSYSERR_NOERROR;
4037 /**************************************************************************
4038 * mixerGetLineControlsW [WINMM.@]
4040 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4041 DWORD fdwControls)
4043 WINMM_MMDevice *mmdevice;
4044 HRESULT hr;
4046 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4048 hr = WINMM_InitMMDevices();
4049 if(FAILED(hr))
4050 return MMSYSERR_NODRIVER;
4052 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4053 MIXER_GETLINECONTROLSF_ONEBYID |
4054 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4055 MIXER_OBJECTF_HMIXER |
4056 MIXER_OBJECTF_MIXER)){
4057 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4058 return MMSYSERR_INVALFLAG;
4061 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4062 return MMSYSERR_INVALPARAM;
4064 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4065 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4066 TRACE("cControls: %u\n", lpmlcW->cControls);
4068 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4069 if(!mmdevice)
4070 return MMSYSERR_INVALHANDLE;
4072 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4073 case MIXER_GETLINECONTROLSF_ALL:
4074 if(lpmlcW->cControls != 2)
4075 return MMSYSERR_INVALPARAM;
4076 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4077 return MMSYSERR_INVALPARAM;
4078 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4079 return MIXERR_INVALLINE;
4080 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4081 &lpmlcW->pamxctrl[0], fdwControls);
4082 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4083 &lpmlcW->pamxctrl[1], fdwControls);
4084 return MMSYSERR_NOERROR;
4085 case MIXER_GETLINECONTROLSF_ONEBYID:
4086 if(lpmlcW->cControls != 1)
4087 return MMSYSERR_INVALPARAM;
4088 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4089 return MMSYSERR_INVALPARAM;
4090 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4091 return MIXERR_INVALLINE;
4092 if(lpmlcW->u.dwControlID == 0)
4093 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4094 lpmlcW->pamxctrl, fdwControls);
4095 if(lpmlcW->u.dwControlID == 1)
4096 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4097 lpmlcW->pamxctrl, fdwControls);
4098 return MMSYSERR_NOTSUPPORTED;
4099 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
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.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4107 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4108 lpmlcW->pamxctrl, fdwControls);
4109 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4110 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4111 lpmlcW->pamxctrl, fdwControls);
4112 return MMSYSERR_NOTSUPPORTED;
4115 return MMSYSERR_NOTSUPPORTED;
4118 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4119 MIXERLINEW *info, DWORD flags)
4121 BOOL is_out = TRUE;
4122 if(mmdevice->in_caps.szPname[0] != '\0')
4123 is_out = FALSE;
4125 if(info->dwSource != 0)
4126 return MIXERR_INVALLINE;
4128 info->dwDestination = 0;
4129 info->dwLineID = 0;
4130 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4131 info->cConnections = 0;
4132 info->cControls = 2;
4133 /* volume & mute always affect all channels, so claim 1 channel */
4134 info->cChannels = 1;
4135 info->Target.dwDeviceID = mmdev_index;
4136 info->Target.wMid = ~0;
4137 info->Target.wPid = ~0;
4138 info->Target.vDriverVersion = 0;
4140 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4141 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4143 if(is_out){
4144 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4145 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4146 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4147 sizeof(info->Target.szPname));
4148 }else{
4149 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4150 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4151 info->Target.szPname[0] = '\0';
4154 return MMSYSERR_NOERROR;
4157 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4158 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4160 BOOL is_out = TRUE;
4161 if(mmdevice->in_caps.szPname[0] != '\0')
4162 is_out = FALSE;
4164 if(info->dwDestination != 0)
4165 return MIXERR_INVALLINE;
4167 info->dwSource = 0xFFFFFFFF;
4168 info->dwLineID = 0xFFFF0000;
4169 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4170 info->cConnections = 1;
4171 info->cControls = 2;
4173 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4174 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4176 info->Target.dwDeviceID = mmdev_index;
4177 info->Target.wMid = ~0;
4178 info->Target.wPid = ~0;
4179 info->Target.vDriverVersion = 0;
4181 if(is_out){
4182 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4183 info->cChannels = mmdevice->out_caps.wChannels;
4184 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4185 info->Target.szPname[0] = '\0';
4186 }else{
4187 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4188 info->cChannels = mmdevice->in_caps.wChannels;
4189 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4190 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4191 sizeof(info->Target.szPname));
4194 return MMSYSERR_NOERROR;
4197 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4198 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4200 BOOL is_out = TRUE;
4201 if(mmdevice->in_caps.szPname[0] != '\0')
4202 is_out = FALSE;
4204 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4205 if(is_out)
4206 return MIXERR_INVALLINE;
4207 info->dwDestination = 0;
4208 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4211 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4212 if(!is_out)
4213 return MIXERR_INVALLINE;
4214 info->dwDestination = 0;
4215 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4218 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4219 if(is_out)
4220 return MIXERR_INVALLINE;
4221 info->dwSource = 0;
4222 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4225 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4226 if(!is_out)
4227 return MIXERR_INVALLINE;
4228 info->dwSource = 0;
4229 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4232 TRACE("Returning INVALLINE on this component type: %u\n",
4233 info->dwComponentType);
4235 return MIXERR_INVALLINE;
4238 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4239 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4241 if(info->dwLineID == 0xFFFF0000){
4242 info->dwDestination = 0;
4243 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4246 if(info->dwLineID == 0){
4247 info->dwSource = 0;
4248 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4251 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4252 return MIXERR_INVALLINE;
4255 /**************************************************************************
4256 * mixerGetLineInfoW [WINMM.@]
4258 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4260 UINT mmdev_index;
4261 WINMM_MMDevice *mmdevice;
4262 HRESULT hr;
4264 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4266 hr = WINMM_InitMMDevices();
4267 if(FAILED(hr))
4268 return MMSYSERR_NODRIVER;
4270 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4271 return MMSYSERR_INVALPARAM;
4273 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4274 TRACE("dwSource: %u\n", lpmliW->dwSource);
4275 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4276 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4277 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4279 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4280 MIXER_GETLINEINFOF_DESTINATION |
4281 MIXER_GETLINEINFOF_LINEID |
4282 MIXER_GETLINEINFOF_SOURCE |
4283 MIXER_GETLINEINFOF_TARGETTYPE |
4284 MIXER_OBJECTF_HMIXER |
4285 MIXER_OBJECTF_MIXER)){
4286 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4287 return MMSYSERR_INVALFLAG;
4290 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4291 if(!mmdevice)
4292 return MMSYSERR_INVALHANDLE;
4294 lpmliW->dwUser = 0;
4296 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4297 case MIXER_GETLINEINFOF_DESTINATION:
4298 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4299 fdwInfo);
4300 case MIXER_GETLINEINFOF_SOURCE:
4301 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4302 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4303 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4304 fdwInfo);
4305 case MIXER_GETLINEINFOF_LINEID:
4306 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4307 case MIXER_GETLINEINFOF_TARGETTYPE:
4308 FIXME("TARGETTYPE flag not implemented!\n");
4309 return MIXERR_INVALLINE;
4312 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo);
4313 return MMSYSERR_INVALFLAG;
4316 /**************************************************************************
4317 * mixerGetLineInfoA [WINMM.@]
4319 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4320 DWORD fdwInfo)
4322 MIXERLINEW mliW;
4323 UINT ret;
4325 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4327 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4328 return MMSYSERR_INVALPARAM;
4330 mliW.cbStruct = sizeof(mliW);
4331 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4332 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4333 mliW.dwComponentType = lpmliA->dwComponentType;
4334 break;
4335 case MIXER_GETLINEINFOF_DESTINATION:
4336 mliW.dwDestination = lpmliA->dwDestination;
4337 break;
4338 case MIXER_GETLINEINFOF_LINEID:
4339 mliW.dwLineID = lpmliA->dwLineID;
4340 break;
4341 case MIXER_GETLINEINFOF_SOURCE:
4342 mliW.dwDestination = lpmliA->dwDestination;
4343 mliW.dwSource = lpmliA->dwSource;
4344 break;
4345 case MIXER_GETLINEINFOF_TARGETTYPE:
4346 mliW.Target.dwType = lpmliA->Target.dwType;
4347 mliW.Target.wMid = lpmliA->Target.wMid;
4348 mliW.Target.wPid = lpmliA->Target.wPid;
4349 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4350 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
4351 break;
4352 default:
4353 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4354 return MMSYSERR_INVALFLAG;
4357 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4359 if(ret == MMSYSERR_NOERROR)
4361 lpmliA->dwDestination = mliW.dwDestination;
4362 lpmliA->dwSource = mliW.dwSource;
4363 lpmliA->dwLineID = mliW.dwLineID;
4364 lpmliA->fdwLine = mliW.fdwLine;
4365 lpmliA->dwUser = mliW.dwUser;
4366 lpmliA->dwComponentType = mliW.dwComponentType;
4367 lpmliA->cChannels = mliW.cChannels;
4368 lpmliA->cConnections = mliW.cConnections;
4369 lpmliA->cControls = mliW.cControls;
4370 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4371 sizeof(lpmliA->szShortName), NULL, NULL);
4372 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4373 sizeof(lpmliA->szName), NULL, NULL );
4374 lpmliA->Target.dwType = mliW.Target.dwType;
4375 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4376 lpmliA->Target.wMid = mliW.Target.wMid;
4377 lpmliA->Target.wPid = mliW.Target.wPid;
4378 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4379 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4380 sizeof(lpmliA->Target.szPname), NULL, NULL );
4382 return ret;
4385 /**************************************************************************
4386 * mixerSetControlDetails [WINMM.@]
4388 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4389 DWORD fdwDetails)
4391 WINMM_ControlDetails details;
4392 UINT ret;
4394 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4396 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4397 MIXER_SETCONTROLDETAILSF_CUSTOM)
4398 return MMSYSERR_NOTSUPPORTED;
4400 if(!lpmcd)
4401 return MMSYSERR_INVALPARAM;
4403 if(!WINMM_StartDevicesThread())
4404 return MMSYSERR_NODRIVER;
4406 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4408 details.hmix = hmix;
4409 details.details = lpmcd;
4410 details.flags = fdwDetails;
4412 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4413 (DWORD_PTR)&details, 0);
4414 InterlockedDecrement(&g_devthread_token);
4415 return ret;
4418 /**************************************************************************
4419 * mixerMessage [WINMM.@]
4421 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4423 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4425 return MMSYSERR_NOTSUPPORTED;