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;
230 TRACE("(%p)->() AddRef from %ld\n", iface, This->refCount);
232 return InterlockedIncrement(&This->refCount);
235 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
238 IPinImpl *This = (IPinImpl *)iface;
242 EnterCriticalSection(This->pCritSec);
244 if (This->pConnectedTo)
246 IPin_Release(This->pConnectedTo);
247 This->pConnectedTo = NULL;
253 LeaveCriticalSection(This->pCritSec);
258 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
261 IPinImpl *This = (IPinImpl *)iface;
263 /* TRACE("(%p)\n", ppPin);*/
265 EnterCriticalSection(This->pCritSec);
267 if (This->pConnectedTo)
269 *ppPin = This->pConnectedTo;
274 hr = VFW_E_NOT_CONNECTED;
276 LeaveCriticalSection(This->pCritSec);
281 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
284 IPinImpl *This = (IPinImpl *)iface;
286 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
288 EnterCriticalSection(This->pCritSec);
290 if (This->pConnectedTo)
292 CopyMediaType(pmt, &This->mtCurrent);
297 ZeroMemory(pmt, sizeof(*pmt));
298 hr = VFW_E_NOT_CONNECTED;
301 LeaveCriticalSection(This->pCritSec);
306 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
308 IPinImpl *This = (IPinImpl *)iface;
310 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
312 Copy_PinInfo(pInfo, &This->pinInfo);
317 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
319 IPinImpl *This = (IPinImpl *)iface;
321 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
323 *pPinDir = This->pinInfo.dir;
328 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
330 IPinImpl *This = (IPinImpl *)iface;
332 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
334 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
336 return E_OUTOFMEMORY;
338 strcpyW(*Id, This->pinInfo.achName);
343 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
345 IPinImpl *This = (IPinImpl *)iface;
347 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
349 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
352 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
354 IPinImpl *This = (IPinImpl *)iface;
355 ENUMMEDIADETAILS emd;
357 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
359 /* override this method to allow enumeration of your types */
361 emd.pMediaTypes = NULL;
363 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
366 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
368 IPinImpl *This = (IPinImpl *)iface;
370 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
372 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
375 /*** IPin implementation for an input pin ***/
377 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
379 InputPin *This = (InputPin *)iface;
381 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
385 if (IsEqualIID(riid, &IID_IUnknown))
386 *ppv = (LPVOID)iface;
387 else if (IsEqualIID(riid, &IID_IPin))
388 *ppv = (LPVOID)iface;
389 else if (IsEqualIID(riid, &IID_IMemInputPin))
390 *ppv = (LPVOID)&This->lpVtblMemInput;
394 IUnknown_AddRef((IUnknown *)(*ppv));
398 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
400 return E_NOINTERFACE;
403 ULONG WINAPI InputPin_Release(IPin * iface)
405 InputPin *This = (InputPin *)iface;
407 TRACE("(%p)->() Release from %ld\n", iface, This->pin.refCount);
409 if (!InterlockedDecrement(&This->pin.refCount))
411 DeleteMediaType(&This->pin.mtCurrent);
412 if (This->pAllocator)
413 IMemAllocator_Release(This->pAllocator);
418 return This->pin.refCount;
421 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
423 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
429 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
431 InputPin *This = (InputPin *)iface;
432 PIN_DIRECTION pindirReceive;
435 TRACE("(%p, %p)\n", pReceivePin, pmt);
436 dump_AM_MEDIA_TYPE(pmt);
438 EnterCriticalSection(This->pin.pCritSec);
440 if (This->pin.pConnectedTo)
441 hr = VFW_E_ALREADY_CONNECTED;
443 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
444 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
445 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
449 IPin_QueryDirection(pReceivePin, &pindirReceive);
451 if (pindirReceive != PINDIR_OUTPUT)
453 ERR("Can't connect from non-output pin\n");
454 hr = VFW_E_INVALID_DIRECTION;
460 CopyMediaType(&This->pin.mtCurrent, pmt);
461 This->pin.pConnectedTo = pReceivePin;
462 IPin_AddRef(pReceivePin);
465 LeaveCriticalSection(This->pin.pCritSec);
470 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
477 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
483 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
489 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
491 InputPin *This = (InputPin *)iface;
493 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
495 This->tStart = tStart;
502 static const IPinVtbl InputPin_Vtbl =
504 InputPin_QueryInterface,
508 InputPin_ReceiveConnection,
510 IPinImpl_ConnectedTo,
511 IPinImpl_ConnectionMediaType,
512 IPinImpl_QueryPinInfo,
513 IPinImpl_QueryDirection,
515 IPinImpl_QueryAccept,
516 IPinImpl_EnumMediaTypes,
517 IPinImpl_QueryInternalConnections,
518 InputPin_EndOfStream,
524 /*** IMemInputPin implementation ***/
526 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
528 ICOM_THIS_From_IMemInputPin(InputPin, iface);
530 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
533 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
535 ICOM_THIS_From_IMemInputPin(InputPin, iface);
537 return IPin_AddRef((IPin *)&This->pin);
540 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
542 ICOM_THIS_From_IMemInputPin(InputPin, iface);
544 return IPin_Release((IPin *)&This->pin);
547 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
549 ICOM_THIS_From_IMemInputPin(InputPin, iface);
551 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
553 *ppAllocator = This->pAllocator;
555 IMemAllocator_AddRef(*ppAllocator);
557 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
560 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
562 ICOM_THIS_From_IMemInputPin(InputPin, iface);
564 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
566 if (This->pAllocator)
567 IMemAllocator_Release(This->pAllocator);
568 This->pAllocator = pAllocator;
569 if (This->pAllocator)
570 IMemAllocator_AddRef(This->pAllocator);
575 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
577 ICOM_THIS_From_IMemInputPin(InputPin, iface);
579 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
581 /* override this method if you have any specific requirements */
586 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
588 ICOM_THIS_From_IMemInputPin(InputPin, iface);
590 /* this trace commented out for performance reasons */
591 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
593 return This->fnSampleProc(This->pin.pUserData, pSample);
596 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
599 ICOM_THIS_From_IMemInputPin(InputPin, iface);
601 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
603 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
605 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
613 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
615 ICOM_THIS_From_IMemInputPin(InputPin, iface);
617 FIXME("(%p/%p)->()\n", This, iface);
619 /* FIXME: we should check whether any output pins will block */
624 static const IMemInputPinVtbl MemInputPin_Vtbl =
626 MemInputPin_QueryInterface,
629 MemInputPin_GetAllocator,
630 MemInputPin_NotifyAllocator,
631 MemInputPin_GetAllocatorRequirements,
633 MemInputPin_ReceiveMultiple,
634 MemInputPin_ReceiveCanBlock
637 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
639 OutputPin *This = (OutputPin *)iface;
641 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
645 if (IsEqualIID(riid, &IID_IUnknown))
646 *ppv = (LPVOID)iface;
647 else if (IsEqualIID(riid, &IID_IPin))
648 *ppv = (LPVOID)iface;
652 IUnknown_AddRef((IUnknown *)(*ppv));
656 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
658 return E_NOINTERFACE;
661 ULONG WINAPI OutputPin_Release(IPin * iface)
663 OutputPin *This = (OutputPin *)iface;
665 TRACE("(%p/%p)->()\n", This, iface);
667 if (!InterlockedDecrement(&This->pin.refCount))
669 DeleteMediaType(&This->pin.mtCurrent);
673 return This->pin.refCount;
676 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
679 OutputPin *This = (OutputPin *)iface;
681 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
682 dump_AM_MEDIA_TYPE(pmt);
684 /* If we try to connect to ourself, we will definitely deadlock.
685 * There are other cases where we could deadlock too, but this
686 * catches the obvious case */
687 assert(pReceivePin != iface);
689 EnterCriticalSection(This->pin.pCritSec);
691 /* if we have been a specific type to connect with, then we can either connect
692 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
693 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
694 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
697 /* negotiate media type */
699 IEnumMediaTypes * pEnumCandidates;
700 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
702 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
704 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
706 /* try this filter's media types first */
707 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
709 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
710 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
713 CoTaskMemFree(pmtCandidate);
716 CoTaskMemFree(pmtCandidate);
718 IEnumMediaTypes_Release(pEnumCandidates);
721 /* then try receiver filter's media types */
722 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
724 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
726 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
728 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
729 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
732 CoTaskMemFree(pmtCandidate);
735 CoTaskMemFree(pmtCandidate);
737 IEnumMediaTypes_Release(pEnumCandidates);
739 } /* if negotiate media type */
741 LeaveCriticalSection(This->pin.pCritSec);
743 TRACE(" -- %lx\n", hr);
747 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
749 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
754 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
757 OutputPin *This = (OutputPin *)iface;
761 EnterCriticalSection(This->pin.pCritSec);
763 if (This->pMemInputPin)
765 IMemInputPin_Release(This->pMemInputPin);
766 This->pMemInputPin = NULL;
768 if (This->pin.pConnectedTo)
770 IPin_Release(This->pin.pConnectedTo);
771 This->pin.pConnectedTo = NULL;
777 LeaveCriticalSection(This->pin.pCritSec);
782 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
786 /* not supposed to do anything in an output pin */
791 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
793 TRACE("(%p)->()\n", iface);
795 /* not supposed to do anything in an output pin */
800 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
802 TRACE("(%p)->()\n", iface);
804 /* not supposed to do anything in an output pin */
809 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
811 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
813 /* not supposed to do anything in an output pin */
818 static const IPinVtbl OutputPin_Vtbl =
820 OutputPin_QueryInterface,
824 OutputPin_ReceiveConnection,
825 OutputPin_Disconnect,
826 IPinImpl_ConnectedTo,
827 IPinImpl_ConnectionMediaType,
828 IPinImpl_QueryPinInfo,
829 IPinImpl_QueryDirection,
831 IPinImpl_QueryAccept,
832 IPinImpl_EnumMediaTypes,
833 IPinImpl_QueryInternalConnections,
834 OutputPin_EndOfStream,
835 OutputPin_BeginFlush,
840 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
844 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
846 EnterCriticalSection(This->pin.pCritSec);
848 if (!This->pin.pConnectedTo)
849 hr = VFW_E_NOT_CONNECTED;
852 IMemAllocator * pAlloc = NULL;
854 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
857 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
860 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
863 IMemAllocator_Release(pAlloc);
866 LeaveCriticalSection(This->pin.pCritSec);
871 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
874 IMemInputPin * pMemConnected = NULL;
876 EnterCriticalSection(This->pin.pCritSec);
878 if (!This->pin.pConnectedTo || !This->pMemInputPin)
879 hr = VFW_E_NOT_CONNECTED;
882 /* we don't have the lock held when using This->pMemInputPin,
883 * so we need to AddRef it to stop it being deleted while we are
885 pMemConnected = This->pMemInputPin;
886 IMemInputPin_AddRef(pMemConnected);
889 LeaveCriticalSection(This->pin.pCritSec);
893 /* NOTE: if we are in a critical section when Receive is called
894 * then it causes some problems (most notably with the native Video
895 * Renderer) if we are re-entered for whatever reason */
896 hr = IMemInputPin_Receive(pMemConnected, pSample);
897 IMemInputPin_Release(pMemConnected);
903 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
907 EnterCriticalSection(This->pin.pCritSec);
909 if (!This->pin.pConnectedTo)
910 hr = VFW_E_NOT_CONNECTED;
912 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
914 LeaveCriticalSection(This->pin.pCritSec);
919 HRESULT OutputPin_CommitAllocator(OutputPin * This)
923 TRACE("(%p)->()\n", This);
925 EnterCriticalSection(This->pin.pCritSec);
927 if (!This->pin.pConnectedTo || !This->pMemInputPin)
928 hr = VFW_E_NOT_CONNECTED;
931 IMemAllocator * pAlloc = NULL;
933 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
936 hr = IMemAllocator_Commit(pAlloc);
939 IMemAllocator_Release(pAlloc);
942 LeaveCriticalSection(This->pin.pCritSec);
947 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
951 TRACE("(%p)->()\n", This);
953 EnterCriticalSection(This->pin.pCritSec);
955 if (!This->pin.pConnectedTo || !This->pMemInputPin)
956 hr = VFW_E_NOT_CONNECTED;
959 IMemAllocator * pAlloc = NULL;
961 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
964 hr = IMemAllocator_Decommit(pAlloc);
967 IMemAllocator_Release(pAlloc);
970 hr = IPin_Disconnect(This->pin.pConnectedTo);
973 LeaveCriticalSection(This->pin.pCritSec);
979 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
985 if (pPinInfo->dir != PINDIR_INPUT)
987 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
991 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
994 return E_OUTOFMEMORY;
996 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
998 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1000 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1006 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1008 /* Common attributes */
1009 pPinImpl->pin.refCount = 1;
1010 pPinImpl->pin.pConnectedTo = NULL;
1011 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1012 pPinImpl->pin.pUserData = pUserData;
1013 pPinImpl->pin.pCritSec = pCritSec;
1014 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1016 /* Input pin attributes */
1017 pPinImpl->fnSampleProc = pSampleProc;
1018 pPinImpl->fnPreConnect = NULL;
1019 pPinImpl->pAlloc = NULL;
1020 pPinImpl->pReader = NULL;
1021 pPinImpl->hThread = NULL;
1022 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1024 pPinImpl->rtStart = 0;
1025 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1030 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1032 PIN_DIRECTION pindirReceive;
1034 PullPin *This = (PullPin *)iface;
1036 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1037 dump_AM_MEDIA_TYPE(pmt);
1039 EnterCriticalSection(This->pin.pCritSec);
1041 if (This->pin.pConnectedTo)
1042 hr = VFW_E_ALREADY_CONNECTED;
1044 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1045 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1046 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1050 IPin_QueryDirection(pReceivePin, &pindirReceive);
1052 if (pindirReceive != PINDIR_OUTPUT)
1054 ERR("Can't connect from non-output pin\n");
1055 hr = VFW_E_INVALID_DIRECTION;
1061 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1066 ALLOCATOR_PROPERTIES props;
1068 props.cbBuffer = 64 * 1024; /* 64k bytes */
1071 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1074 if (SUCCEEDED(hr) && This->fnPreConnect)
1076 hr = This->fnPreConnect(iface, pReceivePin);
1081 CopyMediaType(&This->pin.mtCurrent, pmt);
1082 This->pin.pConnectedTo = pReceivePin;
1083 IPin_AddRef(pReceivePin);
1086 LeaveCriticalSection(This->pin.pCritSec);
1090 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1092 PullPin *This = (PullPin *)iface;
1094 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1098 if (IsEqualIID(riid, &IID_IUnknown))
1099 *ppv = (LPVOID)iface;
1100 else if (IsEqualIID(riid, &IID_IPin))
1101 *ppv = (LPVOID)iface;
1105 IUnknown_AddRef((IUnknown *)(*ppv));
1109 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1111 return E_NOINTERFACE;
1114 ULONG WINAPI PullPin_Release(IPin * iface)
1116 PullPin *This = (PullPin *)iface;
1118 TRACE("(%p/%p)->()\n", This, iface);
1120 if (!InterlockedDecrement(&This->pin.refCount))
1123 PullPin_StopProcessing(This);
1124 IMemAllocator_Release(This->pAlloc);
1125 IAsyncReader_Release(This->pReader);
1126 CloseHandle(This->hEventStateChanged);
1127 CoTaskMemFree(This);
1130 return This->pin.refCount;
1133 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1136 SleepEx(INFINITE, TRUE);
1139 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1141 PullPin *This = (PullPin *)iface;
1144 REFERENCE_TIME rtCurrent;
1145 ALLOCATOR_PROPERTIES allocProps;
1147 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1149 SetEvent(This->hEventStateChanged);
1151 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1153 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1157 while (rtCurrent < This->rtStop)
1159 /* FIXME: to improve performance by quite a bit this should be changed
1160 * so that one sample is processed while one sample is fetched. However,
1161 * it is harder to debug so for the moment it will stay as it is */
1162 IMediaSample * pSample = NULL;
1163 REFERENCE_TIME rtSampleStop;
1166 TRACE("Process sample\n");
1168 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1172 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1173 if (rtSampleStop > This->rtStop)
1174 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1175 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1176 rtCurrent = rtSampleStop;
1180 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1183 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1186 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1188 ERR("Processing error: %lx\n", hr);
1191 IMediaSample_Release(pSample);
1199 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1201 PullPin *This = (PullPin *)iface;
1203 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1205 EnterCriticalSection(This->pin.pCritSec);
1209 CloseHandle(This->hThread);
1210 This->hThread = NULL;
1211 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1212 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1214 LeaveCriticalSection(This->pin.pCritSec);
1216 SetEvent(This->hEventStateChanged);
1221 HRESULT PullPin_InitProcessing(PullPin * This)
1225 TRACE("(%p)->()\n", This);
1227 assert(!This->hThread);
1229 /* if we are connected */
1232 EnterCriticalSection(This->pin.pCritSec);
1235 assert(!This->hThread);
1237 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1239 hr = HRESULT_FROM_WIN32(GetLastError());
1242 hr = IMemAllocator_Commit(This->pAlloc);
1244 LeaveCriticalSection(This->pin.pCritSec);
1247 TRACE(" -- %lx\n", hr);
1252 HRESULT PullPin_StartProcessing(PullPin * This)
1254 /* if we are connected */
1255 TRACE("(%p)->()\n", This);
1258 assert(This->hThread);
1260 ResetEvent(This->hEventStateChanged);
1262 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1263 return HRESULT_FROM_WIN32(GetLastError());
1269 HRESULT PullPin_PauseProcessing(PullPin * This)
1271 /* make the processing function exit its loop */
1277 HRESULT PullPin_StopProcessing(PullPin * This)
1279 /* if we are connected */
1282 assert(This->hThread);
1284 ResetEvent(This->hEventStateChanged);
1286 PullPin_PauseProcessing(This);
1288 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1289 return HRESULT_FROM_WIN32(GetLastError());
1295 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1297 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1302 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1304 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1306 PullPin_BeginFlush((IPin *)This);
1307 /* FIXME: need critical section? */
1308 This->rtStart = rtStart;
1309 This->rtStop = rtStop;
1310 PullPin_EndFlush((IPin *)This);
1315 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1317 FIXME("(%p)->()\n", iface);
1321 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1323 FIXME("(%p)->()\n", iface);
1327 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1329 FIXME("(%p)->()\n", iface);
1333 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1335 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1339 static const IPinVtbl PullPin_Vtbl =
1341 PullPin_QueryInterface,
1345 PullPin_ReceiveConnection,
1346 IPinImpl_Disconnect,
1347 IPinImpl_ConnectedTo,
1348 IPinImpl_ConnectionMediaType,
1349 IPinImpl_QueryPinInfo,
1350 IPinImpl_QueryDirection,
1352 IPinImpl_QueryAccept,
1353 IPinImpl_EnumMediaTypes,
1354 IPinImpl_QueryInternalConnections,
1355 PullPin_EndOfStream,