Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / quartz / pin.c
blobd2266ac2e9a5ccc3f258cdd2d0779009e5450563
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;
54 /* Function called as a helper to IPin_Connect */
55 /* specific AM_MEDIA_TYPE - it cannot be NULL */
56 /* NOTE: not part of standard interface */
57 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
59 OutputPin *This = (OutputPin *)iface;
60 HRESULT hr;
61 IMemAllocator * pMemAlloc = NULL;
62 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
64 TRACE("(%p, %p)\n", pReceivePin, pmt);
65 dump_AM_MEDIA_TYPE(pmt);
67 /* FIXME: call queryacceptproc */
69 This->pin.pConnectedTo = pReceivePin;
70 IPin_AddRef(pReceivePin);
71 CopyMediaType(&This->pin.mtCurrent, pmt);
73 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
75 /* get the IMemInputPin interface we will use to deliver samples to the
76 * connected pin */
77 if (SUCCEEDED(hr))
79 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
81 if (SUCCEEDED(hr))
82 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
84 if (hr == VFW_E_NO_ALLOCATOR)
86 /* Input pin provides no allocator, use standard memory allocator */
87 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
89 if (SUCCEEDED(hr))
91 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
95 if (SUCCEEDED(hr))
96 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
98 if (pMemAlloc)
99 IMemAllocator_Release(pMemAlloc);
101 /* break connection if we couldn't get the allocator */
102 if (FAILED(hr))
103 IPin_Disconnect(pReceivePin);
106 if (FAILED(hr))
108 IPin_Release(This->pin.pConnectedTo);
109 This->pin.pConnectedTo = NULL;
110 DeleteMediaType(&This->pin.mtCurrent);
113 TRACE(" -- %lx\n", hr);
114 return hr;
117 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
119 InputPin * pPinImpl;
121 *ppPin = NULL;
123 if (pPinInfo->dir != PINDIR_INPUT)
125 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
126 return E_INVALIDARG;
129 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
131 if (!pPinImpl)
132 return E_OUTOFMEMORY;
134 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
136 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
137 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
139 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
140 return S_OK;
142 return E_FAIL;
145 /* Note that we don't init the vtables here (like C++ constructor) */
146 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
148 TRACE("\n");
150 /* Common attributes */
151 pPinImpl->pin.refCount = 1;
152 pPinImpl->pin.pConnectedTo = NULL;
153 pPinImpl->pin.fnQueryAccept = pQueryAccept;
154 pPinImpl->pin.pUserData = pUserData;
155 pPinImpl->pin.pCritSec = pCritSec;
156 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
158 /* Input pin attributes */
159 pPinImpl->fnSampleProc = pSampleProc;
160 pPinImpl->pAllocator = NULL;
161 pPinImpl->tStart = 0;
162 pPinImpl->tStop = 0;
163 pPinImpl->dRate = 0;
165 return S_OK;
168 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
170 TRACE("\n");
172 /* Common attributes */
173 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
174 pPinImpl->pin.refCount = 1;
175 pPinImpl->pin.pConnectedTo = NULL;
176 pPinImpl->pin.fnQueryAccept = pQueryAccept;
177 pPinImpl->pin.pUserData = pUserData;
178 pPinImpl->pin.pCritSec = pCritSec;
179 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
181 /* Output pin attributes */
182 pPinImpl->pMemInputPin = NULL;
183 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
184 if (props)
186 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
187 if (pPinImpl->allocProps.cbAlign == 0)
188 pPinImpl->allocProps.cbAlign = 1;
190 else
191 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
193 return S_OK;
196 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
198 OutputPin * pPinImpl;
200 *ppPin = NULL;
202 if (pPinInfo->dir != PINDIR_OUTPUT)
204 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
205 return E_INVALIDARG;
208 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
210 if (!pPinImpl)
211 return E_OUTOFMEMORY;
213 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
215 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
217 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
218 return S_OK;
220 return E_FAIL;
223 /*** Common pin functions ***/
225 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
227 IPinImpl *This = (IPinImpl *)iface;
228 ULONG refCount = InterlockedIncrement(&This->refCount);
230 TRACE("(%p)->() AddRef from %ld\n", iface, refCount - 1);
232 return refCount;
235 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
237 HRESULT hr;
238 IPinImpl *This = (IPinImpl *)iface;
240 TRACE("()\n");
242 EnterCriticalSection(This->pCritSec);
244 if (This->pConnectedTo)
246 IPin_Release(This->pConnectedTo);
247 This->pConnectedTo = NULL;
248 hr = S_OK;
250 else
251 hr = S_FALSE;
253 LeaveCriticalSection(This->pCritSec);
255 return hr;
258 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
260 HRESULT hr;
261 IPinImpl *This = (IPinImpl *)iface;
263 /* TRACE("(%p)\n", ppPin);*/
265 EnterCriticalSection(This->pCritSec);
267 if (This->pConnectedTo)
269 *ppPin = This->pConnectedTo;
270 IPin_AddRef(*ppPin);
271 hr = S_OK;
273 else
274 hr = VFW_E_NOT_CONNECTED;
276 LeaveCriticalSection(This->pCritSec);
278 return hr;
281 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
283 HRESULT hr;
284 IPinImpl *This = (IPinImpl *)iface;
286 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
288 EnterCriticalSection(This->pCritSec);
290 if (This->pConnectedTo)
292 CopyMediaType(pmt, &This->mtCurrent);
293 hr = S_OK;
295 else
297 ZeroMemory(pmt, sizeof(*pmt));
298 hr = VFW_E_NOT_CONNECTED;
301 LeaveCriticalSection(This->pCritSec);
303 return hr;
306 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
308 IPinImpl *This = (IPinImpl *)iface;
310 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
312 Copy_PinInfo(pInfo, &This->pinInfo);
313 IBaseFilter_AddRef(pInfo->pFilter);
315 return S_OK;
318 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
320 IPinImpl *This = (IPinImpl *)iface;
322 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
324 *pPinDir = This->pinInfo.dir;
326 return S_OK;
329 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
331 IPinImpl *This = (IPinImpl *)iface;
333 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
335 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
336 if (!Id)
337 return E_OUTOFMEMORY;
339 strcpyW(*Id, This->pinInfo.achName);
341 return S_OK;
344 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
346 IPinImpl *This = (IPinImpl *)iface;
348 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
350 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
353 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
355 IPinImpl *This = (IPinImpl *)iface;
356 ENUMMEDIADETAILS emd;
358 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
360 /* override this method to allow enumeration of your types */
361 emd.cMediaTypes = 0;
362 emd.pMediaTypes = NULL;
364 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
367 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
369 IPinImpl *This = (IPinImpl *)iface;
371 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
373 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
376 /*** IPin implementation for an input pin ***/
378 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
380 InputPin *This = (InputPin *)iface;
382 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
384 *ppv = NULL;
386 if (IsEqualIID(riid, &IID_IUnknown))
387 *ppv = (LPVOID)iface;
388 else if (IsEqualIID(riid, &IID_IPin))
389 *ppv = (LPVOID)iface;
390 else if (IsEqualIID(riid, &IID_IMemInputPin))
391 *ppv = (LPVOID)&This->lpVtblMemInput;
393 if (*ppv)
395 IUnknown_AddRef((IUnknown *)(*ppv));
396 return S_OK;
399 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
401 return E_NOINTERFACE;
404 ULONG WINAPI InputPin_Release(IPin * iface)
406 InputPin *This = (InputPin *)iface;
407 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
409 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
411 if (!refCount)
413 DeleteMediaType(&This->pin.mtCurrent);
414 if (This->pAllocator)
415 IMemAllocator_Release(This->pAllocator);
416 CoTaskMemFree(This);
417 return 0;
419 else
420 return refCount;
423 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
425 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
427 return E_UNEXPECTED;
431 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
433 InputPin *This = (InputPin *)iface;
434 PIN_DIRECTION pindirReceive;
435 HRESULT hr = S_OK;
437 TRACE("(%p, %p)\n", pReceivePin, pmt);
438 dump_AM_MEDIA_TYPE(pmt);
440 EnterCriticalSection(This->pin.pCritSec);
442 if (This->pin.pConnectedTo)
443 hr = VFW_E_ALREADY_CONNECTED;
445 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
446 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
447 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
449 if (SUCCEEDED(hr))
451 IPin_QueryDirection(pReceivePin, &pindirReceive);
453 if (pindirReceive != PINDIR_OUTPUT)
455 ERR("Can't connect from non-output pin\n");
456 hr = VFW_E_INVALID_DIRECTION;
460 if (SUCCEEDED(hr))
462 CopyMediaType(&This->pin.mtCurrent, pmt);
463 This->pin.pConnectedTo = pReceivePin;
464 IPin_AddRef(pReceivePin);
467 LeaveCriticalSection(This->pin.pCritSec);
469 return hr;
472 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
474 TRACE("()\n");
476 return S_OK;
479 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
481 FIXME("()\n");
482 return E_NOTIMPL;
485 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
487 FIXME("()\n");
488 return E_NOTIMPL;
491 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
493 InputPin *This = (InputPin *)iface;
495 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
497 This->tStart = tStart;
498 This->tStop = tStop;
499 This->dRate = dRate;
501 return S_OK;
504 static const IPinVtbl InputPin_Vtbl =
506 InputPin_QueryInterface,
507 IPinImpl_AddRef,
508 InputPin_Release,
509 InputPin_Connect,
510 InputPin_ReceiveConnection,
511 IPinImpl_Disconnect,
512 IPinImpl_ConnectedTo,
513 IPinImpl_ConnectionMediaType,
514 IPinImpl_QueryPinInfo,
515 IPinImpl_QueryDirection,
516 IPinImpl_QueryId,
517 IPinImpl_QueryAccept,
518 IPinImpl_EnumMediaTypes,
519 IPinImpl_QueryInternalConnections,
520 InputPin_EndOfStream,
521 InputPin_BeginFlush,
522 InputPin_EndFlush,
523 InputPin_NewSegment
526 /*** IMemInputPin implementation ***/
528 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
530 ICOM_THIS_From_IMemInputPin(InputPin, iface);
532 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
535 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
537 ICOM_THIS_From_IMemInputPin(InputPin, iface);
539 return IPin_AddRef((IPin *)&This->pin);
542 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
544 ICOM_THIS_From_IMemInputPin(InputPin, iface);
546 return IPin_Release((IPin *)&This->pin);
549 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
551 ICOM_THIS_From_IMemInputPin(InputPin, iface);
553 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
555 *ppAllocator = This->pAllocator;
556 if (*ppAllocator)
557 IMemAllocator_AddRef(*ppAllocator);
559 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
562 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
564 ICOM_THIS_From_IMemInputPin(InputPin, iface);
566 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
568 if (This->pAllocator)
569 IMemAllocator_Release(This->pAllocator);
570 This->pAllocator = pAllocator;
571 if (This->pAllocator)
572 IMemAllocator_AddRef(This->pAllocator);
574 return S_OK;
577 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
579 ICOM_THIS_From_IMemInputPin(InputPin, iface);
581 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
583 /* override this method if you have any specific requirements */
585 return E_NOTIMPL;
588 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
590 ICOM_THIS_From_IMemInputPin(InputPin, iface);
592 /* this trace commented out for performance reasons */
593 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
595 return This->fnSampleProc(This->pin.pUserData, pSample);
598 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
600 HRESULT hr = S_OK;
601 ICOM_THIS_From_IMemInputPin(InputPin, iface);
603 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
605 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
607 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
608 if (hr != S_OK)
609 break;
612 return hr;
615 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
617 ICOM_THIS_From_IMemInputPin(InputPin, iface);
619 FIXME("(%p/%p)->()\n", This, iface);
621 /* FIXME: we should check whether any output pins will block */
623 return S_OK;
626 static const IMemInputPinVtbl MemInputPin_Vtbl =
628 MemInputPin_QueryInterface,
629 MemInputPin_AddRef,
630 MemInputPin_Release,
631 MemInputPin_GetAllocator,
632 MemInputPin_NotifyAllocator,
633 MemInputPin_GetAllocatorRequirements,
634 MemInputPin_Receive,
635 MemInputPin_ReceiveMultiple,
636 MemInputPin_ReceiveCanBlock
639 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
641 OutputPin *This = (OutputPin *)iface;
643 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
645 *ppv = NULL;
647 if (IsEqualIID(riid, &IID_IUnknown))
648 *ppv = (LPVOID)iface;
649 else if (IsEqualIID(riid, &IID_IPin))
650 *ppv = (LPVOID)iface;
652 if (*ppv)
654 IUnknown_AddRef((IUnknown *)(*ppv));
655 return S_OK;
658 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
660 return E_NOINTERFACE;
663 ULONG WINAPI OutputPin_Release(IPin * iface)
665 OutputPin *This = (OutputPin *)iface;
666 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
668 TRACE("(%p/%p)->()\n", This, iface);
670 if (!refCount)
672 DeleteMediaType(&This->pin.mtCurrent);
673 CoTaskMemFree(This);
674 return 0;
676 return refCount;
679 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
681 HRESULT hr;
682 OutputPin *This = (OutputPin *)iface;
684 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
685 dump_AM_MEDIA_TYPE(pmt);
687 /* If we try to connect to ourself, we will definitely deadlock.
688 * There are other cases where we could deadlock too, but this
689 * catches the obvious case */
690 assert(pReceivePin != iface);
692 EnterCriticalSection(This->pin.pCritSec);
694 /* if we have been a specific type to connect with, then we can either connect
695 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
696 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
697 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
698 else
700 /* negotiate media type */
702 IEnumMediaTypes * pEnumCandidates;
703 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
705 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
707 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
709 /* try this filter's media types first */
710 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
712 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
713 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
715 hr = S_OK;
716 CoTaskMemFree(pmtCandidate);
717 break;
719 CoTaskMemFree(pmtCandidate);
721 IEnumMediaTypes_Release(pEnumCandidates);
724 /* then try receiver filter's media types */
725 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
727 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
729 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
731 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
732 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
734 hr = S_OK;
735 CoTaskMemFree(pmtCandidate);
736 break;
738 CoTaskMemFree(pmtCandidate);
739 } /* while */
740 IEnumMediaTypes_Release(pEnumCandidates);
741 } /* if not found */
742 } /* if negotiate media type */
743 } /* if succeeded */
744 LeaveCriticalSection(This->pin.pCritSec);
746 TRACE(" -- %lx\n", hr);
747 return hr;
750 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
752 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
754 return E_UNEXPECTED;
757 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
759 HRESULT hr;
760 OutputPin *This = (OutputPin *)iface;
762 TRACE("()\n");
764 EnterCriticalSection(This->pin.pCritSec);
766 if (This->pMemInputPin)
768 IMemInputPin_Release(This->pMemInputPin);
769 This->pMemInputPin = NULL;
771 if (This->pin.pConnectedTo)
773 IPin_Release(This->pin.pConnectedTo);
774 This->pin.pConnectedTo = NULL;
775 hr = S_OK;
777 else
778 hr = S_FALSE;
780 LeaveCriticalSection(This->pin.pCritSec);
782 return hr;
785 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
787 TRACE("()\n");
789 /* not supposed to do anything in an output pin */
791 return E_UNEXPECTED;
794 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
796 TRACE("(%p)->()\n", iface);
798 /* not supposed to do anything in an output pin */
800 return E_UNEXPECTED;
803 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
805 TRACE("(%p)->()\n", iface);
807 /* not supposed to do anything in an output pin */
809 return E_UNEXPECTED;
812 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
814 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
816 /* not supposed to do anything in an output pin */
818 return E_UNEXPECTED;
821 static const IPinVtbl OutputPin_Vtbl =
823 OutputPin_QueryInterface,
824 IPinImpl_AddRef,
825 OutputPin_Release,
826 OutputPin_Connect,
827 OutputPin_ReceiveConnection,
828 OutputPin_Disconnect,
829 IPinImpl_ConnectedTo,
830 IPinImpl_ConnectionMediaType,
831 IPinImpl_QueryPinInfo,
832 IPinImpl_QueryDirection,
833 IPinImpl_QueryId,
834 IPinImpl_QueryAccept,
835 IPinImpl_EnumMediaTypes,
836 IPinImpl_QueryInternalConnections,
837 OutputPin_EndOfStream,
838 OutputPin_BeginFlush,
839 OutputPin_EndFlush,
840 OutputPin_NewSegment
843 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
845 HRESULT hr;
847 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
849 EnterCriticalSection(This->pin.pCritSec);
851 if (!This->pin.pConnectedTo)
852 hr = VFW_E_NOT_CONNECTED;
853 else
855 IMemAllocator * pAlloc = NULL;
857 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
859 if (SUCCEEDED(hr))
860 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
862 if (SUCCEEDED(hr))
863 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
865 if (pAlloc)
866 IMemAllocator_Release(pAlloc);
869 LeaveCriticalSection(This->pin.pCritSec);
871 return hr;
874 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
876 HRESULT hr = S_OK;
877 IMemInputPin * pMemConnected = NULL;
879 EnterCriticalSection(This->pin.pCritSec);
881 if (!This->pin.pConnectedTo || !This->pMemInputPin)
882 hr = VFW_E_NOT_CONNECTED;
883 else
885 /* we don't have the lock held when using This->pMemInputPin,
886 * so we need to AddRef it to stop it being deleted while we are
887 * using it. */
888 pMemConnected = This->pMemInputPin;
889 IMemInputPin_AddRef(pMemConnected);
892 LeaveCriticalSection(This->pin.pCritSec);
894 if (SUCCEEDED(hr))
896 /* NOTE: if we are in a critical section when Receive is called
897 * then it causes some problems (most notably with the native Video
898 * Renderer) if we are re-entered for whatever reason */
899 hr = IMemInputPin_Receive(pMemConnected, pSample);
900 IMemInputPin_Release(pMemConnected);
903 return hr;
906 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
908 HRESULT hr;
910 EnterCriticalSection(This->pin.pCritSec);
912 if (!This->pin.pConnectedTo)
913 hr = VFW_E_NOT_CONNECTED;
914 else
915 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
917 LeaveCriticalSection(This->pin.pCritSec);
919 return hr;
922 HRESULT OutputPin_CommitAllocator(OutputPin * This)
924 HRESULT hr;
926 TRACE("(%p)->()\n", This);
928 EnterCriticalSection(This->pin.pCritSec);
930 if (!This->pin.pConnectedTo || !This->pMemInputPin)
931 hr = VFW_E_NOT_CONNECTED;
932 else
934 IMemAllocator * pAlloc = NULL;
936 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
938 if (SUCCEEDED(hr))
939 hr = IMemAllocator_Commit(pAlloc);
941 if (pAlloc)
942 IMemAllocator_Release(pAlloc);
945 LeaveCriticalSection(This->pin.pCritSec);
947 return hr;
950 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
952 HRESULT hr;
954 TRACE("(%p)->()\n", This);
956 EnterCriticalSection(This->pin.pCritSec);
958 if (!This->pin.pConnectedTo || !This->pMemInputPin)
959 hr = VFW_E_NOT_CONNECTED;
960 else
962 IMemAllocator * pAlloc = NULL;
964 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
966 if (SUCCEEDED(hr))
967 hr = IMemAllocator_Decommit(pAlloc);
969 if (pAlloc)
970 IMemAllocator_Release(pAlloc);
972 if (SUCCEEDED(hr))
973 hr = IPin_Disconnect(This->pin.pConnectedTo);
976 LeaveCriticalSection(This->pin.pCritSec);
978 return hr;
982 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
984 PullPin * pPinImpl;
986 *ppPin = NULL;
988 if (pPinInfo->dir != PINDIR_INPUT)
990 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
991 return E_INVALIDARG;
994 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
996 if (!pPinImpl)
997 return E_OUTOFMEMORY;
999 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1001 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1003 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1004 return S_OK;
1006 return E_FAIL;
1009 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1011 /* Common attributes */
1012 pPinImpl->pin.refCount = 1;
1013 pPinImpl->pin.pConnectedTo = NULL;
1014 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1015 pPinImpl->pin.pUserData = pUserData;
1016 pPinImpl->pin.pCritSec = pCritSec;
1017 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1019 /* Input pin attributes */
1020 pPinImpl->fnSampleProc = pSampleProc;
1021 pPinImpl->fnPreConnect = NULL;
1022 pPinImpl->pAlloc = NULL;
1023 pPinImpl->pReader = NULL;
1024 pPinImpl->hThread = NULL;
1025 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1027 pPinImpl->rtStart = 0;
1028 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1030 return S_OK;
1033 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1035 PIN_DIRECTION pindirReceive;
1036 HRESULT hr = S_OK;
1037 PullPin *This = (PullPin *)iface;
1039 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1040 dump_AM_MEDIA_TYPE(pmt);
1042 EnterCriticalSection(This->pin.pCritSec);
1044 if (This->pin.pConnectedTo)
1045 hr = VFW_E_ALREADY_CONNECTED;
1047 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1048 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1049 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1051 if (SUCCEEDED(hr))
1053 IPin_QueryDirection(pReceivePin, &pindirReceive);
1055 if (pindirReceive != PINDIR_OUTPUT)
1057 ERR("Can't connect from non-output pin\n");
1058 hr = VFW_E_INVALID_DIRECTION;
1062 if (SUCCEEDED(hr))
1064 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1067 if (SUCCEEDED(hr))
1069 ALLOCATOR_PROPERTIES props;
1070 props.cBuffers = 3;
1071 props.cbBuffer = 64 * 1024; /* 64k bytes */
1072 props.cbAlign = 1;
1073 props.cbPrefix = 0;
1074 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1077 if (SUCCEEDED(hr) && This->fnPreConnect)
1079 hr = This->fnPreConnect(iface, pReceivePin);
1082 if (SUCCEEDED(hr))
1084 CopyMediaType(&This->pin.mtCurrent, pmt);
1085 This->pin.pConnectedTo = pReceivePin;
1086 IPin_AddRef(pReceivePin);
1089 LeaveCriticalSection(This->pin.pCritSec);
1090 return hr;
1093 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1095 PullPin *This = (PullPin *)iface;
1097 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1099 *ppv = NULL;
1101 if (IsEqualIID(riid, &IID_IUnknown))
1102 *ppv = (LPVOID)iface;
1103 else if (IsEqualIID(riid, &IID_IPin))
1104 *ppv = (LPVOID)iface;
1106 if (*ppv)
1108 IUnknown_AddRef((IUnknown *)(*ppv));
1109 return S_OK;
1112 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1114 return E_NOINTERFACE;
1117 ULONG WINAPI PullPin_Release(IPin * iface)
1119 PullPin *This = (PullPin *)iface;
1120 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1122 TRACE("(%p/%p)->()\n", This, iface);
1124 if (!refCount)
1126 if (This->hThread)
1127 PullPin_StopProcessing(This);
1128 IMemAllocator_Release(This->pAlloc);
1129 IAsyncReader_Release(This->pReader);
1130 CloseHandle(This->hEventStateChanged);
1131 CoTaskMemFree(This);
1132 return 0;
1134 return refCount;
1137 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1139 for (;;)
1140 SleepEx(INFINITE, TRUE);
1143 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1145 PullPin *This = (PullPin *)iface;
1146 HRESULT hr;
1148 REFERENCE_TIME rtCurrent;
1149 ALLOCATOR_PROPERTIES allocProps;
1151 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1153 SetEvent(This->hEventStateChanged);
1155 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1157 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1159 TRACE("Start\n");
1161 while (rtCurrent < This->rtStop && hr == S_OK)
1163 /* FIXME: to improve performance by quite a bit this should be changed
1164 * so that one sample is processed while one sample is fetched. However,
1165 * it is harder to debug so for the moment it will stay as it is */
1166 IMediaSample * pSample = NULL;
1167 REFERENCE_TIME rtSampleStop;
1168 DWORD dwUser;
1170 TRACE("Process sample\n");
1172 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1174 if (SUCCEEDED(hr))
1176 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1177 if (rtSampleStop > This->rtStop)
1178 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1179 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1180 rtCurrent = rtSampleStop;
1183 if (SUCCEEDED(hr))
1184 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1186 if (SUCCEEDED(hr))
1187 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1189 if (SUCCEEDED(hr))
1190 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1191 else
1192 ERR("Processing error: %lx\n", hr);
1194 if (pSample)
1195 IMediaSample_Release(pSample);
1198 CoUninitialize();
1200 TRACE("End\n");
1203 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1205 PullPin *This = (PullPin *)iface;
1207 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1209 EnterCriticalSection(This->pin.pCritSec);
1211 HRESULT hr;
1213 CloseHandle(This->hThread);
1214 This->hThread = NULL;
1215 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1216 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1218 LeaveCriticalSection(This->pin.pCritSec);
1220 SetEvent(This->hEventStateChanged);
1222 ExitThread(0);
1225 HRESULT PullPin_InitProcessing(PullPin * This)
1227 HRESULT hr = S_OK;
1229 TRACE("(%p)->()\n", This);
1231 assert(!This->hThread);
1233 /* if we are connected */
1234 if (This->pAlloc)
1236 EnterCriticalSection(This->pin.pCritSec);
1238 DWORD dwThreadId;
1239 assert(!This->hThread);
1241 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1242 if (!This->hThread)
1243 hr = HRESULT_FROM_WIN32(GetLastError());
1245 if (SUCCEEDED(hr))
1246 hr = IMemAllocator_Commit(This->pAlloc);
1248 LeaveCriticalSection(This->pin.pCritSec);
1251 TRACE(" -- %lx\n", hr);
1253 return hr;
1256 HRESULT PullPin_StartProcessing(PullPin * This)
1258 /* if we are connected */
1259 TRACE("(%p)->()\n", This);
1260 if(This->pAlloc)
1262 assert(This->hThread);
1264 ResetEvent(This->hEventStateChanged);
1266 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1267 return HRESULT_FROM_WIN32(GetLastError());
1270 return S_OK;
1273 HRESULT PullPin_PauseProcessing(PullPin * This)
1275 /* make the processing function exit its loop */
1276 This->rtStop = 0;
1278 return S_OK;
1281 HRESULT PullPin_StopProcessing(PullPin * This)
1283 /* if we are connected */
1284 if (This->pAlloc)
1286 assert(This->hThread);
1288 ResetEvent(This->hEventStateChanged);
1290 PullPin_PauseProcessing(This);
1292 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1293 return HRESULT_FROM_WIN32(GetLastError());
1296 return S_OK;
1299 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1301 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1302 return S_FALSE;
1303 return S_OK;
1306 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1308 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1310 PullPin_BeginFlush((IPin *)This);
1311 /* FIXME: need critical section? */
1312 This->rtStart = rtStart;
1313 This->rtStop = rtStop;
1314 PullPin_EndFlush((IPin *)This);
1316 return S_OK;
1319 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1321 FIXME("(%p)->()\n", iface);
1322 return E_NOTIMPL;
1325 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1327 FIXME("(%p)->()\n", iface);
1328 return E_NOTIMPL;
1331 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1333 FIXME("(%p)->()\n", iface);
1334 return E_NOTIMPL;
1337 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1339 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1340 return E_NOTIMPL;
1343 static const IPinVtbl PullPin_Vtbl =
1345 PullPin_QueryInterface,
1346 IPinImpl_AddRef,
1347 PullPin_Release,
1348 OutputPin_Connect,
1349 PullPin_ReceiveConnection,
1350 IPinImpl_Disconnect,
1351 IPinImpl_ConnectedTo,
1352 IPinImpl_ConnectionMediaType,
1353 IPinImpl_QueryPinInfo,
1354 IPinImpl_QueryDirection,
1355 IPinImpl_QueryId,
1356 IPinImpl_QueryAccept,
1357 IPinImpl_EnumMediaTypes,
1358 IPinImpl_QueryInternalConnections,
1359 PullPin_EndOfStream,
1360 PullPin_BeginFlush,
1361 PullPin_EndFlush,
1362 PullPin_NewSegment