- A few cosmetic fixes.
[wine.git] / dlls / quartz / pin.c
blob05acb1a6ff6565fb42ddd79c3d2855ecd5170bad
1 /*
2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "quartz_private.h"
22 #include "pin.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include <assert.h>
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
32 static const struct IPinVtbl InputPin_Vtbl;
33 static const struct IPinVtbl OutputPin_Vtbl;
34 static const struct IMemInputPinVtbl MemInputPin_Vtbl;
35 static const struct IPinVtbl PullPin_Vtbl;
37 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
40 #define _IMemInputPin_Offset ((int)(&(((InputPin*)0)->lpVtblMemInput)))
41 #define ICOM_THIS_From_IMemInputPin(impl, iface) impl* This = (impl*)(((char*)iface)-_IMemInputPin_Offset);
43 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
45 /* Tempting to just do a memcpy, but the name field is
46 128 characters long! We will probably never exceed 10
47 most of the time, so we are better off copying
48 each field manually */
49 strcpyW(pDest->achName, pSrc->achName);
50 pDest->dir = pSrc->dir;
51 pDest->pFilter = pSrc->pFilter;
52 IBaseFilter_AddRef(pDest->pFilter);
55 /* Internal function called as a helper to IPin_Connect */
56 /* specific AM_MEDIA_TYPE - it cannot be NULL */
57 /* NOTE: not part of standard interface */
58 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
60 ICOM_THIS(OutputPin, iface);
61 HRESULT hr;
62 IMemAllocator * pMemAlloc = NULL;
63 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
65 TRACE("(%p, %p)\n", pReceivePin, pmt);
66 dump_AM_MEDIA_TYPE(pmt);
68 /* FIXME: call queryacceptproc */
70 This->pin.pConnectedTo = pReceivePin;
71 IPin_AddRef(pReceivePin);
72 CopyMediaType(&This->pin.mtCurrent, pmt);
74 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
76 /* get the IMemInputPin interface we will use to deliver samples to the
77 * connected pin */
78 if (SUCCEEDED(hr))
80 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
82 if (SUCCEEDED(hr))
83 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
85 if (SUCCEEDED(hr))
86 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
88 if (pMemAlloc)
89 IMemAllocator_Release(pMemAlloc);
91 /* break connection if we couldn't get the allocator */
92 if (FAILED(hr))
93 IPin_Disconnect(pReceivePin);
96 if (FAILED(hr))
98 IPin_Release(This->pin.pConnectedTo);
99 This->pin.pConnectedTo = NULL;
100 DeleteMediaType(&This->pin.mtCurrent);
103 TRACE(" -- %lx\n", hr);
104 return hr;
107 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
109 InputPin * pPinImpl;
111 *ppPin = NULL;
113 if (pPinInfo->dir != PINDIR_INPUT)
115 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
116 return E_INVALIDARG;
119 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
121 if (!pPinImpl)
122 return E_OUTOFMEMORY;
124 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
126 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
127 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
129 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
130 return S_OK;
132 return E_FAIL;
135 /* Note that we don't init the vtables here (like C++ constructor) */
136 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
138 /* Common attributes */
139 pPinImpl->pin.refCount = 1;
140 pPinImpl->pin.pConnectedTo = NULL;
141 pPinImpl->pin.fnQueryAccept = pQueryAccept;
142 pPinImpl->pin.pUserData = pUserData;
143 pPinImpl->pin.pCritSec = pCritSec;
144 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
146 /* Input pin attributes */
147 pPinImpl->fnSampleProc = pSampleProc;
148 pPinImpl->pAllocator = NULL;
149 pPinImpl->tStart = 0;
150 pPinImpl->tStop = 0;
151 pPinImpl->dRate = 0;
153 return S_OK;
156 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
158 /* Common attributes */
159 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
160 pPinImpl->pin.refCount = 1;
161 pPinImpl->pin.pConnectedTo = NULL;
162 pPinImpl->pin.fnQueryAccept = pQueryAccept;
163 pPinImpl->pin.pUserData = pUserData;
164 pPinImpl->pin.pCritSec = pCritSec;
165 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
167 /* Output pin attributes */
168 pPinImpl->pMemInputPin = NULL;
169 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
170 if (props)
172 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
173 if (pPinImpl->allocProps.cbAlign == 0)
174 pPinImpl->allocProps.cbAlign = 1;
176 else
177 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
180 return S_OK;
183 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
185 OutputPin * pPinImpl;
187 *ppPin = NULL;
189 if (pPinInfo->dir != PINDIR_OUTPUT)
191 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
192 return E_INVALIDARG;
195 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
197 if (!pPinImpl)
198 return E_OUTOFMEMORY;
200 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
202 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
204 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
205 return S_OK;
207 return E_FAIL;
210 /*** Common pin functions ***/
212 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
214 ICOM_THIS(IPinImpl, iface);
216 TRACE("()\n");
218 return InterlockedIncrement(&This->refCount);
221 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
223 HRESULT hr;
224 ICOM_THIS(IPinImpl, iface);
226 TRACE("()\n");
228 EnterCriticalSection(This->pCritSec);
230 if (This->pConnectedTo)
232 IPin_Release(This->pConnectedTo);
233 This->pConnectedTo = NULL;
234 hr = S_OK;
236 else
237 hr = S_FALSE;
239 LeaveCriticalSection(This->pCritSec);
241 return hr;
244 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
246 ICOM_THIS(IPinImpl, iface);
248 /* TRACE("(%p)\n", ppPin);*/
250 *ppPin = This->pConnectedTo;
252 if (*ppPin)
254 IPin_AddRef(*ppPin);
255 return S_OK;
257 else
258 return VFW_E_NOT_CONNECTED;
261 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
263 HRESULT hr;
264 ICOM_THIS(IPinImpl, iface);
266 TRACE("(%p)\n", pmt);
268 EnterCriticalSection(This->pCritSec);
270 if (This->pConnectedTo)
272 CopyMediaType(pmt, &This->mtCurrent);
273 hr = S_OK;
275 else
277 ZeroMemory(pmt, sizeof(*pmt));
278 hr = VFW_E_NOT_CONNECTED;
281 LeaveCriticalSection(This->pCritSec);
283 return hr;
286 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
288 ICOM_THIS(IPinImpl, iface);
290 TRACE("(%p)\n", pInfo);
292 Copy_PinInfo(pInfo, &This->pinInfo);
294 return S_OK;
297 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
299 ICOM_THIS(IPinImpl, iface);
301 TRACE("(%p)\n", pPinDir);
303 *pPinDir = This->pinInfo.dir;
305 return S_OK;
308 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
310 ICOM_THIS(IPinImpl, iface);
312 TRACE("(%p)\n", Id);
314 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
315 if (!Id)
316 return E_OUTOFMEMORY;
318 strcpyW(*Id, This->pinInfo.achName);
320 return S_OK;
323 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
325 ICOM_THIS(IPinImpl, iface);
327 TRACE("(%p)\n", pmt);
329 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
332 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
334 ENUMMEDIADETAILS emd;
336 TRACE("(%p)\n", ppEnum);
338 /* override this method to allow enumeration of your types */
339 emd.cMediaTypes = 0;
340 emd.pMediaTypes = NULL;
342 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
345 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
347 TRACE("(%p, %p)\n", apPin, cPin);
349 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
352 /*** IPin implementation for an input pin ***/
354 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
356 ICOM_THIS(InputPin, iface);
358 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
360 *ppv = NULL;
362 if (IsEqualIID(riid, &IID_IUnknown))
363 *ppv = (LPVOID)iface;
364 else if (IsEqualIID(riid, &IID_IPin))
365 *ppv = (LPVOID)iface;
366 else if (IsEqualIID(riid, &IID_IMemInputPin))
367 *ppv = (LPVOID)&This->lpVtblMemInput;
369 if (*ppv)
371 IUnknown_AddRef((IUnknown *)(*ppv));
372 return S_OK;
375 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
377 return E_NOINTERFACE;
380 ULONG WINAPI InputPin_Release(IPin * iface)
382 ICOM_THIS(InputPin, iface);
384 TRACE("()\n");
386 if (!InterlockedDecrement(&This->pin.refCount))
388 DeleteMediaType(&This->pin.mtCurrent);
389 if (This->pAllocator)
390 IMemAllocator_Release(This->pAllocator);
391 CoTaskMemFree(This);
392 return 0;
394 else
395 return This->pin.refCount;
398 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
400 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
402 return E_UNEXPECTED;
406 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
408 ICOM_THIS(InputPin, iface);
409 PIN_DIRECTION pindirReceive;
410 HRESULT hr = S_OK;
412 TRACE("(%p, %p)\n", pReceivePin, pmt);
413 dump_AM_MEDIA_TYPE(pmt);
415 EnterCriticalSection(This->pin.pCritSec);
417 if (This->pin.pConnectedTo)
418 hr = VFW_E_ALREADY_CONNECTED;
420 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
421 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
422 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
424 if (SUCCEEDED(hr))
426 IPin_QueryDirection(pReceivePin, &pindirReceive);
428 if (pindirReceive != PINDIR_OUTPUT)
430 ERR("Can't connect from non-output pin\n");
431 hr = VFW_E_INVALID_DIRECTION;
435 if (SUCCEEDED(hr))
437 CopyMediaType(&This->pin.mtCurrent, pmt);
438 This->pin.pConnectedTo = pReceivePin;
439 IPin_AddRef(pReceivePin);
442 LeaveCriticalSection(This->pin.pCritSec);
444 return hr;
447 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
449 TRACE("()\n");
451 return S_OK;
454 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
456 FIXME("()\n");
457 return E_NOTIMPL;
460 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
462 FIXME("()\n");
463 return E_NOTIMPL;
466 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
468 ICOM_THIS(InputPin, iface);
470 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
472 This->tStart = tStart;
473 This->tStop = tStop;
474 This->dRate = dRate;
476 return S_OK;
479 static const IPinVtbl InputPin_Vtbl =
481 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
482 InputPin_QueryInterface,
483 IPinImpl_AddRef,
484 InputPin_Release,
485 InputPin_Connect,
486 InputPin_ReceiveConnection,
487 IPinImpl_Disconnect,
488 IPinImpl_ConnectedTo,
489 IPinImpl_ConnectionMediaType,
490 IPinImpl_QueryPinInfo,
491 IPinImpl_QueryDirection,
492 IPinImpl_QueryId,
493 IPinImpl_QueryAccept,
494 IPinImpl_EnumMediaTypes,
495 IPinImpl_QueryInternalConnections,
496 InputPin_EndOfStream,
497 InputPin_BeginFlush,
498 InputPin_EndFlush,
499 InputPin_NewSegment
502 /*** IMemInputPin implementation ***/
504 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
506 ICOM_THIS_From_IMemInputPin(InputPin, iface);
508 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
511 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
513 ICOM_THIS_From_IMemInputPin(InputPin, iface);
515 return IPin_AddRef((IPin *)&This->pin);
518 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
520 ICOM_THIS_From_IMemInputPin(InputPin, iface);
522 return IPin_Release((IPin *)&This->pin);
525 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
527 ICOM_THIS_From_IMemInputPin(InputPin, iface);
529 TRACE("MemInputPin_GetAllocator()\n");
531 *ppAllocator = This->pAllocator;
532 if (*ppAllocator)
533 IMemAllocator_AddRef(*ppAllocator);
535 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
538 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
540 ICOM_THIS_From_IMemInputPin(InputPin, iface);
542 TRACE("()\n");
544 if (This->pAllocator)
545 IMemAllocator_Release(This->pAllocator);
546 This->pAllocator = pAllocator;
547 if (This->pAllocator)
548 IMemAllocator_AddRef(This->pAllocator);
550 return S_OK;
553 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
555 TRACE("(%p)\n", pProps);
557 /* override this method if you have any specific requirements */
559 return E_NOTIMPL;
562 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
564 ICOM_THIS_From_IMemInputPin(InputPin, iface);
566 /* this trace commented out for performance reasons */
567 /* TRACE("(%p)\n", pSample);*/
569 return This->fnSampleProc(This->pin.pUserData, pSample);
572 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
574 HRESULT hr = S_OK;
575 TRACE("(%p, %ld, %p)\n", pSamples, nSamples, nSamplesProcessed);
577 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
579 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
580 if (hr != S_OK)
581 break;
584 return hr;
587 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
589 FIXME("()\n");
591 /* FIXME: we should check whether any output pins will block */
593 return S_OK;
596 static const IMemInputPinVtbl MemInputPin_Vtbl =
598 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
599 MemInputPin_QueryInterface,
600 MemInputPin_AddRef,
601 MemInputPin_Release,
602 MemInputPin_GetAllocator,
603 MemInputPin_NotifyAllocator,
604 MemInputPin_GetAllocatorRequirements,
605 MemInputPin_Receive,
606 MemInputPin_ReceiveMultiple,
607 MemInputPin_ReceiveCanBlock
610 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
612 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
614 *ppv = NULL;
616 if (IsEqualIID(riid, &IID_IUnknown))
617 *ppv = (LPVOID)iface;
618 else if (IsEqualIID(riid, &IID_IPin))
619 *ppv = (LPVOID)iface;
621 if (*ppv)
623 IUnknown_AddRef((IUnknown *)(*ppv));
624 return S_OK;
627 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
629 return E_NOINTERFACE;
632 ULONG WINAPI OutputPin_Release(IPin * iface)
634 ICOM_THIS(OutputPin, iface);
636 TRACE("()\n");
638 if (!InterlockedDecrement(&This->pin.refCount))
640 DeleteMediaType(&This->pin.mtCurrent);
641 CoTaskMemFree(This);
642 return 0;
644 return This->pin.refCount;
647 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
649 HRESULT hr;
650 ICOM_THIS(OutputPin, iface);
652 TRACE("(%p, %p)\n", pReceivePin, pmt);
653 dump_AM_MEDIA_TYPE(pmt);
655 /* If we try to connect to ourself, we will definitely deadlock.
656 * There are other cases where we could deadlock too, but this
657 * catches the obvious case */
658 assert(pReceivePin != iface);
660 EnterCriticalSection(This->pin.pCritSec);
662 /* if we have been a specific type to connect with, then we can either connect
663 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
664 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
665 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
666 else
668 /* negotiate media type */
670 IEnumMediaTypes * pEnumCandidates;
671 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
673 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
675 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
677 /* try this filter's media types first */
678 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
680 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
681 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
683 hr = S_OK;
684 CoTaskMemFree(pmtCandidate);
685 break;
687 CoTaskMemFree(pmtCandidate);
689 IEnumMediaTypes_Release(pEnumCandidates);
692 /* then try receiver filter's media types */
693 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
695 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
697 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
698 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
700 hr = S_OK;
701 CoTaskMemFree(pmtCandidate);
702 break;
704 CoTaskMemFree(pmtCandidate);
705 } /* while */
706 IEnumMediaTypes_Release(pEnumCandidates);
707 } /* if not found */
708 } /* if negotiate media type */
709 } /* if succeeded */
710 LeaveCriticalSection(This->pin.pCritSec);
712 FIXME(" -- %lx\n", hr);
713 return hr;
716 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
718 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
720 return E_UNEXPECTED;
723 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
725 HRESULT hr;
726 ICOM_THIS(OutputPin, iface);
728 TRACE("()\n");
730 EnterCriticalSection(This->pin.pCritSec);
732 if (This->pMemInputPin)
734 IMemInputPin_Release(This->pMemInputPin);
735 This->pMemInputPin = NULL;
737 if (This->pin.pConnectedTo)
739 IPin_Release(This->pin.pConnectedTo);
740 This->pin.pConnectedTo = NULL;
741 hr = S_OK;
743 else
744 hr = S_FALSE;
746 LeaveCriticalSection(This->pin.pCritSec);
748 return hr;
751 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
753 TRACE("()\n");
755 /* not supposed to do anything in an output pin */
757 return E_UNEXPECTED;
760 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
762 TRACE("()\n");
764 /* not supposed to do anything in an output pin */
766 return E_UNEXPECTED;
769 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
771 TRACE("()\n");
773 /* not supposed to do anything in an output pin */
775 return E_UNEXPECTED;
778 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
780 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
782 /* not supposed to do anything in an output pin */
784 return E_UNEXPECTED;
787 static const IPinVtbl OutputPin_Vtbl =
789 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
790 OutputPin_QueryInterface,
791 IPinImpl_AddRef,
792 OutputPin_Release,
793 OutputPin_Connect,
794 OutputPin_ReceiveConnection,
795 OutputPin_Disconnect,
796 IPinImpl_ConnectedTo,
797 IPinImpl_ConnectionMediaType,
798 IPinImpl_QueryPinInfo,
799 IPinImpl_QueryDirection,
800 IPinImpl_QueryId,
801 IPinImpl_QueryAccept,
802 IPinImpl_EnumMediaTypes,
803 IPinImpl_QueryInternalConnections,
804 OutputPin_EndOfStream,
805 OutputPin_BeginFlush,
806 OutputPin_EndFlush,
807 OutputPin_NewSegment
810 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
812 HRESULT hr;
814 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
816 EnterCriticalSection(This->pin.pCritSec);
818 if (!This->pin.pConnectedTo)
819 hr = VFW_E_NOT_CONNECTED;
820 else
822 IMemInputPin * pMemInputPin = NULL;
823 IMemAllocator * pAlloc = NULL;
825 /* FIXME: should we cache this? */
826 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
828 if (SUCCEEDED(hr))
829 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
831 if (SUCCEEDED(hr))
832 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
834 if (SUCCEEDED(hr))
835 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
837 if (pAlloc)
838 IMemAllocator_Release(pAlloc);
839 if (pMemInputPin)
840 IMemInputPin_Release(pMemInputPin);
843 LeaveCriticalSection(This->pin.pCritSec);
845 return hr;
848 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
850 HRESULT hr;
851 IMemInputPin * pMemConnected = NULL;
853 EnterCriticalSection(This->pin.pCritSec);
855 if (!This->pin.pConnectedTo)
856 hr = VFW_E_NOT_CONNECTED;
857 else
859 /* FIXME: should we cache this? - if we do, then we need to keep the local version
860 * and AddRef here instead */
861 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemConnected);
864 LeaveCriticalSection(This->pin.pCritSec);
866 if (SUCCEEDED(hr) && pMemConnected)
868 /* NOTE: if we are in a critical section when Receive is called
869 * then it causes some problems (most notably with the native Video
870 * Renderer) if we are re-entered for whatever reason */
871 hr = IMemInputPin_Receive(pMemConnected, pSample);
872 IMemInputPin_Release(pMemConnected);
875 return hr;
878 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
880 HRESULT hr;
882 EnterCriticalSection(This->pin.pCritSec);
884 if (!This->pin.pConnectedTo)
885 hr = VFW_E_NOT_CONNECTED;
886 else
887 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
889 LeaveCriticalSection(This->pin.pCritSec);
891 return hr;
894 HRESULT OutputPin_CommitAllocator(OutputPin * This)
896 HRESULT hr;
898 TRACE("()\n");
900 EnterCriticalSection(This->pin.pCritSec);
902 if (!This->pin.pConnectedTo)
903 hr = VFW_E_NOT_CONNECTED;
904 else
906 IMemInputPin * pMemInputPin = NULL;
907 IMemAllocator * pAlloc = NULL;
908 /* FIXME: should we cache this? */
909 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
911 if (SUCCEEDED(hr))
912 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
914 if (SUCCEEDED(hr))
915 hr = IMemAllocator_Commit(pAlloc);
917 if (pAlloc)
918 IMemAllocator_Release(pAlloc);
920 if (pMemInputPin)
921 IMemInputPin_Release(pMemInputPin);
924 LeaveCriticalSection(This->pin.pCritSec);
926 return hr;
929 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
931 HRESULT hr;
933 TRACE("()\n");
935 EnterCriticalSection(This->pin.pCritSec);
937 if (!This->pin.pConnectedTo)
938 hr = VFW_E_NOT_CONNECTED;
939 else
941 IMemInputPin * pMemInputPin = NULL;
942 IMemAllocator * pAlloc = NULL;
943 /* FIXME: should we cache this? */
944 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
946 if (SUCCEEDED(hr))
947 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
949 if (SUCCEEDED(hr))
950 hr = IMemAllocator_Decommit(pAlloc);
952 if (pAlloc)
953 IMemAllocator_Release(pAlloc);
955 if (pMemInputPin)
956 IMemInputPin_Release(pMemInputPin);
958 if (SUCCEEDED(hr))
959 hr = IPin_Disconnect(This->pin.pConnectedTo);
962 LeaveCriticalSection(This->pin.pCritSec);
964 return hr;
968 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
970 PullPin * pPinImpl;
972 *ppPin = NULL;
974 if (pPinInfo->dir != PINDIR_INPUT)
976 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
977 return E_INVALIDARG;
980 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
982 if (!pPinImpl)
983 return E_OUTOFMEMORY;
985 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
987 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
989 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
990 return S_OK;
992 return E_FAIL;
995 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
997 /* Common attributes */
998 pPinImpl->pin.refCount = 1;
999 pPinImpl->pin.pConnectedTo = NULL;
1000 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1001 pPinImpl->pin.pUserData = pUserData;
1002 pPinImpl->pin.pCritSec = pCritSec;
1003 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1005 /* Input pin attributes */
1006 pPinImpl->fnSampleProc = pSampleProc;
1007 pPinImpl->fnPreConnect = NULL;
1008 pPinImpl->pAlloc = NULL;
1009 pPinImpl->pReader = NULL;
1010 pPinImpl->hThread = NULL;
1011 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1013 pPinImpl->rtStart = 0;
1014 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1016 return S_OK;
1019 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1021 PIN_DIRECTION pindirReceive;
1022 HRESULT hr = S_OK;
1023 ICOM_THIS(PullPin, iface);
1025 TRACE("(%p, %p)\n", pReceivePin, pmt);
1026 dump_AM_MEDIA_TYPE(pmt);
1028 EnterCriticalSection(This->pin.pCritSec);
1030 if (This->pin.pConnectedTo)
1031 hr = VFW_E_ALREADY_CONNECTED;
1033 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1034 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1035 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1037 if (SUCCEEDED(hr))
1039 IPin_QueryDirection(pReceivePin, &pindirReceive);
1041 if (pindirReceive != PINDIR_OUTPUT)
1043 ERR("Can't connect from non-output pin\n");
1044 hr = VFW_E_INVALID_DIRECTION;
1048 if (SUCCEEDED(hr))
1050 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1053 if (SUCCEEDED(hr))
1055 ALLOCATOR_PROPERTIES props;
1056 props.cBuffers = 3;
1057 props.cbBuffer = 64 * 1024; /* 64k bytes */
1058 props.cbAlign = 1;
1059 props.cbPrefix = 0;
1060 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1063 if (SUCCEEDED(hr) && This->fnPreConnect)
1065 hr = This->fnPreConnect(iface, pReceivePin);
1068 if (SUCCEEDED(hr))
1070 CopyMediaType(&This->pin.mtCurrent, pmt);
1071 This->pin.pConnectedTo = pReceivePin;
1072 IPin_AddRef(pReceivePin);
1075 LeaveCriticalSection(This->pin.pCritSec);
1076 return hr;
1079 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1081 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
1083 *ppv = NULL;
1085 if (IsEqualIID(riid, &IID_IUnknown))
1086 *ppv = (LPVOID)iface;
1087 else if (IsEqualIID(riid, &IID_IPin))
1088 *ppv = (LPVOID)iface;
1090 if (*ppv)
1092 IUnknown_AddRef((IUnknown *)(*ppv));
1093 return S_OK;
1096 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1098 return E_NOINTERFACE;
1101 ULONG WINAPI PullPin_Release(IPin * iface)
1103 ICOM_THIS(PullPin, iface);
1105 TRACE("()\n");
1107 if (!InterlockedDecrement(&This->pin.refCount))
1109 if (This->hThread)
1110 PullPin_StopProcessing(This);
1111 IMemAllocator_Release(This->pAlloc);
1112 IAsyncReader_Release(This->pReader);
1113 CloseHandle(This->hEventStateChanged);
1114 CoTaskMemFree(This);
1115 return 0;
1117 return This->pin.refCount;
1120 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1122 for (;;)
1123 SleepEx(INFINITE, TRUE);
1126 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1128 ICOM_THIS(PullPin, iface);
1129 HRESULT hr;
1131 REFERENCE_TIME rtCurrent;
1132 ALLOCATOR_PROPERTIES allocProps;
1134 SetEvent(This->hEventStateChanged);
1136 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1138 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1140 while (rtCurrent < This->rtStop)
1142 /* FIXME: to improve performance by quite a bit this should be changed
1143 * so that one sample is processed while one sample is fetched. However,
1144 * it is harder to debug so for the moment it will stay as it is */
1145 IMediaSample * pSample = NULL;
1146 REFERENCE_TIME rtSampleStop;
1147 DWORD dwUser;
1149 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1151 if (SUCCEEDED(hr))
1153 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1154 if (rtSampleStop > This->rtStop)
1155 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1156 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1157 rtCurrent = rtSampleStop;
1160 if (SUCCEEDED(hr))
1161 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1163 if (SUCCEEDED(hr))
1164 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1166 if (SUCCEEDED(hr))
1167 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1168 else
1169 ERR("Processing error: %lx\n", hr);
1171 if (pSample)
1172 IMemAllocator_ReleaseBuffer(This->pAlloc, pSample);
1176 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1178 ICOM_THIS(PullPin, iface);
1180 TRACE("()\n");
1182 EnterCriticalSection(This->pin.pCritSec);
1184 HRESULT hr;
1186 CloseHandle(This->hThread);
1187 This->hThread = NULL;
1188 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1189 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1191 LeaveCriticalSection(This->pin.pCritSec);
1193 SetEvent(This->hEventStateChanged);
1195 ExitThread(0);
1198 HRESULT PullPin_InitProcessing(PullPin * This)
1200 HRESULT hr = S_OK;
1202 TRACE("()\n");
1204 assert(!This->hThread);
1206 /* if we are connected */
1207 if (This->pAlloc)
1209 EnterCriticalSection(This->pin.pCritSec);
1211 DWORD dwThreadId;
1212 assert(!This->hThread);
1214 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1215 if (!This->hThread)
1216 hr = HRESULT_FROM_WIN32(GetLastError());
1218 if (SUCCEEDED(hr))
1219 hr = IMemAllocator_Commit(This->pAlloc);
1221 LeaveCriticalSection(This->pin.pCritSec);
1224 TRACE(" -- %lx\n", hr);
1226 return hr;
1229 HRESULT PullPin_StartProcessing(PullPin * This)
1231 /* if we are connected */
1232 if(This->pAlloc)
1234 assert(This->hThread);
1236 ResetEvent(This->hEventStateChanged);
1238 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1239 return HRESULT_FROM_WIN32(GetLastError());
1242 return S_OK;
1245 HRESULT PullPin_PauseProcessing(PullPin * This)
1247 /* make the processing function exit its loop */
1248 This->rtStop = 0;
1250 return S_OK;
1253 HRESULT PullPin_StopProcessing(PullPin * This)
1255 /* if we are connected */
1256 if (This->pAlloc)
1258 assert(This->hThread);
1260 ResetEvent(This->hEventStateChanged);
1262 PullPin_PauseProcessing(This);
1264 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1265 return HRESULT_FROM_WIN32(GetLastError());
1268 return S_OK;
1271 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1273 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1274 return S_FALSE;
1275 return S_OK;
1278 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1280 FIXME("(%lx%08lx, %lx%08lx)\n", (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1282 PullPin_BeginFlush((IPin *)This);
1283 /* FIXME: need critical section? */
1284 This->rtStart = rtStart;
1285 This->rtStop = rtStop;
1286 PullPin_EndFlush((IPin *)This);
1288 return S_OK;
1291 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1293 FIXME("()\n");
1294 return E_NOTIMPL;
1297 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1299 FIXME("()\n");
1300 return E_NOTIMPL;
1303 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1305 FIXME("()\n");
1306 return E_NOTIMPL;
1309 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1311 FIXME("()\n");
1312 return E_NOTIMPL;
1315 static const IPinVtbl PullPin_Vtbl =
1317 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1318 PullPin_QueryInterface,
1319 IPinImpl_AddRef,
1320 PullPin_Release,
1321 OutputPin_Connect,
1322 PullPin_ReceiveConnection,
1323 IPinImpl_Disconnect,
1324 IPinImpl_ConnectedTo,
1325 IPinImpl_ConnectionMediaType,
1326 IPinImpl_QueryPinInfo,
1327 IPinImpl_QueryDirection,
1328 IPinImpl_QueryId,
1329 IPinImpl_QueryAccept,
1330 IPinImpl_EnumMediaTypes,
1331 IPinImpl_QueryInternalConnections,
1332 PullPin_EndOfStream,
1333 PullPin_BeginFlush,
1334 PullPin_EndFlush,
1335 PullPin_NewSegment