[AUDIO-BRINGUP]
[reactos.git] / dll / win32 / wdmaud.drv / mmixer.c
1 /*
2 * PROJECT: ReactOS Sound System
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/wdmaud.drv/mmixer.c
5 *
6 * PURPOSE: WDM Audio Mixer API (User-mode part)
7 * PROGRAMMERS: Johannes Anderwald
8 */
9
10 #include "wdmaud.h"
11
12 typedef struct
13 {
14 KSSTREAM_HEADER Header;
15 HANDLE hDevice;
16 PSOUND_OVERLAPPED Overlap;
17 LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine;
18 }IO_PACKET, *LPIO_PACKET;
19
20 BOOL MMixerLibraryInitialized = FALSE;
21
22
23
24 PVOID Alloc(ULONG NumBytes);
25 MIXER_STATUS Close(HANDLE hDevice);
26 VOID Free(PVOID Block);
27 VOID Copy(PVOID Src, PVOID Dst, ULONG NumBytes);
28 MIXER_STATUS Open(IN LPWSTR DevicePath, OUT PHANDLE hDevice);
29 MIXER_STATUS Control(IN HANDLE hMixer, IN ULONG dwIoControlCode, IN PVOID lpInBuffer, IN ULONG nInBufferSize, OUT PVOID lpOutBuffer, ULONG nOutBufferSize, PULONG lpBytesReturned);
30 MIXER_STATUS Enum(IN PVOID EnumContext, IN ULONG DeviceIndex, OUT LPWSTR * DeviceName, OUT PHANDLE OutHandle, OUT PHANDLE OutKey);
31 MIXER_STATUS OpenKey(IN HANDLE hKey, IN LPWSTR SubKey, IN ULONG DesiredAccess, OUT PHANDLE OutKey);
32 MIXER_STATUS CloseKey(IN HANDLE hKey);
33 MIXER_STATUS QueryKeyValue(IN HANDLE hKey, IN LPWSTR KeyName, OUT PVOID * ResultBuffer, OUT PULONG ResultLength, OUT PULONG KeyType);
34 PVOID AllocEventData(IN ULONG ExtraSize);
35 VOID FreeEventData(IN PVOID EventData);
36
37 MIXER_CONTEXT MixerContext =
38 {
39 sizeof(MIXER_CONTEXT),
40 NULL,
41 Alloc,
42 Control,
43 Free,
44 Open,
45 Close,
46 Copy,
47 OpenKey,
48 QueryKeyValue,
49 CloseKey,
50 AllocEventData,
51 FreeEventData
52 };
53
54 GUID CategoryGuid = {STATIC_KSCATEGORY_AUDIO};
55
56 MIXER_STATUS
57 QueryKeyValue(
58 IN HANDLE hKey,
59 IN LPWSTR KeyName,
60 OUT PVOID * ResultBuffer,
61 OUT PULONG ResultLength,
62 OUT PULONG KeyType)
63 {
64 if (RegQueryValueExW((HKEY)hKey, KeyName, NULL, KeyType, NULL, ResultLength) == ERROR_FILE_NOT_FOUND)
65 return MM_STATUS_UNSUCCESSFUL;
66
67 *ResultBuffer = HeapAlloc(GetProcessHeap(), 0, *ResultLength);
68 if (*ResultBuffer == NULL)
69 return MM_STATUS_NO_MEMORY;
70
71 if (RegQueryValueExW((HKEY)hKey, KeyName, NULL, KeyType, *ResultBuffer, ResultLength) != ERROR_SUCCESS)
72 {
73 HeapFree(GetProcessHeap(), 0, *ResultBuffer);
74 return MM_STATUS_UNSUCCESSFUL;
75 }
76 return MM_STATUS_SUCCESS;
77 }
78
79 MIXER_STATUS
80 OpenKey(
81 IN HANDLE hKey,
82 IN LPWSTR SubKey,
83 IN ULONG DesiredAccess,
84 OUT PHANDLE OutKey)
85 {
86 if (RegOpenKeyExW((HKEY)hKey, SubKey, 0, DesiredAccess, (PHKEY)OutKey) == ERROR_SUCCESS)
87 return MM_STATUS_SUCCESS;
88
89 return MM_STATUS_UNSUCCESSFUL;
90 }
91
92 MIXER_STATUS
93 CloseKey(
94 IN HANDLE hKey)
95 {
96 RegCloseKey((HKEY)hKey);
97 return MM_STATUS_SUCCESS;
98 }
99
100
101 PVOID Alloc(ULONG NumBytes)
102 {
103 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, NumBytes);
104 }
105
106 MIXER_STATUS
107 Close(HANDLE hDevice)
108 {
109 if (CloseHandle(hDevice))
110 return MM_STATUS_SUCCESS;
111 else
112 return MM_STATUS_UNSUCCESSFUL;
113 }
114
115 VOID
116 Free(PVOID Block)
117 {
118 HeapFree(GetProcessHeap(), 0, Block);
119 }
120
121 VOID
122 Copy(PVOID Src, PVOID Dst, ULONG NumBytes)
123 {
124 CopyMemory(Src, Dst, NumBytes);
125 }
126
127 MIXER_STATUS
128 Open(
129 IN LPWSTR DevicePath,
130 OUT PHANDLE hDevice)
131 {
132 DevicePath[1] = L'\\';
133 *hDevice = CreateFileW(DevicePath,
134 GENERIC_READ | GENERIC_WRITE,
135 0,
136 NULL,
137 OPEN_EXISTING,
138 FILE_FLAG_OVERLAPPED,
139 NULL);
140 if (*hDevice == INVALID_HANDLE_VALUE)
141 {
142 return MM_STATUS_UNSUCCESSFUL;
143 }
144
145 return MM_STATUS_SUCCESS;
146 }
147
148 MIXER_STATUS
149 Control(
150 IN HANDLE hMixer,
151 IN ULONG dwIoControlCode,
152 IN PVOID lpInBuffer,
153 IN ULONG nInBufferSize,
154 OUT PVOID lpOutBuffer,
155 ULONG nOutBufferSize,
156 PULONG lpBytesReturned)
157 {
158 OVERLAPPED Overlapped;
159 BOOLEAN IoResult;
160 DWORD Transferred = 0;
161
162 /* Overlapped I/O is done here - this is used for waiting for completion */
163 ZeroMemory(&Overlapped, sizeof(OVERLAPPED));
164 Overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
165
166 if ( ! Overlapped.hEvent )
167 return MM_STATUS_NO_MEMORY;
168
169 /* Talk to the device */
170 IoResult = DeviceIoControl(hMixer,
171 dwIoControlCode,
172 lpInBuffer,
173 nInBufferSize,
174 lpOutBuffer,
175 nOutBufferSize,
176 &Transferred,
177 &Overlapped);
178
179 /* If failure occurs, make sure it's not just due to the overlapped I/O */
180 if ( ! IoResult )
181 {
182 if ( GetLastError() != ERROR_IO_PENDING )
183 {
184 CloseHandle(Overlapped.hEvent);
185
186 if (GetLastError() == ERROR_MORE_DATA || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
187 {
188 if ( lpBytesReturned )
189 *lpBytesReturned = Transferred;
190 return MM_STATUS_MORE_ENTRIES;
191 }
192
193 return MM_STATUS_UNSUCCESSFUL;
194 }
195 }
196
197 /* Wait for the I/O to complete */
198 IoResult = GetOverlappedResult(hMixer,
199 &Overlapped,
200 &Transferred,
201 TRUE);
202
203 /* Don't need this any more */
204 CloseHandle(Overlapped.hEvent);
205
206 if ( ! IoResult )
207 return MM_STATUS_UNSUCCESSFUL;
208
209 if ( lpBytesReturned )
210 *lpBytesReturned = Transferred;
211
212 return MM_STATUS_SUCCESS;
213 }
214
215 MIXER_STATUS
216 Enum(
217 IN PVOID EnumContext,
218 IN ULONG DeviceIndex,
219 OUT LPWSTR * DeviceName,
220 OUT PHANDLE OutHandle,
221 OUT PHANDLE OutKey)
222 {
223 SP_DEVICE_INTERFACE_DATA InterfaceData;
224 SP_DEVINFO_DATA DeviceData;
225 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DetailData;
226 BOOL Result;
227 DWORD Length;
228 MIXER_STATUS Status;
229
230 //printf("Enum EnumContext %p DeviceIndex %lu OutHandle %p\n", EnumContext, DeviceIndex, OutHandle);
231
232 InterfaceData.cbSize = sizeof(InterfaceData);
233 InterfaceData.Reserved = 0;
234
235 Result = SetupDiEnumDeviceInterfaces(EnumContext,
236 NULL,
237 &CategoryGuid,
238 DeviceIndex,
239 &InterfaceData);
240
241 if (!Result)
242 {
243 if (GetLastError() == ERROR_NO_MORE_ITEMS)
244 {
245 return MM_STATUS_NO_MORE_DEVICES;
246 }
247 return MM_STATUS_UNSUCCESSFUL;
248 }
249
250 Length = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W) + MAX_PATH * sizeof(WCHAR);
251 DetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)HeapAlloc(GetProcessHeap(),
252 0,
253 Length);
254 DetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
255 DeviceData.cbSize = sizeof(DeviceData);
256 DeviceData.Reserved = 0;
257
258 Result = SetupDiGetDeviceInterfaceDetailW(EnumContext,
259 &InterfaceData,
260 DetailData,
261 Length,
262 NULL,
263 &DeviceData);
264
265 if (!Result)
266 {
267 DPRINT("SetupDiGetDeviceInterfaceDetailW failed with %lu\n", GetLastError());
268 return MM_STATUS_UNSUCCESSFUL;
269 }
270
271
272 *OutKey = SetupDiOpenDeviceInterfaceRegKey(EnumContext, &InterfaceData, 0, KEY_READ);
273 if ((HKEY)*OutKey == INVALID_HANDLE_VALUE)
274 {
275 HeapFree(GetProcessHeap(), 0, DetailData);
276 return MM_STATUS_UNSUCCESSFUL;
277 }
278
279 Status = Open(DetailData->DevicePath, OutHandle);
280
281 if (Status != MM_STATUS_SUCCESS)
282 {
283 RegCloseKey((HKEY)*OutKey);
284 HeapFree(GetProcessHeap(), 0, DetailData);
285 return Status;
286 }
287
288 *DeviceName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(DetailData->DevicePath)+1) * sizeof(WCHAR));
289 if (*DeviceName == NULL)
290 {
291 CloseHandle(*OutHandle);
292 RegCloseKey((HKEY)*OutKey);
293 HeapFree(GetProcessHeap(), 0, DetailData);
294 return MM_STATUS_NO_MEMORY;
295 }
296 DPRINT1("DeviceName %S\n", DetailData->DevicePath);
297 wcscpy(*DeviceName, DetailData->DevicePath);
298 HeapFree(GetProcessHeap(), 0, DetailData);
299
300 return Status;
301 }
302
303 PVOID
304 AllocEventData(
305 IN ULONG ExtraSize)
306 {
307 PKSEVENTDATA Data = (PKSEVENTDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(KSEVENTDATA) + ExtraSize);
308 if (!Data)
309 return NULL;
310
311 Data->EventHandle.Event = CreateEventW(NULL, FALSE, FALSE, NULL);
312 if (!Data->EventHandle.Event)
313 {
314 HeapFree(GetProcessHeap(), 0, Data);
315 return NULL;
316 }
317
318 Data->NotificationType = KSEVENTF_EVENT_HANDLE;
319 return Data;
320 }
321
322 VOID
323 FreeEventData(IN PVOID EventData)
324 {
325 PKSEVENTDATA Data = (PKSEVENTDATA)EventData;
326
327 CloseHandle(Data->EventHandle.Event);
328 HeapFree(GetProcessHeap(), 0, Data);
329 }
330
331
332 BOOL
333 WdmAudInitUserModeMixer()
334 {
335 HDEVINFO DeviceHandle;
336 MIXER_STATUS Status;
337
338 if (MMixerLibraryInitialized)
339 {
340 /* library is already initialized */
341 return TRUE;
342 }
343
344
345 /* create a device list */
346 DeviceHandle = SetupDiGetClassDevs(&CategoryGuid,
347 NULL,
348 NULL,
349 DIGCF_DEVICEINTERFACE/* FIXME |DIGCF_PRESENT*/);
350
351 if (DeviceHandle == INVALID_HANDLE_VALUE)
352 {
353 /* failed to create a device list */
354 return FALSE;
355 }
356
357
358 /* initialize the mixer library */
359 Status = MMixerInitialize(&MixerContext, Enum, (PVOID)DeviceHandle);
360
361 /* free device list */
362 SetupDiDestroyDeviceInfoList(DeviceHandle);
363
364 if (Status != MM_STATUS_SUCCESS)
365 {
366 /* failed to initialize mixer library */
367 DPRINT1("Failed to initialize mixer library with %x\n", Status);
368 return FALSE;
369 }
370
371 /* library is now initialized */
372 MMixerLibraryInitialized = TRUE;
373
374 /* completed successfully */
375 return TRUE;
376 }
377
378 MMRESULT
379 WdmAudCleanupByMMixer()
380 {
381 /* TODO */
382 return MMSYSERR_NOERROR;
383 }
384
385 MMRESULT
386 WdmAudGetMixerCapabilties(
387 IN ULONG DeviceId,
388 LPMIXERCAPSW Capabilities)
389 {
390 if (MMixerGetCapabilities(&MixerContext, DeviceId, Capabilities) == MM_STATUS_SUCCESS)
391 return MMSYSERR_NOERROR;
392
393 return MMSYSERR_BADDEVICEID;
394 }
395
396 MMRESULT
397 WdmAudGetLineInfo(
398 IN HANDLE hMixer,
399 IN DWORD MixerId,
400 IN LPMIXERLINEW MixLine,
401 IN ULONG Flags)
402 {
403 if (MMixerGetLineInfo(&MixerContext, hMixer, MixerId, Flags, MixLine) == MM_STATUS_SUCCESS)
404 return MMSYSERR_NOERROR;
405
406 return MMSYSERR_ERROR;
407 }
408
409 MMRESULT
410 WdmAudGetLineControls(
411 IN HANDLE hMixer,
412 IN DWORD MixerId,
413 IN LPMIXERLINECONTROLSW MixControls,
414 IN ULONG Flags)
415 {
416 if (MMixerGetLineControls(&MixerContext, hMixer, MixerId, Flags, MixControls) == MM_STATUS_SUCCESS)
417 return MMSYSERR_NOERROR;
418
419 return MMSYSERR_ERROR;
420 }
421
422 MMRESULT
423 WdmAudSetControlDetails(
424 IN HANDLE hMixer,
425 IN DWORD MixerId,
426 IN LPMIXERCONTROLDETAILS MixDetails,
427 IN ULONG Flags)
428 {
429 if (MMixerSetControlDetails(&MixerContext, hMixer, MixerId, Flags, MixDetails) == MM_STATUS_SUCCESS)
430 return MMSYSERR_NOERROR;
431
432 return MMSYSERR_ERROR;
433
434 }
435
436 MMRESULT
437 WdmAudGetControlDetails(
438 IN HANDLE hMixer,
439 IN DWORD MixerId,
440 IN LPMIXERCONTROLDETAILS MixDetails,
441 IN ULONG Flags)
442 {
443 if (MMixerGetControlDetails(&MixerContext, hMixer, MixerId, Flags, MixDetails) == MM_STATUS_SUCCESS)
444 return MMSYSERR_NOERROR;
445
446 return MMSYSERR_ERROR;
447 }
448
449 MMRESULT
450 WdmAudGetWaveOutCapabilities(
451 IN ULONG DeviceId,
452 LPWAVEOUTCAPSW Capabilities)
453 {
454 if (MMixerWaveOutCapabilities(&MixerContext, DeviceId, Capabilities) == MM_STATUS_SUCCESS)
455 return MMSYSERR_NOERROR;
456
457 return MMSYSERR_ERROR;
458
459 }
460
461 MMRESULT
462 WdmAudGetWaveInCapabilities(
463 IN ULONG DeviceId,
464 LPWAVEINCAPSW Capabilities)
465 {
466 if (MMixerWaveInCapabilities(&MixerContext, DeviceId, Capabilities) == MM_STATUS_SUCCESS)
467 return MMSYSERR_NOERROR;
468
469 return MMSYSERR_ERROR;
470 }
471
472 MMRESULT
473 WdmAudSetWaveDeviceFormatByMMixer(
474 IN PSOUND_DEVICE_INSTANCE Instance,
475 IN DWORD DeviceId,
476 IN PWAVEFORMATEX WaveFormat,
477 IN DWORD WaveFormatSize)
478 {
479 MMDEVICE_TYPE DeviceType;
480 PSOUND_DEVICE SoundDevice;
481 MMRESULT Result;
482 BOOL bWaveIn;
483
484 Result = GetSoundDeviceFromInstance(Instance, &SoundDevice);
485
486 if ( ! MMSUCCESS(Result) )
487 {
488 return TranslateInternalMmResult(Result);
489 }
490
491 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
492 SND_ASSERT( Result == MMSYSERR_NOERROR );
493
494 bWaveIn = (DeviceType == WAVE_IN_DEVICE_TYPE ? TRUE : FALSE);
495
496 if (MMixerOpenWave(&MixerContext, DeviceId, bWaveIn, WaveFormat, NULL, NULL, &Instance->Handle) == MM_STATUS_SUCCESS)
497 {
498 if (DeviceType == WAVE_OUT_DEVICE_TYPE)
499 {
500 MMixerSetWaveStatus(&MixerContext, Instance->Handle, KSSTATE_ACQUIRE);
501 MMixerSetWaveStatus(&MixerContext, Instance->Handle, KSSTATE_PAUSE);
502 MMixerSetWaveStatus(&MixerContext, Instance->Handle, KSSTATE_RUN);
503 }
504 return MMSYSERR_NOERROR;
505 }
506 return MMSYSERR_ERROR;
507 }
508
509
510 MMRESULT
511 WdmAudGetCapabilitiesByMMixer(
512 IN PSOUND_DEVICE SoundDevice,
513 IN DWORD DeviceId,
514 OUT PVOID Capabilities,
515 IN DWORD CapabilitiesSize)
516 {
517 MMDEVICE_TYPE DeviceType;
518 MMRESULT Result;
519
520 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
521 SND_ASSERT( Result == MMSYSERR_NOERROR );
522
523 if (DeviceType == MIXER_DEVICE_TYPE)
524 {
525 return WdmAudGetMixerCapabilties(DeviceId, (LPMIXERCAPSW)Capabilities);
526 }
527 else if (DeviceType == WAVE_OUT_DEVICE_TYPE)
528 {
529 return WdmAudGetWaveOutCapabilities(DeviceId, (LPWAVEOUTCAPSW)Capabilities);
530 }
531 else if (DeviceType == WAVE_IN_DEVICE_TYPE)
532 {
533 return WdmAudGetWaveInCapabilities(DeviceId, (LPWAVEINCAPSW)Capabilities);
534 }
535 else
536 {
537 // not supported
538 return MMSYSERR_ERROR;
539 }
540 }
541
542 MMRESULT
543 WdmAudOpenSoundDeviceByMMixer(
544 IN struct _SOUND_DEVICE* SoundDevice,
545 OUT PVOID* Handle)
546 {
547 if (WdmAudInitUserModeMixer())
548 return MMSYSERR_NOERROR;
549 else
550 return MMSYSERR_ERROR;
551 }
552
553 MMRESULT
554 WdmAudCloseSoundDeviceByMMixer(
555 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
556 IN PVOID Handle)
557 {
558 MMDEVICE_TYPE DeviceType;
559 PSOUND_DEVICE SoundDevice;
560 MMRESULT Result;
561
562 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
563
564 if ( ! MMSUCCESS(Result) )
565 {
566 return TranslateInternalMmResult(Result);
567 }
568
569 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
570 SND_ASSERT( Result == MMSYSERR_NOERROR );
571
572 if (DeviceType == MIXER_DEVICE_TYPE)
573 {
574 /* no op */
575 return MMSYSERR_NOERROR;
576 }
577 else if (DeviceType == WAVE_IN_DEVICE_TYPE || DeviceType == WAVE_OUT_DEVICE_TYPE)
578 {
579 /* make sure the pin is stopped */
580 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_PAUSE);
581 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_ACQUIRE);
582 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_STOP);
583
584 CloseHandle(Handle);
585 return MMSYSERR_NOERROR;
586 }
587
588 /* midi is not supported */
589 return MMSYSERR_ERROR;
590 }
591
592 MMRESULT
593 WdmAudGetNumWdmDevsByMMixer(
594 IN MMDEVICE_TYPE DeviceType,
595 OUT DWORD* DeviceCount)
596 {
597 switch(DeviceType)
598 {
599 case MIXER_DEVICE_TYPE:
600 *DeviceCount = MMixerGetCount(&MixerContext);
601 break;
602 case WAVE_OUT_DEVICE_TYPE:
603 *DeviceCount = MMixerGetWaveOutCount(&MixerContext);
604 break;
605 case WAVE_IN_DEVICE_TYPE:
606 *DeviceCount = MMixerGetWaveInCount(&MixerContext);
607 break;
608 default:
609 *DeviceCount = 0;
610 }
611 return MMSYSERR_NOERROR;
612 }
613
614 MMRESULT
615 WdmAudQueryMixerInfoByMMixer(
616 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
617 IN DWORD MixerId,
618 IN UINT uMsg,
619 IN LPVOID Parameter,
620 IN DWORD Flags)
621 {
622 LPMIXERLINEW MixLine;
623 LPMIXERLINECONTROLSW MixControls;
624 LPMIXERCONTROLDETAILS MixDetails;
625 HANDLE hMixer = NULL;
626
627 MixLine = (LPMIXERLINEW)Parameter;
628 MixControls = (LPMIXERLINECONTROLSW)Parameter;
629 MixDetails = (LPMIXERCONTROLDETAILS)Parameter;
630
631 /* FIXME param checks */
632
633 if (SoundDeviceInstance)
634 {
635 hMixer = SoundDeviceInstance->Handle;
636 }
637
638 switch(uMsg)
639 {
640 case MXDM_GETLINEINFO:
641 return WdmAudGetLineInfo(hMixer, MixerId, MixLine, Flags);
642 case MXDM_GETLINECONTROLS:
643 return WdmAudGetLineControls(hMixer, MixerId, MixControls, Flags);
644 case MXDM_SETCONTROLDETAILS:
645 return WdmAudSetControlDetails(hMixer, MixerId, MixDetails, Flags);
646 case MXDM_GETCONTROLDETAILS:
647 return WdmAudGetControlDetails(hMixer, MixerId, MixDetails, Flags);
648 default:
649 DPRINT1("MixerId %lu, uMsg %lu, Parameter %p, Flags %lu\n", MixerId, uMsg, Parameter, Flags);
650 SND_ASSERT(0);
651 return MMSYSERR_NOTSUPPORTED;
652 }
653 }
654
655 MMRESULT
656 WdmAudGetDeviceInterfaceStringByMMixer(
657 IN MMDEVICE_TYPE DeviceType,
658 IN DWORD DeviceId,
659 IN LPWSTR Interface,
660 IN DWORD InterfaceLength,
661 OUT DWORD * InterfaceSize)
662 {
663 /* FIXME */
664 return MMSYSERR_NOTSUPPORTED;
665 }
666
667 MMRESULT
668 WdmAudSetMixerDeviceFormatByMMixer(
669 IN PSOUND_DEVICE_INSTANCE Instance,
670 IN DWORD DeviceId,
671 IN PWAVEFORMATEX WaveFormat,
672 IN DWORD WaveFormatSize)
673 {
674 Instance->hNotifyEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
675 if ( ! Instance->hNotifyEvent )
676 return MMSYSERR_NOMEM;
677
678 if (MMixerOpen(&MixerContext, DeviceId, Instance->hNotifyEvent, NULL /* FIXME */, &Instance->Handle) == MM_STATUS_SUCCESS)
679 return MMSYSERR_NOERROR;
680
681 return MMSYSERR_BADDEVICEID;
682 }
683
684 MMRESULT
685 WdmAudSetWaveStateByMMixer(
686 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
687 IN BOOL bStart)
688 {
689 MMDEVICE_TYPE DeviceType;
690 PSOUND_DEVICE SoundDevice;
691 MMRESULT Result;
692
693 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
694 SND_ASSERT( Result == MMSYSERR_NOERROR );
695
696
697 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
698 SND_ASSERT( Result == MMSYSERR_NOERROR );
699
700 if (DeviceType == WAVE_IN_DEVICE_TYPE || DeviceType == WAVE_OUT_DEVICE_TYPE)
701 {
702 if (bStart)
703 {
704 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_ACQUIRE);
705 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_PAUSE);
706 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_RUN);
707 }
708 else
709 {
710 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_PAUSE);
711 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_ACQUIRE);
712 MMixerSetWaveStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_STOP);
713 }
714 }
715 else if (DeviceType == MIDI_IN_DEVICE_TYPE || DeviceType == MIDI_OUT_DEVICE_TYPE)
716 {
717 if (bStart)
718 {
719 MMixerSetMidiStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_ACQUIRE);
720 MMixerSetMidiStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_PAUSE);
721 MMixerSetMidiStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_RUN);
722 }
723 else
724 {
725 MMixerSetMidiStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_PAUSE);
726 MMixerSetMidiStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_ACQUIRE);
727 MMixerSetMidiStatus(&MixerContext, SoundDeviceInstance->Handle, KSSTATE_STOP);
728 }
729 }
730
731 return MMSYSERR_NOERROR;
732 }
733
734 MMRESULT
735 WdmAudResetStreamByMMixer(
736 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
737 IN MMDEVICE_TYPE DeviceType,
738 IN BOOLEAN bStartReset)
739 {
740 /* FIXME */
741 return MMSYSERR_NOTSUPPORTED;
742 }
743
744 MMRESULT
745 WdmAudGetWavePositionByMMixer(
746 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
747 IN MMTIME* Time)
748 {
749 /* FIXME */
750 return MMSYSERR_NOTSUPPORTED;
751 }
752
753 DWORD
754 WINAPI
755 IoStreamingThread(
756 LPVOID lpParameter)
757 {
758 DWORD Length;
759 MMRESULT Result;
760 LPIO_PACKET Packet = (LPIO_PACKET)lpParameter;
761
762 Result = SyncOverlappedDeviceIoControl(Packet->hDevice,
763 IOCTL_KS_WRITE_STREAM, //FIXME IOCTL_KS_READ_STREAM
764 NULL,
765 0,
766 &Packet->Header,
767 sizeof(KSSTREAM_HEADER),
768 &Length);
769
770 /* HACK:
771 * don't call completion routine directly
772 */
773
774 Packet->CompletionRoutine(ERROR_SUCCESS, Packet->Header.DataUsed, (LPOVERLAPPED)Packet->Overlap);
775
776 HeapFree(GetProcessHeap(), 0, Packet);
777 return 0;
778 }
779
780
781
782
783
784
785
786 MMRESULT
787 WdmAudCommitWaveBufferByMMixer(
788 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
789 IN PVOID OffsetPtr,
790 IN DWORD Length,
791 IN PSOUND_OVERLAPPED Overlap,
792 IN LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine)
793 {
794 PSOUND_DEVICE SoundDevice;
795 MMDEVICE_TYPE DeviceType;
796 MMRESULT Result;
797 LPIO_PACKET Packet;
798 HANDLE hThread;
799
800 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
801
802 if ( ! MMSUCCESS(Result) )
803 {
804 return TranslateInternalMmResult(Result);
805 }
806
807 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
808 SND_ASSERT( Result == MMSYSERR_NOERROR );
809
810 Packet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IO_PACKET));
811 if ( ! Packet )
812 {
813 /* no memory */
814 return MMSYSERR_NOMEM;
815 }
816
817 /* setup stream packet */
818 Packet->Header.Size = sizeof(KSSTREAM_HEADER);
819 Packet->Header.PresentationTime.Numerator = 1;
820 Packet->Header.PresentationTime.Denominator = 1;
821 Packet->Header.Data = OffsetPtr;
822 Packet->Header.FrameExtent = Length;
823 Packet->hDevice = SoundDeviceInstance->Handle;
824 Packet->Overlap = Overlap;
825 Packet->CompletionRoutine = CompletionRoutine;
826
827 if (DeviceType == WAVE_OUT_DEVICE_TYPE)
828 {
829 Packet->Header.DataUsed = Length;
830 }
831
832 hThread = CreateThread(NULL, 0, IoStreamingThread, (LPVOID)Packet, 0, NULL);
833 if (hThread == NULL)
834 {
835 /* error */
836 return MMSYSERR_ERROR;
837 }
838
839 CloseHandle(hThread);
840
841 return MMSYSERR_NOERROR;
842 }