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 );
138 if (pin_info.pFilter)
139 IBaseFilter_Release( pin_info.pFilter );
143 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
145 /* Tempting to just do a memcpy, but the name field is
146 128 characters long! We will probably never exceed 10
147 most of the time, so we are better off copying
148 each field manually */
149 strcpyW(pDest->achName, pSrc->achName);
150 pDest->dir = pSrc->dir;
151 pDest->pFilter = pSrc->pFilter;
154 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
158 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
161 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
164 dump_AM_MEDIA_TYPE(pmt1);
166 dump_AM_MEDIA_TYPE(pmt2);
167 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
168 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
171 /*** Common Base Pin function */
172 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
176 return VFW_S_NO_MORE_ITEMS;
179 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
184 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
186 BasePin *This = (BasePin *)iface;
187 ULONG refCount = InterlockedIncrement(&This->refCount);
189 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
194 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
197 BasePin *This = (BasePin *)iface;
201 EnterCriticalSection(This->pCritSec);
203 if (This->pConnectedTo)
205 IPin_Release(This->pConnectedTo);
206 This->pConnectedTo = NULL;
207 FreeMediaType(&This->mtCurrent);
208 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
214 LeaveCriticalSection(This->pCritSec);
219 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
222 BasePin *This = (BasePin *)iface;
224 TRACE("(%p)\n", ppPin);
226 EnterCriticalSection(This->pCritSec);
228 if (This->pConnectedTo)
230 *ppPin = This->pConnectedTo;
236 hr = VFW_E_NOT_CONNECTED;
240 LeaveCriticalSection(This->pCritSec);
245 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
248 BasePin *This = (BasePin *)iface;
250 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
252 EnterCriticalSection(This->pCritSec);
254 if (This->pConnectedTo)
256 CopyMediaType(pmt, &This->mtCurrent);
261 ZeroMemory(pmt, sizeof(*pmt));
262 hr = VFW_E_NOT_CONNECTED;
265 LeaveCriticalSection(This->pCritSec);
270 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
272 BasePin *This = (BasePin *)iface;
274 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
276 Copy_PinInfo(pInfo, &This->pinInfo);
277 IBaseFilter_AddRef(pInfo->pFilter);
282 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
284 BasePin *This = (BasePin *)iface;
286 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
288 *pPinDir = This->pinInfo.dir;
293 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
295 BasePin *This = (BasePin *)iface;
297 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
299 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
301 return E_OUTOFMEMORY;
303 strcpyW(*Id, This->pinInfo.achName);
308 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
310 TRACE("(%p)->(%p)\n", iface, pmt);
315 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
317 BasePin *This = (BasePin *)iface;
319 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
321 /* override this method to allow enumeration of your types */
323 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
326 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
328 BasePin *This = (BasePin *)iface;
330 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
332 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
335 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
337 BasePin *This = (BasePin *)iface;
339 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
341 This->tStart = tStart;
348 /*** OutputPin implementation ***/
350 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
352 BaseOutputPin *This = (BaseOutputPin *)iface;
354 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
358 if (IsEqualIID(riid, &IID_IUnknown))
360 else if (IsEqualIID(riid, &IID_IPin))
362 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
363 IsEqualIID(riid, &IID_IQualityControl))
365 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
370 IUnknown_AddRef((IUnknown *)(*ppv));
374 FIXME("No interface for %s!\n", debugstr_guid(riid));
376 return E_NOINTERFACE;
379 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
381 BaseOutputPin *This = (BaseOutputPin *)iface;
382 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
384 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
388 FreeMediaType(&This->pin.mtCurrent);
395 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
398 BaseOutputPin *This = (BaseOutputPin *)iface;
400 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
401 dump_AM_MEDIA_TYPE(pmt);
403 /* If we try to connect to ourself, we will definitely deadlock.
404 * There are other cases where we could deadlock too, but this
405 * catches the obvious case */
406 assert(pReceivePin != iface);
408 EnterCriticalSection(This->pin.pCritSec);
410 /* if we have been a specific type to connect with, then we can either connect
411 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
412 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
413 hr = This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmt);
416 /* negotiate media type */
418 IEnumMediaTypes * pEnumCandidates;
419 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
421 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
423 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
425 /* try this filter's media types first */
426 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
428 assert(pmtCandidate);
429 dump_AM_MEDIA_TYPE(pmtCandidate);
430 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
431 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
432 assert(pmtCandidate->pbFormat);
433 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
434 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
437 DeleteMediaType(pmtCandidate);
440 DeleteMediaType(pmtCandidate);
443 IEnumMediaTypes_Release(pEnumCandidates);
446 /* then try receiver filter's media types */
447 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
449 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
451 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
453 assert(pmtCandidate);
454 dump_AM_MEDIA_TYPE(pmtCandidate);
455 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
456 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
459 DeleteMediaType(pmtCandidate);
462 DeleteMediaType(pmtCandidate);
465 IEnumMediaTypes_Release(pEnumCandidates);
467 } /* if negotiate media type */
469 LeaveCriticalSection(This->pin.pCritSec);
471 TRACE(" -- %x\n", hr);
475 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
477 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
482 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
485 BaseOutputPin *This = (BaseOutputPin *)iface;
489 EnterCriticalSection(This->pin.pCritSec);
491 if (This->pMemInputPin)
493 IMemInputPin_Release(This->pMemInputPin);
494 This->pMemInputPin = NULL;
496 if (This->pin.pConnectedTo)
498 IPin_Release(This->pin.pConnectedTo);
499 This->pin.pConnectedTo = NULL;
500 FreeMediaType(&This->pin.mtCurrent);
501 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
507 LeaveCriticalSection(This->pin.pCritSec);
512 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
516 /* not supposed to do anything in an output pin */
521 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
523 TRACE("(%p)->()\n", iface);
525 /* not supposed to do anything in an output pin */
530 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
532 TRACE("(%p)->()\n", iface);
534 /* not supposed to do anything in an output pin */
539 static const IPinVtbl OutputPin_Vtbl =
541 BaseOutputPinImpl_QueryInterface,
543 BaseOutputPinImpl_Release,
544 BaseOutputPinImpl_Connect,
545 BaseOutputPinImpl_ReceiveConnection,
546 BaseOutputPinImpl_Disconnect,
547 BasePinImpl_ConnectedTo,
548 BasePinImpl_ConnectionMediaType,
549 BasePinImpl_QueryPinInfo,
550 BasePinImpl_QueryDirection,
552 BasePinImpl_QueryAccept,
553 BasePinImpl_EnumMediaTypes,
554 BasePinImpl_QueryInternalConnections,
555 BaseOutputPinImpl_EndOfStream,
556 BaseOutputPinImpl_BeginFlush,
557 BaseOutputPinImpl_EndFlush,
558 BasePinImpl_NewSegment
561 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
565 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
567 EnterCriticalSection(This->pin.pCritSec);
569 if (!This->pin.pConnectedTo)
570 hr = VFW_E_NOT_CONNECTED;
573 IMemAllocator * pAlloc = NULL;
575 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
578 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
581 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
584 IMemAllocator_Release(pAlloc);
587 LeaveCriticalSection(This->pin.pCritSec);
592 /* replaces OutputPin_SendSample */
593 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
596 IMemInputPin * pMemConnected = NULL;
599 EnterCriticalSection(This->pin.pCritSec);
601 if (!This->pin.pConnectedTo || !This->pMemInputPin)
602 hr = VFW_E_NOT_CONNECTED;
605 /* we don't have the lock held when using This->pMemInputPin,
606 * so we need to AddRef it to stop it being deleted while we are
607 * using it. Same with its filter. */
608 pMemConnected = This->pMemInputPin;
609 IMemInputPin_AddRef(pMemConnected);
610 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
613 LeaveCriticalSection(This->pin.pCritSec);
617 /* NOTE: if we are in a critical section when Receive is called
618 * then it causes some problems (most notably with the native Video
619 * Renderer) if we are re-entered for whatever reason */
620 hr = IMemInputPin_Receive(pMemConnected, pSample);
622 /* If the filter's destroyed, tell upstream to stop sending data */
623 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
627 IMemInputPin_Release(pMemConnected);
632 /* replaces OutputPin_CommitAllocator */
633 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
637 TRACE("(%p)->()\n", This);
639 EnterCriticalSection(This->pin.pCritSec);
641 if (!This->pin.pConnectedTo || !This->pMemInputPin)
642 hr = VFW_E_NOT_CONNECTED;
645 IMemAllocator * pAlloc = NULL;
647 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
650 hr = IMemAllocator_Commit(pAlloc);
653 IMemAllocator_Release(pAlloc);
656 LeaveCriticalSection(This->pin.pCritSec);
658 TRACE("--> %08x\n", hr);
662 /* replaces OutputPin_DecommitAllocator */
663 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
667 TRACE("(%p)->()\n", This);
669 EnterCriticalSection(This->pin.pCritSec);
671 if (!This->pin.pConnectedTo || !This->pMemInputPin)
672 hr = VFW_E_NOT_CONNECTED;
675 IMemAllocator * pAlloc = NULL;
677 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
680 hr = IMemAllocator_Decommit(pAlloc);
683 IMemAllocator_Release(pAlloc);
686 LeaveCriticalSection(This->pin.pCritSec);
688 TRACE("--> %08x\n", hr);
692 /* replaces OutputPin_DeliverDisconnect */
693 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
697 TRACE("(%p)->()\n", This);
699 EnterCriticalSection(This->pin.pCritSec);
701 if (!This->pin.pConnectedTo || !This->pMemInputPin)
702 hr = VFW_E_NOT_CONNECTED;
705 IMemAllocator * pAlloc = NULL;
707 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
710 hr = IMemAllocator_Decommit(pAlloc);
713 IMemAllocator_Release(pAlloc);
716 hr = IPin_Disconnect(This->pin.pConnectedTo);
718 IPin_Disconnect((IPin *)This);
720 LeaveCriticalSection(This->pin.pCritSec);
725 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
727 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
730 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
734 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
736 if (hr == VFW_E_NO_ALLOCATOR)
737 /* Input pin provides no allocator, use standard memory allocator */
738 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
742 ALLOCATOR_PROPERTIES rProps;
743 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
745 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
746 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
750 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
755 /*** The Construct functions ***/
757 /* Function called as a helper to IPin_Connect */
758 /* specific AM_MEDIA_TYPE - it cannot be NULL */
759 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
761 BaseOutputPin *This = (BaseOutputPin *)iface;
763 IMemAllocator * pMemAlloc = NULL;
765 TRACE("(%p, %p)\n", pReceivePin, pmt);
766 dump_AM_MEDIA_TYPE(pmt);
768 /* FIXME: call queryacceptproc */
770 This->pin.pConnectedTo = pReceivePin;
771 IPin_AddRef(pReceivePin);
772 CopyMediaType(&This->pin.mtCurrent, pmt);
774 hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
776 /* get the IMemInputPin interface we will use to deliver samples to the
780 This->pMemInputPin = NULL;
781 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
785 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
787 IMemAllocator_Release(pMemAlloc);
790 /* break connection if we couldn't get the allocator */
793 if (This->pMemInputPin)
794 IMemInputPin_Release(This->pMemInputPin);
795 This->pMemInputPin = NULL;
797 IPin_Disconnect(pReceivePin);
803 IPin_Release(This->pin.pConnectedTo);
804 This->pin.pConnectedTo = NULL;
805 FreeMediaType(&This->pin.mtCurrent);
808 TRACE(" -- %x\n", hr);
812 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
816 /* Common attributes */
817 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
818 pPinImpl->pin.refCount = 1;
819 pPinImpl->pin.pConnectedTo = NULL;
820 pPinImpl->pin.pCritSec = pCritSec;
821 pPinImpl->pin.tStart = 0;
822 pPinImpl->pin.tStop = 0;
823 pPinImpl->pin.dRate = 1.0;
824 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
825 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
826 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
828 /* Output pin attributes */
829 pPinImpl->pMemInputPin = NULL;
830 pPinImpl->pFuncsTable = pBaseOutputFuncsTable;
835 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)
837 BaseOutputPin * pPinImpl;
841 if (pPinInfo->dir != PINDIR_OUTPUT)
843 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
847 assert(outputpin_size >= sizeof(BaseOutputPin));
848 assert(pBaseFuncsTable->pfnAttemptConnection);
850 pPinImpl = CoTaskMemAlloc(outputpin_size);
853 return E_OUTOFMEMORY;
855 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseOutputFuncsTable, pCritSec, pPinImpl)))
857 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
861 CoTaskMemFree(pPinImpl);
865 /*** Input Pin implementation ***/
867 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
869 BaseInputPin *This = (BaseInputPin *)iface;
871 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
875 if (IsEqualIID(riid, &IID_IUnknown))
877 else if (IsEqualIID(riid, &IID_IPin))
879 else if (IsEqualIID(riid, &IID_IMemInputPin))
880 *ppv = &This->lpVtblMemInput;
881 else if (IsEqualIID(riid, &IID_IMediaSeeking))
883 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
888 IUnknown_AddRef((IUnknown *)(*ppv));
892 FIXME("No interface for %s!\n", debugstr_guid(riid));
894 return E_NOINTERFACE;
897 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
899 BaseInputPin *This = (BaseInputPin *)iface;
900 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
902 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
906 FreeMediaType(&This->pin.mtCurrent);
907 if (This->pAllocator)
908 IMemAllocator_Release(This->pAllocator);
909 This->pAllocator = NULL;
910 This->pin.lpVtbl = NULL;
918 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
920 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
926 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
928 BaseInputPin *This = (BaseInputPin *)iface;
929 PIN_DIRECTION pindirReceive;
932 TRACE("(%p, %p)\n", pReceivePin, pmt);
933 dump_AM_MEDIA_TYPE(pmt);
935 EnterCriticalSection(This->pin.pCritSec);
937 if (This->pin.pConnectedTo)
938 hr = VFW_E_ALREADY_CONNECTED;
940 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
941 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
942 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
946 IPin_QueryDirection(pReceivePin, &pindirReceive);
948 if (pindirReceive != PINDIR_OUTPUT)
950 ERR("Can't connect from non-output pin\n");
951 hr = VFW_E_INVALID_DIRECTION;
957 CopyMediaType(&This->pin.mtCurrent, pmt);
958 This->pin.pConnectedTo = pReceivePin;
959 IPin_AddRef(pReceivePin);
962 LeaveCriticalSection(This->pin.pCritSec);
967 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
969 return IPin_EndOfStream( pin );
972 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
974 BaseInputPin *This = (BaseInputPin *)iface;
976 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
978 return (This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) == S_OK ? S_OK : S_FALSE);
981 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
984 BaseInputPin *This = (BaseInputPin *)iface;
986 TRACE("(%p)\n", This);
988 EnterCriticalSection(This->pin.pCritSec);
992 This->end_of_stream = 1;
993 LeaveCriticalSection(This->pin.pCritSec);
996 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
1000 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1002 return IPin_BeginFlush( pin );
1005 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1007 BaseInputPin *This = (BaseInputPin *)iface;
1009 TRACE("() semi-stub\n");
1011 EnterCriticalSection(This->pin.pCritSec);
1014 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1015 LeaveCriticalSection(This->pin.pCritSec);
1020 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1022 return IPin_EndFlush( pin );
1025 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1027 BaseInputPin *This = (BaseInputPin *)iface;
1029 TRACE("(%p)\n", This);
1031 EnterCriticalSection(This->pin.pCritSec);
1032 This->flushing = This->end_of_stream = 0;
1034 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1035 LeaveCriticalSection(This->pin.pCritSec);
1040 typedef struct newsegmentargs
1042 REFERENCE_TIME tStart, tStop;
1046 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1048 newsegmentargs *args = data;
1049 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1052 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1054 BaseInputPin *This = (BaseInputPin *)iface;
1055 newsegmentargs args;
1057 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1059 args.tStart = This->pin.tStart = tStart;
1060 args.tStop = This->pin.tStop = tStop;
1061 args.rate = This->pin.dRate = dRate;
1063 return SendFurther( iface, deliver_newsegment, &args, NULL );
1066 static const IPinVtbl InputPin_Vtbl =
1068 BaseInputPinImpl_QueryInterface,
1070 BaseInputPinImpl_Release,
1071 BaseInputPinImpl_Connect,
1072 BaseInputPinImpl_ReceiveConnection,
1073 BasePinImpl_Disconnect,
1074 BasePinImpl_ConnectedTo,
1075 BasePinImpl_ConnectionMediaType,
1076 BasePinImpl_QueryPinInfo,
1077 BasePinImpl_QueryDirection,
1078 BasePinImpl_QueryId,
1079 BaseInputPinImpl_QueryAccept,
1080 BasePinImpl_EnumMediaTypes,
1081 BasePinImpl_QueryInternalConnections,
1082 BaseInputPinImpl_EndOfStream,
1083 BaseInputPinImpl_BeginFlush,
1084 BaseInputPinImpl_EndFlush,
1085 BaseInputPinImpl_NewSegment
1088 /*** IMemInputPin implementation ***/
1090 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1092 return (BaseInputPin *)((char*)iface - FIELD_OFFSET(BaseInputPin, lpVtblMemInput));
1095 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1097 BaseInputPin *This = impl_from_IMemInputPin(iface);
1099 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
1102 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1104 BaseInputPin *This = impl_from_IMemInputPin(iface);
1106 return IPin_AddRef((IPin *)&This->pin);
1109 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1111 BaseInputPin *This = impl_from_IMemInputPin(iface);
1113 return IPin_Release((IPin *)&This->pin);
1116 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1118 BaseInputPin *This = impl_from_IMemInputPin(iface);
1120 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1122 *ppAllocator = This->pAllocator;
1124 IMemAllocator_AddRef(*ppAllocator);
1126 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1129 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1131 BaseInputPin *This = impl_from_IMemInputPin(iface);
1133 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1136 FIXME("Read only flag not handled yet!\n");
1138 /* FIXME: Should we release the allocator on disconnection? */
1141 WARN("Null allocator\n");
1145 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1148 if (This->pAllocator)
1149 IMemAllocator_Release(This->pAllocator);
1150 This->pAllocator = pAllocator;
1151 if (This->pAllocator)
1152 IMemAllocator_AddRef(This->pAllocator);
1157 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1159 BaseInputPin *This = impl_from_IMemInputPin(iface);
1161 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1163 /* override this method if you have any specific requirements */
1168 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1170 BaseInputPin *This = impl_from_IMemInputPin(iface);
1171 HRESULT hr = S_FALSE;
1173 /* this trace commented out for performance reasons */
1174 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1175 if (This->pFuncsTable->pfnReceive)
1176 hr = This->pFuncsTable->pfnReceive(This, pSample);
1180 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1183 BaseInputPin *This = impl_from_IMemInputPin(iface);
1185 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1187 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1189 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1197 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1199 BaseInputPin *This = impl_from_IMemInputPin(iface);
1201 TRACE("(%p/%p)->()\n", This, iface);
1206 static const IMemInputPinVtbl MemInputPin_Vtbl =
1208 MemInputPin_QueryInterface,
1210 MemInputPin_Release,
1211 MemInputPin_GetAllocator,
1212 MemInputPin_NotifyAllocator,
1213 MemInputPin_GetAllocatorRequirements,
1214 MemInputPin_Receive,
1215 MemInputPin_ReceiveMultiple,
1216 MemInputPin_ReceiveCanBlock
1219 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1220 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1221 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1225 /* Common attributes */
1226 pPinImpl->pin.refCount = 1;
1227 pPinImpl->pin.pConnectedTo = NULL;
1228 pPinImpl->pin.pCritSec = pCritSec;
1229 pPinImpl->pin.tStart = 0;
1230 pPinImpl->pin.tStop = 0;
1231 pPinImpl->pin.dRate = 1.0;
1232 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1233 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1234 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
1236 /* Input pin attributes */
1237 pPinImpl->pFuncsTable = pBaseInputFuncsTable;
1238 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1239 if (pPinImpl->preferred_allocator)
1240 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1241 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1242 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1243 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1248 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1249 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1250 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1252 BaseInputPin * pPinImpl;
1256 assert(pBaseFuncsTable->pfnCheckMediaType);
1258 if (pPinInfo->dir != PINDIR_INPUT)
1260 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1261 return E_INVALIDARG;
1264 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1267 return E_OUTOFMEMORY;
1269 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseInputFuncsTable, pCritSec, allocator, pPinImpl)))
1271 *ppPin = (IPin *)pPinImpl;
1275 CoTaskMemFree(pPinImpl);