-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathAvsCameraDMFT.cpp
More file actions
1496 lines (1304 loc) · 48.2 KB
/
AvsCameraDMFT.cpp
File metadata and controls
1496 lines (1304 loc) · 48.2 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
//
// Copyright (C) Microsoft. All rights reserved.
//
#include "stdafx.h"
#ifdef MF_WPP
#include "AvsCameraDMFT.tmh" //--REF_ANALYZER_DONT_REMOVE--
#endif
// TODO: required to avoid bug OS bug 36971659 in extended property handling that introduces a 16 bytes cookie
typedef struct
{
KSCAMERA_EXTENDEDPROP_HEADER header;
} KSCAMERA_EXTENDEDPROP_HEADER_BUFFERED, * PKSCAMERA_EXTENDEDPROP_HEADER_BUFFERED;
//
// This DeviceMFT is a stripped down implementation of the device MFT Sample present in the sample Repo
// The original DMFT is present at https://github.com/microsoft/Windows-driver-samples/tree/main/avstream/sampledevicemft
//
CMultipinMft::CMultipinMft()
: m_nRefCount( 0 ),
m_InputPinCount( 0 ),
m_OutputPinCount( 0 ),
m_dwWorkQueueId ( MFASYNC_CALLBACK_QUEUE_MULTITHREADED ),
m_lWorkQueuePriority ( 0 ),
m_spAttributes( nullptr ),
m_spSourceTransform( nullptr ),
m_SymbolicLink(nullptr),
m_hSelectedProfileKSEvent { nullptr },
m_hSelectedProfileKSEventSentToDriver { nullptr },
m_isProfileDDISupportedInBaseDriver{},
m_selectedProfileId { KSCAMERAPROFILE_Legacy, 0, 0 },
m_profileCallback {this, &CMultipinMft::ProfileAsyncResultCallback },
m_profileAsyncResult {nullptr}
{
HRESULT hr = S_OK;
ComPtr<IMFAttributes> pAttributes = nullptr;
MFCreateAttributes( &pAttributes, 0 );
DMFTCHECKHR_GOTO(pAttributes->SetUINT32( MF_TRANSFORM_ASYNC, TRUE ),done);
DMFTCHECKHR_GOTO(pAttributes->SetUINT32( MFT_SUPPORT_DYNAMIC_FORMAT_CHANGE, TRUE ),done);
DMFTCHECKHR_GOTO(pAttributes->SetUINT32( MF_SA_D3D_AWARE, TRUE ), done);
DMFTCHECKHR_GOTO(pAttributes->SetString( MFT_ENUM_HARDWARE_URL_Attribute, L"SampleMultiPinMft" ),done);
m_spAttributes = pAttributes;
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
}
CMultipinMft::~CMultipinMft( )
{
m_OutPins.clear();
SAFE_ARRAYDELETE(m_SymbolicLink);
m_spSourceTransform = nullptr;
}
IFACEMETHODIMP_(ULONG) CMultipinMft::AddRef(
void
)
{
return InterlockedIncrement(&m_nRefCount);
}
IFACEMETHODIMP_(ULONG) CMultipinMft::Release(
void
)
{
ULONG uCount = InterlockedDecrement(&m_nRefCount);
if ( uCount == 0 )
{
delete this;
}
return uCount;
}
IFACEMETHODIMP CMultipinMft::QueryInterface(
_In_ REFIID iid,
_COM_Outptr_ void** ppv
)
{
HRESULT hr = S_OK;
*ppv = NULL;
if ((iid == __uuidof(IMFDeviceTransform)) || (iid == __uuidof(IUnknown)))
{
*ppv = static_cast< IMFDeviceTransform* >(this);
}
else if ( iid == __uuidof( IMFMediaEventGenerator ) )
{
*ppv = static_cast< IMFMediaEventGenerator* >(this);
}
else if ( iid == __uuidof( IMFShutdown ) )
{
*ppv = static_cast< IMFShutdown* >( this );
}
else if ( iid == __uuidof( IKsControl ) )
{
*ppv = static_cast< IKsControl* >( this );
}
else if ( iid == __uuidof( IMFRealTimeClientEx ) )
{
*ppv = static_cast< IMFRealTimeClientEx* >( this );
}
else
{
hr = E_NOINTERFACE;
goto done;
}
AddRef();
done:
return hr;
}
/*++
Description:
This function is the entry point of the transform
The following things may be initialized here
1) Query for MF_DEVICEMFT_CONNECTED_FILTER_KSCONTROL on the attributes supplied
2) From the IUnknown acquired get the IMFTransform interface.
3) Get the stream count.. The output streams are of consequence to the tranform.
The input streams should correspond to the output streams exposed by the source transform
acquired from the Attributes supplied.
4) Get the IKSControl which is used to send KSPROPERTIES, KSEVENTS and KSMETHODS to the driver for the filer level. Store it in your filter class
5) Get the OutPutStreamAttributes for the output pins of the source transform. This can further be used to QI and acquire
the IKSControl related to the specific pin. This can be used to send PIN level KSPROPERTIES, EVENTS and METHODS to the pins
6) Create the output pins
--*/
IFACEMETHODIMP CMultipinMft::InitializeTransform (
_In_ IMFAttributes *pAttributes
)
{
HRESULT hr = S_OK;
ComPtr<IUnknown> spFilterUnk = nullptr;
DWORD *pcInputStreams = NULL, *pcOutputStreams = NULL;
DWORD inputStreams = 0;
DWORD outputStreams = 0;
GUID* outGuids = NULL;
GUID streamCategory = GUID_NULL;
ULONG ulOutPinIndex = 0;
UINT32 uiSymLinkLen = 0;
DMFTCHECKNULL_GOTO( pAttributes, done, E_INVALIDARG );
//
// The attribute passed with MF_DEVICEMFT_CONNECTED_FILTER_KSCONTROL is the source transform. This generally represents a filter
// This needs to be stored so that we know the device properties. We cache it. We query for the IKSControl which is used to send
// controls to the driver.
//
DMFTCHECKHR_GOTO( pAttributes->GetUnknown( MF_DEVICEMFT_CONNECTED_FILTER_KSCONTROL,IID_PPV_ARGS( &spFilterUnk ) ),done );
if (SUCCEEDED(pAttributes->GetStringLength(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &uiSymLinkLen))) // Not available prior to RS5
{
m_SymbolicLink = new (std::nothrow) WCHAR[++uiSymLinkLen];
DMFTCHECKNULL_GOTO(m_SymbolicLink, done, E_OUTOFMEMORY);
DMFTCHECKHR_GOTO(pAttributes->GetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, m_SymbolicLink, uiSymLinkLen, &uiSymLinkLen), done);
}
DMFTCHECKHR_GOTO( spFilterUnk.As( &m_spSourceTransform ), done );
DMFTCHECKHR_GOTO( m_spSourceTransform.As( &m_spIkscontrol ), done );
DMFTCHECKHR_GOTO(m_spSourceTransform->GetStreamCount(&inputStreams, &outputStreams), done);
spFilterUnk = nullptr;
//
//The number of input pins created by the device transform should match the pins exposed by
//the source transform i.e. outputStreams from SourceTransform or DevProxy = Input pins of the Device MFT
//
if ( inputStreams > 0 || outputStreams > 0 )
{
pcInputStreams = new (std::nothrow) DWORD[ inputStreams ];
DMFTCHECKNULL_GOTO( pcInputStreams, done, E_OUTOFMEMORY);
pcOutputStreams = new (std::nothrow) DWORD[ outputStreams ];
DMFTCHECKNULL_GOTO( pcOutputStreams, done, E_OUTOFMEMORY );
DMFTCHECKHR_GOTO( m_spSourceTransform->GetStreamIDs( inputStreams, pcInputStreams,
outputStreams,
pcOutputStreams ),done );
for ( ULONG ulIndex = 0; ulIndex < outputStreams; ulIndex++ )
{
ComPtr<IMFAttributes> spInAttributes;
ComPtr<CInPin> spInPin;
DMFTCHECKHR_GOTO(m_spSourceTransform->GetOutputStreamAttributes(ulIndex, &spInAttributes),done);
spInPin = new (std::nothrow) CInPin(spInAttributes.Get(), ulIndex, this);
DMFTCHECKNULL_GOTO(spInPin.Get(), done, E_OUTOFMEMORY);
hr = ExceptionBoundary([&]()
{
m_InPins.push_back(spInPin.Get());
});
DMFTCHECKHR_GOTO(hr, done);
DMFTCHECKHR_GOTO(spInPin->Init(m_spSourceTransform.Get()), done);
}
//
// Create one on one mapping
//
for (ULONG ulIndex = 0; ulIndex < m_InPins.size(); ulIndex++)
{
ComPtr<CInPin> spInPin = (CInPin*)m_InPins[ulIndex].Get();
if (spInPin.Get())
{
ComPtr<COutPin> spOutPin;
ComPtr<IKsControl> spKscontrol;
GUID pinGuid = GUID_NULL;
UINT32 uiFrameSourceType = 0;
DMFTCHECKHR_GOTO(spInPin.As(&spKscontrol), done); // Grab the IKSControl off the input pin
DMFTCHECKHR_GOTO(spInPin->GetGUID(MF_DEVICESTREAM_STREAM_CATEGORY, &pinGuid), done); // Get the Stream Category. Advertise on the output pin
spOutPin = new (std::nothrow) COutPin(
ulIndex,
this,
spKscontrol.Get()); // Create the output pin
DMFTCHECKNULL_GOTO(spOutPin.Get(), done, E_OUTOFMEMORY);
DMFTCHECKHR_GOTO(spOutPin->SetGUID(MF_DEVICESTREAM_STREAM_CATEGORY, pinGuid), done); // Advertise the Stream category to the Pipeline
DMFTCHECKHR_GOTO(spOutPin->SetUINT32(MF_DEVICESTREAM_STREAM_ID, ulIndex), done);
if (SUCCEEDED(spInPin->GetUINT32(MF_DEVICESTREAM_ATTRIBUTE_FRAMESOURCE_TYPES, &uiFrameSourceType)))
{
DMFTCHECKHR_GOTO(spOutPin->SetUINT32(MF_DEVICESTREAM_ATTRIBUTE_FRAMESOURCE_TYPES, uiFrameSourceType), done);
}
hr = BridgeInputPinOutputPin(spInPin.Get(), spOutPin.Get());
if (SUCCEEDED(hr))
{
DMFTCHECKHR_GOTO(ExceptionBoundary([&]()
{
m_OutPins.push_back(spOutPin.Get());
}), done);
ulOutPinIndex++;
}
DMFTCHECKHR_GOTO(hr, done);
}
}
}
m_InputPinCount = ULONG ( m_InPins.size() );
m_OutputPinCount = ULONG ( m_OutPins.size() );
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!",hr,hr);
if ( pcInputStreams )
{
delete[ ] ( pcInputStreams );
}
if ( pcOutputStreams )
{
delete[ ] ( pcOutputStreams );
}
if ( outGuids )
{
delete [] ( outGuids );
}
if ( FAILED( hr ) )
{
//Release the pins and the resources acquired
m_InPins.clear();
m_OutPins.clear();
//
// Simply clear the custom pins since the input pins must have deleted the pin
//
m_spSourceTransform = nullptr;
m_spIkscontrol = nullptr;
}
return hr;
}
IFACEMETHODIMP CMultipinMft::SetWorkQueueEx(
_In_ DWORD dwWorkQueueId,
_In_ LONG lWorkItemBasePriority
)
/*++
Description:
Implements IMFRealTimeClientEx::SetWorkQueueEx function
--*/
{
CAutoLock lock( m_critSec );
//
// Cache the WorkQueuId and WorkItemBasePriority. This is called once soon after the device MFT is initialized
//
m_dwWorkQueueId = dwWorkQueueId;
m_lWorkQueuePriority = lWorkItemBasePriority;
// Set it on the pins
for (DWORD dwIndex = 0; dwIndex < (DWORD)m_InPins.size(); dwIndex++)
{
m_InPins[dwIndex]->SetWorkQueue(dwWorkQueueId);
}
for (DWORD dwIndex = 0; dwIndex < (DWORD)m_OutPins.size(); dwIndex++)
{
m_OutPins[dwIndex]->SetWorkQueue(dwWorkQueueId);
}
return S_OK;
}
//
// IMFDeviceTransform functions
//
IFACEMETHODIMP CMultipinMft::GetStreamCount(
_Inout_ DWORD *pdwInputStreams,
_Inout_ DWORD *pdwOutputStreams
)
/*++
Description: Implements IMFTransform::GetStreamCount function
--*/
{
HRESULT hr = S_OK;
CAutoLock lock(m_critSec);
DMFTCHECKNULL_GOTO(pdwInputStreams, done, E_INVALIDARG);
DMFTCHECKNULL_GOTO(pdwOutputStreams, done, E_INVALIDARG);
*pdwInputStreams = m_InputPinCount;
*pdwOutputStreams = m_OutputPinCount;
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr );
done:
return hr;
}
//
//Doesn't strictly conform to the GetStreamIDs on IMFTransform Interface!
//
IFACEMETHODIMP CMultipinMft::GetStreamIDs(
_In_ DWORD dwInputIDArraySize,
_When_(dwInputIDArraySize >= m_InputPinCount, _Out_writes_(dwInputIDArraySize)) DWORD* pdwInputIDs,
_In_ DWORD dwOutputIDArraySize,
_When_(dwOutputIDArraySize >= m_OutputPinCount && (pdwInputIDs && (dwInputIDArraySize > 0)),
_Out_writes_(dwOutputIDArraySize)) _On_failure_(_Valid_) DWORD* pdwOutputIDs
)
/*++
Description:
Implements IMFTransform::GetStreamIDs function
--*/
{
HRESULT hr = S_OK;
CAutoLock lock(m_critSec);
if ( ( dwInputIDArraySize < m_InputPinCount ) && ( dwOutputIDArraySize < m_OutputPinCount ) )
{
hr = MF_E_BUFFERTOOSMALL;
goto done;
}
if ( dwInputIDArraySize )
{
DMFTCHECKNULL_GOTO( pdwInputIDs, done, E_POINTER );
for ( DWORD dwIndex = 0; dwIndex < ((dwInputIDArraySize > m_InputPinCount) ? m_InputPinCount:
dwInputIDArraySize); dwIndex++ )
{
pdwInputIDs[ dwIndex ] = ( m_InPins[dwIndex] )->streamId();
}
}
if ( dwOutputIDArraySize )
{
DMFTCHECKNULL_GOTO( pdwOutputIDs, done, E_POINTER );
for ( DWORD dwIndex = 0; dwIndex < ((dwOutputIDArraySize > m_OutputPinCount)? m_OutputPinCount:
dwOutputIDArraySize); dwIndex++ )
{
pdwOutputIDs[ dwIndex ] = (m_OutPins[ dwIndex ])->streamId();
}
}
done:
return hr;
}
/*++
Name: CMultipinMft::GetInputAvailableType
Description:
Implements IMFTransform::GetInputAvailableType function. This function
gets the media type supported by the specified stream based on the
index dwTypeIndex.
--*/
IFACEMETHODIMP CMultipinMft::GetInputAvailableType(
_In_ DWORD dwInputStreamID,
_In_ DWORD dwTypeIndex,
_Out_ IMFMediaType** ppMediaType
)
{
HRESULT hr = S_OK;
ComPtr<CInPin> spiPin = GetInPin( dwInputStreamID );
DMFTCHECKNULL_GOTO(ppMediaType, done, E_INVALIDARG);
DMFTCHECKNULL_GOTO( spiPin, done, MF_E_INVALIDSTREAMNUMBER );
*ppMediaType = nullptr;
hr = spiPin->GetOutputAvailableType( dwTypeIndex,ppMediaType );
if (FAILED(hr))
{
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! Pin: %d Index: %d exiting %!HRESULT!",
dwInputStreamID,
dwTypeIndex,
hr);
}
done:
return hr;
}
IFACEMETHODIMP CMultipinMft::GetOutputAvailableType(
_In_ DWORD dwOutputStreamID,
_In_ DWORD dwTypeIndex,
_Out_ IMFMediaType** ppMediaType
)
/*++
Description:
Implements IMFTransform::GetOutputAvailableType function. This function
gets the media type supported by the specified stream based on the
index dwTypeIndex.
--*/
{
HRESULT hr = S_OK;
CAutoLock Lock(m_critSec);
ComPtr<COutPin> spoPin = GetOutPin( dwOutputStreamID );
DMFTCHECKNULL_GOTO( spoPin.Get(), done, MF_E_INVALIDSTREAMNUMBER );
DMFTCHECKNULL_GOTO(ppMediaType, done, E_INVALIDARG);
*ppMediaType = nullptr;
hr = spoPin->GetOutputAvailableType( dwTypeIndex, ppMediaType );
if ( FAILED( hr ) )
{
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! Pin: %d Index: %d exiting %!HRESULT!",
dwOutputStreamID,
dwTypeIndex,
hr );
}
done:
return hr;
}
IFACEMETHODIMP CMultipinMft::GetInputCurrentType(
_In_ DWORD dwInputStreamID,
_COM_Outptr_result_maybenull_ IMFMediaType** ppMediaType
)
/*++
Description:
Implements IMFTransform::GetInputCurrentType function. This function
returns the current media type set on the specified stream.
--*/
{
//
// The input current types will not come to this transform.
// The outputs of this transform matter. The DTM manages the
// output of this transform and the inptuts of the source transform
//
UNREFERENCED_PARAMETER(dwInputStreamID);
UNREFERENCED_PARAMETER(ppMediaType);
return S_OK;
}
IFACEMETHODIMP CMultipinMft::GetOutputCurrentType(
_In_ DWORD dwOutputStreamID,
_Out_ IMFMediaType** ppMediaType
)
/*++
Description:
Implements IMFTransform::GetOutputCurrentType function. This function
returns the current media type set on the specified stream.
--*/
{
HRESULT hr = S_OK;
ComPtr<COutPin> spoPin;
CAutoLock lock( m_critSec );
DMFTCHECKNULL_GOTO( ppMediaType, done, E_INVALIDARG );
*ppMediaType = nullptr;
spoPin = GetOutPin( dwOutputStreamID );
DMFTCHECKNULL_GOTO(spoPin, done, MF_E_INVALIDSTREAMNUMBER );
DMFTCHECKHR_GOTO(spoPin->getMediaType( ppMediaType ),done );
DMFTCHECKNULL_GOTO( *ppMediaType, done, MF_E_TRANSFORM_TYPE_NOT_SET );
done:
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr );
return hr;
}
IFACEMETHODIMP CMultipinMft::ProcessEvent(
_In_ DWORD dwInputStreamID,
_In_ IMFMediaEvent* pEvent
)
/*++
Description:
Implements IMFTransform::ProcessEvent function. This function
processes events that come to the MFT.
--*/
{
UNREFERENCED_PARAMETER(dwInputStreamID);
UNREFERENCED_PARAMETER(pEvent);
return S_OK;
}
IFACEMETHODIMP CMultipinMft::ProcessMessage(
_In_ MFT_MESSAGE_TYPE eMessage,
_In_ ULONG_PTR ulParam
)
/*++
Description:
Implements IMFTransform::ProcessMessage function. This function
processes messages coming to the MFT.
--*/
{
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER(ulParam);
CAutoLock _lock( m_critSec );
switch ( eMessage )
{
case MFT_MESSAGE_COMMAND_FLUSH:
//
// This is MFT wide flush.. Flush all output pins
//
(VOID)FlushAllStreams();
break;
case MFT_MESSAGE_COMMAND_DRAIN:
//
// There is no draining for Device MFT. Just kept here for reference
//
break;
case MFT_MESSAGE_NOTIFY_START_OF_STREAM:
//
// No op for device MFTs
//
break;
case MFT_MESSAGE_SET_D3D_MANAGER:
{
if ( ulParam )
{
ComPtr< IDirect3DDeviceManager9 > spD3D9Manager;
ComPtr< IMFDXGIDeviceManager > spDXGIManager;
hr = ( ( IUnknown* ) ulParam )->QueryInterface( IID_PPV_ARGS( &spD3D9Manager ) );
if ( SUCCEEDED( hr ) )
{
m_spDeviceManagerUnk = ( IUnknown* )ulParam;
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! IDirect3DDeviceManager9 %p, is passed", spD3D9Manager.Get() );
}
else
{
hr = ( ( IUnknown* ) ulParam )->QueryInterface( IID_PPV_ARGS( &spDXGIManager ) );
if ( SUCCEEDED(hr) )
{
m_spDeviceManagerUnk = (IUnknown*)ulParam;
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! IMFDXGIDeviceManager %p, is passed", spDXGIManager.Get());
}
}
}
else
{
m_spDeviceManagerUnk = nullptr;
hr = S_OK;
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC!IDirect3DDeviceManager9 was not passed in");
}
//
// set it on the pins. Can happen anytime
//
for (DWORD dwIndex = 0; dwIndex < (DWORD)m_InPins.size(); dwIndex++)
{
m_InPins[dwIndex]->SetD3DManager(m_spDeviceManagerUnk.Get());
}
for (DWORD dwIndex = 0; dwIndex < (DWORD)m_OutPins.size(); dwIndex++)
{
m_OutPins[dwIndex]->SetD3DManager(m_spDeviceManagerUnk.Get());
}
}
break;
case MFT_MESSAGE_NOTIFY_BEGIN_STREAMING:
{
SetStreamingState( DeviceStreamState_Run );
}
break;
case MFT_MESSAGE_NOTIFY_END_STREAMING:
{
SetStreamingState(DeviceStreamState_Stop);
}
break;
case MFT_MESSAGE_NOTIFY_END_OF_STREAM:
{
SetStreamingState(DeviceStreamState_Stop);
}
break;
default:
;
}
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr );
return hr;
}
IFACEMETHODIMP CMultipinMft::ProcessInput(
_In_ DWORD dwInputStreamID,
_In_ IMFSample* pSample,
_In_ DWORD dwFlags
)
/*++
Description:
Implements IMFTransform::ProcessInput function.This function is called
when the sourcetransform has input to feed. the pins will try to deliver the
samples to the active output pins conencted. if none are connected then just
returns the sample back to the source transform
--*/
{
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER( dwFlags );
CAutoLock lock(m_critSec);
ComPtr<CInPin> spInPin = GetInPin( dwInputStreamID );
DMFTCHECKNULL_GOTO(spInPin, done, MF_E_INVALIDSTREAMNUMBER);
if ( !IsStreaming() )
{
goto done;
}
if (m_selectedProfileId.Type == KSCAMERAPROFILE_FaceAuth_Mode)
{
// DMFT might switch to different behavior when profile, KSCAMERAPROFILE_FaceAuth_Mode is selected.
}
DMFTCHECKHR_GOTO(spInPin->SendSample( pSample ), done );
done:
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr );
//
//@@@@ README : There is a bug in the sample that the device transform manager which manages the
// device MFT does not release the sample after passing it to Device MFT in processInput like it should. The
// Device MFT therefore unfortunately has to make sure that the sample that leaves processoutput has a reference count of 1
//
SAFE_RELEASE(pSample);
return hr;
}
IFACEMETHODIMP CMultipinMft::ProcessOutput(
_In_ DWORD dwFlags,
_In_ DWORD cOutputBufferCount,
_Inout_updates_(cOutputBufferCount) MFT_OUTPUT_DATA_BUFFER *pOutputSamples,
_Out_ DWORD *pdwStatus
)
/*++
Description:
Implements IMFTransform::ProcessOutput function. This is called by the DTM when
the DT indicates it has samples to give. The DTM will send enough MFT_OUTPUT_DATA_BUFFER
pointers to be filled up as is the number of output pins available. The DT should traverse its
output pins and populate the corresponding MFT_OUTPUT_DATA_BUFFER with the samples available
--*/
{
HRESULT hr = S_OK;
BOOL gotOne = false;
ComPtr<COutPin> spOpin;
UNREFERENCED_PARAMETER( dwFlags );
if (cOutputBufferCount > m_OutputPinCount )
{
DMFTCHECKHR_GOTO( E_INVALIDARG, done );
}
*pdwStatus = 0;
for ( DWORD i = 0; i < cOutputBufferCount; i++ )
{
DWORD dwStreamID = pOutputSamples[i].dwStreamID;
{
CAutoLock _lock(m_critSec);
spOpin = nullptr;
spOpin = GetOutPin(dwStreamID);
GUID pinGuid = GUID_NULL;
DMFTCHECKNULL_GOTO(spOpin.Get(), done, E_INVALIDARG);
}
if ( SUCCEEDED(spOpin->ProcessOutput( dwFlags, &pOutputSamples[i],
pdwStatus ) ) )
{
if (pOutputSamples[i].pSample)
{
ProcessMetadata(pOutputSamples[i].pSample);
}
gotOne = true;
}
}
if (gotOne)
{
hr = S_OK;
}
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::GetInputStreamAttributes(
_In_ DWORD dwInputStreamID,
_COM_Outptr_result_maybenull_ IMFAttributes** ppAttributes
)
/*++
Description:
Implements IMFTransform::GetInputStreamAttributes function. This function
gets the specified input stream's attributes.
--*/
{
HRESULT hr = S_OK;
ComPtr<CInPin> spIPin;
CAutoLock Lock(m_critSec);
DMFTCHECKNULL_GOTO( ppAttributes, done, E_INVALIDARG );
*ppAttributes = nullptr;
spIPin = GetInPin( dwInputStreamID );
DMFTCHECKNULL_GOTO(spIPin, done, E_INVALIDARG );
hr = spIPin->getPinAttributes(ppAttributes);
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::GetOutputStreamAttributes(
_In_ DWORD dwOutputStreamID,
_Out_ IMFAttributes** ppAttributes
)
/*++
Description:
Implements IMFTransform::GetOutputStreamAttributes function. This function
gets the specified output stream's attributes.
--*/
{
HRESULT hr = S_OK;
ComPtr<COutPin> spoPin;
CAutoLock Lock(m_critSec);
DMFTCHECKNULL_GOTO(ppAttributes, done, E_INVALIDARG);
*ppAttributes = nullptr;
spoPin = GetOutPin(dwOutputStreamID);
DMFTCHECKNULL_GOTO(spoPin, done, E_INVALIDARG );
DMFTCHECKHR_GOTO(spoPin->getPinAttributes(ppAttributes), done );
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
_Requires_no_locks_held_
IFACEMETHODIMP CMultipinMft::SetInputStreamState(
_In_ DWORD dwStreamID,
_In_ IMFMediaType *pMediaType,
_In_ DeviceStreamState value,
_In_ DWORD dwFlags
)
/*++
Description:
Implements IMFdeviceTransform::SetInputStreamState function.
Sets the input stream state.
The control lock is not taken here. The lock is taken for operations on
output pins. This operation is a result of the DT notifying the DTM that
output pin change has resulted in the need for the input to be changed. In
this case the DTM sends a getpreferredinputstate and then this call
--*/
{
HRESULT hr = S_OK;
ComPtr<CInPin> spiPin = GetInPin(dwStreamID);
CAutoLock Lock(m_critSec);
DMFTCHECKNULL_GOTO(spiPin, done, MF_E_INVALIDSTREAMNUMBER);
DMFTCHECKHR_GOTO(spiPin->SetInputStreamState(pMediaType, value, dwFlags),done);
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::GetInputStreamState(
_In_ DWORD dwStreamID,
_Out_ DeviceStreamState *value
)
{
HRESULT hr = S_OK;
ComPtr<CInPin> piPin = GetInPin(dwStreamID);
DMFTCHECKNULL_GOTO(piPin, done, MF_E_INVALIDSTREAMNUMBER);
*value = piPin->GetState();
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::SetOutputStreamState(
_In_ DWORD dwStreamID,
_In_ IMFMediaType *pMediaType,
_In_ DeviceStreamState state,
_In_ DWORD dwFlags
)
/*++
Description:
Implements IMFdeviceTransform::SetOutputStreamState function.
Sets the output stream state. This is called whenever the stream
is selected or deslected i.e. started or stopped.
The control lock taken here and this operation should be atomic.
This function should check the input pins connected to the output pin
switch off the state of the input pin. Check if any other Pin connected
to the input pin is in a conflicting state with the state requested on this
output pin. Accordinly it calculates the media type to be set on the input pin
and the state to transition into. It then might recreate the other output pins
connected to it
--*/
{
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER(dwFlags);
CAutoLock Lock(m_critSec);
DMFTCHECKHR_GOTO(ChangeMediaTypeEx(dwStreamID, pMediaType, state),done);
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::GetOutputStreamState(
_In_ DWORD dwStreamID,
_Out_ DeviceStreamState *pState
)
/*++
Description:
Implements IMFdeviceTransform::GetOutputStreamState function.
Gets the output stream state.
Called by the DTM to checks states. Atomic operation. needs a lock
--*/
{
HRESULT hr = S_OK;
CAutoLock lock(m_critSec);
ComPtr<COutPin> spoPin = GetOutPin(dwStreamID);
DMFTCHECKNULL_GOTO(pState, done, E_INVALIDARG);
DMFTCHECKNULL_GOTO(spoPin, done, MF_E_INVALIDSTREAMNUMBER);
*pState = spoPin->GetState();
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::GetInputStreamPreferredState(
_In_ DWORD dwStreamID,
_Inout_ DeviceStreamState *value,
_Outptr_opt_result_maybenull_ IMFMediaType **ppMediaType
)
/*++
Description:
Implements IMFdeviceTransform::GetInputStreamPreferredState function.
Gets the preferred state and the media type to be set on the input pin.
The lock is not held as this will always be called only when we notify
DTM to call us. We notify DTM only from the context on operations
happening on the output pin
--*/
{
HRESULT hr = S_OK;
CAutoLock lock(m_critSec);
ComPtr<CInPin> spiPin = GetInPin(dwStreamID);
DMFTCHECKNULL_GOTO(ppMediaType, done, E_INVALIDARG);
DMFTCHECKNULL_GOTO(spiPin, done, MF_E_INVALIDSTREAMNUMBER);
hr = spiPin->GetInputStreamPreferredState(value, ppMediaType);
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::FlushInputStream(
_In_ DWORD dwStreamIndex,
_In_ DWORD dwFlags
)
/*++
Description:
Implements IMFdeviceTransform::FlushInputStream function.
--*/
{
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER(dwStreamIndex);
UNREFERENCED_PARAMETER(dwFlags);
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
IFACEMETHODIMP CMultipinMft::FlushOutputStream(
_In_ DWORD dwStreamIndex,
_In_ DWORD dwFlags
)
/*++
Description:
Implements IMFdeviceTransform::FlushOutputStream function.
Called by the DTM to flush streams
--*/
{
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER(dwFlags);
CAutoLock Lock(m_critSec);
ComPtr<COutPin> spoPin = GetOutPin(dwStreamIndex);
DMFTCHECKNULL_GOTO(spoPin, done, E_INVALIDARG);
DeviceStreamState oldState = spoPin->SetState(DeviceStreamState_Disabled);
DMFTCHECKHR_GOTO(spoPin->FlushQueues(),done);
spoPin->SetState(oldState);
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}
/*++
Description:
Called when the Device Transform gets a MFT_MESSAGE_COMMAND_FLUSH. We drain all the queues.
This is called in device source when the source gets end of streaming.
--*/
IFACEMETHODIMP_(VOID) CMultipinMft::FlushAllStreams(
VOID
)
{
DeviceStreamState oldState;
CAutoLock Lock(m_critSec);
for ( DWORD dwIndex = 0, dwSize = (DWORD)m_OutPins.size(); dwIndex < dwSize; dwIndex++ )
{
ComPtr<COutPin> spoPin = (COutPin *)m_OutPins[dwIndex].Get();
oldState = spoPin->SetState(DeviceStreamState_Disabled);
spoPin->FlushQueues();
//
//Restore state
//
spoPin->SetState(oldState);
}
}
//
// IKsControl interface functions
//
IFACEMETHODIMP CMultipinMft::KsProperty(
_In_reads_bytes_(ulPropertyLength) PKSPROPERTY pProperty,
_In_ ULONG ulPropertyLength,
_Inout_updates_bytes_(ulDataLength) LPVOID pvPropertyData,
_In_ ULONG ulDataLength,
_Inout_ ULONG* pulBytesReturned
)
/*++
Description: