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
32 #include "qcap_main.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
43 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
44 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
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;
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
82 hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
85 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
87 if (hr == VFW_E_NO_ALLOCATOR)
89 /* Input pin provides no allocator, use standard memory allocator */
90 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
94 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
99 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
102 IMemAllocator_Release(pMemAlloc);
104 /* break connection if we couldn't get the allocator */
106 IPin_Disconnect(pReceivePin);
111 IPin_Release(This->pin.pConnectedTo);
112 This->pin.pConnectedTo = NULL;
113 DeleteMediaType(&This->pin.mtCurrent);
116 TRACE(" -- %x\n", hr);
120 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props,
121 LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
125 /* Common attributes */
126 pPinImpl->pin.refCount = 1;
127 pPinImpl->pin.pConnectedTo = NULL;
128 pPinImpl->pin.pCritSec = pCritSec;
129 Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
131 /* Output pin attributes */
132 pPinImpl->pMemInputPin = NULL;
133 pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
136 pPinImpl->allocProps = *props;
137 if (pPinImpl->allocProps.cbAlign == 0)
138 pPinImpl->allocProps.cbAlign = 1;
141 ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
146 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
149 OutputPin *This = (OutputPin *)iface;
151 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
152 dump_AM_MEDIA_TYPE(pmt);
154 /* If we try to connect to ourself, we will definitely deadlock.
155 * There are other cases where we could deadlock too, but this
156 * catches the obvious case */
157 assert(pReceivePin != iface);
159 EnterCriticalSection(This->pin.pCritSec);
161 /* if we have been a specific type to connect with, then we can either connect
162 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
163 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
164 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
167 /* negotiate media type */
169 IEnumMediaTypes * pEnumCandidates;
170 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
172 if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
174 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
176 /* try this filter's media types first */
177 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
179 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
180 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
184 DeleteMediaType(pmtCandidate);
187 DeleteMediaType(pmtCandidate);
189 IEnumMediaTypes_Release(pEnumCandidates);
192 /* then try receiver filter's media types */
193 if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
195 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
197 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
199 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
200 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
203 DeleteMediaType(pmtCandidate);
206 DeleteMediaType(pmtCandidate);
208 IEnumMediaTypes_Release(pEnumCandidates);
210 } /* if negotiate media type */
212 LeaveCriticalSection(This->pin.pCritSec);
214 TRACE(" -- %x\n", hr);
218 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
220 ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
225 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
228 OutputPin *This = (OutputPin *)iface;
232 EnterCriticalSection(This->pin.pCritSec);
234 if (This->pMemInputPin)
236 IMemInputPin_Release(This->pMemInputPin);
237 This->pMemInputPin = NULL;
239 if (This->pin.pConnectedTo)
241 IPin_Release(This->pin.pConnectedTo);
242 This->pin.pConnectedTo = NULL;
248 LeaveCriticalSection(This->pin.pCritSec);
253 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
257 TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
259 EnterCriticalSection(This->pin.pCritSec);
261 if (!This->pin.pConnectedTo)
262 hr = VFW_E_NOT_CONNECTED;
265 IMemAllocator * pAlloc = NULL;
267 hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
270 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
273 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
276 IMemAllocator_Release(pAlloc);
279 LeaveCriticalSection(This->pin.pCritSec);
284 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
287 IMemInputPin * pMemConnected = NULL;
289 EnterCriticalSection(This->pin.pCritSec);
291 if (!This->pin.pConnectedTo || !This->pMemInputPin)
292 hr = VFW_E_NOT_CONNECTED;
295 /* we don't have the lock held when using This->pMemInputPin,
296 * so we need to AddRef it to stop it being deleted while we are
298 pMemConnected = This->pMemInputPin;
299 IMemInputPin_AddRef(pMemConnected);
302 LeaveCriticalSection(This->pin.pCritSec);
306 /* NOTE: if we are in a critical section when Receive is called
307 * then it causes some problems (most notably with the native Video
308 * Renderer) if we are re-entered for whatever reason */
309 hr = IMemInputPin_Receive(pMemConnected, pSample);
310 IMemInputPin_Release(pMemConnected);