comctl32/tests: Make test_combo_WS_VSCROLL() static.
[wine.git] / dlls / strmbase / pin.c
blob48f24973d69c13895e0fecd4a679de4776a83e22
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 IMemInputPinVtbl MemInputPin_Vtbl;
36 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
38 static inline BasePin *impl_from_IPin( IPin *iface )
40 return CONTAINING_RECORD(iface, BasePin, IPin_iface);
43 /** Helper function, there are a lot of places where the error code is inherited
44 * The following rules apply:
46 * Return the first received error code (E_NOTIMPL is ignored)
47 * If no errors occur: return the first received non-error-code that isn't S_OK
49 static HRESULT updatehres( HRESULT original, HRESULT new )
51 if (FAILED( original ) || new == E_NOTIMPL)
52 return original;
54 if (FAILED( new ) || original == S_OK)
55 return new;
57 return original;
60 /** Sends a message from a pin further to other, similar pins
61 * fnMiddle is called on each pin found further on the stream.
62 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
64 * If the pin given is an input pin, the message will be sent downstream to other input pins
65 * If the pin given is an output pin, the message will be sent upstream to other output pins
67 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
69 PIN_INFO pin_info;
70 ULONG amount = 0;
71 HRESULT hr = S_OK;
72 HRESULT hr_return = S_OK;
73 IEnumPins *enumpins = NULL;
74 BOOL foundend = TRUE;
75 PIN_DIRECTION from_dir;
77 IPin_QueryDirection( from, &from_dir );
79 hr = IPin_QueryInternalConnections( from, NULL, &amount );
80 if (hr != E_NOTIMPL && amount)
81 FIXME("Use QueryInternalConnections!\n");
83 pin_info.pFilter = NULL;
84 hr = IPin_QueryPinInfo( from, &pin_info );
85 if (FAILED(hr))
86 goto out;
88 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
89 if (FAILED(hr))
90 goto out;
92 hr = IEnumPins_Reset( enumpins );
93 while (hr == S_OK) {
94 IPin *pin = NULL;
95 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
96 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
98 hr = IEnumPins_Reset( enumpins );
99 continue;
101 if (pin)
103 PIN_DIRECTION dir;
105 IPin_QueryDirection( pin, &dir );
106 if (dir != from_dir)
108 IPin *connected = NULL;
110 foundend = FALSE;
111 IPin_ConnectedTo( pin, &connected );
112 if (connected)
114 HRESULT hr_local;
116 hr_local = fnMiddle( connected, arg );
117 hr_return = updatehres( hr_return, hr_local );
118 IPin_Release(connected);
121 IPin_Release( pin );
123 else
125 hr = S_OK;
126 break;
130 if (!foundend)
131 hr = hr_return;
132 else if (fnEnd) {
133 HRESULT hr_local;
135 hr_local = fnEnd( from, arg );
136 hr_return = updatehres( hr_return, hr_local );
138 IEnumPins_Release(enumpins);
140 out:
141 if (pin_info.pFilter)
142 IBaseFilter_Release( pin_info.pFilter );
143 return hr;
146 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
148 /* Tempting to just do a memcpy, but the name field is
149 128 characters long! We will probably never exceed 10
150 most of the time, so we are better off copying
151 each field manually */
152 strcpyW(pDest->achName, pSrc->achName);
153 pDest->dir = pSrc->dir;
154 pDest->pFilter = pSrc->pFilter;
157 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
159 if (!pmt)
160 return;
161 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
164 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
166 TRACE("pmt1: ");
167 dump_AM_MEDIA_TYPE(pmt1);
168 TRACE("pmt2: ");
169 dump_AM_MEDIA_TYPE(pmt2);
170 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
171 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
174 /*** Common Base Pin function */
175 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
177 if (iPosition < 0)
178 return E_INVALIDARG;
179 return VFW_S_NO_MORE_ITEMS;
182 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
184 return 1;
187 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
189 BasePin *This = impl_from_IPin(iface);
190 ULONG refCount = InterlockedIncrement(&This->refCount);
192 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
194 return refCount;
197 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
199 HRESULT hr;
200 BasePin *This = impl_from_IPin(iface);
202 TRACE("()\n");
204 EnterCriticalSection(This->pCritSec);
206 if (This->pConnectedTo)
208 IPin_Release(This->pConnectedTo);
209 This->pConnectedTo = NULL;
210 FreeMediaType(&This->mtCurrent);
211 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
212 hr = S_OK;
214 else
215 hr = S_FALSE;
217 LeaveCriticalSection(This->pCritSec);
219 return hr;
222 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
224 HRESULT hr;
225 BasePin *This = impl_from_IPin(iface);
227 TRACE("(%p)\n", ppPin);
229 EnterCriticalSection(This->pCritSec);
231 if (This->pConnectedTo)
233 *ppPin = This->pConnectedTo;
234 IPin_AddRef(*ppPin);
235 hr = S_OK;
237 else
239 hr = VFW_E_NOT_CONNECTED;
240 *ppPin = NULL;
243 LeaveCriticalSection(This->pCritSec);
245 return hr;
248 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
250 HRESULT hr;
251 BasePin *This = impl_from_IPin(iface);
253 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
255 EnterCriticalSection(This->pCritSec);
257 if (This->pConnectedTo)
259 CopyMediaType(pmt, &This->mtCurrent);
260 hr = S_OK;
262 else
264 ZeroMemory(pmt, sizeof(*pmt));
265 hr = VFW_E_NOT_CONNECTED;
268 LeaveCriticalSection(This->pCritSec);
270 return hr;
273 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
275 BasePin *This = impl_from_IPin(iface);
277 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
279 Copy_PinInfo(pInfo, &This->pinInfo);
280 IBaseFilter_AddRef(pInfo->pFilter);
282 return S_OK;
285 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
287 BasePin *This = impl_from_IPin(iface);
289 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
291 *pPinDir = This->pinInfo.dir;
293 return S_OK;
296 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
298 BasePin *This = impl_from_IPin(iface);
300 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
302 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
303 if (!*Id)
304 return E_OUTOFMEMORY;
306 strcpyW(*Id, This->pinInfo.achName);
308 return S_OK;
311 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
313 TRACE("(%p)->(%p)\n", iface, pmt);
315 return S_OK;
318 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
320 BasePin *This = impl_from_IPin(iface);
322 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
324 /* override this method to allow enumeration of your types */
326 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
329 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
331 BasePin *This = impl_from_IPin(iface);
333 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
335 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
338 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
340 BasePin *This = impl_from_IPin(iface);
342 TRACE("(%s, %s, %e)\n", wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
344 This->tStart = tStart;
345 This->tStop = tStop;
346 This->dRate = dRate;
348 return S_OK;
351 /*** OutputPin implementation ***/
353 static inline BaseOutputPin *impl_BaseOutputPin_from_IPin( IPin *iface )
355 return CONTAINING_RECORD(iface, BaseOutputPin, pin.IPin_iface);
358 static inline BaseOutputPin *impl_BaseOutputPin_from_BasePin( BasePin *iface )
360 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
363 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
365 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
367 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
369 *ppv = NULL;
371 if (IsEqualIID(riid, &IID_IUnknown))
372 *ppv = iface;
373 else if (IsEqualIID(riid, &IID_IPin))
374 *ppv = iface;
375 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
376 IsEqualIID(riid, &IID_IQualityControl))
378 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
381 if (*ppv)
383 IUnknown_AddRef((IUnknown *)(*ppv));
384 return S_OK;
387 FIXME("No interface for %s!\n", debugstr_guid(riid));
389 return E_NOINTERFACE;
392 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
394 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
395 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
397 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
399 if (!refCount)
400 BaseOutputPin_Destroy(This);
402 return refCount;
405 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
407 HRESULT hr;
408 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
410 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
411 dump_AM_MEDIA_TYPE(pmt);
413 if (!pReceivePin)
414 return E_POINTER;
416 /* If we try to connect to ourselves, we will definitely deadlock.
417 * There are other cases where we could deadlock too, but this
418 * catches the obvious case */
419 assert(pReceivePin != iface);
421 EnterCriticalSection(This->pin.pCritSec);
423 /* if we have been a specific type to connect with, then we can either connect
424 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
425 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
426 hr = This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmt);
427 else
429 /* negotiate media type */
431 IEnumMediaTypes * pEnumCandidates;
432 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
434 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
436 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
438 /* try this filter's media types first */
439 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
441 assert(pmtCandidate);
442 dump_AM_MEDIA_TYPE(pmtCandidate);
443 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
444 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
445 assert(pmtCandidate->pbFormat);
446 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
447 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
449 hr = S_OK;
450 DeleteMediaType(pmtCandidate);
451 break;
453 DeleteMediaType(pmtCandidate);
454 pmtCandidate = NULL;
456 IEnumMediaTypes_Release(pEnumCandidates);
459 /* then try receiver filter's media types */
460 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
462 ULONG fetched;
464 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
466 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, &fetched))
468 assert(pmtCandidate);
469 dump_AM_MEDIA_TYPE(pmtCandidate);
470 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
471 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
473 hr = S_OK;
474 DeleteMediaType(pmtCandidate);
475 break;
477 DeleteMediaType(pmtCandidate);
478 pmtCandidate = NULL;
479 } /* while */
480 IEnumMediaTypes_Release(pEnumCandidates);
481 } /* if not found */
482 } /* if negotiate media type */
483 } /* if succeeded */
484 LeaveCriticalSection(This->pin.pCritSec);
486 TRACE(" -- %x\n", hr);
487 return hr;
490 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
492 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
494 return E_UNEXPECTED;
497 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
499 HRESULT hr;
500 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
502 TRACE("()\n");
504 EnterCriticalSection(This->pin.pCritSec);
506 if (This->pMemInputPin)
508 IMemInputPin_Release(This->pMemInputPin);
509 This->pMemInputPin = NULL;
511 if (This->pin.pConnectedTo)
513 IPin_Release(This->pin.pConnectedTo);
514 This->pin.pConnectedTo = NULL;
515 FreeMediaType(&This->pin.mtCurrent);
516 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
517 hr = S_OK;
519 else
520 hr = S_FALSE;
522 LeaveCriticalSection(This->pin.pCritSec);
524 return hr;
527 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
529 TRACE("()\n");
531 /* not supposed to do anything in an output pin */
533 return E_UNEXPECTED;
536 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
538 TRACE("(%p)->()\n", iface);
540 /* not supposed to do anything in an output pin */
542 return E_UNEXPECTED;
545 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
547 TRACE("(%p)->()\n", iface);
549 /* not supposed to do anything in an output pin */
551 return E_UNEXPECTED;
554 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
556 HRESULT hr;
558 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
560 if (!This->pin.pConnectedTo)
561 hr = VFW_E_NOT_CONNECTED;
562 else
564 hr = IMemAllocator_GetBuffer(This->pAllocator, ppSample, tStart, tStop, dwFlags);
566 if (SUCCEEDED(hr))
567 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
570 return hr;
573 /* replaces OutputPin_SendSample */
574 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
576 IMemInputPin * pMemConnected = NULL;
577 PIN_INFO pinInfo;
578 HRESULT hr;
580 EnterCriticalSection(This->pin.pCritSec);
582 if (!This->pin.pConnectedTo || !This->pMemInputPin)
583 hr = VFW_E_NOT_CONNECTED;
584 else
586 /* we don't have the lock held when using This->pMemInputPin,
587 * so we need to AddRef it to stop it being deleted while we are
588 * using it. Same with its filter. */
589 pMemConnected = This->pMemInputPin;
590 IMemInputPin_AddRef(pMemConnected);
591 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
594 LeaveCriticalSection(This->pin.pCritSec);
596 if (SUCCEEDED(hr))
598 /* NOTE: if we are in a critical section when Receive is called
599 * then it causes some problems (most notably with the native Video
600 * Renderer) if we are re-entered for whatever reason */
601 hr = IMemInputPin_Receive(pMemConnected, pSample);
603 /* If the filter's destroyed, tell upstream to stop sending data */
604 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
605 hr = S_FALSE;
607 if (pMemConnected)
608 IMemInputPin_Release(pMemConnected);
610 return hr;
613 /* replaces OutputPin_CommitAllocator */
614 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
616 HRESULT hr;
618 TRACE("(%p)->()\n", This);
620 EnterCriticalSection(This->pin.pCritSec);
622 if (!This->pin.pConnectedTo || !This->pMemInputPin)
623 hr = VFW_E_NOT_CONNECTED;
624 else
625 hr = IMemAllocator_Commit(This->pAllocator);
627 LeaveCriticalSection(This->pin.pCritSec);
629 TRACE("--> %08x\n", hr);
630 return hr;
633 /* replaces OutputPin_DecommitAllocator */
634 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
636 HRESULT hr;
638 TRACE("(%p)->()\n", This);
640 EnterCriticalSection(This->pin.pCritSec);
642 if (!This->pin.pConnectedTo || !This->pMemInputPin)
643 hr = VFW_E_NOT_CONNECTED;
644 else
645 hr = IMemAllocator_Decommit(This->pAllocator);
647 LeaveCriticalSection(This->pin.pCritSec);
649 TRACE("--> %08x\n", hr);
650 return hr;
653 /* replaces OutputPin_DeliverDisconnect */
654 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
656 HRESULT hr;
658 TRACE("(%p)->()\n", This);
660 EnterCriticalSection(This->pin.pCritSec);
662 if (!This->pin.pConnectedTo || !This->pMemInputPin)
663 hr = VFW_E_NOT_CONNECTED;
664 else
666 hr = IMemAllocator_Decommit(This->pAllocator);
668 if (SUCCEEDED(hr))
669 hr = IPin_Disconnect(This->pin.pConnectedTo);
671 IPin_Disconnect(&This->pin.IPin_iface);
673 LeaveCriticalSection(This->pin.pCritSec);
675 return hr;
678 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
680 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
683 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
685 HRESULT hr;
687 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
689 if (hr == VFW_E_NO_ALLOCATOR)
690 /* Input pin provides no allocator, use standard memory allocator */
691 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
693 if (SUCCEEDED(hr))
695 ALLOCATOR_PROPERTIES rProps;
696 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
698 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
699 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
702 if (SUCCEEDED(hr))
703 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
705 return hr;
708 /*** The Construct functions ***/
710 /* Function called as a helper to IPin_Connect */
711 /* specific AM_MEDIA_TYPE - it cannot be NULL */
712 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
714 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
715 HRESULT hr;
716 IMemAllocator * pMemAlloc = NULL;
718 TRACE("(%p, %p)\n", pReceivePin, pmt);
719 dump_AM_MEDIA_TYPE(pmt);
721 /* FIXME: call queryacceptproc */
723 This->pin.pConnectedTo = pReceivePin;
724 IPin_AddRef(pReceivePin);
725 CopyMediaType(&This->pin.mtCurrent, pmt);
727 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
729 /* get the IMemInputPin interface we will use to deliver samples to the
730 * connected pin */
731 if (SUCCEEDED(hr))
733 This->pMemInputPin = NULL;
734 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
736 if (SUCCEEDED(hr))
738 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
739 if (SUCCEEDED(hr))
740 This->pAllocator = pMemAlloc;
741 else if (pMemAlloc)
742 IMemAllocator_Release(pMemAlloc);
745 /* break connection if we couldn't get the allocator */
746 if (FAILED(hr))
748 if (This->pMemInputPin)
749 IMemInputPin_Release(This->pMemInputPin);
750 This->pMemInputPin = NULL;
752 IPin_Disconnect(pReceivePin);
756 if (FAILED(hr))
758 IPin_Release(This->pin.pConnectedTo);
759 This->pin.pConnectedTo = NULL;
760 FreeMediaType(&This->pin.mtCurrent);
763 TRACE(" -- %x\n", hr);
764 return hr;
767 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
769 TRACE("\n");
771 /* Common attributes */
772 pPinImpl->pin.IPin_iface.lpVtbl = OutputPin_Vtbl;
773 pPinImpl->pin.refCount = 1;
774 pPinImpl->pin.pConnectedTo = NULL;
775 pPinImpl->pin.pCritSec = pCritSec;
776 pPinImpl->pin.tStart = 0;
777 pPinImpl->pin.tStop = 0;
778 pPinImpl->pin.dRate = 1.0;
779 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
780 pPinImpl->pin.pFuncsTable = &vtbl->base;
781 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
783 /* Output pin attributes */
784 pPinImpl->pMemInputPin = NULL;
785 pPinImpl->pAllocator = NULL;
786 pPinImpl->pFuncsTable = vtbl;
788 return S_OK;
791 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
793 BaseOutputPin * pPinImpl;
795 *ppPin = NULL;
797 if (pPinInfo->dir != PINDIR_OUTPUT)
799 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
800 return E_INVALIDARG;
803 assert(outputpin_size >= sizeof(BaseOutputPin));
804 assert(vtbl->base.pfnAttemptConnection);
806 pPinImpl = CoTaskMemAlloc(outputpin_size);
808 if (!pPinImpl)
809 return E_OUTOFMEMORY;
811 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, vtbl, pCritSec, pPinImpl)))
813 *ppPin = &pPinImpl->pin.IPin_iface;
814 return S_OK;
817 CoTaskMemFree(pPinImpl);
818 return E_FAIL;
821 HRESULT WINAPI BaseOutputPin_Destroy(BaseOutputPin *This)
823 FreeMediaType(&This->pin.mtCurrent);
824 if (This->pAllocator)
825 IMemAllocator_Release(This->pAllocator);
826 This->pAllocator = NULL;
827 CoTaskMemFree(This);
828 return S_OK;
831 /*** Input Pin implementation ***/
833 static inline BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
835 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
838 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
840 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
842 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
844 *ppv = NULL;
846 if (IsEqualIID(riid, &IID_IUnknown))
847 *ppv = iface;
848 else if (IsEqualIID(riid, &IID_IPin))
849 *ppv = iface;
850 else if (IsEqualIID(riid, &IID_IMemInputPin))
851 *ppv = &This->IMemInputPin_iface;
852 else if (IsEqualIID(riid, &IID_IMediaSeeking))
854 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
857 if (*ppv)
859 IUnknown_AddRef((IUnknown *)(*ppv));
860 return S_OK;
863 FIXME("No interface for %s!\n", debugstr_guid(riid));
865 return E_NOINTERFACE;
868 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
870 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
871 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
873 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
875 if (!refCount)
876 BaseInputPin_Destroy(This);
878 return refCount;
881 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
883 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
885 return E_UNEXPECTED;
889 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
891 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
892 PIN_DIRECTION pindirReceive;
893 HRESULT hr = S_OK;
895 TRACE("(%p, %p)\n", pReceivePin, pmt);
896 dump_AM_MEDIA_TYPE(pmt);
898 EnterCriticalSection(This->pin.pCritSec);
900 if (This->pin.pConnectedTo)
901 hr = VFW_E_ALREADY_CONNECTED;
903 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) != S_OK)
904 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
905 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
907 if (SUCCEEDED(hr))
909 IPin_QueryDirection(pReceivePin, &pindirReceive);
911 if (pindirReceive != PINDIR_OUTPUT)
913 ERR("Can't connect from non-output pin\n");
914 hr = VFW_E_INVALID_DIRECTION;
918 if (SUCCEEDED(hr))
920 CopyMediaType(&This->pin.mtCurrent, pmt);
921 This->pin.pConnectedTo = pReceivePin;
922 IPin_AddRef(pReceivePin);
925 LeaveCriticalSection(This->pin.pCritSec);
927 return hr;
930 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
932 return IPin_EndOfStream( pin );
935 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
937 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
939 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
941 return (This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) == S_OK ? S_OK : S_FALSE);
944 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
946 HRESULT hr = S_OK;
947 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
949 TRACE("(%p)\n", This);
951 EnterCriticalSection(This->pin.pCritSec);
952 if (This->flushing)
953 hr = S_FALSE;
954 else
955 This->end_of_stream = TRUE;
956 LeaveCriticalSection(This->pin.pCritSec);
958 if (hr == S_OK)
959 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
960 return hr;
963 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
965 return IPin_BeginFlush( pin );
968 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
970 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
971 HRESULT hr;
972 TRACE("() semi-stub\n");
974 EnterCriticalSection(This->pin.pCritSec);
975 This->flushing = TRUE;
977 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
978 LeaveCriticalSection(This->pin.pCritSec);
980 return hr;
983 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
985 return IPin_EndFlush( pin );
988 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
990 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
991 HRESULT hr;
992 TRACE("(%p)\n", This);
994 EnterCriticalSection(This->pin.pCritSec);
995 This->flushing = This->end_of_stream = FALSE;
997 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
998 LeaveCriticalSection(This->pin.pCritSec);
1000 return hr;
1003 typedef struct newsegmentargs
1005 REFERENCE_TIME tStart, tStop;
1006 double rate;
1007 } newsegmentargs;
1009 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1011 newsegmentargs *args = data;
1012 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1015 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1017 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1018 newsegmentargs args;
1020 TRACE("(%s, %s, %e)\n", wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1022 args.tStart = This->pin.tStart = tStart;
1023 args.tStop = This->pin.tStop = tStop;
1024 args.rate = This->pin.dRate = dRate;
1026 return SendFurther( iface, deliver_newsegment, &args, NULL );
1029 /*** IMemInputPin implementation ***/
1031 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1033 return CONTAINING_RECORD(iface, BaseInputPin, IMemInputPin_iface);
1036 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1038 BaseInputPin *This = impl_from_IMemInputPin(iface);
1040 return IPin_QueryInterface(&This->pin.IPin_iface, riid, ppv);
1043 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1045 BaseInputPin *This = impl_from_IMemInputPin(iface);
1047 return IPin_AddRef(&This->pin.IPin_iface);
1050 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1052 BaseInputPin *This = impl_from_IMemInputPin(iface);
1054 return IPin_Release(&This->pin.IPin_iface);
1057 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1059 BaseInputPin *This = impl_from_IMemInputPin(iface);
1061 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1063 *ppAllocator = This->pAllocator;
1064 if (*ppAllocator)
1065 IMemAllocator_AddRef(*ppAllocator);
1067 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1070 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1072 BaseInputPin *This = impl_from_IMemInputPin(iface);
1074 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1076 if (bReadOnly)
1077 FIXME("Read only flag not handled yet!\n");
1079 /* FIXME: Should we release the allocator on disconnection? */
1080 if (!pAllocator)
1082 WARN("Null allocator\n");
1083 return E_POINTER;
1086 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1087 return E_FAIL;
1089 if (This->pAllocator)
1090 IMemAllocator_Release(This->pAllocator);
1091 This->pAllocator = pAllocator;
1092 if (This->pAllocator)
1093 IMemAllocator_AddRef(This->pAllocator);
1095 return S_OK;
1098 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1100 BaseInputPin *This = impl_from_IMemInputPin(iface);
1102 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1104 /* override this method if you have any specific requirements */
1106 return E_NOTIMPL;
1109 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1111 BaseInputPin *This = impl_from_IMemInputPin(iface);
1112 HRESULT hr = S_FALSE;
1114 /* this trace commented out for performance reasons */
1115 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1116 if (This->pFuncsTable->pfnReceive)
1117 hr = This->pFuncsTable->pfnReceive(This, pSample);
1118 return hr;
1121 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1123 HRESULT hr = S_OK;
1124 BaseInputPin *This = impl_from_IMemInputPin(iface);
1126 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1128 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1130 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1131 if (hr != S_OK)
1132 break;
1135 return hr;
1138 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1140 BaseInputPin *This = impl_from_IMemInputPin(iface);
1142 TRACE("(%p/%p)->()\n", This, iface);
1144 return S_OK;
1147 static const IMemInputPinVtbl MemInputPin_Vtbl =
1149 MemInputPin_QueryInterface,
1150 MemInputPin_AddRef,
1151 MemInputPin_Release,
1152 MemInputPin_GetAllocator,
1153 MemInputPin_NotifyAllocator,
1154 MemInputPin_GetAllocatorRequirements,
1155 MemInputPin_Receive,
1156 MemInputPin_ReceiveMultiple,
1157 MemInputPin_ReceiveCanBlock
1160 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1161 const BaseInputPinFuncTable* vtbl,
1162 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1164 TRACE("\n");
1166 /* Common attributes */
1167 pPinImpl->pin.refCount = 1;
1168 pPinImpl->pin.pConnectedTo = NULL;
1169 pPinImpl->pin.pCritSec = pCritSec;
1170 pPinImpl->pin.tStart = 0;
1171 pPinImpl->pin.tStop = 0;
1172 pPinImpl->pin.dRate = 1.0;
1173 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1174 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1175 pPinImpl->pin.pFuncsTable = &vtbl->base;
1177 /* Input pin attributes */
1178 pPinImpl->pFuncsTable = vtbl;
1179 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1180 if (pPinImpl->preferred_allocator)
1181 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1182 pPinImpl->pin.IPin_iface.lpVtbl = InputPin_Vtbl;
1183 pPinImpl->IMemInputPin_iface.lpVtbl = &MemInputPin_Vtbl;
1184 pPinImpl->flushing = pPinImpl->end_of_stream = FALSE;
1186 return S_OK;
1189 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, LONG inputpin_size, const PIN_INFO * pPinInfo,
1190 const BaseInputPinFuncTable* vtbl,
1191 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1193 BaseInputPin * pPinImpl;
1195 *ppPin = NULL;
1197 assert(inputpin_size >= sizeof(BaseInputPin));
1198 assert(vtbl->base.pfnCheckMediaType);
1200 if (pPinInfo->dir != PINDIR_INPUT)
1202 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1203 return E_INVALIDARG;
1206 pPinImpl = CoTaskMemAlloc(inputpin_size);
1208 if (!pPinImpl)
1209 return E_OUTOFMEMORY;
1211 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, vtbl, pCritSec, allocator, pPinImpl)))
1213 *ppPin = &pPinImpl->pin.IPin_iface;
1214 return S_OK;
1217 CoTaskMemFree(pPinImpl);
1218 return E_FAIL;
1221 HRESULT WINAPI BaseInputPin_Destroy(BaseInputPin *This)
1223 FreeMediaType(&This->pin.mtCurrent);
1224 if (This->pAllocator)
1225 IMemAllocator_Release(This->pAllocator);
1226 This->pAllocator = NULL;
1227 This->pin.IPin_iface.lpVtbl = NULL;
1228 CoTaskMemFree(This);
1229 return S_OK;