xcb.c 16.4 KB
Newer Older
1 2 3 4 5 6 7
/**
 * @file xcb.c
 * @brief X11 C Bindings screen capture demux module for VLC media player
 */
/*****************************************************************************
 * Copyright © 2009 Rémi Denis-Courmont
 *
8 9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18 19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
22 23 24 25 26

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdarg.h>
27
#include <assert.h>
28
#include <errno.h>
29
#include <xcb/xcb.h>
30
#include <xcb/composite.h>
31 32 33 34
#ifdef HAVE_SYS_SHM_H
# include <sys/shm.h>
# include <xcb/shm.h>
#endif
35 36 37 38 39 40 41 42 43 44
#include <vlc_common.h>
#include <vlc_demux.h>
#include <vlc_plugin.h>

#define FPS_TEXT N_("Frame rate")
#define FPS_LONGTEXT N_( \
    "How many times the screen content should be refreshed per second.")

#define LEFT_TEXT N_("Region left column")
#define LEFT_LONGTEXT N_( \
Tobias Güntner's avatar
Tobias Güntner committed
45
    "Abscissa of the capture region in pixels.")
46 47 48 49 50 51 52 53 54 55 56 57 58

#define TOP_TEXT N_("Region top row")
#define TOP_LONGTEXT N_( \
    "Ordinate of the capture region in pixels.")

#define WIDTH_TEXT N_("Capture region width")
#define WIDTH_LONGTEXT N_( \
    "Pixel width of the capture region, or 0 for full width")

#define HEIGHT_TEXT N_("Capture region height")
#define HEIGHT_LONGTEXT N_( \
    "Pixel height of the capture region, or 0 for full height")

59 60 61 62
#define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
#define FOLLOW_MOUSE_LONGTEXT N_( \
    "Follow the mouse when capturing a subscreen." )

63 64 65 66 67 68 69 70 71 72 73 74 75 76
static int  Open (vlc_object_t *);
static void Close (vlc_object_t *);

/*
 * Module descriptor
 */
vlc_module_begin ()
    set_shortname (N_("Screen"))
    set_description (N_("Screen capture (with X11/XCB)"))
    set_category (CAT_INPUT)
    set_subcategory (SUBCAT_INPUT_ACCESS)
    set_capability ("access_demux", 0)
    set_callbacks (Open, Close)

77
    add_float ("screen-fps", 2.0, FPS_TEXT, FPS_LONGTEXT, true)
78
    add_integer ("screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true)
79 80
        change_integer_range (-32768, 32767)
        change_safe ()
81
    add_integer ("screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true)
82 83
        change_integer_range (-32768, 32767)
        change_safe ()
84
    add_integer ("screen-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, true)
85 86
        change_integer_range (0, 65535)
        change_safe ()
87
    add_integer ("screen-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
88 89
        change_integer_range (0, 65535)
        change_safe ()
90
    add_bool ("screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
91
              FOLLOW_MOUSE_LONGTEXT, true)
92

93
    add_shortcut ("screen", "window")
94 95 96 97 98
vlc_module_end ()

/*
 * Local prototypes
 */
99
static void Demux (void *);
100
static int Control (demux_t *, int, va_list);
101
static es_out_id_t *InitES (demux_t *, uint_fast16_t, uint_fast16_t,
102
                            uint_fast8_t, uint8_t *);
103 104 105

struct demux_sys_t
{
106
    /* All owned by timer thread while timer is armed: */
107 108 109 110 111 112 113 114 115 116
    xcb_connection_t *conn; /**< XCB connection */
    es_out_id_t      *es; /**< Window capture stream */
    float             rate; /**< Frame rate */
    xcb_window_t      window; /**< Captured window XID  */
    xcb_pixmap_t      pixmap; /**< Pixmap for composited capture */
    xcb_shm_seg_t     segment; /**< SHM segment XID */
    int16_t           x, y; /**< Requested capture top-left coordinates */
    uint16_t          w, h; /**< Requested capture pixel dimensions */
    uint8_t           bpp; /**< Actual bytes per pixel *es */
    bool              shm; /**< Whether to use MIT-SHM */
117
    bool              follow_mouse;
118
    uint16_t          cur_w, cur_h; /**< Actual capture pixel dimensions */
119
    /* Timer does not use this, only input thread: */
120
    vlc_timer_t       timer;
121 122
};

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/** Checks MIT-SHM shared memory support */
static bool CheckSHM (xcb_connection_t *conn)
{
#ifdef HAVE_SYS_SHM_H
    xcb_shm_query_version_cookie_t ck = xcb_shm_query_version (conn);
    xcb_shm_query_version_reply_t *r;

    r = xcb_shm_query_version_reply (conn, ck, NULL);
    free (r);
    return r != NULL;
#else
    (void) conn;
    return false;
#endif
}

139 140 141 142 143 144 145 146 147 148
/**
 * Probes and initializes.
 */
static int Open (vlc_object_t *obj)
{
    demux_t *demux = (demux_t *)obj;
    demux_sys_t *p_sys = malloc (sizeof (*p_sys));

    if (p_sys == NULL)
        return VLC_ENOMEM;
149
    demux->p_sys = p_sys;
150 151

    /* Connect to X server */
152
    char *display = var_InheritString (obj, "x11-display");
153 154 155 156 157 158 159 160 161 162
    int snum;
    xcb_connection_t *conn = xcb_connect (display, &snum);
    free (display);
    if (xcb_connection_has_error (conn))
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
    p_sys->conn = conn;

163 164
   /* Find configured screen */
    if (!strcmp (demux->psz_access, "screen"))
165
    {
166 167 168 169
        const xcb_setup_t *setup = xcb_get_setup (conn);
        const xcb_screen_t *scr = NULL;
        for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
             i.rem > 0; xcb_screen_next (&i))
170
        {
171 172
            if (snum == 0)
            {
173
                scr = i.data;
174 175 176
                break;
            }
            snum--;
177
        }
178 179 180 181 182 183
        if (scr == NULL)
        {
            msg_Err (obj, "bad X11 screen number");
            goto error;
        }
        p_sys->window = scr->root;
184 185
    }
    else
186
    /* Determine capture window */
187 188 189
    if (!strcmp (demux->psz_access, "window"))
    {
        char *end;
190
        unsigned long ul = strtoul (demux->psz_location, &end, 0);
191 192
        if (*end || ul > 0xffffffff)
        {
193
            msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
194 195 196
            goto error;
        }
        p_sys->window = ul;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

        xcb_composite_query_version_reply_t *r =
            xcb_composite_query_version_reply (conn,
                xcb_composite_query_version (conn, 0, 4), NULL);
        if (r == NULL || r->minor_version < 2)
        {
            msg_Err (obj, "X Composite extension not available");
            free (r);
            goto error;
        }
        msg_Dbg (obj, "using Composite extension v%"PRIu32".%"PRIu32,
                 r->major_version, r->minor_version);
        free (r);

        xcb_composite_redirect_window (conn, p_sys->window,
                                       XCB_COMPOSITE_REDIRECT_AUTOMATIC);
213 214 215 216 217
    }
    else
        goto error;

    /* Window properties */
218
    p_sys->pixmap = xcb_generate_id (conn);
219 220
    p_sys->segment = xcb_generate_id (conn);
    p_sys->shm = CheckSHM (conn);
221 222
    p_sys->w = var_InheritInteger (obj, "screen-width");
    p_sys->h = var_InheritInteger (obj, "screen-height");
223 224 225 226 227 228 229 230 231
    if (p_sys->w != 0 || p_sys->h != 0)
        p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
    else /* Following mouse is meaningless if width&height are dynamic. */
        p_sys->follow_mouse = false;
    if (!p_sys->follow_mouse) /* X and Y are meaningless if following mouse */
    {
        p_sys->x = var_InheritInteger (obj, "screen-left");
        p_sys->y = var_InheritInteger (obj, "screen-top");
    }
232 233

    /* Initializes format */
234
    p_sys->rate = var_InheritFloat (obj, "screen-fps");
235
    if (!p_sys->rate)
236
        goto error;
237 238 239

    mtime_t interval = (float)CLOCK_FREQ / p_sys->rate;
    if (!interval)
240 241
        goto error;

242 243
    p_sys->cur_w = 0;
    p_sys->cur_h = 0;
244
    p_sys->bpp = 0;
245
    p_sys->es = NULL;
246
    if (vlc_timer_create (&p_sys->timer, Demux, demux))
247
        goto error;
248
    vlc_timer_schedule (p_sys->timer, false, 1, interval);
249 250

    /* Initializes demux */
251
    demux->pf_demux   = NULL;
252 253 254 255
    demux->pf_control = Control;
    return VLC_SUCCESS;

error:
256 257
    xcb_disconnect (p_sys->conn);
    free (p_sys);
258 259 260 261 262 263 264 265 266 267 268 269
    return VLC_EGENERIC;
}


/**
 * Releases resources
 */
static void Close (vlc_object_t *obj)
{
    demux_t *demux = (demux_t *)obj;
    demux_sys_t *p_sys = demux->p_sys;

270
    vlc_timer_destroy (p_sys->timer);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    xcb_disconnect (p_sys->conn);
    free (p_sys);
}


/**
 * Control callback
 */
static int Control (demux_t *demux, int query, va_list args)
{
    switch (query)
    {
        case DEMUX_GET_POSITION:
        {
            float *v = va_arg (args, float *);
            *v = 0.;
            return VLC_SUCCESS;
        }

        case DEMUX_GET_LENGTH:
        case DEMUX_GET_TIME:
        {
            int64_t *v = va_arg (args, int64_t *);
            *v = 0;
            return VLC_SUCCESS;
        }

        /* TODO: get title info -> crawl visible windows */

        case DEMUX_GET_PTS_DELAY:
        {
            int64_t *v = va_arg (args, int64_t *);
303
            *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
304 305 306
            return VLC_SUCCESS;
        }

307
        case DEMUX_CAN_PAUSE:
308
        case DEMUX_CAN_CONTROL_PACE:
309 310 311
        case DEMUX_CAN_CONTROL_RATE:
        case DEMUX_CAN_SEEK:
        {
312
            bool *v = va_arg (args, bool *);
313 314 315
            *v = false;
            return VLC_SUCCESS;
        }
316 317 318

        case DEMUX_SET_PAUSE_STATE:
            return VLC_SUCCESS; /* should not happen */
319 320 321 322 323 324 325 326 327
    }

    return VLC_EGENERIC;
}


/**
 * Processing callback
 */
328
static void Demux (void *opaque)
329
{
330
    demux_t *demux = opaque;
331 332 333 334 335 336
    demux_sys_t *sys = demux->p_sys;
    xcb_connection_t *conn = sys->conn;

    /* Determine capture region */
    xcb_get_geometry_cookie_t gc;
    xcb_query_pointer_cookie_t qc;
337

338 339 340
    gc = xcb_get_geometry (conn, sys->window);
    if (sys->follow_mouse)
        qc = xcb_query_pointer (conn, sys->window);
341

342
    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, gc, NULL);
343 344
    if (geo == NULL)
    {
345
        msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, sys->window);
346
discard:
347 348
        if (sys->follow_mouse)
            xcb_discard_reply (conn, gc.sequence);
349 350 351
        return;
    }

352 353
    unsigned w = sys->w;
    unsigned h = sys->h;
354
    int x, y;
355

356
    if (sys->follow_mouse)
357 358 359
    {
        xcb_query_pointer_reply_t *ptr =
            xcb_query_pointer_reply (conn, qc, NULL);
360
        if (ptr == NULL)
361
        {
362 363
            free (geo);
            return;
364
        }
365 366 367 368

        if (w == 0 || w > geo->width)
            w = geo->width;
        x = ptr->win_x;
369
        if (x < (int)(w / 2))
370
            x = 0;
371
        else if (x >= (int)(geo->width - (w / 2)))
372 373 374 375 376 377 378
            x = geo->width - w;
        else
            x -= w / 2;

        if (h == 0 || h > geo->height)
            h = geo->height;
        y = ptr->win_y;
379
        if (y < (int)(h / 2))
380
            y = 0;
381
        else if (y >= (int)(geo->height - (h / 2)))
382 383 384 385 386 387 388 389 390 391 392 393
            y = geo->height - h;
        else
            y -= h / 2;
    }
    else
    {
        int max;

        x = sys->x;
        max = (int)geo->width - x;
        if (max <= 0)
            goto discard;
394
        if (w == 0 || w > (unsigned)max)
395 396 397 398 399 400
            w = max;

        y = sys->y;
        max = (int)geo->height - y;
        if (max <= 0)
            goto discard;
401
        if (h == 0 || h > (unsigned)max)
402
            h = max;
403 404
    }

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    /* Update elementary stream format (if needed) */
    if (w != sys->cur_w || h != sys->cur_h)
    {
        if (sys->es != NULL)
            es_out_Del (demux->out, sys->es);

        /* Update composite pixmap */
        if (sys->window != geo->root)
        {
            xcb_free_pixmap (conn, sys->pixmap); /* no-op first time */
            xcb_composite_name_window_pixmap (conn, sys->window, sys->pixmap);
            xcb_create_pixmap (conn, geo->depth, sys->pixmap,
                               geo->root, geo->width, geo->height);
        }

420
        sys->es = InitES (demux, w, h, geo->depth, &sys->bpp);
421 422 423 424
        if (sys->es != NULL)
        {
            sys->cur_w = w;
            sys->cur_h = h;
425
            sys->bpp /= 8; /* bits -> bytes */
426 427 428
        }
    }

429
    /* Capture screen */
430 431 432 433
    xcb_drawable_t drawable =
        (sys->window != geo->root) ? sys->pixmap : sys->window;
    free (geo);

434 435 436 437 438 439 440 441
    block_t *block = NULL;
#if HAVE_SYS_SHM_H
    if (sys->shm)
    {   /* Capture screen through shared memory */
        size_t size = w * h * sys->bpp;
        int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
        if (id == -1) /* XXX: fallback */
        {
442 443
            msg_Err (demux, "shared memory allocation error: %s",
                     vlc_strerror_c(errno));
444 445 446 447 448 449
            goto noshm;
        }

        /* Attach the segment to X and capture */
        xcb_shm_get_image_reply_t *img;
        xcb_shm_get_image_cookie_t ck;
450

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        xcb_shm_attach (conn, sys->segment, id, 0 /* read/write */);
        ck = xcb_shm_get_image (conn, drawable, x, y, w, h, ~0,
                                XCB_IMAGE_FORMAT_Z_PIXMAP, sys->segment, 0);
        xcb_shm_detach (conn, sys->segment);
        img = xcb_shm_get_image_reply (conn, ck, NULL);
        xcb_flush (conn); /* ensure eventual detach */

        if (img == NULL)
        {
            shmctl (id, IPC_RMID, 0);
            goto noshm;
        }
        free (img);

        /* Attach the segment to VLC */
        void *shm = shmat (id, NULL, 0 /* read/write */);
        shmctl (id, IPC_RMID, 0);
        if (-1 == (intptr_t)shm)
        {
470 471
            msg_Err (demux, "shared memory attachment error: %s",
                     vlc_strerror_c(errno));
472 473 474 475 476 477 478 479 480
            return;
        }

        block = block_shm_Alloc (shm, size);
        if (unlikely(block == NULL))
            shmdt (shm);
    }
noshm:
#endif
481
    if (block == NULL)
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    {   /* Capture screen through socket (fallback) */
        xcb_get_image_reply_t *img;

        img = xcb_get_image_reply (conn,
            xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
                           x, y, w, h, ~0), NULL);
        if (img == NULL)
            return;

        uint8_t *data = xcb_get_image_data (img);
        size_t datalen = xcb_get_image_data_length (img);
        block = block_heap_Alloc (img, data + datalen - (uint8_t *)img);
        if (block == NULL)
            return;
        block->p_buffer = data;
        block->i_buffer = datalen;
    }
499

500
    /* Send block - zero copy */
501
    if (sys->es != NULL)
502
    {
503
        block->i_pts = block->i_dts = mdate ();
504

505
        es_out_SetPCR(demux->out, block->i_pts);
506
        es_out_Send (demux->out, sys->es, block);
507
    }
508 509
    else
        block_Release (block);
510
}
511 512

static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
513 514
                            uint_fast16_t height, uint_fast8_t depth,
                            uint8_t *bpp)
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
{
    demux_sys_t *p_sys = demux->p_sys;
    const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
    uint32_t chroma = 0;

    for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
             *end = fmt + xcb_setup_pixmap_formats_length (setup);
         fmt < end; fmt++)
    {
        if (fmt->depth != depth)
            continue;
        switch (fmt->depth)
        {
            case 32:
                if (fmt->bits_per_pixel == 32)
530
                    chroma = VLC_CODEC_ARGB;
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
                break;
            case 24:
                if (fmt->bits_per_pixel == 32)
                    chroma = VLC_CODEC_RGB32;
                else if (fmt->bits_per_pixel == 24)
                    chroma = VLC_CODEC_RGB24;
                break;
            case 16:
                if (fmt->bits_per_pixel == 16)
                    chroma = VLC_CODEC_RGB16;
                break;
            case 15:
                if (fmt->bits_per_pixel == 16)
                    chroma = VLC_CODEC_RGB15;
                break;
            case 8: /* XXX: screw grey scale! */
                if (fmt->bits_per_pixel == 8)
                    chroma = VLC_CODEC_RGB8;
                break;
        }
        if (chroma != 0)
552 553
        {
            *bpp = fmt->bits_per_pixel;
554
            break;
555
        }
556 557 558 559 560 561 562 563 564 565 566 567
    }

    if (!chroma)
    {
        msg_Err (demux, "unsupported pixmap formats");
        return NULL;
    }

    es_format_t fmt;

    es_format_Init (&fmt, VIDEO_ES, chroma);
    fmt.video.i_chroma = chroma;
568
    fmt.video.i_bits_per_pixel = *bpp;
569 570 571 572 573 574 575 576
    fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
    fmt.video.i_frame_rate = 1000 * p_sys->rate;
    fmt.video.i_frame_rate_base = 1000;
    fmt.video.i_visible_width = fmt.video.i_width = width;
    fmt.video.i_visible_height = fmt.video.i_height = height;

    return es_out_Add (demux->out, &fmt);
}