libavdevice/dshow.h
Go to the documentation of this file.
00001 /*
00002  * DirectShow capture interface
00003  * Copyright (c) 2010 Ramiro Polla
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #define DSHOWDEBUG 0
00023 
00024 #include "avdevice.h"
00025 
00026 #define COBJMACROS
00027 #include <windows.h>
00028 #include <dshow.h>
00029 #include <dvdmedia.h>
00030 
00031 long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src);
00032 void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps);
00033 void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps);
00034 void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type);
00035 void ff_printGUID(const GUID *g);
00036 
00037 #if DSHOWDEBUG
00038 extern const AVClass *ff_dshow_context_class_ptr;
00039 #define dshowdebug(...) av_log(&ff_dshow_context_class_ptr, AV_LOG_DEBUG, __VA_ARGS__)
00040 #else
00041 #define dshowdebug(...)
00042 #endif
00043 
00044 static inline void nothing(void *foo)
00045 {
00046 }
00047 
00048 struct GUIDoffset {
00049     const GUID *iid;
00050     int offset;
00051 };
00052 
00053 enum dshowDeviceType {
00054     VideoDevice = 0,
00055     AudioDevice = 1,
00056 };
00057 
00058 #define DECLARE_QUERYINTERFACE(class, ...)                                   \
00059 long WINAPI                                                                  \
00060 class##_QueryInterface(class *this, const GUID *riid, void **ppvObject)      \
00061 {                                                                            \
00062     struct GUIDoffset ifaces[] = __VA_ARGS__;                                \
00063     int i;                                                                   \
00064     dshowdebug(AV_STRINGIFY(class)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
00065     ff_printGUID(riid);                                                      \
00066     if (!ppvObject)                                                          \
00067         return E_POINTER;                                                    \
00068     for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) {                 \
00069         if (IsEqualGUID(riid, ifaces[i].iid)) {                              \
00070             void *obj = (void *) ((uint8_t *) this + ifaces[i].offset);      \
00071             class##_AddRef(this);                                            \
00072             dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset);  \
00073             *ppvObject = (void *) obj;                                       \
00074             return S_OK;                                                     \
00075         }                                                                    \
00076     }                                                                        \
00077     dshowdebug("\tE_NOINTERFACE\n");                                         \
00078     *ppvObject = NULL;                                                       \
00079     return E_NOINTERFACE;                                                    \
00080 }
00081 #define DECLARE_ADDREF(class)                                                \
00082 unsigned long WINAPI                                                         \
00083 class##_AddRef(class *this)                                                  \
00084 {                                                                            \
00085     dshowdebug(AV_STRINGIFY(class)"_AddRef(%p)\t%ld\n", this, this->ref+1);  \
00086     return InterlockedIncrement(&this->ref);                                 \
00087 }
00088 #define DECLARE_RELEASE(class)                                               \
00089 unsigned long WINAPI                                                         \
00090 class##_Release(class *this)                                                 \
00091 {                                                                            \
00092     long ref = InterlockedDecrement(&this->ref);                             \
00093     dshowdebug(AV_STRINGIFY(class)"_Release(%p)\t%ld\n", this, ref);         \
00094     if (!ref)                                                                \
00095         class##_Destroy(this);                                               \
00096     return ref;                                                              \
00097 }
00098 
00099 #define DECLARE_DESTROY(class, func)                                         \
00100 void class##_Destroy(class *this)                                            \
00101 {                                                                            \
00102     dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this);                   \
00103     func(this);                                                              \
00104     if (this) {                                                              \
00105         if (this->vtbl)                                                      \
00106             CoTaskMemFree(this->vtbl);                                       \
00107         CoTaskMemFree(this);                                                 \
00108     }                                                                        \
00109 }
00110 #define DECLARE_CREATE(class, setup, ...)                                    \
00111 class *class##_Create(__VA_ARGS__)                                           \
00112 {                                                                            \
00113     class *this = CoTaskMemAlloc(sizeof(class));                             \
00114     void  *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl));                       \
00115     dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this);                    \
00116     if (!this || !vtbl)                                                      \
00117         goto fail;                                                           \
00118     ZeroMemory(this, sizeof(class));                                         \
00119     ZeroMemory(vtbl, sizeof(*this->vtbl));                                   \
00120     this->ref  = 1;                                                          \
00121     this->vtbl = vtbl;                                                       \
00122     if (!setup)                                                              \
00123         goto fail;                                                           \
00124     dshowdebug("created "AV_STRINGIFY(class)" %p\n", this);                  \
00125     return this;                                                             \
00126 fail:                                                                        \
00127     class##_Destroy(this);                                                   \
00128     dshowdebug("could not create "AV_STRINGIFY(class)"\n");                  \
00129     return NULL;                                                             \
00130 }
00131 
00132 #define SETVTBL(vtbl, class, fn) \
00133     do { (vtbl)->fn = (void *) class##_##fn; } while(0)
00134 
00135 /*****************************************************************************
00136  * Forward Declarations
00137  ****************************************************************************/
00138 typedef struct libAVPin libAVPin;
00139 typedef struct libAVMemInputPin libAVMemInputPin;
00140 typedef struct libAVEnumPins libAVEnumPins;
00141 typedef struct libAVEnumMediaTypes libAVEnumMediaTypes;
00142 typedef struct libAVFilter libAVFilter;
00143 
00144 /*****************************************************************************
00145  * libAVPin
00146  ****************************************************************************/
00147 struct libAVPin {
00148     IPinVtbl *vtbl;
00149     long ref;
00150     libAVFilter *filter;
00151     IPin *connectedto;
00152     AM_MEDIA_TYPE type;
00153     IMemInputPinVtbl *imemvtbl;
00154 };
00155 
00156 long          WINAPI libAVPin_QueryInterface          (libAVPin *, const GUID *, void **);
00157 unsigned long WINAPI libAVPin_AddRef                  (libAVPin *);
00158 unsigned long WINAPI libAVPin_Release                 (libAVPin *);
00159 long          WINAPI libAVPin_Connect                 (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
00160 long          WINAPI libAVPin_ReceiveConnection       (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
00161 long          WINAPI libAVPin_Disconnect              (libAVPin *);
00162 long          WINAPI libAVPin_ConnectedTo             (libAVPin *, IPin **);
00163 long          WINAPI libAVPin_ConnectionMediaType     (libAVPin *, AM_MEDIA_TYPE *);
00164 long          WINAPI libAVPin_QueryPinInfo            (libAVPin *, PIN_INFO *);
00165 long          WINAPI libAVPin_QueryDirection          (libAVPin *, PIN_DIRECTION *);
00166 long          WINAPI libAVPin_QueryId                 (libAVPin *, wchar_t **);
00167 long          WINAPI libAVPin_QueryAccept             (libAVPin *, const AM_MEDIA_TYPE *);
00168 long          WINAPI libAVPin_EnumMediaTypes          (libAVPin *, IEnumMediaTypes **);
00169 long          WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *);
00170 long          WINAPI libAVPin_EndOfStream             (libAVPin *);
00171 long          WINAPI libAVPin_BeginFlush              (libAVPin *);
00172 long          WINAPI libAVPin_EndFlush                (libAVPin *);
00173 long          WINAPI libAVPin_NewSegment              (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double);
00174 
00175 long          WINAPI libAVMemInputPin_QueryInterface          (libAVMemInputPin *, const GUID *, void **);
00176 unsigned long WINAPI libAVMemInputPin_AddRef                  (libAVMemInputPin *);
00177 unsigned long WINAPI libAVMemInputPin_Release                 (libAVMemInputPin *);
00178 long          WINAPI libAVMemInputPin_GetAllocator            (libAVMemInputPin *, IMemAllocator **);
00179 long          WINAPI libAVMemInputPin_NotifyAllocator         (libAVMemInputPin *, IMemAllocator *, WINBOOL);
00180 long          WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *);
00181 long          WINAPI libAVMemInputPin_Receive                 (libAVMemInputPin *, IMediaSample *);
00182 long          WINAPI libAVMemInputPin_ReceiveMultiple         (libAVMemInputPin *, IMediaSample **, long, long *);
00183 long          WINAPI libAVMemInputPin_ReceiveCanBlock         (libAVMemInputPin *);
00184 
00185 void                 libAVPin_Destroy(libAVPin *);
00186 libAVPin            *libAVPin_Create (libAVFilter *filter);
00187 
00188 void                 libAVMemInputPin_Destroy(libAVMemInputPin *);
00189 
00190 /*****************************************************************************
00191  * libAVEnumPins
00192  ****************************************************************************/
00193 struct libAVEnumPins {
00194     IEnumPinsVtbl *vtbl;
00195     long ref;
00196     int pos;
00197     libAVPin *pin;
00198     libAVFilter *filter;
00199 };
00200 
00201 long          WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **);
00202 unsigned long WINAPI libAVEnumPins_AddRef        (libAVEnumPins *);
00203 unsigned long WINAPI libAVEnumPins_Release       (libAVEnumPins *);
00204 long          WINAPI libAVEnumPins_Next          (libAVEnumPins *, unsigned long, IPin **, unsigned long *);
00205 long          WINAPI libAVEnumPins_Skip          (libAVEnumPins *, unsigned long);
00206 long          WINAPI libAVEnumPins_Reset         (libAVEnumPins *);
00207 long          WINAPI libAVEnumPins_Clone         (libAVEnumPins *, libAVEnumPins **);
00208 
00209 void                 libAVEnumPins_Destroy(libAVEnumPins *);
00210 libAVEnumPins       *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter);
00211 
00212 /*****************************************************************************
00213  * libAVEnumMediaTypes
00214  ****************************************************************************/
00215 struct libAVEnumMediaTypes {
00216     IEnumPinsVtbl *vtbl;
00217     long ref;
00218     int pos;
00219     AM_MEDIA_TYPE type;
00220 };
00221 
00222 long          WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **);
00223 unsigned long WINAPI libAVEnumMediaTypes_AddRef        (libAVEnumMediaTypes *);
00224 unsigned long WINAPI libAVEnumMediaTypes_Release       (libAVEnumMediaTypes *);
00225 long          WINAPI libAVEnumMediaTypes_Next          (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
00226 long          WINAPI libAVEnumMediaTypes_Skip          (libAVEnumMediaTypes *, unsigned long);
00227 long          WINAPI libAVEnumMediaTypes_Reset         (libAVEnumMediaTypes *);
00228 long          WINAPI libAVEnumMediaTypes_Clone         (libAVEnumMediaTypes *, libAVEnumMediaTypes **);
00229 
00230 void                 libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *);
00231 libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type);
00232 
00233 /*****************************************************************************
00234  * libAVFilter
00235  ****************************************************************************/
00236 struct libAVFilter {
00237     IBaseFilterVtbl *vtbl;
00238     long ref;
00239     const wchar_t *name;
00240     libAVPin *pin;
00241     FILTER_INFO info;
00242     FILTER_STATE state;
00243     IReferenceClock *clock;
00244     enum dshowDeviceType type;
00245     void *priv_data;
00246     int stream_index;
00247     int64_t start_time;
00248     void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time);
00249 };
00250 
00251 long          WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **);
00252 unsigned long WINAPI libAVFilter_AddRef         (libAVFilter *);
00253 unsigned long WINAPI libAVFilter_Release        (libAVFilter *);
00254 long          WINAPI libAVFilter_GetClassID     (libAVFilter *, CLSID *);
00255 long          WINAPI libAVFilter_Stop           (libAVFilter *);
00256 long          WINAPI libAVFilter_Pause          (libAVFilter *);
00257 long          WINAPI libAVFilter_Run            (libAVFilter *, REFERENCE_TIME);
00258 long          WINAPI libAVFilter_GetState       (libAVFilter *, DWORD, FILTER_STATE *);
00259 long          WINAPI libAVFilter_SetSyncSource  (libAVFilter *, IReferenceClock *);
00260 long          WINAPI libAVFilter_GetSyncSource  (libAVFilter *, IReferenceClock **);
00261 long          WINAPI libAVFilter_EnumPins       (libAVFilter *, IEnumPins **);
00262 long          WINAPI libAVFilter_FindPin        (libAVFilter *, const wchar_t *, IPin **);
00263 long          WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *);
00264 long          WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *);
00265 long          WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **);
00266 
00267 void                 libAVFilter_Destroy(libAVFilter *);
00268 libAVFilter         *libAVFilter_Create (void *, void *, enum dshowDeviceType);