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 OutputPin_Vtbl;
36 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
38 /* Tempting to just do a memcpy, but the name field is
39 128 characters long! We will probably never exceed 10
40 most of the time, so we are better off copying
41 each field manually */
42 strcpyW(pDest->achName, pSrc->achName);
43 pDest->dir = pSrc->dir;
44 pDest->pFilter = pSrc->pFilter;
47 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
51 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
54 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
57 dump_AM_MEDIA_TYPE(pmt1);
59 dump_AM_MEDIA_TYPE(pmt2);
60 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
61 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
64 /*** Common Base Pin function */
65 HRESULT WINAPI BasePinImpl_GetMediaType(IPin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
69 return VFW_S_NO_MORE_ITEMS;
72 LONG WINAPI BasePinImpl_GetMediaTypeVersion(IPin *iface)
77 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
79 BasePin *This = (BasePin *)iface;
80 ULONG refCount = InterlockedIncrement(&This->refCount);
82 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
87 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
90 BasePin *This = (BasePin *)iface;
94 EnterCriticalSection(This->pCritSec);
96 if (This->pConnectedTo)
98 IPin_Release(This->pConnectedTo);
99 This->pConnectedTo = NULL;
100 FreeMediaType(&This->mtCurrent);
101 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
107 LeaveCriticalSection(This->pCritSec);
112 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
115 BasePin *This = (BasePin *)iface;
117 TRACE("(%p)\n", ppPin);
119 EnterCriticalSection(This->pCritSec);
121 if (This->pConnectedTo)
123 *ppPin = This->pConnectedTo;
129 hr = VFW_E_NOT_CONNECTED;
133 LeaveCriticalSection(This->pCritSec);
138 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
141 BasePin *This = (BasePin *)iface;
143 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
145 EnterCriticalSection(This->pCritSec);
147 if (This->pConnectedTo)
149 CopyMediaType(pmt, &This->mtCurrent);
154 ZeroMemory(pmt, sizeof(*pmt));
155 hr = VFW_E_NOT_CONNECTED;
158 LeaveCriticalSection(This->pCritSec);
163 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
165 BasePin *This = (BasePin *)iface;
167 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
169 Copy_PinInfo(pInfo, &This->pinInfo);
170 IBaseFilter_AddRef(pInfo->pFilter);
175 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
177 BasePin *This = (BasePin *)iface;
179 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
181 *pPinDir = This->pinInfo.dir;
186 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
188 BasePin *This = (BasePin *)iface;
190 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
192 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
194 return E_OUTOFMEMORY;
196 strcpyW(*Id, This->pinInfo.achName);
201 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
203 TRACE("(%p)->(%p)\n", iface, pmt);
208 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
210 BasePin *This = (BasePin *)iface;
212 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
214 /* override this method to allow enumeration of your types */
216 return EnumMediaTypes_Construct(iface, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion , ppEnum);
219 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
221 BasePin *This = (BasePin *)iface;
223 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
225 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
228 /*** OutputPin implementation ***/
230 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
232 BaseOutputPin *This = (BaseOutputPin *)iface;
234 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
238 if (IsEqualIID(riid, &IID_IUnknown))
240 else if (IsEqualIID(riid, &IID_IPin))
242 else if (IsEqualIID(riid, &IID_IMediaSeeking))
244 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
249 IUnknown_AddRef((IUnknown *)(*ppv));
253 FIXME("No interface for %s!\n", debugstr_guid(riid));
255 return E_NOINTERFACE;
258 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
260 BaseOutputPin *This = (BaseOutputPin *)iface;
261 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
263 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
267 FreeMediaType(&This->pin.mtCurrent);
274 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
277 BaseOutputPin *This = (BaseOutputPin *)iface;
279 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
280 dump_AM_MEDIA_TYPE(pmt);
282 /* If we try to connect to ourself, we will definitely deadlock.
283 * There are other cases where we could deadlock too, but this
284 * catches the obvious case */
285 assert(pReceivePin != iface);
287 EnterCriticalSection(This->pin.pCritSec);
289 /* if we have been a specific type to connect with, then we can either connect
290 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
291 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
292 hr = This->pAttemptConnection(iface, pReceivePin, pmt);
295 /* negotiate media type */
297 IEnumMediaTypes * pEnumCandidates;
298 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
300 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
302 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
304 /* try this filter's media types first */
305 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
307 assert(pmtCandidate);
308 dump_AM_MEDIA_TYPE(pmtCandidate);
309 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
310 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
311 assert(pmtCandidate->pbFormat);
312 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
313 (This->pAttemptConnection(iface, pReceivePin, pmtCandidate) == S_OK))
316 DeleteMediaType(pmtCandidate);
319 DeleteMediaType(pmtCandidate);
322 IEnumMediaTypes_Release(pEnumCandidates);
325 /* then try receiver filter's media types */
326 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
328 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
330 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
332 assert(pmtCandidate);
333 dump_AM_MEDIA_TYPE(pmtCandidate);
334 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
335 (This->pAttemptConnection(iface, pReceivePin, pmtCandidate) == S_OK))
338 DeleteMediaType(pmtCandidate);
341 DeleteMediaType(pmtCandidate);
344 IEnumMediaTypes_Release(pEnumCandidates);
346 } /* if negotiate media type */
348 LeaveCriticalSection(This->pin.pCritSec);
350 TRACE(" -- %x\n", hr);
354 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
356 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
361 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
364 BaseOutputPin *This = (BaseOutputPin *)iface;
368 EnterCriticalSection(This->pin.pCritSec);
370 if (This->pMemInputPin)
372 IMemInputPin_Release(This->pMemInputPin);
373 This->pMemInputPin = NULL;
375 if (This->pin.pConnectedTo)
377 IPin_Release(This->pin.pConnectedTo);
378 This->pin.pConnectedTo = NULL;
379 FreeMediaType(&This->pin.mtCurrent);
380 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
386 LeaveCriticalSection(This->pin.pCritSec);
391 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
395 /* not supposed to do anything in an output pin */
400 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
402 TRACE("(%p)->()\n", iface);
404 /* not supposed to do anything in an output pin */
409 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
411 TRACE("(%p)->()\n", iface);
413 /* not supposed to do anything in an output pin */
418 HRESULT WINAPI BaseOutputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
420 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
422 /* not supposed to do anything in an output pin */
427 static const IPinVtbl OutputPin_Vtbl =
429 BaseOutputPinImpl_QueryInterface,
431 BaseOutputPinImpl_Release,
432 BaseOutputPinImpl_Connect,
433 BaseOutputPinImpl_ReceiveConnection,
434 BaseOutputPinImpl_Disconnect,
435 BasePinImpl_ConnectedTo,
436 BasePinImpl_ConnectionMediaType,
437 BasePinImpl_QueryPinInfo,
438 BasePinImpl_QueryDirection,
440 BasePinImpl_QueryAccept,
441 BasePinImpl_EnumMediaTypes,
442 BasePinImpl_QueryInternalConnections,
443 BaseOutputPinImpl_EndOfStream,
444 BaseOutputPinImpl_BeginFlush,
445 BaseOutputPinImpl_EndFlush,
446 BaseOutputPinImpl_NewSegment
449 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
453 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
455 EnterCriticalSection(This->pin.pCritSec);
457 if (!This->pin.pConnectedTo)
458 hr = VFW_E_NOT_CONNECTED;
461 IMemAllocator * pAlloc = NULL;
463 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
466 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
469 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
472 IMemAllocator_Release(pAlloc);
475 LeaveCriticalSection(This->pin.pCritSec);
480 /* replaces OutputPin_SendSample */
481 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
484 IMemInputPin * pMemConnected = NULL;
487 EnterCriticalSection(This->pin.pCritSec);
489 if (!This->pin.pConnectedTo || !This->pMemInputPin)
490 hr = VFW_E_NOT_CONNECTED;
493 /* we don't have the lock held when using This->pMemInputPin,
494 * so we need to AddRef it to stop it being deleted while we are
495 * using it. Same with its filter. */
496 pMemConnected = This->pMemInputPin;
497 IMemInputPin_AddRef(pMemConnected);
498 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
501 LeaveCriticalSection(This->pin.pCritSec);
505 /* NOTE: if we are in a critical section when Receive is called
506 * then it causes some problems (most notably with the native Video
507 * Renderer) if we are re-entered for whatever reason */
508 hr = IMemInputPin_Receive(pMemConnected, pSample);
510 /* If the filter's destroyed, tell upstream to stop sending data */
511 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
515 IMemInputPin_Release(pMemConnected);
520 /* replaces OutputPin_CommitAllocator */
521 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
525 TRACE("(%p)->()\n", This);
527 EnterCriticalSection(This->pin.pCritSec);
529 if (!This->pin.pConnectedTo || !This->pMemInputPin)
530 hr = VFW_E_NOT_CONNECTED;
533 IMemAllocator * pAlloc = NULL;
535 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
538 hr = IMemAllocator_Commit(pAlloc);
541 IMemAllocator_Release(pAlloc);
544 LeaveCriticalSection(This->pin.pCritSec);
546 TRACE("--> %08x\n", hr);
550 /* replaces OutputPin_DecommitAllocator */
551 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
555 TRACE("(%p)->()\n", This);
557 EnterCriticalSection(This->pin.pCritSec);
559 if (!This->pin.pConnectedTo || !This->pMemInputPin)
560 hr = VFW_E_NOT_CONNECTED;
563 IMemAllocator * pAlloc = NULL;
565 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
568 hr = IMemAllocator_Decommit(pAlloc);
571 IMemAllocator_Release(pAlloc);
574 LeaveCriticalSection(This->pin.pCritSec);
576 TRACE("--> %08x\n", hr);
580 /* replaces OutputPin_DeliverDisconnect */
581 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
585 TRACE("(%p)->()\n", This);
587 EnterCriticalSection(This->pin.pCritSec);
589 if (!This->pin.pConnectedTo || !This->pMemInputPin)
590 hr = VFW_E_NOT_CONNECTED;
591 else if (!This->custom_allocator)
593 IMemAllocator * pAlloc = NULL;
595 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
598 hr = IMemAllocator_Decommit(pAlloc);
601 IMemAllocator_Release(pAlloc);
604 hr = IPin_Disconnect(This->pin.pConnectedTo);
606 else /* Kill the allocator! */
608 hr = IPin_Disconnect(This->pin.pConnectedTo);
610 IPin_Disconnect((IPin *)This);
612 LeaveCriticalSection(This->pin.pCritSec);
617 /*** The Construct functions ***/
619 /* Function called as a helper to IPin_Connect */
620 /* specific AM_MEDIA_TYPE - it cannot be NULL */
621 static HRESULT WINAPI OutputPin_AttemptConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
623 BaseOutputPin *This = (BaseOutputPin *)iface;
625 IMemAllocator * pMemAlloc = NULL;
626 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
628 TRACE("(%p, %p)\n", pReceivePin, pmt);
629 dump_AM_MEDIA_TYPE(pmt);
631 /* FIXME: call queryacceptproc */
633 This->pin.pConnectedTo = pReceivePin;
634 IPin_AddRef(pReceivePin);
635 CopyMediaType(&This->pin.mtCurrent, pmt);
637 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
639 /* get the IMemInputPin interface we will use to deliver samples to the
643 This->pMemInputPin = NULL;
644 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
646 if (SUCCEEDED(hr) && !This->custom_allocator)
648 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
650 if (hr == VFW_E_NO_ALLOCATOR)
651 /* Input pin provides no allocator, use standard memory allocator */
652 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
655 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
658 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
661 IMemAllocator_Release(pMemAlloc);
663 else if (SUCCEEDED(hr))
667 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
670 hr = VFW_E_NO_ALLOCATOR;
673 /* break connection if we couldn't get the allocator */
676 if (This->pMemInputPin)
677 IMemInputPin_Release(This->pMemInputPin);
678 This->pMemInputPin = NULL;
680 IPin_Disconnect(pReceivePin);
686 IPin_Release(This->pin.pConnectedTo);
687 This->pin.pConnectedTo = NULL;
688 FreeMediaType(&This->pin.mtCurrent);
691 TRACE(" -- %x\n", hr);
695 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO *
696 pPinInfo, const ALLOCATOR_PROPERTIES * props, BasePin_AttemptConnection pConnectProc, LPCRITICAL_SECTION pCritSec,
697 BaseOutputPin * pPinImpl)
701 /* Common attributes */
702 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
703 pPinImpl->pin.refCount = 1;
704 pPinImpl->pin.pConnectedTo = NULL;
705 pPinImpl->pin.pCritSec = pCritSec;
706 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
707 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
709 /* Output pin attributes */
710 pPinImpl->pMemInputPin = NULL;
712 pPinImpl->pAttemptConnection = pConnectProc;
714 pPinImpl->pAttemptConnection = OutputPin_AttemptConnection;
715 /* If custom_allocator is set, you will need to specify an allocator
716 * in the alloc member of the struct before an output pin can connect
718 pPinImpl->custom_allocator = 0;
719 pPinImpl->alloc = NULL;
720 pPinImpl->readonly = FALSE;
723 pPinImpl->allocProps = *props;
724 if (pPinImpl->allocProps.cbAlign == 0)
725 pPinImpl->allocProps.cbAlign = 1;
728 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
733 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, BasePin_AttemptConnection pConnectProc, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
735 BaseOutputPin * pPinImpl;
739 if (pPinInfo->dir != PINDIR_OUTPUT)
741 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
745 assert(outputpin_size >= sizeof(BaseOutputPin));
747 pPinImpl = CoTaskMemAlloc(outputpin_size);
750 return E_OUTOFMEMORY;
752 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pConnectProc, pCritSec, pPinImpl)))
754 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
758 CoTaskMemFree(pPinImpl);