Browse code

Remove the legacy X11 screen grabber

The XCB screen grabber is a drop-in replacement and not under GPL.

Diego Biurrun authored on 2016/07/27 18:25:19
Showing 7 changed files
... ...
@@ -61,6 +61,7 @@ version <next>:
61 61
 - Duck TrueMotion 2.0 Real Time decoder
62 62
 - Intel QSV video scaling and deinterlacing filter
63 63
 - OpenH264 decoder wrapper
64
+- Removed the legacy X11 screen grabber, use XCB instead
64 65
 
65 66
 
66 67
 version 11:
... ...
@@ -13,7 +13,6 @@ configure to activate them. In this case, Libav's license changes to GPL v2+.
13 13
 
14 14
 Specifically, the GPL parts of Libav are
15 15
 
16
-- the X11 grabber in libavdevice/x11grab.c
17 16
 - the texi2pod.pl tool
18 17
 - the following filters in libavfilter:
19 18
     - vf_blackframe.c
... ...
@@ -227,7 +227,6 @@ External library support:
227 227
   --enable-libxcb-xfixes     X11 mouse rendering [auto]
228 228
   --enable-libxvid           MPEG-4 ASP video encoding
229 229
   --enable-openssl           crypto
230
-  --enable-x11grab           X11 grabbing through xlib (legacy, use xcb instead)
231 230
   --enable-zlib              compression [autodetect]
232 231
 
233 232
   The following libraries provide various hardware acceleration features:
... ...
@@ -1242,7 +1241,6 @@ EXTERNAL_LIBRARY_GPL_LIST="
1242 1242
     libx265
1243 1243
     libxavs
1244 1244
     libxvid
1245
-    x11grab
1246 1245
 "
1247 1246
 
1248 1247
 EXTERNAL_LIBRARY_NONFREE_LIST="
... ...
@@ -2369,7 +2367,6 @@ sndio_outdev_deps="sndio_h"
2369 2369
 v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
2370 2370
 vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines"
2371 2371
 vfwcap_indev_extralibs="-lavicap32"
2372
-x11grab_indev_deps="x11grab"
2373 2372
 x11grab_xcb_indev_deps="libxcb"
2374 2373
 
2375 2374
 # protocols
... ...
@@ -4752,10 +4749,10 @@ fi
4752 4752
 
4753 4753
 check_lib X11/Xlib.h XOpenDisplay -lX11 && enable xlib
4754 4754
 
4755
-if enabled libxcb || enabled x11grab && ! disabled libxcb; then
4755
+if enabled libxcb; then
4756 4756
     check_pkg_config xcb-shape xcb/shape.h xcb_shape_rectangles || {
4757 4757
         enabled libxcb && die "ERROR: libxcb not found";
4758
-    } && disable x11grab && enable libxcb
4758
+    } && enable libxcb
4759 4759
 
4760 4760
     disabled libxcb_shm ||
4761 4761
         check_pkg_config xcb-shm xcb/shm.h xcb_shm_attach || {
... ...
@@ -4771,12 +4768,6 @@ if enabled libxcb || enabled x11grab && ! disabled libxcb; then
4771 4771
     add_extralibs "$xcb_event_libs $xcb_shm_libs $xcb_xfixes_libs"
4772 4772
 fi
4773 4773
 
4774
-if enabled x11grab; then
4775
-    enabled xlib || die "ERROR: Xlib not found"
4776
-    require Xext X11/extensions/XShm.h XShmCreateImage -lXext
4777
-    require Xfixes X11/extensions/Xfixes.h XFixesGetCursorImage -lXfixes
4778
-fi
4779
-
4780 4774
 enabled vaapi &&
4781 4775
     check_code cc "va/va.h" "vaCreateSurfaces(0, 0, 0, 0, 0, 0, 0, 0)" ||
4782 4776
     disable vaapi
... ...
@@ -23,7 +23,6 @@ OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_dec.o sndio.o
23 23
 OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_enc.o sndio.o
24 24
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o
25 25
 OBJS-$(CONFIG_VFWCAP_INDEV)              += vfwcap.o
26
-OBJS-$(CONFIG_X11GRAB_INDEV)             += x11grab.o
27 26
 OBJS-$(CONFIG_X11GRAB_XCB_INDEV)         += xcbgrab.o
28 27
 
29 28
 # external libraries
... ...
@@ -58,7 +58,6 @@ void avdevice_register_all(void)
58 58
     REGISTER_INOUTDEV(SNDIO,            sndio);
59 59
     REGISTER_INDEV   (V4L2,             v4l2);
60 60
     REGISTER_INDEV   (VFWCAP,           vfwcap);
61
-    REGISTER_INDEV   (X11GRAB,          x11grab);
62 61
     REGISTER_INDEV   (X11GRAB_XCB,      x11grab_xcb);
63 62
 
64 63
     /* external libraries */
... ...
@@ -36,7 +36,7 @@
36 36
  * (de)muxers in libavdevice are of the AVFMT_NOFILE type (they use their own
37 37
  * I/O functions). The filename passed to avformat_open_input() often does not
38 38
  * refer to an actually existing file, but has some special device-specific
39
- * meaning - e.g. for x11grab it is the display name.
39
+ * meaning - e.g. for x11grab_xcb it is the display name.
40 40
  *
41 41
  * To use libavdevice, simply call avdevice_register_all() to register all
42 42
  * compiled muxers and demuxers. They all use standard libavformat API.
43 43
deleted file mode 100644
... ...
@@ -1,647 +0,0 @@
1
-/*
2
- * X11 video grab interface
3
- *
4
- * This file is part of Libav.
5
- *
6
- * Libav integration:
7
- * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org>
8
- *                    Edouard Gomez <ed.gomez@free.fr>
9
- *
10
- * This file contains code from grab.c:
11
- * Copyright (c) 2000-2001 Fabrice Bellard
12
- *
13
- * This file contains code from the xvidcap project:
14
- * Copyright (C) 1997-1998 Rasca, Berlin
15
- *               2003-2004 Karl H. Beckers, Frankfurt
16
- *
17
- * Libav is free software; you can redistribute it and/or modify
18
- * it under the terms of the GNU General Public License as published by
19
- * the Free Software Foundation; either version 2 of the License, or
20
- * (at your option) any later version.
21
- *
22
- * Libav is distributed in the hope that it will be useful,
23
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
- * GNU General Public License for more details.
26
- *
27
- * You should have received a copy of the GNU General Public License
28
- * along with Libav; if not, write to the Free Software
29
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30
- */
31
-
32
-/**
33
- * @file
34
- * X11 frame device demuxer
35
- * @author Clemens Fruhwirth <clemens@endorphin.org>
36
- * @author Edouard Gomez <ed.gomez@free.fr>
37
- */
38
-
39
-#include "config.h"
40
-
41
-#include <time.h>
42
-#include <sys/shm.h>
43
-
44
-#include <X11/X.h>
45
-#include <X11/Xlib.h>
46
-#include <X11/Xlibint.h>
47
-#include <X11/Xproto.h>
48
-#include <X11/Xutil.h>
49
-
50
-#include <X11/extensions/shape.h>
51
-#include <X11/extensions/Xfixes.h>
52
-#include <X11/extensions/XShm.h>
53
-
54
-#include "libavutil/internal.h"
55
-#include "libavutil/log.h"
56
-#include "libavutil/opt.h"
57
-#include "libavutil/parseutils.h"
58
-#include "libavutil/time.h"
59
-
60
-#include "libavformat/avformat.h"
61
-#include "libavformat/internal.h"
62
-
63
-/** X11 device demuxer context */
64
-typedef struct X11GrabContext {
65
-    const AVClass *class;    /**< Class for private options. */
66
-    int frame_size;          /**< Size in bytes of a grabbed frame */
67
-    AVRational time_base;    /**< Time base */
68
-    int64_t time_frame;      /**< Current time */
69
-
70
-    char *video_size;        /**< String describing video size, set by a private option. */
71
-    int height;              /**< Height of the grab frame */
72
-    int width;               /**< Width of the grab frame */
73
-    int x_off;               /**< Horizontal top-left corner coordinate */
74
-    int y_off;               /**< Vertical top-left corner coordinate */
75
-
76
-    Display *dpy;            /**< X11 display from which x11grab grabs frames */
77
-    XImage *image;           /**< X11 image holding the grab */
78
-    int use_shm;             /**< !0 when using XShm extension */
79
-    XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm info */
80
-    int draw_mouse;          /**< Set by a private option. */
81
-    int follow_mouse;        /**< Set by a private option. */
82
-    int show_region;         /**< set by a private option. */
83
-    char *framerate;         /**< Set by a private option. */
84
-
85
-    Window region_win;       /**< This is used by show_region option. */
86
-} X11GrabContext;
87
-
88
-#define REGION_WIN_BORDER 3
89
-
90
-/**
91
- * Draw grabbing region window
92
- *
93
- * @param s x11grab context
94
- */
95
-static void x11grab_draw_region_win(X11GrabContext *s)
96
-{
97
-    Display *dpy = s->dpy;
98
-    Window win   = s->region_win;
99
-    int screen = DefaultScreen(dpy);
100
-    GC gc = XCreateGC(dpy, win, 0, 0);
101
-
102
-    XSetForeground(dpy, gc, WhitePixel(dpy, screen));
103
-    XSetBackground(dpy, gc, BlackPixel(dpy, screen));
104
-    XSetLineAttributes(dpy, gc, REGION_WIN_BORDER, LineDoubleDash, 0, 0);
105
-    XDrawRectangle(dpy, win, gc, 1, 1,
106
-                   (s->width  + REGION_WIN_BORDER * 2) - 1 * 2 - 1,
107
-                   (s->height + REGION_WIN_BORDER * 2) - 1 * 2 - 1);
108
-    XFreeGC(dpy, gc);
109
-}
110
-
111
-/**
112
- * Initialize grabbing region window
113
- *
114
- * @param s x11grab context
115
- */
116
-static void x11grab_region_win_init(X11GrabContext *s)
117
-{
118
-    Display *dpy = s->dpy;
119
-    XRectangle rect;
120
-    XSetWindowAttributes attribs = { .override_redirect = True };
121
-    int screen = DefaultScreen(dpy);
122
-
123
-    s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
124
-                                  s->x_off  - REGION_WIN_BORDER,
125
-                                  s->y_off  - REGION_WIN_BORDER,
126
-                                  s->width  + REGION_WIN_BORDER * 2,
127
-                                  s->height + REGION_WIN_BORDER * 2,
128
-                                  0, CopyFromParent,
129
-                                  InputOutput, CopyFromParent,
130
-                                  CWOverrideRedirect, &attribs);
131
-    rect.x      = 0;
132
-    rect.y      = 0;
133
-    rect.width  = s->width;
134
-    rect.height = s->height;
135
-    XShapeCombineRectangles(dpy, s->region_win,
136
-                            ShapeBounding, REGION_WIN_BORDER, REGION_WIN_BORDER,
137
-                            &rect, 1, ShapeSubtract, 0);
138
-    XMapWindow(dpy, s->region_win);
139
-    XSelectInput(dpy, s->region_win, ExposureMask | StructureNotifyMask);
140
-    x11grab_draw_region_win(s);
141
-}
142
-
143
-static int setup_shm(AVFormatContext *s, Display *dpy, XImage **image)
144
-{
145
-    X11GrabContext *g = s->priv_data;
146
-    int scr           = XDefaultScreen(dpy);
147
-    XImage *img       = XShmCreateImage(dpy, DefaultVisual(dpy, scr),
148
-                                        DefaultDepth(dpy, scr), ZPixmap, NULL,
149
-                                        &g->shminfo, g->width, g->height);
150
-
151
-    g->shminfo.shmid = shmget(IPC_PRIVATE, img->bytes_per_line * img->height,
152
-                              IPC_CREAT | 0777);
153
-
154
-    if (g->shminfo.shmid == -1) {
155
-        av_log(s, AV_LOG_ERROR, "Cannot get shared memory!\n");
156
-        return AVERROR(ENOMEM);
157
-    }
158
-
159
-    g->shminfo.shmaddr  = img->data = shmat(g->shminfo.shmid, 0, 0);
160
-    g->shminfo.readOnly = False;
161
-
162
-    if (!XShmAttach(dpy, &g->shminfo)) {
163
-        av_log(s, AV_LOG_ERROR, "Failed to attach shared memory!\n");
164
-        /* needs some better error subroutine :) */
165
-        return AVERROR(EIO);
166
-    }
167
-
168
-    *image = img;
169
-    return 0;
170
-}
171
-
172
-static int setup_mouse(Display *dpy, int screen)
173
-{
174
-    int ev_ret, ev_err;
175
-
176
-    if (XFixesQueryExtension(dpy, &ev_ret, &ev_err)) {
177
-        Window root = RootWindow(dpy, screen);
178
-        XFixesSelectCursorInput(dpy, root, XFixesDisplayCursorNotifyMask);
179
-        return 0;
180
-    }
181
-
182
-    return AVERROR(ENOSYS);
183
-}
184
-
185
-static int pixfmt_from_image(AVFormatContext *s, XImage *image, int *pix_fmt)
186
-{
187
-    av_log(s, AV_LOG_DEBUG,
188
-           "Image r 0x%.6lx g 0x%.6lx b 0x%.6lx and depth %i\n",
189
-           image->red_mask,
190
-           image->green_mask,
191
-           image->blue_mask,
192
-           image->bits_per_pixel);
193
-
194
-    switch (image->bits_per_pixel) {
195
-    case 8:
196
-        *pix_fmt =  AV_PIX_FMT_PAL8;
197
-        break;
198
-    case 16:
199
-        if (image->red_mask   == 0xf800 &&
200
-            image->green_mask == 0x07e0 &&
201
-            image->blue_mask  == 0x001f) {
202
-            *pix_fmt = AV_PIX_FMT_RGB565;
203
-        } else if (image->red_mask   == 0x7c00 &&
204
-                   image->green_mask == 0x03e0 &&
205
-                   image->blue_mask  == 0x001f) {
206
-            *pix_fmt = AV_PIX_FMT_RGB555;
207
-        }
208
-        break;
209
-    case 24:
210
-        if (image->red_mask   == 0xff0000 &&
211
-            image->green_mask == 0x00ff00 &&
212
-            image->blue_mask  == 0x0000ff) {
213
-            *pix_fmt = AV_PIX_FMT_BGR24;
214
-        } else if (image->red_mask   == 0x0000ff &&
215
-                   image->green_mask == 0x00ff00 &&
216
-                   image->blue_mask  == 0xff0000) {
217
-            *pix_fmt = AV_PIX_FMT_RGB24;
218
-        }
219
-        break;
220
-    case 32:
221
-        *pix_fmt = AV_PIX_FMT_RGB32;
222
-        break;
223
-    default:
224
-        av_log(s, AV_LOG_ERROR,
225
-               "XImages with RGB mask 0x%.6lx 0x%.6lx 0x%.6lx and depth %i "
226
-               "are currently not supported.\n",
227
-               image->red_mask,
228
-               image->green_mask,
229
-               image->blue_mask,
230
-               image->bits_per_pixel);
231
-
232
-        return AVERROR_PATCHWELCOME;
233
-    }
234
-
235
-    return 0;
236
-}
237
-
238
-/**
239
- * Initialize the x11 grab device demuxer (public device demuxer API).
240
- *
241
- * @param s1 Context from avformat core
242
- * @return <ul>
243
- *          <li>AVERROR(ENOMEM) no memory left</li>
244
- *          <li>AVERROR(EIO) other failure case</li>
245
- *          <li>0 success</li>
246
- *         </ul>
247
- */
248
-static int x11grab_read_header(AVFormatContext *s1)
249
-{
250
-    X11GrabContext *x11grab = s1->priv_data;
251
-    Display *dpy;
252
-    AVStream *st = NULL;
253
-    XImage *image;
254
-    int x_off = 0, y_off = 0, ret = 0, screen, use_shm;
255
-    char *param, *offset;
256
-    AVRational framerate;
257
-
258
-    param = av_strdup(s1->filename);
259
-    if (!param)
260
-        goto out;
261
-
262
-    offset = strchr(param, '+');
263
-    if (offset) {
264
-        sscanf(offset, "%d,%d", &x_off, &y_off);
265
-        x11grab->draw_mouse = !strstr(offset, "nomouse");
266
-        *offset = 0;
267
-    }
268
-
269
-    ret = av_parse_video_size(&x11grab->width, &x11grab->height,
270
-                              x11grab->video_size);
271
-    if (ret < 0) {
272
-        av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
273
-        goto out;
274
-    }
275
-
276
-    ret = av_parse_video_rate(&framerate, x11grab->framerate);
277
-    if (ret < 0) {
278
-        av_log(s1, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
279
-               x11grab->framerate);
280
-        goto out;
281
-    }
282
-    av_log(s1, AV_LOG_INFO,
283
-           "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
284
-           s1->filename, param, x_off, y_off, x11grab->width, x11grab->height);
285
-
286
-    dpy = XOpenDisplay(param);
287
-    if (!dpy) {
288
-        av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
289
-        ret = AVERROR(EIO);
290
-        goto out;
291
-    }
292
-
293
-    st = avformat_new_stream(s1, NULL);
294
-    if (!st) {
295
-        ret = AVERROR(ENOMEM);
296
-        goto out;
297
-    }
298
-    avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
299
-
300
-    screen = DefaultScreen(dpy);
301
-
302
-    if (x11grab->follow_mouse) {
303
-        int screen_w, screen_h;
304
-        Window w;
305
-
306
-        screen_w = DisplayWidth(dpy, screen);
307
-        screen_h = DisplayHeight(dpy, screen);
308
-        XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
309
-                      &ret, &ret, &ret);
310
-        x_off -= x11grab->width / 2;
311
-        y_off -= x11grab->height / 2;
312
-        x_off  = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
313
-        y_off  = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
314
-        av_log(s1, AV_LOG_INFO,
315
-               "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
316
-               x_off, y_off);
317
-    }
318
-
319
-    use_shm = XShmQueryExtension(dpy);
320
-    av_log(s1, AV_LOG_INFO,
321
-           "shared memory extension %sfound\n", use_shm ? "" : "not ");
322
-
323
-    if (use_shm && setup_shm(s1, dpy, &image) < 0) {
324
-        av_log(s1, AV_LOG_WARNING, "Falling back to XGetImage\n");
325
-        use_shm = 0;
326
-    }
327
-
328
-    if (!use_shm) {
329
-        image = XGetImage(dpy, RootWindow(dpy, screen),
330
-                          x_off, y_off,
331
-                          x11grab->width, x11grab->height,
332
-                          AllPlanes, ZPixmap);
333
-    }
334
-
335
-    if (x11grab->draw_mouse && setup_mouse(dpy, screen) < 0) {
336
-        av_log(s1, AV_LOG_WARNING,
337
-               "XFixes not available, cannot draw the mouse cursor\n");
338
-        x11grab->draw_mouse = 0;
339
-    }
340
-
341
-    x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
342
-    x11grab->dpy        = dpy;
343
-    x11grab->time_base  = (AVRational) { framerate.den, framerate.num };
344
-    x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
345
-    x11grab->x_off      = x_off;
346
-    x11grab->y_off      = y_off;
347
-    x11grab->image      = image;
348
-    x11grab->use_shm    = use_shm;
349
-
350
-    ret = pixfmt_from_image(s1, image, &st->codecpar->format);
351
-    if (ret < 0)
352
-        goto out;
353
-
354
-    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
355
-    st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
356
-    st->codecpar->width      = x11grab->width;
357
-    st->codecpar->height     = x11grab->height;
358
-    st->codecpar->bit_rate   = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
359
-
360
-    st->avg_frame_rate       = av_inv_q(x11grab->time_base);
361
-
362
-out:
363
-    av_free(param);
364
-    return ret;
365
-}
366
-
367
-/**
368
- * Paint a mouse pointer in an X11 image.
369
- *
370
- * @param image image to paint the mouse pointer to
371
- * @param s context used to retrieve original grabbing rectangle
372
- *          coordinates
373
- */
374
-static void paint_mouse_pointer(XImage *image, X11GrabContext *s)
375
-{
376
-    int x_off    = s->x_off;
377
-    int y_off    = s->y_off;
378
-    int width    = s->width;
379
-    int height   = s->height;
380
-    Display *dpy = s->dpy;
381
-    XFixesCursorImage *xcim;
382
-    int x, y;
383
-    int line, column;
384
-    int to_line, to_column;
385
-    int pixstride = image->bits_per_pixel >> 3;
386
-    /* Warning: in its insanity, xlib provides unsigned image data through a
387
-     * char* pointer, so we have to make it uint8_t to make things not break.
388
-     * Anyone who performs further investigation of the xlib API likely risks
389
-     * permanent brain damage. */
390
-    uint8_t *pix = image->data;
391
-
392
-    /* Code doesn't currently support 16-bit or PAL8 */
393
-    if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
394
-        return;
395
-
396
-    xcim = XFixesGetCursorImage(dpy);
397
-    if (!xcim)
398
-        return;
399
-
400
-    x = xcim->x - xcim->xhot;
401
-    y = xcim->y - xcim->yhot;
402
-
403
-    to_line   = FFMIN((y + xcim->height), (height + y_off));
404
-    to_column = FFMIN((x + xcim->width),  (width  + x_off));
405
-
406
-    for (line = FFMAX(y, y_off); line < to_line; line++) {
407
-        for (column = FFMAX(x, x_off); column < to_column; column++) {
408
-            int xcim_addr  = (line  - y)     * xcim->width + column - x;
409
-            int image_addr = ((line - y_off) * width       + column - x_off) * pixstride;
410
-            int r          = (uint8_t)(xcim->pixels[xcim_addr] >>  0);
411
-            int g          = (uint8_t)(xcim->pixels[xcim_addr] >>  8);
412
-            int b          = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
413
-            int a          = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
414
-
415
-            if (a == 255) {
416
-                pix[image_addr + 0] = r;
417
-                pix[image_addr + 1] = g;
418
-                pix[image_addr + 2] = b;
419
-            } else if (a) {
420
-                /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
421
-                pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) + 255 / 2) / 255;
422
-                pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) + 255 / 2) / 255;
423
-                pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) + 255 / 2) / 255;
424
-            }
425
-        }
426
-    }
427
-
428
-    XFree(xcim);
429
-    xcim = NULL;
430
-}
431
-
432
-/**
433
- * Read new data in the image structure.
434
- *
435
- * @param dpy X11 display to grab from
436
- * @param d
437
- * @param image Image where the grab will be put
438
- * @param x Top-Left grabbing rectangle horizontal coordinate
439
- * @param y Top-Left grabbing rectangle vertical coordinate
440
- * @return 0 if error, !0 if successful
441
- */
442
-static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
443
-{
444
-    xGetImageReply rep;
445
-    xGetImageReq *req;
446
-    long nbytes;
447
-
448
-    if (!image)
449
-        return 0;
450
-
451
-    LockDisplay(dpy);
452
-    GetReq(GetImage, req);
453
-
454
-    /* First set up the standard stuff in the request */
455
-    req->drawable  = d;
456
-    req->x         = x;
457
-    req->y         = y;
458
-    req->width     = image->width;
459
-    req->height    = image->height;
460
-    req->planeMask = (unsigned int)AllPlanes;
461
-    req->format    = ZPixmap;
462
-
463
-    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
464
-        UnlockDisplay(dpy);
465
-        SyncHandle();
466
-        return 0;
467
-    }
468
-
469
-    nbytes = (long)rep.length << 2;
470
-    _XReadPad(dpy, image->data, nbytes);
471
-
472
-    UnlockDisplay(dpy);
473
-    SyncHandle();
474
-    return 1;
475
-}
476
-
477
-/**
478
- * Grab a frame from x11 (public device demuxer API).
479
- *
480
- * @param s1 Context from avformat core
481
- * @param pkt Packet holding the brabbed frame
482
- * @return frame size in bytes
483
- */
484
-static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
485
-{
486
-    X11GrabContext *s = s1->priv_data;
487
-    Display *dpy      = s->dpy;
488
-    XImage *image     = s->image;
489
-    int x_off         = s->x_off;
490
-    int y_off         = s->y_off;
491
-    int follow_mouse  = s->follow_mouse;
492
-    int screen, pointer_x, pointer_y, _, same_screen = 1;
493
-    Window w, root;
494
-    int64_t curtime, delay;
495
-    struct timespec ts;
496
-
497
-    /* Calculate the time of the next frame */
498
-    s->time_frame += INT64_C(1000000);
499
-
500
-    /* wait based on the frame rate */
501
-    for (;;) {
502
-        curtime = av_gettime();
503
-        delay   = s->time_frame * av_q2d(s->time_base) - curtime;
504
-        if (delay <= 0) {
505
-            if (delay < INT64_C(-1000000) * av_q2d(s->time_base))
506
-                s->time_frame += INT64_C(1000000);
507
-            break;
508
-        }
509
-        ts.tv_sec  = delay / 1000000;
510
-        ts.tv_nsec = (delay % 1000000) * 1000;
511
-        nanosleep(&ts, NULL);
512
-    }
513
-
514
-    pkt->data = image->data;
515
-    pkt->size = s->frame_size;
516
-    pkt->pts  = curtime;
517
-
518
-    screen = DefaultScreen(dpy);
519
-    root   = RootWindow(dpy, screen);
520
-
521
-    if (follow_mouse || s->draw_mouse)
522
-        same_screen = XQueryPointer(dpy, root, &w, &w,
523
-                                    &pointer_x, &pointer_y, &_, &_, &_);
524
-
525
-    if (follow_mouse && same_screen) {
526
-        int screen_w, screen_h;
527
-
528
-        screen_w = DisplayWidth(dpy, screen);
529
-        screen_h = DisplayHeight(dpy, screen);
530
-        if (follow_mouse == -1) {
531
-            // follow the mouse, put it at center of grabbing region
532
-            x_off += pointer_x - s->width / 2 - x_off;
533
-            y_off += pointer_y - s->height / 2 - y_off;
534
-        } else {
535
-            // follow the mouse, but only move the grabbing region when mouse
536
-            // reaches within certain pixels to the edge.
537
-            if (pointer_x > x_off + s->width - follow_mouse)
538
-                x_off += pointer_x - (x_off + s->width - follow_mouse);
539
-            else if (pointer_x < x_off + follow_mouse)
540
-                x_off -= (x_off + follow_mouse) - pointer_x;
541
-            if (pointer_y > y_off + s->height - follow_mouse)
542
-                y_off += pointer_y - (y_off + s->height - follow_mouse);
543
-            else if (pointer_y < y_off + follow_mouse)
544
-                y_off -= (y_off + follow_mouse) - pointer_y;
545
-        }
546
-        // adjust grabbing region position if it goes out of screen.
547
-        s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
548
-        s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
549
-
550
-        if (s->show_region && s->region_win)
551
-            XMoveWindow(dpy, s->region_win,
552
-                        s->x_off - REGION_WIN_BORDER,
553
-                        s->y_off - REGION_WIN_BORDER);
554
-    }
555
-
556
-    if (s->show_region && same_screen) {
557
-        if (s->region_win) {
558
-            XEvent evt = { .type = NoEventMask };
559
-            // Clean up the events, and do the initial draw or redraw.
560
-            while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
561
-                                   &evt))
562
-                ;
563
-            if (evt.type)
564
-                x11grab_draw_region_win(s);
565
-        } else {
566
-            x11grab_region_win_init(s);
567
-        }
568
-    }
569
-
570
-    if (s->use_shm) {
571
-        if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
572
-            av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
573
-    } else {
574
-        if (!xget_zpixmap(dpy, root, image, x_off, y_off))
575
-            av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
576
-    }
577
-
578
-    if (s->draw_mouse && same_screen)
579
-        paint_mouse_pointer(image, s);
580
-
581
-    return s->frame_size;
582
-}
583
-
584
-/**
585
- * Close x11 frame grabber (public device demuxer API).
586
- *
587
- * @param s1 Context from avformat core
588
- * @return 0 success, !0 failure
589
- */
590
-static int x11grab_read_close(AVFormatContext *s1)
591
-{
592
-    X11GrabContext *x11grab = s1->priv_data;
593
-
594
-    /* Detach cleanly from shared mem */
595
-    if (x11grab->use_shm) {
596
-        XShmDetach(x11grab->dpy, &x11grab->shminfo);
597
-        shmdt(x11grab->shminfo.shmaddr);
598
-        shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
599
-    }
600
-
601
-    /* Destroy X11 image */
602
-    if (x11grab->image) {
603
-        XDestroyImage(x11grab->image);
604
-        x11grab->image = NULL;
605
-    }
606
-
607
-    if (x11grab->region_win)
608
-        XDestroyWindow(x11grab->dpy, x11grab->region_win);
609
-
610
-    /* Free X11 display */
611
-    XCloseDisplay(x11grab->dpy);
612
-    return 0;
613
-}
614
-
615
-#define OFFSET(x) offsetof(X11GrabContext, x)
616
-#define DEC AV_OPT_FLAG_DECODING_PARAM
617
-static const AVOption options[] = {
618
-    { "grab_x", "Initial x coordinate.", OFFSET(x_off), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
619
-    { "grab_y", "Initial y coordinate.", OFFSET(y_off), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
620
-    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC },
621
-    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
622
-    { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, DEC },
623
-    { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
624
-      OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, INT_MAX, DEC, "follow_mouse" },
625
-    { "centered", "Keep the mouse pointer at the center of grabbing region when following.", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, DEC, "follow_mouse" },
626
-    { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
627
-    { NULL },
628
-};
629
-
630
-static const AVClass x11_class = {
631
-    .class_name = "X11grab indev",
632
-    .item_name  = av_default_item_name,
633
-    .option     = options,
634
-    .version    = LIBAVUTIL_VERSION_INT,
635
-};
636
-
637
-/** x11 grabber device demuxer declaration */
638
-AVInputFormat ff_x11grab_demuxer = {
639
-    .name           = "x11grab",
640
-    .long_name      = NULL_IF_CONFIG_SMALL("X11grab"),
641
-    .priv_data_size = sizeof(X11GrabContext),
642
-    .read_header    = x11grab_read_header,
643
-    .read_packet    = x11grab_read_packet,
644
-    .read_close     = x11grab_read_close,
645
-    .flags          = AVFMT_NOFILE,
646
-    .priv_class     = &x11_class,
647
-};