[FFmpeg-devel] [PATCH] gdigrab: added option to select window by HWND.

caleb.javahacking at gmail.com caleb.javahacking at gmail.com
Fri Oct 22 14:44:27 EEST 2021


From: Caleb Whiting <caleb.javahacking at gmail.com>

This allows more precise selection of windows.
---
 libavdevice/gdigrab.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/libavdevice/gdigrab.c b/libavdevice/gdigrab.c
index c069232472..266da3414c 100644
--- a/libavdevice/gdigrab.c
+++ b/libavdevice/gdigrab.c
@@ -231,9 +231,9 @@ gdigrab_read_header(AVFormatContext *s1)
     HBITMAP hbmp   = NULL;
     void *buffer   = NULL;
 
-    const char *filename = s1->url;
-    const char *name     = NULL;
-    AVStream   *st       = NULL;
+    const char *spec           = s1->url;
+    const char *spec_value     = NULL;
+    AVStream   *st             = NULL;
 
     int bpp;
     int horzres;
@@ -245,11 +245,26 @@ gdigrab_read_header(AVFormatContext *s1)
     BITMAP bmp;
     int ret;
 
-    if (!strncmp(filename, "title=", 6)) {
+    if (!strncmp(spec, "hwnd=", 5)) {
+        spec_value = spec + 5;
+        hwnd = (HWND) atoi(spec_value);
+        if (!IsWindow(hwnd)) {
+            av_log(s1, AV_LOG_ERROR,
+                   "Invalid window '%s', aborting.\n", spec_value);
+            ret = AVERROR(EIO);
+            goto error;
+        }
+
+        if (gdigrab->show_region) {
+            av_log(s1, AV_LOG_WARNING,
+                    "Can't show region when grabbing a window.\n");
+            gdigrab->show_region = 0;
+        }
+    } else if (!strncmp(spec, "title=", 6)) {
         wchar_t *name_w = NULL;
-        name = filename + 6;
+        spec_value = spec + 6;
 
-        if(utf8towchar(name, &name_w)) {
+        if(utf8towchar(spec_value, &name_w)) {
             ret = AVERROR(errno);
             goto error;
         }
@@ -262,7 +277,7 @@ gdigrab_read_header(AVFormatContext *s1)
         av_freep(&name_w);
         if (!hwnd) {
             av_log(s1, AV_LOG_ERROR,
-                   "Can't find window '%s', aborting.\n", name);
+                   "Can't find window '%s', aborting.\n", spec_value);
             ret = AVERROR(EIO);
             goto error;
         }
@@ -271,7 +286,7 @@ gdigrab_read_header(AVFormatContext *s1)
                     "Can't show region when grabbing a window.\n");
             gdigrab->show_region = 0;
         }
-    } else if (!strcmp(filename, "desktop")) {
+    } else if (!strcmp(spec, "desktop")) {
         hwnd = NULL;
     } else {
         av_log(s1, AV_LOG_ERROR,
@@ -338,10 +353,10 @@ gdigrab_read_header(AVFormatContext *s1)
     }
 
 
-    if (name) {
+    if (spec_value) {
         av_log(s1, AV_LOG_INFO,
                "Found window %s, capturing %lix%lix%i at (%li,%li)\n",
-               name,
+               spec_value,
                clip_rect.right - clip_rect.left,
                clip_rect.bottom - clip_rect.top,
                bpp, clip_rect.left, clip_rect.top);
-- 
2.25.1



More information about the ffmpeg-devel mailing list