wined3d: Use the texture draw binding instead of the surface draw binding.
[wine.git] / dlls / strmbase / pin.c
blob02af5b325f8b721a8cb85c73ab4ff59ad0d3c396
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 BaseOutputPin_Destroy(This);
405 return 0;
407 return refCount;
410 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
412 HRESULT hr;
413 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
415 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
416 dump_AM_MEDIA_TYPE(pmt);
418 if (!pReceivePin)
419 return E_POINTER;
421 /* If we try to connect to ourselves, we will definitely deadlock.
422 * There are other cases where we could deadlock too, but this
423 * catches the obvious case */
424 assert(pReceivePin != iface);
426 EnterCriticalSection(This->pin.pCritSec);
428 /* if we have been a specific type to connect with, then we can either connect
429 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
430 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
431 hr = This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmt);
432 else
434 /* negotiate media type */
436 IEnumMediaTypes * pEnumCandidates;
437 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
439 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
441 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
443 /* try this filter's media types first */
444 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
446 assert(pmtCandidate);
447 dump_AM_MEDIA_TYPE(pmtCandidate);
448 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
449 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
450 assert(pmtCandidate->pbFormat);
451 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
452 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
454 hr = S_OK;
455 DeleteMediaType(pmtCandidate);
456 break;
458 DeleteMediaType(pmtCandidate);
459 pmtCandidate = NULL;
461 IEnumMediaTypes_Release(pEnumCandidates);
464 /* then try receiver filter's media types */
465 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
467 ULONG fetched;
469 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
471 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, &fetched))
473 assert(pmtCandidate);
474 dump_AM_MEDIA_TYPE(pmtCandidate);
475 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
476 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
478 hr = S_OK;
479 DeleteMediaType(pmtCandidate);
480 break;
482 DeleteMediaType(pmtCandidate);
483 pmtCandidate = NULL;
484 } /* while */
485 IEnumMediaTypes_Release(pEnumCandidates);
486 } /* if not found */
487 } /* if negotiate media type */
488 } /* if succeeded */
489 LeaveCriticalSection(This->pin.pCritSec);
491 TRACE(" -- %x\n", hr);
492 return hr;
495 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
497 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
499 return E_UNEXPECTED;
502 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
504 HRESULT hr;
505 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
507 TRACE("()\n");
509 EnterCriticalSection(This->pin.pCritSec);
511 if (This->pMemInputPin)
513 IMemInputPin_Release(This->pMemInputPin);
514 This->pMemInputPin = NULL;
516 if (This->pin.pConnectedTo)
518 IPin_Release(This->pin.pConnectedTo);
519 This->pin.pConnectedTo = NULL;
520 FreeMediaType(&This->pin.mtCurrent);
521 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
522 hr = S_OK;
524 else
525 hr = S_FALSE;
527 LeaveCriticalSection(This->pin.pCritSec);
529 return hr;
532 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
534 TRACE("()\n");
536 /* not supposed to do anything in an output pin */
538 return E_UNEXPECTED;
541 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
543 TRACE("(%p)->()\n", iface);
545 /* not supposed to do anything in an output pin */
547 return E_UNEXPECTED;
550 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
552 TRACE("(%p)->()\n", iface);
554 /* not supposed to do anything in an output pin */
556 return E_UNEXPECTED;
559 static const IPinVtbl OutputPin_Vtbl =
561 BaseOutputPinImpl_QueryInterface,
562 BasePinImpl_AddRef,
563 BaseOutputPinImpl_Release,
564 BaseOutputPinImpl_Connect,
565 BaseOutputPinImpl_ReceiveConnection,
566 BaseOutputPinImpl_Disconnect,
567 BasePinImpl_ConnectedTo,
568 BasePinImpl_ConnectionMediaType,
569 BasePinImpl_QueryPinInfo,
570 BasePinImpl_QueryDirection,
571 BasePinImpl_QueryId,
572 BasePinImpl_QueryAccept,
573 BasePinImpl_EnumMediaTypes,
574 BasePinImpl_QueryInternalConnections,
575 BaseOutputPinImpl_EndOfStream,
576 BaseOutputPinImpl_BeginFlush,
577 BaseOutputPinImpl_EndFlush,
578 BasePinImpl_NewSegment
581 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
583 HRESULT hr;
585 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
587 if (!This->pin.pConnectedTo)
588 hr = VFW_E_NOT_CONNECTED;
589 else
591 hr = IMemAllocator_GetBuffer(This->pAllocator, ppSample, tStart, tStop, dwFlags);
593 if (SUCCEEDED(hr))
594 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
597 return hr;
600 /* replaces OutputPin_SendSample */
601 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
603 HRESULT hr = S_OK;
604 IMemInputPin * pMemConnected = NULL;
605 PIN_INFO pinInfo;
607 EnterCriticalSection(This->pin.pCritSec);
609 if (!This->pin.pConnectedTo || !This->pMemInputPin)
610 hr = VFW_E_NOT_CONNECTED;
611 else
613 /* we don't have the lock held when using This->pMemInputPin,
614 * so we need to AddRef it to stop it being deleted while we are
615 * using it. Same with its filter. */
616 pMemConnected = This->pMemInputPin;
617 IMemInputPin_AddRef(pMemConnected);
618 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
621 LeaveCriticalSection(This->pin.pCritSec);
623 if (SUCCEEDED(hr))
625 /* NOTE: if we are in a critical section when Receive is called
626 * then it causes some problems (most notably with the native Video
627 * Renderer) if we are re-entered for whatever reason */
628 hr = IMemInputPin_Receive(pMemConnected, pSample);
630 /* If the filter's destroyed, tell upstream to stop sending data */
631 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
632 hr = S_FALSE;
634 if (pMemConnected)
635 IMemInputPin_Release(pMemConnected);
637 return hr;
640 /* replaces OutputPin_CommitAllocator */
641 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
643 HRESULT hr = S_OK;
645 TRACE("(%p)->()\n", This);
647 EnterCriticalSection(This->pin.pCritSec);
649 if (!This->pin.pConnectedTo || !This->pMemInputPin)
650 hr = VFW_E_NOT_CONNECTED;
651 else
652 hr = IMemAllocator_Commit(This->pAllocator);
654 LeaveCriticalSection(This->pin.pCritSec);
656 TRACE("--> %08x\n", hr);
657 return hr;
660 /* replaces OutputPin_DecommitAllocator */
661 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
663 HRESULT hr = S_OK;
665 TRACE("(%p)->()\n", This);
667 EnterCriticalSection(This->pin.pCritSec);
669 if (!This->pin.pConnectedTo || !This->pMemInputPin)
670 hr = VFW_E_NOT_CONNECTED;
671 else
672 hr = IMemAllocator_Decommit(This->pAllocator);
674 LeaveCriticalSection(This->pin.pCritSec);
676 TRACE("--> %08x\n", hr);
677 return hr;
680 /* replaces OutputPin_DeliverDisconnect */
681 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
683 HRESULT hr;
685 TRACE("(%p)->()\n", This);
687 EnterCriticalSection(This->pin.pCritSec);
689 if (!This->pin.pConnectedTo || !This->pMemInputPin)
690 hr = VFW_E_NOT_CONNECTED;
691 else
693 hr = IMemAllocator_Decommit(This->pAllocator);
695 if (SUCCEEDED(hr))
696 hr = IPin_Disconnect(This->pin.pConnectedTo);
698 IPin_Disconnect(&This->pin.IPin_iface);
700 LeaveCriticalSection(This->pin.pCritSec);
702 return hr;
705 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
707 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
710 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
712 HRESULT hr;
714 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
716 if (hr == VFW_E_NO_ALLOCATOR)
717 /* Input pin provides no allocator, use standard memory allocator */
718 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
720 if (SUCCEEDED(hr))
722 ALLOCATOR_PROPERTIES rProps;
723 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
725 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
726 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
729 if (SUCCEEDED(hr))
730 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
732 return hr;
735 /*** The Construct functions ***/
737 /* Function called as a helper to IPin_Connect */
738 /* specific AM_MEDIA_TYPE - it cannot be NULL */
739 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
741 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
742 HRESULT hr;
743 IMemAllocator * pMemAlloc = NULL;
745 TRACE("(%p, %p)\n", pReceivePin, pmt);
746 dump_AM_MEDIA_TYPE(pmt);
748 /* FIXME: call queryacceptproc */
750 This->pin.pConnectedTo = pReceivePin;
751 IPin_AddRef(pReceivePin);
752 CopyMediaType(&This->pin.mtCurrent, pmt);
754 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
756 /* get the IMemInputPin interface we will use to deliver samples to the
757 * connected pin */
758 if (SUCCEEDED(hr))
760 This->pMemInputPin = NULL;
761 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
763 if (SUCCEEDED(hr))
765 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
766 if (SUCCEEDED(hr))
767 This->pAllocator = pMemAlloc;
768 else if (pMemAlloc)
769 IMemAllocator_Release(pMemAlloc);
772 /* break connection if we couldn't get the allocator */
773 if (FAILED(hr))
775 if (This->pMemInputPin)
776 IMemInputPin_Release(This->pMemInputPin);
777 This->pMemInputPin = NULL;
779 IPin_Disconnect(pReceivePin);
783 if (FAILED(hr))
785 IPin_Release(This->pin.pConnectedTo);
786 This->pin.pConnectedTo = NULL;
787 FreeMediaType(&This->pin.mtCurrent);
790 TRACE(" -- %x\n", hr);
791 return hr;
794 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
796 TRACE("\n");
798 /* Common attributes */
799 pPinImpl->pin.IPin_iface.lpVtbl = OutputPin_Vtbl;
800 pPinImpl->pin.refCount = 1;
801 pPinImpl->pin.pConnectedTo = NULL;
802 pPinImpl->pin.pCritSec = pCritSec;
803 pPinImpl->pin.tStart = 0;
804 pPinImpl->pin.tStop = 0;
805 pPinImpl->pin.dRate = 1.0;
806 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
807 pPinImpl->pin.pFuncsTable = &vtbl->base;
808 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
810 /* Output pin attributes */
811 pPinImpl->pMemInputPin = NULL;
812 pPinImpl->pAllocator = NULL;
813 pPinImpl->pFuncsTable = vtbl;
815 return S_OK;
818 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
820 BaseOutputPin * pPinImpl;
822 *ppPin = NULL;
824 if (pPinInfo->dir != PINDIR_OUTPUT)
826 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
827 return E_INVALIDARG;
830 assert(outputpin_size >= sizeof(BaseOutputPin));
831 assert(vtbl->base.pfnAttemptConnection);
833 pPinImpl = CoTaskMemAlloc(outputpin_size);
835 if (!pPinImpl)
836 return E_OUTOFMEMORY;
838 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, vtbl, pCritSec, pPinImpl)))
840 *ppPin = &pPinImpl->pin.IPin_iface;
841 return S_OK;
844 CoTaskMemFree(pPinImpl);
845 return E_FAIL;
848 HRESULT WINAPI BaseOutputPin_Destroy(BaseOutputPin *This)
850 FreeMediaType(&This->pin.mtCurrent);
851 if (This->pAllocator)
852 IMemAllocator_Release(This->pAllocator);
853 This->pAllocator = NULL;
854 CoTaskMemFree(This);
855 return S_OK;
858 /*** Input Pin implementation ***/
860 static inline BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
862 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
865 static inline BaseInputPin *impl_BaseInputPin_from_BasePin( BasePin *iface )
867 return CONTAINING_RECORD(iface, BaseInputPin, pin);
870 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
872 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
874 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
876 *ppv = NULL;
878 if (IsEqualIID(riid, &IID_IUnknown))
879 *ppv = iface;
880 else if (IsEqualIID(riid, &IID_IPin))
881 *ppv = iface;
882 else if (IsEqualIID(riid, &IID_IMemInputPin))
883 *ppv = &This->IMemInputPin_iface;
884 else if (IsEqualIID(riid, &IID_IMediaSeeking))
886 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
889 if (*ppv)
891 IUnknown_AddRef((IUnknown *)(*ppv));
892 return S_OK;
895 FIXME("No interface for %s!\n", debugstr_guid(riid));
897 return E_NOINTERFACE;
900 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
902 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
903 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
905 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
907 if (!refCount)
909 BaseInputPin_Destroy(This);
910 return 0;
912 else
913 return refCount;
916 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
918 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
920 return E_UNEXPECTED;
924 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
926 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
927 PIN_DIRECTION pindirReceive;
928 HRESULT hr = S_OK;
930 TRACE("(%p, %p)\n", pReceivePin, pmt);
931 dump_AM_MEDIA_TYPE(pmt);
933 EnterCriticalSection(This->pin.pCritSec);
935 if (This->pin.pConnectedTo)
936 hr = VFW_E_ALREADY_CONNECTED;
938 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) != S_OK)
939 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
940 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
942 if (SUCCEEDED(hr))
944 IPin_QueryDirection(pReceivePin, &pindirReceive);
946 if (pindirReceive != PINDIR_OUTPUT)
948 ERR("Can't connect from non-output pin\n");
949 hr = VFW_E_INVALID_DIRECTION;
953 if (SUCCEEDED(hr))
955 CopyMediaType(&This->pin.mtCurrent, pmt);
956 This->pin.pConnectedTo = pReceivePin;
957 IPin_AddRef(pReceivePin);
960 LeaveCriticalSection(This->pin.pCritSec);
962 return hr;
965 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
967 return IPin_EndOfStream( pin );
970 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
972 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
974 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
976 return (This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) == S_OK ? S_OK : S_FALSE);
979 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
981 HRESULT hr = S_OK;
982 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
984 TRACE("(%p)\n", This);
986 EnterCriticalSection(This->pin.pCritSec);
987 if (This->flushing)
988 hr = S_FALSE;
989 else
990 This->end_of_stream = TRUE;
991 LeaveCriticalSection(This->pin.pCritSec);
993 if (hr == S_OK)
994 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
995 return hr;
998 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1000 return IPin_BeginFlush( pin );
1003 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1005 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1006 HRESULT hr;
1007 TRACE("() semi-stub\n");
1009 EnterCriticalSection(This->pin.pCritSec);
1010 This->flushing = TRUE;
1012 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1013 LeaveCriticalSection(This->pin.pCritSec);
1015 return hr;
1018 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1020 return IPin_EndFlush( pin );
1023 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1025 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1026 HRESULT hr;
1027 TRACE("(%p)\n", This);
1029 EnterCriticalSection(This->pin.pCritSec);
1030 This->flushing = This->end_of_stream = FALSE;
1032 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1033 LeaveCriticalSection(This->pin.pCritSec);
1035 return hr;
1038 typedef struct newsegmentargs
1040 REFERENCE_TIME tStart, tStop;
1041 double rate;
1042 } newsegmentargs;
1044 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1046 newsegmentargs *args = data;
1047 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1050 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1052 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1053 newsegmentargs args;
1055 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1057 args.tStart = This->pin.tStart = tStart;
1058 args.tStop = This->pin.tStop = tStop;
1059 args.rate = This->pin.dRate = dRate;
1061 return SendFurther( iface, deliver_newsegment, &args, NULL );
1064 static const IPinVtbl InputPin_Vtbl =
1066 BaseInputPinImpl_QueryInterface,
1067 BasePinImpl_AddRef,
1068 BaseInputPinImpl_Release,
1069 BaseInputPinImpl_Connect,
1070 BaseInputPinImpl_ReceiveConnection,
1071 BasePinImpl_Disconnect,
1072 BasePinImpl_ConnectedTo,
1073 BasePinImpl_ConnectionMediaType,
1074 BasePinImpl_QueryPinInfo,
1075 BasePinImpl_QueryDirection,
1076 BasePinImpl_QueryId,
1077 BaseInputPinImpl_QueryAccept,
1078 BasePinImpl_EnumMediaTypes,
1079 BasePinImpl_QueryInternalConnections,
1080 BaseInputPinImpl_EndOfStream,
1081 BaseInputPinImpl_BeginFlush,
1082 BaseInputPinImpl_EndFlush,
1083 BaseInputPinImpl_NewSegment
1086 /*** IMemInputPin implementation ***/
1088 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1090 return CONTAINING_RECORD(iface, BaseInputPin, IMemInputPin_iface);
1093 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1095 BaseInputPin *This = impl_from_IMemInputPin(iface);
1097 return IPin_QueryInterface(&This->pin.IPin_iface, riid, ppv);
1100 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1102 BaseInputPin *This = impl_from_IMemInputPin(iface);
1104 return IPin_AddRef(&This->pin.IPin_iface);
1107 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1109 BaseInputPin *This = impl_from_IMemInputPin(iface);
1111 return IPin_Release(&This->pin.IPin_iface);
1114 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1116 BaseInputPin *This = impl_from_IMemInputPin(iface);
1118 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1120 *ppAllocator = This->pAllocator;
1121 if (*ppAllocator)
1122 IMemAllocator_AddRef(*ppAllocator);
1124 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1127 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1129 BaseInputPin *This = impl_from_IMemInputPin(iface);
1131 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1133 if (bReadOnly)
1134 FIXME("Read only flag not handled yet!\n");
1136 /* FIXME: Should we release the allocator on disconnection? */
1137 if (!pAllocator)
1139 WARN("Null allocator\n");
1140 return E_POINTER;
1143 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1144 return E_FAIL;
1146 if (This->pAllocator)
1147 IMemAllocator_Release(This->pAllocator);
1148 This->pAllocator = pAllocator;
1149 if (This->pAllocator)
1150 IMemAllocator_AddRef(This->pAllocator);
1152 return S_OK;
1155 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1157 BaseInputPin *This = impl_from_IMemInputPin(iface);
1159 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1161 /* override this method if you have any specific requirements */
1163 return E_NOTIMPL;
1166 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1168 BaseInputPin *This = impl_from_IMemInputPin(iface);
1169 HRESULT hr = S_FALSE;
1171 /* this trace commented out for performance reasons */
1172 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1173 if (This->pFuncsTable->pfnReceive)
1174 hr = This->pFuncsTable->pfnReceive(This, pSample);
1175 return hr;
1178 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1180 HRESULT hr = S_OK;
1181 BaseInputPin *This = impl_from_IMemInputPin(iface);
1183 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1185 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1187 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1188 if (hr != S_OK)
1189 break;
1192 return hr;
1195 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1197 BaseInputPin *This = impl_from_IMemInputPin(iface);
1199 TRACE("(%p/%p)->()\n", This, iface);
1201 return S_OK;
1204 static const IMemInputPinVtbl MemInputPin_Vtbl =
1206 MemInputPin_QueryInterface,
1207 MemInputPin_AddRef,
1208 MemInputPin_Release,
1209 MemInputPin_GetAllocator,
1210 MemInputPin_NotifyAllocator,
1211 MemInputPin_GetAllocatorRequirements,
1212 MemInputPin_Receive,
1213 MemInputPin_ReceiveMultiple,
1214 MemInputPin_ReceiveCanBlock
1217 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1218 const BaseInputPinFuncTable* vtbl,
1219 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1221 TRACE("\n");
1223 /* Common attributes */
1224 pPinImpl->pin.refCount = 1;
1225 pPinImpl->pin.pConnectedTo = NULL;
1226 pPinImpl->pin.pCritSec = pCritSec;
1227 pPinImpl->pin.tStart = 0;
1228 pPinImpl->pin.tStop = 0;
1229 pPinImpl->pin.dRate = 1.0;
1230 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1231 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1232 pPinImpl->pin.pFuncsTable = &vtbl->base;
1234 /* Input pin attributes */
1235 pPinImpl->pFuncsTable = vtbl;
1236 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1237 if (pPinImpl->preferred_allocator)
1238 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1239 pPinImpl->pin.IPin_iface.lpVtbl = InputPin_Vtbl;
1240 pPinImpl->IMemInputPin_iface.lpVtbl = &MemInputPin_Vtbl;
1241 pPinImpl->flushing = pPinImpl->end_of_stream = FALSE;
1243 return S_OK;
1246 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, LONG inputpin_size, const PIN_INFO * pPinInfo,
1247 const BaseInputPinFuncTable* vtbl,
1248 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1250 BaseInputPin * pPinImpl;
1252 *ppPin = NULL;
1254 assert(inputpin_size >= sizeof(BaseInputPin));
1255 assert(vtbl->base.pfnCheckMediaType);
1257 if (pPinInfo->dir != PINDIR_INPUT)
1259 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1260 return E_INVALIDARG;
1263 pPinImpl = CoTaskMemAlloc(inputpin_size);
1265 if (!pPinImpl)
1266 return E_OUTOFMEMORY;
1268 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, vtbl, pCritSec, allocator, pPinImpl)))
1270 *ppPin = (IPin *)pPinImpl;
1271 return S_OK;
1274 CoTaskMemFree(pPinImpl);
1275 return E_FAIL;
1278 HRESULT WINAPI BaseInputPin_Destroy(BaseInputPin *This)
1280 FreeMediaType(&This->pin.mtCurrent);
1281 if (This->pAllocator)
1282 IMemAllocator_Release(This->pAllocator);
1283 This->pAllocator = NULL;
1284 This->pin.IPin_iface.lpVtbl = NULL;
1285 CoTaskMemFree(This);
1286 return S_OK;