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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "quartz_private.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
32 static const struct IPinVtbl InputPin_Vtbl
;
33 static const struct IPinVtbl OutputPin_Vtbl
;
34 static const struct IMemInputPinVtbl MemInputPin_Vtbl
;
35 static const struct IPinVtbl PullPin_Vtbl
;
37 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
40 #define _IMemInputPin_Offset ((int)(&(((InputPin*)0)->lpVtblMemInput)))
41 #define ICOM_THIS_From_IMemInputPin(impl, iface) impl* This = (impl*)(((char*)iface)-_IMemInputPin_Offset);
43 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
45 /* Tempting to just do a memcpy, but the name field is
46 128 characters long! We will probably never exceed 10
47 most of the time, so we are better off copying
48 each field manually */
49 strcpyW(pDest
->achName
, pSrc
->achName
);
50 pDest
->dir
= pSrc
->dir
;
51 pDest
->pFilter
= pSrc
->pFilter
;
52 IBaseFilter_AddRef(pDest
->pFilter
);
55 /* Internal function called as a helper to IPin_Connect */
56 /* specific AM_MEDIA_TYPE - it cannot be NULL */
57 /* NOTE: not part of standard interface */
58 static HRESULT
OutputPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
60 ICOM_THIS(OutputPin
, iface
);
62 IMemAllocator
* pMemAlloc
= NULL
;
63 ALLOCATOR_PROPERTIES actual
; /* FIXME: should we put the actual props back in to This? */
65 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
66 dump_AM_MEDIA_TYPE(pmt
);
68 /* FIXME: call queryacceptproc */
70 This
->pin
.pConnectedTo
= pReceivePin
;
71 IPin_AddRef(pReceivePin
);
72 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
74 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
76 /* get the IMemInputPin interface we will use to deliver samples to the
80 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
83 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pMemAlloc
);
86 hr
= IMemAllocator_SetProperties(pMemAlloc
, &This
->allocProps
, &actual
);
89 IMemAllocator_Release(pMemAlloc
);
91 /* break connection if we couldn't get the allocator */
93 IPin_Disconnect(pReceivePin
);
98 IPin_Release(This
->pin
.pConnectedTo
);
99 This
->pin
.pConnectedTo
= NULL
;
100 DeleteMediaType(&This
->pin
.mtCurrent
);
103 TRACE(" -- %lx\n", hr
);
107 HRESULT
InputPin_Construct(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
113 if (pPinInfo
->dir
!= PINDIR_INPUT
)
115 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
119 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
122 return E_OUTOFMEMORY
;
124 if (SUCCEEDED(InputPin_Init(pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
126 pPinImpl
->pin
.lpVtbl
= &InputPin_Vtbl
;
127 pPinImpl
->lpVtblMemInput
= &MemInputPin_Vtbl
;
129 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
135 /* Note that we don't init the vtables here (like C++ constructor) */
136 HRESULT
InputPin_Init(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, InputPin
* pPinImpl
)
138 /* Common attributes */
139 pPinImpl
->pin
.refCount
= 1;
140 pPinImpl
->pin
.pConnectedTo
= NULL
;
141 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
142 pPinImpl
->pin
.pUserData
= pUserData
;
143 pPinImpl
->pin
.pCritSec
= pCritSec
;
144 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
146 /* Input pin attributes */
147 pPinImpl
->fnSampleProc
= pSampleProc
;
148 pPinImpl
->pAllocator
= NULL
;
149 pPinImpl
->tStart
= 0;
156 HRESULT
OutputPin_Init(const PIN_INFO
* pPinInfo
, ALLOCATOR_PROPERTIES
* props
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, OutputPin
* pPinImpl
)
158 /* Common attributes */
159 pPinImpl
->pin
.lpVtbl
= &OutputPin_Vtbl
;
160 pPinImpl
->pin
.refCount
= 1;
161 pPinImpl
->pin
.pConnectedTo
= NULL
;
162 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
163 pPinImpl
->pin
.pUserData
= pUserData
;
164 pPinImpl
->pin
.pCritSec
= pCritSec
;
165 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
167 /* Output pin attributes */
168 pPinImpl
->pMemInputPin
= NULL
;
169 pPinImpl
->pConnectSpecific
= OutputPin_ConnectSpecific
;
172 memcpy(&pPinImpl
->allocProps
, props
, sizeof(pPinImpl
->allocProps
));
173 if (pPinImpl
->allocProps
.cbAlign
== 0)
174 pPinImpl
->allocProps
.cbAlign
= 1;
177 ZeroMemory(&pPinImpl
->allocProps
, sizeof(pPinImpl
->allocProps
));
183 HRESULT
OutputPin_Construct(const PIN_INFO
* pPinInfo
, ALLOCATOR_PROPERTIES
*props
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
185 OutputPin
* pPinImpl
;
189 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
191 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
195 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
198 return E_OUTOFMEMORY
;
200 if (SUCCEEDED(OutputPin_Init(pPinInfo
, props
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
202 pPinImpl
->pin
.lpVtbl
= &OutputPin_Vtbl
;
204 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
210 /*** Common pin functions ***/
212 ULONG WINAPI
IPinImpl_AddRef(IPin
* iface
)
214 ICOM_THIS(IPinImpl
, iface
);
218 return InterlockedIncrement(&This
->refCount
);
221 HRESULT WINAPI
IPinImpl_Disconnect(IPin
* iface
)
224 ICOM_THIS(IPinImpl
, iface
);
228 EnterCriticalSection(This
->pCritSec
);
230 if (This
->pConnectedTo
)
232 IPin_Release(This
->pConnectedTo
);
233 This
->pConnectedTo
= NULL
;
239 LeaveCriticalSection(This
->pCritSec
);
244 HRESULT WINAPI
IPinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
246 ICOM_THIS(IPinImpl
, iface
);
248 /* TRACE("(%p)\n", ppPin);*/
250 *ppPin
= This
->pConnectedTo
;
258 return VFW_E_NOT_CONNECTED
;
261 HRESULT WINAPI
IPinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
264 ICOM_THIS(IPinImpl
, iface
);
266 TRACE("(%p)\n", pmt
);
268 EnterCriticalSection(This
->pCritSec
);
270 if (This
->pConnectedTo
)
272 CopyMediaType(pmt
, &This
->mtCurrent
);
277 ZeroMemory(pmt
, sizeof(*pmt
));
278 hr
= VFW_E_NOT_CONNECTED
;
281 LeaveCriticalSection(This
->pCritSec
);
286 HRESULT WINAPI
IPinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
288 ICOM_THIS(IPinImpl
, iface
);
290 TRACE("(%p)\n", pInfo
);
292 Copy_PinInfo(pInfo
, &This
->pinInfo
);
297 HRESULT WINAPI
IPinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
299 ICOM_THIS(IPinImpl
, iface
);
301 TRACE("(%p)\n", pPinDir
);
303 *pPinDir
= This
->pinInfo
.dir
;
308 HRESULT WINAPI
IPinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
310 ICOM_THIS(IPinImpl
, iface
);
314 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
316 return E_OUTOFMEMORY
;
318 strcpyW(*Id
, This
->pinInfo
.achName
);
323 HRESULT WINAPI
IPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
325 ICOM_THIS(IPinImpl
, iface
);
327 TRACE("(%p)\n", pmt
);
329 return (This
->fnQueryAccept(This
->pUserData
, pmt
) == S_OK
? S_OK
: S_FALSE
);
332 HRESULT WINAPI
IPinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
334 ENUMMEDIADETAILS emd
;
336 TRACE("(%p)\n", ppEnum
);
338 /* override this method to allow enumeration of your types */
340 emd
.pMediaTypes
= NULL
;
342 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
345 HRESULT WINAPI
IPinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
347 TRACE("(%p, %p)\n", apPin
, cPin
);
349 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
352 /*** IPin implementation for an input pin ***/
354 HRESULT WINAPI
InputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
356 ICOM_THIS(InputPin
, iface
);
358 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
362 if (IsEqualIID(riid
, &IID_IUnknown
))
363 *ppv
= (LPVOID
)iface
;
364 else if (IsEqualIID(riid
, &IID_IPin
))
365 *ppv
= (LPVOID
)iface
;
366 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
367 *ppv
= (LPVOID
)&This
->lpVtblMemInput
;
371 IUnknown_AddRef((IUnknown
*)(*ppv
));
375 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
377 return E_NOINTERFACE
;
380 ULONG WINAPI
InputPin_Release(IPin
* iface
)
382 ICOM_THIS(InputPin
, iface
);
386 if (!InterlockedDecrement(&This
->pin
.refCount
))
388 DeleteMediaType(&This
->pin
.mtCurrent
);
389 if (This
->pAllocator
)
390 IMemAllocator_Release(This
->pAllocator
);
395 return This
->pin
.refCount
;
398 HRESULT WINAPI
InputPin_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
400 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
406 HRESULT WINAPI
InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
408 ICOM_THIS(InputPin
, iface
);
409 PIN_DIRECTION pindirReceive
;
412 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
413 dump_AM_MEDIA_TYPE(pmt
);
415 EnterCriticalSection(This
->pin
.pCritSec
);
417 if (This
->pin
.pConnectedTo
)
418 hr
= VFW_E_ALREADY_CONNECTED
;
420 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
421 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
422 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
426 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
428 if (pindirReceive
!= PINDIR_OUTPUT
)
430 ERR("Can't connect from non-output pin\n");
431 hr
= VFW_E_INVALID_DIRECTION
;
437 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
438 This
->pin
.pConnectedTo
= pReceivePin
;
439 IPin_AddRef(pReceivePin
);
442 LeaveCriticalSection(This
->pin
.pCritSec
);
447 HRESULT WINAPI
InputPin_EndOfStream(IPin
* iface
)
454 HRESULT WINAPI
InputPin_BeginFlush(IPin
* iface
)
460 HRESULT WINAPI
InputPin_EndFlush(IPin
* iface
)
466 HRESULT WINAPI
InputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
468 ICOM_THIS(InputPin
, iface
);
470 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
472 This
->tStart
= tStart
;
479 static const IPinVtbl InputPin_Vtbl
=
481 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
482 InputPin_QueryInterface
,
486 InputPin_ReceiveConnection
,
488 IPinImpl_ConnectedTo
,
489 IPinImpl_ConnectionMediaType
,
490 IPinImpl_QueryPinInfo
,
491 IPinImpl_QueryDirection
,
493 IPinImpl_QueryAccept
,
494 IPinImpl_EnumMediaTypes
,
495 IPinImpl_QueryInternalConnections
,
496 InputPin_EndOfStream
,
502 /*** IMemInputPin implementation ***/
504 HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
506 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
508 return IPin_QueryInterface((IPin
*)&This
->pin
, riid
, ppv
);
511 ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
513 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
515 return IPin_AddRef((IPin
*)&This
->pin
);
518 ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
520 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
522 return IPin_Release((IPin
*)&This
->pin
);
525 HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
527 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
529 TRACE("MemInputPin_GetAllocator()\n");
531 *ppAllocator
= This
->pAllocator
;
533 IMemAllocator_AddRef(*ppAllocator
);
535 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
538 HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
540 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
544 if (This
->pAllocator
)
545 IMemAllocator_Release(This
->pAllocator
);
546 This
->pAllocator
= pAllocator
;
547 if (This
->pAllocator
)
548 IMemAllocator_AddRef(This
->pAllocator
);
553 HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
555 TRACE("(%p)\n", pProps
);
557 /* override this method if you have any specific requirements */
562 HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
564 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
566 /* this trace commented out for performance reasons */
567 /* TRACE("(%p)\n", pSample);*/
569 return This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
572 HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, long nSamples
, long *nSamplesProcessed
)
575 TRACE("(%p, %ld, %p)\n", pSamples
, nSamples
, nSamplesProcessed
);
577 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
579 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
587 HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
591 /* FIXME: we should check whether any output pins will block */
596 static const IMemInputPinVtbl MemInputPin_Vtbl
=
598 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
599 MemInputPin_QueryInterface
,
602 MemInputPin_GetAllocator
,
603 MemInputPin_NotifyAllocator
,
604 MemInputPin_GetAllocatorRequirements
,
606 MemInputPin_ReceiveMultiple
,
607 MemInputPin_ReceiveCanBlock
610 HRESULT WINAPI
OutputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
612 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
616 if (IsEqualIID(riid
, &IID_IUnknown
))
617 *ppv
= (LPVOID
)iface
;
618 else if (IsEqualIID(riid
, &IID_IPin
))
619 *ppv
= (LPVOID
)iface
;
623 IUnknown_AddRef((IUnknown
*)(*ppv
));
627 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
629 return E_NOINTERFACE
;
632 ULONG WINAPI
OutputPin_Release(IPin
* iface
)
634 ICOM_THIS(OutputPin
, iface
);
638 if (!InterlockedDecrement(&This
->pin
.refCount
))
640 DeleteMediaType(&This
->pin
.mtCurrent
);
644 return This
->pin
.refCount
;
647 HRESULT WINAPI
OutputPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
650 ICOM_THIS(OutputPin
, iface
);
652 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
653 dump_AM_MEDIA_TYPE(pmt
);
655 /* If we try to connect to ourself, we will definitely deadlock.
656 * There are other cases where we could deadlock too, but this
657 * catches the obvious case */
658 assert(pReceivePin
!= iface
);
660 EnterCriticalSection(This
->pin
.pCritSec
);
662 /* if we have been a specific type to connect with, then we can either connect
663 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
664 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
665 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
668 /* negotiate media type */
670 IEnumMediaTypes
* pEnumCandidates
;
671 AM_MEDIA_TYPE
* pmtCandidate
; /* Candidate media type */
673 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
675 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
677 /* try this filter's media types first */
678 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
680 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
681 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
684 CoTaskMemFree(pmtCandidate
);
687 CoTaskMemFree(pmtCandidate
);
689 IEnumMediaTypes_Release(pEnumCandidates
);
692 /* then try receiver filter's media types */
693 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
695 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
697 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
698 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
701 CoTaskMemFree(pmtCandidate
);
704 CoTaskMemFree(pmtCandidate
);
706 IEnumMediaTypes_Release(pEnumCandidates
);
708 } /* if negotiate media type */
710 LeaveCriticalSection(This
->pin
.pCritSec
);
712 FIXME(" -- %lx\n", hr
);
716 HRESULT WINAPI
OutputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
718 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
723 HRESULT WINAPI
OutputPin_Disconnect(IPin
* iface
)
726 ICOM_THIS(OutputPin
, iface
);
730 EnterCriticalSection(This
->pin
.pCritSec
);
732 if (This
->pMemInputPin
)
734 IMemInputPin_Release(This
->pMemInputPin
);
735 This
->pMemInputPin
= NULL
;
737 if (This
->pin
.pConnectedTo
)
739 IPin_Release(This
->pin
.pConnectedTo
);
740 This
->pin
.pConnectedTo
= NULL
;
746 LeaveCriticalSection(This
->pin
.pCritSec
);
751 HRESULT WINAPI
OutputPin_EndOfStream(IPin
* iface
)
755 /* not supposed to do anything in an output pin */
760 HRESULT WINAPI
OutputPin_BeginFlush(IPin
* iface
)
764 /* not supposed to do anything in an output pin */
769 HRESULT WINAPI
OutputPin_EndFlush(IPin
* iface
)
773 /* not supposed to do anything in an output pin */
778 HRESULT WINAPI
OutputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
780 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
782 /* not supposed to do anything in an output pin */
787 static const IPinVtbl OutputPin_Vtbl
=
789 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
790 OutputPin_QueryInterface
,
794 OutputPin_ReceiveConnection
,
795 OutputPin_Disconnect
,
796 IPinImpl_ConnectedTo
,
797 IPinImpl_ConnectionMediaType
,
798 IPinImpl_QueryPinInfo
,
799 IPinImpl_QueryDirection
,
801 IPinImpl_QueryAccept
,
802 IPinImpl_EnumMediaTypes
,
803 IPinImpl_QueryInternalConnections
,
804 OutputPin_EndOfStream
,
805 OutputPin_BeginFlush
,
810 HRESULT
OutputPin_GetDeliveryBuffer(OutputPin
* This
, IMediaSample
** ppSample
, const REFERENCE_TIME
* tStart
, const REFERENCE_TIME
* tStop
, DWORD dwFlags
)
814 TRACE("(%p, %p, %p, %lx)\n", ppSample
, tStart
, tStop
, dwFlags
);
816 EnterCriticalSection(This
->pin
.pCritSec
);
818 if (!This
->pin
.pConnectedTo
)
819 hr
= VFW_E_NOT_CONNECTED
;
822 IMemInputPin
* pMemInputPin
= NULL
;
823 IMemAllocator
* pAlloc
= NULL
;
825 /* FIXME: should we cache this? */
826 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemInputPin
);
829 hr
= IMemInputPin_GetAllocator(pMemInputPin
, &pAlloc
);
832 hr
= IMemAllocator_GetBuffer(pAlloc
, ppSample
, (REFERENCE_TIME
*)tStart
, (REFERENCE_TIME
*)tStop
, dwFlags
);
835 hr
= IMediaSample_SetTime(*ppSample
, (REFERENCE_TIME
*)tStart
, (REFERENCE_TIME
*)tStop
);
838 IMemAllocator_Release(pAlloc
);
840 IMemInputPin_Release(pMemInputPin
);
843 LeaveCriticalSection(This
->pin
.pCritSec
);
848 HRESULT
OutputPin_SendSample(OutputPin
* This
, IMediaSample
* pSample
)
851 IMemInputPin
* pMemConnected
= NULL
;
853 EnterCriticalSection(This
->pin
.pCritSec
);
855 if (!This
->pin
.pConnectedTo
)
856 hr
= VFW_E_NOT_CONNECTED
;
859 /* FIXME: should we cache this? - if we do, then we need to keep the local version
860 * and AddRef here instead */
861 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemConnected
);
864 LeaveCriticalSection(This
->pin
.pCritSec
);
866 if (SUCCEEDED(hr
) && pMemConnected
)
868 /* NOTE: if we are in a critical section when Receive is called
869 * then it causes some problems (most notably with the native Video
870 * Renderer) if we are re-entered for whatever reason */
871 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
872 IMemInputPin_Release(pMemConnected
);
878 HRESULT
OutputPin_DeliverNewSegment(OutputPin
* This
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
882 EnterCriticalSection(This
->pin
.pCritSec
);
884 if (!This
->pin
.pConnectedTo
)
885 hr
= VFW_E_NOT_CONNECTED
;
887 hr
= IPin_NewSegment(This
->pin
.pConnectedTo
, tStart
, tStop
, dRate
);
889 LeaveCriticalSection(This
->pin
.pCritSec
);
894 HRESULT
OutputPin_CommitAllocator(OutputPin
* This
)
900 EnterCriticalSection(This
->pin
.pCritSec
);
902 if (!This
->pin
.pConnectedTo
)
903 hr
= VFW_E_NOT_CONNECTED
;
906 IMemInputPin
* pMemInputPin
= NULL
;
907 IMemAllocator
* pAlloc
= NULL
;
908 /* FIXME: should we cache this? */
909 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemInputPin
);
912 hr
= IMemInputPin_GetAllocator(pMemInputPin
, &pAlloc
);
915 hr
= IMemAllocator_Commit(pAlloc
);
918 IMemAllocator_Release(pAlloc
);
921 IMemInputPin_Release(pMemInputPin
);
924 LeaveCriticalSection(This
->pin
.pCritSec
);
929 HRESULT
OutputPin_DeliverDisconnect(OutputPin
* This
)
935 EnterCriticalSection(This
->pin
.pCritSec
);
937 if (!This
->pin
.pConnectedTo
)
938 hr
= VFW_E_NOT_CONNECTED
;
941 IMemInputPin
* pMemInputPin
= NULL
;
942 IMemAllocator
* pAlloc
= NULL
;
943 /* FIXME: should we cache this? */
944 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemInputPin
);
947 hr
= IMemInputPin_GetAllocator(pMemInputPin
, &pAlloc
);
950 hr
= IMemAllocator_Decommit(pAlloc
);
953 IMemAllocator_Release(pAlloc
);
956 IMemInputPin_Release(pMemInputPin
);
959 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
962 LeaveCriticalSection(This
->pin
.pCritSec
);
968 HRESULT
PullPin_Construct(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
974 if (pPinInfo
->dir
!= PINDIR_INPUT
)
976 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
980 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
983 return E_OUTOFMEMORY
;
985 if (SUCCEEDED(PullPin_Init(pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
987 pPinImpl
->pin
.lpVtbl
= &PullPin_Vtbl
;
989 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
995 HRESULT
PullPin_Init(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, PullPin
* pPinImpl
)
997 /* Common attributes */
998 pPinImpl
->pin
.refCount
= 1;
999 pPinImpl
->pin
.pConnectedTo
= NULL
;
1000 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
1001 pPinImpl
->pin
.pUserData
= pUserData
;
1002 pPinImpl
->pin
.pCritSec
= pCritSec
;
1003 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1005 /* Input pin attributes */
1006 pPinImpl
->fnSampleProc
= pSampleProc
;
1007 pPinImpl
->fnPreConnect
= NULL
;
1008 pPinImpl
->pAlloc
= NULL
;
1009 pPinImpl
->pReader
= NULL
;
1010 pPinImpl
->hThread
= NULL
;
1011 pPinImpl
->hEventStateChanged
= CreateEventW(NULL
, FALSE
, TRUE
, NULL
);
1013 pPinImpl
->rtStart
= 0;
1014 pPinImpl
->rtStop
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
1019 HRESULT WINAPI
PullPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
1021 PIN_DIRECTION pindirReceive
;
1023 ICOM_THIS(PullPin
, iface
);
1025 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
1026 dump_AM_MEDIA_TYPE(pmt
);
1028 EnterCriticalSection(This
->pin
.pCritSec
);
1030 if (This
->pin
.pConnectedTo
)
1031 hr
= VFW_E_ALREADY_CONNECTED
;
1033 if (SUCCEEDED(hr
) && (This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
))
1034 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
1035 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1039 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1041 if (pindirReceive
!= PINDIR_OUTPUT
)
1043 ERR("Can't connect from non-output pin\n");
1044 hr
= VFW_E_INVALID_DIRECTION
;
1050 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1055 ALLOCATOR_PROPERTIES props
;
1057 props
.cbBuffer
= 64 * 1024; /* 64k bytes */
1060 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1063 if (SUCCEEDED(hr
) && This
->fnPreConnect
)
1065 hr
= This
->fnPreConnect(iface
, pReceivePin
);
1070 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1071 This
->pin
.pConnectedTo
= pReceivePin
;
1072 IPin_AddRef(pReceivePin
);
1075 LeaveCriticalSection(This
->pin
.pCritSec
);
1079 HRESULT WINAPI
PullPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1081 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
1085 if (IsEqualIID(riid
, &IID_IUnknown
))
1086 *ppv
= (LPVOID
)iface
;
1087 else if (IsEqualIID(riid
, &IID_IPin
))
1088 *ppv
= (LPVOID
)iface
;
1092 IUnknown_AddRef((IUnknown
*)(*ppv
));
1096 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
1098 return E_NOINTERFACE
;
1101 ULONG WINAPI
PullPin_Release(IPin
* iface
)
1103 ICOM_THIS(PullPin
, iface
);
1107 if (!InterlockedDecrement(&This
->pin
.refCount
))
1110 PullPin_StopProcessing(This
);
1111 IMemAllocator_Release(This
->pAlloc
);
1112 IAsyncReader_Release(This
->pReader
);
1113 CloseHandle(This
->hEventStateChanged
);
1114 CoTaskMemFree(This
);
1117 return This
->pin
.refCount
;
1120 static DWORD WINAPI
PullPin_Thread_Main(LPVOID pv
)
1123 SleepEx(INFINITE
, TRUE
);
1126 static void CALLBACK
PullPin_Thread_Process(ULONG_PTR iface
)
1128 ICOM_THIS(PullPin
, iface
);
1131 REFERENCE_TIME rtCurrent
;
1132 ALLOCATOR_PROPERTIES allocProps
;
1134 SetEvent(This
->hEventStateChanged
);
1136 hr
= IMemAllocator_GetProperties(This
->pAlloc
, &allocProps
);
1138 rtCurrent
= MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This
->rtStart
), allocProps
.cbAlign
));
1140 while (rtCurrent
< This
->rtStop
)
1142 /* FIXME: to improve performance by quite a bit this should be changed
1143 * so that one sample is processed while one sample is fetched. However,
1144 * it is harder to debug so for the moment it will stay as it is */
1145 IMediaSample
* pSample
= NULL
;
1146 REFERENCE_TIME rtSampleStop
;
1149 hr
= IMemAllocator_GetBuffer(This
->pAlloc
, &pSample
, NULL
, NULL
, 0);
1153 rtSampleStop
= rtCurrent
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample
));
1154 if (rtSampleStop
> This
->rtStop
)
1155 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This
->rtStop
), allocProps
.cbAlign
));
1156 hr
= IMediaSample_SetTime(pSample
, &rtCurrent
, &rtSampleStop
);
1157 rtCurrent
= rtSampleStop
;
1161 hr
= IAsyncReader_Request(This
->pReader
, pSample
, (ULONG_PTR
)0);
1164 hr
= IAsyncReader_WaitForNext(This
->pReader
, 10000, &pSample
, &dwUser
);
1167 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
1169 ERR("Processing error: %lx\n", hr
);
1172 IMemAllocator_ReleaseBuffer(This
->pAlloc
, pSample
);
1176 static void CALLBACK
PullPin_Thread_Stop(ULONG_PTR iface
)
1178 ICOM_THIS(PullPin
, iface
);
1182 EnterCriticalSection(This
->pin
.pCritSec
);
1186 CloseHandle(This
->hThread
);
1187 This
->hThread
= NULL
;
1188 if (FAILED(hr
= IMemAllocator_Decommit(This
->pAlloc
)))
1189 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr
);
1191 LeaveCriticalSection(This
->pin
.pCritSec
);
1193 SetEvent(This
->hEventStateChanged
);
1198 HRESULT
PullPin_InitProcessing(PullPin
* This
)
1204 assert(!This
->hThread
);
1206 /* if we are connected */
1209 EnterCriticalSection(This
->pin
.pCritSec
);
1212 assert(!This
->hThread
);
1214 This
->hThread
= CreateThread(NULL
, 0, PullPin_Thread_Main
, NULL
, 0, &dwThreadId
);
1216 hr
= HRESULT_FROM_WIN32(GetLastError());
1219 hr
= IMemAllocator_Commit(This
->pAlloc
);
1221 LeaveCriticalSection(This
->pin
.pCritSec
);
1224 TRACE(" -- %lx\n", hr
);
1229 HRESULT
PullPin_StartProcessing(PullPin
* This
)
1231 /* if we are connected */
1234 assert(This
->hThread
);
1236 ResetEvent(This
->hEventStateChanged
);
1238 if (!QueueUserAPC(PullPin_Thread_Process
, This
->hThread
, (ULONG_PTR
)This
))
1239 return HRESULT_FROM_WIN32(GetLastError());
1245 HRESULT
PullPin_PauseProcessing(PullPin
* This
)
1247 /* make the processing function exit its loop */
1253 HRESULT
PullPin_StopProcessing(PullPin
* This
)
1255 /* if we are connected */
1258 assert(This
->hThread
);
1260 ResetEvent(This
->hEventStateChanged
);
1262 PullPin_PauseProcessing(This
);
1264 if (!QueueUserAPC(PullPin_Thread_Stop
, This
->hThread
, (ULONG_PTR
)This
))
1265 return HRESULT_FROM_WIN32(GetLastError());
1271 HRESULT
PullPin_WaitForStateChange(PullPin
* This
, DWORD dwMilliseconds
)
1273 if (WaitForSingleObject(This
->hEventStateChanged
, dwMilliseconds
) == WAIT_TIMEOUT
)
1278 HRESULT
PullPin_Seek(PullPin
* This
, REFERENCE_TIME rtStart
, REFERENCE_TIME rtStop
)
1280 FIXME("(%lx%08lx, %lx%08lx)\n", (LONG
)(rtStart
>> 32), (LONG
)rtStart
, (LONG
)(rtStop
>> 32), (LONG
)rtStop
);
1282 PullPin_BeginFlush((IPin
*)This
);
1283 /* FIXME: need critical section? */
1284 This
->rtStart
= rtStart
;
1285 This
->rtStop
= rtStop
;
1286 PullPin_EndFlush((IPin
*)This
);
1291 HRESULT WINAPI
PullPin_EndOfStream(IPin
* iface
)
1297 HRESULT WINAPI
PullPin_BeginFlush(IPin
* iface
)
1303 HRESULT WINAPI
PullPin_EndFlush(IPin
* iface
)
1309 HRESULT WINAPI
PullPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1315 static const IPinVtbl PullPin_Vtbl
=
1317 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1318 PullPin_QueryInterface
,
1322 PullPin_ReceiveConnection
,
1323 IPinImpl_Disconnect
,
1324 IPinImpl_ConnectedTo
,
1325 IPinImpl_ConnectionMediaType
,
1326 IPinImpl_QueryPinInfo
,
1327 IPinImpl_QueryDirection
,
1329 IPinImpl_QueryAccept
,
1330 IPinImpl_EnumMediaTypes
,
1331 IPinImpl_QueryInternalConnections
,
1332 PullPin_EndOfStream
,