2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "quartz_private.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
32 static const struct IPinVtbl InputPin_Vtbl;
33 static const struct IPinVtbl OutputPin_Vtbl;
34 static const struct IMemInputPinVtbl MemInputPin_Vtbl;
35 static const struct IPinVtbl PullPin_Vtbl;
37 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
40 #define _IMemInputPin_Offset ((int)(&(((InputPin*)0)->lpVtblMemInput)))
41 #define ICOM_THIS_From_IMemInputPin(impl, iface) impl* This = (impl*)(((char*)iface)-_IMemInputPin_Offset);
43 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
45 /* Tempting to just do a memcpy, but the name field is
46 128 characters long! We will probably never exceed 10
47 most of the time, so we are better off copying
48 each field manually */
49 strcpyW(pDest->achName, pSrc->achName);
50 pDest->dir = pSrc->dir;
51 pDest->pFilter = pSrc->pFilter;
52 IBaseFilter_AddRef(pDest->pFilter);
55 /* Function called as a helper to IPin_Connect */
56 /* specific AM_MEDIA_TYPE - it cannot be NULL */
57 /* NOTE: not part of standard interface */
58 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
60 ICOM_THIS(OutputPin, iface);
62 IMemAllocator * pMemAlloc = NULL;
63 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
65 TRACE("(%p, %p)\n", pReceivePin, pmt);
66 dump_AM_MEDIA_TYPE(pmt);
68 /* FIXME: call queryacceptproc */
70 This->pin.pConnectedTo = pReceivePin;
71 IPin_AddRef(pReceivePin);
72 CopyMediaType(&This->pin.mtCurrent, pmt);
74 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
76 /* get the IMemInputPin interface we will use to deliver samples to the
80 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
83 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
86 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
89 IMemAllocator_Release(pMemAlloc);
91 /* break connection if we couldn't get the allocator */
93 IPin_Disconnect(pReceivePin);
98 IPin_Release(This->pin.pConnectedTo);
99 This->pin.pConnectedTo = NULL;
100 DeleteMediaType(&This->pin.mtCurrent);
103 TRACE(" -- %lx\n", hr);
107 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
113 if (pPinInfo->dir != PINDIR_INPUT)
115 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
119 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
122 return E_OUTOFMEMORY;
124 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
126 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
127 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
129 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
135 /* Note that we don't init the vtables here (like C++ constructor) */
136 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
140 /* Common attributes */
141 pPinImpl->pin.refCount = 1;
142 pPinImpl->pin.pConnectedTo = NULL;
143 pPinImpl->pin.fnQueryAccept = pQueryAccept;
144 pPinImpl->pin.pUserData = pUserData;
145 pPinImpl->pin.pCritSec = pCritSec;
146 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
148 /* Input pin attributes */
149 pPinImpl->fnSampleProc = pSampleProc;
150 pPinImpl->pAllocator = NULL;
151 pPinImpl->tStart = 0;
158 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
162 /* Common attributes */
163 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
164 pPinImpl->pin.refCount = 1;
165 pPinImpl->pin.pConnectedTo = NULL;
166 pPinImpl->pin.fnQueryAccept = pQueryAccept;
167 pPinImpl->pin.pUserData = pUserData;
168 pPinImpl->pin.pCritSec = pCritSec;
169 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
171 /* Output pin attributes */
172 pPinImpl->pMemInputPin = NULL;
173 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
176 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
177 if (pPinImpl->allocProps.cbAlign == 0)
178 pPinImpl->allocProps.cbAlign = 1;
181 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
187 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
189 OutputPin * pPinImpl;
193 if (pPinInfo->dir != PINDIR_OUTPUT)
195 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
199 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
202 return E_OUTOFMEMORY;
204 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
206 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
208 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
214 /*** Common pin functions ***/
216 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
218 ICOM_THIS(IPinImpl, iface);
222 return InterlockedIncrement(&This->refCount);
225 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
228 ICOM_THIS(IPinImpl, iface);
232 EnterCriticalSection(This->pCritSec);
234 if (This->pConnectedTo)
236 IPin_Release(This->pConnectedTo);
237 This->pConnectedTo = NULL;
243 LeaveCriticalSection(This->pCritSec);
248 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
251 ICOM_THIS(IPinImpl, iface);
253 /* TRACE("(%p)\n", ppPin);*/
255 EnterCriticalSection(This->pCritSec);
257 if (This->pConnectedTo)
259 *ppPin = This->pConnectedTo;
264 hr = VFW_E_NOT_CONNECTED;
266 LeaveCriticalSection(This->pCritSec);
271 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
274 ICOM_THIS(IPinImpl, iface);
276 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
278 EnterCriticalSection(This->pCritSec);
280 if (This->pConnectedTo)
282 CopyMediaType(pmt, &This->mtCurrent);
287 ZeroMemory(pmt, sizeof(*pmt));
288 hr = VFW_E_NOT_CONNECTED;
291 LeaveCriticalSection(This->pCritSec);
296 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
298 ICOM_THIS(IPinImpl, iface);
300 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
302 Copy_PinInfo(pInfo, &This->pinInfo);
307 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
309 ICOM_THIS(IPinImpl, iface);
311 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
313 *pPinDir = This->pinInfo.dir;
318 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
320 ICOM_THIS(IPinImpl, iface);
322 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
324 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
326 return E_OUTOFMEMORY;
328 strcpyW(*Id, This->pinInfo.achName);
333 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
335 ICOM_THIS(IPinImpl, iface);
337 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
339 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
342 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
344 ICOM_THIS(IPinImpl, iface);
345 ENUMMEDIADETAILS emd;
347 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
349 /* override this method to allow enumeration of your types */
351 emd.pMediaTypes = NULL;
353 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
356 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
358 ICOM_THIS(IPinImpl, iface);
360 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
362 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
365 /*** IPin implementation for an input pin ***/
367 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
369 ICOM_THIS(InputPin, iface);
371 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
375 if (IsEqualIID(riid, &IID_IUnknown))
376 *ppv = (LPVOID)iface;
377 else if (IsEqualIID(riid, &IID_IPin))
378 *ppv = (LPVOID)iface;
379 else if (IsEqualIID(riid, &IID_IMemInputPin))
380 *ppv = (LPVOID)&This->lpVtblMemInput;
384 IUnknown_AddRef((IUnknown *)(*ppv));
388 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
390 return E_NOINTERFACE;
393 ULONG WINAPI InputPin_Release(IPin * iface)
395 ICOM_THIS(InputPin, iface);
399 if (!InterlockedDecrement(&This->pin.refCount))
401 DeleteMediaType(&This->pin.mtCurrent);
402 if (This->pAllocator)
403 IMemAllocator_Release(This->pAllocator);
408 return This->pin.refCount;
411 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
413 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
419 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
421 ICOM_THIS(InputPin, iface);
422 PIN_DIRECTION pindirReceive;
425 TRACE("(%p, %p)\n", pReceivePin, pmt);
426 dump_AM_MEDIA_TYPE(pmt);
428 EnterCriticalSection(This->pin.pCritSec);
430 if (This->pin.pConnectedTo)
431 hr = VFW_E_ALREADY_CONNECTED;
433 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
434 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
435 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
439 IPin_QueryDirection(pReceivePin, &pindirReceive);
441 if (pindirReceive != PINDIR_OUTPUT)
443 ERR("Can't connect from non-output pin\n");
444 hr = VFW_E_INVALID_DIRECTION;
450 CopyMediaType(&This->pin.mtCurrent, pmt);
451 This->pin.pConnectedTo = pReceivePin;
452 IPin_AddRef(pReceivePin);
455 LeaveCriticalSection(This->pin.pCritSec);
460 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
467 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
473 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
479 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
481 ICOM_THIS(InputPin, iface);
483 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
485 This->tStart = tStart;
492 static const IPinVtbl InputPin_Vtbl =
494 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
495 InputPin_QueryInterface,
499 InputPin_ReceiveConnection,
501 IPinImpl_ConnectedTo,
502 IPinImpl_ConnectionMediaType,
503 IPinImpl_QueryPinInfo,
504 IPinImpl_QueryDirection,
506 IPinImpl_QueryAccept,
507 IPinImpl_EnumMediaTypes,
508 IPinImpl_QueryInternalConnections,
509 InputPin_EndOfStream,
515 /*** IMemInputPin implementation ***/
517 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
519 ICOM_THIS_From_IMemInputPin(InputPin, iface);
521 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
524 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
526 ICOM_THIS_From_IMemInputPin(InputPin, iface);
528 return IPin_AddRef((IPin *)&This->pin);
531 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
533 ICOM_THIS_From_IMemInputPin(InputPin, iface);
535 return IPin_Release((IPin *)&This->pin);
538 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
540 ICOM_THIS_From_IMemInputPin(InputPin, iface);
542 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
544 *ppAllocator = This->pAllocator;
546 IMemAllocator_AddRef(*ppAllocator);
548 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
551 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
553 ICOM_THIS_From_IMemInputPin(InputPin, iface);
555 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
557 if (This->pAllocator)
558 IMemAllocator_Release(This->pAllocator);
559 This->pAllocator = pAllocator;
560 if (This->pAllocator)
561 IMemAllocator_AddRef(This->pAllocator);
566 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
568 ICOM_THIS_From_IMemInputPin(InputPin, iface);
570 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
572 /* override this method if you have any specific requirements */
577 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
579 ICOM_THIS_From_IMemInputPin(InputPin, iface);
581 /* this trace commented out for performance reasons */
582 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
584 return This->fnSampleProc(This->pin.pUserData, pSample);
587 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
590 ICOM_THIS_From_IMemInputPin(InputPin, iface);
592 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
594 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
596 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
604 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
606 ICOM_THIS_From_IMemInputPin(InputPin, iface);
608 FIXME("(%p/%p)->()\n", This, iface);
610 /* FIXME: we should check whether any output pins will block */
615 static const IMemInputPinVtbl MemInputPin_Vtbl =
617 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
618 MemInputPin_QueryInterface,
621 MemInputPin_GetAllocator,
622 MemInputPin_NotifyAllocator,
623 MemInputPin_GetAllocatorRequirements,
625 MemInputPin_ReceiveMultiple,
626 MemInputPin_ReceiveCanBlock
629 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
631 ICOM_THIS(OutputPin, iface);
633 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
637 if (IsEqualIID(riid, &IID_IUnknown))
638 *ppv = (LPVOID)iface;
639 else if (IsEqualIID(riid, &IID_IPin))
640 *ppv = (LPVOID)iface;
644 IUnknown_AddRef((IUnknown *)(*ppv));
648 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
650 return E_NOINTERFACE;
653 ULONG WINAPI OutputPin_Release(IPin * iface)
655 ICOM_THIS(OutputPin, iface);
657 TRACE("(%p/%p)->()\n", This, iface);
659 if (!InterlockedDecrement(&This->pin.refCount))
661 DeleteMediaType(&This->pin.mtCurrent);
665 return This->pin.refCount;
668 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
671 ICOM_THIS(OutputPin, iface);
673 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
674 dump_AM_MEDIA_TYPE(pmt);
676 /* If we try to connect to ourself, we will definitely deadlock.
677 * There are other cases where we could deadlock too, but this
678 * catches the obvious case */
679 assert(pReceivePin != iface);
681 EnterCriticalSection(This->pin.pCritSec);
683 /* if we have been a specific type to connect with, then we can either connect
684 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
685 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
686 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
689 /* negotiate media type */
691 IEnumMediaTypes * pEnumCandidates;
692 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
694 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
696 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
698 /* try this filter's media types first */
699 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
701 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
702 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
705 CoTaskMemFree(pmtCandidate);
708 CoTaskMemFree(pmtCandidate);
710 IEnumMediaTypes_Release(pEnumCandidates);
713 /* then try receiver filter's media types */
714 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
716 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
718 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
720 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
721 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
724 CoTaskMemFree(pmtCandidate);
727 CoTaskMemFree(pmtCandidate);
729 IEnumMediaTypes_Release(pEnumCandidates);
731 } /* if negotiate media type */
733 LeaveCriticalSection(This->pin.pCritSec);
735 FIXME(" -- %lx\n", hr);
739 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
741 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
746 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
749 ICOM_THIS(OutputPin, iface);
753 EnterCriticalSection(This->pin.pCritSec);
755 if (This->pMemInputPin)
757 IMemInputPin_Release(This->pMemInputPin);
758 This->pMemInputPin = NULL;
760 if (This->pin.pConnectedTo)
762 IPin_Release(This->pin.pConnectedTo);
763 This->pin.pConnectedTo = NULL;
769 LeaveCriticalSection(This->pin.pCritSec);
774 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
778 /* not supposed to do anything in an output pin */
783 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
785 TRACE("(%p)->()\n", iface);
787 /* not supposed to do anything in an output pin */
792 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
794 TRACE("(%p)->()\n", iface);
796 /* not supposed to do anything in an output pin */
801 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
803 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
805 /* not supposed to do anything in an output pin */
810 static const IPinVtbl OutputPin_Vtbl =
812 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
813 OutputPin_QueryInterface,
817 OutputPin_ReceiveConnection,
818 OutputPin_Disconnect,
819 IPinImpl_ConnectedTo,
820 IPinImpl_ConnectionMediaType,
821 IPinImpl_QueryPinInfo,
822 IPinImpl_QueryDirection,
824 IPinImpl_QueryAccept,
825 IPinImpl_EnumMediaTypes,
826 IPinImpl_QueryInternalConnections,
827 OutputPin_EndOfStream,
828 OutputPin_BeginFlush,
833 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
837 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
839 EnterCriticalSection(This->pin.pCritSec);
841 if (!This->pin.pConnectedTo)
842 hr = VFW_E_NOT_CONNECTED;
845 IMemAllocator * pAlloc = NULL;
847 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
850 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
853 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
856 IMemAllocator_Release(pAlloc);
859 LeaveCriticalSection(This->pin.pCritSec);
864 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
867 IMemInputPin * pMemConnected = NULL;
869 EnterCriticalSection(This->pin.pCritSec);
871 if (!This->pin.pConnectedTo || !This->pMemInputPin)
872 hr = VFW_E_NOT_CONNECTED;
875 /* we don't have the lock held when using This->pMemInputPin,
876 * so we need to AddRef it to stop it being deleted while we are
878 pMemConnected = This->pMemInputPin;
879 IMemInputPin_AddRef(pMemConnected);
882 LeaveCriticalSection(This->pin.pCritSec);
886 /* NOTE: if we are in a critical section when Receive is called
887 * then it causes some problems (most notably with the native Video
888 * Renderer) if we are re-entered for whatever reason */
889 hr = IMemInputPin_Receive(pMemConnected, pSample);
890 IMemInputPin_Release(pMemConnected);
896 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
900 EnterCriticalSection(This->pin.pCritSec);
902 if (!This->pin.pConnectedTo)
903 hr = VFW_E_NOT_CONNECTED;
905 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
907 LeaveCriticalSection(This->pin.pCritSec);
912 HRESULT OutputPin_CommitAllocator(OutputPin * This)
916 TRACE("(%p)->()\n", This);
918 EnterCriticalSection(This->pin.pCritSec);
920 if (!This->pin.pConnectedTo || !This->pMemInputPin)
921 hr = VFW_E_NOT_CONNECTED;
924 IMemAllocator * pAlloc = NULL;
926 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
929 hr = IMemAllocator_Commit(pAlloc);
932 IMemAllocator_Release(pAlloc);
935 LeaveCriticalSection(This->pin.pCritSec);
940 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
944 TRACE("(%p)->()\n", This);
946 EnterCriticalSection(This->pin.pCritSec);
948 if (!This->pin.pConnectedTo || !This->pMemInputPin)
949 hr = VFW_E_NOT_CONNECTED;
952 IMemAllocator * pAlloc = NULL;
954 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
957 hr = IMemAllocator_Decommit(pAlloc);
960 IMemAllocator_Release(pAlloc);
963 hr = IPin_Disconnect(This->pin.pConnectedTo);
966 LeaveCriticalSection(This->pin.pCritSec);
972 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
978 if (pPinInfo->dir != PINDIR_INPUT)
980 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
984 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
987 return E_OUTOFMEMORY;
989 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
991 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
993 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
999 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1001 /* Common attributes */
1002 pPinImpl->pin.refCount = 1;
1003 pPinImpl->pin.pConnectedTo = NULL;
1004 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1005 pPinImpl->pin.pUserData = pUserData;
1006 pPinImpl->pin.pCritSec = pCritSec;
1007 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1009 /* Input pin attributes */
1010 pPinImpl->fnSampleProc = pSampleProc;
1011 pPinImpl->fnPreConnect = NULL;
1012 pPinImpl->pAlloc = NULL;
1013 pPinImpl->pReader = NULL;
1014 pPinImpl->hThread = NULL;
1015 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1017 pPinImpl->rtStart = 0;
1018 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1023 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1025 PIN_DIRECTION pindirReceive;
1027 ICOM_THIS(PullPin, iface);
1029 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1030 dump_AM_MEDIA_TYPE(pmt);
1032 EnterCriticalSection(This->pin.pCritSec);
1034 if (This->pin.pConnectedTo)
1035 hr = VFW_E_ALREADY_CONNECTED;
1037 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1038 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1039 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1043 IPin_QueryDirection(pReceivePin, &pindirReceive);
1045 if (pindirReceive != PINDIR_OUTPUT)
1047 ERR("Can't connect from non-output pin\n");
1048 hr = VFW_E_INVALID_DIRECTION;
1054 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1059 ALLOCATOR_PROPERTIES props;
1061 props.cbBuffer = 64 * 1024; /* 64k bytes */
1064 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1067 if (SUCCEEDED(hr) && This->fnPreConnect)
1069 hr = This->fnPreConnect(iface, pReceivePin);
1074 CopyMediaType(&This->pin.mtCurrent, pmt);
1075 This->pin.pConnectedTo = pReceivePin;
1076 IPin_AddRef(pReceivePin);
1079 LeaveCriticalSection(This->pin.pCritSec);
1083 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1085 ICOM_THIS(PullPin, iface);
1087 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1091 if (IsEqualIID(riid, &IID_IUnknown))
1092 *ppv = (LPVOID)iface;
1093 else if (IsEqualIID(riid, &IID_IPin))
1094 *ppv = (LPVOID)iface;
1098 IUnknown_AddRef((IUnknown *)(*ppv));
1102 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1104 return E_NOINTERFACE;
1107 ULONG WINAPI PullPin_Release(IPin * iface)
1109 ICOM_THIS(PullPin, iface);
1111 TRACE("(%p/%p)->()\n", This, iface);
1113 if (!InterlockedDecrement(&This->pin.refCount))
1116 PullPin_StopProcessing(This);
1117 IMemAllocator_Release(This->pAlloc);
1118 IAsyncReader_Release(This->pReader);
1119 CloseHandle(This->hEventStateChanged);
1120 CoTaskMemFree(This);
1123 return This->pin.refCount;
1126 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1129 SleepEx(INFINITE, TRUE);
1132 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1134 ICOM_THIS(PullPin, iface);
1137 REFERENCE_TIME rtCurrent;
1138 ALLOCATOR_PROPERTIES allocProps;
1140 SetEvent(This->hEventStateChanged);
1142 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1144 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1146 while (rtCurrent < This->rtStop)
1148 /* FIXME: to improve performance by quite a bit this should be changed
1149 * so that one sample is processed while one sample is fetched. However,
1150 * it is harder to debug so for the moment it will stay as it is */
1151 IMediaSample * pSample = NULL;
1152 REFERENCE_TIME rtSampleStop;
1155 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1159 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1160 if (rtSampleStop > This->rtStop)
1161 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1162 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1163 rtCurrent = rtSampleStop;
1167 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1170 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1173 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1175 ERR("Processing error: %lx\n", hr);
1178 IMediaSample_Release(pSample);
1182 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1184 ICOM_THIS(PullPin, iface);
1186 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1188 EnterCriticalSection(This->pin.pCritSec);
1192 CloseHandle(This->hThread);
1193 This->hThread = NULL;
1194 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1195 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1197 LeaveCriticalSection(This->pin.pCritSec);
1199 SetEvent(This->hEventStateChanged);
1204 HRESULT PullPin_InitProcessing(PullPin * This)
1208 TRACE("(%p)->()\n", This);
1210 assert(!This->hThread);
1212 /* if we are connected */
1215 EnterCriticalSection(This->pin.pCritSec);
1218 assert(!This->hThread);
1220 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1222 hr = HRESULT_FROM_WIN32(GetLastError());
1225 hr = IMemAllocator_Commit(This->pAlloc);
1227 LeaveCriticalSection(This->pin.pCritSec);
1230 TRACE(" -- %lx\n", hr);
1235 HRESULT PullPin_StartProcessing(PullPin * This)
1237 /* if we are connected */
1238 TRACE("(%p)->()\n", This);
1241 assert(This->hThread);
1243 ResetEvent(This->hEventStateChanged);
1245 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1246 return HRESULT_FROM_WIN32(GetLastError());
1252 HRESULT PullPin_PauseProcessing(PullPin * This)
1254 /* make the processing function exit its loop */
1260 HRESULT PullPin_StopProcessing(PullPin * This)
1262 /* if we are connected */
1265 assert(This->hThread);
1267 ResetEvent(This->hEventStateChanged);
1269 PullPin_PauseProcessing(This);
1271 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1272 return HRESULT_FROM_WIN32(GetLastError());
1278 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1280 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1285 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1287 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1289 PullPin_BeginFlush((IPin *)This);
1290 /* FIXME: need critical section? */
1291 This->rtStart = rtStart;
1292 This->rtStop = rtStop;
1293 PullPin_EndFlush((IPin *)This);
1298 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1300 FIXME("(%p)->()\n", iface);
1304 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1306 FIXME("(%p)->()\n", iface);
1310 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1312 FIXME("(%p)->()\n", iface);
1316 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1318 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1322 static const IPinVtbl PullPin_Vtbl =
1324 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1325 PullPin_QueryInterface,
1329 PullPin_ReceiveConnection,
1330 IPinImpl_Disconnect,
1331 IPinImpl_ConnectedTo,
1332 IPinImpl_ConnectionMediaType,
1333 IPinImpl_QueryPinInfo,
1334 IPinImpl_QueryDirection,
1336 IPinImpl_QueryAccept,
1337 IPinImpl_EnumMediaTypes,
1338 IPinImpl_QueryInternalConnections,
1339 PullPin_EndOfStream,