-
Notifications
You must be signed in to change notification settings - Fork 315
/
pa_win_wdmks.c
6812 lines (5893 loc) · 235 KB
/
pa_win_wdmks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
* PortAudio Windows WDM-KS interface
*
* Author: Andrew Baldwin, Robert Bielik (WaveRT)
* Based on the Open Source API proposed by Ross Bencina
* Copyright (c) 1999-2004 Andrew Baldwin, Ross Bencina, Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
/** @file
@ingroup hostapi_src
@brief Portaudio WDM-KS host API.
@note This is the implementation of the Portaudio host API using the
Windows WDM/Kernel Streaming API in order to enable very low latency
playback and recording on all modern Windows platforms (e.g. 2K, XP, Vista, Win7)
Note: This API accesses the device drivers below the usual KMIXER
component which is normally used to enable multi-client mixing and
format conversion. That means that it will lock out all other users
of a device for the duration of active stream using those devices
*/
#include <stdio.h>
#if (defined(_WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) /* MSC version 6 and above */
#pragma comment( lib, "setupapi.lib" )
#endif
/* Debugging/tracing support */
#define PA_LOGE_
#define PA_LOGL_
#ifdef __GNUC__
#include <initguid.h>
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#endif
#include <string.h> /* strlen() */
#include <assert.h>
#include <wchar.h> /* iswspace() */
#include "pa_util.h"
#include "pa_allocation.h"
#include "pa_hostapi.h"
#include "pa_stream.h"
#include "pa_cpuload.h"
#include "pa_process.h"
#include "portaudio.h"
#include "pa_debugprint.h"
#include "pa_memorybarrier.h"
#include "pa_ringbuffer.h"
#include "pa_trace.h"
#include "pa_win_waveformat.h"
#include "pa_win_version.h"
#include "pa_win_wdmks.h"
#ifndef DRV_QUERYDEVICEINTERFACE
#define DRV_QUERYDEVICEINTERFACE (DRV_RESERVED + 12)
#endif
#ifndef DRV_QUERYDEVICEINTERFACESIZE
#define DRV_QUERYDEVICEINTERFACESIZE (DRV_RESERVED + 13)
#endif
#include <windows.h>
#ifndef __GNUC__ /* Fix for ticket #257: MinGW-w64: Inclusion of <winioctl.h> triggers multiple redefinition errors. */
#include <winioctl.h>
#endif
#include <process.h>
#include <math.h>
#ifdef _MSC_VER
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#endif
/* The PA_HP_TRACE macro is used in RT parts, so it can be switched off without affecting
the rest of the debug tracing */
#if 1
#define PA_HP_TRACE(x) PaUtil_AddHighSpeedLogMessage x ;
#else
#define PA_HP_TRACE(x)
#endif
/* A define that selects whether the resulting pin names are chosen from pin category
instead of the available pin names, who sometimes can be quite cheesy, like "Volume control".
Default is to use the pin category.
*/
#ifndef PA_WDMKS_USE_CATEGORY_FOR_PIN_NAMES
#define PA_WDMKS_USE_CATEGORY_FOR_PIN_NAMES 1
#endif
#ifdef __GNUC__
#undef PA_LOGE_
#define PA_LOGE_ PA_DEBUG(("%s {\n",__FUNCTION__))
#undef PA_LOGL_
#define PA_LOGL_ PA_DEBUG(("} %s\n",__FUNCTION__))
/* These defines are set in order to allow the WIndows DirectX
* headers to compile with a GCC compiler such as MinGW
* NOTE: The headers may generate a few warning in GCC, but
* they should compile */
#define _INC_MMSYSTEM
#define _INC_MMREG
#define _NTRTL_ /* Turn off default definition of DEFINE_GUIDEX */
#define DEFINE_GUID_THUNK(name,guid) DEFINE_GUID(name,guid)
#define DEFINE_GUIDEX(n) DEFINE_GUID_THUNK( n, STATIC_##n )
#if !defined( DEFINE_WAVEFORMATEX_GUID )
#define DEFINE_WAVEFORMATEX_GUID(x) (USHORT)(x), 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
#endif
#define WAVE_FORMAT_ADPCM 0x0002
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
#define WAVE_FORMAT_ALAW 0x0006
#define WAVE_FORMAT_MULAW 0x0007
#define WAVE_FORMAT_MPEG 0x0050
#define WAVE_FORMAT_DRM 0x0009
#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}}
#define DYNAMIC_GUID(data) DYNAMIC_GUID_THUNK(data)
#endif
/* use CreateThread for CYGWIN/Windows Mobile, _beginthreadex for all others */
#if !defined(__CYGWIN__) && !defined(_WIN32_WCE)
#define CREATE_THREAD_FUNCTION (HANDLE)_beginthreadex
#define PA_THREAD_FUNC static unsigned WINAPI
#else
#define CREATE_THREAD_FUNCTION CreateThread
#define PA_THREAD_FUNC static DWORD WINAPI
#endif
#ifdef _MSC_VER
#define NOMMIDS
#define DYNAMIC_GUID(data) {data}
#define _NTRTL_ /* Turn off default definition of DEFINE_GUIDEX */
#undef DEFINE_GUID
#ifdef DECLSPEC_SELECTANY
#define PA_DECLSPEC_SELECTANY DECLSPEC_SELECTANY
#else
#define PA_DECLSPEC_SELECTANY
#endif
#if defined(__clang__) || (defined(_MSVC_TRADITIONAL) && !_MSVC_TRADITIONAL) /* clang-cl and new msvc preprocessor: avoid too many arguments error */
#define DEFINE_GUID(n, ...) EXTERN_C const GUID PA_DECLSPEC_SELECTANY n = {__VA_ARGS__}
#define DEFINE_GUID_THUNK(n, ...) DEFINE_GUID(n, __VA_ARGS__)
#define DEFINE_GUIDEX(n) DEFINE_GUID_THUNK(n, STATIC_##n)
#else
#define DEFINE_GUID(n, data) EXTERN_C const GUID PA_DECLSPEC_SELECTANY n = {data}
#define DEFINE_GUID_THUNK(n, data) DEFINE_GUID(n, data)
#define DEFINE_GUIDEX(n) DEFINE_GUID_THUNK(n, STATIC_##n)
#endif /* __clang__, !_MSVC_TRADITIONAL */
#endif
#include <setupapi.h>
#ifndef EXTERN_C
#define EXTERN_C extern
#endif
#if defined(__GNUC__)
/* For MinGW we reference mingw-include files supplied with WASAPI */
#define WINBOOL BOOL
#include "../wasapi/mingw-include/ks.h"
#include "../wasapi/mingw-include/ksmedia.h"
#else
#include <mmreg.h>
#include <ks.h>
/* Note that Windows SDK V6.0A or later is needed for WaveRT specific structs to be present in
ksmedia.h. Also make sure that the SDK include path is before other include paths (that may contain
an "old" ksmedia.h), so the proper ksmedia.h is used */
#include <ksmedia.h>
#endif
#include <assert.h>
#include <stdio.h>
/* These next definitions allow the use of the KSUSER DLL */
typedef /*KSDDKAPI*/ DWORD WINAPI KSCREATEPIN(HANDLE, PKSPIN_CONNECT, ACCESS_MASK, PHANDLE);
extern HMODULE DllKsUser;
extern KSCREATEPIN* FunctionKsCreatePin;
/* These definitions allows the use of AVRT.DLL on Vista and later OSs */
typedef enum _PA_AVRT_PRIORITY
{
PA_AVRT_PRIORITY_LOW = -1,
PA_AVRT_PRIORITY_NORMAL,
PA_AVRT_PRIORITY_HIGH,
PA_AVRT_PRIORITY_CRITICAL
} PA_AVRT_PRIORITY, *PPA_AVRT_PRIORITY;
typedef struct
{
HINSTANCE hInstance;
HANDLE (WINAPI *AvSetMmThreadCharacteristics) (LPCSTR, LPDWORD);
BOOL (WINAPI *AvRevertMmThreadCharacteristics) (HANDLE);
BOOL (WINAPI *AvSetMmThreadPriority) (HANDLE, PA_AVRT_PRIORITY);
} PaWinWDMKSAvRtEntryPoints;
static PaWinWDMKSAvRtEntryPoints paWinWDMKSAvRtEntryPoints = {0};
/* An unspecified channel count (-1) is not treated correctly, so we replace it with
* an arbitrarily large number */
#define MAXIMUM_NUMBER_OF_CHANNELS 256
/* Forward definition to break circular type reference between pin and filter */
struct __PaWinWdmFilter;
typedef struct __PaWinWdmFilter PaWinWdmFilter;
struct __PaWinWdmPin;
typedef struct __PaWinWdmPin PaWinWdmPin;
struct __PaWinWdmStream;
typedef struct __PaWinWdmStream PaWinWdmStream;
/* Function prototype for getting audio position */
typedef PaError (*FunctionGetPinAudioPosition)(PaWinWdmPin*, unsigned long*);
/* Function prototype for memory barrier */
typedef void (*FunctionMemoryBarrier)(void);
struct __PaProcessThreadInfo;
typedef struct __PaProcessThreadInfo PaProcessThreadInfo;
typedef PaError (*FunctionPinHandler)(PaProcessThreadInfo* pInfo, unsigned eventIndex);
typedef enum __PaStreamStartEnum
{
StreamStart_kOk,
StreamStart_kFailed,
StreamStart_kCnt
} PaStreamStartEnum;
/* Multiplexed input structure.
* Very often several physical inputs are multiplexed through a MUX node (represented in the topology filter) */
typedef struct __PaWinWdmMuxedInput
{
wchar_t friendlyName[MAX_PATH];
ULONG muxPinId;
ULONG muxNodeId;
ULONG endpointPinId;
} PaWinWdmMuxedInput;
/* The Pin structure
* A pin is an input or output node, e.g. for audio flow */
struct __PaWinWdmPin
{
HANDLE handle;
PaWinWdmMuxedInput** inputs;
unsigned inputCount;
wchar_t friendlyName[MAX_PATH];
PaWinWdmFilter* parentFilter;
PaWDMKSSubType pinKsSubType;
unsigned long pinId;
unsigned long endpointPinId; /* For output pins */
KSPIN_CONNECT* pinConnect;
unsigned long pinConnectSize;
KSDATAFORMAT_WAVEFORMATEX* ksDataFormatWfx;
KSPIN_COMMUNICATION communication;
KSDATARANGE* dataRanges;
KSMULTIPLE_ITEM* dataRangesItem;
KSPIN_DATAFLOW dataFlow;
KSPIN_CINSTANCES instances;
unsigned long frameSize;
int maxChannels;
unsigned long formats;
int defaultSampleRate;
ULONG *positionRegister; /* WaveRT */
ULONG hwLatency; /* WaveRT */
FunctionMemoryBarrier fnMemBarrier; /* WaveRT */
FunctionGetPinAudioPosition fnAudioPosition; /* WaveRT */
FunctionPinHandler fnEventHandler;
FunctionPinHandler fnSubmitHandler;
};
/* The Filter structure
* A filter has a number of pins and a "friendly name" */
struct __PaWinWdmFilter
{
HANDLE handle;
PaWinWDMKSDeviceInfo devInfo; /* This will hold information that is exposed in PaDeviceInfo */
DWORD deviceNode;
int pinCount;
PaWinWdmPin** pins;
PaWinWdmFilter* topologyFilter;
wchar_t friendlyName[MAX_PATH];
int validPinCount;
int usageCount;
KSMULTIPLE_ITEM* connections;
KSMULTIPLE_ITEM* nodes;
int filterRefCount;
};
typedef struct __PaWinWdmDeviceInfo
{
PaDeviceInfo inheritedDeviceInfo;
char compositeName[MAX_PATH]; /* Composite name consists of pin name + device name in utf8 */
PaWinWdmFilter* filter;
unsigned long pin;
int muxPosition; /* Used only for input devices */
int endpointPinId;
}
PaWinWdmDeviceInfo;
/* PaWinWdmHostApiRepresentation - host api datastructure specific to this implementation */
typedef struct __PaWinWdmHostApiRepresentation
{
PaUtilHostApiRepresentation inheritedHostApiRep;
PaUtilStreamInterface callbackStreamInterface;
PaUtilStreamInterface blockingStreamInterface;
PaUtilAllocationGroup* allocations;
int deviceCount;
}
PaWinWdmHostApiRepresentation;
typedef struct __DATAPACKET
{
KSSTREAM_HEADER Header;
OVERLAPPED Signal;
} DATAPACKET;
typedef struct __PaIOPacket
{
DATAPACKET* packet;
unsigned startByte;
unsigned lengthBytes;
} PaIOPacket;
typedef struct __PaWinWdmIOInfo
{
PaWinWdmPin* pPin;
char* hostBuffer;
unsigned hostBufferSize;
unsigned framesPerBuffer;
unsigned bytesPerFrame;
unsigned bytesPerSample;
unsigned noOfPackets; /* Only used in WaveCyclic */
HANDLE *events; /* noOfPackets handles (WaveCyclic) 1 (WaveRT) */
DATAPACKET *packets; /* noOfPackets packets (WaveCyclic) 2 (WaveRT) */
/* WaveRT polled mode */
unsigned lastPosition;
unsigned pollCntr;
} PaWinWdmIOInfo;
/* PaWinWdmStream - a stream data structure specifically for this implementation */
struct __PaWinWdmStream
{
PaUtilStreamRepresentation streamRepresentation;
PaWDMKSSpecificStreamInfo hostApiStreamInfo; /* This holds info that is exposed through PaStreamInfo */
PaUtilCpuLoadMeasurer cpuLoadMeasurer;
PaUtilBufferProcessor bufferProcessor;
#if PA_TRACE_REALTIME_EVENTS
LogHandle hLog;
#endif
PaUtilAllocationGroup* allocGroup;
PaWinWdmIOInfo capture;
PaWinWdmIOInfo render;
int streamStarted;
int streamActive;
int streamStop;
int streamAbort;
int oldProcessPriority;
HANDLE streamThread;
HANDLE eventAbort;
HANDLE eventStreamStart[StreamStart_kCnt]; /* 0 = OK, 1 = Failed */
PaError threadResult;
PaStreamFlags streamFlags;
/* Capture ring buffer */
PaUtilRingBuffer ringBuffer;
char* ringBufferData;
/* These values handle the case where the user wants to use fewer
* channels than the device has */
int userInputChannels;
int deviceInputChannels;
int userOutputChannels;
int deviceOutputChannels;
};
/* Gather all processing variables in a struct */
struct __PaProcessThreadInfo
{
PaWinWdmStream *stream;
PaStreamCallbackTimeInfo ti;
PaStreamCallbackFlags underover;
int cbResult;
volatile int pending;
volatile int priming;
volatile int pinsStarted;
unsigned long timeout;
unsigned captureHead;
unsigned captureTail;
unsigned renderHead;
unsigned renderTail;
PaIOPacket capturePackets[4];
PaIOPacket renderPackets[4];
};
/* Used for transferring device infos during scanning / rescanning */
typedef struct __PaWinWDMScanDeviceInfosResults
{
PaDeviceInfo **deviceInfos;
PaDeviceIndex defaultInputDevice;
PaDeviceIndex defaultOutputDevice;
} PaWinWDMScanDeviceInfosResults;
static const unsigned cPacketsArrayMask = 3;
HMODULE DllKsUser = NULL;
KSCREATEPIN* FunctionKsCreatePin = NULL;
/* prototypes for functions declared in this file */
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
PaError PaWinWdm_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* Low level I/O functions */
static PaError WdmSyncIoctl(HANDLE handle,
unsigned long ioctlNumber,
void* inBuffer,
unsigned long inBufferCount,
void* outBuffer,
unsigned long outBufferCount,
unsigned long* bytesReturned);
static PaError WdmGetPropertySimple(HANDLE handle,
const GUID* const guidPropertySet,
unsigned long property,
void* value,
unsigned long valueCount);
static PaError WdmSetPropertySimple(HANDLE handle,
const GUID* const guidPropertySet,
unsigned long property,
void* value,
unsigned long valueCount,
void* instance,
unsigned long instanceCount);
static PaError WdmGetPinPropertySimple(HANDLE handle,
unsigned long pinId,
const GUID* const guidPropertySet,
unsigned long property,
void* value,
unsigned long valueCount,
unsigned long* byteCount);
static PaError WdmGetPinPropertyMulti(HANDLE handle,
unsigned long pinId,
const GUID* const guidPropertySet,
unsigned long property,
KSMULTIPLE_ITEM** ksMultipleItem);
static PaError WdmGetPropertyMulti(HANDLE handle,
const GUID* const guidPropertySet,
unsigned long property,
KSMULTIPLE_ITEM** ksMultipleItem);
static PaError WdmSetMuxNodeProperty(HANDLE handle,
ULONG nodeId,
ULONG pinId);
/** Pin management functions */
static PaWinWdmPin* PinNew(PaWinWdmFilter* parentFilter, unsigned long pinId, PaError* error);
static void PinFree(PaWinWdmPin* pin);
static void PinClose(PaWinWdmPin* pin);
static PaError PinInstantiate(PaWinWdmPin* pin);
/*static PaError PinGetState(PaWinWdmPin* pin, KSSTATE* state); NOT USED */
static PaError PinSetState(PaWinWdmPin* pin, KSSTATE state);
static PaError PinSetFormat(PaWinWdmPin* pin, const WAVEFORMATEX* format);
static PaError PinIsFormatSupported(PaWinWdmPin* pin, const WAVEFORMATEX* format);
/* WaveRT support */
static PaError PinQueryNotificationSupport(PaWinWdmPin* pPin, BOOL* pbResult);
static PaError PinGetBuffer(PaWinWdmPin* pPin, void** pBuffer, DWORD* pRequestedBufSize, BOOL* pbCallMemBarrier);
static PaError PinRegisterPositionRegister(PaWinWdmPin* pPin);
static PaError PinRegisterNotificationHandle(PaWinWdmPin* pPin, HANDLE handle);
static PaError PinUnregisterNotificationHandle(PaWinWdmPin* pPin, HANDLE handle);
static PaError PinGetHwLatency(PaWinWdmPin* pPin, ULONG* pFifoSize, ULONG* pChipsetDelay, ULONG* pCodecDelay);
static PaError PinGetAudioPositionMemoryMapped(PaWinWdmPin* pPin, ULONG* pPosition);
static PaError PinGetAudioPositionViaIOCTLRead(PaWinWdmPin* pPin, ULONG* pPosition);
static PaError PinGetAudioPositionViaIOCTLWrite(PaWinWdmPin* pPin, ULONG* pPosition);
/* Filter management functions */
static PaWinWdmFilter* FilterNew(PaWDMKSType type, DWORD devNode, const wchar_t* filterName, const wchar_t* friendlyName, PaError* error);
static PaError FilterInitializePins(PaWinWdmFilter* filter);
static void FilterFree(PaWinWdmFilter* filter);
static void FilterAddRef(PaWinWdmFilter* filter);
static PaWinWdmPin* FilterCreatePin(
PaWinWdmFilter* filter,
int pinId,
const WAVEFORMATEX* wfex,
PaError* error);
static PaError FilterUse(PaWinWdmFilter* filter);
static void FilterRelease(PaWinWdmFilter* filter);
/* Hot plug functions */
static BOOL IsDeviceTheSame(const PaWinWdmDeviceInfo* pDev1,
const PaWinWdmDeviceInfo* pDev2);
/* Interface functions */
static void Terminate( struct PaUtilHostApiRepresentation *hostApi );
static PaError IsFormatSupported(
struct PaUtilHostApiRepresentation *hostApi,
const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate );
static PaError ScanDeviceInfos( struct PaUtilHostApiRepresentation *hostApi, PaHostApiIndex index, void **newDeviceInfos, int *newDeviceCount );
static PaError CommitDeviceInfos( struct PaUtilHostApiRepresentation *hostApi, PaHostApiIndex index, void *deviceInfos, int deviceCount );
static PaError DisposeDeviceInfos( struct PaUtilHostApiRepresentation *hostApi, void *deviceInfos, int deviceCount );
static PaError OpenStream(
struct PaUtilHostApiRepresentation *hostApi,
PaStream** s,
const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate,
unsigned long framesPerBuffer,
PaStreamFlags streamFlags,
PaStreamCallback *streamCallback,
void *userData );
static PaError CloseStream( PaStream* stream );
static PaError StartStream( PaStream *stream );
static PaError StopStream( PaStream *stream );
static PaError AbortStream( PaStream *stream );
static PaError IsStreamStopped( PaStream *s );
static PaError IsStreamActive( PaStream *stream );
static PaTime GetStreamTime( PaStream *stream );
static double GetStreamCpuLoad( PaStream* stream );
static PaError ReadStream(
PaStream* stream,
void *buffer,
unsigned long frames );
static PaError WriteStream(
PaStream* stream,
const void *buffer,
unsigned long frames );
static signed long GetStreamReadAvailable( PaStream* stream );
static signed long GetStreamWriteAvailable( PaStream* stream );
/* Utility functions */
static unsigned long GetWfexSize(const WAVEFORMATEX* wfex);
static PaWinWdmFilter** BuildFilterList(int* filterCount, int* noOfPaDevices, PaError* result);
static BOOL PinWrite(HANDLE h, DATAPACKET* p);
static BOOL PinRead(HANDLE h, DATAPACKET* p);
static void DuplicateFirstChannelInt16(void* buffer, int channels, int samples);
static void DuplicateFirstChannelInt24(void* buffer, int channels, int samples);
PA_THREAD_FUNC ProcessingThread(void*);
/* Pin handler functions */
static PaError PaPinCaptureEventHandler_WaveCyclic(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinCaptureSubmitHandler_WaveCyclic(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinRenderEventHandler_WaveCyclic(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinRenderSubmitHandler_WaveCyclic(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinCaptureEventHandler_WaveRTEvent(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinCaptureEventHandler_WaveRTPolled(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinCaptureSubmitHandler_WaveRTEvent(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinCaptureSubmitHandler_WaveRTPolled(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinRenderEventHandler_WaveRTEvent(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinRenderEventHandler_WaveRTPolled(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinRenderSubmitHandler_WaveRTEvent(PaProcessThreadInfo* pInfo, unsigned eventIndex);
static PaError PaPinRenderSubmitHandler_WaveRTPolled(PaProcessThreadInfo* pInfo, unsigned eventIndex);
/* Function bodies */
#if defined(_DEBUG) && defined(PA_ENABLE_DEBUG_OUTPUT)
#define PA_WDMKS_SET_TREF
static PaTime tRef = 0;
static void PaWinWdmDebugPrintf(const char* fmt, ...)
{
va_list list;
char buffer[1024];
PaTime t = PaUtil_GetTime() - tRef;
va_start(list, fmt);
_vsnprintf(buffer, 1023, fmt, list);
va_end(list);
PaUtil_DebugPrint("%6.3lf: %s", t, buffer);
}
#ifdef PA_DEBUG
#undef PA_DEBUG
#define PA_DEBUG(x) PaWinWdmDebugPrintf x ;
#endif
#endif
static BOOL IsDeviceTheSame(const PaWinWdmDeviceInfo* pDev1,
const PaWinWdmDeviceInfo* pDev2)
{
if (pDev1 == NULL || pDev2 == NULL)
return FALSE;
if (pDev1 == pDev2)
return TRUE;
if (strcmp(pDev1->compositeName, pDev2->compositeName) == 0)
return TRUE;
return FALSE;
}
static BOOL IsEarlierThanVista()
{
return (PaWinUtil_GetOsVersion() < paOsVersionWindowsVistaServer2008);
}
static void MemoryBarrierDummy(void)
{
/* Do nothing */
}
static void MemoryBarrierRead(void)
{
PaUtil_ReadMemoryBarrier();
}
static void MemoryBarrierWrite(void)
{
PaUtil_WriteMemoryBarrier();
}
static unsigned long GetWfexSize(const WAVEFORMATEX* wfex)
{
if( wfex->wFormatTag == WAVE_FORMAT_PCM )
{
return sizeof( WAVEFORMATEX );
}
else
{
return (sizeof( WAVEFORMATEX ) + wfex->cbSize);
}
}
static void PaWinWDM_SetLastErrorInfo(long errCode, const char* fmt, ...)
{
va_list list;
char buffer[1024];
va_start(list, fmt);
_vsnprintf(buffer, 1023, fmt, list);
va_end(list);
PaUtil_SetLastHostErrorInfo(paWDMKS, errCode, buffer);
}
/*
Low level pin/filter access functions
*/
static PaError WdmSyncIoctl(
HANDLE handle,
unsigned long ioctlNumber,
void* inBuffer,
unsigned long inBufferCount,
void* outBuffer,
unsigned long outBufferCount,
unsigned long* bytesReturned)
{
PaError result = paNoError;
unsigned long dummyBytesReturned = 0;
BOOL bRes;
if( !bytesReturned )
{
/* Use a dummy as the caller hasn't supplied one */
bytesReturned = &dummyBytesReturned;
}
bRes = DeviceIoControl(handle, ioctlNumber, inBuffer, inBufferCount, outBuffer, outBufferCount, bytesReturned, NULL);
if (!bRes)
{
unsigned long error = GetLastError();
if ( !(((error == ERROR_INSUFFICIENT_BUFFER ) || ( error == ERROR_MORE_DATA )) &&
( ioctlNumber == IOCTL_KS_PROPERTY ) &&
( outBufferCount == 0 ) ) )
{
KSPROPERTY* ksProperty = (KSPROPERTY*)inBuffer;
PaWinWDM_SetLastErrorInfo(result, "WdmSyncIoctl: DeviceIoControl GLE = 0x%08X (prop_set = {%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}, prop_id = %u)",
error,
ksProperty->Set.Data1, ksProperty->Set.Data2, ksProperty->Set.Data3,
ksProperty->Set.Data4[0], ksProperty->Set.Data4[1],
ksProperty->Set.Data4[2], ksProperty->Set.Data4[3],
ksProperty->Set.Data4[4], ksProperty->Set.Data4[5],
ksProperty->Set.Data4[6], ksProperty->Set.Data4[7],
ksProperty->Id
);
result = paUnanticipatedHostError;
}
}
return result;
}
static PaError WdmGetPropertySimple(HANDLE handle,
const GUID* const guidPropertySet,
unsigned long property,
void* value,
unsigned long valueCount)
{
PaError result;
KSPROPERTY ksProperty;
ksProperty.Set = *guidPropertySet;
ksProperty.Id = property;
ksProperty.Flags = KSPROPERTY_TYPE_GET;
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
&ksProperty,
sizeof(KSPROPERTY),
value,
valueCount,
NULL);
return result;
}
static PaError WdmSetPropertySimple(
HANDLE handle,
const GUID* const guidPropertySet,
unsigned long property,
void* value,
unsigned long valueCount,
void* instance,
unsigned long instanceCount)
{
PaError result;
KSPROPERTY* ksProperty;
unsigned long propertyCount = 0;
propertyCount = sizeof(KSPROPERTY) + instanceCount;
ksProperty = (KSPROPERTY*)_alloca( propertyCount );
if( !ksProperty )
{
return paInsufficientMemory;
}
ksProperty->Set = *guidPropertySet;
ksProperty->Id = property;
ksProperty->Flags = KSPROPERTY_TYPE_SET;
if( instance )
{
memcpy((void*)((char*)ksProperty + sizeof(KSPROPERTY)), instance, instanceCount);
}
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
ksProperty,
propertyCount,
value,
valueCount,
NULL);
return result;
}
static PaError WdmGetPinPropertySimple(
HANDLE handle,
unsigned long pinId,
const GUID* const guidPropertySet,
unsigned long property,
void* value,
unsigned long valueCount,
unsigned long *byteCount)
{
PaError result;
KSP_PIN ksPProp;
ksPProp.Property.Set = *guidPropertySet;
ksPProp.Property.Id = property;
ksPProp.Property.Flags = KSPROPERTY_TYPE_GET;
ksPProp.PinId = pinId;
ksPProp.Reserved = 0;
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
&ksPProp,
sizeof(KSP_PIN),
value,
valueCount,
byteCount);
return result;
}
static PaError WdmGetPinPropertyMulti(
HANDLE handle,
unsigned long pinId,
const GUID* const guidPropertySet,
unsigned long property,
KSMULTIPLE_ITEM** ksMultipleItem)
{
PaError result;
unsigned long multipleItemSize = 0;
KSP_PIN ksPProp;
ksPProp.Property.Set = *guidPropertySet;
ksPProp.Property.Id = property;
ksPProp.Property.Flags = KSPROPERTY_TYPE_GET;
ksPProp.PinId = pinId;
ksPProp.Reserved = 0;
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
&ksPProp.Property,
sizeof(KSP_PIN),
NULL,
0,
&multipleItemSize);
if( result != paNoError )
{
return result;
}
*ksMultipleItem = (KSMULTIPLE_ITEM*)PaUtil_AllocateZeroInitializedMemory( multipleItemSize );
if( !*ksMultipleItem )
{
return paInsufficientMemory;
}
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
&ksPProp,
sizeof(KSP_PIN),
(void*)*ksMultipleItem,
multipleItemSize,
NULL);
if( result != paNoError )
{
PaUtil_FreeMemory( ksMultipleItem );
}
return result;
}
static PaError WdmGetPropertyMulti(HANDLE handle,
const GUID* const guidPropertySet,
unsigned long property,
KSMULTIPLE_ITEM** ksMultipleItem)
{
PaError result;
unsigned long multipleItemSize = 0;
KSPROPERTY ksProp;
ksProp.Set = *guidPropertySet;
ksProp.Id = property;
ksProp.Flags = KSPROPERTY_TYPE_GET;
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
&ksProp,
sizeof(KSPROPERTY),
NULL,
0,
&multipleItemSize);
if( result != paNoError )
{
return result;
}
*ksMultipleItem = (KSMULTIPLE_ITEM*)PaUtil_AllocateZeroInitializedMemory( multipleItemSize );
if( !*ksMultipleItem )
{
return paInsufficientMemory;
}
result = WdmSyncIoctl(
handle,
IOCTL_KS_PROPERTY,
&ksProp,
sizeof(KSPROPERTY),
(void*)*ksMultipleItem,
multipleItemSize,
NULL);
if( result != paNoError )
{
PaUtil_FreeMemory( ksMultipleItem );
}
return result;
}
static PaError WdmSetMuxNodeProperty(HANDLE handle,
ULONG nodeId,
ULONG pinId)
{
PaError result = paNoError;
KSNODEPROPERTY prop;
prop.Property.Set = KSPROPSETID_Audio;
prop.Property.Id = KSPROPERTY_AUDIO_MUX_SOURCE;
prop.Property.Flags = KSPROPERTY_TYPE_SET | KSPROPERTY_TYPE_TOPOLOGY;
prop.NodeId = nodeId;
prop.Reserved = 0;
result = WdmSyncIoctl(handle, IOCTL_KS_PROPERTY, &prop, sizeof(KSNODEPROPERTY), &pinId, sizeof(ULONG), NULL);
return result;
}
/* Used when traversing topology for outputs */
static const KSTOPOLOGY_CONNECTION* GetConnectionTo(const KSTOPOLOGY_CONNECTION* pFrom, PaWinWdmFilter* filter, int muxIdx)
{
unsigned i;
const KSTOPOLOGY_CONNECTION* retval = NULL;
const KSTOPOLOGY_CONNECTION* connections = (const KSTOPOLOGY_CONNECTION*)(filter->connections + 1);
(void)muxIdx;
PA_DEBUG(("GetConnectionTo: Checking %u connections... (pFrom = %p)", filter->connections->Count, pFrom));
for (i = 0; i < filter->connections->Count; ++i)
{
const KSTOPOLOGY_CONNECTION* pConn = connections + i;
if (pConn == pFrom)
continue;
if (pConn->FromNode == pFrom->ToNode)
{
retval = pConn;
break;
}
}
PA_DEBUG(("GetConnectionTo: Returning %p\n", retval));
return retval;
}
/* Used when traversing topology for inputs */
static const KSTOPOLOGY_CONNECTION* GetConnectionFrom(const KSTOPOLOGY_CONNECTION* pTo, PaWinWdmFilter* filter, int muxIdx)
{
unsigned i;
const KSTOPOLOGY_CONNECTION* retval = NULL;
const KSTOPOLOGY_CONNECTION* connections = (const KSTOPOLOGY_CONNECTION*)(filter->connections + 1);
int muxCntr = 0;
PA_DEBUG(("GetConnectionFrom: Checking %u connections... (pTo = %p)\n", filter->connections->Count, pTo));
for (i = 0; i < filter->connections->Count; ++i)
{
const KSTOPOLOGY_CONNECTION* pConn = connections + i;
if (pConn == pTo)
continue;
if (pConn->ToNode == pTo->FromNode)
{
if (muxIdx >= 0)