1
//------------------------------------------------------------------------------
4
// Desc: DirectShow base classes - implements class hierarchy for streams
7
// Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
8
//------------------------------------------------------------------------------
11
//=====================================================================
12
//=====================================================================
13
// The following classes are declared in this header:
16
// CBaseMediaFilter Basic IMediaFilter support (abstract class)
17
// CBaseFilter Support for IBaseFilter (incl. IMediaFilter)
18
// CEnumPins Enumerate input and output pins
19
// CEnumMediaTypes Enumerate the preferred pin formats
20
// CBasePin Abstract base class for IPin interface
21
// CBaseOutputPin Adds data provider member functions
22
// CBaseInputPin Implements IMemInputPin interface
23
// CMediaSample Basic transport unit for IMemInputPin
24
// CBaseAllocator General list guff for most allocators
25
// CMemAllocator Implements memory buffer allocation
27
//=====================================================================
28
//=====================================================================
30
#include <pjmedia-videodev/config.h>
32
#if defined(PJMEDIA_VIDEO_DEV_HAS_DSHOW) && PJMEDIA_VIDEO_DEV_HAS_DSHOW != 0
42
//=====================================================================
44
//=====================================================================
45
STDAPI CreateMemoryAllocator(__deref_out IMemAllocator **ppAllocator)
47
return CoCreateInstance(CLSID_MemoryAllocator,
51
(void **)ppAllocator);
54
// Put this one here rather than in ctlutil.cpp to avoid linking
55
// anything brought in by ctlutil.cpp
56
STDAPI CreatePosPassThru(
57
__in_opt LPUNKNOWN pAgg,
60
__deref_out IUnknown **ppPassThru
65
HRESULT hr = CoCreateInstance(CLSID_SeekingPassThru,
75
ISeekingPassThru *pPassThru;
76
hr = pUnkSeek->QueryInterface(IID_ISeekingPassThru, (void**)&pPassThru);
81
hr = pPassThru->Init(bRenderer, pPin);
87
*ppPassThru = pUnkSeek;
93
#define CONNECT_TRACE_LEVEL 3
95
//=====================================================================
96
//=====================================================================
97
// Implements CBaseMediaFilter
98
//=====================================================================
99
//=====================================================================
104
CBaseMediaFilter::CBaseMediaFilter(__in_opt LPCTSTR pName,
105
__inout_opt LPUNKNOWN pUnk,
106
__in CCritSec *pLock,
108
CUnknown(pName, pUnk),
111
m_State(State_Stopped),
119
CBaseMediaFilter::~CBaseMediaFilter()
121
// must be stopped, but can't call Stop here since
122
// our critsec has been destroyed.
124
/* Release any clock we were using */
133
/* Override this to say what interfaces we support and where */
136
CBaseMediaFilter::NonDelegatingQueryInterface(
138
__deref_out void ** ppv)
140
if (riid == IID_IMediaFilter) {
141
return GetInterface((IMediaFilter *) this, ppv);
142
} else if (riid == IID_IPersist) {
143
return GetInterface((IPersist *) this, ppv);
145
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
149
/* Return the filter's clsid */
151
CBaseMediaFilter::GetClassID(__out CLSID *pClsID)
153
CheckPointer(pClsID,E_POINTER);
154
ValidateReadWritePtr(pClsID,sizeof(CLSID));
159
/* Override this if your state changes are not done synchronously */
162
CBaseMediaFilter::GetState(DWORD dwMSecs, __out FILTER_STATE *State)
164
UNREFERENCED_PARAMETER(dwMSecs);
165
CheckPointer(State,E_POINTER);
166
ValidateReadWritePtr(State,sizeof(FILTER_STATE));
173
/* Set the clock we will use for synchronisation */
176
CBaseMediaFilter::SetSyncSource(__inout_opt IReferenceClock *pClock)
178
CAutoLock cObjectLock(m_pLock);
180
// Ensure the new one does not go away - even if the same as the old
185
// if we have a clock, release it
190
// Set the new reference clock (might be NULL)
191
// Should we query it to ensure it is a clock? Consider for a debug build.
197
/* Return the clock we are using for synchronisation */
199
CBaseMediaFilter::GetSyncSource(__deref_out_opt IReferenceClock **pClock)
201
CheckPointer(pClock,E_POINTER);
202
ValidateReadWritePtr(pClock,sizeof(IReferenceClock *));
203
CAutoLock cObjectLock(m_pLock);
206
// returning an interface... addref it...
209
*pClock = (IReferenceClock*)m_pClock;
214
/* Put the filter into a stopped state */
217
CBaseMediaFilter::Stop()
219
CAutoLock cObjectLock(m_pLock);
221
m_State = State_Stopped;
226
/* Put the filter into a paused state */
229
CBaseMediaFilter::Pause()
231
CAutoLock cObjectLock(m_pLock);
233
m_State = State_Paused;
238
// Put the filter into a running state.
240
// The time parameter is the offset to be added to the samples'
241
// stream time to get the reference time at which they should be presented.
243
// you can either add these two and compare it against the reference clock,
244
// or you can call CBaseMediaFilter::StreamTime and compare that against
245
// the sample timestamp.
248
CBaseMediaFilter::Run(REFERENCE_TIME tStart)
250
CAutoLock cObjectLock(m_pLock);
252
// remember the stream time offset
255
if (m_State == State_Stopped){
256
HRESULT hr = Pause();
262
m_State = State_Running;
268
// return the current stream time - samples with start timestamps of this
269
// time or before should be rendered by now
271
CBaseMediaFilter::StreamTime(CRefTime& rtStream)
273
// Caller must lock for synchronization
274
// We can't grab the filter lock because we want to be able to call
275
// this from worker threads without deadlocking
277
if (m_pClock == NULL) {
278
return VFW_E_NO_CLOCK;
281
// get the current reference time
282
HRESULT hr = m_pClock->GetTime((REFERENCE_TIME*)&rtStream);
287
// subtract the stream offset to get stream time
288
rtStream -= m_tStart;
294
//=====================================================================
295
//=====================================================================
296
// Implements CBaseFilter
297
//=====================================================================
298
//=====================================================================
301
/* Override this to say what interfaces we support and where */
303
STDMETHODIMP CBaseFilter::NonDelegatingQueryInterface(REFIID riid,
304
__deref_out void **ppv)
306
/* Do we have this interface */
308
if (riid == IID_IBaseFilter) {
309
return GetInterface((IBaseFilter *) this, ppv);
310
} else if (riid == IID_IMediaFilter) {
311
return GetInterface((IMediaFilter *) this, ppv);
312
} else if (riid == IID_IPersist) {
313
return GetInterface((IPersist *) this, ppv);
314
} else if (riid == IID_IAMovieSetup) {
315
return GetInterface((IAMovieSetup *) this, ppv);
317
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
322
STDMETHODIMP_(ULONG) CBaseFilter::NonDelegatingRelease()
325
KASSERT(m_pGraph == NULL);
327
return CUnknown::NonDelegatingRelease();
334
CBaseFilter::CBaseFilter(__in_opt LPCTSTR pName,
335
__inout_opt LPUNKNOWN pUnk,
336
__in CCritSec *pLock,
338
CUnknown( pName, pUnk ),
341
m_State(State_Stopped),
349
PERFLOG_CTOR( pName ? pName : L"CBaseFilter", (IBaseFilter *) this );
352
ASSERT(pLock != NULL);
355
/* Passes in a redundant HRESULT argument */
357
CBaseFilter::CBaseFilter(__in_opt LPCTSTR pName,
358
__in_opt LPUNKNOWN pUnk,
359
__in CCritSec *pLock,
361
__inout HRESULT *phr) :
362
CUnknown( pName, pUnk ),
365
m_State(State_Stopped),
373
PERFLOG_CTOR( pName ? pName : L"CBaseFilter", (IBaseFilter *) this );
376
ASSERT(pLock != NULL);
377
UNREFERENCED_PARAMETER(phr);
381
CBaseFilter::CBaseFilter(__in_opt LPCSTR pName,
382
__in_opt LPUNKNOWN pUnk,
383
__in CCritSec *pLock,
385
CUnknown( pName, pUnk ),
388
m_State(State_Stopped),
396
PERFLOG_CTOR( L"CBaseFilter", (IBaseFilter *) this );
399
ASSERT(pLock != NULL);
401
CBaseFilter::CBaseFilter(__in_opt LPCSTR pName,
402
__in_opt LPUNKNOWN pUnk,
403
__in CCritSec *pLock,
405
__inout HRESULT *phr) :
406
CUnknown( pName, pUnk ),
409
m_State(State_Stopped),
417
PERFLOG_CTOR( L"CBaseFilter", (IBaseFilter *) this );
420
ASSERT(pLock != NULL);
421
UNREFERENCED_PARAMETER(phr);
427
CBaseFilter::~CBaseFilter()
430
PERFLOG_DTOR( L"CBaseFilter", (IBaseFilter *) this );
433
// NOTE we do NOT hold references on the filtergraph for m_pGraph or m_pSink
434
// When we did we had the circular reference problem. Nothing would go away.
438
// must be stopped, but can't call Stop here since
439
// our critsec has been destroyed.
441
/* Release any clock we were using */
448
/* Return the filter's clsid */
450
CBaseFilter::GetClassID(__out CLSID *pClsID)
452
CheckPointer(pClsID,E_POINTER);
453
ValidateReadWritePtr(pClsID,sizeof(CLSID));
458
/* Override this if your state changes are not done synchronously */
460
CBaseFilter::GetState(DWORD dwMSecs, __out FILTER_STATE *State)
462
UNREFERENCED_PARAMETER(dwMSecs);
463
CheckPointer(State,E_POINTER);
464
ValidateReadWritePtr(State,sizeof(FILTER_STATE));
471
/* Set the clock we will use for synchronisation */
474
CBaseFilter::SetSyncSource(__in_opt IReferenceClock *pClock)
476
CAutoLock cObjectLock(m_pLock);
478
// Ensure the new one does not go away - even if the same as the old
483
// if we have a clock, release it
488
// Set the new reference clock (might be NULL)
489
// Should we query it to ensure it is a clock? Consider for a debug build.
495
/* Return the clock we are using for synchronisation */
497
CBaseFilter::GetSyncSource(__deref_out_opt IReferenceClock **pClock)
499
CheckPointer(pClock,E_POINTER);
500
ValidateReadWritePtr(pClock,sizeof(IReferenceClock *));
501
CAutoLock cObjectLock(m_pLock);
504
// returning an interface... addref it...
507
*pClock = (IReferenceClock*)m_pClock;
513
// override CBaseMediaFilter Stop method, to deactivate any pins this
518
CAutoLock cObjectLock(m_pLock);
519
HRESULT hr = NOERROR;
521
// notify all pins of the state change
522
if (m_State != State_Stopped) {
523
int cPins = GetPinCount();
524
for (int c = 0; c < cPins; c++) {
526
CBasePin *pPin = GetPin(c);
531
// Disconnected pins are not activated - this saves pins worrying
532
// about this state themselves. We ignore the return code to make
533
// sure everyone is inactivated regardless. The base input pin
534
// class can return an error if it has no allocator but Stop can
535
// be used to resync the graph state after something has gone bad
537
if (pPin->IsConnected()) {
538
HRESULT hrTmp = pPin->Inactive();
539
if (FAILED(hrTmp) && SUCCEEDED(hr)) {
547
PERFLOG_STOP( m_pName ? m_pName : L"CBaseFilter", (IBaseFilter *) this, m_State );
550
m_State = State_Stopped;
555
// override CBaseMediaFilter Pause method to activate any pins
556
// this filter has (also called from Run)
561
CAutoLock cObjectLock(m_pLock);
563
// notify all pins of the change to active state
564
if (m_State == State_Stopped) {
565
int cPins = GetPinCount();
566
for (int c = 0; c < cPins; c++) {
568
CBasePin *pPin = GetPin(c);
573
// Disconnected pins are not activated - this saves pins
574
// worrying about this state themselves
576
if (pPin->IsConnected()) {
577
HRESULT hr = pPin->Active();
587
PERFLOG_PAUSE( m_pName ? m_pName : L"CBaseFilter", (IBaseFilter *) this, m_State );
590
m_State = State_Paused;
594
// Put the filter into a running state.
596
// The time parameter is the offset to be added to the samples'
597
// stream time to get the reference time at which they should be presented.
599
// you can either add these two and compare it against the reference clock,
600
// or you can call CBaseFilter::StreamTime and compare that against
601
// the sample timestamp.
604
CBaseFilter::Run(REFERENCE_TIME tStart)
606
CAutoLock cObjectLock(m_pLock);
608
// remember the stream time offset
611
if (m_State == State_Stopped){
612
HRESULT hr = Pause();
618
// notify all pins of the change to active state
619
if (m_State != State_Running) {
620
int cPins = GetPinCount();
621
for (int c = 0; c < cPins; c++) {
623
CBasePin *pPin = GetPin(c);
628
// Disconnected pins are not activated - this saves pins
629
// worrying about this state themselves
631
if (pPin->IsConnected()) {
632
HRESULT hr = pPin->Run(tStart);
641
PERFLOG_RUN( m_pName ? m_pName : L"CBaseFilter", (IBaseFilter *) this, tStart, m_State );
644
m_State = State_Running;
649
// return the current stream time - samples with start timestamps of this
650
// time or before should be rendered by now
652
CBaseFilter::StreamTime(CRefTime& rtStream)
654
// Caller must lock for synchronization
655
// We can't grab the filter lock because we want to be able to call
656
// this from worker threads without deadlocking
658
if (m_pClock == NULL) {
659
return VFW_E_NO_CLOCK;
662
// get the current reference time
663
HRESULT hr = m_pClock->GetTime((REFERENCE_TIME*)&rtStream);
668
// subtract the stream offset to get stream time
669
rtStream -= m_tStart;
675
/* Create an enumerator for the pins attached to this filter */
678
CBaseFilter::EnumPins(__deref_out IEnumPins **ppEnum)
680
CheckPointer(ppEnum,E_POINTER);
681
ValidateReadWritePtr(ppEnum,sizeof(IEnumPins *));
683
/* Create a new ref counted enumerator */
685
*ppEnum = new CEnumPins(this,
688
return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
692
// default behaviour of FindPin is to assume pins are named
693
// by their pin names
695
CBaseFilter::FindPin(
697
__deref_out IPin ** ppPin
700
CheckPointer(ppPin,E_POINTER);
701
ValidateReadWritePtr(ppPin,sizeof(IPin *));
703
// We're going to search the pin list so maintain integrity
704
CAutoLock lck(m_pLock);
705
int iCount = GetPinCount();
706
for (int i = 0; i < iCount; i++) {
707
CBasePin *pPin = GetPin(i);
712
if (0 == lstrcmpW(pPin->Name(), Id)) {
713
// Found one that matches
715
// AddRef() and return it
722
return VFW_E_NOT_FOUND;
725
/* Return information about this filter */
728
CBaseFilter::QueryFilterInfo(__out FILTER_INFO * pInfo)
730
CheckPointer(pInfo,E_POINTER);
731
ValidateReadWritePtr(pInfo,sizeof(FILTER_INFO));
734
(void)StringCchCopyW(pInfo->achName, NUMELMS(pInfo->achName), m_pName);
736
pInfo->achName[0] = L'\0';
738
pInfo->pGraph = m_pGraph;
745
/* Provide the filter with a filter graph */
748
CBaseFilter::JoinFilterGraph(
749
__inout_opt IFilterGraph * pGraph,
750
__in_opt LPCWSTR pName)
752
CAutoLock cObjectLock(m_pLock);
754
// NOTE: we no longer hold references on the graph (m_pGraph, m_pSink)
758
HRESULT hr = m_pGraph->QueryInterface(IID_IMediaEventSink,
761
ASSERT(m_pSink == NULL);
763
else m_pSink->Release(); // we do NOT keep a reference on it.
765
// if graph pointer is null, then we should
766
// also release the IMediaEventSink on the same object - we don't
767
// refcount it, so just set it to null
779
HRESULT hr = StringCchLengthW(pName, STRSAFE_MAX_CCH, &namelen);
783
m_pName = new WCHAR[namelen + 1];
785
(void)StringCchCopyW(m_pName, namelen + 1, pName);
787
return E_OUTOFMEMORY;
792
PERFLOG_JOINGRAPH( m_pName ? m_pName : L"CBaseFilter",(IBaseFilter *) this, pGraph );
799
// return a Vendor information string. Optional - may return E_NOTIMPL.
800
// memory returned should be freed using CoTaskMemFree
801
// default implementation returns E_NOTIMPL
803
CBaseFilter::QueryVendorInfo(
804
__deref_out LPWSTR* pVendorInfo)
806
UNREFERENCED_PARAMETER(pVendorInfo);
811
// send an event notification to the filter graph if we know about it.
812
// returns S_OK if delivered, S_FALSE if the filter graph does not sink
813
// events, or an error otherwise.
815
CBaseFilter::NotifyEvent(
817
LONG_PTR EventParam1,
818
LONG_PTR EventParam2)
820
// Snapshot so we don't have to lock up
821
IMediaEventSink *pSink = m_pSink;
823
if (EC_COMPLETE == EventCode) {
824
EventParam2 = (LONG_PTR)(IBaseFilter*)this;
827
return pSink->Notify(EventCode, EventParam1, EventParam2);
834
// pPin is the pin to reconnect
835
// pmt is the type to reconnect with - can be NULL
836
// Calls ReconnectEx on the filter graph
838
CBaseFilter::ReconnectPin(
840
__in_opt AM_MEDIA_TYPE const *pmt
843
IFilterGraph2 *pGraph2;
844
if (m_pGraph != NULL) {
845
HRESULT hr = m_pGraph->QueryInterface(IID_IFilterGraph2, (void **)&pGraph2);
847
hr = pGraph2->ReconnectEx(pPin, pmt);
851
return m_pGraph->Reconnect(pPin);
854
return E_NOINTERFACE;
860
/* This is the same idea as the media type version does for type enumeration
861
on pins but for the list of pins available. So if the list of pins you
862
provide changes dynamically then either override this virtual function
863
to provide the version number, or more simply call IncrementPinVersion */
865
LONG CBaseFilter::GetPinVersion()
871
/* Increment the current pin version cookie */
873
void CBaseFilter::IncrementPinVersion()
875
InterlockedIncrement(&m_PinVersion);
878
/* register filter */
880
STDMETHODIMP CBaseFilter::Register()
882
// get setup data, if it exists
884
LPAMOVIESETUP_FILTER psetupdata = GetSetupData();
886
// check we've got data
888
if( NULL == psetupdata ) return S_FALSE;
890
// init is ref counted so call just in case
891
// we're being called cold.
893
HRESULT hr = CoInitialize( (LPVOID)NULL );
894
ASSERT( SUCCEEDED(hr) );
896
// get hold of IFilterMapper
899
hr = CoCreateInstance( CLSID_FilterMapper
901
, CLSCTX_INPROC_SERVER
906
hr = AMovieSetupRegisterFilter( psetupdata, pIFM, TRUE );
912
CoFreeUnusedLibraries();
919
/* unregister filter */
921
STDMETHODIMP CBaseFilter::Unregister()
923
// get setup data, if it exists
925
LPAMOVIESETUP_FILTER psetupdata = GetSetupData();
927
// check we've got data
929
if( NULL == psetupdata ) return S_FALSE;
931
// OLE init is ref counted so call
932
// just in case we're being called cold.
934
HRESULT hr = CoInitialize( (LPVOID)NULL );
935
ASSERT( SUCCEEDED(hr) );
937
// get hold of IFilterMapper
940
hr = CoCreateInstance( CLSID_FilterMapper
942
, CLSCTX_INPROC_SERVER
947
hr = AMovieSetupRegisterFilter( psetupdata, pIFM, FALSE );
956
CoFreeUnusedLibraries();
959
// handle one acceptable "error" - that
960
// of filter not being registered!
961
// (couldn't find a suitable #define'd
962
// name for the error!)
964
if( 0x80070002 == hr)
971
//=====================================================================
972
//=====================================================================
973
// Implements CEnumPins
974
//=====================================================================
975
//=====================================================================
978
CEnumPins::CEnumPins(__in CBaseFilter *pFilter,
979
__in_opt CEnumPins *pEnumPins) :
983
m_cRef(1), // Already ref counted
984
m_PinCache(NAME("Pin Cache"))
988
m_dwCookie = DbgRegisterObjectCreation("CEnumPins", 0);
991
/* We must be owned by a filter derived from CBaseFilter */
993
ASSERT(pFilter != NULL);
995
/* Hold a reference count on our filter */
998
/* Are we creating a new enumerator */
1000
if (pEnumPins == NULL) {
1001
m_Version = m_pFilter->GetPinVersion();
1002
m_PinCount = m_pFilter->GetPinCount();
1004
ASSERT(m_Position <= m_PinCount);
1005
m_Position = pEnumPins->m_Position;
1006
m_PinCount = pEnumPins->m_PinCount;
1007
m_Version = pEnumPins->m_Version;
1008
m_PinCache.AddTail(&(pEnumPins->m_PinCache));
1013
/* Destructor releases the reference count on our filter NOTE since we hold
1014
a reference count on the filter who created us we know it is safe to
1015
release it, no access can be made to it afterwards though as we have just
1016
caused the last reference count to go and the object to be deleted */
1018
CEnumPins::~CEnumPins()
1020
m_pFilter->Release();
1023
DbgRegisterObjectDestruction(m_dwCookie);
1028
/* Override this to say what interfaces we support where */
1031
CEnumPins::QueryInterface(REFIID riid, __deref_out void **ppv)
1033
CheckPointer(ppv, E_POINTER);
1035
/* Do we have this interface */
1037
if (riid == IID_IEnumPins || riid == IID_IUnknown) {
1038
return GetInterface((IEnumPins *) this, ppv);
1041
return E_NOINTERFACE;
1045
STDMETHODIMP_(ULONG)
1048
return InterlockedIncrement(&m_cRef);
1051
STDMETHODIMP_(ULONG)
1052
CEnumPins::Release()
1054
ULONG cRef = InterlockedDecrement(&m_cRef);
1061
/* One of an enumerator's basic member functions allows us to create a cloned
1062
interface that initially has the same state. Since we are taking a snapshot
1063
of an object (current position and all) we must lock access at the start */
1066
CEnumPins::Clone(__deref_out IEnumPins **ppEnum)
1068
CheckPointer(ppEnum,E_POINTER);
1069
ValidateReadWritePtr(ppEnum,sizeof(IEnumPins *));
1070
HRESULT hr = NOERROR;
1072
/* Check we are still in sync with the filter */
1073
if (AreWeOutOfSync() == TRUE) {
1075
hr = VFW_E_ENUM_OUT_OF_SYNC;
1077
*ppEnum = new CEnumPins(m_pFilter,
1079
if (*ppEnum == NULL) {
1087
/* Return the next pin after the current position */
1090
CEnumPins::Next(ULONG cPins, // place this many pins...
1091
__out_ecount(cPins) IPin **ppPins, // ...in this array
1092
__out_opt ULONG *pcFetched) // actual count passed returned here
1094
CheckPointer(ppPins,E_POINTER);
1095
ValidateReadWritePtr(ppPins,cPins * sizeof(IPin *));
1099
if (pcFetched!=NULL) {
1100
ValidateWritePtr(pcFetched, sizeof(ULONG));
1101
*pcFetched = 0; // default unless we succeed
1103
// now check that the parameter is valid
1104
else if (cPins>1) { // pcFetched == NULL
1105
return E_INVALIDARG;
1107
ULONG cFetched = 0; // increment as we get each one.
1109
/* Check we are still in sync with the filter */
1110
if (AreWeOutOfSync() == TRUE) {
1111
// If we are out of sync, we should refresh the enumerator.
1112
// This will reset the position and update the other members, but
1113
// will not clear cache of pins we have already returned.
1117
/* Return each pin interface NOTE GetPin returns CBasePin * not addrefed
1118
so we must QI for the IPin (which increments its reference count)
1119
If while we are retrieving a pin from the filter an error occurs we
1120
assume that our internal state is stale with respect to the filter
1121
(for example someone has deleted a pin) so we
1122
return VFW_E_ENUM_OUT_OF_SYNC */
1124
while (cFetched < cPins && m_PinCount > m_Position) {
1126
/* Get the next pin object from the filter */
1128
CBasePin *pPin = m_pFilter->GetPin(m_Position++);
1130
// If this happend, and it's not the first time through, then we've got a problem,
1131
// since we should really go back and release the iPins, which we have previously
1133
ASSERT( cFetched==0 );
1134
return VFW_E_ENUM_OUT_OF_SYNC;
1137
/* We only want to return this pin, if it is not in our cache */
1138
if (0 == m_PinCache.Find(pPin))
1140
/* From the object get an IPin interface */
1148
m_PinCache.AddTail(pPin);
1152
if (pcFetched!=NULL) {
1153
*pcFetched = cFetched;
1156
return (cPins==cFetched ? NOERROR : S_FALSE);
1160
/* Skip over one or more entries in the enumerator */
1163
CEnumPins::Skip(ULONG cPins)
1165
/* Check we are still in sync with the filter */
1166
if (AreWeOutOfSync() == TRUE) {
1167
return VFW_E_ENUM_OUT_OF_SYNC;
1170
/* Work out how many pins are left to skip over */
1171
/* We could position at the end if we are asked to skip too many... */
1172
/* ..which would match the base implementation for CEnumMediaTypes::Skip */
1174
ULONG PinsLeft = m_PinCount - m_Position;
1175
if (cPins > PinsLeft) {
1178
m_Position += cPins;
1183
/* Set the current position back to the start */
1184
/* Reset has 4 simple steps:
1186
* Set position to head of list
1187
* Sync enumerator with object being enumerated
1188
* Clear the cache of pins already returned
1195
m_Version = m_pFilter->GetPinVersion();
1196
m_PinCount = m_pFilter->GetPinCount();
1201
m_PinCache.RemoveAll();
1207
/* Set the current position back to the start */
1208
/* Refresh has 3 simple steps:
1210
* Set position to head of list
1211
* Sync enumerator with object being enumerated
1216
CEnumPins::Refresh()
1218
m_Version = m_pFilter->GetPinVersion();
1219
m_PinCount = m_pFilter->GetPinCount();
1226
//=====================================================================
1227
//=====================================================================
1228
// Implements CEnumMediaTypes
1229
//=====================================================================
1230
//=====================================================================
1233
CEnumMediaTypes::CEnumMediaTypes(__in CBasePin *pPin,
1234
__in_opt CEnumMediaTypes *pEnumMediaTypes) :
1241
m_dwCookie = DbgRegisterObjectCreation("CEnumMediaTypes", 0);
1244
/* We must be owned by a pin derived from CBasePin */
1246
ASSERT(pPin != NULL);
1248
/* Hold a reference count on our pin */
1251
/* Are we creating a new enumerator */
1253
if (pEnumMediaTypes == NULL) {
1254
m_Version = m_pPin->GetMediaTypeVersion();
1258
m_Position = pEnumMediaTypes->m_Position;
1259
m_Version = pEnumMediaTypes->m_Version;
1263
/* Destructor releases the reference count on our base pin. NOTE since we hold
1264
a reference count on the pin who created us we know it is safe to release
1265
it, no access can be made to it afterwards though as we might have just
1266
caused the last reference count to go and the object to be deleted */
1268
CEnumMediaTypes::~CEnumMediaTypes()
1271
DbgRegisterObjectDestruction(m_dwCookie);
1277
/* Override this to say what interfaces we support where */
1280
CEnumMediaTypes::QueryInterface(REFIID riid, __deref_out void **ppv)
1282
CheckPointer(ppv, E_POINTER);
1284
/* Do we have this interface */
1286
if (riid == IID_IEnumMediaTypes || riid == IID_IUnknown) {
1287
return GetInterface((IEnumMediaTypes *) this, ppv);
1290
return E_NOINTERFACE;
1294
STDMETHODIMP_(ULONG)
1295
CEnumMediaTypes::AddRef()
1297
return InterlockedIncrement(&m_cRef);
1300
STDMETHODIMP_(ULONG)
1301
CEnumMediaTypes::Release()
1303
ULONG cRef = InterlockedDecrement(&m_cRef);
1310
/* One of an enumerator's basic member functions allows us to create a cloned
1311
interface that initially has the same state. Since we are taking a snapshot
1312
of an object (current position and all) we must lock access at the start */
1315
CEnumMediaTypes::Clone(__deref_out IEnumMediaTypes **ppEnum)
1317
CheckPointer(ppEnum,E_POINTER);
1318
ValidateReadWritePtr(ppEnum,sizeof(IEnumMediaTypes *));
1319
HRESULT hr = NOERROR;
1321
/* Check we are still in sync with the pin */
1322
if (AreWeOutOfSync() == TRUE) {
1324
hr = VFW_E_ENUM_OUT_OF_SYNC;
1327
*ppEnum = new CEnumMediaTypes(m_pPin,
1330
if (*ppEnum == NULL) {
1338
/* Enumerate the next pin(s) after the current position. The client using this
1339
interface passes in a pointer to an array of pointers each of which will
1340
be filled in with a pointer to a fully initialised media type format
1341
Return NOERROR if it all works,
1342
S_FALSE if fewer than cMediaTypes were enumerated.
1343
VFW_E_ENUM_OUT_OF_SYNC if the enumerator has been broken by
1344
state changes in the filter
1345
The actual count always correctly reflects the number of types in the array.
1349
CEnumMediaTypes::Next(ULONG cMediaTypes, // place this many types...
1350
__out_ecount(cMediaTypes) AM_MEDIA_TYPE **ppMediaTypes, // ...in this array
1351
__out ULONG *pcFetched) // actual count passed
1353
CheckPointer(ppMediaTypes,E_POINTER);
1354
ValidateReadWritePtr(ppMediaTypes,cMediaTypes * sizeof(AM_MEDIA_TYPE *));
1355
/* Check we are still in sync with the pin */
1356
if (AreWeOutOfSync() == TRUE) {
1357
return VFW_E_ENUM_OUT_OF_SYNC;
1360
if (pcFetched!=NULL) {
1361
ValidateWritePtr(pcFetched, sizeof(ULONG));
1362
*pcFetched = 0; // default unless we succeed
1364
// now check that the parameter is valid
1365
else if (cMediaTypes>1) { // pcFetched == NULL
1366
return E_INVALIDARG;
1368
ULONG cFetched = 0; // increment as we get each one.
1370
/* Return each media type by asking the filter for them in turn - If we
1371
have an error code retured to us while we are retrieving a media type
1372
we assume that our internal state is stale with respect to the filter
1373
(for example the window size changing) so we return
1374
VFW_E_ENUM_OUT_OF_SYNC */
1376
while (cMediaTypes) {
1380
HRESULT hr = m_pPin->GetMediaType(m_Position++, &cmt);
1385
/* We now have a CMediaType object that contains the next media type
1386
but when we assign it to the array position we CANNOT just assign
1387
the AM_MEDIA_TYPE structure because as soon as the object goes out of
1388
scope it will delete the memory we have just copied. The function
1389
we use is CreateMediaType which allocates a task memory block */
1391
/* Transfer across the format block manually to save an allocate
1392
and free on the format block and generally go faster */
1394
*ppMediaTypes = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1395
if (*ppMediaTypes == NULL) {
1399
/* Do a regular copy */
1400
**ppMediaTypes = cmt;
1402
/* Make sure the destructor doesn't free these */
1403
cmt.pbFormat = NULL;
1404
cmt.cbFormat = NULL;
1413
if (pcFetched!=NULL) {
1414
*pcFetched = cFetched;
1417
return ( cMediaTypes==0 ? NOERROR : S_FALSE );
1421
/* Skip over one or more entries in the enumerator */
1424
CEnumMediaTypes::Skip(ULONG cMediaTypes)
1426
// If we're skipping 0 elements we're guaranteed to skip the
1427
// correct number of elements
1428
if (cMediaTypes == 0) {
1432
/* Check we are still in sync with the pin */
1433
if (AreWeOutOfSync() == TRUE) {
1434
return VFW_E_ENUM_OUT_OF_SYNC;
1437
m_Position += cMediaTypes;
1439
/* See if we're over the end */
1441
return S_OK == m_pPin->GetMediaType(m_Position - 1, &cmt) ? S_OK : S_FALSE;
1445
/* Set the current position back to the start */
1446
/* Reset has 3 simple steps:
1448
* set position to head of list
1449
* sync enumerator with object being enumerated
1454
CEnumMediaTypes::Reset()
1459
// Bring the enumerator back into step with the current state. This
1460
// may be a noop but ensures that the enumerator will be valid on the
1462
m_Version = m_pPin->GetMediaTypeVersion();
1467
//=====================================================================
1468
//=====================================================================
1469
// Implements CBasePin
1470
//=====================================================================
1471
//=====================================================================
1474
/* NOTE The implementation of this class calls the CUnknown constructor with
1475
a NULL outer unknown pointer. This has the effect of making us a self
1476
contained class, ie any QueryInterface, AddRef or Release calls will be
1477
routed to the class's NonDelegatingUnknown methods. You will typically
1478
find that the classes that do this then override one or more of these
1479
virtual functions to provide more specialised behaviour. A good example
1480
of this is where a class wants to keep the QueryInterface internal but
1481
still wants its lifetime controlled by the external object */
1485
CBasePin::CBasePin(__in_opt LPCTSTR pObjectName,
1486
__in CBaseFilter *pFilter,
1487
__in CCritSec *pLock,
1488
__inout HRESULT *phr,
1489
__in_opt LPCWSTR pName,
1490
PIN_DIRECTION dir) :
1491
CUnknown( pObjectName, NULL ),
1497
m_bRunTimeError(FALSE),
1502
m_bCanReconnectWhenActive(false),
1503
m_bTryMyTypesFirst(false),
1506
/* WARNING - pFilter is often not a properly constituted object at
1507
this state (in particular QueryInterface may not work) - this
1508
is because its owner is often its containing object and we
1509
have been called from the containing object's constructor so
1510
the filter's owner has not yet had its CUnknown constructor
1514
PERFLOG_CTOR( pName ? pName : L"CBasePin", (IPin *) this );
1517
ASSERT(pFilter != NULL);
1518
ASSERT(pLock != NULL);
1522
HRESULT hr = StringCchLengthW(pName, STRSAFE_MAX_CCH, &cchName);
1523
if (SUCCEEDED(hr)) {
1524
m_pName = new WCHAR[cchName + 1];
1526
(void)StringCchCopyW(m_pName, cchName + 1, pName);
1537
CBasePin::CBasePin(__in_opt LPCSTR pObjectName,
1538
__in CBaseFilter *pFilter,
1539
__in CCritSec *pLock,
1540
__inout HRESULT *phr,
1541
__in_opt LPCWSTR pName,
1542
PIN_DIRECTION dir) :
1543
CUnknown( pObjectName, NULL ),
1549
m_bRunTimeError(FALSE),
1554
m_bCanReconnectWhenActive(false),
1555
m_bTryMyTypesFirst(false),
1558
/* WARNING - pFilter is often not a properly constituted object at
1559
this state (in particular QueryInterface may not work) - this
1560
is because its owner is often its containing object and we
1561
have been called from the containing object's constructor so
1562
the filter's owner has not yet had its CUnknown constructor
1566
PERFLOG_CTOR( pName ? pName : L"CBasePin", (IPin *) this );
1569
ASSERT(pFilter != NULL);
1570
ASSERT(pLock != NULL);
1574
HRESULT hr = StringCchLengthW(pName, STRSAFE_MAX_CCH, &cchName);
1575
if (SUCCEEDED(hr)) {
1576
m_pName = new WCHAR[cchName + 1];
1578
(void)StringCchCopyW(m_pName, cchName + 1, pName);
1590
/* Destructor since a connected pin holds a reference count on us there is
1591
no way that we can be deleted unless we are not currently connected */
1593
CBasePin::~CBasePin()
1596
PERFLOG_DTOR( m_pName ? m_pName : L"CBasePin", (IPin *) this );
1599
// We don't call disconnect because if the filter is going away
1600
// all the pins must have a reference count of zero so they must
1601
// have been disconnected anyway - (but check the assumption)
1602
ASSERT(m_Connected == FALSE);
1606
// check the internal reference count is consistent
1607
ASSERT(m_cRef == 0);
1611
/* Override this to say what interfaces we support and where */
1614
CBasePin::NonDelegatingQueryInterface(REFIID riid, __deref_out void ** ppv)
1616
/* Do we have this interface */
1618
if (riid == IID_IPin) {
1619
return GetInterface((IPin *) this, ppv);
1620
} else if (riid == IID_IQualityControl) {
1621
return GetInterface((IQualityControl *) this, ppv);
1623
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
1628
/* Override to increment the owning filter's reference count */
1630
STDMETHODIMP_(ULONG)
1631
CBasePin::NonDelegatingAddRef()
1633
ASSERT(InterlockedIncrement(&m_cRef) > 0);
1634
return m_pFilter->AddRef();
1638
/* Override to decrement the owning filter's reference count */
1640
STDMETHODIMP_(ULONG)
1641
CBasePin::NonDelegatingRelease()
1643
ASSERT(InterlockedDecrement(&m_cRef) >= 0);
1644
return m_pFilter->Release();
1648
/* Displays pin connection information */
1652
CBasePin::DisplayPinInfo(IPin *pReceivePin)
1655
if (DbgCheckModuleLevel(LOG_TRACE, CONNECT_TRACE_LEVEL)) {
1656
PIN_INFO ConnectPinInfo;
1657
PIN_INFO ReceivePinInfo;
1659
if (FAILED(QueryPinInfo(&ConnectPinInfo))) {
1660
StringCchCopyW(ConnectPinInfo.achName, sizeof(ConnectPinInfo.achName)/sizeof(WCHAR), L"Bad Pin");
1662
QueryPinInfoReleaseFilter(ConnectPinInfo);
1665
if (FAILED(pReceivePin->QueryPinInfo(&ReceivePinInfo))) {
1666
StringCchCopyW(ReceivePinInfo.achName, sizeof(ReceivePinInfo.achName)/sizeof(WCHAR), L"Bad Pin");
1668
QueryPinInfoReleaseFilter(ReceivePinInfo);
1671
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Trying to connect Pins :")));
1672
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" <%ls>"), ConnectPinInfo.achName));
1673
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" <%ls>"), ReceivePinInfo.achName));
1679
/* Displays general information on the pin media type */
1682
void CBasePin::DisplayTypeInfo(IPin *pPin, const CMediaType *pmt)
1684
UNREFERENCED_PARAMETER(pPin);
1685
if (DbgCheckModuleLevel(LOG_TRACE, CONNECT_TRACE_LEVEL)) {
1686
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Trying media type:")));
1687
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" major type: %hs"),
1688
GuidNames[*pmt->Type()]));
1689
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT(" sub type : %hs"),
1690
GuidNames[*pmt->Subtype()]));
1695
/* Asked to connect to a pin. A pin is always attached to an owning filter
1696
object so we always delegate our locking to that object. We first of all
1697
retrieve a media type enumerator for the input pin and see if we accept
1698
any of the formats that it would ideally like, failing that we retrieve
1699
our enumerator and see if it will accept any of our preferred types */
1704
__in_opt const AM_MEDIA_TYPE *pmt // optional media type
1707
CheckPointer(pReceivePin,E_POINTER);
1708
ValidateReadPtr(pReceivePin,sizeof(IPin));
1709
CAutoLock cObjectLock(m_pLock);
1710
DisplayPinInfo(pReceivePin);
1712
/* See if we are already connected */
1715
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Already connected")));
1716
return VFW_E_ALREADY_CONNECTED;
1719
/* See if the filter is active */
1720
if (!IsStopped() && !m_bCanReconnectWhenActive) {
1721
return VFW_E_NOT_STOPPED;
1725
// Find a mutually agreeable media type -
1726
// Pass in the template media type. If this is partially specified,
1727
// each of the enumerated media types will need to be checked against
1728
// it. If it is non-null and fully specified, we will just try to connect
1731
const CMediaType * ptype = (CMediaType*)pmt;
1732
HRESULT hr = AgreeMediaType(pReceivePin, ptype);
1734
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Failed to agree type")));
1736
// Since the procedure is already returning an error code, there
1737
// is nothing else this function can do to report the error.
1738
EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );
1741
PERFLOG_CONNECT( (IPin *) this, pReceivePin, hr, pmt );
1747
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Connection succeeded")));
1750
PERFLOG_CONNECT( (IPin *) this, pReceivePin, NOERROR, pmt );
1756
// given a specific media type, attempt a connection (includes
1757
// checking that the type is acceptable to this pin)
1759
CBasePin::AttemptConnection(
1760
IPin* pReceivePin, // connect to this pin
1761
const CMediaType* pmt // using this type
1764
// The caller should hold the filter lock becasue this function
1765
// uses m_Connected. The caller should also hold the filter lock
1766
// because this function calls SetMediaType(), IsStopped() and
1767
// CompleteConnect().
1768
ASSERT(CritCheckIn(m_pLock));
1770
// Check that the connection is valid -- need to do this for every
1771
// connect attempt since BreakConnect will undo it.
1772
HRESULT hr = CheckConnect(pReceivePin);
1774
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("CheckConnect failed")));
1776
// Since the procedure is already returning an error code, there
1777
// is nothing else this function can do to report the error.
1778
EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );
1783
DisplayTypeInfo(pReceivePin, pmt);
1785
/* Check we will accept this media type */
1787
hr = CheckMediaType(pmt);
1788
if (hr == NOERROR) {
1790
/* Make ourselves look connected otherwise ReceiveConnection
1791
may not be able to complete the connection
1793
m_Connected = pReceivePin;
1794
m_Connected->AddRef();
1795
hr = SetMediaType(pmt);
1796
if (SUCCEEDED(hr)) {
1797
/* See if the other pin will accept this type */
1799
hr = pReceivePin->ReceiveConnection((IPin *)this, pmt);
1800
if (SUCCEEDED(hr)) {
1801
/* Complete the connection */
1803
hr = CompleteConnect(pReceivePin);
1804
if (SUCCEEDED(hr)) {
1808
CONNECT_TRACE_LEVEL,
1809
TEXT("Failed to complete connection")));
1810
pReceivePin->Disconnect();
1815
// we cannot use this media type
1817
// return a specific media type error if there is one
1818
// or map a general failure code to something more helpful
1819
// (in particular S_FALSE gets changed to an error code)
1820
if (SUCCEEDED(hr) ||
1822
(hr == E_INVALIDARG)) {
1823
hr = VFW_E_TYPE_NOT_ACCEPTED;
1827
// BreakConnect and release any connection here in case CheckMediaType
1828
// failed, or if we set anything up during a call back during
1829
// ReceiveConnection.
1831
// Since the procedure is already returning an error code, there
1832
// is nothing else this function can do to report the error.
1833
EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );
1835
/* If failed then undo our state */
1837
m_Connected->Release();
1844
/* Given an enumerator we cycle through all the media types it proposes and
1845
firstly suggest them to our derived pin class and if that succeeds try
1846
them with the pin in a ReceiveConnection call. This means that if our pin
1847
proposes a media type we still check in here that we can support it. This
1848
is deliberate so that in simple cases the enumerator can hold all of the
1849
media types even if some of them are not really currently available */
1851
HRESULT CBasePin::TryMediaTypes(
1853
__in_opt const CMediaType *pmt,
1854
IEnumMediaTypes *pEnum)
1856
/* Reset the current enumerator position */
1858
HRESULT hr = pEnum->Reset();
1863
CMediaType *pMediaType = NULL;
1864
ULONG ulMediaCount = 0;
1866
// attempt to remember a specific error code if there is one
1867
HRESULT hrFailure = S_OK;
1871
/* Retrieve the next media type NOTE each time round the loop the
1872
enumerator interface will allocate another AM_MEDIA_TYPE structure
1873
If we are successful then we copy it into our output object, if
1874
not then we must delete the memory allocated before returning */
1876
hr = pEnum->Next(1, (AM_MEDIA_TYPE**)&pMediaType,&ulMediaCount);
1878
if (S_OK == hrFailure) {
1879
hrFailure = VFW_E_NO_ACCEPTABLE_TYPES;
1885
ASSERT(ulMediaCount == 1);
1888
// check that this matches the partial type (if any)
1892
pMediaType->MatchesPartial(pmt))) {
1894
hr = AttemptConnection(pReceivePin, pMediaType);
1896
// attempt to remember a specific error code
1898
SUCCEEDED(hrFailure) &&
1900
(hr != E_INVALIDARG) &&
1901
(hr != VFW_E_TYPE_NOT_ACCEPTED)) {
1905
hr = VFW_E_NO_ACCEPTABLE_TYPES;
1909
DeleteMediaType(pMediaType);
1920
/* This is called to make the connection, including the taask of finding
1921
a media type for the pin connection. pmt is the proposed media type
1922
from the Connect call: if this is fully specified, we will try that.
1923
Otherwise we enumerate and try all the input pin's types first and
1924
if that fails we then enumerate and try all our preferred media types.
1925
For each media type we check it against pmt (if non-null and partially
1926
specified) as well as checking that both pins will accept it.
1929
HRESULT CBasePin::AgreeMediaType(
1931
const CMediaType *pmt)
1933
ASSERT(pReceivePin);
1934
IEnumMediaTypes *pEnumMediaTypes = NULL;
1936
// if the media type is fully specified then use that
1937
if ( (pmt != NULL) && (!pmt->IsPartiallySpecified())) {
1939
// if this media type fails, then we must fail the connection
1940
// since if pmt is nonnull we are only allowed to connect
1941
// using a type that matches it.
1943
return AttemptConnection(pReceivePin, pmt);
1947
/* Try the other pin's enumerator */
1949
HRESULT hrFailure = VFW_E_NO_ACCEPTABLE_TYPES;
1951
for (int i = 0; i < 2; i++) {
1953
if (i == (int)m_bTryMyTypesFirst) {
1954
hr = pReceivePin->EnumMediaTypes(&pEnumMediaTypes);
1956
hr = EnumMediaTypes(&pEnumMediaTypes);
1958
if (SUCCEEDED(hr)) {
1959
ASSERT(pEnumMediaTypes);
1960
hr = TryMediaTypes(pReceivePin,pmt,pEnumMediaTypes);
1961
pEnumMediaTypes->Release();
1962
if (SUCCEEDED(hr)) {
1965
// try to remember specific error codes if there are any
1966
if ((hr != E_FAIL) &&
1967
(hr != E_INVALIDARG) &&
1968
(hr != VFW_E_TYPE_NOT_ACCEPTED)) {
1979
/* Called when we want to complete a connection to another filter. Failing
1980
this will also fail the connection and disconnect the other pin as well */
1983
CBasePin::CompleteConnect(IPin *pReceivePin)
1985
UNREFERENCED_PARAMETER(pReceivePin);
1990
/* This is called to set the format for a pin connection - CheckMediaType
1991
will have been called to check the connection format and if it didn't
1992
return an error code then this (virtual) function will be invoked */
1995
CBasePin::SetMediaType(const CMediaType *pmt)
1997
HRESULT hr = m_mt.Set(*pmt);
2006
/* This is called during Connect() to provide a virtual method that can do
2007
any specific check needed for connection such as QueryInterface. This
2008
base class method just checks that the pin directions don't match */
2011
CBasePin::CheckConnect(IPin * pPin)
2013
/* Check that pin directions DONT match */
2016
pPin->QueryDirection(&pd);
2018
ASSERT((pd == PINDIR_OUTPUT) || (pd == PINDIR_INPUT));
2019
ASSERT((m_dir == PINDIR_OUTPUT) || (m_dir == PINDIR_INPUT));
2021
// we should allow for non-input and non-output connections?
2023
return VFW_E_INVALID_DIRECTION;
2029
/* This is called when we realise we can't make a connection to the pin and
2030
must undo anything we did in CheckConnect - override to release QIs done */
2033
CBasePin::BreakConnect()
2039
/* Called normally by an output pin on an input pin to try and establish a
2044
CBasePin::ReceiveConnection(
2045
IPin * pConnector, // this is the pin who we will connect to
2046
const AM_MEDIA_TYPE *pmt // this is the media type we will exchange
2049
CheckPointer(pConnector,E_POINTER);
2050
CheckPointer(pmt,E_POINTER);
2051
ValidateReadPtr(pConnector,sizeof(IPin));
2052
ValidateReadPtr(pmt,sizeof(AM_MEDIA_TYPE));
2053
CAutoLock cObjectLock(m_pLock);
2055
/* Are we already connected */
2057
return VFW_E_ALREADY_CONNECTED;
2060
/* See if the filter is active */
2061
if (!IsStopped() && !m_bCanReconnectWhenActive) {
2062
return VFW_E_NOT_STOPPED;
2065
HRESULT hr = CheckConnect(pConnector);
2067
// Since the procedure is already returning an error code, there
2068
// is nothing else this function can do to report the error.
2069
EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );
2072
PERFLOG_RXCONNECT( pConnector, (IPin *) this, hr, pmt );
2078
/* Ask derived class if this media type is ok */
2080
CMediaType * pcmt = (CMediaType*) pmt;
2081
hr = CheckMediaType(pcmt);
2082
if (hr != NOERROR) {
2083
// no -we don't support this media type
2085
// Since the procedure is already returning an error code, there
2086
// is nothing else this function can do to report the error.
2087
EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );
2089
// return a specific media type error if there is one
2090
// or map a general failure code to something more helpful
2091
// (in particular S_FALSE gets changed to an error code)
2092
if (SUCCEEDED(hr) ||
2094
(hr == E_INVALIDARG)) {
2095
hr = VFW_E_TYPE_NOT_ACCEPTED;
2099
PERFLOG_RXCONNECT( pConnector, (IPin *) this, hr, pmt );
2105
/* Complete the connection */
2107
m_Connected = pConnector;
2108
m_Connected->AddRef();
2109
hr = SetMediaType(pcmt);
2110
if (SUCCEEDED(hr)) {
2111
hr = CompleteConnect(pConnector);
2112
if (SUCCEEDED(hr)) {
2115
PERFLOG_RXCONNECT( pConnector, (IPin *) this, NOERROR, pmt );
2122
DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Failed to set the media type or failed to complete the connection.")));
2123
m_Connected->Release();
2126
// Since the procedure is already returning an error code, there
2127
// is nothing else this function can do to report the error.
2128
EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );
2131
PERFLOG_RXCONNECT( pConnector, (IPin *) this, hr, pmt );
2138
/* Called when we want to terminate a pin connection */
2141
CBasePin::Disconnect()
2143
CAutoLock cObjectLock(m_pLock);
2145
/* See if the filter is active */
2147
return VFW_E_NOT_STOPPED;
2150
return DisconnectInternal();
2154
CBasePin::DisconnectInternal()
2156
ASSERT(CritCheckIn(m_pLock));
2159
HRESULT hr = BreakConnect();
2160
if( FAILED( hr ) ) {
2163
PERFLOG_DISCONNECT( (IPin *) this, m_Connected, hr );
2166
// There is usually a bug in the program if BreakConnect() fails.
2167
DbgBreak( "WARNING: BreakConnect() failed in CBasePin::Disconnect()." );
2171
m_Connected->Release();
2175
PERFLOG_DISCONNECT( (IPin *) this, m_Connected, S_OK );
2180
// no connection - not an error
2183
PERFLOG_DISCONNECT( (IPin *) this, m_Connected, S_FALSE );
2191
/* Return an AddRef()'d pointer to the connected pin if there is one */
2193
CBasePin::ConnectedTo(
2194
__deref_out IPin **ppPin
2197
CheckPointer(ppPin,E_POINTER);
2198
ValidateReadWritePtr(ppPin,sizeof(IPin *));
2200
// It's pointless to lock here.
2201
// The caller should ensure integrity.
2204
IPin *pPin = m_Connected;
2210
ASSERT(*ppPin == NULL);
2211
return VFW_E_NOT_CONNECTED;
2215
/* Return the media type of the connection */
2217
CBasePin::ConnectionMediaType(
2218
__out AM_MEDIA_TYPE *pmt
2221
CheckPointer(pmt,E_POINTER);
2222
ValidateReadWritePtr(pmt,sizeof(AM_MEDIA_TYPE));
2223
CAutoLock cObjectLock(m_pLock);
2225
/* Copy constructor of m_mt allocates the memory */
2226
if (IsConnected()) {
2227
CopyMediaType( pmt, &m_mt );
2230
((CMediaType *)pmt)->InitMediaType();
2231
return VFW_E_NOT_CONNECTED;
2235
/* Return information about the filter we are connect to */
2238
CBasePin::QueryPinInfo(
2239
__out PIN_INFO * pInfo
2242
CheckPointer(pInfo,E_POINTER);
2243
ValidateReadWritePtr(pInfo,sizeof(PIN_INFO));
2245
pInfo->pFilter = m_pFilter;
2247
m_pFilter->AddRef();
2251
(void)StringCchCopyW(pInfo->achName, NUMELMS(pInfo->achName), m_pName);
2253
pInfo->achName[0] = L'\0';
2262
CBasePin::QueryDirection(
2263
__out PIN_DIRECTION * pPinDir
2266
CheckPointer(pPinDir,E_POINTER);
2267
ValidateReadWritePtr(pPinDir,sizeof(PIN_DIRECTION));
2273
// Default QueryId to return the pin's name
2276
__deref_out LPWSTR * Id
2279
// We're not going away because someone's got a pointer to us
2280
// so there's no need to lock
2282
return AMGetWideString(Name(), Id);
2285
/* Does this pin support this media type WARNING this interface function does
2286
not lock the main object as it is meant to be asynchronous by nature - if
2287
the media types you support depend on some internal state that is updated
2288
dynamically then you will need to implement locking in a derived class */
2291
CBasePin::QueryAccept(
2292
const AM_MEDIA_TYPE *pmt
2295
CheckPointer(pmt,E_POINTER);
2296
ValidateReadPtr(pmt,sizeof(AM_MEDIA_TYPE));
2298
/* The CheckMediaType method is valid to return error codes if the media
2299
type is horrible, an example might be E_INVALIDARG. What we do here
2300
is map all the error codes into either S_OK or S_FALSE regardless */
2302
HRESULT hr = CheckMediaType((CMediaType*)pmt);
2306
// note that the only defined success codes should be S_OK and S_FALSE...
2311
/* This can be called to return an enumerator for the pin's list of preferred
2312
media types. An input pin is not obliged to have any preferred formats
2313
although it can do. For example, the window renderer has a preferred type
2314
which describes a video image that matches the current window size. All
2315
output pins should expose at least one preferred format otherwise it is
2316
possible that neither pin has any types and so no connection is possible */
2319
CBasePin::EnumMediaTypes(
2320
__deref_out IEnumMediaTypes **ppEnum
2323
CheckPointer(ppEnum,E_POINTER);
2324
ValidateReadWritePtr(ppEnum,sizeof(IEnumMediaTypes *));
2326
/* Create a new ref counted enumerator */
2328
*ppEnum = new CEnumMediaTypes(this,
2331
if (*ppEnum == NULL) {
2332
return E_OUTOFMEMORY;
2340
/* This is a virtual function that returns a media type corresponding with
2341
place iPosition in the list. This base class simply returns an error as
2342
we support no media types by default but derived classes should override */
2344
HRESULT CBasePin::GetMediaType(int iPosition, __inout CMediaType *pMediaType)
2346
UNREFERENCED_PARAMETER(iPosition);
2347
UNREFERENCED_PARAMETER(pMediaType);
2348
return E_UNEXPECTED;
2352
/* This is a virtual function that returns the current media type version.
2353
The base class initialises the media type enumerators with the value 1
2354
By default we always returns that same value. A Derived class may change
2355
the list of media types available and after doing so it should increment
2356
the version either in a method derived from this, or more simply by just
2357
incrementing the m_TypeVersion base pin variable. The type enumerators
2358
call this when they want to see if their enumerations are out of date */
2360
LONG CBasePin::GetMediaTypeVersion()
2362
return m_TypeVersion;
2366
/* Increment the cookie representing the current media type version */
2368
void CBasePin::IncrementTypeVersion()
2370
InterlockedIncrement(&m_TypeVersion);
2374
/* Called by IMediaFilter implementation when the state changes from Stopped
2375
to either paused or running and in derived classes could do things like
2376
commit memory and grab hardware resource (the default is to do nothing) */
2379
CBasePin::Active(void)
2384
/* Called by IMediaFilter implementation when the state changes from
2385
to either paused to running and in derived classes could do things like
2386
commit memory and grab hardware resource (the default is to do nothing) */
2389
CBasePin::Run(REFERENCE_TIME tStart)
2391
UNREFERENCED_PARAMETER(tStart);
2396
/* Also called by the IMediaFilter implementation when the state changes to
2397
Stopped at which point you should decommit allocators and free hardware
2398
resources you grabbed in the Active call (default is also to do nothing) */
2401
CBasePin::Inactive(void)
2403
m_bRunTimeError = FALSE;
2408
// Called when no more data will arrive
2410
CBasePin::EndOfStream(void)
2417
CBasePin::SetSink(IQualityControl * piqc)
2419
CAutoLock cObjectLock(m_pLock);
2420
if (piqc) ValidateReadPtr(piqc,sizeof(IQualityControl));
2427
CBasePin::Notify(IBaseFilter * pSender, Quality q)
2429
UNREFERENCED_PARAMETER(q);
2430
UNREFERENCED_PARAMETER(pSender);
2431
DbgBreak("IQualityControl::Notify not over-ridden from CBasePin. (IGNORE is OK)");
2436
// NewSegment notifies of the start/stop/rate applying to the data
2437
// about to be received. Default implementation records data and
2439
// Override this to pass downstream.
2441
CBasePin::NewSegment(
2442
REFERENCE_TIME tStart,
2443
REFERENCE_TIME tStop,
2454
//=====================================================================
2455
//=====================================================================
2456
// Implements CBaseOutputPin
2457
//=====================================================================
2458
//=====================================================================
2461
CBaseOutputPin::CBaseOutputPin(__in_opt LPCTSTR pObjectName,
2462
__in CBaseFilter *pFilter,
2463
__in CCritSec *pLock,
2464
__inout HRESULT *phr,
2465
__in_opt LPCWSTR pName) :
2466
CBasePin(pObjectName, pFilter, pLock, phr, pName, PINDIR_OUTPUT),
2474
CBaseOutputPin::CBaseOutputPin(__in_opt LPCSTR pObjectName,
2475
__in CBaseFilter *pFilter,
2476
__in CCritSec *pLock,
2477
__inout HRESULT *phr,
2478
__in_opt LPCWSTR pName) :
2479
CBasePin(pObjectName, pFilter, pLock, phr, pName, PINDIR_OUTPUT),
2487
/* This is called after a media type has been proposed
2489
Try to complete the connection by agreeing the allocator
2492
CBaseOutputPin::CompleteConnect(IPin *pReceivePin)
2494
UNREFERENCED_PARAMETER(pReceivePin);
2495
return DecideAllocator(m_pInputPin, &m_pAllocator);
2499
/* This method is called when the output pin is about to try and connect to
2500
an input pin. It is at this point that you should try and grab any extra
2501
interfaces that you need, in this case IMemInputPin. Because this is
2502
only called if we are not currently connected we do NOT need to call
2503
BreakConnect. This also makes it easier to derive classes from us as
2504
BreakConnect is only called when we actually have to break a connection
2505
(or a partly made connection) and not when we are checking a connection */
2507
/* Overriden from CBasePin */
2510
CBaseOutputPin::CheckConnect(IPin * pPin)
2512
HRESULT hr = CBasePin::CheckConnect(pPin);
2517
// get an input pin and an allocator interface
2518
hr = pPin->QueryInterface(IID_IMemInputPin, (void **) &m_pInputPin);
2526
/* Overriden from CBasePin */
2529
CBaseOutputPin::BreakConnect()
2531
/* Release any allocator we hold */
2534
// Always decommit the allocator because a downstream filter may or
2535
// may not decommit the connection's allocator. A memory leak could
2536
// occur if the allocator is not decommited when a connection is broken.
2537
HRESULT hr = m_pAllocator->Decommit();
2538
if( FAILED( hr ) ) {
2542
m_pAllocator->Release();
2543
m_pAllocator = NULL;
2546
/* Release any input pin interface we hold */
2549
m_pInputPin->Release();
2556
/* This is called when the input pin didn't give us a valid allocator */
2559
CBaseOutputPin::InitAllocator(__deref_out IMemAllocator **ppAlloc)
2561
return CreateMemoryAllocator(ppAlloc);
2565
/* Decide on an allocator, override this if you want to use your own allocator
2566
Override DecideBufferSize to call SetProperties. If the input pin fails
2567
the GetAllocator call then this will construct a CMemAllocator and call
2568
DecideBufferSize on that, and if that fails then we are completely hosed.
2569
If the you succeed the DecideBufferSize call, we will notify the input
2570
pin of the selected allocator. NOTE this is called during Connect() which
2571
therefore looks after grabbing and locking the object's critical section */
2573
// We query the input pin for its requested properties and pass this to
2574
// DecideBufferSize to allow it to fulfill requests that it is happy
2575
// with (eg most people don't care about alignment and are thus happy to
2576
// use the downstream pin's alignment request).
2579
CBaseOutputPin::DecideAllocator(IMemInputPin *pPin, __deref_out IMemAllocator **ppAlloc)
2581
HRESULT hr = NOERROR;
2584
// get downstream prop request
2585
// the derived class may modify this in DecideBufferSize, but
2586
// we assume that he will consistently modify it the same way,
2587
// so we only get it once
2588
ALLOCATOR_PROPERTIES prop;
2589
ZeroMemory(&prop, sizeof(prop));
2591
// whatever he returns, we assume prop is either all zeros
2592
// or he has filled it out.
2593
pPin->GetAllocatorRequirements(&prop);
2595
// if he doesn't care about alignment, then set it to 1
2596
if (prop.cbAlign == 0) {
2600
/* Try the allocator provided by the input pin */
2602
hr = pPin->GetAllocator(ppAlloc);
2603
if (SUCCEEDED(hr)) {
2605
hr = DecideBufferSize(*ppAlloc, &prop);
2606
if (SUCCEEDED(hr)) {
2607
hr = pPin->NotifyAllocator(*ppAlloc, FALSE);
2608
if (SUCCEEDED(hr)) {
2614
/* If the GetAllocator failed we may not have an interface */
2617
(*ppAlloc)->Release();
2621
/* Try the output pin's allocator by the same method */
2623
hr = InitAllocator(ppAlloc);
2624
if (SUCCEEDED(hr)) {
2626
// note - the properties passed here are in the same
2627
// structure as above and may have been modified by
2628
// the previous call to DecideBufferSize
2629
hr = DecideBufferSize(*ppAlloc, &prop);
2630
if (SUCCEEDED(hr)) {
2631
hr = pPin->NotifyAllocator(*ppAlloc, FALSE);
2632
if (SUCCEEDED(hr)) {
2638
/* Likewise we may not have an interface to release */
2641
(*ppAlloc)->Release();
2648
/* This returns an empty sample buffer from the allocator WARNING the same
2649
dangers and restrictions apply here as described below for Deliver() */
2652
CBaseOutputPin::GetDeliveryBuffer(__deref_out IMediaSample ** ppSample,
2653
__in_opt REFERENCE_TIME * pStartTime,
2654
__in_opt REFERENCE_TIME * pEndTime,
2657
if (m_pAllocator != NULL) {
2658
return m_pAllocator->GetBuffer(ppSample,pStartTime,pEndTime,dwFlags);
2660
return E_NOINTERFACE;
2665
/* Deliver a filled-in sample to the connected input pin. NOTE the object must
2666
have locked itself before calling us otherwise we may get halfway through
2667
executing this method only to find the filter graph has got in and
2668
disconnected us from the input pin. If the filter has no worker threads
2669
then the lock is best applied on Receive(), otherwise it should be done
2670
when the worker thread is ready to deliver. There is a wee snag to worker
2671
threads that this shows up. The worker thread must lock the object when
2672
it is ready to deliver a sample, but it may have to wait until a state
2673
change has completed, but that may never complete because the state change
2674
is waiting for the worker thread to complete. The way to handle this is for
2675
the state change code to grab the critical section, then set an abort event
2676
for the worker thread, then release the critical section and wait for the
2677
worker thread to see the event we set and then signal that it has finished
2678
(with another event). At which point the state change code can complete */
2680
// note (if you've still got any breath left after reading that) that you
2681
// need to release the sample yourself after this call. if the connected
2682
// input pin needs to hold onto the sample beyond the call, it will addref
2683
// the sample itself.
2685
// of course you must release this one and call GetDeliveryBuffer for the
2686
// next. You cannot reuse it directly.
2689
CBaseOutputPin::Deliver(IMediaSample * pSample)
2691
if (m_pInputPin == NULL) {
2692
return VFW_E_NOT_CONNECTED;
2696
PERFLOG_DELIVER( m_pName ? m_pName : L"CBaseOutputPin", (IPin *) this, (IPin *) m_pInputPin, pSample, &m_mt );
2699
return m_pInputPin->Receive(pSample);
2703
// called from elsewhere in our filter to pass EOS downstream to
2704
// our connected input pin
2706
CBaseOutputPin::DeliverEndOfStream(void)
2708
// remember this is on IPin not IMemInputPin
2709
if (m_Connected == NULL) {
2710
return VFW_E_NOT_CONNECTED;
2712
return m_Connected->EndOfStream();
2716
/* Commit the allocator's memory, this is called through IMediaFilter
2717
which is responsible for locking the object before calling us */
2720
CBaseOutputPin::Active(void)
2722
if (m_pAllocator == NULL) {
2723
return VFW_E_NO_ALLOCATOR;
2725
return m_pAllocator->Commit();
2729
/* Free up or unprepare allocator's memory, this is called through
2730
IMediaFilter which is responsible for locking the object first */
2733
CBaseOutputPin::Inactive(void)
2735
m_bRunTimeError = FALSE;
2736
if (m_pAllocator == NULL) {
2737
return VFW_E_NO_ALLOCATOR;
2739
return m_pAllocator->Decommit();
2742
// we have a default handling of EndOfStream which is to return
2743
// an error, since this should be called on input pins only
2745
CBaseOutputPin::EndOfStream(void)
2747
return E_UNEXPECTED;
2751
// BeginFlush should be called on input pins only
2753
CBaseOutputPin::BeginFlush(void)
2755
return E_UNEXPECTED;
2758
// EndFlush should be called on input pins only
2760
CBaseOutputPin::EndFlush(void)
2762
return E_UNEXPECTED;
2765
// call BeginFlush on the connected input pin
2767
CBaseOutputPin::DeliverBeginFlush(void)
2769
// remember this is on IPin not IMemInputPin
2770
if (m_Connected == NULL) {
2771
return VFW_E_NOT_CONNECTED;
2773
return m_Connected->BeginFlush();
2776
// call EndFlush on the connected input pin
2778
CBaseOutputPin::DeliverEndFlush(void)
2780
// remember this is on IPin not IMemInputPin
2781
if (m_Connected == NULL) {
2782
return VFW_E_NOT_CONNECTED;
2784
return m_Connected->EndFlush();
2786
// deliver NewSegment to connected pin
2788
CBaseOutputPin::DeliverNewSegment(
2789
REFERENCE_TIME tStart,
2790
REFERENCE_TIME tStop,
2793
if (m_Connected == NULL) {
2794
return VFW_E_NOT_CONNECTED;
2796
return m_Connected->NewSegment(tStart, tStop, dRate);
2800
//=====================================================================
2801
//=====================================================================
2802
// Implements CBaseInputPin
2803
//=====================================================================
2804
//=====================================================================
2807
/* Constructor creates a default allocator object */
2809
CBaseInputPin::CBaseInputPin(__in_opt LPCTSTR pObjectName,
2810
__in CBaseFilter *pFilter,
2811
__in CCritSec *pLock,
2812
__inout HRESULT *phr,
2813
__in_opt LPCWSTR pPinName) :
2814
CBasePin(pObjectName, pFilter, pLock, phr, pPinName, PINDIR_INPUT),
2819
ZeroMemory(&m_SampleProps, sizeof(m_SampleProps));
2823
CBaseInputPin::CBaseInputPin(__in LPCSTR pObjectName,
2824
__in CBaseFilter *pFilter,
2825
__in CCritSec *pLock,
2826
__inout HRESULT *phr,
2827
__in_opt LPCWSTR pPinName) :
2828
CBasePin(pObjectName, pFilter, pLock, phr, pPinName, PINDIR_INPUT),
2833
ZeroMemory(&m_SampleProps, sizeof(m_SampleProps));
2837
/* Destructor releases it's reference count on the default allocator */
2839
CBaseInputPin::~CBaseInputPin()
2841
if (m_pAllocator != NULL) {
2842
m_pAllocator->Release();
2843
m_pAllocator = NULL;
2848
// override this to publicise our interfaces
2850
CBaseInputPin::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
2852
/* Do we know about this interface */
2854
if (riid == IID_IMemInputPin) {
2855
return GetInterface((IMemInputPin *) this, ppv);
2857
return CBasePin::NonDelegatingQueryInterface(riid, ppv);
2862
/* Return the allocator interface that this input pin would like the output
2863
pin to use. NOTE subsequent calls to GetAllocator should all return an
2864
interface onto the SAME object so we create one object at the start
2867
The allocator is Release()'d on disconnect and replaced on
2870
Override this to provide your own allocator.
2874
CBaseInputPin::GetAllocator(
2875
__deref_out IMemAllocator **ppAllocator)
2877
CheckPointer(ppAllocator,E_POINTER);
2878
ValidateReadWritePtr(ppAllocator,sizeof(IMemAllocator *));
2879
CAutoLock cObjectLock(m_pLock);
2881
if (m_pAllocator == NULL) {
2882
HRESULT hr = CreateMemoryAllocator(&m_pAllocator);
2887
ASSERT(m_pAllocator != NULL);
2888
*ppAllocator = m_pAllocator;
2889
m_pAllocator->AddRef();
2894
/* Tell the input pin which allocator the output pin is actually going to use
2895
Override this if you care - NOTE the locking we do both here and also in
2896
GetAllocator is unnecessary but derived classes that do something useful
2897
will undoubtedly have to lock the object so this might help remind people */
2900
CBaseInputPin::NotifyAllocator(
2901
IMemAllocator * pAllocator,
2904
CheckPointer(pAllocator,E_POINTER);
2905
ValidateReadPtr(pAllocator,sizeof(IMemAllocator));
2906
CAutoLock cObjectLock(m_pLock);
2908
IMemAllocator *pOldAllocator = m_pAllocator;
2909
pAllocator->AddRef();
2910
m_pAllocator = pAllocator;
2912
if (pOldAllocator != NULL) {
2913
pOldAllocator->Release();
2916
// the readonly flag indicates whether samples from this allocator should
2917
// be regarded as readonly - if true, then inplace transforms will not be
2919
m_bReadOnly = (BYTE)bReadOnly;
2925
CBaseInputPin::BreakConnect()
2927
/* We don't need our allocator any more */
2929
// Always decommit the allocator because a downstream filter may or
2930
// may not decommit the connection's allocator. A memory leak could
2931
// occur if the allocator is not decommited when a pin is disconnected.
2932
HRESULT hr = m_pAllocator->Decommit();
2933
if( FAILED( hr ) ) {
2937
m_pAllocator->Release();
2938
m_pAllocator = NULL;
2945
/* Do something with this media sample - this base class checks to see if the
2946
format has changed with this media sample and if so checks that the filter
2947
will accept it, generating a run time error if not. Once we have raised a
2948
run time error we set a flag so that no more samples will be accepted
2950
It is important that any filter should override this method and implement
2951
synchronization so that samples are not processed when the pin is
2956
CBaseInputPin::Receive(IMediaSample *pSample)
2958
CheckPointer(pSample,E_POINTER);
2959
ValidateReadPtr(pSample,sizeof(IMediaSample));
2962
HRESULT hr = CheckStreaming();
2968
PERFLOG_RECEIVE( m_pName ? m_pName : L"CBaseInputPin", (IPin *) m_Connected, (IPin *) this, pSample, &m_mt );
2972
/* Check for IMediaSample2 */
2973
IMediaSample2 *pSample2;
2974
if (SUCCEEDED(pSample->QueryInterface(IID_IMediaSample2, (void **)&pSample2))) {
2975
hr = pSample2->GetProperties(sizeof(m_SampleProps), (PBYTE)&m_SampleProps);
2976
pSample2->Release();
2981
/* Get the properties the hard way */
2982
m_SampleProps.cbData = sizeof(m_SampleProps);
2983
m_SampleProps.dwTypeSpecificFlags = 0;
2984
m_SampleProps.dwStreamId = AM_STREAM_MEDIA;
2985
m_SampleProps.dwSampleFlags = 0;
2986
if (S_OK == pSample->IsDiscontinuity()) {
2987
m_SampleProps.dwSampleFlags |= AM_SAMPLE_DATADISCONTINUITY;
2989
if (S_OK == pSample->IsPreroll()) {
2990
m_SampleProps.dwSampleFlags |= AM_SAMPLE_PREROLL;
2992
if (S_OK == pSample->IsSyncPoint()) {
2993
m_SampleProps.dwSampleFlags |= AM_SAMPLE_SPLICEPOINT;
2995
if (SUCCEEDED(pSample->GetTime(&m_SampleProps.tStart,
2996
&m_SampleProps.tStop))) {
2997
m_SampleProps.dwSampleFlags |= AM_SAMPLE_TIMEVALID |
2998
AM_SAMPLE_STOPVALID;
3000
if (S_OK == pSample->GetMediaType(&m_SampleProps.pMediaType)) {
3001
m_SampleProps.dwSampleFlags |= AM_SAMPLE_TYPECHANGED;
3003
pSample->GetPointer(&m_SampleProps.pbBuffer);
3004
m_SampleProps.lActual = pSample->GetActualDataLength();
3005
m_SampleProps.cbBuffer = pSample->GetSize();
3008
/* Has the format changed in this sample */
3010
if (!(m_SampleProps.dwSampleFlags & AM_SAMPLE_TYPECHANGED)) {
3014
/* Check the derived class accepts this format */
3015
/* This shouldn't fail as the source must call QueryAccept first */
3017
hr = CheckMediaType((CMediaType *)m_SampleProps.pMediaType);
3019
if (hr == NOERROR) {
3023
/* Raise a runtime error if we fail the media type */
3025
m_bRunTimeError = TRUE;
3027
m_pFilter->NotifyEvent(EC_ERRORABORT,VFW_E_TYPE_NOT_ACCEPTED,0);
3028
return VFW_E_INVALIDMEDIATYPE;
3032
/* Receive multiple samples */
3034
CBaseInputPin::ReceiveMultiple (
3035
__in_ecount(nSamples) IMediaSample **pSamples,
3037
__out long *nSamplesProcessed)
3039
CheckPointer(pSamples,E_POINTER);
3040
ValidateReadPtr(pSamples,nSamples * sizeof(IMediaSample *));
3043
*nSamplesProcessed = 0;
3044
while (nSamples-- > 0) {
3045
hr = Receive(pSamples[*nSamplesProcessed]);
3047
/* S_FALSE means don't send any more */
3051
(*nSamplesProcessed)++;
3056
/* See if Receive() might block */
3058
CBaseInputPin::ReceiveCanBlock()
3060
/* Ask all the output pins if they block
3061
If there are no output pin assume we do block
3063
int cPins = m_pFilter->GetPinCount();
3064
int cOutputPins = 0;
3065
for (int c = 0; c < cPins; c++) {
3066
CBasePin *pPin = m_pFilter->GetPin(c);
3071
HRESULT hr = pPin->QueryDirection(&pd);
3076
if (pd == PINDIR_OUTPUT) {
3079
hr = pPin->ConnectedTo(&pConnected);
3080
if (SUCCEEDED(hr)) {
3081
ASSERT(pConnected != NULL);
3083
IMemInputPin *pInputPin;
3084
hr = pConnected->QueryInterface(
3086
(void **)&pInputPin);
3087
pConnected->Release();
3088
if (SUCCEEDED(hr)) {
3089
hr = pInputPin->ReceiveCanBlock();
3090
pInputPin->Release();
3091
if (hr != S_FALSE) {
3095
/* There's a transport we don't understand here */
3101
return cOutputPins == 0 ? S_OK : S_FALSE;
3104
// Default handling for BeginFlush - call at the beginning
3105
// of your implementation (makes sure that all Receive calls
3106
// fail). After calling this, you need to free any queued data
3107
// and then call downstream.
3109
CBaseInputPin::BeginFlush(void)
3111
// BeginFlush is NOT synchronized with streaming but is part of
3112
// a control action - hence we synchronize with the filter
3113
CAutoLock lck(m_pLock);
3115
// if we are already in mid-flush, this is probably a mistake
3116
// though not harmful - try to pick it up for now so I can think about it
3117
ASSERT(!m_bFlushing);
3119
// first thing to do is ensure that no further Receive calls succeed
3122
// now discard any data and call downstream - must do that
3123
// in derived classes
3127
// default handling for EndFlush - call at end of your implementation
3128
// - before calling this, ensure that there is no queued data and no thread
3129
// pushing any more without a further receive, then call downstream,
3130
// then call this method to clear the m_bFlushing flag and re-enable
3133
CBaseInputPin::EndFlush(void)
3135
// Endlush is NOT synchronized with streaming but is part of
3136
// a control action - hence we synchronize with the filter
3137
CAutoLock lck(m_pLock);
3139
// almost certainly a mistake if we are not in mid-flush
3140
ASSERT(m_bFlushing);
3142
// before calling, sync with pushing thread and ensure
3143
// no more data is going downstream, then call EndFlush on
3146
// now re-enable Receives
3147
m_bFlushing = FALSE;
3150
m_bRunTimeError = FALSE;
3157
CBaseInputPin::Notify(IBaseFilter * pSender, Quality q)
3159
UNREFERENCED_PARAMETER(q);
3160
CheckPointer(pSender,E_POINTER);
3161
ValidateReadPtr(pSender,sizeof(IBaseFilter));
3162
DbgBreak("IQuality::Notify called on an input pin");
3166
/* Free up or unprepare allocator's memory, this is called through
3167
IMediaFilter which is responsible for locking the object first */
3170
CBaseInputPin::Inactive(void)
3172
m_bRunTimeError = FALSE;
3173
if (m_pAllocator == NULL) {
3174
return VFW_E_NO_ALLOCATOR;
3177
m_bFlushing = FALSE;
3179
return m_pAllocator->Decommit();
3182
// what requirements do we have of the allocator - override if you want
3183
// to support other people's allocators but need a specific alignment
3186
CBaseInputPin::GetAllocatorRequirements(__out ALLOCATOR_PROPERTIES*pProps)
3188
UNREFERENCED_PARAMETER(pProps);
3192
// Check if it's OK to process data
3195
CBaseInputPin::CheckStreaming()
3197
// Shouldn't be able to get any data if we're not connected!
3198
ASSERT(IsConnected());
3200
// Don't process stuff in Stopped state
3202
return VFW_E_WRONG_STATE;
3207
if (m_bRunTimeError) {
3208
return VFW_E_RUNTIME_ERROR;
3213
// Pass on the Quality notification q to
3214
// a. Our QualityControl sink (if we have one) or else
3215
// b. to our upstream filter
3216
// and if that doesn't work, throw it away with a bad return code
3218
CBaseInputPin::PassNotify(Quality& q)
3220
// We pass the message on, which means that we find the quality sink
3221
// for our input pin and send it there
3223
DbgLog((LOG_TRACE,3,TEXT("Passing Quality notification through transform")));
3224
if (m_pQSink!=NULL) {
3225
return m_pQSink->Notify(m_pFilter, q);
3227
// no sink set, so pass it upstream
3229
IQualityControl * pIQC;
3231
hr = VFW_E_NOT_FOUND; // default
3233
m_Connected->QueryInterface(IID_IQualityControl, (void**)&pIQC);
3236
hr = pIQC->Notify(m_pFilter, q);
3245
//=====================================================================
3246
//=====================================================================
3247
// Memory allocation class, implements CMediaSample
3248
//=====================================================================
3249
//=====================================================================
3252
/* NOTE The implementation of this class calls the CUnknown constructor with
3253
a NULL outer unknown pointer. This has the effect of making us a self
3254
contained class, ie any QueryInterface, AddRef or Release calls will be
3255
routed to the class's NonDelegatingUnknown methods. You will typically
3256
find that the classes that do this then override one or more of these
3257
virtual functions to provide more specialised behaviour. A good example
3258
of this is where a class wants to keep the QueryInterface internal but
3259
still wants it's lifetime controlled by the external object */
3261
/* The last two parameters have default values of NULL and zero */
3263
CMediaSample::CMediaSample(__in_opt LPCTSTR pName,
3264
__in_opt CBaseAllocator *pAllocator,
3265
__inout_opt HRESULT *phr,
3266
__in_bcount_opt(length) LPBYTE pBuffer,
3268
m_pBuffer(pBuffer), // Initialise the buffer
3269
m_cbBuffer(length), // And it's length
3270
m_lActual(length), // By default, actual = length
3271
m_pMediaType(NULL), // No media type change
3272
m_dwFlags(0), // Nothing set
3273
m_cRef(0), // 0 ref count
3274
m_dwTypeSpecificFlags(0), // Type specific flags
3275
m_dwStreamId(AM_STREAM_MEDIA), // Stream id
3276
m_pAllocator(pAllocator) // Allocator
3279
PERFLOG_CTOR( pName ? pName : L"CMediaSample", (IMediaSample *) this );
3282
/* We must have an owner and it must also be derived from class
3283
CBaseAllocator BUT we do not hold a reference count on it */
3288
*phr = VFW_E_BUFFER_OVERFLOW;
3294
CMediaSample::CMediaSample(__in_opt LPCSTR pName,
3295
__in_opt CBaseAllocator *pAllocator,
3296
__inout_opt HRESULT *phr,
3297
__in_bcount_opt(length) LPBYTE pBuffer,
3299
m_pBuffer(pBuffer), // Initialise the buffer
3300
m_cbBuffer(length), // And it's length
3301
m_lActual(length), // By default, actual = length
3302
m_pMediaType(NULL), // No media type change
3303
m_dwFlags(0), // Nothing set
3304
m_cRef(0), // 0 ref count
3305
m_dwTypeSpecificFlags(0), // Type specific flags
3306
m_dwStreamId(AM_STREAM_MEDIA), // Stream id
3307
m_pAllocator(pAllocator) // Allocator
3310
PERFLOG_CTOR( L"CMediaSample", (IMediaSample *) this );
3313
/* We must have an owner and it must also be derived from class
3314
CBaseAllocator BUT we do not hold a reference count on it */
3320
/* Destructor deletes the media type memory */
3322
CMediaSample::~CMediaSample()
3325
PERFLOG_DTOR( L"CMediaSample", (IMediaSample *) this );
3329
DeleteMediaType(m_pMediaType);
3333
/* Override this to publicise our interfaces */
3336
CMediaSample::QueryInterface(REFIID riid, __deref_out void **ppv)
3338
if (riid == IID_IMediaSample ||
3339
riid == IID_IMediaSample2 ||
3340
riid == IID_IUnknown) {
3341
return GetInterface((IMediaSample *) this, ppv);
3344
return E_NOINTERFACE;
3348
STDMETHODIMP_(ULONG)
3349
CMediaSample::AddRef()
3351
return InterlockedIncrement(&m_cRef);
3355
// -- CMediaSample lifetimes --
3357
// On final release of this sample buffer it is not deleted but
3358
// returned to the freelist of the owning memory allocator
3360
// The allocator may be waiting for the last buffer to be placed on the free
3361
// list in order to decommit all the memory, so the ReleaseBuffer() call may
3362
// result in this sample being deleted. We also need to hold a refcount on
3363
// the allocator to stop that going away until we have finished with this.
3364
// However, we cannot release the allocator before the ReleaseBuffer, as the
3365
// release may cause us to be deleted. Similarly we can't do it afterwards.
3367
// Thus we must leave it to the allocator to hold an addref on our behalf.
3368
// When he issues us in GetBuffer, he addref's himself. When ReleaseBuffer
3369
// is called, he releases himself, possibly causing us and him to be deleted.
3372
STDMETHODIMP_(ULONG)
3373
CMediaSample::Release()
3375
/* Decrement our own private reference count */
3381
lRef = InterlockedDecrement(&m_cRef);
3385
DbgLog((LOG_MEMORY,3,TEXT(" Unknown %X ref-- = %d"),
3388
/* Did we release our final reference count */
3390
/* Free all resources */
3391
if (m_dwFlags & Sample_TypeChanged) {
3394
ASSERT(m_pMediaType == NULL);
3396
m_dwTypeSpecificFlags = 0;
3397
m_dwStreamId = AM_STREAM_MEDIA;
3399
/* This may cause us to be deleted */
3400
// Our refcount is reliably 0 thus no-one will mess with us
3401
m_pAllocator->ReleaseBuffer(this);
3407
// set the buffer pointer and length. Used by allocators that
3408
// want variable sized pointers or pointers into already-read data.
3409
// This is only available through a CMediaSample* not an IMediaSample*
3410
// and so cannot be changed by clients.
3412
CMediaSample::SetPointer(__in_bcount(cBytes) BYTE * ptr, LONG cBytes)
3415
return VFW_E_BUFFER_OVERFLOW;
3417
m_pBuffer = ptr; // new buffer area (could be null)
3418
m_cbBuffer = cBytes; // length of buffer
3419
m_lActual = cBytes; // length of data in buffer (assume full)
3425
// get me a read/write pointer to this buffer's memory. I will actually
3426
// want to use sizeUsed bytes.
3428
CMediaSample::GetPointer(__deref_out BYTE ** ppBuffer)
3430
ValidateReadWritePtr(ppBuffer,sizeof(BYTE *));
3432
// creator must have set pointer either during
3433
// constructor or by SetPointer
3436
*ppBuffer = m_pBuffer;
3441
// return the size in bytes of this buffer
3443
CMediaSample::GetSize(void)
3449
// get the stream time at which this sample should start and finish.
3451
CMediaSample::GetTime(
3452
__out REFERENCE_TIME * pTimeStart, // put time here
3453
__out REFERENCE_TIME * pTimeEnd
3456
ValidateReadWritePtr(pTimeStart,sizeof(REFERENCE_TIME));
3457
ValidateReadWritePtr(pTimeEnd,sizeof(REFERENCE_TIME));
3459
if (!(m_dwFlags & Sample_StopValid)) {
3460
if (!(m_dwFlags & Sample_TimeValid)) {
3461
return VFW_E_SAMPLE_TIME_NOT_SET;
3463
*pTimeStart = m_Start;
3465
// Make sure old stuff works
3466
*pTimeEnd = m_Start + 1;
3467
return VFW_S_NO_STOP_TIME;
3471
*pTimeStart = m_Start;
3477
// Set the stream time at which this sample should start and finish.
3478
// NULL pointers means the time is reset
3480
CMediaSample::SetTime(
3481
__in_opt REFERENCE_TIME * pTimeStart,
3482
__in_opt REFERENCE_TIME * pTimeEnd
3485
if (pTimeStart == NULL) {
3486
ASSERT(pTimeEnd == NULL);
3487
m_dwFlags &= ~(Sample_TimeValid | Sample_StopValid);
3489
if (pTimeEnd == NULL) {
3490
m_Start = *pTimeStart;
3491
m_dwFlags |= Sample_TimeValid;
3492
m_dwFlags &= ~Sample_StopValid;
3494
ValidateReadPtr(pTimeStart,sizeof(REFERENCE_TIME));
3495
ValidateReadPtr(pTimeEnd,sizeof(REFERENCE_TIME));
3496
ASSERT(*pTimeEnd >= *pTimeStart);
3498
m_Start = *pTimeStart;
3500
m_dwFlags |= Sample_TimeValid | Sample_StopValid;
3507
// get the media times (eg bytes) for this sample
3509
CMediaSample::GetMediaTime(
3510
__out LONGLONG * pTimeStart,
3511
__out LONGLONG * pTimeEnd
3514
ValidateReadWritePtr(pTimeStart,sizeof(LONGLONG));
3515
ValidateReadWritePtr(pTimeEnd,sizeof(LONGLONG));
3517
if (!(m_dwFlags & Sample_MediaTimeValid)) {
3518
return VFW_E_MEDIA_TIME_NOT_SET;
3521
*pTimeStart = m_MediaStart;
3522
*pTimeEnd = (m_MediaStart + m_MediaEnd);
3527
// Set the media times for this sample
3529
CMediaSample::SetMediaTime(
3530
__in_opt LONGLONG * pTimeStart,
3531
__in_opt LONGLONG * pTimeEnd
3534
if (pTimeStart == NULL) {
3535
ASSERT(pTimeEnd == NULL);
3536
m_dwFlags &= ~Sample_MediaTimeValid;
3538
if (NULL == pTimeEnd) {
3541
ValidateReadPtr(pTimeStart,sizeof(LONGLONG));
3542
ValidateReadPtr(pTimeEnd,sizeof(LONGLONG));
3543
ASSERT(*pTimeEnd >= *pTimeStart);
3545
m_MediaStart = *pTimeStart;
3546
m_MediaEnd = (LONG)(*pTimeEnd - *pTimeStart);
3547
m_dwFlags |= Sample_MediaTimeValid;
3554
CMediaSample::IsSyncPoint(void)
3556
if (m_dwFlags & Sample_SyncPoint) {
3565
CMediaSample::SetSyncPoint(BOOL bIsSyncPoint)
3568
m_dwFlags |= Sample_SyncPoint;
3570
m_dwFlags &= ~Sample_SyncPoint;
3575
// returns S_OK if there is a discontinuity in the data (this same is
3576
// not a continuation of the previous stream of data
3577
// - there has been a seek).
3579
CMediaSample::IsDiscontinuity(void)
3581
if (m_dwFlags & Sample_Discontinuity) {
3588
// set the discontinuity property - TRUE if this sample is not a
3589
// continuation, but a new sample after a seek.
3591
CMediaSample::SetDiscontinuity(BOOL bDiscont)
3593
// should be TRUE or FALSE
3595
m_dwFlags |= Sample_Discontinuity;
3597
m_dwFlags &= ~Sample_Discontinuity;
3603
CMediaSample::IsPreroll(void)
3605
if (m_dwFlags & Sample_Preroll) {
3614
CMediaSample::SetPreroll(BOOL bIsPreroll)
3617
m_dwFlags |= Sample_Preroll;
3619
m_dwFlags &= ~Sample_Preroll;
3625
CMediaSample::GetActualDataLength(void)
3632
CMediaSample::SetActualDataLength(LONG lActual)
3634
if (lActual > m_cbBuffer || lActual < 0) {
3635
ASSERT(lActual <= GetSize());
3636
return VFW_E_BUFFER_OVERFLOW;
3638
m_lActual = lActual;
3643
/* These allow for limited format changes in band */
3646
CMediaSample::GetMediaType(__deref_out AM_MEDIA_TYPE **ppMediaType)
3648
ValidateReadWritePtr(ppMediaType,sizeof(AM_MEDIA_TYPE *));
3649
ASSERT(ppMediaType);
3651
/* Do we have a new media type for them */
3653
if (!(m_dwFlags & Sample_TypeChanged)) {
3654
ASSERT(m_pMediaType == NULL);
3655
*ppMediaType = NULL;
3659
ASSERT(m_pMediaType);
3661
/* Create a copy of our media type */
3663
*ppMediaType = CreateMediaType(m_pMediaType);
3664
if (*ppMediaType == NULL) {
3665
return E_OUTOFMEMORY;
3671
/* Mark this sample as having a different format type */
3674
CMediaSample::SetMediaType(__in_opt AM_MEDIA_TYPE *pMediaType)
3676
/* Delete the current media type */
3679
DeleteMediaType(m_pMediaType);
3680
m_pMediaType = NULL;
3683
/* Mechanism for resetting the format type */
3685
if (pMediaType == NULL) {
3686
m_dwFlags &= ~Sample_TypeChanged;
3691
ValidateReadPtr(pMediaType,sizeof(AM_MEDIA_TYPE));
3693
/* Take a copy of the media type */
3695
m_pMediaType = CreateMediaType(pMediaType);
3696
if (m_pMediaType == NULL) {
3697
m_dwFlags &= ~Sample_TypeChanged;
3698
return E_OUTOFMEMORY;
3701
m_dwFlags |= Sample_TypeChanged;
3705
// Set and get properties (IMediaSample2)
3706
STDMETHODIMP CMediaSample::GetProperties(
3708
__out_bcount(cbProperties) BYTE * pbProperties
3711
if (0 != cbProperties) {
3712
CheckPointer(pbProperties, E_POINTER);
3713
// Return generic stuff up to the length
3714
AM_SAMPLE2_PROPERTIES Props;
3715
Props.cbData = min(cbProperties, sizeof(Props));
3716
Props.dwSampleFlags = m_dwFlags & ~Sample_MediaTimeValid;
3717
Props.dwTypeSpecificFlags = m_dwTypeSpecificFlags;
3718
Props.pbBuffer = m_pBuffer;
3719
Props.cbBuffer = m_cbBuffer;
3720
Props.lActual = m_lActual;
3721
Props.tStart = m_Start;
3722
Props.tStop = m_End;
3723
Props.dwStreamId = m_dwStreamId;
3724
if (m_dwFlags & AM_SAMPLE_TYPECHANGED) {
3725
Props.pMediaType = m_pMediaType;
3727
Props.pMediaType = NULL;
3729
CopyMemory(pbProperties, &Props, Props.cbData);
3734
#define CONTAINS_FIELD(type, field, offset) \
3735
((FIELD_OFFSET(type, field) + sizeof(((type *)0)->field)) <= offset)
3737
HRESULT CMediaSample::SetProperties(
3739
__in_bcount(cbProperties) const BYTE * pbProperties
3743
/* Generic properties */
3744
AM_MEDIA_TYPE *pMediaType = NULL;
3746
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, cbData, cbProperties)) {
3747
CheckPointer(pbProperties, E_POINTER);
3748
AM_SAMPLE2_PROPERTIES *pProps =
3749
(AM_SAMPLE2_PROPERTIES *)pbProperties;
3751
/* Don't use more data than is actually there */
3752
if (pProps->cbData < cbProperties) {
3753
cbProperties = pProps->cbData;
3755
/* We only handle IMediaSample2 */
3756
if (cbProperties > sizeof(*pProps) ||
3757
pProps->cbData > sizeof(*pProps)) {
3758
return E_INVALIDARG;
3760
/* Do checks first, the assignments (for backout) */
3761
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwSampleFlags, cbProperties)) {
3762
/* Check the flags */
3763
if (pProps->dwSampleFlags &
3764
(~Sample_ValidFlags | Sample_MediaTimeValid)) {
3765
return E_INVALIDARG;
3767
/* Check a flag isn't being set for a property
3770
if ((pProps->dwSampleFlags & AM_SAMPLE_TIMEVALID) &&
3771
!(m_dwFlags & AM_SAMPLE_TIMEVALID) &&
3772
!CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, tStop, cbProperties)) {
3773
return E_INVALIDARG;
3776
/* NB - can't SET the pointer or size */
3777
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, pbBuffer, cbProperties)) {
3779
/* Check pbBuffer */
3780
if (pProps->pbBuffer != 0 && pProps->pbBuffer != m_pBuffer) {
3781
return E_INVALIDARG;
3784
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, cbBuffer, cbProperties)) {
3786
/* Check cbBuffer */
3787
if (pProps->cbBuffer != 0 && pProps->cbBuffer != m_cbBuffer) {
3788
return E_INVALIDARG;
3791
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, cbBuffer, cbProperties) &&
3792
CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, lActual, cbProperties)) {
3795
if (pProps->cbBuffer < pProps->lActual) {
3796
return E_INVALIDARG;
3800
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, pMediaType, cbProperties)) {
3802
/* Check pMediaType */
3803
if (pProps->dwSampleFlags & AM_SAMPLE_TYPECHANGED) {
3804
CheckPointer(pProps->pMediaType, E_POINTER);
3805
pMediaType = CreateMediaType(pProps->pMediaType);
3806
if (pMediaType == NULL) {
3807
return E_OUTOFMEMORY;
3812
/* Now do the assignments */
3813
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwStreamId, cbProperties)) {
3814
m_dwStreamId = pProps->dwStreamId;
3816
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwSampleFlags, cbProperties)) {
3818
m_dwFlags = pProps->dwSampleFlags |
3819
(m_dwFlags & Sample_MediaTimeValid);
3820
m_dwTypeSpecificFlags = pProps->dwTypeSpecificFlags;
3822
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, dwTypeSpecificFlags, cbProperties)) {
3823
m_dwTypeSpecificFlags = pProps->dwTypeSpecificFlags;
3827
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, lActual, cbProperties)) {
3829
m_lActual = pProps->lActual;
3832
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, tStop, cbProperties)) {
3835
m_End = pProps->tStop;
3837
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, tStart, cbProperties)) {
3840
m_Start = pProps->tStart;
3843
if (CONTAINS_FIELD(AM_SAMPLE2_PROPERTIES, pMediaType, cbProperties)) {
3844
/* Set pMediaType */
3845
if (pProps->dwSampleFlags & AM_SAMPLE_TYPECHANGED) {
3846
if (m_pMediaType != NULL) {
3847
DeleteMediaType(m_pMediaType);
3849
m_pMediaType = pMediaType;
3853
/* Fix up the type changed flag to correctly reflect the current state
3854
If, for instance the input contained no type change but the
3855
output does then if we don't do this we'd lose the
3859
m_dwFlags |= Sample_TypeChanged;
3861
m_dwFlags &= ~Sample_TypeChanged;
3870
// The streaming thread calls IPin::NewSegment(), IPin::EndOfStream(),
3871
// IMemInputPin::Receive() and IMemInputPin::ReceiveMultiple() on the
3872
// connected input pin. The application thread calls Block(). The
3873
// following class members can only be called by the streaming thread.
3876
// DeliverNewSegment()
3877
// StartUsingOutputPin()
3878
// StopUsingOutputPin()
3879
// ChangeOutputFormat()
3880
// ChangeMediaType()
3881
// DynamicReconnect()
3883
// The following class members can only be called by the application thread.
3886
// SynchronousBlockOutputPin()
3887
// AsynchronousBlockOutputPin()
3890
CDynamicOutputPin::CDynamicOutputPin(
3891
__in_opt LPCTSTR pObjectName,
3892
__in CBaseFilter *pFilter,
3893
__in CCritSec *pLock,
3894
__inout HRESULT *phr,
3895
__in_opt LPCWSTR pName) :
3896
CBaseOutputPin(pObjectName, pFilter, pLock, phr, pName),
3898
m_pGraphConfig(NULL),
3899
m_bPinUsesReadOnlyAllocator(FALSE),
3900
m_BlockState(NOT_BLOCKED),
3901
m_hUnblockOutputPinEvent(NULL),
3902
m_hNotifyCallerPinBlockedEvent(NULL),
3903
m_dwBlockCallerThreadID(0),
3904
m_dwNumOutstandingOutputPinUsers(0)
3906
HRESULT hr = Initialize();
3907
if( FAILED( hr ) ) {
3914
CDynamicOutputPin::CDynamicOutputPin(
3915
__in_opt LPCSTR pObjectName,
3916
__in CBaseFilter *pFilter,
3917
__in CCritSec *pLock,
3918
__inout HRESULT *phr,
3919
__in_opt LPCWSTR pName) :
3920
CBaseOutputPin(pObjectName, pFilter, pLock, phr, pName),
3922
m_pGraphConfig(NULL),
3923
m_bPinUsesReadOnlyAllocator(FALSE),
3924
m_BlockState(NOT_BLOCKED),
3925
m_hUnblockOutputPinEvent(NULL),
3926
m_hNotifyCallerPinBlockedEvent(NULL),
3927
m_dwBlockCallerThreadID(0),
3928
m_dwNumOutstandingOutputPinUsers(0)
3930
HRESULT hr = Initialize();
3931
if( FAILED( hr ) ) {
3938
CDynamicOutputPin::~CDynamicOutputPin()
3940
if(NULL != m_hUnblockOutputPinEvent) {
3941
// This call should not fail because we have access to m_hUnblockOutputPinEvent
3942
// and m_hUnblockOutputPinEvent is a valid event.
3943
EXECUTE_ASSERT(::CloseHandle(m_hUnblockOutputPinEvent));
3946
if(NULL != m_hNotifyCallerPinBlockedEvent) {
3947
// This call should not fail because we have access to m_hNotifyCallerPinBlockedEvent
3948
// and m_hNotifyCallerPinBlockedEvent is a valid event.
3949
EXECUTE_ASSERT(::CloseHandle(m_hNotifyCallerPinBlockedEvent));
3953
HRESULT CDynamicOutputPin::Initialize(void)
3955
m_hUnblockOutputPinEvent = ::CreateEvent( NULL, // The event will have the default security descriptor.
3956
TRUE, // This is a manual reset event.
3957
TRUE, // The event is initially signaled.
3958
NULL ); // The event is not named.
3960
// CreateEvent() returns NULL if an error occurs.
3961
if(NULL == m_hUnblockOutputPinEvent) {
3962
return AmGetLastErrorToHResult();
3965
// Set flag to say we can reconnect while streaming.
3966
SetReconnectWhenActive(true);
3971
STDMETHODIMP CDynamicOutputPin::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
3973
if(riid == IID_IPinFlowControl) {
3974
return GetInterface(static_cast<IPinFlowControl*>(this), ppv);
3976
return CBaseOutputPin::NonDelegatingQueryInterface(riid, ppv);
3980
STDMETHODIMP CDynamicOutputPin::Disconnect(void)
3982
CAutoLock cObjectLock(m_pLock);
3983
return DisconnectInternal();
3986
STDMETHODIMP CDynamicOutputPin::Block(DWORD dwBlockFlags, HANDLE hEvent)
3988
const DWORD VALID_FLAGS = AM_PIN_FLOW_CONTROL_BLOCK;
3990
// Check for illegal flags.
3991
if(dwBlockFlags & ~VALID_FLAGS) {
3992
return E_INVALIDARG;
3995
// Make sure the event is unsignaled.
3996
if((dwBlockFlags & AM_PIN_FLOW_CONTROL_BLOCK) && (NULL != hEvent)) {
3997
if( !::ResetEvent( hEvent ) ) {
3998
return AmGetLastErrorToHResult();
4002
// No flags are set if we are unblocking the output pin.
4003
if(0 == dwBlockFlags) {
4005
// This parameter should be NULL because unblock operations are always synchronous.
4006
// There is no need to notify the caller when the event is done.
4007
if(NULL != hEvent) {
4008
return E_INVALIDARG;
4018
if(dwBlockFlags & AM_PIN_FLOW_CONTROL_BLOCK) {
4019
// IPinFlowControl::Block()'s hEvent parameter is NULL if the block is synchronous.
4020
// If hEvent is not NULL, the block is asynchronous.
4021
if(NULL == hEvent) {
4022
hr = SynchronousBlockOutputPin();
4024
hr = AsynchronousBlockOutputPin(hEvent);
4027
hr = UnblockOutputPin();
4041
HRESULT CDynamicOutputPin::SynchronousBlockOutputPin(void)
4043
HANDLE hNotifyCallerPinBlockedEvent = :: CreateEvent( NULL, // The event will have the default security attributes.
4044
FALSE, // This is an automatic reset event.
4045
FALSE, // The event is initially unsignaled.
4046
NULL ); // The event is not named.
4048
// CreateEvent() returns NULL if an error occurs.
4049
if(NULL == hNotifyCallerPinBlockedEvent) {
4050
return AmGetLastErrorToHResult();
4053
HRESULT hr = AsynchronousBlockOutputPin(hNotifyCallerPinBlockedEvent);
4055
// This call should not fail because we have access to hNotifyCallerPinBlockedEvent
4056
// and hNotifyCallerPinBlockedEvent is a valid event.
4057
EXECUTE_ASSERT(::CloseHandle(hNotifyCallerPinBlockedEvent));
4062
hr = WaitEvent(hNotifyCallerPinBlockedEvent);
4064
// This call should not fail because we have access to hNotifyCallerPinBlockedEvent
4065
// and hNotifyCallerPinBlockedEvent is a valid event.
4066
EXECUTE_ASSERT(::CloseHandle(hNotifyCallerPinBlockedEvent));
4075
HRESULT CDynamicOutputPin::AsynchronousBlockOutputPin(HANDLE hNotifyCallerPinBlockedEvent)
4077
// This function holds the m_BlockStateLock because it uses
4078
// m_dwBlockCallerThreadID, m_BlockState and
4079
// m_hNotifyCallerPinBlockedEvent.
4080
CAutoLock alBlockStateLock(&m_BlockStateLock);
4082
if(NOT_BLOCKED != m_BlockState) {
4083
if(m_dwBlockCallerThreadID == ::GetCurrentThreadId()) {
4084
return VFW_E_PIN_ALREADY_BLOCKED_ON_THIS_THREAD;
4086
return VFW_E_PIN_ALREADY_BLOCKED;
4090
BOOL fSuccess = ::DuplicateHandle( ::GetCurrentProcess(),
4091
hNotifyCallerPinBlockedEvent,
4092
::GetCurrentProcess(),
4093
&m_hNotifyCallerPinBlockedEvent,
4098
return AmGetLastErrorToHResult();
4101
m_BlockState = PENDING;
4102
m_dwBlockCallerThreadID = ::GetCurrentThreadId();
4104
// The output pin cannot be blocked if the streaming thread is
4105
// calling IPin::NewSegment(), IPin::EndOfStream(), IMemInputPin::Receive()
4106
// or IMemInputPin::ReceiveMultiple() on the connected input pin. Also, it
4107
// cannot be blocked if the streaming thread is calling DynamicReconnect(),
4108
// ChangeMediaType() or ChangeOutputFormat().
4109
if(!StreamingThreadUsingOutputPin()) {
4111
// The output pin can be immediately blocked.
4118
void CDynamicOutputPin::BlockOutputPin(void)
4120
// The caller should always hold the m_BlockStateLock because this function
4121
// uses m_BlockState and m_hNotifyCallerPinBlockedEvent.
4122
ASSERT(CritCheckIn(&m_BlockStateLock));
4124
// This function should not be called if the streaming thread is modifying
4125
// the connection state or it's passing data downstream.
4126
ASSERT(!StreamingThreadUsingOutputPin());
4128
// This should not fail because we successfully created the event
4129
// and we have the security permissions to change it's state.
4130
EXECUTE_ASSERT(::ResetEvent(m_hUnblockOutputPinEvent));
4132
// This event should not fail because AsynchronousBlockOutputPin() successfully
4133
// duplicated this handle and we have the appropriate security permissions.
4134
EXECUTE_ASSERT(::SetEvent(m_hNotifyCallerPinBlockedEvent));
4135
EXECUTE_ASSERT(::CloseHandle(m_hNotifyCallerPinBlockedEvent));
4137
m_BlockState = BLOCKED;
4138
m_hNotifyCallerPinBlockedEvent = NULL;
4141
HRESULT CDynamicOutputPin::UnblockOutputPin(void)
4143
// UnblockOutputPin() holds the m_BlockStateLock because it
4144
// uses m_BlockState, m_dwBlockCallerThreadID and
4145
// m_hNotifyCallerPinBlockedEvent.
4146
CAutoLock alBlockStateLock(&m_BlockStateLock);
4148
if(NOT_BLOCKED == m_BlockState) {
4152
// This should not fail because we successfully created the event
4153
// and we have the security permissions to change it's state.
4154
EXECUTE_ASSERT(::SetEvent(m_hUnblockOutputPinEvent));
4156
// Cancel the block operation if it's still pending.
4157
if(NULL != m_hNotifyCallerPinBlockedEvent) {
4158
// This event should not fail because AsynchronousBlockOutputPin() successfully
4159
// duplicated this handle and we have the appropriate security permissions.
4160
EXECUTE_ASSERT(::SetEvent(m_hNotifyCallerPinBlockedEvent));
4161
EXECUTE_ASSERT(::CloseHandle(m_hNotifyCallerPinBlockedEvent));
4164
m_BlockState = NOT_BLOCKED;
4165
m_dwBlockCallerThreadID = 0;
4166
m_hNotifyCallerPinBlockedEvent = NULL;
4171
HRESULT CDynamicOutputPin::StartUsingOutputPin(void)
4173
// The caller should not hold m_BlockStateLock. If the caller does,
4174
// a deadlock could occur.
4175
ASSERT(CritCheckOut(&m_BlockStateLock));
4177
CAutoLock alBlockStateLock(&m_BlockStateLock);
4183
// Are we in the middle of a block operation?
4184
while(BLOCKED == m_BlockState) {
4185
m_BlockStateLock.Unlock();
4187
// If this ASSERT fires, a deadlock could occur. The caller should make sure
4188
// that this thread never acquires the Block State lock more than once.
4189
ASSERT(CritCheckOut( &m_BlockStateLock ));
4191
// WaitForMultipleObjects() returns WAIT_OBJECT_0 if the unblock event
4192
// is fired. It returns WAIT_OBJECT_0 + 1 if the stop event if fired.
4193
// See the Windows SDK documentation for more information on
4194
// WaitForMultipleObjects().
4195
const DWORD UNBLOCK = WAIT_OBJECT_0;
4196
const DWORD STOP = WAIT_OBJECT_0 + 1;
4198
HANDLE ahWaitEvents[] = { m_hUnblockOutputPinEvent, m_hStopEvent };
4199
DWORD dwNumWaitEvents = sizeof(ahWaitEvents)/sizeof(HANDLE);
4201
DWORD dwReturnValue = ::WaitForMultipleObjects( dwNumWaitEvents, ahWaitEvents, FALSE, INFINITE );
4203
m_BlockStateLock.Lock();
4209
switch( dwReturnValue ) {
4214
return VFW_E_STATE_CHANGED;
4217
return AmGetLastErrorToHResult();
4220
DbgBreak( "An Unexpected case occured in CDynamicOutputPin::StartUsingOutputPin()." );
4221
return E_UNEXPECTED;
4225
m_dwNumOutstandingOutputPinUsers++;
4234
void CDynamicOutputPin::StopUsingOutputPin(void)
4236
CAutoLock alBlockStateLock(&m_BlockStateLock);
4242
m_dwNumOutstandingOutputPinUsers--;
4244
if((m_dwNumOutstandingOutputPinUsers == 0) && (NOT_BLOCKED != m_BlockState)) {
4253
bool CDynamicOutputPin::StreamingThreadUsingOutputPin(void)
4255
CAutoLock alBlockStateLock(&m_BlockStateLock);
4257
return (m_dwNumOutstandingOutputPinUsers > 0);
4260
void CDynamicOutputPin::SetConfigInfo(IGraphConfig *pGraphConfig, HANDLE hStopEvent)
4262
// This pointer is not addrefed because filters are not allowed to
4263
// hold references to the filter graph manager. See the documentation for
4264
// IBaseFilter::JoinFilterGraph() in the Direct Show SDK for more information.
4265
m_pGraphConfig = pGraphConfig;
4267
m_hStopEvent = hStopEvent;
4270
HRESULT CDynamicOutputPin::Active(void)
4272
// Make sure the user initialized the object by calling SetConfigInfo().
4273
if((NULL == m_hStopEvent) || (NULL == m_pGraphConfig)) {
4274
DbgBreak( ERROR: CDynamicOutputPin::Active() failed because m_pGraphConfig and m_hStopEvent were not initialized. Call SetConfigInfo() to initialize them. );
4278
// If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo().
4279
// The ASSERT can also fire if the event if destroyed and then Active() is called. An event
4280
// handle is invalid if 1) the event does not exist or the user does not have the security
4281
// permissions to use the event.
4282
EXECUTE_ASSERT(ResetEvent(m_hStopEvent));
4284
return CBaseOutputPin::Active();
4287
HRESULT CDynamicOutputPin::Inactive(void)
4289
// If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo().
4290
// The ASSERT can also fire if the event if destroyed and then Active() is called. An event
4291
// handle is invalid if 1) the event does not exist or the user does not have the security
4292
// permissions to use the event.
4293
EXECUTE_ASSERT(SetEvent(m_hStopEvent));
4295
return CBaseOutputPin::Inactive();
4298
HRESULT CDynamicOutputPin::DeliverBeginFlush(void)
4300
// If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo().
4301
// The ASSERT can also fire if the event if destroyed and then DeliverBeginFlush() is called.
4302
// An event handle is invalid if 1) the event does not exist or the user does not have the security
4303
// permissions to use the event.
4304
EXECUTE_ASSERT(SetEvent(m_hStopEvent));
4306
return CBaseOutputPin::DeliverBeginFlush();
4309
HRESULT CDynamicOutputPin::DeliverEndFlush(void)
4311
// If this ASSERT fires, the user may have passed an invalid event handle to SetConfigInfo().
4312
// The ASSERT can also fire if the event if destroyed and then DeliverBeginFlush() is called.
4313
// An event handle is invalid if 1) the event does not exist or the user does not have the security
4314
// permissions to use the event.
4315
EXECUTE_ASSERT(ResetEvent(m_hStopEvent));
4317
return CBaseOutputPin::DeliverEndFlush();
4321
// ChangeOutputFormat() either dynamicly changes the connection's format type or it dynamicly
4322
// reconnects the output pin.
4323
HRESULT CDynamicOutputPin::ChangeOutputFormat
4325
const AM_MEDIA_TYPE *pmt,
4326
REFERENCE_TIME tSegmentStart,
4327
REFERENCE_TIME tSegmentStop,
4331
// The caller should call StartUsingOutputPin() before calling this
4333
ASSERT(StreamingThreadUsingOutputPin());
4335
// Callers should always pass a valid media type to ChangeOutputFormat() .
4336
ASSERT(NULL != pmt);
4338
CMediaType cmt(*pmt);
4339
HRESULT hr = ChangeMediaType(&cmt);
4344
hr = DeliverNewSegment(tSegmentStart, tSegmentStop, dSegmentRate);
4345
if( FAILED( hr ) ) {
4352
HRESULT CDynamicOutputPin::ChangeMediaType(const CMediaType *pmt)
4354
// The caller should call StartUsingOutputPin() before calling this
4356
ASSERT(StreamingThreadUsingOutputPin());
4358
// This function assumes the filter graph is running.
4359
ASSERT(!IsStopped());
4361
if(!IsConnected()) {
4362
return VFW_E_NOT_CONNECTED;
4365
/* First check if the downstream pin will accept a dynamic
4368
QzCComPtr<IPinConnection> pConnection;
4370
m_Connected->QueryInterface(IID_IPinConnection, (void **)&pConnection);
4371
if(pConnection != NULL) {
4373
if(S_OK == pConnection->DynamicQueryAccept(pmt)) {
4375
HRESULT hr = ChangeMediaTypeHelper(pmt);
4384
/* Can't do the dynamic connection */
4385
return DynamicReconnect(pmt);
4388
HRESULT CDynamicOutputPin::ChangeMediaTypeHelper(const CMediaType *pmt)
4390
// The caller should call StartUsingOutputPin() before calling this
4392
ASSERT(StreamingThreadUsingOutputPin());
4394
HRESULT hr = m_Connected->ReceiveConnection(this, pmt);
4399
hr = SetMediaType(pmt);
4404
// Does this pin use the local memory transport?
4405
if(NULL != m_pInputPin) {
4406
// This function assumes that m_pInputPin and m_Connected are
4407
// two different interfaces to the same object.
4408
ASSERT(::IsEqualObject(m_Connected, m_pInputPin));
4410
ALLOCATOR_PROPERTIES apInputPinRequirements;
4411
apInputPinRequirements.cbAlign = 0;
4412
apInputPinRequirements.cbBuffer = 0;
4413
apInputPinRequirements.cbPrefix = 0;
4414
apInputPinRequirements.cBuffers = 0;
4416
m_pInputPin->GetAllocatorRequirements(&apInputPinRequirements);
4418
// A zero allignment does not make any sense.
4419
if(0 == apInputPinRequirements.cbAlign) {
4420
apInputPinRequirements.cbAlign = 1;
4423
hr = m_pAllocator->Decommit();
4428
hr = DecideBufferSize(m_pAllocator, &apInputPinRequirements);
4433
hr = m_pAllocator->Commit();
4438
hr = m_pInputPin->NotifyAllocator(m_pAllocator, m_bPinUsesReadOnlyAllocator);
4447
// this method has to be called from the thread that is pushing data,
4448
// and it's the caller's responsibility to make sure that the thread
4449
// has no outstand samples because they cannot be delivered after a
4452
HRESULT CDynamicOutputPin::DynamicReconnect( const CMediaType* pmt )
4454
// The caller should call StartUsingOutputPin() before calling this
4456
ASSERT(StreamingThreadUsingOutputPin());
4458
if((m_pGraphConfig == NULL) || (NULL == m_hStopEvent)) {
4462
HRESULT hr = m_pGraphConfig->Reconnect(
4468
AM_GRAPH_CONFIG_RECONNECT_CACHE_REMOVED_FILTERS );
4473
HRESULT CDynamicOutputPin::CompleteConnect(IPin *pReceivePin)
4475
HRESULT hr = CBaseOutputPin::CompleteConnect(pReceivePin);
4477
if(!IsStopped() && m_pAllocator) {
4478
hr = m_pAllocator->Commit();
4479
ASSERT(hr != VFW_E_ALREADY_COMMITTED);
4487
void CDynamicOutputPin::AssertValid(void)
4489
// Make sure the object was correctly initialized.
4491
// This ASSERT only fires if the object failed to initialize
4492
// and the user ignored the constructor's return code (phr).
4493
ASSERT(NULL != m_hUnblockOutputPinEvent);
4495
// If either of these ASSERTs fire, the user did not correctly call
4497
ASSERT(NULL != m_hStopEvent);
4498
ASSERT(NULL != m_pGraphConfig);
4500
// Make sure the block state is consistent.
4502
CAutoLock alBlockStateLock(&m_BlockStateLock);
4504
// BLOCK_STATE variables only have three legal values: PENDING, BLOCKED and NOT_BLOCKED.
4505
ASSERT((NOT_BLOCKED == m_BlockState) || (PENDING == m_BlockState) || (BLOCKED == m_BlockState));
4507
// m_hNotifyCallerPinBlockedEvent is only needed when a block operation cannot complete
4509
ASSERT(((NULL == m_hNotifyCallerPinBlockedEvent) && (PENDING != m_BlockState)) ||
4510
((NULL != m_hNotifyCallerPinBlockedEvent) && (PENDING == m_BlockState)) );
4512
// m_dwBlockCallerThreadID should always be 0 if the pin is not blocked and
4513
// the user is not trying to block the pin.
4514
ASSERT((0 == m_dwBlockCallerThreadID) || (NOT_BLOCKED != m_BlockState));
4516
// If this ASSERT fires, the streaming thread is using the output pin and the
4517
// output pin is blocked.
4518
ASSERT(((0 != m_dwNumOutstandingOutputPinUsers) && (BLOCKED != m_BlockState)) ||
4519
((0 == m_dwNumOutstandingOutputPinUsers) && (NOT_BLOCKED != m_BlockState)) ||
4520
((0 == m_dwNumOutstandingOutputPinUsers) && (NOT_BLOCKED == m_BlockState)) );
4524
HRESULT CDynamicOutputPin::WaitEvent(HANDLE hEvent)
4526
const DWORD EVENT_SIGNALED = WAIT_OBJECT_0;
4528
DWORD dwReturnValue = ::WaitForSingleObject(hEvent, INFINITE);
4530
switch( dwReturnValue ) {
4531
case EVENT_SIGNALED:
4535
return AmGetLastErrorToHResult();
4538
DbgBreak( "An Unexpected case occured in CDynamicOutputPin::WaitEvent()." );
4539
return E_UNEXPECTED;
4543
//=====================================================================
4544
//=====================================================================
4545
// Implements CBaseAllocator
4546
//=====================================================================
4547
//=====================================================================
4550
/* Constructor overrides the default settings for the free list to request
4551
that it be alertable (ie the list can be cast to a handle which can be
4552
passed to WaitForSingleObject). Both of the allocator lists also ask for
4553
object locking, the all list matches the object default settings but I
4554
have included them here just so it is obvious what kind of list it is */
4556
CBaseAllocator::CBaseAllocator(__in_opt LPCTSTR pName,
4557
__inout_opt LPUNKNOWN pUnk,
4558
__inout HRESULT *phr,
4560
BOOL fEnableReleaseCallback
4562
CUnknown(pName, pUnk),
4565
m_bCommitted(FALSE),
4566
m_bDecommitInProgress(FALSE),
4573
m_fEnableReleaseCallback(fEnableReleaseCallback),
4577
PERFLOG_CTOR( pName ? pName : L"CBaseAllocator", (IMemAllocator *) this );
4581
m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL);
4582
if (m_hSem == NULL) {
4583
*phr = E_OUTOFMEMORY;
4590
CBaseAllocator::CBaseAllocator(__in_opt LPCSTR pName,
4591
__inout_opt LPUNKNOWN pUnk,
4592
__inout HRESULT *phr,
4594
BOOL fEnableReleaseCallback) :
4595
CUnknown(pName, pUnk),
4598
m_bCommitted(FALSE),
4599
m_bDecommitInProgress(FALSE),
4606
m_fEnableReleaseCallback(fEnableReleaseCallback),
4610
PERFLOG_CTOR( L"CBaseAllocator", (IMemAllocator *) this );
4614
m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL);
4615
if (m_hSem == NULL) {
4616
*phr = E_OUTOFMEMORY;
4625
CBaseAllocator::~CBaseAllocator()
4627
// we can't call Decommit here since that would mean a call to a
4628
// pure virtual in destructor.
4629
// We must assume that the derived class has gone into decommit state in
4632
PERFLOG_DTOR( L"CBaseAllocator", (IMemAllocator *) this );
4635
ASSERT(!m_bCommitted);
4636
if (m_hSem != NULL) {
4637
EXECUTE_ASSERT(CloseHandle(m_hSem));
4640
m_pNotify->Release();
4645
/* Override this to publicise our interfaces */
4648
CBaseAllocator::NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv)
4650
/* Do we know about this interface */
4652
if (riid == IID_IMemAllocator ||
4653
riid == IID_IMemAllocatorCallbackTemp && m_fEnableReleaseCallback) {
4654
return GetInterface((IMemAllocatorCallbackTemp *) this, ppv);
4656
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
4661
/* This sets the size and count of the required samples. The memory isn't
4662
actually allocated until Commit() is called, if memory has already been
4663
allocated then assuming no samples are outstanding the user may call us
4664
to change the buffering, the memory will be released in Commit() */
4667
CBaseAllocator::SetProperties(
4668
__in ALLOCATOR_PROPERTIES* pRequest,
4669
__out ALLOCATOR_PROPERTIES* pActual)
4671
CheckPointer(pRequest, E_POINTER);
4672
CheckPointer(pActual, E_POINTER);
4673
ValidateReadWritePtr(pActual, sizeof(ALLOCATOR_PROPERTIES));
4674
CAutoLock cObjectLock(this);
4676
ZeroMemory(pActual, sizeof(ALLOCATOR_PROPERTIES));
4678
ASSERT(pRequest->cbBuffer > 0);
4680
/* Check the alignment requested */
4681
if (pRequest->cbAlign != 1) {
4682
DbgLog((LOG_ERROR, 2, TEXT("Alignment requested was 0x%x, not 1"),
4683
pRequest->cbAlign));
4684
return VFW_E_BADALIGN;
4687
/* Can't do this if already committed, there is an argument that says we
4688
should not reject the SetProperties call if there are buffers still
4689
active. However this is called by the source filter, which is the same
4690
person who is holding the samples. Therefore it is not unreasonable
4691
for them to free all their samples before changing the requirements */
4694
return VFW_E_ALREADY_COMMITTED;
4697
/* Must be no outstanding buffers */
4699
if (m_lAllocated != m_lFree.GetCount()) {
4700
return VFW_E_BUFFERS_OUTSTANDING;
4703
/* There isn't any real need to check the parameters as they
4704
will just be rejected when the user finally calls Commit */
4706
pActual->cbBuffer = m_lSize = pRequest->cbBuffer;
4707
pActual->cBuffers = m_lCount = pRequest->cBuffers;
4708
pActual->cbAlign = m_lAlignment = pRequest->cbAlign;
4709
pActual->cbPrefix = m_lPrefix = pRequest->cbPrefix;
4716
CBaseAllocator::GetProperties(
4717
__out ALLOCATOR_PROPERTIES * pActual)
4719
CheckPointer(pActual,E_POINTER);
4720
ValidateReadWritePtr(pActual,sizeof(ALLOCATOR_PROPERTIES));
4722
CAutoLock cObjectLock(this);
4723
pActual->cbBuffer = m_lSize;
4724
pActual->cBuffers = m_lCount;
4725
pActual->cbAlign = m_lAlignment;
4726
pActual->cbPrefix = m_lPrefix;
4730
// get container for a sample. Blocking, synchronous call to get the
4731
// next free buffer (as represented by an IMediaSample interface).
4732
// on return, the time etc properties will be invalid, but the buffer
4733
// pointer and size will be correct.
4735
HRESULT CBaseAllocator::GetBuffer(__deref_out IMediaSample **ppBuffer,
4736
__in_opt REFERENCE_TIME *pStartTime,
4737
__in_opt REFERENCE_TIME *pEndTime,
4741
UNREFERENCED_PARAMETER(pStartTime);
4742
UNREFERENCED_PARAMETER(pEndTime);
4743
UNREFERENCED_PARAMETER(dwFlags);
4744
CMediaSample *pSample;
4750
CAutoLock cObjectLock(this);
4752
/* Check we are committed */
4753
if (!m_bCommitted) {
4754
return VFW_E_NOT_COMMITTED;
4756
pSample = (CMediaSample *) m_lFree.RemoveHead();
4757
if (pSample == NULL) {
4762
/* If we didn't get a sample then wait for the list to signal */
4767
if (dwFlags & AM_GBF_NOWAIT) {
4768
return VFW_E_TIMEOUT;
4770
ASSERT(m_hSem != NULL);
4771
WaitForSingleObject(m_hSem, INFINITE);
4774
/* Addref the buffer up to one. On release
4775
back to zero instead of being deleted, it will requeue itself by
4776
calling the ReleaseBuffer member function. NOTE the owner of a
4777
media sample must always be derived from CBaseAllocator */
4780
ASSERT(pSample->m_cRef == 0);
4781
pSample->m_cRef = 1;
4782
*ppBuffer = pSample;
4785
PERFLOG_GETBUFFER( (IMemAllocator *) this, pSample );
4792
/* Final release of a CMediaSample will call this */
4795
CBaseAllocator::ReleaseBuffer(IMediaSample * pSample)
4797
CheckPointer(pSample,E_POINTER);
4798
ValidateReadPtr(pSample,sizeof(IMediaSample));
4801
PERFLOG_RELBUFFER( (IMemAllocator *) this, pSample );
4805
BOOL bRelease = FALSE;
4807
CAutoLock cal(this);
4809
/* Put back on the free list */
4811
m_lFree.Add((CMediaSample *)pSample);
4812
if (m_lWaiting != 0) {
4816
// if there is a pending Decommit, then we need to complete it by
4817
// calling Free() when the last buffer is placed on the free list
4819
LONG l1 = m_lFree.GetCount();
4820
if (m_bDecommitInProgress && (l1 == m_lAllocated)) {
4822
m_bDecommitInProgress = FALSE;
4829
ASSERT(m_fEnableReleaseCallback);
4832
// Note that this is not synchronized with setting up a notification
4835
m_pNotify->NotifyRelease();
4838
/* For each buffer there is one AddRef, made in GetBuffer and released
4839
here. This may cause the allocator and all samples to be deleted */
4848
CBaseAllocator::SetNotify(
4849
IMemAllocatorNotifyCallbackTemp* pNotify
4852
ASSERT(m_fEnableReleaseCallback);
4853
CAutoLock lck(this);
4858
m_pNotify->Release();
4860
m_pNotify = pNotify;
4865
CBaseAllocator::GetFreeCount(
4866
__out LONG* plBuffersFree
4869
ASSERT(m_fEnableReleaseCallback);
4870
CAutoLock cObjectLock(this);
4871
*plBuffersFree = m_lCount - m_lAllocated + m_lFree.GetCount();
4876
CBaseAllocator::NotifySample()
4878
if (m_lWaiting != 0) {
4879
ASSERT(m_hSem != NULL);
4880
ReleaseSemaphore(m_hSem, m_lWaiting, 0);
4886
CBaseAllocator::Commit()
4888
/* Check we are not decommitted */
4889
CAutoLock cObjectLock(this);
4891
// cannot need to alloc or re-alloc if we are committed
4896
/* Allow GetBuffer calls */
4898
m_bCommitted = TRUE;
4900
// is there a pending decommit ? if so, just cancel it
4901
if (m_bDecommitInProgress) {
4902
m_bDecommitInProgress = FALSE;
4904
// don't call Alloc at this point. He cannot allow SetProperties
4905
// between Decommit and the last free, so the buffer size cannot have
4906
// changed. And because some of the buffers are not free yet, he
4907
// cannot re-alloc anyway.
4911
DbgLog((LOG_MEMORY, 1, TEXT("Allocating: %ldx%ld"), m_lCount, m_lSize));
4913
// actually need to allocate the samples
4914
HRESULT hr = Alloc();
4916
m_bCommitted = FALSE;
4925
CBaseAllocator::Decommit()
4927
BOOL bRelease = FALSE;
4929
/* Check we are not already decommitted */
4930
CAutoLock cObjectLock(this);
4931
if (m_bCommitted == FALSE) {
4932
if (m_bDecommitInProgress == FALSE) {
4937
/* No more GetBuffer calls will succeed */
4938
m_bCommitted = FALSE;
4940
// are any buffers outstanding?
4941
if (m_lFree.GetCount() < m_lAllocated) {
4942
// please complete the decommit when last buffer is freed
4943
m_bDecommitInProgress = TRUE;
4945
m_bDecommitInProgress = FALSE;
4947
// need to complete the decommit here as there are no
4948
// outstanding buffers
4954
// Tell anyone waiting that they can go now so we can
4955
// reject their call
4956
#pragma warning(push)
4958
#pragma warning(disable:4068)
4960
#pragma prefast(suppress:__WARNING_DEREF_NULL_PTR, "Suppress warning related to Free() invalidating 'this' which is no applicable to CBaseAllocator::Free()")
4963
#pragma warning(pop)
4973
/* Base definition of allocation which checks we are ok to go ahead and do
4974
the full allocation. We return S_FALSE if the requirements are the same */
4977
CBaseAllocator::Alloc(void)
4979
/* Error if he hasn't set the size yet */
4980
if (m_lCount <= 0 || m_lSize <= 0 || m_lAlignment <= 0) {
4981
return VFW_E_SIZENOTSET;
4984
/* should never get here while buffers outstanding */
4985
ASSERT(m_lFree.GetCount() == m_lAllocated);
4987
/* If the requirements haven't changed then don't reallocate */
4988
if (m_bChanged == FALSE) {
4995
/* Implement CBaseAllocator::CSampleList::Remove(pSample)
4996
Removes pSample from the list
4999
CBaseAllocator::CSampleList::Remove(__inout CMediaSample * pSample)
5001
CMediaSample **pSearch;
5002
for (pSearch = &m_List;
5004
pSearch = &(CBaseAllocator::NextSample(*pSearch))) {
5005
if (*pSearch == pSample) {
5006
*pSearch = CBaseAllocator::NextSample(pSample);
5007
CBaseAllocator::NextSample(pSample) = NULL;
5012
DbgBreak("Couldn't find sample in list");
5015
//=====================================================================
5016
//=====================================================================
5017
// Implements CMemAllocator
5018
//=====================================================================
5019
//=====================================================================
5022
/* This goes in the factory template table to create new instances */
5023
CUnknown *CMemAllocator::CreateInstance(__inout_opt LPUNKNOWN pUnk, __inout HRESULT *phr)
5025
CUnknown *pUnkRet = new CMemAllocator(NAME("CMemAllocator"), pUnk, phr);
5029
CMemAllocator::CMemAllocator(
5030
__in_opt LPCTSTR pName,
5031
__inout_opt LPUNKNOWN pUnk,
5032
__inout HRESULT *phr)
5033
: CBaseAllocator(pName, pUnk, phr, TRUE, TRUE),
5039
CMemAllocator::CMemAllocator(
5040
__in_opt LPCSTR pName,
5041
__inout_opt LPUNKNOWN pUnk,
5042
__inout HRESULT *phr)
5043
: CBaseAllocator(pName, pUnk, phr, TRUE, TRUE),
5049
/* This sets the size and count of the required samples. The memory isn't
5050
actually allocated until Commit() is called, if memory has already been
5051
allocated then assuming no samples are outstanding the user may call us
5052
to change the buffering, the memory will be released in Commit() */
5054
CMemAllocator::SetProperties(
5055
__in ALLOCATOR_PROPERTIES* pRequest,
5056
__out ALLOCATOR_PROPERTIES* pActual)
5058
CheckPointer(pActual,E_POINTER);
5059
ValidateReadWritePtr(pActual,sizeof(ALLOCATOR_PROPERTIES));
5060
CAutoLock cObjectLock(this);
5062
ZeroMemory(pActual, sizeof(ALLOCATOR_PROPERTIES));
5064
ASSERT(pRequest->cbBuffer > 0);
5066
SYSTEM_INFO SysInfo;
5067
GetSystemInfo(&SysInfo);
5069
/* Check the alignment request is a power of 2 */
5070
if ((-pRequest->cbAlign & pRequest->cbAlign) != pRequest->cbAlign) {
5071
DbgLog((LOG_ERROR, 1, TEXT("Alignment requested 0x%x not a power of 2!"),
5072
pRequest->cbAlign));
5074
/* Check the alignment requested */
5075
if (pRequest->cbAlign == 0 ||
5076
(SysInfo.dwAllocationGranularity & (pRequest->cbAlign - 1)) != 0) {
5077
DbgLog((LOG_ERROR, 1, TEXT("Invalid alignment 0x%x requested - granularity = 0x%x"),
5078
pRequest->cbAlign, SysInfo.dwAllocationGranularity));
5079
return VFW_E_BADALIGN;
5082
/* Can't do this if already committed, there is an argument that says we
5083
should not reject the SetProperties call if there are buffers still
5084
active. However this is called by the source filter, which is the same
5085
person who is holding the samples. Therefore it is not unreasonable
5086
for them to free all their samples before changing the requirements */
5088
if (m_bCommitted == TRUE) {
5089
return VFW_E_ALREADY_COMMITTED;
5092
/* Must be no outstanding buffers */
5094
if (m_lFree.GetCount() < m_lAllocated) {
5095
return VFW_E_BUFFERS_OUTSTANDING;
5098
/* There isn't any real need to check the parameters as they
5099
will just be rejected when the user finally calls Commit */
5101
// round length up to alignment - remember that prefix is included in
5103
LONG lSize = pRequest->cbBuffer + pRequest->cbPrefix;
5104
LONG lRemainder = lSize % pRequest->cbAlign;
5105
if (lRemainder != 0) {
5106
lSize = lSize - lRemainder + pRequest->cbAlign;
5108
pActual->cbBuffer = m_lSize = (lSize - pRequest->cbPrefix);
5110
pActual->cBuffers = m_lCount = pRequest->cBuffers;
5111
pActual->cbAlign = m_lAlignment = pRequest->cbAlign;
5112
pActual->cbPrefix = m_lPrefix = pRequest->cbPrefix;
5118
// override this to allocate our resources when Commit is called.
5120
// note that our resources may be already allocated when this is called,
5121
// since we don't free them on Decommit. We will only be called when in
5122
// decommit state with all buffers free.
5124
// object locked by caller
5126
CMemAllocator::Alloc(void)
5128
CAutoLock lck(this);
5130
/* Check he has called SetProperties */
5131
HRESULT hr = CBaseAllocator::Alloc();
5136
/* If the requirements haven't changed then don't reallocate */
5137
if (hr == S_FALSE) {
5141
ASSERT(hr == S_OK); // we use this fact in the loop below
5143
/* Free the old resources */
5148
/* Make sure we've got reasonable values */
5149
if ( m_lSize < 0 || m_lPrefix < 0 || m_lCount < 0 ) {
5150
return E_OUTOFMEMORY;
5153
/* Compute the aligned size */
5154
LONG lAlignedSize = m_lSize + m_lPrefix;
5156
/* Check overflow */
5157
if (lAlignedSize < m_lSize) {
5158
return E_OUTOFMEMORY;
5161
if (m_lAlignment > 1) {
5162
LONG lRemainder = lAlignedSize % m_lAlignment;
5163
if (lRemainder != 0) {
5164
LONG lNewSize = lAlignedSize + m_lAlignment - lRemainder;
5165
if (lNewSize < lAlignedSize) {
5166
return E_OUTOFMEMORY;
5168
lAlignedSize = lNewSize;
5172
/* Create the contiguous memory block for the samples
5173
making sure it's properly aligned (64K should be enough!)
5175
ASSERT(lAlignedSize % m_lAlignment == 0);
5177
LONGLONG lToAllocate = m_lCount * (LONGLONG)lAlignedSize;
5179
/* Check overflow */
5180
if (lToAllocate > MAXLONG) {
5181
return E_OUTOFMEMORY;
5184
m_pBuffer = (PBYTE)VirtualAlloc(NULL,
5189
if (m_pBuffer == NULL) {
5190
return E_OUTOFMEMORY;
5193
LPBYTE pNext = m_pBuffer;
5194
CMediaSample *pSample;
5196
ASSERT(m_lAllocated == 0);
5198
// Create the new samples - we have allocated m_lSize bytes for each sample
5199
// plus m_lPrefix bytes per sample as a prefix. We set the pointer to
5200
// the memory after the prefix - so that GetPointer() will return a pointer
5201
// to m_lSize bytes.
5202
for (; m_lAllocated < m_lCount; m_lAllocated++, pNext += lAlignedSize) {
5205
pSample = new CMediaSample(
5206
NAME("Default memory media sample"),
5209
pNext + m_lPrefix, // GetPointer() value
5210
m_lSize); // not including prefix
5212
ASSERT(SUCCEEDED(hr));
5213
if (pSample == NULL) {
5214
return E_OUTOFMEMORY;
5218
m_lFree.Add(pSample);
5226
// override this to free up any resources we have allocated.
5227
// called from the base class on Decommit when all buffers have been
5228
// returned to the free list.
5230
// caller has already locked the object.
5232
// in our case, we keep the memory until we are deleted, so
5233
// we do nothing here. The memory is deleted in the destructor by
5234
// calling ReallyFree()
5236
CMemAllocator::Free(void)
5242
// called from the destructor (and from Alloc if changing size/count) to
5243
// actually free up the memory
5245
CMemAllocator::ReallyFree(void)
5247
/* Should never be deleting this unless all buffers are freed */
5249
ASSERT(m_lAllocated == m_lFree.GetCount());
5251
/* Free up all the CMediaSamples */
5253
CMediaSample *pSample;
5255
pSample = m_lFree.RemoveHead();
5256
if (pSample != NULL) {
5265
// free the block of buffer memory
5267
EXECUTE_ASSERT(VirtualFree(m_pBuffer, 0, MEM_RELEASE));
5273
/* Destructor frees our memory resources */
5275
CMemAllocator::~CMemAllocator()
5281
// ------------------------------------------------------------------------
5282
// filter registration through IFilterMapper. used if IFilterMapper is
5283
// not found (Quartz 1.0 install)
5286
AMovieSetupRegisterFilter( const AMOVIESETUP_FILTER * const psetupdata
5287
, IFilterMapper * pIFM
5290
DbgLog((LOG_TRACE, 3, TEXT("= AMovieSetupRegisterFilter")));
5292
// check we've got data
5294
if( NULL == psetupdata ) return S_FALSE;
5297
// unregister filter
5298
// (as pins are subkeys of filter's CLSID key
5299
// they do not need to be removed separately).
5301
DbgLog((LOG_TRACE, 3, TEXT("= = unregister filter")));
5302
HRESULT hr = pIFM->UnregisterFilter( *(psetupdata->clsID) );
5309
DbgLog((LOG_TRACE, 3, TEXT("= = register filter")));
5310
hr = pIFM->RegisterFilter( *(psetupdata->clsID)
5311
, psetupdata->strName
5312
, psetupdata->dwMerit );
5317
DbgLog((LOG_TRACE, 3, TEXT("= = register filter pins")));
5318
for( UINT m1=0; m1 < psetupdata->nPins; m1++ )
5320
hr = pIFM->RegisterPin( *(psetupdata->clsID)
5321
, psetupdata->lpPin[m1].strName
5322
, psetupdata->lpPin[m1].bRendered
5323
, psetupdata->lpPin[m1].bOutput
5324
, psetupdata->lpPin[m1].bZero
5325
, psetupdata->lpPin[m1].bMany
5326
, *(psetupdata->lpPin[m1].clsConnectsToFilter)
5327
, psetupdata->lpPin[m1].strConnectsToPin );
5331
// and each pin's media types
5333
DbgLog((LOG_TRACE, 3, TEXT("= = register filter pin types")));
5334
for( UINT m2=0; m2 < psetupdata->lpPin[m1].nMediaTypes; m2++ )
5336
hr = pIFM->RegisterPinType( *(psetupdata->clsID)
5337
, psetupdata->lpPin[m1].strName
5338
, *(psetupdata->lpPin[m1].lpMediaType[m2].clsMajorType)
5339
, *(psetupdata->lpPin[m1].lpMediaType[m2].clsMinorType) );
5340
if( FAILED(hr) ) break;
5342
if( FAILED(hr) ) break;
5344
if( FAILED(hr) ) break;
5349
// handle one acceptable "error" - that
5350
// of filter not being registered!
5351
// (couldn't find a suitable #define'd
5352
// name for the error!)
5354
if( 0x80070002 == hr)
5360
// Remove warnings about unreferenced inline functions
5361
#pragma warning(disable:4514)
5363
#endif /* PJMEDIA_VIDEO_DEV_HAS_DSHOW */