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 #ifndef AVDEVICE_DSHOW_CAPTURE_H
23 #define AVDEVICE_DSHOW_CAPTURE_H
30 #define WIN32_LEAN_AND_MEAN
32 #define NO_DSHOW_STRSAFE
36 #include "libavcodec/internal.h"
37 #include "libavcodec/packet_internal.h"
39 /* EC_DEVICE_LOST is not defined in MinGW dshow headers. */
40 #ifndef EC_DEVICE_LOST
41 #define EC_DEVICE_LOST 0x1f
44 long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src);
45 void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps);
46 void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps);
47 void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type);
48 void ff_printGUID(const GUID *g);
50 extern const AVClass *ff_dshow_context_class_ptr;
51 #define dshowdebug(...) ff_dlog(&ff_dshow_context_class_ptr, __VA_ARGS__)
53 static inline void nothing(void *foo)
62 enum dshowDeviceType {
67 enum dshowSourceFilterType {
68 VideoSourceDevice = 0,
69 AudioSourceDevice = 1,
72 #define DECLARE_QUERYINTERFACE(prefix, class, ...) \
74 ff_dshow_##prefix##_QueryInterface(class *this, const GUID *riid, void **ppvObject) \
76 struct GUIDoffset ifaces[] = __VA_ARGS__; \
78 dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
82 for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) { \
83 if (IsEqualGUID(riid, ifaces[i].iid)) { \
84 void *obj = (void *) ((uint8_t *) this + ifaces[i].offset); \
85 ff_dshow_##prefix##_AddRef(this); \
86 dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset); \
87 *ppvObject = (void *) obj; \
91 dshowdebug("\tE_NOINTERFACE\n"); \
93 return E_NOINTERFACE; \
95 #define DECLARE_ADDREF(prefix, class) \
97 ff_dshow_##prefix##_AddRef(class *this) \
99 dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_AddRef(%p)\t%ld\n", this, this->ref+1); \
100 return InterlockedIncrement(&this->ref); \
102 #define DECLARE_RELEASE(prefix, class) \
104 ff_dshow_##prefix##_Release(class *this) \
106 long ref = InterlockedDecrement(&this->ref); \
107 dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Release(%p)\t%ld\n", this, ref); \
109 ff_dshow_##prefix##_Destroy(this); \
113 #define DECLARE_DESTROY(prefix, class, func) \
114 void ff_dshow_##prefix##_Destroy(class *this) \
116 dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Destroy(%p)\n", this); \
120 CoTaskMemFree(this->vtbl); \
121 CoTaskMemFree(this); \
124 #define DECLARE_CREATE(prefix, class, setup, ...) \
125 class *ff_dshow_##prefix##_Create(__VA_ARGS__) \
127 class *this = CoTaskMemAlloc(sizeof(class)); \
128 void *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
129 dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Create(%p)\n", this); \
130 if (!this || !vtbl) \
132 ZeroMemory(this, sizeof(class)); \
133 ZeroMemory(vtbl, sizeof(*this->vtbl)); \
138 dshowdebug("created ff_dshow_"AV_STRINGIFY(prefix)" %p\n", this); \
141 ff_dshow_##prefix##_Destroy(this); \
142 dshowdebug("could not create ff_dshow_"AV_STRINGIFY(prefix)"\n"); \
146 #define SETVTBL(vtbl, prefix, fn) \
147 do { (vtbl)->fn = (void *) ff_dshow_##prefix##_##fn; } while(0)
149 /*****************************************************************************
150 * Forward Declarations
151 ****************************************************************************/
152 typedef struct DShowPin DShowPin;
153 typedef struct DShowMemInputPin DShowMemInputPin;
154 typedef struct DShowEnumPins DShowEnumPins;
155 typedef struct DShowEnumMediaTypes DShowEnumMediaTypes;
156 typedef struct DShowFilter DShowFilter;
158 /*****************************************************************************
160 ****************************************************************************/
167 IMemInputPinVtbl *imemvtbl;
170 long ff_dshow_pin_QueryInterface (DShowPin *, const GUID *, void **);
171 unsigned long ff_dshow_pin_AddRef (DShowPin *);
172 unsigned long ff_dshow_pin_Release (DShowPin *);
173 long ff_dshow_pin_Connect (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
174 long ff_dshow_pin_ReceiveConnection (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
175 long ff_dshow_pin_Disconnect (DShowPin *);
176 long ff_dshow_pin_ConnectedTo (DShowPin *, IPin **);
177 long ff_dshow_pin_ConnectionMediaType (DShowPin *, AM_MEDIA_TYPE *);
178 long ff_dshow_pin_QueryPinInfo (DShowPin *, PIN_INFO *);
179 long ff_dshow_pin_QueryDirection (DShowPin *, PIN_DIRECTION *);
180 long ff_dshow_pin_QueryId (DShowPin *, wchar_t **);
181 long ff_dshow_pin_QueryAccept (DShowPin *, const AM_MEDIA_TYPE *);
182 long ff_dshow_pin_EnumMediaTypes (DShowPin *, IEnumMediaTypes **);
183 long ff_dshow_pin_QueryInternalConnections(DShowPin *, IPin **, unsigned long *);
184 long ff_dshow_pin_EndOfStream (DShowPin *);
185 long ff_dshow_pin_BeginFlush (DShowPin *);
186 long ff_dshow_pin_EndFlush (DShowPin *);
187 long ff_dshow_pin_NewSegment (DShowPin *, REFERENCE_TIME, REFERENCE_TIME, double);
189 long ff_dshow_meminputpin_QueryInterface (DShowMemInputPin *, const GUID *, void **);
190 unsigned long ff_dshow_meminputpin_AddRef (DShowMemInputPin *);
191 unsigned long ff_dshow_meminputpin_Release (DShowMemInputPin *);
192 long ff_dshow_meminputpin_GetAllocator (DShowMemInputPin *, IMemAllocator **);
193 long ff_dshow_meminputpin_NotifyAllocator (DShowMemInputPin *, IMemAllocator *, BOOL);
194 long ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *, ALLOCATOR_PROPERTIES *);
195 long ff_dshow_meminputpin_Receive (DShowMemInputPin *, IMediaSample *);
196 long ff_dshow_meminputpin_ReceiveMultiple (DShowMemInputPin *, IMediaSample **, long, long *);
197 long ff_dshow_meminputpin_ReceiveCanBlock (DShowMemInputPin *);
199 void ff_dshow_pin_Destroy(DShowPin *);
200 DShowPin *ff_dshow_pin_Create (DShowFilter *filter);
202 void ff_dshow_meminputpin_Destroy(DShowMemInputPin *);
204 /*****************************************************************************
206 ****************************************************************************/
207 struct DShowEnumPins {
215 long ff_dshow_enumpins_QueryInterface(DShowEnumPins *, const GUID *, void **);
216 unsigned long ff_dshow_enumpins_AddRef (DShowEnumPins *);
217 unsigned long ff_dshow_enumpins_Release (DShowEnumPins *);
218 long ff_dshow_enumpins_Next (DShowEnumPins *, unsigned long, IPin **, unsigned long *);
219 long ff_dshow_enumpins_Skip (DShowEnumPins *, unsigned long);
220 long ff_dshow_enumpins_Reset (DShowEnumPins *);
221 long ff_dshow_enumpins_Clone (DShowEnumPins *, DShowEnumPins **);
223 void ff_dshow_enumpins_Destroy(DShowEnumPins *);
224 DShowEnumPins *ff_dshow_enumpins_Create (DShowPin *pin, DShowFilter *filter);
226 /*****************************************************************************
227 * DShowEnumMediaTypes
228 ****************************************************************************/
229 struct DShowEnumMediaTypes {
230 IEnumMediaTypesVtbl *vtbl;
236 long ff_dshow_enummediatypes_QueryInterface(DShowEnumMediaTypes *, const GUID *, void **);
237 unsigned long ff_dshow_enummediatypes_AddRef (DShowEnumMediaTypes *);
238 unsigned long ff_dshow_enummediatypes_Release (DShowEnumMediaTypes *);
239 long ff_dshow_enummediatypes_Next (DShowEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
240 long ff_dshow_enummediatypes_Skip (DShowEnumMediaTypes *, unsigned long);
241 long ff_dshow_enummediatypes_Reset (DShowEnumMediaTypes *);
242 long ff_dshow_enummediatypes_Clone (DShowEnumMediaTypes *, DShowEnumMediaTypes **);
244 void ff_dshow_enummediatypes_Destroy(DShowEnumMediaTypes *);
245 DShowEnumMediaTypes *ff_dshow_enummediatypes_Create(const AM_MEDIA_TYPE *type);
247 /*****************************************************************************
249 ****************************************************************************/
251 IBaseFilterVtbl *vtbl;
257 IReferenceClock *clock;
258 enum dshowDeviceType type;
262 void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType type);
265 long ff_dshow_filter_QueryInterface (DShowFilter *, const GUID *, void **);
266 unsigned long ff_dshow_filter_AddRef (DShowFilter *);
267 unsigned long ff_dshow_filter_Release (DShowFilter *);
268 long ff_dshow_filter_GetClassID (DShowFilter *, CLSID *);
269 long ff_dshow_filter_Stop (DShowFilter *);
270 long ff_dshow_filter_Pause (DShowFilter *);
271 long ff_dshow_filter_Run (DShowFilter *, REFERENCE_TIME);
272 long ff_dshow_filter_GetState (DShowFilter *, DWORD, FILTER_STATE *);
273 long ff_dshow_filter_SetSyncSource (DShowFilter *, IReferenceClock *);
274 long ff_dshow_filter_GetSyncSource (DShowFilter *, IReferenceClock **);
275 long ff_dshow_filter_EnumPins (DShowFilter *, IEnumPins **);
276 long ff_dshow_filter_FindPin (DShowFilter *, const wchar_t *, IPin **);
277 long ff_dshow_filter_QueryFilterInfo(DShowFilter *, FILTER_INFO *);
278 long ff_dshow_filter_JoinFilterGraph(DShowFilter *, IFilterGraph *, const wchar_t *);
279 long ff_dshow_filter_QueryVendorInfo(DShowFilter *, wchar_t **);
281 void ff_dshow_filter_Destroy(DShowFilter *);
282 DShowFilter *ff_dshow_filter_Create (void *, void *, enum dshowDeviceType);
284 /*****************************************************************************
286 ****************************************************************************/
288 const AVClass *class;
290 IGraphBuilder *graph;
292 char *device_name[2];
293 char *device_unique_name[2];
295 int video_device_number;
296 int audio_device_number;
300 int audio_buffer_size;
301 int crossbar_video_input_pin_number;
302 int crossbar_audio_input_pin_number;
303 char *video_pin_name;
304 char *audio_pin_name;
305 int show_video_device_dialog;
306 int show_audio_device_dialog;
307 int show_video_crossbar_connection_dialog;
308 int show_audio_crossbar_connection_dialog;
309 int show_analog_tv_tuner_dialog;
310 int show_analog_tv_tuner_audio_dialog;
311 char *audio_filter_load_file;
312 char *audio_filter_save_file;
313 char *video_filter_load_file;
314 char *video_filter_save_file;
316 IBaseFilter *device_filter[2];
318 DShowFilter *capture_filter[2];
319 DShowPin *capture_pin[2];
322 HANDLE event[2]; /* event[0] is set by DirectShow
323 * event[1] is set by callback() */
328 int64_t curbufsize[2];
329 unsigned int video_frame_num;
331 IMediaControl *control;
332 IMediaEvent *media_event;
334 enum AVPixelFormat pixel_format;
335 enum AVCodecID video_codec_id;
339 int requested_height;
340 AVRational requested_framerate;
347 /*****************************************************************************
349 ****************************************************************************/
350 HRESULT ff_dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
351 IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx);
353 void ff_dshow_show_filter_properties(IBaseFilter *pFilter, AVFormatContext *avctx);
355 #endif /* AVDEVICE_DSHOW_CAPTURE_H */