Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/src/hostapi/wdmks/pa_win_wdmks.c

raw symbols SCIP
1 /*
2 * $Id: pa_win_wdmks.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
3 * PortAudio Windows WDM-KS interface
4 *
5 * Author: Andrew Baldwin
6 * Based on the Open Source API proposed by Ross Bencina
7 * Copyright (c) 1999-2004 Andrew Baldwin, Ross Bencina, Phil Burk
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files
11 * (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * Any person wishing to distribute modifications to the Software is
21 * requested to send the modifications to the original developer so that
22 * they can be incorporated into the canonical version.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 /** @file
34 @brief Portaudio WDM-KS host API.
35
36 @note This is the implementation of the Portaudio host API using the
37 Windows WDM/Kernel Streaming API in order to enable very low latency
38 playback and recording on all modern Windows platforms (e.g. 2K, XP)
39 Note: This API accesses the device drivers below the usual KMIXER
40 component which is normally used to enable multi-client mixing and
41 format conversion. That means that it will lock out all other users
42 of a device for the duration of active stream using those devices
43 */
44
45 #include <stdio.h>
46
47 /* Debugging/tracing support */
48
49 #define PA_LOGE_
50 #define PA_LOGL_
51
52 #ifdef __GNUC__
53 #include <initguid.h>
54 #define _WIN32_WINNT 0x0501
55 #define WINVER 0x0501
56 #endif
57
58 #include <string.h> /* strlen() */
59 #include <assert.h>
60
61 #include "pa_util.h"
62 #include "pa_allocation.h"
63 #include "pa_hostapi.h"
64 #include "pa_stream.h"
65 #include "pa_cpuload.h"
66 #include "pa_process.h"
67 #include "portaudio.h"
68
69 #include <windows.h>
70 #include <winioctl.h>
71
72
73 #ifdef __GNUC__
74 #undef PA_LOGE_
75 #define PA_LOGE_ PA_DEBUG(("%s {\n",__FUNCTION__))
76 #undef PA_LOGL_
77 #define PA_LOGL_ PA_DEBUG(("} %s\n",__FUNCTION__))
78 /* These defines are set in order to allow the WIndows DirectX
79 * headers to compile with a GCC compiler such as MinGW
80 * NOTE: The headers may generate a few warning in GCC, but
81 * they should compile */
82 #define _INC_MMSYSTEM
83 #define _INC_MMREG
84 #define _NTRTL_ /* Turn off default definition of DEFINE_GUIDEX */
85 #define DEFINE_GUID_THUNK(name,guid) DEFINE_GUID(name,guid)
86 #define DEFINE_GUIDEX(n) DEFINE_GUID_THUNK( n, STATIC_##n )
87 #if !defined( DEFINE_WAVEFORMATEX_GUID )
88 #define DEFINE_WAVEFORMATEX_GUID(x) (USHORT)(x), 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
89 #endif
90 #define WAVE_FORMAT_ADPCM 0x0002
91 #define WAVE_FORMAT_IEEE_FLOAT 0x0003
92 #define WAVE_FORMAT_ALAW 0x0006
93 #define WAVE_FORMAT_MULAW 0x0007
94 #define WAVE_FORMAT_MPEG 0x0050
95 #define WAVE_FORMAT_DRM 0x0009
96 #define DYNAMIC_GUID_THUNK(l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
97 #define DYNAMIC_GUID(data) DYNAMIC_GUID_THUNK(data)
98 #endif
99
100 #ifdef _MSC_VER
101 #define DYNAMIC_GUID(data) {data}
102 #define _INC_MMREG
103 #define _NTRTL_ /* Turn off default definition of DEFINE_GUIDEX */
104 #undef DEFINE_GUID
105 #define DEFINE_GUID(n,data) EXTERN_C const GUID n = {data}
106 #define DEFINE_GUID_THUNK(n,data) DEFINE_GUID(n,data)
107 #define DEFINE_GUIDEX(n) DEFINE_GUID_THUNK(n, STATIC_##n)
108 #if !defined( DEFINE_WAVEFORMATEX_GUID )
109 #define DEFINE_WAVEFORMATEX_GUID(x) (USHORT)(x), 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
110 #endif
111 #define WAVE_FORMAT_ADPCM 0x0002
112 #define WAVE_FORMAT_IEEE_FLOAT 0x0003
113 #define WAVE_FORMAT_ALAW 0x0006
114 #define WAVE_FORMAT_MULAW 0x0007
115 #define WAVE_FORMAT_MPEG 0x0050
116 #define WAVE_FORMAT_DRM 0x0009
117 #endif
118
119 #include <ks.h>
120 #include <ksmedia.h>
121 #include <tchar.h>
122 #include <assert.h>
123 #include <stdio.h>
124
125 /* These next definitions allow the use of the KSUSER DLL */
126 typedef KSDDKAPI DWORD WINAPI KSCREATEPIN(HANDLE, PKSPIN_CONNECT, ACCESS_MASK, PHANDLE);
127 extern HMODULE DllKsUser;
128 extern KSCREATEPIN* FunctionKsCreatePin;
129
130 /* Forward definition to break circular type reference between pin and filter */
131 struct __PaWinWdmFilter;
132 typedef struct __PaWinWdmFilter PaWinWdmFilter;
133
134 /* The Pin structure
135 * A pin is an input or output node, e.g. for audio flow */
136 typedef struct __PaWinWdmPin
137 {
138 HANDLE handle;
139 PaWinWdmFilter* parentFilter;
140 unsigned long pinId;
141 KSPIN_CONNECT* pinConnect;
142 unsigned long pinConnectSize;
143 KSDATAFORMAT_WAVEFORMATEX* ksDataFormatWfx;
144 KSPIN_COMMUNICATION communication;
145 KSDATARANGE* dataRanges;
146 KSMULTIPLE_ITEM* dataRangesItem;
147 KSPIN_DATAFLOW dataFlow;
148 KSPIN_CINSTANCES instances;
149 unsigned long frameSize;
150 int maxChannels;
151 unsigned long formats;
152 int bestSampleRate;
153 }
154 PaWinWdmPin;
155
156 /* The Filter structure
157 * A filter has a number of pins and a "friendly name" */
158 struct __PaWinWdmFilter
159 {
160 HANDLE handle;
161 int pinCount;
162 PaWinWdmPin** pins;
163 TCHAR filterName[MAX_PATH];
164 TCHAR friendlyName[MAX_PATH];
165 int maxInputChannels;
166 int maxOutputChannels;
167 unsigned long formats;
168 int usageCount;
169 int bestSampleRate;
170 };
171
172 /* PaWinWdmHostApiRepresentation - host api datastructure specific to this implementation */
173 typedef struct __PaWinWdmHostApiRepresentation
174 {
175 PaUtilHostApiRepresentation inheritedHostApiRep;
176 PaUtilStreamInterface callbackStreamInterface;
177 PaUtilStreamInterface blockingStreamInterface;
178
179 PaUtilAllocationGroup* allocations;
180 PaWinWdmFilter** filters;
181 int filterCount;
182 }
183 PaWinWdmHostApiRepresentation;
184
185 typedef struct __PaWinWdmDeviceInfo
186 {
187 PaDeviceInfo inheritedDeviceInfo;
188 PaWinWdmFilter* filter;
189 }
190 PaWinWdmDeviceInfo;
191
192 typedef struct __DATAPACKET
193 {
194 KSSTREAM_HEADER Header;
195 OVERLAPPED Signal;
196 } DATAPACKET;
197
198 /* PaWinWdmStream - a stream data structure specifically for this implementation */
199 typedef struct __PaWinWdmStream
200 {
201 PaUtilStreamRepresentation streamRepresentation;
202 PaUtilCpuLoadMeasurer cpuLoadMeasurer;
203 PaUtilBufferProcessor bufferProcessor;
204
205 PaWinWdmPin* recordingPin;
206 PaWinWdmPin* playbackPin;
207 char* hostBuffer;
208 unsigned long framesPerHostIBuffer;
209 unsigned long framesPerHostOBuffer;
210 int bytesPerInputFrame;
211 int bytesPerOutputFrame;
212 int streamStarted;
213 int streamActive;
214 int streamStop;
215 int streamAbort;
216 int oldProcessPriority;
217 HANDLE streamThread;
218 HANDLE events[5]; /* 2 play + 2 record packets + abort events */
219 DATAPACKET packets[4]; /* 2 play + 2 record */
220 PaStreamFlags streamFlags;
221 /* These values handle the case where the user wants to use fewer
222 * channels than the device has */
223 int userInputChannels;
224 int deviceInputChannels;
225 int userOutputChannels;
226 int deviceOutputChannels;
227 int inputSampleSize;
228 int outputSampleSize;
229 }
230 PaWinWdmStream;
231
232 #include <setupapi.h>
233
234 HMODULE DllKsUser = NULL;
235 KSCREATEPIN* FunctionKsCreatePin = NULL;
236
237 /* prototypes for functions declared in this file */
238
239 #ifdef __cplusplus
240 extern "C"
241 {
242 #endif /* __cplusplus */
243
244 PaError PaWinWdm_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
245
246 #ifdef __cplusplus
247 }
248 #endif /* __cplusplus */
249
250 /* Low level I/O functions */
251 static PaError WdmSyncIoctl(HANDLE handle,
252 unsigned long ioctlNumber,
253 void* inBuffer,
254 unsigned long inBufferCount,
255 void* outBuffer,
256 unsigned long outBufferCount,
257 unsigned long* bytesReturned);
258 static PaError WdmGetPropertySimple(HANDLE handle,
259 const GUID* const guidPropertySet,
260 unsigned long property,
261 void* value,
262 unsigned long valueCount,
263 void* instance,
264 unsigned long instanceCount);
265 static PaError WdmSetPropertySimple(HANDLE handle,
266 const GUID* const guidPropertySet,
267 unsigned long property,
268 void* value,
269 unsigned long valueCount,
270 void* instance,
271 unsigned long instanceCount);
272 static PaError WdmGetPinPropertySimple(HANDLE handle,
273 unsigned long pinId,
274 const GUID* const guidPropertySet,
275 unsigned long property,
276 void* value,
277 unsigned long valueCount);
278 static PaError WdmGetPinPropertyMulti(HANDLE handle,
279 unsigned long pinId,
280 const GUID* const guidPropertySet,
281 unsigned long property,
282 KSMULTIPLE_ITEM** ksMultipleItem);
283
284 /** Pin management functions */
285 static PaWinWdmPin* PinNew(PaWinWdmFilter* parentFilter, unsigned long pinId, PaError* error);
286 static void PinFree(PaWinWdmPin* pin);
287 static void PinClose(PaWinWdmPin* pin);
288 static PaError PinInstantiate(PaWinWdmPin* pin);
289 /*static PaError PinGetState(PaWinWdmPin* pin, KSSTATE* state); NOT USED */
290 static PaError PinSetState(PaWinWdmPin* pin, KSSTATE state);
291 static PaError PinSetFormat(PaWinWdmPin* pin, const WAVEFORMATEX* format);
292 static PaError PinIsFormatSupported(PaWinWdmPin* pin, const WAVEFORMATEX* format);
293
294 /* Filter management functions */
295 static PaWinWdmFilter* FilterNew(
296 TCHAR* filterName,
297 TCHAR* friendlyName,
298 PaError* error);
299 static void FilterFree(PaWinWdmFilter* filter);
300 static PaWinWdmPin* FilterCreateRenderPin(
301 PaWinWdmFilter* filter,
302 const WAVEFORMATEX* wfex,
303 PaError* error);
304 static PaWinWdmPin* FilterFindViableRenderPin(
305 PaWinWdmFilter* filter,
306 const WAVEFORMATEX* wfex,
307 PaError* error);
308 static PaError FilterCanCreateRenderPin(
309 PaWinWdmFilter* filter,
310 const WAVEFORMATEX* wfex);
311 static PaWinWdmPin* FilterCreateCapturePin(
312 PaWinWdmFilter* filter,
313 const WAVEFORMATEX* wfex,
314 PaError* error);
315 static PaWinWdmPin* FilterFindViableCapturePin(
316 PaWinWdmFilter* filter,
317 const WAVEFORMATEX* wfex,
318 PaError* error);
319 static PaError FilterCanCreateCapturePin(
320 PaWinWdmFilter* filter,
321 const WAVEFORMATEX* pwfx);
322 static PaError FilterUse(
323 PaWinWdmFilter* filter);
324 static void FilterRelease(
325 PaWinWdmFilter* filter);
326
327 /* Interface functions */
328 static void Terminate( struct PaUtilHostApiRepresentation *hostApi );
329 static PaError IsFormatSupported(
330 struct PaUtilHostApiRepresentation *hostApi,
331 const PaStreamParameters *inputParameters,
332 const PaStreamParameters *outputParameters,
333 double sampleRate );
334 static PaError OpenStream(
335 struct PaUtilHostApiRepresentation *hostApi,
336 PaStream** s,
337 const PaStreamParameters *inputParameters,
338 const PaStreamParameters *outputParameters,
339 double sampleRate,
340 unsigned long framesPerBuffer,
341 PaStreamFlags streamFlags,
342 PaStreamCallback *streamCallback,
343 void *userData );
344 static PaError CloseStream( PaStream* stream );
345 static PaError StartStream( PaStream *stream );
346 static PaError StopStream( PaStream *stream );
347 static PaError AbortStream( PaStream *stream );
348 static PaError IsStreamStopped( PaStream *s );
349 static PaError IsStreamActive( PaStream *stream );
350 static PaTime GetStreamTime( PaStream *stream );
351 static double GetStreamCpuLoad( PaStream* stream );
352 static PaError ReadStream(
353 PaStream* stream,
354 void *buffer,
355 unsigned long frames );
356 static PaError WriteStream(
357 PaStream* stream,
358 const void *buffer,
359 unsigned long frames );
360 static signed long GetStreamReadAvailable( PaStream* stream );
361 static signed long GetStreamWriteAvailable( PaStream* stream );
362
363 /* Utility functions */
364 static unsigned long GetWfexSize(const WAVEFORMATEX* wfex);
365 static PaError BuildFilterList(PaWinWdmHostApiRepresentation* wdmHostApi);
366 static BOOL PinWrite(HANDLE h, DATAPACKET* p);
367 static BOOL PinRead(HANDLE h, DATAPACKET* p);
368 static void DuplicateFirstChannelInt16(void* buffer, int channels, int samples);
369 static void DuplicateFirstChannelInt24(void* buffer, int channels, int samples);
370 static DWORD WINAPI ProcessingThread(LPVOID pParam);
371
372 /* Function bodies */
373
374 static unsigned long GetWfexSize(const WAVEFORMATEX* wfex)
375 {
376 if( wfex->wFormatTag == WAVE_FORMAT_PCM )
377 {
378 return sizeof( WAVEFORMATEX );
379 }
380 else
381 {
382 return (sizeof( WAVEFORMATEX ) + wfex->cbSize);
383 }
384 }
385
386 /*
387 Low level pin/filter access functions
388 */
389 static PaError WdmSyncIoctl(
390 HANDLE handle,
391 unsigned long ioctlNumber,
392 void* inBuffer,
393 unsigned long inBufferCount,
394 void* outBuffer,
395 unsigned long outBufferCount,
396 unsigned long* bytesReturned)
397 {
398 PaError result = paNoError;
399 OVERLAPPED overlapped;
400 int boolResult;
401 unsigned long dummyBytesReturned;
402 unsigned long error;
403
404 if( !bytesReturned )
405 {
406 /* User a dummy as the caller hasn't supplied one */
407 bytesReturned = &dummyBytesReturned;
408 }
409
410 FillMemory((void *)&overlapped,sizeof(overlapped),0);
411 overlapped.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
412 if( !overlapped.hEvent )
413 {
414 result = paInsufficientMemory;
415 goto error;
416 }
417 overlapped.hEvent = (HANDLE)((DWORD_PTR)overlapped.hEvent | 0x1);
418
419 boolResult = DeviceIoControl(handle, ioctlNumber, inBuffer, inBufferCount,
420 outBuffer, outBufferCount, bytesReturned, &overlapped);
421 if( !boolResult )
422 {
423 error = GetLastError();
424 if( error == ERROR_IO_PENDING )
425 {
426 error = WaitForSingleObject(overlapped.hEvent,INFINITE);
427 if( error != WAIT_OBJECT_0 )
428 {
429 result = paUnanticipatedHostError;
430 goto error;
431 }
432 }
433 else if((( error == ERROR_INSUFFICIENT_BUFFER ) ||
434 ( error == ERROR_MORE_DATA )) &&
435 ( ioctlNumber == IOCTL_KS_PROPERTY ) &&
436 ( outBufferCount == 0 ))
437 {
438 boolResult = TRUE;
439 }
440 else
441 {
442 result = paUnanticipatedHostError;
443 }
444 }
445 if( !boolResult )
446 *bytesReturned = 0;
447
448 error:
449 if( overlapped.hEvent )
450 {
451 CloseHandle( overlapped.hEvent );
452 }
453 return result;
454 }
455
456 static PaError WdmGetPropertySimple(HANDLE handle,
457 const GUID* const guidPropertySet,
458 unsigned long property,
459 void* value,
460 unsigned long valueCount,
461 void* instance,
462 unsigned long instanceCount)
463 {
464 PaError result;
465 KSPROPERTY* ksProperty;
466 unsigned long propertyCount;
467
468 propertyCount = sizeof(KSPROPERTY) + instanceCount;
469 ksProperty = (KSPROPERTY*)PaUtil_AllocateMemory( propertyCount );
470 if( !ksProperty )
471 {
472 return paInsufficientMemory;
473 }
474
475 FillMemory((void*)ksProperty,sizeof(ksProperty),0);
476 ksProperty->Set = *guidPropertySet;
477 ksProperty->Id = property;
478 ksProperty->Flags = KSPROPERTY_TYPE_GET;
479
480 if( instance )
481 {
482 memcpy( (void*)(((char*)ksProperty)+sizeof(KSPROPERTY)), instance, instanceCount );
483 }
484
485 result = WdmSyncIoctl(
486 handle,
487 IOCTL_KS_PROPERTY,
488 ksProperty,
489 propertyCount,
490 value,
491 valueCount,
492 NULL);
493
494 PaUtil_FreeMemory( ksProperty );
495 return result;
496 }
497
498 static PaError WdmSetPropertySimple(
499 HANDLE handle,
500 const GUID* const guidPropertySet,
501 unsigned long property,
502 void* value,
503 unsigned long valueCount,
504 void* instance,
505 unsigned long instanceCount)
506 {
507 PaError result;
508 KSPROPERTY* ksProperty;
509 unsigned long propertyCount = 0;
510
511 propertyCount = sizeof(KSPROPERTY) + instanceCount;
512 ksProperty = (KSPROPERTY*)PaUtil_AllocateMemory( propertyCount );
513 if( !ksProperty )
514 {
515 return paInsufficientMemory;
516 }
517
518 ksProperty->Set = *guidPropertySet;
519 ksProperty->Id = property;
520 ksProperty->Flags = KSPROPERTY_TYPE_SET;
521
522 if( instance )
523 {
524 memcpy((void*)((char*)ksProperty + sizeof(KSPROPERTY)), instance, instanceCount);
525 }
526
527 result = WdmSyncIoctl(
528 handle,
529 IOCTL_KS_PROPERTY,
530 ksProperty,
531 propertyCount,
532 value,
533 valueCount,
534 NULL);
535
536 PaUtil_FreeMemory( ksProperty );
537 return result;
538 }
539
540 static PaError WdmGetPinPropertySimple(
541 HANDLE handle,
542 unsigned long pinId,
543 const GUID* const guidPropertySet,
544 unsigned long property,
545 void* value,
546 unsigned long valueCount)
547 {
548 PaError result;
549
550 KSP_PIN ksPProp;
551 ksPProp.Property.Set = *guidPropertySet;
552 ksPProp.Property.Id = property;
553 ksPProp.Property.Flags = KSPROPERTY_TYPE_GET;
554 ksPProp.PinId = pinId;
555 ksPProp.Reserved = 0;
556
557 result = WdmSyncIoctl(
558 handle,
559 IOCTL_KS_PROPERTY,
560 &ksPProp,
561 sizeof(KSP_PIN),
562 value,
563 valueCount,
564 NULL);
565
566 return result;
567 }
568
569 static PaError WdmGetPinPropertyMulti(
570 HANDLE handle,
571 unsigned long pinId,
572 const GUID* const guidPropertySet,
573 unsigned long property,
574 KSMULTIPLE_ITEM** ksMultipleItem)
575 {
576 PaError result;
577 unsigned long multipleItemSize = 0;
578 KSP_PIN ksPProp;
579
580 ksPProp.Property.Set = *guidPropertySet;
581 ksPProp.Property.Id = property;
582 ksPProp.Property.Flags = KSPROPERTY_TYPE_GET;
583 ksPProp.PinId = pinId;
584 ksPProp.Reserved = 0;
585
586 result = WdmSyncIoctl(
587 handle,
588 IOCTL_KS_PROPERTY,
589 &ksPProp.Property,
590 sizeof(KSP_PIN),
591 NULL,
592 0,
593 &multipleItemSize);
594 if( result != paNoError )
595 {
596 return result;
597 }
598
599 *ksMultipleItem = (KSMULTIPLE_ITEM*)PaUtil_AllocateMemory( multipleItemSize );
600 if( !*ksMultipleItem )
601 {
602 return paInsufficientMemory;
603 }
604
605 result = WdmSyncIoctl(
606 handle,
607 IOCTL_KS_PROPERTY,
608 &ksPProp,
609 sizeof(KSP_PIN),
610 (void*)*ksMultipleItem,
611 multipleItemSize,
612 NULL);
613
614 if( result != paNoError )
615 {
616 PaUtil_FreeMemory( ksMultipleItem );
617 }
618
619 return result;
620 }
621
622
623 /*
624 Create a new pin object belonging to a filter
625 The pin object holds all the configuration information about the pin
626 before it is opened, and then the handle of the pin after is opened
627 */
628 static PaWinWdmPin* PinNew(PaWinWdmFilter* parentFilter, unsigned long pinId, PaError* error)
629 {
630 PaWinWdmPin* pin;
631 PaError result;
632 unsigned long i;
633 KSMULTIPLE_ITEM* item = NULL;
634 KSIDENTIFIER* identifier;
635 KSDATARANGE* dataRange;
636
637 PA_LOGE_;
638 PA_DEBUG(("Creating pin %d:\n",pinId));
639
640 /* Allocate the new PIN object */
641 pin = (PaWinWdmPin*)PaUtil_AllocateMemory( sizeof(PaWinWdmPin) );
642 if( !pin )
643 {
644 result = paInsufficientMemory;
645 goto error;
646 }
647
648 /* Zero the pin object */
649 /* memset( (void*)pin, 0, sizeof(PaWinWdmPin) ); */
650
651 pin->parentFilter = parentFilter;
652 pin->pinId = pinId;
653
654 /* Allocate a connect structure */
655 pin->pinConnectSize = sizeof(KSPIN_CONNECT) + sizeof(KSDATAFORMAT_WAVEFORMATEX);
656 pin->pinConnect = (KSPIN_CONNECT*)PaUtil_AllocateMemory( pin->pinConnectSize );
657 if( !pin->pinConnect )
658 {
659 result = paInsufficientMemory;
660 goto error;
661 }
662
663 /* Configure the connect structure with default values */
664 pin->pinConnect->Interface.Set = KSINTERFACESETID_Standard;
665 pin->pinConnect->Interface.Id = KSINTERFACE_STANDARD_STREAMING;
666 pin->pinConnect->Interface.Flags = 0;
667 pin->pinConnect->Medium.Set = KSMEDIUMSETID_Standard;
668 pin->pinConnect->Medium.Id = KSMEDIUM_TYPE_ANYINSTANCE;
669 pin->pinConnect->Medium.Flags = 0;
670 pin->pinConnect->PinId = pinId;
671 pin->pinConnect->PinToHandle = NULL;
672 pin->pinConnect->Priority.PriorityClass = KSPRIORITY_NORMAL;
673 pin->pinConnect->Priority.PrioritySubClass = 1;
674 pin->ksDataFormatWfx = (KSDATAFORMAT_WAVEFORMATEX*)(pin->pinConnect + 1);
675 pin->ksDataFormatWfx->DataFormat.FormatSize = sizeof(KSDATAFORMAT_WAVEFORMATEX);
676 pin->ksDataFormatWfx->DataFormat.Flags = 0;
677 pin->ksDataFormatWfx->DataFormat.Reserved = 0;
678 pin->ksDataFormatWfx->DataFormat.MajorFormat = KSDATAFORMAT_TYPE_AUDIO;
679 pin->ksDataFormatWfx->DataFormat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
680 pin->ksDataFormatWfx->DataFormat.Specifier = KSDATAFORMAT_SPECIFIER_WAVEFORMATEX;
681
682 pin->frameSize = 0; /* Unknown until we instantiate pin */
683
684 /* Get the COMMUNICATION property */
685 result = WdmGetPinPropertySimple(
686 parentFilter->handle,
687 pinId,
688 &KSPROPSETID_Pin,
689 KSPROPERTY_PIN_COMMUNICATION,
690 &pin->communication,
691 sizeof(KSPIN_COMMUNICATION));
692 if( result != paNoError )
693 goto error;
694
695 if( /*(pin->communication != KSPIN_COMMUNICATION_SOURCE) &&*/
696 (pin->communication != KSPIN_COMMUNICATION_SINK) &&
697 (pin->communication != KSPIN_COMMUNICATION_BOTH) )
698 {
699 PA_DEBUG(("Not source/sink\n"));
700 result = paInvalidDevice;
701 goto error;
702 }
703
704 /* Get dataflow information */
705 result = WdmGetPinPropertySimple(
706 parentFilter->handle,
707 pinId,
708 &KSPROPSETID_Pin,
709 KSPROPERTY_PIN_DATAFLOW,
710 &pin->dataFlow,
711 sizeof(KSPIN_DATAFLOW));
712
713 if( result != paNoError )
714 goto error;
715
716 /* Get the INTERFACE property list */
717 result = WdmGetPinPropertyMulti(
718 parentFilter->handle,
719 pinId,
720 &KSPROPSETID_Pin,
721 KSPROPERTY_PIN_INTERFACES,
722 &item);
723
724 if( result != paNoError )
725 goto error;
726
727 identifier = (KSIDENTIFIER*)(item+1);
728
729 /* Check that at least one interface is STANDARD_STREAMING */
730 result = paUnanticipatedHostError;
731 for( i = 0; i < item->Count; i++ )
732 {
733 if( !memcmp( (void*)&identifier[i].Set, (void*)&KSINTERFACESETID_Standard, sizeof( GUID ) ) &&
734 ( identifier[i].Id == KSINTERFACE_STANDARD_STREAMING ) )
735 {
736 result = paNoError;
737 break;
738 }
739 }
740
741 if( result != paNoError )
742 {
743 PA_DEBUG(("No standard streaming\n"));
744 goto error;
745 }
746
747 /* Don't need interfaces any more */
748 PaUtil_FreeMemory( item );
749 item = NULL;
750
751 /* Get the MEDIUM properties list */
752 result = WdmGetPinPropertyMulti(
753 parentFilter->handle,
754 pinId,
755 &KSPROPSETID_Pin,
756 KSPROPERTY_PIN_MEDIUMS,
757 &item);
758
759 if( result != paNoError )
760 goto error;
761
762 identifier = (KSIDENTIFIER*)(item+1); /* Not actually necessary... */
763
764 /* Check that at least one medium is STANDARD_DEVIO */
765 result = paUnanticipatedHostError;
766 for( i = 0; i < item->Count; i++ )
767 {
768 if( !memcmp( (void*)&identifier[i].Set, (void*)&KSMEDIUMSETID_Standard, sizeof( GUID ) ) &&
769 ( identifier[i].Id == KSMEDIUM_STANDARD_DEVIO ) )
770 {
771 result = paNoError;
772 break;
773 }
774 }
775
776 if( result != paNoError )
777 {
778 PA_DEBUG(("No standard devio\n"));
779 goto error;
780 }
781 /* Don't need mediums any more */
782 PaUtil_FreeMemory( item );
783 item = NULL;
784
785 /* Get DATARANGES */
786 result = WdmGetPinPropertyMulti(
787 parentFilter->handle,
788 pinId,
789 &KSPROPSETID_Pin,
790 KSPROPERTY_PIN_DATARANGES,
791 &pin->dataRangesItem);
792
793 if( result != paNoError )
794 goto error;
795
796 pin->dataRanges = (KSDATARANGE*)(pin->dataRangesItem +1);
797
798 /* Check that at least one datarange supports audio */
799 result = paUnanticipatedHostError;
800 dataRange = pin->dataRanges;
801 pin->maxChannels = 0;
802 pin->bestSampleRate = 0;
803 pin->formats = 0;
804 for( i = 0; i <pin->dataRangesItem->Count; i++)
805 {
806 PA_DEBUG(("DR major format %x\n",*(unsigned long*)(&(dataRange->MajorFormat))));
807 /* Check that subformat is WAVEFORMATEX, PCM or WILDCARD */
808 if( IS_VALID_WAVEFORMATEX_GUID(&dataRange->SubFormat) ||
809 !memcmp((void*)&dataRange->SubFormat, (void*)&KSDATAFORMAT_SUBTYPE_PCM, sizeof ( GUID ) ) ||
810 ( !memcmp((void*)&dataRange->SubFormat, (void*)&KSDATAFORMAT_SUBTYPE_WILDCARD, sizeof ( GUID ) ) &&
811 ( !memcmp((void*)&dataRange->MajorFormat, (void*)&KSDATAFORMAT_TYPE_AUDIO, sizeof ( GUID ) ) ) ) )
812 {
813 result = paNoError;
814 /* Record the maximum possible channels with this pin */
815 PA_DEBUG(("MaxChannel: %d\n",pin->maxChannels));
816 if( (int)((KSDATARANGE_AUDIO*)dataRange)->MaximumChannels > pin->maxChannels )
817 {
818 pin->maxChannels = ((KSDATARANGE_AUDIO*)dataRange)->MaximumChannels;
819 /*PA_DEBUG(("MaxChannel: %d\n",pin->maxChannels));*/
820 }
821 /* Record the formats (bit depths) that are supported */
822 if( ((KSDATARANGE_AUDIO*)dataRange)->MinimumBitsPerSample <= 16 )
823 {
824 pin->formats |= paInt16;
825 PA_DEBUG(("Format 16 bit supported\n"));
826 }
827 if( ((KSDATARANGE_AUDIO*)dataRange)->MaximumBitsPerSample >= 24 )
828 {
829 pin->formats |= paInt24;
830 PA_DEBUG(("Format 24 bit supported\n"));
831 }
832 if( ( pin->bestSampleRate != 48000) &&
833 (((KSDATARANGE_AUDIO*)dataRange)->MaximumSampleFrequency >= 48000) &&
834 (((KSDATARANGE_AUDIO*)dataRange)->MinimumSampleFrequency <= 48000) )
835 {
836 pin->bestSampleRate = 48000;
837 PA_DEBUG(("48kHz supported\n"));
838 }
839 else if(( pin->bestSampleRate != 48000) && ( pin->bestSampleRate != 44100 ) &&
840 (((KSDATARANGE_AUDIO*)dataRange)->MaximumSampleFrequency >= 44100) &&
841 (((KSDATARANGE_AUDIO*)dataRange)->MinimumSampleFrequency <= 44100) )
842 {
843 pin->bestSampleRate = 44100;
844 PA_DEBUG(("44.1kHz supported\n"));
845 }
846 else
847 {
848 pin->bestSampleRate = ((KSDATARANGE_AUDIO*)dataRange)->MaximumSampleFrequency;
849 }
850 }
851 dataRange = (KSDATARANGE*)( ((char*)dataRange) + dataRange->FormatSize);
852 }
853
854 if( result != paNoError )
855 goto error;
856
857 /* Get instance information */
858 result = WdmGetPinPropertySimple(
859 parentFilter->handle,
860 pinId,
861 &KSPROPSETID_Pin,
862 KSPROPERTY_PIN_CINSTANCES,
863 &pin->instances,
864 sizeof(KSPIN_CINSTANCES));
865
866 if( result != paNoError )
867 goto error;
868
869 /* Success */
870 *error = paNoError;
871 PA_DEBUG(("Pin created successfully\n"));
872 PA_LOGL_;
873 return pin;
874
875 error:
876 /*
877 Error cleanup
878 */
879 PaUtil_FreeMemory( item );
880 if( pin )
881 {
882 PaUtil_FreeMemory( pin->pinConnect );
883 PaUtil_FreeMemory( pin->dataRangesItem );
884 PaUtil_FreeMemory( pin );
885 }
886 *error = result;
887 PA_LOGL_;
888 return NULL;
889 }
890
891 /*
892 Safely free all resources associated with the pin
893 */
894 static void PinFree(PaWinWdmPin* pin)
895 {
896 PA_LOGE_;
897 if( pin )
898 {
899 PinClose(pin);
900 if( pin->pinConnect )
901 {
902 PaUtil_FreeMemory( pin->pinConnect );
903 }
904 if( pin->dataRangesItem )
905 {
906 PaUtil_FreeMemory( pin->dataRangesItem );
907 }
908 PaUtil_FreeMemory( pin );
909 }
910 PA_LOGL_;
911 }
912
913 /*
914 If the pin handle is open, close it
915 */
916 static void PinClose(PaWinWdmPin* pin)
917 {
918 PA_LOGE_;
919 if( pin == NULL )
920 {
921 PA_DEBUG(("Closing NULL pin!"));
922 PA_LOGL_;
923 return;
924 }
925 if( pin->handle != NULL )
926 {
927 PinSetState( pin, KSSTATE_PAUSE );
928 PinSetState( pin, KSSTATE_STOP );
929 CloseHandle( pin->handle );
930 pin->handle = NULL;
931 FilterRelease(pin->parentFilter);
932 }
933 PA_LOGL_;
934 }
935
936 /*
937 Set the state of this (instantiated) pin
938 */
939 static PaError PinSetState(PaWinWdmPin* pin, KSSTATE state)
940 {
941 PaError result;
942
943 PA_LOGE_;
944 if( pin == NULL )
945 return paInternalError;
946 if( pin->handle == NULL )
947 return paInternalError;
948
949 result = WdmSetPropertySimple(
950 pin->handle,
951 &KSPROPSETID_Connection,
952 KSPROPERTY_CONNECTION_STATE,
953 &state,
954 sizeof(state),
955 NULL,
956 0);
957 PA_LOGL_;
958 return result;
959 }
960
961 static PaError PinInstantiate(PaWinWdmPin* pin)
962 {
963 PaError result;
964 unsigned long createResult;
965 KSALLOCATOR_FRAMING ksaf;
966 KSALLOCATOR_FRAMING_EX ksafex;
967
968 PA_LOGE_;
969
970 if( pin == NULL )
971 return paInternalError;
972 if(!pin->pinConnect)
973 return paInternalError;
974
975 FilterUse(pin->parentFilter);
976
977 createResult = FunctionKsCreatePin(
978 pin->parentFilter->handle,
979 pin->pinConnect,
980 GENERIC_WRITE | GENERIC_READ,
981 &pin->handle
982 );
983
984 PA_DEBUG(("Pin create result = %x\n",createResult));
985 if( createResult != ERROR_SUCCESS )
986 {
987 FilterRelease(pin->parentFilter);
988 pin->handle = NULL;
989 return paInvalidDevice;
990 }
991
992 result = WdmGetPropertySimple(
993 pin->handle,
994 &KSPROPSETID_Connection,
995 KSPROPERTY_CONNECTION_ALLOCATORFRAMING,
996 &ksaf,
997 sizeof(ksaf),
998 NULL,
999 0);
1000
1001 if( result != paNoError )
1002 {
1003 result = WdmGetPropertySimple(
1004 pin->handle,
1005 &KSPROPSETID_Connection,
1006 KSPROPERTY_CONNECTION_ALLOCATORFRAMING_EX,
1007 &ksafex,
1008 sizeof(ksafex),
1009 NULL,
1010 0);
1011 if( result == paNoError )
1012 {
1013 pin->frameSize = ksafex.FramingItem[0].FramingRange.Range.MinFrameSize;
1014 }
1015 }
1016 else
1017 {
1018 pin->frameSize = ksaf.FrameSize;
1019 }
1020
1021 PA_LOGL_;
1022
1023 return paNoError;
1024 }
1025
1026 /* NOT USED
1027 static PaError PinGetState(PaWinWdmPin* pin, KSSTATE* state)
1028 {
1029 PaError result;
1030
1031 if( state == NULL )
1032 return paInternalError;
1033 if( pin == NULL )
1034 return paInternalError;
1035 if( pin->handle == NULL )
1036 return paInternalError;
1037
1038 result = WdmGetPropertySimple(
1039 pin->handle,
1040 KSPROPSETID_Connection,
1041 KSPROPERTY_CONNECTION_STATE,
1042 state,
1043 sizeof(KSSTATE),
1044 NULL,
1045 0);
1046
1047 return result;
1048 }
1049 */
1050 static PaError PinSetFormat(PaWinWdmPin* pin, const WAVEFORMATEX* format)
1051 {
1052 unsigned long size;
1053 void* newConnect;
1054
1055 PA_LOGE_;
1056
1057 if( pin == NULL )
1058 return paInternalError;
1059 if( format == NULL )
1060 return paInternalError;
1061
1062 size = GetWfexSize(format) + sizeof(KSPIN_CONNECT) + sizeof(KSDATAFORMAT_WAVEFORMATEX) - sizeof(WAVEFORMATEX);
1063
1064 if( pin->pinConnectSize != size )
1065 {
1066 newConnect = PaUtil_AllocateMemory( size );
1067 if( newConnect == NULL )
1068 return paInsufficientMemory;
1069 memcpy( newConnect, (void*)pin->pinConnect, min(pin->pinConnectSize,size) );
1070 PaUtil_FreeMemory( pin->pinConnect );
1071 pin->pinConnect = (KSPIN_CONNECT*)newConnect;
1072 pin->pinConnectSize = size;
1073 pin->ksDataFormatWfx = (KSDATAFORMAT_WAVEFORMATEX*)((KSPIN_CONNECT*)newConnect + 1);
1074 pin->ksDataFormatWfx->DataFormat.FormatSize = size - sizeof(KSPIN_CONNECT);
1075 }
1076
1077 memcpy( (void*)&(pin->ksDataFormatWfx->WaveFormatEx), format, GetWfexSize(format) );
1078 pin->ksDataFormatWfx->DataFormat.SampleSize = (unsigned short)(format->nChannels * (format->wBitsPerSample / 8));
1079
1080 PA_LOGL_;
1081
1082 return paNoError;
1083 }
1084
1085 static PaError PinIsFormatSupported(PaWinWdmPin* pin, const WAVEFORMATEX* format)
1086 {
1087 KSDATARANGE_AUDIO* dataRange;
1088 unsigned long count;
1089 GUID guid = DYNAMIC_GUID( DEFINE_WAVEFORMATEX_GUID(format->wFormatTag) );
1090 PaError result = paInvalidDevice;
1091
1092 PA_LOGE_;
1093
1094 if( format->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
1095 {
1096 guid = ((WAVEFORMATEXTENSIBLE*)format)->SubFormat;
1097 }
1098 dataRange = (KSDATARANGE_AUDIO*)pin->dataRanges;
1099 for(count = 0; count<pin->dataRangesItem->Count; count++)
1100 {
1101 if(( !memcmp(&(dataRange->DataRange.MajorFormat),&KSDATAFORMAT_TYPE_AUDIO,sizeof(GUID)) ) ||
1102 ( !memcmp(&(dataRange->DataRange.MajorFormat),&KSDATAFORMAT_TYPE_WILDCARD,sizeof(GUID)) ))
1103 {
1104 /* This is an audio or wildcard datarange... */
1105 if(( !memcmp(&(dataRange->DataRange.SubFormat),&KSDATAFORMAT_SUBTYPE_WILDCARD,sizeof(GUID)) ) ||
1106 ( !memcmp(&(dataRange->DataRange.SubFormat),&guid,sizeof(GUID)) ))
1107 {
1108 if(( !memcmp(&(dataRange->DataRange.Specifier),&KSDATAFORMAT_SPECIFIER_WILDCARD,sizeof(GUID)) ) ||
1109 ( !memcmp(&(dataRange->DataRange.Specifier),&KSDATAFORMAT_SPECIFIER_WAVEFORMATEX,sizeof(GUID) )))
1110 {
1111
1112 PA_DEBUG(("Pin:%x, DataRange:%d\n",(void*)pin,count));
1113 PA_DEBUG(("\tFormatSize:%d, SampleSize:%d\n",dataRange->DataRange.FormatSize,dataRange->DataRange.SampleSize));
1114 PA_DEBUG(("\tMaxChannels:%d\n",dataRange->MaximumChannels));
1115 PA_DEBUG(("\tBits:%d-%d\n",dataRange->MinimumBitsPerSample,dataRange->MaximumBitsPerSample));
1116 PA_DEBUG(("\tSampleRate:%d-%d\n",dataRange->MinimumSampleFrequency,dataRange->MaximumSampleFrequency));
1117
1118 if( dataRange->MaximumChannels < format->nChannels )
1119 {
1120 result = paInvalidChannelCount;
1121 continue;
1122 }
1123 if( dataRange->MinimumBitsPerSample > format->wBitsPerSample )
1124 {
1125 result = paSampleFormatNotSupported;
1126 continue;
1127 }
1128 if( dataRange->MaximumBitsPerSample < format->wBitsPerSample )
1129 {
1130 result = paSampleFormatNotSupported;
1131 continue;
1132 }
1133 if( dataRange->MinimumSampleFrequency > format->nSamplesPerSec )
1134 {
1135 result = paInvalidSampleRate;
1136 continue;
1137 }
1138 if( dataRange->MaximumSampleFrequency < format->nSamplesPerSec )
1139 {
1140 result = paInvalidSampleRate;
1141 continue;
1142 }
1143 /* Success! */
1144 PA_LOGL_;
1145 return paNoError;
1146 }
1147 }
1148 }
1149 dataRange = (KSDATARANGE_AUDIO*)( ((char*)dataRange) + dataRange->DataRange.FormatSize);
1150 }
1151
1152 PA_LOGL_;
1153
1154 return result;
1155 }
1156
1157 /**
1158 * Create a new filter object
1159 */
1160 static PaWinWdmFilter* FilterNew(TCHAR* filterName, TCHAR* friendlyName, PaError* error)
1161 {
1162 PaWinWdmFilter* filter;
1163 PaError result;
1164 int pinId;
1165 int valid;
1166
1167
1168 /* Allocate the new filter object */
1169 filter = (PaWinWdmFilter*)PaUtil_AllocateMemory( sizeof(PaWinWdmFilter) );
1170 if( !filter )
1171 {
1172 result = paInsufficientMemory;
1173 goto error;
1174 }
1175
1176 /* Zero the filter object - done by AllocateMemory */
1177 /* memset( (void*)filter, 0, sizeof(PaWinWdmFilter) ); */
1178
1179 /* Copy the filter name */
1180 _tcsncpy(filter->filterName, filterName, MAX_PATH);
1181
1182 /* Copy the friendly name */
1183 _tcsncpy(filter->friendlyName, friendlyName, MAX_PATH);
1184
1185 /* Open the filter handle */
1186 result = FilterUse(filter);
1187 if( result != paNoError )
1188 {
1189 goto error;
1190 }
1191
1192 /* Get pin count */
1193 result = WdmGetPinPropertySimple
1194 (
1195 filter->handle,
1196 0,
1197 &KSPROPSETID_Pin,
1198 KSPROPERTY_PIN_CTYPES,
1199 &filter->pinCount,
1200 sizeof(filter->pinCount)
1201 );
1202
1203 if( result != paNoError)
1204 {
1205 goto error;
1206 }
1207
1208 /* Allocate pointer array to hold the pins */
1209 filter->pins = (PaWinWdmPin**)PaUtil_AllocateMemory( sizeof(PaWinWdmPin*) * filter->pinCount );
1210 if( !filter->pins )
1211 {
1212 result = paInsufficientMemory;
1213 goto error;
1214 }
1215
1216 /* Create all the pins we can */
1217 filter->maxInputChannels = 0;
1218 filter->maxOutputChannels = 0;
1219 filter->bestSampleRate = 0;
1220
1221 valid = 0;
1222 for(pinId = 0; pinId < filter->pinCount; pinId++)
1223 {
1224 /* Create the pin with this Id */
1225 PaWinWdmPin* newPin;
1226 newPin = PinNew(filter, pinId, &result);
1227 if( result == paInsufficientMemory )
1228 goto error;
1229 if( newPin != NULL )
1230 {
1231 filter->pins[pinId] = newPin;
1232 valid = 1;
1233
1234 /* Get the max output channel count */
1235 if(( newPin->dataFlow == KSPIN_DATAFLOW_IN ) &&
1236 (( newPin->communication == KSPIN_COMMUNICATION_SINK) ||
1237 ( newPin->communication == KSPIN_COMMUNICATION_BOTH)))
1238 {
1239 if(newPin->maxChannels > filter->maxOutputChannels)
1240 filter->maxOutputChannels = newPin->maxChannels;
1241 filter->formats |= newPin->formats;
1242 }
1243 /* Get the max input channel count */
1244 if(( newPin->dataFlow == KSPIN_DATAFLOW_OUT ) &&
1245 (( newPin->communication == KSPIN_COMMUNICATION_SINK) ||
1246 ( newPin->communication == KSPIN_COMMUNICATION_BOTH)))
1247 {
1248 if(newPin->maxChannels > filter->maxInputChannels)
1249 filter->maxInputChannels = newPin->maxChannels;
1250 filter->formats |= newPin->formats;
1251 }
1252
1253 if(newPin->bestSampleRate > filter->bestSampleRate)
1254 {
1255 filter->bestSampleRate = newPin->bestSampleRate;
1256 }
1257 }
1258 }
1259
1260 if(( filter->maxInputChannels == 0) && ( filter->maxOutputChannels == 0))
1261 {
1262 /* No input or output... not valid */
1263 valid = 0;
1264 }
1265
1266 if( !valid )
1267 {
1268 /* No valid pin was found on this filter so we destroy it */
1269 result = paDeviceUnavailable;
1270 goto error;
1271 }
1272
1273 /* Close the filter handle for now
1274 * It will be opened later when needed */
1275 FilterRelease(filter);
1276
1277 *error = paNoError;
1278 return filter;
1279
1280 error:
1281 /*
1282 Error cleanup
1283 */
1284 if( filter )
1285 {
1286 for( pinId = 0; pinId < filter->pinCount; pinId++ )
1287 PinFree(filter->pins[pinId]);
1288 PaUtil_FreeMemory( filter->pins );
1289 if( filter->handle )
1290 CloseHandle( filter->handle );
1291 PaUtil_FreeMemory( filter );
1292 }
1293 *error = result;
1294 return NULL;
1295 }
1296
1297 /**
1298 * Free a previously created filter
1299 */
1300 static void FilterFree(PaWinWdmFilter* filter)
1301 {
1302 int pinId;
1303 PA_LOGL_;
1304 if( filter )
1305 {
1306 for( pinId = 0; pinId < filter->pinCount; pinId++ )
1307 PinFree(filter->pins[pinId]);
1308 PaUtil_FreeMemory( filter->pins );
1309 if( filter->handle )
1310 CloseHandle( filter->handle );
1311 PaUtil_FreeMemory( filter );
1312 }
1313 PA_LOGE_;
1314 }
1315
1316 /**
1317 * Reopen the filter handle if necessary so it can be used
1318 **/
1319 static PaError FilterUse(PaWinWdmFilter* filter)
1320 {
1321 assert( filter );
1322
1323 PA_LOGE_;
1324 if( filter->handle == NULL )
1325 {
1326 /* Open the filter */
1327 filter->handle = CreateFile(
1328 filter->filterName,
1329 GENERIC_READ | GENERIC_WRITE,
1330 0,
1331 NULL,
1332 OPEN_EXISTING,
1333 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
1334 NULL);
1335
1336 if( filter->handle == NULL )
1337 {
1338 return paDeviceUnavailable;
1339 }
1340 }
1341 filter->usageCount++;
1342 PA_LOGL_;
1343 return paNoError;
1344 }
1345
1346 /**
1347 * Release the filter handle if nobody is using it
1348 **/
1349 static void FilterRelease(PaWinWdmFilter* filter)
1350 {
1351 assert( filter );
1352 assert( filter->usageCount > 0 );
1353
1354 PA_LOGE_;
1355 filter->usageCount--;
1356 if( filter->usageCount == 0 )
1357 {
1358 if( filter->handle != NULL )
1359 {
1360 CloseHandle( filter->handle );
1361 filter->handle = NULL;
1362 }
1363 }
1364 PA_LOGL_;
1365 }
1366
1367 /**
1368 * Create a render (playback) Pin using the supplied format
1369 **/
1370 static PaWinWdmPin* FilterCreateRenderPin(PaWinWdmFilter* filter,
1371 const WAVEFORMATEX* wfex,
1372 PaError* error)
1373 {
1374 PaError result;
1375 PaWinWdmPin* pin;
1376
1377 assert( filter );
1378
1379 pin = FilterFindViableRenderPin(filter,wfex,&result);
1380 if(!pin)
1381 {
1382 goto error;
1383 }
1384 result = PinSetFormat(pin,wfex);
1385 if( result != paNoError )
1386 {
1387 goto error;
1388 }
1389 result = PinInstantiate(pin);
1390 if( result != paNoError )
1391 {
1392 goto error;
1393 }
1394
1395 *error = paNoError;
1396 return pin;
1397
1398 error:
1399 *error = result;
1400 return NULL;
1401 }
1402
1403 /**
1404 * Find a pin that supports the given format
1405 **/
1406 static PaWinWdmPin* FilterFindViableRenderPin(PaWinWdmFilter* filter,
1407 const WAVEFORMATEX* wfex,
1408 PaError* error)
1409 {
1410 int pinId;
1411 PaWinWdmPin* pin;
1412 PaError result = paDeviceUnavailable;
1413 *error = paNoError;
1414
1415 assert( filter );
1416
1417 for( pinId = 0; pinId<filter->pinCount; pinId++ )
1418 {
1419 pin = filter->pins[pinId];
1420 if( pin != NULL )
1421 {
1422 if(( pin->dataFlow == KSPIN_DATAFLOW_IN ) &&
1423 (( pin->communication == KSPIN_COMMUNICATION_SINK) ||
1424 ( pin->communication == KSPIN_COMMUNICATION_BOTH)))
1425 {
1426 result = PinIsFormatSupported( pin, wfex );
1427 if( result == paNoError )
1428 {
1429 return pin;
1430 }
1431 }
1432 }
1433 }
1434
1435 *error = result;
1436 return NULL;
1437 }
1438
1439 /**
1440 * Check if there is a pin that should playback
1441 * with the supplied format
1442 **/
1443 static PaError FilterCanCreateRenderPin(PaWinWdmFilter* filter,
1444 const WAVEFORMATEX* wfex)
1445 {
1446 PaWinWdmPin* pin;
1447 PaError result;
1448
1449 assert ( filter );
1450
1451 pin = FilterFindViableRenderPin(filter,wfex,&result);
1452 /* result will be paNoError if pin found
1453 * or else an error code indicating what is wrong with the format
1454 **/
1455 return result;
1456 }
1457
1458 /**
1459 * Create a capture (record) Pin using the supplied format
1460 **/
1461 static PaWinWdmPin* FilterCreateCapturePin(PaWinWdmFilter* filter,
1462 const WAVEFORMATEX* wfex,
1463 PaError* error)
1464 {
1465 PaError result;
1466 PaWinWdmPin* pin;
1467
1468 assert( filter );
1469
1470 pin = FilterFindViableCapturePin(filter,wfex,&result);
1471 if(!pin)
1472 {
1473 goto error;
1474 }
1475
1476 result = PinSetFormat(pin,wfex);
1477 if( result != paNoError )
1478 {
1479 goto error;
1480 }
1481
1482 result = PinInstantiate(pin);
1483 if( result != paNoError )
1484 {
1485 goto error;
1486 }
1487
1488 *error = paNoError;
1489 return pin;
1490
1491 error:
1492 *error = result;
1493 return NULL;
1494 }
1495
1496 /**
1497 * Find a capture pin that supports the given format
1498 **/
1499 static PaWinWdmPin* FilterFindViableCapturePin(PaWinWdmFilter* filter,
1500 const WAVEFORMATEX* wfex,
1501 PaError* error)
1502 {
1503 int pinId;
1504 PaWinWdmPin* pin;
1505 PaError result = paDeviceUnavailable;
1506 *error = paNoError;
1507
1508 assert( filter );
1509
1510 for( pinId = 0; pinId<filter->pinCount; pinId++ )
1511 {
1512 pin = filter->pins[pinId];
1513 if( pin != NULL )
1514 {
1515 if(( pin->dataFlow == KSPIN_DATAFLOW_OUT ) &&
1516 (( pin->communication == KSPIN_COMMUNICATION_SINK) ||
1517 ( pin->communication == KSPIN_COMMUNICATION_BOTH)))
1518 {
1519 result = PinIsFormatSupported( pin, wfex );
1520 if( result == paNoError )
1521 {
1522 return pin;
1523 }
1524 }
1525 }
1526 }
1527
1528 *error = result;
1529 return NULL;
1530 }
1531
1532 /**
1533 * Check if there is a pin that should playback
1534 * with the supplied format
1535 **/
1536 static PaError FilterCanCreateCapturePin(PaWinWdmFilter* filter,
1537 const WAVEFORMATEX* wfex)
1538 {
1539 PaWinWdmPin* pin;
1540 PaError result;
1541
1542 assert ( filter );
1543
1544 pin = FilterFindViableCapturePin(filter,wfex,&result);
1545 /* result will be paNoError if pin found
1546 * or else an error code indicating what is wrong with the format
1547 **/
1548 return result;
1549 }
1550
1551 /**
1552 * Build the list of available filters
1553 */
1554 static PaError BuildFilterList(PaWinWdmHostApiRepresentation* wdmHostApi)
1555 {
1556 PaError result = paNoError;
1557 HDEVINFO handle = NULL;
1558 int device;
1559 int invalidDevices;
1560 int slot;
1561 SP_DEVICE_INTERFACE_DATA interfaceData;
1562 SP_DEVICE_INTERFACE_DATA aliasData;
1563 SP_DEVINFO_DATA devInfoData;
1564 int noError;
1565 const int sizeInterface = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + (MAX_PATH * sizeof(WCHAR));
1566 unsigned char interfaceDetailsArray[sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + (MAX_PATH * sizeof(WCHAR))];
1567 SP_DEVICE_INTERFACE_DETAIL_DATA* devInterfaceDetails = (SP_DEVICE_INTERFACE_DETAIL_DATA*)interfaceDetailsArray;
1568 TCHAR friendlyName[MAX_PATH];
1569 HKEY hkey;
1570 DWORD sizeFriendlyName;
1571 DWORD type;
1572 PaWinWdmFilter* newFilter;
1573 GUID* category = (GUID*)&KSCATEGORY_AUDIO;
1574 GUID* alias_render = (GUID*)&KSCATEGORY_RENDER;
1575 GUID* alias_capture = (GUID*)&KSCATEGORY_CAPTURE;
1576 DWORD hasAlias;
1577
1578 PA_LOGE_;
1579
1580 devInterfaceDetails->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
1581
1582 /* Open a handle to search for devices (filters) */
1583 handle = SetupDiGetClassDevs(category,NULL,NULL,DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
1584 if( handle == NULL )
1585 {
1586 return paUnanticipatedHostError;
1587 }
1588 PA_DEBUG(("Setup called\n"));
1589
1590 /* First let's count the number of devices so we can allocate a list */
1591 invalidDevices = 0;
1592 for( device = 0;;device++ )
1593 {
1594 interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
1595 interfaceData.Reserved = 0;
1596 aliasData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
1597 aliasData.Reserved = 0;
1598 noError = SetupDiEnumDeviceInterfaces(handle,NULL,category,device,&interfaceData);
1599 PA_DEBUG(("Enum called\n"));
1600 if( !noError )
1601 break; /* No more devices */
1602
1603 /* Check this one has the render or capture alias */
1604 hasAlias = 0;
1605 noError = SetupDiGetDeviceInterfaceAlias(handle,&interfaceData,alias_render,&aliasData);
1606 PA_DEBUG(("noError = %d\n",noError));
1607 if(noError)
1608 {
1609 if(aliasData.Flags && (!(aliasData.Flags & SPINT_REMOVED)))
1610 {
1611 PA_DEBUG(("Device %d has render alias\n",device));
1612 hasAlias |= 1; /* Has render alias */
1613 }
1614 else
1615 {
1616 PA_DEBUG(("Device %d has no render alias\n",device));
1617 }
1618 }
1619 noError = SetupDiGetDeviceInterfaceAlias(handle,&interfaceData,alias_capture,&aliasData);
1620 if(noError)
1621 {
1622 if(aliasData.Flags && (!(aliasData.Flags & SPINT_REMOVED)))
1623 {
1624 PA_DEBUG(("Device %d has capture alias\n",device));
1625 hasAlias |= 2; /* Has capture alias */
1626 }
1627 else
1628 {
1629 PA_DEBUG(("Device %d has no capture alias\n",device));
1630 }
1631 }
1632 if(!hasAlias)
1633 invalidDevices++; /* This was not a valid capture or render audio device */
1634
1635 }
1636 /* Remember how many there are */
1637 wdmHostApi->filterCount = device-invalidDevices;
1638
1639 PA_DEBUG(("Interfaces found: %d\n",device-invalidDevices));
1640
1641 /* Now allocate the list of pointers to devices */
1642 wdmHostApi->filters = (PaWinWdmFilter**)PaUtil_AllocateMemory( sizeof(PaWinWdmFilter*) * device );
1643 if( !wdmHostApi->filters )
1644 {
1645 if(handle != NULL)
1646 SetupDiDestroyDeviceInfoList(handle);
1647 return paInsufficientMemory;
1648 }
1649
1650 /* Now create filter objects for each interface found */
1651 slot = 0;
1652 for( device = 0;;device++ )
1653 {
1654 interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
1655 interfaceData.Reserved = 0;
1656 aliasData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
1657 aliasData.Reserved = 0;
1658 devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
1659 devInfoData.Reserved = 0;
1660
1661 noError = SetupDiEnumDeviceInterfaces(handle,NULL,category,device,&interfaceData);
1662 if( !noError )
1663 break; /* No more devices */
1664
1665 /* Check this one has the render or capture alias */
1666 hasAlias = 0;
1667 noError = SetupDiGetDeviceInterfaceAlias(handle,&interfaceData,alias_render,&aliasData);
1668 if(noError)
1669 {
1670 if(aliasData.Flags && (!(aliasData.Flags & SPINT_REMOVED)))
1671 {
1672 PA_DEBUG(("Device %d has render alias\n",device));
1673 hasAlias |= 1; /* Has render alias */
1674 }
1675 }
1676 noError = SetupDiGetDeviceInterfaceAlias(handle,&interfaceData,alias_capture,&aliasData);
1677 if(noError)
1678 {
1679 if(aliasData.Flags && (!(aliasData.Flags & SPINT_REMOVED)))
1680 {
1681 PA_DEBUG(("Device %d has capture alias\n",device));
1682 hasAlias |= 2; /* Has capture alias */
1683 }
1684 }
1685 if(!hasAlias)
1686 continue; /* This was not a valid capture or render audio device */
1687
1688 noError = SetupDiGetDeviceInterfaceDetail(handle,&interfaceData,devInterfaceDetails,sizeInterface,NULL,&devInfoData);
1689 if( noError )
1690 {
1691 /* Try to get the "friendly name" for this interface */
1692 sizeFriendlyName = sizeof(friendlyName);
1693 /* Fix contributed by Ben Allison
1694 * Removed KEY_SET_VALUE from flags on following call
1695 * as its causes failure when running without admin rights
1696 * and it was not required */
1697 hkey=SetupDiOpenDeviceInterfaceRegKey(handle,&interfaceData,0,KEY_QUERY_VALUE);
1698 if(hkey!=INVALID_HANDLE_VALUE)
1699 {
1700 noError = RegQueryValueEx(hkey,TEXT("FriendlyName"),0,&type,(BYTE*)friendlyName,&sizeFriendlyName);
1701 if( noError == ERROR_SUCCESS )
1702 {
1703 PA_DEBUG(("Interface %d, Name: %s\n",device,friendlyName));
1704 RegCloseKey(hkey);
1705 }
1706 else
1707 {
1708 friendlyName[0] = 0;
1709 }
1710 }
1711 newFilter = FilterNew(devInterfaceDetails->DevicePath,friendlyName,&result);
1712 if( result == paNoError )
1713 {
1714 PA_DEBUG(("Filter created\n"));
1715 wdmHostApi->filters[slot] = newFilter;
1716 slot++;
1717 }
1718 else
1719 {
1720 PA_DEBUG(("Filter NOT created\n"));
1721 /* As there are now less filters than we initially thought
1722 * we must reduce the count by one */
1723 wdmHostApi->filterCount--;
1724 }
1725 }
1726 }
1727
1728 /* Clean up */
1729 if(handle != NULL)
1730 SetupDiDestroyDeviceInfoList(handle);
1731
1732 return paNoError;
1733 }
1734
1735 PaError PaWinWdm_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex )
1736 {
1737 PaError result = paNoError;
1738 int i, deviceCount;
1739 PaWinWdmHostApiRepresentation *wdmHostApi;
1740 PaWinWdmDeviceInfo *deviceInfoArray;
1741 PaWinWdmFilter* pFilter;
1742 PaWinWdmDeviceInfo *wdmDeviceInfo;
1743 PaDeviceInfo *deviceInfo;
1744
1745 PA_LOGE_;
1746
1747 /*
1748 Attempt to load the KSUSER.DLL without which we cannot create pins
1749 We will unload this on termination
1750 */
1751 if(DllKsUser == NULL)
1752 {
1753 DllKsUser = LoadLibrary(TEXT("ksuser.dll"));
1754 if(DllKsUser == NULL)
1755 goto error;
1756 }
1757
1758 FunctionKsCreatePin = (KSCREATEPIN*)GetProcAddress(DllKsUser, "KsCreatePin");
1759 if(FunctionKsCreatePin == NULL)
1760 goto error;
1761
1762 wdmHostApi = (PaWinWdmHostApiRepresentation*)PaUtil_AllocateMemory( sizeof(PaWinWdmHostApiRepresentation) );
1763 if( !wdmHostApi )
1764 {
1765 result = paInsufficientMemory;
1766 goto error;
1767 }
1768
1769 wdmHostApi->allocations = PaUtil_CreateAllocationGroup();
1770 if( !wdmHostApi->allocations )
1771 {
1772 result = paInsufficientMemory;
1773 goto error;
1774 }
1775
1776 result = BuildFilterList( wdmHostApi );
1777 if( result != paNoError )
1778 {
1779 goto error;
1780 }
1781 deviceCount = wdmHostApi->filterCount;
1782
1783 *hostApi = &wdmHostApi->inheritedHostApiRep;
1784 (*hostApi)->info.structVersion = 1;
1785 (*hostApi)->info.type = paWDMKS;
1786 (*hostApi)->info.name = "Windows WDM-KS";
1787 (*hostApi)->info.defaultInputDevice = paNoDevice;
1788 (*hostApi)->info.defaultOutputDevice = paNoDevice;
1789
1790 if( deviceCount > 0 )
1791 {
1792 (*hostApi)->deviceInfos = (PaDeviceInfo**)PaUtil_GroupAllocateMemory(
1793 wdmHostApi->allocations, sizeof(PaWinWdmDeviceInfo*) * deviceCount );
1794 if( !(*hostApi)->deviceInfos )
1795 {
1796 result = paInsufficientMemory;
1797 goto error;
1798 }
1799
1800 /* allocate all device info structs in a contiguous block */
1801 deviceInfoArray = (PaWinWdmDeviceInfo*)PaUtil_GroupAllocateMemory(
1802 wdmHostApi->allocations, sizeof(PaWinWdmDeviceInfo) * deviceCount );
1803 if( !deviceInfoArray )
1804 {
1805 result = paInsufficientMemory;
1806 goto error;
1807 }
1808
1809 for( i=0; i < deviceCount; ++i )
1810 {
1811 wdmDeviceInfo = &deviceInfoArray[i];
1812 deviceInfo = &wdmDeviceInfo->inheritedDeviceInfo;
1813 pFilter = wdmHostApi->filters[i];
1814 if( pFilter == NULL )
1815 continue;
1816 wdmDeviceInfo->filter = pFilter;
1817 deviceInfo->structVersion = 2;
1818 deviceInfo->hostApi = hostApiIndex;
1819 deviceInfo->name = (char*)pFilter->friendlyName;
1820 PA_DEBUG(("Device found name: %s\n",(char*)pFilter->friendlyName));
1821 deviceInfo->maxInputChannels = pFilter->maxInputChannels;
1822 if(deviceInfo->maxInputChannels > 0)
1823 {
1824 /* Set the default input device to the first device we find with
1825 * more than zero input channels
1826 **/
1827 if((*hostApi)->info.defaultInputDevice == paNoDevice)
1828 {
1829 (*hostApi)->info.defaultInputDevice = i;
1830 }
1831 }
1832
1833 deviceInfo->maxOutputChannels = pFilter->maxOutputChannels;
1834 if(deviceInfo->maxOutputChannels > 0)
1835 {
1836 /* Set the default output device to the first device we find with
1837 * more than zero output channels
1838 **/
1839 if((*hostApi)->info.defaultOutputDevice == paNoDevice)
1840 {
1841 (*hostApi)->info.defaultOutputDevice = i;
1842 }
1843 }
1844
1845 /* These low values are not very useful because
1846 * a) The lowest latency we end up with can depend on many factors such
1847 * as the device buffer sizes/granularities, sample rate, channels and format
1848 * b) We cannot know the device buffer sizes until we try to open/use it at
1849 * a particular setting
1850 * So: we give 512x48000Hz frames as the default low input latency
1851 **/
1852 deviceInfo->defaultLowInputLatency = (512.0/48000.0);
1853 deviceInfo->defaultLowOutputLatency = (512.0/48000.0);
1854 deviceInfo->defaultHighInputLatency = (4096.0/48000.0);
1855 deviceInfo->defaultHighOutputLatency = (4096.0/48000.0);
1856 deviceInfo->defaultSampleRate = (double)(pFilter->bestSampleRate);
1857
1858 (*hostApi)->deviceInfos[i] = deviceInfo;
1859 }
1860 }
1861
1862 (*hostApi)->info.deviceCount = deviceCount;
1863
1864 (*hostApi)->Terminate = Terminate;
1865 (*hostApi)->OpenStream = OpenStream;
1866 (*hostApi)->IsFormatSupported = IsFormatSupported;
1867
1868 PaUtil_InitializeStreamInterface( &wdmHostApi->callbackStreamInterface, CloseStream, StartStream,
1869 StopStream, AbortStream, IsStreamStopped, IsStreamActive,
1870 GetStreamTime, GetStreamCpuLoad,
1871 PaUtil_DummyRead, PaUtil_DummyWrite,
1872 PaUtil_DummyGetReadAvailable, PaUtil_DummyGetWriteAvailable );
1873
1874 PaUtil_InitializeStreamInterface( &wdmHostApi->blockingStreamInterface, CloseStream, StartStream,
1875 StopStream, AbortStream, IsStreamStopped, IsStreamActive,
1876 GetStreamTime, PaUtil_DummyGetCpuLoad,
1877 ReadStream, WriteStream, GetStreamReadAvailable, GetStreamWriteAvailable );
1878
1879 PA_LOGL_;
1880 return result;
1881
1882 error:
1883 if( DllKsUser != NULL )
1884 {
1885 FreeLibrary( DllKsUser );
1886 DllKsUser = NULL;
1887 }
1888
1889 if( wdmHostApi )
1890 {
1891 PaUtil_FreeMemory( wdmHostApi->filters );
1892 if( wdmHostApi->allocations )
1893 {
1894 PaUtil_FreeAllAllocations( wdmHostApi->allocations );
1895 PaUtil_DestroyAllocationGroup( wdmHostApi->allocations );
1896 }
1897 PaUtil_FreeMemory( wdmHostApi );
1898 }
1899 PA_LOGL_;
1900 return result;
1901 }
1902
1903
1904 static void Terminate( struct PaUtilHostApiRepresentation *hostApi )
1905 {
1906 PaWinWdmHostApiRepresentation *wdmHostApi = (PaWinWdmHostApiRepresentation*)hostApi;
1907 int i;
1908 PA_LOGE_;
1909
1910 if( wdmHostApi->filters )
1911 {
1912 for( i=0; i<wdmHostApi->filterCount; i++)
1913 {
1914 if( wdmHostApi->filters[i] != NULL )
1915 {
1916 FilterFree( wdmHostApi->filters[i] );
1917 wdmHostApi->filters[i] = NULL;
1918 }
1919 }
1920 }
1921 PaUtil_FreeMemory( wdmHostApi->filters );
1922 if( wdmHostApi->allocations )
1923 {
1924 PaUtil_FreeAllAllocations( wdmHostApi->allocations );
1925 PaUtil_DestroyAllocationGroup( wdmHostApi->allocations );
1926 }
1927 PaUtil_FreeMemory( wdmHostApi );
1928 PA_LOGL_;
1929 }
1930
1931 static void FillWFEXT( WAVEFORMATEXTENSIBLE* pwfext, PaSampleFormat sampleFormat, double sampleRate, int channelCount)
1932 {
1933 PA_LOGE_;
1934 PA_DEBUG(( "sampleFormat = %lx\n" , sampleFormat ));
1935 PA_DEBUG(( "sampleRate = %f\n" , sampleRate ));
1936 PA_DEBUG(( "chanelCount = %d\n", channelCount ));
1937
1938 pwfext->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1939 pwfext->Format.nChannels = channelCount;
1940 pwfext->Format.nSamplesPerSec = (int)sampleRate;
1941 if(channelCount == 1)
1942 pwfext->dwChannelMask = KSAUDIO_SPEAKER_DIRECTOUT;
1943 else
1944 pwfext->dwChannelMask = KSAUDIO_SPEAKER_STEREO;
1945 if(sampleFormat == paFloat32)
1946 {
1947 pwfext->Format.nBlockAlign = channelCount * 4;
1948 pwfext->Format.wBitsPerSample = 32;
1949 pwfext->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
1950 pwfext->Samples.wValidBitsPerSample = 32;
1951 pwfext->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1952 }
1953 else if(sampleFormat == paInt32)
1954 {
1955 pwfext->Format.nBlockAlign = channelCount * 4;
1956 pwfext->Format.wBitsPerSample = 32;
1957 pwfext->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
1958 pwfext->Samples.wValidBitsPerSample = 32;
1959 pwfext->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1960 }
1961 else if(sampleFormat == paInt24)
1962 {
1963 pwfext->Format.nBlockAlign = channelCount * 3;
1964 pwfext->Format.wBitsPerSample = 24;
1965 pwfext->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
1966 pwfext->Samples.wValidBitsPerSample = 24;
1967 pwfext->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1968 }
1969 else if(sampleFormat == paInt16)
1970 {
1971 pwfext->Format.nBlockAlign = channelCount * 2;
1972 pwfext->Format.wBitsPerSample = 16;
1973 pwfext->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
1974 pwfext->Samples.wValidBitsPerSample = 16;
1975 pwfext->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1976 }
1977 pwfext->Format.nAvgBytesPerSec = pwfext->Format.nSamplesPerSec * pwfext->Format.nBlockAlign;
1978
1979 PA_LOGL_;
1980 }
1981
1982 static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi,
1983 const PaStreamParameters *inputParameters,
1984 const PaStreamParameters *outputParameters,
1985 double sampleRate )
1986 {
1987 int inputChannelCount, outputChannelCount;
1988 PaSampleFormat inputSampleFormat, outputSampleFormat;
1989 PaWinWdmHostApiRepresentation *wdmHostApi = (PaWinWdmHostApiRepresentation*)hostApi;
1990 PaWinWdmFilter* pFilter;
1991 int result = paFormatIsSupported;
1992 WAVEFORMATEXTENSIBLE wfx;
1993
1994 PA_LOGE_;
1995
1996 if( inputParameters )
1997 {
1998 inputChannelCount = inputParameters->channelCount;
1999 inputSampleFormat = inputParameters->sampleFormat;
2000
2001 /* all standard sample formats are supported by the buffer adapter,
2002 this implementation doesn't support any custom sample formats */
2003 if( inputSampleFormat & paCustomFormat )
2004 return paSampleFormatNotSupported;
2005
2006 /* unless alternate device specification is supported, reject the use of
2007 paUseHostApiSpecificDeviceSpecification */
2008
2009 if( inputParameters->device == paUseHostApiSpecificDeviceSpecification )
2010 return paInvalidDevice;
2011
2012 /* check that input device can support inputChannelCount */
2013 if( inputChannelCount > hostApi->deviceInfos[ inputParameters->device ]->maxInputChannels )
2014 return paInvalidChannelCount;
2015
2016 /* validate inputStreamInfo */
2017 if( inputParameters->hostApiSpecificStreamInfo )
2018 return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */
2019
2020 /* Check that the input format is supported */
2021 FillWFEXT(&wfx,paInt16,sampleRate,inputChannelCount);
2022
2023 pFilter = wdmHostApi->filters[inputParameters->device];
2024 result = FilterCanCreateCapturePin(pFilter,(const WAVEFORMATEX*)&wfx);
2025 if( result != paNoError )
2026 {
2027 /* Try a WAVE_FORMAT_PCM instead */
2028 wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
2029 wfx.Format.cbSize = 0;
2030 wfx.Samples.wValidBitsPerSample = 0;
2031 wfx.dwChannelMask = 0;
2032 wfx.SubFormat = GUID_NULL;
2033 result = FilterCanCreateCapturePin(pFilter,(const WAVEFORMATEX*)&wfx);
2034 if( result != paNoError )
2035 return result;
2036 }
2037 }
2038 else
2039 {
2040 inputChannelCount = 0;
2041 }
2042
2043 if( outputParameters )
2044 {
2045 outputChannelCount = outputParameters->channelCount;
2046 outputSampleFormat = outputParameters->sampleFormat;
2047
2048 /* all standard sample formats are supported by the buffer adapter,
2049 this implementation doesn't support any custom sample formats */
2050 if( outputSampleFormat & paCustomFormat )
2051 return paSampleFormatNotSupported;
2052
2053 /* unless alternate device specification is supported, reject the use of
2054 paUseHostApiSpecificDeviceSpecification */
2055
2056 if( outputParameters->device == paUseHostApiSpecificDeviceSpecification )
2057 return paInvalidDevice;
2058
2059 /* check that output device can support outputChannelCount */
2060 if( outputChannelCount > hostApi->deviceInfos[ outputParameters->device ]->maxOutputChannels )
2061 return paInvalidChannelCount;
2062
2063 /* validate outputStreamInfo */
2064 if( outputParameters->hostApiSpecificStreamInfo )
2065 return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */
2066
2067 /* Check that the output format is supported */
2068 FillWFEXT(&wfx,paInt16,sampleRate,outputChannelCount);
2069
2070 pFilter = wdmHostApi->filters[outputParameters->device];
2071 result = FilterCanCreateRenderPin(pFilter,(const WAVEFORMATEX*)&wfx);
2072 if( result != paNoError )
2073 {
2074 /* Try a WAVE_FORMAT_PCM instead */
2075 wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
2076 wfx.Format.cbSize = 0;
2077 wfx.Samples.wValidBitsPerSample = 0;
2078 wfx.dwChannelMask = 0;
2079 wfx.SubFormat = GUID_NULL;
2080 result = FilterCanCreateRenderPin(pFilter,(const WAVEFORMATEX*)&wfx);
2081 if( result != paNoError )
2082 return result;
2083 }
2084
2085 }
2086 else
2087 {
2088 outputChannelCount = 0;
2089 }
2090
2091 /*
2092 IMPLEMENT ME:
2093
2094 - if a full duplex stream is requested, check that the combination
2095 of input and output parameters is supported if necessary
2096
2097 - check that the device supports sampleRate
2098
2099 Because the buffer adapter handles conversion between all standard
2100 sample formats, the following checks are only required if paCustomFormat
2101 is implemented, or under some other unusual conditions.
2102
2103 - check that input device can support inputSampleFormat, or that
2104 we have the capability to convert from inputSampleFormat to
2105 a native format
2106
2107 - check that output device can support outputSampleFormat, or that
2108 we have the capability to convert from outputSampleFormat to
2109 a native format
2110 */
2111 if((inputChannelCount == 0)&&(outputChannelCount == 0))
2112 result = paSampleFormatNotSupported; /* Not right error */
2113
2114 PA_LOGL_;
2115 return result;
2116 }
2117
2118 /* see pa_hostapi.h for a list of validity guarantees made about OpenStream parameters */
2119
2120 static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
2121 PaStream** s,
2122 const PaStreamParameters *inputParameters,
2123 const PaStreamParameters *outputParameters,
2124 double sampleRate,
2125 unsigned long framesPerBuffer,
2126 PaStreamFlags streamFlags,
2127 PaStreamCallback *streamCallback,
2128 void *userData )
2129 {
2130 PaError result = paNoError;
2131 PaWinWdmHostApiRepresentation *wdmHostApi = (PaWinWdmHostApiRepresentation*)hostApi;
2132 PaWinWdmStream *stream = 0;
2133 /* unsigned long framesPerHostBuffer; these may not be equivalent for all implementations */
2134 PaSampleFormat inputSampleFormat, outputSampleFormat;
2135 PaSampleFormat hostInputSampleFormat, hostOutputSampleFormat;
2136 int userInputChannels,userOutputChannels;
2137 int size;
2138 PaWinWdmFilter* pFilter;
2139 WAVEFORMATEXTENSIBLE wfx;
2140
2141 PA_LOGE_;
2142 PA_DEBUG(("OpenStream:sampleRate = %f\n",sampleRate));
2143 PA_DEBUG(("OpenStream:framesPerBuffer = %lu\n",framesPerBuffer));
2144
2145 if( inputParameters )
2146 {
2147 userInputChannels = inputParameters->channelCount;
2148 inputSampleFormat = inputParameters->sampleFormat;
2149
2150 /* unless alternate device specification is supported, reject the use of
2151 paUseHostApiSpecificDeviceSpecification */
2152
2153 if( inputParameters->device == paUseHostApiSpecificDeviceSpecification )
2154 return paInvalidDevice;
2155
2156 /* check that input device can support stream->userInputChannels */
2157 if( userInputChannels > hostApi->deviceInfos[ inputParameters->device ]->maxInputChannels )
2158 return paInvalidChannelCount;
2159
2160 /* validate inputStreamInfo */
2161 if( inputParameters->hostApiSpecificStreamInfo )
2162 return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */
2163
2164 }
2165 else
2166 {
2167 userInputChannels = 0;
2168 inputSampleFormat = hostInputSampleFormat = paInt16; /* Surpress 'uninitialised var' warnings. */
2169 }
2170
2171 if( outputParameters )
2172 {
2173 userOutputChannels = outputParameters->channelCount;
2174 outputSampleFormat = outputParameters->sampleFormat;
2175
2176 /* unless alternate device specification is supported, reject the use of
2177 paUseHostApiSpecificDeviceSpecification */
2178
2179 if( outputParameters->device == paUseHostApiSpecificDeviceSpecification )
2180 return paInvalidDevice;
2181
2182 /* check that output device can support stream->userInputChannels */
2183 if( userOutputChannels > hostApi->deviceInfos[ outputParameters->device ]->maxOutputChannels )
2184 return paInvalidChannelCount;
2185
2186 /* validate outputStreamInfo */
2187 if( outputParameters->hostApiSpecificStreamInfo )
2188 return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */
2189
2190 }
2191 else
2192 {
2193 userOutputChannels = 0;
2194 outputSampleFormat = hostOutputSampleFormat = paInt16; /* Surpress 'uninitialized var' warnings. */
2195 }
2196
2197 /* validate platform specific flags */
2198 if( (streamFlags & paPlatformSpecificFlags) != 0 )
2199 return paInvalidFlag; /* unexpected platform specific flag */
2200
2201 stream = (PaWinWdmStream*)PaUtil_AllocateMemory( sizeof(PaWinWdmStream) );
2202 if( !stream )
2203 {
2204 result = paInsufficientMemory;
2205 goto error;
2206 }
2207 /* Zero the stream object */
2208 /* memset((void*)stream,0,sizeof(PaWinWdmStream)); */
2209
2210 if( streamCallback )
2211 {
2212 PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation,
2213 &wdmHostApi->callbackStreamInterface, streamCallback, userData );
2214 }
2215 else
2216 {
2217 PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation,
2218 &wdmHostApi->blockingStreamInterface, streamCallback, userData );
2219 }
2220
2221 PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, sampleRate );
2222
2223 /* Instantiate the input pin if necessary */
2224 if(userInputChannels > 0)
2225 {
2226 result = paSampleFormatNotSupported;
2227 pFilter = wdmHostApi->filters[inputParameters->device];
2228 stream->userInputChannels = userInputChannels;
2229
2230 if(((inputSampleFormat & ~paNonInterleaved) & pFilter->formats) != 0)
2231 { /* inputSampleFormat is supported, so try to use it */
2232 hostInputSampleFormat = inputSampleFormat;
2233 FillWFEXT(&wfx, hostInputSampleFormat, sampleRate, stream->userInputChannels);
2234 stream->bytesPerInputFrame = wfx.Format.nBlockAlign;
2235 stream->recordingPin = FilterCreateCapturePin(pFilter, (const WAVEFORMATEX*)&wfx, &result);
2236 stream->deviceInputChannels = stream->userInputChannels;
2237 }
2238
2239 if(result != paNoError)
2240 { /* Search through all PaSampleFormats to find one that works */
2241 hostInputSampleFormat = paFloat32;
2242
2243 do {
2244 FillWFEXT(&wfx, hostInputSampleFormat, sampleRate, stream->userInputChannels);
2245 stream->bytesPerInputFrame = wfx.Format.nBlockAlign;
2246 stream->recordingPin = FilterCreateCapturePin(pFilter, (const WAVEFORMATEX*)&wfx, &result);
2247 stream->deviceInputChannels = stream->userInputChannels;
2248
2249 if(stream->recordingPin == NULL) result = paSampleFormatNotSupported;
2250 if(result != paNoError) hostInputSampleFormat <<= 1;
2251 }
2252 while(result != paNoError && hostInputSampleFormat <= paUInt8);
2253 }
2254
2255 if(result != paNoError)
2256 { /* None of the PaSampleFormats worked. Set the hostInputSampleFormat to the best fit
2257 * and try a PCM format.
2258 **/
2259 hostInputSampleFormat =
2260 PaUtil_SelectClosestAvailableFormat( pFilter->formats, inputSampleFormat );
2261
2262 /* Try a WAVE_FORMAT_PCM instead */
2263 wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
2264 wfx.Format.cbSize = 0;
2265 wfx.Samples.wValidBitsPerSample = 0;
2266 wfx.dwChannelMask = 0;
2267 wfx.SubFormat = GUID_NULL;
2268 stream->recordingPin = FilterCreateCapturePin(pFilter,(const WAVEFORMATEX*)&wfx,&result);
2269 if(stream->recordingPin == NULL) result = paSampleFormatNotSupported;
2270 }
2271
2272 if( result != paNoError )
2273 {
2274 /* Some or all KS devices can only handle the exact number of channels
2275 * they specify. But PortAudio clients expect to be able to
2276 * at least specify mono I/O on a multi-channel device
2277 * If this is the case, then we will do the channel mapping internally
2278 **/
2279 if( stream->userInputChannels < pFilter->maxInputChannels )
2280 {
2281 FillWFEXT(&wfx,hostInputSampleFormat,sampleRate,pFilter->maxInputChannels);
2282 stream->bytesPerInputFrame = wfx.Format.nBlockAlign;
2283 stream->recordingPin = FilterCreateCapturePin(pFilter,(const WAVEFORMATEX*)&wfx,&result);
2284 stream->deviceInputChannels = pFilter->maxInputChannels;
2285
2286 if( result != paNoError )
2287 {
2288 /* Try a WAVE_FORMAT_PCM instead */
2289 wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
2290 wfx.Format.cbSize = 0;
2291 wfx.Samples.wValidBitsPerSample = 0;
2292 wfx.dwChannelMask = 0;
2293 wfx.SubFormat = GUID_NULL;
2294 stream->recordingPin = FilterCreateCapturePin(pFilter,(const WAVEFORMATEX*)&wfx,&result);
2295 }
2296 }
2297 }
2298
2299 if(stream->recordingPin == NULL)
2300 {
2301 goto error;
2302 }
2303
2304 switch(hostInputSampleFormat)
2305 {
2306 case paInt16: stream->inputSampleSize = 2; break;
2307 case paInt24: stream->inputSampleSize = 3; break;
2308 case paInt32:
2309 case paFloat32: stream->inputSampleSize = 4; break;
2310 }
2311
2312 stream->recordingPin->frameSize /= stream->bytesPerInputFrame;
2313 PA_DEBUG(("Pin output frames: %d\n",stream->recordingPin->frameSize));
2314 }
2315 else
2316 {
2317 stream->recordingPin = NULL;
2318 stream->bytesPerInputFrame = 0;
2319 }
2320
2321 /* Instantiate the output pin if necessary */
2322 if(userOutputChannels > 0)
2323 {
2324 result = paSampleFormatNotSupported;
2325 pFilter = wdmHostApi->filters[outputParameters->device];
2326 stream->userOutputChannels = userOutputChannels;
2327
2328 if(((outputSampleFormat & ~paNonInterleaved) & pFilter->formats) != 0)
2329 {
2330 hostOutputSampleFormat = outputSampleFormat;
2331 FillWFEXT(&wfx,hostOutputSampleFormat,sampleRate,stream->userOutputChannels);
2332 stream->bytesPerOutputFrame = wfx.Format.nBlockAlign;
2333 stream->playbackPin = FilterCreateRenderPin(pFilter,(WAVEFORMATEX*)&wfx,&result);
2334 stream->deviceOutputChannels = stream->userOutputChannels;
2335 }
2336
2337 if(result != paNoError)
2338 {
2339 hostOutputSampleFormat = paFloat32;
2340
2341 do {
2342 FillWFEXT(&wfx,hostOutputSampleFormat,sampleRate,stream->userOutputChannels);
2343 stream->bytesPerOutputFrame = wfx.Format.nBlockAlign;
2344 stream->playbackPin = FilterCreateRenderPin(pFilter,(WAVEFORMATEX*)&wfx,&result);
2345 stream->deviceOutputChannels = stream->userOutputChannels;
2346
2347 if(stream->playbackPin == NULL) result = paSampleFormatNotSupported;
2348 if(result != paNoError) hostOutputSampleFormat <<= 1;
2349 }
2350 while(result != paNoError && hostOutputSampleFormat <= paUInt8);
2351 }
2352
2353 if(result != paNoError)
2354 {
2355 hostOutputSampleFormat =
2356 PaUtil_SelectClosestAvailableFormat( pFilter->formats, outputSampleFormat );
2357
2358 /* Try a WAVE_FORMAT_PCM instead */
2359 wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
2360 wfx.Format.cbSize = 0;
2361 wfx.Samples.wValidBitsPerSample = 0;
2362 wfx.dwChannelMask = 0;
2363 wfx.SubFormat = GUID_NULL;
2364 stream->playbackPin = FilterCreateRenderPin(pFilter,(WAVEFORMATEX*)&wfx,&result);
2365 if(stream->playbackPin == NULL) result = paSampleFormatNotSupported;
2366 }
2367
2368 if( result != paNoError )
2369 {
2370 /* Some or all KS devices can only handle the exact number of channels
2371 * they specify. But PortAudio clients expect to be able to
2372 * at least specify mono I/O on a multi-channel device
2373 * If this is the case, then we will do the channel mapping internally
2374 **/
2375 if( stream->userOutputChannels < pFilter->maxOutputChannels )
2376 {
2377 FillWFEXT(&wfx,hostOutputSampleFormat,sampleRate,pFilter->maxOutputChannels);
2378 stream->bytesPerOutputFrame = wfx.Format.nBlockAlign;
2379 stream->playbackPin = FilterCreateRenderPin(pFilter,(const WAVEFORMATEX*)&wfx,&result);
2380 stream->deviceOutputChannels = pFilter->maxOutputChannels;
2381 if( result != paNoError )
2382 {
2383 /* Try a WAVE_FORMAT_PCM instead */
2384 wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
2385 wfx.Format.cbSize = 0;
2386 wfx.Samples.wValidBitsPerSample = 0;
2387 wfx.dwChannelMask = 0;
2388 wfx.SubFormat = GUID_NULL;
2389 stream->playbackPin = FilterCreateRenderPin(pFilter,(const WAVEFORMATEX*)&wfx,&result);
2390 }
2391 }
2392 }
2393
2394 if(stream->playbackPin == NULL)
2395 {
2396 goto error;
2397 }
2398
2399 switch(hostOutputSampleFormat)
2400 {
2401 case paInt16: stream->outputSampleSize = 2; break;
2402 case paInt24: stream->outputSampleSize = 3; break;
2403 case paInt32:
2404 case paFloat32: stream->outputSampleSize = 4; break;
2405 }
2406
2407 stream->playbackPin->frameSize /= stream->bytesPerOutputFrame;
2408 PA_DEBUG(("Pin output frames: %d\n",stream->playbackPin->frameSize));
2409 }
2410 else
2411 {
2412 stream->playbackPin = NULL;
2413 stream->bytesPerOutputFrame = 0;
2414 }
2415
2416 /* Calculate the framesPerHostXxxxBuffer size based upon the suggested latency values */
2417
2418 /* Record the buffer length */
2419 if(inputParameters)
2420 {
2421 /* Calculate the frames from the user's value - add a bit to round up */
2422 stream->framesPerHostIBuffer = (unsigned long)((inputParameters->suggestedLatency*sampleRate)+0.0001);
2423 if(stream->framesPerHostIBuffer > (unsigned long)sampleRate)
2424 { /* Upper limit is 1 second */
2425 stream->framesPerHostIBuffer = (unsigned long)sampleRate;
2426 }
2427 else if(stream->framesPerHostIBuffer < stream->recordingPin->frameSize)
2428 {
2429 stream->framesPerHostIBuffer = stream->recordingPin->frameSize;
2430 }
2431 PA_DEBUG(("Input frames chosen:%ld\n",stream->framesPerHostIBuffer));
2432 }
2433
2434 if(outputParameters)
2435 {
2436 /* Calculate the frames from the user's value - add a bit to round up */
2437 stream->framesPerHostOBuffer = (unsigned long)((outputParameters->suggestedLatency*sampleRate)+0.0001);
2438 if(stream->framesPerHostOBuffer > (unsigned long)sampleRate)
2439 { /* Upper limit is 1 second */
2440 stream->framesPerHostOBuffer = (unsigned long)sampleRate;
2441 }
2442 else if(stream->framesPerHostOBuffer < stream->playbackPin->frameSize)
2443 {
2444 stream->framesPerHostOBuffer = stream->playbackPin->frameSize;
2445 }
2446 PA_DEBUG(("Output frames chosen:%ld\n",stream->framesPerHostOBuffer));
2447 }
2448
2449 /* Host buffer size is bounded to the largest of the input and output
2450 frame sizes */
2451
2452 result = PaUtil_InitializeBufferProcessor( &stream->bufferProcessor,
2453 stream->userInputChannels, inputSampleFormat, hostInputSampleFormat,
2454 stream->userOutputChannels, outputSampleFormat, hostOutputSampleFormat,
2455 sampleRate, streamFlags, framesPerBuffer,
2456 max(stream->framesPerHostOBuffer,stream->framesPerHostIBuffer),
2457 paUtilBoundedHostBufferSize,
2458 streamCallback, userData );
2459 if( result != paNoError )
2460 goto error;
2461
2462 stream->streamRepresentation.streamInfo.inputLatency =
2463 ((double)stream->framesPerHostIBuffer) / sampleRate;
2464 stream->streamRepresentation.streamInfo.outputLatency =
2465 ((double)stream->framesPerHostOBuffer) / sampleRate;
2466 stream->streamRepresentation.streamInfo.sampleRate = sampleRate;
2467
2468 PA_DEBUG(("BytesPerInputFrame = %d\n",stream->bytesPerInputFrame));
2469 PA_DEBUG(("BytesPerOutputFrame = %d\n",stream->bytesPerOutputFrame));
2470
2471 /* Allocate all the buffers for host I/O */
2472 size = 2 * (stream->framesPerHostIBuffer*stream->bytesPerInputFrame + stream->framesPerHostOBuffer*stream->bytesPerOutputFrame);
2473 PA_DEBUG(("Buffer size = %d\n",size));
2474 stream->hostBuffer = (char*)PaUtil_AllocateMemory(size);
2475 PA_DEBUG(("Buffer allocated\n"));
2476 if( !stream->hostBuffer )
2477 {
2478 PA_DEBUG(("Cannot allocate host buffer!\n"));
2479 result = paInsufficientMemory;
2480 goto error;
2481 }
2482 PA_DEBUG(("Buffer start = %p\n",stream->hostBuffer));
2483 /* memset(stream->hostBuffer,0,size); */
2484
2485 /* Set up the packets */
2486 stream->events[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
2487 ResetEvent(stream->events[0]); /* Record buffer 1 */
2488 stream->events[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
2489 ResetEvent(stream->events[1]); /* Record buffer 2 */
2490 stream->events[2] = CreateEvent(NULL, FALSE, FALSE, NULL);
2491 ResetEvent(stream->events[2]); /* Play buffer 1 */
2492 stream->events[3] = CreateEvent(NULL, FALSE, FALSE, NULL);
2493 ResetEvent(stream->events[3]); /* Play buffer 2 */
2494 stream->events[4] = CreateEvent(NULL, FALSE, FALSE, NULL);
2495 ResetEvent(stream->events[4]); /* Abort event */
2496 if(stream->userInputChannels > 0)
2497 {
2498 DATAPACKET *p = &(stream->packets[0]);
2499 p->Signal.hEvent = stream->events[0];
2500 p->Header.Data = stream->hostBuffer;
2501 p->Header.FrameExtent = stream->framesPerHostIBuffer*stream->bytesPerInputFrame;
2502 p->Header.DataUsed = 0;
2503 p->Header.Size = sizeof(p->Header);
2504 p->Header.PresentationTime.Numerator = 1;
2505 p->Header.PresentationTime.Denominator = 1;
2506
2507 p = &(stream->packets[1]);
2508 p->Signal.hEvent = stream->events[1];
2509 p->Header.Data = stream->hostBuffer + stream->framesPerHostIBuffer*stream->bytesPerInputFrame;
2510 p->Header.FrameExtent = stream->framesPerHostIBuffer*stream->bytesPerInputFrame;
2511 p->Header.DataUsed = 0;
2512 p->Header.Size = sizeof(p->Header);
2513 p->Header.PresentationTime.Numerator = 1;
2514 p->Header.PresentationTime.Denominator = 1;
2515 }
2516 if(stream->userOutputChannels > 0)
2517 {
2518 DATAPACKET *p = &(stream->packets[2]);
2519 p->Signal.hEvent = stream->events[2];
2520 p->Header.Data = stream->hostBuffer + 2*stream->framesPerHostIBuffer*stream->bytesPerInputFrame;
2521 p->Header.FrameExtent = stream->framesPerHostOBuffer*stream->bytesPerOutputFrame;
2522 p->Header.DataUsed = stream->framesPerHostOBuffer*stream->bytesPerOutputFrame;
2523 p->Header.Size = sizeof(p->Header);
2524 p->Header.PresentationTime.Numerator = 1;
2525 p->Header.PresentationTime.Denominator = 1;
2526
2527 p = &(stream->packets[3]);
2528 p->Signal.hEvent = stream->events[3];
2529 p->Header.Data = stream->hostBuffer + 2*stream->framesPerHostIBuffer*stream->bytesPerInputFrame + stream->framesPerHostOBuffer*stream->bytesPerOutputFrame;
2530 p->Header.FrameExtent = stream->framesPerHostOBuffer*stream->bytesPerOutputFrame;
2531 p->Header.DataUsed = stream->framesPerHostOBuffer*stream->bytesPerOutputFrame;
2532 p->Header.Size = sizeof(p->Header);
2533 p->Header.PresentationTime.Numerator = 1;
2534 p->Header.PresentationTime.Denominator = 1;
2535 }
2536
2537 stream->streamStarted = 0;
2538 stream->streamActive = 0;
2539 stream->streamStop = 0;
2540 stream->streamAbort = 0;
2541 stream->streamFlags = streamFlags;
2542 stream->oldProcessPriority = REALTIME_PRIORITY_CLASS;
2543
2544 *s = (PaStream*)stream;
2545
2546 PA_LOGL_;
2547 return result;
2548
2549 error:
2550 size = 5;
2551 while(size--)
2552 {
2553 if(stream->events[size] != NULL)
2554 {
2555 CloseHandle(stream->events[size]);
2556 stream->events[size] = NULL;
2557 }
2558 }
2559 if(stream->hostBuffer)
2560 PaUtil_FreeMemory( stream->hostBuffer );
2561
2562 if(stream->playbackPin)
2563 PinClose(stream->playbackPin);
2564 if(stream->recordingPin)
2565 PinClose(stream->recordingPin);
2566
2567 if( stream )
2568 PaUtil_FreeMemory( stream );
2569
2570 PA_LOGL_;
2571 return result;
2572 }
2573
2574 /*
2575 When CloseStream() is called, the multi-api layer ensures that
2576 the stream has already been stopped or aborted.
2577 */
2578 static PaError CloseStream( PaStream* s )
2579 {
2580 PaError result = paNoError;
2581 PaWinWdmStream *stream = (PaWinWdmStream*)s;
2582 int size;
2583
2584 PA_LOGE_;
2585
2586 assert(!stream->streamStarted);
2587 assert(!stream->streamActive);
2588
2589 PaUtil_TerminateBufferProcessor( &stream->bufferProcessor );
2590 PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation );
2591 size = 5;
2592 while(size--)
2593 {
2594 if(stream->events[size] != NULL)
2595 {
2596 CloseHandle(stream->events[size]);
2597 stream->events[size] = NULL;
2598 }
2599 }
2600 if(stream->hostBuffer)
2601 PaUtil_FreeMemory( stream->hostBuffer );
2602
2603 if(stream->playbackPin)
2604 PinClose(stream->playbackPin);
2605 if(stream->recordingPin)
2606 PinClose(stream->recordingPin);
2607
2608 PaUtil_FreeMemory( stream );
2609
2610 PA_LOGL_;
2611 return result;
2612 }
2613
2614 /*
2615 Write the supplied packet to the pin
2616 Asynchronous
2617 Should return false on success
2618 */
2619 static BOOL PinWrite(HANDLE h, DATAPACKET* p)
2620 {
2621 unsigned long cbReturned = 0;
2622 return DeviceIoControl(h,IOCTL_KS_WRITE_STREAM,NULL,0,
2623 &p->Header,p->Header.Size,&cbReturned,&p->Signal);
2624 }
2625
2626 /*
2627 Read to the supplied packet from the pin
2628 Asynchronous
2629 Should return false on success
2630 */
2631 static BOOL PinRead(HANDLE h, DATAPACKET* p)
2632 {
2633 unsigned long cbReturned = 0;
2634 return DeviceIoControl(h,IOCTL_KS_READ_STREAM,NULL,0,
2635 &p->Header,p->Header.Size,&cbReturned,&p->Signal);
2636 }
2637
2638 /*
2639 Copy the first interleaved channel of 16 bit data to the other channels
2640 */
2641 static void DuplicateFirstChannelInt16(void* buffer, int channels, int samples)
2642 {
2643 unsigned short* data = (unsigned short*)buffer;
2644 int channel;
2645 unsigned short sourceSample;
2646 while( samples-- )
2647 {
2648 sourceSample = *data++;
2649 channel = channels-1;
2650 while( channel-- )
2651 {
2652 *data++ = sourceSample;
2653 }
2654 }
2655 }
2656
2657 /*
2658 Copy the first interleaved channel of 24 bit data to the other channels
2659 */
2660 static void DuplicateFirstChannelInt24(void* buffer, int channels, int samples)
2661 {
2662 unsigned char* data = (unsigned char*)buffer;
2663 int channel;
2664 unsigned char sourceSample[3];
2665 while( samples-- )
2666 {
2667 sourceSample[0] = data[0];
2668 sourceSample[1] = data[1];
2669 sourceSample[2] = data[2];
2670 data += 3;
2671 channel = channels-1;
2672 while( channel-- )
2673 {
2674 data[0] = sourceSample[0];
2675 data[1] = sourceSample[1];
2676 data[2] = sourceSample[2];
2677 data += 3;
2678 }
2679 }
2680 }
2681
2682 /*
2683 Copy the first interleaved channel of 32 bit data to the other channels
2684 */
2685 static void DuplicateFirstChannelInt32(void* buffer, int channels, int samples)
2686 {
2687 unsigned long* data = (unsigned long*)buffer;
2688 int channel;
2689 unsigned long sourceSample;
2690 while( samples-- )
2691 {
2692 sourceSample = *data++;
2693 channel = channels-1;
2694 while( channel-- )
2695 {
2696 *data++ = sourceSample;
2697 }
2698 }
2699 }
2700
2701 static DWORD WINAPI ProcessingThread(LPVOID pParam)
2702 {
2703 PaWinWdmStream *stream = (PaWinWdmStream*)pParam;
2704 PaStreamCallbackTimeInfo ti;
2705 int cbResult = paContinue;
2706 int inbuf = 0;
2707 int outbuf = 0;
2708 int pending = 0;
2709 PaError result;
2710 unsigned long wait;
2711 unsigned long eventSignaled;
2712 int fillPlaybuf = 0;
2713 int emptyRecordbuf = 0;
2714 int framesProcessed;
2715 unsigned long timeout;
2716 int i;
2717 int doChannelCopy;
2718 int priming = 0;
2719 PaStreamCallbackFlags underover = 0;
2720
2721 PA_LOGE_;
2722
2723 ti.inputBufferAdcTime = 0.0;
2724 ti.currentTime = 0.0;
2725 ti.outputBufferDacTime = 0.0;
2726
2727 /* Get double buffering going */
2728
2729 /* Submit buffers */
2730 if(stream->playbackPin)
2731 {
2732 result = PinSetState(stream->playbackPin, KSSTATE_RUN);
2733
2734 PA_DEBUG(("play state run = %d;",(int)result));
2735 SetEvent(stream->events[outbuf+2]);
2736 outbuf = (outbuf+1)&1;
2737 SetEvent(stream->events[outbuf+2]);
2738 outbuf = (outbuf+1)&1;
2739 pending += 2;
2740 priming += 4;
2741 }
2742 if(stream->recordingPin)
2743 {
2744 result = PinSetState(stream->recordingPin, KSSTATE_RUN);
2745
2746 PA_DEBUG(("recording state run = %d;",(int)result));
2747 PinRead(stream->recordingPin->handle,&stream->packets[inbuf]);
2748 inbuf = (inbuf+1)&1; /* Increment and wrap */
2749 PinRead(stream->recordingPin->handle,&stream->packets[inbuf]);
2750 inbuf = (inbuf+1)&1; /* Increment and wrap */
2751 /* FIXME - do error checking */
2752 pending += 2;
2753 }
2754 PA_DEBUG(("Out buffer len:%f\n",(2000*stream->framesPerHostOBuffer) / stream->streamRepresentation.streamInfo.sampleRate));
2755 PA_DEBUG(("In buffer len:%f\n",(2000*stream->framesPerHostIBuffer) / stream->streamRepresentation.streamInfo.sampleRate));
2756 timeout = max(
2757 ((2000*(DWORD)stream->framesPerHostOBuffer) / (DWORD)stream->streamRepresentation.streamInfo.sampleRate),
2758 ((2000*(DWORD)stream->framesPerHostIBuffer) / (DWORD)stream->streamRepresentation.streamInfo.sampleRate));
2759 timeout = max(timeout,1);
2760 PA_DEBUG(("Timeout = %ld\n",timeout));
2761
2762 while(!stream->streamAbort)
2763 {
2764 fillPlaybuf = 0;
2765 emptyRecordbuf = 0;
2766
2767 /* Wait for next input or output buffer to be finished with*/
2768 assert(pending>0);
2769
2770 if(stream->streamStop)
2771 {
2772 PA_DEBUG(("ss1:pending=%d ",pending));
2773 }
2774 wait = WaitForMultipleObjects(5, stream->events, FALSE, 0);
2775 if( wait == WAIT_TIMEOUT )
2776 {
2777 /* No (under|over)flow has ocurred */
2778 wait = WaitForMultipleObjects(5, stream->events, FALSE, timeout);
2779 eventSignaled = wait - WAIT_OBJECT_0;
2780 }
2781 else
2782 {
2783 eventSignaled = wait - WAIT_OBJECT_0;
2784 if( eventSignaled < 2 )
2785 {
2786 underover |= paInputOverflow;
2787 PA_DEBUG(("Input overflow\n"));
2788 }
2789 else if(( eventSignaled < 4 )&&(!priming))
2790 {
2791 underover |= paOutputUnderflow;
2792 PA_DEBUG(("Output underflow\n"));
2793 }
2794 }
2795
2796 if(stream->streamStop)
2797 {
2798 PA_DEBUG(("ss2:wait=%ld",wait));
2799 }
2800 if(wait == WAIT_FAILED)
2801 {
2802 PA_DEBUG(("Wait fail = %ld! ",wait));
2803 break;
2804 }
2805 if(wait == WAIT_TIMEOUT)
2806 {
2807 continue;
2808 }
2809
2810 if(eventSignaled < 2)
2811 { /* Recording input buffer has been filled */
2812 if(stream->playbackPin)
2813 {
2814 /* First check if also the next playback buffer has been signaled */
2815 wait = WaitForSingleObject(stream->events[outbuf+2],0);
2816 if(wait == WAIT_OBJECT_0)
2817 {
2818 /* Yes, so do both buffers at same time */
2819 fillPlaybuf = 1;
2820 pending--;
2821 /* Was this an underflow situation? */
2822 if( underover )
2823 underover |= paOutputUnderflow; /* Yes! */
2824 }
2825 }
2826 emptyRecordbuf = 1;
2827 pending--;
2828 }
2829 else if(eventSignaled < 4)
2830 { /* Playback output buffer has been emptied */
2831 if(stream->recordingPin)
2832 {
2833 /* First check if also the next recording buffer has been signaled */
2834 wait = WaitForSingleObject(stream->events[inbuf],0);
2835 if(wait == WAIT_OBJECT_0)
2836 { /* Yes, so do both buffers at same time */
2837 emptyRecordbuf = 1;
2838 pending--;
2839 /* Was this an overflow situation? */
2840 if( underover )
2841 underover |= paInputOverflow; /* Yes! */
2842 }
2843 }
2844 fillPlaybuf = 1;
2845 pending--;
2846 }
2847 else
2848 {
2849 /* Abort event! */
2850 assert(stream->streamAbort); /* Should have been set */
2851 PA_DEBUG(("ABORTING "));
2852 break;
2853 }
2854 ResetEvent(stream->events[eventSignaled]);
2855
2856 if(stream->streamStop)
2857 {
2858 PA_DEBUG(("Stream stop! pending=%d",pending));
2859 cbResult = paComplete; /* Stop, but play remaining buffers */
2860 }
2861
2862 /* Do necessary buffer processing (which will invoke user callback if necessary */
2863 doChannelCopy = 0;
2864 if(cbResult==paContinue)
2865 {
2866 PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer );
2867 if((stream->bufferProcessor.hostInputFrameCount[0] + stream->bufferProcessor.hostInputFrameCount[1]) ==
2868 (stream->bufferProcessor.hostOutputFrameCount[0] + stream->bufferProcessor.hostOutputFrameCount[1]) )
2869 PaUtil_BeginBufferProcessing(&stream->bufferProcessor,&ti,underover);
2870 underover = 0; /* Reset the (under|over)flow status */
2871 if(fillPlaybuf)
2872 {
2873 PaUtil_SetOutputFrameCount(&stream->bufferProcessor,0);
2874 if( stream->userOutputChannels == 1 )
2875 {
2876 /* Write the single user channel to the first interleaved block */
2877 PaUtil_SetOutputChannel(&stream->bufferProcessor,0,stream->packets[outbuf+2].Header.Data,stream->deviceOutputChannels);
2878 /* We will do a copy to the other channels after the data has been written */
2879 doChannelCopy = 1;
2880 }
2881 else
2882 {
2883 for(i=0;i<stream->userOutputChannels;i++)
2884 {
2885 /* Only write the user output channels. Leave the rest blank */
2886 PaUtil_SetOutputChannel(&stream->bufferProcessor,i,((unsigned char*)(stream->packets[outbuf+2].Header.Data))+(i*stream->outputSampleSize),stream->deviceOutputChannels);
2887 }
2888 }
2889 }
2890 if(emptyRecordbuf)
2891 {
2892 PaUtil_SetInputFrameCount(&stream->bufferProcessor,stream->packets[inbuf].Header.DataUsed/stream->bytesPerInputFrame);
2893 for(i=0;i<stream->userInputChannels;i++)
2894 {
2895 /* Only read as many channels as the user wants */
2896 PaUtil_SetInputChannel(&stream->bufferProcessor,i,((unsigned char*)(stream->packets[inbuf].Header.Data))+(i*stream->inputSampleSize),stream->deviceInputChannels);
2897 }
2898 }
2899 /* Only call the EndBufferProcessing function is the total input frames == total output frames */
2900 if((stream->bufferProcessor.hostInputFrameCount[0] + stream->bufferProcessor.hostInputFrameCount[1]) ==
2901 (stream->bufferProcessor.hostOutputFrameCount[0] + stream->bufferProcessor.hostOutputFrameCount[1]) )
2902 framesProcessed = PaUtil_EndBufferProcessing(&stream->bufferProcessor,&cbResult);
2903 else framesProcessed = 0;
2904 if( doChannelCopy )
2905 {
2906 /* Copy the first output channel to the other channels */
2907 switch(stream->outputSampleSize)
2908 {
2909 case 2:
2910 DuplicateFirstChannelInt16(stream->packets[outbuf+2].Header.Data,stream->deviceOutputChannels,stream->framesPerHostOBuffer);
2911 break;
2912 case 3:
2913 DuplicateFirstChannelInt24(stream->packets[outbuf+2].Header.Data,stream->deviceOutputChannels,stream->framesPerHostOBuffer);
2914 break;
2915 case 4:
2916 DuplicateFirstChannelInt32(stream->packets[outbuf+2].Header.Data,stream->deviceOutputChannels,stream->framesPerHostOBuffer);
2917 break;
2918 default:
2919 assert(0); /* Unsupported format! */
2920 break;
2921 }
2922 }
2923 PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesProcessed );
2924 }
2925 else
2926 {
2927 fillPlaybuf = 0;
2928 emptyRecordbuf = 0;
2929 }
2930
2931 /*
2932 if(cbResult != paContinue)
2933 {
2934 PA_DEBUG(("cbResult=%d, pending=%d:",cbResult,pending));
2935 }
2936 */
2937 /* Submit buffers */
2938 if((fillPlaybuf)&&(cbResult!=paAbort))
2939 {
2940 if(!PinWrite(stream->playbackPin->handle,&stream->packets[outbuf+2]))
2941 outbuf = (outbuf+1)&1; /* Increment and wrap */
2942 pending++;
2943 if( priming )
2944 priming--; /* Have to prime twice */
2945 }
2946 if((emptyRecordbuf)&&(cbResult==paContinue))
2947 {
2948 stream->packets[inbuf].Header.DataUsed = 0; /* Reset for reuse */
2949 PinRead(stream->recordingPin->handle,&stream->packets[inbuf]);
2950 inbuf = (inbuf+1)&1; /* Increment and wrap */
2951 pending++;
2952 }
2953 if(pending==0)
2954 {
2955 PA_DEBUG(("pending==0 finished...;"));
2956 break;
2957 }
2958 if((!stream->playbackPin)&&(cbResult!=paContinue))
2959 {
2960 PA_DEBUG(("record only cbResult=%d...;",cbResult));
2961 break;
2962 }
2963 }
2964
2965 PA_DEBUG(("Finished thread"));
2966
2967 /* Finished, either normally or aborted */
2968 if(stream->playbackPin)
2969 {
2970 result = PinSetState(stream->playbackPin, KSSTATE_PAUSE);
2971 result = PinSetState(stream->playbackPin, KSSTATE_STOP);
2972 }
2973 if(stream->recordingPin)
2974 {
2975 result = PinSetState(stream->recordingPin, KSSTATE_PAUSE);
2976 result = PinSetState(stream->recordingPin, KSSTATE_STOP);
2977 }
2978
2979 stream->streamActive = 0;
2980
2981 if((!stream->streamStop)&&(!stream->streamAbort))
2982 {
2983 /* Invoke the user stream finished callback */
2984 /* Only do it from here if not being stopped/aborted by user */
2985 if( stream->streamRepresentation.streamFinishedCallback != 0 )
2986 stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData );
2987 }
2988 stream->streamStop = 0;
2989 stream->streamAbort = 0;
2990
2991 /* Reset process priority if necessary */
2992 if(stream->oldProcessPriority != REALTIME_PRIORITY_CLASS)
2993 {
2994 SetPriorityClass(GetCurrentProcess(),stream->oldProcessPriority);
2995 stream->oldProcessPriority = REALTIME_PRIORITY_CLASS;
2996 }
2997
2998 PA_LOGL_;
2999 ExitThread(0);
3000 return 0;
3001 }
3002
3003 static PaError StartStream( PaStream *s )
3004 {
3005 PaError result = paNoError;
3006 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3007 DWORD dwID;
3008 BOOL ret;
3009 int size;
3010
3011 PA_LOGE_;
3012
3013 stream->streamStop = 0;
3014 stream->streamAbort = 0;
3015 size = 5;
3016 while(size--)
3017 {
3018 if(stream->events[size] != NULL)
3019 {
3020 ResetEvent(stream->events[size]);
3021 }
3022 }
3023
3024 PaUtil_ResetBufferProcessor( &stream->bufferProcessor );
3025
3026 stream->oldProcessPriority = GetPriorityClass(GetCurrentProcess());
3027 /* Uncomment the following line to enable dynamic boosting of the process
3028 * priority to real time for best low latency support
3029 * Disabled by default because RT processes can easily block the OS */
3030 /*ret = SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
3031 PA_DEBUG(("Class ret = %d;",ret));*/
3032
3033 stream->streamStarted = 1;
3034 stream->streamThread = CreateThread(NULL, 0, ProcessingThread, stream, 0, &dwID);
3035 if(stream->streamThread == NULL)
3036 {
3037 stream->streamStarted = 0;
3038 result = paInsufficientMemory;
3039 goto end;
3040 }
3041 ret = SetThreadPriority(stream->streamThread,THREAD_PRIORITY_TIME_CRITICAL);
3042 PA_DEBUG(("Priority ret = %d;",ret));
3043 /* Make the stream active */
3044 stream->streamActive = 1;
3045
3046 end:
3047 PA_LOGL_;
3048 return result;
3049 }
3050
3051
3052 static PaError StopStream( PaStream *s )
3053 {
3054 PaError result = paNoError;
3055 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3056 int doCb = 0;
3057
3058 PA_LOGE_;
3059
3060 if(stream->streamActive)
3061 {
3062 doCb = 1;
3063 stream->streamStop = 1;
3064 while(stream->streamActive)
3065 {
3066 PA_DEBUG(("W."));
3067 Sleep(10); /* Let thread sleep for 10 msec */
3068 }
3069 }
3070
3071 PA_DEBUG(("Terminating thread"));
3072 if(stream->streamStarted && stream->streamThread)
3073 {
3074 TerminateThread(stream->streamThread,0);
3075 stream->streamThread = NULL;
3076 }
3077
3078 stream->streamStarted = 0;
3079
3080 if(stream->oldProcessPriority != REALTIME_PRIORITY_CLASS)
3081 {
3082 SetPriorityClass(GetCurrentProcess(),stream->oldProcessPriority);
3083 stream->oldProcessPriority = REALTIME_PRIORITY_CLASS;
3084 }
3085
3086 if(doCb)
3087 {
3088 /* Do user callback now after all state has been reset */
3089 /* This means it should be safe for the called function */
3090 /* to invoke e.g. StartStream */
3091 if( stream->streamRepresentation.streamFinishedCallback != 0 )
3092 stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData );
3093 }
3094
3095 PA_LOGL_;
3096 return result;
3097 }
3098
3099 static PaError AbortStream( PaStream *s )
3100 {
3101 PaError result = paNoError;
3102 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3103 int doCb = 0;
3104
3105 PA_LOGE_;
3106
3107 if(stream->streamActive)
3108 {
3109 doCb = 1;
3110 stream->streamAbort = 1;
3111 SetEvent(stream->events[4]); /* Signal immediately */
3112 while(stream->streamActive)
3113 {
3114 Sleep(10);
3115 }
3116 }
3117
3118 if(stream->streamStarted && stream->streamThread)
3119 {
3120 TerminateThread(stream->streamThread,0);
3121 stream->streamThread = NULL;
3122 }
3123
3124 stream->streamStarted = 0;
3125
3126 if(stream->oldProcessPriority != REALTIME_PRIORITY_CLASS)
3127 {
3128 SetPriorityClass(GetCurrentProcess(),stream->oldProcessPriority);
3129 stream->oldProcessPriority = REALTIME_PRIORITY_CLASS;
3130 }
3131
3132 if(doCb)
3133 {
3134 /* Do user callback now after all state has been reset */
3135 /* This means it should be safe for the called function */
3136 /* to invoke e.g. StartStream */
3137 if( stream->streamRepresentation.streamFinishedCallback != 0 )
3138 stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData );
3139 }
3140
3141 stream->streamActive = 0;
3142 stream->streamStarted = 0;
3143
3144 PA_LOGL_;
3145 return result;
3146 }
3147
3148
3149 static PaError IsStreamStopped( PaStream *s )
3150 {
3151 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3152 int result = 0;
3153
3154 PA_LOGE_;
3155
3156 if(!stream->streamStarted)
3157 result = 1;
3158
3159 PA_LOGL_;
3160 return result;
3161 }
3162
3163
3164 static PaError IsStreamActive( PaStream *s )
3165 {
3166 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3167 int result = 0;
3168
3169 PA_LOGE_;
3170
3171 if(stream->streamActive)
3172 result = 1;
3173
3174 PA_LOGL_;
3175 return result;
3176 }
3177
3178
3179 static PaTime GetStreamTime( PaStream* s )
3180 {
3181 PA_LOGE_;
3182 PA_LOGL_;
3183 (void)s;
3184 return PaUtil_GetTime();
3185 }
3186
3187
3188 static double GetStreamCpuLoad( PaStream* s )
3189 {
3190 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3191 double result;
3192 PA_LOGE_;
3193 result = PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer );
3194 PA_LOGL_;
3195 return result;
3196 }
3197
3198
3199 /*
3200 As separate stream interfaces are used for blocking and callback
3201 streams, the following functions can be guaranteed to only be called
3202 for blocking streams.
3203 */
3204
3205 static PaError ReadStream( PaStream* s,
3206 void *buffer,
3207 unsigned long frames )
3208 {
3209 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3210
3211 PA_LOGE_;
3212
3213 /* suppress unused variable warnings */
3214 (void) buffer;
3215 (void) frames;
3216 (void) stream;
3217
3218 /* IMPLEMENT ME, see portaudio.h for required behavior*/
3219 PA_LOGL_;
3220 return paNoError;
3221 }
3222
3223
3224 static PaError WriteStream( PaStream* s,
3225 const void *buffer,
3226 unsigned long frames )
3227 {
3228 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3229
3230 PA_LOGE_;
3231
3232 /* suppress unused variable warnings */
3233 (void) buffer;
3234 (void) frames;
3235 (void) stream;
3236
3237 /* IMPLEMENT ME, see portaudio.h for required behavior*/
3238 PA_LOGL_;
3239 return paNoError;
3240 }
3241
3242
3243 static signed long GetStreamReadAvailable( PaStream* s )
3244 {
3245 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3246
3247 PA_LOGE_;
3248
3249 /* suppress unused variable warnings */
3250 (void) stream;
3251
3252 /* IMPLEMENT ME, see portaudio.h for required behavior*/
3253 PA_LOGL_;
3254 return 0;
3255 }
3256
3257
3258 static signed long GetStreamWriteAvailable( PaStream* s )
3259 {
3260 PaWinWdmStream *stream = (PaWinWdmStream*)s;
3261
3262 PA_LOGE_;
3263 /* suppress unused variable warnings */
3264 (void) stream;
3265
3266 /* IMPLEMENT ME, see portaudio.h for required behavior*/
3267 PA_LOGL_;
3268 return 0;
3269 }