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", "window")
100 static void Demux (void *);
101 static int Control (demux_t *, int, va_list);
102 static es_out_id_t *InitES (demux_t *, uint_fast16_t, uint_fast16_t,
107 xcb_connection_t *conn;
109 mtime_t pts, interval;
114 uint16_t cur_w, cur_h;
116 /* fmt, es and pts are protected by the lock. The rest is read-only. */
118 /* Timer does not use this, only input thread: */
123 * Probes and initializes.
125 static int Open (vlc_object_t *obj)
127 demux_t *demux = (demux_t *)obj;
128 demux_sys_t *p_sys = malloc (sizeof (*p_sys));
132 demux->p_sys = p_sys;
134 /* Connect to X server */
135 char *display = var_InheritString (obj, "x11-display");
137 xcb_connection_t *conn = xcb_connect (display, &snum);
139 if (xcb_connection_has_error (conn))
146 /* Find configured screen */
147 if (!strcmp (demux->psz_access, "screen"))
149 const xcb_setup_t *setup = xcb_get_setup (conn);
150 const xcb_screen_t *scr = NULL;
151 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
152 i.rem > 0; xcb_screen_next (&i))
163 msg_Err (obj, "bad X11 screen number");
166 p_sys->window = scr->root;
169 /* Determine capture window */
170 if (!strcmp (demux->psz_access, "window"))
173 unsigned long ul = strtoul (demux->psz_location, &end, 0);
174 if (*end || ul > 0xffffffff)
176 msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
184 /* Window properties */
185 p_sys->x = var_InheritInteger (obj, "screen-left");
186 p_sys->y = var_InheritInteger (obj, "screen-top");
187 p_sys->w = var_InheritInteger (obj, "screen-width");
188 p_sys->h = var_InheritInteger (obj, "screen-height");
189 p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
191 /* Initializes format */
192 p_sys->rate = var_InheritFloat (obj, "screen-fps");
195 p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
196 if (!p_sys->interval)
198 var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
203 p_sys->pts = VLC_TS_INVALID;
204 vlc_mutex_init (&p_sys->lock);
205 if (vlc_timer_create (&p_sys->timer, Demux, demux))
207 vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
209 /* Initializes demux */
210 demux->pf_demux = NULL;
211 demux->pf_control = Control;
215 xcb_disconnect (p_sys->conn);
224 static void Close (vlc_object_t *obj)
226 demux_t *demux = (demux_t *)obj;
227 demux_sys_t *p_sys = demux->p_sys;
229 vlc_timer_destroy (p_sys->timer);
230 vlc_mutex_destroy (&p_sys->lock);
231 xcb_disconnect (p_sys->conn);
239 static int Control (demux_t *demux, int query, va_list args)
241 demux_sys_t *p_sys = demux->p_sys;
245 case DEMUX_GET_POSITION:
247 float *v = va_arg (args, float *);
252 case DEMUX_GET_LENGTH:
255 int64_t *v = va_arg (args, int64_t *);
260 /* TODO: get title info -> crawl visible windows */
262 case DEMUX_GET_PTS_DELAY:
264 int64_t *v = va_arg (args, int64_t *);
265 *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
269 case DEMUX_CAN_PAUSE:
271 bool *v = (bool*)va_arg( args, bool * );
276 case DEMUX_SET_PAUSE_STATE:
278 bool pausing = va_arg (args, int);
282 vlc_mutex_lock (&p_sys->lock);
283 p_sys->pts = VLC_TS_INVALID;
284 es_out_Control (demux->out, ES_OUT_RESET_PCR);
285 vlc_mutex_unlock (&p_sys->lock);
287 vlc_timer_schedule (p_sys->timer, false,
288 pausing ? 0 : 1, p_sys->interval);
292 case DEMUX_CAN_CONTROL_PACE:
293 case DEMUX_CAN_CONTROL_RATE:
296 bool *v = (bool*)va_arg( args, bool * );
307 * Processing callback
309 static void Demux (void *data)
311 demux_t *demux = data;
312 demux_sys_t *p_sys = demux->p_sys;
313 xcb_connection_t *conn = p_sys->conn;
315 /* Update capture region (if needed) */
317 xcb_get_geometry_reply_t *geo =
318 xcb_get_geometry_reply (conn,
319 xcb_get_geometry (conn, p_sys->window), NULL);
322 msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, p_sys->window);
326 xcb_window_t root = geo->root;
327 int16_t x = p_sys->x, y = p_sys->y;
328 xcb_translate_coordinates_cookie_t tc;
329 xcb_query_pointer_cookie_t qc;
331 if (p_sys->window != root)
332 tc = xcb_translate_coordinates (conn, p_sys->window, root,
334 if (p_sys->follow_mouse)
335 qc = xcb_query_pointer (conn, p_sys->window);
337 uint16_t ow = geo->width - x;
338 uint16_t oh = geo->height - y;
339 uint16_t w = p_sys->w;
340 uint16_t h = p_sys->h;
341 if (w == 0 || w > ow)
343 if (h == 0 || h > oh)
345 uint8_t depth = geo->depth;
348 if (p_sys->window != root)
350 xcb_translate_coordinates_reply_t *coords =
351 xcb_translate_coordinates_reply (conn, tc, NULL);
360 if (p_sys->follow_mouse)
362 xcb_query_pointer_reply_t *ptr =
363 xcb_query_pointer_reply (conn, qc, NULL);
366 int16_t min_x = x + (w / 2);
367 int16_t min_y = y + (h / 2);
368 int16_t max_x = x + ow - ((w + 1) / 2);
369 int16_t max_y = y + oh - ((h + 1) / 2);
371 assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
372 if (ptr->root_x > max_x)
374 else if (ptr->root_x > min_x)
375 x += ptr->root_x - min_x;
377 assert (max_y >= min_y);
378 if (ptr->root_y > max_y)
380 else if (ptr->root_y > min_y)
381 y += ptr->root_y - min_y;
385 xcb_get_image_reply_t *img;
386 img = xcb_get_image_reply (conn,
387 xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, root,
388 x, y, w, h, ~0), NULL);
392 /* Send block - zero copy */
393 block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
394 xcb_get_image_data_length (img));
398 vlc_mutex_lock (&p_sys->lock);
399 if (w != p_sys->cur_w || h != p_sys->cur_h)
401 if (p_sys->es != NULL)
402 es_out_Del (demux->out, p_sys->es);
403 p_sys->es = InitES (demux, w, h, depth);
404 if (p_sys->es != NULL)
412 if (p_sys->es != NULL)
414 if (p_sys->pts == VLC_TS_INVALID)
415 p_sys->pts = mdate ();
416 block->i_pts = block->i_dts = p_sys->pts;
418 es_out_Control (demux->out, ES_OUT_SET_PCR, p_sys->pts);
419 es_out_Send (demux->out, p_sys->es, block);
420 p_sys->pts += p_sys->interval;
422 vlc_mutex_unlock (&p_sys->lock);
425 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
426 uint_fast16_t height, uint_fast8_t depth)
428 demux_sys_t *p_sys = demux->p_sys;
429 const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
433 for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
434 *end = fmt + xcb_setup_pixmap_formats_length (setup);
437 if (fmt->depth != depth)
443 if (fmt->bits_per_pixel == 32)
444 chroma = VLC_CODEC_RGBA;
447 if (fmt->bits_per_pixel == 32)
449 chroma = VLC_CODEC_RGB32;
452 else if (fmt->bits_per_pixel == 24)
453 chroma = VLC_CODEC_RGB24;
456 if (fmt->bits_per_pixel == 16)
457 chroma = VLC_CODEC_RGB16;
460 if (fmt->bits_per_pixel == 16)
461 chroma = VLC_CODEC_RGB15;
463 case 8: /* XXX: screw grey scale! */
464 if (fmt->bits_per_pixel == 8)
465 chroma = VLC_CODEC_RGB8;
474 msg_Err (demux, "unsupported pixmap formats");
480 es_format_Init (&fmt, VIDEO_ES, chroma);
481 fmt.video.i_chroma = chroma;
482 fmt.video.i_bits_per_pixel = bpp;
483 fmt.video.i_sar_num = fmt.video.i_sar_den = 1;
484 fmt.video.i_frame_rate = 1000 * p_sys->rate;
485 fmt.video.i_frame_rate_base = 1000;
486 fmt.video.i_visible_width = fmt.video.i_width = width;
487 fmt.video.i_visible_height = fmt.video.i_height = height;
489 return es_out_Add (demux->out, &fmt);