dshow: rename dshow.h to avoid conflict with system header of equal name
[ffmpeg.git] / 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 #include <windows.h>
31 #include <dshow.h>
32 #include <dvdmedia.h>
33
34 long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src);
35 void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps);
36 void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps);
37 void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type);
38 void ff_printGUID(const GUID *g);
39
40 #if DSHOWDEBUG
41 extern const AVClass *ff_dshow_context_class_ptr;
42 #define dshowdebug(...) av_log(&ff_dshow_context_class_ptr, AV_LOG_DEBUG, __VA_ARGS__)
43 #else
44 #define dshowdebug(...)
45 #endif
46
47 static inline void nothing(void *foo)
48 {
49 }
50
51 struct GUIDoffset {
52     const GUID *iid;
53     int offset;
54 };
55
56 enum dshowDeviceType {
57     VideoDevice = 0,
58     AudioDevice = 1,
59 };
60
61 #define DECLARE_QUERYINTERFACE(class, ...)                                   \
62 long WINAPI                                                                  \
63 class##_QueryInterface(class *this, const GUID *riid, void **ppvObject)      \
64 {                                                                            \
65     struct GUIDoffset ifaces[] = __VA_ARGS__;                                \
66     int i;                                                                   \
67     dshowdebug(AV_STRINGIFY(class)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
68     ff_printGUID(riid);                                                      \
69     if (!ppvObject)                                                          \
70         return E_POINTER;                                                    \
71     for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) {                 \
72         if (IsEqualGUID(riid, ifaces[i].iid)) {                              \
73             void *obj = (void *) ((uint8_t *) this + ifaces[i].offset);      \
74             class##_AddRef(this);                                            \
75             dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset);  \
76             *ppvObject = (void *) obj;                                       \
77             return S_OK;                                                     \
78         }                                                                    \
79     }                                                                        \
80     dshowdebug("\tE_NOINTERFACE\n");                                         \
81     *ppvObject = NULL;                                                       \
82     return E_NOINTERFACE;                                                    \
83 }
84 #define DECLARE_ADDREF(class)                                                \
85 unsigned long WINAPI                                                         \
86 class##_AddRef(class *this)                                                  \
87 {                                                                            \
88     dshowdebug(AV_STRINGIFY(class)"_AddRef(%p)\t%ld\n", this, this->ref+1);  \
89     return InterlockedIncrement(&this->ref);                                 \
90 }
91 #define DECLARE_RELEASE(class)                                               \
92 unsigned long WINAPI                                                         \
93 class##_Release(class *this)                                                 \
94 {                                                                            \
95     long ref = InterlockedDecrement(&this->ref);                             \
96     dshowdebug(AV_STRINGIFY(class)"_Release(%p)\t%ld\n", this, ref);         \
97     if (!ref)                                                                \
98         class##_Destroy(this);                                               \
99     return ref;                                                              \
100 }
101
102 #define DECLARE_DESTROY(class, func)                                         \
103 void class##_Destroy(class *this)                                            \
104 {                                                                            \
105     dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this);                   \
106     func(this);                                                              \
107     if (this) {                                                              \
108         if (this->vtbl)                                                      \
109             CoTaskMemFree(this->vtbl);                                       \
110         CoTaskMemFree(this);                                                 \
111     }                                                                        \
112 }
113 #define DECLARE_CREATE(class, setup, ...)                                    \
114 class *class##_Create(__VA_ARGS__)                                           \
115 {                                                                            \
116     class *this = CoTaskMemAlloc(sizeof(class));                             \
117     void  *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl));                       \
118     dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this);                    \
119     if (!this || !vtbl)                                                      \
120         goto fail;                                                           \
121     ZeroMemory(this, sizeof(class));                                         \
122     ZeroMemory(vtbl, sizeof(*this->vtbl));                                   \
123     this->ref  = 1;                                                          \
124     this->vtbl = vtbl;                                                       \
125     if (!setup)                                                              \
126         goto fail;                                                           \
127     dshowdebug("created "AV_STRINGIFY(class)" %p\n", this);                  \
128     return this;                                                             \
129 fail:                                                                        \
130     class##_Destroy(this);                                                   \
131     dshowdebug("could not create "AV_STRINGIFY(class)"\n");                  \
132     return NULL;                                                             \
133 }
134
135 #define SETVTBL(vtbl, class, fn) \
136     do { (vtbl)->fn = (void *) class##_##fn; } while(0)
137
138 /*****************************************************************************
139  * Forward Declarations
140  ****************************************************************************/
141 typedef struct libAVPin libAVPin;
142 typedef struct libAVMemInputPin libAVMemInputPin;
143 typedef struct libAVEnumPins libAVEnumPins;
144 typedef struct libAVEnumMediaTypes libAVEnumMediaTypes;
145 typedef struct libAVFilter libAVFilter;
146
147 /*****************************************************************************
148  * libAVPin
149  ****************************************************************************/
150 struct libAVPin {
151     IPinVtbl *vtbl;
152     long ref;
153     libAVFilter *filter;
154     IPin *connectedto;
155     AM_MEDIA_TYPE type;
156     IMemInputPinVtbl *imemvtbl;
157 };
158
159 long          WINAPI libAVPin_QueryInterface          (libAVPin *, const GUID *, void **);
160 unsigned long WINAPI libAVPin_AddRef                  (libAVPin *);
161 unsigned long WINAPI libAVPin_Release                 (libAVPin *);
162 long          WINAPI libAVPin_Connect                 (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
163 long          WINAPI libAVPin_ReceiveConnection       (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
164 long          WINAPI libAVPin_Disconnect              (libAVPin *);
165 long          WINAPI libAVPin_ConnectedTo             (libAVPin *, IPin **);
166 long          WINAPI libAVPin_ConnectionMediaType     (libAVPin *, AM_MEDIA_TYPE *);
167 long          WINAPI libAVPin_QueryPinInfo            (libAVPin *, PIN_INFO *);
168 long          WINAPI libAVPin_QueryDirection          (libAVPin *, PIN_DIRECTION *);
169 long          WINAPI libAVPin_QueryId                 (libAVPin *, wchar_t **);
170 long          WINAPI libAVPin_QueryAccept             (libAVPin *, const AM_MEDIA_TYPE *);
171 long          WINAPI libAVPin_EnumMediaTypes          (libAVPin *, IEnumMediaTypes **);
172 long          WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *);
173 long          WINAPI libAVPin_EndOfStream             (libAVPin *);
174 long          WINAPI libAVPin_BeginFlush              (libAVPin *);
175 long          WINAPI libAVPin_EndFlush                (libAVPin *);
176 long          WINAPI libAVPin_NewSegment              (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double);
177
178 long          WINAPI libAVMemInputPin_QueryInterface          (libAVMemInputPin *, const GUID *, void **);
179 unsigned long WINAPI libAVMemInputPin_AddRef                  (libAVMemInputPin *);
180 unsigned long WINAPI libAVMemInputPin_Release                 (libAVMemInputPin *);
181 long          WINAPI libAVMemInputPin_GetAllocator            (libAVMemInputPin *, IMemAllocator **);
182 long          WINAPI libAVMemInputPin_NotifyAllocator         (libAVMemInputPin *, IMemAllocator *, WINBOOL);
183 long          WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *);
184 long          WINAPI libAVMemInputPin_Receive                 (libAVMemInputPin *, IMediaSample *);
185 long          WINAPI libAVMemInputPin_ReceiveMultiple         (libAVMemInputPin *, IMediaSample **, long, long *);
186 long          WINAPI libAVMemInputPin_ReceiveCanBlock         (libAVMemInputPin *);
187
188 void                 libAVPin_Destroy(libAVPin *);
189 libAVPin            *libAVPin_Create (libAVFilter *filter);
190
191 void                 libAVMemInputPin_Destroy(libAVMemInputPin *);
192
193 /*****************************************************************************
194  * libAVEnumPins
195  ****************************************************************************/
196 struct libAVEnumPins {
197     IEnumPinsVtbl *vtbl;
198     long ref;
199     int pos;
200     libAVPin *pin;
201     libAVFilter *filter;
202 };
203
204 long          WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **);
205 unsigned long WINAPI libAVEnumPins_AddRef        (libAVEnumPins *);
206 unsigned long WINAPI libAVEnumPins_Release       (libAVEnumPins *);
207 long          WINAPI libAVEnumPins_Next          (libAVEnumPins *, unsigned long, IPin **, unsigned long *);
208 long          WINAPI libAVEnumPins_Skip          (libAVEnumPins *, unsigned long);
209 long          WINAPI libAVEnumPins_Reset         (libAVEnumPins *);
210 long          WINAPI libAVEnumPins_Clone         (libAVEnumPins *, libAVEnumPins **);
211
212 void                 libAVEnumPins_Destroy(libAVEnumPins *);
213 libAVEnumPins       *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter);
214
215 /*****************************************************************************
216  * libAVEnumMediaTypes
217  ****************************************************************************/
218 struct libAVEnumMediaTypes {
219     IEnumPinsVtbl *vtbl;
220     long ref;
221     int pos;
222     AM_MEDIA_TYPE type;
223 };
224
225 long          WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **);
226 unsigned long WINAPI libAVEnumMediaTypes_AddRef        (libAVEnumMediaTypes *);
227 unsigned long WINAPI libAVEnumMediaTypes_Release       (libAVEnumMediaTypes *);
228 long          WINAPI libAVEnumMediaTypes_Next          (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
229 long          WINAPI libAVEnumMediaTypes_Skip          (libAVEnumMediaTypes *, unsigned long);
230 long          WINAPI libAVEnumMediaTypes_Reset         (libAVEnumMediaTypes *);
231 long          WINAPI libAVEnumMediaTypes_Clone         (libAVEnumMediaTypes *, libAVEnumMediaTypes **);
232
233 void                 libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *);
234 libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type);
235
236 /*****************************************************************************
237  * libAVFilter
238  ****************************************************************************/
239 struct libAVFilter {
240     IBaseFilterVtbl *vtbl;
241     long ref;
242     const wchar_t *name;
243     libAVPin *pin;
244     FILTER_INFO info;
245     FILTER_STATE state;
246     IReferenceClock *clock;
247     enum dshowDeviceType type;
248     void *priv_data;
249     int stream_index;
250     int64_t start_time;
251     void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time);
252 };
253
254 long          WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **);
255 unsigned long WINAPI libAVFilter_AddRef         (libAVFilter *);
256 unsigned long WINAPI libAVFilter_Release        (libAVFilter *);
257 long          WINAPI libAVFilter_GetClassID     (libAVFilter *, CLSID *);
258 long          WINAPI libAVFilter_Stop           (libAVFilter *);
259 long          WINAPI libAVFilter_Pause          (libAVFilter *);
260 long          WINAPI libAVFilter_Run            (libAVFilter *, REFERENCE_TIME);
261 long          WINAPI libAVFilter_GetState       (libAVFilter *, DWORD, FILTER_STATE *);
262 long          WINAPI libAVFilter_SetSyncSource  (libAVFilter *, IReferenceClock *);
263 long          WINAPI libAVFilter_GetSyncSource  (libAVFilter *, IReferenceClock **);
264 long          WINAPI libAVFilter_EnumPins       (libAVFilter *, IEnumPins **);
265 long          WINAPI libAVFilter_FindPin        (libAVFilter *, const wchar_t *, IPin **);
266 long          WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *);
267 long          WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *);
268 long          WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **);
269
270 void                 libAVFilter_Destroy(libAVFilter *);
271 libAVFilter         *libAVFilter_Create (void *, void *, enum dshowDeviceType);
272
273 #endif /* AVDEVICE_DSHOW_H */