Fixed buffer overflow.
[wine/multimedia.git] / dlls / quartz / pin.c
blobdfc6a5134c3a1719d1de32265195ca160420efc5
1 /*
2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "quartz_private.h"
22 #include "pin.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include <assert.h>
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
32 static const struct IPinVtbl InputPin_Vtbl;
33 static const struct IPinVtbl OutputPin_Vtbl;
34 static const struct IMemInputPinVtbl MemInputPin_Vtbl;
35 static const struct IPinVtbl PullPin_Vtbl;
37 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
40 #define _IMemInputPin_Offset ((int)(&(((InputPin*)0)->lpVtblMemInput)))
41 #define ICOM_THIS_From_IMemInputPin(impl, iface) impl* This = (impl*)(((char*)iface)-_IMemInputPin_Offset);
43 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
45 /* Tempting to just do a memcpy, but the name field is
46 128 characters long! We will probably never exceed 10
47 most of the time, so we are better off copying
48 each field manually */
49 strcpyW(pDest->achName, pSrc->achName);
50 pDest->dir = pSrc->dir;
51 pDest->pFilter = pSrc->pFilter;
52 IBaseFilter_AddRef(pDest->pFilter);
55 /* Internal function called as a helper to IPin_Connect */
56 /* specific AM_MEDIA_TYPE - it cannot be NULL */
57 /* NOTE: not part of standard interface */
58 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
60 ICOM_THIS(OutputPin, iface);
61 HRESULT hr;
62 IMemAllocator * pMemAlloc = NULL;
63 ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
65 TRACE("(%p, %p)\n", pReceivePin, pmt);
66 dump_AM_MEDIA_TYPE(pmt);
68 /* FIXME: call queryacceptproc */
70 This->pin.pConnectedTo = pReceivePin;
71 IPin_AddRef(pReceivePin);
72 CopyMediaType(&This->pin.mtCurrent, pmt);
74 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
76 /* get the IMemInputPin interface we will use to deliver samples to the
77 * connected pin */
78 if (SUCCEEDED(hr))
80 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
82 if (SUCCEEDED(hr))
83 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
85 if (SUCCEEDED(hr))
86 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
88 if (pMemAlloc)
89 IMemAllocator_Release(pMemAlloc);
91 /* break connection if we couldn't get the allocator */
92 if (FAILED(hr))
93 IPin_Disconnect(pReceivePin);
96 if (FAILED(hr))
98 IPin_Release(This->pin.pConnectedTo);
99 This->pin.pConnectedTo = NULL;
100 DeleteMediaType(&This->pin.mtCurrent);
103 TRACE(" -- %lx\n", hr);
104 return hr;
107 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
109 InputPin * pPinImpl;
111 *ppPin = NULL;
113 if (pPinInfo->dir != PINDIR_INPUT)
115 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
116 return E_INVALIDARG;
119 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
121 if (!pPinImpl)
122 return E_OUTOFMEMORY;
124 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
126 pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
127 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
129 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
130 return S_OK;
132 return E_FAIL;
135 /* Note that we don't init the vtables here (like C++ constructor) */
136 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
138 TRACE("\n");
140 /* Common attributes */
141 pPinImpl->pin.refCount = 1;
142 pPinImpl->pin.pConnectedTo = NULL;
143 pPinImpl->pin.fnQueryAccept = pQueryAccept;
144 pPinImpl->pin.pUserData = pUserData;
145 pPinImpl->pin.pCritSec = pCritSec;
146 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
148 /* Input pin attributes */
149 pPinImpl->fnSampleProc = pSampleProc;
150 pPinImpl->pAllocator = NULL;
151 pPinImpl->tStart = 0;
152 pPinImpl->tStop = 0;
153 pPinImpl->dRate = 0;
155 return S_OK;
158 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
160 TRACE("\n");
162 /* Common attributes */
163 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
164 pPinImpl->pin.refCount = 1;
165 pPinImpl->pin.pConnectedTo = NULL;
166 pPinImpl->pin.fnQueryAccept = pQueryAccept;
167 pPinImpl->pin.pUserData = pUserData;
168 pPinImpl->pin.pCritSec = pCritSec;
169 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
171 /* Output pin attributes */
172 pPinImpl->pMemInputPin = NULL;
173 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
174 if (props)
176 memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
177 if (pPinImpl->allocProps.cbAlign == 0)
178 pPinImpl->allocProps.cbAlign = 1;
180 else
181 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
184 return S_OK;
187 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
189 OutputPin * pPinImpl;
191 *ppPin = NULL;
193 if (pPinInfo->dir != PINDIR_OUTPUT)
195 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
196 return E_INVALIDARG;
199 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
201 if (!pPinImpl)
202 return E_OUTOFMEMORY;
204 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
206 pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
208 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
209 return S_OK;
211 return E_FAIL;
214 /*** Common pin functions ***/
216 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
218 ICOM_THIS(IPinImpl, iface);
220 TRACE("()\n");
222 return InterlockedIncrement(&This->refCount);
225 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
227 HRESULT hr;
228 ICOM_THIS(IPinImpl, iface);
230 TRACE("()\n");
232 EnterCriticalSection(This->pCritSec);
234 if (This->pConnectedTo)
236 IPin_Release(This->pConnectedTo);
237 This->pConnectedTo = NULL;
238 hr = S_OK;
240 else
241 hr = S_FALSE;
243 LeaveCriticalSection(This->pCritSec);
245 return hr;
248 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
250 ICOM_THIS(IPinImpl, iface);
252 /* TRACE("(%p)\n", ppPin);*/
254 *ppPin = This->pConnectedTo;
256 if (*ppPin)
258 IPin_AddRef(*ppPin);
259 return S_OK;
261 else
262 return VFW_E_NOT_CONNECTED;
265 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
267 HRESULT hr;
268 ICOM_THIS(IPinImpl, iface);
270 TRACE("(%p)\n", pmt);
272 EnterCriticalSection(This->pCritSec);
274 if (This->pConnectedTo)
276 CopyMediaType(pmt, &This->mtCurrent);
277 hr = S_OK;
279 else
281 ZeroMemory(pmt, sizeof(*pmt));
282 hr = VFW_E_NOT_CONNECTED;
285 LeaveCriticalSection(This->pCritSec);
287 return hr;
290 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
292 ICOM_THIS(IPinImpl, iface);
294 TRACE("(%p)\n", pInfo);
296 Copy_PinInfo(pInfo, &This->pinInfo);
298 return S_OK;
301 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
303 ICOM_THIS(IPinImpl, iface);
305 TRACE("(%p)\n", pPinDir);
307 *pPinDir = This->pinInfo.dir;
309 return S_OK;
312 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
314 ICOM_THIS(IPinImpl, iface);
316 TRACE("(%p)\n", Id);
318 *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
319 if (!Id)
320 return E_OUTOFMEMORY;
322 strcpyW(*Id, This->pinInfo.achName);
324 return S_OK;
327 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
329 ICOM_THIS(IPinImpl, iface);
331 TRACE("(%p)\n", pmt);
333 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
336 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
338 ENUMMEDIADETAILS emd;
340 TRACE("(%p)\n", ppEnum);
342 /* override this method to allow enumeration of your types */
343 emd.cMediaTypes = 0;
344 emd.pMediaTypes = NULL;
346 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
349 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
351 TRACE("(%p, %p)\n", apPin, cPin);
353 return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
356 /*** IPin implementation for an input pin ***/
358 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
360 ICOM_THIS(InputPin, iface);
362 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
364 *ppv = NULL;
366 if (IsEqualIID(riid, &IID_IUnknown))
367 *ppv = (LPVOID)iface;
368 else if (IsEqualIID(riid, &IID_IPin))
369 *ppv = (LPVOID)iface;
370 else if (IsEqualIID(riid, &IID_IMemInputPin))
371 *ppv = (LPVOID)&This->lpVtblMemInput;
373 if (*ppv)
375 IUnknown_AddRef((IUnknown *)(*ppv));
376 return S_OK;
379 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
381 return E_NOINTERFACE;
384 ULONG WINAPI InputPin_Release(IPin * iface)
386 ICOM_THIS(InputPin, iface);
388 TRACE("()\n");
390 if (!InterlockedDecrement(&This->pin.refCount))
392 DeleteMediaType(&This->pin.mtCurrent);
393 if (This->pAllocator)
394 IMemAllocator_Release(This->pAllocator);
395 CoTaskMemFree(This);
396 return 0;
398 else
399 return This->pin.refCount;
402 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
404 ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
406 return E_UNEXPECTED;
410 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
412 ICOM_THIS(InputPin, iface);
413 PIN_DIRECTION pindirReceive;
414 HRESULT hr = S_OK;
416 TRACE("(%p, %p)\n", pReceivePin, pmt);
417 dump_AM_MEDIA_TYPE(pmt);
419 EnterCriticalSection(This->pin.pCritSec);
421 if (This->pin.pConnectedTo)
422 hr = VFW_E_ALREADY_CONNECTED;
424 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
425 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
426 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
428 if (SUCCEEDED(hr))
430 IPin_QueryDirection(pReceivePin, &pindirReceive);
432 if (pindirReceive != PINDIR_OUTPUT)
434 ERR("Can't connect from non-output pin\n");
435 hr = VFW_E_INVALID_DIRECTION;
439 if (SUCCEEDED(hr))
441 CopyMediaType(&This->pin.mtCurrent, pmt);
442 This->pin.pConnectedTo = pReceivePin;
443 IPin_AddRef(pReceivePin);
446 LeaveCriticalSection(This->pin.pCritSec);
448 return hr;
451 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
453 TRACE("()\n");
455 return S_OK;
458 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
460 FIXME("()\n");
461 return E_NOTIMPL;
464 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
466 FIXME("()\n");
467 return E_NOTIMPL;
470 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
472 ICOM_THIS(InputPin, iface);
474 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
476 This->tStart = tStart;
477 This->tStop = tStop;
478 This->dRate = dRate;
480 return S_OK;
483 static const IPinVtbl InputPin_Vtbl =
485 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
486 InputPin_QueryInterface,
487 IPinImpl_AddRef,
488 InputPin_Release,
489 InputPin_Connect,
490 InputPin_ReceiveConnection,
491 IPinImpl_Disconnect,
492 IPinImpl_ConnectedTo,
493 IPinImpl_ConnectionMediaType,
494 IPinImpl_QueryPinInfo,
495 IPinImpl_QueryDirection,
496 IPinImpl_QueryId,
497 IPinImpl_QueryAccept,
498 IPinImpl_EnumMediaTypes,
499 IPinImpl_QueryInternalConnections,
500 InputPin_EndOfStream,
501 InputPin_BeginFlush,
502 InputPin_EndFlush,
503 InputPin_NewSegment
506 /*** IMemInputPin implementation ***/
508 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
510 ICOM_THIS_From_IMemInputPin(InputPin, iface);
512 return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
515 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
517 ICOM_THIS_From_IMemInputPin(InputPin, iface);
519 return IPin_AddRef((IPin *)&This->pin);
522 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
524 ICOM_THIS_From_IMemInputPin(InputPin, iface);
526 return IPin_Release((IPin *)&This->pin);
529 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
531 ICOM_THIS_From_IMemInputPin(InputPin, iface);
533 TRACE("MemInputPin_GetAllocator()\n");
535 *ppAllocator = This->pAllocator;
536 if (*ppAllocator)
537 IMemAllocator_AddRef(*ppAllocator);
539 return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
542 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
544 ICOM_THIS_From_IMemInputPin(InputPin, iface);
546 TRACE("()\n");
548 if (This->pAllocator)
549 IMemAllocator_Release(This->pAllocator);
550 This->pAllocator = pAllocator;
551 if (This->pAllocator)
552 IMemAllocator_AddRef(This->pAllocator);
554 return S_OK;
557 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
559 TRACE("(%p)\n", pProps);
561 /* override this method if you have any specific requirements */
563 return E_NOTIMPL;
566 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
568 ICOM_THIS_From_IMemInputPin(InputPin, iface);
570 /* this trace commented out for performance reasons */
571 /* TRACE("(%p)\n", pSample);*/
573 return This->fnSampleProc(This->pin.pUserData, pSample);
576 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
578 HRESULT hr = S_OK;
579 TRACE("(%p, %ld, %p)\n", pSamples, nSamples, nSamplesProcessed);
581 for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
583 hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
584 if (hr != S_OK)
585 break;
588 return hr;
591 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
593 FIXME("()\n");
595 /* FIXME: we should check whether any output pins will block */
597 return S_OK;
600 static const IMemInputPinVtbl MemInputPin_Vtbl =
602 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
603 MemInputPin_QueryInterface,
604 MemInputPin_AddRef,
605 MemInputPin_Release,
606 MemInputPin_GetAllocator,
607 MemInputPin_NotifyAllocator,
608 MemInputPin_GetAllocatorRequirements,
609 MemInputPin_Receive,
610 MemInputPin_ReceiveMultiple,
611 MemInputPin_ReceiveCanBlock
614 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
616 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
618 *ppv = NULL;
620 if (IsEqualIID(riid, &IID_IUnknown))
621 *ppv = (LPVOID)iface;
622 else if (IsEqualIID(riid, &IID_IPin))
623 *ppv = (LPVOID)iface;
625 if (*ppv)
627 IUnknown_AddRef((IUnknown *)(*ppv));
628 return S_OK;
631 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
633 return E_NOINTERFACE;
636 ULONG WINAPI OutputPin_Release(IPin * iface)
638 ICOM_THIS(OutputPin, iface);
640 TRACE("()\n");
642 if (!InterlockedDecrement(&This->pin.refCount))
644 DeleteMediaType(&This->pin.mtCurrent);
645 CoTaskMemFree(This);
646 return 0;
648 return This->pin.refCount;
651 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
653 HRESULT hr;
654 ICOM_THIS(OutputPin, iface);
656 TRACE("(%p, %p)\n", pReceivePin, pmt);
657 dump_AM_MEDIA_TYPE(pmt);
659 /* If we try to connect to ourself, we will definitely deadlock.
660 * There are other cases where we could deadlock too, but this
661 * catches the obvious case */
662 assert(pReceivePin != iface);
664 EnterCriticalSection(This->pin.pCritSec);
666 /* if we have been a specific type to connect with, then we can either connect
667 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
668 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
669 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
670 else
672 /* negotiate media type */
674 IEnumMediaTypes * pEnumCandidates;
675 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
677 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
679 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
681 /* try this filter's media types first */
682 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
684 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
685 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
687 hr = S_OK;
688 CoTaskMemFree(pmtCandidate);
689 break;
691 CoTaskMemFree(pmtCandidate);
693 IEnumMediaTypes_Release(pEnumCandidates);
696 /* then try receiver filter's media types */
697 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
699 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
701 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
702 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
704 hr = S_OK;
705 CoTaskMemFree(pmtCandidate);
706 break;
708 CoTaskMemFree(pmtCandidate);
709 } /* while */
710 IEnumMediaTypes_Release(pEnumCandidates);
711 } /* if not found */
712 } /* if negotiate media type */
713 } /* if succeeded */
714 LeaveCriticalSection(This->pin.pCritSec);
716 FIXME(" -- %lx\n", hr);
717 return hr;
720 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
722 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
724 return E_UNEXPECTED;
727 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
729 HRESULT hr;
730 ICOM_THIS(OutputPin, iface);
732 TRACE("()\n");
734 EnterCriticalSection(This->pin.pCritSec);
736 if (This->pMemInputPin)
738 IMemInputPin_Release(This->pMemInputPin);
739 This->pMemInputPin = NULL;
741 if (This->pin.pConnectedTo)
743 IPin_Release(This->pin.pConnectedTo);
744 This->pin.pConnectedTo = NULL;
745 hr = S_OK;
747 else
748 hr = S_FALSE;
750 LeaveCriticalSection(This->pin.pCritSec);
752 return hr;
755 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
757 TRACE("()\n");
759 /* not supposed to do anything in an output pin */
761 return E_UNEXPECTED;
764 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
766 TRACE("()\n");
768 /* not supposed to do anything in an output pin */
770 return E_UNEXPECTED;
773 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
775 TRACE("()\n");
777 /* not supposed to do anything in an output pin */
779 return E_UNEXPECTED;
782 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
784 TRACE("(%lx%08lx, %lx%08lx, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
786 /* not supposed to do anything in an output pin */
788 return E_UNEXPECTED;
791 static const IPinVtbl OutputPin_Vtbl =
793 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
794 OutputPin_QueryInterface,
795 IPinImpl_AddRef,
796 OutputPin_Release,
797 OutputPin_Connect,
798 OutputPin_ReceiveConnection,
799 OutputPin_Disconnect,
800 IPinImpl_ConnectedTo,
801 IPinImpl_ConnectionMediaType,
802 IPinImpl_QueryPinInfo,
803 IPinImpl_QueryDirection,
804 IPinImpl_QueryId,
805 IPinImpl_QueryAccept,
806 IPinImpl_EnumMediaTypes,
807 IPinImpl_QueryInternalConnections,
808 OutputPin_EndOfStream,
809 OutputPin_BeginFlush,
810 OutputPin_EndFlush,
811 OutputPin_NewSegment
814 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, DWORD dwFlags)
816 HRESULT hr;
818 TRACE("(%p, %p, %p, %lx)\n", ppSample, tStart, tStop, dwFlags);
820 EnterCriticalSection(This->pin.pCritSec);
822 if (!This->pin.pConnectedTo)
823 hr = VFW_E_NOT_CONNECTED;
824 else
826 IMemInputPin * pMemInputPin = NULL;
827 IMemAllocator * pAlloc = NULL;
829 /* FIXME: should we cache this? */
830 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
832 if (SUCCEEDED(hr))
833 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
835 if (SUCCEEDED(hr))
836 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop, dwFlags);
838 if (SUCCEEDED(hr))
839 hr = IMediaSample_SetTime(*ppSample, (REFERENCE_TIME *)tStart, (REFERENCE_TIME *)tStop);
841 if (pAlloc)
842 IMemAllocator_Release(pAlloc);
843 if (pMemInputPin)
844 IMemInputPin_Release(pMemInputPin);
847 LeaveCriticalSection(This->pin.pCritSec);
849 return hr;
852 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
854 HRESULT hr;
855 IMemInputPin * pMemConnected = NULL;
857 EnterCriticalSection(This->pin.pCritSec);
859 if (!This->pin.pConnectedTo)
860 hr = VFW_E_NOT_CONNECTED;
861 else
863 /* FIXME: should we cache this? - if we do, then we need to keep the local version
864 * and AddRef here instead */
865 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemConnected);
868 LeaveCriticalSection(This->pin.pCritSec);
870 if (SUCCEEDED(hr) && pMemConnected)
872 /* NOTE: if we are in a critical section when Receive is called
873 * then it causes some problems (most notably with the native Video
874 * Renderer) if we are re-entered for whatever reason */
875 hr = IMemInputPin_Receive(pMemConnected, pSample);
876 IMemInputPin_Release(pMemConnected);
879 return hr;
882 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
884 HRESULT hr;
886 EnterCriticalSection(This->pin.pCritSec);
888 if (!This->pin.pConnectedTo)
889 hr = VFW_E_NOT_CONNECTED;
890 else
891 hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
893 LeaveCriticalSection(This->pin.pCritSec);
895 return hr;
898 HRESULT OutputPin_CommitAllocator(OutputPin * This)
900 HRESULT hr;
902 TRACE("()\n");
904 EnterCriticalSection(This->pin.pCritSec);
906 if (!This->pin.pConnectedTo)
907 hr = VFW_E_NOT_CONNECTED;
908 else
910 IMemInputPin * pMemInputPin = NULL;
911 IMemAllocator * pAlloc = NULL;
912 /* FIXME: should we cache this? */
913 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
915 if (SUCCEEDED(hr))
916 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
918 if (SUCCEEDED(hr))
919 hr = IMemAllocator_Commit(pAlloc);
921 if (pAlloc)
922 IMemAllocator_Release(pAlloc);
924 if (pMemInputPin)
925 IMemInputPin_Release(pMemInputPin);
928 LeaveCriticalSection(This->pin.pCritSec);
930 return hr;
933 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
935 HRESULT hr;
937 TRACE("()\n");
939 EnterCriticalSection(This->pin.pCritSec);
941 if (!This->pin.pConnectedTo)
942 hr = VFW_E_NOT_CONNECTED;
943 else
945 IMemInputPin * pMemInputPin = NULL;
946 IMemAllocator * pAlloc = NULL;
947 /* FIXME: should we cache this? */
948 hr = IPin_QueryInterface(This->pin.pConnectedTo, &IID_IMemInputPin, (LPVOID *)&pMemInputPin);
950 if (SUCCEEDED(hr))
951 hr = IMemInputPin_GetAllocator(pMemInputPin, &pAlloc);
953 if (SUCCEEDED(hr))
954 hr = IMemAllocator_Decommit(pAlloc);
956 if (pAlloc)
957 IMemAllocator_Release(pAlloc);
959 if (pMemInputPin)
960 IMemInputPin_Release(pMemInputPin);
962 if (SUCCEEDED(hr))
963 hr = IPin_Disconnect(This->pin.pConnectedTo);
966 LeaveCriticalSection(This->pin.pCritSec);
968 return hr;
972 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
974 PullPin * pPinImpl;
976 *ppPin = NULL;
978 if (pPinInfo->dir != PINDIR_INPUT)
980 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
981 return E_INVALIDARG;
984 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
986 if (!pPinImpl)
987 return E_OUTOFMEMORY;
989 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
991 pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
993 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
994 return S_OK;
996 return E_FAIL;
999 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1001 /* Common attributes */
1002 pPinImpl->pin.refCount = 1;
1003 pPinImpl->pin.pConnectedTo = NULL;
1004 pPinImpl->pin.fnQueryAccept = pQueryAccept;
1005 pPinImpl->pin.pUserData = pUserData;
1006 pPinImpl->pin.pCritSec = pCritSec;
1007 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1009 /* Input pin attributes */
1010 pPinImpl->fnSampleProc = pSampleProc;
1011 pPinImpl->fnPreConnect = NULL;
1012 pPinImpl->pAlloc = NULL;
1013 pPinImpl->pReader = NULL;
1014 pPinImpl->hThread = NULL;
1015 pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1017 pPinImpl->rtStart = 0;
1018 pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1020 return S_OK;
1023 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1025 PIN_DIRECTION pindirReceive;
1026 HRESULT hr = S_OK;
1027 ICOM_THIS(PullPin, iface);
1029 TRACE("(%p, %p)\n", pReceivePin, pmt);
1030 dump_AM_MEDIA_TYPE(pmt);
1032 EnterCriticalSection(This->pin.pCritSec);
1034 if (This->pin.pConnectedTo)
1035 hr = VFW_E_ALREADY_CONNECTED;
1037 if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1038 hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
1039 * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1041 if (SUCCEEDED(hr))
1043 IPin_QueryDirection(pReceivePin, &pindirReceive);
1045 if (pindirReceive != PINDIR_OUTPUT)
1047 ERR("Can't connect from non-output pin\n");
1048 hr = VFW_E_INVALID_DIRECTION;
1052 if (SUCCEEDED(hr))
1054 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1057 if (SUCCEEDED(hr))
1059 ALLOCATOR_PROPERTIES props;
1060 props.cBuffers = 3;
1061 props.cbBuffer = 64 * 1024; /* 64k bytes */
1062 props.cbAlign = 1;
1063 props.cbPrefix = 0;
1064 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1067 if (SUCCEEDED(hr) && This->fnPreConnect)
1069 hr = This->fnPreConnect(iface, pReceivePin);
1072 if (SUCCEEDED(hr))
1074 CopyMediaType(&This->pin.mtCurrent, pmt);
1075 This->pin.pConnectedTo = pReceivePin;
1076 IPin_AddRef(pReceivePin);
1079 LeaveCriticalSection(This->pin.pCritSec);
1080 return hr;
1083 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1085 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
1087 *ppv = NULL;
1089 if (IsEqualIID(riid, &IID_IUnknown))
1090 *ppv = (LPVOID)iface;
1091 else if (IsEqualIID(riid, &IID_IPin))
1092 *ppv = (LPVOID)iface;
1094 if (*ppv)
1096 IUnknown_AddRef((IUnknown *)(*ppv));
1097 return S_OK;
1100 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1102 return E_NOINTERFACE;
1105 ULONG WINAPI PullPin_Release(IPin * iface)
1107 ICOM_THIS(PullPin, iface);
1109 TRACE("()\n");
1111 if (!InterlockedDecrement(&This->pin.refCount))
1113 if (This->hThread)
1114 PullPin_StopProcessing(This);
1115 IMemAllocator_Release(This->pAlloc);
1116 IAsyncReader_Release(This->pReader);
1117 CloseHandle(This->hEventStateChanged);
1118 CoTaskMemFree(This);
1119 return 0;
1121 return This->pin.refCount;
1124 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1126 for (;;)
1127 SleepEx(INFINITE, TRUE);
1130 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1132 ICOM_THIS(PullPin, iface);
1133 HRESULT hr;
1135 REFERENCE_TIME rtCurrent;
1136 ALLOCATOR_PROPERTIES allocProps;
1138 SetEvent(This->hEventStateChanged);
1140 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1142 rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1144 while (rtCurrent < This->rtStop)
1146 /* FIXME: to improve performance by quite a bit this should be changed
1147 * so that one sample is processed while one sample is fetched. However,
1148 * it is harder to debug so for the moment it will stay as it is */
1149 IMediaSample * pSample = NULL;
1150 REFERENCE_TIME rtSampleStop;
1151 DWORD dwUser;
1153 hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1155 if (SUCCEEDED(hr))
1157 rtSampleStop = rtCurrent + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1158 if (rtSampleStop > This->rtStop)
1159 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1160 hr = IMediaSample_SetTime(pSample, &rtCurrent, &rtSampleStop);
1161 rtCurrent = rtSampleStop;
1164 if (SUCCEEDED(hr))
1165 hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1167 if (SUCCEEDED(hr))
1168 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1170 if (SUCCEEDED(hr))
1171 hr = This->fnSampleProc(This->pin.pUserData, pSample);
1172 else
1173 ERR("Processing error: %lx\n", hr);
1175 if (pSample)
1176 IMemAllocator_ReleaseBuffer(This->pAlloc, pSample);
1180 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1182 ICOM_THIS(PullPin, iface);
1184 TRACE("()\n");
1186 EnterCriticalSection(This->pin.pCritSec);
1188 HRESULT hr;
1190 CloseHandle(This->hThread);
1191 This->hThread = NULL;
1192 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1193 ERR("Allocator decommit failed with error %lx. Possible memory leak\n", hr);
1195 LeaveCriticalSection(This->pin.pCritSec);
1197 SetEvent(This->hEventStateChanged);
1199 ExitThread(0);
1202 HRESULT PullPin_InitProcessing(PullPin * This)
1204 HRESULT hr = S_OK;
1206 TRACE("()\n");
1208 assert(!This->hThread);
1210 /* if we are connected */
1211 if (This->pAlloc)
1213 EnterCriticalSection(This->pin.pCritSec);
1215 DWORD dwThreadId;
1216 assert(!This->hThread);
1218 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1219 if (!This->hThread)
1220 hr = HRESULT_FROM_WIN32(GetLastError());
1222 if (SUCCEEDED(hr))
1223 hr = IMemAllocator_Commit(This->pAlloc);
1225 LeaveCriticalSection(This->pin.pCritSec);
1228 TRACE(" -- %lx\n", hr);
1230 return hr;
1233 HRESULT PullPin_StartProcessing(PullPin * This)
1235 /* if we are connected */
1236 if(This->pAlloc)
1238 assert(This->hThread);
1240 ResetEvent(This->hEventStateChanged);
1242 if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1243 return HRESULT_FROM_WIN32(GetLastError());
1246 return S_OK;
1249 HRESULT PullPin_PauseProcessing(PullPin * This)
1251 /* make the processing function exit its loop */
1252 This->rtStop = 0;
1254 return S_OK;
1257 HRESULT PullPin_StopProcessing(PullPin * This)
1259 /* if we are connected */
1260 if (This->pAlloc)
1262 assert(This->hThread);
1264 ResetEvent(This->hEventStateChanged);
1266 PullPin_PauseProcessing(This);
1268 if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1269 return HRESULT_FROM_WIN32(GetLastError());
1272 return S_OK;
1275 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1277 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1278 return S_FALSE;
1279 return S_OK;
1282 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1284 FIXME("(%lx%08lx, %lx%08lx)\n", (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1286 PullPin_BeginFlush((IPin *)This);
1287 /* FIXME: need critical section? */
1288 This->rtStart = rtStart;
1289 This->rtStop = rtStop;
1290 PullPin_EndFlush((IPin *)This);
1292 return S_OK;
1295 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1297 FIXME("()\n");
1298 return E_NOTIMPL;
1301 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1303 FIXME("()\n");
1304 return E_NOTIMPL;
1307 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1309 FIXME("()\n");
1310 return E_NOTIMPL;
1313 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1315 FIXME("()\n");
1316 return E_NOTIMPL;
1319 static const IPinVtbl PullPin_Vtbl =
1321 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1322 PullPin_QueryInterface,
1323 IPinImpl_AddRef,
1324 PullPin_Release,
1325 OutputPin_Connect,
1326 PullPin_ReceiveConnection,
1327 IPinImpl_Disconnect,
1328 IPinImpl_ConnectedTo,
1329 IPinImpl_ConnectionMediaType,
1330 IPinImpl_QueryPinInfo,
1331 IPinImpl_QueryDirection,
1332 IPinImpl_QueryId,
1333 IPinImpl_QueryAccept,
1334 IPinImpl_EnumMediaTypes,
1335 IPinImpl_QueryInternalConnections,
1336 PullPin_EndOfStream,
1337 PullPin_BeginFlush,
1338 PullPin_EndFlush,
1339 PullPin_NewSegment