dshow: rename dshow.h to avoid conflict with system header of equal name
[ffmpeg.git] / libavdevice / dshow_pin.c
1 /*
2  * DirectShow capture interface
3  * Copyright (c) 2010 Ramiro Polla
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #define NO_DSHOW_STRSAFE
23 #include "dshow_capture.h"
24
25 #include <stddef.h>
26 #define imemoffset offsetof(libAVPin, imemvtbl)
27
28 DECLARE_QUERYINTERFACE(libAVPin,
29     { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
30 DECLARE_ADDREF(libAVPin)
31 DECLARE_RELEASE(libAVPin)
32
33 long WINAPI
34 libAVPin_Connect(libAVPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
35 {
36     dshowdebug("libAVPin_Connect(%p, %p, %p)\n", this, pin, type);
37     /* Input pins receive connections. */
38     return S_FALSE;
39 }
40 long WINAPI
41 libAVPin_ReceiveConnection(libAVPin *this, IPin *pin,
42                            const AM_MEDIA_TYPE *type)
43 {
44     enum dshowDeviceType devtype = this->filter->type;
45     dshowdebug("libAVPin_ReceiveConnection(%p)\n", this);
46
47     if (!pin)
48         return E_POINTER;
49     if (this->connectedto)
50         return VFW_E_ALREADY_CONNECTED;
51
52     ff_print_AM_MEDIA_TYPE(type);
53     if (devtype == VideoDevice) {
54         if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video))
55             return VFW_E_TYPE_NOT_ACCEPTED;
56     } else {
57         if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
58             return VFW_E_TYPE_NOT_ACCEPTED;
59     }
60
61     IPin_AddRef(pin);
62     this->connectedto = pin;
63
64     ff_copy_dshow_media_type(&this->type, type);
65
66     return S_OK;
67 }
68 long WINAPI
69 libAVPin_Disconnect(libAVPin *this)
70 {
71     dshowdebug("libAVPin_Disconnect(%p)\n", this);
72
73     if (this->filter->state != State_Stopped)
74         return VFW_E_NOT_STOPPED;
75     if (!this->connectedto)
76         return S_FALSE;
77     IPin_Release(this->connectedto);
78     this->connectedto = NULL;
79
80     return S_OK;
81 }
82 long WINAPI
83 libAVPin_ConnectedTo(libAVPin *this, IPin **pin)
84 {
85     dshowdebug("libAVPin_ConnectedTo(%p)\n", this);
86
87     if (!pin)
88         return E_POINTER;
89     if (!this->connectedto)
90         return VFW_E_NOT_CONNECTED;
91     IPin_AddRef(this->connectedto);
92     *pin = this->connectedto;
93
94     return S_OK;
95 }
96 long WINAPI
97 libAVPin_ConnectionMediaType(libAVPin *this, AM_MEDIA_TYPE *type)
98 {
99     dshowdebug("libAVPin_ConnectionMediaType(%p)\n", this);
100
101     if (!type)
102         return E_POINTER;
103     if (!this->connectedto)
104         return VFW_E_NOT_CONNECTED;
105
106     return ff_copy_dshow_media_type(type, &this->type);
107 }
108 long WINAPI
109 libAVPin_QueryPinInfo(libAVPin *this, PIN_INFO *info)
110 {
111     dshowdebug("libAVPin_QueryPinInfo(%p)\n", this);
112
113     if (!info)
114         return E_POINTER;
115
116     if (this->filter)
117         libAVFilter_AddRef(this->filter);
118
119     info->pFilter = (IBaseFilter *) this->filter;
120     info->dir     = PINDIR_INPUT;
121     wcscpy(info->achName, L"Capture");
122
123     return S_OK;
124 }
125 long WINAPI
126 libAVPin_QueryDirection(libAVPin *this, PIN_DIRECTION *dir)
127 {
128     dshowdebug("libAVPin_QueryDirection(%p)\n", this);
129     if (!dir)
130         return E_POINTER;
131     *dir = PINDIR_INPUT;
132     return S_OK;
133 }
134 long WINAPI
135 libAVPin_QueryId(libAVPin *this, wchar_t **id)
136 {
137     dshowdebug("libAVPin_QueryId(%p)\n", this);
138
139     if (!id)
140         return E_POINTER;
141
142     *id = wcsdup(L"libAV Pin");
143
144     return S_OK;
145 }
146 long WINAPI
147 libAVPin_QueryAccept(libAVPin *this, const AM_MEDIA_TYPE *type)
148 {
149     dshowdebug("libAVPin_QueryAccept(%p)\n", this);
150     return S_FALSE;
151 }
152 long WINAPI
153 libAVPin_EnumMediaTypes(libAVPin *this, IEnumMediaTypes **enumtypes)
154 {
155     const AM_MEDIA_TYPE *type = NULL;
156     libAVEnumMediaTypes *new;
157     dshowdebug("libAVPin_EnumMediaTypes(%p)\n", this);
158
159     if (!enumtypes)
160         return E_POINTER;
161     new = libAVEnumMediaTypes_Create(type);
162     if (!new)
163         return E_OUTOFMEMORY;
164
165     *enumtypes = (IEnumMediaTypes *) new;
166     return S_OK;
167 }
168 long WINAPI
169 libAVPin_QueryInternalConnections(libAVPin *this, IPin **pin,
170                                   unsigned long *npin)
171 {
172     dshowdebug("libAVPin_QueryInternalConnections(%p)\n", this);
173     return E_NOTIMPL;
174 }
175 long WINAPI
176 libAVPin_EndOfStream(libAVPin *this)
177 {
178     dshowdebug("libAVPin_EndOfStream(%p)\n", this);
179     /* I don't care. */
180     return S_OK;
181 }
182 long WINAPI
183 libAVPin_BeginFlush(libAVPin *this)
184 {
185     dshowdebug("libAVPin_BeginFlush(%p)\n", this);
186     /* I don't care. */
187     return S_OK;
188 }
189 long WINAPI
190 libAVPin_EndFlush(libAVPin *this)
191 {
192     dshowdebug("libAVPin_EndFlush(%p)\n", this);
193     /* I don't care. */
194     return S_OK;
195 }
196 long WINAPI
197 libAVPin_NewSegment(libAVPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
198                     double rate)
199 {
200     dshowdebug("libAVPin_NewSegment(%p)\n", this);
201     /* I don't care. */
202     return S_OK;
203 }
204
205 static int
206 libAVPin_Setup(libAVPin *this, libAVFilter *filter)
207 {
208     IPinVtbl *vtbl = this->vtbl;
209     IMemInputPinVtbl *imemvtbl;
210
211     if (!filter)
212         return 0;
213
214     imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
215     if (!imemvtbl)
216         return 0;
217
218     SETVTBL(imemvtbl, libAVMemInputPin, QueryInterface);
219     SETVTBL(imemvtbl, libAVMemInputPin, AddRef);
220     SETVTBL(imemvtbl, libAVMemInputPin, Release);
221     SETVTBL(imemvtbl, libAVMemInputPin, GetAllocator);
222     SETVTBL(imemvtbl, libAVMemInputPin, NotifyAllocator);
223     SETVTBL(imemvtbl, libAVMemInputPin, GetAllocatorRequirements);
224     SETVTBL(imemvtbl, libAVMemInputPin, Receive);
225     SETVTBL(imemvtbl, libAVMemInputPin, ReceiveMultiple);
226     SETVTBL(imemvtbl, libAVMemInputPin, ReceiveCanBlock);
227
228     this->imemvtbl = imemvtbl;
229
230     SETVTBL(vtbl, libAVPin, QueryInterface);
231     SETVTBL(vtbl, libAVPin, AddRef);
232     SETVTBL(vtbl, libAVPin, Release);
233     SETVTBL(vtbl, libAVPin, Connect);
234     SETVTBL(vtbl, libAVPin, ReceiveConnection);
235     SETVTBL(vtbl, libAVPin, Disconnect);
236     SETVTBL(vtbl, libAVPin, ConnectedTo);
237     SETVTBL(vtbl, libAVPin, ConnectionMediaType);
238     SETVTBL(vtbl, libAVPin, QueryPinInfo);
239     SETVTBL(vtbl, libAVPin, QueryDirection);
240     SETVTBL(vtbl, libAVPin, QueryId);
241     SETVTBL(vtbl, libAVPin, QueryAccept);
242     SETVTBL(vtbl, libAVPin, EnumMediaTypes);
243     SETVTBL(vtbl, libAVPin, QueryInternalConnections);
244     SETVTBL(vtbl, libAVPin, EndOfStream);
245     SETVTBL(vtbl, libAVPin, BeginFlush);
246     SETVTBL(vtbl, libAVPin, EndFlush);
247     SETVTBL(vtbl, libAVPin, NewSegment);
248
249     this->filter = filter;
250
251     return 1;
252 }
253 DECLARE_CREATE(libAVPin, libAVPin_Setup(this, filter), libAVFilter *filter)
254 DECLARE_DESTROY(libAVPin, nothing)
255
256 /*****************************************************************************
257  * libAVMemInputPin
258  ****************************************************************************/
259 long WINAPI
260 libAVMemInputPin_QueryInterface(libAVMemInputPin *this, const GUID *riid,
261                                 void **ppvObject)
262 {
263     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
264     dshowdebug("libAVMemInputPin_QueryInterface(%p)\n", this);
265     return libAVPin_QueryInterface(pin, riid, ppvObject);
266 }
267 unsigned long WINAPI
268 libAVMemInputPin_AddRef(libAVMemInputPin *this)
269 {
270     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
271     dshowdebug("libAVMemInputPin_AddRef(%p)\n", this);
272     return libAVPin_AddRef(pin);
273 }
274 unsigned long WINAPI
275 libAVMemInputPin_Release(libAVMemInputPin *this)
276 {
277     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
278     dshowdebug("libAVMemInputPin_Release(%p)\n", this);
279     return libAVPin_Release(pin);
280 }
281 long WINAPI
282 libAVMemInputPin_GetAllocator(libAVMemInputPin *this, IMemAllocator **alloc)
283 {
284     dshowdebug("libAVMemInputPin_GetAllocator(%p)\n", this);
285     return VFW_E_NO_ALLOCATOR;
286 }
287 long WINAPI
288 libAVMemInputPin_NotifyAllocator(libAVMemInputPin *this, IMemAllocator *alloc,
289                                  WINBOOL rdwr)
290 {
291     dshowdebug("libAVMemInputPin_NotifyAllocator(%p)\n", this);
292     return S_OK;
293 }
294 long WINAPI
295 libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *this,
296                                           ALLOCATOR_PROPERTIES *props)
297 {
298     dshowdebug("libAVMemInputPin_GetAllocatorRequirements(%p)\n", this);
299     return E_NOTIMPL;
300 }
301 long WINAPI
302 libAVMemInputPin_Receive(libAVMemInputPin *this, IMediaSample *sample)
303 {
304     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
305     enum dshowDeviceType devtype = pin->filter->type;
306     void *priv_data;
307     uint8_t *buf;
308     int buf_size;
309     int index;
310     int64_t curtime;
311
312     dshowdebug("libAVMemInputPin_Receive(%p)\n", this);
313
314     if (!sample)
315         return E_POINTER;
316
317     if (devtype == VideoDevice) {
318         /* PTS from video devices is unreliable. */
319         IReferenceClock *clock = pin->filter->clock;
320         IReferenceClock_GetTime(clock, &curtime);
321     } else {
322         int64_t dummy;
323         IMediaSample_GetTime(sample, &curtime, &dummy);
324         curtime += pin->filter->start_time;
325     }
326
327     buf_size = IMediaSample_GetActualDataLength(sample);
328     IMediaSample_GetPointer(sample, &buf);
329     priv_data = pin->filter->priv_data;
330     index = pin->filter->stream_index;
331
332     pin->filter->callback(priv_data, index, buf, buf_size, curtime);
333
334     return S_OK;
335 }
336 long WINAPI
337 libAVMemInputPin_ReceiveMultiple(libAVMemInputPin *this,
338                                  IMediaSample **samples, long n, long *nproc)
339 {
340     int i;
341     dshowdebug("libAVMemInputPin_ReceiveMultiple(%p)\n", this);
342
343     for (i = 0; i < n; i++)
344         libAVMemInputPin_Receive(this, samples[i]);
345
346     *nproc = n;
347     return S_OK;
348 }
349 long WINAPI
350 libAVMemInputPin_ReceiveCanBlock(libAVMemInputPin *this)
351 {
352     dshowdebug("libAVMemInputPin_ReceiveCanBlock(%p)\n", this);
353     /* I swear I will not block. */
354     return S_FALSE;
355 }
356
357 void
358 libAVMemInputPin_Destroy(libAVMemInputPin *this)
359 {
360     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
361     dshowdebug("libAVMemInputPin_Destroy(%p)\n", this);
362     return libAVPin_Destroy(pin);
363 }