dshow_capture.h 16 KB

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