wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / strmbase / pin.c
blob97408e88ed74da10e2e6e62295dfbd1106da70bf
1 /*
2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include "dshow.h"
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27 #include "wine/strmbase.h"
28 #include "uuids.h"
29 #include "vfwmsgs.h"
30 #include <assert.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
34 static const IPinVtbl InputPin_Vtbl;
35 static const IPinVtbl OutputPin_Vtbl;
36 static const IMemInputPinVtbl MemInputPin_Vtbl;
38 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
40 /** Helper function, there are a lot of places where the error code is inherited
41 * The following rules apply:
43 * Return the first received error code (E_NOTIMPL is ignored)
44 * If no errors occur: return the first received non-error-code that isn't S_OK
46 static HRESULT updatehres( HRESULT original, HRESULT new )
48 if (FAILED( original ) || new == E_NOTIMPL)
49 return original;
51 if (FAILED( new ) || original == S_OK)
52 return new;
54 return original;
57 /** Sends a message from a pin further to other, similar pins
58 * fnMiddle is called on each pin found further on the stream.
59 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
61 * If the pin given is an input pin, the message will be sent downstream to other input pins
62 * If the pin given is an output pin, the message will be sent upstream to other output pins
64 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
66 PIN_INFO pin_info;
67 ULONG amount = 0;
68 HRESULT hr = S_OK;
69 HRESULT hr_return = S_OK;
70 IEnumPins *enumpins = NULL;
71 BOOL foundend = TRUE;
72 PIN_DIRECTION from_dir;
74 IPin_QueryDirection( from, &from_dir );
76 hr = IPin_QueryInternalConnections( from, NULL, &amount );
77 if (hr != E_NOTIMPL && amount)
78 FIXME("Use QueryInternalConnections!\n");
79 hr = S_OK;
81 pin_info.pFilter = NULL;
82 hr = IPin_QueryPinInfo( from, &pin_info );
83 if (FAILED(hr))
84 goto out;
86 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
87 if (FAILED(hr))
88 goto out;
90 hr = IEnumPins_Reset( enumpins );
91 while (hr == S_OK) {
92 IPin *pin = NULL;
93 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
94 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
96 hr = IEnumPins_Reset( enumpins );
97 continue;
99 if (pin)
101 PIN_DIRECTION dir;
103 IPin_QueryDirection( pin, &dir );
104 if (dir != from_dir)
106 IPin *connected = NULL;
108 foundend = FALSE;
109 IPin_ConnectedTo( pin, &connected );
110 if (connected)
112 HRESULT hr_local;
114 hr_local = fnMiddle( connected, arg );
115 hr_return = updatehres( hr_return, hr_local );
116 IPin_Release(connected);
119 IPin_Release( pin );
121 else
123 hr = S_OK;
124 break;
128 if (!foundend)
129 hr = hr_return;
130 else if (fnEnd) {
131 HRESULT hr_local;
133 hr_local = fnEnd( from, arg );
134 hr_return = updatehres( hr_return, hr_local );
136 IEnumPins_Release(enumpins);
138 out:
139 if (pin_info.pFilter)
140 IBaseFilter_Release( pin_info.pFilter );
141 return hr;
144 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
146 /* Tempting to just do a memcpy, but the name field is
147 128 characters long! We will probably never exceed 10
148 most of the time, so we are better off copying
149 each field manually */
150 strcpyW(pDest->achName, pSrc->achName);
151 pDest->dir = pSrc->dir;
152 pDest->pFilter = pSrc->pFilter;
155 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
157 if (!pmt)
158 return;
159 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
162 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
164 TRACE("pmt1: ");
165 dump_AM_MEDIA_TYPE(pmt1);
166 TRACE("pmt2: ");
167 dump_AM_MEDIA_TYPE(pmt2);
168 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
169 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
172 /*** Common Base Pin function */
173 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
175 if (iPosition < 0)
176 return E_INVALIDARG;
177 return VFW_S_NO_MORE_ITEMS;
180 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
182 return 1;
185 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
187 BasePin *This = (BasePin *)iface;
188 ULONG refCount = InterlockedIncrement(&This->refCount);
190 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
192 return refCount;
195 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
197 HRESULT hr;
198 BasePin *This = (BasePin *)iface;
200 TRACE("()\n");
202 EnterCriticalSection(This->pCritSec);
204 if (This->pConnectedTo)
206 IPin_Release(This->pConnectedTo);
207 This->pConnectedTo = NULL;
208 FreeMediaType(&This->mtCurrent);
209 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
210 hr = S_OK;
212 else
213 hr = S_FALSE;
215 LeaveCriticalSection(This->pCritSec);
217 return hr;
220 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
222 HRESULT hr;
223 BasePin *This = (BasePin *)iface;
225 TRACE("(%p)\n", ppPin);
227 EnterCriticalSection(This->pCritSec);
229 if (This->pConnectedTo)
231 *ppPin = This->pConnectedTo;
232 IPin_AddRef(*ppPin);
233 hr = S_OK;
235 else
237 hr = VFW_E_NOT_CONNECTED;
238 *ppPin = NULL;
241 LeaveCriticalSection(This->pCritSec);
243 return hr;
246 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
248 HRESULT hr;
249 BasePin *This = (BasePin *)iface;
251 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
253 EnterCriticalSection(This->pCritSec);
255 if (This->pConnectedTo)
257 CopyMediaType(pmt, &This->mtCurrent);
258 hr = S_OK;
260 else
262 ZeroMemory(pmt, sizeof(*pmt));
263 hr = VFW_E_NOT_CONNECTED;
266 LeaveCriticalSection(This->pCritSec);
268 return hr;
271 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
273 BasePin *This = (BasePin *)iface;
275 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
277 Copy_PinInfo(pInfo, &This->pinInfo);
278 IBaseFilter_AddRef(pInfo->pFilter);
280 return S_OK;
283 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
285 BasePin *This = (BasePin *)iface;
287 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
289 *pPinDir = This->pinInfo.dir;
291 return S_OK;
294 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
296 BasePin *This = (BasePin *)iface;
298 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
300 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
301 if (!*Id)
302 return E_OUTOFMEMORY;
304 strcpyW(*Id, This->pinInfo.achName);
306 return S_OK;
309 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
311 TRACE("(%p)->(%p)\n", iface, pmt);
313 return S_OK;
316 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
318 BasePin *This = (BasePin *)iface;
320 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
322 /* override this method to allow enumeration of your types */
324 return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
327 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
329 BasePin *This = (BasePin *)iface;
331 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
333 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
336 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
338 BasePin *This = (BasePin *)iface;
340 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
342 This->tStart = tStart;
343 This->tStop = tStop;
344 This->dRate = dRate;
346 return S_OK;
349 /*** OutputPin implementation ***/
351 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
353 BaseOutputPin *This = (BaseOutputPin *)iface;
355 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
357 *ppv = NULL;
359 if (IsEqualIID(riid, &IID_IUnknown))
360 *ppv = iface;
361 else if (IsEqualIID(riid, &IID_IPin))
362 *ppv = iface;
363 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
364 IsEqualIID(riid, &IID_IQualityControl))
366 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
369 if (*ppv)
371 IUnknown_AddRef((IUnknown *)(*ppv));
372 return S_OK;
375 FIXME("No interface for %s!\n", debugstr_guid(riid));
377 return E_NOINTERFACE;
380 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
382 BaseOutputPin *This = (BaseOutputPin *)iface;
383 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
385 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
387 if (!refCount)
389 FreeMediaType(&This->pin.mtCurrent);
390 CoTaskMemFree(This);
391 return 0;
393 return refCount;
396 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
398 HRESULT hr;
399 BaseOutputPin *This = (BaseOutputPin *)iface;
401 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
402 dump_AM_MEDIA_TYPE(pmt);
404 /* If we try to connect to ourself, we will definitely deadlock.
405 * There are other cases where we could deadlock too, but this
406 * catches the obvious case */
407 assert(pReceivePin != iface);
409 EnterCriticalSection(This->pin.pCritSec);
411 /* if we have been a specific type to connect with, then we can either connect
412 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
413 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
414 hr = This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmt);
415 else
417 /* negotiate media type */
419 IEnumMediaTypes * pEnumCandidates;
420 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
422 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
424 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
426 /* try this filter's media types first */
427 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
429 assert(pmtCandidate);
430 dump_AM_MEDIA_TYPE(pmtCandidate);
431 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
432 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
433 assert(pmtCandidate->pbFormat);
434 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
435 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
437 hr = S_OK;
438 DeleteMediaType(pmtCandidate);
439 break;
441 DeleteMediaType(pmtCandidate);
442 pmtCandidate = NULL;
444 IEnumMediaTypes_Release(pEnumCandidates);
447 /* then try receiver filter's media types */
448 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
450 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
452 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
454 assert(pmtCandidate);
455 dump_AM_MEDIA_TYPE(pmtCandidate);
456 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
457 (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
459 hr = S_OK;
460 DeleteMediaType(pmtCandidate);
461 break;
463 DeleteMediaType(pmtCandidate);
464 pmtCandidate = NULL;
465 } /* while */
466 IEnumMediaTypes_Release(pEnumCandidates);
467 } /* if not found */
468 } /* if negotiate media type */
469 } /* if succeeded */
470 LeaveCriticalSection(This->pin.pCritSec);
472 TRACE(" -- %x\n", hr);
473 return hr;
476 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
478 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
480 return E_UNEXPECTED;
483 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
485 HRESULT hr;
486 BaseOutputPin *This = (BaseOutputPin *)iface;
488 TRACE("()\n");
490 EnterCriticalSection(This->pin.pCritSec);
492 if (This->pMemInputPin)
494 IMemInputPin_Release(This->pMemInputPin);
495 This->pMemInputPin = NULL;
497 if (This->pin.pConnectedTo)
499 IPin_Release(This->pin.pConnectedTo);
500 This->pin.pConnectedTo = NULL;
501 FreeMediaType(&This->pin.mtCurrent);
502 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
503 hr = S_OK;
505 else
506 hr = S_FALSE;
508 LeaveCriticalSection(This->pin.pCritSec);
510 return hr;
513 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
515 TRACE("()\n");
517 /* not supposed to do anything in an output pin */
519 return E_UNEXPECTED;
522 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
524 TRACE("(%p)->()\n", iface);
526 /* not supposed to do anything in an output pin */
528 return E_UNEXPECTED;
531 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
533 TRACE("(%p)->()\n", iface);
535 /* not supposed to do anything in an output pin */
537 return E_UNEXPECTED;
540 static const IPinVtbl OutputPin_Vtbl =
542 BaseOutputPinImpl_QueryInterface,
543 BasePinImpl_AddRef,
544 BaseOutputPinImpl_Release,
545 BaseOutputPinImpl_Connect,
546 BaseOutputPinImpl_ReceiveConnection,
547 BaseOutputPinImpl_Disconnect,
548 BasePinImpl_ConnectedTo,
549 BasePinImpl_ConnectionMediaType,
550 BasePinImpl_QueryPinInfo,
551 BasePinImpl_QueryDirection,
552 BasePinImpl_QueryId,
553 BasePinImpl_QueryAccept,
554 BasePinImpl_EnumMediaTypes,
555 BasePinImpl_QueryInternalConnections,
556 BaseOutputPinImpl_EndOfStream,
557 BaseOutputPinImpl_BeginFlush,
558 BaseOutputPinImpl_EndFlush,
559 BasePinImpl_NewSegment
562 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
564 HRESULT hr;
566 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
568 EnterCriticalSection(This->pin.pCritSec);
570 if (!This->pin.pConnectedTo)
571 hr = VFW_E_NOT_CONNECTED;
572 else
574 IMemAllocator * pAlloc = NULL;
576 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
578 if (SUCCEEDED(hr))
579 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
581 if (SUCCEEDED(hr))
582 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
584 if (pAlloc)
585 IMemAllocator_Release(pAlloc);
588 LeaveCriticalSection(This->pin.pCritSec);
590 return hr;
593 /* replaces OutputPin_SendSample */
594 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
596 HRESULT hr = S_OK;
597 IMemInputPin * pMemConnected = NULL;
598 PIN_INFO pinInfo;
600 EnterCriticalSection(This->pin.pCritSec);
602 if (!This->pin.pConnectedTo || !This->pMemInputPin)
603 hr = VFW_E_NOT_CONNECTED;
604 else
606 /* we don't have the lock held when using This->pMemInputPin,
607 * so we need to AddRef it to stop it being deleted while we are
608 * using it. Same with its filter. */
609 pMemConnected = This->pMemInputPin;
610 IMemInputPin_AddRef(pMemConnected);
611 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
614 LeaveCriticalSection(This->pin.pCritSec);
616 if (SUCCEEDED(hr))
618 /* NOTE: if we are in a critical section when Receive is called
619 * then it causes some problems (most notably with the native Video
620 * Renderer) if we are re-entered for whatever reason */
621 hr = IMemInputPin_Receive(pMemConnected, pSample);
623 /* If the filter's destroyed, tell upstream to stop sending data */
624 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
625 hr = S_FALSE;
627 if (pMemConnected)
628 IMemInputPin_Release(pMemConnected);
630 return hr;
633 /* replaces OutputPin_CommitAllocator */
634 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
636 HRESULT hr = S_OK;
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
646 IMemAllocator * pAlloc = NULL;
648 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
650 if (SUCCEEDED(hr))
651 hr = IMemAllocator_Commit(pAlloc);
653 if (pAlloc)
654 IMemAllocator_Release(pAlloc);
657 LeaveCriticalSection(This->pin.pCritSec);
659 TRACE("--> %08x\n", hr);
660 return hr;
663 /* replaces OutputPin_DecommitAllocator */
664 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
666 HRESULT hr = S_OK;
668 TRACE("(%p)->()\n", This);
670 EnterCriticalSection(This->pin.pCritSec);
672 if (!This->pin.pConnectedTo || !This->pMemInputPin)
673 hr = VFW_E_NOT_CONNECTED;
674 else
676 IMemAllocator * pAlloc = NULL;
678 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
680 if (SUCCEEDED(hr))
681 hr = IMemAllocator_Decommit(pAlloc);
683 if (pAlloc)
684 IMemAllocator_Release(pAlloc);
687 LeaveCriticalSection(This->pin.pCritSec);
689 TRACE("--> %08x\n", hr);
690 return hr;
693 /* replaces OutputPin_DeliverDisconnect */
694 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
696 HRESULT hr;
698 TRACE("(%p)->()\n", This);
700 EnterCriticalSection(This->pin.pCritSec);
702 if (!This->pin.pConnectedTo || !This->pMemInputPin)
703 hr = VFW_E_NOT_CONNECTED;
704 else
706 IMemAllocator * pAlloc = NULL;
708 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
710 if (SUCCEEDED(hr))
711 hr = IMemAllocator_Decommit(pAlloc);
713 if (pAlloc)
714 IMemAllocator_Release(pAlloc);
716 if (SUCCEEDED(hr))
717 hr = IPin_Disconnect(This->pin.pConnectedTo);
719 IPin_Disconnect((IPin *)This);
721 LeaveCriticalSection(This->pin.pCritSec);
723 return hr;
726 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
728 return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
731 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
733 HRESULT hr;
735 hr = IMemInputPin_GetAllocator(pPin, pAlloc);
737 if (hr == VFW_E_NO_ALLOCATOR)
738 /* Input pin provides no allocator, use standard memory allocator */
739 hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
741 if (SUCCEEDED(hr))
743 ALLOCATOR_PROPERTIES rProps;
744 ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
746 IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
747 hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
750 if (SUCCEEDED(hr))
751 hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
753 return hr;
756 /*** The Construct functions ***/
758 /* Function called as a helper to IPin_Connect */
759 /* specific AM_MEDIA_TYPE - it cannot be NULL */
760 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
762 BaseOutputPin *This = (BaseOutputPin *)iface;
763 HRESULT hr;
764 IMemAllocator * pMemAlloc = NULL;
766 TRACE("(%p, %p)\n", pReceivePin, pmt);
767 dump_AM_MEDIA_TYPE(pmt);
769 /* FIXME: call queryacceptproc */
771 This->pin.pConnectedTo = pReceivePin;
772 IPin_AddRef(pReceivePin);
773 CopyMediaType(&This->pin.mtCurrent, pmt);
775 hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
777 /* get the IMemInputPin interface we will use to deliver samples to the
778 * connected pin */
779 if (SUCCEEDED(hr))
781 This->pMemInputPin = NULL;
782 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
784 if (SUCCEEDED(hr))
786 hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
787 if (pMemAlloc)
788 IMemAllocator_Release(pMemAlloc);
791 /* break connection if we couldn't get the allocator */
792 if (FAILED(hr))
794 if (This->pMemInputPin)
795 IMemInputPin_Release(This->pMemInputPin);
796 This->pMemInputPin = NULL;
798 IPin_Disconnect(pReceivePin);
802 if (FAILED(hr))
804 IPin_Release(This->pin.pConnectedTo);
805 This->pin.pConnectedTo = NULL;
806 FreeMediaType(&This->pin.mtCurrent);
809 TRACE(" -- %x\n", hr);
810 return hr;
813 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
815 TRACE("\n");
817 /* Common attributes */
818 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
819 pPinImpl->pin.refCount = 1;
820 pPinImpl->pin.pConnectedTo = NULL;
821 pPinImpl->pin.pCritSec = pCritSec;
822 pPinImpl->pin.tStart = 0;
823 pPinImpl->pin.tStop = 0;
824 pPinImpl->pin.dRate = 1.0;
825 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
826 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
827 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
829 /* Output pin attributes */
830 pPinImpl->pMemInputPin = NULL;
831 pPinImpl->pFuncsTable = pBaseOutputFuncsTable;
833 return S_OK;
836 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
838 BaseOutputPin * pPinImpl;
840 *ppPin = NULL;
842 if (pPinInfo->dir != PINDIR_OUTPUT)
844 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
845 return E_INVALIDARG;
848 assert(outputpin_size >= sizeof(BaseOutputPin));
849 assert(pBaseFuncsTable->pfnAttemptConnection);
851 pPinImpl = CoTaskMemAlloc(outputpin_size);
853 if (!pPinImpl)
854 return E_OUTOFMEMORY;
856 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseOutputFuncsTable, pCritSec, pPinImpl)))
858 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
859 return S_OK;
862 CoTaskMemFree(pPinImpl);
863 return E_FAIL;
866 /*** Input Pin implementation ***/
868 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
870 BaseInputPin *This = (BaseInputPin *)iface;
872 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
874 *ppv = NULL;
876 if (IsEqualIID(riid, &IID_IUnknown))
877 *ppv = iface;
878 else if (IsEqualIID(riid, &IID_IPin))
879 *ppv = iface;
880 else if (IsEqualIID(riid, &IID_IMemInputPin))
881 *ppv = &This->lpVtblMemInput;
882 else if (IsEqualIID(riid, &IID_IMediaSeeking))
884 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
887 if (*ppv)
889 IUnknown_AddRef((IUnknown *)(*ppv));
890 return S_OK;
893 FIXME("No interface for %s!\n", debugstr_guid(riid));
895 return E_NOINTERFACE;
898 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
900 BaseInputPin *This = (BaseInputPin *)iface;
901 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
903 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
905 if (!refCount)
907 FreeMediaType(&This->pin.mtCurrent);
908 if (This->pAllocator)
909 IMemAllocator_Release(This->pAllocator);
910 This->pAllocator = NULL;
911 This->pin.lpVtbl = NULL;
912 CoTaskMemFree(This);
913 return 0;
915 else
916 return refCount;
919 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
921 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
923 return E_UNEXPECTED;
927 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
929 BaseInputPin *This = (BaseInputPin *)iface;
930 PIN_DIRECTION pindirReceive;
931 HRESULT hr = S_OK;
933 TRACE("(%p, %p)\n", pReceivePin, pmt);
934 dump_AM_MEDIA_TYPE(pmt);
936 EnterCriticalSection(This->pin.pCritSec);
938 if (This->pin.pConnectedTo)
939 hr = VFW_E_ALREADY_CONNECTED;
941 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
942 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
943 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
945 if (SUCCEEDED(hr))
947 IPin_QueryDirection(pReceivePin, &pindirReceive);
949 if (pindirReceive != PINDIR_OUTPUT)
951 ERR("Can't connect from non-output pin\n");
952 hr = VFW_E_INVALID_DIRECTION;
956 if (SUCCEEDED(hr))
958 CopyMediaType(&This->pin.mtCurrent, pmt);
959 This->pin.pConnectedTo = pReceivePin;
960 IPin_AddRef(pReceivePin);
963 LeaveCriticalSection(This->pin.pCritSec);
965 return hr;
968 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
970 return IPin_EndOfStream( pin );
973 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
975 BaseInputPin *This = (BaseInputPin *)iface;
977 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
979 return (This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) == S_OK ? S_OK : S_FALSE);
982 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
984 HRESULT hr = S_OK;
985 BaseInputPin *This = (BaseInputPin *)iface;
987 TRACE("(%p)\n", This);
989 EnterCriticalSection(This->pin.pCritSec);
990 if (This->flushing)
991 hr = S_FALSE;
992 else
993 This->end_of_stream = 1;
994 LeaveCriticalSection(This->pin.pCritSec);
996 if (hr == S_OK)
997 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
998 return hr;
1001 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1003 return IPin_BeginFlush( pin );
1006 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1008 BaseInputPin *This = (BaseInputPin *)iface;
1009 HRESULT hr;
1010 TRACE("() semi-stub\n");
1012 EnterCriticalSection(This->pin.pCritSec);
1013 This->flushing = 1;
1015 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1016 LeaveCriticalSection(This->pin.pCritSec);
1018 return hr;
1021 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1023 return IPin_EndFlush( pin );
1026 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1028 BaseInputPin *This = (BaseInputPin *)iface;
1029 HRESULT hr;
1030 TRACE("(%p)\n", This);
1032 EnterCriticalSection(This->pin.pCritSec);
1033 This->flushing = This->end_of_stream = 0;
1035 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1036 LeaveCriticalSection(This->pin.pCritSec);
1038 return hr;
1041 typedef struct newsegmentargs
1043 REFERENCE_TIME tStart, tStop;
1044 double rate;
1045 } newsegmentargs;
1047 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1049 newsegmentargs *args = data;
1050 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1053 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1055 BaseInputPin *This = (BaseInputPin *)iface;
1056 newsegmentargs args;
1058 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1060 args.tStart = This->pin.tStart = tStart;
1061 args.tStop = This->pin.tStop = tStop;
1062 args.rate = This->pin.dRate = dRate;
1064 return SendFurther( iface, deliver_newsegment, &args, NULL );
1067 static const IPinVtbl InputPin_Vtbl =
1069 BaseInputPinImpl_QueryInterface,
1070 BasePinImpl_AddRef,
1071 BaseInputPinImpl_Release,
1072 BaseInputPinImpl_Connect,
1073 BaseInputPinImpl_ReceiveConnection,
1074 BasePinImpl_Disconnect,
1075 BasePinImpl_ConnectedTo,
1076 BasePinImpl_ConnectionMediaType,
1077 BasePinImpl_QueryPinInfo,
1078 BasePinImpl_QueryDirection,
1079 BasePinImpl_QueryId,
1080 BaseInputPinImpl_QueryAccept,
1081 BasePinImpl_EnumMediaTypes,
1082 BasePinImpl_QueryInternalConnections,
1083 BaseInputPinImpl_EndOfStream,
1084 BaseInputPinImpl_BeginFlush,
1085 BaseInputPinImpl_EndFlush,
1086 BaseInputPinImpl_NewSegment
1089 /*** IMemInputPin implementation ***/
1091 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1093 return (BaseInputPin *)((char*)iface - FIELD_OFFSET(BaseInputPin, lpVtblMemInput));
1096 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1098 BaseInputPin *This = impl_from_IMemInputPin(iface);
1100 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
1103 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1105 BaseInputPin *This = impl_from_IMemInputPin(iface);
1107 return IPin_AddRef((IPin *)&This->pin);
1110 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1112 BaseInputPin *This = impl_from_IMemInputPin(iface);
1114 return IPin_Release((IPin *)&This->pin);
1117 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1119 BaseInputPin *This = impl_from_IMemInputPin(iface);
1121 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1123 *ppAllocator = This->pAllocator;
1124 if (*ppAllocator)
1125 IMemAllocator_AddRef(*ppAllocator);
1127 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1130 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1132 BaseInputPin *This = impl_from_IMemInputPin(iface);
1134 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1136 if (bReadOnly)
1137 FIXME("Read only flag not handled yet!\n");
1139 /* FIXME: Should we release the allocator on disconnection? */
1140 if (!pAllocator)
1142 WARN("Null allocator\n");
1143 return E_POINTER;
1146 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1147 return E_FAIL;
1149 if (This->pAllocator)
1150 IMemAllocator_Release(This->pAllocator);
1151 This->pAllocator = pAllocator;
1152 if (This->pAllocator)
1153 IMemAllocator_AddRef(This->pAllocator);
1155 return S_OK;
1158 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1160 BaseInputPin *This = impl_from_IMemInputPin(iface);
1162 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1164 /* override this method if you have any specific requirements */
1166 return E_NOTIMPL;
1169 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1171 BaseInputPin *This = impl_from_IMemInputPin(iface);
1172 HRESULT hr = S_FALSE;
1174 /* this trace commented out for performance reasons */
1175 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1176 if (This->pFuncsTable->pfnReceive)
1177 hr = This->pFuncsTable->pfnReceive(This, pSample);
1178 return hr;
1181 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1183 HRESULT hr = S_OK;
1184 BaseInputPin *This = impl_from_IMemInputPin(iface);
1186 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1188 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1190 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1191 if (hr != S_OK)
1192 break;
1195 return hr;
1198 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1200 BaseInputPin *This = impl_from_IMemInputPin(iface);
1202 TRACE("(%p/%p)->()\n", This, iface);
1204 return S_OK;
1207 static const IMemInputPinVtbl MemInputPin_Vtbl =
1209 MemInputPin_QueryInterface,
1210 MemInputPin_AddRef,
1211 MemInputPin_Release,
1212 MemInputPin_GetAllocator,
1213 MemInputPin_NotifyAllocator,
1214 MemInputPin_GetAllocatorRequirements,
1215 MemInputPin_Receive,
1216 MemInputPin_ReceiveMultiple,
1217 MemInputPin_ReceiveCanBlock
1220 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1221 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1222 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1224 TRACE("\n");
1226 /* Common attributes */
1227 pPinImpl->pin.refCount = 1;
1228 pPinImpl->pin.pConnectedTo = NULL;
1229 pPinImpl->pin.pCritSec = pCritSec;
1230 pPinImpl->pin.tStart = 0;
1231 pPinImpl->pin.tStop = 0;
1232 pPinImpl->pin.dRate = 1.0;
1233 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1234 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1235 pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
1237 /* Input pin attributes */
1238 pPinImpl->pFuncsTable = pBaseInputFuncsTable;
1239 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1240 if (pPinImpl->preferred_allocator)
1241 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1242 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1243 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1244 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1246 return S_OK;
1249 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1250 const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1251 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1253 BaseInputPin * pPinImpl;
1255 *ppPin = NULL;
1257 assert(pBaseFuncsTable->pfnCheckMediaType);
1259 if (pPinInfo->dir != PINDIR_INPUT)
1261 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1262 return E_INVALIDARG;
1265 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1267 if (!pPinImpl)
1268 return E_OUTOFMEMORY;
1270 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseInputFuncsTable, pCritSec, allocator, pPinImpl)))
1272 *ppPin = (IPin *)pPinImpl;
1273 return S_OK;
1276 CoTaskMemFree(pPinImpl);
1277 return E_FAIL;