gdigrab.c 22 KB

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