dshow_capture.h 14 KB

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