dshow_capture.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #include "libavcodec/packet_internal.h"
  33. /* EC_DEVICE_LOST is not defined in MinGW dshow headers. */
  34. #ifndef EC_DEVICE_LOST
  35. #define EC_DEVICE_LOST 0x1f
  36. #endif
  37. long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src);
  38. void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps);
  39. void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps);
  40. void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type);
  41. void ff_printGUID(const GUID *g);
  42. extern const AVClass *ff_dshow_context_class_ptr;
  43. #define dshowdebug(...) ff_dlog(&ff_dshow_context_class_ptr, __VA_ARGS__)
  44. static inline void nothing(void *foo)
  45. {
  46. }
  47. struct GUIDoffset {
  48. const GUID *iid;
  49. int offset;
  50. };
  51. enum dshowDeviceType {
  52. VideoDevice = 0,
  53. AudioDevice = 1,
  54. };
  55. enum dshowSourceFilterType {
  56. VideoSourceDevice = 0,
  57. AudioSourceDevice = 1,
  58. };
  59. #define DECLARE_QUERYINTERFACE(prefix, class, ...) \
  60. long WINAPI \
  61. ff_dshow_##prefix##_QueryInterface(class *this, const GUID *riid, void **ppvObject) \
  62. { \
  63. struct GUIDoffset ifaces[] = __VA_ARGS__; \
  64. int i; \
  65. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
  66. ff_printGUID(riid); \
  67. if (!ppvObject) \
  68. return E_POINTER; \
  69. for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) { \
  70. if (IsEqualGUID(riid, ifaces[i].iid)) { \
  71. void *obj = (void *) ((uint8_t *) this + ifaces[i].offset); \
  72. ff_dshow_##prefix##_AddRef(this); \
  73. dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset); \
  74. *ppvObject = (void *) obj; \
  75. return S_OK; \
  76. } \
  77. } \
  78. dshowdebug("\tE_NOINTERFACE\n"); \
  79. *ppvObject = NULL; \
  80. return E_NOINTERFACE; \
  81. }
  82. #define DECLARE_ADDREF(prefix, class) \
  83. unsigned long WINAPI \
  84. ff_dshow_##prefix##_AddRef(class *this) \
  85. { \
  86. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_AddRef(%p)\t%ld\n", this, this->ref+1); \
  87. return InterlockedIncrement(&this->ref); \
  88. }
  89. #define DECLARE_RELEASE(prefix, class) \
  90. unsigned long WINAPI \
  91. ff_dshow_##prefix##_Release(class *this) \
  92. { \
  93. long ref = InterlockedDecrement(&this->ref); \
  94. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Release(%p)\t%ld\n", this, ref); \
  95. if (!ref) \
  96. ff_dshow_##prefix##_Destroy(this); \
  97. return ref; \
  98. }
  99. #define DECLARE_DESTROY(prefix, class, func) \
  100. void ff_dshow_##prefix##_Destroy(class *this) \
  101. { \
  102. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Destroy(%p)\n", this); \
  103. func(this); \
  104. if (this) { \
  105. if (this->vtbl) \
  106. CoTaskMemFree(this->vtbl); \
  107. CoTaskMemFree(this); \
  108. } \
  109. }
  110. #define DECLARE_CREATE(prefix, class, setup, ...) \
  111. class *ff_dshow_##prefix##_Create(__VA_ARGS__) \
  112. { \
  113. class *this = CoTaskMemAlloc(sizeof(class)); \
  114. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Create(%p)\n", this); \
  115. if (!this) \
  116. goto fail; \
  117. ZeroMemory(this, sizeof(class)); \
  118. this->vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
  119. if (!this->vtbl) \
  120. goto fail; \
  121. ZeroMemory(this->vtbl, sizeof(*this->vtbl)); \
  122. this->ref = 1; \
  123. if (!setup) \
  124. goto fail; \
  125. dshowdebug("created ff_dshow_"AV_STRINGIFY(prefix)" %p\n", this); \
  126. return this; \
  127. fail: \
  128. ff_dshow_##prefix##_Destroy(this); \
  129. dshowdebug("could not create ff_dshow_"AV_STRINGIFY(prefix)"\n"); \
  130. return NULL; \
  131. }
  132. #define SETVTBL(vtbl, prefix, fn) \
  133. do { (vtbl)->fn = (void *) ff_dshow_##prefix##_##fn; } while(0)
  134. /*****************************************************************************
  135. * Forward Declarations
  136. ****************************************************************************/
  137. typedef struct DShowPin DShowPin;
  138. typedef struct DShowMemInputPin DShowMemInputPin;
  139. typedef struct DShowEnumPins DShowEnumPins;
  140. typedef struct DShowEnumMediaTypes DShowEnumMediaTypes;
  141. typedef struct DShowFilter DShowFilter;
  142. /*****************************************************************************
  143. * DShowPin
  144. ****************************************************************************/
  145. struct DShowPin {
  146. IPinVtbl *vtbl;
  147. long ref;
  148. DShowFilter *filter;
  149. IPin *connectedto;
  150. AM_MEDIA_TYPE type;
  151. IMemInputPinVtbl *imemvtbl;
  152. };
  153. long WINAPI ff_dshow_pin_QueryInterface (DShowPin *, const GUID *, void **);
  154. unsigned long WINAPI ff_dshow_pin_AddRef (DShowPin *);
  155. unsigned long WINAPI ff_dshow_pin_Release (DShowPin *);
  156. long WINAPI ff_dshow_pin_Connect (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
  157. long WINAPI ff_dshow_pin_ReceiveConnection (DShowPin *, IPin *, const AM_MEDIA_TYPE *);
  158. long WINAPI ff_dshow_pin_Disconnect (DShowPin *);
  159. long WINAPI ff_dshow_pin_ConnectedTo (DShowPin *, IPin **);
  160. long WINAPI ff_dshow_pin_ConnectionMediaType (DShowPin *, AM_MEDIA_TYPE *);
  161. long WINAPI ff_dshow_pin_QueryPinInfo (DShowPin *, PIN_INFO *);
  162. long WINAPI ff_dshow_pin_QueryDirection (DShowPin *, PIN_DIRECTION *);
  163. long WINAPI ff_dshow_pin_QueryId (DShowPin *, wchar_t **);
  164. long WINAPI ff_dshow_pin_QueryAccept (DShowPin *, const AM_MEDIA_TYPE *);
  165. long WINAPI ff_dshow_pin_EnumMediaTypes (DShowPin *, IEnumMediaTypes **);
  166. long WINAPI ff_dshow_pin_QueryInternalConnections(DShowPin *, IPin **, unsigned long *);
  167. long WINAPI ff_dshow_pin_EndOfStream (DShowPin *);
  168. long WINAPI ff_dshow_pin_BeginFlush (DShowPin *);
  169. long WINAPI ff_dshow_pin_EndFlush (DShowPin *);
  170. long WINAPI ff_dshow_pin_NewSegment (DShowPin *, REFERENCE_TIME, REFERENCE_TIME, double);
  171. long WINAPI ff_dshow_meminputpin_QueryInterface (DShowMemInputPin *, const GUID *, void **);
  172. unsigned long WINAPI ff_dshow_meminputpin_AddRef (DShowMemInputPin *);
  173. unsigned long WINAPI ff_dshow_meminputpin_Release (DShowMemInputPin *);
  174. long WINAPI ff_dshow_meminputpin_GetAllocator (DShowMemInputPin *, IMemAllocator **);
  175. long WINAPI ff_dshow_meminputpin_NotifyAllocator (DShowMemInputPin *, IMemAllocator *, BOOL);
  176. long WINAPI ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *, ALLOCATOR_PROPERTIES *);
  177. long WINAPI ff_dshow_meminputpin_Receive (DShowMemInputPin *, IMediaSample *);
  178. long WINAPI ff_dshow_meminputpin_ReceiveMultiple (DShowMemInputPin *, IMediaSample **, long, long *);
  179. long WINAPI ff_dshow_meminputpin_ReceiveCanBlock (DShowMemInputPin *);
  180. void ff_dshow_pin_Destroy(DShowPin *);
  181. DShowPin *ff_dshow_pin_Create (DShowFilter *filter);
  182. void ff_dshow_meminputpin_Destroy(DShowMemInputPin *);
  183. /*****************************************************************************
  184. * DShowEnumPins
  185. ****************************************************************************/
  186. struct DShowEnumPins {
  187. IEnumPinsVtbl *vtbl;
  188. long ref;
  189. int pos;
  190. DShowPin *pin;
  191. DShowFilter *filter;
  192. };
  193. long WINAPI ff_dshow_enumpins_QueryInterface(DShowEnumPins *, const GUID *, void **);
  194. unsigned long WINAPI ff_dshow_enumpins_AddRef (DShowEnumPins *);
  195. unsigned long WINAPI ff_dshow_enumpins_Release (DShowEnumPins *);
  196. long WINAPI ff_dshow_enumpins_Next (DShowEnumPins *, unsigned long, IPin **, unsigned long *);
  197. long WINAPI ff_dshow_enumpins_Skip (DShowEnumPins *, unsigned long);
  198. long WINAPI ff_dshow_enumpins_Reset (DShowEnumPins *);
  199. long WINAPI ff_dshow_enumpins_Clone (DShowEnumPins *, DShowEnumPins **);
  200. void ff_dshow_enumpins_Destroy(DShowEnumPins *);
  201. DShowEnumPins *ff_dshow_enumpins_Create (DShowPin *pin, DShowFilter *filter);
  202. /*****************************************************************************
  203. * DShowEnumMediaTypes
  204. ****************************************************************************/
  205. struct DShowEnumMediaTypes {
  206. IEnumMediaTypesVtbl *vtbl;
  207. long ref;
  208. int pos;
  209. AM_MEDIA_TYPE type;
  210. };
  211. long WINAPI ff_dshow_enummediatypes_QueryInterface(DShowEnumMediaTypes *, const GUID *, void **);
  212. unsigned long WINAPI ff_dshow_enummediatypes_AddRef (DShowEnumMediaTypes *);
  213. unsigned long WINAPI ff_dshow_enummediatypes_Release (DShowEnumMediaTypes *);
  214. long WINAPI ff_dshow_enummediatypes_Next (DShowEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
  215. long WINAPI ff_dshow_enummediatypes_Skip (DShowEnumMediaTypes *, unsigned long);
  216. long WINAPI ff_dshow_enummediatypes_Reset (DShowEnumMediaTypes *);
  217. long WINAPI ff_dshow_enummediatypes_Clone (DShowEnumMediaTypes *, DShowEnumMediaTypes **);
  218. void ff_dshow_enummediatypes_Destroy(DShowEnumMediaTypes *);
  219. DShowEnumMediaTypes *ff_dshow_enummediatypes_Create(const AM_MEDIA_TYPE *type);
  220. /*****************************************************************************
  221. * DShowFilter
  222. ****************************************************************************/
  223. struct DShowFilter {
  224. IBaseFilterVtbl *vtbl;
  225. long ref;
  226. const wchar_t *name;
  227. DShowPin *pin;
  228. FILTER_INFO info;
  229. FILTER_STATE state;
  230. IReferenceClock *clock;
  231. enum dshowDeviceType type;
  232. void *priv_data;
  233. int stream_index;
  234. int64_t start_time;
  235. void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType type);
  236. };
  237. long WINAPI ff_dshow_filter_QueryInterface (DShowFilter *, const GUID *, void **);
  238. unsigned long WINAPI ff_dshow_filter_AddRef (DShowFilter *);
  239. unsigned long WINAPI ff_dshow_filter_Release (DShowFilter *);
  240. long WINAPI ff_dshow_filter_GetClassID (DShowFilter *, CLSID *);
  241. long WINAPI ff_dshow_filter_Stop (DShowFilter *);
  242. long WINAPI ff_dshow_filter_Pause (DShowFilter *);
  243. long WINAPI ff_dshow_filter_Run (DShowFilter *, REFERENCE_TIME);
  244. long WINAPI ff_dshow_filter_GetState (DShowFilter *, DWORD, FILTER_STATE *);
  245. long WINAPI ff_dshow_filter_SetSyncSource (DShowFilter *, IReferenceClock *);
  246. long WINAPI ff_dshow_filter_GetSyncSource (DShowFilter *, IReferenceClock **);
  247. long WINAPI ff_dshow_filter_EnumPins (DShowFilter *, IEnumPins **);
  248. long WINAPI ff_dshow_filter_FindPin (DShowFilter *, const wchar_t *, IPin **);
  249. long WINAPI ff_dshow_filter_QueryFilterInfo(DShowFilter *, FILTER_INFO *);
  250. long WINAPI ff_dshow_filter_JoinFilterGraph(DShowFilter *, IFilterGraph *, const wchar_t *);
  251. long WINAPI ff_dshow_filter_QueryVendorInfo(DShowFilter *, wchar_t **);
  252. void ff_dshow_filter_Destroy(DShowFilter *);
  253. DShowFilter *ff_dshow_filter_Create (void *, void *, enum dshowDeviceType);
  254. /*****************************************************************************
  255. * dshow_ctx
  256. ****************************************************************************/
  257. struct dshow_ctx {
  258. const AVClass *class;
  259. IGraphBuilder *graph;
  260. char *device_name[2];
  261. char *device_unique_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. char *audio_filter_load_file;
  278. char *audio_filter_save_file;
  279. char *video_filter_load_file;
  280. char *video_filter_save_file;
  281. int use_video_device_timestamps;
  282. IBaseFilter *device_filter[2];
  283. IPin *device_pin[2];
  284. DShowFilter *capture_filter[2];
  285. DShowPin *capture_pin[2];
  286. HANDLE mutex;
  287. HANDLE event[2]; /* event[0] is set by DirectShow
  288. * event[1] is set by callback() */
  289. PacketListEntry *pktl;
  290. int eof;
  291. int64_t curbufsize[2];
  292. unsigned int video_frame_num;
  293. IMediaControl *control;
  294. IMediaEvent *media_event;
  295. enum AVPixelFormat pixel_format;
  296. enum AVCodecID video_codec_id;
  297. char *framerate;
  298. int requested_width;
  299. int requested_height;
  300. AVRational requested_framerate;
  301. int sample_rate;
  302. int sample_size;
  303. int channels;
  304. };
  305. /*****************************************************************************
  306. * CrossBar
  307. ****************************************************************************/
  308. HRESULT ff_dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
  309. IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx);
  310. void ff_dshow_show_filter_properties(IBaseFilter *pFilter, AVFormatContext *avctx);
  311. #endif /* AVDEVICE_DSHOW_CAPTURE_H */