gdigrab.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * GDI video grab interface
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * Copyright (C) 2013 Calvin Walton <calvin.walton@kepstin.ca>
  7. * Copyright (C) 2007-2010 Christophe Gisquet <word1.word2@gmail.com>
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public License
  11. * as published by the Free Software Foundation; either version 2.1
  12. * of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * GDI frame device demuxer
  26. * @author Calvin Walton <calvin.walton@kepstin.ca>
  27. * @author Christophe Gisquet <word1.word2@gmail.com>
  28. */
  29. #include "config.h"
  30. #include "libavformat/internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/time.h"
  33. #include "libavutil/wchar_filename.h"
  34. #include <windows.h>
  35. /**
  36. * GDI Device Demuxer context
  37. */
  38. struct gdigrab {
  39. const AVClass *class; /**< Class for private options */
  40. int frame_size; /**< Size in bytes of the frame pixel data */
  41. int header_size; /**< Size in bytes of the DIB header */
  42. AVRational time_base; /**< Time base */
  43. int64_t time_frame; /**< Current time */
  44. int draw_mouse; /**< Draw mouse cursor (private option) */
  45. int show_region; /**< Draw border (private option) */
  46. AVRational framerate; /**< Capture framerate (private option) */
  47. int width; /**< Width of the grab frame (private option) */
  48. int height; /**< Height of the grab frame (private option) */
  49. int offset_x; /**< Capture x offset (private option) */
  50. int offset_y; /**< Capture y offset (private option) */
  51. HWND hwnd; /**< Handle of the window for the grab */
  52. HDC source_hdc; /**< Source device context */
  53. HDC dest_hdc; /**< Destination, source-compatible DC */
  54. BITMAPINFO bmi; /**< Information describing DIB format */
  55. HBITMAP hbmp; /**< Information on the bitmap captured */
  56. void *buffer; /**< The buffer containing the bitmap image data */
  57. RECT clip_rect; /**< The subarea of the screen or window to clip */
  58. HWND region_hwnd; /**< Handle of the region border window */
  59. int cursor_error_printed;
  60. };
  61. #define WIN32_API_ERROR(str) \
  62. av_log(s1, AV_LOG_ERROR, str " (error %li)\n", GetLastError())
  63. #define REGION_WND_BORDER 3
  64. /**
  65. * Callback to handle Windows messages for the region outline window.
  66. *
  67. * In particular, this handles painting the frame rectangle.
  68. *
  69. * @param hwnd The region outline window handle.
  70. * @param msg The Windows message.
  71. * @param wparam First Windows message parameter.
  72. * @param lparam Second Windows message parameter.
  73. * @return 0 success, !0 failure
  74. */
  75. static LRESULT CALLBACK
  76. gdigrab_region_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  77. {
  78. PAINTSTRUCT ps;
  79. HDC hdc;
  80. RECT rect;
  81. switch (msg) {
  82. case WM_PAINT:
  83. hdc = BeginPaint(hwnd, &ps);
  84. GetClientRect(hwnd, &rect);
  85. FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
  86. rect.left++; rect.top++; rect.right--; rect.bottom--;
  87. FrameRect(hdc, &rect, GetStockObject(WHITE_BRUSH));
  88. rect.left++; rect.top++; rect.right--; rect.bottom--;
  89. FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
  90. EndPaint(hwnd, &ps);
  91. break;
  92. default:
  93. return DefWindowProc(hwnd, msg, wparam, lparam);
  94. }
  95. return 0;
  96. }
  97. /**
  98. * Initialize the region outline window.
  99. *
  100. * @param s1 The format context.
  101. * @param gdigrab gdigrab context.
  102. * @return 0 success, !0 failure
  103. */
  104. static int
  105. gdigrab_region_wnd_init(AVFormatContext *s1, struct gdigrab *gdigrab)
  106. {
  107. HWND hwnd;
  108. RECT rect = gdigrab->clip_rect;
  109. HRGN region = NULL;
  110. HRGN region_interior = NULL;
  111. DWORD style = WS_POPUP | WS_VISIBLE;
  112. DWORD ex = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
  113. rect.left -= REGION_WND_BORDER; rect.top -= REGION_WND_BORDER;
  114. rect.right += REGION_WND_BORDER; rect.bottom += REGION_WND_BORDER;
  115. AdjustWindowRectEx(&rect, style, FALSE, ex);
  116. // Create a window with no owner; use WC_DIALOG instead of writing a custom
  117. // window class
  118. hwnd = CreateWindowEx(ex, WC_DIALOG, NULL, style, rect.left, rect.top,
  119. rect.right - rect.left, rect.bottom - rect.top,
  120. NULL, NULL, NULL, NULL);
  121. if (!hwnd) {
  122. WIN32_API_ERROR("Could not create region display window");
  123. goto error;
  124. }
  125. // Set the window shape to only include the border area
  126. GetClientRect(hwnd, &rect);
  127. region = CreateRectRgn(0, 0,
  128. rect.right - rect.left, rect.bottom - rect.top);
  129. region_interior = CreateRectRgn(REGION_WND_BORDER, REGION_WND_BORDER,
  130. rect.right - rect.left - REGION_WND_BORDER,
  131. rect.bottom - rect.top - REGION_WND_BORDER);
  132. CombineRgn(region, region, region_interior, RGN_DIFF);
  133. if (!SetWindowRgn(hwnd, region, FALSE)) {
  134. WIN32_API_ERROR("Could not set window region");
  135. goto error;
  136. }
  137. // The "region" memory is now owned by the window
  138. region = NULL;
  139. DeleteObject(region_interior);
  140. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) gdigrab_region_wnd_proc);
  141. ShowWindow(hwnd, SW_SHOW);
  142. gdigrab->region_hwnd = hwnd;
  143. return 0;
  144. error:
  145. if (region)
  146. DeleteObject(region);
  147. if (region_interior)
  148. DeleteObject(region_interior);
  149. if (hwnd)
  150. DestroyWindow(hwnd);
  151. return 1;
  152. }
  153. /**
  154. * Cleanup/free the region outline window.
  155. *
  156. * @param s1 The format context.
  157. * @param gdigrab gdigrab context.
  158. */
  159. static void
  160. gdigrab_region_wnd_destroy(AVFormatContext *s1, struct gdigrab *gdigrab)
  161. {
  162. if (gdigrab->region_hwnd)
  163. DestroyWindow(gdigrab->region_hwnd);
  164. gdigrab->region_hwnd = NULL;
  165. }
  166. /**
  167. * Process the Windows message queue.
  168. *
  169. * This is important to prevent Windows from thinking the window has become
  170. * unresponsive. As well, things like WM_PAINT (to actually draw the window
  171. * contents) are handled from the message queue context.
  172. *
  173. * @param s1 The format context.
  174. * @param gdigrab gdigrab context.
  175. */
  176. static void
  177. gdigrab_region_wnd_update(AVFormatContext *s1, struct gdigrab *gdigrab)
  178. {
  179. HWND hwnd = gdigrab->region_hwnd;
  180. MSG msg;
  181. while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
  182. DispatchMessage(&msg);
  183. }
  184. }
  185. /**
  186. * Initializes the gdi grab device demuxer (public device demuxer API).
  187. *
  188. * @param s1 Context from avformat core
  189. * @return AVERROR_IO error, 0 success
  190. */
  191. static int
  192. gdigrab_read_header(AVFormatContext *s1)
  193. {
  194. struct gdigrab *gdigrab = s1->priv_data;
  195. HWND hwnd;
  196. HDC source_hdc = NULL;
  197. HDC dest_hdc = NULL;
  198. BITMAPINFO bmi;
  199. HBITMAP hbmp = NULL;
  200. void *buffer = NULL;
  201. const char *filename = s1->url;
  202. const char *name = NULL;
  203. AVStream *st = NULL;
  204. int bpp;
  205. int horzres;
  206. int vertres;
  207. int desktophorzres;
  208. int desktopvertres;
  209. RECT virtual_rect;
  210. RECT clip_rect;
  211. BITMAP bmp;
  212. int ret;
  213. if (!strncmp(filename, "title=", 6)) {
  214. wchar_t *name_w = NULL;
  215. name = filename + 6;
  216. if(utf8towchar(name, &name_w)) {
  217. ret = AVERROR(errno);
  218. goto error;
  219. }
  220. if(!name_w) {
  221. ret = AVERROR(EINVAL);
  222. goto error;
  223. }
  224. hwnd = FindWindowW(NULL, name_w);
  225. av_freep(&name_w);
  226. if (!hwnd) {
  227. av_log(s1, AV_LOG_ERROR,
  228. "Can't find window '%s', aborting.\n", name);
  229. ret = AVERROR(EIO);
  230. goto error;
  231. }
  232. if (gdigrab->show_region) {
  233. av_log(s1, AV_LOG_WARNING,
  234. "Can't show region when grabbing a window.\n");
  235. gdigrab->show_region = 0;
  236. }
  237. } else if (!strcmp(filename, "desktop")) {
  238. hwnd = NULL;
  239. } else {
  240. av_log(s1, AV_LOG_ERROR,
  241. "Please use \"desktop\" or \"title=<windowname>\" to specify your target.\n");
  242. ret = AVERROR(EIO);
  243. goto error;
  244. }
  245. /* This will get the device context for the selected window, or if
  246. * none, the primary screen */
  247. source_hdc = GetDC(hwnd);
  248. if (!source_hdc) {
  249. WIN32_API_ERROR("Couldn't get window device context");
  250. ret = AVERROR(EIO);
  251. goto error;
  252. }
  253. bpp = GetDeviceCaps(source_hdc, BITSPIXEL);
  254. horzres = GetDeviceCaps(source_hdc, HORZRES);
  255. vertres = GetDeviceCaps(source_hdc, VERTRES);
  256. desktophorzres = GetDeviceCaps(source_hdc, DESKTOPHORZRES);
  257. desktopvertres = GetDeviceCaps(source_hdc, DESKTOPVERTRES);
  258. if (hwnd) {
  259. GetClientRect(hwnd, &virtual_rect);
  260. /* window -- get the right height and width for scaling DPI */
  261. virtual_rect.left = virtual_rect.left * desktophorzres / horzres;
  262. virtual_rect.right = virtual_rect.right * desktophorzres / horzres;
  263. virtual_rect.top = virtual_rect.top * desktopvertres / vertres;
  264. virtual_rect.bottom = virtual_rect.bottom * desktopvertres / vertres;
  265. } else {
  266. /* desktop -- get the right height and width for scaling DPI */
  267. virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
  268. virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
  269. virtual_rect.right = (virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN)) * desktophorzres / horzres;
  270. virtual_rect.bottom = (virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN)) * desktopvertres / vertres;
  271. }
  272. /* If no width or height set, use full screen/window area */
  273. if (!gdigrab->width || !gdigrab->height) {
  274. clip_rect.left = virtual_rect.left;
  275. clip_rect.top = virtual_rect.top;
  276. clip_rect.right = virtual_rect.right;
  277. clip_rect.bottom = virtual_rect.bottom;
  278. } else {
  279. clip_rect.left = gdigrab->offset_x;
  280. clip_rect.top = gdigrab->offset_y;
  281. clip_rect.right = gdigrab->width + gdigrab->offset_x;
  282. clip_rect.bottom = gdigrab->height + gdigrab->offset_y;
  283. }
  284. if (clip_rect.left < virtual_rect.left ||
  285. clip_rect.top < virtual_rect.top ||
  286. clip_rect.right > virtual_rect.right ||
  287. clip_rect.bottom > virtual_rect.bottom) {
  288. av_log(s1, AV_LOG_ERROR,
  289. "Capture area (%li,%li),(%li,%li) extends outside window area (%li,%li),(%li,%li)",
  290. clip_rect.left, clip_rect.top,
  291. clip_rect.right, clip_rect.bottom,
  292. virtual_rect.left, virtual_rect.top,
  293. virtual_rect.right, virtual_rect.bottom);
  294. ret = AVERROR(EIO);
  295. goto error;
  296. }
  297. if (name) {
  298. av_log(s1, AV_LOG_INFO,
  299. "Found window %s, capturing %lix%lix%i at (%li,%li)\n",
  300. name,
  301. clip_rect.right - clip_rect.left,
  302. clip_rect.bottom - clip_rect.top,
  303. bpp, clip_rect.left, clip_rect.top);
  304. } else {
  305. av_log(s1, AV_LOG_INFO,
  306. "Capturing whole desktop as %lix%lix%i at (%li,%li)\n",
  307. clip_rect.right - clip_rect.left,
  308. clip_rect.bottom - clip_rect.top,
  309. bpp, clip_rect.left, clip_rect.top);
  310. }
  311. if (clip_rect.right - clip_rect.left <= 0 ||
  312. clip_rect.bottom - clip_rect.top <= 0 || bpp%8) {
  313. av_log(s1, AV_LOG_ERROR, "Invalid properties, aborting\n");
  314. ret = AVERROR(EIO);
  315. goto error;
  316. }
  317. dest_hdc = CreateCompatibleDC(source_hdc);
  318. if (!dest_hdc) {
  319. WIN32_API_ERROR("Screen DC CreateCompatibleDC");
  320. ret = AVERROR(EIO);
  321. goto error;
  322. }
  323. /* Create a DIB and select it into the dest_hdc */
  324. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  325. bmi.bmiHeader.biWidth = clip_rect.right - clip_rect.left;
  326. bmi.bmiHeader.biHeight = -(clip_rect.bottom - clip_rect.top);
  327. bmi.bmiHeader.biPlanes = 1;
  328. bmi.bmiHeader.biBitCount = bpp;
  329. bmi.bmiHeader.biCompression = BI_RGB;
  330. bmi.bmiHeader.biSizeImage = 0;
  331. bmi.bmiHeader.biXPelsPerMeter = 0;
  332. bmi.bmiHeader.biYPelsPerMeter = 0;
  333. bmi.bmiHeader.biClrUsed = 0;
  334. bmi.bmiHeader.biClrImportant = 0;
  335. hbmp = CreateDIBSection(dest_hdc, &bmi, DIB_RGB_COLORS,
  336. &buffer, NULL, 0);
  337. if (!hbmp) {
  338. WIN32_API_ERROR("Creating DIB Section");
  339. ret = AVERROR(EIO);
  340. goto error;
  341. }
  342. if (!SelectObject(dest_hdc, hbmp)) {
  343. WIN32_API_ERROR("SelectObject");
  344. ret = AVERROR(EIO);
  345. goto error;
  346. }
  347. /* Get info from the bitmap */
  348. GetObject(hbmp, sizeof(BITMAP), &bmp);
  349. st = avformat_new_stream(s1, NULL);
  350. if (!st) {
  351. ret = AVERROR(ENOMEM);
  352. goto error;
  353. }
  354. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  355. gdigrab->frame_size = bmp.bmWidthBytes * bmp.bmHeight * bmp.bmPlanes;
  356. gdigrab->header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
  357. (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */;
  358. gdigrab->time_base = av_inv_q(gdigrab->framerate);
  359. gdigrab->time_frame = av_gettime_relative() / av_q2d(gdigrab->time_base);
  360. gdigrab->hwnd = hwnd;
  361. gdigrab->source_hdc = source_hdc;
  362. gdigrab->dest_hdc = dest_hdc;
  363. gdigrab->hbmp = hbmp;
  364. gdigrab->bmi = bmi;
  365. gdigrab->buffer = buffer;
  366. gdigrab->clip_rect = clip_rect;
  367. gdigrab->cursor_error_printed = 0;
  368. if (gdigrab->show_region) {
  369. if (gdigrab_region_wnd_init(s1, gdigrab)) {
  370. ret = AVERROR(EIO);
  371. goto error;
  372. }
  373. }
  374. st->avg_frame_rate = av_inv_q(gdigrab->time_base);
  375. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  376. st->codecpar->codec_id = AV_CODEC_ID_BMP;
  377. st->codecpar->bit_rate = (gdigrab->header_size + gdigrab->frame_size) * 1/av_q2d(gdigrab->time_base) * 8;
  378. return 0;
  379. error:
  380. if (source_hdc)
  381. ReleaseDC(hwnd, source_hdc);
  382. if (dest_hdc)
  383. DeleteDC(dest_hdc);
  384. if (hbmp)
  385. DeleteObject(hbmp);
  386. if (source_hdc)
  387. DeleteDC(source_hdc);
  388. return ret;
  389. }
  390. /**
  391. * Paints a mouse pointer in a Win32 image.
  392. *
  393. * @param s1 Context of the log information
  394. * @param s Current grad structure
  395. */
  396. static void paint_mouse_pointer(AVFormatContext *s1, struct gdigrab *gdigrab)
  397. {
  398. CURSORINFO ci = {0};
  399. #define CURSOR_ERROR(str) \
  400. if (!gdigrab->cursor_error_printed) { \
  401. WIN32_API_ERROR(str); \
  402. gdigrab->cursor_error_printed = 1; \
  403. }
  404. ci.cbSize = sizeof(ci);
  405. if (GetCursorInfo(&ci)) {
  406. HCURSOR icon = CopyCursor(ci.hCursor);
  407. ICONINFO info;
  408. POINT pos;
  409. RECT clip_rect = gdigrab->clip_rect;
  410. HWND hwnd = gdigrab->hwnd;
  411. int horzres = GetDeviceCaps(gdigrab->source_hdc, HORZRES);
  412. int vertres = GetDeviceCaps(gdigrab->source_hdc, VERTRES);
  413. int desktophorzres = GetDeviceCaps(gdigrab->source_hdc, DESKTOPHORZRES);
  414. int desktopvertres = GetDeviceCaps(gdigrab->source_hdc, DESKTOPVERTRES);
  415. info.hbmMask = NULL;
  416. info.hbmColor = NULL;
  417. if (ci.flags != CURSOR_SHOWING)
  418. return;
  419. if (!icon) {
  420. /* Use the standard arrow cursor as a fallback.
  421. * You'll probably only hit this in Wine, which can't fetch
  422. * the current system cursor. */
  423. icon = CopyCursor(LoadCursor(NULL, IDC_ARROW));
  424. }
  425. if (!GetIconInfo(icon, &info)) {
  426. CURSOR_ERROR("Could not get icon info");
  427. goto icon_error;
  428. }
  429. if (hwnd) {
  430. RECT rect;
  431. if (GetWindowRect(hwnd, &rect)) {
  432. pos.x = ci.ptScreenPos.x - clip_rect.left - info.xHotspot - rect.left;
  433. pos.y = ci.ptScreenPos.y - clip_rect.top - info.yHotspot - rect.top;
  434. //that would keep the correct location of mouse with hidpi screens
  435. pos.x = pos.x * desktophorzres / horzres;
  436. pos.y = pos.y * desktopvertres / vertres;
  437. } else {
  438. CURSOR_ERROR("Couldn't get window rectangle");
  439. goto icon_error;
  440. }
  441. } else {
  442. //that would keep the correct location of mouse with hidpi screens
  443. pos.x = ci.ptScreenPos.x * desktophorzres / horzres - clip_rect.left - info.xHotspot;
  444. pos.y = ci.ptScreenPos.y * desktopvertres / vertres - clip_rect.top - info.yHotspot;
  445. }
  446. av_log(s1, AV_LOG_DEBUG, "Cursor pos (%li,%li) -> (%li,%li)\n",
  447. ci.ptScreenPos.x, ci.ptScreenPos.y, pos.x, pos.y);
  448. if (pos.x >= 0 && pos.x <= clip_rect.right - clip_rect.left &&
  449. pos.y >= 0 && pos.y <= clip_rect.bottom - clip_rect.top) {
  450. if (!DrawIcon(gdigrab->dest_hdc, pos.x, pos.y, icon))
  451. CURSOR_ERROR("Couldn't draw icon");
  452. }
  453. icon_error:
  454. if (info.hbmMask)
  455. DeleteObject(info.hbmMask);
  456. if (info.hbmColor)
  457. DeleteObject(info.hbmColor);
  458. if (icon)
  459. DestroyCursor(icon);
  460. } else {
  461. CURSOR_ERROR("Couldn't get cursor info");
  462. }
  463. }
  464. /**
  465. * Grabs a frame from gdi (public device demuxer API).
  466. *
  467. * @param s1 Context from avformat core
  468. * @param pkt Packet holding the grabbed frame
  469. * @return frame size in bytes
  470. */
  471. static int gdigrab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  472. {
  473. struct gdigrab *gdigrab = s1->priv_data;
  474. HDC dest_hdc = gdigrab->dest_hdc;
  475. HDC source_hdc = gdigrab->source_hdc;
  476. RECT clip_rect = gdigrab->clip_rect;
  477. AVRational time_base = gdigrab->time_base;
  478. int64_t time_frame = gdigrab->time_frame;
  479. BITMAPFILEHEADER bfh;
  480. int file_size = gdigrab->header_size + gdigrab->frame_size;
  481. int64_t curtime, delay;
  482. /* Calculate the time of the next frame */
  483. time_frame += INT64_C(1000000);
  484. /* Run Window message processing queue */
  485. if (gdigrab->show_region)
  486. gdigrab_region_wnd_update(s1, gdigrab);
  487. /* wait based on the frame rate */
  488. for (;;) {
  489. curtime = av_gettime_relative();
  490. delay = time_frame * av_q2d(time_base) - curtime;
  491. if (delay <= 0) {
  492. if (delay < INT64_C(-1000000) * av_q2d(time_base)) {
  493. time_frame += INT64_C(1000000);
  494. }
  495. break;
  496. }
  497. if (s1->flags & AVFMT_FLAG_NONBLOCK) {
  498. return AVERROR(EAGAIN);
  499. } else {
  500. av_usleep(delay);
  501. }
  502. }
  503. if (av_new_packet(pkt, file_size) < 0)
  504. return AVERROR(ENOMEM);
  505. pkt->pts = av_gettime();
  506. /* Blit screen grab */
  507. if (!BitBlt(dest_hdc, 0, 0,
  508. clip_rect.right - clip_rect.left,
  509. clip_rect.bottom - clip_rect.top,
  510. source_hdc,
  511. clip_rect.left, clip_rect.top, SRCCOPY | CAPTUREBLT)) {
  512. WIN32_API_ERROR("Failed to capture image");
  513. return AVERROR(EIO);
  514. }
  515. if (gdigrab->draw_mouse)
  516. paint_mouse_pointer(s1, gdigrab);
  517. /* Copy bits to packet data */
  518. bfh.bfType = 0x4d42; /* "BM" in little-endian */
  519. bfh.bfSize = file_size;
  520. bfh.bfReserved1 = 0;
  521. bfh.bfReserved2 = 0;
  522. bfh.bfOffBits = gdigrab->header_size;
  523. memcpy(pkt->data, &bfh, sizeof(bfh));
  524. memcpy(pkt->data + sizeof(bfh), &gdigrab->bmi.bmiHeader, sizeof(gdigrab->bmi.bmiHeader));
  525. if (gdigrab->bmi.bmiHeader.biBitCount <= 8)
  526. GetDIBColorTable(dest_hdc, 0, 1 << gdigrab->bmi.bmiHeader.biBitCount,
  527. (RGBQUAD *) (pkt->data + sizeof(bfh) + sizeof(gdigrab->bmi.bmiHeader)));
  528. memcpy(pkt->data + gdigrab->header_size, gdigrab->buffer, gdigrab->frame_size);
  529. gdigrab->time_frame = time_frame;
  530. return gdigrab->header_size + gdigrab->frame_size;
  531. }
  532. /**
  533. * Closes gdi frame grabber (public device demuxer API).
  534. *
  535. * @param s1 Context from avformat core
  536. * @return 0 success, !0 failure
  537. */
  538. static int gdigrab_read_close(AVFormatContext *s1)
  539. {
  540. struct gdigrab *s = s1->priv_data;
  541. if (s->show_region)
  542. gdigrab_region_wnd_destroy(s1, s);
  543. if (s->source_hdc)
  544. ReleaseDC(s->hwnd, s->source_hdc);
  545. if (s->dest_hdc)
  546. DeleteDC(s->dest_hdc);
  547. if (s->hbmp)
  548. DeleteObject(s->hbmp);
  549. if (s->source_hdc)
  550. DeleteDC(s->source_hdc);
  551. return 0;
  552. }
  553. #define OFFSET(x) offsetof(struct gdigrab, x)
  554. #define DEC AV_OPT_FLAG_DECODING_PARAM
  555. static const AVOption options[] = {
  556. { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  557. { "show_region", "draw border around capture area", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  558. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, INT_MAX, DEC },
  559. { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  560. { "offset_x", "capture area x offset", OFFSET(offset_x), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },
  561. { "offset_y", "capture area y offset", OFFSET(offset_y), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },
  562. { NULL },
  563. };
  564. static const AVClass gdigrab_class = {
  565. .class_name = "GDIgrab indev",
  566. .item_name = av_default_item_name,
  567. .option = options,
  568. .version = LIBAVUTIL_VERSION_INT,
  569. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  570. };
  571. /** gdi grabber device demuxer declaration */
  572. const AVInputFormat ff_gdigrab_demuxer = {
  573. .name = "gdigrab",
  574. .long_name = NULL_IF_CONFIG_SMALL("GDI API Windows frame grabber"),
  575. .priv_data_size = sizeof(struct gdigrab),
  576. .read_header = gdigrab_read_header,
  577. .read_packet = gdigrab_read_packet,
  578. .read_close = gdigrab_read_close,
  579. .flags = AVFMT_NOFILE,
  580. .priv_class = &gdigrab_class,
  581. };