Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / strmbase / pin.c
blob1731145c13b317c432ce1b156de5ce6695313c51
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 /** Helper function, there are a lot of places where the error code is inherited
41 * The following rules apply:
43 * Return the first received error code (E_NOTIMPL is ignored)
44 * If no errors occur: return the first received non-error-code that isn't S_OK
46 static HRESULT updatehres( HRESULT original, HRESULT new )
48 if (FAILED( original ) || new == E_NOTIMPL)
49 return original;
51 if (FAILED( new ) || original == S_OK)
52 return new;
54 return original;
57 /** Sends a message from a pin further to other, similar pins
58 * fnMiddle is called on each pin found further on the stream.
59 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
61 * If the pin given is an input pin, the message will be sent downstream to other input pins
62 * If the pin given is an output pin, the message will be sent upstream to other output pins
64 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
66 PIN_INFO pin_info;
67 ULONG amount = 0;
68 HRESULT hr = S_OK;
69 HRESULT hr_return = S_OK;
70 IEnumPins *enumpins = NULL;
71 BOOL foundend = TRUE;
72 PIN_DIRECTION from_dir;
74 IPin_QueryDirection( from, &from_dir );
76 hr = IPin_QueryInternalConnections( from, NULL, &amount );
77 if (hr != E_NOTIMPL && amount)
78 FIXME("Use QueryInternalConnections!\n");
79 hr = S_OK;
81 pin_info.pFilter = NULL;
82 hr = IPin_QueryPinInfo( from, &pin_info );
83 if (FAILED(hr))
84 goto out;
86 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
87 if (FAILED(hr))
88 goto out;
90 hr = IEnumPins_Reset( enumpins );
91 while (hr == S_OK) {
92 IPin *pin = NULL;
93 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
94 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
96 hr = IEnumPins_Reset( enumpins );
97 continue;
99 if (pin)
101 PIN_DIRECTION dir;
103 IPin_QueryDirection( pin, &dir );
104 if (dir != from_dir)
106 IPin *connected = NULL;
108 foundend = FALSE;
109 IPin_ConnectedTo( pin, &connected );
110 if (connected)
112 HRESULT hr_local;
114 hr_local = fnMiddle( connected, arg );
115 hr_return = updatehres( hr_return, hr_local );
116 IPin_Release(connected);
119 IPin_Release( pin );
121 else
123 hr = S_OK;
124 break;
128 if (!foundend)
129 hr = hr_return;
130 else if (fnEnd) {
131 HRESULT hr_local;
133 hr_local = fnEnd( from, arg );
134 hr_return = updatehres( hr_return, hr_local );
137 out:
138 if (pin_info.pFilter)
139 IBaseFilter_Release( pin_info.pFilter );
140 return hr;
143 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
145 /* Tempting to just do a memcpy, but the name field is
146 128 characters long! We will probably never exceed 10
147 most of the time, so we are better off copying
148 each field manually */
149 strcpyW(pDest->achName, pSrc->achName);
150 pDest->dir = pSrc->dir;
151 pDest->pFilter = pSrc->pFilter;
154 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
156 if (!pmt)
157 return;
158 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
161 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
163 TRACE("pmt1: ");
164 dump_AM_MEDIA_TYPE(pmt1);
165 TRACE("pmt2: ");
166 dump_AM_MEDIA_TYPE(pmt2);
167 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
168 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
171 /*** Common Base Pin function */
172 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
174 if (iPosition < 0)
175 return E_INVALIDARG;
176 return VFW_S_NO_MORE_ITEMS;
179 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
181 return 1;
184 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
186 BasePin *This = (BasePin *)iface;
187 ULONG refCount = InterlockedIncrement(&This->refCount);
189 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
191 return refCount;
194 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
196 HRESULT hr;
197 BasePin *This = (BasePin *)iface;
199 TRACE("()\n");
201 EnterCriticalSection(This->pCritSec);
203 if (This->pConnectedTo)
205 IPin_Release(This->pConnectedTo);
206 This->pConnectedTo = NULL;
207 FreeMediaType(&This->mtCurrent);
208 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
209 hr = S_OK;
211 else
212 hr = S_FALSE;
214 LeaveCriticalSection(This->pCritSec);
216 return hr;
219 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
221 HRESULT hr;
222 BasePin *This = (BasePin *)iface;
224 TRACE("(%p)\n", ppPin);
226 EnterCriticalSection(This->pCritSec);
228 if (This->pConnectedTo)
230 *ppPin = This->pConnectedTo;
231 IPin_AddRef(*ppPin);
232 hr = S_OK;
234 else
236 hr = VFW_E_NOT_CONNECTED;
237 *ppPin = NULL;
240 LeaveCriticalSection(This->pCritSec);
242 return hr;
245 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
247 HRESULT hr;
248 BasePin *This = (BasePin *)iface;
250 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
252 EnterCriticalSection(This->pCritSec);
254 if (This->pConnectedTo)
256 CopyMediaType(pmt, &This->mtCurrent);
257 hr = S_OK;
259 else
261 ZeroMemory(pmt, sizeof(*pmt));
262 hr = VFW_E_NOT_CONNECTED;
265 LeaveCriticalSection(This->pCritSec);
267 return hr;
270 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
272 BasePin *This = (BasePin *)iface;
274 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
276 Copy_PinInfo(pInfo, &This->pinInfo);
277 IBaseFilter_AddRef(pInfo->pFilter);
279 return S_OK;
282 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
284 BasePin *This = (BasePin *)iface;
286 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
288 *pPinDir = This->pinInfo.dir;
290 return S_OK;
293 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
295 BasePin *This = (BasePin *)iface;
297 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
299 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
300 if (!*Id)
301 return E_OUTOFMEMORY;
303 strcpyW(*Id, This->pinInfo.achName);
305 return S_OK;
308 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
310 TRACE("(%p)->(%p)\n", iface, pmt);
312 return S_OK;
315 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
317 BasePin *This = (BasePin *)iface;
319 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
321 /* override this method to allow enumeration of your types */
323 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
326 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
328 BasePin *This = (BasePin *)iface;
330 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
332 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
335 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
337 BasePin *This = (BasePin *)iface;
339 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
341 This->tStart = tStart;
342 This->tStop = tStop;
343 This->dRate = dRate;
345 return S_OK;
348 /*** OutputPin implementation ***/
350 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
352 BaseOutputPin *This = (BaseOutputPin *)iface;
354 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
356 *ppv = NULL;
358 if (IsEqualIID(riid, &IID_IUnknown))
359 *ppv = iface;
360 else if (IsEqualIID(riid, &IID_IPin))
361 *ppv = iface;
362 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
363 IsEqualIID(riid, &IID_IQualityControl))
365 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
368 if (*ppv)
370 IUnknown_AddRef((IUnknown *)(*ppv));
371 return S_OK;
374 FIXME("No interface for %s!\n", debugstr_guid(riid));
376 return E_NOINTERFACE;
379 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
381 BaseOutputPin *This = (BaseOutputPin *)iface;
382 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
384 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
386 if (!refCount)
388 FreeMediaType(&This->pin.mtCurrent);
389 CoTaskMemFree(This);
390 return 0;
392 return refCount;
395 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
397 HRESULT hr;
398 BaseOutputPin *This = (BaseOutputPin *)iface;
400 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
401 dump_AM_MEDIA_TYPE(pmt);
403 /* If we try to connect to ourself, we will definitely deadlock.
404 * There are other cases where we could deadlock too, but this
405 * catches the obvious case */
406 assert(pReceivePin != iface);
408 EnterCriticalSection(This->pin.pCritSec);
410 /* if we have been a specific type to connect with, then we can either connect
411 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
412 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
413 hr = This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmt);
414 else
416 /* negotiate media type */
418 IEnumMediaTypes * pEnumCandidates;
419 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
421 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
423 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
425 /* try this filter's media types first */
426 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
428 assert(pmtCandidate);
429 dump_AM_MEDIA_TYPE(pmtCandidate);
430 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
431 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
432 assert(pmtCandidate->pbFormat);
433 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
434 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
436 hr = S_OK;
437 DeleteMediaType(pmtCandidate);
438 break;
440 DeleteMediaType(pmtCandidate);
441 pmtCandidate = NULL;
443 IEnumMediaTypes_Release(pEnumCandidates);
446 /* then try receiver filter's media types */
447 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
449 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
451 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
453 assert(pmtCandidate);
454 dump_AM_MEDIA_TYPE(pmtCandidate);
455 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
456 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
458 hr = S_OK;
459 DeleteMediaType(pmtCandidate);
460 break;
462 DeleteMediaType(pmtCandidate);
463 pmtCandidate = NULL;
464 } /* while */
465 IEnumMediaTypes_Release(pEnumCandidates);
466 } /* if not found */
467 } /* if negotiate media type */
468 } /* if succeeded */
469 LeaveCriticalSection(This->pin.pCritSec);
471 TRACE(" -- %x\n", hr);
472 return hr;
475 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
477 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
479 return E_UNEXPECTED;
482 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
484 HRESULT hr;
485 BaseOutputPin *This = (BaseOutputPin *)iface;
487 TRACE("()\n");
489 EnterCriticalSection(This->pin.pCritSec);
491 if (This->pMemInputPin)
493 IMemInputPin_Release(This->pMemInputPin);
494 This->pMemInputPin = NULL;
496 if (This->pin.pConnectedTo)
498 IPin_Release(This->pin.pConnectedTo);
499 This->pin.pConnectedTo = NULL;
500 FreeMediaType(&This->pin.mtCurrent);
501 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
502 hr = S_OK;
504 else
505 hr = S_FALSE;
507 LeaveCriticalSection(This->pin.pCritSec);
509 return hr;
512 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
514 TRACE("()\n");
516 /* not supposed to do anything in an output pin */
518 return E_UNEXPECTED;
521 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
523 TRACE("(%p)->()\n", iface);
525 /* not supposed to do anything in an output pin */
527 return E_UNEXPECTED;
530 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
532 TRACE("(%p)->()\n", iface);
534 /* not supposed to do anything in an output pin */
536 return E_UNEXPECTED;
539 static const IPinVtbl OutputPin_Vtbl =
541 BaseOutputPinImpl_QueryInterface,
542 BasePinImpl_AddRef,
543 BaseOutputPinImpl_Release,
544 BaseOutputPinImpl_Connect,
545 BaseOutputPinImpl_ReceiveConnection,
546 BaseOutputPinImpl_Disconnect,
547 BasePinImpl_ConnectedTo,
548 BasePinImpl_ConnectionMediaType,
549 BasePinImpl_QueryPinInfo,
550 BasePinImpl_QueryDirection,
551 BasePinImpl_QueryId,
552 BasePinImpl_QueryAccept,
553 BasePinImpl_EnumMediaTypes,
554 BasePinImpl_QueryInternalConnections,
555 BaseOutputPinImpl_EndOfStream,
556 BaseOutputPinImpl_BeginFlush,
557 BaseOutputPinImpl_EndFlush,
558 BasePinImpl_NewSegment
561 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
563 HRESULT hr;
565 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
567 EnterCriticalSection(This->pin.pCritSec);
569 if (!This->pin.pConnectedTo)
570 hr = VFW_E_NOT_CONNECTED;
571 else
573 IMemAllocator * pAlloc = NULL;
575 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
577 if (SUCCEEDED(hr))
578 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
580 if (SUCCEEDED(hr))
581 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
583 if (pAlloc)
584 IMemAllocator_Release(pAlloc);
587 LeaveCriticalSection(This->pin.pCritSec);
589 return hr;
592 /* replaces OutputPin_SendSample */
593 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
595 HRESULT hr = S_OK;
596 IMemInputPin * pMemConnected = NULL;
597 PIN_INFO pinInfo;
599 EnterCriticalSection(This->pin.pCritSec);
601 if (!This->pin.pConnectedTo || !This->pMemInputPin)
602 hr = VFW_E_NOT_CONNECTED;
603 else
605 /* we don't have the lock held when using This->pMemInputPin,
606 * so we need to AddRef it to stop it being deleted while we are
607 * using it. Same with its filter. */
608 pMemConnected = This->pMemInputPin;
609 IMemInputPin_AddRef(pMemConnected);
610 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
613 LeaveCriticalSection(This->pin.pCritSec);
615 if (SUCCEEDED(hr))
617 /* NOTE: if we are in a critical section when Receive is called
618 * then it causes some problems (most notably with the native Video
619 * Renderer) if we are re-entered for whatever reason */
620 hr = IMemInputPin_Receive(pMemConnected, pSample);
622 /* If the filter's destroyed, tell upstream to stop sending data */
623 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
624 hr = S_FALSE;
626 if (pMemConnected)
627 IMemInputPin_Release(pMemConnected);
629 return hr;
632 /* replaces OutputPin_CommitAllocator */
633 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
635 HRESULT hr = S_OK;
637 TRACE("(%p)->()\n", This);
639 EnterCriticalSection(This->pin.pCritSec);
641 if (!This->pin.pConnectedTo || !This->pMemInputPin)
642 hr = VFW_E_NOT_CONNECTED;
643 else
645 IMemAllocator * pAlloc = NULL;
647 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
649 if (SUCCEEDED(hr))
650 hr = IMemAllocator_Commit(pAlloc);
652 if (pAlloc)
653 IMemAllocator_Release(pAlloc);
656 LeaveCriticalSection(This->pin.pCritSec);
658 TRACE("--> %08x\n", hr);
659 return hr;
662 /* replaces OutputPin_DecommitAllocator */
663 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
665 HRESULT hr = S_OK;
667 TRACE("(%p)->()\n", This);
669 EnterCriticalSection(This->pin.pCritSec);
671 if (!This->pin.pConnectedTo || !This->pMemInputPin)
672 hr = VFW_E_NOT_CONNECTED;
673 else
675 IMemAllocator * pAlloc = NULL;
677 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
679 if (SUCCEEDED(hr))
680 hr = IMemAllocator_Decommit(pAlloc);
682 if (pAlloc)
683 IMemAllocator_Release(pAlloc);
686 LeaveCriticalSection(This->pin.pCritSec);
688 TRACE("--> %08x\n", hr);
689 return hr;
692 /* replaces OutputPin_DeliverDisconnect */
693 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
695 HRESULT hr;
697 TRACE("(%p)->()\n", This);
699 EnterCriticalSection(This->pin.pCritSec);
701 if (!This->pin.pConnectedTo || !This->pMemInputPin)
702 hr = VFW_E_NOT_CONNECTED;
703 else
705 IMemAllocator * pAlloc = NULL;
707 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
709 if (SUCCEEDED(hr))
710 hr = IMemAllocator_Decommit(pAlloc);
712 if (pAlloc)
713 IMemAllocator_Release(pAlloc);
715 if (SUCCEEDED(hr))
716 hr = IPin_Disconnect(This->pin.pConnectedTo);
718 IPin_Disconnect((IPin *)This);
720 LeaveCriticalSection(This->pin.pCritSec);
722 return hr;
725 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
727 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
730 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
732 HRESULT hr;
734 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
736 if (hr == VFW_E_NO_ALLOCATOR)
737 /* Input pin provides no allocator, use standard memory allocator */
738 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
740 if (SUCCEEDED(hr))
742 ALLOCATOR_PROPERTIES rProps;
743 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
745 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
746 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
749 if (SUCCEEDED(hr))
750 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
752 return hr;
755 /*** The Construct functions ***/
757 /* Function called as a helper to IPin_Connect */
758 /* specific AM_MEDIA_TYPE - it cannot be NULL */
759 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
761 BaseOutputPin *This = (BaseOutputPin *)iface;
762 HRESULT hr;
763 IMemAllocator * pMemAlloc = NULL;
765 TRACE("(%p, %p)\n", pReceivePin, pmt);
766 dump_AM_MEDIA_TYPE(pmt);
768 /* FIXME: call queryacceptproc */
770 This->pin.pConnectedTo = pReceivePin;
771 IPin_AddRef(pReceivePin);
772 CopyMediaType(&This->pin.mtCurrent, pmt);
774 hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
776 /* get the IMemInputPin interface we will use to deliver samples to the
777 * connected pin */
778 if (SUCCEEDED(hr))
780 This->pMemInputPin = NULL;
781 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
783 if (SUCCEEDED(hr))
785 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
786 if (pMemAlloc)
787 IMemAllocator_Release(pMemAlloc);
790 /* break connection if we couldn't get the allocator */
791 if (FAILED(hr))
793 if (This->pMemInputPin)
794 IMemInputPin_Release(This->pMemInputPin);
795 This->pMemInputPin = NULL;
797 IPin_Disconnect(pReceivePin);
801 if (FAILED(hr))
803 IPin_Release(This->pin.pConnectedTo);
804 This->pin.pConnectedTo = NULL;
805 FreeMediaType(&This->pin.mtCurrent);
808 TRACE(" -- %x\n", hr);
809 return hr;
812 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
814 TRACE("\n");
816 /* Common attributes */
817 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
818 pPinImpl->pin.refCount = 1;
819 pPinImpl->pin.pConnectedTo = NULL;
820 pPinImpl->pin.pCritSec = pCritSec;
821 pPinImpl->pin.tStart = 0;
822 pPinImpl->pin.tStop = 0;
823 pPinImpl->pin.dRate = 1.0;
824 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
825 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
826 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
828 /* Output pin attributes */
829 pPinImpl->pMemInputPin = NULL;
830 pPinImpl->pFuncsTable = pBaseOutputFuncsTable;
832 return S_OK;
835 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)
837 BaseOutputPin * pPinImpl;
839 *ppPin = NULL;
841 if (pPinInfo->dir != PINDIR_OUTPUT)
843 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
844 return E_INVALIDARG;
847 assert(outputpin_size >= sizeof(BaseOutputPin));
848 assert(pBaseFuncsTable->pfnAttemptConnection);
850 pPinImpl = CoTaskMemAlloc(outputpin_size);
852 if (!pPinImpl)
853 return E_OUTOFMEMORY;
855 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseOutputFuncsTable, pCritSec, pPinImpl)))
857 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
858 return S_OK;
861 CoTaskMemFree(pPinImpl);
862 return E_FAIL;
865 /*** Input Pin implementation ***/
867 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
869 BaseInputPin *This = (BaseInputPin *)iface;
871 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
873 *ppv = NULL;
875 if (IsEqualIID(riid, &IID_IUnknown))
876 *ppv = iface;
877 else if (IsEqualIID(riid, &IID_IPin))
878 *ppv = iface;
879 else if (IsEqualIID(riid, &IID_IMemInputPin))
880 *ppv = &This->lpVtblMemInput;
881 else if (IsEqualIID(riid, &IID_IMediaSeeking))
883 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
886 if (*ppv)
888 IUnknown_AddRef((IUnknown *)(*ppv));
889 return S_OK;
892 FIXME("No interface for %s!\n", debugstr_guid(riid));
894 return E_NOINTERFACE;
897 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
899 BaseInputPin *This = (BaseInputPin *)iface;
900 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
902 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
904 if (!refCount)
906 FreeMediaType(&This->pin.mtCurrent);
907 if (This->pAllocator)
908 IMemAllocator_Release(This->pAllocator);
909 This->pAllocator = NULL;
910 This->pin.lpVtbl = NULL;
911 CoTaskMemFree(This);
912 return 0;
914 else
915 return refCount;
918 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
920 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
922 return E_UNEXPECTED;
926 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
928 BaseInputPin *This = (BaseInputPin *)iface;
929 PIN_DIRECTION pindirReceive;
930 HRESULT hr = S_OK;
932 TRACE("(%p, %p)\n", pReceivePin, pmt);
933 dump_AM_MEDIA_TYPE(pmt);
935 EnterCriticalSection(This->pin.pCritSec);
937 if (This->pin.pConnectedTo)
938 hr = VFW_E_ALREADY_CONNECTED;
940 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
941 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
942 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
944 if (SUCCEEDED(hr))
946 IPin_QueryDirection(pReceivePin, &pindirReceive);
948 if (pindirReceive != PINDIR_OUTPUT)
950 ERR("Can't connect from non-output pin\n");
951 hr = VFW_E_INVALID_DIRECTION;
955 if (SUCCEEDED(hr))
957 CopyMediaType(&This->pin.mtCurrent, pmt);
958 This->pin.pConnectedTo = pReceivePin;
959 IPin_AddRef(pReceivePin);
962 LeaveCriticalSection(This->pin.pCritSec);
964 return hr;
967 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
969 return IPin_EndOfStream( pin );
972 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
974 BaseInputPin *This = (BaseInputPin *)iface;
976 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
978 return (This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) == S_OK ? S_OK : S_FALSE);
981 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
983 HRESULT hr = S_OK;
984 BaseInputPin *This = (BaseInputPin *)iface;
986 TRACE("(%p)\n", This);
988 EnterCriticalSection(This->pin.pCritSec);
989 if (This->flushing)
990 hr = S_FALSE;
991 else
992 This->end_of_stream = 1;
993 LeaveCriticalSection(This->pin.pCritSec);
995 if (hr == S_OK)
996 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
997 return hr;
1000 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1002 return IPin_BeginFlush( pin );
1005 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1007 BaseInputPin *This = (BaseInputPin *)iface;
1008 HRESULT hr;
1009 TRACE("() semi-stub\n");
1011 EnterCriticalSection(This->pin.pCritSec);
1012 This->flushing = 1;
1014 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1015 LeaveCriticalSection(This->pin.pCritSec);
1017 return hr;
1020 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1022 return IPin_EndFlush( pin );
1025 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1027 BaseInputPin *This = (BaseInputPin *)iface;
1028 HRESULT hr;
1029 TRACE("(%p)\n", This);
1031 EnterCriticalSection(This->pin.pCritSec);
1032 This->flushing = This->end_of_stream = 0;
1034 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1035 LeaveCriticalSection(This->pin.pCritSec);
1037 return hr;
1040 typedef struct newsegmentargs
1042 REFERENCE_TIME tStart, tStop;
1043 double rate;
1044 } newsegmentargs;
1046 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1048 newsegmentargs *args = data;
1049 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1052 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1054 BaseInputPin *This = (BaseInputPin *)iface;
1055 newsegmentargs args;
1057 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1059 args.tStart = This->pin.tStart = tStart;
1060 args.tStop = This->pin.tStop = tStop;
1061 args.rate = This->pin.dRate = dRate;
1063 return SendFurther( iface, deliver_newsegment, &args, NULL );
1066 static const IPinVtbl InputPin_Vtbl =
1068 BaseInputPinImpl_QueryInterface,
1069 BasePinImpl_AddRef,
1070 BaseInputPinImpl_Release,
1071 BaseInputPinImpl_Connect,
1072 BaseInputPinImpl_ReceiveConnection,
1073 BasePinImpl_Disconnect,
1074 BasePinImpl_ConnectedTo,
1075 BasePinImpl_ConnectionMediaType,
1076 BasePinImpl_QueryPinInfo,
1077 BasePinImpl_QueryDirection,
1078 BasePinImpl_QueryId,
1079 BaseInputPinImpl_QueryAccept,
1080 BasePinImpl_EnumMediaTypes,
1081 BasePinImpl_QueryInternalConnections,
1082 BaseInputPinImpl_EndOfStream,
1083 BaseInputPinImpl_BeginFlush,
1084 BaseInputPinImpl_EndFlush,
1085 BaseInputPinImpl_NewSegment
1088 /*** IMemInputPin implementation ***/
1090 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1092 return (BaseInputPin *)((char*)iface - FIELD_OFFSET(BaseInputPin, lpVtblMemInput));
1095 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1097 BaseInputPin *This = impl_from_IMemInputPin(iface);
1099 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
1102 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1104 BaseInputPin *This = impl_from_IMemInputPin(iface);
1106 return IPin_AddRef((IPin *)&This->pin);
1109 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1111 BaseInputPin *This = impl_from_IMemInputPin(iface);
1113 return IPin_Release((IPin *)&This->pin);
1116 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1118 BaseInputPin *This = impl_from_IMemInputPin(iface);
1120 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1122 *ppAllocator = This->pAllocator;
1123 if (*ppAllocator)
1124 IMemAllocator_AddRef(*ppAllocator);
1126 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1129 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1131 BaseInputPin *This = impl_from_IMemInputPin(iface);
1133 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1135 if (bReadOnly)
1136 FIXME("Read only flag not handled yet!\n");
1138 /* FIXME: Should we release the allocator on disconnection? */
1139 if (!pAllocator)
1141 WARN("Null allocator\n");
1142 return E_POINTER;
1145 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1146 return E_FAIL;
1148 if (This->pAllocator)
1149 IMemAllocator_Release(This->pAllocator);
1150 This->pAllocator = pAllocator;
1151 if (This->pAllocator)
1152 IMemAllocator_AddRef(This->pAllocator);
1154 return S_OK;
1157 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1159 BaseInputPin *This = impl_from_IMemInputPin(iface);
1161 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1163 /* override this method if you have any specific requirements */
1165 return E_NOTIMPL;
1168 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1170 BaseInputPin *This = impl_from_IMemInputPin(iface);
1171 HRESULT hr = S_FALSE;
1173 /* this trace commented out for performance reasons */
1174 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1175 if (This->pFuncsTable->pfnReceive)
1176 hr = This->pFuncsTable->pfnReceive(This, pSample);
1177 return hr;
1180 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1182 HRESULT hr = S_OK;
1183 BaseInputPin *This = impl_from_IMemInputPin(iface);
1185 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1187 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1189 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1190 if (hr != S_OK)
1191 break;
1194 return hr;
1197 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1199 BaseInputPin *This = impl_from_IMemInputPin(iface);
1201 TRACE("(%p/%p)->()\n", This, iface);
1203 return S_OK;
1206 static const IMemInputPinVtbl MemInputPin_Vtbl =
1208 MemInputPin_QueryInterface,
1209 MemInputPin_AddRef,
1210 MemInputPin_Release,
1211 MemInputPin_GetAllocator,
1212 MemInputPin_NotifyAllocator,
1213 MemInputPin_GetAllocatorRequirements,
1214 MemInputPin_Receive,
1215 MemInputPin_ReceiveMultiple,
1216 MemInputPin_ReceiveCanBlock
1219 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1220 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1221 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1223 TRACE("\n");
1225 /* Common attributes */
1226 pPinImpl->pin.refCount = 1;
1227 pPinImpl->pin.pConnectedTo = NULL;
1228 pPinImpl->pin.pCritSec = pCritSec;
1229 pPinImpl->pin.tStart = 0;
1230 pPinImpl->pin.tStop = 0;
1231 pPinImpl->pin.dRate = 1.0;
1232 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1233 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1234 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
1236 /* Input pin attributes */
1237 pPinImpl->pFuncsTable = pBaseInputFuncsTable;
1238 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1239 if (pPinImpl->preferred_allocator)
1240 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1241 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1242 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1243 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1245 return S_OK;
1248 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1249 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1250 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1252 BaseInputPin * pPinImpl;
1254 *ppPin = NULL;
1256 assert(pBaseFuncsTable->pfnCheckMediaType);
1258 if (pPinInfo->dir != PINDIR_INPUT)
1260 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1261 return E_INVALIDARG;
1264 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1266 if (!pPinImpl)
1267 return E_OUTOFMEMORY;
1269 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseInputFuncsTable, pCritSec, allocator, pPinImpl)))
1271 *ppPin = (IPin *)pPinImpl;
1272 return S_OK;
1275 CoTaskMemFree(pPinImpl);
1276 return E_FAIL;