dshow_capture.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #include <windows.h>
  27. #define NO_DSHOW_STRSAFE
  28. #include <dshow.h>
  29. #include <dvdmedia.h>
  30. #include "libavcodec/internal.h"
  31. #include "libavcodec/packet_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(prefix, class, ...) \
  59. long WINAPI \
  60. ff_dshow_##prefix##_QueryInterface(class *this, const GUID *riid, void **ppvObject) \
  61. { \
  62. struct GUIDoffset ifaces[] = __VA_ARGS__; \
  63. int i; \
  64. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_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. ff_dshow_##prefix##_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(prefix, class) \
  82. unsigned long WINAPI \
  83. ff_dshow_##prefix##_AddRef(class *this) \
  84. { \
  85. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_AddRef(%p)\t%ld\n", this, this->ref+1); \
  86. return InterlockedIncrement(&this->ref); \
  87. }
  88. #define DECLARE_RELEASE(prefix, class) \
  89. unsigned long WINAPI \
  90. ff_dshow_##prefix##_Release(class *this) \
  91. { \
  92. long ref = InterlockedDecrement(&this->ref); \
  93. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Release(%p)\t%ld\n", this, ref); \
  94. if (!ref) \
  95. ff_dshow_##prefix##_Destroy(this); \
  96. return ref; \
  97. }
  98. #define DECLARE_DESTROY(prefix, class, func) \
  99. void ff_dshow_##prefix##_Destroy(class *this) \
  100. { \
  101. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_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(prefix, class, setup, ...) \
  110. class *ff_dshow_##prefix##_Create(__VA_ARGS__) \
  111. { \
  112. class *this = CoTaskMemAlloc(sizeof(class)); \
  113. void *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
  114. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_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 ff_dshow_"AV_STRINGIFY(prefix)" %p\n", this); \
  124. return this; \
  125. fail: \
  126. ff_dshow_##prefix##_Destroy(this); \
  127. dshowdebug("could not create ff_dshow_"AV_STRINGIFY(prefix)"\n"); \
  128. return NULL; \
  129. }
  130. #define SETVTBL(vtbl, prefix, fn) \
  131. do { (vtbl)->fn = (void *) ff_dshow_##prefix##_##fn; } while(0)
  132. /*****************************************************************************
  133. * Forward Declarations
  134. ****************************************************************************/
  135. typedef struct DShowPin DShowPin;
  136. typedef struct DShowMemInputPin DShowMemInputPin;
  137. typedef struct DShowEnumPins DShowEnumPins;
  138. typedef struct DShowEnumMediaTypes DShowEnumMediaTypes;
  139. typedef struct DShowFilter DShowFilter;
  140. /*****************************************************************************
  141. * DShowPin
  142. ****************************************************************************/
  143. struct DShowPin {
  144. IPinVtbl *vtbl;
  145. long ref;
  146. DShowFilter *filter;
  147. IPin *connectedto;
  148. AM_MEDIA_TYPE type;
  149. IMemInputPinVtbl *imemvtbl;
  150. };
  151. long WINAPI ff_dshow_pin_QueryInterface (DShowPin *, const GUID *, void **);
  152. unsigned long WINAPI ff_dshow_pin_AddRef (DShowPin *);
  153. unsigned long WINAPI ff_dshow_pin_Release (DShowPin *);
  154. long WINAPI ff_dshow_pin_Connect (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
  155. long WINAPI ff_dshow_pin_ReceiveConnection (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
  156. long WINAPI ff_dshow_pin_Disconnect (DShowPin *);
  157. long WINAPI ff_dshow_pin_ConnectedTo (DShowPin *, IPin **);
  158. long WINAPI ff_dshow_pin_ConnectionMediaType (DShowPin *, AM_MEDIA_TYPE *);
  159. long WINAPI ff_dshow_pin_QueryPinInfo (DShowPin *, PIN_INFO *);
  160. long WINAPI ff_dshow_pin_QueryDirection (DShowPin *, PIN_DIRECTION *);
  161. long WINAPI ff_dshow_pin_QueryId (DShowPin *, wchar_t **);
  162. long WINAPI ff_dshow_pin_QueryAccept (DShowPin *, const AM_MEDIA_TYPE *);
  163. long WINAPI ff_dshow_pin_EnumMediaTypes (DShowPin *, IEnumMediaTypes **);
  164. long WINAPI ff_dshow_pin_QueryInternalConnections(DShowPin *, IPin **, unsigned long *);
  165. long WINAPI ff_dshow_pin_EndOfStream (DShowPin *);
  166. long WINAPI ff_dshow_pin_BeginFlush (DShowPin *);
  167. long WINAPI ff_dshow_pin_EndFlush (DShowPin *);
  168. long WINAPI ff_dshow_pin_NewSegment (DShowPin *, REFERENCE_TIME, REFERENCE_TIME, double);
  169. long WINAPI ff_dshow_meminputpin_QueryInterface (DShowMemInputPin *, const GUID *, void **);
  170. unsigned long WINAPI ff_dshow_meminputpin_AddRef (DShowMemInputPin *);
  171. unsigned long WINAPI ff_dshow_meminputpin_Release (DShowMemInputPin *);
  172. long WINAPI ff_dshow_meminputpin_GetAllocator (DShowMemInputPin *, IMemAllocator **);
  173. long WINAPI ff_dshow_meminputpin_NotifyAllocator (DShowMemInputPin *, IMemAllocator *, BOOL);
  174. long WINAPI ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *, ALLOCATOR_PROPERTIES *);
  175. long WINAPI ff_dshow_meminputpin_Receive (DShowMemInputPin *, IMediaSample *);
  176. long WINAPI ff_dshow_meminputpin_ReceiveMultiple (DShowMemInputPin *, IMediaSample **, long, long *);
  177. long WINAPI ff_dshow_meminputpin_ReceiveCanBlock (DShowMemInputPin *);
  178. void ff_dshow_pin_Destroy(DShowPin *);
  179. DShowPin *ff_dshow_pin_Create (DShowFilter *filter);
  180. void ff_dshow_meminputpin_Destroy(DShowMemInputPin *);
  181. /*****************************************************************************
  182. * DShowEnumPins
  183. ****************************************************************************/
  184. struct DShowEnumPins {
  185. IEnumPinsVtbl *vtbl;
  186. long ref;
  187. int pos;
  188. DShowPin *pin;
  189. DShowFilter *filter;
  190. };
  191. long WINAPI ff_dshow_enumpins_QueryInterface(DShowEnumPins *, const GUID *, void **);
  192. unsigned long WINAPI ff_dshow_enumpins_AddRef (DShowEnumPins *);
  193. unsigned long WINAPI ff_dshow_enumpins_Release (DShowEnumPins *);
  194. long WINAPI ff_dshow_enumpins_Next (DShowEnumPins *, unsigned long, IPin **, unsigned long *);
  195. long WINAPI ff_dshow_enumpins_Skip (DShowEnumPins *, unsigned long);
  196. long WINAPI ff_dshow_enumpins_Reset (DShowEnumPins *);
  197. long WINAPI ff_dshow_enumpins_Clone (DShowEnumPins *, DShowEnumPins **);
  198. void ff_dshow_enumpins_Destroy(DShowEnumPins *);
  199. DShowEnumPins *ff_dshow_enumpins_Create (DShowPin *pin, DShowFilter *filter);
  200. /*****************************************************************************
  201. * DShowEnumMediaTypes
  202. ****************************************************************************/
  203. struct DShowEnumMediaTypes {
  204. IEnumMediaTypesVtbl *vtbl;
  205. long ref;
  206. int pos;
  207. AM_MEDIA_TYPE type;
  208. };
  209. long WINAPI ff_dshow_enummediatypes_QueryInterface(DShowEnumMediaTypes *, const GUID *, void **);
  210. unsigned long WINAPI ff_dshow_enummediatypes_AddRef (DShowEnumMediaTypes *);
  211. unsigned long WINAPI ff_dshow_enummediatypes_Release (DShowEnumMediaTypes *);
  212. long WINAPI ff_dshow_enummediatypes_Next (DShowEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
  213. long WINAPI ff_dshow_enummediatypes_Skip (DShowEnumMediaTypes *, unsigned long);
  214. long WINAPI ff_dshow_enummediatypes_Reset (DShowEnumMediaTypes *);
  215. long WINAPI ff_dshow_enummediatypes_Clone (DShowEnumMediaTypes *, DShowEnumMediaTypes **);
  216. void ff_dshow_enummediatypes_Destroy(DShowEnumMediaTypes *);
  217. DShowEnumMediaTypes *ff_dshow_enummediatypes_Create(const AM_MEDIA_TYPE *type);
  218. /*****************************************************************************
  219. * DShowFilter
  220. ****************************************************************************/
  221. struct DShowFilter {
  222. IBaseFilterVtbl *vtbl;
  223. long ref;
  224. const wchar_t *name;
  225. DShowPin *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 ff_dshow_filter_QueryInterface (DShowFilter *, const GUID *, void **);
  236. unsigned long WINAPI ff_dshow_filter_AddRef (DShowFilter *);
  237. unsigned long WINAPI ff_dshow_filter_Release (DShowFilter *);
  238. long WINAPI ff_dshow_filter_GetClassID (DShowFilter *, CLSID *);
  239. long WINAPI ff_dshow_filter_Stop (DShowFilter *);
  240. long WINAPI ff_dshow_filter_Pause (DShowFilter *);
  241. long WINAPI ff_dshow_filter_Run (DShowFilter *, REFERENCE_TIME);
  242. long WINAPI ff_dshow_filter_GetState (DShowFilter *, DWORD, FILTER_STATE *);
  243. long WINAPI ff_dshow_filter_SetSyncSource (DShowFilter *, IReferenceClock *);
  244. long WINAPI ff_dshow_filter_GetSyncSource (DShowFilter *, IReferenceClock **);
  245. long WINAPI ff_dshow_filter_EnumPins (DShowFilter *, IEnumPins **);
  246. long WINAPI ff_dshow_filter_FindPin (DShowFilter *, const wchar_t *, IPin **);
  247. long WINAPI ff_dshow_filter_QueryFilterInfo(DShowFilter *, FILTER_INFO *);
  248. long WINAPI ff_dshow_filter_JoinFilterGraph(DShowFilter *, IFilterGraph *, const wchar_t *);
  249. long WINAPI ff_dshow_filter_QueryVendorInfo(DShowFilter *, wchar_t **);
  250. void ff_dshow_filter_Destroy(DShowFilter *);
  251. DShowFilter *ff_dshow_filter_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. int use_video_device_timestamps;
  280. IBaseFilter *device_filter[2];
  281. IPin *device_pin[2];
  282. DShowFilter *capture_filter[2];
  283. DShowPin *capture_pin[2];
  284. HANDLE mutex;
  285. HANDLE event[2]; /* event[0] is set by DirectShow
  286. * event[1] is set by callback() */
  287. PacketListEntry *pktl;
  288. int eof;
  289. int64_t curbufsize[2];
  290. unsigned int video_frame_num;
  291. IMediaControl *control;
  292. IMediaEvent *media_event;
  293. enum AVPixelFormat pixel_format;
  294. enum AVCodecID video_codec_id;
  295. char *framerate;
  296. int requested_width;
  297. int requested_height;
  298. AVRational requested_framerate;
  299. int sample_rate;
  300. int sample_size;
  301. int channels;
  302. };
  303. /*****************************************************************************
  304. * CrossBar
  305. ****************************************************************************/
  306. HRESULT ff_dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
  307. IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx);
  308. void ff_dshow_show_filter_properties(IBaseFilter *pFilter, AVFormatContext *avctx);
  309. #endif /* AVDEVICE_DSHOW_CAPTURE_H */