mpr: Implement WNetClearConnections().
[wine.git] / dlls / strmbase / pin.c
bloba100ae114f9d3a7ea965eb379f2a55b160a0ac88
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 /* avoid copying uninitialized data */
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 = impl_from_IPin(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 = impl_from_IPin(iface);
199 TRACE("(%p)->()\n", This);
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 = impl_from_IPin(iface);
224 TRACE("(%p)->(%p)\n", This, 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 = impl_from_IPin(iface);
250 TRACE("(%p)->(%p)\n", This, 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 = impl_from_IPin(iface);
274 TRACE("(%p)->(%p)\n", This, 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 = impl_from_IPin(iface);
286 TRACE("(%p)->(%p)\n", This, pPinDir);
288 *pPinDir = This->pinInfo.dir;
290 return S_OK;
293 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
295 BasePin *This = impl_from_IPin(iface);
297 TRACE("(%p)->(%p)\n", This, 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 BasePin *This = impl_from_IPin(iface);
312 TRACE("(%p)->(%p)\n", iface, pmt);
314 return (This->pFuncsTable->pfnCheckMediaType(This, pmt) == S_OK ? S_OK : S_FALSE);
317 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
319 BasePin *This = impl_from_IPin(iface);
321 TRACE("(%p)->(%p)\n", This, ppEnum);
323 /* override this method to allow enumeration of your types */
325 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
328 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
330 BasePin *This = impl_from_IPin(iface);
332 TRACE("(%p)->(%p, %p)\n", This, apPin, cPin);
334 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
337 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
339 BasePin *This = impl_from_IPin(iface);
341 TRACE("(%p)->(%s, %s, %e)\n", This, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
343 This->tStart = tStart;
344 This->tStop = tStop;
345 This->dRate = dRate;
347 return S_OK;
350 /*** OutputPin implementation ***/
352 static inline BaseOutputPin *impl_BaseOutputPin_from_IPin( IPin *iface )
354 return CONTAINING_RECORD(iface, BaseOutputPin, pin.IPin_iface);
357 static inline BaseOutputPin *impl_BaseOutputPin_from_BasePin( BasePin *iface )
359 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
362 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
364 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
366 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
368 *ppv = NULL;
370 if (IsEqualIID(riid, &IID_IUnknown))
371 *ppv = iface;
372 else if (IsEqualIID(riid, &IID_IPin))
373 *ppv = iface;
374 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
375 IsEqualIID(riid, &IID_IQualityControl))
377 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
380 if (*ppv)
382 IUnknown_AddRef((IUnknown *)(*ppv));
383 return S_OK;
386 FIXME("No interface for %s!\n", debugstr_guid(riid));
388 return E_NOINTERFACE;
391 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
393 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
394 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
396 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
398 if (!refCount)
399 BaseOutputPin_Destroy(This);
401 return refCount;
404 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
406 HRESULT hr;
407 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
409 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
410 dump_AM_MEDIA_TYPE(pmt);
412 if (!pReceivePin)
413 return E_POINTER;
415 /* If we try to connect to ourselves, we will definitely deadlock.
416 * There are other cases where we could deadlock too, but this
417 * catches the obvious case */
418 assert(pReceivePin != iface);
420 EnterCriticalSection(This->pin.pCritSec);
422 /* if we have been a specific type to connect with, then we can either connect
423 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
424 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
425 hr = This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmt);
426 else
428 /* negotiate media type */
430 IEnumMediaTypes * pEnumCandidates;
431 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
433 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
435 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
437 /* try this filter's media types first */
438 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
440 assert(pmtCandidate);
441 dump_AM_MEDIA_TYPE(pmtCandidate);
442 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
443 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
444 assert(pmtCandidate->pbFormat);
445 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
446 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
448 hr = S_OK;
449 DeleteMediaType(pmtCandidate);
450 break;
452 DeleteMediaType(pmtCandidate);
453 pmtCandidate = NULL;
455 IEnumMediaTypes_Release(pEnumCandidates);
458 /* then try receiver filter's media types */
459 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
461 ULONG fetched;
463 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
465 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, &fetched))
467 assert(pmtCandidate);
468 dump_AM_MEDIA_TYPE(pmtCandidate);
469 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
470 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
472 hr = S_OK;
473 DeleteMediaType(pmtCandidate);
474 break;
476 DeleteMediaType(pmtCandidate);
477 pmtCandidate = NULL;
478 } /* while */
479 IEnumMediaTypes_Release(pEnumCandidates);
480 } /* if not found */
481 } /* if negotiate media type */
482 } /* if succeeded */
483 LeaveCriticalSection(This->pin.pCritSec);
485 TRACE(" -- %x\n", hr);
486 return hr;
489 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin *iface, IPin *pin, const AM_MEDIA_TYPE *pmt)
491 ERR("(%p)->(%p, %p) incoming connection on an output pin!\n", iface, pin, pmt);
492 return E_UNEXPECTED;
495 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
497 HRESULT hr;
498 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
500 TRACE("(%p)->()\n", This);
502 EnterCriticalSection(This->pin.pCritSec);
504 if (This->pMemInputPin)
506 IMemInputPin_Release(This->pMemInputPin);
507 This->pMemInputPin = NULL;
509 if (This->pin.pConnectedTo)
511 IPin_Release(This->pin.pConnectedTo);
512 This->pin.pConnectedTo = NULL;
513 FreeMediaType(&This->pin.mtCurrent);
514 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
515 hr = S_OK;
517 else
518 hr = S_FALSE;
520 LeaveCriticalSection(This->pin.pCritSec);
522 return hr;
525 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
527 TRACE("(%p)->()\n", iface);
529 /* not supposed to do anything in an output pin */
531 return E_UNEXPECTED;
534 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
536 TRACE("(%p)->()\n", iface);
538 /* not supposed to do anything in an output pin */
540 return E_UNEXPECTED;
543 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
545 TRACE("(%p)->()\n", iface);
547 /* not supposed to do anything in an output pin */
549 return E_UNEXPECTED;
552 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
554 HRESULT hr;
556 TRACE("(%p)->(%p, %p, %p, %x)\n", This, ppSample, tStart, tStop, dwFlags);
558 if (!This->pin.pConnectedTo)
559 hr = VFW_E_NOT_CONNECTED;
560 else
562 hr = IMemAllocator_GetBuffer(This->pAllocator, ppSample, tStart, tStop, dwFlags);
564 if (SUCCEEDED(hr))
565 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
568 return hr;
571 /* replaces OutputPin_SendSample */
572 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
574 IMemInputPin * pMemConnected = NULL;
575 PIN_INFO pinInfo;
576 HRESULT hr;
578 EnterCriticalSection(This->pin.pCritSec);
580 if (!This->pin.pConnectedTo || !This->pMemInputPin)
581 hr = VFW_E_NOT_CONNECTED;
582 else
584 /* we don't have the lock held when using This->pMemInputPin,
585 * so we need to AddRef it to stop it being deleted while we are
586 * using it. Same with its filter. */
587 pMemConnected = This->pMemInputPin;
588 IMemInputPin_AddRef(pMemConnected);
589 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
592 LeaveCriticalSection(This->pin.pCritSec);
594 if (SUCCEEDED(hr))
596 /* NOTE: if we are in a critical section when Receive is called
597 * then it causes some problems (most notably with the native Video
598 * Renderer) if we are re-entered for whatever reason */
599 hr = IMemInputPin_Receive(pMemConnected, pSample);
601 /* If the filter's destroyed, tell upstream to stop sending data */
602 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
603 hr = S_FALSE;
605 if (pMemConnected)
606 IMemInputPin_Release(pMemConnected);
608 return hr;
611 /* replaces OutputPin_CommitAllocator */
612 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
614 HRESULT hr;
616 TRACE("(%p)->()\n", This);
618 EnterCriticalSection(This->pin.pCritSec);
620 if (!This->pin.pConnectedTo || !This->pMemInputPin)
621 hr = VFW_E_NOT_CONNECTED;
622 else
623 hr = IMemAllocator_Commit(This->pAllocator);
625 LeaveCriticalSection(This->pin.pCritSec);
627 TRACE("--> %08x\n", hr);
628 return hr;
631 /* replaces OutputPin_DecommitAllocator */
632 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
634 HRESULT hr;
636 TRACE("(%p)->()\n", This);
638 EnterCriticalSection(This->pin.pCritSec);
640 if (!This->pin.pConnectedTo || !This->pMemInputPin)
641 hr = VFW_E_NOT_CONNECTED;
642 else
643 hr = IMemAllocator_Decommit(This->pAllocator);
645 LeaveCriticalSection(This->pin.pCritSec);
647 TRACE("--> %08x\n", hr);
648 return hr;
651 /* replaces OutputPin_DeliverDisconnect */
652 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
654 HRESULT hr;
656 TRACE("(%p)->()\n", This);
658 EnterCriticalSection(This->pin.pCritSec);
660 if (!This->pin.pConnectedTo || !This->pMemInputPin)
661 hr = VFW_E_NOT_CONNECTED;
662 else
664 hr = IMemAllocator_Decommit(This->pAllocator);
666 if (SUCCEEDED(hr))
667 hr = IPin_Disconnect(This->pin.pConnectedTo);
669 IPin_Disconnect(&This->pin.IPin_iface);
671 LeaveCriticalSection(This->pin.pCritSec);
673 return hr;
676 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
678 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
681 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
683 HRESULT hr;
685 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
687 if (hr == VFW_E_NO_ALLOCATOR)
688 /* Input pin provides no allocator, use standard memory allocator */
689 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
691 if (SUCCEEDED(hr))
693 ALLOCATOR_PROPERTIES rProps;
694 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
696 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
697 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
700 if (SUCCEEDED(hr))
701 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
703 return hr;
706 /*** The Construct functions ***/
708 /* Function called as a helper to IPin_Connect */
709 /* specific AM_MEDIA_TYPE - it cannot be NULL */
710 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
712 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
713 HRESULT hr;
714 IMemAllocator * pMemAlloc = NULL;
716 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
717 dump_AM_MEDIA_TYPE(pmt);
719 if ((hr = This->pFuncsTable->base.pfnCheckMediaType(&This->pin, pmt)) != S_OK)
720 return hr;
722 This->pin.pConnectedTo = pReceivePin;
723 IPin_AddRef(pReceivePin);
724 CopyMediaType(&This->pin.mtCurrent, pmt);
726 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
728 /* get the IMemInputPin interface we will use to deliver samples to the
729 * connected pin */
730 if (SUCCEEDED(hr))
732 This->pMemInputPin = NULL;
733 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
735 if (SUCCEEDED(hr))
737 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
738 if (SUCCEEDED(hr))
739 This->pAllocator = pMemAlloc;
740 else if (pMemAlloc)
741 IMemAllocator_Release(pMemAlloc);
744 /* break connection if we couldn't get the allocator */
745 if (FAILED(hr))
747 if (This->pMemInputPin)
748 IMemInputPin_Release(This->pMemInputPin);
749 This->pMemInputPin = NULL;
751 IPin_Disconnect(pReceivePin);
755 if (FAILED(hr))
757 IPin_Release(This->pin.pConnectedTo);
758 This->pin.pConnectedTo = NULL;
759 FreeMediaType(&This->pin.mtCurrent);
762 TRACE(" -- %x\n", hr);
763 return hr;
766 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
768 TRACE("(%p)\n", pPinImpl);
770 /* Common attributes */
771 pPinImpl->pin.IPin_iface.lpVtbl = OutputPin_Vtbl;
772 pPinImpl->pin.refCount = 1;
773 pPinImpl->pin.pConnectedTo = NULL;
774 pPinImpl->pin.pCritSec = pCritSec;
775 pPinImpl->pin.tStart = 0;
776 pPinImpl->pin.tStop = 0;
777 pPinImpl->pin.dRate = 1.0;
778 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
779 pPinImpl->pin.pFuncsTable = &vtbl->base;
780 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
782 /* Output pin attributes */
783 pPinImpl->pMemInputPin = NULL;
784 pPinImpl->pAllocator = NULL;
785 pPinImpl->pFuncsTable = vtbl;
787 return S_OK;
790 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
792 BaseOutputPin * pPinImpl;
794 *ppPin = NULL;
796 if (pPinInfo->dir != PINDIR_OUTPUT)
798 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
799 return E_INVALIDARG;
802 assert(outputpin_size >= sizeof(BaseOutputPin));
803 assert(vtbl->base.pfnAttemptConnection);
805 pPinImpl = CoTaskMemAlloc(outputpin_size);
807 if (!pPinImpl)
808 return E_OUTOFMEMORY;
810 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, vtbl, pCritSec, pPinImpl)))
812 *ppPin = &pPinImpl->pin.IPin_iface;
813 return S_OK;
816 CoTaskMemFree(pPinImpl);
817 return E_FAIL;
820 HRESULT WINAPI BaseOutputPin_Destroy(BaseOutputPin *This)
822 FreeMediaType(&This->pin.mtCurrent);
823 if (This->pAllocator)
824 IMemAllocator_Release(This->pAllocator);
825 This->pAllocator = NULL;
826 CoTaskMemFree(This);
827 return S_OK;
830 /*** Input Pin implementation ***/
832 static inline BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
834 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
837 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
839 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
841 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
843 *ppv = NULL;
845 if (IsEqualIID(riid, &IID_IUnknown))
846 *ppv = iface;
847 else if (IsEqualIID(riid, &IID_IPin))
848 *ppv = iface;
849 else if (IsEqualIID(riid, &IID_IMemInputPin))
850 *ppv = &This->IMemInputPin_iface;
851 else if (IsEqualIID(riid, &IID_IMediaSeeking))
853 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
856 if (*ppv)
858 IUnknown_AddRef((IUnknown *)(*ppv));
859 return S_OK;
862 FIXME("No interface for %s!\n", debugstr_guid(riid));
864 return E_NOINTERFACE;
867 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
869 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
870 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
872 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
874 if (!refCount)
875 BaseInputPin_Destroy(This);
877 return refCount;
880 HRESULT WINAPI BaseInputPinImpl_Connect(IPin *iface, IPin *pin, const AM_MEDIA_TYPE *pmt)
882 ERR("(%p)->(%p, %p) outgoing connection on an input pin!\n", iface, pin, pmt);
883 return E_UNEXPECTED;
887 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
889 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
890 PIN_DIRECTION pindirReceive;
891 HRESULT hr = S_OK;
893 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
894 dump_AM_MEDIA_TYPE(pmt);
896 EnterCriticalSection(This->pin.pCritSec);
898 if (This->pin.pConnectedTo)
899 hr = VFW_E_ALREADY_CONNECTED;
901 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) != S_OK)
902 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
903 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
905 if (SUCCEEDED(hr))
907 IPin_QueryDirection(pReceivePin, &pindirReceive);
909 if (pindirReceive != PINDIR_OUTPUT)
911 ERR("Can't connect from non-output pin\n");
912 hr = VFW_E_INVALID_DIRECTION;
916 if (SUCCEEDED(hr))
918 CopyMediaType(&This->pin.mtCurrent, pmt);
919 This->pin.pConnectedTo = pReceivePin;
920 IPin_AddRef(pReceivePin);
923 LeaveCriticalSection(This->pin.pCritSec);
925 return hr;
928 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
930 return IPin_EndOfStream( pin );
933 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
935 HRESULT hr = S_OK;
936 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
938 TRACE("(%p)->()\n", This);
940 EnterCriticalSection(This->pin.pCritSec);
941 if (This->flushing)
942 hr = S_FALSE;
943 else
944 This->end_of_stream = TRUE;
945 LeaveCriticalSection(This->pin.pCritSec);
947 if (hr == S_OK)
948 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
949 return hr;
952 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
954 return IPin_BeginFlush( pin );
957 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
959 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
960 HRESULT hr;
961 TRACE("(%p) semi-stub\n", This);
963 EnterCriticalSection(This->pin.pCritSec);
964 This->flushing = TRUE;
966 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
967 LeaveCriticalSection(This->pin.pCritSec);
969 return hr;
972 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
974 return IPin_EndFlush( pin );
977 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
979 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
980 HRESULT hr;
981 TRACE("(%p)->()\n", This);
983 EnterCriticalSection(This->pin.pCritSec);
984 This->flushing = This->end_of_stream = FALSE;
986 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
987 LeaveCriticalSection(This->pin.pCritSec);
989 return hr;
992 typedef struct newsegmentargs
994 REFERENCE_TIME tStart, tStop;
995 double rate;
996 } newsegmentargs;
998 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1000 newsegmentargs *args = data;
1001 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1004 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1006 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1007 newsegmentargs args;
1009 TRACE("(%p)->(%s, %s, %e)\n", This, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1011 args.tStart = This->pin.tStart = tStart;
1012 args.tStop = This->pin.tStop = tStop;
1013 args.rate = This->pin.dRate = dRate;
1015 return SendFurther( iface, deliver_newsegment, &args, NULL );
1018 /*** IMemInputPin implementation ***/
1020 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1022 return CONTAINING_RECORD(iface, BaseInputPin, IMemInputPin_iface);
1025 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1027 BaseInputPin *This = impl_from_IMemInputPin(iface);
1029 return IPin_QueryInterface(&This->pin.IPin_iface, riid, ppv);
1032 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1034 BaseInputPin *This = impl_from_IMemInputPin(iface);
1036 return IPin_AddRef(&This->pin.IPin_iface);
1039 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1041 BaseInputPin *This = impl_from_IMemInputPin(iface);
1043 return IPin_Release(&This->pin.IPin_iface);
1046 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1048 BaseInputPin *This = impl_from_IMemInputPin(iface);
1050 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1052 *ppAllocator = This->pAllocator;
1053 if (*ppAllocator)
1054 IMemAllocator_AddRef(*ppAllocator);
1056 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1059 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1061 BaseInputPin *This = impl_from_IMemInputPin(iface);
1063 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1065 if (bReadOnly)
1066 FIXME("Read only flag not handled yet!\n");
1068 /* FIXME: Should we release the allocator on disconnection? */
1069 if (!pAllocator)
1071 WARN("Null allocator\n");
1072 return E_POINTER;
1075 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1076 return E_FAIL;
1078 if (This->pAllocator)
1079 IMemAllocator_Release(This->pAllocator);
1080 This->pAllocator = pAllocator;
1081 if (This->pAllocator)
1082 IMemAllocator_AddRef(This->pAllocator);
1084 return S_OK;
1087 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1089 BaseInputPin *This = impl_from_IMemInputPin(iface);
1091 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1093 /* override this method if you have any specific requirements */
1095 return E_NOTIMPL;
1098 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1100 BaseInputPin *This = impl_from_IMemInputPin(iface);
1101 HRESULT hr = S_FALSE;
1103 /* this trace commented out for performance reasons */
1104 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1105 if (This->pFuncsTable->pfnReceive)
1106 hr = This->pFuncsTable->pfnReceive(This, pSample);
1107 return hr;
1110 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1112 HRESULT hr = S_OK;
1113 BaseInputPin *This = impl_from_IMemInputPin(iface);
1115 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1117 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1119 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1120 if (hr != S_OK)
1121 break;
1124 return hr;
1127 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1129 BaseInputPin *This = impl_from_IMemInputPin(iface);
1131 TRACE("(%p/%p)->()\n", This, iface);
1133 return S_OK;
1136 static const IMemInputPinVtbl MemInputPin_Vtbl =
1138 MemInputPin_QueryInterface,
1139 MemInputPin_AddRef,
1140 MemInputPin_Release,
1141 MemInputPin_GetAllocator,
1142 MemInputPin_NotifyAllocator,
1143 MemInputPin_GetAllocatorRequirements,
1144 MemInputPin_Receive,
1145 MemInputPin_ReceiveMultiple,
1146 MemInputPin_ReceiveCanBlock
1149 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1150 const BaseInputPinFuncTable* vtbl,
1151 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1153 TRACE("(%p)\n", pPinImpl);
1155 /* Common attributes */
1156 pPinImpl->pin.refCount = 1;
1157 pPinImpl->pin.pConnectedTo = NULL;
1158 pPinImpl->pin.pCritSec = pCritSec;
1159 pPinImpl->pin.tStart = 0;
1160 pPinImpl->pin.tStop = 0;
1161 pPinImpl->pin.dRate = 1.0;
1162 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1163 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1164 pPinImpl->pin.pFuncsTable = &vtbl->base;
1166 /* Input pin attributes */
1167 pPinImpl->pFuncsTable = vtbl;
1168 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1169 if (pPinImpl->preferred_allocator)
1170 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1171 pPinImpl->pin.IPin_iface.lpVtbl = InputPin_Vtbl;
1172 pPinImpl->IMemInputPin_iface.lpVtbl = &MemInputPin_Vtbl;
1173 pPinImpl->flushing = pPinImpl->end_of_stream = FALSE;
1175 return S_OK;
1178 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, LONG inputpin_size, const PIN_INFO * pPinInfo,
1179 const BaseInputPinFuncTable* vtbl,
1180 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1182 BaseInputPin * pPinImpl;
1184 *ppPin = NULL;
1186 assert(inputpin_size >= sizeof(BaseInputPin));
1187 assert(vtbl->base.pfnCheckMediaType);
1189 if (pPinInfo->dir != PINDIR_INPUT)
1191 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1192 return E_INVALIDARG;
1195 pPinImpl = CoTaskMemAlloc(inputpin_size);
1197 if (!pPinImpl)
1198 return E_OUTOFMEMORY;
1200 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, vtbl, pCritSec, allocator, pPinImpl)))
1202 *ppPin = &pPinImpl->pin.IPin_iface;
1203 return S_OK;
1206 CoTaskMemFree(pPinImpl);
1207 return E_FAIL;
1210 HRESULT WINAPI BaseInputPin_Destroy(BaseInputPin *This)
1212 FreeMediaType(&This->pin.mtCurrent);
1213 if (This->pAllocator)
1214 IMemAllocator_Release(This->pAllocator);
1215 This->pAllocator = NULL;
1216 This->pin.IPin_iface.lpVtbl = NULL;
1217 CoTaskMemFree(This);
1218 return S_OK;