gdigrab.c 22 KB

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