3 * @brief X11 C Bindings screen capture demux module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This library 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 General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, 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 CACHING_TEXT N_("Caching value in ms")
35 #define CACHING_LONGTEXT N_( \
36 "Caching value for screen capture. " \
37 "This value should be set in milliseconds.")
39 #define FPS_TEXT N_("Frame rate")
40 #define FPS_LONGTEXT N_( \
41 "How many times the screen content should be refreshed per second.")
43 #define LEFT_TEXT N_("Region left column")
44 #define LEFT_LONGTEXT N_( \
45 "Abscissa of the capture reion in pixels.")
47 #define TOP_TEXT N_("Region top row")
48 #define TOP_LONGTEXT N_( \
49 "Ordinate of the capture region in pixels.")
51 #define WIDTH_TEXT N_("Capture region width")
52 #define WIDTH_LONGTEXT N_( \
53 "Pixel width of the capture region, or 0 for full width")
55 #define HEIGHT_TEXT N_("Capture region height")
56 #define HEIGHT_LONGTEXT N_( \
57 "Pixel height of the capture region, or 0 for full height")
59 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
60 #define FOLLOW_MOUSE_LONGTEXT N_( \
61 "Follow the mouse when capturing a subscreen." )
63 static int Open (vlc_object_t *);
64 static void Close (vlc_object_t *);
70 set_shortname (N_("Screen"))
71 set_description (N_("Screen capture (with X11/XCB)"))
72 set_category (CAT_INPUT)
73 set_subcategory (SUBCAT_INPUT_ACCESS)
74 set_capability ("access_demux", 0)
75 set_callbacks (Open, Close)
77 add_integer ("screen-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
78 CACHING_TEXT, CACHING_LONGTEXT, true)
79 add_float ("screen-fps", 2.0, FPS_TEXT, FPS_LONGTEXT, true)
80 add_integer ("screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true)
81 change_integer_range (-32768, 32767)
83 add_integer ("screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true)
84 change_integer_range (-32768, 32767)
86 add_integer ("screen-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, true)
87 change_integer_range (0, 65535)
89 add_integer ("screen-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
90 change_integer_range (0, 65535)
92 add_bool ("screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
93 FOLLOW_MOUSE_LONGTEXT, true)
95 add_shortcut ("screen", "window")
101 static void Demux (void *);
102 static int Control (demux_t *, int, va_list);
103 static es_out_id_t *InitES (demux_t *, uint_fast16_t, uint_fast16_t,
108 /* All owned by timer thread while timer is armed: */
109 xcb_connection_t *conn;
111 mtime_t pts, interval;
117 uint16_t cur_w, cur_h;
119 /* Timer does not use this, only input thread: */
124 * Probes and initializes.
126 static int Open (vlc_object_t *obj)
128 demux_t *demux = (demux_t *)obj;
129 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
133 demux->p_sys = p_sys;
135 /* Connect to X server */
136 char *display = var_InheritString (obj, "x11-display");
138 xcb_connection_t *conn = xcb_connect (display, &snum);
140 if (xcb_connection_has_error (conn))
147 /* Find configured screen */
148 if (!strcmp (demux->psz_access, "screen"))
150 const xcb_setup_t *setup = xcb_get_setup (conn);
151 const xcb_screen_t *scr = NULL;
152 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
153 i.rem > 0; xcb_screen_next (&i))
164 msg_Err (obj, "bad X11 screen number");
167 p_sys->window = scr->root;
170 /* Determine capture window */
171 if (!strcmp (demux->psz_access, "window"))
174 unsigned long ul = strtoul (demux->psz_location, &end, 0);
175 if (*end || ul > 0xffffffff)
177 msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
182 xcb_composite_query_version_reply_t *r =
183 xcb_composite_query_version_reply (conn,
184 xcb_composite_query_version (conn, 0, 4), NULL);
185 if (r == NULL || r->minor_version < 2)
187 msg_Err (obj, "X Composite extension not available");
191 msg_Dbg (obj, "using Composite extension v%"PRIu32".%"PRIu32,
192 r->major_version, r->minor_version);
195 xcb_composite_redirect_window (conn, p_sys->window,
196 XCB_COMPOSITE_REDIRECT_AUTOMATIC);
201 /* Window properties */
202 p_sys->pixmap = xcb_generate_id (conn);
203 p_sys->x = var_InheritInteger (obj, "screen-left");
204 p_sys->y = var_InheritInteger (obj, "screen-top");
205 p_sys->w = var_InheritInteger (obj, "screen-width");
206 p_sys->h = var_InheritInteger (obj, "screen-height");
207 p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
209 /* Initializes format */
210 p_sys->rate = var_InheritFloat (obj, "screen-fps");
213 p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
214 if (!p_sys->interval)
216 var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
221 p_sys->pts = VLC_TS_INVALID;
222 if (vlc_timer_create (&p_sys->timer, Demux, demux))
224 vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
226 /* Initializes demux */
227 demux->pf_demux = NULL;
228 demux->pf_control = Control;
232 xcb_disconnect (p_sys->conn);
241 static void Close (vlc_object_t *obj)
243 demux_t *demux = (demux_t *)obj;
244 demux_sys_t *p_sys = demux->p_sys;
246 vlc_timer_destroy (p_sys->timer);
247 xcb_disconnect (p_sys->conn);
255 static int Control (demux_t *demux, int query, va_list args)
259 case DEMUX_GET_POSITION:
261 float *v = va_arg (args, float *);
266 case DEMUX_GET_LENGTH:
269 int64_t *v = va_arg (args, int64_t *);
274 /* TODO: get title info -> crawl visible windows */
276 case DEMUX_GET_PTS_DELAY:
278 int64_t *v = va_arg (args, int64_t *);
279 *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
283 case DEMUX_CAN_PAUSE:
284 case DEMUX_CAN_CONTROL_PACE:
285 case DEMUX_CAN_CONTROL_RATE:
288 bool *v = (bool*)va_arg( args, bool * );
293 case DEMUX_SET_PAUSE_STATE:
294 return VLC_SUCCESS; /* should not happen */
302 * Processing callback
304 static void Demux (void *data)
306 demux_t *demux = data;
307 demux_sys_t *sys = demux->p_sys;
308 xcb_connection_t *conn = sys->conn;
310 /* Determine capture region */
311 xcb_get_geometry_cookie_t gc;
312 xcb_query_pointer_cookie_t qc;
314 gc = xcb_get_geometry (conn, sys->window);
315 if (sys->follow_mouse)
316 qc = xcb_query_pointer (conn, sys->window);
318 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, gc, NULL);
321 msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, sys->window);
322 if (sys->follow_mouse)
323 xcb_discard_reply (conn, gc.sequence);
327 /* FIXME: review for signed overflow */
328 int16_t x = sys->x, y = sys->y;
329 uint16_t w = sys->w, h = sys->h;
331 uint16_t ow = geo->width - sys->x;
332 uint16_t oh = geo->height - sys->y;
333 if (w == 0 || w > ow)
335 if (h == 0 || h > oh)
338 if (sys->follow_mouse)
340 xcb_query_pointer_reply_t *ptr =
341 xcb_query_pointer_reply (conn, qc, NULL);
344 int16_t min_x = x + (w / 2);
345 int16_t min_y = y + (h / 2);
346 int16_t max_x = x + ow - ((w + 1) / 2);
347 int16_t max_y = y + oh - ((h + 1) / 2);
349 assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
350 if (ptr->root_x > max_x)
352 else if (ptr->root_x > min_x)
353 x += ptr->root_x - min_x;
355 assert (max_y >= min_y);
356 if (ptr->root_y > max_y)
358 else if (ptr->root_y > min_y)
359 y += ptr->root_y - min_y;
364 /* Update elementary stream format (if needed) */
365 if (w != sys->cur_w || h != sys->cur_h)
368 es_out_Del (demux->out, sys->es);
370 /* Update composite pixmap */
371 if (sys->window != geo->root)
373 xcb_free_pixmap (conn, sys->pixmap); /* no-op first time */
374 xcb_composite_name_window_pixmap (conn, sys->window, sys->pixmap);
375 xcb_create_pixmap (conn, geo->depth, sys->pixmap,
376 geo->root, geo->width, geo->height);
379 sys->es = InitES (demux, w, h, geo->depth);
388 xcb_drawable_t drawable =
389 (sys->window != geo->root) ? sys->pixmap : sys->window;
392 xcb_get_image_reply_t *img;
393 img = xcb_get_image_reply (conn,
394 xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
395 x, y, w, h, ~0), NULL);
399 block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
400 xcb_get_image_data_length (img));
404 /* Send block - zero copy */
407 if (sys->pts == VLC_TS_INVALID)
409 block->i_pts = block->i_dts = sys->pts;
411 es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
412 es_out_Send (demux->out, sys->es, block);
413 sys->pts += sys->interval;
417 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
418 uint_fast16_t height, uint_fast8_t depth)
420 demux_sys_t *p_sys = demux->p_sys;
421 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
425 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
426 *end = fmt + xcb_setup_pixmap_formats_length (setup);
429 if (fmt->depth != depth)
435 if (fmt->bits_per_pixel == 32)
436 chroma = VLC_CODEC_RGBA;
439 if (fmt->bits_per_pixel == 32)
441 chroma = VLC_CODEC_RGB32;
444 else if (fmt->bits_per_pixel == 24)
445 chroma = VLC_CODEC_RGB24;
448 if (fmt->bits_per_pixel == 16)
449 chroma = VLC_CODEC_RGB16;
452 if (fmt->bits_per_pixel == 16)
453 chroma = VLC_CODEC_RGB15;
455 case 8: /* XXX: screw grey scale! */
456 if (fmt->bits_per_pixel == 8)
457 chroma = VLC_CODEC_RGB8;
466 msg_Err (demux, "unsupported pixmap formats");
472 es_format_Init (&fmt, VIDEO_ES, chroma);
473 fmt.video.i_chroma = chroma;
474 fmt.video.i_bits_per_pixel = bpp;
475 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
476 fmt.video.i_frame_rate = 1000 * p_sys->rate;
477 fmt.video.i_frame_rate_base = 1000;
478 fmt.video.i_visible_width = fmt.video.i_width = width;
479 fmt.video.i_visible_height = fmt.video.i_height = height;
481 return es_out_Add (demux->out, &fmt);