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.

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