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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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)*(boundary))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN((value)+(boundary)-1, (boundary)))
40 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
42 /** Helper function, there are a lot of places where the error code is inherited
43 * The following rules apply:
45 * Return the first received error code (E_NOTIMPL is ignored)
46 * If no errors occur: return the first received non-error-code that isn't S_OK
48 HRESULT updatehres( HRESULT original, HRESULT new )
50 if (FAILED( original ) || new == E_NOTIMPL)
53 if (FAILED( new ) || original == S_OK)
59 /** Sends a message from a pin further to other, similar pins
60 * fnMiddle is called on each pin found further on the stream.
61 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
63 * If the pin given is an input pin, the message will be sent downstream to other input pins
64 * If the pin given is an output pin, the message will be sent upstream to other output pins
66 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
71 HRESULT hr_return = S_OK;
72 IEnumPins *enumpins = NULL;
74 PIN_DIRECTION from_dir;
76 IPin_QueryDirection( from, &from_dir );
78 hr = IPin_QueryInternalConnections( from, NULL, &amount );
79 if (hr != E_NOTIMPL && amount)
80 FIXME("Use QueryInternalConnections!\n");
83 pin_info.pFilter = NULL;
84 hr = IPin_QueryPinInfo( from, &pin_info );
88 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
92 hr = IEnumPins_Reset( enumpins );
95 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
96 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
98 hr = IEnumPins_Reset( enumpins );
105 IPin_QueryDirection( pin, &dir );
108 IPin *connected = NULL;
111 IPin_ConnectedTo( pin, &connected );
116 hr_local = fnMiddle( connected, arg );
117 hr_return = updatehres( hr_return, hr_local );
118 IPin_Release(connected);
123 } while (hr == S_OK);
129 hr_local = fnEnd( from, arg );
130 hr_return = updatehres( hr_return, hr_local );
134 if (pin_info.pFilter)
135 IBaseFilter_Release( pin_info.pFilter );
139 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
141 return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
145 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
147 /* Tempting to just do a memcpy, but the name field is
148 128 characters long! We will probably never exceed 10
149 most of the time, so we are better off copying
150 each field manually */
151 strcpyW(pDest->achName, pSrc->achName);
152 pDest->dir = pSrc->dir;
153 pDest->pFilter = pSrc->pFilter;
156 /* Function called as a helper to IPin_Connect */
157 /* specific AM_MEDIA_TYPE - it cannot be NULL */
158 /* NOTE: not part of standard interface */
159 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
161 OutputPin *This = (OutputPin *)iface;
163 IMemAllocator * pMemAlloc = NULL;
164 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
166 TRACE("(%p, %p)\n", pReceivePin, pmt);
167 dump_AM_MEDIA_TYPE(pmt);
169 /* FIXME: call queryacceptproc */
171 This->pin.pConnectedTo = pReceivePin;
172 IPin_AddRef(pReceivePin);
173 CopyMediaType(&This->pin.mtCurrent, pmt);
175 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
177 /* get the IMemInputPin interface we will use to deliver samples to the
181 This->pMemInputPin = NULL;
182 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
186 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
188 if (hr == VFW_E_NO_ALLOCATOR)
190 /* Input pin provides no allocator, use standard memory allocator */
191 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
195 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
200 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
203 IMemAllocator_Release(pMemAlloc);
206 /* break connection if we couldn't get the allocator */
209 if (This->pMemInputPin)
210 IMemInputPin_Release(This->pMemInputPin);
211 This->pMemInputPin = NULL;
213 IPin_Disconnect(pReceivePin);
219 IPin_Release(This->pin.pConnectedTo);
220 This->pin.pConnectedTo = NULL;
221 FreeMediaType(&This->pin.mtCurrent);
224 TRACE(" -- %x\n", hr);
228 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
234 if (pPinInfo->dir != PINDIR_INPUT)
236 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
240 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
243 return E_OUTOFMEMORY;
245 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
247 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
248 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
250 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
254 CoTaskMemFree(pPinImpl);
258 /* Note that we don't init the vtables here (like C++ constructor) */
259 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
263 /* Common attributes */
264 pPinImpl->pin.refCount = 1;
265 pPinImpl->pin.pConnectedTo = NULL;
266 pPinImpl->pin.fnQueryAccept = pQueryAccept;
267 pPinImpl->pin.pUserData = pUserData;
268 pPinImpl->pin.pCritSec = pCritSec;
269 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
270 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
272 /* Input pin attributes */
273 pPinImpl->fnSampleProc = pSampleProc;
274 pPinImpl->pAllocator = NULL;
275 pPinImpl->tStart = 0;
282 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
283 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
287 /* Common attributes */
288 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
289 pPinImpl->pin.refCount = 1;
290 pPinImpl->pin.pConnectedTo = NULL;
291 pPinImpl->pin.fnQueryAccept = pQueryAccept;
292 pPinImpl->pin.pUserData = pUserData;
293 pPinImpl->pin.pCritSec = pCritSec;
294 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
295 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
297 /* Output pin attributes */
298 pPinImpl->pMemInputPin = NULL;
299 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
302 pPinImpl->allocProps = *props;
303 if (pPinImpl->allocProps.cbAlign == 0)
304 pPinImpl->allocProps.cbAlign = 1;
307 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
312 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
314 OutputPin * pPinImpl;
318 if (pPinInfo->dir != PINDIR_OUTPUT)
320 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
324 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
327 return E_OUTOFMEMORY;
329 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
331 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
333 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
337 CoTaskMemFree(pPinImpl);
341 /*** Common pin functions ***/
343 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
345 IPinImpl *This = (IPinImpl *)iface;
346 ULONG refCount = InterlockedIncrement(&This->refCount);
348 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
353 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
356 IPinImpl *This = (IPinImpl *)iface;
360 EnterCriticalSection(This->pCritSec);
362 if (This->pConnectedTo)
364 IPin_Release(This->pConnectedTo);
365 This->pConnectedTo = NULL;
371 LeaveCriticalSection(This->pCritSec);
376 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
379 IPinImpl *This = (IPinImpl *)iface;
381 TRACE("(%p)\n", ppPin);
383 EnterCriticalSection(This->pCritSec);
385 if (This->pConnectedTo)
387 *ppPin = This->pConnectedTo;
392 hr = VFW_E_NOT_CONNECTED;
394 LeaveCriticalSection(This->pCritSec);
399 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
402 IPinImpl *This = (IPinImpl *)iface;
404 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
406 EnterCriticalSection(This->pCritSec);
408 if (This->pConnectedTo)
410 CopyMediaType(pmt, &This->mtCurrent);
415 ZeroMemory(pmt, sizeof(*pmt));
416 hr = VFW_E_NOT_CONNECTED;
419 LeaveCriticalSection(This->pCritSec);
424 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
426 IPinImpl *This = (IPinImpl *)iface;
428 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
430 Copy_PinInfo(pInfo, &This->pinInfo);
431 IBaseFilter_AddRef(pInfo->pFilter);
436 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
438 IPinImpl *This = (IPinImpl *)iface;
440 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
442 *pPinDir = This->pinInfo.dir;
447 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
449 IPinImpl *This = (IPinImpl *)iface;
451 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
453 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
455 return E_OUTOFMEMORY;
457 strcpyW(*Id, This->pinInfo.achName);
462 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
464 IPinImpl *This = (IPinImpl *)iface;
466 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
468 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
471 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
473 IPinImpl *This = (IPinImpl *)iface;
474 ENUMMEDIADETAILS emd;
476 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
478 /* override this method to allow enumeration of your types */
480 emd.pMediaTypes = NULL;
482 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
485 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
487 IPinImpl *This = (IPinImpl *)iface;
489 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
491 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
494 /*** IPin implementation for an input pin ***/
496 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
498 InputPin *This = (InputPin *)iface;
500 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
504 if (IsEqualIID(riid, &IID_IUnknown))
505 *ppv = (LPVOID)iface;
506 else if (IsEqualIID(riid, &IID_IPin))
507 *ppv = (LPVOID)iface;
508 else if (IsEqualIID(riid, &IID_IMemInputPin))
509 *ppv = (LPVOID)&This->lpVtblMemInput;
510 else if (IsEqualIID(riid, &IID_IMediaSeeking))
512 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
517 IUnknown_AddRef((IUnknown *)(*ppv));
521 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
523 return E_NOINTERFACE;
526 ULONG WINAPI InputPin_Release(IPin * iface)
528 InputPin *This = (InputPin *)iface;
529 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
531 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
535 FreeMediaType(&This->pin.mtCurrent);
536 if (This->pAllocator)
537 IMemAllocator_Release(This->pAllocator);
545 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
547 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
553 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
555 InputPin *This = (InputPin *)iface;
556 PIN_DIRECTION pindirReceive;
559 TRACE("(%p, %p)\n", pReceivePin, pmt);
560 dump_AM_MEDIA_TYPE(pmt);
562 EnterCriticalSection(This->pin.pCritSec);
564 if (This->pin.pConnectedTo)
565 hr = VFW_E_ALREADY_CONNECTED;
567 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
568 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
569 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
573 IPin_QueryDirection(pReceivePin, &pindirReceive);
575 if (pindirReceive != PINDIR_OUTPUT)
577 ERR("Can't connect from non-output pin\n");
578 hr = VFW_E_INVALID_DIRECTION;
584 CopyMediaType(&This->pin.mtCurrent, pmt);
585 This->pin.pConnectedTo = pReceivePin;
586 IPin_AddRef(pReceivePin);
589 LeaveCriticalSection(This->pin.pCritSec);
594 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
596 return IPin_EndOfStream( pin );
599 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
603 /* Should do an end of stream notification?
604 * Also, don't accept any more samples from now!
605 * TODO: Don't accept any more packets
608 return SendFurther( iface, deliver_endofstream, NULL, NULL );
611 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
613 return IPin_BeginFlush( pin );
616 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
620 /* TODO: Drop all cached packets, and don't accept any more samples! */
621 return SendFurther( iface, deliver_beginflush, NULL, NULL );
624 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
626 return IPin_EndFlush( pin );
629 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
633 /* TODO: Accept any samples again */
634 return SendFurther( iface, deliver_endflush, NULL, NULL );
637 typedef struct newsegmentargs
639 REFERENCE_TIME tStart, tStop;
643 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
645 newsegmentargs *args = data;
646 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
649 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
651 InputPin *This = (InputPin *)iface;
654 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
656 args.tStart = This->tStart = tStart;
657 args.tStop = This->tStop = tStop;
658 args.rate = This->dRate = dRate;
660 return SendFurther( iface, deliver_newsegment, &args, NULL );
663 static const IPinVtbl InputPin_Vtbl =
665 InputPin_QueryInterface,
669 InputPin_ReceiveConnection,
671 IPinImpl_ConnectedTo,
672 IPinImpl_ConnectionMediaType,
673 IPinImpl_QueryPinInfo,
674 IPinImpl_QueryDirection,
676 IPinImpl_QueryAccept,
677 IPinImpl_EnumMediaTypes,
678 IPinImpl_QueryInternalConnections,
679 InputPin_EndOfStream,
685 /*** IMemInputPin implementation ***/
687 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
689 InputPin *This = impl_from_IMemInputPin(iface);
691 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
694 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
696 InputPin *This = impl_from_IMemInputPin(iface);
698 return IPin_AddRef((IPin *)&This->pin);
701 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
703 InputPin *This = impl_from_IMemInputPin(iface);
705 return IPin_Release((IPin *)&This->pin);
708 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
710 InputPin *This = impl_from_IMemInputPin(iface);
712 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
714 *ppAllocator = This->pAllocator;
716 IMemAllocator_AddRef(*ppAllocator);
718 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
721 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
723 InputPin *This = impl_from_IMemInputPin(iface);
725 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
727 if (This->pAllocator)
728 IMemAllocator_Release(This->pAllocator);
729 This->pAllocator = pAllocator;
730 if (This->pAllocator)
731 IMemAllocator_AddRef(This->pAllocator);
736 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
738 InputPin *This = impl_from_IMemInputPin(iface);
740 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
742 /* override this method if you have any specific requirements */
747 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
749 InputPin *This = impl_from_IMemInputPin(iface);
751 /* this trace commented out for performance reasons */
752 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
754 return This->fnSampleProc(This->pin.pUserData, pSample);
757 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
760 InputPin *This = impl_from_IMemInputPin(iface);
762 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
764 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
766 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
774 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
776 InputPin *This = impl_from_IMemInputPin(iface);
778 FIXME("(%p/%p)->()\n", This, iface);
780 /* FIXME: we should check whether any output pins will block */
785 static const IMemInputPinVtbl MemInputPin_Vtbl =
787 MemInputPin_QueryInterface,
790 MemInputPin_GetAllocator,
791 MemInputPin_NotifyAllocator,
792 MemInputPin_GetAllocatorRequirements,
794 MemInputPin_ReceiveMultiple,
795 MemInputPin_ReceiveCanBlock
798 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
800 OutputPin *This = (OutputPin *)iface;
802 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
806 if (IsEqualIID(riid, &IID_IUnknown))
807 *ppv = (LPVOID)iface;
808 else if (IsEqualIID(riid, &IID_IPin))
809 *ppv = (LPVOID)iface;
810 else if (IsEqualIID(riid, &IID_IMediaSeeking))
812 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
817 IUnknown_AddRef((IUnknown *)(*ppv));
821 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
823 return E_NOINTERFACE;
826 ULONG WINAPI OutputPin_Release(IPin * iface)
828 OutputPin *This = (OutputPin *)iface;
829 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
831 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
835 FreeMediaType(&This->pin.mtCurrent);
842 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
845 OutputPin *This = (OutputPin *)iface;
847 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
848 dump_AM_MEDIA_TYPE(pmt);
850 /* If we try to connect to ourself, we will definitely deadlock.
851 * There are other cases where we could deadlock too, but this
852 * catches the obvious case */
853 assert(pReceivePin != iface);
855 EnterCriticalSection(This->pin.pCritSec);
857 /* if we have been a specific type to connect with, then we can either connect
858 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
859 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
860 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
863 /* negotiate media type */
865 IEnumMediaTypes * pEnumCandidates;
866 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
868 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
870 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
872 /* try this filter's media types first */
873 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
875 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
876 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
879 CoTaskMemFree(pmtCandidate);
882 CoTaskMemFree(pmtCandidate);
884 IEnumMediaTypes_Release(pEnumCandidates);
887 /* then try receiver filter's media types */
888 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
890 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
892 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
894 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
895 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
898 CoTaskMemFree(pmtCandidate);
901 CoTaskMemFree(pmtCandidate);
903 IEnumMediaTypes_Release(pEnumCandidates);
905 } /* if negotiate media type */
907 LeaveCriticalSection(This->pin.pCritSec);
909 TRACE(" -- %x\n", hr);
913 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
915 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
920 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
923 OutputPin *This = (OutputPin *)iface;
927 EnterCriticalSection(This->pin.pCritSec);
929 if (This->pMemInputPin)
931 IMemInputPin_Release(This->pMemInputPin);
932 This->pMemInputPin = NULL;
934 if (This->pin.pConnectedTo)
936 IPin_Release(This->pin.pConnectedTo);
937 This->pin.pConnectedTo = NULL;
943 LeaveCriticalSection(This->pin.pCritSec);
948 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
952 /* not supposed to do anything in an output pin */
957 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
959 TRACE("(%p)->()\n", iface);
961 /* not supposed to do anything in an output pin */
966 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
968 TRACE("(%p)->()\n", iface);
970 /* not supposed to do anything in an output pin */
975 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
977 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
979 /* not supposed to do anything in an output pin */
984 static const IPinVtbl OutputPin_Vtbl =
986 OutputPin_QueryInterface,
990 OutputPin_ReceiveConnection,
991 OutputPin_Disconnect,
992 IPinImpl_ConnectedTo,
993 IPinImpl_ConnectionMediaType,
994 IPinImpl_QueryPinInfo,
995 IPinImpl_QueryDirection,
997 IPinImpl_QueryAccept,
998 IPinImpl_EnumMediaTypes,
999 IPinImpl_QueryInternalConnections,
1000 OutputPin_EndOfStream,
1001 OutputPin_BeginFlush,
1003 OutputPin_NewSegment
1006 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1010 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1012 EnterCriticalSection(This->pin.pCritSec);
1014 if (!This->pin.pConnectedTo)
1015 hr = VFW_E_NOT_CONNECTED;
1018 IMemAllocator * pAlloc = NULL;
1020 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1023 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1026 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1029 IMemAllocator_Release(pAlloc);
1032 LeaveCriticalSection(This->pin.pCritSec);
1037 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1040 IMemInputPin * pMemConnected = NULL;
1043 EnterCriticalSection(This->pin.pCritSec);
1045 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1046 hr = VFW_E_NOT_CONNECTED;
1049 /* we don't have the lock held when using This->pMemInputPin,
1050 * so we need to AddRef it to stop it being deleted while we are
1051 * using it. Same with its filter. */
1052 pMemConnected = This->pMemInputPin;
1053 IMemInputPin_AddRef(pMemConnected);
1054 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1057 LeaveCriticalSection(This->pin.pCritSec);
1061 /* NOTE: if we are in a critical section when Receive is called
1062 * then it causes some problems (most notably with the native Video
1063 * Renderer) if we are re-entered for whatever reason */
1064 hr = IMemInputPin_Receive(pMemConnected, pSample);
1066 /* If the filter's destroyed, tell upstream to stop sending data */
1067 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1071 IMemInputPin_Release(pMemConnected);
1076 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1080 EnterCriticalSection(This->pin.pCritSec);
1082 if (!This->pin.pConnectedTo)
1083 hr = VFW_E_NOT_CONNECTED;
1085 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1087 LeaveCriticalSection(This->pin.pCritSec);
1092 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1096 TRACE("(%p)->()\n", This);
1098 EnterCriticalSection(This->pin.pCritSec);
1100 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1101 hr = VFW_E_NOT_CONNECTED;
1104 IMemAllocator * pAlloc = NULL;
1106 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1109 hr = IMemAllocator_Commit(pAlloc);
1112 IMemAllocator_Release(pAlloc);
1115 LeaveCriticalSection(This->pin.pCritSec);
1120 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1124 TRACE("(%p)->()\n", This);
1126 EnterCriticalSection(This->pin.pCritSec);
1128 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1129 hr = VFW_E_NOT_CONNECTED;
1132 IMemAllocator * pAlloc = NULL;
1134 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1137 hr = IMemAllocator_Decommit(pAlloc);
1140 IMemAllocator_Release(pAlloc);
1143 hr = IPin_Disconnect(This->pin.pConnectedTo);
1146 LeaveCriticalSection(This->pin.pCritSec);
1152 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1158 if (pPinInfo->dir != PINDIR_INPUT)
1160 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1161 return E_INVALIDARG;
1164 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1167 return E_OUTOFMEMORY;
1169 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1171 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1173 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1177 CoTaskMemFree(pPinImpl);
1181 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1183 /* Common attributes */
1184 pPinImpl->pin.refCount = 1;
1185 pPinImpl->pin.pConnectedTo = NULL;
1186 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1187 pPinImpl->pin.pUserData = pUserData;
1188 pPinImpl->pin.pCritSec = pCritSec;
1189 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1190 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1192 /* Input pin attributes */
1193 pPinImpl->fnSampleProc = pSampleProc;
1194 pPinImpl->fnPreConnect = NULL;
1195 pPinImpl->pAlloc = NULL;
1196 pPinImpl->pReader = NULL;
1197 pPinImpl->hThread = NULL;
1198 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1200 pPinImpl->rtStart = 0;
1201 pPinImpl->rtCurrent = 0;
1202 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1207 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1209 PIN_DIRECTION pindirReceive;
1211 PullPin *This = (PullPin *)iface;
1213 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1214 dump_AM_MEDIA_TYPE(pmt);
1216 EnterCriticalSection(This->pin.pCritSec);
1218 if (This->pin.pConnectedTo)
1219 hr = VFW_E_ALREADY_CONNECTED;
1221 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1222 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1223 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1227 IPin_QueryDirection(pReceivePin, &pindirReceive);
1229 if (pindirReceive != PINDIR_OUTPUT)
1231 ERR("Can't connect from non-output pin\n");
1232 hr = VFW_E_INVALID_DIRECTION;
1236 This->pReader = NULL;
1237 This->pAlloc = NULL;
1240 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1245 ALLOCATOR_PROPERTIES props;
1247 props.cbBuffer = 64 * 1024; /* 64k bytes */
1250 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1253 if (SUCCEEDED(hr) && This->fnPreConnect)
1255 hr = This->fnPreConnect(iface, pReceivePin);
1260 CopyMediaType(&This->pin.mtCurrent, pmt);
1261 This->pin.pConnectedTo = pReceivePin;
1262 IPin_AddRef(pReceivePin);
1267 IAsyncReader_Release(This->pReader);
1268 This->pReader = NULL;
1270 IMemAllocator_Release(This->pAlloc);
1271 This->pAlloc = NULL;
1274 LeaveCriticalSection(This->pin.pCritSec);
1278 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1280 PullPin *This = (PullPin *)iface;
1282 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1286 if (IsEqualIID(riid, &IID_IUnknown))
1287 *ppv = (LPVOID)iface;
1288 else if (IsEqualIID(riid, &IID_IPin))
1289 *ppv = (LPVOID)iface;
1290 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1292 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1297 IUnknown_AddRef((IUnknown *)(*ppv));
1301 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1303 return E_NOINTERFACE;
1306 ULONG WINAPI PullPin_Release(IPin * iface)
1308 PullPin *This = (PullPin *)iface;
1309 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1311 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1316 IMemAllocator_Release(This->pAlloc);
1318 IAsyncReader_Release(This->pReader);
1319 CloseHandle(This->hEventStateChanged);
1320 CoTaskMemFree(This);
1326 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1329 SleepEx(INFINITE, TRUE);
1332 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1334 PullPin *This = (PullPin *)iface;
1337 ALLOCATOR_PROPERTIES allocProps;
1339 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1341 SetEvent(This->hEventStateChanged);
1343 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1345 if (This->rtCurrent < This->rtStart)
1346 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1350 while (This->rtCurrent < This->rtStop && hr == S_OK)
1352 /* FIXME: to improve performance by quite a bit this should be changed
1353 * so that one sample is processed while one sample is fetched. However,
1354 * it is harder to debug so for the moment it will stay as it is */
1355 IMediaSample * pSample = NULL;
1356 REFERENCE_TIME rtSampleStart;
1357 REFERENCE_TIME rtSampleStop;
1360 TRACE("Process sample\n");
1362 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1366 rtSampleStart = This->rtCurrent;
1367 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1368 if (rtSampleStop > This->rtStop)
1369 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1370 hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1371 This->rtCurrent = rtSampleStop;
1375 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1378 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1382 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample));
1383 if (rtSampleStop > This->rtStop)
1384 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1385 hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1389 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1391 ERR("Processing error: %x\n", hr);
1394 IMediaSample_Release(pSample);
1402 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1404 PullPin *This = (PullPin *)iface;
1406 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1408 EnterCriticalSection(This->pin.pCritSec);
1412 CloseHandle(This->hThread);
1413 This->hThread = NULL;
1414 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1415 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1417 LeaveCriticalSection(This->pin.pCritSec);
1419 SetEvent(This->hEventStateChanged);
1421 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1426 HRESULT PullPin_InitProcessing(PullPin * This)
1430 TRACE("(%p)->()\n", This);
1432 assert(!This->hThread);
1434 /* if we are connected */
1437 EnterCriticalSection(This->pin.pCritSec);
1440 assert(!This->hThread);
1442 /* AddRef the filter to make sure it and it's pins will be around
1443 * as long as the thread */
1444 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1446 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1449 hr = HRESULT_FROM_WIN32(GetLastError());
1450 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1454 hr = IMemAllocator_Commit(This->pAlloc);
1456 LeaveCriticalSection(This->pin.pCritSec);
1459 TRACE(" -- %x\n", hr);
1464 HRESULT PullPin_StartProcessing(PullPin * This)
1466 /* if we are connected */
1467 TRACE("(%p)->()\n", This);
1470 assert(This->hThread);
1472 ResetEvent(This->hEventStateChanged);
1474 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1475 return HRESULT_FROM_WIN32(GetLastError());
1481 HRESULT PullPin_PauseProcessing(PullPin * This)
1483 /* make the processing function exit its loop */
1489 HRESULT PullPin_StopProcessing(PullPin * This)
1491 /* if we are connected */
1494 assert(This->hThread);
1496 ResetEvent(This->hEventStateChanged);
1498 PullPin_PauseProcessing(This);
1500 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1501 return HRESULT_FROM_WIN32(GetLastError());
1507 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1509 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1514 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1516 FIXME("(%p)->(%x%08x, %x%08x)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1518 PullPin_BeginFlush((IPin *)This);
1519 /* FIXME: need critical section? */
1520 This->rtStart = rtStart;
1521 This->rtStop = rtStop;
1522 PullPin_EndFlush((IPin *)This);
1527 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1529 FIXME("(%p)->() stub\n", iface);
1531 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1534 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1536 FIXME("(%p)->() stub\n", iface);
1538 return SendFurther( iface, deliver_beginflush, NULL, NULL );
1541 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1543 FIXME("(%p)->() stub\n", iface);
1545 return SendFurther( iface, deliver_endflush, NULL, NULL );
1548 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1550 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1552 return SendFurther( iface, deliver_newsegment, NULL, NULL );
1555 static const IPinVtbl PullPin_Vtbl =
1557 PullPin_QueryInterface,
1561 PullPin_ReceiveConnection,
1562 IPinImpl_Disconnect,
1563 IPinImpl_ConnectedTo,
1564 IPinImpl_ConnectionMediaType,
1565 IPinImpl_QueryPinInfo,
1566 IPinImpl_QueryDirection,
1568 IPinImpl_QueryAccept,
1569 IPinImpl_EnumMediaTypes,
1570 IPinImpl_QueryInternalConnections,
1571 PullPin_EndOfStream,