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