FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xcbgrab.c
Go to the documentation of this file.
1 /*
2  * XCB input grabber
3  * Copyright (C) 2014 Luca Barbato <lu_zero@gentoo.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 
24 #include <stdlib.h>
25 #include <xcb/xcb.h>
26 
27 #if CONFIG_LIBXCB_XFIXES
28 #include <xcb/xfixes.h>
29 #endif
30 
31 #if CONFIG_LIBXCB_SHM
32 #include <sys/shm.h>
33 #include <xcb/shm.h>
34 #endif
35 
36 #if CONFIG_LIBXCB_SHAPE
37 #include <xcb/shape.h>
38 #endif
39 
40 #include "libavformat/avformat.h"
41 #include "libavformat/internal.h"
42 
43 #include "libavutil/mathematics.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/parseutils.h"
46 #include "libavutil/time.h"
47 
48 typedef struct XCBGrabContext {
49  const AVClass *class;
50 
51  xcb_connection_t *conn;
52  xcb_screen_t *screen;
53  xcb_window_t window;
54 #if CONFIG_LIBXCB_SHM
55  xcb_shm_seg_t segment;
56 #endif
57  int64_t time_frame;
59 
60  int x, y;
61  int width, height;
63  int bpp;
64 
69  int centered;
70 
71  const char *video_size;
72  const char *framerate;
73 
74  int has_shm;
76 
77 #define FOLLOW_CENTER -1
78 
79 #define OFFSET(x) offsetof(XCBGrabContext, x)
80 #define D AV_OPT_FLAG_DECODING_PARAM
81 static const AVOption options[] = {
82  { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
83  { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
84  { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
85  { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
86  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
87  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
88  { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
89  { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
90  OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
91  { "centered", "Keep the mouse pointer at the center of grabbing region when following.", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, D, "follow_mouse" },
92  { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
93  { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
94  { NULL },
95 };
96 
97 static const AVClass xcbgrab_class = {
98  .class_name = "xcbgrab indev",
99  .item_name = av_default_item_name,
100  .option = options,
101  .version = LIBAVUTIL_VERSION_INT,
103 };
104 
106  xcb_query_pointer_reply_t *p,
107  xcb_get_geometry_reply_t *geo)
108 {
109  XCBGrabContext *c = s->priv_data;
110  int x = c->x, y = c->y;
111  int w = c->width, h = c->height, f = c->follow_mouse;
112  int p_x, p_y;
113 
114  if (!p || !geo)
115  return AVERROR(EIO);
116 
117  p_x = p->win_x;
118  p_y = p->win_y;
119 
120  if (f == FOLLOW_CENTER) {
121  x = p_x - w / 2;
122  y = p_y - h / 2;
123  } else {
124  int left = x + f;
125  int right = x + w - f;
126  int top = y + f;
127  int bottom = y + h + f;
128  if (p_x > right) {
129  x += p_x - right;
130  } else if (p_x < left) {
131  x -= left - p_x;
132  }
133  if (p_y > bottom) {
134  y += p_y - bottom;
135  } else if (p_y < top) {
136  y -= top - p_y;
137  }
138  }
139 
140  c->x = FFMIN(FFMAX(0, x), geo->width - w);
141  c->y = FFMIN(FFMAX(0, y), geo->height - h);
142 
143  return 0;
144 }
145 
147 {
148  XCBGrabContext *c = s->priv_data;
149  xcb_get_image_cookie_t iq;
150  xcb_get_image_reply_t *img;
151  xcb_drawable_t drawable = c->screen->root;
152  uint8_t *data;
153  int length, ret;
154 
155  iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
156  c->x, c->y, c->width, c->height, ~0);
157 
158  img = xcb_get_image_reply(c->conn, iq, NULL);
159  if (!img)
160  return AVERROR(EAGAIN);
161 
162  data = xcb_get_image_data(img);
163  length = xcb_get_image_data_length(img);
164 
165  ret = av_new_packet(pkt, length);
166 
167  if (!ret)
168  memcpy(pkt->data, data, length);
169 
170  free(img);
171 
172  return ret;
173 }
174 
176 {
177  XCBGrabContext *c = s->priv_data;
178  int64_t curtime, delay;
179  int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
180 
181  c->time_frame += frame_time;
182 
183  for (;;) {
184  curtime = av_gettime();
185  delay = c->time_frame - curtime;
186  if (delay <= 0)
187  break;
188  av_usleep(delay);
189  }
190 
191  pkt->pts = curtime;
192 }
193 
194 #if CONFIG_LIBXCB_SHM
195 static int check_shm(xcb_connection_t *conn)
196 {
197  xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
198  xcb_shm_query_version_reply_t *reply;
199 
200  reply = xcb_shm_query_version_reply(conn, cookie, NULL);
201  if (reply) {
202  free(reply);
203  return 1;
204  }
205 
206  return 0;
207 }
208 
209 static void dealloc_shm(void *unused, uint8_t *data)
210 {
211  shmdt(data);
212 }
213 
214 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
215 {
216  XCBGrabContext *c = s->priv_data;
217  xcb_shm_get_image_cookie_t iq;
218  xcb_shm_get_image_reply_t *img;
219  xcb_drawable_t drawable = c->screen->root;
220  uint8_t *data;
222  int id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
223  xcb_generic_error_t *e = NULL;
224 
225  if (id == -1) {
226  char errbuf[1024];
227  int err = AVERROR(errno);
228  av_strerror(err, errbuf, sizeof(errbuf));
229  av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
230  size, errbuf);
231  return err;
232  }
233 
234  xcb_shm_attach(c->conn, c->segment, id, 0);
235 
236  iq = xcb_shm_get_image(c->conn, drawable,
237  c->x, c->y, c->width, c->height, ~0,
238  XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
239 
240  xcb_shm_detach(c->conn, c->segment);
241 
242  img = xcb_shm_get_image_reply(c->conn, iq, &e);
243 
244  xcb_flush(c->conn);
245 
246  if (e) {
247  av_log(s, AV_LOG_ERROR,
248  "Cannot get the image data "
249  "event_error: response_type:%u error_code:%u "
250  "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
251  e->response_type, e->error_code,
252  e->sequence, e->resource_id, e->minor_code, e->major_code);
253 
254  shmctl(id, IPC_RMID, 0);
255  return AVERROR(EACCES);
256  }
257 
258  free(img);
259 
260  data = shmat(id, NULL, 0);
261  shmctl(id, IPC_RMID, 0);
262 
263  if ((intptr_t)data == -1)
264  return AVERROR(errno);
265 
266  pkt->buf = av_buffer_create(data, size, dealloc_shm, NULL, 0);
267  if (!pkt->buf) {
268  shmdt(data);
269  return AVERROR(ENOMEM);
270  }
271 
272  pkt->data = pkt->buf->data;
273  pkt->size = c->frame_size;
274 
275  return 0;
276 }
277 #endif /* CONFIG_LIBXCB_SHM */
278 
279 #if CONFIG_LIBXCB_XFIXES
280 static int check_xfixes(xcb_connection_t *conn)
281 {
282  xcb_xfixes_query_version_cookie_t cookie;
283  xcb_xfixes_query_version_reply_t *reply;
284 
285  cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
286  XCB_XFIXES_MINOR_VERSION);
287  reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
288 
289  if (reply) {
290  free(reply);
291  return 1;
292  }
293  return 0;
294 }
295 
296 #define BLEND(target, source, alpha) \
297  (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
298 
299 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
300  xcb_query_pointer_reply_t *p,
301  xcb_get_geometry_reply_t *geo)
302 {
303  XCBGrabContext *gr = s->priv_data;
304  uint32_t *cursor;
305  uint8_t *image = pkt->data;
306  int stride = gr->bpp / 8;
307  xcb_xfixes_get_cursor_image_cookie_t cc;
308  xcb_xfixes_get_cursor_image_reply_t *ci;
309  int cx, cy, x, y, w, h, c_off, i_off;
310 
311  cc = xcb_xfixes_get_cursor_image(gr->conn);
312  ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
313  if (!ci)
314  return;
315 
316  cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
317  if (!cursor)
318  return;
319 
320  cx = ci->x - ci->xhot;
321  cy = ci->y - ci->yhot;
322 
323  x = FFMAX(cx, gr->x);
324  y = FFMAX(cy, gr->y);
325 
326  w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
327  h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
328 
329  c_off = x - cx;
330  i_off = x - gr->x;
331 
332  cursor += (y - cy) * ci->width;
333  image += (y - gr->y) * gr->width * stride;
334 
335  for (y = 0; y < h; y++) {
336  cursor += c_off;
337  image += i_off * stride;
338  for (x = 0; x < w; x++, cursor++, image += stride) {
339  int r, g, b, a;
340 
341  r = *cursor & 0xff;
342  g = (*cursor >> 8) & 0xff;
343  b = (*cursor >> 16) & 0xff;
344  a = (*cursor >> 24) & 0xff;
345 
346  if (!a)
347  continue;
348 
349  if (a == 255) {
350  image[0] = r;
351  image[1] = g;
352  image[2] = b;
353  } else {
354  image[0] = BLEND(r, image[0], a);
355  image[1] = BLEND(g, image[1], a);
356  image[2] = BLEND(b, image[2], a);
357  }
358 
359  }
360  cursor += ci->width - w - c_off;
361  image += (gr->width - w - i_off) * stride;
362  }
363 
364  free(ci);
365 }
366 #endif /* CONFIG_LIBXCB_XFIXES */
367 
369 {
370  XCBGrabContext *c = s->priv_data;
371  const uint32_t args[] = { c->x - c->region_border,
372  c->y - c->region_border };
373 
374  xcb_configure_window(c->conn,
375  c->window,
376  XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
377  args);
378 }
379 
381 {
382  XCBGrabContext *c = s->priv_data;
383  xcb_query_pointer_cookie_t pc;
384  xcb_get_geometry_cookie_t gc;
385  xcb_query_pointer_reply_t *p = NULL;
386  xcb_get_geometry_reply_t *geo = NULL;
387  int ret = 0;
388 
389  wait_frame(s, pkt);
390 
391  if (c->follow_mouse || c->draw_mouse) {
392  pc = xcb_query_pointer(c->conn, c->screen->root);
393  gc = xcb_get_geometry(c->conn, c->screen->root);
394  p = xcb_query_pointer_reply(c->conn, pc, NULL);
395  geo = xcb_get_geometry_reply(c->conn, gc, NULL);
396  }
397 
398  if (c->follow_mouse && p->same_screen)
399  xcbgrab_reposition(s, p, geo);
400 
401  if (c->show_region)
403 
404 #if CONFIG_LIBXCB_SHM
405  if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
406  c->has_shm = 0;
407 #endif
408  if (!c->has_shm)
409  ret = xcbgrab_frame(s, pkt);
410 
411 #if CONFIG_LIBXCB_XFIXES
412  if (c->draw_mouse && p->same_screen)
413  xcbgrab_draw_mouse(s, pkt, p, geo);
414 #endif
415 
416  free(p);
417  free(geo);
418 
419  return ret;
420 }
421 
423 {
424  XCBGrabContext *ctx = s->priv_data;
425 
426  xcb_disconnect(ctx->conn);
427 
428  return 0;
429 }
430 
431 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
432 {
433  xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
434  xcb_screen_t *screen = NULL;
435 
436  for (; it.rem > 0; xcb_screen_next (&it)) {
437  if (!screen_num) {
438  screen = it.data;
439  break;
440  }
441 
442  screen_num--;
443  }
444 
445  return screen;
446 }
447 
449  int *pix_fmt)
450 {
451  XCBGrabContext *c = s->priv_data;
452  const xcb_setup_t *setup = xcb_get_setup(c->conn);
453  const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
454  int length = xcb_setup_pixmap_formats_length(setup);
455 
456  *pix_fmt = 0;
457 
458  while (length--) {
459  if (fmt->depth == depth) {
460  switch (depth) {
461  case 32:
462  if (fmt->bits_per_pixel == 32)
463  *pix_fmt = AV_PIX_FMT_0RGB;
464  break;
465  case 24:
466  if (fmt->bits_per_pixel == 32)
467  *pix_fmt = AV_PIX_FMT_0RGB32;
468  else if (fmt->bits_per_pixel == 24)
469  *pix_fmt = AV_PIX_FMT_RGB24;
470  break;
471  case 16:
472  if (fmt->bits_per_pixel == 16)
473  *pix_fmt = AV_PIX_FMT_RGB565;
474  break;
475  case 15:
476  if (fmt->bits_per_pixel == 16)
477  *pix_fmt = AV_PIX_FMT_RGB555;
478  break;
479  case 8:
480  if (fmt->bits_per_pixel == 8)
481  *pix_fmt = AV_PIX_FMT_RGB8;
482  break;
483  }
484  }
485 
486  if (*pix_fmt) {
487  c->bpp = fmt->bits_per_pixel;
488  c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
489  return 0;
490  }
491 
492  fmt++;
493  }
494  av_log(s, AV_LOG_ERROR, "Pixmap format not mappable.\n");
495 
496  return AVERROR_PATCHWELCOME;
497 }
498 
500 {
501  XCBGrabContext *c = s->priv_data;
503  xcb_get_geometry_cookie_t gc;
504  xcb_get_geometry_reply_t *geo;
505  int ret;
506 
507  if (!st)
508  return AVERROR(ENOMEM);
509 
510  ret = av_parse_video_size(&c->width, &c->height, c->video_size);
511  if (ret < 0)
512  return ret;
513 
515  if (ret < 0)
516  return ret;
517 
518  avpriv_set_pts_info(st, 64, 1, 1000000);
519 
520  gc = xcb_get_geometry(c->conn, c->screen->root);
521  geo = xcb_get_geometry_reply(c->conn, gc, NULL);
522 
523  c->width = FFMIN(geo->width, c->width);
524  c->height = FFMIN(geo->height, c->height);
526  st->avg_frame_rate.num };
527  c->time_frame = av_gettime();
528 
531  st->codec->width = c->width;
532  st->codec->height = c->height;
533  st->codec->time_base = c->time_base;
534 
535  ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codec->pix_fmt);
536 
537  free(geo);
538 
539  return ret;
540 }
541 
543 {
544  XCBGrabContext *c = s->priv_data;
545  xcb_gcontext_t gc = xcb_generate_id(c->conn);
546  uint32_t mask = XCB_GC_FOREGROUND |
547  XCB_GC_BACKGROUND |
548  XCB_GC_LINE_WIDTH |
549  XCB_GC_LINE_STYLE |
550  XCB_GC_FILL_STYLE;
551  uint32_t values[] = { c->screen->black_pixel,
552  c->screen->white_pixel,
553  c->region_border,
554  XCB_LINE_STYLE_DOUBLE_DASH,
555  XCB_FILL_STYLE_SOLID };
556  xcb_rectangle_t r = { 1, 1,
557  c->width + c->region_border * 2 - 3,
558  c->height + c->region_border * 2 - 3 };
559 
560  xcb_create_gc(c->conn, gc, c->window, mask, values);
561 
562  xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
563 }
564 
566 {
567  XCBGrabContext *c = s->priv_data;
568  uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
569  uint32_t values[] = { 1,
570  XCB_EVENT_MASK_EXPOSURE |
571  XCB_EVENT_MASK_STRUCTURE_NOTIFY };
572  xcb_rectangle_t rect = { 0, 0, c->width, c->height };
573 
574  c->window = xcb_generate_id(c->conn);
575 
576  xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
577  c->window,
578  c->screen->root,
579  c->x - c->region_border,
580  c->y - c->region_border,
581  c->width + c->region_border * 2,
582  c->height + c->region_border * 2,
583  0,
584  XCB_WINDOW_CLASS_INPUT_OUTPUT,
585  XCB_COPY_FROM_PARENT,
586  mask, values);
587 
588 #if CONFIG_LIBXCB_SHAPE
589  xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
590  XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
591  c->window,
593  1, &rect);
594 #endif
595 
596  xcb_map_window(c->conn, c->window);
597 
598  draw_rectangle(s);
599 }
600 
602 {
603  XCBGrabContext *c = s->priv_data;
604  int screen_num, ret;
605  const xcb_setup_t *setup;
606  char *display_name = av_strdup(s->filename);
607 
608  if (!display_name)
609  return AVERROR(ENOMEM);
610 
611  if (!sscanf(s->filename, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
612  *display_name = 0;
613  sscanf(s->filename, "+%d,%d", &c->x, &c->y);
614  }
615 
616  c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
617  av_freep(&display_name);
618 
619  if ((ret = xcb_connection_has_error(c->conn))) {
620  av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
621  s->filename[0] ? s->filename : "default", ret);
622  return AVERROR(EIO);
623  }
624  setup = xcb_get_setup(c->conn);
625 
626  c->screen = get_screen(setup, screen_num);
627  if (!c->screen) {
628  av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
629  screen_num);
631  return AVERROR(EIO);
632  }
633 
634  ret = create_stream(s);
635 
636  if (ret < 0) {
638  return ret;
639  }
640 
641 #if CONFIG_LIBXCB_SHM
642  if ((c->has_shm = check_shm(c->conn)))
643  c->segment = xcb_generate_id(c->conn);
644 #endif
645 
646 #if CONFIG_LIBXCB_XFIXES
647  if (c->draw_mouse) {
648  if (!(c->draw_mouse = check_xfixes(c->conn))) {
650  "XFixes not available, cannot draw the mouse.\n");
651  }
652  if (c->bpp < 24) {
653  avpriv_report_missing_feature(s, "%d bits per pixel screen",
654  c->bpp);
655  c->draw_mouse = 0;
656  }
657  }
658 #endif
659 
660  if (c->show_region)
661  setup_window(s);
662 
663  return 0;
664 }
665 
667  .name = "x11grab",
668  .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
669  .priv_data_size = sizeof(XCBGrabContext),
673  .flags = AVFMT_NOFILE,
674  .priv_class = &xcbgrab_class,
675 };