push 8794ef13811f1799d6928b27ab1cf994e8ac55b4
[wine/hacks.git] / dlls / quartz / pin.c
blob58e5beeec89e2f2281b942ef75c559d4a1077a29
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 This->pMemInputPin = NULL;
83 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
85 if (SUCCEEDED(hr))
87 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
89 if (hr == VFW_E_NO_ALLOCATOR)
91 /* Input pin provides no allocator, use standard memory allocator */
92 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
94 if (SUCCEEDED(hr))
96 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
100 if (SUCCEEDED(hr))
101 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
103 if (pMemAlloc)
104 IMemAllocator_Release(pMemAlloc);
107 /* break connection if we couldn't get the allocator */
108 if (FAILED(hr))
110 if (This->pMemInputPin)
111 IMemInputPin_Release(This->pMemInputPin);
112 This->pMemInputPin = NULL;
114 IPin_Disconnect(pReceivePin);
118 if (FAILED(hr))
120 IPin_Release(This->pin.pConnectedTo);
121 This->pin.pConnectedTo = NULL;
122 FreeMediaType(&This->pin.mtCurrent);
125 TRACE(" -- %x\n", hr);
126 return hr;
129 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
131 InputPin * pPinImpl;
133 *ppPin = NULL;
135 if (pPinInfo->dir != PINDIR_INPUT)
137 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
138 return E_INVALIDARG;
141 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
143 if (!pPinImpl)
144 return E_OUTOFMEMORY;
146 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
148 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
149 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
151 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
152 return S_OK;
154 return E_FAIL;
157 /* Note that we don't init the vtables here (like C++ constructor) */
158 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
160 TRACE("\n");
162 /* Common attributes */
163 pPinImpl->pin.refCount = 1;
164 pPinImpl->pin.pConnectedTo = NULL;
165 pPinImpl->pin.fnQueryAccept = pQueryAccept;
166 pPinImpl->pin.pUserData = pUserData;
167 pPinImpl->pin.pCritSec = pCritSec;
168 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
169 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
171 /* Input pin attributes */
172 pPinImpl->fnSampleProc = pSampleProc;
173 pPinImpl->pAllocator = NULL;
174 pPinImpl->tStart = 0;
175 pPinImpl->tStop = 0;
176 pPinImpl->dRate = 0;
178 return S_OK;
181 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
183 TRACE("\n");
185 /* Common attributes */
186 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
187 pPinImpl->pin.refCount = 1;
188 pPinImpl->pin.pConnectedTo = NULL;
189 pPinImpl->pin.fnQueryAccept = pQueryAccept;
190 pPinImpl->pin.pUserData = pUserData;
191 pPinImpl->pin.pCritSec = pCritSec;
192 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
193 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
195 /* Output pin attributes */
196 pPinImpl->pMemInputPin = NULL;
197 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
198 if (props)
200 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
201 if (pPinImpl->allocProps.cbAlign == 0)
202 pPinImpl->allocProps.cbAlign = 1;
204 else
205 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
207 return S_OK;
210 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
212 OutputPin * pPinImpl;
214 *ppPin = NULL;
216 if (pPinInfo->dir != PINDIR_OUTPUT)
218 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
219 return E_INVALIDARG;
222 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
224 if (!pPinImpl)
225 return E_OUTOFMEMORY;
227 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
229 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
231 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
232 return S_OK;
234 return E_FAIL;
237 /*** Common pin functions ***/
239 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
241 IPinImpl *This = (IPinImpl *)iface;
242 ULONG refCount = InterlockedIncrement(&This->refCount);
244 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
246 return refCount;
249 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
251 HRESULT hr;
252 IPinImpl *This = (IPinImpl *)iface;
254 TRACE("()\n");
256 EnterCriticalSection(This->pCritSec);
258 if (This->pConnectedTo)
260 IPin_Release(This->pConnectedTo);
261 This->pConnectedTo = NULL;
262 hr = S_OK;
264 else
265 hr = S_FALSE;
267 LeaveCriticalSection(This->pCritSec);
269 return hr;
272 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
274 HRESULT hr;
275 IPinImpl *This = (IPinImpl *)iface;
277 /* TRACE("(%p)\n", ppPin);*/
279 EnterCriticalSection(This->pCritSec);
281 if (This->pConnectedTo)
283 *ppPin = This->pConnectedTo;
284 IPin_AddRef(*ppPin);
285 hr = S_OK;
287 else
288 hr = VFW_E_NOT_CONNECTED;
290 LeaveCriticalSection(This->pCritSec);
292 return hr;
295 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
297 HRESULT hr;
298 IPinImpl *This = (IPinImpl *)iface;
300 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
302 EnterCriticalSection(This->pCritSec);
304 if (This->pConnectedTo)
306 CopyMediaType(pmt, &This->mtCurrent);
307 hr = S_OK;
309 else
311 ZeroMemory(pmt, sizeof(*pmt));
312 hr = VFW_E_NOT_CONNECTED;
315 LeaveCriticalSection(This->pCritSec);
317 return hr;
320 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
322 IPinImpl *This = (IPinImpl *)iface;
324 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
326 Copy_PinInfo(pInfo, &This->pinInfo);
327 IBaseFilter_AddRef(pInfo->pFilter);
329 return S_OK;
332 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
334 IPinImpl *This = (IPinImpl *)iface;
336 TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
338 *pPinDir = This->pinInfo.dir;
340 return S_OK;
343 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
345 IPinImpl *This = (IPinImpl *)iface;
347 TRACE("(%p/%p)->(%p)\n", This, iface, Id);
349 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
350 if (!*Id)
351 return E_OUTOFMEMORY;
353 strcpyW(*Id, This->pinInfo.achName);
355 return S_OK;
358 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
360 IPinImpl *This = (IPinImpl *)iface;
362 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
364 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
367 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
369 IPinImpl *This = (IPinImpl *)iface;
370 ENUMMEDIADETAILS emd;
372 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
374 /* override this method to allow enumeration of your types */
375 emd.cMediaTypes = 0;
376 emd.pMediaTypes = NULL;
378 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
381 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
383 IPinImpl *This = (IPinImpl *)iface;
385 TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
387 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
390 /*** IPin implementation for an input pin ***/
392 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
394 InputPin *This = (InputPin *)iface;
396 TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
398 *ppv = NULL;
400 if (IsEqualIID(riid, &IID_IUnknown))
401 *ppv = (LPVOID)iface;
402 else if (IsEqualIID(riid, &IID_IPin))
403 *ppv = (LPVOID)iface;
404 else if (IsEqualIID(riid, &IID_IMemInputPin))
405 *ppv = (LPVOID)&This->lpVtblMemInput;
407 if (*ppv)
409 IUnknown_AddRef((IUnknown *)(*ppv));
410 return S_OK;
413 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
415 return E_NOINTERFACE;
418 ULONG WINAPI InputPin_Release(IPin * iface)
420 InputPin *This = (InputPin *)iface;
421 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
423 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
425 if (!refCount)
427 FreeMediaType(&This->pin.mtCurrent);
428 if (This->pAllocator)
429 IMemAllocator_Release(This->pAllocator);
430 CoTaskMemFree(This);
431 return 0;
433 else
434 return refCount;
437 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
439 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
441 return E_UNEXPECTED;
445 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
447 InputPin *This = (InputPin *)iface;
448 PIN_DIRECTION pindirReceive;
449 HRESULT hr = S_OK;
451 TRACE("(%p, %p)\n", pReceivePin, pmt);
452 dump_AM_MEDIA_TYPE(pmt);
454 EnterCriticalSection(This->pin.pCritSec);
456 if (This->pin.pConnectedTo)
457 hr = VFW_E_ALREADY_CONNECTED;
459 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
460 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
461 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
463 if (SUCCEEDED(hr))
465 IPin_QueryDirection(pReceivePin, &pindirReceive);
467 if (pindirReceive != PINDIR_OUTPUT)
469 ERR("Can't connect from non-output pin\n");
470 hr = VFW_E_INVALID_DIRECTION;
474 if (SUCCEEDED(hr))
476 CopyMediaType(&This->pin.mtCurrent, pmt);
477 This->pin.pConnectedTo = pReceivePin;
478 IPin_AddRef(pReceivePin);
481 LeaveCriticalSection(This->pin.pCritSec);
483 return hr;
486 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
488 TRACE("()\n");
490 return S_OK;
493 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
495 FIXME("()\n");
496 return E_NOTIMPL;
499 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
501 FIXME("()\n");
502 return E_NOTIMPL;
505 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
507 InputPin *This = (InputPin *)iface;
509 TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
511 This->tStart = tStart;
512 This->tStop = tStop;
513 This->dRate = dRate;
515 return S_OK;
518 static const IPinVtbl InputPin_Vtbl =
520 InputPin_QueryInterface,
521 IPinImpl_AddRef,
522 InputPin_Release,
523 InputPin_Connect,
524 InputPin_ReceiveConnection,
525 IPinImpl_Disconnect,
526 IPinImpl_ConnectedTo,
527 IPinImpl_ConnectionMediaType,
528 IPinImpl_QueryPinInfo,
529 IPinImpl_QueryDirection,
530 IPinImpl_QueryId,
531 IPinImpl_QueryAccept,
532 IPinImpl_EnumMediaTypes,
533 IPinImpl_QueryInternalConnections,
534 InputPin_EndOfStream,
535 InputPin_BeginFlush,
536 InputPin_EndFlush,
537 InputPin_NewSegment
540 /*** IMemInputPin implementation ***/
542 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
544 InputPin *This = impl_from_IMemInputPin(iface);
546 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
549 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
551 InputPin *This = impl_from_IMemInputPin(iface);
553 return IPin_AddRef((IPin *)&This->pin);
556 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
558 InputPin *This = impl_from_IMemInputPin(iface);
560 return IPin_Release((IPin *)&This->pin);
563 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
565 InputPin *This = impl_from_IMemInputPin(iface);
567 TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
569 *ppAllocator = This->pAllocator;
570 if (*ppAllocator)
571 IMemAllocator_AddRef(*ppAllocator);
573 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
576 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
578 InputPin *This = impl_from_IMemInputPin(iface);
580 TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
582 if (This->pAllocator)
583 IMemAllocator_Release(This->pAllocator);
584 This->pAllocator = pAllocator;
585 if (This->pAllocator)
586 IMemAllocator_AddRef(This->pAllocator);
588 return S_OK;
591 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
593 InputPin *This = impl_from_IMemInputPin(iface);
595 TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
597 /* override this method if you have any specific requirements */
599 return E_NOTIMPL;
602 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
604 InputPin *This = impl_from_IMemInputPin(iface);
606 /* this trace commented out for performance reasons */
607 /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
609 return This->fnSampleProc(This->pin.pUserData, pSample);
612 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
614 HRESULT hr = S_OK;
615 InputPin *This = impl_from_IMemInputPin(iface);
617 TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
619 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
621 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
622 if (hr != S_OK)
623 break;
626 return hr;
629 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
631 InputPin *This = impl_from_IMemInputPin(iface);
633 FIXME("(%p/%p)->()\n", This, iface);
635 /* FIXME: we should check whether any output pins will block */
637 return S_OK;
640 static const IMemInputPinVtbl MemInputPin_Vtbl =
642 MemInputPin_QueryInterface,
643 MemInputPin_AddRef,
644 MemInputPin_Release,
645 MemInputPin_GetAllocator,
646 MemInputPin_NotifyAllocator,
647 MemInputPin_GetAllocatorRequirements,
648 MemInputPin_Receive,
649 MemInputPin_ReceiveMultiple,
650 MemInputPin_ReceiveCanBlock
653 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
655 OutputPin *This = (OutputPin *)iface;
657 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
659 *ppv = NULL;
661 if (IsEqualIID(riid, &IID_IUnknown))
662 *ppv = (LPVOID)iface;
663 else if (IsEqualIID(riid, &IID_IPin))
664 *ppv = (LPVOID)iface;
666 if (*ppv)
668 IUnknown_AddRef((IUnknown *)(*ppv));
669 return S_OK;
672 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
674 return E_NOINTERFACE;
677 ULONG WINAPI OutputPin_Release(IPin * iface)
679 OutputPin *This = (OutputPin *)iface;
680 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
682 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
684 if (!refCount)
686 FreeMediaType(&This->pin.mtCurrent);
687 CoTaskMemFree(This);
688 return 0;
690 return refCount;
693 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
695 HRESULT hr;
696 OutputPin *This = (OutputPin *)iface;
698 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
699 dump_AM_MEDIA_TYPE(pmt);
701 /* If we try to connect to ourself, we will definitely deadlock.
702 * There are other cases where we could deadlock too, but this
703 * catches the obvious case */
704 assert(pReceivePin != iface);
706 EnterCriticalSection(This->pin.pCritSec);
708 /* if we have been a specific type to connect with, then we can either connect
709 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
710 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
711 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
712 else
714 /* negotiate media type */
716 IEnumMediaTypes * pEnumCandidates;
717 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
719 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
721 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
723 /* try this filter's media types first */
724 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
726 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
727 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
729 hr = S_OK;
730 CoTaskMemFree(pmtCandidate);
731 break;
733 CoTaskMemFree(pmtCandidate);
735 IEnumMediaTypes_Release(pEnumCandidates);
738 /* then try receiver filter's media types */
739 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
741 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
743 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
745 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
746 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
748 hr = S_OK;
749 CoTaskMemFree(pmtCandidate);
750 break;
752 CoTaskMemFree(pmtCandidate);
753 } /* while */
754 IEnumMediaTypes_Release(pEnumCandidates);
755 } /* if not found */
756 } /* if negotiate media type */
757 } /* if succeeded */
758 LeaveCriticalSection(This->pin.pCritSec);
760 TRACE(" -- %x\n", hr);
761 return hr;
764 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
766 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
768 return E_UNEXPECTED;
771 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
773 HRESULT hr;
774 OutputPin *This = (OutputPin *)iface;
776 TRACE("()\n");
778 EnterCriticalSection(This->pin.pCritSec);
780 if (This->pMemInputPin)
782 IMemInputPin_Release(This->pMemInputPin);
783 This->pMemInputPin = NULL;
785 if (This->pin.pConnectedTo)
787 IPin_Release(This->pin.pConnectedTo);
788 This->pin.pConnectedTo = NULL;
789 hr = S_OK;
791 else
792 hr = S_FALSE;
794 LeaveCriticalSection(This->pin.pCritSec);
796 return hr;
799 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
801 TRACE("()\n");
803 /* not supposed to do anything in an output pin */
805 return E_UNEXPECTED;
808 HRESULT WINAPI OutputPin_BeginFlush(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_EndFlush(IPin * iface)
819 TRACE("(%p)->()\n", iface);
821 /* not supposed to do anything in an output pin */
823 return E_UNEXPECTED;
826 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
828 TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
830 /* not supposed to do anything in an output pin */
832 return E_UNEXPECTED;
835 static const IPinVtbl OutputPin_Vtbl =
837 OutputPin_QueryInterface,
838 IPinImpl_AddRef,
839 OutputPin_Release,
840 OutputPin_Connect,
841 OutputPin_ReceiveConnection,
842 OutputPin_Disconnect,
843 IPinImpl_ConnectedTo,
844 IPinImpl_ConnectionMediaType,
845 IPinImpl_QueryPinInfo,
846 IPinImpl_QueryDirection,
847 IPinImpl_QueryId,
848 IPinImpl_QueryAccept,
849 IPinImpl_EnumMediaTypes,
850 IPinImpl_QueryInternalConnections,
851 OutputPin_EndOfStream,
852 OutputPin_BeginFlush,
853 OutputPin_EndFlush,
854 OutputPin_NewSegment
857 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
859 HRESULT hr;
861 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
863 EnterCriticalSection(This->pin.pCritSec);
865 if (!This->pin.pConnectedTo)
866 hr = VFW_E_NOT_CONNECTED;
867 else
869 IMemAllocator * pAlloc = NULL;
871 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
873 if (SUCCEEDED(hr))
874 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
876 if (SUCCEEDED(hr))
877 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
879 if (pAlloc)
880 IMemAllocator_Release(pAlloc);
883 LeaveCriticalSection(This->pin.pCritSec);
885 return hr;
888 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
890 HRESULT hr = S_OK;
891 IMemInputPin * pMemConnected = NULL;
892 PIN_INFO pinInfo;
894 EnterCriticalSection(This->pin.pCritSec);
896 if (!This->pin.pConnectedTo || !This->pMemInputPin)
897 hr = VFW_E_NOT_CONNECTED;
898 else
900 /* we don't have the lock held when using This->pMemInputPin,
901 * so we need to AddRef it to stop it being deleted while we are
902 * using it. Same with its filter. */
903 pMemConnected = This->pMemInputPin;
904 IMemInputPin_AddRef(pMemConnected);
905 hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
908 LeaveCriticalSection(This->pin.pCritSec);
910 if (SUCCEEDED(hr))
912 /* NOTE: if we are in a critical section when Receive is called
913 * then it causes some problems (most notably with the native Video
914 * Renderer) if we are re-entered for whatever reason */
915 hr = IMemInputPin_Receive(pMemConnected, pSample);
916 IBaseFilter_Release(pinInfo.pFilter);
918 if (pMemConnected)
919 IMemInputPin_Release(pMemConnected);
921 return hr;
924 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
926 HRESULT hr;
928 EnterCriticalSection(This->pin.pCritSec);
930 if (!This->pin.pConnectedTo)
931 hr = VFW_E_NOT_CONNECTED;
932 else
933 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
935 LeaveCriticalSection(This->pin.pCritSec);
937 return hr;
940 HRESULT OutputPin_CommitAllocator(OutputPin * This)
942 HRESULT hr;
944 TRACE("(%p)->()\n", This);
946 EnterCriticalSection(This->pin.pCritSec);
948 if (!This->pin.pConnectedTo || !This->pMemInputPin)
949 hr = VFW_E_NOT_CONNECTED;
950 else
952 IMemAllocator * pAlloc = NULL;
954 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
956 if (SUCCEEDED(hr))
957 hr = IMemAllocator_Commit(pAlloc);
959 if (pAlloc)
960 IMemAllocator_Release(pAlloc);
963 LeaveCriticalSection(This->pin.pCritSec);
965 return hr;
968 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
970 HRESULT hr;
972 TRACE("(%p)->()\n", This);
974 EnterCriticalSection(This->pin.pCritSec);
976 if (!This->pin.pConnectedTo || !This->pMemInputPin)
977 hr = VFW_E_NOT_CONNECTED;
978 else
980 IMemAllocator * pAlloc = NULL;
982 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
984 if (SUCCEEDED(hr))
985 hr = IMemAllocator_Decommit(pAlloc);
987 if (pAlloc)
988 IMemAllocator_Release(pAlloc);
990 if (SUCCEEDED(hr))
991 hr = IPin_Disconnect(This->pin.pConnectedTo);
994 LeaveCriticalSection(This->pin.pCritSec);
996 return hr;
1000 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1002 PullPin * pPinImpl;
1004 *ppPin = NULL;
1006 if (pPinInfo->dir != PINDIR_INPUT)
1008 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1009 return E_INVALIDARG;
1012 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1014 if (!pPinImpl)
1015 return E_OUTOFMEMORY;
1017 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1019 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1021 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1022 return S_OK;
1024 return E_FAIL;
1027 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1029 /* Common attributes */
1030 pPinImpl->pin.refCount = 1;
1031 pPinImpl->pin.pConnectedTo = NULL;
1032 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1033 pPinImpl->pin.pUserData = pUserData;
1034 pPinImpl->pin.pCritSec = pCritSec;
1035 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1036 ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1038 /* Input pin attributes */
1039 pPinImpl->fnSampleProc = pSampleProc;
1040 pPinImpl->fnPreConnect = NULL;
1041 pPinImpl->pAlloc = NULL;
1042 pPinImpl->pReader = NULL;
1043 pPinImpl->hThread = NULL;
1044 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1046 pPinImpl->rtStart = 0;
1047 pPinImpl->rtCurrent = 0;
1048 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1050 return S_OK;
1053 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1055 PIN_DIRECTION pindirReceive;
1056 HRESULT hr = S_OK;
1057 PullPin *This = (PullPin *)iface;
1059 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1060 dump_AM_MEDIA_TYPE(pmt);
1062 EnterCriticalSection(This->pin.pCritSec);
1064 if (This->pin.pConnectedTo)
1065 hr = VFW_E_ALREADY_CONNECTED;
1067 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1068 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1069 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1071 if (SUCCEEDED(hr))
1073 IPin_QueryDirection(pReceivePin, &pindirReceive);
1075 if (pindirReceive != PINDIR_OUTPUT)
1077 ERR("Can't connect from non-output pin\n");
1078 hr = VFW_E_INVALID_DIRECTION;
1082 This->pReader = NULL;
1083 This->pAlloc = NULL;
1084 if (SUCCEEDED(hr))
1086 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1089 if (SUCCEEDED(hr))
1091 ALLOCATOR_PROPERTIES props;
1092 props.cBuffers = 3;
1093 props.cbBuffer = 64 * 1024; /* 64k bytes */
1094 props.cbAlign = 1;
1095 props.cbPrefix = 0;
1096 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1099 if (SUCCEEDED(hr) && This->fnPreConnect)
1101 hr = This->fnPreConnect(iface, pReceivePin);
1104 if (SUCCEEDED(hr))
1106 CopyMediaType(&This->pin.mtCurrent, pmt);
1107 This->pin.pConnectedTo = pReceivePin;
1108 IPin_AddRef(pReceivePin);
1110 else
1112 if (This->pReader)
1113 IAsyncReader_Release(This->pReader);
1114 This->pReader = NULL;
1115 if (This->pAlloc)
1116 IMemAllocator_Release(This->pAlloc);
1117 This->pAlloc = NULL;
1120 LeaveCriticalSection(This->pin.pCritSec);
1121 return hr;
1124 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1126 PullPin *This = (PullPin *)iface;
1128 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1130 *ppv = NULL;
1132 if (IsEqualIID(riid, &IID_IUnknown))
1133 *ppv = (LPVOID)iface;
1134 else if (IsEqualIID(riid, &IID_IPin))
1135 *ppv = (LPVOID)iface;
1137 if (*ppv)
1139 IUnknown_AddRef((IUnknown *)(*ppv));
1140 return S_OK;
1143 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1145 return E_NOINTERFACE;
1148 ULONG WINAPI PullPin_Release(IPin * iface)
1150 PullPin *This = (PullPin *)iface;
1151 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1153 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1155 if (!refCount)
1157 if (This->hThread)
1158 PullPin_StopProcessing(This);
1159 if(This->pAlloc)
1160 IMemAllocator_Release(This->pAlloc);
1161 if(This->pReader)
1162 IAsyncReader_Release(This->pReader);
1163 CloseHandle(This->hEventStateChanged);
1164 CoTaskMemFree(This);
1165 return 0;
1167 return refCount;
1170 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1172 for (;;)
1173 SleepEx(INFINITE, TRUE);
1176 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1178 PullPin *This = (PullPin *)iface;
1179 HRESULT hr;
1181 ALLOCATOR_PROPERTIES allocProps;
1182 PIN_INFO pinInfo;
1184 CoInitializeEx(NULL, COINIT_MULTITHREADED);
1186 SetEvent(This->hEventStateChanged);
1188 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1190 if (This->rtCurrent < This->rtStart)
1191 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1193 TRACE("Start\n");
1195 while (This->rtCurrent < This->rtStop && hr == S_OK)
1197 /* FIXME: to improve performance by quite a bit this should be changed
1198 * so that one sample is processed while one sample is fetched. However,
1199 * it is harder to debug so for the moment it will stay as it is */
1200 IMediaSample * pSample = NULL;
1201 REFERENCE_TIME rtSampleStop;
1202 DWORD_PTR dwUser;
1204 pinInfo.pFilter = NULL;
1206 TRACE("Process sample\n");
1208 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1210 if (SUCCEEDED(hr))
1212 rtSampleStop = This->rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1213 if (rtSampleStop > This->rtStop)
1214 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1215 hr = IMediaSample_SetTime(pSample, &This->rtCurrent, &rtSampleStop);
1216 This->rtCurrent = rtSampleStop;
1219 if (SUCCEEDED(hr))
1220 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1222 if (SUCCEEDED(hr))
1223 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1225 if (SUCCEEDED(hr))
1226 hr = IPin_QueryPinInfo((IPin*)&This->pin, &pinInfo);
1228 if (SUCCEEDED(hr))
1229 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1230 else
1231 ERR("Processing error: %x\n", hr);
1233 if (pinInfo.pFilter)
1234 IBaseFilter_Release(pinInfo.pFilter);
1235 if (pSample)
1236 IMediaSample_Release(pSample);
1239 CoUninitialize();
1241 TRACE("End\n");
1244 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1246 PullPin *This = (PullPin *)iface;
1248 TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1250 EnterCriticalSection(This->pin.pCritSec);
1252 HRESULT hr;
1254 CloseHandle(This->hThread);
1255 This->hThread = NULL;
1256 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1257 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1259 LeaveCriticalSection(This->pin.pCritSec);
1261 SetEvent(This->hEventStateChanged);
1263 ExitThread(0);
1266 HRESULT PullPin_InitProcessing(PullPin * This)
1268 HRESULT hr = S_OK;
1270 TRACE("(%p)->()\n", This);
1272 assert(!This->hThread);
1274 /* if we are connected */
1275 if (This->pAlloc)
1277 EnterCriticalSection(This->pin.pCritSec);
1279 DWORD dwThreadId;
1280 assert(!This->hThread);
1282 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1283 if (!This->hThread)
1284 hr = HRESULT_FROM_WIN32(GetLastError());
1286 if (SUCCEEDED(hr))
1287 hr = IMemAllocator_Commit(This->pAlloc);
1289 LeaveCriticalSection(This->pin.pCritSec);
1292 TRACE(" -- %x\n", hr);
1294 return hr;
1297 HRESULT PullPin_StartProcessing(PullPin * This)
1299 /* if we are connected */
1300 TRACE("(%p)->()\n", This);
1301 if(This->pAlloc)
1303 assert(This->hThread);
1305 ResetEvent(This->hEventStateChanged);
1307 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1308 return HRESULT_FROM_WIN32(GetLastError());
1311 return S_OK;
1314 HRESULT PullPin_PauseProcessing(PullPin * This)
1316 /* make the processing function exit its loop */
1317 This->rtStop = 0;
1319 return S_OK;
1322 HRESULT PullPin_StopProcessing(PullPin * This)
1324 /* if we are connected */
1325 if (This->pAlloc)
1327 assert(This->hThread);
1329 ResetEvent(This->hEventStateChanged);
1331 PullPin_PauseProcessing(This);
1333 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1334 return HRESULT_FROM_WIN32(GetLastError());
1337 return S_OK;
1340 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1342 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1343 return S_FALSE;
1344 return S_OK;
1347 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1349 FIXME("(%p)->(%x%08x, %x%08x)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1351 PullPin_BeginFlush((IPin *)This);
1352 /* FIXME: need critical section? */
1353 This->rtStart = rtStart;
1354 This->rtStop = rtStop;
1355 PullPin_EndFlush((IPin *)This);
1357 return S_OK;
1360 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1362 FIXME("(%p)->()\n", iface);
1363 return E_NOTIMPL;
1366 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1368 FIXME("(%p)->()\n", iface);
1369 return E_NOTIMPL;
1372 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1374 FIXME("(%p)->()\n", iface);
1375 return E_NOTIMPL;
1378 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1380 FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1381 return E_NOTIMPL;
1384 static const IPinVtbl PullPin_Vtbl =
1386 PullPin_QueryInterface,
1387 IPinImpl_AddRef,
1388 PullPin_Release,
1389 OutputPin_Connect,
1390 PullPin_ReceiveConnection,
1391 IPinImpl_Disconnect,
1392 IPinImpl_ConnectedTo,
1393 IPinImpl_ConnectionMediaType,
1394 IPinImpl_QueryPinInfo,
1395 IPinImpl_QueryDirection,
1396 IPinImpl_QueryId,
1397 IPinImpl_QueryAccept,
1398 IPinImpl_EnumMediaTypes,
1399 IPinImpl_QueryInternalConnections,
1400 PullPin_EndOfStream,
1401 PullPin_BeginFlush,
1402 PullPin_EndFlush,
1403 PullPin_NewSegment