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
44 #include "mmdeviceapi.h"
45 #include "audioclient.h"
46 #include "audiopolicy.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(winmm
);
52 /* HWAVE (and HMIXER) format:
54 * XXXX... 1FDD DDDD IIII IIII
55 * X = unused (must be 0)
56 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
57 * F = flow direction (0 = IN, 1 = OUT)
58 * D = index into g_out_mmdevices, or all 1s for the MAPPER device
59 * I = index in the mmdevice's devices array
61 * Two reasons that we don't just use pointers:
62 * - HWAVEs must fit into 16 bits for compatibility with old applications.
63 * - We must be able to identify bad devices without crashing.
66 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
67 #define AC_BUFLEN (10 * 100000)
68 #define MAX_DEVICES 256
69 #define MAPPER_INDEX 0x3F
71 typedef struct _WINMM_CBInfo
{
78 struct _WINMM_MMDevice
;
79 typedef struct _WINMM_MMDevice WINMM_MMDevice
;
81 typedef struct _WINMM_Device
{
90 IAudioRenderClient
*render
;
91 IAudioCaptureClient
*capture
;
93 IAudioStreamVolume
*volume
;
95 WAVEFORMATEX
*orig_fmt
;
96 HACMSTREAM acm_handle
;
97 ACMSTREAMHEADER acm_hdr
;
100 WAVEHDR
*first
, *last
, *playing
, *loop_start
;
104 UINT32 bytes_per_frame
, samples_per_sec
, ofs_bytes
, played_frames
;
105 UINT32 remainder_frames
; /* header chunk frames already played when a device switch occurred */
107 /* stored in frames of sample rate, *not* AC::GetFrequency */
108 UINT64 last_clock_pos
;
111 CRITICAL_SECTION lock
;
113 WINMM_MMDevice
*parent
;
116 struct _WINMM_MMDevice
{
117 WAVEOUTCAPSW out_caps
; /* must not be modified outside of WINMM_InitMMDevices*/
118 WAVEINCAPSW in_caps
; /* must not be modified outside of WINMM_InitMMDevices*/
122 ISimpleAudioVolume
*volume
;
128 /* HMIXER format is the same as the HWAVE format, but the I bits are
129 * replaced by the value of this counter, to keep each HMIXER unique */
132 CRITICAL_SECTION lock
;
134 WINMM_Device
*devices
[MAX_DEVICES
];
137 static WINMM_MMDevice
*g_out_mmdevices
;
138 static WINMM_MMDevice
**g_out_map
;
139 static UINT g_outmmdevices_count
;
140 static WINMM_Device
*g_out_mapper_devices
[MAX_DEVICES
];
142 static WINMM_MMDevice
*g_in_mmdevices
;
143 static WINMM_MMDevice
**g_in_map
;
144 static UINT g_inmmdevices_count
;
145 static WINMM_Device
*g_in_mapper_devices
[MAX_DEVICES
];
147 static IMMDeviceEnumerator
*g_devenum
;
149 static CRITICAL_SECTION g_devthread_lock
;
150 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug
=
152 0, 0, &g_devthread_lock
,
153 { &g_devthread_lock_debug
.ProcessLocksList
, &g_devthread_lock_debug
.ProcessLocksList
},
154 0, 0, { (DWORD_PTR
)(__FILE__
": g_devthread_lock") }
156 static CRITICAL_SECTION g_devthread_lock
= { &g_devthread_lock_debug
, -1, 0, 0, 0, 0 };
157 static LONG g_devthread_token
;
158 static HANDLE g_devices_thread
;
159 static HWND g_devices_hwnd
;
160 static HMODULE g_devthread_module
;
162 static UINT g_devhandle_count
;
163 static HANDLE
*g_device_handles
;
164 static WINMM_Device
**g_handle_devices
;
166 typedef struct _WINMM_OpenInfo
{
169 WAVEFORMATEX
*format
;
176 typedef struct _WINMM_ControlDetails
{
178 MIXERCONTROLDETAILS
*details
;
180 } WINMM_ControlDetails
;
182 typedef struct _WINMM_QueryInterfaceInfo
{
187 } WINMM_QueryInterfaceInfo
;
189 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
);
190 static LRESULT
WOD_Close(HWAVEOUT hwave
);
191 static LRESULT
WID_Open(WINMM_OpenInfo
*info
);
192 static LRESULT
WID_Close(HWAVEIN hwave
);
193 static MMRESULT
WINMM_BeginPlaying(WINMM_Device
*device
);
195 void WINMM_DeleteWaveform(void)
200 CloseHandle(g_devices_thread
);
202 for(i
= 0; i
< g_outmmdevices_count
; ++i
){
203 WINMM_MMDevice
*mmdevice
= &g_out_mmdevices
[i
];
205 for(j
= 0; j
< MAX_DEVICES
&& mmdevice
->devices
[j
]; ++j
){
206 WINMM_Device
*device
= mmdevice
->devices
[j
];
208 CloseHandle(device
->handle
);
209 device
->lock
.DebugInfo
->Spare
[0] = 0;
210 DeleteCriticalSection(&device
->lock
);
214 ISimpleAudioVolume_Release(mmdevice
->volume
);
215 CoTaskMemFree(mmdevice
->dev_id
);
216 mmdevice
->lock
.DebugInfo
->Spare
[0] = 0;
217 DeleteCriticalSection(&mmdevice
->lock
);
220 for(i
= 0; i
< g_inmmdevices_count
; ++i
){
221 WINMM_MMDevice
*mmdevice
= &g_in_mmdevices
[i
];
223 for(j
= 0; j
< MAX_DEVICES
&& mmdevice
->devices
[j
]; ++j
){
224 WINMM_Device
*device
= mmdevice
->devices
[j
];
226 CloseHandle(device
->handle
);
227 device
->lock
.DebugInfo
->Spare
[0] = 0;
228 DeleteCriticalSection(&device
->lock
);
232 ISimpleAudioVolume_Release(mmdevice
->volume
);
233 CoTaskMemFree(mmdevice
->dev_id
);
234 mmdevice
->lock
.DebugInfo
->Spare
[0] = 0;
235 DeleteCriticalSection(&mmdevice
->lock
);
238 HeapFree(GetProcessHeap(), 0, g_out_mmdevices
);
239 HeapFree(GetProcessHeap(), 0, g_in_mmdevices
);
241 HeapFree(GetProcessHeap(), 0, g_device_handles
);
242 HeapFree(GetProcessHeap(), 0, g_handle_devices
);
244 DeleteCriticalSection(&g_devthread_lock
);
247 static inline HWAVE
WINMM_MakeHWAVE(UINT mmdevice
, BOOL is_out
, UINT device
)
249 return ULongToHandle((1 << 15) | ((!!is_out
) << 14) |
250 (mmdevice
<< 8) | device
);
253 static inline void WINMM_DecomposeHWAVE(HWAVE hwave
, UINT
*mmdevice_index
,
254 BOOL
*is_out
, UINT
*device_index
, UINT
*junk
)
256 ULONG32 l
= HandleToULong(hwave
);
257 *device_index
= l
& 0xFF;
258 *mmdevice_index
= (l
>> 8) & 0x3F;
259 *is_out
= (l
>> 14) & 0x1;
263 static void WINMM_InitDevice(WINMM_Device
*device
)
265 InitializeCriticalSection(&device
->lock
);
266 device
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
269 static inline WINMM_MMDevice
*read_map(WINMM_MMDevice
**map
, UINT index
)
272 EnterCriticalSection(&g_devthread_lock
);
274 LeaveCriticalSection(&g_devthread_lock
);
278 /* finds the first unused Device, marks it as "open", and returns
279 * a pointer to the device
281 * IMPORTANT: it is the caller's responsibility to release the device's lock
284 static WINMM_Device
*WINMM_FindUnusedDevice(WINMM_Device
**devices
,
285 WINMM_MMDevice
*parent
, UINT internal_index
, BOOL is_out
)
289 for(i
= 0; i
< MAX_DEVICES
; ++i
){
290 WINMM_Device
*device
= devices
[i
];
293 device
= devices
[i
] = HeapAlloc(GetProcessHeap(),
294 HEAP_ZERO_MEMORY
, sizeof(WINMM_Device
));
298 WINMM_InitDevice(device
);
299 EnterCriticalSection(&device
->lock
);
301 EnterCriticalSection(&device
->lock
);
304 device
->handle
= WINMM_MakeHWAVE(internal_index
, is_out
, i
);
305 device
->parent
= parent
;
311 LeaveCriticalSection(&device
->lock
);
314 TRACE("All devices in use: mmdevice: %u\n", internal_index
);
319 static inline BOOL
WINMM_ValidateAndLock(WINMM_Device
*device
)
324 EnterCriticalSection(&device
->lock
);
327 LeaveCriticalSection(&device
->lock
);
334 static WINMM_Device
*WINMM_GetDeviceFromHWAVE(HWAVE hwave
)
336 WINMM_MMDevice
*mmdevice
;
337 WINMM_Device
*device
;
338 UINT mmdevice_index
, device_index
, junk
;
341 WINMM_DecomposeHWAVE(hwave
, &mmdevice_index
, &is_out
, &device_index
, &junk
);
346 if(mmdevice_index
== MAPPER_INDEX
){
347 EnterCriticalSection(&g_devthread_lock
);
349 device
= g_out_mapper_devices
[device_index
];
351 device
= g_in_mapper_devices
[device_index
];
352 LeaveCriticalSection(&g_devthread_lock
);
356 if(mmdevice_index
>= (is_out
? g_outmmdevices_count
: g_inmmdevices_count
))
360 mmdevice
= &g_out_mmdevices
[mmdevice_index
];
362 mmdevice
= &g_in_mmdevices
[mmdevice_index
];
364 EnterCriticalSection(&mmdevice
->lock
);
366 device
= mmdevice
->devices
[device_index
];
368 LeaveCriticalSection(&mmdevice
->lock
);
373 /* Note: NotifyClient should never be called while holding the device lock
374 * since the client may call wave* functions from within the callback. */
375 static inline void WINMM_NotifyClient(WINMM_CBInfo
*info
, WORD msg
, DWORD_PTR param1
,
378 DriverCallback(info
->callback
, info
->flags
, (HDRVR
)info
->hwave
,
379 msg
, info
->user
, param1
, param2
);
382 static MMRESULT
hr2mmr(HRESULT hr
)
386 case AUDCLNT_E_NOT_STOPPED
:
387 return MMSYSERR_NOERROR
;
388 case AUDCLNT_E_UNSUPPORTED_FORMAT
:
389 return WAVERR_BADFORMAT
;
390 case AUDCLNT_E_DEVICE_IN_USE
:
391 return MMSYSERR_ALLOCATED
;
392 case AUDCLNT_E_ENDPOINT_CREATE_FAILED
:
393 return MMSYSERR_NOTENABLED
;
395 return MMSYSERR_NOMEM
;
398 return MMSYSERR_INVALPARAM
;
399 case AUDCLNT_E_DEVICE_INVALIDATED
: /* DSERR_BUFFERLOST */
401 return FAILED(hr
) ? MMSYSERR_ERROR
: MMSYSERR_NOERROR
;
405 static HRESULT
WINMM_GetFriendlyName(IMMDevice
*device
, WCHAR
*out
,
412 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
416 PropVariantInit(&var
);
418 hr
= IPropertyStore_GetValue(ps
,
419 (PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &var
);
421 IPropertyStore_Release(ps
);
425 lstrcpynW(out
, var
.u
.pwszVal
, outlen
);
427 PropVariantClear(&var
);
429 IPropertyStore_Release(ps
);
434 static HRESULT
WINMM_TestFormat(IAudioClient
*client
, DWORD rate
, DWORD depth
,
437 WAVEFORMATEX fmt
, *junk
;
440 fmt
.wFormatTag
= WAVE_FORMAT_PCM
;
441 fmt
.nChannels
= channels
;
442 fmt
.nSamplesPerSec
= rate
;
443 fmt
.wBitsPerSample
= depth
;
444 fmt
.nBlockAlign
= (channels
* depth
) / 8;
445 fmt
.nAvgBytesPerSec
= rate
* fmt
.nBlockAlign
;
448 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
,
456 static struct _TestFormat
{
461 } formats_to_test
[] = {
462 { WAVE_FORMAT_1M08
, 11025, 8, 1 },
463 { WAVE_FORMAT_1M16
, 11025, 16, 1 },
464 { WAVE_FORMAT_1S08
, 11025, 8, 2 },
465 { WAVE_FORMAT_1S16
, 11025, 16, 2 },
466 { WAVE_FORMAT_2M08
, 22050, 8, 1 },
467 { WAVE_FORMAT_2M16
, 22050, 16, 1 },
468 { WAVE_FORMAT_2S08
, 22050, 8, 2 },
469 { WAVE_FORMAT_2S16
, 22050, 16, 2 },
470 { WAVE_FORMAT_4M08
, 44100, 8, 1 },
471 { WAVE_FORMAT_4M16
, 44100, 16, 1 },
472 { WAVE_FORMAT_4S08
, 44100, 8, 2 },
473 { WAVE_FORMAT_4S16
, 44100, 16, 2 },
474 { WAVE_FORMAT_48M08
, 48000, 8, 1 },
475 { WAVE_FORMAT_48M16
, 48000, 16, 1 },
476 { WAVE_FORMAT_48S08
, 48000, 8, 2 },
477 { WAVE_FORMAT_48S16
, 48000, 16, 2 },
478 { WAVE_FORMAT_96M08
, 96000, 8, 1 },
479 { WAVE_FORMAT_96M16
, 96000, 16, 1 },
480 { WAVE_FORMAT_96S08
, 96000, 8, 2 },
481 { WAVE_FORMAT_96S16
, 96000, 16, 2 },
485 static DWORD
WINMM_GetSupportedFormats(IMMDevice
*device
)
489 struct _TestFormat
*fmt
;
490 IAudioClient
*client
;
492 hr
= IMMDevice_Activate(device
, &IID_IAudioClient
,
493 CLSCTX_INPROC_SERVER
, NULL
, (void**)&client
);
497 for(fmt
= formats_to_test
; fmt
->flag
; ++fmt
){
498 hr
= WINMM_TestFormat(client
, fmt
->rate
, fmt
->depth
, fmt
->channels
);
503 IAudioClient_Release(client
);
508 static HRESULT
WINMM_InitMMDevice(EDataFlow flow
, IMMDevice
*device
,
509 WINMM_MMDevice
*dev
, UINT index
)
513 dev
->dataflow
= flow
;
515 dev
->out_caps
.wMid
= 0xFF;
516 dev
->out_caps
.wPid
= 0xFF;
517 dev
->out_caps
.vDriverVersion
= 0x00010001;
518 dev
->out_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
519 dev
->out_caps
.wReserved1
= 0;
520 dev
->out_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
521 WAVECAPS_SAMPLEACCURATE
;
522 dev
->out_caps
.wChannels
= 2;
523 dev
->out_caps
.szPname
[0] = '\0';
525 hr
= WINMM_GetFriendlyName(device
, dev
->out_caps
.szPname
,
526 sizeof(dev
->out_caps
.szPname
) /
527 sizeof(*dev
->out_caps
.szPname
));
531 dev
->in_caps
.wMid
= 0xFF;
532 dev
->in_caps
.wPid
= 0xFF;
533 dev
->in_caps
.vDriverVersion
= 0x00010001;
534 dev
->in_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
535 dev
->in_caps
.wReserved1
= 0;
536 dev
->in_caps
.wChannels
= 2;
537 dev
->in_caps
.szPname
[0] = '\0';
539 hr
= WINMM_GetFriendlyName(device
, dev
->in_caps
.szPname
,
540 sizeof(dev
->in_caps
.szPname
) /
541 sizeof(*dev
->in_caps
.szPname
));
546 hr
= IMMDevice_GetId(device
, &dev
->dev_id
);
550 CoCreateGuid(&dev
->session
);
554 InitializeCriticalSection(&dev
->lock
);
555 dev
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
560 static HRESULT
WINMM_EnumDevices(WINMM_MMDevice
**devices
,
561 WINMM_MMDevice
***map
, UINT
*devcount
, EDataFlow flow
,
562 IMMDeviceEnumerator
*devenum
)
564 IMMDeviceCollection
*devcoll
;
567 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flow
,
568 DEVICE_STATE_ACTIVE
, &devcoll
);
572 hr
= IMMDeviceCollection_GetCount(devcoll
, devcount
);
574 IMMDeviceCollection_Release(devcoll
);
580 IMMDevice
*def_dev
= NULL
;
582 *devices
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
583 sizeof(WINMM_MMDevice
) * (*devcount
));
585 IMMDeviceCollection_Release(devcoll
);
586 return E_OUTOFMEMORY
;
589 *map
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
590 sizeof(WINMM_MMDevice
*) * (*devcount
));
592 IMMDeviceCollection_Release(devcoll
);
593 HeapFree(GetProcessHeap(), 0, *devices
);
594 return E_OUTOFMEMORY
;
597 /* make sure that device 0 is the default device */
598 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
,
599 flow
, eConsole
, &def_dev
);
601 for(n
= 0; n
< *devcount
; ++n
){
604 hr
= IMMDeviceCollection_Item(devcoll
, n
, &device
);
606 WINMM_InitMMDevice(flow
, device
, &(*devices
)[n
], n
);
608 if(device
== def_dev
)
609 (*map
)[0] = &(*devices
)[n
];
611 (*map
)[count
] = &(*devices
)[n
];
615 IMMDevice_Release(device
);
619 IMMDevice_Release(def_dev
);
624 IMMDeviceCollection_Release(devcoll
);
629 static HRESULT WINAPI
notif_QueryInterface(IMMNotificationClient
*iface
,
630 const GUID
*riid
, void **obj
)
632 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid
));
633 return E_NOINTERFACE
;
636 static ULONG WINAPI
notif_AddRef(IMMNotificationClient
*iface
)
641 static ULONG WINAPI
notif_Release(IMMNotificationClient
*iface
)
646 static HRESULT WINAPI
notif_OnDeviceStateChanged(IMMNotificationClient
*iface
,
647 const WCHAR
*device_id
, DWORD new_state
)
649 TRACE("Ignoring OnDeviceStateChanged callback\n");
653 static HRESULT WINAPI
notif_OnDeviceAdded(IMMNotificationClient
*iface
,
654 const WCHAR
*device_id
)
656 TRACE("Ignoring OnDeviceAdded callback\n");
660 static HRESULT WINAPI
notif_OnDeviceRemoved(IMMNotificationClient
*iface
,
661 const WCHAR
*device_id
)
663 TRACE("Ignoring OnDeviceRemoved callback\n");
667 static HRESULT
update_mapping(WINMM_MMDevice
***map
, UINT count
,
668 const WCHAR
*default_id
)
670 WINMM_MMDevice
*prev
;
674 for(i
= 0; i
< count
; ++i
){
677 if(!lstrcmpW((*map
)[i
]->dev_id
, default_id
)){
678 (*map
)[0] = (*map
)[i
];
689 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
695 static HRESULT
reroute_mapper_device(WINMM_Device
*device
, BOOL is_out
)
701 UINT64 clock_freq
, clock_pos
;
703 TRACE("rerouting device %p\n", device
->handle
);
705 EnterCriticalSection(&device
->lock
);
707 if(!device
->open
|| device
->acm_handle
){
708 /* Windows 7 doesn't re-route ACM devices, so we don't either.
709 * Seems to be because of the data waveXxxPrepareHeader allocates. */
710 LeaveCriticalSection(&device
->lock
);
714 stopped
= device
->stopped
;
717 info
.req_device
= WAVE_MAPPER
;
718 info
.format
= device
->orig_fmt
;
719 info
.callback
= device
->cb_info
.callback
;
720 info
.cb_user
= device
->cb_info
.user
;
721 /* We have to use direct here so that we don't suddenly introduce ACM
722 * into a playing stream that hasn't been Prepared for it */
723 info
.flags
= (device
->cb_info
.flags
<< 16) | WAVE_FORMAT_DIRECT_QUERY
;
727 mr
= WOD_Open(&info
);
729 mr
= WID_Open(&info
);
731 if(mr
!= MMSYSERR_NOERROR
){
732 TRACE("New default device doesn't support this stream: %p\n", device
->handle
);
733 LeaveCriticalSection(&device
->lock
);
737 hr
= IAudioClient_Stop(device
->client
);
739 WARN("Stop failed: %08x\n", hr
);
741 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
743 WARN("GetFrequency failed: %08x\n", hr
);
744 LeaveCriticalSection(&device
->lock
);
748 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
750 WARN("GetPosition failed: %08x\n", hr
);
751 LeaveCriticalSection(&device
->lock
);
755 device
->remainder_frames
= MulDiv(clock_pos
, device
->samples_per_sec
, clock_freq
) - device
->last_clock_pos
;
757 info
.handle
= device
->handle
;
758 info
.flags
= (device
->cb_info
.flags
<< 16) | WAVE_FORMAT_DIRECT
;
761 WOD_Close((HWAVEOUT
)device
->handle
);
762 device
->parent
= read_map(g_out_map
, 0);
763 mr
= WOD_Open(&info
);
765 WID_Close((HWAVEIN
)device
->handle
);
766 device
->parent
= read_map(g_in_map
, 0);
767 mr
= WID_Open(&info
);
770 if(mr
!= MMSYSERR_NOERROR
){
771 ERR("Opening new default device failed! %u\n", mr
);
772 LeaveCriticalSection(&device
->lock
);
776 HeapFree(GetProcessHeap(), 0, info
.format
);
779 WINMM_BeginPlaying(device
);
781 LeaveCriticalSection(&device
->lock
);
786 static HRESULT WINAPI
notif_OnDefaultDeviceChanged(IMMNotificationClient
*iface
,
787 EDataFlow flow
, ERole role
, const WCHAR
*device_id
)
791 TRACE("%u %u %s\n", flow
, role
, wine_dbgstr_w(device_id
));
796 EnterCriticalSection(&g_devthread_lock
);
799 update_mapping(&g_out_map
, g_outmmdevices_count
, device_id
);
801 update_mapping(&g_in_map
, g_inmmdevices_count
, device_id
);
803 for(i
= 0; i
< MAX_DEVICES
&& g_out_mapper_devices
[i
]; ++i
)
804 reroute_mapper_device(g_out_mapper_devices
[i
], TRUE
);
806 for(i
= 0; i
< MAX_DEVICES
&& g_in_mapper_devices
[i
]; ++i
)
807 reroute_mapper_device(g_in_mapper_devices
[i
], FALSE
);
809 LeaveCriticalSection(&g_devthread_lock
);
814 static HRESULT WINAPI
notif_OnPropertyValueChanged(IMMNotificationClient
*iface
,
815 const WCHAR
*device_id
, const PROPERTYKEY key
)
817 TRACE("Ignoring OnPropertyValueChanged callback\n");
821 static IMMNotificationClientVtbl g_notif_vtbl
= {
822 notif_QueryInterface
,
825 notif_OnDeviceStateChanged
,
827 notif_OnDeviceRemoved
,
828 notif_OnDefaultDeviceChanged
,
829 notif_OnPropertyValueChanged
832 static IMMNotificationClient g_notif
= { &g_notif_vtbl
};
834 static HRESULT
WINMM_InitMMDevices(void)
837 IMMDeviceEnumerator
*devenum
= NULL
;
839 if(g_outmmdevices_count
|| g_inmmdevices_count
)
842 init_hr
= CoInitialize(NULL
);
844 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
845 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
849 hr
= IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum
, &g_notif
);
851 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr
);
853 hr
= WINMM_EnumDevices(&g_out_mmdevices
, &g_out_map
, &g_outmmdevices_count
,
856 g_outmmdevices_count
= 0;
857 g_inmmdevices_count
= 0;
861 hr
= WINMM_EnumDevices(&g_in_mmdevices
, &g_in_map
, &g_inmmdevices_count
,
864 g_inmmdevices_count
= 0;
870 IMMDeviceEnumerator_Release(devenum
);
871 if(SUCCEEDED(init_hr
))
877 static inline BOOL
WINMM_IsMapper(UINT device
)
879 return (device
== WAVE_MAPPER
|| device
== (UINT16
)WAVE_MAPPER
);
882 static MMRESULT
WINMM_TryDeviceMapping(WINMM_Device
*device
, WAVEFORMATEX
*fmt
,
883 WORD channels
, DWORD freq
, DWORD bits_per_samp
, BOOL is_query
, BOOL is_out
)
885 WAVEFORMATEX target
, *closer_fmt
= NULL
;
889 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
890 WAVE_FORMAT_PCM
, channels
, freq
, bits_per_samp
);
892 target
.wFormatTag
= WAVE_FORMAT_PCM
;
893 target
.nChannels
= channels
;
894 target
.nSamplesPerSec
= freq
;
895 target
.wBitsPerSample
= bits_per_samp
;
896 target
.nBlockAlign
= (target
.nChannels
* target
.wBitsPerSample
) / 8;
897 target
.nAvgBytesPerSec
= target
.nSamplesPerSec
* target
.nBlockAlign
;
900 hr
= IAudioClient_IsFormatSupported(device
->client
,
901 AUDCLNT_SHAREMODE_SHARED
, &target
, &closer_fmt
);
902 CoTaskMemFree(closer_fmt
);
904 return WAVERR_BADFORMAT
;
906 /* device supports our target format, so see if MSACM can
907 * do the conversion */
909 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, fmt
, &target
, NULL
,
912 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, &target
, fmt
, NULL
,
914 if(mr
!= MMSYSERR_NOERROR
)
917 /* yes it can. initialize the audioclient and return success */
919 acmStreamClose(device
->acm_handle
, 0);
920 device
->acm_handle
= NULL
;
921 return MMSYSERR_NOERROR
;
924 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
925 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
926 AC_BUFLEN
, 0, &target
, &device
->parent
->session
);
928 WARN("Initialize failed: %08x\n", hr
);
929 acmStreamClose(device
->acm_handle
, 0);
930 device
->acm_handle
= NULL
;
931 return MMSYSERR_ERROR
;
934 device
->bytes_per_frame
= target
.nBlockAlign
;
935 device
->samples_per_sec
= target
.nSamplesPerSec
;
939 return MMSYSERR_NOERROR
;
942 static MMRESULT
WINMM_MapDevice(WINMM_Device
*device
, BOOL is_query
, BOOL is_out
)
945 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)device
->orig_fmt
;
947 TRACE("(%p, %u)\n", device
, is_out
);
949 /* set up the ACM stream */
950 if(device
->orig_fmt
->wFormatTag
!= WAVE_FORMAT_PCM
&&
951 !(device
->orig_fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
952 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
953 /* convert to PCM format if it's not already */
954 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
,
955 device
->orig_fmt
->nChannels
, device
->orig_fmt
->nSamplesPerSec
,
956 16, is_query
, is_out
);
957 if(mr
== MMSYSERR_NOERROR
)
960 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
,
961 device
->orig_fmt
->nChannels
, device
->orig_fmt
->nSamplesPerSec
,
962 8, is_query
, is_out
);
963 if(mr
== MMSYSERR_NOERROR
)
968 /* first try just changing bit depth and channels */
969 channels
= device
->orig_fmt
->nChannels
;
970 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
971 device
->orig_fmt
->nSamplesPerSec
, 16, is_query
, is_out
);
972 if(mr
== MMSYSERR_NOERROR
)
974 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
975 device
->orig_fmt
->nSamplesPerSec
, 8, is_query
, is_out
);
976 if(mr
== MMSYSERR_NOERROR
)
979 channels
= (channels
== 2) ? 1 : 2;
980 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
981 device
->orig_fmt
->nSamplesPerSec
, 16, is_query
, is_out
);
982 if(mr
== MMSYSERR_NOERROR
)
984 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
985 device
->orig_fmt
->nSamplesPerSec
, 8, is_query
, is_out
);
986 if(mr
== MMSYSERR_NOERROR
)
989 /* that didn't work, so now try different sample rates */
990 channels
= device
->orig_fmt
->nChannels
;
991 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 16, is_query
, is_out
);
992 if(mr
== MMSYSERR_NOERROR
)
994 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 16, is_query
, is_out
);
995 if(mr
== MMSYSERR_NOERROR
)
997 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 16, is_query
, is_out
);
998 if(mr
== MMSYSERR_NOERROR
)
1000 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 16, is_query
, is_out
);
1001 if(mr
== MMSYSERR_NOERROR
)
1003 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 16, is_query
, is_out
);
1004 if(mr
== MMSYSERR_NOERROR
)
1007 channels
= (channels
== 2) ? 1 : 2;
1008 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 16, is_query
, is_out
);
1009 if(mr
== MMSYSERR_NOERROR
)
1011 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 16, is_query
, is_out
);
1012 if(mr
== MMSYSERR_NOERROR
)
1014 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 16, is_query
, is_out
);
1015 if(mr
== MMSYSERR_NOERROR
)
1017 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 16, is_query
, is_out
);
1018 if(mr
== MMSYSERR_NOERROR
)
1020 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 16, is_query
, is_out
);
1021 if(mr
== MMSYSERR_NOERROR
)
1024 channels
= device
->orig_fmt
->nChannels
;
1025 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 8, is_query
, is_out
);
1026 if(mr
== MMSYSERR_NOERROR
)
1028 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 8, is_query
, is_out
);
1029 if(mr
== MMSYSERR_NOERROR
)
1031 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 8, is_query
, is_out
);
1032 if(mr
== MMSYSERR_NOERROR
)
1034 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 8, is_query
, is_out
);
1035 if(mr
== MMSYSERR_NOERROR
)
1037 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 8, is_query
, is_out
);
1038 if(mr
== MMSYSERR_NOERROR
)
1041 channels
= (channels
== 2) ? 1 : 2;
1042 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 8, is_query
, is_out
);
1043 if(mr
== MMSYSERR_NOERROR
)
1045 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 8, is_query
, is_out
);
1046 if(mr
== MMSYSERR_NOERROR
)
1048 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 8, is_query
, is_out
);
1049 if(mr
== MMSYSERR_NOERROR
)
1051 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 8, is_query
, is_out
);
1052 if(mr
== MMSYSERR_NOERROR
)
1054 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 8, is_query
, is_out
);
1055 if(mr
== MMSYSERR_NOERROR
)
1059 WARN("Unable to find compatible device!\n");
1060 return WAVERR_BADFORMAT
;
1063 static LRESULT
WINMM_OpenDevice(WINMM_Device
*device
, WINMM_OpenInfo
*info
,
1066 LRESULT ret
= MMSYSERR_NOMEM
;
1069 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, device
->parent
->dev_id
,
1072 WARN("Device %s (%s) unavailable: %08x\n",
1073 wine_dbgstr_w(device
->parent
->dev_id
),
1074 wine_dbgstr_w(device
->parent
->out_caps
.szPname
), hr
);
1075 ret
= MMSYSERR_NODRIVER
;
1079 /* this is where winexyz.drv opens the audio device */
1080 hr
= IMMDevice_Activate(device
->device
, &IID_IAudioClient
,
1081 CLSCTX_INPROC_SERVER
, NULL
, (void**)&device
->client
);
1083 WARN("Activate failed: %08x\n", hr
);
1085 if(ret
== MMSYSERR_ERROR
)
1086 ret
= MMSYSERR_NOTENABLED
;
1090 if(info
->format
->wFormatTag
== WAVE_FORMAT_PCM
){
1091 /* we aren't guaranteed that the struct in lpFormat is a full
1092 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1093 device
->orig_fmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX
));
1094 memcpy(device
->orig_fmt
, info
->format
, sizeof(PCMWAVEFORMAT
));
1095 device
->orig_fmt
->cbSize
= 0;
1096 if(device
->orig_fmt
->wBitsPerSample
% 8 != 0){
1097 WARN("Fixing bad wBitsPerSample (%u)\n", device
->orig_fmt
->wBitsPerSample
);
1098 device
->orig_fmt
->wBitsPerSample
= (device
->orig_fmt
->wBitsPerSample
+ 7) & ~7;
1100 /* winmm ignores broken blockalign and avgbytes */
1101 if(device
->orig_fmt
->nBlockAlign
!= device
->orig_fmt
->nChannels
* device
->orig_fmt
->wBitsPerSample
/8){
1102 WARN("Fixing bad nBlockAlign (%u)\n", device
->orig_fmt
->nBlockAlign
);
1103 device
->orig_fmt
->nBlockAlign
= device
->orig_fmt
->nChannels
* device
->orig_fmt
->wBitsPerSample
/8;
1105 if (device
->orig_fmt
->nAvgBytesPerSec
!= device
->orig_fmt
->nSamplesPerSec
* device
->orig_fmt
->nBlockAlign
) {
1106 WARN("Fixing bad nAvgBytesPerSec (%u)\n", device
->orig_fmt
->nAvgBytesPerSec
);
1107 device
->orig_fmt
->nAvgBytesPerSec
= device
->orig_fmt
->nSamplesPerSec
* device
->orig_fmt
->nBlockAlign
;
1110 device
->orig_fmt
= HeapAlloc(GetProcessHeap(), 0,
1111 sizeof(WAVEFORMATEX
) + info
->format
->cbSize
);
1112 memcpy(device
->orig_fmt
, info
->format
,
1113 sizeof(WAVEFORMATEX
) + info
->format
->cbSize
);
1116 if(info
->flags
& WAVE_FORMAT_QUERY
){
1117 WAVEFORMATEX
*closer_fmt
= NULL
;
1119 hr
= IAudioClient_IsFormatSupported(device
->client
,
1120 AUDCLNT_SHAREMODE_SHARED
, device
->orig_fmt
, &closer_fmt
);
1121 CoTaskMemFree(closer_fmt
);
1122 if((hr
== S_FALSE
|| hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
) && !(info
->flags
& WAVE_FORMAT_DIRECT
))
1123 ret
= WINMM_MapDevice(device
, TRUE
, is_out
);
1125 ret
= hr
== S_FALSE
? WAVERR_BADFORMAT
: hr2mmr(hr
);
1129 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
1130 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
1131 AC_BUFLEN
, 0, device
->orig_fmt
, &device
->parent
->session
);
1133 if(hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
&& !(info
->flags
& WAVE_FORMAT_DIRECT
)){
1134 ret
= WINMM_MapDevice(device
, FALSE
, is_out
);
1135 if(ret
!= MMSYSERR_NOERROR
|| info
->flags
& WAVE_FORMAT_QUERY
)
1138 WARN("Initialize failed: %08x\n", hr
);
1143 device
->bytes_per_frame
= device
->orig_fmt
->nBlockAlign
;
1144 device
->samples_per_sec
= device
->orig_fmt
->nSamplesPerSec
;
1147 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioClock
,
1148 (void**)&device
->clock
);
1150 WARN("GetService failed: %08x\n", hr
);
1155 device
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1157 WARN("CreateEvent failed: %08x\n", hr
);
1161 /* As the devices thread is waiting on g_device_handles, it can
1162 * only be modified from within this same thread. */
1163 if(g_device_handles
){
1164 g_device_handles
= HeapReAlloc(GetProcessHeap(), 0, g_device_handles
,
1165 sizeof(HANDLE
) * (g_devhandle_count
+ 1));
1166 g_handle_devices
= HeapReAlloc(GetProcessHeap(), 0, g_handle_devices
,
1167 sizeof(WINMM_Device
*) * (g_devhandle_count
+ 1));
1169 g_device_handles
= HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE
));
1170 g_handle_devices
= HeapAlloc(GetProcessHeap(), 0,
1171 sizeof(WINMM_Device
*));
1173 g_device_handles
[g_devhandle_count
] = device
->event
;
1174 g_handle_devices
[g_devhandle_count
] = device
;
1175 ++g_devhandle_count
;
1178 hr
= IAudioClient_SetEventHandle(device
->client
, device
->event
);
1180 WARN("SetEventHandle failed: %08x\n", hr
);
1185 device
->played_frames
= 0;
1186 device
->ofs_bytes
= 0;
1187 device
->loop_counter
= 0;
1188 device
->first
= device
->last
= device
->playing
= device
->loop_start
= NULL
;
1191 device
->stopped
= TRUE
;
1192 device
->last_clock_pos
= 0;
1194 device
->cb_info
.flags
= HIWORD(info
->flags
& CALLBACK_TYPEMASK
);
1195 device
->cb_info
.callback
= info
->callback
;
1196 device
->cb_info
.user
= info
->cb_user
;
1197 device
->cb_info
.hwave
= device
->handle
;
1199 info
->handle
= device
->handle
;
1201 return MMSYSERR_NOERROR
;
1205 IAudioClient_Release(device
->client
);
1206 device
->client
= NULL
;
1209 IMMDevice_Release(device
->device
);
1210 device
->device
= NULL
;
1216 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
)
1218 WINMM_Device
*device
;
1219 LRESULT ret
= MMSYSERR_ERROR
;
1222 if(info
->handle
!= 0){
1223 device
= WINMM_GetDeviceFromHWAVE(info
->handle
);
1225 WARN("Unexpected! Invalid info->handle given: %p\n", info
->handle
);
1226 return MMSYSERR_ERROR
;
1229 EnterCriticalSection(&device
->lock
);
1231 device
->open
= TRUE
;
1233 CRITICAL_SECTION
*lock
;
1234 UINT internal_index
;
1235 WINMM_Device
**devices
;
1236 WINMM_MMDevice
*mmdevice
;
1238 if(WINMM_IsMapper(info
->req_device
)){
1239 if (g_outmmdevices_count
== 0)
1240 return MMSYSERR_BADDEVICEID
;
1241 devices
= g_out_mapper_devices
;
1242 mmdevice
= read_map(g_out_map
, 0);
1243 lock
= &g_devthread_lock
;
1244 internal_index
= MAPPER_INDEX
;
1246 if(info
->req_device
>= g_outmmdevices_count
)
1247 return MMSYSERR_BADDEVICEID
;
1249 mmdevice
= read_map(g_out_map
, info
->req_device
);
1251 if(!mmdevice
->out_caps
.szPname
[0])
1252 return MMSYSERR_NOTENABLED
;
1254 devices
= mmdevice
->devices
;
1255 lock
= &mmdevice
->lock
;
1256 internal_index
= mmdevice
->index
;
1259 EnterCriticalSection(lock
);
1261 device
= WINMM_FindUnusedDevice(devices
, mmdevice
,
1262 internal_index
, TRUE
);
1264 LeaveCriticalSection(lock
);
1265 return MMSYSERR_ALLOCATED
;
1268 LeaveCriticalSection(lock
);
1271 ret
= WINMM_OpenDevice(device
, info
, TRUE
);
1272 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1274 ret
= MMSYSERR_ERROR
;
1276 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioRenderClient
,
1277 (void**)&device
->render
);
1279 ERR("GetService failed: %08x\n", hr
);
1283 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioStreamVolume
,
1284 (void**)&device
->volume
);
1286 ERR("GetService failed: %08x\n", hr
);
1290 LeaveCriticalSection(&device
->lock
);
1292 return MMSYSERR_NOERROR
;
1296 IMMDevice_Release(device
->device
);
1297 device
->device
= NULL
;
1300 IAudioClient_Release(device
->client
);
1301 device
->client
= NULL
;
1304 IAudioRenderClient_Release(device
->render
);
1305 device
->render
= NULL
;
1308 IAudioStreamVolume_Release(device
->volume
);
1309 device
->volume
= NULL
;
1312 IAudioClock_Release(device
->clock
);
1313 device
->clock
= NULL
;
1315 device
->open
= FALSE
;
1316 LeaveCriticalSection(&device
->lock
);
1320 static LRESULT
WID_Open(WINMM_OpenInfo
*info
)
1322 WINMM_Device
*device
, **devices
;
1323 WINMM_MMDevice
*mmdevice
;
1324 UINT internal_index
;
1325 CRITICAL_SECTION
*lock
;
1326 LRESULT ret
= MMSYSERR_ERROR
;
1329 if(WINMM_IsMapper(info
->req_device
)){
1330 if (g_inmmdevices_count
== 0)
1331 return MMSYSERR_BADDEVICEID
;
1332 devices
= g_in_mapper_devices
;
1333 mmdevice
= read_map(g_in_map
, 0);
1334 lock
= &g_devthread_lock
;
1335 internal_index
= MAPPER_INDEX
;
1337 if(info
->req_device
>= g_inmmdevices_count
)
1338 return MMSYSERR_BADDEVICEID
;
1340 mmdevice
= read_map(g_in_map
, info
->req_device
);
1342 if(!mmdevice
->in_caps
.szPname
[0])
1343 return MMSYSERR_NOTENABLED
;
1345 devices
= mmdevice
->devices
;
1346 lock
= &mmdevice
->lock
;
1347 internal_index
= mmdevice
->index
;
1350 EnterCriticalSection(lock
);
1352 device
= WINMM_FindUnusedDevice(devices
, mmdevice
, internal_index
, FALSE
);
1354 LeaveCriticalSection(lock
);
1355 return MMSYSERR_ALLOCATED
;
1358 LeaveCriticalSection(lock
);
1360 ret
= WINMM_OpenDevice(device
, info
, FALSE
);
1361 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1363 ret
= MMSYSERR_ERROR
;
1365 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioCaptureClient
,
1366 (void**)&device
->capture
);
1368 WARN("GetService failed: %08x\n", hr
);
1372 LeaveCriticalSection(&device
->lock
);
1374 return MMSYSERR_NOERROR
;
1378 IMMDevice_Release(device
->device
);
1379 device
->device
= NULL
;
1382 IAudioClient_Release(device
->client
);
1383 device
->client
= NULL
;
1385 if(device
->capture
){
1386 IAudioCaptureClient_Release(device
->capture
);
1387 device
->capture
= NULL
;
1390 IAudioClock_Release(device
->clock
);
1391 device
->clock
= NULL
;
1393 device
->open
= FALSE
;
1394 LeaveCriticalSection(&device
->lock
);
1398 static HRESULT
WINMM_CloseDevice(WINMM_Device
*device
)
1400 device
->open
= FALSE
;
1402 if(!device
->stopped
){
1403 IAudioClient_Stop(device
->client
);
1404 device
->stopped
= TRUE
;
1407 if(device
->acm_handle
){
1408 acmStreamClose(device
->acm_handle
, 0);
1409 device
->acm_handle
= NULL
;
1412 IMMDevice_Release(device
->device
);
1413 device
->device
= NULL
;
1415 IAudioClient_Release(device
->client
);
1416 device
->client
= NULL
;
1418 IAudioClock_Release(device
->clock
);
1419 device
->clock
= NULL
;
1424 static LRESULT
WOD_Close(HWAVEOUT hwave
)
1426 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1428 TRACE("(%p)\n", hwave
);
1430 if(!WINMM_ValidateAndLock(device
))
1431 return MMSYSERR_INVALHANDLE
;
1433 WINMM_CloseDevice(device
);
1435 IAudioRenderClient_Release(device
->render
);
1436 device
->render
= NULL
;
1438 IAudioStreamVolume_Release(device
->volume
);
1439 device
->volume
= NULL
;
1441 LeaveCriticalSection(&device
->lock
);
1443 return MMSYSERR_NOERROR
;
1446 static LRESULT
WID_Close(HWAVEIN hwave
)
1448 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1450 TRACE("(%p)\n", hwave
);
1452 if(!WINMM_ValidateAndLock(device
))
1453 return MMSYSERR_INVALHANDLE
;
1455 WINMM_CloseDevice(device
);
1457 IAudioCaptureClient_Release(device
->capture
);
1458 device
->capture
= NULL
;
1460 LeaveCriticalSection(&device
->lock
);
1462 return MMSYSERR_NOERROR
;
1465 static DWORD
WINMM_FixedBufferLen(DWORD length
, WINMM_Device
*device
)
1467 return length
- length
% device
->bytes_per_frame
;
1470 static LRESULT
WINMM_PrepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1472 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1474 TRACE("(%p, %p)\n", hwave
, header
);
1476 if(!WINMM_ValidateAndLock(device
))
1477 return MMSYSERR_INVALHANDLE
;
1479 if(device
->render
&& device
->acm_handle
){
1480 ACMSTREAMHEADER
*ash
;
1484 mr
= acmStreamSize(device
->acm_handle
, header
->dwBufferLength
, &size
,
1485 ACM_STREAMSIZEF_SOURCE
);
1486 if(mr
!= MMSYSERR_NOERROR
){
1487 LeaveCriticalSection(&device
->lock
);
1491 ash
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER
) + size
);
1493 LeaveCriticalSection(&device
->lock
);
1494 return MMSYSERR_NOMEM
;
1497 ash
->cbStruct
= sizeof(*ash
);
1499 ash
->dwUser
= (DWORD_PTR
)header
;
1500 ash
->pbSrc
= (BYTE
*)header
->lpData
;
1501 ash
->cbSrcLength
= header
->dwBufferLength
;
1502 ash
->dwSrcUser
= header
->dwUser
;
1503 ash
->pbDst
= (BYTE
*)ash
+ sizeof(ACMSTREAMHEADER
);
1504 ash
->cbDstLength
= size
;
1507 mr
= acmStreamPrepareHeader(device
->acm_handle
, ash
, 0);
1508 if(mr
!= MMSYSERR_NOERROR
){
1509 HeapFree(GetProcessHeap(), 0, ash
);
1510 LeaveCriticalSection(&device
->lock
);
1514 header
->reserved
= (DWORD_PTR
)ash
;
1517 LeaveCriticalSection(&device
->lock
);
1519 header
->dwFlags
|= WHDR_PREPARED
;
1520 header
->dwFlags
&= ~(WHDR_DONE
|WHDR_INQUEUE
); /* flags cleared since w2k */
1522 return MMSYSERR_NOERROR
;
1525 static LRESULT
WINMM_UnprepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1527 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1529 TRACE("(%p, %p)\n", hwave
, header
);
1531 if(!WINMM_ValidateAndLock(device
))
1532 return MMSYSERR_INVALHANDLE
;
1534 if(device
->render
&& device
->acm_handle
){
1535 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1537 acmStreamUnprepareHeader(device
->acm_handle
, ash
, 0);
1539 HeapFree(GetProcessHeap(), 0, ash
);
1542 LeaveCriticalSection(&device
->lock
);
1544 header
->dwFlags
&= ~WHDR_PREPARED
;
1546 return MMSYSERR_NOERROR
;
1549 static UINT32
WINMM_HeaderLenBytes(WINMM_Device
*device
, WAVEHDR
*header
)
1551 if(device
->acm_handle
){
1552 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1553 return WINMM_FixedBufferLen(ash
->cbDstLengthUsed
, device
);
1556 return WINMM_FixedBufferLen(header
->dwBufferLength
, device
);
1559 static UINT32
WINMM_HeaderLenFrames(WINMM_Device
*device
, WAVEHDR
*header
)
1561 return WINMM_HeaderLenBytes(device
, header
) / device
->bytes_per_frame
;
1564 static WAVEHDR
*WOD_MarkDoneHeaders(WINMM_Device
*device
)
1567 WAVEHDR
*first
= device
->first
, *queue
= first
, *last
= NULL
;
1568 UINT64 clock_freq
, clock_pos
, clock_frames
;
1569 UINT32 nloops
, queue_frames
= 0;
1571 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
1573 WARN("GetFrequency failed: %08x\n", hr
);
1577 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
1579 WARN("GetPosition failed: %08x\n", hr
);
1583 clock_frames
= (clock_pos
* device
->samples_per_sec
) / clock_freq
;
1585 nloops
= device
->loop_counter
;
1587 (queue_frames
+= WINMM_HeaderLenFrames(device
, queue
)) <=
1588 clock_frames
- device
->last_clock_pos
+ device
->remainder_frames
){
1591 device
->last_clock_pos
+= queue_frames
;
1592 device
->remainder_frames
= 0;
1594 queue
= device
->first
= queue
->lpNext
;
1596 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1597 if(queue
->dwFlags
& WHDR_ENDLOOP
)
1600 queue
= queue
->lpNext
;
1601 }else if(queue
->dwFlags
& WHDR_ENDLOOP
){
1602 queue
= device
->loop_start
;
1609 last
->lpNext
= NULL
;
1615 static void WOD_PushData(WINMM_Device
*device
)
1617 WINMM_CBInfo cb_info
;
1619 UINT32 pad
, bufsize
, avail_frames
, queue_frames
, written
, ofs
;
1620 UINT32 queue_bytes
, nloops
;
1622 WAVEHDR
*queue
, *first
= NULL
;
1624 TRACE("(%p)\n", device
->handle
);
1626 EnterCriticalSection(&device
->lock
);
1632 if (device
->stopped
)
1634 device
->stopped
= TRUE
;
1635 device
->last_clock_pos
= 0;
1636 IAudioClient_Stop(device
->client
);
1637 IAudioClient_Reset(device
->client
);
1641 hr
= IAudioClient_GetBufferSize(device
->client
, &bufsize
);
1643 WARN("GetBufferSize failed: %08x\n", hr
);
1647 hr
= IAudioClient_GetCurrentPadding(device
->client
, &pad
);
1649 WARN("GetCurrentPadding failed: %08x\n", hr
);
1653 first
= WOD_MarkDoneHeaders(device
);
1655 /* determine which is larger between the available buffer size and
1656 * the amount of data left in the queue */
1657 avail_frames
= bufsize
- pad
;
1659 queue
= device
->playing
;
1660 ofs
= device
->ofs_bytes
;
1663 while(queue
&& queue_frames
< avail_frames
){
1664 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1665 queue_frames
+= (queue_bytes
- ofs
) / device
->bytes_per_frame
;
1668 if(queue
->dwFlags
& WHDR_ENDLOOP
&& nloops
< device
->loop_counter
){
1669 queue
= device
->loop_start
;
1672 queue
= queue
->lpNext
;
1675 if(queue_frames
< avail_frames
)
1676 avail_frames
= queue_frames
;
1677 if(avail_frames
== 0)
1680 hr
= IAudioRenderClient_GetBuffer(device
->render
, avail_frames
, &data
);
1682 WARN("GetBuffer failed: %08x\n", hr
);
1687 while(device
->playing
&& written
< avail_frames
){
1688 UINT32 copy_frames
, copy_bytes
;
1691 queue
= device
->playing
;
1693 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1694 if(device
->acm_handle
)
1695 queue_data
= ((ACMSTREAMHEADER
*)queue
->reserved
)->pbDst
;
1697 queue_data
= (BYTE
*)queue
->lpData
;
1699 queue_frames
= (queue_bytes
- device
->ofs_bytes
) /
1700 device
->bytes_per_frame
;
1702 copy_frames
= queue_frames
< (avail_frames
- written
) ?
1703 queue_frames
: avail_frames
- written
;
1704 copy_bytes
= copy_frames
* device
->bytes_per_frame
;
1706 memcpy(data
, queue_data
+ device
->ofs_bytes
, copy_bytes
);
1709 written
+= copy_frames
;
1710 device
->ofs_bytes
+= copy_bytes
;
1712 if(device
->ofs_bytes
>= queue_bytes
){
1713 device
->ofs_bytes
= 0;
1715 if(!(queue
->dwFlags
& (WHDR_BEGINLOOP
| WHDR_ENDLOOP
)))
1716 device
->playing
= queue
->lpNext
;
1718 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1719 device
->loop_start
= device
->playing
;
1720 device
->playing
= queue
->lpNext
;
1721 device
->loop_counter
= queue
->dwLoops
;
1723 if(queue
->dwFlags
& WHDR_ENDLOOP
){
1724 --device
->loop_counter
;
1725 if(device
->loop_counter
)
1726 device
->playing
= device
->loop_start
;
1728 device
->loop_start
= device
->playing
= queue
->lpNext
;
1734 hr
= IAudioRenderClient_ReleaseBuffer(device
->render
, avail_frames
, 0);
1736 WARN("ReleaseBuffer failed: %08x\n", hr
);
1740 if(device
->orig_fmt
->nSamplesPerSec
!= device
->samples_per_sec
)
1741 device
->played_frames
+= MulDiv(avail_frames
, device
->orig_fmt
->nSamplesPerSec
, device
->samples_per_sec
);
1743 device
->played_frames
+= avail_frames
;
1746 cb_info
= device
->cb_info
;
1748 LeaveCriticalSection(&device
->lock
);
1751 WAVEHDR
*next
= first
->lpNext
;
1752 first
->dwFlags
&= ~WHDR_INQUEUE
;
1753 first
->dwFlags
|= WHDR_DONE
;
1754 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
1759 static void WID_PullACMData(WINMM_Device
*device
)
1761 UINT32 packet
, packet_bytes
;
1768 if(device
->acm_hdr
.cbDstLength
== 0){
1769 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet
,
1770 &flags
, NULL
, NULL
);
1773 WARN("GetBuffer failed: %08x\n", hr
);
1777 acmStreamSize(device
->acm_handle
, packet
* device
->bytes_per_frame
,
1778 &packet_bytes
, ACM_STREAMSIZEF_SOURCE
);
1780 device
->acm_offs
= 0;
1782 device
->acm_hdr
.cbStruct
= sizeof(device
->acm_hdr
);
1783 device
->acm_hdr
.fdwStatus
= 0;
1784 device
->acm_hdr
.dwUser
= 0;
1785 device
->acm_hdr
.pbSrc
= data
;
1786 device
->acm_hdr
.cbSrcLength
= packet
* device
->bytes_per_frame
;
1787 device
->acm_hdr
.cbSrcLengthUsed
= 0;
1788 device
->acm_hdr
.dwSrcUser
= 0;
1789 device
->acm_hdr
.pbDst
= HeapAlloc(GetProcessHeap(), 0, packet_bytes
);
1790 device
->acm_hdr
.cbDstLength
= packet_bytes
;
1791 device
->acm_hdr
.cbDstLengthUsed
= 0;
1792 device
->acm_hdr
.dwDstUser
= 0;
1794 mr
= acmStreamPrepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1795 if(mr
!= MMSYSERR_NOERROR
){
1796 WARN("acmStreamPrepareHeader failed: %d\n", mr
);
1800 mr
= acmStreamConvert(device
->acm_handle
, &device
->acm_hdr
, 0);
1801 if(mr
!= MMSYSERR_NOERROR
){
1802 WARN("acmStreamConvert failed: %d\n", mr
);
1806 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet
);
1808 WARN("ReleaseBuffer failed: %08x\n", hr
);
1810 device
->played_frames
+= packet
;
1813 queue
= device
->first
;
1815 UINT32 to_copy_bytes
;
1817 to_copy_bytes
= min(WINMM_FixedBufferLen(queue
->dwBufferLength
, device
) - queue
->dwBytesRecorded
,
1818 WINMM_FixedBufferLen(device
->acm_hdr
.cbDstLengthUsed
, device
) - device
->acm_offs
);
1820 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1821 device
->acm_hdr
.pbDst
+ device
->acm_offs
, to_copy_bytes
);
1823 queue
->dwBytesRecorded
+= to_copy_bytes
;
1824 device
->acm_offs
+= to_copy_bytes
;
1826 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1827 device
->bytes_per_frame
){
1828 queue
->dwFlags
&= ~WHDR_INQUEUE
;
1829 queue
->dwFlags
|= WHDR_DONE
;
1830 device
->first
= queue
= queue
->lpNext
;
1833 if(device
->acm_offs
>= WINMM_FixedBufferLen(device
->acm_hdr
.cbDstLengthUsed
, device
)){
1834 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1835 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1836 device
->acm_hdr
.cbDstLength
= 0;
1837 device
->acm_hdr
.cbDstLengthUsed
= 0;
1839 /* done with this ACM Header, so try to pull more data */
1840 WID_PullACMData(device
);
1845 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1846 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1847 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1848 device
->acm_hdr
.cbDstLength
= 0;
1849 device
->acm_hdr
.cbDstLengthUsed
= 0;
1852 static void WID_PullData(WINMM_Device
*device
)
1854 WINMM_CBInfo cb_info
;
1855 WAVEHDR
*queue
, *first
= NULL
, *last
= NULL
;
1858 TRACE("(%p)\n", device
->handle
);
1860 EnterCriticalSection(&device
->lock
);
1862 if(!device
->device
|| !device
->first
)
1865 first
= device
->first
;
1867 if(device
->acm_handle
){
1868 WID_PullACMData(device
);
1872 while(device
->first
){
1874 UINT32 packet_len
, packet
;
1877 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet_len
,
1878 &flags
, NULL
, NULL
);
1881 WARN("GetBuffer failed: %08x\n", hr
);
1882 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1883 IAudioCaptureClient_ReleaseBuffer(device
->capture
, 0);
1887 packet
= packet_len
;
1888 queue
= device
->first
;
1889 while(queue
&& packet
> 0){
1890 UINT32 to_copy_bytes
;
1892 to_copy_bytes
= min(packet
* device
->bytes_per_frame
,
1893 WINMM_FixedBufferLen(queue
->dwBufferLength
, device
) - queue
->dwBytesRecorded
);
1895 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1896 data
+ (packet_len
- packet
) * device
->bytes_per_frame
,
1899 queue
->dwBytesRecorded
+= to_copy_bytes
;
1901 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1902 device
->bytes_per_frame
){
1904 device
->first
= queue
= queue
->lpNext
;
1907 packet
-= to_copy_bytes
/ device
->bytes_per_frame
;
1910 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet_len
);
1912 WARN("ReleaseBuffer failed: %08x\n", hr
);
1915 WARN("losing %u frames\n", packet
);
1916 device
->played_frames
+= packet_len
- packet
;
1920 cb_info
= device
->cb_info
;
1922 LeaveCriticalSection(&device
->lock
);
1925 last
->lpNext
= NULL
;
1927 WAVEHDR
*next
= first
->lpNext
;
1928 first
->dwFlags
&= ~WHDR_INQUEUE
;
1929 first
->dwFlags
|= WHDR_DONE
;
1930 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
1936 static MMRESULT
WINMM_BeginPlaying(WINMM_Device
*device
)
1940 TRACE("(%p)\n", device
->handle
);
1943 /* prebuffer data before starting */
1944 WOD_PushData(device
);
1946 if(device
->stopped
){
1947 device
->stopped
= FALSE
;
1949 hr
= IAudioClient_Start(device
->client
);
1950 if(FAILED(hr
) && hr
!= AUDCLNT_E_NOT_STOPPED
){
1951 device
->stopped
= TRUE
;
1952 WARN("Start failed: %08x\n", hr
);
1953 return MMSYSERR_ERROR
;
1957 return MMSYSERR_NOERROR
;
1960 static LRESULT
WINMM_Pause(HWAVE hwave
)
1962 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1965 TRACE("(%p)\n", hwave
);
1967 if(!WINMM_ValidateAndLock(device
))
1968 return MMSYSERR_INVALHANDLE
;
1970 hr
= IAudioClient_Stop(device
->client
);
1972 LeaveCriticalSection(&device
->lock
);
1973 WARN("Stop failed: %08x\n", hr
);
1974 return MMSYSERR_ERROR
;
1977 device
->stopped
= FALSE
;
1979 LeaveCriticalSection(&device
->lock
);
1981 return MMSYSERR_NOERROR
;
1984 static LRESULT
WINMM_Reset(HWAVE hwave
)
1986 WINMM_CBInfo cb_info
;
1987 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1992 TRACE("(%p)\n", hwave
);
1994 if(!WINMM_ValidateAndLock(device
))
1995 return MMSYSERR_INVALHANDLE
;
1997 hr
= IAudioClient_Stop(device
->client
);
1999 LeaveCriticalSection(&device
->lock
);
2000 WARN("Stop failed: %08x\n", hr
);
2001 return MMSYSERR_ERROR
;
2003 device
->stopped
= TRUE
;
2005 first
= device
->first
;
2006 device
->first
= device
->last
= device
->playing
= NULL
;
2007 device
->ofs_bytes
= 0;
2008 device
->played_frames
= 0;
2009 device
->loop_counter
= 0;
2010 device
->last_clock_pos
= 0;
2011 IAudioClient_Reset(device
->client
);
2013 cb_info
= device
->cb_info
;
2014 is_out
= device
->render
!= NULL
;
2016 LeaveCriticalSection(&device
->lock
);
2019 WAVEHDR
*next
= first
->lpNext
;
2020 first
->dwFlags
&= ~WHDR_INQUEUE
;
2021 first
->dwFlags
|= WHDR_DONE
;
2023 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
2025 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
2029 return MMSYSERR_NOERROR
;
2032 static MMRESULT
WINMM_FramesToMMTime(MMTIME
*time
, UINT32 played_frames
,
2033 UINT32 sample_rate
, UINT32 bytes_per_sec
)
2035 switch(time
->wType
){
2037 time
->u
.sample
= played_frames
;
2038 return MMSYSERR_NOERROR
;
2040 time
->u
.ms
= (UINT64
)played_frames
* 1000 / sample_rate
;
2041 return MMSYSERR_NOERROR
;
2043 time
->u
.smpte
.fps
= 30;
2044 played_frames
+= sample_rate
/ time
->u
.smpte
.fps
- 1; /* round up */
2045 time
->u
.smpte
.frame
= (played_frames
% sample_rate
) * time
->u
.smpte
.fps
/ sample_rate
;
2046 played_frames
/= sample_rate
; /* yields seconds */
2047 time
->u
.smpte
.sec
= played_frames
% 60;
2048 played_frames
/= 60;
2049 time
->u
.smpte
.min
= played_frames
% 60;
2050 time
->u
.smpte
.hour
= played_frames
/ 60;
2051 return MMSYSERR_NOERROR
;
2053 time
->wType
= TIME_BYTES
;
2056 time
->u
.cb
= MulDiv(played_frames
, bytes_per_sec
, sample_rate
);
2057 return MMSYSERR_NOERROR
;
2061 static LRESULT
WINMM_GetPosition(HWAVE hwave
, MMTIME
*time
)
2063 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
2064 UINT32 played_frames
, sample_rate
, bytes_per_sec
;
2066 TRACE("(%p, %p)\n", hwave
, time
);
2068 if(!WINMM_ValidateAndLock(device
))
2069 return MMSYSERR_INVALHANDLE
;
2071 played_frames
= device
->played_frames
;
2072 sample_rate
= device
->orig_fmt
->nSamplesPerSec
;
2073 bytes_per_sec
= device
->orig_fmt
->nAvgBytesPerSec
;
2075 LeaveCriticalSection(&device
->lock
);
2077 return WINMM_FramesToMMTime(time
, played_frames
, sample_rate
, bytes_per_sec
);
2080 static WINMM_MMDevice
*WINMM_GetMixerMMDevice(HMIXEROBJ hmix
, DWORD flags
,
2083 UINT mmdev
, dev
, junk
, *out
;
2091 switch(flags
& 0xF0000000){
2092 case MIXER_OBJECTF_MIXER
: /* == 0 */
2093 *out
= HandleToULong(hmix
);
2094 if(*out
< g_outmmdevices_count
)
2095 return read_map(g_out_map
, *out
);
2096 if(*out
- g_outmmdevices_count
< g_inmmdevices_count
){
2097 *out
-= g_outmmdevices_count
;
2098 return read_map(g_in_map
, *out
);
2100 /* fall through -- if it's not a valid mixer device, then
2101 * it could be a valid mixer handle. windows seems to do
2103 case MIXER_OBJECTF_HMIXER
:
2104 case MIXER_OBJECTF_HWAVEOUT
:
2105 case MIXER_OBJECTF_HWAVEIN
:
2106 WINMM_DecomposeHWAVE((HWAVE
)hmix
, out
, &is_out
, &dev
, &junk
);
2107 if(junk
!= 0x1 || (is_out
&& *out
>= g_outmmdevices_count
) ||
2108 (!is_out
&& *out
>= g_inmmdevices_count
))
2111 return read_map(g_out_map
, *out
);
2112 return read_map(g_in_map
, *out
);
2113 case MIXER_OBJECTF_WAVEOUT
:
2114 *out
= HandleToULong(hmix
);
2115 if(*out
< g_outmmdevices_count
)
2116 return read_map(g_out_map
, *out
);
2118 case MIXER_OBJECTF_WAVEIN
:
2119 *out
= HandleToULong(hmix
);
2120 if(*out
< g_inmmdevices_count
)
2121 return read_map(g_in_map
, *out
);
2128 static MMRESULT
WINMM_SetupMMDeviceVolume(WINMM_MMDevice
*mmdevice
)
2130 IAudioSessionManager
*sesman
;
2134 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
, &device
);
2136 WARN("Device %s (%s) unavailable: %08x\n",
2137 wine_dbgstr_w(mmdevice
->dev_id
),
2138 wine_dbgstr_w(mmdevice
->out_caps
.szPname
), hr
);
2139 return MMSYSERR_ERROR
;
2142 hr
= IMMDevice_Activate(device
, &IID_IAudioSessionManager
,
2143 CLSCTX_INPROC_SERVER
, NULL
, (void**)&sesman
);
2145 WARN("Activate failed: %08x\n", hr
);
2146 IMMDevice_Release(device
);
2147 return MMSYSERR_ERROR
;
2150 IMMDevice_Release(device
);
2152 hr
= IAudioSessionManager_GetSimpleAudioVolume(sesman
, &mmdevice
->session
,
2153 FALSE
, &mmdevice
->volume
);
2154 IAudioSessionManager_Release(sesman
);
2156 WARN("GetSimpleAudioVolume failed: %08x\n", hr
);
2157 return MMSYSERR_ERROR
;
2160 return MMSYSERR_NOERROR
;
2163 static LRESULT
MXD_GetControlDetails(WINMM_ControlDetails
*details
)
2165 WINMM_MMDevice
*mmdevice
;
2166 MIXERCONTROLDETAILS
*control
= details
->details
;
2169 TRACE("(%p)\n", details
->hmix
);
2171 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
2173 return MMSYSERR_INVALHANDLE
;
2175 EnterCriticalSection(&mmdevice
->lock
);
2177 if(!mmdevice
->volume
){
2180 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
2181 if(mr
!= MMSYSERR_NOERROR
){
2182 LeaveCriticalSection(&mmdevice
->lock
);
2187 if(control
->dwControlID
== 0){
2189 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
2191 if(!control
->paDetails
||
2192 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
2193 LeaveCriticalSection(&mmdevice
->lock
);
2194 return MMSYSERR_INVALPARAM
;
2197 hr
= ISimpleAudioVolume_GetMasterVolume(mmdevice
->volume
, &vol
);
2199 WARN("GetMasterVolume failed: %08x\n", hr
);
2200 LeaveCriticalSection(&mmdevice
->lock
);
2201 return MMSYSERR_ERROR
;
2204 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
2205 udet
->dwValue
= vol
* ((unsigned int)0xFFFF);
2206 }else if(control
->dwControlID
== 1){
2208 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
2210 if(!control
->paDetails
||
2211 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
2212 LeaveCriticalSection(&mmdevice
->lock
);
2213 return MMSYSERR_INVALPARAM
;
2216 hr
= ISimpleAudioVolume_GetMute(mmdevice
->volume
, &mute
);
2218 WARN("GetMute failed: %08x\n", hr
);
2219 LeaveCriticalSection(&mmdevice
->lock
);
2220 return MMSYSERR_ERROR
;
2223 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
2224 bdet
->fValue
= mute
;
2225 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2226 FIXME("What should the sw-side mixer controls map to?\n");
2228 LeaveCriticalSection(&mmdevice
->lock
);
2229 return MIXERR_INVALCONTROL
;
2232 LeaveCriticalSection(&mmdevice
->lock
);
2234 return MMSYSERR_NOERROR
;
2237 static LRESULT
MXD_SetControlDetails(WINMM_ControlDetails
*details
)
2239 WINMM_MMDevice
*mmdevice
;
2240 MIXERCONTROLDETAILS
*control
= details
->details
;
2243 TRACE("(%p)\n", details
->hmix
);
2245 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
2247 return MMSYSERR_INVALHANDLE
;
2249 EnterCriticalSection(&mmdevice
->lock
);
2251 if(!mmdevice
->volume
){
2254 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
2255 if(mr
!= MMSYSERR_NOERROR
){
2256 LeaveCriticalSection(&mmdevice
->lock
);
2261 if(control
->dwControlID
== 0){
2263 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
2265 if(!control
->paDetails
||
2266 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
2267 LeaveCriticalSection(&mmdevice
->lock
);
2268 return MMSYSERR_INVALPARAM
;
2271 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
2273 if(udet
->dwValue
> 65535){
2274 LeaveCriticalSection(&mmdevice
->lock
);
2275 return MMSYSERR_INVALPARAM
;
2278 vol
= udet
->dwValue
/ 65535.f
;
2280 hr
= ISimpleAudioVolume_SetMasterVolume(mmdevice
->volume
, vol
, NULL
);
2282 WARN("SetMasterVolume failed: %08x\n", hr
);
2283 LeaveCriticalSection(&mmdevice
->lock
);
2284 return MMSYSERR_ERROR
;
2286 }else if(control
->dwControlID
== 1){
2288 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
2290 if(!control
->paDetails
||
2291 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
2292 LeaveCriticalSection(&mmdevice
->lock
);
2293 return MMSYSERR_INVALPARAM
;
2296 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
2297 mute
= bdet
->fValue
;
2299 hr
= ISimpleAudioVolume_SetMute(mmdevice
->volume
, mute
, NULL
);
2301 WARN("SetMute failed: %08x\n", hr
);
2302 LeaveCriticalSection(&mmdevice
->lock
);
2303 return MMSYSERR_ERROR
;
2305 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2306 FIXME("What should the sw-side mixer controls map to?\n");
2308 LeaveCriticalSection(&mmdevice
->lock
);
2309 return MIXERR_INVALCONTROL
;
2312 LeaveCriticalSection(&mmdevice
->lock
);
2314 return MMSYSERR_NOERROR
;
2317 static LRESULT
DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo
*info
)
2319 WINMM_MMDevice
*mmdevice
;
2326 static const PROPERTYKEY deviceinterface_key
= {
2327 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2330 if(WINMM_IsMapper(info
->index
)){
2332 if(*info
->len_bytes
< sizeof(WCHAR
))
2333 return MMSYSERR_INVALPARAM
;
2336 *info
->len_bytes
= sizeof(WCHAR
);
2337 return MMSYSERR_NOERROR
;
2341 if(info
->index
>= g_outmmdevices_count
)
2342 return MMSYSERR_INVALHANDLE
;
2344 mmdevice
= &g_out_mmdevices
[info
->index
];
2346 if(info
->index
>= g_inmmdevices_count
)
2347 return MMSYSERR_INVALHANDLE
;
2349 mmdevice
= &g_in_mmdevices
[info
->index
];
2352 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
,
2355 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice
->dev_id
), hr
);
2356 return MMSYSERR_ERROR
;
2359 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
2361 WARN("OpenPropertyStore failed: %08x\n", hr
);
2362 IMMDevice_Release(device
);
2363 return MMSYSERR_ERROR
;
2366 PropVariantInit(&pv
);
2367 hr
= IPropertyStore_GetValue(ps
, &deviceinterface_key
, &pv
);
2369 WARN("GetValue failed: %08x\n", hr
);
2370 IPropertyStore_Release(ps
);
2371 IMMDevice_Release(device
);
2372 return MMSYSERR_ERROR
;
2374 if(pv
.vt
!= VT_LPWSTR
){
2375 WARN("Got unexpected property type: %u\n", pv
.vt
);
2376 PropVariantClear(&pv
);
2377 IPropertyStore_Release(ps
);
2378 IMMDevice_Release(device
);
2379 return MMSYSERR_ERROR
;
2382 len_bytes
= (lstrlenW(pv
.u
.pwszVal
) + 1) * sizeof(WCHAR
);
2385 if(len_bytes
> *info
->len_bytes
){
2386 PropVariantClear(&pv
);
2387 IPropertyStore_Release(ps
);
2388 IMMDevice_Release(device
);
2389 return MMSYSERR_INVALPARAM
;
2392 memcpy(info
->str
, pv
.u
.pwszVal
, len_bytes
);
2394 *info
->len_bytes
= len_bytes
;
2396 PropVariantClear(&pv
);
2397 IPropertyStore_Release(ps
);
2398 IMMDevice_Release(device
);
2400 return MMSYSERR_NOERROR
;
2403 static LRESULT CALLBACK
WINMM_DevicesMsgProc(HWND hwnd
, UINT msg
, WPARAM wparam
,
2408 return WOD_Open((WINMM_OpenInfo
*)wparam
);
2410 return WOD_Close((HWAVEOUT
)wparam
);
2412 return WID_Open((WINMM_OpenInfo
*)wparam
);
2414 return WID_Close((HWAVEIN
)wparam
);
2415 case MXDM_GETCONTROLDETAILS
:
2416 return MXD_GetControlDetails((WINMM_ControlDetails
*)wparam
);
2417 case MXDM_SETCONTROLDETAILS
:
2418 return MXD_SetControlDetails((WINMM_ControlDetails
*)wparam
);
2419 case DRV_QUERYDEVICEINTERFACESIZE
:
2420 case DRV_QUERYDEVICEINTERFACE
:
2421 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo
*)wparam
);
2423 return DefWindowProcW(hwnd
, msg
, wparam
, lparam
);
2426 static BOOL
WINMM_DevicesThreadDone(void)
2430 EnterCriticalSection(&g_devthread_lock
);
2432 if(g_devthread_token
> 0){
2433 LeaveCriticalSection(&g_devthread_lock
);
2437 for(i
= 0; i
< g_devhandle_count
; ++i
){
2438 if(g_handle_devices
[i
]->open
){
2439 LeaveCriticalSection(&g_devthread_lock
);
2444 DestroyWindow(g_devices_hwnd
);
2445 g_devices_hwnd
= NULL
;
2446 IMMDeviceEnumerator_Release(g_devenum
);
2450 LeaveCriticalSection(&g_devthread_lock
);
2455 static DWORD WINAPI
WINMM_DevicesThreadProc(void *arg
)
2459 static const WCHAR messageW
[] = {'M','e','s','s','a','g','e',0};
2461 hr
= CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
2463 WARN("CoInitializeEx failed: %08x\n", hr
);
2464 FreeLibraryAndExitThread(g_devthread_module
, 1);
2467 hr
= WINMM_InitMMDevices();
2470 FreeLibraryAndExitThread(g_devthread_module
, 1);
2473 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
2474 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&g_devenum
);
2476 WARN("CoCreateInstance failed: %08x\n", hr
);
2478 FreeLibraryAndExitThread(g_devthread_module
, 1);
2481 g_devices_hwnd
= CreateWindowW(messageW
, NULL
, 0, 0, 0, 0, 0,
2482 HWND_MESSAGE
, NULL
, NULL
, NULL
);
2483 if(!g_devices_hwnd
){
2484 WARN("CreateWindow failed: %d\n", GetLastError());
2486 FreeLibraryAndExitThread(g_devthread_module
, 1);
2489 SetWindowLongPtrW(g_devices_hwnd
, GWLP_WNDPROC
,
2490 (LONG_PTR
)WINMM_DevicesMsgProc
);
2492 /* inform caller that the thread is ready to process messages */
2494 evt
= NULL
; /* do not use after this point */
2498 wait
= MsgWaitForMultipleObjects(g_devhandle_count
, g_device_handles
,
2499 FALSE
, INFINITE
, QS_ALLINPUT
);
2500 if(wait
== g_devhandle_count
+ WAIT_OBJECT_0
){
2502 if(PeekMessageW(&msg
, g_devices_hwnd
, 0, 0, PM_REMOVE
))
2503 WARN("Unexpected message: 0x%x\n", msg
.message
);
2506 }else if(wait
< g_devhandle_count
+ WAIT_OBJECT_0
){
2507 WINMM_Device
*device
= g_handle_devices
[wait
- WAIT_OBJECT_0
];
2509 WOD_PushData(device
);
2511 WID_PullData(device
);
2513 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait
,
2515 if(WINMM_DevicesThreadDone()){
2516 TRACE("Quitting devices thread\n");
2517 FreeLibraryAndExitThread(g_devthread_module
, 0);
2521 FreeLibraryAndExitThread(g_devthread_module
, 0);
2524 /* on success, increments g_devthread_token to prevent
2525 * device thread shutdown. caller must decrement. */
2526 static BOOL
WINMM_StartDevicesThread(void)
2531 EnterCriticalSection(&g_devthread_lock
);
2534 wait
= WaitForSingleObject(g_devices_thread
, 0);
2535 if(wait
== WAIT_TIMEOUT
){
2536 /* thread still running */
2537 InterlockedIncrement(&g_devthread_token
);
2538 LeaveCriticalSection(&g_devthread_lock
);
2541 if(wait
!= WAIT_OBJECT_0
){
2543 LeaveCriticalSection(&g_devthread_lock
);
2546 TRACE("Devices thread left dangling message window?\n");
2547 g_devices_hwnd
= NULL
;
2548 CloseHandle(g_devices_thread
);
2549 g_devices_thread
= NULL
;
2550 }else if(g_devices_thread
){
2551 WaitForSingleObject(g_devices_thread
, INFINITE
);
2552 CloseHandle(g_devices_thread
);
2553 g_devices_thread
= NULL
;
2556 TRACE("Starting up devices thread\n");
2558 /* The devices thread holds a reference to the winmm module
2559 * to prevent it from unloading while it's running. */
2560 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
2561 (const WCHAR
*)WINMM_StartDevicesThread
, &g_devthread_module
);
2563 events
[0] = CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2565 g_devices_thread
= CreateThread(NULL
, 0, WINMM_DevicesThreadProc
,
2566 events
[0], 0, NULL
);
2567 if(!g_devices_thread
){
2568 LeaveCriticalSection(&g_devthread_lock
);
2569 CloseHandle(events
[0]);
2570 FreeLibrary(g_devthread_module
);
2574 events
[1] = g_devices_thread
;
2575 wait
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
2576 CloseHandle(events
[0]);
2577 if(wait
!= WAIT_OBJECT_0
){
2578 if(wait
== 1 + WAIT_OBJECT_0
){
2579 CloseHandle(g_devices_thread
);
2580 g_devices_thread
= NULL
;
2581 g_devices_hwnd
= NULL
;
2583 LeaveCriticalSection(&g_devthread_lock
);
2587 InterlockedIncrement(&g_devthread_token
);
2589 LeaveCriticalSection(&g_devthread_lock
);
2594 /**************************************************************************
2595 * waveOutGetNumDevs [WINMM.@]
2597 UINT WINAPI
waveOutGetNumDevs(void)
2599 HRESULT hr
= WINMM_InitMMDevices();
2603 TRACE("count: %u\n", g_outmmdevices_count
);
2605 return g_outmmdevices_count
;
2608 /**************************************************************************
2609 * waveOutGetDevCapsA [WINMM.@]
2611 UINT WINAPI
waveOutGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEOUTCAPSA lpCaps
,
2617 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2620 return MMSYSERR_INVALPARAM
;
2622 ret
= waveOutGetDevCapsW(uDeviceID
, &wocW
, sizeof(wocW
));
2624 if (ret
== MMSYSERR_NOERROR
) {
2626 wocA
.wMid
= wocW
.wMid
;
2627 wocA
.wPid
= wocW
.wPid
;
2628 wocA
.vDriverVersion
= wocW
.vDriverVersion
;
2629 WideCharToMultiByte( CP_ACP
, 0, wocW
.szPname
, -1, wocA
.szPname
,
2630 sizeof(wocA
.szPname
), NULL
, NULL
);
2631 wocA
.dwFormats
= wocW
.dwFormats
;
2632 wocA
.wChannels
= wocW
.wChannels
;
2633 wocA
.dwSupport
= wocW
.dwSupport
;
2634 wocA
.wReserved1
= wocW
.wReserved1
;
2635 memcpy(lpCaps
, &wocA
, min(uSize
, sizeof(wocA
)));
2640 /**************************************************************************
2641 * waveOutGetDevCapsW [WINMM.@]
2643 UINT WINAPI
waveOutGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEOUTCAPSW lpCaps
,
2646 WAVEOUTCAPSW mapper_caps
, *caps
;
2649 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2651 hr
= WINMM_InitMMDevices();
2653 return MMSYSERR_NODRIVER
;
2655 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
2657 if(WINMM_IsMapper(uDeviceID
)){
2658 mapper_caps
.wMid
= 0xFF;
2659 mapper_caps
.wPid
= 0xFF;
2660 mapper_caps
.vDriverVersion
= 0x00010001;
2661 mapper_caps
.dwFormats
= 0xFFFFFFFF;
2662 mapper_caps
.wReserved1
= 0;
2663 mapper_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
2664 WAVECAPS_SAMPLEACCURATE
;
2665 mapper_caps
.wChannels
= 2;
2666 LoadStringW(hWinMM32Instance
, IDS_MAPPER_NAME
, mapper_caps
.szPname
, MAXPNAMELEN
);
2668 caps
= &mapper_caps
;
2670 if(uDeviceID
>= g_outmmdevices_count
)
2671 return MMSYSERR_BADDEVICEID
;
2673 caps
= &read_map(g_out_map
, uDeviceID
)->out_caps
;
2676 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
2678 return MMSYSERR_NOERROR
;
2681 /**************************************************************************
2682 * waveOutGetErrorTextA [WINMM.@]
2683 * waveInGetErrorTextA [WINMM.@]
2685 UINT WINAPI
waveOutGetErrorTextA(UINT uError
, LPSTR lpText
, UINT uSize
)
2689 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
2690 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
2693 LPWSTR xstr
= HeapAlloc(GetProcessHeap(), 0, uSize
* sizeof(WCHAR
));
2694 if (!xstr
) ret
= MMSYSERR_NOMEM
;
2697 ret
= waveOutGetErrorTextW(uError
, xstr
, uSize
);
2698 if (ret
== MMSYSERR_NOERROR
)
2699 WideCharToMultiByte(CP_ACP
, 0, xstr
, -1, lpText
, uSize
, NULL
, NULL
);
2700 HeapFree(GetProcessHeap(), 0, xstr
);
2706 /**************************************************************************
2707 * waveOutGetErrorTextW [WINMM.@]
2708 * waveInGetErrorTextW [WINMM.@]
2710 UINT WINAPI
waveOutGetErrorTextW(UINT uError
, LPWSTR lpText
, UINT uSize
)
2712 UINT ret
= MMSYSERR_BADERRNUM
;
2714 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
2715 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
2717 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2718 * a warning for the test was always true */
2719 (/*uError >= MMSYSERR_BASE && */ uError
<= MMSYSERR_LASTERROR
) ||
2720 (uError
>= WAVERR_BASE
&& uError
<= WAVERR_LASTERROR
)) {
2721 if (LoadStringW(hWinMM32Instance
,
2722 uError
, lpText
, uSize
) > 0) {
2723 ret
= MMSYSERR_NOERROR
;
2729 /**************************************************************************
2730 * waveOutOpen [WINMM.@]
2731 * All the args/structs have the same layout as the win16 equivalents
2733 MMRESULT WINAPI
waveOutOpen(LPHWAVEOUT lphWaveOut
, UINT uDeviceID
,
2734 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
2735 DWORD_PTR dwInstance
, DWORD dwFlags
)
2738 WINMM_OpenInfo info
;
2739 WINMM_CBInfo cb_info
;
2741 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut
, uDeviceID
, lpFormat
,
2742 dwCallback
, dwInstance
, dwFlags
);
2744 if(!lphWaveOut
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
2745 return MMSYSERR_INVALPARAM
;
2747 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
2748 if(res
!= MMSYSERR_NOERROR
)
2751 if(!WINMM_StartDevicesThread())
2752 return MMSYSERR_NODRIVER
;
2755 info
.format
= (WAVEFORMATEX
*)lpFormat
;
2756 info
.callback
= dwCallback
;
2757 info
.cb_user
= dwInstance
;
2758 info
.req_device
= uDeviceID
;
2759 info
.flags
= dwFlags
;
2762 res
= SendMessageW(g_devices_hwnd
, WODM_OPEN
, (DWORD_PTR
)&info
, 0);
2763 InterlockedDecrement(&g_devthread_token
);
2764 if(res
!= MMSYSERR_NOERROR
|| (dwFlags
& WAVE_FORMAT_QUERY
))
2768 *lphWaveOut
= (HWAVEOUT
)info
.handle
;
2770 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2771 cb_info
.callback
= dwCallback
;
2772 cb_info
.user
= dwInstance
;
2773 cb_info
.hwave
= info
.handle
;
2775 WINMM_NotifyClient(&cb_info
, WOM_OPEN
, 0, 0);
2780 /**************************************************************************
2781 * waveOutClose [WINMM.@]
2783 UINT WINAPI
waveOutClose(HWAVEOUT hWaveOut
)
2786 WINMM_Device
*device
;
2787 WINMM_CBInfo cb_info
;
2789 TRACE("(%p)\n", hWaveOut
);
2791 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2793 if(!WINMM_ValidateAndLock(device
))
2794 return MMSYSERR_INVALHANDLE
;
2796 cb_info
= device
->cb_info
;
2798 LeaveCriticalSection(&device
->lock
);
2800 res
= SendMessageW(g_devices_hwnd
, WODM_CLOSE
, (WPARAM
)hWaveOut
, 0);
2802 if(res
== MMSYSERR_NOERROR
)
2803 WINMM_NotifyClient(&cb_info
, WOM_CLOSE
, 0, 0);
2808 /**************************************************************************
2809 * waveOutPrepareHeader [WINMM.@]
2811 UINT WINAPI
waveOutPrepareHeader(HWAVEOUT hWaveOut
,
2812 WAVEHDR
* lpWaveOutHdr
, UINT uSize
)
2814 TRACE("(%p, %p, %u)\n", hWaveOut
, lpWaveOutHdr
, uSize
);
2816 if(!lpWaveOutHdr
|| uSize
< sizeof(WAVEHDR
))
2817 return MMSYSERR_INVALPARAM
;
2819 if(lpWaveOutHdr
->dwFlags
& WHDR_PREPARED
)
2820 return MMSYSERR_NOERROR
;
2822 return WINMM_PrepareHeader((HWAVE
)hWaveOut
, lpWaveOutHdr
);
2825 /**************************************************************************
2826 * waveOutUnprepareHeader [WINMM.@]
2828 UINT WINAPI
waveOutUnprepareHeader(HWAVEOUT hWaveOut
,
2829 LPWAVEHDR lpWaveOutHdr
, UINT uSize
)
2831 TRACE("(%p, %p, %u)\n", hWaveOut
, lpWaveOutHdr
, uSize
);
2833 if(!lpWaveOutHdr
|| uSize
< sizeof(WAVEHDR
))
2834 return MMSYSERR_INVALPARAM
;
2836 if(lpWaveOutHdr
->dwFlags
& WHDR_INQUEUE
)
2837 return WAVERR_STILLPLAYING
;
2839 if(!(lpWaveOutHdr
->dwFlags
& WHDR_PREPARED
))
2840 return MMSYSERR_NOERROR
;
2842 return WINMM_UnprepareHeader((HWAVE
)hWaveOut
, lpWaveOutHdr
);
2845 /**************************************************************************
2846 * waveOutWrite [WINMM.@]
2848 UINT WINAPI
waveOutWrite(HWAVEOUT hWaveOut
, WAVEHDR
*header
, UINT uSize
)
2850 WINMM_Device
*device
;
2853 TRACE("(%p, %p, %u)\n", hWaveOut
, header
, uSize
);
2855 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2857 if(!WINMM_ValidateAndLock(device
))
2858 return MMSYSERR_INVALHANDLE
;
2860 if(!header
->lpData
|| !(header
->dwFlags
& WHDR_PREPARED
)){
2861 LeaveCriticalSection(&device
->lock
);
2862 return WAVERR_UNPREPARED
;
2865 if(header
->dwFlags
& WHDR_INQUEUE
){
2866 LeaveCriticalSection(&device
->lock
);
2867 return WAVERR_STILLPLAYING
;
2870 TRACE("dwBufferLength: %u\n", header
->dwBufferLength
);
2872 if(device
->acm_handle
){
2873 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
2875 ash
->cbSrcLength
= header
->dwBufferLength
;
2876 mr
= acmStreamConvert(device
->acm_handle
, ash
, 0);
2877 if(mr
!= MMSYSERR_NOERROR
){
2878 LeaveCriticalSection(&device
->lock
);
2884 device
->last
->lpNext
= header
;
2885 device
->last
= header
;
2886 if(!device
->playing
)
2887 device
->playing
= header
;
2889 device
->playing
= device
->first
= device
->last
= header
;
2890 if(header
->dwFlags
& WHDR_BEGINLOOP
){
2891 device
->loop_counter
= header
->dwLoops
;
2892 device
->loop_start
= header
;
2896 header
->lpNext
= NULL
;
2897 header
->dwFlags
&= ~WHDR_DONE
;
2898 header
->dwFlags
|= WHDR_INQUEUE
;
2900 mr
= WINMM_BeginPlaying(device
);
2902 LeaveCriticalSection(&device
->lock
);
2907 /**************************************************************************
2908 * waveOutBreakLoop [WINMM.@]
2910 UINT WINAPI
waveOutBreakLoop(HWAVEOUT hWaveOut
)
2912 WINMM_Device
*device
;
2914 TRACE("(%p)\n", hWaveOut
);
2916 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2918 if(!WINMM_ValidateAndLock(device
))
2919 return MMSYSERR_INVALHANDLE
;
2921 device
->loop_counter
= 0;
2923 LeaveCriticalSection(&device
->lock
);
2925 return MMSYSERR_NOERROR
;
2928 /**************************************************************************
2929 * waveOutPause [WINMM.@]
2931 UINT WINAPI
waveOutPause(HWAVEOUT hWaveOut
)
2933 TRACE("(%p)\n", hWaveOut
);
2935 return WINMM_Pause((HWAVE
)hWaveOut
);
2938 /**************************************************************************
2939 * waveOutReset [WINMM.@]
2941 UINT WINAPI
waveOutReset(HWAVEOUT hWaveOut
)
2943 TRACE("(%p)\n", hWaveOut
);
2945 return WINMM_Reset((HWAVE
)hWaveOut
);
2948 /**************************************************************************
2949 * waveOutRestart [WINMM.@]
2951 UINT WINAPI
waveOutRestart(HWAVEOUT hWaveOut
)
2953 WINMM_Device
*device
;
2956 TRACE("(%p)\n", hWaveOut
);
2958 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2960 if(!WINMM_ValidateAndLock(device
))
2961 return MMSYSERR_INVALHANDLE
;
2963 device
->stopped
= TRUE
;
2965 mr
= WINMM_BeginPlaying(device
);
2967 LeaveCriticalSection(&device
->lock
);
2972 /**************************************************************************
2973 * waveOutGetPosition [WINMM.@]
2975 UINT WINAPI
waveOutGetPosition(HWAVEOUT hWaveOut
, LPMMTIME lpTime
,
2978 TRACE("(%p, %p, %u)\n", hWaveOut
, lpTime
, uSize
);
2980 if(!uSize
|| !lpTime
)
2981 return MMSYSERR_INVALPARAM
;
2983 if(uSize
< sizeof(MMTIME
))
2984 return MMSYSERR_ERROR
;
2986 return WINMM_GetPosition((HWAVE
)hWaveOut
, lpTime
);
2989 /**************************************************************************
2990 * waveOutGetPitch [WINMM.@]
2992 UINT WINAPI
waveOutGetPitch(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
2994 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
2995 return MMSYSERR_NOTSUPPORTED
;
2998 /**************************************************************************
2999 * waveOutSetPitch [WINMM.@]
3001 UINT WINAPI
waveOutSetPitch(HWAVEOUT hWaveOut
, DWORD dw
)
3003 TRACE("(%p, %08x)\n", hWaveOut
, dw
);
3005 return MMSYSERR_NOTSUPPORTED
;
3008 /**************************************************************************
3009 * waveOutGetPlaybackRate [WINMM.@]
3011 UINT WINAPI
waveOutGetPlaybackRate(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
3013 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
3015 return MMSYSERR_NOTSUPPORTED
;
3018 /**************************************************************************
3019 * waveOutSetPlaybackRate [WINMM.@]
3021 UINT WINAPI
waveOutSetPlaybackRate(HWAVEOUT hWaveOut
, DWORD dw
)
3023 TRACE("(%p, %08x)\n", hWaveOut
, dw
);
3025 return MMSYSERR_NOTSUPPORTED
;
3028 /**************************************************************************
3029 * waveOutGetVolume [WINMM.@]
3031 UINT WINAPI
waveOutGetVolume(HWAVEOUT hWaveOut
, DWORD
*out
)
3033 WINMM_Device
*device
;
3038 TRACE("(%p, %p)\n", hWaveOut
, out
);
3041 return MMSYSERR_INVALPARAM
;
3043 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3045 if(!WINMM_ValidateAndLock(device
))
3046 return MMSYSERR_INVALHANDLE
;
3048 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
3050 LeaveCriticalSection(&device
->lock
);
3051 WARN("GetChannelCount failed: %08x\n", hr
);
3052 return MMSYSERR_ERROR
;
3055 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
3057 LeaveCriticalSection(&device
->lock
);
3058 return MMSYSERR_NOMEM
;
3061 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
3063 LeaveCriticalSection(&device
->lock
);
3064 HeapFree(GetProcessHeap(), 0, vols
);
3065 WARN("GetAllVolumes failed: %08x\n", hr
);
3066 return MMSYSERR_ERROR
;
3069 LeaveCriticalSection(&device
->lock
);
3071 *out
= ((UINT16
)(vols
[0] * (DWORD
)0xFFFF));
3073 *out
|= ((UINT16
)(vols
[1] * (DWORD
)0xFFFF)) << 16;
3075 HeapFree(GetProcessHeap(), 0, vols
);
3077 return MMSYSERR_NOERROR
;
3080 /**************************************************************************
3081 * waveOutSetVolume [WINMM.@]
3083 UINT WINAPI
waveOutSetVolume(HWAVEOUT hWaveOut
, DWORD in
)
3085 WINMM_Device
*device
;
3090 TRACE("(%p, %08x)\n", hWaveOut
, in
);
3092 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3094 if(!WINMM_ValidateAndLock(device
))
3095 return MMSYSERR_INVALHANDLE
;
3097 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
3099 LeaveCriticalSection(&device
->lock
);
3100 WARN("GetChannelCount failed: %08x\n", hr
);
3101 return MMSYSERR_ERROR
;
3104 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
3106 LeaveCriticalSection(&device
->lock
);
3107 return MMSYSERR_NOMEM
;
3110 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
3112 LeaveCriticalSection(&device
->lock
);
3113 HeapFree(GetProcessHeap(), 0, vols
);
3114 WARN("GetAllVolumes failed: %08x\n", hr
);
3115 return MMSYSERR_ERROR
;
3118 vols
[0] = (float)((DWORD
)(in
& 0xFFFF) / (float)0xFFFF);
3120 vols
[1] = (float)((DWORD
)(in
>> 16) / (float)0xFFFF);
3122 hr
= IAudioStreamVolume_SetAllVolumes(device
->volume
, channels
, vols
);
3124 LeaveCriticalSection(&device
->lock
);
3125 HeapFree(GetProcessHeap(), 0, vols
);
3126 WARN("SetAllVolumes failed: %08x\n", hr
);
3127 return MMSYSERR_ERROR
;
3130 LeaveCriticalSection(&device
->lock
);
3132 HeapFree(GetProcessHeap(), 0, vols
);
3134 return MMSYSERR_NOERROR
;
3137 /**************************************************************************
3138 * waveOutGetID [WINMM.@]
3140 UINT WINAPI
waveOutGetID(HWAVEOUT hWaveOut
, UINT
* lpuDeviceID
)
3142 WINMM_Device
*device
;
3146 TRACE("(%p, %p)\n", hWaveOut
, lpuDeviceID
);
3149 return MMSYSERR_INVALPARAM
;
3151 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3152 if(!WINMM_ValidateAndLock(device
))
3153 return MMSYSERR_INVALHANDLE
;
3155 LeaveCriticalSection(&device
->lock
);
3157 WINMM_DecomposeHWAVE((HWAVE
)hWaveOut
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3159 return MMSYSERR_NOERROR
;
3162 static UINT
WINMM_QueryInstanceIDSize(UINT device
, DWORD_PTR
*len
, BOOL is_out
)
3165 WINMM_MMDevice
**devices
;
3167 TRACE("(%u, %p, %d)\n", device
, len
, is_out
);
3170 count
= g_outmmdevices_count
;
3171 devices
= g_out_map
;
3173 count
= g_inmmdevices_count
;
3178 return MMSYSERR_INVALHANDLE
;
3180 EnterCriticalSection(&g_devthread_lock
);
3181 *len
= (lstrlenW(devices
[device
]->dev_id
) + 1) * sizeof(WCHAR
);
3182 LeaveCriticalSection(&g_devthread_lock
);
3184 return MMSYSERR_NOERROR
;
3187 static UINT
WINMM_QueryInstanceID(UINT device
, WCHAR
*str
, DWORD_PTR len
,
3191 WINMM_MMDevice
**devices
;
3193 TRACE("(%u, %p, %d)\n", device
, str
, is_out
);
3196 count
= g_outmmdevices_count
;
3197 devices
= g_out_map
;
3199 count
= g_inmmdevices_count
;
3204 return MMSYSERR_INVALHANDLE
;
3206 EnterCriticalSection(&g_devthread_lock
);
3207 id_len
= (lstrlenW(devices
[device
]->dev_id
) + 1) * sizeof(WCHAR
);
3209 LeaveCriticalSection(&g_devthread_lock
);
3210 return MMSYSERR_ERROR
;
3213 memcpy(str
, devices
[device
]->dev_id
, id_len
);
3214 LeaveCriticalSection(&g_devthread_lock
);
3216 return MMSYSERR_NOERROR
;
3219 static UINT
get_device_interface(UINT msg
, BOOL is_out
, UINT index
, WCHAR
*out
, ULONG
*out_len
)
3221 WINMM_QueryInterfaceInfo info
;
3224 if(!WINMM_StartDevicesThread())
3225 return MMSYSERR_NODRIVER
;
3227 info
.is_out
= is_out
;
3230 info
.len_bytes
= out_len
;
3232 ret
= SendMessageW(g_devices_hwnd
, msg
, (DWORD_PTR
)&info
, 0);
3233 InterlockedDecrement(&g_devthread_token
);
3237 /**************************************************************************
3238 * waveOutMessage [WINMM.@]
3240 UINT WINAPI
waveOutMessage(HWAVEOUT hWaveOut
, UINT uMessage
,
3241 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3243 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut
, uMessage
, dwParam1
, dwParam2
);
3246 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3247 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut
),
3248 (DWORD_PTR
*)dwParam1
, TRUE
);
3249 case DRV_QUERYFUNCTIONINSTANCEID
:
3250 return WINMM_QueryInstanceID(HandleToULong(hWaveOut
), (WCHAR
*)dwParam1
, dwParam2
, TRUE
);
3251 case DRV_QUERYDEVICEINTERFACESIZE
:
3252 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE
, TRUE
, HandleToULong(hWaveOut
),
3253 NULL
, (ULONG
*)dwParam1
);
3254 case DRV_QUERYDEVICEINTERFACE
:
3256 ULONG size
= dwParam2
;
3257 return get_device_interface(DRV_QUERYDEVICEINTERFACE
, TRUE
, HandleToULong(hWaveOut
),
3258 (WCHAR
*)dwParam1
, &size
);
3260 case DRV_QUERYMAPPABLE
:
3261 return MMSYSERR_NOERROR
;
3262 case DRVM_MAPPER_PREFERRED_GET
:
3264 if(g_outmmdevices_count
> 0)
3265 /* Device 0 is always the default device */
3266 *(DWORD
*)dwParam1
= 0;
3268 *(DWORD
*)dwParam1
= -1;
3273 *(DWORD
*)dwParam2
= 0;
3275 return MMSYSERR_NOERROR
;
3278 TRACE("Message not supported: %u\n", uMessage
);
3280 return MMSYSERR_NOTSUPPORTED
;
3283 /**************************************************************************
3284 * waveInGetNumDevs [WINMM.@]
3286 UINT WINAPI
waveInGetNumDevs(void)
3288 HRESULT hr
= WINMM_InitMMDevices();
3292 TRACE("count: %u\n", g_inmmdevices_count
);
3294 return g_inmmdevices_count
;
3297 /**************************************************************************
3298 * waveInGetDevCapsW [WINMM.@]
3300 UINT WINAPI
waveInGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEINCAPSW lpCaps
, UINT uSize
)
3302 WAVEINCAPSW mapper_caps
, *caps
;
3305 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3307 hr
= WINMM_InitMMDevices();
3309 return MMSYSERR_NODRIVER
;
3312 return MMSYSERR_INVALPARAM
;
3314 if(WINMM_IsMapper(uDeviceID
)){
3315 mapper_caps
.wMid
= 0xFF;
3316 mapper_caps
.wPid
= 0xFF;
3317 mapper_caps
.vDriverVersion
= 0x00010001;
3318 mapper_caps
.dwFormats
= 0xFFFFFFFF;
3319 mapper_caps
.wReserved1
= 0;
3320 mapper_caps
.wChannels
= 2;
3321 LoadStringW(hWinMM32Instance
, IDS_MAPPER_NAME
, mapper_caps
.szPname
, MAXPNAMELEN
);
3323 caps
= &mapper_caps
;
3325 if(uDeviceID
>= g_inmmdevices_count
)
3326 return MMSYSERR_BADDEVICEID
;
3328 caps
= &read_map(g_in_map
, uDeviceID
)->in_caps
;
3331 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
3333 return MMSYSERR_NOERROR
;
3336 /**************************************************************************
3337 * waveInGetDevCapsA [WINMM.@]
3339 UINT WINAPI
waveInGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEINCAPSA lpCaps
, UINT uSize
)
3344 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3347 return MMSYSERR_INVALPARAM
;
3349 ret
= waveInGetDevCapsW(uDeviceID
, &wicW
, sizeof(wicW
));
3351 if (ret
== MMSYSERR_NOERROR
) {
3353 wicA
.wMid
= wicW
.wMid
;
3354 wicA
.wPid
= wicW
.wPid
;
3355 wicA
.vDriverVersion
= wicW
.vDriverVersion
;
3356 WideCharToMultiByte( CP_ACP
, 0, wicW
.szPname
, -1, wicA
.szPname
,
3357 sizeof(wicA
.szPname
), NULL
, NULL
);
3358 wicA
.dwFormats
= wicW
.dwFormats
;
3359 wicA
.wChannels
= wicW
.wChannels
;
3360 wicA
.wReserved1
= wicW
.wReserved1
;
3361 memcpy(lpCaps
, &wicA
, min(uSize
, sizeof(wicA
)));
3366 /**************************************************************************
3367 * waveInOpen [WINMM.@]
3369 MMRESULT WINAPI
waveInOpen(HWAVEIN
* lphWaveIn
, UINT uDeviceID
,
3370 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
3371 DWORD_PTR dwInstance
, DWORD dwFlags
)
3374 WINMM_OpenInfo info
;
3375 WINMM_CBInfo cb_info
;
3377 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn
, uDeviceID
, lpFormat
,
3378 dwCallback
, dwInstance
, dwFlags
);
3380 if(!lphWaveIn
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
3381 return MMSYSERR_INVALPARAM
;
3383 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
3384 if(res
!= MMSYSERR_NOERROR
)
3387 if(!WINMM_StartDevicesThread())
3388 return MMSYSERR_NODRIVER
;
3391 info
.format
= (WAVEFORMATEX
*)lpFormat
;
3392 info
.callback
= dwCallback
;
3393 info
.cb_user
= dwInstance
;
3394 info
.req_device
= uDeviceID
;
3395 info
.flags
= dwFlags
;
3398 res
= SendMessageW(g_devices_hwnd
, WIDM_OPEN
, (DWORD_PTR
)&info
, 0);
3399 InterlockedDecrement(&g_devthread_token
);
3400 if(res
!= MMSYSERR_NOERROR
|| (dwFlags
& WAVE_FORMAT_QUERY
))
3404 *lphWaveIn
= (HWAVEIN
)info
.handle
;
3406 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
3407 cb_info
.callback
= dwCallback
;
3408 cb_info
.user
= dwInstance
;
3409 cb_info
.hwave
= info
.handle
;
3411 WINMM_NotifyClient(&cb_info
, WIM_OPEN
, 0, 0);
3416 /**************************************************************************
3417 * waveInClose [WINMM.@]
3419 UINT WINAPI
waveInClose(HWAVEIN hWaveIn
)
3421 WINMM_Device
*device
;
3422 WINMM_CBInfo cb_info
;
3425 TRACE("(%p)\n", hWaveIn
);
3427 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3429 if(!WINMM_ValidateAndLock(device
))
3430 return MMSYSERR_INVALHANDLE
;
3432 cb_info
= device
->cb_info
;
3434 LeaveCriticalSection(&device
->lock
);
3436 res
= SendMessageW(g_devices_hwnd
, WIDM_CLOSE
, (WPARAM
)hWaveIn
, 0);
3438 if(res
== MMSYSERR_NOERROR
)
3439 WINMM_NotifyClient(&cb_info
, WIM_CLOSE
, 0, 0);
3444 /**************************************************************************
3445 * waveInPrepareHeader [WINMM.@]
3447 UINT WINAPI
waveInPrepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3450 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3452 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3453 return MMSYSERR_INVALPARAM
;
3455 if(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
)
3456 return MMSYSERR_NOERROR
;
3458 return WINMM_PrepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3461 /**************************************************************************
3462 * waveInUnprepareHeader [WINMM.@]
3464 UINT WINAPI
waveInUnprepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3467 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3469 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3470 return MMSYSERR_INVALPARAM
;
3472 if(lpWaveInHdr
->dwFlags
& WHDR_INQUEUE
)
3473 return WAVERR_STILLPLAYING
;
3475 if(!(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
))
3476 return MMSYSERR_NOERROR
;
3478 return WINMM_UnprepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3481 /**************************************************************************
3482 * waveInAddBuffer [WINMM.@]
3484 UINT WINAPI
waveInAddBuffer(HWAVEIN hWaveIn
, WAVEHDR
*header
, UINT uSize
)
3486 WINMM_Device
*device
;
3488 TRACE("(%p, %p, %u)\n", hWaveIn
, header
, uSize
);
3490 if(!header
|| uSize
< sizeof(WAVEHDR
))
3491 return MMSYSERR_INVALPARAM
;
3493 if(!(header
->dwFlags
& WHDR_PREPARED
))
3494 return WAVERR_UNPREPARED
;
3496 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3498 if(!WINMM_ValidateAndLock(device
))
3499 return MMSYSERR_INVALHANDLE
;
3502 device
->first
= device
->last
= header
;
3504 device
->last
->lpNext
= header
;
3505 device
->last
= header
;
3508 header
->dwBytesRecorded
= 0;
3509 header
->lpNext
= NULL
;
3510 header
->dwFlags
&= ~WHDR_DONE
;
3511 header
->dwFlags
|= WHDR_INQUEUE
;
3513 LeaveCriticalSection(&device
->lock
);
3515 return MMSYSERR_NOERROR
;
3518 /**************************************************************************
3519 * waveInReset [WINMM.@]
3521 UINT WINAPI
waveInReset(HWAVEIN hWaveIn
)
3523 TRACE("(%p)\n", hWaveIn
);
3525 return WINMM_Reset((HWAVE
)hWaveIn
);
3528 /**************************************************************************
3529 * waveInStart [WINMM.@]
3531 UINT WINAPI
waveInStart(HWAVEIN hWaveIn
)
3533 WINMM_Device
*device
;
3536 TRACE("(%p)\n", hWaveIn
);
3538 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3540 if(!WINMM_ValidateAndLock(device
))
3541 return MMSYSERR_INVALHANDLE
;
3543 mr
= WINMM_BeginPlaying(device
);
3545 LeaveCriticalSection(&device
->lock
);
3550 /**************************************************************************
3551 * waveInStop [WINMM.@]
3553 UINT WINAPI
waveInStop(HWAVEIN hWaveIn
)
3555 WINMM_CBInfo cb_info
;
3556 WINMM_Device
*device
;
3560 TRACE("(%p)\n", hWaveIn
);
3562 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3564 if(!WINMM_ValidateAndLock(device
))
3565 return MMSYSERR_INVALHANDLE
;
3567 hr
= WINMM_Pause((HWAVE
)hWaveIn
);
3569 LeaveCriticalSection(&device
->lock
);
3570 return MMSYSERR_ERROR
;
3572 device
->stopped
= TRUE
;
3574 buf
= device
->first
;
3575 if(buf
&& buf
->dwBytesRecorded
> 0){
3576 device
->first
= buf
->lpNext
;
3580 cb_info
= device
->cb_info
;
3582 LeaveCriticalSection(&device
->lock
);
3585 buf
->dwFlags
&= ~WHDR_INQUEUE
;
3586 buf
->dwFlags
|= WHDR_DONE
;
3587 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)buf
, 0);
3590 return MMSYSERR_NOERROR
;
3593 /**************************************************************************
3594 * waveInGetPosition [WINMM.@]
3596 UINT WINAPI
waveInGetPosition(HWAVEIN hWaveIn
, LPMMTIME lpTime
,
3599 TRACE("(%p, %p, %u)\n", hWaveIn
, lpTime
, uSize
);
3601 if(!uSize
|| !lpTime
)
3602 return MMSYSERR_INVALPARAM
;
3604 if(uSize
< sizeof(MMTIME
))
3605 return MMSYSERR_ERROR
;
3607 return WINMM_GetPosition((HWAVE
)hWaveIn
, lpTime
);
3610 /**************************************************************************
3611 * waveInGetID [WINMM.@]
3613 UINT WINAPI
waveInGetID(HWAVEIN hWaveIn
, UINT
* lpuDeviceID
)
3617 WINMM_Device
*device
;
3619 TRACE("(%p, %p)\n", hWaveIn
, lpuDeviceID
);
3622 return MMSYSERR_INVALPARAM
;
3624 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3625 if(!WINMM_ValidateAndLock(device
))
3626 return MMSYSERR_INVALHANDLE
;
3628 LeaveCriticalSection(&device
->lock
);
3630 WINMM_DecomposeHWAVE((HWAVE
)hWaveIn
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3632 return MMSYSERR_NOERROR
;
3635 /**************************************************************************
3636 * waveInMessage [WINMM.@]
3638 UINT WINAPI
waveInMessage(HWAVEIN hWaveIn
, UINT uMessage
,
3639 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3641 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn
, uMessage
, dwParam1
, dwParam2
);
3644 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3645 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn
),
3646 (DWORD_PTR
*)dwParam1
, FALSE
);
3647 case DRV_QUERYFUNCTIONINSTANCEID
:
3648 return WINMM_QueryInstanceID(HandleToULong(hWaveIn
), (WCHAR
*)dwParam1
, dwParam2
, FALSE
);
3649 case DRV_QUERYDEVICEINTERFACESIZE
:
3650 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE
, FALSE
, HandleToULong(hWaveIn
),
3651 NULL
, (ULONG
*)dwParam1
);
3652 case DRV_QUERYDEVICEINTERFACE
:
3654 ULONG size
= dwParam2
;
3655 return get_device_interface(DRV_QUERYDEVICEINTERFACE
, FALSE
, HandleToULong(hWaveIn
),
3656 (WCHAR
*)dwParam1
, &size
);
3658 case DRV_QUERYMAPPABLE
:
3659 return MMSYSERR_NOERROR
;
3660 case DRVM_MAPPER_PREFERRED_GET
:
3662 if(g_inmmdevices_count
> 0)
3663 /* Device 0 is always the default device */
3664 *(DWORD
*)dwParam1
= 0;
3666 *(DWORD
*)dwParam1
= -1;
3671 *(DWORD
*)dwParam2
= 0;
3673 return MMSYSERR_NOERROR
;
3676 TRACE("Message not supported: %u\n", uMessage
);
3678 return MMSYSERR_NOTSUPPORTED
;
3681 UINT WINAPI
mixerGetNumDevs(void)
3687 hr
= WINMM_InitMMDevices();
3691 return g_outmmdevices_count
+ g_inmmdevices_count
;
3694 /**************************************************************************
3695 * mixerGetDevCapsA [WINMM.@]
3697 UINT WINAPI
mixerGetDevCapsA(UINT_PTR uDeviceID
, LPMIXERCAPSA lpCaps
, UINT uSize
)
3702 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3705 return MMSYSERR_INVALPARAM
;
3707 ret
= mixerGetDevCapsW(uDeviceID
, &micW
, sizeof(micW
));
3709 if (ret
== MMSYSERR_NOERROR
) {
3711 micA
.wMid
= micW
.wMid
;
3712 micA
.wPid
= micW
.wPid
;
3713 micA
.vDriverVersion
= micW
.vDriverVersion
;
3714 WideCharToMultiByte( CP_ACP
, 0, micW
.szPname
, -1, micA
.szPname
,
3715 sizeof(micA
.szPname
), NULL
, NULL
);
3716 micA
.fdwSupport
= micW
.fdwSupport
;
3717 micA
.cDestinations
= micW
.cDestinations
;
3718 memcpy(lpCaps
, &micA
, min(uSize
, sizeof(micA
)));
3723 /**************************************************************************
3724 * mixerGetDevCapsW [WINMM.@]
3726 UINT WINAPI
mixerGetDevCapsW(UINT_PTR uDeviceID
, LPMIXERCAPSW lpCaps
, UINT uSize
)
3728 WINMM_MMDevice
*mmdevice
;
3732 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3734 hr
= WINMM_InitMMDevices();
3736 return MMSYSERR_NODRIVER
;
3739 return MMSYSERR_INVALPARAM
;
3742 return MMSYSERR_NOERROR
;
3744 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3745 mmdevice
= WINMM_GetMixerMMDevice((HMIXEROBJ
)uDeviceID
,
3746 MIXER_OBJECTF_MIXER
, NULL
);
3747 else if(uDeviceID
< g_outmmdevices_count
)
3748 mmdevice
= read_map(g_out_map
, uDeviceID
);
3750 mmdevice
= read_map(g_in_map
, uDeviceID
- g_outmmdevices_count
);
3753 return MMSYSERR_BADDEVICEID
;
3755 if(mmdevice
->dataflow
== eRender
)
3756 memcpy(caps
.szPname
, mmdevice
->out_caps
.szPname
, sizeof(caps
.szPname
));
3758 memcpy(caps
.szPname
, mmdevice
->in_caps
.szPname
, sizeof(caps
.szPname
));
3762 caps
.vDriverVersion
= 0x00010001;
3763 caps
.fdwSupport
= 0;
3764 caps
.cDestinations
= 1;
3766 memcpy(lpCaps
, &caps
, uSize
);
3768 return MMSYSERR_NOERROR
;
3771 /**************************************************************************
3772 * mixerOpen [WINMM.@]
3774 UINT WINAPI
mixerOpen(LPHMIXER lphMix
, UINT uDeviceID
, DWORD_PTR dwCallback
,
3775 DWORD_PTR dwInstance
, DWORD fdwOpen
)
3777 WINMM_MMDevice
*mmdevice
;
3781 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix
, uDeviceID
, dwCallback
,
3782 dwInstance
, fdwOpen
);
3784 hr
= WINMM_InitMMDevices();
3786 return MMSYSERR_NODRIVER
;
3789 return MMSYSERR_INVALPARAM
;
3791 mr
= WINMM_CheckCallback(dwCallback
, fdwOpen
, TRUE
);
3792 if(mr
!= MMSYSERR_NOERROR
)
3795 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3796 return MMSYSERR_BADDEVICEID
;
3798 if(uDeviceID
< g_outmmdevices_count
){
3799 mmdevice
= read_map(g_out_map
, uDeviceID
);
3800 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
, TRUE
,
3801 mmdevice
->mixer_count
);
3803 mmdevice
= read_map(g_in_map
, uDeviceID
- g_outmmdevices_count
);
3804 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
- g_outmmdevices_count
,
3805 FALSE
, mmdevice
->mixer_count
);
3808 ++mmdevice
->mixer_count
;
3810 return MMSYSERR_NOERROR
;
3813 /**************************************************************************
3814 * mixerClose [WINMM.@]
3816 UINT WINAPI
mixerClose(HMIXER hMix
)
3818 TRACE("(%p)\n", hMix
);
3820 return MMSYSERR_NOERROR
;
3823 /**************************************************************************
3824 * mixerGetID [WINMM.@]
3826 UINT WINAPI
mixerGetID(HMIXEROBJ hmix
, LPUINT lpid
, DWORD fdwID
)
3828 WINMM_MMDevice
*mmdevice
;
3831 TRACE("(%p, %p, %x)\n", hmix
, lpid
, fdwID
);
3833 hr
= WINMM_InitMMDevices();
3835 return MMSYSERR_NODRIVER
;
3838 return MMSYSERR_INVALPARAM
;
3840 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwID
, lpid
);
3842 return MMSYSERR_INVALHANDLE
;
3844 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3845 *lpid
+= g_outmmdevices_count
;
3847 return MMSYSERR_NOERROR
;
3850 /**************************************************************************
3851 * mixerGetControlDetailsW [WINMM.@]
3853 UINT WINAPI
mixerGetControlDetailsW(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdW
,
3856 WINMM_ControlDetails details
;
3858 TRACE("(%p, %p, %x)\n", hmix
, lpmcdW
, fdwDetails
);
3860 if(!WINMM_StartDevicesThread())
3861 return MMSYSERR_NODRIVER
;
3863 if(!lpmcdW
|| !lpmcdW
->paDetails
)
3864 return MMSYSERR_INVALPARAM
;
3866 TRACE("dwControlID: %u\n", lpmcdW
->dwControlID
);
3868 details
.hmix
= hmix
;
3869 details
.details
= lpmcdW
;
3870 details
.flags
= fdwDetails
;
3872 return SendMessageW(g_devices_hwnd
, MXDM_GETCONTROLDETAILS
,
3873 (DWORD_PTR
)&details
, 0);
3876 /**************************************************************************
3877 * mixerGetControlDetailsA [WINMM.@]
3879 UINT WINAPI
mixerGetControlDetailsA(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdA
,
3882 UINT ret
= MMSYSERR_NOTSUPPORTED
;
3884 TRACE("(%p, %p, %08x)\n", hmix
, lpmcdA
, fdwDetails
);
3886 if (lpmcdA
== NULL
|| lpmcdA
->cbStruct
!= sizeof(*lpmcdA
))
3887 return MMSYSERR_INVALPARAM
;
3889 switch (fdwDetails
& MIXER_GETCONTROLDETAILSF_QUERYMASK
) {
3890 case MIXER_GETCONTROLDETAILSF_VALUE
:
3891 /* can safely use A structure as it is, no string inside */
3892 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3894 case MIXER_GETCONTROLDETAILSF_LISTTEXT
:
3896 MIXERCONTROLDETAILS_LISTTEXTA
*pDetailsA
= lpmcdA
->paDetails
;
3897 MIXERCONTROLDETAILS_LISTTEXTW
*pDetailsW
;
3898 int size
= max(1, lpmcdA
->cChannels
) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3901 if (lpmcdA
->u
.cMultipleItems
!= 0) {
3902 size
*= lpmcdA
->u
.cMultipleItems
;
3904 pDetailsW
= HeapAlloc(GetProcessHeap(), 0, size
);
3905 lpmcdA
->paDetails
= pDetailsW
;
3906 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3907 /* set up lpmcd->paDetails */
3908 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3909 /* copy from lpmcd->paDetails back to paDetailsW; */
3910 if (ret
== MMSYSERR_NOERROR
) {
3911 for (i
= 0; i
< lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
; i
++) {
3912 pDetailsA
->dwParam1
= pDetailsW
->dwParam1
;
3913 pDetailsA
->dwParam2
= pDetailsW
->dwParam2
;
3914 WideCharToMultiByte( CP_ACP
, 0, pDetailsW
->szName
, -1,
3916 sizeof(pDetailsA
->szName
), NULL
, NULL
);
3920 pDetailsA
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3921 pDetailsW
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3923 HeapFree(GetProcessHeap(), 0, pDetailsW
);
3924 lpmcdA
->paDetails
= pDetailsA
;
3925 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTA
);
3929 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails
);
3935 /**************************************************************************
3936 * mixerGetLineControlsA [WINMM.@]
3938 UINT WINAPI
mixerGetLineControlsA(HMIXEROBJ hmix
, LPMIXERLINECONTROLSA lpmlcA
,
3941 MIXERLINECONTROLSW mlcW
;
3945 TRACE("(%p, %p, %x)\n", hmix
, lpmlcA
, fdwControls
);
3947 if (lpmlcA
== NULL
|| lpmlcA
->cbStruct
!= sizeof(*lpmlcA
) ||
3948 lpmlcA
->cbmxctrl
!= sizeof(MIXERCONTROLA
))
3949 return MMSYSERR_INVALPARAM
;
3951 mlcW
.cbStruct
= sizeof(mlcW
);
3952 mlcW
.dwLineID
= lpmlcA
->dwLineID
;
3953 mlcW
.u
.dwControlID
= lpmlcA
->u
.dwControlID
;
3954 mlcW
.u
.dwControlType
= lpmlcA
->u
.dwControlType
;
3956 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3957 the control count is assumed to be 1 - This is relied upon by a game,
3958 "Dynomite Deluze" */
3959 if (MIXER_GETLINECONTROLSF_ONEBYTYPE
== (fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
)) {
3962 mlcW
.cControls
= lpmlcA
->cControls
;
3964 mlcW
.cbmxctrl
= sizeof(MIXERCONTROLW
);
3965 mlcW
.pamxctrl
= HeapAlloc(GetProcessHeap(), 0,
3966 mlcW
.cControls
* mlcW
.cbmxctrl
);
3968 ret
= mixerGetLineControlsW(hmix
, &mlcW
, fdwControls
);
3970 if (ret
== MMSYSERR_NOERROR
) {
3971 lpmlcA
->dwLineID
= mlcW
.dwLineID
;
3972 lpmlcA
->u
.dwControlID
= mlcW
.u
.dwControlID
;
3973 lpmlcA
->u
.dwControlType
= mlcW
.u
.dwControlType
;
3975 for (i
= 0; i
< mlcW
.cControls
; i
++) {
3976 lpmlcA
->pamxctrl
[i
].cbStruct
= sizeof(MIXERCONTROLA
);
3977 lpmlcA
->pamxctrl
[i
].dwControlID
= mlcW
.pamxctrl
[i
].dwControlID
;
3978 lpmlcA
->pamxctrl
[i
].dwControlType
= mlcW
.pamxctrl
[i
].dwControlType
;
3979 lpmlcA
->pamxctrl
[i
].fdwControl
= mlcW
.pamxctrl
[i
].fdwControl
;
3980 lpmlcA
->pamxctrl
[i
].cMultipleItems
= mlcW
.pamxctrl
[i
].cMultipleItems
;
3981 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szShortName
, -1,
3982 lpmlcA
->pamxctrl
[i
].szShortName
,
3983 sizeof(lpmlcA
->pamxctrl
[i
].szShortName
), NULL
, NULL
);
3984 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szName
, -1,
3985 lpmlcA
->pamxctrl
[i
].szName
,
3986 sizeof(lpmlcA
->pamxctrl
[i
].szName
), NULL
, NULL
);
3987 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3988 * sizeof(mlcW.pamxctrl[i].Bounds) */
3989 memcpy(&lpmlcA
->pamxctrl
[i
].Bounds
, &mlcW
.pamxctrl
[i
].Bounds
,
3990 sizeof(mlcW
.pamxctrl
[i
].Bounds
));
3991 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3992 * sizeof(mlcW.pamxctrl[i].Metrics) */
3993 memcpy(&lpmlcA
->pamxctrl
[i
].Metrics
, &mlcW
.pamxctrl
[i
].Metrics
,
3994 sizeof(mlcW
.pamxctrl
[i
].Metrics
));
3998 HeapFree(GetProcessHeap(), 0, mlcW
.pamxctrl
);
4003 static UINT
WINMM_GetVolumeLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
4004 MIXERCONTROLW
*ctl
, DWORD flags
)
4006 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 0 : 2;
4007 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_VOLUME
;
4008 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
4009 ctl
->cMultipleItems
= 0;
4010 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, ctl
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4011 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, ctl
->szName
, MIXER_LONG_NAME_CHARS
);
4012 ctl
->Bounds
.s1
.dwMinimum
= 0;
4013 ctl
->Bounds
.s1
.dwMaximum
= 0xFFFF;
4014 ctl
->Metrics
.cSteps
= 192;
4016 return MMSYSERR_NOERROR
;
4019 static UINT
WINMM_GetMuteLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
4020 MIXERCONTROLW
*ctl
, DWORD flags
)
4022 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 1 : 3;
4023 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_MUTE
;
4024 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
4025 ctl
->cMultipleItems
= 0;
4026 LoadStringW(hWinMM32Instance
, IDS_MUTE
, ctl
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4027 LoadStringW(hWinMM32Instance
, IDS_MUTE
, ctl
->szName
, MIXER_LONG_NAME_CHARS
);
4028 ctl
->Bounds
.s1
.dwMinimum
= 0;
4029 ctl
->Bounds
.s1
.dwMaximum
= 1;
4030 ctl
->Metrics
.cSteps
= 0;
4032 return MMSYSERR_NOERROR
;
4035 /**************************************************************************
4036 * mixerGetLineControlsW [WINMM.@]
4038 UINT WINAPI
mixerGetLineControlsW(HMIXEROBJ hmix
, LPMIXERLINECONTROLSW lpmlcW
,
4041 WINMM_MMDevice
*mmdevice
;
4044 TRACE("(%p, %p, %08x)\n", hmix
, lpmlcW
, fdwControls
);
4046 hr
= WINMM_InitMMDevices();
4048 return MMSYSERR_NODRIVER
;
4050 if(fdwControls
& ~(MIXER_GETLINECONTROLSF_ALL
|
4051 MIXER_GETLINECONTROLSF_ONEBYID
|
4052 MIXER_GETLINECONTROLSF_ONEBYTYPE
|
4053 MIXER_OBJECTF_HMIXER
|
4054 MIXER_OBJECTF_MIXER
)){
4055 WARN("Unknown GetLineControls flag: %x\n", fdwControls
);
4056 return MMSYSERR_INVALFLAG
;
4059 if(!lpmlcW
|| lpmlcW
->cbStruct
< sizeof(*lpmlcW
) || !lpmlcW
->pamxctrl
)
4060 return MMSYSERR_INVALPARAM
;
4062 TRACE("dwLineID: %u\n", lpmlcW
->dwLineID
);
4063 TRACE("dwControl: %x\n", lpmlcW
->u
.dwControlID
);
4064 TRACE("cControls: %u\n", lpmlcW
->cControls
);
4066 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwControls
, NULL
);
4068 return MMSYSERR_INVALHANDLE
;
4070 switch(fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
){
4071 case MIXER_GETLINECONTROLSF_ALL
:
4072 if(lpmlcW
->cControls
!= 2)
4073 return MMSYSERR_INVALPARAM
;
4074 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4075 return MMSYSERR_INVALPARAM
;
4076 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4077 return MIXERR_INVALLINE
;
4078 WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4079 &lpmlcW
->pamxctrl
[0], fdwControls
);
4080 WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4081 &lpmlcW
->pamxctrl
[1], fdwControls
);
4082 return MMSYSERR_NOERROR
;
4083 case MIXER_GETLINECONTROLSF_ONEBYID
:
4084 if(lpmlcW
->cControls
!= 1)
4085 return MMSYSERR_INVALPARAM
;
4086 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4087 return MMSYSERR_INVALPARAM
;
4088 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4089 return MIXERR_INVALLINE
;
4090 if(lpmlcW
->u
.dwControlID
== 0)
4091 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4092 lpmlcW
->pamxctrl
, fdwControls
);
4093 if(lpmlcW
->u
.dwControlID
== 1)
4094 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4095 lpmlcW
->pamxctrl
, fdwControls
);
4096 return MMSYSERR_NOTSUPPORTED
;
4097 case MIXER_GETLINECONTROLSF_ONEBYTYPE
:
4098 if(lpmlcW
->cControls
!= 1)
4099 return MMSYSERR_INVALPARAM
;
4100 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4101 return MMSYSERR_INVALPARAM
;
4102 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4103 return MIXERR_INVALLINE
;
4104 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_VOLUME
)
4105 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4106 lpmlcW
->pamxctrl
, fdwControls
);
4107 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_MUTE
)
4108 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4109 lpmlcW
->pamxctrl
, fdwControls
);
4110 return MMSYSERR_NOTSUPPORTED
;
4113 return MMSYSERR_NOTSUPPORTED
;
4116 static UINT
WINMM_GetSourceLineInfo(WINMM_MMDevice
*mmdevice
, UINT mmdev_index
,
4117 MIXERLINEW
*info
, DWORD flags
)
4120 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4123 if(info
->dwSource
!= 0)
4124 return MIXERR_INVALLINE
;
4126 info
->dwDestination
= 0;
4128 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
| MIXERLINE_LINEF_SOURCE
;
4129 info
->cConnections
= 0;
4130 info
->cControls
= 2;
4131 /* volume & mute always affect all channels, so claim 1 channel */
4132 info
->cChannels
= 1;
4133 info
->Target
.dwDeviceID
= mmdev_index
;
4134 info
->Target
.wMid
= ~0;
4135 info
->Target
.wPid
= ~0;
4136 info
->Target
.vDriverVersion
= 0;
4138 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, info
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4139 LoadStringW(hWinMM32Instance
, IDS_MASTER_VOLUME
, info
->szName
, MIXER_LONG_NAME_CHARS
);
4142 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
;
4143 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEOUT
;
4144 memcpy(info
->Target
.szPname
, mmdevice
->out_caps
.szPname
,
4145 sizeof(info
->Target
.szPname
));
4147 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
;
4148 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
4149 info
->Target
.szPname
[0] = '\0';
4152 return MMSYSERR_NOERROR
;
4155 static UINT
WINMM_GetDestinationLineInfo(WINMM_MMDevice
*mmdevice
,
4156 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4159 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4162 if(info
->dwDestination
!= 0)
4163 return MIXERR_INVALLINE
;
4165 info
->dwSource
= 0xFFFFFFFF;
4166 info
->dwLineID
= 0xFFFF0000;
4167 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
;
4168 info
->cConnections
= 1;
4169 info
->cControls
= 2;
4171 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, info
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4172 LoadStringW(hWinMM32Instance
, IDS_MASTER_VOLUME
, info
->szName
, MIXER_LONG_NAME_CHARS
);
4174 info
->Target
.dwDeviceID
= mmdev_index
;
4175 info
->Target
.wMid
= ~0;
4176 info
->Target
.wPid
= ~0;
4177 info
->Target
.vDriverVersion
= 0;
4180 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
;
4181 info
->cChannels
= mmdevice
->out_caps
.wChannels
;
4182 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
4183 info
->Target
.szPname
[0] = '\0';
4185 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_WAVEIN
;
4186 info
->cChannels
= mmdevice
->in_caps
.wChannels
;
4187 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEIN
;
4188 memcpy(info
->Target
.szPname
, mmdevice
->in_caps
.szPname
,
4189 sizeof(info
->Target
.szPname
));
4192 return MMSYSERR_NOERROR
;
4195 static UINT
WINMM_GetComponentTypeLineInfo(WINMM_MMDevice
*mmdevice
,
4196 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4199 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4202 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_WAVEIN
){
4204 return MIXERR_INVALLINE
;
4205 info
->dwDestination
= 0;
4206 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4209 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
){
4211 return MIXERR_INVALLINE
;
4212 info
->dwDestination
= 0;
4213 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4216 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
){
4218 return MIXERR_INVALLINE
;
4220 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4223 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
){
4225 return MIXERR_INVALLINE
;
4227 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4230 TRACE("Returning INVALLINE on this component type: %u\n",
4231 info
->dwComponentType
);
4233 return MIXERR_INVALLINE
;
4236 static UINT
WINMM_GetLineIDLineInfo(WINMM_MMDevice
*mmdevice
,
4237 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4239 if(info
->dwLineID
== 0xFFFF0000){
4240 info
->dwDestination
= 0;
4241 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4244 if(info
->dwLineID
== 0){
4246 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4249 TRACE("Returning INVALLINE on this dwLineID: %u\n", info
->dwLineID
);
4250 return MIXERR_INVALLINE
;
4253 /**************************************************************************
4254 * mixerGetLineInfoW [WINMM.@]
4256 UINT WINAPI
mixerGetLineInfoW(HMIXEROBJ hmix
, LPMIXERLINEW lpmliW
, DWORD fdwInfo
)
4259 WINMM_MMDevice
*mmdevice
;
4262 TRACE("(%p, %p, %x)\n", hmix
, lpmliW
, fdwInfo
);
4264 hr
= WINMM_InitMMDevices();
4266 return MMSYSERR_NODRIVER
;
4268 if(!lpmliW
|| lpmliW
->cbStruct
< sizeof(MIXERLINEW
))
4269 return MMSYSERR_INVALPARAM
;
4271 TRACE("dwDestination: %u\n", lpmliW
->dwDestination
);
4272 TRACE("dwSource: %u\n", lpmliW
->dwSource
);
4273 TRACE("dwLineID: %u\n", lpmliW
->dwLineID
);
4274 TRACE("fdwLine: 0x%x\n", lpmliW
->fdwLine
);
4275 TRACE("dwComponentType: 0x%x\n", lpmliW
->dwComponentType
);
4277 if(fdwInfo
& ~(MIXER_GETLINEINFOF_COMPONENTTYPE
|
4278 MIXER_GETLINEINFOF_DESTINATION
|
4279 MIXER_GETLINEINFOF_LINEID
|
4280 MIXER_GETLINEINFOF_SOURCE
|
4281 MIXER_GETLINEINFOF_TARGETTYPE
|
4282 MIXER_OBJECTF_HMIXER
|
4283 MIXER_OBJECTF_MIXER
)){
4284 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo
);
4285 return MMSYSERR_INVALFLAG
;
4288 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwInfo
, &mmdev_index
);
4290 return MMSYSERR_INVALHANDLE
;
4294 switch(fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
){
4295 case MIXER_GETLINEINFOF_DESTINATION
:
4296 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, lpmliW
,
4298 case MIXER_GETLINEINFOF_SOURCE
:
4299 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
4300 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
4301 return WINMM_GetComponentTypeLineInfo(mmdevice
, mmdev_index
, lpmliW
,
4303 case MIXER_GETLINEINFOF_LINEID
:
4304 return WINMM_GetLineIDLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
4305 case MIXER_GETLINEINFOF_TARGETTYPE
:
4306 FIXME("TARGETTYPE flag not implemented!\n");
4307 return MIXERR_INVALLINE
;
4310 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo
);
4311 return MMSYSERR_INVALFLAG
;
4314 /**************************************************************************
4315 * mixerGetLineInfoA [WINMM.@]
4317 UINT WINAPI
mixerGetLineInfoA(HMIXEROBJ hmix
, LPMIXERLINEA lpmliA
,
4323 TRACE("(%p, %p, %x)\n", hmix
, lpmliA
, fdwInfo
);
4325 if (lpmliA
== NULL
|| lpmliA
->cbStruct
!= sizeof(*lpmliA
))
4326 return MMSYSERR_INVALPARAM
;
4328 mliW
.cbStruct
= sizeof(mliW
);
4329 switch (fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
) {
4330 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
4331 mliW
.dwComponentType
= lpmliA
->dwComponentType
;
4333 case MIXER_GETLINEINFOF_DESTINATION
:
4334 mliW
.dwDestination
= lpmliA
->dwDestination
;
4336 case MIXER_GETLINEINFOF_LINEID
:
4337 mliW
.dwLineID
= lpmliA
->dwLineID
;
4339 case MIXER_GETLINEINFOF_SOURCE
:
4340 mliW
.dwDestination
= lpmliA
->dwDestination
;
4341 mliW
.dwSource
= lpmliA
->dwSource
;
4343 case MIXER_GETLINEINFOF_TARGETTYPE
:
4344 mliW
.Target
.dwType
= lpmliA
->Target
.dwType
;
4345 mliW
.Target
.wMid
= lpmliA
->Target
.wMid
;
4346 mliW
.Target
.wPid
= lpmliA
->Target
.wPid
;
4347 mliW
.Target
.vDriverVersion
= lpmliA
->Target
.vDriverVersion
;
4348 MultiByteToWideChar( CP_ACP
, 0, lpmliA
->Target
.szPname
, -1, mliW
.Target
.szPname
, sizeof(mliW
.Target
.szPname
)/sizeof(WCHAR
));
4351 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo
);
4352 return MMSYSERR_INVALFLAG
;
4355 ret
= mixerGetLineInfoW(hmix
, &mliW
, fdwInfo
);
4357 if(ret
== MMSYSERR_NOERROR
)
4359 lpmliA
->dwDestination
= mliW
.dwDestination
;
4360 lpmliA
->dwSource
= mliW
.dwSource
;
4361 lpmliA
->dwLineID
= mliW
.dwLineID
;
4362 lpmliA
->fdwLine
= mliW
.fdwLine
;
4363 lpmliA
->dwUser
= mliW
.dwUser
;
4364 lpmliA
->dwComponentType
= mliW
.dwComponentType
;
4365 lpmliA
->cChannels
= mliW
.cChannels
;
4366 lpmliA
->cConnections
= mliW
.cConnections
;
4367 lpmliA
->cControls
= mliW
.cControls
;
4368 WideCharToMultiByte( CP_ACP
, 0, mliW
.szShortName
, -1, lpmliA
->szShortName
,
4369 sizeof(lpmliA
->szShortName
), NULL
, NULL
);
4370 WideCharToMultiByte( CP_ACP
, 0, mliW
.szName
, -1, lpmliA
->szName
,
4371 sizeof(lpmliA
->szName
), NULL
, NULL
);
4372 lpmliA
->Target
.dwType
= mliW
.Target
.dwType
;
4373 lpmliA
->Target
.dwDeviceID
= mliW
.Target
.dwDeviceID
;
4374 lpmliA
->Target
.wMid
= mliW
.Target
.wMid
;
4375 lpmliA
->Target
.wPid
= mliW
.Target
.wPid
;
4376 lpmliA
->Target
.vDriverVersion
= mliW
.Target
.vDriverVersion
;
4377 WideCharToMultiByte( CP_ACP
, 0, mliW
.Target
.szPname
, -1, lpmliA
->Target
.szPname
,
4378 sizeof(lpmliA
->Target
.szPname
), NULL
, NULL
);
4383 /**************************************************************************
4384 * mixerSetControlDetails [WINMM.@]
4386 UINT WINAPI
mixerSetControlDetails(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcd
,
4389 WINMM_ControlDetails details
;
4392 TRACE("(%p, %p, %x)\n", hmix
, lpmcd
, fdwDetails
);
4394 if((fdwDetails
& MIXER_SETCONTROLDETAILSF_QUERYMASK
) ==
4395 MIXER_SETCONTROLDETAILSF_CUSTOM
)
4396 return MMSYSERR_NOTSUPPORTED
;
4399 return MMSYSERR_INVALPARAM
;
4401 if(!WINMM_StartDevicesThread())
4402 return MMSYSERR_NODRIVER
;
4404 TRACE("dwControlID: %u\n", lpmcd
->dwControlID
);
4406 details
.hmix
= hmix
;
4407 details
.details
= lpmcd
;
4408 details
.flags
= fdwDetails
;
4410 ret
= SendMessageW(g_devices_hwnd
, MXDM_SETCONTROLDETAILS
,
4411 (DWORD_PTR
)&details
, 0);
4412 InterlockedDecrement(&g_devthread_token
);
4416 /**************************************************************************
4417 * mixerMessage [WINMM.@]
4419 DWORD WINAPI
mixerMessage(HMIXER hmix
, UINT uMsg
, DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
4421 TRACE("(%p, %d, %lx, %lx)\n", hmix
, uMsg
, dwParam1
, dwParam2
);
4423 return MMSYSERR_NOTSUPPORTED
;