You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
14KB

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