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 InputPin_QueryInterface,
498 InputPin_ReceiveConnection,
500 IPinImpl_ConnectedTo,
501 IPinImpl_ConnectionMediaType,
502 IPinImpl_QueryPinInfo,
503 IPinImpl_QueryDirection,
505 IPinImpl_QueryAccept,
506 IPinImpl_EnumMediaTypes,
507 IPinImpl_QueryInternalConnections,
508 InputPin_EndOfStream,
514 /*** IMemInputPin implementation ***/
516 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
518 ICOM_THIS_From_IMemInputPin(InputPin, iface);
520 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
523 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
525 ICOM_THIS_From_IMemInputPin(InputPin, iface);
527 return IPin_AddRef((IPin *)&This->pin);
530 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
532 ICOM_THIS_From_IMemInputPin(InputPin, iface);
534 return IPin_Release((IPin *)&This->pin);
537 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
539 ICOM_THIS_From_IMemInputPin(InputPin, iface);
541 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
543 *ppAllocator = This->pAllocator;
545 IMemAllocator_AddRef(*ppAllocator);
547 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
550 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
552 ICOM_THIS_From_IMemInputPin(InputPin, iface);
554 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
556 if (This->pAllocator)
557 IMemAllocator_Release(This->pAllocator);
558 This->pAllocator = pAllocator;
559 if (This->pAllocator)
560 IMemAllocator_AddRef(This->pAllocator);
565 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
567 ICOM_THIS_From_IMemInputPin(InputPin, iface);
569 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
571 /* override this method if you have any specific requirements */
576 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
578 ICOM_THIS_From_IMemInputPin(InputPin, iface);
580 /* this trace commented out for performance reasons */
581 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
583 return This->fnSampleProc(This->pin.pUserData, pSample);
586 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
589 ICOM_THIS_From_IMemInputPin(InputPin, iface);
591 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
593 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
595 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
603 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
605 ICOM_THIS_From_IMemInputPin(InputPin, iface);
607 FIXME("(%p/%p)->()\n", This, iface);
609 /* FIXME: we should check whether any output pins will block */
614 static const IMemInputPinVtbl MemInputPin_Vtbl =
616 MemInputPin_QueryInterface,
619 MemInputPin_GetAllocator,
620 MemInputPin_NotifyAllocator,
621 MemInputPin_GetAllocatorRequirements,
623 MemInputPin_ReceiveMultiple,
624 MemInputPin_ReceiveCanBlock
627 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
629 ICOM_THIS(OutputPin, iface);
631 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
635 if (IsEqualIID(riid, &IID_IUnknown))
636 *ppv = (LPVOID)iface;
637 else if (IsEqualIID(riid, &IID_IPin))
638 *ppv = (LPVOID)iface;
642 IUnknown_AddRef((IUnknown *)(*ppv));
646 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
648 return E_NOINTERFACE;
651 ULONG WINAPI OutputPin_Release(IPin * iface)
653 ICOM_THIS(OutputPin, iface);
655 TRACE("(%p/%p)->()\n", This, iface);
657 if (!InterlockedDecrement(&This->pin.refCount))
659 DeleteMediaType(&This->pin.mtCurrent);
663 return This->pin.refCount;
666 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
669 ICOM_THIS(OutputPin, iface);
671 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
672 dump_AM_MEDIA_TYPE(pmt);
674 /* If we try to connect to ourself, we will definitely deadlock.
675 * There are other cases where we could deadlock too, but this
676 * catches the obvious case */
677 assert(pReceivePin != iface);
679 EnterCriticalSection(This->pin.pCritSec);
681 /* if we have been a specific type to connect with, then we can either connect
682 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
683 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
684 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
687 /* negotiate media type */
689 IEnumMediaTypes * pEnumCandidates;
690 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
692 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
694 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
696 /* try this filter's media types first */
697 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
699 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
700 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
703 CoTaskMemFree(pmtCandidate);
706 CoTaskMemFree(pmtCandidate);
708 IEnumMediaTypes_Release(pEnumCandidates);
711 /* then try receiver filter's media types */
712 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
714 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
716 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
718 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
719 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
722 CoTaskMemFree(pmtCandidate);
725 CoTaskMemFree(pmtCandidate);
727 IEnumMediaTypes_Release(pEnumCandidates);
729 } /* if negotiate media type */
731 LeaveCriticalSection(This->pin.pCritSec);
733 FIXME(" -- %lx\n", hr);
737 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
739 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
744 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
747 ICOM_THIS(OutputPin, iface);
751 EnterCriticalSection(This->pin.pCritSec);
753 if (This->pMemInputPin)
755 IMemInputPin_Release(This->pMemInputPin);
756 This->pMemInputPin = NULL;
758 if (This->pin.pConnectedTo)
760 IPin_Release(This->pin.pConnectedTo);
761 This->pin.pConnectedTo = NULL;
767 LeaveCriticalSection(This->pin.pCritSec);
772 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
776 /* not supposed to do anything in an output pin */
781 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
783 TRACE("(%p)->()\n", iface);
785 /* not supposed to do anything in an output pin */
790 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
792 TRACE("(%p)->()\n", iface);
794 /* not supposed to do anything in an output pin */
799 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
801 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
803 /* not supposed to do anything in an output pin */
808 static const IPinVtbl OutputPin_Vtbl =
810 OutputPin_QueryInterface,
814 OutputPin_ReceiveConnection,
815 OutputPin_Disconnect,
816 IPinImpl_ConnectedTo,
817 IPinImpl_ConnectionMediaType,
818 IPinImpl_QueryPinInfo,
819 IPinImpl_QueryDirection,
821 IPinImpl_QueryAccept,
822 IPinImpl_EnumMediaTypes,
823 IPinImpl_QueryInternalConnections,
824 OutputPin_EndOfStream,
825 OutputPin_BeginFlush,
830 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
834 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
836 EnterCriticalSection(This->pin.pCritSec);
838 if (!This->pin.pConnectedTo)
839 hr = VFW_E_NOT_CONNECTED;
842 IMemAllocator * pAlloc = NULL;
844 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
847 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
850 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
853 IMemAllocator_Release(pAlloc);
856 LeaveCriticalSection(This->pin.pCritSec);
861 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
864 IMemInputPin * pMemConnected = NULL;
866 EnterCriticalSection(This->pin.pCritSec);
868 if (!This->pin.pConnectedTo || !This->pMemInputPin)
869 hr = VFW_E_NOT_CONNECTED;
872 /* we don't have the lock held when using This->pMemInputPin,
873 * so we need to AddRef it to stop it being deleted while we are
875 pMemConnected = This->pMemInputPin;
876 IMemInputPin_AddRef(pMemConnected);
879 LeaveCriticalSection(This->pin.pCritSec);
883 /* NOTE: if we are in a critical section when Receive is called
884 * then it causes some problems (most notably with the native Video
885 * Renderer) if we are re-entered for whatever reason */
886 hr = IMemInputPin_Receive(pMemConnected, pSample);
887 IMemInputPin_Release(pMemConnected);
893 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
897 EnterCriticalSection(This->pin.pCritSec);
899 if (!This->pin.pConnectedTo)
900 hr = VFW_E_NOT_CONNECTED;
902 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
904 LeaveCriticalSection(This->pin.pCritSec);
909 HRESULT OutputPin_CommitAllocator(OutputPin * This)
913 TRACE("(%p)->()\n", This);
915 EnterCriticalSection(This->pin.pCritSec);
917 if (!This->pin.pConnectedTo || !This->pMemInputPin)
918 hr = VFW_E_NOT_CONNECTED;
921 IMemAllocator * pAlloc = NULL;
923 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
926 hr = IMemAllocator_Commit(pAlloc);
929 IMemAllocator_Release(pAlloc);
932 LeaveCriticalSection(This->pin.pCritSec);
937 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
941 TRACE("(%p)->()\n", This);
943 EnterCriticalSection(This->pin.pCritSec);
945 if (!This->pin.pConnectedTo || !This->pMemInputPin)
946 hr = VFW_E_NOT_CONNECTED;
949 IMemAllocator * pAlloc = NULL;
951 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
954 hr = IMemAllocator_Decommit(pAlloc);
957 IMemAllocator_Release(pAlloc);
960 hr = IPin_Disconnect(This->pin.pConnectedTo);
963 LeaveCriticalSection(This->pin.pCritSec);
969 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
975 if (pPinInfo->dir != PINDIR_INPUT)
977 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
981 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
984 return E_OUTOFMEMORY;
986 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
988 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
990 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
996 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
998 /* Common attributes */
999 pPinImpl->pin.refCount = 1;
1000 pPinImpl->pin.pConnectedTo = NULL;
1001 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1002 pPinImpl->pin.pUserData = pUserData;
1003 pPinImpl->pin.pCritSec = pCritSec;
1004 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1006 /* Input pin attributes */
1007 pPinImpl->fnSampleProc = pSampleProc;
1008 pPinImpl->fnPreConnect = NULL;
1009 pPinImpl->pAlloc = NULL;
1010 pPinImpl->pReader = NULL;
1011 pPinImpl->hThread = NULL;
1012 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1014 pPinImpl->rtStart = 0;
1015 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1020 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1022 PIN_DIRECTION pindirReceive;
1024 ICOM_THIS(PullPin, iface);
1026 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1027 dump_AM_MEDIA_TYPE(pmt);
1029 EnterCriticalSection(This->pin.pCritSec);
1031 if (This->pin.pConnectedTo)
1032 hr = VFW_E_ALREADY_CONNECTED;
1034 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1035 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1036 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1040 IPin_QueryDirection(pReceivePin, &pindirReceive);
1042 if (pindirReceive != PINDIR_OUTPUT)
1044 ERR("Can't connect from non-output pin\n");
1045 hr = VFW_E_INVALID_DIRECTION;
1051 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1056 ALLOCATOR_PROPERTIES props;
1058 props.cbBuffer = 64 * 1024; /* 64k bytes */
1061 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1064 if (SUCCEEDED(hr) && This->fnPreConnect)
1066 hr = This->fnPreConnect(iface, pReceivePin);
1071 CopyMediaType(&This->pin.mtCurrent, pmt);
1072 This->pin.pConnectedTo = pReceivePin;
1073 IPin_AddRef(pReceivePin);
1076 LeaveCriticalSection(This->pin.pCritSec);
1080 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1082 ICOM_THIS(PullPin, iface);
1084 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1088 if (IsEqualIID(riid, &IID_IUnknown))
1089 *ppv = (LPVOID)iface;
1090 else if (IsEqualIID(riid, &IID_IPin))
1091 *ppv = (LPVOID)iface;
1095 IUnknown_AddRef((IUnknown *)(*ppv));
1099 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1101 return E_NOINTERFACE;
1104 ULONG WINAPI PullPin_Release(IPin * iface)
1106 ICOM_THIS(PullPin, iface);
1108 TRACE("(%p/%p)->()\n", This, iface);
1110 if (!InterlockedDecrement(&This->pin.refCount))
1113 PullPin_StopProcessing(This);
1114 IMemAllocator_Release(This->pAlloc);
1115 IAsyncReader_Release(This->pReader);
1116 CloseHandle(This->hEventStateChanged);
1117 CoTaskMemFree(This);
1120 return This->pin.refCount;
1123 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1126 SleepEx(INFINITE, TRUE);
1129 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1131 ICOM_THIS(PullPin, iface);
1134 REFERENCE_TIME rtCurrent;
1135 ALLOCATOR_PROPERTIES allocProps;
1137 SetEvent(This->hEventStateChanged);
1139 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1141 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1143 while (rtCurrent < This->rtStop)
1145 /* FIXME: to improve performance by quite a bit this should be changed
1146 * so that one sample is processed while one sample is fetched. However,
1147 * it is harder to debug so for the moment it will stay as it is */
1148 IMediaSample * pSample = NULL;
1149 REFERENCE_TIME rtSampleStop;
1152 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1156 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1157 if (rtSampleStop > This->rtStop)
1158 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1159 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1160 rtCurrent = rtSampleStop;
1164 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1167 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1170 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1172 ERR("Processing error: %lx\n", hr);
1175 IMediaSample_Release(pSample);
1179 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1181 ICOM_THIS(PullPin, iface);
1183 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1185 EnterCriticalSection(This->pin.pCritSec);
1189 CloseHandle(This->hThread);
1190 This->hThread = NULL;
1191 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1192 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1194 LeaveCriticalSection(This->pin.pCritSec);
1196 SetEvent(This->hEventStateChanged);
1201 HRESULT PullPin_InitProcessing(PullPin * This)
1205 TRACE("(%p)->()\n", This);
1207 assert(!This->hThread);
1209 /* if we are connected */
1212 EnterCriticalSection(This->pin.pCritSec);
1215 assert(!This->hThread);
1217 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1219 hr = HRESULT_FROM_WIN32(GetLastError());
1222 hr = IMemAllocator_Commit(This->pAlloc);
1224 LeaveCriticalSection(This->pin.pCritSec);
1227 TRACE(" -- %lx\n", hr);
1232 HRESULT PullPin_StartProcessing(PullPin * This)
1234 /* if we are connected */
1235 TRACE("(%p)->()\n", This);
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("(%p)->(%lx%08lx, %lx%08lx)\n", This, (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)
1297 FIXME("(%p)->()\n", iface);
1301 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1303 FIXME("(%p)->()\n", iface);
1307 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1309 FIXME("(%p)->()\n", iface);
1313 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1315 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1319 static const IPinVtbl PullPin_Vtbl =
1321 PullPin_QueryInterface,
1325 PullPin_ReceiveConnection,
1326 IPinImpl_Disconnect,
1327 IPinImpl_ConnectedTo,
1328 IPinImpl_ConnectionMediaType,
1329 IPinImpl_QueryPinInfo,
1330 IPinImpl_QueryDirection,
1332 IPinImpl_QueryAccept,
1333 IPinImpl_EnumMediaTypes,
1334 IPinImpl_QueryInternalConnections,
1335 PullPin_EndOfStream,