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
);
194 static void WOD_PushData(WINMM_Device
*device
);
196 void WINMM_DeleteWaveform(void)
201 CloseHandle(g_devices_thread
);
203 for(i
= 0; i
< g_outmmdevices_count
; ++i
){
204 WINMM_MMDevice
*mmdevice
= &g_out_mmdevices
[i
];
206 for(j
= 0; j
< MAX_DEVICES
&& mmdevice
->devices
[j
]; ++j
){
207 WINMM_Device
*device
= mmdevice
->devices
[j
];
209 CloseHandle(device
->handle
);
210 device
->lock
.DebugInfo
->Spare
[0] = 0;
211 DeleteCriticalSection(&device
->lock
);
215 ISimpleAudioVolume_Release(mmdevice
->volume
);
216 CoTaskMemFree(mmdevice
->dev_id
);
217 mmdevice
->lock
.DebugInfo
->Spare
[0] = 0;
218 DeleteCriticalSection(&mmdevice
->lock
);
221 for(i
= 0; i
< g_inmmdevices_count
; ++i
){
222 WINMM_MMDevice
*mmdevice
= &g_in_mmdevices
[i
];
224 for(j
= 0; j
< MAX_DEVICES
&& mmdevice
->devices
[j
]; ++j
){
225 WINMM_Device
*device
= mmdevice
->devices
[j
];
227 CloseHandle(device
->handle
);
228 device
->lock
.DebugInfo
->Spare
[0] = 0;
229 DeleteCriticalSection(&device
->lock
);
233 ISimpleAudioVolume_Release(mmdevice
->volume
);
234 CoTaskMemFree(mmdevice
->dev_id
);
235 mmdevice
->lock
.DebugInfo
->Spare
[0] = 0;
236 DeleteCriticalSection(&mmdevice
->lock
);
239 HeapFree(GetProcessHeap(), 0, g_out_mmdevices
);
240 HeapFree(GetProcessHeap(), 0, g_in_mmdevices
);
242 HeapFree(GetProcessHeap(), 0, g_device_handles
);
243 HeapFree(GetProcessHeap(), 0, g_handle_devices
);
245 DeleteCriticalSection(&g_devthread_lock
);
248 static inline HWAVE
WINMM_MakeHWAVE(UINT mmdevice
, BOOL is_out
, UINT device
)
250 return ULongToHandle((1 << 15) | ((!!is_out
) << 14) |
251 (mmdevice
<< 8) | device
);
254 static inline void WINMM_DecomposeHWAVE(HWAVE hwave
, UINT
*mmdevice_index
,
255 BOOL
*is_out
, UINT
*device_index
, UINT
*junk
)
257 ULONG32 l
= HandleToULong(hwave
);
258 *device_index
= l
& 0xFF;
259 *mmdevice_index
= (l
>> 8) & 0x3F;
260 *is_out
= (l
>> 14) & 0x1;
264 static void WINMM_InitDevice(WINMM_Device
*device
)
266 InitializeCriticalSection(&device
->lock
);
267 device
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
270 static inline WINMM_MMDevice
*read_map(WINMM_MMDevice
**map
, UINT index
)
273 EnterCriticalSection(&g_devthread_lock
);
275 LeaveCriticalSection(&g_devthread_lock
);
279 /* finds the first unused Device, marks it as "open", and returns
280 * a pointer to the device
282 * IMPORTANT: it is the caller's responsibility to release the device's lock
285 static WINMM_Device
*WINMM_FindUnusedDevice(WINMM_Device
**devices
,
286 WINMM_MMDevice
*parent
, UINT internal_index
, BOOL is_out
)
290 for(i
= 0; i
< MAX_DEVICES
; ++i
){
291 WINMM_Device
*device
= devices
[i
];
294 device
= devices
[i
] = HeapAlloc(GetProcessHeap(),
295 HEAP_ZERO_MEMORY
, sizeof(WINMM_Device
));
299 WINMM_InitDevice(device
);
300 EnterCriticalSection(&device
->lock
);
302 EnterCriticalSection(&device
->lock
);
305 device
->handle
= WINMM_MakeHWAVE(internal_index
, is_out
, i
);
306 device
->parent
= parent
;
312 LeaveCriticalSection(&device
->lock
);
315 TRACE("All devices in use: mmdevice: %u\n", internal_index
);
320 static inline BOOL
WINMM_ValidateAndLock(WINMM_Device
*device
)
325 EnterCriticalSection(&device
->lock
);
328 LeaveCriticalSection(&device
->lock
);
335 static WINMM_Device
*WINMM_GetDeviceFromHWAVE(HWAVE hwave
)
337 WINMM_MMDevice
*mmdevice
;
338 WINMM_Device
*device
;
339 UINT mmdevice_index
, device_index
, junk
;
342 WINMM_DecomposeHWAVE(hwave
, &mmdevice_index
, &is_out
, &device_index
, &junk
);
347 if(mmdevice_index
== MAPPER_INDEX
){
348 EnterCriticalSection(&g_devthread_lock
);
350 device
= g_out_mapper_devices
[device_index
];
352 device
= g_in_mapper_devices
[device_index
];
353 LeaveCriticalSection(&g_devthread_lock
);
357 if(mmdevice_index
>= (is_out
? g_outmmdevices_count
: g_inmmdevices_count
))
361 mmdevice
= &g_out_mmdevices
[mmdevice_index
];
363 mmdevice
= &g_in_mmdevices
[mmdevice_index
];
365 EnterCriticalSection(&mmdevice
->lock
);
367 device
= mmdevice
->devices
[device_index
];
369 LeaveCriticalSection(&mmdevice
->lock
);
374 /* Note: NotifyClient should never be called while holding the device lock
375 * since the client may call wave* functions from within the callback. */
376 static inline void WINMM_NotifyClient(WINMM_CBInfo
*info
, WORD msg
, DWORD_PTR param1
,
379 DriverCallback(info
->callback
, info
->flags
, (HDRVR
)info
->hwave
,
380 msg
, info
->user
, param1
, param2
);
383 static MMRESULT
hr2mmr(HRESULT hr
)
387 case AUDCLNT_E_NOT_STOPPED
:
388 return MMSYSERR_NOERROR
;
389 case AUDCLNT_E_UNSUPPORTED_FORMAT
:
390 return WAVERR_BADFORMAT
;
391 case AUDCLNT_E_DEVICE_IN_USE
:
392 return MMSYSERR_ALLOCATED
;
393 case AUDCLNT_E_ENDPOINT_CREATE_FAILED
:
394 return MMSYSERR_NOTENABLED
;
396 return MMSYSERR_NOMEM
;
399 return MMSYSERR_INVALPARAM
;
400 case AUDCLNT_E_DEVICE_INVALIDATED
: /* DSERR_BUFFERLOST */
402 return FAILED(hr
) ? MMSYSERR_ERROR
: MMSYSERR_NOERROR
;
406 static HRESULT
WINMM_GetFriendlyName(IMMDevice
*device
, WCHAR
*out
,
413 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
417 PropVariantInit(&var
);
419 hr
= IPropertyStore_GetValue(ps
,
420 (PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &var
);
422 IPropertyStore_Release(ps
);
426 lstrcpynW(out
, var
.pwszVal
, outlen
);
428 PropVariantClear(&var
);
430 IPropertyStore_Release(ps
);
435 static HRESULT
WINMM_TestFormat(IAudioClient
*client
, DWORD rate
, DWORD depth
,
438 WAVEFORMATEX fmt
, *junk
;
441 fmt
.wFormatTag
= WAVE_FORMAT_PCM
;
442 fmt
.nChannels
= channels
;
443 fmt
.nSamplesPerSec
= rate
;
444 fmt
.wBitsPerSample
= depth
;
445 fmt
.nBlockAlign
= (channels
* depth
) / 8;
446 fmt
.nAvgBytesPerSec
= rate
* fmt
.nBlockAlign
;
449 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
,
457 static struct _TestFormat
{
462 } formats_to_test
[] = {
463 { WAVE_FORMAT_1M08
, 11025, 8, 1 },
464 { WAVE_FORMAT_1M16
, 11025, 16, 1 },
465 { WAVE_FORMAT_1S08
, 11025, 8, 2 },
466 { WAVE_FORMAT_1S16
, 11025, 16, 2 },
467 { WAVE_FORMAT_2M08
, 22050, 8, 1 },
468 { WAVE_FORMAT_2M16
, 22050, 16, 1 },
469 { WAVE_FORMAT_2S08
, 22050, 8, 2 },
470 { WAVE_FORMAT_2S16
, 22050, 16, 2 },
471 { WAVE_FORMAT_4M08
, 44100, 8, 1 },
472 { WAVE_FORMAT_4M16
, 44100, 16, 1 },
473 { WAVE_FORMAT_4S08
, 44100, 8, 2 },
474 { WAVE_FORMAT_4S16
, 44100, 16, 2 },
475 { WAVE_FORMAT_48M08
, 48000, 8, 1 },
476 { WAVE_FORMAT_48M16
, 48000, 16, 1 },
477 { WAVE_FORMAT_48S08
, 48000, 8, 2 },
478 { WAVE_FORMAT_48S16
, 48000, 16, 2 },
479 { WAVE_FORMAT_96M08
, 96000, 8, 1 },
480 { WAVE_FORMAT_96M16
, 96000, 16, 1 },
481 { WAVE_FORMAT_96S08
, 96000, 8, 2 },
482 { WAVE_FORMAT_96S16
, 96000, 16, 2 },
486 static DWORD
WINMM_GetSupportedFormats(IMMDevice
*device
)
490 struct _TestFormat
*fmt
;
491 IAudioClient
*client
;
493 hr
= IMMDevice_Activate(device
, &IID_IAudioClient
,
494 CLSCTX_INPROC_SERVER
, NULL
, (void**)&client
);
498 for(fmt
= formats_to_test
; fmt
->flag
; ++fmt
){
499 hr
= WINMM_TestFormat(client
, fmt
->rate
, fmt
->depth
, fmt
->channels
);
504 IAudioClient_Release(client
);
509 static HRESULT
WINMM_InitMMDevice(EDataFlow flow
, IMMDevice
*device
,
510 WINMM_MMDevice
*dev
, UINT index
)
514 dev
->dataflow
= flow
;
516 dev
->out_caps
.wMid
= 0xFF;
517 dev
->out_caps
.wPid
= 0xFF;
518 dev
->out_caps
.vDriverVersion
= 0x00010001;
519 dev
->out_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
520 dev
->out_caps
.wReserved1
= 0;
521 dev
->out_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
522 WAVECAPS_SAMPLEACCURATE
;
523 dev
->out_caps
.wChannels
= 2;
524 dev
->out_caps
.szPname
[0] = '\0';
526 hr
= WINMM_GetFriendlyName(device
, dev
->out_caps
.szPname
,
527 sizeof(dev
->out_caps
.szPname
) /
528 sizeof(*dev
->out_caps
.szPname
));
532 dev
->in_caps
.wMid
= 0xFF;
533 dev
->in_caps
.wPid
= 0xFF;
534 dev
->in_caps
.vDriverVersion
= 0x00010001;
535 dev
->in_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
536 dev
->in_caps
.wReserved1
= 0;
537 dev
->in_caps
.wChannels
= 2;
538 dev
->in_caps
.szPname
[0] = '\0';
540 hr
= WINMM_GetFriendlyName(device
, dev
->in_caps
.szPname
,
541 sizeof(dev
->in_caps
.szPname
) /
542 sizeof(*dev
->in_caps
.szPname
));
547 hr
= IMMDevice_GetId(device
, &dev
->dev_id
);
551 CoCreateGuid(&dev
->session
);
555 InitializeCriticalSection(&dev
->lock
);
556 dev
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
561 static HRESULT
WINMM_EnumDevices(WINMM_MMDevice
**devices
,
562 WINMM_MMDevice
***map
, UINT
*devcount
, EDataFlow flow
,
563 IMMDeviceEnumerator
*devenum
)
565 IMMDeviceCollection
*devcoll
;
568 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flow
,
569 DEVICE_STATE_ACTIVE
, &devcoll
);
573 hr
= IMMDeviceCollection_GetCount(devcoll
, devcount
);
575 IMMDeviceCollection_Release(devcoll
);
581 IMMDevice
*def_dev
= NULL
;
583 *devices
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
584 sizeof(WINMM_MMDevice
) * (*devcount
));
586 IMMDeviceCollection_Release(devcoll
);
587 return E_OUTOFMEMORY
;
590 *map
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
591 sizeof(WINMM_MMDevice
*) * (*devcount
));
593 IMMDeviceCollection_Release(devcoll
);
594 HeapFree(GetProcessHeap(), 0, *devices
);
595 return E_OUTOFMEMORY
;
598 /* make sure that device 0 is the default device */
599 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
,
600 flow
, eConsole
, &def_dev
);
602 for(n
= 0; n
< *devcount
; ++n
){
605 hr
= IMMDeviceCollection_Item(devcoll
, n
, &device
);
607 WINMM_InitMMDevice(flow
, device
, &(*devices
)[n
], n
);
609 if(device
== def_dev
)
610 (*map
)[0] = &(*devices
)[n
];
612 (*map
)[count
] = &(*devices
)[n
];
616 IMMDevice_Release(device
);
620 IMMDevice_Release(def_dev
);
625 IMMDeviceCollection_Release(devcoll
);
630 static HRESULT WINAPI
notif_QueryInterface(IMMNotificationClient
*iface
,
631 const GUID
*riid
, void **obj
)
633 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid
));
634 return E_NOINTERFACE
;
637 static ULONG WINAPI
notif_AddRef(IMMNotificationClient
*iface
)
642 static ULONG WINAPI
notif_Release(IMMNotificationClient
*iface
)
647 static HRESULT WINAPI
notif_OnDeviceStateChanged(IMMNotificationClient
*iface
,
648 const WCHAR
*device_id
, DWORD new_state
)
650 TRACE("Ignoring OnDeviceStateChanged callback\n");
654 static HRESULT WINAPI
notif_OnDeviceAdded(IMMNotificationClient
*iface
,
655 const WCHAR
*device_id
)
657 TRACE("Ignoring OnDeviceAdded callback\n");
661 static HRESULT WINAPI
notif_OnDeviceRemoved(IMMNotificationClient
*iface
,
662 const WCHAR
*device_id
)
664 TRACE("Ignoring OnDeviceRemoved callback\n");
668 static HRESULT
update_mapping(WINMM_MMDevice
***map
, UINT count
,
669 const WCHAR
*default_id
)
671 WINMM_MMDevice
*prev
;
675 for(i
= 0; i
< count
; ++i
){
678 if(!wcscmp((*map
)[i
]->dev_id
, default_id
)){
679 (*map
)[0] = (*map
)[i
];
690 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
696 static HRESULT
reroute_mapper_device(WINMM_Device
*device
, BOOL is_out
)
702 UINT64 clock_freq
, clock_pos
;
704 TRACE("rerouting device %p\n", device
->handle
);
706 EnterCriticalSection(&device
->lock
);
708 if(!device
->open
|| device
->acm_handle
){
709 /* Windows 7 doesn't re-route ACM devices, so we don't either.
710 * Seems to be because of the data waveXxxPrepareHeader allocates. */
711 LeaveCriticalSection(&device
->lock
);
715 stopped
= device
->stopped
;
718 info
.req_device
= WAVE_MAPPER
;
719 info
.format
= device
->orig_fmt
;
720 info
.callback
= device
->cb_info
.callback
;
721 info
.cb_user
= device
->cb_info
.user
;
722 /* We have to use direct here so that we don't suddenly introduce ACM
723 * into a playing stream that hasn't been Prepared for it */
724 info
.flags
= (device
->cb_info
.flags
<< 16) | WAVE_FORMAT_DIRECT_QUERY
;
728 mr
= WOD_Open(&info
);
730 mr
= WID_Open(&info
);
732 if(mr
!= MMSYSERR_NOERROR
){
733 TRACE("New default device doesn't support this stream: %p\n", device
->handle
);
734 LeaveCriticalSection(&device
->lock
);
738 hr
= IAudioClient_Stop(device
->client
);
740 WARN("Stop failed: %08lx\n", hr
);
742 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
744 WARN("GetFrequency failed: %08lx\n", hr
);
745 LeaveCriticalSection(&device
->lock
);
749 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
751 WARN("GetPosition failed: %08lx\n", hr
);
752 LeaveCriticalSection(&device
->lock
);
756 device
->remainder_frames
= MulDiv(clock_pos
, device
->samples_per_sec
, clock_freq
) - device
->last_clock_pos
;
758 info
.handle
= device
->handle
;
759 info
.flags
= (device
->cb_info
.flags
<< 16) | WAVE_FORMAT_DIRECT
;
762 WOD_Close((HWAVEOUT
)device
->handle
);
763 device
->parent
= read_map(g_out_map
, 0);
764 mr
= WOD_Open(&info
);
766 WID_Close((HWAVEIN
)device
->handle
);
767 device
->parent
= read_map(g_in_map
, 0);
768 mr
= WID_Open(&info
);
771 if(mr
!= MMSYSERR_NOERROR
){
772 ERR("Opening new default device failed! %u\n", mr
);
773 LeaveCriticalSection(&device
->lock
);
777 HeapFree(GetProcessHeap(), 0, info
.format
);
781 WOD_PushData(device
);
782 WINMM_BeginPlaying(device
);
785 LeaveCriticalSection(&device
->lock
);
790 static HRESULT WINAPI
notif_OnDefaultDeviceChanged(IMMNotificationClient
*iface
,
791 EDataFlow flow
, ERole role
, const WCHAR
*device_id
)
795 TRACE("%u %u %s\n", flow
, role
, wine_dbgstr_w(device_id
));
800 EnterCriticalSection(&g_devthread_lock
);
803 update_mapping(&g_out_map
, g_outmmdevices_count
, device_id
);
805 update_mapping(&g_in_map
, g_inmmdevices_count
, device_id
);
807 for(i
= 0; i
< MAX_DEVICES
&& g_out_mapper_devices
[i
]; ++i
)
808 reroute_mapper_device(g_out_mapper_devices
[i
], TRUE
);
810 for(i
= 0; i
< MAX_DEVICES
&& g_in_mapper_devices
[i
]; ++i
)
811 reroute_mapper_device(g_in_mapper_devices
[i
], FALSE
);
813 LeaveCriticalSection(&g_devthread_lock
);
818 static HRESULT WINAPI
notif_OnPropertyValueChanged(IMMNotificationClient
*iface
,
819 const WCHAR
*device_id
, const PROPERTYKEY key
)
821 TRACE("Ignoring OnPropertyValueChanged callback\n");
825 static IMMNotificationClientVtbl g_notif_vtbl
= {
826 notif_QueryInterface
,
829 notif_OnDeviceStateChanged
,
831 notif_OnDeviceRemoved
,
832 notif_OnDefaultDeviceChanged
,
833 notif_OnPropertyValueChanged
836 static IMMNotificationClient g_notif
= { &g_notif_vtbl
};
838 static HRESULT
WINMM_InitMMDevices(void)
841 IMMDeviceEnumerator
*devenum
= NULL
;
843 if(g_outmmdevices_count
|| g_inmmdevices_count
)
846 init_hr
= CoInitialize(NULL
);
848 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
849 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
853 hr
= IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum
, &g_notif
);
855 WARN("RegisterEndpointNotificationCallback failed: %08lx\n", hr
);
857 hr
= WINMM_EnumDevices(&g_out_mmdevices
, &g_out_map
, &g_outmmdevices_count
,
860 g_outmmdevices_count
= 0;
861 g_inmmdevices_count
= 0;
865 hr
= WINMM_EnumDevices(&g_in_mmdevices
, &g_in_map
, &g_inmmdevices_count
,
868 g_inmmdevices_count
= 0;
874 IMMDeviceEnumerator_Release(devenum
);
875 if(SUCCEEDED(init_hr
))
881 static inline BOOL
WINMM_IsMapper(UINT device
)
883 return (device
== WAVE_MAPPER
|| device
== (UINT16
)WAVE_MAPPER
);
886 static MMRESULT
WINMM_TryDeviceMapping(WINMM_Device
*device
, WAVEFORMATEX
*fmt
,
887 WORD channels
, DWORD freq
, DWORD bits_per_samp
, BOOL is_query
, BOOL is_out
)
889 WAVEFORMATEX target
, *closer_fmt
= NULL
;
893 TRACE("format: %u, channels: %u, sample rate: %lu, bit depth: %lu\n",
894 WAVE_FORMAT_PCM
, channels
, freq
, bits_per_samp
);
896 target
.wFormatTag
= WAVE_FORMAT_PCM
;
897 target
.nChannels
= channels
;
898 target
.nSamplesPerSec
= freq
;
899 target
.wBitsPerSample
= bits_per_samp
;
900 target
.nBlockAlign
= (target
.nChannels
* target
.wBitsPerSample
) / 8;
901 target
.nAvgBytesPerSec
= target
.nSamplesPerSec
* target
.nBlockAlign
;
904 hr
= IAudioClient_IsFormatSupported(device
->client
,
905 AUDCLNT_SHAREMODE_SHARED
, &target
, &closer_fmt
);
906 CoTaskMemFree(closer_fmt
);
908 return WAVERR_BADFORMAT
;
910 /* device supports our target format, so see if MSACM can
911 * do the conversion */
913 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, fmt
, &target
, NULL
,
916 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, &target
, fmt
, NULL
,
918 if(mr
!= MMSYSERR_NOERROR
)
921 /* yes it can. initialize the audioclient and return success */
923 acmStreamClose(device
->acm_handle
, 0);
924 device
->acm_handle
= NULL
;
925 return MMSYSERR_NOERROR
;
928 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
929 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
930 AC_BUFLEN
, 0, &target
, &device
->parent
->session
);
932 WARN("Initialize failed: %08lx\n", hr
);
933 acmStreamClose(device
->acm_handle
, 0);
934 device
->acm_handle
= NULL
;
935 return MMSYSERR_ERROR
;
938 device
->bytes_per_frame
= target
.nBlockAlign
;
939 device
->samples_per_sec
= target
.nSamplesPerSec
;
943 return MMSYSERR_NOERROR
;
946 static MMRESULT
WINMM_MapDevice(WINMM_Device
*device
, BOOL is_query
, BOOL is_out
)
949 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)device
->orig_fmt
;
951 TRACE("(%p, %u)\n", device
, is_out
);
953 /* set up the ACM stream */
954 if(device
->orig_fmt
->wFormatTag
!= WAVE_FORMAT_PCM
&&
955 !(device
->orig_fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
956 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
957 /* convert to PCM format if it's not already */
958 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
,
959 device
->orig_fmt
->nChannels
, device
->orig_fmt
->nSamplesPerSec
,
960 16, is_query
, is_out
);
961 if(mr
== MMSYSERR_NOERROR
)
964 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
,
965 device
->orig_fmt
->nChannels
, device
->orig_fmt
->nSamplesPerSec
,
966 8, is_query
, is_out
);
967 if(mr
== MMSYSERR_NOERROR
)
972 /* first try just changing bit depth and channels */
973 channels
= device
->orig_fmt
->nChannels
;
974 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
975 device
->orig_fmt
->nSamplesPerSec
, 16, is_query
, is_out
);
976 if(mr
== MMSYSERR_NOERROR
)
978 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
979 device
->orig_fmt
->nSamplesPerSec
, 8, is_query
, is_out
);
980 if(mr
== MMSYSERR_NOERROR
)
983 channels
= (channels
== 2) ? 1 : 2;
984 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
985 device
->orig_fmt
->nSamplesPerSec
, 16, is_query
, is_out
);
986 if(mr
== MMSYSERR_NOERROR
)
988 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
989 device
->orig_fmt
->nSamplesPerSec
, 8, is_query
, is_out
);
990 if(mr
== MMSYSERR_NOERROR
)
993 /* that didn't work, so now try different sample rates */
994 channels
= device
->orig_fmt
->nChannels
;
995 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 16, is_query
, is_out
);
996 if(mr
== MMSYSERR_NOERROR
)
998 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 16, is_query
, is_out
);
999 if(mr
== MMSYSERR_NOERROR
)
1001 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 16, is_query
, is_out
);
1002 if(mr
== MMSYSERR_NOERROR
)
1004 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 16, is_query
, is_out
);
1005 if(mr
== MMSYSERR_NOERROR
)
1007 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 16, is_query
, is_out
);
1008 if(mr
== MMSYSERR_NOERROR
)
1011 channels
= (channels
== 2) ? 1 : 2;
1012 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 16, is_query
, is_out
);
1013 if(mr
== MMSYSERR_NOERROR
)
1015 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 16, is_query
, is_out
);
1016 if(mr
== MMSYSERR_NOERROR
)
1018 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 16, is_query
, is_out
);
1019 if(mr
== MMSYSERR_NOERROR
)
1021 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 16, is_query
, is_out
);
1022 if(mr
== MMSYSERR_NOERROR
)
1024 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 16, is_query
, is_out
);
1025 if(mr
== MMSYSERR_NOERROR
)
1028 channels
= device
->orig_fmt
->nChannels
;
1029 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 8, is_query
, is_out
);
1030 if(mr
== MMSYSERR_NOERROR
)
1032 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 8, is_query
, is_out
);
1033 if(mr
== MMSYSERR_NOERROR
)
1035 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 8, is_query
, is_out
);
1036 if(mr
== MMSYSERR_NOERROR
)
1038 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 8, is_query
, is_out
);
1039 if(mr
== MMSYSERR_NOERROR
)
1041 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 8, is_query
, is_out
);
1042 if(mr
== MMSYSERR_NOERROR
)
1045 channels
= (channels
== 2) ? 1 : 2;
1046 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 8, is_query
, is_out
);
1047 if(mr
== MMSYSERR_NOERROR
)
1049 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 8, is_query
, is_out
);
1050 if(mr
== MMSYSERR_NOERROR
)
1052 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 8, is_query
, is_out
);
1053 if(mr
== MMSYSERR_NOERROR
)
1055 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 8, is_query
, is_out
);
1056 if(mr
== MMSYSERR_NOERROR
)
1058 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 8, is_query
, is_out
);
1059 if(mr
== MMSYSERR_NOERROR
)
1063 WARN("Unable to find compatible device!\n");
1064 return WAVERR_BADFORMAT
;
1067 static LRESULT
WINMM_OpenDevice(WINMM_Device
*device
, WINMM_OpenInfo
*info
,
1070 LRESULT ret
= MMSYSERR_NOMEM
;
1073 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, device
->parent
->dev_id
,
1076 WARN("Device %s (%s) unavailable: %08lx\n",
1077 wine_dbgstr_w(device
->parent
->dev_id
),
1078 wine_dbgstr_w(device
->parent
->out_caps
.szPname
), hr
);
1079 ret
= MMSYSERR_NODRIVER
;
1083 /* this is where winexyz.drv opens the audio device */
1084 hr
= IMMDevice_Activate(device
->device
, &IID_IAudioClient
,
1085 CLSCTX_INPROC_SERVER
, NULL
, (void**)&device
->client
);
1087 WARN("Activate failed: %08lx\n", hr
);
1089 if(ret
== MMSYSERR_ERROR
)
1090 ret
= MMSYSERR_NOTENABLED
;
1094 if(info
->format
->wFormatTag
== WAVE_FORMAT_PCM
){
1096 if (info
->format
->nSamplesPerSec
== 0)
1098 ret
= MMSYSERR_INVALPARAM
;
1102 /* we aren't guaranteed that the struct in lpFormat is a full
1103 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1104 device
->orig_fmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX
));
1105 memcpy(device
->orig_fmt
, info
->format
, sizeof(PCMWAVEFORMAT
));
1106 device
->orig_fmt
->cbSize
= 0;
1107 if(device
->orig_fmt
->wBitsPerSample
% 8 != 0){
1108 WARN("Fixing bad wBitsPerSample (%u)\n", device
->orig_fmt
->wBitsPerSample
);
1109 device
->orig_fmt
->wBitsPerSample
= (device
->orig_fmt
->wBitsPerSample
+ 7) & ~7;
1111 /* winmm ignores broken blockalign and avgbytes */
1112 if(device
->orig_fmt
->nBlockAlign
!= device
->orig_fmt
->nChannels
* device
->orig_fmt
->wBitsPerSample
/8){
1113 WARN("Fixing bad nBlockAlign (%u)\n", device
->orig_fmt
->nBlockAlign
);
1114 device
->orig_fmt
->nBlockAlign
= device
->orig_fmt
->nChannels
* device
->orig_fmt
->wBitsPerSample
/8;
1116 if (device
->orig_fmt
->nAvgBytesPerSec
!= device
->orig_fmt
->nSamplesPerSec
* device
->orig_fmt
->nBlockAlign
) {
1117 WARN("Fixing bad nAvgBytesPerSec (%lu)\n", device
->orig_fmt
->nAvgBytesPerSec
);
1118 device
->orig_fmt
->nAvgBytesPerSec
= device
->orig_fmt
->nSamplesPerSec
* device
->orig_fmt
->nBlockAlign
;
1121 device
->orig_fmt
= HeapAlloc(GetProcessHeap(), 0,
1122 sizeof(WAVEFORMATEX
) + info
->format
->cbSize
);
1123 memcpy(device
->orig_fmt
, info
->format
,
1124 sizeof(WAVEFORMATEX
) + info
->format
->cbSize
);
1127 if(info
->flags
& WAVE_FORMAT_QUERY
){
1128 WAVEFORMATEX
*closer_fmt
= NULL
;
1130 hr
= IAudioClient_IsFormatSupported(device
->client
,
1131 AUDCLNT_SHAREMODE_SHARED
, device
->orig_fmt
, &closer_fmt
);
1132 CoTaskMemFree(closer_fmt
);
1133 if((hr
== S_FALSE
|| hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
) && !(info
->flags
& WAVE_FORMAT_DIRECT
))
1134 ret
= WINMM_MapDevice(device
, TRUE
, is_out
);
1136 ret
= hr
== S_FALSE
? WAVERR_BADFORMAT
: hr2mmr(hr
);
1140 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
1141 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
1142 AC_BUFLEN
, 0, device
->orig_fmt
, &device
->parent
->session
);
1144 if(hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
&& !(info
->flags
& WAVE_FORMAT_DIRECT
)){
1145 ret
= WINMM_MapDevice(device
, FALSE
, is_out
);
1146 if(ret
!= MMSYSERR_NOERROR
|| info
->flags
& WAVE_FORMAT_QUERY
)
1149 WARN("Initialize failed: %08lx\n", hr
);
1154 device
->bytes_per_frame
= device
->orig_fmt
->nBlockAlign
;
1155 device
->samples_per_sec
= device
->orig_fmt
->nSamplesPerSec
;
1158 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioClock
,
1159 (void**)&device
->clock
);
1161 WARN("GetService failed: %08lx\n", hr
);
1166 device
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1168 WARN("CreateEvent failed: %08lx\n", hr
);
1172 /* As the devices thread is waiting on g_device_handles, it can
1173 * only be modified from within this same thread. */
1174 if(g_device_handles
){
1175 g_device_handles
= HeapReAlloc(GetProcessHeap(), 0, g_device_handles
,
1176 sizeof(HANDLE
) * (g_devhandle_count
+ 1));
1177 g_handle_devices
= HeapReAlloc(GetProcessHeap(), 0, g_handle_devices
,
1178 sizeof(WINMM_Device
*) * (g_devhandle_count
+ 1));
1180 g_device_handles
= HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE
));
1181 g_handle_devices
= HeapAlloc(GetProcessHeap(), 0,
1182 sizeof(WINMM_Device
*));
1184 g_device_handles
[g_devhandle_count
] = device
->event
;
1185 g_handle_devices
[g_devhandle_count
] = device
;
1186 ++g_devhandle_count
;
1189 hr
= IAudioClient_SetEventHandle(device
->client
, device
->event
);
1191 WARN("SetEventHandle failed: %08lx\n", hr
);
1196 device
->played_frames
= 0;
1197 device
->ofs_bytes
= 0;
1198 device
->loop_counter
= 0;
1199 device
->first
= device
->last
= device
->playing
= device
->loop_start
= NULL
;
1202 device
->stopped
= TRUE
;
1203 device
->last_clock_pos
= 0;
1205 device
->cb_info
.flags
= HIWORD(info
->flags
& CALLBACK_TYPEMASK
);
1206 device
->cb_info
.callback
= info
->callback
;
1207 device
->cb_info
.user
= info
->cb_user
;
1208 device
->cb_info
.hwave
= device
->handle
;
1210 info
->handle
= device
->handle
;
1212 return MMSYSERR_NOERROR
;
1216 IAudioClient_Release(device
->client
);
1217 device
->client
= NULL
;
1220 IMMDevice_Release(device
->device
);
1221 device
->device
= NULL
;
1227 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
)
1229 WINMM_Device
*device
;
1230 LRESULT ret
= MMSYSERR_ERROR
;
1233 if(info
->handle
!= 0){
1234 device
= WINMM_GetDeviceFromHWAVE(info
->handle
);
1236 WARN("Unexpected! Invalid info->handle given: %p\n", info
->handle
);
1237 return MMSYSERR_ERROR
;
1240 EnterCriticalSection(&device
->lock
);
1242 device
->open
= TRUE
;
1244 CRITICAL_SECTION
*lock
;
1245 UINT internal_index
;
1246 WINMM_Device
**devices
;
1247 WINMM_MMDevice
*mmdevice
;
1249 if(WINMM_IsMapper(info
->req_device
)){
1250 if (g_outmmdevices_count
== 0)
1251 return MMSYSERR_BADDEVICEID
;
1252 devices
= g_out_mapper_devices
;
1253 mmdevice
= read_map(g_out_map
, 0);
1254 lock
= &g_devthread_lock
;
1255 internal_index
= MAPPER_INDEX
;
1257 if(info
->req_device
>= g_outmmdevices_count
)
1258 return MMSYSERR_BADDEVICEID
;
1260 mmdevice
= read_map(g_out_map
, info
->req_device
);
1262 if(!mmdevice
->out_caps
.szPname
[0])
1263 return MMSYSERR_NOTENABLED
;
1265 devices
= mmdevice
->devices
;
1266 lock
= &mmdevice
->lock
;
1267 internal_index
= mmdevice
->index
;
1270 EnterCriticalSection(lock
);
1272 device
= WINMM_FindUnusedDevice(devices
, mmdevice
,
1273 internal_index
, TRUE
);
1275 LeaveCriticalSection(lock
);
1276 return MMSYSERR_ALLOCATED
;
1279 LeaveCriticalSection(lock
);
1282 ret
= WINMM_OpenDevice(device
, info
, TRUE
);
1283 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1285 ret
= MMSYSERR_ERROR
;
1287 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioRenderClient
,
1288 (void**)&device
->render
);
1290 ERR("GetService failed: %08lx\n", hr
);
1294 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioStreamVolume
,
1295 (void**)&device
->volume
);
1297 ERR("GetService failed: %08lx\n", hr
);
1301 LeaveCriticalSection(&device
->lock
);
1303 return MMSYSERR_NOERROR
;
1307 IMMDevice_Release(device
->device
);
1308 device
->device
= NULL
;
1311 IAudioClient_Release(device
->client
);
1312 device
->client
= NULL
;
1315 IAudioRenderClient_Release(device
->render
);
1316 device
->render
= NULL
;
1319 IAudioStreamVolume_Release(device
->volume
);
1320 device
->volume
= NULL
;
1323 IAudioClock_Release(device
->clock
);
1324 device
->clock
= NULL
;
1326 device
->open
= FALSE
;
1327 LeaveCriticalSection(&device
->lock
);
1331 static LRESULT
WID_Open(WINMM_OpenInfo
*info
)
1333 WINMM_Device
*device
, **devices
;
1334 WINMM_MMDevice
*mmdevice
;
1335 UINT internal_index
;
1336 CRITICAL_SECTION
*lock
;
1337 LRESULT ret
= MMSYSERR_ERROR
;
1340 if(WINMM_IsMapper(info
->req_device
)){
1341 if (g_inmmdevices_count
== 0)
1342 return MMSYSERR_BADDEVICEID
;
1343 devices
= g_in_mapper_devices
;
1344 mmdevice
= read_map(g_in_map
, 0);
1345 lock
= &g_devthread_lock
;
1346 internal_index
= MAPPER_INDEX
;
1348 if(info
->req_device
>= g_inmmdevices_count
)
1349 return MMSYSERR_BADDEVICEID
;
1351 mmdevice
= read_map(g_in_map
, info
->req_device
);
1353 if(!mmdevice
->in_caps
.szPname
[0])
1354 return MMSYSERR_NOTENABLED
;
1356 devices
= mmdevice
->devices
;
1357 lock
= &mmdevice
->lock
;
1358 internal_index
= mmdevice
->index
;
1361 EnterCriticalSection(lock
);
1363 device
= WINMM_FindUnusedDevice(devices
, mmdevice
, internal_index
, FALSE
);
1365 LeaveCriticalSection(lock
);
1366 return MMSYSERR_ALLOCATED
;
1369 LeaveCriticalSection(lock
);
1371 ret
= WINMM_OpenDevice(device
, info
, FALSE
);
1372 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1374 ret
= MMSYSERR_ERROR
;
1376 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioCaptureClient
,
1377 (void**)&device
->capture
);
1379 WARN("GetService failed: %08lx\n", hr
);
1383 LeaveCriticalSection(&device
->lock
);
1385 return MMSYSERR_NOERROR
;
1389 IMMDevice_Release(device
->device
);
1390 device
->device
= NULL
;
1393 IAudioClient_Release(device
->client
);
1394 device
->client
= NULL
;
1396 if(device
->capture
){
1397 IAudioCaptureClient_Release(device
->capture
);
1398 device
->capture
= NULL
;
1401 IAudioClock_Release(device
->clock
);
1402 device
->clock
= NULL
;
1404 device
->open
= FALSE
;
1405 LeaveCriticalSection(&device
->lock
);
1409 static HRESULT
WINMM_CloseDevice(WINMM_Device
*device
)
1411 device
->open
= FALSE
;
1413 if(!device
->stopped
){
1414 IAudioClient_Stop(device
->client
);
1415 device
->stopped
= TRUE
;
1418 if(device
->acm_handle
){
1419 acmStreamClose(device
->acm_handle
, 0);
1420 device
->acm_handle
= NULL
;
1423 IMMDevice_Release(device
->device
);
1424 device
->device
= NULL
;
1426 IAudioClient_Release(device
->client
);
1427 device
->client
= NULL
;
1429 IAudioClock_Release(device
->clock
);
1430 device
->clock
= NULL
;
1432 HeapFree(GetProcessHeap(), 0, device
->orig_fmt
);
1437 static LRESULT
WOD_Close(HWAVEOUT hwave
)
1439 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1441 TRACE("(%p)\n", hwave
);
1443 if(!WINMM_ValidateAndLock(device
))
1444 return MMSYSERR_INVALHANDLE
;
1446 WINMM_CloseDevice(device
);
1448 IAudioRenderClient_Release(device
->render
);
1449 device
->render
= NULL
;
1451 IAudioStreamVolume_Release(device
->volume
);
1452 device
->volume
= NULL
;
1454 LeaveCriticalSection(&device
->lock
);
1456 return MMSYSERR_NOERROR
;
1459 static LRESULT
WID_Close(HWAVEIN hwave
)
1461 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1463 TRACE("(%p)\n", hwave
);
1465 if(!WINMM_ValidateAndLock(device
))
1466 return MMSYSERR_INVALHANDLE
;
1468 WINMM_CloseDevice(device
);
1470 IAudioCaptureClient_Release(device
->capture
);
1471 device
->capture
= NULL
;
1473 LeaveCriticalSection(&device
->lock
);
1475 return MMSYSERR_NOERROR
;
1478 static DWORD
WINMM_FixedBufferLen(DWORD length
, WINMM_Device
*device
)
1480 return length
- length
% device
->bytes_per_frame
;
1483 static LRESULT
WINMM_PrepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1485 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1487 TRACE("(%p, %p)\n", hwave
, header
);
1489 if(!WINMM_ValidateAndLock(device
))
1490 return MMSYSERR_INVALHANDLE
;
1492 if(device
->render
&& device
->acm_handle
){
1493 ACMSTREAMHEADER
*ash
;
1497 mr
= acmStreamSize(device
->acm_handle
, header
->dwBufferLength
, &size
,
1498 ACM_STREAMSIZEF_SOURCE
);
1499 if(mr
!= MMSYSERR_NOERROR
){
1500 LeaveCriticalSection(&device
->lock
);
1504 ash
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER
) + size
);
1506 LeaveCriticalSection(&device
->lock
);
1507 return MMSYSERR_NOMEM
;
1510 ash
->cbStruct
= sizeof(*ash
);
1512 ash
->dwUser
= (DWORD_PTR
)header
;
1513 ash
->pbSrc
= (BYTE
*)header
->lpData
;
1514 ash
->cbSrcLength
= header
->dwBufferLength
;
1515 ash
->dwSrcUser
= header
->dwUser
;
1516 ash
->pbDst
= (BYTE
*)ash
+ sizeof(ACMSTREAMHEADER
);
1517 ash
->cbDstLength
= size
;
1520 mr
= acmStreamPrepareHeader(device
->acm_handle
, ash
, 0);
1521 if(mr
!= MMSYSERR_NOERROR
){
1522 HeapFree(GetProcessHeap(), 0, ash
);
1523 LeaveCriticalSection(&device
->lock
);
1527 header
->reserved
= (DWORD_PTR
)ash
;
1530 LeaveCriticalSection(&device
->lock
);
1532 header
->dwFlags
|= WHDR_PREPARED
;
1533 header
->dwFlags
&= ~(WHDR_DONE
|WHDR_INQUEUE
); /* flags cleared since w2k */
1535 return MMSYSERR_NOERROR
;
1538 static LRESULT
WINMM_UnprepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1540 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1542 TRACE("(%p, %p)\n", hwave
, header
);
1544 if(!WINMM_ValidateAndLock(device
))
1545 return MMSYSERR_INVALHANDLE
;
1547 if(device
->render
&& device
->acm_handle
){
1548 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1550 acmStreamUnprepareHeader(device
->acm_handle
, ash
, 0);
1552 HeapFree(GetProcessHeap(), 0, ash
);
1555 LeaveCriticalSection(&device
->lock
);
1557 header
->dwFlags
&= ~WHDR_PREPARED
;
1559 return MMSYSERR_NOERROR
;
1562 static UINT32
WINMM_HeaderLenBytes(WINMM_Device
*device
, WAVEHDR
*header
)
1564 if(device
->acm_handle
){
1565 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1566 return WINMM_FixedBufferLen(ash
->cbDstLengthUsed
, device
);
1569 return WINMM_FixedBufferLen(header
->dwBufferLength
, device
);
1572 static UINT32
WINMM_HeaderLenFrames(WINMM_Device
*device
, WAVEHDR
*header
)
1574 return WINMM_HeaderLenBytes(device
, header
) / device
->bytes_per_frame
;
1577 static WAVEHDR
*WOD_MarkDoneHeaders(WINMM_Device
*device
)
1580 WAVEHDR
*first
= device
->first
, *queue
= first
, *last
= NULL
;
1581 UINT64 clock_freq
, clock_pos
, clock_frames
;
1582 UINT32 nloops
, queue_frames
= 0;
1584 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
1586 WARN("GetFrequency failed: %08lx\n", hr
);
1590 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
1592 WARN("GetPosition failed: %08lx\n", hr
);
1596 clock_frames
= (clock_pos
* device
->samples_per_sec
) / clock_freq
;
1598 nloops
= device
->loop_counter
;
1600 (queue_frames
+= WINMM_HeaderLenFrames(device
, queue
)) <=
1601 clock_frames
- device
->last_clock_pos
+ device
->remainder_frames
){
1604 device
->last_clock_pos
+= queue_frames
;
1605 device
->remainder_frames
= 0;
1607 queue
= device
->first
= queue
->lpNext
;
1609 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1610 if(queue
->dwFlags
& WHDR_ENDLOOP
)
1613 queue
= queue
->lpNext
;
1614 }else if(queue
->dwFlags
& WHDR_ENDLOOP
){
1615 queue
= device
->loop_start
;
1622 last
->lpNext
= NULL
;
1628 static void WOD_PushData(WINMM_Device
*device
)
1630 WINMM_CBInfo cb_info
;
1632 UINT32 pad
, bufsize
, avail_frames
, queue_frames
, written
, ofs
;
1633 UINT32 queue_bytes
, nloops
;
1635 WAVEHDR
*queue
, *first
= NULL
;
1637 TRACE("(%p)\n", device
->handle
);
1639 EnterCriticalSection(&device
->lock
);
1645 if (device
->stopped
)
1647 device
->stopped
= TRUE
;
1648 device
->last_clock_pos
= 0;
1649 IAudioClient_Stop(device
->client
);
1650 IAudioClient_Reset(device
->client
);
1654 hr
= IAudioClient_GetBufferSize(device
->client
, &bufsize
);
1656 WARN("GetBufferSize failed: %08lx\n", hr
);
1660 hr
= IAudioClient_GetCurrentPadding(device
->client
, &pad
);
1662 WARN("GetCurrentPadding failed: %08lx\n", hr
);
1666 first
= WOD_MarkDoneHeaders(device
);
1668 /* determine which is larger between the available buffer size and
1669 * the amount of data left in the queue */
1670 avail_frames
= bufsize
- pad
;
1672 queue
= device
->playing
;
1673 ofs
= device
->ofs_bytes
;
1676 while(queue
&& queue_frames
< avail_frames
){
1677 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1678 queue_frames
+= (queue_bytes
- ofs
) / device
->bytes_per_frame
;
1681 if(queue
->dwFlags
& WHDR_ENDLOOP
&& nloops
< device
->loop_counter
){
1682 queue
= device
->loop_start
;
1685 queue
= queue
->lpNext
;
1688 if(queue_frames
< avail_frames
)
1689 avail_frames
= queue_frames
;
1690 if(avail_frames
== 0)
1693 hr
= IAudioRenderClient_GetBuffer(device
->render
, avail_frames
, &data
);
1695 WARN("GetBuffer failed: %08lx\n", hr
);
1700 while(device
->playing
&& written
< avail_frames
){
1701 UINT32 copy_frames
, copy_bytes
;
1704 queue
= device
->playing
;
1706 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1707 if(device
->acm_handle
)
1708 queue_data
= ((ACMSTREAMHEADER
*)queue
->reserved
)->pbDst
;
1710 queue_data
= (BYTE
*)queue
->lpData
;
1712 queue_frames
= (queue_bytes
- device
->ofs_bytes
) /
1713 device
->bytes_per_frame
;
1715 copy_frames
= queue_frames
< (avail_frames
- written
) ?
1716 queue_frames
: avail_frames
- written
;
1717 copy_bytes
= copy_frames
* device
->bytes_per_frame
;
1719 memcpy(data
, queue_data
+ device
->ofs_bytes
, copy_bytes
);
1722 written
+= copy_frames
;
1723 device
->ofs_bytes
+= copy_bytes
;
1725 if(device
->ofs_bytes
>= queue_bytes
){
1726 device
->ofs_bytes
= 0;
1728 if(!(queue
->dwFlags
& (WHDR_BEGINLOOP
| WHDR_ENDLOOP
)))
1729 device
->playing
= queue
->lpNext
;
1731 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1732 device
->loop_start
= device
->playing
;
1733 device
->playing
= queue
->lpNext
;
1734 device
->loop_counter
= queue
->dwLoops
;
1736 if(queue
->dwFlags
& WHDR_ENDLOOP
){
1737 --device
->loop_counter
;
1738 if(device
->loop_counter
)
1739 device
->playing
= device
->loop_start
;
1741 device
->loop_start
= device
->playing
= queue
->lpNext
;
1747 hr
= IAudioRenderClient_ReleaseBuffer(device
->render
, avail_frames
, 0);
1749 WARN("ReleaseBuffer failed: %08lx\n", hr
);
1753 if(device
->orig_fmt
->nSamplesPerSec
!= device
->samples_per_sec
)
1754 device
->played_frames
+= MulDiv(avail_frames
, device
->orig_fmt
->nSamplesPerSec
, device
->samples_per_sec
);
1756 device
->played_frames
+= avail_frames
;
1759 cb_info
= device
->cb_info
;
1761 LeaveCriticalSection(&device
->lock
);
1764 WAVEHDR
*next
= first
->lpNext
;
1765 first
->dwFlags
&= ~WHDR_INQUEUE
;
1766 first
->dwFlags
|= WHDR_DONE
;
1767 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
1772 static void WID_PullACMData(WINMM_Device
*device
)
1782 if(device
->acm_hdr
.cbDstLength
== 0){
1783 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet
,
1784 &flags
, NULL
, NULL
);
1787 WARN("GetBuffer failed: %08lx\n", hr
);
1791 acmStreamSize(device
->acm_handle
, packet
* device
->bytes_per_frame
,
1792 &packet_bytes
, ACM_STREAMSIZEF_SOURCE
);
1794 device
->acm_offs
= 0;
1796 device
->acm_hdr
.cbStruct
= sizeof(device
->acm_hdr
);
1797 device
->acm_hdr
.fdwStatus
= 0;
1798 device
->acm_hdr
.dwUser
= 0;
1799 device
->acm_hdr
.pbSrc
= data
;
1800 device
->acm_hdr
.cbSrcLength
= packet
* device
->bytes_per_frame
;
1801 device
->acm_hdr
.cbSrcLengthUsed
= 0;
1802 device
->acm_hdr
.dwSrcUser
= 0;
1803 device
->acm_hdr
.pbDst
= HeapAlloc(GetProcessHeap(), 0, packet_bytes
);
1804 device
->acm_hdr
.cbDstLength
= packet_bytes
;
1805 device
->acm_hdr
.cbDstLengthUsed
= 0;
1806 device
->acm_hdr
.dwDstUser
= 0;
1808 mr
= acmStreamPrepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1809 if(mr
!= MMSYSERR_NOERROR
){
1810 WARN("acmStreamPrepareHeader failed: %d\n", mr
);
1814 mr
= acmStreamConvert(device
->acm_handle
, &device
->acm_hdr
, 0);
1815 if(mr
!= MMSYSERR_NOERROR
){
1816 WARN("acmStreamConvert failed: %d\n", mr
);
1820 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet
);
1822 WARN("ReleaseBuffer failed: %08lx\n", hr
);
1824 device
->played_frames
+= packet
;
1827 queue
= device
->first
;
1829 UINT32 to_copy_bytes
;
1831 to_copy_bytes
= min(WINMM_FixedBufferLen(queue
->dwBufferLength
, device
) - queue
->dwBytesRecorded
,
1832 WINMM_FixedBufferLen(device
->acm_hdr
.cbDstLengthUsed
, device
) - device
->acm_offs
);
1834 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1835 device
->acm_hdr
.pbDst
+ device
->acm_offs
, to_copy_bytes
);
1837 queue
->dwBytesRecorded
+= to_copy_bytes
;
1838 device
->acm_offs
+= to_copy_bytes
;
1840 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1841 device
->bytes_per_frame
){
1842 queue
->dwFlags
&= ~WHDR_INQUEUE
;
1843 queue
->dwFlags
|= WHDR_DONE
;
1844 device
->first
= queue
= queue
->lpNext
;
1847 if(device
->acm_offs
>= WINMM_FixedBufferLen(device
->acm_hdr
.cbDstLengthUsed
, device
)){
1848 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1849 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1850 device
->acm_hdr
.cbDstLength
= 0;
1851 device
->acm_hdr
.cbDstLengthUsed
= 0;
1853 /* done with this ACM Header, so try to pull more data */
1854 WID_PullACMData(device
);
1859 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1860 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1861 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1862 device
->acm_hdr
.cbDstLength
= 0;
1863 device
->acm_hdr
.cbDstLengthUsed
= 0;
1866 static void WID_PullData(WINMM_Device
*device
)
1868 WINMM_CBInfo cb_info
;
1869 WAVEHDR
*queue
, *first
= NULL
, *last
= NULL
;
1872 TRACE("(%p)\n", device
->handle
);
1874 EnterCriticalSection(&device
->lock
);
1876 if(!device
->device
|| !device
->first
)
1879 first
= device
->first
;
1881 if(device
->acm_handle
){
1882 WID_PullACMData(device
);
1886 while(device
->first
){
1888 UINT32 packet_len
, packet
;
1891 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet_len
,
1892 &flags
, NULL
, NULL
);
1895 WARN("GetBuffer failed: %08lx\n", hr
);
1896 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1897 IAudioCaptureClient_ReleaseBuffer(device
->capture
, 0);
1901 packet
= packet_len
;
1902 queue
= device
->first
;
1903 while(queue
&& packet
> 0){
1904 UINT32 to_copy_bytes
;
1906 to_copy_bytes
= min(packet
* device
->bytes_per_frame
,
1907 WINMM_FixedBufferLen(queue
->dwBufferLength
, device
) - queue
->dwBytesRecorded
);
1909 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1910 data
+ (packet_len
- packet
) * device
->bytes_per_frame
,
1913 queue
->dwBytesRecorded
+= to_copy_bytes
;
1915 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1916 device
->bytes_per_frame
){
1918 device
->first
= queue
= queue
->lpNext
;
1921 packet
-= to_copy_bytes
/ device
->bytes_per_frame
;
1924 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet_len
);
1926 WARN("ReleaseBuffer failed: %08lx\n", hr
);
1929 WARN("losing %u frames\n", packet
);
1930 device
->played_frames
+= packet_len
- packet
;
1934 cb_info
= device
->cb_info
;
1936 LeaveCriticalSection(&device
->lock
);
1939 last
->lpNext
= NULL
;
1941 WAVEHDR
*next
= first
->lpNext
;
1942 first
->dwFlags
&= ~WHDR_INQUEUE
;
1943 first
->dwFlags
|= WHDR_DONE
;
1944 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
1950 static MMRESULT
WINMM_BeginPlaying(WINMM_Device
*device
)
1954 TRACE("(%p)\n", device
->handle
);
1956 if(device
->stopped
){
1957 device
->stopped
= FALSE
;
1959 hr
= IAudioClient_Start(device
->client
);
1960 if(FAILED(hr
) && hr
!= AUDCLNT_E_NOT_STOPPED
){
1961 device
->stopped
= TRUE
;
1962 WARN("Start failed: %08lx\n", hr
);
1963 return MMSYSERR_ERROR
;
1967 return MMSYSERR_NOERROR
;
1970 static LRESULT
WINMM_Pause(WINMM_Device
*device
)
1974 TRACE("(%p)\n", device
->handle
);
1976 hr
= IAudioClient_Stop(device
->client
);
1978 WARN("Stop failed: %08lx\n", hr
);
1979 return MMSYSERR_ERROR
;
1982 device
->stopped
= FALSE
;
1984 return MMSYSERR_NOERROR
;
1987 static LRESULT
WINMM_Reset(HWAVE hwave
)
1989 WINMM_CBInfo cb_info
;
1990 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1995 TRACE("(%p)\n", hwave
);
1997 if(!WINMM_ValidateAndLock(device
))
1998 return MMSYSERR_INVALHANDLE
;
2000 hr
= IAudioClient_Stop(device
->client
);
2002 LeaveCriticalSection(&device
->lock
);
2003 WARN("Stop failed: %08lx\n", hr
);
2004 return MMSYSERR_ERROR
;
2006 device
->stopped
= TRUE
;
2008 first
= device
->first
;
2009 device
->first
= device
->last
= device
->playing
= NULL
;
2010 device
->ofs_bytes
= 0;
2011 device
->played_frames
= 0;
2012 device
->loop_counter
= 0;
2013 device
->last_clock_pos
= 0;
2014 IAudioClient_Reset(device
->client
);
2016 cb_info
= device
->cb_info
;
2017 is_out
= device
->render
!= NULL
;
2019 LeaveCriticalSection(&device
->lock
);
2022 WAVEHDR
*next
= first
->lpNext
;
2023 first
->dwFlags
&= ~WHDR_INQUEUE
;
2024 first
->dwFlags
|= WHDR_DONE
;
2026 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
2028 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
2032 return MMSYSERR_NOERROR
;
2035 static MMRESULT
WINMM_FramesToMMTime(MMTIME
*time
, UINT32 played_frames
,
2036 UINT32 sample_rate
, UINT32 bytes_per_sec
)
2038 switch(time
->wType
){
2040 time
->u
.sample
= played_frames
;
2041 return MMSYSERR_NOERROR
;
2043 time
->wType
= TIME_BYTES
;
2046 time
->u
.cb
= MulDiv(played_frames
, bytes_per_sec
, sample_rate
);
2047 return MMSYSERR_NOERROR
;
2051 static LRESULT
WINMM_GetPosition(HWAVE hwave
, MMTIME
*time
)
2053 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
2054 UINT32 played_frames
, sample_rate
, bytes_per_sec
;
2056 TRACE("(%p, %p)\n", hwave
, time
);
2058 if(!WINMM_ValidateAndLock(device
))
2059 return MMSYSERR_INVALHANDLE
;
2061 played_frames
= device
->played_frames
;
2062 sample_rate
= device
->orig_fmt
->nSamplesPerSec
;
2063 bytes_per_sec
= device
->orig_fmt
->nAvgBytesPerSec
;
2065 LeaveCriticalSection(&device
->lock
);
2067 return WINMM_FramesToMMTime(time
, played_frames
, sample_rate
, bytes_per_sec
);
2070 static WINMM_MMDevice
*WINMM_GetMixerMMDevice(HMIXEROBJ hmix
, DWORD flags
,
2073 UINT mmdev
, dev
, junk
, *out
;
2081 switch(flags
& 0xF0000000){
2082 case MIXER_OBJECTF_MIXER
: /* == 0 */
2083 *out
= HandleToULong(hmix
);
2084 if(*out
< g_outmmdevices_count
)
2085 return read_map(g_out_map
, *out
);
2086 if(*out
- g_outmmdevices_count
< g_inmmdevices_count
){
2087 *out
-= g_outmmdevices_count
;
2088 return read_map(g_in_map
, *out
);
2090 /* fall through -- if it's not a valid mixer device, then
2091 * it could be a valid mixer handle. windows seems to do
2093 case MIXER_OBJECTF_HMIXER
:
2094 case MIXER_OBJECTF_HWAVEOUT
:
2095 case MIXER_OBJECTF_HWAVEIN
:
2096 WINMM_DecomposeHWAVE((HWAVE
)hmix
, out
, &is_out
, &dev
, &junk
);
2097 if(junk
!= 0x1 || (is_out
&& *out
>= g_outmmdevices_count
) ||
2098 (!is_out
&& *out
>= g_inmmdevices_count
))
2101 return read_map(g_out_map
, *out
);
2102 return read_map(g_in_map
, *out
);
2103 case MIXER_OBJECTF_WAVEOUT
:
2104 *out
= HandleToULong(hmix
);
2105 if(*out
< g_outmmdevices_count
)
2106 return read_map(g_out_map
, *out
);
2108 case MIXER_OBJECTF_WAVEIN
:
2109 *out
= HandleToULong(hmix
);
2110 if(*out
< g_inmmdevices_count
)
2111 return read_map(g_in_map
, *out
);
2118 static MMRESULT
WINMM_SetupMMDeviceVolume(WINMM_MMDevice
*mmdevice
)
2120 IAudioSessionManager
*sesman
;
2124 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
, &device
);
2126 WARN("Device %s (%s) unavailable: %08lx\n",
2127 wine_dbgstr_w(mmdevice
->dev_id
),
2128 wine_dbgstr_w(mmdevice
->out_caps
.szPname
), hr
);
2129 return MMSYSERR_ERROR
;
2132 hr
= IMMDevice_Activate(device
, &IID_IAudioSessionManager
,
2133 CLSCTX_INPROC_SERVER
, NULL
, (void**)&sesman
);
2135 WARN("Activate failed: %08lx\n", hr
);
2136 IMMDevice_Release(device
);
2137 return MMSYSERR_ERROR
;
2140 IMMDevice_Release(device
);
2142 hr
= IAudioSessionManager_GetSimpleAudioVolume(sesman
, &mmdevice
->session
,
2143 FALSE
, &mmdevice
->volume
);
2144 IAudioSessionManager_Release(sesman
);
2146 WARN("GetSimpleAudioVolume failed: %08lx\n", hr
);
2147 return MMSYSERR_ERROR
;
2150 return MMSYSERR_NOERROR
;
2153 static LRESULT
MXD_GetControlDetails(WINMM_ControlDetails
*details
)
2155 WINMM_MMDevice
*mmdevice
;
2156 MIXERCONTROLDETAILS
*control
= details
->details
;
2159 TRACE("(%p)\n", details
->hmix
);
2161 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
2163 return MMSYSERR_INVALHANDLE
;
2165 EnterCriticalSection(&mmdevice
->lock
);
2167 if(!mmdevice
->volume
){
2170 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
2171 if(mr
!= MMSYSERR_NOERROR
){
2172 LeaveCriticalSection(&mmdevice
->lock
);
2177 if(control
->dwControlID
== 0){
2179 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
2181 if(!control
->paDetails
||
2182 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
2183 LeaveCriticalSection(&mmdevice
->lock
);
2184 return MMSYSERR_INVALPARAM
;
2187 hr
= ISimpleAudioVolume_GetMasterVolume(mmdevice
->volume
, &vol
);
2189 WARN("GetMasterVolume failed: %08lx\n", hr
);
2190 LeaveCriticalSection(&mmdevice
->lock
);
2191 return MMSYSERR_ERROR
;
2194 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
2195 udet
->dwValue
= vol
* ((unsigned int)0xFFFF);
2196 }else if(control
->dwControlID
== 1){
2198 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
2200 if(!control
->paDetails
||
2201 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
2202 LeaveCriticalSection(&mmdevice
->lock
);
2203 return MMSYSERR_INVALPARAM
;
2206 hr
= ISimpleAudioVolume_GetMute(mmdevice
->volume
, &mute
);
2208 WARN("GetMute failed: %08lx\n", hr
);
2209 LeaveCriticalSection(&mmdevice
->lock
);
2210 return MMSYSERR_ERROR
;
2213 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
2214 bdet
->fValue
= mute
;
2215 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2216 FIXME("What should the sw-side mixer controls map to?\n");
2218 LeaveCriticalSection(&mmdevice
->lock
);
2219 return MIXERR_INVALCONTROL
;
2222 LeaveCriticalSection(&mmdevice
->lock
);
2224 return MMSYSERR_NOERROR
;
2227 static LRESULT
MXD_SetControlDetails(WINMM_ControlDetails
*details
)
2229 WINMM_MMDevice
*mmdevice
;
2230 MIXERCONTROLDETAILS
*control
= details
->details
;
2233 TRACE("(%p)\n", details
->hmix
);
2235 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
2237 return MMSYSERR_INVALHANDLE
;
2239 EnterCriticalSection(&mmdevice
->lock
);
2241 if(!mmdevice
->volume
){
2244 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
2245 if(mr
!= MMSYSERR_NOERROR
){
2246 LeaveCriticalSection(&mmdevice
->lock
);
2251 if(control
->dwControlID
== 0){
2253 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
2255 if(!control
->paDetails
||
2256 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
2257 LeaveCriticalSection(&mmdevice
->lock
);
2258 return MMSYSERR_INVALPARAM
;
2261 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
2263 if(udet
->dwValue
> 65535){
2264 LeaveCriticalSection(&mmdevice
->lock
);
2265 return MMSYSERR_INVALPARAM
;
2268 vol
= udet
->dwValue
/ 65535.f
;
2270 hr
= ISimpleAudioVolume_SetMasterVolume(mmdevice
->volume
, vol
, NULL
);
2272 WARN("SetMasterVolume failed: %08lx\n", hr
);
2273 LeaveCriticalSection(&mmdevice
->lock
);
2274 return MMSYSERR_ERROR
;
2276 }else if(control
->dwControlID
== 1){
2278 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
2280 if(!control
->paDetails
||
2281 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
2282 LeaveCriticalSection(&mmdevice
->lock
);
2283 return MMSYSERR_INVALPARAM
;
2286 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
2287 mute
= bdet
->fValue
;
2289 hr
= ISimpleAudioVolume_SetMute(mmdevice
->volume
, mute
, NULL
);
2291 WARN("SetMute failed: %08lx\n", hr
);
2292 LeaveCriticalSection(&mmdevice
->lock
);
2293 return MMSYSERR_ERROR
;
2295 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2296 FIXME("What should the sw-side mixer controls map to?\n");
2298 LeaveCriticalSection(&mmdevice
->lock
);
2299 return MIXERR_INVALCONTROL
;
2302 LeaveCriticalSection(&mmdevice
->lock
);
2304 return MMSYSERR_NOERROR
;
2307 static LRESULT
DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo
*info
)
2309 WINMM_MMDevice
*mmdevice
;
2316 static const PROPERTYKEY deviceinterface_key
= {
2317 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2320 if(WINMM_IsMapper(info
->index
)){
2322 if(*info
->len_bytes
< sizeof(WCHAR
))
2323 return MMSYSERR_INVALPARAM
;
2326 *info
->len_bytes
= sizeof(WCHAR
);
2327 return MMSYSERR_NOERROR
;
2331 if(info
->index
>= g_outmmdevices_count
)
2332 return MMSYSERR_INVALHANDLE
;
2334 mmdevice
= &g_out_mmdevices
[info
->index
];
2336 if(info
->index
>= g_inmmdevices_count
)
2337 return MMSYSERR_INVALHANDLE
;
2339 mmdevice
= &g_in_mmdevices
[info
->index
];
2342 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
,
2345 WARN("Device %s unavailable: %08lx\n", wine_dbgstr_w(mmdevice
->dev_id
), hr
);
2346 return MMSYSERR_ERROR
;
2349 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
2351 WARN("OpenPropertyStore failed: %08lx\n", hr
);
2352 IMMDevice_Release(device
);
2353 return MMSYSERR_ERROR
;
2356 PropVariantInit(&pv
);
2357 hr
= IPropertyStore_GetValue(ps
, &deviceinterface_key
, &pv
);
2359 WARN("GetValue failed: %08lx\n", hr
);
2360 IPropertyStore_Release(ps
);
2361 IMMDevice_Release(device
);
2362 return MMSYSERR_ERROR
;
2364 if(pv
.vt
!= VT_LPWSTR
){
2365 WARN("Got unexpected property type: %u\n", pv
.vt
);
2366 PropVariantClear(&pv
);
2367 IPropertyStore_Release(ps
);
2368 IMMDevice_Release(device
);
2369 return MMSYSERR_ERROR
;
2372 len_bytes
= (lstrlenW(pv
.pwszVal
) + 1) * sizeof(WCHAR
);
2375 if(len_bytes
> *info
->len_bytes
){
2376 PropVariantClear(&pv
);
2377 IPropertyStore_Release(ps
);
2378 IMMDevice_Release(device
);
2379 return MMSYSERR_INVALPARAM
;
2382 memcpy(info
->str
, pv
.pwszVal
, len_bytes
);
2384 *info
->len_bytes
= len_bytes
;
2386 PropVariantClear(&pv
);
2387 IPropertyStore_Release(ps
);
2388 IMMDevice_Release(device
);
2390 return MMSYSERR_NOERROR
;
2393 static LRESULT CALLBACK
WINMM_DevicesMsgProc(HWND hwnd
, UINT msg
, WPARAM wparam
,
2398 return WOD_Open((WINMM_OpenInfo
*)wparam
);
2400 return WOD_Close((HWAVEOUT
)wparam
);
2402 return WID_Open((WINMM_OpenInfo
*)wparam
);
2404 return WID_Close((HWAVEIN
)wparam
);
2405 case MXDM_GETCONTROLDETAILS
:
2406 return MXD_GetControlDetails((WINMM_ControlDetails
*)wparam
);
2407 case MXDM_SETCONTROLDETAILS
:
2408 return MXD_SetControlDetails((WINMM_ControlDetails
*)wparam
);
2409 case DRV_QUERYDEVICEINTERFACESIZE
:
2410 case DRV_QUERYDEVICEINTERFACE
:
2411 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo
*)wparam
);
2413 return DefWindowProcW(hwnd
, msg
, wparam
, lparam
);
2416 static BOOL
WINMM_DevicesThreadDone(void)
2420 EnterCriticalSection(&g_devthread_lock
);
2422 if(g_devthread_token
> 0){
2423 LeaveCriticalSection(&g_devthread_lock
);
2427 for(i
= 0; i
< g_devhandle_count
; ++i
){
2428 if(g_handle_devices
[i
]->open
){
2429 LeaveCriticalSection(&g_devthread_lock
);
2434 DestroyWindow(g_devices_hwnd
);
2435 g_devices_hwnd
= NULL
;
2436 IMMDeviceEnumerator_Release(g_devenum
);
2440 LeaveCriticalSection(&g_devthread_lock
);
2445 static DWORD WINAPI
WINMM_DevicesThreadProc(void *arg
)
2450 hr
= CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
2452 WARN("CoInitializeEx failed: %08lx\n", hr
);
2453 FreeLibraryAndExitThread(g_devthread_module
, 1);
2456 hr
= WINMM_InitMMDevices();
2459 FreeLibraryAndExitThread(g_devthread_module
, 1);
2462 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
2463 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&g_devenum
);
2465 WARN("CoCreateInstance failed: %08lx\n", hr
);
2467 FreeLibraryAndExitThread(g_devthread_module
, 1);
2470 g_devices_hwnd
= CreateWindowW(L
"Message", NULL
, 0, 0, 0, 0, 0,
2471 HWND_MESSAGE
, NULL
, NULL
, NULL
);
2472 if(!g_devices_hwnd
){
2473 WARN("CreateWindow failed: %ld\n", GetLastError());
2475 FreeLibraryAndExitThread(g_devthread_module
, 1);
2478 SetWindowLongPtrW(g_devices_hwnd
, GWLP_WNDPROC
,
2479 (LONG_PTR
)WINMM_DevicesMsgProc
);
2481 /* inform caller that the thread is ready to process messages */
2483 evt
= NULL
; /* do not use after this point */
2487 wait
= MsgWaitForMultipleObjects(g_devhandle_count
, g_device_handles
,
2488 FALSE
, INFINITE
, QS_ALLINPUT
);
2489 if(wait
== g_devhandle_count
+ WAIT_OBJECT_0
){
2491 if(PeekMessageW(&msg
, g_devices_hwnd
, 0, 0, PM_REMOVE
))
2492 WARN("Unexpected message: 0x%x\n", msg
.message
);
2495 }else if(wait
< g_devhandle_count
+ WAIT_OBJECT_0
){
2496 WINMM_Device
*device
= g_handle_devices
[wait
- WAIT_OBJECT_0
];
2498 WOD_PushData(device
);
2500 WID_PullData(device
);
2502 WARN("Unexpected MsgWait result 0x%lx, GLE: %ld\n", wait
,
2504 if(WINMM_DevicesThreadDone()){
2505 TRACE("Quitting devices thread\n");
2506 FreeLibraryAndExitThread(g_devthread_module
, 0);
2510 FreeLibraryAndExitThread(g_devthread_module
, 0);
2513 /* on success, increments g_devthread_token to prevent
2514 * device thread shutdown. caller must decrement. */
2515 static BOOL
WINMM_StartDevicesThread(void)
2520 EnterCriticalSection(&g_devthread_lock
);
2523 wait
= WaitForSingleObject(g_devices_thread
, 0);
2524 if(wait
== WAIT_TIMEOUT
){
2525 /* thread still running */
2526 InterlockedIncrement(&g_devthread_token
);
2527 LeaveCriticalSection(&g_devthread_lock
);
2530 if(wait
!= WAIT_OBJECT_0
){
2532 LeaveCriticalSection(&g_devthread_lock
);
2535 TRACE("Devices thread left dangling message window?\n");
2536 g_devices_hwnd
= NULL
;
2537 CloseHandle(g_devices_thread
);
2538 g_devices_thread
= NULL
;
2539 }else if(g_devices_thread
){
2540 WaitForSingleObject(g_devices_thread
, INFINITE
);
2541 CloseHandle(g_devices_thread
);
2542 g_devices_thread
= NULL
;
2545 TRACE("Starting up devices thread\n");
2547 /* The devices thread holds a reference to the winmm module
2548 * to prevent it from unloading while it's running. */
2549 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
2550 (const WCHAR
*)WINMM_StartDevicesThread
, &g_devthread_module
);
2552 events
[0] = CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2554 g_devices_thread
= CreateThread(NULL
, 0, WINMM_DevicesThreadProc
,
2555 events
[0], 0, NULL
);
2556 if(!g_devices_thread
){
2557 LeaveCriticalSection(&g_devthread_lock
);
2558 CloseHandle(events
[0]);
2559 FreeLibrary(g_devthread_module
);
2563 events
[1] = g_devices_thread
;
2564 wait
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
2565 CloseHandle(events
[0]);
2566 if(wait
!= WAIT_OBJECT_0
){
2567 if(wait
== 1 + WAIT_OBJECT_0
){
2568 CloseHandle(g_devices_thread
);
2569 g_devices_thread
= NULL
;
2570 g_devices_hwnd
= NULL
;
2572 LeaveCriticalSection(&g_devthread_lock
);
2576 InterlockedIncrement(&g_devthread_token
);
2578 LeaveCriticalSection(&g_devthread_lock
);
2583 /**************************************************************************
2584 * waveOutGetNumDevs [WINMM.@]
2586 UINT WINAPI
waveOutGetNumDevs(void)
2588 HRESULT hr
= WINMM_InitMMDevices();
2592 TRACE("count: %u\n", g_outmmdevices_count
);
2594 return g_outmmdevices_count
;
2597 /**************************************************************************
2598 * waveOutGetDevCapsA [WINMM.@]
2600 UINT WINAPI
waveOutGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEOUTCAPSA lpCaps
,
2606 TRACE("(%Iu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2609 return MMSYSERR_INVALPARAM
;
2611 ret
= waveOutGetDevCapsW(uDeviceID
, &wocW
, sizeof(wocW
));
2613 if (ret
== MMSYSERR_NOERROR
) {
2615 wocA
.wMid
= wocW
.wMid
;
2616 wocA
.wPid
= wocW
.wPid
;
2617 wocA
.vDriverVersion
= wocW
.vDriverVersion
;
2618 WideCharToMultiByte( CP_ACP
, 0, wocW
.szPname
, -1, wocA
.szPname
,
2619 sizeof(wocA
.szPname
), NULL
, NULL
);
2620 wocA
.dwFormats
= wocW
.dwFormats
;
2621 wocA
.wChannels
= wocW
.wChannels
;
2622 wocA
.dwSupport
= wocW
.dwSupport
;
2623 wocA
.wReserved1
= wocW
.wReserved1
;
2624 memcpy(lpCaps
, &wocA
, min(uSize
, sizeof(wocA
)));
2629 /**************************************************************************
2630 * waveOutGetDevCapsW [WINMM.@]
2632 UINT WINAPI
waveOutGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEOUTCAPSW lpCaps
,
2635 WAVEOUTCAPSW mapper_caps
, *caps
;
2638 TRACE("(%Iu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2640 hr
= WINMM_InitMMDevices();
2642 return MMSYSERR_NODRIVER
;
2644 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
2646 if(WINMM_IsMapper(uDeviceID
)){
2647 mapper_caps
.wMid
= 0xFF;
2648 mapper_caps
.wPid
= 0xFF;
2649 mapper_caps
.vDriverVersion
= 0x00010001;
2650 mapper_caps
.dwFormats
= 0xFFFFFFFF;
2651 mapper_caps
.wReserved1
= 0;
2652 mapper_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
2653 WAVECAPS_SAMPLEACCURATE
;
2654 mapper_caps
.wChannels
= 2;
2655 LoadStringW(hWinMM32Instance
, IDS_MAPPER_NAME
, mapper_caps
.szPname
, MAXPNAMELEN
);
2657 caps
= &mapper_caps
;
2659 if(uDeviceID
>= g_outmmdevices_count
){
2660 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)uDeviceID
);
2662 if(!WINMM_ValidateAndLock(device
))
2663 return MMSYSERR_BADDEVICEID
;
2665 caps
= &device
->parent
->out_caps
;
2667 LeaveCriticalSection(&device
->lock
);
2669 caps
= &read_map(g_out_map
, uDeviceID
)->out_caps
;
2673 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
2675 return MMSYSERR_NOERROR
;
2678 /**************************************************************************
2679 * waveOutGetErrorTextA [WINMM.@]
2680 * waveInGetErrorTextA [WINMM.@]
2682 UINT WINAPI
waveOutGetErrorTextA(UINT uError
, LPSTR lpText
, UINT uSize
)
2686 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
2687 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
2690 LPWSTR xstr
= HeapAlloc(GetProcessHeap(), 0, uSize
* sizeof(WCHAR
));
2691 if (!xstr
) ret
= MMSYSERR_NOMEM
;
2694 ret
= waveOutGetErrorTextW(uError
, xstr
, uSize
);
2695 if (ret
== MMSYSERR_NOERROR
)
2696 WideCharToMultiByte(CP_ACP
, 0, xstr
, -1, lpText
, uSize
, NULL
, NULL
);
2697 HeapFree(GetProcessHeap(), 0, xstr
);
2703 /**************************************************************************
2704 * waveOutGetErrorTextW [WINMM.@]
2705 * waveInGetErrorTextW [WINMM.@]
2707 UINT WINAPI
waveOutGetErrorTextW(UINT uError
, LPWSTR lpText
, UINT uSize
)
2709 UINT ret
= MMSYSERR_BADERRNUM
;
2711 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
2712 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
2714 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2715 * a warning for the test was always true */
2716 (/*uError >= MMSYSERR_BASE && */ uError
<= MMSYSERR_LASTERROR
) ||
2717 (uError
>= WAVERR_BASE
&& uError
<= WAVERR_LASTERROR
)) {
2718 if (LoadStringW(hWinMM32Instance
,
2719 uError
, lpText
, uSize
) > 0) {
2720 ret
= MMSYSERR_NOERROR
;
2726 /**************************************************************************
2727 * waveOutOpen [WINMM.@]
2728 * All the args/structs have the same layout as the win16 equivalents
2730 MMRESULT WINAPI
waveOutOpen(LPHWAVEOUT lphWaveOut
, UINT uDeviceID
,
2731 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
2732 DWORD_PTR dwInstance
, DWORD dwFlags
)
2735 WINMM_OpenInfo info
;
2736 WINMM_CBInfo cb_info
;
2738 TRACE("(%p, %u, %p, %Ix, %Ix, %08lx)\n", lphWaveOut
, uDeviceID
, lpFormat
,
2739 dwCallback
, dwInstance
, dwFlags
);
2741 if(!lphWaveOut
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
2742 return MMSYSERR_INVALPARAM
;
2744 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
2745 if(res
!= MMSYSERR_NOERROR
)
2748 if(!WINMM_StartDevicesThread())
2749 return MMSYSERR_NODRIVER
;
2752 info
.format
= (WAVEFORMATEX
*)lpFormat
;
2753 info
.callback
= dwCallback
;
2754 info
.cb_user
= dwInstance
;
2755 info
.req_device
= uDeviceID
;
2756 info
.flags
= dwFlags
;
2759 res
= SendMessageW(g_devices_hwnd
, WODM_OPEN
, (DWORD_PTR
)&info
, 0);
2760 InterlockedDecrement(&g_devthread_token
);
2761 if(res
!= MMSYSERR_NOERROR
|| (dwFlags
& WAVE_FORMAT_QUERY
))
2765 *lphWaveOut
= (HWAVEOUT
)info
.handle
;
2767 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2768 cb_info
.callback
= dwCallback
;
2769 cb_info
.user
= dwInstance
;
2770 cb_info
.hwave
= info
.handle
;
2772 WINMM_NotifyClient(&cb_info
, WOM_OPEN
, 0, 0);
2777 /**************************************************************************
2778 * waveOutClose [WINMM.@]
2780 UINT WINAPI
waveOutClose(HWAVEOUT hWaveOut
)
2783 WINMM_Device
*device
;
2784 WINMM_CBInfo cb_info
;
2786 TRACE("(%p)\n", hWaveOut
);
2788 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2790 if(!WINMM_ValidateAndLock(device
))
2791 return MMSYSERR_INVALHANDLE
;
2793 cb_info
= device
->cb_info
;
2795 LeaveCriticalSection(&device
->lock
);
2797 res
= SendMessageW(g_devices_hwnd
, WODM_CLOSE
, (WPARAM
)hWaveOut
, 0);
2799 if(res
== MMSYSERR_NOERROR
)
2800 WINMM_NotifyClient(&cb_info
, WOM_CLOSE
, 0, 0);
2805 /**************************************************************************
2806 * waveOutPrepareHeader [WINMM.@]
2808 UINT WINAPI
waveOutPrepareHeader(HWAVEOUT hWaveOut
,
2809 WAVEHDR
* lpWaveOutHdr
, UINT uSize
)
2811 TRACE("(%p, %p, %u)\n", hWaveOut
, lpWaveOutHdr
, uSize
);
2813 if(!lpWaveOutHdr
|| uSize
< sizeof(WAVEHDR
))
2814 return MMSYSERR_INVALPARAM
;
2816 if(lpWaveOutHdr
->dwFlags
& WHDR_PREPARED
)
2817 return MMSYSERR_NOERROR
;
2819 return WINMM_PrepareHeader((HWAVE
)hWaveOut
, lpWaveOutHdr
);
2822 /**************************************************************************
2823 * waveOutUnprepareHeader [WINMM.@]
2825 UINT WINAPI
waveOutUnprepareHeader(HWAVEOUT hWaveOut
,
2826 LPWAVEHDR lpWaveOutHdr
, UINT uSize
)
2828 TRACE("(%p, %p, %u)\n", hWaveOut
, lpWaveOutHdr
, uSize
);
2830 if(!lpWaveOutHdr
|| uSize
< sizeof(WAVEHDR
))
2831 return MMSYSERR_INVALPARAM
;
2833 if(lpWaveOutHdr
->dwFlags
& WHDR_INQUEUE
)
2834 return WAVERR_STILLPLAYING
;
2836 if(!(lpWaveOutHdr
->dwFlags
& WHDR_PREPARED
))
2837 return MMSYSERR_NOERROR
;
2839 return WINMM_UnprepareHeader((HWAVE
)hWaveOut
, lpWaveOutHdr
);
2842 /**************************************************************************
2843 * waveOutWrite [WINMM.@]
2845 UINT WINAPI
waveOutWrite(HWAVEOUT hWaveOut
, WAVEHDR
*header
, UINT uSize
)
2847 WINMM_Device
*device
;
2850 TRACE("(%p, %p, %u)\n", hWaveOut
, header
, uSize
);
2852 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2854 if(!WINMM_ValidateAndLock(device
))
2855 return MMSYSERR_INVALHANDLE
;
2857 if(!header
->lpData
|| !(header
->dwFlags
& WHDR_PREPARED
)){
2858 LeaveCriticalSection(&device
->lock
);
2859 return WAVERR_UNPREPARED
;
2862 if(header
->dwFlags
& WHDR_INQUEUE
){
2863 LeaveCriticalSection(&device
->lock
);
2864 return WAVERR_STILLPLAYING
;
2867 TRACE("dwBufferLength: %lu\n", header
->dwBufferLength
);
2869 if(device
->acm_handle
){
2870 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
2872 ash
->cbSrcLength
= header
->dwBufferLength
;
2873 mr
= acmStreamConvert(device
->acm_handle
, ash
, 0);
2874 if(mr
!= MMSYSERR_NOERROR
){
2875 LeaveCriticalSection(&device
->lock
);
2881 device
->last
->lpNext
= header
;
2882 device
->last
= header
;
2883 if(!device
->playing
)
2884 device
->playing
= header
;
2886 device
->playing
= device
->first
= device
->last
= header
;
2887 if(header
->dwFlags
& WHDR_BEGINLOOP
){
2888 device
->loop_counter
= header
->dwLoops
;
2889 device
->loop_start
= header
;
2893 header
->lpNext
= NULL
;
2894 header
->dwFlags
&= ~WHDR_DONE
;
2895 header
->dwFlags
|= WHDR_INQUEUE
;
2898 WOD_PushData(device
);
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 WINMM_Device
*device
;
2936 TRACE("(%p)\n", hWaveOut
);
2938 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2940 if(!WINMM_ValidateAndLock(device
))
2941 return MMSYSERR_INVALHANDLE
;
2943 mr
= WINMM_Pause(device
);
2945 LeaveCriticalSection(&device
->lock
);
2950 /**************************************************************************
2951 * waveOutReset [WINMM.@]
2953 UINT WINAPI
waveOutReset(HWAVEOUT hWaveOut
)
2955 TRACE("(%p)\n", hWaveOut
);
2957 return WINMM_Reset((HWAVE
)hWaveOut
);
2960 /**************************************************************************
2961 * waveOutRestart [WINMM.@]
2963 UINT WINAPI
waveOutRestart(HWAVEOUT hWaveOut
)
2965 WINMM_Device
*device
;
2968 TRACE("(%p)\n", hWaveOut
);
2970 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2972 if(!WINMM_ValidateAndLock(device
))
2973 return MMSYSERR_INVALHANDLE
;
2975 device
->stopped
= TRUE
;
2978 WOD_PushData(device
);
2980 mr
= WINMM_BeginPlaying(device
);
2982 LeaveCriticalSection(&device
->lock
);
2987 /**************************************************************************
2988 * waveOutGetPosition [WINMM.@]
2990 UINT WINAPI
waveOutGetPosition(HWAVEOUT hWaveOut
, LPMMTIME lpTime
,
2993 TRACE("(%p, %p, %u)\n", hWaveOut
, lpTime
, uSize
);
2995 if(!uSize
|| !lpTime
)
2996 return MMSYSERR_INVALPARAM
;
2998 if(uSize
< sizeof(MMTIME
))
2999 return MMSYSERR_ERROR
;
3001 return WINMM_GetPosition((HWAVE
)hWaveOut
, lpTime
);
3004 /**************************************************************************
3005 * waveOutGetPitch [WINMM.@]
3007 UINT WINAPI
waveOutGetPitch(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
3009 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
3010 return MMSYSERR_NOTSUPPORTED
;
3013 /**************************************************************************
3014 * waveOutSetPitch [WINMM.@]
3016 UINT WINAPI
waveOutSetPitch(HWAVEOUT hWaveOut
, DWORD dw
)
3018 TRACE("(%p, %08lx)\n", hWaveOut
, dw
);
3020 return MMSYSERR_NOTSUPPORTED
;
3023 /**************************************************************************
3024 * waveOutGetPlaybackRate [WINMM.@]
3026 UINT WINAPI
waveOutGetPlaybackRate(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
3028 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
3030 return MMSYSERR_NOTSUPPORTED
;
3033 /**************************************************************************
3034 * waveOutSetPlaybackRate [WINMM.@]
3036 UINT WINAPI
waveOutSetPlaybackRate(HWAVEOUT hWaveOut
, DWORD dw
)
3038 TRACE("(%p, %08lx)\n", hWaveOut
, dw
);
3040 return MMSYSERR_NOTSUPPORTED
;
3043 /**************************************************************************
3044 * waveOutGetVolume [WINMM.@]
3046 UINT WINAPI
waveOutGetVolume(HWAVEOUT hWaveOut
, DWORD
*out
)
3048 WINMM_Device
*device
;
3053 TRACE("(%p, %p)\n", hWaveOut
, out
);
3056 return MMSYSERR_INVALPARAM
;
3058 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3060 if(!WINMM_ValidateAndLock(device
))
3061 return MMSYSERR_INVALHANDLE
;
3063 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
3065 LeaveCriticalSection(&device
->lock
);
3066 WARN("GetChannelCount failed: %08lx\n", hr
);
3067 return MMSYSERR_ERROR
;
3070 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
3072 LeaveCriticalSection(&device
->lock
);
3073 return MMSYSERR_NOMEM
;
3076 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
3078 LeaveCriticalSection(&device
->lock
);
3079 HeapFree(GetProcessHeap(), 0, vols
);
3080 WARN("GetAllVolumes failed: %08lx\n", hr
);
3081 return MMSYSERR_ERROR
;
3084 LeaveCriticalSection(&device
->lock
);
3086 *out
= ((UINT16
)(vols
[0] * (DWORD
)0xFFFF));
3088 *out
|= ((UINT16
)(vols
[1] * (DWORD
)0xFFFF)) << 16;
3090 HeapFree(GetProcessHeap(), 0, vols
);
3092 return MMSYSERR_NOERROR
;
3095 /**************************************************************************
3096 * waveOutSetVolume [WINMM.@]
3098 UINT WINAPI
waveOutSetVolume(HWAVEOUT hWaveOut
, DWORD in
)
3100 WINMM_Device
*device
;
3105 TRACE("(%p, %08lx)\n", hWaveOut
, in
);
3107 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3109 if(!WINMM_ValidateAndLock(device
))
3110 return MMSYSERR_INVALHANDLE
;
3112 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
3114 LeaveCriticalSection(&device
->lock
);
3115 WARN("GetChannelCount failed: %08lx\n", hr
);
3116 return MMSYSERR_ERROR
;
3119 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
3121 LeaveCriticalSection(&device
->lock
);
3122 return MMSYSERR_NOMEM
;
3125 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
3127 LeaveCriticalSection(&device
->lock
);
3128 HeapFree(GetProcessHeap(), 0, vols
);
3129 WARN("GetAllVolumes failed: %08lx\n", hr
);
3130 return MMSYSERR_ERROR
;
3133 vols
[0] = (float)((in
& 0xFFFF) / (float)0xFFFF);
3135 vols
[1] = (float)((in
>> 16) / (float)0xFFFF);
3137 hr
= IAudioStreamVolume_SetAllVolumes(device
->volume
, channels
, vols
);
3139 LeaveCriticalSection(&device
->lock
);
3140 HeapFree(GetProcessHeap(), 0, vols
);
3141 WARN("SetAllVolumes failed: %08lx\n", hr
);
3142 return MMSYSERR_ERROR
;
3145 LeaveCriticalSection(&device
->lock
);
3147 HeapFree(GetProcessHeap(), 0, vols
);
3149 return MMSYSERR_NOERROR
;
3152 /**************************************************************************
3153 * waveOutGetID [WINMM.@]
3155 UINT WINAPI
waveOutGetID(HWAVEOUT hWaveOut
, UINT
* lpuDeviceID
)
3157 WINMM_Device
*device
;
3161 TRACE("(%p, %p)\n", hWaveOut
, lpuDeviceID
);
3164 return MMSYSERR_INVALPARAM
;
3166 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3167 if(!WINMM_ValidateAndLock(device
))
3168 return MMSYSERR_INVALHANDLE
;
3170 LeaveCriticalSection(&device
->lock
);
3172 WINMM_DecomposeHWAVE((HWAVE
)hWaveOut
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3174 return MMSYSERR_NOERROR
;
3177 static UINT
WINMM_QueryInstanceIDSize(UINT device
, DWORD_PTR
*len
, BOOL is_out
)
3180 WINMM_MMDevice
**devices
;
3182 TRACE("(%u, %p, %d)\n", device
, len
, is_out
);
3185 count
= g_outmmdevices_count
;
3186 devices
= g_out_map
;
3188 count
= g_inmmdevices_count
;
3193 return MMSYSERR_INVALHANDLE
;
3195 EnterCriticalSection(&g_devthread_lock
);
3196 *len
= (lstrlenW(devices
[device
]->dev_id
) + 1) * sizeof(WCHAR
);
3197 LeaveCriticalSection(&g_devthread_lock
);
3199 return MMSYSERR_NOERROR
;
3202 static UINT
WINMM_QueryInstanceID(UINT device
, WCHAR
*str
, DWORD_PTR len
,
3206 WINMM_MMDevice
**devices
;
3208 TRACE("(%u, %p, %d)\n", device
, str
, is_out
);
3211 count
= g_outmmdevices_count
;
3212 devices
= g_out_map
;
3214 count
= g_inmmdevices_count
;
3219 return MMSYSERR_INVALHANDLE
;
3221 EnterCriticalSection(&g_devthread_lock
);
3222 id_len
= (lstrlenW(devices
[device
]->dev_id
) + 1) * sizeof(WCHAR
);
3224 LeaveCriticalSection(&g_devthread_lock
);
3225 return MMSYSERR_ERROR
;
3228 memcpy(str
, devices
[device
]->dev_id
, id_len
);
3229 LeaveCriticalSection(&g_devthread_lock
);
3231 return MMSYSERR_NOERROR
;
3234 static UINT
get_device_interface(UINT msg
, BOOL is_out
, UINT index
, WCHAR
*out
, ULONG
*out_len
)
3236 WINMM_QueryInterfaceInfo info
;
3239 if(!WINMM_StartDevicesThread())
3240 return MMSYSERR_NODRIVER
;
3242 info
.is_out
= is_out
;
3245 info
.len_bytes
= out_len
;
3247 ret
= SendMessageW(g_devices_hwnd
, msg
, (DWORD_PTR
)&info
, 0);
3248 InterlockedDecrement(&g_devthread_token
);
3252 /**************************************************************************
3253 * waveOutMessage [WINMM.@]
3255 UINT WINAPI
waveOutMessage(HWAVEOUT hWaveOut
, UINT uMessage
,
3256 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3258 TRACE("(%p, %u, %Ix, %Ix)\n", hWaveOut
, uMessage
, dwParam1
, dwParam2
);
3261 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3262 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut
),
3263 (DWORD_PTR
*)dwParam1
, TRUE
);
3264 case DRV_QUERYFUNCTIONINSTANCEID
:
3265 return WINMM_QueryInstanceID(HandleToULong(hWaveOut
), (WCHAR
*)dwParam1
, dwParam2
, TRUE
);
3266 case DRV_QUERYDEVICEINTERFACESIZE
:
3267 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE
, TRUE
, HandleToULong(hWaveOut
),
3268 NULL
, (ULONG
*)dwParam1
);
3269 case DRV_QUERYDEVICEINTERFACE
:
3271 ULONG size
= dwParam2
;
3272 return get_device_interface(DRV_QUERYDEVICEINTERFACE
, TRUE
, HandleToULong(hWaveOut
),
3273 (WCHAR
*)dwParam1
, &size
);
3275 case DRV_QUERYMAPPABLE
:
3276 return MMSYSERR_NOERROR
;
3277 case DRVM_MAPPER_PREFERRED_GET
:
3278 if(!dwParam1
|| !dwParam2
)
3279 return MMSYSERR_INVALPARAM
;
3281 if(g_outmmdevices_count
> 0)
3282 /* Device 0 is always the default device */
3283 *(DWORD
*)dwParam1
= 0;
3285 *(DWORD
*)dwParam1
= -1;
3288 *(DWORD
*)dwParam2
= 0;
3290 return MMSYSERR_NOERROR
;
3293 TRACE("Message not supported: %u\n", uMessage
);
3295 return MMSYSERR_NOTSUPPORTED
;
3298 /**************************************************************************
3299 * waveInGetNumDevs [WINMM.@]
3301 UINT WINAPI
waveInGetNumDevs(void)
3303 HRESULT hr
= WINMM_InitMMDevices();
3307 TRACE("count: %u\n", g_inmmdevices_count
);
3309 return g_inmmdevices_count
;
3312 /**************************************************************************
3313 * waveInGetDevCapsW [WINMM.@]
3315 UINT WINAPI
waveInGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEINCAPSW lpCaps
, UINT uSize
)
3317 WAVEINCAPSW mapper_caps
, *caps
;
3320 TRACE("(%Iu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3322 hr
= WINMM_InitMMDevices();
3324 return MMSYSERR_NODRIVER
;
3327 return MMSYSERR_INVALPARAM
;
3329 if(WINMM_IsMapper(uDeviceID
)){
3330 mapper_caps
.wMid
= 0xFF;
3331 mapper_caps
.wPid
= 0xFF;
3332 mapper_caps
.vDriverVersion
= 0x00010001;
3333 mapper_caps
.dwFormats
= 0xFFFFFFFF;
3334 mapper_caps
.wReserved1
= 0;
3335 mapper_caps
.wChannels
= 2;
3336 LoadStringW(hWinMM32Instance
, IDS_MAPPER_NAME
, mapper_caps
.szPname
, MAXPNAMELEN
);
3338 caps
= &mapper_caps
;
3340 if(uDeviceID
>= g_inmmdevices_count
){
3341 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)uDeviceID
);
3343 if(!WINMM_ValidateAndLock(device
))
3344 return MMSYSERR_BADDEVICEID
;
3346 caps
= &device
->parent
->in_caps
;
3348 LeaveCriticalSection(&device
->lock
);
3350 caps
= &read_map(g_in_map
, uDeviceID
)->in_caps
;
3354 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
3356 return MMSYSERR_NOERROR
;
3359 /**************************************************************************
3360 * waveInGetDevCapsA [WINMM.@]
3362 UINT WINAPI
waveInGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEINCAPSA lpCaps
, UINT uSize
)
3367 TRACE("(%Iu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3370 return MMSYSERR_INVALPARAM
;
3372 ret
= waveInGetDevCapsW(uDeviceID
, &wicW
, sizeof(wicW
));
3374 if (ret
== MMSYSERR_NOERROR
) {
3376 wicA
.wMid
= wicW
.wMid
;
3377 wicA
.wPid
= wicW
.wPid
;
3378 wicA
.vDriverVersion
= wicW
.vDriverVersion
;
3379 WideCharToMultiByte( CP_ACP
, 0, wicW
.szPname
, -1, wicA
.szPname
,
3380 sizeof(wicA
.szPname
), NULL
, NULL
);
3381 wicA
.dwFormats
= wicW
.dwFormats
;
3382 wicA
.wChannels
= wicW
.wChannels
;
3383 wicA
.wReserved1
= wicW
.wReserved1
;
3384 memcpy(lpCaps
, &wicA
, min(uSize
, sizeof(wicA
)));
3389 /**************************************************************************
3390 * waveInOpen [WINMM.@]
3392 MMRESULT WINAPI
waveInOpen(HWAVEIN
* lphWaveIn
, UINT uDeviceID
,
3393 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
3394 DWORD_PTR dwInstance
, DWORD dwFlags
)
3397 WINMM_OpenInfo info
;
3398 WINMM_CBInfo cb_info
;
3400 TRACE("(%p, %x, %p, %Ix, %Ix, %08lx)\n", lphWaveIn
, uDeviceID
, lpFormat
,
3401 dwCallback
, dwInstance
, dwFlags
);
3403 if(!lphWaveIn
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
3404 return MMSYSERR_INVALPARAM
;
3406 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
3407 if(res
!= MMSYSERR_NOERROR
)
3410 if(!WINMM_StartDevicesThread())
3411 return MMSYSERR_NODRIVER
;
3414 info
.format
= (WAVEFORMATEX
*)lpFormat
;
3415 info
.callback
= dwCallback
;
3416 info
.cb_user
= dwInstance
;
3417 info
.req_device
= uDeviceID
;
3418 info
.flags
= dwFlags
;
3421 res
= SendMessageW(g_devices_hwnd
, WIDM_OPEN
, (DWORD_PTR
)&info
, 0);
3422 InterlockedDecrement(&g_devthread_token
);
3423 if(res
!= MMSYSERR_NOERROR
|| (dwFlags
& WAVE_FORMAT_QUERY
))
3427 *lphWaveIn
= (HWAVEIN
)info
.handle
;
3429 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
3430 cb_info
.callback
= dwCallback
;
3431 cb_info
.user
= dwInstance
;
3432 cb_info
.hwave
= info
.handle
;
3434 WINMM_NotifyClient(&cb_info
, WIM_OPEN
, 0, 0);
3439 /**************************************************************************
3440 * waveInClose [WINMM.@]
3442 UINT WINAPI
waveInClose(HWAVEIN hWaveIn
)
3444 WINMM_Device
*device
;
3445 WINMM_CBInfo cb_info
;
3448 TRACE("(%p)\n", hWaveIn
);
3450 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3452 if(!WINMM_ValidateAndLock(device
))
3453 return MMSYSERR_INVALHANDLE
;
3455 cb_info
= device
->cb_info
;
3457 LeaveCriticalSection(&device
->lock
);
3459 res
= SendMessageW(g_devices_hwnd
, WIDM_CLOSE
, (WPARAM
)hWaveIn
, 0);
3461 if(res
== MMSYSERR_NOERROR
)
3462 WINMM_NotifyClient(&cb_info
, WIM_CLOSE
, 0, 0);
3467 /**************************************************************************
3468 * waveInPrepareHeader [WINMM.@]
3470 UINT WINAPI
waveInPrepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3473 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3475 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3476 return MMSYSERR_INVALPARAM
;
3478 if(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
)
3479 return MMSYSERR_NOERROR
;
3481 return WINMM_PrepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3484 /**************************************************************************
3485 * waveInUnprepareHeader [WINMM.@]
3487 UINT WINAPI
waveInUnprepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3490 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3492 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3493 return MMSYSERR_INVALPARAM
;
3495 if(lpWaveInHdr
->dwFlags
& WHDR_INQUEUE
)
3496 return WAVERR_STILLPLAYING
;
3498 if(!(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
))
3499 return MMSYSERR_NOERROR
;
3501 return WINMM_UnprepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3504 /**************************************************************************
3505 * waveInAddBuffer [WINMM.@]
3507 UINT WINAPI
waveInAddBuffer(HWAVEIN hWaveIn
, WAVEHDR
*header
, UINT uSize
)
3509 WINMM_Device
*device
;
3511 TRACE("(%p, %p, %u)\n", hWaveIn
, header
, uSize
);
3513 if(!header
|| uSize
< sizeof(WAVEHDR
))
3514 return MMSYSERR_INVALPARAM
;
3516 if(!(header
->dwFlags
& WHDR_PREPARED
))
3517 return WAVERR_UNPREPARED
;
3519 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3521 if(!WINMM_ValidateAndLock(device
))
3522 return MMSYSERR_INVALHANDLE
;
3525 device
->first
= device
->last
= header
;
3527 device
->last
->lpNext
= header
;
3528 device
->last
= header
;
3531 header
->dwBytesRecorded
= 0;
3532 header
->lpNext
= NULL
;
3533 header
->dwFlags
&= ~WHDR_DONE
;
3534 header
->dwFlags
|= WHDR_INQUEUE
;
3536 LeaveCriticalSection(&device
->lock
);
3538 return MMSYSERR_NOERROR
;
3541 /**************************************************************************
3542 * waveInReset [WINMM.@]
3544 UINT WINAPI
waveInReset(HWAVEIN hWaveIn
)
3546 TRACE("(%p)\n", hWaveIn
);
3548 return WINMM_Reset((HWAVE
)hWaveIn
);
3551 /**************************************************************************
3552 * waveInStart [WINMM.@]
3554 UINT WINAPI
waveInStart(HWAVEIN hWaveIn
)
3556 WINMM_Device
*device
;
3559 TRACE("(%p)\n", hWaveIn
);
3561 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3563 if(!WINMM_ValidateAndLock(device
))
3564 return MMSYSERR_INVALHANDLE
;
3566 mr
= WINMM_BeginPlaying(device
);
3568 LeaveCriticalSection(&device
->lock
);
3573 /**************************************************************************
3574 * waveInStop [WINMM.@]
3576 UINT WINAPI
waveInStop(HWAVEIN hWaveIn
)
3578 WINMM_CBInfo cb_info
;
3579 WINMM_Device
*device
;
3583 TRACE("(%p)\n", hWaveIn
);
3585 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3587 if(!WINMM_ValidateAndLock(device
))
3588 return MMSYSERR_INVALHANDLE
;
3590 hr
= WINMM_Pause(device
);
3592 LeaveCriticalSection(&device
->lock
);
3593 return MMSYSERR_ERROR
;
3595 device
->stopped
= TRUE
;
3597 buf
= device
->first
;
3598 if(buf
&& buf
->dwBytesRecorded
> 0){
3599 device
->first
= buf
->lpNext
;
3603 cb_info
= device
->cb_info
;
3605 LeaveCriticalSection(&device
->lock
);
3608 buf
->dwFlags
&= ~WHDR_INQUEUE
;
3609 buf
->dwFlags
|= WHDR_DONE
;
3610 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)buf
, 0);
3613 return MMSYSERR_NOERROR
;
3616 /**************************************************************************
3617 * waveInGetPosition [WINMM.@]
3619 UINT WINAPI
waveInGetPosition(HWAVEIN hWaveIn
, LPMMTIME lpTime
,
3622 TRACE("(%p, %p, %u)\n", hWaveIn
, lpTime
, uSize
);
3624 if(!uSize
|| !lpTime
)
3625 return MMSYSERR_INVALPARAM
;
3627 if(uSize
< sizeof(MMTIME
))
3628 return MMSYSERR_ERROR
;
3630 return WINMM_GetPosition((HWAVE
)hWaveIn
, lpTime
);
3633 /**************************************************************************
3634 * waveInGetID [WINMM.@]
3636 UINT WINAPI
waveInGetID(HWAVEIN hWaveIn
, UINT
* lpuDeviceID
)
3640 WINMM_Device
*device
;
3642 TRACE("(%p, %p)\n", hWaveIn
, lpuDeviceID
);
3645 return MMSYSERR_INVALPARAM
;
3647 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3648 if(!WINMM_ValidateAndLock(device
))
3649 return MMSYSERR_INVALHANDLE
;
3651 LeaveCriticalSection(&device
->lock
);
3653 WINMM_DecomposeHWAVE((HWAVE
)hWaveIn
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3655 return MMSYSERR_NOERROR
;
3658 /**************************************************************************
3659 * waveInMessage [WINMM.@]
3661 UINT WINAPI
waveInMessage(HWAVEIN hWaveIn
, UINT uMessage
,
3662 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3664 TRACE("(%p, %u, %Id, %Id)\n", hWaveIn
, uMessage
, dwParam1
, dwParam2
);
3667 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3668 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn
),
3669 (DWORD_PTR
*)dwParam1
, FALSE
);
3670 case DRV_QUERYFUNCTIONINSTANCEID
:
3671 return WINMM_QueryInstanceID(HandleToULong(hWaveIn
), (WCHAR
*)dwParam1
, dwParam2
, FALSE
);
3672 case DRV_QUERYDEVICEINTERFACESIZE
:
3673 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE
, FALSE
, HandleToULong(hWaveIn
),
3674 NULL
, (ULONG
*)dwParam1
);
3675 case DRV_QUERYDEVICEINTERFACE
:
3677 ULONG size
= dwParam2
;
3678 return get_device_interface(DRV_QUERYDEVICEINTERFACE
, FALSE
, HandleToULong(hWaveIn
),
3679 (WCHAR
*)dwParam1
, &size
);
3681 case DRV_QUERYMAPPABLE
:
3682 return MMSYSERR_NOERROR
;
3683 case DRVM_MAPPER_PREFERRED_GET
:
3684 if(!dwParam1
|| !dwParam2
)
3685 return MMSYSERR_INVALPARAM
;
3687 if(g_inmmdevices_count
> 0)
3688 /* Device 0 is always the default device */
3689 *(DWORD
*)dwParam1
= 0;
3691 *(DWORD
*)dwParam1
= -1;
3694 *(DWORD
*)dwParam2
= 0;
3696 return MMSYSERR_NOERROR
;
3699 TRACE("Message not supported: %u\n", uMessage
);
3701 return MMSYSERR_NOTSUPPORTED
;
3704 UINT WINAPI
mixerGetNumDevs(void)
3710 hr
= WINMM_InitMMDevices();
3714 return g_outmmdevices_count
+ g_inmmdevices_count
;
3717 /**************************************************************************
3718 * mixerGetDevCapsA [WINMM.@]
3720 UINT WINAPI
mixerGetDevCapsA(UINT_PTR uDeviceID
, LPMIXERCAPSA lpCaps
, UINT uSize
)
3725 TRACE("(%Iu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3728 return MMSYSERR_INVALPARAM
;
3730 ret
= mixerGetDevCapsW(uDeviceID
, &micW
, sizeof(micW
));
3732 if (ret
== MMSYSERR_NOERROR
) {
3734 micA
.wMid
= micW
.wMid
;
3735 micA
.wPid
= micW
.wPid
;
3736 micA
.vDriverVersion
= micW
.vDriverVersion
;
3737 WideCharToMultiByte( CP_ACP
, 0, micW
.szPname
, -1, micA
.szPname
,
3738 sizeof(micA
.szPname
), NULL
, NULL
);
3739 micA
.fdwSupport
= micW
.fdwSupport
;
3740 micA
.cDestinations
= micW
.cDestinations
;
3741 memcpy(lpCaps
, &micA
, min(uSize
, sizeof(micA
)));
3746 /**************************************************************************
3747 * mixerGetDevCapsW [WINMM.@]
3749 UINT WINAPI
mixerGetDevCapsW(UINT_PTR uDeviceID
, LPMIXERCAPSW lpCaps
, UINT uSize
)
3751 WINMM_MMDevice
*mmdevice
;
3755 TRACE("(%Iu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3757 hr
= WINMM_InitMMDevices();
3759 return MMSYSERR_NODRIVER
;
3762 return MMSYSERR_INVALPARAM
;
3765 return MMSYSERR_NOERROR
;
3767 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3768 mmdevice
= WINMM_GetMixerMMDevice((HMIXEROBJ
)uDeviceID
,
3769 MIXER_OBJECTF_MIXER
, NULL
);
3770 else if(uDeviceID
< g_outmmdevices_count
)
3771 mmdevice
= read_map(g_out_map
, uDeviceID
);
3773 mmdevice
= read_map(g_in_map
, uDeviceID
- g_outmmdevices_count
);
3776 return MMSYSERR_BADDEVICEID
;
3778 if(mmdevice
->dataflow
== eRender
)
3779 memcpy(caps
.szPname
, mmdevice
->out_caps
.szPname
, sizeof(caps
.szPname
));
3781 memcpy(caps
.szPname
, mmdevice
->in_caps
.szPname
, sizeof(caps
.szPname
));
3785 caps
.vDriverVersion
= 0x00010001;
3786 caps
.fdwSupport
= 0;
3787 caps
.cDestinations
= 1;
3789 memcpy(lpCaps
, &caps
, uSize
);
3791 return MMSYSERR_NOERROR
;
3794 /**************************************************************************
3795 * mixerOpen [WINMM.@]
3797 UINT WINAPI
mixerOpen(LPHMIXER lphMix
, UINT uDeviceID
, DWORD_PTR dwCallback
,
3798 DWORD_PTR dwInstance
, DWORD fdwOpen
)
3800 WINMM_MMDevice
*mmdevice
;
3804 TRACE("(%p, %d, %Ix, %Ix, %lx)\n", lphMix
, uDeviceID
, dwCallback
,
3805 dwInstance
, fdwOpen
);
3807 hr
= WINMM_InitMMDevices();
3809 return MMSYSERR_NODRIVER
;
3812 return MMSYSERR_INVALPARAM
;
3814 mr
= WINMM_CheckCallback(dwCallback
, fdwOpen
, TRUE
);
3815 if(mr
!= MMSYSERR_NOERROR
)
3818 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3819 return MMSYSERR_BADDEVICEID
;
3821 if(uDeviceID
< g_outmmdevices_count
){
3822 mmdevice
= read_map(g_out_map
, uDeviceID
);
3823 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
, TRUE
,
3824 mmdevice
->mixer_count
);
3826 mmdevice
= read_map(g_in_map
, uDeviceID
- g_outmmdevices_count
);
3827 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
- g_outmmdevices_count
,
3828 FALSE
, mmdevice
->mixer_count
);
3831 ++mmdevice
->mixer_count
;
3833 return MMSYSERR_NOERROR
;
3836 /**************************************************************************
3837 * mixerClose [WINMM.@]
3839 UINT WINAPI
mixerClose(HMIXER hMix
)
3841 TRACE("(%p)\n", hMix
);
3843 return MMSYSERR_NOERROR
;
3846 /**************************************************************************
3847 * mixerGetID [WINMM.@]
3849 UINT WINAPI
mixerGetID(HMIXEROBJ hmix
, LPUINT lpid
, DWORD fdwID
)
3851 WINMM_MMDevice
*mmdevice
;
3854 TRACE("(%p, %p, %lx)\n", hmix
, lpid
, fdwID
);
3856 hr
= WINMM_InitMMDevices();
3858 return MMSYSERR_NODRIVER
;
3861 return MMSYSERR_INVALPARAM
;
3863 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwID
, lpid
);
3865 return MMSYSERR_INVALHANDLE
;
3867 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3868 *lpid
+= g_outmmdevices_count
;
3870 return MMSYSERR_NOERROR
;
3873 /**************************************************************************
3874 * mixerGetControlDetailsW [WINMM.@]
3876 UINT WINAPI
mixerGetControlDetailsW(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdW
,
3879 WINMM_ControlDetails details
;
3881 TRACE("(%p, %p, %lx)\n", hmix
, lpmcdW
, fdwDetails
);
3883 if(!WINMM_StartDevicesThread())
3884 return MMSYSERR_NODRIVER
;
3886 if(!lpmcdW
|| !lpmcdW
->paDetails
)
3887 return MMSYSERR_INVALPARAM
;
3889 TRACE("dwControlID: %lu\n", lpmcdW
->dwControlID
);
3891 details
.hmix
= hmix
;
3892 details
.details
= lpmcdW
;
3893 details
.flags
= fdwDetails
;
3895 return SendMessageW(g_devices_hwnd
, MXDM_GETCONTROLDETAILS
,
3896 (DWORD_PTR
)&details
, 0);
3899 /**************************************************************************
3900 * mixerGetControlDetailsA [WINMM.@]
3902 UINT WINAPI
mixerGetControlDetailsA(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdA
,
3905 UINT ret
= MMSYSERR_NOTSUPPORTED
;
3907 TRACE("(%p, %p, %08lx)\n", hmix
, lpmcdA
, fdwDetails
);
3909 if (lpmcdA
== NULL
|| lpmcdA
->cbStruct
!= sizeof(*lpmcdA
))
3910 return MMSYSERR_INVALPARAM
;
3912 switch (fdwDetails
& MIXER_GETCONTROLDETAILSF_QUERYMASK
) {
3913 case MIXER_GETCONTROLDETAILSF_VALUE
:
3914 /* can safely use A structure as it is, no string inside */
3915 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3917 case MIXER_GETCONTROLDETAILSF_LISTTEXT
:
3919 MIXERCONTROLDETAILS_LISTTEXTA
*pDetailsA
= lpmcdA
->paDetails
;
3920 MIXERCONTROLDETAILS_LISTTEXTW
*pDetailsW
;
3921 int size
= max(1, lpmcdA
->cChannels
) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3924 if (lpmcdA
->u
.cMultipleItems
!= 0) {
3925 size
*= lpmcdA
->u
.cMultipleItems
;
3927 pDetailsW
= HeapAlloc(GetProcessHeap(), 0, size
);
3928 lpmcdA
->paDetails
= pDetailsW
;
3929 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3930 /* set up lpmcd->paDetails */
3931 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3932 /* copy from lpmcd->paDetails back to paDetailsW; */
3933 if (ret
== MMSYSERR_NOERROR
) {
3934 for (i
= 0; i
< lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
; i
++) {
3935 pDetailsA
->dwParam1
= pDetailsW
->dwParam1
;
3936 pDetailsA
->dwParam2
= pDetailsW
->dwParam2
;
3937 WideCharToMultiByte( CP_ACP
, 0, pDetailsW
->szName
, -1,
3939 sizeof(pDetailsA
->szName
), NULL
, NULL
);
3943 pDetailsA
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3944 pDetailsW
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3946 HeapFree(GetProcessHeap(), 0, pDetailsW
);
3947 lpmcdA
->paDetails
= pDetailsA
;
3948 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTA
);
3952 WARN("Unsupported fdwDetails=0x%08lx\n", fdwDetails
);
3958 /**************************************************************************
3959 * mixerGetLineControlsA [WINMM.@]
3961 UINT WINAPI
mixerGetLineControlsA(HMIXEROBJ hmix
, LPMIXERLINECONTROLSA lpmlcA
,
3964 MIXERLINECONTROLSW mlcW
;
3968 TRACE("(%p, %p, %lx)\n", hmix
, lpmlcA
, fdwControls
);
3970 if (lpmlcA
== NULL
|| lpmlcA
->cbStruct
!= sizeof(*lpmlcA
) ||
3971 lpmlcA
->cbmxctrl
!= sizeof(MIXERCONTROLA
))
3972 return MMSYSERR_INVALPARAM
;
3974 mlcW
.cbStruct
= sizeof(mlcW
);
3975 mlcW
.dwLineID
= lpmlcA
->dwLineID
;
3976 mlcW
.u
.dwControlID
= lpmlcA
->u
.dwControlID
;
3977 mlcW
.u
.dwControlType
= lpmlcA
->u
.dwControlType
;
3979 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3980 the control count is assumed to be 1 - This is relied upon by a game,
3981 "Dynomite Deluze" */
3982 if (MIXER_GETLINECONTROLSF_ONEBYTYPE
== (fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
)) {
3985 mlcW
.cControls
= lpmlcA
->cControls
;
3987 mlcW
.cbmxctrl
= sizeof(MIXERCONTROLW
);
3988 mlcW
.pamxctrl
= HeapAlloc(GetProcessHeap(), 0,
3989 mlcW
.cControls
* mlcW
.cbmxctrl
);
3991 ret
= mixerGetLineControlsW(hmix
, &mlcW
, fdwControls
);
3993 if (ret
== MMSYSERR_NOERROR
) {
3994 lpmlcA
->dwLineID
= mlcW
.dwLineID
;
3995 lpmlcA
->u
.dwControlID
= mlcW
.u
.dwControlID
;
3996 lpmlcA
->u
.dwControlType
= mlcW
.u
.dwControlType
;
3998 for (i
= 0; i
< mlcW
.cControls
; i
++) {
3999 lpmlcA
->pamxctrl
[i
].cbStruct
= sizeof(MIXERCONTROLA
);
4000 lpmlcA
->pamxctrl
[i
].dwControlID
= mlcW
.pamxctrl
[i
].dwControlID
;
4001 lpmlcA
->pamxctrl
[i
].dwControlType
= mlcW
.pamxctrl
[i
].dwControlType
;
4002 lpmlcA
->pamxctrl
[i
].fdwControl
= mlcW
.pamxctrl
[i
].fdwControl
;
4003 lpmlcA
->pamxctrl
[i
].cMultipleItems
= mlcW
.pamxctrl
[i
].cMultipleItems
;
4004 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szShortName
, -1,
4005 lpmlcA
->pamxctrl
[i
].szShortName
,
4006 sizeof(lpmlcA
->pamxctrl
[i
].szShortName
), NULL
, NULL
);
4007 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szName
, -1,
4008 lpmlcA
->pamxctrl
[i
].szName
,
4009 sizeof(lpmlcA
->pamxctrl
[i
].szName
), NULL
, NULL
);
4010 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
4011 * sizeof(mlcW.pamxctrl[i].Bounds) */
4012 memcpy(&lpmlcA
->pamxctrl
[i
].Bounds
, &mlcW
.pamxctrl
[i
].Bounds
,
4013 sizeof(mlcW
.pamxctrl
[i
].Bounds
));
4014 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
4015 * sizeof(mlcW.pamxctrl[i].Metrics) */
4016 memcpy(&lpmlcA
->pamxctrl
[i
].Metrics
, &mlcW
.pamxctrl
[i
].Metrics
,
4017 sizeof(mlcW
.pamxctrl
[i
].Metrics
));
4021 HeapFree(GetProcessHeap(), 0, mlcW
.pamxctrl
);
4026 static UINT
WINMM_GetVolumeLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
4027 MIXERCONTROLW
*ctl
, DWORD flags
)
4029 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 0 : 2;
4030 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_VOLUME
;
4031 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
4032 ctl
->cMultipleItems
= 0;
4033 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, ctl
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4034 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, ctl
->szName
, MIXER_LONG_NAME_CHARS
);
4035 ctl
->Bounds
.s1
.dwMinimum
= 0;
4036 ctl
->Bounds
.s1
.dwMaximum
= 0xFFFF;
4037 ctl
->Metrics
.cSteps
= 192;
4039 return MMSYSERR_NOERROR
;
4042 static UINT
WINMM_GetMuteLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
4043 MIXERCONTROLW
*ctl
, DWORD flags
)
4045 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 1 : 3;
4046 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_MUTE
;
4047 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
4048 ctl
->cMultipleItems
= 0;
4049 LoadStringW(hWinMM32Instance
, IDS_MUTE
, ctl
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4050 LoadStringW(hWinMM32Instance
, IDS_MUTE
, ctl
->szName
, MIXER_LONG_NAME_CHARS
);
4051 ctl
->Bounds
.s1
.dwMinimum
= 0;
4052 ctl
->Bounds
.s1
.dwMaximum
= 1;
4053 ctl
->Metrics
.cSteps
= 0;
4055 return MMSYSERR_NOERROR
;
4058 /**************************************************************************
4059 * mixerGetLineControlsW [WINMM.@]
4061 UINT WINAPI
mixerGetLineControlsW(HMIXEROBJ hmix
, LPMIXERLINECONTROLSW lpmlcW
,
4064 WINMM_MMDevice
*mmdevice
;
4067 TRACE("(%p, %p, %08lx)\n", hmix
, lpmlcW
, fdwControls
);
4069 hr
= WINMM_InitMMDevices();
4071 return MMSYSERR_NODRIVER
;
4073 if(fdwControls
& ~(MIXER_GETLINECONTROLSF_ALL
|
4074 MIXER_GETLINECONTROLSF_ONEBYID
|
4075 MIXER_GETLINECONTROLSF_ONEBYTYPE
|
4076 MIXER_OBJECTF_HMIXER
|
4077 MIXER_OBJECTF_MIXER
)){
4078 WARN("Unknown GetLineControls flag: %lx\n", fdwControls
);
4079 return MMSYSERR_INVALFLAG
;
4082 if(!lpmlcW
|| lpmlcW
->cbStruct
< sizeof(*lpmlcW
) || !lpmlcW
->pamxctrl
)
4083 return MMSYSERR_INVALPARAM
;
4085 TRACE("dwLineID: %lu\n", lpmlcW
->dwLineID
);
4086 TRACE("dwControl: %lx\n", lpmlcW
->u
.dwControlID
);
4087 TRACE("cControls: %lu\n", lpmlcW
->cControls
);
4089 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwControls
, NULL
);
4091 return MMSYSERR_INVALHANDLE
;
4093 switch(fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
){
4094 case MIXER_GETLINECONTROLSF_ALL
:
4095 if(lpmlcW
->cControls
!= 2)
4096 return MMSYSERR_INVALPARAM
;
4097 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4098 return MMSYSERR_INVALPARAM
;
4099 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4100 return MIXERR_INVALLINE
;
4101 WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4102 &lpmlcW
->pamxctrl
[0], fdwControls
);
4103 WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4104 &lpmlcW
->pamxctrl
[1], fdwControls
);
4105 return MMSYSERR_NOERROR
;
4106 case MIXER_GETLINECONTROLSF_ONEBYID
:
4107 if(lpmlcW
->cControls
!= 1)
4108 return MMSYSERR_INVALPARAM
;
4109 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4110 return MMSYSERR_INVALPARAM
;
4111 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4112 return MIXERR_INVALLINE
;
4113 if(lpmlcW
->u
.dwControlID
== 0)
4114 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4115 lpmlcW
->pamxctrl
, fdwControls
);
4116 if(lpmlcW
->u
.dwControlID
== 1)
4117 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4118 lpmlcW
->pamxctrl
, fdwControls
);
4119 return MMSYSERR_NOTSUPPORTED
;
4120 case MIXER_GETLINECONTROLSF_ONEBYTYPE
:
4121 if(lpmlcW
->cControls
!= 1)
4122 return MMSYSERR_INVALPARAM
;
4123 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4124 return MMSYSERR_INVALPARAM
;
4125 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4126 return MIXERR_INVALLINE
;
4127 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_VOLUME
)
4128 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4129 lpmlcW
->pamxctrl
, fdwControls
);
4130 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_MUTE
)
4131 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4132 lpmlcW
->pamxctrl
, fdwControls
);
4133 return MMSYSERR_NOTSUPPORTED
;
4136 return MMSYSERR_NOTSUPPORTED
;
4139 static UINT
WINMM_GetSourceLineInfo(WINMM_MMDevice
*mmdevice
, UINT mmdev_index
,
4140 MIXERLINEW
*info
, DWORD flags
)
4143 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4146 if(info
->dwSource
!= 0)
4147 return MIXERR_INVALLINE
;
4149 info
->dwDestination
= 0;
4151 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
| MIXERLINE_LINEF_SOURCE
;
4152 info
->cConnections
= 0;
4153 info
->cControls
= 2;
4154 /* volume & mute always affect all channels, so claim 1 channel */
4155 info
->cChannels
= 1;
4156 info
->Target
.dwDeviceID
= mmdev_index
;
4157 info
->Target
.wMid
= ~0;
4158 info
->Target
.wPid
= ~0;
4159 info
->Target
.vDriverVersion
= 0;
4161 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, info
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4162 LoadStringW(hWinMM32Instance
, IDS_MASTER_VOLUME
, info
->szName
, MIXER_LONG_NAME_CHARS
);
4165 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
;
4166 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEOUT
;
4167 memcpy(info
->Target
.szPname
, mmdevice
->out_caps
.szPname
,
4168 sizeof(info
->Target
.szPname
));
4170 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
;
4171 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
4172 info
->Target
.szPname
[0] = '\0';
4175 return MMSYSERR_NOERROR
;
4178 static UINT
WINMM_GetDestinationLineInfo(WINMM_MMDevice
*mmdevice
,
4179 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4182 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4185 if(info
->dwDestination
!= 0)
4186 return MIXERR_INVALLINE
;
4188 info
->dwSource
= 0xFFFFFFFF;
4189 info
->dwLineID
= 0xFFFF0000;
4190 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
;
4191 info
->cConnections
= 1;
4192 info
->cControls
= 2;
4194 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, info
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4195 LoadStringW(hWinMM32Instance
, IDS_MASTER_VOLUME
, info
->szName
, MIXER_LONG_NAME_CHARS
);
4197 info
->Target
.dwDeviceID
= mmdev_index
;
4198 info
->Target
.wMid
= ~0;
4199 info
->Target
.wPid
= ~0;
4200 info
->Target
.vDriverVersion
= 0;
4203 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
;
4204 info
->cChannels
= mmdevice
->out_caps
.wChannels
;
4205 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
4206 info
->Target
.szPname
[0] = '\0';
4208 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_WAVEIN
;
4209 info
->cChannels
= mmdevice
->in_caps
.wChannels
;
4210 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEIN
;
4211 memcpy(info
->Target
.szPname
, mmdevice
->in_caps
.szPname
,
4212 sizeof(info
->Target
.szPname
));
4215 return MMSYSERR_NOERROR
;
4218 static UINT
WINMM_GetComponentTypeLineInfo(WINMM_MMDevice
*mmdevice
,
4219 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4222 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4225 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_WAVEIN
){
4227 return MIXERR_INVALLINE
;
4228 info
->dwDestination
= 0;
4229 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4232 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
){
4234 return MIXERR_INVALLINE
;
4235 info
->dwDestination
= 0;
4236 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4239 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
){
4241 return MIXERR_INVALLINE
;
4243 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4246 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
){
4248 return MIXERR_INVALLINE
;
4250 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4253 TRACE("Returning INVALLINE on this component type: %lu\n",
4254 info
->dwComponentType
);
4256 return MIXERR_INVALLINE
;
4259 static UINT
WINMM_GetLineIDLineInfo(WINMM_MMDevice
*mmdevice
,
4260 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4262 if(info
->dwLineID
== 0xFFFF0000){
4263 info
->dwDestination
= 0;
4264 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4267 if(info
->dwLineID
== 0){
4269 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4272 TRACE("Returning INVALLINE on this dwLineID: %lu\n", info
->dwLineID
);
4273 return MIXERR_INVALLINE
;
4276 /**************************************************************************
4277 * mixerGetLineInfoW [WINMM.@]
4279 UINT WINAPI
mixerGetLineInfoW(HMIXEROBJ hmix
, LPMIXERLINEW lpmliW
, DWORD fdwInfo
)
4282 WINMM_MMDevice
*mmdevice
;
4285 TRACE("(%p, %p, %lx)\n", hmix
, lpmliW
, fdwInfo
);
4287 hr
= WINMM_InitMMDevices();
4289 return MMSYSERR_NODRIVER
;
4291 if(!lpmliW
|| lpmliW
->cbStruct
< sizeof(MIXERLINEW
))
4292 return MMSYSERR_INVALPARAM
;
4294 TRACE("dwDestination: %lu\n", lpmliW
->dwDestination
);
4295 TRACE("dwSource: %lu\n", lpmliW
->dwSource
);
4296 TRACE("dwLineID: %lu\n", lpmliW
->dwLineID
);
4297 TRACE("fdwLine: 0x%lx\n", lpmliW
->fdwLine
);
4298 TRACE("dwComponentType: 0x%lx\n", lpmliW
->dwComponentType
);
4300 if(fdwInfo
& ~(MIXER_GETLINEINFOF_COMPONENTTYPE
|
4301 MIXER_GETLINEINFOF_DESTINATION
|
4302 MIXER_GETLINEINFOF_LINEID
|
4303 MIXER_GETLINEINFOF_SOURCE
|
4304 MIXER_GETLINEINFOF_TARGETTYPE
|
4305 MIXER_OBJECTF_HMIXER
|
4306 MIXER_OBJECTF_MIXER
)){
4307 WARN("Unknown GetLineInfo flag: %lx\n", fdwInfo
);
4308 return MMSYSERR_INVALFLAG
;
4311 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwInfo
, &mmdev_index
);
4313 return MMSYSERR_INVALHANDLE
;
4317 switch(fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
){
4318 case MIXER_GETLINEINFOF_DESTINATION
:
4319 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, lpmliW
,
4321 case MIXER_GETLINEINFOF_SOURCE
:
4322 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
4323 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
4324 return WINMM_GetComponentTypeLineInfo(mmdevice
, mmdev_index
, lpmliW
,
4326 case MIXER_GETLINEINFOF_LINEID
:
4327 return WINMM_GetLineIDLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
4328 case MIXER_GETLINEINFOF_TARGETTYPE
:
4329 FIXME("TARGETTYPE flag not implemented!\n");
4330 return MIXERR_INVALLINE
;
4333 TRACE("Returning INVALFLAG on these flags: %lx\n", fdwInfo
);
4334 return MMSYSERR_INVALFLAG
;
4337 /**************************************************************************
4338 * mixerGetLineInfoA [WINMM.@]
4340 UINT WINAPI
mixerGetLineInfoA(HMIXEROBJ hmix
, LPMIXERLINEA lpmliA
,
4346 TRACE("(%p, %p, %lx)\n", hmix
, lpmliA
, fdwInfo
);
4348 if (lpmliA
== NULL
|| lpmliA
->cbStruct
!= sizeof(*lpmliA
))
4349 return MMSYSERR_INVALPARAM
;
4351 mliW
.cbStruct
= sizeof(mliW
);
4352 switch (fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
) {
4353 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
4354 mliW
.dwComponentType
= lpmliA
->dwComponentType
;
4356 case MIXER_GETLINEINFOF_DESTINATION
:
4357 mliW
.dwDestination
= lpmliA
->dwDestination
;
4359 case MIXER_GETLINEINFOF_LINEID
:
4360 mliW
.dwLineID
= lpmliA
->dwLineID
;
4362 case MIXER_GETLINEINFOF_SOURCE
:
4363 mliW
.dwDestination
= lpmliA
->dwDestination
;
4364 mliW
.dwSource
= lpmliA
->dwSource
;
4366 case MIXER_GETLINEINFOF_TARGETTYPE
:
4367 mliW
.Target
.dwType
= lpmliA
->Target
.dwType
;
4368 mliW
.Target
.wMid
= lpmliA
->Target
.wMid
;
4369 mliW
.Target
.wPid
= lpmliA
->Target
.wPid
;
4370 mliW
.Target
.vDriverVersion
= lpmliA
->Target
.vDriverVersion
;
4371 MultiByteToWideChar(CP_ACP
, 0, lpmliA
->Target
.szPname
, -1, mliW
.Target
.szPname
, ARRAY_SIZE(mliW
.Target
.szPname
));
4374 WARN("Unsupported fdwControls=0x%08lx\n", fdwInfo
);
4375 return MMSYSERR_INVALFLAG
;
4378 ret
= mixerGetLineInfoW(hmix
, &mliW
, fdwInfo
);
4380 if(ret
== MMSYSERR_NOERROR
)
4382 lpmliA
->dwDestination
= mliW
.dwDestination
;
4383 lpmliA
->dwSource
= mliW
.dwSource
;
4384 lpmliA
->dwLineID
= mliW
.dwLineID
;
4385 lpmliA
->fdwLine
= mliW
.fdwLine
;
4386 lpmliA
->dwUser
= mliW
.dwUser
;
4387 lpmliA
->dwComponentType
= mliW
.dwComponentType
;
4388 lpmliA
->cChannels
= mliW
.cChannels
;
4389 lpmliA
->cConnections
= mliW
.cConnections
;
4390 lpmliA
->cControls
= mliW
.cControls
;
4391 WideCharToMultiByte( CP_ACP
, 0, mliW
.szShortName
, -1, lpmliA
->szShortName
,
4392 sizeof(lpmliA
->szShortName
), NULL
, NULL
);
4393 WideCharToMultiByte( CP_ACP
, 0, mliW
.szName
, -1, lpmliA
->szName
,
4394 sizeof(lpmliA
->szName
), NULL
, NULL
);
4395 lpmliA
->Target
.dwType
= mliW
.Target
.dwType
;
4396 lpmliA
->Target
.dwDeviceID
= mliW
.Target
.dwDeviceID
;
4397 lpmliA
->Target
.wMid
= mliW
.Target
.wMid
;
4398 lpmliA
->Target
.wPid
= mliW
.Target
.wPid
;
4399 lpmliA
->Target
.vDriverVersion
= mliW
.Target
.vDriverVersion
;
4400 WideCharToMultiByte( CP_ACP
, 0, mliW
.Target
.szPname
, -1, lpmliA
->Target
.szPname
,
4401 sizeof(lpmliA
->Target
.szPname
), NULL
, NULL
);
4406 /**************************************************************************
4407 * mixerSetControlDetails [WINMM.@]
4409 UINT WINAPI
mixerSetControlDetails(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcd
,
4412 WINMM_ControlDetails details
;
4415 TRACE("(%p, %p, %lx)\n", hmix
, lpmcd
, fdwDetails
);
4417 if((fdwDetails
& MIXER_SETCONTROLDETAILSF_QUERYMASK
) ==
4418 MIXER_SETCONTROLDETAILSF_CUSTOM
)
4419 return MMSYSERR_NOTSUPPORTED
;
4422 return MMSYSERR_INVALPARAM
;
4424 if(!WINMM_StartDevicesThread())
4425 return MMSYSERR_NODRIVER
;
4427 TRACE("dwControlID: %lu\n", lpmcd
->dwControlID
);
4429 details
.hmix
= hmix
;
4430 details
.details
= lpmcd
;
4431 details
.flags
= fdwDetails
;
4433 ret
= SendMessageW(g_devices_hwnd
, MXDM_SETCONTROLDETAILS
,
4434 (DWORD_PTR
)&details
, 0);
4435 InterlockedDecrement(&g_devthread_token
);
4439 /**************************************************************************
4440 * mixerMessage [WINMM.@]
4442 DWORD WINAPI
mixerMessage(HMIXER hmix
, UINT uMsg
, DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
4444 TRACE("(%p, %d, %Ix, %Ix)\n", hmix
, uMsg
, dwParam1
, dwParam2
);
4446 return MMSYSERR_NOTSUPPORTED
;