]> git.sesse.net Git - ffmpeg/blob - libavdevice/dshow_capture.h
Merge commit '84f226a3bcd8b39801a4c9051c033ab7d61aaf76'
[ffmpeg] / libavdevice / dshow_capture.h
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 #ifndef AVDEVICE_DSHOW_H
23 #define AVDEVICE_DSHOW_H
24
25 #define DSHOWDEBUG 0
26
27 #include "avdevice.h"
28
29 #define COBJMACROS
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
32 #define NO_DSHOW_STRSAFE
33 #include <dshow.h>
34 #include <dvdmedia.h>
35
36 /* EC_DEVICE_LOST is not defined in MinGW dshow headers. */
37 #ifndef EC_DEVICE_LOST
38 #define EC_DEVICE_LOST 0x1f
39 #endif
40
41 long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src);
42 void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps);
43 void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps);
44 void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type);
45 void ff_printGUID(const GUID *g);
46
47 #if DSHOWDEBUG
48 extern const AVClass *ff_dshow_context_class_ptr;
49 #define dshowdebug(...) av_log(&ff_dshow_context_class_ptr, AV_LOG_DEBUG, __VA_ARGS__)
50 #else
51 #define dshowdebug(...)
52 #endif
53
54 static inline void nothing(void *foo)
55 {
56 }
57
58 struct GUIDoffset {
59     const GUID *iid;
60     int offset;
61 };
62
63 enum dshowDeviceType {
64     VideoDevice = 0,
65     AudioDevice = 1,
66 };
67
68 enum dshowSourceFilterType {
69     VideoSourceDevice = 0,
70     AudioSourceDevice = 1,
71 };
72
73 #define DECLARE_QUERYINTERFACE(class, ...)                                   \
74 long WINAPI                                                                  \
75 class##_QueryInterface(class *this, const GUID *riid, void **ppvObject)      \
76 {                                                                            \
77     struct GUIDoffset ifaces[] = __VA_ARGS__;                                \
78     int i;                                                                   \
79     dshowdebug(AV_STRINGIFY(class)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
80     ff_printGUID(riid);                                                      \
81     if (!ppvObject)                                                          \
82         return E_POINTER;                                                    \
83     for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) {                 \
84         if (IsEqualGUID(riid, ifaces[i].iid)) {                              \
85             void *obj = (void *) ((uint8_t *) this + ifaces[i].offset);      \
86             class##_AddRef(this);                                            \
87             dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset);  \
88             *ppvObject = (void *) obj;                                       \
89             return S_OK;                                                     \
90         }                                                                    \
91     }                                                                        \
92     dshowdebug("\tE_NOINTERFACE\n");                                         \
93     *ppvObject = NULL;                                                       \
94     return E_NOINTERFACE;                                                    \
95 }
96 #define DECLARE_ADDREF(class)                                                \
97 unsigned long WINAPI                                                         \
98 class##_AddRef(class *this)                                                  \
99 {                                                                            \
100     dshowdebug(AV_STRINGIFY(class)"_AddRef(%p)\t%ld\n", this, this->ref+1);  \
101     return InterlockedIncrement(&this->ref);                                 \
102 }
103 #define DECLARE_RELEASE(class)                                               \
104 unsigned long WINAPI                                                         \
105 class##_Release(class *this)                                                 \
106 {                                                                            \
107     long ref = InterlockedDecrement(&this->ref);                             \
108     dshowdebug(AV_STRINGIFY(class)"_Release(%p)\t%ld\n", this, ref);         \
109     if (!ref)                                                                \
110         class##_Destroy(this);                                               \
111     return ref;                                                              \
112 }
113
114 #define DECLARE_DESTROY(class, func)                                         \
115 void class##_Destroy(class *this)                                            \
116 {                                                                            \
117     dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this);                   \
118     func(this);                                                              \
119     if (this) {                                                              \
120         if (this->vtbl)                                                      \
121             CoTaskMemFree(this->vtbl);                                       \
122         CoTaskMemFree(this);                                                 \
123     }                                                                        \
124 }
125 #define DECLARE_CREATE(class, setup, ...)                                    \
126 class *class##_Create(__VA_ARGS__)                                           \
127 {                                                                            \
128     class *this = CoTaskMemAlloc(sizeof(class));                             \
129     void  *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl));                       \
130     dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this);                    \
131     if (!this || !vtbl)                                                      \
132         goto fail;                                                           \
133     ZeroMemory(this, sizeof(class));                                         \
134     ZeroMemory(vtbl, sizeof(*this->vtbl));                                   \
135     this->ref  = 1;                                                          \
136     this->vtbl = vtbl;                                                       \
137     if (!setup)                                                              \
138         goto fail;                                                           \
139     dshowdebug("created "AV_STRINGIFY(class)" %p\n", this);                  \
140     return this;                                                             \
141 fail:                                                                        \
142     class##_Destroy(this);                                                   \
143     dshowdebug("could not create "AV_STRINGIFY(class)"\n");                  \
144     return NULL;                                                             \
145 }
146
147 #define SETVTBL(vtbl, class, fn) \
148     do { (vtbl)->fn = (void *) class##_##fn; } while(0)
149
150 /*****************************************************************************
151  * Forward Declarations
152  ****************************************************************************/
153 typedef struct libAVPin libAVPin;
154 typedef struct libAVMemInputPin libAVMemInputPin;
155 typedef struct libAVEnumPins libAVEnumPins;
156 typedef struct libAVEnumMediaTypes libAVEnumMediaTypes;
157 typedef struct libAVFilter libAVFilter;
158
159 /*****************************************************************************
160  * libAVPin
161  ****************************************************************************/
162 struct libAVPin {
163     IPinVtbl *vtbl;
164     long ref;
165     libAVFilter *filter;
166     IPin *connectedto;
167     AM_MEDIA_TYPE type;
168     IMemInputPinVtbl *imemvtbl;
169 };
170
171 long          WINAPI libAVPin_QueryInterface          (libAVPin *, const GUID *, void **);
172 unsigned long WINAPI libAVPin_AddRef                  (libAVPin *);
173 unsigned long WINAPI libAVPin_Release                 (libAVPin *);
174 long          WINAPI libAVPin_Connect                 (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
175 long          WINAPI libAVPin_ReceiveConnection       (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
176 long          WINAPI libAVPin_Disconnect              (libAVPin *);
177 long          WINAPI libAVPin_ConnectedTo             (libAVPin *, IPin **);
178 long          WINAPI libAVPin_ConnectionMediaType     (libAVPin *, AM_MEDIA_TYPE *);
179 long          WINAPI libAVPin_QueryPinInfo            (libAVPin *, PIN_INFO *);
180 long          WINAPI libAVPin_QueryDirection          (libAVPin *, PIN_DIRECTION *);
181 long          WINAPI libAVPin_QueryId                 (libAVPin *, wchar_t **);
182 long          WINAPI libAVPin_QueryAccept             (libAVPin *, const AM_MEDIA_TYPE *);
183 long          WINAPI libAVPin_EnumMediaTypes          (libAVPin *, IEnumMediaTypes **);
184 long          WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *);
185 long          WINAPI libAVPin_EndOfStream             (libAVPin *);
186 long          WINAPI libAVPin_BeginFlush              (libAVPin *);
187 long          WINAPI libAVPin_EndFlush                (libAVPin *);
188 long          WINAPI libAVPin_NewSegment              (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double);
189
190 long          WINAPI libAVMemInputPin_QueryInterface          (libAVMemInputPin *, const GUID *, void **);
191 unsigned long WINAPI libAVMemInputPin_AddRef                  (libAVMemInputPin *);
192 unsigned long WINAPI libAVMemInputPin_Release                 (libAVMemInputPin *);
193 long          WINAPI libAVMemInputPin_GetAllocator            (libAVMemInputPin *, IMemAllocator **);
194 long          WINAPI libAVMemInputPin_NotifyAllocator         (libAVMemInputPin *, IMemAllocator *, BOOL);
195 long          WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *);
196 long          WINAPI libAVMemInputPin_Receive                 (libAVMemInputPin *, IMediaSample *);
197 long          WINAPI libAVMemInputPin_ReceiveMultiple         (libAVMemInputPin *, IMediaSample **, long, long *);
198 long          WINAPI libAVMemInputPin_ReceiveCanBlock         (libAVMemInputPin *);
199
200 void                 libAVPin_Destroy(libAVPin *);
201 libAVPin            *libAVPin_Create (libAVFilter *filter);
202
203 void                 libAVMemInputPin_Destroy(libAVMemInputPin *);
204
205 /*****************************************************************************
206  * libAVEnumPins
207  ****************************************************************************/
208 struct libAVEnumPins {
209     IEnumPinsVtbl *vtbl;
210     long ref;
211     int pos;
212     libAVPin *pin;
213     libAVFilter *filter;
214 };
215
216 long          WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **);
217 unsigned long WINAPI libAVEnumPins_AddRef        (libAVEnumPins *);
218 unsigned long WINAPI libAVEnumPins_Release       (libAVEnumPins *);
219 long          WINAPI libAVEnumPins_Next          (libAVEnumPins *, unsigned long, IPin **, unsigned long *);
220 long          WINAPI libAVEnumPins_Skip          (libAVEnumPins *, unsigned long);
221 long          WINAPI libAVEnumPins_Reset         (libAVEnumPins *);
222 long          WINAPI libAVEnumPins_Clone         (libAVEnumPins *, libAVEnumPins **);
223
224 void                 libAVEnumPins_Destroy(libAVEnumPins *);
225 libAVEnumPins       *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter);
226
227 /*****************************************************************************
228  * libAVEnumMediaTypes
229  ****************************************************************************/
230 struct libAVEnumMediaTypes {
231     IEnumMediaTypesVtbl *vtbl;
232     long ref;
233     int pos;
234     AM_MEDIA_TYPE type;
235 };
236
237 long          WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **);
238 unsigned long WINAPI libAVEnumMediaTypes_AddRef        (libAVEnumMediaTypes *);
239 unsigned long WINAPI libAVEnumMediaTypes_Release       (libAVEnumMediaTypes *);
240 long          WINAPI libAVEnumMediaTypes_Next          (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
241 long          WINAPI libAVEnumMediaTypes_Skip          (libAVEnumMediaTypes *, unsigned long);
242 long          WINAPI libAVEnumMediaTypes_Reset         (libAVEnumMediaTypes *);
243 long          WINAPI libAVEnumMediaTypes_Clone         (libAVEnumMediaTypes *, libAVEnumMediaTypes **);
244
245 void                 libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *);
246 libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type);
247
248 /*****************************************************************************
249  * libAVFilter
250  ****************************************************************************/
251 struct libAVFilter {
252     IBaseFilterVtbl *vtbl;
253     long ref;
254     const wchar_t *name;
255     libAVPin *pin;
256     FILTER_INFO info;
257     FILTER_STATE state;
258     IReferenceClock *clock;
259     enum dshowDeviceType type;
260     void *priv_data;
261     int stream_index;
262     int64_t start_time;
263     void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType type);
264 };
265
266 long          WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **);
267 unsigned long WINAPI libAVFilter_AddRef         (libAVFilter *);
268 unsigned long WINAPI libAVFilter_Release        (libAVFilter *);
269 long          WINAPI libAVFilter_GetClassID     (libAVFilter *, CLSID *);
270 long          WINAPI libAVFilter_Stop           (libAVFilter *);
271 long          WINAPI libAVFilter_Pause          (libAVFilter *);
272 long          WINAPI libAVFilter_Run            (libAVFilter *, REFERENCE_TIME);
273 long          WINAPI libAVFilter_GetState       (libAVFilter *, DWORD, FILTER_STATE *);
274 long          WINAPI libAVFilter_SetSyncSource  (libAVFilter *, IReferenceClock *);
275 long          WINAPI libAVFilter_GetSyncSource  (libAVFilter *, IReferenceClock **);
276 long          WINAPI libAVFilter_EnumPins       (libAVFilter *, IEnumPins **);
277 long          WINAPI libAVFilter_FindPin        (libAVFilter *, const wchar_t *, IPin **);
278 long          WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *);
279 long          WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *);
280 long          WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **);
281
282 void                 libAVFilter_Destroy(libAVFilter *);
283 libAVFilter         *libAVFilter_Create (void *, void *, enum dshowDeviceType);
284
285 /*****************************************************************************
286  * dshow_ctx
287  ****************************************************************************/
288 struct dshow_ctx {
289     const AVClass *class;
290
291     IGraphBuilder *graph;
292
293     char *device_name[2];
294     int video_device_number;
295     int audio_device_number;
296
297     int   list_options;
298     int   list_devices;
299     int   audio_buffer_size;
300     int   crossbar_video_input_pin_number;
301     int   crossbar_audio_input_pin_number;
302     char *video_pin_name;
303     char *audio_pin_name;
304     int   show_video_device_dialog;
305     int   show_audio_device_dialog;
306     int   show_video_crossbar_connection_dialog;
307     int   show_audio_crossbar_connection_dialog;
308     int   show_analog_tv_tuner_dialog;
309     int   show_analog_tv_tuner_audio_dialog;
310
311     IBaseFilter *device_filter[2];
312     IPin        *device_pin[2];
313     libAVFilter *capture_filter[2];
314     libAVPin    *capture_pin[2];
315
316     HANDLE mutex;
317     HANDLE event[2]; /* event[0] is set by DirectShow
318                       * event[1] is set by callback() */
319     AVPacketList *pktl;
320
321     int eof;
322
323     int64_t curbufsize[2];
324     unsigned int video_frame_num;
325
326     IMediaControl *control;
327     IMediaEvent *media_event;
328
329     enum AVPixelFormat pixel_format;
330     enum AVCodecID video_codec_id;
331     char *framerate;
332
333     int requested_width;
334     int requested_height;
335     AVRational requested_framerate;
336
337     int sample_rate;
338     int sample_size;
339     int channels;
340 };
341
342 /*****************************************************************************
343  * CrossBar
344  ****************************************************************************/
345 HRESULT dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
346     IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx);
347
348 void dshow_show_filter_properties(IBaseFilter *pFilter, AVFormatContext *avctx);
349
350 #endif /* AVDEVICE_DSHOW_H */