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
22 #include "strmbase_private.h"
24 static const IPinVtbl InputPin_Vtbl
;
25 static const IPinVtbl OutputPin_Vtbl
;
26 static const IMemInputPinVtbl MemInputPin_Vtbl
;
28 typedef HRESULT (*SendPinFunc
)( IPin
*to
, LPVOID arg
);
30 static inline BasePin
*impl_from_IPin( IPin
*iface
)
32 return CONTAINING_RECORD(iface
, BasePin
, IPin_iface
);
35 /** Helper function, there are a lot of places where the error code is inherited
36 * The following rules apply:
38 * Return the first received error code (E_NOTIMPL is ignored)
39 * If no errors occur: return the first received non-error-code that isn't S_OK
41 static HRESULT
updatehres( HRESULT original
, HRESULT
new )
43 if (FAILED( original
) || new == E_NOTIMPL
)
46 if (FAILED( new ) || original
== S_OK
)
52 /** Sends a message from a pin further to other, similar pins
53 * fnMiddle is called on each pin found further on the stream.
54 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
56 * If the pin given is an input pin, the message will be sent downstream to other input pins
57 * If the pin given is an output pin, the message will be sent upstream to other output pins
59 static HRESULT
SendFurther( IPin
*from
, SendPinFunc fnMiddle
, LPVOID arg
, SendPinFunc fnEnd
)
64 HRESULT hr_return
= S_OK
;
65 IEnumPins
*enumpins
= NULL
;
67 PIN_DIRECTION from_dir
;
69 IPin_QueryDirection( from
, &from_dir
);
71 hr
= IPin_QueryInternalConnections( from
, NULL
, &amount
);
72 if (hr
!= E_NOTIMPL
&& amount
)
73 FIXME("Use QueryInternalConnections!\n");
76 pin_info
.pFilter
= NULL
;
77 hr
= IPin_QueryPinInfo( from
, &pin_info
);
81 hr
= IBaseFilter_EnumPins( pin_info
.pFilter
, &enumpins
);
85 hr
= IEnumPins_Reset( enumpins
);
88 hr
= IEnumPins_Next( enumpins
, 1, &pin
, NULL
);
89 if (hr
== VFW_E_ENUM_OUT_OF_SYNC
)
91 hr
= IEnumPins_Reset( enumpins
);
98 IPin_QueryDirection( pin
, &dir
);
101 IPin
*connected
= NULL
;
104 IPin_ConnectedTo( pin
, &connected
);
109 hr_local
= fnMiddle( connected
, arg
);
110 hr_return
= updatehres( hr_return
, hr_local
);
111 IPin_Release(connected
);
128 hr_local
= fnEnd( from
, arg
);
129 hr_return
= updatehres( hr_return
, hr_local
);
131 IEnumPins_Release(enumpins
);
134 if (pin_info
.pFilter
)
135 IBaseFilter_Release( pin_info
.pFilter
);
139 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
141 /* Tempting to just do a memcpy, but the name field is
142 128 characters long! We will probably never exceed 10
143 most of the time, so we are better off copying
144 each field manually */
145 strcpyW(pDest
->achName
, pSrc
->achName
);
146 pDest
->dir
= pSrc
->dir
;
147 pDest
->pFilter
= pSrc
->pFilter
;
150 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE
* pmt
)
154 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt
->majortype
), debugstr_guid(&pmt
->subtype
), debugstr_guid(&pmt
->formattype
));
157 static BOOL
CompareMediaTypes(const AM_MEDIA_TYPE
* pmt1
, const AM_MEDIA_TYPE
* pmt2
, BOOL bWildcards
)
160 dump_AM_MEDIA_TYPE(pmt1
);
162 dump_AM_MEDIA_TYPE(pmt2
);
163 return (((bWildcards
&& (IsEqualGUID(&pmt1
->majortype
, &GUID_NULL
) || IsEqualGUID(&pmt2
->majortype
, &GUID_NULL
))) || IsEqualGUID(&pmt1
->majortype
, &pmt2
->majortype
)) &&
164 ((bWildcards
&& (IsEqualGUID(&pmt1
->subtype
, &GUID_NULL
) || IsEqualGUID(&pmt2
->subtype
, &GUID_NULL
))) || IsEqualGUID(&pmt1
->subtype
, &pmt2
->subtype
)));
167 /*** Common Base Pin function */
168 HRESULT WINAPI
BasePinImpl_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
172 return VFW_S_NO_MORE_ITEMS
;
175 LONG WINAPI
BasePinImpl_GetMediaTypeVersion(BasePin
*iface
)
180 ULONG WINAPI
BasePinImpl_AddRef(IPin
* iface
)
182 BasePin
*This
= impl_from_IPin(iface
);
183 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
185 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
190 HRESULT WINAPI
BasePinImpl_Disconnect(IPin
* iface
)
193 BasePin
*This
= impl_from_IPin(iface
);
197 EnterCriticalSection(This
->pCritSec
);
199 if (This
->pConnectedTo
)
201 IPin_Release(This
->pConnectedTo
);
202 This
->pConnectedTo
= NULL
;
203 FreeMediaType(&This
->mtCurrent
);
204 ZeroMemory(&This
->mtCurrent
, sizeof(This
->mtCurrent
));
210 LeaveCriticalSection(This
->pCritSec
);
215 HRESULT WINAPI
BasePinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
218 BasePin
*This
= impl_from_IPin(iface
);
220 TRACE("(%p)\n", ppPin
);
222 EnterCriticalSection(This
->pCritSec
);
224 if (This
->pConnectedTo
)
226 *ppPin
= This
->pConnectedTo
;
232 hr
= VFW_E_NOT_CONNECTED
;
236 LeaveCriticalSection(This
->pCritSec
);
241 HRESULT WINAPI
BasePinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
244 BasePin
*This
= impl_from_IPin(iface
);
246 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
248 EnterCriticalSection(This
->pCritSec
);
250 if (This
->pConnectedTo
)
252 CopyMediaType(pmt
, &This
->mtCurrent
);
257 ZeroMemory(pmt
, sizeof(*pmt
));
258 hr
= VFW_E_NOT_CONNECTED
;
261 LeaveCriticalSection(This
->pCritSec
);
266 HRESULT WINAPI
BasePinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
268 BasePin
*This
= impl_from_IPin(iface
);
270 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
272 Copy_PinInfo(pInfo
, &This
->pinInfo
);
273 IBaseFilter_AddRef(pInfo
->pFilter
);
278 HRESULT WINAPI
BasePinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
280 BasePin
*This
= impl_from_IPin(iface
);
282 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
284 *pPinDir
= This
->pinInfo
.dir
;
289 HRESULT WINAPI
BasePinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
291 BasePin
*This
= impl_from_IPin(iface
);
293 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
295 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
297 return E_OUTOFMEMORY
;
299 strcpyW(*Id
, This
->pinInfo
.achName
);
304 HRESULT WINAPI
BasePinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
306 TRACE("(%p)->(%p)\n", iface
, pmt
);
311 HRESULT WINAPI
BasePinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
313 BasePin
*This
= impl_from_IPin(iface
);
315 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
317 /* override this method to allow enumeration of your types */
319 return EnumMediaTypes_Construct(This
, This
->pFuncsTable
->pfnGetMediaType
, This
->pFuncsTable
->pfnGetMediaTypeVersion
, ppEnum
);
322 HRESULT WINAPI
BasePinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
324 BasePin
*This
= impl_from_IPin(iface
);
326 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
328 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
331 HRESULT WINAPI
BasePinImpl_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
333 BasePin
*This
= impl_from_IPin(iface
);
335 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
337 This
->tStart
= tStart
;
344 /*** OutputPin implementation ***/
346 static inline BaseOutputPin
*impl_BaseOutputPin_from_IPin( IPin
*iface
)
348 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
.IPin_iface
);
351 static inline BaseOutputPin
*impl_BaseOutputPin_from_BasePin( BasePin
*iface
)
353 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
);
356 HRESULT WINAPI
BaseOutputPinImpl_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
358 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
360 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
364 if (IsEqualIID(riid
, &IID_IUnknown
))
366 else if (IsEqualIID(riid
, &IID_IPin
))
368 else if (IsEqualIID(riid
, &IID_IMediaSeeking
) ||
369 IsEqualIID(riid
, &IID_IQualityControl
))
371 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, riid
, ppv
);
376 IUnknown_AddRef((IUnknown
*)(*ppv
));
380 FIXME("No interface for %s!\n", debugstr_guid(riid
));
382 return E_NOINTERFACE
;
385 ULONG WINAPI
BaseOutputPinImpl_Release(IPin
* iface
)
387 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
388 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
390 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
394 FreeMediaType(&This
->pin
.mtCurrent
);
395 if (This
->pAllocator
)
396 IMemAllocator_Release(This
->pAllocator
);
397 This
->pAllocator
= NULL
;
404 HRESULT WINAPI
BaseOutputPinImpl_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
407 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
409 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
410 dump_AM_MEDIA_TYPE(pmt
);
412 /* If we try to connect to ourselves, we will definitely deadlock.
413 * There are other cases where we could deadlock too, but this
414 * catches the obvious case */
415 assert(pReceivePin
!= iface
);
417 EnterCriticalSection(This
->pin
.pCritSec
);
419 /* if we have been a specific type to connect with, then we can either connect
420 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
421 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
422 hr
= This
->pin
.pFuncsTable
->pfnAttemptConnection(&This
->pin
, pReceivePin
, pmt
);
425 /* negotiate media type */
427 IEnumMediaTypes
* pEnumCandidates
;
428 AM_MEDIA_TYPE
* pmtCandidate
= NULL
; /* Candidate media type */
430 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
432 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
434 /* try this filter's media types first */
435 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
437 assert(pmtCandidate
);
438 dump_AM_MEDIA_TYPE(pmtCandidate
);
439 if (!IsEqualGUID(&FORMAT_None
, &pmtCandidate
->formattype
)
440 && !IsEqualGUID(&GUID_NULL
, &pmtCandidate
->formattype
))
441 assert(pmtCandidate
->pbFormat
);
442 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
443 (This
->pin
.pFuncsTable
->pfnAttemptConnection(&This
->pin
, pReceivePin
, pmtCandidate
) == S_OK
))
446 DeleteMediaType(pmtCandidate
);
449 DeleteMediaType(pmtCandidate
);
452 IEnumMediaTypes_Release(pEnumCandidates
);
455 /* then try receiver filter's media types */
456 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
458 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
460 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
462 assert(pmtCandidate
);
463 dump_AM_MEDIA_TYPE(pmtCandidate
);
464 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
465 (This
->pin
.pFuncsTable
->pfnAttemptConnection(&This
->pin
, pReceivePin
, pmtCandidate
) == S_OK
))
468 DeleteMediaType(pmtCandidate
);
471 DeleteMediaType(pmtCandidate
);
474 IEnumMediaTypes_Release(pEnumCandidates
);
476 } /* if negotiate media type */
478 LeaveCriticalSection(This
->pin
.pCritSec
);
480 TRACE(" -- %x\n", hr
);
484 HRESULT WINAPI
BaseOutputPinImpl_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
486 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
491 HRESULT WINAPI
BaseOutputPinImpl_Disconnect(IPin
* iface
)
494 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
498 EnterCriticalSection(This
->pin
.pCritSec
);
500 if (This
->pMemInputPin
)
502 IMemInputPin_Release(This
->pMemInputPin
);
503 This
->pMemInputPin
= NULL
;
505 if (This
->pin
.pConnectedTo
)
507 IPin_Release(This
->pin
.pConnectedTo
);
508 This
->pin
.pConnectedTo
= NULL
;
509 FreeMediaType(&This
->pin
.mtCurrent
);
510 ZeroMemory(&This
->pin
.mtCurrent
, sizeof(This
->pin
.mtCurrent
));
516 LeaveCriticalSection(This
->pin
.pCritSec
);
521 HRESULT WINAPI
BaseOutputPinImpl_EndOfStream(IPin
* iface
)
525 /* not supposed to do anything in an output pin */
530 HRESULT WINAPI
BaseOutputPinImpl_BeginFlush(IPin
* iface
)
532 TRACE("(%p)->()\n", iface
);
534 /* not supposed to do anything in an output pin */
539 HRESULT WINAPI
BaseOutputPinImpl_EndFlush(IPin
* iface
)
541 TRACE("(%p)->()\n", iface
);
543 /* not supposed to do anything in an output pin */
548 static const IPinVtbl OutputPin_Vtbl
=
550 BaseOutputPinImpl_QueryInterface
,
552 BaseOutputPinImpl_Release
,
553 BaseOutputPinImpl_Connect
,
554 BaseOutputPinImpl_ReceiveConnection
,
555 BaseOutputPinImpl_Disconnect
,
556 BasePinImpl_ConnectedTo
,
557 BasePinImpl_ConnectionMediaType
,
558 BasePinImpl_QueryPinInfo
,
559 BasePinImpl_QueryDirection
,
561 BasePinImpl_QueryAccept
,
562 BasePinImpl_EnumMediaTypes
,
563 BasePinImpl_QueryInternalConnections
,
564 BaseOutputPinImpl_EndOfStream
,
565 BaseOutputPinImpl_BeginFlush
,
566 BaseOutputPinImpl_EndFlush
,
567 BasePinImpl_NewSegment
570 HRESULT WINAPI
BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin
*This
, IMediaSample
** ppSample
, REFERENCE_TIME
* tStart
, REFERENCE_TIME
* tStop
, DWORD dwFlags
)
574 TRACE("(%p, %p, %p, %x)\n", ppSample
, tStart
, tStop
, dwFlags
);
576 if (!This
->pin
.pConnectedTo
)
577 hr
= VFW_E_NOT_CONNECTED
;
580 hr
= IMemAllocator_GetBuffer(This
->pAllocator
, ppSample
, tStart
, tStop
, dwFlags
);
583 hr
= IMediaSample_SetTime(*ppSample
, tStart
, tStop
);
589 /* replaces OutputPin_SendSample */
590 HRESULT WINAPI
BaseOutputPinImpl_Deliver(BaseOutputPin
*This
, IMediaSample
* pSample
)
593 IMemInputPin
* pMemConnected
= NULL
;
596 EnterCriticalSection(This
->pin
.pCritSec
);
598 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
599 hr
= VFW_E_NOT_CONNECTED
;
602 /* we don't have the lock held when using This->pMemInputPin,
603 * so we need to AddRef it to stop it being deleted while we are
604 * using it. Same with its filter. */
605 pMemConnected
= This
->pMemInputPin
;
606 IMemInputPin_AddRef(pMemConnected
);
607 hr
= IPin_QueryPinInfo(This
->pin
.pConnectedTo
, &pinInfo
);
610 LeaveCriticalSection(This
->pin
.pCritSec
);
614 /* NOTE: if we are in a critical section when Receive is called
615 * then it causes some problems (most notably with the native Video
616 * Renderer) if we are re-entered for whatever reason */
617 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
619 /* If the filter's destroyed, tell upstream to stop sending data */
620 if(IBaseFilter_Release(pinInfo
.pFilter
) == 0 && SUCCEEDED(hr
))
624 IMemInputPin_Release(pMemConnected
);
629 /* replaces OutputPin_CommitAllocator */
630 HRESULT WINAPI
BaseOutputPinImpl_Active(BaseOutputPin
*This
)
634 TRACE("(%p)->()\n", This
);
636 EnterCriticalSection(This
->pin
.pCritSec
);
638 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
639 hr
= VFW_E_NOT_CONNECTED
;
641 hr
= IMemAllocator_Commit(This
->pAllocator
);
643 LeaveCriticalSection(This
->pin
.pCritSec
);
645 TRACE("--> %08x\n", hr
);
649 /* replaces OutputPin_DecommitAllocator */
650 HRESULT WINAPI
BaseOutputPinImpl_Inactive(BaseOutputPin
*This
)
654 TRACE("(%p)->()\n", This
);
656 EnterCriticalSection(This
->pin
.pCritSec
);
658 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
659 hr
= VFW_E_NOT_CONNECTED
;
661 hr
= IMemAllocator_Decommit(This
->pAllocator
);
663 LeaveCriticalSection(This
->pin
.pCritSec
);
665 TRACE("--> %08x\n", hr
);
669 /* replaces OutputPin_DeliverDisconnect */
670 HRESULT WINAPI
BaseOutputPinImpl_BreakConnect(BaseOutputPin
*This
)
674 TRACE("(%p)->()\n", This
);
676 EnterCriticalSection(This
->pin
.pCritSec
);
678 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
679 hr
= VFW_E_NOT_CONNECTED
;
682 hr
= IMemAllocator_Decommit(This
->pAllocator
);
685 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
687 IPin_Disconnect(&This
->pin
.IPin_iface
);
689 LeaveCriticalSection(This
->pin
.pCritSec
);
694 HRESULT WINAPI
BaseOutputPinImpl_InitAllocator(BaseOutputPin
*This
, IMemAllocator
**pMemAlloc
)
696 return CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMemAllocator
, (LPVOID
*)pMemAlloc
);
699 HRESULT WINAPI
BaseOutputPinImpl_DecideAllocator(BaseOutputPin
*This
, IMemInputPin
*pPin
, IMemAllocator
**pAlloc
)
703 hr
= IMemInputPin_GetAllocator(pPin
, pAlloc
);
705 if (hr
== VFW_E_NO_ALLOCATOR
)
706 /* Input pin provides no allocator, use standard memory allocator */
707 hr
= BaseOutputPinImpl_InitAllocator(This
, pAlloc
);
711 ALLOCATOR_PROPERTIES rProps
;
712 ZeroMemory(&rProps
, sizeof(ALLOCATOR_PROPERTIES
));
714 IMemInputPin_GetAllocatorRequirements(pPin
, &rProps
);
715 hr
= This
->pFuncsTable
->pfnDecideBufferSize(This
, *pAlloc
, &rProps
);
719 hr
= IMemInputPin_NotifyAllocator(pPin
, *pAlloc
, FALSE
);
724 /*** The Construct functions ***/
726 /* Function called as a helper to IPin_Connect */
727 /* specific AM_MEDIA_TYPE - it cannot be NULL */
728 HRESULT WINAPI
BaseOutputPinImpl_AttemptConnection(BasePin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
730 BaseOutputPin
*This
= impl_BaseOutputPin_from_BasePin(iface
);
732 IMemAllocator
* pMemAlloc
= NULL
;
734 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
735 dump_AM_MEDIA_TYPE(pmt
);
737 /* FIXME: call queryacceptproc */
739 This
->pin
.pConnectedTo
= pReceivePin
;
740 IPin_AddRef(pReceivePin
);
741 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
743 hr
= IPin_ReceiveConnection(pReceivePin
, &iface
->IPin_iface
, pmt
);
745 /* get the IMemInputPin interface we will use to deliver samples to the
749 This
->pMemInputPin
= NULL
;
750 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
754 hr
= This
->pFuncsTable
->pfnDecideAllocator(This
, This
->pMemInputPin
, &pMemAlloc
);
756 This
->pAllocator
= pMemAlloc
;
758 IMemAllocator_Release(pMemAlloc
);
761 /* break connection if we couldn't get the allocator */
764 if (This
->pMemInputPin
)
765 IMemInputPin_Release(This
->pMemInputPin
);
766 This
->pMemInputPin
= NULL
;
768 IPin_Disconnect(pReceivePin
);
774 IPin_Release(This
->pin
.pConnectedTo
);
775 This
->pin
.pConnectedTo
= NULL
;
776 FreeMediaType(&This
->pin
.mtCurrent
);
779 TRACE(" -- %x\n", hr
);
783 static HRESULT
OutputPin_Init(const IPinVtbl
*OutputPin_Vtbl
, const PIN_INFO
* pPinInfo
, const BasePinFuncTable
* pBaseFuncsTable
, const BaseOutputPinFuncTable
* pBaseOutputFuncsTable
, LPCRITICAL_SECTION pCritSec
, BaseOutputPin
* pPinImpl
)
787 /* Common attributes */
788 pPinImpl
->pin
.IPin_iface
.lpVtbl
= OutputPin_Vtbl
;
789 pPinImpl
->pin
.refCount
= 1;
790 pPinImpl
->pin
.pConnectedTo
= NULL
;
791 pPinImpl
->pin
.pCritSec
= pCritSec
;
792 pPinImpl
->pin
.tStart
= 0;
793 pPinImpl
->pin
.tStop
= 0;
794 pPinImpl
->pin
.dRate
= 1.0;
795 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
796 pPinImpl
->pin
.pFuncsTable
= pBaseFuncsTable
;
797 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
799 /* Output pin attributes */
800 pPinImpl
->pMemInputPin
= NULL
;
801 pPinImpl
->pAllocator
= NULL
;
802 pPinImpl
->pFuncsTable
= pBaseOutputFuncsTable
;
807 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
)
809 BaseOutputPin
* pPinImpl
;
813 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
815 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
819 assert(outputpin_size
>= sizeof(BaseOutputPin
));
820 assert(pBaseFuncsTable
->pfnAttemptConnection
);
822 pPinImpl
= CoTaskMemAlloc(outputpin_size
);
825 return E_OUTOFMEMORY
;
827 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl
, pPinInfo
, pBaseFuncsTable
, pBaseOutputFuncsTable
, pCritSec
, pPinImpl
)))
829 *ppPin
= &pPinImpl
->pin
.IPin_iface
;
833 CoTaskMemFree(pPinImpl
);
837 /*** Input Pin implementation ***/
839 static inline BaseInputPin
*impl_BaseInputPin_from_IPin( IPin
*iface
)
841 return CONTAINING_RECORD(iface
, BaseInputPin
, pin
.IPin_iface
);
844 static inline BaseInputPin
*impl_BaseInputPin_from_BasePin( BasePin
*iface
)
846 return CONTAINING_RECORD(iface
, BaseInputPin
, pin
);
849 HRESULT WINAPI
BaseInputPinImpl_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
851 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
853 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
857 if (IsEqualIID(riid
, &IID_IUnknown
))
859 else if (IsEqualIID(riid
, &IID_IPin
))
861 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
862 *ppv
= &This
->IMemInputPin_iface
;
863 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
865 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
870 IUnknown_AddRef((IUnknown
*)(*ppv
));
874 FIXME("No interface for %s!\n", debugstr_guid(riid
));
876 return E_NOINTERFACE
;
879 ULONG WINAPI
BaseInputPinImpl_Release(IPin
* iface
)
881 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
882 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
884 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
888 FreeMediaType(&This
->pin
.mtCurrent
);
889 if (This
->pAllocator
)
890 IMemAllocator_Release(This
->pAllocator
);
891 This
->pAllocator
= NULL
;
892 This
->pin
.IPin_iface
.lpVtbl
= NULL
;
900 HRESULT WINAPI
BaseInputPinImpl_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
902 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
908 HRESULT WINAPI
BaseInputPinImpl_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
910 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
911 PIN_DIRECTION pindirReceive
;
914 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
915 dump_AM_MEDIA_TYPE(pmt
);
917 EnterCriticalSection(This
->pin
.pCritSec
);
919 if (This
->pin
.pConnectedTo
)
920 hr
= VFW_E_ALREADY_CONNECTED
;
922 if (SUCCEEDED(hr
) && This
->pin
.pFuncsTable
->pfnCheckMediaType(&This
->pin
, pmt
) != S_OK
)
923 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
924 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
928 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
930 if (pindirReceive
!= PINDIR_OUTPUT
)
932 ERR("Can't connect from non-output pin\n");
933 hr
= VFW_E_INVALID_DIRECTION
;
939 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
940 This
->pin
.pConnectedTo
= pReceivePin
;
941 IPin_AddRef(pReceivePin
);
944 LeaveCriticalSection(This
->pin
.pCritSec
);
949 static HRESULT
deliver_endofstream(IPin
* pin
, LPVOID unused
)
951 return IPin_EndOfStream( pin
);
954 HRESULT WINAPI
BaseInputPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
956 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
958 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
960 return (This
->pin
.pFuncsTable
->pfnCheckMediaType(&This
->pin
, pmt
) == S_OK
? S_OK
: S_FALSE
);
963 HRESULT WINAPI
BaseInputPinImpl_EndOfStream(IPin
* iface
)
966 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
968 TRACE("(%p)\n", This
);
970 EnterCriticalSection(This
->pin
.pCritSec
);
974 This
->end_of_stream
= 1;
975 LeaveCriticalSection(This
->pin
.pCritSec
);
978 hr
= SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
982 static HRESULT
deliver_beginflush(IPin
* pin
, LPVOID unused
)
984 return IPin_BeginFlush( pin
);
987 HRESULT WINAPI
BaseInputPinImpl_BeginFlush(IPin
* iface
)
989 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
991 TRACE("() semi-stub\n");
993 EnterCriticalSection(This
->pin
.pCritSec
);
996 hr
= SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
997 LeaveCriticalSection(This
->pin
.pCritSec
);
1002 static HRESULT
deliver_endflush(IPin
* pin
, LPVOID unused
)
1004 return IPin_EndFlush( pin
);
1007 HRESULT WINAPI
BaseInputPinImpl_EndFlush(IPin
* iface
)
1009 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
1011 TRACE("(%p)\n", This
);
1013 EnterCriticalSection(This
->pin
.pCritSec
);
1014 This
->flushing
= This
->end_of_stream
= 0;
1016 hr
= SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
1017 LeaveCriticalSection(This
->pin
.pCritSec
);
1022 typedef struct newsegmentargs
1024 REFERENCE_TIME tStart
, tStop
;
1028 static HRESULT
deliver_newsegment(IPin
*pin
, LPVOID data
)
1030 newsegmentargs
*args
= data
;
1031 return IPin_NewSegment(pin
, args
->tStart
, args
->tStop
, args
->rate
);
1034 HRESULT WINAPI
BaseInputPinImpl_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1036 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
1037 newsegmentargs args
;
1039 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
1041 args
.tStart
= This
->pin
.tStart
= tStart
;
1042 args
.tStop
= This
->pin
.tStop
= tStop
;
1043 args
.rate
= This
->pin
.dRate
= dRate
;
1045 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
1048 static const IPinVtbl InputPin_Vtbl
=
1050 BaseInputPinImpl_QueryInterface
,
1052 BaseInputPinImpl_Release
,
1053 BaseInputPinImpl_Connect
,
1054 BaseInputPinImpl_ReceiveConnection
,
1055 BasePinImpl_Disconnect
,
1056 BasePinImpl_ConnectedTo
,
1057 BasePinImpl_ConnectionMediaType
,
1058 BasePinImpl_QueryPinInfo
,
1059 BasePinImpl_QueryDirection
,
1060 BasePinImpl_QueryId
,
1061 BaseInputPinImpl_QueryAccept
,
1062 BasePinImpl_EnumMediaTypes
,
1063 BasePinImpl_QueryInternalConnections
,
1064 BaseInputPinImpl_EndOfStream
,
1065 BaseInputPinImpl_BeginFlush
,
1066 BaseInputPinImpl_EndFlush
,
1067 BaseInputPinImpl_NewSegment
1070 /*** IMemInputPin implementation ***/
1072 static inline BaseInputPin
*impl_from_IMemInputPin( IMemInputPin
*iface
)
1074 return CONTAINING_RECORD(iface
, BaseInputPin
, IMemInputPin_iface
);
1077 static HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1079 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1081 return IPin_QueryInterface(&This
->pin
.IPin_iface
, riid
, ppv
);
1084 static ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
1086 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1088 return IPin_AddRef(&This
->pin
.IPin_iface
);
1091 static ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
1093 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1095 return IPin_Release(&This
->pin
.IPin_iface
);
1098 static HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
1100 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1102 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
1104 *ppAllocator
= This
->pAllocator
;
1106 IMemAllocator_AddRef(*ppAllocator
);
1108 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
1111 static HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
1113 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1115 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
1118 FIXME("Read only flag not handled yet!\n");
1120 /* FIXME: Should we release the allocator on disconnection? */
1123 WARN("Null allocator\n");
1127 if (This
->preferred_allocator
&& pAllocator
!= This
->preferred_allocator
)
1130 if (This
->pAllocator
)
1131 IMemAllocator_Release(This
->pAllocator
);
1132 This
->pAllocator
= pAllocator
;
1133 if (This
->pAllocator
)
1134 IMemAllocator_AddRef(This
->pAllocator
);
1139 static HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
1141 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1143 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
1145 /* override this method if you have any specific requirements */
1150 static HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
1152 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1153 HRESULT hr
= S_FALSE
;
1155 /* this trace commented out for performance reasons */
1156 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1157 if (This
->pFuncsTable
->pfnReceive
)
1158 hr
= This
->pFuncsTable
->pfnReceive(This
, pSample
);
1162 static HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, LONG nSamples
, LONG
*nSamplesProcessed
)
1165 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1167 TRACE("(%p/%p)->(%p, %d, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
1169 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
1171 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
1179 static HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
1181 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1183 TRACE("(%p/%p)->()\n", This
, iface
);
1188 static const IMemInputPinVtbl MemInputPin_Vtbl
=
1190 MemInputPin_QueryInterface
,
1192 MemInputPin_Release
,
1193 MemInputPin_GetAllocator
,
1194 MemInputPin_NotifyAllocator
,
1195 MemInputPin_GetAllocatorRequirements
,
1196 MemInputPin_Receive
,
1197 MemInputPin_ReceiveMultiple
,
1198 MemInputPin_ReceiveCanBlock
1201 static HRESULT
InputPin_Init(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
,
1202 const BasePinFuncTable
* pBaseFuncsTable
, const BaseInputPinFuncTable
* pBaseInputFuncsTable
,
1203 LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, BaseInputPin
* pPinImpl
)
1207 /* Common attributes */
1208 pPinImpl
->pin
.refCount
= 1;
1209 pPinImpl
->pin
.pConnectedTo
= NULL
;
1210 pPinImpl
->pin
.pCritSec
= pCritSec
;
1211 pPinImpl
->pin
.tStart
= 0;
1212 pPinImpl
->pin
.tStop
= 0;
1213 pPinImpl
->pin
.dRate
= 1.0;
1214 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1215 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1216 pPinImpl
->pin
.pFuncsTable
= pBaseFuncsTable
;
1218 /* Input pin attributes */
1219 pPinImpl
->pFuncsTable
= pBaseInputFuncsTable
;
1220 pPinImpl
->pAllocator
= pPinImpl
->preferred_allocator
= allocator
;
1221 if (pPinImpl
->preferred_allocator
)
1222 IMemAllocator_AddRef(pPinImpl
->preferred_allocator
);
1223 pPinImpl
->pin
.IPin_iface
.lpVtbl
= InputPin_Vtbl
;
1224 pPinImpl
->IMemInputPin_iface
.lpVtbl
= &MemInputPin_Vtbl
;
1225 pPinImpl
->flushing
= pPinImpl
->end_of_stream
= 0;
1230 HRESULT
BaseInputPin_Construct(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
,
1231 const BasePinFuncTable
* pBaseFuncsTable
, const BaseInputPinFuncTable
* pBaseInputFuncsTable
,
1232 LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, IPin
** ppPin
)
1234 BaseInputPin
* pPinImpl
;
1238 assert(pBaseFuncsTable
->pfnCheckMediaType
);
1240 if (pPinInfo
->dir
!= PINDIR_INPUT
)
1242 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1243 return E_INVALIDARG
;
1246 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
1249 return E_OUTOFMEMORY
;
1251 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl
, pPinInfo
, pBaseFuncsTable
, pBaseInputFuncsTable
, pCritSec
, allocator
, pPinImpl
)))
1253 *ppPin
= (IPin
*)pPinImpl
;
1257 CoTaskMemFree(pPinImpl
);