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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
StreamEngine.cpp
Abstract:
Virtual Streaming Engine - this module controls streaming logic for
the device.
Environment:
Kernel mode
--*/
#include "private.h"
#include <devguid.h>
#include "stdunk.h"
#include <ks.h>
#include <mmsystem.h>
#include <ksmedia.h>
#include "streamengine.h"
#ifndef __INTELLISENSE__
#include "streamengine.tmh"
#endif
_Use_decl_annotations_
PAGED_CODE_SEG
CStreamEngine::CStreamEngine(
ACXSTREAM Stream,
ACXDATAFORMAT StreamFormat,
CSimPeakMeter *circuitPeakmeter
)
: m_PacketsCount(0),
m_PacketSize(0),
m_FirstPacketOffset(0),
m_NotificationTimer(NULL),
m_CurrentState(AcxStreamStateStop),
m_CurrentPacket(0),
m_Position(0),
m_Stream(Stream),
m_StreamFormat(StreamFormat),
m_StartTime(0),
m_StartPosition(0),
m_GlitchAdjust(0),
m_pCircuitPeakmeter(circuitPeakmeter)
{
PAGED_CODE();
KeQueryPerformanceCounter(&m_PerformanceCounterFrequency);
RtlZeroMemory(m_Packets, sizeof(m_Packets));
}
_Use_decl_annotations_
#pragma code_seg()
CStreamEngine::~CStreamEngine()
{
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::AllocateRtPackets(
ULONG PacketCount,
ULONG PacketSize,
PACX_RTPACKET * Packets
)
{
PAGED_CODE();
NTSTATUS status = STATUS_SUCCESS;
PVOID packetBuffer = NULL;
PACX_RTPACKET packets = NULL;
auto exit = scope_exit([&]() {
if (packetBuffer)
{
ExFreePoolWithTag(packetBuffer, DRIVER_TAG);
}
if (packets)
{
FreeRtPackets(packets, PacketCount);
}
});
RETURN_NTSTATUS_IF_TRUE(PacketCount > MAX_PACKET_COUNT, STATUS_INVALID_PARAMETER);
size_t packetsSize = 0;
RETURN_NTSTATUS_IF_FAILED(RtlSizeTMult(PacketCount, sizeof(ACX_RTPACKET), &packetsSize));
#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "On error packets gets freed inside scope_exit.")
packets = (PACX_RTPACKET)ExAllocatePool2(POOL_FLAG_NON_PAGED, packetsSize, DRIVER_TAG);
RETURN_NTSTATUS_IF_TRUE(!packets, STATUS_NO_MEMORY);
// ExAllocatePool2 zeros memory.
// We need to allocate page-aligned buffers, to ensure no kernel memory leaks
// to user space. Round up the packet size to page aligned, then calculate
// the first packet's buffer offset so packet 0 ends on a page boundary and
// packet 1 begins on a page boundary.
ULONG packetAllocSizeInPages = 0;
ULONG packetAllocSizeInBytes = 0;
ULONG firstPacketOffset = 0;
RETURN_NTSTATUS_IF_FAILED(RtlULongAdd(PacketSize, PAGE_SIZE - 1, &packetAllocSizeInPages));
packetAllocSizeInPages = packetAllocSizeInPages / PAGE_SIZE;
packetAllocSizeInBytes = PAGE_SIZE * packetAllocSizeInPages;
firstPacketOffset = packetAllocSizeInBytes - PacketSize;
ULONG i;
for (i = 0; i < PacketCount; ++i)
{
PMDL pMdl = NULL;
ACX_RTPACKET_INIT(&packets[i]);
packetBuffer = ExAllocatePool2(POOL_FLAG_NON_PAGED, packetAllocSizeInBytes, DRIVER_TAG);
RETURN_NTSTATUS_IF_TRUE(packetBuffer == NULL, STATUS_NO_MEMORY);
// ExAllocatePool2 zeros memory.
pMdl = IoAllocateMdl(packetBuffer, packetAllocSizeInBytes, FALSE, FALSE, NULL);
RETURN_NTSTATUS_IF_TRUE(pMdl == NULL, STATUS_NO_MEMORY);
MmBuildMdlForNonPagedPool(pMdl);
WDF_MEMORY_DESCRIPTOR_INIT_MDL(
&((packets)[i].RtPacketBuffer),
pMdl,
packetAllocSizeInBytes);
packets[i].RtPacketSize = PacketSize;
if (i == 0)
{
packets[i].RtPacketOffset = firstPacketOffset;
}
else
{
packets[i].RtPacketOffset = 0;
}
m_Packets[i] = packetBuffer;
packetBuffer = NULL;
}
*Packets = packets;
packets = NULL;
m_PacketsCount = PacketCount;
m_PacketSize = PacketSize;
m_FirstPacketOffset = firstPacketOffset;
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
VOID
CStreamEngine::FreeRtPackets(
PACX_RTPACKET Packets,
ULONG PacketCount
)
{
ULONG i;
PVOID buffer;
PAGED_CODE();
for (i = 0; i < PacketCount; ++i)
{
if (Packets[i].RtPacketBuffer.u.MdlType.Mdl)
{
buffer = MmGetMdlVirtualAddress(Packets[i].RtPacketBuffer.u.MdlType.Mdl);
IoFreeMdl(Packets[i].RtPacketBuffer.u.MdlType.Mdl);
ExFreePool(buffer);
}
}
ExFreePool(Packets);
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::PrepareHardware()
{
PAGED_CODE();
NTSTATUS status = STATUS_SUCCESS;
WDF_TIMER_CONFIG timerConfig;
WDF_OBJECT_ATTRIBUTES timerAttributes;
WDF_TIMER_CONFIG_INIT(&timerConfig, CStreamEngine::s_EvtStreamPassCallback);
timerConfig.AutomaticSerialization = TRUE;
timerConfig.UseHighResolutionTimer = WdfTrue;
timerConfig.Period = 0;
WDF_OBJECT_ATTRIBUTES_INIT(&timerAttributes);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&timerAttributes, STREAM_TIMER_CONTEXT);
timerAttributes.ParentObject = m_Stream;
RETURN_NTSTATUS_IF_FAILED(WdfTimerCreate(
&timerConfig,
&timerAttributes,
&m_NotificationTimer
));
PSTREAM_TIMER_CONTEXT timerCtx;
timerCtx = GetStreamTimerContext(m_NotificationTimer);
timerCtx->StreamEngine = this;
m_CurrentState = AcxStreamStatePause;
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::ReleaseHardware()
{
PAGED_CODE();
if (m_NotificationTimer)
{
WdfTimerStop(m_NotificationTimer, TRUE);
WdfObjectDelete(m_NotificationTimer);
m_NotificationTimer = NULL;
}
KeFlushQueuedDpcs();
m_Position = 0;
m_GlitchAdjust = 0;
m_CurrentPacket = 0;
m_CurrentState = AcxStreamStateStop;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::Pause()
{
PAGED_CODE();
NTSTATUS status = STATUS_SUCCESS;
DrvLogInfo(g_SDCAVDspLog, FLAG_STREAM, L"CStreamEngine::Pause - from %d", m_CurrentState);
RETURN_NTSTATUS_IF_TRUE(m_CurrentState != AcxStreamStateRun, STATUS_INVALID_STATE_TRANSITION);
m_PeakMeter.StopStream();
if (m_pCircuitPeakmeter)
{
m_pCircuitPeakmeter->StopStream();
}
WdfTimerStop(m_NotificationTimer, TRUE);
// Save the position we paused at.
UpdatePosition();
m_CurrentState = AcxStreamStatePause;
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::Run()
{
NTSTATUS status = STATUS_SUCCESS;
PAGED_CODE();
DrvLogInfo(g_SDCAVDspLog, FLAG_STREAM, L"CStreamEngine::Run");
if (m_CurrentState != AcxStreamStatePause)
{
status = STATUS_INVALID_STATE_TRANSITION;
return status;
}
m_PeakMeter.StartStream();
if (m_pCircuitPeakmeter)
{
m_pCircuitPeakmeter->StartStream();
}
// Save the time and position - if we ran and paused previously, the StartTime and StartPosition will allow
// us to continue scheduling packet completions correctly, while still reporting absolute position from the
// start of the stream.
m_StartTime = KSCONVERT_PERFORMANCE_TIME(m_PerformanceCounterFrequency.QuadPart, KeQueryPerformanceCounter(NULL));
m_StartPosition = m_Position;
// Reset time we've lost to glitches
m_GlitchAdjust = 0;
ScheduleNextPass();
m_CurrentState = AcxStreamStateRun;
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::GetPresentationPosition(
PULONGLONG PositionInBlocks,
PULONGLONG QPCPosition
)
{
PAGED_CODE();
DrvLogVerbose(g_SDCAVDspLog, FLAG_STREAM, L"CStreamEngine::GetPresentationPosition");
ULONG blockAlign;
LARGE_INTEGER qpc;
blockAlign = AcxDataFormatGetBlockAlign(m_StreamFormat);
qpc = KeQueryPerformanceCounter(NULL);
// Update the position based on the current time
UpdatePosition();
*PositionInBlocks = m_Position / blockAlign;
*QPCPosition = (ULONGLONG)qpc.QuadPart;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::AssignDrmContentId(
ULONG DrmContentId,
PACXDRMRIGHTS DrmRights
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(DrmContentId);
UNREFERENCED_PARAMETER(DrmRights);
//
// At this point the driver should enforce the new DrmRights.
//
// HDMI render: if DigitalOutputDisable or CopyProtect is true, enable HDCP.
//
// From MSDN:
//
// This sample doesn't forward protected content, but if your driver uses
// lower layer drivers or a different stack to properly work, please see the
// following info from MSDN:
//
// "Before allowing protected content to flow through a data path, the system
// verifies that the data path is secure. To do so, the system authenticates
// each module in the data path beginning at the upstream end of the data path
// and moving downstream. As each module is authenticated, that module gives
// the system information about the next module in the data path so that it
// can also be authenticated. To be successfully authenticated, a module's
// binary file must be signed as DRM-compliant.
//
// Two adjacent modules in the data path can communicate with each other in
// one of several ways. If the upstream module calls the downstream module
// through IoCallDriver, the downstream module is part of a WDM driver. In
// this case, the upstream module calls the AcxDrmForwardContentToDeviceObject
// function to provide the system with the device object representing the
// downstream module. (If the two modules communicate through the downstream
// module's content handlers, the upstream module calls AcxDrmAddContentHandlers
// instead.)
//
// For more information, see MSDN's DRM Functions and Interfaces.
//
return STATUS_SUCCESS;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::GetHWLatency(
ULONG * FifoSize,
ULONG * Delay
)
{
PAGED_CODE();
*FifoSize = 128;
*Delay = 0;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
PAGED_CODE_SEG
CSimPeakMeter *
CStreamEngine::GetPeakMeter()
{
PAGED_CODE();
return &m_PeakMeter;
}
_Use_decl_annotations_
#pragma code_seg()
VOID
CStreamEngine::s_EvtStreamPassCallback(
WDFTIMER Timer
)
{
CStreamEngine * This;
PSTREAM_TIMER_CONTEXT timerCtx;
// Get our stream engine pointer from the timer context
timerCtx = GetStreamTimerContext(Timer);
This = timerCtx->StreamEngine;
// Call the StreamPassCallback for the engine
This->StreamPassCallback();
}
// This is run every time the stream timer fires
_Use_decl_annotations_
#pragma code_seg()
VOID
CStreamEngine::StreamPassCallback()
{
ULONGLONG completedPacket;
ULONGLONG qpcCompleted;
// Save the time at which we moved to the next packet
qpcCompleted = (ULONGLONG)KeQueryPerformanceCounter(NULL).QuadPart;
// Process the packet (e.g. save render to file/generate capture data)
ProcessPacket();
// We've completed a packet! Increment our currently active packet
completedPacket = (ULONG)InterlockedIncrement((LONG*)&m_CurrentPacket) - 1;
InterlockedExchange64(&m_LastPacketStart.QuadPart, m_CurrentPacketStart.QuadPart);
InterlockedExchange64(&m_CurrentPacketStart.QuadPart, qpcCompleted);
// Tell ACX we've completed the packet.
(void)AcxRtStreamNotifyPacketComplete(m_Stream, completedPacket, qpcCompleted);
// Schedule when our new current packet will finish
ScheduleNextPass();
}
_Use_decl_annotations_
#pragma code_seg()
VOID
CStreamEngine::ScheduleNextPass()
{
LONGLONG delay = 0;
ULONG bytesPerSecond;
ULONGLONG nextPacket = 0;
ULONGLONG nextPacketStartPosition = 0;
ULONGLONG nextPacketPositionFromLastPause = 0;
ULONGLONG nextPacketTimeFromLastPauseHns = 0;
ULONGLONG nextPacketTime = 0;
ULONGLONG currentTime;
BOOLEAN inTimerQueue = FALSE;
// Get the number of bytes per second from our stored stream format
bytesPerSecond = GetBytesPerSecond();
// Calculate the absolute position of the beginning of the next packet from the beginning of the stream
nextPacket = m_CurrentPacket + 1;
nextPacketStartPosition = nextPacket * m_PacketSize;
// Adjust next packet position to account for the last time we resumed from Pause
nextPacketPositionFromLastPause = nextPacketStartPosition - m_StartPosition;
// Convert from bytes to HNS (to prevent truncation, multiply first then divide)
nextPacketTimeFromLastPauseHns = nextPacketPositionFromLastPause * HNS_PER_SEC / bytesPerSecond;
// Next packet time is Time @ resume from Pause, offset for lost time due to glitch, with next packet time added
nextPacketTime = m_StartTime + m_GlitchAdjust + nextPacketTimeFromLastPauseHns;
currentTime = KSCONVERT_PERFORMANCE_TIME(m_PerformanceCounterFrequency.QuadPart, KeQueryPerformanceCounter(NULL));
// Determine how long we want to wait, in HNS. Negative since it's a relative wait
delay = -(LONGLONG)(nextPacketTime - currentTime);
// If the delay isn't negative, this means we lost some time (e.g. broken into kernel debugger). Update
// our glitch adjust to account for that lost time, and attempt to schedule again
if (delay >= 0)
{
// Glitch!!!
// Update the glitch adjustment and set the new delay.
m_GlitchAdjust += delay;
StreamPassCallback();
return;
}
// Start the timer for our next pass! Note the timer isn't periodic.
inTimerQueue = WdfTimerStart(m_NotificationTimer, delay);
// We shouldn't be scheduling our next pass if the timer was previously still pending
ASSERT(inTimerQueue == FALSE);
}
_Use_decl_annotations_
#pragma code_seg()
VOID
CStreamEngine::UpdatePosition()
{
ULONGLONG currentTime;
ULONG bytesPerSecond;
if (m_CurrentState != AcxStreamStateRun)
{
return;
}
bytesPerSecond = GetBytesPerSecond();
currentTime = KSCONVERT_PERFORMANCE_TIME(m_PerformanceCounterFrequency.QuadPart, KeQueryPerformanceCounter(NULL));
// Update position
m_Position = m_StartPosition - m_GlitchAdjust + (currentTime - m_StartTime) * bytesPerSecond / HNS_PER_SEC;
}
_Use_decl_annotations_
#pragma code_seg()
ULONG
CStreamEngine::GetBytesPerSecond()
{
ULONG bytesPerSecond;
bytesPerSecond = AcxDataFormatGetAverageBytesPerSec(m_StreamFormat);
return bytesPerSecond;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CStreamEngine::GetCurrentPacket(
PULONG CurrentPacket
)
{
ULONG currentPacket;
PAGED_CODE();
currentPacket = (ULONG)InterlockedCompareExchange((LONG*)&m_CurrentPacket, -1, -1);
*CurrentPacket = currentPacket;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
PAGED_CODE_SEG
CRenderStreamEngine::CRenderStreamEngine(
ACXSTREAM Stream,
ACXDATAFORMAT StreamFormat,
CSimPeakMeter *circuitPeakmeter
)
: CStreamEngine(Stream, StreamFormat, circuitPeakmeter)
{
PAGED_CODE();
}
_Use_decl_annotations_
PAGED_CODE_SEG
CRenderStreamEngine::~CRenderStreamEngine()
{
PAGED_CODE();
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CRenderStreamEngine::PrepareHardware()
{
PAGED_CODE();
NTSTATUS status = STATUS_SUCCESS;
RETURN_NTSTATUS_IF_FAILED(CStreamEngine::PrepareHardware());
// ignore failure
RETURN_NTSTATUS_IF_FAILED(m_SaveData.SetDataFormat((PKSDATAFORMAT)AcxDataFormatGetKsDataFormat(m_StreamFormat)));
// ignore failure
RETURN_NTSTATUS_IF_FAILED(m_SaveData.Initialize(FALSE));
// ignore failure
RETURN_NTSTATUS_IF_FAILED(m_SaveData.SetMaxWriteSize(m_PacketSize * m_PacketsCount * 16));
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CRenderStreamEngine::ReleaseHardware()
{
PAGED_CODE();
m_SaveData.WaitAllWorkItems();
m_SaveData.Cleanup();
return CStreamEngine::ReleaseHardware();
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CRenderStreamEngine::AssignDrmContentId(
ULONG DrmContentId,
PACXDRMRIGHTS DrmRights
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(DrmContentId);
//
// At this point the driver should enforce the new DrmRights.
// The sample driver handles DrmRights per stream basis, and
// stops writing the stream to disk, if CopyProtect = TRUE.
//
// HDMI render: if DigitalOutputDisable or CopyProtect is true, enable HDCP.
// Loopback: if CopyProtect is true, disable loopback stream.
//
//
// Sample writes each stream seperately to disk. If the rights for this
// stream indicates that the stream is CopyProtected, stop writing to disk.
//
m_SaveData.Disable(DrmRights->CopyProtect);
//
// From MSDN:
//
// This sample doesn't forward protected content, but if your driver uses
// lower layer drivers or a different stack to properly work, please see the
// following info from MSDN:
//
// "Before allowing protected content to flow through a data path, the system
// verifies that the data path is secure. To do so, the system authenticates
// each module in the data path beginning at the upstream end of the data path
// and moving downstream. As each module is authenticated, that module gives
// the system information about the next module in the data path so that it
// can also be authenticated. To be successfully authenticated, a module's
// binary file must be signed as DRM-compliant.
//
// Two adjacent modules in the data path can communicate with each other in
// one of several ways. If the upstream module calls the downstream module
// through IoCallDriver, the downstream module is part of a WDM driver. In
// this case, the upstream module calls the AcxDrmForwardContentToDeviceObject
// function to provide the system with the device object representing the
// downstream module. (If the two modules communicate through the downstream
// module's content handlers, the upstream module calls AcxDrmAddContentHandlers
// instead.)
//
// For more information, see MSDN's DRM Functions and Interfaces.
//
return STATUS_SUCCESS;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CRenderStreamEngine::SetRenderPacket(
ULONG Packet,
ULONG Flags,
ULONG EosPacketLength
)
{
NTSTATUS status = STATUS_SUCCESS;
ULONG currentPacket;
UNREFERENCED_PARAMETER(Flags);
UNREFERENCED_PARAMETER(EosPacketLength);
PAGED_CODE();
currentPacket = (ULONG)InterlockedCompareExchange((LONG*)&m_CurrentPacket, -1, -1);
if (Packet <= currentPacket)
{
//ASSERT(FALSE);
status = STATUS_DATA_LATE_ERROR;
}
else if (Packet > currentPacket + 1)
{
//ASSERT(FALSE);
status = STATUS_DATA_OVERRUN;
}
return status;
}
_Use_decl_annotations_
#pragma code_seg()
NTSTATUS
CRenderStreamEngine::GetLinearBufferPosition(
_Out_ PULONGLONG Position
)
{
NTSTATUS status;
ULONGLONG qpcIgnored = 0;
// For this sample, we're borrowing the Presentation Position.
// An actual device would return the position of the last byte
// read from the audio buffer, not the last byte presented to the user
status = GetPresentationPosition(Position, &qpcIgnored);
if (!NT_SUCCESS(status))
{
return status;
}
*Position *= AcxDataFormatGetBlockAlign(m_StreamFormat);
return STATUS_SUCCESS;
}
_Use_decl_annotations_
#pragma code_seg()
VOID
CRenderStreamEngine::ProcessPacket()
{
ULONG currentPacket;
ULONG packetIndex;
PBYTE packetBuffer;
currentPacket = (ULONG)InterlockedCompareExchange((LONG*)&m_CurrentPacket, -1, -1);
packetIndex = currentPacket % m_PacketsCount;
packetBuffer = (PBYTE)m_Packets[packetIndex];
// Packet 0 starts at an offset if the size isn't a multiple of page_size
if (packetIndex == 0)
{
packetBuffer += m_FirstPacketOffset;
}
m_SaveData.WriteData(packetBuffer, m_PacketSize);
}
_Use_decl_annotations_
PAGED_CODE_SEG
CCaptureStreamEngine::CCaptureStreamEngine(
ACXSTREAM Stream,
ACXDATAFORMAT StreamFormat
)
: CStreamEngine(Stream, StreamFormat, nullptr),
m_EnableWaveCapture(0)
{
PAGED_CODE();
m_CurrentPacketStart.QuadPart = 0;
m_LastPacketStart.QuadPart = 0;
RtlInitUnicodeString(&m_HostCaptureFileName, NULL);
RtlInitUnicodeString(&m_LoopbackCaptureFileName, NULL);
}
_Use_decl_annotations_
PAGED_CODE_SEG
CCaptureStreamEngine::~CCaptureStreamEngine()
{
PAGED_CODE();
RtlFreeUnicodeString(&m_HostCaptureFileName);
RtlFreeUnicodeString(&m_LoopbackCaptureFileName);
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CCaptureStreamEngine::PrepareHardware()
{
PAGED_CODE();
NTSTATUS status = STATUS_SUCCESS;
RETURN_NTSTATUS_IF_FAILED(CStreamEngine::PrepareHardware());
RETURN_NTSTATUS_IF_FAILED(ReadRegistrySettings());
if (m_EnableWaveCapture)
{
status = m_WaveReader.Init((PWAVEFORMATEXTENSIBLE)AcxDataFormatGetWaveFormatExtensible(m_StreamFormat),
&m_HostCaptureFileName);
if (!NT_SUCCESS(status))
{
m_EnableWaveCapture = FALSE;
}
}
if (!m_EnableWaveCapture)
{
status = m_ToneGenerator.Init(DEFAULT_FREQUENCY, (PWAVEFORMATEXTENSIBLE)AcxDataFormatGetWaveFormatExtensible(m_StreamFormat));
}
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CCaptureStreamEngine::ReleaseHardware()
{
PAGED_CODE();
if (m_EnableWaveCapture)
{
m_WaveReader.WaitAllWorkItems();
}
return CStreamEngine::ReleaseHardware();
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CCaptureStreamEngine::GetCapturePacket(
ULONG * LastCapturePacket,
ULONGLONG * QPCPacketStart,
BOOLEAN * MoreData
)
{
NTSTATUS status = STATUS_SUCCESS;
ULONG currentPacket;
LONGLONG qpcPacketStart;
PAGED_CODE();
currentPacket = (ULONG)InterlockedCompareExchange((LONG*)&m_CurrentPacket, -1, -1);
qpcPacketStart = InterlockedCompareExchange64(&m_LastPacketStart.QuadPart, -1, -1);
*LastCapturePacket = currentPacket - 1;
*QPCPacketStart = (ULONGLONG)qpcPacketStart;
*MoreData = FALSE;
return status;
}
_Use_decl_annotations_
#pragma code_seg()
VOID
CCaptureStreamEngine::ProcessPacket()
{
ULONG currentPacket;
ULONG packetIndex;
PBYTE packetBuffer;
currentPacket = (ULONG)InterlockedCompareExchange((LONG*)&m_CurrentPacket, -1, -1);
packetIndex = currentPacket % m_PacketsCount;
packetBuffer = (PBYTE)m_Packets[packetIndex];
// Packet 0 starts at an offset if the size isn't a multiple of page_size
if (packetIndex == 0)
{
packetBuffer += m_FirstPacketOffset;
}
if (m_EnableWaveCapture)
{
m_WaveReader.ReadWaveData(packetBuffer, m_PacketSize);
}
else
{
m_ToneGenerator.GenerateSine(packetBuffer, m_PacketSize);
}
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CCaptureStreamEngine::ReadRegistrySettings()
{
PAGED_CODE();
NTSTATUS status = STATUS_SUCCESS;
// TRUE only on SUCCESS
m_EnableWaveCapture = FALSE;
RTL_QUERY_REGISTRY_TABLE paramTable[] = {
// QueryRoutine Flags Name EntryContext DefaultType DefaultData DefaultLength
{ NULL, RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_TYPECHECK, L"EnableWaveCapture", &m_EnableWaveCapture, (REG_DWORD << RTL_QUERY_REGISTRY_TYPECHECK_SHIFT) | REG_DWORD, &m_EnableWaveCapture, sizeof(DWORD) },
{ NULL, RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_TYPECHECK, L"HostCaptureFileName", &m_HostCaptureFileName, (REG_SZ << RTL_QUERY_REGISTRY_TYPECHECK_SHIFT) | REG_SZ, &m_HostCaptureFileName, sizeof(UNICODE_STRING) },
{ NULL, RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_TYPECHECK, L"LoopbackCaptureFileName", &m_LoopbackCaptureFileName, (REG_SZ << RTL_QUERY_REGISTRY_TYPECHECK_SHIFT) | REG_SZ, &m_LoopbackCaptureFileName, sizeof(UNICODE_STRING) },
{ NULL, 0, NULL, NULL, 0, NULL, 0 }
};
UNICODE_STRING parametersPath;
RtlInitUnicodeString(¶metersPath, NULL);
// The sizeof(WCHAR) is added to the maximum length, for allowing a space for null termination of the string.
parametersPath.MaximumLength = g_RegistryPath.Length + sizeof(L"\\Parameters") + sizeof(WCHAR);
#pragma prefast(suppress:__WARNING_ALIASED_MEMORY_LEAK, "memory is freed by scope_exit")
parametersPath.Buffer = (PWCH)ExAllocatePool2(POOL_FLAG_PAGED, parametersPath.MaximumLength, DRIVER_TAG);
RETURN_NTSTATUS_IF_TRUE(parametersPath.Buffer == NULL, STATUS_INSUFFICIENT_RESOURCES);
auto parametersPath_free = scope_exit([¶metersPath]() {
ExFreePool(parametersPath.Buffer);
});
// ExAllocatePool2 zeros memory.
RtlAppendUnicodeToString(¶metersPath, g_RegistryPath.Buffer);
RtlAppendUnicodeToString(¶metersPath, L"\\Parameters");
RETURN_NTSTATUS_IF_FAILED(RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
parametersPath.Buffer,
¶mTable[0],
NULL,
NULL));
m_EnableWaveCapture = TRUE;
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
CBufferedCaptureStreamEngine::CBufferedCaptureStreamEngine(
_In_ ACXSTREAM Stream,
_In_ ACXDATAFORMAT StreamFormat,
_In_ CKeywordDetector * KeywordDetector
)
: CCaptureStreamEngine(Stream, StreamFormat),
m_KeywordDetector(KeywordDetector)
{
PAGED_CODE();
}
_Use_decl_annotations_
PAGED_CODE_SEG
CBufferedCaptureStreamEngine::~CBufferedCaptureStreamEngine()
{
PAGED_CODE();
}
// This is run every time the stream timer fires
_Use_decl_annotations_
#pragma code_seg()
VOID
CBufferedCaptureStreamEngine::StreamPassCallback()
{
LARGE_INTEGER qpc;
LARGE_INTEGER qpcFrequency;
BOOLEAN isRealtime = FALSE;
ULONGLONG completedPacket;
LONGLONG NewPacketNumber;
ULONGLONG NewPerformanceCount;
qpc = KeQueryPerformanceCounter(&qpcFrequency);
// As this is a simulation, we still want the ScheduleNextPass to
// keep producing data. To that end, update the current packet
// information used for production.
completedPacket = (ULONG)InterlockedIncrement((LONG*)&m_CurrentPacket) - 1;
InterlockedExchange64(&m_LastPacketStart.QuadPart, m_CurrentPacketStart.QuadPart);
InterlockedExchange64(&m_CurrentPacketStart.QuadPart, qpc.QuadPart);
// Add the next packet to the fifo queue
m_KeywordDetector->DpcRoutine(qpc.QuadPart, qpcFrequency.QuadPart, &isRealtime, &NewPacketNumber, &NewPerformanceCount);
if (isRealtime && (m_CurrentState == AcxStreamStateRun))
{
// We are running real time and just completed a packet, so notify.
(void)AcxRtStreamNotifyPacketComplete(m_Stream, NewPacketNumber, NewPerformanceCount);
}
// Schedule when our new current packet will finish
ScheduleNextPass();
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CBufferedCaptureStreamEngine::Pause()
{
PAGED_CODE();
m_KeywordDetector->Stop();
return CCaptureStreamEngine::Pause();
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CBufferedCaptureStreamEngine::Run()
{
PAGED_CODE();
ULONG FrontCapturePacket;
ULONGLONG QPCFrontPacket;
m_KeywordDetector->Run();
NTSTATUS status = CCaptureStreamEngine::Run();
NTSTATUS fifoStatus = m_KeywordDetector->GetFifoStart(&FrontCapturePacket, &QPCFrontPacket);
if (NT_SUCCESS(fifoStatus))
{
// We just entered the run state, so we need to trigger the packet completion for the first
// buffer in the fifo
(void)AcxRtStreamNotifyPacketComplete(m_Stream, FrontCapturePacket, QPCFrontPacket);
}
return status;
}
_Use_decl_annotations_
PAGED_CODE_SEG
NTSTATUS
CBufferedCaptureStreamEngine::GetCapturePacket(
_Out_ ULONG * LastCapturePacket,
_Out_ ULONGLONG * QPCPacketStart,
_Out_ BOOLEAN * MoreData
)
{
PAGED_CODE();
ULONG nextPacketNumber;
ULONGLONG nextQPCCount;
// retrieve the packet from the fifo queue
NTSTATUS status = m_KeywordDetector->GetReadPacket(m_PacketsCount, m_PacketSize, m_Packets, LastCapturePacket, QPCPacketStart, MoreData, &nextPacketNumber, &nextQPCCount);
if (NT_SUCCESS(status) && MoreData)
{
(void)AcxRtStreamNotifyPacketComplete(m_Stream, nextPacketNumber, nextQPCCount);
}
return status;
}
|