strmbase: Move BasePin implementation to strmbase.
[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
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "uuids.h"
37 #include "vfwmsgs.h"
38 #include <assert.h>
39 #include "pin.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 /* 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)
61 {
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? */
66
67     TRACE("(%p, %p)\n", pReceivePin, pmt);
68     dump_AM_MEDIA_TYPE(pmt);
69
70     /* FIXME: call queryacceptproc */
71     
72     This->pin.pConnectedTo = pReceivePin;
73     IPin_AddRef(pReceivePin);
74     CopyMediaType(&This->pin.mtCurrent, pmt);
75
76     hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
77
78     /* get the IMemInputPin interface we will use to deliver samples to the
79      * connected pin */
80     if (SUCCEEDED(hr))
81     {
82         hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
83
84         if (SUCCEEDED(hr))
85             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
86
87         if (hr == VFW_E_NO_ALLOCATOR)
88         {
89             /* Input pin provides no allocator, use standard memory allocator */
90             hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
91
92             if (SUCCEEDED(hr))
93             {
94                 hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
95             }
96         }
97
98         if (SUCCEEDED(hr))
99             hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
100
101         if (pMemAlloc)
102             IMemAllocator_Release(pMemAlloc);
103
104         /* break connection if we couldn't get the allocator */
105         if (FAILED(hr))
106             IPin_Disconnect(pReceivePin);
107     }
108
109     if (FAILED(hr))
110     {
111         IPin_Release(This->pin.pConnectedTo);
112         This->pin.pConnectedTo = NULL;
113         DeleteMediaType(&This->pin.mtCurrent);
114     }
115
116     TRACE(" -- %x\n", hr);
117     return hr;
118 }
119
120 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props,
121                        LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
122 {
123     TRACE("\n");
124
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);
130
131     /* Output pin attributes */
132     pPinImpl->pMemInputPin = NULL;
133     pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
134     if (props)
135     {
136         pPinImpl->allocProps = *props;
137         if (pPinImpl->allocProps.cbAlign == 0)
138             pPinImpl->allocProps.cbAlign = 1;
139     }
140     else
141         ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
142
143     return S_OK;
144 }
145
146 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
147 {
148     HRESULT hr;
149     OutputPin *This = (OutputPin *)iface;
150
151     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
152     dump_AM_MEDIA_TYPE(pmt);
153
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);
158
159     EnterCriticalSection(This->pin.pCritSec);
160     {
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);
165         else
166         {
167             /* negotiate media type */
168
169             IEnumMediaTypes * pEnumCandidates;
170             AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
171
172             if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
173             {
174                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
175
176                 /* try this filter's media types first */
177                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
178                 {
179                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
180                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
181                     {
182                         hr = S_OK;
183                         TRACE("o_o\n");
184                         DeleteMediaType(pmtCandidate);
185                         break;
186                     }
187                     DeleteMediaType(pmtCandidate);
188                 }
189                 IEnumMediaTypes_Release(pEnumCandidates);
190             }
191
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 */
194             {
195                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
196
197                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
198                 {
199                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
200                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
201                     {
202                         hr = S_OK;
203                         DeleteMediaType(pmtCandidate);
204                         break;
205                     }
206                     DeleteMediaType(pmtCandidate);
207                 } /* while */
208                 IEnumMediaTypes_Release(pEnumCandidates);
209             } /* if not found */
210         } /* if negotiate media type */
211     } /* if succeeded */
212     LeaveCriticalSection(This->pin.pCritSec);
213
214     TRACE(" -- %x\n", hr);
215     return hr;
216 }
217
218 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
219 {
220     ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
221
222     return E_UNEXPECTED;
223 }
224
225 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
226 {
227     HRESULT hr;
228     OutputPin *This = (OutputPin *)iface;
229
230     TRACE("()\n");
231
232     EnterCriticalSection(This->pin.pCritSec);
233     {
234         if (This->pMemInputPin)
235         {
236             IMemInputPin_Release(This->pMemInputPin);
237             This->pMemInputPin = NULL;
238         }
239         if (This->pin.pConnectedTo)
240         {
241             IPin_Release(This->pin.pConnectedTo);
242             This->pin.pConnectedTo = NULL;
243             hr = S_OK;
244         }
245         else
246             hr = S_FALSE;
247     }
248     LeaveCriticalSection(This->pin.pCritSec);
249
250     return hr;
251 }
252
253 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
254 {
255     HRESULT hr;
256
257     TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
258
259     EnterCriticalSection(This->pin.pCritSec);
260     {
261         if (!This->pin.pConnectedTo)
262             hr = VFW_E_NOT_CONNECTED;
263         else
264         {
265             IMemAllocator * pAlloc = NULL;
266
267             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
268
269             if (SUCCEEDED(hr))
270                 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
271
272             if (SUCCEEDED(hr))
273                 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
274
275             if (pAlloc)
276                 IMemAllocator_Release(pAlloc);
277         }
278     }
279     LeaveCriticalSection(This->pin.pCritSec);
280
281     return hr;
282 }
283
284 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
285 {
286     HRESULT hr = S_OK;
287     IMemInputPin * pMemConnected = NULL;
288
289     EnterCriticalSection(This->pin.pCritSec);
290     {
291         if (!This->pin.pConnectedTo || !This->pMemInputPin)
292             hr = VFW_E_NOT_CONNECTED;
293         else
294         {
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
297              * using it. */
298             pMemConnected = This->pMemInputPin;
299             IMemInputPin_AddRef(pMemConnected);
300         }
301     }
302     LeaveCriticalSection(This->pin.pCritSec);
303
304     if (SUCCEEDED(hr))
305     {
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);
311     }
312
313     return hr;
314 }