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);
130 hr_local = fnEnd( from, arg );
131 hr_return = updatehres( hr_return, hr_local );
135 if (pin_info.pFilter)
136 IBaseFilter_Release( pin_info.pFilter );
140 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
142 return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
146 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
148 /* Tempting to just do a memcpy, but the name field is
149 128 characters long! We will probably never exceed 10
150 most of the time, so we are better off copying
151 each field manually */
152 strcpyW(pDest->achName, pSrc->achName);
153 pDest->dir = pSrc->dir;
154 pDest->pFilter = pSrc->pFilter;
157 /* Function called as a helper to IPin_Connect */
158 /* specific AM_MEDIA_TYPE - it cannot be NULL */
159 /* NOTE: not part of standard interface */
160 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
162 OutputPin *This = (OutputPin *)iface;
164 IMemAllocator * pMemAlloc = NULL;
165 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
167 TRACE("(%p, %p)\n", pReceivePin, pmt);
168 dump_AM_MEDIA_TYPE(pmt);
170 /* FIXME: call queryacceptproc */
172 This->pin.pConnectedTo = pReceivePin;
173 IPin_AddRef(pReceivePin);
174 CopyMediaType(&This->pin.mtCurrent, pmt);
176 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
178 /* get the IMemInputPin interface we will use to deliver samples to the
182 This->pMemInputPin = NULL;
183 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
185 if (SUCCEEDED(hr) && !This->custom_allocator)
187 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
189 if (hr == VFW_E_NO_ALLOCATOR)
191 /* Input pin provides no allocator, use standard memory allocator */
192 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
196 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
201 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
204 IMemAllocator_Release(pMemAlloc);
206 else if (SUCCEEDED(hr))
210 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
213 hr = VFW_E_NO_ALLOCATOR;
216 /* break connection if we couldn't get the allocator */
219 if (This->pMemInputPin)
220 IMemInputPin_Release(This->pMemInputPin);
221 This->pMemInputPin = NULL;
223 IPin_Disconnect(pReceivePin);
229 IPin_Release(This->pin.pConnectedTo);
230 This->pin.pConnectedTo = NULL;
231 FreeMediaType(&This->pin.mtCurrent);
234 TRACE(" -- %x\n", hr);
238 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData,
239 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, InputPin * pPinImpl)
243 /* Common attributes */
244 pPinImpl->pin.refCount = 1;
245 pPinImpl->pin.pConnectedTo = NULL;
246 pPinImpl->pin.fnQueryAccept = pQueryAccept;
247 pPinImpl->pin.pUserData = pUserData;
248 pPinImpl->pin.pCritSec = pCritSec;
249 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
250 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
252 /* Input pin attributes */
253 pPinImpl->fnSampleProc = pSampleProc;
254 pPinImpl->fnCleanProc = pCleanUp;
255 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
256 if (pPinImpl->preferred_allocator)
257 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
258 pPinImpl->tStart = 0;
260 pPinImpl->dRate = 1.0;
261 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
262 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
263 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
268 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
269 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
273 /* Common attributes */
274 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
275 pPinImpl->pin.refCount = 1;
276 pPinImpl->pin.pConnectedTo = NULL;
277 pPinImpl->pin.fnQueryAccept = pQueryAccept;
278 pPinImpl->pin.pUserData = pUserData;
279 pPinImpl->pin.pCritSec = pCritSec;
280 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
281 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
283 /* Output pin attributes */
284 pPinImpl->pMemInputPin = NULL;
285 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
286 /* If custom_allocator is set, you will need to specify an allocator
287 * in the alloc member of the struct before an output pin can connect
289 pPinImpl->custom_allocator = 0;
290 pPinImpl->alloc = NULL;
291 pPinImpl->readonly = FALSE;
294 pPinImpl->allocProps = *props;
295 if (pPinImpl->allocProps.cbAlign == 0)
296 pPinImpl->allocProps.cbAlign = 1;
299 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
304 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)
310 if (pPinInfo->dir != PINDIR_INPUT)
312 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
316 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
319 return E_OUTOFMEMORY;
321 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, allocator, pPinImpl)))
323 *ppPin = (IPin *)pPinImpl;
327 CoTaskMemFree(pPinImpl);
331 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)
333 OutputPin * pPinImpl;
337 if (pPinInfo->dir != PINDIR_OUTPUT)
339 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
343 assert(outputpin_size >= sizeof(OutputPin));
345 pPinImpl = CoTaskMemAlloc(outputpin_size);
348 return E_OUTOFMEMORY;
350 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
352 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
356 CoTaskMemFree(pPinImpl);
360 /*** Common pin functions ***/
362 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
364 IPinImpl *This = (IPinImpl *)iface;
365 ULONG refCount = InterlockedIncrement(&This->refCount);
367 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
372 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
375 IPinImpl *This = (IPinImpl *)iface;
379 EnterCriticalSection(This->pCritSec);
381 if (This->pConnectedTo)
383 IPin_Release(This->pConnectedTo);
384 This->pConnectedTo = NULL;
390 LeaveCriticalSection(This->pCritSec);
395 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
398 IPinImpl *This = (IPinImpl *)iface;
400 TRACE("(%p)\n", ppPin);
402 EnterCriticalSection(This->pCritSec);
404 if (This->pConnectedTo)
406 *ppPin = This->pConnectedTo;
411 hr = VFW_E_NOT_CONNECTED;
413 LeaveCriticalSection(This->pCritSec);
418 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
421 IPinImpl *This = (IPinImpl *)iface;
423 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
425 EnterCriticalSection(This->pCritSec);
427 if (This->pConnectedTo)
429 CopyMediaType(pmt, &This->mtCurrent);
434 ZeroMemory(pmt, sizeof(*pmt));
435 hr = VFW_E_NOT_CONNECTED;
438 LeaveCriticalSection(This->pCritSec);
443 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
445 IPinImpl *This = (IPinImpl *)iface;
447 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
449 Copy_PinInfo(pInfo, &This->pinInfo);
450 IBaseFilter_AddRef(pInfo->pFilter);
455 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
457 IPinImpl *This = (IPinImpl *)iface;
459 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
461 *pPinDir = This->pinInfo.dir;
466 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
468 IPinImpl *This = (IPinImpl *)iface;
470 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
472 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
474 return E_OUTOFMEMORY;
476 strcpyW(*Id, This->pinInfo.achName);
481 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
483 IPinImpl *This = (IPinImpl *)iface;
485 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
487 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
490 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
492 IPinImpl *This = (IPinImpl *)iface;
493 ENUMMEDIADETAILS emd;
495 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
497 /* override this method to allow enumeration of your types */
499 emd.pMediaTypes = NULL;
501 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
504 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
506 IPinImpl *This = (IPinImpl *)iface;
508 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
510 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
513 /*** IPin implementation for an input pin ***/
515 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
517 InputPin *This = (InputPin *)iface;
519 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
523 if (IsEqualIID(riid, &IID_IUnknown))
524 *ppv = (LPVOID)iface;
525 else if (IsEqualIID(riid, &IID_IPin))
526 *ppv = (LPVOID)iface;
527 else if (IsEqualIID(riid, &IID_IMemInputPin))
528 *ppv = (LPVOID)&This->lpVtblMemInput;
529 else if (IsEqualIID(riid, &IID_IMediaSeeking))
531 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
536 IUnknown_AddRef((IUnknown *)(*ppv));
540 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
542 return E_NOINTERFACE;
545 ULONG WINAPI InputPin_Release(IPin * iface)
547 InputPin *This = (InputPin *)iface;
548 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
550 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
554 FreeMediaType(&This->pin.mtCurrent);
555 if (This->pAllocator)
556 IMemAllocator_Release(This->pAllocator);
557 This->pAllocator = NULL;
558 This->pin.lpVtbl = NULL;
566 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
568 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
574 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
576 InputPin *This = (InputPin *)iface;
577 PIN_DIRECTION pindirReceive;
580 TRACE("(%p, %p)\n", pReceivePin, pmt);
581 dump_AM_MEDIA_TYPE(pmt);
583 EnterCriticalSection(This->pin.pCritSec);
585 if (This->pin.pConnectedTo)
586 hr = VFW_E_ALREADY_CONNECTED;
588 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
589 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
590 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
594 IPin_QueryDirection(pReceivePin, &pindirReceive);
596 if (pindirReceive != PINDIR_OUTPUT)
598 ERR("Can't connect from non-output pin\n");
599 hr = VFW_E_INVALID_DIRECTION;
605 CopyMediaType(&This->pin.mtCurrent, pmt);
606 This->pin.pConnectedTo = pReceivePin;
607 IPin_AddRef(pReceivePin);
610 LeaveCriticalSection(This->pin.pCritSec);
615 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
617 return IPin_EndOfStream( pin );
620 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
622 InputPin *This = (InputPin *)iface;
623 TRACE("(%p)\n", This);
625 This->end_of_stream = 1;
627 return SendFurther( iface, deliver_endofstream, NULL, NULL );
630 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
632 return IPin_BeginFlush( pin );
635 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
637 InputPin *This = (InputPin *)iface;
639 TRACE("() semi-stub\n");
641 EnterCriticalSection(This->pin.pCritSec);
644 if (This->fnCleanProc)
645 This->fnCleanProc(This->pin.pUserData);
647 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
648 LeaveCriticalSection(This->pin.pCritSec);
653 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
655 return IPin_EndFlush( pin );
658 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
660 InputPin *This = (InputPin *)iface;
662 TRACE("(%p)\n", This);
664 EnterCriticalSection(This->pin.pCritSec);
667 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
668 LeaveCriticalSection(This->pin.pCritSec);
673 typedef struct newsegmentargs
675 REFERENCE_TIME tStart, tStop;
679 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
681 newsegmentargs *args = data;
682 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
685 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
687 InputPin *This = (InputPin *)iface;
690 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
692 args.tStart = This->tStart = tStart;
693 args.tStop = This->tStop = tStop;
694 args.rate = This->dRate = dRate;
696 return SendFurther( iface, deliver_newsegment, &args, NULL );
699 static const IPinVtbl InputPin_Vtbl =
701 InputPin_QueryInterface,
705 InputPin_ReceiveConnection,
707 IPinImpl_ConnectedTo,
708 IPinImpl_ConnectionMediaType,
709 IPinImpl_QueryPinInfo,
710 IPinImpl_QueryDirection,
712 IPinImpl_QueryAccept,
713 IPinImpl_EnumMediaTypes,
714 IPinImpl_QueryInternalConnections,
715 InputPin_EndOfStream,
721 /*** IMemInputPin implementation ***/
723 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
725 InputPin *This = impl_from_IMemInputPin(iface);
727 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
730 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
732 InputPin *This = impl_from_IMemInputPin(iface);
734 return IPin_AddRef((IPin *)&This->pin);
737 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
739 InputPin *This = impl_from_IMemInputPin(iface);
741 return IPin_Release((IPin *)&This->pin);
744 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
746 InputPin *This = impl_from_IMemInputPin(iface);
748 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
750 *ppAllocator = This->pAllocator;
752 IMemAllocator_AddRef(*ppAllocator);
754 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
757 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
759 InputPin *This = impl_from_IMemInputPin(iface);
761 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
764 FIXME("Read only flag not handled yet!\n");
766 /* FIXME: Should we release the allocator on disconnection? */
769 WARN("Null allocator\n");
773 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
776 if (This->pAllocator)
777 IMemAllocator_Release(This->pAllocator);
778 This->pAllocator = pAllocator;
779 if (This->pAllocator)
780 IMemAllocator_AddRef(This->pAllocator);
785 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
787 InputPin *This = impl_from_IMemInputPin(iface);
789 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
791 /* override this method if you have any specific requirements */
796 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
798 InputPin *This = impl_from_IMemInputPin(iface);
801 /* this trace commented out for performance reasons */
802 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
804 EnterCriticalSection(This->pin.pCritSec);
805 if (!This->end_of_stream && !This->flushing)
806 hr = This->fnSampleProc(This->pin.pUserData, pSample);
809 LeaveCriticalSection(This->pin.pCritSec);
813 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
816 InputPin *This = impl_from_IMemInputPin(iface);
818 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
820 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
822 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
830 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
832 InputPin *This = impl_from_IMemInputPin(iface);
834 TRACE("(%p/%p)->()\n", This, iface);
839 static const IMemInputPinVtbl MemInputPin_Vtbl =
841 MemInputPin_QueryInterface,
844 MemInputPin_GetAllocator,
845 MemInputPin_NotifyAllocator,
846 MemInputPin_GetAllocatorRequirements,
848 MemInputPin_ReceiveMultiple,
849 MemInputPin_ReceiveCanBlock
852 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
854 OutputPin *This = (OutputPin *)iface;
856 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
860 if (IsEqualIID(riid, &IID_IUnknown))
861 *ppv = (LPVOID)iface;
862 else if (IsEqualIID(riid, &IID_IPin))
863 *ppv = (LPVOID)iface;
864 else if (IsEqualIID(riid, &IID_IMediaSeeking))
866 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
871 IUnknown_AddRef((IUnknown *)(*ppv));
875 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
877 return E_NOINTERFACE;
880 ULONG WINAPI OutputPin_Release(IPin * iface)
882 OutputPin *This = (OutputPin *)iface;
883 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
885 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
889 FreeMediaType(&This->pin.mtCurrent);
896 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
899 OutputPin *This = (OutputPin *)iface;
901 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
902 dump_AM_MEDIA_TYPE(pmt);
904 /* If we try to connect to ourself, we will definitely deadlock.
905 * There are other cases where we could deadlock too, but this
906 * catches the obvious case */
907 assert(pReceivePin != iface);
909 EnterCriticalSection(This->pin.pCritSec);
911 /* if we have been a specific type to connect with, then we can either connect
912 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
913 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
914 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
917 /* negotiate media type */
919 IEnumMediaTypes * pEnumCandidates;
920 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
922 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
924 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
926 /* try this filter's media types first */
927 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
929 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
930 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
933 CoTaskMemFree(pmtCandidate);
936 CoTaskMemFree(pmtCandidate);
938 IEnumMediaTypes_Release(pEnumCandidates);
941 /* then try receiver filter's media types */
942 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
944 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
946 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
948 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
949 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
952 CoTaskMemFree(pmtCandidate);
955 CoTaskMemFree(pmtCandidate);
957 IEnumMediaTypes_Release(pEnumCandidates);
959 } /* if negotiate media type */
961 LeaveCriticalSection(This->pin.pCritSec);
963 TRACE(" -- %x\n", hr);
967 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
969 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
974 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
977 OutputPin *This = (OutputPin *)iface;
981 EnterCriticalSection(This->pin.pCritSec);
983 if (This->pMemInputPin)
985 IMemInputPin_Release(This->pMemInputPin);
986 This->pMemInputPin = NULL;
988 if (This->pin.pConnectedTo)
990 IPin_Release(This->pin.pConnectedTo);
991 This->pin.pConnectedTo = NULL;
997 LeaveCriticalSection(This->pin.pCritSec);
1002 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
1006 /* not supposed to do anything in an output pin */
1008 return E_UNEXPECTED;
1011 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
1013 TRACE("(%p)->()\n", iface);
1015 /* not supposed to do anything in an output pin */
1017 return E_UNEXPECTED;
1020 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
1022 TRACE("(%p)->()\n", iface);
1024 /* not supposed to do anything in an output pin */
1026 return E_UNEXPECTED;
1029 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1031 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1033 /* not supposed to do anything in an output pin */
1035 return E_UNEXPECTED;
1038 static const IPinVtbl OutputPin_Vtbl =
1040 OutputPin_QueryInterface,
1044 OutputPin_ReceiveConnection,
1045 OutputPin_Disconnect,
1046 IPinImpl_ConnectedTo,
1047 IPinImpl_ConnectionMediaType,
1048 IPinImpl_QueryPinInfo,
1049 IPinImpl_QueryDirection,
1051 IPinImpl_QueryAccept,
1052 IPinImpl_EnumMediaTypes,
1053 IPinImpl_QueryInternalConnections,
1054 OutputPin_EndOfStream,
1055 OutputPin_BeginFlush,
1057 OutputPin_NewSegment
1060 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1064 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1066 EnterCriticalSection(This->pin.pCritSec);
1068 if (!This->pin.pConnectedTo)
1069 hr = VFW_E_NOT_CONNECTED;
1072 IMemAllocator * pAlloc = NULL;
1074 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1077 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1080 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1083 IMemAllocator_Release(pAlloc);
1086 LeaveCriticalSection(This->pin.pCritSec);
1091 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1094 IMemInputPin * pMemConnected = NULL;
1097 EnterCriticalSection(This->pin.pCritSec);
1099 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1100 hr = VFW_E_NOT_CONNECTED;
1103 /* we don't have the lock held when using This->pMemInputPin,
1104 * so we need to AddRef it to stop it being deleted while we are
1105 * using it. Same with its filter. */
1106 pMemConnected = This->pMemInputPin;
1107 IMemInputPin_AddRef(pMemConnected);
1108 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1111 LeaveCriticalSection(This->pin.pCritSec);
1115 /* NOTE: if we are in a critical section when Receive is called
1116 * then it causes some problems (most notably with the native Video
1117 * Renderer) if we are re-entered for whatever reason */
1118 hr = IMemInputPin_Receive(pMemConnected, pSample);
1120 /* If the filter's destroyed, tell upstream to stop sending data */
1121 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1125 IMemInputPin_Release(pMemConnected);
1130 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1134 EnterCriticalSection(This->pin.pCritSec);
1136 if (!This->pin.pConnectedTo)
1137 hr = VFW_E_NOT_CONNECTED;
1139 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1141 LeaveCriticalSection(This->pin.pCritSec);
1146 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1150 TRACE("(%p)->()\n", This);
1152 EnterCriticalSection(This->pin.pCritSec);
1154 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1155 hr = VFW_E_NOT_CONNECTED;
1158 IMemAllocator * pAlloc = NULL;
1160 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1163 hr = IMemAllocator_Commit(pAlloc);
1166 IMemAllocator_Release(pAlloc);
1169 LeaveCriticalSection(This->pin.pCritSec);
1171 TRACE("--> %08x\n", hr);
1175 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1179 TRACE("(%p)->()\n", This);
1181 EnterCriticalSection(This->pin.pCritSec);
1183 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1184 hr = VFW_E_NOT_CONNECTED;
1185 else if (!This->custom_allocator)
1187 IMemAllocator * pAlloc = NULL;
1189 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1192 hr = IMemAllocator_Decommit(pAlloc);
1195 IMemAllocator_Release(pAlloc);
1198 hr = IPin_Disconnect(This->pin.pConnectedTo);
1200 else /* Kill the allocator! */
1202 hr = IPin_Disconnect(This->pin.pConnectedTo);
1204 IPin_Disconnect((IPin *)This);
1206 LeaveCriticalSection(This->pin.pCritSec);
1212 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData,
1213 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1215 /* Common attributes */
1216 pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1217 pPinImpl->pin.refCount = 1;
1218 pPinImpl->pin.pConnectedTo = NULL;
1219 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1220 pPinImpl->pin.pUserData = pUserData;
1221 pPinImpl->pin.pCritSec = pCritSec;
1222 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1223 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1225 /* Input pin attributes */
1226 pPinImpl->fnSampleProc = pSampleProc;
1227 pPinImpl->fnCleanProc = pCleanUp;
1228 pPinImpl->fnDone = pDone;
1229 pPinImpl->fnPreConnect = NULL;
1230 pPinImpl->pAlloc = NULL;
1231 pPinImpl->pReader = NULL;
1232 pPinImpl->hThread = NULL;
1233 pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1234 pPinImpl->thread_sleepy = CreateEventW(NULL, FALSE, FALSE, NULL);
1236 pPinImpl->rtStart = 0;
1237 pPinImpl->rtCurrent = 0;
1238 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1239 pPinImpl->dRate = 1.0;
1240 pPinImpl->state = Req_Die;
1241 pPinImpl->fnCustomRequest = pCustomRequest;
1242 pPinImpl->stop_playback = 1;
1244 InitializeCriticalSection(&pPinImpl->thread_lock);
1245 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1250 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)
1256 if (pPinInfo->dir != PINDIR_INPUT)
1258 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1259 return E_INVALIDARG;
1262 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1265 return E_OUTOFMEMORY;
1267 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
1269 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1273 CoTaskMemFree(pPinImpl);
1277 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1279 PIN_DIRECTION pindirReceive;
1281 PullPin *This = (PullPin *)iface;
1283 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1284 dump_AM_MEDIA_TYPE(pmt);
1286 EnterCriticalSection(This->pin.pCritSec);
1288 ALLOCATOR_PROPERTIES props;
1291 props.cbBuffer = 64 * 1024; /* 64k bytes */
1295 if (This->pin.pConnectedTo)
1296 hr = VFW_E_ALREADY_CONNECTED;
1298 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1299 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1300 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1304 IPin_QueryDirection(pReceivePin, &pindirReceive);
1306 if (pindirReceive != PINDIR_OUTPUT)
1308 ERR("Can't connect from non-output pin\n");
1309 hr = VFW_E_INVALID_DIRECTION;
1313 This->pReader = NULL;
1314 This->pAlloc = NULL;
1317 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1320 if (SUCCEEDED(hr) && This->fnPreConnect)
1322 hr = This->fnPreConnect(iface, pReceivePin, &props);
1327 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1332 CopyMediaType(&This->pin.mtCurrent, pmt);
1333 This->pin.pConnectedTo = pReceivePin;
1334 IPin_AddRef(pReceivePin);
1335 hr = IMemAllocator_Commit(This->pAlloc);
1342 IAsyncReader_Release(This->pReader);
1343 This->pReader = NULL;
1345 IMemAllocator_Release(This->pAlloc);
1346 This->pAlloc = NULL;
1349 LeaveCriticalSection(This->pin.pCritSec);
1353 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1355 PullPin *This = (PullPin *)iface;
1357 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1361 if (IsEqualIID(riid, &IID_IUnknown))
1362 *ppv = (LPVOID)iface;
1363 else if (IsEqualIID(riid, &IID_IPin))
1364 *ppv = (LPVOID)iface;
1365 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1367 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1372 IUnknown_AddRef((IUnknown *)(*ppv));
1376 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1378 return E_NOINTERFACE;
1381 ULONG WINAPI PullPin_Release(IPin *iface)
1383 PullPin *This = (PullPin *)iface;
1384 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1386 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1390 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1391 assert(!This->hThread);
1394 IMemAllocator_Release(This->pAlloc);
1396 IAsyncReader_Release(This->pReader);
1397 CloseHandle(This->thread_sleepy);
1398 CloseHandle(This->hEventStateChanged);
1399 This->thread_lock.DebugInfo->Spare[0] = 0;
1400 DeleteCriticalSection(&This->thread_lock);
1401 CoTaskMemFree(This);
1407 static HRESULT PullPin_Standard_Request(PullPin *This, BOOL start)
1409 REFERENCE_TIME rtSampleStart;
1410 REFERENCE_TIME rtSampleStop;
1411 IMediaSample *sample = NULL;
1414 TRACE("Requesting sample!\n");
1417 This->rtNext = This->rtCurrent;
1419 if (This->rtNext >= This->rtStop)
1420 /* Last sample has already been queued, request nothing more */
1423 hr = IMemAllocator_GetBuffer(This->pAlloc, &sample, NULL, NULL, 0);
1427 rtSampleStart = This->rtNext;
1428 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample));
1429 if (rtSampleStop > This->rtStop)
1430 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), This->cbAlign));
1431 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
1433 This->rtCurrent = This->rtNext;
1434 This->rtNext = rtSampleStop;
1437 hr = IAsyncReader_Request(This->pReader, sample, 0);
1440 FIXME("Failed to queue sample : %08x\n", hr);
1445 static void CALLBACK PullPin_Flush(PullPin *This)
1447 IMediaSample *pSample;
1448 TRACE("Flushing!\n");
1450 EnterCriticalSection(This->pin.pCritSec);
1453 /* Flush outstanding samples */
1454 IAsyncReader_BeginFlush(This->pReader);
1459 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
1464 assert(!IMediaSample_GetActualDataLength(pSample));
1465 if (This->fnCustomRequest)
1466 This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1468 IMediaSample_Release(pSample);
1471 IAsyncReader_EndFlush(This->pReader);
1473 LeaveCriticalSection(This->pin.pCritSec);
1476 static void CALLBACK PullPin_Thread_Process(PullPin *This)
1479 IMediaSample * pSample = NULL;
1480 ALLOCATOR_PROPERTIES allocProps;
1482 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1484 This->cbAlign = allocProps.cbAlign;
1486 if (This->rtCurrent < This->rtStart)
1487 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
1491 if (This->rtCurrent >= This->rtStop)
1493 IPin_EndOfStream((IPin *)This);
1497 /* There is no sample in our buffer */
1498 if (!This->fnCustomRequest)
1499 hr = PullPin_Standard_Request(This, TRUE);
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);
1514 TRACE("Process sample\n");
1516 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1518 /* Calling fnCustomRequest is not specifically useful here: It can be handled inside fnSampleProc */
1519 if (pSample && !This->fnCustomRequest)
1520 hr = PullPin_Standard_Request(This, FALSE);
1522 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1523 if (SUCCEEDED(hr) || (This->fnCustomRequest && pSample))
1525 REFERENCE_TIME rtStart, rtStop;
1528 IMediaSample_GetTime(pSample, &rtStart, &rtStop);
1532 hr = This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1534 if (This->fnCustomRequest)
1538 if (This->rtCurrent == rtStart)
1543 /* Maybe it's transient? */
1545 /* rtNext = rtCurrent, because the next sample is already queued */
1546 else if (rtStop != This->rtCurrent && rtStop < This->rtStop)
1548 WARN("Position changed! rtStop: %u, rtCurrent: %u\n", (DWORD)BYTES_FROM_MEDIATIME(rtStop), (DWORD)BYTES_FROM_MEDIATIME(This->rtCurrent));
1549 PullPin_Flush(This);
1550 hr = PullPin_Standard_Request(This, TRUE);
1552 } while (rejected && (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback));
1556 /* FIXME: This is not well handled yet! */
1557 ERR("Processing error: %x\n", hr);
1562 IMediaSample_Release(pSample);
1565 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1567 /* Sample was rejected, and we are asked to terminate */
1570 IMediaSample_Release(pSample);
1573 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1574 * Flush remaining samples
1576 TRACE("Almost done..\n");
1579 This->fnDone(This->pin.pUserData);
1580 PullPin_Flush(This);
1582 TRACE("End: %08x, %d\n", hr, This->stop_playback);
1585 static void CALLBACK PullPin_Thread_Pause(PullPin *This)
1587 TRACE("(%p)->()\n", This);
1589 EnterCriticalSection(This->pin.pCritSec);
1591 This->state = Req_Sleepy;
1592 SetEvent(This->hEventStateChanged);
1594 LeaveCriticalSection(This->pin.pCritSec);
1597 static void CALLBACK PullPin_Thread_Stop(PullPin *This)
1599 TRACE("(%p)->()\n", This);
1601 EnterCriticalSection(This->pin.pCritSec);
1603 CloseHandle(This->hThread);
1604 This->hThread = NULL;
1605 SetEvent(This->hEventStateChanged);
1607 LeaveCriticalSection(This->pin.pCritSec);
1609 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1615 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1618 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1622 WaitForSingleObject(This->thread_sleepy, INFINITE);
1624 TRACE("State: %d\n", This->state);
1626 switch (This->state)
1628 case Req_Die: PullPin_Thread_Stop(This); break;
1629 case Req_Run: PullPin_Thread_Process(This); break;
1630 case Req_Pause: PullPin_Thread_Pause(This); break;
1631 case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
1632 default: ERR("Unknown state request: %d\n", This->state); break;
1637 HRESULT PullPin_InitProcessing(PullPin * This)
1641 TRACE("(%p)->()\n", This);
1643 /* if we are connected */
1648 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1649 EnterCriticalSection(This->pin.pCritSec);
1651 assert(!This->hThread);
1652 assert(This->state == Req_Die);
1653 assert(This->stop_playback);
1654 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1655 This->state = Req_Sleepy;
1657 /* AddRef the filter to make sure it and it's pins will be around
1658 * as long as the thread */
1659 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1662 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
1665 hr = HRESULT_FROM_WIN32(GetLastError());
1666 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1671 SetEvent(This->hEventStateChanged);
1672 /* If assert fails, that means a command was not processed before the thread previously terminated */
1674 LeaveCriticalSection(This->pin.pCritSec);
1677 TRACE(" -- %x\n", hr);
1682 HRESULT PullPin_StartProcessing(PullPin * This)
1684 /* if we are connected */
1685 TRACE("(%p)->()\n", This);
1688 assert(This->hThread);
1690 PullPin_WaitForStateChange(This, INFINITE);
1692 assert(This->state == Req_Sleepy);
1695 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1696 This->state = Req_Run;
1697 This->stop_playback = 0;
1698 ResetEvent(This->hEventStateChanged);
1699 SetEvent(This->thread_sleepy);
1705 HRESULT PullPin_PauseProcessing(PullPin * This)
1707 /* if we are connected */
1708 TRACE("(%p)->()\n", This);
1711 assert(This->hThread);
1713 PullPin_WaitForStateChange(This, INFINITE);
1715 EnterCriticalSection(This->pin.pCritSec);
1717 IAsyncReader_BeginFlush(This->pReader);
1719 assert(!This->stop_playback);
1720 assert(This->state == Req_Run|| This->state == Req_Sleepy);
1722 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1723 This->state = Req_Pause;
1724 This->stop_playback = 1;
1725 ResetEvent(This->hEventStateChanged);
1726 SetEvent(This->thread_sleepy);
1728 LeaveCriticalSection(This->pin.pCritSec);
1734 HRESULT PullPin_StopProcessing(PullPin * This)
1736 TRACE("(%p)->()\n", This);
1738 /* if we are alive */
1739 assert(This->hThread);
1741 PullPin_WaitForStateChange(This, INFINITE);
1743 assert(This->state == Req_Pause || This->state == Req_Sleepy);
1745 This->stop_playback = 1;
1746 This->state = Req_Die;
1747 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1748 ResetEvent(This->hEventStateChanged);
1749 SetEvent(This->thread_sleepy);
1753 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1755 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1760 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1762 FIXME("(%p)->() stub\n", iface);
1764 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1767 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1769 PullPin *This = (PullPin *)iface;
1770 TRACE("(%p)->()\n", This);
1772 EnterCriticalSection(This->pin.pCritSec);
1774 SendFurther( iface, deliver_beginflush, NULL, NULL );
1776 LeaveCriticalSection(This->pin.pCritSec);
1778 EnterCriticalSection(&This->thread_lock);
1780 PullPin_WaitForStateChange(This, INFINITE);
1782 if (This->hThread && !This->stop_playback)
1784 PullPin_PauseProcessing(This);
1785 PullPin_WaitForStateChange(This, INFINITE);
1788 LeaveCriticalSection(&This->thread_lock);
1790 EnterCriticalSection(This->pin.pCritSec);
1792 This->fnCleanProc(This->pin.pUserData);
1794 LeaveCriticalSection(This->pin.pCritSec);
1799 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1801 PullPin *This = (PullPin *)iface;
1803 TRACE("(%p)->()\n", iface);
1805 EnterCriticalSection(&This->thread_lock);
1808 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1810 if (This->stop_playback && state == State_Running)
1811 PullPin_StartProcessing(This);
1813 PullPin_WaitForStateChange(This, INFINITE);
1815 LeaveCriticalSection(&This->thread_lock);
1817 EnterCriticalSection(This->pin.pCritSec);
1818 SendFurther( iface, deliver_endflush, NULL, NULL );
1819 LeaveCriticalSection(This->pin.pCritSec);
1824 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
1827 PullPin *This = (PullPin *)iface;
1831 EnterCriticalSection(This->pin.pCritSec);
1833 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1834 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1836 if (This->pin.pConnectedTo)
1838 IPin_Release(This->pin.pConnectedTo);
1839 This->pin.pConnectedTo = NULL;
1845 LeaveCriticalSection(This->pin.pCritSec);
1850 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1852 newsegmentargs args;
1853 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1855 args.tStart = tStart;
1859 return SendFurther( iface, deliver_newsegment, &args, NULL );
1862 static const IPinVtbl PullPin_Vtbl =
1864 PullPin_QueryInterface,
1868 PullPin_ReceiveConnection,
1870 IPinImpl_ConnectedTo,
1871 IPinImpl_ConnectionMediaType,
1872 IPinImpl_QueryPinInfo,
1873 IPinImpl_QueryDirection,
1875 IPinImpl_QueryAccept,
1876 IPinImpl_EnumMediaTypes,
1877 IPinImpl_QueryInternalConnections,
1878 PullPin_EndOfStream,