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 #define NO_DSHOW_STRSAFE
26 #define imemoffset offsetof(libAVPin, imemvtbl)
28 DECLARE_QUERYINTERFACE(libAVPin,
29 { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
30 DECLARE_ADDREF(libAVPin)
31 DECLARE_RELEASE(libAVPin)
34 libAVPin_Connect(libAVPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
36 dshowdebug("libAVPin_Connect(%p, %p, %p)\n", this, pin, type);
37 /* Input pins receive connections. */
41 libAVPin_ReceiveConnection(libAVPin *this, IPin *pin,
42 const AM_MEDIA_TYPE *type)
44 enum dshowDeviceType devtype = this->filter->type;
45 dshowdebug("libAVPin_ReceiveConnection(%p)\n", this);
49 if (this->connectedto)
50 return VFW_E_ALREADY_CONNECTED;
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;
57 if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
58 return VFW_E_TYPE_NOT_ACCEPTED;
62 this->connectedto = pin;
64 ff_copy_dshow_media_type(&this->type, type);
69 libAVPin_Disconnect(libAVPin *this)
71 dshowdebug("libAVPin_Disconnect(%p)\n", this);
73 if (this->filter->state != State_Stopped)
74 return VFW_E_NOT_STOPPED;
75 if (!this->connectedto)
77 IPin_Release(this->connectedto);
78 this->connectedto = NULL;
83 libAVPin_ConnectedTo(libAVPin *this, IPin **pin)
85 dshowdebug("libAVPin_ConnectedTo(%p)\n", this);
89 if (!this->connectedto)
90 return VFW_E_NOT_CONNECTED;
91 IPin_AddRef(this->connectedto);
92 *pin = this->connectedto;
97 libAVPin_ConnectionMediaType(libAVPin *this, AM_MEDIA_TYPE *type)
99 dshowdebug("libAVPin_ConnectionMediaType(%p)\n", this);
103 if (!this->connectedto)
104 return VFW_E_NOT_CONNECTED;
106 return ff_copy_dshow_media_type(type, &this->type);
109 libAVPin_QueryPinInfo(libAVPin *this, PIN_INFO *info)
111 dshowdebug("libAVPin_QueryPinInfo(%p)\n", this);
117 libAVFilter_AddRef(this->filter);
119 info->pFilter = (IBaseFilter *) this->filter;
120 info->dir = PINDIR_INPUT;
121 wcscpy(info->achName, L"Capture");
126 libAVPin_QueryDirection(libAVPin *this, PIN_DIRECTION *dir)
128 dshowdebug("libAVPin_QueryDirection(%p)\n", this);
135 libAVPin_QueryId(libAVPin *this, wchar_t **id)
137 dshowdebug("libAVPin_QueryId(%p)\n", this);
142 *id = wcsdup(L"libAV Pin");
147 libAVPin_QueryAccept(libAVPin *this, const AM_MEDIA_TYPE *type)
149 dshowdebug("libAVPin_QueryAccept(%p)\n", this);
153 libAVPin_EnumMediaTypes(libAVPin *this, IEnumMediaTypes **enumtypes)
155 const AM_MEDIA_TYPE *type = NULL;
156 libAVEnumMediaTypes *new;
157 dshowdebug("libAVPin_EnumMediaTypes(%p)\n", this);
161 new = libAVEnumMediaTypes_Create(type);
163 return E_OUTOFMEMORY;
165 *enumtypes = (IEnumMediaTypes *) new;
169 libAVPin_QueryInternalConnections(libAVPin *this, IPin **pin,
172 dshowdebug("libAVPin_QueryInternalConnections(%p)\n", this);
176 libAVPin_EndOfStream(libAVPin *this)
178 dshowdebug("libAVPin_EndOfStream(%p)\n", this);
183 libAVPin_BeginFlush(libAVPin *this)
185 dshowdebug("libAVPin_BeginFlush(%p)\n", this);
190 libAVPin_EndFlush(libAVPin *this)
192 dshowdebug("libAVPin_EndFlush(%p)\n", this);
197 libAVPin_NewSegment(libAVPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
200 dshowdebug("libAVPin_NewSegment(%p)\n", this);
206 libAVPin_Setup(libAVPin *this, libAVFilter *filter)
208 IPinVtbl *vtbl = this->vtbl;
209 IMemInputPinVtbl *imemvtbl;
214 imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
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);
228 this->imemvtbl = imemvtbl;
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);
249 this->filter = filter;
253 DECLARE_CREATE(libAVPin, libAVPin_Setup(this, filter), libAVFilter *filter)
254 DECLARE_DESTROY(libAVPin, nothing)
256 /*****************************************************************************
258 ****************************************************************************/
260 libAVMemInputPin_QueryInterface(libAVMemInputPin *this, const GUID *riid,
263 libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
264 dshowdebug("libAVMemInputPin_QueryInterface(%p)\n", this);
265 return libAVPin_QueryInterface(pin, riid, ppvObject);
268 libAVMemInputPin_AddRef(libAVMemInputPin *this)
270 libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
271 dshowdebug("libAVMemInputPin_AddRef(%p)\n", this);
272 return libAVPin_AddRef(pin);
275 libAVMemInputPin_Release(libAVMemInputPin *this)
277 libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
278 dshowdebug("libAVMemInputPin_Release(%p)\n", this);
279 return libAVPin_Release(pin);
282 libAVMemInputPin_GetAllocator(libAVMemInputPin *this, IMemAllocator **alloc)
284 dshowdebug("libAVMemInputPin_GetAllocator(%p)\n", this);
285 return VFW_E_NO_ALLOCATOR;
288 libAVMemInputPin_NotifyAllocator(libAVMemInputPin *this, IMemAllocator *alloc,
291 dshowdebug("libAVMemInputPin_NotifyAllocator(%p)\n", this);
295 libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *this,
296 ALLOCATOR_PROPERTIES *props)
298 dshowdebug("libAVMemInputPin_GetAllocatorRequirements(%p)\n", this);
302 libAVMemInputPin_Receive(libAVMemInputPin *this, IMediaSample *sample)
304 libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
305 enum dshowDeviceType devtype = pin->filter->type;
312 dshowdebug("libAVMemInputPin_Receive(%p)\n", this);
317 if (devtype == VideoDevice) {
318 /* PTS from video devices is unreliable. */
319 IReferenceClock *clock = pin->filter->clock;
320 IReferenceClock_GetTime(clock, &curtime);
323 IMediaSample_GetTime(sample, &curtime, &dummy);
324 curtime += pin->filter->start_time;
327 buf_size = IMediaSample_GetActualDataLength(sample);
328 IMediaSample_GetPointer(sample, &buf);
329 priv_data = pin->filter->priv_data;
330 index = pin->filter->stream_index;
332 pin->filter->callback(priv_data, index, buf, buf_size, curtime);
337 libAVMemInputPin_ReceiveMultiple(libAVMemInputPin *this,
338 IMediaSample **samples, long n, long *nproc)
341 dshowdebug("libAVMemInputPin_ReceiveMultiple(%p)\n", this);
343 for (i = 0; i < n; i++)
344 libAVMemInputPin_Receive(this, samples[i]);
350 libAVMemInputPin_ReceiveCanBlock(libAVMemInputPin *this)
352 dshowdebug("libAVMemInputPin_ReceiveCanBlock(%p)\n", this);
353 /* I swear I will not block. */
358 libAVMemInputPin_Destroy(libAVMemInputPin *this)
360 libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
361 dshowdebug("libAVMemInputPin_Destroy(%p)\n", this);
362 return libAVPin_Destroy(pin);