dshow.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. #include "dshow_capture.h"
  22. #include "libavutil/parseutils.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/opt.h"
  25. #include "libavformat/internal.h"
  26. #include "libavformat/riff.h"
  27. #include "avdevice.h"
  28. #include "libavcodec/raw.h"
  29. static enum AVPixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
  30. {
  31. switch(biCompression) {
  32. case BI_BITFIELDS:
  33. case BI_RGB:
  34. switch(biBitCount) { /* 1-8 are untested */
  35. case 1:
  36. return AV_PIX_FMT_MONOWHITE;
  37. case 4:
  38. return AV_PIX_FMT_RGB4;
  39. case 8:
  40. return AV_PIX_FMT_RGB8;
  41. case 16:
  42. return AV_PIX_FMT_RGB555;
  43. case 24:
  44. return AV_PIX_FMT_BGR24;
  45. case 32:
  46. return AV_PIX_FMT_0RGB32;
  47. }
  48. }
  49. return avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), biCompression); // all others
  50. }
  51. static int
  52. dshow_read_close(AVFormatContext *s)
  53. {
  54. struct dshow_ctx *ctx = s->priv_data;
  55. AVPacketList *pktl;
  56. if (ctx->control) {
  57. IMediaControl_Stop(ctx->control);
  58. IMediaControl_Release(ctx->control);
  59. }
  60. if (ctx->media_event)
  61. IMediaEvent_Release(ctx->media_event);
  62. if (ctx->graph) {
  63. IEnumFilters *fenum;
  64. int r;
  65. r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
  66. if (r == S_OK) {
  67. IBaseFilter *f;
  68. IEnumFilters_Reset(fenum);
  69. while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK) {
  70. if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK)
  71. IEnumFilters_Reset(fenum); /* When a filter is removed,
  72. * the list must be reset. */
  73. IBaseFilter_Release(f);
  74. }
  75. IEnumFilters_Release(fenum);
  76. }
  77. IGraphBuilder_Release(ctx->graph);
  78. }
  79. if (ctx->capture_pin[VideoDevice])
  80. libAVPin_Release(ctx->capture_pin[VideoDevice]);
  81. if (ctx->capture_pin[AudioDevice])
  82. libAVPin_Release(ctx->capture_pin[AudioDevice]);
  83. if (ctx->capture_filter[VideoDevice])
  84. libAVFilter_Release(ctx->capture_filter[VideoDevice]);
  85. if (ctx->capture_filter[AudioDevice])
  86. libAVFilter_Release(ctx->capture_filter[AudioDevice]);
  87. if (ctx->device_pin[VideoDevice])
  88. IPin_Release(ctx->device_pin[VideoDevice]);
  89. if (ctx->device_pin[AudioDevice])
  90. IPin_Release(ctx->device_pin[AudioDevice]);
  91. if (ctx->device_filter[VideoDevice])
  92. IBaseFilter_Release(ctx->device_filter[VideoDevice]);
  93. if (ctx->device_filter[AudioDevice])
  94. IBaseFilter_Release(ctx->device_filter[AudioDevice]);
  95. if (ctx->device_name[0])
  96. av_freep(&ctx->device_name[0]);
  97. if (ctx->device_name[1])
  98. av_freep(&ctx->device_name[1]);
  99. if(ctx->mutex)
  100. CloseHandle(ctx->mutex);
  101. if(ctx->event[0])
  102. CloseHandle(ctx->event[0]);
  103. if(ctx->event[1])
  104. CloseHandle(ctx->event[1]);
  105. pktl = ctx->pktl;
  106. while (pktl) {
  107. AVPacketList *next = pktl->next;
  108. av_free_packet(&pktl->pkt);
  109. av_free(pktl);
  110. pktl = next;
  111. }
  112. CoUninitialize();
  113. return 0;
  114. }
  115. static char *dup_wchar_to_utf8(wchar_t *w)
  116. {
  117. char *s = NULL;
  118. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  119. s = av_malloc(l);
  120. if (s)
  121. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  122. return s;
  123. }
  124. static int shall_we_drop(AVFormatContext *s, int index, enum dshowDeviceType devtype)
  125. {
  126. struct dshow_ctx *ctx = s->priv_data;
  127. static const uint8_t dropscore[] = {62, 75, 87, 100};
  128. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  129. unsigned int buffer_fullness = (ctx->curbufsize[index]*100)/s->max_picture_buffer;
  130. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  131. if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
  132. av_log(s, AV_LOG_ERROR,
  133. "real-time buffer [%s] [%s input] too full or near too full (%d%% of size: %d [rtbufsize parameter])! frame dropped!\n",
  134. ctx->device_name[devtype], devtypename, buffer_fullness, s->max_picture_buffer);
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. static void
  140. callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
  141. {
  142. AVFormatContext *s = priv_data;
  143. struct dshow_ctx *ctx = s->priv_data;
  144. AVPacketList **ppktl, *pktl_next;
  145. // dump_videohdr(s, vdhdr);
  146. WaitForSingleObject(ctx->mutex, INFINITE);
  147. if(shall_we_drop(s, index, devtype))
  148. goto fail;
  149. pktl_next = av_mallocz(sizeof(AVPacketList));
  150. if(!pktl_next)
  151. goto fail;
  152. if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
  153. av_free(pktl_next);
  154. goto fail;
  155. }
  156. pktl_next->pkt.stream_index = index;
  157. pktl_next->pkt.pts = time;
  158. memcpy(pktl_next->pkt.data, buf, buf_size);
  159. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  160. *ppktl = pktl_next;
  161. ctx->curbufsize[index] += buf_size;
  162. SetEvent(ctx->event[1]);
  163. ReleaseMutex(ctx->mutex);
  164. return;
  165. fail:
  166. ReleaseMutex(ctx->mutex);
  167. return;
  168. }
  169. /**
  170. * Cycle through available devices using the device enumerator devenum,
  171. * retrieve the device with type specified by devtype and return the
  172. * pointer to the object found in *pfilter.
  173. * If pfilter is NULL, list all device names.
  174. */
  175. static int
  176. dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
  177. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype, IBaseFilter **pfilter)
  178. {
  179. struct dshow_ctx *ctx = avctx->priv_data;
  180. IBaseFilter *device_filter = NULL;
  181. IEnumMoniker *classenum = NULL;
  182. IMoniker *m = NULL;
  183. const char *device_name = ctx->device_name[devtype];
  184. int skip = (devtype == VideoDevice) ? ctx->video_device_number
  185. : ctx->audio_device_number;
  186. int r;
  187. const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
  188. &CLSID_AudioInputDeviceCategory };
  189. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio only";
  190. const char *sourcetypename = (sourcetype == VideoSourceDevice) ? "video" : "audio";
  191. r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[sourcetype],
  192. (IEnumMoniker **) &classenum, 0);
  193. if (r != S_OK) {
  194. av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices (or none found).\n",
  195. devtypename);
  196. return AVERROR(EIO);
  197. }
  198. while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) {
  199. IPropertyBag *bag = NULL;
  200. char *friendly_name = NULL;
  201. char *unique_name = NULL;
  202. VARIANT var;
  203. IBindCtx *bind_ctx = NULL;
  204. LPOLESTR olestr = NULL;
  205. LPMALLOC co_malloc = NULL;
  206. int i;
  207. r = CoGetMalloc(1, &co_malloc);
  208. if (r = S_OK)
  209. goto fail1;
  210. r = CreateBindCtx(0, &bind_ctx);
  211. if (r != S_OK)
  212. goto fail1;
  213. /* GetDisplayname works for both video and audio, DevicePath doesn't */
  214. r = IMoniker_GetDisplayName(m, bind_ctx, NULL, &olestr);
  215. if (r != S_OK)
  216. goto fail1;
  217. unique_name = dup_wchar_to_utf8(olestr);
  218. /* replace ':' with '_' since we use : to delineate between sources */
  219. for (i = 0; i < strlen(unique_name); i++) {
  220. if (unique_name[i] == ':')
  221. unique_name[i] = '_';
  222. }
  223. r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
  224. if (r != S_OK)
  225. goto fail1;
  226. var.vt = VT_BSTR;
  227. r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
  228. if (r != S_OK)
  229. goto fail1;
  230. friendly_name = dup_wchar_to_utf8(var.bstrVal);
  231. if (pfilter) {
  232. if (strcmp(device_name, friendly_name) && strcmp(device_name, unique_name))
  233. goto fail1;
  234. if (!skip--) {
  235. r = IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
  236. if (r != S_OK) {
  237. av_log(avctx, AV_LOG_ERROR, "Unable to BindToObject for %s\n", device_name);
  238. goto fail1;
  239. }
  240. }
  241. } else {
  242. av_log(avctx, AV_LOG_INFO, " \"%s\"\n", friendly_name);
  243. av_log(avctx, AV_LOG_INFO, " Alternative name \"%s\"\n", unique_name);
  244. }
  245. fail1:
  246. if (olestr && co_malloc)
  247. IMalloc_Free(co_malloc, olestr);
  248. if (bind_ctx)
  249. IBindCtx_Release(bind_ctx);
  250. av_free(friendly_name);
  251. av_free(unique_name);
  252. if (bag)
  253. IPropertyBag_Release(bag);
  254. IMoniker_Release(m);
  255. }
  256. IEnumMoniker_Release(classenum);
  257. if (pfilter) {
  258. if (!device_filter) {
  259. av_log(avctx, AV_LOG_ERROR, "Could not find %s device with name [%s] among source devices of type %s.\n",
  260. devtypename, device_name, sourcetypename);
  261. return AVERROR(EIO);
  262. }
  263. *pfilter = device_filter;
  264. }
  265. return 0;
  266. }
  267. /**
  268. * Cycle through available formats using the specified pin,
  269. * try to set parameters specified through AVOptions and if successful
  270. * return 1 in *pformat_set.
  271. * If pformat_set is NULL, list all pin capabilities.
  272. */
  273. static void
  274. dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
  275. IPin *pin, int *pformat_set)
  276. {
  277. struct dshow_ctx *ctx = avctx->priv_data;
  278. IAMStreamConfig *config = NULL;
  279. AM_MEDIA_TYPE *type = NULL;
  280. int format_set = 0;
  281. void *caps = NULL;
  282. int i, n, size, r;
  283. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  284. return;
  285. if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK)
  286. goto end;
  287. caps = av_malloc(size);
  288. if (!caps)
  289. goto end;
  290. for (i = 0; i < n && !format_set; i++) {
  291. r = IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps);
  292. if (r != S_OK)
  293. goto next;
  294. #if DSHOWDEBUG
  295. ff_print_AM_MEDIA_TYPE(type);
  296. #endif
  297. if (devtype == VideoDevice) {
  298. VIDEO_STREAM_CONFIG_CAPS *vcaps = caps;
  299. BITMAPINFOHEADER *bih;
  300. int64_t *fr;
  301. const AVCodecTag *const tags[] = { avformat_get_riff_video_tags(), NULL };
  302. #if DSHOWDEBUG
  303. ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps);
  304. #endif
  305. if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) {
  306. VIDEOINFOHEADER *v = (void *) type->pbFormat;
  307. fr = &v->AvgTimePerFrame;
  308. bih = &v->bmiHeader;
  309. } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) {
  310. VIDEOINFOHEADER2 *v = (void *) type->pbFormat;
  311. fr = &v->AvgTimePerFrame;
  312. bih = &v->bmiHeader;
  313. } else {
  314. goto next;
  315. }
  316. if (!pformat_set) {
  317. enum AVPixelFormat pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  318. if (pix_fmt == AV_PIX_FMT_NONE) {
  319. enum AVCodecID codec_id = av_codec_get_id(tags, bih->biCompression);
  320. AVCodec *codec = avcodec_find_decoder(codec_id);
  321. if (codec_id == AV_CODEC_ID_NONE || !codec) {
  322. av_log(avctx, AV_LOG_INFO, " unknown compression type 0x%X", (int) bih->biCompression);
  323. } else {
  324. av_log(avctx, AV_LOG_INFO, " vcodec=%s", codec->name);
  325. }
  326. } else {
  327. av_log(avctx, AV_LOG_INFO, " pixel_format=%s", av_get_pix_fmt_name(pix_fmt));
  328. }
  329. av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g\n",
  330. vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy,
  331. 1e7 / vcaps->MaxFrameInterval,
  332. vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy,
  333. 1e7 / vcaps->MinFrameInterval);
  334. continue;
  335. }
  336. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  337. if (ctx->video_codec_id != av_codec_get_id(tags, bih->biCompression))
  338. goto next;
  339. }
  340. if (ctx->pixel_format != AV_PIX_FMT_NONE &&
  341. ctx->pixel_format != dshow_pixfmt(bih->biCompression, bih->biBitCount)) {
  342. goto next;
  343. }
  344. if (ctx->framerate) {
  345. int64_t framerate = ((int64_t) ctx->requested_framerate.den*10000000)
  346. / ctx->requested_framerate.num;
  347. if (framerate > vcaps->MaxFrameInterval ||
  348. framerate < vcaps->MinFrameInterval)
  349. goto next;
  350. *fr = framerate;
  351. }
  352. if (ctx->requested_width && ctx->requested_height) {
  353. if (ctx->requested_width > vcaps->MaxOutputSize.cx ||
  354. ctx->requested_width < vcaps->MinOutputSize.cx ||
  355. ctx->requested_height > vcaps->MaxOutputSize.cy ||
  356. ctx->requested_height < vcaps->MinOutputSize.cy)
  357. goto next;
  358. bih->biWidth = ctx->requested_width;
  359. bih->biHeight = ctx->requested_height;
  360. }
  361. } else {
  362. AUDIO_STREAM_CONFIG_CAPS *acaps = caps;
  363. WAVEFORMATEX *fx;
  364. #if DSHOWDEBUG
  365. ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps);
  366. #endif
  367. if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) {
  368. fx = (void *) type->pbFormat;
  369. } else {
  370. goto next;
  371. }
  372. if (!pformat_set) {
  373. av_log(avctx, AV_LOG_INFO, " min ch=%lu bits=%lu rate=%6lu max ch=%lu bits=%lu rate=%6lu\n",
  374. acaps->MinimumChannels, acaps->MinimumBitsPerSample, acaps->MinimumSampleFrequency,
  375. acaps->MaximumChannels, acaps->MaximumBitsPerSample, acaps->MaximumSampleFrequency);
  376. continue;
  377. }
  378. if (ctx->sample_rate) {
  379. if (ctx->sample_rate > acaps->MaximumSampleFrequency ||
  380. ctx->sample_rate < acaps->MinimumSampleFrequency)
  381. goto next;
  382. fx->nSamplesPerSec = ctx->sample_rate;
  383. }
  384. if (ctx->sample_size) {
  385. if (ctx->sample_size > acaps->MaximumBitsPerSample ||
  386. ctx->sample_size < acaps->MinimumBitsPerSample)
  387. goto next;
  388. fx->wBitsPerSample = ctx->sample_size;
  389. }
  390. if (ctx->channels) {
  391. if (ctx->channels > acaps->MaximumChannels ||
  392. ctx->channels < acaps->MinimumChannels)
  393. goto next;
  394. fx->nChannels = ctx->channels;
  395. }
  396. }
  397. if (IAMStreamConfig_SetFormat(config, type) != S_OK)
  398. goto next;
  399. format_set = 1;
  400. next:
  401. if (type->pbFormat)
  402. CoTaskMemFree(type->pbFormat);
  403. CoTaskMemFree(type);
  404. }
  405. end:
  406. IAMStreamConfig_Release(config);
  407. av_free(caps);
  408. if (pformat_set)
  409. *pformat_set = format_set;
  410. }
  411. /**
  412. * Set audio device buffer size in milliseconds (which can directly impact
  413. * latency, depending on the device).
  414. */
  415. static int
  416. dshow_set_audio_buffer_size(AVFormatContext *avctx, IPin *pin)
  417. {
  418. struct dshow_ctx *ctx = avctx->priv_data;
  419. IAMBufferNegotiation *buffer_negotiation = NULL;
  420. ALLOCATOR_PROPERTIES props = { -1, -1, -1, -1 };
  421. IAMStreamConfig *config = NULL;
  422. AM_MEDIA_TYPE *type = NULL;
  423. int ret = AVERROR(EIO);
  424. if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK)
  425. goto end;
  426. if (IAMStreamConfig_GetFormat(config, &type) != S_OK)
  427. goto end;
  428. if (!IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx))
  429. goto end;
  430. props.cbBuffer = (((WAVEFORMATEX *) type->pbFormat)->nAvgBytesPerSec)
  431. * ctx->audio_buffer_size / 1000;
  432. if (IPin_QueryInterface(pin, &IID_IAMBufferNegotiation, (void **) &buffer_negotiation) != S_OK)
  433. goto end;
  434. if (IAMBufferNegotiation_SuggestAllocatorProperties(buffer_negotiation, &props) != S_OK)
  435. goto end;
  436. ret = 0;
  437. end:
  438. if (buffer_negotiation)
  439. IAMBufferNegotiation_Release(buffer_negotiation);
  440. if (type) {
  441. if (type->pbFormat)
  442. CoTaskMemFree(type->pbFormat);
  443. CoTaskMemFree(type);
  444. }
  445. if (config)
  446. IAMStreamConfig_Release(config);
  447. return ret;
  448. }
  449. /**
  450. * Pops up a user dialog allowing them to adjust properties for the given filter, if possible.
  451. */
  452. void
  453. dshow_show_filter_properties(IBaseFilter *device_filter, AVFormatContext *avctx) {
  454. ISpecifyPropertyPages *property_pages = NULL;
  455. IUnknown *device_filter_iunknown = NULL;
  456. HRESULT hr;
  457. FILTER_INFO filter_info = {0}; /* a warning on this line is false positive GCC bug 53119 AFAICT */
  458. CAUUID ca_guid = {0};
  459. hr = IBaseFilter_QueryInterface(device_filter, &IID_ISpecifyPropertyPages, (void **)&property_pages);
  460. if (hr != S_OK) {
  461. av_log(avctx, AV_LOG_WARNING, "requested filter does not have a property page to show");
  462. goto end;
  463. }
  464. hr = IBaseFilter_QueryFilterInfo(device_filter, &filter_info);
  465. if (hr != S_OK) {
  466. goto fail;
  467. }
  468. hr = IBaseFilter_QueryInterface(device_filter, &IID_IUnknown, (void **)&device_filter_iunknown);
  469. if (hr != S_OK) {
  470. goto fail;
  471. }
  472. hr = ISpecifyPropertyPages_GetPages(property_pages, &ca_guid);
  473. if (hr != S_OK) {
  474. goto fail;
  475. }
  476. hr = OleCreatePropertyFrame(NULL, 0, 0, filter_info.achName, 1, &device_filter_iunknown, ca_guid.cElems,
  477. ca_guid.pElems, 0, 0, NULL);
  478. if (hr != S_OK) {
  479. goto fail;
  480. }
  481. goto end;
  482. fail:
  483. av_log(avctx, AV_LOG_ERROR, "Failure showing property pages for filter");
  484. end:
  485. if (property_pages)
  486. ISpecifyPropertyPages_Release(property_pages);
  487. if (device_filter_iunknown)
  488. IUnknown_Release(device_filter_iunknown);
  489. if (filter_info.pGraph)
  490. IFilterGraph_Release(filter_info.pGraph);
  491. if (ca_guid.pElems)
  492. CoTaskMemFree(ca_guid.pElems);
  493. }
  494. /**
  495. * Cycle through available pins using the device_filter device, of type
  496. * devtype, retrieve the first output pin and return the pointer to the
  497. * object found in *ppin.
  498. * If ppin is NULL, cycle through all pins listing audio/video capabilities.
  499. */
  500. static int
  501. dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,
  502. enum dshowSourceFilterType sourcetype, IBaseFilter *device_filter, IPin **ppin)
  503. {
  504. struct dshow_ctx *ctx = avctx->priv_data;
  505. IEnumPins *pins = 0;
  506. IPin *device_pin = NULL;
  507. IPin *pin;
  508. int r;
  509. const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
  510. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio only";
  511. const char *sourcetypename = (sourcetype == VideoSourceDevice) ? "video" : "audio";
  512. int set_format = (devtype == VideoDevice && (ctx->framerate ||
  513. (ctx->requested_width && ctx->requested_height) ||
  514. ctx->pixel_format != AV_PIX_FMT_NONE ||
  515. ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO))
  516. || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate));
  517. int format_set = 0;
  518. int should_show_properties = (devtype == VideoDevice) ? ctx->show_video_device_dialog : ctx->show_audio_device_dialog;
  519. if (should_show_properties)
  520. dshow_show_filter_properties(device_filter, avctx);
  521. r = IBaseFilter_EnumPins(device_filter, &pins);
  522. if (r != S_OK) {
  523. av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
  524. return AVERROR(EIO);
  525. }
  526. if (!ppin) {
  527. av_log(avctx, AV_LOG_INFO, "DirectShow %s device options (from %s devices)\n",
  528. devtypename, sourcetypename);
  529. }
  530. while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) {
  531. IKsPropertySet *p = NULL;
  532. IEnumMediaTypes *types = NULL;
  533. PIN_INFO info = {0};
  534. AM_MEDIA_TYPE *type;
  535. GUID category;
  536. DWORD r2;
  537. char *name_buf = NULL;
  538. wchar_t *pin_id = NULL;
  539. char *pin_buf = NULL;
  540. char *desired_pin_name = devtype == VideoDevice ? ctx->video_pin_name : ctx->audio_pin_name;
  541. IPin_QueryPinInfo(pin, &info);
  542. IBaseFilter_Release(info.pFilter);
  543. if (info.dir != PINDIR_OUTPUT)
  544. goto next;
  545. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  546. goto next;
  547. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  548. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  549. goto next;
  550. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  551. goto next;
  552. name_buf = dup_wchar_to_utf8(info.achName);
  553. r = IPin_QueryId(pin, &pin_id);
  554. if (r != S_OK) {
  555. av_log(avctx, AV_LOG_ERROR, "Could not query pin id\n");
  556. return AVERROR(EIO);
  557. }
  558. pin_buf = dup_wchar_to_utf8(pin_id);
  559. if (!ppin) {
  560. av_log(avctx, AV_LOG_INFO, " Pin \"%s\" (alternative pin name \"%s\")\n", name_buf, pin_buf);
  561. dshow_cycle_formats(avctx, devtype, pin, NULL);
  562. goto next;
  563. }
  564. if (desired_pin_name) {
  565. if(strcmp(name_buf, desired_pin_name) && strcmp(pin_buf, desired_pin_name)) {
  566. av_log(avctx, AV_LOG_DEBUG, "skipping pin \"%s\" (\"%s\") != requested \"%s\"\n",
  567. name_buf, pin_buf, desired_pin_name);
  568. goto next;
  569. }
  570. }
  571. if (set_format) {
  572. dshow_cycle_formats(avctx, devtype, pin, &format_set);
  573. if (!format_set) {
  574. goto next;
  575. }
  576. }
  577. if (devtype == AudioDevice && ctx->audio_buffer_size) {
  578. if (dshow_set_audio_buffer_size(avctx, pin) < 0) {
  579. av_log(avctx, AV_LOG_ERROR, "unable to set audio buffer size %d to pin, using pin anyway...", ctx->audio_buffer_size);
  580. }
  581. }
  582. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  583. goto next;
  584. IEnumMediaTypes_Reset(types);
  585. /* in case format_set was not called, just verify the majortype */
  586. while (!device_pin && IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) {
  587. if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
  588. device_pin = pin;
  589. av_log(avctx, AV_LOG_DEBUG, "Selecting pin %s on %s\n", name_buf, devtypename);
  590. goto next;
  591. }
  592. CoTaskMemFree(type);
  593. }
  594. next:
  595. if (types)
  596. IEnumMediaTypes_Release(types);
  597. if (p)
  598. IKsPropertySet_Release(p);
  599. if (device_pin != pin)
  600. IPin_Release(pin);
  601. av_free(name_buf);
  602. av_free(pin_buf);
  603. if (pin_id)
  604. CoTaskMemFree(pin_id);
  605. }
  606. IEnumPins_Release(pins);
  607. if (ppin) {
  608. if (set_format && !format_set) {
  609. av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);
  610. return AVERROR(EIO);
  611. }
  612. if (!device_pin) {
  613. av_log(avctx, AV_LOG_ERROR,
  614. "Could not find output pin from %s capture device.\n", devtypename);
  615. return AVERROR(EIO);
  616. }
  617. *ppin = device_pin;
  618. }
  619. return 0;
  620. }
  621. /**
  622. * List options for device with type devtype, source filter type sourcetype
  623. *
  624. * @param devenum device enumerator used for accessing the device
  625. */
  626. static int
  627. dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
  628. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype)
  629. {
  630. struct dshow_ctx *ctx = avctx->priv_data;
  631. IBaseFilter *device_filter = NULL;
  632. int r;
  633. if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter)) < 0)
  634. return r;
  635. ctx->device_filter[devtype] = device_filter;
  636. if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, NULL)) < 0)
  637. return r;
  638. return 0;
  639. }
  640. static int
  641. dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
  642. enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype)
  643. {
  644. struct dshow_ctx *ctx = avctx->priv_data;
  645. IBaseFilter *device_filter = NULL;
  646. IGraphBuilder *graph = ctx->graph;
  647. IPin *device_pin = NULL;
  648. libAVPin *capture_pin = NULL;
  649. libAVFilter *capture_filter = NULL;
  650. ICaptureGraphBuilder2 *graph_builder2 = NULL;
  651. int ret = AVERROR(EIO);
  652. int r;
  653. const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
  654. if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter)) < 0) {
  655. ret = r;
  656. goto error;
  657. }
  658. ctx->device_filter [devtype] = device_filter;
  659. r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
  660. if (r != S_OK) {
  661. av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
  662. goto error;
  663. }
  664. if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, &device_pin)) < 0) {
  665. ret = r;
  666. goto error;
  667. }
  668. ctx->device_pin[devtype] = device_pin;
  669. capture_filter = libAVFilter_Create(avctx, callback, devtype);
  670. if (!capture_filter) {
  671. av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
  672. goto error;
  673. }
  674. ctx->capture_filter[devtype] = capture_filter;
  675. r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
  676. filter_name[devtype]);
  677. if (r != S_OK) {
  678. av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
  679. goto error;
  680. }
  681. libAVPin_AddRef(capture_filter->pin);
  682. capture_pin = capture_filter->pin;
  683. ctx->capture_pin[devtype] = capture_pin;
  684. r = CoCreateInstance(&CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
  685. &IID_ICaptureGraphBuilder2, (void **) &graph_builder2);
  686. if (r != S_OK) {
  687. av_log(avctx, AV_LOG_ERROR, "Could not create CaptureGraphBuilder2\n");
  688. goto error;
  689. }
  690. ICaptureGraphBuilder2_SetFiltergraph(graph_builder2, graph);
  691. if (r != S_OK) {
  692. av_log(avctx, AV_LOG_ERROR, "Could not set graph for CaptureGraphBuilder2\n");
  693. goto error;
  694. }
  695. r = ICaptureGraphBuilder2_RenderStream(graph_builder2, NULL, NULL, (IUnknown *) device_pin, NULL /* no intermediate filter */,
  696. (IBaseFilter *) capture_filter); /* connect pins, optionally insert intermediate filters like crossbar if necessary */
  697. if (r != S_OK) {
  698. av_log(avctx, AV_LOG_ERROR, "Could not RenderStream to connect pins\n");
  699. goto error;
  700. }
  701. r = dshow_try_setup_crossbar_options(graph_builder2, device_filter, devtype, avctx);
  702. if (r != S_OK) {
  703. av_log(avctx, AV_LOG_ERROR, "Could not setup CrossBar\n");
  704. goto error;
  705. }
  706. ret = 0;
  707. error:
  708. if (graph_builder2 != NULL)
  709. ICaptureGraphBuilder2_Release(graph_builder2);
  710. return ret;
  711. }
  712. static enum AVCodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
  713. {
  714. switch (sample_fmt) {
  715. case AV_SAMPLE_FMT_U8: return AV_CODEC_ID_PCM_U8;
  716. case AV_SAMPLE_FMT_S16: return AV_CODEC_ID_PCM_S16LE;
  717. case AV_SAMPLE_FMT_S32: return AV_CODEC_ID_PCM_S32LE;
  718. default: return AV_CODEC_ID_NONE; /* Should never happen. */
  719. }
  720. }
  721. static enum AVSampleFormat sample_fmt_bits_per_sample(int bits)
  722. {
  723. switch (bits) {
  724. case 8: return AV_SAMPLE_FMT_U8;
  725. case 16: return AV_SAMPLE_FMT_S16;
  726. case 32: return AV_SAMPLE_FMT_S32;
  727. default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
  728. }
  729. }
  730. static int
  731. dshow_add_device(AVFormatContext *avctx,
  732. enum dshowDeviceType devtype)
  733. {
  734. struct dshow_ctx *ctx = avctx->priv_data;
  735. AM_MEDIA_TYPE type;
  736. AVCodecContext *codec;
  737. AVStream *st;
  738. int ret = AVERROR(EIO);
  739. st = avformat_new_stream(avctx, NULL);
  740. if (!st) {
  741. ret = AVERROR(ENOMEM);
  742. goto error;
  743. }
  744. st->id = devtype;
  745. ctx->capture_filter[devtype]->stream_index = st->index;
  746. libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
  747. codec = st->codec;
  748. if (devtype == VideoDevice) {
  749. BITMAPINFOHEADER *bih = NULL;
  750. AVRational time_base;
  751. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  752. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  753. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  754. bih = &v->bmiHeader;
  755. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  756. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  757. time_base = (AVRational) { v->AvgTimePerFrame, 10000000 };
  758. bih = &v->bmiHeader;
  759. }
  760. if (!bih) {
  761. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  762. goto error;
  763. }
  764. codec->time_base = time_base;
  765. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  766. codec->width = bih->biWidth;
  767. codec->height = bih->biHeight;
  768. codec->codec_tag = bih->biCompression;
  769. codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  770. if (bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
  771. av_log(avctx, AV_LOG_DEBUG, "attempt to use full range for HDYC...\n");
  772. codec->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
  773. }
  774. if (codec->pix_fmt == AV_PIX_FMT_NONE) {
  775. const AVCodecTag *const tags[] = { avformat_get_riff_video_tags(), NULL };
  776. codec->codec_id = av_codec_get_id(tags, bih->biCompression);
  777. if (codec->codec_id == AV_CODEC_ID_NONE) {
  778. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  779. "Please report type 0x%X.\n", (int) bih->biCompression);
  780. return AVERROR_PATCHWELCOME;
  781. }
  782. codec->bits_per_coded_sample = bih->biBitCount;
  783. } else {
  784. codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  785. if (bih->biCompression == BI_RGB || bih->biCompression == BI_BITFIELDS) {
  786. codec->bits_per_coded_sample = bih->biBitCount;
  787. codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  788. if (codec->extradata) {
  789. codec->extradata_size = 9;
  790. memcpy(codec->extradata, "BottomUp", 9);
  791. }
  792. }
  793. }
  794. } else {
  795. WAVEFORMATEX *fx = NULL;
  796. if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  797. fx = (void *) type.pbFormat;
  798. }
  799. if (!fx) {
  800. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  801. goto error;
  802. }
  803. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  804. codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
  805. codec->codec_id = waveform_codec_id(codec->sample_fmt);
  806. codec->sample_rate = fx->nSamplesPerSec;
  807. codec->channels = fx->nChannels;
  808. }
  809. avpriv_set_pts_info(st, 64, 1, 10000000);
  810. ret = 0;
  811. error:
  812. return ret;
  813. }
  814. static int parse_device_name(AVFormatContext *avctx)
  815. {
  816. struct dshow_ctx *ctx = avctx->priv_data;
  817. char **device_name = ctx->device_name;
  818. char *name = av_strdup(avctx->filename);
  819. char *tmp = name;
  820. int ret = 1;
  821. char *type;
  822. while ((type = strtok(tmp, "="))) {
  823. char *token = strtok(NULL, ":");
  824. tmp = NULL;
  825. if (!strcmp(type, "video")) {
  826. device_name[0] = token;
  827. } else if (!strcmp(type, "audio")) {
  828. device_name[1] = token;
  829. } else {
  830. device_name[0] = NULL;
  831. device_name[1] = NULL;
  832. break;
  833. }
  834. }
  835. if (!device_name[0] && !device_name[1]) {
  836. ret = 0;
  837. } else {
  838. if (device_name[0])
  839. device_name[0] = av_strdup(device_name[0]);
  840. if (device_name[1])
  841. device_name[1] = av_strdup(device_name[1]);
  842. }
  843. av_free(name);
  844. return ret;
  845. }
  846. static int dshow_read_header(AVFormatContext *avctx)
  847. {
  848. struct dshow_ctx *ctx = avctx->priv_data;
  849. IGraphBuilder *graph = NULL;
  850. ICreateDevEnum *devenum = NULL;
  851. IMediaControl *control = NULL;
  852. IMediaEvent *media_event = NULL;
  853. HANDLE media_event_handle;
  854. HANDLE proc;
  855. int ret = AVERROR(EIO);
  856. int r;
  857. CoInitialize(0);
  858. if (!ctx->list_devices && !parse_device_name(avctx)) {
  859. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  860. goto error;
  861. }
  862. ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id
  863. : AV_CODEC_ID_RAWVIDEO;
  864. if (ctx->pixel_format != AV_PIX_FMT_NONE) {
  865. if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) {
  866. av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when "
  867. "video codec is not set or set to rawvideo\n");
  868. ret = AVERROR(EINVAL);
  869. goto error;
  870. }
  871. }
  872. if (ctx->framerate) {
  873. r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate);
  874. if (r < 0) {
  875. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate);
  876. goto error;
  877. }
  878. }
  879. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  880. &IID_IGraphBuilder, (void **) &graph);
  881. if (r != S_OK) {
  882. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  883. goto error;
  884. }
  885. ctx->graph = graph;
  886. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  887. &IID_ICreateDevEnum, (void **) &devenum);
  888. if (r != S_OK) {
  889. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  890. goto error;
  891. }
  892. if (ctx->list_devices) {
  893. av_log(avctx, AV_LOG_INFO, "DirectShow video devices (some may be both video and audio devices)\n");
  894. dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL);
  895. av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
  896. dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL);
  897. ret = AVERROR_EXIT;
  898. goto error;
  899. }
  900. if (ctx->list_options) {
  901. if (ctx->device_name[VideoDevice])
  902. if ((r = dshow_list_device_options(avctx, devenum, VideoDevice, VideoSourceDevice))) {
  903. ret = r;
  904. goto error;
  905. }
  906. if (ctx->device_name[AudioDevice]) {
  907. if (dshow_list_device_options(avctx, devenum, AudioDevice, AudioSourceDevice)) {
  908. /* show audio options from combined video+audio sources as fallback */
  909. if ((r = dshow_list_device_options(avctx, devenum, AudioDevice, VideoSourceDevice))) {
  910. ret = r;
  911. goto error;
  912. }
  913. }
  914. }
  915. }
  916. if (ctx->device_name[VideoDevice]) {
  917. if ((r = dshow_open_device(avctx, devenum, VideoDevice, VideoSourceDevice)) < 0 ||
  918. (r = dshow_add_device(avctx, VideoDevice)) < 0) {
  919. ret = r;
  920. goto error;
  921. }
  922. }
  923. if (ctx->device_name[AudioDevice]) {
  924. if ((r = dshow_open_device(avctx, devenum, AudioDevice, AudioSourceDevice)) < 0 ||
  925. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  926. av_log(avctx, AV_LOG_INFO, "Searching for audio device within video devices for %s\n", ctx->device_name[AudioDevice]);
  927. /* see if there's a video source with an audio pin with the given audio name */
  928. if ((r = dshow_open_device(avctx, devenum, AudioDevice, VideoSourceDevice)) < 0 ||
  929. (r = dshow_add_device(avctx, AudioDevice)) < 0) {
  930. ret = r;
  931. goto error;
  932. }
  933. }
  934. }
  935. if (ctx->list_options) {
  936. /* allow it to list crossbar options in dshow_open_device */
  937. ret = AVERROR_EXIT;
  938. goto error;
  939. }
  940. ctx->curbufsize[0] = 0;
  941. ctx->curbufsize[1] = 0;
  942. ctx->mutex = CreateMutex(NULL, 0, NULL);
  943. if (!ctx->mutex) {
  944. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  945. goto error;
  946. }
  947. ctx->event[1] = CreateEvent(NULL, 1, 0, NULL);
  948. if (!ctx->event[1]) {
  949. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  950. goto error;
  951. }
  952. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  953. if (r != S_OK) {
  954. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  955. goto error;
  956. }
  957. ctx->control = control;
  958. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event);
  959. if (r != S_OK) {
  960. av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n");
  961. goto error;
  962. }
  963. ctx->media_event = media_event;
  964. r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle);
  965. if (r != S_OK) {
  966. av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n");
  967. goto error;
  968. }
  969. proc = GetCurrentProcess();
  970. r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0],
  971. 0, 0, DUPLICATE_SAME_ACCESS);
  972. if (!r) {
  973. av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n");
  974. goto error;
  975. }
  976. r = IMediaControl_Run(control);
  977. if (r == S_FALSE) {
  978. OAFilterState pfs;
  979. r = IMediaControl_GetState(control, 0, &pfs);
  980. }
  981. if (r != S_OK) {
  982. av_log(avctx, AV_LOG_ERROR, "Could not run graph (sometimes caused by a device already in use by other application)\n");
  983. goto error;
  984. }
  985. ret = 0;
  986. error:
  987. if (devenum)
  988. ICreateDevEnum_Release(devenum);
  989. if (ret < 0)
  990. dshow_read_close(avctx);
  991. return ret;
  992. }
  993. /**
  994. * Checks media events from DirectShow and returns -1 on error or EOF. Also
  995. * purges all events that might be in the event queue to stop the trigger
  996. * of event notification.
  997. */
  998. static int dshow_check_event_queue(IMediaEvent *media_event)
  999. {
  1000. LONG_PTR p1, p2;
  1001. long code;
  1002. int ret = 0;
  1003. while (IMediaEvent_GetEvent(media_event, &code, &p1, &p2, 0) != E_ABORT) {
  1004. if (code == EC_COMPLETE || code == EC_DEVICE_LOST || code == EC_ERRORABORT)
  1005. ret = -1;
  1006. IMediaEvent_FreeEventParams(media_event, code, p1, p2);
  1007. }
  1008. return ret;
  1009. }
  1010. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  1011. {
  1012. struct dshow_ctx *ctx = s->priv_data;
  1013. AVPacketList *pktl = NULL;
  1014. while (!ctx->eof && !pktl) {
  1015. WaitForSingleObject(ctx->mutex, INFINITE);
  1016. pktl = ctx->pktl;
  1017. if (pktl) {
  1018. *pkt = pktl->pkt;
  1019. ctx->pktl = ctx->pktl->next;
  1020. av_free(pktl);
  1021. ctx->curbufsize[pkt->stream_index] -= pkt->size;
  1022. }
  1023. ResetEvent(ctx->event[1]);
  1024. ReleaseMutex(ctx->mutex);
  1025. if (!pktl) {
  1026. if (dshow_check_event_queue(ctx->media_event) < 0) {
  1027. ctx->eof = 1;
  1028. } else if (s->flags & AVFMT_FLAG_NONBLOCK) {
  1029. return AVERROR(EAGAIN);
  1030. } else {
  1031. WaitForMultipleObjects(2, ctx->event, 0, INFINITE);
  1032. }
  1033. }
  1034. }
  1035. return ctx->eof ? AVERROR(EIO) : pkt->size;
  1036. }
  1037. #define OFFSET(x) offsetof(struct dshow_ctx, x)
  1038. #define DEC AV_OPT_FLAG_DECODING_PARAM
  1039. static const AVOption options[] = {
  1040. { "video_size", "set video size given a string such as 640x480 or hd720.", OFFSET(requested_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  1041. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, INT_MAX, DEC },
  1042. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  1043. { "sample_rate", "set audio sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1044. { "sample_size", "set audio sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 16, DEC },
  1045. { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1046. { "audio_buffer_size", "set audio device buffer latency size in milliseconds (default is the device's default)", OFFSET(audio_buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1047. { "list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, DEC, "list_devices" },
  1048. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "list_devices" },
  1049. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "list_devices" },
  1050. { "list_options", "list available options for specified device", OFFSET(list_options), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, DEC, "list_options" },
  1051. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "list_options" },
  1052. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "list_options" },
  1053. { "video_device_number", "set video device number for devices with same name (starts at 0)", OFFSET(video_device_number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1054. { "audio_device_number", "set audio device number for devices with same name (starts at 0)", OFFSET(audio_device_number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  1055. { "video_pin_name", "select video capture pin by name", OFFSET(video_pin_name),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  1056. { "audio_pin_name", "select audio capture pin by name", OFFSET(audio_pin_name),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  1057. { "crossbar_video_input_pin_number", "set video input pin number for crossbar device", OFFSET(crossbar_video_input_pin_number), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC },
  1058. { "crossbar_audio_input_pin_number", "set audio input pin number for crossbar device", OFFSET(crossbar_audio_input_pin_number), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC },
  1059. { "show_video_device_dialog", "display property dialog for video capture device", OFFSET(show_video_device_dialog), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC, "show_video_device_dialog" },
  1060. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "show_video_device_dialog" },
  1061. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "show_video_device_dialog" },
  1062. { "show_audio_device_dialog", "display property dialog for audio capture device", OFFSET(show_audio_device_dialog), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC, "show_audio_device_dialog" },
  1063. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "show_audio_device_dialog" },
  1064. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "show_audio_device_dialog" },
  1065. { "show_video_crossbar_connection_dialog", "display property dialog for crossbar connecting pins filter on video device", OFFSET(show_video_crossbar_connection_dialog), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC, "show_video_crossbar_connection_dialog" },
  1066. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "show_video_crossbar_connection_dialog" },
  1067. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "show_video_crossbar_connection_dialog" },
  1068. { "show_audio_crossbar_connection_dialog", "display property dialog for crossbar connecting pins filter on audio device", OFFSET(show_audio_crossbar_connection_dialog), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC, "show_audio_crossbar_connection_dialog" },
  1069. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "show_audio_crossbar_connection_dialog" },
  1070. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "show_audio_crossbar_connection_dialog" },
  1071. { "show_analog_tv_tuner_dialog", "display property dialog for analog tuner filter", OFFSET(show_analog_tv_tuner_dialog), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC, "show_analog_tv_tuner_dialog" },
  1072. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "show_analog_tv_tuner_dialog" },
  1073. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "show_analog_tv_tuner_dialog" },
  1074. { "show_analog_tv_tuner_audio_dialog", "display property dialog for analog tuner audio filter", OFFSET(show_analog_tv_tuner_audio_dialog), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC, "show_analog_tv_tuner_dialog" },
  1075. { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, DEC, "show_analog_tv_tuner_audio_dialog" },
  1076. { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, DEC, "show_analog_tv_tuner_audio_dialog" },
  1077. { NULL },
  1078. };
  1079. static const AVClass dshow_class = {
  1080. .class_name = "dshow indev",
  1081. .item_name = av_default_item_name,
  1082. .option = options,
  1083. .version = LIBAVUTIL_VERSION_INT,
  1084. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  1085. };
  1086. AVInputFormat ff_dshow_demuxer = {
  1087. .name = "dshow",
  1088. .long_name = NULL_IF_CONFIG_SMALL("DirectShow capture"),
  1089. .priv_data_size = sizeof(struct dshow_ctx),
  1090. .read_header = dshow_read_header,
  1091. .read_packet = dshow_read_packet,
  1092. .read_close = dshow_read_close,
  1093. .flags = AVFMT_NOFILE,
  1094. .priv_class = &dshow_class,
  1095. };