3 * @brief X11 C Bindings screen capture demux module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
29 #include <xcb/composite.h>
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
32 #include <vlc_plugin.h>
34 #define FPS_TEXT N_("Frame rate")
35 #define FPS_LONGTEXT N_( \
36 "How many times the screen content should be refreshed per second.")
38 #define LEFT_TEXT N_("Region left column")
39 #define LEFT_LONGTEXT N_( \
40 "Abscissa of the capture region in pixels.")
42 #define TOP_TEXT N_("Region top row")
43 #define TOP_LONGTEXT N_( \
44 "Ordinate of the capture region in pixels.")
46 #define WIDTH_TEXT N_("Capture region width")
47 #define WIDTH_LONGTEXT N_( \
48 "Pixel width of the capture region, or 0 for full width")
50 #define HEIGHT_TEXT N_("Capture region height")
51 #define HEIGHT_LONGTEXT N_( \
52 "Pixel height of the capture region, or 0 for full height")
54 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
55 #define FOLLOW_MOUSE_LONGTEXT N_( \
56 "Follow the mouse when capturing a subscreen." )
58 static int Open (vlc_object_t *);
59 static void Close (vlc_object_t *);
65 set_shortname (N_("Screen"))
66 set_description (N_("Screen capture (with X11/XCB)"))
67 set_category (CAT_INPUT)
68 set_subcategory (SUBCAT_INPUT_ACCESS)
69 set_capability ("access_demux", 0)
70 set_callbacks (Open, Close)
72 add_float ("screen-fps", 2.0, FPS_TEXT, FPS_LONGTEXT, true)
73 add_integer ("screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true)
74 change_integer_range (-32768, 32767)
76 add_integer ("screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true)
77 change_integer_range (-32768, 32767)
79 add_integer ("screen-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, true)
80 change_integer_range (0, 65535)
82 add_integer ("screen-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
83 change_integer_range (0, 65535)
85 add_bool ("screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
86 FOLLOW_MOUSE_LONGTEXT, true)
88 add_shortcut ("screen", "window")
94 static void Demux (void *);
95 static int Control (demux_t *, int, va_list);
96 static es_out_id_t *InitES (demux_t *, uint_fast16_t, uint_fast16_t,
101 /* All owned by timer thread while timer is armed: */
102 xcb_connection_t *conn;
104 mtime_t pts, interval;
110 uint16_t cur_w, cur_h;
112 /* Timer does not use this, only input thread: */
117 * Probes and initializes.
119 static int Open (vlc_object_t *obj)
121 demux_t *demux = (demux_t *)obj;
122 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
126 demux->p_sys = p_sys;
128 /* Connect to X server */
129 char *display = var_InheritString (obj, "x11-display");
131 xcb_connection_t *conn = xcb_connect (display, &snum);
133 if (xcb_connection_has_error (conn))
140 /* Find configured screen */
141 if (!strcmp (demux->psz_access, "screen"))
143 const xcb_setup_t *setup = xcb_get_setup (conn);
144 const xcb_screen_t *scr = NULL;
145 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
146 i.rem > 0; xcb_screen_next (&i))
157 msg_Err (obj, "bad X11 screen number");
160 p_sys->window = scr->root;
163 /* Determine capture window */
164 if (!strcmp (demux->psz_access, "window"))
167 unsigned long ul = strtoul (demux->psz_location, &end, 0);
168 if (*end || ul > 0xffffffff)
170 msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
175 xcb_composite_query_version_reply_t *r =
176 xcb_composite_query_version_reply (conn,
177 xcb_composite_query_version (conn, 0, 4), NULL);
178 if (r == NULL || r->minor_version < 2)
180 msg_Err (obj, "X Composite extension not available");
184 msg_Dbg (obj, "using Composite extension v%"PRIu32".%"PRIu32,
185 r->major_version, r->minor_version);
188 xcb_composite_redirect_window (conn, p_sys->window,
189 XCB_COMPOSITE_REDIRECT_AUTOMATIC);
194 /* Window properties */
195 p_sys->pixmap = xcb_generate_id (conn);
196 p_sys->w = var_InheritInteger (obj, "screen-width");
197 p_sys->h = var_InheritInteger (obj, "screen-height");
198 if (p_sys->w != 0 || p_sys->h != 0)
199 p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
200 else /* Following mouse is meaningless if width&height are dynamic. */
201 p_sys->follow_mouse = false;
202 if (!p_sys->follow_mouse) /* X and Y are meaningless if following mouse */
204 p_sys->x = var_InheritInteger (obj, "screen-left");
205 p_sys->y = var_InheritInteger (obj, "screen-top");
208 /* Initializes format */
209 p_sys->rate = var_InheritFloat (obj, "screen-fps");
212 p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
213 if (!p_sys->interval)
219 p_sys->pts = VLC_TS_INVALID;
220 if (vlc_timer_create (&p_sys->timer, Demux, demux))
222 vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
224 /* Initializes demux */
225 demux->pf_demux = NULL;
226 demux->pf_control = Control;
230 xcb_disconnect (p_sys->conn);
239 static void Close (vlc_object_t *obj)
241 demux_t *demux = (demux_t *)obj;
242 demux_sys_t *p_sys = demux->p_sys;
244 vlc_timer_destroy (p_sys->timer);
245 xcb_disconnect (p_sys->conn);
253 static int Control (demux_t *demux, int query, va_list args)
257 case DEMUX_GET_POSITION:
259 float *v = va_arg (args, float *);
264 case DEMUX_GET_LENGTH:
267 int64_t *v = va_arg (args, int64_t *);
272 /* TODO: get title info -> crawl visible windows */
274 case DEMUX_GET_PTS_DELAY:
276 int64_t *v = va_arg (args, int64_t *);
277 *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
281 case DEMUX_CAN_PAUSE:
282 case DEMUX_CAN_CONTROL_PACE:
283 case DEMUX_CAN_CONTROL_RATE:
286 bool *v = (bool*)va_arg( args, bool * );
291 case DEMUX_SET_PAUSE_STATE:
292 return VLC_SUCCESS; /* should not happen */
300 * Processing callback
302 static void Demux (void *data)
304 demux_t *demux = data;
305 demux_sys_t *sys = demux->p_sys;
306 xcb_connection_t *conn = sys->conn;
308 /* Determine capture region */
309 xcb_get_geometry_cookie_t gc;
310 xcb_query_pointer_cookie_t qc;
312 gc = xcb_get_geometry (conn, sys->window);
313 if (sys->follow_mouse)
314 qc = xcb_query_pointer (conn, sys->window);
316 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, gc, NULL);
319 msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, sys->window);
321 if (sys->follow_mouse)
322 xcb_discard_reply (conn, gc.sequence);
330 if (sys->follow_mouse)
332 xcb_query_pointer_reply_t *ptr =
333 xcb_query_pointer_reply (conn, qc, NULL);
340 if (w == 0 || w > geo->width)
345 else if (x >= (int)geo->width - (w / 2))
350 if (h == 0 || h > geo->height)
355 else if (y >= (int)geo->height - (h / 2))
365 max = (int)geo->width - x;
368 if (w == 0 || w > max)
372 max = (int)geo->height - y;
375 if (h == 0 || h > max)
379 /* Update elementary stream format (if needed) */
380 if (w != sys->cur_w || h != sys->cur_h)
383 es_out_Del (demux->out, sys->es);
385 /* Update composite pixmap */
386 if (sys->window != geo->root)
388 xcb_free_pixmap (conn, sys->pixmap); /* no-op first time */
389 xcb_composite_name_window_pixmap (conn, sys->window, sys->pixmap);
390 xcb_create_pixmap (conn, geo->depth, sys->pixmap,
391 geo->root, geo->width, geo->height);
394 sys->es = InitES (demux, w, h, geo->depth);
403 xcb_drawable_t drawable =
404 (sys->window != geo->root) ? sys->pixmap : sys->window;
407 xcb_get_image_reply_t *img;
408 img = xcb_get_image_reply (conn,
409 xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
410 x, y, w, h, ~0), NULL);
414 block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
415 xcb_get_image_data_length (img));
419 /* Send block - zero copy */
422 if (sys->pts == VLC_TS_INVALID)
424 block->i_pts = block->i_dts = sys->pts;
426 es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
427 es_out_Send (demux->out, sys->es, block);
428 sys->pts += sys->interval;
432 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
433 uint_fast16_t height, uint_fast8_t depth)
435 demux_sys_t *p_sys = demux->p_sys;
436 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
440 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
441 *end = fmt + xcb_setup_pixmap_formats_length (setup);
444 if (fmt->depth != depth)
450 if (fmt->bits_per_pixel == 32)
451 chroma = VLC_CODEC_RGBA;
454 if (fmt->bits_per_pixel == 32)
456 chroma = VLC_CODEC_RGB32;
459 else if (fmt->bits_per_pixel == 24)
460 chroma = VLC_CODEC_RGB24;
463 if (fmt->bits_per_pixel == 16)
464 chroma = VLC_CODEC_RGB16;
467 if (fmt->bits_per_pixel == 16)
468 chroma = VLC_CODEC_RGB15;
470 case 8: /* XXX: screw grey scale! */
471 if (fmt->bits_per_pixel == 8)
472 chroma = VLC_CODEC_RGB8;
481 msg_Err (demux, "unsupported pixmap formats");
487 es_format_Init (&fmt, VIDEO_ES, chroma);
488 fmt.video.i_chroma = chroma;
489 fmt.video.i_bits_per_pixel = bpp;
490 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
491 fmt.video.i_frame_rate = 1000 * p_sys->rate;
492 fmt.video.i_frame_rate_base = 1000;
493 fmt.video.i_visible_width = fmt.video.i_width = width;
494 fmt.video.i_visible_height = fmt.video.i_height = height;
496 return es_out_Add (demux->out, &fmt);