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 IPinVtbl InputPin_Vtbl;
33 static const IPinVtbl OutputPin_Vtbl;
34 static const IMemInputPinVtbl MemInputPin_Vtbl;
35 static const 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;
54 /* Function called as a helper to IPin_Connect */
55 /* specific AM_MEDIA_TYPE - it cannot be NULL */
56 /* NOTE: not part of standard interface */
57 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
59 OutputPin *This = (OutputPin *)iface;
61 IMemAllocator * pMemAlloc = NULL;
62 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
64 TRACE("(%p, %p)\n", pReceivePin, pmt);
65 dump_AM_MEDIA_TYPE(pmt);
67 /* FIXME: call queryacceptproc */
69 This->pin.pConnectedTo = pReceivePin;
70 IPin_AddRef(pReceivePin);
71 CopyMediaType(&This->pin.mtCurrent, pmt);
73 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
75 /* get the IMemInputPin interface we will use to deliver samples to the
79 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
82 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
84 if (hr == VFW_E_NO_ALLOCATOR)
86 /* Input pin provides no allocator, use standard memory allocator */
87 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
91 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
96 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
99 IMemAllocator_Release(pMemAlloc);
101 /* break connection if we couldn't get the allocator */
103 IPin_Disconnect(pReceivePin);
108 IPin_Release(This->pin.pConnectedTo);
109 This->pin.pConnectedTo = NULL;
110 FreeMediaType(&This->pin.mtCurrent);
113 TRACE(" -- %lx\n", hr);
117 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
123 if (pPinInfo->dir != PINDIR_INPUT)
125 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
129 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
132 return E_OUTOFMEMORY;
134 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
136 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
137 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
139 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
145 /* Note that we don't init the vtables here (like C++ constructor) */
146 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
150 /* Common attributes */
151 pPinImpl->pin.refCount = 1;
152 pPinImpl->pin.pConnectedTo = NULL;
153 pPinImpl->pin.fnQueryAccept = pQueryAccept;
154 pPinImpl->pin.pUserData = pUserData;
155 pPinImpl->pin.pCritSec = pCritSec;
156 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
157 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
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);
181 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
183 /* Output pin attributes */
184 pPinImpl->pMemInputPin = NULL;
185 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
188 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
189 if (pPinImpl->allocProps.cbAlign == 0)
190 pPinImpl->allocProps.cbAlign = 1;
193 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
198 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
200 OutputPin * pPinImpl;
204 if (pPinInfo->dir != PINDIR_OUTPUT)
206 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
210 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
213 return E_OUTOFMEMORY;
215 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
217 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
219 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
225 /*** Common pin functions ***/
227 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
229 IPinImpl *This = (IPinImpl *)iface;
230 ULONG refCount = InterlockedIncrement(&This->refCount);
232 TRACE("(%p)->() AddRef from %ld\n", iface, refCount - 1);
237 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
240 IPinImpl *This = (IPinImpl *)iface;
244 EnterCriticalSection(This->pCritSec);
246 if (This->pConnectedTo)
248 IPin_Release(This->pConnectedTo);
249 This->pConnectedTo = NULL;
255 LeaveCriticalSection(This->pCritSec);
260 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
263 IPinImpl *This = (IPinImpl *)iface;
265 /* TRACE("(%p)\n", ppPin);*/
267 EnterCriticalSection(This->pCritSec);
269 if (This->pConnectedTo)
271 *ppPin = This->pConnectedTo;
276 hr = VFW_E_NOT_CONNECTED;
278 LeaveCriticalSection(This->pCritSec);
283 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
286 IPinImpl *This = (IPinImpl *)iface;
288 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
290 EnterCriticalSection(This->pCritSec);
292 if (This->pConnectedTo)
294 CopyMediaType(pmt, &This->mtCurrent);
299 ZeroMemory(pmt, sizeof(*pmt));
300 hr = VFW_E_NOT_CONNECTED;
303 LeaveCriticalSection(This->pCritSec);
308 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
310 IPinImpl *This = (IPinImpl *)iface;
312 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
314 Copy_PinInfo(pInfo, &This->pinInfo);
315 IBaseFilter_AddRef(pInfo->pFilter);
320 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
322 IPinImpl *This = (IPinImpl *)iface;
324 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
326 *pPinDir = This->pinInfo.dir;
331 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
333 IPinImpl *This = (IPinImpl *)iface;
335 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
337 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
339 return E_OUTOFMEMORY;
341 strcpyW(*Id, This->pinInfo.achName);
346 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
348 IPinImpl *This = (IPinImpl *)iface;
350 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
352 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
355 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
357 IPinImpl *This = (IPinImpl *)iface;
358 ENUMMEDIADETAILS emd;
360 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
362 /* override this method to allow enumeration of your types */
364 emd.pMediaTypes = NULL;
366 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
369 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
371 IPinImpl *This = (IPinImpl *)iface;
373 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
375 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
378 /*** IPin implementation for an input pin ***/
380 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
382 InputPin *This = (InputPin *)iface;
384 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
388 if (IsEqualIID(riid, &IID_IUnknown))
389 *ppv = (LPVOID)iface;
390 else if (IsEqualIID(riid, &IID_IPin))
391 *ppv = (LPVOID)iface;
392 else if (IsEqualIID(riid, &IID_IMemInputPin))
393 *ppv = (LPVOID)&This->lpVtblMemInput;
397 IUnknown_AddRef((IUnknown *)(*ppv));
401 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
403 return E_NOINTERFACE;
406 ULONG WINAPI InputPin_Release(IPin * iface)
408 InputPin *This = (InputPin *)iface;
409 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
411 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
415 FreeMediaType(&This->pin.mtCurrent);
416 if (This->pAllocator)
417 IMemAllocator_Release(This->pAllocator);
425 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
427 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
433 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
435 InputPin *This = (InputPin *)iface;
436 PIN_DIRECTION pindirReceive;
439 TRACE("(%p, %p)\n", pReceivePin, pmt);
440 dump_AM_MEDIA_TYPE(pmt);
442 EnterCriticalSection(This->pin.pCritSec);
444 if (This->pin.pConnectedTo)
445 hr = VFW_E_ALREADY_CONNECTED;
447 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
448 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
449 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
453 IPin_QueryDirection(pReceivePin, &pindirReceive);
455 if (pindirReceive != PINDIR_OUTPUT)
457 ERR("Can't connect from non-output pin\n");
458 hr = VFW_E_INVALID_DIRECTION;
464 CopyMediaType(&This->pin.mtCurrent, pmt);
465 This->pin.pConnectedTo = pReceivePin;
466 IPin_AddRef(pReceivePin);
469 LeaveCriticalSection(This->pin.pCritSec);
474 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
481 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
487 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
493 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
495 InputPin *This = (InputPin *)iface;
497 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
499 This->tStart = tStart;
506 static const IPinVtbl InputPin_Vtbl =
508 InputPin_QueryInterface,
512 InputPin_ReceiveConnection,
514 IPinImpl_ConnectedTo,
515 IPinImpl_ConnectionMediaType,
516 IPinImpl_QueryPinInfo,
517 IPinImpl_QueryDirection,
519 IPinImpl_QueryAccept,
520 IPinImpl_EnumMediaTypes,
521 IPinImpl_QueryInternalConnections,
522 InputPin_EndOfStream,
528 /*** IMemInputPin implementation ***/
530 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
532 ICOM_THIS_From_IMemInputPin(InputPin, iface);
534 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
537 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
539 ICOM_THIS_From_IMemInputPin(InputPin, iface);
541 return IPin_AddRef((IPin *)&This->pin);
544 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
546 ICOM_THIS_From_IMemInputPin(InputPin, iface);
548 return IPin_Release((IPin *)&This->pin);
551 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
553 ICOM_THIS_From_IMemInputPin(InputPin, iface);
555 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
557 *ppAllocator = This->pAllocator;
559 IMemAllocator_AddRef(*ppAllocator);
561 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
564 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
566 ICOM_THIS_From_IMemInputPin(InputPin, iface);
568 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
570 if (This->pAllocator)
571 IMemAllocator_Release(This->pAllocator);
572 This->pAllocator = pAllocator;
573 if (This->pAllocator)
574 IMemAllocator_AddRef(This->pAllocator);
579 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
581 ICOM_THIS_From_IMemInputPin(InputPin, iface);
583 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
585 /* override this method if you have any specific requirements */
590 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
592 ICOM_THIS_From_IMemInputPin(InputPin, iface);
594 /* this trace commented out for performance reasons */
595 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
597 return This->fnSampleProc(This->pin.pUserData, pSample);
600 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
603 ICOM_THIS_From_IMemInputPin(InputPin, iface);
605 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
607 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
609 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
617 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
619 ICOM_THIS_From_IMemInputPin(InputPin, iface);
621 FIXME("(%p/%p)->()\n", This, iface);
623 /* FIXME: we should check whether any output pins will block */
628 static const IMemInputPinVtbl MemInputPin_Vtbl =
630 MemInputPin_QueryInterface,
633 MemInputPin_GetAllocator,
634 MemInputPin_NotifyAllocator,
635 MemInputPin_GetAllocatorRequirements,
637 MemInputPin_ReceiveMultiple,
638 MemInputPin_ReceiveCanBlock
641 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
643 OutputPin *This = (OutputPin *)iface;
645 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
649 if (IsEqualIID(riid, &IID_IUnknown))
650 *ppv = (LPVOID)iface;
651 else if (IsEqualIID(riid, &IID_IPin))
652 *ppv = (LPVOID)iface;
656 IUnknown_AddRef((IUnknown *)(*ppv));
660 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
662 return E_NOINTERFACE;
665 ULONG WINAPI OutputPin_Release(IPin * iface)
667 OutputPin *This = (OutputPin *)iface;
668 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
670 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
674 FreeMediaType(&This->pin.mtCurrent);
681 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
684 OutputPin *This = (OutputPin *)iface;
686 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
687 dump_AM_MEDIA_TYPE(pmt);
689 /* If we try to connect to ourself, we will definitely deadlock.
690 * There are other cases where we could deadlock too, but this
691 * catches the obvious case */
692 assert(pReceivePin != iface);
694 EnterCriticalSection(This->pin.pCritSec);
696 /* if we have been a specific type to connect with, then we can either connect
697 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
698 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
699 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
702 /* negotiate media type */
704 IEnumMediaTypes * pEnumCandidates;
705 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
707 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
709 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
711 /* try this filter's media types first */
712 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
714 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
715 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
718 CoTaskMemFree(pmtCandidate);
721 CoTaskMemFree(pmtCandidate);
723 IEnumMediaTypes_Release(pEnumCandidates);
726 /* then try receiver filter's media types */
727 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
729 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
731 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
733 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
734 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
737 CoTaskMemFree(pmtCandidate);
740 CoTaskMemFree(pmtCandidate);
742 IEnumMediaTypes_Release(pEnumCandidates);
744 } /* if negotiate media type */
746 LeaveCriticalSection(This->pin.pCritSec);
748 TRACE(" -- %lx\n", hr);
752 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
754 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
759 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
762 OutputPin *This = (OutputPin *)iface;
766 EnterCriticalSection(This->pin.pCritSec);
768 if (This->pMemInputPin)
770 IMemInputPin_Release(This->pMemInputPin);
771 This->pMemInputPin = NULL;
773 if (This->pin.pConnectedTo)
775 IPin_Release(This->pin.pConnectedTo);
776 This->pin.pConnectedTo = NULL;
782 LeaveCriticalSection(This->pin.pCritSec);
787 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
791 /* not supposed to do anything in an output pin */
796 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
798 TRACE("(%p)->()\n", iface);
800 /* not supposed to do anything in an output pin */
805 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
807 TRACE("(%p)->()\n", iface);
809 /* not supposed to do anything in an output pin */
814 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
816 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
818 /* not supposed to do anything in an output pin */
823 static const IPinVtbl OutputPin_Vtbl =
825 OutputPin_QueryInterface,
829 OutputPin_ReceiveConnection,
830 OutputPin_Disconnect,
831 IPinImpl_ConnectedTo,
832 IPinImpl_ConnectionMediaType,
833 IPinImpl_QueryPinInfo,
834 IPinImpl_QueryDirection,
836 IPinImpl_QueryAccept,
837 IPinImpl_EnumMediaTypes,
838 IPinImpl_QueryInternalConnections,
839 OutputPin_EndOfStream,
840 OutputPin_BeginFlush,
845 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
849 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
851 EnterCriticalSection(This->pin.pCritSec);
853 if (!This->pin.pConnectedTo)
854 hr = VFW_E_NOT_CONNECTED;
857 IMemAllocator * pAlloc = NULL;
859 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
862 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
865 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
868 IMemAllocator_Release(pAlloc);
871 LeaveCriticalSection(This->pin.pCritSec);
876 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
879 IMemInputPin * pMemConnected = NULL;
881 EnterCriticalSection(This->pin.pCritSec);
883 if (!This->pin.pConnectedTo || !This->pMemInputPin)
884 hr = VFW_E_NOT_CONNECTED;
887 /* we don't have the lock held when using This->pMemInputPin,
888 * so we need to AddRef it to stop it being deleted while we are
890 pMemConnected = This->pMemInputPin;
891 IMemInputPin_AddRef(pMemConnected);
894 LeaveCriticalSection(This->pin.pCritSec);
898 /* NOTE: if we are in a critical section when Receive is called
899 * then it causes some problems (most notably with the native Video
900 * Renderer) if we are re-entered for whatever reason */
901 hr = IMemInputPin_Receive(pMemConnected, pSample);
902 IMemInputPin_Release(pMemConnected);
908 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
912 EnterCriticalSection(This->pin.pCritSec);
914 if (!This->pin.pConnectedTo)
915 hr = VFW_E_NOT_CONNECTED;
917 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
919 LeaveCriticalSection(This->pin.pCritSec);
924 HRESULT OutputPin_CommitAllocator(OutputPin * This)
928 TRACE("(%p)->()\n", This);
930 EnterCriticalSection(This->pin.pCritSec);
932 if (!This->pin.pConnectedTo || !This->pMemInputPin)
933 hr = VFW_E_NOT_CONNECTED;
936 IMemAllocator * pAlloc = NULL;
938 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
941 hr = IMemAllocator_Commit(pAlloc);
944 IMemAllocator_Release(pAlloc);
947 LeaveCriticalSection(This->pin.pCritSec);
952 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
956 TRACE("(%p)->()\n", This);
958 EnterCriticalSection(This->pin.pCritSec);
960 if (!This->pin.pConnectedTo || !This->pMemInputPin)
961 hr = VFW_E_NOT_CONNECTED;
964 IMemAllocator * pAlloc = NULL;
966 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
969 hr = IMemAllocator_Decommit(pAlloc);
972 IMemAllocator_Release(pAlloc);
975 hr = IPin_Disconnect(This->pin.pConnectedTo);
978 LeaveCriticalSection(This->pin.pCritSec);
984 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
990 if (pPinInfo->dir != PINDIR_INPUT)
992 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
996 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
999 return E_OUTOFMEMORY;
1001 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1003 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1005 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1011 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1013 /* Common attributes */
1014 pPinImpl->pin.refCount = 1;
1015 pPinImpl->pin.pConnectedTo = NULL;
1016 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1017 pPinImpl->pin.pUserData = pUserData;
1018 pPinImpl->pin.pCritSec = pCritSec;
1019 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1020 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1022 /* Input pin attributes */
1023 pPinImpl->fnSampleProc = pSampleProc;
1024 pPinImpl->fnPreConnect = NULL;
1025 pPinImpl->pAlloc = NULL;
1026 pPinImpl->pReader = NULL;
1027 pPinImpl->hThread = NULL;
1028 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1030 pPinImpl->rtStart = 0;
1031 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1036 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1038 PIN_DIRECTION pindirReceive;
1040 PullPin *This = (PullPin *)iface;
1042 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1043 dump_AM_MEDIA_TYPE(pmt);
1045 EnterCriticalSection(This->pin.pCritSec);
1047 if (This->pin.pConnectedTo)
1048 hr = VFW_E_ALREADY_CONNECTED;
1050 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1051 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1052 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1056 IPin_QueryDirection(pReceivePin, &pindirReceive);
1058 if (pindirReceive != PINDIR_OUTPUT)
1060 ERR("Can't connect from non-output pin\n");
1061 hr = VFW_E_INVALID_DIRECTION;
1067 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1072 ALLOCATOR_PROPERTIES props;
1074 props.cbBuffer = 64 * 1024; /* 64k bytes */
1077 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1080 if (SUCCEEDED(hr) && This->fnPreConnect)
1082 hr = This->fnPreConnect(iface, pReceivePin);
1087 CopyMediaType(&This->pin.mtCurrent, pmt);
1088 This->pin.pConnectedTo = pReceivePin;
1089 IPin_AddRef(pReceivePin);
1092 LeaveCriticalSection(This->pin.pCritSec);
1096 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1098 PullPin *This = (PullPin *)iface;
1100 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1104 if (IsEqualIID(riid, &IID_IUnknown))
1105 *ppv = (LPVOID)iface;
1106 else if (IsEqualIID(riid, &IID_IPin))
1107 *ppv = (LPVOID)iface;
1111 IUnknown_AddRef((IUnknown *)(*ppv));
1115 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1117 return E_NOINTERFACE;
1120 ULONG WINAPI PullPin_Release(IPin * iface)
1122 PullPin *This = (PullPin *)iface;
1123 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1125 TRACE("(%p/%p)->()\n", This, iface);
1130 PullPin_StopProcessing(This);
1131 IMemAllocator_Release(This->pAlloc);
1132 IAsyncReader_Release(This->pReader);
1133 CloseHandle(This->hEventStateChanged);
1134 CoTaskMemFree(This);
1140 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1143 SleepEx(INFINITE, TRUE);
1146 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1148 PullPin *This = (PullPin *)iface;
1151 REFERENCE_TIME rtCurrent;
1152 ALLOCATOR_PROPERTIES allocProps;
1154 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1156 SetEvent(This->hEventStateChanged);
1158 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1160 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1164 while (rtCurrent < This->rtStop && hr == S_OK)
1166 /* FIXME: to improve performance by quite a bit this should be changed
1167 * so that one sample is processed while one sample is fetched. However,
1168 * it is harder to debug so for the moment it will stay as it is */
1169 IMediaSample * pSample = NULL;
1170 REFERENCE_TIME rtSampleStop;
1173 TRACE("Process sample\n");
1175 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1179 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1180 if (rtSampleStop > This->rtStop)
1181 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1182 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1183 rtCurrent = rtSampleStop;
1187 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1190 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1193 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1195 ERR("Processing error: %lx\n", hr);
1198 IMediaSample_Release(pSample);
1206 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1208 PullPin *This = (PullPin *)iface;
1210 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1212 EnterCriticalSection(This->pin.pCritSec);
1216 CloseHandle(This->hThread);
1217 This->hThread = NULL;
1218 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1219 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1221 LeaveCriticalSection(This->pin.pCritSec);
1223 SetEvent(This->hEventStateChanged);
1228 HRESULT PullPin_InitProcessing(PullPin * This)
1232 TRACE("(%p)->()\n", This);
1234 assert(!This->hThread);
1236 /* if we are connected */
1239 EnterCriticalSection(This->pin.pCritSec);
1242 assert(!This->hThread);
1244 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1246 hr = HRESULT_FROM_WIN32(GetLastError());
1249 hr = IMemAllocator_Commit(This->pAlloc);
1251 LeaveCriticalSection(This->pin.pCritSec);
1254 TRACE(" -- %lx\n", hr);
1259 HRESULT PullPin_StartProcessing(PullPin * This)
1261 /* if we are connected */
1262 TRACE("(%p)->()\n", This);
1265 assert(This->hThread);
1267 ResetEvent(This->hEventStateChanged);
1269 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1270 return HRESULT_FROM_WIN32(GetLastError());
1276 HRESULT PullPin_PauseProcessing(PullPin * This)
1278 /* make the processing function exit its loop */
1284 HRESULT PullPin_StopProcessing(PullPin * This)
1286 /* if we are connected */
1289 assert(This->hThread);
1291 ResetEvent(This->hEventStateChanged);
1293 PullPin_PauseProcessing(This);
1295 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1296 return HRESULT_FROM_WIN32(GetLastError());
1302 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1304 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1309 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1311 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1313 PullPin_BeginFlush((IPin *)This);
1314 /* FIXME: need critical section? */
1315 This->rtStart = rtStart;
1316 This->rtStop = rtStop;
1317 PullPin_EndFlush((IPin *)This);
1322 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1324 FIXME("(%p)->()\n", iface);
1328 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1330 FIXME("(%p)->()\n", iface);
1334 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1336 FIXME("(%p)->()\n", iface);
1340 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1342 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1346 static const IPinVtbl PullPin_Vtbl =
1348 PullPin_QueryInterface,
1352 PullPin_ReceiveConnection,
1353 IPinImpl_Disconnect,
1354 IPinImpl_ConnectedTo,
1355 IPinImpl_ConnectionMediaType,
1356 IPinImpl_QueryPinInfo,
1357 IPinImpl_QueryDirection,
1359 IPinImpl_QueryAccept,
1360 IPinImpl_EnumMediaTypes,
1361 IPinImpl_QueryInternalConnections,
1362 PullPin_EndOfStream,