Replace the ICOM_THIS_From macros by inline functions.
[wine/multimedia.git] / dlls / quartz / pin.c
blob70e60f0fa20f4cd45fe22b4292dac7f74466ee0d
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 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-1))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
40 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
42 return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
46 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
48 /* Tempting to just do a memcpy, but the name field is
49 128 characters long! We will probably never exceed 10
50 most of the time, so we are better off copying
51 each field manually */
52 strcpyW(pDest->achName, pSrc->achName);
53 pDest->dir = pSrc->dir;
54 pDest->pFilter = pSrc->pFilter;
57 /* Function called as a helper to IPin_Connect */
58 /* specific AM_MEDIA_TYPE - it cannot be NULL */
59 /* NOTE: not part of standard interface */
60 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
62 OutputPin *This = (OutputPin *)iface;
63 HRESULT hr;
64 IMemAllocator * pMemAlloc = NULL;
65 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
67 TRACE("(%p, %p)\n", pReceivePin, pmt);
68 dump_AM_MEDIA_TYPE(pmt);
70 /* FIXME: call queryacceptproc */
72 This->pin.pConnectedTo = pReceivePin;
73 IPin_AddRef(pReceivePin);
74 CopyMediaType(&This->pin.mtCurrent, pmt);
76 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
78 /* get the IMemInputPin interface we will use to deliver samples to the
79 * connected pin */
80 if (SUCCEEDED(hr))
82 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
84 if (SUCCEEDED(hr))
85 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
87 if (hr == VFW_E_NO_ALLOCATOR)
89 /* Input pin provides no allocator, use standard memory allocator */
90 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
92 if (SUCCEEDED(hr))
94 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
98 if (SUCCEEDED(hr))
99 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
101 if (pMemAlloc)
102 IMemAllocator_Release(pMemAlloc);
104 /* break connection if we couldn't get the allocator */
105 if (FAILED(hr))
106 IPin_Disconnect(pReceivePin);
109 if (FAILED(hr))
111 IPin_Release(This->pin.pConnectedTo);
112 This->pin.pConnectedTo = NULL;
113 FreeMediaType(&This->pin.mtCurrent);
116 TRACE(" -- %lx\n", hr);
117 return hr;
120 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
122 InputPin * pPinImpl;
124 *ppPin = NULL;
126 if (pPinInfo->dir != PINDIR_INPUT)
128 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
129 return E_INVALIDARG;
132 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
134 if (!pPinImpl)
135 return E_OUTOFMEMORY;
137 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
139 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
140 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
142 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
143 return S_OK;
145 return E_FAIL;
148 /* Note that we don't init the vtables here (like C++ constructor) */
149 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
151 TRACE("\n");
153 /* Common attributes */
154 pPinImpl->pin.refCount = 1;
155 pPinImpl->pin.pConnectedTo = NULL;
156 pPinImpl->pin.fnQueryAccept = pQueryAccept;
157 pPinImpl->pin.pUserData = pUserData;
158 pPinImpl->pin.pCritSec = pCritSec;
159 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
160 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
162 /* Input pin attributes */
163 pPinImpl->fnSampleProc = pSampleProc;
164 pPinImpl->pAllocator = NULL;
165 pPinImpl->tStart = 0;
166 pPinImpl->tStop = 0;
167 pPinImpl->dRate = 0;
169 return S_OK;
172 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
174 TRACE("\n");
176 /* Common attributes */
177 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
178 pPinImpl->pin.refCount = 1;
179 pPinImpl->pin.pConnectedTo = NULL;
180 pPinImpl->pin.fnQueryAccept = pQueryAccept;
181 pPinImpl->pin.pUserData = pUserData;
182 pPinImpl->pin.pCritSec = pCritSec;
183 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
184 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
186 /* Output pin attributes */
187 pPinImpl->pMemInputPin = NULL;
188 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
189 if (props)
191 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
192 if (pPinImpl->allocProps.cbAlign == 0)
193 pPinImpl->allocProps.cbAlign = 1;
195 else
196 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
198 return S_OK;
201 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
203 OutputPin * pPinImpl;
205 *ppPin = NULL;
207 if (pPinInfo->dir != PINDIR_OUTPUT)
209 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
210 return E_INVALIDARG;
213 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
215 if (!pPinImpl)
216 return E_OUTOFMEMORY;
218 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
220 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
222 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
223 return S_OK;
225 return E_FAIL;
228 /*** Common pin functions ***/
230 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
232 IPinImpl *This = (IPinImpl *)iface;
233 ULONG refCount = InterlockedIncrement(&This->refCount);
235 TRACE("(%p)->() AddRef from %ld\n", iface, refCount - 1);
237 return refCount;
240 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
242 HRESULT hr;
243 IPinImpl *This = (IPinImpl *)iface;
245 TRACE("()\n");
247 EnterCriticalSection(This->pCritSec);
249 if (This->pConnectedTo)
251 IPin_Release(This->pConnectedTo);
252 This->pConnectedTo = NULL;
253 hr = S_OK;
255 else
256 hr = S_FALSE;
258 LeaveCriticalSection(This->pCritSec);
260 return hr;
263 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
265 HRESULT hr;
266 IPinImpl *This = (IPinImpl *)iface;
268 /* TRACE("(%p)\n", ppPin);*/
270 EnterCriticalSection(This->pCritSec);
272 if (This->pConnectedTo)
274 *ppPin = This->pConnectedTo;
275 IPin_AddRef(*ppPin);
276 hr = S_OK;
278 else
279 hr = VFW_E_NOT_CONNECTED;
281 LeaveCriticalSection(This->pCritSec);
283 return hr;
286 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
288 HRESULT hr;
289 IPinImpl *This = (IPinImpl *)iface;
291 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
293 EnterCriticalSection(This->pCritSec);
295 if (This->pConnectedTo)
297 CopyMediaType(pmt, &This->mtCurrent);
298 hr = S_OK;
300 else
302 ZeroMemory(pmt, sizeof(*pmt));
303 hr = VFW_E_NOT_CONNECTED;
306 LeaveCriticalSection(This->pCritSec);
308 return hr;
311 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
313 IPinImpl *This = (IPinImpl *)iface;
315 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
317 Copy_PinInfo(pInfo, &This->pinInfo);
318 IBaseFilter_AddRef(pInfo->pFilter);
320 return S_OK;
323 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
325 IPinImpl *This = (IPinImpl *)iface;
327 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
329 *pPinDir = This->pinInfo.dir;
331 return S_OK;
334 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
336 IPinImpl *This = (IPinImpl *)iface;
338 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
340 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
341 if (!Id)
342 return E_OUTOFMEMORY;
344 strcpyW(*Id, This->pinInfo.achName);
346 return S_OK;
349 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
351 IPinImpl *This = (IPinImpl *)iface;
353 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
355 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
358 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
360 IPinImpl *This = (IPinImpl *)iface;
361 ENUMMEDIADETAILS emd;
363 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
365 /* override this method to allow enumeration of your types */
366 emd.cMediaTypes = 0;
367 emd.pMediaTypes = NULL;
369 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
372 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
374 IPinImpl *This = (IPinImpl *)iface;
376 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
378 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
381 /*** IPin implementation for an input pin ***/
383 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
385 InputPin *This = (InputPin *)iface;
387 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
389 *ppv = NULL;
391 if (IsEqualIID(riid, &IID_IUnknown))
392 *ppv = (LPVOID)iface;
393 else if (IsEqualIID(riid, &IID_IPin))
394 *ppv = (LPVOID)iface;
395 else if (IsEqualIID(riid, &IID_IMemInputPin))
396 *ppv = (LPVOID)&This->lpVtblMemInput;
398 if (*ppv)
400 IUnknown_AddRef((IUnknown *)(*ppv));
401 return S_OK;
404 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
406 return E_NOINTERFACE;
409 ULONG WINAPI InputPin_Release(IPin * iface)
411 InputPin *This = (InputPin *)iface;
412 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
414 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
416 if (!refCount)
418 FreeMediaType(&This->pin.mtCurrent);
419 if (This->pAllocator)
420 IMemAllocator_Release(This->pAllocator);
421 CoTaskMemFree(This);
422 return 0;
424 else
425 return refCount;
428 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
430 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
432 return E_UNEXPECTED;
436 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
438 InputPin *This = (InputPin *)iface;
439 PIN_DIRECTION pindirReceive;
440 HRESULT hr = S_OK;
442 TRACE("(%p, %p)\n", pReceivePin, pmt);
443 dump_AM_MEDIA_TYPE(pmt);
445 EnterCriticalSection(This->pin.pCritSec);
447 if (This->pin.pConnectedTo)
448 hr = VFW_E_ALREADY_CONNECTED;
450 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
451 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
452 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
454 if (SUCCEEDED(hr))
456 IPin_QueryDirection(pReceivePin, &pindirReceive);
458 if (pindirReceive != PINDIR_OUTPUT)
460 ERR("Can't connect from non-output pin\n");
461 hr = VFW_E_INVALID_DIRECTION;
465 if (SUCCEEDED(hr))
467 CopyMediaType(&This->pin.mtCurrent, pmt);
468 This->pin.pConnectedTo = pReceivePin;
469 IPin_AddRef(pReceivePin);
472 LeaveCriticalSection(This->pin.pCritSec);
474 return hr;
477 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
479 TRACE("()\n");
481 return S_OK;
484 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
486 FIXME("()\n");
487 return E_NOTIMPL;
490 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
492 FIXME("()\n");
493 return E_NOTIMPL;
496 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
498 InputPin *This = (InputPin *)iface;
500 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
502 This->tStart = tStart;
503 This->tStop = tStop;
504 This->dRate = dRate;
506 return S_OK;
509 static const IPinVtbl InputPin_Vtbl =
511 InputPin_QueryInterface,
512 IPinImpl_AddRef,
513 InputPin_Release,
514 InputPin_Connect,
515 InputPin_ReceiveConnection,
516 IPinImpl_Disconnect,
517 IPinImpl_ConnectedTo,
518 IPinImpl_ConnectionMediaType,
519 IPinImpl_QueryPinInfo,
520 IPinImpl_QueryDirection,
521 IPinImpl_QueryId,
522 IPinImpl_QueryAccept,
523 IPinImpl_EnumMediaTypes,
524 IPinImpl_QueryInternalConnections,
525 InputPin_EndOfStream,
526 InputPin_BeginFlush,
527 InputPin_EndFlush,
528 InputPin_NewSegment
531 /*** IMemInputPin implementation ***/
533 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
535 InputPin *This = impl_from_IMemInputPin(iface);
537 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
540 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
542 InputPin *This = impl_from_IMemInputPin(iface);
544 return IPin_AddRef((IPin *)&This->pin);
547 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
549 InputPin *This = impl_from_IMemInputPin(iface);
551 return IPin_Release((IPin *)&This->pin);
554 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
556 InputPin *This = impl_from_IMemInputPin(iface);
558 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
560 *ppAllocator = This->pAllocator;
561 if (*ppAllocator)
562 IMemAllocator_AddRef(*ppAllocator);
564 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
567 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
569 InputPin *This = impl_from_IMemInputPin(iface);
571 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
573 if (This->pAllocator)
574 IMemAllocator_Release(This->pAllocator);
575 This->pAllocator = pAllocator;
576 if (This->pAllocator)
577 IMemAllocator_AddRef(This->pAllocator);
579 return S_OK;
582 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
584 InputPin *This = impl_from_IMemInputPin(iface);
586 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
588 /* override this method if you have any specific requirements */
590 return E_NOTIMPL;
593 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
595 InputPin *This = impl_from_IMemInputPin(iface);
597 /* this trace commented out for performance reasons */
598 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
600 return This->fnSampleProc(This->pin.pUserData, pSample);
603 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
605 HRESULT hr = S_OK;
606 InputPin *This = impl_from_IMemInputPin(iface);
608 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
610 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
612 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
613 if (hr != S_OK)
614 break;
617 return hr;
620 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
622 InputPin *This = impl_from_IMemInputPin(iface);
624 FIXME("(%p/%p)->()\n", This, iface);
626 /* FIXME: we should check whether any output pins will block */
628 return S_OK;
631 static const IMemInputPinVtbl MemInputPin_Vtbl =
633 MemInputPin_QueryInterface,
634 MemInputPin_AddRef,
635 MemInputPin_Release,
636 MemInputPin_GetAllocator,
637 MemInputPin_NotifyAllocator,
638 MemInputPin_GetAllocatorRequirements,
639 MemInputPin_Receive,
640 MemInputPin_ReceiveMultiple,
641 MemInputPin_ReceiveCanBlock
644 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
646 OutputPin *This = (OutputPin *)iface;
648 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
650 *ppv = NULL;
652 if (IsEqualIID(riid, &IID_IUnknown))
653 *ppv = (LPVOID)iface;
654 else if (IsEqualIID(riid, &IID_IPin))
655 *ppv = (LPVOID)iface;
657 if (*ppv)
659 IUnknown_AddRef((IUnknown *)(*ppv));
660 return S_OK;
663 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
665 return E_NOINTERFACE;
668 ULONG WINAPI OutputPin_Release(IPin * iface)
670 OutputPin *This = (OutputPin *)iface;
671 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
673 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
675 if (!refCount)
677 FreeMediaType(&This->pin.mtCurrent);
678 CoTaskMemFree(This);
679 return 0;
681 return refCount;
684 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
686 HRESULT hr;
687 OutputPin *This = (OutputPin *)iface;
689 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
690 dump_AM_MEDIA_TYPE(pmt);
692 /* If we try to connect to ourself, we will definitely deadlock.
693 * There are other cases where we could deadlock too, but this
694 * catches the obvious case */
695 assert(pReceivePin != iface);
697 EnterCriticalSection(This->pin.pCritSec);
699 /* if we have been a specific type to connect with, then we can either connect
700 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
701 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
702 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
703 else
705 /* negotiate media type */
707 IEnumMediaTypes * pEnumCandidates;
708 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
710 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
712 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
714 /* try this filter's media types first */
715 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
717 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
718 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
720 hr = S_OK;
721 CoTaskMemFree(pmtCandidate);
722 break;
724 CoTaskMemFree(pmtCandidate);
726 IEnumMediaTypes_Release(pEnumCandidates);
729 /* then try receiver filter's media types */
730 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
732 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
734 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
736 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
737 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
739 hr = S_OK;
740 CoTaskMemFree(pmtCandidate);
741 break;
743 CoTaskMemFree(pmtCandidate);
744 } /* while */
745 IEnumMediaTypes_Release(pEnumCandidates);
746 } /* if not found */
747 } /* if negotiate media type */
748 } /* if succeeded */
749 LeaveCriticalSection(This->pin.pCritSec);
751 TRACE(" -- %lx\n", hr);
752 return hr;
755 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
757 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
759 return E_UNEXPECTED;
762 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
764 HRESULT hr;
765 OutputPin *This = (OutputPin *)iface;
767 TRACE("()\n");
769 EnterCriticalSection(This->pin.pCritSec);
771 if (This->pMemInputPin)
773 IMemInputPin_Release(This->pMemInputPin);
774 This->pMemInputPin = NULL;
776 if (This->pin.pConnectedTo)
778 IPin_Release(This->pin.pConnectedTo);
779 This->pin.pConnectedTo = NULL;
780 hr = S_OK;
782 else
783 hr = S_FALSE;
785 LeaveCriticalSection(This->pin.pCritSec);
787 return hr;
790 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
792 TRACE("()\n");
794 /* not supposed to do anything in an output pin */
796 return E_UNEXPECTED;
799 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
801 TRACE("(%p)->()\n", iface);
803 /* not supposed to do anything in an output pin */
805 return E_UNEXPECTED;
808 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
810 TRACE("(%p)->()\n", iface);
812 /* not supposed to do anything in an output pin */
814 return E_UNEXPECTED;
817 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
819 TRACE("(%p)->(%lx%08lx, %lx%08lx, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
821 /* not supposed to do anything in an output pin */
823 return E_UNEXPECTED;
826 static const IPinVtbl OutputPin_Vtbl =
828 OutputPin_QueryInterface,
829 IPinImpl_AddRef,
830 OutputPin_Release,
831 OutputPin_Connect,
832 OutputPin_ReceiveConnection,
833 OutputPin_Disconnect,
834 IPinImpl_ConnectedTo,
835 IPinImpl_ConnectionMediaType,
836 IPinImpl_QueryPinInfo,
837 IPinImpl_QueryDirection,
838 IPinImpl_QueryId,
839 IPinImpl_QueryAccept,
840 IPinImpl_EnumMediaTypes,
841 IPinImpl_QueryInternalConnections,
842 OutputPin_EndOfStream,
843 OutputPin_BeginFlush,
844 OutputPin_EndFlush,
845 OutputPin_NewSegment
848 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
850 HRESULT hr;
852 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
854 EnterCriticalSection(This->pin.pCritSec);
856 if (!This->pin.pConnectedTo)
857 hr = VFW_E_NOT_CONNECTED;
858 else
860 IMemAllocator * pAlloc = NULL;
862 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
864 if (SUCCEEDED(hr))
865 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
867 if (SUCCEEDED(hr))
868 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
870 if (pAlloc)
871 IMemAllocator_Release(pAlloc);
874 LeaveCriticalSection(This->pin.pCritSec);
876 return hr;
879 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
881 HRESULT hr = S_OK;
882 IMemInputPin * pMemConnected = NULL;
884 EnterCriticalSection(This->pin.pCritSec);
886 if (!This->pin.pConnectedTo || !This->pMemInputPin)
887 hr = VFW_E_NOT_CONNECTED;
888 else
890 /* we don't have the lock held when using This->pMemInputPin,
891 * so we need to AddRef it to stop it being deleted while we are
892 * using it. */
893 pMemConnected = This->pMemInputPin;
894 IMemInputPin_AddRef(pMemConnected);
897 LeaveCriticalSection(This->pin.pCritSec);
899 if (SUCCEEDED(hr))
901 /* NOTE: if we are in a critical section when Receive is called
902 * then it causes some problems (most notably with the native Video
903 * Renderer) if we are re-entered for whatever reason */
904 hr = IMemInputPin_Receive(pMemConnected, pSample);
905 IMemInputPin_Release(pMemConnected);
908 return hr;
911 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
913 HRESULT hr;
915 EnterCriticalSection(This->pin.pCritSec);
917 if (!This->pin.pConnectedTo)
918 hr = VFW_E_NOT_CONNECTED;
919 else
920 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
922 LeaveCriticalSection(This->pin.pCritSec);
924 return hr;
927 HRESULT OutputPin_CommitAllocator(OutputPin * This)
929 HRESULT hr;
931 TRACE("(%p)->()\n", This);
933 EnterCriticalSection(This->pin.pCritSec);
935 if (!This->pin.pConnectedTo || !This->pMemInputPin)
936 hr = VFW_E_NOT_CONNECTED;
937 else
939 IMemAllocator * pAlloc = NULL;
941 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
943 if (SUCCEEDED(hr))
944 hr = IMemAllocator_Commit(pAlloc);
946 if (pAlloc)
947 IMemAllocator_Release(pAlloc);
950 LeaveCriticalSection(This->pin.pCritSec);
952 return hr;
955 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
957 HRESULT hr;
959 TRACE("(%p)->()\n", This);
961 EnterCriticalSection(This->pin.pCritSec);
963 if (!This->pin.pConnectedTo || !This->pMemInputPin)
964 hr = VFW_E_NOT_CONNECTED;
965 else
967 IMemAllocator * pAlloc = NULL;
969 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
971 if (SUCCEEDED(hr))
972 hr = IMemAllocator_Decommit(pAlloc);
974 if (pAlloc)
975 IMemAllocator_Release(pAlloc);
977 if (SUCCEEDED(hr))
978 hr = IPin_Disconnect(This->pin.pConnectedTo);
981 LeaveCriticalSection(This->pin.pCritSec);
983 return hr;
987 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
989 PullPin * pPinImpl;
991 *ppPin = NULL;
993 if (pPinInfo->dir != PINDIR_INPUT)
995 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
996 return E_INVALIDARG;
999 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1001 if (!pPinImpl)
1002 return E_OUTOFMEMORY;
1004 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1006 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1008 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1009 return S_OK;
1011 return E_FAIL;
1014 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1016 /* Common attributes */
1017 pPinImpl->pin.refCount = 1;
1018 pPinImpl->pin.pConnectedTo = NULL;
1019 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1020 pPinImpl->pin.pUserData = pUserData;
1021 pPinImpl->pin.pCritSec = pCritSec;
1022 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1023 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1025 /* Input pin attributes */
1026 pPinImpl->fnSampleProc = pSampleProc;
1027 pPinImpl->fnPreConnect = NULL;
1028 pPinImpl->pAlloc = NULL;
1029 pPinImpl->pReader = NULL;
1030 pPinImpl->hThread = NULL;
1031 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1033 pPinImpl->rtStart = 0;
1034 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1036 return S_OK;
1039 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1041 PIN_DIRECTION pindirReceive;
1042 HRESULT hr = S_OK;
1043 PullPin *This = (PullPin *)iface;
1045 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1046 dump_AM_MEDIA_TYPE(pmt);
1048 EnterCriticalSection(This->pin.pCritSec);
1050 if (This->pin.pConnectedTo)
1051 hr = VFW_E_ALREADY_CONNECTED;
1053 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1054 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1055 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1057 if (SUCCEEDED(hr))
1059 IPin_QueryDirection(pReceivePin, &pindirReceive);
1061 if (pindirReceive != PINDIR_OUTPUT)
1063 ERR("Can't connect from non-output pin\n");
1064 hr = VFW_E_INVALID_DIRECTION;
1068 if (SUCCEEDED(hr))
1070 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1073 if (SUCCEEDED(hr))
1075 ALLOCATOR_PROPERTIES props;
1076 props.cBuffers = 3;
1077 props.cbBuffer = 64 * 1024; /* 64k bytes */
1078 props.cbAlign = 1;
1079 props.cbPrefix = 0;
1080 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1083 if (SUCCEEDED(hr) && This->fnPreConnect)
1085 hr = This->fnPreConnect(iface, pReceivePin);
1088 if (SUCCEEDED(hr))
1090 CopyMediaType(&This->pin.mtCurrent, pmt);
1091 This->pin.pConnectedTo = pReceivePin;
1092 IPin_AddRef(pReceivePin);
1095 LeaveCriticalSection(This->pin.pCritSec);
1096 return hr;
1099 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1101 PullPin *This = (PullPin *)iface;
1103 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1105 *ppv = NULL;
1107 if (IsEqualIID(riid, &IID_IUnknown))
1108 *ppv = (LPVOID)iface;
1109 else if (IsEqualIID(riid, &IID_IPin))
1110 *ppv = (LPVOID)iface;
1112 if (*ppv)
1114 IUnknown_AddRef((IUnknown *)(*ppv));
1115 return S_OK;
1118 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1120 return E_NOINTERFACE;
1123 ULONG WINAPI PullPin_Release(IPin * iface)
1125 PullPin *This = (PullPin *)iface;
1126 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1128 TRACE("(%p/%p)->()\n", This, iface);
1130 if (!refCount)
1132 if (This->hThread)
1133 PullPin_StopProcessing(This);
1134 IMemAllocator_Release(This->pAlloc);
1135 IAsyncReader_Release(This->pReader);
1136 CloseHandle(This->hEventStateChanged);
1137 CoTaskMemFree(This);
1138 return 0;
1140 return refCount;
1143 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1145 for (;;)
1146 SleepEx(INFINITE, TRUE);
1149 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1151 PullPin *This = (PullPin *)iface;
1152 HRESULT hr;
1154 REFERENCE_TIME rtCurrent;
1155 ALLOCATOR_PROPERTIES allocProps;
1157 CoInitializeEx(NULL, COINIT_MULTITHREADED);
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 TRACE("Start\n");
1167 while (rtCurrent < This->rtStop && hr == S_OK)
1169 /* FIXME: to improve performance by quite a bit this should be changed
1170 * so that one sample is processed while one sample is fetched. However,
1171 * it is harder to debug so for the moment it will stay as it is */
1172 IMediaSample * pSample = NULL;
1173 REFERENCE_TIME rtSampleStop;
1174 DWORD dwUser;
1176 TRACE("Process sample\n");
1178 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1180 if (SUCCEEDED(hr))
1182 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1183 if (rtSampleStop > This->rtStop)
1184 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1185 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1186 rtCurrent = rtSampleStop;
1189 if (SUCCEEDED(hr))
1190 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1192 if (SUCCEEDED(hr))
1193 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1195 if (SUCCEEDED(hr))
1196 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1197 else
1198 ERR("Processing error: %lx\n", hr);
1200 if (pSample)
1201 IMediaSample_Release(pSample);
1204 CoUninitialize();
1206 TRACE("End\n");
1209 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1211 PullPin *This = (PullPin *)iface;
1213 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1215 EnterCriticalSection(This->pin.pCritSec);
1217 HRESULT hr;
1219 CloseHandle(This->hThread);
1220 This->hThread = NULL;
1221 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1222 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1224 LeaveCriticalSection(This->pin.pCritSec);
1226 SetEvent(This->hEventStateChanged);
1228 ExitThread(0);
1231 HRESULT PullPin_InitProcessing(PullPin * This)
1233 HRESULT hr = S_OK;
1235 TRACE("(%p)->()\n", This);
1237 assert(!This->hThread);
1239 /* if we are connected */
1240 if (This->pAlloc)
1242 EnterCriticalSection(This->pin.pCritSec);
1244 DWORD dwThreadId;
1245 assert(!This->hThread);
1247 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1248 if (!This->hThread)
1249 hr = HRESULT_FROM_WIN32(GetLastError());
1251 if (SUCCEEDED(hr))
1252 hr = IMemAllocator_Commit(This->pAlloc);
1254 LeaveCriticalSection(This->pin.pCritSec);
1257 TRACE(" -- %lx\n", hr);
1259 return hr;
1262 HRESULT PullPin_StartProcessing(PullPin * This)
1264 /* if we are connected */
1265 TRACE("(%p)->()\n", This);
1266 if(This->pAlloc)
1268 assert(This->hThread);
1270 ResetEvent(This->hEventStateChanged);
1272 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1273 return HRESULT_FROM_WIN32(GetLastError());
1276 return S_OK;
1279 HRESULT PullPin_PauseProcessing(PullPin * This)
1281 /* make the processing function exit its loop */
1282 This->rtStop = 0;
1284 return S_OK;
1287 HRESULT PullPin_StopProcessing(PullPin * This)
1289 /* if we are connected */
1290 if (This->pAlloc)
1292 assert(This->hThread);
1294 ResetEvent(This->hEventStateChanged);
1296 PullPin_PauseProcessing(This);
1298 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1299 return HRESULT_FROM_WIN32(GetLastError());
1302 return S_OK;
1305 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1307 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1308 return S_FALSE;
1309 return S_OK;
1312 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1314 FIXME("(%p)->(%lx%08lx, %lx%08lx)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1316 PullPin_BeginFlush((IPin *)This);
1317 /* FIXME: need critical section? */
1318 This->rtStart = rtStart;
1319 This->rtStop = rtStop;
1320 PullPin_EndFlush((IPin *)This);
1322 return S_OK;
1325 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1327 FIXME("(%p)->()\n", iface);
1328 return E_NOTIMPL;
1331 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1333 FIXME("(%p)->()\n", iface);
1334 return E_NOTIMPL;
1337 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1339 FIXME("(%p)->()\n", iface);
1340 return E_NOTIMPL;
1343 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1345 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1346 return E_NOTIMPL;
1349 static const IPinVtbl PullPin_Vtbl =
1351 PullPin_QueryInterface,
1352 IPinImpl_AddRef,
1353 PullPin_Release,
1354 OutputPin_Connect,
1355 PullPin_ReceiveConnection,
1356 IPinImpl_Disconnect,
1357 IPinImpl_ConnectedTo,
1358 IPinImpl_ConnectionMediaType,
1359 IPinImpl_QueryPinInfo,
1360 IPinImpl_QueryDirection,
1361 IPinImpl_QueryId,
1362 IPinImpl_QueryAccept,
1363 IPinImpl_EnumMediaTypes,
1364 IPinImpl_QueryInternalConnections,
1365 PullPin_EndOfStream,
1366 PullPin_BeginFlush,
1367 PullPin_EndFlush,
1368 PullPin_NewSegment