gdigrab.c 22 KB

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