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. #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. void *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \
  115. dshowdebug("ff_dshow_"AV_STRINGIFY(prefix)"_Create(%p)\n", this); \
  116. if (!this || !vtbl) \
  117. goto fail; \
  118. ZeroMemory(this, sizeof(class)); \
  119. ZeroMemory(vtbl, sizeof(*this->vtbl)); \
  120. this->ref = 1; \
  121. this->vtbl = vtbl; \
  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 */