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))
364 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
369 IUnknown_AddRef((IUnknown *)(*ppv));
373 FIXME("No interface for %s!\n", debugstr_guid(riid));
375 return E_NOINTERFACE;
378 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
380 BaseOutputPin *This = (BaseOutputPin *)iface;
381 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
383 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
387 FreeMediaType(&This->pin.mtCurrent);
394 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
397 BaseOutputPin *This = (BaseOutputPin *)iface;
399 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
400 dump_AM_MEDIA_TYPE(pmt);
402 /* If we try to connect to ourself, we will definitely deadlock.
403 * There are other cases where we could deadlock too, but this
404 * catches the obvious case */
405 assert(pReceivePin != iface);
407 EnterCriticalSection(This->pin.pCritSec);
409 /* if we have been a specific type to connect with, then we can either connect
410 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
411 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
412 hr = This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmt);
415 /* negotiate media type */
417 IEnumMediaTypes * pEnumCandidates;
418 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
420 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
422 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
424 /* try this filter's media types first */
425 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
427 assert(pmtCandidate);
428 dump_AM_MEDIA_TYPE(pmtCandidate);
429 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
430 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
431 assert(pmtCandidate->pbFormat);
432 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
433 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
436 DeleteMediaType(pmtCandidate);
439 DeleteMediaType(pmtCandidate);
442 IEnumMediaTypes_Release(pEnumCandidates);
445 /* then try receiver filter's media types */
446 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
448 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
450 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
452 assert(pmtCandidate);
453 dump_AM_MEDIA_TYPE(pmtCandidate);
454 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
455 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
458 DeleteMediaType(pmtCandidate);
461 DeleteMediaType(pmtCandidate);
464 IEnumMediaTypes_Release(pEnumCandidates);
466 } /* if negotiate media type */
468 LeaveCriticalSection(This->pin.pCritSec);
470 TRACE(" -- %x\n", hr);
474 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
476 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
481 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
484 BaseOutputPin *This = (BaseOutputPin *)iface;
488 EnterCriticalSection(This->pin.pCritSec);
490 if (This->pMemInputPin)
492 IMemInputPin_Release(This->pMemInputPin);
493 This->pMemInputPin = NULL;
495 if (This->pin.pConnectedTo)
497 IPin_Release(This->pin.pConnectedTo);
498 This->pin.pConnectedTo = NULL;
499 FreeMediaType(&This->pin.mtCurrent);
500 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
506 LeaveCriticalSection(This->pin.pCritSec);
511 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
515 /* not supposed to do anything in an output pin */
520 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
522 TRACE("(%p)->()\n", iface);
524 /* not supposed to do anything in an output pin */
529 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
531 TRACE("(%p)->()\n", iface);
533 /* not supposed to do anything in an output pin */
538 static const IPinVtbl OutputPin_Vtbl =
540 BaseOutputPinImpl_QueryInterface,
542 BaseOutputPinImpl_Release,
543 BaseOutputPinImpl_Connect,
544 BaseOutputPinImpl_ReceiveConnection,
545 BaseOutputPinImpl_Disconnect,
546 BasePinImpl_ConnectedTo,
547 BasePinImpl_ConnectionMediaType,
548 BasePinImpl_QueryPinInfo,
549 BasePinImpl_QueryDirection,
551 BasePinImpl_QueryAccept,
552 BasePinImpl_EnumMediaTypes,
553 BasePinImpl_QueryInternalConnections,
554 BaseOutputPinImpl_EndOfStream,
555 BaseOutputPinImpl_BeginFlush,
556 BaseOutputPinImpl_EndFlush,
557 BasePinImpl_NewSegment
560 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
564 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
566 EnterCriticalSection(This->pin.pCritSec);
568 if (!This->pin.pConnectedTo)
569 hr = VFW_E_NOT_CONNECTED;
572 IMemAllocator * pAlloc = NULL;
574 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
577 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
580 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
583 IMemAllocator_Release(pAlloc);
586 LeaveCriticalSection(This->pin.pCritSec);
591 /* replaces OutputPin_SendSample */
592 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
595 IMemInputPin * pMemConnected = NULL;
598 EnterCriticalSection(This->pin.pCritSec);
600 if (!This->pin.pConnectedTo || !This->pMemInputPin)
601 hr = VFW_E_NOT_CONNECTED;
604 /* we don't have the lock held when using This->pMemInputPin,
605 * so we need to AddRef it to stop it being deleted while we are
606 * using it. Same with its filter. */
607 pMemConnected = This->pMemInputPin;
608 IMemInputPin_AddRef(pMemConnected);
609 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
612 LeaveCriticalSection(This->pin.pCritSec);
616 /* NOTE: if we are in a critical section when Receive is called
617 * then it causes some problems (most notably with the native Video
618 * Renderer) if we are re-entered for whatever reason */
619 hr = IMemInputPin_Receive(pMemConnected, pSample);
621 /* If the filter's destroyed, tell upstream to stop sending data */
622 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
626 IMemInputPin_Release(pMemConnected);
631 /* replaces OutputPin_CommitAllocator */
632 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
636 TRACE("(%p)->()\n", This);
638 EnterCriticalSection(This->pin.pCritSec);
640 if (!This->pin.pConnectedTo || !This->pMemInputPin)
641 hr = VFW_E_NOT_CONNECTED;
644 IMemAllocator * pAlloc = NULL;
646 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
649 hr = IMemAllocator_Commit(pAlloc);
652 IMemAllocator_Release(pAlloc);
655 LeaveCriticalSection(This->pin.pCritSec);
657 TRACE("--> %08x\n", hr);
661 /* replaces OutputPin_DecommitAllocator */
662 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
666 TRACE("(%p)->()\n", This);
668 EnterCriticalSection(This->pin.pCritSec);
670 if (!This->pin.pConnectedTo || !This->pMemInputPin)
671 hr = VFW_E_NOT_CONNECTED;
674 IMemAllocator * pAlloc = NULL;
676 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
679 hr = IMemAllocator_Decommit(pAlloc);
682 IMemAllocator_Release(pAlloc);
685 LeaveCriticalSection(This->pin.pCritSec);
687 TRACE("--> %08x\n", hr);
691 /* replaces OutputPin_DeliverDisconnect */
692 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
696 TRACE("(%p)->()\n", This);
698 EnterCriticalSection(This->pin.pCritSec);
700 if (!This->pin.pConnectedTo || !This->pMemInputPin)
701 hr = VFW_E_NOT_CONNECTED;
704 IMemAllocator * pAlloc = NULL;
706 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
709 hr = IMemAllocator_Decommit(pAlloc);
712 IMemAllocator_Release(pAlloc);
715 hr = IPin_Disconnect(This->pin.pConnectedTo);
717 IPin_Disconnect((IPin *)This);
719 LeaveCriticalSection(This->pin.pCritSec);
724 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
726 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
729 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
733 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
735 if (hr == VFW_E_NO_ALLOCATOR)
736 /* Input pin provides no allocator, use standard memory allocator */
737 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
741 ALLOCATOR_PROPERTIES rProps;
742 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
744 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
745 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
749 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
754 /*** The Construct functions ***/
756 /* Function called as a helper to IPin_Connect */
757 /* specific AM_MEDIA_TYPE - it cannot be NULL */
758 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
760 BaseOutputPin *This = (BaseOutputPin *)iface;
762 IMemAllocator * pMemAlloc = NULL;
764 TRACE("(%p, %p)\n", pReceivePin, pmt);
765 dump_AM_MEDIA_TYPE(pmt);
767 /* FIXME: call queryacceptproc */
769 This->pin.pConnectedTo = pReceivePin;
770 IPin_AddRef(pReceivePin);
771 CopyMediaType(&This->pin.mtCurrent, pmt);
773 hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
775 /* get the IMemInputPin interface we will use to deliver samples to the
779 This->pMemInputPin = NULL;
780 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
784 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
786 IMemAllocator_Release(pMemAlloc);
789 /* break connection if we couldn't get the allocator */
792 if (This->pMemInputPin)
793 IMemInputPin_Release(This->pMemInputPin);
794 This->pMemInputPin = NULL;
796 IPin_Disconnect(pReceivePin);
802 IPin_Release(This->pin.pConnectedTo);
803 This->pin.pConnectedTo = NULL;
804 FreeMediaType(&This->pin.mtCurrent);
807 TRACE(" -- %x\n", hr);
811 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
815 /* Common attributes */
816 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
817 pPinImpl->pin.refCount = 1;
818 pPinImpl->pin.pConnectedTo = NULL;
819 pPinImpl->pin.pCritSec = pCritSec;
820 pPinImpl->pin.tStart = 0;
821 pPinImpl->pin.tStop = 0;
822 pPinImpl->pin.dRate = 1.0;
823 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
824 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
825 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
827 /* Output pin attributes */
828 pPinImpl->pMemInputPin = NULL;
829 pPinImpl->pFuncsTable = pBaseOutputFuncsTable;
834 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)
836 BaseOutputPin * pPinImpl;
840 if (pPinInfo->dir != PINDIR_OUTPUT)
842 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
846 assert(outputpin_size >= sizeof(BaseOutputPin));
847 assert(pBaseFuncsTable->pfnAttemptConnection);
849 pPinImpl = CoTaskMemAlloc(outputpin_size);
852 return E_OUTOFMEMORY;
854 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseOutputFuncsTable, pCritSec, pPinImpl)))
856 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
860 CoTaskMemFree(pPinImpl);
864 /*** Input Pin implementation ***/
866 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
868 BaseInputPin *This = (BaseInputPin *)iface;
870 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
874 if (IsEqualIID(riid, &IID_IUnknown))
876 else if (IsEqualIID(riid, &IID_IPin))
878 else if (IsEqualIID(riid, &IID_IMemInputPin))
879 *ppv = &This->lpVtblMemInput;
880 else if (IsEqualIID(riid, &IID_IMediaSeeking))
882 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
887 IUnknown_AddRef((IUnknown *)(*ppv));
891 FIXME("No interface for %s!\n", debugstr_guid(riid));
893 return E_NOINTERFACE;
896 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
898 BaseInputPin *This = (BaseInputPin *)iface;
899 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
901 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
905 FreeMediaType(&This->pin.mtCurrent);
906 if (This->pAllocator)
907 IMemAllocator_Release(This->pAllocator);
908 This->pAllocator = NULL;
909 This->pin.lpVtbl = NULL;
917 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
919 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
925 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
927 BaseInputPin *This = (BaseInputPin *)iface;
928 PIN_DIRECTION pindirReceive;
931 TRACE("(%p, %p)\n", pReceivePin, pmt);
932 dump_AM_MEDIA_TYPE(pmt);
934 EnterCriticalSection(This->pin.pCritSec);
936 if (This->pin.pConnectedTo)
937 hr = VFW_E_ALREADY_CONNECTED;
939 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
940 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
941 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
945 IPin_QueryDirection(pReceivePin, &pindirReceive);
947 if (pindirReceive != PINDIR_OUTPUT)
949 ERR("Can't connect from non-output pin\n");
950 hr = VFW_E_INVALID_DIRECTION;
956 CopyMediaType(&This->pin.mtCurrent, pmt);
957 This->pin.pConnectedTo = pReceivePin;
958 IPin_AddRef(pReceivePin);
961 LeaveCriticalSection(This->pin.pCritSec);
966 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
968 return IPin_EndOfStream( pin );
971 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
973 BaseInputPin *This = (BaseInputPin *)iface;
975 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
977 return (This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) == S_OK ? S_OK : S_FALSE);
980 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
983 BaseInputPin *This = (BaseInputPin *)iface;
985 TRACE("(%p)\n", This);
987 EnterCriticalSection(This->pin.pCritSec);
991 This->end_of_stream = 1;
992 LeaveCriticalSection(This->pin.pCritSec);
995 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
999 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1001 return IPin_BeginFlush( pin );
1004 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1006 BaseInputPin *This = (BaseInputPin *)iface;
1008 TRACE("() semi-stub\n");
1010 EnterCriticalSection(This->pin.pCritSec);
1013 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1014 LeaveCriticalSection(This->pin.pCritSec);
1019 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1021 return IPin_EndFlush( pin );
1024 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1026 BaseInputPin *This = (BaseInputPin *)iface;
1028 TRACE("(%p)\n", This);
1030 EnterCriticalSection(This->pin.pCritSec);
1031 This->flushing = This->end_of_stream = 0;
1033 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1034 LeaveCriticalSection(This->pin.pCritSec);
1039 typedef struct newsegmentargs
1041 REFERENCE_TIME tStart, tStop;
1045 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1047 newsegmentargs *args = data;
1048 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1051 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1053 BaseInputPin *This = (BaseInputPin *)iface;
1054 newsegmentargs args;
1056 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1058 args.tStart = This->pin.tStart = tStart;
1059 args.tStop = This->pin.tStop = tStop;
1060 args.rate = This->pin.dRate = dRate;
1062 return SendFurther( iface, deliver_newsegment, &args, NULL );
1065 static const IPinVtbl InputPin_Vtbl =
1067 BaseInputPinImpl_QueryInterface,
1069 BaseInputPinImpl_Release,
1070 BaseInputPinImpl_Connect,
1071 BaseInputPinImpl_ReceiveConnection,
1072 BasePinImpl_Disconnect,
1073 BasePinImpl_ConnectedTo,
1074 BasePinImpl_ConnectionMediaType,
1075 BasePinImpl_QueryPinInfo,
1076 BasePinImpl_QueryDirection,
1077 BasePinImpl_QueryId,
1078 BaseInputPinImpl_QueryAccept,
1079 BasePinImpl_EnumMediaTypes,
1080 BasePinImpl_QueryInternalConnections,
1081 BaseInputPinImpl_EndOfStream,
1082 BaseInputPinImpl_BeginFlush,
1083 BaseInputPinImpl_EndFlush,
1084 BaseInputPinImpl_NewSegment
1087 /*** IMemInputPin implementation ***/
1089 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1091 return (BaseInputPin *)((char*)iface - FIELD_OFFSET(BaseInputPin, lpVtblMemInput));
1094 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1096 BaseInputPin *This = impl_from_IMemInputPin(iface);
1098 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
1101 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1103 BaseInputPin *This = impl_from_IMemInputPin(iface);
1105 return IPin_AddRef((IPin *)&This->pin);
1108 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1110 BaseInputPin *This = impl_from_IMemInputPin(iface);
1112 return IPin_Release((IPin *)&This->pin);
1115 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1117 BaseInputPin *This = impl_from_IMemInputPin(iface);
1119 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1121 *ppAllocator = This->pAllocator;
1123 IMemAllocator_AddRef(*ppAllocator);
1125 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1128 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1130 BaseInputPin *This = impl_from_IMemInputPin(iface);
1132 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1135 FIXME("Read only flag not handled yet!\n");
1137 /* FIXME: Should we release the allocator on disconnection? */
1140 WARN("Null allocator\n");
1144 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1147 if (This->pAllocator)
1148 IMemAllocator_Release(This->pAllocator);
1149 This->pAllocator = pAllocator;
1150 if (This->pAllocator)
1151 IMemAllocator_AddRef(This->pAllocator);
1156 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1158 BaseInputPin *This = impl_from_IMemInputPin(iface);
1160 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1162 /* override this method if you have any specific requirements */
1167 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1169 BaseInputPin *This = impl_from_IMemInputPin(iface);
1170 HRESULT hr = S_FALSE;
1172 /* this trace commented out for performance reasons */
1173 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1174 if (This->pFuncsTable->pfnReceive)
1175 hr = This->pFuncsTable->pfnReceive(This, pSample);
1179 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1182 BaseInputPin *This = impl_from_IMemInputPin(iface);
1184 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1186 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1188 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1196 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1198 BaseInputPin *This = impl_from_IMemInputPin(iface);
1200 TRACE("(%p/%p)->()\n", This, iface);
1205 static const IMemInputPinVtbl MemInputPin_Vtbl =
1207 MemInputPin_QueryInterface,
1209 MemInputPin_Release,
1210 MemInputPin_GetAllocator,
1211 MemInputPin_NotifyAllocator,
1212 MemInputPin_GetAllocatorRequirements,
1213 MemInputPin_Receive,
1214 MemInputPin_ReceiveMultiple,
1215 MemInputPin_ReceiveCanBlock
1218 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1219 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1220 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1224 /* Common attributes */
1225 pPinImpl->pin.refCount = 1;
1226 pPinImpl->pin.pConnectedTo = NULL;
1227 pPinImpl->pin.pCritSec = pCritSec;
1228 pPinImpl->pin.tStart = 0;
1229 pPinImpl->pin.tStop = 0;
1230 pPinImpl->pin.dRate = 1.0;
1231 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1232 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1233 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
1235 /* Input pin attributes */
1236 pPinImpl->pFuncsTable = pBaseInputFuncsTable;
1237 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1238 if (pPinImpl->preferred_allocator)
1239 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1240 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1241 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1242 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1247 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1248 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1249 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1251 BaseInputPin * pPinImpl;
1255 assert(pBaseFuncsTable->pfnCheckMediaType);
1257 if (pPinInfo->dir != PINDIR_INPUT)
1259 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1260 return E_INVALIDARG;
1263 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1266 return E_OUTOFMEMORY;
1268 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseInputFuncsTable, pCritSec, allocator, pPinImpl)))
1270 *ppPin = (IPin *)pPinImpl;
1274 CoTaskMemFree(pPinImpl);