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