qcap: Constify some variables.
[wine] / dlls / qcap / pin.c
1 /*
2  * Generic Implementation of IPin Interface
3  *
4  * Copyright 2003 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wtypes.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "dshow.h"
31
32 #include "qcap_main.h"
33 #include "pin.h"
34
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37 #include "uuids.h"
38 #include "vfwmsgs.h"
39 #include <assert.h>
40
41 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
42
43 #define ALIGNDOWN(value,boundary) ((value) & ~(boundary-1))
44 #define ALIGNUP(value,boundary) (ALIGNDOWN(value - 1, boundary) + boundary)
45
46 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
47 {
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;
55 }
56
57 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
58 {
59     HRESULT hr;
60     IPinImpl *This = (IPinImpl *)iface;
61
62 /*  TRACE("(%p)\n", ppPin);*/
63
64     EnterCriticalSection(This->pCritSec);
65     {
66         if (This->pConnectedTo)
67         {
68             *ppPin = This->pConnectedTo;
69             IPin_AddRef(*ppPin);
70             hr = S_OK;
71         }
72         else
73             hr = VFW_E_NOT_CONNECTED;
74     }
75     LeaveCriticalSection(This->pCritSec);
76
77     return hr;
78 }
79
80 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
81 {
82     HRESULT hr;
83     IPinImpl *This = (IPinImpl *)iface;
84
85     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
86
87     EnterCriticalSection(This->pCritSec);
88     {
89         if (This->pConnectedTo)
90         {
91             CopyMediaType(pmt, &This->mtCurrent);
92             hr = S_OK;
93         }
94         else
95         {
96             ZeroMemory(pmt, sizeof(*pmt));
97             hr = VFW_E_NOT_CONNECTED;
98         }
99     }
100     LeaveCriticalSection(This->pCritSec);
101
102     return hr;
103 }
104
105 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
106 {
107     IPinImpl *This = (IPinImpl *)iface;
108
109     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
110
111     Copy_PinInfo(pInfo, &This->pinInfo);
112     IBaseFilter_AddRef(pInfo->pFilter);
113
114     return S_OK;
115 }
116
117 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
118 {
119     IPinImpl *This = (IPinImpl *)iface;
120
121     TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
122
123     *pPinDir = This->pinInfo.dir;
124
125     return S_OK;
126 }
127
128 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
129 {
130     IPinImpl *This = (IPinImpl *)iface;
131
132     TRACE("(%p/%p)->(%p)\n", This, iface, Id);
133
134     *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
135     if (!*Id)
136         return E_OUTOFMEMORY;
137
138     strcpyW(*Id, This->pinInfo.achName);
139
140     return S_OK;
141 }
142
143 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
144 {
145     IPinImpl *This = (IPinImpl *)iface;
146
147     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
148
149     if (!This->fnQueryAccept) return S_OK;
150
151     return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
152 }
153
154 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
155 {
156     IPinImpl *This = (IPinImpl *)iface;
157
158     TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
159
160     return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
161 }
162
163 /* Function called as a helper to IPin_Connect */
164 /* specific AM_MEDIA_TYPE - it cannot be NULL */
165 /* NOTE: not part of standard interface */
166 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
167 {
168     OutputPin *This = (OutputPin *)iface;
169     HRESULT hr;
170     IMemAllocator * pMemAlloc = NULL;
171     ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
172
173     TRACE("(%p, %p)\n", pReceivePin, pmt);
174     dump_AM_MEDIA_TYPE(pmt);
175
176     /* FIXME: call queryacceptproc */
177     
178     This->pin.pConnectedTo = pReceivePin;
179     IPin_AddRef(pReceivePin);
180     CopyMediaType(&This->pin.mtCurrent, pmt);
181
182     hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
183
184     /* get the IMemInputPin interface we will use to deliver samples to the
185      * connected pin */
186     if (SUCCEEDED(hr))
187     {
188         hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
189
190         if (SUCCEEDED(hr))
191             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
192
193         if (hr == VFW_E_NO_ALLOCATOR)
194         {
195             /* Input pin provides no allocator, use standard memory allocator */
196             hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
197
198             if (SUCCEEDED(hr))
199             {
200                 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
201             }
202         }
203
204         if (SUCCEEDED(hr))
205             hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
206
207         if (pMemAlloc)
208             IMemAllocator_Release(pMemAlloc);
209
210         /* break connection if we couldn't get the allocator */
211         if (FAILED(hr))
212             IPin_Disconnect(pReceivePin);
213     }
214
215     if (FAILED(hr))
216     {
217         IPin_Release(This->pin.pConnectedTo);
218         This->pin.pConnectedTo = NULL;
219         DeleteMediaType(&This->pin.mtCurrent);
220     }
221
222     TRACE(" -- %x\n", hr);
223     return hr;
224 }
225
226 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props,
227                        LPVOID pUserData, QUERYACCEPTPROC pQueryAccept,
228                        LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
229 {
230     TRACE("\n");
231
232     /* Common attributes */
233     pPinImpl->pin.refCount = 1;
234     pPinImpl->pin.pConnectedTo = NULL;
235     pPinImpl->pin.fnQueryAccept = pQueryAccept;
236     pPinImpl->pin.pUserData = pUserData;
237     pPinImpl->pin.pCritSec = pCritSec;
238     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
239
240     /* Output pin attributes */
241     pPinImpl->pMemInputPin = NULL;
242     pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
243     if (props)
244     {
245         memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
246         if (pPinImpl->allocProps.cbAlign == 0)
247             pPinImpl->allocProps.cbAlign = 1;
248     }
249     else
250         ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
251
252     return S_OK;
253 }
254
255 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
256 {
257     HRESULT hr;
258     OutputPin *This = (OutputPin *)iface;
259
260     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
261     dump_AM_MEDIA_TYPE(pmt);
262
263     /* If we try to connect to ourself, we will definitely deadlock.
264      * There are other cases where we could deadlock too, but this
265      * catches the obvious case */
266     assert(pReceivePin != iface);
267
268     EnterCriticalSection(This->pin.pCritSec);
269     {
270         /* if we have been a specific type to connect with, then we can either connect
271          * with that or fail. We cannot choose different AM_MEDIA_TYPE */
272         if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
273             hr = This->pConnectSpecific(iface, pReceivePin, pmt);
274         else
275         {
276             /* negotiate media type */
277
278             IEnumMediaTypes * pEnumCandidates;
279             AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
280
281             if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
282             {
283                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
284
285                 /* try this filter's media types first */
286                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
287                 {
288                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
289                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
290                     {
291                         hr = S_OK;
292                         TRACE("o_o\n");
293                         DeleteMediaType(pmtCandidate);
294                         break;
295                     }
296                     DeleteMediaType(pmtCandidate);
297                 }
298                 IEnumMediaTypes_Release(pEnumCandidates);
299             }
300
301             /* then try receiver filter's media types */
302             if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
303             {
304                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
305
306                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
307                 {
308                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
309                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
310                     {
311                         hr = S_OK;
312                         DeleteMediaType(pmtCandidate);
313                         break;
314                     }
315                     DeleteMediaType(pmtCandidate);
316                 } /* while */
317                 IEnumMediaTypes_Release(pEnumCandidates);
318             } /* if not found */
319         } /* if negotiate media type */
320     } /* if succeeded */
321     LeaveCriticalSection(This->pin.pCritSec);
322
323     TRACE(" -- %x\n", hr);
324     return hr;
325 }
326
327 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
328 {
329     ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
330
331     return E_UNEXPECTED;
332 }
333
334 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
335 {
336     HRESULT hr;
337     OutputPin *This = (OutputPin *)iface;
338
339     TRACE("()\n");
340
341     EnterCriticalSection(This->pin.pCritSec);
342     {
343         if (This->pMemInputPin)
344         {
345             IMemInputPin_Release(This->pMemInputPin);
346             This->pMemInputPin = NULL;
347         }
348         if (This->pin.pConnectedTo)
349         {
350             IPin_Release(This->pin.pConnectedTo);
351             This->pin.pConnectedTo = NULL;
352             hr = S_OK;
353         }
354         else
355             hr = S_FALSE;
356     }
357     LeaveCriticalSection(This->pin.pCritSec);
358
359     return hr;
360 }
361
362 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
363 {
364     HRESULT hr;
365
366     TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
367
368     EnterCriticalSection(This->pin.pCritSec);
369     {
370         if (!This->pin.pConnectedTo)
371             hr = VFW_E_NOT_CONNECTED;
372         else
373         {
374             IMemAllocator * pAlloc = NULL;
375
376             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
377
378             if (SUCCEEDED(hr))
379                 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
380
381             if (SUCCEEDED(hr))
382                 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
383
384             if (pAlloc)
385                 IMemAllocator_Release(pAlloc);
386         }
387     }
388     LeaveCriticalSection(This->pin.pCritSec);
389
390     return hr;
391 }
392
393 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
394 {
395     HRESULT hr = S_OK;
396     IMemInputPin * pMemConnected = NULL;
397
398     EnterCriticalSection(This->pin.pCritSec);
399     {
400         if (!This->pin.pConnectedTo || !This->pMemInputPin)
401             hr = VFW_E_NOT_CONNECTED;
402         else
403         {
404             /* we don't have the lock held when using This->pMemInputPin,
405              * so we need to AddRef it to stop it being deleted while we are
406              * using it. */
407             pMemConnected = This->pMemInputPin;
408             IMemInputPin_AddRef(pMemConnected);
409         }
410     }
411     LeaveCriticalSection(This->pin.pCritSec);
412
413     if (SUCCEEDED(hr))
414     {
415         /* NOTE: if we are in a critical section when Receive is called
416          * then it causes some problems (most notably with the native Video
417          * Renderer) if we are re-entered for whatever reason */
418         hr = IMemInputPin_Receive(pMemConnected, pSample);
419         IMemInputPin_Release(pMemConnected);
420     }
421
422     return hr;
423 }