vbscript: Handle index read access to array properties.
[wine.git] / dlls / winmm / waveform.c
blobecd2acb8b28870f6b283d57ba86705d7b2686f67
1 /*
2 * Copyright 1993 Martin Ayotte
3 * 1998-2002 Eric Pouech
4 * 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "mmsystem.h"
32 #include "mmreg.h"
33 #include "msacm.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "winternl.h"
38 #include "winemm.h"
39 #include "resource.h"
41 #include "ole2.h"
42 #include "initguid.h"
43 #include "devpkey.h"
44 #include "mmdeviceapi.h"
45 #include "audioclient.h"
46 #include "audiopolicy.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
52 /* HWAVE (and HMIXER) format:
54 * XXXX... 1FDD DDDD IIII IIII
55 * X = unused (must be 0)
56 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
57 * F = flow direction (0 = IN, 1 = OUT)
58 * D = index into g_out_mmdevices, or all 1s for the MAPPER device
59 * I = index in the mmdevice's devices array
61 * Two reasons that we don't just use pointers:
62 * - HWAVEs must fit into 16 bits for compatibility with old applications.
63 * - We must be able to identify bad devices without crashing.
66 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
67 #define AC_BUFLEN (10 * 100000)
68 #define MAX_DEVICES 256
69 #define MAPPER_INDEX 0x3F
71 typedef struct _WINMM_CBInfo {
72 DWORD_PTR callback;
73 DWORD_PTR user;
74 DWORD flags;
75 HWAVE hwave;
76 } WINMM_CBInfo;
78 struct _WINMM_MMDevice;
79 typedef struct _WINMM_MMDevice WINMM_MMDevice;
81 typedef struct _WINMM_Device {
82 WINMM_CBInfo cb_info;
84 HWAVE handle;
86 BOOL open;
88 IMMDevice *device;
89 IAudioClient *client;
90 IAudioRenderClient *render;
91 IAudioCaptureClient *capture;
92 IAudioClock *clock;
93 IAudioStreamVolume *volume;
95 WAVEFORMATEX *orig_fmt;
96 HACMSTREAM acm_handle;
97 ACMSTREAMHEADER acm_hdr;
98 UINT32 acm_offs;
100 WAVEHDR *first, *last, *playing, *loop_start;
102 BOOL stopped;
103 DWORD loop_counter;
104 UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
105 UINT32 remainder_frames; /* header chunk frames already played when a device switch occurred */
107 /* stored in frames of sample rate, *not* AC::GetFrequency */
108 UINT64 last_clock_pos;
110 HANDLE event;
111 CRITICAL_SECTION lock;
113 WINMM_MMDevice *parent;
114 } WINMM_Device;
116 struct _WINMM_MMDevice {
117 WAVEOUTCAPSW out_caps; /* must not be modified outside of WINMM_InitMMDevices*/
118 WAVEINCAPSW in_caps; /* must not be modified outside of WINMM_InitMMDevices*/
119 WCHAR *dev_id;
120 EDataFlow dataflow;
122 ISimpleAudioVolume *volume;
124 GUID session;
126 UINT index;
128 /* HMIXER format is the same as the HWAVE format, but the I bits are
129 * replaced by the value of this counter, to keep each HMIXER unique */
130 UINT mixer_count;
132 CRITICAL_SECTION lock;
134 WINMM_Device *devices[MAX_DEVICES];
137 static WINMM_MMDevice *g_out_mmdevices;
138 static WINMM_MMDevice **g_out_map;
139 static UINT g_outmmdevices_count;
140 static WINMM_Device *g_out_mapper_devices[MAX_DEVICES];
142 static WINMM_MMDevice *g_in_mmdevices;
143 static WINMM_MMDevice **g_in_map;
144 static UINT g_inmmdevices_count;
145 static WINMM_Device *g_in_mapper_devices[MAX_DEVICES];
147 static IMMDeviceEnumerator *g_devenum;
149 static CRITICAL_SECTION g_devthread_lock;
150 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug =
152 0, 0, &g_devthread_lock,
153 { &g_devthread_lock_debug.ProcessLocksList, &g_devthread_lock_debug.ProcessLocksList },
154 0, 0, { (DWORD_PTR)(__FILE__ ": g_devthread_lock") }
156 static CRITICAL_SECTION g_devthread_lock = { &g_devthread_lock_debug, -1, 0, 0, 0, 0 };
157 static LONG g_devthread_token;
158 static HANDLE g_devices_thread;
159 static HWND g_devices_hwnd;
160 static HMODULE g_devthread_module;
162 static UINT g_devhandle_count;
163 static HANDLE *g_device_handles;
164 static WINMM_Device **g_handle_devices;
166 typedef struct _WINMM_OpenInfo {
167 HWAVE handle;
168 UINT req_device;
169 WAVEFORMATEX *format;
170 DWORD_PTR callback;
171 DWORD_PTR cb_user;
172 DWORD flags;
173 BOOL reset;
174 } WINMM_OpenInfo;
176 typedef struct _WINMM_ControlDetails {
177 HMIXEROBJ hmix;
178 MIXERCONTROLDETAILS *details;
179 DWORD flags;
180 } WINMM_ControlDetails;
182 typedef struct _WINMM_QueryInterfaceInfo {
183 BOOL is_out;
184 UINT index;
185 WCHAR *str;
186 ULONG *len_bytes;
187 } WINMM_QueryInterfaceInfo;
189 static LRESULT WOD_Open(WINMM_OpenInfo *info);
190 static LRESULT WOD_Close(HWAVEOUT hwave);
191 static LRESULT WID_Open(WINMM_OpenInfo *info);
192 static LRESULT WID_Close(HWAVEIN hwave);
193 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device);
194 static void WOD_PushData(WINMM_Device *device);
196 void WINMM_DeleteWaveform(void)
198 UINT i, j;
200 if(g_devices_thread)
201 CloseHandle(g_devices_thread);
203 for(i = 0; i < g_outmmdevices_count; ++i){
204 WINMM_MMDevice *mmdevice = &g_out_mmdevices[i];
206 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
207 WINMM_Device *device = mmdevice->devices[j];
208 if(device->handle)
209 CloseHandle(device->handle);
210 device->lock.DebugInfo->Spare[0] = 0;
211 DeleteCriticalSection(&device->lock);
214 if(mmdevice->volume)
215 ISimpleAudioVolume_Release(mmdevice->volume);
216 CoTaskMemFree(mmdevice->dev_id);
217 mmdevice->lock.DebugInfo->Spare[0] = 0;
218 DeleteCriticalSection(&mmdevice->lock);
221 for(i = 0; i < g_inmmdevices_count; ++i){
222 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
224 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
225 WINMM_Device *device = mmdevice->devices[j];
226 if(device->handle)
227 CloseHandle(device->handle);
228 device->lock.DebugInfo->Spare[0] = 0;
229 DeleteCriticalSection(&device->lock);
232 if(mmdevice->volume)
233 ISimpleAudioVolume_Release(mmdevice->volume);
234 CoTaskMemFree(mmdevice->dev_id);
235 mmdevice->lock.DebugInfo->Spare[0] = 0;
236 DeleteCriticalSection(&mmdevice->lock);
239 HeapFree(GetProcessHeap(), 0, g_out_mmdevices);
240 HeapFree(GetProcessHeap(), 0, g_in_mmdevices);
242 HeapFree(GetProcessHeap(), 0, g_device_handles);
243 HeapFree(GetProcessHeap(), 0, g_handle_devices);
245 DeleteCriticalSection(&g_devthread_lock);
248 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
250 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
251 (mmdevice << 8) | device);
254 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
255 BOOL *is_out, UINT *device_index, UINT *junk)
257 ULONG32 l = HandleToULong(hwave);
258 *device_index = l & 0xFF;
259 *mmdevice_index = (l >> 8) & 0x3F;
260 *is_out = (l >> 14) & 0x1;
261 *junk = l >> 15;
264 static void WINMM_InitDevice(WINMM_Device *device)
266 InitializeCriticalSection(&device->lock);
267 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
270 static inline WINMM_MMDevice *read_map(WINMM_MMDevice **map, UINT index)
272 WINMM_MMDevice *ret;
273 EnterCriticalSection(&g_devthread_lock);
274 ret = map[index];
275 LeaveCriticalSection(&g_devthread_lock);
276 return ret;
279 /* finds the first unused Device, marks it as "open", and returns
280 * a pointer to the device
282 * IMPORTANT: it is the caller's responsibility to release the device's lock
283 * on success
285 static WINMM_Device *WINMM_FindUnusedDevice(WINMM_Device **devices,
286 WINMM_MMDevice *parent, UINT internal_index, BOOL is_out)
288 UINT i;
290 for(i = 0; i < MAX_DEVICES; ++i){
291 WINMM_Device *device = devices[i];
293 if(!device){
294 device = devices[i] = HeapAlloc(GetProcessHeap(),
295 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
296 if(!device)
297 return NULL;
299 WINMM_InitDevice(device);
300 EnterCriticalSection(&device->lock);
301 }else
302 EnterCriticalSection(&device->lock);
304 if(!device->open){
305 device->handle = WINMM_MakeHWAVE(internal_index, is_out, i);
306 device->parent = parent;
307 device->open = TRUE;
309 return device;
312 LeaveCriticalSection(&device->lock);
315 TRACE("All devices in use: mmdevice: %u\n", internal_index);
317 return NULL;
320 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
322 if(!device)
323 return FALSE;
325 EnterCriticalSection(&device->lock);
327 if(!device->open){
328 LeaveCriticalSection(&device->lock);
329 return FALSE;
332 return TRUE;
335 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
337 WINMM_MMDevice *mmdevice;
338 WINMM_Device *device;
339 UINT mmdevice_index, device_index, junk;
340 BOOL is_out;
342 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
344 if(junk != 0x1)
345 return NULL;
347 if(mmdevice_index == MAPPER_INDEX){
348 EnterCriticalSection(&g_devthread_lock);
349 if(is_out)
350 device = g_out_mapper_devices[device_index];
351 else
352 device = g_in_mapper_devices[device_index];
353 LeaveCriticalSection(&g_devthread_lock);
354 return device;
357 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
358 return NULL;
360 if(is_out)
361 mmdevice = &g_out_mmdevices[mmdevice_index];
362 else
363 mmdevice = &g_in_mmdevices[mmdevice_index];
365 EnterCriticalSection(&mmdevice->lock);
367 device = mmdevice->devices[device_index];
369 LeaveCriticalSection(&mmdevice->lock);
371 return device;
374 /* Note: NotifyClient should never be called while holding the device lock
375 * since the client may call wave* functions from within the callback. */
376 static inline void WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
377 DWORD_PTR param2)
379 DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
380 msg, info->user, param1, param2);
383 static MMRESULT hr2mmr(HRESULT hr)
385 switch(hr){
386 case S_OK:
387 case AUDCLNT_E_NOT_STOPPED:
388 return MMSYSERR_NOERROR;
389 case AUDCLNT_E_UNSUPPORTED_FORMAT:
390 return WAVERR_BADFORMAT;
391 case AUDCLNT_E_DEVICE_IN_USE:
392 return MMSYSERR_ALLOCATED;
393 case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
394 return MMSYSERR_NOTENABLED;
395 case E_OUTOFMEMORY:
396 return MMSYSERR_NOMEM;
397 case E_POINTER:
398 case E_INVALIDARG:
399 return MMSYSERR_INVALPARAM;
400 case AUDCLNT_E_DEVICE_INVALIDATED: /* DSERR_BUFFERLOST */
401 default:
402 return FAILED(hr) ? MMSYSERR_ERROR : MMSYSERR_NOERROR;
406 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
407 UINT outlen)
409 IPropertyStore *ps;
410 PROPVARIANT var;
411 HRESULT hr;
413 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
414 if(FAILED(hr))
415 return hr;
417 PropVariantInit(&var);
419 hr = IPropertyStore_GetValue(ps,
420 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
421 if(FAILED(hr)){
422 IPropertyStore_Release(ps);
423 return hr;
426 lstrcpynW(out, var.pwszVal, outlen);
428 PropVariantClear(&var);
430 IPropertyStore_Release(ps);
432 return S_OK;
435 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
436 WORD channels)
438 WAVEFORMATEX fmt, *junk;
439 HRESULT hr;
441 fmt.wFormatTag = WAVE_FORMAT_PCM;
442 fmt.nChannels = channels;
443 fmt.nSamplesPerSec = rate;
444 fmt.wBitsPerSample = depth;
445 fmt.nBlockAlign = (channels * depth) / 8;
446 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
447 fmt.cbSize = 0;
449 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
450 &fmt, &junk);
451 if(SUCCEEDED(hr))
452 CoTaskMemFree(junk);
454 return hr;
457 static struct _TestFormat {
458 DWORD flag;
459 DWORD rate;
460 DWORD depth;
461 WORD channels;
462 } formats_to_test[] = {
463 { WAVE_FORMAT_1M08, 11025, 8, 1 },
464 { WAVE_FORMAT_1M16, 11025, 16, 1 },
465 { WAVE_FORMAT_1S08, 11025, 8, 2 },
466 { WAVE_FORMAT_1S16, 11025, 16, 2 },
467 { WAVE_FORMAT_2M08, 22050, 8, 1 },
468 { WAVE_FORMAT_2M16, 22050, 16, 1 },
469 { WAVE_FORMAT_2S08, 22050, 8, 2 },
470 { WAVE_FORMAT_2S16, 22050, 16, 2 },
471 { WAVE_FORMAT_4M08, 44100, 8, 1 },
472 { WAVE_FORMAT_4M16, 44100, 16, 1 },
473 { WAVE_FORMAT_4S08, 44100, 8, 2 },
474 { WAVE_FORMAT_4S16, 44100, 16, 2 },
475 { WAVE_FORMAT_48M08, 48000, 8, 1 },
476 { WAVE_FORMAT_48M16, 48000, 16, 1 },
477 { WAVE_FORMAT_48S08, 48000, 8, 2 },
478 { WAVE_FORMAT_48S16, 48000, 16, 2 },
479 { WAVE_FORMAT_96M08, 96000, 8, 1 },
480 { WAVE_FORMAT_96M16, 96000, 16, 1 },
481 { WAVE_FORMAT_96S08, 96000, 8, 2 },
482 { WAVE_FORMAT_96S16, 96000, 16, 2 },
486 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
488 DWORD flags = 0;
489 HRESULT hr;
490 struct _TestFormat *fmt;
491 IAudioClient *client;
493 hr = IMMDevice_Activate(device, &IID_IAudioClient,
494 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
495 if(FAILED(hr))
496 return 0;
498 for(fmt = formats_to_test; fmt->flag; ++fmt){
499 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
500 if(hr == S_OK)
501 flags |= fmt->flag;
504 IAudioClient_Release(client);
506 return flags;
509 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
510 WINMM_MMDevice *dev, UINT index)
512 HRESULT hr;
514 dev->dataflow = flow;
515 if(flow == eRender){
516 dev->out_caps.wMid = 0xFF;
517 dev->out_caps.wPid = 0xFF;
518 dev->out_caps.vDriverVersion = 0x00010001;
519 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
520 dev->out_caps.wReserved1 = 0;
521 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
522 WAVECAPS_SAMPLEACCURATE;
523 dev->out_caps.wChannels = 2;
524 dev->out_caps.szPname[0] = '\0';
526 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
527 sizeof(dev->out_caps.szPname) /
528 sizeof(*dev->out_caps.szPname));
529 if(FAILED(hr))
530 return hr;
531 }else{
532 dev->in_caps.wMid = 0xFF;
533 dev->in_caps.wPid = 0xFF;
534 dev->in_caps.vDriverVersion = 0x00010001;
535 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
536 dev->in_caps.wReserved1 = 0;
537 dev->in_caps.wChannels = 2;
538 dev->in_caps.szPname[0] = '\0';
540 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
541 sizeof(dev->in_caps.szPname) /
542 sizeof(*dev->in_caps.szPname));
543 if(FAILED(hr))
544 return hr;
547 hr = IMMDevice_GetId(device, &dev->dev_id);
548 if(FAILED(hr))
549 return hr;
551 CoCreateGuid(&dev->session);
553 dev->index = index;
555 InitializeCriticalSection(&dev->lock);
556 dev->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
558 return S_OK;
561 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices,
562 WINMM_MMDevice ***map, UINT *devcount, EDataFlow flow,
563 IMMDeviceEnumerator *devenum)
565 IMMDeviceCollection *devcoll;
566 HRESULT hr;
568 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
569 DEVICE_STATE_ACTIVE, &devcoll);
570 if(FAILED(hr))
571 return hr;
573 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
574 if(FAILED(hr)){
575 IMMDeviceCollection_Release(devcoll);
576 return hr;
579 if(*devcount > 0){
580 UINT n, count = 1;
581 IMMDevice *def_dev = NULL;
583 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
584 sizeof(WINMM_MMDevice) * (*devcount));
585 if(!*devices){
586 IMMDeviceCollection_Release(devcoll);
587 return E_OUTOFMEMORY;
590 *map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
591 sizeof(WINMM_MMDevice *) * (*devcount));
592 if(!*map){
593 IMMDeviceCollection_Release(devcoll);
594 HeapFree(GetProcessHeap(), 0, *devices);
595 return E_OUTOFMEMORY;
598 /* make sure that device 0 is the default device */
599 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
600 flow, eConsole, &def_dev);
602 for(n = 0; n < *devcount; ++n){
603 IMMDevice *device;
605 hr = IMMDeviceCollection_Item(devcoll, n, &device);
606 if(SUCCEEDED(hr)){
607 WINMM_InitMMDevice(flow, device, &(*devices)[n], n);
609 if(device == def_dev)
610 (*map)[0] = &(*devices)[n];
611 else{
612 (*map)[count] = &(*devices)[n];
613 ++count;
616 IMMDevice_Release(device);
620 IMMDevice_Release(def_dev);
622 *devcount = count;
625 IMMDeviceCollection_Release(devcoll);
627 return S_OK;
630 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
631 const GUID *riid, void **obj)
633 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid));
634 return E_NOINTERFACE;
637 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
639 return 2;
642 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
644 return 1;
647 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
648 const WCHAR *device_id, DWORD new_state)
650 TRACE("Ignoring OnDeviceStateChanged callback\n");
651 return S_OK;
654 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
655 const WCHAR *device_id)
657 TRACE("Ignoring OnDeviceAdded callback\n");
658 return S_OK;
661 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
662 const WCHAR *device_id)
664 TRACE("Ignoring OnDeviceRemoved callback\n");
665 return S_OK;
668 static HRESULT update_mapping(WINMM_MMDevice ***map, UINT count,
669 const WCHAR *default_id)
671 WINMM_MMDevice *prev;
672 UINT i;
674 prev = (*map)[0];
675 for(i = 0; i < count; ++i){
676 WINMM_MMDevice *tmp;
678 if(!wcscmp((*map)[i]->dev_id, default_id)){
679 (*map)[0] = (*map)[i];
680 (*map)[i] = prev;
682 return S_OK;
685 tmp = (*map)[i];
686 (*map)[i] = prev;
687 prev = tmp;
690 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
691 (*map)[0] = prev;
693 return S_FALSE;
696 static HRESULT reroute_mapper_device(WINMM_Device *device, BOOL is_out)
698 WINMM_OpenInfo info;
699 BOOL stopped;
700 MMRESULT mr;
701 HRESULT hr;
702 UINT64 clock_freq, clock_pos;
704 TRACE("rerouting device %p\n", device->handle);
706 EnterCriticalSection(&device->lock);
708 if(!device->open || device->acm_handle){
709 /* Windows 7 doesn't re-route ACM devices, so we don't either.
710 * Seems to be because of the data waveXxxPrepareHeader allocates. */
711 LeaveCriticalSection(&device->lock);
712 return S_FALSE;
715 stopped = device->stopped;
717 info.handle = 0;
718 info.req_device = WAVE_MAPPER;
719 info.format = device->orig_fmt;
720 info.callback = device->cb_info.callback;
721 info.cb_user = device->cb_info.user;
722 /* We have to use direct here so that we don't suddenly introduce ACM
723 * into a playing stream that hasn't been Prepared for it */
724 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT_QUERY;
725 info.reset = FALSE;
727 if(is_out)
728 mr = WOD_Open(&info);
729 else
730 mr = WID_Open(&info);
732 if(mr != MMSYSERR_NOERROR){
733 TRACE("New default device doesn't support this stream: %p\n", device->handle);
734 LeaveCriticalSection(&device->lock);
735 return S_FALSE;
738 hr = IAudioClient_Stop(device->client);
739 if(FAILED(hr))
740 WARN("Stop failed: %08lx\n", hr);
742 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
743 if(FAILED(hr)){
744 WARN("GetFrequency failed: %08lx\n", hr);
745 LeaveCriticalSection(&device->lock);
746 return hr;
749 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
750 if(FAILED(hr)){
751 WARN("GetPosition failed: %08lx\n", hr);
752 LeaveCriticalSection(&device->lock);
753 return hr;
756 device->remainder_frames = MulDiv(clock_pos, device->samples_per_sec, clock_freq) - device->last_clock_pos;
758 info.handle = device->handle;
759 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT;
761 if(is_out){
762 WOD_Close((HWAVEOUT)device->handle);
763 device->parent = read_map(g_out_map, 0);
764 mr = WOD_Open(&info);
765 }else{
766 WID_Close((HWAVEIN)device->handle);
767 device->parent = read_map(g_in_map, 0);
768 mr = WID_Open(&info);
771 if(mr != MMSYSERR_NOERROR){
772 ERR("Opening new default device failed! %u\n", mr);
773 LeaveCriticalSection(&device->lock);
774 return E_FAIL;
777 HeapFree(GetProcessHeap(), 0, info.format);
779 if(!stopped){
780 if(is_out)
781 WOD_PushData(device);
782 WINMM_BeginPlaying(device);
785 LeaveCriticalSection(&device->lock);
787 return S_OK;
790 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
791 EDataFlow flow, ERole role, const WCHAR *device_id)
793 UINT i;
795 TRACE("%u %u %s\n", flow, role, wine_dbgstr_w(device_id));
797 if(role != eConsole)
798 return S_OK;
800 EnterCriticalSection(&g_devthread_lock);
802 if(flow == eRender)
803 update_mapping(&g_out_map, g_outmmdevices_count, device_id);
804 else
805 update_mapping(&g_in_map, g_inmmdevices_count, device_id);
807 for(i = 0; i < MAX_DEVICES && g_out_mapper_devices[i]; ++i)
808 reroute_mapper_device(g_out_mapper_devices[i], TRUE);
810 for(i = 0; i < MAX_DEVICES && g_in_mapper_devices[i]; ++i)
811 reroute_mapper_device(g_in_mapper_devices[i], FALSE);
813 LeaveCriticalSection(&g_devthread_lock);
815 return S_OK;
818 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
819 const WCHAR *device_id, const PROPERTYKEY key)
821 TRACE("Ignoring OnPropertyValueChanged callback\n");
822 return S_OK;
825 static IMMNotificationClientVtbl g_notif_vtbl = {
826 notif_QueryInterface,
827 notif_AddRef,
828 notif_Release,
829 notif_OnDeviceStateChanged,
830 notif_OnDeviceAdded,
831 notif_OnDeviceRemoved,
832 notif_OnDefaultDeviceChanged,
833 notif_OnPropertyValueChanged
836 static IMMNotificationClient g_notif = { &g_notif_vtbl };
838 static HRESULT WINMM_InitMMDevices(void)
840 HRESULT hr, init_hr;
841 IMMDeviceEnumerator *devenum = NULL;
843 if(g_outmmdevices_count || g_inmmdevices_count)
844 return S_FALSE;
846 init_hr = CoInitialize(NULL);
848 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
849 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
850 if(FAILED(hr))
851 goto exit;
853 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum, &g_notif);
854 if(FAILED(hr))
855 WARN("RegisterEndpointNotificationCallback failed: %08lx\n", hr);
857 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_out_map, &g_outmmdevices_count,
858 eRender, devenum);
859 if(FAILED(hr)){
860 g_outmmdevices_count = 0;
861 g_inmmdevices_count = 0;
862 goto exit;
865 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_in_map, &g_inmmdevices_count,
866 eCapture, devenum);
867 if(FAILED(hr)){
868 g_inmmdevices_count = 0;
869 goto exit;
872 exit:
873 if(devenum)
874 IMMDeviceEnumerator_Release(devenum);
875 if(SUCCEEDED(init_hr))
876 CoUninitialize();
878 return hr;
881 static inline BOOL WINMM_IsMapper(UINT device)
883 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
886 static MMRESULT WINMM_TryDeviceMapping(WINMM_Device *device, WAVEFORMATEX *fmt,
887 WORD channels, DWORD freq, DWORD bits_per_samp, BOOL is_query, BOOL is_out)
889 WAVEFORMATEX target, *closer_fmt = NULL;
890 HRESULT hr;
891 MMRESULT mr;
893 TRACE("format: %u, channels: %u, sample rate: %lu, bit depth: %lu\n",
894 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
896 target.wFormatTag = WAVE_FORMAT_PCM;
897 target.nChannels = channels;
898 target.nSamplesPerSec = freq;
899 target.wBitsPerSample = bits_per_samp;
900 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
901 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
902 target.cbSize = 0;
904 hr = IAudioClient_IsFormatSupported(device->client,
905 AUDCLNT_SHAREMODE_SHARED, &target, &closer_fmt);
906 CoTaskMemFree(closer_fmt);
907 if(hr != S_OK)
908 return WAVERR_BADFORMAT;
910 /* device supports our target format, so see if MSACM can
911 * do the conversion */
912 if(is_out)
913 mr = acmStreamOpen(&device->acm_handle, NULL, fmt, &target, NULL,
914 0, 0, 0);
915 else
916 mr = acmStreamOpen(&device->acm_handle, NULL, &target, fmt, NULL,
917 0, 0, 0);
918 if(mr != MMSYSERR_NOERROR)
919 return mr;
921 /* yes it can. initialize the audioclient and return success */
922 if(is_query){
923 acmStreamClose(device->acm_handle, 0);
924 device->acm_handle = NULL;
925 return MMSYSERR_NOERROR;
928 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
929 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
930 AC_BUFLEN, 0, &target, &device->parent->session);
931 if(hr != S_OK){
932 WARN("Initialize failed: %08lx\n", hr);
933 acmStreamClose(device->acm_handle, 0);
934 device->acm_handle = NULL;
935 return MMSYSERR_ERROR;
938 device->bytes_per_frame = target.nBlockAlign;
939 device->samples_per_sec = target.nSamplesPerSec;
941 TRACE("Success!\n");
943 return MMSYSERR_NOERROR;
946 static MMRESULT WINMM_MapDevice(WINMM_Device *device, BOOL is_query, BOOL is_out)
948 MMRESULT mr;
949 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)device->orig_fmt;
951 TRACE("(%p, %u)\n", device, is_out);
953 /* set up the ACM stream */
954 if(device->orig_fmt->wFormatTag != WAVE_FORMAT_PCM &&
955 !(device->orig_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
956 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
957 /* convert to PCM format if it's not already */
958 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
959 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
960 16, is_query, is_out);
961 if(mr == MMSYSERR_NOERROR)
962 return mr;
964 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
965 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
966 8, is_query, is_out);
967 if(mr == MMSYSERR_NOERROR)
968 return mr;
969 }else{
970 WORD channels;
972 /* first try just changing bit depth and channels */
973 channels = device->orig_fmt->nChannels;
974 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
975 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
976 if(mr == MMSYSERR_NOERROR)
977 return mr;
978 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
979 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
980 if(mr == MMSYSERR_NOERROR)
981 return mr;
983 channels = (channels == 2) ? 1 : 2;
984 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
985 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
986 if(mr == MMSYSERR_NOERROR)
987 return mr;
988 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
989 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
990 if(mr == MMSYSERR_NOERROR)
991 return mr;
993 /* that didn't work, so now try different sample rates */
994 channels = device->orig_fmt->nChannels;
995 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
996 if(mr == MMSYSERR_NOERROR)
997 return mr;
998 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
999 if(mr == MMSYSERR_NOERROR)
1000 return mr;
1001 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
1002 if(mr == MMSYSERR_NOERROR)
1003 return mr;
1004 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1005 if(mr == MMSYSERR_NOERROR)
1006 return mr;
1007 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1008 if(mr == MMSYSERR_NOERROR)
1009 return mr;
1011 channels = (channels == 2) ? 1 : 2;
1012 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
1013 if(mr == MMSYSERR_NOERROR)
1014 return mr;
1015 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
1016 if(mr == MMSYSERR_NOERROR)
1017 return mr;
1018 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
1019 if(mr == MMSYSERR_NOERROR)
1020 return mr;
1021 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1022 if(mr == MMSYSERR_NOERROR)
1023 return mr;
1024 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1025 if(mr == MMSYSERR_NOERROR)
1026 return mr;
1028 channels = device->orig_fmt->nChannels;
1029 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1030 if(mr == MMSYSERR_NOERROR)
1031 return mr;
1032 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1033 if(mr == MMSYSERR_NOERROR)
1034 return mr;
1035 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1036 if(mr == MMSYSERR_NOERROR)
1037 return mr;
1038 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1039 if(mr == MMSYSERR_NOERROR)
1040 return mr;
1041 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1042 if(mr == MMSYSERR_NOERROR)
1043 return mr;
1045 channels = (channels == 2) ? 1 : 2;
1046 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1047 if(mr == MMSYSERR_NOERROR)
1048 return mr;
1049 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1050 if(mr == MMSYSERR_NOERROR)
1051 return mr;
1052 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1053 if(mr == MMSYSERR_NOERROR)
1054 return mr;
1055 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1056 if(mr == MMSYSERR_NOERROR)
1057 return mr;
1058 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1059 if(mr == MMSYSERR_NOERROR)
1060 return mr;
1063 WARN("Unable to find compatible device!\n");
1064 return WAVERR_BADFORMAT;
1067 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_OpenInfo *info,
1068 BOOL is_out)
1070 LRESULT ret = MMSYSERR_NOMEM;
1071 HRESULT hr;
1073 hr = IMMDeviceEnumerator_GetDevice(g_devenum, device->parent->dev_id,
1074 &device->device);
1075 if(FAILED(hr)){
1076 WARN("Device %s (%s) unavailable: %08lx\n",
1077 wine_dbgstr_w(device->parent->dev_id),
1078 wine_dbgstr_w(device->parent->out_caps.szPname), hr);
1079 ret = MMSYSERR_NODRIVER;
1080 goto error;
1083 /* this is where winexyz.drv opens the audio device */
1084 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
1085 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
1086 if(FAILED(hr)){
1087 WARN("Activate failed: %08lx\n", hr);
1088 ret = hr2mmr(hr);
1089 if(ret == MMSYSERR_ERROR)
1090 ret = MMSYSERR_NOTENABLED;
1091 goto error;
1094 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
1096 if (info->format->nSamplesPerSec == 0)
1098 ret = MMSYSERR_INVALPARAM;
1099 goto error;
1102 /* we aren't guaranteed that the struct in lpFormat is a full
1103 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1104 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
1105 memcpy(device->orig_fmt, info->format, sizeof(PCMWAVEFORMAT));
1106 device->orig_fmt->cbSize = 0;
1107 if(device->orig_fmt->wBitsPerSample % 8 != 0){
1108 WARN("Fixing bad wBitsPerSample (%u)\n", device->orig_fmt->wBitsPerSample);
1109 device->orig_fmt->wBitsPerSample = (device->orig_fmt->wBitsPerSample + 7) & ~7;
1111 /* winmm ignores broken blockalign and avgbytes */
1112 if(device->orig_fmt->nBlockAlign != device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8){
1113 WARN("Fixing bad nBlockAlign (%u)\n", device->orig_fmt->nBlockAlign);
1114 device->orig_fmt->nBlockAlign = device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8;
1116 if (device->orig_fmt->nAvgBytesPerSec != device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign) {
1117 WARN("Fixing bad nAvgBytesPerSec (%lu)\n", device->orig_fmt->nAvgBytesPerSec);
1118 device->orig_fmt->nAvgBytesPerSec = device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign;
1120 }else{
1121 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0,
1122 sizeof(WAVEFORMATEX) + info->format->cbSize);
1123 memcpy(device->orig_fmt, info->format,
1124 sizeof(WAVEFORMATEX) + info->format->cbSize);
1127 if(info->flags & WAVE_FORMAT_QUERY){
1128 WAVEFORMATEX *closer_fmt = NULL;
1130 hr = IAudioClient_IsFormatSupported(device->client,
1131 AUDCLNT_SHAREMODE_SHARED, device->orig_fmt, &closer_fmt);
1132 CoTaskMemFree(closer_fmt);
1133 if((hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT) && !(info->flags & WAVE_FORMAT_DIRECT))
1134 ret = WINMM_MapDevice(device, TRUE, is_out);
1135 else
1136 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1137 goto error;
1140 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1141 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1142 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1143 if(FAILED(hr)){
1144 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1145 ret = WINMM_MapDevice(device, FALSE, is_out);
1146 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1147 goto error;
1148 }else{
1149 WARN("Initialize failed: %08lx\n", hr);
1150 ret = hr2mmr(hr);
1151 goto error;
1153 }else{
1154 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1155 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1158 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1159 (void**)&device->clock);
1160 if(FAILED(hr)){
1161 WARN("GetService failed: %08lx\n", hr);
1162 goto error;
1165 if(!device->event){
1166 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1167 if(!device->event){
1168 WARN("CreateEvent failed: %08lx\n", hr);
1169 goto error;
1172 /* As the devices thread is waiting on g_device_handles, it can
1173 * only be modified from within this same thread. */
1174 if(g_device_handles){
1175 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1176 sizeof(HANDLE) * (g_devhandle_count + 1));
1177 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1178 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1179 }else{
1180 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1181 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1182 sizeof(WINMM_Device *));
1184 g_device_handles[g_devhandle_count] = device->event;
1185 g_handle_devices[g_devhandle_count] = device;
1186 ++g_devhandle_count;
1189 hr = IAudioClient_SetEventHandle(device->client, device->event);
1190 if(FAILED(hr)){
1191 WARN("SetEventHandle failed: %08lx\n", hr);
1192 goto error;
1195 if(info->reset){
1196 device->played_frames = 0;
1197 device->ofs_bytes = 0;
1198 device->loop_counter = 0;
1199 device->first = device->last = device->playing = device->loop_start = NULL;
1202 device->stopped = TRUE;
1203 device->last_clock_pos = 0;
1205 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1206 device->cb_info.callback = info->callback;
1207 device->cb_info.user = info->cb_user;
1208 device->cb_info.hwave = device->handle;
1210 info->handle = device->handle;
1212 return MMSYSERR_NOERROR;
1214 error:
1215 if(device->client){
1216 IAudioClient_Release(device->client);
1217 device->client = NULL;
1219 if(device->device){
1220 IMMDevice_Release(device->device);
1221 device->device = NULL;
1224 return ret;
1227 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1229 WINMM_Device *device;
1230 LRESULT ret = MMSYSERR_ERROR;
1231 HRESULT hr;
1233 if(info->handle != 0){
1234 device = WINMM_GetDeviceFromHWAVE(info->handle);
1235 if(!device){
1236 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1237 return MMSYSERR_ERROR;
1240 EnterCriticalSection(&device->lock);
1242 device->open = TRUE;
1243 }else{
1244 CRITICAL_SECTION *lock;
1245 UINT internal_index;
1246 WINMM_Device **devices;
1247 WINMM_MMDevice *mmdevice;
1249 if(WINMM_IsMapper(info->req_device)){
1250 if (g_outmmdevices_count == 0)
1251 return MMSYSERR_BADDEVICEID;
1252 devices = g_out_mapper_devices;
1253 mmdevice = read_map(g_out_map, 0);
1254 lock = &g_devthread_lock;
1255 internal_index = MAPPER_INDEX;
1256 }else{
1257 if(info->req_device >= g_outmmdevices_count)
1258 return MMSYSERR_BADDEVICEID;
1260 mmdevice = read_map(g_out_map, info->req_device);
1262 if(!mmdevice->out_caps.szPname[0])
1263 return MMSYSERR_NOTENABLED;
1265 devices = mmdevice->devices;
1266 lock = &mmdevice->lock;
1267 internal_index = mmdevice->index;
1270 EnterCriticalSection(lock);
1272 device = WINMM_FindUnusedDevice(devices, mmdevice,
1273 internal_index, TRUE);
1274 if(!device){
1275 LeaveCriticalSection(lock);
1276 return MMSYSERR_ALLOCATED;
1279 LeaveCriticalSection(lock);
1282 ret = WINMM_OpenDevice(device, info, TRUE);
1283 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1284 goto error;
1285 ret = MMSYSERR_ERROR;
1287 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1288 (void**)&device->render);
1289 if(FAILED(hr)){
1290 ERR("GetService failed: %08lx\n", hr);
1291 goto error;
1294 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1295 (void**)&device->volume);
1296 if(FAILED(hr)){
1297 ERR("GetService failed: %08lx\n", hr);
1298 goto error;
1301 LeaveCriticalSection(&device->lock);
1303 return MMSYSERR_NOERROR;
1305 error:
1306 if(device->device){
1307 IMMDevice_Release(device->device);
1308 device->device = NULL;
1310 if(device->client){
1311 IAudioClient_Release(device->client);
1312 device->client = NULL;
1314 if(device->render){
1315 IAudioRenderClient_Release(device->render);
1316 device->render = NULL;
1318 if(device->volume){
1319 IAudioStreamVolume_Release(device->volume);
1320 device->volume = NULL;
1322 if(device->clock){
1323 IAudioClock_Release(device->clock);
1324 device->clock = NULL;
1326 device->open = FALSE;
1327 LeaveCriticalSection(&device->lock);
1328 return ret;
1331 static LRESULT WID_Open(WINMM_OpenInfo *info)
1333 WINMM_Device *device, **devices;
1334 WINMM_MMDevice *mmdevice;
1335 UINT internal_index;
1336 CRITICAL_SECTION *lock;
1337 LRESULT ret = MMSYSERR_ERROR;
1338 HRESULT hr;
1340 if(WINMM_IsMapper(info->req_device)){
1341 if (g_inmmdevices_count == 0)
1342 return MMSYSERR_BADDEVICEID;
1343 devices = g_in_mapper_devices;
1344 mmdevice = read_map(g_in_map, 0);
1345 lock = &g_devthread_lock;
1346 internal_index = MAPPER_INDEX;
1347 }else{
1348 if(info->req_device >= g_inmmdevices_count)
1349 return MMSYSERR_BADDEVICEID;
1351 mmdevice = read_map(g_in_map, info->req_device);
1353 if(!mmdevice->in_caps.szPname[0])
1354 return MMSYSERR_NOTENABLED;
1356 devices = mmdevice->devices;
1357 lock = &mmdevice->lock;
1358 internal_index = mmdevice->index;
1361 EnterCriticalSection(lock);
1363 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1364 if(!device){
1365 LeaveCriticalSection(lock);
1366 return MMSYSERR_ALLOCATED;
1369 LeaveCriticalSection(lock);
1371 ret = WINMM_OpenDevice(device, info, FALSE);
1372 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1373 goto error;
1374 ret = MMSYSERR_ERROR;
1376 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1377 (void**)&device->capture);
1378 if(FAILED(hr)){
1379 WARN("GetService failed: %08lx\n", hr);
1380 goto error;
1383 LeaveCriticalSection(&device->lock);
1385 return MMSYSERR_NOERROR;
1387 error:
1388 if(device->device){
1389 IMMDevice_Release(device->device);
1390 device->device = NULL;
1392 if(device->client){
1393 IAudioClient_Release(device->client);
1394 device->client = NULL;
1396 if(device->capture){
1397 IAudioCaptureClient_Release(device->capture);
1398 device->capture = NULL;
1400 if(device->clock){
1401 IAudioClock_Release(device->clock);
1402 device->clock = NULL;
1404 device->open = FALSE;
1405 LeaveCriticalSection(&device->lock);
1406 return ret;
1409 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1411 device->open = FALSE;
1413 if(!device->stopped){
1414 IAudioClient_Stop(device->client);
1415 device->stopped = TRUE;
1418 if(device->acm_handle){
1419 acmStreamClose(device->acm_handle, 0);
1420 device->acm_handle = NULL;
1423 IMMDevice_Release(device->device);
1424 device->device = NULL;
1426 IAudioClient_Release(device->client);
1427 device->client = NULL;
1429 IAudioClock_Release(device->clock);
1430 device->clock = NULL;
1432 HeapFree(GetProcessHeap(), 0, device->orig_fmt);
1434 return S_OK;
1437 static LRESULT WOD_Close(HWAVEOUT hwave)
1439 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1441 TRACE("(%p)\n", hwave);
1443 if(!WINMM_ValidateAndLock(device))
1444 return MMSYSERR_INVALHANDLE;
1446 WINMM_CloseDevice(device);
1448 IAudioRenderClient_Release(device->render);
1449 device->render = NULL;
1451 IAudioStreamVolume_Release(device->volume);
1452 device->volume = NULL;
1454 LeaveCriticalSection(&device->lock);
1456 return MMSYSERR_NOERROR;
1459 static LRESULT WID_Close(HWAVEIN hwave)
1461 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1463 TRACE("(%p)\n", hwave);
1465 if(!WINMM_ValidateAndLock(device))
1466 return MMSYSERR_INVALHANDLE;
1468 WINMM_CloseDevice(device);
1470 IAudioCaptureClient_Release(device->capture);
1471 device->capture = NULL;
1473 LeaveCriticalSection(&device->lock);
1475 return MMSYSERR_NOERROR;
1478 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1480 return length - length % device->bytes_per_frame;
1483 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1485 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1487 TRACE("(%p, %p)\n", hwave, header);
1489 if(!WINMM_ValidateAndLock(device))
1490 return MMSYSERR_INVALHANDLE;
1492 if(device->render && device->acm_handle){
1493 ACMSTREAMHEADER *ash;
1494 DWORD size;
1495 MMRESULT mr;
1497 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1498 ACM_STREAMSIZEF_SOURCE);
1499 if(mr != MMSYSERR_NOERROR){
1500 LeaveCriticalSection(&device->lock);
1501 return mr;
1504 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1505 if(!ash){
1506 LeaveCriticalSection(&device->lock);
1507 return MMSYSERR_NOMEM;
1510 ash->cbStruct = sizeof(*ash);
1511 ash->fdwStatus = 0;
1512 ash->dwUser = (DWORD_PTR)header;
1513 ash->pbSrc = (BYTE*)header->lpData;
1514 ash->cbSrcLength = header->dwBufferLength;
1515 ash->dwSrcUser = header->dwUser;
1516 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1517 ash->cbDstLength = size;
1518 ash->dwDstUser = 0;
1520 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1521 if(mr != MMSYSERR_NOERROR){
1522 HeapFree(GetProcessHeap(), 0, ash);
1523 LeaveCriticalSection(&device->lock);
1524 return mr;
1527 header->reserved = (DWORD_PTR)ash;
1530 LeaveCriticalSection(&device->lock);
1532 header->dwFlags |= WHDR_PREPARED;
1533 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1535 return MMSYSERR_NOERROR;
1538 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1540 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1542 TRACE("(%p, %p)\n", hwave, header);
1544 if(!WINMM_ValidateAndLock(device))
1545 return MMSYSERR_INVALHANDLE;
1547 if(device->render && device->acm_handle){
1548 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1550 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1552 HeapFree(GetProcessHeap(), 0, ash);
1555 LeaveCriticalSection(&device->lock);
1557 header->dwFlags &= ~WHDR_PREPARED;
1559 return MMSYSERR_NOERROR;
1562 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1564 if(device->acm_handle){
1565 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1566 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1569 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1572 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1574 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1577 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1579 HRESULT hr;
1580 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1581 UINT64 clock_freq, clock_pos, clock_frames;
1582 UINT32 nloops, queue_frames = 0;
1584 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1585 if(FAILED(hr)){
1586 WARN("GetFrequency failed: %08lx\n", hr);
1587 return NULL;
1590 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1591 if(FAILED(hr)){
1592 WARN("GetPosition failed: %08lx\n", hr);
1593 return NULL;
1596 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1598 nloops = device->loop_counter;
1599 while(queue &&
1600 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1601 clock_frames - device->last_clock_pos + device->remainder_frames){
1602 if(!nloops){
1603 last = queue;
1604 device->last_clock_pos += queue_frames;
1605 device->remainder_frames = 0;
1606 queue_frames = 0;
1607 queue = device->first = queue->lpNext;
1608 }else{
1609 if(queue->dwFlags & WHDR_BEGINLOOP){
1610 if(queue->dwFlags & WHDR_ENDLOOP)
1611 --nloops;
1612 else
1613 queue = queue->lpNext;
1614 }else if(queue->dwFlags & WHDR_ENDLOOP){
1615 queue = device->loop_start;
1616 --nloops;
1621 if(last){
1622 last->lpNext = NULL;
1623 return first;
1624 }else
1625 return NULL;
1628 static void WOD_PushData(WINMM_Device *device)
1630 WINMM_CBInfo cb_info;
1631 HRESULT hr;
1632 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1633 UINT32 queue_bytes, nloops;
1634 BYTE *data;
1635 WAVEHDR *queue, *first = NULL;
1637 TRACE("(%p)\n", device->handle);
1639 EnterCriticalSection(&device->lock);
1641 if(!device->device)
1642 goto exit;
1644 if(!device->first){
1645 if (device->stopped)
1646 goto exit;
1647 device->stopped = TRUE;
1648 device->last_clock_pos = 0;
1649 IAudioClient_Stop(device->client);
1650 IAudioClient_Reset(device->client);
1651 goto exit;
1654 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1655 if(FAILED(hr)){
1656 WARN("GetBufferSize failed: %08lx\n", hr);
1657 goto exit;
1660 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1661 if(FAILED(hr)){
1662 WARN("GetCurrentPadding failed: %08lx\n", hr);
1663 goto exit;
1666 first = WOD_MarkDoneHeaders(device);
1668 /* determine which is larger between the available buffer size and
1669 * the amount of data left in the queue */
1670 avail_frames = bufsize - pad;
1672 queue = device->playing;
1673 ofs = device->ofs_bytes;
1674 queue_frames = 0;
1675 nloops = 0;
1676 while(queue && queue_frames < avail_frames){
1677 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1678 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1679 ofs = 0;
1681 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1682 queue = device->loop_start;
1683 ++nloops;
1684 }else
1685 queue = queue->lpNext;
1688 if(queue_frames < avail_frames)
1689 avail_frames = queue_frames;
1690 if(avail_frames == 0)
1691 goto exit;
1693 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1694 if(FAILED(hr)){
1695 WARN("GetBuffer failed: %08lx\n", hr);
1696 goto exit;
1699 written = 0;
1700 while(device->playing && written < avail_frames){
1701 UINT32 copy_frames, copy_bytes;
1702 BYTE *queue_data;
1704 queue = device->playing;
1706 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1707 if(device->acm_handle)
1708 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1709 else
1710 queue_data = (BYTE*)queue->lpData;
1712 queue_frames = (queue_bytes - device->ofs_bytes) /
1713 device->bytes_per_frame;
1715 copy_frames = queue_frames < (avail_frames - written) ?
1716 queue_frames : avail_frames - written;
1717 copy_bytes = copy_frames * device->bytes_per_frame;
1719 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1721 data += copy_bytes;
1722 written += copy_frames;
1723 device->ofs_bytes += copy_bytes;
1725 if(device->ofs_bytes >= queue_bytes){
1726 device->ofs_bytes = 0;
1728 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1729 device->playing = queue->lpNext;
1730 else{
1731 if(queue->dwFlags & WHDR_BEGINLOOP){
1732 device->loop_start = device->playing;
1733 device->playing = queue->lpNext;
1734 device->loop_counter = queue->dwLoops;
1736 if(queue->dwFlags & WHDR_ENDLOOP){
1737 --device->loop_counter;
1738 if(device->loop_counter)
1739 device->playing = device->loop_start;
1740 else
1741 device->loop_start = device->playing = queue->lpNext;
1747 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1748 if(FAILED(hr)){
1749 WARN("ReleaseBuffer failed: %08lx\n", hr);
1750 goto exit;
1753 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1754 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1755 else
1756 device->played_frames += avail_frames;
1758 exit:
1759 cb_info = device->cb_info;
1761 LeaveCriticalSection(&device->lock);
1763 while(first){
1764 WAVEHDR *next = first->lpNext;
1765 first->dwFlags &= ~WHDR_INQUEUE;
1766 first->dwFlags |= WHDR_DONE;
1767 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1768 first = next;
1772 static void WID_PullACMData(WINMM_Device *device)
1774 UINT32 packet;
1775 DWORD packet_bytes;
1776 DWORD flags;
1777 BYTE *data;
1778 WAVEHDR *queue;
1779 HRESULT hr;
1780 MMRESULT mr;
1782 if(device->acm_hdr.cbDstLength == 0){
1783 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1784 &flags, NULL, NULL);
1785 if(hr != S_OK){
1786 if(FAILED(hr))
1787 WARN("GetBuffer failed: %08lx\n", hr);
1788 return;
1791 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1792 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1794 device->acm_offs = 0;
1796 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1797 device->acm_hdr.fdwStatus = 0;
1798 device->acm_hdr.dwUser = 0;
1799 device->acm_hdr.pbSrc = data;
1800 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1801 device->acm_hdr.cbSrcLengthUsed = 0;
1802 device->acm_hdr.dwSrcUser = 0;
1803 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1804 device->acm_hdr.cbDstLength = packet_bytes;
1805 device->acm_hdr.cbDstLengthUsed = 0;
1806 device->acm_hdr.dwDstUser = 0;
1808 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1809 if(mr != MMSYSERR_NOERROR){
1810 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1811 return;
1814 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1815 if(mr != MMSYSERR_NOERROR){
1816 WARN("acmStreamConvert failed: %d\n", mr);
1817 return;
1820 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1821 if(FAILED(hr))
1822 WARN("ReleaseBuffer failed: %08lx\n", hr);
1824 device->played_frames += packet;
1827 queue = device->first;
1828 while(queue){
1829 UINT32 to_copy_bytes;
1831 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1832 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1834 memcpy(queue->lpData + queue->dwBytesRecorded,
1835 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1837 queue->dwBytesRecorded += to_copy_bytes;
1838 device->acm_offs += to_copy_bytes;
1840 if(queue->dwBufferLength - queue->dwBytesRecorded <
1841 device->bytes_per_frame){
1842 queue->dwFlags &= ~WHDR_INQUEUE;
1843 queue->dwFlags |= WHDR_DONE;
1844 device->first = queue = queue->lpNext;
1847 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1848 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1849 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1850 device->acm_hdr.cbDstLength = 0;
1851 device->acm_hdr.cbDstLengthUsed = 0;
1853 /* done with this ACM Header, so try to pull more data */
1854 WID_PullACMData(device);
1855 return;
1859 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1860 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1861 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1862 device->acm_hdr.cbDstLength = 0;
1863 device->acm_hdr.cbDstLengthUsed = 0;
1866 static void WID_PullData(WINMM_Device *device)
1868 WINMM_CBInfo cb_info;
1869 WAVEHDR *queue, *first = NULL, *last = NULL;
1870 HRESULT hr;
1872 TRACE("(%p)\n", device->handle);
1874 EnterCriticalSection(&device->lock);
1876 if(!device->device || !device->first)
1877 goto exit;
1879 first = device->first;
1881 if(device->acm_handle){
1882 WID_PullACMData(device);
1883 goto exit;
1886 while(device->first){
1887 BYTE *data;
1888 UINT32 packet_len, packet;
1889 DWORD flags;
1891 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1892 &flags, NULL, NULL);
1893 if(hr != S_OK){
1894 if(FAILED(hr))
1895 WARN("GetBuffer failed: %08lx\n", hr);
1896 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1897 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1898 goto exit;
1901 packet = packet_len;
1902 queue = device->first;
1903 while(queue && packet > 0){
1904 UINT32 to_copy_bytes;
1906 to_copy_bytes = min(packet * device->bytes_per_frame,
1907 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1909 memcpy(queue->lpData + queue->dwBytesRecorded,
1910 data + (packet_len - packet) * device->bytes_per_frame,
1911 to_copy_bytes);
1913 queue->dwBytesRecorded += to_copy_bytes;
1915 if(queue->dwBufferLength - queue->dwBytesRecorded <
1916 device->bytes_per_frame){
1917 last = queue;
1918 device->first = queue = queue->lpNext;
1921 packet -= to_copy_bytes / device->bytes_per_frame;
1924 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1925 if(FAILED(hr))
1926 WARN("ReleaseBuffer failed: %08lx\n", hr);
1928 if(packet > 0)
1929 WARN("losing %u frames\n", packet);
1930 device->played_frames += packet_len - packet;
1933 exit:
1934 cb_info = device->cb_info;
1936 LeaveCriticalSection(&device->lock);
1938 if(last){
1939 last->lpNext = NULL;
1940 while(first){
1941 WAVEHDR *next = first->lpNext;
1942 first->dwFlags &= ~WHDR_INQUEUE;
1943 first->dwFlags |= WHDR_DONE;
1944 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1945 first = next;
1950 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1952 HRESULT hr;
1954 TRACE("(%p)\n", device->handle);
1956 if(device->stopped){
1957 device->stopped = FALSE;
1959 hr = IAudioClient_Start(device->client);
1960 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1961 device->stopped = TRUE;
1962 WARN("Start failed: %08lx\n", hr);
1963 return MMSYSERR_ERROR;
1967 return MMSYSERR_NOERROR;
1970 static LRESULT WINMM_Pause(WINMM_Device *device)
1972 HRESULT hr;
1974 TRACE("(%p)\n", device->handle);
1976 hr = IAudioClient_Stop(device->client);
1977 if(FAILED(hr)){
1978 WARN("Stop failed: %08lx\n", hr);
1979 return MMSYSERR_ERROR;
1982 device->stopped = FALSE;
1984 return MMSYSERR_NOERROR;
1987 static LRESULT WINMM_Reset(HWAVE hwave)
1989 WINMM_CBInfo cb_info;
1990 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1991 BOOL is_out;
1992 WAVEHDR *first;
1993 HRESULT hr;
1995 TRACE("(%p)\n", hwave);
1997 if(!WINMM_ValidateAndLock(device))
1998 return MMSYSERR_INVALHANDLE;
2000 hr = IAudioClient_Stop(device->client);
2001 if(FAILED(hr)){
2002 LeaveCriticalSection(&device->lock);
2003 WARN("Stop failed: %08lx\n", hr);
2004 return MMSYSERR_ERROR;
2006 device->stopped = TRUE;
2008 first = device->first;
2009 device->first = device->last = device->playing = NULL;
2010 device->ofs_bytes = 0;
2011 device->played_frames = 0;
2012 device->loop_counter = 0;
2013 device->last_clock_pos = 0;
2014 IAudioClient_Reset(device->client);
2016 cb_info = device->cb_info;
2017 is_out = device->render != NULL;
2019 LeaveCriticalSection(&device->lock);
2021 while(first){
2022 WAVEHDR *next = first->lpNext;
2023 first->dwFlags &= ~WHDR_INQUEUE;
2024 first->dwFlags |= WHDR_DONE;
2025 if(is_out)
2026 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2027 else
2028 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2029 first = next;
2032 return MMSYSERR_NOERROR;
2035 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2036 UINT32 sample_rate, UINT32 bytes_per_sec)
2038 switch(time->wType){
2039 case TIME_SAMPLES:
2040 time->u.sample = played_frames;
2041 return MMSYSERR_NOERROR;
2042 default:
2043 time->wType = TIME_BYTES;
2044 /* fall through */
2045 case TIME_BYTES:
2046 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2047 return MMSYSERR_NOERROR;
2051 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2053 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2054 UINT32 played_frames, sample_rate, bytes_per_sec;
2056 TRACE("(%p, %p)\n", hwave, time);
2058 if(!WINMM_ValidateAndLock(device))
2059 return MMSYSERR_INVALHANDLE;
2061 played_frames = device->played_frames;
2062 sample_rate = device->orig_fmt->nSamplesPerSec;
2063 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2065 LeaveCriticalSection(&device->lock);
2067 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2070 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2071 UINT *mmdev_index)
2073 UINT mmdev, dev, junk, *out;
2074 BOOL is_out;
2076 if(!mmdev_index)
2077 out = &mmdev;
2078 else
2079 out = mmdev_index;
2081 switch(flags & 0xF0000000){
2082 case MIXER_OBJECTF_MIXER: /* == 0 */
2083 *out = HandleToULong(hmix);
2084 if(*out < g_outmmdevices_count)
2085 return read_map(g_out_map, *out);
2086 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2087 *out -= g_outmmdevices_count;
2088 return read_map(g_in_map, *out);
2090 /* fall through -- if it's not a valid mixer device, then
2091 * it could be a valid mixer handle. windows seems to do
2092 * this as well. */
2093 case MIXER_OBJECTF_HMIXER:
2094 case MIXER_OBJECTF_HWAVEOUT:
2095 case MIXER_OBJECTF_HWAVEIN:
2096 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2097 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2098 (!is_out && *out >= g_inmmdevices_count))
2099 return NULL;
2100 if(is_out)
2101 return read_map(g_out_map, *out);
2102 return read_map(g_in_map, *out);
2103 case MIXER_OBJECTF_WAVEOUT:
2104 *out = HandleToULong(hmix);
2105 if(*out < g_outmmdevices_count)
2106 return read_map(g_out_map, *out);
2107 return NULL;
2108 case MIXER_OBJECTF_WAVEIN:
2109 *out = HandleToULong(hmix);
2110 if(*out < g_inmmdevices_count)
2111 return read_map(g_in_map, *out);
2112 return NULL;
2115 return NULL;
2118 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2120 IAudioSessionManager *sesman;
2121 IMMDevice *device;
2122 HRESULT hr;
2124 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2125 if(FAILED(hr)){
2126 WARN("Device %s (%s) unavailable: %08lx\n",
2127 wine_dbgstr_w(mmdevice->dev_id),
2128 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2129 return MMSYSERR_ERROR;
2132 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2133 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2134 if(FAILED(hr)){
2135 WARN("Activate failed: %08lx\n", hr);
2136 IMMDevice_Release(device);
2137 return MMSYSERR_ERROR;
2140 IMMDevice_Release(device);
2142 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2143 FALSE, &mmdevice->volume);
2144 IAudioSessionManager_Release(sesman);
2145 if(FAILED(hr)){
2146 WARN("GetSimpleAudioVolume failed: %08lx\n", hr);
2147 return MMSYSERR_ERROR;
2150 return MMSYSERR_NOERROR;
2153 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2155 WINMM_MMDevice *mmdevice;
2156 MIXERCONTROLDETAILS *control = details->details;
2157 HRESULT hr;
2159 TRACE("(%p)\n", details->hmix);
2161 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2162 if(!mmdevice)
2163 return MMSYSERR_INVALHANDLE;
2165 EnterCriticalSection(&mmdevice->lock);
2167 if(!mmdevice->volume){
2168 MMRESULT mr;
2170 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2171 if(mr != MMSYSERR_NOERROR){
2172 LeaveCriticalSection(&mmdevice->lock);
2173 return mr;
2177 if(control->dwControlID == 0){
2178 float vol;
2179 MIXERCONTROLDETAILS_UNSIGNED *udet;
2181 if(!control->paDetails ||
2182 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2183 LeaveCriticalSection(&mmdevice->lock);
2184 return MMSYSERR_INVALPARAM;
2187 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2188 if(FAILED(hr)){
2189 WARN("GetMasterVolume failed: %08lx\n", hr);
2190 LeaveCriticalSection(&mmdevice->lock);
2191 return MMSYSERR_ERROR;
2194 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2195 udet->dwValue = vol * ((unsigned int)0xFFFF);
2196 }else if(control->dwControlID == 1){
2197 BOOL mute;
2198 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2200 if(!control->paDetails ||
2201 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2202 LeaveCriticalSection(&mmdevice->lock);
2203 return MMSYSERR_INVALPARAM;
2206 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2207 if(FAILED(hr)){
2208 WARN("GetMute failed: %08lx\n", hr);
2209 LeaveCriticalSection(&mmdevice->lock);
2210 return MMSYSERR_ERROR;
2213 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2214 bdet->fValue = mute;
2215 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2216 FIXME("What should the sw-side mixer controls map to?\n");
2217 }else{
2218 LeaveCriticalSection(&mmdevice->lock);
2219 return MIXERR_INVALCONTROL;
2222 LeaveCriticalSection(&mmdevice->lock);
2224 return MMSYSERR_NOERROR;
2227 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2229 WINMM_MMDevice *mmdevice;
2230 MIXERCONTROLDETAILS *control = details->details;
2231 HRESULT hr;
2233 TRACE("(%p)\n", details->hmix);
2235 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2236 if(!mmdevice)
2237 return MMSYSERR_INVALHANDLE;
2239 EnterCriticalSection(&mmdevice->lock);
2241 if(!mmdevice->volume){
2242 MMRESULT mr;
2244 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2245 if(mr != MMSYSERR_NOERROR){
2246 LeaveCriticalSection(&mmdevice->lock);
2247 return mr;
2251 if(control->dwControlID == 0){
2252 float vol;
2253 MIXERCONTROLDETAILS_UNSIGNED *udet;
2255 if(!control->paDetails ||
2256 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2257 LeaveCriticalSection(&mmdevice->lock);
2258 return MMSYSERR_INVALPARAM;
2261 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2263 if(udet->dwValue > 65535){
2264 LeaveCriticalSection(&mmdevice->lock);
2265 return MMSYSERR_INVALPARAM;
2268 vol = udet->dwValue / 65535.f;
2270 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2271 if(FAILED(hr)){
2272 WARN("SetMasterVolume failed: %08lx\n", hr);
2273 LeaveCriticalSection(&mmdevice->lock);
2274 return MMSYSERR_ERROR;
2276 }else if(control->dwControlID == 1){
2277 BOOL mute;
2278 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2280 if(!control->paDetails ||
2281 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2282 LeaveCriticalSection(&mmdevice->lock);
2283 return MMSYSERR_INVALPARAM;
2286 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2287 mute = bdet->fValue;
2289 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2290 if(FAILED(hr)){
2291 WARN("SetMute failed: %08lx\n", hr);
2292 LeaveCriticalSection(&mmdevice->lock);
2293 return MMSYSERR_ERROR;
2295 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2296 FIXME("What should the sw-side mixer controls map to?\n");
2297 }else{
2298 LeaveCriticalSection(&mmdevice->lock);
2299 return MIXERR_INVALCONTROL;
2302 LeaveCriticalSection(&mmdevice->lock);
2304 return MMSYSERR_NOERROR;
2307 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2309 WINMM_MMDevice *mmdevice;
2310 IMMDevice *device;
2311 IPropertyStore *ps;
2312 PROPVARIANT pv;
2313 DWORD len_bytes;
2314 HRESULT hr;
2316 static const PROPERTYKEY deviceinterface_key = {
2317 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2320 if(WINMM_IsMapper(info->index)){
2321 if(info->str){
2322 if(*info->len_bytes < sizeof(WCHAR))
2323 return MMSYSERR_INVALPARAM;
2324 *info->str = 0;
2325 }else
2326 *info->len_bytes = sizeof(WCHAR);
2327 return MMSYSERR_NOERROR;
2330 if(info->is_out){
2331 if(info->index >= g_outmmdevices_count)
2332 return MMSYSERR_INVALHANDLE;
2334 mmdevice = &g_out_mmdevices[info->index];
2335 }else{
2336 if(info->index >= g_inmmdevices_count)
2337 return MMSYSERR_INVALHANDLE;
2339 mmdevice = &g_in_mmdevices[info->index];
2342 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2343 &device);
2344 if(FAILED(hr)){
2345 WARN("Device %s unavailable: %08lx\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2346 return MMSYSERR_ERROR;
2349 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2350 if(FAILED(hr)){
2351 WARN("OpenPropertyStore failed: %08lx\n", hr);
2352 IMMDevice_Release(device);
2353 return MMSYSERR_ERROR;
2356 PropVariantInit(&pv);
2357 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2358 if(FAILED(hr)){
2359 WARN("GetValue failed: %08lx\n", hr);
2360 IPropertyStore_Release(ps);
2361 IMMDevice_Release(device);
2362 return MMSYSERR_ERROR;
2364 if(pv.vt != VT_LPWSTR){
2365 WARN("Got unexpected property type: %u\n", pv.vt);
2366 PropVariantClear(&pv);
2367 IPropertyStore_Release(ps);
2368 IMMDevice_Release(device);
2369 return MMSYSERR_ERROR;
2372 len_bytes = (lstrlenW(pv.pwszVal) + 1) * sizeof(WCHAR);
2374 if(info->str){
2375 if(len_bytes > *info->len_bytes){
2376 PropVariantClear(&pv);
2377 IPropertyStore_Release(ps);
2378 IMMDevice_Release(device);
2379 return MMSYSERR_INVALPARAM;
2382 memcpy(info->str, pv.pwszVal, len_bytes);
2383 }else
2384 *info->len_bytes = len_bytes;
2386 PropVariantClear(&pv);
2387 IPropertyStore_Release(ps);
2388 IMMDevice_Release(device);
2390 return MMSYSERR_NOERROR;
2393 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2394 LPARAM lparam)
2396 switch(msg){
2397 case WODM_OPEN:
2398 return WOD_Open((WINMM_OpenInfo*)wparam);
2399 case WODM_CLOSE:
2400 return WOD_Close((HWAVEOUT)wparam);
2401 case WIDM_OPEN:
2402 return WID_Open((WINMM_OpenInfo*)wparam);
2403 case WIDM_CLOSE:
2404 return WID_Close((HWAVEIN)wparam);
2405 case MXDM_GETCONTROLDETAILS:
2406 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2407 case MXDM_SETCONTROLDETAILS:
2408 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2409 case DRV_QUERYDEVICEINTERFACESIZE:
2410 case DRV_QUERYDEVICEINTERFACE:
2411 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2413 return DefWindowProcW(hwnd, msg, wparam, lparam);
2416 static BOOL WINMM_DevicesThreadDone(void)
2418 UINT i;
2420 EnterCriticalSection(&g_devthread_lock);
2422 if(g_devthread_token > 0){
2423 LeaveCriticalSection(&g_devthread_lock);
2424 return FALSE;
2427 for(i = 0; i < g_devhandle_count; ++i){
2428 if(g_handle_devices[i]->open){
2429 LeaveCriticalSection(&g_devthread_lock);
2430 return FALSE;
2434 DestroyWindow(g_devices_hwnd);
2435 g_devices_hwnd = NULL;
2436 IMMDeviceEnumerator_Release(g_devenum);
2437 g_devenum = NULL;
2438 CoUninitialize();
2440 LeaveCriticalSection(&g_devthread_lock);
2442 return TRUE;
2445 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2447 HANDLE evt = arg;
2448 HRESULT hr;
2450 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2451 if(FAILED(hr)){
2452 WARN("CoInitializeEx failed: %08lx\n", hr);
2453 FreeLibraryAndExitThread(g_devthread_module, 1);
2456 hr = WINMM_InitMMDevices();
2457 if(FAILED(hr)){
2458 CoUninitialize();
2459 FreeLibraryAndExitThread(g_devthread_module, 1);
2462 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2463 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2464 if(FAILED(hr)){
2465 WARN("CoCreateInstance failed: %08lx\n", hr);
2466 CoUninitialize();
2467 FreeLibraryAndExitThread(g_devthread_module, 1);
2470 g_devices_hwnd = CreateWindowW(L"Message", NULL, 0, 0, 0, 0, 0,
2471 HWND_MESSAGE, NULL, NULL, NULL);
2472 if(!g_devices_hwnd){
2473 WARN("CreateWindow failed: %ld\n", GetLastError());
2474 CoUninitialize();
2475 FreeLibraryAndExitThread(g_devthread_module, 1);
2478 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2479 (LONG_PTR)WINMM_DevicesMsgProc);
2481 /* inform caller that the thread is ready to process messages */
2482 SetEvent(evt);
2483 evt = NULL; /* do not use after this point */
2485 while(1){
2486 DWORD wait;
2487 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2488 FALSE, INFINITE, QS_ALLINPUT);
2489 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2490 MSG msg;
2491 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2492 WARN("Unexpected message: 0x%x\n", msg.message);
2493 if(!g_devices_hwnd)
2494 break;
2495 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2496 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2497 if(device->render)
2498 WOD_PushData(device);
2499 else
2500 WID_PullData(device);
2501 }else
2502 WARN("Unexpected MsgWait result 0x%lx, GLE: %ld\n", wait,
2503 GetLastError());
2504 if(WINMM_DevicesThreadDone()){
2505 TRACE("Quitting devices thread\n");
2506 FreeLibraryAndExitThread(g_devthread_module, 0);
2510 FreeLibraryAndExitThread(g_devthread_module, 0);
2513 /* on success, increments g_devthread_token to prevent
2514 * device thread shutdown. caller must decrement. */
2515 static BOOL WINMM_StartDevicesThread(void)
2517 HANDLE events[2];
2518 DWORD wait;
2520 EnterCriticalSection(&g_devthread_lock);
2522 if(g_devices_hwnd){
2523 wait = WaitForSingleObject(g_devices_thread, 0);
2524 if(wait == WAIT_TIMEOUT){
2525 /* thread still running */
2526 InterlockedIncrement(&g_devthread_token);
2527 LeaveCriticalSection(&g_devthread_lock);
2528 return TRUE;
2530 if(wait != WAIT_OBJECT_0){
2531 /* error */
2532 LeaveCriticalSection(&g_devthread_lock);
2533 return FALSE;
2535 TRACE("Devices thread left dangling message window?\n");
2536 g_devices_hwnd = NULL;
2537 CloseHandle(g_devices_thread);
2538 g_devices_thread = NULL;
2539 }else if(g_devices_thread){
2540 WaitForSingleObject(g_devices_thread, INFINITE);
2541 CloseHandle(g_devices_thread);
2542 g_devices_thread = NULL;
2545 TRACE("Starting up devices thread\n");
2547 /* The devices thread holds a reference to the winmm module
2548 * to prevent it from unloading while it's running. */
2549 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2550 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2552 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2554 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2555 events[0], 0, NULL);
2556 if(!g_devices_thread){
2557 LeaveCriticalSection(&g_devthread_lock);
2558 CloseHandle(events[0]);
2559 FreeLibrary(g_devthread_module);
2560 return FALSE;
2563 events[1] = g_devices_thread;
2564 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2565 CloseHandle(events[0]);
2566 if(wait != WAIT_OBJECT_0){
2567 if(wait == 1 + WAIT_OBJECT_0){
2568 CloseHandle(g_devices_thread);
2569 g_devices_thread = NULL;
2570 g_devices_hwnd = NULL;
2572 LeaveCriticalSection(&g_devthread_lock);
2573 return FALSE;
2576 InterlockedIncrement(&g_devthread_token);
2578 LeaveCriticalSection(&g_devthread_lock);
2580 return TRUE;
2583 /**************************************************************************
2584 * waveOutGetNumDevs [WINMM.@]
2586 UINT WINAPI waveOutGetNumDevs(void)
2588 HRESULT hr = WINMM_InitMMDevices();
2589 if(FAILED(hr))
2590 return 0;
2592 TRACE("count: %u\n", g_outmmdevices_count);
2594 return g_outmmdevices_count;
2597 /**************************************************************************
2598 * waveOutGetDevCapsA [WINMM.@]
2600 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2601 UINT uSize)
2603 WAVEOUTCAPSW wocW;
2604 UINT ret;
2606 TRACE("(%Iu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2608 if(!lpCaps)
2609 return MMSYSERR_INVALPARAM;
2611 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2613 if (ret == MMSYSERR_NOERROR) {
2614 WAVEOUTCAPSA wocA;
2615 wocA.wMid = wocW.wMid;
2616 wocA.wPid = wocW.wPid;
2617 wocA.vDriverVersion = wocW.vDriverVersion;
2618 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2619 sizeof(wocA.szPname), NULL, NULL );
2620 wocA.dwFormats = wocW.dwFormats;
2621 wocA.wChannels = wocW.wChannels;
2622 wocA.dwSupport = wocW.dwSupport;
2623 wocA.wReserved1 = wocW.wReserved1;
2624 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2626 return ret;
2629 /**************************************************************************
2630 * waveOutGetDevCapsW [WINMM.@]
2632 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2633 UINT uSize)
2635 WAVEOUTCAPSW mapper_caps, *caps;
2636 HRESULT hr;
2638 TRACE("(%Iu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2640 hr = WINMM_InitMMDevices();
2641 if(FAILED(hr))
2642 return MMSYSERR_NODRIVER;
2644 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2646 if(WINMM_IsMapper(uDeviceID)){
2647 mapper_caps.wMid = 0xFF;
2648 mapper_caps.wPid = 0xFF;
2649 mapper_caps.vDriverVersion = 0x00010001;
2650 mapper_caps.dwFormats = 0xFFFFFFFF;
2651 mapper_caps.wReserved1 = 0;
2652 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2653 WAVECAPS_SAMPLEACCURATE;
2654 mapper_caps.wChannels = 2;
2655 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
2657 caps = &mapper_caps;
2658 }else{
2659 if(uDeviceID >= g_outmmdevices_count){
2660 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)uDeviceID);
2662 if(!WINMM_ValidateAndLock(device))
2663 return MMSYSERR_BADDEVICEID;
2665 caps = &device->parent->out_caps;
2667 LeaveCriticalSection(&device->lock);
2668 }else{
2669 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2673 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2675 return MMSYSERR_NOERROR;
2678 /**************************************************************************
2679 * waveOutGetErrorTextA [WINMM.@]
2680 * waveInGetErrorTextA [WINMM.@]
2682 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2684 UINT ret;
2686 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2687 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2688 else
2690 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2691 if (!xstr) ret = MMSYSERR_NOMEM;
2692 else
2694 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2695 if (ret == MMSYSERR_NOERROR)
2696 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2697 HeapFree(GetProcessHeap(), 0, xstr);
2700 return ret;
2703 /**************************************************************************
2704 * waveOutGetErrorTextW [WINMM.@]
2705 * waveInGetErrorTextW [WINMM.@]
2707 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2709 UINT ret = MMSYSERR_BADERRNUM;
2711 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2712 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2713 else if (
2714 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2715 * a warning for the test was always true */
2716 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2717 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2718 if (LoadStringW(hWinMM32Instance,
2719 uError, lpText, uSize) > 0) {
2720 ret = MMSYSERR_NOERROR;
2723 return ret;
2726 /**************************************************************************
2727 * waveOutOpen [WINMM.@]
2728 * All the args/structs have the same layout as the win16 equivalents
2730 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2731 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2732 DWORD_PTR dwInstance, DWORD dwFlags)
2734 LRESULT res;
2735 WINMM_OpenInfo info;
2736 WINMM_CBInfo cb_info;
2738 TRACE("(%p, %u, %p, %Ix, %Ix, %08lx)\n", lphWaveOut, uDeviceID, lpFormat,
2739 dwCallback, dwInstance, dwFlags);
2741 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2742 return MMSYSERR_INVALPARAM;
2744 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2745 if(res != MMSYSERR_NOERROR)
2746 return res;
2748 if(!WINMM_StartDevicesThread())
2749 return MMSYSERR_NODRIVER;
2751 info.handle = 0;
2752 info.format = (WAVEFORMATEX*)lpFormat;
2753 info.callback = dwCallback;
2754 info.cb_user = dwInstance;
2755 info.req_device = uDeviceID;
2756 info.flags = dwFlags;
2757 info.reset = TRUE;
2759 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2760 InterlockedDecrement(&g_devthread_token);
2761 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2762 return res;
2764 if(lphWaveOut)
2765 *lphWaveOut = (HWAVEOUT)info.handle;
2767 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2768 cb_info.callback = dwCallback;
2769 cb_info.user = dwInstance;
2770 cb_info.hwave = info.handle;
2772 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2774 return res;
2777 /**************************************************************************
2778 * waveOutClose [WINMM.@]
2780 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2782 UINT res;
2783 WINMM_Device *device;
2784 WINMM_CBInfo cb_info;
2786 TRACE("(%p)\n", hWaveOut);
2788 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2790 if(!WINMM_ValidateAndLock(device))
2791 return MMSYSERR_INVALHANDLE;
2793 cb_info = device->cb_info;
2795 LeaveCriticalSection(&device->lock);
2797 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2799 if(res == MMSYSERR_NOERROR)
2800 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2802 return res;
2805 /**************************************************************************
2806 * waveOutPrepareHeader [WINMM.@]
2808 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2809 WAVEHDR* lpWaveOutHdr, UINT uSize)
2811 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2813 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2814 return MMSYSERR_INVALPARAM;
2816 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2817 return MMSYSERR_NOERROR;
2819 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2822 /**************************************************************************
2823 * waveOutUnprepareHeader [WINMM.@]
2825 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2826 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2828 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2830 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2831 return MMSYSERR_INVALPARAM;
2833 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2834 return WAVERR_STILLPLAYING;
2836 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2837 return MMSYSERR_NOERROR;
2839 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2842 /**************************************************************************
2843 * waveOutWrite [WINMM.@]
2845 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2847 WINMM_Device *device;
2848 MMRESULT mr;
2850 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2852 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2854 if(!WINMM_ValidateAndLock(device))
2855 return MMSYSERR_INVALHANDLE;
2857 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2858 LeaveCriticalSection(&device->lock);
2859 return WAVERR_UNPREPARED;
2862 if(header->dwFlags & WHDR_INQUEUE){
2863 LeaveCriticalSection(&device->lock);
2864 return WAVERR_STILLPLAYING;
2867 TRACE("dwBufferLength: %lu\n", header->dwBufferLength);
2869 if(device->acm_handle){
2870 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2872 ash->cbSrcLength = header->dwBufferLength;
2873 mr = acmStreamConvert(device->acm_handle, ash, 0);
2874 if(mr != MMSYSERR_NOERROR){
2875 LeaveCriticalSection(&device->lock);
2876 return mr;
2880 if(device->first){
2881 device->last->lpNext = header;
2882 device->last = header;
2883 if(!device->playing)
2884 device->playing = header;
2885 }else{
2886 device->playing = device->first = device->last = header;
2887 if(header->dwFlags & WHDR_BEGINLOOP){
2888 device->loop_counter = header->dwLoops;
2889 device->loop_start = header;
2893 header->lpNext = NULL;
2894 header->dwFlags &= ~WHDR_DONE;
2895 header->dwFlags |= WHDR_INQUEUE;
2897 if(device->stopped)
2898 WOD_PushData(device);
2900 mr = WINMM_BeginPlaying(device);
2902 LeaveCriticalSection(&device->lock);
2904 return mr;
2907 /**************************************************************************
2908 * waveOutBreakLoop [WINMM.@]
2910 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2912 WINMM_Device *device;
2914 TRACE("(%p)\n", hWaveOut);
2916 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2918 if(!WINMM_ValidateAndLock(device))
2919 return MMSYSERR_INVALHANDLE;
2921 device->loop_counter = 0;
2923 LeaveCriticalSection(&device->lock);
2925 return MMSYSERR_NOERROR;
2928 /**************************************************************************
2929 * waveOutPause [WINMM.@]
2931 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2933 WINMM_Device *device;
2934 MMRESULT mr;
2936 TRACE("(%p)\n", hWaveOut);
2938 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2940 if(!WINMM_ValidateAndLock(device))
2941 return MMSYSERR_INVALHANDLE;
2943 mr = WINMM_Pause(device);
2945 LeaveCriticalSection(&device->lock);
2947 return mr;
2950 /**************************************************************************
2951 * waveOutReset [WINMM.@]
2953 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2955 TRACE("(%p)\n", hWaveOut);
2957 return WINMM_Reset((HWAVE)hWaveOut);
2960 /**************************************************************************
2961 * waveOutRestart [WINMM.@]
2963 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2965 WINMM_Device *device;
2966 MMRESULT mr;
2968 TRACE("(%p)\n", hWaveOut);
2970 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2972 if(!WINMM_ValidateAndLock(device))
2973 return MMSYSERR_INVALHANDLE;
2975 device->stopped = TRUE;
2977 if(device->render)
2978 WOD_PushData(device);
2980 mr = WINMM_BeginPlaying(device);
2982 LeaveCriticalSection(&device->lock);
2984 return mr;
2987 /**************************************************************************
2988 * waveOutGetPosition [WINMM.@]
2990 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2991 UINT uSize)
2993 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2995 if(!uSize || !lpTime)
2996 return MMSYSERR_INVALPARAM;
2998 if(uSize < sizeof(MMTIME))
2999 return MMSYSERR_ERROR;
3001 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
3004 /**************************************************************************
3005 * waveOutGetPitch [WINMM.@]
3007 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
3009 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3010 return MMSYSERR_NOTSUPPORTED;
3013 /**************************************************************************
3014 * waveOutSetPitch [WINMM.@]
3016 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3018 TRACE("(%p, %08lx)\n", hWaveOut, dw);
3020 return MMSYSERR_NOTSUPPORTED;
3023 /**************************************************************************
3024 * waveOutGetPlaybackRate [WINMM.@]
3026 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3028 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3030 return MMSYSERR_NOTSUPPORTED;
3033 /**************************************************************************
3034 * waveOutSetPlaybackRate [WINMM.@]
3036 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3038 TRACE("(%p, %08lx)\n", hWaveOut, dw);
3040 return MMSYSERR_NOTSUPPORTED;
3043 /**************************************************************************
3044 * waveOutGetVolume [WINMM.@]
3046 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3048 WINMM_Device *device;
3049 UINT32 channels;
3050 float *vols;
3051 HRESULT hr;
3053 TRACE("(%p, %p)\n", hWaveOut, out);
3055 if(!out)
3056 return MMSYSERR_INVALPARAM;
3058 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3060 if(!WINMM_ValidateAndLock(device))
3061 return MMSYSERR_INVALHANDLE;
3063 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3064 if(FAILED(hr)){
3065 LeaveCriticalSection(&device->lock);
3066 WARN("GetChannelCount failed: %08lx\n", hr);
3067 return MMSYSERR_ERROR;
3070 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3071 if(!vols){
3072 LeaveCriticalSection(&device->lock);
3073 return MMSYSERR_NOMEM;
3076 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3077 if(FAILED(hr)){
3078 LeaveCriticalSection(&device->lock);
3079 HeapFree(GetProcessHeap(), 0, vols);
3080 WARN("GetAllVolumes failed: %08lx\n", hr);
3081 return MMSYSERR_ERROR;
3084 LeaveCriticalSection(&device->lock);
3086 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3087 if(channels > 1)
3088 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3090 HeapFree(GetProcessHeap(), 0, vols);
3092 return MMSYSERR_NOERROR;
3095 /**************************************************************************
3096 * waveOutSetVolume [WINMM.@]
3098 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3100 WINMM_Device *device;
3101 UINT32 channels;
3102 float *vols;
3103 HRESULT hr;
3105 TRACE("(%p, %08lx)\n", hWaveOut, in);
3107 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3109 if(!WINMM_ValidateAndLock(device))
3110 return MMSYSERR_INVALHANDLE;
3112 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3113 if(FAILED(hr)){
3114 LeaveCriticalSection(&device->lock);
3115 WARN("GetChannelCount failed: %08lx\n", hr);
3116 return MMSYSERR_ERROR;
3119 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3120 if(!vols){
3121 LeaveCriticalSection(&device->lock);
3122 return MMSYSERR_NOMEM;
3125 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3126 if(FAILED(hr)){
3127 LeaveCriticalSection(&device->lock);
3128 HeapFree(GetProcessHeap(), 0, vols);
3129 WARN("GetAllVolumes failed: %08lx\n", hr);
3130 return MMSYSERR_ERROR;
3133 vols[0] = (float)((in & 0xFFFF) / (float)0xFFFF);
3134 if(channels > 1)
3135 vols[1] = (float)((in >> 16) / (float)0xFFFF);
3137 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3138 if(FAILED(hr)){
3139 LeaveCriticalSection(&device->lock);
3140 HeapFree(GetProcessHeap(), 0, vols);
3141 WARN("SetAllVolumes failed: %08lx\n", hr);
3142 return MMSYSERR_ERROR;
3145 LeaveCriticalSection(&device->lock);
3147 HeapFree(GetProcessHeap(), 0, vols);
3149 return MMSYSERR_NOERROR;
3152 /**************************************************************************
3153 * waveOutGetID [WINMM.@]
3155 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3157 WINMM_Device *device;
3158 UINT dev, junk;
3159 BOOL is_out;
3161 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3163 if(!lpuDeviceID)
3164 return MMSYSERR_INVALPARAM;
3166 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3167 if(!WINMM_ValidateAndLock(device))
3168 return MMSYSERR_INVALHANDLE;
3170 LeaveCriticalSection(&device->lock);
3172 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3174 return MMSYSERR_NOERROR;
3177 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3179 UINT count;
3180 WINMM_MMDevice **devices;
3182 TRACE("(%u, %p, %d)\n", device, len, is_out);
3184 if(is_out){
3185 count = g_outmmdevices_count;
3186 devices = g_out_map;
3187 }else{
3188 count = g_inmmdevices_count;
3189 devices = g_in_map;
3192 if(device >= count)
3193 return MMSYSERR_INVALHANDLE;
3195 EnterCriticalSection(&g_devthread_lock);
3196 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3197 LeaveCriticalSection(&g_devthread_lock);
3199 return MMSYSERR_NOERROR;
3202 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3203 BOOL is_out)
3205 UINT count, id_len;
3206 WINMM_MMDevice **devices;
3208 TRACE("(%u, %p, %d)\n", device, str, is_out);
3210 if(is_out){
3211 count = g_outmmdevices_count;
3212 devices = g_out_map;
3213 }else{
3214 count = g_inmmdevices_count;
3215 devices = g_in_map;
3218 if(device >= count)
3219 return MMSYSERR_INVALHANDLE;
3221 EnterCriticalSection(&g_devthread_lock);
3222 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3223 if(len < id_len){
3224 LeaveCriticalSection(&g_devthread_lock);
3225 return MMSYSERR_ERROR;
3228 memcpy(str, devices[device]->dev_id, id_len);
3229 LeaveCriticalSection(&g_devthread_lock);
3231 return MMSYSERR_NOERROR;
3234 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3236 WINMM_QueryInterfaceInfo info;
3237 UINT ret;
3239 if(!WINMM_StartDevicesThread())
3240 return MMSYSERR_NODRIVER;
3242 info.is_out = is_out;
3243 info.index = index;
3244 info.str = out;
3245 info.len_bytes = out_len;
3247 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3248 InterlockedDecrement(&g_devthread_token);
3249 return ret;
3252 /**************************************************************************
3253 * waveOutMessage [WINMM.@]
3255 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3256 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3258 TRACE("(%p, %u, %Ix, %Ix)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3260 switch(uMessage){
3261 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3262 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3263 (DWORD_PTR*)dwParam1, TRUE);
3264 case DRV_QUERYFUNCTIONINSTANCEID:
3265 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3266 case DRV_QUERYDEVICEINTERFACESIZE:
3267 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3268 NULL, (ULONG*)dwParam1);
3269 case DRV_QUERYDEVICEINTERFACE:
3271 ULONG size = dwParam2;
3272 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3273 (WCHAR*)dwParam1, &size);
3275 case DRV_QUERYMAPPABLE:
3276 return MMSYSERR_NOERROR;
3277 case DRVM_MAPPER_PREFERRED_GET:
3278 if(!dwParam1 || !dwParam2)
3279 return MMSYSERR_INVALPARAM;
3281 if(g_outmmdevices_count > 0)
3282 /* Device 0 is always the default device */
3283 *(DWORD *)dwParam1 = 0;
3284 else
3285 *(DWORD *)dwParam1 = -1;
3287 /* Status flags */
3288 *(DWORD *)dwParam2 = 0;
3290 return MMSYSERR_NOERROR;
3293 TRACE("Message not supported: %u\n", uMessage);
3295 return MMSYSERR_NOTSUPPORTED;
3298 /**************************************************************************
3299 * waveInGetNumDevs [WINMM.@]
3301 UINT WINAPI waveInGetNumDevs(void)
3303 HRESULT hr = WINMM_InitMMDevices();
3304 if(FAILED(hr))
3305 return 0;
3307 TRACE("count: %u\n", g_inmmdevices_count);
3309 return g_inmmdevices_count;
3312 /**************************************************************************
3313 * waveInGetDevCapsW [WINMM.@]
3315 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3317 WAVEINCAPSW mapper_caps, *caps;
3318 HRESULT hr;
3320 TRACE("(%Iu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3322 hr = WINMM_InitMMDevices();
3323 if(FAILED(hr))
3324 return MMSYSERR_NODRIVER;
3326 if(!lpCaps)
3327 return MMSYSERR_INVALPARAM;
3329 if(WINMM_IsMapper(uDeviceID)){
3330 mapper_caps.wMid = 0xFF;
3331 mapper_caps.wPid = 0xFF;
3332 mapper_caps.vDriverVersion = 0x00010001;
3333 mapper_caps.dwFormats = 0xFFFFFFFF;
3334 mapper_caps.wReserved1 = 0;
3335 mapper_caps.wChannels = 2;
3336 LoadStringW(hWinMM32Instance, IDS_MAPPER_NAME, mapper_caps.szPname, MAXPNAMELEN);
3338 caps = &mapper_caps;
3339 }else{
3340 if(uDeviceID >= g_inmmdevices_count){
3341 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)uDeviceID);
3343 if(!WINMM_ValidateAndLock(device))
3344 return MMSYSERR_BADDEVICEID;
3346 caps = &device->parent->in_caps;
3348 LeaveCriticalSection(&device->lock);
3349 }else{
3350 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3354 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3356 return MMSYSERR_NOERROR;
3359 /**************************************************************************
3360 * waveInGetDevCapsA [WINMM.@]
3362 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3364 UINT ret;
3365 WAVEINCAPSW wicW;
3367 TRACE("(%Iu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3369 if(!lpCaps)
3370 return MMSYSERR_INVALPARAM;
3372 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3374 if (ret == MMSYSERR_NOERROR) {
3375 WAVEINCAPSA wicA;
3376 wicA.wMid = wicW.wMid;
3377 wicA.wPid = wicW.wPid;
3378 wicA.vDriverVersion = wicW.vDriverVersion;
3379 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3380 sizeof(wicA.szPname), NULL, NULL );
3381 wicA.dwFormats = wicW.dwFormats;
3382 wicA.wChannels = wicW.wChannels;
3383 wicA.wReserved1 = wicW.wReserved1;
3384 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3386 return ret;
3389 /**************************************************************************
3390 * waveInOpen [WINMM.@]
3392 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3393 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3394 DWORD_PTR dwInstance, DWORD dwFlags)
3396 LRESULT res;
3397 WINMM_OpenInfo info;
3398 WINMM_CBInfo cb_info;
3400 TRACE("(%p, %x, %p, %Ix, %Ix, %08lx)\n", lphWaveIn, uDeviceID, lpFormat,
3401 dwCallback, dwInstance, dwFlags);
3403 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3404 return MMSYSERR_INVALPARAM;
3406 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3407 if(res != MMSYSERR_NOERROR)
3408 return res;
3410 if(!WINMM_StartDevicesThread())
3411 return MMSYSERR_NODRIVER;
3413 info.handle = 0;
3414 info.format = (WAVEFORMATEX*)lpFormat;
3415 info.callback = dwCallback;
3416 info.cb_user = dwInstance;
3417 info.req_device = uDeviceID;
3418 info.flags = dwFlags;
3419 info.reset = TRUE;
3421 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3422 InterlockedDecrement(&g_devthread_token);
3423 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3424 return res;
3426 if(lphWaveIn)
3427 *lphWaveIn = (HWAVEIN)info.handle;
3429 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3430 cb_info.callback = dwCallback;
3431 cb_info.user = dwInstance;
3432 cb_info.hwave = info.handle;
3434 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3436 return res;
3439 /**************************************************************************
3440 * waveInClose [WINMM.@]
3442 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3444 WINMM_Device *device;
3445 WINMM_CBInfo cb_info;
3446 UINT res;
3448 TRACE("(%p)\n", hWaveIn);
3450 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3452 if(!WINMM_ValidateAndLock(device))
3453 return MMSYSERR_INVALHANDLE;
3455 cb_info = device->cb_info;
3457 LeaveCriticalSection(&device->lock);
3459 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3461 if(res == MMSYSERR_NOERROR)
3462 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3464 return res;
3467 /**************************************************************************
3468 * waveInPrepareHeader [WINMM.@]
3470 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3471 UINT uSize)
3473 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3475 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3476 return MMSYSERR_INVALPARAM;
3478 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3479 return MMSYSERR_NOERROR;
3481 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3484 /**************************************************************************
3485 * waveInUnprepareHeader [WINMM.@]
3487 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3488 UINT uSize)
3490 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3492 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3493 return MMSYSERR_INVALPARAM;
3495 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3496 return WAVERR_STILLPLAYING;
3498 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3499 return MMSYSERR_NOERROR;
3501 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3504 /**************************************************************************
3505 * waveInAddBuffer [WINMM.@]
3507 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3509 WINMM_Device *device;
3511 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3513 if(!header || uSize < sizeof(WAVEHDR))
3514 return MMSYSERR_INVALPARAM;
3516 if(!(header->dwFlags & WHDR_PREPARED))
3517 return WAVERR_UNPREPARED;
3519 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3521 if(!WINMM_ValidateAndLock(device))
3522 return MMSYSERR_INVALHANDLE;
3524 if(!device->first)
3525 device->first = device->last = header;
3526 else{
3527 device->last->lpNext = header;
3528 device->last = header;
3531 header->dwBytesRecorded = 0;
3532 header->lpNext = NULL;
3533 header->dwFlags &= ~WHDR_DONE;
3534 header->dwFlags |= WHDR_INQUEUE;
3536 LeaveCriticalSection(&device->lock);
3538 return MMSYSERR_NOERROR;
3541 /**************************************************************************
3542 * waveInReset [WINMM.@]
3544 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3546 TRACE("(%p)\n", hWaveIn);
3548 return WINMM_Reset((HWAVE)hWaveIn);
3551 /**************************************************************************
3552 * waveInStart [WINMM.@]
3554 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3556 WINMM_Device *device;
3557 MMRESULT mr;
3559 TRACE("(%p)\n", hWaveIn);
3561 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3563 if(!WINMM_ValidateAndLock(device))
3564 return MMSYSERR_INVALHANDLE;
3566 mr = WINMM_BeginPlaying(device);
3568 LeaveCriticalSection(&device->lock);
3570 return mr;
3573 /**************************************************************************
3574 * waveInStop [WINMM.@]
3576 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3578 WINMM_CBInfo cb_info;
3579 WINMM_Device *device;
3580 WAVEHDR *buf;
3581 HRESULT hr;
3583 TRACE("(%p)\n", hWaveIn);
3585 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3587 if(!WINMM_ValidateAndLock(device))
3588 return MMSYSERR_INVALHANDLE;
3590 hr = WINMM_Pause(device);
3591 if(FAILED(hr)){
3592 LeaveCriticalSection(&device->lock);
3593 return MMSYSERR_ERROR;
3595 device->stopped = TRUE;
3597 buf = device->first;
3598 if(buf && buf->dwBytesRecorded > 0){
3599 device->first = buf->lpNext;
3600 }else
3601 buf = NULL;
3603 cb_info = device->cb_info;
3605 LeaveCriticalSection(&device->lock);
3607 if(buf){
3608 buf->dwFlags &= ~WHDR_INQUEUE;
3609 buf->dwFlags |= WHDR_DONE;
3610 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3613 return MMSYSERR_NOERROR;
3616 /**************************************************************************
3617 * waveInGetPosition [WINMM.@]
3619 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3620 UINT uSize)
3622 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3624 if(!uSize || !lpTime)
3625 return MMSYSERR_INVALPARAM;
3627 if(uSize < sizeof(MMTIME))
3628 return MMSYSERR_ERROR;
3630 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3633 /**************************************************************************
3634 * waveInGetID [WINMM.@]
3636 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3638 UINT dev, junk;
3639 BOOL is_out;
3640 WINMM_Device *device;
3642 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3644 if(!lpuDeviceID)
3645 return MMSYSERR_INVALPARAM;
3647 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3648 if(!WINMM_ValidateAndLock(device))
3649 return MMSYSERR_INVALHANDLE;
3651 LeaveCriticalSection(&device->lock);
3653 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3655 return MMSYSERR_NOERROR;
3658 /**************************************************************************
3659 * waveInMessage [WINMM.@]
3661 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3662 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3664 TRACE("(%p, %u, %Id, %Id)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3666 switch(uMessage){
3667 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3668 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3669 (DWORD_PTR*)dwParam1, FALSE);
3670 case DRV_QUERYFUNCTIONINSTANCEID:
3671 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3672 case DRV_QUERYDEVICEINTERFACESIZE:
3673 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3674 NULL, (ULONG*)dwParam1);
3675 case DRV_QUERYDEVICEINTERFACE:
3677 ULONG size = dwParam2;
3678 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3679 (WCHAR*)dwParam1, &size);
3681 case DRV_QUERYMAPPABLE:
3682 return MMSYSERR_NOERROR;
3683 case DRVM_MAPPER_PREFERRED_GET:
3684 if(!dwParam1 || !dwParam2)
3685 return MMSYSERR_INVALPARAM;
3687 if(g_inmmdevices_count > 0)
3688 /* Device 0 is always the default device */
3689 *(DWORD *)dwParam1 = 0;
3690 else
3691 *(DWORD *)dwParam1 = -1;
3693 /* Status flags */
3694 *(DWORD *)dwParam2 = 0;
3696 return MMSYSERR_NOERROR;
3699 TRACE("Message not supported: %u\n", uMessage);
3701 return MMSYSERR_NOTSUPPORTED;
3704 UINT WINAPI mixerGetNumDevs(void)
3706 HRESULT hr;
3708 TRACE("\n");
3710 hr = WINMM_InitMMDevices();
3711 if(FAILED(hr))
3712 return 0;
3714 return g_outmmdevices_count + g_inmmdevices_count;
3717 /**************************************************************************
3718 * mixerGetDevCapsA [WINMM.@]
3720 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3722 MIXERCAPSW micW;
3723 UINT ret;
3725 TRACE("(%Iu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3727 if(!lpCaps)
3728 return MMSYSERR_INVALPARAM;
3730 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3732 if (ret == MMSYSERR_NOERROR) {
3733 MIXERCAPSA micA;
3734 micA.wMid = micW.wMid;
3735 micA.wPid = micW.wPid;
3736 micA.vDriverVersion = micW.vDriverVersion;
3737 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3738 sizeof(micA.szPname), NULL, NULL );
3739 micA.fdwSupport = micW.fdwSupport;
3740 micA.cDestinations = micW.cDestinations;
3741 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3743 return ret;
3746 /**************************************************************************
3747 * mixerGetDevCapsW [WINMM.@]
3749 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3751 WINMM_MMDevice *mmdevice;
3752 MIXERCAPSW caps;
3753 HRESULT hr;
3755 TRACE("(%Iu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3757 hr = WINMM_InitMMDevices();
3758 if(FAILED(hr))
3759 return MMSYSERR_NODRIVER;
3761 if(!lpCaps)
3762 return MMSYSERR_INVALPARAM;
3764 if(!uSize)
3765 return MMSYSERR_NOERROR;
3767 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3768 mmdevice = WINMM_GetMixerMMDevice((HMIXEROBJ)uDeviceID,
3769 MIXER_OBJECTF_MIXER, NULL);
3770 else if(uDeviceID < g_outmmdevices_count)
3771 mmdevice = read_map(g_out_map, uDeviceID);
3772 else
3773 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3775 if(!mmdevice)
3776 return MMSYSERR_BADDEVICEID;
3778 if(mmdevice->dataflow == eRender)
3779 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3780 else
3781 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3783 caps.wMid = 0xFF;
3784 caps.wPid = 0xFF;
3785 caps.vDriverVersion = 0x00010001;
3786 caps.fdwSupport = 0;
3787 caps.cDestinations = 1;
3789 memcpy(lpCaps, &caps, uSize);
3791 return MMSYSERR_NOERROR;
3794 /**************************************************************************
3795 * mixerOpen [WINMM.@]
3797 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3798 DWORD_PTR dwInstance, DWORD fdwOpen)
3800 WINMM_MMDevice *mmdevice;
3801 MMRESULT mr;
3802 HRESULT hr;
3804 TRACE("(%p, %d, %Ix, %Ix, %lx)\n", lphMix, uDeviceID, dwCallback,
3805 dwInstance, fdwOpen);
3807 hr = WINMM_InitMMDevices();
3808 if(FAILED(hr))
3809 return MMSYSERR_NODRIVER;
3811 if(!lphMix)
3812 return MMSYSERR_INVALPARAM;
3814 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3815 if(mr != MMSYSERR_NOERROR)
3816 return mr;
3818 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3819 return MMSYSERR_BADDEVICEID;
3821 if(uDeviceID < g_outmmdevices_count){
3822 mmdevice = read_map(g_out_map, uDeviceID);
3823 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3824 mmdevice->mixer_count);
3825 }else{
3826 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3827 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3828 FALSE, mmdevice->mixer_count);
3831 ++mmdevice->mixer_count;
3833 return MMSYSERR_NOERROR;
3836 /**************************************************************************
3837 * mixerClose [WINMM.@]
3839 UINT WINAPI mixerClose(HMIXER hMix)
3841 TRACE("(%p)\n", hMix);
3843 return MMSYSERR_NOERROR;
3846 /**************************************************************************
3847 * mixerGetID [WINMM.@]
3849 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3851 WINMM_MMDevice *mmdevice;
3852 HRESULT hr;
3854 TRACE("(%p, %p, %lx)\n", hmix, lpid, fdwID);
3856 hr = WINMM_InitMMDevices();
3857 if(FAILED(hr))
3858 return MMSYSERR_NODRIVER;
3860 if(!lpid)
3861 return MMSYSERR_INVALPARAM;
3863 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3864 if(!mmdevice)
3865 return MMSYSERR_INVALHANDLE;
3867 if(mmdevice->in_caps.szPname[0] != '\0')
3868 *lpid += g_outmmdevices_count;
3870 return MMSYSERR_NOERROR;
3873 /**************************************************************************
3874 * mixerGetControlDetailsW [WINMM.@]
3876 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3877 DWORD fdwDetails)
3879 WINMM_ControlDetails details;
3881 TRACE("(%p, %p, %lx)\n", hmix, lpmcdW, fdwDetails);
3883 if(!WINMM_StartDevicesThread())
3884 return MMSYSERR_NODRIVER;
3886 if(!lpmcdW || !lpmcdW->paDetails)
3887 return MMSYSERR_INVALPARAM;
3889 TRACE("dwControlID: %lu\n", lpmcdW->dwControlID);
3891 details.hmix = hmix;
3892 details.details = lpmcdW;
3893 details.flags = fdwDetails;
3895 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3896 (DWORD_PTR)&details, 0);
3899 /**************************************************************************
3900 * mixerGetControlDetailsA [WINMM.@]
3902 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3903 DWORD fdwDetails)
3905 UINT ret = MMSYSERR_NOTSUPPORTED;
3907 TRACE("(%p, %p, %08lx)\n", hmix, lpmcdA, fdwDetails);
3909 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3910 return MMSYSERR_INVALPARAM;
3912 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3913 case MIXER_GETCONTROLDETAILSF_VALUE:
3914 /* can safely use A structure as it is, no string inside */
3915 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3916 break;
3917 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3919 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3920 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3921 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3922 unsigned int i;
3924 if (lpmcdA->u.cMultipleItems != 0) {
3925 size *= lpmcdA->u.cMultipleItems;
3927 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3928 lpmcdA->paDetails = pDetailsW;
3929 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3930 /* set up lpmcd->paDetails */
3931 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3932 /* copy from lpmcd->paDetails back to paDetailsW; */
3933 if (ret == MMSYSERR_NOERROR) {
3934 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3935 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3936 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3937 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3938 pDetailsA->szName,
3939 sizeof(pDetailsA->szName), NULL, NULL );
3940 pDetailsA++;
3941 pDetailsW++;
3943 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3944 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3946 HeapFree(GetProcessHeap(), 0, pDetailsW);
3947 lpmcdA->paDetails = pDetailsA;
3948 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3950 break;
3951 default:
3952 WARN("Unsupported fdwDetails=0x%08lx\n", fdwDetails);
3955 return ret;
3958 /**************************************************************************
3959 * mixerGetLineControlsA [WINMM.@]
3961 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3962 DWORD fdwControls)
3964 MIXERLINECONTROLSW mlcW;
3965 DWORD ret;
3966 unsigned int i;
3968 TRACE("(%p, %p, %lx)\n", hmix, lpmlcA, fdwControls);
3970 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3971 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3972 return MMSYSERR_INVALPARAM;
3974 mlcW.cbStruct = sizeof(mlcW);
3975 mlcW.dwLineID = lpmlcA->dwLineID;
3976 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3977 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3979 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3980 the control count is assumed to be 1 - This is relied upon by a game,
3981 "Dynomite Deluze" */
3982 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3983 mlcW.cControls = 1;
3984 } else {
3985 mlcW.cControls = lpmlcA->cControls;
3987 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3988 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3989 mlcW.cControls * mlcW.cbmxctrl);
3991 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3993 if (ret == MMSYSERR_NOERROR) {
3994 lpmlcA->dwLineID = mlcW.dwLineID;
3995 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3996 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3998 for (i = 0; i < mlcW.cControls; i++) {
3999 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
4000 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
4001 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
4002 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
4003 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
4004 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
4005 lpmlcA->pamxctrl[i].szShortName,
4006 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
4007 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
4008 lpmlcA->pamxctrl[i].szName,
4009 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
4010 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
4011 * sizeof(mlcW.pamxctrl[i].Bounds) */
4012 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
4013 sizeof(mlcW.pamxctrl[i].Bounds));
4014 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
4015 * sizeof(mlcW.pamxctrl[i].Metrics) */
4016 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
4017 sizeof(mlcW.pamxctrl[i].Metrics));
4021 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
4023 return ret;
4026 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4027 MIXERCONTROLW *ctl, DWORD flags)
4029 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4030 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4031 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4032 ctl->cMultipleItems = 0;
4033 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4034 LoadStringW(hWinMM32Instance, IDS_VOLUME, ctl->szName, MIXER_LONG_NAME_CHARS);
4035 ctl->Bounds.s1.dwMinimum = 0;
4036 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4037 ctl->Metrics.cSteps = 192;
4039 return MMSYSERR_NOERROR;
4042 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4043 MIXERCONTROLW *ctl, DWORD flags)
4045 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4046 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4047 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4048 ctl->cMultipleItems = 0;
4049 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szShortName, MIXER_SHORT_NAME_CHARS);
4050 LoadStringW(hWinMM32Instance, IDS_MUTE, ctl->szName, MIXER_LONG_NAME_CHARS);
4051 ctl->Bounds.s1.dwMinimum = 0;
4052 ctl->Bounds.s1.dwMaximum = 1;
4053 ctl->Metrics.cSteps = 0;
4055 return MMSYSERR_NOERROR;
4058 /**************************************************************************
4059 * mixerGetLineControlsW [WINMM.@]
4061 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4062 DWORD fdwControls)
4064 WINMM_MMDevice *mmdevice;
4065 HRESULT hr;
4067 TRACE("(%p, %p, %08lx)\n", hmix, lpmlcW, fdwControls);
4069 hr = WINMM_InitMMDevices();
4070 if(FAILED(hr))
4071 return MMSYSERR_NODRIVER;
4073 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4074 MIXER_GETLINECONTROLSF_ONEBYID |
4075 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4076 MIXER_OBJECTF_HMIXER |
4077 MIXER_OBJECTF_MIXER)){
4078 WARN("Unknown GetLineControls flag: %lx\n", fdwControls);
4079 return MMSYSERR_INVALFLAG;
4082 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4083 return MMSYSERR_INVALPARAM;
4085 TRACE("dwLineID: %lu\n", lpmlcW->dwLineID);
4086 TRACE("dwControl: %lx\n", lpmlcW->u.dwControlID);
4087 TRACE("cControls: %lu\n", lpmlcW->cControls);
4089 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4090 if(!mmdevice)
4091 return MMSYSERR_INVALHANDLE;
4093 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4094 case MIXER_GETLINECONTROLSF_ALL:
4095 if(lpmlcW->cControls != 2)
4096 return MMSYSERR_INVALPARAM;
4097 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4098 return MMSYSERR_INVALPARAM;
4099 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4100 return MIXERR_INVALLINE;
4101 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4102 &lpmlcW->pamxctrl[0], fdwControls);
4103 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4104 &lpmlcW->pamxctrl[1], fdwControls);
4105 return MMSYSERR_NOERROR;
4106 case MIXER_GETLINECONTROLSF_ONEBYID:
4107 if(lpmlcW->cControls != 1)
4108 return MMSYSERR_INVALPARAM;
4109 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4110 return MMSYSERR_INVALPARAM;
4111 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4112 return MIXERR_INVALLINE;
4113 if(lpmlcW->u.dwControlID == 0)
4114 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4115 lpmlcW->pamxctrl, fdwControls);
4116 if(lpmlcW->u.dwControlID == 1)
4117 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4118 lpmlcW->pamxctrl, fdwControls);
4119 return MMSYSERR_NOTSUPPORTED;
4120 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4121 if(lpmlcW->cControls != 1)
4122 return MMSYSERR_INVALPARAM;
4123 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4124 return MMSYSERR_INVALPARAM;
4125 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4126 return MIXERR_INVALLINE;
4127 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4128 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4129 lpmlcW->pamxctrl, fdwControls);
4130 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4131 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4132 lpmlcW->pamxctrl, fdwControls);
4133 return MMSYSERR_NOTSUPPORTED;
4136 return MMSYSERR_NOTSUPPORTED;
4139 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4140 MIXERLINEW *info, DWORD flags)
4142 BOOL is_out = TRUE;
4143 if(mmdevice->in_caps.szPname[0] != '\0')
4144 is_out = FALSE;
4146 if(info->dwSource != 0)
4147 return MIXERR_INVALLINE;
4149 info->dwDestination = 0;
4150 info->dwLineID = 0;
4151 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4152 info->cConnections = 0;
4153 info->cControls = 2;
4154 /* volume & mute always affect all channels, so claim 1 channel */
4155 info->cChannels = 1;
4156 info->Target.dwDeviceID = mmdev_index;
4157 info->Target.wMid = ~0;
4158 info->Target.wPid = ~0;
4159 info->Target.vDriverVersion = 0;
4161 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4162 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4164 if(is_out){
4165 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4166 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4167 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4168 sizeof(info->Target.szPname));
4169 }else{
4170 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4171 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4172 info->Target.szPname[0] = '\0';
4175 return MMSYSERR_NOERROR;
4178 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4179 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4181 BOOL is_out = TRUE;
4182 if(mmdevice->in_caps.szPname[0] != '\0')
4183 is_out = FALSE;
4185 if(info->dwDestination != 0)
4186 return MIXERR_INVALLINE;
4188 info->dwSource = 0xFFFFFFFF;
4189 info->dwLineID = 0xFFFF0000;
4190 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4191 info->cConnections = 1;
4192 info->cControls = 2;
4194 LoadStringW(hWinMM32Instance, IDS_VOLUME, info->szShortName, MIXER_SHORT_NAME_CHARS);
4195 LoadStringW(hWinMM32Instance, IDS_MASTER_VOLUME, info->szName, MIXER_LONG_NAME_CHARS);
4197 info->Target.dwDeviceID = mmdev_index;
4198 info->Target.wMid = ~0;
4199 info->Target.wPid = ~0;
4200 info->Target.vDriverVersion = 0;
4202 if(is_out){
4203 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4204 info->cChannels = mmdevice->out_caps.wChannels;
4205 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4206 info->Target.szPname[0] = '\0';
4207 }else{
4208 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4209 info->cChannels = mmdevice->in_caps.wChannels;
4210 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4211 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4212 sizeof(info->Target.szPname));
4215 return MMSYSERR_NOERROR;
4218 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4219 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4221 BOOL is_out = TRUE;
4222 if(mmdevice->in_caps.szPname[0] != '\0')
4223 is_out = FALSE;
4225 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4226 if(is_out)
4227 return MIXERR_INVALLINE;
4228 info->dwDestination = 0;
4229 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4232 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4233 if(!is_out)
4234 return MIXERR_INVALLINE;
4235 info->dwDestination = 0;
4236 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4239 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4240 if(is_out)
4241 return MIXERR_INVALLINE;
4242 info->dwSource = 0;
4243 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4246 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4247 if(!is_out)
4248 return MIXERR_INVALLINE;
4249 info->dwSource = 0;
4250 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4253 TRACE("Returning INVALLINE on this component type: %lu\n",
4254 info->dwComponentType);
4256 return MIXERR_INVALLINE;
4259 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4260 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4262 if(info->dwLineID == 0xFFFF0000){
4263 info->dwDestination = 0;
4264 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4267 if(info->dwLineID == 0){
4268 info->dwSource = 0;
4269 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4272 TRACE("Returning INVALLINE on this dwLineID: %lu\n", info->dwLineID);
4273 return MIXERR_INVALLINE;
4276 /**************************************************************************
4277 * mixerGetLineInfoW [WINMM.@]
4279 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4281 UINT mmdev_index;
4282 WINMM_MMDevice *mmdevice;
4283 HRESULT hr;
4285 TRACE("(%p, %p, %lx)\n", hmix, lpmliW, fdwInfo);
4287 hr = WINMM_InitMMDevices();
4288 if(FAILED(hr))
4289 return MMSYSERR_NODRIVER;
4291 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4292 return MMSYSERR_INVALPARAM;
4294 TRACE("dwDestination: %lu\n", lpmliW->dwDestination);
4295 TRACE("dwSource: %lu\n", lpmliW->dwSource);
4296 TRACE("dwLineID: %lu\n", lpmliW->dwLineID);
4297 TRACE("fdwLine: 0x%lx\n", lpmliW->fdwLine);
4298 TRACE("dwComponentType: 0x%lx\n", lpmliW->dwComponentType);
4300 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4301 MIXER_GETLINEINFOF_DESTINATION |
4302 MIXER_GETLINEINFOF_LINEID |
4303 MIXER_GETLINEINFOF_SOURCE |
4304 MIXER_GETLINEINFOF_TARGETTYPE |
4305 MIXER_OBJECTF_HMIXER |
4306 MIXER_OBJECTF_MIXER)){
4307 WARN("Unknown GetLineInfo flag: %lx\n", fdwInfo);
4308 return MMSYSERR_INVALFLAG;
4311 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4312 if(!mmdevice)
4313 return MMSYSERR_INVALHANDLE;
4315 lpmliW->dwUser = 0;
4317 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4318 case MIXER_GETLINEINFOF_DESTINATION:
4319 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4320 fdwInfo);
4321 case MIXER_GETLINEINFOF_SOURCE:
4322 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4323 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4324 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4325 fdwInfo);
4326 case MIXER_GETLINEINFOF_LINEID:
4327 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4328 case MIXER_GETLINEINFOF_TARGETTYPE:
4329 FIXME("TARGETTYPE flag not implemented!\n");
4330 return MIXERR_INVALLINE;
4333 TRACE("Returning INVALFLAG on these flags: %lx\n", fdwInfo);
4334 return MMSYSERR_INVALFLAG;
4337 /**************************************************************************
4338 * mixerGetLineInfoA [WINMM.@]
4340 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4341 DWORD fdwInfo)
4343 MIXERLINEW mliW;
4344 UINT ret;
4346 TRACE("(%p, %p, %lx)\n", hmix, lpmliA, fdwInfo);
4348 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4349 return MMSYSERR_INVALPARAM;
4351 mliW.cbStruct = sizeof(mliW);
4352 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4353 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4354 mliW.dwComponentType = lpmliA->dwComponentType;
4355 break;
4356 case MIXER_GETLINEINFOF_DESTINATION:
4357 mliW.dwDestination = lpmliA->dwDestination;
4358 break;
4359 case MIXER_GETLINEINFOF_LINEID:
4360 mliW.dwLineID = lpmliA->dwLineID;
4361 break;
4362 case MIXER_GETLINEINFOF_SOURCE:
4363 mliW.dwDestination = lpmliA->dwDestination;
4364 mliW.dwSource = lpmliA->dwSource;
4365 break;
4366 case MIXER_GETLINEINFOF_TARGETTYPE:
4367 mliW.Target.dwType = lpmliA->Target.dwType;
4368 mliW.Target.wMid = lpmliA->Target.wMid;
4369 mliW.Target.wPid = lpmliA->Target.wPid;
4370 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4371 MultiByteToWideChar(CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, ARRAY_SIZE(mliW.Target.szPname));
4372 break;
4373 default:
4374 WARN("Unsupported fdwControls=0x%08lx\n", fdwInfo);
4375 return MMSYSERR_INVALFLAG;
4378 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4380 if(ret == MMSYSERR_NOERROR)
4382 lpmliA->dwDestination = mliW.dwDestination;
4383 lpmliA->dwSource = mliW.dwSource;
4384 lpmliA->dwLineID = mliW.dwLineID;
4385 lpmliA->fdwLine = mliW.fdwLine;
4386 lpmliA->dwUser = mliW.dwUser;
4387 lpmliA->dwComponentType = mliW.dwComponentType;
4388 lpmliA->cChannels = mliW.cChannels;
4389 lpmliA->cConnections = mliW.cConnections;
4390 lpmliA->cControls = mliW.cControls;
4391 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4392 sizeof(lpmliA->szShortName), NULL, NULL);
4393 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4394 sizeof(lpmliA->szName), NULL, NULL );
4395 lpmliA->Target.dwType = mliW.Target.dwType;
4396 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4397 lpmliA->Target.wMid = mliW.Target.wMid;
4398 lpmliA->Target.wPid = mliW.Target.wPid;
4399 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4400 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4401 sizeof(lpmliA->Target.szPname), NULL, NULL );
4403 return ret;
4406 /**************************************************************************
4407 * mixerSetControlDetails [WINMM.@]
4409 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4410 DWORD fdwDetails)
4412 WINMM_ControlDetails details;
4413 UINT ret;
4415 TRACE("(%p, %p, %lx)\n", hmix, lpmcd, fdwDetails);
4417 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4418 MIXER_SETCONTROLDETAILSF_CUSTOM)
4419 return MMSYSERR_NOTSUPPORTED;
4421 if(!lpmcd)
4422 return MMSYSERR_INVALPARAM;
4424 if(!WINMM_StartDevicesThread())
4425 return MMSYSERR_NODRIVER;
4427 TRACE("dwControlID: %lu\n", lpmcd->dwControlID);
4429 details.hmix = hmix;
4430 details.details = lpmcd;
4431 details.flags = fdwDetails;
4433 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4434 (DWORD_PTR)&details, 0);
4435 InterlockedDecrement(&g_devthread_token);
4436 return ret;
4439 /**************************************************************************
4440 * mixerMessage [WINMM.@]
4442 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4444 TRACE("(%p, %d, %Ix, %Ix)\n", hmix, uMsg, dwParam1, dwParam2);
4446 return MMSYSERR_NOTSUPPORTED;