2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27 #include "wine/strmbase.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(strmbase
);
34 static const IPinVtbl InputPin_Vtbl
;
35 static const IPinVtbl OutputPin_Vtbl
;
36 static const IMemInputPinVtbl MemInputPin_Vtbl
;
38 typedef HRESULT (*SendPinFunc
)( IPin
*to
, LPVOID arg
);
40 /** Helper function, there are a lot of places where the error code is inherited
41 * The following rules apply:
43 * Return the first received error code (E_NOTIMPL is ignored)
44 * If no errors occur: return the first received non-error-code that isn't S_OK
46 static HRESULT
updatehres( HRESULT original
, HRESULT
new )
48 if (FAILED( original
) || new == E_NOTIMPL
)
51 if (FAILED( new ) || original
== S_OK
)
57 /** Sends a message from a pin further to other, similar pins
58 * fnMiddle is called on each pin found further on the stream.
59 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
61 * If the pin given is an input pin, the message will be sent downstream to other input pins
62 * If the pin given is an output pin, the message will be sent upstream to other output pins
64 static HRESULT
SendFurther( IPin
*from
, SendPinFunc fnMiddle
, LPVOID arg
, SendPinFunc fnEnd
)
69 HRESULT hr_return
= S_OK
;
70 IEnumPins
*enumpins
= NULL
;
72 PIN_DIRECTION from_dir
;
74 IPin_QueryDirection( from
, &from_dir
);
76 hr
= IPin_QueryInternalConnections( from
, NULL
, &amount
);
77 if (hr
!= E_NOTIMPL
&& amount
)
78 FIXME("Use QueryInternalConnections!\n");
81 pin_info
.pFilter
= NULL
;
82 hr
= IPin_QueryPinInfo( from
, &pin_info
);
86 hr
= IBaseFilter_EnumPins( pin_info
.pFilter
, &enumpins
);
90 hr
= IEnumPins_Reset( enumpins
);
93 hr
= IEnumPins_Next( enumpins
, 1, &pin
, NULL
);
94 if (hr
== VFW_E_ENUM_OUT_OF_SYNC
)
96 hr
= IEnumPins_Reset( enumpins
);
103 IPin_QueryDirection( pin
, &dir
);
106 IPin
*connected
= NULL
;
109 IPin_ConnectedTo( pin
, &connected
);
114 hr_local
= fnMiddle( connected
, arg
);
115 hr_return
= updatehres( hr_return
, hr_local
);
116 IPin_Release(connected
);
133 hr_local
= fnEnd( from
, arg
);
134 hr_return
= updatehres( hr_return
, hr_local
);
136 IEnumPins_Release(enumpins
);
139 if (pin_info
.pFilter
)
140 IBaseFilter_Release( pin_info
.pFilter
);
144 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
146 /* Tempting to just do a memcpy, but the name field is
147 128 characters long! We will probably never exceed 10
148 most of the time, so we are better off copying
149 each field manually */
150 strcpyW(pDest
->achName
, pSrc
->achName
);
151 pDest
->dir
= pSrc
->dir
;
152 pDest
->pFilter
= pSrc
->pFilter
;
155 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE
* pmt
)
159 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt
->majortype
), debugstr_guid(&pmt
->subtype
), debugstr_guid(&pmt
->formattype
));
162 static BOOL
CompareMediaTypes(const AM_MEDIA_TYPE
* pmt1
, const AM_MEDIA_TYPE
* pmt2
, BOOL bWildcards
)
165 dump_AM_MEDIA_TYPE(pmt1
);
167 dump_AM_MEDIA_TYPE(pmt2
);
168 return (((bWildcards
&& (IsEqualGUID(&pmt1
->majortype
, &GUID_NULL
) || IsEqualGUID(&pmt2
->majortype
, &GUID_NULL
))) || IsEqualGUID(&pmt1
->majortype
, &pmt2
->majortype
)) &&
169 ((bWildcards
&& (IsEqualGUID(&pmt1
->subtype
, &GUID_NULL
) || IsEqualGUID(&pmt2
->subtype
, &GUID_NULL
))) || IsEqualGUID(&pmt1
->subtype
, &pmt2
->subtype
)));
172 /*** Common Base Pin function */
173 HRESULT WINAPI
BasePinImpl_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
177 return VFW_S_NO_MORE_ITEMS
;
180 LONG WINAPI
BasePinImpl_GetMediaTypeVersion(BasePin
*iface
)
185 ULONG WINAPI
BasePinImpl_AddRef(IPin
* iface
)
187 BasePin
*This
= (BasePin
*)iface
;
188 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
190 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
195 HRESULT WINAPI
BasePinImpl_Disconnect(IPin
* iface
)
198 BasePin
*This
= (BasePin
*)iface
;
202 EnterCriticalSection(This
->pCritSec
);
204 if (This
->pConnectedTo
)
206 IPin_Release(This
->pConnectedTo
);
207 This
->pConnectedTo
= NULL
;
208 FreeMediaType(&This
->mtCurrent
);
209 ZeroMemory(&This
->mtCurrent
, sizeof(This
->mtCurrent
));
215 LeaveCriticalSection(This
->pCritSec
);
220 HRESULT WINAPI
BasePinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
223 BasePin
*This
= (BasePin
*)iface
;
225 TRACE("(%p)\n", ppPin
);
227 EnterCriticalSection(This
->pCritSec
);
229 if (This
->pConnectedTo
)
231 *ppPin
= This
->pConnectedTo
;
237 hr
= VFW_E_NOT_CONNECTED
;
241 LeaveCriticalSection(This
->pCritSec
);
246 HRESULT WINAPI
BasePinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
249 BasePin
*This
= (BasePin
*)iface
;
251 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
253 EnterCriticalSection(This
->pCritSec
);
255 if (This
->pConnectedTo
)
257 CopyMediaType(pmt
, &This
->mtCurrent
);
262 ZeroMemory(pmt
, sizeof(*pmt
));
263 hr
= VFW_E_NOT_CONNECTED
;
266 LeaveCriticalSection(This
->pCritSec
);
271 HRESULT WINAPI
BasePinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
273 BasePin
*This
= (BasePin
*)iface
;
275 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
277 Copy_PinInfo(pInfo
, &This
->pinInfo
);
278 IBaseFilter_AddRef(pInfo
->pFilter
);
283 HRESULT WINAPI
BasePinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
285 BasePin
*This
= (BasePin
*)iface
;
287 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
289 *pPinDir
= This
->pinInfo
.dir
;
294 HRESULT WINAPI
BasePinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
296 BasePin
*This
= (BasePin
*)iface
;
298 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
300 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
302 return E_OUTOFMEMORY
;
304 strcpyW(*Id
, This
->pinInfo
.achName
);
309 HRESULT WINAPI
BasePinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
311 TRACE("(%p)->(%p)\n", iface
, pmt
);
316 HRESULT WINAPI
BasePinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
318 BasePin
*This
= (BasePin
*)iface
;
320 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
322 /* override this method to allow enumeration of your types */
324 return EnumMediaTypes_Construct(This
, This
->pFuncsTable
->pfnGetMediaType
, This
->pFuncsTable
->pfnGetMediaTypeVersion
, ppEnum
);
327 HRESULT WINAPI
BasePinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
329 BasePin
*This
= (BasePin
*)iface
;
331 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
333 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
336 HRESULT WINAPI
BasePinImpl_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
338 BasePin
*This
= (BasePin
*)iface
;
340 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
342 This
->tStart
= tStart
;
349 /*** OutputPin implementation ***/
351 HRESULT WINAPI
BaseOutputPinImpl_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
353 BaseOutputPin
*This
= (BaseOutputPin
*)iface
;
355 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
359 if (IsEqualIID(riid
, &IID_IUnknown
))
361 else if (IsEqualIID(riid
, &IID_IPin
))
363 else if (IsEqualIID(riid
, &IID_IMediaSeeking
) ||
364 IsEqualIID(riid
, &IID_IQualityControl
))
366 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, riid
, ppv
);
371 IUnknown_AddRef((IUnknown
*)(*ppv
));
375 FIXME("No interface for %s!\n", debugstr_guid(riid
));
377 return E_NOINTERFACE
;
380 ULONG WINAPI
BaseOutputPinImpl_Release(IPin
* iface
)
382 BaseOutputPin
*This
= (BaseOutputPin
*)iface
;
383 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
385 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
389 FreeMediaType(&This
->pin
.mtCurrent
);
396 HRESULT WINAPI
BaseOutputPinImpl_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
399 BaseOutputPin
*This
= (BaseOutputPin
*)iface
;
401 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
402 dump_AM_MEDIA_TYPE(pmt
);
404 /* If we try to connect to ourself, we will definitely deadlock.
405 * There are other cases where we could deadlock too, but this
406 * catches the obvious case */
407 assert(pReceivePin
!= iface
);
409 EnterCriticalSection(This
->pin
.pCritSec
);
411 /* if we have been a specific type to connect with, then we can either connect
412 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
413 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
414 hr
= This
->pin
.pFuncsTable
->pfnAttemptConnection((BasePin
*)This
, pReceivePin
, pmt
);
417 /* negotiate media type */
419 IEnumMediaTypes
* pEnumCandidates
;
420 AM_MEDIA_TYPE
* pmtCandidate
= NULL
; /* Candidate media type */
422 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
424 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
426 /* try this filter's media types first */
427 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
429 assert(pmtCandidate
);
430 dump_AM_MEDIA_TYPE(pmtCandidate
);
431 if (!IsEqualGUID(&FORMAT_None
, &pmtCandidate
->formattype
)
432 && !IsEqualGUID(&GUID_NULL
, &pmtCandidate
->formattype
))
433 assert(pmtCandidate
->pbFormat
);
434 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
435 (This
->pin
.pFuncsTable
->pfnAttemptConnection((BasePin
*)This
, pReceivePin
, pmtCandidate
) == S_OK
))
438 DeleteMediaType(pmtCandidate
);
441 DeleteMediaType(pmtCandidate
);
444 IEnumMediaTypes_Release(pEnumCandidates
);
447 /* then try receiver filter's media types */
448 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
450 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
452 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
454 assert(pmtCandidate
);
455 dump_AM_MEDIA_TYPE(pmtCandidate
);
456 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
457 (This
->pin
.pFuncsTable
->pfnAttemptConnection((BasePin
*)This
, pReceivePin
, pmtCandidate
) == S_OK
))
460 DeleteMediaType(pmtCandidate
);
463 DeleteMediaType(pmtCandidate
);
466 IEnumMediaTypes_Release(pEnumCandidates
);
468 } /* if negotiate media type */
470 LeaveCriticalSection(This
->pin
.pCritSec
);
472 TRACE(" -- %x\n", hr
);
476 HRESULT WINAPI
BaseOutputPinImpl_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
478 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
483 HRESULT WINAPI
BaseOutputPinImpl_Disconnect(IPin
* iface
)
486 BaseOutputPin
*This
= (BaseOutputPin
*)iface
;
490 EnterCriticalSection(This
->pin
.pCritSec
);
492 if (This
->pMemInputPin
)
494 IMemInputPin_Release(This
->pMemInputPin
);
495 This
->pMemInputPin
= NULL
;
497 if (This
->pin
.pConnectedTo
)
499 IPin_Release(This
->pin
.pConnectedTo
);
500 This
->pin
.pConnectedTo
= NULL
;
501 FreeMediaType(&This
->pin
.mtCurrent
);
502 ZeroMemory(&This
->pin
.mtCurrent
, sizeof(This
->pin
.mtCurrent
));
508 LeaveCriticalSection(This
->pin
.pCritSec
);
513 HRESULT WINAPI
BaseOutputPinImpl_EndOfStream(IPin
* iface
)
517 /* not supposed to do anything in an output pin */
522 HRESULT WINAPI
BaseOutputPinImpl_BeginFlush(IPin
* iface
)
524 TRACE("(%p)->()\n", iface
);
526 /* not supposed to do anything in an output pin */
531 HRESULT WINAPI
BaseOutputPinImpl_EndFlush(IPin
* iface
)
533 TRACE("(%p)->()\n", iface
);
535 /* not supposed to do anything in an output pin */
540 static const IPinVtbl OutputPin_Vtbl
=
542 BaseOutputPinImpl_QueryInterface
,
544 BaseOutputPinImpl_Release
,
545 BaseOutputPinImpl_Connect
,
546 BaseOutputPinImpl_ReceiveConnection
,
547 BaseOutputPinImpl_Disconnect
,
548 BasePinImpl_ConnectedTo
,
549 BasePinImpl_ConnectionMediaType
,
550 BasePinImpl_QueryPinInfo
,
551 BasePinImpl_QueryDirection
,
553 BasePinImpl_QueryAccept
,
554 BasePinImpl_EnumMediaTypes
,
555 BasePinImpl_QueryInternalConnections
,
556 BaseOutputPinImpl_EndOfStream
,
557 BaseOutputPinImpl_BeginFlush
,
558 BaseOutputPinImpl_EndFlush
,
559 BasePinImpl_NewSegment
562 HRESULT WINAPI
BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin
*This
, IMediaSample
** ppSample
, REFERENCE_TIME
* tStart
, REFERENCE_TIME
* tStop
, DWORD dwFlags
)
566 TRACE("(%p, %p, %p, %x)\n", ppSample
, tStart
, tStop
, dwFlags
);
568 EnterCriticalSection(This
->pin
.pCritSec
);
570 if (!This
->pin
.pConnectedTo
)
571 hr
= VFW_E_NOT_CONNECTED
;
574 IMemAllocator
* pAlloc
= NULL
;
576 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
579 hr
= IMemAllocator_GetBuffer(pAlloc
, ppSample
, tStart
, tStop
, dwFlags
);
582 hr
= IMediaSample_SetTime(*ppSample
, tStart
, tStop
);
585 IMemAllocator_Release(pAlloc
);
588 LeaveCriticalSection(This
->pin
.pCritSec
);
593 /* replaces OutputPin_SendSample */
594 HRESULT WINAPI
BaseOutputPinImpl_Deliver(BaseOutputPin
*This
, IMediaSample
* pSample
)
597 IMemInputPin
* pMemConnected
= NULL
;
600 EnterCriticalSection(This
->pin
.pCritSec
);
602 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
603 hr
= VFW_E_NOT_CONNECTED
;
606 /* we don't have the lock held when using This->pMemInputPin,
607 * so we need to AddRef it to stop it being deleted while we are
608 * using it. Same with its filter. */
609 pMemConnected
= This
->pMemInputPin
;
610 IMemInputPin_AddRef(pMemConnected
);
611 hr
= IPin_QueryPinInfo(This
->pin
.pConnectedTo
, &pinInfo
);
614 LeaveCriticalSection(This
->pin
.pCritSec
);
618 /* NOTE: if we are in a critical section when Receive is called
619 * then it causes some problems (most notably with the native Video
620 * Renderer) if we are re-entered for whatever reason */
621 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
623 /* If the filter's destroyed, tell upstream to stop sending data */
624 if(IBaseFilter_Release(pinInfo
.pFilter
) == 0 && SUCCEEDED(hr
))
628 IMemInputPin_Release(pMemConnected
);
633 /* replaces OutputPin_CommitAllocator */
634 HRESULT WINAPI
BaseOutputPinImpl_Active(BaseOutputPin
*This
)
638 TRACE("(%p)->()\n", This
);
640 EnterCriticalSection(This
->pin
.pCritSec
);
642 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
643 hr
= VFW_E_NOT_CONNECTED
;
646 IMemAllocator
* pAlloc
= NULL
;
648 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
651 hr
= IMemAllocator_Commit(pAlloc
);
654 IMemAllocator_Release(pAlloc
);
657 LeaveCriticalSection(This
->pin
.pCritSec
);
659 TRACE("--> %08x\n", hr
);
663 /* replaces OutputPin_DecommitAllocator */
664 HRESULT WINAPI
BaseOutputPinImpl_Inactive(BaseOutputPin
*This
)
668 TRACE("(%p)->()\n", This
);
670 EnterCriticalSection(This
->pin
.pCritSec
);
672 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
673 hr
= VFW_E_NOT_CONNECTED
;
676 IMemAllocator
* pAlloc
= NULL
;
678 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
681 hr
= IMemAllocator_Decommit(pAlloc
);
684 IMemAllocator_Release(pAlloc
);
687 LeaveCriticalSection(This
->pin
.pCritSec
);
689 TRACE("--> %08x\n", hr
);
693 /* replaces OutputPin_DeliverDisconnect */
694 HRESULT WINAPI
BaseOutputPinImpl_BreakConnect(BaseOutputPin
*This
)
698 TRACE("(%p)->()\n", This
);
700 EnterCriticalSection(This
->pin
.pCritSec
);
702 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
703 hr
= VFW_E_NOT_CONNECTED
;
706 IMemAllocator
* pAlloc
= NULL
;
708 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
711 hr
= IMemAllocator_Decommit(pAlloc
);
714 IMemAllocator_Release(pAlloc
);
717 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
719 IPin_Disconnect((IPin
*)This
);
721 LeaveCriticalSection(This
->pin
.pCritSec
);
726 HRESULT WINAPI
BaseOutputPinImpl_InitAllocator(BaseOutputPin
*This
, IMemAllocator
**pMemAlloc
)
728 return CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMemAllocator
, (LPVOID
*)pMemAlloc
);
731 HRESULT WINAPI
BaseOutputPinImpl_DecideAllocator(BaseOutputPin
*This
, IMemInputPin
*pPin
, IMemAllocator
**pAlloc
)
735 hr
= IMemInputPin_GetAllocator(pPin
, pAlloc
);
737 if (hr
== VFW_E_NO_ALLOCATOR
)
738 /* Input pin provides no allocator, use standard memory allocator */
739 hr
= BaseOutputPinImpl_InitAllocator(This
, pAlloc
);
743 ALLOCATOR_PROPERTIES rProps
;
744 ZeroMemory(&rProps
, sizeof(ALLOCATOR_PROPERTIES
));
746 IMemInputPin_GetAllocatorRequirements(pPin
, &rProps
);
747 hr
= This
->pFuncsTable
->pfnDecideBufferSize(This
, *pAlloc
, &rProps
);
751 hr
= IMemInputPin_NotifyAllocator(pPin
, *pAlloc
, FALSE
);
756 /*** The Construct functions ***/
758 /* Function called as a helper to IPin_Connect */
759 /* specific AM_MEDIA_TYPE - it cannot be NULL */
760 HRESULT WINAPI
BaseOutputPinImpl_AttemptConnection(BasePin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
762 BaseOutputPin
*This
= (BaseOutputPin
*)iface
;
764 IMemAllocator
* pMemAlloc
= NULL
;
766 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
767 dump_AM_MEDIA_TYPE(pmt
);
769 /* FIXME: call queryacceptproc */
771 This
->pin
.pConnectedTo
= pReceivePin
;
772 IPin_AddRef(pReceivePin
);
773 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
775 hr
= IPin_ReceiveConnection(pReceivePin
, (IPin
*)iface
, pmt
);
777 /* get the IMemInputPin interface we will use to deliver samples to the
781 This
->pMemInputPin
= NULL
;
782 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
786 hr
= This
->pFuncsTable
->pfnDecideAllocator(This
, This
->pMemInputPin
, &pMemAlloc
);
788 IMemAllocator_Release(pMemAlloc
);
791 /* break connection if we couldn't get the allocator */
794 if (This
->pMemInputPin
)
795 IMemInputPin_Release(This
->pMemInputPin
);
796 This
->pMemInputPin
= NULL
;
798 IPin_Disconnect(pReceivePin
);
804 IPin_Release(This
->pin
.pConnectedTo
);
805 This
->pin
.pConnectedTo
= NULL
;
806 FreeMediaType(&This
->pin
.mtCurrent
);
809 TRACE(" -- %x\n", hr
);
813 static HRESULT
OutputPin_Init(const IPinVtbl
*OutputPin_Vtbl
, const PIN_INFO
* pPinInfo
, const BasePinFuncTable
* pBaseFuncsTable
, const BaseOutputPinFuncTable
* pBaseOutputFuncsTable
, LPCRITICAL_SECTION pCritSec
, BaseOutputPin
* pPinImpl
)
817 /* Common attributes */
818 pPinImpl
->pin
.lpVtbl
= OutputPin_Vtbl
;
819 pPinImpl
->pin
.refCount
= 1;
820 pPinImpl
->pin
.pConnectedTo
= NULL
;
821 pPinImpl
->pin
.pCritSec
= pCritSec
;
822 pPinImpl
->pin
.tStart
= 0;
823 pPinImpl
->pin
.tStop
= 0;
824 pPinImpl
->pin
.dRate
= 1.0;
825 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
826 pPinImpl
->pin
.pFuncsTable
= pBaseFuncsTable
;
827 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
829 /* Output pin attributes */
830 pPinImpl
->pMemInputPin
= NULL
;
831 pPinImpl
->pFuncsTable
= pBaseOutputFuncsTable
;
836 HRESULT WINAPI
BaseOutputPin_Construct(const IPinVtbl
*OutputPin_Vtbl
, LONG outputpin_size
, const PIN_INFO
* pPinInfo
, const BasePinFuncTable
* pBaseFuncsTable
, const BaseOutputPinFuncTable
* pBaseOutputFuncsTable
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
838 BaseOutputPin
* pPinImpl
;
842 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
844 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
848 assert(outputpin_size
>= sizeof(BaseOutputPin
));
849 assert(pBaseFuncsTable
->pfnAttemptConnection
);
851 pPinImpl
= CoTaskMemAlloc(outputpin_size
);
854 return E_OUTOFMEMORY
;
856 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl
, pPinInfo
, pBaseFuncsTable
, pBaseOutputFuncsTable
, pCritSec
, pPinImpl
)))
858 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
862 CoTaskMemFree(pPinImpl
);
866 /*** Input Pin implementation ***/
868 HRESULT WINAPI
BaseInputPinImpl_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
870 BaseInputPin
*This
= (BaseInputPin
*)iface
;
872 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
876 if (IsEqualIID(riid
, &IID_IUnknown
))
878 else if (IsEqualIID(riid
, &IID_IPin
))
880 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
881 *ppv
= &This
->lpVtblMemInput
;
882 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
884 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
889 IUnknown_AddRef((IUnknown
*)(*ppv
));
893 FIXME("No interface for %s!\n", debugstr_guid(riid
));
895 return E_NOINTERFACE
;
898 ULONG WINAPI
BaseInputPinImpl_Release(IPin
* iface
)
900 BaseInputPin
*This
= (BaseInputPin
*)iface
;
901 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
903 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
907 FreeMediaType(&This
->pin
.mtCurrent
);
908 if (This
->pAllocator
)
909 IMemAllocator_Release(This
->pAllocator
);
910 This
->pAllocator
= NULL
;
911 This
->pin
.lpVtbl
= NULL
;
919 HRESULT WINAPI
BaseInputPinImpl_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
921 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
927 HRESULT WINAPI
BaseInputPinImpl_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
929 BaseInputPin
*This
= (BaseInputPin
*)iface
;
930 PIN_DIRECTION pindirReceive
;
933 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
934 dump_AM_MEDIA_TYPE(pmt
);
936 EnterCriticalSection(This
->pin
.pCritSec
);
938 if (This
->pin
.pConnectedTo
)
939 hr
= VFW_E_ALREADY_CONNECTED
;
941 if (SUCCEEDED(hr
) && This
->pin
.pFuncsTable
->pfnCheckMediaType((BasePin
*)This
, pmt
) != S_OK
)
942 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
943 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
947 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
949 if (pindirReceive
!= PINDIR_OUTPUT
)
951 ERR("Can't connect from non-output pin\n");
952 hr
= VFW_E_INVALID_DIRECTION
;
958 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
959 This
->pin
.pConnectedTo
= pReceivePin
;
960 IPin_AddRef(pReceivePin
);
963 LeaveCriticalSection(This
->pin
.pCritSec
);
968 static HRESULT
deliver_endofstream(IPin
* pin
, LPVOID unused
)
970 return IPin_EndOfStream( pin
);
973 HRESULT WINAPI
BaseInputPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
975 BaseInputPin
*This
= (BaseInputPin
*)iface
;
977 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
979 return (This
->pin
.pFuncsTable
->pfnCheckMediaType((BasePin
*)This
, pmt
) == S_OK
? S_OK
: S_FALSE
);
982 HRESULT WINAPI
BaseInputPinImpl_EndOfStream(IPin
* iface
)
985 BaseInputPin
*This
= (BaseInputPin
*)iface
;
987 TRACE("(%p)\n", This
);
989 EnterCriticalSection(This
->pin
.pCritSec
);
993 This
->end_of_stream
= 1;
994 LeaveCriticalSection(This
->pin
.pCritSec
);
997 hr
= SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
1001 static HRESULT
deliver_beginflush(IPin
* pin
, LPVOID unused
)
1003 return IPin_BeginFlush( pin
);
1006 HRESULT WINAPI
BaseInputPinImpl_BeginFlush(IPin
* iface
)
1008 BaseInputPin
*This
= (BaseInputPin
*)iface
;
1010 TRACE("() semi-stub\n");
1012 EnterCriticalSection(This
->pin
.pCritSec
);
1015 hr
= SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
1016 LeaveCriticalSection(This
->pin
.pCritSec
);
1021 static HRESULT
deliver_endflush(IPin
* pin
, LPVOID unused
)
1023 return IPin_EndFlush( pin
);
1026 HRESULT WINAPI
BaseInputPinImpl_EndFlush(IPin
* iface
)
1028 BaseInputPin
*This
= (BaseInputPin
*)iface
;
1030 TRACE("(%p)\n", This
);
1032 EnterCriticalSection(This
->pin
.pCritSec
);
1033 This
->flushing
= This
->end_of_stream
= 0;
1035 hr
= SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
1036 LeaveCriticalSection(This
->pin
.pCritSec
);
1041 typedef struct newsegmentargs
1043 REFERENCE_TIME tStart
, tStop
;
1047 static HRESULT
deliver_newsegment(IPin
*pin
, LPVOID data
)
1049 newsegmentargs
*args
= data
;
1050 return IPin_NewSegment(pin
, args
->tStart
, args
->tStop
, args
->rate
);
1053 HRESULT WINAPI
BaseInputPinImpl_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1055 BaseInputPin
*This
= (BaseInputPin
*)iface
;
1056 newsegmentargs args
;
1058 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
1060 args
.tStart
= This
->pin
.tStart
= tStart
;
1061 args
.tStop
= This
->pin
.tStop
= tStop
;
1062 args
.rate
= This
->pin
.dRate
= dRate
;
1064 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
1067 static const IPinVtbl InputPin_Vtbl
=
1069 BaseInputPinImpl_QueryInterface
,
1071 BaseInputPinImpl_Release
,
1072 BaseInputPinImpl_Connect
,
1073 BaseInputPinImpl_ReceiveConnection
,
1074 BasePinImpl_Disconnect
,
1075 BasePinImpl_ConnectedTo
,
1076 BasePinImpl_ConnectionMediaType
,
1077 BasePinImpl_QueryPinInfo
,
1078 BasePinImpl_QueryDirection
,
1079 BasePinImpl_QueryId
,
1080 BaseInputPinImpl_QueryAccept
,
1081 BasePinImpl_EnumMediaTypes
,
1082 BasePinImpl_QueryInternalConnections
,
1083 BaseInputPinImpl_EndOfStream
,
1084 BaseInputPinImpl_BeginFlush
,
1085 BaseInputPinImpl_EndFlush
,
1086 BaseInputPinImpl_NewSegment
1089 /*** IMemInputPin implementation ***/
1091 static inline BaseInputPin
*impl_from_IMemInputPin( IMemInputPin
*iface
)
1093 return (BaseInputPin
*)((char*)iface
- FIELD_OFFSET(BaseInputPin
, lpVtblMemInput
));
1096 static HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1098 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1100 return IPin_QueryInterface((IPin
*)&This
->pin
, riid
, ppv
);
1103 static ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
1105 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1107 return IPin_AddRef((IPin
*)&This
->pin
);
1110 static ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
1112 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1114 return IPin_Release((IPin
*)&This
->pin
);
1117 static HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
1119 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1121 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
1123 *ppAllocator
= This
->pAllocator
;
1125 IMemAllocator_AddRef(*ppAllocator
);
1127 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
1130 static HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
1132 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1134 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
1137 FIXME("Read only flag not handled yet!\n");
1139 /* FIXME: Should we release the allocator on disconnection? */
1142 WARN("Null allocator\n");
1146 if (This
->preferred_allocator
&& pAllocator
!= This
->preferred_allocator
)
1149 if (This
->pAllocator
)
1150 IMemAllocator_Release(This
->pAllocator
);
1151 This
->pAllocator
= pAllocator
;
1152 if (This
->pAllocator
)
1153 IMemAllocator_AddRef(This
->pAllocator
);
1158 static HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
1160 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1162 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
1164 /* override this method if you have any specific requirements */
1169 static HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
1171 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1172 HRESULT hr
= S_FALSE
;
1174 /* this trace commented out for performance reasons */
1175 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1176 if (This
->pFuncsTable
->pfnReceive
)
1177 hr
= This
->pFuncsTable
->pfnReceive(This
, pSample
);
1181 static HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, LONG nSamples
, LONG
*nSamplesProcessed
)
1184 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1186 TRACE("(%p/%p)->(%p, %d, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
1188 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
1190 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
1198 static HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
1200 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1202 TRACE("(%p/%p)->()\n", This
, iface
);
1207 static const IMemInputPinVtbl MemInputPin_Vtbl
=
1209 MemInputPin_QueryInterface
,
1211 MemInputPin_Release
,
1212 MemInputPin_GetAllocator
,
1213 MemInputPin_NotifyAllocator
,
1214 MemInputPin_GetAllocatorRequirements
,
1215 MemInputPin_Receive
,
1216 MemInputPin_ReceiveMultiple
,
1217 MemInputPin_ReceiveCanBlock
1220 static HRESULT
InputPin_Init(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
,
1221 const BasePinFuncTable
* pBaseFuncsTable
, const BaseInputPinFuncTable
* pBaseInputFuncsTable
,
1222 LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, BaseInputPin
* pPinImpl
)
1226 /* Common attributes */
1227 pPinImpl
->pin
.refCount
= 1;
1228 pPinImpl
->pin
.pConnectedTo
= NULL
;
1229 pPinImpl
->pin
.pCritSec
= pCritSec
;
1230 pPinImpl
->pin
.tStart
= 0;
1231 pPinImpl
->pin
.tStop
= 0;
1232 pPinImpl
->pin
.dRate
= 1.0;
1233 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1234 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1235 pPinImpl
->pin
.pFuncsTable
= pBaseFuncsTable
;
1237 /* Input pin attributes */
1238 pPinImpl
->pFuncsTable
= pBaseInputFuncsTable
;
1239 pPinImpl
->pAllocator
= pPinImpl
->preferred_allocator
= allocator
;
1240 if (pPinImpl
->preferred_allocator
)
1241 IMemAllocator_AddRef(pPinImpl
->preferred_allocator
);
1242 pPinImpl
->pin
.lpVtbl
= InputPin_Vtbl
;
1243 pPinImpl
->lpVtblMemInput
= &MemInputPin_Vtbl
;
1244 pPinImpl
->flushing
= pPinImpl
->end_of_stream
= 0;
1249 HRESULT
BaseInputPin_Construct(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
,
1250 const BasePinFuncTable
* pBaseFuncsTable
, const BaseInputPinFuncTable
* pBaseInputFuncsTable
,
1251 LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, IPin
** ppPin
)
1253 BaseInputPin
* pPinImpl
;
1257 assert(pBaseFuncsTable
->pfnCheckMediaType
);
1259 if (pPinInfo
->dir
!= PINDIR_INPUT
)
1261 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1262 return E_INVALIDARG
;
1265 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
1268 return E_OUTOFMEMORY
;
1270 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl
, pPinInfo
, pBaseFuncsTable
, pBaseInputFuncsTable
, pCritSec
, allocator
, pPinImpl
)))
1272 *ppPin
= (IPin
*)pPinImpl
;
1276 CoTaskMemFree(pPinImpl
);