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 /* Internal 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)
250 ICOM_THIS(IPinImpl, iface);
252 /* TRACE("(%p)\n", ppPin);*/
254 *ppPin = This->pConnectedTo;
262 return VFW_E_NOT_CONNECTED;
265 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
268 ICOM_THIS(IPinImpl, iface);
270 TRACE("(%p)\n", pmt);
272 EnterCriticalSection(This->pCritSec);
274 if (This->pConnectedTo)
276 CopyMediaType(pmt, &This->mtCurrent);
281 ZeroMemory(pmt, sizeof(*pmt));
282 hr = VFW_E_NOT_CONNECTED;
285 LeaveCriticalSection(This->pCritSec);
290 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
292 ICOM_THIS(IPinImpl, iface);
294 TRACE("(%p)\n", pInfo);
296 Copy_PinInfo(pInfo, &This->pinInfo);
301 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
303 ICOM_THIS(IPinImpl, iface);
305 TRACE("(%p)\n", pPinDir);
307 *pPinDir = This->pinInfo.dir;
312 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
314 ICOM_THIS(IPinImpl, iface);
318 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
320 return E_OUTOFMEMORY;
322 strcpyW(*Id, This->pinInfo.achName);
327 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
329 ICOM_THIS(IPinImpl, iface);
331 TRACE("(%p)\n", pmt);
333 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
336 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
338 ENUMMEDIADETAILS emd;
340 TRACE("(%p)\n", ppEnum);
342 /* override this method to allow enumeration of your types */
344 emd.pMediaTypes = NULL;
346 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
349 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
351 TRACE("(%p, %p)\n", apPin, cPin);
353 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
356 /*** IPin implementation for an input pin ***/
358 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
360 ICOM_THIS(InputPin, iface);
362 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
366 if (IsEqualIID(riid, &IID_IUnknown))
367 *ppv = (LPVOID)iface;
368 else if (IsEqualIID(riid, &IID_IPin))
369 *ppv = (LPVOID)iface;
370 else if (IsEqualIID(riid, &IID_IMemInputPin))
371 *ppv = (LPVOID)&This->lpVtblMemInput;
375 IUnknown_AddRef((IUnknown *)(*ppv));
379 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
381 return E_NOINTERFACE;
384 ULONG WINAPI InputPin_Release(IPin * iface)
386 ICOM_THIS(InputPin, iface);
390 if (!InterlockedDecrement(&This->pin.refCount))
392 DeleteMediaType(&This->pin.mtCurrent);
393 if (This->pAllocator)
394 IMemAllocator_Release(This->pAllocator);
399 return This->pin.refCount;
402 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
404 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
410 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
412 ICOM_THIS(InputPin, iface);
413 PIN_DIRECTION pindirReceive;
416 TRACE("(%p, %p)\n", pReceivePin, pmt);
417 dump_AM_MEDIA_TYPE(pmt);
419 EnterCriticalSection(This->pin.pCritSec);
421 if (This->pin.pConnectedTo)
422 hr = VFW_E_ALREADY_CONNECTED;
424 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
425 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
426 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
430 IPin_QueryDirection(pReceivePin, &pindirReceive);
432 if (pindirReceive != PINDIR_OUTPUT)
434 ERR("Can't connect from non-output pin\n");
435 hr = VFW_E_INVALID_DIRECTION;
441 CopyMediaType(&This->pin.mtCurrent, pmt);
442 This->pin.pConnectedTo = pReceivePin;
443 IPin_AddRef(pReceivePin);
446 LeaveCriticalSection(This->pin.pCritSec);
451 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
458 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
464 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
470 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
472 ICOM_THIS(InputPin, iface);
474 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
476 This->tStart = tStart;
483 static const IPinVtbl InputPin_Vtbl =
485 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
486 InputPin_QueryInterface,
490 InputPin_ReceiveConnection,
492 IPinImpl_ConnectedTo,
493 IPinImpl_ConnectionMediaType,
494 IPinImpl_QueryPinInfo,
495 IPinImpl_QueryDirection,
497 IPinImpl_QueryAccept,
498 IPinImpl_EnumMediaTypes,
499 IPinImpl_QueryInternalConnections,
500 InputPin_EndOfStream,
506 /*** IMemInputPin implementation ***/
508 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
510 ICOM_THIS_From_IMemInputPin(InputPin, iface);
512 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
515 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
517 ICOM_THIS_From_IMemInputPin(InputPin, iface);
519 return IPin_AddRef((IPin *)&This->pin);
522 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
524 ICOM_THIS_From_IMemInputPin(InputPin, iface);
526 return IPin_Release((IPin *)&This->pin);
529 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
531 ICOM_THIS_From_IMemInputPin(InputPin, iface);
533 TRACE("MemInputPin_GetAllocator()\n");
535 *ppAllocator = This->pAllocator;
537 IMemAllocator_AddRef(*ppAllocator);
539 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
542 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
544 ICOM_THIS_From_IMemInputPin(InputPin, iface);
548 if (This->pAllocator)
549 IMemAllocator_Release(This->pAllocator);
550 This->pAllocator = pAllocator;
551 if (This->pAllocator)
552 IMemAllocator_AddRef(This->pAllocator);
557 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
559 TRACE("(%p)\n", pProps);
561 /* override this method if you have any specific requirements */
566 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
568 ICOM_THIS_From_IMemInputPin(InputPin, iface);
570 /* this trace commented out for performance reasons */
571 /* TRACE("(%p)\n", pSample);*/
573 return This->fnSampleProc(This->pin.pUserData, pSample);
576 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
579 TRACE("(%p, %ld, %p)\n", pSamples, nSamples, nSamplesProcessed);
581 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
583 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
591 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
595 /* FIXME: we should check whether any output pins will block */
600 static const IMemInputPinVtbl MemInputPin_Vtbl =
602 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
603 MemInputPin_QueryInterface,
606 MemInputPin_GetAllocator,
607 MemInputPin_NotifyAllocator,
608 MemInputPin_GetAllocatorRequirements,
610 MemInputPin_ReceiveMultiple,
611 MemInputPin_ReceiveCanBlock
614 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
616 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
620 if (IsEqualIID(riid, &IID_IUnknown))
621 *ppv = (LPVOID)iface;
622 else if (IsEqualIID(riid, &IID_IPin))
623 *ppv = (LPVOID)iface;
627 IUnknown_AddRef((IUnknown *)(*ppv));
631 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
633 return E_NOINTERFACE;
636 ULONG WINAPI OutputPin_Release(IPin * iface)
638 ICOM_THIS(OutputPin, iface);
642 if (!InterlockedDecrement(&This->pin.refCount))
644 DeleteMediaType(&This->pin.mtCurrent);
648 return This->pin.refCount;
651 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
654 ICOM_THIS(OutputPin, iface);
656 TRACE("(%p, %p)\n", pReceivePin, pmt);
657 dump_AM_MEDIA_TYPE(pmt);
659 /* If we try to connect to ourself, we will definitely deadlock.
660 * There are other cases where we could deadlock too, but this
661 * catches the obvious case */
662 assert(pReceivePin != iface);
664 EnterCriticalSection(This->pin.pCritSec);
666 /* if we have been a specific type to connect with, then we can either connect
667 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
668 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
669 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
672 /* negotiate media type */
674 IEnumMediaTypes * pEnumCandidates;
675 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
677 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
679 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
681 /* try this filter's media types first */
682 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
684 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
685 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
688 CoTaskMemFree(pmtCandidate);
691 CoTaskMemFree(pmtCandidate);
693 IEnumMediaTypes_Release(pEnumCandidates);
696 /* then try receiver filter's media types */
697 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
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);
712 } /* if negotiate media type */
714 LeaveCriticalSection(This->pin.pCritSec);
716 FIXME(" -- %lx\n", hr);
720 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
722 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
727 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
730 ICOM_THIS(OutputPin, iface);
734 EnterCriticalSection(This->pin.pCritSec);
736 if (This->pMemInputPin)
738 IMemInputPin_Release(This->pMemInputPin);
739 This->pMemInputPin = NULL;
741 if (This->pin.pConnectedTo)
743 IPin_Release(This->pin.pConnectedTo);
744 This->pin.pConnectedTo = NULL;
750 LeaveCriticalSection(This->pin.pCritSec);
755 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
759 /* not supposed to do anything in an output pin */
764 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
768 /* not supposed to do anything in an output pin */
773 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
777 /* not supposed to do anything in an output pin */
782 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
784 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
786 /* not supposed to do anything in an output pin */
791 static const IPinVtbl OutputPin_Vtbl =
793 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
794 OutputPin_QueryInterface,
798 OutputPin_ReceiveConnection,
799 OutputPin_Disconnect,
800 IPinImpl_ConnectedTo,
801 IPinImpl_ConnectionMediaType,
802 IPinImpl_QueryPinInfo,
803 IPinImpl_QueryDirection,
805 IPinImpl_QueryAccept,
806 IPinImpl_EnumMediaTypes,
807 IPinImpl_QueryInternalConnections,
808 OutputPin_EndOfStream,
809 OutputPin_BeginFlush,
814 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
818 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
820 EnterCriticalSection(This->pin.pCritSec);
822 if (!This->pin.pConnectedTo)
823 hr = VFW_E_NOT_CONNECTED;
826 IMemInputPin * pMemInputPin = NULL;
827 IMemAllocator * pAlloc = NULL;
829 /* FIXME: should we cache this? */
830 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
833 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
836 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
839 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
842 IMemAllocator_Release(pAlloc);
844 IMemInputPin_Release(pMemInputPin);
847 LeaveCriticalSection(This->pin.pCritSec);
852 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
855 IMemInputPin * pMemConnected = NULL;
857 EnterCriticalSection(This->pin.pCritSec);
859 if (!This->pin.pConnectedTo)
860 hr = VFW_E_NOT_CONNECTED;
863 /* FIXME: should we cache this? - if we do, then we need to keep the local version
864 * and AddRef here instead */
865 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemConnected);
868 LeaveCriticalSection(This->pin.pCritSec);
870 if (SUCCEEDED(hr) && pMemConnected)
872 /* NOTE: if we are in a critical section when Receive is called
873 * then it causes some problems (most notably with the native Video
874 * Renderer) if we are re-entered for whatever reason */
875 hr = IMemInputPin_Receive(pMemConnected, pSample);
876 IMemInputPin_Release(pMemConnected);
882 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
886 EnterCriticalSection(This->pin.pCritSec);
888 if (!This->pin.pConnectedTo)
889 hr = VFW_E_NOT_CONNECTED;
891 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
893 LeaveCriticalSection(This->pin.pCritSec);
898 HRESULT OutputPin_CommitAllocator(OutputPin * This)
904 EnterCriticalSection(This->pin.pCritSec);
906 if (!This->pin.pConnectedTo)
907 hr = VFW_E_NOT_CONNECTED;
910 IMemInputPin * pMemInputPin = NULL;
911 IMemAllocator * pAlloc = NULL;
912 /* FIXME: should we cache this? */
913 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
916 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
919 hr = IMemAllocator_Commit(pAlloc);
922 IMemAllocator_Release(pAlloc);
925 IMemInputPin_Release(pMemInputPin);
928 LeaveCriticalSection(This->pin.pCritSec);
933 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
939 EnterCriticalSection(This->pin.pCritSec);
941 if (!This->pin.pConnectedTo)
942 hr = VFW_E_NOT_CONNECTED;
945 IMemInputPin * pMemInputPin = NULL;
946 IMemAllocator * pAlloc = NULL;
947 /* FIXME: should we cache this? */
948 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
951 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
954 hr = IMemAllocator_Decommit(pAlloc);
957 IMemAllocator_Release(pAlloc);
960 IMemInputPin_Release(pMemInputPin);
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)\n", 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 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
1089 if (IsEqualIID(riid, &IID_IUnknown))
1090 *ppv = (LPVOID)iface;
1091 else if (IsEqualIID(riid, &IID_IPin))
1092 *ppv = (LPVOID)iface;
1096 IUnknown_AddRef((IUnknown *)(*ppv));
1100 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1102 return E_NOINTERFACE;
1105 ULONG WINAPI PullPin_Release(IPin * iface)
1107 ICOM_THIS(PullPin, iface);
1111 if (!InterlockedDecrement(&This->pin.refCount))
1114 PullPin_StopProcessing(This);
1115 IMemAllocator_Release(This->pAlloc);
1116 IAsyncReader_Release(This->pReader);
1117 CloseHandle(This->hEventStateChanged);
1118 CoTaskMemFree(This);
1121 return This->pin.refCount;
1124 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1127 SleepEx(INFINITE, TRUE);
1130 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1132 ICOM_THIS(PullPin, iface);
1135 REFERENCE_TIME rtCurrent;
1136 ALLOCATOR_PROPERTIES allocProps;
1138 SetEvent(This->hEventStateChanged);
1140 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1142 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1144 while (rtCurrent < This->rtStop)
1146 /* FIXME: to improve performance by quite a bit this should be changed
1147 * so that one sample is processed while one sample is fetched. However,
1148 * it is harder to debug so for the moment it will stay as it is */
1149 IMediaSample * pSample = NULL;
1150 REFERENCE_TIME rtSampleStop;
1153 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1157 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1158 if (rtSampleStop > This->rtStop)
1159 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1160 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1161 rtCurrent = rtSampleStop;
1165 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1168 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1171 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1173 ERR("Processing error: %lx\n", hr);
1176 IMemAllocator_ReleaseBuffer(This->pAlloc, pSample);
1180 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1182 ICOM_THIS(PullPin, iface);
1186 EnterCriticalSection(This->pin.pCritSec);
1190 CloseHandle(This->hThread);
1191 This->hThread = NULL;
1192 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1193 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1195 LeaveCriticalSection(This->pin.pCritSec);
1197 SetEvent(This->hEventStateChanged);
1202 HRESULT PullPin_InitProcessing(PullPin * This)
1208 assert(!This->hThread);
1210 /* if we are connected */
1213 EnterCriticalSection(This->pin.pCritSec);
1216 assert(!This->hThread);
1218 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1220 hr = HRESULT_FROM_WIN32(GetLastError());
1223 hr = IMemAllocator_Commit(This->pAlloc);
1225 LeaveCriticalSection(This->pin.pCritSec);
1228 TRACE(" -- %lx\n", hr);
1233 HRESULT PullPin_StartProcessing(PullPin * This)
1235 /* if we are connected */
1238 assert(This->hThread);
1240 ResetEvent(This->hEventStateChanged);
1242 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1243 return HRESULT_FROM_WIN32(GetLastError());
1249 HRESULT PullPin_PauseProcessing(PullPin * This)
1251 /* make the processing function exit its loop */
1257 HRESULT PullPin_StopProcessing(PullPin * This)
1259 /* if we are connected */
1262 assert(This->hThread);
1264 ResetEvent(This->hEventStateChanged);
1266 PullPin_PauseProcessing(This);
1268 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1269 return HRESULT_FROM_WIN32(GetLastError());
1275 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1277 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1282 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1284 FIXME("(%lx%08lx, %lx%08lx)\n", (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1286 PullPin_BeginFlush((IPin *)This);
1287 /* FIXME: need critical section? */
1288 This->rtStart = rtStart;
1289 This->rtStop = rtStop;
1290 PullPin_EndFlush((IPin *)This);
1295 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1301 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1307 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1313 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1319 static const IPinVtbl PullPin_Vtbl =
1321 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1322 PullPin_QueryInterface,
1326 PullPin_ReceiveConnection,
1327 IPinImpl_Disconnect,
1328 IPinImpl_ConnectedTo,
1329 IPinImpl_ConnectionMediaType,
1330 IPinImpl_QueryPinInfo,
1331 IPinImpl_QueryDirection,
1333 IPinImpl_QueryAccept,
1334 IPinImpl_EnumMediaTypes,
1335 IPinImpl_QueryInternalConnections,
1336 PullPin_EndOfStream,