strmbase: Move InputPin implementation to strmbase.
[wine/testsucceed.git] / dlls / strmbase / pin.c
blob3c3d75317160b2cfb70fd23568be3d6ae0510334
1 /*
2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include "dshow.h"
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27 #include "wine/strmbase.h"
28 #include "uuids.h"
29 #include "vfwmsgs.h"
30 #include <assert.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
34 static const IPinVtbl InputPin_Vtbl;
35 static const IPinVtbl OutputPin_Vtbl;
36 static const IMemInputPinVtbl MemInputPin_Vtbl;
38 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
40 /** Helper function, there are a lot of places where the error code is inherited
41 * The following rules apply:
43 * Return the first received error code (E_NOTIMPL is ignored)
44 * If no errors occur: return the first received non-error-code that isn't S_OK
46 static HRESULT updatehres( HRESULT original, HRESULT new )
48 if (FAILED( original ) || new == E_NOTIMPL)
49 return original;
51 if (FAILED( new ) || original == S_OK)
52 return new;
54 return original;
57 /** Sends a message from a pin further to other, similar pins
58 * fnMiddle is called on each pin found further on the stream.
59 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
61 * If the pin given is an input pin, the message will be sent downstream to other input pins
62 * If the pin given is an output pin, the message will be sent upstream to other output pins
64 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
66 PIN_INFO pin_info;
67 ULONG amount = 0;
68 HRESULT hr = S_OK;
69 HRESULT hr_return = S_OK;
70 IEnumPins *enumpins = NULL;
71 BOOL foundend = TRUE;
72 PIN_DIRECTION from_dir;
74 IPin_QueryDirection( from, &from_dir );
76 hr = IPin_QueryInternalConnections( from, NULL, &amount );
77 if (hr != E_NOTIMPL && amount)
78 FIXME("Use QueryInternalConnections!\n");
79 hr = S_OK;
81 pin_info.pFilter = NULL;
82 hr = IPin_QueryPinInfo( from, &pin_info );
83 if (FAILED(hr))
84 goto out;
86 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
87 if (FAILED(hr))
88 goto out;
90 hr = IEnumPins_Reset( enumpins );
91 while (hr == S_OK) {
92 IPin *pin = NULL;
93 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
94 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
96 hr = IEnumPins_Reset( enumpins );
97 continue;
99 if (pin)
101 PIN_DIRECTION dir;
103 IPin_QueryDirection( pin, &dir );
104 if (dir != from_dir)
106 IPin *connected = NULL;
108 foundend = FALSE;
109 IPin_ConnectedTo( pin, &connected );
110 if (connected)
112 HRESULT hr_local;
114 hr_local = fnMiddle( connected, arg );
115 hr_return = updatehres( hr_return, hr_local );
116 IPin_Release(connected);
119 IPin_Release( pin );
121 else
123 hr = S_OK;
124 break;
128 if (!foundend)
129 hr = hr_return;
130 else if (fnEnd) {
131 HRESULT hr_local;
133 hr_local = fnEnd( from, arg );
134 hr_return = updatehres( hr_return, hr_local );
137 out:
138 if (pin_info.pFilter)
139 IBaseFilter_Release( pin_info.pFilter );
140 return hr;
143 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
145 /* Tempting to just do a memcpy, but the name field is
146 128 characters long! We will probably never exceed 10
147 most of the time, so we are better off copying
148 each field manually */
149 strcpyW(pDest->achName, pSrc->achName);
150 pDest->dir = pSrc->dir;
151 pDest->pFilter = pSrc->pFilter;
154 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
156 if (!pmt)
157 return;
158 TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
161 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
163 TRACE("pmt1: ");
164 dump_AM_MEDIA_TYPE(pmt1);
165 TRACE("pmt2: ");
166 dump_AM_MEDIA_TYPE(pmt2);
167 return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
168 ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL) || IsEqualGUID(&pmt2->subtype, &GUID_NULL))) || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
171 /*** Common Base Pin function */
172 HRESULT WINAPI BasePinImpl_GetMediaType(IPin *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(IPin *iface)
181 return 1;
184 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
186 BasePin *This = (BasePin *)iface;
187 ULONG refCount = InterlockedIncrement(&This->refCount);
189 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
191 return refCount;
194 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
196 HRESULT hr;
197 BasePin *This = (BasePin *)iface;
199 TRACE("()\n");
201 EnterCriticalSection(This->pCritSec);
203 if (This->pConnectedTo)
205 IPin_Release(This->pConnectedTo);
206 This->pConnectedTo = NULL;
207 FreeMediaType(&This->mtCurrent);
208 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
209 hr = S_OK;
211 else
212 hr = S_FALSE;
214 LeaveCriticalSection(This->pCritSec);
216 return hr;
219 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
221 HRESULT hr;
222 BasePin *This = (BasePin *)iface;
224 TRACE("(%p)\n", ppPin);
226 EnterCriticalSection(This->pCritSec);
228 if (This->pConnectedTo)
230 *ppPin = This->pConnectedTo;
231 IPin_AddRef(*ppPin);
232 hr = S_OK;
234 else
236 hr = VFW_E_NOT_CONNECTED;
237 *ppPin = NULL;
240 LeaveCriticalSection(This->pCritSec);
242 return hr;
245 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
247 HRESULT hr;
248 BasePin *This = (BasePin *)iface;
250 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
252 EnterCriticalSection(This->pCritSec);
254 if (This->pConnectedTo)
256 CopyMediaType(pmt, &This->mtCurrent);
257 hr = S_OK;
259 else
261 ZeroMemory(pmt, sizeof(*pmt));
262 hr = VFW_E_NOT_CONNECTED;
265 LeaveCriticalSection(This->pCritSec);
267 return hr;
270 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
272 BasePin *This = (BasePin *)iface;
274 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
276 Copy_PinInfo(pInfo, &This->pinInfo);
277 IBaseFilter_AddRef(pInfo->pFilter);
279 return S_OK;
282 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
284 BasePin *This = (BasePin *)iface;
286 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
288 *pPinDir = This->pinInfo.dir;
290 return S_OK;
293 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
295 BasePin *This = (BasePin *)iface;
297 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
299 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
300 if (!*Id)
301 return E_OUTOFMEMORY;
303 strcpyW(*Id, This->pinInfo.achName);
305 return S_OK;
308 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
310 TRACE("(%p)->(%p)\n", iface, pmt);
312 return S_OK;
315 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
317 BasePin *This = (BasePin *)iface;
319 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
321 /* override this method to allow enumeration of your types */
323 return EnumMediaTypes_Construct(iface, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion , ppEnum);
326 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
328 BasePin *This = (BasePin *)iface;
330 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
332 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
335 /*** OutputPin implementation ***/
337 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
339 BaseOutputPin *This = (BaseOutputPin *)iface;
341 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
343 *ppv = NULL;
345 if (IsEqualIID(riid, &IID_IUnknown))
346 *ppv = iface;
347 else if (IsEqualIID(riid, &IID_IPin))
348 *ppv = iface;
349 else if (IsEqualIID(riid, &IID_IMediaSeeking))
351 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
354 if (*ppv)
356 IUnknown_AddRef((IUnknown *)(*ppv));
357 return S_OK;
360 FIXME("No interface for %s!\n", debugstr_guid(riid));
362 return E_NOINTERFACE;
365 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
367 BaseOutputPin *This = (BaseOutputPin *)iface;
368 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
370 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
372 if (!refCount)
374 FreeMediaType(&This->pin.mtCurrent);
375 CoTaskMemFree(This);
376 return 0;
378 return refCount;
381 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
383 HRESULT hr;
384 BaseOutputPin *This = (BaseOutputPin *)iface;
386 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
387 dump_AM_MEDIA_TYPE(pmt);
389 /* If we try to connect to ourself, we will definitely deadlock.
390 * There are other cases where we could deadlock too, but this
391 * catches the obvious case */
392 assert(pReceivePin != iface);
394 EnterCriticalSection(This->pin.pCritSec);
396 /* if we have been a specific type to connect with, then we can either connect
397 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
398 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
399 hr = This->pAttemptConnection(iface, pReceivePin, pmt);
400 else
402 /* negotiate media type */
404 IEnumMediaTypes * pEnumCandidates;
405 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
407 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
409 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
411 /* try this filter's media types first */
412 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
414 assert(pmtCandidate);
415 dump_AM_MEDIA_TYPE(pmtCandidate);
416 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
417 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
418 assert(pmtCandidate->pbFormat);
419 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
420 (This->pAttemptConnection(iface, pReceivePin, pmtCandidate) == S_OK))
422 hr = S_OK;
423 DeleteMediaType(pmtCandidate);
424 break;
426 DeleteMediaType(pmtCandidate);
427 pmtCandidate = NULL;
429 IEnumMediaTypes_Release(pEnumCandidates);
432 /* then try receiver filter's media types */
433 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
435 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
437 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
439 assert(pmtCandidate);
440 dump_AM_MEDIA_TYPE(pmtCandidate);
441 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
442 (This->pAttemptConnection(iface, pReceivePin, pmtCandidate) == S_OK))
444 hr = S_OK;
445 DeleteMediaType(pmtCandidate);
446 break;
448 DeleteMediaType(pmtCandidate);
449 pmtCandidate = NULL;
450 } /* while */
451 IEnumMediaTypes_Release(pEnumCandidates);
452 } /* if not found */
453 } /* if negotiate media type */
454 } /* if succeeded */
455 LeaveCriticalSection(This->pin.pCritSec);
457 TRACE(" -- %x\n", hr);
458 return hr;
461 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
463 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
465 return E_UNEXPECTED;
468 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
470 HRESULT hr;
471 BaseOutputPin *This = (BaseOutputPin *)iface;
473 TRACE("()\n");
475 EnterCriticalSection(This->pin.pCritSec);
477 if (This->pMemInputPin)
479 IMemInputPin_Release(This->pMemInputPin);
480 This->pMemInputPin = NULL;
482 if (This->pin.pConnectedTo)
484 IPin_Release(This->pin.pConnectedTo);
485 This->pin.pConnectedTo = NULL;
486 FreeMediaType(&This->pin.mtCurrent);
487 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
488 hr = S_OK;
490 else
491 hr = S_FALSE;
493 LeaveCriticalSection(This->pin.pCritSec);
495 return hr;
498 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
500 TRACE("()\n");
502 /* not supposed to do anything in an output pin */
504 return E_UNEXPECTED;
507 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
509 TRACE("(%p)->()\n", iface);
511 /* not supposed to do anything in an output pin */
513 return E_UNEXPECTED;
516 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
518 TRACE("(%p)->()\n", iface);
520 /* not supposed to do anything in an output pin */
522 return E_UNEXPECTED;
525 HRESULT WINAPI BaseOutputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
527 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
529 /* not supposed to do anything in an output pin */
531 return E_UNEXPECTED;
534 static const IPinVtbl OutputPin_Vtbl =
536 BaseOutputPinImpl_QueryInterface,
537 BasePinImpl_AddRef,
538 BaseOutputPinImpl_Release,
539 BaseOutputPinImpl_Connect,
540 BaseOutputPinImpl_ReceiveConnection,
541 BaseOutputPinImpl_Disconnect,
542 BasePinImpl_ConnectedTo,
543 BasePinImpl_ConnectionMediaType,
544 BasePinImpl_QueryPinInfo,
545 BasePinImpl_QueryDirection,
546 BasePinImpl_QueryId,
547 BasePinImpl_QueryAccept,
548 BasePinImpl_EnumMediaTypes,
549 BasePinImpl_QueryInternalConnections,
550 BaseOutputPinImpl_EndOfStream,
551 BaseOutputPinImpl_BeginFlush,
552 BaseOutputPinImpl_EndFlush,
553 BaseOutputPinImpl_NewSegment
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 EnterCriticalSection(This->pin.pCritSec);
564 if (!This->pin.pConnectedTo)
565 hr = VFW_E_NOT_CONNECTED;
566 else
568 IMemAllocator * pAlloc = NULL;
570 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
572 if (SUCCEEDED(hr))
573 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
575 if (SUCCEEDED(hr))
576 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
578 if (pAlloc)
579 IMemAllocator_Release(pAlloc);
582 LeaveCriticalSection(This->pin.pCritSec);
584 return hr;
587 /* replaces OutputPin_SendSample */
588 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
590 HRESULT hr = S_OK;
591 IMemInputPin * pMemConnected = NULL;
592 PIN_INFO pinInfo;
594 EnterCriticalSection(This->pin.pCritSec);
596 if (!This->pin.pConnectedTo || !This->pMemInputPin)
597 hr = VFW_E_NOT_CONNECTED;
598 else
600 /* we don't have the lock held when using This->pMemInputPin,
601 * so we need to AddRef it to stop it being deleted while we are
602 * using it. Same with its filter. */
603 pMemConnected = This->pMemInputPin;
604 IMemInputPin_AddRef(pMemConnected);
605 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
608 LeaveCriticalSection(This->pin.pCritSec);
610 if (SUCCEEDED(hr))
612 /* NOTE: if we are in a critical section when Receive is called
613 * then it causes some problems (most notably with the native Video
614 * Renderer) if we are re-entered for whatever reason */
615 hr = IMemInputPin_Receive(pMemConnected, pSample);
617 /* If the filter's destroyed, tell upstream to stop sending data */
618 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
619 hr = S_FALSE;
621 if (pMemConnected)
622 IMemInputPin_Release(pMemConnected);
624 return hr;
627 /* replaces OutputPin_CommitAllocator */
628 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
630 HRESULT hr = S_OK;
632 TRACE("(%p)->()\n", This);
634 EnterCriticalSection(This->pin.pCritSec);
636 if (!This->pin.pConnectedTo || !This->pMemInputPin)
637 hr = VFW_E_NOT_CONNECTED;
638 else
640 IMemAllocator * pAlloc = NULL;
642 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
644 if (SUCCEEDED(hr))
645 hr = IMemAllocator_Commit(pAlloc);
647 if (pAlloc)
648 IMemAllocator_Release(pAlloc);
651 LeaveCriticalSection(This->pin.pCritSec);
653 TRACE("--> %08x\n", hr);
654 return hr;
657 /* replaces OutputPin_DecommitAllocator */
658 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
660 HRESULT hr = S_OK;
662 TRACE("(%p)->()\n", This);
664 EnterCriticalSection(This->pin.pCritSec);
666 if (!This->pin.pConnectedTo || !This->pMemInputPin)
667 hr = VFW_E_NOT_CONNECTED;
668 else
670 IMemAllocator * pAlloc = NULL;
672 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
674 if (SUCCEEDED(hr))
675 hr = IMemAllocator_Decommit(pAlloc);
677 if (pAlloc)
678 IMemAllocator_Release(pAlloc);
681 LeaveCriticalSection(This->pin.pCritSec);
683 TRACE("--> %08x\n", hr);
684 return hr;
687 /* replaces OutputPin_DeliverDisconnect */
688 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
690 HRESULT hr;
692 TRACE("(%p)->()\n", This);
694 EnterCriticalSection(This->pin.pCritSec);
696 if (!This->pin.pConnectedTo || !This->pMemInputPin)
697 hr = VFW_E_NOT_CONNECTED;
698 else if (!This->custom_allocator)
700 IMemAllocator * pAlloc = NULL;
702 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
704 if (SUCCEEDED(hr))
705 hr = IMemAllocator_Decommit(pAlloc);
707 if (pAlloc)
708 IMemAllocator_Release(pAlloc);
710 if (SUCCEEDED(hr))
711 hr = IPin_Disconnect(This->pin.pConnectedTo);
713 else /* Kill the allocator! */
715 hr = IPin_Disconnect(This->pin.pConnectedTo);
717 IPin_Disconnect((IPin *)This);
719 LeaveCriticalSection(This->pin.pCritSec);
721 return hr;
724 /*** The Construct functions ***/
726 /* Function called as a helper to IPin_Connect */
727 /* specific AM_MEDIA_TYPE - it cannot be NULL */
728 static HRESULT WINAPI OutputPin_AttemptConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
730 BaseOutputPin *This = (BaseOutputPin *)iface;
731 HRESULT hr;
732 IMemAllocator * pMemAlloc = NULL;
733 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
735 TRACE("(%p, %p)\n", pReceivePin, pmt);
736 dump_AM_MEDIA_TYPE(pmt);
738 /* FIXME: call queryacceptproc */
740 This->pin.pConnectedTo = pReceivePin;
741 IPin_AddRef(pReceivePin);
742 CopyMediaType(&This->pin.mtCurrent, pmt);
744 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
746 /* get the IMemInputPin interface we will use to deliver samples to the
747 * connected pin */
748 if (SUCCEEDED(hr))
750 This->pMemInputPin = NULL;
751 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
753 if (SUCCEEDED(hr) && !This->custom_allocator)
755 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
757 if (hr == VFW_E_NO_ALLOCATOR)
758 /* Input pin provides no allocator, use standard memory allocator */
759 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
761 if (SUCCEEDED(hr))
762 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
764 if (SUCCEEDED(hr))
765 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
767 if (pMemAlloc)
768 IMemAllocator_Release(pMemAlloc);
770 else if (SUCCEEDED(hr))
772 if (This->alloc)
774 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
776 else
777 hr = VFW_E_NO_ALLOCATOR;
780 /* break connection if we couldn't get the allocator */
781 if (FAILED(hr))
783 if (This->pMemInputPin)
784 IMemInputPin_Release(This->pMemInputPin);
785 This->pMemInputPin = NULL;
787 IPin_Disconnect(pReceivePin);
791 if (FAILED(hr))
793 IPin_Release(This->pin.pConnectedTo);
794 This->pin.pConnectedTo = NULL;
795 FreeMediaType(&This->pin.mtCurrent);
798 TRACE(" -- %x\n", hr);
799 return hr;
802 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO *
803 pPinInfo, const ALLOCATOR_PROPERTIES * props, BasePin_AttemptConnection pConnectProc, LPCRITICAL_SECTION pCritSec,
804 BaseOutputPin * pPinImpl)
806 TRACE("\n");
808 /* Common attributes */
809 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
810 pPinImpl->pin.refCount = 1;
811 pPinImpl->pin.pConnectedTo = NULL;
812 pPinImpl->pin.pCritSec = pCritSec;
813 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
814 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
816 /* Output pin attributes */
817 pPinImpl->pMemInputPin = NULL;
818 if(pConnectProc)
819 pPinImpl->pAttemptConnection = pConnectProc;
820 else
821 pPinImpl->pAttemptConnection = OutputPin_AttemptConnection;
822 /* If custom_allocator is set, you will need to specify an allocator
823 * in the alloc member of the struct before an output pin can connect
825 pPinImpl->custom_allocator = 0;
826 pPinImpl->alloc = NULL;
827 pPinImpl->readonly = FALSE;
828 if (props)
830 pPinImpl->allocProps = *props;
831 if (pPinImpl->allocProps.cbAlign == 0)
832 pPinImpl->allocProps.cbAlign = 1;
834 else
835 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
837 return S_OK;
840 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, BasePin_AttemptConnection pConnectProc, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
842 BaseOutputPin * pPinImpl;
844 *ppPin = NULL;
846 if (pPinInfo->dir != PINDIR_OUTPUT)
848 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
849 return E_INVALIDARG;
852 assert(outputpin_size >= sizeof(BaseOutputPin));
854 pPinImpl = CoTaskMemAlloc(outputpin_size);
856 if (!pPinImpl)
857 return E_OUTOFMEMORY;
859 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pConnectProc, pCritSec, pPinImpl)))
861 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
862 return S_OK;
865 CoTaskMemFree(pPinImpl);
866 return E_FAIL;
869 /*** Input Pin implementation ***/
871 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
873 BaseInputPin *This = (BaseInputPin *)iface;
875 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
877 *ppv = NULL;
879 if (IsEqualIID(riid, &IID_IUnknown))
880 *ppv = iface;
881 else if (IsEqualIID(riid, &IID_IPin))
882 *ppv = iface;
883 else if (IsEqualIID(riid, &IID_IMemInputPin))
884 *ppv = &This->lpVtblMemInput;
885 else if (IsEqualIID(riid, &IID_IMediaSeeking))
887 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
890 if (*ppv)
892 IUnknown_AddRef((IUnknown *)(*ppv));
893 return S_OK;
896 FIXME("No interface for %s!\n", debugstr_guid(riid));
898 return E_NOINTERFACE;
901 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
903 BaseInputPin *This = (BaseInputPin *)iface;
904 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
906 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
908 if (!refCount)
910 FreeMediaType(&This->pin.mtCurrent);
911 if (This->pAllocator)
912 IMemAllocator_Release(This->pAllocator);
913 This->pAllocator = NULL;
914 This->pin.lpVtbl = NULL;
915 CoTaskMemFree(This);
916 return 0;
918 else
919 return refCount;
922 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
924 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
926 return E_UNEXPECTED;
930 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
932 BaseInputPin *This = (BaseInputPin *)iface;
933 PIN_DIRECTION pindirReceive;
934 HRESULT hr = S_OK;
936 TRACE("(%p, %p)\n", pReceivePin, pmt);
937 dump_AM_MEDIA_TYPE(pmt);
939 EnterCriticalSection(This->pin.pCritSec);
941 if (This->pin.pConnectedTo)
942 hr = VFW_E_ALREADY_CONNECTED;
944 if (SUCCEEDED(hr) && This->fnCheckMediaType((IPin*)This, pmt) != S_OK)
945 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
946 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
948 if (SUCCEEDED(hr))
950 IPin_QueryDirection(pReceivePin, &pindirReceive);
952 if (pindirReceive != PINDIR_OUTPUT)
954 ERR("Can't connect from non-output pin\n");
955 hr = VFW_E_INVALID_DIRECTION;
959 if (SUCCEEDED(hr))
961 CopyMediaType(&This->pin.mtCurrent, pmt);
962 This->pin.pConnectedTo = pReceivePin;
963 IPin_AddRef(pReceivePin);
966 LeaveCriticalSection(This->pin.pCritSec);
968 return hr;
971 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
973 return IPin_EndOfStream( pin );
976 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
978 BaseInputPin *This = (BaseInputPin *)iface;
980 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
982 return (This->fnCheckMediaType((IPin*)This, pmt) == S_OK ? S_OK : S_FALSE);
985 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
987 HRESULT hr = S_OK;
988 BaseInputPin *This = (BaseInputPin *)iface;
990 TRACE("(%p)\n", This);
992 EnterCriticalSection(This->pin.pCritSec);
993 if (This->flushing)
994 hr = S_FALSE;
995 else
996 This->end_of_stream = 1;
997 LeaveCriticalSection(This->pin.pCritSec);
999 if (hr == S_OK)
1000 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
1001 return hr;
1004 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1006 return IPin_BeginFlush( pin );
1009 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1011 BaseInputPin *This = (BaseInputPin *)iface;
1012 HRESULT hr;
1013 TRACE("() semi-stub\n");
1015 EnterCriticalSection(This->pin.pCritSec);
1016 This->flushing = 1;
1018 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1019 LeaveCriticalSection(This->pin.pCritSec);
1021 return hr;
1024 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1026 return IPin_EndFlush( pin );
1029 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1031 BaseInputPin *This = (BaseInputPin *)iface;
1032 HRESULT hr;
1033 TRACE("(%p)\n", This);
1035 EnterCriticalSection(This->pin.pCritSec);
1036 This->flushing = This->end_of_stream = 0;
1038 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1039 LeaveCriticalSection(This->pin.pCritSec);
1041 return hr;
1044 typedef struct newsegmentargs
1046 REFERENCE_TIME tStart, tStop;
1047 double rate;
1048 } newsegmentargs;
1050 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1052 newsegmentargs *args = data;
1053 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1056 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1058 BaseInputPin *This = (BaseInputPin *)iface;
1059 newsegmentargs args;
1061 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1063 args.tStart = This->tStart = tStart;
1064 args.tStop = This->tStop = tStop;
1065 args.rate = This->dRate = dRate;
1067 return SendFurther( iface, deliver_newsegment, &args, NULL );
1070 static const IPinVtbl InputPin_Vtbl =
1072 BaseInputPinImpl_QueryInterface,
1073 BasePinImpl_AddRef,
1074 BaseInputPinImpl_Release,
1075 BaseInputPinImpl_Connect,
1076 BaseInputPinImpl_ReceiveConnection,
1077 BasePinImpl_Disconnect,
1078 BasePinImpl_ConnectedTo,
1079 BasePinImpl_ConnectionMediaType,
1080 BasePinImpl_QueryPinInfo,
1081 BasePinImpl_QueryDirection,
1082 BasePinImpl_QueryId,
1083 BaseInputPinImpl_QueryAccept,
1084 BasePinImpl_EnumMediaTypes,
1085 BasePinImpl_QueryInternalConnections,
1086 BaseInputPinImpl_EndOfStream,
1087 BaseInputPinImpl_BeginFlush,
1088 BaseInputPinImpl_EndFlush,
1089 BaseInputPinImpl_NewSegment
1092 /*** IMemInputPin implementation ***/
1094 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1096 return (BaseInputPin *)((char*)iface - FIELD_OFFSET(BaseInputPin, lpVtblMemInput));
1099 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1101 BaseInputPin *This = impl_from_IMemInputPin(iface);
1103 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
1106 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1108 BaseInputPin *This = impl_from_IMemInputPin(iface);
1110 return IPin_AddRef((IPin *)&This->pin);
1113 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1115 BaseInputPin *This = impl_from_IMemInputPin(iface);
1117 return IPin_Release((IPin *)&This->pin);
1120 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1122 BaseInputPin *This = impl_from_IMemInputPin(iface);
1124 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1126 *ppAllocator = This->pAllocator;
1127 if (*ppAllocator)
1128 IMemAllocator_AddRef(*ppAllocator);
1130 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1133 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1135 BaseInputPin *This = impl_from_IMemInputPin(iface);
1137 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1139 if (bReadOnly)
1140 FIXME("Read only flag not handled yet!\n");
1142 /* FIXME: Should we release the allocator on disconnection? */
1143 if (!pAllocator)
1145 WARN("Null allocator\n");
1146 return E_POINTER;
1149 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1150 return E_FAIL;
1152 if (This->pAllocator)
1153 IMemAllocator_Release(This->pAllocator);
1154 This->pAllocator = pAllocator;
1155 if (This->pAllocator)
1156 IMemAllocator_AddRef(This->pAllocator);
1158 return S_OK;
1161 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1163 BaseInputPin *This = impl_from_IMemInputPin(iface);
1165 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1167 /* override this method if you have any specific requirements */
1169 return E_NOTIMPL;
1172 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1174 BaseInputPin *This = impl_from_IMemInputPin(iface);
1175 HRESULT hr;
1177 /* this trace commented out for performance reasons */
1178 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1179 hr = This->fnReceive((IPin*)This, pSample);
1180 return hr;
1183 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1185 HRESULT hr = S_OK;
1186 BaseInputPin *This = impl_from_IMemInputPin(iface);
1188 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1190 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1192 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1193 if (hr != S_OK)
1194 break;
1197 return hr;
1200 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1202 BaseInputPin *This = impl_from_IMemInputPin(iface);
1204 TRACE("(%p/%p)->()\n", This, iface);
1206 return S_OK;
1209 static const IMemInputPinVtbl MemInputPin_Vtbl =
1211 MemInputPin_QueryInterface,
1212 MemInputPin_AddRef,
1213 MemInputPin_Release,
1214 MemInputPin_GetAllocator,
1215 MemInputPin_NotifyAllocator,
1216 MemInputPin_GetAllocatorRequirements,
1217 MemInputPin_Receive,
1218 MemInputPin_ReceiveMultiple,
1219 MemInputPin_ReceiveCanBlock
1222 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1223 BasePin_CheckMediaType pCheckMediaType, BaseInputPin_Receive pReceive,
1224 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1226 TRACE("\n");
1228 /* Common attributes */
1229 pPinImpl->pin.refCount = 1;
1230 pPinImpl->pin.pConnectedTo = NULL;
1231 pPinImpl->pin.pCritSec = pCritSec;
1232 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1233 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1235 /* Input pin attributes */
1236 pPinImpl->fnCheckMediaType = pCheckMediaType;
1237 pPinImpl->fnReceive = pReceive;
1238 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1239 if (pPinImpl->preferred_allocator)
1240 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1241 pPinImpl->tStart = 0;
1242 pPinImpl->tStop = 0;
1243 pPinImpl->dRate = 1.0;
1244 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1245 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1246 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1248 return S_OK;
1251 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1252 BasePin_CheckMediaType pCheckMediaType, BaseInputPin_Receive pReceive,
1253 LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1255 BaseInputPin * pPinImpl;
1257 *ppPin = NULL;
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, pCheckMediaType, pReceive, pCritSec, allocator, pPinImpl)))
1272 *ppPin = (IPin *)pPinImpl;
1273 return S_OK;
1276 CoTaskMemFree(pPinImpl);
1277 return E_FAIL;