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"
24 #include "wine/debug.h"
25 #include "wine/unicode.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 static inline InputPin
*impl_from_IMemInputPin( IMemInputPin
*iface
)
42 return (InputPin
*)((char*)iface
- FIELD_OFFSET(InputPin
, lpVtblMemInput
));
46 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
48 /* Tempting to just do a memcpy, but the name field is
49 128 characters long! We will probably never exceed 10
50 most of the time, so we are better off copying
51 each field manually */
52 strcpyW(pDest
->achName
, pSrc
->achName
);
53 pDest
->dir
= pSrc
->dir
;
54 pDest
->pFilter
= pSrc
->pFilter
;
57 /* Function called as a helper to IPin_Connect */
58 /* specific AM_MEDIA_TYPE - it cannot be NULL */
59 /* NOTE: not part of standard interface */
60 static HRESULT
OutputPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
62 OutputPin
*This
= (OutputPin
*)iface
;
64 IMemAllocator
* pMemAlloc
= NULL
;
65 ALLOCATOR_PROPERTIES actual
; /* FIXME: should we put the actual props back in to This? */
67 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
68 dump_AM_MEDIA_TYPE(pmt
);
70 /* FIXME: call queryacceptproc */
72 This
->pin
.pConnectedTo
= pReceivePin
;
73 IPin_AddRef(pReceivePin
);
74 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
76 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
78 /* get the IMemInputPin interface we will use to deliver samples to the
82 This
->pMemInputPin
= NULL
;
83 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
87 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pMemAlloc
);
89 if (hr
== VFW_E_NO_ALLOCATOR
)
91 /* Input pin provides no allocator, use standard memory allocator */
92 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMemAllocator
, (LPVOID
*)&pMemAlloc
);
96 hr
= IMemInputPin_NotifyAllocator(This
->pMemInputPin
, pMemAlloc
, FALSE
);
101 hr
= IMemAllocator_SetProperties(pMemAlloc
, &This
->allocProps
, &actual
);
104 IMemAllocator_Release(pMemAlloc
);
107 /* break connection if we couldn't get the allocator */
110 if (This
->pMemInputPin
)
111 IMemInputPin_Release(This
->pMemInputPin
);
112 This
->pMemInputPin
= NULL
;
114 IPin_Disconnect(pReceivePin
);
120 IPin_Release(This
->pin
.pConnectedTo
);
121 This
->pin
.pConnectedTo
= NULL
;
122 FreeMediaType(&This
->pin
.mtCurrent
);
125 TRACE(" -- %x\n", hr
);
129 HRESULT
InputPin_Construct(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
135 if (pPinInfo
->dir
!= PINDIR_INPUT
)
137 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
141 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
144 return E_OUTOFMEMORY
;
146 if (SUCCEEDED(InputPin_Init(pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
148 pPinImpl
->pin
.lpVtbl
= &InputPin_Vtbl
;
149 pPinImpl
->lpVtblMemInput
= &MemInputPin_Vtbl
;
151 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
157 /* Note that we don't init the vtables here (like C++ constructor) */
158 HRESULT
InputPin_Init(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, InputPin
* pPinImpl
)
162 /* Common attributes */
163 pPinImpl
->pin
.refCount
= 1;
164 pPinImpl
->pin
.pConnectedTo
= NULL
;
165 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
166 pPinImpl
->pin
.pUserData
= pUserData
;
167 pPinImpl
->pin
.pCritSec
= pCritSec
;
168 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
169 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
171 /* Input pin attributes */
172 pPinImpl
->fnSampleProc
= pSampleProc
;
173 pPinImpl
->pAllocator
= NULL
;
174 pPinImpl
->tStart
= 0;
181 HRESULT
OutputPin_Init(const PIN_INFO
* pPinInfo
, const ALLOCATOR_PROPERTIES
* props
, LPVOID pUserData
,
182 QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, OutputPin
* pPinImpl
)
186 /* Common attributes */
187 pPinImpl
->pin
.lpVtbl
= &OutputPin_Vtbl
;
188 pPinImpl
->pin
.refCount
= 1;
189 pPinImpl
->pin
.pConnectedTo
= NULL
;
190 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
191 pPinImpl
->pin
.pUserData
= pUserData
;
192 pPinImpl
->pin
.pCritSec
= pCritSec
;
193 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
194 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
196 /* Output pin attributes */
197 pPinImpl
->pMemInputPin
= NULL
;
198 pPinImpl
->pConnectSpecific
= OutputPin_ConnectSpecific
;
201 memcpy(&pPinImpl
->allocProps
, props
, sizeof(pPinImpl
->allocProps
));
202 if (pPinImpl
->allocProps
.cbAlign
== 0)
203 pPinImpl
->allocProps
.cbAlign
= 1;
206 ZeroMemory(&pPinImpl
->allocProps
, sizeof(pPinImpl
->allocProps
));
211 HRESULT
OutputPin_Construct(const PIN_INFO
* pPinInfo
, ALLOCATOR_PROPERTIES
*props
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
213 OutputPin
* pPinImpl
;
217 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
219 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
223 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
226 return E_OUTOFMEMORY
;
228 if (SUCCEEDED(OutputPin_Init(pPinInfo
, props
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
230 pPinImpl
->pin
.lpVtbl
= &OutputPin_Vtbl
;
232 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
238 /*** Common pin functions ***/
240 ULONG WINAPI
IPinImpl_AddRef(IPin
* iface
)
242 IPinImpl
*This
= (IPinImpl
*)iface
;
243 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
245 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
250 HRESULT WINAPI
IPinImpl_Disconnect(IPin
* iface
)
253 IPinImpl
*This
= (IPinImpl
*)iface
;
257 EnterCriticalSection(This
->pCritSec
);
259 if (This
->pConnectedTo
)
261 IPin_Release(This
->pConnectedTo
);
262 This
->pConnectedTo
= NULL
;
268 LeaveCriticalSection(This
->pCritSec
);
273 HRESULT WINAPI
IPinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
276 IPinImpl
*This
= (IPinImpl
*)iface
;
278 /* TRACE("(%p)\n", ppPin);*/
280 EnterCriticalSection(This
->pCritSec
);
282 if (This
->pConnectedTo
)
284 *ppPin
= This
->pConnectedTo
;
289 hr
= VFW_E_NOT_CONNECTED
;
291 LeaveCriticalSection(This
->pCritSec
);
296 HRESULT WINAPI
IPinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
299 IPinImpl
*This
= (IPinImpl
*)iface
;
301 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
303 EnterCriticalSection(This
->pCritSec
);
305 if (This
->pConnectedTo
)
307 CopyMediaType(pmt
, &This
->mtCurrent
);
312 ZeroMemory(pmt
, sizeof(*pmt
));
313 hr
= VFW_E_NOT_CONNECTED
;
316 LeaveCriticalSection(This
->pCritSec
);
321 HRESULT WINAPI
IPinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
323 IPinImpl
*This
= (IPinImpl
*)iface
;
325 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
327 Copy_PinInfo(pInfo
, &This
->pinInfo
);
328 IBaseFilter_AddRef(pInfo
->pFilter
);
333 HRESULT WINAPI
IPinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
335 IPinImpl
*This
= (IPinImpl
*)iface
;
337 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
339 *pPinDir
= This
->pinInfo
.dir
;
344 HRESULT WINAPI
IPinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
346 IPinImpl
*This
= (IPinImpl
*)iface
;
348 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
350 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
352 return E_OUTOFMEMORY
;
354 strcpyW(*Id
, This
->pinInfo
.achName
);
359 HRESULT WINAPI
IPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
361 IPinImpl
*This
= (IPinImpl
*)iface
;
363 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
365 return (This
->fnQueryAccept(This
->pUserData
, pmt
) == S_OK
? S_OK
: S_FALSE
);
368 HRESULT WINAPI
IPinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
370 IPinImpl
*This
= (IPinImpl
*)iface
;
371 ENUMMEDIADETAILS emd
;
373 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
375 /* override this method to allow enumeration of your types */
377 emd
.pMediaTypes
= NULL
;
379 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
382 HRESULT WINAPI
IPinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
384 IPinImpl
*This
= (IPinImpl
*)iface
;
386 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
388 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
391 /*** IPin implementation for an input pin ***/
393 HRESULT WINAPI
InputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
395 InputPin
*This
= (InputPin
*)iface
;
397 TRACE("(%p)->(%s, %p)\n", iface
, qzdebugstr_guid(riid
), ppv
);
401 if (IsEqualIID(riid
, &IID_IUnknown
))
402 *ppv
= (LPVOID
)iface
;
403 else if (IsEqualIID(riid
, &IID_IPin
))
404 *ppv
= (LPVOID
)iface
;
405 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
406 *ppv
= (LPVOID
)&This
->lpVtblMemInput
;
410 IUnknown_AddRef((IUnknown
*)(*ppv
));
414 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
416 return E_NOINTERFACE
;
419 ULONG WINAPI
InputPin_Release(IPin
* iface
)
421 InputPin
*This
= (InputPin
*)iface
;
422 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
424 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
428 FreeMediaType(&This
->pin
.mtCurrent
);
429 if (This
->pAllocator
)
430 IMemAllocator_Release(This
->pAllocator
);
438 HRESULT WINAPI
InputPin_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
440 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
446 HRESULT WINAPI
InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
448 InputPin
*This
= (InputPin
*)iface
;
449 PIN_DIRECTION pindirReceive
;
452 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
453 dump_AM_MEDIA_TYPE(pmt
);
455 EnterCriticalSection(This
->pin
.pCritSec
);
457 if (This
->pin
.pConnectedTo
)
458 hr
= VFW_E_ALREADY_CONNECTED
;
460 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
461 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
462 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
466 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
468 if (pindirReceive
!= PINDIR_OUTPUT
)
470 ERR("Can't connect from non-output pin\n");
471 hr
= VFW_E_INVALID_DIRECTION
;
477 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
478 This
->pin
.pConnectedTo
= pReceivePin
;
479 IPin_AddRef(pReceivePin
);
482 LeaveCriticalSection(This
->pin
.pCritSec
);
487 HRESULT WINAPI
InputPin_EndOfStream(IPin
* iface
)
494 HRESULT WINAPI
InputPin_BeginFlush(IPin
* iface
)
500 HRESULT WINAPI
InputPin_EndFlush(IPin
* iface
)
506 HRESULT WINAPI
InputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
508 InputPin
*This
= (InputPin
*)iface
;
510 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
512 This
->tStart
= tStart
;
519 static const IPinVtbl InputPin_Vtbl
=
521 InputPin_QueryInterface
,
525 InputPin_ReceiveConnection
,
527 IPinImpl_ConnectedTo
,
528 IPinImpl_ConnectionMediaType
,
529 IPinImpl_QueryPinInfo
,
530 IPinImpl_QueryDirection
,
532 IPinImpl_QueryAccept
,
533 IPinImpl_EnumMediaTypes
,
534 IPinImpl_QueryInternalConnections
,
535 InputPin_EndOfStream
,
541 /*** IMemInputPin implementation ***/
543 HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
545 InputPin
*This
= impl_from_IMemInputPin(iface
);
547 return IPin_QueryInterface((IPin
*)&This
->pin
, riid
, ppv
);
550 ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
552 InputPin
*This
= impl_from_IMemInputPin(iface
);
554 return IPin_AddRef((IPin
*)&This
->pin
);
557 ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
559 InputPin
*This
= impl_from_IMemInputPin(iface
);
561 return IPin_Release((IPin
*)&This
->pin
);
564 HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
566 InputPin
*This
= impl_from_IMemInputPin(iface
);
568 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
570 *ppAllocator
= This
->pAllocator
;
572 IMemAllocator_AddRef(*ppAllocator
);
574 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
577 HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
579 InputPin
*This
= impl_from_IMemInputPin(iface
);
581 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
583 if (This
->pAllocator
)
584 IMemAllocator_Release(This
->pAllocator
);
585 This
->pAllocator
= pAllocator
;
586 if (This
->pAllocator
)
587 IMemAllocator_AddRef(This
->pAllocator
);
592 HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
594 InputPin
*This
= impl_from_IMemInputPin(iface
);
596 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
598 /* override this method if you have any specific requirements */
603 HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
605 InputPin
*This
= impl_from_IMemInputPin(iface
);
607 /* this trace commented out for performance reasons */
608 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
610 return This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
613 HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, long nSamples
, long *nSamplesProcessed
)
616 InputPin
*This
= impl_from_IMemInputPin(iface
);
618 TRACE("(%p/%p)->(%p, %ld, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
620 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
622 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
630 HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
632 InputPin
*This
= impl_from_IMemInputPin(iface
);
634 FIXME("(%p/%p)->()\n", This
, iface
);
636 /* FIXME: we should check whether any output pins will block */
641 static const IMemInputPinVtbl MemInputPin_Vtbl
=
643 MemInputPin_QueryInterface
,
646 MemInputPin_GetAllocator
,
647 MemInputPin_NotifyAllocator
,
648 MemInputPin_GetAllocatorRequirements
,
650 MemInputPin_ReceiveMultiple
,
651 MemInputPin_ReceiveCanBlock
654 HRESULT WINAPI
OutputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
656 OutputPin
*This
= (OutputPin
*)iface
;
658 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
662 if (IsEqualIID(riid
, &IID_IUnknown
))
663 *ppv
= (LPVOID
)iface
;
664 else if (IsEqualIID(riid
, &IID_IPin
))
665 *ppv
= (LPVOID
)iface
;
669 IUnknown_AddRef((IUnknown
*)(*ppv
));
673 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
675 return E_NOINTERFACE
;
678 ULONG WINAPI
OutputPin_Release(IPin
* iface
)
680 OutputPin
*This
= (OutputPin
*)iface
;
681 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
683 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
687 FreeMediaType(&This
->pin
.mtCurrent
);
694 HRESULT WINAPI
OutputPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
697 OutputPin
*This
= (OutputPin
*)iface
;
699 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
700 dump_AM_MEDIA_TYPE(pmt
);
702 /* If we try to connect to ourself, we will definitely deadlock.
703 * There are other cases where we could deadlock too, but this
704 * catches the obvious case */
705 assert(pReceivePin
!= iface
);
707 EnterCriticalSection(This
->pin
.pCritSec
);
709 /* if we have been a specific type to connect with, then we can either connect
710 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
711 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
712 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
715 /* negotiate media type */
717 IEnumMediaTypes
* pEnumCandidates
;
718 AM_MEDIA_TYPE
* pmtCandidate
; /* Candidate media type */
720 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
722 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
724 /* try this filter's media types first */
725 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
727 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
728 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
731 CoTaskMemFree(pmtCandidate
);
734 CoTaskMemFree(pmtCandidate
);
736 IEnumMediaTypes_Release(pEnumCandidates
);
739 /* then try receiver filter's media types */
740 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
742 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
744 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
746 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
747 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
750 CoTaskMemFree(pmtCandidate
);
753 CoTaskMemFree(pmtCandidate
);
755 IEnumMediaTypes_Release(pEnumCandidates
);
757 } /* if negotiate media type */
759 LeaveCriticalSection(This
->pin
.pCritSec
);
761 TRACE(" -- %x\n", hr
);
765 HRESULT WINAPI
OutputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
767 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
772 HRESULT WINAPI
OutputPin_Disconnect(IPin
* iface
)
775 OutputPin
*This
= (OutputPin
*)iface
;
779 EnterCriticalSection(This
->pin
.pCritSec
);
781 if (This
->pMemInputPin
)
783 IMemInputPin_Release(This
->pMemInputPin
);
784 This
->pMemInputPin
= NULL
;
786 if (This
->pin
.pConnectedTo
)
788 IPin_Release(This
->pin
.pConnectedTo
);
789 This
->pin
.pConnectedTo
= NULL
;
795 LeaveCriticalSection(This
->pin
.pCritSec
);
800 HRESULT WINAPI
OutputPin_EndOfStream(IPin
* iface
)
804 /* not supposed to do anything in an output pin */
809 HRESULT WINAPI
OutputPin_BeginFlush(IPin
* iface
)
811 TRACE("(%p)->()\n", iface
);
813 /* not supposed to do anything in an output pin */
818 HRESULT WINAPI
OutputPin_EndFlush(IPin
* iface
)
820 TRACE("(%p)->()\n", iface
);
822 /* not supposed to do anything in an output pin */
827 HRESULT WINAPI
OutputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
829 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface
, (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
831 /* not supposed to do anything in an output pin */
836 static const IPinVtbl OutputPin_Vtbl
=
838 OutputPin_QueryInterface
,
842 OutputPin_ReceiveConnection
,
843 OutputPin_Disconnect
,
844 IPinImpl_ConnectedTo
,
845 IPinImpl_ConnectionMediaType
,
846 IPinImpl_QueryPinInfo
,
847 IPinImpl_QueryDirection
,
849 IPinImpl_QueryAccept
,
850 IPinImpl_EnumMediaTypes
,
851 IPinImpl_QueryInternalConnections
,
852 OutputPin_EndOfStream
,
853 OutputPin_BeginFlush
,
858 HRESULT
OutputPin_GetDeliveryBuffer(OutputPin
* This
, IMediaSample
** ppSample
, REFERENCE_TIME
* tStart
, REFERENCE_TIME
* tStop
, DWORD dwFlags
)
862 TRACE("(%p, %p, %p, %x)\n", ppSample
, tStart
, tStop
, dwFlags
);
864 EnterCriticalSection(This
->pin
.pCritSec
);
866 if (!This
->pin
.pConnectedTo
)
867 hr
= VFW_E_NOT_CONNECTED
;
870 IMemAllocator
* pAlloc
= NULL
;
872 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
875 hr
= IMemAllocator_GetBuffer(pAlloc
, ppSample
, tStart
, tStop
, dwFlags
);
878 hr
= IMediaSample_SetTime(*ppSample
, tStart
, tStop
);
881 IMemAllocator_Release(pAlloc
);
884 LeaveCriticalSection(This
->pin
.pCritSec
);
889 HRESULT
OutputPin_SendSample(OutputPin
* This
, IMediaSample
* pSample
)
892 IMemInputPin
* pMemConnected
= NULL
;
895 EnterCriticalSection(This
->pin
.pCritSec
);
897 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
898 hr
= VFW_E_NOT_CONNECTED
;
901 /* we don't have the lock held when using This->pMemInputPin,
902 * so we need to AddRef it to stop it being deleted while we are
903 * using it. Same with its filter. */
904 pMemConnected
= This
->pMemInputPin
;
905 IMemInputPin_AddRef(pMemConnected
);
906 hr
= IPin_QueryPinInfo(This
->pin
.pConnectedTo
, &pinInfo
);
909 LeaveCriticalSection(This
->pin
.pCritSec
);
913 /* NOTE: if we are in a critical section when Receive is called
914 * then it causes some problems (most notably with the native Video
915 * Renderer) if we are re-entered for whatever reason */
916 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
918 /* If the filter's destroyed, tell upstream to stop sending data */
919 if(IBaseFilter_Release(pinInfo
.pFilter
) == 0 && SUCCEEDED(hr
))
923 IMemInputPin_Release(pMemConnected
);
928 HRESULT
OutputPin_DeliverNewSegment(OutputPin
* This
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
932 EnterCriticalSection(This
->pin
.pCritSec
);
934 if (!This
->pin
.pConnectedTo
)
935 hr
= VFW_E_NOT_CONNECTED
;
937 hr
= IPin_NewSegment(This
->pin
.pConnectedTo
, tStart
, tStop
, dRate
);
939 LeaveCriticalSection(This
->pin
.pCritSec
);
944 HRESULT
OutputPin_CommitAllocator(OutputPin
* This
)
948 TRACE("(%p)->()\n", This
);
950 EnterCriticalSection(This
->pin
.pCritSec
);
952 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
953 hr
= VFW_E_NOT_CONNECTED
;
956 IMemAllocator
* pAlloc
= NULL
;
958 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
961 hr
= IMemAllocator_Commit(pAlloc
);
964 IMemAllocator_Release(pAlloc
);
967 LeaveCriticalSection(This
->pin
.pCritSec
);
972 HRESULT
OutputPin_DeliverDisconnect(OutputPin
* This
)
976 TRACE("(%p)->()\n", This
);
978 EnterCriticalSection(This
->pin
.pCritSec
);
980 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
981 hr
= VFW_E_NOT_CONNECTED
;
984 IMemAllocator
* pAlloc
= NULL
;
986 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
989 hr
= IMemAllocator_Decommit(pAlloc
);
992 IMemAllocator_Release(pAlloc
);
995 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
998 LeaveCriticalSection(This
->pin
.pCritSec
);
1004 HRESULT
PullPin_Construct(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
1010 if (pPinInfo
->dir
!= PINDIR_INPUT
)
1012 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1013 return E_INVALIDARG
;
1016 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
1019 return E_OUTOFMEMORY
;
1021 if (SUCCEEDED(PullPin_Init(pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
1023 pPinImpl
->pin
.lpVtbl
= &PullPin_Vtbl
;
1025 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
1031 HRESULT
PullPin_Init(const PIN_INFO
* pPinInfo
, SAMPLEPROC pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, PullPin
* pPinImpl
)
1033 /* Common attributes */
1034 pPinImpl
->pin
.refCount
= 1;
1035 pPinImpl
->pin
.pConnectedTo
= NULL
;
1036 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
1037 pPinImpl
->pin
.pUserData
= pUserData
;
1038 pPinImpl
->pin
.pCritSec
= pCritSec
;
1039 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1040 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1042 /* Input pin attributes */
1043 pPinImpl
->fnSampleProc
= pSampleProc
;
1044 pPinImpl
->fnPreConnect
= NULL
;
1045 pPinImpl
->pAlloc
= NULL
;
1046 pPinImpl
->pReader
= NULL
;
1047 pPinImpl
->hThread
= NULL
;
1048 pPinImpl
->hEventStateChanged
= CreateEventW(NULL
, FALSE
, TRUE
, NULL
);
1050 pPinImpl
->rtStart
= 0;
1051 pPinImpl
->rtCurrent
= 0;
1052 pPinImpl
->rtStop
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
1057 HRESULT WINAPI
PullPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
1059 PIN_DIRECTION pindirReceive
;
1061 PullPin
*This
= (PullPin
*)iface
;
1063 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1064 dump_AM_MEDIA_TYPE(pmt
);
1066 EnterCriticalSection(This
->pin
.pCritSec
);
1068 if (This
->pin
.pConnectedTo
)
1069 hr
= VFW_E_ALREADY_CONNECTED
;
1071 if (SUCCEEDED(hr
) && (This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
))
1072 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
1073 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1077 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1079 if (pindirReceive
!= PINDIR_OUTPUT
)
1081 ERR("Can't connect from non-output pin\n");
1082 hr
= VFW_E_INVALID_DIRECTION
;
1086 This
->pReader
= NULL
;
1087 This
->pAlloc
= NULL
;
1090 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1095 ALLOCATOR_PROPERTIES props
;
1097 props
.cbBuffer
= 64 * 1024; /* 64k bytes */
1100 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1103 if (SUCCEEDED(hr
) && This
->fnPreConnect
)
1105 hr
= This
->fnPreConnect(iface
, pReceivePin
);
1110 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1111 This
->pin
.pConnectedTo
= pReceivePin
;
1112 IPin_AddRef(pReceivePin
);
1117 IAsyncReader_Release(This
->pReader
);
1118 This
->pReader
= NULL
;
1120 IMemAllocator_Release(This
->pAlloc
);
1121 This
->pAlloc
= NULL
;
1124 LeaveCriticalSection(This
->pin
.pCritSec
);
1128 HRESULT WINAPI
PullPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1130 PullPin
*This
= (PullPin
*)iface
;
1132 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
1136 if (IsEqualIID(riid
, &IID_IUnknown
))
1137 *ppv
= (LPVOID
)iface
;
1138 else if (IsEqualIID(riid
, &IID_IPin
))
1139 *ppv
= (LPVOID
)iface
;
1143 IUnknown_AddRef((IUnknown
*)(*ppv
));
1147 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
1149 return E_NOINTERFACE
;
1152 ULONG WINAPI
PullPin_Release(IPin
* iface
)
1154 PullPin
*This
= (PullPin
*)iface
;
1155 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
1157 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
1162 IMemAllocator_Release(This
->pAlloc
);
1164 IAsyncReader_Release(This
->pReader
);
1165 CloseHandle(This
->hEventStateChanged
);
1166 CoTaskMemFree(This
);
1172 static DWORD WINAPI
PullPin_Thread_Main(LPVOID pv
)
1175 SleepEx(INFINITE
, TRUE
);
1178 static void CALLBACK
PullPin_Thread_Process(ULONG_PTR iface
)
1180 PullPin
*This
= (PullPin
*)iface
;
1183 ALLOCATOR_PROPERTIES allocProps
;
1185 CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1187 SetEvent(This
->hEventStateChanged
);
1189 hr
= IMemAllocator_GetProperties(This
->pAlloc
, &allocProps
);
1191 if (This
->rtCurrent
< This
->rtStart
)
1192 This
->rtCurrent
= MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This
->rtStart
), allocProps
.cbAlign
));
1196 while (This
->rtCurrent
< This
->rtStop
&& hr
== S_OK
)
1198 /* FIXME: to improve performance by quite a bit this should be changed
1199 * so that one sample is processed while one sample is fetched. However,
1200 * it is harder to debug so for the moment it will stay as it is */
1201 IMediaSample
* pSample
= NULL
;
1202 REFERENCE_TIME rtSampleStart
;
1203 REFERENCE_TIME rtSampleStop
;
1206 TRACE("Process sample\n");
1208 hr
= IMemAllocator_GetBuffer(This
->pAlloc
, &pSample
, NULL
, NULL
, 0);
1212 rtSampleStart
= This
->rtCurrent
;
1213 rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample
));
1214 if (rtSampleStop
> This
->rtStop
)
1215 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This
->rtStop
), allocProps
.cbAlign
));
1216 hr
= IMediaSample_SetTime(pSample
, &rtSampleStart
, &rtSampleStop
);
1217 This
->rtCurrent
= rtSampleStop
;
1221 hr
= IAsyncReader_Request(This
->pReader
, pSample
, (ULONG_PTR
)0);
1224 hr
= IAsyncReader_WaitForNext(This
->pReader
, 10000, &pSample
, &dwUser
);
1228 rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample
));
1229 if (rtSampleStop
> This
->rtStop
)
1230 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This
->rtStop
), allocProps
.cbAlign
));
1231 hr
= IMediaSample_SetTime(pSample
, &rtSampleStart
, &rtSampleStop
);
1235 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
1237 ERR("Processing error: %x\n", hr
);
1240 IMediaSample_Release(pSample
);
1248 static void CALLBACK
PullPin_Thread_Stop(ULONG_PTR iface
)
1250 PullPin
*This
= (PullPin
*)iface
;
1252 TRACE("(%p/%p)->()\n", This
, (LPVOID
)iface
);
1254 EnterCriticalSection(This
->pin
.pCritSec
);
1258 CloseHandle(This
->hThread
);
1259 This
->hThread
= NULL
;
1260 if (FAILED(hr
= IMemAllocator_Decommit(This
->pAlloc
)))
1261 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr
);
1263 LeaveCriticalSection(This
->pin
.pCritSec
);
1265 SetEvent(This
->hEventStateChanged
);
1267 IBaseFilter_Release(This
->pin
.pinInfo
.pFilter
);
1272 HRESULT
PullPin_InitProcessing(PullPin
* This
)
1276 TRACE("(%p)->()\n", This
);
1278 assert(!This
->hThread
);
1280 /* if we are connected */
1283 EnterCriticalSection(This
->pin
.pCritSec
);
1286 assert(!This
->hThread
);
1288 /* AddRef the filter to make sure it and it's pins will be around
1289 * as long as the thread */
1290 IBaseFilter_AddRef(This
->pin
.pinInfo
.pFilter
);
1292 This
->hThread
= CreateThread(NULL
, 0, PullPin_Thread_Main
, NULL
, 0, &dwThreadId
);
1295 hr
= HRESULT_FROM_WIN32(GetLastError());
1296 IBaseFilter_Release(This
->pin
.pinInfo
.pFilter
);
1300 hr
= IMemAllocator_Commit(This
->pAlloc
);
1302 LeaveCriticalSection(This
->pin
.pCritSec
);
1305 TRACE(" -- %x\n", hr
);
1310 HRESULT
PullPin_StartProcessing(PullPin
* This
)
1312 /* if we are connected */
1313 TRACE("(%p)->()\n", This
);
1316 assert(This
->hThread
);
1318 ResetEvent(This
->hEventStateChanged
);
1320 if (!QueueUserAPC(PullPin_Thread_Process
, This
->hThread
, (ULONG_PTR
)This
))
1321 return HRESULT_FROM_WIN32(GetLastError());
1327 HRESULT
PullPin_PauseProcessing(PullPin
* This
)
1329 /* make the processing function exit its loop */
1335 HRESULT
PullPin_StopProcessing(PullPin
* This
)
1337 /* if we are connected */
1340 assert(This
->hThread
);
1342 ResetEvent(This
->hEventStateChanged
);
1344 PullPin_PauseProcessing(This
);
1346 if (!QueueUserAPC(PullPin_Thread_Stop
, This
->hThread
, (ULONG_PTR
)This
))
1347 return HRESULT_FROM_WIN32(GetLastError());
1353 HRESULT
PullPin_WaitForStateChange(PullPin
* This
, DWORD dwMilliseconds
)
1355 if (WaitForSingleObject(This
->hEventStateChanged
, dwMilliseconds
) == WAIT_TIMEOUT
)
1360 HRESULT
PullPin_Seek(PullPin
* This
, REFERENCE_TIME rtStart
, REFERENCE_TIME rtStop
)
1362 FIXME("(%p)->(%x%08x, %x%08x)\n", This
, (LONG
)(rtStart
>> 32), (LONG
)rtStart
, (LONG
)(rtStop
>> 32), (LONG
)rtStop
);
1364 PullPin_BeginFlush((IPin
*)This
);
1365 /* FIXME: need critical section? */
1366 This
->rtStart
= rtStart
;
1367 This
->rtStop
= rtStop
;
1368 PullPin_EndFlush((IPin
*)This
);
1373 HRESULT WINAPI
PullPin_EndOfStream(IPin
* iface
)
1375 FIXME("(%p)->()\n", iface
);
1379 HRESULT WINAPI
PullPin_BeginFlush(IPin
* iface
)
1381 FIXME("(%p)->()\n", iface
);
1385 HRESULT WINAPI
PullPin_EndFlush(IPin
* iface
)
1387 FIXME("(%p)->()\n", iface
);
1391 HRESULT WINAPI
PullPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1393 FIXME("(%p)->(%s, %s, %g)\n", iface
, wine_dbgstr_longlong(tStart
), wine_dbgstr_longlong(tStop
), dRate
);
1397 static const IPinVtbl PullPin_Vtbl
=
1399 PullPin_QueryInterface
,
1403 PullPin_ReceiveConnection
,
1404 IPinImpl_Disconnect
,
1405 IPinImpl_ConnectedTo
,
1406 IPinImpl_ConnectionMediaType
,
1407 IPinImpl_QueryPinInfo
,
1408 IPinImpl_QueryDirection
,
1410 IPinImpl_QueryAccept
,
1411 IPinImpl_EnumMediaTypes
,
1412 IPinImpl_QueryInternalConnections
,
1413 PullPin_EndOfStream
,