]> git.sesse.net Git - vlc/blob - modules/access/screen/xcb.c
Typo
[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 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.
12  *
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.
17  *
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  *****************************************************************************/
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 <xcb/composite.h>
30 #include <vlc_common.h>
31 #include <vlc_demux.h>
32 #include <vlc_plugin.h>
33
34 #define FPS_TEXT N_("Frame rate")
35 #define FPS_LONGTEXT N_( \
36     "How many times the screen content should be refreshed per second.")
37
38 #define LEFT_TEXT N_("Region left column")
39 #define LEFT_LONGTEXT N_( \
40     "Abscissa of the capture region in pixels.")
41
42 #define TOP_TEXT N_("Region top row")
43 #define TOP_LONGTEXT N_( \
44     "Ordinate of the capture region in pixels.")
45
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")
49
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")
53
54 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
55 #define FOLLOW_MOUSE_LONGTEXT N_( \
56     "Follow the mouse when capturing a subscreen." )
57
58 static int  Open (vlc_object_t *);
59 static void Close (vlc_object_t *);
60
61 /*
62  * Module descriptor
63  */
64 vlc_module_begin ()
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)
71
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)
75         change_safe ()
76     add_integer ("screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true)
77         change_integer_range (-32768, 32767)
78         change_safe ()
79     add_integer ("screen-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, true)
80         change_integer_range (0, 65535)
81         change_safe ()
82     add_integer ("screen-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, true)
83         change_integer_range (0, 65535)
84         change_safe ()
85     add_bool ("screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
86               FOLLOW_MOUSE_LONGTEXT, true)
87
88     add_shortcut ("screen", "window")
89 vlc_module_end ()
90
91 /*
92  * Local prototypes
93  */
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,
97                             uint_fast8_t);
98
99 struct demux_sys_t
100 {
101     /* All owned by timer thread while timer is armed: */
102     xcb_connection_t *conn;
103     es_out_id_t      *es;
104     mtime_t           pts, interval;
105     float             rate;
106     xcb_window_t      window;
107     xcb_pixmap_t      pixmap;
108     int16_t           x, y;
109     uint16_t          w, h;
110     uint16_t          cur_w, cur_h;
111     bool              follow_mouse;
112     /* Timer does not use this, only input thread: */
113     vlc_timer_t       timer;
114 };
115
116 /**
117  * Probes and initializes.
118  */
119 static int Open (vlc_object_t *obj)
120 {
121     demux_t *demux = (demux_t *)obj;
122     demux_sys_t *p_sys = malloc (sizeof (*p_sys));
123
124     if (p_sys == NULL)
125         return VLC_ENOMEM;
126     demux->p_sys = p_sys;
127
128     /* Connect to X server */
129     char *display = var_InheritString (obj, "x11-display");
130     int snum;
131     xcb_connection_t *conn = xcb_connect (display, &snum);
132     free (display);
133     if (xcb_connection_has_error (conn))
134     {
135         free (p_sys);
136         return VLC_EGENERIC;
137     }
138     p_sys->conn = conn;
139
140    /* Find configured screen */
141     if (!strcmp (demux->psz_access, "screen"))
142     {
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))
147         {
148             if (snum == 0)
149             {
150                scr = i.data;
151                 break;
152             }
153             snum--;
154         }
155         if (scr == NULL)
156         {
157             msg_Err (obj, "bad X11 screen number");
158             goto error;
159         }
160         p_sys->window = scr->root;
161     }
162     else
163     /* Determine capture window */
164     if (!strcmp (demux->psz_access, "window"))
165     {
166         char *end;
167         unsigned long ul = strtoul (demux->psz_location, &end, 0);
168         if (*end || ul > 0xffffffff)
169         {
170             msg_Err (obj, "bad X11 drawable %s", demux->psz_location);
171             goto error;
172         }
173         p_sys->window = ul;
174
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)
179         {
180             msg_Err (obj, "X Composite extension not available");
181             free (r);
182             goto error;
183         }
184         msg_Dbg (obj, "using Composite extension v%"PRIu32".%"PRIu32,
185                  r->major_version, r->minor_version);
186         free (r);
187
188         xcb_composite_redirect_window (conn, p_sys->window,
189                                        XCB_COMPOSITE_REDIRECT_AUTOMATIC);
190     }
191     else
192         goto error;
193
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 */
203     {
204         p_sys->x = var_InheritInteger (obj, "screen-left");
205         p_sys->y = var_InheritInteger (obj, "screen-top");
206     }
207
208     /* Initializes format */
209     p_sys->rate = var_InheritFloat (obj, "screen-fps");
210     if (!p_sys->rate)
211         goto error;
212     p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
213     if (!p_sys->interval)
214         goto error;
215
216     p_sys->cur_w = 0;
217     p_sys->cur_h = 0;
218     p_sys->es = NULL;
219     p_sys->pts = VLC_TS_INVALID;
220     if (vlc_timer_create (&p_sys->timer, Demux, demux))
221         goto error;
222     vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
223
224     /* Initializes demux */
225     demux->pf_demux   = NULL;
226     demux->pf_control = Control;
227     return VLC_SUCCESS;
228
229 error:
230     xcb_disconnect (p_sys->conn);
231     free (p_sys);
232     return VLC_EGENERIC;
233 }
234
235
236 /**
237  * Releases resources
238  */
239 static void Close (vlc_object_t *obj)
240 {
241     demux_t *demux = (demux_t *)obj;
242     demux_sys_t *p_sys = demux->p_sys;
243
244     vlc_timer_destroy (p_sys->timer);
245     xcb_disconnect (p_sys->conn);
246     free (p_sys);
247 }
248
249
250 /**
251  * Control callback
252  */
253 static int Control (demux_t *demux, int query, va_list args)
254 {
255     switch (query)
256     {
257         case DEMUX_GET_POSITION:
258         {
259             float *v = va_arg (args, float *);
260             *v = 0.;
261             return VLC_SUCCESS;
262         }
263
264         case DEMUX_GET_LENGTH:
265         case DEMUX_GET_TIME:
266         {
267             int64_t *v = va_arg (args, int64_t *);
268             *v = 0;
269             return VLC_SUCCESS;
270         }
271
272         /* TODO: get title info -> crawl visible windows */
273
274         case DEMUX_GET_PTS_DELAY:
275         {
276             int64_t *v = va_arg (args, int64_t *);
277             *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
278             return VLC_SUCCESS;
279         }
280
281         case DEMUX_CAN_PAUSE:
282         case DEMUX_CAN_CONTROL_PACE:
283         case DEMUX_CAN_CONTROL_RATE:
284         case DEMUX_CAN_SEEK:
285         {
286             bool *v = (bool*)va_arg( args, bool * );
287             *v = false;
288             return VLC_SUCCESS;
289         }
290
291         case DEMUX_SET_PAUSE_STATE:
292             return VLC_SUCCESS; /* should not happen */
293     }
294
295     return VLC_EGENERIC;
296 }
297
298
299 /**
300  * Processing callback
301  */
302 static void Demux (void *data)
303 {
304     demux_t *demux = data;
305     demux_sys_t *sys = demux->p_sys;
306     xcb_connection_t *conn = sys->conn;
307
308     /* Determine capture region */
309     xcb_get_geometry_cookie_t gc;
310     xcb_query_pointer_cookie_t qc;
311
312     gc = xcb_get_geometry (conn, sys->window);
313     if (sys->follow_mouse)
314         qc = xcb_query_pointer (conn, sys->window);
315
316     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, gc, NULL);
317     if (geo == NULL)
318     {
319         msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, sys->window);
320 discard:
321         if (sys->follow_mouse)
322             xcb_discard_reply (conn, gc.sequence);
323         return;
324     }
325
326     int w = sys->w;
327     int h = sys->h;
328     int x, y;
329
330     if (sys->follow_mouse)
331     {
332         xcb_query_pointer_reply_t *ptr =
333             xcb_query_pointer_reply (conn, qc, NULL);
334         if (ptr == NULL)
335         {
336             free (geo);
337             return;
338         }
339
340         if (w == 0 || w > geo->width)
341             w = geo->width;
342         x = ptr->win_x;
343         if (x < w / 2)
344             x = 0;
345         else if (x >= (int)geo->width - (w / 2))
346             x = geo->width - w;
347         else
348             x -= w / 2;
349
350         if (h == 0 || h > geo->height)
351             h = geo->height;
352         y = ptr->win_y;
353         if (y < h / 2)
354             y = 0;
355         else if (y >= (int)geo->height - (h / 2))
356             y = geo->height - h;
357         else
358             y -= h / 2;
359     }
360     else
361     {
362         int max;
363
364         x = sys->x;
365         max = (int)geo->width - x;
366         if (max <= 0)
367             goto discard;
368         if (w == 0 || w > max)
369             w = max;
370
371         y = sys->y;
372         max = (int)geo->height - y;
373         if (max <= 0)
374             goto discard;
375         if (h == 0 || h > max)
376             h = max;
377     }
378
379     /* Update elementary stream format (if needed) */
380     if (w != sys->cur_w || h != sys->cur_h)
381     {
382         if (sys->es != NULL)
383             es_out_Del (demux->out, sys->es);
384
385         /* Update composite pixmap */
386         if (sys->window != geo->root)
387         {
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);
392         }
393
394         sys->es = InitES (demux, w, h, geo->depth);
395         if (sys->es != NULL)
396         {
397             sys->cur_w = w;
398             sys->cur_h = h;
399         }
400     }
401
402     /* Capture screen */
403     xcb_drawable_t drawable =
404         (sys->window != geo->root) ? sys->pixmap : sys->window;
405     free (geo);
406
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);
411     if (img == NULL)
412         return;
413
414     block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
415                                        xcb_get_image_data_length (img));
416     if (block == NULL)
417         return;
418
419     /* Send block - zero copy */
420     if (sys->es != NULL)
421     {
422         if (sys->pts == VLC_TS_INVALID)
423             sys->pts = mdate ();
424         block->i_pts = block->i_dts = sys->pts;
425
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;
429     }
430 }
431
432 static es_out_id_t *InitES (demux_t *demux, uint_fast16_t width,
433                             uint_fast16_t height, uint_fast8_t depth)
434 {
435     demux_sys_t *p_sys = demux->p_sys;
436     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
437     uint32_t chroma = 0;
438     uint8_t bpp;
439
440     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
441              *end = fmt + xcb_setup_pixmap_formats_length (setup);
442          fmt < end; fmt++)
443     {
444         if (fmt->depth != depth)
445             continue;
446         bpp = fmt->depth;
447         switch (fmt->depth)
448         {
449             case 32:
450                 if (fmt->bits_per_pixel == 32)
451                     chroma = VLC_CODEC_RGBA;
452                 break;
453             case 24:
454                 if (fmt->bits_per_pixel == 32)
455                 {
456                     chroma = VLC_CODEC_RGB32;
457                     bpp = 32;
458                 }
459                 else if (fmt->bits_per_pixel == 24)
460                     chroma = VLC_CODEC_RGB24;
461                 break;
462             case 16:
463                 if (fmt->bits_per_pixel == 16)
464                     chroma = VLC_CODEC_RGB16;
465                 break;
466             case 15:
467                 if (fmt->bits_per_pixel == 16)
468                     chroma = VLC_CODEC_RGB15;
469                 break;
470             case 8: /* XXX: screw grey scale! */
471                 if (fmt->bits_per_pixel == 8)
472                     chroma = VLC_CODEC_RGB8;
473                 break;
474         }
475         if (chroma != 0)
476             break;
477     }
478
479     if (!chroma)
480     {
481         msg_Err (demux, "unsupported pixmap formats");
482         return NULL;
483     }
484
485     es_format_t fmt;
486
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;
495
496     return es_out_Add (demux->out, &fmt);
497 }