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
/*
 * COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS Configuration of network devices
 * FILE:            dll/directx/dsound_new/misc.c
 * PURPOSE:         Misc support routines
 *
 * PROGRAMMERS:     Johannes Anderwald (johannes.anderwald@reactos.org)
 */

#include "precomp.h"

const GUID KSPROPSETID_Pin                     = {0x8C134960L, 0x51AD, 0x11CF, {0x87, 0x8A, 0x94, 0xF8, 0x01, 0xC1, 0x00, 0x00}};
const GUID KSPROPSETID_Topology                 = {0x720D4AC0L, 0x7533, 0x11D0, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
const GUID KSPROPSETID_Audio = {0x45FFAAA0L, 0x6E1B, 0x11D0, {0xBC, 0xF2, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};


VOID
PerformChannelConversion(
    PUCHAR Buffer,
    ULONG BufferLength,
    PULONG BytesRead,
    ULONG OldChannels,
    ULONG NewChannels,
    ULONG BitsPerSample,
    PUCHAR Result,
    ULONG ResultLength,
    PULONG BytesWritten)
{
    DWORD NewIndex, OldIndex;
    DWORD NewLength, Skip;

    if (NewChannels > OldChannels)
    {
        UNIMPLEMENTED;
        ASSERT(0);
    }

    /* setup index */
    NewIndex = 0;
    OldIndex = 0;

    /* calculate offsets */
    NewLength = NewChannels * (BitsPerSample/8);
    Skip = OldChannels * (BitsPerSample/8);

    do
    {
        if (NewIndex + NewLength>= ResultLength)
        {
            NewIndex = ResultLength;
            break;
        }

        if (OldIndex + Skip >= BufferLength)
        {
            OldIndex = BufferLength;
            break;
        }

        /* copy first channel */
        RtlMoveMemory(&Result[NewIndex], &Buffer[OldIndex], NewLength);

        /* skip other channels */
        OldIndex += Skip;

        /* increment offset */
        NewIndex += NewLength;

    }while(TRUE);

    *BytesRead = OldIndex;
    *BytesWritten = NewIndex;
}


BOOL
SetPinFormat(
    IN HANDLE hPin,
    IN LPWAVEFORMATEX WaveFormatEx)
{
    DWORD dwResult;
    KSPROPERTY Property;
    KSDATAFORMAT_WAVEFORMATEX DataFormat;

    /* setup connection request */
    Property.Id = KSPROPERTY_CONNECTION_DATAFORMAT;
    Property.Set = KSPROPSETID_Connection;
    Property.Flags = KSPROPERTY_TYPE_SET;

    /* setup data format */
    DataFormat.WaveFormatEx.wFormatTag = WaveFormatEx->wFormatTag;
    DataFormat.WaveFormatEx.nSamplesPerSec = WaveFormatEx->nSamplesPerSec;
    DataFormat.WaveFormatEx.nBlockAlign = WaveFormatEx->nBlockAlign;
    DataFormat.WaveFormatEx.cbSize = 0;
    DataFormat.DataFormat.FormatSize = sizeof(KSDATAFORMAT) + sizeof(WAVEFORMATEX);
    DataFormat.DataFormat.Flags = 0;
    DataFormat.DataFormat.Reserved = 0;
    DataFormat.DataFormat.MajorFormat = KSDATAFORMAT_TYPE_AUDIO;
    DataFormat.DataFormat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
    DataFormat.DataFormat.Specifier = KSDATAFORMAT_SPECIFIER_WAVEFORMATEX;
    DataFormat.DataFormat.SampleSize = 4;
    DataFormat.WaveFormatEx.nChannels = WaveFormatEx->nChannels;
    DataFormat.WaveFormatEx.nAvgBytesPerSec = WaveFormatEx->nAvgBytesPerSec;
    DataFormat.WaveFormatEx.wBitsPerSample = WaveFormatEx->wBitsPerSample;

    dwResult = SyncOverlappedDeviceIoControl(hPin, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSPROPERTY),(LPVOID)&DataFormat, sizeof(KSDATAFORMAT_WAVEFORMATEX), NULL);

    if (dwResult == ERROR_SUCCESS)
        return TRUE;
    else
        return FALSE;
}


BOOL
DoDataIntersection(
    HANDLE hFilter,
    DWORD PinId,
    DWORD SampleFrequency,
    LPWAVEFORMATEX WaveFormatEx,
    DWORD MinimumBitsPerSample,
    DWORD MaximumBitsPerSample,
    DWORD MaximumChannels,
    LPWAVEFORMATEX WaveFormatOut)
{
    DWORD nChannels, nBitsPerSample;
    KSDATAFORMAT_WAVEFORMATEX WaveFormat;
    PKSP_PIN Pin;
    PKSMULTIPLE_ITEM Item;
    PKSDATAFORMAT_WAVEFORMATEX DataFormat;
    DWORD dwResult;

    /* allocate request */
    Pin = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(KSP_PIN) + sizeof(KSMULTIPLE_ITEM) + sizeof(KSDATAFORMAT_WAVEFORMATEX));
    if (!Pin)
    {
        /* no memory */
        return FALSE;
    }

    Item = (PKSMULTIPLE_ITEM)(Pin + 1);
    DataFormat = (PKSDATAFORMAT_WAVEFORMATEX)(Item + 1);

    /* setup request */
    Pin->PinId = PinId;
    Pin->Property.Flags = KSPROPERTY_TYPE_GET;
    Pin->Property.Set = KSPROPSETID_Pin;
    Pin->Property.Id = KSPROPERTY_PIN_DATAINTERSECTION;
    Item->Count = 1;
    Item->Size = sizeof(KSDATAFORMAT_WAVEFORMATEX);


    DataFormat->WaveFormatEx.wFormatTag = WaveFormatEx->wFormatTag;
    DataFormat->WaveFormatEx.nSamplesPerSec = SampleFrequency;
    DataFormat->WaveFormatEx.nBlockAlign = WaveFormatEx->nBlockAlign;
    DataFormat->WaveFormatEx.cbSize = 0;
    DataFormat->DataFormat.FormatSize = sizeof(KSDATAFORMAT) + sizeof(WAVEFORMATEX);
    DataFormat->DataFormat.Flags = 0;
    DataFormat->DataFormat.Reserved = 0;
    DataFormat->DataFormat.MajorFormat = KSDATAFORMAT_TYPE_AUDIO;
    DataFormat->DataFormat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
    DataFormat->DataFormat.Specifier = KSDATAFORMAT_SPECIFIER_WAVEFORMATEX;
    DataFormat->DataFormat.SampleSize = 4;

    for(nChannels = 1; nChannels <= 2; nChannels++)
    {
        for(nBitsPerSample = MinimumBitsPerSample; nBitsPerSample <= MaximumBitsPerSample; nBitsPerSample += 8)
        {
            DataFormat->WaveFormatEx.nChannels = nChannels;
            DataFormat->WaveFormatEx.nAvgBytesPerSec = (nBitsPerSample / 8) * nChannels * SampleFrequency;
            DataFormat->WaveFormatEx.wBitsPerSample = nBitsPerSample;

            DPRINT("CurrentFormat: InFormat nChannels %u wBitsPerSample %u nSamplesPerSec %u\n",
                   nChannels, nBitsPerSample, SampleFrequency);

            dwResult = SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)Pin, sizeof(KSP_PIN) + sizeof(KSMULTIPLE_ITEM) + sizeof(KSDATAFORMAT_WAVEFORMATEX),
                                                     (LPVOID)&WaveFormat, sizeof(KSDATAFORMAT_WAVEFORMATEX), NULL);

            DPRINT("dwResult %x\n", dwResult);


            if (dwResult == ERROR_SUCCESS)
            {
                /* found a compatible audio range */
                WaveFormatOut->cbSize = 0;
                WaveFormatOut->nBlockAlign = WaveFormatEx->nBlockAlign;
                WaveFormatOut->wFormatTag = WaveFormatEx->wFormatTag;
                WaveFormatOut->nAvgBytesPerSec = (nBitsPerSample / 8) * nChannels * SampleFrequency;
                WaveFormatOut->wBitsPerSample = nBitsPerSample;
                WaveFormatOut->nSamplesPerSec = SampleFrequency;
                WaveFormatOut->nChannels = nChannels;

                /* free buffer */
                HeapFree(GetProcessHeap(), 0, Pin);

                DPRINT("InFormat  nChannels %u wBitsPerSample %u nSamplesPerSec %u\nOutFormat nChannels %u nBitsPerSample %u nSamplesPerSec %u\n",
                       WaveFormatEx->nChannels, WaveFormatEx->wBitsPerSample, WaveFormatEx->nSamplesPerSec,
                       WaveFormatOut->nChannels, WaveFormatOut->wBitsPerSample, WaveFormatOut->nSamplesPerSec);

                return TRUE;
            }
        }
    }

    /* free buffer */
    HeapFree(GetProcessHeap(), 0, Pin);
    ASSERT(0);
    return FALSE;
}

DWORD
OpenPin(
    HANDLE hFilter,
    ULONG PinId,
    LPWAVEFORMATEX WaveFormatEx,
    PHANDLE hPin,
    BOOL bLoop)
{
    DWORD Size, Result;
    PKSPIN_CONNECT PinConnect;
    PKSDATAFORMAT_WAVEFORMATEX DataFormat;

    /* calculate request size */
    Size = sizeof(KSPIN_CONNECT) + sizeof(KSDATAFORMAT_WAVEFORMATEX);

    PinConnect = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
    if (!PinConnect)
    {
        /* not enough memory */
        return DSERR_OUTOFMEMORY;
    }
    /* build pin request */
    PinConnect->Interface.Set = KSINTERFACESETID_Standard;

    if (bLoop)
        PinConnect->Interface.Id = KSINTERFACE_STANDARD_LOOPED_STREAMING;
    else
        PinConnect->Interface.Id = KSINTERFACE_STANDARD_STREAMING;

    PinConnect->Interface.Flags = 0;
    PinConnect->Medium.Set = KSMEDIUMSETID_Standard;
    PinConnect->Medium.Id = KSMEDIUM_TYPE_ANYINSTANCE;
    PinConnect->Medium.Flags = 0;
    PinConnect->PinToHandle = NULL;
    PinConnect->PinId = PinId;
    PinConnect->Priority.PriorityClass = KSPRIORITY_NORMAL;
    PinConnect->Priority.PrioritySubClass = 1;

    DataFormat = (PKSDATAFORMAT_WAVEFORMATEX) (PinConnect + 1);

    /* initialize data format */
    DataFormat->WaveFormatEx.wFormatTag = WaveFormatEx->wFormatTag;
    DataFormat->WaveFormatEx.nChannels = WaveFormatEx->nChannels;
    DataFormat->WaveFormatEx.nSamplesPerSec = WaveFormatEx->nSamplesPerSec;
    DataFormat->WaveFormatEx.nBlockAlign = WaveFormatEx->nBlockAlign;
    DataFormat->WaveFormatEx.nAvgBytesPerSec = WaveFormatEx->nAvgBytesPerSec;
    DataFormat->WaveFormatEx.wBitsPerSample = WaveFormatEx->wBitsPerSample;
    DataFormat->WaveFormatEx.cbSize = 0;
    DataFormat->DataFormat.FormatSize = sizeof(KSDATAFORMAT) + sizeof(WAVEFORMATEX);
    DataFormat->DataFormat.Flags = 0;
    DataFormat->DataFormat.Reserved = 0;
    DataFormat->DataFormat.MajorFormat = KSDATAFORMAT_TYPE_AUDIO;

    DataFormat->DataFormat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
    DataFormat->DataFormat.Specifier = KSDATAFORMAT_SPECIFIER_WAVEFORMATEX;
    DataFormat->DataFormat.SampleSize = 4;

    Result = KsCreatePin(hFilter, PinConnect, GENERIC_READ | GENERIC_WRITE, hPin);

    HeapFree(GetProcessHeap(), 0, PinConnect);

    return Result;
}


DWORD
OpenFilter(
    IN LPCWSTR lpFileName,
    IN PHANDLE OutHandle)
{
    HANDLE Handle;

    /* open the filter */
    Handle = CreateFileW(lpFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

    /* check for success */
    if (Handle == INVALID_HANDLE_VALUE)
    {
        DPRINT("Failed to open Filter %ws\n", lpFileName);
        return GetLastError();
    }

    *OutHandle = Handle;
    return ERROR_SUCCESS;
}

DWORD
SyncOverlappedDeviceIoControl(
    IN  HANDLE Handle,
    IN  DWORD IoControlCode,
    IN  LPVOID InBuffer,
    IN  DWORD InBufferSize,
    OUT LPVOID OutBuffer,
    IN  DWORD OutBufferSize,
    OUT LPDWORD BytesTransferred OPTIONAL)
{
    OVERLAPPED Overlapped;
    BOOLEAN IoResult;
    DWORD Transferred = 0;

    /* Overlapped I/O is done here - this is used for waiting for completion */
    ZeroMemory(&Overlapped, sizeof(OVERLAPPED));
    Overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (!Overlapped.hEvent)
        return GetLastError();

    /* Talk to the device */
    IoResult = DeviceIoControl(Handle,
                               IoControlCode,
                               InBuffer,
                               InBufferSize,
                               OutBuffer,
                               OutBufferSize,
                               BytesTransferred,
                               &Overlapped);

    /* If failure occurs, make sure it's not just due to the overlapped I/O */
    if (!IoResult)
    {
        if ( GetLastError() != ERROR_IO_PENDING )
        {
            CloseHandle(Overlapped.hEvent);
            return GetLastError();
        }
    }

    /* Wait for the I/O to complete */
    IoResult = GetOverlappedResult(Handle,
                                   &Overlapped,
                                   &Transferred,
                                   TRUE);

    /* Don't need this any more */
    CloseHandle(Overlapped.hEvent);

    if (!IoResult)
        return GetLastError();

    if ( BytesTransferred )
        *BytesTransferred = Transferred;

    return ERROR_SUCCESS;
}

DWORD
GetFilterPinCount(
    IN HANDLE hFilter,
    OUT PULONG NumPins)
{
    KSPROPERTY Pin;

    *NumPins = 0;

    /* setup the pin request */
    Pin.Flags = KSPROPERTY_TYPE_GET;
    Pin.Set = KSPROPSETID_Pin;
    Pin.Id = KSPROPERTY_PIN_CTYPES;

    /* query the device */
    return SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Pin, sizeof(KSPROPERTY), (PVOID)NumPins, sizeof(ULONG), NULL);
}

DWORD
GetFilterNodeProperty(
    IN HANDLE hFilter,
    IN ULONG PropertyId,
    OUT PKSMULTIPLE_ITEM *OutMultipleItem)
{
    DWORD Status, BytesReturned;
    PKSMULTIPLE_ITEM MultipleItem;
    KSPROPERTY Property;

    /* setup query request */
    Property.Id = PropertyId;
    Property.Flags = KSPROPERTY_TYPE_GET;
    Property.Set = KSPROPSETID_Topology;

    /* query the size */
    Status = SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSPROPERTY), NULL, 0, &BytesReturned);

    if (Status != ERROR_MORE_DATA)
    {
        /* failed */
        DPRINT("Failed to query PropertyId %lu ErrorCode %lx\n", PropertyId, Status);
        return Status;
    }

    MultipleItem = HeapAlloc(GetProcessHeap(), 0, BytesReturned);
    if (!MultipleItem)
    {
        /* not enough memory */
        DPRINT("Failed to allocate %u Bytes\n", BytesReturned);
        return ERROR_OUTOFMEMORY;
    }

    /* retrieve data ranges */
    Status = SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSP_PIN), (LPVOID)MultipleItem, BytesReturned, &BytesReturned);


    if (Status != ERROR_SUCCESS)
    {
        /* failed to get data ranges */
        DPRINT("SyncOverlappedDeviceIoControl failed with %lx\n", Status);

        HeapFree(GetProcessHeap(), 0, MultipleItem);
        return Status;
    }

    /* save result */
    *OutMultipleItem = MultipleItem;
    return Status;

}

DWORD
GetFilterPinCommunication(
    IN HANDLE hFilter,
    IN ULONG PinId,
    OUT PKSPIN_COMMUNICATION Communication)
{
    KSP_PIN Property;

    Property.Property.Flags = KSPROPERTY_TYPE_GET;
    Property.Property.Set = KSPROPSETID_Pin;
    Property.Property.Id = KSPROPERTY_PIN_COMMUNICATION;
    Property.PinId = PinId;
    Property.Reserved = 0;

    return SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSP_PIN), (LPVOID)Communication, sizeof(KSPIN_COMMUNICATION), NULL);
}

DWORD
GetFilterPinDataFlow(
    IN HANDLE hFilter,
    IN ULONG PinId,
    OUT PKSPIN_DATAFLOW DataFlow)
{
    KSP_PIN Property;

    Property.Property.Flags = KSPROPERTY_TYPE_GET;
    Property.Property.Set = KSPROPSETID_Pin;
    Property.Property.Id = KSPROPERTY_PIN_DATAFLOW;
    Property.PinId = PinId;
    Property.Reserved = 0;

    return SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSP_PIN), (LPVOID)DataFlow, sizeof(KSPIN_DATAFLOW), NULL);
}

DWORD
GetFilterPinDataRanges(
    IN HANDLE hFilter,
    IN ULONG PinId,
    IN OUT PKSMULTIPLE_ITEM * OutMultipleItem)
{
    KSP_PIN Property;
    ULONG BytesReturned = 0;
    DWORD Status;
    PKSMULTIPLE_ITEM MultipleItem;

    /* prepare request */
    Property.Reserved = 0;
    Property.PinId = PinId;
    Property.Property.Set = KSPROPSETID_Pin;
    Property.Property.Id = KSPROPERTY_PIN_DATARANGES;
    Property.Property.Flags = KSPROPERTY_TYPE_GET;

    /* retrieve size of data ranges buffer */
    Status = SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSP_PIN), NULL, 0, &BytesReturned);<--- Status is assigned

#if 0
    if (Status != ERROR_MORE_DATA)
    {
        DPRINT("SyncOverlappedDeviceIoControl failed with %lx\n", Status);
        return Status;
    }
#endif

    ASSERT(BytesReturned);
    MultipleItem = HeapAlloc(GetProcessHeap(), 0, BytesReturned);
    if (!MultipleItem)
    {
        /* not enough memory */
        DPRINT("Failed to allocate %u Bytes\n", BytesReturned);
        return ERROR_OUTOFMEMORY;
    }

    /* retrieve data ranges */
    Status = SyncOverlappedDeviceIoControl(hFilter, IOCTL_KS_PROPERTY, (LPVOID)&Property, sizeof(KSP_PIN), (LPVOID)MultipleItem, BytesReturned, &BytesReturned);<--- Status is overwritten


    if (Status != ERROR_SUCCESS)
    {
        /* failed to get data ranges */
        DPRINT("SyncOverlappedDeviceIoControl failed with %lx\n", Status);

        HeapFree(GetProcessHeap(), 0, MultipleItem);
        return Status;
    }

    /* save result */
    *OutMultipleItem = MultipleItem;
    return Status;
}

BOOL
CreateCompatiblePin(
    IN HANDLE hFilter,
    IN DWORD PinId,
    IN BOOL bLoop,
    IN LPWAVEFORMATEX WaveFormatEx,
    OUT LPWAVEFORMATEX WaveFormatOut,
    OUT PHANDLE hPin)
{
    PKSMULTIPLE_ITEM Item = NULL;
    PKSDATARANGE_AUDIO AudioRange;
    DWORD dwResult;
    DWORD dwIndex, nChannels;

    dwResult = GetFilterPinDataRanges(hFilter, PinId, &Item);

    if (dwResult != ERROR_SUCCESS)
    {
        /* failed to get data ranges */
         return FALSE;
    }

    CopyMemory(WaveFormatOut, WaveFormatEx, sizeof(WAVEFORMATEX));

    /* iterate through all dataranges */
    AudioRange = (PKSDATARANGE_AUDIO)(Item + 1);
    for(dwIndex = 0; dwIndex < Item->Count; dwIndex++)
    {
        if (AudioRange->DataRange.FormatSize != sizeof(KSDATARANGE_AUDIO))
        {
            UNIMPLEMENTED;
            AudioRange = (PKSDATARANGE_AUDIO)((PUCHAR)AudioRange + AudioRange->DataRange.FormatSize);
            continue;
        }

        if (WaveFormatOut->nSamplesPerSec < AudioRange->MinimumSampleFrequency)
            WaveFormatOut->nSamplesPerSec = AudioRange->MinimumSampleFrequency;
        else if (WaveFormatOut->nSamplesPerSec > AudioRange->MaximumSampleFrequency)
            WaveFormatOut->nSamplesPerSec = AudioRange->MaximumSampleFrequency;

        if (WaveFormatOut->wBitsPerSample < AudioRange->MinimumBitsPerSample)
            WaveFormatOut->wBitsPerSample = AudioRange->MinimumBitsPerSample;
        else if (WaveFormatOut->wBitsPerSample > AudioRange->MaximumBitsPerSample)
            WaveFormatOut->wBitsPerSample = AudioRange->MaximumBitsPerSample;

        DPRINT("MinimumBitsPerSample %u MaximumBitsPerSample %u MinimumSampleFrequency %u MaximumSampleFrequency %u\n",
            AudioRange->MinimumBitsPerSample, AudioRange->MaximumBitsPerSample, AudioRange->MinimumSampleFrequency, AudioRange->MaximumSampleFrequency);

        for(nChannels = 1; nChannels <= AudioRange->MaximumChannels; nChannels++)
        {
            WaveFormatOut->nChannels = nChannels;

            dwResult = OpenPin(hFilter, PinId, WaveFormatOut, hPin, TRUE);
            if (dwResult == ERROR_SUCCESS)
            {
                DPRINT("InFormat  nChannels %u wBitsPerSample %u nSamplesPerSec %u\nOutFormat nChannels %u nBitsPerSample %u nSamplesPerSec %u\n",
                       WaveFormatEx->nChannels, WaveFormatEx->wBitsPerSample, WaveFormatEx->nSamplesPerSec,
                       WaveFormatOut->nChannels, WaveFormatOut->wBitsPerSample, WaveFormatOut->nSamplesPerSec);


                /* free buffer */
                HeapFree(GetProcessHeap(), 0, Item);
                return TRUE;
            }
        }
        AudioRange = (PKSDATARANGE_AUDIO)((PUCHAR)AudioRange + AudioRange->DataRange.FormatSize);
    }

    /* free buffer */
    HeapFree(GetProcessHeap(), 0, Item);
    return FALSE;
}