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 static inline BasePin
*impl_from_IPin( IPin
*iface
)
42 return CONTAINING_RECORD(iface
, BasePin
, IPin_iface
);
45 /** Helper function, there are a lot of places where the error code is inherited
46 * The following rules apply:
48 * Return the first received error code (E_NOTIMPL is ignored)
49 * If no errors occur: return the first received non-error-code that isn't S_OK
51 static HRESULT
updatehres( HRESULT original
, HRESULT
new )
53 if (FAILED( original
) || new == E_NOTIMPL
)
56 if (FAILED( new ) || original
== S_OK
)
62 /** Sends a message from a pin further to other, similar pins
63 * fnMiddle is called on each pin found further on the stream.
64 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
66 * If the pin given is an input pin, the message will be sent downstream to other input pins
67 * If the pin given is an output pin, the message will be sent upstream to other output pins
69 static HRESULT
SendFurther( IPin
*from
, SendPinFunc fnMiddle
, LPVOID arg
, SendPinFunc fnEnd
)
74 HRESULT hr_return
= S_OK
;
75 IEnumPins
*enumpins
= NULL
;
77 PIN_DIRECTION from_dir
;
79 IPin_QueryDirection( from
, &from_dir
);
81 hr
= IPin_QueryInternalConnections( from
, NULL
, &amount
);
82 if (hr
!= E_NOTIMPL
&& amount
)
83 FIXME("Use QueryInternalConnections!\n");
86 pin_info
.pFilter
= NULL
;
87 hr
= IPin_QueryPinInfo( from
, &pin_info
);
91 hr
= IBaseFilter_EnumPins( pin_info
.pFilter
, &enumpins
);
95 hr
= IEnumPins_Reset( enumpins
);
98 hr
= IEnumPins_Next( enumpins
, 1, &pin
, NULL
);
99 if (hr
== VFW_E_ENUM_OUT_OF_SYNC
)
101 hr
= IEnumPins_Reset( enumpins
);
108 IPin_QueryDirection( pin
, &dir
);
111 IPin
*connected
= NULL
;
114 IPin_ConnectedTo( pin
, &connected
);
119 hr_local
= fnMiddle( connected
, arg
);
120 hr_return
= updatehres( hr_return
, hr_local
);
121 IPin_Release(connected
);
138 hr_local
= fnEnd( from
, arg
);
139 hr_return
= updatehres( hr_return
, hr_local
);
141 IEnumPins_Release(enumpins
);
144 if (pin_info
.pFilter
)
145 IBaseFilter_Release( pin_info
.pFilter
);
149 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
151 /* Tempting to just do a memcpy, but the name field is
152 128 characters long! We will probably never exceed 10
153 most of the time, so we are better off copying
154 each field manually */
155 strcpyW(pDest
->achName
, pSrc
->achName
);
156 pDest
->dir
= pSrc
->dir
;
157 pDest
->pFilter
= pSrc
->pFilter
;
160 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE
* pmt
)
164 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt
->majortype
), debugstr_guid(&pmt
->subtype
), debugstr_guid(&pmt
->formattype
));
167 static BOOL
CompareMediaTypes(const AM_MEDIA_TYPE
* pmt1
, const AM_MEDIA_TYPE
* pmt2
, BOOL bWildcards
)
170 dump_AM_MEDIA_TYPE(pmt1
);
172 dump_AM_MEDIA_TYPE(pmt2
);
173 return (((bWildcards
&& (IsEqualGUID(&pmt1
->majortype
, &GUID_NULL
) || IsEqualGUID(&pmt2
->majortype
, &GUID_NULL
))) || IsEqualGUID(&pmt1
->majortype
, &pmt2
->majortype
)) &&
174 ((bWildcards
&& (IsEqualGUID(&pmt1
->subtype
, &GUID_NULL
) || IsEqualGUID(&pmt2
->subtype
, &GUID_NULL
))) || IsEqualGUID(&pmt1
->subtype
, &pmt2
->subtype
)));
177 /*** Common Base Pin function */
178 HRESULT WINAPI
BasePinImpl_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
182 return VFW_S_NO_MORE_ITEMS
;
185 LONG WINAPI
BasePinImpl_GetMediaTypeVersion(BasePin
*iface
)
190 ULONG WINAPI
BasePinImpl_AddRef(IPin
* iface
)
192 BasePin
*This
= impl_from_IPin(iface
);
193 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
195 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
200 HRESULT WINAPI
BasePinImpl_Disconnect(IPin
* iface
)
203 BasePin
*This
= impl_from_IPin(iface
);
207 EnterCriticalSection(This
->pCritSec
);
209 if (This
->pConnectedTo
)
211 IPin_Release(This
->pConnectedTo
);
212 This
->pConnectedTo
= NULL
;
213 FreeMediaType(&This
->mtCurrent
);
214 ZeroMemory(&This
->mtCurrent
, sizeof(This
->mtCurrent
));
220 LeaveCriticalSection(This
->pCritSec
);
225 HRESULT WINAPI
BasePinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
228 BasePin
*This
= impl_from_IPin(iface
);
230 TRACE("(%p)\n", ppPin
);
232 EnterCriticalSection(This
->pCritSec
);
234 if (This
->pConnectedTo
)
236 *ppPin
= This
->pConnectedTo
;
242 hr
= VFW_E_NOT_CONNECTED
;
246 LeaveCriticalSection(This
->pCritSec
);
251 HRESULT WINAPI
BasePinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
254 BasePin
*This
= impl_from_IPin(iface
);
256 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
258 EnterCriticalSection(This
->pCritSec
);
260 if (This
->pConnectedTo
)
262 CopyMediaType(pmt
, &This
->mtCurrent
);
267 ZeroMemory(pmt
, sizeof(*pmt
));
268 hr
= VFW_E_NOT_CONNECTED
;
271 LeaveCriticalSection(This
->pCritSec
);
276 HRESULT WINAPI
BasePinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
278 BasePin
*This
= impl_from_IPin(iface
);
280 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
282 Copy_PinInfo(pInfo
, &This
->pinInfo
);
283 IBaseFilter_AddRef(pInfo
->pFilter
);
288 HRESULT WINAPI
BasePinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
290 BasePin
*This
= impl_from_IPin(iface
);
292 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
294 *pPinDir
= This
->pinInfo
.dir
;
299 HRESULT WINAPI
BasePinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
301 BasePin
*This
= impl_from_IPin(iface
);
303 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
305 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
307 return E_OUTOFMEMORY
;
309 strcpyW(*Id
, This
->pinInfo
.achName
);
314 HRESULT WINAPI
BasePinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
316 TRACE("(%p)->(%p)\n", iface
, pmt
);
321 HRESULT WINAPI
BasePinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
323 BasePin
*This
= impl_from_IPin(iface
);
325 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
327 /* override this method to allow enumeration of your types */
329 return EnumMediaTypes_Construct(This
, This
->pFuncsTable
->pfnGetMediaType
, This
->pFuncsTable
->pfnGetMediaTypeVersion
, ppEnum
);
332 HRESULT WINAPI
BasePinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
334 BasePin
*This
= impl_from_IPin(iface
);
336 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
338 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
341 HRESULT WINAPI
BasePinImpl_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
343 BasePin
*This
= impl_from_IPin(iface
);
345 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
347 This
->tStart
= tStart
;
354 /*** OutputPin implementation ***/
356 static inline BaseOutputPin
*impl_BaseOutputPin_from_IPin( IPin
*iface
)
358 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
.IPin_iface
);
361 static inline BaseOutputPin
*impl_BaseOutputPin_from_BasePin( BasePin
*iface
)
363 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
);
366 HRESULT WINAPI
BaseOutputPinImpl_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
368 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
370 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
374 if (IsEqualIID(riid
, &IID_IUnknown
))
376 else if (IsEqualIID(riid
, &IID_IPin
))
378 else if (IsEqualIID(riid
, &IID_IMediaSeeking
) ||
379 IsEqualIID(riid
, &IID_IQualityControl
))
381 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, riid
, ppv
);
386 IUnknown_AddRef((IUnknown
*)(*ppv
));
390 FIXME("No interface for %s!\n", debugstr_guid(riid
));
392 return E_NOINTERFACE
;
395 ULONG WINAPI
BaseOutputPinImpl_Release(IPin
* iface
)
397 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
398 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
400 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
404 FreeMediaType(&This
->pin
.mtCurrent
);
405 if (This
->pAllocator
)
406 IMemAllocator_Release(This
->pAllocator
);
407 This
->pAllocator
= NULL
;
414 HRESULT WINAPI
BaseOutputPinImpl_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
417 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
419 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
420 dump_AM_MEDIA_TYPE(pmt
);
425 /* If we try to connect to ourselves, we will definitely deadlock.
426 * There are other cases where we could deadlock too, but this
427 * catches the obvious case */
428 assert(pReceivePin
!= iface
);
430 EnterCriticalSection(This
->pin
.pCritSec
);
432 /* if we have been a specific type to connect with, then we can either connect
433 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
434 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
435 hr
= This
->pin
.pFuncsTable
->pfnAttemptConnection(&This
->pin
, pReceivePin
, pmt
);
438 /* negotiate media type */
440 IEnumMediaTypes
* pEnumCandidates
;
441 AM_MEDIA_TYPE
* pmtCandidate
= NULL
; /* Candidate media type */
443 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
445 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
447 /* try this filter's media types first */
448 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
450 assert(pmtCandidate
);
451 dump_AM_MEDIA_TYPE(pmtCandidate
);
452 if (!IsEqualGUID(&FORMAT_None
, &pmtCandidate
->formattype
)
453 && !IsEqualGUID(&GUID_NULL
, &pmtCandidate
->formattype
))
454 assert(pmtCandidate
->pbFormat
);
455 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
456 (This
->pin
.pFuncsTable
->pfnAttemptConnection(&This
->pin
, pReceivePin
, pmtCandidate
) == S_OK
))
459 DeleteMediaType(pmtCandidate
);
462 DeleteMediaType(pmtCandidate
);
465 IEnumMediaTypes_Release(pEnumCandidates
);
468 /* then try receiver filter's media types */
469 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
473 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
475 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, &fetched
))
477 assert(pmtCandidate
);
478 dump_AM_MEDIA_TYPE(pmtCandidate
);
479 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
480 (This
->pin
.pFuncsTable
->pfnAttemptConnection(&This
->pin
, pReceivePin
, pmtCandidate
) == S_OK
))
483 DeleteMediaType(pmtCandidate
);
486 DeleteMediaType(pmtCandidate
);
489 IEnumMediaTypes_Release(pEnumCandidates
);
491 } /* if negotiate media type */
493 LeaveCriticalSection(This
->pin
.pCritSec
);
495 TRACE(" -- %x\n", hr
);
499 HRESULT WINAPI
BaseOutputPinImpl_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
501 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
506 HRESULT WINAPI
BaseOutputPinImpl_Disconnect(IPin
* iface
)
509 BaseOutputPin
*This
= impl_BaseOutputPin_from_IPin(iface
);
513 EnterCriticalSection(This
->pin
.pCritSec
);
515 if (This
->pMemInputPin
)
517 IMemInputPin_Release(This
->pMemInputPin
);
518 This
->pMemInputPin
= NULL
;
520 if (This
->pin
.pConnectedTo
)
522 IPin_Release(This
->pin
.pConnectedTo
);
523 This
->pin
.pConnectedTo
= NULL
;
524 FreeMediaType(&This
->pin
.mtCurrent
);
525 ZeroMemory(&This
->pin
.mtCurrent
, sizeof(This
->pin
.mtCurrent
));
531 LeaveCriticalSection(This
->pin
.pCritSec
);
536 HRESULT WINAPI
BaseOutputPinImpl_EndOfStream(IPin
* iface
)
540 /* not supposed to do anything in an output pin */
545 HRESULT WINAPI
BaseOutputPinImpl_BeginFlush(IPin
* iface
)
547 TRACE("(%p)->()\n", iface
);
549 /* not supposed to do anything in an output pin */
554 HRESULT WINAPI
BaseOutputPinImpl_EndFlush(IPin
* iface
)
556 TRACE("(%p)->()\n", iface
);
558 /* not supposed to do anything in an output pin */
563 static const IPinVtbl OutputPin_Vtbl
=
565 BaseOutputPinImpl_QueryInterface
,
567 BaseOutputPinImpl_Release
,
568 BaseOutputPinImpl_Connect
,
569 BaseOutputPinImpl_ReceiveConnection
,
570 BaseOutputPinImpl_Disconnect
,
571 BasePinImpl_ConnectedTo
,
572 BasePinImpl_ConnectionMediaType
,
573 BasePinImpl_QueryPinInfo
,
574 BasePinImpl_QueryDirection
,
576 BasePinImpl_QueryAccept
,
577 BasePinImpl_EnumMediaTypes
,
578 BasePinImpl_QueryInternalConnections
,
579 BaseOutputPinImpl_EndOfStream
,
580 BaseOutputPinImpl_BeginFlush
,
581 BaseOutputPinImpl_EndFlush
,
582 BasePinImpl_NewSegment
585 HRESULT WINAPI
BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin
*This
, IMediaSample
** ppSample
, REFERENCE_TIME
* tStart
, REFERENCE_TIME
* tStop
, DWORD dwFlags
)
589 TRACE("(%p, %p, %p, %x)\n", ppSample
, tStart
, tStop
, dwFlags
);
591 if (!This
->pin
.pConnectedTo
)
592 hr
= VFW_E_NOT_CONNECTED
;
595 hr
= IMemAllocator_GetBuffer(This
->pAllocator
, ppSample
, tStart
, tStop
, dwFlags
);
598 hr
= IMediaSample_SetTime(*ppSample
, tStart
, tStop
);
604 /* replaces OutputPin_SendSample */
605 HRESULT WINAPI
BaseOutputPinImpl_Deliver(BaseOutputPin
*This
, IMediaSample
* pSample
)
608 IMemInputPin
* pMemConnected
= NULL
;
611 EnterCriticalSection(This
->pin
.pCritSec
);
613 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
614 hr
= VFW_E_NOT_CONNECTED
;
617 /* we don't have the lock held when using This->pMemInputPin,
618 * so we need to AddRef it to stop it being deleted while we are
619 * using it. Same with its filter. */
620 pMemConnected
= This
->pMemInputPin
;
621 IMemInputPin_AddRef(pMemConnected
);
622 hr
= IPin_QueryPinInfo(This
->pin
.pConnectedTo
, &pinInfo
);
625 LeaveCriticalSection(This
->pin
.pCritSec
);
629 /* NOTE: if we are in a critical section when Receive is called
630 * then it causes some problems (most notably with the native Video
631 * Renderer) if we are re-entered for whatever reason */
632 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
634 /* If the filter's destroyed, tell upstream to stop sending data */
635 if(IBaseFilter_Release(pinInfo
.pFilter
) == 0 && SUCCEEDED(hr
))
639 IMemInputPin_Release(pMemConnected
);
644 /* replaces OutputPin_CommitAllocator */
645 HRESULT WINAPI
BaseOutputPinImpl_Active(BaseOutputPin
*This
)
649 TRACE("(%p)->()\n", This
);
651 EnterCriticalSection(This
->pin
.pCritSec
);
653 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
654 hr
= VFW_E_NOT_CONNECTED
;
656 hr
= IMemAllocator_Commit(This
->pAllocator
);
658 LeaveCriticalSection(This
->pin
.pCritSec
);
660 TRACE("--> %08x\n", hr
);
664 /* replaces OutputPin_DecommitAllocator */
665 HRESULT WINAPI
BaseOutputPinImpl_Inactive(BaseOutputPin
*This
)
669 TRACE("(%p)->()\n", This
);
671 EnterCriticalSection(This
->pin
.pCritSec
);
673 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
674 hr
= VFW_E_NOT_CONNECTED
;
676 hr
= IMemAllocator_Decommit(This
->pAllocator
);
678 LeaveCriticalSection(This
->pin
.pCritSec
);
680 TRACE("--> %08x\n", hr
);
684 /* replaces OutputPin_DeliverDisconnect */
685 HRESULT WINAPI
BaseOutputPinImpl_BreakConnect(BaseOutputPin
*This
)
689 TRACE("(%p)->()\n", This
);
691 EnterCriticalSection(This
->pin
.pCritSec
);
693 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
694 hr
= VFW_E_NOT_CONNECTED
;
697 hr
= IMemAllocator_Decommit(This
->pAllocator
);
700 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
702 IPin_Disconnect(&This
->pin
.IPin_iface
);
704 LeaveCriticalSection(This
->pin
.pCritSec
);
709 HRESULT WINAPI
BaseOutputPinImpl_InitAllocator(BaseOutputPin
*This
, IMemAllocator
**pMemAlloc
)
711 return CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMemAllocator
, (LPVOID
*)pMemAlloc
);
714 HRESULT WINAPI
BaseOutputPinImpl_DecideAllocator(BaseOutputPin
*This
, IMemInputPin
*pPin
, IMemAllocator
**pAlloc
)
718 hr
= IMemInputPin_GetAllocator(pPin
, pAlloc
);
720 if (hr
== VFW_E_NO_ALLOCATOR
)
721 /* Input pin provides no allocator, use standard memory allocator */
722 hr
= BaseOutputPinImpl_InitAllocator(This
, pAlloc
);
726 ALLOCATOR_PROPERTIES rProps
;
727 ZeroMemory(&rProps
, sizeof(ALLOCATOR_PROPERTIES
));
729 IMemInputPin_GetAllocatorRequirements(pPin
, &rProps
);
730 hr
= This
->pFuncsTable
->pfnDecideBufferSize(This
, *pAlloc
, &rProps
);
734 hr
= IMemInputPin_NotifyAllocator(pPin
, *pAlloc
, FALSE
);
739 /*** The Construct functions ***/
741 /* Function called as a helper to IPin_Connect */
742 /* specific AM_MEDIA_TYPE - it cannot be NULL */
743 HRESULT WINAPI
BaseOutputPinImpl_AttemptConnection(BasePin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
745 BaseOutputPin
*This
= impl_BaseOutputPin_from_BasePin(iface
);
747 IMemAllocator
* pMemAlloc
= NULL
;
749 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
750 dump_AM_MEDIA_TYPE(pmt
);
752 /* FIXME: call queryacceptproc */
754 This
->pin
.pConnectedTo
= pReceivePin
;
755 IPin_AddRef(pReceivePin
);
756 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
758 hr
= IPin_ReceiveConnection(pReceivePin
, &iface
->IPin_iface
, pmt
);
760 /* get the IMemInputPin interface we will use to deliver samples to the
764 This
->pMemInputPin
= NULL
;
765 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
769 hr
= This
->pFuncsTable
->pfnDecideAllocator(This
, This
->pMemInputPin
, &pMemAlloc
);
771 This
->pAllocator
= pMemAlloc
;
773 IMemAllocator_Release(pMemAlloc
);
776 /* break connection if we couldn't get the allocator */
779 if (This
->pMemInputPin
)
780 IMemInputPin_Release(This
->pMemInputPin
);
781 This
->pMemInputPin
= NULL
;
783 IPin_Disconnect(pReceivePin
);
789 IPin_Release(This
->pin
.pConnectedTo
);
790 This
->pin
.pConnectedTo
= NULL
;
791 FreeMediaType(&This
->pin
.mtCurrent
);
794 TRACE(" -- %x\n", hr
);
798 static HRESULT
OutputPin_Init(const IPinVtbl
*OutputPin_Vtbl
, const PIN_INFO
* pPinInfo
, const BaseOutputPinFuncTable
* vtbl
, LPCRITICAL_SECTION pCritSec
, BaseOutputPin
* pPinImpl
)
802 /* Common attributes */
803 pPinImpl
->pin
.IPin_iface
.lpVtbl
= OutputPin_Vtbl
;
804 pPinImpl
->pin
.refCount
= 1;
805 pPinImpl
->pin
.pConnectedTo
= NULL
;
806 pPinImpl
->pin
.pCritSec
= pCritSec
;
807 pPinImpl
->pin
.tStart
= 0;
808 pPinImpl
->pin
.tStop
= 0;
809 pPinImpl
->pin
.dRate
= 1.0;
810 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
811 pPinImpl
->pin
.pFuncsTable
= &vtbl
->base
;
812 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
814 /* Output pin attributes */
815 pPinImpl
->pMemInputPin
= NULL
;
816 pPinImpl
->pAllocator
= NULL
;
817 pPinImpl
->pFuncsTable
= vtbl
;
822 HRESULT WINAPI
BaseOutputPin_Construct(const IPinVtbl
*OutputPin_Vtbl
, LONG outputpin_size
, const PIN_INFO
* pPinInfo
, const BaseOutputPinFuncTable
* vtbl
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
824 BaseOutputPin
* pPinImpl
;
828 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
830 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
834 assert(outputpin_size
>= sizeof(BaseOutputPin
));
835 assert(vtbl
->base
.pfnAttemptConnection
);
837 pPinImpl
= CoTaskMemAlloc(outputpin_size
);
840 return E_OUTOFMEMORY
;
842 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl
, pPinInfo
, vtbl
, pCritSec
, pPinImpl
)))
844 *ppPin
= &pPinImpl
->pin
.IPin_iface
;
848 CoTaskMemFree(pPinImpl
);
852 /*** Input Pin implementation ***/
854 static inline BaseInputPin
*impl_BaseInputPin_from_IPin( IPin
*iface
)
856 return CONTAINING_RECORD(iface
, BaseInputPin
, pin
.IPin_iface
);
859 static inline BaseInputPin
*impl_BaseInputPin_from_BasePin( BasePin
*iface
)
861 return CONTAINING_RECORD(iface
, BaseInputPin
, pin
);
864 HRESULT WINAPI
BaseInputPinImpl_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
866 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
868 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
872 if (IsEqualIID(riid
, &IID_IUnknown
))
874 else if (IsEqualIID(riid
, &IID_IPin
))
876 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
877 *ppv
= &This
->IMemInputPin_iface
;
878 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
880 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
885 IUnknown_AddRef((IUnknown
*)(*ppv
));
889 FIXME("No interface for %s!\n", debugstr_guid(riid
));
891 return E_NOINTERFACE
;
894 ULONG WINAPI
BaseInputPinImpl_Release(IPin
* iface
)
896 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
897 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
899 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
903 FreeMediaType(&This
->pin
.mtCurrent
);
904 if (This
->pAllocator
)
905 IMemAllocator_Release(This
->pAllocator
);
906 This
->pAllocator
= NULL
;
907 This
->pin
.IPin_iface
.lpVtbl
= NULL
;
915 HRESULT WINAPI
BaseInputPinImpl_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
917 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
923 HRESULT WINAPI
BaseInputPinImpl_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
925 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
926 PIN_DIRECTION pindirReceive
;
929 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
930 dump_AM_MEDIA_TYPE(pmt
);
932 EnterCriticalSection(This
->pin
.pCritSec
);
934 if (This
->pin
.pConnectedTo
)
935 hr
= VFW_E_ALREADY_CONNECTED
;
937 if (SUCCEEDED(hr
) && This
->pin
.pFuncsTable
->pfnCheckMediaType(&This
->pin
, pmt
) != S_OK
)
938 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
939 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
943 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
945 if (pindirReceive
!= PINDIR_OUTPUT
)
947 ERR("Can't connect from non-output pin\n");
948 hr
= VFW_E_INVALID_DIRECTION
;
954 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
955 This
->pin
.pConnectedTo
= pReceivePin
;
956 IPin_AddRef(pReceivePin
);
959 LeaveCriticalSection(This
->pin
.pCritSec
);
964 static HRESULT
deliver_endofstream(IPin
* pin
, LPVOID unused
)
966 return IPin_EndOfStream( pin
);
969 HRESULT WINAPI
BaseInputPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
971 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
973 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
975 return (This
->pin
.pFuncsTable
->pfnCheckMediaType(&This
->pin
, pmt
) == S_OK
? S_OK
: S_FALSE
);
978 HRESULT WINAPI
BaseInputPinImpl_EndOfStream(IPin
* iface
)
981 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
983 TRACE("(%p)\n", This
);
985 EnterCriticalSection(This
->pin
.pCritSec
);
989 This
->end_of_stream
= TRUE
;
990 LeaveCriticalSection(This
->pin
.pCritSec
);
993 hr
= SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
997 static HRESULT
deliver_beginflush(IPin
* pin
, LPVOID unused
)
999 return IPin_BeginFlush( pin
);
1002 HRESULT WINAPI
BaseInputPinImpl_BeginFlush(IPin
* iface
)
1004 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
1006 TRACE("() semi-stub\n");
1008 EnterCriticalSection(This
->pin
.pCritSec
);
1009 This
->flushing
= TRUE
;
1011 hr
= SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
1012 LeaveCriticalSection(This
->pin
.pCritSec
);
1017 static HRESULT
deliver_endflush(IPin
* pin
, LPVOID unused
)
1019 return IPin_EndFlush( pin
);
1022 HRESULT WINAPI
BaseInputPinImpl_EndFlush(IPin
* iface
)
1024 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
1026 TRACE("(%p)\n", This
);
1028 EnterCriticalSection(This
->pin
.pCritSec
);
1029 This
->flushing
= This
->end_of_stream
= FALSE
;
1031 hr
= SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
1032 LeaveCriticalSection(This
->pin
.pCritSec
);
1037 typedef struct newsegmentargs
1039 REFERENCE_TIME tStart
, tStop
;
1043 static HRESULT
deliver_newsegment(IPin
*pin
, LPVOID data
)
1045 newsegmentargs
*args
= data
;
1046 return IPin_NewSegment(pin
, args
->tStart
, args
->tStop
, args
->rate
);
1049 HRESULT WINAPI
BaseInputPinImpl_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1051 BaseInputPin
*This
= impl_BaseInputPin_from_IPin(iface
);
1052 newsegmentargs args
;
1054 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
1056 args
.tStart
= This
->pin
.tStart
= tStart
;
1057 args
.tStop
= This
->pin
.tStop
= tStop
;
1058 args
.rate
= This
->pin
.dRate
= dRate
;
1060 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
1063 static const IPinVtbl InputPin_Vtbl
=
1065 BaseInputPinImpl_QueryInterface
,
1067 BaseInputPinImpl_Release
,
1068 BaseInputPinImpl_Connect
,
1069 BaseInputPinImpl_ReceiveConnection
,
1070 BasePinImpl_Disconnect
,
1071 BasePinImpl_ConnectedTo
,
1072 BasePinImpl_ConnectionMediaType
,
1073 BasePinImpl_QueryPinInfo
,
1074 BasePinImpl_QueryDirection
,
1075 BasePinImpl_QueryId
,
1076 BaseInputPinImpl_QueryAccept
,
1077 BasePinImpl_EnumMediaTypes
,
1078 BasePinImpl_QueryInternalConnections
,
1079 BaseInputPinImpl_EndOfStream
,
1080 BaseInputPinImpl_BeginFlush
,
1081 BaseInputPinImpl_EndFlush
,
1082 BaseInputPinImpl_NewSegment
1085 /*** IMemInputPin implementation ***/
1087 static inline BaseInputPin
*impl_from_IMemInputPin( IMemInputPin
*iface
)
1089 return CONTAINING_RECORD(iface
, BaseInputPin
, IMemInputPin_iface
);
1092 static HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1094 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1096 return IPin_QueryInterface(&This
->pin
.IPin_iface
, riid
, ppv
);
1099 static ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
1101 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1103 return IPin_AddRef(&This
->pin
.IPin_iface
);
1106 static ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
1108 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1110 return IPin_Release(&This
->pin
.IPin_iface
);
1113 static HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
1115 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1117 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
1119 *ppAllocator
= This
->pAllocator
;
1121 IMemAllocator_AddRef(*ppAllocator
);
1123 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
1126 static HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
1128 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1130 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
1133 FIXME("Read only flag not handled yet!\n");
1135 /* FIXME: Should we release the allocator on disconnection? */
1138 WARN("Null allocator\n");
1142 if (This
->preferred_allocator
&& pAllocator
!= This
->preferred_allocator
)
1145 if (This
->pAllocator
)
1146 IMemAllocator_Release(This
->pAllocator
);
1147 This
->pAllocator
= pAllocator
;
1148 if (This
->pAllocator
)
1149 IMemAllocator_AddRef(This
->pAllocator
);
1154 static HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
1156 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1158 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
1160 /* override this method if you have any specific requirements */
1165 static HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
1167 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1168 HRESULT hr
= S_FALSE
;
1170 /* this trace commented out for performance reasons */
1171 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1172 if (This
->pFuncsTable
->pfnReceive
)
1173 hr
= This
->pFuncsTable
->pfnReceive(This
, pSample
);
1177 static HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, LONG nSamples
, LONG
*nSamplesProcessed
)
1180 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1182 TRACE("(%p/%p)->(%p, %d, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
1184 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
1186 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
1194 static HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
1196 BaseInputPin
*This
= impl_from_IMemInputPin(iface
);
1198 TRACE("(%p/%p)->()\n", This
, iface
);
1203 static const IMemInputPinVtbl MemInputPin_Vtbl
=
1205 MemInputPin_QueryInterface
,
1207 MemInputPin_Release
,
1208 MemInputPin_GetAllocator
,
1209 MemInputPin_NotifyAllocator
,
1210 MemInputPin_GetAllocatorRequirements
,
1211 MemInputPin_Receive
,
1212 MemInputPin_ReceiveMultiple
,
1213 MemInputPin_ReceiveCanBlock
1216 static HRESULT
InputPin_Init(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
,
1217 const BaseInputPinFuncTable
* vtbl
,
1218 LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, BaseInputPin
* pPinImpl
)
1222 /* Common attributes */
1223 pPinImpl
->pin
.refCount
= 1;
1224 pPinImpl
->pin
.pConnectedTo
= NULL
;
1225 pPinImpl
->pin
.pCritSec
= pCritSec
;
1226 pPinImpl
->pin
.tStart
= 0;
1227 pPinImpl
->pin
.tStop
= 0;
1228 pPinImpl
->pin
.dRate
= 1.0;
1229 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1230 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1231 pPinImpl
->pin
.pFuncsTable
= &vtbl
->base
;
1233 /* Input pin attributes */
1234 pPinImpl
->pFuncsTable
= vtbl
;
1235 pPinImpl
->pAllocator
= pPinImpl
->preferred_allocator
= allocator
;
1236 if (pPinImpl
->preferred_allocator
)
1237 IMemAllocator_AddRef(pPinImpl
->preferred_allocator
);
1238 pPinImpl
->pin
.IPin_iface
.lpVtbl
= InputPin_Vtbl
;
1239 pPinImpl
->IMemInputPin_iface
.lpVtbl
= &MemInputPin_Vtbl
;
1240 pPinImpl
->flushing
= pPinImpl
->end_of_stream
= FALSE
;
1245 HRESULT
BaseInputPin_Construct(const IPinVtbl
*InputPin_Vtbl
, LONG inputpin_size
, const PIN_INFO
* pPinInfo
,
1246 const BaseInputPinFuncTable
* vtbl
,
1247 LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, IPin
** ppPin
)
1249 BaseInputPin
* pPinImpl
;
1253 assert(inputpin_size
>= sizeof(BaseInputPin
));
1254 assert(vtbl
->base
.pfnCheckMediaType
);
1256 if (pPinInfo
->dir
!= PINDIR_INPUT
)
1258 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1259 return E_INVALIDARG
;
1262 pPinImpl
= CoTaskMemAlloc(inputpin_size
);
1265 return E_OUTOFMEMORY
;
1267 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl
, pPinInfo
, vtbl
, pCritSec
, allocator
, pPinImpl
)))
1269 *ppPin
= (IPin
*)pPinImpl
;
1273 CoTaskMemFree(pPinImpl
);