dshow.c 48 KB

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