d3drm: Implement IDirect3DRMFrameX_AddTransform.
[wine/multimedia.git] / dlls / strmbase / pin.c
blob936ae0c9bcbdc45313123e1a6d9bdb91e6775c49
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");
84 hr = S_OK;
86 pin_info.pFilter = NULL;
87 hr = IPin_QueryPinInfo( from, &pin_info );
88 if (FAILED(hr))
89 goto out;
91 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
92 if (FAILED(hr))
93 goto out;
95 hr = IEnumPins_Reset( enumpins );
96 while (hr == S_OK) {
97 IPin *pin = NULL;
98 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
99 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
101 hr = IEnumPins_Reset( enumpins );
102 continue;
104 if (pin)
106 PIN_DIRECTION dir;
108 IPin_QueryDirection( pin, &dir );
109 if (dir != from_dir)
111 IPin *connected = NULL;
113 foundend = FALSE;
114 IPin_ConnectedTo( pin, &connected );
115 if (connected)
117 HRESULT hr_local;
119 hr_local = fnMiddle( connected, arg );
120 hr_return = updatehres( hr_return, hr_local );
121 IPin_Release(connected);
124 IPin_Release( pin );
126 else
128 hr = S_OK;
129 break;
133 if (!foundend)
134 hr = hr_return;
135 else if (fnEnd) {
136 HRESULT hr_local;
138 hr_local = fnEnd( from, arg );
139 hr_return = updatehres( hr_return, hr_local );
141 IEnumPins_Release(enumpins);
143 out:
144 if (pin_info.pFilter)
145 IBaseFilter_Release( pin_info.pFilter );
146 return hr;
149 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
151 /* Tempting to just do a memcpy, but the name field is
152 128 characters long! We will probably never exceed 10
153 most of the time, so we are better off copying
154 each field manually */
155 strcpyW(pDest->achName, pSrc->achName);
156 pDest->dir = pSrc->dir;
157 pDest->pFilter = pSrc->pFilter;
160 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
162 if (!pmt)
163 return;
164 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
167 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
169 TRACE("pmt1: ");
170 dump_AM_MEDIA_TYPE(pmt1);
171 TRACE("pmt2: ");
172 dump_AM_MEDIA_TYPE(pmt2);
173 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
174 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
177 /*** Common Base Pin function */
178 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
180 if (iPosition < 0)
181 return E_INVALIDARG;
182 return VFW_S_NO_MORE_ITEMS;
185 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
187 return 1;
190 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
192 BasePin *This = impl_from_IPin(iface);
193 ULONG refCount = InterlockedIncrement(&This->refCount);
195 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
197 return refCount;
200 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
202 HRESULT hr;
203 BasePin *This = impl_from_IPin(iface);
205 TRACE("()\n");
207 EnterCriticalSection(This->pCritSec);
209 if (This->pConnectedTo)
211 IPin_Release(This->pConnectedTo);
212 This->pConnectedTo = NULL;
213 FreeMediaType(&This->mtCurrent);
214 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
215 hr = S_OK;
217 else
218 hr = S_FALSE;
220 LeaveCriticalSection(This->pCritSec);
222 return hr;
225 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
227 HRESULT hr;
228 BasePin *This = impl_from_IPin(iface);
230 TRACE("(%p)\n", ppPin);
232 EnterCriticalSection(This->pCritSec);
234 if (This->pConnectedTo)
236 *ppPin = This->pConnectedTo;
237 IPin_AddRef(*ppPin);
238 hr = S_OK;
240 else
242 hr = VFW_E_NOT_CONNECTED;
243 *ppPin = NULL;
246 LeaveCriticalSection(This->pCritSec);
248 return hr;
251 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
253 HRESULT hr;
254 BasePin *This = impl_from_IPin(iface);
256 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
258 EnterCriticalSection(This->pCritSec);
260 if (This->pConnectedTo)
262 CopyMediaType(pmt, &This->mtCurrent);
263 hr = S_OK;
265 else
267 ZeroMemory(pmt, sizeof(*pmt));
268 hr = VFW_E_NOT_CONNECTED;
271 LeaveCriticalSection(This->pCritSec);
273 return hr;
276 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
278 BasePin *This = impl_from_IPin(iface);
280 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
282 Copy_PinInfo(pInfo, &This->pinInfo);
283 IBaseFilter_AddRef(pInfo->pFilter);
285 return S_OK;
288 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
290 BasePin *This = impl_from_IPin(iface);
292 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
294 *pPinDir = This->pinInfo.dir;
296 return S_OK;
299 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
301 BasePin *This = impl_from_IPin(iface);
303 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
305 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
306 if (!*Id)
307 return E_OUTOFMEMORY;
309 strcpyW(*Id, This->pinInfo.achName);
311 return S_OK;
314 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
316 TRACE("(%p)->(%p)\n", iface, pmt);
318 return S_OK;
321 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
323 BasePin *This = impl_from_IPin(iface);
325 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
327 /* override this method to allow enumeration of your types */
329 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
332 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
334 BasePin *This = impl_from_IPin(iface);
336 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
338 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
341 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
343 BasePin *This = impl_from_IPin(iface);
345 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
347 This->tStart = tStart;
348 This->tStop = tStop;
349 This->dRate = dRate;
351 return S_OK;
354 /*** OutputPin implementation ***/
356 static inline BaseOutputPin *impl_BaseOutputPin_from_IPin( IPin *iface )
358 return CONTAINING_RECORD(iface, BaseOutputPin, pin.IPin_iface);
361 static inline BaseOutputPin *impl_BaseOutputPin_from_BasePin( BasePin *iface )
363 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
366 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
368 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
370 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
372 *ppv = NULL;
374 if (IsEqualIID(riid, &IID_IUnknown))
375 *ppv = iface;
376 else if (IsEqualIID(riid, &IID_IPin))
377 *ppv = iface;
378 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
379 IsEqualIID(riid, &IID_IQualityControl))
381 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
384 if (*ppv)
386 IUnknown_AddRef((IUnknown *)(*ppv));
387 return S_OK;
390 FIXME("No interface for %s!\n", debugstr_guid(riid));
392 return E_NOINTERFACE;
395 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
397 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
398 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
400 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
402 if (!refCount)
404 FreeMediaType(&This->pin.mtCurrent);
405 CoTaskMemFree(This);
406 return 0;
408 return refCount;
411 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
413 HRESULT hr;
414 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
416 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
417 dump_AM_MEDIA_TYPE(pmt);
419 /* If we try to connect to ourselves, we will definitely deadlock.
420 * There are other cases where we could deadlock too, but this
421 * catches the obvious case */
422 assert(pReceivePin != iface);
424 EnterCriticalSection(This->pin.pCritSec);
426 /* if we have been a specific type to connect with, then we can either connect
427 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
428 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
429 hr = This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmt);
430 else
432 /* negotiate media type */
434 IEnumMediaTypes * pEnumCandidates;
435 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
437 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
439 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
441 /* try this filter's media types first */
442 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
444 assert(pmtCandidate);
445 dump_AM_MEDIA_TYPE(pmtCandidate);
446 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
447 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
448 assert(pmtCandidate->pbFormat);
449 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
450 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
452 hr = S_OK;
453 DeleteMediaType(pmtCandidate);
454 break;
456 DeleteMediaType(pmtCandidate);
457 pmtCandidate = NULL;
459 IEnumMediaTypes_Release(pEnumCandidates);
462 /* then try receiver filter's media types */
463 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
465 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
467 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
469 assert(pmtCandidate);
470 dump_AM_MEDIA_TYPE(pmtCandidate);
471 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
472 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
474 hr = S_OK;
475 DeleteMediaType(pmtCandidate);
476 break;
478 DeleteMediaType(pmtCandidate);
479 pmtCandidate = NULL;
480 } /* while */
481 IEnumMediaTypes_Release(pEnumCandidates);
482 } /* if not found */
483 } /* if negotiate media type */
484 } /* if succeeded */
485 LeaveCriticalSection(This->pin.pCritSec);
487 TRACE(" -- %x\n", hr);
488 return hr;
491 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
493 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
495 return E_UNEXPECTED;
498 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
500 HRESULT hr;
501 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
503 TRACE("()\n");
505 EnterCriticalSection(This->pin.pCritSec);
507 if (This->pMemInputPin)
509 IMemInputPin_Release(This->pMemInputPin);
510 This->pMemInputPin = NULL;
512 if (This->pin.pConnectedTo)
514 IPin_Release(This->pin.pConnectedTo);
515 This->pin.pConnectedTo = NULL;
516 FreeMediaType(&This->pin.mtCurrent);
517 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
518 hr = S_OK;
520 else
521 hr = S_FALSE;
523 LeaveCriticalSection(This->pin.pCritSec);
525 return hr;
528 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
530 TRACE("()\n");
532 /* not supposed to do anything in an output pin */
534 return E_UNEXPECTED;
537 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
539 TRACE("(%p)->()\n", iface);
541 /* not supposed to do anything in an output pin */
543 return E_UNEXPECTED;
546 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
548 TRACE("(%p)->()\n", iface);
550 /* not supposed to do anything in an output pin */
552 return E_UNEXPECTED;
555 static const IPinVtbl OutputPin_Vtbl =
557 BaseOutputPinImpl_QueryInterface,
558 BasePinImpl_AddRef,
559 BaseOutputPinImpl_Release,
560 BaseOutputPinImpl_Connect,
561 BaseOutputPinImpl_ReceiveConnection,
562 BaseOutputPinImpl_Disconnect,
563 BasePinImpl_ConnectedTo,
564 BasePinImpl_ConnectionMediaType,
565 BasePinImpl_QueryPinInfo,
566 BasePinImpl_QueryDirection,
567 BasePinImpl_QueryId,
568 BasePinImpl_QueryAccept,
569 BasePinImpl_EnumMediaTypes,
570 BasePinImpl_QueryInternalConnections,
571 BaseOutputPinImpl_EndOfStream,
572 BaseOutputPinImpl_BeginFlush,
573 BaseOutputPinImpl_EndFlush,
574 BasePinImpl_NewSegment
577 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
579 HRESULT hr;
581 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
583 if (!This->pin.pConnectedTo)
584 hr = VFW_E_NOT_CONNECTED;
585 else
587 IMemAllocator * pAlloc = NULL;
589 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
591 if (SUCCEEDED(hr))
592 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
594 if (SUCCEEDED(hr))
595 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
597 if (pAlloc)
598 IMemAllocator_Release(pAlloc);
601 return hr;
604 /* replaces OutputPin_SendSample */
605 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
607 HRESULT hr = S_OK;
608 IMemInputPin * pMemConnected = NULL;
609 PIN_INFO pinInfo;
611 EnterCriticalSection(This->pin.pCritSec);
613 if (!This->pin.pConnectedTo || !This->pMemInputPin)
614 hr = VFW_E_NOT_CONNECTED;
615 else
617 /* we don't have the lock held when using This->pMemInputPin,
618 * so we need to AddRef it to stop it being deleted while we are
619 * using it. Same with its filter. */
620 pMemConnected = This->pMemInputPin;
621 IMemInputPin_AddRef(pMemConnected);
622 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
625 LeaveCriticalSection(This->pin.pCritSec);
627 if (SUCCEEDED(hr))
629 /* NOTE: if we are in a critical section when Receive is called
630 * then it causes some problems (most notably with the native Video
631 * Renderer) if we are re-entered for whatever reason */
632 hr = IMemInputPin_Receive(pMemConnected, pSample);
634 /* If the filter's destroyed, tell upstream to stop sending data */
635 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
636 hr = S_FALSE;
638 if (pMemConnected)
639 IMemInputPin_Release(pMemConnected);
641 return hr;
644 /* replaces OutputPin_CommitAllocator */
645 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
647 HRESULT hr = S_OK;
649 TRACE("(%p)->()\n", This);
651 EnterCriticalSection(This->pin.pCritSec);
653 if (!This->pin.pConnectedTo || !This->pMemInputPin)
654 hr = VFW_E_NOT_CONNECTED;
655 else
657 IMemAllocator * pAlloc = NULL;
659 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
661 if (SUCCEEDED(hr))
662 hr = IMemAllocator_Commit(pAlloc);
664 if (pAlloc)
665 IMemAllocator_Release(pAlloc);
668 LeaveCriticalSection(This->pin.pCritSec);
670 TRACE("--> %08x\n", hr);
671 return hr;
674 /* replaces OutputPin_DecommitAllocator */
675 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
677 HRESULT hr = S_OK;
679 TRACE("(%p)->()\n", This);
681 EnterCriticalSection(This->pin.pCritSec);
683 if (!This->pin.pConnectedTo || !This->pMemInputPin)
684 hr = VFW_E_NOT_CONNECTED;
685 else
687 IMemAllocator * pAlloc = NULL;
689 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
691 if (SUCCEEDED(hr))
692 hr = IMemAllocator_Decommit(pAlloc);
694 if (pAlloc)
695 IMemAllocator_Release(pAlloc);
698 LeaveCriticalSection(This->pin.pCritSec);
700 TRACE("--> %08x\n", hr);
701 return hr;
704 /* replaces OutputPin_DeliverDisconnect */
705 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
707 HRESULT hr;
709 TRACE("(%p)->()\n", This);
711 EnterCriticalSection(This->pin.pCritSec);
713 if (!This->pin.pConnectedTo || !This->pMemInputPin)
714 hr = VFW_E_NOT_CONNECTED;
715 else
717 IMemAllocator * pAlloc = NULL;
719 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
721 if (SUCCEEDED(hr))
722 hr = IMemAllocator_Decommit(pAlloc);
724 if (pAlloc)
725 IMemAllocator_Release(pAlloc);
727 if (SUCCEEDED(hr))
728 hr = IPin_Disconnect(This->pin.pConnectedTo);
730 IPin_Disconnect(&This->pin.IPin_iface);
732 LeaveCriticalSection(This->pin.pCritSec);
734 return hr;
737 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
739 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
742 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
744 HRESULT hr;
746 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
748 if (hr == VFW_E_NO_ALLOCATOR)
749 /* Input pin provides no allocator, use standard memory allocator */
750 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
752 if (SUCCEEDED(hr))
754 ALLOCATOR_PROPERTIES rProps;
755 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
757 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
758 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
761 if (SUCCEEDED(hr))
762 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
764 return hr;
767 /*** The Construct functions ***/
769 /* Function called as a helper to IPin_Connect */
770 /* specific AM_MEDIA_TYPE - it cannot be NULL */
771 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
773 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
774 HRESULT hr;
775 IMemAllocator * pMemAlloc = NULL;
777 TRACE("(%p, %p)\n", pReceivePin, pmt);
778 dump_AM_MEDIA_TYPE(pmt);
780 /* FIXME: call queryacceptproc */
782 This->pin.pConnectedTo = pReceivePin;
783 IPin_AddRef(pReceivePin);
784 CopyMediaType(&This->pin.mtCurrent, pmt);
786 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
788 /* get the IMemInputPin interface we will use to deliver samples to the
789 * connected pin */
790 if (SUCCEEDED(hr))
792 This->pMemInputPin = NULL;
793 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
795 if (SUCCEEDED(hr))
797 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
798 if (pMemAlloc)
799 IMemAllocator_Release(pMemAlloc);
802 /* break connection if we couldn't get the allocator */
803 if (FAILED(hr))
805 if (This->pMemInputPin)
806 IMemInputPin_Release(This->pMemInputPin);
807 This->pMemInputPin = NULL;
809 IPin_Disconnect(pReceivePin);
813 if (FAILED(hr))
815 IPin_Release(This->pin.pConnectedTo);
816 This->pin.pConnectedTo = NULL;
817 FreeMediaType(&This->pin.mtCurrent);
820 TRACE(" -- %x\n", hr);
821 return hr;
824 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
826 TRACE("\n");
828 /* Common attributes */
829 pPinImpl->pin.IPin_iface.lpVtbl = OutputPin_Vtbl;
830 pPinImpl->pin.refCount = 1;
831 pPinImpl->pin.pConnectedTo = NULL;
832 pPinImpl->pin.pCritSec = pCritSec;
833 pPinImpl->pin.tStart = 0;
834 pPinImpl->pin.tStop = 0;
835 pPinImpl->pin.dRate = 1.0;
836 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
837 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
838 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
840 /* Output pin attributes */
841 pPinImpl->pMemInputPin = NULL;
842 pPinImpl->pFuncsTable = pBaseOutputFuncsTable;
844 return S_OK;
847 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)
849 BaseOutputPin * pPinImpl;
851 *ppPin = NULL;
853 if (pPinInfo->dir != PINDIR_OUTPUT)
855 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
856 return E_INVALIDARG;
859 assert(outputpin_size >= sizeof(BaseOutputPin));
860 assert(pBaseFuncsTable->pfnAttemptConnection);
862 pPinImpl = CoTaskMemAlloc(outputpin_size);
864 if (!pPinImpl)
865 return E_OUTOFMEMORY;
867 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseOutputFuncsTable, pCritSec, pPinImpl)))
869 *ppPin = &pPinImpl->pin.IPin_iface;
870 return S_OK;
873 CoTaskMemFree(pPinImpl);
874 return E_FAIL;
877 /*** Input Pin implementation ***/
879 static inline BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
881 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
884 static inline BaseInputPin *impl_BaseInputPin_from_BasePin( BasePin *iface )
886 return CONTAINING_RECORD(iface, BaseInputPin, pin);
889 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
891 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
893 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
895 *ppv = NULL;
897 if (IsEqualIID(riid, &IID_IUnknown))
898 *ppv = iface;
899 else if (IsEqualIID(riid, &IID_IPin))
900 *ppv = iface;
901 else if (IsEqualIID(riid, &IID_IMemInputPin))
902 *ppv = &This->IMemInputPin_iface;
903 else if (IsEqualIID(riid, &IID_IMediaSeeking))
905 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
908 if (*ppv)
910 IUnknown_AddRef((IUnknown *)(*ppv));
911 return S_OK;
914 FIXME("No interface for %s!\n", debugstr_guid(riid));
916 return E_NOINTERFACE;
919 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
921 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
922 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
924 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
926 if (!refCount)
928 FreeMediaType(&This->pin.mtCurrent);
929 if (This->pAllocator)
930 IMemAllocator_Release(This->pAllocator);
931 This->pAllocator = NULL;
932 This->pin.IPin_iface.lpVtbl = NULL;
933 CoTaskMemFree(This);
934 return 0;
936 else
937 return refCount;
940 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
942 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
944 return E_UNEXPECTED;
948 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
950 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
951 PIN_DIRECTION pindirReceive;
952 HRESULT hr = S_OK;
954 TRACE("(%p, %p)\n", pReceivePin, pmt);
955 dump_AM_MEDIA_TYPE(pmt);
957 EnterCriticalSection(This->pin.pCritSec);
959 if (This->pin.pConnectedTo)
960 hr = VFW_E_ALREADY_CONNECTED;
962 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) != S_OK)
963 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
964 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
966 if (SUCCEEDED(hr))
968 IPin_QueryDirection(pReceivePin, &pindirReceive);
970 if (pindirReceive != PINDIR_OUTPUT)
972 ERR("Can't connect from non-output pin\n");
973 hr = VFW_E_INVALID_DIRECTION;
977 if (SUCCEEDED(hr))
979 CopyMediaType(&This->pin.mtCurrent, pmt);
980 This->pin.pConnectedTo = pReceivePin;
981 IPin_AddRef(pReceivePin);
984 LeaveCriticalSection(This->pin.pCritSec);
986 return hr;
989 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
991 return IPin_EndOfStream( pin );
994 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
996 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
998 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
1000 return (This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) == S_OK ? S_OK : S_FALSE);
1003 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
1005 HRESULT hr = S_OK;
1006 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1008 TRACE("(%p)\n", This);
1010 EnterCriticalSection(This->pin.pCritSec);
1011 if (This->flushing)
1012 hr = S_FALSE;
1013 else
1014 This->end_of_stream = 1;
1015 LeaveCriticalSection(This->pin.pCritSec);
1017 if (hr == S_OK)
1018 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
1019 return hr;
1022 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1024 return IPin_BeginFlush( pin );
1027 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1029 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1030 HRESULT hr;
1031 TRACE("() semi-stub\n");
1033 EnterCriticalSection(This->pin.pCritSec);
1034 This->flushing = 1;
1036 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1037 LeaveCriticalSection(This->pin.pCritSec);
1039 return hr;
1042 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1044 return IPin_EndFlush( pin );
1047 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1049 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1050 HRESULT hr;
1051 TRACE("(%p)\n", This);
1053 EnterCriticalSection(This->pin.pCritSec);
1054 This->flushing = This->end_of_stream = 0;
1056 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1057 LeaveCriticalSection(This->pin.pCritSec);
1059 return hr;
1062 typedef struct newsegmentargs
1064 REFERENCE_TIME tStart, tStop;
1065 double rate;
1066 } newsegmentargs;
1068 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1070 newsegmentargs *args = data;
1071 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1074 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1076 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1077 newsegmentargs args;
1079 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1081 args.tStart = This->pin.tStart = tStart;
1082 args.tStop = This->pin.tStop = tStop;
1083 args.rate = This->pin.dRate = dRate;
1085 return SendFurther( iface, deliver_newsegment, &args, NULL );
1088 static const IPinVtbl InputPin_Vtbl =
1090 BaseInputPinImpl_QueryInterface,
1091 BasePinImpl_AddRef,
1092 BaseInputPinImpl_Release,
1093 BaseInputPinImpl_Connect,
1094 BaseInputPinImpl_ReceiveConnection,
1095 BasePinImpl_Disconnect,
1096 BasePinImpl_ConnectedTo,
1097 BasePinImpl_ConnectionMediaType,
1098 BasePinImpl_QueryPinInfo,
1099 BasePinImpl_QueryDirection,
1100 BasePinImpl_QueryId,
1101 BaseInputPinImpl_QueryAccept,
1102 BasePinImpl_EnumMediaTypes,
1103 BasePinImpl_QueryInternalConnections,
1104 BaseInputPinImpl_EndOfStream,
1105 BaseInputPinImpl_BeginFlush,
1106 BaseInputPinImpl_EndFlush,
1107 BaseInputPinImpl_NewSegment
1110 /*** IMemInputPin implementation ***/
1112 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1114 return CONTAINING_RECORD(iface, BaseInputPin, IMemInputPin_iface);
1117 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1119 BaseInputPin *This = impl_from_IMemInputPin(iface);
1121 return IPin_QueryInterface(&This->pin.IPin_iface, riid, ppv);
1124 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1126 BaseInputPin *This = impl_from_IMemInputPin(iface);
1128 return IPin_AddRef(&This->pin.IPin_iface);
1131 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1133 BaseInputPin *This = impl_from_IMemInputPin(iface);
1135 return IPin_Release(&This->pin.IPin_iface);
1138 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1140 BaseInputPin *This = impl_from_IMemInputPin(iface);
1142 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1144 *ppAllocator = This->pAllocator;
1145 if (*ppAllocator)
1146 IMemAllocator_AddRef(*ppAllocator);
1148 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1151 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1153 BaseInputPin *This = impl_from_IMemInputPin(iface);
1155 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1157 if (bReadOnly)
1158 FIXME("Read only flag not handled yet!\n");
1160 /* FIXME: Should we release the allocator on disconnection? */
1161 if (!pAllocator)
1163 WARN("Null allocator\n");
1164 return E_POINTER;
1167 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1168 return E_FAIL;
1170 if (This->pAllocator)
1171 IMemAllocator_Release(This->pAllocator);
1172 This->pAllocator = pAllocator;
1173 if (This->pAllocator)
1174 IMemAllocator_AddRef(This->pAllocator);
1176 return S_OK;
1179 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1181 BaseInputPin *This = impl_from_IMemInputPin(iface);
1183 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1185 /* override this method if you have any specific requirements */
1187 return E_NOTIMPL;
1190 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1192 BaseInputPin *This = impl_from_IMemInputPin(iface);
1193 HRESULT hr = S_FALSE;
1195 /* this trace commented out for performance reasons */
1196 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1197 if (This->pFuncsTable->pfnReceive)
1198 hr = This->pFuncsTable->pfnReceive(This, pSample);
1199 return hr;
1202 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1204 HRESULT hr = S_OK;
1205 BaseInputPin *This = impl_from_IMemInputPin(iface);
1207 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1209 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1211 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1212 if (hr != S_OK)
1213 break;
1216 return hr;
1219 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1221 BaseInputPin *This = impl_from_IMemInputPin(iface);
1223 TRACE("(%p/%p)->()\n", This, iface);
1225 return S_OK;
1228 static const IMemInputPinVtbl MemInputPin_Vtbl =
1230 MemInputPin_QueryInterface,
1231 MemInputPin_AddRef,
1232 MemInputPin_Release,
1233 MemInputPin_GetAllocator,
1234 MemInputPin_NotifyAllocator,
1235 MemInputPin_GetAllocatorRequirements,
1236 MemInputPin_Receive,
1237 MemInputPin_ReceiveMultiple,
1238 MemInputPin_ReceiveCanBlock
1241 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1242 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1243 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1245 TRACE("\n");
1247 /* Common attributes */
1248 pPinImpl->pin.refCount = 1;
1249 pPinImpl->pin.pConnectedTo = NULL;
1250 pPinImpl->pin.pCritSec = pCritSec;
1251 pPinImpl->pin.tStart = 0;
1252 pPinImpl->pin.tStop = 0;
1253 pPinImpl->pin.dRate = 1.0;
1254 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1255 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1256 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
1258 /* Input pin attributes */
1259 pPinImpl->pFuncsTable = pBaseInputFuncsTable;
1260 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1261 if (pPinImpl->preferred_allocator)
1262 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1263 pPinImpl->pin.IPin_iface.lpVtbl = InputPin_Vtbl;
1264 pPinImpl->IMemInputPin_iface.lpVtbl = &MemInputPin_Vtbl;
1265 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1267 return S_OK;
1270 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1271 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1272 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1274 BaseInputPin * pPinImpl;
1276 *ppPin = NULL;
1278 assert(pBaseFuncsTable->pfnCheckMediaType);
1280 if (pPinInfo->dir != PINDIR_INPUT)
1282 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1283 return E_INVALIDARG;
1286 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1288 if (!pPinImpl)
1289 return E_OUTOFMEMORY;
1291 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseInputFuncsTable, pCritSec, allocator, pPinImpl)))
1293 *ppPin = (IPin *)pPinImpl;
1294 return S_OK;
1297 CoTaskMemFree(pPinImpl);
1298 return E_FAIL;