TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / strmbase / pin.c
blobd61469c86a76b9c3a3058c0c71fc968c4835111e
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("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)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)
401 BaseOutputPin_Destroy(This);
402 return 0;
404 return refCount;
407 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
409 HRESULT hr;
410 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
412 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
413 dump_AM_MEDIA_TYPE(pmt);
415 if (!pReceivePin)
416 return E_POINTER;
418 /* If we try to connect to ourselves, we will definitely deadlock.
419 * There are other cases where we could deadlock too, but this
420 * catches the obvious case */
421 assert(pReceivePin != iface);
423 EnterCriticalSection(This->pin.pCritSec);
425 /* if we have been a specific type to connect with, then we can either connect
426 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
427 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
428 hr = This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmt);
429 else
431 /* negotiate media type */
433 IEnumMediaTypes * pEnumCandidates;
434 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
436 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
438 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
440 /* try this filter's media types first */
441 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
443 assert(pmtCandidate);
444 dump_AM_MEDIA_TYPE(pmtCandidate);
445 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
446 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
447 assert(pmtCandidate->pbFormat);
448 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
449 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
451 hr = S_OK;
452 DeleteMediaType(pmtCandidate);
453 break;
455 DeleteMediaType(pmtCandidate);
456 pmtCandidate = NULL;
458 IEnumMediaTypes_Release(pEnumCandidates);
461 /* then try receiver filter's media types */
462 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
464 ULONG fetched;
466 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
468 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, &fetched))
470 assert(pmtCandidate);
471 dump_AM_MEDIA_TYPE(pmtCandidate);
472 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
473 (This->pin.pFuncsTable->pfnAttemptConnection(&This->pin, pReceivePin, pmtCandidate) == S_OK))
475 hr = S_OK;
476 DeleteMediaType(pmtCandidate);
477 break;
479 DeleteMediaType(pmtCandidate);
480 pmtCandidate = NULL;
481 } /* while */
482 IEnumMediaTypes_Release(pEnumCandidates);
483 } /* if not found */
484 } /* if negotiate media type */
485 } /* if succeeded */
486 LeaveCriticalSection(This->pin.pCritSec);
488 TRACE(" -- %x\n", hr);
489 return hr;
492 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
494 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
496 return E_UNEXPECTED;
499 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
501 HRESULT hr;
502 BaseOutputPin *This = impl_BaseOutputPin_from_IPin(iface);
504 TRACE("()\n");
506 EnterCriticalSection(This->pin.pCritSec);
508 if (This->pMemInputPin)
510 IMemInputPin_Release(This->pMemInputPin);
511 This->pMemInputPin = NULL;
513 if (This->pin.pConnectedTo)
515 IPin_Release(This->pin.pConnectedTo);
516 This->pin.pConnectedTo = NULL;
517 FreeMediaType(&This->pin.mtCurrent);
518 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
519 hr = S_OK;
521 else
522 hr = S_FALSE;
524 LeaveCriticalSection(This->pin.pCritSec);
526 return hr;
529 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
531 TRACE("()\n");
533 /* not supposed to do anything in an output pin */
535 return E_UNEXPECTED;
538 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
540 TRACE("(%p)->()\n", iface);
542 /* not supposed to do anything in an output pin */
544 return E_UNEXPECTED;
547 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
549 TRACE("(%p)->()\n", iface);
551 /* not supposed to do anything in an output pin */
553 return E_UNEXPECTED;
556 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
558 HRESULT hr;
560 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
562 if (!This->pin.pConnectedTo)
563 hr = VFW_E_NOT_CONNECTED;
564 else
566 hr = IMemAllocator_GetBuffer(This->pAllocator, ppSample, tStart, tStop, dwFlags);
568 if (SUCCEEDED(hr))
569 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
572 return hr;
575 /* replaces OutputPin_SendSample */
576 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
578 IMemInputPin * pMemConnected = NULL;
579 PIN_INFO pinInfo;
580 HRESULT hr;
582 EnterCriticalSection(This->pin.pCritSec);
584 if (!This->pin.pConnectedTo || !This->pMemInputPin)
585 hr = VFW_E_NOT_CONNECTED;
586 else
588 /* we don't have the lock held when using This->pMemInputPin,
589 * so we need to AddRef it to stop it being deleted while we are
590 * using it. Same with its filter. */
591 pMemConnected = This->pMemInputPin;
592 IMemInputPin_AddRef(pMemConnected);
593 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
596 LeaveCriticalSection(This->pin.pCritSec);
598 if (SUCCEEDED(hr))
600 /* NOTE: if we are in a critical section when Receive is called
601 * then it causes some problems (most notably with the native Video
602 * Renderer) if we are re-entered for whatever reason */
603 hr = IMemInputPin_Receive(pMemConnected, pSample);
605 /* If the filter's destroyed, tell upstream to stop sending data */
606 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
607 hr = S_FALSE;
609 if (pMemConnected)
610 IMemInputPin_Release(pMemConnected);
612 return hr;
615 /* replaces OutputPin_CommitAllocator */
616 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
618 HRESULT hr;
620 TRACE("(%p)->()\n", This);
622 EnterCriticalSection(This->pin.pCritSec);
624 if (!This->pin.pConnectedTo || !This->pMemInputPin)
625 hr = VFW_E_NOT_CONNECTED;
626 else
627 hr = IMemAllocator_Commit(This->pAllocator);
629 LeaveCriticalSection(This->pin.pCritSec);
631 TRACE("--> %08x\n", hr);
632 return hr;
635 /* replaces OutputPin_DecommitAllocator */
636 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
638 HRESULT hr;
640 TRACE("(%p)->()\n", This);
642 EnterCriticalSection(This->pin.pCritSec);
644 if (!This->pin.pConnectedTo || !This->pMemInputPin)
645 hr = VFW_E_NOT_CONNECTED;
646 else
647 hr = IMemAllocator_Decommit(This->pAllocator);
649 LeaveCriticalSection(This->pin.pCritSec);
651 TRACE("--> %08x\n", hr);
652 return hr;
655 /* replaces OutputPin_DeliverDisconnect */
656 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
658 HRESULT hr;
660 TRACE("(%p)->()\n", This);
662 EnterCriticalSection(This->pin.pCritSec);
664 if (!This->pin.pConnectedTo || !This->pMemInputPin)
665 hr = VFW_E_NOT_CONNECTED;
666 else
668 hr = IMemAllocator_Decommit(This->pAllocator);
670 if (SUCCEEDED(hr))
671 hr = IPin_Disconnect(This->pin.pConnectedTo);
673 IPin_Disconnect(&This->pin.IPin_iface);
675 LeaveCriticalSection(This->pin.pCritSec);
677 return hr;
680 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
682 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
685 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
687 HRESULT hr;
689 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
691 if (hr == VFW_E_NO_ALLOCATOR)
692 /* Input pin provides no allocator, use standard memory allocator */
693 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
695 if (SUCCEEDED(hr))
697 ALLOCATOR_PROPERTIES rProps;
698 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
700 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
701 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
704 if (SUCCEEDED(hr))
705 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
707 return hr;
710 /*** The Construct functions ***/
712 /* Function called as a helper to IPin_Connect */
713 /* specific AM_MEDIA_TYPE - it cannot be NULL */
714 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
716 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
717 HRESULT hr;
718 IMemAllocator * pMemAlloc = NULL;
720 TRACE("(%p, %p)\n", pReceivePin, pmt);
721 dump_AM_MEDIA_TYPE(pmt);
723 /* FIXME: call queryacceptproc */
725 This->pin.pConnectedTo = pReceivePin;
726 IPin_AddRef(pReceivePin);
727 CopyMediaType(&This->pin.mtCurrent, pmt);
729 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
731 /* get the IMemInputPin interface we will use to deliver samples to the
732 * connected pin */
733 if (SUCCEEDED(hr))
735 This->pMemInputPin = NULL;
736 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
738 if (SUCCEEDED(hr))
740 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
741 if (SUCCEEDED(hr))
742 This->pAllocator = pMemAlloc;
743 else if (pMemAlloc)
744 IMemAllocator_Release(pMemAlloc);
747 /* break connection if we couldn't get the allocator */
748 if (FAILED(hr))
750 if (This->pMemInputPin)
751 IMemInputPin_Release(This->pMemInputPin);
752 This->pMemInputPin = NULL;
754 IPin_Disconnect(pReceivePin);
758 if (FAILED(hr))
760 IPin_Release(This->pin.pConnectedTo);
761 This->pin.pConnectedTo = NULL;
762 FreeMediaType(&This->pin.mtCurrent);
765 TRACE(" -- %x\n", hr);
766 return hr;
769 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
771 TRACE("\n");
773 /* Common attributes */
774 pPinImpl->pin.IPin_iface.lpVtbl = OutputPin_Vtbl;
775 pPinImpl->pin.refCount = 1;
776 pPinImpl->pin.pConnectedTo = NULL;
777 pPinImpl->pin.pCritSec = pCritSec;
778 pPinImpl->pin.tStart = 0;
779 pPinImpl->pin.tStop = 0;
780 pPinImpl->pin.dRate = 1.0;
781 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
782 pPinImpl->pin.pFuncsTable = &vtbl->base;
783 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
785 /* Output pin attributes */
786 pPinImpl->pMemInputPin = NULL;
787 pPinImpl->pAllocator = NULL;
788 pPinImpl->pFuncsTable = vtbl;
790 return S_OK;
793 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BaseOutputPinFuncTable* vtbl, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
795 BaseOutputPin * pPinImpl;
797 *ppPin = NULL;
799 if (pPinInfo->dir != PINDIR_OUTPUT)
801 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
802 return E_INVALIDARG;
805 assert(outputpin_size >= sizeof(BaseOutputPin));
806 assert(vtbl->base.pfnAttemptConnection);
808 pPinImpl = CoTaskMemAlloc(outputpin_size);
810 if (!pPinImpl)
811 return E_OUTOFMEMORY;
813 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, vtbl, pCritSec, pPinImpl)))
815 *ppPin = &pPinImpl->pin.IPin_iface;
816 return S_OK;
819 CoTaskMemFree(pPinImpl);
820 return E_FAIL;
823 HRESULT WINAPI BaseOutputPin_Destroy(BaseOutputPin *This)
825 FreeMediaType(&This->pin.mtCurrent);
826 if (This->pAllocator)
827 IMemAllocator_Release(This->pAllocator);
828 This->pAllocator = NULL;
829 CoTaskMemFree(This);
830 return S_OK;
833 /*** Input Pin implementation ***/
835 static inline BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
837 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
840 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
842 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
844 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
846 *ppv = NULL;
848 if (IsEqualIID(riid, &IID_IUnknown))
849 *ppv = iface;
850 else if (IsEqualIID(riid, &IID_IPin))
851 *ppv = iface;
852 else if (IsEqualIID(riid, &IID_IMemInputPin))
853 *ppv = &This->IMemInputPin_iface;
854 else if (IsEqualIID(riid, &IID_IMediaSeeking))
856 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
859 if (*ppv)
861 IUnknown_AddRef((IUnknown *)(*ppv));
862 return S_OK;
865 FIXME("No interface for %s!\n", debugstr_guid(riid));
867 return E_NOINTERFACE;
870 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
872 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
873 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
875 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
877 if (!refCount)
878 BaseInputPin_Destroy(This);
880 return refCount;
883 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
885 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
887 return E_UNEXPECTED;
891 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
893 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
894 PIN_DIRECTION pindirReceive;
895 HRESULT hr = S_OK;
897 TRACE("(%p, %p)\n", pReceivePin, pmt);
898 dump_AM_MEDIA_TYPE(pmt);
900 EnterCriticalSection(This->pin.pCritSec);
902 if (This->pin.pConnectedTo)
903 hr = VFW_E_ALREADY_CONNECTED;
905 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) != S_OK)
906 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
907 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
909 if (SUCCEEDED(hr))
911 IPin_QueryDirection(pReceivePin, &pindirReceive);
913 if (pindirReceive != PINDIR_OUTPUT)
915 ERR("Can't connect from non-output pin\n");
916 hr = VFW_E_INVALID_DIRECTION;
920 if (SUCCEEDED(hr))
922 CopyMediaType(&This->pin.mtCurrent, pmt);
923 This->pin.pConnectedTo = pReceivePin;
924 IPin_AddRef(pReceivePin);
927 LeaveCriticalSection(This->pin.pCritSec);
929 return hr;
932 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
934 return IPin_EndOfStream( pin );
937 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
939 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
941 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
943 return (This->pin.pFuncsTable->pfnCheckMediaType(&This->pin, pmt) == S_OK ? S_OK : S_FALSE);
946 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
948 HRESULT hr = S_OK;
949 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
951 TRACE("(%p)\n", This);
953 EnterCriticalSection(This->pin.pCritSec);
954 if (This->flushing)
955 hr = S_FALSE;
956 else
957 This->end_of_stream = TRUE;
958 LeaveCriticalSection(This->pin.pCritSec);
960 if (hr == S_OK)
961 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
962 return hr;
965 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
967 return IPin_BeginFlush( pin );
970 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
972 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
973 HRESULT hr;
974 TRACE("() semi-stub\n");
976 EnterCriticalSection(This->pin.pCritSec);
977 This->flushing = TRUE;
979 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
980 LeaveCriticalSection(This->pin.pCritSec);
982 return hr;
985 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
987 return IPin_EndFlush( pin );
990 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
992 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
993 HRESULT hr;
994 TRACE("(%p)\n", This);
996 EnterCriticalSection(This->pin.pCritSec);
997 This->flushing = This->end_of_stream = FALSE;
999 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1000 LeaveCriticalSection(This->pin.pCritSec);
1002 return hr;
1005 typedef struct newsegmentargs
1007 REFERENCE_TIME tStart, tStop;
1008 double rate;
1009 } newsegmentargs;
1011 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1013 newsegmentargs *args = data;
1014 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1017 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1019 BaseInputPin *This = impl_BaseInputPin_from_IPin(iface);
1020 newsegmentargs args;
1022 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1024 args.tStart = This->pin.tStart = tStart;
1025 args.tStop = This->pin.tStop = tStop;
1026 args.rate = This->pin.dRate = dRate;
1028 return SendFurther( iface, deliver_newsegment, &args, NULL );
1031 /*** IMemInputPin implementation ***/
1033 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1035 return CONTAINING_RECORD(iface, BaseInputPin, IMemInputPin_iface);
1038 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1040 BaseInputPin *This = impl_from_IMemInputPin(iface);
1042 return IPin_QueryInterface(&This->pin.IPin_iface, riid, ppv);
1045 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1047 BaseInputPin *This = impl_from_IMemInputPin(iface);
1049 return IPin_AddRef(&This->pin.IPin_iface);
1052 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1054 BaseInputPin *This = impl_from_IMemInputPin(iface);
1056 return IPin_Release(&This->pin.IPin_iface);
1059 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1061 BaseInputPin *This = impl_from_IMemInputPin(iface);
1063 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1065 *ppAllocator = This->pAllocator;
1066 if (*ppAllocator)
1067 IMemAllocator_AddRef(*ppAllocator);
1069 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1072 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1074 BaseInputPin *This = impl_from_IMemInputPin(iface);
1076 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1078 if (bReadOnly)
1079 FIXME("Read only flag not handled yet!\n");
1081 /* FIXME: Should we release the allocator on disconnection? */
1082 if (!pAllocator)
1084 WARN("Null allocator\n");
1085 return E_POINTER;
1088 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1089 return E_FAIL;
1091 if (This->pAllocator)
1092 IMemAllocator_Release(This->pAllocator);
1093 This->pAllocator = pAllocator;
1094 if (This->pAllocator)
1095 IMemAllocator_AddRef(This->pAllocator);
1097 return S_OK;
1100 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1102 BaseInputPin *This = impl_from_IMemInputPin(iface);
1104 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1106 /* override this method if you have any specific requirements */
1108 return E_NOTIMPL;
1111 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1113 BaseInputPin *This = impl_from_IMemInputPin(iface);
1114 HRESULT hr = S_FALSE;
1116 /* this trace commented out for performance reasons */
1117 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1118 if (This->pFuncsTable->pfnReceive)
1119 hr = This->pFuncsTable->pfnReceive(This, pSample);
1120 return hr;
1123 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1125 HRESULT hr = S_OK;
1126 BaseInputPin *This = impl_from_IMemInputPin(iface);
1128 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1130 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1132 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1133 if (hr != S_OK)
1134 break;
1137 return hr;
1140 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1142 BaseInputPin *This = impl_from_IMemInputPin(iface);
1144 TRACE("(%p/%p)->()\n", This, iface);
1146 return S_OK;
1149 static const IMemInputPinVtbl MemInputPin_Vtbl =
1151 MemInputPin_QueryInterface,
1152 MemInputPin_AddRef,
1153 MemInputPin_Release,
1154 MemInputPin_GetAllocator,
1155 MemInputPin_NotifyAllocator,
1156 MemInputPin_GetAllocatorRequirements,
1157 MemInputPin_Receive,
1158 MemInputPin_ReceiveMultiple,
1159 MemInputPin_ReceiveCanBlock
1162 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1163 const BaseInputPinFuncTable* vtbl,
1164 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1166 TRACE("\n");
1168 /* Common attributes */
1169 pPinImpl->pin.refCount = 1;
1170 pPinImpl->pin.pConnectedTo = NULL;
1171 pPinImpl->pin.pCritSec = pCritSec;
1172 pPinImpl->pin.tStart = 0;
1173 pPinImpl->pin.tStop = 0;
1174 pPinImpl->pin.dRate = 1.0;
1175 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1176 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1177 pPinImpl->pin.pFuncsTable = &vtbl->base;
1179 /* Input pin attributes */
1180 pPinImpl->pFuncsTable = vtbl;
1181 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1182 if (pPinImpl->preferred_allocator)
1183 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1184 pPinImpl->pin.IPin_iface.lpVtbl = InputPin_Vtbl;
1185 pPinImpl->IMemInputPin_iface.lpVtbl = &MemInputPin_Vtbl;
1186 pPinImpl->flushing = pPinImpl->end_of_stream = FALSE;
1188 return S_OK;
1191 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, LONG inputpin_size, const PIN_INFO * pPinInfo,
1192 const BaseInputPinFuncTable* vtbl,
1193 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1195 BaseInputPin * pPinImpl;
1197 *ppPin = NULL;
1199 assert(inputpin_size >= sizeof(BaseInputPin));
1200 assert(vtbl->base.pfnCheckMediaType);
1202 if (pPinInfo->dir != PINDIR_INPUT)
1204 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1205 return E_INVALIDARG;
1208 pPinImpl = CoTaskMemAlloc(inputpin_size);
1210 if (!pPinImpl)
1211 return E_OUTOFMEMORY;
1213 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, vtbl, pCritSec, allocator, pPinImpl)))
1215 *ppPin = (IPin *)pPinImpl;
1216 return S_OK;
1219 CoTaskMemFree(pPinImpl);
1220 return E_FAIL;
1223 HRESULT WINAPI BaseInputPin_Destroy(BaseInputPin *This)
1225 FreeMediaType(&This->pin.mtCurrent);
1226 if (This->pAllocator)
1227 IMemAllocator_Release(This->pAllocator);
1228 This->pAllocator = NULL;
1229 This->pin.IPin_iface.lpVtbl = NULL;
1230 CoTaskMemFree(This);
1231 return S_OK;