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 typedef HRESULT (*SendPinFunc
)( IPin
*to
, LPVOID arg
);
42 /** Helper function, there are a lot of places where the error code is inherited
43 * The following rules apply:
45 * Return the first received error code (E_NOTIMPL is ignored)
46 * If no errors occur: return the first received non-error-code that isn't S_OK
48 HRESULT
updatehres( HRESULT original
, HRESULT
new )
50 if (FAILED( original
) || new == E_NOTIMPL
)
53 if (FAILED( new ) || original
== S_OK
)
59 /** Sends a message from a pin further to other, similar pins
60 * fnMiddle is called on each pin found further on the stream.
61 * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
63 * If the pin given is an input pin, the message will be sent downstream to other input pins
64 * If the pin given is an output pin, the message will be sent upstream to other output pins
66 static HRESULT
SendFurther( IPin
*from
, SendPinFunc fnMiddle
, LPVOID arg
, SendPinFunc fnEnd
)
71 HRESULT hr_return
= S_OK
;
72 IEnumPins
*enumpins
= NULL
;
74 PIN_DIRECTION from_dir
;
76 IPin_QueryDirection( from
, &from_dir
);
78 hr
= IPin_QueryInternalConnections( from
, NULL
, &amount
);
79 if (hr
!= E_NOTIMPL
&& amount
)
80 FIXME("Use QueryInternalConnections!\n");
83 pin_info
.pFilter
= NULL
;
84 hr
= IPin_QueryPinInfo( from
, &pin_info
);
88 hr
= IBaseFilter_EnumPins( pin_info
.pFilter
, &enumpins
);
92 hr
= IEnumPins_Reset( enumpins
);
95 hr
= IEnumPins_Next( enumpins
, 1, &pin
, NULL
);
96 if (hr
== VFW_E_ENUM_OUT_OF_SYNC
)
98 hr
= IEnumPins_Reset( enumpins
);
105 IPin_QueryDirection( pin
, &dir
);
108 IPin
*connected
= NULL
;
111 IPin_ConnectedTo( pin
, &connected
);
116 hr_local
= fnMiddle( connected
, arg
);
117 hr_return
= updatehres( hr_return
, hr_local
);
118 IPin_Release(connected
);
135 hr_local
= fnEnd( from
, arg
);
136 hr_return
= updatehres( hr_return
, hr_local
);
140 if (pin_info
.pFilter
)
141 IBaseFilter_Release( pin_info
.pFilter
);
145 static inline InputPin
*impl_from_IMemInputPin( IMemInputPin
*iface
)
147 return (InputPin
*)((char*)iface
- FIELD_OFFSET(InputPin
, lpVtblMemInput
));
151 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
153 /* Tempting to just do a memcpy, but the name field is
154 128 characters long! We will probably never exceed 10
155 most of the time, so we are better off copying
156 each field manually */
157 strcpyW(pDest
->achName
, pSrc
->achName
);
158 pDest
->dir
= pSrc
->dir
;
159 pDest
->pFilter
= pSrc
->pFilter
;
162 /* Function called as a helper to IPin_Connect */
163 /* specific AM_MEDIA_TYPE - it cannot be NULL */
164 /* NOTE: not part of standard interface */
165 static HRESULT
OutputPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
167 OutputPin
*This
= (OutputPin
*)iface
;
169 IMemAllocator
* pMemAlloc
= NULL
;
170 ALLOCATOR_PROPERTIES actual
; /* FIXME: should we put the actual props back in to This? */
172 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
173 dump_AM_MEDIA_TYPE(pmt
);
175 /* FIXME: call queryacceptproc */
177 This
->pin
.pConnectedTo
= pReceivePin
;
178 IPin_AddRef(pReceivePin
);
179 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
181 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
183 /* get the IMemInputPin interface we will use to deliver samples to the
187 This
->pMemInputPin
= NULL
;
188 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
190 if (SUCCEEDED(hr
) && !This
->custom_allocator
)
192 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pMemAlloc
);
194 if (hr
== VFW_E_NO_ALLOCATOR
)
196 /* Input pin provides no allocator, use standard memory allocator */
197 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMemAllocator
, (LPVOID
*)&pMemAlloc
);
201 hr
= IMemInputPin_NotifyAllocator(This
->pMemInputPin
, pMemAlloc
, This
->readonly
);
206 hr
= IMemAllocator_SetProperties(pMemAlloc
, &This
->allocProps
, &actual
);
209 IMemAllocator_Release(pMemAlloc
);
211 else if (SUCCEEDED(hr
))
215 hr
= IMemInputPin_NotifyAllocator(This
->pMemInputPin
, This
->alloc
, This
->readonly
);
218 hr
= VFW_E_NO_ALLOCATOR
;
221 /* break connection if we couldn't get the allocator */
224 if (This
->pMemInputPin
)
225 IMemInputPin_Release(This
->pMemInputPin
);
226 This
->pMemInputPin
= NULL
;
228 IPin_Disconnect(pReceivePin
);
234 IPin_Release(This
->pin
.pConnectedTo
);
235 This
->pin
.pConnectedTo
= NULL
;
236 FreeMediaType(&This
->pin
.mtCurrent
);
239 TRACE(" -- %x\n", hr
);
243 static HRESULT
InputPin_Init(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
, SAMPLEPROC_PUSH pSampleProc
, LPVOID pUserData
,
244 QUERYACCEPTPROC pQueryAccept
, CLEANUPPROC pCleanUp
, LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, InputPin
* pPinImpl
)
248 /* Common attributes */
249 pPinImpl
->pin
.refCount
= 1;
250 pPinImpl
->pin
.pConnectedTo
= NULL
;
251 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
252 pPinImpl
->pin
.pUserData
= pUserData
;
253 pPinImpl
->pin
.pCritSec
= pCritSec
;
254 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
255 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
257 /* Input pin attributes */
258 pPinImpl
->fnSampleProc
= pSampleProc
;
259 pPinImpl
->fnCleanProc
= pCleanUp
;
260 pPinImpl
->pAllocator
= pPinImpl
->preferred_allocator
= allocator
;
261 if (pPinImpl
->preferred_allocator
)
262 IMemAllocator_AddRef(pPinImpl
->preferred_allocator
);
263 pPinImpl
->tStart
= 0;
265 pPinImpl
->dRate
= 1.0;
266 pPinImpl
->pin
.lpVtbl
= InputPin_Vtbl
;
267 pPinImpl
->lpVtblMemInput
= &MemInputPin_Vtbl
;
268 pPinImpl
->flushing
= pPinImpl
->end_of_stream
= 0;
273 static HRESULT
OutputPin_Init(const IPinVtbl
*OutputPin_Vtbl
, const PIN_INFO
* pPinInfo
, const ALLOCATOR_PROPERTIES
* props
, LPVOID pUserData
,
274 QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, OutputPin
* pPinImpl
)
278 /* Common attributes */
279 pPinImpl
->pin
.lpVtbl
= OutputPin_Vtbl
;
280 pPinImpl
->pin
.refCount
= 1;
281 pPinImpl
->pin
.pConnectedTo
= NULL
;
282 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
283 pPinImpl
->pin
.pUserData
= pUserData
;
284 pPinImpl
->pin
.pCritSec
= pCritSec
;
285 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
286 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
288 /* Output pin attributes */
289 pPinImpl
->pMemInputPin
= NULL
;
290 pPinImpl
->pConnectSpecific
= OutputPin_ConnectSpecific
;
291 /* If custom_allocator is set, you will need to specify an allocator
292 * in the alloc member of the struct before an output pin can connect
294 pPinImpl
->custom_allocator
= 0;
295 pPinImpl
->alloc
= NULL
;
296 pPinImpl
->readonly
= FALSE
;
299 pPinImpl
->allocProps
= *props
;
300 if (pPinImpl
->allocProps
.cbAlign
== 0)
301 pPinImpl
->allocProps
.cbAlign
= 1;
304 ZeroMemory(&pPinImpl
->allocProps
, sizeof(pPinImpl
->allocProps
));
309 HRESULT
InputPin_Construct(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
, SAMPLEPROC_PUSH pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, CLEANUPPROC pCleanUp
, LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, IPin
** ppPin
)
315 if (pPinInfo
->dir
!= PINDIR_INPUT
)
317 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
321 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
324 return E_OUTOFMEMORY
;
326 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl
, pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCleanUp
, pCritSec
, allocator
, pPinImpl
)))
328 *ppPin
= (IPin
*)pPinImpl
;
332 CoTaskMemFree(pPinImpl
);
336 HRESULT
OutputPin_Construct(const IPinVtbl
*OutputPin_Vtbl
, long outputpin_size
, const PIN_INFO
* pPinInfo
, ALLOCATOR_PROPERTIES
*props
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
338 OutputPin
* pPinImpl
;
342 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
344 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
348 assert(outputpin_size
>= sizeof(OutputPin
));
350 pPinImpl
= CoTaskMemAlloc(outputpin_size
);
353 return E_OUTOFMEMORY
;
355 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl
, pPinInfo
, props
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
357 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
361 CoTaskMemFree(pPinImpl
);
365 /*** Common pin functions ***/
367 ULONG WINAPI
IPinImpl_AddRef(IPin
* iface
)
369 IPinImpl
*This
= (IPinImpl
*)iface
;
370 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
372 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
377 HRESULT WINAPI
IPinImpl_Disconnect(IPin
* iface
)
380 IPinImpl
*This
= (IPinImpl
*)iface
;
384 EnterCriticalSection(This
->pCritSec
);
386 if (This
->pConnectedTo
)
388 IPin_Release(This
->pConnectedTo
);
389 This
->pConnectedTo
= NULL
;
395 LeaveCriticalSection(This
->pCritSec
);
400 HRESULT WINAPI
IPinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
403 IPinImpl
*This
= (IPinImpl
*)iface
;
405 TRACE("(%p)\n", ppPin
);
407 EnterCriticalSection(This
->pCritSec
);
409 if (This
->pConnectedTo
)
411 *ppPin
= This
->pConnectedTo
;
417 hr
= VFW_E_NOT_CONNECTED
;
421 LeaveCriticalSection(This
->pCritSec
);
426 HRESULT WINAPI
IPinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
429 IPinImpl
*This
= (IPinImpl
*)iface
;
431 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
433 EnterCriticalSection(This
->pCritSec
);
435 if (This
->pConnectedTo
)
437 CopyMediaType(pmt
, &This
->mtCurrent
);
442 ZeroMemory(pmt
, sizeof(*pmt
));
443 hr
= VFW_E_NOT_CONNECTED
;
446 LeaveCriticalSection(This
->pCritSec
);
451 HRESULT WINAPI
IPinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
453 IPinImpl
*This
= (IPinImpl
*)iface
;
455 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
457 Copy_PinInfo(pInfo
, &This
->pinInfo
);
458 IBaseFilter_AddRef(pInfo
->pFilter
);
463 HRESULT WINAPI
IPinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
465 IPinImpl
*This
= (IPinImpl
*)iface
;
467 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
469 *pPinDir
= This
->pinInfo
.dir
;
474 HRESULT WINAPI
IPinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
476 IPinImpl
*This
= (IPinImpl
*)iface
;
478 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
480 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
482 return E_OUTOFMEMORY
;
484 strcpyW(*Id
, This
->pinInfo
.achName
);
489 HRESULT WINAPI
IPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
491 IPinImpl
*This
= (IPinImpl
*)iface
;
493 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
495 return (This
->fnQueryAccept(This
->pUserData
, pmt
) == S_OK
? S_OK
: S_FALSE
);
498 HRESULT WINAPI
IPinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
500 IPinImpl
*This
= (IPinImpl
*)iface
;
501 ENUMMEDIADETAILS emd
;
503 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
505 /* override this method to allow enumeration of your types */
507 emd
.pMediaTypes
= NULL
;
509 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
512 HRESULT WINAPI
IPinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
514 IPinImpl
*This
= (IPinImpl
*)iface
;
516 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
518 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
521 /*** IPin implementation for an input pin ***/
523 HRESULT WINAPI
InputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
525 InputPin
*This
= (InputPin
*)iface
;
527 TRACE("(%p)->(%s, %p)\n", iface
, qzdebugstr_guid(riid
), ppv
);
531 if (IsEqualIID(riid
, &IID_IUnknown
))
532 *ppv
= (LPVOID
)iface
;
533 else if (IsEqualIID(riid
, &IID_IPin
))
534 *ppv
= (LPVOID
)iface
;
535 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
536 *ppv
= (LPVOID
)&This
->lpVtblMemInput
;
537 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
539 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
544 IUnknown_AddRef((IUnknown
*)(*ppv
));
548 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
550 return E_NOINTERFACE
;
553 ULONG WINAPI
InputPin_Release(IPin
* iface
)
555 InputPin
*This
= (InputPin
*)iface
;
556 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
558 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
562 FreeMediaType(&This
->pin
.mtCurrent
);
563 if (This
->pAllocator
)
564 IMemAllocator_Release(This
->pAllocator
);
565 This
->pAllocator
= NULL
;
566 This
->pin
.lpVtbl
= NULL
;
574 HRESULT WINAPI
InputPin_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
576 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
582 HRESULT WINAPI
InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
584 InputPin
*This
= (InputPin
*)iface
;
585 PIN_DIRECTION pindirReceive
;
588 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
589 dump_AM_MEDIA_TYPE(pmt
);
591 EnterCriticalSection(This
->pin
.pCritSec
);
593 if (This
->pin
.pConnectedTo
)
594 hr
= VFW_E_ALREADY_CONNECTED
;
596 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
597 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
598 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
602 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
604 if (pindirReceive
!= PINDIR_OUTPUT
)
606 ERR("Can't connect from non-output pin\n");
607 hr
= VFW_E_INVALID_DIRECTION
;
613 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
614 This
->pin
.pConnectedTo
= pReceivePin
;
615 IPin_AddRef(pReceivePin
);
618 LeaveCriticalSection(This
->pin
.pCritSec
);
623 static HRESULT
deliver_endofstream(IPin
* pin
, LPVOID unused
)
625 return IPin_EndOfStream( pin
);
628 HRESULT WINAPI
InputPin_EndOfStream(IPin
* iface
)
631 InputPin
*This
= (InputPin
*)iface
;
633 TRACE("(%p)\n", This
);
635 EnterCriticalSection(This
->pin
.pCritSec
);
639 This
->end_of_stream
= 1;
640 LeaveCriticalSection(This
->pin
.pCritSec
);
643 hr
= SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
647 static HRESULT
deliver_beginflush(IPin
* pin
, LPVOID unused
)
649 return IPin_BeginFlush( pin
);
652 HRESULT WINAPI
InputPin_BeginFlush(IPin
* iface
)
654 InputPin
*This
= (InputPin
*)iface
;
656 TRACE("() semi-stub\n");
658 EnterCriticalSection(This
->pin
.pCritSec
);
661 if (This
->fnCleanProc
)
662 This
->fnCleanProc(This
->pin
.pUserData
);
664 hr
= SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
665 LeaveCriticalSection(This
->pin
.pCritSec
);
670 static HRESULT
deliver_endflush(IPin
* pin
, LPVOID unused
)
672 return IPin_EndFlush( pin
);
675 HRESULT WINAPI
InputPin_EndFlush(IPin
* iface
)
677 InputPin
*This
= (InputPin
*)iface
;
679 TRACE("(%p)\n", This
);
681 EnterCriticalSection(This
->pin
.pCritSec
);
682 This
->flushing
= This
->end_of_stream
= 0;
684 hr
= SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
685 LeaveCriticalSection(This
->pin
.pCritSec
);
690 typedef struct newsegmentargs
692 REFERENCE_TIME tStart
, tStop
;
696 static HRESULT
deliver_newsegment(IPin
*pin
, LPVOID data
)
698 newsegmentargs
*args
= data
;
699 return IPin_NewSegment(pin
, args
->tStart
, args
->tStop
, args
->rate
);
702 HRESULT WINAPI
InputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
704 InputPin
*This
= (InputPin
*)iface
;
707 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
709 args
.tStart
= This
->tStart
= tStart
;
710 args
.tStop
= This
->tStop
= tStop
;
711 args
.rate
= This
->dRate
= dRate
;
713 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
716 static const IPinVtbl InputPin_Vtbl
=
718 InputPin_QueryInterface
,
722 InputPin_ReceiveConnection
,
724 IPinImpl_ConnectedTo
,
725 IPinImpl_ConnectionMediaType
,
726 IPinImpl_QueryPinInfo
,
727 IPinImpl_QueryDirection
,
729 IPinImpl_QueryAccept
,
730 IPinImpl_EnumMediaTypes
,
731 IPinImpl_QueryInternalConnections
,
732 InputPin_EndOfStream
,
738 /*** IMemInputPin implementation ***/
740 HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
742 InputPin
*This
= impl_from_IMemInputPin(iface
);
744 return IPin_QueryInterface((IPin
*)&This
->pin
, riid
, ppv
);
747 ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
749 InputPin
*This
= impl_from_IMemInputPin(iface
);
751 return IPin_AddRef((IPin
*)&This
->pin
);
754 ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
756 InputPin
*This
= impl_from_IMemInputPin(iface
);
758 return IPin_Release((IPin
*)&This
->pin
);
761 HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
763 InputPin
*This
= impl_from_IMemInputPin(iface
);
765 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
767 *ppAllocator
= This
->pAllocator
;
769 IMemAllocator_AddRef(*ppAllocator
);
771 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
774 HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
776 InputPin
*This
= impl_from_IMemInputPin(iface
);
778 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
781 FIXME("Read only flag not handled yet!\n");
783 /* FIXME: Should we release the allocator on disconnection? */
786 WARN("Null allocator\n");
790 if (This
->preferred_allocator
&& pAllocator
!= This
->preferred_allocator
)
793 if (This
->pAllocator
)
794 IMemAllocator_Release(This
->pAllocator
);
795 This
->pAllocator
= pAllocator
;
796 if (This
->pAllocator
)
797 IMemAllocator_AddRef(This
->pAllocator
);
802 HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
804 InputPin
*This
= impl_from_IMemInputPin(iface
);
806 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
808 /* override this method if you have any specific requirements */
813 HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
815 InputPin
*This
= impl_from_IMemInputPin(iface
);
818 /* this trace commented out for performance reasons */
819 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
820 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
824 HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, long nSamples
, long *nSamplesProcessed
)
827 InputPin
*This
= impl_from_IMemInputPin(iface
);
829 TRACE("(%p/%p)->(%p, %ld, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
831 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
833 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
841 HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
843 InputPin
*This
= impl_from_IMemInputPin(iface
);
845 TRACE("(%p/%p)->()\n", This
, iface
);
850 static const IMemInputPinVtbl MemInputPin_Vtbl
=
852 MemInputPin_QueryInterface
,
855 MemInputPin_GetAllocator
,
856 MemInputPin_NotifyAllocator
,
857 MemInputPin_GetAllocatorRequirements
,
859 MemInputPin_ReceiveMultiple
,
860 MemInputPin_ReceiveCanBlock
863 HRESULT WINAPI
OutputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
865 OutputPin
*This
= (OutputPin
*)iface
;
867 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
871 if (IsEqualIID(riid
, &IID_IUnknown
))
872 *ppv
= (LPVOID
)iface
;
873 else if (IsEqualIID(riid
, &IID_IPin
))
874 *ppv
= (LPVOID
)iface
;
875 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
877 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
882 IUnknown_AddRef((IUnknown
*)(*ppv
));
886 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
888 return E_NOINTERFACE
;
891 ULONG WINAPI
OutputPin_Release(IPin
* iface
)
893 OutputPin
*This
= (OutputPin
*)iface
;
894 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
896 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
900 FreeMediaType(&This
->pin
.mtCurrent
);
907 HRESULT WINAPI
OutputPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
910 OutputPin
*This
= (OutputPin
*)iface
;
912 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
913 dump_AM_MEDIA_TYPE(pmt
);
915 /* If we try to connect to ourself, we will definitely deadlock.
916 * There are other cases where we could deadlock too, but this
917 * catches the obvious case */
918 assert(pReceivePin
!= iface
);
920 EnterCriticalSection(This
->pin
.pCritSec
);
922 /* if we have been a specific type to connect with, then we can either connect
923 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
924 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
925 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
928 /* negotiate media type */
930 IEnumMediaTypes
* pEnumCandidates
;
931 AM_MEDIA_TYPE
* pmtCandidate
= NULL
; /* Candidate media type */
933 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
935 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
937 /* try this filter's media types first */
938 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
940 assert(pmtCandidate
);
941 if (!IsEqualGUID(&FORMAT_None
, &pmtCandidate
->formattype
))
942 assert(pmtCandidate
->pbFormat
);
943 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
944 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
947 CoTaskMemFree(pmtCandidate
);
950 DeleteMediaType(pmtCandidate
);
953 IEnumMediaTypes_Release(pEnumCandidates
);
956 /* then try receiver filter's media types */
957 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
959 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
961 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
963 assert(pmtCandidate
);
964 if (!IsEqualGUID(&FORMAT_None
, &pmtCandidate
->formattype
))
965 assert(pmtCandidate
->pbFormat
);
966 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
967 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
970 CoTaskMemFree(pmtCandidate
);
973 DeleteMediaType(pmtCandidate
);
976 IEnumMediaTypes_Release(pEnumCandidates
);
978 } /* if negotiate media type */
980 LeaveCriticalSection(This
->pin
.pCritSec
);
982 TRACE(" -- %x\n", hr
);
986 HRESULT WINAPI
OutputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
988 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
993 HRESULT WINAPI
OutputPin_Disconnect(IPin
* iface
)
996 OutputPin
*This
= (OutputPin
*)iface
;
1000 EnterCriticalSection(This
->pin
.pCritSec
);
1002 if (This
->pMemInputPin
)
1004 IMemInputPin_Release(This
->pMemInputPin
);
1005 This
->pMemInputPin
= NULL
;
1007 if (This
->pin
.pConnectedTo
)
1009 IPin_Release(This
->pin
.pConnectedTo
);
1010 This
->pin
.pConnectedTo
= NULL
;
1016 LeaveCriticalSection(This
->pin
.pCritSec
);
1021 HRESULT WINAPI
OutputPin_EndOfStream(IPin
* iface
)
1025 /* not supposed to do anything in an output pin */
1027 return E_UNEXPECTED
;
1030 HRESULT WINAPI
OutputPin_BeginFlush(IPin
* iface
)
1032 TRACE("(%p)->()\n", iface
);
1034 /* not supposed to do anything in an output pin */
1036 return E_UNEXPECTED
;
1039 HRESULT WINAPI
OutputPin_EndFlush(IPin
* iface
)
1041 TRACE("(%p)->()\n", iface
);
1043 /* not supposed to do anything in an output pin */
1045 return E_UNEXPECTED
;
1048 HRESULT WINAPI
OutputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1050 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface
, (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
1052 /* not supposed to do anything in an output pin */
1054 return E_UNEXPECTED
;
1057 static const IPinVtbl OutputPin_Vtbl
=
1059 OutputPin_QueryInterface
,
1063 OutputPin_ReceiveConnection
,
1064 OutputPin_Disconnect
,
1065 IPinImpl_ConnectedTo
,
1066 IPinImpl_ConnectionMediaType
,
1067 IPinImpl_QueryPinInfo
,
1068 IPinImpl_QueryDirection
,
1070 IPinImpl_QueryAccept
,
1071 IPinImpl_EnumMediaTypes
,
1072 IPinImpl_QueryInternalConnections
,
1073 OutputPin_EndOfStream
,
1074 OutputPin_BeginFlush
,
1076 OutputPin_NewSegment
1079 HRESULT
OutputPin_GetDeliveryBuffer(OutputPin
* This
, IMediaSample
** ppSample
, REFERENCE_TIME
* tStart
, REFERENCE_TIME
* tStop
, DWORD dwFlags
)
1083 TRACE("(%p, %p, %p, %x)\n", ppSample
, tStart
, tStop
, dwFlags
);
1085 EnterCriticalSection(This
->pin
.pCritSec
);
1087 if (!This
->pin
.pConnectedTo
)
1088 hr
= VFW_E_NOT_CONNECTED
;
1091 IMemAllocator
* pAlloc
= NULL
;
1093 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1096 hr
= IMemAllocator_GetBuffer(pAlloc
, ppSample
, tStart
, tStop
, dwFlags
);
1099 hr
= IMediaSample_SetTime(*ppSample
, tStart
, tStop
);
1102 IMemAllocator_Release(pAlloc
);
1105 LeaveCriticalSection(This
->pin
.pCritSec
);
1110 HRESULT
OutputPin_SendSample(OutputPin
* This
, IMediaSample
* pSample
)
1113 IMemInputPin
* pMemConnected
= NULL
;
1116 EnterCriticalSection(This
->pin
.pCritSec
);
1118 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1119 hr
= VFW_E_NOT_CONNECTED
;
1122 /* we don't have the lock held when using This->pMemInputPin,
1123 * so we need to AddRef it to stop it being deleted while we are
1124 * using it. Same with its filter. */
1125 pMemConnected
= This
->pMemInputPin
;
1126 IMemInputPin_AddRef(pMemConnected
);
1127 hr
= IPin_QueryPinInfo(This
->pin
.pConnectedTo
, &pinInfo
);
1130 LeaveCriticalSection(This
->pin
.pCritSec
);
1134 /* NOTE: if we are in a critical section when Receive is called
1135 * then it causes some problems (most notably with the native Video
1136 * Renderer) if we are re-entered for whatever reason */
1137 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
1139 /* If the filter's destroyed, tell upstream to stop sending data */
1140 if(IBaseFilter_Release(pinInfo
.pFilter
) == 0 && SUCCEEDED(hr
))
1144 IMemInputPin_Release(pMemConnected
);
1149 HRESULT
OutputPin_DeliverNewSegment(OutputPin
* This
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1153 EnterCriticalSection(This
->pin
.pCritSec
);
1155 if (!This
->pin
.pConnectedTo
)
1156 hr
= VFW_E_NOT_CONNECTED
;
1158 hr
= IPin_NewSegment(This
->pin
.pConnectedTo
, tStart
, tStop
, dRate
);
1160 LeaveCriticalSection(This
->pin
.pCritSec
);
1165 HRESULT
OutputPin_CommitAllocator(OutputPin
* This
)
1169 TRACE("(%p)->()\n", This
);
1171 EnterCriticalSection(This
->pin
.pCritSec
);
1173 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1174 hr
= VFW_E_NOT_CONNECTED
;
1177 IMemAllocator
* pAlloc
= NULL
;
1179 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1182 hr
= IMemAllocator_Commit(pAlloc
);
1185 IMemAllocator_Release(pAlloc
);
1188 LeaveCriticalSection(This
->pin
.pCritSec
);
1190 TRACE("--> %08x\n", hr
);
1194 HRESULT
OutputPin_DecommitAllocator(OutputPin
* This
)
1198 TRACE("(%p)->()\n", This
);
1200 EnterCriticalSection(This
->pin
.pCritSec
);
1202 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1203 hr
= VFW_E_NOT_CONNECTED
;
1206 IMemAllocator
* pAlloc
= NULL
;
1208 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1211 hr
= IMemAllocator_Decommit(pAlloc
);
1214 IMemAllocator_Release(pAlloc
);
1217 LeaveCriticalSection(This
->pin
.pCritSec
);
1219 TRACE("--> %08x\n", hr
);
1223 HRESULT
OutputPin_DeliverDisconnect(OutputPin
* This
)
1227 TRACE("(%p)->()\n", This
);
1229 EnterCriticalSection(This
->pin
.pCritSec
);
1231 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1232 hr
= VFW_E_NOT_CONNECTED
;
1233 else if (!This
->custom_allocator
)
1235 IMemAllocator
* pAlloc
= NULL
;
1237 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1240 hr
= IMemAllocator_Decommit(pAlloc
);
1243 IMemAllocator_Release(pAlloc
);
1246 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1248 else /* Kill the allocator! */
1250 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1252 IPin_Disconnect((IPin
*)This
);
1254 LeaveCriticalSection(This
->pin
.pCritSec
);
1260 static HRESULT
PullPin_Init(const IPinVtbl
*PullPin_Vtbl
, const PIN_INFO
* pPinInfo
, SAMPLEPROC_PULL pSampleProc
, LPVOID pUserData
,
1261 QUERYACCEPTPROC pQueryAccept
, CLEANUPPROC pCleanUp
, REQUESTPROC pCustomRequest
, STOPPROCESSPROC pDone
, LPCRITICAL_SECTION pCritSec
, PullPin
* pPinImpl
)
1263 /* Common attributes */
1264 pPinImpl
->pin
.lpVtbl
= PullPin_Vtbl
;
1265 pPinImpl
->pin
.refCount
= 1;
1266 pPinImpl
->pin
.pConnectedTo
= NULL
;
1267 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
1268 pPinImpl
->pin
.pUserData
= pUserData
;
1269 pPinImpl
->pin
.pCritSec
= pCritSec
;
1270 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1271 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1273 /* Input pin attributes */
1274 pPinImpl
->fnSampleProc
= pSampleProc
;
1275 pPinImpl
->fnCleanProc
= pCleanUp
;
1276 pPinImpl
->fnDone
= pDone
;
1277 pPinImpl
->fnPreConnect
= NULL
;
1278 pPinImpl
->pAlloc
= NULL
;
1279 pPinImpl
->pReader
= NULL
;
1280 pPinImpl
->hThread
= NULL
;
1281 pPinImpl
->hEventStateChanged
= CreateEventW(NULL
, TRUE
, TRUE
, NULL
);
1282 pPinImpl
->thread_sleepy
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1284 pPinImpl
->rtStart
= 0;
1285 pPinImpl
->rtCurrent
= 0;
1286 pPinImpl
->rtStop
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
1287 pPinImpl
->dRate
= 1.0;
1288 pPinImpl
->state
= Req_Die
;
1289 pPinImpl
->fnCustomRequest
= pCustomRequest
;
1290 pPinImpl
->stop_playback
= 1;
1292 InitializeCriticalSection(&pPinImpl
->thread_lock
);
1293 pPinImpl
->thread_lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)( __FILE__
": PullPin.thread_lock");
1298 HRESULT
PullPin_Construct(const IPinVtbl
*PullPin_Vtbl
, const PIN_INFO
* pPinInfo
, SAMPLEPROC_PULL pSampleProc
, LPVOID pUserData
, QUERYACCEPTPROC pQueryAccept
, CLEANUPPROC pCleanUp
, REQUESTPROC pCustomRequest
, STOPPROCESSPROC pDone
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
1304 if (pPinInfo
->dir
!= PINDIR_INPUT
)
1306 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1307 return E_INVALIDARG
;
1310 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
1313 return E_OUTOFMEMORY
;
1315 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl
, pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCleanUp
, pCustomRequest
, pDone
, pCritSec
, pPinImpl
)))
1317 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
1321 CoTaskMemFree(pPinImpl
);
1325 static HRESULT
PullPin_InitProcessing(PullPin
* This
);
1327 HRESULT WINAPI
PullPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
1329 PIN_DIRECTION pindirReceive
;
1331 PullPin
*This
= (PullPin
*)iface
;
1333 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1334 dump_AM_MEDIA_TYPE(pmt
);
1336 EnterCriticalSection(This
->pin
.pCritSec
);
1337 if (!This
->pin
.pConnectedTo
)
1339 ALLOCATOR_PROPERTIES props
;
1342 props
.cbBuffer
= 64 * 1024; /* 64k bytes */
1346 if (SUCCEEDED(hr
) && (This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
))
1347 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
1348 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1352 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1354 if (pindirReceive
!= PINDIR_OUTPUT
)
1356 ERR("Can't connect from non-output pin\n");
1357 hr
= VFW_E_INVALID_DIRECTION
;
1361 This
->pReader
= NULL
;
1362 This
->pAlloc
= NULL
;
1365 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1368 if (SUCCEEDED(hr
) && This
->fnPreConnect
)
1370 hr
= This
->fnPreConnect(iface
, pReceivePin
, &props
);
1375 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1380 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1381 This
->pin
.pConnectedTo
= pReceivePin
;
1382 IPin_AddRef(pReceivePin
);
1383 hr
= IMemAllocator_Commit(This
->pAlloc
);
1387 hr
= PullPin_InitProcessing(This
);
1392 IAsyncReader_Release(This
->pReader
);
1393 This
->pReader
= NULL
;
1395 IMemAllocator_Release(This
->pAlloc
);
1396 This
->pAlloc
= NULL
;
1400 hr
= VFW_E_ALREADY_CONNECTED
;
1401 LeaveCriticalSection(This
->pin
.pCritSec
);
1405 HRESULT WINAPI
PullPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1407 PullPin
*This
= (PullPin
*)iface
;
1409 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
1413 if (IsEqualIID(riid
, &IID_IUnknown
))
1414 *ppv
= (LPVOID
)iface
;
1415 else if (IsEqualIID(riid
, &IID_IPin
))
1416 *ppv
= (LPVOID
)iface
;
1417 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1419 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
1424 IUnknown_AddRef((IUnknown
*)(*ppv
));
1428 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
1430 return E_NOINTERFACE
;
1433 ULONG WINAPI
PullPin_Release(IPin
*iface
)
1435 PullPin
*This
= (PullPin
*)iface
;
1436 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
1438 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
1442 WaitForSingleObject(This
->hEventStateChanged
, INFINITE
);
1443 assert(!This
->hThread
);
1446 IMemAllocator_Release(This
->pAlloc
);
1448 IAsyncReader_Release(This
->pReader
);
1449 CloseHandle(This
->thread_sleepy
);
1450 CloseHandle(This
->hEventStateChanged
);
1451 This
->thread_lock
.DebugInfo
->Spare
[0] = 0;
1452 DeleteCriticalSection(&This
->thread_lock
);
1453 CoTaskMemFree(This
);
1459 static void CALLBACK
PullPin_Flush(PullPin
*This
)
1461 IMediaSample
*pSample
;
1462 TRACE("Flushing!\n");
1466 /* Flush outstanding samples */
1467 IAsyncReader_BeginFlush(This
->pReader
);
1473 IAsyncReader_WaitForNext(This
->pReader
, 0, &pSample
, &dwUser
);
1478 assert(!IMediaSample_GetActualDataLength(pSample
));
1480 IMediaSample_Release(pSample
);
1483 IAsyncReader_EndFlush(This
->pReader
);
1487 static void CALLBACK
PullPin_Thread_Process(PullPin
*This
)
1490 IMediaSample
* pSample
= NULL
;
1491 ALLOCATOR_PROPERTIES allocProps
;
1493 hr
= IMemAllocator_GetProperties(This
->pAlloc
, &allocProps
);
1495 This
->cbAlign
= allocProps
.cbAlign
;
1497 if (This
->rtCurrent
< This
->rtStart
)
1498 This
->rtCurrent
= MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This
->rtStart
), This
->cbAlign
));
1502 if (This
->rtCurrent
>= This
->rtStop
)
1504 IPin_EndOfStream((IPin
*)This
);
1508 /* There is no sample in our buffer */
1509 hr
= This
->fnCustomRequest(This
->pin
.pUserData
);
1512 ERR("Request error: %x\n", hr
);
1514 EnterCriticalSection(This
->pin
.pCritSec
);
1515 SetEvent(This
->hEventStateChanged
);
1516 LeaveCriticalSection(This
->pin
.pCritSec
);
1523 TRACE("Process sample\n");
1525 hr
= IAsyncReader_WaitForNext(This
->pReader
, 10000, &pSample
, &dwUser
);
1527 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1530 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
, dwUser
);
1534 /* FIXME: This is not well handled yet! */
1535 ERR("Processing error: %x\n", hr
);
1540 IMediaSample_Release(pSample
);
1543 } while (This
->rtCurrent
< This
->rtStop
&& hr
== S_OK
&& !This
->stop_playback
);
1545 /* Sample was rejected, and we are asked to terminate */
1548 IMediaSample_Release(pSample
);
1551 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1552 * Flush remaining samples
1555 This
->fnDone(This
->pin
.pUserData
);
1557 TRACE("End: %08x, %d\n", hr
, This
->stop_playback
);
1560 static void CALLBACK
PullPin_Thread_Pause(PullPin
*This
)
1562 EnterCriticalSection(This
->pin
.pCritSec
);
1563 This
->state
= Req_Sleepy
;
1564 SetEvent(This
->hEventStateChanged
);
1565 LeaveCriticalSection(This
->pin
.pCritSec
);
1568 static void CALLBACK
PullPin_Thread_Stop(PullPin
*This
)
1570 TRACE("(%p)->()\n", This
);
1572 EnterCriticalSection(This
->pin
.pCritSec
);
1574 CloseHandle(This
->hThread
);
1575 This
->hThread
= NULL
;
1576 SetEvent(This
->hEventStateChanged
);
1578 LeaveCriticalSection(This
->pin
.pCritSec
);
1580 IBaseFilter_Release(This
->pin
.pinInfo
.pFilter
);
1586 static void CALLBACK
PullPin_Thread_Flush(PullPin
*This
)
1588 PullPin_Flush(This
);
1590 EnterCriticalSection(This
->pin
.pCritSec
);
1591 This
->state
= Req_Sleepy
;
1592 SetEvent(This
->hEventStateChanged
);
1593 LeaveCriticalSection(This
->pin
.pCritSec
);
1596 static DWORD WINAPI
PullPin_Thread_Main(LPVOID pv
)
1599 CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1601 PullPin_Flush(This
);
1605 WaitForSingleObject(This
->thread_sleepy
, INFINITE
);
1607 TRACE("State: %d\n", This
->state
);
1609 switch (This
->state
)
1611 case Req_Die
: PullPin_Thread_Stop(This
); break;
1612 case Req_Run
: PullPin_Thread_Process(This
); break;
1613 case Req_Pause
: PullPin_Thread_Pause(This
); break;
1614 case Req_Flush
: PullPin_Thread_Flush(This
); break;
1615 case Req_Sleepy
: ERR("Should not be signalled with SLEEPY!\n"); break;
1616 default: ERR("Unknown state request: %d\n", This
->state
); break;
1621 static HRESULT
PullPin_InitProcessing(PullPin
* This
)
1625 TRACE("(%p)->()\n", This
);
1627 /* if we are connected */
1632 WaitForSingleObject(This
->hEventStateChanged
, INFINITE
);
1633 EnterCriticalSection(This
->pin
.pCritSec
);
1635 assert(!This
->hThread
);
1636 assert(This
->state
== Req_Die
);
1637 assert(This
->stop_playback
);
1638 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1639 This
->state
= Req_Sleepy
;
1641 /* AddRef the filter to make sure it and it's pins will be around
1642 * as long as the thread */
1643 IBaseFilter_AddRef(This
->pin
.pinInfo
.pFilter
);
1646 This
->hThread
= CreateThread(NULL
, 0, PullPin_Thread_Main
, This
, 0, &dwThreadId
);
1649 hr
= HRESULT_FROM_WIN32(GetLastError());
1650 IBaseFilter_Release(This
->pin
.pinInfo
.pFilter
);
1655 SetEvent(This
->hEventStateChanged
);
1656 /* If assert fails, that means a command was not processed before the thread previously terminated */
1658 LeaveCriticalSection(This
->pin
.pCritSec
);
1661 TRACE(" -- %x\n", hr
);
1666 HRESULT
PullPin_StartProcessing(PullPin
* This
)
1668 /* if we are connected */
1669 TRACE("(%p)->()\n", This
);
1672 assert(This
->hThread
);
1674 PullPin_WaitForStateChange(This
, INFINITE
);
1676 assert(This
->state
== Req_Sleepy
);
1679 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1680 This
->state
= Req_Run
;
1681 This
->stop_playback
= 0;
1682 ResetEvent(This
->hEventStateChanged
);
1683 SetEvent(This
->thread_sleepy
);
1689 HRESULT
PullPin_PauseProcessing(PullPin
* This
)
1691 /* if we are connected */
1692 TRACE("(%p)->()\n", This
);
1695 assert(This
->hThread
);
1697 PullPin_WaitForStateChange(This
, INFINITE
);
1699 EnterCriticalSection(This
->pin
.pCritSec
);
1701 assert(!This
->stop_playback
);
1702 assert(This
->state
== Req_Run
|| This
->state
== Req_Sleepy
);
1704 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1705 This
->state
= Req_Pause
;
1706 This
->stop_playback
= 1;
1707 ResetEvent(This
->hEventStateChanged
);
1708 SetEvent(This
->thread_sleepy
);
1710 LeaveCriticalSection(This
->pin
.pCritSec
);
1716 static HRESULT
PullPin_StopProcessing(PullPin
* This
)
1718 TRACE("(%p)->()\n", This
);
1720 /* if we are alive */
1721 assert(This
->hThread
);
1723 PullPin_WaitForStateChange(This
, INFINITE
);
1725 assert(This
->state
== Req_Pause
|| This
->state
== Req_Sleepy
);
1727 This
->stop_playback
= 1;
1728 This
->state
= Req_Die
;
1729 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1730 ResetEvent(This
->hEventStateChanged
);
1731 SetEvent(This
->thread_sleepy
);
1735 HRESULT
PullPin_WaitForStateChange(PullPin
* This
, DWORD dwMilliseconds
)
1737 if (WaitForSingleObject(This
->hEventStateChanged
, dwMilliseconds
) == WAIT_TIMEOUT
)
1742 HRESULT WINAPI
PullPin_EndOfStream(IPin
* iface
)
1744 FIXME("(%p)->() stub\n", iface
);
1746 return SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
1749 HRESULT WINAPI
PullPin_BeginFlush(IPin
* iface
)
1751 PullPin
*This
= (PullPin
*)iface
;
1752 TRACE("(%p)->()\n", This
);
1754 EnterCriticalSection(This
->pin
.pCritSec
);
1756 SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
1758 LeaveCriticalSection(This
->pin
.pCritSec
);
1760 EnterCriticalSection(&This
->thread_lock
);
1763 IAsyncReader_BeginFlush(This
->pReader
);
1764 PullPin_WaitForStateChange(This
, INFINITE
);
1766 if (This
->hThread
&& This
->state
== Req_Run
)
1768 PullPin_PauseProcessing(This
);
1769 PullPin_WaitForStateChange(This
, INFINITE
);
1772 This
->state
= Req_Flush
;
1773 ResetEvent(This
->hEventStateChanged
);
1774 SetEvent(This
->thread_sleepy
);
1775 PullPin_WaitForStateChange(This
, INFINITE
);
1777 LeaveCriticalSection(&This
->thread_lock
);
1779 EnterCriticalSection(This
->pin
.pCritSec
);
1781 This
->fnCleanProc(This
->pin
.pUserData
);
1783 LeaveCriticalSection(This
->pin
.pCritSec
);
1788 HRESULT WINAPI
PullPin_EndFlush(IPin
* iface
)
1790 PullPin
*This
= (PullPin
*)iface
;
1792 TRACE("(%p)->()\n", iface
);
1794 /* Send further first: Else a race condition might terminate processing early */
1795 EnterCriticalSection(This
->pin
.pCritSec
);
1796 SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
1797 LeaveCriticalSection(This
->pin
.pCritSec
);
1799 EnterCriticalSection(&This
->thread_lock
);
1802 IBaseFilter_GetState(This
->pin
.pinInfo
.pFilter
, INFINITE
, &state
);
1804 if (state
!= State_Stopped
)
1805 PullPin_StartProcessing(This
);
1807 PullPin_WaitForStateChange(This
, INFINITE
);
1809 LeaveCriticalSection(&This
->thread_lock
);
1814 HRESULT WINAPI
PullPin_Disconnect(IPin
*iface
)
1817 PullPin
*This
= (PullPin
*)iface
;
1821 EnterCriticalSection(This
->pin
.pCritSec
);
1823 if (FAILED(hr
= IMemAllocator_Decommit(This
->pAlloc
)))
1824 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr
);
1826 if (This
->pin
.pConnectedTo
)
1828 IPin_Release(This
->pin
.pConnectedTo
);
1829 This
->pin
.pConnectedTo
= NULL
;
1830 PullPin_StopProcessing(This
);
1836 LeaveCriticalSection(This
->pin
.pCritSec
);
1841 HRESULT WINAPI
PullPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1843 newsegmentargs args
;
1844 FIXME("(%p)->(%s, %s, %g) stub\n", iface
, wine_dbgstr_longlong(tStart
), wine_dbgstr_longlong(tStop
), dRate
);
1846 args
.tStart
= tStart
;
1850 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
1853 static const IPinVtbl PullPin_Vtbl
=
1855 PullPin_QueryInterface
,
1859 PullPin_ReceiveConnection
,
1861 IPinImpl_ConnectedTo
,
1862 IPinImpl_ConnectionMediaType
,
1863 IPinImpl_QueryPinInfo
,
1864 IPinImpl_QueryDirection
,
1866 IPinImpl_QueryAccept
,
1867 IPinImpl_EnumMediaTypes
,
1868 IPinImpl_QueryInternalConnections
,
1869 PullPin_EndOfStream
,