summaryrefslogtreecommitdiff
path: root/audio/sysvad/EndpointsCommon/MiniportStreamAudioEngineNode.cpp
blob: 1b0f13f1c880ed07b13a8732cf02151980649c99 (plain)
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
1001
1002
1003
/*++

Module Name:

    MiniportStreamAudioEngineNode.cpp

Abstract:

    Implementation of IMiniportstreamAudioEngineNode interface for wavert steam.

--*/

#include <sysvad.h>
#include <intsafe.h>
#include <ks.h>
#include "simple.h"
#include "minwavert.h"
#include "minwavertstream.h"
#include "UnittestData.h"
#define MINWAVERTSTREAM_POOLTAG 'SRWM'
#define HNSTIME_PER_MILLISECOND 10000

#pragma warning (disable : 4127)

#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetLfxState 

Description:

    Portcls calls this method to get a offload stream's Lfx state 

Parameters
    
    _Out_ pbEnable: a pointer to a BOOL value for receieving the returned LFX state

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks
    The Lfx operations (on the offload stream) inside HW Audio Engine (such as src, dsp, and other special effects) are hidden from the software audio stack.
So, the driver should return TRUE if any one of the effects is on and returns FALSE when all the opertations are off.
-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::GetLfxState(_Out_ BOOL *_pbEnable)
{
    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::GetLfxState]"));

    *_pbEnable = m_bLfxEnabled;
    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::SetLfxState 
 
Decscription:

    Portcls calls this method to set a offload stream's Lfx state

Parameters:
    
    _In_ bEnable: a BOOL value. TRU: to enable Lfx, FALSE: to disable Lfx

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks
    The local operations (on the offload stream) inside HW Audio Engine (such as src, dsp, and other special effects) are hidden from the software audio stack.
So, when handling disabling Lfx, the driver should ALL the effects. When enabling Lfx, it's up the driver+HW Audio Engine to decide what opertations to turn on
when they see appropriate.
-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::SetLfxState(_In_ BOOL _bEnable)
{
    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::SetGfxState]"));

    UNREFERENCED_PARAMETER(_bEnable);
    m_bLfxEnabled = _bEnable;
    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamChannelVolume 
 
Decscription:

    When handling GET volume KS property for the device, Portcls calls 
    this method, inside its property handlers, to get the current setting on the specific channel.

Parameters:

        _In_ _uiChannel:  the target channel for this GET volume operation
        _Out_ _pVolume: a pointer to a LONG variable for receiving returned information

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::GetStreamChannelVolume(_In_ UINT32 _uiChannel, _Out_ LONG *_pVolume)
{
    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::GetStreamChannelVolume]"));

    *_pVolume = m_plVolumeLevel[_uiChannel];

    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamChannelMute 
 
Decscription:

    When handling GET mute KS property for the device, Portcls calls 
    this method, inside its property handlers, to get the current setting on the specific channel.

Parameters:

        _In_ _uiChannel:  the target channel for this GET volume operation
        _Out_ _pbMute: a pointer to a BOOL variable for receiving returned information

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
STDMETHODIMP_(NTSTATUS) CMiniportWaveRTStream::GetStreamChannelMute(_In_ UINT32 _uiChannel, _Out_  BOOL  *_pbMute)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();
    ASSERT (_pbMute);

    DPF_ENTER(("[CMiniportWaveRTStream::GetStreamChannelMute]"));
    ntStatus = GetChannelMute(_uiChannel, _pbMute);

    return ntStatus;
}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamAttributeSteppings 
 
Decscription:

    When handling volume, mute, and meter related KS properties, Portcls calls 
    this method, inside its property handlers, to know the property stepping information for the 
    corresponding KS property.

Parameters:

        _In_ _targetType:  the query target (volume. mute, or peak meter)
        _Out_ _pKsPropMembHead: a pointer to a PKSPROPERTY_STEPPING_LONG variable for receiving returned channel count information
        _In_ ulBufferSize: a pointer to a ULONG variable that has the size of the buffer pointed by _pKsPropMembHead

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::GetStreamAttributeSteppings(_In_  eChannelTargetType _targetType, _Out_ PKSPROPERTY_STEPPING_LONG _pKsPropMembHead, _In_  UINT32 _ui32DataSize)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::GetDeviceAttributeSteppings]"));

    switch (_targetType)
    {
        case eVolumeAttribute:
             ntStatus = GetVolumeSteppings(_pKsPropMembHead, _ui32DataSize);;
             break;
        case eMuteAttribute:
             ntStatus = GetMuteSteppings(_pKsPropMembHead, _ui32DataSize);;
             break;
        case ePeakMeterAttribute:
             ntStatus = GetPeakMeterSteppings(_pKsPropMembHead, _ui32DataSize);;
             break;
        default:
            ntStatus = STATUS_INVALID_DEVICE_REQUEST;
            break;
    }
     return ntStatus;

}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::SetStreamChannelVolume 
 
Decscription:

    When handling SET volume KS property for the device, Portcls calls 
    this method, inside its property handlers, to set the current setting on the specific channel.

Parameters:

        _In_ Channel:  the target channel for this GET volume operation
        _In_ TargetVolume: volume value to set 
        _In_ CurveType: type of curve to apply to the ramp
        _In_ CurveDuration: amount of time in hns over which to ramp the volume

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
STDMETHODIMP_(NTSTATUS) CMiniportWaveRTStream::SetStreamChannelVolume
(
    _In_ UINT32             Channel,
    _In_ LONG                TargetVolume,
    _In_ AUDIO_CURVE_TYPE   CurveType,
    _In_ ULONGLONG          CurveDuration
)
{
    UNREFERENCED_PARAMETER(CurveType);
    UNREFERENCED_PARAMETER(CurveDuration);
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::SetStreamChannelVolume]"));

    // Snap the volume level to our range of steppings.
    LONG lVolume = VOLUME_NORMALIZE_IN_RANGE(TargetVolume); 

    // If Channel is ALL_CHANNELS_ID, then set the level on all channels
    if ( ALL_CHANNELS_ID == Channel )
    {
        for (UINT32 i = 0; i < m_pWfExt->Format.nChannels; i++)
        {
            ntStatus = SetChannelVolume(i, lVolume);
        }
    }
    else
    {
        ntStatus = SetChannelVolume(Channel, lVolume);
    }

    return ntStatus;
}

#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamChannelPeakMeter 
 
Decscription:

    When handling GET peak meter KS property for the device, Portcls calls 
    this method, inside its property handlers, to get the current setting on the specific channel.

Parameters:

        _In_ _uiChannel:  the target channel for this GET peak meter operation
    _Out_ _pPeakMeterValue: a pointer to a LONG variable for receiving returned information

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
STDMETHODIMP_(NTSTATUS) CMiniportWaveRTStream::GetStreamChannelPeakMeter(_In_ UINT32 _uiChannel, _Out_  LONG  *_pPeakMeterValue)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::GetStreamChannelPeakMeter]"));

    ntStatus = GetChannelPeakMeter(_uiChannel, _pPeakMeterValue);

    return ntStatus;
}

#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::SetStreamChannelMute 
 
Decscription:

    When handling SET mute KS property for the device, Portcls calls 
    this method, inside its property handlers, to set the current setting on the specific channel.

Parameters:

        _In_ _uiChannel:  the target channel for this Set mute operation
        _In_ _bMute: mute value to set 

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::SetStreamChannelMute(_In_ UINT32 _uiChannel, _In_  BOOL  _bMute)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::SetStreamChannelMute]"));
    
    // If Channel is ALL_CHANNELS_ID, then set the mute info on all channels
    if ( ALL_CHANNELS_ID == _uiChannel )
    {
        for (UINT32 i = 0; i < m_pWfExt->Format.nChannels; i++)
        {
            ntStatus = SetChannelMute(i, _bMute);
        }
    }
    else
    {
        ntStatus = SetChannelMute(_uiChannel, _bMute);
    }
 
    return ntStatus;
}
//presentation
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamPresentationPosition 
 
Decscription:

    Portcls calls this method, inside its property handlers, to get a stream's presentation 
    postion.

Parameters:

        _Out_ pPresentationPosition: a pointer to a KSAUDIO_PRESENTATION_POSITION variable for receiving returned information

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::GetStreamPresentationPosition(_Out_ KSAUDIO_PRESENTATION_POSITION *_pPresentationPosition)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();
    ASSERT(_pPresentationPosition);
    DPF_ENTER(("[CMiniportWaveRTStream::GetStreamPresentationPosition]"));

    ntStatus = GetPresentationPosition(_pPresentationPosition);
 
    return ntStatus;
}
//StreamCurrentWritePosition
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::SetStreamCurrentWritePosition 
 
Decscription:

    Portcls calls this method, inside its property handlers, to set a stream's write position 
    postion.

Parameters:

        _In_ ulCurrentWritePosition: a position value indicating the last valid byte in the buffer

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

_ulCurrentWritePosition specifies the current write position in bytes, which an offload driver can use to know how much valid data in the WaveRT buffer.
Let say, BufferByteSize is the WaveRT buffer size,  the valid value range for _ulCurrentWritePosition be 0... BufferByteSize (inclusive)

0: no valid data in the buffer
n: the last valid data byte is at the offset n from the beginning of the buffer. If this is a value smaller than a previous write position, 
it means a wrap-around has happened, in the case, the driver would need to take that in to consider when calculating how many bytes are valid for fetching.
BufferByteSize: the last valid data byte is at the end of the buffer

After a pin is instantiated and before any KSPROPERTY_AUDIO_WAVERT_CURRENT_WRITE_POSITION is received, a driver should assume its current write position is zero.

-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::SetStreamCurrentWritePosition(_In_ ULONG _ulCurrentWritePosition)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();
    DPF_ENTER(("[CMiniportWaveRTStream::SetStreamCurrentWritePosition]"));

    ntStatus = SetCurrentWritePosition(_ulCurrentWritePosition);

    return ntStatus;
}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamLinearBufferPosition 
 
Decscription:

    Portcls calls this method, inside its property handlers, to set a stream's write position 
    postion.

Parameters:

        _Out_ pullLinearBufferPosition: a pointer to a ULONGLONG variable for receiving returned information


Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks
The returned  value is the number of bytes that the DMA has fetched from the audio buffer since the beginning of the stream
-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::GetStreamLinearBufferPosition(_Out_ ULONGLONG *_pullLinearBufferPosition)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();
    ASSERT(_pullLinearBufferPosition);
    DPF_ENTER(("[CMiniportWaveRTStream::GetStreamLinearBufferPosition]"));

    ntStatus = GetPositions(_pullLinearBufferPosition, NULL, NULL);
 
    return ntStatus;
}
//SetStreamLoopbackProtection
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::SetStreamLoopbackProtection 
 
Decscription:

    Portcls calls this method, inside its property handlers, to set the loopback
    protection option

Parameters:
        _In_ protectionOption: protection option
                                 CONSTRICTOR_OPTION_DISABLE: Turn protection  off.
                                 CONSTRICTOR_OPTION_MUTE: Mute the loopback stream
Return Value:

    Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks
    This content protection settings requestion could come in from the host process
    pin or offload pin. The miniport needs to mute its loopback stream contents for 
    for this request from either host process pin or offload pin.
-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::SetStreamLoopbackProtection(_In_ CONSTRICTOR_OPTION protectionOption)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();
    DPF_ENTER(("[CMiniportWaveRTStream::SetStreamLoopbackProtection]"));

    //
    // Miniport driver mutes/unmutes the loopback here.
    // 
    ntStatus = m_pMiniport->SetLoopbackProtection(protectionOption);
    
    return ntStatus;
}
#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
IMiniportStreamAudioEngineNode::GetStreamChannelCount 
 
Decscription:

    When handling volume, mute, and meter related KS properties, Portcls calls 
    this method, inside its property handlers, to know the number of channels for the 
    corresponding KS property.

Parameters:

        _In_ _targetType:  the query target (volume, mute, or peak meter)
        _Out_ _pulChannelCount: a pointer to a UINT32 variable for receiving returned channel count information

Return Value:

   Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks

-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::GetStreamChannelCount(_In_  eChannelTargetType    _targetType,_Out_  UINT32 *_pulChannelCount)
{
    NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;

    PAGED_CODE ();

    DPF_ENTER(("[CMiniportWaveRTStream::GetStreamChannelCount]"));

    switch (_targetType)
    {
        case eVolumeAttribute:
             ntStatus = GetVolumeChannelCount(_pulChannelCount);
             break;
        case eMuteAttribute:
             ntStatus = GetMuteChannelCount(_pulChannelCount);
             break;
        case ePeakMeterAttribute:
             ntStatus = GetPeakMeterChannelCount(_pulChannelCount);
             break;
        default:
            ntStatus = STATUS_INVALID_DEVICE_REQUEST;
            break;
    }

    return ntStatus;
}


//------------------------------------------------------------------------------------------------------------------------
//CMiniportWaveRTStream private supporting functions
//------------------------------------------------------------------------------------------------------------------------
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetVolumeChannelCount(_Out_ UINT32 *_puiChannelCount)
{
    PAGED_CODE();

    DPF_ENTER(("[CMiniportWaveRTStream::GetVolumeChannelCount]"));

    ASSERT(_puiChannelCount);
    ASSERT(m_pWfExt);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    *_puiChannelCount = m_pWfExt->Format.nChannels;
    return ntStatus;
}

#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetVolumeSteppings(_Out_writes_bytes_(_ui32DataSize)  PKSPROPERTY_STEPPING_LONG _pKsPropStepLong, _In_  UINT32 _ui32DataSize)
{
    PAGED_CODE ();
    UINT32 ulChannelCount = _ui32DataSize / sizeof(KSPROPERTY_STEPPING_LONG);
    ASSERT (_pKsPropStepLong);
    DPF_ENTER(("[CMiniportWaveRTStream::GetVolumeSteppings]"));

    if (ulChannelCount != m_pWfExt->Format.nChannels)
    {
        return STATUS_INVALID_PARAMETER;
    }

    for(UINT i = 0; i < ulChannelCount; i++)
    {
        _pKsPropStepLong[i].SteppingDelta = VOLUME_STEPPING_DELTA;
        _pKsPropStepLong[i].Bounds.SignedMaximum = VOLUME_SIGNED_MAXIMUM;
        _pKsPropStepLong[i].Bounds.SignedMinimum = VOLUME_SIGNED_MINIMUM;
    }

    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetChannelVolume(_In_  UINT32 _uiChannel, _Out_  LONG *_pVolume)
{
    PAGED_CODE ();
    ASSERT (_pVolume);
    DPF_ENTER(("[CMiniportWaveRTStream::GetChannelVolume]"));

    *_pVolume = m_plVolumeLevel[_uiChannel];

    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::SetChannelVolume(_In_  UINT32 _uiChannel, _In_  LONG _Volume)
{
    PAGED_CODE ();
    DPF_ENTER(("[CMiniportWaveRTStream::SetChannelVolume]"));

    m_plVolumeLevel[_uiChannel] = _Volume;

    return STATUS_SUCCESS;
}
///- metering
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetPeakMeterChannelCount(_Out_ UINT32 *puiChannelCount)
{
    PAGED_CODE();

    DPF_ENTER(("[CMiniportWaveRTStream::GetPeakMeterChannelCount]"));

    ASSERT(puiChannelCount);
    ASSERT(m_pWfExt);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    *puiChannelCount = m_pWfExt->Format.nChannels;
    return ntStatus;
}

#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetPeakMeterSteppings(_Out_writes_bytes_(_ui32DataSize) PKSPROPERTY_STEPPING_LONG _pKsPropStepLong, _In_  UINT32 _ui32DataSize)
{
    PAGED_CODE ();
    UINT32 ulChannelCount = _ui32DataSize / sizeof(KSPROPERTY_STEPPING_LONG);

    ASSERT (_pKsPropStepLong);
    DPF_ENTER(("[CMiniportWaveRTStream::GetPeakMeterSteppings]"));

    if (ulChannelCount != m_pWfExt->Format.nChannels)
    {
        return STATUS_INVALID_PARAMETER;
    }

    for(UINT i = 0; i < ulChannelCount; i++)
    {
        _pKsPropStepLong[i].SteppingDelta = PEAKMETER_STEPPING_DELTA;
        _pKsPropStepLong[i].Bounds.SignedMaximum = PEAKMETER_SIGNED_MAXIMUM;
        _pKsPropStepLong[i].Bounds.SignedMinimum = PEAKMETER_SIGNED_MINIMUM;
    }

    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetChannelPeakMeter(_In_  UINT32 _uiChannel, _Out_  LONG *_plPeakMeter)
{
    PAGED_CODE ();
    ASSERT (_plPeakMeter);
    UNREFERENCED_PARAMETER(_uiChannel);
    DPF_ENTER(("[CMiniportWaveRTStream::GetChannelPeakMeter]"));

    *_plPeakMeter = PEAKMETER_NORMALIZE_IN_RANGE(PEAKMETER_SIGNED_MAXIMUM / 2);

    return STATUS_SUCCESS;
}

 //=============================================================================
// stream Mute
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetMuteChannelCount(_Out_ UINT32 *puiChannelCount)
{
    PAGED_CODE();

    DPF_ENTER(("[CMiniportWaveRTStream::GetMuteChannelCount]"));

    ASSERT(puiChannelCount);
    ASSERT(m_pWfExt);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    *puiChannelCount = m_pWfExt->Format.nChannels;
    return ntStatus;
}

#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetMuteSteppings(_Out_writes_bytes_(_ui32DataSize) PKSPROPERTY_STEPPING_LONG _pKsPropStepLong, _In_  UINT32 _ui32DataSize)
{
    PAGED_CODE ();
    UINT32 ulChannelCount = _ui32DataSize / sizeof(KSPROPERTY_STEPPING_LONG);

    ASSERT (_pKsPropStepLong);
    DPF_ENTER(("[CMiniportWaveRTStream::GetMuteSteppings]"));

    if (ulChannelCount != m_pWfExt->Format.nChannels)
    {
        return STATUS_INVALID_PARAMETER;
    }

    for(UINT i = 0; i < ulChannelCount; i++)
    {
        _pKsPropStepLong[i].SteppingDelta = 1;
        _pKsPropStepLong[i].Bounds.SignedMaximum = TRUE;
        _pKsPropStepLong[i].Bounds.SignedMinimum = FALSE;
    }

    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::GetChannelMute(_In_  UINT32 _uiChannel, _Out_  BOOL *_pbMute)
{
    PAGED_CODE ();
    ASSERT (_pbMute);
    DPF_ENTER(("[CMiniportWaveRTStream::GetChannelMute]"));

    *_pbMute = m_pbMuted[_uiChannel];
    return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
NTSTATUS CMiniportWaveRTStream::SetChannelMute(_In_  UINT32 _uiChannel, _In_  BOOL _bMute)
{
    PAGED_CODE ();
    DPF_ENTER(("[CMiniportWaveRTStream::SetChannelMute]"));

    m_pbMuted[_uiChannel] = _bMute;

    return STATUS_SUCCESS;
}
//presentation
#pragma code_seg()
//
//  UINT64 u64PositionInBlocks; // The block offset from the start of the stream to the current post-decoded uncompressed 
//                              // position in the stream, where a block is the group of channels in the same sample; for a PCM stream, 
//                              // a block is same as a frame. For compressed formats, a block is a single sample within a frame 
//                              // (eg. each MP3 frame has 1152 samples or 1152 blocks) 
//  UINT64 u64QPCPosition;      // The value of the performance counter at the time that the audio endpoint device read the device 
//                              // position (*pu64Position) in response to the KSAUDIO_PRESENTATION_POSITION call.

NTSTATUS CMiniportWaveRTStream::GetPresentationPosition(_Out_  KSAUDIO_PRESENTATION_POSITION *_pPresentationPosition)
{
    ASSERT (_pPresentationPosition);
    LARGE_INTEGER timeStamp;
    PADAPTERCOMMON pAdapterComm = m_pMiniport->GetAdapterCommObj();

    DPF_ENTER(("[CMiniportWaveRTStream::GetPresentationPosition]"));

    ULONGLONG ullLinearPosition = {0};
    ULONGLONG ullPresentationPosition = {0};
    NTSTATUS status = STATUS_SUCCESS;
    
    status = GetPositions(&ullLinearPosition, &ullPresentationPosition, &timeStamp);
    if (!NT_SUCCESS(status)) 
    { 
        return status;
    }

    _pPresentationPosition->u64PositionInBlocks = ullPresentationPosition * m_pWfExt->Format.nSamplesPerSec / m_pWfExt->Format.nAvgBytesPerSec;
    _pPresentationPosition->u64QPCPosition = (UINT64)timeStamp.QuadPart;

    //Event type: eMINIPORT_GET_PRESENTATION_POSITION
    //Parameter 1: Current linear buffer position    
    //Parameter 2: Previous WaveRtBufferWritePosition that the driver received    
    //Parameter 3: Presentation position
    //Parameter 4: 0
    pAdapterComm->WriteEtwEvent(eMINIPORT_GET_PRESENTATION_POSITION,
                                ullLinearPosition, // replace with the correct "Current linear buffer position"    
                                m_ulCurrentWritePosition,
                                _pPresentationPosition->u64PositionInBlocks,
                                0);  // always zero
    return STATUS_SUCCESS;
}

#pragma code_seg()
NTSTATUS CMiniportWaveRTStream::SetCurrentWritePosition(_In_  ULONG _ulCurrentWritePosition)
{
    DPF_ENTER(("[CMiniportWaveRTStream::SetCurrentWritePosition]"));
    
    NTSTATUS ntStatus;

#if defined(SYSVAD_BTH_BYPASS) || defined (SYSVAD_USB_SIDEBAND)
    if (m_SidebandStarted)
    {
        ntStatus = GetSidebandStreamNtStatus();
        IF_FAILED_JUMP(ntStatus, Done);
    }
#endif // defined(SYSVAD_BTH_BYPASS) || defined (SYSVAD_USB_SIDEBAND)

    //
    // Basic validation. WritePosition indicates the position (1-based) of the last valid byte.
    //
    if (_ulCurrentWritePosition == 0)
    {
        ntStatus = STATUS_INVALID_DEVICE_REQUEST;
        goto Done;
    }

    KIRQL oldIrql;
    KeAcquireSpinLock(&m_PositionSpinLock, &oldIrql);
    ntStatus = SetCurrentWritePositionInternal(_ulCurrentWritePosition);
    KeReleaseSpinLock(&m_PositionSpinLock, oldIrql);

Done:
    return ntStatus;
}

#pragma code_seg()
NTSTATUS CMiniportWaveRTStream::SetCurrentWritePositionInternal(_In_  ULONG _ulCurrentWritePosition)
{
    DPF_ENTER(("[CMiniportWaveRTStream::SetCurrentWritePositionInternal]"));
    
    ASSERT(m_bEoSReceived == FALSE);

    if (m_bEoSReceived)
    {
        return STATUS_INVALID_DEVICE_REQUEST;
    }

    if (_ulCurrentWritePosition > m_ulDmaBufferSize)
    {
        return STATUS_INVALID_DEVICE_REQUEST;
    }
    
    PADAPTERCOMMON pAdapterComm = m_pMiniport->GetAdapterCommObj();

    //Event type: eMINIPORT_SET_WAVERT_BUFFER_WRITE_POSITION
    //Parameter 1: Current linear buffer position    
    //Parameter 2: Previous WaveRtBufferWritePosition that the driver received    
    //Parameter 3: Target WaveRtBufferWritePosition received from portcls
    //Parameter 4: 0
    pAdapterComm->WriteEtwEvent(eMINIPORT_SET_WAVERT_BUFFER_WRITE_POSITION, 
                                m_ullLinearPosition, // replace with the correct "Current linear buffer position"    
                                m_ulCurrentWritePosition,
                                _ulCurrentWritePosition, // this is new write position
                                0); // always zero

    //
    // Check for eMINIPORT_GLITCH_REPORT - Same WaveRT buffer write during event driven mode.
    //
    if (m_ulNotificationIntervalMs > 0)
    {
        if (m_ulCurrentWritePosition == _ulCurrentWritePosition)
        {
            //Event type: eMINIPORT_GLITCH_REPORT
            //Parameter 1: Current linear buffer position 
            //Parameter 2: Previous WaveRtBufferWritePosition that the driver received 
            //Parameter 3: Major glitch code: 3: Received same WaveRT buffer twice in a row during event driven mode
            //Parameter 4: Minor code for the glitch cause
            pAdapterComm->WriteEtwEvent(eMINIPORT_GLITCH_REPORT, 
                                        m_ullLinearPosition, // replace with the correct "Current linear buffer position"
                                        m_ulCurrentWritePosition,
                                        3, // received same WaveRT buffer twice in a row during event driven mode
                                        _ulCurrentWritePosition);
        }
    }
    
    m_ulCurrentWritePosition = _ulCurrentWritePosition;
    InterlockedExchange(&m_IsCurrentWritePositionUpdated, 1);

    return STATUS_SUCCESS;
}

//linear and presentation positions
#pragma code_seg()
NTSTATUS CMiniportWaveRTStream::GetPositions
(
    _Out_opt_  ULONGLONG *      _pullLinearBufferPosition, 
    _Out_opt_  ULONGLONG *      _pullPresentationPosition, 
    _Out_opt_  LARGE_INTEGER *  _pliQPCTime
)
{
    DPF_ENTER(("[CMiniportWaveRTStream::GetPositions]"));

    NTSTATUS        ntStatus;
    LARGE_INTEGER   ilQPC;
    KIRQL           oldIrql;
#if defined(SYSVAD_BTH_BYPASS) || defined(SYSVAD_USB_SIDEBAND)
    if (m_SidebandStarted)
    {
        ntStatus = GetSidebandStreamNtStatus();
        IF_FAILED_JUMP(ntStatus, Done);
    }
#endif // defined(SYSVAD_BTH_BYPASS) || defined(SYSVAD_USB_SIDEBAND)

    // Update *_pullLinearBufferPosition with the the number of bytes fetched from waveRT ever since a stream got set into RUN
    // state.
    // Once the stream is set to STOP state, any further read on this call would return zero.

    //
    // Get the current time and update position.
    //
    KeAcquireSpinLock(&m_PositionSpinLock, &oldIrql);
    ilQPC = KeQueryPerformanceCounter(NULL);
    if (m_KsState == KSSTATE_RUN)
    {
        UpdatePosition(ilQPC);
    }
    if (_pullLinearBufferPosition)
    {
        *_pullLinearBufferPosition = m_ullLinearPosition;
    }
    if (_pullPresentationPosition)
    {
        *_pullPresentationPosition = m_ullPresentationPosition;
    }
    KeReleaseSpinLock(&m_PositionSpinLock, oldIrql);
    if (_pliQPCTime)
    {
        *_pliQPCTime = ilQPC;
    }

    ntStatus = STATUS_SUCCESS;

#if defined(SYSVAD_BTH_BYPASS) || defined(SYSVAD_USB_SIDEBAND)
Done:
#endif // defined(SYSVAD_BTH_BYPASS) || defined(SYSVAD_USB_SIDEBAND)
    return ntStatus;
}

#pragma code_seg("PAGE")
/*-----------------------------------------------------------------------------
CMiniportWaveRTStream::SetLoopbackProtection
 
Decscription:

    Portcls calls this method, inside its property handlers, to set the loopback
    protection option

Parameters:
        _In_ protectionOption: protection option
                                 CONSTRICTOR_OPTION_DISABLE: Turn protection  off.
                                 CONSTRICTOR_OPTION_MUTE: Mute the loopback stream
Return Value:

    Appropriate NTSTATUS code

Called at PASSIVE_LEVEL

Remarks
    This content protection settings requestion could come in from the host process
    pin or offload pin. The miniport needs to mute its loopback stream contents for 
    for this request from either host process pin or offload pin.
-------------------------------------------------------------------------------------------------------------------------*/
NTSTATUS CMiniportWaveRTStream::SetLoopbackProtection(_In_ CONSTRICTOR_OPTION protectionOption)
{
    PAGED_CODE ();
    DPF_ENTER(("[CMiniportWaveRTStream::SetLoopbackProtection]"));

    //
    // Miniport driver mutes/unmutes the loopback here.
    // 
    m_ToneGenerator.SetMute(protectionOption == CONSTRICTOR_OPTION_MUTE);
    
    return STATUS_SUCCESS;
}

#pragma code_seg()
//----------------------------------------------------------------------------------
// Description:
//      Set current write position for the very last buffer in a stream
//
// Parameters:
//      ULONG _ulWritePosition: point to the last valid data byte
//
// Remark:
//
//      In Win 8, for offload streams, double buffering mechanism is used and the buffer completion contact
//      between the audio stack and driver is: a driver signals buffer completion only when it reaches the
//      end of either buffer. Thus, there is no concept of partial buffer (not all the data in a buffer are  
//      valid, only the first N frames are valid and the rest of the buffer might contain invalid or silence 
//      data. So, when the very last buffer is partially filled, its completion will not get singled until 
//      all data is consumed, which might become undesirable when the size of a buffer is large and valid frame
//      count is low.
//      In WinBlue, a driver implements IMiniportStreamAudioEngineNode2 to claim being able to handle the very 
//      last buffer that's partial filled. 
//      For a driver claiming supporting IMiniportStreamAudioEngineNode2, for the very last buffer position write,
//      it will receive the very last buffer write via SetStreamCurrentWritePositionForLastBuffer() call instead of 
//      SetStreamCurrentWritePosition(). A driver should check _ulWritePosition parameter to see how much valid data
//      in the buffer (instead of blindly assuming all the data are valid) before signaling buffer completion.
//      No more SetStreamCurrentWritePositionForLastBuffer or SetStreamCurrentWritePosition will be called after this 
//      SetStreamCurrentWritePositionForLastBuffer() call.
//-----------------------------------------------------------------------------------------------------------------------

NTSTATUS CMiniportWaveRTStream::SetStreamCurrentWritePositionForLastBuffer(_In_ ULONG _ulWritePosition)
{
    DPF_ENTER(("[CMiniportWaveRT::SetStreamCurrentWritePositionForLastBuffer]"));
    NTSTATUS        ntStatus;
    KIRQL           oldIrql;

    KeAcquireSpinLock(&m_PositionSpinLock, &oldIrql);

    // Miniport driver needs to prepare to signal buffer completion event
    // when it's done with reading the last valid byte - an _ulWritePosition offset from the beginning WaveRT buffer
    // Note: _ulWritePosition will be smaller than buffer size in most of the cases 
    ntStatus = SetCurrentWritePositionInternal(_ulWritePosition);
    if (NT_SUCCESS(ntStatus))
    {
        m_bEoSReceived = TRUE;
    }

    KeReleaseSpinLock(&m_PositionSpinLock, oldIrql);

    return ntStatus;
}