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 <vlc_common.h>
30 #include <vlc_demux.h>
31 #include <vlc_plugin.h>
33 #define CACHING_TEXT N_("Caching value in ms")
34 #define CACHING_LONGTEXT N_( \
35 "Caching value for screen capture. " \
36 "This value should be set in milliseconds.")
38 #define FPS_TEXT N_("Frame rate")
39 #define FPS_LONGTEXT N_( \
40 "How many times the screen content should be refreshed per second.")
42 #define LEFT_TEXT N_("Region left column")
43 #define LEFT_LONGTEXT N_( \
44 "Abscissa of the capture reion in pixels.")
46 #define TOP_TEXT N_("Region top row")
47 #define TOP_LONGTEXT N_( \
48 "Ordinate of the capture region in pixels.")
50 #define WIDTH_TEXT N_("Capture region width")
51 #define WIDTH_LONGTEXT N_( \
52 "Pixel width of the capture region, or 0 for full width")
54 #define HEIGHT_TEXT N_("Capture region height")
55 #define HEIGHT_LONGTEXT N_( \
56 "Pixel height of the capture region, or 0 for full height")
58 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
59 #define FOLLOW_MOUSE_LONGTEXT N_( \
60 "Follow the mouse when capturing a subscreen." )
62 static int Open (vlc_object_t *);
63 static void Close (vlc_object_t *);
69 set_shortname (N_("Screen"))
70 set_description (N_("Screen capture (with X11/XCB)"))
71 set_category (CAT_INPUT)
72 set_subcategory (SUBCAT_INPUT_ACCESS)
73 set_capability ("access_demux", 0)
74 set_callbacks (Open, Close)
76 add_integer ("screen-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
77 NULL, CACHING_TEXT, CACHING_LONGTEXT, true)
78 add_float ("screen-fps", 2.0, NULL, FPS_TEXT, FPS_LONGTEXT, true)
79 add_integer ("screen-left", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
80 change_integer_range (-32768, 32767)
82 add_integer ("screen-top", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
83 change_integer_range (-32768, 32767)
85 add_integer ("screen-width", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
86 change_integer_range (0, 65535)
88 add_integer ("screen-height", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
89 change_integer_range (0, 65535)
91 add_bool ("screen-follow-mouse", false, NULL, FOLLOW_MOUSE_TEXT,
92 FOLLOW_MOUSE_LONGTEXT, true)
94 add_shortcut ("screen")
95 add_shortcut ("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 xcb_connection_t *conn;
110 mtime_t pts, interval;
115 uint16_t cur_w, cur_h;
117 /* fmt, es and pts are protected by the lock. The rest is read-only. */
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_CreateGetNonEmptyString (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_path, &end, 0);
175 if (*end || ul > 0xffffffff)
177 msg_Err (obj, "bad X11 drawable %s", demux->psz_path);
185 /* Window properties */
186 p_sys->x = var_CreateGetInteger (obj, "screen-left");
187 p_sys->y = var_CreateGetInteger (obj, "screen-top");
188 p_sys->w = var_CreateGetInteger (obj, "screen-width");
189 p_sys->h = var_CreateGetInteger (obj, "screen-height");
190 p_sys->follow_mouse = var_CreateGetBool (obj, "screen-follow-mouse");
192 /* Initializes format */
193 p_sys->rate = var_CreateGetFloat (obj, "screen-fps");
196 p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
197 if (!p_sys->interval)
199 var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
204 p_sys->pts = VLC_TS_INVALID;
205 vlc_mutex_init (&p_sys->lock);
206 if (vlc_timer_create (&p_sys->timer, Demux, demux))
208 vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
210 /* Initializes demux */
211 demux->pf_demux = NULL;
212 demux->pf_control = Control;
216 xcb_disconnect (p_sys->conn);
225 static void Close (vlc_object_t *obj)
227 demux_t *demux = (demux_t *)obj;
228 demux_sys_t *p_sys = demux->p_sys;
230 vlc_timer_destroy (p_sys->timer);
231 vlc_mutex_destroy (&p_sys->lock);
232 xcb_disconnect (p_sys->conn);
240 static int Control (demux_t *demux, int query, va_list args)
242 demux_sys_t *p_sys = demux->p_sys;
246 case DEMUX_GET_POSITION:
248 float *v = va_arg (args, float *);
253 case DEMUX_GET_LENGTH:
256 int64_t *v = va_arg (args, int64_t *);
261 /* TODO: get title info -> crawl visible windows */
263 case DEMUX_GET_PTS_DELAY:
265 int64_t *v = va_arg (args, int64_t *);
266 *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
270 case DEMUX_CAN_PAUSE:
272 bool *v = (bool*)va_arg( args, bool * );
277 case DEMUX_SET_PAUSE_STATE:
279 bool pausing = va_arg (args, int);
283 vlc_mutex_lock (&p_sys->lock);
284 p_sys->pts = VLC_TS_INVALID;
285 es_out_Control (demux->out, ES_OUT_RESET_PCR);
286 vlc_mutex_unlock (&p_sys->lock);
288 vlc_timer_schedule (p_sys->timer, false,
289 pausing ? 0 : 1, p_sys->interval);
293 case DEMUX_CAN_CONTROL_PACE:
294 case DEMUX_CAN_CONTROL_RATE:
297 bool *v = (bool*)va_arg( args, bool * );
308 * Processing callback
310 static void Demux (void *data)
312 demux_t *demux = data;
313 demux_sys_t *p_sys = demux->p_sys;
314 xcb_connection_t *conn = p_sys->conn;
316 /* Update capture region (if needed) */
318 xcb_get_geometry_reply_t *geo =
319 xcb_get_geometry_reply (conn,
320 xcb_get_geometry (conn, p_sys->window), NULL);
323 msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, p_sys->window);
327 xcb_window_t root = geo->root;
328 int16_t x = p_sys->x, y = p_sys->y;
329 xcb_translate_coordinates_cookie_t tc;
330 xcb_query_pointer_cookie_t qc;
332 if (p_sys->window != root)
333 tc = xcb_translate_coordinates (conn, p_sys->window, root,
335 if (p_sys->follow_mouse)
336 qc = xcb_query_pointer (conn, p_sys->window);
338 uint16_t ow = geo->width - x;
339 uint16_t oh = geo->height - y;
340 uint16_t w = p_sys->w;
341 uint16_t h = p_sys->h;
342 if (w == 0 || w > ow)
344 if (h == 0 || h > oh)
346 uint8_t depth = geo->depth;
349 if (p_sys->window != root)
351 xcb_translate_coordinates_reply_t *coords =
352 xcb_translate_coordinates_reply (conn, tc, NULL);
361 if (p_sys->follow_mouse)
363 xcb_query_pointer_reply_t *ptr =
364 xcb_query_pointer_reply (conn, qc, NULL);
367 int16_t min_x = x + (w / 2);
368 int16_t min_y = y + (h / 2);
369 int16_t max_x = x + ow - ((w + 1) / 2);
370 int16_t max_y = y + oh - ((h + 1) / 2);
372 assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
373 if (ptr->root_x > max_x)
375 else if (ptr->root_x > min_x)
376 x += ptr->root_x - min_x;
378 assert (max_y >= min_y);
379 if (ptr->root_y > max_y)
381 else if (ptr->root_y > min_y)
382 y += ptr->root_y - min_y;
386 xcb_get_image_reply_t *img;
387 img = xcb_get_image_reply (conn,
388 xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, root,
389 x, y, w, h, ~0), NULL);
393 /* Send block - zero copy */
394 block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
395 xcb_get_image_data_length (img));
399 vlc_mutex_lock (&p_sys->lock);
400 if (w != p_sys->cur_w || h != p_sys->cur_h)
402 if (p_sys->es != NULL)
403 es_out_Del (demux->out, p_sys->es);
404 p_sys->es = InitES (demux, w, h, depth);
405 if (p_sys->es != NULL)
413 if (p_sys->es != NULL)
415 if (p_sys->pts == VLC_TS_INVALID)
416 p_sys->pts = mdate ();
417 block->i_pts = block->i_dts = p_sys->pts;
419 es_out_Control (demux->out, ES_OUT_SET_PCR, p_sys->pts);
420 es_out_Send (demux->out, p_sys->es, block);
421 p_sys->pts += p_sys->interval;
423 vlc_mutex_unlock (&p_sys->lock);
426 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
427 uint_fast16_t height, uint_fast8_t depth)
429 demux_sys_t *p_sys = demux->p_sys;
430 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
434 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
435 *end = fmt + xcb_setup_pixmap_formats_length (setup);
438 if (fmt->depth != depth)
444 if (fmt->bits_per_pixel == 32)
445 chroma = VLC_CODEC_RGBA;
448 if (fmt->bits_per_pixel == 32)
450 chroma = VLC_CODEC_RGB32;
453 else if (fmt->bits_per_pixel == 24)
454 chroma = VLC_CODEC_RGB24;
457 if (fmt->bits_per_pixel == 16)
458 chroma = VLC_CODEC_RGB16;
461 if (fmt->bits_per_pixel == 16)
462 chroma = VLC_CODEC_RGB15;
464 case 8: /* XXX: screw grey scale! */
465 if (fmt->bits_per_pixel == 8)
466 chroma = VLC_CODEC_RGB8;
475 msg_Err (demux, "unsupported pixmap formats");
481 es_format_Init (&fmt, VIDEO_ES, chroma);
482 fmt.video.i_chroma = chroma;
483 fmt.video.i_bits_per_pixel = bpp;
484 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
485 fmt.video.i_frame_rate = 1000 * p_sys->rate;
486 fmt.video.i_frame_rate_base = 1000;
487 fmt.video.i_visible_width = fmt.video.i_width = width;
488 fmt.video.i_visible_height = fmt.video.i_height = height;
490 return es_out_Add (demux->out, &fmt);