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 OutputPin *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);
85 if (hr == VFW_E_NO_ALLOCATOR)
87 /* Input pin provides no allocator, use standard memory allocator */
88 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
92 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
97 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
100 IMemAllocator_Release(pMemAlloc);
102 /* break connection if we couldn't get the allocator */
104 IPin_Disconnect(pReceivePin);
109 IPin_Release(This->pin.pConnectedTo);
110 This->pin.pConnectedTo = NULL;
111 DeleteMediaType(&This->pin.mtCurrent);
114 TRACE(" -- %lx\n", hr);
118 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
124 if (pPinInfo->dir != PINDIR_INPUT)
126 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
130 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
133 return E_OUTOFMEMORY;
135 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
137 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
138 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
140 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
146 /* Note that we don't init the vtables here (like C++ constructor) */
147 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
151 /* Common attributes */
152 pPinImpl->pin.refCount = 1;
153 pPinImpl->pin.pConnectedTo = NULL;
154 pPinImpl->pin.fnQueryAccept = pQueryAccept;
155 pPinImpl->pin.pUserData = pUserData;
156 pPinImpl->pin.pCritSec = pCritSec;
157 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
159 /* Input pin attributes */
160 pPinImpl->fnSampleProc = pSampleProc;
161 pPinImpl->pAllocator = NULL;
162 pPinImpl->tStart = 0;
169 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
173 /* Common attributes */
174 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
175 pPinImpl->pin.refCount = 1;
176 pPinImpl->pin.pConnectedTo = NULL;
177 pPinImpl->pin.fnQueryAccept = pQueryAccept;
178 pPinImpl->pin.pUserData = pUserData;
179 pPinImpl->pin.pCritSec = pCritSec;
180 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
182 /* Output pin attributes */
183 pPinImpl->pMemInputPin = NULL;
184 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
187 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
188 if (pPinImpl->allocProps.cbAlign == 0)
189 pPinImpl->allocProps.cbAlign = 1;
192 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
197 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
199 OutputPin * pPinImpl;
203 if (pPinInfo->dir != PINDIR_OUTPUT)
205 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
209 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
212 return E_OUTOFMEMORY;
214 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
216 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
218 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
224 /*** Common pin functions ***/
226 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
228 IPinImpl *This = (IPinImpl *)iface;
229 ULONG refCount = InterlockedIncrement(&This->refCount);
231 TRACE("(%p)->() AddRef from %ld\n", iface, refCount - 1);
236 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
239 IPinImpl *This = (IPinImpl *)iface;
243 EnterCriticalSection(This->pCritSec);
245 if (This->pConnectedTo)
247 IPin_Release(This->pConnectedTo);
248 This->pConnectedTo = NULL;
254 LeaveCriticalSection(This->pCritSec);
259 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
262 IPinImpl *This = (IPinImpl *)iface;
264 /* TRACE("(%p)\n", ppPin);*/
266 EnterCriticalSection(This->pCritSec);
268 if (This->pConnectedTo)
270 *ppPin = This->pConnectedTo;
275 hr = VFW_E_NOT_CONNECTED;
277 LeaveCriticalSection(This->pCritSec);
282 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
285 IPinImpl *This = (IPinImpl *)iface;
287 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
289 EnterCriticalSection(This->pCritSec);
291 if (This->pConnectedTo)
293 CopyMediaType(pmt, &This->mtCurrent);
298 ZeroMemory(pmt, sizeof(*pmt));
299 hr = VFW_E_NOT_CONNECTED;
302 LeaveCriticalSection(This->pCritSec);
307 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
309 IPinImpl *This = (IPinImpl *)iface;
311 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
313 Copy_PinInfo(pInfo, &This->pinInfo);
318 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
320 IPinImpl *This = (IPinImpl *)iface;
322 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
324 *pPinDir = This->pinInfo.dir;
329 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
331 IPinImpl *This = (IPinImpl *)iface;
333 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
335 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
337 return E_OUTOFMEMORY;
339 strcpyW(*Id, This->pinInfo.achName);
344 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
346 IPinImpl *This = (IPinImpl *)iface;
348 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
350 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
353 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
355 IPinImpl *This = (IPinImpl *)iface;
356 ENUMMEDIADETAILS emd;
358 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
360 /* override this method to allow enumeration of your types */
362 emd.pMediaTypes = NULL;
364 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
367 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
369 IPinImpl *This = (IPinImpl *)iface;
371 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
373 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
376 /*** IPin implementation for an input pin ***/
378 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
380 InputPin *This = (InputPin *)iface;
382 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
386 if (IsEqualIID(riid, &IID_IUnknown))
387 *ppv = (LPVOID)iface;
388 else if (IsEqualIID(riid, &IID_IPin))
389 *ppv = (LPVOID)iface;
390 else if (IsEqualIID(riid, &IID_IMemInputPin))
391 *ppv = (LPVOID)&This->lpVtblMemInput;
395 IUnknown_AddRef((IUnknown *)(*ppv));
399 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
401 return E_NOINTERFACE;
404 ULONG WINAPI InputPin_Release(IPin * iface)
406 InputPin *This = (InputPin *)iface;
407 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
409 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
413 DeleteMediaType(&This->pin.mtCurrent);
414 if (This->pAllocator)
415 IMemAllocator_Release(This->pAllocator);
423 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
425 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
431 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
433 InputPin *This = (InputPin *)iface;
434 PIN_DIRECTION pindirReceive;
437 TRACE("(%p, %p)\n", pReceivePin, pmt);
438 dump_AM_MEDIA_TYPE(pmt);
440 EnterCriticalSection(This->pin.pCritSec);
442 if (This->pin.pConnectedTo)
443 hr = VFW_E_ALREADY_CONNECTED;
445 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
446 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
447 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
451 IPin_QueryDirection(pReceivePin, &pindirReceive);
453 if (pindirReceive != PINDIR_OUTPUT)
455 ERR("Can't connect from non-output pin\n");
456 hr = VFW_E_INVALID_DIRECTION;
462 CopyMediaType(&This->pin.mtCurrent, pmt);
463 This->pin.pConnectedTo = pReceivePin;
464 IPin_AddRef(pReceivePin);
467 LeaveCriticalSection(This->pin.pCritSec);
472 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
479 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
485 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
491 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
493 InputPin *This = (InputPin *)iface;
495 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
497 This->tStart = tStart;
504 static const IPinVtbl InputPin_Vtbl =
506 InputPin_QueryInterface,
510 InputPin_ReceiveConnection,
512 IPinImpl_ConnectedTo,
513 IPinImpl_ConnectionMediaType,
514 IPinImpl_QueryPinInfo,
515 IPinImpl_QueryDirection,
517 IPinImpl_QueryAccept,
518 IPinImpl_EnumMediaTypes,
519 IPinImpl_QueryInternalConnections,
520 InputPin_EndOfStream,
526 /*** IMemInputPin implementation ***/
528 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
530 ICOM_THIS_From_IMemInputPin(InputPin, iface);
532 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
535 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
537 ICOM_THIS_From_IMemInputPin(InputPin, iface);
539 return IPin_AddRef((IPin *)&This->pin);
542 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
544 ICOM_THIS_From_IMemInputPin(InputPin, iface);
546 return IPin_Release((IPin *)&This->pin);
549 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
551 ICOM_THIS_From_IMemInputPin(InputPin, iface);
553 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
555 *ppAllocator = This->pAllocator;
557 IMemAllocator_AddRef(*ppAllocator);
559 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
562 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
564 ICOM_THIS_From_IMemInputPin(InputPin, iface);
566 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
568 if (This->pAllocator)
569 IMemAllocator_Release(This->pAllocator);
570 This->pAllocator = pAllocator;
571 if (This->pAllocator)
572 IMemAllocator_AddRef(This->pAllocator);
577 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
579 ICOM_THIS_From_IMemInputPin(InputPin, iface);
581 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
583 /* override this method if you have any specific requirements */
588 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
590 ICOM_THIS_From_IMemInputPin(InputPin, iface);
592 /* this trace commented out for performance reasons */
593 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
595 return This->fnSampleProc(This->pin.pUserData, pSample);
598 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
601 ICOM_THIS_From_IMemInputPin(InputPin, iface);
603 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
605 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
607 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
615 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
617 ICOM_THIS_From_IMemInputPin(InputPin, iface);
619 FIXME("(%p/%p)->()\n", This, iface);
621 /* FIXME: we should check whether any output pins will block */
626 static const IMemInputPinVtbl MemInputPin_Vtbl =
628 MemInputPin_QueryInterface,
631 MemInputPin_GetAllocator,
632 MemInputPin_NotifyAllocator,
633 MemInputPin_GetAllocatorRequirements,
635 MemInputPin_ReceiveMultiple,
636 MemInputPin_ReceiveCanBlock
639 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
641 OutputPin *This = (OutputPin *)iface;
643 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
647 if (IsEqualIID(riid, &IID_IUnknown))
648 *ppv = (LPVOID)iface;
649 else if (IsEqualIID(riid, &IID_IPin))
650 *ppv = (LPVOID)iface;
654 IUnknown_AddRef((IUnknown *)(*ppv));
658 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
660 return E_NOINTERFACE;
663 ULONG WINAPI OutputPin_Release(IPin * iface)
665 OutputPin *This = (OutputPin *)iface;
666 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
668 TRACE("(%p/%p)->()\n", This, iface);
672 DeleteMediaType(&This->pin.mtCurrent);
679 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
682 OutputPin *This = (OutputPin *)iface;
684 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
685 dump_AM_MEDIA_TYPE(pmt);
687 /* If we try to connect to ourself, we will definitely deadlock.
688 * There are other cases where we could deadlock too, but this
689 * catches the obvious case */
690 assert(pReceivePin != iface);
692 EnterCriticalSection(This->pin.pCritSec);
694 /* if we have been a specific type to connect with, then we can either connect
695 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
696 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
697 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
700 /* negotiate media type */
702 IEnumMediaTypes * pEnumCandidates;
703 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
705 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
707 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
709 /* try this filter's media types first */
710 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
712 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
713 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
716 CoTaskMemFree(pmtCandidate);
719 CoTaskMemFree(pmtCandidate);
721 IEnumMediaTypes_Release(pEnumCandidates);
724 /* then try receiver filter's media types */
725 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
727 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
729 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
731 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
732 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
735 CoTaskMemFree(pmtCandidate);
738 CoTaskMemFree(pmtCandidate);
740 IEnumMediaTypes_Release(pEnumCandidates);
742 } /* if negotiate media type */
744 LeaveCriticalSection(This->pin.pCritSec);
746 TRACE(" -- %lx\n", hr);
750 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
752 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
757 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
760 OutputPin *This = (OutputPin *)iface;
764 EnterCriticalSection(This->pin.pCritSec);
766 if (This->pMemInputPin)
768 IMemInputPin_Release(This->pMemInputPin);
769 This->pMemInputPin = NULL;
771 if (This->pin.pConnectedTo)
773 IPin_Release(This->pin.pConnectedTo);
774 This->pin.pConnectedTo = NULL;
780 LeaveCriticalSection(This->pin.pCritSec);
785 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
789 /* not supposed to do anything in an output pin */
794 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
796 TRACE("(%p)->()\n", iface);
798 /* not supposed to do anything in an output pin */
803 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
805 TRACE("(%p)->()\n", iface);
807 /* not supposed to do anything in an output pin */
812 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
814 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
816 /* not supposed to do anything in an output pin */
821 static const IPinVtbl OutputPin_Vtbl =
823 OutputPin_QueryInterface,
827 OutputPin_ReceiveConnection,
828 OutputPin_Disconnect,
829 IPinImpl_ConnectedTo,
830 IPinImpl_ConnectionMediaType,
831 IPinImpl_QueryPinInfo,
832 IPinImpl_QueryDirection,
834 IPinImpl_QueryAccept,
835 IPinImpl_EnumMediaTypes,
836 IPinImpl_QueryInternalConnections,
837 OutputPin_EndOfStream,
838 OutputPin_BeginFlush,
843 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
847 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
849 EnterCriticalSection(This->pin.pCritSec);
851 if (!This->pin.pConnectedTo)
852 hr = VFW_E_NOT_CONNECTED;
855 IMemAllocator * pAlloc = NULL;
857 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
860 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
863 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
866 IMemAllocator_Release(pAlloc);
869 LeaveCriticalSection(This->pin.pCritSec);
874 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
877 IMemInputPin * pMemConnected = NULL;
879 EnterCriticalSection(This->pin.pCritSec);
881 if (!This->pin.pConnectedTo || !This->pMemInputPin)
882 hr = VFW_E_NOT_CONNECTED;
885 /* we don't have the lock held when using This->pMemInputPin,
886 * so we need to AddRef it to stop it being deleted while we are
888 pMemConnected = This->pMemInputPin;
889 IMemInputPin_AddRef(pMemConnected);
892 LeaveCriticalSection(This->pin.pCritSec);
896 /* NOTE: if we are in a critical section when Receive is called
897 * then it causes some problems (most notably with the native Video
898 * Renderer) if we are re-entered for whatever reason */
899 hr = IMemInputPin_Receive(pMemConnected, pSample);
900 IMemInputPin_Release(pMemConnected);
906 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
910 EnterCriticalSection(This->pin.pCritSec);
912 if (!This->pin.pConnectedTo)
913 hr = VFW_E_NOT_CONNECTED;
915 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
917 LeaveCriticalSection(This->pin.pCritSec);
922 HRESULT OutputPin_CommitAllocator(OutputPin * This)
926 TRACE("(%p)->()\n", This);
928 EnterCriticalSection(This->pin.pCritSec);
930 if (!This->pin.pConnectedTo || !This->pMemInputPin)
931 hr = VFW_E_NOT_CONNECTED;
934 IMemAllocator * pAlloc = NULL;
936 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
939 hr = IMemAllocator_Commit(pAlloc);
942 IMemAllocator_Release(pAlloc);
945 LeaveCriticalSection(This->pin.pCritSec);
950 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
954 TRACE("(%p)->()\n", This);
956 EnterCriticalSection(This->pin.pCritSec);
958 if (!This->pin.pConnectedTo || !This->pMemInputPin)
959 hr = VFW_E_NOT_CONNECTED;
962 IMemAllocator * pAlloc = NULL;
964 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
967 hr = IMemAllocator_Decommit(pAlloc);
970 IMemAllocator_Release(pAlloc);
973 hr = IPin_Disconnect(This->pin.pConnectedTo);
976 LeaveCriticalSection(This->pin.pCritSec);
982 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
988 if (pPinInfo->dir != PINDIR_INPUT)
990 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
994 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
997 return E_OUTOFMEMORY;
999 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1001 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1003 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1009 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1011 /* Common attributes */
1012 pPinImpl->pin.refCount = 1;
1013 pPinImpl->pin.pConnectedTo = NULL;
1014 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1015 pPinImpl->pin.pUserData = pUserData;
1016 pPinImpl->pin.pCritSec = pCritSec;
1017 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1019 /* Input pin attributes */
1020 pPinImpl->fnSampleProc = pSampleProc;
1021 pPinImpl->fnPreConnect = NULL;
1022 pPinImpl->pAlloc = NULL;
1023 pPinImpl->pReader = NULL;
1024 pPinImpl->hThread = NULL;
1025 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1027 pPinImpl->rtStart = 0;
1028 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1033 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1035 PIN_DIRECTION pindirReceive;
1037 PullPin *This = (PullPin *)iface;
1039 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1040 dump_AM_MEDIA_TYPE(pmt);
1042 EnterCriticalSection(This->pin.pCritSec);
1044 if (This->pin.pConnectedTo)
1045 hr = VFW_E_ALREADY_CONNECTED;
1047 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1048 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1049 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1053 IPin_QueryDirection(pReceivePin, &pindirReceive);
1055 if (pindirReceive != PINDIR_OUTPUT)
1057 ERR("Can't connect from non-output pin\n");
1058 hr = VFW_E_INVALID_DIRECTION;
1064 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1069 ALLOCATOR_PROPERTIES props;
1071 props.cbBuffer = 64 * 1024; /* 64k bytes */
1074 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1077 if (SUCCEEDED(hr) && This->fnPreConnect)
1079 hr = This->fnPreConnect(iface, pReceivePin);
1084 CopyMediaType(&This->pin.mtCurrent, pmt);
1085 This->pin.pConnectedTo = pReceivePin;
1086 IPin_AddRef(pReceivePin);
1089 LeaveCriticalSection(This->pin.pCritSec);
1093 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1095 PullPin *This = (PullPin *)iface;
1097 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1101 if (IsEqualIID(riid, &IID_IUnknown))
1102 *ppv = (LPVOID)iface;
1103 else if (IsEqualIID(riid, &IID_IPin))
1104 *ppv = (LPVOID)iface;
1108 IUnknown_AddRef((IUnknown *)(*ppv));
1112 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1114 return E_NOINTERFACE;
1117 ULONG WINAPI PullPin_Release(IPin * iface)
1119 PullPin *This = (PullPin *)iface;
1120 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1122 TRACE("(%p/%p)->()\n", This, iface);
1127 PullPin_StopProcessing(This);
1128 IMemAllocator_Release(This->pAlloc);
1129 IAsyncReader_Release(This->pReader);
1130 CloseHandle(This->hEventStateChanged);
1131 CoTaskMemFree(This);
1137 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1140 SleepEx(INFINITE, TRUE);
1143 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1145 PullPin *This = (PullPin *)iface;
1148 REFERENCE_TIME rtCurrent;
1149 ALLOCATOR_PROPERTIES allocProps;
1151 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1153 SetEvent(This->hEventStateChanged);
1155 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1157 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1161 while (rtCurrent < This->rtStop && hr == S_OK)
1163 /* FIXME: to improve performance by quite a bit this should be changed
1164 * so that one sample is processed while one sample is fetched. However,
1165 * it is harder to debug so for the moment it will stay as it is */
1166 IMediaSample * pSample = NULL;
1167 REFERENCE_TIME rtSampleStop;
1170 TRACE("Process sample\n");
1172 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1176 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1177 if (rtSampleStop > This->rtStop)
1178 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1179 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1180 rtCurrent = rtSampleStop;
1184 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1187 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1190 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1192 ERR("Processing error: %lx\n", hr);
1195 IMediaSample_Release(pSample);
1203 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1205 PullPin *This = (PullPin *)iface;
1207 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1209 EnterCriticalSection(This->pin.pCritSec);
1213 CloseHandle(This->hThread);
1214 This->hThread = NULL;
1215 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1216 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1218 LeaveCriticalSection(This->pin.pCritSec);
1220 SetEvent(This->hEventStateChanged);
1225 HRESULT PullPin_InitProcessing(PullPin * This)
1229 TRACE("(%p)->()\n", This);
1231 assert(!This->hThread);
1233 /* if we are connected */
1236 EnterCriticalSection(This->pin.pCritSec);
1239 assert(!This->hThread);
1241 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1243 hr = HRESULT_FROM_WIN32(GetLastError());
1246 hr = IMemAllocator_Commit(This->pAlloc);
1248 LeaveCriticalSection(This->pin.pCritSec);
1251 TRACE(" -- %lx\n", hr);
1256 HRESULT PullPin_StartProcessing(PullPin * This)
1258 /* if we are connected */
1259 TRACE("(%p)->()\n", This);
1262 assert(This->hThread);
1264 ResetEvent(This->hEventStateChanged);
1266 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1267 return HRESULT_FROM_WIN32(GetLastError());
1273 HRESULT PullPin_PauseProcessing(PullPin * This)
1275 /* make the processing function exit its loop */
1281 HRESULT PullPin_StopProcessing(PullPin * This)
1283 /* if we are connected */
1286 assert(This->hThread);
1288 ResetEvent(This->hEventStateChanged);
1290 PullPin_PauseProcessing(This);
1292 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1293 return HRESULT_FROM_WIN32(GetLastError());
1299 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1301 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1306 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1308 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1310 PullPin_BeginFlush((IPin *)This);
1311 /* FIXME: need critical section? */
1312 This->rtStart = rtStart;
1313 This->rtStop = rtStop;
1314 PullPin_EndFlush((IPin *)This);
1319 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1321 FIXME("(%p)->()\n", iface);
1325 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1327 FIXME("(%p)->()\n", iface);
1331 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1333 FIXME("(%p)->()\n", iface);
1337 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1339 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1343 static const IPinVtbl PullPin_Vtbl =
1345 PullPin_QueryInterface,
1349 PullPin_ReceiveConnection,
1350 IPinImpl_Disconnect,
1351 IPinImpl_ConnectedTo,
1352 IPinImpl_ConnectionMediaType,
1353 IPinImpl_QueryPinInfo,
1354 IPinImpl_QueryDirection,
1356 IPinImpl_QueryAccept,
1357 IPinImpl_EnumMediaTypes,
1358 IPinImpl_QueryInternalConnections,
1359 PullPin_EndOfStream,