quartz: Fix incorrect use of mtCurrent in transform filter.
[wine/wine64.git] / dlls / quartz / pin.c
blob0abeb1335eb3a716aaa0a0412862d80f12633ee4
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 else
125 hr = S_OK;
126 break;
130 if (!foundend)
131 hr = hr_return;
132 else if (fnEnd) {
133 HRESULT hr_local;
135 hr_local = fnEnd( from, arg );
136 hr_return = updatehres( hr_return, hr_local );
139 out:
140 if (pin_info.pFilter)
141 IBaseFilter_Release( pin_info.pFilter );
142 return hr;
145 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
147 return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
151 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
153 /* Tempting to just do a memcpy, but the name field is
154 128 characters long! We will probably never exceed 10
155 most of the time, so we are better off copying
156 each field manually */
157 strcpyW(pDest->achName, pSrc->achName);
158 pDest->dir = pSrc->dir;
159 pDest->pFilter = pSrc->pFilter;
162 /* Function called as a helper to IPin_Connect */
163 /* specific AM_MEDIA_TYPE - it cannot be NULL */
164 /* NOTE: not part of standard interface */
165 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
167 OutputPin *This = (OutputPin *)iface;
168 HRESULT hr;
169 IMemAllocator * pMemAlloc = NULL;
170 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
172 TRACE("(%p, %p)\n", pReceivePin, pmt);
173 dump_AM_MEDIA_TYPE(pmt);
175 /* FIXME: call queryacceptproc */
177 This->pin.pConnectedTo = pReceivePin;
178 IPin_AddRef(pReceivePin);
179 CopyMediaType(&This->pin.mtCurrent, pmt);
181 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
183 /* get the IMemInputPin interface we will use to deliver samples to the
184 * connected pin */
185 if (SUCCEEDED(hr))
187 This->pMemInputPin = NULL;
188 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
190 if (SUCCEEDED(hr) && !This->custom_allocator)
192 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
194 if (hr == VFW_E_NO_ALLOCATOR)
196 /* Input pin provides no allocator, use standard memory allocator */
197 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
199 if (SUCCEEDED(hr))
201 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
205 if (SUCCEEDED(hr))
206 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
208 if (pMemAlloc)
209 IMemAllocator_Release(pMemAlloc);
211 else if (SUCCEEDED(hr))
213 if (This->alloc)
215 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
217 else
218 hr = VFW_E_NO_ALLOCATOR;
221 /* break connection if we couldn't get the allocator */
222 if (FAILED(hr))
224 if (This->pMemInputPin)
225 IMemInputPin_Release(This->pMemInputPin);
226 This->pMemInputPin = NULL;
228 IPin_Disconnect(pReceivePin);
232 if (FAILED(hr))
234 IPin_Release(This->pin.pConnectedTo);
235 This->pin.pConnectedTo = NULL;
236 FreeMediaType(&This->pin.mtCurrent);
239 TRACE(" -- %x\n", hr);
240 return hr;
243 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData,
244 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, InputPin * pPinImpl)
246 TRACE("\n");
248 /* Common attributes */
249 pPinImpl->pin.refCount = 1;
250 pPinImpl->pin.pConnectedTo = NULL;
251 pPinImpl->pin.fnQueryAccept = pQueryAccept;
252 pPinImpl->pin.pUserData = pUserData;
253 pPinImpl->pin.pCritSec = pCritSec;
254 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
255 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
257 /* Input pin attributes */
258 pPinImpl->fnSampleProc = pSampleProc;
259 pPinImpl->fnCleanProc = pCleanUp;
260 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
261 if (pPinImpl->preferred_allocator)
262 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
263 pPinImpl->tStart = 0;
264 pPinImpl->tStop = 0;
265 pPinImpl->dRate = 1.0;
266 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
267 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
268 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
270 return S_OK;
273 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
274 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
276 TRACE("\n");
278 /* Common attributes */
279 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
280 pPinImpl->pin.refCount = 1;
281 pPinImpl->pin.pConnectedTo = NULL;
282 pPinImpl->pin.fnQueryAccept = pQueryAccept;
283 pPinImpl->pin.pUserData = pUserData;
284 pPinImpl->pin.pCritSec = pCritSec;
285 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
286 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
288 /* Output pin attributes */
289 pPinImpl->pMemInputPin = NULL;
290 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
291 /* If custom_allocator is set, you will need to specify an allocator
292 * in the alloc member of the struct before an output pin can connect
294 pPinImpl->custom_allocator = 0;
295 pPinImpl->alloc = NULL;
296 pPinImpl->readonly = FALSE;
297 if (props)
299 pPinImpl->allocProps = *props;
300 if (pPinImpl->allocProps.cbAlign == 0)
301 pPinImpl->allocProps.cbAlign = 1;
303 else
304 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
306 return S_OK;
309 HRESULT InputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
311 InputPin * pPinImpl;
313 *ppPin = NULL;
315 if (pPinInfo->dir != PINDIR_INPUT)
317 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
318 return E_INVALIDARG;
321 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
323 if (!pPinImpl)
324 return E_OUTOFMEMORY;
326 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, allocator, pPinImpl)))
328 *ppPin = (IPin *)pPinImpl;
329 return S_OK;
332 CoTaskMemFree(pPinImpl);
333 return E_FAIL;
336 HRESULT OutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, long outputpin_size, const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
338 OutputPin * pPinImpl;
340 *ppPin = NULL;
342 if (pPinInfo->dir != PINDIR_OUTPUT)
344 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
345 return E_INVALIDARG;
348 assert(outputpin_size >= sizeof(OutputPin));
350 pPinImpl = CoTaskMemAlloc(outputpin_size);
352 if (!pPinImpl)
353 return E_OUTOFMEMORY;
355 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
357 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
358 return S_OK;
361 CoTaskMemFree(pPinImpl);
362 return E_FAIL;
365 /*** Common pin functions ***/
367 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
369 IPinImpl *This = (IPinImpl *)iface;
370 ULONG refCount = InterlockedIncrement(&This->refCount);
372 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
374 return refCount;
377 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
379 HRESULT hr;
380 IPinImpl *This = (IPinImpl *)iface;
382 TRACE("()\n");
384 EnterCriticalSection(This->pCritSec);
386 if (This->pConnectedTo)
388 IPin_Release(This->pConnectedTo);
389 This->pConnectedTo = NULL;
390 hr = S_OK;
392 else
393 hr = S_FALSE;
395 LeaveCriticalSection(This->pCritSec);
397 return hr;
400 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
402 HRESULT hr;
403 IPinImpl *This = (IPinImpl *)iface;
405 TRACE("(%p)\n", ppPin);
407 EnterCriticalSection(This->pCritSec);
409 if (This->pConnectedTo)
411 *ppPin = This->pConnectedTo;
412 IPin_AddRef(*ppPin);
413 hr = S_OK;
415 else
417 hr = VFW_E_NOT_CONNECTED;
418 *ppPin = NULL;
421 LeaveCriticalSection(This->pCritSec);
423 return hr;
426 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
428 HRESULT hr;
429 IPinImpl *This = (IPinImpl *)iface;
431 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
433 EnterCriticalSection(This->pCritSec);
435 if (This->pConnectedTo)
437 CopyMediaType(pmt, &This->mtCurrent);
438 hr = S_OK;
440 else
442 ZeroMemory(pmt, sizeof(*pmt));
443 hr = VFW_E_NOT_CONNECTED;
446 LeaveCriticalSection(This->pCritSec);
448 return hr;
451 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
453 IPinImpl *This = (IPinImpl *)iface;
455 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
457 Copy_PinInfo(pInfo, &This->pinInfo);
458 IBaseFilter_AddRef(pInfo->pFilter);
460 return S_OK;
463 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
465 IPinImpl *This = (IPinImpl *)iface;
467 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
469 *pPinDir = This->pinInfo.dir;
471 return S_OK;
474 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
476 IPinImpl *This = (IPinImpl *)iface;
478 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
480 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
481 if (!*Id)
482 return E_OUTOFMEMORY;
484 strcpyW(*Id, This->pinInfo.achName);
486 return S_OK;
489 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
491 IPinImpl *This = (IPinImpl *)iface;
493 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
495 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
498 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
500 IPinImpl *This = (IPinImpl *)iface;
501 ENUMMEDIADETAILS emd;
503 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
505 /* override this method to allow enumeration of your types */
506 emd.cMediaTypes = 0;
507 emd.pMediaTypes = NULL;
509 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
512 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
514 IPinImpl *This = (IPinImpl *)iface;
516 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
518 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
521 /*** IPin implementation for an input pin ***/
523 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
525 InputPin *This = (InputPin *)iface;
527 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
529 *ppv = NULL;
531 if (IsEqualIID(riid, &IID_IUnknown))
532 *ppv = (LPVOID)iface;
533 else if (IsEqualIID(riid, &IID_IPin))
534 *ppv = (LPVOID)iface;
535 else if (IsEqualIID(riid, &IID_IMemInputPin))
536 *ppv = (LPVOID)&This->lpVtblMemInput;
537 else if (IsEqualIID(riid, &IID_IMediaSeeking))
539 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
542 if (*ppv)
544 IUnknown_AddRef((IUnknown *)(*ppv));
545 return S_OK;
548 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
550 return E_NOINTERFACE;
553 ULONG WINAPI InputPin_Release(IPin * iface)
555 InputPin *This = (InputPin *)iface;
556 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
558 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
560 if (!refCount)
562 FreeMediaType(&This->pin.mtCurrent);
563 if (This->pAllocator)
564 IMemAllocator_Release(This->pAllocator);
565 This->pAllocator = NULL;
566 This->pin.lpVtbl = NULL;
567 CoTaskMemFree(This);
568 return 0;
570 else
571 return refCount;
574 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
576 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
578 return E_UNEXPECTED;
582 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
584 InputPin *This = (InputPin *)iface;
585 PIN_DIRECTION pindirReceive;
586 HRESULT hr = S_OK;
588 TRACE("(%p, %p)\n", pReceivePin, pmt);
589 dump_AM_MEDIA_TYPE(pmt);
591 EnterCriticalSection(This->pin.pCritSec);
593 if (This->pin.pConnectedTo)
594 hr = VFW_E_ALREADY_CONNECTED;
596 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
597 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
598 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
600 if (SUCCEEDED(hr))
602 IPin_QueryDirection(pReceivePin, &pindirReceive);
604 if (pindirReceive != PINDIR_OUTPUT)
606 ERR("Can't connect from non-output pin\n");
607 hr = VFW_E_INVALID_DIRECTION;
611 if (SUCCEEDED(hr))
613 CopyMediaType(&This->pin.mtCurrent, pmt);
614 This->pin.pConnectedTo = pReceivePin;
615 IPin_AddRef(pReceivePin);
618 LeaveCriticalSection(This->pin.pCritSec);
620 return hr;
623 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
625 return IPin_EndOfStream( pin );
628 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
630 HRESULT hr = S_OK;
631 InputPin *This = (InputPin *)iface;
633 TRACE("(%p)\n", This);
635 EnterCriticalSection(This->pin.pCritSec);
636 if (This->flushing)
637 hr = S_FALSE;
638 else
639 This->end_of_stream = 1;
640 LeaveCriticalSection(This->pin.pCritSec);
642 if (hr == S_OK)
643 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
644 return hr;
647 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
649 return IPin_BeginFlush( pin );
652 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
654 InputPin *This = (InputPin *)iface;
655 HRESULT hr;
656 TRACE("() semi-stub\n");
658 EnterCriticalSection(This->pin.pCritSec);
659 This->flushing = 1;
661 if (This->fnCleanProc)
662 This->fnCleanProc(This->pin.pUserData);
664 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
665 LeaveCriticalSection(This->pin.pCritSec);
667 return hr;
670 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
672 return IPin_EndFlush( pin );
675 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
677 InputPin *This = (InputPin *)iface;
678 HRESULT hr;
679 TRACE("(%p)\n", This);
681 EnterCriticalSection(This->pin.pCritSec);
682 This->flushing = This->end_of_stream = 0;
684 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
685 LeaveCriticalSection(This->pin.pCritSec);
687 return hr;
690 typedef struct newsegmentargs
692 REFERENCE_TIME tStart, tStop;
693 double rate;
694 } newsegmentargs;
696 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
698 newsegmentargs *args = data;
699 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
702 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
704 InputPin *This = (InputPin *)iface;
705 newsegmentargs args;
707 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
709 args.tStart = This->tStart = tStart;
710 args.tStop = This->tStop = tStop;
711 args.rate = This->dRate = dRate;
713 return SendFurther( iface, deliver_newsegment, &args, NULL );
716 static const IPinVtbl InputPin_Vtbl =
718 InputPin_QueryInterface,
719 IPinImpl_AddRef,
720 InputPin_Release,
721 InputPin_Connect,
722 InputPin_ReceiveConnection,
723 IPinImpl_Disconnect,
724 IPinImpl_ConnectedTo,
725 IPinImpl_ConnectionMediaType,
726 IPinImpl_QueryPinInfo,
727 IPinImpl_QueryDirection,
728 IPinImpl_QueryId,
729 IPinImpl_QueryAccept,
730 IPinImpl_EnumMediaTypes,
731 IPinImpl_QueryInternalConnections,
732 InputPin_EndOfStream,
733 InputPin_BeginFlush,
734 InputPin_EndFlush,
735 InputPin_NewSegment
738 /*** IMemInputPin implementation ***/
740 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
742 InputPin *This = impl_from_IMemInputPin(iface);
744 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
747 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
749 InputPin *This = impl_from_IMemInputPin(iface);
751 return IPin_AddRef((IPin *)&This->pin);
754 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
756 InputPin *This = impl_from_IMemInputPin(iface);
758 return IPin_Release((IPin *)&This->pin);
761 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
763 InputPin *This = impl_from_IMemInputPin(iface);
765 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
767 *ppAllocator = This->pAllocator;
768 if (*ppAllocator)
769 IMemAllocator_AddRef(*ppAllocator);
771 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
774 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
776 InputPin *This = impl_from_IMemInputPin(iface);
778 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
780 if (bReadOnly)
781 FIXME("Read only flag not handled yet!\n");
783 /* FIXME: Should we release the allocator on disconnection? */
784 if (!pAllocator)
786 WARN("Null allocator\n");
787 return E_POINTER;
790 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
791 return E_FAIL;
793 if (This->pAllocator)
794 IMemAllocator_Release(This->pAllocator);
795 This->pAllocator = pAllocator;
796 if (This->pAllocator)
797 IMemAllocator_AddRef(This->pAllocator);
799 return S_OK;
802 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
804 InputPin *This = impl_from_IMemInputPin(iface);
806 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
808 /* override this method if you have any specific requirements */
810 return E_NOTIMPL;
813 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
815 InputPin *This = impl_from_IMemInputPin(iface);
816 HRESULT hr;
818 /* this trace commented out for performance reasons */
819 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
820 hr = This->fnSampleProc(This->pin.pUserData, pSample);
821 return hr;
824 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
826 HRESULT hr = S_OK;
827 InputPin *This = impl_from_IMemInputPin(iface);
829 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
831 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
833 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
834 if (hr != S_OK)
835 break;
838 return hr;
841 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
843 InputPin *This = impl_from_IMemInputPin(iface);
845 TRACE("(%p/%p)->()\n", This, iface);
847 return S_OK;
850 static const IMemInputPinVtbl MemInputPin_Vtbl =
852 MemInputPin_QueryInterface,
853 MemInputPin_AddRef,
854 MemInputPin_Release,
855 MemInputPin_GetAllocator,
856 MemInputPin_NotifyAllocator,
857 MemInputPin_GetAllocatorRequirements,
858 MemInputPin_Receive,
859 MemInputPin_ReceiveMultiple,
860 MemInputPin_ReceiveCanBlock
863 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
865 OutputPin *This = (OutputPin *)iface;
867 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
869 *ppv = NULL;
871 if (IsEqualIID(riid, &IID_IUnknown))
872 *ppv = (LPVOID)iface;
873 else if (IsEqualIID(riid, &IID_IPin))
874 *ppv = (LPVOID)iface;
875 else if (IsEqualIID(riid, &IID_IMediaSeeking))
877 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
880 if (*ppv)
882 IUnknown_AddRef((IUnknown *)(*ppv));
883 return S_OK;
886 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
888 return E_NOINTERFACE;
891 ULONG WINAPI OutputPin_Release(IPin * iface)
893 OutputPin *This = (OutputPin *)iface;
894 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
896 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
898 if (!refCount)
900 FreeMediaType(&This->pin.mtCurrent);
901 CoTaskMemFree(This);
902 return 0;
904 return refCount;
907 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
909 HRESULT hr;
910 OutputPin *This = (OutputPin *)iface;
912 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
913 dump_AM_MEDIA_TYPE(pmt);
915 /* If we try to connect to ourself, we will definitely deadlock.
916 * There are other cases where we could deadlock too, but this
917 * catches the obvious case */
918 assert(pReceivePin != iface);
920 EnterCriticalSection(This->pin.pCritSec);
922 /* if we have been a specific type to connect with, then we can either connect
923 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
924 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
925 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
926 else
928 /* negotiate media type */
930 IEnumMediaTypes * pEnumCandidates;
931 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
933 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
935 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
937 /* try this filter's media types first */
938 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
940 assert(pmtCandidate);
941 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype))
942 assert(pmtCandidate->pbFormat);
943 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
944 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
946 hr = S_OK;
947 CoTaskMemFree(pmtCandidate);
948 break;
950 DeleteMediaType(pmtCandidate);
951 pmtCandidate = NULL;
953 IEnumMediaTypes_Release(pEnumCandidates);
956 /* then try receiver filter's media types */
957 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
959 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
961 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
963 assert(pmtCandidate);
964 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype))
965 assert(pmtCandidate->pbFormat);
966 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
967 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
969 hr = S_OK;
970 CoTaskMemFree(pmtCandidate);
971 break;
973 DeleteMediaType(pmtCandidate);
974 pmtCandidate = NULL;
975 } /* while */
976 IEnumMediaTypes_Release(pEnumCandidates);
977 } /* if not found */
978 } /* if negotiate media type */
979 } /* if succeeded */
980 LeaveCriticalSection(This->pin.pCritSec);
982 TRACE(" -- %x\n", hr);
983 return hr;
986 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
988 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
990 return E_UNEXPECTED;
993 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
995 HRESULT hr;
996 OutputPin *This = (OutputPin *)iface;
998 TRACE("()\n");
1000 EnterCriticalSection(This->pin.pCritSec);
1002 if (This->pMemInputPin)
1004 IMemInputPin_Release(This->pMemInputPin);
1005 This->pMemInputPin = NULL;
1007 if (This->pin.pConnectedTo)
1009 IPin_Release(This->pin.pConnectedTo);
1010 This->pin.pConnectedTo = NULL;
1011 hr = S_OK;
1013 else
1014 hr = S_FALSE;
1016 LeaveCriticalSection(This->pin.pCritSec);
1018 return hr;
1021 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
1023 TRACE("()\n");
1025 /* not supposed to do anything in an output pin */
1027 return E_UNEXPECTED;
1030 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
1032 TRACE("(%p)->()\n", iface);
1034 /* not supposed to do anything in an output pin */
1036 return E_UNEXPECTED;
1039 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
1041 TRACE("(%p)->()\n", iface);
1043 /* not supposed to do anything in an output pin */
1045 return E_UNEXPECTED;
1048 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1050 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1052 /* not supposed to do anything in an output pin */
1054 return E_UNEXPECTED;
1057 static const IPinVtbl OutputPin_Vtbl =
1059 OutputPin_QueryInterface,
1060 IPinImpl_AddRef,
1061 OutputPin_Release,
1062 OutputPin_Connect,
1063 OutputPin_ReceiveConnection,
1064 OutputPin_Disconnect,
1065 IPinImpl_ConnectedTo,
1066 IPinImpl_ConnectionMediaType,
1067 IPinImpl_QueryPinInfo,
1068 IPinImpl_QueryDirection,
1069 IPinImpl_QueryId,
1070 IPinImpl_QueryAccept,
1071 IPinImpl_EnumMediaTypes,
1072 IPinImpl_QueryInternalConnections,
1073 OutputPin_EndOfStream,
1074 OutputPin_BeginFlush,
1075 OutputPin_EndFlush,
1076 OutputPin_NewSegment
1079 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1081 HRESULT hr;
1083 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1085 EnterCriticalSection(This->pin.pCritSec);
1087 if (!This->pin.pConnectedTo)
1088 hr = VFW_E_NOT_CONNECTED;
1089 else
1091 IMemAllocator * pAlloc = NULL;
1093 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1095 if (SUCCEEDED(hr))
1096 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1098 if (SUCCEEDED(hr))
1099 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1101 if (pAlloc)
1102 IMemAllocator_Release(pAlloc);
1105 LeaveCriticalSection(This->pin.pCritSec);
1107 return hr;
1110 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1112 HRESULT hr = S_OK;
1113 IMemInputPin * pMemConnected = NULL;
1114 PIN_INFO pinInfo;
1116 EnterCriticalSection(This->pin.pCritSec);
1118 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1119 hr = VFW_E_NOT_CONNECTED;
1120 else
1122 /* we don't have the lock held when using This->pMemInputPin,
1123 * so we need to AddRef it to stop it being deleted while we are
1124 * using it. Same with its filter. */
1125 pMemConnected = This->pMemInputPin;
1126 IMemInputPin_AddRef(pMemConnected);
1127 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1130 LeaveCriticalSection(This->pin.pCritSec);
1132 if (SUCCEEDED(hr))
1134 /* NOTE: if we are in a critical section when Receive is called
1135 * then it causes some problems (most notably with the native Video
1136 * Renderer) if we are re-entered for whatever reason */
1137 hr = IMemInputPin_Receive(pMemConnected, pSample);
1139 /* If the filter's destroyed, tell upstream to stop sending data */
1140 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1141 hr = S_FALSE;
1143 if (pMemConnected)
1144 IMemInputPin_Release(pMemConnected);
1146 return hr;
1149 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1151 HRESULT hr;
1153 EnterCriticalSection(This->pin.pCritSec);
1155 if (!This->pin.pConnectedTo)
1156 hr = VFW_E_NOT_CONNECTED;
1157 else
1158 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1160 LeaveCriticalSection(This->pin.pCritSec);
1162 return hr;
1165 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1167 HRESULT hr = S_OK;
1169 TRACE("(%p)->()\n", This);
1171 EnterCriticalSection(This->pin.pCritSec);
1173 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1174 hr = VFW_E_NOT_CONNECTED;
1175 else
1177 IMemAllocator * pAlloc = NULL;
1179 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1181 if (SUCCEEDED(hr))
1182 hr = IMemAllocator_Commit(pAlloc);
1184 if (pAlloc)
1185 IMemAllocator_Release(pAlloc);
1188 LeaveCriticalSection(This->pin.pCritSec);
1190 TRACE("--> %08x\n", hr);
1191 return hr;
1194 HRESULT OutputPin_DecommitAllocator(OutputPin * This)
1196 HRESULT hr = S_OK;
1198 TRACE("(%p)->()\n", This);
1200 EnterCriticalSection(This->pin.pCritSec);
1202 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1203 hr = VFW_E_NOT_CONNECTED;
1204 else
1206 IMemAllocator * pAlloc = NULL;
1208 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1210 if (SUCCEEDED(hr))
1211 hr = IMemAllocator_Decommit(pAlloc);
1213 if (pAlloc)
1214 IMemAllocator_Release(pAlloc);
1217 LeaveCriticalSection(This->pin.pCritSec);
1219 TRACE("--> %08x\n", hr);
1220 return hr;
1223 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1225 HRESULT hr;
1227 TRACE("(%p)->()\n", This);
1229 EnterCriticalSection(This->pin.pCritSec);
1231 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1232 hr = VFW_E_NOT_CONNECTED;
1233 else if (!This->custom_allocator)
1235 IMemAllocator * pAlloc = NULL;
1237 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1239 if (SUCCEEDED(hr))
1240 hr = IMemAllocator_Decommit(pAlloc);
1242 if (pAlloc)
1243 IMemAllocator_Release(pAlloc);
1245 if (SUCCEEDED(hr))
1246 hr = IPin_Disconnect(This->pin.pConnectedTo);
1248 else /* Kill the allocator! */
1250 hr = IPin_Disconnect(This->pin.pConnectedTo);
1252 IPin_Disconnect((IPin *)This);
1254 LeaveCriticalSection(This->pin.pCritSec);
1256 return hr;
1260 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData,
1261 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1263 /* Common attributes */
1264 pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1265 pPinImpl->pin.refCount = 1;
1266 pPinImpl->pin.pConnectedTo = NULL;
1267 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1268 pPinImpl->pin.pUserData = pUserData;
1269 pPinImpl->pin.pCritSec = pCritSec;
1270 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1271 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1273 /* Input pin attributes */
1274 pPinImpl->fnSampleProc = pSampleProc;
1275 pPinImpl->fnCleanProc = pCleanUp;
1276 pPinImpl->fnDone = pDone;
1277 pPinImpl->fnPreConnect = NULL;
1278 pPinImpl->pAlloc = NULL;
1279 pPinImpl->pReader = NULL;
1280 pPinImpl->hThread = NULL;
1281 pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1282 pPinImpl->thread_sleepy = CreateEventW(NULL, FALSE, FALSE, NULL);
1284 pPinImpl->rtStart = 0;
1285 pPinImpl->rtCurrent = 0;
1286 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1287 pPinImpl->dRate = 1.0;
1288 pPinImpl->state = Req_Die;
1289 pPinImpl->fnCustomRequest = pCustomRequest;
1290 pPinImpl->stop_playback = 1;
1292 InitializeCriticalSection(&pPinImpl->thread_lock);
1293 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1295 return S_OK;
1298 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)
1300 PullPin * pPinImpl;
1302 *ppPin = NULL;
1304 if (pPinInfo->dir != PINDIR_INPUT)
1306 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1307 return E_INVALIDARG;
1310 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1312 if (!pPinImpl)
1313 return E_OUTOFMEMORY;
1315 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
1317 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1318 return S_OK;
1321 CoTaskMemFree(pPinImpl);
1322 return E_FAIL;
1325 static HRESULT PullPin_InitProcessing(PullPin * This);
1327 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1329 PIN_DIRECTION pindirReceive;
1330 HRESULT hr = S_OK;
1331 PullPin *This = (PullPin *)iface;
1333 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1334 dump_AM_MEDIA_TYPE(pmt);
1336 EnterCriticalSection(This->pin.pCritSec);
1337 if (!This->pin.pConnectedTo)
1339 ALLOCATOR_PROPERTIES props;
1341 props.cBuffers = 3;
1342 props.cbBuffer = 64 * 1024; /* 64k bytes */
1343 props.cbAlign = 1;
1344 props.cbPrefix = 0;
1346 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1347 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1348 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1350 if (SUCCEEDED(hr))
1352 IPin_QueryDirection(pReceivePin, &pindirReceive);
1354 if (pindirReceive != PINDIR_OUTPUT)
1356 ERR("Can't connect from non-output pin\n");
1357 hr = VFW_E_INVALID_DIRECTION;
1361 This->pReader = NULL;
1362 This->pAlloc = NULL;
1363 if (SUCCEEDED(hr))
1365 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1368 if (SUCCEEDED(hr) && This->fnPreConnect)
1370 hr = This->fnPreConnect(iface, pReceivePin, &props);
1373 if (SUCCEEDED(hr))
1375 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1378 if (SUCCEEDED(hr))
1380 CopyMediaType(&This->pin.mtCurrent, pmt);
1381 This->pin.pConnectedTo = pReceivePin;
1382 IPin_AddRef(pReceivePin);
1383 hr = IMemAllocator_Commit(This->pAlloc);
1386 if (SUCCEEDED(hr))
1387 hr = PullPin_InitProcessing(This);
1389 if (FAILED(hr))
1391 if (This->pReader)
1392 IAsyncReader_Release(This->pReader);
1393 This->pReader = NULL;
1394 if (This->pAlloc)
1395 IMemAllocator_Release(This->pAlloc);
1396 This->pAlloc = NULL;
1399 else
1400 hr = VFW_E_ALREADY_CONNECTED;
1401 LeaveCriticalSection(This->pin.pCritSec);
1402 return hr;
1405 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1407 PullPin *This = (PullPin *)iface;
1409 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1411 *ppv = NULL;
1413 if (IsEqualIID(riid, &IID_IUnknown))
1414 *ppv = (LPVOID)iface;
1415 else if (IsEqualIID(riid, &IID_IPin))
1416 *ppv = (LPVOID)iface;
1417 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1419 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1422 if (*ppv)
1424 IUnknown_AddRef((IUnknown *)(*ppv));
1425 return S_OK;
1428 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1430 return E_NOINTERFACE;
1433 ULONG WINAPI PullPin_Release(IPin *iface)
1435 PullPin *This = (PullPin *)iface;
1436 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1438 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1440 if (!refCount)
1442 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1443 assert(!This->hThread);
1445 if(This->pAlloc)
1446 IMemAllocator_Release(This->pAlloc);
1447 if(This->pReader)
1448 IAsyncReader_Release(This->pReader);
1449 CloseHandle(This->thread_sleepy);
1450 CloseHandle(This->hEventStateChanged);
1451 This->thread_lock.DebugInfo->Spare[0] = 0;
1452 DeleteCriticalSection(&This->thread_lock);
1453 CoTaskMemFree(This);
1454 return 0;
1456 return refCount;
1459 static void CALLBACK PullPin_Flush(PullPin *This)
1461 IMediaSample *pSample;
1462 TRACE("Flushing!\n");
1464 if (This->pReader)
1466 /* Flush outstanding samples */
1467 IAsyncReader_BeginFlush(This->pReader);
1469 for (;;)
1471 DWORD_PTR dwUser;
1473 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
1475 if (!pSample)
1476 break;
1478 assert(!IMediaSample_GetActualDataLength(pSample));
1480 IMediaSample_Release(pSample);
1483 IAsyncReader_EndFlush(This->pReader);
1487 static void CALLBACK PullPin_Thread_Process(PullPin *This)
1489 HRESULT hr;
1490 IMediaSample * pSample = NULL;
1491 ALLOCATOR_PROPERTIES allocProps;
1493 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1495 This->cbAlign = allocProps.cbAlign;
1497 if (This->rtCurrent < This->rtStart)
1498 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
1500 TRACE("Start\n");
1502 if (This->rtCurrent >= This->rtStop)
1504 IPin_EndOfStream((IPin *)This);
1505 return;
1508 /* There is no sample in our buffer */
1509 hr = This->fnCustomRequest(This->pin.pUserData);
1511 if (FAILED(hr))
1512 ERR("Request error: %x\n", hr);
1514 EnterCriticalSection(This->pin.pCritSec);
1515 SetEvent(This->hEventStateChanged);
1516 LeaveCriticalSection(This->pin.pCritSec);
1518 if (SUCCEEDED(hr))
1521 DWORD_PTR dwUser;
1523 TRACE("Process sample\n");
1525 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1527 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1528 if (SUCCEEDED(hr))
1530 hr = This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1532 else
1534 /* FIXME: This is not well handled yet! */
1535 ERR("Processing error: %x\n", hr);
1538 if (pSample)
1540 IMediaSample_Release(pSample);
1541 pSample = NULL;
1543 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1545 /* Sample was rejected, and we are asked to terminate */
1546 if (pSample)
1548 IMediaSample_Release(pSample);
1551 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1552 * Flush remaining samples
1554 if (This->fnDone)
1555 This->fnDone(This->pin.pUserData);
1557 TRACE("End: %08x, %d\n", hr, This->stop_playback);
1560 static void CALLBACK PullPin_Thread_Pause(PullPin *This)
1562 EnterCriticalSection(This->pin.pCritSec);
1563 This->state = Req_Sleepy;
1564 SetEvent(This->hEventStateChanged);
1565 LeaveCriticalSection(This->pin.pCritSec);
1568 static void CALLBACK PullPin_Thread_Stop(PullPin *This)
1570 TRACE("(%p)->()\n", This);
1572 EnterCriticalSection(This->pin.pCritSec);
1574 CloseHandle(This->hThread);
1575 This->hThread = NULL;
1576 SetEvent(This->hEventStateChanged);
1578 LeaveCriticalSection(This->pin.pCritSec);
1580 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1582 CoUninitialize();
1583 ExitThread(0);
1586 static void CALLBACK PullPin_Thread_Flush(PullPin *This)
1588 PullPin_Flush(This);
1590 EnterCriticalSection(This->pin.pCritSec);
1591 This->state = Req_Sleepy;
1592 SetEvent(This->hEventStateChanged);
1593 LeaveCriticalSection(This->pin.pCritSec);
1596 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1598 PullPin *This = pv;
1599 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1601 PullPin_Flush(This);
1603 for (;;)
1605 WaitForSingleObject(This->thread_sleepy, INFINITE);
1607 TRACE("State: %d\n", This->state);
1609 switch (This->state)
1611 case Req_Die: PullPin_Thread_Stop(This); break;
1612 case Req_Run: PullPin_Thread_Process(This); break;
1613 case Req_Pause: PullPin_Thread_Pause(This); break;
1614 case Req_Flush: PullPin_Thread_Flush(This); break;
1615 case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
1616 default: ERR("Unknown state request: %d\n", This->state); break;
1621 static HRESULT PullPin_InitProcessing(PullPin * This)
1623 HRESULT hr = S_OK;
1625 TRACE("(%p)->()\n", This);
1627 /* if we are connected */
1628 if (This->pAlloc)
1630 DWORD dwThreadId;
1632 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1633 EnterCriticalSection(This->pin.pCritSec);
1635 assert(!This->hThread);
1636 assert(This->state == Req_Die);
1637 assert(This->stop_playback);
1638 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1639 This->state = Req_Sleepy;
1641 /* AddRef the filter to make sure it and it's pins will be around
1642 * as long as the thread */
1643 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1646 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
1647 if (!This->hThread)
1649 hr = HRESULT_FROM_WIN32(GetLastError());
1650 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1653 if (SUCCEEDED(hr))
1655 SetEvent(This->hEventStateChanged);
1656 /* If assert fails, that means a command was not processed before the thread previously terminated */
1658 LeaveCriticalSection(This->pin.pCritSec);
1661 TRACE(" -- %x\n", hr);
1663 return hr;
1666 HRESULT PullPin_StartProcessing(PullPin * This)
1668 /* if we are connected */
1669 TRACE("(%p)->()\n", This);
1670 if(This->pAlloc)
1672 assert(This->hThread);
1674 PullPin_WaitForStateChange(This, INFINITE);
1676 assert(This->state == Req_Sleepy);
1678 /* Wake up! */
1679 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1680 This->state = Req_Run;
1681 This->stop_playback = 0;
1682 ResetEvent(This->hEventStateChanged);
1683 SetEvent(This->thread_sleepy);
1686 return S_OK;
1689 HRESULT PullPin_PauseProcessing(PullPin * This)
1691 /* if we are connected */
1692 TRACE("(%p)->()\n", This);
1693 if(This->pAlloc)
1695 assert(This->hThread);
1697 PullPin_WaitForStateChange(This, INFINITE);
1699 EnterCriticalSection(This->pin.pCritSec);
1701 assert(!This->stop_playback);
1702 assert(This->state == Req_Run|| This->state == Req_Sleepy);
1704 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1705 This->state = Req_Pause;
1706 This->stop_playback = 1;
1707 ResetEvent(This->hEventStateChanged);
1708 SetEvent(This->thread_sleepy);
1710 LeaveCriticalSection(This->pin.pCritSec);
1713 return S_OK;
1716 static HRESULT PullPin_StopProcessing(PullPin * This)
1718 TRACE("(%p)->()\n", This);
1720 /* if we are alive */
1721 assert(This->hThread);
1723 PullPin_WaitForStateChange(This, INFINITE);
1725 assert(This->state == Req_Pause || This->state == Req_Sleepy);
1727 This->stop_playback = 1;
1728 This->state = Req_Die;
1729 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1730 ResetEvent(This->hEventStateChanged);
1731 SetEvent(This->thread_sleepy);
1732 return S_OK;
1735 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1737 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1738 return S_FALSE;
1739 return S_OK;
1742 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1744 FIXME("(%p)->() stub\n", iface);
1746 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1749 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1751 PullPin *This = (PullPin *)iface;
1752 TRACE("(%p)->()\n", This);
1754 EnterCriticalSection(This->pin.pCritSec);
1756 SendFurther( iface, deliver_beginflush, NULL, NULL );
1758 LeaveCriticalSection(This->pin.pCritSec);
1760 EnterCriticalSection(&This->thread_lock);
1762 if (This->pReader)
1763 IAsyncReader_BeginFlush(This->pReader);
1764 PullPin_WaitForStateChange(This, INFINITE);
1766 if (This->hThread && This->state == Req_Run)
1768 PullPin_PauseProcessing(This);
1769 PullPin_WaitForStateChange(This, INFINITE);
1772 This->state = Req_Flush;
1773 ResetEvent(This->hEventStateChanged);
1774 SetEvent(This->thread_sleepy);
1775 PullPin_WaitForStateChange(This, INFINITE);
1777 LeaveCriticalSection(&This->thread_lock);
1779 EnterCriticalSection(This->pin.pCritSec);
1781 This->fnCleanProc(This->pin.pUserData);
1783 LeaveCriticalSection(This->pin.pCritSec);
1785 return S_OK;
1788 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1790 PullPin *This = (PullPin *)iface;
1792 TRACE("(%p)->()\n", iface);
1794 /* Send further first: Else a race condition might terminate processing early */
1795 EnterCriticalSection(This->pin.pCritSec);
1796 SendFurther( iface, deliver_endflush, NULL, NULL );
1797 LeaveCriticalSection(This->pin.pCritSec);
1799 EnterCriticalSection(&This->thread_lock);
1801 FILTER_STATE state;
1802 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1804 if (state != State_Stopped)
1805 PullPin_StartProcessing(This);
1807 PullPin_WaitForStateChange(This, INFINITE);
1809 LeaveCriticalSection(&This->thread_lock);
1811 return S_OK;
1814 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
1816 HRESULT hr;
1817 PullPin *This = (PullPin *)iface;
1819 TRACE("()\n");
1821 EnterCriticalSection(This->pin.pCritSec);
1823 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1824 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1826 if (This->pin.pConnectedTo)
1828 IPin_Release(This->pin.pConnectedTo);
1829 This->pin.pConnectedTo = NULL;
1830 PullPin_StopProcessing(This);
1831 hr = S_OK;
1833 else
1834 hr = S_FALSE;
1836 LeaveCriticalSection(This->pin.pCritSec);
1838 return hr;
1841 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1843 newsegmentargs args;
1844 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1846 args.tStart = tStart;
1847 args.tStop = tStop;
1848 args.rate = dRate;
1850 return SendFurther( iface, deliver_newsegment, &args, NULL );
1853 static const IPinVtbl PullPin_Vtbl =
1855 PullPin_QueryInterface,
1856 IPinImpl_AddRef,
1857 PullPin_Release,
1858 InputPin_Connect,
1859 PullPin_ReceiveConnection,
1860 PullPin_Disconnect,
1861 IPinImpl_ConnectedTo,
1862 IPinImpl_ConnectionMediaType,
1863 IPinImpl_QueryPinInfo,
1864 IPinImpl_QueryDirection,
1865 IPinImpl_QueryId,
1866 IPinImpl_QueryAccept,
1867 IPinImpl_EnumMediaTypes,
1868 IPinImpl_QueryInternalConnections,
1869 PullPin_EndOfStream,
1870 PullPin_BeginFlush,
1871 PullPin_EndFlush,
1872 PullPin_NewSegment