d3dcompiler/tests: Add a couple more checks for parent data values.
[wine.git] / dlls / winmm / waveform.c
blob6b259ffaf080d037ffbd0f6584951663c7825e82
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"
40 #include "ole2.h"
41 #include "initguid.h"
42 #include "devpkey.h"
43 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "audiopolicy.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
51 /* FIXME: Should be localized */
52 static const WCHAR volumeW[] = {'V','o','l','u','m','e',0};
53 static const WCHAR mastervolumeW[] = {'M','a','s','t','e','r',' ','V','o','l',
54 'u','m','e',0};
55 static const WCHAR muteW[] = {'M','u','t','e',0};
57 /* HWAVE (and HMIXER) format:
59 * XXXX... 1FDD DDDD IIII IIII
60 * X = unused (must be 0)
61 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
62 * F = flow direction (0 = IN, 1 = OUT)
63 * D = index into g_out_mmdevices, or all 1s for the MAPPER device
64 * I = index in the mmdevice's devices array
66 * Two reasons that we don't just use pointers:
67 * - HWAVEs must fit into 16 bits for compatibility with old applications.
68 * - We must be able to identify bad devices without crashing.
71 /* buffer size = 100 * 100000 (100 ns) = 1 second */
72 #define AC_BUFLEN (100 * 100000)
73 #define MAX_DEVICES 256
74 #define MAPPER_INDEX 0x3F
76 typedef struct _WINMM_CBInfo {
77 DWORD_PTR callback;
78 DWORD_PTR user;
79 DWORD flags;
80 HWAVE hwave;
81 } WINMM_CBInfo;
83 struct _WINMM_MMDevice;
84 typedef struct _WINMM_MMDevice WINMM_MMDevice;
86 typedef struct _WINMM_Device {
87 WINMM_CBInfo cb_info;
89 HWAVE handle;
91 BOOL open;
93 IMMDevice *device;
94 IAudioClient *client;
95 IAudioRenderClient *render;
96 IAudioCaptureClient *capture;
97 IAudioClock *clock;
98 IAudioStreamVolume *volume;
100 WAVEFORMATEX *orig_fmt;
101 HACMSTREAM acm_handle;
102 ACMSTREAMHEADER acm_hdr;
103 UINT32 acm_offs;
105 WAVEHDR *first, *last, *playing, *loop_start;
107 BOOL stopped;
108 DWORD loop_counter;
109 UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
110 UINT32 remainder_frames; /* header chunk frames already played when a device switch occurred */
112 /* stored in frames of sample rate, *not* AC::GetFrequency */
113 UINT64 last_clock_pos;
115 HANDLE event;
116 CRITICAL_SECTION lock;
118 WINMM_MMDevice *parent;
119 } WINMM_Device;
121 struct _WINMM_MMDevice {
122 WAVEOUTCAPSW out_caps; /* must not be modified outside of WINMM_InitMMDevices*/
123 WAVEINCAPSW in_caps; /* must not be modified outside of WINMM_InitMMDevices*/
124 WCHAR *dev_id;
126 ISimpleAudioVolume *volume;
128 GUID session;
130 UINT index;
132 /* HMIXER format is the same as the HWAVE format, but the I bits are
133 * replaced by the value of this counter, to keep each HMIXER unique */
134 UINT mixer_count;
136 CRITICAL_SECTION lock;
138 WINMM_Device *devices[MAX_DEVICES];
141 static WINMM_MMDevice *g_out_mmdevices;
142 static WINMM_MMDevice **g_out_map;
143 static UINT g_outmmdevices_count;
144 static WINMM_Device *g_out_mapper_devices[MAX_DEVICES];
146 static WINMM_MMDevice *g_in_mmdevices;
147 static WINMM_MMDevice **g_in_map;
148 static UINT g_inmmdevices_count;
149 static WINMM_Device *g_in_mapper_devices[MAX_DEVICES];
151 static IMMDeviceEnumerator *g_devenum;
153 static CRITICAL_SECTION g_devthread_lock;
154 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug =
156 0, 0, &g_devthread_lock,
157 { &g_devthread_lock_debug.ProcessLocksList, &g_devthread_lock_debug.ProcessLocksList },
158 0, 0, { (DWORD_PTR)(__FILE__ ": g_devthread_lock") }
160 static CRITICAL_SECTION g_devthread_lock = { &g_devthread_lock_debug, -1, 0, 0, 0, 0 };
161 static LONG g_devthread_token;
162 static HANDLE g_devices_thread;
163 static HWND g_devices_hwnd;
164 static HMODULE g_devthread_module;
166 static UINT g_devhandle_count;
167 static HANDLE *g_device_handles;
168 static WINMM_Device **g_handle_devices;
170 typedef struct _WINMM_OpenInfo {
171 HWAVE handle;
172 UINT req_device;
173 WAVEFORMATEX *format;
174 DWORD_PTR callback;
175 DWORD_PTR cb_user;
176 DWORD flags;
177 BOOL reset;
178 } WINMM_OpenInfo;
180 typedef struct _WINMM_ControlDetails {
181 HMIXEROBJ hmix;
182 MIXERCONTROLDETAILS *details;
183 DWORD flags;
184 } WINMM_ControlDetails;
186 typedef struct _WINMM_QueryInterfaceInfo {
187 BOOL is_out;
188 UINT index;
189 WCHAR *str;
190 UINT *len_bytes;
191 } WINMM_QueryInterfaceInfo;
193 static LRESULT WOD_Open(WINMM_OpenInfo *info);
194 static LRESULT WOD_Close(HWAVEOUT hwave);
195 static LRESULT WID_Open(WINMM_OpenInfo *info);
196 static LRESULT WID_Close(HWAVEIN hwave);
197 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device);
199 void WINMM_DeleteWaveform(void)
201 UINT i, j;
203 if(g_devices_thread)
204 CloseHandle(g_devices_thread);
206 for(i = 0; i < g_outmmdevices_count; ++i){
207 WINMM_MMDevice *mmdevice = &g_out_mmdevices[i];
209 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
210 WINMM_Device *device = mmdevice->devices[j];
211 if(device->handle)
212 CloseHandle(device->handle);
213 DeleteCriticalSection(&device->lock);
216 if(mmdevice->volume)
217 ISimpleAudioVolume_Release(mmdevice->volume);
218 CoTaskMemFree(mmdevice->dev_id);
219 DeleteCriticalSection(&mmdevice->lock);
222 for(i = 0; i < g_inmmdevices_count; ++i){
223 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
225 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
226 WINMM_Device *device = mmdevice->devices[j];
227 if(device->handle)
228 CloseHandle(device->handle);
229 DeleteCriticalSection(&device->lock);
232 if(mmdevice->volume)
233 ISimpleAudioVolume_Release(mmdevice->volume);
234 CoTaskMemFree(mmdevice->dev_id);
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 if(flow == eRender){
514 dev->out_caps.wMid = 0xFF;
515 dev->out_caps.wPid = 0xFF;
516 dev->out_caps.vDriverVersion = 0x00010001;
517 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
518 dev->out_caps.wReserved1 = 0;
519 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
520 WAVECAPS_SAMPLEACCURATE;
521 dev->out_caps.wChannels = 2;
522 dev->out_caps.szPname[0] = '\0';
524 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
525 sizeof(dev->out_caps.szPname) /
526 sizeof(*dev->out_caps.szPname));
527 if(FAILED(hr))
528 return hr;
529 }else{
530 dev->in_caps.wMid = 0xFF;
531 dev->in_caps.wPid = 0xFF;
532 dev->in_caps.vDriverVersion = 0x00010001;
533 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
534 dev->in_caps.wReserved1 = 0;
535 dev->in_caps.wChannels = 2;
536 dev->in_caps.szPname[0] = '\0';
538 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
539 sizeof(dev->in_caps.szPname) /
540 sizeof(*dev->in_caps.szPname));
541 if(FAILED(hr))
542 return hr;
545 hr = IMMDevice_GetId(device, &dev->dev_id);
546 if(FAILED(hr))
547 return hr;
549 CoCreateGuid(&dev->session);
551 dev->index = index;
553 InitializeCriticalSection(&dev->lock);
554 dev->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
556 return S_OK;
559 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices,
560 WINMM_MMDevice ***map, UINT *devcount, EDataFlow flow,
561 IMMDeviceEnumerator *devenum)
563 IMMDeviceCollection *devcoll;
564 HRESULT hr;
566 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
567 DEVICE_STATE_ACTIVE, &devcoll);
568 if(FAILED(hr))
569 return hr;
571 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
572 if(FAILED(hr)){
573 IMMDeviceCollection_Release(devcoll);
574 return hr;
577 if(*devcount > 0){
578 UINT n, count = 1;
579 IMMDevice *def_dev = NULL;
581 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
582 sizeof(WINMM_MMDevice) * (*devcount));
583 if(!*devices){
584 IMMDeviceCollection_Release(devcoll);
585 return E_OUTOFMEMORY;
588 *map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
589 sizeof(WINMM_MMDevice *) * (*devcount));
590 if(!*map){
591 IMMDeviceCollection_Release(devcoll);
592 HeapFree(GetProcessHeap(), 0, *devices);
593 return E_OUTOFMEMORY;
596 /* make sure that device 0 is the default device */
597 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
598 flow, eConsole, &def_dev);
600 for(n = 0; n < *devcount; ++n){
601 IMMDevice *device;
603 hr = IMMDeviceCollection_Item(devcoll, n, &device);
604 if(SUCCEEDED(hr)){
605 WINMM_InitMMDevice(flow, device, &(*devices)[n], n);
607 if(device == def_dev)
608 (*map)[0] = &(*devices)[n];
609 else{
610 (*map)[count] = &(*devices)[n];
611 ++count;
614 IMMDevice_Release(device);
618 IMMDevice_Release(def_dev);
620 *devcount = count;
623 IMMDeviceCollection_Release(devcoll);
625 return S_OK;
628 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
629 const GUID *riid, void **obj)
631 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid));
632 return E_NOINTERFACE;
635 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
637 return 2;
640 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
642 return 1;
645 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
646 const WCHAR *device_id, DWORD new_state)
648 TRACE("Ignoring OnDeviceStateChanged callback\n");
649 return S_OK;
652 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
653 const WCHAR *device_id)
655 TRACE("Ignoring OnDeviceAdded callback\n");
656 return S_OK;
659 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
660 const WCHAR *device_id)
662 TRACE("Ignoring OnDeviceRemoved callback\n");
663 return S_OK;
666 static HRESULT update_mapping(WINMM_MMDevice ***map, UINT count,
667 const WCHAR *default_id)
669 WINMM_MMDevice *prev;
670 UINT i;
672 prev = (*map)[0];
673 for(i = 0; i < count; ++i){
674 WINMM_MMDevice *tmp;
676 if(!lstrcmpW((*map)[i]->dev_id, default_id)){
677 (*map)[0] = (*map)[i];
678 (*map)[i] = prev;
680 return S_OK;
683 tmp = (*map)[i];
684 (*map)[i] = prev;
685 prev = tmp;
688 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
689 (*map)[0] = prev;
691 return S_FALSE;
694 static HRESULT reroute_mapper_device(WINMM_Device *device, BOOL is_out)
696 WINMM_OpenInfo info;
697 BOOL stopped;
698 MMRESULT mr;
699 HRESULT hr;
700 UINT64 clock_freq, clock_pos;
702 TRACE("rerouting device %p\n", device->handle);
704 EnterCriticalSection(&device->lock);
706 if(!device->open || device->acm_handle){
707 /* Windows 7 doesn't re-route ACM devices, so we don't either.
708 * Seems to be because of the data waveXxxPrepareHeader allocates. */
709 LeaveCriticalSection(&device->lock);
710 return S_FALSE;
713 stopped = device->stopped;
715 info.handle = 0;
716 info.req_device = WAVE_MAPPER;
717 info.format = device->orig_fmt;
718 info.callback = device->cb_info.callback;
719 info.cb_user = device->cb_info.user;
720 /* We have to use direct here so that we don't suddenly introduce ACM
721 * into a playing stream that hasn't been Prepared for it */
722 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT_QUERY;
723 info.reset = FALSE;
725 if(is_out)
726 mr = WOD_Open(&info);
727 else
728 mr = WID_Open(&info);
730 if(mr != MMSYSERR_NOERROR){
731 TRACE("New default device doesn't support this stream: %p\n", device->handle);
732 LeaveCriticalSection(&device->lock);
733 return S_FALSE;
736 hr = IAudioClient_Stop(device->client);
737 if(FAILED(hr))
738 WARN("Stop failed: %08x\n", hr);
740 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
741 if(FAILED(hr)){
742 WARN("GetFrequency failed: %08x\n", hr);
743 LeaveCriticalSection(&device->lock);
744 return hr;
747 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
748 if(FAILED(hr)){
749 WARN("GetPosition failed: %08x\n", hr);
750 LeaveCriticalSection(&device->lock);
751 return hr;
754 device->remainder_frames = MulDiv(clock_pos, device->samples_per_sec, clock_freq) - device->last_clock_pos;
756 info.handle = device->handle;
757 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT;
759 if(is_out){
760 WOD_Close((HWAVEOUT)device->handle);
761 device->parent = read_map(g_out_map, 0);
762 mr = WOD_Open(&info);
763 }else{
764 WID_Close((HWAVEIN)device->handle);
765 device->parent = read_map(g_in_map, 0);
766 mr = WID_Open(&info);
769 if(mr != MMSYSERR_NOERROR){
770 ERR("Opening new default device failed! %u\n", mr);
771 LeaveCriticalSection(&device->lock);
772 return E_FAIL;
775 HeapFree(GetProcessHeap(), 0, info.format);
777 if(!stopped)
778 WINMM_BeginPlaying(device);
780 LeaveCriticalSection(&device->lock);
782 return S_OK;
785 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
786 EDataFlow flow, ERole role, const WCHAR *device_id)
788 UINT i;
790 TRACE("%u %u %s\n", flow, role, wine_dbgstr_w(device_id));
792 if(role != eConsole)
793 return S_OK;
795 EnterCriticalSection(&g_devthread_lock);
797 if(flow == eRender)
798 update_mapping(&g_out_map, g_outmmdevices_count, device_id);
799 else
800 update_mapping(&g_in_map, g_inmmdevices_count, device_id);
802 for(i = 0; i < MAX_DEVICES && g_out_mapper_devices[i]; ++i)
803 reroute_mapper_device(g_out_mapper_devices[i], TRUE);
805 for(i = 0; i < MAX_DEVICES && g_in_mapper_devices[i]; ++i)
806 reroute_mapper_device(g_in_mapper_devices[i], FALSE);
808 LeaveCriticalSection(&g_devthread_lock);
810 return S_OK;
813 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
814 const WCHAR *device_id, const PROPERTYKEY key)
816 TRACE("Ignoring OnPropertyValueChanged callback\n");
817 return S_OK;
820 static IMMNotificationClientVtbl g_notif_vtbl = {
821 notif_QueryInterface,
822 notif_AddRef,
823 notif_Release,
824 notif_OnDeviceStateChanged,
825 notif_OnDeviceAdded,
826 notif_OnDeviceRemoved,
827 notif_OnDefaultDeviceChanged,
828 notif_OnPropertyValueChanged
831 static IMMNotificationClient g_notif = { &g_notif_vtbl };
833 static HRESULT WINMM_InitMMDevices(void)
835 HRESULT hr, init_hr;
836 IMMDeviceEnumerator *devenum = NULL;
838 if(g_outmmdevices_count || g_inmmdevices_count)
839 return S_FALSE;
841 init_hr = CoInitialize(NULL);
843 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
844 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
845 if(FAILED(hr))
846 goto exit;
848 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum, &g_notif);
849 if(FAILED(hr))
850 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr);
852 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_out_map, &g_outmmdevices_count,
853 eRender, devenum);
854 if(FAILED(hr)){
855 g_outmmdevices_count = 0;
856 g_inmmdevices_count = 0;
857 goto exit;
860 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_in_map, &g_inmmdevices_count,
861 eCapture, devenum);
862 if(FAILED(hr)){
863 g_inmmdevices_count = 0;
864 goto exit;
867 exit:
868 if(devenum)
869 IMMDeviceEnumerator_Release(devenum);
870 if(SUCCEEDED(init_hr))
871 CoUninitialize();
873 return hr;
876 static inline BOOL WINMM_IsMapper(UINT device)
878 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
881 static MMRESULT WINMM_TryDeviceMapping(WINMM_Device *device, WAVEFORMATEX *fmt,
882 WORD channels, DWORD freq, DWORD bits_per_samp, BOOL is_query, BOOL is_out)
884 WAVEFORMATEX target, *closer_fmt = NULL;
885 HRESULT hr;
886 MMRESULT mr;
888 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
889 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
891 target.wFormatTag = WAVE_FORMAT_PCM;
892 target.nChannels = channels;
893 target.nSamplesPerSec = freq;
894 target.wBitsPerSample = bits_per_samp;
895 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
896 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
897 target.cbSize = 0;
899 hr = IAudioClient_IsFormatSupported(device->client,
900 AUDCLNT_SHAREMODE_SHARED, &target, &closer_fmt);
901 if(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 if(closer_fmt)
1122 CoTaskMemFree(closer_fmt);
1123 if((hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT) && !(info->flags & WAVE_FORMAT_DIRECT))
1124 ret = WINMM_MapDevice(device, TRUE, is_out);
1125 else
1126 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1127 goto error;
1130 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1131 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1132 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1133 if(FAILED(hr)){
1134 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1135 ret = WINMM_MapDevice(device, FALSE, is_out);
1136 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1137 goto error;
1138 }else{
1139 WARN("Initialize failed: %08x\n", hr);
1140 ret = hr2mmr(hr);
1141 goto error;
1143 }else{
1144 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1145 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1148 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1149 (void**)&device->clock);
1150 if(FAILED(hr)){
1151 WARN("GetService failed: %08x\n", hr);
1152 goto error;
1155 if(!device->event){
1156 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1157 if(!device->event){
1158 WARN("CreateEvent failed: %08x\n", hr);
1159 goto error;
1162 /* As the devices thread is waiting on g_device_handles, it can
1163 * only be modified from within this same thread. */
1164 if(g_device_handles){
1165 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1166 sizeof(HANDLE) * (g_devhandle_count + 1));
1167 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1168 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1169 }else{
1170 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1171 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1172 sizeof(WINMM_Device *));
1174 g_device_handles[g_devhandle_count] = device->event;
1175 g_handle_devices[g_devhandle_count] = device;
1176 ++g_devhandle_count;
1179 hr = IAudioClient_SetEventHandle(device->client, device->event);
1180 if(FAILED(hr)){
1181 WARN("SetEventHandle failed: %08x\n", hr);
1182 goto error;
1185 if(info->reset){
1186 device->played_frames = 0;
1187 device->ofs_bytes = 0;
1188 device->loop_counter = 0;
1189 device->first = device->last = device->playing = device->loop_start = NULL;
1192 device->stopped = TRUE;
1193 device->last_clock_pos = 0;
1195 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1196 device->cb_info.callback = info->callback;
1197 device->cb_info.user = info->cb_user;
1198 device->cb_info.hwave = device->handle;
1200 info->handle = device->handle;
1202 return MMSYSERR_NOERROR;
1204 error:
1205 if(device->client){
1206 IAudioClient_Release(device->client);
1207 device->client = NULL;
1209 if(device->device){
1210 IMMDevice_Release(device->device);
1211 device->device = NULL;
1214 return ret;
1217 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1219 WINMM_Device *device;
1220 LRESULT ret = MMSYSERR_ERROR;
1221 HRESULT hr;
1223 if(info->handle != 0){
1224 device = WINMM_GetDeviceFromHWAVE(info->handle);
1225 if(!device){
1226 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1227 return MMSYSERR_ERROR;
1230 EnterCriticalSection(&device->lock);
1232 device->open = TRUE;
1233 }else{
1234 CRITICAL_SECTION *lock;
1235 UINT internal_index;
1236 WINMM_Device **devices;
1237 WINMM_MMDevice *mmdevice;
1239 if(WINMM_IsMapper(info->req_device)){
1240 if (g_outmmdevices_count == 0)
1241 return MMSYSERR_BADDEVICEID;
1242 devices = g_out_mapper_devices;
1243 mmdevice = read_map(g_out_map, 0);
1244 lock = &g_devthread_lock;
1245 internal_index = MAPPER_INDEX;
1246 }else{
1247 if(info->req_device >= g_outmmdevices_count)
1248 return MMSYSERR_BADDEVICEID;
1250 mmdevice = read_map(g_out_map, info->req_device);
1252 if(!mmdevice->out_caps.szPname[0])
1253 return MMSYSERR_NOTENABLED;
1255 devices = mmdevice->devices;
1256 lock = &mmdevice->lock;
1257 internal_index = mmdevice->index;
1260 EnterCriticalSection(lock);
1262 device = WINMM_FindUnusedDevice(devices, mmdevice,
1263 internal_index, TRUE);
1264 if(!device){
1265 LeaveCriticalSection(lock);
1266 return MMSYSERR_ALLOCATED;
1269 LeaveCriticalSection(lock);
1272 ret = WINMM_OpenDevice(device, info, TRUE);
1273 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1274 goto error;
1275 ret = MMSYSERR_ERROR;
1277 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1278 (void**)&device->render);
1279 if(FAILED(hr)){
1280 ERR("GetService failed: %08x\n", hr);
1281 goto error;
1284 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1285 (void**)&device->volume);
1286 if(FAILED(hr)){
1287 ERR("GetService failed: %08x\n", hr);
1288 goto error;
1291 LeaveCriticalSection(&device->lock);
1293 return MMSYSERR_NOERROR;
1295 error:
1296 if(device->device){
1297 IMMDevice_Release(device->device);
1298 device->device = NULL;
1300 if(device->client){
1301 IAudioClient_Release(device->client);
1302 device->client = NULL;
1304 if(device->render){
1305 IAudioRenderClient_Release(device->render);
1306 device->render = NULL;
1308 if(device->volume){
1309 IAudioStreamVolume_Release(device->volume);
1310 device->volume = NULL;
1312 if(device->clock){
1313 IAudioClock_Release(device->clock);
1314 device->clock = NULL;
1316 device->open = FALSE;
1317 LeaveCriticalSection(&device->lock);
1318 return ret;
1321 static LRESULT WID_Open(WINMM_OpenInfo *info)
1323 WINMM_Device *device, **devices;
1324 WINMM_MMDevice *mmdevice;
1325 UINT internal_index;
1326 CRITICAL_SECTION *lock;
1327 LRESULT ret = MMSYSERR_ERROR;
1328 HRESULT hr;
1330 if(WINMM_IsMapper(info->req_device)){
1331 if (g_inmmdevices_count == 0)
1332 return MMSYSERR_BADDEVICEID;
1333 devices = g_in_mapper_devices;
1334 mmdevice = read_map(g_in_map, 0);
1335 lock = &g_devthread_lock;
1336 internal_index = MAPPER_INDEX;
1337 }else{
1338 if(info->req_device >= g_inmmdevices_count)
1339 return MMSYSERR_BADDEVICEID;
1341 mmdevice = read_map(g_in_map, info->req_device);
1343 if(!mmdevice->in_caps.szPname[0])
1344 return MMSYSERR_NOTENABLED;
1346 devices = mmdevice->devices;
1347 lock = &mmdevice->lock;
1348 internal_index = mmdevice->index;
1351 EnterCriticalSection(lock);
1353 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1354 if(!device){
1355 LeaveCriticalSection(lock);
1356 return MMSYSERR_ALLOCATED;
1359 LeaveCriticalSection(lock);
1361 ret = WINMM_OpenDevice(device, info, FALSE);
1362 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1363 goto error;
1364 ret = MMSYSERR_ERROR;
1366 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1367 (void**)&device->capture);
1368 if(FAILED(hr)){
1369 WARN("GetService failed: %08x\n", hr);
1370 goto error;
1373 LeaveCriticalSection(&device->lock);
1375 return MMSYSERR_NOERROR;
1377 error:
1378 if(device->device){
1379 IMMDevice_Release(device->device);
1380 device->device = NULL;
1382 if(device->client){
1383 IAudioClient_Release(device->client);
1384 device->client = NULL;
1386 if(device->capture){
1387 IAudioCaptureClient_Release(device->capture);
1388 device->capture = NULL;
1390 if(device->clock){
1391 IAudioClock_Release(device->clock);
1392 device->clock = NULL;
1394 device->open = FALSE;
1395 LeaveCriticalSection(&device->lock);
1396 return ret;
1399 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1401 device->open = FALSE;
1403 if(!device->stopped){
1404 IAudioClient_Stop(device->client);
1405 device->stopped = TRUE;
1408 if(device->acm_handle){
1409 acmStreamClose(device->acm_handle, 0);
1410 device->acm_handle = NULL;
1413 IMMDevice_Release(device->device);
1414 device->device = NULL;
1416 IAudioClient_Release(device->client);
1417 device->client = NULL;
1419 IAudioClock_Release(device->clock);
1420 device->clock = NULL;
1422 return S_OK;
1425 static LRESULT WOD_Close(HWAVEOUT hwave)
1427 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1429 TRACE("(%p)\n", hwave);
1431 if(!WINMM_ValidateAndLock(device))
1432 return MMSYSERR_INVALHANDLE;
1434 WINMM_CloseDevice(device);
1436 IAudioRenderClient_Release(device->render);
1437 device->render = NULL;
1439 IAudioStreamVolume_Release(device->volume);
1440 device->volume = NULL;
1442 LeaveCriticalSection(&device->lock);
1444 return MMSYSERR_NOERROR;
1447 static LRESULT WID_Close(HWAVEIN hwave)
1449 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1451 TRACE("(%p)\n", hwave);
1453 if(!WINMM_ValidateAndLock(device))
1454 return MMSYSERR_INVALHANDLE;
1456 WINMM_CloseDevice(device);
1458 IAudioCaptureClient_Release(device->capture);
1459 device->capture = NULL;
1461 LeaveCriticalSection(&device->lock);
1463 return MMSYSERR_NOERROR;
1466 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1468 return length - length % device->bytes_per_frame;
1471 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1473 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1475 TRACE("(%p, %p)\n", hwave, header);
1477 if(!WINMM_ValidateAndLock(device))
1478 return MMSYSERR_INVALHANDLE;
1480 if(device->render && device->acm_handle){
1481 ACMSTREAMHEADER *ash;
1482 DWORD size;
1483 MMRESULT mr;
1485 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1486 ACM_STREAMSIZEF_SOURCE);
1487 if(mr != MMSYSERR_NOERROR){
1488 LeaveCriticalSection(&device->lock);
1489 return mr;
1492 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1493 if(!ash){
1494 LeaveCriticalSection(&device->lock);
1495 return MMSYSERR_NOMEM;
1498 ash->cbStruct = sizeof(*ash);
1499 ash->fdwStatus = 0;
1500 ash->dwUser = (DWORD_PTR)header;
1501 ash->pbSrc = (BYTE*)header->lpData;
1502 ash->cbSrcLength = header->dwBufferLength;
1503 ash->dwSrcUser = header->dwUser;
1504 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1505 ash->cbDstLength = size;
1506 ash->dwDstUser = 0;
1508 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1509 if(mr != MMSYSERR_NOERROR){
1510 HeapFree(GetProcessHeap(), 0, ash);
1511 LeaveCriticalSection(&device->lock);
1512 return mr;
1515 header->reserved = (DWORD_PTR)ash;
1518 LeaveCriticalSection(&device->lock);
1520 header->dwFlags |= WHDR_PREPARED;
1521 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1523 return MMSYSERR_NOERROR;
1526 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1528 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1530 TRACE("(%p, %p)\n", hwave, header);
1532 if(!WINMM_ValidateAndLock(device))
1533 return MMSYSERR_INVALHANDLE;
1535 if(device->render && device->acm_handle){
1536 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1538 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1540 HeapFree(GetProcessHeap(), 0, ash);
1543 LeaveCriticalSection(&device->lock);
1545 header->dwFlags &= ~WHDR_PREPARED;
1547 return MMSYSERR_NOERROR;
1550 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1552 if(device->acm_handle){
1553 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1554 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1557 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1560 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1562 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1565 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1567 HRESULT hr;
1568 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1569 UINT64 clock_freq, clock_pos, clock_frames;
1570 UINT32 nloops, queue_frames = 0;
1572 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1573 if(FAILED(hr)){
1574 WARN("GetFrequency failed: %08x\n", hr);
1575 return NULL;
1578 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1579 if(FAILED(hr)){
1580 WARN("GetPosition failed: %08x\n", hr);
1581 return NULL;
1584 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1586 nloops = device->loop_counter;
1587 while(queue &&
1588 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1589 clock_frames - device->last_clock_pos + device->remainder_frames){
1590 if(!nloops){
1591 last = queue;
1592 device->last_clock_pos += queue_frames;
1593 device->remainder_frames = 0;
1594 queue_frames = 0;
1595 queue = device->first = queue->lpNext;
1596 }else{
1597 if(queue->dwFlags & WHDR_BEGINLOOP){
1598 if(queue->dwFlags & WHDR_ENDLOOP)
1599 --nloops;
1600 else
1601 queue = queue->lpNext;
1602 }else if(queue->dwFlags & WHDR_ENDLOOP){
1603 queue = device->loop_start;
1604 --nloops;
1609 if(last){
1610 last->lpNext = NULL;
1611 return first;
1612 }else
1613 return NULL;
1616 static void WOD_PushData(WINMM_Device *device)
1618 WINMM_CBInfo cb_info;
1619 HRESULT hr;
1620 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1621 UINT32 queue_bytes, nloops;
1622 BYTE *data;
1623 WAVEHDR *queue, *first = NULL;
1625 TRACE("(%p)\n", device->handle);
1627 EnterCriticalSection(&device->lock);
1629 if(!device->device)
1630 goto exit;
1632 if(!device->first){
1633 if (device->stopped)
1634 goto exit;
1635 device->stopped = TRUE;
1636 device->last_clock_pos = 0;
1637 IAudioClient_Stop(device->client);
1638 IAudioClient_Reset(device->client);
1639 goto exit;
1642 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1643 if(FAILED(hr)){
1644 WARN("GetBufferSize failed: %08x\n", hr);
1645 goto exit;
1648 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1649 if(FAILED(hr)){
1650 WARN("GetCurrentPadding failed: %08x\n", hr);
1651 goto exit;
1654 first = WOD_MarkDoneHeaders(device);
1656 /* determine which is larger between the available buffer size and
1657 * the amount of data left in the queue */
1658 avail_frames = bufsize - pad;
1660 queue = device->playing;
1661 ofs = device->ofs_bytes;
1662 queue_frames = 0;
1663 nloops = 0;
1664 while(queue && queue_frames < avail_frames){
1665 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1666 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1667 ofs = 0;
1669 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1670 queue = device->loop_start;
1671 ++nloops;
1672 }else
1673 queue = queue->lpNext;
1676 if(queue_frames < avail_frames)
1677 avail_frames = queue_frames;
1678 if(avail_frames == 0)
1679 goto exit;
1681 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1682 if(FAILED(hr)){
1683 WARN("GetBuffer failed: %08x\n", hr);
1684 goto exit;
1687 written = 0;
1688 while(device->playing && written < avail_frames){
1689 UINT32 copy_frames, copy_bytes;
1690 BYTE *queue_data;
1692 queue = device->playing;
1694 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1695 if(device->acm_handle)
1696 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1697 else
1698 queue_data = (BYTE*)queue->lpData;
1700 queue_frames = (queue_bytes - device->ofs_bytes) /
1701 device->bytes_per_frame;
1703 copy_frames = queue_frames < (avail_frames - written) ?
1704 queue_frames : avail_frames - written;
1705 copy_bytes = copy_frames * device->bytes_per_frame;
1707 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1709 data += copy_bytes;
1710 written += copy_frames;
1711 device->ofs_bytes += copy_bytes;
1713 if(device->ofs_bytes >= queue_bytes){
1714 device->ofs_bytes = 0;
1716 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1717 device->playing = queue->lpNext;
1718 else{
1719 if(queue->dwFlags & WHDR_BEGINLOOP){
1720 device->loop_start = device->playing;
1721 device->playing = queue->lpNext;
1722 device->loop_counter = queue->dwLoops;
1724 if(queue->dwFlags & WHDR_ENDLOOP){
1725 --device->loop_counter;
1726 if(device->loop_counter)
1727 device->playing = device->loop_start;
1728 else
1729 device->loop_start = device->playing = queue->lpNext;
1735 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1736 if(FAILED(hr)){
1737 WARN("ReleaseBuffer failed: %08x\n", hr);
1738 goto exit;
1741 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1742 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1743 else
1744 device->played_frames += avail_frames;
1746 exit:
1747 cb_info = device->cb_info;
1749 LeaveCriticalSection(&device->lock);
1751 while(first){
1752 WAVEHDR *next = first->lpNext;
1753 first->dwFlags &= ~WHDR_INQUEUE;
1754 first->dwFlags |= WHDR_DONE;
1755 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1756 first = next;
1760 static void WID_PullACMData(WINMM_Device *device)
1762 UINT32 packet, packet_bytes;
1763 DWORD flags;
1764 BYTE *data;
1765 WAVEHDR *queue;
1766 HRESULT hr;
1767 MMRESULT mr;
1769 if(device->acm_hdr.cbDstLength == 0){
1770 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1771 &flags, NULL, NULL);
1772 if(hr != S_OK){
1773 if(FAILED(hr))
1774 WARN("GetBuffer failed: %08x\n", hr);
1775 return;
1778 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1779 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1781 device->acm_offs = 0;
1783 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1784 device->acm_hdr.fdwStatus = 0;
1785 device->acm_hdr.dwUser = 0;
1786 device->acm_hdr.pbSrc = data;
1787 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1788 device->acm_hdr.cbSrcLengthUsed = 0;
1789 device->acm_hdr.dwSrcUser = 0;
1790 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1791 device->acm_hdr.cbDstLength = packet_bytes;
1792 device->acm_hdr.cbDstLengthUsed = 0;
1793 device->acm_hdr.dwDstUser = 0;
1795 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1796 if(mr != MMSYSERR_NOERROR){
1797 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1798 return;
1801 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1802 if(mr != MMSYSERR_NOERROR){
1803 WARN("acmStreamConvert failed: %d\n", mr);
1804 return;
1807 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1808 if(FAILED(hr))
1809 WARN("ReleaseBuffer failed: %08x\n", hr);
1811 device->played_frames += packet;
1814 queue = device->first;
1815 while(queue){
1816 UINT32 to_copy_bytes;
1818 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1819 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1821 memcpy(queue->lpData + queue->dwBytesRecorded,
1822 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1824 queue->dwBytesRecorded += to_copy_bytes;
1825 device->acm_offs += to_copy_bytes;
1827 if(queue->dwBufferLength - queue->dwBytesRecorded <
1828 device->bytes_per_frame){
1829 queue->dwFlags &= ~WHDR_INQUEUE;
1830 queue->dwFlags |= WHDR_DONE;
1831 device->first = queue = queue->lpNext;
1834 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1835 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1836 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1837 device->acm_hdr.cbDstLength = 0;
1838 device->acm_hdr.cbDstLengthUsed = 0;
1840 /* done with this ACM Header, so try to pull more data */
1841 WID_PullACMData(device);
1842 return;
1846 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1847 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1848 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1849 device->acm_hdr.cbDstLength = 0;
1850 device->acm_hdr.cbDstLengthUsed = 0;
1853 static void WID_PullData(WINMM_Device *device)
1855 WINMM_CBInfo cb_info;
1856 WAVEHDR *queue, *first = NULL, *last = NULL;
1857 HRESULT hr;
1859 TRACE("(%p)\n", device->handle);
1861 EnterCriticalSection(&device->lock);
1863 if(!device->device || !device->first)
1864 goto exit;
1866 first = device->first;
1868 if(device->acm_handle){
1869 WID_PullACMData(device);
1870 goto exit;
1873 while(device->first){
1874 BYTE *data;
1875 UINT32 packet_len, packet;
1876 DWORD flags;
1878 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1879 &flags, NULL, NULL);
1880 if(hr != S_OK){
1881 if(FAILED(hr))
1882 WARN("GetBuffer failed: %08x\n", hr);
1883 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1884 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1885 goto exit;
1888 packet = packet_len;
1889 queue = device->first;
1890 while(queue && packet > 0){
1891 UINT32 to_copy_bytes;
1893 to_copy_bytes = min(packet * device->bytes_per_frame,
1894 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1896 memcpy(queue->lpData + queue->dwBytesRecorded,
1897 data + (packet_len - packet) * device->bytes_per_frame,
1898 to_copy_bytes);
1900 queue->dwBytesRecorded += to_copy_bytes;
1902 if(queue->dwBufferLength - queue->dwBytesRecorded <
1903 device->bytes_per_frame){
1904 last = queue;
1905 device->first = queue = queue->lpNext;
1908 packet -= to_copy_bytes / device->bytes_per_frame;
1911 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1912 if(FAILED(hr))
1913 WARN("ReleaseBuffer failed: %08x\n", hr);
1915 if(packet > 0)
1916 WARN("losing %u frames\n", packet);
1917 device->played_frames += packet_len - packet;
1920 exit:
1921 cb_info = device->cb_info;
1923 LeaveCriticalSection(&device->lock);
1925 if(last){
1926 last->lpNext = NULL;
1927 while(first){
1928 WAVEHDR *next = first->lpNext;
1929 first->dwFlags &= ~WHDR_INQUEUE;
1930 first->dwFlags |= WHDR_DONE;
1931 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1932 first = next;
1937 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1939 HRESULT hr;
1941 TRACE("(%p)\n", device->handle);
1943 if(device->render)
1944 /* prebuffer data before starting */
1945 WOD_PushData(device);
1947 if(device->stopped){
1948 device->stopped = FALSE;
1950 hr = IAudioClient_Start(device->client);
1951 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1952 device->stopped = TRUE;
1953 WARN("Start failed: %08x\n", hr);
1954 return MMSYSERR_ERROR;
1958 return MMSYSERR_NOERROR;
1961 static LRESULT WINMM_Pause(HWAVE hwave)
1963 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1964 HRESULT hr;
1966 TRACE("(%p)\n", hwave);
1968 if(!WINMM_ValidateAndLock(device))
1969 return MMSYSERR_INVALHANDLE;
1971 hr = IAudioClient_Stop(device->client);
1972 if(FAILED(hr)){
1973 LeaveCriticalSection(&device->lock);
1974 WARN("Stop failed: %08x\n", hr);
1975 return MMSYSERR_ERROR;
1978 device->stopped = FALSE;
1980 LeaveCriticalSection(&device->lock);
1982 return MMSYSERR_NOERROR;
1985 static LRESULT WINMM_Reset(HWAVE hwave)
1987 WINMM_CBInfo cb_info;
1988 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1989 BOOL is_out;
1990 WAVEHDR *first;
1991 HRESULT hr;
1993 TRACE("(%p)\n", hwave);
1995 if(!WINMM_ValidateAndLock(device))
1996 return MMSYSERR_INVALHANDLE;
1998 hr = IAudioClient_Stop(device->client);
1999 if(FAILED(hr)){
2000 LeaveCriticalSection(&device->lock);
2001 WARN("Stop failed: %08x\n", hr);
2002 return MMSYSERR_ERROR;
2004 device->stopped = TRUE;
2006 first = device->first;
2007 device->first = device->last = device->playing = NULL;
2008 device->ofs_bytes = 0;
2009 device->played_frames = 0;
2010 device->loop_counter = 0;
2011 device->last_clock_pos = 0;
2012 IAudioClient_Reset(device->client);
2014 cb_info = device->cb_info;
2015 is_out = device->render != NULL;
2017 LeaveCriticalSection(&device->lock);
2019 while(first){
2020 WAVEHDR *next = first->lpNext;
2021 first->dwFlags &= ~WHDR_INQUEUE;
2022 first->dwFlags |= WHDR_DONE;
2023 if(is_out)
2024 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2025 else
2026 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2027 first = next;
2030 return MMSYSERR_NOERROR;
2033 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2034 UINT32 sample_rate, UINT32 bytes_per_sec)
2036 switch(time->wType){
2037 case TIME_SAMPLES:
2038 time->u.sample = played_frames;
2039 return MMSYSERR_NOERROR;
2040 case TIME_MS:
2041 time->u.ms = (UINT64)played_frames * 1000 / sample_rate;
2042 return MMSYSERR_NOERROR;
2043 case TIME_SMPTE:
2044 time->u.smpte.fps = 30;
2045 played_frames += sample_rate / time->u.smpte.fps - 1; /* round up */
2046 time->u.smpte.frame = (played_frames % sample_rate) * time->u.smpte.fps / sample_rate;
2047 played_frames /= sample_rate; /* yields seconds */
2048 time->u.smpte.sec = played_frames % 60;
2049 played_frames /= 60;
2050 time->u.smpte.min = played_frames % 60;
2051 time->u.smpte.hour= played_frames / 60;
2052 return MMSYSERR_NOERROR;
2053 default:
2054 time->wType = TIME_BYTES;
2055 /* fall through */
2056 case TIME_BYTES:
2057 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2058 return MMSYSERR_NOERROR;
2062 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2064 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2065 UINT32 played_frames, sample_rate, bytes_per_sec;
2067 TRACE("(%p, %p)\n", hwave, time);
2069 if(!WINMM_ValidateAndLock(device))
2070 return MMSYSERR_INVALHANDLE;
2072 played_frames = device->played_frames;
2073 sample_rate = device->orig_fmt->nSamplesPerSec;
2074 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2076 LeaveCriticalSection(&device->lock);
2078 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2081 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2082 UINT *mmdev_index)
2084 UINT mmdev, dev, junk, *out;
2085 BOOL is_out;
2087 if(!mmdev_index)
2088 out = &mmdev;
2089 else
2090 out = mmdev_index;
2092 switch(flags & 0xF0000000){
2093 case MIXER_OBJECTF_MIXER: /* == 0 */
2094 *out = HandleToULong(hmix);
2095 if(*out < g_outmmdevices_count)
2096 return read_map(g_out_map, *out);
2097 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2098 *out -= g_outmmdevices_count;
2099 return read_map(g_in_map, *out);
2101 /* fall through -- if it's not a valid mixer device, then
2102 * it could be a valid mixer handle. windows seems to do
2103 * this as well. */
2104 case MIXER_OBJECTF_HMIXER:
2105 case MIXER_OBJECTF_HWAVEOUT:
2106 case MIXER_OBJECTF_HWAVEIN:
2107 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2108 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2109 (!is_out && *out >= g_inmmdevices_count))
2110 return NULL;
2111 if(is_out)
2112 return read_map(g_out_map, *out);
2113 return read_map(g_in_map, *out);
2114 case MIXER_OBJECTF_WAVEOUT:
2115 *out = HandleToULong(hmix);
2116 if(*out < g_outmmdevices_count)
2117 return read_map(g_out_map, *out);
2118 return NULL;
2119 case MIXER_OBJECTF_WAVEIN:
2120 *out = HandleToULong(hmix);
2121 if(*out < g_inmmdevices_count)
2122 return read_map(g_in_map, *out);
2123 return NULL;
2126 return NULL;
2129 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2131 IAudioSessionManager *sesman;
2132 IMMDevice *device;
2133 HRESULT hr;
2135 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2136 if(FAILED(hr)){
2137 WARN("Device %s (%s) unavailable: %08x\n",
2138 wine_dbgstr_w(mmdevice->dev_id),
2139 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2140 return MMSYSERR_ERROR;
2143 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2144 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2145 if(FAILED(hr)){
2146 WARN("Activate failed: %08x\n", hr);
2147 IMMDevice_Release(device);
2148 return MMSYSERR_ERROR;
2151 IMMDevice_Release(device);
2153 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2154 FALSE, &mmdevice->volume);
2155 IAudioSessionManager_Release(sesman);
2156 if(FAILED(hr)){
2157 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2158 return MMSYSERR_ERROR;
2161 return MMSYSERR_NOERROR;
2164 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2166 WINMM_MMDevice *mmdevice;
2167 MIXERCONTROLDETAILS *control = details->details;
2168 HRESULT hr;
2170 TRACE("(%p)\n", details->hmix);
2172 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2173 if(!mmdevice)
2174 return MMSYSERR_INVALHANDLE;
2176 EnterCriticalSection(&mmdevice->lock);
2178 if(!mmdevice->volume){
2179 MMRESULT mr;
2181 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2182 if(mr != MMSYSERR_NOERROR){
2183 LeaveCriticalSection(&mmdevice->lock);
2184 return mr;
2188 if(control->dwControlID == 0){
2189 float vol;
2190 MIXERCONTROLDETAILS_UNSIGNED *udet;
2192 if(!control->paDetails ||
2193 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2194 LeaveCriticalSection(&mmdevice->lock);
2195 return MMSYSERR_INVALPARAM;
2198 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2199 if(FAILED(hr)){
2200 WARN("GetMasterVolume failed: %08x\n", hr);
2201 LeaveCriticalSection(&mmdevice->lock);
2202 return MMSYSERR_ERROR;
2205 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2206 udet->dwValue = vol * ((unsigned int)0xFFFF);
2207 }else if(control->dwControlID == 1){
2208 BOOL mute;
2209 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2211 if(!control->paDetails ||
2212 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2213 LeaveCriticalSection(&mmdevice->lock);
2214 return MMSYSERR_INVALPARAM;
2217 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2218 if(FAILED(hr)){
2219 WARN("GetMute failed: %08x\n", hr);
2220 LeaveCriticalSection(&mmdevice->lock);
2221 return MMSYSERR_ERROR;
2224 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2225 bdet->fValue = mute;
2226 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2227 FIXME("What should the sw-side mixer controls map to?\n");
2228 }else{
2229 LeaveCriticalSection(&mmdevice->lock);
2230 return MIXERR_INVALCONTROL;
2233 LeaveCriticalSection(&mmdevice->lock);
2235 return MMSYSERR_NOERROR;
2238 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2240 WINMM_MMDevice *mmdevice;
2241 MIXERCONTROLDETAILS *control = details->details;
2242 HRESULT hr;
2244 TRACE("(%p)\n", details->hmix);
2246 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2247 if(!mmdevice)
2248 return MMSYSERR_INVALHANDLE;
2250 EnterCriticalSection(&mmdevice->lock);
2252 if(!mmdevice->volume){
2253 MMRESULT mr;
2255 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2256 if(mr != MMSYSERR_NOERROR){
2257 LeaveCriticalSection(&mmdevice->lock);
2258 return mr;
2262 if(control->dwControlID == 0){
2263 float vol;
2264 MIXERCONTROLDETAILS_UNSIGNED *udet;
2266 if(!control->paDetails ||
2267 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2268 LeaveCriticalSection(&mmdevice->lock);
2269 return MMSYSERR_INVALPARAM;
2272 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2274 if(udet->dwValue > 65535){
2275 LeaveCriticalSection(&mmdevice->lock);
2276 return MMSYSERR_INVALPARAM;
2279 vol = udet->dwValue / 65535.f;
2281 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2282 if(FAILED(hr)){
2283 WARN("SetMasterVolume failed: %08x\n", hr);
2284 LeaveCriticalSection(&mmdevice->lock);
2285 return MMSYSERR_ERROR;
2287 }else if(control->dwControlID == 1){
2288 BOOL mute;
2289 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2291 if(!control->paDetails ||
2292 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2293 LeaveCriticalSection(&mmdevice->lock);
2294 return MMSYSERR_INVALPARAM;
2297 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2298 mute = bdet->fValue;
2300 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2301 if(FAILED(hr)){
2302 WARN("SetMute failed: %08x\n", hr);
2303 LeaveCriticalSection(&mmdevice->lock);
2304 return MMSYSERR_ERROR;
2306 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2307 FIXME("What should the sw-side mixer controls map to?\n");
2308 }else{
2309 LeaveCriticalSection(&mmdevice->lock);
2310 return MIXERR_INVALCONTROL;
2313 LeaveCriticalSection(&mmdevice->lock);
2315 return MMSYSERR_NOERROR;
2318 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2320 WINMM_MMDevice *mmdevice;
2321 IMMDevice *device;
2322 IPropertyStore *ps;
2323 PROPVARIANT pv;
2324 DWORD len_bytes;
2325 HRESULT hr;
2327 static const PROPERTYKEY deviceinterface_key = {
2328 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2331 if(WINMM_IsMapper(info->index)){
2332 if(info->str){
2333 if(*info->len_bytes < sizeof(WCHAR))
2334 return MMSYSERR_INVALPARAM;
2335 *info->str = 0;
2336 }else
2337 *info->len_bytes = sizeof(WCHAR);
2338 return MMSYSERR_NOERROR;
2341 if(info->is_out){
2342 if(info->index >= g_outmmdevices_count)
2343 return MMSYSERR_INVALHANDLE;
2345 mmdevice = &g_out_mmdevices[info->index];
2346 }else{
2347 if(info->index >= g_inmmdevices_count)
2348 return MMSYSERR_INVALHANDLE;
2350 mmdevice = &g_in_mmdevices[info->index];
2353 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2354 &device);
2355 if(FAILED(hr)){
2356 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2357 return MMSYSERR_ERROR;
2360 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2361 if(FAILED(hr)){
2362 WARN("OpenPropertyStore failed: %08x\n", hr);
2363 IMMDevice_Release(device);
2364 return MMSYSERR_ERROR;
2367 PropVariantInit(&pv);
2368 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2369 if(FAILED(hr)){
2370 WARN("GetValue failed: %08x\n", hr);
2371 IPropertyStore_Release(ps);
2372 IMMDevice_Release(device);
2373 return MMSYSERR_ERROR;
2375 if(pv.vt != VT_LPWSTR){
2376 WARN("Got unexpected property type: %u\n", pv.vt);
2377 PropVariantClear(&pv);
2378 IPropertyStore_Release(ps);
2379 IMMDevice_Release(device);
2380 return MMSYSERR_ERROR;
2383 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2385 if(info->str){
2386 if(len_bytes > *info->len_bytes){
2387 PropVariantClear(&pv);
2388 IPropertyStore_Release(ps);
2389 IMMDevice_Release(device);
2390 return MMSYSERR_INVALPARAM;
2393 memcpy(info->str, pv.u.pwszVal, len_bytes);
2394 }else
2395 *info->len_bytes = len_bytes;
2397 PropVariantClear(&pv);
2398 IPropertyStore_Release(ps);
2399 IMMDevice_Release(device);
2401 return MMSYSERR_NOERROR;
2404 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2405 LPARAM lparam)
2407 switch(msg){
2408 case WODM_OPEN:
2409 return WOD_Open((WINMM_OpenInfo*)wparam);
2410 case WODM_CLOSE:
2411 return WOD_Close((HWAVEOUT)wparam);
2412 case WIDM_OPEN:
2413 return WID_Open((WINMM_OpenInfo*)wparam);
2414 case WIDM_CLOSE:
2415 return WID_Close((HWAVEIN)wparam);
2416 case MXDM_GETCONTROLDETAILS:
2417 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2418 case MXDM_SETCONTROLDETAILS:
2419 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2420 case DRV_QUERYDEVICEINTERFACESIZE:
2421 case DRV_QUERYDEVICEINTERFACE:
2422 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2424 return DefWindowProcW(hwnd, msg, wparam, lparam);
2427 static BOOL WINMM_DevicesThreadDone(void)
2429 UINT i;
2431 EnterCriticalSection(&g_devthread_lock);
2433 if(g_devthread_token > 0){
2434 LeaveCriticalSection(&g_devthread_lock);
2435 return FALSE;
2438 for(i = 0; i < g_devhandle_count; ++i){
2439 if(g_handle_devices[i]->open){
2440 LeaveCriticalSection(&g_devthread_lock);
2441 return FALSE;
2445 DestroyWindow(g_devices_hwnd);
2446 g_devices_hwnd = NULL;
2447 IMMDeviceEnumerator_Release(g_devenum);
2448 g_devenum = NULL;
2449 CoUninitialize();
2451 LeaveCriticalSection(&g_devthread_lock);
2453 return TRUE;
2456 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2458 HANDLE evt = arg;
2459 HRESULT hr;
2460 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2462 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2463 if(FAILED(hr)){
2464 WARN("CoInitializeEx failed: %08x\n", hr);
2465 FreeLibraryAndExitThread(g_devthread_module, 1);
2468 hr = WINMM_InitMMDevices();
2469 if(FAILED(hr)){
2470 CoUninitialize();
2471 FreeLibraryAndExitThread(g_devthread_module, 1);
2474 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2475 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2476 if(FAILED(hr)){
2477 WARN("CoCreateInstance failed: %08x\n", hr);
2478 CoUninitialize();
2479 FreeLibraryAndExitThread(g_devthread_module, 1);
2482 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2483 HWND_MESSAGE, NULL, NULL, NULL);
2484 if(!g_devices_hwnd){
2485 WARN("CreateWindow failed: %d\n", GetLastError());
2486 CoUninitialize();
2487 FreeLibraryAndExitThread(g_devthread_module, 1);
2490 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2491 (LONG_PTR)WINMM_DevicesMsgProc);
2493 /* inform caller that the thread is ready to process messages */
2494 SetEvent(evt);
2495 evt = NULL; /* do not use after this point */
2497 while(1){
2498 DWORD wait;
2499 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2500 FALSE, INFINITE, QS_ALLINPUT);
2501 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2502 MSG msg;
2503 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2504 WARN("Unexpected message: 0x%x\n", msg.message);
2505 if(!g_devices_hwnd)
2506 break;
2507 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2508 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2509 if(device->render)
2510 WOD_PushData(device);
2511 else
2512 WID_PullData(device);
2513 }else
2514 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2515 GetLastError());
2516 if(WINMM_DevicesThreadDone()){
2517 TRACE("Quitting devices thread\n");
2518 FreeLibraryAndExitThread(g_devthread_module, 0);
2522 FreeLibraryAndExitThread(g_devthread_module, 0);
2525 /* on success, increments g_devthread_token to prevent
2526 * device thread shutdown. caller must decrement. */
2527 static BOOL WINMM_StartDevicesThread(void)
2529 HANDLE events[2];
2530 DWORD wait;
2532 EnterCriticalSection(&g_devthread_lock);
2534 if(g_devices_hwnd){
2535 wait = WaitForSingleObject(g_devices_thread, 0);
2536 if(wait == WAIT_TIMEOUT){
2537 /* thread still running */
2538 InterlockedIncrement(&g_devthread_token);
2539 LeaveCriticalSection(&g_devthread_lock);
2540 return TRUE;
2542 if(wait != WAIT_OBJECT_0){
2543 /* error */
2544 LeaveCriticalSection(&g_devthread_lock);
2545 return FALSE;
2547 TRACE("Devices thread left dangling message window?\n");
2548 g_devices_hwnd = NULL;
2549 CloseHandle(g_devices_thread);
2550 g_devices_thread = NULL;
2551 }else if(g_devices_thread){
2552 WaitForSingleObject(g_devices_thread, INFINITE);
2553 CloseHandle(g_devices_thread);
2554 g_devices_thread = NULL;
2557 TRACE("Starting up devices thread\n");
2559 /* The devices thread holds a reference to the winmm module
2560 * to prevent it from unloading while it's running. */
2561 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2562 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2564 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2566 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2567 events[0], 0, NULL);
2568 if(!g_devices_thread){
2569 LeaveCriticalSection(&g_devthread_lock);
2570 CloseHandle(events[0]);
2571 FreeLibrary(g_devthread_module);
2572 return FALSE;
2575 events[1] = g_devices_thread;
2576 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2577 CloseHandle(events[0]);
2578 if(wait != WAIT_OBJECT_0){
2579 if(wait == 1 + WAIT_OBJECT_0){
2580 CloseHandle(g_devices_thread);
2581 g_devices_thread = NULL;
2582 g_devices_hwnd = NULL;
2584 LeaveCriticalSection(&g_devthread_lock);
2585 return FALSE;
2588 InterlockedIncrement(&g_devthread_token);
2590 LeaveCriticalSection(&g_devthread_lock);
2592 return TRUE;
2595 /**************************************************************************
2596 * waveOutGetNumDevs [WINMM.@]
2598 UINT WINAPI waveOutGetNumDevs(void)
2600 HRESULT hr = WINMM_InitMMDevices();
2601 if(FAILED(hr))
2602 return 0;
2604 TRACE("count: %u\n", g_outmmdevices_count);
2606 return g_outmmdevices_count;
2609 /**************************************************************************
2610 * waveOutGetDevCapsA [WINMM.@]
2612 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2613 UINT uSize)
2615 WAVEOUTCAPSW wocW;
2616 UINT ret;
2618 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2620 if(!lpCaps)
2621 return MMSYSERR_INVALPARAM;
2623 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2625 if (ret == MMSYSERR_NOERROR) {
2626 WAVEOUTCAPSA wocA;
2627 wocA.wMid = wocW.wMid;
2628 wocA.wPid = wocW.wPid;
2629 wocA.vDriverVersion = wocW.vDriverVersion;
2630 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2631 sizeof(wocA.szPname), NULL, NULL );
2632 wocA.dwFormats = wocW.dwFormats;
2633 wocA.wChannels = wocW.wChannels;
2634 wocA.dwSupport = wocW.dwSupport;
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 /* FIXME: Should be localized */
2659 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2660 'n','d',' ','M','a','p','p','e','r',0};
2662 mapper_caps.wMid = 0xFF;
2663 mapper_caps.wPid = 0xFF;
2664 mapper_caps.vDriverVersion = 0x00010001;
2665 mapper_caps.dwFormats = 0xFFFFFFFF;
2666 mapper_caps.wReserved1 = 0;
2667 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2668 WAVECAPS_SAMPLEACCURATE;
2669 mapper_caps.wChannels = 2;
2670 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2672 caps = &mapper_caps;
2673 }else{
2674 if(uDeviceID >= g_outmmdevices_count)
2675 return MMSYSERR_BADDEVICEID;
2677 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2680 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2682 return MMSYSERR_NOERROR;
2685 /**************************************************************************
2686 * waveOutGetErrorTextA [WINMM.@]
2687 * waveInGetErrorTextA [WINMM.@]
2689 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2691 UINT ret;
2693 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2694 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2695 else
2697 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2698 if (!xstr) ret = MMSYSERR_NOMEM;
2699 else
2701 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2702 if (ret == MMSYSERR_NOERROR)
2703 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2704 HeapFree(GetProcessHeap(), 0, xstr);
2707 return ret;
2710 /**************************************************************************
2711 * waveOutGetErrorTextW [WINMM.@]
2712 * waveInGetErrorTextW [WINMM.@]
2714 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2716 UINT ret = MMSYSERR_BADERRNUM;
2718 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2719 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2720 else if (
2721 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2722 * a warning for the test was always true */
2723 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2724 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2725 if (LoadStringW(hWinMM32Instance,
2726 uError, lpText, uSize) > 0) {
2727 ret = MMSYSERR_NOERROR;
2730 return ret;
2733 /**************************************************************************
2734 * waveOutOpen [WINMM.@]
2735 * All the args/structs have the same layout as the win16 equivalents
2737 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2738 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2739 DWORD_PTR dwInstance, DWORD dwFlags)
2741 LRESULT res;
2742 WINMM_OpenInfo info;
2743 WINMM_CBInfo cb_info;
2745 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2746 dwCallback, dwInstance, dwFlags);
2748 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2749 return MMSYSERR_INVALPARAM;
2751 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2752 if(res != MMSYSERR_NOERROR)
2753 return res;
2755 if(!WINMM_StartDevicesThread())
2756 return MMSYSERR_NODRIVER;
2758 info.handle = 0;
2759 info.format = (WAVEFORMATEX*)lpFormat;
2760 info.callback = dwCallback;
2761 info.cb_user = dwInstance;
2762 info.req_device = uDeviceID;
2763 info.flags = dwFlags;
2764 info.reset = TRUE;
2766 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2767 InterlockedDecrement(&g_devthread_token);
2768 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2769 return res;
2771 if(lphWaveOut)
2772 *lphWaveOut = (HWAVEOUT)info.handle;
2774 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2775 cb_info.callback = dwCallback;
2776 cb_info.user = dwInstance;
2777 cb_info.hwave = info.handle;
2779 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2781 return res;
2784 /**************************************************************************
2785 * waveOutClose [WINMM.@]
2787 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2789 UINT res;
2790 WINMM_Device *device;
2791 WINMM_CBInfo cb_info;
2793 TRACE("(%p)\n", hWaveOut);
2795 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2797 if(!WINMM_ValidateAndLock(device))
2798 return MMSYSERR_INVALHANDLE;
2800 cb_info = device->cb_info;
2802 LeaveCriticalSection(&device->lock);
2804 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2806 if(res == MMSYSERR_NOERROR)
2807 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2809 return res;
2812 /**************************************************************************
2813 * waveOutPrepareHeader [WINMM.@]
2815 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2816 WAVEHDR* lpWaveOutHdr, UINT uSize)
2818 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2820 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2821 return MMSYSERR_INVALPARAM;
2823 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2824 return MMSYSERR_NOERROR;
2826 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2829 /**************************************************************************
2830 * waveOutUnprepareHeader [WINMM.@]
2832 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2833 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2835 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2837 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2838 return MMSYSERR_INVALPARAM;
2840 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2841 return WAVERR_STILLPLAYING;
2843 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2844 return MMSYSERR_NOERROR;
2846 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2849 /**************************************************************************
2850 * waveOutWrite [WINMM.@]
2852 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2854 WINMM_Device *device;
2855 MMRESULT mr;
2857 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2859 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2861 if(!WINMM_ValidateAndLock(device))
2862 return MMSYSERR_INVALHANDLE;
2864 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2865 LeaveCriticalSection(&device->lock);
2866 return WAVERR_UNPREPARED;
2869 if(header->dwFlags & WHDR_INQUEUE){
2870 LeaveCriticalSection(&device->lock);
2871 return WAVERR_STILLPLAYING;
2874 TRACE("dwBufferLength: %u\n", header->dwBufferLength);
2876 if(device->acm_handle){
2877 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2879 ash->cbSrcLength = header->dwBufferLength;
2880 mr = acmStreamConvert(device->acm_handle, ash, 0);
2881 if(mr != MMSYSERR_NOERROR){
2882 LeaveCriticalSection(&device->lock);
2883 return mr;
2887 if(device->first){
2888 device->last->lpNext = header;
2889 device->last = header;
2890 if(!device->playing)
2891 device->playing = header;
2892 }else{
2893 device->playing = device->first = device->last = header;
2894 if(header->dwFlags & WHDR_BEGINLOOP){
2895 device->loop_counter = header->dwLoops;
2896 device->loop_start = header;
2900 header->lpNext = NULL;
2901 header->dwFlags &= ~WHDR_DONE;
2902 header->dwFlags |= WHDR_INQUEUE;
2904 mr = WINMM_BeginPlaying(device);
2906 LeaveCriticalSection(&device->lock);
2908 return mr;
2911 /**************************************************************************
2912 * waveOutBreakLoop [WINMM.@]
2914 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2916 WINMM_Device *device;
2918 TRACE("(%p)\n", hWaveOut);
2920 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2922 if(!WINMM_ValidateAndLock(device))
2923 return MMSYSERR_INVALHANDLE;
2925 device->loop_counter = 0;
2927 LeaveCriticalSection(&device->lock);
2929 return MMSYSERR_NOERROR;
2932 /**************************************************************************
2933 * waveOutPause [WINMM.@]
2935 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2937 TRACE("(%p)\n", hWaveOut);
2939 return WINMM_Pause((HWAVE)hWaveOut);
2942 /**************************************************************************
2943 * waveOutReset [WINMM.@]
2945 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2947 TRACE("(%p)\n", hWaveOut);
2949 return WINMM_Reset((HWAVE)hWaveOut);
2952 /**************************************************************************
2953 * waveOutRestart [WINMM.@]
2955 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2957 WINMM_Device *device;
2958 MMRESULT mr;
2960 TRACE("(%p)\n", hWaveOut);
2962 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2964 if(!WINMM_ValidateAndLock(device))
2965 return MMSYSERR_INVALHANDLE;
2967 device->stopped = TRUE;
2969 mr = WINMM_BeginPlaying(device);
2971 LeaveCriticalSection(&device->lock);
2973 return mr;
2976 /**************************************************************************
2977 * waveOutGetPosition [WINMM.@]
2979 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2980 UINT uSize)
2982 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2984 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
2985 return MMSYSERR_INVALPARAM;
2987 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2990 /**************************************************************************
2991 * waveOutGetPitch [WINMM.@]
2993 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2995 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2996 return MMSYSERR_NOTSUPPORTED;
2999 /**************************************************************************
3000 * waveOutSetPitch [WINMM.@]
3002 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3004 TRACE("(%p, %08x)\n", hWaveOut, dw);
3006 return MMSYSERR_NOTSUPPORTED;
3009 /**************************************************************************
3010 * waveOutGetPlaybackRate [WINMM.@]
3012 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3014 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3016 return MMSYSERR_NOTSUPPORTED;
3019 /**************************************************************************
3020 * waveOutSetPlaybackRate [WINMM.@]
3022 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3024 TRACE("(%p, %08x)\n", hWaveOut, dw);
3026 return MMSYSERR_NOTSUPPORTED;
3029 /**************************************************************************
3030 * waveOutGetVolume [WINMM.@]
3032 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3034 WINMM_Device *device;
3035 UINT32 channels;
3036 float *vols;
3037 HRESULT hr;
3039 TRACE("(%p, %p)\n", hWaveOut, out);
3041 if(!out)
3042 return MMSYSERR_INVALPARAM;
3044 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3046 if(!WINMM_ValidateAndLock(device))
3047 return MMSYSERR_INVALHANDLE;
3049 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3050 if(FAILED(hr)){
3051 LeaveCriticalSection(&device->lock);
3052 WARN("GetChannelCount failed: %08x\n", hr);
3053 return MMSYSERR_ERROR;
3056 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3057 if(!vols){
3058 LeaveCriticalSection(&device->lock);
3059 return MMSYSERR_NOMEM;
3062 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3063 if(FAILED(hr)){
3064 LeaveCriticalSection(&device->lock);
3065 HeapFree(GetProcessHeap(), 0, vols);
3066 WARN("GetAllVolumes failed: %08x\n", hr);
3067 return MMSYSERR_ERROR;
3070 LeaveCriticalSection(&device->lock);
3072 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3073 if(channels > 1)
3074 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3076 HeapFree(GetProcessHeap(), 0, vols);
3078 return MMSYSERR_NOERROR;
3081 /**************************************************************************
3082 * waveOutSetVolume [WINMM.@]
3084 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3086 WINMM_Device *device;
3087 UINT32 channels;
3088 float *vols;
3089 HRESULT hr;
3091 TRACE("(%p, %08x)\n", hWaveOut, in);
3093 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3095 if(!WINMM_ValidateAndLock(device))
3096 return MMSYSERR_INVALHANDLE;
3098 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3099 if(FAILED(hr)){
3100 LeaveCriticalSection(&device->lock);
3101 WARN("GetChannelCount failed: %08x\n", hr);
3102 return MMSYSERR_ERROR;
3105 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3106 if(!vols){
3107 LeaveCriticalSection(&device->lock);
3108 return MMSYSERR_NOMEM;
3111 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3112 if(FAILED(hr)){
3113 LeaveCriticalSection(&device->lock);
3114 HeapFree(GetProcessHeap(), 0, vols);
3115 WARN("GetAllVolumes failed: %08x\n", hr);
3116 return MMSYSERR_ERROR;
3119 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
3120 if(channels > 1)
3121 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
3123 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3124 if(FAILED(hr)){
3125 LeaveCriticalSection(&device->lock);
3126 HeapFree(GetProcessHeap(), 0, vols);
3127 WARN("SetAllVolumes failed: %08x\n", hr);
3128 return MMSYSERR_ERROR;
3131 LeaveCriticalSection(&device->lock);
3133 HeapFree(GetProcessHeap(), 0, vols);
3135 return MMSYSERR_NOERROR;
3138 /**************************************************************************
3139 * waveOutGetID [WINMM.@]
3141 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3143 WINMM_Device *device;
3144 UINT dev, junk;
3145 BOOL is_out;
3147 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3149 if(!lpuDeviceID)
3150 return MMSYSERR_INVALPARAM;
3152 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3153 if(!WINMM_ValidateAndLock(device))
3154 return MMSYSERR_INVALHANDLE;
3156 LeaveCriticalSection(&device->lock);
3158 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3160 return MMSYSERR_NOERROR;
3163 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3165 UINT count;
3166 WINMM_MMDevice **devices;
3168 TRACE("(%u, %p, %d)\n", device, len, is_out);
3170 if(is_out){
3171 count = g_outmmdevices_count;
3172 devices = g_out_map;
3173 }else{
3174 count = g_inmmdevices_count;
3175 devices = g_in_map;
3178 if(device >= count)
3179 return MMSYSERR_INVALHANDLE;
3181 EnterCriticalSection(&g_devthread_lock);
3182 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3183 LeaveCriticalSection(&g_devthread_lock);
3185 return MMSYSERR_NOERROR;
3188 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3189 BOOL is_out)
3191 UINT count, id_len;
3192 WINMM_MMDevice **devices;
3194 TRACE("(%u, %p, %d)\n", device, str, is_out);
3196 if(is_out){
3197 count = g_outmmdevices_count;
3198 devices = g_out_map;
3199 }else{
3200 count = g_inmmdevices_count;
3201 devices = g_in_map;
3204 if(device >= count)
3205 return MMSYSERR_INVALHANDLE;
3207 EnterCriticalSection(&g_devthread_lock);
3208 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3209 if(len < id_len){
3210 LeaveCriticalSection(&g_devthread_lock);
3211 return MMSYSERR_ERROR;
3214 memcpy(str, devices[device]->dev_id, id_len);
3215 LeaveCriticalSection(&g_devthread_lock);
3217 return MMSYSERR_NOERROR;
3220 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3222 WINMM_QueryInterfaceInfo info;
3223 UINT ret;
3225 if(!WINMM_StartDevicesThread())
3226 return MMSYSERR_NODRIVER;
3228 info.is_out = is_out;
3229 info.index = index;
3230 info.str = out;
3231 info.len_bytes = out_len;
3233 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3234 InterlockedDecrement(&g_devthread_token);
3235 return ret;
3238 /**************************************************************************
3239 * waveOutMessage [WINMM.@]
3241 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3242 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3244 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3246 switch(uMessage){
3247 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3248 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3249 (DWORD_PTR*)dwParam1, TRUE);
3250 case DRV_QUERYFUNCTIONINSTANCEID:
3251 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3252 case DRV_QUERYDEVICEINTERFACESIZE:
3253 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3254 NULL, (ULONG*)dwParam1);
3255 case DRV_QUERYDEVICEINTERFACE:
3257 ULONG size = dwParam2;
3258 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3259 (WCHAR*)dwParam1, &size);
3261 case DRV_QUERYMAPPABLE:
3262 return MMSYSERR_NOERROR;
3263 case DRVM_MAPPER_PREFERRED_GET:
3264 if(dwParam1) {
3265 if(g_outmmdevices_count > 0)
3266 /* Device 0 is always the default device */
3267 *(DWORD *)dwParam1 = 0;
3268 else
3269 *(DWORD *)dwParam1 = -1;
3272 if(dwParam2)
3273 /* Status flags */
3274 *(DWORD *)dwParam2 = 0;
3276 return MMSYSERR_NOERROR;
3279 TRACE("Message not supported: %u\n", uMessage);
3281 return MMSYSERR_NOTSUPPORTED;
3284 /**************************************************************************
3285 * waveInGetNumDevs [WINMM.@]
3287 UINT WINAPI waveInGetNumDevs(void)
3289 HRESULT hr = WINMM_InitMMDevices();
3290 if(FAILED(hr))
3291 return 0;
3293 TRACE("count: %u\n", g_inmmdevices_count);
3295 return g_inmmdevices_count;
3298 /**************************************************************************
3299 * waveInGetDevCapsW [WINMM.@]
3301 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3303 WAVEINCAPSW mapper_caps, *caps;
3304 HRESULT hr;
3306 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3308 hr = WINMM_InitMMDevices();
3309 if(FAILED(hr))
3310 return MMSYSERR_NODRIVER;
3312 if(!lpCaps)
3313 return MMSYSERR_INVALPARAM;
3315 if(WINMM_IsMapper(uDeviceID)){
3316 /* FIXME: Should be localized */
3317 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
3318 'n','d',' ','M','a','p','p','e','r',0};
3320 mapper_caps.wMid = 0xFF;
3321 mapper_caps.wPid = 0xFF;
3322 mapper_caps.vDriverVersion = 0x00010001;
3323 mapper_caps.dwFormats = 0xFFFFFFFF;
3324 mapper_caps.wReserved1 = 0;
3325 mapper_caps.wChannels = 2;
3326 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
3328 caps = &mapper_caps;
3329 }else{
3330 if(uDeviceID >= g_inmmdevices_count)
3331 return MMSYSERR_BADDEVICEID;
3333 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3336 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3338 return MMSYSERR_NOERROR;
3341 /**************************************************************************
3342 * waveInGetDevCapsA [WINMM.@]
3344 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3346 UINT ret;
3347 WAVEINCAPSW wicW;
3349 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3351 if(!lpCaps)
3352 return MMSYSERR_INVALPARAM;
3354 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3356 if (ret == MMSYSERR_NOERROR) {
3357 WAVEINCAPSA wicA;
3358 wicA.wMid = wicW.wMid;
3359 wicA.wPid = wicW.wPid;
3360 wicA.vDriverVersion = wicW.vDriverVersion;
3361 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3362 sizeof(wicA.szPname), NULL, NULL );
3363 wicA.dwFormats = wicW.dwFormats;
3364 wicA.wChannels = wicW.wChannels;
3365 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3367 return ret;
3370 /**************************************************************************
3371 * waveInOpen [WINMM.@]
3373 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3374 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3375 DWORD_PTR dwInstance, DWORD dwFlags)
3377 LRESULT res;
3378 WINMM_OpenInfo info;
3379 WINMM_CBInfo cb_info;
3381 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3382 dwCallback, dwInstance, dwFlags);
3384 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3385 return MMSYSERR_INVALPARAM;
3387 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3388 if(res != MMSYSERR_NOERROR)
3389 return res;
3391 if(!WINMM_StartDevicesThread())
3392 return MMSYSERR_NODRIVER;
3394 info.handle = 0;
3395 info.format = (WAVEFORMATEX*)lpFormat;
3396 info.callback = dwCallback;
3397 info.cb_user = dwInstance;
3398 info.req_device = uDeviceID;
3399 info.flags = dwFlags;
3400 info.reset = TRUE;
3402 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3403 InterlockedDecrement(&g_devthread_token);
3404 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3405 return res;
3407 if(lphWaveIn)
3408 *lphWaveIn = (HWAVEIN)info.handle;
3410 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3411 cb_info.callback = dwCallback;
3412 cb_info.user = dwInstance;
3413 cb_info.hwave = info.handle;
3415 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3417 return res;
3420 /**************************************************************************
3421 * waveInClose [WINMM.@]
3423 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3425 WINMM_Device *device;
3426 WINMM_CBInfo cb_info;
3427 UINT res;
3429 TRACE("(%p)\n", hWaveIn);
3431 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3433 if(!WINMM_ValidateAndLock(device))
3434 return MMSYSERR_INVALHANDLE;
3436 cb_info = device->cb_info;
3438 LeaveCriticalSection(&device->lock);
3440 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3442 if(res == MMSYSERR_NOERROR)
3443 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3445 return res;
3448 /**************************************************************************
3449 * waveInPrepareHeader [WINMM.@]
3451 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3452 UINT uSize)
3454 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3456 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3457 return MMSYSERR_INVALPARAM;
3459 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3460 return MMSYSERR_NOERROR;
3462 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3465 /**************************************************************************
3466 * waveInUnprepareHeader [WINMM.@]
3468 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3469 UINT uSize)
3471 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3473 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3474 return MMSYSERR_INVALPARAM;
3476 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3477 return WAVERR_STILLPLAYING;
3479 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3480 return MMSYSERR_NOERROR;
3482 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3485 /**************************************************************************
3486 * waveInAddBuffer [WINMM.@]
3488 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3490 WINMM_Device *device;
3492 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3494 if(!header || uSize < sizeof(WAVEHDR))
3495 return MMSYSERR_INVALPARAM;
3497 if(!(header->dwFlags & WHDR_PREPARED))
3498 return WAVERR_UNPREPARED;
3500 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3502 if(!WINMM_ValidateAndLock(device))
3503 return MMSYSERR_INVALHANDLE;
3505 if(!device->first)
3506 device->first = device->last = header;
3507 else{
3508 device->last->lpNext = header;
3509 device->last = header;
3512 header->dwBytesRecorded = 0;
3513 header->lpNext = NULL;
3514 header->dwFlags &= ~WHDR_DONE;
3515 header->dwFlags |= WHDR_INQUEUE;
3517 LeaveCriticalSection(&device->lock);
3519 return MMSYSERR_NOERROR;
3522 /**************************************************************************
3523 * waveInReset [WINMM.@]
3525 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3527 TRACE("(%p)\n", hWaveIn);
3529 return WINMM_Reset((HWAVE)hWaveIn);
3532 /**************************************************************************
3533 * waveInStart [WINMM.@]
3535 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3537 WINMM_Device *device;
3538 MMRESULT mr;
3540 TRACE("(%p)\n", hWaveIn);
3542 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3544 if(!WINMM_ValidateAndLock(device))
3545 return MMSYSERR_INVALHANDLE;
3547 mr = WINMM_BeginPlaying(device);
3549 LeaveCriticalSection(&device->lock);
3551 return mr;
3554 /**************************************************************************
3555 * waveInStop [WINMM.@]
3557 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3559 WINMM_CBInfo cb_info;
3560 WINMM_Device *device;
3561 WAVEHDR *buf;
3562 HRESULT hr;
3564 TRACE("(%p)\n", hWaveIn);
3566 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3568 if(!WINMM_ValidateAndLock(device))
3569 return MMSYSERR_INVALHANDLE;
3571 hr = WINMM_Pause((HWAVE)hWaveIn);
3572 if(FAILED(hr)){
3573 LeaveCriticalSection(&device->lock);
3574 return MMSYSERR_ERROR;
3576 device->stopped = TRUE;
3578 buf = device->first;
3579 if(buf && buf->dwBytesRecorded > 0){
3580 device->first = buf->lpNext;
3581 }else
3582 buf = NULL;
3584 cb_info = device->cb_info;
3586 LeaveCriticalSection(&device->lock);
3588 if(buf){
3589 buf->dwFlags &= ~WHDR_INQUEUE;
3590 buf->dwFlags |= WHDR_DONE;
3591 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3594 return MMSYSERR_NOERROR;
3597 /**************************************************************************
3598 * waveInGetPosition [WINMM.@]
3600 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3601 UINT uSize)
3603 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3605 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
3606 return MMSYSERR_INVALPARAM;
3608 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3611 /**************************************************************************
3612 * waveInGetID [WINMM.@]
3614 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3616 UINT dev, junk;
3617 BOOL is_out;
3618 WINMM_Device *device;
3620 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3622 if(!lpuDeviceID)
3623 return MMSYSERR_INVALPARAM;
3625 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3626 if(!WINMM_ValidateAndLock(device))
3627 return MMSYSERR_INVALHANDLE;
3629 LeaveCriticalSection(&device->lock);
3631 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3633 return MMSYSERR_NOERROR;
3636 /**************************************************************************
3637 * waveInMessage [WINMM.@]
3639 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3640 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3642 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3644 switch(uMessage){
3645 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3646 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3647 (DWORD_PTR*)dwParam1, FALSE);
3648 case DRV_QUERYFUNCTIONINSTANCEID:
3649 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3650 case DRV_QUERYDEVICEINTERFACESIZE:
3651 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3652 NULL, (ULONG*)dwParam1);
3653 case DRV_QUERYDEVICEINTERFACE:
3655 ULONG size = dwParam2;
3656 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3657 (WCHAR*)dwParam1, &size);
3659 case DRV_QUERYMAPPABLE:
3660 return MMSYSERR_NOERROR;
3661 case DRVM_MAPPER_PREFERRED_GET:
3662 if(dwParam1) {
3663 if(g_inmmdevices_count > 0)
3664 /* Device 0 is always the default device */
3665 *(DWORD *)dwParam1 = 0;
3666 else
3667 *(DWORD *)dwParam1 = -1;
3670 if(dwParam2)
3671 /* Status flags */
3672 *(DWORD *)dwParam2 = 0;
3674 return MMSYSERR_NOERROR;
3677 TRACE("Message not supported: %u\n", uMessage);
3679 return MMSYSERR_NOTSUPPORTED;
3682 UINT WINAPI mixerGetNumDevs(void)
3684 HRESULT hr;
3686 TRACE("\n");
3688 hr = WINMM_InitMMDevices();
3689 if(FAILED(hr))
3690 return 0;
3692 return g_outmmdevices_count + g_inmmdevices_count;
3695 /**************************************************************************
3696 * mixerGetDevCapsA [WINMM.@]
3698 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3700 MIXERCAPSW micW;
3701 UINT ret;
3703 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3705 if(!lpCaps)
3706 return MMSYSERR_INVALPARAM;
3708 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3710 if (ret == MMSYSERR_NOERROR) {
3711 MIXERCAPSA micA;
3712 micA.wMid = micW.wMid;
3713 micA.wPid = micW.wPid;
3714 micA.vDriverVersion = micW.vDriverVersion;
3715 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3716 sizeof(micA.szPname), NULL, NULL );
3717 micA.fdwSupport = micW.fdwSupport;
3718 micA.cDestinations = micW.cDestinations;
3719 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3721 return ret;
3724 /**************************************************************************
3725 * mixerGetDevCapsW [WINMM.@]
3727 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3729 WINMM_MMDevice *mmdevice;
3730 MIXERCAPSW caps;
3731 HRESULT hr;
3733 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3735 hr = WINMM_InitMMDevices();
3736 if(FAILED(hr))
3737 return MMSYSERR_NODRIVER;
3739 if(!lpCaps)
3740 return MMSYSERR_INVALPARAM;
3742 if(!uSize)
3743 return MMSYSERR_NOERROR;
3745 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3746 return MMSYSERR_BADDEVICEID;
3748 if(uDeviceID < g_outmmdevices_count){
3749 mmdevice = read_map(g_out_map, uDeviceID);
3750 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3751 }else{
3752 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3753 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3756 caps.wMid = 0xFF;
3757 caps.wPid = 0xFF;
3758 caps.vDriverVersion = 0x00010001;
3759 caps.fdwSupport = 0;
3760 caps.cDestinations = 1;
3762 memcpy(lpCaps, &caps, uSize);
3764 return MMSYSERR_NOERROR;
3767 /**************************************************************************
3768 * mixerOpen [WINMM.@]
3770 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3771 DWORD_PTR dwInstance, DWORD fdwOpen)
3773 WINMM_MMDevice *mmdevice;
3774 MMRESULT mr;
3775 HRESULT hr;
3777 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3778 dwInstance, fdwOpen);
3780 hr = WINMM_InitMMDevices();
3781 if(FAILED(hr))
3782 return MMSYSERR_NODRIVER;
3784 if(!lphMix)
3785 return MMSYSERR_INVALPARAM;
3787 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3788 if(mr != MMSYSERR_NOERROR)
3789 return mr;
3791 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3792 return MMSYSERR_BADDEVICEID;
3794 if(uDeviceID < g_outmmdevices_count){
3795 mmdevice = read_map(g_out_map, uDeviceID);
3796 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3797 mmdevice->mixer_count);
3798 }else{
3799 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3800 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3801 FALSE, mmdevice->mixer_count);
3804 ++mmdevice->mixer_count;
3806 return MMSYSERR_NOERROR;
3809 /**************************************************************************
3810 * mixerClose [WINMM.@]
3812 UINT WINAPI mixerClose(HMIXER hMix)
3814 TRACE("(%p)\n", hMix);
3816 return MMSYSERR_NOERROR;
3819 /**************************************************************************
3820 * mixerGetID [WINMM.@]
3822 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3824 WINMM_MMDevice *mmdevice;
3825 HRESULT hr;
3827 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3829 hr = WINMM_InitMMDevices();
3830 if(FAILED(hr))
3831 return MMSYSERR_NODRIVER;
3833 if(!lpid)
3834 return MMSYSERR_INVALPARAM;
3836 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3837 if(!mmdevice)
3838 return MMSYSERR_INVALHANDLE;
3840 if(mmdevice->in_caps.szPname[0] != '\0')
3841 *lpid += g_outmmdevices_count;
3843 return MMSYSERR_NOERROR;
3846 /**************************************************************************
3847 * mixerGetControlDetailsW [WINMM.@]
3849 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3850 DWORD fdwDetails)
3852 WINMM_ControlDetails details;
3853 HRESULT hr;
3855 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3857 hr = WINMM_InitMMDevices();
3858 if(FAILED(hr))
3859 return MMSYSERR_NODRIVER;
3861 if(!lpmcdW || !lpmcdW->paDetails)
3862 return MMSYSERR_INVALPARAM;
3864 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3866 details.hmix = hmix;
3867 details.details = lpmcdW;
3868 details.flags = fdwDetails;
3870 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3871 (DWORD_PTR)&details, 0);
3874 /**************************************************************************
3875 * mixerGetControlDetailsA [WINMM.@]
3877 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3878 DWORD fdwDetails)
3880 UINT ret = MMSYSERR_NOTSUPPORTED;
3882 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3884 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3885 return MMSYSERR_INVALPARAM;
3887 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3888 case MIXER_GETCONTROLDETAILSF_VALUE:
3889 /* can safely use A structure as it is, no string inside */
3890 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3891 break;
3892 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3894 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3895 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3896 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3897 unsigned int i;
3899 if (lpmcdA->u.cMultipleItems != 0) {
3900 size *= lpmcdA->u.cMultipleItems;
3902 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3903 lpmcdA->paDetails = pDetailsW;
3904 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3905 /* set up lpmcd->paDetails */
3906 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3907 /* copy from lpmcd->paDetails back to paDetailsW; */
3908 if (ret == MMSYSERR_NOERROR) {
3909 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3910 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3911 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3912 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3913 pDetailsA->szName,
3914 sizeof(pDetailsA->szName), NULL, NULL );
3915 pDetailsA++;
3916 pDetailsW++;
3918 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3919 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3921 HeapFree(GetProcessHeap(), 0, pDetailsW);
3922 lpmcdA->paDetails = pDetailsA;
3923 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3925 break;
3926 default:
3927 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3930 return ret;
3933 /**************************************************************************
3934 * mixerGetLineControlsA [WINMM.@]
3936 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3937 DWORD fdwControls)
3939 MIXERLINECONTROLSW mlcW;
3940 DWORD ret;
3941 unsigned int i;
3943 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3945 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3946 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3947 return MMSYSERR_INVALPARAM;
3949 mlcW.cbStruct = sizeof(mlcW);
3950 mlcW.dwLineID = lpmlcA->dwLineID;
3951 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3952 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3954 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3955 the control count is assumed to be 1 - This is relied upon by a game,
3956 "Dynomite Deluze" */
3957 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3958 mlcW.cControls = 1;
3959 } else {
3960 mlcW.cControls = lpmlcA->cControls;
3962 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3963 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3964 mlcW.cControls * mlcW.cbmxctrl);
3966 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3968 if (ret == MMSYSERR_NOERROR) {
3969 lpmlcA->dwLineID = mlcW.dwLineID;
3970 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3971 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3973 for (i = 0; i < mlcW.cControls; i++) {
3974 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3975 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3976 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3977 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3978 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3979 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3980 lpmlcA->pamxctrl[i].szShortName,
3981 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3982 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3983 lpmlcA->pamxctrl[i].szName,
3984 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3985 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3986 * sizeof(mlcW.pamxctrl[i].Bounds) */
3987 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3988 sizeof(mlcW.pamxctrl[i].Bounds));
3989 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3990 * sizeof(mlcW.pamxctrl[i].Metrics) */
3991 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3992 sizeof(mlcW.pamxctrl[i].Metrics));
3996 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
3998 return ret;
4001 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4002 MIXERCONTROLW *ctl, DWORD flags)
4004 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4005 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4006 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4007 ctl->cMultipleItems = 0;
4008 lstrcpyW(ctl->szShortName, volumeW);
4009 lstrcpyW(ctl->szName, volumeW);
4010 ctl->Bounds.s1.dwMinimum = 0;
4011 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4012 ctl->Metrics.cSteps = 192;
4014 return MMSYSERR_NOERROR;
4017 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4018 MIXERCONTROLW *ctl, DWORD flags)
4020 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4021 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4022 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4023 ctl->cMultipleItems = 0;
4024 lstrcpyW(ctl->szShortName, muteW);
4025 lstrcpyW(ctl->szName, muteW);
4026 ctl->Bounds.s1.dwMinimum = 0;
4027 ctl->Bounds.s1.dwMaximum = 1;
4028 ctl->Metrics.cSteps = 0;
4030 return MMSYSERR_NOERROR;
4033 /**************************************************************************
4034 * mixerGetLineControlsW [WINMM.@]
4036 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4037 DWORD fdwControls)
4039 WINMM_MMDevice *mmdevice;
4040 HRESULT hr;
4042 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4044 hr = WINMM_InitMMDevices();
4045 if(FAILED(hr))
4046 return MMSYSERR_NODRIVER;
4048 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4049 MIXER_GETLINECONTROLSF_ONEBYID |
4050 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4051 MIXER_OBJECTF_HMIXER |
4052 MIXER_OBJECTF_MIXER)){
4053 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4054 return MMSYSERR_INVALFLAG;
4057 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4058 return MMSYSERR_INVALPARAM;
4060 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4061 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4062 TRACE("cControls: %u\n", lpmlcW->cControls);
4064 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4065 if(!mmdevice)
4066 return MMSYSERR_INVALHANDLE;
4068 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4069 case MIXER_GETLINECONTROLSF_ALL:
4070 if(lpmlcW->cControls != 2)
4071 return MMSYSERR_INVALPARAM;
4072 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4073 return MMSYSERR_INVALPARAM;
4074 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4075 return MIXERR_INVALLINE;
4076 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4077 &lpmlcW->pamxctrl[0], fdwControls);
4078 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4079 &lpmlcW->pamxctrl[1], fdwControls);
4080 return MMSYSERR_NOERROR;
4081 case MIXER_GETLINECONTROLSF_ONEBYID:
4082 if(lpmlcW->cControls != 1)
4083 return MMSYSERR_INVALPARAM;
4084 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4085 return MMSYSERR_INVALPARAM;
4086 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4087 return MIXERR_INVALLINE;
4088 if(lpmlcW->u.dwControlID == 0)
4089 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4090 lpmlcW->pamxctrl, fdwControls);
4091 if(lpmlcW->u.dwControlID == 1)
4092 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4093 lpmlcW->pamxctrl, fdwControls);
4094 return MMSYSERR_NOTSUPPORTED;
4095 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4096 if(lpmlcW->cControls != 1)
4097 return MMSYSERR_INVALPARAM;
4098 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4099 return MMSYSERR_INVALPARAM;
4100 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4101 return MIXERR_INVALLINE;
4102 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4103 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4104 lpmlcW->pamxctrl, fdwControls);
4105 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4106 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4107 lpmlcW->pamxctrl, fdwControls);
4108 return MMSYSERR_NOTSUPPORTED;
4111 return MMSYSERR_NOTSUPPORTED;
4114 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4115 MIXERLINEW *info, DWORD flags)
4117 BOOL is_out = TRUE;
4118 if(mmdevice->in_caps.szPname[0] != '\0')
4119 is_out = FALSE;
4121 if(info->dwSource != 0)
4122 return MIXERR_INVALLINE;
4124 info->dwDestination = 0;
4125 info->dwLineID = 0;
4126 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4127 info->cConnections = 0;
4128 info->cControls = 2;
4129 /* volume & mute always affect all channels, so claim 1 channel */
4130 info->cChannels = 1;
4131 info->Target.dwDeviceID = mmdev_index;
4132 info->Target.wMid = ~0;
4133 info->Target.wPid = ~0;
4134 info->Target.vDriverVersion = 0;
4136 lstrcpyW(info->szShortName, volumeW);
4137 lstrcpyW(info->szName, mastervolumeW);
4139 if(is_out){
4140 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4141 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4142 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4143 sizeof(info->Target.szPname));
4144 }else{
4145 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4146 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4147 info->Target.szPname[0] = '\0';
4150 return MMSYSERR_NOERROR;
4153 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4154 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4156 BOOL is_out = TRUE;
4157 if(mmdevice->in_caps.szPname[0] != '\0')
4158 is_out = FALSE;
4160 if(info->dwDestination != 0)
4161 return MIXERR_INVALLINE;
4163 info->dwSource = 0xFFFFFFFF;
4164 info->dwLineID = 0xFFFF0000;
4165 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4166 info->cConnections = 1;
4167 info->cControls = 2;
4169 lstrcpyW(info->szShortName, volumeW);
4170 lstrcpyW(info->szName, mastervolumeW);
4172 info->Target.dwDeviceID = mmdev_index;
4173 info->Target.wMid = ~0;
4174 info->Target.wPid = ~0;
4175 info->Target.vDriverVersion = 0;
4177 if(is_out){
4178 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4179 info->cChannels = mmdevice->out_caps.wChannels;
4180 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4181 info->Target.szPname[0] = '\0';
4182 }else{
4183 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4184 info->cChannels = mmdevice->in_caps.wChannels;
4185 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4186 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4187 sizeof(info->Target.szPname));
4190 return MMSYSERR_NOERROR;
4193 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4194 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4196 BOOL is_out = TRUE;
4197 if(mmdevice->in_caps.szPname[0] != '\0')
4198 is_out = FALSE;
4200 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4201 if(is_out)
4202 return MIXERR_INVALLINE;
4203 info->dwDestination = 0;
4204 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4207 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4208 if(!is_out)
4209 return MIXERR_INVALLINE;
4210 info->dwDestination = 0;
4211 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4214 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4215 if(is_out)
4216 return MIXERR_INVALLINE;
4217 info->dwSource = 0;
4218 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4221 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4222 if(!is_out)
4223 return MIXERR_INVALLINE;
4224 info->dwSource = 0;
4225 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4228 TRACE("Returning INVALLINE on this component type: %u\n",
4229 info->dwComponentType);
4231 return MIXERR_INVALLINE;
4234 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4235 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4237 if(info->dwLineID == 0xFFFF0000){
4238 info->dwDestination = 0;
4239 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4242 if(info->dwLineID == 0){
4243 info->dwSource = 0;
4244 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4247 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4248 return MIXERR_INVALLINE;
4251 /**************************************************************************
4252 * mixerGetLineInfoW [WINMM.@]
4254 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4256 UINT mmdev_index;
4257 WINMM_MMDevice *mmdevice;
4258 HRESULT hr;
4260 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4262 hr = WINMM_InitMMDevices();
4263 if(FAILED(hr))
4264 return MMSYSERR_NODRIVER;
4266 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4267 return MMSYSERR_INVALPARAM;
4269 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4270 TRACE("dwSource: %u\n", lpmliW->dwSource);
4271 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4272 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4273 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4275 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4276 MIXER_GETLINEINFOF_DESTINATION |
4277 MIXER_GETLINEINFOF_LINEID |
4278 MIXER_GETLINEINFOF_SOURCE |
4279 MIXER_GETLINEINFOF_TARGETTYPE |
4280 MIXER_OBJECTF_HMIXER |
4281 MIXER_OBJECTF_MIXER)){
4282 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4283 return MMSYSERR_INVALFLAG;
4286 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4287 if(!mmdevice)
4288 return MMSYSERR_INVALHANDLE;
4290 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4291 case MIXER_GETLINEINFOF_DESTINATION:
4292 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4293 fdwInfo);
4294 case MIXER_GETLINEINFOF_SOURCE:
4295 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4296 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4297 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4298 fdwInfo);
4299 case MIXER_GETLINEINFOF_LINEID:
4300 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4301 case MIXER_GETLINEINFOF_TARGETTYPE:
4302 FIXME("TARGETTYPE flag not implemented!\n");
4303 return MIXERR_INVALLINE;
4306 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
4307 return MMSYSERR_INVALFLAG;
4310 /**************************************************************************
4311 * mixerGetLineInfoA [WINMM.@]
4313 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4314 DWORD fdwInfo)
4316 MIXERLINEW mliW;
4317 UINT ret;
4319 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4321 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4322 return MMSYSERR_INVALPARAM;
4324 mliW.cbStruct = sizeof(mliW);
4325 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4326 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4327 mliW.dwComponentType = lpmliA->dwComponentType;
4328 break;
4329 case MIXER_GETLINEINFOF_DESTINATION:
4330 mliW.dwDestination = lpmliA->dwDestination;
4331 break;
4332 case MIXER_GETLINEINFOF_LINEID:
4333 mliW.dwLineID = lpmliA->dwLineID;
4334 break;
4335 case MIXER_GETLINEINFOF_SOURCE:
4336 mliW.dwDestination = lpmliA->dwDestination;
4337 mliW.dwSource = lpmliA->dwSource;
4338 break;
4339 case MIXER_GETLINEINFOF_TARGETTYPE:
4340 mliW.Target.dwType = lpmliA->Target.dwType;
4341 mliW.Target.wMid = lpmliA->Target.wMid;
4342 mliW.Target.wPid = lpmliA->Target.wPid;
4343 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4344 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
4345 break;
4346 default:
4347 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4348 return MMSYSERR_INVALFLAG;
4351 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4353 if(ret == MMSYSERR_NOERROR)
4355 lpmliA->dwDestination = mliW.dwDestination;
4356 lpmliA->dwSource = mliW.dwSource;
4357 lpmliA->dwLineID = mliW.dwLineID;
4358 lpmliA->fdwLine = mliW.fdwLine;
4359 lpmliA->dwUser = mliW.dwUser;
4360 lpmliA->dwComponentType = mliW.dwComponentType;
4361 lpmliA->cChannels = mliW.cChannels;
4362 lpmliA->cConnections = mliW.cConnections;
4363 lpmliA->cControls = mliW.cControls;
4364 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4365 sizeof(lpmliA->szShortName), NULL, NULL);
4366 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4367 sizeof(lpmliA->szName), NULL, NULL );
4368 lpmliA->Target.dwType = mliW.Target.dwType;
4369 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4370 lpmliA->Target.wMid = mliW.Target.wMid;
4371 lpmliA->Target.wPid = mliW.Target.wPid;
4372 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4373 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4374 sizeof(lpmliA->Target.szPname), NULL, NULL );
4376 return ret;
4379 /**************************************************************************
4380 * mixerSetControlDetails [WINMM.@]
4382 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4383 DWORD fdwDetails)
4385 WINMM_ControlDetails details;
4386 UINT ret;
4388 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4390 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4391 MIXER_SETCONTROLDETAILSF_CUSTOM)
4392 return MMSYSERR_NOTSUPPORTED;
4394 if(!lpmcd)
4395 return MMSYSERR_INVALPARAM;
4397 if(!WINMM_StartDevicesThread())
4398 return MMSYSERR_NODRIVER;
4400 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4402 details.hmix = hmix;
4403 details.details = lpmcd;
4404 details.flags = fdwDetails;
4406 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4407 (DWORD_PTR)&details, 0);
4408 InterlockedDecrement(&g_devthread_token);
4409 return ret;
4412 /**************************************************************************
4413 * mixerMessage [WINMM.@]
4415 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4417 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4419 return MMSYSERR_NOTSUPPORTED;