quartz: Flux AsyncReader in PullPin_BeginFlush.
[wine/wine-kai.git] / dlls / quartz / pin.c
blobb641ff2c3ad492b4b50e2fd400ec9f975d0cd5db
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 );
125 if (!foundend)
126 hr = hr_return;
127 else if (fnEnd) {
128 HRESULT hr_local;
130 hr_local = fnEnd( from, arg );
131 hr_return = updatehres( hr_return, hr_local );
134 out:
135 if (pin_info.pFilter)
136 IBaseFilter_Release( pin_info.pFilter );
137 return hr;
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;
163 HRESULT hr;
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
179 * connected pin */
180 if (SUCCEEDED(hr))
182 This->pMemInputPin = NULL;
183 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
185 if (SUCCEEDED(hr))
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);
194 if (SUCCEEDED(hr))
196 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
200 if (SUCCEEDED(hr))
201 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
203 if (pMemAlloc)
204 IMemAllocator_Release(pMemAlloc);
207 /* break connection if we couldn't get the allocator */
208 if (FAILED(hr))
210 if (This->pMemInputPin)
211 IMemInputPin_Release(This->pMemInputPin);
212 This->pMemInputPin = NULL;
214 IPin_Disconnect(pReceivePin);
218 if (FAILED(hr))
220 IPin_Release(This->pin.pConnectedTo);
221 This->pin.pConnectedTo = NULL;
222 FreeMediaType(&This->pin.mtCurrent);
225 TRACE(" -- %x\n", hr);
226 return hr;
229 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData,
230 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
232 TRACE("\n");
234 /* Common attributes */
235 pPinImpl->pin.refCount = 1;
236 pPinImpl->pin.pConnectedTo = NULL;
237 pPinImpl->pin.fnQueryAccept = pQueryAccept;
238 pPinImpl->pin.pUserData = pUserData;
239 pPinImpl->pin.pCritSec = pCritSec;
240 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
241 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
243 /* Input pin attributes */
244 pPinImpl->fnSampleProc = pSampleProc;
245 pPinImpl->fnCleanProc = pCleanUp;
246 pPinImpl->pAllocator = NULL;
247 pPinImpl->tStart = 0;
248 pPinImpl->tStop = 0;
249 pPinImpl->dRate = 1.0;
250 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
251 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
252 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
254 return S_OK;
257 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
258 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
260 TRACE("\n");
262 /* Common attributes */
263 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
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 /* Output pin attributes */
273 pPinImpl->pMemInputPin = NULL;
274 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
275 if (props)
277 pPinImpl->allocProps = *props;
278 if (pPinImpl->allocProps.cbAlign == 0)
279 pPinImpl->allocProps.cbAlign = 1;
281 else
282 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
284 return S_OK;
287 HRESULT InputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
289 InputPin * pPinImpl;
291 *ppPin = NULL;
293 if (pPinInfo->dir != PINDIR_INPUT)
295 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
296 return E_INVALIDARG;
299 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
301 if (!pPinImpl)
302 return E_OUTOFMEMORY;
304 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, pPinImpl)))
306 *ppPin = (IPin *)pPinImpl;
307 return S_OK;
310 CoTaskMemFree(pPinImpl);
311 return E_FAIL;
314 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)
316 OutputPin * pPinImpl;
318 *ppPin = NULL;
320 if (pPinInfo->dir != PINDIR_OUTPUT)
322 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
323 return E_INVALIDARG;
326 assert(outputpin_size >= sizeof(OutputPin));
328 pPinImpl = CoTaskMemAlloc(outputpin_size);
330 if (!pPinImpl)
331 return E_OUTOFMEMORY;
333 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
335 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
336 return S_OK;
339 CoTaskMemFree(pPinImpl);
340 return E_FAIL;
343 /*** Common pin functions ***/
345 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
347 IPinImpl *This = (IPinImpl *)iface;
348 ULONG refCount = InterlockedIncrement(&This->refCount);
350 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
352 return refCount;
355 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
357 HRESULT hr;
358 IPinImpl *This = (IPinImpl *)iface;
360 TRACE("()\n");
362 EnterCriticalSection(This->pCritSec);
364 if (This->pConnectedTo)
366 IPin_Release(This->pConnectedTo);
367 This->pConnectedTo = NULL;
368 hr = S_OK;
370 else
371 hr = S_FALSE;
373 LeaveCriticalSection(This->pCritSec);
375 return hr;
378 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
380 HRESULT hr;
381 IPinImpl *This = (IPinImpl *)iface;
383 TRACE("(%p)\n", ppPin);
385 EnterCriticalSection(This->pCritSec);
387 if (This->pConnectedTo)
389 *ppPin = This->pConnectedTo;
390 IPin_AddRef(*ppPin);
391 hr = S_OK;
393 else
394 hr = VFW_E_NOT_CONNECTED;
396 LeaveCriticalSection(This->pCritSec);
398 return hr;
401 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
403 HRESULT hr;
404 IPinImpl *This = (IPinImpl *)iface;
406 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
408 EnterCriticalSection(This->pCritSec);
410 if (This->pConnectedTo)
412 CopyMediaType(pmt, &This->mtCurrent);
413 hr = S_OK;
415 else
417 ZeroMemory(pmt, sizeof(*pmt));
418 hr = VFW_E_NOT_CONNECTED;
421 LeaveCriticalSection(This->pCritSec);
423 return hr;
426 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
428 IPinImpl *This = (IPinImpl *)iface;
430 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
432 Copy_PinInfo(pInfo, &This->pinInfo);
433 IBaseFilter_AddRef(pInfo->pFilter);
435 return S_OK;
438 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
440 IPinImpl *This = (IPinImpl *)iface;
442 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
444 *pPinDir = This->pinInfo.dir;
446 return S_OK;
449 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
451 IPinImpl *This = (IPinImpl *)iface;
453 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
455 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
456 if (!*Id)
457 return E_OUTOFMEMORY;
459 strcpyW(*Id, This->pinInfo.achName);
461 return S_OK;
464 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
466 IPinImpl *This = (IPinImpl *)iface;
468 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
470 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
473 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
475 IPinImpl *This = (IPinImpl *)iface;
476 ENUMMEDIADETAILS emd;
478 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
480 /* override this method to allow enumeration of your types */
481 emd.cMediaTypes = 0;
482 emd.pMediaTypes = NULL;
484 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
487 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
489 IPinImpl *This = (IPinImpl *)iface;
491 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
493 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
496 /*** IPin implementation for an input pin ***/
498 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
500 InputPin *This = (InputPin *)iface;
502 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
504 *ppv = NULL;
506 if (IsEqualIID(riid, &IID_IUnknown))
507 *ppv = (LPVOID)iface;
508 else if (IsEqualIID(riid, &IID_IPin))
509 *ppv = (LPVOID)iface;
510 else if (IsEqualIID(riid, &IID_IMemInputPin))
511 *ppv = (LPVOID)&This->lpVtblMemInput;
512 else if (IsEqualIID(riid, &IID_IMediaSeeking))
514 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
517 if (*ppv)
519 IUnknown_AddRef((IUnknown *)(*ppv));
520 return S_OK;
523 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
525 return E_NOINTERFACE;
528 ULONG WINAPI InputPin_Release(IPin * iface)
530 InputPin *This = (InputPin *)iface;
531 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
533 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
535 if (!refCount)
537 FreeMediaType(&This->pin.mtCurrent);
538 if (This->pAllocator)
539 IMemAllocator_Release(This->pAllocator);
540 CoTaskMemFree(This);
541 return 0;
543 else
544 return refCount;
547 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
549 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
551 return E_UNEXPECTED;
555 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
557 InputPin *This = (InputPin *)iface;
558 PIN_DIRECTION pindirReceive;
559 HRESULT hr = S_OK;
561 TRACE("(%p, %p)\n", pReceivePin, pmt);
562 dump_AM_MEDIA_TYPE(pmt);
564 EnterCriticalSection(This->pin.pCritSec);
566 if (This->pin.pConnectedTo)
567 hr = VFW_E_ALREADY_CONNECTED;
569 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
570 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
571 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
573 if (SUCCEEDED(hr))
575 IPin_QueryDirection(pReceivePin, &pindirReceive);
577 if (pindirReceive != PINDIR_OUTPUT)
579 ERR("Can't connect from non-output pin\n");
580 hr = VFW_E_INVALID_DIRECTION;
584 if (SUCCEEDED(hr))
586 CopyMediaType(&This->pin.mtCurrent, pmt);
587 This->pin.pConnectedTo = pReceivePin;
588 IPin_AddRef(pReceivePin);
591 LeaveCriticalSection(This->pin.pCritSec);
593 return hr;
596 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
598 return IPin_EndOfStream( pin );
601 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
603 InputPin *This = (InputPin *)iface;
604 TRACE("(%p)\n", This);
606 This->end_of_stream = 1;
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 InputPin *This = (InputPin *)iface;
619 HRESULT hr;
620 TRACE("() semi-stub\n");
622 EnterCriticalSection(This->pin.pCritSec);
623 This->flushing = 1;
625 if (This->fnCleanProc)
626 This->fnCleanProc(This->pin.pUserData);
628 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
629 LeaveCriticalSection(This->pin.pCritSec);
631 return hr;
634 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
636 return IPin_EndFlush( pin );
639 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
641 InputPin *This = (InputPin *)iface;
642 HRESULT hr;
643 TRACE("(%p)\n", This);
645 EnterCriticalSection(This->pin.pCritSec);
646 This->flushing = 0;
648 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
649 LeaveCriticalSection(This->pin.pCritSec);
651 return hr;
654 typedef struct newsegmentargs
656 REFERENCE_TIME tStart, tStop;
657 double rate;
658 } newsegmentargs;
660 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
662 newsegmentargs *args = data;
663 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
666 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
668 InputPin *This = (InputPin *)iface;
669 newsegmentargs args;
671 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
673 args.tStart = This->tStart = tStart;
674 args.tStop = This->tStop = tStop;
675 args.rate = This->dRate = dRate;
677 return SendFurther( iface, deliver_newsegment, &args, NULL );
680 static const IPinVtbl InputPin_Vtbl =
682 InputPin_QueryInterface,
683 IPinImpl_AddRef,
684 InputPin_Release,
685 InputPin_Connect,
686 InputPin_ReceiveConnection,
687 IPinImpl_Disconnect,
688 IPinImpl_ConnectedTo,
689 IPinImpl_ConnectionMediaType,
690 IPinImpl_QueryPinInfo,
691 IPinImpl_QueryDirection,
692 IPinImpl_QueryId,
693 IPinImpl_QueryAccept,
694 IPinImpl_EnumMediaTypes,
695 IPinImpl_QueryInternalConnections,
696 InputPin_EndOfStream,
697 InputPin_BeginFlush,
698 InputPin_EndFlush,
699 InputPin_NewSegment
702 /*** IMemInputPin implementation ***/
704 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
706 InputPin *This = impl_from_IMemInputPin(iface);
708 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
711 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
713 InputPin *This = impl_from_IMemInputPin(iface);
715 return IPin_AddRef((IPin *)&This->pin);
718 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
720 InputPin *This = impl_from_IMemInputPin(iface);
722 return IPin_Release((IPin *)&This->pin);
725 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
727 InputPin *This = impl_from_IMemInputPin(iface);
729 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
731 *ppAllocator = This->pAllocator;
732 if (*ppAllocator)
733 IMemAllocator_AddRef(*ppAllocator);
735 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
738 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
740 InputPin *This = impl_from_IMemInputPin(iface);
742 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
744 if (This->pAllocator)
745 IMemAllocator_Release(This->pAllocator);
746 This->pAllocator = pAllocator;
747 if (This->pAllocator)
748 IMemAllocator_AddRef(This->pAllocator);
750 return S_OK;
753 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
755 InputPin *This = impl_from_IMemInputPin(iface);
757 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
759 /* override this method if you have any specific requirements */
761 return E_NOTIMPL;
764 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
766 InputPin *This = impl_from_IMemInputPin(iface);
767 HRESULT hr;
769 /* this trace commented out for performance reasons */
770 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
772 EnterCriticalSection(This->pin.pCritSec);
773 if (!This->end_of_stream && !This->flushing && !This->end_of_stream)
774 hr = This->fnSampleProc(This->pin.pUserData, pSample);
775 else
776 hr = S_FALSE;
777 LeaveCriticalSection(This->pin.pCritSec);
778 return hr;
781 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
783 HRESULT hr = S_OK;
784 InputPin *This = impl_from_IMemInputPin(iface);
786 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
788 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
790 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
791 if (hr != S_OK)
792 break;
795 return hr;
798 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
800 InputPin *This = impl_from_IMemInputPin(iface);
802 FIXME("(%p/%p)->()\n", This, iface);
804 /* FIXME: we should check whether any output pins will block */
806 return S_OK;
809 static const IMemInputPinVtbl MemInputPin_Vtbl =
811 MemInputPin_QueryInterface,
812 MemInputPin_AddRef,
813 MemInputPin_Release,
814 MemInputPin_GetAllocator,
815 MemInputPin_NotifyAllocator,
816 MemInputPin_GetAllocatorRequirements,
817 MemInputPin_Receive,
818 MemInputPin_ReceiveMultiple,
819 MemInputPin_ReceiveCanBlock
822 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
824 OutputPin *This = (OutputPin *)iface;
826 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
828 *ppv = NULL;
830 if (IsEqualIID(riid, &IID_IUnknown))
831 *ppv = (LPVOID)iface;
832 else if (IsEqualIID(riid, &IID_IPin))
833 *ppv = (LPVOID)iface;
834 else if (IsEqualIID(riid, &IID_IMediaSeeking))
836 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
839 if (*ppv)
841 IUnknown_AddRef((IUnknown *)(*ppv));
842 return S_OK;
845 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
847 return E_NOINTERFACE;
850 ULONG WINAPI OutputPin_Release(IPin * iface)
852 OutputPin *This = (OutputPin *)iface;
853 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
855 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
857 if (!refCount)
859 FreeMediaType(&This->pin.mtCurrent);
860 CoTaskMemFree(This);
861 return 0;
863 return refCount;
866 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
868 HRESULT hr;
869 OutputPin *This = (OutputPin *)iface;
871 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
872 dump_AM_MEDIA_TYPE(pmt);
874 /* If we try to connect to ourself, we will definitely deadlock.
875 * There are other cases where we could deadlock too, but this
876 * catches the obvious case */
877 assert(pReceivePin != iface);
879 EnterCriticalSection(This->pin.pCritSec);
881 /* if we have been a specific type to connect with, then we can either connect
882 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
883 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
884 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
885 else
887 /* negotiate media type */
889 IEnumMediaTypes * pEnumCandidates;
890 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
892 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
894 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
896 /* try this filter's media types first */
897 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
899 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
900 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
902 hr = S_OK;
903 CoTaskMemFree(pmtCandidate);
904 break;
906 CoTaskMemFree(pmtCandidate);
908 IEnumMediaTypes_Release(pEnumCandidates);
911 /* then try receiver filter's media types */
912 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
914 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
916 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
918 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
919 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
921 hr = S_OK;
922 CoTaskMemFree(pmtCandidate);
923 break;
925 CoTaskMemFree(pmtCandidate);
926 } /* while */
927 IEnumMediaTypes_Release(pEnumCandidates);
928 } /* if not found */
929 } /* if negotiate media type */
930 } /* if succeeded */
931 LeaveCriticalSection(This->pin.pCritSec);
933 TRACE(" -- %x\n", hr);
934 return hr;
937 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
939 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
941 return E_UNEXPECTED;
944 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
946 HRESULT hr;
947 OutputPin *This = (OutputPin *)iface;
949 TRACE("()\n");
951 EnterCriticalSection(This->pin.pCritSec);
953 if (This->pMemInputPin)
955 IMemInputPin_Release(This->pMemInputPin);
956 This->pMemInputPin = NULL;
958 if (This->pin.pConnectedTo)
960 IPin_Release(This->pin.pConnectedTo);
961 This->pin.pConnectedTo = NULL;
962 hr = S_OK;
964 else
965 hr = S_FALSE;
967 LeaveCriticalSection(This->pin.pCritSec);
969 return hr;
972 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
974 TRACE("()\n");
976 /* not supposed to do anything in an output pin */
978 return E_UNEXPECTED;
981 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
983 TRACE("(%p)->()\n", iface);
985 /* not supposed to do anything in an output pin */
987 return E_UNEXPECTED;
990 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
992 TRACE("(%p)->()\n", iface);
994 /* not supposed to do anything in an output pin */
996 return E_UNEXPECTED;
999 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1001 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1003 /* not supposed to do anything in an output pin */
1005 return E_UNEXPECTED;
1008 static const IPinVtbl OutputPin_Vtbl =
1010 OutputPin_QueryInterface,
1011 IPinImpl_AddRef,
1012 OutputPin_Release,
1013 OutputPin_Connect,
1014 OutputPin_ReceiveConnection,
1015 OutputPin_Disconnect,
1016 IPinImpl_ConnectedTo,
1017 IPinImpl_ConnectionMediaType,
1018 IPinImpl_QueryPinInfo,
1019 IPinImpl_QueryDirection,
1020 IPinImpl_QueryId,
1021 IPinImpl_QueryAccept,
1022 IPinImpl_EnumMediaTypes,
1023 IPinImpl_QueryInternalConnections,
1024 OutputPin_EndOfStream,
1025 OutputPin_BeginFlush,
1026 OutputPin_EndFlush,
1027 OutputPin_NewSegment
1030 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1032 HRESULT hr;
1034 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1036 EnterCriticalSection(This->pin.pCritSec);
1038 if (!This->pin.pConnectedTo)
1039 hr = VFW_E_NOT_CONNECTED;
1040 else
1042 IMemAllocator * pAlloc = NULL;
1044 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1046 if (SUCCEEDED(hr))
1047 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1049 if (SUCCEEDED(hr))
1050 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1052 if (pAlloc)
1053 IMemAllocator_Release(pAlloc);
1056 LeaveCriticalSection(This->pin.pCritSec);
1058 return hr;
1061 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1063 HRESULT hr = S_OK;
1064 IMemInputPin * pMemConnected = NULL;
1065 PIN_INFO pinInfo;
1067 EnterCriticalSection(This->pin.pCritSec);
1069 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1070 hr = VFW_E_NOT_CONNECTED;
1071 else
1073 /* we don't have the lock held when using This->pMemInputPin,
1074 * so we need to AddRef it to stop it being deleted while we are
1075 * using it. Same with its filter. */
1076 pMemConnected = This->pMemInputPin;
1077 IMemInputPin_AddRef(pMemConnected);
1078 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1081 LeaveCriticalSection(This->pin.pCritSec);
1083 if (SUCCEEDED(hr))
1085 /* NOTE: if we are in a critical section when Receive is called
1086 * then it causes some problems (most notably with the native Video
1087 * Renderer) if we are re-entered for whatever reason */
1088 hr = IMemInputPin_Receive(pMemConnected, pSample);
1090 /* If the filter's destroyed, tell upstream to stop sending data */
1091 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1092 hr = S_FALSE;
1094 if (pMemConnected)
1095 IMemInputPin_Release(pMemConnected);
1097 return hr;
1100 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1102 HRESULT hr;
1104 EnterCriticalSection(This->pin.pCritSec);
1106 if (!This->pin.pConnectedTo)
1107 hr = VFW_E_NOT_CONNECTED;
1108 else
1109 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1111 LeaveCriticalSection(This->pin.pCritSec);
1113 return hr;
1116 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1118 HRESULT hr;
1120 TRACE("(%p)->()\n", This);
1122 EnterCriticalSection(This->pin.pCritSec);
1124 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1125 hr = VFW_E_NOT_CONNECTED;
1126 else
1128 IMemAllocator * pAlloc = NULL;
1130 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1132 if (SUCCEEDED(hr))
1133 hr = IMemAllocator_Commit(pAlloc);
1135 if (pAlloc)
1136 IMemAllocator_Release(pAlloc);
1139 LeaveCriticalSection(This->pin.pCritSec);
1141 return hr;
1144 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1146 HRESULT hr;
1148 TRACE("(%p)->()\n", This);
1150 EnterCriticalSection(This->pin.pCritSec);
1152 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1153 hr = VFW_E_NOT_CONNECTED;
1154 else
1156 IMemAllocator * pAlloc = NULL;
1158 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1160 if (SUCCEEDED(hr))
1161 hr = IMemAllocator_Decommit(pAlloc);
1163 if (pAlloc)
1164 IMemAllocator_Release(pAlloc);
1166 if (SUCCEEDED(hr))
1167 hr = IPin_Disconnect(This->pin.pConnectedTo);
1170 LeaveCriticalSection(This->pin.pCritSec);
1172 return hr;
1176 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData,
1177 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1179 /* Common attributes */
1180 pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1181 pPinImpl->pin.refCount = 1;
1182 pPinImpl->pin.pConnectedTo = NULL;
1183 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1184 pPinImpl->pin.pUserData = pUserData;
1185 pPinImpl->pin.pCritSec = pCritSec;
1186 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1187 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1189 /* Input pin attributes */
1190 pPinImpl->fnSampleProc = pSampleProc;
1191 pPinImpl->fnCleanProc = pCleanUp;
1192 pPinImpl->fnPreConnect = NULL;
1193 pPinImpl->pAlloc = NULL;
1194 pPinImpl->pReader = NULL;
1195 pPinImpl->hThread = NULL;
1196 pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1198 pPinImpl->rtStart = 0;
1199 pPinImpl->rtCurrent = 0;
1200 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1201 pPinImpl->dRate = 1.0;
1202 pPinImpl->state = State_Stopped;
1204 InitializeCriticalSection(&pPinImpl->thread_lock);
1205 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1207 return S_OK;
1210 HRESULT PullPin_Construct(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1212 PullPin * pPinImpl;
1214 *ppPin = NULL;
1216 if (pPinInfo->dir != PINDIR_INPUT)
1218 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1219 return E_INVALIDARG;
1222 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1224 if (!pPinImpl)
1225 return E_OUTOFMEMORY;
1227 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, pPinImpl)))
1229 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1230 return S_OK;
1233 CoTaskMemFree(pPinImpl);
1234 return E_FAIL;
1237 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1239 PIN_DIRECTION pindirReceive;
1240 HRESULT hr = S_OK;
1241 PullPin *This = (PullPin *)iface;
1243 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1244 dump_AM_MEDIA_TYPE(pmt);
1246 EnterCriticalSection(This->pin.pCritSec);
1248 if (This->pin.pConnectedTo)
1249 hr = VFW_E_ALREADY_CONNECTED;
1251 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1252 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1253 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1255 if (SUCCEEDED(hr))
1257 IPin_QueryDirection(pReceivePin, &pindirReceive);
1259 if (pindirReceive != PINDIR_OUTPUT)
1261 ERR("Can't connect from non-output pin\n");
1262 hr = VFW_E_INVALID_DIRECTION;
1266 This->pReader = NULL;
1267 This->pAlloc = NULL;
1268 if (SUCCEEDED(hr))
1270 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1273 if (SUCCEEDED(hr))
1275 ALLOCATOR_PROPERTIES props;
1276 props.cBuffers = 3;
1277 props.cbBuffer = 64 * 1024; /* 64k bytes */
1278 props.cbAlign = 1;
1279 props.cbPrefix = 0;
1280 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1283 if (SUCCEEDED(hr) && This->fnPreConnect)
1285 hr = This->fnPreConnect(iface, pReceivePin);
1288 if (SUCCEEDED(hr))
1290 CopyMediaType(&This->pin.mtCurrent, pmt);
1291 This->pin.pConnectedTo = pReceivePin;
1292 IPin_AddRef(pReceivePin);
1294 else
1296 if (This->pReader)
1297 IAsyncReader_Release(This->pReader);
1298 This->pReader = NULL;
1299 if (This->pAlloc)
1300 IMemAllocator_Release(This->pAlloc);
1301 This->pAlloc = NULL;
1304 LeaveCriticalSection(This->pin.pCritSec);
1305 return hr;
1308 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1310 PullPin *This = (PullPin *)iface;
1312 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1314 *ppv = NULL;
1316 if (IsEqualIID(riid, &IID_IUnknown))
1317 *ppv = (LPVOID)iface;
1318 else if (IsEqualIID(riid, &IID_IPin))
1319 *ppv = (LPVOID)iface;
1320 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1322 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1325 if (*ppv)
1327 IUnknown_AddRef((IUnknown *)(*ppv));
1328 return S_OK;
1331 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1333 return E_NOINTERFACE;
1336 ULONG WINAPI PullPin_Release(IPin * iface)
1338 PullPin *This = (PullPin *)iface;
1339 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1341 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1343 if (!refCount)
1345 if(This->pAlloc)
1346 IMemAllocator_Release(This->pAlloc);
1347 if(This->pReader)
1348 IAsyncReader_Release(This->pReader);
1349 CloseHandle(This->hEventStateChanged);
1350 This->thread_lock.DebugInfo->Spare[0] = 0;
1351 DeleteCriticalSection(&This->thread_lock);
1352 CoTaskMemFree(This);
1353 return 0;
1355 return refCount;
1358 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1360 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1362 for (;;)
1363 SleepEx(INFINITE, TRUE);
1366 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1368 PullPin *This = (PullPin *)iface;
1369 HRESULT hr;
1371 ALLOCATOR_PROPERTIES allocProps;
1374 EnterCriticalSection(This->pin.pCritSec);
1375 SetEvent(This->hEventStateChanged);
1376 This->state = State_Running;
1377 LeaveCriticalSection(This->pin.pCritSec);
1379 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1381 if (This->rtCurrent < This->rtStart)
1382 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1384 TRACE("Start\n");
1386 if (This->rtCurrent >= This->rtStop)
1388 FIXME("Send an EndOfStream?\n");
1390 else do
1392 /* FIXME: to improve performance by quite a bit this should be changed
1393 * so that one sample is processed while one sample is fetched. However,
1394 * it is harder to debug so for the moment it will stay as it is */
1395 IMediaSample * pSample = NULL;
1396 REFERENCE_TIME rtSampleStart;
1397 REFERENCE_TIME rtSampleStop;
1398 DWORD_PTR dwUser;
1400 TRACE("Process sample\n");
1402 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1404 if (SUCCEEDED(hr))
1406 rtSampleStart = This->rtCurrent;
1407 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1408 if (rtSampleStop > This->rtStop)
1409 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1410 hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1411 This->rtCurrent = rtSampleStop;
1414 if (SUCCEEDED(hr))
1415 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1417 if (SUCCEEDED(hr))
1418 hr = IAsyncReader_WaitForNext(This->pReader, 1000, &pSample, &dwUser);
1420 if (SUCCEEDED(hr))
1422 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample));
1423 if (rtSampleStop > This->rtStop)
1424 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1425 hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1428 if (SUCCEEDED(hr))
1429 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1430 else
1431 ERR("Processing error: %x\n", hr);
1433 if (pSample)
1434 IMediaSample_Release(pSample);
1435 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1437 EnterCriticalSection(This->pin.pCritSec);
1438 This->state = State_Paused;
1439 LeaveCriticalSection(This->pin.pCritSec);
1440 TRACE("End\n");
1443 static void CALLBACK PullPin_Thread_Flush(ULONG_PTR iface)
1445 PullPin *This = (PullPin *)iface;
1446 IMediaSample *pSample;
1448 EnterCriticalSection(This->pin.pCritSec);
1449 if (This->pReader)
1451 /* Flush outstanding samples */
1452 IAsyncReader_BeginFlush(This->pReader);
1454 for (;;)
1456 IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1458 if (!pSample)
1459 break;
1460 IMediaSample_Release(pSample);
1463 IAsyncReader_EndFlush(This->pReader);
1465 LeaveCriticalSection(This->pin.pCritSec);
1468 static void CALLBACK PullPin_Thread_Pause(ULONG_PTR iface)
1470 PullPin *This = (PullPin *)iface;
1472 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1474 EnterCriticalSection(This->pin.pCritSec);
1476 This->state = State_Paused;
1477 SetEvent(This->hEventStateChanged);
1479 LeaveCriticalSection(This->pin.pCritSec);
1482 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1484 PullPin *This = (PullPin *)iface;
1486 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1488 EnterCriticalSection(This->pin.pCritSec);
1490 HRESULT hr;
1492 CloseHandle(This->hThread);
1493 This->hThread = NULL;
1494 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1495 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1497 SetEvent(This->hEventStateChanged);
1498 This->state = State_Stopped;
1500 LeaveCriticalSection(This->pin.pCritSec);
1502 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1504 CoUninitialize();
1505 ExitThread(0);
1508 HRESULT PullPin_InitProcessing(PullPin * This)
1510 HRESULT hr = S_OK;
1512 TRACE("(%p)->()\n", This);
1514 /* if we are connected */
1515 if (This->pAlloc)
1517 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1518 EnterCriticalSection(This->pin.pCritSec);
1519 if (This->state == State_Stopped)
1521 DWORD dwThreadId;
1522 assert(!This->hThread);
1524 /* AddRef the filter to make sure it and it's pins will be around
1525 * as long as the thread */
1526 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1528 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1529 if (!This->hThread)
1531 hr = HRESULT_FROM_WIN32(GetLastError());
1532 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1535 if (SUCCEEDED(hr))
1537 hr = IMemAllocator_Commit(This->pAlloc);
1538 This->state = State_Paused;
1539 SetEvent(This->hEventStateChanged);
1542 else assert(This->hThread);
1543 LeaveCriticalSection(This->pin.pCritSec);
1546 TRACE(" -- %x\n", hr);
1548 return hr;
1551 HRESULT PullPin_StartProcessing(PullPin * This)
1553 /* if we are connected */
1554 TRACE("(%p)->()\n", This);
1555 if(This->pAlloc)
1557 assert(This->hThread);
1559 PullPin_WaitForStateChange(This, INFINITE);
1560 ResetEvent(This->hEventStateChanged);
1561 This->stop_playback = 0;
1563 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1564 return HRESULT_FROM_WIN32(GetLastError());
1567 return S_OK;
1570 HRESULT PullPin_PauseProcessing(PullPin * This)
1572 /* if we are connected */
1573 TRACE("(%p)->()\n", This);
1574 if(This->pAlloc)
1576 assert(This->hThread);
1578 PullPin_WaitForStateChange(This, INFINITE);
1579 EnterCriticalSection(This->pin.pCritSec);
1580 This->stop_playback = 1;
1581 LeaveCriticalSection(This->pin.pCritSec);
1582 ResetEvent(This->hEventStateChanged);
1584 if (!QueueUserAPC(PullPin_Thread_Pause, This->hThread, (ULONG_PTR)This))
1585 return HRESULT_FROM_WIN32(GetLastError());
1588 return S_OK;
1591 HRESULT PullPin_StopProcessing(PullPin * This)
1593 TRACE("(%p)->()\n", This);
1595 /* if we are alive */
1596 if (This->hThread)
1598 PullPin_WaitForStateChange(This, INFINITE);
1600 This->stop_playback = 1;
1601 ResetEvent(This->hEventStateChanged);
1603 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1604 return HRESULT_FROM_WIN32(GetLastError());
1607 return S_OK;
1610 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1612 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1613 return S_FALSE;
1614 return S_OK;
1617 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1619 FIXME("(%p)->() stub\n", iface);
1621 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1624 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1626 PullPin *This = (PullPin *)iface;
1627 TRACE("(%p)->()\n", iface);
1629 EnterCriticalSection(This->pin.pCritSec);
1631 SendFurther( iface, deliver_beginflush, NULL, NULL );
1633 LeaveCriticalSection(This->pin.pCritSec);
1635 EnterCriticalSection(&This->thread_lock);
1637 if (This->state == State_Running)
1638 PullPin_PauseProcessing(This);
1640 PullPin_WaitForStateChange(This, INFINITE);
1642 /* Workaround: The file async reader only cancels io on current thread
1643 * only vista and newer have CancelIoEx, so try the thread context if available
1644 * Anyone has some documentation on NtCancelFileIoEx?
1646 if (This->hThread)
1647 QueueUserAPC(PullPin_Thread_Flush, This->hThread, (ULONG_PTR)This);
1648 else
1649 PullPin_Thread_Flush((ULONG_PTR)This);
1651 LeaveCriticalSection(&This->thread_lock);
1653 EnterCriticalSection(This->pin.pCritSec);
1655 This->fnCleanProc(This->pin.pUserData);
1657 LeaveCriticalSection(This->pin.pCritSec);
1659 return S_OK;
1662 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1664 PullPin *This = (PullPin *)iface;
1666 TRACE("(%p)->()\n", iface);
1668 EnterCriticalSection(&This->thread_lock);
1670 FILTER_STATE state;
1671 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1673 if (state == State_Running && This->state == State_Paused)
1674 PullPin_StartProcessing(This);
1676 PullPin_WaitForStateChange(This, INFINITE);
1678 LeaveCriticalSection(&This->thread_lock);
1680 EnterCriticalSection(This->pin.pCritSec);
1681 SendFurther( iface, deliver_endflush, NULL, NULL );
1682 LeaveCriticalSection(This->pin.pCritSec);
1684 return S_OK;
1687 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1689 newsegmentargs args;
1690 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1692 args.tStart = tStart;
1693 args.tStop = tStop;
1694 args.rate = dRate;
1696 return SendFurther( iface, deliver_newsegment, &args, NULL );
1699 static const IPinVtbl PullPin_Vtbl =
1701 PullPin_QueryInterface,
1702 IPinImpl_AddRef,
1703 PullPin_Release,
1704 InputPin_Connect,
1705 PullPin_ReceiveConnection,
1706 IPinImpl_Disconnect,
1707 IPinImpl_ConnectedTo,
1708 IPinImpl_ConnectionMediaType,
1709 IPinImpl_QueryPinInfo,
1710 IPinImpl_QueryDirection,
1711 IPinImpl_QueryId,
1712 IPinImpl_QueryAccept,
1713 IPinImpl_EnumMediaTypes,
1714 IPinImpl_QueryInternalConnections,
1715 PullPin_EndOfStream,
1716 PullPin_BeginFlush,
1717 PullPin_EndFlush,
1718 PullPin_NewSegment