- Merge audio components from head
[reactos.git] / dll / win32 / wdmaud.drv / legacy.c
1 /*
2 * PROJECT: ReactOS Sound System
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/wdmaud.drv/wdmaud.c
5 *
6 * PURPOSE: WDM Audio Driver (User-mode part)
7 * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
8 Johannes Anderwald
9 *
10 * NOTES: Looking for wodMessage & co? You won't find them here. Try
11 * the MME Buddy library, which is where these routines are
12 * actually implemented.
13 *
14 */
15
16 #include "wdmaud.h"
17
18 #include <stdio.h>
19
20 #define KERNEL_DEVICE_NAME L"\\\\.\\wdmaud"
21
22 HANDLE KernelHandle = INVALID_HANDLE_VALUE;
23 DWORD OpenCount = 0;
24
25 DWORD
26 WINAPI
27 MixerEventThreadRoutine(
28 LPVOID Parameter)
29 {
30 HANDLE WaitObjects[2];
31 DWORD dwResult;
32 MMRESULT Result;
33 WDMAUD_DEVICE_INFO DeviceInfo;
34 PSOUND_DEVICE_INSTANCE Instance = (PSOUND_DEVICE_INSTANCE)Parameter;
35
36 /* setup wait objects */
37 WaitObjects[0] = Instance->hNotifyEvent;
38 WaitObjects[1] = Instance->hStopEvent;
39
40 /* zero device info */
41 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
42
43 DeviceInfo.hDevice = Instance->Handle;
44 DeviceInfo.DeviceType = MIXER_DEVICE_TYPE;
45
46 do
47 {
48 dwResult = WaitForMultipleObjects(2, WaitObjects, FALSE, INFINITE);
49
50 if (dwResult == WAIT_OBJECT_0 + 1)
51 {
52 /* stop event was signalled */
53 break;
54 }
55
56 do
57 {
58 Result = SyncOverlappedDeviceIoControl(KernelHandle,
59 IOCTL_GET_MIXER_EVENT,
60 (LPVOID) &DeviceInfo,
61 sizeof(WDMAUD_DEVICE_INFO),
62 (LPVOID) &DeviceInfo,
63 sizeof(WDMAUD_DEVICE_INFO),
64 NULL);
65
66 if (Result == MMSYSERR_NOERROR)
67 {
68 DriverCallback(Instance->WinMM.ClientCallback,
69 HIWORD(Instance->WinMM.Flags),
70 Instance->WinMM.Handle,
71 DeviceInfo.u.MixerEvent.NotificationType,
72 Instance->WinMM.ClientCallbackInstanceData,
73 (DWORD_PTR)DeviceInfo.u.MixerEvent.Value,
74 0);
75 }
76 }while(Result == MMSYSERR_NOERROR);
77 }while(TRUE);
78
79 /* done */
80 return 0;
81 }
82
83 MMRESULT
84 WdmAudCleanupByLegacy()
85 {
86 if (KernelHandle != INVALID_HANDLE_VALUE)
87 {
88 CloseHandle(KernelHandle);
89 KernelHandle = INVALID_HANDLE_VALUE;
90 }
91
92 return MMSYSERR_NOERROR;
93 }
94
95 MMRESULT
96 WdmAudGetNumWdmDevsByLegacy(
97 IN MMDEVICE_TYPE DeviceType,
98 OUT DWORD* DeviceCount)
99 {
100 MMRESULT Result;
101 WDMAUD_DEVICE_INFO DeviceInfo;
102
103 VALIDATE_MMSYS_PARAMETER( KernelHandle != INVALID_HANDLE_VALUE );
104 VALIDATE_MMSYS_PARAMETER( IS_VALID_SOUND_DEVICE_TYPE(DeviceType) );
105 VALIDATE_MMSYS_PARAMETER( DeviceCount );
106
107 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
108 DeviceInfo.DeviceType = DeviceType;
109
110 Result = SyncOverlappedDeviceIoControl(KernelHandle,
111 IOCTL_GETNUMDEVS_TYPE,
112 (LPVOID) &DeviceInfo,
113 sizeof(WDMAUD_DEVICE_INFO),
114 (LPVOID) &DeviceInfo,
115 sizeof(WDMAUD_DEVICE_INFO),
116 NULL);
117
118 if ( ! MMSUCCESS( Result ) )
119 {
120 SND_ERR(L"Call to IOCTL_GETNUMDEVS_TYPE failed\n");
121 *DeviceCount = 0;
122 return TranslateInternalMmResult(Result);
123 }
124
125 *DeviceCount = DeviceInfo.DeviceCount;
126
127 return MMSYSERR_NOERROR;
128 }
129
130 MMRESULT
131 WdmAudGetCapabilitiesByLegacy(
132 IN PSOUND_DEVICE SoundDevice,
133 IN DWORD DeviceId,
134 OUT PVOID Capabilities,
135 IN DWORD CapabilitiesSize)
136 {
137 MMRESULT Result;
138 MMDEVICE_TYPE DeviceType;
139 WDMAUD_DEVICE_INFO DeviceInfo;
140
141 SND_ASSERT( SoundDevice );
142 SND_ASSERT( Capabilities );
143
144 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
145 SND_ASSERT( Result == MMSYSERR_NOERROR );
146
147 if ( ! MMSUCCESS(Result) )
148 return Result;
149
150 SND_TRACE(L"WDMAUD - GetWdmDeviceCapabilities DeviceType %u DeviceId %u\n", DeviceType, DeviceId);
151
152 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
153 DeviceInfo.DeviceType = DeviceType;
154 DeviceInfo.DeviceIndex = DeviceId;
155
156 Result = SyncOverlappedDeviceIoControl(KernelHandle,
157 IOCTL_GETCAPABILITIES,
158 (LPVOID) &DeviceInfo,
159 sizeof(WDMAUD_DEVICE_INFO),
160 (LPVOID) &DeviceInfo,
161 sizeof(WDMAUD_DEVICE_INFO),
162 NULL);
163
164 if ( ! MMSUCCESS(Result) )
165 {
166 return TranslateInternalMmResult(Result);
167 }
168
169 /* This is pretty much a big hack right now */
170 switch ( DeviceType )
171 {
172 case MIXER_DEVICE_TYPE:
173 {
174 LPMIXERCAPSW MixerCaps = (LPMIXERCAPSW) Capabilities;
175
176 DeviceInfo.u.MixCaps.szPname[MAXPNAMELEN-1] = L'\0';
177 CopyWideString(MixerCaps->szPname, DeviceInfo.u.MixCaps.szPname);
178
179 MixerCaps->cDestinations = DeviceInfo.u.MixCaps.cDestinations;
180 MixerCaps->fdwSupport = DeviceInfo.u.MixCaps.fdwSupport;
181 MixerCaps->vDriverVersion = DeviceInfo.u.MixCaps.vDriverVersion;
182 MixerCaps->wMid = DeviceInfo.u.MixCaps.wMid;
183 MixerCaps->wPid = DeviceInfo.u.MixCaps.wPid;
184 break;
185 }
186 case WAVE_OUT_DEVICE_TYPE :
187 {
188 LPWAVEOUTCAPSW WaveOutCaps = (LPWAVEOUTCAPSW) Capabilities;
189
190 DeviceInfo.u.WaveOutCaps.szPname[MAXPNAMELEN-1] = L'\0';
191 WaveOutCaps->wMid = DeviceInfo.u.WaveOutCaps.wMid;
192 WaveOutCaps->wPid = DeviceInfo.u.WaveOutCaps.wPid;
193
194 WaveOutCaps->vDriverVersion = DeviceInfo.u.WaveOutCaps.vDriverVersion;
195 CopyWideString(WaveOutCaps->szPname, DeviceInfo.u.WaveOutCaps.szPname);
196
197 WaveOutCaps->dwFormats = DeviceInfo.u.WaveOutCaps.dwFormats;
198 WaveOutCaps->wChannels = DeviceInfo.u.WaveOutCaps.wChannels;
199 WaveOutCaps->dwSupport = DeviceInfo.u.WaveOutCaps.dwSupport;
200 break;
201 }
202 case WAVE_IN_DEVICE_TYPE :
203 {
204 LPWAVEINCAPSW WaveInCaps = (LPWAVEINCAPSW) Capabilities;
205
206 DeviceInfo.u.WaveInCaps.szPname[MAXPNAMELEN-1] = L'\0';
207
208 WaveInCaps->wMid = DeviceInfo.u.WaveInCaps.wMid;
209 WaveInCaps->wPid = DeviceInfo.u.WaveInCaps.wPid;
210
211 WaveInCaps->vDriverVersion = DeviceInfo.u.WaveInCaps.vDriverVersion;
212 CopyWideString(WaveInCaps->szPname, DeviceInfo.u.WaveInCaps.szPname);
213
214 WaveInCaps->dwFormats = DeviceInfo.u.WaveInCaps.dwFormats;
215 WaveInCaps->wChannels = DeviceInfo.u.WaveInCaps.wChannels;
216 WaveInCaps->wReserved1 = 0;
217 break;
218 }
219 case MIDI_IN_DEVICE_TYPE :
220 {
221 LPMIDIINCAPSW MidiInCaps = (LPMIDIINCAPSW)Capabilities;
222
223 DeviceInfo.u.MidiInCaps.szPname[MAXPNAMELEN-1] = L'\0';
224
225 MidiInCaps->vDriverVersion = DeviceInfo.u.MidiInCaps.vDriverVersion;
226 MidiInCaps->wMid = DeviceInfo.u.MidiInCaps.wMid;
227 MidiInCaps->wPid = DeviceInfo.u.MidiInCaps.wPid;
228 MidiInCaps->dwSupport = DeviceInfo.u.MidiInCaps.dwSupport;
229
230 CopyWideString(MidiInCaps->szPname, DeviceInfo.u.MidiInCaps.szPname);
231 break;
232 }
233 case MIDI_OUT_DEVICE_TYPE :
234 {
235 LPMIDIOUTCAPSW MidiOutCaps = (LPMIDIOUTCAPSW)Capabilities;
236
237 DeviceInfo.u.MidiOutCaps.szPname[MAXPNAMELEN-1] = L'\0';
238
239 MidiOutCaps->vDriverVersion = DeviceInfo.u.MidiOutCaps.vDriverVersion;
240 MidiOutCaps->wMid = DeviceInfo.u.MidiOutCaps.wMid;
241 MidiOutCaps->wPid = DeviceInfo.u.MidiOutCaps.wPid;
242 MidiOutCaps->dwSupport = DeviceInfo.u.MidiOutCaps.dwSupport;
243
244 CopyWideString(MidiOutCaps->szPname, DeviceInfo.u.MidiOutCaps.szPname);
245 break;
246 }
247 }
248
249 return MMSYSERR_NOERROR;
250 }
251
252 MMRESULT
253 WdmAudOpenSoundDeviceByLegacy()
254 {
255 /* Only open this if it's not already open */
256 if ( KernelHandle == INVALID_HANDLE_VALUE )
257 {
258 SND_TRACE(L"Opening wdmaud device\n");
259 KernelHandle = CreateFileW(KERNEL_DEVICE_NAME,
260 GENERIC_READ | GENERIC_WRITE,
261 0,
262 NULL,
263 OPEN_EXISTING,
264 FILE_FLAG_OVERLAPPED,
265 NULL);
266 }
267
268 if ( KernelHandle == INVALID_HANDLE_VALUE )
269 return MMSYSERR_ERROR;
270
271 ++ OpenCount;
272
273 return MMSYSERR_NOERROR;
274 }
275
276 MMRESULT
277 WdmAudCloseSoundDeviceByLegacy(
278 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
279 IN PVOID Handle)
280 {
281 WDMAUD_DEVICE_INFO DeviceInfo;
282 MMRESULT Result;
283 MMDEVICE_TYPE DeviceType;
284 PSOUND_DEVICE SoundDevice;
285
286 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
287
288 if ( ! MMSUCCESS(Result) )
289 {
290 return TranslateInternalMmResult(Result);
291 }
292
293 if ( OpenCount == 0 )
294 {
295 return MMSYSERR_NOERROR;
296 }
297
298 SND_ASSERT( KernelHandle != INVALID_HANDLE_VALUE );
299
300 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
301 SND_ASSERT( Result == MMSYSERR_NOERROR );
302
303 if (SoundDeviceInstance->Handle != (PVOID)KernelHandle)
304 {
305 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
306
307 DeviceInfo.DeviceType = DeviceType;
308 DeviceInfo.hDevice = SoundDeviceInstance->Handle;
309
310 /* First stop the stream */
311 if (DeviceType != MIXER_DEVICE_TYPE)
312 {
313 DeviceInfo.u.State = KSSTATE_PAUSE;
314 SyncOverlappedDeviceIoControl(KernelHandle,
315 IOCTL_SETDEVICE_STATE,
316 (LPVOID) &DeviceInfo,
317 sizeof(WDMAUD_DEVICE_INFO),
318 (LPVOID) &DeviceInfo,
319 sizeof(WDMAUD_DEVICE_INFO),
320 NULL);
321
322 DeviceInfo.u.State = KSSTATE_ACQUIRE;
323 SyncOverlappedDeviceIoControl(KernelHandle,
324 IOCTL_SETDEVICE_STATE,
325 (LPVOID) &DeviceInfo,
326 sizeof(WDMAUD_DEVICE_INFO),
327 (LPVOID) &DeviceInfo,
328 sizeof(WDMAUD_DEVICE_INFO),
329 NULL);
330
331
332 DeviceInfo.u.State = KSSTATE_STOP;
333 SyncOverlappedDeviceIoControl(KernelHandle,
334 IOCTL_SETDEVICE_STATE,
335 (LPVOID) &DeviceInfo,
336 sizeof(WDMAUD_DEVICE_INFO),
337 (LPVOID) &DeviceInfo,
338 sizeof(WDMAUD_DEVICE_INFO),
339 NULL);
340 }
341
342 SyncOverlappedDeviceIoControl(KernelHandle,
343 IOCTL_CLOSE_WDMAUD,
344 (LPVOID) &DeviceInfo,
345 sizeof(WDMAUD_DEVICE_INFO),
346 (LPVOID) &DeviceInfo,
347 sizeof(WDMAUD_DEVICE_INFO),
348 NULL);
349 }
350
351 if (DeviceType == MIXER_DEVICE_TYPE)
352 {
353 SetEvent(SoundDeviceInstance->hStopEvent);
354 CloseHandle(SoundDeviceInstance->hStopEvent);
355 CloseHandle(SoundDeviceInstance->hNotifyEvent);
356 }
357
358 --OpenCount;
359
360 if ( OpenCount < 1 )
361 {
362 CloseHandle(KernelHandle);
363 KernelHandle = INVALID_HANDLE_VALUE;
364 }
365
366 return MMSYSERR_NOERROR;
367 }
368
369 MMRESULT
370 WdmAudSetMixerDeviceFormatByLegacy(
371 IN PSOUND_DEVICE_INSTANCE Instance,
372 IN DWORD DeviceId,
373 IN PWAVEFORMATEX WaveFormat,
374 IN DWORD WaveFormatSize)
375 {
376 MMRESULT Result;
377 WDMAUD_DEVICE_INFO DeviceInfo;
378 HANDLE hThread;
379
380 Instance->hNotifyEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
381 if ( ! Instance->hNotifyEvent )
382 return MMSYSERR_NOMEM;
383
384 if (Instance->Handle != NULL)
385 {
386 /* device is already open */
387 return MMSYSERR_NOERROR;
388 }
389
390 Instance->hStopEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
391 if ( ! Instance->hStopEvent )
392 return MMSYSERR_NOMEM;
393
394 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
395 DeviceInfo.DeviceType = MIXER_DEVICE_TYPE;
396 DeviceInfo.DeviceIndex = DeviceId;
397 DeviceInfo.u.hNotifyEvent = Instance->hNotifyEvent;
398
399 Result = SyncOverlappedDeviceIoControl(KernelHandle,
400 IOCTL_OPEN_WDMAUD,
401 (LPVOID) &DeviceInfo,
402 sizeof(WDMAUD_DEVICE_INFO),
403 (LPVOID) &DeviceInfo,
404 sizeof(WDMAUD_DEVICE_INFO),
405 NULL);
406
407 if ( ! MMSUCCESS(Result) )
408 {
409 CloseHandle(Instance->hNotifyEvent);
410 CloseHandle(Instance->hStopEvent);
411 return TranslateInternalMmResult(Result);
412 }
413
414 hThread = CreateThread(NULL, 0, MixerEventThreadRoutine, (LPVOID)Instance, 0, NULL);
415 if ( hThread )
416 {
417 CloseHandle(hThread);
418 }
419
420 /* Store sound device handle instance handle */
421 Instance->Handle = (PVOID)DeviceInfo.hDevice;
422
423 return MMSYSERR_NOERROR;
424 }
425
426 MMRESULT
427 WdmAudSetWaveDeviceFormatByLegacy(
428 IN PSOUND_DEVICE_INSTANCE Instance,
429 IN DWORD DeviceId,
430 IN PWAVEFORMATEX WaveFormat,
431 IN DWORD WaveFormatSize)
432 {
433 MMRESULT Result;
434 PSOUND_DEVICE SoundDevice;
435 PVOID Identifier;
436 WDMAUD_DEVICE_INFO DeviceInfo;
437 MMDEVICE_TYPE DeviceType;
438
439 Result = GetSoundDeviceFromInstance(Instance, &SoundDevice);
440
441 if ( ! MMSUCCESS(Result) )
442 {
443 return TranslateInternalMmResult(Result);
444 }
445
446 Result = GetSoundDeviceIdentifier(SoundDevice, &Identifier);
447
448 if ( ! MMSUCCESS(Result) )
449 {
450 return TranslateInternalMmResult(Result);
451 }
452
453 if (Instance->Handle != NULL)
454 {
455 /* device is already open */
456 return MMSYSERR_NOERROR;
457 }
458
459 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
460
461 SND_ASSERT( Result == MMSYSERR_NOERROR );
462
463 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
464 DeviceInfo.DeviceType = DeviceType;
465 DeviceInfo.DeviceIndex = DeviceId;
466 DeviceInfo.u.WaveFormatEx.cbSize = sizeof(WAVEFORMATEX); //WaveFormat->cbSize;
467 DeviceInfo.u.WaveFormatEx.wFormatTag = WaveFormat->wFormatTag;
468 #ifdef USERMODE_MIXER
469 DeviceInfo.u.WaveFormatEx.nChannels = 2;
470 DeviceInfo.u.WaveFormatEx.nSamplesPerSec = 44100;
471 DeviceInfo.u.WaveFormatEx.nBlockAlign = 4;
472 DeviceInfo.u.WaveFormatEx.nAvgBytesPerSec = 176400;
473 DeviceInfo.u.WaveFormatEx.wBitsPerSample = 16;
474 #else
475 DeviceInfo.u.WaveFormatEx.nChannels = WaveFormat->nChannels;
476 DeviceInfo.u.WaveFormatEx.nSamplesPerSec = WaveFormat->nSamplesPerSec;
477 DeviceInfo.u.WaveFormatEx.nBlockAlign = WaveFormat->nBlockAlign;
478 DeviceInfo.u.WaveFormatEx.nAvgBytesPerSec = WaveFormat->nAvgBytesPerSec;
479 DeviceInfo.u.WaveFormatEx.wBitsPerSample = WaveFormat->wBitsPerSample;
480 #endif
481
482 Result = SyncOverlappedDeviceIoControl(KernelHandle,
483 IOCTL_OPEN_WDMAUD,
484 (LPVOID) &DeviceInfo,
485 sizeof(WDMAUD_DEVICE_INFO),
486 (LPVOID) &DeviceInfo,
487 sizeof(WDMAUD_DEVICE_INFO),
488 NULL);
489
490 if ( ! MMSUCCESS(Result) )
491 {
492 return TranslateInternalMmResult(Result);
493 }
494
495 /* Store format */
496 Instance->WaveFormatEx.cbSize = WaveFormat->cbSize;
497 Instance->WaveFormatEx.wFormatTag = WaveFormat->wFormatTag;
498 Instance->WaveFormatEx.nChannels = WaveFormat->nChannels;
499 Instance->WaveFormatEx.nSamplesPerSec = WaveFormat->nSamplesPerSec;
500 Instance->WaveFormatEx.nBlockAlign = WaveFormat->nBlockAlign;
501 Instance->WaveFormatEx.nAvgBytesPerSec = WaveFormat->nAvgBytesPerSec;
502 Instance->WaveFormatEx.wBitsPerSample = WaveFormat->wBitsPerSample;
503
504 /* Store sound device handle instance handle */
505 Instance->Handle = (PVOID)DeviceInfo.hDevice;
506
507 /* Now determine framing requirements */
508 Result = SyncOverlappedDeviceIoControl(KernelHandle,
509 IOCTL_GETFRAMESIZE,
510 (LPVOID) &DeviceInfo,
511 sizeof(WDMAUD_DEVICE_INFO),
512 (LPVOID) &DeviceInfo,
513 sizeof(WDMAUD_DEVICE_INFO),
514 NULL);
515
516 if ( MMSUCCESS(Result) )
517 {
518 if (DeviceInfo.u.FrameSize)
519 {
520 Instance->FrameSize = DeviceInfo.u.FrameSize * 2;
521 Instance->BufferCount = WaveFormat->nAvgBytesPerSec / Instance->FrameSize;
522 SND_TRACE(L"FrameSize %u BufferCount %u\n", Instance->FrameSize, Instance->BufferCount);
523 }
524 }
525 else
526 {
527 // use a default of 100 buffers
528 Instance->BufferCount = 100;
529 }
530
531 if (DeviceType == WAVE_OUT_DEVICE_TYPE)
532 {
533 /* Now start the stream */
534 DeviceInfo.u.State = KSSTATE_RUN;
535 SyncOverlappedDeviceIoControl(KernelHandle,
536 IOCTL_SETDEVICE_STATE,
537 (LPVOID) &DeviceInfo,
538 sizeof(WDMAUD_DEVICE_INFO),
539 (LPVOID) &DeviceInfo,
540 sizeof(WDMAUD_DEVICE_INFO),
541 NULL);
542 }
543
544 return MMSYSERR_NOERROR;
545 }
546
547 MMRESULT
548 WdmAudCommitWaveBufferByLegacy(
549 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
550 IN PVOID OffsetPtr,
551 IN DWORD Length,
552 IN PSOUND_OVERLAPPED Overlap,
553 IN LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine)
554 {
555 HANDLE Handle;
556 MMRESULT Result;
557 WDMAUD_DEVICE_INFO DeviceInfo;
558 PSOUND_DEVICE SoundDevice;
559 MMDEVICE_TYPE DeviceType;
560 BOOL Ret;
561
562 VALIDATE_MMSYS_PARAMETER( SoundDeviceInstance );
563 VALIDATE_MMSYS_PARAMETER( OffsetPtr );
564 VALIDATE_MMSYS_PARAMETER( Overlap );
565 VALIDATE_MMSYS_PARAMETER( CompletionRoutine );
566
567 GetSoundDeviceInstanceHandle(SoundDeviceInstance, &Handle);
568 SND_ASSERT(Handle);
569
570 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
571
572 if ( ! MMSUCCESS(Result) )
573 {
574 return TranslateInternalMmResult(Result);
575 }
576
577 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
578 SND_ASSERT( Result == MMSYSERR_NOERROR );
579
580 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
581
582 DeviceInfo.Header.FrameExtent = Length;
583 if (DeviceType == WAVE_OUT_DEVICE_TYPE)
584 {
585 DeviceInfo.Header.DataUsed = Length;
586 }
587 DeviceInfo.Header.Data = OffsetPtr;
588 DeviceInfo.Header.Size = sizeof(WDMAUD_DEVICE_INFO);
589 DeviceInfo.Header.PresentationTime.Numerator = 1;
590 DeviceInfo.Header.PresentationTime.Denominator = 1;
591 DeviceInfo.hDevice = Handle;
592 DeviceInfo.DeviceType = DeviceType;
593
594
595
596 Overlap->Standard.hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
597
598 if (DeviceType == WAVE_OUT_DEVICE_TYPE)
599 {
600 Ret = WriteFileEx(KernelHandle, &DeviceInfo, sizeof(WDMAUD_DEVICE_INFO), (LPOVERLAPPED)Overlap, CompletionRoutine);
601 if (Ret)
602 WaitForSingleObjectEx (KernelHandle, INFINITE, TRUE);
603 }
604 else if (DeviceType == WAVE_IN_DEVICE_TYPE)
605 {
606 Ret = ReadFileEx(KernelHandle, &DeviceInfo, sizeof(WDMAUD_DEVICE_INFO), (LPOVERLAPPED)Overlap, CompletionRoutine);
607 //if (Ret)
608 // WaitForSingleObjectEx (KernelHandle, INFINITE, TRUE);
609 }
610
611 return MMSYSERR_NOERROR;
612 }
613
614
615
616 MMRESULT
617 WdmAudSetWaveStateByLegacy(
618 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
619 IN BOOL bStart)
620 {
621 MMRESULT Result;
622 PSOUND_DEVICE SoundDevice;
623 WDMAUD_DEVICE_INFO DeviceInfo;
624 MMDEVICE_TYPE DeviceType;
625 HANDLE Handle;
626
627 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
628
629 if ( ! MMSUCCESS(Result) )
630 {
631 return TranslateInternalMmResult(Result);
632 }
633
634 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
635 SND_ASSERT( Result == MMSYSERR_NOERROR );
636
637 Result = GetSoundDeviceInstanceHandle(SoundDeviceInstance, &Handle);
638 SND_ASSERT( Result == MMSYSERR_NOERROR );
639
640 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
641 DeviceInfo.hDevice = Handle;
642 DeviceInfo.DeviceType = DeviceType;
643
644 if (bStart)
645 DeviceInfo.u.State = KSSTATE_RUN;
646 else
647 DeviceInfo.u.State = KSSTATE_PAUSE;
648 Result = SyncOverlappedDeviceIoControl(KernelHandle,
649 IOCTL_SETDEVICE_STATE,
650 (LPVOID) &DeviceInfo,
651 sizeof(WDMAUD_DEVICE_INFO),
652 (LPVOID) &DeviceInfo,
653 sizeof(WDMAUD_DEVICE_INFO),
654 NULL);
655
656 return Result;
657 }
658
659 MMRESULT
660 WdmAudGetDeviceInterfaceStringByLegacy(
661 IN MMDEVICE_TYPE DeviceType,
662 IN DWORD DeviceId,
663 IN LPWSTR Interface,
664 IN DWORD InterfaceLength,
665 OUT DWORD * InterfaceSize)
666 {
667 WDMAUD_DEVICE_INFO DeviceInfo;
668 MMRESULT Result;
669
670 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
671 DeviceInfo.DeviceType = DeviceType;
672 DeviceInfo.DeviceIndex = DeviceId;
673
674
675 Result = SyncOverlappedDeviceIoControl(KernelHandle,
676 IOCTL_QUERYDEVICEINTERFACESTRING,
677 (LPVOID) &DeviceInfo,
678 sizeof(WDMAUD_DEVICE_INFO),
679 (LPVOID) &DeviceInfo,
680 sizeof(WDMAUD_DEVICE_INFO),
681 NULL);
682
683
684 if ( ! MMSUCCESS(Result) )
685 {
686 return TranslateInternalMmResult(Result);
687 }
688
689
690 if (!Interface)
691 {
692 SND_ASSERT(InterfaceSize);
693
694 *InterfaceSize = DeviceInfo.u.Interface.DeviceInterfaceStringSize;
695 return MMSYSERR_NOERROR;
696 }
697
698 if (InterfaceLength < DeviceInfo.u.Interface.DeviceInterfaceStringSize)
699 {
700 /* buffer is too small */
701 return MMSYSERR_MOREDATA;
702 }
703
704 DeviceInfo.u.Interface.DeviceInterfaceStringSize = InterfaceLength;
705 DeviceInfo.u.Interface.DeviceInterfaceString = Interface;
706
707 Result = SyncOverlappedDeviceIoControl(KernelHandle,
708 IOCTL_QUERYDEVICEINTERFACESTRING,
709 (LPVOID) &DeviceInfo,
710 sizeof(WDMAUD_DEVICE_INFO),
711 (LPVOID) &DeviceInfo,
712 sizeof(WDMAUD_DEVICE_INFO),
713 NULL);
714
715 if ( MMSUCCESS(Result) && InterfaceLength > 2)
716 {
717 Interface[1] = L'\\';
718 Interface[InterfaceLength-1] = L'\0';
719 }
720
721 return Result;
722 }
723
724 MMRESULT
725 WdmAudGetWavePositionByLegacy(
726 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
727 IN MMTIME* Time)
728 {
729 MMRESULT Result;
730 PSOUND_DEVICE SoundDevice;
731 WDMAUD_DEVICE_INFO DeviceInfo;
732 MMDEVICE_TYPE DeviceType;
733 HANDLE Handle;
734
735 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
736
737 if ( ! MMSUCCESS(Result) )
738 {
739 return TranslateInternalMmResult(Result);
740 }
741
742 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
743 SND_ASSERT( Result == MMSYSERR_NOERROR );
744
745 Result = GetSoundDeviceInstanceHandle(SoundDeviceInstance, &Handle);
746 SND_ASSERT( Result == MMSYSERR_NOERROR );
747
748 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
749 DeviceInfo.hDevice = Handle;
750 DeviceInfo.DeviceType = DeviceType;
751
752 Result = SyncOverlappedDeviceIoControl(KernelHandle,
753 IOCTL_OPEN_WDMAUD,
754 (LPVOID) &DeviceInfo,
755 sizeof(WDMAUD_DEVICE_INFO),
756 (LPVOID) &DeviceInfo,
757 sizeof(WDMAUD_DEVICE_INFO),
758 NULL);
759
760 if ( ! MMSUCCESS(Result) )
761 {
762 return TranslateInternalMmResult(Result);
763 }
764
765 Time->wType = TIME_BYTES;
766 Time->u.cb = (DWORD)DeviceInfo.u.Position;
767
768 return MMSYSERR_NOERROR;
769 }
770
771
772 MMRESULT
773 WdmAudResetStreamByLegacy(
774 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
775 IN MMDEVICE_TYPE DeviceType,
776 IN BOOLEAN bStartReset)
777 {
778 MMRESULT Result;
779 HANDLE Handle;
780 WDMAUD_DEVICE_INFO DeviceInfo;
781
782 Result = GetSoundDeviceInstanceHandle(SoundDeviceInstance, &Handle);
783 SND_ASSERT( Result == MMSYSERR_NOERROR );
784
785 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
786 DeviceInfo.hDevice = Handle;
787 DeviceInfo.DeviceType = DeviceType;
788 DeviceInfo.u.ResetStream = (bStartReset ? KSRESET_BEGIN : KSRESET_END);
789
790 Result = SyncOverlappedDeviceIoControl(KernelHandle,
791 IOCTL_RESET_STREAM,
792 (LPVOID) &DeviceInfo,
793 sizeof(WDMAUD_DEVICE_INFO),
794 (LPVOID) &DeviceInfo,
795 sizeof(WDMAUD_DEVICE_INFO),
796 NULL);
797 return Result;
798 }
799
800 MMRESULT
801 WdmAudQueryMixerInfoByLegacy(
802 IN struct _SOUND_DEVICE_INSTANCE* SoundDeviceInstance,
803 IN UINT uMsg,
804 IN LPVOID Parameter,
805 IN DWORD Flags)
806 {
807 MMRESULT Result;
808 WDMAUD_DEVICE_INFO DeviceInfo;
809 HANDLE Handle;
810 DWORD IoControlCode;
811 LPMIXERLINEW MixLine;
812 LPMIXERLINECONTROLSW MixControls;
813 LPMIXERCONTROLDETAILS MixDetails;
814
815 SND_TRACE(L"uMsg %x Flags %x\n", uMsg, Flags);
816
817 Result = GetSoundDeviceInstanceHandle(SoundDeviceInstance, &Handle);
818 SND_ASSERT( Result == MMSYSERR_NOERROR );
819
820 ZeroMemory(&DeviceInfo, sizeof(WDMAUD_DEVICE_INFO));
821 DeviceInfo.hDevice = Handle;
822 DeviceInfo.DeviceType = MIXER_DEVICE_TYPE;
823 DeviceInfo.Flags = Flags;
824
825 MixLine = (LPMIXERLINEW)Parameter;
826 MixControls = (LPMIXERLINECONTROLSW)Parameter;
827 MixDetails = (LPMIXERCONTROLDETAILS)Parameter;
828
829 switch(uMsg)
830 {
831 case MXDM_GETLINEINFO:
832 RtlCopyMemory(&DeviceInfo.u.MixLine, MixLine, sizeof(MIXERLINEW));
833 IoControlCode = IOCTL_GETLINEINFO;
834 break;
835 case MXDM_GETLINECONTROLS:
836 RtlCopyMemory(&DeviceInfo.u.MixControls, MixControls, sizeof(MIXERLINECONTROLSW));
837 IoControlCode = IOCTL_GETLINECONTROLS;
838 break;
839 case MXDM_SETCONTROLDETAILS:
840 RtlCopyMemory(&DeviceInfo.u.MixDetails, MixDetails, sizeof(MIXERCONTROLDETAILS));
841 IoControlCode = IOCTL_SETCONTROLDETAILS;
842 break;
843 case MXDM_GETCONTROLDETAILS:
844 RtlCopyMemory(&DeviceInfo.u.MixDetails, MixDetails, sizeof(MIXERCONTROLDETAILS));
845 IoControlCode = IOCTL_GETCONTROLDETAILS;
846 break;
847 default:
848 SND_ASSERT(0);
849 return MMSYSERR_NOTSUPPORTED;
850 }
851
852 Result = SyncOverlappedDeviceIoControl(KernelHandle,
853 IoControlCode,
854 (LPVOID) &DeviceInfo,
855 sizeof(WDMAUD_DEVICE_INFO),
856 (LPVOID) &DeviceInfo,
857 sizeof(WDMAUD_DEVICE_INFO),
858 NULL);
859
860 if ( ! MMSUCCESS(Result) )
861 {
862 return Result;
863 }
864
865 switch(uMsg)
866 {
867 case MXDM_GETLINEINFO:
868 {
869 RtlCopyMemory(MixLine, &DeviceInfo.u.MixLine, sizeof(MIXERLINEW));
870 break;
871 }
872 }
873
874 return Result;
875 }