dpvoice: Turn GetCompressionTypes into a semi-stub.
[wine.git] / dlls / winmm / waveform.c
blobba74e028020a7bcad7d475f306b915fc8d3aecfb
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;
125 EDataFlow dataflow;
127 ISimpleAudioVolume *volume;
129 GUID session;
131 UINT index;
133 /* HMIXER format is the same as the HWAVE format, but the I bits are
134 * replaced by the value of this counter, to keep each HMIXER unique */
135 UINT mixer_count;
137 CRITICAL_SECTION lock;
139 WINMM_Device *devices[MAX_DEVICES];
142 static WINMM_MMDevice *g_out_mmdevices;
143 static WINMM_MMDevice **g_out_map;
144 static UINT g_outmmdevices_count;
145 static WINMM_Device *g_out_mapper_devices[MAX_DEVICES];
147 static WINMM_MMDevice *g_in_mmdevices;
148 static WINMM_MMDevice **g_in_map;
149 static UINT g_inmmdevices_count;
150 static WINMM_Device *g_in_mapper_devices[MAX_DEVICES];
152 static IMMDeviceEnumerator *g_devenum;
154 static CRITICAL_SECTION g_devthread_lock;
155 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug =
157 0, 0, &g_devthread_lock,
158 { &g_devthread_lock_debug.ProcessLocksList, &g_devthread_lock_debug.ProcessLocksList },
159 0, 0, { (DWORD_PTR)(__FILE__ ": g_devthread_lock") }
161 static CRITICAL_SECTION g_devthread_lock = { &g_devthread_lock_debug, -1, 0, 0, 0, 0 };
162 static LONG g_devthread_token;
163 static HANDLE g_devices_thread;
164 static HWND g_devices_hwnd;
165 static HMODULE g_devthread_module;
167 static UINT g_devhandle_count;
168 static HANDLE *g_device_handles;
169 static WINMM_Device **g_handle_devices;
171 typedef struct _WINMM_OpenInfo {
172 HWAVE handle;
173 UINT req_device;
174 WAVEFORMATEX *format;
175 DWORD_PTR callback;
176 DWORD_PTR cb_user;
177 DWORD flags;
178 BOOL reset;
179 } WINMM_OpenInfo;
181 typedef struct _WINMM_ControlDetails {
182 HMIXEROBJ hmix;
183 MIXERCONTROLDETAILS *details;
184 DWORD flags;
185 } WINMM_ControlDetails;
187 typedef struct _WINMM_QueryInterfaceInfo {
188 BOOL is_out;
189 UINT index;
190 WCHAR *str;
191 UINT *len_bytes;
192 } WINMM_QueryInterfaceInfo;
194 static LRESULT WOD_Open(WINMM_OpenInfo *info);
195 static LRESULT WOD_Close(HWAVEOUT hwave);
196 static LRESULT WID_Open(WINMM_OpenInfo *info);
197 static LRESULT WID_Close(HWAVEIN hwave);
198 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device);
200 void WINMM_DeleteWaveform(void)
202 UINT i, j;
204 if(g_devices_thread)
205 CloseHandle(g_devices_thread);
207 for(i = 0; i < g_outmmdevices_count; ++i){
208 WINMM_MMDevice *mmdevice = &g_out_mmdevices[i];
210 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
211 WINMM_Device *device = mmdevice->devices[j];
212 if(device->handle)
213 CloseHandle(device->handle);
214 DeleteCriticalSection(&device->lock);
217 if(mmdevice->volume)
218 ISimpleAudioVolume_Release(mmdevice->volume);
219 CoTaskMemFree(mmdevice->dev_id);
220 DeleteCriticalSection(&mmdevice->lock);
223 for(i = 0; i < g_inmmdevices_count; ++i){
224 WINMM_MMDevice *mmdevice = &g_in_mmdevices[i];
226 for(j = 0; j < MAX_DEVICES && mmdevice->devices[j]; ++j){
227 WINMM_Device *device = mmdevice->devices[j];
228 if(device->handle)
229 CloseHandle(device->handle);
230 DeleteCriticalSection(&device->lock);
233 if(mmdevice->volume)
234 ISimpleAudioVolume_Release(mmdevice->volume);
235 CoTaskMemFree(mmdevice->dev_id);
236 DeleteCriticalSection(&mmdevice->lock);
239 HeapFree(GetProcessHeap(), 0, g_out_mmdevices);
240 HeapFree(GetProcessHeap(), 0, g_in_mmdevices);
242 HeapFree(GetProcessHeap(), 0, g_device_handles);
243 HeapFree(GetProcessHeap(), 0, g_handle_devices);
245 DeleteCriticalSection(&g_devthread_lock);
248 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
250 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
251 (mmdevice << 8) | device);
254 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
255 BOOL *is_out, UINT *device_index, UINT *junk)
257 ULONG32 l = HandleToULong(hwave);
258 *device_index = l & 0xFF;
259 *mmdevice_index = (l >> 8) & 0x3F;
260 *is_out = (l >> 14) & 0x1;
261 *junk = l >> 15;
264 static void WINMM_InitDevice(WINMM_Device *device)
266 InitializeCriticalSection(&device->lock);
267 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
270 static inline WINMM_MMDevice *read_map(WINMM_MMDevice **map, UINT index)
272 WINMM_MMDevice *ret;
273 EnterCriticalSection(&g_devthread_lock);
274 ret = map[index];
275 LeaveCriticalSection(&g_devthread_lock);
276 return ret;
279 /* finds the first unused Device, marks it as "open", and returns
280 * a pointer to the device
282 * IMPORTANT: it is the caller's responsibility to release the device's lock
283 * on success
285 static WINMM_Device *WINMM_FindUnusedDevice(WINMM_Device **devices,
286 WINMM_MMDevice *parent, UINT internal_index, BOOL is_out)
288 UINT i;
290 for(i = 0; i < MAX_DEVICES; ++i){
291 WINMM_Device *device = devices[i];
293 if(!device){
294 device = devices[i] = HeapAlloc(GetProcessHeap(),
295 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
296 if(!device)
297 return NULL;
299 WINMM_InitDevice(device);
300 EnterCriticalSection(&device->lock);
301 }else
302 EnterCriticalSection(&device->lock);
304 if(!device->open){
305 device->handle = WINMM_MakeHWAVE(internal_index, is_out, i);
306 device->parent = parent;
307 device->open = TRUE;
309 return device;
312 LeaveCriticalSection(&device->lock);
315 TRACE("All devices in use: mmdevice: %u\n", internal_index);
317 return NULL;
320 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
322 if(!device)
323 return FALSE;
325 EnterCriticalSection(&device->lock);
327 if(!device->open){
328 LeaveCriticalSection(&device->lock);
329 return FALSE;
332 return TRUE;
335 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
337 WINMM_MMDevice *mmdevice;
338 WINMM_Device *device;
339 UINT mmdevice_index, device_index, junk;
340 BOOL is_out;
342 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
344 if(junk != 0x1)
345 return NULL;
347 if(mmdevice_index == MAPPER_INDEX){
348 EnterCriticalSection(&g_devthread_lock);
349 if(is_out)
350 device = g_out_mapper_devices[device_index];
351 else
352 device = g_in_mapper_devices[device_index];
353 LeaveCriticalSection(&g_devthread_lock);
354 return device;
357 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
358 return NULL;
360 if(is_out)
361 mmdevice = &g_out_mmdevices[mmdevice_index];
362 else
363 mmdevice = &g_in_mmdevices[mmdevice_index];
365 EnterCriticalSection(&mmdevice->lock);
367 device = mmdevice->devices[device_index];
369 LeaveCriticalSection(&mmdevice->lock);
371 return device;
374 /* Note: NotifyClient should never be called while holding the device lock
375 * since the client may call wave* functions from within the callback. */
376 static inline void WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
377 DWORD_PTR param2)
379 DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
380 msg, info->user, param1, param2);
383 static MMRESULT hr2mmr(HRESULT hr)
385 switch(hr){
386 case S_OK:
387 case AUDCLNT_E_NOT_STOPPED:
388 return MMSYSERR_NOERROR;
389 case AUDCLNT_E_UNSUPPORTED_FORMAT:
390 return WAVERR_BADFORMAT;
391 case AUDCLNT_E_DEVICE_IN_USE:
392 return MMSYSERR_ALLOCATED;
393 case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
394 return MMSYSERR_NOTENABLED;
395 case E_OUTOFMEMORY:
396 return MMSYSERR_NOMEM;
397 case E_POINTER:
398 case E_INVALIDARG:
399 return MMSYSERR_INVALPARAM;
400 case AUDCLNT_E_DEVICE_INVALIDATED: /* DSERR_BUFFERLOST */
401 default:
402 return FAILED(hr) ? MMSYSERR_ERROR : MMSYSERR_NOERROR;
406 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
407 UINT outlen)
409 IPropertyStore *ps;
410 PROPVARIANT var;
411 HRESULT hr;
413 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
414 if(FAILED(hr))
415 return hr;
417 PropVariantInit(&var);
419 hr = IPropertyStore_GetValue(ps,
420 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
421 if(FAILED(hr)){
422 IPropertyStore_Release(ps);
423 return hr;
426 lstrcpynW(out, var.u.pwszVal, outlen);
428 PropVariantClear(&var);
430 IPropertyStore_Release(ps);
432 return S_OK;
435 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
436 WORD channels)
438 WAVEFORMATEX fmt, *junk;
439 HRESULT hr;
441 fmt.wFormatTag = WAVE_FORMAT_PCM;
442 fmt.nChannels = channels;
443 fmt.nSamplesPerSec = rate;
444 fmt.wBitsPerSample = depth;
445 fmt.nBlockAlign = (channels * depth) / 8;
446 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
447 fmt.cbSize = 0;
449 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
450 &fmt, &junk);
451 if(SUCCEEDED(hr))
452 CoTaskMemFree(junk);
454 return hr;
457 static struct _TestFormat {
458 DWORD flag;
459 DWORD rate;
460 DWORD depth;
461 WORD channels;
462 } formats_to_test[] = {
463 { WAVE_FORMAT_1M08, 11025, 8, 1 },
464 { WAVE_FORMAT_1M16, 11025, 16, 1 },
465 { WAVE_FORMAT_1S08, 11025, 8, 2 },
466 { WAVE_FORMAT_1S16, 11025, 16, 2 },
467 { WAVE_FORMAT_2M08, 22050, 8, 1 },
468 { WAVE_FORMAT_2M16, 22050, 16, 1 },
469 { WAVE_FORMAT_2S08, 22050, 8, 2 },
470 { WAVE_FORMAT_2S16, 22050, 16, 2 },
471 { WAVE_FORMAT_4M08, 44100, 8, 1 },
472 { WAVE_FORMAT_4M16, 44100, 16, 1 },
473 { WAVE_FORMAT_4S08, 44100, 8, 2 },
474 { WAVE_FORMAT_4S16, 44100, 16, 2 },
475 { WAVE_FORMAT_48M08, 48000, 8, 1 },
476 { WAVE_FORMAT_48M16, 48000, 16, 1 },
477 { WAVE_FORMAT_48S08, 48000, 8, 2 },
478 { WAVE_FORMAT_48S16, 48000, 16, 2 },
479 { WAVE_FORMAT_96M08, 96000, 8, 1 },
480 { WAVE_FORMAT_96M16, 96000, 16, 1 },
481 { WAVE_FORMAT_96S08, 96000, 8, 2 },
482 { WAVE_FORMAT_96S16, 96000, 16, 2 },
486 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
488 DWORD flags = 0;
489 HRESULT hr;
490 struct _TestFormat *fmt;
491 IAudioClient *client;
493 hr = IMMDevice_Activate(device, &IID_IAudioClient,
494 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
495 if(FAILED(hr))
496 return 0;
498 for(fmt = formats_to_test; fmt->flag; ++fmt){
499 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
500 if(hr == S_OK)
501 flags |= fmt->flag;
504 IAudioClient_Release(client);
506 return flags;
509 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
510 WINMM_MMDevice *dev, UINT index)
512 HRESULT hr;
514 dev->dataflow = flow;
515 if(flow == eRender){
516 dev->out_caps.wMid = 0xFF;
517 dev->out_caps.wPid = 0xFF;
518 dev->out_caps.vDriverVersion = 0x00010001;
519 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
520 dev->out_caps.wReserved1 = 0;
521 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
522 WAVECAPS_SAMPLEACCURATE;
523 dev->out_caps.wChannels = 2;
524 dev->out_caps.szPname[0] = '\0';
526 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
527 sizeof(dev->out_caps.szPname) /
528 sizeof(*dev->out_caps.szPname));
529 if(FAILED(hr))
530 return hr;
531 }else{
532 dev->in_caps.wMid = 0xFF;
533 dev->in_caps.wPid = 0xFF;
534 dev->in_caps.vDriverVersion = 0x00010001;
535 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
536 dev->in_caps.wReserved1 = 0;
537 dev->in_caps.wChannels = 2;
538 dev->in_caps.szPname[0] = '\0';
540 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
541 sizeof(dev->in_caps.szPname) /
542 sizeof(*dev->in_caps.szPname));
543 if(FAILED(hr))
544 return hr;
547 hr = IMMDevice_GetId(device, &dev->dev_id);
548 if(FAILED(hr))
549 return hr;
551 CoCreateGuid(&dev->session);
553 dev->index = index;
555 InitializeCriticalSection(&dev->lock);
556 dev->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_Device.lock");
558 return S_OK;
561 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices,
562 WINMM_MMDevice ***map, UINT *devcount, EDataFlow flow,
563 IMMDeviceEnumerator *devenum)
565 IMMDeviceCollection *devcoll;
566 HRESULT hr;
568 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
569 DEVICE_STATE_ACTIVE, &devcoll);
570 if(FAILED(hr))
571 return hr;
573 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
574 if(FAILED(hr)){
575 IMMDeviceCollection_Release(devcoll);
576 return hr;
579 if(*devcount > 0){
580 UINT n, count = 1;
581 IMMDevice *def_dev = NULL;
583 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
584 sizeof(WINMM_MMDevice) * (*devcount));
585 if(!*devices){
586 IMMDeviceCollection_Release(devcoll);
587 return E_OUTOFMEMORY;
590 *map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
591 sizeof(WINMM_MMDevice *) * (*devcount));
592 if(!*map){
593 IMMDeviceCollection_Release(devcoll);
594 HeapFree(GetProcessHeap(), 0, *devices);
595 return E_OUTOFMEMORY;
598 /* make sure that device 0 is the default device */
599 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
600 flow, eConsole, &def_dev);
602 for(n = 0; n < *devcount; ++n){
603 IMMDevice *device;
605 hr = IMMDeviceCollection_Item(devcoll, n, &device);
606 if(SUCCEEDED(hr)){
607 WINMM_InitMMDevice(flow, device, &(*devices)[n], n);
609 if(device == def_dev)
610 (*map)[0] = &(*devices)[n];
611 else{
612 (*map)[count] = &(*devices)[n];
613 ++count;
616 IMMDevice_Release(device);
620 IMMDevice_Release(def_dev);
622 *devcount = count;
625 IMMDeviceCollection_Release(devcoll);
627 return S_OK;
630 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
631 const GUID *riid, void **obj)
633 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid));
634 return E_NOINTERFACE;
637 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
639 return 2;
642 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
644 return 1;
647 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
648 const WCHAR *device_id, DWORD new_state)
650 TRACE("Ignoring OnDeviceStateChanged callback\n");
651 return S_OK;
654 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
655 const WCHAR *device_id)
657 TRACE("Ignoring OnDeviceAdded callback\n");
658 return S_OK;
661 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
662 const WCHAR *device_id)
664 TRACE("Ignoring OnDeviceRemoved callback\n");
665 return S_OK;
668 static HRESULT update_mapping(WINMM_MMDevice ***map, UINT count,
669 const WCHAR *default_id)
671 WINMM_MMDevice *prev;
672 UINT i;
674 prev = (*map)[0];
675 for(i = 0; i < count; ++i){
676 WINMM_MMDevice *tmp;
678 if(!lstrcmpW((*map)[i]->dev_id, default_id)){
679 (*map)[0] = (*map)[i];
680 (*map)[i] = prev;
682 return S_OK;
685 tmp = (*map)[i];
686 (*map)[i] = prev;
687 prev = tmp;
690 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
691 (*map)[0] = prev;
693 return S_FALSE;
696 static HRESULT reroute_mapper_device(WINMM_Device *device, BOOL is_out)
698 WINMM_OpenInfo info;
699 BOOL stopped;
700 MMRESULT mr;
701 HRESULT hr;
702 UINT64 clock_freq, clock_pos;
704 TRACE("rerouting device %p\n", device->handle);
706 EnterCriticalSection(&device->lock);
708 if(!device->open || device->acm_handle){
709 /* Windows 7 doesn't re-route ACM devices, so we don't either.
710 * Seems to be because of the data waveXxxPrepareHeader allocates. */
711 LeaveCriticalSection(&device->lock);
712 return S_FALSE;
715 stopped = device->stopped;
717 info.handle = 0;
718 info.req_device = WAVE_MAPPER;
719 info.format = device->orig_fmt;
720 info.callback = device->cb_info.callback;
721 info.cb_user = device->cb_info.user;
722 /* We have to use direct here so that we don't suddenly introduce ACM
723 * into a playing stream that hasn't been Prepared for it */
724 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT_QUERY;
725 info.reset = FALSE;
727 if(is_out)
728 mr = WOD_Open(&info);
729 else
730 mr = WID_Open(&info);
732 if(mr != MMSYSERR_NOERROR){
733 TRACE("New default device doesn't support this stream: %p\n", device->handle);
734 LeaveCriticalSection(&device->lock);
735 return S_FALSE;
738 hr = IAudioClient_Stop(device->client);
739 if(FAILED(hr))
740 WARN("Stop failed: %08x\n", hr);
742 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
743 if(FAILED(hr)){
744 WARN("GetFrequency failed: %08x\n", hr);
745 LeaveCriticalSection(&device->lock);
746 return hr;
749 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
750 if(FAILED(hr)){
751 WARN("GetPosition failed: %08x\n", hr);
752 LeaveCriticalSection(&device->lock);
753 return hr;
756 device->remainder_frames = MulDiv(clock_pos, device->samples_per_sec, clock_freq) - device->last_clock_pos;
758 info.handle = device->handle;
759 info.flags = (device->cb_info.flags << 16) | WAVE_FORMAT_DIRECT;
761 if(is_out){
762 WOD_Close((HWAVEOUT)device->handle);
763 device->parent = read_map(g_out_map, 0);
764 mr = WOD_Open(&info);
765 }else{
766 WID_Close((HWAVEIN)device->handle);
767 device->parent = read_map(g_in_map, 0);
768 mr = WID_Open(&info);
771 if(mr != MMSYSERR_NOERROR){
772 ERR("Opening new default device failed! %u\n", mr);
773 LeaveCriticalSection(&device->lock);
774 return E_FAIL;
777 HeapFree(GetProcessHeap(), 0, info.format);
779 if(!stopped)
780 WINMM_BeginPlaying(device);
782 LeaveCriticalSection(&device->lock);
784 return S_OK;
787 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
788 EDataFlow flow, ERole role, const WCHAR *device_id)
790 UINT i;
792 TRACE("%u %u %s\n", flow, role, wine_dbgstr_w(device_id));
794 if(role != eConsole)
795 return S_OK;
797 EnterCriticalSection(&g_devthread_lock);
799 if(flow == eRender)
800 update_mapping(&g_out_map, g_outmmdevices_count, device_id);
801 else
802 update_mapping(&g_in_map, g_inmmdevices_count, device_id);
804 for(i = 0; i < MAX_DEVICES && g_out_mapper_devices[i]; ++i)
805 reroute_mapper_device(g_out_mapper_devices[i], TRUE);
807 for(i = 0; i < MAX_DEVICES && g_in_mapper_devices[i]; ++i)
808 reroute_mapper_device(g_in_mapper_devices[i], FALSE);
810 LeaveCriticalSection(&g_devthread_lock);
812 return S_OK;
815 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
816 const WCHAR *device_id, const PROPERTYKEY key)
818 TRACE("Ignoring OnPropertyValueChanged callback\n");
819 return S_OK;
822 static IMMNotificationClientVtbl g_notif_vtbl = {
823 notif_QueryInterface,
824 notif_AddRef,
825 notif_Release,
826 notif_OnDeviceStateChanged,
827 notif_OnDeviceAdded,
828 notif_OnDeviceRemoved,
829 notif_OnDefaultDeviceChanged,
830 notif_OnPropertyValueChanged
833 static IMMNotificationClient g_notif = { &g_notif_vtbl };
835 static HRESULT WINMM_InitMMDevices(void)
837 HRESULT hr, init_hr;
838 IMMDeviceEnumerator *devenum = NULL;
840 if(g_outmmdevices_count || g_inmmdevices_count)
841 return S_FALSE;
843 init_hr = CoInitialize(NULL);
845 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
846 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
847 if(FAILED(hr))
848 goto exit;
850 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum, &g_notif);
851 if(FAILED(hr))
852 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr);
854 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_out_map, &g_outmmdevices_count,
855 eRender, devenum);
856 if(FAILED(hr)){
857 g_outmmdevices_count = 0;
858 g_inmmdevices_count = 0;
859 goto exit;
862 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_in_map, &g_inmmdevices_count,
863 eCapture, devenum);
864 if(FAILED(hr)){
865 g_inmmdevices_count = 0;
866 goto exit;
869 exit:
870 if(devenum)
871 IMMDeviceEnumerator_Release(devenum);
872 if(SUCCEEDED(init_hr))
873 CoUninitialize();
875 return hr;
878 static inline BOOL WINMM_IsMapper(UINT device)
880 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
883 static MMRESULT WINMM_TryDeviceMapping(WINMM_Device *device, WAVEFORMATEX *fmt,
884 WORD channels, DWORD freq, DWORD bits_per_samp, BOOL is_query, BOOL is_out)
886 WAVEFORMATEX target, *closer_fmt = NULL;
887 HRESULT hr;
888 MMRESULT mr;
890 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
891 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
893 target.wFormatTag = WAVE_FORMAT_PCM;
894 target.nChannels = channels;
895 target.nSamplesPerSec = freq;
896 target.wBitsPerSample = bits_per_samp;
897 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
898 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
899 target.cbSize = 0;
901 hr = IAudioClient_IsFormatSupported(device->client,
902 AUDCLNT_SHAREMODE_SHARED, &target, &closer_fmt);
903 if(closer_fmt)
904 CoTaskMemFree(closer_fmt);
905 if(hr != S_OK)
906 return WAVERR_BADFORMAT;
908 /* device supports our target format, so see if MSACM can
909 * do the conversion */
910 if(is_out)
911 mr = acmStreamOpen(&device->acm_handle, NULL, fmt, &target, NULL,
912 0, 0, 0);
913 else
914 mr = acmStreamOpen(&device->acm_handle, NULL, &target, fmt, NULL,
915 0, 0, 0);
916 if(mr != MMSYSERR_NOERROR)
917 return mr;
919 /* yes it can. initialize the audioclient and return success */
920 if(is_query){
921 acmStreamClose(device->acm_handle, 0);
922 device->acm_handle = NULL;
923 return MMSYSERR_NOERROR;
926 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
927 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
928 AC_BUFLEN, 0, &target, &device->parent->session);
929 if(hr != S_OK){
930 WARN("Initialize failed: %08x\n", hr);
931 acmStreamClose(device->acm_handle, 0);
932 device->acm_handle = NULL;
933 return MMSYSERR_ERROR;
936 device->bytes_per_frame = target.nBlockAlign;
937 device->samples_per_sec = target.nSamplesPerSec;
939 TRACE("Success!\n");
941 return MMSYSERR_NOERROR;
944 static MMRESULT WINMM_MapDevice(WINMM_Device *device, BOOL is_query, BOOL is_out)
946 MMRESULT mr;
947 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)device->orig_fmt;
949 TRACE("(%p, %u)\n", device, is_out);
951 /* set up the ACM stream */
952 if(device->orig_fmt->wFormatTag != WAVE_FORMAT_PCM &&
953 !(device->orig_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
954 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
955 /* convert to PCM format if it's not already */
956 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
957 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
958 16, is_query, is_out);
959 if(mr == MMSYSERR_NOERROR)
960 return mr;
962 mr = WINMM_TryDeviceMapping(device, device->orig_fmt,
963 device->orig_fmt->nChannels, device->orig_fmt->nSamplesPerSec,
964 8, is_query, is_out);
965 if(mr == MMSYSERR_NOERROR)
966 return mr;
967 }else{
968 WORD channels;
970 /* first try just changing bit depth and channels */
971 channels = device->orig_fmt->nChannels;
972 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
973 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
974 if(mr == MMSYSERR_NOERROR)
975 return mr;
976 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
977 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
978 if(mr == MMSYSERR_NOERROR)
979 return mr;
981 channels = (channels == 2) ? 1 : 2;
982 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
983 device->orig_fmt->nSamplesPerSec, 16, is_query, is_out);
984 if(mr == MMSYSERR_NOERROR)
985 return mr;
986 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels,
987 device->orig_fmt->nSamplesPerSec, 8, is_query, is_out);
988 if(mr == MMSYSERR_NOERROR)
989 return mr;
991 /* that didn't work, so now try different sample rates */
992 channels = device->orig_fmt->nChannels;
993 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
994 if(mr == MMSYSERR_NOERROR)
995 return mr;
996 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
997 if(mr == MMSYSERR_NOERROR)
998 return mr;
999 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
1000 if(mr == MMSYSERR_NOERROR)
1001 return mr;
1002 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1003 if(mr == MMSYSERR_NOERROR)
1004 return mr;
1005 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1006 if(mr == MMSYSERR_NOERROR)
1007 return mr;
1009 channels = (channels == 2) ? 1 : 2;
1010 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 16, is_query, is_out);
1011 if(mr == MMSYSERR_NOERROR)
1012 return mr;
1013 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 16, is_query, is_out);
1014 if(mr == MMSYSERR_NOERROR)
1015 return mr;
1016 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 16, is_query, is_out);
1017 if(mr == MMSYSERR_NOERROR)
1018 return mr;
1019 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 16, is_query, is_out);
1020 if(mr == MMSYSERR_NOERROR)
1021 return mr;
1022 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 16, is_query, is_out);
1023 if(mr == MMSYSERR_NOERROR)
1024 return mr;
1026 channels = device->orig_fmt->nChannels;
1027 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1028 if(mr == MMSYSERR_NOERROR)
1029 return mr;
1030 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1031 if(mr == MMSYSERR_NOERROR)
1032 return mr;
1033 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1034 if(mr == MMSYSERR_NOERROR)
1035 return mr;
1036 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1037 if(mr == MMSYSERR_NOERROR)
1038 return mr;
1039 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1040 if(mr == MMSYSERR_NOERROR)
1041 return mr;
1043 channels = (channels == 2) ? 1 : 2;
1044 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 96000, 8, is_query, is_out);
1045 if(mr == MMSYSERR_NOERROR)
1046 return mr;
1047 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 48000, 8, is_query, is_out);
1048 if(mr == MMSYSERR_NOERROR)
1049 return mr;
1050 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 44100, 8, is_query, is_out);
1051 if(mr == MMSYSERR_NOERROR)
1052 return mr;
1053 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 22050, 8, is_query, is_out);
1054 if(mr == MMSYSERR_NOERROR)
1055 return mr;
1056 mr = WINMM_TryDeviceMapping(device, device->orig_fmt, channels, 11025, 8, is_query, is_out);
1057 if(mr == MMSYSERR_NOERROR)
1058 return mr;
1061 WARN("Unable to find compatible device!\n");
1062 return WAVERR_BADFORMAT;
1065 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_OpenInfo *info,
1066 BOOL is_out)
1068 LRESULT ret = MMSYSERR_NOMEM;
1069 HRESULT hr;
1071 hr = IMMDeviceEnumerator_GetDevice(g_devenum, device->parent->dev_id,
1072 &device->device);
1073 if(FAILED(hr)){
1074 WARN("Device %s (%s) unavailable: %08x\n",
1075 wine_dbgstr_w(device->parent->dev_id),
1076 wine_dbgstr_w(device->parent->out_caps.szPname), hr);
1077 ret = MMSYSERR_NODRIVER;
1078 goto error;
1081 /* this is where winexyz.drv opens the audio device */
1082 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
1083 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
1084 if(FAILED(hr)){
1085 WARN("Activate failed: %08x\n", hr);
1086 ret = hr2mmr(hr);
1087 if(ret == MMSYSERR_ERROR)
1088 ret = MMSYSERR_NOTENABLED;
1089 goto error;
1092 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
1093 /* we aren't guaranteed that the struct in lpFormat is a full
1094 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1095 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
1096 memcpy(device->orig_fmt, info->format, sizeof(PCMWAVEFORMAT));
1097 device->orig_fmt->cbSize = 0;
1098 if(device->orig_fmt->wBitsPerSample % 8 != 0){
1099 WARN("Fixing bad wBitsPerSample (%u)\n", device->orig_fmt->wBitsPerSample);
1100 device->orig_fmt->wBitsPerSample = (device->orig_fmt->wBitsPerSample + 7) & ~7;
1102 /* winmm ignores broken blockalign and avgbytes */
1103 if(device->orig_fmt->nBlockAlign != device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8){
1104 WARN("Fixing bad nBlockAlign (%u)\n", device->orig_fmt->nBlockAlign);
1105 device->orig_fmt->nBlockAlign = device->orig_fmt->nChannels * device->orig_fmt->wBitsPerSample/8;
1107 if (device->orig_fmt->nAvgBytesPerSec != device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign) {
1108 WARN("Fixing bad nAvgBytesPerSec (%u)\n", device->orig_fmt->nAvgBytesPerSec);
1109 device->orig_fmt->nAvgBytesPerSec = device->orig_fmt->nSamplesPerSec * device->orig_fmt->nBlockAlign;
1111 }else{
1112 device->orig_fmt = HeapAlloc(GetProcessHeap(), 0,
1113 sizeof(WAVEFORMATEX) + info->format->cbSize);
1114 memcpy(device->orig_fmt, info->format,
1115 sizeof(WAVEFORMATEX) + info->format->cbSize);
1118 if(info->flags & WAVE_FORMAT_QUERY){
1119 WAVEFORMATEX *closer_fmt = NULL;
1121 hr = IAudioClient_IsFormatSupported(device->client,
1122 AUDCLNT_SHAREMODE_SHARED, device->orig_fmt, &closer_fmt);
1123 if(closer_fmt)
1124 CoTaskMemFree(closer_fmt);
1125 if((hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT) && !(info->flags & WAVE_FORMAT_DIRECT))
1126 ret = WINMM_MapDevice(device, TRUE, is_out);
1127 else
1128 ret = hr == S_FALSE ? WAVERR_BADFORMAT : hr2mmr(hr);
1129 goto error;
1132 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
1133 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
1134 AC_BUFLEN, 0, device->orig_fmt, &device->parent->session);
1135 if(FAILED(hr)){
1136 if(hr == AUDCLNT_E_UNSUPPORTED_FORMAT && !(info->flags & WAVE_FORMAT_DIRECT)){
1137 ret = WINMM_MapDevice(device, FALSE, is_out);
1138 if(ret != MMSYSERR_NOERROR || info->flags & WAVE_FORMAT_QUERY)
1139 goto error;
1140 }else{
1141 WARN("Initialize failed: %08x\n", hr);
1142 ret = hr2mmr(hr);
1143 goto error;
1145 }else{
1146 device->bytes_per_frame = device->orig_fmt->nBlockAlign;
1147 device->samples_per_sec = device->orig_fmt->nSamplesPerSec;
1150 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
1151 (void**)&device->clock);
1152 if(FAILED(hr)){
1153 WARN("GetService failed: %08x\n", hr);
1154 goto error;
1157 if(!device->event){
1158 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
1159 if(!device->event){
1160 WARN("CreateEvent failed: %08x\n", hr);
1161 goto error;
1164 /* As the devices thread is waiting on g_device_handles, it can
1165 * only be modified from within this same thread. */
1166 if(g_device_handles){
1167 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
1168 sizeof(HANDLE) * (g_devhandle_count + 1));
1169 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
1170 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
1171 }else{
1172 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
1173 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
1174 sizeof(WINMM_Device *));
1176 g_device_handles[g_devhandle_count] = device->event;
1177 g_handle_devices[g_devhandle_count] = device;
1178 ++g_devhandle_count;
1181 hr = IAudioClient_SetEventHandle(device->client, device->event);
1182 if(FAILED(hr)){
1183 WARN("SetEventHandle failed: %08x\n", hr);
1184 goto error;
1187 if(info->reset){
1188 device->played_frames = 0;
1189 device->ofs_bytes = 0;
1190 device->loop_counter = 0;
1191 device->first = device->last = device->playing = device->loop_start = NULL;
1194 device->stopped = TRUE;
1195 device->last_clock_pos = 0;
1197 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
1198 device->cb_info.callback = info->callback;
1199 device->cb_info.user = info->cb_user;
1200 device->cb_info.hwave = device->handle;
1202 info->handle = device->handle;
1204 return MMSYSERR_NOERROR;
1206 error:
1207 if(device->client){
1208 IAudioClient_Release(device->client);
1209 device->client = NULL;
1211 if(device->device){
1212 IMMDevice_Release(device->device);
1213 device->device = NULL;
1216 return ret;
1219 static LRESULT WOD_Open(WINMM_OpenInfo *info)
1221 WINMM_Device *device;
1222 LRESULT ret = MMSYSERR_ERROR;
1223 HRESULT hr;
1225 if(info->handle != 0){
1226 device = WINMM_GetDeviceFromHWAVE(info->handle);
1227 if(!device){
1228 WARN("Unexpected! Invalid info->handle given: %p\n", info->handle);
1229 return MMSYSERR_ERROR;
1232 EnterCriticalSection(&device->lock);
1234 device->open = TRUE;
1235 }else{
1236 CRITICAL_SECTION *lock;
1237 UINT internal_index;
1238 WINMM_Device **devices;
1239 WINMM_MMDevice *mmdevice;
1241 if(WINMM_IsMapper(info->req_device)){
1242 if (g_outmmdevices_count == 0)
1243 return MMSYSERR_BADDEVICEID;
1244 devices = g_out_mapper_devices;
1245 mmdevice = read_map(g_out_map, 0);
1246 lock = &g_devthread_lock;
1247 internal_index = MAPPER_INDEX;
1248 }else{
1249 if(info->req_device >= g_outmmdevices_count)
1250 return MMSYSERR_BADDEVICEID;
1252 mmdevice = read_map(g_out_map, info->req_device);
1254 if(!mmdevice->out_caps.szPname[0])
1255 return MMSYSERR_NOTENABLED;
1257 devices = mmdevice->devices;
1258 lock = &mmdevice->lock;
1259 internal_index = mmdevice->index;
1262 EnterCriticalSection(lock);
1264 device = WINMM_FindUnusedDevice(devices, mmdevice,
1265 internal_index, TRUE);
1266 if(!device){
1267 LeaveCriticalSection(lock);
1268 return MMSYSERR_ALLOCATED;
1271 LeaveCriticalSection(lock);
1274 ret = WINMM_OpenDevice(device, info, TRUE);
1275 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1276 goto error;
1277 ret = MMSYSERR_ERROR;
1279 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
1280 (void**)&device->render);
1281 if(FAILED(hr)){
1282 ERR("GetService failed: %08x\n", hr);
1283 goto error;
1286 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
1287 (void**)&device->volume);
1288 if(FAILED(hr)){
1289 ERR("GetService failed: %08x\n", hr);
1290 goto error;
1293 LeaveCriticalSection(&device->lock);
1295 return MMSYSERR_NOERROR;
1297 error:
1298 if(device->device){
1299 IMMDevice_Release(device->device);
1300 device->device = NULL;
1302 if(device->client){
1303 IAudioClient_Release(device->client);
1304 device->client = NULL;
1306 if(device->render){
1307 IAudioRenderClient_Release(device->render);
1308 device->render = NULL;
1310 if(device->volume){
1311 IAudioStreamVolume_Release(device->volume);
1312 device->volume = NULL;
1314 if(device->clock){
1315 IAudioClock_Release(device->clock);
1316 device->clock = NULL;
1318 device->open = FALSE;
1319 LeaveCriticalSection(&device->lock);
1320 return ret;
1323 static LRESULT WID_Open(WINMM_OpenInfo *info)
1325 WINMM_Device *device, **devices;
1326 WINMM_MMDevice *mmdevice;
1327 UINT internal_index;
1328 CRITICAL_SECTION *lock;
1329 LRESULT ret = MMSYSERR_ERROR;
1330 HRESULT hr;
1332 if(WINMM_IsMapper(info->req_device)){
1333 if (g_inmmdevices_count == 0)
1334 return MMSYSERR_BADDEVICEID;
1335 devices = g_in_mapper_devices;
1336 mmdevice = read_map(g_in_map, 0);
1337 lock = &g_devthread_lock;
1338 internal_index = MAPPER_INDEX;
1339 }else{
1340 if(info->req_device >= g_inmmdevices_count)
1341 return MMSYSERR_BADDEVICEID;
1343 mmdevice = read_map(g_in_map, info->req_device);
1345 if(!mmdevice->in_caps.szPname[0])
1346 return MMSYSERR_NOTENABLED;
1348 devices = mmdevice->devices;
1349 lock = &mmdevice->lock;
1350 internal_index = mmdevice->index;
1353 EnterCriticalSection(lock);
1355 device = WINMM_FindUnusedDevice(devices, mmdevice, internal_index, FALSE);
1356 if(!device){
1357 LeaveCriticalSection(lock);
1358 return MMSYSERR_ALLOCATED;
1361 LeaveCriticalSection(lock);
1363 ret = WINMM_OpenDevice(device, info, FALSE);
1364 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1365 goto error;
1366 ret = MMSYSERR_ERROR;
1368 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1369 (void**)&device->capture);
1370 if(FAILED(hr)){
1371 WARN("GetService failed: %08x\n", hr);
1372 goto error;
1375 LeaveCriticalSection(&device->lock);
1377 return MMSYSERR_NOERROR;
1379 error:
1380 if(device->device){
1381 IMMDevice_Release(device->device);
1382 device->device = NULL;
1384 if(device->client){
1385 IAudioClient_Release(device->client);
1386 device->client = NULL;
1388 if(device->capture){
1389 IAudioCaptureClient_Release(device->capture);
1390 device->capture = NULL;
1392 if(device->clock){
1393 IAudioClock_Release(device->clock);
1394 device->clock = NULL;
1396 device->open = FALSE;
1397 LeaveCriticalSection(&device->lock);
1398 return ret;
1401 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1403 device->open = FALSE;
1405 if(!device->stopped){
1406 IAudioClient_Stop(device->client);
1407 device->stopped = TRUE;
1410 if(device->acm_handle){
1411 acmStreamClose(device->acm_handle, 0);
1412 device->acm_handle = NULL;
1415 IMMDevice_Release(device->device);
1416 device->device = NULL;
1418 IAudioClient_Release(device->client);
1419 device->client = NULL;
1421 IAudioClock_Release(device->clock);
1422 device->clock = NULL;
1424 return S_OK;
1427 static LRESULT WOD_Close(HWAVEOUT hwave)
1429 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1431 TRACE("(%p)\n", hwave);
1433 if(!WINMM_ValidateAndLock(device))
1434 return MMSYSERR_INVALHANDLE;
1436 WINMM_CloseDevice(device);
1438 IAudioRenderClient_Release(device->render);
1439 device->render = NULL;
1441 IAudioStreamVolume_Release(device->volume);
1442 device->volume = NULL;
1444 LeaveCriticalSection(&device->lock);
1446 return MMSYSERR_NOERROR;
1449 static LRESULT WID_Close(HWAVEIN hwave)
1451 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1453 TRACE("(%p)\n", hwave);
1455 if(!WINMM_ValidateAndLock(device))
1456 return MMSYSERR_INVALHANDLE;
1458 WINMM_CloseDevice(device);
1460 IAudioCaptureClient_Release(device->capture);
1461 device->capture = NULL;
1463 LeaveCriticalSection(&device->lock);
1465 return MMSYSERR_NOERROR;
1468 static DWORD WINMM_FixedBufferLen(DWORD length, WINMM_Device *device)
1470 return length - length % device->bytes_per_frame;
1473 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1475 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1477 TRACE("(%p, %p)\n", hwave, header);
1479 if(!WINMM_ValidateAndLock(device))
1480 return MMSYSERR_INVALHANDLE;
1482 if(device->render && device->acm_handle){
1483 ACMSTREAMHEADER *ash;
1484 DWORD size;
1485 MMRESULT mr;
1487 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1488 ACM_STREAMSIZEF_SOURCE);
1489 if(mr != MMSYSERR_NOERROR){
1490 LeaveCriticalSection(&device->lock);
1491 return mr;
1494 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1495 if(!ash){
1496 LeaveCriticalSection(&device->lock);
1497 return MMSYSERR_NOMEM;
1500 ash->cbStruct = sizeof(*ash);
1501 ash->fdwStatus = 0;
1502 ash->dwUser = (DWORD_PTR)header;
1503 ash->pbSrc = (BYTE*)header->lpData;
1504 ash->cbSrcLength = header->dwBufferLength;
1505 ash->dwSrcUser = header->dwUser;
1506 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1507 ash->cbDstLength = size;
1508 ash->dwDstUser = 0;
1510 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1511 if(mr != MMSYSERR_NOERROR){
1512 HeapFree(GetProcessHeap(), 0, ash);
1513 LeaveCriticalSection(&device->lock);
1514 return mr;
1517 header->reserved = (DWORD_PTR)ash;
1520 LeaveCriticalSection(&device->lock);
1522 header->dwFlags |= WHDR_PREPARED;
1523 header->dwFlags &= ~(WHDR_DONE|WHDR_INQUEUE); /* flags cleared since w2k */
1525 return MMSYSERR_NOERROR;
1528 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1530 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1532 TRACE("(%p, %p)\n", hwave, header);
1534 if(!WINMM_ValidateAndLock(device))
1535 return MMSYSERR_INVALHANDLE;
1537 if(device->render && device->acm_handle){
1538 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1540 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1542 HeapFree(GetProcessHeap(), 0, ash);
1545 LeaveCriticalSection(&device->lock);
1547 header->dwFlags &= ~WHDR_PREPARED;
1549 return MMSYSERR_NOERROR;
1552 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1554 if(device->acm_handle){
1555 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1556 return WINMM_FixedBufferLen(ash->cbDstLengthUsed, device);
1559 return WINMM_FixedBufferLen(header->dwBufferLength, device);
1562 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1564 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1567 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1569 HRESULT hr;
1570 WAVEHDR *first = device->first, *queue = first, *last = NULL;
1571 UINT64 clock_freq, clock_pos, clock_frames;
1572 UINT32 nloops, queue_frames = 0;
1574 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1575 if(FAILED(hr)){
1576 WARN("GetFrequency failed: %08x\n", hr);
1577 return NULL;
1580 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1581 if(FAILED(hr)){
1582 WARN("GetPosition failed: %08x\n", hr);
1583 return NULL;
1586 clock_frames = (clock_pos * device->samples_per_sec) / clock_freq;
1588 nloops = device->loop_counter;
1589 while(queue &&
1590 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1591 clock_frames - device->last_clock_pos + device->remainder_frames){
1592 if(!nloops){
1593 last = queue;
1594 device->last_clock_pos += queue_frames;
1595 device->remainder_frames = 0;
1596 queue_frames = 0;
1597 queue = device->first = queue->lpNext;
1598 }else{
1599 if(queue->dwFlags & WHDR_BEGINLOOP){
1600 if(queue->dwFlags & WHDR_ENDLOOP)
1601 --nloops;
1602 else
1603 queue = queue->lpNext;
1604 }else if(queue->dwFlags & WHDR_ENDLOOP){
1605 queue = device->loop_start;
1606 --nloops;
1611 if(last){
1612 last->lpNext = NULL;
1613 return first;
1614 }else
1615 return NULL;
1618 static void WOD_PushData(WINMM_Device *device)
1620 WINMM_CBInfo cb_info;
1621 HRESULT hr;
1622 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1623 UINT32 queue_bytes, nloops;
1624 BYTE *data;
1625 WAVEHDR *queue, *first = NULL;
1627 TRACE("(%p)\n", device->handle);
1629 EnterCriticalSection(&device->lock);
1631 if(!device->device)
1632 goto exit;
1634 if(!device->first){
1635 if (device->stopped)
1636 goto exit;
1637 device->stopped = TRUE;
1638 device->last_clock_pos = 0;
1639 IAudioClient_Stop(device->client);
1640 IAudioClient_Reset(device->client);
1641 goto exit;
1644 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1645 if(FAILED(hr)){
1646 WARN("GetBufferSize failed: %08x\n", hr);
1647 goto exit;
1650 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1651 if(FAILED(hr)){
1652 WARN("GetCurrentPadding failed: %08x\n", hr);
1653 goto exit;
1656 first = WOD_MarkDoneHeaders(device);
1658 /* determine which is larger between the available buffer size and
1659 * the amount of data left in the queue */
1660 avail_frames = bufsize - pad;
1662 queue = device->playing;
1663 ofs = device->ofs_bytes;
1664 queue_frames = 0;
1665 nloops = 0;
1666 while(queue && queue_frames < avail_frames){
1667 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1668 queue_frames += (queue_bytes - ofs) / device->bytes_per_frame;
1669 ofs = 0;
1671 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1672 queue = device->loop_start;
1673 ++nloops;
1674 }else
1675 queue = queue->lpNext;
1678 if(queue_frames < avail_frames)
1679 avail_frames = queue_frames;
1680 if(avail_frames == 0)
1681 goto exit;
1683 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1684 if(FAILED(hr)){
1685 WARN("GetBuffer failed: %08x\n", hr);
1686 goto exit;
1689 written = 0;
1690 while(device->playing && written < avail_frames){
1691 UINT32 copy_frames, copy_bytes;
1692 BYTE *queue_data;
1694 queue = device->playing;
1696 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1697 if(device->acm_handle)
1698 queue_data = ((ACMSTREAMHEADER*)queue->reserved)->pbDst;
1699 else
1700 queue_data = (BYTE*)queue->lpData;
1702 queue_frames = (queue_bytes - device->ofs_bytes) /
1703 device->bytes_per_frame;
1705 copy_frames = queue_frames < (avail_frames - written) ?
1706 queue_frames : avail_frames - written;
1707 copy_bytes = copy_frames * device->bytes_per_frame;
1709 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1711 data += copy_bytes;
1712 written += copy_frames;
1713 device->ofs_bytes += copy_bytes;
1715 if(device->ofs_bytes >= queue_bytes){
1716 device->ofs_bytes = 0;
1718 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1719 device->playing = queue->lpNext;
1720 else{
1721 if(queue->dwFlags & WHDR_BEGINLOOP){
1722 device->loop_start = device->playing;
1723 device->playing = queue->lpNext;
1724 device->loop_counter = queue->dwLoops;
1726 if(queue->dwFlags & WHDR_ENDLOOP){
1727 --device->loop_counter;
1728 if(device->loop_counter)
1729 device->playing = device->loop_start;
1730 else
1731 device->loop_start = device->playing = queue->lpNext;
1737 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1738 if(FAILED(hr)){
1739 WARN("ReleaseBuffer failed: %08x\n", hr);
1740 goto exit;
1743 if(device->orig_fmt->nSamplesPerSec != device->samples_per_sec)
1744 device->played_frames += MulDiv(avail_frames, device->orig_fmt->nSamplesPerSec, device->samples_per_sec);
1745 else
1746 device->played_frames += avail_frames;
1748 exit:
1749 cb_info = device->cb_info;
1751 LeaveCriticalSection(&device->lock);
1753 while(first){
1754 WAVEHDR *next = first->lpNext;
1755 first->dwFlags &= ~WHDR_INQUEUE;
1756 first->dwFlags |= WHDR_DONE;
1757 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1758 first = next;
1762 static void WID_PullACMData(WINMM_Device *device)
1764 UINT32 packet, packet_bytes;
1765 DWORD flags;
1766 BYTE *data;
1767 WAVEHDR *queue;
1768 HRESULT hr;
1769 MMRESULT mr;
1771 if(device->acm_hdr.cbDstLength == 0){
1772 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1773 &flags, NULL, NULL);
1774 if(hr != S_OK){
1775 if(FAILED(hr))
1776 WARN("GetBuffer failed: %08x\n", hr);
1777 return;
1780 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1781 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1783 device->acm_offs = 0;
1785 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1786 device->acm_hdr.fdwStatus = 0;
1787 device->acm_hdr.dwUser = 0;
1788 device->acm_hdr.pbSrc = data;
1789 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1790 device->acm_hdr.cbSrcLengthUsed = 0;
1791 device->acm_hdr.dwSrcUser = 0;
1792 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1793 device->acm_hdr.cbDstLength = packet_bytes;
1794 device->acm_hdr.cbDstLengthUsed = 0;
1795 device->acm_hdr.dwDstUser = 0;
1797 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1798 if(mr != MMSYSERR_NOERROR){
1799 WARN("acmStreamPrepareHeader failed: %d\n", mr);
1800 return;
1803 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1804 if(mr != MMSYSERR_NOERROR){
1805 WARN("acmStreamConvert failed: %d\n", mr);
1806 return;
1809 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1810 if(FAILED(hr))
1811 WARN("ReleaseBuffer failed: %08x\n", hr);
1813 device->played_frames += packet;
1816 queue = device->first;
1817 while(queue){
1818 UINT32 to_copy_bytes;
1820 to_copy_bytes = min(WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded,
1821 WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device) - device->acm_offs);
1823 memcpy(queue->lpData + queue->dwBytesRecorded,
1824 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1826 queue->dwBytesRecorded += to_copy_bytes;
1827 device->acm_offs += to_copy_bytes;
1829 if(queue->dwBufferLength - queue->dwBytesRecorded <
1830 device->bytes_per_frame){
1831 queue->dwFlags &= ~WHDR_INQUEUE;
1832 queue->dwFlags |= WHDR_DONE;
1833 device->first = queue = queue->lpNext;
1836 if(device->acm_offs >= WINMM_FixedBufferLen(device->acm_hdr.cbDstLengthUsed, device)){
1837 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1838 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1839 device->acm_hdr.cbDstLength = 0;
1840 device->acm_hdr.cbDstLengthUsed = 0;
1842 /* done with this ACM Header, so try to pull more data */
1843 WID_PullACMData(device);
1844 return;
1848 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1849 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1850 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1851 device->acm_hdr.cbDstLength = 0;
1852 device->acm_hdr.cbDstLengthUsed = 0;
1855 static void WID_PullData(WINMM_Device *device)
1857 WINMM_CBInfo cb_info;
1858 WAVEHDR *queue, *first = NULL, *last = NULL;
1859 HRESULT hr;
1861 TRACE("(%p)\n", device->handle);
1863 EnterCriticalSection(&device->lock);
1865 if(!device->device || !device->first)
1866 goto exit;
1868 first = device->first;
1870 if(device->acm_handle){
1871 WID_PullACMData(device);
1872 goto exit;
1875 while(device->first){
1876 BYTE *data;
1877 UINT32 packet_len, packet;
1878 DWORD flags;
1880 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet_len,
1881 &flags, NULL, NULL);
1882 if(hr != S_OK){
1883 if(FAILED(hr))
1884 WARN("GetBuffer failed: %08x\n", hr);
1885 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1886 IAudioCaptureClient_ReleaseBuffer(device->capture, 0);
1887 goto exit;
1890 packet = packet_len;
1891 queue = device->first;
1892 while(queue && packet > 0){
1893 UINT32 to_copy_bytes;
1895 to_copy_bytes = min(packet * device->bytes_per_frame,
1896 WINMM_FixedBufferLen(queue->dwBufferLength, device) - queue->dwBytesRecorded);
1898 memcpy(queue->lpData + queue->dwBytesRecorded,
1899 data + (packet_len - packet) * device->bytes_per_frame,
1900 to_copy_bytes);
1902 queue->dwBytesRecorded += to_copy_bytes;
1904 if(queue->dwBufferLength - queue->dwBytesRecorded <
1905 device->bytes_per_frame){
1906 last = queue;
1907 device->first = queue = queue->lpNext;
1910 packet -= to_copy_bytes / device->bytes_per_frame;
1913 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1914 if(FAILED(hr))
1915 WARN("ReleaseBuffer failed: %08x\n", hr);
1917 if(packet > 0)
1918 WARN("losing %u frames\n", packet);
1919 device->played_frames += packet_len - packet;
1922 exit:
1923 cb_info = device->cb_info;
1925 LeaveCriticalSection(&device->lock);
1927 if(last){
1928 last->lpNext = NULL;
1929 while(first){
1930 WAVEHDR *next = first->lpNext;
1931 first->dwFlags &= ~WHDR_INQUEUE;
1932 first->dwFlags |= WHDR_DONE;
1933 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1934 first = next;
1939 static MMRESULT WINMM_BeginPlaying(WINMM_Device *device)
1941 HRESULT hr;
1943 TRACE("(%p)\n", device->handle);
1945 if(device->render)
1946 /* prebuffer data before starting */
1947 WOD_PushData(device);
1949 if(device->stopped){
1950 device->stopped = FALSE;
1952 hr = IAudioClient_Start(device->client);
1953 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1954 device->stopped = TRUE;
1955 WARN("Start failed: %08x\n", hr);
1956 return MMSYSERR_ERROR;
1960 return MMSYSERR_NOERROR;
1963 static LRESULT WINMM_Pause(HWAVE hwave)
1965 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1966 HRESULT hr;
1968 TRACE("(%p)\n", hwave);
1970 if(!WINMM_ValidateAndLock(device))
1971 return MMSYSERR_INVALHANDLE;
1973 hr = IAudioClient_Stop(device->client);
1974 if(FAILED(hr)){
1975 LeaveCriticalSection(&device->lock);
1976 WARN("Stop failed: %08x\n", hr);
1977 return MMSYSERR_ERROR;
1980 device->stopped = FALSE;
1982 LeaveCriticalSection(&device->lock);
1984 return MMSYSERR_NOERROR;
1987 static LRESULT WINMM_Reset(HWAVE hwave)
1989 WINMM_CBInfo cb_info;
1990 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1991 BOOL is_out;
1992 WAVEHDR *first;
1993 HRESULT hr;
1995 TRACE("(%p)\n", hwave);
1997 if(!WINMM_ValidateAndLock(device))
1998 return MMSYSERR_INVALHANDLE;
2000 hr = IAudioClient_Stop(device->client);
2001 if(FAILED(hr)){
2002 LeaveCriticalSection(&device->lock);
2003 WARN("Stop failed: %08x\n", hr);
2004 return MMSYSERR_ERROR;
2006 device->stopped = TRUE;
2008 first = device->first;
2009 device->first = device->last = device->playing = NULL;
2010 device->ofs_bytes = 0;
2011 device->played_frames = 0;
2012 device->loop_counter = 0;
2013 device->last_clock_pos = 0;
2014 IAudioClient_Reset(device->client);
2016 cb_info = device->cb_info;
2017 is_out = device->render != NULL;
2019 LeaveCriticalSection(&device->lock);
2021 while(first){
2022 WAVEHDR *next = first->lpNext;
2023 first->dwFlags &= ~WHDR_INQUEUE;
2024 first->dwFlags |= WHDR_DONE;
2025 if(is_out)
2026 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
2027 else
2028 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
2029 first = next;
2032 return MMSYSERR_NOERROR;
2035 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
2036 UINT32 sample_rate, UINT32 bytes_per_sec)
2038 switch(time->wType){
2039 case TIME_SAMPLES:
2040 time->u.sample = played_frames;
2041 return MMSYSERR_NOERROR;
2042 case TIME_MS:
2043 time->u.ms = (UINT64)played_frames * 1000 / sample_rate;
2044 return MMSYSERR_NOERROR;
2045 case TIME_SMPTE:
2046 time->u.smpte.fps = 30;
2047 played_frames += sample_rate / time->u.smpte.fps - 1; /* round up */
2048 time->u.smpte.frame = (played_frames % sample_rate) * time->u.smpte.fps / sample_rate;
2049 played_frames /= sample_rate; /* yields seconds */
2050 time->u.smpte.sec = played_frames % 60;
2051 played_frames /= 60;
2052 time->u.smpte.min = played_frames % 60;
2053 time->u.smpte.hour= played_frames / 60;
2054 return MMSYSERR_NOERROR;
2055 default:
2056 time->wType = TIME_BYTES;
2057 /* fall through */
2058 case TIME_BYTES:
2059 time->u.cb = MulDiv(played_frames, bytes_per_sec, sample_rate);
2060 return MMSYSERR_NOERROR;
2064 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
2066 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
2067 UINT32 played_frames, sample_rate, bytes_per_sec;
2069 TRACE("(%p, %p)\n", hwave, time);
2071 if(!WINMM_ValidateAndLock(device))
2072 return MMSYSERR_INVALHANDLE;
2074 played_frames = device->played_frames;
2075 sample_rate = device->orig_fmt->nSamplesPerSec;
2076 bytes_per_sec = device->orig_fmt->nAvgBytesPerSec;
2078 LeaveCriticalSection(&device->lock);
2080 return WINMM_FramesToMMTime(time, played_frames, sample_rate, bytes_per_sec);
2083 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
2084 UINT *mmdev_index)
2086 UINT mmdev, dev, junk, *out;
2087 BOOL is_out;
2089 if(!mmdev_index)
2090 out = &mmdev;
2091 else
2092 out = mmdev_index;
2094 switch(flags & 0xF0000000){
2095 case MIXER_OBJECTF_MIXER: /* == 0 */
2096 *out = HandleToULong(hmix);
2097 if(*out < g_outmmdevices_count)
2098 return read_map(g_out_map, *out);
2099 if(*out - g_outmmdevices_count < g_inmmdevices_count){
2100 *out -= g_outmmdevices_count;
2101 return read_map(g_in_map, *out);
2103 /* fall through -- if it's not a valid mixer device, then
2104 * it could be a valid mixer handle. windows seems to do
2105 * this as well. */
2106 case MIXER_OBJECTF_HMIXER:
2107 case MIXER_OBJECTF_HWAVEOUT:
2108 case MIXER_OBJECTF_HWAVEIN:
2109 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
2110 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
2111 (!is_out && *out >= g_inmmdevices_count))
2112 return NULL;
2113 if(is_out)
2114 return read_map(g_out_map, *out);
2115 return read_map(g_in_map, *out);
2116 case MIXER_OBJECTF_WAVEOUT:
2117 *out = HandleToULong(hmix);
2118 if(*out < g_outmmdevices_count)
2119 return read_map(g_out_map, *out);
2120 return NULL;
2121 case MIXER_OBJECTF_WAVEIN:
2122 *out = HandleToULong(hmix);
2123 if(*out < g_inmmdevices_count)
2124 return read_map(g_in_map, *out);
2125 return NULL;
2128 return NULL;
2131 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
2133 IAudioSessionManager *sesman;
2134 IMMDevice *device;
2135 HRESULT hr;
2137 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
2138 if(FAILED(hr)){
2139 WARN("Device %s (%s) unavailable: %08x\n",
2140 wine_dbgstr_w(mmdevice->dev_id),
2141 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
2142 return MMSYSERR_ERROR;
2145 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
2146 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
2147 if(FAILED(hr)){
2148 WARN("Activate failed: %08x\n", hr);
2149 IMMDevice_Release(device);
2150 return MMSYSERR_ERROR;
2153 IMMDevice_Release(device);
2155 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
2156 FALSE, &mmdevice->volume);
2157 IAudioSessionManager_Release(sesman);
2158 if(FAILED(hr)){
2159 WARN("GetSimpleAudioVolume failed: %08x\n", hr);
2160 return MMSYSERR_ERROR;
2163 return MMSYSERR_NOERROR;
2166 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
2168 WINMM_MMDevice *mmdevice;
2169 MIXERCONTROLDETAILS *control = details->details;
2170 HRESULT hr;
2172 TRACE("(%p)\n", details->hmix);
2174 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2175 if(!mmdevice)
2176 return MMSYSERR_INVALHANDLE;
2178 EnterCriticalSection(&mmdevice->lock);
2180 if(!mmdevice->volume){
2181 MMRESULT mr;
2183 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2184 if(mr != MMSYSERR_NOERROR){
2185 LeaveCriticalSection(&mmdevice->lock);
2186 return mr;
2190 if(control->dwControlID == 0){
2191 float vol;
2192 MIXERCONTROLDETAILS_UNSIGNED *udet;
2194 if(!control->paDetails ||
2195 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2196 LeaveCriticalSection(&mmdevice->lock);
2197 return MMSYSERR_INVALPARAM;
2200 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
2201 if(FAILED(hr)){
2202 WARN("GetMasterVolume failed: %08x\n", hr);
2203 LeaveCriticalSection(&mmdevice->lock);
2204 return MMSYSERR_ERROR;
2207 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2208 udet->dwValue = vol * ((unsigned int)0xFFFF);
2209 }else if(control->dwControlID == 1){
2210 BOOL mute;
2211 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2213 if(!control->paDetails ||
2214 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2215 LeaveCriticalSection(&mmdevice->lock);
2216 return MMSYSERR_INVALPARAM;
2219 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
2220 if(FAILED(hr)){
2221 WARN("GetMute failed: %08x\n", hr);
2222 LeaveCriticalSection(&mmdevice->lock);
2223 return MMSYSERR_ERROR;
2226 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2227 bdet->fValue = mute;
2228 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2229 FIXME("What should the sw-side mixer controls map to?\n");
2230 }else{
2231 LeaveCriticalSection(&mmdevice->lock);
2232 return MIXERR_INVALCONTROL;
2235 LeaveCriticalSection(&mmdevice->lock);
2237 return MMSYSERR_NOERROR;
2240 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
2242 WINMM_MMDevice *mmdevice;
2243 MIXERCONTROLDETAILS *control = details->details;
2244 HRESULT hr;
2246 TRACE("(%p)\n", details->hmix);
2248 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
2249 if(!mmdevice)
2250 return MMSYSERR_INVALHANDLE;
2252 EnterCriticalSection(&mmdevice->lock);
2254 if(!mmdevice->volume){
2255 MMRESULT mr;
2257 mr = WINMM_SetupMMDeviceVolume(mmdevice);
2258 if(mr != MMSYSERR_NOERROR){
2259 LeaveCriticalSection(&mmdevice->lock);
2260 return mr;
2264 if(control->dwControlID == 0){
2265 float vol;
2266 MIXERCONTROLDETAILS_UNSIGNED *udet;
2268 if(!control->paDetails ||
2269 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
2270 LeaveCriticalSection(&mmdevice->lock);
2271 return MMSYSERR_INVALPARAM;
2274 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
2276 if(udet->dwValue > 65535){
2277 LeaveCriticalSection(&mmdevice->lock);
2278 return MMSYSERR_INVALPARAM;
2281 vol = udet->dwValue / 65535.f;
2283 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2284 if(FAILED(hr)){
2285 WARN("SetMasterVolume failed: %08x\n", hr);
2286 LeaveCriticalSection(&mmdevice->lock);
2287 return MMSYSERR_ERROR;
2289 }else if(control->dwControlID == 1){
2290 BOOL mute;
2291 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2293 if(!control->paDetails ||
2294 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2295 LeaveCriticalSection(&mmdevice->lock);
2296 return MMSYSERR_INVALPARAM;
2299 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2300 mute = bdet->fValue;
2302 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2303 if(FAILED(hr)){
2304 WARN("SetMute failed: %08x\n", hr);
2305 LeaveCriticalSection(&mmdevice->lock);
2306 return MMSYSERR_ERROR;
2308 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2309 FIXME("What should the sw-side mixer controls map to?\n");
2310 }else{
2311 LeaveCriticalSection(&mmdevice->lock);
2312 return MIXERR_INVALCONTROL;
2315 LeaveCriticalSection(&mmdevice->lock);
2317 return MMSYSERR_NOERROR;
2320 static LRESULT DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo *info)
2322 WINMM_MMDevice *mmdevice;
2323 IMMDevice *device;
2324 IPropertyStore *ps;
2325 PROPVARIANT pv;
2326 DWORD len_bytes;
2327 HRESULT hr;
2329 static const PROPERTYKEY deviceinterface_key = {
2330 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2333 if(WINMM_IsMapper(info->index)){
2334 if(info->str){
2335 if(*info->len_bytes < sizeof(WCHAR))
2336 return MMSYSERR_INVALPARAM;
2337 *info->str = 0;
2338 }else
2339 *info->len_bytes = sizeof(WCHAR);
2340 return MMSYSERR_NOERROR;
2343 if(info->is_out){
2344 if(info->index >= g_outmmdevices_count)
2345 return MMSYSERR_INVALHANDLE;
2347 mmdevice = &g_out_mmdevices[info->index];
2348 }else{
2349 if(info->index >= g_inmmdevices_count)
2350 return MMSYSERR_INVALHANDLE;
2352 mmdevice = &g_in_mmdevices[info->index];
2355 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
2356 &device);
2357 if(FAILED(hr)){
2358 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice->dev_id), hr);
2359 return MMSYSERR_ERROR;
2362 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
2363 if(FAILED(hr)){
2364 WARN("OpenPropertyStore failed: %08x\n", hr);
2365 IMMDevice_Release(device);
2366 return MMSYSERR_ERROR;
2369 PropVariantInit(&pv);
2370 hr = IPropertyStore_GetValue(ps, &deviceinterface_key, &pv);
2371 if(FAILED(hr)){
2372 WARN("GetValue failed: %08x\n", hr);
2373 IPropertyStore_Release(ps);
2374 IMMDevice_Release(device);
2375 return MMSYSERR_ERROR;
2377 if(pv.vt != VT_LPWSTR){
2378 WARN("Got unexpected property type: %u\n", pv.vt);
2379 PropVariantClear(&pv);
2380 IPropertyStore_Release(ps);
2381 IMMDevice_Release(device);
2382 return MMSYSERR_ERROR;
2385 len_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
2387 if(info->str){
2388 if(len_bytes > *info->len_bytes){
2389 PropVariantClear(&pv);
2390 IPropertyStore_Release(ps);
2391 IMMDevice_Release(device);
2392 return MMSYSERR_INVALPARAM;
2395 memcpy(info->str, pv.u.pwszVal, len_bytes);
2396 }else
2397 *info->len_bytes = len_bytes;
2399 PropVariantClear(&pv);
2400 IPropertyStore_Release(ps);
2401 IMMDevice_Release(device);
2403 return MMSYSERR_NOERROR;
2406 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2407 LPARAM lparam)
2409 switch(msg){
2410 case WODM_OPEN:
2411 return WOD_Open((WINMM_OpenInfo*)wparam);
2412 case WODM_CLOSE:
2413 return WOD_Close((HWAVEOUT)wparam);
2414 case WIDM_OPEN:
2415 return WID_Open((WINMM_OpenInfo*)wparam);
2416 case WIDM_CLOSE:
2417 return WID_Close((HWAVEIN)wparam);
2418 case MXDM_GETCONTROLDETAILS:
2419 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2420 case MXDM_SETCONTROLDETAILS:
2421 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2422 case DRV_QUERYDEVICEINTERFACESIZE:
2423 case DRV_QUERYDEVICEINTERFACE:
2424 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo*)wparam);
2426 return DefWindowProcW(hwnd, msg, wparam, lparam);
2429 static BOOL WINMM_DevicesThreadDone(void)
2431 UINT i;
2433 EnterCriticalSection(&g_devthread_lock);
2435 if(g_devthread_token > 0){
2436 LeaveCriticalSection(&g_devthread_lock);
2437 return FALSE;
2440 for(i = 0; i < g_devhandle_count; ++i){
2441 if(g_handle_devices[i]->open){
2442 LeaveCriticalSection(&g_devthread_lock);
2443 return FALSE;
2447 DestroyWindow(g_devices_hwnd);
2448 g_devices_hwnd = NULL;
2449 IMMDeviceEnumerator_Release(g_devenum);
2450 g_devenum = NULL;
2451 CoUninitialize();
2453 LeaveCriticalSection(&g_devthread_lock);
2455 return TRUE;
2458 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2460 HANDLE evt = arg;
2461 HRESULT hr;
2462 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2464 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2465 if(FAILED(hr)){
2466 WARN("CoInitializeEx failed: %08x\n", hr);
2467 FreeLibraryAndExitThread(g_devthread_module, 1);
2470 hr = WINMM_InitMMDevices();
2471 if(FAILED(hr)){
2472 CoUninitialize();
2473 FreeLibraryAndExitThread(g_devthread_module, 1);
2476 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
2477 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
2478 if(FAILED(hr)){
2479 WARN("CoCreateInstance failed: %08x\n", hr);
2480 CoUninitialize();
2481 FreeLibraryAndExitThread(g_devthread_module, 1);
2484 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2485 HWND_MESSAGE, NULL, NULL, NULL);
2486 if(!g_devices_hwnd){
2487 WARN("CreateWindow failed: %d\n", GetLastError());
2488 CoUninitialize();
2489 FreeLibraryAndExitThread(g_devthread_module, 1);
2492 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2493 (LONG_PTR)WINMM_DevicesMsgProc);
2495 /* inform caller that the thread is ready to process messages */
2496 SetEvent(evt);
2497 evt = NULL; /* do not use after this point */
2499 while(1){
2500 DWORD wait;
2501 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2502 FALSE, INFINITE, QS_ALLINPUT);
2503 if(wait == g_devhandle_count + WAIT_OBJECT_0){
2504 MSG msg;
2505 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2506 WARN("Unexpected message: 0x%x\n", msg.message);
2507 if(!g_devices_hwnd)
2508 break;
2509 }else if(wait < g_devhandle_count + WAIT_OBJECT_0){
2510 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2511 if(device->render)
2512 WOD_PushData(device);
2513 else
2514 WID_PullData(device);
2515 }else
2516 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2517 GetLastError());
2518 if(WINMM_DevicesThreadDone()){
2519 TRACE("Quitting devices thread\n");
2520 FreeLibraryAndExitThread(g_devthread_module, 0);
2524 FreeLibraryAndExitThread(g_devthread_module, 0);
2527 /* on success, increments g_devthread_token to prevent
2528 * device thread shutdown. caller must decrement. */
2529 static BOOL WINMM_StartDevicesThread(void)
2531 HANDLE events[2];
2532 DWORD wait;
2534 EnterCriticalSection(&g_devthread_lock);
2536 if(g_devices_hwnd){
2537 wait = WaitForSingleObject(g_devices_thread, 0);
2538 if(wait == WAIT_TIMEOUT){
2539 /* thread still running */
2540 InterlockedIncrement(&g_devthread_token);
2541 LeaveCriticalSection(&g_devthread_lock);
2542 return TRUE;
2544 if(wait != WAIT_OBJECT_0){
2545 /* error */
2546 LeaveCriticalSection(&g_devthread_lock);
2547 return FALSE;
2549 TRACE("Devices thread left dangling message window?\n");
2550 g_devices_hwnd = NULL;
2551 CloseHandle(g_devices_thread);
2552 g_devices_thread = NULL;
2553 }else if(g_devices_thread){
2554 WaitForSingleObject(g_devices_thread, INFINITE);
2555 CloseHandle(g_devices_thread);
2556 g_devices_thread = NULL;
2559 TRACE("Starting up devices thread\n");
2561 /* The devices thread holds a reference to the winmm module
2562 * to prevent it from unloading while it's running. */
2563 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2564 (const WCHAR *)WINMM_StartDevicesThread, &g_devthread_module);
2566 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2568 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2569 events[0], 0, NULL);
2570 if(!g_devices_thread){
2571 LeaveCriticalSection(&g_devthread_lock);
2572 CloseHandle(events[0]);
2573 FreeLibrary(g_devthread_module);
2574 return FALSE;
2577 events[1] = g_devices_thread;
2578 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2579 CloseHandle(events[0]);
2580 if(wait != WAIT_OBJECT_0){
2581 if(wait == 1 + WAIT_OBJECT_0){
2582 CloseHandle(g_devices_thread);
2583 g_devices_thread = NULL;
2584 g_devices_hwnd = NULL;
2586 LeaveCriticalSection(&g_devthread_lock);
2587 return FALSE;
2590 InterlockedIncrement(&g_devthread_token);
2592 LeaveCriticalSection(&g_devthread_lock);
2594 return TRUE;
2597 /**************************************************************************
2598 * waveOutGetNumDevs [WINMM.@]
2600 UINT WINAPI waveOutGetNumDevs(void)
2602 HRESULT hr = WINMM_InitMMDevices();
2603 if(FAILED(hr))
2604 return 0;
2606 TRACE("count: %u\n", g_outmmdevices_count);
2608 return g_outmmdevices_count;
2611 /**************************************************************************
2612 * waveOutGetDevCapsA [WINMM.@]
2614 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2615 UINT uSize)
2617 WAVEOUTCAPSW wocW;
2618 UINT ret;
2620 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2622 if(!lpCaps)
2623 return MMSYSERR_INVALPARAM;
2625 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2627 if (ret == MMSYSERR_NOERROR) {
2628 WAVEOUTCAPSA wocA;
2629 wocA.wMid = wocW.wMid;
2630 wocA.wPid = wocW.wPid;
2631 wocA.vDriverVersion = wocW.vDriverVersion;
2632 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2633 sizeof(wocA.szPname), NULL, NULL );
2634 wocA.dwFormats = wocW.dwFormats;
2635 wocA.wChannels = wocW.wChannels;
2636 wocA.dwSupport = wocW.dwSupport;
2637 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2639 return ret;
2642 /**************************************************************************
2643 * waveOutGetDevCapsW [WINMM.@]
2645 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2646 UINT uSize)
2648 WAVEOUTCAPSW mapper_caps, *caps;
2649 HRESULT hr;
2651 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2653 hr = WINMM_InitMMDevices();
2654 if(FAILED(hr))
2655 return MMSYSERR_NODRIVER;
2657 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2659 if(WINMM_IsMapper(uDeviceID)){
2660 /* FIXME: Should be localized */
2661 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2662 'n','d',' ','M','a','p','p','e','r',0};
2664 mapper_caps.wMid = 0xFF;
2665 mapper_caps.wPid = 0xFF;
2666 mapper_caps.vDriverVersion = 0x00010001;
2667 mapper_caps.dwFormats = 0xFFFFFFFF;
2668 mapper_caps.wReserved1 = 0;
2669 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2670 WAVECAPS_SAMPLEACCURATE;
2671 mapper_caps.wChannels = 2;
2672 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2674 caps = &mapper_caps;
2675 }else{
2676 if(uDeviceID >= g_outmmdevices_count)
2677 return MMSYSERR_BADDEVICEID;
2679 caps = &read_map(g_out_map, uDeviceID)->out_caps;
2682 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2684 return MMSYSERR_NOERROR;
2687 /**************************************************************************
2688 * waveOutGetErrorTextA [WINMM.@]
2689 * waveInGetErrorTextA [WINMM.@]
2691 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2693 UINT ret;
2695 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2696 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2697 else
2699 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2700 if (!xstr) ret = MMSYSERR_NOMEM;
2701 else
2703 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2704 if (ret == MMSYSERR_NOERROR)
2705 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2706 HeapFree(GetProcessHeap(), 0, xstr);
2709 return ret;
2712 /**************************************************************************
2713 * waveOutGetErrorTextW [WINMM.@]
2714 * waveInGetErrorTextW [WINMM.@]
2716 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2718 UINT ret = MMSYSERR_BADERRNUM;
2720 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2721 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2722 else if (
2723 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2724 * a warning for the test was always true */
2725 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2726 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2727 if (LoadStringW(hWinMM32Instance,
2728 uError, lpText, uSize) > 0) {
2729 ret = MMSYSERR_NOERROR;
2732 return ret;
2735 /**************************************************************************
2736 * waveOutOpen [WINMM.@]
2737 * All the args/structs have the same layout as the win16 equivalents
2739 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2740 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2741 DWORD_PTR dwInstance, DWORD dwFlags)
2743 LRESULT res;
2744 WINMM_OpenInfo info;
2745 WINMM_CBInfo cb_info;
2747 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2748 dwCallback, dwInstance, dwFlags);
2750 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2751 return MMSYSERR_INVALPARAM;
2753 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
2754 if(res != MMSYSERR_NOERROR)
2755 return res;
2757 if(!WINMM_StartDevicesThread())
2758 return MMSYSERR_NODRIVER;
2760 info.handle = 0;
2761 info.format = (WAVEFORMATEX*)lpFormat;
2762 info.callback = dwCallback;
2763 info.cb_user = dwInstance;
2764 info.req_device = uDeviceID;
2765 info.flags = dwFlags;
2766 info.reset = TRUE;
2768 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2769 InterlockedDecrement(&g_devthread_token);
2770 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
2771 return res;
2773 if(lphWaveOut)
2774 *lphWaveOut = (HWAVEOUT)info.handle;
2776 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2777 cb_info.callback = dwCallback;
2778 cb_info.user = dwInstance;
2779 cb_info.hwave = info.handle;
2781 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2783 return res;
2786 /**************************************************************************
2787 * waveOutClose [WINMM.@]
2789 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2791 UINT res;
2792 WINMM_Device *device;
2793 WINMM_CBInfo cb_info;
2795 TRACE("(%p)\n", hWaveOut);
2797 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2799 if(!WINMM_ValidateAndLock(device))
2800 return MMSYSERR_INVALHANDLE;
2802 cb_info = device->cb_info;
2804 LeaveCriticalSection(&device->lock);
2806 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2808 if(res == MMSYSERR_NOERROR)
2809 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2811 return res;
2814 /**************************************************************************
2815 * waveOutPrepareHeader [WINMM.@]
2817 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2818 WAVEHDR* lpWaveOutHdr, UINT uSize)
2820 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2822 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2823 return MMSYSERR_INVALPARAM;
2825 if(lpWaveOutHdr->dwFlags & WHDR_PREPARED)
2826 return MMSYSERR_NOERROR;
2828 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2831 /**************************************************************************
2832 * waveOutUnprepareHeader [WINMM.@]
2834 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2835 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2837 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2839 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2840 return MMSYSERR_INVALPARAM;
2842 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2843 return WAVERR_STILLPLAYING;
2845 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2846 return MMSYSERR_NOERROR;
2848 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2851 /**************************************************************************
2852 * waveOutWrite [WINMM.@]
2854 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2856 WINMM_Device *device;
2857 MMRESULT mr;
2859 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2861 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2863 if(!WINMM_ValidateAndLock(device))
2864 return MMSYSERR_INVALHANDLE;
2866 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2867 LeaveCriticalSection(&device->lock);
2868 return WAVERR_UNPREPARED;
2871 if(header->dwFlags & WHDR_INQUEUE){
2872 LeaveCriticalSection(&device->lock);
2873 return WAVERR_STILLPLAYING;
2876 TRACE("dwBufferLength: %u\n", header->dwBufferLength);
2878 if(device->acm_handle){
2879 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2881 ash->cbSrcLength = header->dwBufferLength;
2882 mr = acmStreamConvert(device->acm_handle, ash, 0);
2883 if(mr != MMSYSERR_NOERROR){
2884 LeaveCriticalSection(&device->lock);
2885 return mr;
2889 if(device->first){
2890 device->last->lpNext = header;
2891 device->last = header;
2892 if(!device->playing)
2893 device->playing = header;
2894 }else{
2895 device->playing = device->first = device->last = header;
2896 if(header->dwFlags & WHDR_BEGINLOOP){
2897 device->loop_counter = header->dwLoops;
2898 device->loop_start = header;
2902 header->lpNext = NULL;
2903 header->dwFlags &= ~WHDR_DONE;
2904 header->dwFlags |= WHDR_INQUEUE;
2906 mr = WINMM_BeginPlaying(device);
2908 LeaveCriticalSection(&device->lock);
2910 return mr;
2913 /**************************************************************************
2914 * waveOutBreakLoop [WINMM.@]
2916 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2918 WINMM_Device *device;
2920 TRACE("(%p)\n", hWaveOut);
2922 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2924 if(!WINMM_ValidateAndLock(device))
2925 return MMSYSERR_INVALHANDLE;
2927 device->loop_counter = 0;
2929 LeaveCriticalSection(&device->lock);
2931 return MMSYSERR_NOERROR;
2934 /**************************************************************************
2935 * waveOutPause [WINMM.@]
2937 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2939 TRACE("(%p)\n", hWaveOut);
2941 return WINMM_Pause((HWAVE)hWaveOut);
2944 /**************************************************************************
2945 * waveOutReset [WINMM.@]
2947 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2949 TRACE("(%p)\n", hWaveOut);
2951 return WINMM_Reset((HWAVE)hWaveOut);
2954 /**************************************************************************
2955 * waveOutRestart [WINMM.@]
2957 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2959 WINMM_Device *device;
2960 MMRESULT mr;
2962 TRACE("(%p)\n", hWaveOut);
2964 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2966 if(!WINMM_ValidateAndLock(device))
2967 return MMSYSERR_INVALHANDLE;
2969 device->stopped = TRUE;
2971 mr = WINMM_BeginPlaying(device);
2973 LeaveCriticalSection(&device->lock);
2975 return mr;
2978 /**************************************************************************
2979 * waveOutGetPosition [WINMM.@]
2981 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2982 UINT uSize)
2984 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2986 if(!uSize || !lpTime)
2987 return MMSYSERR_INVALPARAM;
2989 if(uSize < sizeof(MMTIME))
2990 return MMSYSERR_ERROR;
2992 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2995 /**************************************************************************
2996 * waveOutGetPitch [WINMM.@]
2998 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
3000 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3001 return MMSYSERR_NOTSUPPORTED;
3004 /**************************************************************************
3005 * waveOutSetPitch [WINMM.@]
3007 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
3009 TRACE("(%p, %08x)\n", hWaveOut, dw);
3011 return MMSYSERR_NOTSUPPORTED;
3014 /**************************************************************************
3015 * waveOutGetPlaybackRate [WINMM.@]
3017 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
3019 TRACE("(%p, %p)\n", hWaveOut, lpdw);
3021 return MMSYSERR_NOTSUPPORTED;
3024 /**************************************************************************
3025 * waveOutSetPlaybackRate [WINMM.@]
3027 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
3029 TRACE("(%p, %08x)\n", hWaveOut, dw);
3031 return MMSYSERR_NOTSUPPORTED;
3034 /**************************************************************************
3035 * waveOutGetVolume [WINMM.@]
3037 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
3039 WINMM_Device *device;
3040 UINT32 channels;
3041 float *vols;
3042 HRESULT hr;
3044 TRACE("(%p, %p)\n", hWaveOut, out);
3046 if(!out)
3047 return MMSYSERR_INVALPARAM;
3049 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3051 if(!WINMM_ValidateAndLock(device))
3052 return MMSYSERR_INVALHANDLE;
3054 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3055 if(FAILED(hr)){
3056 LeaveCriticalSection(&device->lock);
3057 WARN("GetChannelCount failed: %08x\n", hr);
3058 return MMSYSERR_ERROR;
3061 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3062 if(!vols){
3063 LeaveCriticalSection(&device->lock);
3064 return MMSYSERR_NOMEM;
3067 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3068 if(FAILED(hr)){
3069 LeaveCriticalSection(&device->lock);
3070 HeapFree(GetProcessHeap(), 0, vols);
3071 WARN("GetAllVolumes failed: %08x\n", hr);
3072 return MMSYSERR_ERROR;
3075 LeaveCriticalSection(&device->lock);
3077 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
3078 if(channels > 1)
3079 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
3081 HeapFree(GetProcessHeap(), 0, vols);
3083 return MMSYSERR_NOERROR;
3086 /**************************************************************************
3087 * waveOutSetVolume [WINMM.@]
3089 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
3091 WINMM_Device *device;
3092 UINT32 channels;
3093 float *vols;
3094 HRESULT hr;
3096 TRACE("(%p, %08x)\n", hWaveOut, in);
3098 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3100 if(!WINMM_ValidateAndLock(device))
3101 return MMSYSERR_INVALHANDLE;
3103 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
3104 if(FAILED(hr)){
3105 LeaveCriticalSection(&device->lock);
3106 WARN("GetChannelCount failed: %08x\n", hr);
3107 return MMSYSERR_ERROR;
3110 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
3111 if(!vols){
3112 LeaveCriticalSection(&device->lock);
3113 return MMSYSERR_NOMEM;
3116 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
3117 if(FAILED(hr)){
3118 LeaveCriticalSection(&device->lock);
3119 HeapFree(GetProcessHeap(), 0, vols);
3120 WARN("GetAllVolumes failed: %08x\n", hr);
3121 return MMSYSERR_ERROR;
3124 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
3125 if(channels > 1)
3126 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
3128 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
3129 if(FAILED(hr)){
3130 LeaveCriticalSection(&device->lock);
3131 HeapFree(GetProcessHeap(), 0, vols);
3132 WARN("SetAllVolumes failed: %08x\n", hr);
3133 return MMSYSERR_ERROR;
3136 LeaveCriticalSection(&device->lock);
3138 HeapFree(GetProcessHeap(), 0, vols);
3140 return MMSYSERR_NOERROR;
3143 /**************************************************************************
3144 * waveOutGetID [WINMM.@]
3146 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
3148 WINMM_Device *device;
3149 UINT dev, junk;
3150 BOOL is_out;
3152 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
3154 if(!lpuDeviceID)
3155 return MMSYSERR_INVALPARAM;
3157 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
3158 if(!WINMM_ValidateAndLock(device))
3159 return MMSYSERR_INVALHANDLE;
3161 LeaveCriticalSection(&device->lock);
3163 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
3165 return MMSYSERR_NOERROR;
3168 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
3170 UINT count;
3171 WINMM_MMDevice **devices;
3173 TRACE("(%u, %p, %d)\n", device, len, is_out);
3175 if(is_out){
3176 count = g_outmmdevices_count;
3177 devices = g_out_map;
3178 }else{
3179 count = g_inmmdevices_count;
3180 devices = g_in_map;
3183 if(device >= count)
3184 return MMSYSERR_INVALHANDLE;
3186 EnterCriticalSection(&g_devthread_lock);
3187 *len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3188 LeaveCriticalSection(&g_devthread_lock);
3190 return MMSYSERR_NOERROR;
3193 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
3194 BOOL is_out)
3196 UINT count, id_len;
3197 WINMM_MMDevice **devices;
3199 TRACE("(%u, %p, %d)\n", device, str, is_out);
3201 if(is_out){
3202 count = g_outmmdevices_count;
3203 devices = g_out_map;
3204 }else{
3205 count = g_inmmdevices_count;
3206 devices = g_in_map;
3209 if(device >= count)
3210 return MMSYSERR_INVALHANDLE;
3212 EnterCriticalSection(&g_devthread_lock);
3213 id_len = (lstrlenW(devices[device]->dev_id) + 1) * sizeof(WCHAR);
3214 if(len < id_len){
3215 LeaveCriticalSection(&g_devthread_lock);
3216 return MMSYSERR_ERROR;
3219 memcpy(str, devices[device]->dev_id, id_len);
3220 LeaveCriticalSection(&g_devthread_lock);
3222 return MMSYSERR_NOERROR;
3225 static UINT get_device_interface(UINT msg, BOOL is_out, UINT index, WCHAR *out, ULONG *out_len)
3227 WINMM_QueryInterfaceInfo info;
3228 UINT ret;
3230 if(!WINMM_StartDevicesThread())
3231 return MMSYSERR_NODRIVER;
3233 info.is_out = is_out;
3234 info.index = index;
3235 info.str = out;
3236 info.len_bytes = out_len;
3238 ret = SendMessageW(g_devices_hwnd, msg, (DWORD_PTR)&info, 0);
3239 InterlockedDecrement(&g_devthread_token);
3240 return ret;
3243 /**************************************************************************
3244 * waveOutMessage [WINMM.@]
3246 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
3247 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3249 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
3251 switch(uMessage){
3252 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3253 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
3254 (DWORD_PTR*)dwParam1, TRUE);
3255 case DRV_QUERYFUNCTIONINSTANCEID:
3256 return WINMM_QueryInstanceID(HandleToULong(hWaveOut), (WCHAR*)dwParam1, dwParam2, TRUE);
3257 case DRV_QUERYDEVICEINTERFACESIZE:
3258 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, TRUE, HandleToULong(hWaveOut),
3259 NULL, (ULONG*)dwParam1);
3260 case DRV_QUERYDEVICEINTERFACE:
3262 ULONG size = dwParam2;
3263 return get_device_interface(DRV_QUERYDEVICEINTERFACE, TRUE, HandleToULong(hWaveOut),
3264 (WCHAR*)dwParam1, &size);
3266 case DRV_QUERYMAPPABLE:
3267 return MMSYSERR_NOERROR;
3268 case DRVM_MAPPER_PREFERRED_GET:
3269 if(dwParam1) {
3270 if(g_outmmdevices_count > 0)
3271 /* Device 0 is always the default device */
3272 *(DWORD *)dwParam1 = 0;
3273 else
3274 *(DWORD *)dwParam1 = -1;
3277 if(dwParam2)
3278 /* Status flags */
3279 *(DWORD *)dwParam2 = 0;
3281 return MMSYSERR_NOERROR;
3284 TRACE("Message not supported: %u\n", uMessage);
3286 return MMSYSERR_NOTSUPPORTED;
3289 /**************************************************************************
3290 * waveInGetNumDevs [WINMM.@]
3292 UINT WINAPI waveInGetNumDevs(void)
3294 HRESULT hr = WINMM_InitMMDevices();
3295 if(FAILED(hr))
3296 return 0;
3298 TRACE("count: %u\n", g_inmmdevices_count);
3300 return g_inmmdevices_count;
3303 /**************************************************************************
3304 * waveInGetDevCapsW [WINMM.@]
3306 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
3308 WAVEINCAPSW mapper_caps, *caps;
3309 HRESULT hr;
3311 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3313 hr = WINMM_InitMMDevices();
3314 if(FAILED(hr))
3315 return MMSYSERR_NODRIVER;
3317 if(!lpCaps)
3318 return MMSYSERR_INVALPARAM;
3320 if(WINMM_IsMapper(uDeviceID)){
3321 /* FIXME: Should be localized */
3322 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
3323 'n','d',' ','M','a','p','p','e','r',0};
3325 mapper_caps.wMid = 0xFF;
3326 mapper_caps.wPid = 0xFF;
3327 mapper_caps.vDriverVersion = 0x00010001;
3328 mapper_caps.dwFormats = 0xFFFFFFFF;
3329 mapper_caps.wReserved1 = 0;
3330 mapper_caps.wChannels = 2;
3331 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
3333 caps = &mapper_caps;
3334 }else{
3335 if(uDeviceID >= g_inmmdevices_count)
3336 return MMSYSERR_BADDEVICEID;
3338 caps = &read_map(g_in_map, uDeviceID)->in_caps;
3341 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
3343 return MMSYSERR_NOERROR;
3346 /**************************************************************************
3347 * waveInGetDevCapsA [WINMM.@]
3349 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
3351 UINT ret;
3352 WAVEINCAPSW wicW;
3354 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3356 if(!lpCaps)
3357 return MMSYSERR_INVALPARAM;
3359 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
3361 if (ret == MMSYSERR_NOERROR) {
3362 WAVEINCAPSA wicA;
3363 wicA.wMid = wicW.wMid;
3364 wicA.wPid = wicW.wPid;
3365 wicA.vDriverVersion = wicW.vDriverVersion;
3366 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
3367 sizeof(wicA.szPname), NULL, NULL );
3368 wicA.dwFormats = wicW.dwFormats;
3369 wicA.wChannels = wicW.wChannels;
3370 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
3372 return ret;
3375 /**************************************************************************
3376 * waveInOpen [WINMM.@]
3378 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3379 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3380 DWORD_PTR dwInstance, DWORD dwFlags)
3382 LRESULT res;
3383 WINMM_OpenInfo info;
3384 WINMM_CBInfo cb_info;
3386 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3387 dwCallback, dwInstance, dwFlags);
3389 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3390 return MMSYSERR_INVALPARAM;
3392 res = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
3393 if(res != MMSYSERR_NOERROR)
3394 return res;
3396 if(!WINMM_StartDevicesThread())
3397 return MMSYSERR_NODRIVER;
3399 info.handle = 0;
3400 info.format = (WAVEFORMATEX*)lpFormat;
3401 info.callback = dwCallback;
3402 info.cb_user = dwInstance;
3403 info.req_device = uDeviceID;
3404 info.flags = dwFlags;
3405 info.reset = TRUE;
3407 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3408 InterlockedDecrement(&g_devthread_token);
3409 if(res != MMSYSERR_NOERROR || (dwFlags & WAVE_FORMAT_QUERY))
3410 return res;
3412 if(lphWaveIn)
3413 *lphWaveIn = (HWAVEIN)info.handle;
3415 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3416 cb_info.callback = dwCallback;
3417 cb_info.user = dwInstance;
3418 cb_info.hwave = info.handle;
3420 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3422 return res;
3425 /**************************************************************************
3426 * waveInClose [WINMM.@]
3428 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3430 WINMM_Device *device;
3431 WINMM_CBInfo cb_info;
3432 UINT res;
3434 TRACE("(%p)\n", hWaveIn);
3436 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3438 if(!WINMM_ValidateAndLock(device))
3439 return MMSYSERR_INVALHANDLE;
3441 cb_info = device->cb_info;
3443 LeaveCriticalSection(&device->lock);
3445 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3447 if(res == MMSYSERR_NOERROR)
3448 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3450 return res;
3453 /**************************************************************************
3454 * waveInPrepareHeader [WINMM.@]
3456 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3457 UINT uSize)
3459 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3461 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3462 return MMSYSERR_INVALPARAM;
3464 if(lpWaveInHdr->dwFlags & WHDR_PREPARED)
3465 return MMSYSERR_NOERROR;
3467 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3470 /**************************************************************************
3471 * waveInUnprepareHeader [WINMM.@]
3473 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3474 UINT uSize)
3476 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3478 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3479 return MMSYSERR_INVALPARAM;
3481 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3482 return WAVERR_STILLPLAYING;
3484 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3485 return MMSYSERR_NOERROR;
3487 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3490 /**************************************************************************
3491 * waveInAddBuffer [WINMM.@]
3493 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3495 WINMM_Device *device;
3497 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3499 if(!header || uSize < sizeof(WAVEHDR))
3500 return MMSYSERR_INVALPARAM;
3502 if(!(header->dwFlags & WHDR_PREPARED))
3503 return WAVERR_UNPREPARED;
3505 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3507 if(!WINMM_ValidateAndLock(device))
3508 return MMSYSERR_INVALHANDLE;
3510 if(!device->first)
3511 device->first = device->last = header;
3512 else{
3513 device->last->lpNext = header;
3514 device->last = header;
3517 header->dwBytesRecorded = 0;
3518 header->lpNext = NULL;
3519 header->dwFlags &= ~WHDR_DONE;
3520 header->dwFlags |= WHDR_INQUEUE;
3522 LeaveCriticalSection(&device->lock);
3524 return MMSYSERR_NOERROR;
3527 /**************************************************************************
3528 * waveInReset [WINMM.@]
3530 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3532 TRACE("(%p)\n", hWaveIn);
3534 return WINMM_Reset((HWAVE)hWaveIn);
3537 /**************************************************************************
3538 * waveInStart [WINMM.@]
3540 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3542 WINMM_Device *device;
3543 MMRESULT mr;
3545 TRACE("(%p)\n", hWaveIn);
3547 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3549 if(!WINMM_ValidateAndLock(device))
3550 return MMSYSERR_INVALHANDLE;
3552 mr = WINMM_BeginPlaying(device);
3554 LeaveCriticalSection(&device->lock);
3556 return mr;
3559 /**************************************************************************
3560 * waveInStop [WINMM.@]
3562 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3564 WINMM_CBInfo cb_info;
3565 WINMM_Device *device;
3566 WAVEHDR *buf;
3567 HRESULT hr;
3569 TRACE("(%p)\n", hWaveIn);
3571 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3573 if(!WINMM_ValidateAndLock(device))
3574 return MMSYSERR_INVALHANDLE;
3576 hr = WINMM_Pause((HWAVE)hWaveIn);
3577 if(FAILED(hr)){
3578 LeaveCriticalSection(&device->lock);
3579 return MMSYSERR_ERROR;
3581 device->stopped = TRUE;
3583 buf = device->first;
3584 if(buf && buf->dwBytesRecorded > 0){
3585 device->first = buf->lpNext;
3586 }else
3587 buf = NULL;
3589 cb_info = device->cb_info;
3591 LeaveCriticalSection(&device->lock);
3593 if(buf){
3594 buf->dwFlags &= ~WHDR_INQUEUE;
3595 buf->dwFlags |= WHDR_DONE;
3596 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3599 return MMSYSERR_NOERROR;
3602 /**************************************************************************
3603 * waveInGetPosition [WINMM.@]
3605 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3606 UINT uSize)
3608 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3610 if(!uSize || !lpTime)
3611 return MMSYSERR_INVALPARAM;
3613 if(uSize < sizeof(MMTIME))
3614 return MMSYSERR_ERROR;
3616 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3619 /**************************************************************************
3620 * waveInGetID [WINMM.@]
3622 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3624 UINT dev, junk;
3625 BOOL is_out;
3626 WINMM_Device *device;
3628 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3630 if(!lpuDeviceID)
3631 return MMSYSERR_INVALPARAM;
3633 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3634 if(!WINMM_ValidateAndLock(device))
3635 return MMSYSERR_INVALHANDLE;
3637 LeaveCriticalSection(&device->lock);
3639 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3641 return MMSYSERR_NOERROR;
3644 /**************************************************************************
3645 * waveInMessage [WINMM.@]
3647 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3648 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3650 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3652 switch(uMessage){
3653 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3654 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3655 (DWORD_PTR*)dwParam1, FALSE);
3656 case DRV_QUERYFUNCTIONINSTANCEID:
3657 return WINMM_QueryInstanceID(HandleToULong(hWaveIn), (WCHAR*)dwParam1, dwParam2, FALSE);
3658 case DRV_QUERYDEVICEINTERFACESIZE:
3659 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE, FALSE, HandleToULong(hWaveIn),
3660 NULL, (ULONG*)dwParam1);
3661 case DRV_QUERYDEVICEINTERFACE:
3663 ULONG size = dwParam2;
3664 return get_device_interface(DRV_QUERYDEVICEINTERFACE, FALSE, HandleToULong(hWaveIn),
3665 (WCHAR*)dwParam1, &size);
3667 case DRV_QUERYMAPPABLE:
3668 return MMSYSERR_NOERROR;
3669 case DRVM_MAPPER_PREFERRED_GET:
3670 if(dwParam1) {
3671 if(g_inmmdevices_count > 0)
3672 /* Device 0 is always the default device */
3673 *(DWORD *)dwParam1 = 0;
3674 else
3675 *(DWORD *)dwParam1 = -1;
3678 if(dwParam2)
3679 /* Status flags */
3680 *(DWORD *)dwParam2 = 0;
3682 return MMSYSERR_NOERROR;
3685 TRACE("Message not supported: %u\n", uMessage);
3687 return MMSYSERR_NOTSUPPORTED;
3690 UINT WINAPI mixerGetNumDevs(void)
3692 HRESULT hr;
3694 TRACE("\n");
3696 hr = WINMM_InitMMDevices();
3697 if(FAILED(hr))
3698 return 0;
3700 return g_outmmdevices_count + g_inmmdevices_count;
3703 /**************************************************************************
3704 * mixerGetDevCapsA [WINMM.@]
3706 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3708 MIXERCAPSW micW;
3709 UINT ret;
3711 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3713 if(!lpCaps)
3714 return MMSYSERR_INVALPARAM;
3716 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3718 if (ret == MMSYSERR_NOERROR) {
3719 MIXERCAPSA micA;
3720 micA.wMid = micW.wMid;
3721 micA.wPid = micW.wPid;
3722 micA.vDriverVersion = micW.vDriverVersion;
3723 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3724 sizeof(micA.szPname), NULL, NULL );
3725 micA.fdwSupport = micW.fdwSupport;
3726 micA.cDestinations = micW.cDestinations;
3727 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3729 return ret;
3732 /**************************************************************************
3733 * mixerGetDevCapsW [WINMM.@]
3735 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3737 WINMM_MMDevice *mmdevice;
3738 MIXERCAPSW caps;
3739 HRESULT hr;
3741 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3743 hr = WINMM_InitMMDevices();
3744 if(FAILED(hr))
3745 return MMSYSERR_NODRIVER;
3747 if(!lpCaps)
3748 return MMSYSERR_INVALPARAM;
3750 if(!uSize)
3751 return MMSYSERR_NOERROR;
3753 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3754 mmdevice = WINMM_GetMixerMMDevice((HMIXEROBJ)uDeviceID,
3755 MIXER_OBJECTF_MIXER, NULL);
3756 else if(uDeviceID < g_outmmdevices_count)
3757 mmdevice = read_map(g_out_map, uDeviceID);
3758 else
3759 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3761 if(!mmdevice)
3762 return MMSYSERR_BADDEVICEID;
3764 if(mmdevice->dataflow == eRender)
3765 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3766 else
3767 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3769 caps.wMid = 0xFF;
3770 caps.wPid = 0xFF;
3771 caps.vDriverVersion = 0x00010001;
3772 caps.fdwSupport = 0;
3773 caps.cDestinations = 1;
3775 memcpy(lpCaps, &caps, uSize);
3777 return MMSYSERR_NOERROR;
3780 /**************************************************************************
3781 * mixerOpen [WINMM.@]
3783 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3784 DWORD_PTR dwInstance, DWORD fdwOpen)
3786 WINMM_MMDevice *mmdevice;
3787 MMRESULT mr;
3788 HRESULT hr;
3790 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3791 dwInstance, fdwOpen);
3793 hr = WINMM_InitMMDevices();
3794 if(FAILED(hr))
3795 return MMSYSERR_NODRIVER;
3797 if(!lphMix)
3798 return MMSYSERR_INVALPARAM;
3800 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3801 if(mr != MMSYSERR_NOERROR)
3802 return mr;
3804 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3805 return MMSYSERR_BADDEVICEID;
3807 if(uDeviceID < g_outmmdevices_count){
3808 mmdevice = read_map(g_out_map, uDeviceID);
3809 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3810 mmdevice->mixer_count);
3811 }else{
3812 mmdevice = read_map(g_in_map, uDeviceID - g_outmmdevices_count);
3813 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3814 FALSE, mmdevice->mixer_count);
3817 ++mmdevice->mixer_count;
3819 return MMSYSERR_NOERROR;
3822 /**************************************************************************
3823 * mixerClose [WINMM.@]
3825 UINT WINAPI mixerClose(HMIXER hMix)
3827 TRACE("(%p)\n", hMix);
3829 return MMSYSERR_NOERROR;
3832 /**************************************************************************
3833 * mixerGetID [WINMM.@]
3835 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3837 WINMM_MMDevice *mmdevice;
3838 HRESULT hr;
3840 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3842 hr = WINMM_InitMMDevices();
3843 if(FAILED(hr))
3844 return MMSYSERR_NODRIVER;
3846 if(!lpid)
3847 return MMSYSERR_INVALPARAM;
3849 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3850 if(!mmdevice)
3851 return MMSYSERR_INVALHANDLE;
3853 if(mmdevice->in_caps.szPname[0] != '\0')
3854 *lpid += g_outmmdevices_count;
3856 return MMSYSERR_NOERROR;
3859 /**************************************************************************
3860 * mixerGetControlDetailsW [WINMM.@]
3862 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3863 DWORD fdwDetails)
3865 WINMM_ControlDetails details;
3867 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3869 if(!WINMM_StartDevicesThread())
3870 return MMSYSERR_NODRIVER;
3872 if(!lpmcdW || !lpmcdW->paDetails)
3873 return MMSYSERR_INVALPARAM;
3875 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3877 details.hmix = hmix;
3878 details.details = lpmcdW;
3879 details.flags = fdwDetails;
3881 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3882 (DWORD_PTR)&details, 0);
3885 /**************************************************************************
3886 * mixerGetControlDetailsA [WINMM.@]
3888 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3889 DWORD fdwDetails)
3891 UINT ret = MMSYSERR_NOTSUPPORTED;
3893 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3895 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3896 return MMSYSERR_INVALPARAM;
3898 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3899 case MIXER_GETCONTROLDETAILSF_VALUE:
3900 /* can safely use A structure as it is, no string inside */
3901 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3902 break;
3903 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3905 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3906 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3907 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3908 unsigned int i;
3910 if (lpmcdA->u.cMultipleItems != 0) {
3911 size *= lpmcdA->u.cMultipleItems;
3913 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3914 lpmcdA->paDetails = pDetailsW;
3915 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3916 /* set up lpmcd->paDetails */
3917 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3918 /* copy from lpmcd->paDetails back to paDetailsW; */
3919 if (ret == MMSYSERR_NOERROR) {
3920 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3921 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3922 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3923 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3924 pDetailsA->szName,
3925 sizeof(pDetailsA->szName), NULL, NULL );
3926 pDetailsA++;
3927 pDetailsW++;
3929 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3930 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3932 HeapFree(GetProcessHeap(), 0, pDetailsW);
3933 lpmcdA->paDetails = pDetailsA;
3934 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3936 break;
3937 default:
3938 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3941 return ret;
3944 /**************************************************************************
3945 * mixerGetLineControlsA [WINMM.@]
3947 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3948 DWORD fdwControls)
3950 MIXERLINECONTROLSW mlcW;
3951 DWORD ret;
3952 unsigned int i;
3954 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3956 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3957 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3958 return MMSYSERR_INVALPARAM;
3960 mlcW.cbStruct = sizeof(mlcW);
3961 mlcW.dwLineID = lpmlcA->dwLineID;
3962 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3963 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3965 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3966 the control count is assumed to be 1 - This is relied upon by a game,
3967 "Dynomite Deluze" */
3968 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3969 mlcW.cControls = 1;
3970 } else {
3971 mlcW.cControls = lpmlcA->cControls;
3973 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3974 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3975 mlcW.cControls * mlcW.cbmxctrl);
3977 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3979 if (ret == MMSYSERR_NOERROR) {
3980 lpmlcA->dwLineID = mlcW.dwLineID;
3981 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3982 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3984 for (i = 0; i < mlcW.cControls; i++) {
3985 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3986 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3987 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3988 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3989 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3990 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3991 lpmlcA->pamxctrl[i].szShortName,
3992 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3993 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3994 lpmlcA->pamxctrl[i].szName,
3995 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3996 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3997 * sizeof(mlcW.pamxctrl[i].Bounds) */
3998 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3999 sizeof(mlcW.pamxctrl[i].Bounds));
4000 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
4001 * sizeof(mlcW.pamxctrl[i].Metrics) */
4002 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
4003 sizeof(mlcW.pamxctrl[i].Metrics));
4007 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
4009 return ret;
4012 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4013 MIXERCONTROLW *ctl, DWORD flags)
4015 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
4016 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
4017 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4018 ctl->cMultipleItems = 0;
4019 lstrcpyW(ctl->szShortName, volumeW);
4020 lstrcpyW(ctl->szName, volumeW);
4021 ctl->Bounds.s1.dwMinimum = 0;
4022 ctl->Bounds.s1.dwMaximum = 0xFFFF;
4023 ctl->Metrics.cSteps = 192;
4025 return MMSYSERR_NOERROR;
4028 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
4029 MIXERCONTROLW *ctl, DWORD flags)
4031 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
4032 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
4033 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
4034 ctl->cMultipleItems = 0;
4035 lstrcpyW(ctl->szShortName, muteW);
4036 lstrcpyW(ctl->szName, muteW);
4037 ctl->Bounds.s1.dwMinimum = 0;
4038 ctl->Bounds.s1.dwMaximum = 1;
4039 ctl->Metrics.cSteps = 0;
4041 return MMSYSERR_NOERROR;
4044 /**************************************************************************
4045 * mixerGetLineControlsW [WINMM.@]
4047 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
4048 DWORD fdwControls)
4050 WINMM_MMDevice *mmdevice;
4051 HRESULT hr;
4053 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
4055 hr = WINMM_InitMMDevices();
4056 if(FAILED(hr))
4057 return MMSYSERR_NODRIVER;
4059 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
4060 MIXER_GETLINECONTROLSF_ONEBYID |
4061 MIXER_GETLINECONTROLSF_ONEBYTYPE |
4062 MIXER_OBJECTF_HMIXER |
4063 MIXER_OBJECTF_MIXER)){
4064 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
4065 return MMSYSERR_INVALFLAG;
4068 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
4069 return MMSYSERR_INVALPARAM;
4071 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
4072 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
4073 TRACE("cControls: %u\n", lpmlcW->cControls);
4075 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
4076 if(!mmdevice)
4077 return MMSYSERR_INVALHANDLE;
4079 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
4080 case MIXER_GETLINECONTROLSF_ALL:
4081 if(lpmlcW->cControls != 2)
4082 return MMSYSERR_INVALPARAM;
4083 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4084 return MMSYSERR_INVALPARAM;
4085 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4086 return MIXERR_INVALLINE;
4087 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4088 &lpmlcW->pamxctrl[0], fdwControls);
4089 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4090 &lpmlcW->pamxctrl[1], fdwControls);
4091 return MMSYSERR_NOERROR;
4092 case MIXER_GETLINECONTROLSF_ONEBYID:
4093 if(lpmlcW->cControls != 1)
4094 return MMSYSERR_INVALPARAM;
4095 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4096 return MMSYSERR_INVALPARAM;
4097 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4098 return MIXERR_INVALLINE;
4099 if(lpmlcW->u.dwControlID == 0)
4100 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4101 lpmlcW->pamxctrl, fdwControls);
4102 if(lpmlcW->u.dwControlID == 1)
4103 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4104 lpmlcW->pamxctrl, fdwControls);
4105 return MMSYSERR_NOTSUPPORTED;
4106 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
4107 if(lpmlcW->cControls != 1)
4108 return MMSYSERR_INVALPARAM;
4109 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
4110 return MMSYSERR_INVALPARAM;
4111 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
4112 return MIXERR_INVALLINE;
4113 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
4114 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
4115 lpmlcW->pamxctrl, fdwControls);
4116 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
4117 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
4118 lpmlcW->pamxctrl, fdwControls);
4119 return MMSYSERR_NOTSUPPORTED;
4122 return MMSYSERR_NOTSUPPORTED;
4125 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
4126 MIXERLINEW *info, DWORD flags)
4128 BOOL is_out = TRUE;
4129 if(mmdevice->in_caps.szPname[0] != '\0')
4130 is_out = FALSE;
4132 if(info->dwSource != 0)
4133 return MIXERR_INVALLINE;
4135 info->dwDestination = 0;
4136 info->dwLineID = 0;
4137 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
4138 info->cConnections = 0;
4139 info->cControls = 2;
4140 /* volume & mute always affect all channels, so claim 1 channel */
4141 info->cChannels = 1;
4142 info->Target.dwDeviceID = mmdev_index;
4143 info->Target.wMid = ~0;
4144 info->Target.wPid = ~0;
4145 info->Target.vDriverVersion = 0;
4147 lstrcpyW(info->szShortName, volumeW);
4148 lstrcpyW(info->szName, mastervolumeW);
4150 if(is_out){
4151 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
4152 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
4153 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
4154 sizeof(info->Target.szPname));
4155 }else{
4156 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
4157 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4158 info->Target.szPname[0] = '\0';
4161 return MMSYSERR_NOERROR;
4164 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
4165 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4167 BOOL is_out = TRUE;
4168 if(mmdevice->in_caps.szPname[0] != '\0')
4169 is_out = FALSE;
4171 if(info->dwDestination != 0)
4172 return MIXERR_INVALLINE;
4174 info->dwSource = 0xFFFFFFFF;
4175 info->dwLineID = 0xFFFF0000;
4176 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
4177 info->cConnections = 1;
4178 info->cControls = 2;
4180 lstrcpyW(info->szShortName, volumeW);
4181 lstrcpyW(info->szName, mastervolumeW);
4183 info->Target.dwDeviceID = mmdev_index;
4184 info->Target.wMid = ~0;
4185 info->Target.wPid = ~0;
4186 info->Target.vDriverVersion = 0;
4188 if(is_out){
4189 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
4190 info->cChannels = mmdevice->out_caps.wChannels;
4191 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
4192 info->Target.szPname[0] = '\0';
4193 }else{
4194 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
4195 info->cChannels = mmdevice->in_caps.wChannels;
4196 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
4197 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
4198 sizeof(info->Target.szPname));
4201 return MMSYSERR_NOERROR;
4204 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
4205 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4207 BOOL is_out = TRUE;
4208 if(mmdevice->in_caps.szPname[0] != '\0')
4209 is_out = FALSE;
4211 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
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_DST_SPEAKERS){
4219 if(!is_out)
4220 return MIXERR_INVALLINE;
4221 info->dwDestination = 0;
4222 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4225 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE){
4226 if(is_out)
4227 return MIXERR_INVALLINE;
4228 info->dwSource = 0;
4229 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4232 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
4233 if(!is_out)
4234 return MIXERR_INVALLINE;
4235 info->dwSource = 0;
4236 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4239 TRACE("Returning INVALLINE on this component type: %u\n",
4240 info->dwComponentType);
4242 return MIXERR_INVALLINE;
4245 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
4246 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
4248 if(info->dwLineID == 0xFFFF0000){
4249 info->dwDestination = 0;
4250 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
4253 if(info->dwLineID == 0){
4254 info->dwSource = 0;
4255 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
4258 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
4259 return MIXERR_INVALLINE;
4262 /**************************************************************************
4263 * mixerGetLineInfoW [WINMM.@]
4265 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
4267 UINT mmdev_index;
4268 WINMM_MMDevice *mmdevice;
4269 HRESULT hr;
4271 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
4273 hr = WINMM_InitMMDevices();
4274 if(FAILED(hr))
4275 return MMSYSERR_NODRIVER;
4277 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
4278 return MMSYSERR_INVALPARAM;
4280 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
4281 TRACE("dwSource: %u\n", lpmliW->dwSource);
4282 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
4283 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
4284 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
4286 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
4287 MIXER_GETLINEINFOF_DESTINATION |
4288 MIXER_GETLINEINFOF_LINEID |
4289 MIXER_GETLINEINFOF_SOURCE |
4290 MIXER_GETLINEINFOF_TARGETTYPE |
4291 MIXER_OBJECTF_HMIXER |
4292 MIXER_OBJECTF_MIXER)){
4293 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
4294 return MMSYSERR_INVALFLAG;
4297 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
4298 if(!mmdevice)
4299 return MMSYSERR_INVALHANDLE;
4301 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
4302 case MIXER_GETLINEINFOF_DESTINATION:
4303 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
4304 fdwInfo);
4305 case MIXER_GETLINEINFOF_SOURCE:
4306 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4307 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4308 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
4309 fdwInfo);
4310 case MIXER_GETLINEINFOF_LINEID:
4311 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
4312 case MIXER_GETLINEINFOF_TARGETTYPE:
4313 FIXME("TARGETTYPE flag not implemented!\n");
4314 return MIXERR_INVALLINE;
4317 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
4318 return MMSYSERR_INVALFLAG;
4321 /**************************************************************************
4322 * mixerGetLineInfoA [WINMM.@]
4324 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
4325 DWORD fdwInfo)
4327 MIXERLINEW mliW;
4328 UINT ret;
4330 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
4332 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
4333 return MMSYSERR_INVALPARAM;
4335 mliW.cbStruct = sizeof(mliW);
4336 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
4337 case MIXER_GETLINEINFOF_COMPONENTTYPE:
4338 mliW.dwComponentType = lpmliA->dwComponentType;
4339 break;
4340 case MIXER_GETLINEINFOF_DESTINATION:
4341 mliW.dwDestination = lpmliA->dwDestination;
4342 break;
4343 case MIXER_GETLINEINFOF_LINEID:
4344 mliW.dwLineID = lpmliA->dwLineID;
4345 break;
4346 case MIXER_GETLINEINFOF_SOURCE:
4347 mliW.dwDestination = lpmliA->dwDestination;
4348 mliW.dwSource = lpmliA->dwSource;
4349 break;
4350 case MIXER_GETLINEINFOF_TARGETTYPE:
4351 mliW.Target.dwType = lpmliA->Target.dwType;
4352 mliW.Target.wMid = lpmliA->Target.wMid;
4353 mliW.Target.wPid = lpmliA->Target.wPid;
4354 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
4355 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
4356 break;
4357 default:
4358 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
4359 return MMSYSERR_INVALFLAG;
4362 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
4364 if(ret == MMSYSERR_NOERROR)
4366 lpmliA->dwDestination = mliW.dwDestination;
4367 lpmliA->dwSource = mliW.dwSource;
4368 lpmliA->dwLineID = mliW.dwLineID;
4369 lpmliA->fdwLine = mliW.fdwLine;
4370 lpmliA->dwUser = mliW.dwUser;
4371 lpmliA->dwComponentType = mliW.dwComponentType;
4372 lpmliA->cChannels = mliW.cChannels;
4373 lpmliA->cConnections = mliW.cConnections;
4374 lpmliA->cControls = mliW.cControls;
4375 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
4376 sizeof(lpmliA->szShortName), NULL, NULL);
4377 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
4378 sizeof(lpmliA->szName), NULL, NULL );
4379 lpmliA->Target.dwType = mliW.Target.dwType;
4380 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
4381 lpmliA->Target.wMid = mliW.Target.wMid;
4382 lpmliA->Target.wPid = mliW.Target.wPid;
4383 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4384 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4385 sizeof(lpmliA->Target.szPname), NULL, NULL );
4387 return ret;
4390 /**************************************************************************
4391 * mixerSetControlDetails [WINMM.@]
4393 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4394 DWORD fdwDetails)
4396 WINMM_ControlDetails details;
4397 UINT ret;
4399 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4401 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4402 MIXER_SETCONTROLDETAILSF_CUSTOM)
4403 return MMSYSERR_NOTSUPPORTED;
4405 if(!lpmcd)
4406 return MMSYSERR_INVALPARAM;
4408 if(!WINMM_StartDevicesThread())
4409 return MMSYSERR_NODRIVER;
4411 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4413 details.hmix = hmix;
4414 details.details = lpmcd;
4415 details.flags = fdwDetails;
4417 ret = SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4418 (DWORD_PTR)&details, 0);
4419 InterlockedDecrement(&g_devthread_token);
4420 return ret;
4423 /**************************************************************************
4424 * mixerMessage [WINMM.@]
4426 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4428 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4430 return MMSYSERR_NOTSUPPORTED;