-
Notifications
You must be signed in to change notification settings - Fork 1
/
capture.cpp
1388 lines (1087 loc) · 35.2 KB
/
capture.cpp
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
/**************************************************************************
Sonix AVStream For UVC
Copyright (c) 2007, Sonix Corporation.
File:
capture.cpp
Abstract:
This file contains source for the video capture pin on the capture
filter. The capture sample performs "fake" DMA directly into
the capture buffers. Common buffer DMA will work slightly differently.
For common buffer DMA, the general technique would be DPC schedules
processing with KsPinAttemptProcessing. The processing routine grabs
the leading edge, copies data out of the common buffer and advances.
Cloning would not be necessary with this technique. It would be
similiar to the way "AVSSamp" works, but it would be pin-centric.
History:
created 2007/01/26 [Saxen Ko]
**************************************************************************/
#include "SnCam.h"
CCapturePin::CCapturePin (
IN PKSPIN Pin
) :
m_Pin (Pin),
//,m_SurfaceType (KS_CAPTURE_ALLOC_SYSTEM)
m_PresentationTime (0)
/*++
Routine Description:
Construct a new capture pin.
Arguments:
Pin -
The AVStream pin object corresponding to the capture pin
Return Value:
None
--*/
{
PAGED_CODE();
PKSDEVICE Device = KsPinGetDevice (Pin);
//
// Set up our device pointer. This gives us access to "hardware I/O"
// during the capture routines.
//
m_Device = reinterpret_cast <CCaptureDevice *> (Device -> Context);
}
/*************************************************/
NTSTATUS
CCapturePin::
DispatchCreate (
IN PKSPIN Pin,
IN PIRP Irp
)
/*++
Routine Description:
Create a new capture pin. This is the creation dispatch for
the video capture pin.
Arguments:
Pin -
The pin being created
Irp -
The creation Irp
Return Value:
Success / Failure
--*/
{
PAGED_CODE();
NTSTATUS Status = STATUS_SUCCESS;
DBGU_TRACE("Enter CCapturePin::DispatchCreate\n");
CCapturePin *CapPin = new (NonPagedPool) CCapturePin (Pin);
//2010/4/20 07:31¤U¤È
ULONG StreamNumber = Pin->Id;
if (!CapPin) {
//
// Return failure if we couldn't create the pin.
//
Status = STATUS_INSUFFICIENT_RESOURCES;
} 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 (
Pin -> Bag,
reinterpret_cast <PVOID> (CapPin),
reinterpret_cast <PFNKSFREE> (CCapturePin::Cleanup)
);
if (!NT_SUCCESS (Status)) {
delete CapPin;
} else {
Pin -> Context = reinterpret_cast <PVOID> (CapPin);
}
}
//
// 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.
//
PKS_VIDEOINFOHEADER VideoInfoHeader = NULL; // for Vista change to KS_VIDEOINFOHEADER2
// shawn 2011/06/23 +++++
PKSDEVICE pDevice = KsPinGetDevice (Pin);
CCaptureDevice *pCapDevice = reinterpret_cast <CCaptureDevice *> (pDevice -> Context);
if (!pCapDevice)
{
Status = STATUS_UNSUCCESSFUL;
// shawn 2011/06/23 -----
}
else
{
//@@Liu++
//2010/4/20 06:54¤U¤È
if(StreamNumber != pCapDevice->pdx->m_STREAM_Capture_MP2TS_Idx) // shawn 2011/06/23 modify
{
if (NT_SUCCESS (Status)) {
VideoInfoHeader = CapPin -> CaptureVideoInfoHeader ();
if (!VideoInfoHeader) {
Status = STATUS_INSUFFICIENT_RESOURCES;
}
}
if(NT_SUCCESS (Status)) {
//
// We need to edit the descriptor to ensure we don't mess up any other
// pins using the descriptor or touch read-only memory.
//
Status = KsEdit (
Pin,
&Pin -> Descriptor,
AVSHWS_POOLTAG);
if (NT_SUCCESS (Status)) {
do {
PKSFILTER Filter = (PKSFILTER)KsGetParent(Pin);
if (!Filter) {
Status = STATUS_UNSUCCESSFUL;
break;
}
CCaptureFilter* ParentFilter = static_cast<CCaptureFilter*>(Filter -> Context);
if (!ParentFilter) {
Status = STATUS_UNSUCCESSFUL;
break;
}
} while (FALSE);
}
//
// If the edits proceeded without running out of memory, adjust
// the framing based on the video info header.
//
Status = KsEdit (
Pin,
&Pin -> Descriptor -> AllocatorFraming,
AVSHWS_POOLTAG);
if (NT_SUCCESS (Status)) {
//
// We've KsEdit'ed this... I'm safe to cast away constness as
// long as the edit succeeded.
//
PKSALLOCATOR_FRAMING_EX Framing =
const_cast <PKSALLOCATOR_FRAMING_EX> (
Pin -> Descriptor -> AllocatorFraming
);
Framing -> FramingItem [0].Frames = 2;
//
// The physical and optimal ranges must be biSizeImage. We only
// support one frame size, precisely the size of each capture
// image.
//
Framing -> FramingItem [0].PhysicalRange.MinFrameSize =
Framing -> FramingItem [0].PhysicalRange.MaxFrameSize =
Framing -> FramingItem [0].FramingRange.Range.MinFrameSize =
Framing -> FramingItem [0].FramingRange.Range.MaxFrameSize =
VideoInfoHeader -> bmiHeader.biSizeImage;
Framing -> FramingItem [0].PhysicalRange.Stepping =
Framing -> FramingItem [0].FramingRange.Range.Stepping =
0;
}
}
}
else // james try M2TS.
{
Status = KsEdit (
Pin,
&Pin -> Descriptor -> AllocatorFraming,
AVSHWS_POOLTAG);
if (NT_SUCCESS (Status)) {
//
// We've KsEdit'ed this... I'm safe to cast away constness as
// long as the edit succeeded.
//
PKSALLOCATOR_FRAMING_EX Framing =
const_cast <PKSALLOCATOR_FRAMING_EX> (
Pin -> Descriptor -> AllocatorFraming
);
Framing -> FramingItem [0].Frames = 2;
//
// The physical and optimal ranges must be biSizeImage. We only
// support one frame size, precisely the size of each capture
// image.
//
Framing -> FramingItem [0].PhysicalRange.MinFrameSize =
Framing -> FramingItem [0].PhysicalRange.MaxFrameSize =
Framing -> FramingItem [0].FramingRange.Range.MinFrameSize =
Framing -> FramingItem [0].FramingRange.Range.MaxFrameSize =
0x13000;
//0x13000;
//0x96000; // james define.
Framing -> FramingItem [0].PhysicalRange.Stepping =
Framing -> FramingItem [0].FramingRange.Range.Stepping =
0;
DBGU_TRACE("Framing -> FramingItem [0].FramingRange.Range.MinFrameSize= %d\n",Framing -> FramingItem [0].FramingRange.Range.MinFrameSize);
}
}
}//@@Liu++
// shawn 2011/06/23 remove
//PKSDEVICE pDevice = KsPinGetDevice (Pin);
//CCaptureDevice *pCapDevice = reinterpret_cast <CCaptureDevice *> (pDevice -> Context);
if (!pCapDevice)
Status = STATUS_UNSUCCESSFUL;
if (NT_SUCCESS (Status))
{
Status = pCapDevice->StreamOpen(TRUE,Pin);
}
DBGU_TRACE("Exit CCapturePin::DispatchCreate Status= %X\n",Status);
return Status;
}
NTSTATUS
CCapturePin::
DispatchClose (
IN PKSPIN Pin,
IN PIRP Irp
)
{
PAGED_CODE();
NTSTATUS Status = STATUS_SUCCESS;
DBGU_TRACE("Enter CCapturePin::DispatchClose\n");
PKSDEVICE pDevice = KsPinGetDevice (Pin);
CCaptureDevice *pCapDevice = reinterpret_cast <CCaptureDevice *> (pDevice -> Context);
if (!pCapDevice)
Status = STATUS_UNSUCCESSFUL;
if (NT_SUCCESS (Status))
{
Status = pCapDevice->StreamOpen(FALSE,Pin);
}
return Status;
}
/*************************************************/
PKS_VIDEOINFOHEADER
CCapturePin::
CaptureVideoInfoHeader (
)
/*++
Routine Description:
Capture the video info header out of the connection format. This
is what we use to base synthesized images off.
Arguments:
None
Return Value:
The captured video info header or NULL if there is insufficient
memory.
--*/
{
PAGED_CODE();
DBGU_TRACE("Enter CCapturePin::CaptureVideoInfoHeader\n");
PKS_VIDEOINFOHEADER ConnectionHeader =
&((reinterpret_cast <PKS_DATAFORMAT_VIDEOINFOHEADER>
(m_Pin -> ConnectionFormat)) -> VideoInfoHeader); // for KS_VIDEOINFOHEADER2
m_VideoInfoHeader = reinterpret_cast <PKS_VIDEOINFOHEADER> (
ExAllocatePoolWithTag (
NonPagedPool,
KS_SIZE_VIDEOHEADER (ConnectionHeader),
AVSHWS_POOLTAG
)
);
if (!m_VideoInfoHeader)
return NULL;
//
// Bag the newly allocated header space. This will get cleaned up
// automatically when the pin closes.
//
NTSTATUS Status =
KsAddItemToObjectBag (
m_Pin -> Bag,
reinterpret_cast <PVOID> (m_VideoInfoHeader),
NULL
);
if (!NT_SUCCESS (Status)) {
ExFreePoolWithTag (m_VideoInfoHeader,AVSHWS_POOLTAG); m_VideoInfoHeader = NULL;
return NULL;
} else {
//
// Copy the connection format video info header into the newly
// allocated "captured" video info header.
//
RtlCopyMemory (
m_VideoInfoHeader,
ConnectionHeader,
KS_SIZE_VIDEOHEADER (ConnectionHeader)
);
}
return m_VideoInfoHeader;
}
// RBK 2012/08/21 , Metro ap get corrupt image when
// streaming 0.1M.Using Graphedit can repo this issue
// as well. the problem is incorrect framming buffer
// size , the size list done as below
// Correct size wrong size
// 172 x 144 x 2 256 x 144 x 2
// 320 x 240 x 2 384 x 240 x 2
// 352 x 288 x 2 384 x 288 x 2
// This function does data shifing only with 320x 240 sizes.
void WorkaroundHPBug(UCHAR *data,ULONG *DataUsed)
{
ULONG ulX = 320;
ULONG ulY = 240;
ULONG ulToX = 384;
PUCHAR img = (PUCHAR)ExAllocatePool(NonPagedPool,384*240*2);
RtlZeroMemory(img,384*240*2);
for (ULONG i = 0;i<ulY;i++)
{
RtlCopyMemory(img+i*ulToX*2,data+i*ulX*2,ulX*2);
}
RtlCopyMemory(data,img,ulY*ulToX*2);
ExFreePool(img);
*DataUsed = ulY*ulToX*2;
}
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();
NTSTATUS Status = STATUS_SUCCESS;
PKSSTREAM_POINTER Leading=NULL;
SnPrint(DEBUGLVL_VERBOSE, ("Enter CCapturePin::Process Stream ID = %d\n",m_Pin->Id));
ASSERT(m_Pin);
if (!m_Device)
{
DBGU_ERROR("DeviceState is not KSSTATE_RUN or m_Device of CCapturePin is NULL!\n");
return STATUS_UNSUCCESSFUL;
}
if (m_Pin->DeviceState != KSSTATE_RUN)
{
DBGU_TRACE("Stream State is not KSSTATE_RUN ..\n");
return STATUS_UNSUCCESSFUL;
}
KsPinAcquireProcessingMutex(m_Pin);
Leading = KsPinGetLeadingEdgeStreamPointer (
m_Pin,
KSSTREAM_POINTER_STATE_LOCKED
);
//
// Find stream pointer that has Data buffer
//
while (NT_SUCCESS (Status) && Leading)
{
//
// 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;
}
break;
}
//2010/4/21 03:53¤U¤È H264
if(Leading && m_Pin->Id == m_Device->pdx->m_STREAM_Capture_MP2TS_Idx) // shawn 2011/06/23 modify
{
//DBGU_TRACE("M2TS : buffer Remaining = %d\n", Leading->OffsetOut.Remaining);
DBGU_TRACE("M2TS : buffer Remaining = %d\n",Leading->OffsetOut.Remaining);
Status = m_Device->FrameReadingProcess(
m_Pin,
Leading->StreamHeader
);
//
// If there is no clock, don't time stamp the packets.
//
Leading -> StreamHeader -> PresentationTime.Time = 0;
Leading -> StreamHeader -> OptionsFlags |= KSSTREAM_HEADER_OPTIONSF_FLUSHONPAUSE;
//
// Advances StreamPointer the specified number of bytes into the stream
// and unlocks it.
//
KsStreamPointerAdvanceOffsetsAndUnlock(
Leading,
0,
//40*188,//m_VideoInfoHeader->bmiHeader.biSizeImage,
Leading->StreamHeader->DataUsed,
TRUE
);
}
else if (Leading && m_VideoInfoHeader)
{
//
// fill Stream header
//
ULONG ulBufferLeft = Leading->OffsetOut.Remaining;
Leading->StreamHeader->DataUsed = 0;
DBGU_TRACE("buffer Remaining = %d\n",Leading->OffsetOut.Remaining);
if (Leading->OffsetOut.Remaining >= m_VideoInfoHeader->bmiHeader.biSizeImage)
{
Leading -> StreamHeader -> Duration =
m_VideoInfoHeader -> AvgTimePerFrame;
Leading -> StreamHeader -> PresentationTime.Numerator =
Leading -> StreamHeader -> PresentationTime.Denominator = 1;
Status = m_Device->FrameReadingProcess(
m_Pin,
Leading->StreamHeader
);
//RBK this is a workaround for incorrect framing buffer issue. see more in the function
// header.
if (m_VideoInfoHeader->bmiHeader.biWidth == 320 &&
m_VideoInfoHeader->bmiHeader.biHeight == 240 &&
m_VideoInfoHeader->bmiHeader.biCompression == 0x32595559 &&
ulBufferLeft == 184320)
WorkaroundHPBug((UCHAR*)Leading->StreamHeader->Data,&Leading->StreamHeader->DataUsed);
if (m_Clock && NT_SUCCESS(Status)) { // shawn 2011/12/16 modify for fixing AMCAP capture error
// shawn 2011/08/29 modify +++++
LONGLONG SystemTime = 0;
LONGLONG ClockTime = m_Clock -> /*GetTime*/GetCorrelatedTime (&SystemTime);
DBGU_TRACE("CCapturePin::Process : m_Clock != NULL, stream time = %x, correlated system time = %x\n", ClockTime, SystemTime);
// shawn 2011/08/29 modify -----
Leading -> StreamHeader -> PresentationTime.Time = ClockTime;
Leading -> StreamHeader -> OptionsFlags |=
KSSTREAM_HEADER_OPTIONSF_FLUSHONPAUSE |
KSSTREAM_HEADER_OPTIONSF_TIMEVALID |
KSSTREAM_HEADER_OPTIONSF_DURATIONVALID;
} else {
//
// If there is no clock, don't time stamp the packets.
//
DBGU_TRACE("CCapturePin::Process : m_Clock == NULL\n");
Leading -> StreamHeader -> PresentationTime.Time = 0;
Leading -> StreamHeader -> OptionsFlags |= KSSTREAM_HEADER_OPTIONSF_FLUSHONPAUSE;
}
//RBK the Status seems not reflect problem data,
// Let's fix it in near future
if ((m_Device->pdx->pVideoDevice->m_InitDropFrames-- > 3) ||
(Leading->StreamHeader->DataUsed == 0))
{
//DbgPrint("drop frame: %X\n", Status);
Leading->StreamHeader->DataUsed =0; // Drop frame
KsStreamPointerUnlock(Leading, FALSE);
Status = STATUS_UNSUCCESSFUL;
}else
//
// Advances StreamPointer the specified number of bytes into the stream
// and unlocks it.
//
KsStreamPointerAdvanceOffsetsAndUnlock(
Leading,
0,
Leading->StreamHeader->DataUsed, // shawn 2011/07/26 modify
TRUE
);
}
else
{
KeDelay(33);
KsStreamPointerUnlock(Leading, FALSE);
Status = STATUS_BUFFER_TOO_SMALL;
}
}
KsPinReleaseProcessingMutex(m_Pin);
//
// Kick processing to happen again if we've completed.
//
KsPinAttemptProcessing (m_Pin, TRUE);
SnPrint(DEBUGLVL_VERBOSE, ("Leave CCapturePin::Process Stream ID = %d (0x%X)\n",m_Pin->Id, Status));
return Status;
}
/*************************************************/
NTSTATUS
CCapturePin::
CleanupReferences (
)
/*++
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();
DBGU_TRACE("Enter CCapturePin::CleanupReferences\n");
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::
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();
DBGU_TRACE("Enter CCapturePin::SetState from %d to %d\n",FromState,ToState);
NTSTATUS Status = STATUS_SUCCESS;
if (!m_Device)
{
DBGU_ERROR("m_Device of CCapturePin is NULL!\n");
return STATUS_UNSUCCESSFUL;
}
switch (ToState) {
case KSSTATE_STOP:
DBGU_TRACE("Enter KSSTATE_STOP StreamID= %d\n",m_Pin->Id);
//
// First, stop the hardware if we actually did anything to it.
//
if (m_StreamState != KSSTATE_RUN) {
Status = m_Device -> Stop (m_Pin);
m_StreamState = KSSTATE_STOP;
}
//
// 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 = CleanupReferences ();
//
// 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_Device -> ReleaseHardwareResources (
m_Pin->Id
);
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.
//
if (FromState == KSSTATE_STOP) {
Status = m_Device -> AcquireHardwareResources (
m_VideoInfoHeader,
m_Pin->Id
);
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;
}
} else {
//
// Standard transport pins will always receive transitions in
// +/- 1 manner. This means we'll always see a PAUSE->ACQUIRE
// transition before stopping the pin.
//
// The below is done because on DirectX 8.0, when the pin gets
// a message to stop, the queue is inaccessible. The reset
// which comes on every stop happens after this (at which time
// the queue is inaccessible also). So, for compatibility with
// DirectX 8.0, I am stopping the "fake" hardware at this
// point and cleaning up all references we have on frames. See
// the comments above regarding the CleanupReferences call.
//
// If this sample were targeting XP only, the below code would
// not be here. Again, I only do this so the sample does not
// hang when it is stopped running on a configuration such as
// Win2K + DX8.
//
if (m_StreamState != KSSTATE_STOP) {
Status = m_Device -> Stop (m_Pin);
m_StreamState = KSSTATE_STOP;
}
Status = CleanupReferences ();
}
break;
case KSSTATE_PAUSE:
DBGU_TRACE("Enter KSSTATE_PAUSE StreamID= %d\n",m_Pin->Id);
//
// Stop the hardware if we're coming down from run.
//
// shawn 2011/12/16 fix AMCAP capture error +++++
if (FromState == KSSTATE_ACQUIRE || FromState == KSSTATE_STOP)
m_Device->pdx->pVideoStream->m_FirstFrameStartTime = 0;
// shawn 2011/12/16 fix AMCAP capture error -----
if (FromState == KSSTATE_RUN) {
m_PresentationTime = 0;
Status = m_Device -> Pause (m_Pin, TRUE);
if (NT_SUCCESS (Status)) {
m_StreamState = KSSTATE_PAUSE;
}
}
break;
case KSSTATE_RUN:
//
// Start the hardware or unpause it depending on
// whether we're initially running or we've paused and restarted.
//
if (m_StreamState == KSSTATE_PAUSE) {
Status = m_Device -> Pause (m_Pin, FALSE);
} else {
Status = m_Device -> Start (m_Pin);
}
if (NT_SUCCESS (Status)) {
m_StreamState = KSSTATE_RUN;
}
break;
}
DBGU_TRACE("Leave CCapturePin::SetState with status = %X\n",Status);
return Status;
}
/*************************************************/
NTSTATUS
CCapturePin::
IntersectHandler (
IN PKSFILTER Filter,
IN PIRP Irp,
IN PKSP_PIN PinInstance,
IN PKSDATARANGE CallerDataRange,
IN PKSDATARANGE DescriptorDataRange,
IN ULONG BufferSize,
OUT PVOID Data OPTIONAL,
OUT PULONG DataSize
)
/*++
Routine Description:
This routine handles video pin intersection queries by determining the
intersection between two data ranges.
Arguments:
Filter -
Contains a void pointer to the filter structure.
Irp -
Contains a pointer to the data intersection property request.
PinInstance -
Contains a pointer to a structure indicating the pin in question.
CallerDataRange -
Contains a pointer to one of the data ranges supplied by the client
in the data intersection request. The format type, subtype and
specifier are compatible with the DescriptorDataRange.
DescriptorDataRange -
Contains a pointer to one of the data ranges from the pin descriptor
for the pin in question. The format type, subtype and specifier are
compatible with the CallerDataRange.
BufferSize -
Contains the size in bytes of the buffer pointed to by the Data
argument. For size queries, this value will be zero.
Data -
Optionally contains a pointer to the buffer to contain the data
format structure representing the best format in the intersection
of the two data ranges. For size queries, this pointer will be
NULL.
DataSize -
Contains a pointer to the location at which to deposit the size
of the data format. This information is supplied by the function
when the format is actually delivered and in response to size
queries.
Return Value:
STATUS_SUCCESS if there is an intersection and it fits in the supplied
buffer, STATUS_BUFFER_OVERFLOW for successful size queries,
STATUS_NO_MATCH if the intersection is empty, or
STATUS_BUFFER_TOO_SMALL if the supplied buffer is too small.
--*/
{
PAGED_CODE();
//DBGU_TRACE("Enter CCapturePin::IntersectHandler\n");
const GUID VideoInfoSpecifier =
{STATICGUIDOF(KSDATAFORMAT_SPECIFIER_VIDEOINFO)};
ASSERT(Filter);
ASSERT(Irp);
ASSERT(PinInstance);
ASSERT(CallerDataRange);
ASSERT(DescriptorDataRange);
ASSERT(DataSize);
ULONG DataFormatSize;
//
// Specifier FORMAT_VideoInfo for VIDEOINFOHEADER2
//
if (IsEqualGUID(CallerDataRange->Specifier, VideoInfoSpecifier) &&
CallerDataRange -> FormatSize >= sizeof (KS_DATARANGE_VIDEO)) {
PKS_DATARANGE_VIDEO callerDataRange =
reinterpret_cast <PKS_DATARANGE_VIDEO> (CallerDataRange);
PKS_DATARANGE_VIDEO descriptorDataRange =
reinterpret_cast <PKS_DATARANGE_VIDEO> (DescriptorDataRange);
PKS_DATAFORMAT_VIDEOINFOHEADER FormatVideoInfoHeader;
//
// Check that the other fields match
//
if ((callerDataRange->bFixedSizeSamples !=
descriptorDataRange->bFixedSizeSamples) ||
(callerDataRange->bTemporalCompression !=
descriptorDataRange->bTemporalCompression) ||
(callerDataRange->StreamDescriptionFlags !=
descriptorDataRange->StreamDescriptionFlags) ||
(callerDataRange->MemoryAllocationFlags !=
descriptorDataRange->MemoryAllocationFlags) ||
(RtlCompareMemory (&callerDataRange->ConfigCaps,
&descriptorDataRange->ConfigCaps,
sizeof (KS_VIDEO_STREAM_CONFIG_CAPS)) !=
sizeof (KS_VIDEO_STREAM_CONFIG_CAPS)))
{
//DBGU_TRACE("Check that the other fields match, return STATUS_NO_MATCH\n");
return STATUS_NO_MATCH;
}
//
// KS_SIZE_VIDEOHEADER() below is relying on bmiHeader.biSize from
// the caller's data range. This **MUST** be validated; the
// extended bmiHeader size (biSize) must not extend past the end
// of the range buffer. Possible arithmetic overflow is also
// checked for.
//
{
ULONG VideoHeaderSize = KS_SIZE_VIDEOHEADER (