shell32: Fix the Chinese translations.
[wine.git] / dlls / quartz / pin.c
blob8619776b4971c426f6d95366fa94630ae2262895
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) && !This->custom_allocator)
187 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
189 if (hr == VFW_E_NO_ALLOCATOR)
191 /* Input pin provides no allocator, use standard memory allocator */
192 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
194 if (SUCCEEDED(hr))
196 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
200 if (SUCCEEDED(hr))
201 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
203 if (pMemAlloc)
204 IMemAllocator_Release(pMemAlloc);
206 else if (SUCCEEDED(hr))
208 if (This->alloc)
210 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
212 else
213 hr = VFW_E_NO_ALLOCATOR;
216 /* break connection if we couldn't get the allocator */
217 if (FAILED(hr))
219 if (This->pMemInputPin)
220 IMemInputPin_Release(This->pMemInputPin);
221 This->pMemInputPin = NULL;
223 IPin_Disconnect(pReceivePin);
227 if (FAILED(hr))
229 IPin_Release(This->pin.pConnectedTo);
230 This->pin.pConnectedTo = NULL;
231 FreeMediaType(&This->pin.mtCurrent);
234 TRACE(" -- %x\n", hr);
235 return hr;
238 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData,
239 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
241 TRACE("\n");
243 /* Common attributes */
244 pPinImpl->pin.refCount = 1;
245 pPinImpl->pin.pConnectedTo = NULL;
246 pPinImpl->pin.fnQueryAccept = pQueryAccept;
247 pPinImpl->pin.pUserData = pUserData;
248 pPinImpl->pin.pCritSec = pCritSec;
249 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
250 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
252 /* Input pin attributes */
253 pPinImpl->fnSampleProc = pSampleProc;
254 pPinImpl->fnCleanProc = pCleanUp;
255 pPinImpl->pAllocator = NULL;
256 pPinImpl->tStart = 0;
257 pPinImpl->tStop = 0;
258 pPinImpl->dRate = 1.0;
259 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
260 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
261 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
263 return S_OK;
266 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
267 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
269 TRACE("\n");
271 /* Common attributes */
272 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
273 pPinImpl->pin.refCount = 1;
274 pPinImpl->pin.pConnectedTo = NULL;
275 pPinImpl->pin.fnQueryAccept = pQueryAccept;
276 pPinImpl->pin.pUserData = pUserData;
277 pPinImpl->pin.pCritSec = pCritSec;
278 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
279 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
281 /* Output pin attributes */
282 pPinImpl->pMemInputPin = NULL;
283 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
284 /* If custom_allocator is set, you will need to specify an allocator
285 * in the alloc member of the struct before an output pin can connect
287 pPinImpl->custom_allocator = 0;
288 pPinImpl->alloc = NULL;
289 pPinImpl->readonly = FALSE;
290 if (props)
292 pPinImpl->allocProps = *props;
293 if (pPinImpl->allocProps.cbAlign == 0)
294 pPinImpl->allocProps.cbAlign = 1;
296 else
297 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
299 return S_OK;
302 HRESULT InputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
304 InputPin * pPinImpl;
306 *ppPin = NULL;
308 if (pPinInfo->dir != PINDIR_INPUT)
310 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
311 return E_INVALIDARG;
314 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
316 if (!pPinImpl)
317 return E_OUTOFMEMORY;
319 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, pPinImpl)))
321 *ppPin = (IPin *)pPinImpl;
322 return S_OK;
325 CoTaskMemFree(pPinImpl);
326 return E_FAIL;
329 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)
331 OutputPin * pPinImpl;
333 *ppPin = NULL;
335 if (pPinInfo->dir != PINDIR_OUTPUT)
337 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
338 return E_INVALIDARG;
341 assert(outputpin_size >= sizeof(OutputPin));
343 pPinImpl = CoTaskMemAlloc(outputpin_size);
345 if (!pPinImpl)
346 return E_OUTOFMEMORY;
348 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
350 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
351 return S_OK;
354 CoTaskMemFree(pPinImpl);
355 return E_FAIL;
358 /*** Common pin functions ***/
360 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
362 IPinImpl *This = (IPinImpl *)iface;
363 ULONG refCount = InterlockedIncrement(&This->refCount);
365 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
367 return refCount;
370 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
372 HRESULT hr;
373 IPinImpl *This = (IPinImpl *)iface;
375 TRACE("()\n");
377 EnterCriticalSection(This->pCritSec);
379 if (This->pConnectedTo)
381 IPin_Release(This->pConnectedTo);
382 This->pConnectedTo = NULL;
383 hr = S_OK;
385 else
386 hr = S_FALSE;
388 LeaveCriticalSection(This->pCritSec);
390 return hr;
393 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
395 HRESULT hr;
396 IPinImpl *This = (IPinImpl *)iface;
398 TRACE("(%p)\n", ppPin);
400 EnterCriticalSection(This->pCritSec);
402 if (This->pConnectedTo)
404 *ppPin = This->pConnectedTo;
405 IPin_AddRef(*ppPin);
406 hr = S_OK;
408 else
409 hr = VFW_E_NOT_CONNECTED;
411 LeaveCriticalSection(This->pCritSec);
413 return hr;
416 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
418 HRESULT hr;
419 IPinImpl *This = (IPinImpl *)iface;
421 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
423 EnterCriticalSection(This->pCritSec);
425 if (This->pConnectedTo)
427 CopyMediaType(pmt, &This->mtCurrent);
428 hr = S_OK;
430 else
432 ZeroMemory(pmt, sizeof(*pmt));
433 hr = VFW_E_NOT_CONNECTED;
436 LeaveCriticalSection(This->pCritSec);
438 return hr;
441 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
443 IPinImpl *This = (IPinImpl *)iface;
445 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
447 Copy_PinInfo(pInfo, &This->pinInfo);
448 IBaseFilter_AddRef(pInfo->pFilter);
450 return S_OK;
453 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
455 IPinImpl *This = (IPinImpl *)iface;
457 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
459 *pPinDir = This->pinInfo.dir;
461 return S_OK;
464 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
466 IPinImpl *This = (IPinImpl *)iface;
468 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
470 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
471 if (!*Id)
472 return E_OUTOFMEMORY;
474 strcpyW(*Id, This->pinInfo.achName);
476 return S_OK;
479 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
481 IPinImpl *This = (IPinImpl *)iface;
483 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
485 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
488 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
490 IPinImpl *This = (IPinImpl *)iface;
491 ENUMMEDIADETAILS emd;
493 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
495 /* override this method to allow enumeration of your types */
496 emd.cMediaTypes = 0;
497 emd.pMediaTypes = NULL;
499 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
502 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
504 IPinImpl *This = (IPinImpl *)iface;
506 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
508 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
511 /*** IPin implementation for an input pin ***/
513 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
515 InputPin *This = (InputPin *)iface;
517 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
519 *ppv = NULL;
521 if (IsEqualIID(riid, &IID_IUnknown))
522 *ppv = (LPVOID)iface;
523 else if (IsEqualIID(riid, &IID_IPin))
524 *ppv = (LPVOID)iface;
525 else if (IsEqualIID(riid, &IID_IMemInputPin))
526 *ppv = (LPVOID)&This->lpVtblMemInput;
527 else if (IsEqualIID(riid, &IID_IMediaSeeking))
529 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
532 if (*ppv)
534 IUnknown_AddRef((IUnknown *)(*ppv));
535 return S_OK;
538 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
540 return E_NOINTERFACE;
543 ULONG WINAPI InputPin_Release(IPin * iface)
545 InputPin *This = (InputPin *)iface;
546 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
548 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
550 if (!refCount)
552 FreeMediaType(&This->pin.mtCurrent);
553 if (This->pAllocator)
554 IMemAllocator_Release(This->pAllocator);
555 CoTaskMemFree(This);
556 return 0;
558 else
559 return refCount;
562 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
564 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
566 return E_UNEXPECTED;
570 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
572 InputPin *This = (InputPin *)iface;
573 PIN_DIRECTION pindirReceive;
574 HRESULT hr = S_OK;
576 TRACE("(%p, %p)\n", pReceivePin, pmt);
577 dump_AM_MEDIA_TYPE(pmt);
579 EnterCriticalSection(This->pin.pCritSec);
581 if (This->pin.pConnectedTo)
582 hr = VFW_E_ALREADY_CONNECTED;
584 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
585 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
586 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
588 if (SUCCEEDED(hr))
590 IPin_QueryDirection(pReceivePin, &pindirReceive);
592 if (pindirReceive != PINDIR_OUTPUT)
594 ERR("Can't connect from non-output pin\n");
595 hr = VFW_E_INVALID_DIRECTION;
599 if (SUCCEEDED(hr))
601 CopyMediaType(&This->pin.mtCurrent, pmt);
602 This->pin.pConnectedTo = pReceivePin;
603 IPin_AddRef(pReceivePin);
606 LeaveCriticalSection(This->pin.pCritSec);
608 return hr;
611 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
613 return IPin_EndOfStream( pin );
616 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
618 InputPin *This = (InputPin *)iface;
619 TRACE("(%p)\n", This);
621 This->end_of_stream = 1;
623 return SendFurther( iface, deliver_endofstream, NULL, NULL );
626 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
628 return IPin_BeginFlush( pin );
631 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
633 InputPin *This = (InputPin *)iface;
634 HRESULT hr;
635 TRACE("() semi-stub\n");
637 EnterCriticalSection(This->pin.pCritSec);
638 This->flushing = 1;
640 if (This->fnCleanProc)
641 This->fnCleanProc(This->pin.pUserData);
643 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
644 LeaveCriticalSection(This->pin.pCritSec);
646 return hr;
649 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
651 return IPin_EndFlush( pin );
654 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
656 InputPin *This = (InputPin *)iface;
657 HRESULT hr;
658 TRACE("(%p)\n", This);
660 EnterCriticalSection(This->pin.pCritSec);
661 This->flushing = 0;
663 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
664 LeaveCriticalSection(This->pin.pCritSec);
666 return hr;
669 typedef struct newsegmentargs
671 REFERENCE_TIME tStart, tStop;
672 double rate;
673 } newsegmentargs;
675 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
677 newsegmentargs *args = data;
678 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
681 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
683 InputPin *This = (InputPin *)iface;
684 newsegmentargs args;
686 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
688 args.tStart = This->tStart = tStart;
689 args.tStop = This->tStop = tStop;
690 args.rate = This->dRate = dRate;
692 return SendFurther( iface, deliver_newsegment, &args, NULL );
695 static const IPinVtbl InputPin_Vtbl =
697 InputPin_QueryInterface,
698 IPinImpl_AddRef,
699 InputPin_Release,
700 InputPin_Connect,
701 InputPin_ReceiveConnection,
702 IPinImpl_Disconnect,
703 IPinImpl_ConnectedTo,
704 IPinImpl_ConnectionMediaType,
705 IPinImpl_QueryPinInfo,
706 IPinImpl_QueryDirection,
707 IPinImpl_QueryId,
708 IPinImpl_QueryAccept,
709 IPinImpl_EnumMediaTypes,
710 IPinImpl_QueryInternalConnections,
711 InputPin_EndOfStream,
712 InputPin_BeginFlush,
713 InputPin_EndFlush,
714 InputPin_NewSegment
717 /*** IMemInputPin implementation ***/
719 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
721 InputPin *This = impl_from_IMemInputPin(iface);
723 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
726 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
728 InputPin *This = impl_from_IMemInputPin(iface);
730 return IPin_AddRef((IPin *)&This->pin);
733 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
735 InputPin *This = impl_from_IMemInputPin(iface);
737 return IPin_Release((IPin *)&This->pin);
740 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
742 InputPin *This = impl_from_IMemInputPin(iface);
744 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
746 *ppAllocator = This->pAllocator;
747 if (*ppAllocator)
748 IMemAllocator_AddRef(*ppAllocator);
750 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
753 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
755 InputPin *This = impl_from_IMemInputPin(iface);
757 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
759 if (bReadOnly)
760 FIXME("Read only flag not handled yet!\n");
762 /* FIXME: Should we release the allocator on disconnection? */
763 if (!pAllocator)
765 WARN("Null allocator\n");
766 return E_POINTER;
769 if (This->pAllocator)
770 IMemAllocator_Release(This->pAllocator);
771 This->pAllocator = pAllocator;
772 if (This->pAllocator)
773 IMemAllocator_AddRef(This->pAllocator);
775 return S_OK;
778 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
780 InputPin *This = impl_from_IMemInputPin(iface);
782 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
784 /* override this method if you have any specific requirements */
786 return E_NOTIMPL;
789 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
791 InputPin *This = impl_from_IMemInputPin(iface);
792 HRESULT hr;
794 /* this trace commented out for performance reasons */
795 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
797 EnterCriticalSection(This->pin.pCritSec);
798 if (!This->end_of_stream && !This->flushing)
799 hr = This->fnSampleProc(This->pin.pUserData, pSample);
800 else
801 hr = S_FALSE;
802 LeaveCriticalSection(This->pin.pCritSec);
803 return hr;
806 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
808 HRESULT hr = S_OK;
809 InputPin *This = impl_from_IMemInputPin(iface);
811 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
813 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
815 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
816 if (hr != S_OK)
817 break;
820 return hr;
823 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
825 InputPin *This = impl_from_IMemInputPin(iface);
827 FIXME("(%p/%p)->()\n", This, iface);
829 /* FIXME: we should check whether any output pins will block */
831 return S_OK;
834 static const IMemInputPinVtbl MemInputPin_Vtbl =
836 MemInputPin_QueryInterface,
837 MemInputPin_AddRef,
838 MemInputPin_Release,
839 MemInputPin_GetAllocator,
840 MemInputPin_NotifyAllocator,
841 MemInputPin_GetAllocatorRequirements,
842 MemInputPin_Receive,
843 MemInputPin_ReceiveMultiple,
844 MemInputPin_ReceiveCanBlock
847 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
849 OutputPin *This = (OutputPin *)iface;
851 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
853 *ppv = NULL;
855 if (IsEqualIID(riid, &IID_IUnknown))
856 *ppv = (LPVOID)iface;
857 else if (IsEqualIID(riid, &IID_IPin))
858 *ppv = (LPVOID)iface;
859 else if (IsEqualIID(riid, &IID_IMediaSeeking))
861 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
864 if (*ppv)
866 IUnknown_AddRef((IUnknown *)(*ppv));
867 return S_OK;
870 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
872 return E_NOINTERFACE;
875 ULONG WINAPI OutputPin_Release(IPin * iface)
877 OutputPin *This = (OutputPin *)iface;
878 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
880 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
882 if (!refCount)
884 FreeMediaType(&This->pin.mtCurrent);
885 CoTaskMemFree(This);
886 return 0;
888 return refCount;
891 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
893 HRESULT hr;
894 OutputPin *This = (OutputPin *)iface;
896 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
897 dump_AM_MEDIA_TYPE(pmt);
899 /* If we try to connect to ourself, we will definitely deadlock.
900 * There are other cases where we could deadlock too, but this
901 * catches the obvious case */
902 assert(pReceivePin != iface);
904 EnterCriticalSection(This->pin.pCritSec);
906 /* if we have been a specific type to connect with, then we can either connect
907 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
908 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
909 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
910 else
912 /* negotiate media type */
914 IEnumMediaTypes * pEnumCandidates;
915 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
917 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
919 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
921 /* try this filter's media types first */
922 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
924 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
925 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
927 hr = S_OK;
928 CoTaskMemFree(pmtCandidate);
929 break;
931 CoTaskMemFree(pmtCandidate);
933 IEnumMediaTypes_Release(pEnumCandidates);
936 /* then try receiver filter's media types */
937 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
939 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
941 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
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 CoTaskMemFree(pmtCandidate);
951 } /* while */
952 IEnumMediaTypes_Release(pEnumCandidates);
953 } /* if not found */
954 } /* if negotiate media type */
955 } /* if succeeded */
956 LeaveCriticalSection(This->pin.pCritSec);
958 TRACE(" -- %x\n", hr);
959 return hr;
962 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
964 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
966 return E_UNEXPECTED;
969 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
971 HRESULT hr;
972 OutputPin *This = (OutputPin *)iface;
974 TRACE("()\n");
976 EnterCriticalSection(This->pin.pCritSec);
978 if (This->pMemInputPin)
980 IMemInputPin_Release(This->pMemInputPin);
981 This->pMemInputPin = NULL;
983 if (This->pin.pConnectedTo)
985 IPin_Release(This->pin.pConnectedTo);
986 This->pin.pConnectedTo = NULL;
987 hr = S_OK;
989 else
990 hr = S_FALSE;
992 LeaveCriticalSection(This->pin.pCritSec);
994 return hr;
997 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
999 TRACE("()\n");
1001 /* not supposed to do anything in an output pin */
1003 return E_UNEXPECTED;
1006 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
1008 TRACE("(%p)->()\n", iface);
1010 /* not supposed to do anything in an output pin */
1012 return E_UNEXPECTED;
1015 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
1017 TRACE("(%p)->()\n", iface);
1019 /* not supposed to do anything in an output pin */
1021 return E_UNEXPECTED;
1024 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1026 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1028 /* not supposed to do anything in an output pin */
1030 return E_UNEXPECTED;
1033 static const IPinVtbl OutputPin_Vtbl =
1035 OutputPin_QueryInterface,
1036 IPinImpl_AddRef,
1037 OutputPin_Release,
1038 OutputPin_Connect,
1039 OutputPin_ReceiveConnection,
1040 OutputPin_Disconnect,
1041 IPinImpl_ConnectedTo,
1042 IPinImpl_ConnectionMediaType,
1043 IPinImpl_QueryPinInfo,
1044 IPinImpl_QueryDirection,
1045 IPinImpl_QueryId,
1046 IPinImpl_QueryAccept,
1047 IPinImpl_EnumMediaTypes,
1048 IPinImpl_QueryInternalConnections,
1049 OutputPin_EndOfStream,
1050 OutputPin_BeginFlush,
1051 OutputPin_EndFlush,
1052 OutputPin_NewSegment
1055 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1057 HRESULT hr;
1059 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1061 EnterCriticalSection(This->pin.pCritSec);
1063 if (!This->pin.pConnectedTo)
1064 hr = VFW_E_NOT_CONNECTED;
1065 else
1067 IMemAllocator * pAlloc = NULL;
1069 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1071 if (SUCCEEDED(hr))
1072 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1074 if (SUCCEEDED(hr))
1075 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1077 if (pAlloc)
1078 IMemAllocator_Release(pAlloc);
1081 LeaveCriticalSection(This->pin.pCritSec);
1083 return hr;
1086 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1088 HRESULT hr = S_OK;
1089 IMemInputPin * pMemConnected = NULL;
1090 PIN_INFO pinInfo;
1092 EnterCriticalSection(This->pin.pCritSec);
1094 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1095 hr = VFW_E_NOT_CONNECTED;
1096 else
1098 /* we don't have the lock held when using This->pMemInputPin,
1099 * so we need to AddRef it to stop it being deleted while we are
1100 * using it. Same with its filter. */
1101 pMemConnected = This->pMemInputPin;
1102 IMemInputPin_AddRef(pMemConnected);
1103 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1106 LeaveCriticalSection(This->pin.pCritSec);
1108 if (SUCCEEDED(hr))
1110 /* NOTE: if we are in a critical section when Receive is called
1111 * then it causes some problems (most notably with the native Video
1112 * Renderer) if we are re-entered for whatever reason */
1113 hr = IMemInputPin_Receive(pMemConnected, pSample);
1115 /* If the filter's destroyed, tell upstream to stop sending data */
1116 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1117 hr = S_FALSE;
1119 if (pMemConnected)
1120 IMemInputPin_Release(pMemConnected);
1122 return hr;
1125 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1127 HRESULT hr;
1129 EnterCriticalSection(This->pin.pCritSec);
1131 if (!This->pin.pConnectedTo)
1132 hr = VFW_E_NOT_CONNECTED;
1133 else
1134 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1136 LeaveCriticalSection(This->pin.pCritSec);
1138 return hr;
1141 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1143 HRESULT hr = S_OK;
1145 TRACE("(%p)->()\n", This);
1147 EnterCriticalSection(This->pin.pCritSec);
1149 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1150 hr = VFW_E_NOT_CONNECTED;
1151 else
1153 IMemAllocator * pAlloc = NULL;
1155 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1157 if (SUCCEEDED(hr))
1158 hr = IMemAllocator_Commit(pAlloc);
1160 if (pAlloc)
1161 IMemAllocator_Release(pAlloc);
1164 LeaveCriticalSection(This->pin.pCritSec);
1166 TRACE("--> %08x\n", hr);
1167 return hr;
1170 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1172 HRESULT hr;
1174 TRACE("(%p)->()\n", This);
1176 EnterCriticalSection(This->pin.pCritSec);
1178 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1179 hr = VFW_E_NOT_CONNECTED;
1180 else if (!This->custom_allocator)
1182 IMemAllocator * pAlloc = NULL;
1184 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1186 if (SUCCEEDED(hr))
1187 hr = IMemAllocator_Decommit(pAlloc);
1189 if (pAlloc)
1190 IMemAllocator_Release(pAlloc);
1192 if (SUCCEEDED(hr))
1193 hr = IPin_Disconnect(This->pin.pConnectedTo);
1195 else /* Kill the allocator! */
1197 hr = IPin_Disconnect(This->pin.pConnectedTo);
1199 IPin_Disconnect((IPin *)This);
1201 LeaveCriticalSection(This->pin.pCritSec);
1203 return hr;
1207 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData,
1208 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1210 /* Common attributes */
1211 pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1212 pPinImpl->pin.refCount = 1;
1213 pPinImpl->pin.pConnectedTo = NULL;
1214 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1215 pPinImpl->pin.pUserData = pUserData;
1216 pPinImpl->pin.pCritSec = pCritSec;
1217 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1218 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1220 /* Input pin attributes */
1221 pPinImpl->fnSampleProc = pSampleProc;
1222 pPinImpl->fnCleanProc = pCleanUp;
1223 pPinImpl->fnDone = pDone;
1224 pPinImpl->fnPreConnect = NULL;
1225 pPinImpl->pAlloc = NULL;
1226 pPinImpl->pReader = NULL;
1227 pPinImpl->hThread = NULL;
1228 pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1229 pPinImpl->thread_sleepy = CreateEventW(NULL, FALSE, FALSE, NULL);
1231 pPinImpl->rtStart = 0;
1232 pPinImpl->rtCurrent = 0;
1233 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1234 pPinImpl->dRate = 1.0;
1235 pPinImpl->state = Req_Die;
1236 pPinImpl->fnCustomRequest = pCustomRequest;
1237 pPinImpl->stop_playback = 1;
1239 InitializeCriticalSection(&pPinImpl->thread_lock);
1240 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1242 return S_OK;
1245 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)
1247 PullPin * pPinImpl;
1249 *ppPin = NULL;
1251 if (pPinInfo->dir != PINDIR_INPUT)
1253 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1254 return E_INVALIDARG;
1257 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1259 if (!pPinImpl)
1260 return E_OUTOFMEMORY;
1262 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
1264 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1265 return S_OK;
1268 CoTaskMemFree(pPinImpl);
1269 return E_FAIL;
1272 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1274 PIN_DIRECTION pindirReceive;
1275 HRESULT hr = S_OK;
1276 PullPin *This = (PullPin *)iface;
1278 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1279 dump_AM_MEDIA_TYPE(pmt);
1281 EnterCriticalSection(This->pin.pCritSec);
1283 ALLOCATOR_PROPERTIES props;
1285 props.cBuffers = 3;
1286 props.cbBuffer = 64 * 1024; /* 64k bytes */
1287 props.cbAlign = 1;
1288 props.cbPrefix = 0;
1290 if (This->pin.pConnectedTo)
1291 hr = VFW_E_ALREADY_CONNECTED;
1293 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1294 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1295 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1297 if (SUCCEEDED(hr))
1299 IPin_QueryDirection(pReceivePin, &pindirReceive);
1301 if (pindirReceive != PINDIR_OUTPUT)
1303 ERR("Can't connect from non-output pin\n");
1304 hr = VFW_E_INVALID_DIRECTION;
1308 This->pReader = NULL;
1309 This->pAlloc = NULL;
1310 if (SUCCEEDED(hr))
1312 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1315 if (SUCCEEDED(hr) && This->fnPreConnect)
1317 hr = This->fnPreConnect(iface, pReceivePin, &props);
1320 if (SUCCEEDED(hr))
1322 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1325 if (SUCCEEDED(hr))
1327 CopyMediaType(&This->pin.mtCurrent, pmt);
1328 This->pin.pConnectedTo = pReceivePin;
1329 IPin_AddRef(pReceivePin);
1330 hr = IMemAllocator_Commit(This->pAlloc);
1334 if (FAILED(hr))
1336 if (This->pReader)
1337 IAsyncReader_Release(This->pReader);
1338 This->pReader = NULL;
1339 if (This->pAlloc)
1340 IMemAllocator_Release(This->pAlloc);
1341 This->pAlloc = NULL;
1344 LeaveCriticalSection(This->pin.pCritSec);
1345 return hr;
1348 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1350 PullPin *This = (PullPin *)iface;
1352 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1354 *ppv = NULL;
1356 if (IsEqualIID(riid, &IID_IUnknown))
1357 *ppv = (LPVOID)iface;
1358 else if (IsEqualIID(riid, &IID_IPin))
1359 *ppv = (LPVOID)iface;
1360 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1362 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1365 if (*ppv)
1367 IUnknown_AddRef((IUnknown *)(*ppv));
1368 return S_OK;
1371 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1373 return E_NOINTERFACE;
1376 ULONG WINAPI PullPin_Release(IPin *iface)
1378 PullPin *This = (PullPin *)iface;
1379 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1381 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1383 if (!refCount)
1385 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1386 assert(!This->hThread);
1388 if(This->pAlloc)
1389 IMemAllocator_Release(This->pAlloc);
1390 if(This->pReader)
1391 IAsyncReader_Release(This->pReader);
1392 CloseHandle(This->thread_sleepy);
1393 CloseHandle(This->hEventStateChanged);
1394 This->thread_lock.DebugInfo->Spare[0] = 0;
1395 DeleteCriticalSection(&This->thread_lock);
1396 CoTaskMemFree(This);
1397 return 0;
1399 return refCount;
1402 static HRESULT PullPin_Standard_Request(PullPin *This, BOOL start)
1404 REFERENCE_TIME rtSampleStart;
1405 REFERENCE_TIME rtSampleStop;
1406 IMediaSample *sample = NULL;
1407 HRESULT hr;
1409 TRACE("Requesting sample!\n");
1411 if (start)
1412 This->rtNext = This->rtCurrent;
1414 if (This->rtNext >= This->rtStop)
1415 /* Last sample has already been queued, request nothing more */
1416 return S_OK;
1418 hr = IMemAllocator_GetBuffer(This->pAlloc, &sample, NULL, NULL, 0);
1420 if (SUCCEEDED(hr))
1422 rtSampleStart = This->rtNext;
1423 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample));
1424 if (rtSampleStop > This->rtStop)
1425 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), This->cbAlign));
1426 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
1428 This->rtCurrent = This->rtNext;
1429 This->rtNext = rtSampleStop;
1431 if (SUCCEEDED(hr))
1432 hr = IAsyncReader_Request(This->pReader, sample, 0);
1434 if (FAILED(hr))
1435 FIXME("Failed to queue sample : %08x\n", hr);
1437 return hr;
1440 static void CALLBACK PullPin_Flush(PullPin *This)
1442 IMediaSample *pSample;
1443 TRACE("Flushing!\n");
1445 EnterCriticalSection(This->pin.pCritSec);
1446 if (This->pReader)
1448 /* Flush outstanding samples */
1449 IAsyncReader_BeginFlush(This->pReader);
1450 for (;;)
1452 DWORD_PTR dwUser;
1454 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
1456 if (!pSample)
1457 break;
1459 assert(!IMediaSample_GetActualDataLength(pSample));
1460 if (This->fnCustomRequest)
1461 This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1463 IMediaSample_Release(pSample);
1466 IAsyncReader_EndFlush(This->pReader);
1468 LeaveCriticalSection(This->pin.pCritSec);
1471 static void CALLBACK PullPin_Thread_Process(PullPin *This)
1473 HRESULT hr;
1474 IMediaSample * pSample = NULL;
1475 ALLOCATOR_PROPERTIES allocProps;
1477 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1479 This->cbAlign = allocProps.cbAlign;
1481 if (This->rtCurrent < This->rtStart)
1482 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
1484 TRACE("Start\n");
1486 if (This->rtCurrent >= This->rtStop)
1488 IPin_EndOfStream((IPin *)This);
1489 return;
1492 /* There is no sample in our buffer */
1493 if (!This->fnCustomRequest)
1494 hr = PullPin_Standard_Request(This, TRUE);
1495 else
1496 hr = This->fnCustomRequest(This->pin.pUserData);
1498 if (FAILED(hr))
1499 ERR("Request error: %x\n", hr);
1501 EnterCriticalSection(This->pin.pCritSec);
1502 SetEvent(This->hEventStateChanged);
1503 LeaveCriticalSection(This->pin.pCritSec);
1507 DWORD_PTR dwUser;
1509 TRACE("Process sample\n");
1511 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1513 /* Calling fnCustomRequest is not specifically useful here: It can be handled inside fnSampleProc */
1514 if (pSample && !This->fnCustomRequest)
1515 hr = PullPin_Standard_Request(This, FALSE);
1517 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1518 if (SUCCEEDED(hr) || (This->fnCustomRequest && pSample))
1520 REFERENCE_TIME rtStart, rtStop;
1521 BOOL rejected;
1523 IMediaSample_GetTime(pSample, &rtStart, &rtStop);
1527 hr = This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1529 if (This->fnCustomRequest)
1530 break;
1532 rejected = FALSE;
1533 if (This->rtCurrent == rtStart)
1535 rejected = TRUE;
1536 TRACE("DENIED!\n");
1537 Sleep(10);
1538 /* Maybe it's transient? */
1540 /* rtNext = rtCurrent, because the next sample is already queued */
1541 else if (rtStop != This->rtCurrent && rtStop < This->rtStop)
1543 WARN("Position changed! rtStop: %u, rtCurrent: %u\n", (DWORD)BYTES_FROM_MEDIATIME(rtStop), (DWORD)BYTES_FROM_MEDIATIME(This->rtCurrent));
1544 PullPin_Flush(This);
1545 hr = PullPin_Standard_Request(This, TRUE);
1547 } while (rejected && (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback));
1549 else
1551 /* FIXME: This is not well handled yet! */
1552 ERR("Processing error: %x\n", hr);
1555 if (pSample)
1557 IMediaSample_Release(pSample);
1558 pSample = NULL;
1560 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1562 /* Sample was rejected, and we are asked to terminate */
1563 if (pSample)
1565 IMediaSample_Release(pSample);
1568 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1569 * Flush remaining samples
1571 TRACE("Almost done..\n");
1573 if (This->fnDone)
1574 This->fnDone(This->pin.pUserData);
1575 PullPin_Flush(This);
1577 TRACE("End: %08x, %d\n", hr, This->stop_playback);
1580 static void CALLBACK PullPin_Thread_Pause(PullPin *This)
1582 TRACE("(%p)->()\n", This);
1584 EnterCriticalSection(This->pin.pCritSec);
1586 This->state = Req_Sleepy;
1587 SetEvent(This->hEventStateChanged);
1589 LeaveCriticalSection(This->pin.pCritSec);
1592 static void CALLBACK PullPin_Thread_Stop(PullPin *This)
1594 TRACE("(%p)->()\n", This);
1596 EnterCriticalSection(This->pin.pCritSec);
1598 CloseHandle(This->hThread);
1599 This->hThread = NULL;
1600 SetEvent(This->hEventStateChanged);
1602 LeaveCriticalSection(This->pin.pCritSec);
1604 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1606 CoUninitialize();
1607 ExitThread(0);
1610 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1612 PullPin *This = pv;
1613 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1615 for (;;)
1617 WaitForSingleObject(This->thread_sleepy, INFINITE);
1619 TRACE("State: %d\n", This->state);
1621 switch (This->state)
1623 case Req_Die: PullPin_Thread_Stop(This); break;
1624 case Req_Run: PullPin_Thread_Process(This); break;
1625 case Req_Pause: PullPin_Thread_Pause(This); break;
1626 case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
1627 default: ERR("Unknown state request: %d\n", This->state); break;
1632 HRESULT PullPin_InitProcessing(PullPin * This)
1634 HRESULT hr = S_OK;
1636 TRACE("(%p)->()\n", This);
1638 /* if we are connected */
1639 if (This->pAlloc)
1641 DWORD dwThreadId;
1643 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1644 EnterCriticalSection(This->pin.pCritSec);
1646 assert(!This->hThread);
1647 assert(This->state == Req_Die);
1648 assert(This->stop_playback);
1649 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1650 This->state = Req_Sleepy;
1652 /* AddRef the filter to make sure it and it's pins will be around
1653 * as long as the thread */
1654 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1657 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
1658 if (!This->hThread)
1660 hr = HRESULT_FROM_WIN32(GetLastError());
1661 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1664 if (SUCCEEDED(hr))
1666 SetEvent(This->hEventStateChanged);
1667 /* If assert fails, that means a command was not processed before the thread previously terminated */
1669 LeaveCriticalSection(This->pin.pCritSec);
1672 TRACE(" -- %x\n", hr);
1674 return hr;
1677 HRESULT PullPin_StartProcessing(PullPin * This)
1679 /* if we are connected */
1680 TRACE("(%p)->()\n", This);
1681 if(This->pAlloc)
1683 assert(This->hThread);
1685 PullPin_WaitForStateChange(This, INFINITE);
1687 assert(This->state == Req_Sleepy);
1689 /* Wake up! */
1690 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1691 This->state = Req_Run;
1692 This->stop_playback = 0;
1693 ResetEvent(This->hEventStateChanged);
1694 SetEvent(This->thread_sleepy);
1697 return S_OK;
1700 HRESULT PullPin_PauseProcessing(PullPin * This)
1702 /* if we are connected */
1703 TRACE("(%p)->()\n", This);
1704 if(This->pAlloc)
1706 assert(This->hThread);
1708 PullPin_WaitForStateChange(This, INFINITE);
1710 EnterCriticalSection(This->pin.pCritSec);
1711 /* Faster! */
1712 IAsyncReader_BeginFlush(This->pReader);
1714 assert(!This->stop_playback);
1715 assert(This->state == Req_Run|| This->state == Req_Sleepy);
1717 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1718 This->state = Req_Pause;
1719 This->stop_playback = 1;
1720 ResetEvent(This->hEventStateChanged);
1721 SetEvent(This->thread_sleepy);
1723 LeaveCriticalSection(This->pin.pCritSec);
1726 return S_OK;
1729 HRESULT PullPin_StopProcessing(PullPin * This)
1731 TRACE("(%p)->()\n", This);
1733 /* if we are alive */
1734 assert(This->hThread);
1736 PullPin_WaitForStateChange(This, INFINITE);
1738 assert(This->state == Req_Pause || This->state == Req_Sleepy);
1740 This->stop_playback = 1;
1741 This->state = Req_Die;
1742 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1743 ResetEvent(This->hEventStateChanged);
1744 SetEvent(This->thread_sleepy);
1745 return S_OK;
1748 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1750 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1751 return S_FALSE;
1752 return S_OK;
1755 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1757 FIXME("(%p)->() stub\n", iface);
1759 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1762 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1764 PullPin *This = (PullPin *)iface;
1765 TRACE("(%p)->()\n", This);
1767 EnterCriticalSection(This->pin.pCritSec);
1769 SendFurther( iface, deliver_beginflush, NULL, NULL );
1771 LeaveCriticalSection(This->pin.pCritSec);
1773 EnterCriticalSection(&This->thread_lock);
1775 PullPin_WaitForStateChange(This, INFINITE);
1777 if (This->hThread && !This->stop_playback)
1779 PullPin_PauseProcessing(This);
1780 PullPin_WaitForStateChange(This, INFINITE);
1783 LeaveCriticalSection(&This->thread_lock);
1785 EnterCriticalSection(This->pin.pCritSec);
1787 This->fnCleanProc(This->pin.pUserData);
1789 LeaveCriticalSection(This->pin.pCritSec);
1791 return S_OK;
1794 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1796 PullPin *This = (PullPin *)iface;
1798 TRACE("(%p)->()\n", iface);
1800 EnterCriticalSection(&This->thread_lock);
1802 FILTER_STATE state;
1803 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1805 if (This->stop_playback && state == State_Running)
1806 PullPin_StartProcessing(This);
1808 PullPin_WaitForStateChange(This, INFINITE);
1810 LeaveCriticalSection(&This->thread_lock);
1812 EnterCriticalSection(This->pin.pCritSec);
1813 SendFurther( iface, deliver_endflush, NULL, NULL );
1814 LeaveCriticalSection(This->pin.pCritSec);
1816 return S_OK;
1819 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
1821 HRESULT hr;
1822 PullPin *This = (PullPin *)iface;
1824 TRACE("()\n");
1826 EnterCriticalSection(This->pin.pCritSec);
1828 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1829 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1831 if (This->pin.pConnectedTo)
1833 IPin_Release(This->pin.pConnectedTo);
1834 This->pin.pConnectedTo = NULL;
1835 hr = S_OK;
1837 else
1838 hr = S_FALSE;
1840 LeaveCriticalSection(This->pin.pCritSec);
1842 return hr;
1845 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1847 newsegmentargs args;
1848 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1850 args.tStart = tStart;
1851 args.tStop = tStop;
1852 args.rate = dRate;
1854 return SendFurther( iface, deliver_newsegment, &args, NULL );
1857 static const IPinVtbl PullPin_Vtbl =
1859 PullPin_QueryInterface,
1860 IPinImpl_AddRef,
1861 PullPin_Release,
1862 InputPin_Connect,
1863 PullPin_ReceiveConnection,
1864 PullPin_Disconnect,
1865 IPinImpl_ConnectedTo,
1866 IPinImpl_ConnectionMediaType,
1867 IPinImpl_QueryPinInfo,
1868 IPinImpl_QueryDirection,
1869 IPinImpl_QueryId,
1870 IPinImpl_QueryAccept,
1871 IPinImpl_EnumMediaTypes,
1872 IPinImpl_QueryInternalConnections,
1873 PullPin_EndOfStream,
1874 PullPin_BeginFlush,
1875 PullPin_EndFlush,
1876 PullPin_NewSegment