]> git.sesse.net Git - vlc/blob - modules/access/screen/xcb.c
Fix #include <SDL/SDL.h> to be #include <SDL.h>
[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")
95     add_shortcut ("window")
96 vlc_module_end ()
97
98 /*
99  * Local prototypes
100  */
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,
104                             uint_fast8_t);
105
106 struct demux_sys_t
107 {
108     xcb_connection_t *conn;
109     es_out_id_t      *es;
110     mtime_t           pts, interval;
111     float             rate;
112     xcb_window_t      window;
113     int16_t           x, y;
114     uint16_t          w, h;
115     uint16_t          cur_w, cur_h;
116     bool              follow_mouse;
117     /* fmt, es and pts are protected by the lock. The rest is read-only. */
118     vlc_mutex_t       lock;
119     /* Timer does not use this, only input thread: */
120     vlc_timer_t       timer;
121 };
122
123 /**
124  * Probes and initializes.
125  */
126 static int Open (vlc_object_t *obj)
127 {
128     demux_t *demux = (demux_t *)obj;
129     demux_sys_t *p_sys = malloc (sizeof (*p_sys));
130
131     if (p_sys == NULL)
132         return VLC_ENOMEM;
133     demux->p_sys = p_sys;
134
135     /* Connect to X server */
136     char *display = var_CreateGetNonEmptyString (obj, "x11-display");
137     int snum;
138     xcb_connection_t *conn = xcb_connect (display, &snum);
139     free (display);
140     if (xcb_connection_has_error (conn))
141     {
142         free (p_sys);
143         return VLC_EGENERIC;
144     }
145     p_sys->conn = conn;
146
147    /* Find configured screen */
148     if (!strcmp (demux->psz_access, "screen"))
149     {
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))
154         {
155             if (snum == 0)
156             {
157                scr = i.data;
158                 break;
159             }
160             snum--;
161         }
162         if (scr == NULL)
163         {
164             msg_Err (obj, "bad X11 screen number");
165             goto error;
166         }
167         p_sys->window = scr->root;
168     }
169     else
170     /* Determine capture window */
171     if (!strcmp (demux->psz_access, "window"))
172     {
173         char *end;
174         unsigned long ul = strtoul (demux->psz_path, &end, 0);
175         if (*end || ul > 0xffffffff)
176         {
177             msg_Err (obj, "bad X11 drawable %s", demux->psz_path);
178             goto error;
179         }
180         p_sys->window = ul;
181     }
182     else
183         goto error;
184
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");
191
192     /* Initializes format */
193     p_sys->rate = var_CreateGetFloat (obj, "screen-fps");
194     if (!p_sys->rate)
195         goto error;
196     p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
197     if (!p_sys->interval)
198         goto error;
199     var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
200
201     p_sys->cur_w = 0;
202     p_sys->cur_h = 0;
203     p_sys->es = NULL;
204     p_sys->pts = VLC_TS_INVALID;
205     vlc_mutex_init (&p_sys->lock);
206     if (vlc_timer_create (&p_sys->timer, Demux, demux))
207         goto error;
208     vlc_timer_schedule (p_sys->timer, false, 1, p_sys->interval);
209
210     /* Initializes demux */
211     demux->pf_demux   = NULL;
212     demux->pf_control = Control;
213     return VLC_SUCCESS;
214
215 error:
216     xcb_disconnect (p_sys->conn);
217     free (p_sys);
218     return VLC_EGENERIC;
219 }
220
221
222 /**
223  * Releases resources
224  */
225 static void Close (vlc_object_t *obj)
226 {
227     demux_t *demux = (demux_t *)obj;
228     demux_sys_t *p_sys = demux->p_sys;
229
230     vlc_timer_destroy (p_sys->timer);
231     vlc_mutex_destroy (&p_sys->lock);
232     xcb_disconnect (p_sys->conn);
233     free (p_sys);
234 }
235
236
237 /**
238  * Control callback
239  */
240 static int Control (demux_t *demux, int query, va_list args)
241 {
242     demux_sys_t *p_sys = demux->p_sys;
243
244     switch (query)
245     {
246         case DEMUX_GET_POSITION:
247         {
248             float *v = va_arg (args, float *);
249             *v = 0.;
250             return VLC_SUCCESS;
251         }
252
253         case DEMUX_GET_LENGTH:
254         case DEMUX_GET_TIME:
255         {
256             int64_t *v = va_arg (args, int64_t *);
257             *v = 0;
258             return VLC_SUCCESS;
259         }
260
261         /* TODO: get title info -> crawl visible windows */
262
263         case DEMUX_GET_PTS_DELAY:
264         {
265             int64_t *v = va_arg (args, int64_t *);
266             *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
267             return VLC_SUCCESS;
268         }
269
270         case DEMUX_CAN_PAUSE:
271         {
272             bool *v = (bool*)va_arg( args, bool * );
273             *v = true;
274             return VLC_SUCCESS;
275         }
276
277         case DEMUX_SET_PAUSE_STATE:
278         {
279             bool pausing = va_arg (args, int);
280
281             if (!pausing)
282             {
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);
287             }
288             vlc_timer_schedule (p_sys->timer, false,
289                                 pausing ? 0 : 1, p_sys->interval);
290             return VLC_SUCCESS;
291         }
292
293         case DEMUX_CAN_CONTROL_PACE:
294         case DEMUX_CAN_CONTROL_RATE:
295         case DEMUX_CAN_SEEK:
296         {
297             bool *v = (bool*)va_arg( args, bool * );
298             *v = false;
299             return VLC_SUCCESS;
300         }
301     }
302
303     return VLC_EGENERIC;
304 }
305
306
307 /**
308  * Processing callback
309  */
310 static void Demux (void *data)
311 {
312     demux_t *demux = data;
313     demux_sys_t *p_sys = demux->p_sys;
314     xcb_connection_t *conn = p_sys->conn;
315
316     /* Update capture region (if needed) */
317
318     xcb_get_geometry_reply_t *geo =
319         xcb_get_geometry_reply (conn,
320             xcb_get_geometry (conn, p_sys->window), NULL);
321     if (geo == NULL)
322     {
323         msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, p_sys->window);
324         return;
325     }
326
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;
331
332     if (p_sys->window != root)
333         tc = xcb_translate_coordinates (conn, p_sys->window, root,
334                                         x, y);
335     if (p_sys->follow_mouse)
336         qc = xcb_query_pointer (conn, p_sys->window);
337
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)
343         w = ow;
344     if (h == 0 || h > oh)
345         h = oh;
346     uint8_t depth = geo->depth;
347     free (geo);
348
349     if (p_sys->window != root)
350     {
351         xcb_translate_coordinates_reply_t *coords =
352              xcb_translate_coordinates_reply (conn, tc, NULL);
353         if (coords != NULL)
354         {
355             x = coords->dst_x;
356             y = coords->dst_y;
357             free (coords);
358         }
359     }
360
361     if (p_sys->follow_mouse)
362     {
363         xcb_query_pointer_reply_t *ptr =
364             xcb_query_pointer_reply (conn, qc, NULL);
365         if (ptr != NULL)
366         {
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);
371
372             assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
373             if (ptr->root_x > max_x)
374                 x += ow - w;
375             else if (ptr->root_x > min_x)
376                 x += ptr->root_x - min_x;
377
378             assert (max_y >= min_y);
379             if (ptr->root_y > max_y)
380                 y += oh - h;
381             else if (ptr->root_y > min_y)
382                 y += ptr->root_y - min_y;
383         }
384     }
385
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     /* Send block - zero copy */
394     block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
395                                        xcb_get_image_data_length (img));
396     if (block == NULL)
397         return;
398
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     /* Capture screen */
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 }