]> git.sesse.net Git - vlc/blob - modules/access/screen/xcb.c
Update LGPL license blurb, choosing v2.1+.
[vlc] / modules / access / screen / xcb.c
1 /**
2  * @file xcb.c
3  * @brief X11 C Bindings screen capture demux module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <xcb/xcb.h>
29 #include <vlc_common.h>
30 #include <vlc_demux.h>
31 #include <vlc_plugin.h>
32
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.")
37
38 #define FPS_TEXT N_("Frame rate")
39 #define FPS_LONGTEXT N_( \
40     "How many times the screen content should be refreshed per second.")
41
42 #define LEFT_TEXT N_("Region left column")
43 #define LEFT_LONGTEXT N_( \
44     "Abscissa of the capture reion in pixels.")
45
46 #define TOP_TEXT N_("Region top row")
47 #define TOP_LONGTEXT N_( \
48     "Ordinate of the capture region in pixels.")
49
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")
53
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")
57
58 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
59 #define FOLLOW_MOUSE_LONGTEXT N_( \
60     "Follow the mouse when capturing a subscreen." )
61
62 static int  Open (vlc_object_t *);
63 static void Close (vlc_object_t *);
64
65 /*
66  * Module descriptor
67  */
68 vlc_module_begin ()
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)
75
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)
81         change_safe ()
82     add_integer ("screen-top", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
83         change_integer_range (-32768, 32767)
84         change_safe ()
85     add_integer ("screen-width", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
86         change_integer_range (0, 65535)
87         change_safe ()
88     add_integer ("screen-height", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true)
89         change_integer_range (0, 65535)
90         change_safe ()
91     add_bool ("screen-follow-mouse", false, NULL, FOLLOW_MOUSE_TEXT,
92               FOLLOW_MOUSE_LONGTEXT, true)
93
94     add_shortcut ("screen", "window")
95 vlc_module_end ()
96
97 /*
98  * Local prototypes
99  */
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,
103                             uint_fast8_t);
104
105 struct demux_sys_t
106 {
107     xcb_connection_t *conn;
108     es_out_id_t      *es;
109     mtime_t           pts, interval;
110     float             rate;
111     xcb_window_t      window;
112     int16_t           x, y;
113     uint16_t          w, h;
114     uint16_t          cur_w, cur_h;
115     bool              follow_mouse;
116     /* fmt, es and pts are protected by the lock. The rest is read-only. */
117     vlc_mutex_t       lock;
118     /* Timer does not use this, only input thread: */
119     vlc_timer_t       timer;
120 };
121
122 /**
123  * Probes and initializes.
124  */
125 static int Open (vlc_object_t *obj)
126 {
127     demux_t *demux = (demux_t *)obj;
128     demux_sys_t *p_sys = malloc (sizeof (*p_sys));
129
130     if (p_sys == NULL)
131         return VLC_ENOMEM;
132     demux->p_sys = p_sys;
133
134     /* Connect to X server */
135     char *display = var_InheritString (obj, "x11-display");
136     int snum;
137     xcb_connection_t *conn = xcb_connect (display, &snum);
138     free (display);
139     if (xcb_connection_has_error (conn))
140     {
141         free (p_sys);
142         return VLC_EGENERIC;
143     }
144     p_sys->conn = conn;
145
146    /* Find configured screen */
147     if (!strcmp (demux->psz_access, "screen"))
148     {
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))
153         {
154             if (snum == 0)
155             {
156                scr = i.data;
157                 break;
158             }
159             snum--;
160         }
161         if (scr == NULL)
162         {
163             msg_Err (obj, "bad X11 screen number");
164             goto error;
165         }
166         p_sys->window = scr->root;
167     }
168     else
169     /* Determine capture window */
170     if (!strcmp (demux->psz_access, "window"))
171     {
172         char *end;
173         unsigned long ul = strtoul (demux->psz_location, &end, 0);
174         if (*end || ul > 0xffffffff)
175         {
176             msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
177             goto error;
178         }
179         p_sys->window = ul;
180     }
181     else
182         goto error;
183
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");
190
191     /* Initializes format */
192     p_sys->rate = var_InheritFloat (obj, "screen-fps");
193     if (!p_sys->rate)
194         goto error;
195     p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
196     if (!p_sys->interval)
197         goto error;
198     var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
199
200     p_sys->cur_w = 0;
201     p_sys->cur_h = 0;
202     p_sys->es = NULL;
203     p_sys->pts = VLC_TS_INVALID;
204     vlc_mutex_init (&p_sys->lock);
205     if (vlc_timer_create (&p_sys->timer, Demux, demux))
206         goto error;
207     vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
208
209     /* Initializes demux */
210     demux->pf_demux   = NULL;
211     demux->pf_control = Control;
212     return VLC_SUCCESS;
213
214 error:
215     xcb_disconnect (p_sys->conn);
216     free (p_sys);
217     return VLC_EGENERIC;
218 }
219
220
221 /**
222  * Releases resources
223  */
224 static void Close (vlc_object_t *obj)
225 {
226     demux_t *demux = (demux_t *)obj;
227     demux_sys_t *p_sys = demux->p_sys;
228
229     vlc_timer_destroy (p_sys->timer);
230     vlc_mutex_destroy (&p_sys->lock);
231     xcb_disconnect (p_sys->conn);
232     free (p_sys);
233 }
234
235
236 /**
237  * Control callback
238  */
239 static int Control (demux_t *demux, int query, va_list args)
240 {
241     demux_sys_t *p_sys = demux->p_sys;
242
243     switch (query)
244     {
245         case DEMUX_GET_POSITION:
246         {
247             float *v = va_arg (args, float *);
248             *v = 0.;
249             return VLC_SUCCESS;
250         }
251
252         case DEMUX_GET_LENGTH:
253         case DEMUX_GET_TIME:
254         {
255             int64_t *v = va_arg (args, int64_t *);
256             *v = 0;
257             return VLC_SUCCESS;
258         }
259
260         /* TODO: get title info -> crawl visible windows */
261
262         case DEMUX_GET_PTS_DELAY:
263         {
264             int64_t *v = va_arg (args, int64_t *);
265             *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
266             return VLC_SUCCESS;
267         }
268
269         case DEMUX_CAN_PAUSE:
270         {
271             bool *v = (bool*)va_arg( args, bool * );
272             *v = true;
273             return VLC_SUCCESS;
274         }
275
276         case DEMUX_SET_PAUSE_STATE:
277         {
278             bool pausing = va_arg (args, int);
279
280             if (!pausing)
281             {
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);
286             }
287             vlc_timer_schedule (p_sys->timer, false,
288                                 pausing ? 0 : 1, p_sys->interval);
289             return VLC_SUCCESS;
290         }
291
292         case DEMUX_CAN_CONTROL_PACE:
293         case DEMUX_CAN_CONTROL_RATE:
294         case DEMUX_CAN_SEEK:
295         {
296             bool *v = (bool*)va_arg( args, bool * );
297             *v = false;
298             return VLC_SUCCESS;
299         }
300     }
301
302     return VLC_EGENERIC;
303 }
304
305
306 /**
307  * Processing callback
308  */
309 static void Demux (void *data)
310 {
311     demux_t *demux = data;
312     demux_sys_t *p_sys = demux->p_sys;
313     xcb_connection_t *conn = p_sys->conn;
314
315     /* Update capture region (if needed) */
316
317     xcb_get_geometry_reply_t *geo =
318         xcb_get_geometry_reply (conn,
319             xcb_get_geometry (conn, p_sys->window), NULL);
320     if (geo == NULL)
321     {
322         msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, p_sys->window);
323         return;
324     }
325
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;
330
331     if (p_sys->window != root)
332         tc = xcb_translate_coordinates (conn, p_sys->window, root,
333                                         x, y);
334     if (p_sys->follow_mouse)
335         qc = xcb_query_pointer (conn, p_sys->window);
336
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)
342         w = ow;
343     if (h == 0 || h > oh)
344         h = oh;
345     uint8_t depth = geo->depth;
346     free (geo);
347
348     if (p_sys->window != root)
349     {
350         xcb_translate_coordinates_reply_t *coords =
351              xcb_translate_coordinates_reply (conn, tc, NULL);
352         if (coords != NULL)
353         {
354             x = coords->dst_x;
355             y = coords->dst_y;
356             free (coords);
357         }
358     }
359
360     if (p_sys->follow_mouse)
361     {
362         xcb_query_pointer_reply_t *ptr =
363             xcb_query_pointer_reply (conn, qc, NULL);
364         if (ptr != NULL)
365         {
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);
370
371             assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
372             if (ptr->root_x > max_x)
373                 x += ow - w;
374             else if (ptr->root_x > min_x)
375                 x += ptr->root_x - min_x;
376
377             assert (max_y >= min_y);
378             if (ptr->root_y > max_y)
379                 y += oh - h;
380             else if (ptr->root_y > min_y)
381                 y += ptr->root_y - min_y;
382         }
383     }
384
385     /* Capture screen */
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);
390     if (img == NULL)
391         return;
392
393     block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
394                                        xcb_get_image_data_length (img));
395     if (block == NULL)
396         return;
397
398     /* Update elementary stream format (if needed) */
399     vlc_mutex_lock (&p_sys->lock);
400     if (w != p_sys->cur_w || h != p_sys->cur_h)
401     {
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)
406         {
407             p_sys->cur_w = w;
408             p_sys->cur_h = h;
409         }
410     }
411
412     /* Send block - zero copy */
413     if (p_sys->es != NULL)
414     {
415         if (p_sys->pts == VLC_TS_INVALID)
416             p_sys->pts = mdate ();
417         block->i_pts = block->i_dts = p_sys->pts;
418
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;
422     }
423     vlc_mutex_unlock (&p_sys->lock);
424 }
425
426 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
427                             uint_fast16_t height, uint_fast8_t depth)
428 {
429     demux_sys_t *p_sys = demux->p_sys;
430     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
431     uint32_t chroma = 0;
432     uint8_t bpp;
433
434     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
435              *end = fmt + xcb_setup_pixmap_formats_length (setup);
436          fmt < end; fmt++)
437     {
438         if (fmt->depth != depth)
439             continue;
440         bpp = fmt->depth;
441         switch (fmt->depth)
442         {
443             case 32:
444                 if (fmt->bits_per_pixel == 32)
445                     chroma = VLC_CODEC_RGBA;
446                 break;
447             case 24:
448                 if (fmt->bits_per_pixel == 32)
449                 {
450                     chroma = VLC_CODEC_RGB32;
451                     bpp = 32;
452                 }
453                 else if (fmt->bits_per_pixel == 24)
454                     chroma = VLC_CODEC_RGB24;
455                 break;
456             case 16:
457                 if (fmt->bits_per_pixel == 16)
458                     chroma = VLC_CODEC_RGB16;
459                 break;
460             case 15:
461                 if (fmt->bits_per_pixel == 16)
462                     chroma = VLC_CODEC_RGB15;
463                 break;
464             case 8: /* XXX: screw grey scale! */
465                 if (fmt->bits_per_pixel == 8)
466                     chroma = VLC_CODEC_RGB8;
467                 break;
468         }
469         if (chroma != 0)
470             break;
471     }
472
473     if (!chroma)
474     {
475         msg_Err (demux, "unsupported pixmap formats");
476         return NULL;
477     }
478
479     es_format_t fmt;
480
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;
489
490     return es_out_Add (demux->out, &fmt);
491 }