dshow.h 14 KB

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