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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
43 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "audiopolicy.h"
47 #include "wine/debug.h"
49 /* TODO: Remove after dsound has been rewritten for mmdevapi */
52 #define DS_HW_ACCEL_FULL 0
54 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
56 /* FIXME: Should be localized */
57 static const WCHAR volumeW[] = {'V','o','l','u','m','e',0};
58 static const WCHAR mastervolumeW[] = {'M','a','s','t','e','r',' ','V','o','l',
60 static const WCHAR muteW[] = {'M','u','t','e',0};
62 /* HWAVE (and HMIXER) format:
64 * XXXX... 1FDD DDDD IIII IIII
65 * X = unused (must be 0)
66 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
67 * F = flow direction (0 = IN, 1 = OUT)
68 * D = index into g_out_mmdevices
69 * I = index in the mmdevice's devices array
71 * Two reasons that we don't just use pointers:
72 * - HWAVEs must fit into 16 bits for compatibility with old applications.
73 * - We must be able to identify bad devices without crashing.
76 #define MAX_DEVICES 256
78 typedef struct _WINMM_CBInfo {
85 struct _WINMM_MMDevice;
86 typedef struct _WINMM_MMDevice WINMM_MMDevice;
88 typedef struct _WINMM_Device {
97 IAudioRenderClient *render;
98 IAudioCaptureClient *capture;
100 IAudioStreamVolume *volume;
102 HACMSTREAM acm_handle;
103 ACMSTREAMHEADER acm_hdr;
106 WAVEHDR *first, *last, *playing, *loop_start;
110 UINT32 bytes_per_frame, samples_per_sec, ofs_bytes, played_frames;
112 /* stored in frames of sample rate, *not* AC::GetFrequency */
113 UINT64 last_clock_pos;
116 CRITICAL_SECTION lock;
118 WINMM_MMDevice *parent;
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*/
126 ISimpleAudioVolume *volume;
130 /* HMIXER format is the same as the HWAVE format, but the I bits are
131 * replaced by the value of this counter, to keep each HMIXER unique */
134 CRITICAL_SECTION lock;
136 WINMM_Device *devices[MAX_DEVICES];
139 static WINMM_MMDevice *g_out_mmdevices;
140 static UINT g_outmmdevices_count;
142 static WINMM_MMDevice *g_in_mmdevices;
143 static UINT g_inmmdevices_count;
145 static IMMDeviceEnumerator *g_devenum;
147 static CRITICAL_SECTION g_devthread_lock;
148 static HANDLE g_devices_thread;
149 static HWND g_devices_hwnd;
151 static UINT g_devhandle_count;
152 static HANDLE *g_device_handles;
153 static WINMM_Device **g_handle_devices;
155 typedef struct _WINMM_OpenInfo {
158 WAVEFORMATEX *format;
164 typedef struct _WINMM_ControlDetails {
166 MIXERCONTROLDETAILS *details;
168 } WINMM_ControlDetails;
170 static LRESULT WOD_Open(WINMM_OpenInfo *info);
171 static LRESULT WOD_Close(HWAVEOUT hwave);
172 static LRESULT WID_Open(WINMM_OpenInfo *info);
173 static LRESULT WID_Close(HWAVEIN hwave);
175 BOOL WINMM_InitWaveform(void)
177 InitializeCriticalSection(&g_devthread_lock);
181 static inline HWAVE WINMM_MakeHWAVE(UINT mmdevice, BOOL is_out, UINT device)
183 return ULongToHandle((1 << 15) | ((!!is_out) << 14) |
184 (mmdevice << 8) | device);
187 static inline void WINMM_DecomposeHWAVE(HWAVE hwave, UINT *mmdevice_index,
188 BOOL *is_out, UINT *device_index, UINT *junk)
190 ULONG32 l = HandleToULong(hwave);
191 *device_index = l & 0xFF;
192 *mmdevice_index = (l >> 8) & 0x3F;
193 *is_out = (l >> 14) & 0x1;
197 static void WINMM_InitDevice(WINMM_Device *device,
198 WINMM_MMDevice *parent, HWAVE hwave)
200 InitializeCriticalSection(&device->lock);
201 device->handle = hwave;
202 device->parent = parent;
205 /* finds the first unused Device, marks it as "open", and returns
206 * a pointer to the device
208 * IMPORTANT: it is the caller's responsibility to release the device's lock
211 static WINMM_Device *WINMM_FindUnusedDevice(BOOL is_out, UINT mmdevice_index)
213 WINMM_MMDevice *mmdevice;
217 mmdevice = &g_out_mmdevices[mmdevice_index];
219 mmdevice = &g_in_mmdevices[mmdevice_index];
221 EnterCriticalSection(&mmdevice->lock);
222 for(i = 0; i < MAX_DEVICES; ++i){
223 WINMM_Device *device = mmdevice->devices[i];
226 device = mmdevice->devices[i] = HeapAlloc(GetProcessHeap(),
227 HEAP_ZERO_MEMORY, sizeof(WINMM_Device));
229 LeaveCriticalSection(&mmdevice->lock);
233 WINMM_InitDevice(device, mmdevice,
234 WINMM_MakeHWAVE(mmdevice_index, is_out, i));
235 EnterCriticalSection(&device->lock);
237 EnterCriticalSection(&device->lock);
240 LeaveCriticalSection(&mmdevice->lock);
242 TRACE("Found free device: mmdevice: %u, device id: %u\n",
247 LeaveCriticalSection(&device->lock);
250 LeaveCriticalSection(&mmdevice->lock);
252 TRACE("All devices in use: mmdevice: %u\n", mmdevice_index);
257 static inline BOOL WINMM_ValidateAndLock(WINMM_Device *device)
262 EnterCriticalSection(&device->lock);
265 LeaveCriticalSection(&device->lock);
272 static WINMM_Device *WINMM_GetDeviceFromHWAVE(HWAVE hwave)
274 WINMM_MMDevice *mmdevice;
275 WINMM_Device *device;
276 UINT mmdevice_index, device_index, junk;
279 WINMM_DecomposeHWAVE(hwave, &mmdevice_index, &is_out, &device_index, &junk);
284 if(mmdevice_index >= (is_out ? g_outmmdevices_count : g_inmmdevices_count))
288 mmdevice = &g_out_mmdevices[mmdevice_index];
290 mmdevice = &g_in_mmdevices[mmdevice_index];
292 EnterCriticalSection(&mmdevice->lock);
294 device = mmdevice->devices[device_index];
296 LeaveCriticalSection(&mmdevice->lock);
301 /* Note: NotifyClient should never be called while holding the device lock
302 * since the client may call wave* functions from within the callback. */
303 static DWORD WINMM_NotifyClient(WINMM_CBInfo *info, WORD msg, DWORD_PTR param1,
306 TRACE("(%p, %u, %lx, %lx)\n", info->hwave, msg, param1, param2);
308 if((info->flags & DCB_TYPEMASK) == DCB_NULL)
309 return MMSYSERR_NOERROR;
311 if(!DriverCallback(info->callback, info->flags, (HDRVR)info->hwave,
312 msg, info->user, param1, param2))
313 return MMSYSERR_ERROR;
315 return MMSYSERR_NOERROR;
318 static HRESULT WINMM_GetFriendlyName(IMMDevice *device, WCHAR *out,
325 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
329 PropVariantInit(&var);
331 hr = IPropertyStore_GetValue(ps,
332 (PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &var);
334 IPropertyStore_Release(ps);
338 lstrcpynW(out, var.u.pwszVal, outlen);
340 PropVariantClear(&var);
342 IPropertyStore_Release(ps);
347 static HRESULT WINMM_TestFormat(IAudioClient *client, DWORD rate, DWORD depth,
350 WAVEFORMATEX fmt, *junk;
353 fmt.wFormatTag = WAVE_FORMAT_PCM;
354 fmt.nChannels = channels;
355 fmt.nSamplesPerSec = rate;
356 fmt.wBitsPerSample = depth;
357 fmt.nBlockAlign = (channels * depth) / 8;
358 fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign;
361 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED,
369 static struct _TestFormat {
374 } formats_to_test[] = {
375 { WAVE_FORMAT_1M08, 11025, 8, 1 },
376 { WAVE_FORMAT_1M16, 11025, 16, 1 },
377 { WAVE_FORMAT_1S08, 11025, 8, 2 },
378 { WAVE_FORMAT_1S16, 11025, 16, 2 },
379 { WAVE_FORMAT_2M08, 22050, 8, 1 },
380 { WAVE_FORMAT_2M16, 22050, 16, 1 },
381 { WAVE_FORMAT_2S08, 22050, 8, 2 },
382 { WAVE_FORMAT_2S16, 22050, 16, 2 },
383 { WAVE_FORMAT_4M08, 44100, 8, 1 },
384 { WAVE_FORMAT_4M16, 44100, 16, 1 },
385 { WAVE_FORMAT_4S08, 44100, 8, 2 },
386 { WAVE_FORMAT_4S16, 44100, 16, 2 },
387 { WAVE_FORMAT_48M08, 48000, 8, 1 },
388 { WAVE_FORMAT_48M16, 48000, 16, 1 },
389 { WAVE_FORMAT_48S08, 48000, 8, 2 },
390 { WAVE_FORMAT_48S16, 48000, 16, 2 },
391 { WAVE_FORMAT_96M08, 96000, 8, 1 },
392 { WAVE_FORMAT_96M16, 96000, 16, 1 },
393 { WAVE_FORMAT_96S08, 96000, 8, 2 },
394 { WAVE_FORMAT_96S16, 96000, 16, 2 },
398 static DWORD WINMM_GetSupportedFormats(IMMDevice *device)
402 struct _TestFormat *fmt;
403 IAudioClient *client;
405 hr = IMMDevice_Activate(device, &IID_IAudioClient,
406 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
410 for(fmt = formats_to_test; fmt->flag; ++fmt){
411 hr = WINMM_TestFormat(client, fmt->rate, fmt->depth, fmt->channels);
416 IAudioClient_Release(client);
421 static HRESULT WINMM_InitMMDevice(EDataFlow flow, IMMDevice *device,
422 WINMM_MMDevice *dev, UINT index)
427 dev->out_caps.wMid = 0xFF;
428 dev->out_caps.wPid = 0xFF;
429 dev->out_caps.vDriverVersion = 0x00010001;
430 dev->out_caps.dwFormats = WINMM_GetSupportedFormats(device);
431 dev->out_caps.wReserved1 = 0;
432 dev->out_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
433 WAVECAPS_SAMPLEACCURATE;
434 dev->out_caps.wChannels = 2;
435 dev->out_caps.szPname[0] = '\0';
437 hr = WINMM_GetFriendlyName(device, dev->out_caps.szPname,
438 sizeof(dev->out_caps.szPname) /
439 sizeof(*dev->out_caps.szPname));
443 dev->in_caps.wMid = 0xFF;
444 dev->in_caps.wPid = 0xFF;
445 dev->in_caps.vDriverVersion = 0x00010001;
446 dev->in_caps.dwFormats = WINMM_GetSupportedFormats(device);
447 dev->in_caps.wReserved1 = 0;
448 dev->in_caps.wChannels = 2;
449 dev->in_caps.szPname[0] = '\0';
451 hr = WINMM_GetFriendlyName(device, dev->in_caps.szPname,
452 sizeof(dev->in_caps.szPname) /
453 sizeof(*dev->in_caps.szPname));
458 hr = IMMDevice_GetId(device, &dev->dev_id);
462 CoCreateGuid(&dev->session);
464 InitializeCriticalSection(&dev->lock);
469 static HRESULT WINMM_EnumDevices(WINMM_MMDevice **devices, UINT *devcount,
472 IMMDeviceCollection *devcoll;
475 hr = IMMDeviceEnumerator_EnumAudioEndpoints(g_devenum, flow,
476 DEVICE_STATE_ACTIVE, &devcoll);
480 hr = IMMDeviceCollection_GetCount(devcoll, devcount);
482 IMMDeviceCollection_Release(devcoll);
488 IMMDevice *def_dev = NULL;
490 *devices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
491 sizeof(WINMM_MMDevice) * (*devcount));
493 IMMDeviceCollection_Release(devcoll);
494 return E_OUTOFMEMORY;
499 /* make sure that device 0 is the default device */
500 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(g_devenum,
501 flow, eConsole, &def_dev);
503 WINMM_InitMMDevice(flow, def_dev, &(*devices)[0], 0);
507 for(n = 0; n < *devcount; ++n){
510 hr = IMMDeviceCollection_Item(devcoll, n, &device);
512 if(device != def_dev){
513 WINMM_InitMMDevice(flow, device, &(*devices)[count], count);
517 IMMDevice_Release(device);
522 IMMDevice_Release(def_dev);
527 IMMDeviceCollection_Release(devcoll);
532 static HRESULT WINMM_InitMMDevices(void)
536 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
537 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&g_devenum);
541 hr = WINMM_EnumDevices(&g_out_mmdevices, &g_outmmdevices_count, eRender);
543 g_outmmdevices_count = 0;
544 g_inmmdevices_count = 0;
548 hr = WINMM_EnumDevices(&g_in_mmdevices, &g_inmmdevices_count, eCapture);
550 g_inmmdevices_count = 0;
557 static inline BOOL WINMM_IsMapper(UINT device)
559 return (device == WAVE_MAPPER || device == (UINT16)WAVE_MAPPER);
562 static MMRESULT WINMM_TryDeviceMapping(WINMM_OpenInfo *info, WORD channels,
563 DWORD freq, DWORD bits_per_samp, BOOL is_out)
565 WINMM_Device *device;
570 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
571 WAVE_FORMAT_PCM, channels, freq, bits_per_samp);
573 target.wFormatTag = WAVE_FORMAT_PCM;
574 target.nChannels = channels;
575 target.nSamplesPerSec = freq;
576 target.wBitsPerSample = bits_per_samp;
577 target.nBlockAlign = (target.nChannels * target.wBitsPerSample) / 8;
578 target.nAvgBytesPerSec = target.nSamplesPerSec * target.nBlockAlign;
582 mr = acmStreamOpen(NULL, NULL, info->format, &target, NULL, 0,
583 0, ACM_STREAMOPENF_QUERY);
585 mr = acmStreamOpen(NULL, NULL, &target, info->format, NULL, 0,
586 0, ACM_STREAMOPENF_QUERY);
587 if(mr != MMSYSERR_NOERROR)
590 /* ACM can convert from src->dst, so try to find a device
591 * that supports dst */
593 if(WINMM_IsMapper(info->req_device)){
594 for(i = 0; i < g_outmmdevices_count; ++i){
595 WINMM_OpenInfo l_info = *info;
596 l_info.req_device = i;
597 l_info.format = ⌖
598 mr = WOD_Open(&l_info);
599 if(mr == MMSYSERR_NOERROR){
600 info->handle = l_info.handle;
605 WINMM_OpenInfo l_info = *info;
606 l_info.flags &= ~WAVE_MAPPED;
607 l_info.format = ⌖
608 mr = WOD_Open(&l_info);
609 if(mr == MMSYSERR_NOERROR)
610 info->handle = l_info.handle;
613 if(WINMM_IsMapper(info->req_device)){
614 for(i = 0; i < g_inmmdevices_count; ++i){
615 WINMM_OpenInfo l_info = *info;
616 l_info.req_device = i;
617 l_info.format = ⌖
618 mr = WID_Open(&l_info);
619 if(mr == MMSYSERR_NOERROR){
620 info->handle = l_info.handle;
625 WINMM_OpenInfo l_info = *info;
626 l_info.flags &= ~WAVE_MAPPED;
627 l_info.format = ⌖
628 mr = WID_Open(&l_info);
629 if(mr == MMSYSERR_NOERROR)
630 info->handle = l_info.handle;
633 if(mr != MMSYSERR_NOERROR)
634 return WAVERR_BADFORMAT;
636 device = WINMM_GetDeviceFromHWAVE((HWAVE)info->handle);
638 return MMSYSERR_INVALHANDLE;
640 /* set up the ACM stream */
642 mr = acmStreamOpen(&device->acm_handle, NULL, info->format, &target,
645 mr = acmStreamOpen(&device->acm_handle, NULL, &target, info->format,
647 if(mr != MMSYSERR_NOERROR){
649 WOD_Close((HWAVEOUT)info->handle);
651 WID_Close((HWAVEIN)info->handle);
656 return MMSYSERR_NOERROR;
659 static MMRESULT WINMM_MapDevice(WINMM_OpenInfo *info, BOOL is_out)
663 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)info->format;
665 TRACE("(%p, %d)\n", info, is_out);
667 /* try to find a direct match */
669 WINMM_OpenInfo l_info = *info;
670 if(WINMM_IsMapper(info->req_device)){
671 for(i = 0; i < g_outmmdevices_count; ++i){
672 l_info.req_device = i;
673 mr = WOD_Open(&l_info);
674 if(mr == MMSYSERR_NOERROR){
675 info->handle = l_info.handle;
680 l_info.flags &= ~WAVE_MAPPED;
681 mr = WOD_Open(&l_info);
682 if(mr == MMSYSERR_NOERROR){
683 info->handle = l_info.handle;
688 WINMM_OpenInfo l_info = *info;
689 if(WINMM_IsMapper(info->req_device)){
690 for(i = 0; i < g_inmmdevices_count; ++i){
691 l_info.req_device = i;
692 mr = WID_Open(&l_info);
693 if(mr == MMSYSERR_NOERROR){
694 info->handle = l_info.handle;
699 l_info.flags &= ~WAVE_MAPPED;
700 mr = WID_Open(&l_info);
701 if(mr == MMSYSERR_NOERROR){
702 info->handle = l_info.handle;
708 /* no direct match, so set up the ACM stream */
709 if(info->format->wFormatTag != WAVE_FORMAT_PCM ||
710 (info->format->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
711 !IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
712 /* convert to PCM format if it's not already */
713 mr = WINMM_TryDeviceMapping(info, info->format->nChannels,
714 info->format->nSamplesPerSec, 16, is_out);
715 if(mr == MMSYSERR_NOERROR)
718 mr = WINMM_TryDeviceMapping(info, info->format->nChannels,
719 info->format->nSamplesPerSec, 8, is_out);
720 if(mr == MMSYSERR_NOERROR)
725 /* first try just changing bit depth and channels */
726 channels = info->format->nChannels;
727 mr = WINMM_TryDeviceMapping(info, channels,
728 info->format->nSamplesPerSec, 16, is_out);
729 if(mr == MMSYSERR_NOERROR)
731 mr = WINMM_TryDeviceMapping(info, channels,
732 info->format->nSamplesPerSec, 8, is_out);
733 if(mr == MMSYSERR_NOERROR)
736 channels = (channels == 2) ? 1 : 2;
737 mr = WINMM_TryDeviceMapping(info, channels,
738 info->format->nSamplesPerSec, 16, is_out);
739 if(mr == MMSYSERR_NOERROR)
741 mr = WINMM_TryDeviceMapping(info, channels,
742 info->format->nSamplesPerSec, 8, is_out);
743 if(mr == MMSYSERR_NOERROR)
746 /* that didn't work, so now try different sample rates */
747 channels = info->format->nChannels;
748 mr = WINMM_TryDeviceMapping(info, channels, 96000, 16, is_out);
749 if(mr == MMSYSERR_NOERROR)
751 mr = WINMM_TryDeviceMapping(info, channels, 48000, 16, is_out);
752 if(mr == MMSYSERR_NOERROR)
754 mr = WINMM_TryDeviceMapping(info, channels, 44100, 16, is_out);
755 if(mr == MMSYSERR_NOERROR)
757 mr = WINMM_TryDeviceMapping(info, channels, 22050, 16, is_out);
758 if(mr == MMSYSERR_NOERROR)
760 mr = WINMM_TryDeviceMapping(info, channels, 11025, 16, is_out);
761 if(mr == MMSYSERR_NOERROR)
764 channels = (channels == 2) ? 1 : 2;
765 mr = WINMM_TryDeviceMapping(info, channels, 96000, 16, is_out);
766 if(mr == MMSYSERR_NOERROR)
768 mr = WINMM_TryDeviceMapping(info, channels, 48000, 16, is_out);
769 if(mr == MMSYSERR_NOERROR)
771 mr = WINMM_TryDeviceMapping(info, channels, 44100, 16, is_out);
772 if(mr == MMSYSERR_NOERROR)
774 mr = WINMM_TryDeviceMapping(info, channels, 22050, 16, is_out);
775 if(mr == MMSYSERR_NOERROR)
777 mr = WINMM_TryDeviceMapping(info, channels, 11025, 16, is_out);
778 if(mr == MMSYSERR_NOERROR)
781 channels = info->format->nChannels;
782 mr = WINMM_TryDeviceMapping(info, channels, 96000, 8, is_out);
783 if(mr == MMSYSERR_NOERROR)
785 mr = WINMM_TryDeviceMapping(info, channels, 48000, 8, is_out);
786 if(mr == MMSYSERR_NOERROR)
788 mr = WINMM_TryDeviceMapping(info, channels, 44100, 8, is_out);
789 if(mr == MMSYSERR_NOERROR)
791 mr = WINMM_TryDeviceMapping(info, channels, 22050, 8, is_out);
792 if(mr == MMSYSERR_NOERROR)
794 mr = WINMM_TryDeviceMapping(info, channels, 11025, 8, is_out);
795 if(mr == MMSYSERR_NOERROR)
798 channels = (channels == 2) ? 1 : 2;
799 mr = WINMM_TryDeviceMapping(info, channels, 96000, 8, is_out);
800 if(mr == MMSYSERR_NOERROR)
802 mr = WINMM_TryDeviceMapping(info, channels, 48000, 8, is_out);
803 if(mr == MMSYSERR_NOERROR)
805 mr = WINMM_TryDeviceMapping(info, channels, 44100, 8, is_out);
806 if(mr == MMSYSERR_NOERROR)
808 mr = WINMM_TryDeviceMapping(info, channels, 22050, 8, is_out);
809 if(mr == MMSYSERR_NOERROR)
811 mr = WINMM_TryDeviceMapping(info, channels, 11025, 8, is_out);
812 if(mr == MMSYSERR_NOERROR)
816 WARN("Unable to find compatible device!\n");
817 return WAVERR_BADFORMAT;
820 static LRESULT WINMM_OpenDevice(WINMM_Device *device, WINMM_MMDevice *mmdevice,
821 WINMM_OpenInfo *info)
823 WAVEFORMATEX *closer_fmt = NULL, *passed_fmt;
824 LRESULT ret = MMSYSERR_ERROR;
827 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id,
830 ERR("Device %s (%s) unavailable: %08x\n",
831 wine_dbgstr_w(mmdevice->dev_id),
832 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
836 hr = IMMDevice_Activate(device->device, &IID_IAudioClient,
837 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
839 ERR("Activate failed: %08x\n", hr);
843 if(info->format->wFormatTag == WAVE_FORMAT_PCM){
844 /* we aren't guaranteed that the struct in lpFormat is a full
845 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
846 passed_fmt = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
847 memcpy(passed_fmt, info->format, sizeof(PCMWAVEFORMAT));
848 passed_fmt->cbSize = 0;
850 passed_fmt = info->format;
852 hr = IAudioClient_IsFormatSupported(device->client,
853 AUDCLNT_SHAREMODE_SHARED, passed_fmt, &closer_fmt);
855 CoTaskMemFree(closer_fmt);
856 if(FAILED(hr) && hr != AUDCLNT_E_UNSUPPORTED_FORMAT){
857 if(info->format->wFormatTag == WAVE_FORMAT_PCM)
858 HeapFree(GetProcessHeap(), 0, passed_fmt);
859 ERR("IsFormatSupported failed: %08x\n", hr);
862 if(hr == S_FALSE || hr == AUDCLNT_E_UNSUPPORTED_FORMAT){
863 if(info->format->wFormatTag == WAVE_FORMAT_PCM)
864 HeapFree(GetProcessHeap(), 0, passed_fmt);
865 ret = WAVERR_BADFORMAT;
868 if(info->flags & WAVE_FORMAT_QUERY){
869 if(info->format->wFormatTag == WAVE_FORMAT_PCM)
870 HeapFree(GetProcessHeap(), 0, passed_fmt);
871 ret = MMSYSERR_NOERROR;
875 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
876 hr = IAudioClient_Initialize(device->client, AUDCLNT_SHAREMODE_SHARED,
877 AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
878 10 * 100000, 50000, passed_fmt, &device->parent->session);
879 if(info->format->wFormatTag == WAVE_FORMAT_PCM)
880 HeapFree(GetProcessHeap(), 0, passed_fmt);
882 ERR("Initialize failed: %08x\n", hr);
886 hr = IAudioClient_GetService(device->client, &IID_IAudioClock,
887 (void**)&device->clock);
889 ERR("GetService failed: %08x\n", hr);
894 device->event = CreateEventW(NULL, FALSE, FALSE, NULL);
896 ERR("CreateEvent failed: %08x\n", hr);
900 if(g_device_handles){
901 g_device_handles = HeapReAlloc(GetProcessHeap(), 0, g_device_handles,
902 sizeof(HANDLE) * (g_devhandle_count + 1));
903 g_handle_devices = HeapReAlloc(GetProcessHeap(), 0, g_handle_devices,
904 sizeof(WINMM_Device *) * (g_devhandle_count + 1));
906 g_device_handles = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE));
907 g_handle_devices = HeapAlloc(GetProcessHeap(), 0,
908 sizeof(WINMM_Device *));
910 g_device_handles[g_devhandle_count] = device->event;
911 g_handle_devices[g_devhandle_count] = device;
915 hr = IAudioClient_SetEventHandle(device->client, device->event);
917 ERR("SetEventHandle failed: %08x\n", hr);
921 device->bytes_per_frame = info->format->nBlockAlign;
922 device->samples_per_sec = info->format->nSamplesPerSec;
924 device->played_frames = 0;
925 device->last_clock_pos = 0;
926 device->ofs_bytes = 0;
927 device->loop_counter = 0;
928 device->stopped = TRUE;
929 device->first = device->last = device->playing = device->loop_start = NULL;
931 device->cb_info.flags = HIWORD(info->flags & CALLBACK_TYPEMASK);
932 device->cb_info.callback = info->callback;
933 device->cb_info.user = info->cb_user;
934 device->cb_info.hwave = (HWAVE)device->handle;
936 info->handle = device->handle;
938 return MMSYSERR_NOERROR;
942 IAudioClient_Release(device->client);
943 device->client = NULL;
946 IMMDevice_Release(device->device);
947 device->device = NULL;
953 static LRESULT WOD_Open(WINMM_OpenInfo *info)
955 WINMM_MMDevice *mmdevice;
956 WINMM_Device *device = NULL;
957 WINMM_CBInfo cb_info;
958 LRESULT ret = MMSYSERR_ERROR;
961 TRACE("(%u, %p, %08x)\n", info->req_device, info, info->flags);
963 if(WINMM_IsMapper(info->req_device) || (info->flags & WAVE_MAPPED))
964 return WINMM_MapDevice(info, TRUE);
966 if(info->req_device >= g_outmmdevices_count)
967 return MMSYSERR_BADDEVICEID;
969 mmdevice = &g_out_mmdevices[info->req_device];
971 if(!mmdevice->out_caps.szPname[0])
972 return MMSYSERR_NOTENABLED;
974 device = WINMM_FindUnusedDevice(TRUE, info->req_device);
976 return MMSYSERR_ALLOCATED;
978 ret = WINMM_OpenDevice(device, mmdevice, info);
979 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
981 ret = MMSYSERR_ERROR;
983 hr = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
984 (void**)&device->render);
986 ERR("GetService failed: %08x\n", hr);
990 hr = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
991 (void**)&device->volume);
993 ERR("GetService failed: %08x\n", hr);
997 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
999 LeaveCriticalSection(&device->lock);
1001 return MMSYSERR_NOERROR;
1005 IMMDevice_Release(device->device);
1006 device->device = NULL;
1009 IAudioClient_Release(device->client);
1010 device->client = NULL;
1013 IAudioRenderClient_Release(device->render);
1014 device->render = NULL;
1017 IAudioStreamVolume_Release(device->volume);
1018 device->volume = NULL;
1021 IAudioClock_Release(device->clock);
1022 device->clock = NULL;
1024 device->open = FALSE;
1025 LeaveCriticalSection(&device->lock);
1029 static LRESULT WID_Open(WINMM_OpenInfo *info)
1031 WINMM_MMDevice *mmdevice;
1032 WINMM_Device *device = NULL;
1033 WINMM_CBInfo cb_info;
1034 LRESULT ret = MMSYSERR_ERROR;
1037 TRACE("(%u, %p, %08x)\n", info->req_device, info, info->flags);
1039 if(WINMM_IsMapper(info->req_device) || info->flags & WAVE_MAPPED)
1040 return WINMM_MapDevice(info, FALSE);
1042 if(info->req_device >= g_inmmdevices_count)
1043 return MMSYSERR_BADDEVICEID;
1045 mmdevice = &g_in_mmdevices[info->req_device];
1047 if(!mmdevice->in_caps.szPname[0])
1048 return MMSYSERR_NOTENABLED;
1050 device = WINMM_FindUnusedDevice(FALSE, info->req_device);
1052 return MMSYSERR_ALLOCATED;
1054 ret = WINMM_OpenDevice(device, mmdevice, info);
1055 if((info->flags & WAVE_FORMAT_QUERY) || ret != MMSYSERR_NOERROR)
1057 ret = MMSYSERR_ERROR;
1059 hr = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
1060 (void**)&device->capture);
1062 ERR("GetService failed: %08x\n", hr);
1066 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
1068 LeaveCriticalSection(&device->lock);
1070 return MMSYSERR_NOERROR;
1074 IMMDevice_Release(device->device);
1075 device->device = NULL;
1078 IAudioClient_Release(device->client);
1079 device->client = NULL;
1081 if(device->capture){
1082 IAudioCaptureClient_Release(device->capture);
1083 device->capture = NULL;
1086 IAudioClock_Release(device->clock);
1087 device->clock = NULL;
1089 device->open = FALSE;
1090 LeaveCriticalSection(&device->lock);
1094 static HRESULT WINMM_CloseDevice(WINMM_Device *device)
1096 device->open = FALSE;
1098 if(!device->stopped){
1099 IAudioClient_Stop(device->client);
1100 device->stopped = TRUE;
1103 if(device->acm_handle){
1104 acmStreamClose(device->acm_handle, 0);
1105 device->acm_handle = NULL;
1108 IMMDevice_Release(device->device);
1109 device->device = NULL;
1111 IAudioClient_Release(device->client);
1112 device->client = NULL;
1114 IAudioClock_Release(device->clock);
1115 device->clock = NULL;
1120 static LRESULT WOD_Close(HWAVEOUT hwave)
1122 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1123 WINMM_CBInfo cb_info;
1125 TRACE("(%p)\n", hwave);
1127 if(!WINMM_ValidateAndLock(device))
1128 return MMSYSERR_INVALHANDLE;
1130 WINMM_CloseDevice(device);
1132 IAudioRenderClient_Release(device->render);
1133 device->render = NULL;
1135 IAudioStreamVolume_Release(device->volume);
1136 device->volume = NULL;
1138 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
1140 LeaveCriticalSection(&device->lock);
1142 return MMSYSERR_NOERROR;
1145 static LRESULT WID_Close(HWAVEIN hwave)
1147 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1148 WINMM_CBInfo cb_info;
1150 TRACE("(%p)\n", hwave);
1152 if(!WINMM_ValidateAndLock(device))
1153 return MMSYSERR_INVALHANDLE;
1155 WINMM_CloseDevice(device);
1157 IAudioCaptureClient_Release(device->capture);
1158 device->capture = NULL;
1160 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
1162 LeaveCriticalSection(&device->lock);
1164 return MMSYSERR_NOERROR;
1167 static LRESULT WINMM_PrepareHeader(HWAVE hwave, WAVEHDR *header)
1169 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1171 TRACE("(%p, %p)\n", hwave, header);
1173 if(!WINMM_ValidateAndLock(device))
1174 return MMSYSERR_INVALHANDLE;
1176 if(device->render && device->acm_handle){
1177 ACMSTREAMHEADER *ash;
1181 mr = acmStreamSize(device->acm_handle, header->dwBufferLength, &size,
1182 ACM_STREAMSIZEF_SOURCE);
1183 if(mr != MMSYSERR_NOERROR){
1184 LeaveCriticalSection(&device->lock);
1188 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + size);
1190 LeaveCriticalSection(&device->lock);
1191 return MMSYSERR_NOMEM;
1194 ash->cbStruct = sizeof(*ash);
1196 ash->dwUser = (DWORD_PTR)header;
1197 ash->pbSrc = (BYTE*)header->lpData;
1198 ash->cbSrcLength = header->dwBufferLength;
1199 ash->dwSrcUser = header->dwUser;
1200 ash->pbDst = (BYTE*)ash + sizeof(ACMSTREAMHEADER);
1201 ash->cbDstLength = size;
1204 mr = acmStreamPrepareHeader(device->acm_handle, ash, 0);
1205 if(mr != MMSYSERR_NOERROR){
1206 LeaveCriticalSection(&device->lock);
1210 header->reserved = (DWORD_PTR)ash;
1213 LeaveCriticalSection(&device->lock);
1215 header->dwFlags |= WHDR_PREPARED;
1216 header->dwFlags &= ~WHDR_DONE;
1218 return MMSYSERR_NOERROR;
1221 static LRESULT WINMM_UnprepareHeader(HWAVE hwave, WAVEHDR *header)
1223 WINMM_Device *device = WINMM_GetDeviceFromHWAVE(hwave);
1225 TRACE("(%p, %p)\n", hwave, header);
1227 if(!WINMM_ValidateAndLock(device))
1228 return MMSYSERR_INVALHANDLE;
1230 if(device->render && device->acm_handle){
1231 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1233 acmStreamUnprepareHeader(device->acm_handle, ash, 0);
1235 HeapFree(GetProcessHeap(), 0, ash);
1238 LeaveCriticalSection(&device->lock);
1240 header->dwFlags &= ~WHDR_PREPARED;
1241 header->dwFlags |= WHDR_DONE;
1243 return MMSYSERR_NOERROR;
1246 static UINT32 WINMM_HeaderLenBytes(WINMM_Device *device, WAVEHDR *header)
1248 if(device->acm_handle){
1249 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
1250 return ash->cbDstLengthUsed;
1253 return header->dwBufferLength;
1256 static UINT32 WINMM_HeaderLenFrames(WINMM_Device *device, WAVEHDR *header)
1258 return WINMM_HeaderLenBytes(device, header) / device->bytes_per_frame;
1261 static WAVEHDR *WOD_MarkDoneHeaders(WINMM_Device *device)
1264 WAVEHDR *queue, *first = device->first;
1265 UINT64 clock_freq, clock_pos, clock_frames;
1266 UINT32 nloops, queue_frames = 0;
1268 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
1270 ERR("GetFrequency failed: %08x\n", hr);
1274 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
1276 ERR("GetPosition failed: %08x\n", hr);
1280 clock_frames = (clock_pos / (double)clock_freq) * device->samples_per_sec;
1282 first = queue = device->first;
1283 nloops = device->loop_counter;
1285 (queue_frames += WINMM_HeaderLenFrames(device, queue)) <=
1286 clock_frames - device->last_clock_pos){
1288 device->first->dwFlags &= ~WHDR_INQUEUE;
1289 device->first->dwFlags |= WHDR_DONE;
1290 device->last_clock_pos += queue_frames;
1292 queue = device->first = queue->lpNext;
1294 if(queue->dwFlags & WHDR_BEGINLOOP){
1295 if(queue->dwFlags & WHDR_ENDLOOP)
1298 queue = queue->lpNext;
1299 }else if(queue->dwFlags & WHDR_ENDLOOP){
1300 queue = device->loop_start;
1309 static void WOD_PushData(WINMM_Device *device)
1311 WINMM_CBInfo cb_info;
1313 UINT32 pad, bufsize, avail_frames, queue_frames, written, ofs;
1314 UINT32 queue_bytes, nloops;
1316 WAVEHDR *queue, *first = NULL;
1318 TRACE("(%p)\n", device->handle);
1320 EnterCriticalSection(&device->lock);
1326 device->stopped = TRUE;
1327 device->last_clock_pos = 0;
1328 IAudioClient_Stop(device->client);
1329 IAudioClient_Reset(device->client);
1333 hr = IAudioClient_GetBufferSize(device->client, &bufsize);
1335 ERR("GetBufferSize failed: %08x\n", hr);
1339 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1341 ERR("GetCurrentPadding failed: %08x\n", hr);
1345 first = WOD_MarkDoneHeaders(device);
1347 /* determine which is larger between the available buffer size and
1348 * the amount of data left in the queue */
1349 avail_frames = bufsize - pad;
1351 queue = device->playing;
1352 ofs = device->ofs_bytes;
1355 while(queue && queue_frames < avail_frames){
1356 queue_bytes = WINMM_HeaderLenBytes(device, queue);
1357 queue_frames = (queue_bytes - ofs) / device->bytes_per_frame;
1361 if(queue->dwFlags & WHDR_ENDLOOP && nloops < device->loop_counter){
1362 queue = device->loop_start;
1365 queue = queue->lpNext;
1368 if(avail_frames != 0 && queue_frames == 0){
1369 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1371 ERR("GetBuffer failed: %08x\n", hr);
1375 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames,
1376 AUDCLNT_BUFFERFLAGS_SILENT);
1378 ERR("ReleaseBuffer failed: %08x\n", hr);
1385 if(queue_frames < avail_frames)
1386 avail_frames = queue_frames;
1387 if(avail_frames == 0)
1390 hr = IAudioRenderClient_GetBuffer(device->render, avail_frames, &data);
1392 ERR("GetBuffer failed: %08x\n", hr);
1397 while(device->playing && written < avail_frames){
1398 UINT32 copy_frames, copy_bytes;
1401 queue = device->playing;
1403 if(device->acm_handle){
1404 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)queue->reserved;
1405 queue_bytes = ash->cbDstLengthUsed;
1406 queue_data = ash->pbDst;
1408 queue_bytes = queue->dwBufferLength;
1409 queue_data = (BYTE*)queue->lpData;
1412 queue_frames = (queue_bytes - device->ofs_bytes) /
1413 device->bytes_per_frame;
1415 copy_frames = queue_frames < (avail_frames - written) ?
1416 queue_frames : avail_frames - written;
1417 copy_bytes = copy_frames * device->bytes_per_frame;
1419 memcpy(data, queue_data + device->ofs_bytes, copy_bytes);
1422 written += copy_frames;
1423 device->ofs_bytes += copy_bytes;
1425 if(device->ofs_bytes >= queue_bytes){
1426 device->ofs_bytes = 0;
1428 if(!(queue->dwFlags & (WHDR_BEGINLOOP | WHDR_ENDLOOP)))
1429 device->playing = queue->lpNext;
1431 if(queue->dwFlags & WHDR_BEGINLOOP){
1432 device->loop_start = device->playing;
1433 device->playing = queue->lpNext;
1434 device->loop_counter = queue->dwLoops;
1436 if(queue->dwFlags & WHDR_ENDLOOP){
1437 --device->loop_counter;
1438 if(device->loop_counter)
1439 device->playing = device->loop_start;
1441 device->loop_start = device->playing = queue->lpNext;
1447 hr = IAudioRenderClient_ReleaseBuffer(device->render, avail_frames, 0);
1449 ERR("ReleaseBuffer failed: %08x\n", hr);
1453 device->played_frames += avail_frames;
1456 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
1458 LeaveCriticalSection(&device->lock);
1460 while(first && (first->dwFlags & WHDR_DONE)){
1461 WAVEHDR *next = first->lpNext;
1462 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1467 static void WID_PullACMData(WINMM_Device *device)
1469 UINT32 packet, packet_bytes;
1476 if(device->acm_hdr.cbDstLength == 0){
1477 hr = IAudioClient_GetCurrentPadding(device->client, &packet);
1479 ERR("GetCurrentPadding failed: %08x\n", hr);
1486 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1487 &flags, NULL, NULL);
1489 ERR("GetBuffer failed: %08x\n", hr);
1493 acmStreamSize(device->acm_handle, packet * device->bytes_per_frame,
1494 &packet_bytes, ACM_STREAMSIZEF_SOURCE);
1496 device->acm_offs = 0;
1498 device->acm_hdr.cbStruct = sizeof(device->acm_hdr);
1499 device->acm_hdr.fdwStatus = 0;
1500 device->acm_hdr.dwUser = 0;
1501 device->acm_hdr.pbSrc = data;
1502 device->acm_hdr.cbSrcLength = packet * device->bytes_per_frame;
1503 device->acm_hdr.cbSrcLengthUsed = 0;
1504 device->acm_hdr.dwSrcUser = 0;
1505 device->acm_hdr.pbDst = HeapAlloc(GetProcessHeap(), 0, packet_bytes);
1506 device->acm_hdr.cbDstLength = packet_bytes;
1507 device->acm_hdr.cbDstLengthUsed = 0;
1508 device->acm_hdr.dwDstUser = 0;
1510 mr = acmStreamPrepareHeader(device->acm_handle, &device->acm_hdr, 0);
1511 if(mr != MMSYSERR_NOERROR){
1512 ERR("acmStreamPrepareHeader failed: %d\n", mr);
1516 mr = acmStreamConvert(device->acm_handle, &device->acm_hdr, 0);
1517 if(mr != MMSYSERR_NOERROR){
1518 ERR("acmStreamConvert failed: %d\n", mr);
1522 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet);
1524 ERR("ReleaseBuffer failed: %08x\n", hr);
1527 queue = device->first;
1529 UINT32 to_copy_bytes;
1531 to_copy_bytes = min(queue->dwBufferLength - queue->dwBytesRecorded,
1532 device->acm_hdr.cbDstLengthUsed - device->acm_offs);
1534 memcpy(queue->lpData + queue->dwBytesRecorded,
1535 device->acm_hdr.pbDst + device->acm_offs, to_copy_bytes);
1537 queue->dwBytesRecorded += to_copy_bytes;
1538 device->acm_offs += to_copy_bytes;
1540 if(queue->dwBufferLength - queue->dwBytesRecorded <
1541 device->bytes_per_frame){
1542 queue->dwFlags &= ~WHDR_INQUEUE;
1543 queue->dwFlags |= WHDR_DONE;
1544 device->first = queue = queue->lpNext;
1547 if(device->acm_offs >= device->acm_hdr.cbDstLengthUsed){
1548 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1549 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1550 device->acm_hdr.cbDstLength = 0;
1551 device->acm_hdr.cbDstLengthUsed = 0;
1553 /* done with this ACM Header, so try to pull more data */
1554 WID_PullACMData(device);
1559 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1560 acmStreamUnprepareHeader(device->acm_handle, &device->acm_hdr, 0);
1561 HeapFree(GetProcessHeap(), 0, device->acm_hdr.pbDst);
1562 device->acm_hdr.cbDstLength = 0;
1563 device->acm_hdr.cbDstLengthUsed = 0;
1566 static void WID_PullData(WINMM_Device *device)
1568 WINMM_CBInfo cb_info;
1569 WAVEHDR *queue, *first = NULL;
1572 TRACE("(%p)\n", device->handle);
1574 EnterCriticalSection(&device->lock);
1576 if(!device->device || !device->first)
1579 first = device->first;
1581 if(device->acm_handle){
1582 WID_PullACMData(device);
1586 while(device->first){
1588 UINT32 pad, packet_len, packet;
1591 hr = IAudioClient_GetCurrentPadding(device->client, &pad);
1593 ERR("GetCurrentPadding failed: %08x\n", hr);
1600 hr = IAudioCaptureClient_GetBuffer(device->capture, &data, &packet,
1601 &flags, NULL, NULL);
1603 ERR("GetBuffer failed: %08x\n", hr);
1607 packet_len = packet;
1608 queue = device->first;
1609 while(queue && packet > 0){
1610 UINT32 to_copy_bytes;
1612 to_copy_bytes = min(packet * device->bytes_per_frame,
1613 queue->dwBufferLength - queue->dwBytesRecorded);
1615 memcpy(queue->lpData + queue->dwBytesRecorded, data,
1618 queue->dwBytesRecorded += to_copy_bytes;
1620 if(queue->dwBufferLength - queue->dwBytesRecorded <
1621 device->bytes_per_frame){
1622 queue->dwFlags &= ~WHDR_INQUEUE;
1623 queue->dwFlags |= WHDR_DONE;
1624 device->first = queue = queue->lpNext;
1627 packet -= to_copy_bytes / device->bytes_per_frame;
1630 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_len);
1632 ERR("ReleaseBuffer failed: %08x\n", hr);
1636 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
1638 LeaveCriticalSection(&device->lock);
1640 while(first && (first->dwFlags & WHDR_DONE)){
1641 WAVEHDR *next = first->lpNext;
1642 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1647 static HRESULT WINMM_BeginPlaying(WINMM_Device *device)
1651 TRACE("(%p)\n", device->handle);
1653 EnterCriticalSection(&device->lock);
1656 /* prebuffer data before starting */
1657 WOD_PushData(device);
1659 if(device->stopped){
1660 device->stopped = FALSE;
1662 hr = IAudioClient_Start(device->client);
1663 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
1664 device->stopped = TRUE;
1665 LeaveCriticalSection(&device->lock);
1666 ERR("Start failed: %08x\n", hr);
1671 LeaveCriticalSection(&device->lock);
1676 static LRESULT WINMM_Pause(HWAVE hwave)
1678 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1681 TRACE("(%p)\n", hwave);
1683 if(!WINMM_ValidateAndLock(device))
1684 return MMSYSERR_INVALHANDLE;
1686 hr = IAudioClient_Stop(device->client);
1688 LeaveCriticalSection(&device->lock);
1689 ERR("Stop failed: %08x\n", hr);
1690 return MMSYSERR_ERROR;
1693 device->stopped = FALSE;
1695 LeaveCriticalSection(&device->lock);
1697 return MMSYSERR_NOERROR;
1700 static LRESULT WINMM_Reset(HWAVE hwave)
1702 WINMM_CBInfo cb_info;
1703 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1707 TRACE("(%p)\n", hwave);
1709 if(!WINMM_ValidateAndLock(device))
1710 return MMSYSERR_INVALHANDLE;
1712 mr = WINMM_Pause(hwave);
1713 if(mr != MMSYSERR_NOERROR){
1714 LeaveCriticalSection(&device->lock);
1717 device->stopped = TRUE;
1720 first = WOD_MarkDoneHeaders(device);
1722 first = device->first;
1723 device->first = device->last = device->playing = NULL;
1724 device->ofs_bytes = 0;
1725 device->played_frames = 0;
1726 device->loop_counter = 0;
1727 device->last_clock_pos = 0;
1729 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
1731 LeaveCriticalSection(&device->lock);
1734 WAVEHDR *next = first->lpNext;
1735 first->dwFlags &= ~WHDR_INQUEUE;
1736 first->dwFlags |= WHDR_DONE;
1738 WINMM_NotifyClient(&cb_info, WOM_DONE, (DWORD_PTR)first, 0);
1740 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)first, 0);
1744 return MMSYSERR_NOERROR;
1747 static MMRESULT WINMM_FramesToMMTime(MMTIME *time, UINT32 played_frames,
1748 UINT32 sample_rate, UINT32 bytes_per_frame)
1750 switch(time->wType){
1752 time->u.sample = played_frames;
1753 return MMSYSERR_NOERROR;
1755 time->u.ms = (DWORD)((played_frames / (double)sample_rate) * 1000);
1756 return MMSYSERR_NOERROR;
1758 time->u.smpte.fps = 30;
1759 if(played_frames >= sample_rate){
1760 time->u.smpte.sec = played_frames / (double)sample_rate;
1761 time->u.smpte.min = time->u.smpte.sec / 60;
1762 time->u.smpte.hour = time->u.smpte.min / 60;
1763 time->u.smpte.sec %= 60;
1764 time->u.smpte.min %= 60;
1765 played_frames %= sample_rate;
1767 time->u.smpte.sec = 0;
1768 time->u.smpte.min = 0;
1769 time->u.smpte.hour = 0;
1771 time->u.smpte.frame = (played_frames / (double)sample_rate) * 30;
1772 return MMSYSERR_NOERROR;
1775 time->wType = TIME_BYTES;
1776 time->u.cb = played_frames * bytes_per_frame;
1777 return MMSYSERR_NOERROR;
1780 return MMSYSERR_ERROR;
1783 static LRESULT WINMM_GetPosition(HWAVE hwave, MMTIME *time)
1785 WINMM_Device *device = WINMM_GetDeviceFromHWAVE((HWAVE)hwave);
1786 UINT32 played_frames, sample_rate, bytes_per_frame;
1788 TRACE("(%p, %p)\n", hwave, time);
1790 if(!WINMM_ValidateAndLock(device))
1791 return MMSYSERR_INVALHANDLE;
1793 played_frames = device->played_frames;
1794 sample_rate = device->samples_per_sec;
1795 bytes_per_frame = device->bytes_per_frame;
1797 LeaveCriticalSection(&device->lock);
1799 return WINMM_FramesToMMTime(time, played_frames, sample_rate,
1803 static WINMM_MMDevice *WINMM_GetMixerMMDevice(HMIXEROBJ hmix, DWORD flags,
1806 UINT mmdev, dev, junk, *out;
1814 switch(flags & 0xF0000000){
1815 case MIXER_OBJECTF_MIXER: /* == 0 */
1816 *out = HandleToULong(hmix);
1817 if(*out < g_outmmdevices_count)
1818 return &g_out_mmdevices[*out];
1819 if(*out - g_outmmdevices_count < g_inmmdevices_count){
1820 *out -= g_outmmdevices_count;
1821 return &g_in_mmdevices[*out];
1823 /* fall through -- if it's not a valid mixer device, then
1824 * it could be a valid mixer handle. windows seems to do
1826 case MIXER_OBJECTF_HMIXER:
1827 case MIXER_OBJECTF_HWAVEOUT:
1828 case MIXER_OBJECTF_HWAVEIN:
1829 WINMM_DecomposeHWAVE((HWAVE)hmix, out, &is_out, &dev, &junk);
1830 if(junk != 0x1 || (is_out && *out >= g_outmmdevices_count) ||
1831 (!is_out && *out >= g_inmmdevices_count))
1834 return &g_out_mmdevices[*out];
1835 return &g_in_mmdevices[*out];
1836 case MIXER_OBJECTF_WAVEOUT:
1837 *out = HandleToULong(hmix);
1838 if(*out < g_outmmdevices_count)
1839 return &g_out_mmdevices[*out];
1841 case MIXER_OBJECTF_WAVEIN:
1842 *out = HandleToULong(hmix);
1843 if(*out < g_inmmdevices_count)
1844 return &g_in_mmdevices[*out];
1851 static MMRESULT WINMM_SetupMMDeviceVolume(WINMM_MMDevice *mmdevice)
1853 IAudioSessionManager *sesman;
1857 hr = IMMDeviceEnumerator_GetDevice(g_devenum, mmdevice->dev_id, &device);
1859 ERR("Device %s (%s) unavailable: %08x\n",
1860 wine_dbgstr_w(mmdevice->dev_id),
1861 wine_dbgstr_w(mmdevice->out_caps.szPname), hr);
1862 return MMSYSERR_ERROR;
1865 hr = IMMDevice_Activate(device, &IID_IAudioSessionManager,
1866 CLSCTX_INPROC_SERVER, NULL, (void**)&sesman);
1868 ERR("Activate failed: %08x\n", hr);
1869 IMMDevice_Release(device);
1870 return MMSYSERR_ERROR;
1873 IMMDevice_Release(device);
1875 hr = IAudioSessionManager_GetSimpleAudioVolume(sesman, &mmdevice->session,
1876 FALSE, &mmdevice->volume);
1877 IAudioSessionManager_Release(sesman);
1879 ERR("GetSimpleAudioVolume failed: %08x\n", hr);
1880 return MMSYSERR_ERROR;
1883 return MMSYSERR_NOERROR;
1886 static LRESULT MXD_GetControlDetails(WINMM_ControlDetails *details)
1888 WINMM_MMDevice *mmdevice;
1889 MIXERCONTROLDETAILS *control = details->details;
1892 TRACE("(%p)\n", details->hmix);
1894 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
1896 return MMSYSERR_INVALHANDLE;
1898 EnterCriticalSection(&mmdevice->lock);
1900 if(!mmdevice->volume){
1903 mr = WINMM_SetupMMDeviceVolume(mmdevice);
1904 if(mr != MMSYSERR_NOERROR){
1905 LeaveCriticalSection(&mmdevice->lock);
1910 if(control->dwControlID == 0){
1912 MIXERCONTROLDETAILS_UNSIGNED *udet;
1914 if(!control->paDetails ||
1915 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
1916 LeaveCriticalSection(&mmdevice->lock);
1917 return MMSYSERR_INVALPARAM;
1920 hr = ISimpleAudioVolume_GetMasterVolume(mmdevice->volume, &vol);
1922 ERR("GetMasterVolume failed: %08x\n", hr);
1923 LeaveCriticalSection(&mmdevice->lock);
1924 return MMSYSERR_ERROR;
1927 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
1928 udet->dwValue = vol * ((unsigned int)0xFFFF);
1929 }else if(control->dwControlID == 1){
1931 MIXERCONTROLDETAILS_BOOLEAN *bdet;
1933 if(!control->paDetails ||
1934 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
1935 LeaveCriticalSection(&mmdevice->lock);
1936 return MMSYSERR_INVALPARAM;
1939 hr = ISimpleAudioVolume_GetMute(mmdevice->volume, &mute);
1941 ERR("GetMute failed: %08x\n", hr);
1942 LeaveCriticalSection(&mmdevice->lock);
1943 return MMSYSERR_ERROR;
1946 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
1947 bdet->fValue = mute;
1948 }else if(control->dwControlID == 2 || control->dwControlID == 3){
1949 FIXME("What should the sw-side mixer controls map to?\n");
1951 LeaveCriticalSection(&mmdevice->lock);
1952 return MIXERR_INVALCONTROL;
1955 LeaveCriticalSection(&mmdevice->lock);
1957 return MMSYSERR_NOERROR;
1960 static LRESULT MXD_SetControlDetails(WINMM_ControlDetails *details)
1962 WINMM_MMDevice *mmdevice;
1963 MIXERCONTROLDETAILS *control = details->details;
1966 TRACE("(%p)\n", details->hmix);
1968 mmdevice = WINMM_GetMixerMMDevice(details->hmix, details->flags, NULL);
1970 return MMSYSERR_INVALHANDLE;
1972 EnterCriticalSection(&mmdevice->lock);
1974 if(!mmdevice->volume){
1977 mr = WINMM_SetupMMDeviceVolume(mmdevice);
1978 if(mr != MMSYSERR_NOERROR){
1979 LeaveCriticalSection(&mmdevice->lock);
1984 if(control->dwControlID == 0){
1986 MIXERCONTROLDETAILS_UNSIGNED *udet;
1988 if(!control->paDetails ||
1989 control->cbDetails < sizeof(MIXERCONTROLDETAILS_UNSIGNED)){
1990 LeaveCriticalSection(&mmdevice->lock);
1991 return MMSYSERR_INVALPARAM;
1994 udet = (MIXERCONTROLDETAILS_UNSIGNED*)control->paDetails;
1996 if(udet->dwValue > 65535){
1997 LeaveCriticalSection(&mmdevice->lock);
1998 return MMSYSERR_INVALPARAM;
2001 vol = udet->dwValue / 65535.f;
2003 hr = ISimpleAudioVolume_SetMasterVolume(mmdevice->volume, vol, NULL);
2005 ERR("SetMasterVolume failed: %08x\n", hr);
2006 LeaveCriticalSection(&mmdevice->lock);
2007 return MMSYSERR_ERROR;
2009 }else if(control->dwControlID == 1){
2011 MIXERCONTROLDETAILS_BOOLEAN *bdet;
2013 if(!control->paDetails ||
2014 control->cbDetails < sizeof(MIXERCONTROLDETAILS_BOOLEAN)){
2015 LeaveCriticalSection(&mmdevice->lock);
2016 return MMSYSERR_INVALPARAM;
2019 bdet = (MIXERCONTROLDETAILS_BOOLEAN*)control->paDetails;
2020 mute = bdet->fValue;
2022 hr = ISimpleAudioVolume_SetMute(mmdevice->volume, mute, NULL);
2024 ERR("SetMute failed: %08x\n", hr);
2025 LeaveCriticalSection(&mmdevice->lock);
2026 return MMSYSERR_ERROR;
2028 }else if(control->dwControlID == 2 || control->dwControlID == 3){
2029 FIXME("What should the sw-side mixer controls map to?\n");
2031 LeaveCriticalSection(&mmdevice->lock);
2032 return MIXERR_INVALCONTROL;
2035 LeaveCriticalSection(&mmdevice->lock);
2037 return MMSYSERR_NOERROR;
2040 static LRESULT CALLBACK WINMM_DevicesMsgProc(HWND hwnd, UINT msg, WPARAM wparam,
2045 return WOD_Open((WINMM_OpenInfo*)wparam);
2047 return WOD_Close((HWAVEOUT)wparam);
2049 return WID_Open((WINMM_OpenInfo*)wparam);
2051 return WID_Close((HWAVEIN)wparam);
2052 case MXDM_GETCONTROLDETAILS:
2053 return MXD_GetControlDetails((WINMM_ControlDetails*)wparam);
2054 case MXDM_SETCONTROLDETAILS:
2055 return MXD_SetControlDetails((WINMM_ControlDetails*)wparam);
2057 return DefWindowProcW(hwnd, msg, wparam, lparam);
2060 static DWORD WINAPI WINMM_DevicesThreadProc(void *arg)
2064 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
2066 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2068 ERR("CoInitializeEx failed: %08x\n", hr);
2072 hr = WINMM_InitMMDevices();
2078 g_devices_hwnd = CreateWindowW(messageW, NULL, 0, 0, 0, 0, 0,
2079 HWND_MESSAGE, NULL, NULL, NULL);
2080 if(!g_devices_hwnd){
2081 ERR("CreateWindow failed: %d\n", GetLastError());
2086 SetWindowLongPtrW(g_devices_hwnd, GWLP_WNDPROC,
2087 (LONG_PTR)WINMM_DevicesMsgProc);
2089 /* inform caller that the thread is ready to process messages */
2091 evt = NULL; /* do not use after this point */
2095 wait = MsgWaitForMultipleObjects(g_devhandle_count, g_device_handles,
2096 FALSE, INFINITE, QS_ALLINPUT);
2097 if(wait == g_devhandle_count - WAIT_OBJECT_0){
2099 if(PeekMessageW(&msg, g_devices_hwnd, 0, 0, PM_REMOVE))
2100 ERR("Unexpected message: 0x%x\n", msg.message);
2101 }else if(wait < g_devhandle_count){
2102 WINMM_Device *device = g_handle_devices[wait - WAIT_OBJECT_0];
2104 WOD_PushData(device);
2106 WID_PullData(device);
2108 ERR("Unexpected MsgWait result 0x%x, GLE: %d\n", wait,
2112 DestroyWindow(g_devices_hwnd);
2119 static BOOL WINMM_StartDevicesThread(void)
2124 EnterCriticalSection(&g_devthread_lock);
2126 if(g_devices_thread){
2129 wait = WaitForSingleObject(g_devices_thread, 0);
2130 if(wait == WAIT_TIMEOUT){
2131 LeaveCriticalSection(&g_devthread_lock);
2134 if(wait != WAIT_OBJECT_0){
2135 LeaveCriticalSection(&g_devthread_lock);
2139 g_devices_thread = NULL;
2140 g_devices_hwnd = NULL;
2143 TRACE("Starting up devices thread\n");
2145 events[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
2147 g_devices_thread = CreateThread(NULL, 0, WINMM_DevicesThreadProc,
2148 events[0], 0, NULL);
2149 if(!g_devices_thread){
2150 LeaveCriticalSection(&g_devthread_lock);
2151 CloseHandle(events[0]);
2155 events[1] = g_devices_thread;
2156 wait = WaitForMultipleObjects(2, events, FALSE, INFINITE);
2157 CloseHandle(events[0]);
2158 if(wait != WAIT_OBJECT_0){
2159 if(wait == 1 - WAIT_OBJECT_0){
2160 CloseHandle(g_devices_thread);
2161 g_devices_thread = NULL;
2162 g_devices_hwnd = NULL;
2164 LeaveCriticalSection(&g_devthread_lock);
2168 LeaveCriticalSection(&g_devthread_lock);
2173 /**************************************************************************
2174 * waveOutGetNumDevs [WINMM.@]
2176 UINT WINAPI waveOutGetNumDevs(void)
2178 if(!WINMM_StartDevicesThread())
2181 TRACE("count: %u\n", g_outmmdevices_count);
2183 return g_outmmdevices_count;
2186 /**************************************************************************
2187 * waveOutGetDevCapsA [WINMM.@]
2189 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2195 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2197 if(!WINMM_StartDevicesThread())
2198 return MMSYSERR_ERROR;
2201 return MMSYSERR_INVALPARAM;
2203 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2205 if (ret == MMSYSERR_NOERROR) {
2207 wocA.wMid = wocW.wMid;
2208 wocA.wPid = wocW.wPid;
2209 wocA.vDriverVersion = wocW.vDriverVersion;
2210 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2211 sizeof(wocA.szPname), NULL, NULL );
2212 wocA.dwFormats = wocW.dwFormats;
2213 wocA.wChannels = wocW.wChannels;
2214 wocA.dwSupport = wocW.dwSupport;
2215 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2220 /**************************************************************************
2221 * waveOutGetDevCapsW [WINMM.@]
2223 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2226 WAVEOUTCAPSW mapper_caps, *caps;
2228 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2230 if(!WINMM_StartDevicesThread())
2231 return MMSYSERR_ERROR;
2233 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2235 if(WINMM_IsMapper(uDeviceID)){
2236 /* FIXME: Should be localized */
2237 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2238 'n','d',' ','M','a','p','p','e','r',0};
2240 mapper_caps.wMid = 0xFF;
2241 mapper_caps.wPid = 0xFF;
2242 mapper_caps.vDriverVersion = 0x00010001;
2243 mapper_caps.dwFormats = 0xFFFFFFFF;
2244 mapper_caps.wReserved1 = 0;
2245 mapper_caps.dwSupport = WAVECAPS_LRVOLUME | WAVECAPS_VOLUME |
2246 WAVECAPS_SAMPLEACCURATE;
2247 mapper_caps.wChannels = 2;
2248 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2250 caps = &mapper_caps;
2252 if(uDeviceID >= g_outmmdevices_count)
2253 return MMSYSERR_BADDEVICEID;
2255 caps = &g_out_mmdevices[uDeviceID].out_caps;
2258 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2260 return MMSYSERR_NOERROR;
2263 /**************************************************************************
2264 * waveOutGetErrorTextA [WINMM.@]
2265 * waveInGetErrorTextA [WINMM.@]
2267 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2271 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2272 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2275 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2276 if (!xstr) ret = MMSYSERR_NOMEM;
2279 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2280 if (ret == MMSYSERR_NOERROR)
2281 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2282 HeapFree(GetProcessHeap(), 0, xstr);
2288 /**************************************************************************
2289 * waveOutGetErrorTextW [WINMM.@]
2290 * waveInGetErrorTextW [WINMM.@]
2292 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2294 UINT ret = MMSYSERR_BADERRNUM;
2296 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2297 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2299 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2300 * a warning for the test was always true */
2301 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2302 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2303 if (LoadStringW(hWinMM32Instance,
2304 uError, lpText, uSize) > 0) {
2305 ret = MMSYSERR_NOERROR;
2311 /**************************************************************************
2312 * waveOutOpen [WINMM.@]
2313 * All the args/structs have the same layout as the win16 equivalents
2315 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2316 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2317 DWORD_PTR dwInstance, DWORD dwFlags)
2321 WINMM_OpenInfo info;
2322 WINMM_CBInfo cb_info;
2324 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut, uDeviceID, lpFormat,
2325 dwCallback, dwInstance, dwFlags);
2327 if(!WINMM_StartDevicesThread())
2328 return MMSYSERR_ERROR;
2330 if(!lphWaveOut && !(dwFlags & WAVE_FORMAT_QUERY))
2331 return MMSYSERR_INVALPARAM;
2333 hr = WINMM_StartDevicesThread();
2335 ERR("Couldn't start the device thread: %08x\n", hr);
2336 return MMSYSERR_ERROR;
2339 info.format = (WAVEFORMATEX*)lpFormat;
2340 info.callback = dwCallback;
2341 info.cb_user = dwInstance;
2342 info.req_device = uDeviceID;
2343 info.flags = dwFlags;
2345 res = SendMessageW(g_devices_hwnd, WODM_OPEN, (DWORD_PTR)&info, 0);
2346 if(res != MMSYSERR_NOERROR)
2350 *lphWaveOut = (HWAVEOUT)info.handle;
2352 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2353 cb_info.callback = dwCallback;
2354 cb_info.user = dwInstance;
2355 cb_info.hwave = info.handle;
2357 WINMM_NotifyClient(&cb_info, WOM_OPEN, 0, 0);
2362 /**************************************************************************
2363 * waveOutClose [WINMM.@]
2365 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2368 WINMM_Device *device;
2369 WINMM_CBInfo cb_info;
2371 TRACE("(%p)\n", hWaveOut);
2373 if(!WINMM_StartDevicesThread())
2374 return MMSYSERR_ERROR;
2376 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2378 if(!WINMM_ValidateAndLock(device))
2379 return MMSYSERR_INVALHANDLE;
2381 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
2383 LeaveCriticalSection(&device->lock);
2385 res = SendMessageW(g_devices_hwnd, WODM_CLOSE, (WPARAM)hWaveOut, 0);
2387 if(res == MMSYSERR_NOERROR)
2388 WINMM_NotifyClient(&cb_info, WOM_CLOSE, 0, 0);
2393 /**************************************************************************
2394 * waveOutPrepareHeader [WINMM.@]
2396 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2397 WAVEHDR* lpWaveOutHdr, UINT uSize)
2399 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2401 if(!WINMM_StartDevicesThread())
2402 return MMSYSERR_ERROR;
2404 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2405 return MMSYSERR_INVALPARAM;
2407 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2408 return WAVERR_STILLPLAYING;
2410 return WINMM_PrepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2413 /**************************************************************************
2414 * waveOutUnprepareHeader [WINMM.@]
2416 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2417 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2419 TRACE("(%p, %p, %u)\n", hWaveOut, lpWaveOutHdr, uSize);
2421 if(!WINMM_StartDevicesThread())
2422 return MMSYSERR_ERROR;
2424 if(!lpWaveOutHdr || uSize < sizeof(WAVEHDR))
2425 return MMSYSERR_INVALPARAM;
2427 if(!(lpWaveOutHdr->dwFlags & WHDR_PREPARED))
2428 return MMSYSERR_NOERROR;
2430 if(lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2431 return WAVERR_STILLPLAYING;
2433 return WINMM_UnprepareHeader((HWAVE)hWaveOut, lpWaveOutHdr);
2436 /**************************************************************************
2437 * waveOutWrite [WINMM.@]
2439 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, WAVEHDR *header, UINT uSize)
2441 WINMM_Device *device;
2444 TRACE("(%p, %p, %u)\n", hWaveOut, header, uSize);
2446 if(!WINMM_StartDevicesThread())
2447 return MMSYSERR_ERROR;
2449 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2451 if(!WINMM_ValidateAndLock(device))
2452 return MMSYSERR_INVALHANDLE;
2454 if(!header->lpData || !(header->dwFlags & WHDR_PREPARED)){
2455 LeaveCriticalSection(&device->lock);
2456 return WAVERR_UNPREPARED;
2459 if(header->dwFlags & WHDR_INQUEUE){
2460 LeaveCriticalSection(&device->lock);
2461 return WAVERR_STILLPLAYING;
2464 if(device->acm_handle){
2465 ACMSTREAMHEADER *ash = (ACMSTREAMHEADER*)header->reserved;
2468 ash->cbSrcLength = header->dwBufferLength;
2469 mr = acmStreamConvert(device->acm_handle, ash, 0);
2470 if(mr != MMSYSERR_NOERROR){
2471 LeaveCriticalSection(&device->lock);
2477 device->last->lpNext = header;
2478 device->last = header;
2479 if(!device->playing)
2480 device->playing = header;
2482 device->playing = device->first = device->last = header;
2483 if(header->dwFlags & WHDR_BEGINLOOP){
2484 device->loop_counter = header->dwLoops;
2485 device->loop_start = header;
2489 header->lpNext = NULL;
2490 header->dwFlags &= ~WHDR_DONE;
2491 header->dwFlags |= WHDR_INQUEUE;
2493 hr = WINMM_BeginPlaying(device);
2495 LeaveCriticalSection(&device->lock);
2496 return MMSYSERR_ERROR;
2499 LeaveCriticalSection(&device->lock);
2501 return MMSYSERR_NOERROR;
2504 /**************************************************************************
2505 * waveOutBreakLoop [WINMM.@]
2507 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2509 WINMM_Device *device;
2511 TRACE("(%p)\n", hWaveOut);
2513 if(!WINMM_StartDevicesThread())
2514 return MMSYSERR_ERROR;
2516 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2518 if(!WINMM_ValidateAndLock(device))
2519 return MMSYSERR_INVALHANDLE;
2521 device->loop_counter = 0;
2523 LeaveCriticalSection(&device->lock);
2525 return MMSYSERR_NOERROR;
2528 /**************************************************************************
2529 * waveOutPause [WINMM.@]
2531 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2533 TRACE("(%p)\n", hWaveOut);
2535 if(!WINMM_StartDevicesThread())
2536 return MMSYSERR_ERROR;
2538 return WINMM_Pause((HWAVE)hWaveOut);
2541 /**************************************************************************
2542 * waveOutReset [WINMM.@]
2544 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2546 TRACE("(%p)\n", hWaveOut);
2548 if(!WINMM_StartDevicesThread())
2549 return MMSYSERR_ERROR;
2551 return WINMM_Reset((HWAVE)hWaveOut);
2554 /**************************************************************************
2555 * waveOutRestart [WINMM.@]
2557 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2559 WINMM_Device *device;
2562 TRACE("(%p)\n", hWaveOut);
2564 if(!WINMM_StartDevicesThread())
2565 return MMSYSERR_ERROR;
2567 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2569 if(!WINMM_ValidateAndLock(device))
2570 return MMSYSERR_INVALHANDLE;
2572 device->stopped = TRUE;
2574 hr = WINMM_BeginPlaying(device);
2576 LeaveCriticalSection(&device->lock);
2577 return MMSYSERR_ERROR;
2580 LeaveCriticalSection(&device->lock);
2582 return MMSYSERR_NOERROR;
2585 /**************************************************************************
2586 * waveOutGetPosition [WINMM.@]
2588 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2591 TRACE("(%p, %p, %u)\n", hWaveOut, lpTime, uSize);
2593 if(!WINMM_StartDevicesThread())
2594 return MMSYSERR_ERROR;
2596 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
2597 return MMSYSERR_INVALPARAM;
2599 return WINMM_GetPosition((HWAVE)hWaveOut, lpTime);
2602 /**************************************************************************
2603 * waveOutGetPitch [WINMM.@]
2605 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2607 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2608 return MMSYSERR_NOTSUPPORTED;
2611 /**************************************************************************
2612 * waveOutSetPitch [WINMM.@]
2614 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
2616 TRACE("(%p, %08x)\n", hWaveOut, dw);
2618 return MMSYSERR_NOTSUPPORTED;
2621 /**************************************************************************
2622 * waveOutGetPlaybackRate [WINMM.@]
2624 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
2626 TRACE("(%p, %p)\n", hWaveOut, lpdw);
2628 return MMSYSERR_NOTSUPPORTED;
2631 /**************************************************************************
2632 * waveOutSetPlaybackRate [WINMM.@]
2634 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
2636 TRACE("(%p, %08x)\n", hWaveOut, dw);
2638 return MMSYSERR_NOTSUPPORTED;
2641 /**************************************************************************
2642 * waveOutGetVolume [WINMM.@]
2644 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, DWORD *out)
2646 WINMM_Device *device;
2651 TRACE("(%p, %p)\n", hWaveOut, out);
2653 if(!WINMM_StartDevicesThread())
2654 return MMSYSERR_ERROR;
2657 return MMSYSERR_INVALPARAM;
2659 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2661 if(!WINMM_ValidateAndLock(device))
2662 return MMSYSERR_INVALHANDLE;
2664 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
2666 LeaveCriticalSection(&device->lock);
2667 ERR("GetChannelCount failed: %08x\n", hr);
2668 return MMSYSERR_ERROR;
2671 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
2673 LeaveCriticalSection(&device->lock);
2674 return MMSYSERR_NOMEM;
2677 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
2679 LeaveCriticalSection(&device->lock);
2680 HeapFree(GetProcessHeap(), 0, vols);
2681 ERR("GetAllVolumes failed: %08x\n", hr);
2682 return MMSYSERR_ERROR;
2685 LeaveCriticalSection(&device->lock);
2687 *out = ((UINT16)(vols[0] * (DWORD)0xFFFF));
2689 *out |= ((UINT16)(vols[1] * (DWORD)0xFFFF)) << 16;
2691 HeapFree(GetProcessHeap(), 0, vols);
2693 return MMSYSERR_NOERROR;
2696 /**************************************************************************
2697 * waveOutSetVolume [WINMM.@]
2699 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD in)
2701 WINMM_Device *device;
2706 TRACE("(%p, %08x)\n", hWaveOut, in);
2708 if(!WINMM_StartDevicesThread())
2709 return MMSYSERR_ERROR;
2711 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2713 if(!WINMM_ValidateAndLock(device))
2714 return MMSYSERR_INVALHANDLE;
2716 hr = IAudioStreamVolume_GetChannelCount(device->volume, &channels);
2718 LeaveCriticalSection(&device->lock);
2719 ERR("GetChannelCount failed: %08x\n", hr);
2720 return MMSYSERR_ERROR;
2723 vols = HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels);
2725 LeaveCriticalSection(&device->lock);
2726 return MMSYSERR_NOMEM;
2729 hr = IAudioStreamVolume_GetAllVolumes(device->volume, channels, vols);
2731 LeaveCriticalSection(&device->lock);
2732 HeapFree(GetProcessHeap(), 0, vols);
2733 ERR("GetAllVolumes failed: %08x\n", hr);
2734 return MMSYSERR_ERROR;
2737 vols[0] = (float)((DWORD)(in & 0xFFFF) / (float)0xFFFF);
2739 vols[1] = (float)((DWORD)(in >> 16) / (float)0xFFFF);
2741 hr = IAudioStreamVolume_SetAllVolumes(device->volume, channels, vols);
2743 LeaveCriticalSection(&device->lock);
2744 HeapFree(GetProcessHeap(), 0, vols);
2745 ERR("SetAllVolumes failed: %08x\n", hr);
2746 return MMSYSERR_ERROR;
2749 LeaveCriticalSection(&device->lock);
2751 HeapFree(GetProcessHeap(), 0, vols);
2753 return MMSYSERR_NOERROR;
2756 /**************************************************************************
2757 * waveOutGetID [WINMM.@]
2759 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
2761 WINMM_Device *device;
2765 TRACE("(%p, %p)\n", hWaveOut, lpuDeviceID);
2767 if(!WINMM_StartDevicesThread())
2768 return MMSYSERR_ERROR;
2771 return MMSYSERR_INVALPARAM;
2773 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveOut);
2774 if(!WINMM_ValidateAndLock(device))
2775 return MMSYSERR_INVALHANDLE;
2777 LeaveCriticalSection(&device->lock);
2779 WINMM_DecomposeHWAVE((HWAVE)hWaveOut, lpuDeviceID, &is_out, &dev, &junk);
2781 return MMSYSERR_NOERROR;
2784 static UINT WINMM_QueryInstanceIDSize(UINT device, DWORD_PTR *len, BOOL is_out)
2787 WINMM_MMDevice *devices;
2789 TRACE("(%u, %p, %d)\n", device, len, is_out);
2792 count = g_outmmdevices_count;
2793 devices = g_out_mmdevices;
2795 count = g_inmmdevices_count;
2796 devices = g_in_mmdevices;
2800 return MMSYSERR_INVALHANDLE;
2802 *len = (lstrlenW(devices[device].dev_id) + 1) * sizeof(WCHAR);
2804 return MMSYSERR_NOERROR;
2807 static UINT WINMM_QueryInstanceID(UINT device, WCHAR *str, DWORD_PTR len,
2811 WINMM_MMDevice *devices;
2813 TRACE("(%u, %p, %d)\n", device, str, is_out);
2816 count = g_outmmdevices_count;
2817 devices = g_out_mmdevices;
2819 count = g_inmmdevices_count;
2820 devices = g_in_mmdevices;
2824 return MMSYSERR_INVALHANDLE;
2826 id_len = (lstrlenW(devices[device].dev_id) + 1) * sizeof(WCHAR);
2828 return MMSYSERR_ERROR;
2830 memcpy(str, devices[device].dev_id, id_len);
2832 return MMSYSERR_NOERROR;
2835 static UINT WINMM_DRVMessage(UINT dev, UINT message, DWORD_PTR param1,
2836 DWORD_PTR param2, BOOL is_out)
2839 UINT type = is_out ? MMDRV_WAVEOUT : MMDRV_WAVEIN;
2841 TRACE("(%u, %u, %ld, %ld, %d)\n", dev, message, param1, param2, is_out);
2843 if((wmld = MMDRV_Get(ULongToHandle(dev), type, FALSE)) == NULL){
2844 if((wmld = MMDRV_Get(ULongToHandle(dev), type, TRUE)) != NULL)
2845 return MMDRV_PhysicalFeatures(wmld, message, param1, param2);
2846 return MMSYSERR_INVALHANDLE;
2849 if(message < DRVM_IOCTL ||
2850 (message >= DRVM_IOCTL_LAST && message < DRVM_MAPPER))
2851 return MMSYSERR_INVALPARAM;
2853 return MMDRV_Message(wmld, message, param1, param2);
2856 static UINT WINMM_FillDSDriverDesc(UINT dev, DSDRIVERDESC *desc, BOOL is_out)
2861 if(dev >= g_outmmdevices_count)
2862 return MMSYSERR_INVALHANDLE;
2863 name = g_out_mmdevices[dev].out_caps.szPname;
2865 if(dev >= g_inmmdevices_count)
2866 return MMSYSERR_INVALHANDLE;
2867 name = g_in_mmdevices[dev].in_caps.szPname;
2870 memset(desc, 0, sizeof(*desc));
2872 strcpy(desc->szDesc, "WinMM: ");
2873 WideCharToMultiByte(CP_ACP, 0, name, -1,
2874 desc->szDesc + strlen(desc->szDesc),
2875 sizeof(desc->szDesc) - strlen(desc->szDesc), NULL, NULL);
2877 strcpy(desc->szDrvname, "winmm.dll");
2879 return MMSYSERR_NOERROR;
2882 /**************************************************************************
2883 * waveOutMessage [WINMM.@]
2885 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
2886 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2888 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut, uMessage, dwParam1, dwParam2);
2891 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
2892 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut),
2893 (DWORD_PTR*)dwParam1, TRUE);
2894 case DRV_QUERYFUNCTIONINSTANCEID:
2895 return WINMM_QueryInstanceID(HandleToULong(hWaveOut),
2896 (WCHAR*)dwParam1, (DWORD_PTR)dwParam2, TRUE);
2897 /* TODO: Remove after dsound has been rewritten for mmdevapi */
2898 case DRV_QUERYDSOUNDDESC:
2899 case DRV_QUERYDSOUNDIFACE:
2900 if(dwParam2 == DS_HW_ACCEL_FULL)
2901 return WINMM_DRVMessage(HandleToULong(hWaveOut), uMessage,
2903 return WINMM_FillDSDriverDesc(HandleToULong(hWaveOut),
2904 (DSDRIVERDESC*)dwParam1, TRUE);
2907 return MMSYSERR_NOTSUPPORTED;
2910 /**************************************************************************
2911 * waveInGetNumDevs [WINMM.@]
2913 UINT WINAPI waveInGetNumDevs(void)
2915 if(!WINMM_StartDevicesThread())
2918 TRACE("count: %u\n", g_inmmdevices_count);
2920 return g_inmmdevices_count;
2923 /**************************************************************************
2924 * waveInGetDevCapsW [WINMM.@]
2926 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
2928 WAVEINCAPSW mapper_caps, *caps;
2930 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2932 if(!WINMM_StartDevicesThread())
2933 return MMSYSERR_ERROR;
2936 return MMSYSERR_INVALPARAM;
2938 if(WINMM_IsMapper(uDeviceID)){
2939 /* FIXME: Should be localized */
2940 static const WCHAR mapper_pnameW[] = {'W','i','n','e',' ','S','o','u',
2941 'n','d',' ','M','a','p','p','e','r',0};
2943 mapper_caps.wMid = 0xFF;
2944 mapper_caps.wPid = 0xFF;
2945 mapper_caps.vDriverVersion = 0x00010001;
2946 mapper_caps.dwFormats = 0xFFFFFFFF;
2947 mapper_caps.wReserved1 = 0;
2948 mapper_caps.wChannels = 2;
2949 lstrcpyW(mapper_caps.szPname, mapper_pnameW);
2951 caps = &mapper_caps;
2953 if(uDeviceID >= g_inmmdevices_count)
2954 return MMSYSERR_BADDEVICEID;
2956 caps = &g_in_mmdevices[uDeviceID].in_caps;
2959 memcpy(lpCaps, caps, min(uSize, sizeof(*lpCaps)));
2961 return MMSYSERR_NOERROR;
2964 /**************************************************************************
2965 * waveInGetDevCapsA [WINMM.@]
2967 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
2972 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
2974 if(!WINMM_StartDevicesThread())
2975 return MMSYSERR_ERROR;
2978 return MMSYSERR_INVALPARAM;
2980 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
2982 if (ret == MMSYSERR_NOERROR) {
2984 wicA.wMid = wicW.wMid;
2985 wicA.wPid = wicW.wPid;
2986 wicA.vDriverVersion = wicW.vDriverVersion;
2987 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
2988 sizeof(wicA.szPname), NULL, NULL );
2989 wicA.dwFormats = wicW.dwFormats;
2990 wicA.wChannels = wicW.wChannels;
2991 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
2996 /**************************************************************************
2997 * waveInOpen [WINMM.@]
2999 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
3000 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
3001 DWORD_PTR dwInstance, DWORD dwFlags)
3005 WINMM_OpenInfo info;
3006 WINMM_CBInfo cb_info;
3008 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn, uDeviceID, lpFormat,
3009 dwCallback, dwInstance, dwFlags);
3011 if(!WINMM_StartDevicesThread())
3012 return MMSYSERR_ERROR;
3014 if(!lphWaveIn && !(dwFlags & WAVE_FORMAT_QUERY))
3015 return MMSYSERR_INVALPARAM;
3017 hr = WINMM_StartDevicesThread();
3019 ERR("Couldn't start the device thread: %08x\n", hr);
3020 return MMSYSERR_ERROR;
3023 info.format = (WAVEFORMATEX*)lpFormat;
3024 info.callback = dwCallback;
3025 info.cb_user = dwInstance;
3026 info.req_device = uDeviceID;
3027 info.flags = dwFlags;
3029 res = SendMessageW(g_devices_hwnd, WIDM_OPEN, (DWORD_PTR)&info, 0);
3030 if(res != MMSYSERR_NOERROR)
3034 *lphWaveIn = (HWAVEIN)info.handle;
3036 cb_info.flags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3037 cb_info.callback = dwCallback;
3038 cb_info.user = dwInstance;
3039 cb_info.hwave = info.handle;
3041 WINMM_NotifyClient(&cb_info, WIM_OPEN, 0, 0);
3046 /**************************************************************************
3047 * waveInClose [WINMM.@]
3049 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
3051 WINMM_Device *device;
3052 WINMM_CBInfo cb_info;
3055 TRACE("(%p)\n", hWaveIn);
3057 if(!WINMM_StartDevicesThread())
3058 return MMSYSERR_ERROR;
3060 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3062 if(!WINMM_ValidateAndLock(device))
3063 return MMSYSERR_INVALHANDLE;
3065 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
3067 LeaveCriticalSection(&device->lock);
3069 res = SendMessageW(g_devices_hwnd, WIDM_CLOSE, (WPARAM)hWaveIn, 0);
3071 if(res == MMSYSERR_NOERROR)
3072 WINMM_NotifyClient(&cb_info, WIM_CLOSE, 0, 0);
3077 /**************************************************************************
3078 * waveInPrepareHeader [WINMM.@]
3080 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3083 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3085 if(!WINMM_StartDevicesThread())
3086 return MMSYSERR_ERROR;
3088 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3089 return MMSYSERR_INVALPARAM;
3091 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3092 return WAVERR_STILLPLAYING;
3094 return WINMM_PrepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3097 /**************************************************************************
3098 * waveInUnprepareHeader [WINMM.@]
3100 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
3103 TRACE("(%p, %p, %u)\n", hWaveIn, lpWaveInHdr, uSize);
3105 if(!WINMM_StartDevicesThread())
3106 return MMSYSERR_ERROR;
3108 if(!lpWaveInHdr || uSize < sizeof(WAVEHDR))
3109 return MMSYSERR_INVALPARAM;
3111 if(!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
3112 return MMSYSERR_NOERROR;
3114 if(lpWaveInHdr->dwFlags & WHDR_INQUEUE)
3115 return WAVERR_STILLPLAYING;
3117 return WINMM_UnprepareHeader((HWAVE)hWaveIn, lpWaveInHdr);
3120 /**************************************************************************
3121 * waveInAddBuffer [WINMM.@]
3123 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn, WAVEHDR *header, UINT uSize)
3125 WINMM_Device *device;
3127 TRACE("(%p, %p, %u)\n", hWaveIn, header, uSize);
3129 if(!header || uSize < sizeof(WAVEHDR))
3130 return MMSYSERR_INVALPARAM;
3132 if(!(header->dwFlags & WHDR_PREPARED))
3133 return WAVERR_UNPREPARED;
3135 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3137 if(!WINMM_ValidateAndLock(device))
3138 return MMSYSERR_INVALHANDLE;
3141 device->first = device->last = header;
3143 device->last->lpNext = header;
3144 device->last = header;
3147 header->dwBytesRecorded = 0;
3148 header->lpNext = NULL;
3149 header->dwFlags &= ~WHDR_DONE;
3150 header->dwFlags |= WHDR_INQUEUE;
3152 LeaveCriticalSection(&device->lock);
3154 return MMSYSERR_NOERROR;
3157 /**************************************************************************
3158 * waveInReset [WINMM.@]
3160 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
3162 TRACE("(%p)\n", hWaveIn);
3164 if(!WINMM_StartDevicesThread())
3165 return MMSYSERR_ERROR;
3167 return WINMM_Reset((HWAVE)hWaveIn);
3170 /**************************************************************************
3171 * waveInStart [WINMM.@]
3173 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
3175 WINMM_Device *device;
3178 TRACE("(%p)\n", hWaveIn);
3180 if(!WINMM_StartDevicesThread())
3181 return MMSYSERR_ERROR;
3183 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3185 if(!WINMM_ValidateAndLock(device))
3186 return MMSYSERR_INVALHANDLE;
3188 hr = WINMM_BeginPlaying(device);
3190 LeaveCriticalSection(&device->lock);
3191 return MMSYSERR_ERROR;
3194 LeaveCriticalSection(&device->lock);
3196 return MMSYSERR_NOERROR;
3199 /**************************************************************************
3200 * waveInStop [WINMM.@]
3202 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
3204 WINMM_CBInfo cb_info;
3205 WINMM_Device *device;
3209 TRACE("(%p)\n", hWaveIn);
3211 if(!WINMM_StartDevicesThread())
3212 return MMSYSERR_ERROR;
3214 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3216 if(!WINMM_ValidateAndLock(device))
3217 return MMSYSERR_INVALHANDLE;
3219 hr = WINMM_Pause((HWAVE)hWaveIn);
3221 LeaveCriticalSection(&device->lock);
3222 return MMSYSERR_ERROR;
3224 device->stopped = TRUE;
3226 buf = device->first;
3227 if(buf && buf->dwBytesRecorded > 0){
3228 device->first = buf->lpNext;
3229 buf->dwFlags &= ~WHDR_INQUEUE;
3230 buf->dwFlags |= WHDR_DONE;
3234 memcpy(&cb_info, &device->cb_info, sizeof(cb_info));
3236 LeaveCriticalSection(&device->lock);
3239 WINMM_NotifyClient(&cb_info, WIM_DATA, (DWORD_PTR)buf, 0);
3241 return MMSYSERR_NOERROR;
3244 /**************************************************************************
3245 * waveInGetPosition [WINMM.@]
3247 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
3250 TRACE("(%p, %p, %u)\n", hWaveIn, lpTime, uSize);
3252 if(!WINMM_StartDevicesThread())
3253 return MMSYSERR_ERROR;
3255 if(!uSize || !lpTime || uSize != sizeof(MMTIME))
3256 return MMSYSERR_INVALPARAM;
3258 return WINMM_GetPosition((HWAVE)hWaveIn, lpTime);
3261 /**************************************************************************
3262 * waveInGetID [WINMM.@]
3264 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
3268 WINMM_Device *device;
3270 TRACE("(%p, %p)\n", hWaveIn, lpuDeviceID);
3272 if(!WINMM_StartDevicesThread())
3273 return MMSYSERR_ERROR;
3276 return MMSYSERR_INVALPARAM;
3278 device = WINMM_GetDeviceFromHWAVE((HWAVE)hWaveIn);
3279 if(!WINMM_ValidateAndLock(device))
3280 return MMSYSERR_INVALHANDLE;
3282 LeaveCriticalSection(&device->lock);
3284 WINMM_DecomposeHWAVE((HWAVE)hWaveIn, lpuDeviceID, &is_out, &dev, &junk);
3286 return MMSYSERR_NOERROR;
3289 /**************************************************************************
3290 * waveInMessage [WINMM.@]
3292 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
3293 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3295 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
3298 case DRV_QUERYFUNCTIONINSTANCEIDSIZE:
3299 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn),
3300 (DWORD_PTR*)dwParam1, FALSE);
3301 case DRV_QUERYFUNCTIONINSTANCEID:
3302 return WINMM_QueryInstanceID(HandleToULong(hWaveIn),
3303 (WCHAR*)dwParam1, (DWORD_PTR)dwParam2, FALSE);
3304 /* TODO: Remove after dsound has been rewritten for mmdevapi */
3305 case DRV_QUERYDSOUNDDESC:
3306 case DRV_QUERYDSOUNDIFACE:
3307 if(dwParam2 == DS_HW_ACCEL_FULL)
3308 return WINMM_DRVMessage(HandleToULong(hWaveIn), uMessage,
3309 dwParam1, 0, FALSE);
3310 return WINMM_FillDSDriverDesc(HandleToULong(hWaveIn),
3311 (DSDRIVERDESC*)dwParam1, FALSE);
3314 return MMSYSERR_NOTSUPPORTED;
3317 UINT WINAPI mixerGetNumDevs(void)
3321 if(!WINMM_StartDevicesThread())
3324 return g_outmmdevices_count + g_inmmdevices_count;
3327 /**************************************************************************
3328 * mixerGetDevCapsA [WINMM.@]
3330 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
3335 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3338 return MMSYSERR_INVALPARAM;
3340 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
3342 if (ret == MMSYSERR_NOERROR) {
3344 micA.wMid = micW.wMid;
3345 micA.wPid = micW.wPid;
3346 micA.vDriverVersion = micW.vDriverVersion;
3347 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
3348 sizeof(micA.szPname), NULL, NULL );
3349 micA.fdwSupport = micW.fdwSupport;
3350 micA.cDestinations = micW.cDestinations;
3351 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
3356 /**************************************************************************
3357 * mixerGetDevCapsW [WINMM.@]
3359 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
3361 WINMM_MMDevice *mmdevice;
3364 TRACE("(%lu, %p, %u)\n", uDeviceID, lpCaps, uSize);
3366 if(!WINMM_StartDevicesThread())
3367 return MMSYSERR_ERROR;
3370 return MMSYSERR_INVALPARAM;
3373 return MMSYSERR_NOERROR;
3375 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3376 return MMSYSERR_BADDEVICEID;
3378 if(uDeviceID < g_outmmdevices_count){
3379 mmdevice = &g_out_mmdevices[uDeviceID];
3380 memcpy(caps.szPname, mmdevice->out_caps.szPname, sizeof(caps.szPname));
3382 mmdevice = &g_in_mmdevices[uDeviceID - g_outmmdevices_count];
3383 memcpy(caps.szPname, mmdevice->in_caps.szPname, sizeof(caps.szPname));
3388 caps.vDriverVersion = 0x00010001;
3389 caps.fdwSupport = 0;
3390 caps.cDestinations = 1;
3392 memcpy(lpCaps, &caps, uSize);
3394 return MMSYSERR_NOERROR;
3397 /**************************************************************************
3398 * mixerOpen [WINMM.@]
3400 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
3401 DWORD_PTR dwInstance, DWORD fdwOpen)
3403 WINMM_MMDevice *mmdevice;
3406 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix, uDeviceID, dwCallback,
3407 dwInstance, fdwOpen);
3409 if(!WINMM_StartDevicesThread())
3410 return MMSYSERR_ERROR;
3413 return MMSYSERR_INVALPARAM;
3415 if(uDeviceID >= g_outmmdevices_count + g_inmmdevices_count)
3416 return MMSYSERR_BADDEVICEID;
3418 mr = WINMM_CheckCallback(dwCallback, fdwOpen, TRUE);
3419 if(mr != MMSYSERR_NOERROR)
3422 if(uDeviceID < g_outmmdevices_count){
3423 mmdevice = &g_out_mmdevices[uDeviceID];
3424 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID, TRUE,
3425 mmdevice->mixer_count);
3427 mmdevice = &g_in_mmdevices[uDeviceID - g_outmmdevices_count];
3428 *lphMix = (HMIXER)WINMM_MakeHWAVE(uDeviceID - g_outmmdevices_count,
3429 FALSE, mmdevice->mixer_count);
3432 ++mmdevice->mixer_count;
3434 return MMSYSERR_NOERROR;
3437 /**************************************************************************
3438 * mixerClose [WINMM.@]
3440 UINT WINAPI mixerClose(HMIXER hMix)
3442 TRACE("(%p)\n", hMix);
3444 return MMSYSERR_NOERROR;
3447 /**************************************************************************
3448 * mixerGetID [WINMM.@]
3450 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
3452 WINMM_MMDevice *mmdevice;
3454 TRACE("(%p, %p, %x)\n", hmix, lpid, fdwID);
3456 if(!WINMM_StartDevicesThread())
3457 return MMSYSERR_ERROR;
3460 return MMSYSERR_INVALPARAM;
3462 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwID, lpid);
3464 return MMSYSERR_INVALHANDLE;
3466 if(mmdevice->in_caps.szPname[0] != '\0')
3467 *lpid += g_outmmdevices_count;
3469 return MMSYSERR_NOERROR;
3472 /**************************************************************************
3473 * mixerGetControlDetailsW [WINMM.@]
3475 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
3478 WINMM_ControlDetails details;
3481 TRACE("(%p, %p, %x)\n", hmix, lpmcdW, fdwDetails);
3483 if(!WINMM_StartDevicesThread())
3484 return MMSYSERR_ERROR;
3487 return MMSYSERR_INVALPARAM;
3489 TRACE("dwControlID: %u\n", lpmcdW->dwControlID);
3491 hr = WINMM_StartDevicesThread();
3493 ERR("Couldn't start the device thread: %08x\n", hr);
3494 return MMSYSERR_ERROR;
3497 details.hmix = hmix;
3498 details.details = lpmcdW;
3499 details.flags = fdwDetails;
3501 return SendMessageW(g_devices_hwnd, MXDM_GETCONTROLDETAILS,
3502 (DWORD_PTR)&details, 0);
3505 /**************************************************************************
3506 * mixerGetControlDetailsA [WINMM.@]
3508 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
3511 UINT ret = MMSYSERR_NOTSUPPORTED;
3513 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
3515 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
3516 return MMSYSERR_INVALPARAM;
3518 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
3519 case MIXER_GETCONTROLDETAILSF_VALUE:
3520 /* can safely use A structure as it is, no string inside */
3521 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3523 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
3525 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
3526 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
3527 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3530 if (lpmcdA->u.cMultipleItems != 0) {
3531 size *= lpmcdA->u.cMultipleItems;
3533 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
3534 lpmcdA->paDetails = pDetailsW;
3535 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
3536 /* set up lpmcd->paDetails */
3537 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
3538 /* copy from lpmcd->paDetails back to paDetailsW; */
3539 if (ret == MMSYSERR_NOERROR) {
3540 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
3541 pDetailsA->dwParam1 = pDetailsW->dwParam1;
3542 pDetailsA->dwParam2 = pDetailsW->dwParam2;
3543 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
3545 sizeof(pDetailsA->szName), NULL, NULL );
3549 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3550 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
3552 HeapFree(GetProcessHeap(), 0, pDetailsW);
3553 lpmcdA->paDetails = pDetailsA;
3554 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
3558 ERR("Unsupported fdwDetails=0x%08x\n", fdwDetails);
3564 /**************************************************************************
3565 * mixerGetLineControlsA [WINMM.@]
3567 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
3570 MIXERLINECONTROLSW mlcW;
3574 TRACE("(%p, %p, %x)\n", hmix, lpmlcA, fdwControls);
3576 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
3577 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
3578 return MMSYSERR_INVALPARAM;
3580 mlcW.cbStruct = sizeof(mlcW);
3581 mlcW.dwLineID = lpmlcA->dwLineID;
3582 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
3583 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
3585 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3586 the control count is assumed to be 1 - This is relied upon by a game,
3587 "Dynomite Deluze" */
3588 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
3591 mlcW.cControls = lpmlcA->cControls;
3593 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
3594 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
3595 mlcW.cControls * mlcW.cbmxctrl);
3597 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
3599 if (ret == MMSYSERR_NOERROR) {
3600 lpmlcA->dwLineID = mlcW.dwLineID;
3601 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
3602 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
3604 for (i = 0; i < mlcW.cControls; i++) {
3605 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
3606 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
3607 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
3608 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
3609 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
3610 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
3611 lpmlcA->pamxctrl[i].szShortName,
3612 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
3613 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
3614 lpmlcA->pamxctrl[i].szName,
3615 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
3616 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3617 * sizeof(mlcW.pamxctrl[i].Bounds) */
3618 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
3619 sizeof(mlcW.pamxctrl[i].Bounds));
3620 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3621 * sizeof(mlcW.pamxctrl[i].Metrics) */
3622 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
3623 sizeof(mlcW.pamxctrl[i].Metrics));
3627 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
3632 static UINT WINMM_GetVolumeLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3633 MIXERCONTROLW *ctl, DWORD flags)
3635 ctl->dwControlID = (line == 0xFFFF0000) ? 0 : 2;
3636 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
3637 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
3638 ctl->cMultipleItems = 0;
3639 lstrcpyW(ctl->szShortName, volumeW);
3640 lstrcpyW(ctl->szName, volumeW);
3641 ctl->Bounds.s1.dwMinimum = 0;
3642 ctl->Bounds.s1.dwMaximum = 0xFFFF;
3643 ctl->Metrics.cSteps = 192;
3645 return MMSYSERR_NOERROR;
3648 static UINT WINMM_GetMuteLineControl(WINMM_MMDevice *mmdevice, DWORD line,
3649 MIXERCONTROLW *ctl, DWORD flags)
3651 ctl->dwControlID = (line == 0xFFFF0000) ? 1 : 3;
3652 ctl->dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
3653 ctl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM;
3654 ctl->cMultipleItems = 0;
3655 lstrcpyW(ctl->szShortName, muteW);
3656 lstrcpyW(ctl->szName, muteW);
3657 ctl->Bounds.s1.dwMinimum = 0;
3658 ctl->Bounds.s1.dwMaximum = 1;
3659 ctl->Metrics.cSteps = 0;
3661 return MMSYSERR_NOERROR;
3664 /**************************************************************************
3665 * mixerGetLineControlsW [WINMM.@]
3667 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
3670 WINMM_MMDevice *mmdevice;
3672 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
3674 if(!WINMM_StartDevicesThread())
3675 return MMSYSERR_ERROR;
3677 if(fdwControls & ~(MIXER_GETLINECONTROLSF_ALL |
3678 MIXER_GETLINECONTROLSF_ONEBYID |
3679 MIXER_GETLINECONTROLSF_ONEBYTYPE |
3680 MIXER_OBJECTF_HMIXER |
3681 MIXER_OBJECTF_MIXER)){
3682 WARN("Unknown GetLineControls flag: %x\n", fdwControls);
3683 return MMSYSERR_INVALFLAG;
3686 if(!lpmlcW || lpmlcW->cbStruct < sizeof(*lpmlcW) || !lpmlcW->pamxctrl)
3687 return MMSYSERR_INVALPARAM;
3689 TRACE("dwLineID: %u\n", lpmlcW->dwLineID);
3690 TRACE("dwControl: %x\n", lpmlcW->u.dwControlID);
3691 TRACE("cControls: %u\n", lpmlcW->cControls);
3693 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwControls, NULL);
3695 return MMSYSERR_INVALHANDLE;
3697 switch(fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK){
3698 case MIXER_GETLINECONTROLSF_ALL:
3699 if(lpmlcW->cControls != 2)
3700 return MMSYSERR_INVALPARAM;
3701 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
3702 return MMSYSERR_INVALPARAM;
3703 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
3704 return MIXERR_INVALLINE;
3705 WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
3706 &lpmlcW->pamxctrl[0], fdwControls);
3707 WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
3708 &lpmlcW->pamxctrl[1], fdwControls);
3709 return MMSYSERR_NOERROR;
3710 case MIXER_GETLINECONTROLSF_ONEBYID:
3711 if(lpmlcW->cControls != 1)
3712 return MMSYSERR_INVALPARAM;
3713 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
3714 return MMSYSERR_INVALPARAM;
3715 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
3716 return MIXERR_INVALLINE;
3717 if(lpmlcW->u.dwControlID == 0)
3718 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
3719 lpmlcW->pamxctrl, fdwControls);
3720 if(lpmlcW->u.dwControlID == 1)
3721 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
3722 lpmlcW->pamxctrl, fdwControls);
3723 return MMSYSERR_NOTSUPPORTED;
3724 case MIXER_GETLINECONTROLSF_ONEBYTYPE:
3725 if(lpmlcW->cControls != 1)
3726 return MMSYSERR_INVALPARAM;
3727 if(lpmlcW->cbmxctrl < sizeof(MIXERCONTROLW))
3728 return MMSYSERR_INVALPARAM;
3729 if(lpmlcW->dwLineID != 0 && lpmlcW->dwLineID != 0xFFFF0000)
3730 return MIXERR_INVALLINE;
3731 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
3732 return WINMM_GetVolumeLineControl(mmdevice, lpmlcW->dwLineID,
3733 lpmlcW->pamxctrl, fdwControls);
3734 if(lpmlcW->u.dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
3735 return WINMM_GetMuteLineControl(mmdevice, lpmlcW->dwLineID,
3736 lpmlcW->pamxctrl, fdwControls);
3737 return MMSYSERR_NOTSUPPORTED;
3740 return MMSYSERR_NOTSUPPORTED;
3743 static UINT WINMM_GetSourceLineInfo(WINMM_MMDevice *mmdevice, UINT mmdev_index,
3744 MIXERLINEW *info, DWORD flags)
3747 if(mmdevice->in_caps.szPname[0] != '\0')
3750 if(info->dwSource != 0)
3751 return MIXERR_INVALLINE;
3753 info->dwDestination = 0;
3755 info->fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
3756 info->cConnections = 0;
3757 info->cControls = 2;
3758 /* volume & mute always affect all channels, so claim 1 channel */
3759 info->cChannels = 1;
3760 info->Target.dwDeviceID = mmdev_index;
3761 info->Target.wMid = ~0;
3762 info->Target.wPid = ~0;
3763 info->Target.vDriverVersion = 0;
3765 lstrcpyW(info->szShortName, volumeW);
3766 lstrcpyW(info->szName, mastervolumeW);
3769 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
3770 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEOUT;
3771 memcpy(info->Target.szPname, mmdevice->out_caps.szPname,
3772 sizeof(info->Target.szPname));
3774 info->dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_LINE;
3775 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
3776 info->Target.szPname[0] = '\0';
3779 return MMSYSERR_NOERROR;
3782 static UINT WINMM_GetDestinationLineInfo(WINMM_MMDevice *mmdevice,
3783 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3786 if(mmdevice->in_caps.szPname[0] != '\0')
3789 if(info->dwDestination != 0)
3790 return MIXERR_INVALLINE;
3792 info->dwSource = 0xFFFFFFFF;
3793 info->dwLineID = 0xFFFF0000;
3794 info->fdwLine = MIXERLINE_LINEF_ACTIVE;
3795 info->cConnections = 1;
3796 info->cControls = 2;
3798 lstrcpyW(info->szShortName, volumeW);
3799 lstrcpyW(info->szName, mastervolumeW);
3801 info->Target.dwDeviceID = mmdev_index;
3802 info->Target.wMid = ~0;
3803 info->Target.wPid = ~0;
3804 info->Target.vDriverVersion = 0;
3807 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
3808 info->cChannels = mmdevice->out_caps.wChannels;
3809 info->Target.dwType = MIXERLINE_TARGETTYPE_UNDEFINED;
3810 info->Target.szPname[0] = '\0';
3812 info->dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
3813 info->cChannels = mmdevice->in_caps.wChannels;
3814 info->Target.dwType = MIXERLINE_TARGETTYPE_WAVEIN;
3815 memcpy(info->Target.szPname, mmdevice->in_caps.szPname,
3816 sizeof(info->Target.szPname));
3819 return MMSYSERR_NOERROR;
3822 static UINT WINMM_GetComponentTypeLineInfo(WINMM_MMDevice *mmdevice,
3823 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3826 if(mmdevice->in_caps.szPname[0] != '\0')
3829 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_WAVEIN){
3831 return MIXERR_INVALLINE;
3832 info->dwDestination = 0;
3833 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3836 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_DST_SPEAKERS){
3838 return MIXERR_INVALLINE;
3839 info->dwDestination = 0;
3840 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3843 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_LINE){
3845 return MIXERR_INVALLINE;
3847 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3850 if(info->dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT){
3852 return MIXERR_INVALLINE;
3854 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3857 TRACE("Returning INVALLINE on this component type: %u\n",
3858 info->dwComponentType);
3860 return MIXERR_INVALLINE;
3863 static UINT WINMM_GetLineIDLineInfo(WINMM_MMDevice *mmdevice,
3864 UINT mmdev_index, MIXERLINEW *info, DWORD flags)
3866 if(info->dwLineID == 0xFFFF0000){
3867 info->dwDestination = 0;
3868 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, info, flags);
3871 if(info->dwLineID == 0){
3873 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, info, flags);
3876 TRACE("Returning INVALLINE on this dwLineID: %u\n", info->dwLineID);
3877 return MIXERR_INVALLINE;
3880 /**************************************************************************
3881 * mixerGetLineInfoW [WINMM.@]
3883 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
3886 WINMM_MMDevice *mmdevice;
3888 TRACE("(%p, %p, %x)\n", hmix, lpmliW, fdwInfo);
3890 if(!WINMM_StartDevicesThread())
3891 return MMSYSERR_ERROR;
3893 if(!lpmliW || lpmliW->cbStruct < sizeof(MIXERLINEW))
3894 return MMSYSERR_INVALPARAM;
3896 TRACE("dwDestination: %u\n", lpmliW->dwDestination);
3897 TRACE("dwSource: %u\n", lpmliW->dwSource);
3898 TRACE("dwLineID: %u\n", lpmliW->dwLineID);
3899 TRACE("fdwLine: 0x%x\n", lpmliW->fdwLine);
3900 TRACE("dwComponentType: 0x%x\n", lpmliW->dwComponentType);
3902 if(fdwInfo & ~(MIXER_GETLINEINFOF_COMPONENTTYPE |
3903 MIXER_GETLINEINFOF_DESTINATION |
3904 MIXER_GETLINEINFOF_LINEID |
3905 MIXER_GETLINEINFOF_SOURCE |
3906 MIXER_GETLINEINFOF_TARGETTYPE |
3907 MIXER_OBJECTF_HMIXER |
3908 MIXER_OBJECTF_MIXER)){
3909 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo);
3910 return MMSYSERR_INVALFLAG;
3913 mmdevice = WINMM_GetMixerMMDevice(hmix, fdwInfo, &mmdev_index);
3915 return MMSYSERR_INVALHANDLE;
3917 switch(fdwInfo & MIXER_GETLINEINFOF_QUERYMASK){
3918 case MIXER_GETLINEINFOF_DESTINATION:
3919 return WINMM_GetDestinationLineInfo(mmdevice, mmdev_index, lpmliW,
3921 case MIXER_GETLINEINFOF_SOURCE:
3922 return WINMM_GetSourceLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
3923 case MIXER_GETLINEINFOF_COMPONENTTYPE:
3924 return WINMM_GetComponentTypeLineInfo(mmdevice, mmdev_index, lpmliW,
3926 case MIXER_GETLINEINFOF_LINEID:
3927 return WINMM_GetLineIDLineInfo(mmdevice, mmdev_index, lpmliW, fdwInfo);
3928 case MIXER_GETLINEINFOF_TARGETTYPE:
3929 FIXME("TARGETTYPE flag not implemented!\n");
3930 return MIXERR_INVALLINE;
3933 TRACE("Returning INVALFLAG on these flags: %lx\n",
3934 fdwInfo & MIXER_GETLINEINFOF_QUERYMASK);
3935 return MMSYSERR_INVALFLAG;
3938 /**************************************************************************
3939 * mixerGetLineInfoA [WINMM.@]
3941 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
3947 TRACE("(%p, %p, %x)\n", hmix, lpmliA, fdwInfo);
3949 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
3950 return MMSYSERR_INVALPARAM;
3952 mliW.cbStruct = sizeof(mliW);
3953 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
3954 case MIXER_GETLINEINFOF_COMPONENTTYPE:
3955 mliW.dwComponentType = lpmliA->dwComponentType;
3957 case MIXER_GETLINEINFOF_DESTINATION:
3958 mliW.dwDestination = lpmliA->dwDestination;
3960 case MIXER_GETLINEINFOF_LINEID:
3961 mliW.dwLineID = lpmliA->dwLineID;
3963 case MIXER_GETLINEINFOF_SOURCE:
3964 mliW.dwDestination = lpmliA->dwDestination;
3965 mliW.dwSource = lpmliA->dwSource;
3967 case MIXER_GETLINEINFOF_TARGETTYPE:
3968 mliW.Target.dwType = lpmliA->Target.dwType;
3969 mliW.Target.wMid = lpmliA->Target.wMid;
3970 mliW.Target.wPid = lpmliA->Target.wPid;
3971 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
3972 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
3975 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
3976 return MMSYSERR_INVALFLAG;
3979 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
3981 if(ret == MMSYSERR_NOERROR)
3983 lpmliA->dwDestination = mliW.dwDestination;
3984 lpmliA->dwSource = mliW.dwSource;
3985 lpmliA->dwLineID = mliW.dwLineID;
3986 lpmliA->fdwLine = mliW.fdwLine;
3987 lpmliA->dwUser = mliW.dwUser;
3988 lpmliA->dwComponentType = mliW.dwComponentType;
3989 lpmliA->cChannels = mliW.cChannels;
3990 lpmliA->cConnections = mliW.cConnections;
3991 lpmliA->cControls = mliW.cControls;
3992 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
3993 sizeof(lpmliA->szShortName), NULL, NULL);
3994 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
3995 sizeof(lpmliA->szName), NULL, NULL );
3996 lpmliA->Target.dwType = mliW.Target.dwType;
3997 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
3998 lpmliA->Target.wMid = mliW.Target.wMid;
3999 lpmliA->Target.wPid = mliW.Target.wPid;
4000 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
4001 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
4002 sizeof(lpmliA->Target.szPname), NULL, NULL );
4007 /**************************************************************************
4008 * mixerSetControlDetails [WINMM.@]
4010 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
4013 WINMM_ControlDetails details;
4016 TRACE("(%p, %p, %x)\n", hmix, lpmcd, fdwDetails);
4018 if(!WINMM_StartDevicesThread())
4019 return MMSYSERR_ERROR;
4021 if((fdwDetails & MIXER_SETCONTROLDETAILSF_QUERYMASK) ==
4022 MIXER_SETCONTROLDETAILSF_CUSTOM)
4023 return MMSYSERR_NOTSUPPORTED;
4026 return MMSYSERR_INVALPARAM;
4028 TRACE("dwControlID: %u\n", lpmcd->dwControlID);
4030 hr = WINMM_StartDevicesThread();
4032 ERR("Couldn't start the device thread: %08x\n", hr);
4033 return MMSYSERR_ERROR;
4036 details.hmix = hmix;
4037 details.details = lpmcd;
4038 details.flags = fdwDetails;
4040 return SendMessageW(g_devices_hwnd, MXDM_SETCONTROLDETAILS,
4041 (DWORD_PTR)&details, 0);
4044 /**************************************************************************
4045 * mixerMessage [WINMM.@]
4047 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
4049 TRACE("(%p, %d, %lx, %lx)\n", hmix, uMsg, dwParam1, dwParam2);
4051 return MMSYSERR_NOTSUPPORTED;