mscoree: Implement CorBindToCurrentRuntime.
[wine/multimedia.git] / dlls / strmbase / pin.c
blob958fbf8f6558e8eef3b386e448992df76d6d1861
1 /*
2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include "dshow.h"
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27 #include "wine/strmbase.h"
28 #include "uuids.h"
29 #include "vfwmsgs.h"
30 #include <assert.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)
54 return original;
56 if (FAILED( new ) || original == S_OK)
57 return new;
59 return original;
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 )
71 PIN_INFO pin_info;
72 ULONG amount = 0;
73 HRESULT hr = S_OK;
74 HRESULT hr_return = S_OK;
75 IEnumPins *enumpins = NULL;
76 BOOL foundend = TRUE;
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");
85 pin_info.pFilter = NULL;
86 hr = IPin_QueryPinInfo( from, &pin_info );
87 if (FAILED(hr))
88 goto out;
90 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
91 if (FAILED(hr))
92 goto out;
94 hr = IEnumPins_Reset( enumpins );
95 while (hr == S_OK) {
96 IPin *pin = NULL;
97 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
98 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
100 hr = IEnumPins_Reset( enumpins );
101 continue;
103 if (pin)
105 PIN_DIRECTION dir;
107 IPin_QueryDirection( pin, &dir );
108 if (dir != from_dir)
110 IPin *connected = NULL;
112 foundend = FALSE;
113 IPin_ConnectedTo( pin, &connected );
114 if (connected)
116 HRESULT hr_local;
118 hr_local = fnMiddle( connected, arg );
119 hr_return = updatehres( hr_return, hr_local );
120 IPin_Release(connected);
123 IPin_Release( pin );
125 else
127 hr = S_OK;
128 break;
132 if (!foundend)
133 hr = hr_return;
134 else if (fnEnd) {
135 HRESULT hr_local;
137 hr_local = fnEnd( from, arg );
138 hr_return = updatehres( hr_return, hr_local );
140 IEnumPins_Release(enumpins);
142 out:
143 if (pin_info.pFilter)
144 IBaseFilter_Release( pin_info.pFilter );
145 return hr;
148 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
150 /* Tempting to just do a memcpy, but the name field is
151 128 characters long! We will probably never exceed 10
152 most of the time, so we are better off copying
153 each field manually */
154 strcpyW(pDest->achName, pSrc->achName);
155 pDest->dir = pSrc->dir;
156 pDest->pFilter = pSrc->pFilter;
159 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
161 if (!pmt)
162 return;
163 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
166 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
168 TRACE("pmt1: ");
169 dump_AM_MEDIA_TYPE(pmt1);
170 TRACE("pmt2: ");
171 dump_AM_MEDIA_TYPE(pmt2);
172 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
173 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
176 /*** Common Base Pin function */
177 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
179 if (iPosition < 0)
180 return E_INVALIDARG;
181 return VFW_S_NO_MORE_ITEMS;
184 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
186 return 1;
189 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
191 BasePin *This = impl_from_IPin(iface);
192 ULONG refCount = InterlockedIncrement(&This->refCount);
194 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
196 return refCount;
199 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
201 HRESULT hr;
202 BasePin *This = impl_from_IPin(iface);
204 TRACE("()\n");
206 EnterCriticalSection(This->pCritSec);
208 if (This->pConnectedTo)
210 IPin_Release(This->pConnectedTo);
211 This->pConnectedTo = NULL;
212 FreeMediaType(&This->mtCurrent);
213 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
214 hr = S_OK;
216 else
217 hr = S_FALSE;
219 LeaveCriticalSection(This->pCritSec);
221 return hr;
224 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
226 HRESULT hr;
227 BasePin *This = impl_from_IPin(iface);
229 TRACE("(%p)\n", ppPin);
231 EnterCriticalSection(This->pCritSec);
233 if (This->pConnectedTo)
235 *ppPin = This->pConnectedTo;
236 IPin_AddRef(*ppPin);
237 hr = S_OK;
239 else
241 hr = VFW_E_NOT_CONNECTED;
242 *ppPin = NULL;
245 LeaveCriticalSection(This->pCritSec);
247 return hr;
250 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
252 HRESULT hr;
253 BasePin *This = impl_from_IPin(iface);
255 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
257 EnterCriticalSection(This->pCritSec);
259 if (This->pConnectedTo)
261 CopyMediaType(pmt, &This->mtCurrent);
262 hr = S_OK;
264 else
266 ZeroMemory(pmt, sizeof(*pmt));
267 hr = VFW_E_NOT_CONNECTED;
270 LeaveCriticalSection(This->pCritSec);
272 return hr;
275 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
277 BasePin *This = impl_from_IPin(iface);
279 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
281 Copy_PinInfo(pInfo, &This->pinInfo);
282 IBaseFilter_AddRef(pInfo->pFilter);
284 return S_OK;
287 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
289 BasePin *This = impl_from_IPin(iface);
291 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
293 *pPinDir = This->pinInfo.dir;
295 return S_OK;
298 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
300 BasePin *This = impl_from_IPin(iface);
302 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
304 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
305 if (!*Id)
306 return E_OUTOFMEMORY;
308 strcpyW(*Id, This->pinInfo.achName);
310 return S_OK;
313 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
315 TRACE("(%p)->(%p)\n", iface, pmt);
317 return S_OK;
320 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
322 BasePin *This = impl_from_IPin(iface);
324 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
326 /* override this method to allow enumeration of your types */
328 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
331 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
333 BasePin *This = impl_from_IPin(iface);
335 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
337 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
340 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
342 BasePin *This = impl_from_IPin(iface);
344 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
346 This->tStart = tStart;
347 This->tStop = tStop;
348 This->dRate = dRate;
350 return S_OK;
353 /*** OutputPin implementation ***/
355 static inline BaseOutputPin *impl_BaseOutputPin_from_IPin( IPin *iface )
357 return CONTAINING_RECORD(iface, BaseOutputPin, pin.IPin_iface);
360 static inline BaseOutputPin *impl_BaseOutputPin_from_BasePin( BasePin *iface )
362 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
365 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
367 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
369 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
371 *ppv = NULL;
373 if (IsEqualIID(riid, &IID_IUnknown))
374 *ppv = iface;
375 else if (IsEqualIID(riid, &IID_IPin))
376 *ppv = iface;
377 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
378 IsEqualIID(riid, &IID_IQualityControl))
380 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
383 if (*ppv)
385 IUnknown_AddRef((IUnknown *)(*ppv));
386 return S_OK;
389 FIXME("No interface for %s!\n", debugstr_guid(riid));
391 return E_NOINTERFACE;
394 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
396 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
397 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
399 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
401 if (!refCount)
403 BaseOutputPin_Destroy(This);
404 return 0;
406 return refCount;
409 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
411 HRESULT hr;
412 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
414 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
415 dump_AM_MEDIA_TYPE(pmt);
417 if (!pReceivePin)
418 return E_POINTER;
420 /* If we try to connect to ourselves, we will definitely deadlock.
421 * There are other cases where we could deadlock too, but this
422 * catches the obvious case */
423 assert(pReceivePin != iface);
425 EnterCriticalSection(This->pin.pCritSec);
427 /* if we have been a specific type to connect with, then we can either connect
428 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
429 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
430 hr = This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmt);
431 else
433 /* negotiate media type */
435 IEnumMediaTypes * pEnumCandidates;
436 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
438 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
440 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
442 /* try this filter's media types first */
443 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
445 assert(pmtCandidate);
446 dump_AM_MEDIA_TYPE(pmtCandidate);
447 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
448 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
449 assert(pmtCandidate->pbFormat);
450 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
451 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
453 hr = S_OK;
454 DeleteMediaType(pmtCandidate);
455 break;
457 DeleteMediaType(pmtCandidate);
458 pmtCandidate = NULL;
460 IEnumMediaTypes_Release(pEnumCandidates);
463 /* then try receiver filter's media types */
464 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
466 ULONG fetched;
468 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
470 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, &fetched))
472 assert(pmtCandidate);
473 dump_AM_MEDIA_TYPE(pmtCandidate);
474 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
475 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
477 hr = S_OK;
478 DeleteMediaType(pmtCandidate);
479 break;
481 DeleteMediaType(pmtCandidate);
482 pmtCandidate = NULL;
483 } /* while */
484 IEnumMediaTypes_Release(pEnumCandidates);
485 } /* if not found */
486 } /* if negotiate media type */
487 } /* if succeeded */
488 LeaveCriticalSection(This->pin.pCritSec);
490 TRACE(" -- %x\n", hr);
491 return hr;
494 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
496 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
498 return E_UNEXPECTED;
501 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
503 HRESULT hr;
504 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
506 TRACE("()\n");
508 EnterCriticalSection(This->pin.pCritSec);
510 if (This->pMemInputPin)
512 IMemInputPin_Release(This->pMemInputPin);
513 This->pMemInputPin = NULL;
515 if (This->pin.pConnectedTo)
517 IPin_Release(This->pin.pConnectedTo);
518 This->pin.pConnectedTo = NULL;
519 FreeMediaType(&This->pin.mtCurrent);
520 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
521 hr = S_OK;
523 else
524 hr = S_FALSE;
526 LeaveCriticalSection(This->pin.pCritSec);
528 return hr;
531 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
533 TRACE("()\n");
535 /* not supposed to do anything in an output pin */
537 return E_UNEXPECTED;
540 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
542 TRACE("(%p)->()\n", iface);
544 /* not supposed to do anything in an output pin */
546 return E_UNEXPECTED;
549 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
551 TRACE("(%p)->()\n", iface);
553 /* not supposed to do anything in an output pin */
555 return E_UNEXPECTED;
558 static const IPinVtbl OutputPin_Vtbl =
560 BaseOutputPinImpl_QueryInterface,
561 BasePinImpl_AddRef,
562 BaseOutputPinImpl_Release,
563 BaseOutputPinImpl_Connect,
564 BaseOutputPinImpl_ReceiveConnection,
565 BaseOutputPinImpl_Disconnect,
566 BasePinImpl_ConnectedTo,
567 BasePinImpl_ConnectionMediaType,
568 BasePinImpl_QueryPinInfo,
569 BasePinImpl_QueryDirection,
570 BasePinImpl_QueryId,
571 BasePinImpl_QueryAccept,
572 BasePinImpl_EnumMediaTypes,
573 BasePinImpl_QueryInternalConnections,
574 BaseOutputPinImpl_EndOfStream,
575 BaseOutputPinImpl_BeginFlush,
576 BaseOutputPinImpl_EndFlush,
577 BasePinImpl_NewSegment
580 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
582 HRESULT hr;
584 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
586 if (!This->pin.pConnectedTo)
587 hr = VFW_E_NOT_CONNECTED;
588 else
590 hr = IMemAllocator_GetBuffer(This->pAllocator, ppSample, tStart, tStop, dwFlags);
592 if (SUCCEEDED(hr))
593 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
596 return hr;
599 /* replaces OutputPin_SendSample */
600 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
602 HRESULT hr = S_OK;
603 IMemInputPin * pMemConnected = NULL;
604 PIN_INFO pinInfo;
606 EnterCriticalSection(This->pin.pCritSec);
608 if (!This->pin.pConnectedTo || !This->pMemInputPin)
609 hr = VFW_E_NOT_CONNECTED;
610 else
612 /* we don't have the lock held when using This->pMemInputPin,
613 * so we need to AddRef it to stop it being deleted while we are
614 * using it. Same with its filter. */
615 pMemConnected = This->pMemInputPin;
616 IMemInputPin_AddRef(pMemConnected);
617 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
620 LeaveCriticalSection(This->pin.pCritSec);
622 if (SUCCEEDED(hr))
624 /* NOTE: if we are in a critical section when Receive is called
625 * then it causes some problems (most notably with the native Video
626 * Renderer) if we are re-entered for whatever reason */
627 hr = IMemInputPin_Receive(pMemConnected, pSample);
629 /* If the filter's destroyed, tell upstream to stop sending data */
630 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
631 hr = S_FALSE;
633 if (pMemConnected)
634 IMemInputPin_Release(pMemConnected);
636 return hr;
639 /* replaces OutputPin_CommitAllocator */
640 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
642 HRESULT hr = S_OK;
644 TRACE("(%p)->()\n", This);
646 EnterCriticalSection(This->pin.pCritSec);
648 if (!This->pin.pConnectedTo || !This->pMemInputPin)
649 hr = VFW_E_NOT_CONNECTED;
650 else
651 hr = IMemAllocator_Commit(This->pAllocator);
653 LeaveCriticalSection(This->pin.pCritSec);
655 TRACE("--> %08x\n", hr);
656 return hr;
659 /* replaces OutputPin_DecommitAllocator */
660 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
662 HRESULT hr = S_OK;
664 TRACE("(%p)->()\n", This);
666 EnterCriticalSection(This->pin.pCritSec);
668 if (!This->pin.pConnectedTo || !This->pMemInputPin)
669 hr = VFW_E_NOT_CONNECTED;
670 else
671 hr = IMemAllocator_Decommit(This->pAllocator);
673 LeaveCriticalSection(This->pin.pCritSec);
675 TRACE("--> %08x\n", hr);
676 return hr;
679 /* replaces OutputPin_DeliverDisconnect */
680 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
682 HRESULT hr;
684 TRACE("(%p)->()\n", This);
686 EnterCriticalSection(This->pin.pCritSec);
688 if (!This->pin.pConnectedTo || !This->pMemInputPin)
689 hr = VFW_E_NOT_CONNECTED;
690 else
692 hr = IMemAllocator_Decommit(This->pAllocator);
694 if (SUCCEEDED(hr))
695 hr = IPin_Disconnect(This->pin.pConnectedTo);
697 IPin_Disconnect(&This->pin.IPin_iface);
699 LeaveCriticalSection(This->pin.pCritSec);
701 return hr;
704 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
706 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
709 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
711 HRESULT hr;
713 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
715 if (hr == VFW_E_NO_ALLOCATOR)
716 /* Input pin provides no allocator, use standard memory allocator */
717 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
719 if (SUCCEEDED(hr))
721 ALLOCATOR_PROPERTIES rProps;
722 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
724 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
725 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
728 if (SUCCEEDED(hr))
729 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
731 return hr;
734 /*** The Construct functions ***/
736 /* Function called as a helper to IPin_Connect */
737 /* specific AM_MEDIA_TYPE - it cannot be NULL */
738 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
740 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
741 HRESULT hr;
742 IMemAllocator * pMemAlloc = NULL;
744 TRACE("(%p, %p)\n", pReceivePin, pmt);
745 dump_AM_MEDIA_TYPE(pmt);
747 /* FIXME: call queryacceptproc */
749 This->pin.pConnectedTo = pReceivePin;
750 IPin_AddRef(pReceivePin);
751 CopyMediaType(&This->pin.mtCurrent, pmt);
753 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
755 /* get the IMemInputPin interface we will use to deliver samples to the
756 * connected pin */
757 if (SUCCEEDED(hr))
759 This->pMemInputPin = NULL;
760 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
762 if (SUCCEEDED(hr))
764 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
765 if (SUCCEEDED(hr))
766 This->pAllocator = pMemAlloc;
767 else if (pMemAlloc)
768 IMemAllocator_Release(pMemAlloc);
771 /* break connection if we couldn't get the allocator */
772 if (FAILED(hr))
774 if (This->pMemInputPin)
775 IMemInputPin_Release(This->pMemInputPin);
776 This->pMemInputPin = NULL;
778 IPin_Disconnect(pReceivePin);
782 if (FAILED(hr))
784 IPin_Release(This->pin.pConnectedTo);
785 This->pin.pConnectedTo = NULL;
786 FreeMediaType(&This->pin.mtCurrent);
789 TRACE(" -- %x\n", hr);
790 return hr;
793 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
795 TRACE("\n");
797 /* Common attributes */
798 pPinImpl->pin.IPin_iface.lpVtbl = OutputPin_Vtbl;
799 pPinImpl->pin.refCount = 1;
800 pPinImpl->pin.pConnectedTo = NULL;
801 pPinImpl->pin.pCritSec = pCritSec;
802 pPinImpl->pin.tStart = 0;
803 pPinImpl->pin.tStop = 0;
804 pPinImpl->pin.dRate = 1.0;
805 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
806 pPinImpl->pin.pFuncsTable = &vtbl->base;
807 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
809 /* Output pin attributes */
810 pPinImpl->pMemInputPin = NULL;
811 pPinImpl->pAllocator = NULL;
812 pPinImpl->pFuncsTable = vtbl;
814 return S_OK;
817 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
819 BaseOutputPin * pPinImpl;
821 *ppPin = NULL;
823 if (pPinInfo->dir != PINDIR_OUTPUT)
825 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
826 return E_INVALIDARG;
829 assert(outputpin_size >= sizeof(BaseOutputPin));
830 assert(vtbl->base.pfnAttemptConnection);
832 pPinImpl = CoTaskMemAlloc(outputpin_size);
834 if (!pPinImpl)
835 return E_OUTOFMEMORY;
837 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, vtbl, pCritSec, pPinImpl)))
839 *ppPin = &pPinImpl->pin.IPin_iface;
840 return S_OK;
843 CoTaskMemFree(pPinImpl);
844 return E_FAIL;
847 HRESULT WINAPI BaseOutputPin_Destroy(BaseOutputPin *This)
849 FreeMediaType(&This->pin.mtCurrent);
850 if (This->pAllocator)
851 IMemAllocator_Release(This->pAllocator);
852 This->pAllocator = NULL;
853 CoTaskMemFree(This);
854 return S_OK;
857 /*** Input Pin implementation ***/
859 static inline BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
861 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
864 static inline BaseInputPin *impl_BaseInputPin_from_BasePin( BasePin *iface )
866 return CONTAINING_RECORD(iface, BaseInputPin, pin);
869 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
871 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
873 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
875 *ppv = NULL;
877 if (IsEqualIID(riid, &IID_IUnknown))
878 *ppv = iface;
879 else if (IsEqualIID(riid, &IID_IPin))
880 *ppv = iface;
881 else if (IsEqualIID(riid, &IID_IMemInputPin))
882 *ppv = &This->IMemInputPin_iface;
883 else if (IsEqualIID(riid, &IID_IMediaSeeking))
885 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
888 if (*ppv)
890 IUnknown_AddRef((IUnknown *)(*ppv));
891 return S_OK;
894 FIXME("No interface for %s!\n", debugstr_guid(riid));
896 return E_NOINTERFACE;
899 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
901 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
902 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
904 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
906 if (!refCount)
908 BaseInputPin_Destroy(This);
909 return 0;
911 else
912 return refCount;
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);
919 return E_UNEXPECTED;
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;
927 HRESULT hr = S_OK;
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? */
941 if (SUCCEEDED(hr))
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;
952 if (SUCCEEDED(hr))
954 CopyMediaType(&This->pin.mtCurrent, pmt);
955 This->pin.pConnectedTo = pReceivePin;
956 IPin_AddRef(pReceivePin);
959 LeaveCriticalSection(This->pin.pCritSec);
961 return hr;
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)
980 HRESULT hr = S_OK;
981 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
983 TRACE("(%p)\n", This);
985 EnterCriticalSection(This->pin.pCritSec);
986 if (This->flushing)
987 hr = S_FALSE;
988 else
989 This->end_of_stream = TRUE;
990 LeaveCriticalSection(This->pin.pCritSec);
992 if (hr == S_OK)
993 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
994 return hr;
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);
1005 HRESULT hr;
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);
1014 return hr;
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);
1025 HRESULT hr;
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);
1034 return hr;
1037 typedef struct newsegmentargs
1039 REFERENCE_TIME tStart, tStop;
1040 double rate;
1041 } newsegmentargs;
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,
1066 BasePinImpl_AddRef,
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;
1120 if (*ppAllocator)
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);
1132 if (bReadOnly)
1133 FIXME("Read only flag not handled yet!\n");
1135 /* FIXME: Should we release the allocator on disconnection? */
1136 if (!pAllocator)
1138 WARN("Null allocator\n");
1139 return E_POINTER;
1142 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1143 return E_FAIL;
1145 if (This->pAllocator)
1146 IMemAllocator_Release(This->pAllocator);
1147 This->pAllocator = pAllocator;
1148 if (This->pAllocator)
1149 IMemAllocator_AddRef(This->pAllocator);
1151 return S_OK;
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 */
1162 return E_NOTIMPL;
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);
1174 return hr;
1177 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1179 HRESULT hr = S_OK;
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]);
1187 if (hr != S_OK)
1188 break;
1191 return hr;
1194 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1196 BaseInputPin *This = impl_from_IMemInputPin(iface);
1198 TRACE("(%p/%p)->()\n", This, iface);
1200 return S_OK;
1203 static const IMemInputPinVtbl MemInputPin_Vtbl =
1205 MemInputPin_QueryInterface,
1206 MemInputPin_AddRef,
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)
1220 TRACE("\n");
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;
1242 return S_OK;
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;
1251 *ppPin = NULL;
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);
1264 if (!pPinImpl)
1265 return E_OUTOFMEMORY;
1267 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, vtbl, pCritSec, allocator, pPinImpl)))
1269 *ppPin = (IPin *)pPinImpl;
1270 return S_OK;
1273 CoTaskMemFree(pPinImpl);
1274 return E_FAIL;
1277 HRESULT WINAPI BaseInputPin_Destroy(BaseInputPin *This)
1279 FreeMediaType(&This->pin.mtCurrent);
1280 if (This->pAllocator)
1281 IMemAllocator_Release(This->pAllocator);
1282 This->pAllocator = NULL;
1283 This->pin.IPin_iface.lpVtbl = NULL;
1284 CoTaskMemFree(This);
1285 return S_OK;