LCOV - code coverage report
Current view: top level - libavdevice - xcbgrab.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 328 0.0 %
Date: 2017-10-24 00:14:43 Functions: 0 17 0.0 %

          Line data    Source code
       1             : /*
       2             :  * XCB input grabber
       3             :  * Copyright (C) 2014 Luca Barbato <lu_zero@gentoo.org>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "config.h"
      23             : 
      24             : #include <stdlib.h>
      25             : #include <xcb/xcb.h>
      26             : 
      27             : #if CONFIG_LIBXCB_XFIXES
      28             : #include <xcb/xfixes.h>
      29             : #endif
      30             : 
      31             : #if CONFIG_LIBXCB_SHM
      32             : #include <sys/shm.h>
      33             : #include <xcb/shm.h>
      34             : #endif
      35             : 
      36             : #if CONFIG_LIBXCB_SHAPE
      37             : #include <xcb/shape.h>
      38             : #endif
      39             : 
      40             : #include "libavutil/internal.h"
      41             : #include "libavutil/mathematics.h"
      42             : #include "libavutil/opt.h"
      43             : #include "libavutil/parseutils.h"
      44             : #include "libavutil/time.h"
      45             : 
      46             : #include "libavformat/avformat.h"
      47             : #include "libavformat/internal.h"
      48             : 
      49             : typedef struct XCBGrabContext {
      50             :     const AVClass *class;
      51             : 
      52             :     uint8_t *buffer;
      53             : 
      54             :     xcb_connection_t *conn;
      55             :     xcb_screen_t *screen;
      56             :     xcb_window_t window;
      57             : #if CONFIG_LIBXCB_SHM
      58             :     xcb_shm_seg_t segment;
      59             : #endif
      60             :     int64_t time_frame;
      61             :     AVRational time_base;
      62             : 
      63             :     int x, y;
      64             :     int width, height;
      65             :     int frame_size;
      66             :     int bpp;
      67             : 
      68             :     int draw_mouse;
      69             :     int follow_mouse;
      70             :     int show_region;
      71             :     int region_border;
      72             :     int centered;
      73             : 
      74             :     const char *video_size;
      75             :     const char *framerate;
      76             : 
      77             :     int has_shm;
      78             : } XCBGrabContext;
      79             : 
      80             : #define FOLLOW_CENTER -1
      81             : 
      82             : #define OFFSET(x) offsetof(XCBGrabContext, x)
      83             : #define D AV_OPT_FLAG_DECODING_PARAM
      84             : static const AVOption options[] = {
      85             :     { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
      86             :     { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
      87             :     { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
      88             :     { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
      89             :     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
      90             :     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
      91             :     { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
      92             :     { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
      93             :       OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 },  FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
      94             :     { "centered", "Keep the mouse pointer at the center of grabbing region when following.", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, D, "follow_mouse" },
      95             :     { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
      96             :     { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
      97             :     { NULL },
      98             : };
      99             : 
     100             : static const AVClass xcbgrab_class = {
     101             :     .class_name = "xcbgrab indev",
     102             :     .item_name  = av_default_item_name,
     103             :     .option     = options,
     104             :     .version    = LIBAVUTIL_VERSION_INT,
     105             :     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
     106             : };
     107             : 
     108           0 : static int xcbgrab_reposition(AVFormatContext *s,
     109             :                               xcb_query_pointer_reply_t *p,
     110             :                               xcb_get_geometry_reply_t *geo)
     111             : {
     112           0 :     XCBGrabContext *c = s->priv_data;
     113           0 :     int x = c->x, y = c->y;
     114           0 :     int w = c->width, h = c->height, f = c->follow_mouse;
     115             :     int p_x, p_y;
     116             : 
     117           0 :     if (!p || !geo)
     118           0 :         return AVERROR(EIO);
     119             : 
     120           0 :     p_x = p->win_x;
     121           0 :     p_y = p->win_y;
     122             : 
     123           0 :     if (f == FOLLOW_CENTER) {
     124           0 :         x = p_x - w / 2;
     125           0 :         y = p_y - h / 2;
     126             :     } else {
     127           0 :         int left   = x + f;
     128           0 :         int right  = x + w - f;
     129           0 :         int top    = y + f;
     130           0 :         int bottom = y + h + f;
     131           0 :         if (p_x > right) {
     132           0 :             x += p_x - right;
     133           0 :         } else if (p_x < left) {
     134           0 :             x -= left - p_x;
     135             :         }
     136           0 :         if (p_y > bottom) {
     137           0 :             y += p_y - bottom;
     138           0 :         } else if (p_y < top) {
     139           0 :             y -= top - p_y;
     140             :         }
     141             :     }
     142             : 
     143           0 :     c->x = FFMIN(FFMAX(0, x), geo->width  - w);
     144           0 :     c->y = FFMIN(FFMAX(0, y), geo->height - h);
     145             : 
     146           0 :     return 0;
     147             : }
     148             : 
     149           0 : static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
     150             : {
     151           0 :     XCBGrabContext *c = s->priv_data;
     152             :     xcb_get_image_cookie_t iq;
     153             :     xcb_get_image_reply_t *img;
     154           0 :     xcb_drawable_t drawable = c->screen->root;
     155           0 :     xcb_generic_error_t *e = NULL;
     156             :     uint8_t *data;
     157             :     int length, ret;
     158             : 
     159           0 :     iq  = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
     160           0 :                         c->x, c->y, c->width, c->height, ~0);
     161             : 
     162           0 :     img = xcb_get_image_reply(c->conn, iq, &e);
     163             : 
     164           0 :     if (e) {
     165           0 :         av_log(s, AV_LOG_ERROR,
     166             :                "Cannot get the image data "
     167             :                "event_error: response_type:%u error_code:%u "
     168             :                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
     169           0 :                e->response_type, e->error_code,
     170           0 :                e->sequence, e->resource_id, e->minor_code, e->major_code);
     171           0 :         return AVERROR(EACCES);
     172             :     }
     173             : 
     174           0 :     if (!img)
     175           0 :         return AVERROR(EAGAIN);
     176             : 
     177           0 :     data   = xcb_get_image_data(img);
     178           0 :     length = xcb_get_image_data_length(img);
     179             : 
     180           0 :     ret = av_new_packet(pkt, length);
     181             : 
     182           0 :     if (!ret)
     183           0 :         memcpy(pkt->data, data, length);
     184             : 
     185           0 :     free(img);
     186             : 
     187           0 :     return ret;
     188             : }
     189             : 
     190           0 : static void wait_frame(AVFormatContext *s, AVPacket *pkt)
     191             : {
     192           0 :     XCBGrabContext *c = s->priv_data;
     193             :     int64_t curtime, delay;
     194           0 :     int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
     195             : 
     196           0 :     c->time_frame += frame_time;
     197             : 
     198             :     for (;;) {
     199           0 :         curtime = av_gettime();
     200           0 :         delay   = c->time_frame - curtime;
     201           0 :         if (delay <= 0)
     202           0 :             break;
     203           0 :         av_usleep(delay);
     204             :     }
     205             : 
     206           0 :     pkt->pts = curtime;
     207           0 : }
     208             : 
     209             : #if CONFIG_LIBXCB_SHM
     210           0 : static int check_shm(xcb_connection_t *conn)
     211             : {
     212           0 :     xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
     213             :     xcb_shm_query_version_reply_t *reply;
     214             : 
     215           0 :     reply = xcb_shm_query_version_reply(conn, cookie, NULL);
     216           0 :     if (reply) {
     217           0 :         free(reply);
     218           0 :         return 1;
     219             :     }
     220             : 
     221           0 :     return 0;
     222             : }
     223             : 
     224           0 : static int allocate_shm(AVFormatContext *s)
     225             : {
     226           0 :     XCBGrabContext *c = s->priv_data;
     227           0 :     int size = c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE;
     228             :     uint8_t *data;
     229             :     int id;
     230             : 
     231           0 :     if (c->buffer)
     232           0 :         return 0;
     233           0 :     id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
     234           0 :     if (id == -1) {
     235             :         char errbuf[1024];
     236           0 :         int err = AVERROR(errno);
     237           0 :         av_strerror(err, errbuf, sizeof(errbuf));
     238           0 :         av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
     239             :                size, errbuf);
     240           0 :         return err;
     241             :     }
     242           0 :     xcb_shm_attach(c->conn, c->segment, id, 0);
     243           0 :     data = shmat(id, NULL, 0);
     244           0 :     shmctl(id, IPC_RMID, 0);
     245           0 :     if ((intptr_t)data == -1 || !data)
     246           0 :         return AVERROR(errno);
     247           0 :     c->buffer = data;
     248           0 :     return 0;
     249             : }
     250             : 
     251           0 : static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
     252             : {
     253           0 :     XCBGrabContext *c = s->priv_data;
     254             :     xcb_shm_get_image_cookie_t iq;
     255             :     xcb_shm_get_image_reply_t *img;
     256           0 :     xcb_drawable_t drawable = c->screen->root;
     257           0 :     xcb_generic_error_t *e = NULL;
     258             :     int ret;
     259             : 
     260           0 :     ret = allocate_shm(s);
     261           0 :     if (ret < 0)
     262           0 :         return ret;
     263             : 
     264           0 :     iq = xcb_shm_get_image(c->conn, drawable,
     265           0 :                            c->x, c->y, c->width, c->height, ~0,
     266             :                            XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
     267           0 :     img = xcb_shm_get_image_reply(c->conn, iq, &e);
     268             : 
     269           0 :     xcb_flush(c->conn);
     270             : 
     271           0 :     if (e) {
     272           0 :         av_log(s, AV_LOG_ERROR,
     273             :                "Cannot get the image data "
     274             :                "event_error: response_type:%u error_code:%u "
     275             :                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
     276           0 :                e->response_type, e->error_code,
     277           0 :                e->sequence, e->resource_id, e->minor_code, e->major_code);
     278             : 
     279           0 :         return AVERROR(EACCES);
     280             :     }
     281             : 
     282           0 :     free(img);
     283             : 
     284           0 :     pkt->data = c->buffer;
     285           0 :     pkt->size = c->frame_size;
     286             : 
     287           0 :     return 0;
     288             : }
     289             : #endif /* CONFIG_LIBXCB_SHM */
     290             : 
     291             : #if CONFIG_LIBXCB_XFIXES
     292           0 : static int check_xfixes(xcb_connection_t *conn)
     293             : {
     294             :     xcb_xfixes_query_version_cookie_t cookie;
     295             :     xcb_xfixes_query_version_reply_t *reply;
     296             : 
     297           0 :     cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
     298             :                                       XCB_XFIXES_MINOR_VERSION);
     299           0 :     reply  = xcb_xfixes_query_version_reply(conn, cookie, NULL);
     300             : 
     301           0 :     if (reply) {
     302           0 :         free(reply);
     303           0 :         return 1;
     304             :     }
     305           0 :     return 0;
     306             : }
     307             : 
     308             : #define BLEND(target, source, alpha) \
     309             :     (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
     310             : 
     311           0 : static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
     312             :                                xcb_query_pointer_reply_t *p,
     313             :                                xcb_get_geometry_reply_t *geo)
     314             : {
     315           0 :     XCBGrabContext *gr = s->priv_data;
     316             :     uint32_t *cursor;
     317           0 :     uint8_t *image = pkt->data;
     318           0 :     int stride     = gr->bpp / 8;
     319             :     xcb_xfixes_get_cursor_image_cookie_t cc;
     320             :     xcb_xfixes_get_cursor_image_reply_t *ci;
     321             :     int cx, cy, x, y, w, h, c_off, i_off;
     322             : 
     323           0 :     cc = xcb_xfixes_get_cursor_image(gr->conn);
     324           0 :     ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
     325           0 :     if (!ci)
     326           0 :         return;
     327             : 
     328           0 :     cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
     329           0 :     if (!cursor)
     330           0 :         return;
     331             : 
     332           0 :     cx = ci->x - ci->xhot;
     333           0 :     cy = ci->y - ci->yhot;
     334             : 
     335           0 :     x = FFMAX(cx, gr->x);
     336           0 :     y = FFMAX(cy, gr->y);
     337             : 
     338           0 :     w = FFMIN(cx + ci->width,  gr->x + gr->width)  - x;
     339           0 :     h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
     340             : 
     341           0 :     c_off = x - cx;
     342           0 :     i_off = x - gr->x;
     343             : 
     344           0 :     cursor += (y - cy) * ci->width;
     345           0 :     image  += (y - gr->y) * gr->width * stride;
     346             : 
     347           0 :     for (y = 0; y < h; y++) {
     348           0 :         cursor += c_off;
     349           0 :         image  += i_off * stride;
     350           0 :         for (x = 0; x < w; x++, cursor++, image += stride) {
     351             :             int r, g, b, a;
     352             : 
     353           0 :             r =  *cursor        & 0xff;
     354           0 :             g = (*cursor >>  8) & 0xff;
     355           0 :             b = (*cursor >> 16) & 0xff;
     356           0 :             a = (*cursor >> 24) & 0xff;
     357             : 
     358           0 :             if (!a)
     359           0 :                 continue;
     360             : 
     361           0 :             if (a == 255) {
     362           0 :                 image[0] = r;
     363           0 :                 image[1] = g;
     364           0 :                 image[2] = b;
     365             :             } else {
     366           0 :                 image[0] = BLEND(r, image[0], a);
     367           0 :                 image[1] = BLEND(g, image[1], a);
     368           0 :                 image[2] = BLEND(b, image[2], a);
     369             :             }
     370             : 
     371             :         }
     372           0 :         cursor +=  ci->width - w - c_off;
     373           0 :         image  += (gr->width - w - i_off) * stride;
     374             :     }
     375             : 
     376           0 :     free(ci);
     377             : }
     378             : #endif /* CONFIG_LIBXCB_XFIXES */
     379             : 
     380           0 : static void xcbgrab_update_region(AVFormatContext *s)
     381             : {
     382           0 :     XCBGrabContext *c     = s->priv_data;
     383           0 :     const uint32_t args[] = { c->x - c->region_border,
     384           0 :                               c->y - c->region_border };
     385             : 
     386           0 :     xcb_configure_window(c->conn,
     387             :                          c->window,
     388             :                          XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
     389             :                          args);
     390           0 : }
     391             : 
     392           0 : static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
     393             : {
     394           0 :     XCBGrabContext *c = s->priv_data;
     395             :     xcb_query_pointer_cookie_t pc;
     396             :     xcb_get_geometry_cookie_t gc;
     397           0 :     xcb_query_pointer_reply_t *p  = NULL;
     398           0 :     xcb_get_geometry_reply_t *geo = NULL;
     399           0 :     int ret = 0;
     400             : 
     401           0 :     wait_frame(s, pkt);
     402             : 
     403           0 :     if (c->follow_mouse || c->draw_mouse) {
     404           0 :         pc  = xcb_query_pointer(c->conn, c->screen->root);
     405           0 :         gc  = xcb_get_geometry(c->conn, c->screen->root);
     406           0 :         p   = xcb_query_pointer_reply(c->conn, pc, NULL);
     407           0 :         geo = xcb_get_geometry_reply(c->conn, gc, NULL);
     408             :     }
     409             : 
     410           0 :     if (c->follow_mouse && p->same_screen)
     411           0 :         xcbgrab_reposition(s, p, geo);
     412             : 
     413           0 :     if (c->show_region)
     414           0 :         xcbgrab_update_region(s);
     415             : 
     416             : #if CONFIG_LIBXCB_SHM
     417           0 :     if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
     418           0 :         c->has_shm = 0;
     419             : #endif
     420           0 :     if (!c->has_shm)
     421           0 :         ret = xcbgrab_frame(s, pkt);
     422             : 
     423             : #if CONFIG_LIBXCB_XFIXES
     424           0 :     if (ret >= 0 && c->draw_mouse && p->same_screen)
     425           0 :         xcbgrab_draw_mouse(s, pkt, p, geo);
     426             : #endif
     427             : 
     428           0 :     free(p);
     429           0 :     free(geo);
     430             : 
     431           0 :     return ret;
     432             : }
     433             : 
     434           0 : static av_cold int xcbgrab_read_close(AVFormatContext *s)
     435             : {
     436           0 :     XCBGrabContext *ctx = s->priv_data;
     437             : 
     438             : #if CONFIG_LIBXCB_SHM
     439           0 :     if (ctx->buffer) {
     440           0 :         shmdt(ctx->buffer);
     441             :     }
     442             : #endif
     443             : 
     444           0 :     xcb_disconnect(ctx->conn);
     445             : 
     446           0 :     return 0;
     447             : }
     448             : 
     449           0 : static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
     450             : {
     451           0 :     xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
     452           0 :     xcb_screen_t *screen     = NULL;
     453             : 
     454           0 :     for (; it.rem > 0; xcb_screen_next (&it)) {
     455           0 :         if (!screen_num) {
     456           0 :             screen = it.data;
     457           0 :             break;
     458             :         }
     459             : 
     460           0 :         screen_num--;
     461             :     }
     462             : 
     463           0 :     return screen;
     464             : }
     465             : 
     466           0 : static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
     467             :                                      int *pix_fmt)
     468             : {
     469           0 :     XCBGrabContext *c        = s->priv_data;
     470           0 :     const xcb_setup_t *setup = xcb_get_setup(c->conn);
     471           0 :     const xcb_format_t *fmt  = xcb_setup_pixmap_formats(setup);
     472           0 :     int length               = xcb_setup_pixmap_formats_length(setup);
     473             : 
     474           0 :     *pix_fmt = 0;
     475             : 
     476           0 :     while (length--) {
     477           0 :         if (fmt->depth == depth) {
     478           0 :             switch (depth) {
     479           0 :             case 32:
     480           0 :                 if (fmt->bits_per_pixel == 32)
     481           0 :                     *pix_fmt = AV_PIX_FMT_0RGB;
     482           0 :                 break;
     483           0 :             case 24:
     484           0 :                 if (fmt->bits_per_pixel == 32)
     485           0 :                     *pix_fmt = AV_PIX_FMT_0RGB32;
     486           0 :                 else if (fmt->bits_per_pixel == 24)
     487           0 :                     *pix_fmt = AV_PIX_FMT_RGB24;
     488           0 :                 break;
     489           0 :             case 16:
     490           0 :                 if (fmt->bits_per_pixel == 16)
     491           0 :                     *pix_fmt = AV_PIX_FMT_RGB565;
     492           0 :                 break;
     493           0 :             case 15:
     494           0 :                 if (fmt->bits_per_pixel == 16)
     495           0 :                     *pix_fmt = AV_PIX_FMT_RGB555;
     496           0 :                 break;
     497           0 :             case 8:
     498           0 :                 if (fmt->bits_per_pixel == 8)
     499           0 :                     *pix_fmt = AV_PIX_FMT_RGB8;
     500           0 :                 break;
     501             :             }
     502             :         }
     503             : 
     504           0 :         if (*pix_fmt) {
     505           0 :             c->bpp        = fmt->bits_per_pixel;
     506           0 :             c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
     507           0 :             return 0;
     508             :         }
     509             : 
     510           0 :         fmt++;
     511             :     }
     512           0 :     avpriv_report_missing_feature(s, "Mapping this pixmap format");
     513             : 
     514           0 :     return AVERROR_PATCHWELCOME;
     515             : }
     516             : 
     517           0 : static int create_stream(AVFormatContext *s)
     518             : {
     519           0 :     XCBGrabContext *c = s->priv_data;
     520           0 :     AVStream *st      = avformat_new_stream(s, NULL);
     521             :     xcb_get_geometry_cookie_t gc;
     522             :     xcb_get_geometry_reply_t *geo;
     523             :     int ret;
     524             : 
     525           0 :     if (!st)
     526           0 :         return AVERROR(ENOMEM);
     527             : 
     528           0 :     ret = av_parse_video_size(&c->width, &c->height, c->video_size);
     529           0 :     if (ret < 0)
     530           0 :         return ret;
     531             : 
     532           0 :     ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
     533           0 :     if (ret < 0)
     534           0 :         return ret;
     535             : 
     536           0 :     avpriv_set_pts_info(st, 64, 1, 1000000);
     537             : 
     538           0 :     gc  = xcb_get_geometry(c->conn, c->screen->root);
     539           0 :     geo = xcb_get_geometry_reply(c->conn, gc, NULL);
     540             : 
     541           0 :     if (c->x + c->width > geo->width ||
     542           0 :         c->y + c->height > geo->height) {
     543           0 :         av_log(s, AV_LOG_ERROR,
     544             :                "Capture area %dx%d at position %d.%d "
     545             :                "outside the screen size %dx%d\n",
     546             :                c->width, c->height,
     547             :                c->x, c->y,
     548           0 :                geo->width, geo->height);
     549           0 :         return AVERROR(EINVAL);
     550             :     }
     551             : 
     552           0 :     c->time_base  = (AVRational){ st->avg_frame_rate.den,
     553           0 :                                   st->avg_frame_rate.num };
     554           0 :     c->time_frame = av_gettime();
     555             : 
     556           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     557           0 :     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
     558           0 :     st->codecpar->width      = c->width;
     559           0 :     st->codecpar->height     = c->height;
     560             : 
     561           0 :     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format);
     562             : 
     563           0 :     free(geo);
     564             : 
     565           0 :     return ret;
     566             : }
     567             : 
     568           0 : static void draw_rectangle(AVFormatContext *s)
     569             : {
     570           0 :     XCBGrabContext *c = s->priv_data;
     571           0 :     xcb_gcontext_t gc = xcb_generate_id(c->conn);
     572           0 :     uint32_t mask     = XCB_GC_FOREGROUND |
     573             :                         XCB_GC_BACKGROUND |
     574             :                         XCB_GC_LINE_WIDTH |
     575             :                         XCB_GC_LINE_STYLE |
     576             :                         XCB_GC_FILL_STYLE;
     577           0 :     uint32_t values[] = { c->screen->black_pixel,
     578           0 :                           c->screen->white_pixel,
     579           0 :                           c->region_border,
     580             :                           XCB_LINE_STYLE_DOUBLE_DASH,
     581             :                           XCB_FILL_STYLE_SOLID };
     582           0 :     xcb_rectangle_t r = { 1, 1,
     583           0 :                           c->width  + c->region_border * 2 - 3,
     584           0 :                           c->height + c->region_border * 2 - 3 };
     585             : 
     586           0 :     xcb_create_gc(c->conn, gc, c->window, mask, values);
     587             : 
     588           0 :     xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
     589           0 : }
     590             : 
     591           0 : static void setup_window(AVFormatContext *s)
     592             : {
     593           0 :     XCBGrabContext *c = s->priv_data;
     594           0 :     uint32_t mask     = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
     595           0 :     uint32_t values[] = { 1,
     596             :                           XCB_EVENT_MASK_EXPOSURE |
     597             :                           XCB_EVENT_MASK_STRUCTURE_NOTIFY };
     598           0 :     av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
     599             : 
     600           0 :     c->window = xcb_generate_id(c->conn);
     601             : 
     602           0 :     xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
     603             :                       c->window,
     604           0 :                       c->screen->root,
     605           0 :                       c->x - c->region_border,
     606           0 :                       c->y - c->region_border,
     607           0 :                       c->width + c->region_border * 2,
     608           0 :                       c->height + c->region_border * 2,
     609             :                       0,
     610             :                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
     611             :                       XCB_COPY_FROM_PARENT,
     612             :                       mask, values);
     613             : 
     614             : #if CONFIG_LIBXCB_SHAPE
     615           0 :     xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
     616             :                          XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
     617             :                          c->window,
     618           0 :                          c->region_border, c->region_border,
     619             :                          1, &rect);
     620             : #endif
     621             : 
     622           0 :     xcb_map_window(c->conn, c->window);
     623             : 
     624           0 :     draw_rectangle(s);
     625           0 : }
     626             : 
     627           0 : static av_cold int xcbgrab_read_header(AVFormatContext *s)
     628             : {
     629           0 :     XCBGrabContext *c = s->priv_data;
     630             :     int screen_num, ret;
     631             :     const xcb_setup_t *setup;
     632           0 :     char *display_name = av_strdup(s->filename);
     633             : 
     634           0 :     if (!display_name)
     635           0 :         return AVERROR(ENOMEM);
     636             : 
     637           0 :     if (!sscanf(s->filename, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
     638           0 :         *display_name = 0;
     639           0 :         sscanf(s->filename, "+%d,%d", &c->x, &c->y);
     640             :     }
     641             : 
     642           0 :     c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
     643           0 :     av_freep(&display_name);
     644             : 
     645           0 :     if ((ret = xcb_connection_has_error(c->conn))) {
     646           0 :         av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
     647           0 :                s->filename[0] ? s->filename : "default", ret);
     648           0 :         return AVERROR(EIO);
     649             :     }
     650             : 
     651           0 :     setup = xcb_get_setup(c->conn);
     652             : 
     653           0 :     c->screen = get_screen(setup, screen_num);
     654           0 :     if (!c->screen) {
     655           0 :         av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
     656             :                screen_num);
     657           0 :         xcbgrab_read_close(s);
     658           0 :         return AVERROR(EIO);
     659             :     }
     660             : 
     661           0 :     ret = create_stream(s);
     662             : 
     663           0 :     if (ret < 0) {
     664           0 :         xcbgrab_read_close(s);
     665           0 :         return ret;
     666             :     }
     667             : 
     668             : #if CONFIG_LIBXCB_SHM
     669           0 :     if ((c->has_shm = check_shm(c->conn)))
     670           0 :         c->segment = xcb_generate_id(c->conn);
     671             : #endif
     672             : 
     673             : #if CONFIG_LIBXCB_XFIXES
     674           0 :     if (c->draw_mouse) {
     675           0 :         if (!(c->draw_mouse = check_xfixes(c->conn))) {
     676           0 :             av_log(s, AV_LOG_WARNING,
     677             :                    "XFixes not available, cannot draw the mouse.\n");
     678             :         }
     679           0 :         if (c->bpp < 24) {
     680           0 :             avpriv_report_missing_feature(s, "%d bits per pixel screen",
     681             :                                           c->bpp);
     682           0 :             c->draw_mouse = 0;
     683             :         }
     684             :     }
     685             : #endif
     686             : 
     687           0 :     if (c->show_region)
     688           0 :         setup_window(s);
     689             : 
     690           0 :     return 0;
     691             : }
     692             : 
     693             : AVInputFormat ff_xcbgrab_demuxer = {
     694             :     .name           = "x11grab",
     695             :     .long_name      = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
     696             :     .priv_data_size = sizeof(XCBGrabContext),
     697             :     .read_header    = xcbgrab_read_header,
     698             :     .read_packet    = xcbgrab_read_packet,
     699             :     .read_close     = xcbgrab_read_close,
     700             :     .flags          = AVFMT_NOFILE,
     701             :     .priv_class     = &xcbgrab_class,
     702             : };

Generated by: LCOV version 1.13