dpwsockx: Implementation of SPInit
[wine/gsoc_dplay.git] / dlls / quartz / pin.c
blob2e24d520667dfa71e0d5e15530dd49f19ae12b2c
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;
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 /*** Common pin functions ***/
159 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
161 IPinImpl *This = (IPinImpl *)iface;
162 ULONG refCount = InterlockedIncrement(&This->refCount);
164 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
166 return refCount;
169 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
171 HRESULT hr;
172 IPinImpl *This = (IPinImpl *)iface;
174 TRACE("()\n");
176 EnterCriticalSection(This->pCritSec);
178 if (This->pConnectedTo)
180 IPin_Release(This->pConnectedTo);
181 This->pConnectedTo = NULL;
182 FreeMediaType(&This->mtCurrent);
183 ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
184 hr = S_OK;
186 else
187 hr = S_FALSE;
189 LeaveCriticalSection(This->pCritSec);
191 return hr;
194 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
196 HRESULT hr;
197 IPinImpl *This = (IPinImpl *)iface;
199 TRACE("(%p)\n", ppPin);
201 EnterCriticalSection(This->pCritSec);
203 if (This->pConnectedTo)
205 *ppPin = This->pConnectedTo;
206 IPin_AddRef(*ppPin);
207 hr = S_OK;
209 else
211 hr = VFW_E_NOT_CONNECTED;
212 *ppPin = NULL;
215 LeaveCriticalSection(This->pCritSec);
217 return hr;
220 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
222 HRESULT hr;
223 IPinImpl *This = (IPinImpl *)iface;
225 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
227 EnterCriticalSection(This->pCritSec);
229 if (This->pConnectedTo)
231 CopyMediaType(pmt, &This->mtCurrent);
232 hr = S_OK;
234 else
236 ZeroMemory(pmt, sizeof(*pmt));
237 hr = VFW_E_NOT_CONNECTED;
240 LeaveCriticalSection(This->pCritSec);
242 return hr;
245 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
247 IPinImpl *This = (IPinImpl *)iface;
249 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
251 Copy_PinInfo(pInfo, &This->pinInfo);
252 IBaseFilter_AddRef(pInfo->pFilter);
254 return S_OK;
257 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
259 IPinImpl *This = (IPinImpl *)iface;
261 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
263 *pPinDir = This->pinInfo.dir;
265 return S_OK;
268 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
270 IPinImpl *This = (IPinImpl *)iface;
272 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
274 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
275 if (!*Id)
276 return E_OUTOFMEMORY;
278 strcpyW(*Id, This->pinInfo.achName);
280 return S_OK;
283 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
285 IPinImpl *This = (IPinImpl *)iface;
287 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
289 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
292 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
294 IPinImpl *This = (IPinImpl *)iface;
295 ENUMMEDIADETAILS emd;
297 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
299 /* override this method to allow enumeration of your types */
300 emd.cMediaTypes = 0;
301 emd.pMediaTypes = NULL;
303 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
306 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
308 IPinImpl *This = (IPinImpl *)iface;
310 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
312 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
315 /*** IPin implementation for an input pin ***/
317 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
319 InputPin *This = (InputPin *)iface;
321 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
323 *ppv = NULL;
325 if (IsEqualIID(riid, &IID_IUnknown))
326 *ppv = iface;
327 else if (IsEqualIID(riid, &IID_IPin))
328 *ppv = iface;
329 else if (IsEqualIID(riid, &IID_IMemInputPin))
330 *ppv = &This->lpVtblMemInput;
331 else if (IsEqualIID(riid, &IID_IMediaSeeking))
333 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
336 if (*ppv)
338 IUnknown_AddRef((IUnknown *)(*ppv));
339 return S_OK;
342 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
344 return E_NOINTERFACE;
347 ULONG WINAPI InputPin_Release(IPin * iface)
349 InputPin *This = (InputPin *)iface;
350 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
352 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
354 if (!refCount)
356 FreeMediaType(&This->pin.mtCurrent);
357 if (This->pAllocator)
358 IMemAllocator_Release(This->pAllocator);
359 This->pAllocator = NULL;
360 This->pin.lpVtbl = NULL;
361 CoTaskMemFree(This);
362 return 0;
364 else
365 return refCount;
368 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
370 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
372 return E_UNEXPECTED;
376 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
378 InputPin *This = (InputPin *)iface;
379 PIN_DIRECTION pindirReceive;
380 HRESULT hr = S_OK;
382 TRACE("(%p, %p)\n", pReceivePin, pmt);
383 dump_AM_MEDIA_TYPE(pmt);
385 EnterCriticalSection(This->pin.pCritSec);
387 if (This->pin.pConnectedTo)
388 hr = VFW_E_ALREADY_CONNECTED;
390 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
391 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
392 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
394 if (SUCCEEDED(hr))
396 IPin_QueryDirection(pReceivePin, &pindirReceive);
398 if (pindirReceive != PINDIR_OUTPUT)
400 ERR("Can't connect from non-output pin\n");
401 hr = VFW_E_INVALID_DIRECTION;
405 if (SUCCEEDED(hr))
407 CopyMediaType(&This->pin.mtCurrent, pmt);
408 This->pin.pConnectedTo = pReceivePin;
409 IPin_AddRef(pReceivePin);
412 LeaveCriticalSection(This->pin.pCritSec);
414 return hr;
417 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
419 return IPin_EndOfStream( pin );
422 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
424 HRESULT hr = S_OK;
425 InputPin *This = (InputPin *)iface;
427 TRACE("(%p)\n", This);
429 EnterCriticalSection(This->pin.pCritSec);
430 if (This->flushing)
431 hr = S_FALSE;
432 else
433 This->end_of_stream = 1;
434 LeaveCriticalSection(This->pin.pCritSec);
436 if (hr == S_OK)
437 hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
438 return hr;
441 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
443 return IPin_BeginFlush( pin );
446 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
448 InputPin *This = (InputPin *)iface;
449 HRESULT hr;
450 TRACE("() semi-stub\n");
452 EnterCriticalSection(This->pin.pCritSec);
453 This->flushing = 1;
455 if (This->fnCleanProc)
456 This->fnCleanProc(This->pin.pUserData);
458 hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
459 LeaveCriticalSection(This->pin.pCritSec);
461 return hr;
464 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
466 return IPin_EndFlush( pin );
469 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
471 InputPin *This = (InputPin *)iface;
472 HRESULT hr;
473 TRACE("(%p)\n", This);
475 EnterCriticalSection(This->pin.pCritSec);
476 This->flushing = This->end_of_stream = 0;
478 hr = SendFurther( iface, deliver_endflush, NULL, NULL );
479 LeaveCriticalSection(This->pin.pCritSec);
481 return hr;
484 typedef struct newsegmentargs
486 REFERENCE_TIME tStart, tStop;
487 double rate;
488 } newsegmentargs;
490 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
492 newsegmentargs *args = data;
493 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
496 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
498 InputPin *This = (InputPin *)iface;
499 newsegmentargs args;
501 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
503 args.tStart = This->tStart = tStart;
504 args.tStop = This->tStop = tStop;
505 args.rate = This->dRate = dRate;
507 return SendFurther( iface, deliver_newsegment, &args, NULL );
510 static const IPinVtbl InputPin_Vtbl =
512 InputPin_QueryInterface,
513 IPinImpl_AddRef,
514 InputPin_Release,
515 InputPin_Connect,
516 InputPin_ReceiveConnection,
517 IPinImpl_Disconnect,
518 IPinImpl_ConnectedTo,
519 IPinImpl_ConnectionMediaType,
520 IPinImpl_QueryPinInfo,
521 IPinImpl_QueryDirection,
522 IPinImpl_QueryId,
523 IPinImpl_QueryAccept,
524 IPinImpl_EnumMediaTypes,
525 IPinImpl_QueryInternalConnections,
526 InputPin_EndOfStream,
527 InputPin_BeginFlush,
528 InputPin_EndFlush,
529 InputPin_NewSegment
532 /*** IMemInputPin implementation ***/
534 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
536 return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
539 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
541 InputPin *This = impl_from_IMemInputPin(iface);
543 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
546 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
548 InputPin *This = impl_from_IMemInputPin(iface);
550 return IPin_AddRef((IPin *)&This->pin);
553 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
555 InputPin *This = impl_from_IMemInputPin(iface);
557 return IPin_Release((IPin *)&This->pin);
560 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
562 InputPin *This = impl_from_IMemInputPin(iface);
564 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
566 *ppAllocator = This->pAllocator;
567 if (*ppAllocator)
568 IMemAllocator_AddRef(*ppAllocator);
570 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
573 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
575 InputPin *This = impl_from_IMemInputPin(iface);
577 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
579 if (bReadOnly)
580 FIXME("Read only flag not handled yet!\n");
582 /* FIXME: Should we release the allocator on disconnection? */
583 if (!pAllocator)
585 WARN("Null allocator\n");
586 return E_POINTER;
589 if (This->preferred_allocator && pAllocator != This->preferred_allocator)
590 return E_FAIL;
592 if (This->pAllocator)
593 IMemAllocator_Release(This->pAllocator);
594 This->pAllocator = pAllocator;
595 if (This->pAllocator)
596 IMemAllocator_AddRef(This->pAllocator);
598 return S_OK;
601 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
603 InputPin *This = impl_from_IMemInputPin(iface);
605 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
607 /* override this method if you have any specific requirements */
609 return E_NOTIMPL;
612 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
614 InputPin *This = impl_from_IMemInputPin(iface);
615 HRESULT hr;
617 /* this trace commented out for performance reasons */
618 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
619 hr = This->fnSampleProc(This->pin.pUserData, pSample);
620 return hr;
623 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
625 HRESULT hr = S_OK;
626 InputPin *This = impl_from_IMemInputPin(iface);
628 TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
630 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
632 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
633 if (hr != S_OK)
634 break;
637 return hr;
640 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
642 InputPin *This = impl_from_IMemInputPin(iface);
644 TRACE("(%p/%p)->()\n", This, iface);
646 return S_OK;
649 static const IMemInputPinVtbl MemInputPin_Vtbl =
651 MemInputPin_QueryInterface,
652 MemInputPin_AddRef,
653 MemInputPin_Release,
654 MemInputPin_GetAllocator,
655 MemInputPin_NotifyAllocator,
656 MemInputPin_GetAllocatorRequirements,
657 MemInputPin_Receive,
658 MemInputPin_ReceiveMultiple,
659 MemInputPin_ReceiveCanBlock
662 /*** OutputPin implementation ***/
664 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
666 OutputPin *This = (OutputPin *)iface;
668 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
670 *ppv = NULL;
672 if (IsEqualIID(riid, &IID_IUnknown))
673 *ppv = iface;
674 else if (IsEqualIID(riid, &IID_IPin))
675 *ppv = iface;
676 else if (IsEqualIID(riid, &IID_IMediaSeeking))
678 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
681 if (*ppv)
683 IUnknown_AddRef((IUnknown *)(*ppv));
684 return S_OK;
687 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
689 return E_NOINTERFACE;
692 ULONG WINAPI OutputPin_Release(IPin * iface)
694 OutputPin *This = (OutputPin *)iface;
695 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
697 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
699 if (!refCount)
701 FreeMediaType(&This->pin.mtCurrent);
702 CoTaskMemFree(This);
703 return 0;
705 return refCount;
708 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
710 HRESULT hr;
711 OutputPin *This = (OutputPin *)iface;
713 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
714 dump_AM_MEDIA_TYPE(pmt);
716 /* If we try to connect to ourself, we will definitely deadlock.
717 * There are other cases where we could deadlock too, but this
718 * catches the obvious case */
719 assert(pReceivePin != iface);
721 EnterCriticalSection(This->pin.pCritSec);
723 /* if we have been a specific type to connect with, then we can either connect
724 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
725 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
726 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
727 else
729 /* negotiate media type */
731 IEnumMediaTypes * pEnumCandidates;
732 AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
734 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
736 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
738 /* try this filter's media types first */
739 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
741 assert(pmtCandidate);
742 dump_AM_MEDIA_TYPE(pmtCandidate);
743 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
744 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
745 assert(pmtCandidate->pbFormat);
746 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
747 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
749 hr = S_OK;
750 DeleteMediaType(pmtCandidate);
751 break;
753 DeleteMediaType(pmtCandidate);
754 pmtCandidate = NULL;
756 IEnumMediaTypes_Release(pEnumCandidates);
759 /* then try receiver filter's media types */
760 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
762 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
764 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
766 assert(pmtCandidate);
767 dump_AM_MEDIA_TYPE(pmtCandidate);
768 if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
769 && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
770 assert(pmtCandidate->pbFormat);
771 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
772 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
774 hr = S_OK;
775 DeleteMediaType(pmtCandidate);
776 break;
778 DeleteMediaType(pmtCandidate);
779 pmtCandidate = NULL;
780 } /* while */
781 IEnumMediaTypes_Release(pEnumCandidates);
782 } /* if not found */
783 } /* if negotiate media type */
784 } /* if succeeded */
785 LeaveCriticalSection(This->pin.pCritSec);
787 TRACE(" -- %x\n", hr);
788 return hr;
791 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
793 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
795 return E_UNEXPECTED;
798 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
800 HRESULT hr;
801 OutputPin *This = (OutputPin *)iface;
803 TRACE("()\n");
805 EnterCriticalSection(This->pin.pCritSec);
807 if (This->pMemInputPin)
809 IMemInputPin_Release(This->pMemInputPin);
810 This->pMemInputPin = NULL;
812 if (This->pin.pConnectedTo)
814 IPin_Release(This->pin.pConnectedTo);
815 This->pin.pConnectedTo = NULL;
816 FreeMediaType(&This->pin.mtCurrent);
817 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
818 hr = S_OK;
820 else
821 hr = S_FALSE;
823 LeaveCriticalSection(This->pin.pCritSec);
825 return hr;
828 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
830 TRACE("()\n");
832 /* not supposed to do anything in an output pin */
834 return E_UNEXPECTED;
837 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
839 TRACE("(%p)->()\n", iface);
841 /* not supposed to do anything in an output pin */
843 return E_UNEXPECTED;
846 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
848 TRACE("(%p)->()\n", iface);
850 /* not supposed to do anything in an output pin */
852 return E_UNEXPECTED;
855 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
857 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
859 /* not supposed to do anything in an output pin */
861 return E_UNEXPECTED;
864 static const IPinVtbl OutputPin_Vtbl =
866 OutputPin_QueryInterface,
867 IPinImpl_AddRef,
868 OutputPin_Release,
869 OutputPin_Connect,
870 OutputPin_ReceiveConnection,
871 OutputPin_Disconnect,
872 IPinImpl_ConnectedTo,
873 IPinImpl_ConnectionMediaType,
874 IPinImpl_QueryPinInfo,
875 IPinImpl_QueryDirection,
876 IPinImpl_QueryId,
877 IPinImpl_QueryAccept,
878 IPinImpl_EnumMediaTypes,
879 IPinImpl_QueryInternalConnections,
880 OutputPin_EndOfStream,
881 OutputPin_BeginFlush,
882 OutputPin_EndFlush,
883 OutputPin_NewSegment
886 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
888 HRESULT hr;
890 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
892 EnterCriticalSection(This->pin.pCritSec);
894 if (!This->pin.pConnectedTo)
895 hr = VFW_E_NOT_CONNECTED;
896 else
898 IMemAllocator * pAlloc = NULL;
900 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
902 if (SUCCEEDED(hr))
903 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
905 if (SUCCEEDED(hr))
906 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
908 if (pAlloc)
909 IMemAllocator_Release(pAlloc);
912 LeaveCriticalSection(This->pin.pCritSec);
914 return hr;
917 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
919 HRESULT hr = S_OK;
920 IMemInputPin * pMemConnected = NULL;
921 PIN_INFO pinInfo;
923 EnterCriticalSection(This->pin.pCritSec);
925 if (!This->pin.pConnectedTo || !This->pMemInputPin)
926 hr = VFW_E_NOT_CONNECTED;
927 else
929 /* we don't have the lock held when using This->pMemInputPin,
930 * so we need to AddRef it to stop it being deleted while we are
931 * using it. Same with its filter. */
932 pMemConnected = This->pMemInputPin;
933 IMemInputPin_AddRef(pMemConnected);
934 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
937 LeaveCriticalSection(This->pin.pCritSec);
939 if (SUCCEEDED(hr))
941 /* NOTE: if we are in a critical section when Receive is called
942 * then it causes some problems (most notably with the native Video
943 * Renderer) if we are re-entered for whatever reason */
944 hr = IMemInputPin_Receive(pMemConnected, pSample);
946 /* If the filter's destroyed, tell upstream to stop sending data */
947 if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
948 hr = S_FALSE;
950 if (pMemConnected)
951 IMemInputPin_Release(pMemConnected);
953 return hr;
956 HRESULT OutputPin_CommitAllocator(OutputPin * This)
958 HRESULT hr = S_OK;
960 TRACE("(%p)->()\n", This);
962 EnterCriticalSection(This->pin.pCritSec);
964 if (!This->pin.pConnectedTo || !This->pMemInputPin)
965 hr = VFW_E_NOT_CONNECTED;
966 else
968 IMemAllocator * pAlloc = NULL;
970 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
972 if (SUCCEEDED(hr))
973 hr = IMemAllocator_Commit(pAlloc);
975 if (pAlloc)
976 IMemAllocator_Release(pAlloc);
979 LeaveCriticalSection(This->pin.pCritSec);
981 TRACE("--> %08x\n", hr);
982 return hr;
985 HRESULT OutputPin_DecommitAllocator(OutputPin * This)
987 HRESULT hr = S_OK;
989 TRACE("(%p)->()\n", This);
991 EnterCriticalSection(This->pin.pCritSec);
993 if (!This->pin.pConnectedTo || !This->pMemInputPin)
994 hr = VFW_E_NOT_CONNECTED;
995 else
997 IMemAllocator * pAlloc = NULL;
999 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1001 if (SUCCEEDED(hr))
1002 hr = IMemAllocator_Decommit(pAlloc);
1004 if (pAlloc)
1005 IMemAllocator_Release(pAlloc);
1008 LeaveCriticalSection(This->pin.pCritSec);
1010 TRACE("--> %08x\n", hr);
1011 return hr;
1014 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1016 HRESULT hr;
1018 TRACE("(%p)->()\n", This);
1020 EnterCriticalSection(This->pin.pCritSec);
1022 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1023 hr = VFW_E_NOT_CONNECTED;
1024 else if (!This->custom_allocator)
1026 IMemAllocator * pAlloc = NULL;
1028 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1030 if (SUCCEEDED(hr))
1031 hr = IMemAllocator_Decommit(pAlloc);
1033 if (pAlloc)
1034 IMemAllocator_Release(pAlloc);
1036 if (SUCCEEDED(hr))
1037 hr = IPin_Disconnect(This->pin.pConnectedTo);
1039 else /* Kill the allocator! */
1041 hr = IPin_Disconnect(This->pin.pConnectedTo);
1043 IPin_Disconnect((IPin *)This);
1045 LeaveCriticalSection(This->pin.pCritSec);
1047 return hr;
1050 /*** PullPin implementation ***/
1052 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData,
1053 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1055 /* Common attributes */
1056 pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1057 pPinImpl->pin.refCount = 1;
1058 pPinImpl->pin.pConnectedTo = NULL;
1059 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1060 pPinImpl->pin.pUserData = pUserData;
1061 pPinImpl->pin.pCritSec = pCritSec;
1062 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1063 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1065 /* Input pin attributes */
1066 pPinImpl->fnSampleProc = pSampleProc;
1067 pPinImpl->fnCleanProc = pCleanUp;
1068 pPinImpl->fnDone = pDone;
1069 pPinImpl->fnPreConnect = NULL;
1070 pPinImpl->pAlloc = NULL;
1071 pPinImpl->pReader = NULL;
1072 pPinImpl->hThread = NULL;
1073 pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1074 pPinImpl->thread_sleepy = CreateEventW(NULL, FALSE, FALSE, NULL);
1076 pPinImpl->rtStart = 0;
1077 pPinImpl->rtCurrent = 0;
1078 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1079 pPinImpl->dRate = 1.0;
1080 pPinImpl->state = Req_Die;
1081 pPinImpl->fnCustomRequest = pCustomRequest;
1082 pPinImpl->stop_playback = 1;
1084 InitializeCriticalSection(&pPinImpl->thread_lock);
1085 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1087 return S_OK;
1090 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)
1092 PullPin * pPinImpl;
1094 *ppPin = NULL;
1096 if (pPinInfo->dir != PINDIR_INPUT)
1098 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1099 return E_INVALIDARG;
1102 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1104 if (!pPinImpl)
1105 return E_OUTOFMEMORY;
1107 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
1109 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1110 return S_OK;
1113 CoTaskMemFree(pPinImpl);
1114 return E_FAIL;
1117 static HRESULT PullPin_InitProcessing(PullPin * This);
1119 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1121 PIN_DIRECTION pindirReceive;
1122 HRESULT hr = S_OK;
1123 PullPin *This = (PullPin *)iface;
1125 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1126 dump_AM_MEDIA_TYPE(pmt);
1128 EnterCriticalSection(This->pin.pCritSec);
1129 if (!This->pin.pConnectedTo)
1131 ALLOCATOR_PROPERTIES props;
1133 props.cBuffers = 3;
1134 props.cbBuffer = 64 * 1024; /* 64k bytes */
1135 props.cbAlign = 1;
1136 props.cbPrefix = 0;
1138 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1139 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1140 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1142 if (SUCCEEDED(hr))
1144 IPin_QueryDirection(pReceivePin, &pindirReceive);
1146 if (pindirReceive != PINDIR_OUTPUT)
1148 ERR("Can't connect from non-output pin\n");
1149 hr = VFW_E_INVALID_DIRECTION;
1153 This->pReader = NULL;
1154 This->pAlloc = NULL;
1155 if (SUCCEEDED(hr))
1157 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1160 if (SUCCEEDED(hr) && This->fnPreConnect)
1162 hr = This->fnPreConnect(iface, pReceivePin, &props);
1165 if (SUCCEEDED(hr))
1167 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1170 if (SUCCEEDED(hr))
1172 CopyMediaType(&This->pin.mtCurrent, pmt);
1173 This->pin.pConnectedTo = pReceivePin;
1174 IPin_AddRef(pReceivePin);
1175 hr = IMemAllocator_Commit(This->pAlloc);
1178 if (SUCCEEDED(hr))
1179 hr = PullPin_InitProcessing(This);
1181 if (FAILED(hr))
1183 if (This->pReader)
1184 IAsyncReader_Release(This->pReader);
1185 This->pReader = NULL;
1186 if (This->pAlloc)
1187 IMemAllocator_Release(This->pAlloc);
1188 This->pAlloc = NULL;
1191 else
1192 hr = VFW_E_ALREADY_CONNECTED;
1193 LeaveCriticalSection(This->pin.pCritSec);
1194 return hr;
1197 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1199 PullPin *This = (PullPin *)iface;
1201 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1203 *ppv = NULL;
1205 if (IsEqualIID(riid, &IID_IUnknown))
1206 *ppv = iface;
1207 else if (IsEqualIID(riid, &IID_IPin))
1208 *ppv = iface;
1209 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1211 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1214 if (*ppv)
1216 IUnknown_AddRef((IUnknown *)(*ppv));
1217 return S_OK;
1220 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1222 return E_NOINTERFACE;
1225 ULONG WINAPI PullPin_Release(IPin *iface)
1227 PullPin *This = (PullPin *)iface;
1228 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1230 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1232 if (!refCount)
1234 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1235 assert(!This->hThread);
1237 if(This->pAlloc)
1238 IMemAllocator_Release(This->pAlloc);
1239 if(This->pReader)
1240 IAsyncReader_Release(This->pReader);
1241 CloseHandle(This->thread_sleepy);
1242 CloseHandle(This->hEventStateChanged);
1243 This->thread_lock.DebugInfo->Spare[0] = 0;
1244 DeleteCriticalSection(&This->thread_lock);
1245 CoTaskMemFree(This);
1246 return 0;
1248 return refCount;
1251 static void PullPin_Flush(PullPin *This)
1253 IMediaSample *pSample;
1254 TRACE("Flushing!\n");
1256 if (This->pReader)
1258 /* Flush outstanding samples */
1259 IAsyncReader_BeginFlush(This->pReader);
1261 for (;;)
1263 DWORD_PTR dwUser;
1265 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
1267 if (!pSample)
1268 break;
1270 assert(!IMediaSample_GetActualDataLength(pSample));
1272 IMediaSample_Release(pSample);
1275 IAsyncReader_EndFlush(This->pReader);
1279 static void PullPin_Thread_Process(PullPin *This)
1281 HRESULT hr;
1282 IMediaSample * pSample = NULL;
1283 ALLOCATOR_PROPERTIES allocProps;
1285 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1287 This->cbAlign = allocProps.cbAlign;
1289 if (This->rtCurrent < This->rtStart)
1290 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
1292 TRACE("Start\n");
1294 if (This->rtCurrent >= This->rtStop)
1296 IPin_EndOfStream((IPin *)This);
1297 return;
1300 /* There is no sample in our buffer */
1301 hr = This->fnCustomRequest(This->pin.pUserData);
1303 if (FAILED(hr))
1304 ERR("Request error: %x\n", hr);
1306 EnterCriticalSection(This->pin.pCritSec);
1307 SetEvent(This->hEventStateChanged);
1308 LeaveCriticalSection(This->pin.pCritSec);
1310 if (SUCCEEDED(hr))
1313 DWORD_PTR dwUser;
1315 TRACE("Process sample\n");
1317 pSample = NULL;
1318 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1320 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1321 if (SUCCEEDED(hr))
1323 hr = This->fnSampleProc(This->pin.pUserData, pSample, dwUser);
1325 else
1327 /* FIXME: This is not well handled yet! */
1328 ERR("Processing error: %x\n", hr);
1329 if (hr == VFW_E_TIMEOUT)
1331 assert(!pSample);
1332 hr = S_OK;
1333 continue;
1337 if (pSample)
1339 IMediaSample_Release(pSample);
1340 pSample = NULL;
1342 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1344 /* Sample was rejected, and we are asked to terminate */
1345 if (pSample)
1347 IMediaSample_Release(pSample);
1350 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1351 * Flush remaining samples
1353 if (This->fnDone)
1354 This->fnDone(This->pin.pUserData);
1356 TRACE("End: %08x, %d\n", hr, This->stop_playback);
1359 static void PullPin_Thread_Pause(PullPin *This)
1361 PullPin_Flush(This);
1363 EnterCriticalSection(This->pin.pCritSec);
1364 This->state = Req_Sleepy;
1365 SetEvent(This->hEventStateChanged);
1366 LeaveCriticalSection(This->pin.pCritSec);
1369 static void PullPin_Thread_Stop(PullPin *This)
1371 TRACE("(%p)->()\n", This);
1373 EnterCriticalSection(This->pin.pCritSec);
1375 CloseHandle(This->hThread);
1376 This->hThread = NULL;
1377 SetEvent(This->hEventStateChanged);
1379 LeaveCriticalSection(This->pin.pCritSec);
1381 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1383 CoUninitialize();
1384 ExitThread(0);
1387 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1389 PullPin *This = pv;
1390 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1392 PullPin_Flush(This);
1394 for (;;)
1396 WaitForSingleObject(This->thread_sleepy, INFINITE);
1398 TRACE("State: %d\n", This->state);
1400 switch (This->state)
1402 case Req_Die: PullPin_Thread_Stop(This); break;
1403 case Req_Run: PullPin_Thread_Process(This); break;
1404 case Req_Pause: PullPin_Thread_Pause(This); break;
1405 case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
1406 default: ERR("Unknown state request: %d\n", This->state); break;
1409 return 0;
1412 static HRESULT PullPin_InitProcessing(PullPin * This)
1414 HRESULT hr = S_OK;
1416 TRACE("(%p)->()\n", This);
1418 /* if we are connected */
1419 if (This->pAlloc)
1421 DWORD dwThreadId;
1423 WaitForSingleObject(This->hEventStateChanged, INFINITE);
1424 EnterCriticalSection(This->pin.pCritSec);
1426 assert(!This->hThread);
1427 assert(This->state == Req_Die);
1428 assert(This->stop_playback);
1429 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1430 This->state = Req_Sleepy;
1432 /* AddRef the filter to make sure it and it's pins will be around
1433 * as long as the thread */
1434 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1437 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
1438 if (!This->hThread)
1440 hr = HRESULT_FROM_WIN32(GetLastError());
1441 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1444 if (SUCCEEDED(hr))
1446 SetEvent(This->hEventStateChanged);
1447 /* If assert fails, that means a command was not processed before the thread previously terminated */
1449 LeaveCriticalSection(This->pin.pCritSec);
1452 TRACE(" -- %x\n", hr);
1454 return hr;
1457 HRESULT PullPin_StartProcessing(PullPin * This)
1459 /* if we are connected */
1460 TRACE("(%p)->()\n", This);
1461 if(This->pAlloc)
1463 assert(This->hThread);
1465 PullPin_WaitForStateChange(This, INFINITE);
1467 assert(This->state == Req_Sleepy);
1469 /* Wake up! */
1470 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1471 This->state = Req_Run;
1472 This->stop_playback = 0;
1473 ResetEvent(This->hEventStateChanged);
1474 SetEvent(This->thread_sleepy);
1477 return S_OK;
1480 HRESULT PullPin_PauseProcessing(PullPin * This)
1482 /* if we are connected */
1483 TRACE("(%p)->()\n", This);
1484 if(This->pAlloc)
1486 assert(This->hThread);
1488 PullPin_WaitForStateChange(This, INFINITE);
1490 EnterCriticalSection(This->pin.pCritSec);
1492 assert(!This->stop_playback);
1493 assert(This->state == Req_Run|| This->state == Req_Sleepy);
1495 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1496 This->state = Req_Pause;
1497 This->stop_playback = 1;
1498 ResetEvent(This->hEventStateChanged);
1499 SetEvent(This->thread_sleepy);
1501 LeaveCriticalSection(This->pin.pCritSec);
1504 return S_OK;
1507 static HRESULT PullPin_StopProcessing(PullPin * This)
1509 TRACE("(%p)->()\n", This);
1511 /* if we are alive */
1512 assert(This->hThread);
1514 PullPin_WaitForStateChange(This, INFINITE);
1516 assert(This->state == Req_Pause || This->state == Req_Sleepy);
1518 This->stop_playback = 1;
1519 This->state = Req_Die;
1520 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
1521 ResetEvent(This->hEventStateChanged);
1522 SetEvent(This->thread_sleepy);
1523 return S_OK;
1526 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1528 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1529 return S_FALSE;
1530 return S_OK;
1533 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1535 FIXME("(%p)->() stub\n", iface);
1537 return SendFurther( iface, deliver_endofstream, NULL, NULL );
1540 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1542 PullPin *This = (PullPin *)iface;
1543 TRACE("(%p)->()\n", This);
1545 EnterCriticalSection(This->pin.pCritSec);
1547 SendFurther( iface, deliver_beginflush, NULL, NULL );
1549 LeaveCriticalSection(This->pin.pCritSec);
1551 EnterCriticalSection(&This->thread_lock);
1553 if (This->pReader)
1554 IAsyncReader_BeginFlush(This->pReader);
1555 PullPin_WaitForStateChange(This, INFINITE);
1557 if (This->hThread && This->state == Req_Run)
1559 PullPin_PauseProcessing(This);
1560 PullPin_WaitForStateChange(This, INFINITE);
1563 LeaveCriticalSection(&This->thread_lock);
1565 EnterCriticalSection(This->pin.pCritSec);
1567 This->fnCleanProc(This->pin.pUserData);
1569 LeaveCriticalSection(This->pin.pCritSec);
1571 return S_OK;
1574 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1576 PullPin *This = (PullPin *)iface;
1578 TRACE("(%p)->()\n", iface);
1580 /* Send further first: Else a race condition might terminate processing early */
1581 EnterCriticalSection(This->pin.pCritSec);
1582 SendFurther( iface, deliver_endflush, NULL, NULL );
1583 LeaveCriticalSection(This->pin.pCritSec);
1585 EnterCriticalSection(&This->thread_lock);
1587 FILTER_STATE state;
1589 if (This->pReader)
1590 IAsyncReader_EndFlush(This->pReader);
1592 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1594 if (state != State_Stopped)
1595 PullPin_StartProcessing(This);
1597 PullPin_WaitForStateChange(This, INFINITE);
1599 LeaveCriticalSection(&This->thread_lock);
1601 return S_OK;
1604 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
1606 HRESULT hr;
1607 PullPin *This = (PullPin *)iface;
1609 TRACE("()\n");
1611 EnterCriticalSection(This->pin.pCritSec);
1613 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1614 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1616 if (This->pin.pConnectedTo)
1618 IPin_Release(This->pin.pConnectedTo);
1619 This->pin.pConnectedTo = NULL;
1620 PullPin_StopProcessing(This);
1622 FreeMediaType(&This->pin.mtCurrent);
1623 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
1624 hr = S_OK;
1626 else
1627 hr = S_FALSE;
1629 LeaveCriticalSection(This->pin.pCritSec);
1631 return hr;
1634 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1636 newsegmentargs args;
1637 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1639 args.tStart = tStart;
1640 args.tStop = tStop;
1641 args.rate = dRate;
1643 return SendFurther( iface, deliver_newsegment, &args, NULL );
1646 static const IPinVtbl PullPin_Vtbl =
1648 PullPin_QueryInterface,
1649 IPinImpl_AddRef,
1650 PullPin_Release,
1651 InputPin_Connect,
1652 PullPin_ReceiveConnection,
1653 PullPin_Disconnect,
1654 IPinImpl_ConnectedTo,
1655 IPinImpl_ConnectionMediaType,
1656 IPinImpl_QueryPinInfo,
1657 IPinImpl_QueryDirection,
1658 IPinImpl_QueryId,
1659 IPinImpl_QueryAccept,
1660 IPinImpl_EnumMediaTypes,
1661 IPinImpl_QueryInternalConnections,
1662 PullPin_EndOfStream,
1663 PullPin_BeginFlush,
1664 PullPin_EndFlush,
1665 PullPin_NewSegment
1668 /*** The Construct functions ***/
1670 /* Function called as a helper to IPin_Connect */
1671 /* specific AM_MEDIA_TYPE - it cannot be NULL */
1672 /* NOTE: not part of standard interface */
1673 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1675 OutputPin *This = (OutputPin *)iface;
1676 HRESULT hr;
1677 IMemAllocator * pMemAlloc = NULL;
1678 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
1680 TRACE("(%p, %p)\n", pReceivePin, pmt);
1681 dump_AM_MEDIA_TYPE(pmt);
1683 /* FIXME: call queryacceptproc */
1685 This->pin.pConnectedTo = pReceivePin;
1686 IPin_AddRef(pReceivePin);
1687 CopyMediaType(&This->pin.mtCurrent, pmt);
1689 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
1691 /* get the IMemInputPin interface we will use to deliver samples to the
1692 * connected pin */
1693 if (SUCCEEDED(hr))
1695 This->pMemInputPin = NULL;
1696 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
1698 if (SUCCEEDED(hr) && !This->custom_allocator)
1700 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
1702 if (hr == VFW_E_NO_ALLOCATOR)
1703 /* Input pin provides no allocator, use standard memory allocator */
1704 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
1706 if (SUCCEEDED(hr))
1707 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
1709 if (SUCCEEDED(hr))
1710 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, This->readonly);
1712 if (pMemAlloc)
1713 IMemAllocator_Release(pMemAlloc);
1715 else if (SUCCEEDED(hr))
1717 if (This->alloc)
1719 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, This->alloc, This->readonly);
1721 else
1722 hr = VFW_E_NO_ALLOCATOR;
1725 /* break connection if we couldn't get the allocator */
1726 if (FAILED(hr))
1728 if (This->pMemInputPin)
1729 IMemInputPin_Release(This->pMemInputPin);
1730 This->pMemInputPin = NULL;
1732 IPin_Disconnect(pReceivePin);
1736 if (FAILED(hr))
1738 IPin_Release(This->pin.pConnectedTo);
1739 This->pin.pConnectedTo = NULL;
1740 FreeMediaType(&This->pin.mtCurrent);
1743 TRACE(" -- %x\n", hr);
1744 return hr;
1747 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PUSH pSampleProc, LPVOID pUserData,
1748 QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, InputPin * pPinImpl)
1750 TRACE("\n");
1752 /* Common attributes */
1753 pPinImpl->pin.refCount = 1;
1754 pPinImpl->pin.pConnectedTo = NULL;
1755 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1756 pPinImpl->pin.pUserData = pUserData;
1757 pPinImpl->pin.pCritSec = pCritSec;
1758 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1759 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1761 /* Input pin attributes */
1762 pPinImpl->fnSampleProc = pSampleProc;
1763 pPinImpl->fnCleanProc = pCleanUp;
1764 pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1765 if (pPinImpl->preferred_allocator)
1766 IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1767 pPinImpl->tStart = 0;
1768 pPinImpl->tStop = 0;
1769 pPinImpl->dRate = 1.0;
1770 pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1771 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1772 pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1774 return S_OK;
1777 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
1778 QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
1780 TRACE("\n");
1782 /* Common attributes */
1783 pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
1784 pPinImpl->pin.refCount = 1;
1785 pPinImpl->pin.pConnectedTo = NULL;
1786 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1787 pPinImpl->pin.pUserData = pUserData;
1788 pPinImpl->pin.pCritSec = pCritSec;
1789 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1790 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1792 /* Output pin attributes */
1793 pPinImpl->pMemInputPin = NULL;
1794 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
1795 /* If custom_allocator is set, you will need to specify an allocator
1796 * in the alloc member of the struct before an output pin can connect
1798 pPinImpl->custom_allocator = 0;
1799 pPinImpl->alloc = NULL;
1800 pPinImpl->readonly = FALSE;
1801 if (props)
1803 pPinImpl->allocProps = *props;
1804 if (pPinImpl->allocProps.cbAlign == 0)
1805 pPinImpl->allocProps.cbAlign = 1;
1807 else
1808 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
1810 return S_OK;
1813 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)
1815 InputPin * pPinImpl;
1817 *ppPin = NULL;
1819 if (pPinInfo->dir != PINDIR_INPUT)
1821 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1822 return E_INVALIDARG;
1825 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1827 if (!pPinImpl)
1828 return E_OUTOFMEMORY;
1830 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, allocator, pPinImpl)))
1832 *ppPin = (IPin *)pPinImpl;
1833 return S_OK;
1836 CoTaskMemFree(pPinImpl);
1837 return E_FAIL;
1840 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)
1842 OutputPin * pPinImpl;
1844 *ppPin = NULL;
1846 if (pPinInfo->dir != PINDIR_OUTPUT)
1848 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
1849 return E_INVALIDARG;
1852 assert(outputpin_size >= sizeof(OutputPin));
1854 pPinImpl = CoTaskMemAlloc(outputpin_size);
1856 if (!pPinImpl)
1857 return E_OUTOFMEMORY;
1859 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1861 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1862 return S_OK;
1865 CoTaskMemFree(pPinImpl);
1866 return E_FAIL;