dshow_capture.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Create(%p)\n", this); \
  114. if (!this) \
  115. goto fail; \
  116. ZeroMemory(this, sizeof(class)); \
  117. this->vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
  118. if (!this->vtbl) \
  119. goto fail; \
  120. ZeroMemory(this->vtbl, sizeof(*this->vtbl)); \
  121. this->ref = 1; \
  122. if (!setup) \
  123. goto fail; \
  124. dshowdebug("created ff_dshow_"AV_STRINGIFY(prefix)" %p\n", this); \
  125. return this; \
  126. fail: \
  127. ff_dshow_##prefix##_Destroy(this); \
  128. dshowdebug("could not create ff_dshow_"AV_STRINGIFY(prefix)"\n"); \
  129. return NULL; \
  130. }
  131. #define SETVTBL(vtbl, prefix, fn) \
  132. do { (vtbl)->fn = (void *) ff_dshow_##prefix##_##fn; } while(0)
  133. /*****************************************************************************
  134. * Forward Declarations
  135. ****************************************************************************/
  136. typedef struct DShowPin DShowPin;
  137. typedef struct DShowMemInputPin DShowMemInputPin;
  138. typedef struct DShowEnumPins DShowEnumPins;
  139. typedef struct DShowEnumMediaTypes DShowEnumMediaTypes;
  140. typedef struct DShowFilter DShowFilter;
  141. /*****************************************************************************
  142. * DShowPin
  143. ****************************************************************************/
  144. struct DShowPin {
  145. IPinVtbl *vtbl;
  146. long ref;
  147. DShowFilter *filter;
  148. IPin *connectedto;
  149. AM_MEDIA_TYPE type;
  150. IMemInputPinVtbl *imemvtbl;
  151. };
  152. long WINAPI ff_dshow_pin_QueryInterface (DShowPin *, const GUID *, void **);
  153. unsigned long WINAPI ff_dshow_pin_AddRef (DShowPin *);
  154. unsigned long WINAPI ff_dshow_pin_Release (DShowPin *);
  155. long WINAPI ff_dshow_pin_Connect (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
  156. long WINAPI ff_dshow_pin_ReceiveConnection (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
  157. long WINAPI ff_dshow_pin_Disconnect (DShowPin *);
  158. long WINAPI ff_dshow_pin_ConnectedTo (DShowPin *, IPin **);
  159. long WINAPI ff_dshow_pin_ConnectionMediaType (DShowPin *, AM_MEDIA_TYPE *);
  160. long WINAPI ff_dshow_pin_QueryPinInfo (DShowPin *, PIN_INFO *);
  161. long WINAPI ff_dshow_pin_QueryDirection (DShowPin *, PIN_DIRECTION *);
  162. long WINAPI ff_dshow_pin_QueryId (DShowPin *, wchar_t **);
  163. long WINAPI ff_dshow_pin_QueryAccept (DShowPin *, const AM_MEDIA_TYPE *);
  164. long WINAPI ff_dshow_pin_EnumMediaTypes (DShowPin *, IEnumMediaTypes **);
  165. long WINAPI ff_dshow_pin_QueryInternalConnections(DShowPin *, IPin **, unsigned long *);
  166. long WINAPI ff_dshow_pin_EndOfStream (DShowPin *);
  167. long WINAPI ff_dshow_pin_BeginFlush (DShowPin *);
  168. long WINAPI ff_dshow_pin_EndFlush (DShowPin *);
  169. long WINAPI ff_dshow_pin_NewSegment (DShowPin *, REFERENCE_TIME, REFERENCE_TIME, double);
  170. long WINAPI ff_dshow_meminputpin_QueryInterface (DShowMemInputPin *, const GUID *, void **);
  171. unsigned long WINAPI ff_dshow_meminputpin_AddRef (DShowMemInputPin *);
  172. unsigned long WINAPI ff_dshow_meminputpin_Release (DShowMemInputPin *);
  173. long WINAPI ff_dshow_meminputpin_GetAllocator (DShowMemInputPin *, IMemAllocator **);
  174. long WINAPI ff_dshow_meminputpin_NotifyAllocator (DShowMemInputPin *, IMemAllocator *, BOOL);
  175. long WINAPI ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *, ALLOCATOR_PROPERTIES *);
  176. long WINAPI ff_dshow_meminputpin_Receive (DShowMemInputPin *, IMediaSample *);
  177. long WINAPI ff_dshow_meminputpin_ReceiveMultiple (DShowMemInputPin *, IMediaSample **, long, long *);
  178. long WINAPI ff_dshow_meminputpin_ReceiveCanBlock (DShowMemInputPin *);
  179. void ff_dshow_pin_Destroy(DShowPin *);
  180. DShowPin *ff_dshow_pin_Create (DShowFilter *filter);
  181. void ff_dshow_meminputpin_Destroy(DShowMemInputPin *);
  182. /*****************************************************************************
  183. * DShowEnumPins
  184. ****************************************************************************/
  185. struct DShowEnumPins {
  186. IEnumPinsVtbl *vtbl;
  187. long ref;
  188. int pos;
  189. DShowPin *pin;
  190. DShowFilter *filter;
  191. };
  192. long WINAPI ff_dshow_enumpins_QueryInterface(DShowEnumPins *, const GUID *, void **);
  193. unsigned long WINAPI ff_dshow_enumpins_AddRef (DShowEnumPins *);
  194. unsigned long WINAPI ff_dshow_enumpins_Release (DShowEnumPins *);
  195. long WINAPI ff_dshow_enumpins_Next (DShowEnumPins *, unsigned long, IPin **, unsigned long *);
  196. long WINAPI ff_dshow_enumpins_Skip (DShowEnumPins *, unsigned long);
  197. long WINAPI ff_dshow_enumpins_Reset (DShowEnumPins *);
  198. long WINAPI ff_dshow_enumpins_Clone (DShowEnumPins *, DShowEnumPins **);
  199. void ff_dshow_enumpins_Destroy(DShowEnumPins *);
  200. DShowEnumPins *ff_dshow_enumpins_Create (DShowPin *pin, DShowFilter *filter);
  201. /*****************************************************************************
  202. * DShowEnumMediaTypes
  203. ****************************************************************************/
  204. struct DShowEnumMediaTypes {
  205. IEnumMediaTypesVtbl *vtbl;
  206. long ref;
  207. int pos;
  208. AM_MEDIA_TYPE type;
  209. };
  210. long WINAPI ff_dshow_enummediatypes_QueryInterface(DShowEnumMediaTypes *, const GUID *, void **);
  211. unsigned long WINAPI ff_dshow_enummediatypes_AddRef (DShowEnumMediaTypes *);
  212. unsigned long WINAPI ff_dshow_enummediatypes_Release (DShowEnumMediaTypes *);
  213. long WINAPI ff_dshow_enummediatypes_Next (DShowEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
  214. long WINAPI ff_dshow_enummediatypes_Skip (DShowEnumMediaTypes *, unsigned long);
  215. long WINAPI ff_dshow_enummediatypes_Reset (DShowEnumMediaTypes *);
  216. long WINAPI ff_dshow_enummediatypes_Clone (DShowEnumMediaTypes *, DShowEnumMediaTypes **);
  217. void ff_dshow_enummediatypes_Destroy(DShowEnumMediaTypes *);
  218. DShowEnumMediaTypes *ff_dshow_enummediatypes_Create(const AM_MEDIA_TYPE *type);
  219. /*****************************************************************************
  220. * DShowFilter
  221. ****************************************************************************/
  222. struct DShowFilter {
  223. IBaseFilterVtbl *vtbl;
  224. long ref;
  225. const wchar_t *name;
  226. DShowPin *pin;
  227. FILTER_INFO info;
  228. FILTER_STATE state;
  229. IReferenceClock *clock;
  230. enum dshowDeviceType type;
  231. void *priv_data;
  232. int stream_index;
  233. int64_t start_time;
  234. void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType type);
  235. };
  236. long WINAPI ff_dshow_filter_QueryInterface (DShowFilter *, const GUID *, void **);
  237. unsigned long WINAPI ff_dshow_filter_AddRef (DShowFilter *);
  238. unsigned long WINAPI ff_dshow_filter_Release (DShowFilter *);
  239. long WINAPI ff_dshow_filter_GetClassID (DShowFilter *, CLSID *);
  240. long WINAPI ff_dshow_filter_Stop (DShowFilter *);
  241. long WINAPI ff_dshow_filter_Pause (DShowFilter *);
  242. long WINAPI ff_dshow_filter_Run (DShowFilter *, REFERENCE_TIME);
  243. long WINAPI ff_dshow_filter_GetState (DShowFilter *, DWORD, FILTER_STATE *);
  244. long WINAPI ff_dshow_filter_SetSyncSource (DShowFilter *, IReferenceClock *);
  245. long WINAPI ff_dshow_filter_GetSyncSource (DShowFilter *, IReferenceClock **);
  246. long WINAPI ff_dshow_filter_EnumPins (DShowFilter *, IEnumPins **);
  247. long WINAPI ff_dshow_filter_FindPin (DShowFilter *, const wchar_t *, IPin **);
  248. long WINAPI ff_dshow_filter_QueryFilterInfo(DShowFilter *, FILTER_INFO *);
  249. long WINAPI ff_dshow_filter_JoinFilterGraph(DShowFilter *, IFilterGraph *, const wchar_t *);
  250. long WINAPI ff_dshow_filter_QueryVendorInfo(DShowFilter *, wchar_t **);
  251. void ff_dshow_filter_Destroy(DShowFilter *);
  252. DShowFilter *ff_dshow_filter_Create (void *, void *, enum dshowDeviceType);
  253. /*****************************************************************************
  254. * dshow_ctx
  255. ****************************************************************************/
  256. struct dshow_ctx {
  257. const AVClass *class;
  258. IGraphBuilder *graph;
  259. char *device_name[2];
  260. char *device_unique_name[2];
  261. int video_device_number;
  262. int audio_device_number;
  263. int list_options;
  264. int list_devices;
  265. int audio_buffer_size;
  266. int crossbar_video_input_pin_number;
  267. int crossbar_audio_input_pin_number;
  268. char *video_pin_name;
  269. char *audio_pin_name;
  270. int show_video_device_dialog;
  271. int show_audio_device_dialog;
  272. int show_video_crossbar_connection_dialog;
  273. int show_audio_crossbar_connection_dialog;
  274. int show_analog_tv_tuner_dialog;
  275. int show_analog_tv_tuner_audio_dialog;
  276. char *audio_filter_load_file;
  277. char *audio_filter_save_file;
  278. char *video_filter_load_file;
  279. char *video_filter_save_file;
  280. int use_video_device_timestamps;
  281. IBaseFilter *device_filter[2];
  282. IPin *device_pin[2];
  283. DShowFilter *capture_filter[2];
  284. DShowPin *capture_pin[2];
  285. HANDLE mutex;
  286. HANDLE event[2]; /* event[0] is set by DirectShow
  287. * event[1] is set by callback() */
  288. PacketListEntry *pktl;
  289. int eof;
  290. int64_t curbufsize[2];
  291. unsigned int video_frame_num;
  292. IMediaControl *control;
  293. IMediaEvent *media_event;
  294. enum AVPixelFormat pixel_format;
  295. enum AVCodecID video_codec_id;
  296. char *framerate;
  297. int requested_width;
  298. int requested_height;
  299. AVRational requested_framerate;
  300. int sample_rate;
  301. int sample_size;
  302. int channels;
  303. };
  304. /*****************************************************************************
  305. * CrossBar
  306. ****************************************************************************/
  307. HRESULT ff_dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
  308. IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx);
  309. void ff_dshow_show_filter_properties(IBaseFilter *pFilter, AVFormatContext *avctx);
  310. #endif /* AVDEVICE_DSHOW_CAPTURE_H */