You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

716 lines
20KB

  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 "libavutil/opt.h"
  22. #include "avdevice.h"
  23. #include "dshow.h"
  24. struct dshow_ctx {
  25. const AVClass *class;
  26. IGraphBuilder *graph;
  27. char *device_name[2];
  28. int list_devices;
  29. IBaseFilter *device_filter[2];
  30. IPin *device_pin[2];
  31. libAVFilter *capture_filter[2];
  32. libAVPin *capture_pin[2];
  33. HANDLE mutex;
  34. HANDLE event;
  35. AVPacketList *pktl;
  36. unsigned int curbufsize;
  37. unsigned int video_frame_num;
  38. IMediaControl *control;
  39. };
  40. static enum PixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)
  41. {
  42. switch(biCompression) {
  43. case MKTAG('U', 'Y', 'V', 'Y'):
  44. return PIX_FMT_UYVY422;
  45. case MKTAG('Y', 'U', 'Y', '2'):
  46. return PIX_FMT_YUYV422;
  47. case MKTAG('I', '4', '2', '0'):
  48. return PIX_FMT_YUV420P;
  49. case BI_RGB:
  50. switch(biBitCount) { /* 1-8 are untested */
  51. case 1:
  52. return PIX_FMT_MONOWHITE;
  53. case 4:
  54. return PIX_FMT_RGB4;
  55. case 8:
  56. return PIX_FMT_RGB8;
  57. case 16:
  58. return PIX_FMT_RGB555;
  59. case 24:
  60. return PIX_FMT_BGR24;
  61. case 32:
  62. return PIX_FMT_RGB32;
  63. }
  64. }
  65. return PIX_FMT_NONE;
  66. }
  67. static enum CodecID dshow_codecid(DWORD biCompression)
  68. {
  69. switch(biCompression) {
  70. case MKTAG('d', 'v', 's', 'd'):
  71. return CODEC_ID_DVVIDEO;
  72. case MKTAG('M', 'J', 'P', 'G'):
  73. case MKTAG('m', 'j', 'p', 'g'):
  74. return CODEC_ID_MJPEG;
  75. }
  76. return CODEC_ID_NONE;
  77. }
  78. static int
  79. dshow_read_close(AVFormatContext *s)
  80. {
  81. struct dshow_ctx *ctx = s->priv_data;
  82. AVPacketList *pktl;
  83. if (ctx->control) {
  84. IMediaControl_Stop(ctx->control);
  85. IMediaControl_Release(ctx->control);
  86. }
  87. if (ctx->capture_pin[VideoDevice])
  88. libAVPin_Release(ctx->capture_pin[VideoDevice]);
  89. if (ctx->capture_pin[AudioDevice])
  90. libAVPin_Release(ctx->capture_pin[AudioDevice]);
  91. if (ctx->capture_filter[VideoDevice])
  92. libAVFilter_Release(ctx->capture_filter[VideoDevice]);
  93. if (ctx->capture_filter[AudioDevice])
  94. libAVFilter_Release(ctx->capture_filter[AudioDevice]);
  95. if (ctx->device_pin[VideoDevice])
  96. IPin_Release(ctx->device_pin[VideoDevice]);
  97. if (ctx->device_pin[AudioDevice])
  98. IPin_Release(ctx->device_pin[AudioDevice]);
  99. if (ctx->device_filter[VideoDevice])
  100. IBaseFilter_Release(ctx->device_filter[VideoDevice]);
  101. if (ctx->device_filter[AudioDevice])
  102. IBaseFilter_Release(ctx->device_filter[AudioDevice]);
  103. if (ctx->graph) {
  104. IEnumFilters *fenum;
  105. int r;
  106. r = IGraphBuilder_EnumFilters(ctx->graph, &fenum);
  107. if (r == S_OK) {
  108. IBaseFilter *f;
  109. IEnumFilters_Reset(fenum);
  110. while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK)
  111. IGraphBuilder_RemoveFilter(ctx->graph, f);
  112. IEnumFilters_Release(fenum);
  113. }
  114. IGraphBuilder_Release(ctx->graph);
  115. }
  116. if (ctx->device_name[0])
  117. av_free(ctx->device_name[0]);
  118. if (ctx->device_name[1])
  119. av_free(ctx->device_name[1]);
  120. if(ctx->mutex)
  121. CloseHandle(ctx->mutex);
  122. if(ctx->event)
  123. CloseHandle(ctx->event);
  124. pktl = ctx->pktl;
  125. while (pktl) {
  126. AVPacketList *next = pktl->next;
  127. av_destruct_packet(&pktl->pkt);
  128. av_free(pktl);
  129. pktl = next;
  130. }
  131. return 0;
  132. }
  133. static char *dup_wchar_to_utf8(wchar_t *w)
  134. {
  135. char *s = NULL;
  136. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  137. s = av_malloc(l);
  138. if (s)
  139. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  140. return s;
  141. }
  142. static int shall_we_drop(AVFormatContext *s)
  143. {
  144. struct dshow_ctx *ctx = s->priv_data;
  145. const uint8_t dropscore[] = {62, 75, 87, 100};
  146. const int ndropscores = FF_ARRAY_ELEMS(dropscore);
  147. unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer;
  148. if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) {
  149. av_log(s, AV_LOG_ERROR,
  150. "real-time buffer %d%% full! frame dropped!\n", buffer_fullness);
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. static void
  156. callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time)
  157. {
  158. AVFormatContext *s = priv_data;
  159. struct dshow_ctx *ctx = s->priv_data;
  160. AVPacketList **ppktl, *pktl_next;
  161. // dump_videohdr(s, vdhdr);
  162. if(shall_we_drop(s))
  163. return;
  164. WaitForSingleObject(ctx->mutex, INFINITE);
  165. pktl_next = av_mallocz(sizeof(AVPacketList));
  166. if(!pktl_next)
  167. goto fail;
  168. if(av_new_packet(&pktl_next->pkt, buf_size) < 0) {
  169. av_free(pktl_next);
  170. goto fail;
  171. }
  172. pktl_next->pkt.stream_index = index;
  173. pktl_next->pkt.pts = time;
  174. memcpy(pktl_next->pkt.data, buf, buf_size);
  175. for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
  176. *ppktl = pktl_next;
  177. ctx->curbufsize += buf_size;
  178. SetEvent(ctx->event);
  179. ReleaseMutex(ctx->mutex);
  180. return;
  181. fail:
  182. ReleaseMutex(ctx->mutex);
  183. return;
  184. }
  185. /**
  186. * Cycle through available devices using the device enumerator devenum,
  187. * retrieve the device with type specified by devtype and return the
  188. * pointer to the object found in *pfilter.
  189. * If pfilter is NULL, list all device names.
  190. */
  191. static int
  192. dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
  193. enum dshowDeviceType devtype, IBaseFilter **pfilter)
  194. {
  195. struct dshow_ctx *ctx = avctx->priv_data;
  196. IBaseFilter *device_filter = NULL;
  197. IEnumMoniker *classenum = NULL;
  198. IMoniker *m = NULL;
  199. const char *device_name = ctx->device_name[devtype];
  200. int r;
  201. const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory,
  202. &CLSID_AudioInputDeviceCategory };
  203. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  204. r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype],
  205. (IEnumMoniker **) &classenum, 0);
  206. if (r != S_OK) {
  207. av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n",
  208. devtypename);
  209. return AVERROR(EIO);
  210. }
  211. while (IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK && !device_filter) {
  212. IPropertyBag *bag = NULL;
  213. char *buf = NULL;
  214. VARIANT var;
  215. r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag);
  216. if (r != S_OK)
  217. goto fail1;
  218. var.vt = VT_BSTR;
  219. r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL);
  220. if (r != S_OK)
  221. goto fail1;
  222. buf = dup_wchar_to_utf8(var.bstrVal);
  223. if (pfilter) {
  224. if (strcmp(device_name, buf))
  225. goto fail1;
  226. IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter);
  227. } else {
  228. av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf);
  229. }
  230. fail1:
  231. if (buf)
  232. av_free(buf);
  233. if (bag)
  234. IPropertyBag_Release(bag);
  235. IMoniker_Release(m);
  236. }
  237. IEnumMoniker_Release(classenum);
  238. if (pfilter) {
  239. if (!device_filter) {
  240. av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n",
  241. devtypename);
  242. return AVERROR(EIO);
  243. }
  244. *pfilter = device_filter;
  245. }
  246. return 0;
  247. }
  248. static int
  249. dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
  250. enum dshowDeviceType devtype)
  251. {
  252. struct dshow_ctx *ctx = avctx->priv_data;
  253. IBaseFilter *device_filter = NULL;
  254. IGraphBuilder *graph = ctx->graph;
  255. IEnumPins *pins = 0;
  256. IPin *device_pin = NULL;
  257. libAVPin *capture_pin = NULL;
  258. libAVFilter *capture_filter = NULL;
  259. int ret = AVERROR(EIO);
  260. IPin *pin;
  261. int r;
  262. const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };
  263. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  264. const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
  265. if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) {
  266. ret = r;
  267. goto error;
  268. }
  269. ctx->device_filter [devtype] = device_filter;
  270. r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
  271. if (r != S_OK) {
  272. av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n");
  273. goto error;
  274. }
  275. r = IBaseFilter_EnumPins(device_filter, &pins);
  276. if (r != S_OK) {
  277. av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");
  278. goto error;
  279. }
  280. while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !device_pin) {
  281. IKsPropertySet *p = NULL;
  282. IEnumMediaTypes *types;
  283. PIN_INFO info = {0};
  284. AM_MEDIA_TYPE *type;
  285. GUID category;
  286. DWORD r2;
  287. IPin_QueryPinInfo(pin, &info);
  288. IBaseFilter_Release(info.pFilter);
  289. if (info.dir != PINDIR_OUTPUT)
  290. goto next;
  291. if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)
  292. goto next;
  293. if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,
  294. NULL, 0, &category, sizeof(GUID), &r2) != S_OK)
  295. goto next;
  296. if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))
  297. goto next;
  298. if (IPin_EnumMediaTypes(pin, &types) != S_OK)
  299. goto next;
  300. IEnumMediaTypes_Reset(types);
  301. while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK && !device_pin) {
  302. if (IsEqualGUID(&type->majortype, mediatype[devtype])) {
  303. device_pin = pin;
  304. goto next;
  305. }
  306. CoTaskMemFree(type);
  307. }
  308. next:
  309. if (types)
  310. IEnumMediaTypes_Release(types);
  311. if (p)
  312. IKsPropertySet_Release(p);
  313. if (device_pin != pin)
  314. IPin_Release(pin);
  315. }
  316. if (!device_pin) {
  317. av_log(avctx, AV_LOG_ERROR,
  318. "Could not find output pin from %s capture device.\n", devtypename);
  319. goto error;
  320. }
  321. ctx->device_pin[devtype] = device_pin;
  322. capture_filter = libAVFilter_Create(avctx, callback, devtype);
  323. if (!capture_filter) {
  324. av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n");
  325. goto error;
  326. }
  327. ctx->capture_filter[devtype] = capture_filter;
  328. r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter,
  329. filter_name[devtype]);
  330. if (r != S_OK) {
  331. av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n");
  332. goto error;
  333. }
  334. libAVPin_AddRef(capture_filter->pin);
  335. capture_pin = capture_filter->pin;
  336. ctx->capture_pin[devtype] = capture_pin;
  337. r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL);
  338. if (r != S_OK) {
  339. av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n");
  340. goto error;
  341. }
  342. ret = 0;
  343. error:
  344. if (pins)
  345. IEnumPins_Release(pins);
  346. return ret;
  347. }
  348. static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt)
  349. {
  350. switch (sample_fmt) {
  351. case AV_SAMPLE_FMT_U8: return CODEC_ID_PCM_U8;
  352. case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE;
  353. case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE;
  354. default: return CODEC_ID_NONE; /* Should never happen. */
  355. }
  356. }
  357. static enum SampleFormat sample_fmt_bits_per_sample(int bits)
  358. {
  359. switch (bits) {
  360. case 8: return AV_SAMPLE_FMT_U8;
  361. case 16: return AV_SAMPLE_FMT_S16;
  362. case 32: return AV_SAMPLE_FMT_S32;
  363. default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */
  364. }
  365. }
  366. static int
  367. dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap,
  368. enum dshowDeviceType devtype)
  369. {
  370. struct dshow_ctx *ctx = avctx->priv_data;
  371. AM_MEDIA_TYPE type;
  372. AVCodecContext *codec;
  373. AVStream *st;
  374. int ret = AVERROR(EIO);
  375. st = av_new_stream(avctx, devtype);
  376. if (!st) {
  377. ret = AVERROR(ENOMEM);
  378. goto error;
  379. }
  380. ctx->capture_filter[devtype]->stream_index = st->index;
  381. libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type);
  382. codec = st->codec;
  383. if (devtype == VideoDevice) {
  384. BITMAPINFOHEADER *bih = NULL;
  385. if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) {
  386. VIDEOINFOHEADER *v = (void *) type.pbFormat;
  387. bih = &v->bmiHeader;
  388. } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) {
  389. VIDEOINFOHEADER2 *v = (void *) type.pbFormat;
  390. bih = &v->bmiHeader;
  391. }
  392. if (!bih) {
  393. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  394. goto error;
  395. }
  396. codec->time_base = ap->time_base;
  397. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  398. codec->width = bih->biWidth;
  399. codec->height = bih->biHeight;
  400. codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount);
  401. if (codec->pix_fmt == PIX_FMT_NONE) {
  402. codec->codec_id = dshow_codecid(bih->biCompression);
  403. if (codec->codec_id == CODEC_ID_NONE) {
  404. av_log(avctx, AV_LOG_ERROR, "Unknown compression type. "
  405. "Please report verbose (-v 9) debug information.\n");
  406. dshow_read_close(avctx);
  407. return AVERROR_PATCHWELCOME;
  408. }
  409. codec->bits_per_coded_sample = bih->biBitCount;
  410. } else {
  411. codec->codec_id = CODEC_ID_RAWVIDEO;
  412. if (bih->biCompression == BI_RGB) {
  413. codec->bits_per_coded_sample = bih->biBitCount;
  414. codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  415. if (codec->extradata) {
  416. codec->extradata_size = 9;
  417. memcpy(codec->extradata, "BottomUp", 9);
  418. }
  419. }
  420. }
  421. } else {
  422. WAVEFORMATEX *fx = NULL;
  423. if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) {
  424. fx = (void *) type.pbFormat;
  425. }
  426. if (!fx) {
  427. av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n");
  428. goto error;
  429. }
  430. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  431. codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample);
  432. codec->codec_id = waveform_codec_id(codec->sample_fmt);
  433. codec->sample_rate = fx->nSamplesPerSec;
  434. codec->channels = fx->nChannels;
  435. }
  436. av_set_pts_info(st, 64, 1, 10000000);
  437. ret = 0;
  438. error:
  439. return ret;
  440. }
  441. static int parse_device_name(AVFormatContext *avctx)
  442. {
  443. struct dshow_ctx *ctx = avctx->priv_data;
  444. char **device_name = ctx->device_name;
  445. char *name = av_strdup(avctx->filename);
  446. char *tmp = name;
  447. int ret = 1;
  448. char *type;
  449. while ((type = strtok(tmp, "="))) {
  450. char *token = strtok(NULL, ":");
  451. tmp = NULL;
  452. if (!strcmp(type, "video")) {
  453. device_name[0] = token;
  454. } else if (!strcmp(type, "audio")) {
  455. device_name[1] = token;
  456. } else {
  457. device_name[0] = NULL;
  458. device_name[1] = NULL;
  459. break;
  460. }
  461. }
  462. if (!device_name[0] && !device_name[1]) {
  463. ret = 0;
  464. } else {
  465. if (device_name[0])
  466. device_name[0] = av_strdup(device_name[0]);
  467. if (device_name[1])
  468. device_name[1] = av_strdup(device_name[1]);
  469. }
  470. av_free(name);
  471. return ret;
  472. }
  473. static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap)
  474. {
  475. struct dshow_ctx *ctx = avctx->priv_data;
  476. IGraphBuilder *graph = NULL;
  477. ICreateDevEnum *devenum = NULL;
  478. IMediaControl *control = NULL;
  479. int ret = AVERROR(EIO);
  480. int r;
  481. if (!ctx->list_devices && !parse_device_name(avctx)) {
  482. av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n");
  483. goto error;
  484. }
  485. CoInitialize(0);
  486. r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  487. &IID_IGraphBuilder, (void **) &graph);
  488. if (r != S_OK) {
  489. av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n");
  490. goto error;
  491. }
  492. ctx->graph = graph;
  493. r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  494. &IID_ICreateDevEnum, (void **) &devenum);
  495. if (r != S_OK) {
  496. av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
  497. goto error;
  498. }
  499. if (ctx->list_devices) {
  500. av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n");
  501. dshow_cycle_devices(avctx, devenum, VideoDevice, NULL);
  502. av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
  503. dshow_cycle_devices(avctx, devenum, AudioDevice, NULL);
  504. ret = AVERROR_EXIT;
  505. goto error;
  506. }
  507. if (ctx->device_name[VideoDevice]) {
  508. ret = dshow_open_device(avctx, devenum, VideoDevice);
  509. if (ret < 0)
  510. goto error;
  511. ret = dshow_add_device(avctx, ap, VideoDevice);
  512. if (ret < 0)
  513. goto error;
  514. }
  515. if (ctx->device_name[AudioDevice]) {
  516. ret = dshow_open_device(avctx, devenum, AudioDevice);
  517. if (ret < 0)
  518. goto error;
  519. ret = dshow_add_device(avctx, ap, AudioDevice);
  520. if (ret < 0)
  521. goto error;
  522. }
  523. ctx->mutex = CreateMutex(NULL, 0, NULL);
  524. if (!ctx->mutex) {
  525. av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n");
  526. goto error;
  527. }
  528. ctx->event = CreateEvent(NULL, 1, 0, NULL);
  529. if (!ctx->event) {
  530. av_log(avctx, AV_LOG_ERROR, "Could not create Event\n");
  531. goto error;
  532. }
  533. r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control);
  534. if (r != S_OK) {
  535. av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n");
  536. goto error;
  537. }
  538. ctx->control = control;
  539. r = IMediaControl_Run(control);
  540. if (r == S_FALSE) {
  541. OAFilterState pfs;
  542. r = IMediaControl_GetState(control, 0, &pfs);
  543. }
  544. if (r != S_OK) {
  545. av_log(avctx, AV_LOG_ERROR, "Could not run filter\n");
  546. goto error;
  547. }
  548. ret = 0;
  549. error:
  550. if (ret < 0)
  551. dshow_read_close(avctx);
  552. if (devenum)
  553. ICreateDevEnum_Release(devenum);
  554. return ret;
  555. }
  556. static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
  557. {
  558. struct dshow_ctx *ctx = s->priv_data;
  559. AVPacketList *pktl = NULL;
  560. while (!pktl) {
  561. WaitForSingleObject(ctx->mutex, INFINITE);
  562. pktl = ctx->pktl;
  563. if (ctx->pktl) {
  564. *pkt = ctx->pktl->pkt;
  565. ctx->pktl = ctx->pktl->next;
  566. av_free(pktl);
  567. }
  568. ResetEvent(ctx->event);
  569. ReleaseMutex(ctx->mutex);
  570. if (!pktl) {
  571. if (s->flags & AVFMT_FLAG_NONBLOCK) {
  572. return AVERROR(EAGAIN);
  573. } else {
  574. WaitForSingleObject(ctx->event, INFINITE);
  575. }
  576. }
  577. }
  578. ctx->curbufsize -= pkt->size;
  579. return pkt->size;
  580. }
  581. #define OFFSET(x) offsetof(struct dshow_ctx, x)
  582. #define DEC AV_OPT_FLAG_DECODING_PARAM
  583. static const AVOption options[] = {
  584. { "list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_devices" },
  585. { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_devices" },
  586. { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_devices" },
  587. { NULL },
  588. };
  589. static const AVClass dshow_class = {
  590. .class_name = "DirectShow indev",
  591. .item_name = av_default_item_name,
  592. .option = options,
  593. .version = LIBAVUTIL_VERSION_INT,
  594. };
  595. AVInputFormat ff_dshow_demuxer = {
  596. "dshow",
  597. NULL_IF_CONFIG_SMALL("DirectShow capture"),
  598. sizeof(struct dshow_ctx),
  599. NULL,
  600. dshow_read_header,
  601. dshow_read_packet,
  602. dshow_read_close,
  603. .flags = AVFMT_NOFILE,
  604. .priv_class = &dshow_class,
  605. };