wined3d: Pass a texture and sub-resource index to wined3d_volume_download_data().
[wine.git] / dlls / winmm / waveform.c
blob1915c217934a241241e937b97333a77f6da03456
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 return S_OK;
1424 static LRESULT WOD_Close(HWAVEOUT hwave)
1426 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1428 TRACE("(%p)\n", hwave);
1430 if(!WINMM_ValidateAndLock(device))
1431 return MMSYSERR_INVALHANDLE;
1433 WINMM_CloseDevice(device);
1435 IAudioRenderClient_Release(device->render);
1436 device->render = NULL;
1438 IAudioStreamVolume_Release(device->volume);
1439 device->volume = NULL;
1441 LeaveCriticalSection(&device->lock);
1443 return MMSYSERR_NOERROR;
1446 static LRESULT WID_Close(HWAVEIN hwave)
1448 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1450 TRACE("(%p)\n", hwave);
1452 if(!WINMM_ValidateAndLock(device))
1453 return MMSYSERR_INVALHANDLE;
1455 WINMM_CloseDevice(device);
1457 IAudioCaptureClient_Release(device->capture);
1458 device->capture = NULL;
1460 LeaveCriticalSection(&device->lock);
1462 return MMSYSERR_NOERROR;
1465 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1467 return length - length % device->bytes_per_frame;
1470 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1472 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1474 TRACE("(%p, %p)\n", hwave, header);
1476 if(!WINMM_ValidateAndLock(device))
1477 return MMSYSERR_INVALHANDLE;
1479 if(device->render && device->acm_handle){
1480 ACMSTREAMHEADER *ash;
1481 DWORD size;
1482 MMRESULT mr;
1484 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1485 ACM_STREAMSIZEF_SOURCE);
1486 if(mr != MMSYSERR_NOERROR){
1487 LeaveCriticalSection(&device->lock);
1488 return mr;
1491 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1492 if(!ash){
1493 LeaveCriticalSection(&device->lock);
1494 return MMSYSERR_NOMEM;
1497 ash->cbStruct = sizeof(*ash);
1498 ash->fdwStatus = 0;
1499 ash->dwUser = (DWORD_PTR)header;
1500 ash->pbSrc = (BYTE*)header->lpData;
1501 ash->cbSrcLength = header->dwBufferLength;
1502 ash->dwSrcUser = header->dwUser;
1503 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1504 ash->cbDstLength = size;
1505 ash->dwDstUser = 0;
1507 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1508 if(mr != MMSYSERR_NOERROR){
1509 HeapFree(GetProcessHeap(), 0, ash);
1510 LeaveCriticalSection(&device->lock);
1511 return mr;
1514 header->reserved = (DWORD_PTR)ash;
1517 LeaveCriticalSection(&device->lock);
1519 header->dwFlags |= WHDR_PREPARED;
1520 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1522 return MMSYSERR_NOERROR;
1525 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1527 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1529 TRACE("(%p, %p)\n", hwave, header);
1531 if(!WINMM_ValidateAndLock(device))
1532 return MMSYSERR_INVALHANDLE;
1534 if(device->render && device->acm_handle){
1535 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1537 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1539 HeapFree(GetProcessHeap(), 0, ash);
1542 LeaveCriticalSection(&device->lock);
1544 header->dwFlags &= ~WHDR_PREPARED;
1546 return MMSYSERR_NOERROR;
1549 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1551 if(device->acm_handle){
1552 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1553 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1556 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1559 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1561 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1564 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1566 HRESULT hr;
1567 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1568 UINT64 clock_freq, clock_pos, clock_frames;
1569 UINT32 nloops, queue_frames = 0;
1571 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1572 if(FAILED(hr)){
1573 WARN("GetFrequency failed: %08x\n", hr);
1574 return NULL;
1577 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1578 if(FAILED(hr)){
1579 WARN("GetPosition failed: %08x\n", hr);
1580 return NULL;
1583 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1585 nloops = device->loop_counter;
1586 while(queue &&
1587 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1588 clock_frames - device->last_clock_pos + device->remainder_frames){
1589 if(!nloops){
1590 last = queue;
1591 device->last_clock_pos += queue_frames;
1592 device->remainder_frames = 0;
1593 queue_frames = 0;
1594 queue = device->first = queue->lpNext;
1595 }else{
1596 if(queue->dwFlags & WHDR_BEGINLOOP){
1597 if(queue->dwFlags & WHDR_ENDLOOP)
1598 --nloops;
1599 else
1600 queue = queue->lpNext;
1601 }else if(queue->dwFlags & WHDR_ENDLOOP){
1602 queue = device->loop_start;
1603 --nloops;
1608 if(last){
1609 last->lpNext = NULL;
1610 return first;
1611 }else
1612 return NULL;
1615 static void WOD_PushData(WINMM_Device *device)
1617 WINMM_CBInfo cb_info;
1618 HRESULT hr;
1619 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1620 UINT32 queue_bytes, nloops;
1621 BYTE *data;
1622 WAVEHDR *queue, *first = NULL;
1624 TRACE("(%p)\n", device->handle);
1626 EnterCriticalSection(&device->lock);
1628 if(!device->device)
1629 goto exit;
1631 if(!device->first){
1632 if (device->stopped)
1633 goto exit;
1634 device->stopped = TRUE;
1635 device->last_clock_pos = 0;
1636 IAudioClient_Stop(device->client);
1637 IAudioClient_Reset(device->client);
1638 goto exit;
1641 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1642 if(FAILED(hr)){
1643 WARN("GetBufferSize failed: %08x\n", hr);
1644 goto exit;
1647 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1648 if(FAILED(hr)){
1649 WARN("GetCurrentPadding failed: %08x\n", hr);
1650 goto exit;
1653 first = WOD_MarkDoneHeaders(device);
1655 /* determine which is larger between the available buffer size and
1656 * the amount of data left in the queue */
1657 avail_frames = bufsize - pad;
1659 queue = device->playing;
1660 ofs = device->ofs_bytes;
1661 queue_frames = 0;
1662 nloops = 0;
1663 while(queue && queue_frames < avail_frames){
1664 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1665 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1666 ofs = 0;
1668 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1669 queue = device->loop_start;
1670 ++nloops;
1671 }else
1672 queue = queue->lpNext;
1675 if(queue_frames < avail_frames)
1676 avail_frames = queue_frames;
1677 if(avail_frames == 0)
1678 goto exit;
1680 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1681 if(FAILED(hr)){
1682 WARN("GetBuffer failed: %08x\n", hr);
1683 goto exit;
1686 written = 0;
1687 while(device->playing && written < avail_frames){
1688 UINT32 copy_frames, copy_bytes;
1689 BYTE *queue_data;
1691 queue = device->playing;
1693 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1694 if(device->acm_handle)
1695 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1696 else
1697 queue_data = (BYTE*)queue->lpData;
1699 queue_frames = (queue_bytes - device->ofs_bytes) /
1700 device->bytes_per_frame;
1702 copy_frames = queue_frames < (avail_frames - written) ?
1703 queue_frames : avail_frames - written;
1704 copy_bytes = copy_frames * device->bytes_per_frame;
1706 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1708 data += copy_bytes;
1709 written += copy_frames;
1710 device->ofs_bytes += copy_bytes;
1712 if(device->ofs_bytes >= queue_bytes){
1713 device->ofs_bytes = 0;
1715 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1716 device->playing = queue->lpNext;
1717 else{
1718 if(queue->dwFlags & WHDR_BEGINLOOP){
1719 device->loop_start = device->playing;
1720 device->playing = queue->lpNext;
1721 device->loop_counter = queue->dwLoops;
1723 if(queue->dwFlags & WHDR_ENDLOOP){
1724 --device->loop_counter;
1725 if(device->loop_counter)
1726 device->playing = device->loop_start;
1727 else
1728 device->loop_start = device->playing = queue->lpNext;
1734 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1735 if(FAILED(hr)){
1736 WARN("ReleaseBuffer failed: %08x\n", hr);
1737 goto exit;
1740 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1741 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1742 else
1743 device->played_frames += avail_frames;
1745 exit:
1746 cb_info = device->cb_info;
1748 LeaveCriticalSection(&device->lock);
1750 while(first){
1751 WAVEHDR *next = first->lpNext;
1752 first->dwFlags &= ~WHDR_INQUEUE;
1753 first->dwFlags |= WHDR_DONE;
1754 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1755 first = next;
1759 static void WID_PullACMData(WINMM_Device *device)
1761 UINT32 packet, packet_bytes;
1762 DWORD flags;
1763 BYTE *data;
1764 WAVEHDR *queue;
1765 HRESULT hr;
1766 MMRESULT mr;
1768 if(device->acm_hdr.cbDstLength == 0){
1769 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1770 &flags, NULL, NULL);
1771 if(hr != S_OK){
1772 if(FAILED(hr))
1773 WARN("GetBuffer failed: %08x\n", hr);
1774 return;
1777 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1778 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1780 device->acm_offs = 0;
1782 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1783 device->acm_hdr.fdwStatus = 0;
1784 device->acm_hdr.dwUser = 0;
1785 device->acm_hdr.pbSrc = data;
1786 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1787 device->acm_hdr.cbSrcLengthUsed = 0;
1788 device->acm_hdr.dwSrcUser = 0;
1789 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1790 device->acm_hdr.cbDstLength = packet_bytes;
1791 device->acm_hdr.cbDstLengthUsed = 0;
1792 device->acm_hdr.dwDstUser = 0;
1794 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1795 if(mr != MMSYSERR_NOERROR){
1796 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1797 return;
1800 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1801 if(mr != MMSYSERR_NOERROR){
1802 WARN("acmStreamConvert failed: %d\n", mr);
1803 return;
1806 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1807 if(FAILED(hr))
1808 WARN("ReleaseBuffer failed: %08x\n", hr);
1810 device->played_frames += packet;
1813 queue = device->first;
1814 while(queue){
1815 UINT32 to_copy_bytes;
1817 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1818 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1820 memcpy(queue->lpData + queue->dwBytesRecorded,
1821 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1823 queue->dwBytesRecorded += to_copy_bytes;
1824 device->acm_offs += to_copy_bytes;
1826 if(queue->dwBufferLength - queue->dwBytesRecorded <
1827 device->bytes_per_frame){
1828 queue->dwFlags &= ~WHDR_INQUEUE;
1829 queue->dwFlags |= WHDR_DONE;
1830 device->first = queue = queue->lpNext;
1833 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1834 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1835 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1836 device->acm_hdr.cbDstLength = 0;
1837 device->acm_hdr.cbDstLengthUsed = 0;
1839 /* done with this ACM Header, so try to pull more data */
1840 WID_PullACMData(device);
1841 return;
1845 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1846 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1847 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1848 device->acm_hdr.cbDstLength = 0;
1849 device->acm_hdr.cbDstLengthUsed = 0;
1852 static void WID_PullData(WINMM_Device *device)
1854 WINMM_CBInfo cb_info;
1855 WAVEHDR *queue, *first = NULL, *last = NULL;
1856 HRESULT hr;
1858 TRACE("(%p)\n", device->handle);
1860 EnterCriticalSection(&device->lock);
1862 if(!device->device || !device->first)
1863 goto exit;
1865 first = device->first;
1867 if(device->acm_handle){
1868 WID_PullACMData(device);
1869 goto exit;
1872 while(device->first){
1873 BYTE *data;
1874 UINT32 packet_len, packet;
1875 DWORD flags;
1877 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1878 &flags, NULL, NULL);
1879 if(hr != S_OK){
1880 if(FAILED(hr))
1881 WARN("GetBuffer failed: %08x\n", hr);
1882 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1883 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1884 goto exit;
1887 packet = packet_len;
1888 queue = device->first;
1889 while(queue && packet > 0){
1890 UINT32 to_copy_bytes;
1892 to_copy_bytes = min(packet * device->bytes_per_frame,
1893 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1895 memcpy(queue->lpData + queue->dwBytesRecorded,
1896 data + (packet_len - packet) * device->bytes_per_frame,
1897 to_copy_bytes);
1899 queue->dwBytesRecorded += to_copy_bytes;
1901 if(queue->dwBufferLength - queue->dwBytesRecorded <
1902 device->bytes_per_frame){
1903 last = queue;
1904 device->first = queue = queue->lpNext;
1907 packet -= to_copy_bytes / device->bytes_per_frame;
1910 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1911 if(FAILED(hr))
1912 WARN("ReleaseBuffer failed: %08x\n", hr);
1914 if(packet > 0)
1915 WARN("losing %u frames\n", packet);
1916 device->played_frames += packet_len - packet;
1919 exit:
1920 cb_info = device->cb_info;
1922 LeaveCriticalSection(&device->lock);
1924 if(last){
1925 last->lpNext = NULL;
1926 while(first){
1927 WAVEHDR *next = first->lpNext;
1928 first->dwFlags &= ~WHDR_INQUEUE;
1929 first->dwFlags |= WHDR_DONE;
1930 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1931 first = next;
1936 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1938 HRESULT hr;
1940 TRACE("(%p)\n", device->handle);
1942 if(device->render)
1943 /* prebuffer data before starting */
1944 WOD_PushData(device);
1946 if(device->stopped){
1947 device->stopped = FALSE;
1949 hr = IAudioClient_Start(device->client);
1950 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1951 device->stopped = TRUE;
1952 WARN("Start failed: %08x\n", hr);
1953 return MMSYSERR_ERROR;
1957 return MMSYSERR_NOERROR;
1960 static LRESULT WINMM_Pause(HWAVE hwave)
1962 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1963 HRESULT hr;
1965 TRACE("(%p)\n", hwave);
1967 if(!WINMM_ValidateAndLock(device))
1968 return MMSYSERR_INVALHANDLE;
1970 hr = IAudioClient_Stop(device->client);
1971 if(FAILED(hr)){
1972 LeaveCriticalSection(&device->lock);
1973 WARN("Stop failed: %08x\n", hr);
1974 return MMSYSERR_ERROR;
1977 device->stopped = FALSE;
1979 LeaveCriticalSection(&device->lock);
1981 return MMSYSERR_NOERROR;
1984 static LRESULT WINMM_Reset(HWAVE hwave)
1986 WINMM_CBInfo cb_info;
1987 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1988 BOOL is_out;
1989 WAVEHDR *first;
1990 HRESULT hr;
1992 TRACE("(%p)\n", hwave);
1994 if(!WINMM_ValidateAndLock(device))
1995 return MMSYSERR_INVALHANDLE;
1997 hr = IAudioClient_Stop(device->client);
1998 if(FAILED(hr)){
1999 LeaveCriticalSection(&device->lock);
2000 WARN("Stop failed: %08x\n", hr);
2001 return MMSYSERR_ERROR;
2003 device->stopped = TRUE;
2005 first = device->first;
2006 device->first = device->last = device->playing = NULL;
2007 device->ofs_bytes = 0;
2008 device->played_frames = 0;
2009 device->loop_counter = 0;
2010 device->last_clock_pos = 0;
2011 IAudioClient_Reset(device->client);
2013 cb_info = device->cb_info;
2014 is_out = device->render != NULL;
2016 LeaveCriticalSection(&device->lock);
2018 while(first){
2019 WAVEHDR *next = first->lpNext;
2020 first->dwFlags &= ~WHDR_INQUEUE;
2021 first->dwFlags |= WHDR_DONE;
2022 if(is_out)
2023 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2024 else
2025 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2026 first = next;
2029 return MMSYSERR_NOERROR;
2032 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2033 UINT32 sample_rate, UINT32 bytes_per_sec)
2035 switch(time->wType){
2036 case TIME_SAMPLES:
2037 time->u.sample = played_frames;
2038 return MMSYSERR_NOERROR;
2039 case TIME_MS:
2040 time->u.ms = (UINT64)played_frames * 1000 / sample_rate;
2041 return MMSYSERR_NOERROR;
2042 case TIME_SMPTE:
2043 time->u.smpte.fps = 30;
2044 played_frames += sample_rate / time->u.smpte.fps - 1; /* round up */
2045 time->u.smpte.frame = (played_frames % sample_rate) * time->u.smpte.fps / sample_rate;
2046 played_frames /= sample_rate; /* yields seconds */
2047 time->u.smpte.sec = played_frames % 60;
2048 played_frames /= 60;
2049 time->u.smpte.min = played_frames % 60;
2050 time->u.smpte.hour= played_frames / 60;
2051 return MMSYSERR_NOERROR;
2052 default:
2053 time->wType = TIME_BYTES;
2054 /* fall through */
2055 case TIME_BYTES:
2056 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2057 return MMSYSERR_NOERROR;
2061 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2063 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2064 UINT32 played_frames, sample_rate, bytes_per_sec;
2066 TRACE("(%p, %p)\n", hwave, time);
2068 if(!WINMM_ValidateAndLock(device))
2069 return MMSYSERR_INVALHANDLE;
2071 played_frames = device->played_frames;
2072 sample_rate = device->orig_fmt->nSamplesPerSec;
2073 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2075 LeaveCriticalSection(&device->lock);
2077 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2080 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2081 UINT *mmdev_index)
2083 UINT mmdev, dev, junk, *out;
2084 BOOL is_out;
2086 if(!mmdev_index)
2087 out = &mmdev;
2088 else
2089 out = mmdev_index;
2091 switch(flags & 0xF0000000){
2092 case MIXER_OBJECTF_MIXER: /* == 0 */
2093 *out = HandleToULong(hmix);
2094 if(*out < g_outmmdevices_count)
2095 return read_map(g_out_map, *out);
2096 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2097 *out -= g_outmmdevices_count;
2098 return read_map(g_in_map, *out);
2100 /* fall through -- if it's not a valid mixer device, then
2101 * it could be a valid mixer handle. windows seems to do
2102 * this as well. */
2103 case MIXER_OBJECTF_HMIXER:
2104 case MIXER_OBJECTF_HWAVEOUT:
2105 case MIXER_OBJECTF_HWAVEIN:
2106 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2107 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2108 (!is_out && *out >= g_inmmdevices_count))
2109 return NULL;
2110 if(is_out)
2111 return read_map(g_out_map, *out);
2112 return read_map(g_in_map, *out);
2113 case MIXER_OBJECTF_WAVEOUT:
2114 *out = HandleToULong(hmix);
2115 if(*out < g_outmmdevices_count)
2116 return read_map(g_out_map, *out);
2117 return NULL;
2118 case MIXER_OBJECTF_WAVEIN:
2119 *out = HandleToULong(hmix);
2120 if(*out < g_inmmdevices_count)
2121 return read_map(g_in_map, *out);
2122 return NULL;
2125 return NULL;
2128 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2130 IAudioSessionManager *sesman;
2131 IMMDevice *device;
2132 HRESULT hr;
2134 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2135 if(FAILED(hr)){
2136 WARN("Device %s (%s) unavailable: %08x\n",
2137 wine_dbgstr_w(mmdevice->dev_id),
2138 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2139 return MMSYSERR_ERROR;
2142 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2143 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2144 if(FAILED(hr)){
2145 WARN("Activate failed: %08x\n", hr);
2146 IMMDevice_Release(device);
2147 return MMSYSERR_ERROR;
2150 IMMDevice_Release(device);
2152 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2153 FALSE, &mmdevice->volume);
2154 IAudioSessionManager_Release(sesman);
2155 if(FAILED(hr)){
2156 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2157 return MMSYSERR_ERROR;
2160 return MMSYSERR_NOERROR;
2163 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2165 WINMM_MMDevice *mmdevice;
2166 MIXERCONTROLDETAILS *control = details->details;
2167 HRESULT hr;
2169 TRACE("(%p)\n", details->hmix);
2171 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2172 if(!mmdevice)
2173 return MMSYSERR_INVALHANDLE;
2175 EnterCriticalSection(&mmdevice->lock);
2177 if(!mmdevice->volume){
2178 MMRESULT mr;
2180 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2181 if(mr != MMSYSERR_NOERROR){
2182 LeaveCriticalSection(&mmdevice->lock);
2183 return mr;
2187 if(control->dwControlID == 0){
2188 float vol;
2189 MIXERCONTROLDETAILS_UNSIGNED *udet;
2191 if(!control->paDetails ||
2192 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2193 LeaveCriticalSection(&mmdevice->lock);
2194 return MMSYSERR_INVALPARAM;
2197 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2198 if(FAILED(hr)){
2199 WARN("GetMasterVolume failed: %08x\n", hr);
2200 LeaveCriticalSection(&mmdevice->lock);
2201 return MMSYSERR_ERROR;
2204 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2205 udet->dwValue = vol * ((unsigned int)0xFFFF);
2206 }else if(control->dwControlID == 1){
2207 BOOL mute;
2208 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2210 if(!control->paDetails ||
2211 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2212 LeaveCriticalSection(&mmdevice->lock);
2213 return MMSYSERR_INVALPARAM;
2216 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2217 if(FAILED(hr)){
2218 WARN("GetMute failed: %08x\n", hr);
2219 LeaveCriticalSection(&mmdevice->lock);
2220 return MMSYSERR_ERROR;
2223 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2224 bdet->fValue = mute;
2225 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2226 FIXME("What should the sw-side mixer controls map to?\n");
2227 }else{
2228 LeaveCriticalSection(&mmdevice->lock);
2229 return MIXERR_INVALCONTROL;
2232 LeaveCriticalSection(&mmdevice->lock);
2234 return MMSYSERR_NOERROR;
2237 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2239 WINMM_MMDevice *mmdevice;
2240 MIXERCONTROLDETAILS *control = details->details;
2241 HRESULT hr;
2243 TRACE("(%p)\n", details->hmix);
2245 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2246 if(!mmdevice)
2247 return MMSYSERR_INVALHANDLE;
2249 EnterCriticalSection(&mmdevice->lock);
2251 if(!mmdevice->volume){
2252 MMRESULT mr;
2254 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2255 if(mr != MMSYSERR_NOERROR){
2256 LeaveCriticalSection(&mmdevice->lock);
2257 return mr;
2261 if(control->dwControlID == 0){
2262 float vol;
2263 MIXERCONTROLDETAILS_UNSIGNED *udet;
2265 if(!control->paDetails ||
2266 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2267 LeaveCriticalSection(&mmdevice->lock);
2268 return MMSYSERR_INVALPARAM;
2271 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2273 if(udet->dwValue > 65535){
2274 LeaveCriticalSection(&mmdevice->lock);
2275 return MMSYSERR_INVALPARAM;
2278 vol = udet->dwValue / 65535.f;
2280 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2281 if(FAILED(hr)){
2282 WARN("SetMasterVolume failed: %08x\n", hr);
2283 LeaveCriticalSection(&mmdevice->lock);
2284 return MMSYSERR_ERROR;
2286 }else if(control->dwControlID == 1){
2287 BOOL mute;
2288 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2290 if(!control->paDetails ||
2291 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2292 LeaveCriticalSection(&mmdevice->lock);
2293 return MMSYSERR_INVALPARAM;
2296 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2297 mute = bdet->fValue;
2299 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2300 if(FAILED(hr)){
2301 WARN("SetMute failed: %08x\n", hr);
2302 LeaveCriticalSection(&mmdevice->lock);
2303 return MMSYSERR_ERROR;
2305 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2306 FIXME("What should the sw-side mixer controls map to?\n");
2307 }else{
2308 LeaveCriticalSection(&mmdevice->lock);
2309 return MIXERR_INVALCONTROL;
2312 LeaveCriticalSection(&mmdevice->lock);
2314 return MMSYSERR_NOERROR;
2317 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2319 WINMM_MMDevice *mmdevice;
2320 IMMDevice *device;
2321 IPropertyStore *ps;
2322 PROPVARIANT pv;
2323 DWORD len_bytes;
2324 HRESULT hr;
2326 static const PROPERTYKEY deviceinterface_key = {
2327 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2330 if(WINMM_IsMapper(info->index)){
2331 if(info->str){
2332 if(*info->len_bytes < sizeof(WCHAR))
2333 return MMSYSERR_INVALPARAM;
2334 *info->str = 0;
2335 }else
2336 *info->len_bytes = sizeof(WCHAR);
2337 return MMSYSERR_NOERROR;
2340 if(info->is_out){
2341 if(info->index >= g_outmmdevices_count)
2342 return MMSYSERR_INVALHANDLE;
2344 mmdevice = &g_out_mmdevices[info->index];
2345 }else{
2346 if(info->index >= g_inmmdevices_count)
2347 return MMSYSERR_INVALHANDLE;
2349 mmdevice = &g_in_mmdevices[info->index];
2352 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2353 &device);
2354 if(FAILED(hr)){
2355 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2356 return MMSYSERR_ERROR;
2359 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2360 if(FAILED(hr)){
2361 WARN("OpenPropertyStore failed: %08x\n", hr);
2362 IMMDevice_Release(device);
2363 return MMSYSERR_ERROR;
2366 PropVariantInit(&pv);
2367 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2368 if(FAILED(hr)){
2369 WARN("GetValue failed: %08x\n", hr);
2370 IPropertyStore_Release(ps);
2371 IMMDevice_Release(device);
2372 return MMSYSERR_ERROR;
2374 if(pv.vt != VT_LPWSTR){
2375 WARN("Got unexpected property type: %u\n", pv.vt);
2376 PropVariantClear(&pv);
2377 IPropertyStore_Release(ps);
2378 IMMDevice_Release(device);
2379 return MMSYSERR_ERROR;
2382 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2384 if(info->str){
2385 if(len_bytes > *info->len_bytes){
2386 PropVariantClear(&pv);
2387 IPropertyStore_Release(ps);
2388 IMMDevice_Release(device);
2389 return MMSYSERR_INVALPARAM;
2392 memcpy(info->str, pv.u.pwszVal, len_bytes);
2393 }else
2394 *info->len_bytes = len_bytes;
2396 PropVariantClear(&pv);
2397 IPropertyStore_Release(ps);
2398 IMMDevice_Release(device);
2400 return MMSYSERR_NOERROR;
2403 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2404 LPARAM lparam)
2406 switch(msg){
2407 case WODM_OPEN:
2408 return WOD_Open((WINMM_OpenInfo*)wparam);
2409 case WODM_CLOSE:
2410 return WOD_Close((HWAVEOUT)wparam);
2411 case WIDM_OPEN:
2412 return WID_Open((WINMM_OpenInfo*)wparam);
2413 case WIDM_CLOSE:
2414 return WID_Close((HWAVEIN)wparam);
2415 case MXDM_GETCONTROLDETAILS:
2416 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2417 case MXDM_SETCONTROLDETAILS:
2418 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2419 case DRV_QUERYDEVICEINTERFACESIZE:
2420 case DRV_QUERYDEVICEINTERFACE:
2421 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2423 return DefWindowProcW(hwnd, msg, wparam, lparam);
2426 static BOOL WINMM_DevicesThreadDone(void)
2428 UINT i;
2430 EnterCriticalSection(&g_devthread_lock);
2432 if(g_devthread_token > 0){
2433 LeaveCriticalSection(&g_devthread_lock);
2434 return FALSE;
2437 for(i = 0; i < g_devhandle_count; ++i){
2438 if(g_handle_devices[i]->open){
2439 LeaveCriticalSection(&g_devthread_lock);
2440 return FALSE;
2444 DestroyWindow(g_devices_hwnd);
2445 g_devices_hwnd = NULL;
2446 IMMDeviceEnumerator_Release(g_devenum);
2447 g_devenum = NULL;
2448 CoUninitialize();
2450 LeaveCriticalSection(&g_devthread_lock);
2452 return TRUE;
2455 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2457 HANDLE evt = arg;
2458 HRESULT hr;
2459 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2461 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2462 if(FAILED(hr)){
2463 WARN("CoInitializeEx failed: %08x\n", hr);
2464 FreeLibraryAndExitThread(g_devthread_module, 1);
2467 hr = WINMM_InitMMDevices();
2468 if(FAILED(hr)){
2469 CoUninitialize();
2470 FreeLibraryAndExitThread(g_devthread_module, 1);
2473 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2474 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2475 if(FAILED(hr)){
2476 WARN("CoCreateInstance failed: %08x\n", hr);
2477 CoUninitialize();
2478 FreeLibraryAndExitThread(g_devthread_module, 1);
2481 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2482 HWND_MESSAGE, NULL, NULL, NULL);
2483 if(!g_devices_hwnd){
2484 WARN("CreateWindow failed: %d\n", GetLastError());
2485 CoUninitialize();
2486 FreeLibraryAndExitThread(g_devthread_module, 1);
2489 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2490 (LONG_PTR)WINMM_DevicesMsgProc);
2492 /* inform caller that the thread is ready to process messages */
2493 SetEvent(evt);
2494 evt = NULL; /* do not use after this point */
2496 while(1){
2497 DWORD wait;
2498 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2499 FALSE, INFINITE, QS_ALLINPUT);
2500 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2501 MSG msg;
2502 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2503 WARN("Unexpected message: 0x%x\n", msg.message);
2504 if(!g_devices_hwnd)
2505 break;
2506 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2507 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2508 if(device->render)
2509 WOD_PushData(device);
2510 else
2511 WID_PullData(device);
2512 }else
2513 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2514 GetLastError());
2515 if(WINMM_DevicesThreadDone()){
2516 TRACE("Quitting devices thread\n");
2517 FreeLibraryAndExitThread(g_devthread_module, 0);
2521 FreeLibraryAndExitThread(g_devthread_module, 0);
2524 /* on success, increments g_devthread_token to prevent
2525 * device thread shutdown. caller must decrement. */
2526 static BOOL WINMM_StartDevicesThread(void)
2528 HANDLE events[2];
2529 DWORD wait;
2531 EnterCriticalSection(&g_devthread_lock);
2533 if(g_devices_hwnd){
2534 wait = WaitForSingleObject(g_devices_thread, 0);
2535 if(wait == WAIT_TIMEOUT){
2536 /* thread still running */
2537 InterlockedIncrement(&g_devthread_token);
2538 LeaveCriticalSection(&g_devthread_lock);
2539 return TRUE;
2541 if(wait != WAIT_OBJECT_0){
2542 /* error */
2543 LeaveCriticalSection(&g_devthread_lock);
2544 return FALSE;
2546 TRACE("Devices thread left dangling message window?\n");
2547 g_devices_hwnd = NULL;
2548 CloseHandle(g_devices_thread);
2549 g_devices_thread = NULL;
2550 }else if(g_devices_thread){
2551 WaitForSingleObject(g_devices_thread, INFINITE);
2552 CloseHandle(g_devices_thread);
2553 g_devices_thread = NULL;
2556 TRACE("Starting up devices thread\n");
2558 /* The devices thread holds a reference to the winmm module
2559 * to prevent it from unloading while it's running. */
2560 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2561 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2563 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2565 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2566 events[0], 0, NULL);
2567 if(!g_devices_thread){
2568 LeaveCriticalSection(&g_devthread_lock);
2569 CloseHandle(events[0]);
2570 FreeLibrary(g_devthread_module);
2571 return FALSE;
2574 events[1] = g_devices_thread;
2575 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2576 CloseHandle(events[0]);
2577 if(wait != WAIT_OBJECT_0){
2578 if(wait == 1 + WAIT_OBJECT_0){
2579 CloseHandle(g_devices_thread);
2580 g_devices_thread = NULL;
2581 g_devices_hwnd = NULL;
2583 LeaveCriticalSection(&g_devthread_lock);
2584 return FALSE;
2587 InterlockedIncrement(&g_devthread_token);
2589 LeaveCriticalSection(&g_devthread_lock);
2591 return TRUE;
2594 /**************************************************************************
2595 * waveOutGetNumDevs [WINMM.@]
2597 UINT WINAPI waveOutGetNumDevs(void)
2599 HRESULT hr = WINMM_InitMMDevices();
2600 if(FAILED(hr))
2601 return 0;
2603 TRACE("count: %u\n", g_outmmdevices_count);
2605 return g_outmmdevices_count;
2608 /**************************************************************************
2609 * waveOutGetDevCapsA [WINMM.@]
2611 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2612 UINT uSize)
2614 WAVEOUTCAPSW wocW;
2615 UINT ret;
2617 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2619 if(!lpCaps)
2620 return MMSYSERR_INVALPARAM;
2622 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2624 if (ret == MMSYSERR_NOERROR) {
2625 WAVEOUTCAPSA wocA;
2626 wocA.wMid = wocW.wMid;
2627 wocA.wPid = wocW.wPid;
2628 wocA.vDriverVersion = wocW.vDriverVersion;
2629 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2630 sizeof(wocA.szPname), NULL, NULL );
2631 wocA.dwFormats = wocW.dwFormats;
2632 wocA.wChannels = wocW.wChannels;
2633 wocA.dwSupport = wocW.dwSupport;
2634 wocA.wReserved1 = wocW.wReserved1;
2635 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2637 return ret;
2640 /**************************************************************************
2641 * waveOutGetDevCapsW [WINMM.@]
2643 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2644 UINT uSize)
2646 WAVEOUTCAPSW mapper_caps, *caps;
2647 HRESULT hr;
2649 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2651 hr = WINMM_InitMMDevices();
2652 if(FAILED(hr))
2653 return MMSYSERR_NODRIVER;
2655 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2657 if(WINMM_IsMapper(uDeviceID)){
2658 mapper_caps.wMid = 0xFF;
2659 mapper_caps.wPid = 0xFF;
2660 mapper_caps.vDriverVersion = 0x00010001;
2661 mapper_caps.dwFormats = 0xFFFFFFFF;
2662 mapper_caps.wReserved1 = 0;
2663 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2664 WAVECAPS_SAMPLEACCURATE;
2665 mapper_caps.wChannels = 2;
2666 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
2668 caps = &mapper_caps;
2669 }else{
2670 if(uDeviceID >= g_outmmdevices_count)
2671 return MMSYSERR_BADDEVICEID;
2673 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2676 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2678 return MMSYSERR_NOERROR;
2681 /**************************************************************************
2682 * waveOutGetErrorTextA [WINMM.@]
2683 * waveInGetErrorTextA [WINMM.@]
2685 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2687 UINT ret;
2689 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2690 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2691 else
2693 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2694 if (!xstr) ret = MMSYSERR_NOMEM;
2695 else
2697 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2698 if (ret == MMSYSERR_NOERROR)
2699 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2700 HeapFree(GetProcessHeap(), 0, xstr);
2703 return ret;
2706 /**************************************************************************
2707 * waveOutGetErrorTextW [WINMM.@]
2708 * waveInGetErrorTextW [WINMM.@]
2710 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2712 UINT ret = MMSYSERR_BADERRNUM;
2714 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2715 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2716 else if (
2717 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2718 * a warning for the test was always true */
2719 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2720 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2721 if (LoadStringW(hWinMM32Instance,
2722 uError, lpText, uSize) > 0) {
2723 ret = MMSYSERR_NOERROR;
2726 return ret;
2729 /**************************************************************************
2730 * waveOutOpen [WINMM.@]
2731 * All the args/structs have the same layout as the win16 equivalents
2733 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2734 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2735 DWORD_PTR dwInstance, DWORD dwFlags)
2737 LRESULT res;
2738 WINMM_OpenInfo info;
2739 WINMM_CBInfo cb_info;
2741 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2742 dwCallback, dwInstance, dwFlags);
2744 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2745 return MMSYSERR_INVALPARAM;
2747 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2748 if(res != MMSYSERR_NOERROR)
2749 return res;
2751 if(!WINMM_StartDevicesThread())
2752 return MMSYSERR_NODRIVER;
2754 info.handle = 0;
2755 info.format = (WAVEFORMATEX*)lpFormat;
2756 info.callback = dwCallback;
2757 info.cb_user = dwInstance;
2758 info.req_device = uDeviceID;
2759 info.flags = dwFlags;
2760 info.reset = TRUE;
2762 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2763 InterlockedDecrement(&g_devthread_token);
2764 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2765 return res;
2767 if(lphWaveOut)
2768 *lphWaveOut = (HWAVEOUT)info.handle;
2770 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2771 cb_info.callback = dwCallback;
2772 cb_info.user = dwInstance;
2773 cb_info.hwave = info.handle;
2775 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2777 return res;
2780 /**************************************************************************
2781 * waveOutClose [WINMM.@]
2783 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2785 UINT res;
2786 WINMM_Device *device;
2787 WINMM_CBInfo cb_info;
2789 TRACE("(%p)\n", hWaveOut);
2791 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2793 if(!WINMM_ValidateAndLock(device))
2794 return MMSYSERR_INVALHANDLE;
2796 cb_info = device->cb_info;
2798 LeaveCriticalSection(&device->lock);
2800 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2802 if(res == MMSYSERR_NOERROR)
2803 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2805 return res;
2808 /**************************************************************************
2809 * waveOutPrepareHeader [WINMM.@]
2811 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2812 WAVEHDR* lpWaveOutHdr, UINT uSize)
2814 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2816 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2817 return MMSYSERR_INVALPARAM;
2819 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2820 return MMSYSERR_NOERROR;
2822 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2825 /**************************************************************************
2826 * waveOutUnprepareHeader [WINMM.@]
2828 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2829 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2831 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2833 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2834 return MMSYSERR_INVALPARAM;
2836 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2837 return WAVERR_STILLPLAYING;
2839 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2840 return MMSYSERR_NOERROR;
2842 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2845 /**************************************************************************
2846 * waveOutWrite [WINMM.@]
2848 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2850 WINMM_Device *device;
2851 MMRESULT mr;
2853 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2855 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2857 if(!WINMM_ValidateAndLock(device))
2858 return MMSYSERR_INVALHANDLE;
2860 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2861 LeaveCriticalSection(&device->lock);
2862 return WAVERR_UNPREPARED;
2865 if(header->dwFlags & WHDR_INQUEUE){
2866 LeaveCriticalSection(&device->lock);
2867 return WAVERR_STILLPLAYING;
2870 TRACE("dwBufferLength: %u\n", header->dwBufferLength);
2872 if(device->acm_handle){
2873 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2875 ash->cbSrcLength = header->dwBufferLength;
2876 mr = acmStreamConvert(device->acm_handle, ash, 0);
2877 if(mr != MMSYSERR_NOERROR){
2878 LeaveCriticalSection(&device->lock);
2879 return mr;
2883 if(device->first){
2884 device->last->lpNext = header;
2885 device->last = header;
2886 if(!device->playing)
2887 device->playing = header;
2888 }else{
2889 device->playing = device->first = device->last = header;
2890 if(header->dwFlags & WHDR_BEGINLOOP){
2891 device->loop_counter = header->dwLoops;
2892 device->loop_start = header;
2896 header->lpNext = NULL;
2897 header->dwFlags &= ~WHDR_DONE;
2898 header->dwFlags |= WHDR_INQUEUE;
2900 mr = WINMM_BeginPlaying(device);
2902 LeaveCriticalSection(&device->lock);
2904 return mr;
2907 /**************************************************************************
2908 * waveOutBreakLoop [WINMM.@]
2910 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2912 WINMM_Device *device;
2914 TRACE("(%p)\n", hWaveOut);
2916 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2918 if(!WINMM_ValidateAndLock(device))
2919 return MMSYSERR_INVALHANDLE;
2921 device->loop_counter = 0;
2923 LeaveCriticalSection(&device->lock);
2925 return MMSYSERR_NOERROR;
2928 /**************************************************************************
2929 * waveOutPause [WINMM.@]
2931 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2933 TRACE("(%p)\n", hWaveOut);
2935 return WINMM_Pause((HWAVE)hWaveOut);
2938 /**************************************************************************
2939 * waveOutReset [WINMM.@]
2941 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2943 TRACE("(%p)\n", hWaveOut);
2945 return WINMM_Reset((HWAVE)hWaveOut);
2948 /**************************************************************************
2949 * waveOutRestart [WINMM.@]
2951 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2953 WINMM_Device *device;
2954 MMRESULT mr;
2956 TRACE("(%p)\n", hWaveOut);
2958 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2960 if(!WINMM_ValidateAndLock(device))
2961 return MMSYSERR_INVALHANDLE;
2963 device->stopped = TRUE;
2965 mr = WINMM_BeginPlaying(device);
2967 LeaveCriticalSection(&device->lock);
2969 return mr;
2972 /**************************************************************************
2973 * waveOutGetPosition [WINMM.@]
2975 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2976 UINT uSize)
2978 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2980 if(!uSize || !lpTime)
2981 return MMSYSERR_INVALPARAM;
2983 if(uSize < sizeof(MMTIME))
2984 return MMSYSERR_ERROR;
2986 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2989 /**************************************************************************
2990 * waveOutGetPitch [WINMM.@]
2992 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2994 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2995 return MMSYSERR_NOTSUPPORTED;
2998 /**************************************************************************
2999 * waveOutSetPitch [WINMM.@]
3001 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3003 TRACE("(%p, %08x)\n", hWaveOut, dw);
3005 return MMSYSERR_NOTSUPPORTED;
3008 /**************************************************************************
3009 * waveOutGetPlaybackRate [WINMM.@]
3011 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3013 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3015 return MMSYSERR_NOTSUPPORTED;
3018 /**************************************************************************
3019 * waveOutSetPlaybackRate [WINMM.@]
3021 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3023 TRACE("(%p, %08x)\n", hWaveOut, dw);
3025 return MMSYSERR_NOTSUPPORTED;
3028 /**************************************************************************
3029 * waveOutGetVolume [WINMM.@]
3031 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3033 WINMM_Device *device;
3034 UINT32 channels;
3035 float *vols;
3036 HRESULT hr;
3038 TRACE("(%p, %p)\n", hWaveOut, out);
3040 if(!out)
3041 return MMSYSERR_INVALPARAM;
3043 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3045 if(!WINMM_ValidateAndLock(device))
3046 return MMSYSERR_INVALHANDLE;
3048 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3049 if(FAILED(hr)){
3050 LeaveCriticalSection(&device->lock);
3051 WARN("GetChannelCount failed: %08x\n", hr);
3052 return MMSYSERR_ERROR;
3055 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3056 if(!vols){
3057 LeaveCriticalSection(&device->lock);
3058 return MMSYSERR_NOMEM;
3061 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3062 if(FAILED(hr)){
3063 LeaveCriticalSection(&device->lock);
3064 HeapFree(GetProcessHeap(), 0, vols);
3065 WARN("GetAllVolumes failed: %08x\n", hr);
3066 return MMSYSERR_ERROR;
3069 LeaveCriticalSection(&device->lock);
3071 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3072 if(channels > 1)
3073 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3075 HeapFree(GetProcessHeap(), 0, vols);
3077 return MMSYSERR_NOERROR;
3080 /**************************************************************************
3081 * waveOutSetVolume [WINMM.@]
3083 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3085 WINMM_Device *device;
3086 UINT32 channels;
3087 float *vols;
3088 HRESULT hr;
3090 TRACE("(%p, %08x)\n", hWaveOut, in);
3092 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3094 if(!WINMM_ValidateAndLock(device))
3095 return MMSYSERR_INVALHANDLE;
3097 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3098 if(FAILED(hr)){
3099 LeaveCriticalSection(&device->lock);
3100 WARN("GetChannelCount failed: %08x\n", hr);
3101 return MMSYSERR_ERROR;
3104 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3105 if(!vols){
3106 LeaveCriticalSection(&device->lock);
3107 return MMSYSERR_NOMEM;
3110 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3111 if(FAILED(hr)){
3112 LeaveCriticalSection(&device->lock);
3113 HeapFree(GetProcessHeap(), 0, vols);
3114 WARN("GetAllVolumes failed: %08x\n", hr);
3115 return MMSYSERR_ERROR;
3118 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
3119 if(channels > 1)
3120 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
3122 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3123 if(FAILED(hr)){
3124 LeaveCriticalSection(&device->lock);
3125 HeapFree(GetProcessHeap(), 0, vols);
3126 WARN("SetAllVolumes failed: %08x\n", hr);
3127 return MMSYSERR_ERROR;
3130 LeaveCriticalSection(&device->lock);
3132 HeapFree(GetProcessHeap(), 0, vols);
3134 return MMSYSERR_NOERROR;
3137 /**************************************************************************
3138 * waveOutGetID [WINMM.@]
3140 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3142 WINMM_Device *device;
3143 UINT dev, junk;
3144 BOOL is_out;
3146 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3148 if(!lpuDeviceID)
3149 return MMSYSERR_INVALPARAM;
3151 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3152 if(!WINMM_ValidateAndLock(device))
3153 return MMSYSERR_INVALHANDLE;
3155 LeaveCriticalSection(&device->lock);
3157 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3159 return MMSYSERR_NOERROR;
3162 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3164 UINT count;
3165 WINMM_MMDevice **devices;
3167 TRACE("(%u, %p, %d)\n", device, len, is_out);
3169 if(is_out){
3170 count = g_outmmdevices_count;
3171 devices = g_out_map;
3172 }else{
3173 count = g_inmmdevices_count;
3174 devices = g_in_map;
3177 if(device >= count)
3178 return MMSYSERR_INVALHANDLE;
3180 EnterCriticalSection(&g_devthread_lock);
3181 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3182 LeaveCriticalSection(&g_devthread_lock);
3184 return MMSYSERR_NOERROR;
3187 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3188 BOOL is_out)
3190 UINT count, id_len;
3191 WINMM_MMDevice **devices;
3193 TRACE("(%u, %p, %d)\n", device, str, is_out);
3195 if(is_out){
3196 count = g_outmmdevices_count;
3197 devices = g_out_map;
3198 }else{
3199 count = g_inmmdevices_count;
3200 devices = g_in_map;
3203 if(device >= count)
3204 return MMSYSERR_INVALHANDLE;
3206 EnterCriticalSection(&g_devthread_lock);
3207 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3208 if(len < id_len){
3209 LeaveCriticalSection(&g_devthread_lock);
3210 return MMSYSERR_ERROR;
3213 memcpy(str, devices[device]->dev_id, id_len);
3214 LeaveCriticalSection(&g_devthread_lock);
3216 return MMSYSERR_NOERROR;
3219 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3221 WINMM_QueryInterfaceInfo info;
3222 UINT ret;
3224 if(!WINMM_StartDevicesThread())
3225 return MMSYSERR_NODRIVER;
3227 info.is_out = is_out;
3228 info.index = index;
3229 info.str = out;
3230 info.len_bytes = out_len;
3232 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3233 InterlockedDecrement(&g_devthread_token);
3234 return ret;
3237 /**************************************************************************
3238 * waveOutMessage [WINMM.@]
3240 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3241 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3243 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3245 switch(uMessage){
3246 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3247 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3248 (DWORD_PTR*)dwParam1, TRUE);
3249 case DRV_QUERYFUNCTIONINSTANCEID:
3250 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3251 case DRV_QUERYDEVICEINTERFACESIZE:
3252 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3253 NULL, (ULONG*)dwParam1);
3254 case DRV_QUERYDEVICEINTERFACE:
3256 ULONG size = dwParam2;
3257 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3258 (WCHAR*)dwParam1, &size);
3260 case DRV_QUERYMAPPABLE:
3261 return MMSYSERR_NOERROR;
3262 case DRVM_MAPPER_PREFERRED_GET:
3263 if(dwParam1) {
3264 if(g_outmmdevices_count > 0)
3265 /* Device 0 is always the default device */
3266 *(DWORD *)dwParam1 = 0;
3267 else
3268 *(DWORD *)dwParam1 = -1;
3271 if(dwParam2)
3272 /* Status flags */
3273 *(DWORD *)dwParam2 = 0;
3275 return MMSYSERR_NOERROR;
3278 TRACE("Message not supported: %u\n", uMessage);
3280 return MMSYSERR_NOTSUPPORTED;
3283 /**************************************************************************
3284 * waveInGetNumDevs [WINMM.@]
3286 UINT WINAPI waveInGetNumDevs(void)
3288 HRESULT hr = WINMM_InitMMDevices();
3289 if(FAILED(hr))
3290 return 0;
3292 TRACE("count: %u\n", g_inmmdevices_count);
3294 return g_inmmdevices_count;
3297 /**************************************************************************
3298 * waveInGetDevCapsW [WINMM.@]
3300 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3302 WAVEINCAPSW mapper_caps, *caps;
3303 HRESULT hr;
3305 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3307 hr = WINMM_InitMMDevices();
3308 if(FAILED(hr))
3309 return MMSYSERR_NODRIVER;
3311 if(!lpCaps)
3312 return MMSYSERR_INVALPARAM;
3314 if(WINMM_IsMapper(uDeviceID)){
3315 mapper_caps.wMid = 0xFF;
3316 mapper_caps.wPid = 0xFF;
3317 mapper_caps.vDriverVersion = 0x00010001;
3318 mapper_caps.dwFormats = 0xFFFFFFFF;
3319 mapper_caps.wReserved1 = 0;
3320 mapper_caps.wChannels = 2;
3321 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
3323 caps = &mapper_caps;
3324 }else{
3325 if(uDeviceID >= g_inmmdevices_count)
3326 return MMSYSERR_BADDEVICEID;
3328 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3331 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3333 return MMSYSERR_NOERROR;
3336 /**************************************************************************
3337 * waveInGetDevCapsA [WINMM.@]
3339 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3341 UINT ret;
3342 WAVEINCAPSW wicW;
3344 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3346 if(!lpCaps)
3347 return MMSYSERR_INVALPARAM;
3349 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3351 if (ret == MMSYSERR_NOERROR) {
3352 WAVEINCAPSA wicA;
3353 wicA.wMid = wicW.wMid;
3354 wicA.wPid = wicW.wPid;
3355 wicA.vDriverVersion = wicW.vDriverVersion;
3356 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3357 sizeof(wicA.szPname), NULL, NULL );
3358 wicA.dwFormats = wicW.dwFormats;
3359 wicA.wChannels = wicW.wChannels;
3360 wicA.wReserved1 = wicW.wReserved1;
3361 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3363 return ret;
3366 /**************************************************************************
3367 * waveInOpen [WINMM.@]
3369 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3370 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3371 DWORD_PTR dwInstance, DWORD dwFlags)
3373 LRESULT res;
3374 WINMM_OpenInfo info;
3375 WINMM_CBInfo cb_info;
3377 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3378 dwCallback, dwInstance, dwFlags);
3380 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3381 return MMSYSERR_INVALPARAM;
3383 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3384 if(res != MMSYSERR_NOERROR)
3385 return res;
3387 if(!WINMM_StartDevicesThread())
3388 return MMSYSERR_NODRIVER;
3390 info.handle = 0;
3391 info.format = (WAVEFORMATEX*)lpFormat;
3392 info.callback = dwCallback;
3393 info.cb_user = dwInstance;
3394 info.req_device = uDeviceID;
3395 info.flags = dwFlags;
3396 info.reset = TRUE;
3398 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3399 InterlockedDecrement(&g_devthread_token);
3400 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3401 return res;
3403 if(lphWaveIn)
3404 *lphWaveIn = (HWAVEIN)info.handle;
3406 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3407 cb_info.callback = dwCallback;
3408 cb_info.user = dwInstance;
3409 cb_info.hwave = info.handle;
3411 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3413 return res;
3416 /**************************************************************************
3417 * waveInClose [WINMM.@]
3419 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3421 WINMM_Device *device;
3422 WINMM_CBInfo cb_info;
3423 UINT res;
3425 TRACE("(%p)\n", hWaveIn);
3427 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3429 if(!WINMM_ValidateAndLock(device))
3430 return MMSYSERR_INVALHANDLE;
3432 cb_info = device->cb_info;
3434 LeaveCriticalSection(&device->lock);
3436 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3438 if(res == MMSYSERR_NOERROR)
3439 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3441 return res;
3444 /**************************************************************************
3445 * waveInPrepareHeader [WINMM.@]
3447 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3448 UINT uSize)
3450 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3452 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3453 return MMSYSERR_INVALPARAM;
3455 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3456 return MMSYSERR_NOERROR;
3458 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3461 /**************************************************************************
3462 * waveInUnprepareHeader [WINMM.@]
3464 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3465 UINT uSize)
3467 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3469 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3470 return MMSYSERR_INVALPARAM;
3472 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3473 return WAVERR_STILLPLAYING;
3475 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3476 return MMSYSERR_NOERROR;
3478 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3481 /**************************************************************************
3482 * waveInAddBuffer [WINMM.@]
3484 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3486 WINMM_Device *device;
3488 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3490 if(!header || uSize < sizeof(WAVEHDR))
3491 return MMSYSERR_INVALPARAM;
3493 if(!(header->dwFlags & WHDR_PREPARED))
3494 return WAVERR_UNPREPARED;
3496 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3498 if(!WINMM_ValidateAndLock(device))
3499 return MMSYSERR_INVALHANDLE;
3501 if(!device->first)
3502 device->first = device->last = header;
3503 else{
3504 device->last->lpNext = header;
3505 device->last = header;
3508 header->dwBytesRecorded = 0;
3509 header->lpNext = NULL;
3510 header->dwFlags &= ~WHDR_DONE;
3511 header->dwFlags |= WHDR_INQUEUE;
3513 LeaveCriticalSection(&device->lock);
3515 return MMSYSERR_NOERROR;
3518 /**************************************************************************
3519 * waveInReset [WINMM.@]
3521 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3523 TRACE("(%p)\n", hWaveIn);
3525 return WINMM_Reset((HWAVE)hWaveIn);
3528 /**************************************************************************
3529 * waveInStart [WINMM.@]
3531 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3533 WINMM_Device *device;
3534 MMRESULT mr;
3536 TRACE("(%p)\n", hWaveIn);
3538 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3540 if(!WINMM_ValidateAndLock(device))
3541 return MMSYSERR_INVALHANDLE;
3543 mr = WINMM_BeginPlaying(device);
3545 LeaveCriticalSection(&device->lock);
3547 return mr;
3550 /**************************************************************************
3551 * waveInStop [WINMM.@]
3553 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3555 WINMM_CBInfo cb_info;
3556 WINMM_Device *device;
3557 WAVEHDR *buf;
3558 HRESULT hr;
3560 TRACE("(%p)\n", hWaveIn);
3562 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3564 if(!WINMM_ValidateAndLock(device))
3565 return MMSYSERR_INVALHANDLE;
3567 hr = WINMM_Pause((HWAVE)hWaveIn);
3568 if(FAILED(hr)){
3569 LeaveCriticalSection(&device->lock);
3570 return MMSYSERR_ERROR;
3572 device->stopped = TRUE;
3574 buf = device->first;
3575 if(buf && buf->dwBytesRecorded > 0){
3576 device->first = buf->lpNext;
3577 }else
3578 buf = NULL;
3580 cb_info = device->cb_info;
3582 LeaveCriticalSection(&device->lock);
3584 if(buf){
3585 buf->dwFlags &= ~WHDR_INQUEUE;
3586 buf->dwFlags |= WHDR_DONE;
3587 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3590 return MMSYSERR_NOERROR;
3593 /**************************************************************************
3594 * waveInGetPosition [WINMM.@]
3596 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3597 UINT uSize)
3599 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3601 if(!uSize || !lpTime)
3602 return MMSYSERR_INVALPARAM;
3604 if(uSize < sizeof(MMTIME))
3605 return MMSYSERR_ERROR;
3607 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3610 /**************************************************************************
3611 * waveInGetID [WINMM.@]
3613 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3615 UINT dev, junk;
3616 BOOL is_out;
3617 WINMM_Device *device;
3619 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3621 if(!lpuDeviceID)
3622 return MMSYSERR_INVALPARAM;
3624 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3625 if(!WINMM_ValidateAndLock(device))
3626 return MMSYSERR_INVALHANDLE;
3628 LeaveCriticalSection(&device->lock);
3630 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3632 return MMSYSERR_NOERROR;
3635 /**************************************************************************
3636 * waveInMessage [WINMM.@]
3638 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3639 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3641 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3643 switch(uMessage){
3644 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3645 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3646 (DWORD_PTR*)dwParam1, FALSE);
3647 case DRV_QUERYFUNCTIONINSTANCEID:
3648 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3649 case DRV_QUERYDEVICEINTERFACESIZE:
3650 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3651 NULL, (ULONG*)dwParam1);
3652 case DRV_QUERYDEVICEINTERFACE:
3654 ULONG size = dwParam2;
3655 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3656 (WCHAR*)dwParam1, &size);
3658 case DRV_QUERYMAPPABLE:
3659 return MMSYSERR_NOERROR;
3660 case DRVM_MAPPER_PREFERRED_GET:
3661 if(dwParam1) {
3662 if(g_inmmdevices_count > 0)
3663 /* Device 0 is always the default device */
3664 *(DWORD *)dwParam1 = 0;
3665 else
3666 *(DWORD *)dwParam1 = -1;
3669 if(dwParam2)
3670 /* Status flags */
3671 *(DWORD *)dwParam2 = 0;
3673 return MMSYSERR_NOERROR;
3676 TRACE("Message not supported: %u\n", uMessage);
3678 return MMSYSERR_NOTSUPPORTED;
3681 UINT WINAPI mixerGetNumDevs(void)
3683 HRESULT hr;
3685 TRACE("\n");
3687 hr = WINMM_InitMMDevices();
3688 if(FAILED(hr))
3689 return 0;
3691 return g_outmmdevices_count + g_inmmdevices_count;
3694 /**************************************************************************
3695 * mixerGetDevCapsA [WINMM.@]
3697 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3699 MIXERCAPSW micW;
3700 UINT ret;
3702 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3704 if(!lpCaps)
3705 return MMSYSERR_INVALPARAM;
3707 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3709 if (ret == MMSYSERR_NOERROR) {
3710 MIXERCAPSA micA;
3711 micA.wMid = micW.wMid;
3712 micA.wPid = micW.wPid;
3713 micA.vDriverVersion = micW.vDriverVersion;
3714 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3715 sizeof(micA.szPname), NULL, NULL );
3716 micA.fdwSupport = micW.fdwSupport;
3717 micA.cDestinations = micW.cDestinations;
3718 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3720 return ret;
3723 /**************************************************************************
3724 * mixerGetDevCapsW [WINMM.@]
3726 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3728 WINMM_MMDevice *mmdevice;
3729 MIXERCAPSW caps;
3730 HRESULT hr;
3732 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3734 hr = WINMM_InitMMDevices();
3735 if(FAILED(hr))
3736 return MMSYSERR_NODRIVER;
3738 if(!lpCaps)
3739 return MMSYSERR_INVALPARAM;
3741 if(!uSize)
3742 return MMSYSERR_NOERROR;
3744 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3745 mmdevice = WINMM_GetMixerMMDevice((HMIXEROBJ)uDeviceID,
3746 MIXER_OBJECTF_MIXER, NULL);
3747 else if(uDeviceID < g_outmmdevices_count)
3748 mmdevice = read_map(g_out_map, uDeviceID);
3749 else
3750 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3752 if(!mmdevice)
3753 return MMSYSERR_BADDEVICEID;
3755 if(mmdevice->dataflow == eRender)
3756 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3757 else
3758 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3760 caps.wMid = 0xFF;
3761 caps.wPid = 0xFF;
3762 caps.vDriverVersion = 0x00010001;
3763 caps.fdwSupport = 0;
3764 caps.cDestinations = 1;
3766 memcpy(lpCaps, &caps, uSize);
3768 return MMSYSERR_NOERROR;
3771 /**************************************************************************
3772 * mixerOpen [WINMM.@]
3774 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3775 DWORD_PTR dwInstance, DWORD fdwOpen)
3777 WINMM_MMDevice *mmdevice;
3778 MMRESULT mr;
3779 HRESULT hr;
3781 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3782 dwInstance, fdwOpen);
3784 hr = WINMM_InitMMDevices();
3785 if(FAILED(hr))
3786 return MMSYSERR_NODRIVER;
3788 if(!lphMix)
3789 return MMSYSERR_INVALPARAM;
3791 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3792 if(mr != MMSYSERR_NOERROR)
3793 return mr;
3795 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3796 return MMSYSERR_BADDEVICEID;
3798 if(uDeviceID < g_outmmdevices_count){
3799 mmdevice = read_map(g_out_map, uDeviceID);
3800 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3801 mmdevice->mixer_count);
3802 }else{
3803 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3804 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3805 FALSE, mmdevice->mixer_count);
3808 ++mmdevice->mixer_count;
3810 return MMSYSERR_NOERROR;
3813 /**************************************************************************
3814 * mixerClose [WINMM.@]
3816 UINT WINAPI mixerClose(HMIXER hMix)
3818 TRACE("(%p)\n", hMix);
3820 return MMSYSERR_NOERROR;
3823 /**************************************************************************
3824 * mixerGetID [WINMM.@]
3826 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3828 WINMM_MMDevice *mmdevice;
3829 HRESULT hr;
3831 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3833 hr = WINMM_InitMMDevices();
3834 if(FAILED(hr))
3835 return MMSYSERR_NODRIVER;
3837 if(!lpid)
3838 return MMSYSERR_INVALPARAM;
3840 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3841 if(!mmdevice)
3842 return MMSYSERR_INVALHANDLE;
3844 if(mmdevice->in_caps.szPname[0] != '\0')
3845 *lpid += g_outmmdevices_count;
3847 return MMSYSERR_NOERROR;
3850 /**************************************************************************
3851 * mixerGetControlDetailsW [WINMM.@]
3853 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3854 DWORD fdwDetails)
3856 WINMM_ControlDetails details;
3858 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3860 if(!WINMM_StartDevicesThread())
3861 return MMSYSERR_NODRIVER;
3863 if(!lpmcdW || !lpmcdW->paDetails)
3864 return MMSYSERR_INVALPARAM;
3866 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3868 details.hmix = hmix;
3869 details.details = lpmcdW;
3870 details.flags = fdwDetails;
3872 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3873 (DWORD_PTR)&details, 0);
3876 /**************************************************************************
3877 * mixerGetControlDetailsA [WINMM.@]
3879 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3880 DWORD fdwDetails)
3882 UINT ret = MMSYSERR_NOTSUPPORTED;
3884 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3886 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3887 return MMSYSERR_INVALPARAM;
3889 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3890 case MIXER_GETCONTROLDETAILSF_VALUE:
3891 /* can safely use A structure as it is, no string inside */
3892 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3893 break;
3894 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3896 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3897 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3898 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3899 unsigned int i;
3901 if (lpmcdA->u.cMultipleItems != 0) {
3902 size *= lpmcdA->u.cMultipleItems;
3904 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3905 lpmcdA->paDetails = pDetailsW;
3906 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3907 /* set up lpmcd->paDetails */
3908 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3909 /* copy from lpmcd->paDetails back to paDetailsW; */
3910 if (ret == MMSYSERR_NOERROR) {
3911 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3912 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3913 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3914 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3915 pDetailsA->szName,
3916 sizeof(pDetailsA->szName), NULL, NULL );
3917 pDetailsA++;
3918 pDetailsW++;
3920 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3921 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3923 HeapFree(GetProcessHeap(), 0, pDetailsW);
3924 lpmcdA->paDetails = pDetailsA;
3925 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3927 break;
3928 default:
3929 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3932 return ret;
3935 /**************************************************************************
3936 * mixerGetLineControlsA [WINMM.@]
3938 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3939 DWORD fdwControls)
3941 MIXERLINECONTROLSW mlcW;
3942 DWORD ret;
3943 unsigned int i;
3945 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3947 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3948 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3949 return MMSYSERR_INVALPARAM;
3951 mlcW.cbStruct = sizeof(mlcW);
3952 mlcW.dwLineID = lpmlcA->dwLineID;
3953 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3954 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3956 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3957 the control count is assumed to be 1 - This is relied upon by a game,
3958 "Dynomite Deluze" */
3959 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3960 mlcW.cControls = 1;
3961 } else {
3962 mlcW.cControls = lpmlcA->cControls;
3964 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3965 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3966 mlcW.cControls * mlcW.cbmxctrl);
3968 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3970 if (ret == MMSYSERR_NOERROR) {
3971 lpmlcA->dwLineID = mlcW.dwLineID;
3972 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3973 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3975 for (i = 0; i < mlcW.cControls; i++) {
3976 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3977 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3978 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3979 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3980 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3981 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3982 lpmlcA->pamxctrl[i].szShortName,
3983 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3984 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3985 lpmlcA->pamxctrl[i].szName,
3986 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3987 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3988 * sizeof(mlcW.pamxctrl[i].Bounds) */
3989 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3990 sizeof(mlcW.pamxctrl[i].Bounds));
3991 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3992 * sizeof(mlcW.pamxctrl[i].Metrics) */
3993 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3994 sizeof(mlcW.pamxctrl[i].Metrics));
3998 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
4000 return ret;
4003 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4004 MIXERCONTROLW *ctl, DWORD flags)
4006 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4007 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4008 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4009 ctl->cMultipleItems = 0;
4010 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4011 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szName, MIXER_LONG_NAME_CHARS);
4012 ctl->Bounds.s1.dwMinimum = 0;
4013 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4014 ctl->Metrics.cSteps = 192;
4016 return MMSYSERR_NOERROR;
4019 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4020 MIXERCONTROLW *ctl, DWORD flags)
4022 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4023 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4024 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4025 ctl->cMultipleItems = 0;
4026 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4027 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szName, MIXER_LONG_NAME_CHARS);
4028 ctl->Bounds.s1.dwMinimum = 0;
4029 ctl->Bounds.s1.dwMaximum = 1;
4030 ctl->Metrics.cSteps = 0;
4032 return MMSYSERR_NOERROR;
4035 /**************************************************************************
4036 * mixerGetLineControlsW [WINMM.@]
4038 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4039 DWORD fdwControls)
4041 WINMM_MMDevice *mmdevice;
4042 HRESULT hr;
4044 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4046 hr = WINMM_InitMMDevices();
4047 if(FAILED(hr))
4048 return MMSYSERR_NODRIVER;
4050 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4051 MIXER_GETLINECONTROLSF_ONEBYID |
4052 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4053 MIXER_OBJECTF_HMIXER |
4054 MIXER_OBJECTF_MIXER)){
4055 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4056 return MMSYSERR_INVALFLAG;
4059 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4060 return MMSYSERR_INVALPARAM;
4062 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4063 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4064 TRACE("cControls: %u\n", lpmlcW->cControls);
4066 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4067 if(!mmdevice)
4068 return MMSYSERR_INVALHANDLE;
4070 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4071 case MIXER_GETLINECONTROLSF_ALL:
4072 if(lpmlcW->cControls != 2)
4073 return MMSYSERR_INVALPARAM;
4074 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4075 return MMSYSERR_INVALPARAM;
4076 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4077 return MIXERR_INVALLINE;
4078 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4079 &lpmlcW->pamxctrl[0], fdwControls);
4080 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4081 &lpmlcW->pamxctrl[1], fdwControls);
4082 return MMSYSERR_NOERROR;
4083 case MIXER_GETLINECONTROLSF_ONEBYID:
4084 if(lpmlcW->cControls != 1)
4085 return MMSYSERR_INVALPARAM;
4086 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4087 return MMSYSERR_INVALPARAM;
4088 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4089 return MIXERR_INVALLINE;
4090 if(lpmlcW->u.dwControlID == 0)
4091 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4092 lpmlcW->pamxctrl, fdwControls);
4093 if(lpmlcW->u.dwControlID == 1)
4094 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4095 lpmlcW->pamxctrl, fdwControls);
4096 return MMSYSERR_NOTSUPPORTED;
4097 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4098 if(lpmlcW->cControls != 1)
4099 return MMSYSERR_INVALPARAM;
4100 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4101 return MMSYSERR_INVALPARAM;
4102 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4103 return MIXERR_INVALLINE;
4104 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4105 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4106 lpmlcW->pamxctrl, fdwControls);
4107 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4108 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4109 lpmlcW->pamxctrl, fdwControls);
4110 return MMSYSERR_NOTSUPPORTED;
4113 return MMSYSERR_NOTSUPPORTED;
4116 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4117 MIXERLINEW *info, DWORD flags)
4119 BOOL is_out = TRUE;
4120 if(mmdevice->in_caps.szPname[0] != '\0')
4121 is_out = FALSE;
4123 if(info->dwSource != 0)
4124 return MIXERR_INVALLINE;
4126 info->dwDestination = 0;
4127 info->dwLineID = 0;
4128 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4129 info->cConnections = 0;
4130 info->cControls = 2;
4131 /* volume & mute always affect all channels, so claim 1 channel */
4132 info->cChannels = 1;
4133 info->Target.dwDeviceID = mmdev_index;
4134 info->Target.wMid = ~0;
4135 info->Target.wPid = ~0;
4136 info->Target.vDriverVersion = 0;
4138 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4139 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4141 if(is_out){
4142 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4143 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4144 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4145 sizeof(info->Target.szPname));
4146 }else{
4147 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4148 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4149 info->Target.szPname[0] = '\0';
4152 return MMSYSERR_NOERROR;
4155 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4156 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4158 BOOL is_out = TRUE;
4159 if(mmdevice->in_caps.szPname[0] != '\0')
4160 is_out = FALSE;
4162 if(info->dwDestination != 0)
4163 return MIXERR_INVALLINE;
4165 info->dwSource = 0xFFFFFFFF;
4166 info->dwLineID = 0xFFFF0000;
4167 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4168 info->cConnections = 1;
4169 info->cControls = 2;
4171 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4172 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4174 info->Target.dwDeviceID = mmdev_index;
4175 info->Target.wMid = ~0;
4176 info->Target.wPid = ~0;
4177 info->Target.vDriverVersion = 0;
4179 if(is_out){
4180 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4181 info->cChannels = mmdevice->out_caps.wChannels;
4182 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4183 info->Target.szPname[0] = '\0';
4184 }else{
4185 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4186 info->cChannels = mmdevice->in_caps.wChannels;
4187 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4188 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4189 sizeof(info->Target.szPname));
4192 return MMSYSERR_NOERROR;
4195 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4196 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4198 BOOL is_out = TRUE;
4199 if(mmdevice->in_caps.szPname[0] != '\0')
4200 is_out = FALSE;
4202 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4203 if(is_out)
4204 return MIXERR_INVALLINE;
4205 info->dwDestination = 0;
4206 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4209 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4210 if(!is_out)
4211 return MIXERR_INVALLINE;
4212 info->dwDestination = 0;
4213 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4216 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4217 if(is_out)
4218 return MIXERR_INVALLINE;
4219 info->dwSource = 0;
4220 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4223 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4224 if(!is_out)
4225 return MIXERR_INVALLINE;
4226 info->dwSource = 0;
4227 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4230 TRACE("Returning INVALLINE on this component type: %u\n",
4231 info->dwComponentType);
4233 return MIXERR_INVALLINE;
4236 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4237 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4239 if(info->dwLineID == 0xFFFF0000){
4240 info->dwDestination = 0;
4241 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4244 if(info->dwLineID == 0){
4245 info->dwSource = 0;
4246 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4249 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4250 return MIXERR_INVALLINE;
4253 /**************************************************************************
4254 * mixerGetLineInfoW [WINMM.@]
4256 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4258 UINT mmdev_index;
4259 WINMM_MMDevice *mmdevice;
4260 HRESULT hr;
4262 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4264 hr = WINMM_InitMMDevices();
4265 if(FAILED(hr))
4266 return MMSYSERR_NODRIVER;
4268 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4269 return MMSYSERR_INVALPARAM;
4271 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4272 TRACE("dwSource: %u\n", lpmliW->dwSource);
4273 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4274 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4275 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4277 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4278 MIXER_GETLINEINFOF_DESTINATION |
4279 MIXER_GETLINEINFOF_LINEID |
4280 MIXER_GETLINEINFOF_SOURCE |
4281 MIXER_GETLINEINFOF_TARGETTYPE |
4282 MIXER_OBJECTF_HMIXER |
4283 MIXER_OBJECTF_MIXER)){
4284 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4285 return MMSYSERR_INVALFLAG;
4288 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4289 if(!mmdevice)
4290 return MMSYSERR_INVALHANDLE;
4292 lpmliW->dwUser = 0;
4294 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4295 case MIXER_GETLINEINFOF_DESTINATION:
4296 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4297 fdwInfo);
4298 case MIXER_GETLINEINFOF_SOURCE:
4299 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4300 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4301 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4302 fdwInfo);
4303 case MIXER_GETLINEINFOF_LINEID:
4304 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4305 case MIXER_GETLINEINFOF_TARGETTYPE:
4306 FIXME("TARGETTYPE flag not implemented!\n");
4307 return MIXERR_INVALLINE;
4310 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo);
4311 return MMSYSERR_INVALFLAG;
4314 /**************************************************************************
4315 * mixerGetLineInfoA [WINMM.@]
4317 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4318 DWORD fdwInfo)
4320 MIXERLINEW mliW;
4321 UINT ret;
4323 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4325 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4326 return MMSYSERR_INVALPARAM;
4328 mliW.cbStruct = sizeof(mliW);
4329 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4330 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4331 mliW.dwComponentType = lpmliA->dwComponentType;
4332 break;
4333 case MIXER_GETLINEINFOF_DESTINATION:
4334 mliW.dwDestination = lpmliA->dwDestination;
4335 break;
4336 case MIXER_GETLINEINFOF_LINEID:
4337 mliW.dwLineID = lpmliA->dwLineID;
4338 break;
4339 case MIXER_GETLINEINFOF_SOURCE:
4340 mliW.dwDestination = lpmliA->dwDestination;
4341 mliW.dwSource = lpmliA->dwSource;
4342 break;
4343 case MIXER_GETLINEINFOF_TARGETTYPE:
4344 mliW.Target.dwType = lpmliA->Target.dwType;
4345 mliW.Target.wMid = lpmliA->Target.wMid;
4346 mliW.Target.wPid = lpmliA->Target.wPid;
4347 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4348 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
4349 break;
4350 default:
4351 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4352 return MMSYSERR_INVALFLAG;
4355 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4357 if(ret == MMSYSERR_NOERROR)
4359 lpmliA->dwDestination = mliW.dwDestination;
4360 lpmliA->dwSource = mliW.dwSource;
4361 lpmliA->dwLineID = mliW.dwLineID;
4362 lpmliA->fdwLine = mliW.fdwLine;
4363 lpmliA->dwUser = mliW.dwUser;
4364 lpmliA->dwComponentType = mliW.dwComponentType;
4365 lpmliA->cChannels = mliW.cChannels;
4366 lpmliA->cConnections = mliW.cConnections;
4367 lpmliA->cControls = mliW.cControls;
4368 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4369 sizeof(lpmliA->szShortName), NULL, NULL);
4370 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4371 sizeof(lpmliA->szName), NULL, NULL );
4372 lpmliA->Target.dwType = mliW.Target.dwType;
4373 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4374 lpmliA->Target.wMid = mliW.Target.wMid;
4375 lpmliA->Target.wPid = mliW.Target.wPid;
4376 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4377 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4378 sizeof(lpmliA->Target.szPname), NULL, NULL );
4380 return ret;
4383 /**************************************************************************
4384 * mixerSetControlDetails [WINMM.@]
4386 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4387 DWORD fdwDetails)
4389 WINMM_ControlDetails details;
4390 UINT ret;
4392 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4394 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4395 MIXER_SETCONTROLDETAILSF_CUSTOM)
4396 return MMSYSERR_NOTSUPPORTED;
4398 if(!lpmcd)
4399 return MMSYSERR_INVALPARAM;
4401 if(!WINMM_StartDevicesThread())
4402 return MMSYSERR_NODRIVER;
4404 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4406 details.hmix = hmix;
4407 details.details = lpmcd;
4408 details.flags = fdwDetails;
4410 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4411 (DWORD_PTR)&details, 0);
4412 InterlockedDecrement(&g_devthread_token);
4413 return ret;
4416 /**************************************************************************
4417 * mixerMessage [WINMM.@]
4419 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4421 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4423 return MMSYSERR_NOTSUPPORTED;