dshow_capture.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #define NO_DSHOW_STRSAFE
  28. #include <dshow.h>
  29. #include <dvdmedia.h>
  30. /* EC_DEVICE_LOST is not defined in MinGW dshow headers. */
  31. #ifndef EC_DEVICE_LOST
  32. #define EC_DEVICE_LOST 0x1f
  33. #endif
  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. #if DSHOWDEBUG
  40. extern const AVClass *ff_dshow_context_class_ptr;
  41. #define dshowdebug(...) av_log(&ff_dshow_context_class_ptr, AV_LOG_DEBUG, __VA_ARGS__)
  42. #else
  43. #define dshowdebug(...)
  44. #endif
  45. static inline void nothing(void *foo)
  46. {
  47. }
  48. struct GUIDoffset {
  49. const GUID *iid;
  50. int offset;
  51. };
  52. enum dshowDeviceType {
  53. VideoDevice = 0,
  54. AudioDevice = 1,
  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. #define DECLARE_DESTROY(class, func) \
  97. void class##_Destroy(class *this) \
  98. { \
  99. dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this); \
  100. func(this); \
  101. if (this) { \
  102. if (this->vtbl) \
  103. CoTaskMemFree(this->vtbl); \
  104. CoTaskMemFree(this); \
  105. } \
  106. }
  107. #define DECLARE_CREATE(class, setup, ...) \
  108. class *class##_Create(__VA_ARGS__) \
  109. { \
  110. class *this = CoTaskMemAlloc(sizeof(class)); \
  111. void *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
  112. dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this); \
  113. if (!this || !vtbl) \
  114. goto fail; \
  115. ZeroMemory(this, sizeof(class)); \
  116. ZeroMemory(vtbl, sizeof(*this->vtbl)); \
  117. this->ref = 1; \
  118. this->vtbl = vtbl; \
  119. if (!setup) \
  120. goto fail; \
  121. dshowdebug("created "AV_STRINGIFY(class)" %p\n", this); \
  122. return this; \
  123. fail: \
  124. class##_Destroy(this); \
  125. dshowdebug("could not create "AV_STRINGIFY(class)"\n"); \
  126. return NULL; \
  127. }
  128. #define SETVTBL(vtbl, class, fn) \
  129. do { (vtbl)->fn = (void *) class##_##fn; } while(0)
  130. /*****************************************************************************
  131. * Forward Declarations
  132. ****************************************************************************/
  133. typedef struct libAVPin libAVPin;
  134. typedef struct libAVMemInputPin libAVMemInputPin;
  135. typedef struct libAVEnumPins libAVEnumPins;
  136. typedef struct libAVEnumMediaTypes libAVEnumMediaTypes;
  137. typedef struct libAVFilter libAVFilter;
  138. /*****************************************************************************
  139. * libAVPin
  140. ****************************************************************************/
  141. struct libAVPin {
  142. IPinVtbl *vtbl;
  143. long ref;
  144. libAVFilter *filter;
  145. IPin *connectedto;
  146. AM_MEDIA_TYPE type;
  147. IMemInputPinVtbl *imemvtbl;
  148. };
  149. long WINAPI libAVPin_QueryInterface (libAVPin *, const GUID *, void **);
  150. unsigned long WINAPI libAVPin_AddRef (libAVPin *);
  151. unsigned long WINAPI libAVPin_Release (libAVPin *);
  152. long WINAPI libAVPin_Connect (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
  153. long WINAPI libAVPin_ReceiveConnection (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
  154. long WINAPI libAVPin_Disconnect (libAVPin *);
  155. long WINAPI libAVPin_ConnectedTo (libAVPin *, IPin **);
  156. long WINAPI libAVPin_ConnectionMediaType (libAVPin *, AM_MEDIA_TYPE *);
  157. long WINAPI libAVPin_QueryPinInfo (libAVPin *, PIN_INFO *);
  158. long WINAPI libAVPin_QueryDirection (libAVPin *, PIN_DIRECTION *);
  159. long WINAPI libAVPin_QueryId (libAVPin *, wchar_t **);
  160. long WINAPI libAVPin_QueryAccept (libAVPin *, const AM_MEDIA_TYPE *);
  161. long WINAPI libAVPin_EnumMediaTypes (libAVPin *, IEnumMediaTypes **);
  162. long WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *);
  163. long WINAPI libAVPin_EndOfStream (libAVPin *);
  164. long WINAPI libAVPin_BeginFlush (libAVPin *);
  165. long WINAPI libAVPin_EndFlush (libAVPin *);
  166. long WINAPI libAVPin_NewSegment (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double);
  167. long WINAPI libAVMemInputPin_QueryInterface (libAVMemInputPin *, const GUID *, void **);
  168. unsigned long WINAPI libAVMemInputPin_AddRef (libAVMemInputPin *);
  169. unsigned long WINAPI libAVMemInputPin_Release (libAVMemInputPin *);
  170. long WINAPI libAVMemInputPin_GetAllocator (libAVMemInputPin *, IMemAllocator **);
  171. long WINAPI libAVMemInputPin_NotifyAllocator (libAVMemInputPin *, IMemAllocator *, BOOL);
  172. long WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *);
  173. long WINAPI libAVMemInputPin_Receive (libAVMemInputPin *, IMediaSample *);
  174. long WINAPI libAVMemInputPin_ReceiveMultiple (libAVMemInputPin *, IMediaSample **, long, long *);
  175. long WINAPI libAVMemInputPin_ReceiveCanBlock (libAVMemInputPin *);
  176. void libAVPin_Destroy(libAVPin *);
  177. libAVPin *libAVPin_Create (libAVFilter *filter);
  178. void libAVMemInputPin_Destroy(libAVMemInputPin *);
  179. /*****************************************************************************
  180. * libAVEnumPins
  181. ****************************************************************************/
  182. struct libAVEnumPins {
  183. IEnumPinsVtbl *vtbl;
  184. long ref;
  185. int pos;
  186. libAVPin *pin;
  187. libAVFilter *filter;
  188. };
  189. long WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **);
  190. unsigned long WINAPI libAVEnumPins_AddRef (libAVEnumPins *);
  191. unsigned long WINAPI libAVEnumPins_Release (libAVEnumPins *);
  192. long WINAPI libAVEnumPins_Next (libAVEnumPins *, unsigned long, IPin **, unsigned long *);
  193. long WINAPI libAVEnumPins_Skip (libAVEnumPins *, unsigned long);
  194. long WINAPI libAVEnumPins_Reset (libAVEnumPins *);
  195. long WINAPI libAVEnumPins_Clone (libAVEnumPins *, libAVEnumPins **);
  196. void libAVEnumPins_Destroy(libAVEnumPins *);
  197. libAVEnumPins *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter);
  198. /*****************************************************************************
  199. * libAVEnumMediaTypes
  200. ****************************************************************************/
  201. struct libAVEnumMediaTypes {
  202. IEnumPinsVtbl *vtbl;
  203. long ref;
  204. int pos;
  205. AM_MEDIA_TYPE type;
  206. };
  207. long WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **);
  208. unsigned long WINAPI libAVEnumMediaTypes_AddRef (libAVEnumMediaTypes *);
  209. unsigned long WINAPI libAVEnumMediaTypes_Release (libAVEnumMediaTypes *);
  210. long WINAPI libAVEnumMediaTypes_Next (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
  211. long WINAPI libAVEnumMediaTypes_Skip (libAVEnumMediaTypes *, unsigned long);
  212. long WINAPI libAVEnumMediaTypes_Reset (libAVEnumMediaTypes *);
  213. long WINAPI libAVEnumMediaTypes_Clone (libAVEnumMediaTypes *, libAVEnumMediaTypes **);
  214. void libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *);
  215. libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type);
  216. /*****************************************************************************
  217. * libAVFilter
  218. ****************************************************************************/
  219. struct libAVFilter {
  220. IBaseFilterVtbl *vtbl;
  221. long ref;
  222. const wchar_t *name;
  223. libAVPin *pin;
  224. FILTER_INFO info;
  225. FILTER_STATE state;
  226. IReferenceClock *clock;
  227. enum dshowDeviceType type;
  228. void *priv_data;
  229. int stream_index;
  230. int64_t start_time;
  231. void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType type);
  232. };
  233. long WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **);
  234. unsigned long WINAPI libAVFilter_AddRef (libAVFilter *);
  235. unsigned long WINAPI libAVFilter_Release (libAVFilter *);
  236. long WINAPI libAVFilter_GetClassID (libAVFilter *, CLSID *);
  237. long WINAPI libAVFilter_Stop (libAVFilter *);
  238. long WINAPI libAVFilter_Pause (libAVFilter *);
  239. long WINAPI libAVFilter_Run (libAVFilter *, REFERENCE_TIME);
  240. long WINAPI libAVFilter_GetState (libAVFilter *, DWORD, FILTER_STATE *);
  241. long WINAPI libAVFilter_SetSyncSource (libAVFilter *, IReferenceClock *);
  242. long WINAPI libAVFilter_GetSyncSource (libAVFilter *, IReferenceClock **);
  243. long WINAPI libAVFilter_EnumPins (libAVFilter *, IEnumPins **);
  244. long WINAPI libAVFilter_FindPin (libAVFilter *, const wchar_t *, IPin **);
  245. long WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *);
  246. long WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *);
  247. long WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **);
  248. void libAVFilter_Destroy(libAVFilter *);
  249. libAVFilter *libAVFilter_Create (void *, void *, enum dshowDeviceType);
  250. #endif /* AVDEVICE_DSHOW_H */