dshow.h 14 KB

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