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