2 * DirectShow capture interface
3 * Copyright (c) 2010 Ramiro Polla
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "dshow_capture.h"
25 #define imemoffset offsetof(DShowPin, imemvtbl)
27 DECLARE_QUERYINTERFACE(pin, DShowPin,
28 { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
29 DECLARE_ADDREF(pin, DShowPin)
30 DECLARE_RELEASE(pin, DShowPin)
32 long ff_dshow_pin_Connect(DShowPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
34 dshowdebug("ff_dshow_pin_Connect(%p, %p, %p)\n", this, pin, type);
35 /* Input pins receive connections. */
38 long ff_dshow_pin_ReceiveConnection(DShowPin *this, IPin *pin,
39 const AM_MEDIA_TYPE *type)
41 enum dshowDeviceType devtype = this->filter->type;
42 dshowdebug("ff_dshow_pin_ReceiveConnection(%p)\n", this);
46 if (this->connectedto)
47 return VFW_E_ALREADY_CONNECTED;
49 ff_print_AM_MEDIA_TYPE(type);
50 if (devtype == VideoDevice) {
51 if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video))
52 return VFW_E_TYPE_NOT_ACCEPTED;
54 if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
55 return VFW_E_TYPE_NOT_ACCEPTED;
59 this->connectedto = pin;
61 ff_copy_dshow_media_type(&this->type, type);
65 long ff_dshow_pin_Disconnect(DShowPin *this)
67 dshowdebug("ff_dshow_pin_Disconnect(%p)\n", this);
69 if (this->filter->state != State_Stopped)
70 return VFW_E_NOT_STOPPED;
71 if (!this->connectedto)
73 IPin_Release(this->connectedto);
74 this->connectedto = NULL;
78 long ff_dshow_pin_ConnectedTo(DShowPin *this, IPin **pin)
80 dshowdebug("ff_dshow_pin_ConnectedTo(%p)\n", this);
84 if (!this->connectedto)
85 return VFW_E_NOT_CONNECTED;
86 IPin_AddRef(this->connectedto);
87 *pin = this->connectedto;
91 long ff_dshow_pin_ConnectionMediaType(DShowPin *this, AM_MEDIA_TYPE *type)
93 dshowdebug("ff_dshow_pin_ConnectionMediaType(%p)\n", this);
97 if (!this->connectedto)
98 return VFW_E_NOT_CONNECTED;
100 return ff_copy_dshow_media_type(type, &this->type);
102 long ff_dshow_pin_QueryPinInfo(DShowPin *this, PIN_INFO *info)
104 dshowdebug("ff_dshow_pin_QueryPinInfo(%p)\n", this);
110 ff_dshow_filter_AddRef(this->filter);
112 info->pFilter = (IBaseFilter *) this->filter;
113 info->dir = PINDIR_INPUT;
114 wcscpy(info->achName, L"Capture");
118 long ff_dshow_pin_QueryDirection(DShowPin *this, PIN_DIRECTION *dir)
120 dshowdebug("ff_dshow_pin_QueryDirection(%p)\n", this);
126 long ff_dshow_pin_QueryId(DShowPin *this, wchar_t **id)
128 dshowdebug("ff_dshow_pin_QueryId(%p)\n", this);
133 *id = wcsdup(L"libAV Pin");
137 long ff_dshow_pin_QueryAccept(DShowPin *this, const AM_MEDIA_TYPE *type)
139 dshowdebug("ff_dshow_pin_QueryAccept(%p)\n", this);
142 long ff_dshow_pin_EnumMediaTypes(DShowPin *this, IEnumMediaTypes **enumtypes)
144 const AM_MEDIA_TYPE *type = NULL;
145 DShowEnumMediaTypes *new;
146 dshowdebug("ff_dshow_pin_EnumMediaTypes(%p)\n", this);
150 new = ff_dshow_enummediatypes_Create(type);
152 return E_OUTOFMEMORY;
154 *enumtypes = (IEnumMediaTypes *) new;
157 long ff_dshow_pin_QueryInternalConnections(DShowPin *this, IPin **pin,
160 dshowdebug("ff_dshow_pin_QueryInternalConnections(%p)\n", this);
163 long ff_dshow_pin_EndOfStream(DShowPin *this)
165 dshowdebug("ff_dshow_pin_EndOfStream(%p)\n", this);
169 long ff_dshow_pin_BeginFlush(DShowPin *this)
171 dshowdebug("ff_dshow_pin_BeginFlush(%p)\n", this);
175 long ff_dshow_pin_EndFlush(DShowPin *this)
177 dshowdebug("ff_dshow_pin_EndFlush(%p)\n", this);
181 long ff_dshow_pin_NewSegment(DShowPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
184 dshowdebug("ff_dshow_pin_NewSegment(%p)\n", this);
189 static int ff_dshow_pin_Setup(DShowPin *this, DShowFilter *filter)
191 IPinVtbl *vtbl = this->vtbl;
192 IMemInputPinVtbl *imemvtbl;
197 imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
201 SETVTBL(imemvtbl, meminputpin, QueryInterface);
202 SETVTBL(imemvtbl, meminputpin, AddRef);
203 SETVTBL(imemvtbl, meminputpin, Release);
204 SETVTBL(imemvtbl, meminputpin, GetAllocator);
205 SETVTBL(imemvtbl, meminputpin, NotifyAllocator);
206 SETVTBL(imemvtbl, meminputpin, GetAllocatorRequirements);
207 SETVTBL(imemvtbl, meminputpin, Receive);
208 SETVTBL(imemvtbl, meminputpin, ReceiveMultiple);
209 SETVTBL(imemvtbl, meminputpin, ReceiveCanBlock);
211 this->imemvtbl = imemvtbl;
213 SETVTBL(vtbl, pin, QueryInterface);
214 SETVTBL(vtbl, pin, AddRef);
215 SETVTBL(vtbl, pin, Release);
216 SETVTBL(vtbl, pin, Connect);
217 SETVTBL(vtbl, pin, ReceiveConnection);
218 SETVTBL(vtbl, pin, Disconnect);
219 SETVTBL(vtbl, pin, ConnectedTo);
220 SETVTBL(vtbl, pin, ConnectionMediaType);
221 SETVTBL(vtbl, pin, QueryPinInfo);
222 SETVTBL(vtbl, pin, QueryDirection);
223 SETVTBL(vtbl, pin, QueryId);
224 SETVTBL(vtbl, pin, QueryAccept);
225 SETVTBL(vtbl, pin, EnumMediaTypes);
226 SETVTBL(vtbl, pin, QueryInternalConnections);
227 SETVTBL(vtbl, pin, EndOfStream);
228 SETVTBL(vtbl, pin, BeginFlush);
229 SETVTBL(vtbl, pin, EndFlush);
230 SETVTBL(vtbl, pin, NewSegment);
232 this->filter = filter;
237 static void ff_dshow_pin_Free(DShowPin *this)
241 av_freep(&this->imemvtbl);
242 if (this->type.pbFormat) {
243 CoTaskMemFree(this->type.pbFormat);
244 this->type.pbFormat = NULL;
247 DECLARE_CREATE(pin, DShowPin, ff_dshow_pin_Setup(this, filter), DShowFilter *filter)
248 DECLARE_DESTROY(pin, DShowPin, ff_dshow_pin_Free)
250 /*****************************************************************************
252 ****************************************************************************/
253 long ff_dshow_meminputpin_QueryInterface(DShowMemInputPin *this, const GUID *riid,
256 DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
257 dshowdebug("ff_dshow_meminputpin_QueryInterface(%p)\n", this);
258 return ff_dshow_pin_QueryInterface(pin, riid, ppvObject);
260 unsigned long ff_dshow_meminputpin_AddRef(DShowMemInputPin *this)
262 DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
263 dshowdebug("ff_dshow_meminputpin_AddRef(%p)\n", this);
264 return ff_dshow_pin_AddRef(pin);
266 unsigned long ff_dshow_meminputpin_Release(DShowMemInputPin *this)
268 DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
269 dshowdebug("ff_dshow_meminputpin_Release(%p)\n", this);
270 return ff_dshow_pin_Release(pin);
272 long ff_dshow_meminputpin_GetAllocator(DShowMemInputPin *this, IMemAllocator **alloc)
274 dshowdebug("ff_dshow_meminputpin_GetAllocator(%p)\n", this);
275 return VFW_E_NO_ALLOCATOR;
277 long ff_dshow_meminputpin_NotifyAllocator(DShowMemInputPin *this, IMemAllocator *alloc,
280 dshowdebug("ff_dshow_meminputpin_NotifyAllocator(%p)\n", this);
283 long ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *this,
284 ALLOCATOR_PROPERTIES *props)
286 dshowdebug("ff_dshow_meminputpin_GetAllocatorRequirements(%p)\n", this);
289 long ff_dshow_meminputpin_Receive(DShowMemInputPin *this, IMediaSample *sample)
291 DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
292 enum dshowDeviceType devtype = pin->filter->type;
296 int buf_size; /* todo should be a long? */
299 int64_t orig_curtime;
301 const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
302 IReferenceClock *clock = pin->filter->clock;
304 struct dshow_ctx *ctx;
307 dshowdebug("ff_dshow_meminputpin_Receive(%p)\n", this);
312 IMediaSample_GetTime(sample, &orig_curtime, &dummy);
313 orig_curtime += pin->filter->start_time;
314 IReferenceClock_GetTime(clock, &graphtime);
315 if (devtype == VideoDevice) {
316 /* PTS from video devices is unreliable. */
317 IReferenceClock_GetTime(clock, &curtime);
319 IMediaSample_GetTime(sample, &curtime, &dummy);
320 if(curtime > 400000000000000000LL) {
321 /* initial frames sometimes start < 0 (shown as a very large number here,
322 like 437650244077016960 which FFmpeg doesn't like.
323 TODO figure out math. For now just drop them. */
324 av_log(NULL, AV_LOG_DEBUG,
325 "dshow dropping initial (or ending) audio frame with odd PTS too high %"PRId64"\n", curtime);
328 curtime += pin->filter->start_time;
331 buf_size = IMediaSample_GetActualDataLength(sample);
332 IMediaSample_GetPointer(sample, &buf);
333 priv_data = pin->filter->priv_data;
336 index = pin->filter->stream_index;
338 av_log(NULL, AV_LOG_VERBOSE, "dshow passing through packet of type %s size %8d "
339 "timestamp %"PRId64" orig timestamp %"PRId64" graph timestamp %"PRId64" diff %"PRId64" %s\n",
340 devtypename, buf_size, curtime, orig_curtime, graphtime, graphtime - orig_curtime, ctx->device_name[devtype]);
341 pin->filter->callback(priv_data, index, buf, buf_size, curtime, devtype);
345 long ff_dshow_meminputpin_ReceiveMultiple(DShowMemInputPin *this,
346 IMediaSample **samples, long n, long *nproc)
349 dshowdebug("ff_dshow_meminputpin_ReceiveMultiple(%p)\n", this);
351 for (i = 0; i < n; i++)
352 ff_dshow_meminputpin_Receive(this, samples[i]);
357 long ff_dshow_meminputpin_ReceiveCanBlock(DShowMemInputPin *this)
359 dshowdebug("ff_dshow_meminputpin_ReceiveCanBlock(%p)\n", this);
360 /* I swear I will not block. */
364 void ff_dshow_meminputpin_Destroy(DShowMemInputPin *this)
366 DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
367 dshowdebug("ff_dshow_meminputpin_Destroy(%p)\n", this);
368 ff_dshow_pin_Destroy(pin);