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
)
140 /* Common attributes */
141 pPinImpl
->pin
.refCount
= 1;
142 pPinImpl
->pin
.pConnectedTo
= NULL
;
143 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
144 pPinImpl
->pin
.pUserData
= pUserData
;
145 pPinImpl
->pin
.pCritSec
= pCritSec
;
146 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
148 /* Input pin attributes */
149 pPinImpl
->fnSampleProc
= pSampleProc
;
150 pPinImpl
->pAllocator
= NULL
;
151 pPinImpl
->tStart
= 0;
158 HRESULT
OutputPin_Init(const PIN_INFO
* pPinInfo
, ALLOCATOR_PROPERTIES
* props
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, OutputPin
* pPinImpl
)
162 /* Common attributes */
163 pPinImpl
->pin
.lpVtbl
= &OutputPin_Vtbl
;
164 pPinImpl
->pin
.refCount
= 1;
165 pPinImpl
->pin
.pConnectedTo
= NULL
;
166 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
167 pPinImpl
->pin
.pUserData
= pUserData
;
168 pPinImpl
->pin
.pCritSec
= pCritSec
;
169 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
171 /* Output pin attributes */
172 pPinImpl
->pMemInputPin
= NULL
;
173 pPinImpl
->pConnectSpecific
= OutputPin_ConnectSpecific
;
176 memcpy(&pPinImpl
->allocProps
, props
, sizeof(pPinImpl
->allocProps
));
177 if (pPinImpl
->allocProps
.cbAlign
== 0)
178 pPinImpl
->allocProps
.cbAlign
= 1;
181 ZeroMemory(&pPinImpl
->allocProps
, sizeof(pPinImpl
->allocProps
));
187 HRESULT
OutputPin_Construct(const PIN_INFO
* pPinInfo
, ALLOCATOR_PROPERTIES
*props
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
189 OutputPin
* pPinImpl
;
193 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
195 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
199 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
202 return E_OUTOFMEMORY
;
204 if (SUCCEEDED(OutputPin_Init(pPinInfo
, props
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
206 pPinImpl
->pin
.lpVtbl
= &OutputPin_Vtbl
;
208 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
214 /*** Common pin functions ***/
216 ULONG WINAPI
IPinImpl_AddRef(IPin
* iface
)
218 ICOM_THIS(IPinImpl
, iface
);
222 return InterlockedIncrement(&This
->refCount
);
225 HRESULT WINAPI
IPinImpl_Disconnect(IPin
* iface
)
228 ICOM_THIS(IPinImpl
, iface
);
232 EnterCriticalSection(This
->pCritSec
);
234 if (This
->pConnectedTo
)
236 IPin_Release(This
->pConnectedTo
);
237 This
->pConnectedTo
= NULL
;
243 LeaveCriticalSection(This
->pCritSec
);
248 HRESULT WINAPI
IPinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
251 ICOM_THIS(IPinImpl
, iface
);
253 /* TRACE("(%p)\n", ppPin);*/
255 EnterCriticalSection(This
->pCritSec
);
257 if (This
->pConnectedTo
)
259 *ppPin
= This
->pConnectedTo
;
264 hr
= VFW_E_NOT_CONNECTED
;
266 LeaveCriticalSection(This
->pCritSec
);
271 HRESULT WINAPI
IPinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
274 ICOM_THIS(IPinImpl
, iface
);
276 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
278 EnterCriticalSection(This
->pCritSec
);
280 if (This
->pConnectedTo
)
282 CopyMediaType(pmt
, &This
->mtCurrent
);
287 ZeroMemory(pmt
, sizeof(*pmt
));
288 hr
= VFW_E_NOT_CONNECTED
;
291 LeaveCriticalSection(This
->pCritSec
);
296 HRESULT WINAPI
IPinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
298 ICOM_THIS(IPinImpl
, iface
);
300 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
302 Copy_PinInfo(pInfo
, &This
->pinInfo
);
307 HRESULT WINAPI
IPinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
309 ICOM_THIS(IPinImpl
, iface
);
311 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
313 *pPinDir
= This
->pinInfo
.dir
;
318 HRESULT WINAPI
IPinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
320 ICOM_THIS(IPinImpl
, iface
);
322 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
324 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
326 return E_OUTOFMEMORY
;
328 strcpyW(*Id
, This
->pinInfo
.achName
);
333 HRESULT WINAPI
IPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
335 ICOM_THIS(IPinImpl
, iface
);
337 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
339 return (This
->fnQueryAccept(This
->pUserData
, pmt
) == S_OK
? S_OK
: S_FALSE
);
342 HRESULT WINAPI
IPinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
344 ICOM_THIS(IPinImpl
, iface
);
345 ENUMMEDIADETAILS emd
;
347 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
349 /* override this method to allow enumeration of your types */
351 emd
.pMediaTypes
= NULL
;
353 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
356 HRESULT WINAPI
IPinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
358 ICOM_THIS(IPinImpl
, iface
);
360 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
362 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
365 /*** IPin implementation for an input pin ***/
367 HRESULT WINAPI
InputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
369 ICOM_THIS(InputPin
, iface
);
371 TRACE("(%p)->(%s, %p)\n", iface
, qzdebugstr_guid(riid
), ppv
);
375 if (IsEqualIID(riid
, &IID_IUnknown
))
376 *ppv
= (LPVOID
)iface
;
377 else if (IsEqualIID(riid
, &IID_IPin
))
378 *ppv
= (LPVOID
)iface
;
379 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
380 *ppv
= (LPVOID
)&This
->lpVtblMemInput
;
384 IUnknown_AddRef((IUnknown
*)(*ppv
));
388 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
390 return E_NOINTERFACE
;
393 ULONG WINAPI
InputPin_Release(IPin
* iface
)
395 ICOM_THIS(InputPin
, iface
);
399 if (!InterlockedDecrement(&This
->pin
.refCount
))
401 DeleteMediaType(&This
->pin
.mtCurrent
);
402 if (This
->pAllocator
)
403 IMemAllocator_Release(This
->pAllocator
);
408 return This
->pin
.refCount
;
411 HRESULT WINAPI
InputPin_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
413 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
419 HRESULT WINAPI
InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
421 ICOM_THIS(InputPin
, iface
);
422 PIN_DIRECTION pindirReceive
;
425 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
426 dump_AM_MEDIA_TYPE(pmt
);
428 EnterCriticalSection(This
->pin
.pCritSec
);
430 if (This
->pin
.pConnectedTo
)
431 hr
= VFW_E_ALREADY_CONNECTED
;
433 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
434 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
435 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
439 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
441 if (pindirReceive
!= PINDIR_OUTPUT
)
443 ERR("Can't connect from non-output pin\n");
444 hr
= VFW_E_INVALID_DIRECTION
;
450 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
451 This
->pin
.pConnectedTo
= pReceivePin
;
452 IPin_AddRef(pReceivePin
);
455 LeaveCriticalSection(This
->pin
.pCritSec
);
460 HRESULT WINAPI
InputPin_EndOfStream(IPin
* iface
)
467 HRESULT WINAPI
InputPin_BeginFlush(IPin
* iface
)
473 HRESULT WINAPI
InputPin_EndFlush(IPin
* iface
)
479 HRESULT WINAPI
InputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
481 ICOM_THIS(InputPin
, iface
);
483 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
485 This
->tStart
= tStart
;
492 static const IPinVtbl InputPin_Vtbl
=
494 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
495 InputPin_QueryInterface
,
499 InputPin_ReceiveConnection
,
501 IPinImpl_ConnectedTo
,
502 IPinImpl_ConnectionMediaType
,
503 IPinImpl_QueryPinInfo
,
504 IPinImpl_QueryDirection
,
506 IPinImpl_QueryAccept
,
507 IPinImpl_EnumMediaTypes
,
508 IPinImpl_QueryInternalConnections
,
509 InputPin_EndOfStream
,
515 /*** IMemInputPin implementation ***/
517 HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
519 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
521 return IPin_QueryInterface((IPin
*)&This
->pin
, riid
, ppv
);
524 ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
526 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
528 return IPin_AddRef((IPin
*)&This
->pin
);
531 ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
533 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
535 return IPin_Release((IPin
*)&This
->pin
);
538 HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
540 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
542 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
544 *ppAllocator
= This
->pAllocator
;
546 IMemAllocator_AddRef(*ppAllocator
);
548 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
551 HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
553 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
555 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
557 if (This
->pAllocator
)
558 IMemAllocator_Release(This
->pAllocator
);
559 This
->pAllocator
= pAllocator
;
560 if (This
->pAllocator
)
561 IMemAllocator_AddRef(This
->pAllocator
);
566 HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
568 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
570 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
572 /* override this method if you have any specific requirements */
577 HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
579 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
581 /* this trace commented out for performance reasons */
582 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
584 return This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
587 HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, long nSamples
, long *nSamplesProcessed
)
590 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
592 TRACE("(%p/%p)->(%p, %ld, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
594 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
596 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
604 HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
606 ICOM_THIS_From_IMemInputPin(InputPin
, iface
);
608 FIXME("(%p/%p)->()\n", This
, iface
);
610 /* FIXME: we should check whether any output pins will block */
615 static const IMemInputPinVtbl MemInputPin_Vtbl
=
617 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
618 MemInputPin_QueryInterface
,
621 MemInputPin_GetAllocator
,
622 MemInputPin_NotifyAllocator
,
623 MemInputPin_GetAllocatorRequirements
,
625 MemInputPin_ReceiveMultiple
,
626 MemInputPin_ReceiveCanBlock
629 HRESULT WINAPI
OutputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
631 ICOM_THIS(OutputPin
, iface
);
633 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
637 if (IsEqualIID(riid
, &IID_IUnknown
))
638 *ppv
= (LPVOID
)iface
;
639 else if (IsEqualIID(riid
, &IID_IPin
))
640 *ppv
= (LPVOID
)iface
;
644 IUnknown_AddRef((IUnknown
*)(*ppv
));
648 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
650 return E_NOINTERFACE
;
653 ULONG WINAPI
OutputPin_Release(IPin
* iface
)
655 ICOM_THIS(OutputPin
, iface
);
657 TRACE("(%p/%p)->()\n", This
, iface
);
659 if (!InterlockedDecrement(&This
->pin
.refCount
))
661 DeleteMediaType(&This
->pin
.mtCurrent
);
665 return This
->pin
.refCount
;
668 HRESULT WINAPI
OutputPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
671 ICOM_THIS(OutputPin
, iface
);
673 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
674 dump_AM_MEDIA_TYPE(pmt
);
676 /* If we try to connect to ourself, we will definitely deadlock.
677 * There are other cases where we could deadlock too, but this
678 * catches the obvious case */
679 assert(pReceivePin
!= iface
);
681 EnterCriticalSection(This
->pin
.pCritSec
);
683 /* if we have been a specific type to connect with, then we can either connect
684 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
685 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
686 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
689 /* negotiate media type */
691 IEnumMediaTypes
* pEnumCandidates
;
692 AM_MEDIA_TYPE
* pmtCandidate
; /* Candidate media type */
694 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
696 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
698 /* try this filter's media types first */
699 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
701 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
702 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
705 CoTaskMemFree(pmtCandidate
);
708 CoTaskMemFree(pmtCandidate
);
710 IEnumMediaTypes_Release(pEnumCandidates
);
713 /* then try receiver filter's media types */
714 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
716 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
718 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
720 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
721 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
724 CoTaskMemFree(pmtCandidate
);
727 CoTaskMemFree(pmtCandidate
);
729 IEnumMediaTypes_Release(pEnumCandidates
);
731 } /* if negotiate media type */
733 LeaveCriticalSection(This
->pin
.pCritSec
);
735 FIXME(" -- %lx\n", hr
);
739 HRESULT WINAPI
OutputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
741 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
746 HRESULT WINAPI
OutputPin_Disconnect(IPin
* iface
)
749 ICOM_THIS(OutputPin
, iface
);
753 EnterCriticalSection(This
->pin
.pCritSec
);
755 if (This
->pMemInputPin
)
757 IMemInputPin_Release(This
->pMemInputPin
);
758 This
->pMemInputPin
= NULL
;
760 if (This
->pin
.pConnectedTo
)
762 IPin_Release(This
->pin
.pConnectedTo
);
763 This
->pin
.pConnectedTo
= NULL
;
769 LeaveCriticalSection(This
->pin
.pCritSec
);
774 HRESULT WINAPI
OutputPin_EndOfStream(IPin
* iface
)
778 /* not supposed to do anything in an output pin */
783 HRESULT WINAPI
OutputPin_BeginFlush(IPin
* iface
)
785 TRACE("(%p)->()\n", iface
);
787 /* not supposed to do anything in an output pin */
792 HRESULT WINAPI
OutputPin_EndFlush(IPin
* iface
)
794 TRACE("(%p)->()\n", iface
);
796 /* not supposed to do anything in an output pin */
801 HRESULT WINAPI
OutputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
803 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface
, (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
805 /* not supposed to do anything in an output pin */
810 static const IPinVtbl OutputPin_Vtbl
=
812 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
813 OutputPin_QueryInterface
,
817 OutputPin_ReceiveConnection
,
818 OutputPin_Disconnect
,
819 IPinImpl_ConnectedTo
,
820 IPinImpl_ConnectionMediaType
,
821 IPinImpl_QueryPinInfo
,
822 IPinImpl_QueryDirection
,
824 IPinImpl_QueryAccept
,
825 IPinImpl_EnumMediaTypes
,
826 IPinImpl_QueryInternalConnections
,
827 OutputPin_EndOfStream
,
828 OutputPin_BeginFlush
,
833 HRESULT
OutputPin_GetDeliveryBuffer(OutputPin
* This
, IMediaSample
** ppSample
, const REFERENCE_TIME
* tStart
, const REFERENCE_TIME
* tStop
, DWORD dwFlags
)
837 TRACE("(%p, %p, %p, %lx)\n", ppSample
, tStart
, tStop
, dwFlags
);
839 EnterCriticalSection(This
->pin
.pCritSec
);
841 if (!This
->pin
.pConnectedTo
)
842 hr
= VFW_E_NOT_CONNECTED
;
845 IMemInputPin
* pMemInputPin
= NULL
;
846 IMemAllocator
* pAlloc
= NULL
;
848 /* FIXME: should we cache this? */
849 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemInputPin
);
852 hr
= IMemInputPin_GetAllocator(pMemInputPin
, &pAlloc
);
855 hr
= IMemAllocator_GetBuffer(pAlloc
, ppSample
, (REFERENCE_TIME
*)tStart
, (REFERENCE_TIME
*)tStop
, dwFlags
);
858 hr
= IMediaSample_SetTime(*ppSample
, (REFERENCE_TIME
*)tStart
, (REFERENCE_TIME
*)tStop
);
861 IMemAllocator_Release(pAlloc
);
863 IMemInputPin_Release(pMemInputPin
);
866 LeaveCriticalSection(This
->pin
.pCritSec
);
871 HRESULT
OutputPin_SendSample(OutputPin
* This
, IMediaSample
* pSample
)
874 IMemInputPin
* pMemConnected
= NULL
;
876 EnterCriticalSection(This
->pin
.pCritSec
);
878 if (!This
->pin
.pConnectedTo
)
879 hr
= VFW_E_NOT_CONNECTED
;
882 /* FIXME: should we cache this? - if we do, then we need to keep the local version
883 * and AddRef here instead */
884 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemConnected
);
887 LeaveCriticalSection(This
->pin
.pCritSec
);
889 if (SUCCEEDED(hr
) && pMemConnected
)
891 /* NOTE: if we are in a critical section when Receive is called
892 * then it causes some problems (most notably with the native Video
893 * Renderer) if we are re-entered for whatever reason */
894 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
895 IMemInputPin_Release(pMemConnected
);
901 HRESULT
OutputPin_DeliverNewSegment(OutputPin
* This
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
905 EnterCriticalSection(This
->pin
.pCritSec
);
907 if (!This
->pin
.pConnectedTo
)
908 hr
= VFW_E_NOT_CONNECTED
;
910 hr
= IPin_NewSegment(This
->pin
.pConnectedTo
, tStart
, tStop
, dRate
);
912 LeaveCriticalSection(This
->pin
.pCritSec
);
917 HRESULT
OutputPin_CommitAllocator(OutputPin
* This
)
921 TRACE("(%p)->()\n", This
);
923 EnterCriticalSection(This
->pin
.pCritSec
);
925 if (!This
->pin
.pConnectedTo
)
926 hr
= VFW_E_NOT_CONNECTED
;
929 IMemInputPin
* pMemInputPin
= NULL
;
930 IMemAllocator
* pAlloc
= NULL
;
931 /* FIXME: should we cache this? */
932 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemInputPin
);
935 hr
= IMemInputPin_GetAllocator(pMemInputPin
, &pAlloc
);
938 hr
= IMemAllocator_Commit(pAlloc
);
941 IMemAllocator_Release(pAlloc
);
944 IMemInputPin_Release(pMemInputPin
);
947 LeaveCriticalSection(This
->pin
.pCritSec
);
952 HRESULT
OutputPin_DeliverDisconnect(OutputPin
* This
)
956 TRACE("(%p)->()\n", This
);
958 EnterCriticalSection(This
->pin
.pCritSec
);
960 if (!This
->pin
.pConnectedTo
)
961 hr
= VFW_E_NOT_CONNECTED
;
964 IMemInputPin
* pMemInputPin
= NULL
;
965 IMemAllocator
* pAlloc
= NULL
;
966 /* FIXME: should we cache this? */
967 hr
= IPin_QueryInterface(This
->pin
.pConnectedTo
, &IID_IMemInputPin
, (LPVOID
*)&pMemInputPin
);
970 hr
= IMemInputPin_GetAllocator(pMemInputPin
, &pAlloc
);
973 hr
= IMemAllocator_Decommit(pAlloc
);
976 IMemAllocator_Release(pAlloc
);
979 IMemInputPin_Release(pMemInputPin
);
982 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
985 LeaveCriticalSection(This
->pin
.pCritSec
);
991 HRESULT
PullPin_Construct(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
997 if (pPinInfo
->dir
!= PINDIR_INPUT
)
999 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1000 return E_INVALIDARG
;
1003 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
1006 return E_OUTOFMEMORY
;
1008 if (SUCCEEDED(PullPin_Init(pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
1010 pPinImpl
->pin
.lpVtbl
= &PullPin_Vtbl
;
1012 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
1018 HRESULT
PullPin_Init(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, PullPin
* pPinImpl
)
1020 /* Common attributes */
1021 pPinImpl
->pin
.refCount
= 1;
1022 pPinImpl
->pin
.pConnectedTo
= NULL
;
1023 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
1024 pPinImpl
->pin
.pUserData
= pUserData
;
1025 pPinImpl
->pin
.pCritSec
= pCritSec
;
1026 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1028 /* Input pin attributes */
1029 pPinImpl
->fnSampleProc
= pSampleProc
;
1030 pPinImpl
->fnPreConnect
= NULL
;
1031 pPinImpl
->pAlloc
= NULL
;
1032 pPinImpl
->pReader
= NULL
;
1033 pPinImpl
->hThread
= NULL
;
1034 pPinImpl
->hEventStateChanged
= CreateEventW(NULL
, FALSE
, TRUE
, NULL
);
1036 pPinImpl
->rtStart
= 0;
1037 pPinImpl
->rtStop
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
1042 HRESULT WINAPI
PullPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
1044 PIN_DIRECTION pindirReceive
;
1046 ICOM_THIS(PullPin
, iface
);
1048 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1049 dump_AM_MEDIA_TYPE(pmt
);
1051 EnterCriticalSection(This
->pin
.pCritSec
);
1053 if (This
->pin
.pConnectedTo
)
1054 hr
= VFW_E_ALREADY_CONNECTED
;
1056 if (SUCCEEDED(hr
) && (This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
))
1057 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
1058 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1062 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1064 if (pindirReceive
!= PINDIR_OUTPUT
)
1066 ERR("Can't connect from non-output pin\n");
1067 hr
= VFW_E_INVALID_DIRECTION
;
1073 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1078 ALLOCATOR_PROPERTIES props
;
1080 props
.cbBuffer
= 64 * 1024; /* 64k bytes */
1083 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1086 if (SUCCEEDED(hr
) && This
->fnPreConnect
)
1088 hr
= This
->fnPreConnect(iface
, pReceivePin
);
1093 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1094 This
->pin
.pConnectedTo
= pReceivePin
;
1095 IPin_AddRef(pReceivePin
);
1098 LeaveCriticalSection(This
->pin
.pCritSec
);
1102 HRESULT WINAPI
PullPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1104 ICOM_THIS(PullPin
, iface
);
1106 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
1110 if (IsEqualIID(riid
, &IID_IUnknown
))
1111 *ppv
= (LPVOID
)iface
;
1112 else if (IsEqualIID(riid
, &IID_IPin
))
1113 *ppv
= (LPVOID
)iface
;
1117 IUnknown_AddRef((IUnknown
*)(*ppv
));
1121 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
1123 return E_NOINTERFACE
;
1126 ULONG WINAPI
PullPin_Release(IPin
* iface
)
1128 ICOM_THIS(PullPin
, iface
);
1130 TRACE("(%p/%p)->()\n", This
, iface
);
1132 if (!InterlockedDecrement(&This
->pin
.refCount
))
1135 PullPin_StopProcessing(This
);
1136 IMemAllocator_Release(This
->pAlloc
);
1137 IAsyncReader_Release(This
->pReader
);
1138 CloseHandle(This
->hEventStateChanged
);
1139 CoTaskMemFree(This
);
1142 return This
->pin
.refCount
;
1145 static DWORD WINAPI
PullPin_Thread_Main(LPVOID pv
)
1148 SleepEx(INFINITE
, TRUE
);
1151 static void CALLBACK
PullPin_Thread_Process(ULONG_PTR iface
)
1153 ICOM_THIS(PullPin
, iface
);
1156 REFERENCE_TIME rtCurrent
;
1157 ALLOCATOR_PROPERTIES allocProps
;
1159 SetEvent(This
->hEventStateChanged
);
1161 hr
= IMemAllocator_GetProperties(This
->pAlloc
, &allocProps
);
1163 rtCurrent
= MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This
->rtStart
), allocProps
.cbAlign
));
1165 while (rtCurrent
< This
->rtStop
)
1167 /* FIXME: to improve performance by quite a bit this should be changed
1168 * so that one sample is processed while one sample is fetched. However,
1169 * it is harder to debug so for the moment it will stay as it is */
1170 IMediaSample
* pSample
= NULL
;
1171 REFERENCE_TIME rtSampleStop
;
1174 hr
= IMemAllocator_GetBuffer(This
->pAlloc
, &pSample
, NULL
, NULL
, 0);
1178 rtSampleStop
= rtCurrent
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample
));
1179 if (rtSampleStop
> This
->rtStop
)
1180 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This
->rtStop
), allocProps
.cbAlign
));
1181 hr
= IMediaSample_SetTime(pSample
, &rtCurrent
, &rtSampleStop
);
1182 rtCurrent
= rtSampleStop
;
1186 hr
= IAsyncReader_Request(This
->pReader
, pSample
, (ULONG_PTR
)0);
1189 hr
= IAsyncReader_WaitForNext(This
->pReader
, 10000, &pSample
, &dwUser
);
1192 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
1194 ERR("Processing error: %lx\n", hr
);
1197 IMediaSample_Release(pSample
);
1201 static void CALLBACK
PullPin_Thread_Stop(ULONG_PTR iface
)
1203 ICOM_THIS(PullPin
, iface
);
1205 TRACE("(%p/%p)->()\n", This
, (LPVOID
)iface
);
1207 EnterCriticalSection(This
->pin
.pCritSec
);
1211 CloseHandle(This
->hThread
);
1212 This
->hThread
= NULL
;
1213 if (FAILED(hr
= IMemAllocator_Decommit(This
->pAlloc
)))
1214 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr
);
1216 LeaveCriticalSection(This
->pin
.pCritSec
);
1218 SetEvent(This
->hEventStateChanged
);
1223 HRESULT
PullPin_InitProcessing(PullPin
* This
)
1227 TRACE("(%p)->()\n", This
);
1229 assert(!This
->hThread
);
1231 /* if we are connected */
1234 EnterCriticalSection(This
->pin
.pCritSec
);
1237 assert(!This
->hThread
);
1239 This
->hThread
= CreateThread(NULL
, 0, PullPin_Thread_Main
, NULL
, 0, &dwThreadId
);
1241 hr
= HRESULT_FROM_WIN32(GetLastError());
1244 hr
= IMemAllocator_Commit(This
->pAlloc
);
1246 LeaveCriticalSection(This
->pin
.pCritSec
);
1249 TRACE(" -- %lx\n", hr
);
1254 HRESULT
PullPin_StartProcessing(PullPin
* This
)
1256 /* if we are connected */
1257 TRACE("(%p)->()\n", This
);
1260 assert(This
->hThread
);
1262 ResetEvent(This
->hEventStateChanged
);
1264 if (!QueueUserAPC(PullPin_Thread_Process
, This
->hThread
, (ULONG_PTR
)This
))
1265 return HRESULT_FROM_WIN32(GetLastError());
1271 HRESULT
PullPin_PauseProcessing(PullPin
* This
)
1273 /* make the processing function exit its loop */
1279 HRESULT
PullPin_StopProcessing(PullPin
* This
)
1281 /* if we are connected */
1284 assert(This
->hThread
);
1286 ResetEvent(This
->hEventStateChanged
);
1288 PullPin_PauseProcessing(This
);
1290 if (!QueueUserAPC(PullPin_Thread_Stop
, This
->hThread
, (ULONG_PTR
)This
))
1291 return HRESULT_FROM_WIN32(GetLastError());
1297 HRESULT
PullPin_WaitForStateChange(PullPin
* This
, DWORD dwMilliseconds
)
1299 if (WaitForSingleObject(This
->hEventStateChanged
, dwMilliseconds
) == WAIT_TIMEOUT
)
1304 HRESULT
PullPin_Seek(PullPin
* This
, REFERENCE_TIME rtStart
, REFERENCE_TIME rtStop
)
1306 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This
, (LONG
)(rtStart
>> 32), (LONG
)rtStart
, (LONG
)(rtStop
>> 32), (LONG
)rtStop
);
1308 PullPin_BeginFlush((IPin
*)This
);
1309 /* FIXME: need critical section? */
1310 This
->rtStart
= rtStart
;
1311 This
->rtStop
= rtStop
;
1312 PullPin_EndFlush((IPin
*)This
);
1317 HRESULT WINAPI
PullPin_EndOfStream(IPin
* iface
)
1319 FIXME("(%p)->()\n", iface
);
1323 HRESULT WINAPI
PullPin_BeginFlush(IPin
* iface
)
1325 FIXME("(%p)->()\n", iface
);
1329 HRESULT WINAPI
PullPin_EndFlush(IPin
* iface
)
1331 FIXME("(%p)->()\n", iface
);
1335 HRESULT WINAPI
PullPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1337 FIXME("(%p)->(%s, %s, %g)\n", iface
, wine_dbgstr_longlong(tStart
), wine_dbgstr_longlong(tStop
), dRate
);
1341 static const IPinVtbl PullPin_Vtbl
=
1343 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1344 PullPin_QueryInterface
,
1348 PullPin_ReceiveConnection
,
1349 IPinImpl_Disconnect
,
1350 IPinImpl_ConnectedTo
,
1351 IPinImpl_ConnectionMediaType
,
1352 IPinImpl_QueryPinInfo
,
1353 IPinImpl_QueryDirection
,
1355 IPinImpl_QueryAccept
,
1356 IPinImpl_EnumMediaTypes
,
1357 IPinImpl_QueryInternalConnections
,
1358 PullPin_EndOfStream
,