-
Notifications
You must be signed in to change notification settings - Fork 8
/
mbase_amfilter.cpp
5203 lines (4209 loc) · 144 KB
/
mbase_amfilter.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
//------------------------------------------------------------------------------
// File: AMFilter.cpp
//
// Desc: DirectShow base classes - implements class hierarchy for streams
// architecture.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//=====================================================================
//=====================================================================
// The following classes are declared in this header:
//
//
// CBaseMediaFilter Basic IMediaFilter support (abstract class)
// CBaseFilter Support for IBaseFilter (incl. IMediaFilter)
// CEnumPins Enumerate input and output pins
// CEnumMediaTypes Enumerate the preferred pin formats
// CBasePin Abstract base class for IPin interface
// CBaseOutputPin Adds data provider member functions
// CBaseInputPin Implements IMemInputPin interface
// CMediaSample Basic transport unit for IMemInputPin
// CBaseAllocator General list guff for most allocators
// CMemAllocator Implements memory buffer allocation
//
//=====================================================================
//=====================================================================
#include <streams.h>
//=====================================================================
// Helpers
//=====================================================================
STDAPI CreateMemoryAllocator(IMemAllocator **ppAllocator)
{
return CoCreateInstance(CLSID_MemoryAllocator,
0,
CLSCTX_INPROC_SERVER,
IID_IMemAllocator,
(void **)ppAllocator);
}
// Put this one here rather than in ctlutil.cpp to avoid linking
// anything brought in by ctlutil.cpp
STDAPI CreatePosPassThru(
LPUNKNOWN pAgg,
BOOL bRenderer,
IPin *pPin,
IUnknown **ppPassThru
)
{
*ppPassThru = NULL;
IUnknown *pUnkSeek;
HRESULT hr = CoCreateInstance(CLSID_SeekingPassThru,
pAgg,
CLSCTX_INPROC_SERVER,
IID_IUnknown,
(void **)&pUnkSeek
);
if (FAILED(hr)) {
return hr;
}
ISeekingPassThru *pPassThru;
hr = pUnkSeek->QueryInterface(IID_ISeekingPassThru, (void**)&pPassThru);
if (FAILED(hr)) {
pUnkSeek->Release();
return hr;
}
hr = pPassThru->Init(bRenderer, pPin);
pPassThru->Release();
if (FAILED(hr)) {
pUnkSeek->Release();
return hr;
}
*ppPassThru = pUnkSeek;
return S_OK;
}
#define CONNECT_TRACE_LEVEL 3
//=====================================================================
//=====================================================================
// Implements CBaseMediaFilter
//=====================================================================
//=====================================================================
/* Constructor */
CBaseMediaFilter::CBaseMediaFilter(const TCHAR *pName,
LPUNKNOWN pUnk,
CCritSec *pLock,
REFCLSID clsid) :
CUnknown(pName, pUnk),
m_pLock(pLock),
m_clsid(clsid),
m_State(State_Stopped),
m_pClock(NULL)
{
}
/* Destructor */
CBaseMediaFilter::~CBaseMediaFilter()
{
// must be stopped, but can't call Stop here since
// our critsec has been destroyed.
/* Release any clock we were using */
if (m_pClock) {
m_pClock->Release();
m_pClock = NULL;
}
}
/* Override this to say what interfaces we support and where */
STDMETHODIMP
CBaseMediaFilter::NonDelegatingQueryInterface(
REFIID riid,
void ** ppv)
{
if (riid == IID_IMediaFilter) {
return GetInterface((IMediaFilter *) this, ppv);
} else if (riid == IID_IPersist) {
return GetInterface((IPersist *) this, ppv);
} else {
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
}
}
/* Return the filter's clsid */
STDMETHODIMP
CBaseMediaFilter::GetClassID(CLSID *pClsID)
{
CheckPointer(pClsID,E_POINTER);
ValidateReadWritePtr(pClsID,sizeof(CLSID));
*pClsID = m_clsid;
return NOERROR;
}
/* Override this if your state changes are not done synchronously */
STDMETHODIMP
CBaseMediaFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
{
UNREFERENCED_PARAMETER(dwMSecs);
CheckPointer(State,E_POINTER);
ValidateReadWritePtr(State,sizeof(FILTER_STATE));
*State = m_State;
return S_OK;
}
/* Set the clock we will use for synchronisation */
STDMETHODIMP
CBaseMediaFilter::SetSyncSource(IReferenceClock *pClock)
{
CAutoLock cObjectLock(m_pLock);
// Ensure the new one does not go away - even if the same as the old
if (pClock) {
pClock->AddRef();
}
// if we have a clock, release it
if (m_pClock) {
m_pClock->Release();
}
// Set the new reference clock (might be NULL)
// Should we query it to ensure it is a clock? Consider for a debug build.
m_pClock = pClock;
return NOERROR;
}
/* Return the clock we are using for synchronisation */
STDMETHODIMP
CBaseMediaFilter::GetSyncSource(IReferenceClock **pClock)
{
CheckPointer(pClock,E_POINTER);
ValidateReadWritePtr(pClock,sizeof(IReferenceClock *));
CAutoLock cObjectLock(m_pLock);
if (m_pClock) {
// returning an interface... addref it...
m_pClock->AddRef();
}
*pClock = (IReferenceClock*)m_pClock;
return NOERROR;
}
/* Put the filter into a stopped state */
STDMETHODIMP
CBaseMediaFilter::Stop()
{
CAutoLock cObjectLock(m_pLock);
m_State = State_Stopped;
return S_OK;
}
/* Put the filter into a paused state */
STDMETHODIMP
CBaseMediaFilter::Pause()
{
CAutoLock cObjectLock(m_pLock);
m_State = State_Paused;
return S_OK;
}
// Put the filter into a running state.
// The time parameter is the offset to be added to the samples'
// stream time to get the reference time at which they should be presented.
//
// you can either add these two and compare it against the reference clock,
// or you can call CBaseMediaFilter::StreamTime and compare that against
// the sample timestamp.
STDMETHODIMP
CBaseMediaFilter::Run(REFERENCE_TIME tStart)
{
CAutoLock cObjectLock(m_pLock);
// remember the stream time offset
m_tStart = tStart;
if (m_State == State_Stopped){
HRESULT hr = Pause();
if (FAILED(hr)) {
return hr;
}
}
m_State = State_Running;
return S_OK;
}
//
// return the current stream time - samples with start timestamps of this
// time or before should be rendered by now
HRESULT
CBaseMediaFilter::StreamTime(CRefTime& rtStream)
{
// Caller must lock for synchronization
// We can't grab the filter lock because we want to be able to call
// this from worker threads without deadlocking
if (m_pClock == NULL) {
return VFW_E_NO_CLOCK;
}
// get the current reference time
HRESULT hr = m_pClock->GetTime((REFERENCE_TIME*)&rtStream);
if (FAILED(hr)) {
return hr;
}
// subtract the stream offset to get stream time
rtStream -= m_tStart;
return S_OK;
}
//=====================================================================
//=====================================================================
// Implements CBaseFilter
//=====================================================================
//=====================================================================
/* Override this to say what interfaces we support and where */
STDMETHODIMP CBaseFilter::NonDelegatingQueryInterface(REFIID riid,
void **ppv)
{
/* Do we have this interface */
if (riid == IID_IBaseFilter) {
return GetInterface((IBaseFilter *) this, ppv);
} else if (riid == IID_IMediaFilter) {
return GetInterface((IMediaFilter *) this, ppv);
} else if (riid == IID_IPersist) {
return GetInterface((IPersist *) this, ppv);
} else if (riid == IID_IAMovieSetup) {
return GetInterface((IAMovieSetup *) this, ppv);
} else {
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
}
}
#ifdef DEBUG
STDMETHODIMP_(ULONG) CBaseFilter::NonDelegatingRelease()
{
if (m_cRef == 1) {
KASSERT(m_pGraph == NULL);
}
return CUnknown::NonDelegatingRelease();
}
#endif
/* Constructor */
CBaseFilter::CBaseFilter(const TCHAR *pName,
LPUNKNOWN pUnk,
CCritSec *pLock,
REFCLSID clsid) :
CUnknown( pName, pUnk ),
m_pLock(pLock),
m_clsid(clsid),
m_State(State_Stopped),
m_pClock(NULL),
m_pGraph(NULL),
m_pSink(NULL),
m_pName(NULL),
m_PinVersion(1)
{
ASSERT(pLock != NULL);
}
/* Passes in a redundant HRESULT argument */
CBaseFilter::CBaseFilter(TCHAR *pName,
LPUNKNOWN pUnk,
CCritSec *pLock,
REFCLSID clsid,
HRESULT *phr) :
CUnknown( pName, pUnk ),
m_pLock(pLock),
m_clsid(clsid),
m_State(State_Stopped),
m_pClock(NULL),
m_pGraph(NULL),
m_pSink(NULL),
m_pName(NULL),
m_PinVersion(1)
{
ASSERT(pLock != NULL);
UNREFERENCED_PARAMETER(phr);
}
#ifdef UNICODE
CBaseFilter::CBaseFilter(const CHAR *pName,
LPUNKNOWN pUnk,
CCritSec *pLock,
REFCLSID clsid) :
CUnknown( pName, pUnk ),
m_pLock(pLock),
m_clsid(clsid),
m_State(State_Stopped),
m_pClock(NULL),
m_pGraph(NULL),
m_pSink(NULL),
m_pName(NULL),
m_PinVersion(1)
{
ASSERT(pLock != NULL);
}
CBaseFilter::CBaseFilter(CHAR *pName,
LPUNKNOWN pUnk,
CCritSec *pLock,
REFCLSID clsid,
HRESULT *phr) :
CUnknown( pName, pUnk ),
m_pLock(pLock),
m_clsid(clsid),
m_State(State_Stopped),
m_pClock(NULL),
m_pGraph(NULL),
m_pSink(NULL),
m_pName(NULL),
m_PinVersion(1)
{
ASSERT(pLock != NULL);
UNREFERENCED_PARAMETER(phr);
}
#endif
/* Destructor */
CBaseFilter::~CBaseFilter()
{
// NOTE we do NOT hold references on the filtergraph for m_pGraph or m_pSink
// When we did we had the circular reference problem. Nothing would go away.
delete[] m_pName;
// must be stopped, but can't call Stop here since
// our critsec has been destroyed.
/* Release any clock we were using */
if (m_pClock) {
m_pClock->Release();
m_pClock = NULL;
}
}
/* Return the filter's clsid */
STDMETHODIMP
CBaseFilter::GetClassID(CLSID *pClsID)
{
CheckPointer(pClsID,E_POINTER);
ValidateReadWritePtr(pClsID,sizeof(CLSID));
*pClsID = m_clsid;
return NOERROR;
}
/* Override this if your state changes are not done synchronously */
STDMETHODIMP
CBaseFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
{
UNREFERENCED_PARAMETER(dwMSecs);
CheckPointer(State,E_POINTER);
ValidateReadWritePtr(State,sizeof(FILTER_STATE));
*State = m_State;
return S_OK;
}
/* Set the clock we will use for synchronisation */
STDMETHODIMP
CBaseFilter::SetSyncSource(IReferenceClock *pClock)
{
CAutoLock cObjectLock(m_pLock);
// Ensure the new one does not go away - even if the same as the old
if (pClock) {
pClock->AddRef();
}
// if we have a clock, release it
if (m_pClock) {
m_pClock->Release();
}
// Set the new reference clock (might be NULL)
// Should we query it to ensure it is a clock? Consider for a debug build.
m_pClock = pClock;
return NOERROR;
}
/* Return the clock we are using for synchronisation */
STDMETHODIMP
CBaseFilter::GetSyncSource(IReferenceClock **pClock)
{
CheckPointer(pClock,E_POINTER);
ValidateReadWritePtr(pClock,sizeof(IReferenceClock *));
CAutoLock cObjectLock(m_pLock);
if (m_pClock) {
// returning an interface... addref it...
m_pClock->AddRef();
}
*pClock = (IReferenceClock*)m_pClock;
return NOERROR;
}
// override CBaseMediaFilter Stop method, to deactivate any pins this
// filter has.
STDMETHODIMP
CBaseFilter::Stop()
{
CAutoLock cObjectLock(m_pLock);
HRESULT hr = NOERROR;
// notify all pins of the state change
if (m_State != State_Stopped) {
int cPins = GetPinCount();
for (int c = 0; c < cPins; c++) {
CBasePin *pPin = GetPin(c);
// Disconnected pins are not activated - this saves pins worrying
// about this state themselves. We ignore the return code to make
// sure everyone is inactivated regardless. The base input pin
// class can return an error if it has no allocator but Stop can
// be used to resync the graph state after something has gone bad
if (pPin->IsConnected()) {
HRESULT hrTmp = pPin->Inactive();
if (FAILED(hrTmp) && SUCCEEDED(hr)) {
hr = hrTmp;
}
}
}
}
m_State = State_Stopped;
return hr;
}
// override CBaseMediaFilter Pause method to activate any pins
// this filter has (also called from Run)
STDMETHODIMP
CBaseFilter::Pause()
{
CAutoLock cObjectLock(m_pLock);
// notify all pins of the change to active state
if (m_State == State_Stopped) {
int cPins = GetPinCount();
for (int c = 0; c < cPins; c++) {
CBasePin *pPin = GetPin(c);
// Disconnected pins are not activated - this saves pins
// worrying about this state themselves
if (pPin->IsConnected()) {
HRESULT hr = pPin->Active();
if (FAILED(hr)) {
return hr;
}
}
}
}
m_State = State_Paused;
return S_OK;
}
// Put the filter into a running state.
// The time parameter is the offset to be added to the samples'
// stream time to get the reference time at which they should be presented.
//
// you can either add these two and compare it against the reference clock,
// or you can call CBaseFilter::StreamTime and compare that against
// the sample timestamp.
STDMETHODIMP
CBaseFilter::Run(REFERENCE_TIME tStart)
{
CAutoLock cObjectLock(m_pLock);
// remember the stream time offset
m_tStart = tStart;
if (m_State == State_Stopped){
HRESULT hr = Pause();
if (FAILED(hr)) {
return hr;
}
}
// notify all pins of the change to active state
if (m_State != State_Running) {
int cPins = GetPinCount();
for (int c = 0; c < cPins; c++) {
CBasePin *pPin = GetPin(c);
// Disconnected pins are not activated - this saves pins
// worrying about this state themselves
if (pPin->IsConnected()) {
HRESULT hr = pPin->Run(tStart);
if (FAILED(hr)) {
return hr;
}
}
}
}
m_State = State_Running;
return S_OK;
}
//
// return the current stream time - samples with start timestamps of this
// time or before should be rendered by now
HRESULT
CBaseFilter::StreamTime(CRefTime& rtStream)
{
// Caller must lock for synchronization
// We can't grab the filter lock because we want to be able to call
// this from worker threads without deadlocking
if (m_pClock == NULL) {
return VFW_E_NO_CLOCK;
}
// get the current reference time
HRESULT hr = m_pClock->GetTime((REFERENCE_TIME*)&rtStream);
if (FAILED(hr)) {
return hr;
}
// subtract the stream offset to get stream time
rtStream -= m_tStart;
return S_OK;
}
/* Create an enumerator for the pins attached to this filter */
STDMETHODIMP
CBaseFilter::EnumPins(IEnumPins **ppEnum)
{
CheckPointer(ppEnum,E_POINTER);
ValidateReadWritePtr(ppEnum,sizeof(IEnumPins *));
/* Create a new ref counted enumerator */
*ppEnum = new CEnumPins(this,
NULL);
return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
}
// default behaviour of FindPin is to assume pins are named
// by their pin names
STDMETHODIMP
CBaseFilter::FindPin(
LPCWSTR Id,
IPin ** ppPin
)
{
CheckPointer(ppPin,E_POINTER);
ValidateReadWritePtr(ppPin,sizeof(IPin *));
// We're going to search the pin list so maintain integrity
CAutoLock lck(m_pLock);
int iCount = GetPinCount();
for (int i = 0; i < iCount; i++) {
CBasePin *pPin = GetPin(i);
ASSERT(pPin != NULL);
if (0 == lstrcmpW(pPin->Name(), Id)) {
// Found one that matches
//
// AddRef() and return it
*ppPin = pPin;
pPin->AddRef();
return S_OK;
}
}
*ppPin = NULL;
return VFW_E_NOT_FOUND;
}
/* Return information about this filter */
STDMETHODIMP
CBaseFilter::QueryFilterInfo(FILTER_INFO * pInfo)
{
CheckPointer(pInfo,E_POINTER);
ValidateReadWritePtr(pInfo,sizeof(FILTER_INFO));
if (m_pName) {
lstrcpynW(pInfo->achName, m_pName, sizeof(pInfo->achName)/sizeof(WCHAR));
} else {
pInfo->achName[0] = L'\0';
}
pInfo->pGraph = m_pGraph;
if (m_pGraph)
m_pGraph->AddRef();
return NOERROR;
}
/* Provide the filter with a filter graph */
STDMETHODIMP
CBaseFilter::JoinFilterGraph(
IFilterGraph * pGraph,
LPCWSTR pName)
{
CAutoLock cObjectLock(m_pLock);
// NOTE: we no longer hold references on the graph (m_pGraph, m_pSink)
m_pGraph = pGraph;
if (m_pGraph) {
HRESULT hr = m_pGraph->QueryInterface(IID_IMediaEventSink,
(void**) &m_pSink);
if (FAILED(hr)) {
ASSERT(m_pSink == NULL);
}
else m_pSink->Release(); // we do NOT keep a reference on it.
} else {
// if graph pointer is null, then we should
// also release the IMediaEventSink on the same object - we don't
// refcount it, so just set it to null
m_pSink = NULL;
}
if (m_pName) {
delete[] m_pName;
m_pName = NULL;
}
if (pName) {
DWORD nameLen = lstrlenW(pName)+1;
m_pName = new WCHAR[nameLen];
if (m_pName) {
CopyMemory(m_pName, pName, nameLen*sizeof(WCHAR));
} else {
// !!! error here?
ASSERT(FALSE);
}
}
return NOERROR;
}
// return a Vendor information string. Optional - may return E_NOTIMPL.
// memory returned should be freed using CoTaskMemFree
// default implementation returns E_NOTIMPL
STDMETHODIMP
CBaseFilter::QueryVendorInfo(
LPWSTR* pVendorInfo)
{
UNREFERENCED_PARAMETER(pVendorInfo);
return E_NOTIMPL;
}
// send an event notification to the filter graph if we know about it.
// returns S_OK if delivered, S_FALSE if the filter graph does not sink
// events, or an error otherwise.
HRESULT
CBaseFilter::NotifyEvent(
long EventCode,
LONG_PTR EventParam1,
LONG_PTR EventParam2)
{
// Snapshot so we don't have to lock up
IMediaEventSink *pSink = m_pSink;
if (pSink) {
if (EC_COMPLETE == EventCode) {
EventParam2 = (LONG_PTR)(IBaseFilter*)this;
}
return pSink->Notify(EventCode, EventParam1, EventParam2);
} else {
return E_NOTIMPL;
}
}
// Request reconnect
// pPin is the pin to reconnect
// pmt is the type to reconnect with - can be NULL
// Calls ReconnectEx on the filter graph
HRESULT
CBaseFilter::ReconnectPin(
IPin *pPin,
AM_MEDIA_TYPE const *pmt
)
{
IFilterGraph2 *pGraph2;
if (m_pGraph != NULL) {
HRESULT hr = m_pGraph->QueryInterface(IID_IFilterGraph2, (void **)&pGraph2);
if (SUCCEEDED(hr)) {
hr = pGraph2->ReconnectEx(pPin, pmt);
pGraph2->Release();
return hr;
} else {
return m_pGraph->Reconnect(pPin);
}
} else {
return E_NOINTERFACE;
}
}
/* This is the same idea as the media type version does for type enumeration
on pins but for the list of pins available. So if the list of pins you
provide changes dynamically then either override this virtual function
to provide the version number, or more simply call IncrementPinVersion */
LONG CBaseFilter::GetPinVersion()
{
return m_PinVersion;
}
/* Increment the current pin version cookie */
void CBaseFilter::IncrementPinVersion()
{
InterlockedIncrement(&m_PinVersion);
}
/* register filter */
STDMETHODIMP CBaseFilter::Register()
{
// get setup data, if it exists
//
LPAMOVIESETUP_FILTER psetupdata = GetSetupData();
// check we've got data
//
if( NULL == psetupdata ) return S_FALSE;
// init is ref counted so call just in case
// we're being called cold.
//
HRESULT hr = CoInitialize( (LPVOID)NULL );
ASSERT( SUCCEEDED(hr) );
// get hold of IFilterMapper
//
IFilterMapper *pIFM;
hr = CoCreateInstance( CLSID_FilterMapper
, NULL
, CLSCTX_INPROC_SERVER
, IID_IFilterMapper
, (void **)&pIFM );
if( SUCCEEDED(hr) )
{
hr = AMovieSetupRegisterFilter( psetupdata, pIFM, TRUE );
pIFM->Release();
}
// and clear up
//
CoFreeUnusedLibraries();
CoUninitialize();
return NOERROR;
}
/* unregister filter */
STDMETHODIMP CBaseFilter::Unregister()
{
// get setup data, if it exists
//
LPAMOVIESETUP_FILTER psetupdata = GetSetupData();
// check we've got data
//
if( NULL == psetupdata ) return S_FALSE;
// OLE init is ref counted so call
// just in case we're being called cold.
//
HRESULT hr = CoInitialize( (LPVOID)NULL );
ASSERT( SUCCEEDED(hr) );
// get hold of IFilterMapper
//
IFilterMapper *pIFM;
hr = CoCreateInstance( CLSID_FilterMapper
, NULL
, CLSCTX_INPROC_SERVER
, IID_IFilterMapper
, (void **)&pIFM );
if( SUCCEEDED(hr) )
{
hr = AMovieSetupRegisterFilter( psetupdata, pIFM, FALSE );
// release interface
//
pIFM->Release();
}
// clear up
//
CoFreeUnusedLibraries();
CoUninitialize();
// handle one acceptable "error" - that
// of filter not being registered!
// (couldn't find a suitable #define'd
// name for the error!)
//
if( 0x80070002 == hr)
return NOERROR;
else
return hr;
}
//=====================================================================
//=====================================================================
// Implements CEnumPins
//=====================================================================
//=====================================================================
CEnumPins::CEnumPins(CBaseFilter *pFilter,
CEnumPins *pEnumPins) :
m_Position(0),
m_PinCount(0),
m_pFilter(pFilter),
m_cRef(1), // Already ref counted
m_PinCache(NAME("Pin Cache"))
{
#ifdef DEBUG
m_dwCookie = DbgRegisterObjectCreation("CEnumPins", 0);
#endif
/* We must be owned by a filter derived from CBaseFilter */
ASSERT(pFilter != NULL);
/* Hold a reference count on our filter */
m_pFilter->AddRef();
/* Are we creating a new enumerator */
if (pEnumPins == NULL) {
m_Version = m_pFilter->GetPinVersion();
m_PinCount = m_pFilter->GetPinCount();
} else {
ASSERT(m_Position <= m_PinCount);
m_Position = pEnumPins->m_Position;
m_PinCount = pEnumPins->m_PinCount;
m_Version = pEnumPins->m_Version;
m_PinCache.AddTail(&(pEnumPins->m_PinCache));
}
}
/* Destructor releases the reference count on our filter NOTE since we hold
a reference count on the filter who created us we know it is safe to
release it, no access can be made to it afterwards though as we have just
caused the last reference count to go and the object to be deleted */
CEnumPins::~CEnumPins()
{
m_pFilter->Release();
#ifdef DEBUG
DbgRegisterObjectDestruction(m_dwCookie);
#endif
}
/* Override this to say what interfaces we support where */
STDMETHODIMP
CEnumPins::QueryInterface(REFIID riid,void **ppv)
{
CheckPointer(ppv, E_POINTER);
/* Do we have this interface */
if (riid == IID_IEnumPins || riid == IID_IUnknown) {
return GetInterface((IEnumPins *) this, ppv);
} else {
*ppv = NULL;
return E_NOINTERFACE;
}
}
STDMETHODIMP_(ULONG)
CEnumPins::AddRef()
{
return InterlockedIncrement(&m_cRef);
}