riched20: Add a helper to debug the run text.
[wine/multimedia.git] / dlls / winmm / waveform.c
blobe6fe360aaf585b73d5fdd29a088545254d4b2195
1 /*
2 * Copyright 1993 Martin Ayotte
3 * 1998-2002 Eric Pouech
4 * 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "mmsystem.h"
32 #include "mmreg.h"
33 #include "msacm.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "winternl.h"
38 #include "winemm.h"
40 #include "ole2.h"
41 #include "initguid.h"
42 #include "devpkey.h"
43 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "audiopolicy.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
51 /* FIXME: Should be localized */
52 static const WCHAR volumeW[] = {'V','o','l','u','m','e',0};
53 static const WCHAR mastervolumeW[] = {'M','a','s','t','e','r',' ','V','o','l',
54 'u','m','e',0};
55 static const WCHAR muteW[] = {'M','u','t','e',0};
57 /* HWAVE (and HMIXER) format:
59 * XXXX... 1FDD DDDD IIII IIII
60 * X = unused (must be 0)
61 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
62 * F = flow direction (0 = IN, 1 = OUT)
63 * D = index into g_out_mmdevices, or all 1s for the MAPPER device
64 * I = index in the mmdevice's devices array
66 * Two reasons that we don't just use pointers:
67 * - HWAVEs must fit into 16 bits for compatibility with old applications.
68 * - We must be able to identify bad devices without crashing.
71 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
72 #define AC_BUFLEN (10 * 100000)
73 #define MAX_DEVICES 256
74 #define MAPPER_INDEX 0x3F
76 typedef struct _WINMM_CBInfo {
77 DWORD_PTR callback;
78 DWORD_PTR user;
79 DWORD flags;
80 HWAVE hwave;
81 } WINMM_CBInfo;
83 struct _WINMM_MMDevice;
84 typedef struct _WINMM_MMDevice WINMM_MMDevice;
86 typedef struct _WINMM_Device {
87 WINMM_CBInfo cb_info;
89 HWAVE handle;
91 BOOL open;
93 IMMDevice *device;
94 IAudioClient *client;
95 IAudioRenderClient *render;
96 IAudioCaptureClient *capture;
97 IAudioClock *clock;
98 IAudioStreamVolume *volume;
100 WAVEFORMATEX *orig_fmt;
101 HACMSTREAM acm_handle;
102 ACMSTREAMHEADER acm_hdr;
103 UINT32 acm_offs;
105 WAVEHDR *first, *last, *playing, *loop_start;
107 BOOL stopped;
108 DWORD loop_counter;
109 UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
110 UINT32 remainder_frames; /* header chunk frames already played when a device switch occurred */
112 /* stored in frames of sample rate, *not* AC::GetFrequency */
113 UINT64 last_clock_pos;
115 HANDLE event;
116 CRITICAL_SECTION lock;
118 WINMM_MMDevice *parent;
119 } WINMM_Device;
121 struct _WINMM_MMDevice {
122 WAVEOUTCAPSW out_caps; /* must not be modified outside of WINMM_InitMMDevices*/
123 WAVEINCAPSW in_caps; /* must not be modified outside of WINMM_InitMMDevices*/
124 WCHAR *dev_id;
126 ISimpleAudioVolume *volume;
128 GUID session;
130 UINT index;
132 /* HMIXER format is the same as the HWAVE format, but the I bits are
133 * replaced by the value of this counter, to keep each HMIXER unique */
134 UINT mixer_count;
136 CRITICAL_SECTION lock;
138 WINMM_Device *devices[MAX_DEVICES];
141 static WINMM_MMDevice *g_out_mmdevices;
142 static WINMM_MMDevice **g_out_map;
143 static UINT g_outmmdevices_count;
144 static WINMM_Device *g_out_mapper_devices[MAX_DEVICES];
146 static WINMM_MMDevice *g_in_mmdevices;
147 static WINMM_MMDevice **g_in_map;
148 static UINT g_inmmdevices_count;
149 static WINMM_Device *g_in_mapper_devices[MAX_DEVICES];
151 static IMMDeviceEnumerator *g_devenum;
153 #define WINMM_WM_QUIT WM_USER
155 static CRITICAL_SECTION g_devthread_lock;
156 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug =
158 0, 0, &g_devthread_lock,
159 { &g_devthread_lock_debug.ProcessLocksList, &g_devthread_lock_debug.ProcessLocksList },
160 0, 0, { (DWORD_PTR)(__FILE__ ": g_devthread_lock") }
162 static CRITICAL_SECTION g_devthread_lock = { &g_devthread_lock_debug, -1, 0, 0, 0, 0 };
163 static HANDLE g_devices_thread;
164 static HWND g_devices_hwnd;
166 static UINT g_devhandle_count;
167 static HANDLE *g_device_handles;
168 static WINMM_Device **g_handle_devices;
170 typedef struct _WINMM_OpenInfo {
171 HWAVE handle;
172 UINT req_device;
173 WAVEFORMATEX *format;
174 DWORD_PTR callback;
175 DWORD_PTR cb_user;
176 DWORD flags;
177 BOOL reset;
178 } WINMM_OpenInfo;
180 typedef struct _WINMM_ControlDetails {
181 HMIXEROBJ hmix;
182 MIXERCONTROLDETAILS *details;
183 DWORD flags;
184 } WINMM_ControlDetails;
186 typedef struct _WINMM_QueryInterfaceInfo {
187 BOOL is_out;
188 UINT index;
189 WCHAR *str;
190 UINT *len_bytes;
191 } WINMM_QueryInterfaceInfo;
193 static LRESULT WOD_Open(WINMM_OpenInfo *info);
194 static LRESULT WOD_Close(HWAVEOUT hwave);
195 static LRESULT WID_Open(WINMM_OpenInfo *info);
196 static LRESULT WID_Close(HWAVEIN hwave);
197 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device);
199 void WINMM_DeleteWaveform(void)
201 UINT i, j;
203 if(g_devices_hwnd){
204 for(i = 0; i < g_outmmdevices_count; ++i){
205 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 SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)device->handle, 0);
212 for(i = 0; i < g_inmmdevices_count; ++i){
213 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
214 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
215 WINMM_Device *device = mmdevice->devices[j];
216 SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)device->handle, 0);
220 SendMessageW(g_devices_hwnd, WINMM_WM_QUIT, 0, 0);
222 for(i = 0; i < g_outmmdevices_count; ++i){
223 WINMM_MMDevice *mmdevice = &g_out_mmdevices[i];
225 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
226 WINMM_Device *device = mmdevice->devices[j];
227 if(device->handle)
228 CloseHandle(device->handle);
229 DeleteCriticalSection(&device->lock);
232 if(mmdevice->volume)
233 ISimpleAudioVolume_Release(mmdevice->volume);
234 CoTaskMemFree(mmdevice->dev_id);
235 DeleteCriticalSection(&mmdevice->lock);
238 for(i = 0; i < g_inmmdevices_count; ++i){
239 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
241 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
242 WINMM_Device *device = mmdevice->devices[j];
243 if(device->handle)
244 CloseHandle(device->handle);
245 DeleteCriticalSection(&device->lock);
248 if(mmdevice->volume)
249 ISimpleAudioVolume_Release(mmdevice->volume);
250 CoTaskMemFree(mmdevice->dev_id);
251 DeleteCriticalSection(&mmdevice->lock);
254 HeapFree(GetProcessHeap(), 0, g_out_mmdevices);
255 HeapFree(GetProcessHeap(), 0, g_in_mmdevices);
257 HeapFree(GetProcessHeap(), 0, g_device_handles);
258 HeapFree(GetProcessHeap(), 0, g_handle_devices);
261 DeleteCriticalSection(&g_devthread_lock);
264 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
266 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
267 (mmdevice << 8) | device);
270 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
271 BOOL *is_out, UINT *device_index, UINT *junk)
273 ULONG32 l = HandleToULong(hwave);
274 *device_index = l & 0xFF;
275 *mmdevice_index = (l >> 8) & 0x3F;
276 *is_out = (l >> 14) & 0x1;
277 *junk = l >> 15;
280 static void WINMM_InitDevice(WINMM_Device *device)
282 InitializeCriticalSection(&device->lock);
283 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
286 static inline WINMM_MMDevice *read_map(WINMM_MMDevice **map, UINT index)
288 WINMM_MMDevice *ret;
289 EnterCriticalSection(&g_devthread_lock);
290 ret = map[index];
291 LeaveCriticalSection(&g_devthread_lock);
292 return ret;
295 /* finds the first unused Device, marks it as "open", and returns
296 * a pointer to the device
298 * IMPORTANT: it is the caller's responsibility to release the device's lock
299 * on success
301 static WINMM_Device *WINMM_FindUnusedDevice(WINMM_Device **devices,
302 WINMM_MMDevice *parent, UINT internal_index, BOOL is_out)
304 UINT i;
306 for(i = 0; i < MAX_DEVICES; ++i){
307 WINMM_Device *device = devices[i];
309 if(!device){
310 device = devices[i] = HeapAlloc(GetProcessHeap(),
311 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
312 if(!device)
313 return NULL;
315 WINMM_InitDevice(device);
316 EnterCriticalSection(&device->lock);
317 }else
318 EnterCriticalSection(&device->lock);
320 if(!device->open){
321 device->handle = WINMM_MakeHWAVE(internal_index, is_out, i);
322 device->parent = parent;
323 device->open = TRUE;
325 return device;
328 LeaveCriticalSection(&device->lock);
331 TRACE("All devices in use: mmdevice: %u\n", internal_index);
333 return NULL;
336 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
338 if(!device)
339 return FALSE;
341 EnterCriticalSection(&device->lock);
343 if(!device->open){
344 LeaveCriticalSection(&device->lock);
345 return FALSE;
348 return TRUE;
351 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
353 WINMM_MMDevice *mmdevice;
354 WINMM_Device *device;
355 UINT mmdevice_index, device_index, junk;
356 BOOL is_out;
358 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
360 if(junk != 0x1)
361 return NULL;
363 if(mmdevice_index == MAPPER_INDEX){
364 EnterCriticalSection(&g_devthread_lock);
365 if(is_out)
366 device = g_out_mapper_devices[device_index];
367 else
368 device = g_in_mapper_devices[device_index];
369 LeaveCriticalSection(&g_devthread_lock);
370 return device;
373 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
374 return NULL;
376 if(is_out)
377 mmdevice = &g_out_mmdevices[mmdevice_index];
378 else
379 mmdevice = &g_in_mmdevices[mmdevice_index];
381 EnterCriticalSection(&mmdevice->lock);
383 device = mmdevice->devices[device_index];
385 LeaveCriticalSection(&mmdevice->lock);
387 return device;
390 /* Note: NotifyClient should never be called while holding the device lock
391 * since the client may call wave* functions from within the callback. */
392 static inline void WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
393 DWORD_PTR param2)
395 DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
396 msg, info->user, param1, param2);
399 static MMRESULT hr2mmr(HRESULT hr)
401 switch(hr){
402 case S_OK:
403 case AUDCLNT_E_NOT_STOPPED:
404 return MMSYSERR_NOERROR;
405 case AUDCLNT_E_UNSUPPORTED_FORMAT:
406 return WAVERR_BADFORMAT;
407 case AUDCLNT_E_DEVICE_IN_USE:
408 return MMSYSERR_ALLOCATED;
409 case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
410 return MMSYSERR_NOTENABLED;
411 case E_OUTOFMEMORY:
412 return MMSYSERR_NOMEM;
413 case E_POINTER:
414 case E_INVALIDARG:
415 return MMSYSERR_INVALPARAM;
416 case AUDCLNT_E_DEVICE_INVALIDATED: /* DSERR_BUFFERLOST */
417 default:
418 return FAILED(hr) ? MMSYSERR_ERROR : MMSYSERR_NOERROR;
422 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
423 UINT outlen)
425 IPropertyStore *ps;
426 PROPVARIANT var;
427 HRESULT hr;
429 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
430 if(FAILED(hr))
431 return hr;
433 PropVariantInit(&var);
435 hr = IPropertyStore_GetValue(ps,
436 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
437 if(FAILED(hr)){
438 IPropertyStore_Release(ps);
439 return hr;
442 lstrcpynW(out, var.u.pwszVal, outlen);
444 PropVariantClear(&var);
446 IPropertyStore_Release(ps);
448 return S_OK;
451 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
452 WORD channels)
454 WAVEFORMATEX fmt, *junk;
455 HRESULT hr;
457 fmt.wFormatTag = WAVE_FORMAT_PCM;
458 fmt.nChannels = channels;
459 fmt.nSamplesPerSec = rate;
460 fmt.wBitsPerSample = depth;
461 fmt.nBlockAlign = (channels * depth) / 8;
462 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
463 fmt.cbSize = 0;
465 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
466 &fmt, &junk);
467 if(SUCCEEDED(hr))
468 CoTaskMemFree(junk);
470 return hr;
473 static struct _TestFormat {
474 DWORD flag;
475 DWORD rate;
476 DWORD depth;
477 WORD channels;
478 } formats_to_test[] = {
479 { WAVE_FORMAT_1M08, 11025, 8, 1 },
480 { WAVE_FORMAT_1M16, 11025, 16, 1 },
481 { WAVE_FORMAT_1S08, 11025, 8, 2 },
482 { WAVE_FORMAT_1S16, 11025, 16, 2 },
483 { WAVE_FORMAT_2M08, 22050, 8, 1 },
484 { WAVE_FORMAT_2M16, 22050, 16, 1 },
485 { WAVE_FORMAT_2S08, 22050, 8, 2 },
486 { WAVE_FORMAT_2S16, 22050, 16, 2 },
487 { WAVE_FORMAT_4M08, 44100, 8, 1 },
488 { WAVE_FORMAT_4M16, 44100, 16, 1 },
489 { WAVE_FORMAT_4S08, 44100, 8, 2 },
490 { WAVE_FORMAT_4S16, 44100, 16, 2 },
491 { WAVE_FORMAT_48M08, 48000, 8, 1 },
492 { WAVE_FORMAT_48M16, 48000, 16, 1 },
493 { WAVE_FORMAT_48S08, 48000, 8, 2 },
494 { WAVE_FORMAT_48S16, 48000, 16, 2 },
495 { WAVE_FORMAT_96M08, 96000, 8, 1 },
496 { WAVE_FORMAT_96M16, 96000, 16, 1 },
497 { WAVE_FORMAT_96S08, 96000, 8, 2 },
498 { WAVE_FORMAT_96S16, 96000, 16, 2 },
502 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
504 DWORD flags = 0;
505 HRESULT hr;
506 struct _TestFormat *fmt;
507 IAudioClient *client;
509 hr = IMMDevice_Activate(device, &IID_IAudioClient,
510 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
511 if(FAILED(hr))
512 return 0;
514 for(fmt = formats_to_test; fmt->flag; ++fmt){
515 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
516 if(hr == S_OK)
517 flags |= fmt->flag;
520 IAudioClient_Release(client);
522 return flags;
525 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
526 WINMM_MMDevice *dev, UINT index)
528 HRESULT hr;
530 if(flow == eRender){
531 dev->out_caps.wMid = 0xFF;
532 dev->out_caps.wPid = 0xFF;
533 dev->out_caps.vDriverVersion = 0x00010001;
534 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
535 dev->out_caps.wReserved1 = 0;
536 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
537 WAVECAPS_SAMPLEACCURATE;
538 dev->out_caps.wChannels = 2;
539 dev->out_caps.szPname[0] = '\0';
541 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
542 sizeof(dev->out_caps.szPname) /
543 sizeof(*dev->out_caps.szPname));
544 if(FAILED(hr))
545 return hr;
546 }else{
547 dev->in_caps.wMid = 0xFF;
548 dev->in_caps.wPid = 0xFF;
549 dev->in_caps.vDriverVersion = 0x00010001;
550 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
551 dev->in_caps.wReserved1 = 0;
552 dev->in_caps.wChannels = 2;
553 dev->in_caps.szPname[0] = '\0';
555 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
556 sizeof(dev->in_caps.szPname) /
557 sizeof(*dev->in_caps.szPname));
558 if(FAILED(hr))
559 return hr;
562 hr = IMMDevice_GetId(device, &dev->dev_id);
563 if(FAILED(hr))
564 return hr;
566 CoCreateGuid(&dev->session);
568 dev->index = index;
570 InitializeCriticalSection(&dev->lock);
571 dev->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
573 return S_OK;
576 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices,
577 WINMM_MMDevice ***map, UINT *devcount, EDataFlow flow,
578 IMMDeviceEnumerator *devenum)
580 IMMDeviceCollection *devcoll;
581 HRESULT hr;
583 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
584 DEVICE_STATE_ACTIVE, &devcoll);
585 if(FAILED(hr))
586 return hr;
588 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
589 if(FAILED(hr)){
590 IMMDeviceCollection_Release(devcoll);
591 return hr;
594 if(*devcount > 0){
595 UINT n, count = 1;
596 IMMDevice *def_dev = NULL;
598 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
599 sizeof(WINMM_MMDevice) * (*devcount));
600 if(!*devices){
601 IMMDeviceCollection_Release(devcoll);
602 return E_OUTOFMEMORY;
605 *map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
606 sizeof(WINMM_MMDevice *) * (*devcount));
607 if(!*map){
608 IMMDeviceCollection_Release(devcoll);
609 HeapFree(GetProcessHeap(), 0, *devices);
610 return E_OUTOFMEMORY;
613 /* make sure that device 0 is the default device */
614 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
615 flow, eConsole, &def_dev);
617 for(n = 0; n < *devcount; ++n){
618 IMMDevice *device;
620 hr = IMMDeviceCollection_Item(devcoll, n, &device);
621 if(SUCCEEDED(hr)){
622 WINMM_InitMMDevice(flow, device, &(*devices)[n], n);
624 if(device == def_dev)
625 (*map)[0] = &(*devices)[n];
626 else{
627 (*map)[count] = &(*devices)[n];
628 ++count;
631 IMMDevice_Release(device);
635 IMMDevice_Release(def_dev);
637 *devcount = count;
640 IMMDeviceCollection_Release(devcoll);
642 return S_OK;
645 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
646 const GUID *riid, void **obj)
648 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid));
649 return E_NOINTERFACE;
652 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
654 return 2;
657 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
659 return 1;
662 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
663 const WCHAR *device_id, DWORD new_state)
665 TRACE("Ignoring OnDeviceStateChanged callback\n");
666 return S_OK;
669 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
670 const WCHAR *device_id)
672 TRACE("Ignoring OnDeviceAdded callback\n");
673 return S_OK;
676 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
677 const WCHAR *device_id)
679 TRACE("Ignoring OnDeviceRemoved callback\n");
680 return S_OK;
683 static HRESULT update_mapping(WINMM_MMDevice ***map, UINT count,
684 const WCHAR *default_id)
686 WINMM_MMDevice *prev;
687 UINT i;
689 prev = (*map)[0];
690 for(i = 0; i < count; ++i){
691 WINMM_MMDevice *tmp;
693 if(!lstrcmpW((*map)[i]->dev_id, default_id)){
694 (*map)[0] = (*map)[i];
695 (*map)[i] = prev;
697 return S_OK;
700 tmp = (*map)[i];
701 (*map)[i] = prev;
702 prev = tmp;
705 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
706 (*map)[0] = prev;
708 return S_FALSE;
711 static HRESULT reroute_mapper_device(WINMM_Device *device, BOOL is_out)
713 WINMM_OpenInfo info;
714 BOOL stopped;
715 MMRESULT mr;
716 HRESULT hr;
717 UINT64 clock_freq, clock_pos;
719 TRACE("rerouting device %p\n", device->handle);
721 EnterCriticalSection(&device->lock);
723 if(!device->open || device->acm_handle){
724 /* Windows 7 doesn't re-route ACM devices, so we don't either.
725 * Seems to be because of the data waveXxxPrepareHeader allocates. */
726 LeaveCriticalSection(&device->lock);
727 return S_FALSE;
730 stopped = device->stopped;
732 info.handle = 0;
733 info.req_device = WAVE_MAPPER;
734 info.format = device->orig_fmt;
735 info.callback = device->cb_info.callback;
736 info.cb_user = device->cb_info.user;
737 /* We have to use direct here so that we don't suddenly introduce ACM
738 * into a playing stream that hasn't been Prepared for it */
739 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT_QUERY;
740 info.reset = FALSE;
742 if(is_out)
743 mr = WOD_Open(&info);
744 else
745 mr = WID_Open(&info);
747 if(mr != MMSYSERR_NOERROR){
748 TRACE("New default device doesn't support this stream: %p\n", device->handle);
749 LeaveCriticalSection(&device->lock);
750 return S_FALSE;
753 hr = IAudioClient_Stop(device->client);
754 if(FAILED(hr))
755 WARN("Stop failed: %08x\n", hr);
757 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
758 if(FAILED(hr)){
759 WARN("GetFrequency failed: %08x\n", hr);
760 LeaveCriticalSection(&device->lock);
761 return hr;
764 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
765 if(FAILED(hr)){
766 WARN("GetPosition failed: %08x\n", hr);
767 LeaveCriticalSection(&device->lock);
768 return hr;
771 device->remainder_frames = MulDiv(clock_pos, device->samples_per_sec, clock_freq) - device->last_clock_pos;
773 info.handle = device->handle;
774 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT;
776 if(is_out){
777 WOD_Close((HWAVEOUT)device->handle);
778 device->parent = read_map(g_out_map, 0);
779 mr = WOD_Open(&info);
780 }else{
781 WID_Close((HWAVEIN)device->handle);
782 device->parent = read_map(g_in_map, 0);
783 mr = WID_Open(&info);
786 if(mr != MMSYSERR_NOERROR){
787 ERR("Opening new default device failed! %u\n", mr);
788 LeaveCriticalSection(&device->lock);
789 return E_FAIL;
792 HeapFree(GetProcessHeap(), 0, info.format);
794 if(!stopped)
795 WINMM_BeginPlaying(device);
797 LeaveCriticalSection(&device->lock);
799 return S_OK;
802 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
803 EDataFlow flow, ERole role, const WCHAR *device_id)
805 UINT i;
807 TRACE("%u %u %s\n", flow, role, wine_dbgstr_w(device_id));
809 if(role != eConsole)
810 return S_OK;
812 EnterCriticalSection(&g_devthread_lock);
814 if(flow == eRender)
815 update_mapping(&g_out_map, g_outmmdevices_count, device_id);
816 else
817 update_mapping(&g_in_map, g_inmmdevices_count, device_id);
819 for(i = 0; i < MAX_DEVICES && g_out_mapper_devices[i]; ++i)
820 reroute_mapper_device(g_out_mapper_devices[i], TRUE);
822 for(i = 0; i < MAX_DEVICES && g_in_mapper_devices[i]; ++i)
823 reroute_mapper_device(g_in_mapper_devices[i], FALSE);
825 LeaveCriticalSection(&g_devthread_lock);
827 return S_OK;
830 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
831 const WCHAR *device_id, const PROPERTYKEY key)
833 TRACE("Ignoring OnPropertyValueChanged callback\n");
834 return S_OK;
837 static IMMNotificationClientVtbl g_notif_vtbl = {
838 notif_QueryInterface,
839 notif_AddRef,
840 notif_Release,
841 notif_OnDeviceStateChanged,
842 notif_OnDeviceAdded,
843 notif_OnDeviceRemoved,
844 notif_OnDefaultDeviceChanged,
845 notif_OnPropertyValueChanged
848 static IMMNotificationClient g_notif = { &g_notif_vtbl };
850 static HRESULT WINMM_InitMMDevices(void)
852 HRESULT hr, init_hr;
853 IMMDeviceEnumerator *devenum = NULL;
855 if(g_outmmdevices_count || g_inmmdevices_count)
856 return S_FALSE;
858 init_hr = CoInitialize(NULL);
860 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
861 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
862 if(FAILED(hr))
863 goto exit;
865 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum, &g_notif);
866 if(FAILED(hr))
867 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr);
869 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_out_map, &g_outmmdevices_count,
870 eRender, devenum);
871 if(FAILED(hr)){
872 g_outmmdevices_count = 0;
873 g_inmmdevices_count = 0;
874 goto exit;
877 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_in_map, &g_inmmdevices_count,
878 eCapture, devenum);
879 if(FAILED(hr)){
880 g_inmmdevices_count = 0;
881 goto exit;
884 exit:
885 if(devenum)
886 IMMDeviceEnumerator_Release(devenum);
887 if(SUCCEEDED(init_hr))
888 CoUninitialize();
890 return hr;
893 static inline BOOL WINMM_IsMapper(UINT device)
895 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
898 static MMRESULT WINMM_TryDeviceMapping(WINMM_Device *device, WAVEFORMATEX *fmt,
899 WORD channels, DWORD freq, DWORD bits_per_samp, BOOL is_out)
901 WAVEFORMATEX target, *closer_fmt = NULL;
902 HRESULT hr;
903 MMRESULT mr;
905 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
906 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
908 target.wFormatTag = WAVE_FORMAT_PCM;
909 target.nChannels = channels;
910 target.nSamplesPerSec = freq;
911 target.wBitsPerSample = bits_per_samp;
912 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
913 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
914 target.cbSize = 0;
916 hr = IAudioClient_IsFormatSupported(device->client,
917 AUDCLNT_SHAREMODE_SHARED, &target, &closer_fmt);
918 if(closer_fmt)
919 CoTaskMemFree(closer_fmt);
920 if(hr != S_OK)
921 return WAVERR_BADFORMAT;
923 /* device supports our target format, so see if MSACM can
924 * do the conversion */
925 if(is_out)
926 mr = acmStreamOpen(&device->acm_handle, NULL, fmt, &target, NULL,
927 0, 0, 0);
928 else
929 mr = acmStreamOpen(&device->acm_handle, NULL, &target, fmt, NULL,
930 0, 0, 0);
931 if(mr != MMSYSERR_NOERROR)
932 return mr;
934 /* yes it can. initialize the audioclient and return success */
935 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
936 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
937 AC_BUFLEN, 0, &target, &device->parent->session);
938 if(hr != S_OK){
939 WARN("Initialize failed: %08x\n", hr);
940 acmStreamClose(device->acm_handle, 0);
941 device->acm_handle = NULL;
942 return MMSYSERR_ERROR;
945 device->bytes_per_frame = target.nBlockAlign;
946 device->samples_per_sec = target.nSamplesPerSec;
948 TRACE("Success!\n");
950 return MMSYSERR_NOERROR;
953 static MMRESULT WINMM_MapDevice(WINMM_Device *device, BOOL is_out)
955 MMRESULT mr;
956 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)device->orig_fmt;
958 TRACE("(%p, %u)\n", device, is_out);
960 /* set up the ACM stream */
961 if(device->orig_fmt->wFormatTag != WAVE_FORMAT_PCM &&
962 !(device->orig_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
963 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
964 /* convert to PCM format if it's not already */
965 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
966 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
967 16, is_out);
968 if(mr == MMSYSERR_NOERROR)
969 return mr;
971 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
972 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
973 8, is_out);
974 if(mr == MMSYSERR_NOERROR)
975 return mr;
976 }else{
977 WORD channels;
979 /* first try just changing bit depth and channels */
980 channels = device->orig_fmt->nChannels;
981 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
982 device->orig_fmt->nSamplesPerSec, 16, is_out);
983 if(mr == MMSYSERR_NOERROR)
984 return mr;
985 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
986 device->orig_fmt->nSamplesPerSec, 8, is_out);
987 if(mr == MMSYSERR_NOERROR)
988 return mr;
990 channels = (channels == 2) ? 1 : 2;
991 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
992 device->orig_fmt->nSamplesPerSec, 16, is_out);
993 if(mr == MMSYSERR_NOERROR)
994 return mr;
995 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
996 device->orig_fmt->nSamplesPerSec, 8, is_out);
997 if(mr == MMSYSERR_NOERROR)
998 return mr;
1000 /* that didn't work, so now try different sample rates */
1001 channels = device->orig_fmt->nChannels;
1002 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_out);
1003 if(mr == MMSYSERR_NOERROR)
1004 return mr;
1005 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_out);
1006 if(mr == MMSYSERR_NOERROR)
1007 return mr;
1008 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_out);
1009 if(mr == MMSYSERR_NOERROR)
1010 return mr;
1011 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_out);
1012 if(mr == MMSYSERR_NOERROR)
1013 return mr;
1014 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_out);
1015 if(mr == MMSYSERR_NOERROR)
1016 return mr;
1018 channels = (channels == 2) ? 1 : 2;
1019 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_out);
1020 if(mr == MMSYSERR_NOERROR)
1021 return mr;
1022 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_out);
1023 if(mr == MMSYSERR_NOERROR)
1024 return mr;
1025 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_out);
1026 if(mr == MMSYSERR_NOERROR)
1027 return mr;
1028 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_out);
1029 if(mr == MMSYSERR_NOERROR)
1030 return mr;
1031 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_out);
1032 if(mr == MMSYSERR_NOERROR)
1033 return mr;
1035 channels = device->orig_fmt->nChannels;
1036 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_out);
1037 if(mr == MMSYSERR_NOERROR)
1038 return mr;
1039 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_out);
1040 if(mr == MMSYSERR_NOERROR)
1041 return mr;
1042 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_out);
1043 if(mr == MMSYSERR_NOERROR)
1044 return mr;
1045 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_out);
1046 if(mr == MMSYSERR_NOERROR)
1047 return mr;
1048 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_out);
1049 if(mr == MMSYSERR_NOERROR)
1050 return mr;
1052 channels = (channels == 2) ? 1 : 2;
1053 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_out);
1054 if(mr == MMSYSERR_NOERROR)
1055 return mr;
1056 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_out);
1057 if(mr == MMSYSERR_NOERROR)
1058 return mr;
1059 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_out);
1060 if(mr == MMSYSERR_NOERROR)
1061 return mr;
1062 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_out);
1063 if(mr == MMSYSERR_NOERROR)
1064 return mr;
1065 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_out);
1066 if(mr == MMSYSERR_NOERROR)
1067 return mr;
1070 WARN("Unable to find compatible device!\n");
1071 return WAVERR_BADFORMAT;
1074 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_OpenInfo *info,
1075 BOOL is_out)
1077 LRESULT ret = MMSYSERR_NOMEM;
1078 HRESULT hr;
1080 hr = IMMDeviceEnumerator_GetDevice(g_devenum, device->parent->dev_id,
1081 &device->device);
1082 if(FAILED(hr)){
1083 WARN("Device %s (%s) unavailable: %08x\n",
1084 wine_dbgstr_w(device->parent->dev_id),
1085 wine_dbgstr_w(device->parent->out_caps.szPname), hr);
1086 ret = MMSYSERR_NODRIVER;
1087 goto error;
1090 /* this is where winexyz.drv opens the audio device */
1091 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
1092 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
1093 if(FAILED(hr)){
1094 WARN("Activate failed: %08x\n", hr);
1095 ret = hr2mmr(hr);
1096 if(ret == MMSYSERR_ERROR)
1097 ret = MMSYSERR_NOTENABLED;
1098 goto error;
1101 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
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 (%u)\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 if(closer_fmt)
1133 CoTaskMemFree(closer_fmt);
1134 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1135 goto error;
1138 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1139 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1140 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1141 if(FAILED(hr)){
1142 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1143 ret = WINMM_MapDevice(device, is_out);
1144 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1145 goto error;
1146 }else{
1147 WARN("Initialize failed: %08x\n", hr);
1148 ret = hr2mmr(hr);
1149 goto error;
1151 }else{
1152 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1153 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1156 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1157 (void**)&device->clock);
1158 if(FAILED(hr)){
1159 WARN("GetService failed: %08x\n", hr);
1160 goto error;
1163 if(!device->event){
1164 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1165 if(!device->event){
1166 WARN("CreateEvent failed: %08x\n", hr);
1167 goto error;
1170 /* As the devices thread is waiting on g_device_handles, it can
1171 * only be modified from within this same thread. */
1172 if(g_device_handles){
1173 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1174 sizeof(HANDLE) * (g_devhandle_count + 1));
1175 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1176 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1177 }else{
1178 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1179 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1180 sizeof(WINMM_Device *));
1182 g_device_handles[g_devhandle_count] = device->event;
1183 g_handle_devices[g_devhandle_count] = device;
1184 ++g_devhandle_count;
1187 hr = IAudioClient_SetEventHandle(device->client, device->event);
1188 if(FAILED(hr)){
1189 WARN("SetEventHandle failed: %08x\n", hr);
1190 goto error;
1193 if(info->reset){
1194 device->played_frames = 0;
1195 device->ofs_bytes = 0;
1196 device->loop_counter = 0;
1197 device->first = device->last = device->playing = device->loop_start = NULL;
1200 device->stopped = TRUE;
1201 device->last_clock_pos = 0;
1203 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1204 device->cb_info.callback = info->callback;
1205 device->cb_info.user = info->cb_user;
1206 device->cb_info.hwave = device->handle;
1208 info->handle = device->handle;
1210 return MMSYSERR_NOERROR;
1212 error:
1213 if(device->client){
1214 IAudioClient_Release(device->client);
1215 device->client = NULL;
1217 if(device->device){
1218 IMMDevice_Release(device->device);
1219 device->device = NULL;
1222 return ret;
1225 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1227 WINMM_Device *device;
1228 LRESULT ret = MMSYSERR_ERROR;
1229 HRESULT hr;
1231 if(info->handle != 0){
1232 device = WINMM_GetDeviceFromHWAVE(info->handle);
1233 if(!device){
1234 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1235 return MMSYSERR_ERROR;
1238 EnterCriticalSection(&device->lock);
1240 device->open = TRUE;
1241 }else{
1242 CRITICAL_SECTION *lock;
1243 UINT internal_index;
1244 WINMM_Device **devices;
1245 WINMM_MMDevice *mmdevice;
1247 if(WINMM_IsMapper(info->req_device)){
1248 devices = g_out_mapper_devices;
1249 mmdevice = read_map(g_out_map, 0);
1250 lock = &g_devthread_lock;
1251 internal_index = MAPPER_INDEX;
1252 }else{
1253 if(info->req_device >= g_outmmdevices_count)
1254 return MMSYSERR_BADDEVICEID;
1256 mmdevice = read_map(g_out_map, info->req_device);
1258 if(!mmdevice->out_caps.szPname[0])
1259 return MMSYSERR_NOTENABLED;
1261 devices = mmdevice->devices;
1262 lock = &mmdevice->lock;
1263 internal_index = mmdevice->index;
1266 EnterCriticalSection(lock);
1268 device = WINMM_FindUnusedDevice(devices, mmdevice,
1269 internal_index, TRUE);
1270 if(!device){
1271 LeaveCriticalSection(lock);
1272 return MMSYSERR_ALLOCATED;
1275 LeaveCriticalSection(lock);
1278 ret = WINMM_OpenDevice(device, info, TRUE);
1279 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1280 goto error;
1281 ret = MMSYSERR_ERROR;
1283 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1284 (void**)&device->render);
1285 if(FAILED(hr)){
1286 ERR("GetService failed: %08x\n", hr);
1287 goto error;
1290 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1291 (void**)&device->volume);
1292 if(FAILED(hr)){
1293 ERR("GetService failed: %08x\n", hr);
1294 goto error;
1297 LeaveCriticalSection(&device->lock);
1299 return MMSYSERR_NOERROR;
1301 error:
1302 if(device->device){
1303 IMMDevice_Release(device->device);
1304 device->device = NULL;
1306 if(device->client){
1307 IAudioClient_Release(device->client);
1308 device->client = NULL;
1310 if(device->render){
1311 IAudioRenderClient_Release(device->render);
1312 device->render = NULL;
1314 if(device->volume){
1315 IAudioStreamVolume_Release(device->volume);
1316 device->volume = NULL;
1318 if(device->clock){
1319 IAudioClock_Release(device->clock);
1320 device->clock = NULL;
1322 device->open = FALSE;
1323 LeaveCriticalSection(&device->lock);
1324 return ret;
1327 static LRESULT WID_Open(WINMM_OpenInfo *info)
1329 WINMM_Device *device, **devices;
1330 WINMM_MMDevice *mmdevice;
1331 UINT internal_index;
1332 CRITICAL_SECTION *lock;
1333 LRESULT ret = MMSYSERR_ERROR;
1334 HRESULT hr;
1336 if(WINMM_IsMapper(info->req_device)){
1337 devices = g_in_mapper_devices;
1338 mmdevice = read_map(g_in_map, 0);
1339 lock = &g_devthread_lock;
1340 internal_index = MAPPER_INDEX;
1341 }else{
1342 if(info->req_device >= g_inmmdevices_count)
1343 return MMSYSERR_BADDEVICEID;
1345 mmdevice = read_map(g_in_map, info->req_device);
1347 if(!mmdevice->in_caps.szPname[0])
1348 return MMSYSERR_NOTENABLED;
1350 devices = mmdevice->devices;
1351 lock = &mmdevice->lock;
1352 internal_index = mmdevice->index;
1355 EnterCriticalSection(lock);
1357 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1358 if(!device){
1359 LeaveCriticalSection(lock);
1360 return MMSYSERR_ALLOCATED;
1363 LeaveCriticalSection(lock);
1365 ret = WINMM_OpenDevice(device, info, FALSE);
1366 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1367 goto error;
1368 ret = MMSYSERR_ERROR;
1370 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1371 (void**)&device->capture);
1372 if(FAILED(hr)){
1373 WARN("GetService failed: %08x\n", hr);
1374 goto error;
1377 LeaveCriticalSection(&device->lock);
1379 return MMSYSERR_NOERROR;
1381 error:
1382 if(device->device){
1383 IMMDevice_Release(device->device);
1384 device->device = NULL;
1386 if(device->client){
1387 IAudioClient_Release(device->client);
1388 device->client = NULL;
1390 if(device->capture){
1391 IAudioCaptureClient_Release(device->capture);
1392 device->capture = NULL;
1394 if(device->clock){
1395 IAudioClock_Release(device->clock);
1396 device->clock = NULL;
1398 device->open = FALSE;
1399 LeaveCriticalSection(&device->lock);
1400 return ret;
1403 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1405 device->open = FALSE;
1407 if(!device->stopped){
1408 IAudioClient_Stop(device->client);
1409 device->stopped = TRUE;
1412 if(device->acm_handle){
1413 acmStreamClose(device->acm_handle, 0);
1414 device->acm_handle = NULL;
1417 IMMDevice_Release(device->device);
1418 device->device = NULL;
1420 IAudioClient_Release(device->client);
1421 device->client = NULL;
1423 IAudioClock_Release(device->clock);
1424 device->clock = NULL;
1426 return S_OK;
1429 static LRESULT WOD_Close(HWAVEOUT hwave)
1431 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1433 TRACE("(%p)\n", hwave);
1435 if(!WINMM_ValidateAndLock(device))
1436 return MMSYSERR_INVALHANDLE;
1438 WINMM_CloseDevice(device);
1440 IAudioRenderClient_Release(device->render);
1441 device->render = NULL;
1443 IAudioStreamVolume_Release(device->volume);
1444 device->volume = NULL;
1446 LeaveCriticalSection(&device->lock);
1448 return MMSYSERR_NOERROR;
1451 static LRESULT WID_Close(HWAVEIN hwave)
1453 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1455 TRACE("(%p)\n", hwave);
1457 if(!WINMM_ValidateAndLock(device))
1458 return MMSYSERR_INVALHANDLE;
1460 WINMM_CloseDevice(device);
1462 IAudioCaptureClient_Release(device->capture);
1463 device->capture = NULL;
1465 LeaveCriticalSection(&device->lock);
1467 return MMSYSERR_NOERROR;
1470 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1472 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1474 TRACE("(%p, %p)\n", hwave, header);
1476 if(!WINMM_ValidateAndLock(device))
1477 return MMSYSERR_INVALHANDLE;
1479 if(device->render && device->acm_handle){
1480 ACMSTREAMHEADER *ash;
1481 DWORD size;
1482 MMRESULT mr;
1484 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1485 ACM_STREAMSIZEF_SOURCE);
1486 if(mr != MMSYSERR_NOERROR){
1487 LeaveCriticalSection(&device->lock);
1488 return mr;
1491 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1492 if(!ash){
1493 LeaveCriticalSection(&device->lock);
1494 return MMSYSERR_NOMEM;
1497 ash->cbStruct = sizeof(*ash);
1498 ash->fdwStatus = 0;
1499 ash->dwUser = (DWORD_PTR)header;
1500 ash->pbSrc = (BYTE*)header->lpData;
1501 ash->cbSrcLength = header->dwBufferLength;
1502 ash->dwSrcUser = header->dwUser;
1503 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1504 ash->cbDstLength = size;
1505 ash->dwDstUser = 0;
1507 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1508 if(mr != MMSYSERR_NOERROR){
1509 HeapFree(GetProcessHeap(), 0, ash);
1510 LeaveCriticalSection(&device->lock);
1511 return mr;
1514 header->reserved = (DWORD_PTR)ash;
1517 LeaveCriticalSection(&device->lock);
1519 header->dwFlags |= WHDR_PREPARED;
1520 header->dwFlags &= ~WHDR_DONE;
1522 return MMSYSERR_NOERROR;
1525 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1527 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1529 TRACE("(%p, %p)\n", hwave, header);
1531 if(!WINMM_ValidateAndLock(device))
1532 return MMSYSERR_INVALHANDLE;
1534 if(device->render && device->acm_handle){
1535 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1537 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1539 HeapFree(GetProcessHeap(), 0, ash);
1542 LeaveCriticalSection(&device->lock);
1544 header->dwFlags &= ~WHDR_PREPARED;
1545 header->dwFlags |= WHDR_DONE;
1547 return MMSYSERR_NOERROR;
1550 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1552 if(device->acm_handle){
1553 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1554 return ash->cbDstLengthUsed;
1557 return header->dwBufferLength;
1560 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1562 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1565 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1567 HRESULT hr;
1568 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1569 UINT64 clock_freq, clock_pos, clock_frames;
1570 UINT32 nloops, queue_frames = 0;
1572 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1573 if(FAILED(hr)){
1574 WARN("GetFrequency failed: %08x\n", hr);
1575 return NULL;
1578 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1579 if(FAILED(hr)){
1580 WARN("GetPosition failed: %08x\n", hr);
1581 return NULL;
1584 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1586 nloops = device->loop_counter;
1587 while(queue &&
1588 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1589 clock_frames - device->last_clock_pos + device->remainder_frames){
1590 if(!nloops){
1591 last = queue;
1592 device->last_clock_pos += queue_frames;
1593 device->remainder_frames = 0;
1594 queue_frames = 0;
1595 queue = device->first = queue->lpNext;
1596 }else{
1597 if(queue->dwFlags & WHDR_BEGINLOOP){
1598 if(queue->dwFlags & WHDR_ENDLOOP)
1599 --nloops;
1600 else
1601 queue = queue->lpNext;
1602 }else if(queue->dwFlags & WHDR_ENDLOOP){
1603 queue = device->loop_start;
1604 --nloops;
1609 if(last){
1610 last->lpNext = NULL;
1611 return first;
1612 }else
1613 return NULL;
1616 static void WOD_PushData(WINMM_Device *device)
1618 WINMM_CBInfo cb_info;
1619 HRESULT hr;
1620 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1621 UINT32 queue_bytes, nloops;
1622 BYTE *data;
1623 WAVEHDR *queue, *first = NULL;
1625 TRACE("(%p)\n", device->handle);
1627 EnterCriticalSection(&device->lock);
1629 if(!device->device)
1630 goto exit;
1632 if(!device->first){
1633 if (device->stopped)
1634 goto exit;
1635 device->stopped = TRUE;
1636 device->last_clock_pos = 0;
1637 IAudioClient_Stop(device->client);
1638 IAudioClient_Reset(device->client);
1639 goto exit;
1642 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1643 if(FAILED(hr)){
1644 WARN("GetBufferSize failed: %08x\n", hr);
1645 goto exit;
1648 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1649 if(FAILED(hr)){
1650 WARN("GetCurrentPadding failed: %08x\n", hr);
1651 goto exit;
1654 first = WOD_MarkDoneHeaders(device);
1656 /* determine which is larger between the available buffer size and
1657 * the amount of data left in the queue */
1658 avail_frames = bufsize - pad;
1660 queue = device->playing;
1661 ofs = device->ofs_bytes;
1662 queue_frames = 0;
1663 nloops = 0;
1664 while(queue && queue_frames < avail_frames){
1665 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1666 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1667 ofs = 0;
1669 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1670 queue = device->loop_start;
1671 ++nloops;
1672 }else
1673 queue = queue->lpNext;
1676 if(queue_frames < avail_frames)
1677 avail_frames = queue_frames;
1678 if(avail_frames == 0)
1679 goto exit;
1681 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1682 if(FAILED(hr)){
1683 WARN("GetBuffer failed: %08x\n", hr);
1684 goto exit;
1687 written = 0;
1688 while(device->playing && written < avail_frames){
1689 UINT32 copy_frames, copy_bytes;
1690 BYTE *queue_data;
1692 queue = device->playing;
1694 if(device->acm_handle){
1695 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)queue->reserved;
1696 queue_bytes = ash->cbDstLengthUsed;
1697 queue_data = ash->pbDst;
1698 }else{
1699 queue_bytes = queue->dwBufferLength;
1700 queue_data = (BYTE*)queue->lpData;
1703 queue_frames = (queue_bytes - device->ofs_bytes) /
1704 device->bytes_per_frame;
1706 copy_frames = queue_frames < (avail_frames - written) ?
1707 queue_frames : avail_frames - written;
1708 copy_bytes = copy_frames * device->bytes_per_frame;
1710 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1712 data += copy_bytes;
1713 written += copy_frames;
1714 device->ofs_bytes += copy_bytes;
1716 if(device->ofs_bytes >= queue_bytes){
1717 device->ofs_bytes = 0;
1719 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1720 device->playing = queue->lpNext;
1721 else{
1722 if(queue->dwFlags & WHDR_BEGINLOOP){
1723 device->loop_start = device->playing;
1724 device->playing = queue->lpNext;
1725 device->loop_counter = queue->dwLoops;
1727 if(queue->dwFlags & WHDR_ENDLOOP){
1728 --device->loop_counter;
1729 if(device->loop_counter)
1730 device->playing = device->loop_start;
1731 else
1732 device->loop_start = device->playing = queue->lpNext;
1738 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1739 if(FAILED(hr)){
1740 WARN("ReleaseBuffer failed: %08x\n", hr);
1741 goto exit;
1744 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1745 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1746 else
1747 device->played_frames += avail_frames;
1749 exit:
1750 cb_info = device->cb_info;
1752 LeaveCriticalSection(&device->lock);
1754 while(first){
1755 WAVEHDR *next = first->lpNext;
1756 first->dwFlags &= ~WHDR_INQUEUE;
1757 first->dwFlags |= WHDR_DONE;
1758 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1759 first = next;
1763 static void WID_PullACMData(WINMM_Device *device)
1765 UINT32 packet, packet_bytes;
1766 DWORD flags;
1767 BYTE *data;
1768 WAVEHDR *queue;
1769 HRESULT hr;
1770 MMRESULT mr;
1772 if(device->acm_hdr.cbDstLength == 0){
1773 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1774 &flags, NULL, NULL);
1775 if(hr != S_OK){
1776 if(FAILED(hr))
1777 WARN("GetBuffer failed: %08x\n", hr);
1778 return;
1781 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1782 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1784 device->acm_offs = 0;
1786 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1787 device->acm_hdr.fdwStatus = 0;
1788 device->acm_hdr.dwUser = 0;
1789 device->acm_hdr.pbSrc = data;
1790 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1791 device->acm_hdr.cbSrcLengthUsed = 0;
1792 device->acm_hdr.dwSrcUser = 0;
1793 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1794 device->acm_hdr.cbDstLength = packet_bytes;
1795 device->acm_hdr.cbDstLengthUsed = 0;
1796 device->acm_hdr.dwDstUser = 0;
1798 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1799 if(mr != MMSYSERR_NOERROR){
1800 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1801 return;
1804 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1805 if(mr != MMSYSERR_NOERROR){
1806 WARN("acmStreamConvert failed: %d\n", mr);
1807 return;
1810 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1811 if(FAILED(hr))
1812 WARN("ReleaseBuffer failed: %08x\n", hr);
1814 device->played_frames += packet;
1817 queue = device->first;
1818 while(queue){
1819 UINT32 to_copy_bytes;
1821 to_copy_bytes = min(queue->dwBufferLength - queue->dwBytesRecorded,
1822 device->acm_hdr.cbDstLengthUsed - device->acm_offs);
1824 memcpy(queue->lpData + queue->dwBytesRecorded,
1825 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1827 queue->dwBytesRecorded += to_copy_bytes;
1828 device->acm_offs += to_copy_bytes;
1830 if(queue->dwBufferLength - queue->dwBytesRecorded <
1831 device->bytes_per_frame){
1832 queue->dwFlags &= ~WHDR_INQUEUE;
1833 queue->dwFlags |= WHDR_DONE;
1834 device->first = queue = queue->lpNext;
1837 if(device->acm_offs >= device->acm_hdr.cbDstLengthUsed){
1838 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1839 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1840 device->acm_hdr.cbDstLength = 0;
1841 device->acm_hdr.cbDstLengthUsed = 0;
1843 /* done with this ACM Header, so try to pull more data */
1844 WID_PullACMData(device);
1845 return;
1849 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1850 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1851 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1852 device->acm_hdr.cbDstLength = 0;
1853 device->acm_hdr.cbDstLengthUsed = 0;
1856 static void WID_PullData(WINMM_Device *device)
1858 WINMM_CBInfo cb_info;
1859 WAVEHDR *queue, *first = NULL, *last = NULL;
1860 HRESULT hr;
1862 TRACE("(%p)\n", device->handle);
1864 EnterCriticalSection(&device->lock);
1866 if(!device->device || !device->first)
1867 goto exit;
1869 first = device->first;
1871 if(device->acm_handle){
1872 WID_PullACMData(device);
1873 goto exit;
1876 while(device->first){
1877 BYTE *data;
1878 UINT32 packet_len, packet;
1879 DWORD flags;
1881 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1882 &flags, NULL, NULL);
1883 if(hr != S_OK){
1884 if(FAILED(hr))
1885 WARN("GetBuffer failed: %08x\n", hr);
1886 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1887 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1888 goto exit;
1891 packet = packet_len;
1892 queue = device->first;
1893 while(queue && packet > 0){
1894 UINT32 to_copy_bytes;
1896 to_copy_bytes = min(packet * device->bytes_per_frame,
1897 queue->dwBufferLength - queue->dwBytesRecorded);
1899 memcpy(queue->lpData + queue->dwBytesRecorded,
1900 data + (packet_len - packet) * device->bytes_per_frame,
1901 to_copy_bytes);
1903 queue->dwBytesRecorded += to_copy_bytes;
1905 if(queue->dwBufferLength - queue->dwBytesRecorded <
1906 device->bytes_per_frame){
1907 last = queue;
1908 device->first = queue = queue->lpNext;
1911 packet -= to_copy_bytes / device->bytes_per_frame;
1914 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1915 if(FAILED(hr))
1916 WARN("ReleaseBuffer failed: %08x\n", hr);
1918 if(packet > 0)
1919 WARN("losing %u frames\n", packet);
1920 device->played_frames += packet_len;
1923 exit:
1924 cb_info = device->cb_info;
1926 LeaveCriticalSection(&device->lock);
1928 if(last){
1929 last->lpNext = NULL;
1930 while(first){
1931 WAVEHDR *next = first->lpNext;
1932 first->dwFlags &= ~WHDR_INQUEUE;
1933 first->dwFlags |= WHDR_DONE;
1934 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1935 first = next;
1940 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1942 HRESULT hr;
1944 TRACE("(%p)\n", device->handle);
1946 if(device->render)
1947 /* prebuffer data before starting */
1948 WOD_PushData(device);
1950 if(device->stopped){
1951 device->stopped = FALSE;
1953 hr = IAudioClient_Start(device->client);
1954 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1955 device->stopped = TRUE;
1956 WARN("Start failed: %08x\n", hr);
1957 return MMSYSERR_ERROR;
1961 return MMSYSERR_NOERROR;
1964 static LRESULT WINMM_Pause(HWAVE hwave)
1966 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1967 HRESULT hr;
1969 TRACE("(%p)\n", hwave);
1971 if(!WINMM_ValidateAndLock(device))
1972 return MMSYSERR_INVALHANDLE;
1974 hr = IAudioClient_Stop(device->client);
1975 if(FAILED(hr)){
1976 LeaveCriticalSection(&device->lock);
1977 WARN("Stop failed: %08x\n", hr);
1978 return MMSYSERR_ERROR;
1981 device->stopped = FALSE;
1983 LeaveCriticalSection(&device->lock);
1985 return MMSYSERR_NOERROR;
1988 static LRESULT WINMM_Reset(HWAVE hwave)
1990 WINMM_CBInfo cb_info;
1991 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1992 BOOL is_out;
1993 WAVEHDR *first;
1994 HRESULT hr;
1996 TRACE("(%p)\n", hwave);
1998 if(!WINMM_ValidateAndLock(device))
1999 return MMSYSERR_INVALHANDLE;
2001 hr = IAudioClient_Stop(device->client);
2002 if(FAILED(hr)){
2003 LeaveCriticalSection(&device->lock);
2004 WARN("Stop failed: %08x\n", hr);
2005 return MMSYSERR_ERROR;
2007 device->stopped = TRUE;
2009 first = device->first;
2010 device->first = device->last = device->playing = NULL;
2011 device->ofs_bytes = 0;
2012 device->played_frames = 0;
2013 device->loop_counter = 0;
2014 device->last_clock_pos = 0;
2015 IAudioClient_Reset(device->client);
2017 cb_info = device->cb_info;
2018 is_out = device->render != NULL;
2020 LeaveCriticalSection(&device->lock);
2022 while(first){
2023 WAVEHDR *next = first->lpNext;
2024 first->dwFlags &= ~WHDR_INQUEUE;
2025 first->dwFlags |= WHDR_DONE;
2026 if(is_out)
2027 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2028 else
2029 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2030 first = next;
2033 return MMSYSERR_NOERROR;
2036 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2037 UINT32 sample_rate, UINT32 bytes_per_frame)
2039 switch(time->wType){
2040 case TIME_SAMPLES:
2041 time->u.sample = played_frames;
2042 return MMSYSERR_NOERROR;
2043 case TIME_MS:
2044 time->u.ms = (UINT64)played_frames * 1000 / sample_rate;
2045 return MMSYSERR_NOERROR;
2046 case TIME_SMPTE:
2047 time->u.smpte.fps = 30;
2048 played_frames += sample_rate / time->u.smpte.fps - 1; /* round up */
2049 time->u.smpte.frame = (played_frames % sample_rate) * time->u.smpte.fps / sample_rate;
2050 played_frames /= sample_rate; /* yields seconds */
2051 time->u.smpte.sec = played_frames % 60;
2052 played_frames /= 60;
2053 time->u.smpte.min = played_frames % 60;
2054 time->u.smpte.hour= played_frames / 60;
2055 return MMSYSERR_NOERROR;
2056 default:
2057 time->wType = TIME_BYTES;
2058 /* fall through */
2059 case TIME_BYTES:
2060 time->u.cb = played_frames * bytes_per_frame;
2061 return MMSYSERR_NOERROR;
2065 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2067 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2068 UINT32 played_frames, sample_rate, bytes_per_frame;
2070 TRACE("(%p, %p)\n", hwave, time);
2072 if(!WINMM_ValidateAndLock(device))
2073 return MMSYSERR_INVALHANDLE;
2075 played_frames = device->played_frames;
2076 sample_rate = device->orig_fmt->nSamplesPerSec;
2077 bytes_per_frame = device->orig_fmt->nBlockAlign;
2079 LeaveCriticalSection(&device->lock);
2081 return WINMM_FramesToMMTime(time, played_frames, sample_rate,
2082 bytes_per_frame);
2085 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2086 UINT *mmdev_index)
2088 UINT mmdev, dev, junk, *out;
2089 BOOL is_out;
2091 if(!mmdev_index)
2092 out = &mmdev;
2093 else
2094 out = mmdev_index;
2096 switch(flags & 0xF0000000){
2097 case MIXER_OBJECTF_MIXER: /* == 0 */
2098 *out = HandleToULong(hmix);
2099 if(*out < g_outmmdevices_count)
2100 return read_map(g_out_map, *out);
2101 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2102 *out -= g_outmmdevices_count;
2103 return read_map(g_in_map, *out);
2105 /* fall through -- if it's not a valid mixer device, then
2106 * it could be a valid mixer handle. windows seems to do
2107 * this as well. */
2108 case MIXER_OBJECTF_HMIXER:
2109 case MIXER_OBJECTF_HWAVEOUT:
2110 case MIXER_OBJECTF_HWAVEIN:
2111 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2112 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2113 (!is_out && *out >= g_inmmdevices_count))
2114 return NULL;
2115 if(is_out)
2116 return read_map(g_out_map, *out);
2117 return read_map(g_in_map, *out);
2118 case MIXER_OBJECTF_WAVEOUT:
2119 *out = HandleToULong(hmix);
2120 if(*out < g_outmmdevices_count)
2121 return read_map(g_out_map, *out);
2122 return NULL;
2123 case MIXER_OBJECTF_WAVEIN:
2124 *out = HandleToULong(hmix);
2125 if(*out < g_inmmdevices_count)
2126 return read_map(g_in_map, *out);
2127 return NULL;
2130 return NULL;
2133 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2135 IAudioSessionManager *sesman;
2136 IMMDevice *device;
2137 HRESULT hr;
2139 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2140 if(FAILED(hr)){
2141 WARN("Device %s (%s) unavailable: %08x\n",
2142 wine_dbgstr_w(mmdevice->dev_id),
2143 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2144 return MMSYSERR_ERROR;
2147 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2148 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2149 if(FAILED(hr)){
2150 WARN("Activate failed: %08x\n", hr);
2151 IMMDevice_Release(device);
2152 return MMSYSERR_ERROR;
2155 IMMDevice_Release(device);
2157 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2158 FALSE, &mmdevice->volume);
2159 IAudioSessionManager_Release(sesman);
2160 if(FAILED(hr)){
2161 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2162 return MMSYSERR_ERROR;
2165 return MMSYSERR_NOERROR;
2168 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2170 WINMM_MMDevice *mmdevice;
2171 MIXERCONTROLDETAILS *control = details->details;
2172 HRESULT hr;
2174 TRACE("(%p)\n", details->hmix);
2176 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2177 if(!mmdevice)
2178 return MMSYSERR_INVALHANDLE;
2180 EnterCriticalSection(&mmdevice->lock);
2182 if(!mmdevice->volume){
2183 MMRESULT mr;
2185 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2186 if(mr != MMSYSERR_NOERROR){
2187 LeaveCriticalSection(&mmdevice->lock);
2188 return mr;
2192 if(control->dwControlID == 0){
2193 float vol;
2194 MIXERCONTROLDETAILS_UNSIGNED *udet;
2196 if(!control->paDetails ||
2197 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2198 LeaveCriticalSection(&mmdevice->lock);
2199 return MMSYSERR_INVALPARAM;
2202 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2203 if(FAILED(hr)){
2204 WARN("GetMasterVolume failed: %08x\n", hr);
2205 LeaveCriticalSection(&mmdevice->lock);
2206 return MMSYSERR_ERROR;
2209 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2210 udet->dwValue = vol * ((unsigned int)0xFFFF);
2211 }else if(control->dwControlID == 1){
2212 BOOL mute;
2213 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2215 if(!control->paDetails ||
2216 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2217 LeaveCriticalSection(&mmdevice->lock);
2218 return MMSYSERR_INVALPARAM;
2221 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2222 if(FAILED(hr)){
2223 WARN("GetMute failed: %08x\n", hr);
2224 LeaveCriticalSection(&mmdevice->lock);
2225 return MMSYSERR_ERROR;
2228 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2229 bdet->fValue = mute;
2230 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2231 FIXME("What should the sw-side mixer controls map to?\n");
2232 }else{
2233 LeaveCriticalSection(&mmdevice->lock);
2234 return MIXERR_INVALCONTROL;
2237 LeaveCriticalSection(&mmdevice->lock);
2239 return MMSYSERR_NOERROR;
2242 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2244 WINMM_MMDevice *mmdevice;
2245 MIXERCONTROLDETAILS *control = details->details;
2246 HRESULT hr;
2248 TRACE("(%p)\n", details->hmix);
2250 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2251 if(!mmdevice)
2252 return MMSYSERR_INVALHANDLE;
2254 EnterCriticalSection(&mmdevice->lock);
2256 if(!mmdevice->volume){
2257 MMRESULT mr;
2259 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2260 if(mr != MMSYSERR_NOERROR){
2261 LeaveCriticalSection(&mmdevice->lock);
2262 return mr;
2266 if(control->dwControlID == 0){
2267 float vol;
2268 MIXERCONTROLDETAILS_UNSIGNED *udet;
2270 if(!control->paDetails ||
2271 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2272 LeaveCriticalSection(&mmdevice->lock);
2273 return MMSYSERR_INVALPARAM;
2276 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2278 if(udet->dwValue > 65535){
2279 LeaveCriticalSection(&mmdevice->lock);
2280 return MMSYSERR_INVALPARAM;
2283 vol = udet->dwValue / 65535.f;
2285 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2286 if(FAILED(hr)){
2287 WARN("SetMasterVolume failed: %08x\n", hr);
2288 LeaveCriticalSection(&mmdevice->lock);
2289 return MMSYSERR_ERROR;
2291 }else if(control->dwControlID == 1){
2292 BOOL mute;
2293 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2295 if(!control->paDetails ||
2296 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2297 LeaveCriticalSection(&mmdevice->lock);
2298 return MMSYSERR_INVALPARAM;
2301 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2302 mute = bdet->fValue;
2304 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2305 if(FAILED(hr)){
2306 WARN("SetMute failed: %08x\n", hr);
2307 LeaveCriticalSection(&mmdevice->lock);
2308 return MMSYSERR_ERROR;
2310 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2311 FIXME("What should the sw-side mixer controls map to?\n");
2312 }else{
2313 LeaveCriticalSection(&mmdevice->lock);
2314 return MIXERR_INVALCONTROL;
2317 LeaveCriticalSection(&mmdevice->lock);
2319 return MMSYSERR_NOERROR;
2322 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2324 WINMM_MMDevice *mmdevice;
2325 IMMDevice *device;
2326 IPropertyStore *ps;
2327 PROPVARIANT pv;
2328 DWORD len_bytes;
2329 HRESULT hr;
2331 static const PROPERTYKEY deviceinterface_key = {
2332 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2335 if(WINMM_IsMapper(info->index)){
2336 if(info->str){
2337 if(*info->len_bytes < sizeof(WCHAR))
2338 return MMSYSERR_INVALPARAM;
2339 *info->str = 0;
2340 }else
2341 *info->len_bytes = sizeof(WCHAR);
2342 return MMSYSERR_NOERROR;
2345 if(info->is_out){
2346 if(info->index >= g_outmmdevices_count)
2347 return MMSYSERR_INVALHANDLE;
2349 mmdevice = &g_out_mmdevices[info->index];
2350 }else{
2351 if(info->index >= g_inmmdevices_count)
2352 return MMSYSERR_INVALHANDLE;
2354 mmdevice = &g_in_mmdevices[info->index];
2357 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2358 &device);
2359 if(FAILED(hr)){
2360 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2361 return MMSYSERR_ERROR;
2364 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2365 if(FAILED(hr)){
2366 WARN("OpenPropertyStore failed: %08x\n", hr);
2367 IMMDevice_Release(device);
2368 return MMSYSERR_ERROR;
2371 PropVariantInit(&pv);
2372 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2373 if(FAILED(hr)){
2374 WARN("GetValue failed: %08x\n", hr);
2375 IPropertyStore_Release(ps);
2376 IMMDevice_Release(device);
2377 return MMSYSERR_ERROR;
2379 if(pv.vt != VT_LPWSTR){
2380 WARN("Got unexpected property type: %u\n", pv.vt);
2381 PropVariantClear(&pv);
2382 IPropertyStore_Release(ps);
2383 IMMDevice_Release(device);
2384 return MMSYSERR_ERROR;
2387 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2389 if(info->str){
2390 if(len_bytes > *info->len_bytes){
2391 PropVariantClear(&pv);
2392 IPropertyStore_Release(ps);
2393 IMMDevice_Release(device);
2394 return MMSYSERR_INVALPARAM;
2397 memcpy(info->str, pv.u.pwszVal, len_bytes);
2398 }else
2399 *info->len_bytes = len_bytes;
2401 PropVariantClear(&pv);
2402 IPropertyStore_Release(ps);
2403 IMMDevice_Release(device);
2405 return MMSYSERR_NOERROR;
2408 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2409 LPARAM lparam)
2411 switch(msg){
2412 case WODM_OPEN:
2413 return WOD_Open((WINMM_OpenInfo*)wparam);
2414 case WODM_CLOSE:
2415 return WOD_Close((HWAVEOUT)wparam);
2416 case WIDM_OPEN:
2417 return WID_Open((WINMM_OpenInfo*)wparam);
2418 case WIDM_CLOSE:
2419 return WID_Close((HWAVEIN)wparam);
2420 case MXDM_GETCONTROLDETAILS:
2421 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2422 case MXDM_SETCONTROLDETAILS:
2423 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2424 case DRV_QUERYDEVICEINTERFACESIZE:
2425 case DRV_QUERYDEVICEINTERFACE:
2426 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2427 case WINMM_WM_QUIT:
2428 TRACE("QUIT message received\n");
2429 DestroyWindow(g_devices_hwnd);
2430 g_devices_hwnd = NULL;
2431 IMMDeviceEnumerator_Release(g_devenum);
2432 CoUninitialize();
2433 return 0;
2435 return DefWindowProcW(hwnd, msg, wparam, lparam);
2438 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2440 HANDLE evt = arg;
2441 HRESULT hr;
2442 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2444 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2445 if(FAILED(hr)){
2446 WARN("CoInitializeEx failed: %08x\n", hr);
2447 return 1;
2450 hr = WINMM_InitMMDevices();
2451 if(FAILED(hr)){
2452 CoUninitialize();
2453 return 1;
2456 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2457 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2458 if(FAILED(hr)){
2459 WARN("CoCreateInstance failed: %08x\n", hr);
2460 CoUninitialize();
2461 return 1;
2464 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2465 HWND_MESSAGE, NULL, NULL, NULL);
2466 if(!g_devices_hwnd){
2467 WARN("CreateWindow failed: %d\n", GetLastError());
2468 IMMDeviceEnumerator_Release(g_devenum);
2469 CoUninitialize();
2470 return 1;
2473 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2474 (LONG_PTR)WINMM_DevicesMsgProc);
2476 /* inform caller that the thread is ready to process messages */
2477 SetEvent(evt);
2478 evt = NULL; /* do not use after this point */
2480 while(1){
2481 DWORD wait;
2482 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2483 FALSE, INFINITE, QS_ALLINPUT);
2484 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2485 MSG msg;
2486 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2487 WARN("Unexpected message: 0x%x\n", msg.message);
2488 if(!g_devices_hwnd)
2489 break;
2490 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2491 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2492 if(device->render)
2493 WOD_PushData(device);
2494 else
2495 WID_PullData(device);
2496 }else
2497 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2498 GetLastError());
2501 return 0;
2504 static BOOL WINMM_StartDevicesThread(void)
2506 HANDLE events[2];
2507 DWORD wait;
2509 EnterCriticalSection(&g_devthread_lock);
2511 if(g_devices_thread){
2512 DWORD wait;
2514 wait = WaitForSingleObject(g_devices_thread, 0);
2515 if(wait == WAIT_TIMEOUT){
2516 LeaveCriticalSection(&g_devthread_lock);
2517 return TRUE;
2519 if(wait != WAIT_OBJECT_0){
2520 LeaveCriticalSection(&g_devthread_lock);
2521 return FALSE;
2524 g_devices_thread = NULL;
2525 g_devices_hwnd = NULL;
2528 TRACE("Starting up devices thread\n");
2530 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2532 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2533 events[0], 0, NULL);
2534 if(!g_devices_thread){
2535 LeaveCriticalSection(&g_devthread_lock);
2536 CloseHandle(events[0]);
2537 return FALSE;
2540 events[1] = g_devices_thread;
2541 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2542 CloseHandle(events[0]);
2543 if(wait != WAIT_OBJECT_0){
2544 if(wait == 1 + WAIT_OBJECT_0){
2545 CloseHandle(g_devices_thread);
2546 g_devices_thread = NULL;
2547 g_devices_hwnd = NULL;
2549 LeaveCriticalSection(&g_devthread_lock);
2550 return FALSE;
2553 LeaveCriticalSection(&g_devthread_lock);
2555 return TRUE;
2558 /**************************************************************************
2559 * waveOutGetNumDevs [WINMM.@]
2561 UINT WINAPI waveOutGetNumDevs(void)
2563 HRESULT hr = WINMM_InitMMDevices();
2564 if(FAILED(hr))
2565 return 0;
2567 TRACE("count: %u\n", g_outmmdevices_count);
2569 return g_outmmdevices_count;
2572 /**************************************************************************
2573 * waveOutGetDevCapsA [WINMM.@]
2575 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2576 UINT uSize)
2578 WAVEOUTCAPSW wocW;
2579 UINT ret;
2581 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2583 if(!lpCaps)
2584 return MMSYSERR_INVALPARAM;
2586 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2588 if (ret == MMSYSERR_NOERROR) {
2589 WAVEOUTCAPSA wocA;
2590 wocA.wMid = wocW.wMid;
2591 wocA.wPid = wocW.wPid;
2592 wocA.vDriverVersion = wocW.vDriverVersion;
2593 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2594 sizeof(wocA.szPname), NULL, NULL );
2595 wocA.dwFormats = wocW.dwFormats;
2596 wocA.wChannels = wocW.wChannels;
2597 wocA.dwSupport = wocW.dwSupport;
2598 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2600 return ret;
2603 /**************************************************************************
2604 * waveOutGetDevCapsW [WINMM.@]
2606 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2607 UINT uSize)
2609 WAVEOUTCAPSW mapper_caps, *caps;
2610 HRESULT hr;
2612 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2614 hr = WINMM_InitMMDevices();
2615 if(FAILED(hr))
2616 return MMSYSERR_NODRIVER;
2618 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2620 if(WINMM_IsMapper(uDeviceID)){
2621 /* FIXME: Should be localized */
2622 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2623 'n','d',' ','M','a','p','p','e','r',0};
2625 mapper_caps.wMid = 0xFF;
2626 mapper_caps.wPid = 0xFF;
2627 mapper_caps.vDriverVersion = 0x00010001;
2628 mapper_caps.dwFormats = 0xFFFFFFFF;
2629 mapper_caps.wReserved1 = 0;
2630 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2631 WAVECAPS_SAMPLEACCURATE;
2632 mapper_caps.wChannels = 2;
2633 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2635 caps = &mapper_caps;
2636 }else{
2637 if(uDeviceID >= g_outmmdevices_count)
2638 return MMSYSERR_BADDEVICEID;
2640 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2643 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2645 return MMSYSERR_NOERROR;
2648 /**************************************************************************
2649 * waveOutGetErrorTextA [WINMM.@]
2650 * waveInGetErrorTextA [WINMM.@]
2652 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2654 UINT ret;
2656 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2657 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2658 else
2660 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2661 if (!xstr) ret = MMSYSERR_NOMEM;
2662 else
2664 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2665 if (ret == MMSYSERR_NOERROR)
2666 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2667 HeapFree(GetProcessHeap(), 0, xstr);
2670 return ret;
2673 /**************************************************************************
2674 * waveOutGetErrorTextW [WINMM.@]
2675 * waveInGetErrorTextW [WINMM.@]
2677 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2679 UINT ret = MMSYSERR_BADERRNUM;
2681 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2682 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2683 else if (
2684 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2685 * a warning for the test was always true */
2686 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2687 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2688 if (LoadStringW(hWinMM32Instance,
2689 uError, lpText, uSize) > 0) {
2690 ret = MMSYSERR_NOERROR;
2693 return ret;
2696 /**************************************************************************
2697 * waveOutOpen [WINMM.@]
2698 * All the args/structs have the same layout as the win16 equivalents
2700 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2701 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2702 DWORD_PTR dwInstance, DWORD dwFlags)
2704 LRESULT res;
2705 WINMM_OpenInfo info;
2706 WINMM_CBInfo cb_info;
2708 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2709 dwCallback, dwInstance, dwFlags);
2711 if(!WINMM_StartDevicesThread())
2712 return MMSYSERR_NODRIVER;
2714 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2715 return MMSYSERR_INVALPARAM;
2717 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2718 if(res != MMSYSERR_NOERROR)
2719 return res;
2721 info.handle = 0;
2722 info.format = (WAVEFORMATEX*)lpFormat;
2723 info.callback = dwCallback;
2724 info.cb_user = dwInstance;
2725 info.req_device = uDeviceID;
2726 info.flags = dwFlags;
2727 info.reset = TRUE;
2729 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2730 if(res != MMSYSERR_NOERROR)
2731 return res;
2733 if(lphWaveOut)
2734 *lphWaveOut = (HWAVEOUT)info.handle;
2736 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2737 cb_info.callback = dwCallback;
2738 cb_info.user = dwInstance;
2739 cb_info.hwave = info.handle;
2741 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2743 return res;
2746 /**************************************************************************
2747 * waveOutClose [WINMM.@]
2749 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2751 UINT res;
2752 WINMM_Device *device;
2753 WINMM_CBInfo cb_info;
2755 TRACE("(%p)\n", hWaveOut);
2757 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2759 if(!WINMM_ValidateAndLock(device))
2760 return MMSYSERR_INVALHANDLE;
2762 cb_info = device->cb_info;
2764 LeaveCriticalSection(&device->lock);
2766 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2768 if(res == MMSYSERR_NOERROR)
2769 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2771 return res;
2774 /**************************************************************************
2775 * waveOutPrepareHeader [WINMM.@]
2777 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2778 WAVEHDR* lpWaveOutHdr, UINT uSize)
2780 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2782 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2783 return MMSYSERR_INVALPARAM;
2785 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2786 return WAVERR_STILLPLAYING;
2788 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2791 /**************************************************************************
2792 * waveOutUnprepareHeader [WINMM.@]
2794 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2795 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2797 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2799 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2800 return MMSYSERR_INVALPARAM;
2802 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2803 return MMSYSERR_NOERROR;
2805 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2806 return WAVERR_STILLPLAYING;
2808 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2811 /**************************************************************************
2812 * waveOutWrite [WINMM.@]
2814 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2816 WINMM_Device *device;
2817 MMRESULT mr;
2819 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2821 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2823 if(!WINMM_ValidateAndLock(device))
2824 return MMSYSERR_INVALHANDLE;
2826 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2827 LeaveCriticalSection(&device->lock);
2828 return WAVERR_UNPREPARED;
2831 if(header->dwFlags & WHDR_INQUEUE){
2832 LeaveCriticalSection(&device->lock);
2833 return WAVERR_STILLPLAYING;
2836 if(device->acm_handle){
2837 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2839 ash->cbSrcLength = header->dwBufferLength;
2840 mr = acmStreamConvert(device->acm_handle, ash, 0);
2841 if(mr != MMSYSERR_NOERROR){
2842 LeaveCriticalSection(&device->lock);
2843 return mr;
2847 if(device->first){
2848 device->last->lpNext = header;
2849 device->last = header;
2850 if(!device->playing)
2851 device->playing = header;
2852 }else{
2853 device->playing = device->first = device->last = header;
2854 if(header->dwFlags & WHDR_BEGINLOOP){
2855 device->loop_counter = header->dwLoops;
2856 device->loop_start = header;
2860 header->lpNext = NULL;
2861 header->dwFlags &= ~WHDR_DONE;
2862 header->dwFlags |= WHDR_INQUEUE;
2864 mr = WINMM_BeginPlaying(device);
2866 LeaveCriticalSection(&device->lock);
2868 return mr;
2871 /**************************************************************************
2872 * waveOutBreakLoop [WINMM.@]
2874 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2876 WINMM_Device *device;
2878 TRACE("(%p)\n", hWaveOut);
2880 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2882 if(!WINMM_ValidateAndLock(device))
2883 return MMSYSERR_INVALHANDLE;
2885 device->loop_counter = 0;
2887 LeaveCriticalSection(&device->lock);
2889 return MMSYSERR_NOERROR;
2892 /**************************************************************************
2893 * waveOutPause [WINMM.@]
2895 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2897 TRACE("(%p)\n", hWaveOut);
2899 return WINMM_Pause((HWAVE)hWaveOut);
2902 /**************************************************************************
2903 * waveOutReset [WINMM.@]
2905 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2907 TRACE("(%p)\n", hWaveOut);
2909 return WINMM_Reset((HWAVE)hWaveOut);
2912 /**************************************************************************
2913 * waveOutRestart [WINMM.@]
2915 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2917 WINMM_Device *device;
2918 MMRESULT mr;
2920 TRACE("(%p)\n", hWaveOut);
2922 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2924 if(!WINMM_ValidateAndLock(device))
2925 return MMSYSERR_INVALHANDLE;
2927 device->stopped = TRUE;
2929 mr = WINMM_BeginPlaying(device);
2931 LeaveCriticalSection(&device->lock);
2933 return mr;
2936 /**************************************************************************
2937 * waveOutGetPosition [WINMM.@]
2939 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2940 UINT uSize)
2942 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2944 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
2945 return MMSYSERR_INVALPARAM;
2947 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2950 /**************************************************************************
2951 * waveOutGetPitch [WINMM.@]
2953 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2955 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2956 return MMSYSERR_NOTSUPPORTED;
2959 /**************************************************************************
2960 * waveOutSetPitch [WINMM.@]
2962 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
2964 TRACE("(%p, %08x)\n", hWaveOut, dw);
2966 return MMSYSERR_NOTSUPPORTED;
2969 /**************************************************************************
2970 * waveOutGetPlaybackRate [WINMM.@]
2972 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
2974 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2976 return MMSYSERR_NOTSUPPORTED;
2979 /**************************************************************************
2980 * waveOutSetPlaybackRate [WINMM.@]
2982 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
2984 TRACE("(%p, %08x)\n", hWaveOut, dw);
2986 return MMSYSERR_NOTSUPPORTED;
2989 /**************************************************************************
2990 * waveOutGetVolume [WINMM.@]
2992 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
2994 WINMM_Device *device;
2995 UINT32 channels;
2996 float *vols;
2997 HRESULT hr;
2999 TRACE("(%p, %p)\n", hWaveOut, out);
3001 if(!out)
3002 return MMSYSERR_INVALPARAM;
3004 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3006 if(!WINMM_ValidateAndLock(device))
3007 return MMSYSERR_INVALHANDLE;
3009 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3010 if(FAILED(hr)){
3011 LeaveCriticalSection(&device->lock);
3012 WARN("GetChannelCount failed: %08x\n", hr);
3013 return MMSYSERR_ERROR;
3016 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3017 if(!vols){
3018 LeaveCriticalSection(&device->lock);
3019 return MMSYSERR_NOMEM;
3022 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3023 if(FAILED(hr)){
3024 LeaveCriticalSection(&device->lock);
3025 HeapFree(GetProcessHeap(), 0, vols);
3026 WARN("GetAllVolumes failed: %08x\n", hr);
3027 return MMSYSERR_ERROR;
3030 LeaveCriticalSection(&device->lock);
3032 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3033 if(channels > 1)
3034 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3036 HeapFree(GetProcessHeap(), 0, vols);
3038 return MMSYSERR_NOERROR;
3041 /**************************************************************************
3042 * waveOutSetVolume [WINMM.@]
3044 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3046 WINMM_Device *device;
3047 UINT32 channels;
3048 float *vols;
3049 HRESULT hr;
3051 TRACE("(%p, %08x)\n", hWaveOut, in);
3053 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3055 if(!WINMM_ValidateAndLock(device))
3056 return MMSYSERR_INVALHANDLE;
3058 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3059 if(FAILED(hr)){
3060 LeaveCriticalSection(&device->lock);
3061 WARN("GetChannelCount failed: %08x\n", hr);
3062 return MMSYSERR_ERROR;
3065 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3066 if(!vols){
3067 LeaveCriticalSection(&device->lock);
3068 return MMSYSERR_NOMEM;
3071 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3072 if(FAILED(hr)){
3073 LeaveCriticalSection(&device->lock);
3074 HeapFree(GetProcessHeap(), 0, vols);
3075 WARN("GetAllVolumes failed: %08x\n", hr);
3076 return MMSYSERR_ERROR;
3079 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
3080 if(channels > 1)
3081 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
3083 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3084 if(FAILED(hr)){
3085 LeaveCriticalSection(&device->lock);
3086 HeapFree(GetProcessHeap(), 0, vols);
3087 WARN("SetAllVolumes failed: %08x\n", hr);
3088 return MMSYSERR_ERROR;
3091 LeaveCriticalSection(&device->lock);
3093 HeapFree(GetProcessHeap(), 0, vols);
3095 return MMSYSERR_NOERROR;
3098 /**************************************************************************
3099 * waveOutGetID [WINMM.@]
3101 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3103 WINMM_Device *device;
3104 UINT dev, junk;
3105 BOOL is_out;
3107 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3109 if(!lpuDeviceID)
3110 return MMSYSERR_INVALPARAM;
3112 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3113 if(!WINMM_ValidateAndLock(device))
3114 return MMSYSERR_INVALHANDLE;
3116 LeaveCriticalSection(&device->lock);
3118 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3120 return MMSYSERR_NOERROR;
3123 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3125 UINT count;
3126 WINMM_MMDevice **devices;
3128 TRACE("(%u, %p, %d)\n", device, len, is_out);
3130 if(is_out){
3131 count = g_outmmdevices_count;
3132 devices = g_out_map;
3133 }else{
3134 count = g_inmmdevices_count;
3135 devices = g_in_map;
3138 if(device >= count)
3139 return MMSYSERR_INVALHANDLE;
3141 EnterCriticalSection(&g_devthread_lock);
3142 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3143 LeaveCriticalSection(&g_devthread_lock);
3145 return MMSYSERR_NOERROR;
3148 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3149 BOOL is_out)
3151 UINT count, id_len;
3152 WINMM_MMDevice **devices;
3154 TRACE("(%u, %p, %d)\n", device, str, is_out);
3156 if(is_out){
3157 count = g_outmmdevices_count;
3158 devices = g_out_map;
3159 }else{
3160 count = g_inmmdevices_count;
3161 devices = g_in_map;
3164 if(device >= count)
3165 return MMSYSERR_INVALHANDLE;
3167 EnterCriticalSection(&g_devthread_lock);
3168 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3169 if(len < id_len){
3170 LeaveCriticalSection(&g_devthread_lock);
3171 return MMSYSERR_ERROR;
3174 memcpy(str, devices[device]->dev_id, id_len);
3175 LeaveCriticalSection(&g_devthread_lock);
3177 return MMSYSERR_NOERROR;
3180 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3182 WINMM_QueryInterfaceInfo info;
3184 if(!WINMM_StartDevicesThread())
3185 return MMSYSERR_NODRIVER;
3187 info.is_out = is_out;
3188 info.index = index;
3189 info.str = out;
3190 info.len_bytes = out_len;
3192 return SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3195 /**************************************************************************
3196 * waveOutMessage [WINMM.@]
3198 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3199 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3201 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3203 switch(uMessage){
3204 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3205 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3206 (DWORD_PTR*)dwParam1, TRUE);
3207 case DRV_QUERYFUNCTIONINSTANCEID:
3208 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3209 case DRV_QUERYDEVICEINTERFACESIZE:
3210 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3211 NULL, (ULONG*)dwParam1);
3212 case DRV_QUERYDEVICEINTERFACE:
3214 ULONG size = dwParam2;
3215 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3216 (WCHAR*)dwParam1, &size);
3218 case DRV_QUERYMAPPABLE:
3219 return MMSYSERR_NOERROR;
3220 case DRVM_MAPPER_PREFERRED_GET:
3221 if(dwParam1) {
3222 if(g_outmmdevices_count > 0)
3223 /* Device 0 is always the default device */
3224 *(DWORD *)dwParam1 = 0;
3225 else
3226 *(DWORD *)dwParam1 = -1;
3229 if(dwParam2)
3230 /* Status flags */
3231 *(DWORD *)dwParam2 = 0;
3233 return MMSYSERR_NOERROR;
3236 TRACE("Message not supported: %u\n", uMessage);
3238 return MMSYSERR_NOTSUPPORTED;
3241 /**************************************************************************
3242 * waveInGetNumDevs [WINMM.@]
3244 UINT WINAPI waveInGetNumDevs(void)
3246 HRESULT hr = WINMM_InitMMDevices();
3247 if(FAILED(hr))
3248 return 0;
3250 TRACE("count: %u\n", g_inmmdevices_count);
3252 return g_inmmdevices_count;
3255 /**************************************************************************
3256 * waveInGetDevCapsW [WINMM.@]
3258 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3260 WAVEINCAPSW mapper_caps, *caps;
3261 HRESULT hr;
3263 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3265 hr = WINMM_InitMMDevices();
3266 if(FAILED(hr))
3267 return MMSYSERR_NODRIVER;
3269 if(!lpCaps)
3270 return MMSYSERR_INVALPARAM;
3272 if(WINMM_IsMapper(uDeviceID)){
3273 /* FIXME: Should be localized */
3274 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
3275 'n','d',' ','M','a','p','p','e','r',0};
3277 mapper_caps.wMid = 0xFF;
3278 mapper_caps.wPid = 0xFF;
3279 mapper_caps.vDriverVersion = 0x00010001;
3280 mapper_caps.dwFormats = 0xFFFFFFFF;
3281 mapper_caps.wReserved1 = 0;
3282 mapper_caps.wChannels = 2;
3283 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
3285 caps = &mapper_caps;
3286 }else{
3287 if(uDeviceID >= g_inmmdevices_count)
3288 return MMSYSERR_BADDEVICEID;
3290 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3293 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3295 return MMSYSERR_NOERROR;
3298 /**************************************************************************
3299 * waveInGetDevCapsA [WINMM.@]
3301 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3303 UINT ret;
3304 WAVEINCAPSW wicW;
3306 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3308 if(!lpCaps)
3309 return MMSYSERR_INVALPARAM;
3311 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3313 if (ret == MMSYSERR_NOERROR) {
3314 WAVEINCAPSA wicA;
3315 wicA.wMid = wicW.wMid;
3316 wicA.wPid = wicW.wPid;
3317 wicA.vDriverVersion = wicW.vDriverVersion;
3318 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3319 sizeof(wicA.szPname), NULL, NULL );
3320 wicA.dwFormats = wicW.dwFormats;
3321 wicA.wChannels = wicW.wChannels;
3322 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3324 return ret;
3327 /**************************************************************************
3328 * waveInOpen [WINMM.@]
3330 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3331 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3332 DWORD_PTR dwInstance, DWORD dwFlags)
3334 LRESULT res;
3335 WINMM_OpenInfo info;
3336 WINMM_CBInfo cb_info;
3338 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3339 dwCallback, dwInstance, dwFlags);
3341 if(!WINMM_StartDevicesThread())
3342 return MMSYSERR_NODRIVER;
3344 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3345 return MMSYSERR_INVALPARAM;
3347 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3348 if(res != MMSYSERR_NOERROR)
3349 return res;
3351 info.handle = 0;
3352 info.format = (WAVEFORMATEX*)lpFormat;
3353 info.callback = dwCallback;
3354 info.cb_user = dwInstance;
3355 info.req_device = uDeviceID;
3356 info.flags = dwFlags;
3357 info.reset = TRUE;
3359 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3360 if(res != MMSYSERR_NOERROR)
3361 return res;
3363 if(lphWaveIn)
3364 *lphWaveIn = (HWAVEIN)info.handle;
3366 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3367 cb_info.callback = dwCallback;
3368 cb_info.user = dwInstance;
3369 cb_info.hwave = info.handle;
3371 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3373 return res;
3376 /**************************************************************************
3377 * waveInClose [WINMM.@]
3379 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3381 WINMM_Device *device;
3382 WINMM_CBInfo cb_info;
3383 UINT res;
3385 TRACE("(%p)\n", hWaveIn);
3387 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3389 if(!WINMM_ValidateAndLock(device))
3390 return MMSYSERR_INVALHANDLE;
3392 cb_info = device->cb_info;
3394 LeaveCriticalSection(&device->lock);
3396 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3398 if(res == MMSYSERR_NOERROR)
3399 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3401 return res;
3404 /**************************************************************************
3405 * waveInPrepareHeader [WINMM.@]
3407 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3408 UINT uSize)
3410 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3412 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3413 return MMSYSERR_INVALPARAM;
3415 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3416 return WAVERR_STILLPLAYING;
3418 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3421 /**************************************************************************
3422 * waveInUnprepareHeader [WINMM.@]
3424 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3425 UINT uSize)
3427 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3429 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3430 return MMSYSERR_INVALPARAM;
3432 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3433 return MMSYSERR_NOERROR;
3435 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3436 return WAVERR_STILLPLAYING;
3438 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3441 /**************************************************************************
3442 * waveInAddBuffer [WINMM.@]
3444 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3446 WINMM_Device *device;
3448 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3450 if(!header || uSize < sizeof(WAVEHDR))
3451 return MMSYSERR_INVALPARAM;
3453 if(!(header->dwFlags & WHDR_PREPARED))
3454 return WAVERR_UNPREPARED;
3456 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3458 if(!WINMM_ValidateAndLock(device))
3459 return MMSYSERR_INVALHANDLE;
3461 if(!device->first)
3462 device->first = device->last = header;
3463 else{
3464 device->last->lpNext = header;
3465 device->last = header;
3468 header->dwBytesRecorded = 0;
3469 header->lpNext = NULL;
3470 header->dwFlags &= ~WHDR_DONE;
3471 header->dwFlags |= WHDR_INQUEUE;
3473 LeaveCriticalSection(&device->lock);
3475 return MMSYSERR_NOERROR;
3478 /**************************************************************************
3479 * waveInReset [WINMM.@]
3481 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3483 TRACE("(%p)\n", hWaveIn);
3485 return WINMM_Reset((HWAVE)hWaveIn);
3488 /**************************************************************************
3489 * waveInStart [WINMM.@]
3491 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3493 WINMM_Device *device;
3494 MMRESULT mr;
3496 TRACE("(%p)\n", hWaveIn);
3498 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3500 if(!WINMM_ValidateAndLock(device))
3501 return MMSYSERR_INVALHANDLE;
3503 mr = WINMM_BeginPlaying(device);
3505 LeaveCriticalSection(&device->lock);
3507 return mr;
3510 /**************************************************************************
3511 * waveInStop [WINMM.@]
3513 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3515 WINMM_CBInfo cb_info;
3516 WINMM_Device *device;
3517 WAVEHDR *buf;
3518 HRESULT hr;
3520 TRACE("(%p)\n", hWaveIn);
3522 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3524 if(!WINMM_ValidateAndLock(device))
3525 return MMSYSERR_INVALHANDLE;
3527 hr = WINMM_Pause((HWAVE)hWaveIn);
3528 if(FAILED(hr)){
3529 LeaveCriticalSection(&device->lock);
3530 return MMSYSERR_ERROR;
3532 device->stopped = TRUE;
3534 buf = device->first;
3535 if(buf && buf->dwBytesRecorded > 0){
3536 device->first = buf->lpNext;
3537 }else
3538 buf = NULL;
3540 cb_info = device->cb_info;
3542 LeaveCriticalSection(&device->lock);
3544 if(buf){
3545 buf->dwFlags &= ~WHDR_INQUEUE;
3546 buf->dwFlags |= WHDR_DONE;
3547 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3550 return MMSYSERR_NOERROR;
3553 /**************************************************************************
3554 * waveInGetPosition [WINMM.@]
3556 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3557 UINT uSize)
3559 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3561 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
3562 return MMSYSERR_INVALPARAM;
3564 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3567 /**************************************************************************
3568 * waveInGetID [WINMM.@]
3570 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3572 UINT dev, junk;
3573 BOOL is_out;
3574 WINMM_Device *device;
3576 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3578 if(!lpuDeviceID)
3579 return MMSYSERR_INVALPARAM;
3581 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3582 if(!WINMM_ValidateAndLock(device))
3583 return MMSYSERR_INVALHANDLE;
3585 LeaveCriticalSection(&device->lock);
3587 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3589 return MMSYSERR_NOERROR;
3592 /**************************************************************************
3593 * waveInMessage [WINMM.@]
3595 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3596 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3598 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3600 switch(uMessage){
3601 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3602 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3603 (DWORD_PTR*)dwParam1, FALSE);
3604 case DRV_QUERYFUNCTIONINSTANCEID:
3605 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3606 case DRV_QUERYDEVICEINTERFACESIZE:
3607 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3608 NULL, (ULONG*)dwParam1);
3609 case DRV_QUERYDEVICEINTERFACE:
3611 ULONG size = dwParam2;
3612 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3613 (WCHAR*)dwParam1, &size);
3615 case DRV_QUERYMAPPABLE:
3616 return MMSYSERR_NOERROR;
3617 case DRVM_MAPPER_PREFERRED_GET:
3618 if(dwParam1) {
3619 if(g_inmmdevices_count > 0)
3620 /* Device 0 is always the default device */
3621 *(DWORD *)dwParam1 = 0;
3622 else
3623 *(DWORD *)dwParam1 = -1;
3626 if(dwParam2)
3627 /* Status flags */
3628 *(DWORD *)dwParam2 = 0;
3630 return MMSYSERR_NOERROR;
3633 TRACE("Message not supported: %u\n", uMessage);
3635 return MMSYSERR_NOTSUPPORTED;
3638 UINT WINAPI mixerGetNumDevs(void)
3640 HRESULT hr;
3642 TRACE("\n");
3644 hr = WINMM_InitMMDevices();
3645 if(FAILED(hr))
3646 return 0;
3648 return g_outmmdevices_count + g_inmmdevices_count;
3651 /**************************************************************************
3652 * mixerGetDevCapsA [WINMM.@]
3654 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3656 MIXERCAPSW micW;
3657 UINT ret;
3659 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3661 if(!lpCaps)
3662 return MMSYSERR_INVALPARAM;
3664 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3666 if (ret == MMSYSERR_NOERROR) {
3667 MIXERCAPSA micA;
3668 micA.wMid = micW.wMid;
3669 micA.wPid = micW.wPid;
3670 micA.vDriverVersion = micW.vDriverVersion;
3671 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3672 sizeof(micA.szPname), NULL, NULL );
3673 micA.fdwSupport = micW.fdwSupport;
3674 micA.cDestinations = micW.cDestinations;
3675 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3677 return ret;
3680 /**************************************************************************
3681 * mixerGetDevCapsW [WINMM.@]
3683 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3685 WINMM_MMDevice *mmdevice;
3686 MIXERCAPSW caps;
3687 HRESULT hr;
3689 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3691 hr = WINMM_InitMMDevices();
3692 if(FAILED(hr))
3693 return MMSYSERR_NODRIVER;
3695 if(!lpCaps)
3696 return MMSYSERR_INVALPARAM;
3698 if(!uSize)
3699 return MMSYSERR_NOERROR;
3701 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3702 return MMSYSERR_BADDEVICEID;
3704 if(uDeviceID < g_outmmdevices_count){
3705 mmdevice = read_map(g_out_map, uDeviceID);
3706 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3707 }else{
3708 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3709 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3712 caps.wMid = 0xFF;
3713 caps.wPid = 0xFF;
3714 caps.vDriverVersion = 0x00010001;
3715 caps.fdwSupport = 0;
3716 caps.cDestinations = 1;
3718 memcpy(lpCaps, &caps, uSize);
3720 return MMSYSERR_NOERROR;
3723 /**************************************************************************
3724 * mixerOpen [WINMM.@]
3726 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3727 DWORD_PTR dwInstance, DWORD fdwOpen)
3729 WINMM_MMDevice *mmdevice;
3730 MMRESULT mr;
3731 HRESULT hr;
3733 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3734 dwInstance, fdwOpen);
3736 hr = WINMM_InitMMDevices();
3737 if(FAILED(hr))
3738 return MMSYSERR_NODRIVER;
3740 if(!lphMix)
3741 return MMSYSERR_INVALPARAM;
3743 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3744 if(mr != MMSYSERR_NOERROR)
3745 return mr;
3747 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3748 return MMSYSERR_BADDEVICEID;
3750 if(uDeviceID < g_outmmdevices_count){
3751 mmdevice = read_map(g_out_map, uDeviceID);
3752 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3753 mmdevice->mixer_count);
3754 }else{
3755 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3756 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3757 FALSE, mmdevice->mixer_count);
3760 ++mmdevice->mixer_count;
3762 return MMSYSERR_NOERROR;
3765 /**************************************************************************
3766 * mixerClose [WINMM.@]
3768 UINT WINAPI mixerClose(HMIXER hMix)
3770 TRACE("(%p)\n", hMix);
3772 return MMSYSERR_NOERROR;
3775 /**************************************************************************
3776 * mixerGetID [WINMM.@]
3778 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3780 WINMM_MMDevice *mmdevice;
3781 HRESULT hr;
3783 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3785 hr = WINMM_InitMMDevices();
3786 if(FAILED(hr))
3787 return MMSYSERR_NODRIVER;
3789 if(!lpid)
3790 return MMSYSERR_INVALPARAM;
3792 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3793 if(!mmdevice)
3794 return MMSYSERR_INVALHANDLE;
3796 if(mmdevice->in_caps.szPname[0] != '\0')
3797 *lpid += g_outmmdevices_count;
3799 return MMSYSERR_NOERROR;
3802 /**************************************************************************
3803 * mixerGetControlDetailsW [WINMM.@]
3805 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3806 DWORD fdwDetails)
3808 WINMM_ControlDetails details;
3809 HRESULT hr;
3811 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3813 hr = WINMM_InitMMDevices();
3814 if(FAILED(hr))
3815 return MMSYSERR_NODRIVER;
3817 if(!lpmcdW)
3818 return MMSYSERR_INVALPARAM;
3820 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3822 details.hmix = hmix;
3823 details.details = lpmcdW;
3824 details.flags = fdwDetails;
3826 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3827 (DWORD_PTR)&details, 0);
3830 /**************************************************************************
3831 * mixerGetControlDetailsA [WINMM.@]
3833 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3834 DWORD fdwDetails)
3836 UINT ret = MMSYSERR_NOTSUPPORTED;
3838 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3840 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3841 return MMSYSERR_INVALPARAM;
3843 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3844 case MIXER_GETCONTROLDETAILSF_VALUE:
3845 /* can safely use A structure as it is, no string inside */
3846 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3847 break;
3848 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3850 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3851 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3852 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3853 unsigned int i;
3855 if (lpmcdA->u.cMultipleItems != 0) {
3856 size *= lpmcdA->u.cMultipleItems;
3858 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3859 lpmcdA->paDetails = pDetailsW;
3860 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3861 /* set up lpmcd->paDetails */
3862 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3863 /* copy from lpmcd->paDetails back to paDetailsW; */
3864 if (ret == MMSYSERR_NOERROR) {
3865 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3866 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3867 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3868 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3869 pDetailsA->szName,
3870 sizeof(pDetailsA->szName), NULL, NULL );
3871 pDetailsA++;
3872 pDetailsW++;
3874 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3875 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3877 HeapFree(GetProcessHeap(), 0, pDetailsW);
3878 lpmcdA->paDetails = pDetailsA;
3879 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3881 break;
3882 default:
3883 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3886 return ret;
3889 /**************************************************************************
3890 * mixerGetLineControlsA [WINMM.@]
3892 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3893 DWORD fdwControls)
3895 MIXERLINECONTROLSW mlcW;
3896 DWORD ret;
3897 unsigned int i;
3899 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3901 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3902 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3903 return MMSYSERR_INVALPARAM;
3905 mlcW.cbStruct = sizeof(mlcW);
3906 mlcW.dwLineID = lpmlcA->dwLineID;
3907 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3908 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3910 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3911 the control count is assumed to be 1 - This is relied upon by a game,
3912 "Dynomite Deluze" */
3913 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3914 mlcW.cControls = 1;
3915 } else {
3916 mlcW.cControls = lpmlcA->cControls;
3918 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3919 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3920 mlcW.cControls * mlcW.cbmxctrl);
3922 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3924 if (ret == MMSYSERR_NOERROR) {
3925 lpmlcA->dwLineID = mlcW.dwLineID;
3926 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3927 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3929 for (i = 0; i < mlcW.cControls; i++) {
3930 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3931 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3932 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3933 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3934 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3935 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3936 lpmlcA->pamxctrl[i].szShortName,
3937 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3938 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3939 lpmlcA->pamxctrl[i].szName,
3940 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3941 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3942 * sizeof(mlcW.pamxctrl[i].Bounds) */
3943 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3944 sizeof(mlcW.pamxctrl[i].Bounds));
3945 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3946 * sizeof(mlcW.pamxctrl[i].Metrics) */
3947 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3948 sizeof(mlcW.pamxctrl[i].Metrics));
3952 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
3954 return ret;
3957 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3958 MIXERCONTROLW *ctl, DWORD flags)
3960 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
3961 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
3962 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
3963 ctl->cMultipleItems = 0;
3964 lstrcpyW(ctl->szShortName, volumeW);
3965 lstrcpyW(ctl->szName, volumeW);
3966 ctl->Bounds.s1.dwMinimum = 0;
3967 ctl->Bounds.s1.dwMaximum = 0xFFFF;
3968 ctl->Metrics.cSteps = 192;
3970 return MMSYSERR_NOERROR;
3973 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3974 MIXERCONTROLW *ctl, DWORD flags)
3976 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
3977 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
3978 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
3979 ctl->cMultipleItems = 0;
3980 lstrcpyW(ctl->szShortName, muteW);
3981 lstrcpyW(ctl->szName, muteW);
3982 ctl->Bounds.s1.dwMinimum = 0;
3983 ctl->Bounds.s1.dwMaximum = 1;
3984 ctl->Metrics.cSteps = 0;
3986 return MMSYSERR_NOERROR;
3989 /**************************************************************************
3990 * mixerGetLineControlsW [WINMM.@]
3992 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
3993 DWORD fdwControls)
3995 WINMM_MMDevice *mmdevice;
3996 HRESULT hr;
3998 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4000 hr = WINMM_InitMMDevices();
4001 if(FAILED(hr))
4002 return MMSYSERR_NODRIVER;
4004 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4005 MIXER_GETLINECONTROLSF_ONEBYID |
4006 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4007 MIXER_OBJECTF_HMIXER |
4008 MIXER_OBJECTF_MIXER)){
4009 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4010 return MMSYSERR_INVALFLAG;
4013 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4014 return MMSYSERR_INVALPARAM;
4016 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4017 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4018 TRACE("cControls: %u\n", lpmlcW->cControls);
4020 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4021 if(!mmdevice)
4022 return MMSYSERR_INVALHANDLE;
4024 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4025 case MIXER_GETLINECONTROLSF_ALL:
4026 if(lpmlcW->cControls != 2)
4027 return MMSYSERR_INVALPARAM;
4028 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4029 return MMSYSERR_INVALPARAM;
4030 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4031 return MIXERR_INVALLINE;
4032 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4033 &lpmlcW->pamxctrl[0], fdwControls);
4034 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4035 &lpmlcW->pamxctrl[1], fdwControls);
4036 return MMSYSERR_NOERROR;
4037 case MIXER_GETLINECONTROLSF_ONEBYID:
4038 if(lpmlcW->cControls != 1)
4039 return MMSYSERR_INVALPARAM;
4040 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4041 return MMSYSERR_INVALPARAM;
4042 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4043 return MIXERR_INVALLINE;
4044 if(lpmlcW->u.dwControlID == 0)
4045 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4046 lpmlcW->pamxctrl, fdwControls);
4047 if(lpmlcW->u.dwControlID == 1)
4048 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4049 lpmlcW->pamxctrl, fdwControls);
4050 return MMSYSERR_NOTSUPPORTED;
4051 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4052 if(lpmlcW->cControls != 1)
4053 return MMSYSERR_INVALPARAM;
4054 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4055 return MMSYSERR_INVALPARAM;
4056 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4057 return MIXERR_INVALLINE;
4058 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4059 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4060 lpmlcW->pamxctrl, fdwControls);
4061 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4062 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4063 lpmlcW->pamxctrl, fdwControls);
4064 return MMSYSERR_NOTSUPPORTED;
4067 return MMSYSERR_NOTSUPPORTED;
4070 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4071 MIXERLINEW *info, DWORD flags)
4073 BOOL is_out = TRUE;
4074 if(mmdevice->in_caps.szPname[0] != '\0')
4075 is_out = FALSE;
4077 if(info->dwSource != 0)
4078 return MIXERR_INVALLINE;
4080 info->dwDestination = 0;
4081 info->dwLineID = 0;
4082 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4083 info->cConnections = 0;
4084 info->cControls = 2;
4085 /* volume & mute always affect all channels, so claim 1 channel */
4086 info->cChannels = 1;
4087 info->Target.dwDeviceID = mmdev_index;
4088 info->Target.wMid = ~0;
4089 info->Target.wPid = ~0;
4090 info->Target.vDriverVersion = 0;
4092 lstrcpyW(info->szShortName, volumeW);
4093 lstrcpyW(info->szName, mastervolumeW);
4095 if(is_out){
4096 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4097 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4098 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4099 sizeof(info->Target.szPname));
4100 }else{
4101 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4102 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4103 info->Target.szPname[0] = '\0';
4106 return MMSYSERR_NOERROR;
4109 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4110 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4112 BOOL is_out = TRUE;
4113 if(mmdevice->in_caps.szPname[0] != '\0')
4114 is_out = FALSE;
4116 if(info->dwDestination != 0)
4117 return MIXERR_INVALLINE;
4119 info->dwSource = 0xFFFFFFFF;
4120 info->dwLineID = 0xFFFF0000;
4121 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4122 info->cConnections = 1;
4123 info->cControls = 2;
4125 lstrcpyW(info->szShortName, volumeW);
4126 lstrcpyW(info->szName, mastervolumeW);
4128 info->Target.dwDeviceID = mmdev_index;
4129 info->Target.wMid = ~0;
4130 info->Target.wPid = ~0;
4131 info->Target.vDriverVersion = 0;
4133 if(is_out){
4134 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4135 info->cChannels = mmdevice->out_caps.wChannels;
4136 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4137 info->Target.szPname[0] = '\0';
4138 }else{
4139 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4140 info->cChannels = mmdevice->in_caps.wChannels;
4141 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4142 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4143 sizeof(info->Target.szPname));
4146 return MMSYSERR_NOERROR;
4149 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4150 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4152 BOOL is_out = TRUE;
4153 if(mmdevice->in_caps.szPname[0] != '\0')
4154 is_out = FALSE;
4156 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4157 if(is_out)
4158 return MIXERR_INVALLINE;
4159 info->dwDestination = 0;
4160 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4163 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4164 if(!is_out)
4165 return MIXERR_INVALLINE;
4166 info->dwDestination = 0;
4167 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4170 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4171 if(is_out)
4172 return MIXERR_INVALLINE;
4173 info->dwSource = 0;
4174 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4177 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4178 if(!is_out)
4179 return MIXERR_INVALLINE;
4180 info->dwSource = 0;
4181 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4184 TRACE("Returning INVALLINE on this component type: %u\n",
4185 info->dwComponentType);
4187 return MIXERR_INVALLINE;
4190 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4191 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4193 if(info->dwLineID == 0xFFFF0000){
4194 info->dwDestination = 0;
4195 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4198 if(info->dwLineID == 0){
4199 info->dwSource = 0;
4200 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4203 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4204 return MIXERR_INVALLINE;
4207 /**************************************************************************
4208 * mixerGetLineInfoW [WINMM.@]
4210 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4212 UINT mmdev_index;
4213 WINMM_MMDevice *mmdevice;
4214 HRESULT hr;
4216 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4218 hr = WINMM_InitMMDevices();
4219 if(FAILED(hr))
4220 return MMSYSERR_NODRIVER;
4222 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4223 return MMSYSERR_INVALPARAM;
4225 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4226 TRACE("dwSource: %u\n", lpmliW->dwSource);
4227 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4228 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4229 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4231 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4232 MIXER_GETLINEINFOF_DESTINATION |
4233 MIXER_GETLINEINFOF_LINEID |
4234 MIXER_GETLINEINFOF_SOURCE |
4235 MIXER_GETLINEINFOF_TARGETTYPE |
4236 MIXER_OBJECTF_HMIXER |
4237 MIXER_OBJECTF_MIXER)){
4238 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4239 return MMSYSERR_INVALFLAG;
4242 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4243 if(!mmdevice)
4244 return MMSYSERR_INVALHANDLE;
4246 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4247 case MIXER_GETLINEINFOF_DESTINATION:
4248 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4249 fdwInfo);
4250 case MIXER_GETLINEINFOF_SOURCE:
4251 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4252 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4253 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4254 fdwInfo);
4255 case MIXER_GETLINEINFOF_LINEID:
4256 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4257 case MIXER_GETLINEINFOF_TARGETTYPE:
4258 FIXME("TARGETTYPE flag not implemented!\n");
4259 return MIXERR_INVALLINE;
4262 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
4263 return MMSYSERR_INVALFLAG;
4266 /**************************************************************************
4267 * mixerGetLineInfoA [WINMM.@]
4269 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4270 DWORD fdwInfo)
4272 MIXERLINEW mliW;
4273 UINT ret;
4275 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4277 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4278 return MMSYSERR_INVALPARAM;
4280 mliW.cbStruct = sizeof(mliW);
4281 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4282 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4283 mliW.dwComponentType = lpmliA->dwComponentType;
4284 break;
4285 case MIXER_GETLINEINFOF_DESTINATION:
4286 mliW.dwDestination = lpmliA->dwDestination;
4287 break;
4288 case MIXER_GETLINEINFOF_LINEID:
4289 mliW.dwLineID = lpmliA->dwLineID;
4290 break;
4291 case MIXER_GETLINEINFOF_SOURCE:
4292 mliW.dwDestination = lpmliA->dwDestination;
4293 mliW.dwSource = lpmliA->dwSource;
4294 break;
4295 case MIXER_GETLINEINFOF_TARGETTYPE:
4296 mliW.Target.dwType = lpmliA->Target.dwType;
4297 mliW.Target.wMid = lpmliA->Target.wMid;
4298 mliW.Target.wPid = lpmliA->Target.wPid;
4299 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4300 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
4301 break;
4302 default:
4303 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4304 return MMSYSERR_INVALFLAG;
4307 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4309 if(ret == MMSYSERR_NOERROR)
4311 lpmliA->dwDestination = mliW.dwDestination;
4312 lpmliA->dwSource = mliW.dwSource;
4313 lpmliA->dwLineID = mliW.dwLineID;
4314 lpmliA->fdwLine = mliW.fdwLine;
4315 lpmliA->dwUser = mliW.dwUser;
4316 lpmliA->dwComponentType = mliW.dwComponentType;
4317 lpmliA->cChannels = mliW.cChannels;
4318 lpmliA->cConnections = mliW.cConnections;
4319 lpmliA->cControls = mliW.cControls;
4320 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4321 sizeof(lpmliA->szShortName), NULL, NULL);
4322 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4323 sizeof(lpmliA->szName), NULL, NULL );
4324 lpmliA->Target.dwType = mliW.Target.dwType;
4325 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4326 lpmliA->Target.wMid = mliW.Target.wMid;
4327 lpmliA->Target.wPid = mliW.Target.wPid;
4328 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4329 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4330 sizeof(lpmliA->Target.szPname), NULL, NULL );
4332 return ret;
4335 /**************************************************************************
4336 * mixerSetControlDetails [WINMM.@]
4338 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4339 DWORD fdwDetails)
4341 WINMM_ControlDetails details;
4343 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4345 if(!WINMM_StartDevicesThread())
4346 return MMSYSERR_NODRIVER;
4348 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4349 MIXER_SETCONTROLDETAILSF_CUSTOM)
4350 return MMSYSERR_NOTSUPPORTED;
4352 if(!lpmcd)
4353 return MMSYSERR_INVALPARAM;
4355 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4357 details.hmix = hmix;
4358 details.details = lpmcd;
4359 details.flags = fdwDetails;
4361 return SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4362 (DWORD_PTR)&details, 0);
4365 /**************************************************************************
4366 * mixerMessage [WINMM.@]
4368 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4370 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4372 return MMSYSERR_NOTSUPPORTED;