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 /**
25 * @file
26 * GDI frame device demuxer
27 * @author Calvin Walton <calvin.walton@kepstin.ca>
28 * @author Christophe Gisquet <word1.word2@gmail.com>
29 */
30
31 #include "config.h"
37 #include <windows.h>
38
39 /**
40 * GDI Device Demuxer context
41 */
43 const AVClass *
class;
/**< Class for private options */
44
45 int frame_size;
/**< Size in bytes of the frame pixel data */
49
50 int draw_mouse;
/**< Draw mouse cursor (private option) */
53 int width;
/**< Width of the grab frame (private option) */
54 int height;
/**< Height of the grab frame (private option) */
55 int offset_x;
/**< Capture x offset (private option) */
56 int offset_y;
/**< Capture y offset (private option) */
57
58 HWND
hwnd;
/**< Handle of the window for the grab */
60 HDC
dest_hdc;
/**< Destination, source-compatible DC */
61 BITMAPINFO
bmi;
/**< Information describing DIB format */
62 HBITMAP
hbmp;
/**< Information on the bitmap captured */
63 void *
buffer;
/**< The buffer containing the bitmap image data */
64 RECT
clip_rect;
/**< The subarea of the screen or window to clip */
65
67
69 };
70
71 #define WIN32_API_ERROR(str) \
72 av_log(s1, AV_LOG_ERROR, str " (error %li)\n", GetLastError())
73
74 #define REGION_WND_BORDER 3
75
76 /**
77 * Callback to handle Windows messages for the region outline window.
78 *
79 * In particular, this handles painting the frame rectangle.
80 *
81 * @param hwnd The region outline window handle.
82 * @param msg The Windows message.
83 * @param wparam First Windows message parameter.
84 * @param lparam Second Windows message parameter.
85 * @return 0 success, !0 failure
86 */
87 static LRESULT CALLBACK
89 {
90 PAINTSTRUCT ps;
91 HDC hdc;
93
94 switch (msg) {
95 case WM_PAINT:
96 hdc = BeginPaint(hwnd, &ps);
97
98 GetClientRect(hwnd, &
rect);
99 FrameRect(hdc, &
rect, GetStockObject(BLACK_BRUSH));
100
102 FrameRect(hdc, &
rect, GetStockObject(WHITE_BRUSH));
103
105 FrameRect(hdc, &
rect, GetStockObject(BLACK_BRUSH));
106
107 EndPaint(hwnd, &ps);
108 break;
109 default:
110 return DefWindowProc(hwnd, msg, wparam, lparam);
111 }
112 return 0;
113 }
114
115 /**
116 * Initialize the region outline window.
117 *
118 * @param s1 The format context.
119 * @param gdigrab gdigrab context.
120 * @return 0 success, !0 failure
121 */
122 static int
124 {
125 HWND hwnd;
128 HRGN region_interior =
NULL;
129
130 DWORD style = WS_POPUP | WS_VISIBLE;
131 DWORD ex = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
132
135
136 AdjustWindowRectEx(&
rect, style, FALSE, ex);
137
138 // Create a window with no owner; use WC_DIALOG instead of writing a custom
139 // window class
140 hwnd = CreateWindowEx(ex, WC_DIALOG,
NULL, style,
rect.left,
rect.top,
143 if (!hwnd) {
146 }
147
148 // Set the window shape to only include the border area
149 GetClientRect(hwnd, &
rect);
150 region = CreateRectRgn(0, 0,
155 CombineRgn(region, region, region_interior, RGN_DIFF);
156 if (!SetWindowRgn(hwnd, region, FALSE)) {
159 }
160 // The "region" memory is now owned by the window
162 DeleteObject(region_interior);
163
165
166 ShowWindow(hwnd, SW_SHOW);
167
169
170 return 0;
171
173 if (region)
174 DeleteObject(region);
175 if (region_interior)
176 DeleteObject(region_interior);
177 if (hwnd)
178 DestroyWindow(hwnd);
179 return 1;
180 }
181
182 /**
183 * Cleanup/free the region outline window.
184 *
185 * @param s1 The format context.
186 * @param gdigrab gdigrab context.
187 */
188 static void
190 {
194 }
195
196 /**
197 * Process the Windows message queue.
198 *
199 * This is important to prevent Windows from thinking the window has become
200 * unresponsive. As well, things like WM_PAINT (to actually draw the window
201 * contents) are handled from the message queue context.
202 *
203 * @param s1 The format context.
204 * @param gdigrab gdigrab context.
205 */
206 static void
208 {
210 MSG msg;
211
212 while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
213 DispatchMessage(&msg);
214 }
215 }
216
217 /**
218 * Initializes the gdi grab device demuxer (public device demuxer API).
219 *
220 * @param s1 Context from avformat core
221 * @return AVERROR_IO error, 0 success
222 */
223 static int
225 {
227
234
235 const char *filename =
s1->url;
238
239 int bpp;
240 int horzres;
241 int vertres;
242 int desktophorzres;
243 int desktopvertres;
244 RECT virtual_rect;
246 BITMAP bmp;
248
249 if (!strncmp(filename, "title=", 6)) {
250 wchar_t *name_w =
NULL;
252
253 if(utf8towchar(
name, &name_w)) {
256 }
257 if(!name_w) {
260 }
261
266 "Can't find window '%s', aborting.\n",
name);
269 }
272 "Can't show region when grabbing a window.\n");
274 }
275 } else if (!strcmp(filename, "desktop")) {
277 } else if (!strncmp(filename, "hwnd=", 5)) {
278 char *p;
280
281 hwnd = (HWND) strtoull(
name, &p, 0);
282
283 if (p ==
NULL || p ==
name || p[0] ==
'0円')
284 {
286 "Invalid window handle '%s', must be a valid integer.\n",
name);
289 }
290 } else {
292 "Please use \"desktop\", \"title=<windowname>\" or \"hwnd=<hwnd>\" to specify your target.\n");
295 }
296
297 /* This will get the device context for the selected window, or if
298 * none, the primary screen */
304 }
306
309 desktophorzres = GetDeviceCaps(
source_hdc, DESKTOPHORZRES);
310 desktopvertres = GetDeviceCaps(
source_hdc, DESKTOPVERTRES);
311
313 GetClientRect(
hwnd, &virtual_rect);
314 /* window -- get the right height and width for scaling DPI */
315 virtual_rect.left = virtual_rect.left * desktophorzres / horzres;
316 virtual_rect.right = virtual_rect.right * desktophorzres / horzres;
317 virtual_rect.top = virtual_rect.top * desktopvertres / vertres;
318 virtual_rect.bottom = virtual_rect.bottom * desktopvertres / vertres;
319 } else {
320 /* desktop -- get the right height and width for scaling DPI */
321 virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
322 virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
323 virtual_rect.right = (virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN)) * desktophorzres / horzres;
324 virtual_rect.bottom = (virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN)) * desktopvertres / vertres;
325 }
326
327 /* If no width or height set, use full screen/window area */
333 } else {
338 }
339
340 if (
clip_rect.left < virtual_rect.left ||
343 clip_rect.bottom > virtual_rect.bottom) {
345 "Capture area (%li,%li),(%li,%li) extends outside window area (%li,%li),(%li,%li)",
348 virtual_rect.left, virtual_rect.top,
349 virtual_rect.right, virtual_rect.bottom);
352 }
353
354
357 "Found window %s, capturing %lix%lix%i at (%li,%li)\n",
362 } else {
364 "Capturing whole desktop as %lix%lix%i at (%li,%li)\n",
368 }
369
375 }
376
382 }
383
384 /* Create a DIB and select it into the dest_hdc */
385 bmi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
388 bmi.bmiHeader.biPlanes = 1;
389 bmi.bmiHeader.biBitCount = bpp;
390 bmi.bmiHeader.biCompression = BI_RGB;
391 bmi.bmiHeader.biSizeImage = 0;
392 bmi.bmiHeader.biXPelsPerMeter = 0;
393 bmi.bmiHeader.biYPelsPerMeter = 0;
394 bmi.bmiHeader.biClrUsed = 0;
395 bmi.bmiHeader.biClrImportant = 0;
402 }
403
408 }
409
410 /* Get info from the bitmap */
411 GetObject(
hbmp,
sizeof(BITMAP), &bmp);
412
414 if (!st) {
417 }
419
422 (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */;
425
433
435
440 }
441 }
442
444
448
449 return 0;
450
461 }
462
463 /**
464 * Paints a mouse pointer in a Win32 image.
465 *
466 * @param s1 Context of the log information
467 * @param s Current grad structure
468 */
470 {
471 CURSORINFO ci = {0};
472
473 #define CURSOR_ERROR(str) \
474 if (!gdigrab->cursor_error_printed) { \
475 WIN32_API_ERROR(str); \
476 gdigrab->cursor_error_printed = 1; \
477 }
478
479 ci.cbSize = sizeof(ci);
480
481 if (GetCursorInfo(&ci)) {
482 HCURSOR icon = CopyCursor(ci.hCursor);
493
494 if (ci.flags != CURSOR_SHOWING)
495 return;
496
497 if (!icon) {
498 /* Use the standard arrow cursor as a fallback.
499 * You'll probably only hit this in Wine, which can't fetch
500 * the current system cursor. */
501 icon = CopyCursor(LoadCursor(
NULL, IDC_ARROW));
502 }
503
504 if (!GetIconInfo(icon, &
info)) {
506 goto icon_error;
507 }
508
511
515
516 //that would keep the correct location of mouse with hidpi screens
517 pos.x =
pos.x * desktophorzres / horzres;
518 pos.y =
pos.y * desktopvertres / vertres;
519 } else {
521 goto icon_error;
522 }
523 } else {
524 //that would keep the correct location of mouse with hidpi screens
525 pos.x = ci.ptScreenPos.x * desktophorzres / horzres -
clip_rect.left -
info.xHotspot;
526 pos.y = ci.ptScreenPos.y * desktopvertres / vertres -
clip_rect.top -
info.yHotspot;
527 }
528
530 ci.ptScreenPos.x, ci.ptScreenPos.y,
pos.x,
pos.y);
531
536 }
537
538 icon_error:
540 DeleteObject(
info.hbmMask);
542 DeleteObject(
info.hbmColor);
543 if (icon)
544 DestroyCursor(icon);
545 } else {
547 }
548 }
549
550 /**
551 * Grabs a frame from gdi (public device demuxer API).
552 *
553 * @param s1 Context from avformat core
554 * @param pkt Packet holding the grabbed frame
555 * @return frame size in bytes
556 */
558 {
560
566
567 BITMAPFILEHEADER bfh;
569
571
572 /* Calculate the time of the next frame */
574
575 /* Run Window message processing queue */
578
579 /* wait based on the frame rate */
580 for (;;) {
583 if (delay <= 0) {
586 }
587 break;
588 }
591 } else {
593 }
594 }
595
599
600 /* Blit screen grab */
608 }
611
612 /* Copy bits to packet data */
613
614 bfh.bfType = 0x4d42; /* "BM" in little-endian */
615 bfh.bfSize = file_size;
616 bfh.bfReserved1 = 0;
617 bfh.bfReserved2 = 0;
619
620 memcpy(
pkt->
data, &bfh,
sizeof(bfh));
621
623
627
629
631
633 }
634
635 /**
636 * Closes gdi frame grabber (public device demuxer API).
637 *
638 * @param s1 Context from avformat core
639 * @return 0 success, !0 failure
640 */
642 {
644
647
649 ReleaseDC(
s->hwnd,
s->source_hdc);
651 DeleteDC(
s->dest_hdc);
653 DeleteObject(
s->hbmp);
655 DeleteDC(
s->source_hdc);
656
657 return 0;
658 }
659
660 #define OFFSET(x) offsetof(struct gdigrab, x)
661 #define DEC AV_OPT_FLAG_DECODING_PARAM
670 };
671
678 };
679
680 /** gdi grabber device demuxer declaration */
686 .priv_data_size =
sizeof(
struct gdigrab),
690 };