Debian Codegraph @

external/portaudio/pa_win_wdmks_utils.c

raw symbols SCIP
1 #ifdef _WIN32
2 /*
3 * PortAudio Portable Real-Time Audio Library
4 * Windows WDM KS utilities
5 *
6 * Copyright (c) 1999 - 2007 Andrew Baldwin, Ross Bencina
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files
10 * (the "Software"), to deal in the Software without restriction,
11 * including without limitation the rights to use, copy, modify, merge,
12 * publish, distribute, sublicense, and/or sell copies of the Software,
13 * and to permit persons to whom the Software is furnished to do so,
14 * subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 /*
29 * The text above constitutes the entire PortAudio license; however,
30 * the PortAudio community also makes the following non-binding requests:
31 *
32 * Any person wishing to distribute modifications to the Software is
33 * requested to send the modifications to the original developer so that
34 * they can be incorporated into the canonical version. It is also
35 * requested that these non-binding requests be included along with the
36 * license above.
37 */
38
39 #include <windows.h>
40 #include <mmreg.h>
41 #ifndef WAVE_FORMAT_IEEE_FLOAT
42 #define WAVE_FORMAT_IEEE_FLOAT 0x0003 // MinGW32 does not define this
43 #endif
44 #ifndef _WAVEFORMATEXTENSIBLE_
45 #define _WAVEFORMATEXTENSIBLE_ // MinGW32 does not define this
46 #endif
47 #ifndef _INC_MMREG
48 #define _INC_MMREG // for STATIC_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
49 #endif
50 #include <winioctl.h> // MinGW32 does not define this automatically
51
52 #if defined(__GNUC__)
53
54 #include "../../hostapi/wasapi/mingw-include/ks.h"
55 #include "../../hostapi/wasapi/mingw-include/ksmedia.h"
56
57 #else
58
59 #include <ks.h>
60 #include <ksmedia.h>
61
62 #endif
63
64 #include <stdio.h> // just for some development printfs
65
66 #include "portaudio.h"
67 #include "pa_util.h"
68 #include "pa_win_wdmks_utils.h"
69
70
71 /* PortAudio-local instances of GUIDs previously sourced from ksguid.lib */
72
73 /* GUID KSDATAFORMAT_TYPE_AUDIO */
74 static const GUID pa_KSDATAFORMAT_TYPE_AUDIO = { STATIC_KSDATAFORMAT_TYPE_AUDIO };
75
76 /* GUID KSDATAFORMAT_SUBTYPE_IEEE_FLOAT */
77 static const GUID pa_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { STATIC_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT };
78
79 /* GUID KSDATAFORMAT_SUBTYPE_PCM */
80 static const GUID pa_KSDATAFORMAT_SUBTYPE_PCM = { STATIC_KSDATAFORMAT_SUBTYPE_PCM };
81
82 /* GUID KSDATAFORMAT_SUBTYPE_WAVEFORMATEX */
83 static const GUID pa_KSDATAFORMAT_SUBTYPE_WAVEFORMATEX = { STATIC_KSDATAFORMAT_SUBTYPE_WAVEFORMATEX };
84
85 /* GUID KSMEDIUMSETID_Standard */
86 static const GUID pa_KSMEDIUMSETID_Standard = { STATIC_KSMEDIUMSETID_Standard };
87
88 /* GUID KSINTERFACESETID_Standard */
89 static const GUID pa_KSINTERFACESETID_Standard = { STATIC_KSINTERFACESETID_Standard };
90
91 /* GUID KSPROPSETID_Pin */
92 static const GUID pa_KSPROPSETID_Pin = { STATIC_KSPROPSETID_Pin };
93
94 #define pa_IS_VALID_WAVEFORMATEX_GUID(Guid)\
95 (!memcmp(((PUSHORT)&pa_KSDATAFORMAT_SUBTYPE_WAVEFORMATEX) + 1, ((PUSHORT)(Guid)) + 1, sizeof(GUID) - sizeof(USHORT)))
96
97
98 static PaError WdmGetPinPropertySimple(
99 HANDLE handle,
100 unsigned long pinId,
101 unsigned long property,
102 void* value,
103 unsigned long valueSize )
104 {
105 DWORD bytesReturned;
106 KSP_PIN ksPProp;
107 ksPProp.Property.Set = pa_KSPROPSETID_Pin;
108 ksPProp.Property.Id = property;
109 ksPProp.Property.Flags = KSPROPERTY_TYPE_GET;
110 ksPProp.PinId = pinId;
111 ksPProp.Reserved = 0;
112
113 if( DeviceIoControl( handle, IOCTL_KS_PROPERTY, &ksPProp, sizeof(KSP_PIN),
114 value, valueSize, &bytesReturned, NULL ) == 0 || bytesReturned != valueSize )
115 {
116 return paUnanticipatedHostError;
117 }
118 else
119 {
120 return paNoError;
121 }
122 }
123
124
125 static PaError WdmGetPinPropertyMulti(
126 HANDLE handle,
127 unsigned long pinId,
128 unsigned long property,
129 KSMULTIPLE_ITEM** ksMultipleItem)
130 {
131 unsigned long multipleItemSize = 0;
132 KSP_PIN ksPProp;
133 DWORD bytesReturned;
134
135 *ksMultipleItem = 0;
136
137 ksPProp.Property.Set = pa_KSPROPSETID_Pin;
138 ksPProp.Property.Id = property;
139 ksPProp.Property.Flags = KSPROPERTY_TYPE_GET;
140 ksPProp.PinId = pinId;
141 ksPProp.Reserved = 0;
142
143 if( DeviceIoControl( handle, IOCTL_KS_PROPERTY, &ksPProp.Property,
144 sizeof(KSP_PIN), NULL, 0, &multipleItemSize, NULL ) == 0 && GetLastError() != ERROR_MORE_DATA )
145 {
146 return paUnanticipatedHostError;
147 }
148
149 *ksMultipleItem = (KSMULTIPLE_ITEM*)PaUtil_AllocateMemory( multipleItemSize );
150 if( !*ksMultipleItem )
151 {
152 return paInsufficientMemory;
153 }
154
155 if( DeviceIoControl( handle, IOCTL_KS_PROPERTY, &ksPProp, sizeof(KSP_PIN),
156 (void*)*ksMultipleItem, multipleItemSize, &bytesReturned, NULL ) == 0 || bytesReturned != multipleItemSize )
157 {
158 PaUtil_FreeMemory( ksMultipleItem );
159 return paUnanticipatedHostError;
160 }
161
162 return paNoError;
163 }
164
165
166 static int GetKSFilterPinCount( HANDLE deviceHandle )
167 {
168 DWORD result;
169
170 if( WdmGetPinPropertySimple( deviceHandle, 0, KSPROPERTY_PIN_CTYPES, &result, sizeof(result) ) == paNoError ){
171 return result;
172 }else{
173 return 0;
174 }
175 }
176
177
178 static KSPIN_COMMUNICATION GetKSFilterPinPropertyCommunication( HANDLE deviceHandle, int pinId )
179 {
180 KSPIN_COMMUNICATION result;
181
182 if( WdmGetPinPropertySimple( deviceHandle, pinId, KSPROPERTY_PIN_COMMUNICATION, &result, sizeof(result) ) == paNoError ){
183 return result;
184 }else{
185 return KSPIN_COMMUNICATION_NONE;
186 }
187 }
188
189
190 static KSPIN_DATAFLOW GetKSFilterPinPropertyDataflow( HANDLE deviceHandle, int pinId )
191 {
192 KSPIN_DATAFLOW result;
193
194 if( WdmGetPinPropertySimple( deviceHandle, pinId, KSPROPERTY_PIN_DATAFLOW, &result, sizeof(result) ) == paNoError ){
195 return result;
196 }else{
197 return (KSPIN_DATAFLOW)0;
198 }
199 }
200
201
202 static int KSFilterPinPropertyIdentifiersInclude(
203 HANDLE deviceHandle, int pinId, unsigned long property, const GUID *identifierSet, unsigned long identifierId )
204 {
205 KSMULTIPLE_ITEM* item = NULL;
206 KSIDENTIFIER* identifier;
207 int i;
208 int result = 0;
209
210 if( WdmGetPinPropertyMulti( deviceHandle, pinId, property, &item) != paNoError )
211 return 0;
212
213 identifier = (KSIDENTIFIER*)(item+1);
214
215 for( i = 0; i < (int)item->Count; i++ )
216 {
217 if( !memcmp( (void*)&identifier[i].Set, (void*)identifierSet, sizeof( GUID ) ) &&
218 ( identifier[i].Id == identifierId ) )
219 {
220 result = 1;
221 break;
222 }
223 }
224
225 PaUtil_FreeMemory( item );
226
227 return result;
228 }
229
230
231 /* return the maximum channel count supported by any pin on the device.
232 if isInput is non-zero we query input pins, otherwise output pins.
233 */
234 int PaWin_WDMKS_QueryFilterMaximumChannelCount( void *wcharDevicePath, int isInput )
235 {
236 HANDLE deviceHandle;
237 ULONG i;
238 int pinCount, pinId;
239 int result = 0;
240 KSPIN_DATAFLOW requiredDataflowDirection = (isInput ? KSPIN_DATAFLOW_OUT : KSPIN_DATAFLOW_IN );
241
242 if( !wcharDevicePath )
243 return 0;
244
245 deviceHandle = CreateFileW( (LPCWSTR)wcharDevicePath, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
246 if( deviceHandle == INVALID_HANDLE_VALUE )
247 return 0;
248
249 pinCount = GetKSFilterPinCount( deviceHandle );
250 for( pinId = 0; pinId < pinCount; ++pinId )
251 {
252 KSPIN_COMMUNICATION communication = GetKSFilterPinPropertyCommunication( deviceHandle, pinId );
253 KSPIN_DATAFLOW dataflow = GetKSFilterPinPropertyDataflow( deviceHandle, pinId );
254 if( ( dataflow == requiredDataflowDirection ) &&
255 (( communication == KSPIN_COMMUNICATION_SINK) ||
256 ( communication == KSPIN_COMMUNICATION_BOTH))
257 && ( KSFilterPinPropertyIdentifiersInclude( deviceHandle, pinId,
258 KSPROPERTY_PIN_INTERFACES, &pa_KSINTERFACESETID_Standard, KSINTERFACE_STANDARD_STREAMING )
259 || KSFilterPinPropertyIdentifiersInclude( deviceHandle, pinId,
260 KSPROPERTY_PIN_INTERFACES, &pa_KSINTERFACESETID_Standard, KSINTERFACE_STANDARD_LOOPED_STREAMING ) )
261 && KSFilterPinPropertyIdentifiersInclude( deviceHandle, pinId,
262 KSPROPERTY_PIN_MEDIUMS, &pa_KSMEDIUMSETID_Standard, KSMEDIUM_STANDARD_DEVIO ) )
263 {
264 KSMULTIPLE_ITEM* item = NULL;
265 if( WdmGetPinPropertyMulti( deviceHandle, pinId, KSPROPERTY_PIN_DATARANGES, &item ) == paNoError )
266 {
267 KSDATARANGE *dataRange = (KSDATARANGE*)(item+1);
268
269 for( i=0; i < item->Count; ++i ){
270
271 if( pa_IS_VALID_WAVEFORMATEX_GUID(&dataRange->SubFormat)
272 || memcmp( (void*)&dataRange->SubFormat, (void*)&pa_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID) ) == 0
273 || memcmp( (void*)&dataRange->SubFormat, (void*)&pa_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(GUID) ) == 0
274 || ( ( memcmp( (void*)&dataRange->MajorFormat, (void*)&pa_KSDATAFORMAT_TYPE_AUDIO, sizeof(GUID) ) == 0 )
275 && ( memcmp( (void*)&dataRange->SubFormat, (void*)&KSDATAFORMAT_SUBTYPE_WILDCARD, sizeof(GUID) ) == 0 ) ) )
276 {
277 KSDATARANGE_AUDIO *dataRangeAudio = (KSDATARANGE_AUDIO*)dataRange;
278
279 /*
280 printf( ">>> %d %d %d %d %S\n", isInput, dataflow, communication, dataRangeAudio->MaximumChannels, devicePath );
281
282 if( memcmp((void*)&dataRange->Specifier, (void*)&KSDATAFORMAT_SPECIFIER_WAVEFORMATEX, sizeof(GUID) ) == 0 )
283 printf( "\tspecifier: KSDATAFORMAT_SPECIFIER_WAVEFORMATEX\n" );
284 else if( memcmp((void*)&dataRange->Specifier, (void*)&KSDATAFORMAT_SPECIFIER_DSOUND, sizeof(GUID) ) == 0 )
285 printf( "\tspecifier: KSDATAFORMAT_SPECIFIER_DSOUND\n" );
286 else if( memcmp((void*)&dataRange->Specifier, (void*)&KSDATAFORMAT_SPECIFIER_WILDCARD, sizeof(GUID) ) == 0 )
287 printf( "\tspecifier: KSDATAFORMAT_SPECIFIER_WILDCARD\n" );
288 else
289 printf( "\tspecifier: ?\n" );
290 */
291
292 /*
293 We assume that very high values for MaximumChannels are not useful and indicate
294 that the driver isn't prepared to tell us the real number of channels which it supports.
295 */
296 if( dataRangeAudio->MaximumChannels < 0xFFFFUL && (int)dataRangeAudio->MaximumChannels > result )
297 result = (int)dataRangeAudio->MaximumChannels;
298 }
299
300 dataRange = (KSDATARANGE*)( ((char*)dataRange) + dataRange->FormatSize);
301 }
302
303 PaUtil_FreeMemory( item );
304 }
305 }
306 }
307
308 CloseHandle( deviceHandle );
309 return result;
310 }
311 #endif