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"
36 #include <windows.h>
37
38 /**
39 * GDI Device Demuxer context
40 */
42 const AVClass *
class;
/**< Class for private options */
43
44 int frame_size;
/**< Size in bytes of the frame pixel data */
48
49 int draw_mouse;
/**< Draw mouse cursor (private option) */
52 int width;
/**< Width of the grab frame (private option) */
53 int height;
/**< Height of the grab frame (private option) */
54 int offset_x;
/**< Capture x offset (private option) */
55 int offset_y;
/**< Capture y offset (private option) */
56
57 HWND
hwnd;
/**< Handle of the window for the grab */
59 HDC
dest_hdc;
/**< Destination, source-compatible DC */
60 BITMAPINFO
bmi;
/**< Information describing DIB format */
61 HBITMAP
hbmp;
/**< Information on the bitmap captured */
62 void *
buffer;
/**< The buffer containing the bitmap image data */
63 RECT
clip_rect;
/**< The subarea of the screen or window to clip */
64
66
68 };
69
70 #define WIN32_API_ERROR(str) \
71 av_log(s1, AV_LOG_ERROR, str " (error %li)\n", GetLastError())
72
73 #define REGION_WND_BORDER 3
74
75 /**
76 * Callback to handle Windows messages for the region outline window.
77 *
78 * In particular, this handles painting the frame rectangle.
79 *
80 * @param hwnd The region outline window handle.
81 * @param msg The Windows message.
82 * @param wparam First Windows message parameter.
83 * @param lparam Second Windows message parameter.
84 * @return 0 success, !0 failure
85 */
86 static LRESULT CALLBACK
88 {
89 PAINTSTRUCT ps;
90 HDC hdc;
92
93 switch (msg) {
94 case WM_PAINT:
95 hdc = BeginPaint(hwnd, &ps);
96
97 GetClientRect(hwnd, &
rect);
98 FrameRect(hdc, &
rect, GetStockObject(BLACK_BRUSH));
99
101 FrameRect(hdc, &
rect, GetStockObject(WHITE_BRUSH));
102
104 FrameRect(hdc, &
rect, GetStockObject(BLACK_BRUSH));
105
106 EndPaint(hwnd, &ps);
107 break;
108 default:
109 return DefWindowProc(hwnd, msg, wparam, lparam);
110 }
111 return 0;
112 }
113
114 /**
115 * Initialize the region outline window.
116 *
117 * @param s1 The format context.
118 * @param gdigrab gdigrab context.
119 * @return 0 success, !0 failure
120 */
121 static int
123 {
124 HWND hwnd;
127 HRGN region_interior =
NULL;
128
129 DWORD style = WS_POPUP | WS_VISIBLE;
130 DWORD ex = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
131
134
135 AdjustWindowRectEx(&
rect, style, FALSE, ex);
136
137 // Create a window with no owner; use WC_DIALOG instead of writing a custom
138 // window class
139 hwnd = CreateWindowEx(ex, WC_DIALOG,
NULL, style,
rect.left,
rect.top,
142 if (!hwnd) {
145 }
146
147 // Set the window shape to only include the border area
148 GetClientRect(hwnd, &
rect);
149 region = CreateRectRgn(0, 0,
154 CombineRgn(region, region, region_interior, RGN_DIFF);
155 if (!SetWindowRgn(hwnd, region, FALSE)) {
158 }
159 // The "region" memory is now owned by the window
161 DeleteObject(region_interior);
162
164
165 ShowWindow(hwnd, SW_SHOW);
166
168
169 return 0;
170
172 if (region)
173 DeleteObject(region);
174 if (region_interior)
175 DeleteObject(region_interior);
176 if (hwnd)
177 DestroyWindow(hwnd);
178 return 1;
179 }
180
181 /**
182 * Cleanup/free the region outline window.
183 *
184 * @param s1 The format context.
185 * @param gdigrab gdigrab context.
186 */
187 static void
189 {
193 }
194
195 /**
196 * Process the Windows message queue.
197 *
198 * This is important to prevent Windows from thinking the window has become
199 * unresponsive. As well, things like WM_PAINT (to actually draw the window
200 * contents) are handled from the message queue context.
201 *
202 * @param s1 The format context.
203 * @param gdigrab gdigrab context.
204 */
205 static void
207 {
209 MSG msg;
210
211 while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
212 DispatchMessage(&msg);
213 }
214 }
215
216 /**
217 * Initializes the gdi grab device demuxer (public device demuxer API).
218 *
219 * @param s1 Context from avformat core
220 * @return AVERROR_IO error, 0 success
221 */
222 static int
224 {
226
233
234 const char *filename =
s1->url;
237
238 int bpp;
239 int horzres;
240 int vertres;
241 int desktophorzres;
242 int desktopvertres;
243 RECT virtual_rect;
245 BITMAP bmp;
247
248 if (!strncmp(filename, "title=", 6)) {
249 wchar_t *name_w =
NULL;
251
252 if(utf8towchar(
name, &name_w)) {
255 }
256 if(!name_w) {
259 }
260
265 "Can't find window '%s', aborting.\n",
name);
268 }
271 "Can't show region when grabbing a window.\n");
273 }
274 } else if (!strcmp(filename, "desktop")) {
276 } else {
278 "Please use \"desktop\" or \"title=<windowname>\" to specify your target.\n");
281 }
282
283 /* This will get the device context for the selected window, or if
284 * none, the primary screen */
290 }
292
295 desktophorzres = GetDeviceCaps(
source_hdc, DESKTOPHORZRES);
296 desktopvertres = GetDeviceCaps(
source_hdc, DESKTOPVERTRES);
297
299 GetClientRect(
hwnd, &virtual_rect);
300 /* window -- get the right height and width for scaling DPI */
301 virtual_rect.left = virtual_rect.left * desktophorzres / horzres;
302 virtual_rect.right = virtual_rect.right * desktophorzres / horzres;
303 virtual_rect.top = virtual_rect.top * desktopvertres / vertres;
304 virtual_rect.bottom = virtual_rect.bottom * desktopvertres / vertres;
305 } else {
306 /* desktop -- get the right height and width for scaling DPI */
307 virtual_rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
308 virtual_rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
309 virtual_rect.right = (virtual_rect.left + GetSystemMetrics(SM_CXVIRTUALSCREEN)) * desktophorzres / horzres;
310 virtual_rect.bottom = (virtual_rect.top + GetSystemMetrics(SM_CYVIRTUALSCREEN)) * desktopvertres / vertres;
311 }
312
313 /* If no width or height set, use full screen/window area */
319 } else {
324 }
325
326 if (
clip_rect.left < virtual_rect.left ||
329 clip_rect.bottom > virtual_rect.bottom) {
331 "Capture area (%li,%li),(%li,%li) extends outside window area (%li,%li),(%li,%li)",
334 virtual_rect.left, virtual_rect.top,
335 virtual_rect.right, virtual_rect.bottom);
338 }
339
340
343 "Found window %s, capturing %lix%lix%i at (%li,%li)\n",
348 } else {
350 "Capturing whole desktop as %lix%lix%i at (%li,%li)\n",
354 }
355
361 }
362
368 }
369
370 /* Create a DIB and select it into the dest_hdc */
371 bmi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
374 bmi.bmiHeader.biPlanes = 1;
375 bmi.bmiHeader.biBitCount = bpp;
376 bmi.bmiHeader.biCompression = BI_RGB;
377 bmi.bmiHeader.biSizeImage = 0;
378 bmi.bmiHeader.biXPelsPerMeter = 0;
379 bmi.bmiHeader.biYPelsPerMeter = 0;
380 bmi.bmiHeader.biClrUsed = 0;
381 bmi.bmiHeader.biClrImportant = 0;
388 }
389
394 }
395
396 /* Get info from the bitmap */
397 GetObject(
hbmp,
sizeof(BITMAP), &bmp);
398
400 if (!st) {
403 }
405
408 (bpp <= 8 ? (1 << bpp) : 0) * sizeof(RGBQUAD) /* palette size */;
411
419
421
426 }
427 }
428
430
434
435 return 0;
436
447 }
448
449 /**
450 * Paints a mouse pointer in a Win32 image.
451 *
452 * @param s1 Context of the log information
453 * @param s Current grad structure
454 */
456 {
457 CURSORINFO ci = {0};
458
459 #define CURSOR_ERROR(str) \
460 if (!gdigrab->cursor_error_printed) { \
461 WIN32_API_ERROR(str); \
462 gdigrab->cursor_error_printed = 1; \
463 }
464
465 ci.cbSize = sizeof(ci);
466
467 if (GetCursorInfo(&ci)) {
468 HCURSOR icon = CopyCursor(ci.hCursor);
479
480 if (ci.flags != CURSOR_SHOWING)
481 return;
482
483 if (!icon) {
484 /* Use the standard arrow cursor as a fallback.
485 * You'll probably only hit this in Wine, which can't fetch
486 * the current system cursor. */
487 icon = CopyCursor(LoadCursor(
NULL, IDC_ARROW));
488 }
489
490 if (!GetIconInfo(icon, &
info)) {
492 goto icon_error;
493 }
494
497
501
502 //that would keep the correct location of mouse with hidpi screens
503 pos.x =
pos.x * desktophorzres / horzres;
504 pos.y =
pos.y * desktopvertres / vertres;
505 } else {
507 goto icon_error;
508 }
509 } else {
510 //that would keep the correct location of mouse with hidpi screens
511 pos.x = ci.ptScreenPos.x * desktophorzres / horzres -
clip_rect.left -
info.xHotspot;
512 pos.y = ci.ptScreenPos.y * desktopvertres / vertres -
clip_rect.top -
info.yHotspot;
513 }
514
516 ci.ptScreenPos.x, ci.ptScreenPos.y,
pos.x,
pos.y);
517
522 }
523
524 icon_error:
526 DeleteObject(
info.hbmMask);
528 DeleteObject(
info.hbmColor);
529 if (icon)
530 DestroyCursor(icon);
531 } else {
533 }
534 }
535
536 /**
537 * Grabs a frame from gdi (public device demuxer API).
538 *
539 * @param s1 Context from avformat core
540 * @param pkt Packet holding the grabbed frame
541 * @return frame size in bytes
542 */
544 {
546
552
553 BITMAPFILEHEADER bfh;
555
556 int64_t curtime, delay;
557
558 /* Calculate the time of the next frame */
560
561 /* Run Window message processing queue */
564
565 /* wait based on the frame rate */
566 for (;;) {
569 if (delay <= 0) {
572 }
573 break;
574 }
577 } else {
579 }
580 }
581
585
586 /* Blit screen grab */
594 }
597
598 /* Copy bits to packet data */
599
600 bfh.bfType = 0x4d42; /* "BM" in little-endian */
601 bfh.bfSize = file_size;
602 bfh.bfReserved1 = 0;
603 bfh.bfReserved2 = 0;
605
606 memcpy(
pkt->
data, &bfh,
sizeof(bfh));
607
609
613
615
617
619 }
620
621 /**
622 * Closes gdi frame grabber (public device demuxer API).
623 *
624 * @param s1 Context from avformat core
625 * @return 0 success, !0 failure
626 */
628 {
630
633
635 ReleaseDC(
s->hwnd,
s->source_hdc);
637 DeleteDC(
s->dest_hdc);
639 DeleteObject(
s->hbmp);
641 DeleteDC(
s->source_hdc);
642
643 return 0;
644 }
645
646 #define OFFSET(x) offsetof(struct gdigrab, x)
647 #define DEC AV_OPT_FLAG_DECODING_PARAM
656 };
657
664 };
665
666 /** gdi grabber device demuxer declaration */
670 .priv_data_size =
sizeof(
struct gdigrab),
676 };