2 * Copyright 1993 Martin Ayotte
3 * 1998-2002 Eric Pouech
4 * 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
44 #include "mmdeviceapi.h"
45 #include "audioclient.h"
46 #include "audiopolicy.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(winmm
);
52 /* HWAVE (and HMIXER) format:
54 * XXXX... 1FDD DDDD IIII IIII
55 * X = unused (must be 0)
56 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
57 * F = flow direction (0 = IN, 1 = OUT)
58 * D = index into g_out_mmdevices, or all 1s for the MAPPER device
59 * I = index in the mmdevice's devices array
61 * Two reasons that we don't just use pointers:
62 * - HWAVEs must fit into 16 bits for compatibility with old applications.
63 * - We must be able to identify bad devices without crashing.
66 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
67 #define AC_BUFLEN (10 * 100000)
68 #define MAX_DEVICES 256
69 #define MAPPER_INDEX 0x3F
71 typedef struct _WINMM_CBInfo
{
78 struct _WINMM_MMDevice
;
79 typedef struct _WINMM_MMDevice WINMM_MMDevice
;
81 typedef struct _WINMM_Device
{
90 IAudioRenderClient
*render
;
91 IAudioCaptureClient
*capture
;
93 IAudioStreamVolume
*volume
;
95 WAVEFORMATEX
*orig_fmt
;
96 HACMSTREAM acm_handle
;
97 ACMSTREAMHEADER acm_hdr
;
100 WAVEHDR
*first
, *last
, *playing
, *loop_start
;
104 UINT32 bytes_per_frame
, samples_per_sec
, ofs_bytes
, played_frames
;
105 UINT32 remainder_frames
; /* header chunk frames already played when a device switch occurred */
107 /* stored in frames of sample rate, *not* AC::GetFrequency */
108 UINT64 last_clock_pos
;
111 CRITICAL_SECTION lock
;
113 WINMM_MMDevice
*parent
;
116 struct _WINMM_MMDevice
{
117 WAVEOUTCAPSW out_caps
; /* must not be modified outside of WINMM_InitMMDevices*/
118 WAVEINCAPSW in_caps
; /* must not be modified outside of WINMM_InitMMDevices*/
122 ISimpleAudioVolume
*volume
;
128 /* HMIXER format is the same as the HWAVE format, but the I bits are
129 * replaced by the value of this counter, to keep each HMIXER unique */
132 CRITICAL_SECTION lock
;
134 WINMM_Device
*devices
[MAX_DEVICES
];
137 static WINMM_MMDevice
*g_out_mmdevices
;
138 static WINMM_MMDevice
**g_out_map
;
139 static UINT g_outmmdevices_count
;
140 static WINMM_Device
*g_out_mapper_devices
[MAX_DEVICES
];
142 static WINMM_MMDevice
*g_in_mmdevices
;
143 static WINMM_MMDevice
**g_in_map
;
144 static UINT g_inmmdevices_count
;
145 static WINMM_Device
*g_in_mapper_devices
[MAX_DEVICES
];
147 static IMMDeviceEnumerator
*g_devenum
;
149 static CRITICAL_SECTION g_devthread_lock
;
150 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug
=
152 0, 0, &g_devthread_lock
,
153 { &g_devthread_lock_debug
.ProcessLocksList
, &g_devthread_lock_debug
.ProcessLocksList
},
154 0, 0, { (DWORD_PTR
)(__FILE__
": g_devthread_lock") }
156 static CRITICAL_SECTION g_devthread_lock
= { &g_devthread_lock_debug
, -1, 0, 0, 0, 0 };
157 static LONG g_devthread_token
;
158 static HANDLE g_devices_thread
;
159 static HWND g_devices_hwnd
;
160 static HMODULE g_devthread_module
;
162 static UINT g_devhandle_count
;
163 static HANDLE
*g_device_handles
;
164 static WINMM_Device
**g_handle_devices
;
166 typedef struct _WINMM_OpenInfo
{
169 WAVEFORMATEX
*format
;
176 typedef struct _WINMM_ControlDetails
{
178 MIXERCONTROLDETAILS
*details
;
180 } WINMM_ControlDetails
;
182 typedef struct _WINMM_QueryInterfaceInfo
{
187 } WINMM_QueryInterfaceInfo
;
189 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
);
190 static LRESULT
WOD_Close(HWAVEOUT hwave
);
191 static LRESULT
WID_Open(WINMM_OpenInfo
*info
);
192 static LRESULT
WID_Close(HWAVEIN hwave
);
193 static MMRESULT
WINMM_BeginPlaying(WINMM_Device
*device
);
195 void WINMM_DeleteWaveform(void)
200 CloseHandle(g_devices_thread
);
202 for(i
= 0; i
< g_outmmdevices_count
; ++i
){
203 WINMM_MMDevice
*mmdevice
= &g_out_mmdevices
[i
];
205 for(j
= 0; j
< MAX_DEVICES
&& mmdevice
->devices
[j
]; ++j
){
206 WINMM_Device
*device
= mmdevice
->devices
[j
];
208 CloseHandle(device
->handle
);
209 DeleteCriticalSection(&device
->lock
);
213 ISimpleAudioVolume_Release(mmdevice
->volume
);
214 CoTaskMemFree(mmdevice
->dev_id
);
215 DeleteCriticalSection(&mmdevice
->lock
);
218 for(i
= 0; i
< g_inmmdevices_count
; ++i
){
219 WINMM_MMDevice
*mmdevice
= &g_in_mmdevices
[i
];
221 for(j
= 0; j
< MAX_DEVICES
&& mmdevice
->devices
[j
]; ++j
){
222 WINMM_Device
*device
= mmdevice
->devices
[j
];
224 CloseHandle(device
->handle
);
225 DeleteCriticalSection(&device
->lock
);
229 ISimpleAudioVolume_Release(mmdevice
->volume
);
230 CoTaskMemFree(mmdevice
->dev_id
);
231 DeleteCriticalSection(&mmdevice
->lock
);
234 HeapFree(GetProcessHeap(), 0, g_out_mmdevices
);
235 HeapFree(GetProcessHeap(), 0, g_in_mmdevices
);
237 HeapFree(GetProcessHeap(), 0, g_device_handles
);
238 HeapFree(GetProcessHeap(), 0, g_handle_devices
);
240 DeleteCriticalSection(&g_devthread_lock
);
243 static inline HWAVE
WINMM_MakeHWAVE(UINT mmdevice
, BOOL is_out
, UINT device
)
245 return ULongToHandle((1 << 15) | ((!!is_out
) << 14) |
246 (mmdevice
<< 8) | device
);
249 static inline void WINMM_DecomposeHWAVE(HWAVE hwave
, UINT
*mmdevice_index
,
250 BOOL
*is_out
, UINT
*device_index
, UINT
*junk
)
252 ULONG32 l
= HandleToULong(hwave
);
253 *device_index
= l
& 0xFF;
254 *mmdevice_index
= (l
>> 8) & 0x3F;
255 *is_out
= (l
>> 14) & 0x1;
259 static void WINMM_InitDevice(WINMM_Device
*device
)
261 InitializeCriticalSection(&device
->lock
);
262 device
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
265 static inline WINMM_MMDevice
*read_map(WINMM_MMDevice
**map
, UINT index
)
268 EnterCriticalSection(&g_devthread_lock
);
270 LeaveCriticalSection(&g_devthread_lock
);
274 /* finds the first unused Device, marks it as "open", and returns
275 * a pointer to the device
277 * IMPORTANT: it is the caller's responsibility to release the device's lock
280 static WINMM_Device
*WINMM_FindUnusedDevice(WINMM_Device
**devices
,
281 WINMM_MMDevice
*parent
, UINT internal_index
, BOOL is_out
)
285 for(i
= 0; i
< MAX_DEVICES
; ++i
){
286 WINMM_Device
*device
= devices
[i
];
289 device
= devices
[i
] = HeapAlloc(GetProcessHeap(),
290 HEAP_ZERO_MEMORY
, sizeof(WINMM_Device
));
294 WINMM_InitDevice(device
);
295 EnterCriticalSection(&device
->lock
);
297 EnterCriticalSection(&device
->lock
);
300 device
->handle
= WINMM_MakeHWAVE(internal_index
, is_out
, i
);
301 device
->parent
= parent
;
307 LeaveCriticalSection(&device
->lock
);
310 TRACE("All devices in use: mmdevice: %u\n", internal_index
);
315 static inline BOOL
WINMM_ValidateAndLock(WINMM_Device
*device
)
320 EnterCriticalSection(&device
->lock
);
323 LeaveCriticalSection(&device
->lock
);
330 static WINMM_Device
*WINMM_GetDeviceFromHWAVE(HWAVE hwave
)
332 WINMM_MMDevice
*mmdevice
;
333 WINMM_Device
*device
;
334 UINT mmdevice_index
, device_index
, junk
;
337 WINMM_DecomposeHWAVE(hwave
, &mmdevice_index
, &is_out
, &device_index
, &junk
);
342 if(mmdevice_index
== MAPPER_INDEX
){
343 EnterCriticalSection(&g_devthread_lock
);
345 device
= g_out_mapper_devices
[device_index
];
347 device
= g_in_mapper_devices
[device_index
];
348 LeaveCriticalSection(&g_devthread_lock
);
352 if(mmdevice_index
>= (is_out
? g_outmmdevices_count
: g_inmmdevices_count
))
356 mmdevice
= &g_out_mmdevices
[mmdevice_index
];
358 mmdevice
= &g_in_mmdevices
[mmdevice_index
];
360 EnterCriticalSection(&mmdevice
->lock
);
362 device
= mmdevice
->devices
[device_index
];
364 LeaveCriticalSection(&mmdevice
->lock
);
369 /* Note: NotifyClient should never be called while holding the device lock
370 * since the client may call wave* functions from within the callback. */
371 static inline void WINMM_NotifyClient(WINMM_CBInfo
*info
, WORD msg
, DWORD_PTR param1
,
374 DriverCallback(info
->callback
, info
->flags
, (HDRVR
)info
->hwave
,
375 msg
, info
->user
, param1
, param2
);
378 static MMRESULT
hr2mmr(HRESULT hr
)
382 case AUDCLNT_E_NOT_STOPPED
:
383 return MMSYSERR_NOERROR
;
384 case AUDCLNT_E_UNSUPPORTED_FORMAT
:
385 return WAVERR_BADFORMAT
;
386 case AUDCLNT_E_DEVICE_IN_USE
:
387 return MMSYSERR_ALLOCATED
;
388 case AUDCLNT_E_ENDPOINT_CREATE_FAILED
:
389 return MMSYSERR_NOTENABLED
;
391 return MMSYSERR_NOMEM
;
394 return MMSYSERR_INVALPARAM
;
395 case AUDCLNT_E_DEVICE_INVALIDATED
: /* DSERR_BUFFERLOST */
397 return FAILED(hr
) ? MMSYSERR_ERROR
: MMSYSERR_NOERROR
;
401 static HRESULT
WINMM_GetFriendlyName(IMMDevice
*device
, WCHAR
*out
,
408 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
412 PropVariantInit(&var
);
414 hr
= IPropertyStore_GetValue(ps
,
415 (PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &var
);
417 IPropertyStore_Release(ps
);
421 lstrcpynW(out
, var
.u
.pwszVal
, outlen
);
423 PropVariantClear(&var
);
425 IPropertyStore_Release(ps
);
430 static HRESULT
WINMM_TestFormat(IAudioClient
*client
, DWORD rate
, DWORD depth
,
433 WAVEFORMATEX fmt
, *junk
;
436 fmt
.wFormatTag
= WAVE_FORMAT_PCM
;
437 fmt
.nChannels
= channels
;
438 fmt
.nSamplesPerSec
= rate
;
439 fmt
.wBitsPerSample
= depth
;
440 fmt
.nBlockAlign
= (channels
* depth
) / 8;
441 fmt
.nAvgBytesPerSec
= rate
* fmt
.nBlockAlign
;
444 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
,
452 static struct _TestFormat
{
457 } formats_to_test
[] = {
458 { WAVE_FORMAT_1M08
, 11025, 8, 1 },
459 { WAVE_FORMAT_1M16
, 11025, 16, 1 },
460 { WAVE_FORMAT_1S08
, 11025, 8, 2 },
461 { WAVE_FORMAT_1S16
, 11025, 16, 2 },
462 { WAVE_FORMAT_2M08
, 22050, 8, 1 },
463 { WAVE_FORMAT_2M16
, 22050, 16, 1 },
464 { WAVE_FORMAT_2S08
, 22050, 8, 2 },
465 { WAVE_FORMAT_2S16
, 22050, 16, 2 },
466 { WAVE_FORMAT_4M08
, 44100, 8, 1 },
467 { WAVE_FORMAT_4M16
, 44100, 16, 1 },
468 { WAVE_FORMAT_4S08
, 44100, 8, 2 },
469 { WAVE_FORMAT_4S16
, 44100, 16, 2 },
470 { WAVE_FORMAT_48M08
, 48000, 8, 1 },
471 { WAVE_FORMAT_48M16
, 48000, 16, 1 },
472 { WAVE_FORMAT_48S08
, 48000, 8, 2 },
473 { WAVE_FORMAT_48S16
, 48000, 16, 2 },
474 { WAVE_FORMAT_96M08
, 96000, 8, 1 },
475 { WAVE_FORMAT_96M16
, 96000, 16, 1 },
476 { WAVE_FORMAT_96S08
, 96000, 8, 2 },
477 { WAVE_FORMAT_96S16
, 96000, 16, 2 },
481 static DWORD
WINMM_GetSupportedFormats(IMMDevice
*device
)
485 struct _TestFormat
*fmt
;
486 IAudioClient
*client
;
488 hr
= IMMDevice_Activate(device
, &IID_IAudioClient
,
489 CLSCTX_INPROC_SERVER
, NULL
, (void**)&client
);
493 for(fmt
= formats_to_test
; fmt
->flag
; ++fmt
){
494 hr
= WINMM_TestFormat(client
, fmt
->rate
, fmt
->depth
, fmt
->channels
);
499 IAudioClient_Release(client
);
504 static HRESULT
WINMM_InitMMDevice(EDataFlow flow
, IMMDevice
*device
,
505 WINMM_MMDevice
*dev
, UINT index
)
509 dev
->dataflow
= flow
;
511 dev
->out_caps
.wMid
= 0xFF;
512 dev
->out_caps
.wPid
= 0xFF;
513 dev
->out_caps
.vDriverVersion
= 0x00010001;
514 dev
->out_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
515 dev
->out_caps
.wReserved1
= 0;
516 dev
->out_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
517 WAVECAPS_SAMPLEACCURATE
;
518 dev
->out_caps
.wChannels
= 2;
519 dev
->out_caps
.szPname
[0] = '\0';
521 hr
= WINMM_GetFriendlyName(device
, dev
->out_caps
.szPname
,
522 sizeof(dev
->out_caps
.szPname
) /
523 sizeof(*dev
->out_caps
.szPname
));
527 dev
->in_caps
.wMid
= 0xFF;
528 dev
->in_caps
.wPid
= 0xFF;
529 dev
->in_caps
.vDriverVersion
= 0x00010001;
530 dev
->in_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
531 dev
->in_caps
.wReserved1
= 0;
532 dev
->in_caps
.wChannels
= 2;
533 dev
->in_caps
.szPname
[0] = '\0';
535 hr
= WINMM_GetFriendlyName(device
, dev
->in_caps
.szPname
,
536 sizeof(dev
->in_caps
.szPname
) /
537 sizeof(*dev
->in_caps
.szPname
));
542 hr
= IMMDevice_GetId(device
, &dev
->dev_id
);
546 CoCreateGuid(&dev
->session
);
550 InitializeCriticalSection(&dev
->lock
);
551 dev
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
556 static HRESULT
WINMM_EnumDevices(WINMM_MMDevice
**devices
,
557 WINMM_MMDevice
***map
, UINT
*devcount
, EDataFlow flow
,
558 IMMDeviceEnumerator
*devenum
)
560 IMMDeviceCollection
*devcoll
;
563 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flow
,
564 DEVICE_STATE_ACTIVE
, &devcoll
);
568 hr
= IMMDeviceCollection_GetCount(devcoll
, devcount
);
570 IMMDeviceCollection_Release(devcoll
);
576 IMMDevice
*def_dev
= NULL
;
578 *devices
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
579 sizeof(WINMM_MMDevice
) * (*devcount
));
581 IMMDeviceCollection_Release(devcoll
);
582 return E_OUTOFMEMORY
;
585 *map
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
586 sizeof(WINMM_MMDevice
*) * (*devcount
));
588 IMMDeviceCollection_Release(devcoll
);
589 HeapFree(GetProcessHeap(), 0, *devices
);
590 return E_OUTOFMEMORY
;
593 /* make sure that device 0 is the default device */
594 IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
,
595 flow
, eConsole
, &def_dev
);
597 for(n
= 0; n
< *devcount
; ++n
){
600 hr
= IMMDeviceCollection_Item(devcoll
, n
, &device
);
602 WINMM_InitMMDevice(flow
, device
, &(*devices
)[n
], n
);
604 if(device
== def_dev
)
605 (*map
)[0] = &(*devices
)[n
];
607 (*map
)[count
] = &(*devices
)[n
];
611 IMMDevice_Release(device
);
615 IMMDevice_Release(def_dev
);
620 IMMDeviceCollection_Release(devcoll
);
625 static HRESULT WINAPI
notif_QueryInterface(IMMNotificationClient
*iface
,
626 const GUID
*riid
, void **obj
)
628 ERR("Unexpected QueryInterface call: %s\n", wine_dbgstr_guid(riid
));
629 return E_NOINTERFACE
;
632 static ULONG WINAPI
notif_AddRef(IMMNotificationClient
*iface
)
637 static ULONG WINAPI
notif_Release(IMMNotificationClient
*iface
)
642 static HRESULT WINAPI
notif_OnDeviceStateChanged(IMMNotificationClient
*iface
,
643 const WCHAR
*device_id
, DWORD new_state
)
645 TRACE("Ignoring OnDeviceStateChanged callback\n");
649 static HRESULT WINAPI
notif_OnDeviceAdded(IMMNotificationClient
*iface
,
650 const WCHAR
*device_id
)
652 TRACE("Ignoring OnDeviceAdded callback\n");
656 static HRESULT WINAPI
notif_OnDeviceRemoved(IMMNotificationClient
*iface
,
657 const WCHAR
*device_id
)
659 TRACE("Ignoring OnDeviceRemoved callback\n");
663 static HRESULT
update_mapping(WINMM_MMDevice
***map
, UINT count
,
664 const WCHAR
*default_id
)
666 WINMM_MMDevice
*prev
;
670 for(i
= 0; i
< count
; ++i
){
673 if(!lstrcmpW((*map
)[i
]->dev_id
, default_id
)){
674 (*map
)[0] = (*map
)[i
];
685 WARN("Couldn't find new default device! Rearranged map for no reason.\n");
691 static HRESULT
reroute_mapper_device(WINMM_Device
*device
, BOOL is_out
)
697 UINT64 clock_freq
, clock_pos
;
699 TRACE("rerouting device %p\n", device
->handle
);
701 EnterCriticalSection(&device
->lock
);
703 if(!device
->open
|| device
->acm_handle
){
704 /* Windows 7 doesn't re-route ACM devices, so we don't either.
705 * Seems to be because of the data waveXxxPrepareHeader allocates. */
706 LeaveCriticalSection(&device
->lock
);
710 stopped
= device
->stopped
;
713 info
.req_device
= WAVE_MAPPER
;
714 info
.format
= device
->orig_fmt
;
715 info
.callback
= device
->cb_info
.callback
;
716 info
.cb_user
= device
->cb_info
.user
;
717 /* We have to use direct here so that we don't suddenly introduce ACM
718 * into a playing stream that hasn't been Prepared for it */
719 info
.flags
= (device
->cb_info
.flags
<< 16) | WAVE_FORMAT_DIRECT_QUERY
;
723 mr
= WOD_Open(&info
);
725 mr
= WID_Open(&info
);
727 if(mr
!= MMSYSERR_NOERROR
){
728 TRACE("New default device doesn't support this stream: %p\n", device
->handle
);
729 LeaveCriticalSection(&device
->lock
);
733 hr
= IAudioClient_Stop(device
->client
);
735 WARN("Stop failed: %08x\n", hr
);
737 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
739 WARN("GetFrequency failed: %08x\n", hr
);
740 LeaveCriticalSection(&device
->lock
);
744 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
746 WARN("GetPosition failed: %08x\n", hr
);
747 LeaveCriticalSection(&device
->lock
);
751 device
->remainder_frames
= MulDiv(clock_pos
, device
->samples_per_sec
, clock_freq
) - device
->last_clock_pos
;
753 info
.handle
= device
->handle
;
754 info
.flags
= (device
->cb_info
.flags
<< 16) | WAVE_FORMAT_DIRECT
;
757 WOD_Close((HWAVEOUT
)device
->handle
);
758 device
->parent
= read_map(g_out_map
, 0);
759 mr
= WOD_Open(&info
);
761 WID_Close((HWAVEIN
)device
->handle
);
762 device
->parent
= read_map(g_in_map
, 0);
763 mr
= WID_Open(&info
);
766 if(mr
!= MMSYSERR_NOERROR
){
767 ERR("Opening new default device failed! %u\n", mr
);
768 LeaveCriticalSection(&device
->lock
);
772 HeapFree(GetProcessHeap(), 0, info
.format
);
775 WINMM_BeginPlaying(device
);
777 LeaveCriticalSection(&device
->lock
);
782 static HRESULT WINAPI
notif_OnDefaultDeviceChanged(IMMNotificationClient
*iface
,
783 EDataFlow flow
, ERole role
, const WCHAR
*device_id
)
787 TRACE("%u %u %s\n", flow
, role
, wine_dbgstr_w(device_id
));
792 EnterCriticalSection(&g_devthread_lock
);
795 update_mapping(&g_out_map
, g_outmmdevices_count
, device_id
);
797 update_mapping(&g_in_map
, g_inmmdevices_count
, device_id
);
799 for(i
= 0; i
< MAX_DEVICES
&& g_out_mapper_devices
[i
]; ++i
)
800 reroute_mapper_device(g_out_mapper_devices
[i
], TRUE
);
802 for(i
= 0; i
< MAX_DEVICES
&& g_in_mapper_devices
[i
]; ++i
)
803 reroute_mapper_device(g_in_mapper_devices
[i
], FALSE
);
805 LeaveCriticalSection(&g_devthread_lock
);
810 static HRESULT WINAPI
notif_OnPropertyValueChanged(IMMNotificationClient
*iface
,
811 const WCHAR
*device_id
, const PROPERTYKEY key
)
813 TRACE("Ignoring OnPropertyValueChanged callback\n");
817 static IMMNotificationClientVtbl g_notif_vtbl
= {
818 notif_QueryInterface
,
821 notif_OnDeviceStateChanged
,
823 notif_OnDeviceRemoved
,
824 notif_OnDefaultDeviceChanged
,
825 notif_OnPropertyValueChanged
828 static IMMNotificationClient g_notif
= { &g_notif_vtbl
};
830 static HRESULT
WINMM_InitMMDevices(void)
833 IMMDeviceEnumerator
*devenum
= NULL
;
835 if(g_outmmdevices_count
|| g_inmmdevices_count
)
838 init_hr
= CoInitialize(NULL
);
840 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
841 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
845 hr
= IMMDeviceEnumerator_RegisterEndpointNotificationCallback(devenum
, &g_notif
);
847 WARN("RegisterEndpointNotificationCallback failed: %08x\n", hr
);
849 hr
= WINMM_EnumDevices(&g_out_mmdevices
, &g_out_map
, &g_outmmdevices_count
,
852 g_outmmdevices_count
= 0;
853 g_inmmdevices_count
= 0;
857 hr
= WINMM_EnumDevices(&g_in_mmdevices
, &g_in_map
, &g_inmmdevices_count
,
860 g_inmmdevices_count
= 0;
866 IMMDeviceEnumerator_Release(devenum
);
867 if(SUCCEEDED(init_hr
))
873 static inline BOOL
WINMM_IsMapper(UINT device
)
875 return (device
== WAVE_MAPPER
|| device
== (UINT16
)WAVE_MAPPER
);
878 static MMRESULT
WINMM_TryDeviceMapping(WINMM_Device
*device
, WAVEFORMATEX
*fmt
,
879 WORD channels
, DWORD freq
, DWORD bits_per_samp
, BOOL is_query
, BOOL is_out
)
881 WAVEFORMATEX target
, *closer_fmt
= NULL
;
885 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
886 WAVE_FORMAT_PCM
, channels
, freq
, bits_per_samp
);
888 target
.wFormatTag
= WAVE_FORMAT_PCM
;
889 target
.nChannels
= channels
;
890 target
.nSamplesPerSec
= freq
;
891 target
.wBitsPerSample
= bits_per_samp
;
892 target
.nBlockAlign
= (target
.nChannels
* target
.wBitsPerSample
) / 8;
893 target
.nAvgBytesPerSec
= target
.nSamplesPerSec
* target
.nBlockAlign
;
896 hr
= IAudioClient_IsFormatSupported(device
->client
,
897 AUDCLNT_SHAREMODE_SHARED
, &target
, &closer_fmt
);
899 CoTaskMemFree(closer_fmt
);
901 return WAVERR_BADFORMAT
;
903 /* device supports our target format, so see if MSACM can
904 * do the conversion */
906 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, fmt
, &target
, NULL
,
909 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, &target
, fmt
, NULL
,
911 if(mr
!= MMSYSERR_NOERROR
)
914 /* yes it can. initialize the audioclient and return success */
916 acmStreamClose(device
->acm_handle
, 0);
917 device
->acm_handle
= NULL
;
918 return MMSYSERR_NOERROR
;
921 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
922 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
923 AC_BUFLEN
, 0, &target
, &device
->parent
->session
);
925 WARN("Initialize failed: %08x\n", hr
);
926 acmStreamClose(device
->acm_handle
, 0);
927 device
->acm_handle
= NULL
;
928 return MMSYSERR_ERROR
;
931 device
->bytes_per_frame
= target
.nBlockAlign
;
932 device
->samples_per_sec
= target
.nSamplesPerSec
;
936 return MMSYSERR_NOERROR
;
939 static MMRESULT
WINMM_MapDevice(WINMM_Device
*device
, BOOL is_query
, BOOL is_out
)
942 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)device
->orig_fmt
;
944 TRACE("(%p, %u)\n", device
, is_out
);
946 /* set up the ACM stream */
947 if(device
->orig_fmt
->wFormatTag
!= WAVE_FORMAT_PCM
&&
948 !(device
->orig_fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
949 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
950 /* convert to PCM format if it's not already */
951 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
,
952 device
->orig_fmt
->nChannels
, device
->orig_fmt
->nSamplesPerSec
,
953 16, is_query
, is_out
);
954 if(mr
== MMSYSERR_NOERROR
)
957 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
,
958 device
->orig_fmt
->nChannels
, device
->orig_fmt
->nSamplesPerSec
,
959 8, is_query
, is_out
);
960 if(mr
== MMSYSERR_NOERROR
)
965 /* first try just changing bit depth and channels */
966 channels
= device
->orig_fmt
->nChannels
;
967 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
968 device
->orig_fmt
->nSamplesPerSec
, 16, is_query
, is_out
);
969 if(mr
== MMSYSERR_NOERROR
)
971 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
972 device
->orig_fmt
->nSamplesPerSec
, 8, is_query
, is_out
);
973 if(mr
== MMSYSERR_NOERROR
)
976 channels
= (channels
== 2) ? 1 : 2;
977 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
978 device
->orig_fmt
->nSamplesPerSec
, 16, is_query
, is_out
);
979 if(mr
== MMSYSERR_NOERROR
)
981 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
,
982 device
->orig_fmt
->nSamplesPerSec
, 8, is_query
, is_out
);
983 if(mr
== MMSYSERR_NOERROR
)
986 /* that didn't work, so now try different sample rates */
987 channels
= device
->orig_fmt
->nChannels
;
988 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 16, is_query
, is_out
);
989 if(mr
== MMSYSERR_NOERROR
)
991 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 16, is_query
, is_out
);
992 if(mr
== MMSYSERR_NOERROR
)
994 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 16, is_query
, is_out
);
995 if(mr
== MMSYSERR_NOERROR
)
997 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 16, is_query
, is_out
);
998 if(mr
== MMSYSERR_NOERROR
)
1000 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 16, is_query
, is_out
);
1001 if(mr
== MMSYSERR_NOERROR
)
1004 channels
= (channels
== 2) ? 1 : 2;
1005 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 16, is_query
, is_out
);
1006 if(mr
== MMSYSERR_NOERROR
)
1008 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 16, is_query
, is_out
);
1009 if(mr
== MMSYSERR_NOERROR
)
1011 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 16, is_query
, is_out
);
1012 if(mr
== MMSYSERR_NOERROR
)
1014 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 16, is_query
, is_out
);
1015 if(mr
== MMSYSERR_NOERROR
)
1017 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 16, is_query
, is_out
);
1018 if(mr
== MMSYSERR_NOERROR
)
1021 channels
= device
->orig_fmt
->nChannels
;
1022 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 8, is_query
, is_out
);
1023 if(mr
== MMSYSERR_NOERROR
)
1025 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 8, is_query
, is_out
);
1026 if(mr
== MMSYSERR_NOERROR
)
1028 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 8, is_query
, is_out
);
1029 if(mr
== MMSYSERR_NOERROR
)
1031 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 8, is_query
, is_out
);
1032 if(mr
== MMSYSERR_NOERROR
)
1034 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 8, is_query
, is_out
);
1035 if(mr
== MMSYSERR_NOERROR
)
1038 channels
= (channels
== 2) ? 1 : 2;
1039 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 96000, 8, is_query
, is_out
);
1040 if(mr
== MMSYSERR_NOERROR
)
1042 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 48000, 8, is_query
, is_out
);
1043 if(mr
== MMSYSERR_NOERROR
)
1045 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 44100, 8, is_query
, is_out
);
1046 if(mr
== MMSYSERR_NOERROR
)
1048 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 22050, 8, is_query
, is_out
);
1049 if(mr
== MMSYSERR_NOERROR
)
1051 mr
= WINMM_TryDeviceMapping(device
, device
->orig_fmt
, channels
, 11025, 8, is_query
, is_out
);
1052 if(mr
== MMSYSERR_NOERROR
)
1056 WARN("Unable to find compatible device!\n");
1057 return WAVERR_BADFORMAT
;
1060 static LRESULT
WINMM_OpenDevice(WINMM_Device
*device
, WINMM_OpenInfo
*info
,
1063 LRESULT ret
= MMSYSERR_NOMEM
;
1066 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, device
->parent
->dev_id
,
1069 WARN("Device %s (%s) unavailable: %08x\n",
1070 wine_dbgstr_w(device
->parent
->dev_id
),
1071 wine_dbgstr_w(device
->parent
->out_caps
.szPname
), hr
);
1072 ret
= MMSYSERR_NODRIVER
;
1076 /* this is where winexyz.drv opens the audio device */
1077 hr
= IMMDevice_Activate(device
->device
, &IID_IAudioClient
,
1078 CLSCTX_INPROC_SERVER
, NULL
, (void**)&device
->client
);
1080 WARN("Activate failed: %08x\n", hr
);
1082 if(ret
== MMSYSERR_ERROR
)
1083 ret
= MMSYSERR_NOTENABLED
;
1087 if(info
->format
->wFormatTag
== WAVE_FORMAT_PCM
){
1088 /* we aren't guaranteed that the struct in lpFormat is a full
1089 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
1090 device
->orig_fmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX
));
1091 memcpy(device
->orig_fmt
, info
->format
, sizeof(PCMWAVEFORMAT
));
1092 device
->orig_fmt
->cbSize
= 0;
1093 if(device
->orig_fmt
->wBitsPerSample
% 8 != 0){
1094 WARN("Fixing bad wBitsPerSample (%u)\n", device
->orig_fmt
->wBitsPerSample
);
1095 device
->orig_fmt
->wBitsPerSample
= (device
->orig_fmt
->wBitsPerSample
+ 7) & ~7;
1097 /* winmm ignores broken blockalign and avgbytes */
1098 if(device
->orig_fmt
->nBlockAlign
!= device
->orig_fmt
->nChannels
* device
->orig_fmt
->wBitsPerSample
/8){
1099 WARN("Fixing bad nBlockAlign (%u)\n", device
->orig_fmt
->nBlockAlign
);
1100 device
->orig_fmt
->nBlockAlign
= device
->orig_fmt
->nChannels
* device
->orig_fmt
->wBitsPerSample
/8;
1102 if (device
->orig_fmt
->nAvgBytesPerSec
!= device
->orig_fmt
->nSamplesPerSec
* device
->orig_fmt
->nBlockAlign
) {
1103 WARN("Fixing bad nAvgBytesPerSec (%u)\n", device
->orig_fmt
->nAvgBytesPerSec
);
1104 device
->orig_fmt
->nAvgBytesPerSec
= device
->orig_fmt
->nSamplesPerSec
* device
->orig_fmt
->nBlockAlign
;
1107 device
->orig_fmt
= HeapAlloc(GetProcessHeap(), 0,
1108 sizeof(WAVEFORMATEX
) + info
->format
->cbSize
);
1109 memcpy(device
->orig_fmt
, info
->format
,
1110 sizeof(WAVEFORMATEX
) + info
->format
->cbSize
);
1113 if(info
->flags
& WAVE_FORMAT_QUERY
){
1114 WAVEFORMATEX
*closer_fmt
= NULL
;
1116 hr
= IAudioClient_IsFormatSupported(device
->client
,
1117 AUDCLNT_SHAREMODE_SHARED
, device
->orig_fmt
, &closer_fmt
);
1119 CoTaskMemFree(closer_fmt
);
1120 if((hr
== S_FALSE
|| hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
) && !(info
->flags
& WAVE_FORMAT_DIRECT
))
1121 ret
= WINMM_MapDevice(device
, TRUE
, is_out
);
1123 ret
= hr
== S_FALSE
? WAVERR_BADFORMAT
: hr2mmr(hr
);
1127 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
1128 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
1129 AC_BUFLEN
, 0, device
->orig_fmt
, &device
->parent
->session
);
1131 if(hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
&& !(info
->flags
& WAVE_FORMAT_DIRECT
)){
1132 ret
= WINMM_MapDevice(device
, FALSE
, is_out
);
1133 if(ret
!= MMSYSERR_NOERROR
|| info
->flags
& WAVE_FORMAT_QUERY
)
1136 WARN("Initialize failed: %08x\n", hr
);
1141 device
->bytes_per_frame
= device
->orig_fmt
->nBlockAlign
;
1142 device
->samples_per_sec
= device
->orig_fmt
->nSamplesPerSec
;
1145 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioClock
,
1146 (void**)&device
->clock
);
1148 WARN("GetService failed: %08x\n", hr
);
1153 device
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1155 WARN("CreateEvent failed: %08x\n", hr
);
1159 /* As the devices thread is waiting on g_device_handles, it can
1160 * only be modified from within this same thread. */
1161 if(g_device_handles
){
1162 g_device_handles
= HeapReAlloc(GetProcessHeap(), 0, g_device_handles
,
1163 sizeof(HANDLE
) * (g_devhandle_count
+ 1));
1164 g_handle_devices
= HeapReAlloc(GetProcessHeap(), 0, g_handle_devices
,
1165 sizeof(WINMM_Device
*) * (g_devhandle_count
+ 1));
1167 g_device_handles
= HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE
));
1168 g_handle_devices
= HeapAlloc(GetProcessHeap(), 0,
1169 sizeof(WINMM_Device
*));
1171 g_device_handles
[g_devhandle_count
] = device
->event
;
1172 g_handle_devices
[g_devhandle_count
] = device
;
1173 ++g_devhandle_count
;
1176 hr
= IAudioClient_SetEventHandle(device
->client
, device
->event
);
1178 WARN("SetEventHandle failed: %08x\n", hr
);
1183 device
->played_frames
= 0;
1184 device
->ofs_bytes
= 0;
1185 device
->loop_counter
= 0;
1186 device
->first
= device
->last
= device
->playing
= device
->loop_start
= NULL
;
1189 device
->stopped
= TRUE
;
1190 device
->last_clock_pos
= 0;
1192 device
->cb_info
.flags
= HIWORD(info
->flags
& CALLBACK_TYPEMASK
);
1193 device
->cb_info
.callback
= info
->callback
;
1194 device
->cb_info
.user
= info
->cb_user
;
1195 device
->cb_info
.hwave
= device
->handle
;
1197 info
->handle
= device
->handle
;
1199 return MMSYSERR_NOERROR
;
1203 IAudioClient_Release(device
->client
);
1204 device
->client
= NULL
;
1207 IMMDevice_Release(device
->device
);
1208 device
->device
= NULL
;
1214 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
)
1216 WINMM_Device
*device
;
1217 LRESULT ret
= MMSYSERR_ERROR
;
1220 if(info
->handle
!= 0){
1221 device
= WINMM_GetDeviceFromHWAVE(info
->handle
);
1223 WARN("Unexpected! Invalid info->handle given: %p\n", info
->handle
);
1224 return MMSYSERR_ERROR
;
1227 EnterCriticalSection(&device
->lock
);
1229 device
->open
= TRUE
;
1231 CRITICAL_SECTION
*lock
;
1232 UINT internal_index
;
1233 WINMM_Device
**devices
;
1234 WINMM_MMDevice
*mmdevice
;
1236 if(WINMM_IsMapper(info
->req_device
)){
1237 if (g_outmmdevices_count
== 0)
1238 return MMSYSERR_BADDEVICEID
;
1239 devices
= g_out_mapper_devices
;
1240 mmdevice
= read_map(g_out_map
, 0);
1241 lock
= &g_devthread_lock
;
1242 internal_index
= MAPPER_INDEX
;
1244 if(info
->req_device
>= g_outmmdevices_count
)
1245 return MMSYSERR_BADDEVICEID
;
1247 mmdevice
= read_map(g_out_map
, info
->req_device
);
1249 if(!mmdevice
->out_caps
.szPname
[0])
1250 return MMSYSERR_NOTENABLED
;
1252 devices
= mmdevice
->devices
;
1253 lock
= &mmdevice
->lock
;
1254 internal_index
= mmdevice
->index
;
1257 EnterCriticalSection(lock
);
1259 device
= WINMM_FindUnusedDevice(devices
, mmdevice
,
1260 internal_index
, TRUE
);
1262 LeaveCriticalSection(lock
);
1263 return MMSYSERR_ALLOCATED
;
1266 LeaveCriticalSection(lock
);
1269 ret
= WINMM_OpenDevice(device
, info
, TRUE
);
1270 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1272 ret
= MMSYSERR_ERROR
;
1274 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioRenderClient
,
1275 (void**)&device
->render
);
1277 ERR("GetService failed: %08x\n", hr
);
1281 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioStreamVolume
,
1282 (void**)&device
->volume
);
1284 ERR("GetService failed: %08x\n", hr
);
1288 LeaveCriticalSection(&device
->lock
);
1290 return MMSYSERR_NOERROR
;
1294 IMMDevice_Release(device
->device
);
1295 device
->device
= NULL
;
1298 IAudioClient_Release(device
->client
);
1299 device
->client
= NULL
;
1302 IAudioRenderClient_Release(device
->render
);
1303 device
->render
= NULL
;
1306 IAudioStreamVolume_Release(device
->volume
);
1307 device
->volume
= NULL
;
1310 IAudioClock_Release(device
->clock
);
1311 device
->clock
= NULL
;
1313 device
->open
= FALSE
;
1314 LeaveCriticalSection(&device
->lock
);
1318 static LRESULT
WID_Open(WINMM_OpenInfo
*info
)
1320 WINMM_Device
*device
, **devices
;
1321 WINMM_MMDevice
*mmdevice
;
1322 UINT internal_index
;
1323 CRITICAL_SECTION
*lock
;
1324 LRESULT ret
= MMSYSERR_ERROR
;
1327 if(WINMM_IsMapper(info
->req_device
)){
1328 if (g_inmmdevices_count
== 0)
1329 return MMSYSERR_BADDEVICEID
;
1330 devices
= g_in_mapper_devices
;
1331 mmdevice
= read_map(g_in_map
, 0);
1332 lock
= &g_devthread_lock
;
1333 internal_index
= MAPPER_INDEX
;
1335 if(info
->req_device
>= g_inmmdevices_count
)
1336 return MMSYSERR_BADDEVICEID
;
1338 mmdevice
= read_map(g_in_map
, info
->req_device
);
1340 if(!mmdevice
->in_caps
.szPname
[0])
1341 return MMSYSERR_NOTENABLED
;
1343 devices
= mmdevice
->devices
;
1344 lock
= &mmdevice
->lock
;
1345 internal_index
= mmdevice
->index
;
1348 EnterCriticalSection(lock
);
1350 device
= WINMM_FindUnusedDevice(devices
, mmdevice
, internal_index
, FALSE
);
1352 LeaveCriticalSection(lock
);
1353 return MMSYSERR_ALLOCATED
;
1356 LeaveCriticalSection(lock
);
1358 ret
= WINMM_OpenDevice(device
, info
, FALSE
);
1359 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1361 ret
= MMSYSERR_ERROR
;
1363 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioCaptureClient
,
1364 (void**)&device
->capture
);
1366 WARN("GetService failed: %08x\n", hr
);
1370 LeaveCriticalSection(&device
->lock
);
1372 return MMSYSERR_NOERROR
;
1376 IMMDevice_Release(device
->device
);
1377 device
->device
= NULL
;
1380 IAudioClient_Release(device
->client
);
1381 device
->client
= NULL
;
1383 if(device
->capture
){
1384 IAudioCaptureClient_Release(device
->capture
);
1385 device
->capture
= NULL
;
1388 IAudioClock_Release(device
->clock
);
1389 device
->clock
= NULL
;
1391 device
->open
= FALSE
;
1392 LeaveCriticalSection(&device
->lock
);
1396 static HRESULT
WINMM_CloseDevice(WINMM_Device
*device
)
1398 device
->open
= FALSE
;
1400 if(!device
->stopped
){
1401 IAudioClient_Stop(device
->client
);
1402 device
->stopped
= TRUE
;
1405 if(device
->acm_handle
){
1406 acmStreamClose(device
->acm_handle
, 0);
1407 device
->acm_handle
= NULL
;
1410 IMMDevice_Release(device
->device
);
1411 device
->device
= NULL
;
1413 IAudioClient_Release(device
->client
);
1414 device
->client
= NULL
;
1416 IAudioClock_Release(device
->clock
);
1417 device
->clock
= NULL
;
1422 static LRESULT
WOD_Close(HWAVEOUT hwave
)
1424 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1426 TRACE("(%p)\n", hwave
);
1428 if(!WINMM_ValidateAndLock(device
))
1429 return MMSYSERR_INVALHANDLE
;
1431 WINMM_CloseDevice(device
);
1433 IAudioRenderClient_Release(device
->render
);
1434 device
->render
= NULL
;
1436 IAudioStreamVolume_Release(device
->volume
);
1437 device
->volume
= NULL
;
1439 LeaveCriticalSection(&device
->lock
);
1441 return MMSYSERR_NOERROR
;
1444 static LRESULT
WID_Close(HWAVEIN hwave
)
1446 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1448 TRACE("(%p)\n", hwave
);
1450 if(!WINMM_ValidateAndLock(device
))
1451 return MMSYSERR_INVALHANDLE
;
1453 WINMM_CloseDevice(device
);
1455 IAudioCaptureClient_Release(device
->capture
);
1456 device
->capture
= NULL
;
1458 LeaveCriticalSection(&device
->lock
);
1460 return MMSYSERR_NOERROR
;
1463 static DWORD
WINMM_FixedBufferLen(DWORD length
, WINMM_Device
*device
)
1465 return length
- length
% device
->bytes_per_frame
;
1468 static LRESULT
WINMM_PrepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1470 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1472 TRACE("(%p, %p)\n", hwave
, header
);
1474 if(!WINMM_ValidateAndLock(device
))
1475 return MMSYSERR_INVALHANDLE
;
1477 if(device
->render
&& device
->acm_handle
){
1478 ACMSTREAMHEADER
*ash
;
1482 mr
= acmStreamSize(device
->acm_handle
, header
->dwBufferLength
, &size
,
1483 ACM_STREAMSIZEF_SOURCE
);
1484 if(mr
!= MMSYSERR_NOERROR
){
1485 LeaveCriticalSection(&device
->lock
);
1489 ash
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER
) + size
);
1491 LeaveCriticalSection(&device
->lock
);
1492 return MMSYSERR_NOMEM
;
1495 ash
->cbStruct
= sizeof(*ash
);
1497 ash
->dwUser
= (DWORD_PTR
)header
;
1498 ash
->pbSrc
= (BYTE
*)header
->lpData
;
1499 ash
->cbSrcLength
= header
->dwBufferLength
;
1500 ash
->dwSrcUser
= header
->dwUser
;
1501 ash
->pbDst
= (BYTE
*)ash
+ sizeof(ACMSTREAMHEADER
);
1502 ash
->cbDstLength
= size
;
1505 mr
= acmStreamPrepareHeader(device
->acm_handle
, ash
, 0);
1506 if(mr
!= MMSYSERR_NOERROR
){
1507 HeapFree(GetProcessHeap(), 0, ash
);
1508 LeaveCriticalSection(&device
->lock
);
1512 header
->reserved
= (DWORD_PTR
)ash
;
1515 LeaveCriticalSection(&device
->lock
);
1517 header
->dwFlags
|= WHDR_PREPARED
;
1518 header
->dwFlags
&= ~(WHDR_DONE
|WHDR_INQUEUE
); /* flags cleared since w2k */
1520 return MMSYSERR_NOERROR
;
1523 static LRESULT
WINMM_UnprepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1525 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1527 TRACE("(%p, %p)\n", hwave
, header
);
1529 if(!WINMM_ValidateAndLock(device
))
1530 return MMSYSERR_INVALHANDLE
;
1532 if(device
->render
&& device
->acm_handle
){
1533 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1535 acmStreamUnprepareHeader(device
->acm_handle
, ash
, 0);
1537 HeapFree(GetProcessHeap(), 0, ash
);
1540 LeaveCriticalSection(&device
->lock
);
1542 header
->dwFlags
&= ~WHDR_PREPARED
;
1544 return MMSYSERR_NOERROR
;
1547 static UINT32
WINMM_HeaderLenBytes(WINMM_Device
*device
, WAVEHDR
*header
)
1549 if(device
->acm_handle
){
1550 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1551 return WINMM_FixedBufferLen(ash
->cbDstLengthUsed
, device
);
1554 return WINMM_FixedBufferLen(header
->dwBufferLength
, device
);
1557 static UINT32
WINMM_HeaderLenFrames(WINMM_Device
*device
, WAVEHDR
*header
)
1559 return WINMM_HeaderLenBytes(device
, header
) / device
->bytes_per_frame
;
1562 static WAVEHDR
*WOD_MarkDoneHeaders(WINMM_Device
*device
)
1565 WAVEHDR
*first
= device
->first
, *queue
= first
, *last
= NULL
;
1566 UINT64 clock_freq
, clock_pos
, clock_frames
;
1567 UINT32 nloops
, queue_frames
= 0;
1569 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
1571 WARN("GetFrequency failed: %08x\n", hr
);
1575 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
1577 WARN("GetPosition failed: %08x\n", hr
);
1581 clock_frames
= (clock_pos
* device
->samples_per_sec
) / clock_freq
;
1583 nloops
= device
->loop_counter
;
1585 (queue_frames
+= WINMM_HeaderLenFrames(device
, queue
)) <=
1586 clock_frames
- device
->last_clock_pos
+ device
->remainder_frames
){
1589 device
->last_clock_pos
+= queue_frames
;
1590 device
->remainder_frames
= 0;
1592 queue
= device
->first
= queue
->lpNext
;
1594 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1595 if(queue
->dwFlags
& WHDR_ENDLOOP
)
1598 queue
= queue
->lpNext
;
1599 }else if(queue
->dwFlags
& WHDR_ENDLOOP
){
1600 queue
= device
->loop_start
;
1607 last
->lpNext
= NULL
;
1613 static void WOD_PushData(WINMM_Device
*device
)
1615 WINMM_CBInfo cb_info
;
1617 UINT32 pad
, bufsize
, avail_frames
, queue_frames
, written
, ofs
;
1618 UINT32 queue_bytes
, nloops
;
1620 WAVEHDR
*queue
, *first
= NULL
;
1622 TRACE("(%p)\n", device
->handle
);
1624 EnterCriticalSection(&device
->lock
);
1630 if (device
->stopped
)
1632 device
->stopped
= TRUE
;
1633 device
->last_clock_pos
= 0;
1634 IAudioClient_Stop(device
->client
);
1635 IAudioClient_Reset(device
->client
);
1639 hr
= IAudioClient_GetBufferSize(device
->client
, &bufsize
);
1641 WARN("GetBufferSize failed: %08x\n", hr
);
1645 hr
= IAudioClient_GetCurrentPadding(device
->client
, &pad
);
1647 WARN("GetCurrentPadding failed: %08x\n", hr
);
1651 first
= WOD_MarkDoneHeaders(device
);
1653 /* determine which is larger between the available buffer size and
1654 * the amount of data left in the queue */
1655 avail_frames
= bufsize
- pad
;
1657 queue
= device
->playing
;
1658 ofs
= device
->ofs_bytes
;
1661 while(queue
&& queue_frames
< avail_frames
){
1662 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1663 queue_frames
+= (queue_bytes
- ofs
) / device
->bytes_per_frame
;
1666 if(queue
->dwFlags
& WHDR_ENDLOOP
&& nloops
< device
->loop_counter
){
1667 queue
= device
->loop_start
;
1670 queue
= queue
->lpNext
;
1673 if(queue_frames
< avail_frames
)
1674 avail_frames
= queue_frames
;
1675 if(avail_frames
== 0)
1678 hr
= IAudioRenderClient_GetBuffer(device
->render
, avail_frames
, &data
);
1680 WARN("GetBuffer failed: %08x\n", hr
);
1685 while(device
->playing
&& written
< avail_frames
){
1686 UINT32 copy_frames
, copy_bytes
;
1689 queue
= device
->playing
;
1691 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1692 if(device
->acm_handle
)
1693 queue_data
= ((ACMSTREAMHEADER
*)queue
->reserved
)->pbDst
;
1695 queue_data
= (BYTE
*)queue
->lpData
;
1697 queue_frames
= (queue_bytes
- device
->ofs_bytes
) /
1698 device
->bytes_per_frame
;
1700 copy_frames
= queue_frames
< (avail_frames
- written
) ?
1701 queue_frames
: avail_frames
- written
;
1702 copy_bytes
= copy_frames
* device
->bytes_per_frame
;
1704 memcpy(data
, queue_data
+ device
->ofs_bytes
, copy_bytes
);
1707 written
+= copy_frames
;
1708 device
->ofs_bytes
+= copy_bytes
;
1710 if(device
->ofs_bytes
>= queue_bytes
){
1711 device
->ofs_bytes
= 0;
1713 if(!(queue
->dwFlags
& (WHDR_BEGINLOOP
| WHDR_ENDLOOP
)))
1714 device
->playing
= queue
->lpNext
;
1716 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1717 device
->loop_start
= device
->playing
;
1718 device
->playing
= queue
->lpNext
;
1719 device
->loop_counter
= queue
->dwLoops
;
1721 if(queue
->dwFlags
& WHDR_ENDLOOP
){
1722 --device
->loop_counter
;
1723 if(device
->loop_counter
)
1724 device
->playing
= device
->loop_start
;
1726 device
->loop_start
= device
->playing
= queue
->lpNext
;
1732 hr
= IAudioRenderClient_ReleaseBuffer(device
->render
, avail_frames
, 0);
1734 WARN("ReleaseBuffer failed: %08x\n", hr
);
1738 if(device
->orig_fmt
->nSamplesPerSec
!= device
->samples_per_sec
)
1739 device
->played_frames
+= MulDiv(avail_frames
, device
->orig_fmt
->nSamplesPerSec
, device
->samples_per_sec
);
1741 device
->played_frames
+= avail_frames
;
1744 cb_info
= device
->cb_info
;
1746 LeaveCriticalSection(&device
->lock
);
1749 WAVEHDR
*next
= first
->lpNext
;
1750 first
->dwFlags
&= ~WHDR_INQUEUE
;
1751 first
->dwFlags
|= WHDR_DONE
;
1752 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
1757 static void WID_PullACMData(WINMM_Device
*device
)
1759 UINT32 packet
, packet_bytes
;
1766 if(device
->acm_hdr
.cbDstLength
== 0){
1767 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet
,
1768 &flags
, NULL
, NULL
);
1771 WARN("GetBuffer failed: %08x\n", hr
);
1775 acmStreamSize(device
->acm_handle
, packet
* device
->bytes_per_frame
,
1776 &packet_bytes
, ACM_STREAMSIZEF_SOURCE
);
1778 device
->acm_offs
= 0;
1780 device
->acm_hdr
.cbStruct
= sizeof(device
->acm_hdr
);
1781 device
->acm_hdr
.fdwStatus
= 0;
1782 device
->acm_hdr
.dwUser
= 0;
1783 device
->acm_hdr
.pbSrc
= data
;
1784 device
->acm_hdr
.cbSrcLength
= packet
* device
->bytes_per_frame
;
1785 device
->acm_hdr
.cbSrcLengthUsed
= 0;
1786 device
->acm_hdr
.dwSrcUser
= 0;
1787 device
->acm_hdr
.pbDst
= HeapAlloc(GetProcessHeap(), 0, packet_bytes
);
1788 device
->acm_hdr
.cbDstLength
= packet_bytes
;
1789 device
->acm_hdr
.cbDstLengthUsed
= 0;
1790 device
->acm_hdr
.dwDstUser
= 0;
1792 mr
= acmStreamPrepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1793 if(mr
!= MMSYSERR_NOERROR
){
1794 WARN("acmStreamPrepareHeader failed: %d\n", mr
);
1798 mr
= acmStreamConvert(device
->acm_handle
, &device
->acm_hdr
, 0);
1799 if(mr
!= MMSYSERR_NOERROR
){
1800 WARN("acmStreamConvert failed: %d\n", mr
);
1804 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet
);
1806 WARN("ReleaseBuffer failed: %08x\n", hr
);
1808 device
->played_frames
+= packet
;
1811 queue
= device
->first
;
1813 UINT32 to_copy_bytes
;
1815 to_copy_bytes
= min(WINMM_FixedBufferLen(queue
->dwBufferLength
, device
) - queue
->dwBytesRecorded
,
1816 WINMM_FixedBufferLen(device
->acm_hdr
.cbDstLengthUsed
, device
) - device
->acm_offs
);
1818 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1819 device
->acm_hdr
.pbDst
+ device
->acm_offs
, to_copy_bytes
);
1821 queue
->dwBytesRecorded
+= to_copy_bytes
;
1822 device
->acm_offs
+= to_copy_bytes
;
1824 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1825 device
->bytes_per_frame
){
1826 queue
->dwFlags
&= ~WHDR_INQUEUE
;
1827 queue
->dwFlags
|= WHDR_DONE
;
1828 device
->first
= queue
= queue
->lpNext
;
1831 if(device
->acm_offs
>= WINMM_FixedBufferLen(device
->acm_hdr
.cbDstLengthUsed
, device
)){
1832 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1833 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1834 device
->acm_hdr
.cbDstLength
= 0;
1835 device
->acm_hdr
.cbDstLengthUsed
= 0;
1837 /* done with this ACM Header, so try to pull more data */
1838 WID_PullACMData(device
);
1843 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1844 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1845 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1846 device
->acm_hdr
.cbDstLength
= 0;
1847 device
->acm_hdr
.cbDstLengthUsed
= 0;
1850 static void WID_PullData(WINMM_Device
*device
)
1852 WINMM_CBInfo cb_info
;
1853 WAVEHDR
*queue
, *first
= NULL
, *last
= NULL
;
1856 TRACE("(%p)\n", device
->handle
);
1858 EnterCriticalSection(&device
->lock
);
1860 if(!device
->device
|| !device
->first
)
1863 first
= device
->first
;
1865 if(device
->acm_handle
){
1866 WID_PullACMData(device
);
1870 while(device
->first
){
1872 UINT32 packet_len
, packet
;
1875 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet_len
,
1876 &flags
, NULL
, NULL
);
1879 WARN("GetBuffer failed: %08x\n", hr
);
1880 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1881 IAudioCaptureClient_ReleaseBuffer(device
->capture
, 0);
1885 packet
= packet_len
;
1886 queue
= device
->first
;
1887 while(queue
&& packet
> 0){
1888 UINT32 to_copy_bytes
;
1890 to_copy_bytes
= min(packet
* device
->bytes_per_frame
,
1891 WINMM_FixedBufferLen(queue
->dwBufferLength
, device
) - queue
->dwBytesRecorded
);
1893 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1894 data
+ (packet_len
- packet
) * device
->bytes_per_frame
,
1897 queue
->dwBytesRecorded
+= to_copy_bytes
;
1899 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1900 device
->bytes_per_frame
){
1902 device
->first
= queue
= queue
->lpNext
;
1905 packet
-= to_copy_bytes
/ device
->bytes_per_frame
;
1908 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet_len
);
1910 WARN("ReleaseBuffer failed: %08x\n", hr
);
1913 WARN("losing %u frames\n", packet
);
1914 device
->played_frames
+= packet_len
- packet
;
1918 cb_info
= device
->cb_info
;
1920 LeaveCriticalSection(&device
->lock
);
1923 last
->lpNext
= NULL
;
1925 WAVEHDR
*next
= first
->lpNext
;
1926 first
->dwFlags
&= ~WHDR_INQUEUE
;
1927 first
->dwFlags
|= WHDR_DONE
;
1928 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
1934 static MMRESULT
WINMM_BeginPlaying(WINMM_Device
*device
)
1938 TRACE("(%p)\n", device
->handle
);
1941 /* prebuffer data before starting */
1942 WOD_PushData(device
);
1944 if(device
->stopped
){
1945 device
->stopped
= FALSE
;
1947 hr
= IAudioClient_Start(device
->client
);
1948 if(FAILED(hr
) && hr
!= AUDCLNT_E_NOT_STOPPED
){
1949 device
->stopped
= TRUE
;
1950 WARN("Start failed: %08x\n", hr
);
1951 return MMSYSERR_ERROR
;
1955 return MMSYSERR_NOERROR
;
1958 static LRESULT
WINMM_Pause(HWAVE hwave
)
1960 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1963 TRACE("(%p)\n", hwave
);
1965 if(!WINMM_ValidateAndLock(device
))
1966 return MMSYSERR_INVALHANDLE
;
1968 hr
= IAudioClient_Stop(device
->client
);
1970 LeaveCriticalSection(&device
->lock
);
1971 WARN("Stop failed: %08x\n", hr
);
1972 return MMSYSERR_ERROR
;
1975 device
->stopped
= FALSE
;
1977 LeaveCriticalSection(&device
->lock
);
1979 return MMSYSERR_NOERROR
;
1982 static LRESULT
WINMM_Reset(HWAVE hwave
)
1984 WINMM_CBInfo cb_info
;
1985 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1990 TRACE("(%p)\n", hwave
);
1992 if(!WINMM_ValidateAndLock(device
))
1993 return MMSYSERR_INVALHANDLE
;
1995 hr
= IAudioClient_Stop(device
->client
);
1997 LeaveCriticalSection(&device
->lock
);
1998 WARN("Stop failed: %08x\n", hr
);
1999 return MMSYSERR_ERROR
;
2001 device
->stopped
= TRUE
;
2003 first
= device
->first
;
2004 device
->first
= device
->last
= device
->playing
= NULL
;
2005 device
->ofs_bytes
= 0;
2006 device
->played_frames
= 0;
2007 device
->loop_counter
= 0;
2008 device
->last_clock_pos
= 0;
2009 IAudioClient_Reset(device
->client
);
2011 cb_info
= device
->cb_info
;
2012 is_out
= device
->render
!= NULL
;
2014 LeaveCriticalSection(&device
->lock
);
2017 WAVEHDR
*next
= first
->lpNext
;
2018 first
->dwFlags
&= ~WHDR_INQUEUE
;
2019 first
->dwFlags
|= WHDR_DONE
;
2021 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
2023 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
2027 return MMSYSERR_NOERROR
;
2030 static MMRESULT
WINMM_FramesToMMTime(MMTIME
*time
, UINT32 played_frames
,
2031 UINT32 sample_rate
, UINT32 bytes_per_sec
)
2033 switch(time
->wType
){
2035 time
->u
.sample
= played_frames
;
2036 return MMSYSERR_NOERROR
;
2038 time
->u
.ms
= (UINT64
)played_frames
* 1000 / sample_rate
;
2039 return MMSYSERR_NOERROR
;
2041 time
->u
.smpte
.fps
= 30;
2042 played_frames
+= sample_rate
/ time
->u
.smpte
.fps
- 1; /* round up */
2043 time
->u
.smpte
.frame
= (played_frames
% sample_rate
) * time
->u
.smpte
.fps
/ sample_rate
;
2044 played_frames
/= sample_rate
; /* yields seconds */
2045 time
->u
.smpte
.sec
= played_frames
% 60;
2046 played_frames
/= 60;
2047 time
->u
.smpte
.min
= played_frames
% 60;
2048 time
->u
.smpte
.hour
= played_frames
/ 60;
2049 return MMSYSERR_NOERROR
;
2051 time
->wType
= TIME_BYTES
;
2054 time
->u
.cb
= MulDiv(played_frames
, bytes_per_sec
, sample_rate
);
2055 return MMSYSERR_NOERROR
;
2059 static LRESULT
WINMM_GetPosition(HWAVE hwave
, MMTIME
*time
)
2061 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
2062 UINT32 played_frames
, sample_rate
, bytes_per_sec
;
2064 TRACE("(%p, %p)\n", hwave
, time
);
2066 if(!WINMM_ValidateAndLock(device
))
2067 return MMSYSERR_INVALHANDLE
;
2069 played_frames
= device
->played_frames
;
2070 sample_rate
= device
->orig_fmt
->nSamplesPerSec
;
2071 bytes_per_sec
= device
->orig_fmt
->nAvgBytesPerSec
;
2073 LeaveCriticalSection(&device
->lock
);
2075 return WINMM_FramesToMMTime(time
, played_frames
, sample_rate
, bytes_per_sec
);
2078 static WINMM_MMDevice
*WINMM_GetMixerMMDevice(HMIXEROBJ hmix
, DWORD flags
,
2081 UINT mmdev
, dev
, junk
, *out
;
2089 switch(flags
& 0xF0000000){
2090 case MIXER_OBJECTF_MIXER
: /* == 0 */
2091 *out
= HandleToULong(hmix
);
2092 if(*out
< g_outmmdevices_count
)
2093 return read_map(g_out_map
, *out
);
2094 if(*out
- g_outmmdevices_count
< g_inmmdevices_count
){
2095 *out
-= g_outmmdevices_count
;
2096 return read_map(g_in_map
, *out
);
2098 /* fall through -- if it's not a valid mixer device, then
2099 * it could be a valid mixer handle. windows seems to do
2101 case MIXER_OBJECTF_HMIXER
:
2102 case MIXER_OBJECTF_HWAVEOUT
:
2103 case MIXER_OBJECTF_HWAVEIN
:
2104 WINMM_DecomposeHWAVE((HWAVE
)hmix
, out
, &is_out
, &dev
, &junk
);
2105 if(junk
!= 0x1 || (is_out
&& *out
>= g_outmmdevices_count
) ||
2106 (!is_out
&& *out
>= g_inmmdevices_count
))
2109 return read_map(g_out_map
, *out
);
2110 return read_map(g_in_map
, *out
);
2111 case MIXER_OBJECTF_WAVEOUT
:
2112 *out
= HandleToULong(hmix
);
2113 if(*out
< g_outmmdevices_count
)
2114 return read_map(g_out_map
, *out
);
2116 case MIXER_OBJECTF_WAVEIN
:
2117 *out
= HandleToULong(hmix
);
2118 if(*out
< g_inmmdevices_count
)
2119 return read_map(g_in_map
, *out
);
2126 static MMRESULT
WINMM_SetupMMDeviceVolume(WINMM_MMDevice
*mmdevice
)
2128 IAudioSessionManager
*sesman
;
2132 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
, &device
);
2134 WARN("Device %s (%s) unavailable: %08x\n",
2135 wine_dbgstr_w(mmdevice
->dev_id
),
2136 wine_dbgstr_w(mmdevice
->out_caps
.szPname
), hr
);
2137 return MMSYSERR_ERROR
;
2140 hr
= IMMDevice_Activate(device
, &IID_IAudioSessionManager
,
2141 CLSCTX_INPROC_SERVER
, NULL
, (void**)&sesman
);
2143 WARN("Activate failed: %08x\n", hr
);
2144 IMMDevice_Release(device
);
2145 return MMSYSERR_ERROR
;
2148 IMMDevice_Release(device
);
2150 hr
= IAudioSessionManager_GetSimpleAudioVolume(sesman
, &mmdevice
->session
,
2151 FALSE
, &mmdevice
->volume
);
2152 IAudioSessionManager_Release(sesman
);
2154 WARN("GetSimpleAudioVolume failed: %08x\n", hr
);
2155 return MMSYSERR_ERROR
;
2158 return MMSYSERR_NOERROR
;
2161 static LRESULT
MXD_GetControlDetails(WINMM_ControlDetails
*details
)
2163 WINMM_MMDevice
*mmdevice
;
2164 MIXERCONTROLDETAILS
*control
= details
->details
;
2167 TRACE("(%p)\n", details
->hmix
);
2169 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
2171 return MMSYSERR_INVALHANDLE
;
2173 EnterCriticalSection(&mmdevice
->lock
);
2175 if(!mmdevice
->volume
){
2178 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
2179 if(mr
!= MMSYSERR_NOERROR
){
2180 LeaveCriticalSection(&mmdevice
->lock
);
2185 if(control
->dwControlID
== 0){
2187 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
2189 if(!control
->paDetails
||
2190 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
2191 LeaveCriticalSection(&mmdevice
->lock
);
2192 return MMSYSERR_INVALPARAM
;
2195 hr
= ISimpleAudioVolume_GetMasterVolume(mmdevice
->volume
, &vol
);
2197 WARN("GetMasterVolume failed: %08x\n", hr
);
2198 LeaveCriticalSection(&mmdevice
->lock
);
2199 return MMSYSERR_ERROR
;
2202 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
2203 udet
->dwValue
= vol
* ((unsigned int)0xFFFF);
2204 }else if(control
->dwControlID
== 1){
2206 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
2208 if(!control
->paDetails
||
2209 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
2210 LeaveCriticalSection(&mmdevice
->lock
);
2211 return MMSYSERR_INVALPARAM
;
2214 hr
= ISimpleAudioVolume_GetMute(mmdevice
->volume
, &mute
);
2216 WARN("GetMute failed: %08x\n", hr
);
2217 LeaveCriticalSection(&mmdevice
->lock
);
2218 return MMSYSERR_ERROR
;
2221 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
2222 bdet
->fValue
= mute
;
2223 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2224 FIXME("What should the sw-side mixer controls map to?\n");
2226 LeaveCriticalSection(&mmdevice
->lock
);
2227 return MIXERR_INVALCONTROL
;
2230 LeaveCriticalSection(&mmdevice
->lock
);
2232 return MMSYSERR_NOERROR
;
2235 static LRESULT
MXD_SetControlDetails(WINMM_ControlDetails
*details
)
2237 WINMM_MMDevice
*mmdevice
;
2238 MIXERCONTROLDETAILS
*control
= details
->details
;
2241 TRACE("(%p)\n", details
->hmix
);
2243 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
2245 return MMSYSERR_INVALHANDLE
;
2247 EnterCriticalSection(&mmdevice
->lock
);
2249 if(!mmdevice
->volume
){
2252 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
2253 if(mr
!= MMSYSERR_NOERROR
){
2254 LeaveCriticalSection(&mmdevice
->lock
);
2259 if(control
->dwControlID
== 0){
2261 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
2263 if(!control
->paDetails
||
2264 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
2265 LeaveCriticalSection(&mmdevice
->lock
);
2266 return MMSYSERR_INVALPARAM
;
2269 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
2271 if(udet
->dwValue
> 65535){
2272 LeaveCriticalSection(&mmdevice
->lock
);
2273 return MMSYSERR_INVALPARAM
;
2276 vol
= udet
->dwValue
/ 65535.f
;
2278 hr
= ISimpleAudioVolume_SetMasterVolume(mmdevice
->volume
, vol
, NULL
);
2280 WARN("SetMasterVolume failed: %08x\n", hr
);
2281 LeaveCriticalSection(&mmdevice
->lock
);
2282 return MMSYSERR_ERROR
;
2284 }else if(control
->dwControlID
== 1){
2286 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
2288 if(!control
->paDetails
||
2289 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
2290 LeaveCriticalSection(&mmdevice
->lock
);
2291 return MMSYSERR_INVALPARAM
;
2294 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
2295 mute
= bdet
->fValue
;
2297 hr
= ISimpleAudioVolume_SetMute(mmdevice
->volume
, mute
, NULL
);
2299 WARN("SetMute failed: %08x\n", hr
);
2300 LeaveCriticalSection(&mmdevice
->lock
);
2301 return MMSYSERR_ERROR
;
2303 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2304 FIXME("What should the sw-side mixer controls map to?\n");
2306 LeaveCriticalSection(&mmdevice
->lock
);
2307 return MIXERR_INVALCONTROL
;
2310 LeaveCriticalSection(&mmdevice
->lock
);
2312 return MMSYSERR_NOERROR
;
2315 static LRESULT
DRV_QueryDeviceInterface(WINMM_QueryInterfaceInfo
*info
)
2317 WINMM_MMDevice
*mmdevice
;
2324 static const PROPERTYKEY deviceinterface_key
= {
2325 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
2328 if(WINMM_IsMapper(info
->index
)){
2330 if(*info
->len_bytes
< sizeof(WCHAR
))
2331 return MMSYSERR_INVALPARAM
;
2334 *info
->len_bytes
= sizeof(WCHAR
);
2335 return MMSYSERR_NOERROR
;
2339 if(info
->index
>= g_outmmdevices_count
)
2340 return MMSYSERR_INVALHANDLE
;
2342 mmdevice
= &g_out_mmdevices
[info
->index
];
2344 if(info
->index
>= g_inmmdevices_count
)
2345 return MMSYSERR_INVALHANDLE
;
2347 mmdevice
= &g_in_mmdevices
[info
->index
];
2350 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
,
2353 WARN("Device %s unavailable: %08x\n", wine_dbgstr_w(mmdevice
->dev_id
), hr
);
2354 return MMSYSERR_ERROR
;
2357 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
2359 WARN("OpenPropertyStore failed: %08x\n", hr
);
2360 IMMDevice_Release(device
);
2361 return MMSYSERR_ERROR
;
2364 PropVariantInit(&pv
);
2365 hr
= IPropertyStore_GetValue(ps
, &deviceinterface_key
, &pv
);
2367 WARN("GetValue failed: %08x\n", hr
);
2368 IPropertyStore_Release(ps
);
2369 IMMDevice_Release(device
);
2370 return MMSYSERR_ERROR
;
2372 if(pv
.vt
!= VT_LPWSTR
){
2373 WARN("Got unexpected property type: %u\n", pv
.vt
);
2374 PropVariantClear(&pv
);
2375 IPropertyStore_Release(ps
);
2376 IMMDevice_Release(device
);
2377 return MMSYSERR_ERROR
;
2380 len_bytes
= (lstrlenW(pv
.u
.pwszVal
) + 1) * sizeof(WCHAR
);
2383 if(len_bytes
> *info
->len_bytes
){
2384 PropVariantClear(&pv
);
2385 IPropertyStore_Release(ps
);
2386 IMMDevice_Release(device
);
2387 return MMSYSERR_INVALPARAM
;
2390 memcpy(info
->str
, pv
.u
.pwszVal
, len_bytes
);
2392 *info
->len_bytes
= len_bytes
;
2394 PropVariantClear(&pv
);
2395 IPropertyStore_Release(ps
);
2396 IMMDevice_Release(device
);
2398 return MMSYSERR_NOERROR
;
2401 static LRESULT CALLBACK
WINMM_DevicesMsgProc(HWND hwnd
, UINT msg
, WPARAM wparam
,
2406 return WOD_Open((WINMM_OpenInfo
*)wparam
);
2408 return WOD_Close((HWAVEOUT
)wparam
);
2410 return WID_Open((WINMM_OpenInfo
*)wparam
);
2412 return WID_Close((HWAVEIN
)wparam
);
2413 case MXDM_GETCONTROLDETAILS
:
2414 return MXD_GetControlDetails((WINMM_ControlDetails
*)wparam
);
2415 case MXDM_SETCONTROLDETAILS
:
2416 return MXD_SetControlDetails((WINMM_ControlDetails
*)wparam
);
2417 case DRV_QUERYDEVICEINTERFACESIZE
:
2418 case DRV_QUERYDEVICEINTERFACE
:
2419 return DRV_QueryDeviceInterface((WINMM_QueryInterfaceInfo
*)wparam
);
2421 return DefWindowProcW(hwnd
, msg
, wparam
, lparam
);
2424 static BOOL
WINMM_DevicesThreadDone(void)
2428 EnterCriticalSection(&g_devthread_lock
);
2430 if(g_devthread_token
> 0){
2431 LeaveCriticalSection(&g_devthread_lock
);
2435 for(i
= 0; i
< g_devhandle_count
; ++i
){
2436 if(g_handle_devices
[i
]->open
){
2437 LeaveCriticalSection(&g_devthread_lock
);
2442 DestroyWindow(g_devices_hwnd
);
2443 g_devices_hwnd
= NULL
;
2444 IMMDeviceEnumerator_Release(g_devenum
);
2448 LeaveCriticalSection(&g_devthread_lock
);
2453 static DWORD WINAPI
WINMM_DevicesThreadProc(void *arg
)
2457 static const WCHAR messageW
[] = {'M','e','s','s','a','g','e',0};
2459 hr
= CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
2461 WARN("CoInitializeEx failed: %08x\n", hr
);
2462 FreeLibraryAndExitThread(g_devthread_module
, 1);
2465 hr
= WINMM_InitMMDevices();
2468 FreeLibraryAndExitThread(g_devthread_module
, 1);
2471 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
2472 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&g_devenum
);
2474 WARN("CoCreateInstance failed: %08x\n", hr
);
2476 FreeLibraryAndExitThread(g_devthread_module
, 1);
2479 g_devices_hwnd
= CreateWindowW(messageW
, NULL
, 0, 0, 0, 0, 0,
2480 HWND_MESSAGE
, NULL
, NULL
, NULL
);
2481 if(!g_devices_hwnd
){
2482 WARN("CreateWindow failed: %d\n", GetLastError());
2484 FreeLibraryAndExitThread(g_devthread_module
, 1);
2487 SetWindowLongPtrW(g_devices_hwnd
, GWLP_WNDPROC
,
2488 (LONG_PTR
)WINMM_DevicesMsgProc
);
2490 /* inform caller that the thread is ready to process messages */
2492 evt
= NULL
; /* do not use after this point */
2496 wait
= MsgWaitForMultipleObjects(g_devhandle_count
, g_device_handles
,
2497 FALSE
, INFINITE
, QS_ALLINPUT
);
2498 if(wait
== g_devhandle_count
+ WAIT_OBJECT_0
){
2500 if(PeekMessageW(&msg
, g_devices_hwnd
, 0, 0, PM_REMOVE
))
2501 WARN("Unexpected message: 0x%x\n", msg
.message
);
2504 }else if(wait
< g_devhandle_count
+ WAIT_OBJECT_0
){
2505 WINMM_Device
*device
= g_handle_devices
[wait
- WAIT_OBJECT_0
];
2507 WOD_PushData(device
);
2509 WID_PullData(device
);
2511 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait
,
2513 if(WINMM_DevicesThreadDone()){
2514 TRACE("Quitting devices thread\n");
2515 FreeLibraryAndExitThread(g_devthread_module
, 0);
2519 FreeLibraryAndExitThread(g_devthread_module
, 0);
2522 /* on success, increments g_devthread_token to prevent
2523 * device thread shutdown. caller must decrement. */
2524 static BOOL
WINMM_StartDevicesThread(void)
2529 EnterCriticalSection(&g_devthread_lock
);
2532 wait
= WaitForSingleObject(g_devices_thread
, 0);
2533 if(wait
== WAIT_TIMEOUT
){
2534 /* thread still running */
2535 InterlockedIncrement(&g_devthread_token
);
2536 LeaveCriticalSection(&g_devthread_lock
);
2539 if(wait
!= WAIT_OBJECT_0
){
2541 LeaveCriticalSection(&g_devthread_lock
);
2544 TRACE("Devices thread left dangling message window?\n");
2545 g_devices_hwnd
= NULL
;
2546 CloseHandle(g_devices_thread
);
2547 g_devices_thread
= NULL
;
2548 }else if(g_devices_thread
){
2549 WaitForSingleObject(g_devices_thread
, INFINITE
);
2550 CloseHandle(g_devices_thread
);
2551 g_devices_thread
= NULL
;
2554 TRACE("Starting up devices thread\n");
2556 /* The devices thread holds a reference to the winmm module
2557 * to prevent it from unloading while it's running. */
2558 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
2559 (const WCHAR
*)WINMM_StartDevicesThread
, &g_devthread_module
);
2561 events
[0] = CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2563 g_devices_thread
= CreateThread(NULL
, 0, WINMM_DevicesThreadProc
,
2564 events
[0], 0, NULL
);
2565 if(!g_devices_thread
){
2566 LeaveCriticalSection(&g_devthread_lock
);
2567 CloseHandle(events
[0]);
2568 FreeLibrary(g_devthread_module
);
2572 events
[1] = g_devices_thread
;
2573 wait
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
2574 CloseHandle(events
[0]);
2575 if(wait
!= WAIT_OBJECT_0
){
2576 if(wait
== 1 + WAIT_OBJECT_0
){
2577 CloseHandle(g_devices_thread
);
2578 g_devices_thread
= NULL
;
2579 g_devices_hwnd
= NULL
;
2581 LeaveCriticalSection(&g_devthread_lock
);
2585 InterlockedIncrement(&g_devthread_token
);
2587 LeaveCriticalSection(&g_devthread_lock
);
2592 /**************************************************************************
2593 * waveOutGetNumDevs [WINMM.@]
2595 UINT WINAPI
waveOutGetNumDevs(void)
2597 HRESULT hr
= WINMM_InitMMDevices();
2601 TRACE("count: %u\n", g_outmmdevices_count
);
2603 return g_outmmdevices_count
;
2606 /**************************************************************************
2607 * waveOutGetDevCapsA [WINMM.@]
2609 UINT WINAPI
waveOutGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEOUTCAPSA lpCaps
,
2615 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2618 return MMSYSERR_INVALPARAM
;
2620 ret
= waveOutGetDevCapsW(uDeviceID
, &wocW
, sizeof(wocW
));
2622 if (ret
== MMSYSERR_NOERROR
) {
2624 wocA
.wMid
= wocW
.wMid
;
2625 wocA
.wPid
= wocW
.wPid
;
2626 wocA
.vDriverVersion
= wocW
.vDriverVersion
;
2627 WideCharToMultiByte( CP_ACP
, 0, wocW
.szPname
, -1, wocA
.szPname
,
2628 sizeof(wocA
.szPname
), NULL
, NULL
);
2629 wocA
.dwFormats
= wocW
.dwFormats
;
2630 wocA
.wChannels
= wocW
.wChannels
;
2631 wocA
.dwSupport
= wocW
.dwSupport
;
2632 memcpy(lpCaps
, &wocA
, min(uSize
, sizeof(wocA
)));
2637 /**************************************************************************
2638 * waveOutGetDevCapsW [WINMM.@]
2640 UINT WINAPI
waveOutGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEOUTCAPSW lpCaps
,
2643 WAVEOUTCAPSW mapper_caps
, *caps
;
2646 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2648 hr
= WINMM_InitMMDevices();
2650 return MMSYSERR_NODRIVER
;
2652 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
2654 if(WINMM_IsMapper(uDeviceID
)){
2655 mapper_caps
.wMid
= 0xFF;
2656 mapper_caps
.wPid
= 0xFF;
2657 mapper_caps
.vDriverVersion
= 0x00010001;
2658 mapper_caps
.dwFormats
= 0xFFFFFFFF;
2659 mapper_caps
.wReserved1
= 0;
2660 mapper_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
2661 WAVECAPS_SAMPLEACCURATE
;
2662 mapper_caps
.wChannels
= 2;
2663 LoadStringW(hWinMM32Instance
, IDS_MAPPER_NAME
, mapper_caps
.szPname
, MAXPNAMELEN
);
2665 caps
= &mapper_caps
;
2667 if(uDeviceID
>= g_outmmdevices_count
)
2668 return MMSYSERR_BADDEVICEID
;
2670 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, %lx, %lx, %08x)\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: %u\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
;
2897 mr
= WINMM_BeginPlaying(device
);
2899 LeaveCriticalSection(&device
->lock
);
2904 /**************************************************************************
2905 * waveOutBreakLoop [WINMM.@]
2907 UINT WINAPI
waveOutBreakLoop(HWAVEOUT hWaveOut
)
2909 WINMM_Device
*device
;
2911 TRACE("(%p)\n", hWaveOut
);
2913 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2915 if(!WINMM_ValidateAndLock(device
))
2916 return MMSYSERR_INVALHANDLE
;
2918 device
->loop_counter
= 0;
2920 LeaveCriticalSection(&device
->lock
);
2922 return MMSYSERR_NOERROR
;
2925 /**************************************************************************
2926 * waveOutPause [WINMM.@]
2928 UINT WINAPI
waveOutPause(HWAVEOUT hWaveOut
)
2930 TRACE("(%p)\n", hWaveOut
);
2932 return WINMM_Pause((HWAVE
)hWaveOut
);
2935 /**************************************************************************
2936 * waveOutReset [WINMM.@]
2938 UINT WINAPI
waveOutReset(HWAVEOUT hWaveOut
)
2940 TRACE("(%p)\n", hWaveOut
);
2942 return WINMM_Reset((HWAVE
)hWaveOut
);
2945 /**************************************************************************
2946 * waveOutRestart [WINMM.@]
2948 UINT WINAPI
waveOutRestart(HWAVEOUT hWaveOut
)
2950 WINMM_Device
*device
;
2953 TRACE("(%p)\n", hWaveOut
);
2955 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2957 if(!WINMM_ValidateAndLock(device
))
2958 return MMSYSERR_INVALHANDLE
;
2960 device
->stopped
= TRUE
;
2962 mr
= WINMM_BeginPlaying(device
);
2964 LeaveCriticalSection(&device
->lock
);
2969 /**************************************************************************
2970 * waveOutGetPosition [WINMM.@]
2972 UINT WINAPI
waveOutGetPosition(HWAVEOUT hWaveOut
, LPMMTIME lpTime
,
2975 TRACE("(%p, %p, %u)\n", hWaveOut
, lpTime
, uSize
);
2977 if(!uSize
|| !lpTime
)
2978 return MMSYSERR_INVALPARAM
;
2980 if(uSize
< sizeof(MMTIME
))
2981 return MMSYSERR_ERROR
;
2983 return WINMM_GetPosition((HWAVE
)hWaveOut
, lpTime
);
2986 /**************************************************************************
2987 * waveOutGetPitch [WINMM.@]
2989 UINT WINAPI
waveOutGetPitch(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
2991 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
2992 return MMSYSERR_NOTSUPPORTED
;
2995 /**************************************************************************
2996 * waveOutSetPitch [WINMM.@]
2998 UINT WINAPI
waveOutSetPitch(HWAVEOUT hWaveOut
, DWORD dw
)
3000 TRACE("(%p, %08x)\n", hWaveOut
, dw
);
3002 return MMSYSERR_NOTSUPPORTED
;
3005 /**************************************************************************
3006 * waveOutGetPlaybackRate [WINMM.@]
3008 UINT WINAPI
waveOutGetPlaybackRate(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
3010 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
3012 return MMSYSERR_NOTSUPPORTED
;
3015 /**************************************************************************
3016 * waveOutSetPlaybackRate [WINMM.@]
3018 UINT WINAPI
waveOutSetPlaybackRate(HWAVEOUT hWaveOut
, DWORD dw
)
3020 TRACE("(%p, %08x)\n", hWaveOut
, dw
);
3022 return MMSYSERR_NOTSUPPORTED
;
3025 /**************************************************************************
3026 * waveOutGetVolume [WINMM.@]
3028 UINT WINAPI
waveOutGetVolume(HWAVEOUT hWaveOut
, DWORD
*out
)
3030 WINMM_Device
*device
;
3035 TRACE("(%p, %p)\n", hWaveOut
, out
);
3038 return MMSYSERR_INVALPARAM
;
3040 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3042 if(!WINMM_ValidateAndLock(device
))
3043 return MMSYSERR_INVALHANDLE
;
3045 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
3047 LeaveCriticalSection(&device
->lock
);
3048 WARN("GetChannelCount failed: %08x\n", hr
);
3049 return MMSYSERR_ERROR
;
3052 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
3054 LeaveCriticalSection(&device
->lock
);
3055 return MMSYSERR_NOMEM
;
3058 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
3060 LeaveCriticalSection(&device
->lock
);
3061 HeapFree(GetProcessHeap(), 0, vols
);
3062 WARN("GetAllVolumes failed: %08x\n", hr
);
3063 return MMSYSERR_ERROR
;
3066 LeaveCriticalSection(&device
->lock
);
3068 *out
= ((UINT16
)(vols
[0] * (DWORD
)0xFFFF));
3070 *out
|= ((UINT16
)(vols
[1] * (DWORD
)0xFFFF)) << 16;
3072 HeapFree(GetProcessHeap(), 0, vols
);
3074 return MMSYSERR_NOERROR
;
3077 /**************************************************************************
3078 * waveOutSetVolume [WINMM.@]
3080 UINT WINAPI
waveOutSetVolume(HWAVEOUT hWaveOut
, DWORD in
)
3082 WINMM_Device
*device
;
3087 TRACE("(%p, %08x)\n", hWaveOut
, in
);
3089 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3091 if(!WINMM_ValidateAndLock(device
))
3092 return MMSYSERR_INVALHANDLE
;
3094 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
3096 LeaveCriticalSection(&device
->lock
);
3097 WARN("GetChannelCount failed: %08x\n", hr
);
3098 return MMSYSERR_ERROR
;
3101 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
3103 LeaveCriticalSection(&device
->lock
);
3104 return MMSYSERR_NOMEM
;
3107 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
3109 LeaveCriticalSection(&device
->lock
);
3110 HeapFree(GetProcessHeap(), 0, vols
);
3111 WARN("GetAllVolumes failed: %08x\n", hr
);
3112 return MMSYSERR_ERROR
;
3115 vols
[0] = (float)((DWORD
)(in
& 0xFFFF) / (float)0xFFFF);
3117 vols
[1] = (float)((DWORD
)(in
>> 16) / (float)0xFFFF);
3119 hr
= IAudioStreamVolume_SetAllVolumes(device
->volume
, channels
, vols
);
3121 LeaveCriticalSection(&device
->lock
);
3122 HeapFree(GetProcessHeap(), 0, vols
);
3123 WARN("SetAllVolumes failed: %08x\n", hr
);
3124 return MMSYSERR_ERROR
;
3127 LeaveCriticalSection(&device
->lock
);
3129 HeapFree(GetProcessHeap(), 0, vols
);
3131 return MMSYSERR_NOERROR
;
3134 /**************************************************************************
3135 * waveOutGetID [WINMM.@]
3137 UINT WINAPI
waveOutGetID(HWAVEOUT hWaveOut
, UINT
* lpuDeviceID
)
3139 WINMM_Device
*device
;
3143 TRACE("(%p, %p)\n", hWaveOut
, lpuDeviceID
);
3146 return MMSYSERR_INVALPARAM
;
3148 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
3149 if(!WINMM_ValidateAndLock(device
))
3150 return MMSYSERR_INVALHANDLE
;
3152 LeaveCriticalSection(&device
->lock
);
3154 WINMM_DecomposeHWAVE((HWAVE
)hWaveOut
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3156 return MMSYSERR_NOERROR
;
3159 static UINT
WINMM_QueryInstanceIDSize(UINT device
, DWORD_PTR
*len
, BOOL is_out
)
3162 WINMM_MMDevice
**devices
;
3164 TRACE("(%u, %p, %d)\n", device
, len
, is_out
);
3167 count
= g_outmmdevices_count
;
3168 devices
= g_out_map
;
3170 count
= g_inmmdevices_count
;
3175 return MMSYSERR_INVALHANDLE
;
3177 EnterCriticalSection(&g_devthread_lock
);
3178 *len
= (lstrlenW(devices
[device
]->dev_id
) + 1) * sizeof(WCHAR
);
3179 LeaveCriticalSection(&g_devthread_lock
);
3181 return MMSYSERR_NOERROR
;
3184 static UINT
WINMM_QueryInstanceID(UINT device
, WCHAR
*str
, DWORD_PTR len
,
3188 WINMM_MMDevice
**devices
;
3190 TRACE("(%u, %p, %d)\n", device
, str
, is_out
);
3193 count
= g_outmmdevices_count
;
3194 devices
= g_out_map
;
3196 count
= g_inmmdevices_count
;
3201 return MMSYSERR_INVALHANDLE
;
3203 EnterCriticalSection(&g_devthread_lock
);
3204 id_len
= (lstrlenW(devices
[device
]->dev_id
) + 1) * sizeof(WCHAR
);
3206 LeaveCriticalSection(&g_devthread_lock
);
3207 return MMSYSERR_ERROR
;
3210 memcpy(str
, devices
[device
]->dev_id
, id_len
);
3211 LeaveCriticalSection(&g_devthread_lock
);
3213 return MMSYSERR_NOERROR
;
3216 static UINT
get_device_interface(UINT msg
, BOOL is_out
, UINT index
, WCHAR
*out
, ULONG
*out_len
)
3218 WINMM_QueryInterfaceInfo info
;
3221 if(!WINMM_StartDevicesThread())
3222 return MMSYSERR_NODRIVER
;
3224 info
.is_out
= is_out
;
3227 info
.len_bytes
= out_len
;
3229 ret
= SendMessageW(g_devices_hwnd
, msg
, (DWORD_PTR
)&info
, 0);
3230 InterlockedDecrement(&g_devthread_token
);
3234 /**************************************************************************
3235 * waveOutMessage [WINMM.@]
3237 UINT WINAPI
waveOutMessage(HWAVEOUT hWaveOut
, UINT uMessage
,
3238 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3240 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut
, uMessage
, dwParam1
, dwParam2
);
3243 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3244 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut
),
3245 (DWORD_PTR
*)dwParam1
, TRUE
);
3246 case DRV_QUERYFUNCTIONINSTANCEID
:
3247 return WINMM_QueryInstanceID(HandleToULong(hWaveOut
), (WCHAR
*)dwParam1
, dwParam2
, TRUE
);
3248 case DRV_QUERYDEVICEINTERFACESIZE
:
3249 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE
, TRUE
, HandleToULong(hWaveOut
),
3250 NULL
, (ULONG
*)dwParam1
);
3251 case DRV_QUERYDEVICEINTERFACE
:
3253 ULONG size
= dwParam2
;
3254 return get_device_interface(DRV_QUERYDEVICEINTERFACE
, TRUE
, HandleToULong(hWaveOut
),
3255 (WCHAR
*)dwParam1
, &size
);
3257 case DRV_QUERYMAPPABLE
:
3258 return MMSYSERR_NOERROR
;
3259 case DRVM_MAPPER_PREFERRED_GET
:
3261 if(g_outmmdevices_count
> 0)
3262 /* Device 0 is always the default device */
3263 *(DWORD
*)dwParam1
= 0;
3265 *(DWORD
*)dwParam1
= -1;
3270 *(DWORD
*)dwParam2
= 0;
3272 return MMSYSERR_NOERROR
;
3275 TRACE("Message not supported: %u\n", uMessage
);
3277 return MMSYSERR_NOTSUPPORTED
;
3280 /**************************************************************************
3281 * waveInGetNumDevs [WINMM.@]
3283 UINT WINAPI
waveInGetNumDevs(void)
3285 HRESULT hr
= WINMM_InitMMDevices();
3289 TRACE("count: %u\n", g_inmmdevices_count
);
3291 return g_inmmdevices_count
;
3294 /**************************************************************************
3295 * waveInGetDevCapsW [WINMM.@]
3297 UINT WINAPI
waveInGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEINCAPSW lpCaps
, UINT uSize
)
3299 WAVEINCAPSW mapper_caps
, *caps
;
3302 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3304 hr
= WINMM_InitMMDevices();
3306 return MMSYSERR_NODRIVER
;
3309 return MMSYSERR_INVALPARAM
;
3311 if(WINMM_IsMapper(uDeviceID
)){
3312 mapper_caps
.wMid
= 0xFF;
3313 mapper_caps
.wPid
= 0xFF;
3314 mapper_caps
.vDriverVersion
= 0x00010001;
3315 mapper_caps
.dwFormats
= 0xFFFFFFFF;
3316 mapper_caps
.wReserved1
= 0;
3317 mapper_caps
.wChannels
= 2;
3318 LoadStringW(hWinMM32Instance
, IDS_MAPPER_NAME
, mapper_caps
.szPname
, MAXPNAMELEN
);
3320 caps
= &mapper_caps
;
3322 if(uDeviceID
>= g_inmmdevices_count
)
3323 return MMSYSERR_BADDEVICEID
;
3325 caps
= &read_map(g_in_map
, uDeviceID
)->in_caps
;
3328 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
3330 return MMSYSERR_NOERROR
;
3333 /**************************************************************************
3334 * waveInGetDevCapsA [WINMM.@]
3336 UINT WINAPI
waveInGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEINCAPSA lpCaps
, UINT uSize
)
3341 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3344 return MMSYSERR_INVALPARAM
;
3346 ret
= waveInGetDevCapsW(uDeviceID
, &wicW
, sizeof(wicW
));
3348 if (ret
== MMSYSERR_NOERROR
) {
3350 wicA
.wMid
= wicW
.wMid
;
3351 wicA
.wPid
= wicW
.wPid
;
3352 wicA
.vDriverVersion
= wicW
.vDriverVersion
;
3353 WideCharToMultiByte( CP_ACP
, 0, wicW
.szPname
, -1, wicA
.szPname
,
3354 sizeof(wicA
.szPname
), NULL
, NULL
);
3355 wicA
.dwFormats
= wicW
.dwFormats
;
3356 wicA
.wChannels
= wicW
.wChannels
;
3357 memcpy(lpCaps
, &wicA
, min(uSize
, sizeof(wicA
)));
3362 /**************************************************************************
3363 * waveInOpen [WINMM.@]
3365 MMRESULT WINAPI
waveInOpen(HWAVEIN
* lphWaveIn
, UINT uDeviceID
,
3366 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
3367 DWORD_PTR dwInstance
, DWORD dwFlags
)
3370 WINMM_OpenInfo info
;
3371 WINMM_CBInfo cb_info
;
3373 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn
, uDeviceID
, lpFormat
,
3374 dwCallback
, dwInstance
, dwFlags
);
3376 if(!lphWaveIn
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
3377 return MMSYSERR_INVALPARAM
;
3379 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
3380 if(res
!= MMSYSERR_NOERROR
)
3383 if(!WINMM_StartDevicesThread())
3384 return MMSYSERR_NODRIVER
;
3387 info
.format
= (WAVEFORMATEX
*)lpFormat
;
3388 info
.callback
= dwCallback
;
3389 info
.cb_user
= dwInstance
;
3390 info
.req_device
= uDeviceID
;
3391 info
.flags
= dwFlags
;
3394 res
= SendMessageW(g_devices_hwnd
, WIDM_OPEN
, (DWORD_PTR
)&info
, 0);
3395 InterlockedDecrement(&g_devthread_token
);
3396 if(res
!= MMSYSERR_NOERROR
|| (dwFlags
& WAVE_FORMAT_QUERY
))
3400 *lphWaveIn
= (HWAVEIN
)info
.handle
;
3402 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
3403 cb_info
.callback
= dwCallback
;
3404 cb_info
.user
= dwInstance
;
3405 cb_info
.hwave
= info
.handle
;
3407 WINMM_NotifyClient(&cb_info
, WIM_OPEN
, 0, 0);
3412 /**************************************************************************
3413 * waveInClose [WINMM.@]
3415 UINT WINAPI
waveInClose(HWAVEIN hWaveIn
)
3417 WINMM_Device
*device
;
3418 WINMM_CBInfo cb_info
;
3421 TRACE("(%p)\n", hWaveIn
);
3423 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3425 if(!WINMM_ValidateAndLock(device
))
3426 return MMSYSERR_INVALHANDLE
;
3428 cb_info
= device
->cb_info
;
3430 LeaveCriticalSection(&device
->lock
);
3432 res
= SendMessageW(g_devices_hwnd
, WIDM_CLOSE
, (WPARAM
)hWaveIn
, 0);
3434 if(res
== MMSYSERR_NOERROR
)
3435 WINMM_NotifyClient(&cb_info
, WIM_CLOSE
, 0, 0);
3440 /**************************************************************************
3441 * waveInPrepareHeader [WINMM.@]
3443 UINT WINAPI
waveInPrepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3446 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3448 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3449 return MMSYSERR_INVALPARAM
;
3451 if(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
)
3452 return MMSYSERR_NOERROR
;
3454 return WINMM_PrepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3457 /**************************************************************************
3458 * waveInUnprepareHeader [WINMM.@]
3460 UINT WINAPI
waveInUnprepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3463 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3465 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3466 return MMSYSERR_INVALPARAM
;
3468 if(lpWaveInHdr
->dwFlags
& WHDR_INQUEUE
)
3469 return WAVERR_STILLPLAYING
;
3471 if(!(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
))
3472 return MMSYSERR_NOERROR
;
3474 return WINMM_UnprepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3477 /**************************************************************************
3478 * waveInAddBuffer [WINMM.@]
3480 UINT WINAPI
waveInAddBuffer(HWAVEIN hWaveIn
, WAVEHDR
*header
, UINT uSize
)
3482 WINMM_Device
*device
;
3484 TRACE("(%p, %p, %u)\n", hWaveIn
, header
, uSize
);
3486 if(!header
|| uSize
< sizeof(WAVEHDR
))
3487 return MMSYSERR_INVALPARAM
;
3489 if(!(header
->dwFlags
& WHDR_PREPARED
))
3490 return WAVERR_UNPREPARED
;
3492 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3494 if(!WINMM_ValidateAndLock(device
))
3495 return MMSYSERR_INVALHANDLE
;
3498 device
->first
= device
->last
= header
;
3500 device
->last
->lpNext
= header
;
3501 device
->last
= header
;
3504 header
->dwBytesRecorded
= 0;
3505 header
->lpNext
= NULL
;
3506 header
->dwFlags
&= ~WHDR_DONE
;
3507 header
->dwFlags
|= WHDR_INQUEUE
;
3509 LeaveCriticalSection(&device
->lock
);
3511 return MMSYSERR_NOERROR
;
3514 /**************************************************************************
3515 * waveInReset [WINMM.@]
3517 UINT WINAPI
waveInReset(HWAVEIN hWaveIn
)
3519 TRACE("(%p)\n", hWaveIn
);
3521 return WINMM_Reset((HWAVE
)hWaveIn
);
3524 /**************************************************************************
3525 * waveInStart [WINMM.@]
3527 UINT WINAPI
waveInStart(HWAVEIN hWaveIn
)
3529 WINMM_Device
*device
;
3532 TRACE("(%p)\n", hWaveIn
);
3534 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3536 if(!WINMM_ValidateAndLock(device
))
3537 return MMSYSERR_INVALHANDLE
;
3539 mr
= WINMM_BeginPlaying(device
);
3541 LeaveCriticalSection(&device
->lock
);
3546 /**************************************************************************
3547 * waveInStop [WINMM.@]
3549 UINT WINAPI
waveInStop(HWAVEIN hWaveIn
)
3551 WINMM_CBInfo cb_info
;
3552 WINMM_Device
*device
;
3556 TRACE("(%p)\n", hWaveIn
);
3558 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3560 if(!WINMM_ValidateAndLock(device
))
3561 return MMSYSERR_INVALHANDLE
;
3563 hr
= WINMM_Pause((HWAVE
)hWaveIn
);
3565 LeaveCriticalSection(&device
->lock
);
3566 return MMSYSERR_ERROR
;
3568 device
->stopped
= TRUE
;
3570 buf
= device
->first
;
3571 if(buf
&& buf
->dwBytesRecorded
> 0){
3572 device
->first
= buf
->lpNext
;
3576 cb_info
= device
->cb_info
;
3578 LeaveCriticalSection(&device
->lock
);
3581 buf
->dwFlags
&= ~WHDR_INQUEUE
;
3582 buf
->dwFlags
|= WHDR_DONE
;
3583 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)buf
, 0);
3586 return MMSYSERR_NOERROR
;
3589 /**************************************************************************
3590 * waveInGetPosition [WINMM.@]
3592 UINT WINAPI
waveInGetPosition(HWAVEIN hWaveIn
, LPMMTIME lpTime
,
3595 TRACE("(%p, %p, %u)\n", hWaveIn
, lpTime
, uSize
);
3597 if(!uSize
|| !lpTime
)
3598 return MMSYSERR_INVALPARAM
;
3600 if(uSize
< sizeof(MMTIME
))
3601 return MMSYSERR_ERROR
;
3603 return WINMM_GetPosition((HWAVE
)hWaveIn
, lpTime
);
3606 /**************************************************************************
3607 * waveInGetID [WINMM.@]
3609 UINT WINAPI
waveInGetID(HWAVEIN hWaveIn
, UINT
* lpuDeviceID
)
3613 WINMM_Device
*device
;
3615 TRACE("(%p, %p)\n", hWaveIn
, lpuDeviceID
);
3618 return MMSYSERR_INVALPARAM
;
3620 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3621 if(!WINMM_ValidateAndLock(device
))
3622 return MMSYSERR_INVALHANDLE
;
3624 LeaveCriticalSection(&device
->lock
);
3626 WINMM_DecomposeHWAVE((HWAVE
)hWaveIn
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3628 return MMSYSERR_NOERROR
;
3631 /**************************************************************************
3632 * waveInMessage [WINMM.@]
3634 UINT WINAPI
waveInMessage(HWAVEIN hWaveIn
, UINT uMessage
,
3635 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3637 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn
, uMessage
, dwParam1
, dwParam2
);
3640 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3641 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn
),
3642 (DWORD_PTR
*)dwParam1
, FALSE
);
3643 case DRV_QUERYFUNCTIONINSTANCEID
:
3644 return WINMM_QueryInstanceID(HandleToULong(hWaveIn
), (WCHAR
*)dwParam1
, dwParam2
, FALSE
);
3645 case DRV_QUERYDEVICEINTERFACESIZE
:
3646 return get_device_interface(DRV_QUERYDEVICEINTERFACESIZE
, FALSE
, HandleToULong(hWaveIn
),
3647 NULL
, (ULONG
*)dwParam1
);
3648 case DRV_QUERYDEVICEINTERFACE
:
3650 ULONG size
= dwParam2
;
3651 return get_device_interface(DRV_QUERYDEVICEINTERFACE
, FALSE
, HandleToULong(hWaveIn
),
3652 (WCHAR
*)dwParam1
, &size
);
3654 case DRV_QUERYMAPPABLE
:
3655 return MMSYSERR_NOERROR
;
3656 case DRVM_MAPPER_PREFERRED_GET
:
3658 if(g_inmmdevices_count
> 0)
3659 /* Device 0 is always the default device */
3660 *(DWORD
*)dwParam1
= 0;
3662 *(DWORD
*)dwParam1
= -1;
3667 *(DWORD
*)dwParam2
= 0;
3669 return MMSYSERR_NOERROR
;
3672 TRACE("Message not supported: %u\n", uMessage
);
3674 return MMSYSERR_NOTSUPPORTED
;
3677 UINT WINAPI
mixerGetNumDevs(void)
3683 hr
= WINMM_InitMMDevices();
3687 return g_outmmdevices_count
+ g_inmmdevices_count
;
3690 /**************************************************************************
3691 * mixerGetDevCapsA [WINMM.@]
3693 UINT WINAPI
mixerGetDevCapsA(UINT_PTR uDeviceID
, LPMIXERCAPSA lpCaps
, UINT uSize
)
3698 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3701 return MMSYSERR_INVALPARAM
;
3703 ret
= mixerGetDevCapsW(uDeviceID
, &micW
, sizeof(micW
));
3705 if (ret
== MMSYSERR_NOERROR
) {
3707 micA
.wMid
= micW
.wMid
;
3708 micA
.wPid
= micW
.wPid
;
3709 micA
.vDriverVersion
= micW
.vDriverVersion
;
3710 WideCharToMultiByte( CP_ACP
, 0, micW
.szPname
, -1, micA
.szPname
,
3711 sizeof(micA
.szPname
), NULL
, NULL
);
3712 micA
.fdwSupport
= micW
.fdwSupport
;
3713 micA
.cDestinations
= micW
.cDestinations
;
3714 memcpy(lpCaps
, &micA
, min(uSize
, sizeof(micA
)));
3719 /**************************************************************************
3720 * mixerGetDevCapsW [WINMM.@]
3722 UINT WINAPI
mixerGetDevCapsW(UINT_PTR uDeviceID
, LPMIXERCAPSW lpCaps
, UINT uSize
)
3724 WINMM_MMDevice
*mmdevice
;
3728 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3730 hr
= WINMM_InitMMDevices();
3732 return MMSYSERR_NODRIVER
;
3735 return MMSYSERR_INVALPARAM
;
3738 return MMSYSERR_NOERROR
;
3740 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3741 mmdevice
= WINMM_GetMixerMMDevice((HMIXEROBJ
)uDeviceID
,
3742 MIXER_OBJECTF_MIXER
, NULL
);
3743 else if(uDeviceID
< g_outmmdevices_count
)
3744 mmdevice
= read_map(g_out_map
, uDeviceID
);
3746 mmdevice
= read_map(g_in_map
, uDeviceID
- g_outmmdevices_count
);
3749 return MMSYSERR_BADDEVICEID
;
3751 if(mmdevice
->dataflow
== eRender
)
3752 memcpy(caps
.szPname
, mmdevice
->out_caps
.szPname
, sizeof(caps
.szPname
));
3754 memcpy(caps
.szPname
, mmdevice
->in_caps
.szPname
, sizeof(caps
.szPname
));
3758 caps
.vDriverVersion
= 0x00010001;
3759 caps
.fdwSupport
= 0;
3760 caps
.cDestinations
= 1;
3762 memcpy(lpCaps
, &caps
, uSize
);
3764 return MMSYSERR_NOERROR
;
3767 /**************************************************************************
3768 * mixerOpen [WINMM.@]
3770 UINT WINAPI
mixerOpen(LPHMIXER lphMix
, UINT uDeviceID
, DWORD_PTR dwCallback
,
3771 DWORD_PTR dwInstance
, DWORD fdwOpen
)
3773 WINMM_MMDevice
*mmdevice
;
3777 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix
, uDeviceID
, dwCallback
,
3778 dwInstance
, fdwOpen
);
3780 hr
= WINMM_InitMMDevices();
3782 return MMSYSERR_NODRIVER
;
3785 return MMSYSERR_INVALPARAM
;
3787 mr
= WINMM_CheckCallback(dwCallback
, fdwOpen
, TRUE
);
3788 if(mr
!= MMSYSERR_NOERROR
)
3791 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3792 return MMSYSERR_BADDEVICEID
;
3794 if(uDeviceID
< g_outmmdevices_count
){
3795 mmdevice
= read_map(g_out_map
, uDeviceID
);
3796 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
, TRUE
,
3797 mmdevice
->mixer_count
);
3799 mmdevice
= read_map(g_in_map
, uDeviceID
- g_outmmdevices_count
);
3800 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
- g_outmmdevices_count
,
3801 FALSE
, mmdevice
->mixer_count
);
3804 ++mmdevice
->mixer_count
;
3806 return MMSYSERR_NOERROR
;
3809 /**************************************************************************
3810 * mixerClose [WINMM.@]
3812 UINT WINAPI
mixerClose(HMIXER hMix
)
3814 TRACE("(%p)\n", hMix
);
3816 return MMSYSERR_NOERROR
;
3819 /**************************************************************************
3820 * mixerGetID [WINMM.@]
3822 UINT WINAPI
mixerGetID(HMIXEROBJ hmix
, LPUINT lpid
, DWORD fdwID
)
3824 WINMM_MMDevice
*mmdevice
;
3827 TRACE("(%p, %p, %x)\n", hmix
, lpid
, fdwID
);
3829 hr
= WINMM_InitMMDevices();
3831 return MMSYSERR_NODRIVER
;
3834 return MMSYSERR_INVALPARAM
;
3836 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwID
, lpid
);
3838 return MMSYSERR_INVALHANDLE
;
3840 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3841 *lpid
+= g_outmmdevices_count
;
3843 return MMSYSERR_NOERROR
;
3846 /**************************************************************************
3847 * mixerGetControlDetailsW [WINMM.@]
3849 UINT WINAPI
mixerGetControlDetailsW(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdW
,
3852 WINMM_ControlDetails details
;
3854 TRACE("(%p, %p, %x)\n", hmix
, lpmcdW
, fdwDetails
);
3856 if(!WINMM_StartDevicesThread())
3857 return MMSYSERR_NODRIVER
;
3859 if(!lpmcdW
|| !lpmcdW
->paDetails
)
3860 return MMSYSERR_INVALPARAM
;
3862 TRACE("dwControlID: %u\n", lpmcdW
->dwControlID
);
3864 details
.hmix
= hmix
;
3865 details
.details
= lpmcdW
;
3866 details
.flags
= fdwDetails
;
3868 return SendMessageW(g_devices_hwnd
, MXDM_GETCONTROLDETAILS
,
3869 (DWORD_PTR
)&details
, 0);
3872 /**************************************************************************
3873 * mixerGetControlDetailsA [WINMM.@]
3875 UINT WINAPI
mixerGetControlDetailsA(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdA
,
3878 UINT ret
= MMSYSERR_NOTSUPPORTED
;
3880 TRACE("(%p, %p, %08x)\n", hmix
, lpmcdA
, fdwDetails
);
3882 if (lpmcdA
== NULL
|| lpmcdA
->cbStruct
!= sizeof(*lpmcdA
))
3883 return MMSYSERR_INVALPARAM
;
3885 switch (fdwDetails
& MIXER_GETCONTROLDETAILSF_QUERYMASK
) {
3886 case MIXER_GETCONTROLDETAILSF_VALUE
:
3887 /* can safely use A structure as it is, no string inside */
3888 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3890 case MIXER_GETCONTROLDETAILSF_LISTTEXT
:
3892 MIXERCONTROLDETAILS_LISTTEXTA
*pDetailsA
= lpmcdA
->paDetails
;
3893 MIXERCONTROLDETAILS_LISTTEXTW
*pDetailsW
;
3894 int size
= max(1, lpmcdA
->cChannels
) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3897 if (lpmcdA
->u
.cMultipleItems
!= 0) {
3898 size
*= lpmcdA
->u
.cMultipleItems
;
3900 pDetailsW
= HeapAlloc(GetProcessHeap(), 0, size
);
3901 lpmcdA
->paDetails
= pDetailsW
;
3902 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3903 /* set up lpmcd->paDetails */
3904 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3905 /* copy from lpmcd->paDetails back to paDetailsW; */
3906 if (ret
== MMSYSERR_NOERROR
) {
3907 for (i
= 0; i
< lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
; i
++) {
3908 pDetailsA
->dwParam1
= pDetailsW
->dwParam1
;
3909 pDetailsA
->dwParam2
= pDetailsW
->dwParam2
;
3910 WideCharToMultiByte( CP_ACP
, 0, pDetailsW
->szName
, -1,
3912 sizeof(pDetailsA
->szName
), NULL
, NULL
);
3916 pDetailsA
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3917 pDetailsW
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3919 HeapFree(GetProcessHeap(), 0, pDetailsW
);
3920 lpmcdA
->paDetails
= pDetailsA
;
3921 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTA
);
3925 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails
);
3931 /**************************************************************************
3932 * mixerGetLineControlsA [WINMM.@]
3934 UINT WINAPI
mixerGetLineControlsA(HMIXEROBJ hmix
, LPMIXERLINECONTROLSA lpmlcA
,
3937 MIXERLINECONTROLSW mlcW
;
3941 TRACE("(%p, %p, %x)\n", hmix
, lpmlcA
, fdwControls
);
3943 if (lpmlcA
== NULL
|| lpmlcA
->cbStruct
!= sizeof(*lpmlcA
) ||
3944 lpmlcA
->cbmxctrl
!= sizeof(MIXERCONTROLA
))
3945 return MMSYSERR_INVALPARAM
;
3947 mlcW
.cbStruct
= sizeof(mlcW
);
3948 mlcW
.dwLineID
= lpmlcA
->dwLineID
;
3949 mlcW
.u
.dwControlID
= lpmlcA
->u
.dwControlID
;
3950 mlcW
.u
.dwControlType
= lpmlcA
->u
.dwControlType
;
3952 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3953 the control count is assumed to be 1 - This is relied upon by a game,
3954 "Dynomite Deluze" */
3955 if (MIXER_GETLINECONTROLSF_ONEBYTYPE
== (fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
)) {
3958 mlcW
.cControls
= lpmlcA
->cControls
;
3960 mlcW
.cbmxctrl
= sizeof(MIXERCONTROLW
);
3961 mlcW
.pamxctrl
= HeapAlloc(GetProcessHeap(), 0,
3962 mlcW
.cControls
* mlcW
.cbmxctrl
);
3964 ret
= mixerGetLineControlsW(hmix
, &mlcW
, fdwControls
);
3966 if (ret
== MMSYSERR_NOERROR
) {
3967 lpmlcA
->dwLineID
= mlcW
.dwLineID
;
3968 lpmlcA
->u
.dwControlID
= mlcW
.u
.dwControlID
;
3969 lpmlcA
->u
.dwControlType
= mlcW
.u
.dwControlType
;
3971 for (i
= 0; i
< mlcW
.cControls
; i
++) {
3972 lpmlcA
->pamxctrl
[i
].cbStruct
= sizeof(MIXERCONTROLA
);
3973 lpmlcA
->pamxctrl
[i
].dwControlID
= mlcW
.pamxctrl
[i
].dwControlID
;
3974 lpmlcA
->pamxctrl
[i
].dwControlType
= mlcW
.pamxctrl
[i
].dwControlType
;
3975 lpmlcA
->pamxctrl
[i
].fdwControl
= mlcW
.pamxctrl
[i
].fdwControl
;
3976 lpmlcA
->pamxctrl
[i
].cMultipleItems
= mlcW
.pamxctrl
[i
].cMultipleItems
;
3977 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szShortName
, -1,
3978 lpmlcA
->pamxctrl
[i
].szShortName
,
3979 sizeof(lpmlcA
->pamxctrl
[i
].szShortName
), NULL
, NULL
);
3980 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szName
, -1,
3981 lpmlcA
->pamxctrl
[i
].szName
,
3982 sizeof(lpmlcA
->pamxctrl
[i
].szName
), NULL
, NULL
);
3983 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3984 * sizeof(mlcW.pamxctrl[i].Bounds) */
3985 memcpy(&lpmlcA
->pamxctrl
[i
].Bounds
, &mlcW
.pamxctrl
[i
].Bounds
,
3986 sizeof(mlcW
.pamxctrl
[i
].Bounds
));
3987 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3988 * sizeof(mlcW.pamxctrl[i].Metrics) */
3989 memcpy(&lpmlcA
->pamxctrl
[i
].Metrics
, &mlcW
.pamxctrl
[i
].Metrics
,
3990 sizeof(mlcW
.pamxctrl
[i
].Metrics
));
3994 HeapFree(GetProcessHeap(), 0, mlcW
.pamxctrl
);
3999 static UINT
WINMM_GetVolumeLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
4000 MIXERCONTROLW
*ctl
, DWORD flags
)
4002 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 0 : 2;
4003 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_VOLUME
;
4004 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
4005 ctl
->cMultipleItems
= 0;
4006 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, ctl
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4007 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, ctl
->szName
, MIXER_LONG_NAME_CHARS
);
4008 ctl
->Bounds
.s1
.dwMinimum
= 0;
4009 ctl
->Bounds
.s1
.dwMaximum
= 0xFFFF;
4010 ctl
->Metrics
.cSteps
= 192;
4012 return MMSYSERR_NOERROR
;
4015 static UINT
WINMM_GetMuteLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
4016 MIXERCONTROLW
*ctl
, DWORD flags
)
4018 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 1 : 3;
4019 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_MUTE
;
4020 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
4021 ctl
->cMultipleItems
= 0;
4022 LoadStringW(hWinMM32Instance
, IDS_MUTE
, ctl
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4023 LoadStringW(hWinMM32Instance
, IDS_MUTE
, ctl
->szName
, MIXER_LONG_NAME_CHARS
);
4024 ctl
->Bounds
.s1
.dwMinimum
= 0;
4025 ctl
->Bounds
.s1
.dwMaximum
= 1;
4026 ctl
->Metrics
.cSteps
= 0;
4028 return MMSYSERR_NOERROR
;
4031 /**************************************************************************
4032 * mixerGetLineControlsW [WINMM.@]
4034 UINT WINAPI
mixerGetLineControlsW(HMIXEROBJ hmix
, LPMIXERLINECONTROLSW lpmlcW
,
4037 WINMM_MMDevice
*mmdevice
;
4040 TRACE("(%p, %p, %08x)\n", hmix
, lpmlcW
, fdwControls
);
4042 hr
= WINMM_InitMMDevices();
4044 return MMSYSERR_NODRIVER
;
4046 if(fdwControls
& ~(MIXER_GETLINECONTROLSF_ALL
|
4047 MIXER_GETLINECONTROLSF_ONEBYID
|
4048 MIXER_GETLINECONTROLSF_ONEBYTYPE
|
4049 MIXER_OBJECTF_HMIXER
|
4050 MIXER_OBJECTF_MIXER
)){
4051 WARN("Unknown GetLineControls flag: %x\n", fdwControls
);
4052 return MMSYSERR_INVALFLAG
;
4055 if(!lpmlcW
|| lpmlcW
->cbStruct
< sizeof(*lpmlcW
) || !lpmlcW
->pamxctrl
)
4056 return MMSYSERR_INVALPARAM
;
4058 TRACE("dwLineID: %u\n", lpmlcW
->dwLineID
);
4059 TRACE("dwControl: %x\n", lpmlcW
->u
.dwControlID
);
4060 TRACE("cControls: %u\n", lpmlcW
->cControls
);
4062 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwControls
, NULL
);
4064 return MMSYSERR_INVALHANDLE
;
4066 switch(fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
){
4067 case MIXER_GETLINECONTROLSF_ALL
:
4068 if(lpmlcW
->cControls
!= 2)
4069 return MMSYSERR_INVALPARAM
;
4070 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4071 return MMSYSERR_INVALPARAM
;
4072 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4073 return MIXERR_INVALLINE
;
4074 WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4075 &lpmlcW
->pamxctrl
[0], fdwControls
);
4076 WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4077 &lpmlcW
->pamxctrl
[1], fdwControls
);
4078 return MMSYSERR_NOERROR
;
4079 case MIXER_GETLINECONTROLSF_ONEBYID
:
4080 if(lpmlcW
->cControls
!= 1)
4081 return MMSYSERR_INVALPARAM
;
4082 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4083 return MMSYSERR_INVALPARAM
;
4084 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4085 return MIXERR_INVALLINE
;
4086 if(lpmlcW
->u
.dwControlID
== 0)
4087 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4088 lpmlcW
->pamxctrl
, fdwControls
);
4089 if(lpmlcW
->u
.dwControlID
== 1)
4090 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4091 lpmlcW
->pamxctrl
, fdwControls
);
4092 return MMSYSERR_NOTSUPPORTED
;
4093 case MIXER_GETLINECONTROLSF_ONEBYTYPE
:
4094 if(lpmlcW
->cControls
!= 1)
4095 return MMSYSERR_INVALPARAM
;
4096 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
4097 return MMSYSERR_INVALPARAM
;
4098 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
4099 return MIXERR_INVALLINE
;
4100 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_VOLUME
)
4101 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
4102 lpmlcW
->pamxctrl
, fdwControls
);
4103 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_MUTE
)
4104 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
4105 lpmlcW
->pamxctrl
, fdwControls
);
4106 return MMSYSERR_NOTSUPPORTED
;
4109 return MMSYSERR_NOTSUPPORTED
;
4112 static UINT
WINMM_GetSourceLineInfo(WINMM_MMDevice
*mmdevice
, UINT mmdev_index
,
4113 MIXERLINEW
*info
, DWORD flags
)
4116 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4119 if(info
->dwSource
!= 0)
4120 return MIXERR_INVALLINE
;
4122 info
->dwDestination
= 0;
4124 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
| MIXERLINE_LINEF_SOURCE
;
4125 info
->cConnections
= 0;
4126 info
->cControls
= 2;
4127 /* volume & mute always affect all channels, so claim 1 channel */
4128 info
->cChannels
= 1;
4129 info
->Target
.dwDeviceID
= mmdev_index
;
4130 info
->Target
.wMid
= ~0;
4131 info
->Target
.wPid
= ~0;
4132 info
->Target
.vDriverVersion
= 0;
4134 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, info
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4135 LoadStringW(hWinMM32Instance
, IDS_MASTER_VOLUME
, info
->szName
, MIXER_LONG_NAME_CHARS
);
4138 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
;
4139 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEOUT
;
4140 memcpy(info
->Target
.szPname
, mmdevice
->out_caps
.szPname
,
4141 sizeof(info
->Target
.szPname
));
4143 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
;
4144 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
4145 info
->Target
.szPname
[0] = '\0';
4148 return MMSYSERR_NOERROR
;
4151 static UINT
WINMM_GetDestinationLineInfo(WINMM_MMDevice
*mmdevice
,
4152 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4155 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4158 if(info
->dwDestination
!= 0)
4159 return MIXERR_INVALLINE
;
4161 info
->dwSource
= 0xFFFFFFFF;
4162 info
->dwLineID
= 0xFFFF0000;
4163 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
;
4164 info
->cConnections
= 1;
4165 info
->cControls
= 2;
4167 LoadStringW(hWinMM32Instance
, IDS_VOLUME
, info
->szShortName
, MIXER_SHORT_NAME_CHARS
);
4168 LoadStringW(hWinMM32Instance
, IDS_MASTER_VOLUME
, info
->szName
, MIXER_LONG_NAME_CHARS
);
4170 info
->Target
.dwDeviceID
= mmdev_index
;
4171 info
->Target
.wMid
= ~0;
4172 info
->Target
.wPid
= ~0;
4173 info
->Target
.vDriverVersion
= 0;
4176 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
;
4177 info
->cChannels
= mmdevice
->out_caps
.wChannels
;
4178 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
4179 info
->Target
.szPname
[0] = '\0';
4181 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_WAVEIN
;
4182 info
->cChannels
= mmdevice
->in_caps
.wChannels
;
4183 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEIN
;
4184 memcpy(info
->Target
.szPname
, mmdevice
->in_caps
.szPname
,
4185 sizeof(info
->Target
.szPname
));
4188 return MMSYSERR_NOERROR
;
4191 static UINT
WINMM_GetComponentTypeLineInfo(WINMM_MMDevice
*mmdevice
,
4192 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4195 if(mmdevice
->in_caps
.szPname
[0] != '\0')
4198 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_WAVEIN
){
4200 return MIXERR_INVALLINE
;
4201 info
->dwDestination
= 0;
4202 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4205 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
){
4207 return MIXERR_INVALLINE
;
4208 info
->dwDestination
= 0;
4209 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4212 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
){
4214 return MIXERR_INVALLINE
;
4216 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4219 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
){
4221 return MIXERR_INVALLINE
;
4223 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4226 TRACE("Returning INVALLINE on this component type: %u\n",
4227 info
->dwComponentType
);
4229 return MIXERR_INVALLINE
;
4232 static UINT
WINMM_GetLineIDLineInfo(WINMM_MMDevice
*mmdevice
,
4233 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
4235 if(info
->dwLineID
== 0xFFFF0000){
4236 info
->dwDestination
= 0;
4237 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4240 if(info
->dwLineID
== 0){
4242 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
4245 TRACE("Returning INVALLINE on this dwLineID: %u\n", info
->dwLineID
);
4246 return MIXERR_INVALLINE
;
4249 /**************************************************************************
4250 * mixerGetLineInfoW [WINMM.@]
4252 UINT WINAPI
mixerGetLineInfoW(HMIXEROBJ hmix
, LPMIXERLINEW lpmliW
, DWORD fdwInfo
)
4255 WINMM_MMDevice
*mmdevice
;
4258 TRACE("(%p, %p, %x)\n", hmix
, lpmliW
, fdwInfo
);
4260 hr
= WINMM_InitMMDevices();
4262 return MMSYSERR_NODRIVER
;
4264 if(!lpmliW
|| lpmliW
->cbStruct
< sizeof(MIXERLINEW
))
4265 return MMSYSERR_INVALPARAM
;
4267 TRACE("dwDestination: %u\n", lpmliW
->dwDestination
);
4268 TRACE("dwSource: %u\n", lpmliW
->dwSource
);
4269 TRACE("dwLineID: %u\n", lpmliW
->dwLineID
);
4270 TRACE("fdwLine: 0x%x\n", lpmliW
->fdwLine
);
4271 TRACE("dwComponentType: 0x%x\n", lpmliW
->dwComponentType
);
4273 if(fdwInfo
& ~(MIXER_GETLINEINFOF_COMPONENTTYPE
|
4274 MIXER_GETLINEINFOF_DESTINATION
|
4275 MIXER_GETLINEINFOF_LINEID
|
4276 MIXER_GETLINEINFOF_SOURCE
|
4277 MIXER_GETLINEINFOF_TARGETTYPE
|
4278 MIXER_OBJECTF_HMIXER
|
4279 MIXER_OBJECTF_MIXER
)){
4280 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo
);
4281 return MMSYSERR_INVALFLAG
;
4284 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwInfo
, &mmdev_index
);
4286 return MMSYSERR_INVALHANDLE
;
4288 switch(fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
){
4289 case MIXER_GETLINEINFOF_DESTINATION
:
4290 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, lpmliW
,
4292 case MIXER_GETLINEINFOF_SOURCE
:
4293 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
4294 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
4295 return WINMM_GetComponentTypeLineInfo(mmdevice
, mmdev_index
, lpmliW
,
4297 case MIXER_GETLINEINFOF_LINEID
:
4298 return WINMM_GetLineIDLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
4299 case MIXER_GETLINEINFOF_TARGETTYPE
:
4300 FIXME("TARGETTYPE flag not implemented!\n");
4301 return MIXERR_INVALLINE
;
4304 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo
);
4305 return MMSYSERR_INVALFLAG
;
4308 /**************************************************************************
4309 * mixerGetLineInfoA [WINMM.@]
4311 UINT WINAPI
mixerGetLineInfoA(HMIXEROBJ hmix
, LPMIXERLINEA lpmliA
,
4317 TRACE("(%p, %p, %x)\n", hmix
, lpmliA
, fdwInfo
);
4319 if (lpmliA
== NULL
|| lpmliA
->cbStruct
!= sizeof(*lpmliA
))
4320 return MMSYSERR_INVALPARAM
;
4322 mliW
.cbStruct
= sizeof(mliW
);
4323 switch (fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
) {
4324 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
4325 mliW
.dwComponentType
= lpmliA
->dwComponentType
;
4327 case MIXER_GETLINEINFOF_DESTINATION
:
4328 mliW
.dwDestination
= lpmliA
->dwDestination
;
4330 case MIXER_GETLINEINFOF_LINEID
:
4331 mliW
.dwLineID
= lpmliA
->dwLineID
;
4333 case MIXER_GETLINEINFOF_SOURCE
:
4334 mliW
.dwDestination
= lpmliA
->dwDestination
;
4335 mliW
.dwSource
= lpmliA
->dwSource
;
4337 case MIXER_GETLINEINFOF_TARGETTYPE
:
4338 mliW
.Target
.dwType
= lpmliA
->Target
.dwType
;
4339 mliW
.Target
.wMid
= lpmliA
->Target
.wMid
;
4340 mliW
.Target
.wPid
= lpmliA
->Target
.wPid
;
4341 mliW
.Target
.vDriverVersion
= lpmliA
->Target
.vDriverVersion
;
4342 MultiByteToWideChar( CP_ACP
, 0, lpmliA
->Target
.szPname
, -1, mliW
.Target
.szPname
, sizeof(mliW
.Target
.szPname
)/sizeof(WCHAR
));
4345 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo
);
4346 return MMSYSERR_INVALFLAG
;
4349 ret
= mixerGetLineInfoW(hmix
, &mliW
, fdwInfo
);
4351 if(ret
== MMSYSERR_NOERROR
)
4353 lpmliA
->dwDestination
= mliW
.dwDestination
;
4354 lpmliA
->dwSource
= mliW
.dwSource
;
4355 lpmliA
->dwLineID
= mliW
.dwLineID
;
4356 lpmliA
->fdwLine
= mliW
.fdwLine
;
4357 lpmliA
->dwUser
= mliW
.dwUser
;
4358 lpmliA
->dwComponentType
= mliW
.dwComponentType
;
4359 lpmliA
->cChannels
= mliW
.cChannels
;
4360 lpmliA
->cConnections
= mliW
.cConnections
;
4361 lpmliA
->cControls
= mliW
.cControls
;
4362 WideCharToMultiByte( CP_ACP
, 0, mliW
.szShortName
, -1, lpmliA
->szShortName
,
4363 sizeof(lpmliA
->szShortName
), NULL
, NULL
);
4364 WideCharToMultiByte( CP_ACP
, 0, mliW
.szName
, -1, lpmliA
->szName
,
4365 sizeof(lpmliA
->szName
), NULL
, NULL
);
4366 lpmliA
->Target
.dwType
= mliW
.Target
.dwType
;
4367 lpmliA
->Target
.dwDeviceID
= mliW
.Target
.dwDeviceID
;
4368 lpmliA
->Target
.wMid
= mliW
.Target
.wMid
;
4369 lpmliA
->Target
.wPid
= mliW
.Target
.wPid
;
4370 lpmliA
->Target
.vDriverVersion
= mliW
.Target
.vDriverVersion
;
4371 WideCharToMultiByte( CP_ACP
, 0, mliW
.Target
.szPname
, -1, lpmliA
->Target
.szPname
,
4372 sizeof(lpmliA
->Target
.szPname
), NULL
, NULL
);
4377 /**************************************************************************
4378 * mixerSetControlDetails [WINMM.@]
4380 UINT WINAPI
mixerSetControlDetails(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcd
,
4383 WINMM_ControlDetails details
;
4386 TRACE("(%p, %p, %x)\n", hmix
, lpmcd
, fdwDetails
);
4388 if((fdwDetails
& MIXER_SETCONTROLDETAILSF_QUERYMASK
) ==
4389 MIXER_SETCONTROLDETAILSF_CUSTOM
)
4390 return MMSYSERR_NOTSUPPORTED
;
4393 return MMSYSERR_INVALPARAM
;
4395 if(!WINMM_StartDevicesThread())
4396 return MMSYSERR_NODRIVER
;
4398 TRACE("dwControlID: %u\n", lpmcd
->dwControlID
);
4400 details
.hmix
= hmix
;
4401 details
.details
= lpmcd
;
4402 details
.flags
= fdwDetails
;
4404 ret
= SendMessageW(g_devices_hwnd
, MXDM_SETCONTROLDETAILS
,
4405 (DWORD_PTR
)&details
, 0);
4406 InterlockedDecrement(&g_devthread_token
);
4410 /**************************************************************************
4411 * mixerMessage [WINMM.@]
4413 DWORD WINAPI
mixerMessage(HMIXER hmix
, UINT uMsg
, DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
4415 TRACE("(%p, %d, %lx, %lx)\n", hmix
, uMsg
, dwParam1
, dwParam2
);
4417 return MMSYSERR_NOTSUPPORTED
;