-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathCapture.cpp
More file actions
2303 lines (1842 loc) · 64.7 KB
/
Capture.cpp
File metadata and controls
2303 lines (1842 loc) · 64.7 KB
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
/**************************************************************************
A/V Stream Camera Sample
Copyright (c) 2001, Microsoft Corporation.
File:
capture.cpp
Abstract:
This is the implementation of the CCapturePin class.
CCapturePin wraps a PKSPIN object and does:
1. Object construction and cleanup,
2. State management,
3. Accepts new frame buffers for filling,
4. Completes frame buffers delivered by the simulation,
5. Negotiates for and sets new formats / data ranges, and
6. Manages allocator framing.
History:
created 3/8/2001
**************************************************************************/
#include "Common.h"
#include <ksmedia.h>
#include "ntintsafe.h"
/**************************************************************************
PAGEABLE CODE
**************************************************************************/
#ifdef ALLOC_PRAGMA
#pragma code_seg("PAGE")
#endif // ALLOC_PRAGMA
CCapturePin::
CCapturePin (
_In_ PKSPIN Pin
)
: m_Pin (Pin)
, m_PinState(PinStopped)
, m_Clock(nullptr)
, m_PendIo(FALSE)
, m_AcquiredResources(FALSE)
, m_VideoInfoHeader(nullptr)
, m_PreviousStreamPointer(nullptr)
, m_PresentationTime(0)
, m_FrameNumber(0)
, m_DroppedFrames(0)
, m_DesiredFrames(2)
/*++
Routine Description:
Construct a new capture pin.
Arguments:
Pin -
The AVStream pin object corresponding to the capture pin
Return Value:
None
--*/
{
PAGED_CODE();
NT_ASSERT(Pin);
PKSDEVICE Device = KsPinGetDevice (Pin);
NT_ASSERT(Device);
//
// Set up our device pointer. This gives us access to "hardware I/O"
// during the capture routines.
//
m_Device = CCaptureDevice::Recast(Device);
NT_ASSERT( m_Device );
m_Sensor = m_Device->GetSensor( KsPinGetParentFilter(Pin ) );
NT_ASSERT( m_Sensor );
}
CCapturePin::
~CCapturePin()
{
PAGED_CODE();
SAFE_FREE( m_VideoInfoHeader );
}
/*************************************************/
NTSTATUS
CCapturePin::
ReleaseAllFrames()
/*++
Routine Description:
Clean up any references we're holding on frames after we abruptly
stop the hardware.
Arguments:
None
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
PKSSTREAM_POINTER Clone = KsPinGetFirstCloneStreamPointer (m_Pin);
PKSSTREAM_POINTER NextClone = NULL;
//
// Walk through the clones, deleting them, and setting DataUsed to
// zero since we didn't use any data!
//
while (Clone)
{
NextClone = KsStreamPointerGetNextClone (Clone);
Clone->StreamHeader->DataUsed = 0;
KsStreamPointerDelete (Clone);
Clone = NextClone;
}
return STATUS_SUCCESS;
}
NTSTATUS
CCapturePin::
Initialize()
/*++
Routine Description:
Post construction initialization:
1. Add us to the Pin's bag,
2. Capture information about the image/video format, and
3. Update the allocator's framing to match.
Arguments:
None
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
DBG_ENTER("(Pin=%d)", m_Pin->Id);
NTSTATUS Status = STATUS_SUCCESS;
if( !m_Sensor )
{
// Fail if we couldn't find the sensor.
Status = STATUS_INVALID_PARAMETER;
}
else
{
//
// Add the item to the object bag if we we were successful.
// Whenever the pin closes, the bag is cleaned up and we will be
// freed.
//
Status = KsAddItemToObjectBag (
m_Pin->Bag,
this,
reinterpret_cast<PFNKSFREE> (Cleanup)
);
if( NT_SUCCESS(Status) )
{
m_Pin->Context = this;
}
}
//
// If we succeeded so far, stash the video info header away and change
// our allocator framing to reflect the fact that only now do we know
// the framing requirements based on the connection format.
//
if (NT_SUCCESS (Status))
{
Status = CaptureBitmapInfoHeader();
}
if (NT_SUCCESS(Status))
{
//
// Edit the framing descriptors to match our pin's requirements given the current format.
//
Status = UpdateAllocatorFraming();
}
DBG_LEAVE("(Pin=%d)=0x%08X", m_Pin->Id, Status);
return Status;
}
/*************************************************/
NTSTATUS
CCapturePin::
CaptureBitmapInfoHeader()
/*++
Routine Description:
Capture information about the image/video format for the pin.
Arguments:
None
Return Value:
Success / Failure
--*/
{
PAGED_CODE( );
const GUID ImageInfoSpecifier ={ STATICGUIDOF( KSDATAFORMAT_SPECIFIER_IMAGE ) };
const GUID VideoInfoSpecifier ={ STATICGUIDOF( KSDATAFORMAT_SPECIFIER_VIDEOINFO ) };
const GUID VideoInfo2Specifier = { STATICGUIDOF(KSDATAFORMAT_SPECIFIER_VIDEOINFO2) };
// Free any previous copy of these header.
SAFE_FREE(m_VideoInfoHeader);
if( IsEqualGUID( m_Pin->ConnectionFormat->Specifier, ImageInfoSpecifier ) &&
m_Pin->ConnectionFormat->FormatSize >= sizeof (KS_DATAFORMAT_IMAGEINFO) )
{
PKS_BITMAPINFOHEADER ConnectionHeader =
&((reinterpret_cast <PKS_DATAFORMAT_IMAGEINFO>
(m_Pin->ConnectionFormat))->ImageInfoHeader);
m_VideoInfoHeader = reinterpret_cast <PKS_VIDEOINFOHEADER> (
ExAllocatePoolZero (
NonPagedPoolNx,
max(sizeof(KS_VIDEOINFOHEADER), ConnectionHeader->biSize + KS_SIZE_PREHEADER),
AVSHWS_POOLTAG
)
);
if( !m_VideoInfoHeader )
{
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Copy the connection format bitmap info header into the newly
// allocated "captured" video info header.
//
m_VideoInfoHeader->bmiHeader = *ConnectionHeader;
PKS_BITMAPINFOHEADER CurrentBitmapInfoHeader = &(m_VideoInfoHeader->bmiHeader);
// If we don't have a known bit-depth (compressed formats), assume the worse-case.
if( CurrentBitmapInfoHeader->biBitCount==0 )
{
CurrentBitmapInfoHeader->biBitCount=32;
}
// Estimate the image size. The derived pin can chose to override this value.
CurrentBitmapInfoHeader->biSizeImage =
(CurrentBitmapInfoHeader->biWidth*CurrentBitmapInfoHeader->biHeight*CurrentBitmapInfoHeader->biBitCount)/8;
// Estimate a frame rate. The derived pin can chose to override this value.
m_VideoInfoHeader->AvgTimePerFrame = ONESECOND/30;
}
else
if( IsEqualGUID( m_Pin->ConnectionFormat->Specifier, VideoInfoSpecifier ) &&
m_Pin->ConnectionFormat->FormatSize >= sizeof (KS_DATAFORMAT_VIDEOINFOHEADER) )
{
PKS_VIDEOINFOHEADER ConnectionHeader =
&((reinterpret_cast <PKS_DATAFORMAT_VIDEOINFOHEADER>
(m_Pin->ConnectionFormat))->
VideoInfoHeader);
m_VideoInfoHeader = reinterpret_cast <PKS_VIDEOINFOHEADER> (
ExAllocatePoolZero (
NonPagedPoolNx,
max(sizeof(KS_VIDEOINFOHEADER), KS_SIZE_VIDEOHEADER (ConnectionHeader)),
AVSHWS_POOLTAG
)
);
if( !m_VideoInfoHeader )
{
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Copy the connection format video info header into the newly
// allocated "captured" video info header.
//
RtlCopyMemory (
m_VideoInfoHeader,
ConnectionHeader,
KS_SIZE_VIDEOHEADER (ConnectionHeader)
);
}
else
if( IsEqualGUID( m_Pin->ConnectionFormat->Specifier, VideoInfo2Specifier ) &&
m_Pin->ConnectionFormat->FormatSize >= sizeof (KS_DATAFORMAT_VIDEOINFOHEADER2) )
{
PKS_VIDEOINFOHEADER2 ConnectionHeader =
&((reinterpret_cast <PKS_DATAFORMAT_VIDEOINFOHEADER2>
(m_Pin->ConnectionFormat))->
VideoInfoHeader2);
m_VideoInfoHeader = reinterpret_cast <PKS_VIDEOINFOHEADER> (
ExAllocatePoolZero(
NonPagedPoolNx,
max(sizeof(KS_VIDEOINFOHEADER), ConnectionHeader->bmiHeader.biSize + KS_SIZE_PREHEADER),
AVSHWS_POOLTAG
)
);
if (!m_VideoInfoHeader)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Copy the connection format video info header into the newly
// allocated "captured" video info header.
//
m_VideoInfoHeader->rcSource = ConnectionHeader->rcSource;
m_VideoInfoHeader->rcTarget = ConnectionHeader->rcTarget;
m_VideoInfoHeader->dwBitRate = ConnectionHeader->dwBitRate;
m_VideoInfoHeader->dwBitErrorRate = ConnectionHeader->dwBitErrorRate;
m_VideoInfoHeader->AvgTimePerFrame = ConnectionHeader->AvgTimePerFrame;
RtlCopyMemory (
&m_VideoInfoHeader->bmiHeader,
&ConnectionHeader->bmiHeader,
ConnectionHeader->bmiHeader.biSize
);
}
else
{
DBG_TRACE("Connection Format was invalid");
return STATUS_INVALID_DEVICE_STATE;
}
DBG_TRACE( "+++ AvgTimePerFrame = %lld +++", m_VideoInfoHeader->AvgTimePerFrame );
return STATUS_SUCCESS;
}
/*************************************************/
NTSTATUS
CCapturePin::
SetState (
_In_ KSSTATE ToState,
_In_ KSSTATE FromState
)
/*++
Routine Description:
This is called when the caputre pin transitions state. The routine
attempts to acquire / release any hardware resources and start up
or shut down capture based on the states we are transitioning to
and away from.
Arguments:
ToState -
The state we're transitioning to
FromState -
The state we're transitioning away from
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
NTSTATUS Status = STATUS_SUCCESS;
DBG_ENTER( "( Pin=%d, ToState=%s, FromState=%s )\n",
m_Pin->Id, KSStateToStateName(ToState), KSStateToStateName(FromState) ) ;
switch (ToState)
{
case KSSTATE_STOP:
//
// First, stop the hardware if we actually did anything to it.
//
if (m_PinState != PinStopped)
{
Status = m_Sensor->Stop(m_Pin);
NT_ASSERT (NT_SUCCESS (Status));
m_PinState = PinStopped;
}
//
// We've stopped the "fake hardware". It has cleared out
// it's scatter / gather tables and will no longer be
// completing clones. We had locks on some frames that were,
// however, in hardware. This will clean them up. An
// alternative location would be in the reset dispatch.
// Note, however, that the reset dispatch can occur in any
// state and this should be understood.
//
// Some hardware may fill all S/G mappings before stopping...
// in this case, you may not have to do this. The
// "fake hardware" here simply stops filling mappings and
// cleans its scatter / gather tables out on the Stop call.
//
Status = ReleaseAllFrames ();
//
// Release any hardware resources related to this pin.
//
if (m_AcquiredResources)
{
//
// If we got an interface to the clock, we must release it.
//
if (m_Clock)
{
m_Clock->Release ();
m_Clock = NULL;
}
m_Sensor->ReleaseHardwareResources (m_Pin);
m_AcquiredResources = FALSE;
}
break;
case KSSTATE_ACQUIRE:
//
// Acquire any hardware resources related to this pin. We should
// only acquire them here -- **NOT** at filter create time.
// This means we do not fail creation of a filter because of
// limited hardware resources.
//
// TODO: Move this to a derived CCapturePin class - one
// specifically for testing, not as a sample.
if(GetAcquireFailureKey())
{
Status = STATUS_INSUFFICIENT_RESOURCES;
break;
}
if (FromState == KSSTATE_STOP)
{
Status =
m_Sensor->
AcquireHardwareResources (
m_Pin,
this,
m_VideoInfoHeader,
&m_HardwareSimulation
);
if (NT_SUCCESS (Status))
{
m_AcquiredResources = TRUE;
//
// Attempt to get an interface to the master clock.
// This will fail if one has not been assigned. Since
// one must be assigned while the pin is still in
// KSSTATE_STOP, this is a guranteed method of getting
// the clock should one be assigned.
//
if (!NT_SUCCESS (
KsPinGetReferenceClockInterface (
m_Pin,
&m_Clock
)
))
{
//
// If we could not get an interface to the clock,
// don't use one.
//
m_Clock = NULL;
}
}
else
{
m_AcquiredResources = FALSE;
}
}
//
// Standard transport pins will always receive transitions in
// +/- 1 manner. This means we'll always see a PAUSE->ACQUIRE
// transition before stopping the pin.
//
m_FrameNumber = 0;
m_DroppedFrames = 0;
break;
case KSSTATE_PAUSE:
//
// Stop the hardware simulation if we're coming down from run.
//
if (FromState == KSSTATE_RUN)
{
m_PresentationTime = 0;
Status = m_Sensor->Pause (m_Pin, TRUE);
if (NT_SUCCESS (Status))
{
m_PinState = PinPaused;
}
}
m_FrameNumber = 0;
break;
case KSSTATE_RUN:
//
// Start the hardware simulation or unpause it depending on
// whether we're initially running or we've paused and restarted.
//
if (FromState == KSSTATE_PAUSE && m_PinState == PinPaused)
{
Status = m_Sensor->Pause (m_Pin, FALSE);
}
else
{
Status = m_Sensor->Start (m_Pin);
}
if (NT_SUCCESS (Status))
{
m_PinState = PinRunning;
}
break;
}
DBG_LEAVE( "( Pin=%d, ToState=%s, FromState=%s ) = 0x%08X\n",
m_Pin->Id, KSStateToStateName(ToState), KSStateToStateName(FromState), Status ) ;
return Status;
}
NTSTATUS
CCapturePin::
Process()
/*++
Routine Description:
The process dispatch for the pin bridges to this location.
We handle setting up scatter gather mappings, etc...
Arguments:
None
Return Value:
Success / Failure
--*/
{
PAGED_CODE( );
DBG_ENTER( "( Pin=%d )", m_Pin->Id );
NTSTATUS Status = STATUS_SUCCESS;
PKSSTREAM_POINTER Leading;
Leading = KsPinGetLeadingEdgeStreamPointer( m_Pin, KSSTREAM_POINTER_STATE_LOCKED );
while( NT_SUCCESS( Status ) && Leading )
{
PKSSTREAM_POINTER ClonePointer;
PSTREAM_POINTER_CONTEXT SPContext = NULL;
//
// If no data is present in the Leading edge stream pointer, just
// move on to the next frame
//
if( NULL == Leading->StreamHeader->Data )
{
Status = KsStreamPointerAdvance( Leading );
continue;
}
//
// For optimization sake in this particular sample, I will only keep
// one clone stream pointer per frame. This complicates the logic
// here but simplifies the completions.
//
// I'm also choosing to do this since I need to keep track of the
// virtual addresses corresponding to each mapping since I'm faking
// DMA. It simplifies that too.
//
if( !m_PreviousStreamPointer )
{
//
// First thing we need to do is clone the leading edge. This allows
// us to keep reference on the frames while they're in DMA.
//
Status = KsStreamPointerClone(
Leading,
NULL,
sizeof (STREAM_POINTER_CONTEXT),
&ClonePointer
);
//
// I use this for easy chunking of the buffer. We're not really
// dealing with physical addresses. This keeps track of what
// virtual address in the buffer the current scatter / gather
// mapping corresponds to for the fake hardware.
//
if( NT_SUCCESS( Status ) )
{
//
// Set the stream header data used to 0. We update this
// in the DMA completions. For queues with DMA, we must
// update this field ourselves.
//
ClonePointer->StreamHeader->DataUsed = 0;
SPContext = reinterpret_cast <PSTREAM_POINTER_CONTEXT>
(ClonePointer->Context);
SPContext->BufferVirtual =
reinterpret_cast <PUCHAR> (
ClonePointer->StreamHeader->Data
);
}
}
else
{
ClonePointer = m_PreviousStreamPointer;
SPContext = reinterpret_cast <PSTREAM_POINTER_CONTEXT>
(ClonePointer->Context);
}
//
// If the clone failed, likely we're out of resources. Break out
// of the loop for now. We may end up starving DMA.
//
if( !NT_SUCCESS( Status ) )
{
KsStreamPointerUnlock( Leading, FALSE );
break;
}
//
// Program the fake hardware. I would use Clone->OffsetOut.*, but
// because of the optimization of one stream pointer per frame, it
// doesn't make complete sense.
//
ULONG MappingsUsed =
m_Sensor->ProgramScatterGatherMappings(
m_Pin,
&ClonePointer,
&(SPContext->BufferVirtual),
Leading->OffsetOut.Mappings,
Leading->OffsetOut.Remaining
);
//
// In order to keep one clone per frame and simplify the fake DMA
// logic, make a check to see if we completely used the mappings in
// the leading edge. Set a flag.
//
if( MappingsUsed == Leading->OffsetOut.Remaining )
{
m_PreviousStreamPointer = NULL;
}
else
{
m_PreviousStreamPointer = ClonePointer;
}
if( MappingsUsed )
{
//
// If any mappings were added to scatter / gather queues,
// advance the leading edge by that number of mappings. If
// we run off the end of the queue, Status will be
// STATUS_DEVICE_NOT_READY. Otherwise, the leading edge will
// point to a new frame. The previous one will not have been
// dismissed (unless "DMA" completed) since there's a clone
// pointer referencing the frames.
//
Status =
KsStreamPointerAdvanceOffsets(
Leading,
0,
MappingsUsed,
FALSE
);
}
else
{
//
// The hardware was incapable of adding more entries. The S/G
// table is full.
//
Status = STATUS_PENDING;
break;
}
}
//
// If the leading edge failed to lock (this is always possible, remember
// that locking CAN occassionally fail), don't blow up passing NULL
// into KsStreamPointerUnlock. Also, set m_PendIo to kick us later...
//
if( !Leading )
{
m_PendIo = TRUE;
//
// If the lock failed, there's no point in getting called back
// immediately. The lock could fail due to insufficient memory,
// etc... In this case, we don't want to get called back immediately.
// Return pending. The m_PendIo flag will cause us to get kicked
// later.
//
Status = STATUS_PENDING;
}
//
// If we didn't run the leading edge off the end of the queue, unlock it.
//
if( NT_SUCCESS( Status ) && Leading )
{
KsStreamPointerUnlock( Leading, FALSE );
}
else
{
//
// DEVICE_NOT_READY indicates that the advancement ran off the end
// of the queue. We couldn't lock the leading edge.
//
if( Status == STATUS_DEVICE_NOT_READY )
{
Status = STATUS_SUCCESS;
}
}
//
// If we failed with something that requires pending, set the pending I/O
// flag so we know we need to start it again.
//
if( !NT_SUCCESS( Status ) || Status == STATUS_PENDING )
{
m_PendIo = TRUE;
}
DBG_LEAVE( "( Pin=%d )=0x%08X", m_Pin->Id, Status );
return Status;
}
//
// Emit metadata here for video or preview pin.
//
// This function gives us one last chance to tack metadata onto the sample.
//
void
CCapturePin::
EmitMetadata(
_Inout_ PKSSTREAM_HEADER pStreamHeader
)
{
PAGED_CODE();
NT_ASSERT(pStreamHeader);
}
NTSTATUS
CCapturePin::
CompleteMapping(
_In_opt_ PKSSTREAM_POINTER Clone
)
/*++
Routine Description:
Called to notify the pin that a given number of scatter / gather
mappings have completed. Let the buffers go if possible.
Arguments:
Clone -
The stream pointer for the frame to complete.
If Clone is null, use the head of the queue.
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
DBG_ENTER("(Clone=%p)", Clone);
NTSTATUS Status = STATUS_SUCCESS;
if( !Clone )
{
Clone = KsPinGetFirstCloneStreamPointer(m_Pin);
}
//
// If we have completed all remaining mappings in this clone, it
// is an indication that the clone is ready to be deleted and the
// buffer released. Set anything required in the stream header which
// has not yet been set. If we have a clock, we can timestamp the
// sample.
//
if( Clone )
{
if( Clone->StreamHeader->DataUsed >= Clone->OffsetOut.Remaining )
{
Clone->StreamHeader->Duration =
m_VideoInfoHeader->AvgTimePerFrame;
Clone->StreamHeader->OptionsFlags |=
KSSTREAM_HEADER_OPTIONSF_DURATIONVALID;
//
// Increment the frame number. This is the total count of frames which
// have attempted capture.
//
m_FrameNumber++;
DBG_TRACE( "m_FrameNumber=%lld", m_FrameNumber );
//
// Double check the Stream Header size. AVStream makes no guarantee
// that because StreamHeaderSize is set to a specific size that you
// will get that size. If the proper data type handlers are not
// installed, the stream header will be of default size.
//
if ( Clone->StreamHeader->Size >= sizeof (KSSTREAM_HEADER) +
sizeof (KS_FRAME_INFO))
{
PKS_FRAME_INFO FrameInfo = reinterpret_cast <PKS_FRAME_INFO> (
Clone->StreamHeader + 1
);
FrameInfo->ExtendedHeaderSize = sizeof (KS_FRAME_INFO);
FrameInfo->dwFrameFlags = KS_VIDEO_FLAG_FRAME;
FrameInfo->PictureNumber = (LONGLONG)m_FrameNumber;
// I don't really have a way to tell if the device has dropped a frame
// or was not able to send a frame on time.
FrameInfo->DropCount = (LONGLONG)m_DroppedFrames;
}
KsStreamPointerDelete (Clone);
}
else
{
//
// If only part of the mappings in this clone have been completed,
// update the pointers. Since we're guaranteed this won't advance
// to a new frame by the check above, it won't fail.
//
Status =
KsStreamPointerAdvanceOffsets(
Clone,
0,
Clone->StreamHeader->DataUsed,
FALSE
);
}
}
else
{
// We had nothing to process.
Status = STATUS_UNSUCCESSFUL;
}
//
// If we've used all the mappings in hardware and pended, we can kick
// processing to happen again if we've completed mappings.
//
if (m_PendIo)
{
KsPinAttemptProcessing (m_Pin, TRUE);
m_PendIo = FALSE;
}
DBG_LEAVE("(Clone=%p)=0x%08X", Clone, Status);
return Status;
}
bool
CCapturePin::
GetAcquireFailureKey()
{
NTSTATUS ntstatus;
bool acquireFailure = false;
UNICODE_STRING RegistryKeyName;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE handleRegKey = NULL;
PKEY_VALUE_FULL_INFORMATION pKeyInfo = NULL;
ULONG ulKeyInfoSizeNeeded = 0;
UNICODE_STRING ValueName = {0};
PAGED_CODE();
// Get the Registry key
RtlInitUnicodeString(&RegistryKeyName, L"\\Registry\\Machine\\Software\\Microsoft\\wtt\\MachineConfig");
InitializeObjectAttributes(&ObjectAttributes,
&RegistryKeyName,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL, // handle
NULL);
ntstatus = ZwOpenKey(&handleRegKey, KEY_READ, &ObjectAttributes);
// If we can't open the key, we're done. Worst case, we cannot stream.
if(NT_SUCCESS(ntstatus))
{
// Get the location from the registry key
RtlInitUnicodeString(&ValueName, L"AcquireFailure");
// Figure out how big keyInfo needs to be
ntstatus = ZwQueryValueKey(handleRegKey,
&ValueName,
KeyValueFullInformation,
pKeyInfo,
0,
&ulKeyInfoSizeNeeded );
// We expect one of these errors
if( (ntstatus == STATUS_BUFFER_TOO_SMALL) || (ntstatus == STATUS_BUFFER_OVERFLOW) )
{
// Allocate the memory needed for the key
pKeyInfo = (PKEY_VALUE_FULL_INFORMATION) new (NonPagedPoolNx) BYTE[ulKeyInfoSizeNeeded];
RtlZeroMemory(pKeyInfo, ulKeyInfoSizeNeeded);
// Now get the actual key data
ntstatus = ZwQueryValueKey( handleRegKey,
&ValueName,
KeyValueFullInformation,
pKeyInfo,
ulKeyInfoSizeNeeded,
&ulKeyInfoSizeNeeded );
if(ntstatus == STATUS_SUCCESS)
{