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);
135 hr_local = fnEnd( from, arg );
136 hr_return = updatehres( hr_return, hr_local );
140 if (pin_info.pFilter)
141 IBaseFilter_Release( pin_info.pFilter );
145 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
147 return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
151 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
153 /* Tempting to just do a memcpy, but the name field is
154 128 characters long! We will probably never exceed 10
155 most of the time, so we are better off copying
156 each field manually */
157 strcpyW(pDest->achName, pSrc->achName);
158 pDest->dir = pSrc->dir;
159 pDest->pFilter = pSrc->pFilter;
162 /* Function called as a helper to IPin_Connect */
163 /* specific AM_MEDIA_TYPE - it cannot be NULL */
164 /* NOTE: not part of standard interface */
165 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
167 OutputPin *This = (OutputPin *)iface;
169 IMemAllocator * pMemAlloc = NULL;
170 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
172 TRACE("(%p, %p)\n", pReceivePin, pmt);
173 dump_AM_MEDIA_TYPE(pmt);
175 /* FIXME: call queryacceptproc */
177 This->pin.pConnectedTo = pReceivePin;
178 IPin_AddRef(pReceivePin);
179 CopyMediaType(&This->pin.mtCurrent, pmt);
181 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
183 /* get the IMemInputPin interface we will use to deliver samples to the
187 This->pMemInputPin = NULL;
188 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
190 if (SUCCEEDED(hr) && !This->custom_allocator)
192 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
194 if (hr == VFW_E_NO_ALLOCATOR)
196 /* Input pin provides no allocator, use standard memory allocator */
197 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
201 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
206 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
209 IMemAllocator_Release(pMemAlloc);
211 else if (SUCCEEDED(hr))
215 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
218 hr = VFW_E_NO_ALLOCATOR;
221 /* break connection if we couldn't get the allocator */
224 if (This->pMemInputPin)
225 IMemInputPin_Release(This->pMemInputPin);
226 This->pMemInputPin = NULL;
228 IPin_Disconnect(pReceivePin);
234 IPin_Release(This->pin.pConnectedTo);
235 This->pin.pConnectedTo = NULL;
236 FreeMediaType(&This->pin.mtCurrent);
239 TRACE(" -- %x\n", hr);
243 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData,
244 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, InputPin * pPinImpl)
248 /* Common attributes */
249 pPinImpl->pin.refCount = 1;
250 pPinImpl->pin.pConnectedTo = NULL;
251 pPinImpl->pin.fnQueryAccept = pQueryAccept;
252 pPinImpl->pin.pUserData = pUserData;
253 pPinImpl->pin.pCritSec = pCritSec;
254 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
255 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
257 /* Input pin attributes */
258 pPinImpl->fnSampleProc = pSampleProc;
259 pPinImpl->fnCleanProc = pCleanUp;
260 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
261 if (pPinImpl->preferred_allocator)
262 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
263 pPinImpl->tStart = 0;
265 pPinImpl->dRate = 1.0;
266 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
267 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
268 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
273 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
274 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
278 /* Common attributes */
279 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
280 pPinImpl->pin.refCount = 1;
281 pPinImpl->pin.pConnectedTo = NULL;
282 pPinImpl->pin.fnQueryAccept = pQueryAccept;
283 pPinImpl->pin.pUserData = pUserData;
284 pPinImpl->pin.pCritSec = pCritSec;
285 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
286 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
288 /* Output pin attributes */
289 pPinImpl->pMemInputPin = NULL;
290 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
291 /* If custom_allocator is set, you will need to specify an allocator
292 * in the alloc member of the struct before an output pin can connect
294 pPinImpl->custom_allocator = 0;
295 pPinImpl->alloc = NULL;
296 pPinImpl->readonly = FALSE;
299 pPinImpl->allocProps = *props;
300 if (pPinImpl->allocProps.cbAlign == 0)
301 pPinImpl->allocProps.cbAlign = 1;
304 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
309 HRESULT InputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
315 if (pPinInfo->dir != PINDIR_INPUT)
317 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
321 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
324 return E_OUTOFMEMORY;
326 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, allocator, pPinImpl)))
328 *ppPin = (IPin *)pPinImpl;
332 CoTaskMemFree(pPinImpl);
336 HRESULT OutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, long outputpin_size, const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
338 OutputPin * pPinImpl;
342 if (pPinInfo->dir != PINDIR_OUTPUT)
344 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
348 assert(outputpin_size >= sizeof(OutputPin));
350 pPinImpl = CoTaskMemAlloc(outputpin_size);
353 return E_OUTOFMEMORY;
355 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
357 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
361 CoTaskMemFree(pPinImpl);
365 /*** Common pin functions ***/
367 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
369 IPinImpl *This = (IPinImpl *)iface;
370 ULONG refCount = InterlockedIncrement(&This->refCount);
372 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
377 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
380 IPinImpl *This = (IPinImpl *)iface;
384 EnterCriticalSection(This->pCritSec);
386 if (This->pConnectedTo)
388 IPin_Release(This->pConnectedTo);
389 This->pConnectedTo = NULL;
395 LeaveCriticalSection(This->pCritSec);
400 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
403 IPinImpl *This = (IPinImpl *)iface;
405 TRACE("(%p)\n", ppPin);
407 EnterCriticalSection(This->pCritSec);
409 if (This->pConnectedTo)
411 *ppPin = This->pConnectedTo;
417 hr = VFW_E_NOT_CONNECTED;
421 LeaveCriticalSection(This->pCritSec);
426 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
429 IPinImpl *This = (IPinImpl *)iface;
431 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
433 EnterCriticalSection(This->pCritSec);
435 if (This->pConnectedTo)
437 CopyMediaType(pmt, &This->mtCurrent);
442 ZeroMemory(pmt, sizeof(*pmt));
443 hr = VFW_E_NOT_CONNECTED;
446 LeaveCriticalSection(This->pCritSec);
451 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
453 IPinImpl *This = (IPinImpl *)iface;
455 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
457 Copy_PinInfo(pInfo, &This->pinInfo);
458 IBaseFilter_AddRef(pInfo->pFilter);
463 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
465 IPinImpl *This = (IPinImpl *)iface;
467 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
469 *pPinDir = This->pinInfo.dir;
474 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
476 IPinImpl *This = (IPinImpl *)iface;
478 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
480 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
482 return E_OUTOFMEMORY;
484 strcpyW(*Id, This->pinInfo.achName);
489 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
491 IPinImpl *This = (IPinImpl *)iface;
493 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
495 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
498 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
500 IPinImpl *This = (IPinImpl *)iface;
501 ENUMMEDIADETAILS emd;
503 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
505 /* override this method to allow enumeration of your types */
507 emd.pMediaTypes = NULL;
509 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
512 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
514 IPinImpl *This = (IPinImpl *)iface;
516 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
518 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
521 /*** IPin implementation for an input pin ***/
523 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
525 InputPin *This = (InputPin *)iface;
527 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
531 if (IsEqualIID(riid, &IID_IUnknown))
532 *ppv = (LPVOID)iface;
533 else if (IsEqualIID(riid, &IID_IPin))
534 *ppv = (LPVOID)iface;
535 else if (IsEqualIID(riid, &IID_IMemInputPin))
536 *ppv = (LPVOID)&This->lpVtblMemInput;
537 else if (IsEqualIID(riid, &IID_IMediaSeeking))
539 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
544 IUnknown_AddRef((IUnknown *)(*ppv));
548 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
550 return E_NOINTERFACE;
553 ULONG WINAPI InputPin_Release(IPin * iface)
555 InputPin *This = (InputPin *)iface;
556 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
558 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
562 FreeMediaType(&This->pin.mtCurrent);
563 if (This->pAllocator)
564 IMemAllocator_Release(This->pAllocator);
565 This->pAllocator = NULL;
566 This->pin.lpVtbl = NULL;
574 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
576 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
582 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
584 InputPin *This = (InputPin *)iface;
585 PIN_DIRECTION pindirReceive;
588 TRACE("(%p, %p)\n", pReceivePin, pmt);
589 dump_AM_MEDIA_TYPE(pmt);
591 EnterCriticalSection(This->pin.pCritSec);
593 if (This->pin.pConnectedTo)
594 hr = VFW_E_ALREADY_CONNECTED;
596 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
597 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
598 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
602 IPin_QueryDirection(pReceivePin, &pindirReceive);
604 if (pindirReceive != PINDIR_OUTPUT)
606 ERR("Can't connect from non-output pin\n");
607 hr = VFW_E_INVALID_DIRECTION;
613 CopyMediaType(&This->pin.mtCurrent, pmt);
614 This->pin.pConnectedTo = pReceivePin;
615 IPin_AddRef(pReceivePin);
618 LeaveCriticalSection(This->pin.pCritSec);
623 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
625 return IPin_EndOfStream( pin );
628 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
631 InputPin *This = (InputPin *)iface;
633 TRACE("(%p)\n", This);
635 EnterCriticalSection(This->pin.pCritSec);
639 This->end_of_stream = 1;
640 LeaveCriticalSection(This->pin.pCritSec);
643 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
647 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
649 return IPin_BeginFlush( pin );
652 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
654 InputPin *This = (InputPin *)iface;
656 TRACE("() semi-stub\n");
658 EnterCriticalSection(This->pin.pCritSec);
661 if (This->fnCleanProc)
662 This->fnCleanProc(This->pin.pUserData);
664 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
665 LeaveCriticalSection(This->pin.pCritSec);
670 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
672 return IPin_EndFlush( pin );
675 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
677 InputPin *This = (InputPin *)iface;
679 TRACE("(%p)\n", This);
681 EnterCriticalSection(This->pin.pCritSec);
682 This->flushing = This->end_of_stream = 0;
684 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
685 LeaveCriticalSection(This->pin.pCritSec);
690 typedef struct newsegmentargs
692 REFERENCE_TIME tStart, tStop;
696 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
698 newsegmentargs *args = data;
699 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
702 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
704 InputPin *This = (InputPin *)iface;
707 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
709 args.tStart = This->tStart = tStart;
710 args.tStop = This->tStop = tStop;
711 args.rate = This->dRate = dRate;
713 return SendFurther( iface, deliver_newsegment, &args, NULL );
716 static const IPinVtbl InputPin_Vtbl =
718 InputPin_QueryInterface,
722 InputPin_ReceiveConnection,
724 IPinImpl_ConnectedTo,
725 IPinImpl_ConnectionMediaType,
726 IPinImpl_QueryPinInfo,
727 IPinImpl_QueryDirection,
729 IPinImpl_QueryAccept,
730 IPinImpl_EnumMediaTypes,
731 IPinImpl_QueryInternalConnections,
732 InputPin_EndOfStream,
738 /*** IMemInputPin implementation ***/
740 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
742 InputPin *This = impl_from_IMemInputPin(iface);
744 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
747 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
749 InputPin *This = impl_from_IMemInputPin(iface);
751 return IPin_AddRef((IPin *)&This->pin);
754 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
756 InputPin *This = impl_from_IMemInputPin(iface);
758 return IPin_Release((IPin *)&This->pin);
761 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
763 InputPin *This = impl_from_IMemInputPin(iface);
765 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
767 *ppAllocator = This->pAllocator;
769 IMemAllocator_AddRef(*ppAllocator);
771 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
774 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
776 InputPin *This = impl_from_IMemInputPin(iface);
778 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
781 FIXME("Read only flag not handled yet!\n");
783 /* FIXME: Should we release the allocator on disconnection? */
786 WARN("Null allocator\n");
790 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
793 if (This->pAllocator)
794 IMemAllocator_Release(This->pAllocator);
795 This->pAllocator = pAllocator;
796 if (This->pAllocator)
797 IMemAllocator_AddRef(This->pAllocator);
802 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
804 InputPin *This = impl_from_IMemInputPin(iface);
806 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
808 /* override this method if you have any specific requirements */
813 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
815 InputPin *This = impl_from_IMemInputPin(iface);
818 /* this trace commented out for performance reasons */
819 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
820 hr = This->fnSampleProc(This->pin.pUserData, pSample);
824 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
827 InputPin *This = impl_from_IMemInputPin(iface);
829 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
831 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
833 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
841 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
843 InputPin *This = impl_from_IMemInputPin(iface);
845 TRACE("(%p/%p)->()\n", This, iface);
850 static const IMemInputPinVtbl MemInputPin_Vtbl =
852 MemInputPin_QueryInterface,
855 MemInputPin_GetAllocator,
856 MemInputPin_NotifyAllocator,
857 MemInputPin_GetAllocatorRequirements,
859 MemInputPin_ReceiveMultiple,
860 MemInputPin_ReceiveCanBlock
863 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
865 OutputPin *This = (OutputPin *)iface;
867 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
871 if (IsEqualIID(riid, &IID_IUnknown))
872 *ppv = (LPVOID)iface;
873 else if (IsEqualIID(riid, &IID_IPin))
874 *ppv = (LPVOID)iface;
875 else if (IsEqualIID(riid, &IID_IMediaSeeking))
877 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
882 IUnknown_AddRef((IUnknown *)(*ppv));
886 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
888 return E_NOINTERFACE;
891 ULONG WINAPI OutputPin_Release(IPin * iface)
893 OutputPin *This = (OutputPin *)iface;
894 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
896 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
900 FreeMediaType(&This->pin.mtCurrent);
907 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
910 OutputPin *This = (OutputPin *)iface;
912 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
913 dump_AM_MEDIA_TYPE(pmt);
915 /* If we try to connect to ourself, we will definitely deadlock.
916 * There are other cases where we could deadlock too, but this
917 * catches the obvious case */
918 assert(pReceivePin != iface);
920 EnterCriticalSection(This->pin.pCritSec);
922 /* if we have been a specific type to connect with, then we can either connect
923 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
924 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
925 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
928 /* negotiate media type */
930 IEnumMediaTypes * pEnumCandidates;
931 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
933 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
935 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
937 /* try this filter's media types first */
938 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
940 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
941 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
944 CoTaskMemFree(pmtCandidate);
947 CoTaskMemFree(pmtCandidate);
949 IEnumMediaTypes_Release(pEnumCandidates);
952 /* then try receiver filter's media types */
953 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
955 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
957 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
959 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
960 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
963 CoTaskMemFree(pmtCandidate);
966 CoTaskMemFree(pmtCandidate);
968 IEnumMediaTypes_Release(pEnumCandidates);
970 } /* if negotiate media type */
972 LeaveCriticalSection(This->pin.pCritSec);
974 TRACE(" -- %x\n", hr);
978 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
980 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
985 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
988 OutputPin *This = (OutputPin *)iface;
992 EnterCriticalSection(This->pin.pCritSec);
994 if (This->pMemInputPin)
996 IMemInputPin_Release(This->pMemInputPin);
997 This->pMemInputPin = NULL;
999 if (This->pin.pConnectedTo)
1001 IPin_Release(This->pin.pConnectedTo);
1002 This->pin.pConnectedTo = NULL;
1008 LeaveCriticalSection(This->pin.pCritSec);
1013 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
1017 /* not supposed to do anything in an output pin */
1019 return E_UNEXPECTED;
1022 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
1024 TRACE("(%p)->()\n", iface);
1026 /* not supposed to do anything in an output pin */
1028 return E_UNEXPECTED;
1031 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
1033 TRACE("(%p)->()\n", iface);
1035 /* not supposed to do anything in an output pin */
1037 return E_UNEXPECTED;
1040 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1042 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1044 /* not supposed to do anything in an output pin */
1046 return E_UNEXPECTED;
1049 static const IPinVtbl OutputPin_Vtbl =
1051 OutputPin_QueryInterface,
1055 OutputPin_ReceiveConnection,
1056 OutputPin_Disconnect,
1057 IPinImpl_ConnectedTo,
1058 IPinImpl_ConnectionMediaType,
1059 IPinImpl_QueryPinInfo,
1060 IPinImpl_QueryDirection,
1062 IPinImpl_QueryAccept,
1063 IPinImpl_EnumMediaTypes,
1064 IPinImpl_QueryInternalConnections,
1065 OutputPin_EndOfStream,
1066 OutputPin_BeginFlush,
1068 OutputPin_NewSegment
1071 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1075 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1077 EnterCriticalSection(This->pin.pCritSec);
1079 if (!This->pin.pConnectedTo)
1080 hr = VFW_E_NOT_CONNECTED;
1083 IMemAllocator * pAlloc = NULL;
1085 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1088 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1091 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1094 IMemAllocator_Release(pAlloc);
1097 LeaveCriticalSection(This->pin.pCritSec);
1102 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1105 IMemInputPin * pMemConnected = NULL;
1108 EnterCriticalSection(This->pin.pCritSec);
1110 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1111 hr = VFW_E_NOT_CONNECTED;
1114 /* we don't have the lock held when using This->pMemInputPin,
1115 * so we need to AddRef it to stop it being deleted while we are
1116 * using it. Same with its filter. */
1117 pMemConnected = This->pMemInputPin;
1118 IMemInputPin_AddRef(pMemConnected);
1119 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1122 LeaveCriticalSection(This->pin.pCritSec);
1126 /* NOTE: if we are in a critical section when Receive is called
1127 * then it causes some problems (most notably with the native Video
1128 * Renderer) if we are re-entered for whatever reason */
1129 hr = IMemInputPin_Receive(pMemConnected, pSample);
1131 /* If the filter's destroyed, tell upstream to stop sending data */
1132 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1136 IMemInputPin_Release(pMemConnected);
1141 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1145 EnterCriticalSection(This->pin.pCritSec);
1147 if (!This->pin.pConnectedTo)
1148 hr = VFW_E_NOT_CONNECTED;
1150 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1152 LeaveCriticalSection(This->pin.pCritSec);
1157 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1161 TRACE("(%p)->()\n", This);
1163 EnterCriticalSection(This->pin.pCritSec);
1165 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1166 hr = VFW_E_NOT_CONNECTED;
1169 IMemAllocator * pAlloc = NULL;
1171 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1174 hr = IMemAllocator_Commit(pAlloc);
1177 IMemAllocator_Release(pAlloc);
1180 LeaveCriticalSection(This->pin.pCritSec);
1182 TRACE("--> %08x\n", hr);
1186 HRESULT OutputPin_DecommitAllocator(OutputPin * This)
1190 TRACE("(%p)->()\n", This);
1192 EnterCriticalSection(This->pin.pCritSec);
1194 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1195 hr = VFW_E_NOT_CONNECTED;
1198 IMemAllocator * pAlloc = NULL;
1200 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1203 hr = IMemAllocator_Decommit(pAlloc);
1206 IMemAllocator_Release(pAlloc);
1209 LeaveCriticalSection(This->pin.pCritSec);
1211 TRACE("--> %08x\n", hr);
1215 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1219 TRACE("(%p)->()\n", This);
1221 EnterCriticalSection(This->pin.pCritSec);
1223 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1224 hr = VFW_E_NOT_CONNECTED;
1225 else if (!This->custom_allocator)
1227 IMemAllocator * pAlloc = NULL;
1229 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1232 hr = IMemAllocator_Decommit(pAlloc);
1235 IMemAllocator_Release(pAlloc);
1238 hr = IPin_Disconnect(This->pin.pConnectedTo);
1240 else /* Kill the allocator! */
1242 hr = IPin_Disconnect(This->pin.pConnectedTo);
1244 IPin_Disconnect((IPin *)This);
1246 LeaveCriticalSection(This->pin.pCritSec);
1252 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData,
1253 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1255 /* Common attributes */
1256 pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1257 pPinImpl->pin.refCount = 1;
1258 pPinImpl->pin.pConnectedTo = NULL;
1259 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1260 pPinImpl->pin.pUserData = pUserData;
1261 pPinImpl->pin.pCritSec = pCritSec;
1262 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1263 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1265 /* Input pin attributes */
1266 pPinImpl->fnSampleProc = pSampleProc;
1267 pPinImpl->fnCleanProc = pCleanUp;
1268 pPinImpl->fnDone = pDone;
1269 pPinImpl->fnPreConnect = NULL;
1270 pPinImpl->pAlloc = NULL;
1271 pPinImpl->pReader = NULL;
1272 pPinImpl->hThread = NULL;
1273 pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1274 pPinImpl->thread_sleepy = CreateEventW(NULL, FALSE, FALSE, NULL);
1276 pPinImpl->rtStart = 0;
1277 pPinImpl->rtCurrent = 0;
1278 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1279 pPinImpl->dRate = 1.0;
1280 pPinImpl->state = Req_Die;
1281 pPinImpl->fnCustomRequest = pCustomRequest;
1282 pPinImpl->stop_playback = 1;
1284 InitializeCriticalSection(&pPinImpl->thread_lock);
1285 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1290 HRESULT PullPin_Construct(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1296 if (pPinInfo->dir != PINDIR_INPUT)
1298 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1299 return E_INVALIDARG;
1302 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1305 return E_OUTOFMEMORY;
1307 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
1309 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1313 CoTaskMemFree(pPinImpl);
1317 static HRESULT PullPin_InitProcessing(PullPin * This);
1319 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1321 PIN_DIRECTION pindirReceive;
1323 PullPin *This = (PullPin *)iface;
1325 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1326 dump_AM_MEDIA_TYPE(pmt);
1328 EnterCriticalSection(This->pin.pCritSec);
1329 if (!This->pin.pConnectedTo)
1331 ALLOCATOR_PROPERTIES props;
1334 props.cbBuffer = 64 * 1024; /* 64k bytes */
1338 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1339 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1340 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1344 IPin_QueryDirection(pReceivePin, &pindirReceive);
1346 if (pindirReceive != PINDIR_OUTPUT)
1348 ERR("Can't connect from non-output pin\n");
1349 hr = VFW_E_INVALID_DIRECTION;
1353 This->pReader = NULL;
1354 This->pAlloc = NULL;
1357 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1360 if (SUCCEEDED(hr) && This->fnPreConnect)
1362 hr = This->fnPreConnect(iface, pReceivePin, &props);
1367 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1372 CopyMediaType(&This->pin.mtCurrent, pmt);
1373 This->pin.pConnectedTo = pReceivePin;
1374 IPin_AddRef(pReceivePin);
1375 hr = IMemAllocator_Commit(This->pAlloc);
1379 hr = PullPin_InitProcessing(This);
1384 IAsyncReader_Release(This->pReader);
1385 This->pReader = NULL;
1387 IMemAllocator_Release(This->pAlloc);
1388 This->pAlloc = NULL;
1392 hr = VFW_E_ALREADY_CONNECTED;
1393 LeaveCriticalSection(This->pin.pCritSec);
1397 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1399 PullPin *This = (PullPin *)iface;
1401 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1405 if (IsEqualIID(riid, &IID_IUnknown))
1406 *ppv = (LPVOID)iface;
1407 else if (IsEqualIID(riid, &IID_IPin))
1408 *ppv = (LPVOID)iface;
1409 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1411 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1416 IUnknown_AddRef((IUnknown *)(*ppv));
1420 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1422 return E_NOINTERFACE;
1425 ULONG WINAPI PullPin_Release(IPin *iface)
1427 PullPin *This = (PullPin *)iface;
1428 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1430 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1434 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1435 assert(!This->hThread);
1438 IMemAllocator_Release(This->pAlloc);
1440 IAsyncReader_Release(This->pReader);
1441 CloseHandle(This->thread_sleepy);
1442 CloseHandle(This->hEventStateChanged);
1443 This->thread_lock.DebugInfo->Spare[0] = 0;
1444 DeleteCriticalSection(&This->thread_lock);
1445 CoTaskMemFree(This);
1451 static void CALLBACK PullPin_Flush(PullPin *This)
1453 IMediaSample *pSample;
1454 TRACE("Flushing!\n");
1458 /* Flush outstanding samples */
1459 IAsyncReader_BeginFlush(This->pReader);
1465 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
1470 assert(!IMediaSample_GetActualDataLength(pSample));
1472 IMediaSample_Release(pSample);
1475 IAsyncReader_EndFlush(This->pReader);
1479 static void CALLBACK PullPin_Thread_Process(PullPin *This)
1482 IMediaSample * pSample = NULL;
1483 ALLOCATOR_PROPERTIES allocProps;
1485 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1487 This->cbAlign = allocProps.cbAlign;
1489 if (This->rtCurrent < This->rtStart)
1490 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
1494 if (This->rtCurrent >= This->rtStop)
1496 IPin_EndOfStream((IPin *)This);
1500 /* There is no sample in our buffer */
1501 hr = This->fnCustomRequest(This->pin.pUserData);
1504 ERR("Request error: %x\n", hr);
1506 EnterCriticalSection(This->pin.pCritSec);
1507 SetEvent(This->hEventStateChanged);
1508 LeaveCriticalSection(This->pin.pCritSec);
1515 TRACE("Process sample\n");
1517 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1519 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1522 hr = This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1526 /* FIXME: This is not well handled yet! */
1527 ERR("Processing error: %x\n", hr);
1532 IMediaSample_Release(pSample);
1535 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1537 /* Sample was rejected, and we are asked to terminate */
1540 IMediaSample_Release(pSample);
1543 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1544 * Flush remaining samples
1547 This->fnDone(This->pin.pUserData);
1549 TRACE("End: %08x, %d\n", hr, This->stop_playback);
1552 static void CALLBACK PullPin_Thread_Pause(PullPin *This)
1554 EnterCriticalSection(This->pin.pCritSec);
1555 This->state = Req_Sleepy;
1556 SetEvent(This->hEventStateChanged);
1557 LeaveCriticalSection(This->pin.pCritSec);
1560 static void CALLBACK PullPin_Thread_Stop(PullPin *This)
1562 TRACE("(%p)->()\n", This);
1564 EnterCriticalSection(This->pin.pCritSec);
1566 CloseHandle(This->hThread);
1567 This->hThread = NULL;
1568 SetEvent(This->hEventStateChanged);
1570 LeaveCriticalSection(This->pin.pCritSec);
1572 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1578 static void CALLBACK PullPin_Thread_Flush(PullPin *This)
1580 PullPin_Flush(This);
1582 EnterCriticalSection(This->pin.pCritSec);
1583 This->state = Req_Sleepy;
1584 SetEvent(This->hEventStateChanged);
1585 LeaveCriticalSection(This->pin.pCritSec);
1588 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1591 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1593 PullPin_Flush(This);
1597 WaitForSingleObject(This->thread_sleepy, INFINITE);
1599 TRACE("State: %d\n", This->state);
1601 switch (This->state)
1603 case Req_Die: PullPin_Thread_Stop(This); break;
1604 case Req_Run: PullPin_Thread_Process(This); break;
1605 case Req_Pause: PullPin_Thread_Pause(This); break;
1606 case Req_Flush: PullPin_Thread_Flush(This); break;
1607 case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
1608 default: ERR("Unknown state request: %d\n", This->state); break;
1613 static HRESULT PullPin_InitProcessing(PullPin * This)
1617 TRACE("(%p)->()\n", This);
1619 /* if we are connected */
1624 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1625 EnterCriticalSection(This->pin.pCritSec);
1627 assert(!This->hThread);
1628 assert(This->state == Req_Die);
1629 assert(This->stop_playback);
1630 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1631 This->state = Req_Sleepy;
1633 /* AddRef the filter to make sure it and it's pins will be around
1634 * as long as the thread */
1635 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1638 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
1641 hr = HRESULT_FROM_WIN32(GetLastError());
1642 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1647 SetEvent(This->hEventStateChanged);
1648 /* If assert fails, that means a command was not processed before the thread previously terminated */
1650 LeaveCriticalSection(This->pin.pCritSec);
1653 TRACE(" -- %x\n", hr);
1658 HRESULT PullPin_StartProcessing(PullPin * This)
1660 /* if we are connected */
1661 TRACE("(%p)->()\n", This);
1664 assert(This->hThread);
1666 PullPin_WaitForStateChange(This, INFINITE);
1668 assert(This->state == Req_Sleepy);
1671 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1672 This->state = Req_Run;
1673 This->stop_playback = 0;
1674 ResetEvent(This->hEventStateChanged);
1675 SetEvent(This->thread_sleepy);
1681 HRESULT PullPin_PauseProcessing(PullPin * This)
1683 /* if we are connected */
1684 TRACE("(%p)->()\n", This);
1687 assert(This->hThread);
1689 PullPin_WaitForStateChange(This, INFINITE);
1691 EnterCriticalSection(This->pin.pCritSec);
1693 assert(!This->stop_playback);
1694 assert(This->state == Req_Run|| This->state == Req_Sleepy);
1696 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1697 This->state = Req_Pause;
1698 This->stop_playback = 1;
1699 ResetEvent(This->hEventStateChanged);
1700 SetEvent(This->thread_sleepy);
1702 LeaveCriticalSection(This->pin.pCritSec);
1708 static HRESULT PullPin_StopProcessing(PullPin * This)
1710 TRACE("(%p)->()\n", This);
1712 /* if we are alive */
1713 assert(This->hThread);
1715 PullPin_WaitForStateChange(This, INFINITE);
1717 assert(This->state == Req_Pause || This->state == Req_Sleepy);
1719 This->stop_playback = 1;
1720 This->state = Req_Die;
1721 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1722 ResetEvent(This->hEventStateChanged);
1723 SetEvent(This->thread_sleepy);
1727 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1729 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1734 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1736 FIXME("(%p)->() stub\n", iface);
1738 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1741 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1743 PullPin *This = (PullPin *)iface;
1744 TRACE("(%p)->()\n", This);
1746 EnterCriticalSection(This->pin.pCritSec);
1748 SendFurther( iface, deliver_beginflush, NULL, NULL );
1750 LeaveCriticalSection(This->pin.pCritSec);
1752 EnterCriticalSection(&This->thread_lock);
1755 IAsyncReader_BeginFlush(This->pReader);
1756 PullPin_WaitForStateChange(This, INFINITE);
1758 if (This->hThread && This->state == Req_Run)
1760 PullPin_PauseProcessing(This);
1761 PullPin_WaitForStateChange(This, INFINITE);
1764 This->state = Req_Flush;
1765 ResetEvent(This->hEventStateChanged);
1766 SetEvent(This->thread_sleepy);
1767 PullPin_WaitForStateChange(This, INFINITE);
1769 LeaveCriticalSection(&This->thread_lock);
1771 EnterCriticalSection(This->pin.pCritSec);
1773 This->fnCleanProc(This->pin.pUserData);
1775 LeaveCriticalSection(This->pin.pCritSec);
1780 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1782 PullPin *This = (PullPin *)iface;
1784 TRACE("(%p)->()\n", iface);
1786 /* Send further first: Else a race condition might terminate processing early */
1787 EnterCriticalSection(This->pin.pCritSec);
1788 SendFurther( iface, deliver_endflush, NULL, NULL );
1789 LeaveCriticalSection(This->pin.pCritSec);
1791 EnterCriticalSection(&This->thread_lock);
1794 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1796 if (state != State_Stopped)
1797 PullPin_StartProcessing(This);
1799 PullPin_WaitForStateChange(This, INFINITE);
1801 LeaveCriticalSection(&This->thread_lock);
1806 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
1809 PullPin *This = (PullPin *)iface;
1813 EnterCriticalSection(This->pin.pCritSec);
1815 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1816 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1818 if (This->pin.pConnectedTo)
1820 IPin_Release(This->pin.pConnectedTo);
1821 This->pin.pConnectedTo = NULL;
1822 PullPin_StopProcessing(This);
1828 LeaveCriticalSection(This->pin.pCritSec);
1833 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1835 newsegmentargs args;
1836 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1838 args.tStart = tStart;
1842 return SendFurther( iface, deliver_newsegment, &args, NULL );
1845 static const IPinVtbl PullPin_Vtbl =
1847 PullPin_QueryInterface,
1851 PullPin_ReceiveConnection,
1853 IPinImpl_ConnectedTo,
1854 IPinImpl_ConnectionMediaType,
1855 IPinImpl_QueryPinInfo,
1856 IPinImpl_QueryDirection,
1858 IPinImpl_QueryAccept,
1859 IPinImpl_EnumMediaTypes,
1860 IPinImpl_QueryInternalConnections,
1861 PullPin_EndOfStream,