dshow_capture.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. enum dshowSourceFilterType {
  58. VideoSourceDevice = 0,
  59. AudioSourceDevice = 1,
  60. };
  61. #define DECLARE_QUERYINTERFACE(class, ...) \
  62. long WINAPI \
  63. class##_QueryInterface(class *this, const GUID *riid, void **ppvObject) \
  64. { \
  65. struct GUIDoffset ifaces[] = __VA_ARGS__; \
  66. int i; \
  67. dshowdebug(AV_STRINGIFY(class)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
  68. ff_printGUID(riid); \
  69. if (!ppvObject) \
  70. return E_POINTER; \
  71. for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) { \
  72. if (IsEqualGUID(riid, ifaces[i].iid)) { \
  73. void *obj = (void *) ((uint8_t *) this + ifaces[i].offset); \
  74. class##_AddRef(this); \
  75. dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset); \
  76. *ppvObject = (void *) obj; \
  77. return S_OK; \
  78. } \
  79. } \
  80. dshowdebug("\tE_NOINTERFACE\n"); \
  81. *ppvObject = NULL; \
  82. return E_NOINTERFACE; \
  83. }
  84. #define DECLARE_ADDREF(class) \
  85. unsigned long WINAPI \
  86. class##_AddRef(class *this) \
  87. { \
  88. dshowdebug(AV_STRINGIFY(class)"_AddRef(%p)\t%ld\n", this, this->ref+1); \
  89. return InterlockedIncrement(&this->ref); \
  90. }
  91. #define DECLARE_RELEASE(class) \
  92. unsigned long WINAPI \
  93. class##_Release(class *this) \
  94. { \
  95. long ref = InterlockedDecrement(&this->ref); \
  96. dshowdebug(AV_STRINGIFY(class)"_Release(%p)\t%ld\n", this, ref); \
  97. if (!ref) \
  98. class##_Destroy(this); \
  99. return ref; \
  100. }
  101. #define DECLARE_DESTROY(class, func) \
  102. void class##_Destroy(class *this) \
  103. { \
  104. dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this); \
  105. func(this); \
  106. if (this) { \
  107. if (this->vtbl) \
  108. CoTaskMemFree(this->vtbl); \
  109. CoTaskMemFree(this); \
  110. } \
  111. }
  112. #define DECLARE_CREATE(class, setup, ...) \
  113. class *class##_Create(__VA_ARGS__) \
  114. { \
  115. class *this = CoTaskMemAlloc(sizeof(class)); \
  116. void *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
  117. dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this); \
  118. if (!this || !vtbl) \
  119. goto fail; \
  120. ZeroMemory(this, sizeof(class)); \
  121. ZeroMemory(vtbl, sizeof(*this->vtbl)); \
  122. this->ref = 1; \
  123. this->vtbl = vtbl; \
  124. if (!setup) \
  125. goto fail; \
  126. dshowdebug("created "AV_STRINGIFY(class)" %p\n", this); \
  127. return this; \
  128. fail: \
  129. class##_Destroy(this); \
  130. dshowdebug("could not create "AV_STRINGIFY(class)"\n"); \
  131. return NULL; \
  132. }
  133. #define SETVTBL(vtbl, class, fn) \
  134. do { (vtbl)->fn = (void *) class##_##fn; } while(0)
  135. /*****************************************************************************
  136. * Forward Declarations
  137. ****************************************************************************/
  138. typedef struct libAVPin libAVPin;
  139. typedef struct libAVMemInputPin libAVMemInputPin;
  140. typedef struct libAVEnumPins libAVEnumPins;
  141. typedef struct libAVEnumMediaTypes libAVEnumMediaTypes;
  142. typedef struct libAVFilter libAVFilter;
  143. /*****************************************************************************
  144. * libAVPin
  145. ****************************************************************************/
  146. struct libAVPin {
  147. IPinVtbl *vtbl;
  148. long ref;
  149. libAVFilter *filter;
  150. IPin *connectedto;
  151. AM_MEDIA_TYPE type;
  152. IMemInputPinVtbl *imemvtbl;
  153. };
  154. long WINAPI libAVPin_QueryInterface (libAVPin *, const GUID *, void **);
  155. unsigned long WINAPI libAVPin_AddRef (libAVPin *);
  156. unsigned long WINAPI libAVPin_Release (libAVPin *);
  157. long WINAPI libAVPin_Connect (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
  158. long WINAPI libAVPin_ReceiveConnection (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
  159. long WINAPI libAVPin_Disconnect (libAVPin *);
  160. long WINAPI libAVPin_ConnectedTo (libAVPin *, IPin **);
  161. long WINAPI libAVPin_ConnectionMediaType (libAVPin *, AM_MEDIA_TYPE *);
  162. long WINAPI libAVPin_QueryPinInfo (libAVPin *, PIN_INFO *);
  163. long WINAPI libAVPin_QueryDirection (libAVPin *, PIN_DIRECTION *);
  164. long WINAPI libAVPin_QueryId (libAVPin *, wchar_t **);
  165. long WINAPI libAVPin_QueryAccept (libAVPin *, const AM_MEDIA_TYPE *);
  166. long WINAPI libAVPin_EnumMediaTypes (libAVPin *, IEnumMediaTypes **);
  167. long WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *);
  168. long WINAPI libAVPin_EndOfStream (libAVPin *);
  169. long WINAPI libAVPin_BeginFlush (libAVPin *);
  170. long WINAPI libAVPin_EndFlush (libAVPin *);
  171. long WINAPI libAVPin_NewSegment (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double);
  172. long WINAPI libAVMemInputPin_QueryInterface (libAVMemInputPin *, const GUID *, void **);
  173. unsigned long WINAPI libAVMemInputPin_AddRef (libAVMemInputPin *);
  174. unsigned long WINAPI libAVMemInputPin_Release (libAVMemInputPin *);
  175. long WINAPI libAVMemInputPin_GetAllocator (libAVMemInputPin *, IMemAllocator **);
  176. long WINAPI libAVMemInputPin_NotifyAllocator (libAVMemInputPin *, IMemAllocator *, BOOL);
  177. long WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *);
  178. long WINAPI libAVMemInputPin_Receive (libAVMemInputPin *, IMediaSample *);
  179. long WINAPI libAVMemInputPin_ReceiveMultiple (libAVMemInputPin *, IMediaSample **, long, long *);
  180. long WINAPI libAVMemInputPin_ReceiveCanBlock (libAVMemInputPin *);
  181. void libAVPin_Destroy(libAVPin *);
  182. libAVPin *libAVPin_Create (libAVFilter *filter);
  183. void libAVMemInputPin_Destroy(libAVMemInputPin *);
  184. /*****************************************************************************
  185. * libAVEnumPins
  186. ****************************************************************************/
  187. struct libAVEnumPins {
  188. IEnumPinsVtbl *vtbl;
  189. long ref;
  190. int pos;
  191. libAVPin *pin;
  192. libAVFilter *filter;
  193. };
  194. long WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **);
  195. unsigned long WINAPI libAVEnumPins_AddRef (libAVEnumPins *);
  196. unsigned long WINAPI libAVEnumPins_Release (libAVEnumPins *);
  197. long WINAPI libAVEnumPins_Next (libAVEnumPins *, unsigned long, IPin **, unsigned long *);
  198. long WINAPI libAVEnumPins_Skip (libAVEnumPins *, unsigned long);
  199. long WINAPI libAVEnumPins_Reset (libAVEnumPins *);
  200. long WINAPI libAVEnumPins_Clone (libAVEnumPins *, libAVEnumPins **);
  201. void libAVEnumPins_Destroy(libAVEnumPins *);
  202. libAVEnumPins *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter);
  203. /*****************************************************************************
  204. * libAVEnumMediaTypes
  205. ****************************************************************************/
  206. struct libAVEnumMediaTypes {
  207. IEnumMediaTypesVtbl *vtbl;
  208. long ref;
  209. int pos;
  210. AM_MEDIA_TYPE type;
  211. };
  212. long WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **);
  213. unsigned long WINAPI libAVEnumMediaTypes_AddRef (libAVEnumMediaTypes *);
  214. unsigned long WINAPI libAVEnumMediaTypes_Release (libAVEnumMediaTypes *);
  215. long WINAPI libAVEnumMediaTypes_Next (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
  216. long WINAPI libAVEnumMediaTypes_Skip (libAVEnumMediaTypes *, unsigned long);
  217. long WINAPI libAVEnumMediaTypes_Reset (libAVEnumMediaTypes *);
  218. long WINAPI libAVEnumMediaTypes_Clone (libAVEnumMediaTypes *, libAVEnumMediaTypes **);
  219. void libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *);
  220. libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type);
  221. /*****************************************************************************
  222. * libAVFilter
  223. ****************************************************************************/
  224. struct libAVFilter {
  225. IBaseFilterVtbl *vtbl;
  226. long ref;
  227. const wchar_t *name;
  228. libAVPin *pin;
  229. FILTER_INFO info;
  230. FILTER_STATE state;
  231. IReferenceClock *clock;
  232. enum dshowDeviceType type;
  233. void *priv_data;
  234. int stream_index;
  235. int64_t start_time;
  236. void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType type);
  237. };
  238. long WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **);
  239. unsigned long WINAPI libAVFilter_AddRef (libAVFilter *);
  240. unsigned long WINAPI libAVFilter_Release (libAVFilter *);
  241. long WINAPI libAVFilter_GetClassID (libAVFilter *, CLSID *);
  242. long WINAPI libAVFilter_Stop (libAVFilter *);
  243. long WINAPI libAVFilter_Pause (libAVFilter *);
  244. long WINAPI libAVFilter_Run (libAVFilter *, REFERENCE_TIME);
  245. long WINAPI libAVFilter_GetState (libAVFilter *, DWORD, FILTER_STATE *);
  246. long WINAPI libAVFilter_SetSyncSource (libAVFilter *, IReferenceClock *);
  247. long WINAPI libAVFilter_GetSyncSource (libAVFilter *, IReferenceClock **);
  248. long WINAPI libAVFilter_EnumPins (libAVFilter *, IEnumPins **);
  249. long WINAPI libAVFilter_FindPin (libAVFilter *, const wchar_t *, IPin **);
  250. long WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *);
  251. long WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *);
  252. long WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **);
  253. void libAVFilter_Destroy(libAVFilter *);
  254. libAVFilter *libAVFilter_Create (void *, void *, enum dshowDeviceType);
  255. /*****************************************************************************
  256. * dshow_ctx
  257. ****************************************************************************/
  258. struct dshow_ctx {
  259. const AVClass *class;
  260. IGraphBuilder *graph;
  261. char *device_name[2];
  262. int video_device_number;
  263. int audio_device_number;
  264. int list_options;
  265. int list_devices;
  266. int audio_buffer_size;
  267. int crossbar_video_input_pin_number;
  268. int crossbar_audio_input_pin_number;
  269. char *video_pin_name;
  270. char *audio_pin_name;
  271. int show_video_device_dialog;
  272. int show_audio_device_dialog;
  273. int show_video_crossbar_connection_dialog;
  274. int show_audio_crossbar_connection_dialog;
  275. int show_analog_tv_tuner_dialog;
  276. int show_analog_tv_tuner_audio_dialog;
  277. IBaseFilter *device_filter[2];
  278. IPin *device_pin[2];
  279. libAVFilter *capture_filter[2];
  280. libAVPin *capture_pin[2];
  281. HANDLE mutex;
  282. HANDLE event[2]; /* event[0] is set by DirectShow
  283. * event[1] is set by callback() */
  284. AVPacketList *pktl;
  285. int eof;
  286. int64_t curbufsize[2];
  287. unsigned int video_frame_num;
  288. IMediaControl *control;
  289. IMediaEvent *media_event;
  290. enum AVPixelFormat pixel_format;
  291. enum AVCodecID video_codec_id;
  292. char *framerate;
  293. int requested_width;
  294. int requested_height;
  295. AVRational requested_framerate;
  296. int sample_rate;
  297. int sample_size;
  298. int channels;
  299. };
  300. /*****************************************************************************
  301. * CrossBar
  302. ****************************************************************************/
  303. HRESULT dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
  304. IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx);
  305. void dshow_show_filter_properties(IBaseFilter *pFilter, AVFormatContext *avctx);
  306. #endif /* AVDEVICE_DSHOW_H */