ntdll: Add stub for RtlSetHeapInformation.
[wine.git] / dlls / winmm / waveform.c
blobf381c16180ebd79e06a7b4e42c9caba11d580942
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 static CRITICAL_SECTION g_devthread_lock;
154 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug =
156 0, 0, &g_devthread_lock,
157 { &g_devthread_lock_debug.ProcessLocksList, &g_devthread_lock_debug.ProcessLocksList },
158 0, 0, { (DWORD_PTR)(__FILE__ ": g_devthread_lock") }
160 static CRITICAL_SECTION g_devthread_lock = { &g_devthread_lock_debug, -1, 0, 0, 0, 0 };
161 static LONG g_devthread_token;
162 static HANDLE g_devices_thread;
163 static HWND g_devices_hwnd;
164 static HMODULE g_devthread_module;
166 static UINT g_devhandle_count;
167 static HANDLE *g_device_handles;
168 static WINMM_Device **g_handle_devices;
170 typedef struct _WINMM_OpenInfo {
171 HWAVE handle;
172 UINT req_device;
173 WAVEFORMATEX *format;
174 DWORD_PTR callback;
175 DWORD_PTR cb_user;
176 DWORD flags;
177 BOOL reset;
178 } WINMM_OpenInfo;
180 typedef struct _WINMM_ControlDetails {
181 HMIXEROBJ hmix;
182 MIXERCONTROLDETAILS *details;
183 DWORD flags;
184 } WINMM_ControlDetails;
186 typedef struct _WINMM_QueryInterfaceInfo {
187 BOOL is_out;
188 UINT index;
189 WCHAR *str;
190 UINT *len_bytes;
191 } WINMM_QueryInterfaceInfo;
193 static LRESULT WOD_Open(WINMM_OpenInfo *info);
194 static LRESULT WOD_Close(HWAVEOUT hwave);
195 static LRESULT WID_Open(WINMM_OpenInfo *info);
196 static LRESULT WID_Close(HWAVEIN hwave);
197 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device);
199 void WINMM_DeleteWaveform(void)
201 UINT i, j;
203 if(g_devices_thread)
204 CloseHandle(g_devices_thread);
206 for(i = 0; i < g_outmmdevices_count; ++i){
207 WINMM_MMDevice *mmdevice = &g_out_mmdevices[i];
209 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
210 WINMM_Device *device = mmdevice->devices[j];
211 if(device->handle)
212 CloseHandle(device->handle);
213 DeleteCriticalSection(&device->lock);
216 if(mmdevice->volume)
217 ISimpleAudioVolume_Release(mmdevice->volume);
218 CoTaskMemFree(mmdevice->dev_id);
219 DeleteCriticalSection(&mmdevice->lock);
222 for(i = 0; i < g_inmmdevices_count; ++i){
223 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
225 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
226 WINMM_Device *device = mmdevice->devices[j];
227 if(device->handle)
228 CloseHandle(device->handle);
229 DeleteCriticalSection(&device->lock);
232 if(mmdevice->volume)
233 ISimpleAudioVolume_Release(mmdevice->volume);
234 CoTaskMemFree(mmdevice->dev_id);
235 DeleteCriticalSection(&mmdevice->lock);
238 HeapFree(GetProcessHeap(), 0, g_out_mmdevices);
239 HeapFree(GetProcessHeap(), 0, g_in_mmdevices);
241 HeapFree(GetProcessHeap(), 0, g_device_handles);
242 HeapFree(GetProcessHeap(), 0, g_handle_devices);
244 DeleteCriticalSection(&g_devthread_lock);
247 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
249 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
250 (mmdevice << 8) | device);
253 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
254 BOOL *is_out, UINT *device_index, UINT *junk)
256 ULONG32 l = HandleToULong(hwave);
257 *device_index = l & 0xFF;
258 *mmdevice_index = (l >> 8) & 0x3F;
259 *is_out = (l >> 14) & 0x1;
260 *junk = l >> 15;
263 static void WINMM_InitDevice(WINMM_Device *device)
265 InitializeCriticalSection(&device->lock);
266 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
269 static inline WINMM_MMDevice *read_map(WINMM_MMDevice **map, UINT index)
271 WINMM_MMDevice *ret;
272 EnterCriticalSection(&g_devthread_lock);
273 ret = map[index];
274 LeaveCriticalSection(&g_devthread_lock);
275 return ret;
278 /* finds the first unused Device, marks it as "open", and returns
279 * a pointer to the device
281 * IMPORTANT: it is the caller's responsibility to release the device's lock
282 * on success
284 static WINMM_Device *WINMM_FindUnusedDevice(WINMM_Device **devices,
285 WINMM_MMDevice *parent, UINT internal_index, BOOL is_out)
287 UINT i;
289 for(i = 0; i < MAX_DEVICES; ++i){
290 WINMM_Device *device = devices[i];
292 if(!device){
293 device = devices[i] = HeapAlloc(GetProcessHeap(),
294 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
295 if(!device)
296 return NULL;
298 WINMM_InitDevice(device);
299 EnterCriticalSection(&device->lock);
300 }else
301 EnterCriticalSection(&device->lock);
303 if(!device->open){
304 device->handle = WINMM_MakeHWAVE(internal_index, is_out, i);
305 device->parent = parent;
306 device->open = TRUE;
308 return device;
311 LeaveCriticalSection(&device->lock);
314 TRACE("All devices in use: mmdevice: %u\n", internal_index);
316 return NULL;
319 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
321 if(!device)
322 return FALSE;
324 EnterCriticalSection(&device->lock);
326 if(!device->open){
327 LeaveCriticalSection(&device->lock);
328 return FALSE;
331 return TRUE;
334 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
336 WINMM_MMDevice *mmdevice;
337 WINMM_Device *device;
338 UINT mmdevice_index, device_index, junk;
339 BOOL is_out;
341 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
343 if(junk != 0x1)
344 return NULL;
346 if(mmdevice_index == MAPPER_INDEX){
347 EnterCriticalSection(&g_devthread_lock);
348 if(is_out)
349 device = g_out_mapper_devices[device_index];
350 else
351 device = g_in_mapper_devices[device_index];
352 LeaveCriticalSection(&g_devthread_lock);
353 return device;
356 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
357 return NULL;
359 if(is_out)
360 mmdevice = &g_out_mmdevices[mmdevice_index];
361 else
362 mmdevice = &g_in_mmdevices[mmdevice_index];
364 EnterCriticalSection(&mmdevice->lock);
366 device = mmdevice->devices[device_index];
368 LeaveCriticalSection(&mmdevice->lock);
370 return device;
373 /* Note: NotifyClient should never be called while holding the device lock
374 * since the client may call wave* functions from within the callback. */
375 static inline void WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
376 DWORD_PTR param2)
378 DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
379 msg, info->user, param1, param2);
382 static MMRESULT hr2mmr(HRESULT hr)
384 switch(hr){
385 case S_OK:
386 case AUDCLNT_E_NOT_STOPPED:
387 return MMSYSERR_NOERROR;
388 case AUDCLNT_E_UNSUPPORTED_FORMAT:
389 return WAVERR_BADFORMAT;
390 case AUDCLNT_E_DEVICE_IN_USE:
391 return MMSYSERR_ALLOCATED;
392 case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
393 return MMSYSERR_NOTENABLED;
394 case E_OUTOFMEMORY:
395 return MMSYSERR_NOMEM;
396 case E_POINTER:
397 case E_INVALIDARG:
398 return MMSYSERR_INVALPARAM;
399 case AUDCLNT_E_DEVICE_INVALIDATED: /* DSERR_BUFFERLOST */
400 default:
401 return FAILED(hr) ? MMSYSERR_ERROR : MMSYSERR_NOERROR;
405 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
406 UINT outlen)
408 IPropertyStore *ps;
409 PROPVARIANT var;
410 HRESULT hr;
412 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
413 if(FAILED(hr))
414 return hr;
416 PropVariantInit(&var);
418 hr = IPropertyStore_GetValue(ps,
419 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
420 if(FAILED(hr)){
421 IPropertyStore_Release(ps);
422 return hr;
425 lstrcpynW(out, var.u.pwszVal, outlen);
427 PropVariantClear(&var);
429 IPropertyStore_Release(ps);
431 return S_OK;
434 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
435 WORD channels)
437 WAVEFORMATEX fmt, *junk;
438 HRESULT hr;
440 fmt.wFormatTag = WAVE_FORMAT_PCM;
441 fmt.nChannels = channels;
442 fmt.nSamplesPerSec = rate;
443 fmt.wBitsPerSample = depth;
444 fmt.nBlockAlign = (channels * depth) / 8;
445 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
446 fmt.cbSize = 0;
448 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
449 &fmt, &junk);
450 if(SUCCEEDED(hr))
451 CoTaskMemFree(junk);
453 return hr;
456 static struct _TestFormat {
457 DWORD flag;
458 DWORD rate;
459 DWORD depth;
460 WORD channels;
461 } formats_to_test[] = {
462 { WAVE_FORMAT_1M08, 11025, 8, 1 },
463 { WAVE_FORMAT_1M16, 11025, 16, 1 },
464 { WAVE_FORMAT_1S08, 11025, 8, 2 },
465 { WAVE_FORMAT_1S16, 11025, 16, 2 },
466 { WAVE_FORMAT_2M08, 22050, 8, 1 },
467 { WAVE_FORMAT_2M16, 22050, 16, 1 },
468 { WAVE_FORMAT_2S08, 22050, 8, 2 },
469 { WAVE_FORMAT_2S16, 22050, 16, 2 },
470 { WAVE_FORMAT_4M08, 44100, 8, 1 },
471 { WAVE_FORMAT_4M16, 44100, 16, 1 },
472 { WAVE_FORMAT_4S08, 44100, 8, 2 },
473 { WAVE_FORMAT_4S16, 44100, 16, 2 },
474 { WAVE_FORMAT_48M08, 48000, 8, 1 },
475 { WAVE_FORMAT_48M16, 48000, 16, 1 },
476 { WAVE_FORMAT_48S08, 48000, 8, 2 },
477 { WAVE_FORMAT_48S16, 48000, 16, 2 },
478 { WAVE_FORMAT_96M08, 96000, 8, 1 },
479 { WAVE_FORMAT_96M16, 96000, 16, 1 },
480 { WAVE_FORMAT_96S08, 96000, 8, 2 },
481 { WAVE_FORMAT_96S16, 96000, 16, 2 },
485 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
487 DWORD flags = 0;
488 HRESULT hr;
489 struct _TestFormat *fmt;
490 IAudioClient *client;
492 hr = IMMDevice_Activate(device, &IID_IAudioClient,
493 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
494 if(FAILED(hr))
495 return 0;
497 for(fmt = formats_to_test; fmt->flag; ++fmt){
498 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
499 if(hr == S_OK)
500 flags |= fmt->flag;
503 IAudioClient_Release(client);
505 return flags;
508 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
509 WINMM_MMDevice *dev, UINT index)
511 HRESULT hr;
513 if(flow == eRender){
514 dev->out_caps.wMid = 0xFF;
515 dev->out_caps.wPid = 0xFF;
516 dev->out_caps.vDriverVersion = 0x00010001;
517 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
518 dev->out_caps.wReserved1 = 0;
519 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
520 WAVECAPS_SAMPLEACCURATE;
521 dev->out_caps.wChannels = 2;
522 dev->out_caps.szPname[0] = '\0';
524 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
525 sizeof(dev->out_caps.szPname) /
526 sizeof(*dev->out_caps.szPname));
527 if(FAILED(hr))
528 return hr;
529 }else{
530 dev->in_caps.wMid = 0xFF;
531 dev->in_caps.wPid = 0xFF;
532 dev->in_caps.vDriverVersion = 0x00010001;
533 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
534 dev->in_caps.wReserved1 = 0;
535 dev->in_caps.wChannels = 2;
536 dev->in_caps.szPname[0] = '\0';
538 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
539 sizeof(dev->in_caps.szPname) /
540 sizeof(*dev->in_caps.szPname));
541 if(FAILED(hr))
542 return hr;
545 hr = IMMDevice_GetId(device, &dev->dev_id);
546 if(FAILED(hr))
547 return hr;
549 CoCreateGuid(&dev->session);
551 dev->index = index;
553 InitializeCriticalSection(&dev->lock);
554 dev->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
556 return S_OK;
559 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices,
560 WINMM_MMDevice ***map, UINT *devcount, EDataFlow flow,
561 IMMDeviceEnumerator *devenum)
563 IMMDeviceCollection *devcoll;
564 HRESULT hr;
566 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
567 DEVICE_STATE_ACTIVE, &devcoll);
568 if(FAILED(hr))
569 return hr;
571 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
572 if(FAILED(hr)){
573 IMMDeviceCollection_Release(devcoll);
574 return hr;
577 if(*devcount > 0){
578 UINT n, count = 1;
579 IMMDevice *def_dev = NULL;
581 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
582 sizeof(WINMM_MMDevice) * (*devcount));
583 if(!*devices){
584 IMMDeviceCollection_Release(devcoll);
585 return E_OUTOFMEMORY;
588 *map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
589 sizeof(WINMM_MMDevice *) * (*devcount));
590 if(!*map){
591 IMMDeviceCollection_Release(devcoll);
592 HeapFree(GetProcessHeap(), 0, *devices);
593 return E_OUTOFMEMORY;
596 /* make sure that device 0 is the default device */
597 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
598 flow, eConsole, &def_dev);
600 for(n = 0; n < *devcount; ++n){
601 IMMDevice *device;
603 hr = IMMDeviceCollection_Item(devcoll, n, &device);
604 if(SUCCEEDED(hr)){
605 WINMM_InitMMDevice(flow, device, &(*devices)[n], n);
607 if(device == def_dev)
608 (*map)[0] = &(*devices)[n];
609 else{
610 (*map)[count] = &(*devices)[n];
611 ++count;
614 IMMDevice_Release(device);
618 IMMDevice_Release(def_dev);
620 *devcount = count;
623 IMMDeviceCollection_Release(devcoll);
625 return S_OK;
628 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
629 const GUID *riid, void **obj)
631 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid));
632 return E_NOINTERFACE;
635 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
637 return 2;
640 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
642 return 1;
645 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
646 const WCHAR *device_id, DWORD new_state)
648 TRACE("Ignoring OnDeviceStateChanged callback\n");
649 return S_OK;
652 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
653 const WCHAR *device_id)
655 TRACE("Ignoring OnDeviceAdded callback\n");
656 return S_OK;
659 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
660 const WCHAR *device_id)
662 TRACE("Ignoring OnDeviceRemoved callback\n");
663 return S_OK;
666 static HRESULT update_mapping(WINMM_MMDevice ***map, UINT count,
667 const WCHAR *default_id)
669 WINMM_MMDevice *prev;
670 UINT i;
672 prev = (*map)[0];
673 for(i = 0; i < count; ++i){
674 WINMM_MMDevice *tmp;
676 if(!lstrcmpW((*map)[i]->dev_id, default_id)){
677 (*map)[0] = (*map)[i];
678 (*map)[i] = prev;
680 return S_OK;
683 tmp = (*map)[i];
684 (*map)[i] = prev;
685 prev = tmp;
688 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
689 (*map)[0] = prev;
691 return S_FALSE;
694 static HRESULT reroute_mapper_device(WINMM_Device *device, BOOL is_out)
696 WINMM_OpenInfo info;
697 BOOL stopped;
698 MMRESULT mr;
699 HRESULT hr;
700 UINT64 clock_freq, clock_pos;
702 TRACE("rerouting device %p\n", device->handle);
704 EnterCriticalSection(&device->lock);
706 if(!device->open || device->acm_handle){
707 /* Windows 7 doesn't re-route ACM devices, so we don't either.
708 * Seems to be because of the data waveXxxPrepareHeader allocates. */
709 LeaveCriticalSection(&device->lock);
710 return S_FALSE;
713 stopped = device->stopped;
715 info.handle = 0;
716 info.req_device = WAVE_MAPPER;
717 info.format = device->orig_fmt;
718 info.callback = device->cb_info.callback;
719 info.cb_user = device->cb_info.user;
720 /* We have to use direct here so that we don't suddenly introduce ACM
721 * into a playing stream that hasn't been Prepared for it */
722 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT_QUERY;
723 info.reset = FALSE;
725 if(is_out)
726 mr = WOD_Open(&info);
727 else
728 mr = WID_Open(&info);
730 if(mr != MMSYSERR_NOERROR){
731 TRACE("New default device doesn't support this stream: %p\n", device->handle);
732 LeaveCriticalSection(&device->lock);
733 return S_FALSE;
736 hr = IAudioClient_Stop(device->client);
737 if(FAILED(hr))
738 WARN("Stop failed: %08x\n", hr);
740 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
741 if(FAILED(hr)){
742 WARN("GetFrequency failed: %08x\n", hr);
743 LeaveCriticalSection(&device->lock);
744 return hr;
747 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
748 if(FAILED(hr)){
749 WARN("GetPosition failed: %08x\n", hr);
750 LeaveCriticalSection(&device->lock);
751 return hr;
754 device->remainder_frames = MulDiv(clock_pos, device->samples_per_sec, clock_freq) - device->last_clock_pos;
756 info.handle = device->handle;
757 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT;
759 if(is_out){
760 WOD_Close((HWAVEOUT)device->handle);
761 device->parent = read_map(g_out_map, 0);
762 mr = WOD_Open(&info);
763 }else{
764 WID_Close((HWAVEIN)device->handle);
765 device->parent = read_map(g_in_map, 0);
766 mr = WID_Open(&info);
769 if(mr != MMSYSERR_NOERROR){
770 ERR("Opening new default device failed! %u\n", mr);
771 LeaveCriticalSection(&device->lock);
772 return E_FAIL;
775 HeapFree(GetProcessHeap(), 0, info.format);
777 if(!stopped)
778 WINMM_BeginPlaying(device);
780 LeaveCriticalSection(&device->lock);
782 return S_OK;
785 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
786 EDataFlow flow, ERole role, const WCHAR *device_id)
788 UINT i;
790 TRACE("%u %u %s\n", flow, role, wine_dbgstr_w(device_id));
792 if(role != eConsole)
793 return S_OK;
795 EnterCriticalSection(&g_devthread_lock);
797 if(flow == eRender)
798 update_mapping(&g_out_map, g_outmmdevices_count, device_id);
799 else
800 update_mapping(&g_in_map, g_inmmdevices_count, device_id);
802 for(i = 0; i < MAX_DEVICES && g_out_mapper_devices[i]; ++i)
803 reroute_mapper_device(g_out_mapper_devices[i], TRUE);
805 for(i = 0; i < MAX_DEVICES && g_in_mapper_devices[i]; ++i)
806 reroute_mapper_device(g_in_mapper_devices[i], FALSE);
808 LeaveCriticalSection(&g_devthread_lock);
810 return S_OK;
813 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
814 const WCHAR *device_id, const PROPERTYKEY key)
816 TRACE("Ignoring OnPropertyValueChanged callback\n");
817 return S_OK;
820 static IMMNotificationClientVtbl g_notif_vtbl = {
821 notif_QueryInterface,
822 notif_AddRef,
823 notif_Release,
824 notif_OnDeviceStateChanged,
825 notif_OnDeviceAdded,
826 notif_OnDeviceRemoved,
827 notif_OnDefaultDeviceChanged,
828 notif_OnPropertyValueChanged
831 static IMMNotificationClient g_notif = { &g_notif_vtbl };
833 static HRESULT WINMM_InitMMDevices(void)
835 HRESULT hr, init_hr;
836 IMMDeviceEnumerator *devenum = NULL;
838 if(g_outmmdevices_count || g_inmmdevices_count)
839 return S_FALSE;
841 init_hr = CoInitialize(NULL);
843 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
844 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
845 if(FAILED(hr))
846 goto exit;
848 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum, &g_notif);
849 if(FAILED(hr))
850 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr);
852 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_out_map, &g_outmmdevices_count,
853 eRender, devenum);
854 if(FAILED(hr)){
855 g_outmmdevices_count = 0;
856 g_inmmdevices_count = 0;
857 goto exit;
860 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_in_map, &g_inmmdevices_count,
861 eCapture, devenum);
862 if(FAILED(hr)){
863 g_inmmdevices_count = 0;
864 goto exit;
867 exit:
868 if(devenum)
869 IMMDeviceEnumerator_Release(devenum);
870 if(SUCCEEDED(init_hr))
871 CoUninitialize();
873 return hr;
876 static inline BOOL WINMM_IsMapper(UINT device)
878 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
881 static MMRESULT WINMM_TryDeviceMapping(WINMM_Device *device, WAVEFORMATEX *fmt,
882 WORD channels, DWORD freq, DWORD bits_per_samp, BOOL is_query, BOOL is_out)
884 WAVEFORMATEX target, *closer_fmt = NULL;
885 HRESULT hr;
886 MMRESULT mr;
888 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
889 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
891 target.wFormatTag = WAVE_FORMAT_PCM;
892 target.nChannels = channels;
893 target.nSamplesPerSec = freq;
894 target.wBitsPerSample = bits_per_samp;
895 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
896 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
897 target.cbSize = 0;
899 hr = IAudioClient_IsFormatSupported(device->client,
900 AUDCLNT_SHAREMODE_SHARED, &target, &closer_fmt);
901 if(closer_fmt)
902 CoTaskMemFree(closer_fmt);
903 if(hr != S_OK)
904 return WAVERR_BADFORMAT;
906 /* device supports our target format, so see if MSACM can
907 * do the conversion */
908 if(is_out)
909 mr = acmStreamOpen(&device->acm_handle, NULL, fmt, &target, NULL,
910 0, 0, 0);
911 else
912 mr = acmStreamOpen(&device->acm_handle, NULL, &target, fmt, NULL,
913 0, 0, 0);
914 if(mr != MMSYSERR_NOERROR)
915 return mr;
917 /* yes it can. initialize the audioclient and return success */
918 if(is_query){
919 acmStreamClose(device->acm_handle, 0);
920 device->acm_handle = NULL;
921 return MMSYSERR_NOERROR;
924 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
925 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
926 AC_BUFLEN, 0, &target, &device->parent->session);
927 if(hr != S_OK){
928 WARN("Initialize failed: %08x\n", hr);
929 acmStreamClose(device->acm_handle, 0);
930 device->acm_handle = NULL;
931 return MMSYSERR_ERROR;
934 device->bytes_per_frame = target.nBlockAlign;
935 device->samples_per_sec = target.nSamplesPerSec;
937 TRACE("Success!\n");
939 return MMSYSERR_NOERROR;
942 static MMRESULT WINMM_MapDevice(WINMM_Device *device, BOOL is_query, BOOL is_out)
944 MMRESULT mr;
945 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)device->orig_fmt;
947 TRACE("(%p, %u)\n", device, is_out);
949 /* set up the ACM stream */
950 if(device->orig_fmt->wFormatTag != WAVE_FORMAT_PCM &&
951 !(device->orig_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
952 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
953 /* convert to PCM format if it's not already */
954 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
955 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
956 16, is_query, is_out);
957 if(mr == MMSYSERR_NOERROR)
958 return mr;
960 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
961 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
962 8, is_query, is_out);
963 if(mr == MMSYSERR_NOERROR)
964 return mr;
965 }else{
966 WORD channels;
968 /* first try just changing bit depth and channels */
969 channels = device->orig_fmt->nChannels;
970 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
971 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
972 if(mr == MMSYSERR_NOERROR)
973 return mr;
974 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
975 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
976 if(mr == MMSYSERR_NOERROR)
977 return mr;
979 channels = (channels == 2) ? 1 : 2;
980 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
981 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
982 if(mr == MMSYSERR_NOERROR)
983 return mr;
984 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
985 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
986 if(mr == MMSYSERR_NOERROR)
987 return mr;
989 /* that didn't work, so now try different sample rates */
990 channels = device->orig_fmt->nChannels;
991 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
992 if(mr == MMSYSERR_NOERROR)
993 return mr;
994 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
995 if(mr == MMSYSERR_NOERROR)
996 return mr;
997 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
998 if(mr == MMSYSERR_NOERROR)
999 return mr;
1000 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1001 if(mr == MMSYSERR_NOERROR)
1002 return mr;
1003 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1004 if(mr == MMSYSERR_NOERROR)
1005 return mr;
1007 channels = (channels == 2) ? 1 : 2;
1008 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
1009 if(mr == MMSYSERR_NOERROR)
1010 return mr;
1011 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
1012 if(mr == MMSYSERR_NOERROR)
1013 return mr;
1014 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
1015 if(mr == MMSYSERR_NOERROR)
1016 return mr;
1017 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1018 if(mr == MMSYSERR_NOERROR)
1019 return mr;
1020 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1021 if(mr == MMSYSERR_NOERROR)
1022 return mr;
1024 channels = device->orig_fmt->nChannels;
1025 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1026 if(mr == MMSYSERR_NOERROR)
1027 return mr;
1028 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1029 if(mr == MMSYSERR_NOERROR)
1030 return mr;
1031 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1032 if(mr == MMSYSERR_NOERROR)
1033 return mr;
1034 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1035 if(mr == MMSYSERR_NOERROR)
1036 return mr;
1037 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1038 if(mr == MMSYSERR_NOERROR)
1039 return mr;
1041 channels = (channels == 2) ? 1 : 2;
1042 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1043 if(mr == MMSYSERR_NOERROR)
1044 return mr;
1045 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1046 if(mr == MMSYSERR_NOERROR)
1047 return mr;
1048 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1049 if(mr == MMSYSERR_NOERROR)
1050 return mr;
1051 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1052 if(mr == MMSYSERR_NOERROR)
1053 return mr;
1054 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1055 if(mr == MMSYSERR_NOERROR)
1056 return mr;
1059 WARN("Unable to find compatible device!\n");
1060 return WAVERR_BADFORMAT;
1063 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_OpenInfo *info,
1064 BOOL is_out)
1066 LRESULT ret = MMSYSERR_NOMEM;
1067 HRESULT hr;
1069 hr = IMMDeviceEnumerator_GetDevice(g_devenum, device->parent->dev_id,
1070 &device->device);
1071 if(FAILED(hr)){
1072 WARN("Device %s (%s) unavailable: %08x\n",
1073 wine_dbgstr_w(device->parent->dev_id),
1074 wine_dbgstr_w(device->parent->out_caps.szPname), hr);
1075 ret = MMSYSERR_NODRIVER;
1076 goto error;
1079 /* this is where winexyz.drv opens the audio device */
1080 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
1081 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
1082 if(FAILED(hr)){
1083 WARN("Activate failed: %08x\n", hr);
1084 ret = hr2mmr(hr);
1085 if(ret == MMSYSERR_ERROR)
1086 ret = MMSYSERR_NOTENABLED;
1087 goto error;
1090 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
1091 /* we aren't guaranteed that the struct in lpFormat is a full
1092 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1093 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
1094 memcpy(device->orig_fmt, info->format, sizeof(PCMWAVEFORMAT));
1095 device->orig_fmt->cbSize = 0;
1096 if(device->orig_fmt->wBitsPerSample % 8 != 0){
1097 WARN("Fixing bad wBitsPerSample (%u)\n", device->orig_fmt->wBitsPerSample);
1098 device->orig_fmt->wBitsPerSample = (device->orig_fmt->wBitsPerSample + 7) & ~7;
1100 /* winmm ignores broken blockalign and avgbytes */
1101 if(device->orig_fmt->nBlockAlign != device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8){
1102 WARN("Fixing bad nBlockAlign (%u)\n", device->orig_fmt->nBlockAlign);
1103 device->orig_fmt->nBlockAlign = device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8;
1105 if (device->orig_fmt->nAvgBytesPerSec != device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign) {
1106 WARN("Fixing bad nAvgBytesPerSec (%u)\n", device->orig_fmt->nAvgBytesPerSec);
1107 device->orig_fmt->nAvgBytesPerSec = device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign;
1109 }else{
1110 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0,
1111 sizeof(WAVEFORMATEX) + info->format->cbSize);
1112 memcpy(device->orig_fmt, info->format,
1113 sizeof(WAVEFORMATEX) + info->format->cbSize);
1116 if(info->flags & WAVE_FORMAT_QUERY){
1117 WAVEFORMATEX *closer_fmt = NULL;
1119 hr = IAudioClient_IsFormatSupported(device->client,
1120 AUDCLNT_SHAREMODE_SHARED, device->orig_fmt, &closer_fmt);
1121 if(closer_fmt)
1122 CoTaskMemFree(closer_fmt);
1123 if((hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT) && !(info->flags & WAVE_FORMAT_DIRECT))
1124 ret = WINMM_MapDevice(device, TRUE, is_out);
1125 else
1126 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1127 goto error;
1130 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1131 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1132 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1133 if(FAILED(hr)){
1134 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1135 ret = WINMM_MapDevice(device, FALSE, is_out);
1136 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1137 goto error;
1138 }else{
1139 WARN("Initialize failed: %08x\n", hr);
1140 ret = hr2mmr(hr);
1141 goto error;
1143 }else{
1144 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1145 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1148 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1149 (void**)&device->clock);
1150 if(FAILED(hr)){
1151 WARN("GetService failed: %08x\n", hr);
1152 goto error;
1155 if(!device->event){
1156 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1157 if(!device->event){
1158 WARN("CreateEvent failed: %08x\n", hr);
1159 goto error;
1162 /* As the devices thread is waiting on g_device_handles, it can
1163 * only be modified from within this same thread. */
1164 if(g_device_handles){
1165 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1166 sizeof(HANDLE) * (g_devhandle_count + 1));
1167 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1168 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1169 }else{
1170 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1171 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1172 sizeof(WINMM_Device *));
1174 g_device_handles[g_devhandle_count] = device->event;
1175 g_handle_devices[g_devhandle_count] = device;
1176 ++g_devhandle_count;
1179 hr = IAudioClient_SetEventHandle(device->client, device->event);
1180 if(FAILED(hr)){
1181 WARN("SetEventHandle failed: %08x\n", hr);
1182 goto error;
1185 if(info->reset){
1186 device->played_frames = 0;
1187 device->ofs_bytes = 0;
1188 device->loop_counter = 0;
1189 device->first = device->last = device->playing = device->loop_start = NULL;
1192 device->stopped = TRUE;
1193 device->last_clock_pos = 0;
1195 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1196 device->cb_info.callback = info->callback;
1197 device->cb_info.user = info->cb_user;
1198 device->cb_info.hwave = device->handle;
1200 info->handle = device->handle;
1202 return MMSYSERR_NOERROR;
1204 error:
1205 if(device->client){
1206 IAudioClient_Release(device->client);
1207 device->client = NULL;
1209 if(device->device){
1210 IMMDevice_Release(device->device);
1211 device->device = NULL;
1214 return ret;
1217 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1219 WINMM_Device *device;
1220 LRESULT ret = MMSYSERR_ERROR;
1221 HRESULT hr;
1223 if(info->handle != 0){
1224 device = WINMM_GetDeviceFromHWAVE(info->handle);
1225 if(!device){
1226 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1227 return MMSYSERR_ERROR;
1230 EnterCriticalSection(&device->lock);
1232 device->open = TRUE;
1233 }else{
1234 CRITICAL_SECTION *lock;
1235 UINT internal_index;
1236 WINMM_Device **devices;
1237 WINMM_MMDevice *mmdevice;
1239 if(WINMM_IsMapper(info->req_device)){
1240 if (g_outmmdevices_count == 0)
1241 return MMSYSERR_BADDEVICEID;
1242 devices = g_out_mapper_devices;
1243 mmdevice = read_map(g_out_map, 0);
1244 lock = &g_devthread_lock;
1245 internal_index = MAPPER_INDEX;
1246 }else{
1247 if(info->req_device >= g_outmmdevices_count)
1248 return MMSYSERR_BADDEVICEID;
1250 mmdevice = read_map(g_out_map, info->req_device);
1252 if(!mmdevice->out_caps.szPname[0])
1253 return MMSYSERR_NOTENABLED;
1255 devices = mmdevice->devices;
1256 lock = &mmdevice->lock;
1257 internal_index = mmdevice->index;
1260 EnterCriticalSection(lock);
1262 device = WINMM_FindUnusedDevice(devices, mmdevice,
1263 internal_index, TRUE);
1264 if(!device){
1265 LeaveCriticalSection(lock);
1266 return MMSYSERR_ALLOCATED;
1269 LeaveCriticalSection(lock);
1272 ret = WINMM_OpenDevice(device, info, TRUE);
1273 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1274 goto error;
1275 ret = MMSYSERR_ERROR;
1277 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1278 (void**)&device->render);
1279 if(FAILED(hr)){
1280 ERR("GetService failed: %08x\n", hr);
1281 goto error;
1284 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1285 (void**)&device->volume);
1286 if(FAILED(hr)){
1287 ERR("GetService failed: %08x\n", hr);
1288 goto error;
1291 LeaveCriticalSection(&device->lock);
1293 return MMSYSERR_NOERROR;
1295 error:
1296 if(device->device){
1297 IMMDevice_Release(device->device);
1298 device->device = NULL;
1300 if(device->client){
1301 IAudioClient_Release(device->client);
1302 device->client = NULL;
1304 if(device->render){
1305 IAudioRenderClient_Release(device->render);
1306 device->render = NULL;
1308 if(device->volume){
1309 IAudioStreamVolume_Release(device->volume);
1310 device->volume = NULL;
1312 if(device->clock){
1313 IAudioClock_Release(device->clock);
1314 device->clock = NULL;
1316 device->open = FALSE;
1317 LeaveCriticalSection(&device->lock);
1318 return ret;
1321 static LRESULT WID_Open(WINMM_OpenInfo *info)
1323 WINMM_Device *device, **devices;
1324 WINMM_MMDevice *mmdevice;
1325 UINT internal_index;
1326 CRITICAL_SECTION *lock;
1327 LRESULT ret = MMSYSERR_ERROR;
1328 HRESULT hr;
1330 if(WINMM_IsMapper(info->req_device)){
1331 if (g_inmmdevices_count == 0)
1332 return MMSYSERR_BADDEVICEID;
1333 devices = g_in_mapper_devices;
1334 mmdevice = read_map(g_in_map, 0);
1335 lock = &g_devthread_lock;
1336 internal_index = MAPPER_INDEX;
1337 }else{
1338 if(info->req_device >= g_inmmdevices_count)
1339 return MMSYSERR_BADDEVICEID;
1341 mmdevice = read_map(g_in_map, info->req_device);
1343 if(!mmdevice->in_caps.szPname[0])
1344 return MMSYSERR_NOTENABLED;
1346 devices = mmdevice->devices;
1347 lock = &mmdevice->lock;
1348 internal_index = mmdevice->index;
1351 EnterCriticalSection(lock);
1353 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1354 if(!device){
1355 LeaveCriticalSection(lock);
1356 return MMSYSERR_ALLOCATED;
1359 LeaveCriticalSection(lock);
1361 ret = WINMM_OpenDevice(device, info, FALSE);
1362 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1363 goto error;
1364 ret = MMSYSERR_ERROR;
1366 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1367 (void**)&device->capture);
1368 if(FAILED(hr)){
1369 WARN("GetService failed: %08x\n", hr);
1370 goto error;
1373 LeaveCriticalSection(&device->lock);
1375 return MMSYSERR_NOERROR;
1377 error:
1378 if(device->device){
1379 IMMDevice_Release(device->device);
1380 device->device = NULL;
1382 if(device->client){
1383 IAudioClient_Release(device->client);
1384 device->client = NULL;
1386 if(device->capture){
1387 IAudioCaptureClient_Release(device->capture);
1388 device->capture = NULL;
1390 if(device->clock){
1391 IAudioClock_Release(device->clock);
1392 device->clock = NULL;
1394 device->open = FALSE;
1395 LeaveCriticalSection(&device->lock);
1396 return ret;
1399 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1401 device->open = FALSE;
1403 if(!device->stopped){
1404 IAudioClient_Stop(device->client);
1405 device->stopped = TRUE;
1408 if(device->acm_handle){
1409 acmStreamClose(device->acm_handle, 0);
1410 device->acm_handle = NULL;
1413 IMMDevice_Release(device->device);
1414 device->device = NULL;
1416 IAudioClient_Release(device->client);
1417 device->client = NULL;
1419 IAudioClock_Release(device->clock);
1420 device->clock = NULL;
1422 return S_OK;
1425 static LRESULT WOD_Close(HWAVEOUT hwave)
1427 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1429 TRACE("(%p)\n", hwave);
1431 if(!WINMM_ValidateAndLock(device))
1432 return MMSYSERR_INVALHANDLE;
1434 WINMM_CloseDevice(device);
1436 IAudioRenderClient_Release(device->render);
1437 device->render = NULL;
1439 IAudioStreamVolume_Release(device->volume);
1440 device->volume = NULL;
1442 LeaveCriticalSection(&device->lock);
1444 return MMSYSERR_NOERROR;
1447 static LRESULT WID_Close(HWAVEIN hwave)
1449 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1451 TRACE("(%p)\n", hwave);
1453 if(!WINMM_ValidateAndLock(device))
1454 return MMSYSERR_INVALHANDLE;
1456 WINMM_CloseDevice(device);
1458 IAudioCaptureClient_Release(device->capture);
1459 device->capture = NULL;
1461 LeaveCriticalSection(&device->lock);
1463 return MMSYSERR_NOERROR;
1466 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1468 return length - length % device->bytes_per_frame;
1471 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1473 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1475 TRACE("(%p, %p)\n", hwave, header);
1477 if(!WINMM_ValidateAndLock(device))
1478 return MMSYSERR_INVALHANDLE;
1480 if(device->render && device->acm_handle){
1481 ACMSTREAMHEADER *ash;
1482 DWORD size;
1483 MMRESULT mr;
1485 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1486 ACM_STREAMSIZEF_SOURCE);
1487 if(mr != MMSYSERR_NOERROR){
1488 LeaveCriticalSection(&device->lock);
1489 return mr;
1492 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1493 if(!ash){
1494 LeaveCriticalSection(&device->lock);
1495 return MMSYSERR_NOMEM;
1498 ash->cbStruct = sizeof(*ash);
1499 ash->fdwStatus = 0;
1500 ash->dwUser = (DWORD_PTR)header;
1501 ash->pbSrc = (BYTE*)header->lpData;
1502 ash->cbSrcLength = header->dwBufferLength;
1503 ash->dwSrcUser = header->dwUser;
1504 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1505 ash->cbDstLength = size;
1506 ash->dwDstUser = 0;
1508 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1509 if(mr != MMSYSERR_NOERROR){
1510 HeapFree(GetProcessHeap(), 0, ash);
1511 LeaveCriticalSection(&device->lock);
1512 return mr;
1515 header->reserved = (DWORD_PTR)ash;
1518 LeaveCriticalSection(&device->lock);
1520 header->dwFlags |= WHDR_PREPARED;
1521 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1523 return MMSYSERR_NOERROR;
1526 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1528 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1530 TRACE("(%p, %p)\n", hwave, header);
1532 if(!WINMM_ValidateAndLock(device))
1533 return MMSYSERR_INVALHANDLE;
1535 if(device->render && device->acm_handle){
1536 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1538 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1540 HeapFree(GetProcessHeap(), 0, ash);
1543 LeaveCriticalSection(&device->lock);
1545 header->dwFlags &= ~WHDR_PREPARED;
1547 return MMSYSERR_NOERROR;
1550 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1552 if(device->acm_handle){
1553 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1554 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1557 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1560 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1562 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1565 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1567 HRESULT hr;
1568 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1569 UINT64 clock_freq, clock_pos, clock_frames;
1570 UINT32 nloops, queue_frames = 0;
1572 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1573 if(FAILED(hr)){
1574 WARN("GetFrequency failed: %08x\n", hr);
1575 return NULL;
1578 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1579 if(FAILED(hr)){
1580 WARN("GetPosition failed: %08x\n", hr);
1581 return NULL;
1584 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1586 nloops = device->loop_counter;
1587 while(queue &&
1588 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1589 clock_frames - device->last_clock_pos + device->remainder_frames){
1590 if(!nloops){
1591 last = queue;
1592 device->last_clock_pos += queue_frames;
1593 device->remainder_frames = 0;
1594 queue_frames = 0;
1595 queue = device->first = queue->lpNext;
1596 }else{
1597 if(queue->dwFlags & WHDR_BEGINLOOP){
1598 if(queue->dwFlags & WHDR_ENDLOOP)
1599 --nloops;
1600 else
1601 queue = queue->lpNext;
1602 }else if(queue->dwFlags & WHDR_ENDLOOP){
1603 queue = device->loop_start;
1604 --nloops;
1609 if(last){
1610 last->lpNext = NULL;
1611 return first;
1612 }else
1613 return NULL;
1616 static void WOD_PushData(WINMM_Device *device)
1618 WINMM_CBInfo cb_info;
1619 HRESULT hr;
1620 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1621 UINT32 queue_bytes, nloops;
1622 BYTE *data;
1623 WAVEHDR *queue, *first = NULL;
1625 TRACE("(%p)\n", device->handle);
1627 EnterCriticalSection(&device->lock);
1629 if(!device->device)
1630 goto exit;
1632 if(!device->first){
1633 if (device->stopped)
1634 goto exit;
1635 device->stopped = TRUE;
1636 device->last_clock_pos = 0;
1637 IAudioClient_Stop(device->client);
1638 IAudioClient_Reset(device->client);
1639 goto exit;
1642 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1643 if(FAILED(hr)){
1644 WARN("GetBufferSize failed: %08x\n", hr);
1645 goto exit;
1648 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1649 if(FAILED(hr)){
1650 WARN("GetCurrentPadding failed: %08x\n", hr);
1651 goto exit;
1654 first = WOD_MarkDoneHeaders(device);
1656 /* determine which is larger between the available buffer size and
1657 * the amount of data left in the queue */
1658 avail_frames = bufsize - pad;
1660 queue = device->playing;
1661 ofs = device->ofs_bytes;
1662 queue_frames = 0;
1663 nloops = 0;
1664 while(queue && queue_frames < avail_frames){
1665 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1666 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1667 ofs = 0;
1669 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1670 queue = device->loop_start;
1671 ++nloops;
1672 }else
1673 queue = queue->lpNext;
1676 if(queue_frames < avail_frames)
1677 avail_frames = queue_frames;
1678 if(avail_frames == 0)
1679 goto exit;
1681 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1682 if(FAILED(hr)){
1683 WARN("GetBuffer failed: %08x\n", hr);
1684 goto exit;
1687 written = 0;
1688 while(device->playing && written < avail_frames){
1689 UINT32 copy_frames, copy_bytes;
1690 BYTE *queue_data;
1692 queue = device->playing;
1694 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1695 if(device->acm_handle)
1696 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1697 else
1698 queue_data = (BYTE*)queue->lpData;
1700 queue_frames = (queue_bytes - device->ofs_bytes) /
1701 device->bytes_per_frame;
1703 copy_frames = queue_frames < (avail_frames - written) ?
1704 queue_frames : avail_frames - written;
1705 copy_bytes = copy_frames * device->bytes_per_frame;
1707 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1709 data += copy_bytes;
1710 written += copy_frames;
1711 device->ofs_bytes += copy_bytes;
1713 if(device->ofs_bytes >= queue_bytes){
1714 device->ofs_bytes = 0;
1716 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1717 device->playing = queue->lpNext;
1718 else{
1719 if(queue->dwFlags & WHDR_BEGINLOOP){
1720 device->loop_start = device->playing;
1721 device->playing = queue->lpNext;
1722 device->loop_counter = queue->dwLoops;
1724 if(queue->dwFlags & WHDR_ENDLOOP){
1725 --device->loop_counter;
1726 if(device->loop_counter)
1727 device->playing = device->loop_start;
1728 else
1729 device->loop_start = device->playing = queue->lpNext;
1735 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1736 if(FAILED(hr)){
1737 WARN("ReleaseBuffer failed: %08x\n", hr);
1738 goto exit;
1741 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1742 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1743 else
1744 device->played_frames += avail_frames;
1746 exit:
1747 cb_info = device->cb_info;
1749 LeaveCriticalSection(&device->lock);
1751 while(first){
1752 WAVEHDR *next = first->lpNext;
1753 first->dwFlags &= ~WHDR_INQUEUE;
1754 first->dwFlags |= WHDR_DONE;
1755 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1756 first = next;
1760 static void WID_PullACMData(WINMM_Device *device)
1762 UINT32 packet, packet_bytes;
1763 DWORD flags;
1764 BYTE *data;
1765 WAVEHDR *queue;
1766 HRESULT hr;
1767 MMRESULT mr;
1769 if(device->acm_hdr.cbDstLength == 0){
1770 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1771 &flags, NULL, NULL);
1772 if(hr != S_OK){
1773 if(FAILED(hr))
1774 WARN("GetBuffer failed: %08x\n", hr);
1775 return;
1778 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1779 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1781 device->acm_offs = 0;
1783 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1784 device->acm_hdr.fdwStatus = 0;
1785 device->acm_hdr.dwUser = 0;
1786 device->acm_hdr.pbSrc = data;
1787 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1788 device->acm_hdr.cbSrcLengthUsed = 0;
1789 device->acm_hdr.dwSrcUser = 0;
1790 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1791 device->acm_hdr.cbDstLength = packet_bytes;
1792 device->acm_hdr.cbDstLengthUsed = 0;
1793 device->acm_hdr.dwDstUser = 0;
1795 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1796 if(mr != MMSYSERR_NOERROR){
1797 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1798 return;
1801 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1802 if(mr != MMSYSERR_NOERROR){
1803 WARN("acmStreamConvert failed: %d\n", mr);
1804 return;
1807 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1808 if(FAILED(hr))
1809 WARN("ReleaseBuffer failed: %08x\n", hr);
1811 device->played_frames += packet;
1814 queue = device->first;
1815 while(queue){
1816 UINT32 to_copy_bytes;
1818 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1819 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1821 memcpy(queue->lpData + queue->dwBytesRecorded,
1822 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1824 queue->dwBytesRecorded += to_copy_bytes;
1825 device->acm_offs += to_copy_bytes;
1827 if(queue->dwBufferLength - queue->dwBytesRecorded <
1828 device->bytes_per_frame){
1829 queue->dwFlags &= ~WHDR_INQUEUE;
1830 queue->dwFlags |= WHDR_DONE;
1831 device->first = queue = queue->lpNext;
1834 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1835 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1836 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1837 device->acm_hdr.cbDstLength = 0;
1838 device->acm_hdr.cbDstLengthUsed = 0;
1840 /* done with this ACM Header, so try to pull more data */
1841 WID_PullACMData(device);
1842 return;
1846 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1847 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1848 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1849 device->acm_hdr.cbDstLength = 0;
1850 device->acm_hdr.cbDstLengthUsed = 0;
1853 static void WID_PullData(WINMM_Device *device)
1855 WINMM_CBInfo cb_info;
1856 WAVEHDR *queue, *first = NULL, *last = NULL;
1857 HRESULT hr;
1859 TRACE("(%p)\n", device->handle);
1861 EnterCriticalSection(&device->lock);
1863 if(!device->device || !device->first)
1864 goto exit;
1866 first = device->first;
1868 if(device->acm_handle){
1869 WID_PullACMData(device);
1870 goto exit;
1873 while(device->first){
1874 BYTE *data;
1875 UINT32 packet_len, packet;
1876 DWORD flags;
1878 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1879 &flags, NULL, NULL);
1880 if(hr != S_OK){
1881 if(FAILED(hr))
1882 WARN("GetBuffer failed: %08x\n", hr);
1883 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1884 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1885 goto exit;
1888 packet = packet_len;
1889 queue = device->first;
1890 while(queue && packet > 0){
1891 UINT32 to_copy_bytes;
1893 to_copy_bytes = min(packet * device->bytes_per_frame,
1894 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1896 memcpy(queue->lpData + queue->dwBytesRecorded,
1897 data + (packet_len - packet) * device->bytes_per_frame,
1898 to_copy_bytes);
1900 queue->dwBytesRecorded += to_copy_bytes;
1902 if(queue->dwBufferLength - queue->dwBytesRecorded <
1903 device->bytes_per_frame){
1904 last = queue;
1905 device->first = queue = queue->lpNext;
1908 packet -= to_copy_bytes / device->bytes_per_frame;
1911 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1912 if(FAILED(hr))
1913 WARN("ReleaseBuffer failed: %08x\n", hr);
1915 if(packet > 0)
1916 WARN("losing %u frames\n", packet);
1917 device->played_frames += packet_len - packet;
1920 exit:
1921 cb_info = device->cb_info;
1923 LeaveCriticalSection(&device->lock);
1925 if(last){
1926 last->lpNext = NULL;
1927 while(first){
1928 WAVEHDR *next = first->lpNext;
1929 first->dwFlags &= ~WHDR_INQUEUE;
1930 first->dwFlags |= WHDR_DONE;
1931 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1932 first = next;
1937 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1939 HRESULT hr;
1941 TRACE("(%p)\n", device->handle);
1943 if(device->render)
1944 /* prebuffer data before starting */
1945 WOD_PushData(device);
1947 if(device->stopped){
1948 device->stopped = FALSE;
1950 hr = IAudioClient_Start(device->client);
1951 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1952 device->stopped = TRUE;
1953 WARN("Start failed: %08x\n", hr);
1954 return MMSYSERR_ERROR;
1958 return MMSYSERR_NOERROR;
1961 static LRESULT WINMM_Pause(HWAVE hwave)
1963 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1964 HRESULT hr;
1966 TRACE("(%p)\n", hwave);
1968 if(!WINMM_ValidateAndLock(device))
1969 return MMSYSERR_INVALHANDLE;
1971 hr = IAudioClient_Stop(device->client);
1972 if(FAILED(hr)){
1973 LeaveCriticalSection(&device->lock);
1974 WARN("Stop failed: %08x\n", hr);
1975 return MMSYSERR_ERROR;
1978 device->stopped = FALSE;
1980 LeaveCriticalSection(&device->lock);
1982 return MMSYSERR_NOERROR;
1985 static LRESULT WINMM_Reset(HWAVE hwave)
1987 WINMM_CBInfo cb_info;
1988 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1989 BOOL is_out;
1990 WAVEHDR *first;
1991 HRESULT hr;
1993 TRACE("(%p)\n", hwave);
1995 if(!WINMM_ValidateAndLock(device))
1996 return MMSYSERR_INVALHANDLE;
1998 hr = IAudioClient_Stop(device->client);
1999 if(FAILED(hr)){
2000 LeaveCriticalSection(&device->lock);
2001 WARN("Stop failed: %08x\n", hr);
2002 return MMSYSERR_ERROR;
2004 device->stopped = TRUE;
2006 first = device->first;
2007 device->first = device->last = device->playing = NULL;
2008 device->ofs_bytes = 0;
2009 device->played_frames = 0;
2010 device->loop_counter = 0;
2011 device->last_clock_pos = 0;
2012 IAudioClient_Reset(device->client);
2014 cb_info = device->cb_info;
2015 is_out = device->render != NULL;
2017 LeaveCriticalSection(&device->lock);
2019 while(first){
2020 WAVEHDR *next = first->lpNext;
2021 first->dwFlags &= ~WHDR_INQUEUE;
2022 first->dwFlags |= WHDR_DONE;
2023 if(is_out)
2024 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2025 else
2026 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2027 first = next;
2030 return MMSYSERR_NOERROR;
2033 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2034 UINT32 sample_rate, UINT32 bytes_per_sec)
2036 switch(time->wType){
2037 case TIME_SAMPLES:
2038 time->u.sample = played_frames;
2039 return MMSYSERR_NOERROR;
2040 case TIME_MS:
2041 time->u.ms = (UINT64)played_frames * 1000 / sample_rate;
2042 return MMSYSERR_NOERROR;
2043 case TIME_SMPTE:
2044 time->u.smpte.fps = 30;
2045 played_frames += sample_rate / time->u.smpte.fps - 1; /* round up */
2046 time->u.smpte.frame = (played_frames % sample_rate) * time->u.smpte.fps / sample_rate;
2047 played_frames /= sample_rate; /* yields seconds */
2048 time->u.smpte.sec = played_frames % 60;
2049 played_frames /= 60;
2050 time->u.smpte.min = played_frames % 60;
2051 time->u.smpte.hour= played_frames / 60;
2052 return MMSYSERR_NOERROR;
2053 default:
2054 time->wType = TIME_BYTES;
2055 /* fall through */
2056 case TIME_BYTES:
2057 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2058 return MMSYSERR_NOERROR;
2062 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2064 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2065 UINT32 played_frames, sample_rate, bytes_per_sec;
2067 TRACE("(%p, %p)\n", hwave, time);
2069 if(!WINMM_ValidateAndLock(device))
2070 return MMSYSERR_INVALHANDLE;
2072 played_frames = device->played_frames;
2073 sample_rate = device->orig_fmt->nSamplesPerSec;
2074 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2076 LeaveCriticalSection(&device->lock);
2078 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2081 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2082 UINT *mmdev_index)
2084 UINT mmdev, dev, junk, *out;
2085 BOOL is_out;
2087 if(!mmdev_index)
2088 out = &mmdev;
2089 else
2090 out = mmdev_index;
2092 switch(flags & 0xF0000000){
2093 case MIXER_OBJECTF_MIXER: /* == 0 */
2094 *out = HandleToULong(hmix);
2095 if(*out < g_outmmdevices_count)
2096 return read_map(g_out_map, *out);
2097 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2098 *out -= g_outmmdevices_count;
2099 return read_map(g_in_map, *out);
2101 /* fall through -- if it's not a valid mixer device, then
2102 * it could be a valid mixer handle. windows seems to do
2103 * this as well. */
2104 case MIXER_OBJECTF_HMIXER:
2105 case MIXER_OBJECTF_HWAVEOUT:
2106 case MIXER_OBJECTF_HWAVEIN:
2107 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2108 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2109 (!is_out && *out >= g_inmmdevices_count))
2110 return NULL;
2111 if(is_out)
2112 return read_map(g_out_map, *out);
2113 return read_map(g_in_map, *out);
2114 case MIXER_OBJECTF_WAVEOUT:
2115 *out = HandleToULong(hmix);
2116 if(*out < g_outmmdevices_count)
2117 return read_map(g_out_map, *out);
2118 return NULL;
2119 case MIXER_OBJECTF_WAVEIN:
2120 *out = HandleToULong(hmix);
2121 if(*out < g_inmmdevices_count)
2122 return read_map(g_in_map, *out);
2123 return NULL;
2126 return NULL;
2129 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2131 IAudioSessionManager *sesman;
2132 IMMDevice *device;
2133 HRESULT hr;
2135 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2136 if(FAILED(hr)){
2137 WARN("Device %s (%s) unavailable: %08x\n",
2138 wine_dbgstr_w(mmdevice->dev_id),
2139 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2140 return MMSYSERR_ERROR;
2143 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2144 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2145 if(FAILED(hr)){
2146 WARN("Activate failed: %08x\n", hr);
2147 IMMDevice_Release(device);
2148 return MMSYSERR_ERROR;
2151 IMMDevice_Release(device);
2153 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2154 FALSE, &mmdevice->volume);
2155 IAudioSessionManager_Release(sesman);
2156 if(FAILED(hr)){
2157 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2158 return MMSYSERR_ERROR;
2161 return MMSYSERR_NOERROR;
2164 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2166 WINMM_MMDevice *mmdevice;
2167 MIXERCONTROLDETAILS *control = details->details;
2168 HRESULT hr;
2170 TRACE("(%p)\n", details->hmix);
2172 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2173 if(!mmdevice)
2174 return MMSYSERR_INVALHANDLE;
2176 EnterCriticalSection(&mmdevice->lock);
2178 if(!mmdevice->volume){
2179 MMRESULT mr;
2181 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2182 if(mr != MMSYSERR_NOERROR){
2183 LeaveCriticalSection(&mmdevice->lock);
2184 return mr;
2188 if(control->dwControlID == 0){
2189 float vol;
2190 MIXERCONTROLDETAILS_UNSIGNED *udet;
2192 if(!control->paDetails ||
2193 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2194 LeaveCriticalSection(&mmdevice->lock);
2195 return MMSYSERR_INVALPARAM;
2198 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2199 if(FAILED(hr)){
2200 WARN("GetMasterVolume failed: %08x\n", hr);
2201 LeaveCriticalSection(&mmdevice->lock);
2202 return MMSYSERR_ERROR;
2205 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2206 udet->dwValue = vol * ((unsigned int)0xFFFF);
2207 }else if(control->dwControlID == 1){
2208 BOOL mute;
2209 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2211 if(!control->paDetails ||
2212 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2213 LeaveCriticalSection(&mmdevice->lock);
2214 return MMSYSERR_INVALPARAM;
2217 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2218 if(FAILED(hr)){
2219 WARN("GetMute failed: %08x\n", hr);
2220 LeaveCriticalSection(&mmdevice->lock);
2221 return MMSYSERR_ERROR;
2224 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2225 bdet->fValue = mute;
2226 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2227 FIXME("What should the sw-side mixer controls map to?\n");
2228 }else{
2229 LeaveCriticalSection(&mmdevice->lock);
2230 return MIXERR_INVALCONTROL;
2233 LeaveCriticalSection(&mmdevice->lock);
2235 return MMSYSERR_NOERROR;
2238 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2240 WINMM_MMDevice *mmdevice;
2241 MIXERCONTROLDETAILS *control = details->details;
2242 HRESULT hr;
2244 TRACE("(%p)\n", details->hmix);
2246 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2247 if(!mmdevice)
2248 return MMSYSERR_INVALHANDLE;
2250 EnterCriticalSection(&mmdevice->lock);
2252 if(!mmdevice->volume){
2253 MMRESULT mr;
2255 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2256 if(mr != MMSYSERR_NOERROR){
2257 LeaveCriticalSection(&mmdevice->lock);
2258 return mr;
2262 if(control->dwControlID == 0){
2263 float vol;
2264 MIXERCONTROLDETAILS_UNSIGNED *udet;
2266 if(!control->paDetails ||
2267 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2268 LeaveCriticalSection(&mmdevice->lock);
2269 return MMSYSERR_INVALPARAM;
2272 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2274 if(udet->dwValue > 65535){
2275 LeaveCriticalSection(&mmdevice->lock);
2276 return MMSYSERR_INVALPARAM;
2279 vol = udet->dwValue / 65535.f;
2281 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2282 if(FAILED(hr)){
2283 WARN("SetMasterVolume failed: %08x\n", hr);
2284 LeaveCriticalSection(&mmdevice->lock);
2285 return MMSYSERR_ERROR;
2287 }else if(control->dwControlID == 1){
2288 BOOL mute;
2289 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2291 if(!control->paDetails ||
2292 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2293 LeaveCriticalSection(&mmdevice->lock);
2294 return MMSYSERR_INVALPARAM;
2297 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2298 mute = bdet->fValue;
2300 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2301 if(FAILED(hr)){
2302 WARN("SetMute failed: %08x\n", hr);
2303 LeaveCriticalSection(&mmdevice->lock);
2304 return MMSYSERR_ERROR;
2306 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2307 FIXME("What should the sw-side mixer controls map to?\n");
2308 }else{
2309 LeaveCriticalSection(&mmdevice->lock);
2310 return MIXERR_INVALCONTROL;
2313 LeaveCriticalSection(&mmdevice->lock);
2315 return MMSYSERR_NOERROR;
2318 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2320 WINMM_MMDevice *mmdevice;
2321 IMMDevice *device;
2322 IPropertyStore *ps;
2323 PROPVARIANT pv;
2324 DWORD len_bytes;
2325 HRESULT hr;
2327 static const PROPERTYKEY deviceinterface_key = {
2328 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2331 if(WINMM_IsMapper(info->index)){
2332 if(info->str){
2333 if(*info->len_bytes < sizeof(WCHAR))
2334 return MMSYSERR_INVALPARAM;
2335 *info->str = 0;
2336 }else
2337 *info->len_bytes = sizeof(WCHAR);
2338 return MMSYSERR_NOERROR;
2341 if(info->is_out){
2342 if(info->index >= g_outmmdevices_count)
2343 return MMSYSERR_INVALHANDLE;
2345 mmdevice = &g_out_mmdevices[info->index];
2346 }else{
2347 if(info->index >= g_inmmdevices_count)
2348 return MMSYSERR_INVALHANDLE;
2350 mmdevice = &g_in_mmdevices[info->index];
2353 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2354 &device);
2355 if(FAILED(hr)){
2356 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2357 return MMSYSERR_ERROR;
2360 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2361 if(FAILED(hr)){
2362 WARN("OpenPropertyStore failed: %08x\n", hr);
2363 IMMDevice_Release(device);
2364 return MMSYSERR_ERROR;
2367 PropVariantInit(&pv);
2368 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2369 if(FAILED(hr)){
2370 WARN("GetValue failed: %08x\n", hr);
2371 IPropertyStore_Release(ps);
2372 IMMDevice_Release(device);
2373 return MMSYSERR_ERROR;
2375 if(pv.vt != VT_LPWSTR){
2376 WARN("Got unexpected property type: %u\n", pv.vt);
2377 PropVariantClear(&pv);
2378 IPropertyStore_Release(ps);
2379 IMMDevice_Release(device);
2380 return MMSYSERR_ERROR;
2383 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2385 if(info->str){
2386 if(len_bytes > *info->len_bytes){
2387 PropVariantClear(&pv);
2388 IPropertyStore_Release(ps);
2389 IMMDevice_Release(device);
2390 return MMSYSERR_INVALPARAM;
2393 memcpy(info->str, pv.u.pwszVal, len_bytes);
2394 }else
2395 *info->len_bytes = len_bytes;
2397 PropVariantClear(&pv);
2398 IPropertyStore_Release(ps);
2399 IMMDevice_Release(device);
2401 return MMSYSERR_NOERROR;
2404 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2405 LPARAM lparam)
2407 switch(msg){
2408 case WODM_OPEN:
2409 return WOD_Open((WINMM_OpenInfo*)wparam);
2410 case WODM_CLOSE:
2411 return WOD_Close((HWAVEOUT)wparam);
2412 case WIDM_OPEN:
2413 return WID_Open((WINMM_OpenInfo*)wparam);
2414 case WIDM_CLOSE:
2415 return WID_Close((HWAVEIN)wparam);
2416 case MXDM_GETCONTROLDETAILS:
2417 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2418 case MXDM_SETCONTROLDETAILS:
2419 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2420 case DRV_QUERYDEVICEINTERFACESIZE:
2421 case DRV_QUERYDEVICEINTERFACE:
2422 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2424 return DefWindowProcW(hwnd, msg, wparam, lparam);
2427 static BOOL WINMM_DevicesThreadDone(void)
2429 UINT i;
2431 EnterCriticalSection(&g_devthread_lock);
2433 if(g_devthread_token > 0){
2434 LeaveCriticalSection(&g_devthread_lock);
2435 return FALSE;
2438 for(i = 0; i < g_devhandle_count; ++i){
2439 if(g_handle_devices[i]->open){
2440 LeaveCriticalSection(&g_devthread_lock);
2441 return FALSE;
2445 DestroyWindow(g_devices_hwnd);
2446 g_devices_hwnd = NULL;
2447 IMMDeviceEnumerator_Release(g_devenum);
2448 g_devenum = NULL;
2449 CoUninitialize();
2451 LeaveCriticalSection(&g_devthread_lock);
2453 return TRUE;
2456 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2458 HANDLE evt = arg;
2459 HRESULT hr;
2460 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2462 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2463 if(FAILED(hr)){
2464 WARN("CoInitializeEx failed: %08x\n", hr);
2465 FreeLibraryAndExitThread(g_devthread_module, 1);
2468 hr = WINMM_InitMMDevices();
2469 if(FAILED(hr)){
2470 CoUninitialize();
2471 FreeLibraryAndExitThread(g_devthread_module, 1);
2474 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2475 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2476 if(FAILED(hr)){
2477 WARN("CoCreateInstance failed: %08x\n", hr);
2478 CoUninitialize();
2479 FreeLibraryAndExitThread(g_devthread_module, 1);
2482 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2483 HWND_MESSAGE, NULL, NULL, NULL);
2484 if(!g_devices_hwnd){
2485 WARN("CreateWindow failed: %d\n", GetLastError());
2486 CoUninitialize();
2487 FreeLibraryAndExitThread(g_devthread_module, 1);
2490 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2491 (LONG_PTR)WINMM_DevicesMsgProc);
2493 /* inform caller that the thread is ready to process messages */
2494 SetEvent(evt);
2495 evt = NULL; /* do not use after this point */
2497 while(1){
2498 DWORD wait;
2499 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2500 FALSE, INFINITE, QS_ALLINPUT);
2501 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2502 MSG msg;
2503 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2504 WARN("Unexpected message: 0x%x\n", msg.message);
2505 if(!g_devices_hwnd)
2506 break;
2507 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2508 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2509 if(device->render)
2510 WOD_PushData(device);
2511 else
2512 WID_PullData(device);
2513 }else
2514 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2515 GetLastError());
2516 if(WINMM_DevicesThreadDone()){
2517 TRACE("Quitting devices thread\n");
2518 FreeLibraryAndExitThread(g_devthread_module, 0);
2522 FreeLibraryAndExitThread(g_devthread_module, 0);
2525 /* on success, increments g_devthread_token to prevent
2526 * device thread shutdown. caller must decrement. */
2527 static BOOL WINMM_StartDevicesThread(void)
2529 HANDLE events[2];
2530 DWORD wait;
2532 EnterCriticalSection(&g_devthread_lock);
2534 if(g_devices_hwnd){
2535 wait = WaitForSingleObject(g_devices_thread, 0);
2536 if(wait == WAIT_TIMEOUT){
2537 /* thread still running */
2538 InterlockedIncrement(&g_devthread_token);
2539 LeaveCriticalSection(&g_devthread_lock);
2540 return TRUE;
2542 if(wait != WAIT_OBJECT_0){
2543 /* error */
2544 LeaveCriticalSection(&g_devthread_lock);
2545 return FALSE;
2547 TRACE("Devices thread left dangling message window?\n");
2548 g_devices_hwnd = NULL;
2549 CloseHandle(g_devices_thread);
2550 g_devices_thread = NULL;
2551 }else if(g_devices_thread){
2552 WaitForSingleObject(g_devices_thread, INFINITE);
2553 CloseHandle(g_devices_thread);
2554 g_devices_thread = NULL;
2557 TRACE("Starting up devices thread\n");
2559 /* The devices thread holds a reference to the winmm module
2560 * to prevent it from unloading while it's running. */
2561 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2562 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2564 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2566 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2567 events[0], 0, NULL);
2568 if(!g_devices_thread){
2569 LeaveCriticalSection(&g_devthread_lock);
2570 CloseHandle(events[0]);
2571 FreeLibrary(g_devthread_module);
2572 return FALSE;
2575 events[1] = g_devices_thread;
2576 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2577 CloseHandle(events[0]);
2578 if(wait != WAIT_OBJECT_0){
2579 if(wait == 1 + WAIT_OBJECT_0){
2580 CloseHandle(g_devices_thread);
2581 g_devices_thread = NULL;
2582 g_devices_hwnd = NULL;
2584 LeaveCriticalSection(&g_devthread_lock);
2585 return FALSE;
2588 InterlockedIncrement(&g_devthread_token);
2590 LeaveCriticalSection(&g_devthread_lock);
2592 return TRUE;
2595 /**************************************************************************
2596 * waveOutGetNumDevs [WINMM.@]
2598 UINT WINAPI waveOutGetNumDevs(void)
2600 HRESULT hr = WINMM_InitMMDevices();
2601 if(FAILED(hr))
2602 return 0;
2604 TRACE("count: %u\n", g_outmmdevices_count);
2606 return g_outmmdevices_count;
2609 /**************************************************************************
2610 * waveOutGetDevCapsA [WINMM.@]
2612 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2613 UINT uSize)
2615 WAVEOUTCAPSW wocW;
2616 UINT ret;
2618 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2620 if(!lpCaps)
2621 return MMSYSERR_INVALPARAM;
2623 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2625 if (ret == MMSYSERR_NOERROR) {
2626 WAVEOUTCAPSA wocA;
2627 wocA.wMid = wocW.wMid;
2628 wocA.wPid = wocW.wPid;
2629 wocA.vDriverVersion = wocW.vDriverVersion;
2630 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2631 sizeof(wocA.szPname), NULL, NULL );
2632 wocA.dwFormats = wocW.dwFormats;
2633 wocA.wChannels = wocW.wChannels;
2634 wocA.dwSupport = wocW.dwSupport;
2635 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2637 return ret;
2640 /**************************************************************************
2641 * waveOutGetDevCapsW [WINMM.@]
2643 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2644 UINT uSize)
2646 WAVEOUTCAPSW mapper_caps, *caps;
2647 HRESULT hr;
2649 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2651 hr = WINMM_InitMMDevices();
2652 if(FAILED(hr))
2653 return MMSYSERR_NODRIVER;
2655 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2657 if(WINMM_IsMapper(uDeviceID)){
2658 /* FIXME: Should be localized */
2659 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2660 'n','d',' ','M','a','p','p','e','r',0};
2662 mapper_caps.wMid = 0xFF;
2663 mapper_caps.wPid = 0xFF;
2664 mapper_caps.vDriverVersion = 0x00010001;
2665 mapper_caps.dwFormats = 0xFFFFFFFF;
2666 mapper_caps.wReserved1 = 0;
2667 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2668 WAVECAPS_SAMPLEACCURATE;
2669 mapper_caps.wChannels = 2;
2670 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2672 caps = &mapper_caps;
2673 }else{
2674 if(uDeviceID >= g_outmmdevices_count)
2675 return MMSYSERR_BADDEVICEID;
2677 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2680 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2682 return MMSYSERR_NOERROR;
2685 /**************************************************************************
2686 * waveOutGetErrorTextA [WINMM.@]
2687 * waveInGetErrorTextA [WINMM.@]
2689 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2691 UINT ret;
2693 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2694 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2695 else
2697 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2698 if (!xstr) ret = MMSYSERR_NOMEM;
2699 else
2701 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2702 if (ret == MMSYSERR_NOERROR)
2703 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2704 HeapFree(GetProcessHeap(), 0, xstr);
2707 return ret;
2710 /**************************************************************************
2711 * waveOutGetErrorTextW [WINMM.@]
2712 * waveInGetErrorTextW [WINMM.@]
2714 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2716 UINT ret = MMSYSERR_BADERRNUM;
2718 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2719 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2720 else if (
2721 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2722 * a warning for the test was always true */
2723 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2724 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2725 if (LoadStringW(hWinMM32Instance,
2726 uError, lpText, uSize) > 0) {
2727 ret = MMSYSERR_NOERROR;
2730 return ret;
2733 /**************************************************************************
2734 * waveOutOpen [WINMM.@]
2735 * All the args/structs have the same layout as the win16 equivalents
2737 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2738 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2739 DWORD_PTR dwInstance, DWORD dwFlags)
2741 LRESULT res;
2742 WINMM_OpenInfo info;
2743 WINMM_CBInfo cb_info;
2745 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2746 dwCallback, dwInstance, dwFlags);
2748 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2749 return MMSYSERR_INVALPARAM;
2751 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2752 if(res != MMSYSERR_NOERROR)
2753 return res;
2755 if(!WINMM_StartDevicesThread())
2756 return MMSYSERR_NODRIVER;
2758 info.handle = 0;
2759 info.format = (WAVEFORMATEX*)lpFormat;
2760 info.callback = dwCallback;
2761 info.cb_user = dwInstance;
2762 info.req_device = uDeviceID;
2763 info.flags = dwFlags;
2764 info.reset = TRUE;
2766 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2767 InterlockedDecrement(&g_devthread_token);
2768 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2769 return res;
2771 if(lphWaveOut)
2772 *lphWaveOut = (HWAVEOUT)info.handle;
2774 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2775 cb_info.callback = dwCallback;
2776 cb_info.user = dwInstance;
2777 cb_info.hwave = info.handle;
2779 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2781 return res;
2784 /**************************************************************************
2785 * waveOutClose [WINMM.@]
2787 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2789 UINT res;
2790 WINMM_Device *device;
2791 WINMM_CBInfo cb_info;
2793 TRACE("(%p)\n", hWaveOut);
2795 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2797 if(!WINMM_ValidateAndLock(device))
2798 return MMSYSERR_INVALHANDLE;
2800 cb_info = device->cb_info;
2802 LeaveCriticalSection(&device->lock);
2804 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2806 if(res == MMSYSERR_NOERROR)
2807 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2809 return res;
2812 /**************************************************************************
2813 * waveOutPrepareHeader [WINMM.@]
2815 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2816 WAVEHDR* lpWaveOutHdr, UINT uSize)
2818 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2820 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2821 return MMSYSERR_INVALPARAM;
2823 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2824 return MMSYSERR_NOERROR;
2826 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2829 /**************************************************************************
2830 * waveOutUnprepareHeader [WINMM.@]
2832 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2833 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2835 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2837 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2838 return MMSYSERR_INVALPARAM;
2840 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2841 return WAVERR_STILLPLAYING;
2843 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2844 return MMSYSERR_NOERROR;
2846 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2849 /**************************************************************************
2850 * waveOutWrite [WINMM.@]
2852 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2854 WINMM_Device *device;
2855 MMRESULT mr;
2857 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2859 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2861 if(!WINMM_ValidateAndLock(device))
2862 return MMSYSERR_INVALHANDLE;
2864 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2865 LeaveCriticalSection(&device->lock);
2866 return WAVERR_UNPREPARED;
2869 if(header->dwFlags & WHDR_INQUEUE){
2870 LeaveCriticalSection(&device->lock);
2871 return WAVERR_STILLPLAYING;
2874 TRACE("dwBufferLength: %u\n", header->dwBufferLength);
2876 if(device->acm_handle){
2877 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2879 ash->cbSrcLength = header->dwBufferLength;
2880 mr = acmStreamConvert(device->acm_handle, ash, 0);
2881 if(mr != MMSYSERR_NOERROR){
2882 LeaveCriticalSection(&device->lock);
2883 return mr;
2887 if(device->first){
2888 device->last->lpNext = header;
2889 device->last = header;
2890 if(!device->playing)
2891 device->playing = header;
2892 }else{
2893 device->playing = device->first = device->last = header;
2894 if(header->dwFlags & WHDR_BEGINLOOP){
2895 device->loop_counter = header->dwLoops;
2896 device->loop_start = header;
2900 header->lpNext = NULL;
2901 header->dwFlags &= ~WHDR_DONE;
2902 header->dwFlags |= WHDR_INQUEUE;
2904 mr = WINMM_BeginPlaying(device);
2906 LeaveCriticalSection(&device->lock);
2908 return mr;
2911 /**************************************************************************
2912 * waveOutBreakLoop [WINMM.@]
2914 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2916 WINMM_Device *device;
2918 TRACE("(%p)\n", hWaveOut);
2920 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2922 if(!WINMM_ValidateAndLock(device))
2923 return MMSYSERR_INVALHANDLE;
2925 device->loop_counter = 0;
2927 LeaveCriticalSection(&device->lock);
2929 return MMSYSERR_NOERROR;
2932 /**************************************************************************
2933 * waveOutPause [WINMM.@]
2935 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2937 TRACE("(%p)\n", hWaveOut);
2939 return WINMM_Pause((HWAVE)hWaveOut);
2942 /**************************************************************************
2943 * waveOutReset [WINMM.@]
2945 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2947 TRACE("(%p)\n", hWaveOut);
2949 return WINMM_Reset((HWAVE)hWaveOut);
2952 /**************************************************************************
2953 * waveOutRestart [WINMM.@]
2955 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2957 WINMM_Device *device;
2958 MMRESULT mr;
2960 TRACE("(%p)\n", hWaveOut);
2962 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2964 if(!WINMM_ValidateAndLock(device))
2965 return MMSYSERR_INVALHANDLE;
2967 device->stopped = TRUE;
2969 mr = WINMM_BeginPlaying(device);
2971 LeaveCriticalSection(&device->lock);
2973 return mr;
2976 /**************************************************************************
2977 * waveOutGetPosition [WINMM.@]
2979 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2980 UINT uSize)
2982 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2984 if(!uSize || !lpTime)
2985 return MMSYSERR_INVALPARAM;
2987 if(uSize < sizeof(MMTIME))
2988 return MMSYSERR_ERROR;
2990 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2993 /**************************************************************************
2994 * waveOutGetPitch [WINMM.@]
2996 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2998 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2999 return MMSYSERR_NOTSUPPORTED;
3002 /**************************************************************************
3003 * waveOutSetPitch [WINMM.@]
3005 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3007 TRACE("(%p, %08x)\n", hWaveOut, dw);
3009 return MMSYSERR_NOTSUPPORTED;
3012 /**************************************************************************
3013 * waveOutGetPlaybackRate [WINMM.@]
3015 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3017 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3019 return MMSYSERR_NOTSUPPORTED;
3022 /**************************************************************************
3023 * waveOutSetPlaybackRate [WINMM.@]
3025 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3027 TRACE("(%p, %08x)\n", hWaveOut, dw);
3029 return MMSYSERR_NOTSUPPORTED;
3032 /**************************************************************************
3033 * waveOutGetVolume [WINMM.@]
3035 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3037 WINMM_Device *device;
3038 UINT32 channels;
3039 float *vols;
3040 HRESULT hr;
3042 TRACE("(%p, %p)\n", hWaveOut, out);
3044 if(!out)
3045 return MMSYSERR_INVALPARAM;
3047 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3049 if(!WINMM_ValidateAndLock(device))
3050 return MMSYSERR_INVALHANDLE;
3052 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3053 if(FAILED(hr)){
3054 LeaveCriticalSection(&device->lock);
3055 WARN("GetChannelCount failed: %08x\n", hr);
3056 return MMSYSERR_ERROR;
3059 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3060 if(!vols){
3061 LeaveCriticalSection(&device->lock);
3062 return MMSYSERR_NOMEM;
3065 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3066 if(FAILED(hr)){
3067 LeaveCriticalSection(&device->lock);
3068 HeapFree(GetProcessHeap(), 0, vols);
3069 WARN("GetAllVolumes failed: %08x\n", hr);
3070 return MMSYSERR_ERROR;
3073 LeaveCriticalSection(&device->lock);
3075 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3076 if(channels > 1)
3077 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3079 HeapFree(GetProcessHeap(), 0, vols);
3081 return MMSYSERR_NOERROR;
3084 /**************************************************************************
3085 * waveOutSetVolume [WINMM.@]
3087 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3089 WINMM_Device *device;
3090 UINT32 channels;
3091 float *vols;
3092 HRESULT hr;
3094 TRACE("(%p, %08x)\n", hWaveOut, in);
3096 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3098 if(!WINMM_ValidateAndLock(device))
3099 return MMSYSERR_INVALHANDLE;
3101 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3102 if(FAILED(hr)){
3103 LeaveCriticalSection(&device->lock);
3104 WARN("GetChannelCount failed: %08x\n", hr);
3105 return MMSYSERR_ERROR;
3108 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3109 if(!vols){
3110 LeaveCriticalSection(&device->lock);
3111 return MMSYSERR_NOMEM;
3114 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3115 if(FAILED(hr)){
3116 LeaveCriticalSection(&device->lock);
3117 HeapFree(GetProcessHeap(), 0, vols);
3118 WARN("GetAllVolumes failed: %08x\n", hr);
3119 return MMSYSERR_ERROR;
3122 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
3123 if(channels > 1)
3124 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
3126 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3127 if(FAILED(hr)){
3128 LeaveCriticalSection(&device->lock);
3129 HeapFree(GetProcessHeap(), 0, vols);
3130 WARN("SetAllVolumes failed: %08x\n", hr);
3131 return MMSYSERR_ERROR;
3134 LeaveCriticalSection(&device->lock);
3136 HeapFree(GetProcessHeap(), 0, vols);
3138 return MMSYSERR_NOERROR;
3141 /**************************************************************************
3142 * waveOutGetID [WINMM.@]
3144 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3146 WINMM_Device *device;
3147 UINT dev, junk;
3148 BOOL is_out;
3150 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3152 if(!lpuDeviceID)
3153 return MMSYSERR_INVALPARAM;
3155 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3156 if(!WINMM_ValidateAndLock(device))
3157 return MMSYSERR_INVALHANDLE;
3159 LeaveCriticalSection(&device->lock);
3161 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3163 return MMSYSERR_NOERROR;
3166 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3168 UINT count;
3169 WINMM_MMDevice **devices;
3171 TRACE("(%u, %p, %d)\n", device, len, is_out);
3173 if(is_out){
3174 count = g_outmmdevices_count;
3175 devices = g_out_map;
3176 }else{
3177 count = g_inmmdevices_count;
3178 devices = g_in_map;
3181 if(device >= count)
3182 return MMSYSERR_INVALHANDLE;
3184 EnterCriticalSection(&g_devthread_lock);
3185 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3186 LeaveCriticalSection(&g_devthread_lock);
3188 return MMSYSERR_NOERROR;
3191 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3192 BOOL is_out)
3194 UINT count, id_len;
3195 WINMM_MMDevice **devices;
3197 TRACE("(%u, %p, %d)\n", device, str, is_out);
3199 if(is_out){
3200 count = g_outmmdevices_count;
3201 devices = g_out_map;
3202 }else{
3203 count = g_inmmdevices_count;
3204 devices = g_in_map;
3207 if(device >= count)
3208 return MMSYSERR_INVALHANDLE;
3210 EnterCriticalSection(&g_devthread_lock);
3211 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3212 if(len < id_len){
3213 LeaveCriticalSection(&g_devthread_lock);
3214 return MMSYSERR_ERROR;
3217 memcpy(str, devices[device]->dev_id, id_len);
3218 LeaveCriticalSection(&g_devthread_lock);
3220 return MMSYSERR_NOERROR;
3223 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3225 WINMM_QueryInterfaceInfo info;
3226 UINT ret;
3228 if(!WINMM_StartDevicesThread())
3229 return MMSYSERR_NODRIVER;
3231 info.is_out = is_out;
3232 info.index = index;
3233 info.str = out;
3234 info.len_bytes = out_len;
3236 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3237 InterlockedDecrement(&g_devthread_token);
3238 return ret;
3241 /**************************************************************************
3242 * waveOutMessage [WINMM.@]
3244 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3245 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3247 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3249 switch(uMessage){
3250 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3251 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3252 (DWORD_PTR*)dwParam1, TRUE);
3253 case DRV_QUERYFUNCTIONINSTANCEID:
3254 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3255 case DRV_QUERYDEVICEINTERFACESIZE:
3256 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3257 NULL, (ULONG*)dwParam1);
3258 case DRV_QUERYDEVICEINTERFACE:
3260 ULONG size = dwParam2;
3261 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3262 (WCHAR*)dwParam1, &size);
3264 case DRV_QUERYMAPPABLE:
3265 return MMSYSERR_NOERROR;
3266 case DRVM_MAPPER_PREFERRED_GET:
3267 if(dwParam1) {
3268 if(g_outmmdevices_count > 0)
3269 /* Device 0 is always the default device */
3270 *(DWORD *)dwParam1 = 0;
3271 else
3272 *(DWORD *)dwParam1 = -1;
3275 if(dwParam2)
3276 /* Status flags */
3277 *(DWORD *)dwParam2 = 0;
3279 return MMSYSERR_NOERROR;
3282 TRACE("Message not supported: %u\n", uMessage);
3284 return MMSYSERR_NOTSUPPORTED;
3287 /**************************************************************************
3288 * waveInGetNumDevs [WINMM.@]
3290 UINT WINAPI waveInGetNumDevs(void)
3292 HRESULT hr = WINMM_InitMMDevices();
3293 if(FAILED(hr))
3294 return 0;
3296 TRACE("count: %u\n", g_inmmdevices_count);
3298 return g_inmmdevices_count;
3301 /**************************************************************************
3302 * waveInGetDevCapsW [WINMM.@]
3304 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3306 WAVEINCAPSW mapper_caps, *caps;
3307 HRESULT hr;
3309 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3311 hr = WINMM_InitMMDevices();
3312 if(FAILED(hr))
3313 return MMSYSERR_NODRIVER;
3315 if(!lpCaps)
3316 return MMSYSERR_INVALPARAM;
3318 if(WINMM_IsMapper(uDeviceID)){
3319 /* FIXME: Should be localized */
3320 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
3321 'n','d',' ','M','a','p','p','e','r',0};
3323 mapper_caps.wMid = 0xFF;
3324 mapper_caps.wPid = 0xFF;
3325 mapper_caps.vDriverVersion = 0x00010001;
3326 mapper_caps.dwFormats = 0xFFFFFFFF;
3327 mapper_caps.wReserved1 = 0;
3328 mapper_caps.wChannels = 2;
3329 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
3331 caps = &mapper_caps;
3332 }else{
3333 if(uDeviceID >= g_inmmdevices_count)
3334 return MMSYSERR_BADDEVICEID;
3336 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3339 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3341 return MMSYSERR_NOERROR;
3344 /**************************************************************************
3345 * waveInGetDevCapsA [WINMM.@]
3347 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3349 UINT ret;
3350 WAVEINCAPSW wicW;
3352 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3354 if(!lpCaps)
3355 return MMSYSERR_INVALPARAM;
3357 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3359 if (ret == MMSYSERR_NOERROR) {
3360 WAVEINCAPSA wicA;
3361 wicA.wMid = wicW.wMid;
3362 wicA.wPid = wicW.wPid;
3363 wicA.vDriverVersion = wicW.vDriverVersion;
3364 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3365 sizeof(wicA.szPname), NULL, NULL );
3366 wicA.dwFormats = wicW.dwFormats;
3367 wicA.wChannels = wicW.wChannels;
3368 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3370 return ret;
3373 /**************************************************************************
3374 * waveInOpen [WINMM.@]
3376 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3377 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3378 DWORD_PTR dwInstance, DWORD dwFlags)
3380 LRESULT res;
3381 WINMM_OpenInfo info;
3382 WINMM_CBInfo cb_info;
3384 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3385 dwCallback, dwInstance, dwFlags);
3387 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3388 return MMSYSERR_INVALPARAM;
3390 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3391 if(res != MMSYSERR_NOERROR)
3392 return res;
3394 if(!WINMM_StartDevicesThread())
3395 return MMSYSERR_NODRIVER;
3397 info.handle = 0;
3398 info.format = (WAVEFORMATEX*)lpFormat;
3399 info.callback = dwCallback;
3400 info.cb_user = dwInstance;
3401 info.req_device = uDeviceID;
3402 info.flags = dwFlags;
3403 info.reset = TRUE;
3405 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3406 InterlockedDecrement(&g_devthread_token);
3407 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3408 return res;
3410 if(lphWaveIn)
3411 *lphWaveIn = (HWAVEIN)info.handle;
3413 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3414 cb_info.callback = dwCallback;
3415 cb_info.user = dwInstance;
3416 cb_info.hwave = info.handle;
3418 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3420 return res;
3423 /**************************************************************************
3424 * waveInClose [WINMM.@]
3426 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3428 WINMM_Device *device;
3429 WINMM_CBInfo cb_info;
3430 UINT res;
3432 TRACE("(%p)\n", hWaveIn);
3434 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3436 if(!WINMM_ValidateAndLock(device))
3437 return MMSYSERR_INVALHANDLE;
3439 cb_info = device->cb_info;
3441 LeaveCriticalSection(&device->lock);
3443 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3445 if(res == MMSYSERR_NOERROR)
3446 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3448 return res;
3451 /**************************************************************************
3452 * waveInPrepareHeader [WINMM.@]
3454 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3455 UINT uSize)
3457 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3459 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3460 return MMSYSERR_INVALPARAM;
3462 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3463 return MMSYSERR_NOERROR;
3465 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3468 /**************************************************************************
3469 * waveInUnprepareHeader [WINMM.@]
3471 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3472 UINT uSize)
3474 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3476 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3477 return MMSYSERR_INVALPARAM;
3479 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3480 return WAVERR_STILLPLAYING;
3482 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3483 return MMSYSERR_NOERROR;
3485 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3488 /**************************************************************************
3489 * waveInAddBuffer [WINMM.@]
3491 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3493 WINMM_Device *device;
3495 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3497 if(!header || uSize < sizeof(WAVEHDR))
3498 return MMSYSERR_INVALPARAM;
3500 if(!(header->dwFlags & WHDR_PREPARED))
3501 return WAVERR_UNPREPARED;
3503 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3505 if(!WINMM_ValidateAndLock(device))
3506 return MMSYSERR_INVALHANDLE;
3508 if(!device->first)
3509 device->first = device->last = header;
3510 else{
3511 device->last->lpNext = header;
3512 device->last = header;
3515 header->dwBytesRecorded = 0;
3516 header->lpNext = NULL;
3517 header->dwFlags &= ~WHDR_DONE;
3518 header->dwFlags |= WHDR_INQUEUE;
3520 LeaveCriticalSection(&device->lock);
3522 return MMSYSERR_NOERROR;
3525 /**************************************************************************
3526 * waveInReset [WINMM.@]
3528 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3530 TRACE("(%p)\n", hWaveIn);
3532 return WINMM_Reset((HWAVE)hWaveIn);
3535 /**************************************************************************
3536 * waveInStart [WINMM.@]
3538 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3540 WINMM_Device *device;
3541 MMRESULT mr;
3543 TRACE("(%p)\n", hWaveIn);
3545 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3547 if(!WINMM_ValidateAndLock(device))
3548 return MMSYSERR_INVALHANDLE;
3550 mr = WINMM_BeginPlaying(device);
3552 LeaveCriticalSection(&device->lock);
3554 return mr;
3557 /**************************************************************************
3558 * waveInStop [WINMM.@]
3560 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3562 WINMM_CBInfo cb_info;
3563 WINMM_Device *device;
3564 WAVEHDR *buf;
3565 HRESULT hr;
3567 TRACE("(%p)\n", hWaveIn);
3569 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3571 if(!WINMM_ValidateAndLock(device))
3572 return MMSYSERR_INVALHANDLE;
3574 hr = WINMM_Pause((HWAVE)hWaveIn);
3575 if(FAILED(hr)){
3576 LeaveCriticalSection(&device->lock);
3577 return MMSYSERR_ERROR;
3579 device->stopped = TRUE;
3581 buf = device->first;
3582 if(buf && buf->dwBytesRecorded > 0){
3583 device->first = buf->lpNext;
3584 }else
3585 buf = NULL;
3587 cb_info = device->cb_info;
3589 LeaveCriticalSection(&device->lock);
3591 if(buf){
3592 buf->dwFlags &= ~WHDR_INQUEUE;
3593 buf->dwFlags |= WHDR_DONE;
3594 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3597 return MMSYSERR_NOERROR;
3600 /**************************************************************************
3601 * waveInGetPosition [WINMM.@]
3603 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3604 UINT uSize)
3606 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3608 if(!uSize || !lpTime)
3609 return MMSYSERR_INVALPARAM;
3611 if(uSize < sizeof(MMTIME))
3612 return MMSYSERR_ERROR;
3614 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3617 /**************************************************************************
3618 * waveInGetID [WINMM.@]
3620 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3622 UINT dev, junk;
3623 BOOL is_out;
3624 WINMM_Device *device;
3626 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3628 if(!lpuDeviceID)
3629 return MMSYSERR_INVALPARAM;
3631 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3632 if(!WINMM_ValidateAndLock(device))
3633 return MMSYSERR_INVALHANDLE;
3635 LeaveCriticalSection(&device->lock);
3637 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3639 return MMSYSERR_NOERROR;
3642 /**************************************************************************
3643 * waveInMessage [WINMM.@]
3645 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3646 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3648 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3650 switch(uMessage){
3651 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3652 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3653 (DWORD_PTR*)dwParam1, FALSE);
3654 case DRV_QUERYFUNCTIONINSTANCEID:
3655 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3656 case DRV_QUERYDEVICEINTERFACESIZE:
3657 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3658 NULL, (ULONG*)dwParam1);
3659 case DRV_QUERYDEVICEINTERFACE:
3661 ULONG size = dwParam2;
3662 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3663 (WCHAR*)dwParam1, &size);
3665 case DRV_QUERYMAPPABLE:
3666 return MMSYSERR_NOERROR;
3667 case DRVM_MAPPER_PREFERRED_GET:
3668 if(dwParam1) {
3669 if(g_inmmdevices_count > 0)
3670 /* Device 0 is always the default device */
3671 *(DWORD *)dwParam1 = 0;
3672 else
3673 *(DWORD *)dwParam1 = -1;
3676 if(dwParam2)
3677 /* Status flags */
3678 *(DWORD *)dwParam2 = 0;
3680 return MMSYSERR_NOERROR;
3683 TRACE("Message not supported: %u\n", uMessage);
3685 return MMSYSERR_NOTSUPPORTED;
3688 UINT WINAPI mixerGetNumDevs(void)
3690 HRESULT hr;
3692 TRACE("\n");
3694 hr = WINMM_InitMMDevices();
3695 if(FAILED(hr))
3696 return 0;
3698 return g_outmmdevices_count + g_inmmdevices_count;
3701 /**************************************************************************
3702 * mixerGetDevCapsA [WINMM.@]
3704 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3706 MIXERCAPSW micW;
3707 UINT ret;
3709 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3711 if(!lpCaps)
3712 return MMSYSERR_INVALPARAM;
3714 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3716 if (ret == MMSYSERR_NOERROR) {
3717 MIXERCAPSA micA;
3718 micA.wMid = micW.wMid;
3719 micA.wPid = micW.wPid;
3720 micA.vDriverVersion = micW.vDriverVersion;
3721 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3722 sizeof(micA.szPname), NULL, NULL );
3723 micA.fdwSupport = micW.fdwSupport;
3724 micA.cDestinations = micW.cDestinations;
3725 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3727 return ret;
3730 /**************************************************************************
3731 * mixerGetDevCapsW [WINMM.@]
3733 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3735 WINMM_MMDevice *mmdevice;
3736 MIXERCAPSW caps;
3737 HRESULT hr;
3739 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3741 hr = WINMM_InitMMDevices();
3742 if(FAILED(hr))
3743 return MMSYSERR_NODRIVER;
3745 if(!lpCaps)
3746 return MMSYSERR_INVALPARAM;
3748 if(!uSize)
3749 return MMSYSERR_NOERROR;
3751 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3752 return MMSYSERR_BADDEVICEID;
3754 if(uDeviceID < g_outmmdevices_count){
3755 mmdevice = read_map(g_out_map, uDeviceID);
3756 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3757 }else{
3758 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3759 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3762 caps.wMid = 0xFF;
3763 caps.wPid = 0xFF;
3764 caps.vDriverVersion = 0x00010001;
3765 caps.fdwSupport = 0;
3766 caps.cDestinations = 1;
3768 memcpy(lpCaps, &caps, uSize);
3770 return MMSYSERR_NOERROR;
3773 /**************************************************************************
3774 * mixerOpen [WINMM.@]
3776 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3777 DWORD_PTR dwInstance, DWORD fdwOpen)
3779 WINMM_MMDevice *mmdevice;
3780 MMRESULT mr;
3781 HRESULT hr;
3783 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3784 dwInstance, fdwOpen);
3786 hr = WINMM_InitMMDevices();
3787 if(FAILED(hr))
3788 return MMSYSERR_NODRIVER;
3790 if(!lphMix)
3791 return MMSYSERR_INVALPARAM;
3793 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3794 if(mr != MMSYSERR_NOERROR)
3795 return mr;
3797 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3798 return MMSYSERR_BADDEVICEID;
3800 if(uDeviceID < g_outmmdevices_count){
3801 mmdevice = read_map(g_out_map, uDeviceID);
3802 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3803 mmdevice->mixer_count);
3804 }else{
3805 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3806 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3807 FALSE, mmdevice->mixer_count);
3810 ++mmdevice->mixer_count;
3812 return MMSYSERR_NOERROR;
3815 /**************************************************************************
3816 * mixerClose [WINMM.@]
3818 UINT WINAPI mixerClose(HMIXER hMix)
3820 TRACE("(%p)\n", hMix);
3822 return MMSYSERR_NOERROR;
3825 /**************************************************************************
3826 * mixerGetID [WINMM.@]
3828 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3830 WINMM_MMDevice *mmdevice;
3831 HRESULT hr;
3833 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3835 hr = WINMM_InitMMDevices();
3836 if(FAILED(hr))
3837 return MMSYSERR_NODRIVER;
3839 if(!lpid)
3840 return MMSYSERR_INVALPARAM;
3842 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3843 if(!mmdevice)
3844 return MMSYSERR_INVALHANDLE;
3846 if(mmdevice->in_caps.szPname[0] != '\0')
3847 *lpid += g_outmmdevices_count;
3849 return MMSYSERR_NOERROR;
3852 /**************************************************************************
3853 * mixerGetControlDetailsW [WINMM.@]
3855 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3856 DWORD fdwDetails)
3858 WINMM_ControlDetails details;
3860 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3862 if(!WINMM_StartDevicesThread())
3863 return MMSYSERR_NODRIVER;
3865 if(!lpmcdW || !lpmcdW->paDetails)
3866 return MMSYSERR_INVALPARAM;
3868 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3870 details.hmix = hmix;
3871 details.details = lpmcdW;
3872 details.flags = fdwDetails;
3874 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3875 (DWORD_PTR)&details, 0);
3878 /**************************************************************************
3879 * mixerGetControlDetailsA [WINMM.@]
3881 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3882 DWORD fdwDetails)
3884 UINT ret = MMSYSERR_NOTSUPPORTED;
3886 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3888 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3889 return MMSYSERR_INVALPARAM;
3891 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3892 case MIXER_GETCONTROLDETAILSF_VALUE:
3893 /* can safely use A structure as it is, no string inside */
3894 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3895 break;
3896 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3898 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3899 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3900 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3901 unsigned int i;
3903 if (lpmcdA->u.cMultipleItems != 0) {
3904 size *= lpmcdA->u.cMultipleItems;
3906 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3907 lpmcdA->paDetails = pDetailsW;
3908 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3909 /* set up lpmcd->paDetails */
3910 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3911 /* copy from lpmcd->paDetails back to paDetailsW; */
3912 if (ret == MMSYSERR_NOERROR) {
3913 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3914 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3915 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3916 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3917 pDetailsA->szName,
3918 sizeof(pDetailsA->szName), NULL, NULL );
3919 pDetailsA++;
3920 pDetailsW++;
3922 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3923 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3925 HeapFree(GetProcessHeap(), 0, pDetailsW);
3926 lpmcdA->paDetails = pDetailsA;
3927 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3929 break;
3930 default:
3931 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3934 return ret;
3937 /**************************************************************************
3938 * mixerGetLineControlsA [WINMM.@]
3940 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3941 DWORD fdwControls)
3943 MIXERLINECONTROLSW mlcW;
3944 DWORD ret;
3945 unsigned int i;
3947 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3949 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3950 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3951 return MMSYSERR_INVALPARAM;
3953 mlcW.cbStruct = sizeof(mlcW);
3954 mlcW.dwLineID = lpmlcA->dwLineID;
3955 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3956 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3958 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3959 the control count is assumed to be 1 - This is relied upon by a game,
3960 "Dynomite Deluze" */
3961 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3962 mlcW.cControls = 1;
3963 } else {
3964 mlcW.cControls = lpmlcA->cControls;
3966 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3967 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3968 mlcW.cControls * mlcW.cbmxctrl);
3970 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3972 if (ret == MMSYSERR_NOERROR) {
3973 lpmlcA->dwLineID = mlcW.dwLineID;
3974 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3975 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3977 for (i = 0; i < mlcW.cControls; i++) {
3978 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3979 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3980 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3981 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3982 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3983 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3984 lpmlcA->pamxctrl[i].szShortName,
3985 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3986 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3987 lpmlcA->pamxctrl[i].szName,
3988 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3989 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3990 * sizeof(mlcW.pamxctrl[i].Bounds) */
3991 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3992 sizeof(mlcW.pamxctrl[i].Bounds));
3993 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3994 * sizeof(mlcW.pamxctrl[i].Metrics) */
3995 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3996 sizeof(mlcW.pamxctrl[i].Metrics));
4000 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
4002 return ret;
4005 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4006 MIXERCONTROLW *ctl, DWORD flags)
4008 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4009 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4010 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4011 ctl->cMultipleItems = 0;
4012 lstrcpyW(ctl->szShortName, volumeW);
4013 lstrcpyW(ctl->szName, volumeW);
4014 ctl->Bounds.s1.dwMinimum = 0;
4015 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4016 ctl->Metrics.cSteps = 192;
4018 return MMSYSERR_NOERROR;
4021 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4022 MIXERCONTROLW *ctl, DWORD flags)
4024 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4025 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4026 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4027 ctl->cMultipleItems = 0;
4028 lstrcpyW(ctl->szShortName, muteW);
4029 lstrcpyW(ctl->szName, muteW);
4030 ctl->Bounds.s1.dwMinimum = 0;
4031 ctl->Bounds.s1.dwMaximum = 1;
4032 ctl->Metrics.cSteps = 0;
4034 return MMSYSERR_NOERROR;
4037 /**************************************************************************
4038 * mixerGetLineControlsW [WINMM.@]
4040 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4041 DWORD fdwControls)
4043 WINMM_MMDevice *mmdevice;
4044 HRESULT hr;
4046 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4048 hr = WINMM_InitMMDevices();
4049 if(FAILED(hr))
4050 return MMSYSERR_NODRIVER;
4052 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4053 MIXER_GETLINECONTROLSF_ONEBYID |
4054 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4055 MIXER_OBJECTF_HMIXER |
4056 MIXER_OBJECTF_MIXER)){
4057 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4058 return MMSYSERR_INVALFLAG;
4061 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4062 return MMSYSERR_INVALPARAM;
4064 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4065 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4066 TRACE("cControls: %u\n", lpmlcW->cControls);
4068 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4069 if(!mmdevice)
4070 return MMSYSERR_INVALHANDLE;
4072 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4073 case MIXER_GETLINECONTROLSF_ALL:
4074 if(lpmlcW->cControls != 2)
4075 return MMSYSERR_INVALPARAM;
4076 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4077 return MMSYSERR_INVALPARAM;
4078 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4079 return MIXERR_INVALLINE;
4080 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4081 &lpmlcW->pamxctrl[0], fdwControls);
4082 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4083 &lpmlcW->pamxctrl[1], fdwControls);
4084 return MMSYSERR_NOERROR;
4085 case MIXER_GETLINECONTROLSF_ONEBYID:
4086 if(lpmlcW->cControls != 1)
4087 return MMSYSERR_INVALPARAM;
4088 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4089 return MMSYSERR_INVALPARAM;
4090 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4091 return MIXERR_INVALLINE;
4092 if(lpmlcW->u.dwControlID == 0)
4093 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4094 lpmlcW->pamxctrl, fdwControls);
4095 if(lpmlcW->u.dwControlID == 1)
4096 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4097 lpmlcW->pamxctrl, fdwControls);
4098 return MMSYSERR_NOTSUPPORTED;
4099 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4100 if(lpmlcW->cControls != 1)
4101 return MMSYSERR_INVALPARAM;
4102 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4103 return MMSYSERR_INVALPARAM;
4104 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4105 return MIXERR_INVALLINE;
4106 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4107 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4108 lpmlcW->pamxctrl, fdwControls);
4109 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4110 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4111 lpmlcW->pamxctrl, fdwControls);
4112 return MMSYSERR_NOTSUPPORTED;
4115 return MMSYSERR_NOTSUPPORTED;
4118 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4119 MIXERLINEW *info, DWORD flags)
4121 BOOL is_out = TRUE;
4122 if(mmdevice->in_caps.szPname[0] != '\0')
4123 is_out = FALSE;
4125 if(info->dwSource != 0)
4126 return MIXERR_INVALLINE;
4128 info->dwDestination = 0;
4129 info->dwLineID = 0;
4130 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4131 info->cConnections = 0;
4132 info->cControls = 2;
4133 /* volume & mute always affect all channels, so claim 1 channel */
4134 info->cChannels = 1;
4135 info->Target.dwDeviceID = mmdev_index;
4136 info->Target.wMid = ~0;
4137 info->Target.wPid = ~0;
4138 info->Target.vDriverVersion = 0;
4140 lstrcpyW(info->szShortName, volumeW);
4141 lstrcpyW(info->szName, mastervolumeW);
4143 if(is_out){
4144 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4145 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4146 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4147 sizeof(info->Target.szPname));
4148 }else{
4149 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4150 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4151 info->Target.szPname[0] = '\0';
4154 return MMSYSERR_NOERROR;
4157 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4158 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4160 BOOL is_out = TRUE;
4161 if(mmdevice->in_caps.szPname[0] != '\0')
4162 is_out = FALSE;
4164 if(info->dwDestination != 0)
4165 return MIXERR_INVALLINE;
4167 info->dwSource = 0xFFFFFFFF;
4168 info->dwLineID = 0xFFFF0000;
4169 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4170 info->cConnections = 1;
4171 info->cControls = 2;
4173 lstrcpyW(info->szShortName, volumeW);
4174 lstrcpyW(info->szName, mastervolumeW);
4176 info->Target.dwDeviceID = mmdev_index;
4177 info->Target.wMid = ~0;
4178 info->Target.wPid = ~0;
4179 info->Target.vDriverVersion = 0;
4181 if(is_out){
4182 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4183 info->cChannels = mmdevice->out_caps.wChannels;
4184 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4185 info->Target.szPname[0] = '\0';
4186 }else{
4187 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4188 info->cChannels = mmdevice->in_caps.wChannels;
4189 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4190 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4191 sizeof(info->Target.szPname));
4194 return MMSYSERR_NOERROR;
4197 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4198 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4200 BOOL is_out = TRUE;
4201 if(mmdevice->in_caps.szPname[0] != '\0')
4202 is_out = FALSE;
4204 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
4205 if(is_out)
4206 return MIXERR_INVALLINE;
4207 info->dwDestination = 0;
4208 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4211 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
4212 if(!is_out)
4213 return MIXERR_INVALLINE;
4214 info->dwDestination = 0;
4215 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4218 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4219 if(is_out)
4220 return MIXERR_INVALLINE;
4221 info->dwSource = 0;
4222 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4225 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4226 if(!is_out)
4227 return MIXERR_INVALLINE;
4228 info->dwSource = 0;
4229 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4232 TRACE("Returning INVALLINE on this component type: %u\n",
4233 info->dwComponentType);
4235 return MIXERR_INVALLINE;
4238 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4239 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4241 if(info->dwLineID == 0xFFFF0000){
4242 info->dwDestination = 0;
4243 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4246 if(info->dwLineID == 0){
4247 info->dwSource = 0;
4248 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4251 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4252 return MIXERR_INVALLINE;
4255 /**************************************************************************
4256 * mixerGetLineInfoW [WINMM.@]
4258 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4260 UINT mmdev_index;
4261 WINMM_MMDevice *mmdevice;
4262 HRESULT hr;
4264 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4266 hr = WINMM_InitMMDevices();
4267 if(FAILED(hr))
4268 return MMSYSERR_NODRIVER;
4270 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4271 return MMSYSERR_INVALPARAM;
4273 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4274 TRACE("dwSource: %u\n", lpmliW->dwSource);
4275 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4276 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4277 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4279 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4280 MIXER_GETLINEINFOF_DESTINATION |
4281 MIXER_GETLINEINFOF_LINEID |
4282 MIXER_GETLINEINFOF_SOURCE |
4283 MIXER_GETLINEINFOF_TARGETTYPE |
4284 MIXER_OBJECTF_HMIXER |
4285 MIXER_OBJECTF_MIXER)){
4286 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4287 return MMSYSERR_INVALFLAG;
4290 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4291 if(!mmdevice)
4292 return MMSYSERR_INVALHANDLE;
4294 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4295 case MIXER_GETLINEINFOF_DESTINATION:
4296 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4297 fdwInfo);
4298 case MIXER_GETLINEINFOF_SOURCE:
4299 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4300 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4301 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4302 fdwInfo);
4303 case MIXER_GETLINEINFOF_LINEID:
4304 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4305 case MIXER_GETLINEINFOF_TARGETTYPE:
4306 FIXME("TARGETTYPE flag not implemented!\n");
4307 return MIXERR_INVALLINE;
4310 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
4311 return MMSYSERR_INVALFLAG;
4314 /**************************************************************************
4315 * mixerGetLineInfoA [WINMM.@]
4317 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4318 DWORD fdwInfo)
4320 MIXERLINEW mliW;
4321 UINT ret;
4323 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4325 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4326 return MMSYSERR_INVALPARAM;
4328 mliW.cbStruct = sizeof(mliW);
4329 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4330 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4331 mliW.dwComponentType = lpmliA->dwComponentType;
4332 break;
4333 case MIXER_GETLINEINFOF_DESTINATION:
4334 mliW.dwDestination = lpmliA->dwDestination;
4335 break;
4336 case MIXER_GETLINEINFOF_LINEID:
4337 mliW.dwLineID = lpmliA->dwLineID;
4338 break;
4339 case MIXER_GETLINEINFOF_SOURCE:
4340 mliW.dwDestination = lpmliA->dwDestination;
4341 mliW.dwSource = lpmliA->dwSource;
4342 break;
4343 case MIXER_GETLINEINFOF_TARGETTYPE:
4344 mliW.Target.dwType = lpmliA->Target.dwType;
4345 mliW.Target.wMid = lpmliA->Target.wMid;
4346 mliW.Target.wPid = lpmliA->Target.wPid;
4347 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4348 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
4349 break;
4350 default:
4351 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4352 return MMSYSERR_INVALFLAG;
4355 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4357 if(ret == MMSYSERR_NOERROR)
4359 lpmliA->dwDestination = mliW.dwDestination;
4360 lpmliA->dwSource = mliW.dwSource;
4361 lpmliA->dwLineID = mliW.dwLineID;
4362 lpmliA->fdwLine = mliW.fdwLine;
4363 lpmliA->dwUser = mliW.dwUser;
4364 lpmliA->dwComponentType = mliW.dwComponentType;
4365 lpmliA->cChannels = mliW.cChannels;
4366 lpmliA->cConnections = mliW.cConnections;
4367 lpmliA->cControls = mliW.cControls;
4368 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4369 sizeof(lpmliA->szShortName), NULL, NULL);
4370 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4371 sizeof(lpmliA->szName), NULL, NULL );
4372 lpmliA->Target.dwType = mliW.Target.dwType;
4373 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4374 lpmliA->Target.wMid = mliW.Target.wMid;
4375 lpmliA->Target.wPid = mliW.Target.wPid;
4376 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4377 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4378 sizeof(lpmliA->Target.szPname), NULL, NULL );
4380 return ret;
4383 /**************************************************************************
4384 * mixerSetControlDetails [WINMM.@]
4386 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4387 DWORD fdwDetails)
4389 WINMM_ControlDetails details;
4390 UINT ret;
4392 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4394 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4395 MIXER_SETCONTROLDETAILSF_CUSTOM)
4396 return MMSYSERR_NOTSUPPORTED;
4398 if(!lpmcd)
4399 return MMSYSERR_INVALPARAM;
4401 if(!WINMM_StartDevicesThread())
4402 return MMSYSERR_NODRIVER;
4404 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4406 details.hmix = hmix;
4407 details.details = lpmcd;
4408 details.flags = fdwDetails;
4410 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4411 (DWORD_PTR)&details, 0);
4412 InterlockedDecrement(&g_devthread_token);
4413 return ret;
4416 /**************************************************************************
4417 * mixerMessage [WINMM.@]
4419 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4421 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4423 return MMSYSERR_NOTSUPPORTED;