Fix potential race in IPinImpl_ConnectedTo.
[wine/multimedia.git] / dlls / quartz / pin.c
blob3e946cf7d95b4e49a4b43d7ca10236536beb87de
1 /*
2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "quartz_private.h"
22 #include "pin.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include <assert.h>
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
32 static const struct IPinVtbl InputPin_Vtbl;
33 static const struct IPinVtbl OutputPin_Vtbl;
34 static const struct IMemInputPinVtbl MemInputPin_Vtbl;
35 static const struct IPinVtbl PullPin_Vtbl;
37 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
40 #define _IMemInputPin_Offset ((int)(&(((InputPin*)0)->lpVtblMemInput)))
41 #define ICOM_THIS_From_IMemInputPin(impl, iface) impl* This = (impl*)(((char*)iface)-_IMemInputPin_Offset);
43 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
45 /* Tempting to just do a memcpy, but the name field is
46 128 characters long! We will probably never exceed 10
47 most of the time, so we are better off copying
48 each field manually */
49 strcpyW(pDest->achName, pSrc->achName);
50 pDest->dir = pSrc->dir;
51 pDest->pFilter = pSrc->pFilter;
52 IBaseFilter_AddRef(pDest->pFilter);
55 /* Internal function called as a helper to IPin_Connect */
56 /* specific AM_MEDIA_TYPE - it cannot be NULL */
57 /* NOTE: not part of standard interface */
58 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
60 ICOM_THIS(OutputPin, iface);
61 HRESULT hr;
62 IMemAllocator * pMemAlloc = NULL;
63 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
65 TRACE("(%p, %p)\n", pReceivePin, pmt);
66 dump_AM_MEDIA_TYPE(pmt);
68 /* FIXME: call queryacceptproc */
70 This->pin.pConnectedTo = pReceivePin;
71 IPin_AddRef(pReceivePin);
72 CopyMediaType(&This->pin.mtCurrent, pmt);
74 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
76 /* get the IMemInputPin interface we will use to deliver samples to the
77 * connected pin */
78 if (SUCCEEDED(hr))
80 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
82 if (SUCCEEDED(hr))
83 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
85 if (SUCCEEDED(hr))
86 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
88 if (pMemAlloc)
89 IMemAllocator_Release(pMemAlloc);
91 /* break connection if we couldn't get the allocator */
92 if (FAILED(hr))
93 IPin_Disconnect(pReceivePin);
96 if (FAILED(hr))
98 IPin_Release(This->pin.pConnectedTo);
99 This->pin.pConnectedTo = NULL;
100 DeleteMediaType(&This->pin.mtCurrent);
103 TRACE(" -- %lx\n", hr);
104 return hr;
107 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
109 InputPin * pPinImpl;
111 *ppPin = NULL;
113 if (pPinInfo->dir != PINDIR_INPUT)
115 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
116 return E_INVALIDARG;
119 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
121 if (!pPinImpl)
122 return E_OUTOFMEMORY;
124 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
126 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
127 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
129 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
130 return S_OK;
132 return E_FAIL;
135 /* Note that we don't init the vtables here (like C++ constructor) */
136 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
138 TRACE("\n");
140 /* Common attributes */
141 pPinImpl->pin.refCount = 1;
142 pPinImpl->pin.pConnectedTo = NULL;
143 pPinImpl->pin.fnQueryAccept = pQueryAccept;
144 pPinImpl->pin.pUserData = pUserData;
145 pPinImpl->pin.pCritSec = pCritSec;
146 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
148 /* Input pin attributes */
149 pPinImpl->fnSampleProc = pSampleProc;
150 pPinImpl->pAllocator = NULL;
151 pPinImpl->tStart = 0;
152 pPinImpl->tStop = 0;
153 pPinImpl->dRate = 0;
155 return S_OK;
158 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
160 TRACE("\n");
162 /* Common attributes */
163 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
164 pPinImpl->pin.refCount = 1;
165 pPinImpl->pin.pConnectedTo = NULL;
166 pPinImpl->pin.fnQueryAccept = pQueryAccept;
167 pPinImpl->pin.pUserData = pUserData;
168 pPinImpl->pin.pCritSec = pCritSec;
169 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
171 /* Output pin attributes */
172 pPinImpl->pMemInputPin = NULL;
173 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
174 if (props)
176 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
177 if (pPinImpl->allocProps.cbAlign == 0)
178 pPinImpl->allocProps.cbAlign = 1;
180 else
181 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
184 return S_OK;
187 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
189 OutputPin * pPinImpl;
191 *ppPin = NULL;
193 if (pPinInfo->dir != PINDIR_OUTPUT)
195 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
196 return E_INVALIDARG;
199 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
201 if (!pPinImpl)
202 return E_OUTOFMEMORY;
204 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
206 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
208 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
209 return S_OK;
211 return E_FAIL;
214 /*** Common pin functions ***/
216 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
218 ICOM_THIS(IPinImpl, iface);
220 TRACE("()\n");
222 return InterlockedIncrement(&This->refCount);
225 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
227 HRESULT hr;
228 ICOM_THIS(IPinImpl, iface);
230 TRACE("()\n");
232 EnterCriticalSection(This->pCritSec);
234 if (This->pConnectedTo)
236 IPin_Release(This->pConnectedTo);
237 This->pConnectedTo = NULL;
238 hr = S_OK;
240 else
241 hr = S_FALSE;
243 LeaveCriticalSection(This->pCritSec);
245 return hr;
248 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
250 HRESULT hr;
251 ICOM_THIS(IPinImpl, iface);
253 /* TRACE("(%p)\n", ppPin);*/
255 EnterCriticalSection(This->pCritSec);
257 if (This->pConnectedTo)
259 *ppPin = This->pConnectedTo;
260 IPin_AddRef(*ppPin);
261 hr = S_OK;
263 else
264 hr = VFW_E_NOT_CONNECTED;
266 LeaveCriticalSection(This->pCritSec);
268 return hr;
271 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
273 HRESULT hr;
274 ICOM_THIS(IPinImpl, iface);
276 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
278 EnterCriticalSection(This->pCritSec);
280 if (This->pConnectedTo)
282 CopyMediaType(pmt, &This->mtCurrent);
283 hr = S_OK;
285 else
287 ZeroMemory(pmt, sizeof(*pmt));
288 hr = VFW_E_NOT_CONNECTED;
291 LeaveCriticalSection(This->pCritSec);
293 return hr;
296 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
298 ICOM_THIS(IPinImpl, iface);
300 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
302 Copy_PinInfo(pInfo, &This->pinInfo);
304 return S_OK;
307 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
309 ICOM_THIS(IPinImpl, iface);
311 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
313 *pPinDir = This->pinInfo.dir;
315 return S_OK;
318 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
320 ICOM_THIS(IPinImpl, iface);
322 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
324 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
325 if (!Id)
326 return E_OUTOFMEMORY;
328 strcpyW(*Id, This->pinInfo.achName);
330 return S_OK;
333 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
335 ICOM_THIS(IPinImpl, iface);
337 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
339 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
342 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
344 ICOM_THIS(IPinImpl, iface);
345 ENUMMEDIADETAILS emd;
347 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
349 /* override this method to allow enumeration of your types */
350 emd.cMediaTypes = 0;
351 emd.pMediaTypes = NULL;
353 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
356 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
358 ICOM_THIS(IPinImpl, iface);
360 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
362 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
365 /*** IPin implementation for an input pin ***/
367 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
369 ICOM_THIS(InputPin, iface);
371 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
373 *ppv = NULL;
375 if (IsEqualIID(riid, &IID_IUnknown))
376 *ppv = (LPVOID)iface;
377 else if (IsEqualIID(riid, &IID_IPin))
378 *ppv = (LPVOID)iface;
379 else if (IsEqualIID(riid, &IID_IMemInputPin))
380 *ppv = (LPVOID)&This->lpVtblMemInput;
382 if (*ppv)
384 IUnknown_AddRef((IUnknown *)(*ppv));
385 return S_OK;
388 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
390 return E_NOINTERFACE;
393 ULONG WINAPI InputPin_Release(IPin * iface)
395 ICOM_THIS(InputPin, iface);
397 TRACE("()\n");
399 if (!InterlockedDecrement(&This->pin.refCount))
401 DeleteMediaType(&This->pin.mtCurrent);
402 if (This->pAllocator)
403 IMemAllocator_Release(This->pAllocator);
404 CoTaskMemFree(This);
405 return 0;
407 else
408 return This->pin.refCount;
411 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
413 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
415 return E_UNEXPECTED;
419 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
421 ICOM_THIS(InputPin, iface);
422 PIN_DIRECTION pindirReceive;
423 HRESULT hr = S_OK;
425 TRACE("(%p, %p)\n", pReceivePin, pmt);
426 dump_AM_MEDIA_TYPE(pmt);
428 EnterCriticalSection(This->pin.pCritSec);
430 if (This->pin.pConnectedTo)
431 hr = VFW_E_ALREADY_CONNECTED;
433 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
434 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
435 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
437 if (SUCCEEDED(hr))
439 IPin_QueryDirection(pReceivePin, &pindirReceive);
441 if (pindirReceive != PINDIR_OUTPUT)
443 ERR("Can't connect from non-output pin\n");
444 hr = VFW_E_INVALID_DIRECTION;
448 if (SUCCEEDED(hr))
450 CopyMediaType(&This->pin.mtCurrent, pmt);
451 This->pin.pConnectedTo = pReceivePin;
452 IPin_AddRef(pReceivePin);
455 LeaveCriticalSection(This->pin.pCritSec);
457 return hr;
460 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
462 TRACE("()\n");
464 return S_OK;
467 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
469 FIXME("()\n");
470 return E_NOTIMPL;
473 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
475 FIXME("()\n");
476 return E_NOTIMPL;
479 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
481 ICOM_THIS(InputPin, iface);
483 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
485 This->tStart = tStart;
486 This->tStop = tStop;
487 This->dRate = dRate;
489 return S_OK;
492 static const IPinVtbl InputPin_Vtbl =
494 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
495 InputPin_QueryInterface,
496 IPinImpl_AddRef,
497 InputPin_Release,
498 InputPin_Connect,
499 InputPin_ReceiveConnection,
500 IPinImpl_Disconnect,
501 IPinImpl_ConnectedTo,
502 IPinImpl_ConnectionMediaType,
503 IPinImpl_QueryPinInfo,
504 IPinImpl_QueryDirection,
505 IPinImpl_QueryId,
506 IPinImpl_QueryAccept,
507 IPinImpl_EnumMediaTypes,
508 IPinImpl_QueryInternalConnections,
509 InputPin_EndOfStream,
510 InputPin_BeginFlush,
511 InputPin_EndFlush,
512 InputPin_NewSegment
515 /*** IMemInputPin implementation ***/
517 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
519 ICOM_THIS_From_IMemInputPin(InputPin, iface);
521 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
524 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
526 ICOM_THIS_From_IMemInputPin(InputPin, iface);
528 return IPin_AddRef((IPin *)&This->pin);
531 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
533 ICOM_THIS_From_IMemInputPin(InputPin, iface);
535 return IPin_Release((IPin *)&This->pin);
538 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
540 ICOM_THIS_From_IMemInputPin(InputPin, iface);
542 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
544 *ppAllocator = This->pAllocator;
545 if (*ppAllocator)
546 IMemAllocator_AddRef(*ppAllocator);
548 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
551 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
553 ICOM_THIS_From_IMemInputPin(InputPin, iface);
555 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
557 if (This->pAllocator)
558 IMemAllocator_Release(This->pAllocator);
559 This->pAllocator = pAllocator;
560 if (This->pAllocator)
561 IMemAllocator_AddRef(This->pAllocator);
563 return S_OK;
566 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
568 ICOM_THIS_From_IMemInputPin(InputPin, iface);
570 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
572 /* override this method if you have any specific requirements */
574 return E_NOTIMPL;
577 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
579 ICOM_THIS_From_IMemInputPin(InputPin, iface);
581 /* this trace commented out for performance reasons */
582 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
584 return This->fnSampleProc(This->pin.pUserData, pSample);
587 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
589 HRESULT hr = S_OK;
590 ICOM_THIS_From_IMemInputPin(InputPin, iface);
592 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
594 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
596 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
597 if (hr != S_OK)
598 break;
601 return hr;
604 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
606 ICOM_THIS_From_IMemInputPin(InputPin, iface);
608 FIXME("(%p/%p)->()\n", This, iface);
610 /* FIXME: we should check whether any output pins will block */
612 return S_OK;
615 static const IMemInputPinVtbl MemInputPin_Vtbl =
617 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
618 MemInputPin_QueryInterface,
619 MemInputPin_AddRef,
620 MemInputPin_Release,
621 MemInputPin_GetAllocator,
622 MemInputPin_NotifyAllocator,
623 MemInputPin_GetAllocatorRequirements,
624 MemInputPin_Receive,
625 MemInputPin_ReceiveMultiple,
626 MemInputPin_ReceiveCanBlock
629 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
631 ICOM_THIS(OutputPin, iface);
633 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
635 *ppv = NULL;
637 if (IsEqualIID(riid, &IID_IUnknown))
638 *ppv = (LPVOID)iface;
639 else if (IsEqualIID(riid, &IID_IPin))
640 *ppv = (LPVOID)iface;
642 if (*ppv)
644 IUnknown_AddRef((IUnknown *)(*ppv));
645 return S_OK;
648 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
650 return E_NOINTERFACE;
653 ULONG WINAPI OutputPin_Release(IPin * iface)
655 ICOM_THIS(OutputPin, iface);
657 TRACE("(%p/%p)->()\n", This, iface);
659 if (!InterlockedDecrement(&This->pin.refCount))
661 DeleteMediaType(&This->pin.mtCurrent);
662 CoTaskMemFree(This);
663 return 0;
665 return This->pin.refCount;
668 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
670 HRESULT hr;
671 ICOM_THIS(OutputPin, iface);
673 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
674 dump_AM_MEDIA_TYPE(pmt);
676 /* If we try to connect to ourself, we will definitely deadlock.
677 * There are other cases where we could deadlock too, but this
678 * catches the obvious case */
679 assert(pReceivePin != iface);
681 EnterCriticalSection(This->pin.pCritSec);
683 /* if we have been a specific type to connect with, then we can either connect
684 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
685 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
686 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
687 else
689 /* negotiate media type */
691 IEnumMediaTypes * pEnumCandidates;
692 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
694 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
696 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
698 /* try this filter's media types first */
699 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
701 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
702 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
704 hr = S_OK;
705 CoTaskMemFree(pmtCandidate);
706 break;
708 CoTaskMemFree(pmtCandidate);
710 IEnumMediaTypes_Release(pEnumCandidates);
713 /* then try receiver filter's media types */
714 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
716 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
718 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
720 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
721 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
723 hr = S_OK;
724 CoTaskMemFree(pmtCandidate);
725 break;
727 CoTaskMemFree(pmtCandidate);
728 } /* while */
729 IEnumMediaTypes_Release(pEnumCandidates);
730 } /* if not found */
731 } /* if negotiate media type */
732 } /* if succeeded */
733 LeaveCriticalSection(This->pin.pCritSec);
735 FIXME(" -- %lx\n", hr);
736 return hr;
739 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
741 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
743 return E_UNEXPECTED;
746 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
748 HRESULT hr;
749 ICOM_THIS(OutputPin, iface);
751 TRACE("()\n");
753 EnterCriticalSection(This->pin.pCritSec);
755 if (This->pMemInputPin)
757 IMemInputPin_Release(This->pMemInputPin);
758 This->pMemInputPin = NULL;
760 if (This->pin.pConnectedTo)
762 IPin_Release(This->pin.pConnectedTo);
763 This->pin.pConnectedTo = NULL;
764 hr = S_OK;
766 else
767 hr = S_FALSE;
769 LeaveCriticalSection(This->pin.pCritSec);
771 return hr;
774 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
776 TRACE("()\n");
778 /* not supposed to do anything in an output pin */
780 return E_UNEXPECTED;
783 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
785 TRACE("(%p)->()\n", iface);
787 /* not supposed to do anything in an output pin */
789 return E_UNEXPECTED;
792 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
794 TRACE("(%p)->()\n", iface);
796 /* not supposed to do anything in an output pin */
798 return E_UNEXPECTED;
801 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
803 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
805 /* not supposed to do anything in an output pin */
807 return E_UNEXPECTED;
810 static const IPinVtbl OutputPin_Vtbl =
812 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
813 OutputPin_QueryInterface,
814 IPinImpl_AddRef,
815 OutputPin_Release,
816 OutputPin_Connect,
817 OutputPin_ReceiveConnection,
818 OutputPin_Disconnect,
819 IPinImpl_ConnectedTo,
820 IPinImpl_ConnectionMediaType,
821 IPinImpl_QueryPinInfo,
822 IPinImpl_QueryDirection,
823 IPinImpl_QueryId,
824 IPinImpl_QueryAccept,
825 IPinImpl_EnumMediaTypes,
826 IPinImpl_QueryInternalConnections,
827 OutputPin_EndOfStream,
828 OutputPin_BeginFlush,
829 OutputPin_EndFlush,
830 OutputPin_NewSegment
833 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
835 HRESULT hr;
837 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
839 EnterCriticalSection(This->pin.pCritSec);
841 if (!This->pin.pConnectedTo)
842 hr = VFW_E_NOT_CONNECTED;
843 else
845 IMemInputPin * pMemInputPin = NULL;
846 IMemAllocator * pAlloc = NULL;
848 /* FIXME: should we cache this? */
849 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
851 if (SUCCEEDED(hr))
852 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
854 if (SUCCEEDED(hr))
855 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
857 if (SUCCEEDED(hr))
858 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
860 if (pAlloc)
861 IMemAllocator_Release(pAlloc);
862 if (pMemInputPin)
863 IMemInputPin_Release(pMemInputPin);
866 LeaveCriticalSection(This->pin.pCritSec);
868 return hr;
871 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
873 HRESULT hr;
874 IMemInputPin * pMemConnected = NULL;
876 EnterCriticalSection(This->pin.pCritSec);
878 if (!This->pin.pConnectedTo)
879 hr = VFW_E_NOT_CONNECTED;
880 else
882 /* FIXME: should we cache this? - if we do, then we need to keep the local version
883 * and AddRef here instead */
884 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemConnected);
887 LeaveCriticalSection(This->pin.pCritSec);
889 if (SUCCEEDED(hr) && pMemConnected)
891 /* NOTE: if we are in a critical section when Receive is called
892 * then it causes some problems (most notably with the native Video
893 * Renderer) if we are re-entered for whatever reason */
894 hr = IMemInputPin_Receive(pMemConnected, pSample);
895 IMemInputPin_Release(pMemConnected);
898 return hr;
901 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
903 HRESULT hr;
905 EnterCriticalSection(This->pin.pCritSec);
907 if (!This->pin.pConnectedTo)
908 hr = VFW_E_NOT_CONNECTED;
909 else
910 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
912 LeaveCriticalSection(This->pin.pCritSec);
914 return hr;
917 HRESULT OutputPin_CommitAllocator(OutputPin * This)
919 HRESULT hr;
921 TRACE("(%p)->()\n", This);
923 EnterCriticalSection(This->pin.pCritSec);
925 if (!This->pin.pConnectedTo)
926 hr = VFW_E_NOT_CONNECTED;
927 else
929 IMemInputPin * pMemInputPin = NULL;
930 IMemAllocator * pAlloc = NULL;
931 /* FIXME: should we cache this? */
932 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
934 if (SUCCEEDED(hr))
935 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
937 if (SUCCEEDED(hr))
938 hr = IMemAllocator_Commit(pAlloc);
940 if (pAlloc)
941 IMemAllocator_Release(pAlloc);
943 if (pMemInputPin)
944 IMemInputPin_Release(pMemInputPin);
947 LeaveCriticalSection(This->pin.pCritSec);
949 return hr;
952 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
954 HRESULT hr;
956 TRACE("(%p)->()\n", This);
958 EnterCriticalSection(This->pin.pCritSec);
960 if (!This->pin.pConnectedTo)
961 hr = VFW_E_NOT_CONNECTED;
962 else
964 IMemInputPin * pMemInputPin = NULL;
965 IMemAllocator * pAlloc = NULL;
966 /* FIXME: should we cache this? */
967 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
969 if (SUCCEEDED(hr))
970 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
972 if (SUCCEEDED(hr))
973 hr = IMemAllocator_Decommit(pAlloc);
975 if (pAlloc)
976 IMemAllocator_Release(pAlloc);
978 if (pMemInputPin)
979 IMemInputPin_Release(pMemInputPin);
981 if (SUCCEEDED(hr))
982 hr = IPin_Disconnect(This->pin.pConnectedTo);
985 LeaveCriticalSection(This->pin.pCritSec);
987 return hr;
991 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
993 PullPin * pPinImpl;
995 *ppPin = NULL;
997 if (pPinInfo->dir != PINDIR_INPUT)
999 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1000 return E_INVALIDARG;
1003 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1005 if (!pPinImpl)
1006 return E_OUTOFMEMORY;
1008 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1010 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1012 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1013 return S_OK;
1015 return E_FAIL;
1018 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1020 /* Common attributes */
1021 pPinImpl->pin.refCount = 1;
1022 pPinImpl->pin.pConnectedTo = NULL;
1023 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1024 pPinImpl->pin.pUserData = pUserData;
1025 pPinImpl->pin.pCritSec = pCritSec;
1026 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1028 /* Input pin attributes */
1029 pPinImpl->fnSampleProc = pSampleProc;
1030 pPinImpl->fnPreConnect = NULL;
1031 pPinImpl->pAlloc = NULL;
1032 pPinImpl->pReader = NULL;
1033 pPinImpl->hThread = NULL;
1034 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1036 pPinImpl->rtStart = 0;
1037 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1039 return S_OK;
1042 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1044 PIN_DIRECTION pindirReceive;
1045 HRESULT hr = S_OK;
1046 ICOM_THIS(PullPin, iface);
1048 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1049 dump_AM_MEDIA_TYPE(pmt);
1051 EnterCriticalSection(This->pin.pCritSec);
1053 if (This->pin.pConnectedTo)
1054 hr = VFW_E_ALREADY_CONNECTED;
1056 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1057 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1058 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1060 if (SUCCEEDED(hr))
1062 IPin_QueryDirection(pReceivePin, &pindirReceive);
1064 if (pindirReceive != PINDIR_OUTPUT)
1066 ERR("Can't connect from non-output pin\n");
1067 hr = VFW_E_INVALID_DIRECTION;
1071 if (SUCCEEDED(hr))
1073 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1076 if (SUCCEEDED(hr))
1078 ALLOCATOR_PROPERTIES props;
1079 props.cBuffers = 3;
1080 props.cbBuffer = 64 * 1024; /* 64k bytes */
1081 props.cbAlign = 1;
1082 props.cbPrefix = 0;
1083 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1086 if (SUCCEEDED(hr) && This->fnPreConnect)
1088 hr = This->fnPreConnect(iface, pReceivePin);
1091 if (SUCCEEDED(hr))
1093 CopyMediaType(&This->pin.mtCurrent, pmt);
1094 This->pin.pConnectedTo = pReceivePin;
1095 IPin_AddRef(pReceivePin);
1098 LeaveCriticalSection(This->pin.pCritSec);
1099 return hr;
1102 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1104 ICOM_THIS(PullPin, iface);
1106 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1108 *ppv = NULL;
1110 if (IsEqualIID(riid, &IID_IUnknown))
1111 *ppv = (LPVOID)iface;
1112 else if (IsEqualIID(riid, &IID_IPin))
1113 *ppv = (LPVOID)iface;
1115 if (*ppv)
1117 IUnknown_AddRef((IUnknown *)(*ppv));
1118 return S_OK;
1121 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1123 return E_NOINTERFACE;
1126 ULONG WINAPI PullPin_Release(IPin * iface)
1128 ICOM_THIS(PullPin, iface);
1130 TRACE("(%p/%p)->()\n", This, iface);
1132 if (!InterlockedDecrement(&This->pin.refCount))
1134 if (This->hThread)
1135 PullPin_StopProcessing(This);
1136 IMemAllocator_Release(This->pAlloc);
1137 IAsyncReader_Release(This->pReader);
1138 CloseHandle(This->hEventStateChanged);
1139 CoTaskMemFree(This);
1140 return 0;
1142 return This->pin.refCount;
1145 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1147 for (;;)
1148 SleepEx(INFINITE, TRUE);
1151 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1153 ICOM_THIS(PullPin, iface);
1154 HRESULT hr;
1156 REFERENCE_TIME rtCurrent;
1157 ALLOCATOR_PROPERTIES allocProps;
1159 SetEvent(This->hEventStateChanged);
1161 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1163 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1165 while (rtCurrent < This->rtStop)
1167 /* FIXME: to improve performance by quite a bit this should be changed
1168 * so that one sample is processed while one sample is fetched. However,
1169 * it is harder to debug so for the moment it will stay as it is */
1170 IMediaSample * pSample = NULL;
1171 REFERENCE_TIME rtSampleStop;
1172 DWORD dwUser;
1174 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1176 if (SUCCEEDED(hr))
1178 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1179 if (rtSampleStop > This->rtStop)
1180 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1181 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1182 rtCurrent = rtSampleStop;
1185 if (SUCCEEDED(hr))
1186 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1188 if (SUCCEEDED(hr))
1189 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1191 if (SUCCEEDED(hr))
1192 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1193 else
1194 ERR("Processing error: %lx\n", hr);
1196 if (pSample)
1197 IMediaSample_Release(pSample);
1201 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1203 ICOM_THIS(PullPin, iface);
1205 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1207 EnterCriticalSection(This->pin.pCritSec);
1209 HRESULT hr;
1211 CloseHandle(This->hThread);
1212 This->hThread = NULL;
1213 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1214 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1216 LeaveCriticalSection(This->pin.pCritSec);
1218 SetEvent(This->hEventStateChanged);
1220 ExitThread(0);
1223 HRESULT PullPin_InitProcessing(PullPin * This)
1225 HRESULT hr = S_OK;
1227 TRACE("(%p)->()\n", This);
1229 assert(!This->hThread);
1231 /* if we are connected */
1232 if (This->pAlloc)
1234 EnterCriticalSection(This->pin.pCritSec);
1236 DWORD dwThreadId;
1237 assert(!This->hThread);
1239 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1240 if (!This->hThread)
1241 hr = HRESULT_FROM_WIN32(GetLastError());
1243 if (SUCCEEDED(hr))
1244 hr = IMemAllocator_Commit(This->pAlloc);
1246 LeaveCriticalSection(This->pin.pCritSec);
1249 TRACE(" -- %lx\n", hr);
1251 return hr;
1254 HRESULT PullPin_StartProcessing(PullPin * This)
1256 /* if we are connected */
1257 TRACE("(%p)->()\n", This);
1258 if(This->pAlloc)
1260 assert(This->hThread);
1262 ResetEvent(This->hEventStateChanged);
1264 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1265 return HRESULT_FROM_WIN32(GetLastError());
1268 return S_OK;
1271 HRESULT PullPin_PauseProcessing(PullPin * This)
1273 /* make the processing function exit its loop */
1274 This->rtStop = 0;
1276 return S_OK;
1279 HRESULT PullPin_StopProcessing(PullPin * This)
1281 /* if we are connected */
1282 if (This->pAlloc)
1284 assert(This->hThread);
1286 ResetEvent(This->hEventStateChanged);
1288 PullPin_PauseProcessing(This);
1290 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1291 return HRESULT_FROM_WIN32(GetLastError());
1294 return S_OK;
1297 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1299 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1300 return S_FALSE;
1301 return S_OK;
1304 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1306 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1308 PullPin_BeginFlush((IPin *)This);
1309 /* FIXME: need critical section? */
1310 This->rtStart = rtStart;
1311 This->rtStop = rtStop;
1312 PullPin_EndFlush((IPin *)This);
1314 return S_OK;
1317 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1319 FIXME("(%p)->()\n", iface);
1320 return E_NOTIMPL;
1323 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1325 FIXME("(%p)->()\n", iface);
1326 return E_NOTIMPL;
1329 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1331 FIXME("(%p)->()\n", iface);
1332 return E_NOTIMPL;
1335 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1337 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1338 return E_NOTIMPL;
1341 static const IPinVtbl PullPin_Vtbl =
1343 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1344 PullPin_QueryInterface,
1345 IPinImpl_AddRef,
1346 PullPin_Release,
1347 OutputPin_Connect,
1348 PullPin_ReceiveConnection,
1349 IPinImpl_Disconnect,
1350 IPinImpl_ConnectedTo,
1351 IPinImpl_ConnectionMediaType,
1352 IPinImpl_QueryPinInfo,
1353 IPinImpl_QueryDirection,
1354 IPinImpl_QueryId,
1355 IPinImpl_QueryAccept,
1356 IPinImpl_EnumMediaTypes,
1357 IPinImpl_QueryInternalConnections,
1358 PullPin_EndOfStream,
1359 PullPin_BeginFlush,
1360 PullPin_EndFlush,
1361 PullPin_NewSegment