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
);
130 hr_local
= fnEnd( from
, arg
);
131 hr_return
= updatehres( hr_return
, hr_local
);
135 if (pin_info
.pFilter
)
136 IBaseFilter_Release( pin_info
.pFilter
);
140 static inline InputPin
*impl_from_IMemInputPin( IMemInputPin
*iface
)
142 return (InputPin
*)((char*)iface
- FIELD_OFFSET(InputPin
, lpVtblMemInput
));
146 static void Copy_PinInfo(PIN_INFO
* pDest
, const PIN_INFO
* pSrc
)
148 /* Tempting to just do a memcpy, but the name field is
149 128 characters long! We will probably never exceed 10
150 most of the time, so we are better off copying
151 each field manually */
152 strcpyW(pDest
->achName
, pSrc
->achName
);
153 pDest
->dir
= pSrc
->dir
;
154 pDest
->pFilter
= pSrc
->pFilter
;
157 /* Function called as a helper to IPin_Connect */
158 /* specific AM_MEDIA_TYPE - it cannot be NULL */
159 /* NOTE: not part of standard interface */
160 static HRESULT
OutputPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
162 OutputPin
*This
= (OutputPin
*)iface
;
164 IMemAllocator
* pMemAlloc
= NULL
;
165 ALLOCATOR_PROPERTIES actual
; /* FIXME: should we put the actual props back in to This? */
167 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
168 dump_AM_MEDIA_TYPE(pmt
);
170 /* FIXME: call queryacceptproc */
172 This
->pin
.pConnectedTo
= pReceivePin
;
173 IPin_AddRef(pReceivePin
);
174 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
176 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
178 /* get the IMemInputPin interface we will use to deliver samples to the
182 This
->pMemInputPin
= NULL
;
183 hr
= IPin_QueryInterface(pReceivePin
, &IID_IMemInputPin
, (LPVOID
)&This
->pMemInputPin
);
185 if (SUCCEEDED(hr
) && !This
->custom_allocator
)
187 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pMemAlloc
);
189 if (hr
== VFW_E_NO_ALLOCATOR
)
191 /* Input pin provides no allocator, use standard memory allocator */
192 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMemAllocator
, (LPVOID
*)&pMemAlloc
);
196 hr
= IMemInputPin_NotifyAllocator(This
->pMemInputPin
, pMemAlloc
, This
->readonly
);
201 hr
= IMemAllocator_SetProperties(pMemAlloc
, &This
->allocProps
, &actual
);
204 IMemAllocator_Release(pMemAlloc
);
206 else if (SUCCEEDED(hr
))
210 hr
= IMemInputPin_NotifyAllocator(This
->pMemInputPin
, This
->alloc
, This
->readonly
);
213 hr
= VFW_E_NO_ALLOCATOR
;
216 /* break connection if we couldn't get the allocator */
219 if (This
->pMemInputPin
)
220 IMemInputPin_Release(This
->pMemInputPin
);
221 This
->pMemInputPin
= NULL
;
223 IPin_Disconnect(pReceivePin
);
229 IPin_Release(This
->pin
.pConnectedTo
);
230 This
->pin
.pConnectedTo
= NULL
;
231 FreeMediaType(&This
->pin
.mtCurrent
);
234 TRACE(" -- %x\n", hr
);
238 static HRESULT
InputPin_Init(const IPinVtbl
*InputPin_Vtbl
, const PIN_INFO
* pPinInfo
, SAMPLEPROC_PUSH pSampleProc
, LPVOID pUserData
,
239 QUERYACCEPTPROC pQueryAccept
, CLEANUPPROC pCleanUp
, LPCRITICAL_SECTION pCritSec
, IMemAllocator
*allocator
, InputPin
* pPinImpl
)
243 /* Common attributes */
244 pPinImpl
->pin
.refCount
= 1;
245 pPinImpl
->pin
.pConnectedTo
= NULL
;
246 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
247 pPinImpl
->pin
.pUserData
= pUserData
;
248 pPinImpl
->pin
.pCritSec
= pCritSec
;
249 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
250 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
252 /* Input pin attributes */
253 pPinImpl
->fnSampleProc
= pSampleProc
;
254 pPinImpl
->fnCleanProc
= pCleanUp
;
255 pPinImpl
->pAllocator
= pPinImpl
->preferred_allocator
= allocator
;
256 if (pPinImpl
->preferred_allocator
)
257 IMemAllocator_AddRef(pPinImpl
->preferred_allocator
);
258 pPinImpl
->tStart
= 0;
260 pPinImpl
->dRate
= 1.0;
261 pPinImpl
->pin
.lpVtbl
= InputPin_Vtbl
;
262 pPinImpl
->lpVtblMemInput
= &MemInputPin_Vtbl
;
263 pPinImpl
->flushing
= pPinImpl
->end_of_stream
= 0;
268 static HRESULT
OutputPin_Init(const IPinVtbl
*OutputPin_Vtbl
, const PIN_INFO
* pPinInfo
, const ALLOCATOR_PROPERTIES
* props
, LPVOID pUserData
,
269 QUERYACCEPTPROC pQueryAccept
, LPCRITICAL_SECTION pCritSec
, OutputPin
* pPinImpl
)
273 /* Common attributes */
274 pPinImpl
->pin
.lpVtbl
= OutputPin_Vtbl
;
275 pPinImpl
->pin
.refCount
= 1;
276 pPinImpl
->pin
.pConnectedTo
= NULL
;
277 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
278 pPinImpl
->pin
.pUserData
= pUserData
;
279 pPinImpl
->pin
.pCritSec
= pCritSec
;
280 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
281 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
283 /* Output pin attributes */
284 pPinImpl
->pMemInputPin
= NULL
;
285 pPinImpl
->pConnectSpecific
= OutputPin_ConnectSpecific
;
286 /* If custom_allocator is set, you will need to specify an allocator
287 * in the alloc member of the struct before an output pin can connect
289 pPinImpl
->custom_allocator
= 0;
290 pPinImpl
->alloc
= NULL
;
291 pPinImpl
->readonly
= FALSE
;
294 pPinImpl
->allocProps
= *props
;
295 if (pPinImpl
->allocProps
.cbAlign
== 0)
296 pPinImpl
->allocProps
.cbAlign
= 1;
299 ZeroMemory(&pPinImpl
->allocProps
, sizeof(pPinImpl
->allocProps
));
304 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
)
310 if (pPinInfo
->dir
!= PINDIR_INPUT
)
312 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
316 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
319 return E_OUTOFMEMORY
;
321 if (SUCCEEDED(InputPin_Init(InputPin_Vtbl
, pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCleanUp
, pCritSec
, allocator
, pPinImpl
)))
323 *ppPin
= (IPin
*)pPinImpl
;
327 CoTaskMemFree(pPinImpl
);
331 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
)
333 OutputPin
* pPinImpl
;
337 if (pPinInfo
->dir
!= PINDIR_OUTPUT
)
339 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo
->dir
);
343 assert(outputpin_size
>= sizeof(OutputPin
));
345 pPinImpl
= CoTaskMemAlloc(outputpin_size
);
348 return E_OUTOFMEMORY
;
350 if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl
, pPinInfo
, props
, pUserData
, pQueryAccept
, pCritSec
, pPinImpl
)))
352 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
356 CoTaskMemFree(pPinImpl
);
360 /*** Common pin functions ***/
362 ULONG WINAPI
IPinImpl_AddRef(IPin
* iface
)
364 IPinImpl
*This
= (IPinImpl
*)iface
;
365 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
367 TRACE("(%p)->() AddRef from %d\n", iface
, refCount
- 1);
372 HRESULT WINAPI
IPinImpl_Disconnect(IPin
* iface
)
375 IPinImpl
*This
= (IPinImpl
*)iface
;
379 EnterCriticalSection(This
->pCritSec
);
381 if (This
->pConnectedTo
)
383 IPin_Release(This
->pConnectedTo
);
384 This
->pConnectedTo
= NULL
;
390 LeaveCriticalSection(This
->pCritSec
);
395 HRESULT WINAPI
IPinImpl_ConnectedTo(IPin
* iface
, IPin
** ppPin
)
398 IPinImpl
*This
= (IPinImpl
*)iface
;
400 TRACE("(%p)\n", ppPin
);
402 EnterCriticalSection(This
->pCritSec
);
404 if (This
->pConnectedTo
)
406 *ppPin
= This
->pConnectedTo
;
412 hr
= VFW_E_NOT_CONNECTED
;
416 LeaveCriticalSection(This
->pCritSec
);
421 HRESULT WINAPI
IPinImpl_ConnectionMediaType(IPin
* iface
, AM_MEDIA_TYPE
* pmt
)
424 IPinImpl
*This
= (IPinImpl
*)iface
;
426 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
428 EnterCriticalSection(This
->pCritSec
);
430 if (This
->pConnectedTo
)
432 CopyMediaType(pmt
, &This
->mtCurrent
);
437 ZeroMemory(pmt
, sizeof(*pmt
));
438 hr
= VFW_E_NOT_CONNECTED
;
441 LeaveCriticalSection(This
->pCritSec
);
446 HRESULT WINAPI
IPinImpl_QueryPinInfo(IPin
* iface
, PIN_INFO
* pInfo
)
448 IPinImpl
*This
= (IPinImpl
*)iface
;
450 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
452 Copy_PinInfo(pInfo
, &This
->pinInfo
);
453 IBaseFilter_AddRef(pInfo
->pFilter
);
458 HRESULT WINAPI
IPinImpl_QueryDirection(IPin
* iface
, PIN_DIRECTION
* pPinDir
)
460 IPinImpl
*This
= (IPinImpl
*)iface
;
462 TRACE("(%p/%p)->(%p)\n", This
, iface
, pPinDir
);
464 *pPinDir
= This
->pinInfo
.dir
;
469 HRESULT WINAPI
IPinImpl_QueryId(IPin
* iface
, LPWSTR
* Id
)
471 IPinImpl
*This
= (IPinImpl
*)iface
;
473 TRACE("(%p/%p)->(%p)\n", This
, iface
, Id
);
475 *Id
= CoTaskMemAlloc((strlenW(This
->pinInfo
.achName
) + 1) * sizeof(WCHAR
));
477 return E_OUTOFMEMORY
;
479 strcpyW(*Id
, This
->pinInfo
.achName
);
484 HRESULT WINAPI
IPinImpl_QueryAccept(IPin
* iface
, const AM_MEDIA_TYPE
* pmt
)
486 IPinImpl
*This
= (IPinImpl
*)iface
;
488 TRACE("(%p/%p)->(%p)\n", This
, iface
, pmt
);
490 return (This
->fnQueryAccept(This
->pUserData
, pmt
) == S_OK
? S_OK
: S_FALSE
);
493 HRESULT WINAPI
IPinImpl_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
495 IPinImpl
*This
= (IPinImpl
*)iface
;
496 ENUMMEDIADETAILS emd
;
498 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
500 /* override this method to allow enumeration of your types */
502 emd
.pMediaTypes
= NULL
;
504 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
507 HRESULT WINAPI
IPinImpl_QueryInternalConnections(IPin
* iface
, IPin
** apPin
, ULONG
* cPin
)
509 IPinImpl
*This
= (IPinImpl
*)iface
;
511 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, apPin
, cPin
);
513 return E_NOTIMPL
; /* to tell caller that all input pins connected to all output pins */
516 /*** IPin implementation for an input pin ***/
518 HRESULT WINAPI
InputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
520 InputPin
*This
= (InputPin
*)iface
;
522 TRACE("(%p)->(%s, %p)\n", iface
, qzdebugstr_guid(riid
), ppv
);
526 if (IsEqualIID(riid
, &IID_IUnknown
))
527 *ppv
= (LPVOID
)iface
;
528 else if (IsEqualIID(riid
, &IID_IPin
))
529 *ppv
= (LPVOID
)iface
;
530 else if (IsEqualIID(riid
, &IID_IMemInputPin
))
531 *ppv
= (LPVOID
)&This
->lpVtblMemInput
;
532 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
534 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
539 IUnknown_AddRef((IUnknown
*)(*ppv
));
543 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
545 return E_NOINTERFACE
;
548 ULONG WINAPI
InputPin_Release(IPin
* iface
)
550 InputPin
*This
= (InputPin
*)iface
;
551 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
553 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
557 FreeMediaType(&This
->pin
.mtCurrent
);
558 if (This
->pAllocator
)
559 IMemAllocator_Release(This
->pAllocator
);
560 This
->pAllocator
= NULL
;
561 This
->pin
.lpVtbl
= NULL
;
569 HRESULT WINAPI
InputPin_Connect(IPin
* iface
, IPin
* pConnector
, const AM_MEDIA_TYPE
* pmt
)
571 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector
, pmt
);
577 HRESULT WINAPI
InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
579 InputPin
*This
= (InputPin
*)iface
;
580 PIN_DIRECTION pindirReceive
;
583 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
584 dump_AM_MEDIA_TYPE(pmt
);
586 EnterCriticalSection(This
->pin
.pCritSec
);
588 if (This
->pin
.pConnectedTo
)
589 hr
= VFW_E_ALREADY_CONNECTED
;
591 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
592 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
593 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
597 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
599 if (pindirReceive
!= PINDIR_OUTPUT
)
601 ERR("Can't connect from non-output pin\n");
602 hr
= VFW_E_INVALID_DIRECTION
;
608 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
609 This
->pin
.pConnectedTo
= pReceivePin
;
610 IPin_AddRef(pReceivePin
);
613 LeaveCriticalSection(This
->pin
.pCritSec
);
618 static HRESULT
deliver_endofstream(IPin
* pin
, LPVOID unused
)
620 return IPin_EndOfStream( pin
);
623 HRESULT WINAPI
InputPin_EndOfStream(IPin
* iface
)
625 InputPin
*This
= (InputPin
*)iface
;
626 TRACE("(%p)\n", This
);
628 This
->end_of_stream
= 1;
630 return SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
633 static HRESULT
deliver_beginflush(IPin
* pin
, LPVOID unused
)
635 return IPin_BeginFlush( pin
);
638 HRESULT WINAPI
InputPin_BeginFlush(IPin
* iface
)
640 InputPin
*This
= (InputPin
*)iface
;
642 TRACE("() semi-stub\n");
644 EnterCriticalSection(This
->pin
.pCritSec
);
647 if (This
->fnCleanProc
)
648 This
->fnCleanProc(This
->pin
.pUserData
);
650 hr
= SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
651 LeaveCriticalSection(This
->pin
.pCritSec
);
656 static HRESULT
deliver_endflush(IPin
* pin
, LPVOID unused
)
658 return IPin_EndFlush( pin
);
661 HRESULT WINAPI
InputPin_EndFlush(IPin
* iface
)
663 InputPin
*This
= (InputPin
*)iface
;
665 TRACE("(%p)\n", This
);
667 EnterCriticalSection(This
->pin
.pCritSec
);
670 hr
= SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
671 LeaveCriticalSection(This
->pin
.pCritSec
);
676 typedef struct newsegmentargs
678 REFERENCE_TIME tStart
, tStop
;
682 static HRESULT
deliver_newsegment(IPin
*pin
, LPVOID data
)
684 newsegmentargs
*args
= data
;
685 return IPin_NewSegment(pin
, args
->tStart
, args
->tStop
, args
->rate
);
688 HRESULT WINAPI
InputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
690 InputPin
*This
= (InputPin
*)iface
;
693 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
695 args
.tStart
= This
->tStart
= tStart
;
696 args
.tStop
= This
->tStop
= tStop
;
697 args
.rate
= This
->dRate
= dRate
;
699 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
702 static const IPinVtbl InputPin_Vtbl
=
704 InputPin_QueryInterface
,
708 InputPin_ReceiveConnection
,
710 IPinImpl_ConnectedTo
,
711 IPinImpl_ConnectionMediaType
,
712 IPinImpl_QueryPinInfo
,
713 IPinImpl_QueryDirection
,
715 IPinImpl_QueryAccept
,
716 IPinImpl_EnumMediaTypes
,
717 IPinImpl_QueryInternalConnections
,
718 InputPin_EndOfStream
,
724 /*** IMemInputPin implementation ***/
726 HRESULT WINAPI
MemInputPin_QueryInterface(IMemInputPin
* iface
, REFIID riid
, LPVOID
* ppv
)
728 InputPin
*This
= impl_from_IMemInputPin(iface
);
730 return IPin_QueryInterface((IPin
*)&This
->pin
, riid
, ppv
);
733 ULONG WINAPI
MemInputPin_AddRef(IMemInputPin
* iface
)
735 InputPin
*This
= impl_from_IMemInputPin(iface
);
737 return IPin_AddRef((IPin
*)&This
->pin
);
740 ULONG WINAPI
MemInputPin_Release(IMemInputPin
* iface
)
742 InputPin
*This
= impl_from_IMemInputPin(iface
);
744 return IPin_Release((IPin
*)&This
->pin
);
747 HRESULT WINAPI
MemInputPin_GetAllocator(IMemInputPin
* iface
, IMemAllocator
** ppAllocator
)
749 InputPin
*This
= impl_from_IMemInputPin(iface
);
751 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppAllocator
);
753 *ppAllocator
= This
->pAllocator
;
755 IMemAllocator_AddRef(*ppAllocator
);
757 return *ppAllocator
? S_OK
: VFW_E_NO_ALLOCATOR
;
760 HRESULT WINAPI
MemInputPin_NotifyAllocator(IMemInputPin
* iface
, IMemAllocator
* pAllocator
, BOOL bReadOnly
)
762 InputPin
*This
= impl_from_IMemInputPin(iface
);
764 TRACE("(%p/%p)->(%p, %d)\n", This
, iface
, pAllocator
, bReadOnly
);
767 FIXME("Read only flag not handled yet!\n");
769 /* FIXME: Should we release the allocator on disconnection? */
772 WARN("Null allocator\n");
776 if (This
->preferred_allocator
&& pAllocator
!= This
->preferred_allocator
)
779 if (This
->pAllocator
)
780 IMemAllocator_Release(This
->pAllocator
);
781 This
->pAllocator
= pAllocator
;
782 if (This
->pAllocator
)
783 IMemAllocator_AddRef(This
->pAllocator
);
788 HRESULT WINAPI
MemInputPin_GetAllocatorRequirements(IMemInputPin
* iface
, ALLOCATOR_PROPERTIES
* pProps
)
790 InputPin
*This
= impl_from_IMemInputPin(iface
);
792 TRACE("(%p/%p)->(%p)\n", This
, iface
, pProps
);
794 /* override this method if you have any specific requirements */
799 HRESULT WINAPI
MemInputPin_Receive(IMemInputPin
* iface
, IMediaSample
* pSample
)
801 InputPin
*This
= impl_from_IMemInputPin(iface
);
804 /* this trace commented out for performance reasons */
805 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
807 EnterCriticalSection(This
->pin
.pCritSec
);
808 if (!This
->end_of_stream
&& !This
->flushing
)
809 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
);
812 LeaveCriticalSection(This
->pin
.pCritSec
);
816 HRESULT WINAPI
MemInputPin_ReceiveMultiple(IMemInputPin
* iface
, IMediaSample
** pSamples
, long nSamples
, long *nSamplesProcessed
)
819 InputPin
*This
= impl_from_IMemInputPin(iface
);
821 TRACE("(%p/%p)->(%p, %ld, %p)\n", This
, iface
, pSamples
, nSamples
, nSamplesProcessed
);
823 for (*nSamplesProcessed
= 0; *nSamplesProcessed
< nSamples
; (*nSamplesProcessed
)++)
825 hr
= IMemInputPin_Receive(iface
, pSamples
[*nSamplesProcessed
]);
833 HRESULT WINAPI
MemInputPin_ReceiveCanBlock(IMemInputPin
* iface
)
835 InputPin
*This
= impl_from_IMemInputPin(iface
);
837 TRACE("(%p/%p)->()\n", This
, iface
);
842 static const IMemInputPinVtbl MemInputPin_Vtbl
=
844 MemInputPin_QueryInterface
,
847 MemInputPin_GetAllocator
,
848 MemInputPin_NotifyAllocator
,
849 MemInputPin_GetAllocatorRequirements
,
851 MemInputPin_ReceiveMultiple
,
852 MemInputPin_ReceiveCanBlock
855 HRESULT WINAPI
OutputPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
857 OutputPin
*This
= (OutputPin
*)iface
;
859 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
863 if (IsEqualIID(riid
, &IID_IUnknown
))
864 *ppv
= (LPVOID
)iface
;
865 else if (IsEqualIID(riid
, &IID_IPin
))
866 *ppv
= (LPVOID
)iface
;
867 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
869 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
874 IUnknown_AddRef((IUnknown
*)(*ppv
));
878 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
880 return E_NOINTERFACE
;
883 ULONG WINAPI
OutputPin_Release(IPin
* iface
)
885 OutputPin
*This
= (OutputPin
*)iface
;
886 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
888 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
892 FreeMediaType(&This
->pin
.mtCurrent
);
899 HRESULT WINAPI
OutputPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
902 OutputPin
*This
= (OutputPin
*)iface
;
904 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
905 dump_AM_MEDIA_TYPE(pmt
);
907 /* If we try to connect to ourself, we will definitely deadlock.
908 * There are other cases where we could deadlock too, but this
909 * catches the obvious case */
910 assert(pReceivePin
!= iface
);
912 EnterCriticalSection(This
->pin
.pCritSec
);
914 /* if we have been a specific type to connect with, then we can either connect
915 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
916 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
917 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
920 /* negotiate media type */
922 IEnumMediaTypes
* pEnumCandidates
;
923 AM_MEDIA_TYPE
* pmtCandidate
; /* Candidate media type */
925 if (SUCCEEDED(hr
= IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
927 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
929 /* try this filter's media types first */
930 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
932 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
933 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
936 CoTaskMemFree(pmtCandidate
);
939 CoTaskMemFree(pmtCandidate
);
941 IEnumMediaTypes_Release(pEnumCandidates
);
944 /* then try receiver filter's media types */
945 if (hr
!= S_OK
&& SUCCEEDED(hr
= IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
947 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
949 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
951 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
952 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
955 CoTaskMemFree(pmtCandidate
);
958 CoTaskMemFree(pmtCandidate
);
960 IEnumMediaTypes_Release(pEnumCandidates
);
962 } /* if negotiate media type */
964 LeaveCriticalSection(This
->pin
.pCritSec
);
966 TRACE(" -- %x\n", hr
);
970 HRESULT WINAPI
OutputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
972 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin
, pmt
);
977 HRESULT WINAPI
OutputPin_Disconnect(IPin
* iface
)
980 OutputPin
*This
= (OutputPin
*)iface
;
984 EnterCriticalSection(This
->pin
.pCritSec
);
986 if (This
->pMemInputPin
)
988 IMemInputPin_Release(This
->pMemInputPin
);
989 This
->pMemInputPin
= NULL
;
991 if (This
->pin
.pConnectedTo
)
993 IPin_Release(This
->pin
.pConnectedTo
);
994 This
->pin
.pConnectedTo
= NULL
;
1000 LeaveCriticalSection(This
->pin
.pCritSec
);
1005 HRESULT WINAPI
OutputPin_EndOfStream(IPin
* iface
)
1009 /* not supposed to do anything in an output pin */
1011 return E_UNEXPECTED
;
1014 HRESULT WINAPI
OutputPin_BeginFlush(IPin
* iface
)
1016 TRACE("(%p)->()\n", iface
);
1018 /* not supposed to do anything in an output pin */
1020 return E_UNEXPECTED
;
1023 HRESULT WINAPI
OutputPin_EndFlush(IPin
* iface
)
1025 TRACE("(%p)->()\n", iface
);
1027 /* not supposed to do anything in an output pin */
1029 return E_UNEXPECTED
;
1032 HRESULT WINAPI
OutputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1034 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface
, (ULONG
)(tStart
>> 32), (ULONG
)tStart
, (ULONG
)(tStop
>> 32), (ULONG
)tStop
, dRate
);
1036 /* not supposed to do anything in an output pin */
1038 return E_UNEXPECTED
;
1041 static const IPinVtbl OutputPin_Vtbl
=
1043 OutputPin_QueryInterface
,
1047 OutputPin_ReceiveConnection
,
1048 OutputPin_Disconnect
,
1049 IPinImpl_ConnectedTo
,
1050 IPinImpl_ConnectionMediaType
,
1051 IPinImpl_QueryPinInfo
,
1052 IPinImpl_QueryDirection
,
1054 IPinImpl_QueryAccept
,
1055 IPinImpl_EnumMediaTypes
,
1056 IPinImpl_QueryInternalConnections
,
1057 OutputPin_EndOfStream
,
1058 OutputPin_BeginFlush
,
1060 OutputPin_NewSegment
1063 HRESULT
OutputPin_GetDeliveryBuffer(OutputPin
* This
, IMediaSample
** ppSample
, REFERENCE_TIME
* tStart
, REFERENCE_TIME
* tStop
, DWORD dwFlags
)
1067 TRACE("(%p, %p, %p, %x)\n", ppSample
, tStart
, tStop
, dwFlags
);
1069 EnterCriticalSection(This
->pin
.pCritSec
);
1071 if (!This
->pin
.pConnectedTo
)
1072 hr
= VFW_E_NOT_CONNECTED
;
1075 IMemAllocator
* pAlloc
= NULL
;
1077 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1080 hr
= IMemAllocator_GetBuffer(pAlloc
, ppSample
, tStart
, tStop
, dwFlags
);
1083 hr
= IMediaSample_SetTime(*ppSample
, tStart
, tStop
);
1086 IMemAllocator_Release(pAlloc
);
1089 LeaveCriticalSection(This
->pin
.pCritSec
);
1094 HRESULT
OutputPin_SendSample(OutputPin
* This
, IMediaSample
* pSample
)
1097 IMemInputPin
* pMemConnected
= NULL
;
1100 EnterCriticalSection(This
->pin
.pCritSec
);
1102 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1103 hr
= VFW_E_NOT_CONNECTED
;
1106 /* we don't have the lock held when using This->pMemInputPin,
1107 * so we need to AddRef it to stop it being deleted while we are
1108 * using it. Same with its filter. */
1109 pMemConnected
= This
->pMemInputPin
;
1110 IMemInputPin_AddRef(pMemConnected
);
1111 hr
= IPin_QueryPinInfo(This
->pin
.pConnectedTo
, &pinInfo
);
1114 LeaveCriticalSection(This
->pin
.pCritSec
);
1118 /* NOTE: if we are in a critical section when Receive is called
1119 * then it causes some problems (most notably with the native Video
1120 * Renderer) if we are re-entered for whatever reason */
1121 hr
= IMemInputPin_Receive(pMemConnected
, pSample
);
1123 /* If the filter's destroyed, tell upstream to stop sending data */
1124 if(IBaseFilter_Release(pinInfo
.pFilter
) == 0 && SUCCEEDED(hr
))
1128 IMemInputPin_Release(pMemConnected
);
1133 HRESULT
OutputPin_DeliverNewSegment(OutputPin
* This
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1137 EnterCriticalSection(This
->pin
.pCritSec
);
1139 if (!This
->pin
.pConnectedTo
)
1140 hr
= VFW_E_NOT_CONNECTED
;
1142 hr
= IPin_NewSegment(This
->pin
.pConnectedTo
, tStart
, tStop
, dRate
);
1144 LeaveCriticalSection(This
->pin
.pCritSec
);
1149 HRESULT
OutputPin_CommitAllocator(OutputPin
* This
)
1153 TRACE("(%p)->()\n", This
);
1155 EnterCriticalSection(This
->pin
.pCritSec
);
1157 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1158 hr
= VFW_E_NOT_CONNECTED
;
1161 IMemAllocator
* pAlloc
= NULL
;
1163 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1166 hr
= IMemAllocator_Commit(pAlloc
);
1169 IMemAllocator_Release(pAlloc
);
1172 LeaveCriticalSection(This
->pin
.pCritSec
);
1174 TRACE("--> %08x\n", hr
);
1178 HRESULT
OutputPin_DecommitAllocator(OutputPin
* This
)
1182 TRACE("(%p)->()\n", This
);
1184 EnterCriticalSection(This
->pin
.pCritSec
);
1186 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1187 hr
= VFW_E_NOT_CONNECTED
;
1190 IMemAllocator
* pAlloc
= NULL
;
1192 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1195 hr
= IMemAllocator_Decommit(pAlloc
);
1198 IMemAllocator_Release(pAlloc
);
1201 LeaveCriticalSection(This
->pin
.pCritSec
);
1203 TRACE("--> %08x\n", hr
);
1207 HRESULT
OutputPin_DeliverDisconnect(OutputPin
* This
)
1211 TRACE("(%p)->()\n", This
);
1213 EnterCriticalSection(This
->pin
.pCritSec
);
1215 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1216 hr
= VFW_E_NOT_CONNECTED
;
1217 else if (!This
->custom_allocator
)
1219 IMemAllocator
* pAlloc
= NULL
;
1221 hr
= IMemInputPin_GetAllocator(This
->pMemInputPin
, &pAlloc
);
1224 hr
= IMemAllocator_Decommit(pAlloc
);
1227 IMemAllocator_Release(pAlloc
);
1230 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1232 else /* Kill the allocator! */
1234 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1236 IPin_Disconnect((IPin
*)This
);
1238 LeaveCriticalSection(This
->pin
.pCritSec
);
1244 static HRESULT
PullPin_Init(const IPinVtbl
*PullPin_Vtbl
, const PIN_INFO
* pPinInfo
, SAMPLEPROC_PULL pSampleProc
, LPVOID pUserData
,
1245 QUERYACCEPTPROC pQueryAccept
, CLEANUPPROC pCleanUp
, REQUESTPROC pCustomRequest
, STOPPROCESSPROC pDone
, LPCRITICAL_SECTION pCritSec
, PullPin
* pPinImpl
)
1247 /* Common attributes */
1248 pPinImpl
->pin
.lpVtbl
= PullPin_Vtbl
;
1249 pPinImpl
->pin
.refCount
= 1;
1250 pPinImpl
->pin
.pConnectedTo
= NULL
;
1251 pPinImpl
->pin
.fnQueryAccept
= pQueryAccept
;
1252 pPinImpl
->pin
.pUserData
= pUserData
;
1253 pPinImpl
->pin
.pCritSec
= pCritSec
;
1254 Copy_PinInfo(&pPinImpl
->pin
.pinInfo
, pPinInfo
);
1255 ZeroMemory(&pPinImpl
->pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1257 /* Input pin attributes */
1258 pPinImpl
->fnSampleProc
= pSampleProc
;
1259 pPinImpl
->fnCleanProc
= pCleanUp
;
1260 pPinImpl
->fnDone
= pDone
;
1261 pPinImpl
->fnPreConnect
= NULL
;
1262 pPinImpl
->pAlloc
= NULL
;
1263 pPinImpl
->pReader
= NULL
;
1264 pPinImpl
->hThread
= NULL
;
1265 pPinImpl
->hEventStateChanged
= CreateEventW(NULL
, TRUE
, TRUE
, NULL
);
1266 pPinImpl
->thread_sleepy
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1268 pPinImpl
->rtStart
= 0;
1269 pPinImpl
->rtCurrent
= 0;
1270 pPinImpl
->rtStop
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
1271 pPinImpl
->dRate
= 1.0;
1272 pPinImpl
->state
= Req_Die
;
1273 pPinImpl
->fnCustomRequest
= pCustomRequest
;
1274 pPinImpl
->stop_playback
= 1;
1276 InitializeCriticalSection(&pPinImpl
->thread_lock
);
1277 pPinImpl
->thread_lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)( __FILE__
": PullPin.thread_lock");
1282 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
)
1288 if (pPinInfo
->dir
!= PINDIR_INPUT
)
1290 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo
->dir
);
1291 return E_INVALIDARG
;
1294 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
1297 return E_OUTOFMEMORY
;
1299 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl
, pPinInfo
, pSampleProc
, pUserData
, pQueryAccept
, pCleanUp
, pCustomRequest
, pDone
, pCritSec
, pPinImpl
)))
1301 *ppPin
= (IPin
*)(&pPinImpl
->pin
.lpVtbl
);
1305 CoTaskMemFree(pPinImpl
);
1309 static HRESULT
PullPin_InitProcessing(PullPin
* This
);
1311 HRESULT WINAPI
PullPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
1313 PIN_DIRECTION pindirReceive
;
1315 PullPin
*This
= (PullPin
*)iface
;
1317 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1318 dump_AM_MEDIA_TYPE(pmt
);
1320 EnterCriticalSection(This
->pin
.pCritSec
);
1321 if (!This
->pin
.pConnectedTo
)
1323 ALLOCATOR_PROPERTIES props
;
1326 props
.cbBuffer
= 64 * 1024; /* 64k bytes */
1330 if (SUCCEEDED(hr
) && (This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
))
1331 hr
= VFW_E_TYPE_NOT_ACCEPTED
; /* FIXME: shouldn't we just map common errors onto
1332 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1336 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1338 if (pindirReceive
!= PINDIR_OUTPUT
)
1340 ERR("Can't connect from non-output pin\n");
1341 hr
= VFW_E_INVALID_DIRECTION
;
1345 This
->pReader
= NULL
;
1346 This
->pAlloc
= NULL
;
1349 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1352 if (SUCCEEDED(hr
) && This
->fnPreConnect
)
1354 hr
= This
->fnPreConnect(iface
, pReceivePin
, &props
);
1359 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1364 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1365 This
->pin
.pConnectedTo
= pReceivePin
;
1366 IPin_AddRef(pReceivePin
);
1367 hr
= IMemAllocator_Commit(This
->pAlloc
);
1371 hr
= PullPin_InitProcessing(This
);
1376 IAsyncReader_Release(This
->pReader
);
1377 This
->pReader
= NULL
;
1379 IMemAllocator_Release(This
->pAlloc
);
1380 This
->pAlloc
= NULL
;
1384 hr
= VFW_E_ALREADY_CONNECTED
;
1385 LeaveCriticalSection(This
->pin
.pCritSec
);
1389 HRESULT WINAPI
PullPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1391 PullPin
*This
= (PullPin
*)iface
;
1393 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
1397 if (IsEqualIID(riid
, &IID_IUnknown
))
1398 *ppv
= (LPVOID
)iface
;
1399 else if (IsEqualIID(riid
, &IID_IPin
))
1400 *ppv
= (LPVOID
)iface
;
1401 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1403 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
1408 IUnknown_AddRef((IUnknown
*)(*ppv
));
1412 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
1414 return E_NOINTERFACE
;
1417 ULONG WINAPI
PullPin_Release(IPin
*iface
)
1419 PullPin
*This
= (PullPin
*)iface
;
1420 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
1422 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
1426 WaitForSingleObject(This
->hEventStateChanged
, INFINITE
);
1427 assert(!This
->hThread
);
1430 IMemAllocator_Release(This
->pAlloc
);
1432 IAsyncReader_Release(This
->pReader
);
1433 CloseHandle(This
->thread_sleepy
);
1434 CloseHandle(This
->hEventStateChanged
);
1435 This
->thread_lock
.DebugInfo
->Spare
[0] = 0;
1436 DeleteCriticalSection(&This
->thread_lock
);
1437 CoTaskMemFree(This
);
1443 static void CALLBACK
PullPin_Flush(PullPin
*This
)
1445 IMediaSample
*pSample
;
1446 TRACE("Flushing!\n");
1450 /* Flush outstanding samples */
1451 IAsyncReader_BeginFlush(This
->pReader
);
1457 IAsyncReader_WaitForNext(This
->pReader
, 0, &pSample
, &dwUser
);
1462 assert(!IMediaSample_GetActualDataLength(pSample
));
1464 IMediaSample_Release(pSample
);
1467 IAsyncReader_EndFlush(This
->pReader
);
1471 static void CALLBACK
PullPin_Thread_Process(PullPin
*This
)
1474 IMediaSample
* pSample
= NULL
;
1475 ALLOCATOR_PROPERTIES allocProps
;
1477 hr
= IMemAllocator_GetProperties(This
->pAlloc
, &allocProps
);
1479 This
->cbAlign
= allocProps
.cbAlign
;
1481 if (This
->rtCurrent
< This
->rtStart
)
1482 This
->rtCurrent
= MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This
->rtStart
), This
->cbAlign
));
1486 if (This
->rtCurrent
>= This
->rtStop
)
1488 IPin_EndOfStream((IPin
*)This
);
1492 /* There is no sample in our buffer */
1493 hr
= This
->fnCustomRequest(This
->pin
.pUserData
);
1496 ERR("Request error: %x\n", hr
);
1498 EnterCriticalSection(This
->pin
.pCritSec
);
1499 SetEvent(This
->hEventStateChanged
);
1500 LeaveCriticalSection(This
->pin
.pCritSec
);
1507 TRACE("Process sample\n");
1509 hr
= IAsyncReader_WaitForNext(This
->pReader
, 10000, &pSample
, &dwUser
);
1511 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
1514 hr
= This
->fnSampleProc(This
->pin
.pUserData
, pSample
, dwUser
);
1518 /* FIXME: This is not well handled yet! */
1519 ERR("Processing error: %x\n", hr
);
1524 IMediaSample_Release(pSample
);
1527 } while (This
->rtCurrent
< This
->rtStop
&& hr
== S_OK
&& !This
->stop_playback
);
1529 /* Sample was rejected, and we are asked to terminate */
1532 IMediaSample_Release(pSample
);
1535 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
1536 * Flush remaining samples
1539 This
->fnDone(This
->pin
.pUserData
);
1541 TRACE("End: %08x, %d\n", hr
, This
->stop_playback
);
1544 static void CALLBACK
PullPin_Thread_Pause(PullPin
*This
)
1546 EnterCriticalSection(This
->pin
.pCritSec
);
1547 This
->state
= Req_Sleepy
;
1548 SetEvent(This
->hEventStateChanged
);
1549 LeaveCriticalSection(This
->pin
.pCritSec
);
1552 static void CALLBACK
PullPin_Thread_Stop(PullPin
*This
)
1554 TRACE("(%p)->()\n", This
);
1556 EnterCriticalSection(This
->pin
.pCritSec
);
1558 CloseHandle(This
->hThread
);
1559 This
->hThread
= NULL
;
1560 SetEvent(This
->hEventStateChanged
);
1562 LeaveCriticalSection(This
->pin
.pCritSec
);
1564 IBaseFilter_Release(This
->pin
.pinInfo
.pFilter
);
1570 static void CALLBACK
PullPin_Thread_Flush(PullPin
*This
)
1572 PullPin_Flush(This
);
1574 EnterCriticalSection(This
->pin
.pCritSec
);
1575 This
->state
= Req_Sleepy
;
1576 SetEvent(This
->hEventStateChanged
);
1577 LeaveCriticalSection(This
->pin
.pCritSec
);
1580 static DWORD WINAPI
PullPin_Thread_Main(LPVOID pv
)
1583 CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
1585 PullPin_Flush(This
);
1589 WaitForSingleObject(This
->thread_sleepy
, INFINITE
);
1591 TRACE("State: %d\n", This
->state
);
1593 switch (This
->state
)
1595 case Req_Die
: PullPin_Thread_Stop(This
); break;
1596 case Req_Run
: PullPin_Thread_Process(This
); break;
1597 case Req_Pause
: PullPin_Thread_Pause(This
); break;
1598 case Req_Flush
: PullPin_Thread_Flush(This
); break;
1599 case Req_Sleepy
: ERR("Should not be signalled with SLEEPY!\n"); break;
1600 default: ERR("Unknown state request: %d\n", This
->state
); break;
1605 static HRESULT
PullPin_InitProcessing(PullPin
* This
)
1609 TRACE("(%p)->()\n", This
);
1611 /* if we are connected */
1616 WaitForSingleObject(This
->hEventStateChanged
, INFINITE
);
1617 EnterCriticalSection(This
->pin
.pCritSec
);
1619 assert(!This
->hThread
);
1620 assert(This
->state
== Req_Die
);
1621 assert(This
->stop_playback
);
1622 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1623 This
->state
= Req_Sleepy
;
1625 /* AddRef the filter to make sure it and it's pins will be around
1626 * as long as the thread */
1627 IBaseFilter_AddRef(This
->pin
.pinInfo
.pFilter
);
1630 This
->hThread
= CreateThread(NULL
, 0, PullPin_Thread_Main
, This
, 0, &dwThreadId
);
1633 hr
= HRESULT_FROM_WIN32(GetLastError());
1634 IBaseFilter_Release(This
->pin
.pinInfo
.pFilter
);
1639 SetEvent(This
->hEventStateChanged
);
1640 /* If assert fails, that means a command was not processed before the thread previously terminated */
1642 LeaveCriticalSection(This
->pin
.pCritSec
);
1645 TRACE(" -- %x\n", hr
);
1650 HRESULT
PullPin_StartProcessing(PullPin
* This
)
1652 /* if we are connected */
1653 TRACE("(%p)->()\n", This
);
1656 assert(This
->hThread
);
1658 PullPin_WaitForStateChange(This
, INFINITE
);
1660 assert(This
->state
== Req_Sleepy
);
1663 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1664 This
->state
= Req_Run
;
1665 This
->stop_playback
= 0;
1666 ResetEvent(This
->hEventStateChanged
);
1667 SetEvent(This
->thread_sleepy
);
1673 HRESULT
PullPin_PauseProcessing(PullPin
* This
)
1675 /* if we are connected */
1676 TRACE("(%p)->()\n", This
);
1679 assert(This
->hThread
);
1681 PullPin_WaitForStateChange(This
, INFINITE
);
1683 EnterCriticalSection(This
->pin
.pCritSec
);
1685 assert(!This
->stop_playback
);
1686 assert(This
->state
== Req_Run
|| This
->state
== Req_Sleepy
);
1688 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1689 This
->state
= Req_Pause
;
1690 This
->stop_playback
= 1;
1691 ResetEvent(This
->hEventStateChanged
);
1692 SetEvent(This
->thread_sleepy
);
1694 LeaveCriticalSection(This
->pin
.pCritSec
);
1700 static HRESULT
PullPin_StopProcessing(PullPin
* This
)
1702 TRACE("(%p)->()\n", This
);
1704 /* if we are alive */
1705 assert(This
->hThread
);
1707 PullPin_WaitForStateChange(This
, INFINITE
);
1709 assert(This
->state
== Req_Pause
|| This
->state
== Req_Sleepy
);
1711 This
->stop_playback
= 1;
1712 This
->state
= Req_Die
;
1713 assert(WaitForSingleObject(This
->thread_sleepy
, 0) == WAIT_TIMEOUT
);
1714 ResetEvent(This
->hEventStateChanged
);
1715 SetEvent(This
->thread_sleepy
);
1719 HRESULT
PullPin_WaitForStateChange(PullPin
* This
, DWORD dwMilliseconds
)
1721 if (WaitForSingleObject(This
->hEventStateChanged
, dwMilliseconds
) == WAIT_TIMEOUT
)
1726 HRESULT WINAPI
PullPin_EndOfStream(IPin
* iface
)
1728 FIXME("(%p)->() stub\n", iface
);
1730 return SendFurther( iface
, deliver_endofstream
, NULL
, NULL
);
1733 HRESULT WINAPI
PullPin_BeginFlush(IPin
* iface
)
1735 PullPin
*This
= (PullPin
*)iface
;
1736 TRACE("(%p)->()\n", This
);
1738 EnterCriticalSection(This
->pin
.pCritSec
);
1740 SendFurther( iface
, deliver_beginflush
, NULL
, NULL
);
1742 LeaveCriticalSection(This
->pin
.pCritSec
);
1744 EnterCriticalSection(&This
->thread_lock
);
1747 IAsyncReader_BeginFlush(This
->pReader
);
1748 PullPin_WaitForStateChange(This
, INFINITE
);
1750 if (This
->hThread
&& This
->state
== Req_Run
)
1752 PullPin_PauseProcessing(This
);
1753 PullPin_WaitForStateChange(This
, INFINITE
);
1756 This
->state
= Req_Flush
;
1757 ResetEvent(This
->hEventStateChanged
);
1758 SetEvent(This
->thread_sleepy
);
1759 PullPin_WaitForStateChange(This
, INFINITE
);
1761 LeaveCriticalSection(&This
->thread_lock
);
1763 EnterCriticalSection(This
->pin
.pCritSec
);
1765 This
->fnCleanProc(This
->pin
.pUserData
);
1767 LeaveCriticalSection(This
->pin
.pCritSec
);
1772 HRESULT WINAPI
PullPin_EndFlush(IPin
* iface
)
1774 PullPin
*This
= (PullPin
*)iface
;
1776 TRACE("(%p)->()\n", iface
);
1778 EnterCriticalSection(&This
->thread_lock
);
1781 IBaseFilter_GetState(This
->pin
.pinInfo
.pFilter
, INFINITE
, &state
);
1783 if (This
->stop_playback
&& state
!= State_Stopped
)
1784 PullPin_StartProcessing(This
);
1786 PullPin_WaitForStateChange(This
, INFINITE
);
1788 LeaveCriticalSection(&This
->thread_lock
);
1790 EnterCriticalSection(This
->pin
.pCritSec
);
1791 SendFurther( iface
, deliver_endflush
, NULL
, NULL
);
1792 LeaveCriticalSection(This
->pin
.pCritSec
);
1797 HRESULT WINAPI
PullPin_Disconnect(IPin
*iface
)
1800 PullPin
*This
= (PullPin
*)iface
;
1804 EnterCriticalSection(This
->pin
.pCritSec
);
1806 if (FAILED(hr
= IMemAllocator_Decommit(This
->pAlloc
)))
1807 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr
);
1809 if (This
->pin
.pConnectedTo
)
1811 IPin_Release(This
->pin
.pConnectedTo
);
1812 This
->pin
.pConnectedTo
= NULL
;
1813 PullPin_StopProcessing(This
);
1819 LeaveCriticalSection(This
->pin
.pCritSec
);
1824 HRESULT WINAPI
PullPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
1826 newsegmentargs args
;
1827 FIXME("(%p)->(%s, %s, %g) stub\n", iface
, wine_dbgstr_longlong(tStart
), wine_dbgstr_longlong(tStop
), dRate
);
1829 args
.tStart
= tStart
;
1833 return SendFurther( iface
, deliver_newsegment
, &args
, NULL
);
1836 static const IPinVtbl PullPin_Vtbl
=
1838 PullPin_QueryInterface
,
1842 PullPin_ReceiveConnection
,
1844 IPinImpl_ConnectedTo
,
1845 IPinImpl_ConnectionMediaType
,
1846 IPinImpl_QueryPinInfo
,
1847 IPinImpl_QueryDirection
,
1849 IPinImpl_QueryAccept
,
1850 IPinImpl_EnumMediaTypes
,
1851 IPinImpl_QueryInternalConnections
,
1852 PullPin_EndOfStream
,