quartz: Fix stop_playback used in pullpin.
[wine/wine-gecko.git] / dlls / quartz / pin.c
blob270a3c5bbf6a653b448732a5bef4f2b1f08f1094
1 /*
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"
22 #include "pin.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include <assert.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)
51 return original;
53 if (FAILED( new ) || original == S_OK)
54 return new;
56 return original;
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 )
68 PIN_INFO pin_info;
69 ULONG amount = 0;
70 HRESULT hr = S_OK;
71 HRESULT hr_return = S_OK;
72 IEnumPins *enumpins = NULL;
73 BOOL foundend = TRUE;
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");
81 hr = S_OK;
83 pin_info.pFilter = NULL;
84 hr = IPin_QueryPinInfo( from, &pin_info );
85 if (FAILED(hr))
86 goto out;
88 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
89 if (FAILED(hr))
90 goto out;
92 hr = IEnumPins_Reset( enumpins );
93 while (hr == S_OK) {
94 IPin *pin = NULL;
95 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
96 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
98 hr = IEnumPins_Reset( enumpins );
99 continue;
101 if (pin)
103 PIN_DIRECTION dir;
105 IPin_QueryDirection( pin, &dir );
106 if (dir != from_dir)
108 IPin *connected = NULL;
110 foundend = FALSE;
111 IPin_ConnectedTo( pin, &connected );
112 if (connected)
114 HRESULT hr_local;
116 hr_local = fnMiddle( connected, arg );
117 hr_return = updatehres( hr_return, hr_local );
118 IPin_Release(connected);
121 IPin_Release( pin );
123 } while (hr == S_OK);
124 if (!foundend)
125 hr = hr_return;
126 else if (fnEnd) {
127 HRESULT hr_local;
129 hr_local = fnEnd( from, arg );
130 hr_return = updatehres( hr_return, hr_local );
133 out:
134 if (pin_info.pFilter)
135 IBaseFilter_Release( pin_info.pFilter );
136 return hr;
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;
162 HRESULT hr;
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
178 * connected pin */
179 if (SUCCEEDED(hr))
181 This->pMemInputPin = NULL;
182 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
184 if (SUCCEEDED(hr))
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);
193 if (SUCCEEDED(hr))
195 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
199 if (SUCCEEDED(hr))
200 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
202 if (pMemAlloc)
203 IMemAllocator_Release(pMemAlloc);
206 /* break connection if we couldn't get the allocator */
207 if (FAILED(hr))
209 if (This->pMemInputPin)
210 IMemInputPin_Release(This->pMemInputPin);
211 This->pMemInputPin = NULL;
213 IPin_Disconnect(pReceivePin);
217 if (FAILED(hr))
219 IPin_Release(This->pin.pConnectedTo);
220 This->pin.pConnectedTo = NULL;
221 FreeMediaType(&This->pin.mtCurrent);
224 TRACE(" -- %x\n", hr);
225 return hr;
228 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
230 InputPin * pPinImpl;
232 *ppPin = NULL;
234 if (pPinInfo->dir != PINDIR_INPUT)
236 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
237 return E_INVALIDARG;
240 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
242 if (!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);
251 return S_OK;
254 CoTaskMemFree(pPinImpl);
255 return E_FAIL;
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)
261 TRACE("\n");
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;
276 pPinImpl->tStop = 0;
277 pPinImpl->dRate = 0;
279 return S_OK;
282 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
283 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
285 TRACE("\n");
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;
300 if (props)
302 pPinImpl->allocProps = *props;
303 if (pPinImpl->allocProps.cbAlign == 0)
304 pPinImpl->allocProps.cbAlign = 1;
306 else
307 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
309 return S_OK;
312 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
314 OutputPin * pPinImpl;
316 *ppPin = NULL;
318 if (pPinInfo->dir != PINDIR_OUTPUT)
320 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
321 return E_INVALIDARG;
324 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
326 if (!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);
334 return S_OK;
337 CoTaskMemFree(pPinImpl);
338 return E_FAIL;
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);
350 return refCount;
353 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
355 HRESULT hr;
356 IPinImpl *This = (IPinImpl *)iface;
358 TRACE("()\n");
360 EnterCriticalSection(This->pCritSec);
362 if (This->pConnectedTo)
364 IPin_Release(This->pConnectedTo);
365 This->pConnectedTo = NULL;
366 hr = S_OK;
368 else
369 hr = S_FALSE;
371 LeaveCriticalSection(This->pCritSec);
373 return hr;
376 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
378 HRESULT hr;
379 IPinImpl *This = (IPinImpl *)iface;
381 TRACE("(%p)\n", ppPin);
383 EnterCriticalSection(This->pCritSec);
385 if (This->pConnectedTo)
387 *ppPin = This->pConnectedTo;
388 IPin_AddRef(*ppPin);
389 hr = S_OK;
391 else
392 hr = VFW_E_NOT_CONNECTED;
394 LeaveCriticalSection(This->pCritSec);
396 return hr;
399 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
401 HRESULT hr;
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);
411 hr = S_OK;
413 else
415 ZeroMemory(pmt, sizeof(*pmt));
416 hr = VFW_E_NOT_CONNECTED;
419 LeaveCriticalSection(This->pCritSec);
421 return hr;
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);
433 return S_OK;
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;
444 return S_OK;
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));
454 if (!*Id)
455 return E_OUTOFMEMORY;
457 strcpyW(*Id, This->pinInfo.achName);
459 return S_OK;
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 */
479 emd.cMediaTypes = 0;
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);
502 *ppv = NULL;
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);
515 if (*ppv)
517 IUnknown_AddRef((IUnknown *)(*ppv));
518 return S_OK;
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);
533 if (!refCount)
535 FreeMediaType(&This->pin.mtCurrent);
536 if (This->pAllocator)
537 IMemAllocator_Release(This->pAllocator);
538 CoTaskMemFree(This);
539 return 0;
541 else
542 return refCount;
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);
549 return E_UNEXPECTED;
553 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
555 InputPin *This = (InputPin *)iface;
556 PIN_DIRECTION pindirReceive;
557 HRESULT hr = S_OK;
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? */
571 if (SUCCEEDED(hr))
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;
582 if (SUCCEEDED(hr))
584 CopyMediaType(&This->pin.mtCurrent, pmt);
585 This->pin.pConnectedTo = pReceivePin;
586 IPin_AddRef(pReceivePin);
589 LeaveCriticalSection(This->pin.pCritSec);
591 return hr;
594 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
596 return IPin_EndOfStream( pin );
599 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
601 FIXME("() stub\n");
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)
618 FIXME("() stub\n");
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)
631 FIXME("() stub\n");
633 /* TODO: Accept any samples again */
634 return SendFurther( iface, deliver_endflush, NULL, NULL );
637 typedef struct newsegmentargs
639 REFERENCE_TIME tStart, tStop;
640 double rate;
641 } newsegmentargs;
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;
652 newsegmentargs args;
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,
666 IPinImpl_AddRef,
667 InputPin_Release,
668 InputPin_Connect,
669 InputPin_ReceiveConnection,
670 IPinImpl_Disconnect,
671 IPinImpl_ConnectedTo,
672 IPinImpl_ConnectionMediaType,
673 IPinImpl_QueryPinInfo,
674 IPinImpl_QueryDirection,
675 IPinImpl_QueryId,
676 IPinImpl_QueryAccept,
677 IPinImpl_EnumMediaTypes,
678 IPinImpl_QueryInternalConnections,
679 InputPin_EndOfStream,
680 InputPin_BeginFlush,
681 InputPin_EndFlush,
682 InputPin_NewSegment
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;
715 if (*ppAllocator)
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);
733 return S_OK;
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 */
744 return E_NOTIMPL;
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)
759 HRESULT hr = S_OK;
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]);
767 if (hr != S_OK)
768 break;
771 return hr;
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 */
782 return S_OK;
785 static const IMemInputPinVtbl MemInputPin_Vtbl =
787 MemInputPin_QueryInterface,
788 MemInputPin_AddRef,
789 MemInputPin_Release,
790 MemInputPin_GetAllocator,
791 MemInputPin_NotifyAllocator,
792 MemInputPin_GetAllocatorRequirements,
793 MemInputPin_Receive,
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);
804 *ppv = NULL;
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);
815 if (*ppv)
817 IUnknown_AddRef((IUnknown *)(*ppv));
818 return S_OK;
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);
833 if (!refCount)
835 FreeMediaType(&This->pin.mtCurrent);
836 CoTaskMemFree(This);
837 return 0;
839 return refCount;
842 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
844 HRESULT hr;
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);
861 else
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))
878 hr = S_OK;
879 CoTaskMemFree(pmtCandidate);
880 break;
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))
897 hr = S_OK;
898 CoTaskMemFree(pmtCandidate);
899 break;
901 CoTaskMemFree(pmtCandidate);
902 } /* while */
903 IEnumMediaTypes_Release(pEnumCandidates);
904 } /* if not found */
905 } /* if negotiate media type */
906 } /* if succeeded */
907 LeaveCriticalSection(This->pin.pCritSec);
909 TRACE(" -- %x\n", hr);
910 return 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);
917 return E_UNEXPECTED;
920 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
922 HRESULT hr;
923 OutputPin *This = (OutputPin *)iface;
925 TRACE("()\n");
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;
938 hr = S_OK;
940 else
941 hr = S_FALSE;
943 LeaveCriticalSection(This->pin.pCritSec);
945 return hr;
948 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
950 TRACE("()\n");
952 /* not supposed to do anything in an output pin */
954 return E_UNEXPECTED;
957 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
959 TRACE("(%p)->()\n", iface);
961 /* not supposed to do anything in an output pin */
963 return E_UNEXPECTED;
966 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
968 TRACE("(%p)->()\n", iface);
970 /* not supposed to do anything in an output pin */
972 return E_UNEXPECTED;
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 */
981 return E_UNEXPECTED;
984 static const IPinVtbl OutputPin_Vtbl =
986 OutputPin_QueryInterface,
987 IPinImpl_AddRef,
988 OutputPin_Release,
989 OutputPin_Connect,
990 OutputPin_ReceiveConnection,
991 OutputPin_Disconnect,
992 IPinImpl_ConnectedTo,
993 IPinImpl_ConnectionMediaType,
994 IPinImpl_QueryPinInfo,
995 IPinImpl_QueryDirection,
996 IPinImpl_QueryId,
997 IPinImpl_QueryAccept,
998 IPinImpl_EnumMediaTypes,
999 IPinImpl_QueryInternalConnections,
1000 OutputPin_EndOfStream,
1001 OutputPin_BeginFlush,
1002 OutputPin_EndFlush,
1003 OutputPin_NewSegment
1006 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1008 HRESULT hr;
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;
1016 else
1018 IMemAllocator * pAlloc = NULL;
1020 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1022 if (SUCCEEDED(hr))
1023 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1025 if (SUCCEEDED(hr))
1026 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1028 if (pAlloc)
1029 IMemAllocator_Release(pAlloc);
1032 LeaveCriticalSection(This->pin.pCritSec);
1034 return hr;
1037 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1039 HRESULT hr = S_OK;
1040 IMemInputPin * pMemConnected = NULL;
1041 PIN_INFO pinInfo;
1043 EnterCriticalSection(This->pin.pCritSec);
1045 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1046 hr = VFW_E_NOT_CONNECTED;
1047 else
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);
1059 if (SUCCEEDED(hr))
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))
1068 hr = S_FALSE;
1070 if (pMemConnected)
1071 IMemInputPin_Release(pMemConnected);
1073 return hr;
1076 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1078 HRESULT hr;
1080 EnterCriticalSection(This->pin.pCritSec);
1082 if (!This->pin.pConnectedTo)
1083 hr = VFW_E_NOT_CONNECTED;
1084 else
1085 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1087 LeaveCriticalSection(This->pin.pCritSec);
1089 return hr;
1092 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1094 HRESULT hr;
1096 TRACE("(%p)->()\n", This);
1098 EnterCriticalSection(This->pin.pCritSec);
1100 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1101 hr = VFW_E_NOT_CONNECTED;
1102 else
1104 IMemAllocator * pAlloc = NULL;
1106 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1108 if (SUCCEEDED(hr))
1109 hr = IMemAllocator_Commit(pAlloc);
1111 if (pAlloc)
1112 IMemAllocator_Release(pAlloc);
1115 LeaveCriticalSection(This->pin.pCritSec);
1117 return hr;
1120 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1122 HRESULT hr;
1124 TRACE("(%p)->()\n", This);
1126 EnterCriticalSection(This->pin.pCritSec);
1128 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1129 hr = VFW_E_NOT_CONNECTED;
1130 else
1132 IMemAllocator * pAlloc = NULL;
1134 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1136 if (SUCCEEDED(hr))
1137 hr = IMemAllocator_Decommit(pAlloc);
1139 if (pAlloc)
1140 IMemAllocator_Release(pAlloc);
1142 if (SUCCEEDED(hr))
1143 hr = IPin_Disconnect(This->pin.pConnectedTo);
1146 LeaveCriticalSection(This->pin.pCritSec);
1148 return hr;
1152 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1154 PullPin * pPinImpl;
1156 *ppPin = NULL;
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));
1166 if (!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);
1174 return S_OK;
1177 CoTaskMemFree(pPinImpl);
1178 return E_FAIL;
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, TRUE, TRUE, NULL);
1200 pPinImpl->rtStart = 0;
1201 pPinImpl->rtCurrent = 0;
1202 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1203 pPinImpl->state = State_Stopped;
1205 return S_OK;
1208 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1210 PIN_DIRECTION pindirReceive;
1211 HRESULT hr = S_OK;
1212 PullPin *This = (PullPin *)iface;
1214 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1215 dump_AM_MEDIA_TYPE(pmt);
1217 EnterCriticalSection(This->pin.pCritSec);
1219 if (This->pin.pConnectedTo)
1220 hr = VFW_E_ALREADY_CONNECTED;
1222 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1223 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1224 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1226 if (SUCCEEDED(hr))
1228 IPin_QueryDirection(pReceivePin, &pindirReceive);
1230 if (pindirReceive != PINDIR_OUTPUT)
1232 ERR("Can't connect from non-output pin\n");
1233 hr = VFW_E_INVALID_DIRECTION;
1237 This->pReader = NULL;
1238 This->pAlloc = NULL;
1239 if (SUCCEEDED(hr))
1241 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1244 if (SUCCEEDED(hr))
1246 ALLOCATOR_PROPERTIES props;
1247 props.cBuffers = 3;
1248 props.cbBuffer = 64 * 1024; /* 64k bytes */
1249 props.cbAlign = 1;
1250 props.cbPrefix = 0;
1251 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1254 if (SUCCEEDED(hr) && This->fnPreConnect)
1256 hr = This->fnPreConnect(iface, pReceivePin);
1259 if (SUCCEEDED(hr))
1261 CopyMediaType(&This->pin.mtCurrent, pmt);
1262 This->pin.pConnectedTo = pReceivePin;
1263 IPin_AddRef(pReceivePin);
1265 else
1267 if (This->pReader)
1268 IAsyncReader_Release(This->pReader);
1269 This->pReader = NULL;
1270 if (This->pAlloc)
1271 IMemAllocator_Release(This->pAlloc);
1272 This->pAlloc = NULL;
1275 LeaveCriticalSection(This->pin.pCritSec);
1276 return hr;
1279 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1281 PullPin *This = (PullPin *)iface;
1283 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1285 *ppv = NULL;
1287 if (IsEqualIID(riid, &IID_IUnknown))
1288 *ppv = (LPVOID)iface;
1289 else if (IsEqualIID(riid, &IID_IPin))
1290 *ppv = (LPVOID)iface;
1291 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1293 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1296 if (*ppv)
1298 IUnknown_AddRef((IUnknown *)(*ppv));
1299 return S_OK;
1302 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1304 return E_NOINTERFACE;
1307 ULONG WINAPI PullPin_Release(IPin * iface)
1309 PullPin *This = (PullPin *)iface;
1310 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1312 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1314 if (!refCount)
1316 if(This->pAlloc)
1317 IMemAllocator_Release(This->pAlloc);
1318 if(This->pReader)
1319 IAsyncReader_Release(This->pReader);
1320 CloseHandle(This->hEventStateChanged);
1321 CoTaskMemFree(This);
1322 return 0;
1324 return refCount;
1327 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1329 for (;;)
1330 SleepEx(INFINITE, TRUE);
1333 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1335 PullPin *This = (PullPin *)iface;
1336 HRESULT hr;
1338 ALLOCATOR_PROPERTIES allocProps;
1340 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1342 EnterCriticalSection(This->pin.pCritSec);
1343 SetEvent(This->hEventStateChanged);
1344 This->state = State_Running;
1345 LeaveCriticalSection(This->pin.pCritSec);
1347 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1349 if (This->rtCurrent < This->rtStart)
1350 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1352 TRACE("Start\n");
1354 while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback)
1356 /* FIXME: to improve performance by quite a bit this should be changed
1357 * so that one sample is processed while one sample is fetched. However,
1358 * it is harder to debug so for the moment it will stay as it is */
1359 IMediaSample * pSample = NULL;
1360 REFERENCE_TIME rtSampleStart;
1361 REFERENCE_TIME rtSampleStop;
1362 DWORD_PTR dwUser;
1364 TRACE("Process sample\n");
1366 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1368 if (SUCCEEDED(hr))
1370 rtSampleStart = This->rtCurrent;
1371 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1372 if (rtSampleStop > This->rtStop)
1373 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1374 hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1375 This->rtCurrent = rtSampleStop;
1378 if (SUCCEEDED(hr))
1379 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1381 if (SUCCEEDED(hr))
1382 hr = IAsyncReader_WaitForNext(This->pReader, 1000, &pSample, &dwUser);
1384 if (SUCCEEDED(hr))
1386 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample));
1387 if (rtSampleStop > This->rtStop)
1388 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1389 hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1392 if (SUCCEEDED(hr))
1393 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1394 else
1395 ERR("Processing error: %x\n", hr);
1397 if (pSample)
1398 IMediaSample_Release(pSample);
1401 CoUninitialize();
1402 EnterCriticalSection(This->pin.pCritSec);
1403 This->state = State_Paused;
1404 LeaveCriticalSection(This->pin.pCritSec);
1405 TRACE("End\n");
1408 static void CALLBACK PullPin_Thread_Pause(ULONG_PTR iface)
1410 PullPin *This = (PullPin *)iface;
1412 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1414 EnterCriticalSection(This->pin.pCritSec);
1416 This->state = State_Paused;
1417 SetEvent(This->hEventStateChanged);
1419 LeaveCriticalSection(This->pin.pCritSec);
1423 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1425 PullPin *This = (PullPin *)iface;
1427 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1429 EnterCriticalSection(This->pin.pCritSec);
1431 HRESULT hr;
1433 CloseHandle(This->hThread);
1434 This->hThread = NULL;
1435 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1436 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1438 SetEvent(This->hEventStateChanged);
1439 This->state = State_Stopped;
1441 LeaveCriticalSection(This->pin.pCritSec);
1443 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1445 ExitThread(0);
1448 HRESULT PullPin_InitProcessing(PullPin * This)
1450 HRESULT hr = S_OK;
1452 TRACE("(%p)->()\n", This);
1454 /* if we are connected */
1455 if (This->pAlloc)
1457 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1458 EnterCriticalSection(This->pin.pCritSec);
1459 if (This->state == State_Stopped)
1461 DWORD dwThreadId;
1462 assert(!This->hThread);
1464 /* AddRef the filter to make sure it and it's pins will be around
1465 * as long as the thread */
1466 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1468 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1469 if (!This->hThread)
1471 hr = HRESULT_FROM_WIN32(GetLastError());
1472 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1475 if (SUCCEEDED(hr))
1477 hr = IMemAllocator_Commit(This->pAlloc);
1478 This->state = State_Paused;
1479 SetEvent(This->hEventStateChanged);
1482 else assert(This->hThread);
1483 LeaveCriticalSection(This->pin.pCritSec);
1486 TRACE(" -- %x\n", hr);
1488 return hr;
1491 HRESULT PullPin_StartProcessing(PullPin * This)
1493 /* if we are connected */
1494 TRACE("(%p)->()\n", This);
1495 if(This->pAlloc)
1497 assert(This->hThread);
1499 PullPin_WaitForStateChange(This, INFINITE);
1500 ResetEvent(This->hEventStateChanged);
1501 This->stop_playback = 0;
1503 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1504 return HRESULT_FROM_WIN32(GetLastError());
1507 return S_OK;
1510 HRESULT PullPin_PauseProcessing(PullPin * This)
1512 /* if we are connected */
1513 TRACE("(%p)->()\n", This);
1514 if(This->pAlloc)
1516 assert(This->hThread);
1518 PullPin_WaitForStateChange(This, INFINITE);
1519 EnterCriticalSection(This->pin.pCritSec);
1520 This->stop_playback = 1;
1521 LeaveCriticalSection(This->pin.pCritSec);
1522 ResetEvent(This->hEventStateChanged);
1524 if (!QueueUserAPC(PullPin_Thread_Pause, This->hThread, (ULONG_PTR)This))
1525 return HRESULT_FROM_WIN32(GetLastError());
1528 return S_OK;
1531 HRESULT PullPin_StopProcessing(PullPin * This)
1533 /* if we are connected */
1534 if (This->pAlloc && This->hThread)
1536 PullPin_WaitForStateChange(This, INFINITE);
1538 This->stop_playback = 1;
1539 ResetEvent(This->hEventStateChanged);
1541 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1542 return HRESULT_FROM_WIN32(GetLastError());
1545 return S_OK;
1548 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1550 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1551 return S_FALSE;
1552 return S_OK;
1555 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1557 FIXME("(%p)->() stub\n", iface);
1559 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1562 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1564 PullPin *This = (PullPin *)iface;
1565 FIXME("(%p)->() stub\n", iface);
1567 SendFurther( iface, deliver_beginflush, NULL, NULL );
1569 if (This->state == State_Running)
1570 return PullPin_PauseProcessing(This);
1571 return S_OK;
1574 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1576 FILTER_STATE state;
1577 PullPin *This = (PullPin *)iface;
1579 FIXME("(%p)->() stub\n", iface);
1580 SendFurther( iface, deliver_endflush, NULL, NULL );
1582 return IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1585 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1587 newsegmentargs args;
1588 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1590 args.tStart = tStart;
1591 args.tStop = tStop;
1592 args.rate = dRate;
1594 return SendFurther( iface, deliver_newsegment, &args, NULL );
1597 static const IPinVtbl PullPin_Vtbl =
1599 PullPin_QueryInterface,
1600 IPinImpl_AddRef,
1601 PullPin_Release,
1602 OutputPin_Connect,
1603 PullPin_ReceiveConnection,
1604 IPinImpl_Disconnect,
1605 IPinImpl_ConnectedTo,
1606 IPinImpl_ConnectionMediaType,
1607 IPinImpl_QueryPinInfo,
1608 IPinImpl_QueryDirection,
1609 IPinImpl_QueryId,
1610 IPinImpl_QueryAccept,
1611 IPinImpl_EnumMediaTypes,
1612 IPinImpl_QueryInternalConnections,
1613 PullPin_EndOfStream,
1614 PullPin_BeginFlush,
1615 PullPin_EndFlush,
1616 PullPin_NewSegment