]> git.sesse.net Git - ffmpeg/blob - libavdevice/xcbgrab.c
avdevice/xcbgrab: fix packet timestamps
[ffmpeg] / libavdevice / xcbgrab.c
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 "libavutil/internal.h"
41 #include "libavutil/mathematics.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/time.h"
45
46 #include "libavformat/avformat.h"
47 #include "libavformat/internal.h"
48
49 typedef struct XCBGrabContext {
50     const AVClass *class;
51
52     xcb_connection_t *conn;
53     xcb_screen_t *screen;
54     xcb_window_t window;
55 #if CONFIG_LIBXCB_SHM
56     AVBufferPool *shm_pool;
57 #endif
58     int64_t time_frame;
59     AVRational time_base;
60     int64_t frame_duration;
61
62     int x, y;
63     int width, height;
64     int frame_size;
65     int bpp;
66
67     int draw_mouse;
68     int follow_mouse;
69     int show_region;
70     int region_border;
71     int centered;
72
73     const char *video_size;
74     const char *framerate;
75
76     int has_shm;
77 } XCBGrabContext;
78
79 #define FOLLOW_CENTER -1
80
81 #define OFFSET(x) offsetof(XCBGrabContext, x)
82 #define D AV_OPT_FLAG_DECODING_PARAM
83 static const AVOption options[] = {
84     { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
85     { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
86     { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
87     { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
88     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
89     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
90     { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
91     { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
92       OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 },  FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
93     { "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" },
94     { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
95     { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
96     { NULL },
97 };
98
99 static const AVClass xcbgrab_class = {
100     .class_name = "xcbgrab indev",
101     .item_name  = av_default_item_name,
102     .option     = options,
103     .version    = LIBAVUTIL_VERSION_INT,
104     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
105 };
106
107 static int xcbgrab_reposition(AVFormatContext *s,
108                               xcb_query_pointer_reply_t *p,
109                               xcb_get_geometry_reply_t *geo)
110 {
111     XCBGrabContext *c = s->priv_data;
112     int x = c->x, y = c->y;
113     int w = c->width, h = c->height, f = c->follow_mouse;
114     int p_x, p_y;
115
116     if (!p || !geo)
117         return AVERROR(EIO);
118
119     p_x = p->win_x;
120     p_y = p->win_y;
121
122     if (f == FOLLOW_CENTER) {
123         x = p_x - w / 2;
124         y = p_y - h / 2;
125     } else {
126         int left   = x + f;
127         int right  = x + w - f;
128         int top    = y + f;
129         int bottom = y + h - f;
130         if (p_x > right) {
131             x += p_x - right;
132         } else if (p_x < left) {
133             x -= left - p_x;
134         }
135         if (p_y > bottom) {
136             y += p_y - bottom;
137         } else if (p_y < top) {
138             y -= top - p_y;
139         }
140     }
141
142     c->x = FFMIN(FFMAX(0, x), geo->width  - w);
143     c->y = FFMIN(FFMAX(0, y), geo->height - h);
144
145     return 0;
146 }
147
148 static void xcbgrab_image_reply_free(void *opaque, uint8_t *data)
149 {
150     free(opaque);
151 }
152
153 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
154 {
155     XCBGrabContext *c = s->priv_data;
156     xcb_get_image_cookie_t iq;
157     xcb_get_image_reply_t *img;
158     xcb_drawable_t drawable = c->screen->root;
159     xcb_generic_error_t *e = NULL;
160     uint8_t *data;
161     int length;
162
163     iq  = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
164                         c->x, c->y, c->width, c->height, ~0);
165
166     img = xcb_get_image_reply(c->conn, iq, &e);
167
168     if (e) {
169         av_log(s, AV_LOG_ERROR,
170                "Cannot get the image data "
171                "event_error: response_type:%u error_code:%u "
172                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
173                e->response_type, e->error_code,
174                e->sequence, e->resource_id, e->minor_code, e->major_code);
175         free(e);
176         return AVERROR(EACCES);
177     }
178
179     if (!img)
180         return AVERROR(EAGAIN);
181
182     data   = xcb_get_image_data(img);
183     length = xcb_get_image_data_length(img);
184
185     av_init_packet(pkt);
186
187     pkt->buf = av_buffer_create(data, length, xcbgrab_image_reply_free, img, 0);
188     if (!pkt->buf) {
189         free(img);
190         return AVERROR(ENOMEM);
191     }
192
193     pkt->data = data;
194     pkt->size = length;
195
196     return 0;
197 }
198
199 static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
200 {
201     XCBGrabContext *c = s->priv_data;
202     int64_t curtime, delay;
203
204     c->time_frame += c->frame_duration;
205
206     for (;;) {
207         curtime = av_gettime();
208         delay   = c->time_frame - curtime;
209         if (delay <= 0)
210             break;
211         av_usleep(delay);
212     }
213
214     return curtime;
215 }
216
217 #if CONFIG_LIBXCB_SHM
218 static int check_shm(xcb_connection_t *conn)
219 {
220     xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
221     xcb_shm_query_version_reply_t *reply;
222
223     reply = xcb_shm_query_version_reply(conn, cookie, NULL);
224     if (reply) {
225         free(reply);
226         return 1;
227     }
228
229     return 0;
230 }
231
232 static void free_shm_buffer(void *opaque, uint8_t *data)
233 {
234     shmdt(data);
235 }
236
237 static AVBufferRef *allocate_shm_buffer(void *opaque, int size)
238 {
239     xcb_connection_t *conn = opaque;
240     xcb_shm_seg_t segment;
241     AVBufferRef *ref;
242     uint8_t *data;
243     int id;
244
245     id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
246     if (id == -1)
247         return NULL;
248
249     segment = xcb_generate_id(conn);
250     xcb_shm_attach(conn, segment, id, 0);
251     data = shmat(id, NULL, 0);
252     shmctl(id, IPC_RMID, 0);
253     if ((intptr_t)data == -1 || !data)
254         return NULL;
255
256     ref = av_buffer_create(data, size, free_shm_buffer, (void *)(ptrdiff_t)segment, 0);
257     if (!ref)
258         shmdt(data);
259
260     return ref;
261 }
262
263 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
264 {
265     XCBGrabContext *c = s->priv_data;
266     xcb_shm_get_image_cookie_t iq;
267     xcb_shm_get_image_reply_t *img;
268     xcb_drawable_t drawable = c->screen->root;
269     xcb_generic_error_t *e = NULL;
270     AVBufferRef *buf;
271     xcb_shm_seg_t segment;
272
273     buf = av_buffer_pool_get(c->shm_pool);
274     if (!buf) {
275         av_log(s, AV_LOG_ERROR, "Could not get shared memory buffer.\n");
276         return AVERROR(ENOMEM);
277     }
278     segment = (xcb_shm_seg_t)av_buffer_pool_buffer_get_opaque(buf);
279
280     iq = xcb_shm_get_image(c->conn, drawable,
281                            c->x, c->y, c->width, c->height, ~0,
282                            XCB_IMAGE_FORMAT_Z_PIXMAP, segment, 0);
283     img = xcb_shm_get_image_reply(c->conn, iq, &e);
284
285     xcb_flush(c->conn);
286
287     if (e) {
288         av_log(s, AV_LOG_ERROR,
289                "Cannot get the image data "
290                "event_error: response_type:%u error_code:%u "
291                "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
292                e->response_type, e->error_code,
293                e->sequence, e->resource_id, e->minor_code, e->major_code);
294
295         free(e);
296         av_buffer_unref(&buf);
297         return AVERROR(EACCES);
298     }
299
300     free(img);
301
302     av_init_packet(pkt);
303
304     pkt->buf = buf;
305     pkt->data = buf->data;
306     pkt->size = c->frame_size;
307
308     return 0;
309 }
310 #endif /* CONFIG_LIBXCB_SHM */
311
312 #if CONFIG_LIBXCB_XFIXES
313 static int check_xfixes(xcb_connection_t *conn)
314 {
315     xcb_xfixes_query_version_cookie_t cookie;
316     xcb_xfixes_query_version_reply_t *reply;
317
318     cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
319                                       XCB_XFIXES_MINOR_VERSION);
320     reply  = xcb_xfixes_query_version_reply(conn, cookie, NULL);
321
322     if (reply) {
323         free(reply);
324         return 1;
325     }
326     return 0;
327 }
328
329 #define BLEND(target, source, alpha) \
330     (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
331
332 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
333                                xcb_query_pointer_reply_t *p,
334                                xcb_get_geometry_reply_t *geo)
335 {
336     XCBGrabContext *gr = s->priv_data;
337     uint32_t *cursor;
338     uint8_t *image = pkt->data;
339     int stride     = gr->bpp / 8;
340     xcb_xfixes_get_cursor_image_cookie_t cc;
341     xcb_xfixes_get_cursor_image_reply_t *ci;
342     int cx, cy, x, y, w, h, c_off, i_off;
343
344     cc = xcb_xfixes_get_cursor_image(gr->conn);
345     ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
346     if (!ci)
347         return;
348
349     cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
350     if (!cursor)
351         return;
352
353     cx = ci->x - ci->xhot;
354     cy = ci->y - ci->yhot;
355
356     x = FFMAX(cx, gr->x);
357     y = FFMAX(cy, gr->y);
358
359     w = FFMIN(cx + ci->width,  gr->x + gr->width)  - x;
360     h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
361
362     c_off = x - cx;
363     i_off = x - gr->x;
364
365     cursor += (y - cy) * ci->width;
366     image  += (y - gr->y) * gr->width * stride;
367
368     for (y = 0; y < h; y++) {
369         cursor += c_off;
370         image  += i_off * stride;
371         for (x = 0; x < w; x++, cursor++, image += stride) {
372             int r, g, b, a;
373
374             r =  *cursor        & 0xff;
375             g = (*cursor >>  8) & 0xff;
376             b = (*cursor >> 16) & 0xff;
377             a = (*cursor >> 24) & 0xff;
378
379             if (!a)
380                 continue;
381
382             if (a == 255) {
383                 image[0] = r;
384                 image[1] = g;
385                 image[2] = b;
386             } else {
387                 image[0] = BLEND(r, image[0], a);
388                 image[1] = BLEND(g, image[1], a);
389                 image[2] = BLEND(b, image[2], a);
390             }
391
392         }
393         cursor +=  ci->width - w - c_off;
394         image  += (gr->width - w - i_off) * stride;
395     }
396
397     free(ci);
398 }
399 #endif /* CONFIG_LIBXCB_XFIXES */
400
401 static void xcbgrab_update_region(AVFormatContext *s)
402 {
403     XCBGrabContext *c     = s->priv_data;
404     const uint32_t args[] = { c->x - c->region_border,
405                               c->y - c->region_border };
406
407     xcb_configure_window(c->conn,
408                          c->window,
409                          XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
410                          args);
411 }
412
413 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
414 {
415     XCBGrabContext *c = s->priv_data;
416     xcb_query_pointer_cookie_t pc;
417     xcb_get_geometry_cookie_t gc;
418     xcb_query_pointer_reply_t *p  = NULL;
419     xcb_get_geometry_reply_t *geo = NULL;
420     int ret = 0;
421     int64_t pts;
422
423     pts = wait_frame(s, pkt);
424
425     if (c->follow_mouse || c->draw_mouse) {
426         pc  = xcb_query_pointer(c->conn, c->screen->root);
427         gc  = xcb_get_geometry(c->conn, c->screen->root);
428         p   = xcb_query_pointer_reply(c->conn, pc, NULL);
429         geo = xcb_get_geometry_reply(c->conn, gc, NULL);
430     }
431
432     if (c->follow_mouse && p->same_screen)
433         xcbgrab_reposition(s, p, geo);
434
435     if (c->show_region)
436         xcbgrab_update_region(s);
437
438 #if CONFIG_LIBXCB_SHM
439     if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0) {
440         av_log(s, AV_LOG_WARNING, "Continuing without shared memory.\n");
441         c->has_shm = 0;
442     }
443 #endif
444     if (!c->has_shm)
445         ret = xcbgrab_frame(s, pkt);
446     pkt->dts = pkt->pts = pts;
447     pkt->duration = c->frame_duration;
448
449 #if CONFIG_LIBXCB_XFIXES
450     if (ret >= 0 && c->draw_mouse && p->same_screen)
451         xcbgrab_draw_mouse(s, pkt, p, geo);
452 #endif
453
454     free(p);
455     free(geo);
456
457     return ret;
458 }
459
460 static av_cold int xcbgrab_read_close(AVFormatContext *s)
461 {
462     XCBGrabContext *ctx = s->priv_data;
463
464 #if CONFIG_LIBXCB_SHM
465     av_buffer_pool_uninit(&ctx->shm_pool);
466 #endif
467
468     xcb_disconnect(ctx->conn);
469
470     return 0;
471 }
472
473 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
474 {
475     xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
476     xcb_screen_t *screen     = NULL;
477
478     for (; it.rem > 0; xcb_screen_next (&it)) {
479         if (!screen_num) {
480             screen = it.data;
481             break;
482         }
483
484         screen_num--;
485     }
486
487     return screen;
488 }
489
490 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
491                                      int *pix_fmt)
492 {
493     XCBGrabContext *c        = s->priv_data;
494     const xcb_setup_t *setup = xcb_get_setup(c->conn);
495     const xcb_format_t *fmt  = xcb_setup_pixmap_formats(setup);
496     int length               = xcb_setup_pixmap_formats_length(setup);
497
498     *pix_fmt = 0;
499
500     while (length--) {
501         if (fmt->depth == depth) {
502             switch (depth) {
503             case 32:
504                 if (fmt->bits_per_pixel == 32)
505                     *pix_fmt = AV_PIX_FMT_0RGB;
506                 break;
507             case 24:
508                 if (fmt->bits_per_pixel == 32)
509                     *pix_fmt = AV_PIX_FMT_0RGB32;
510                 else if (fmt->bits_per_pixel == 24)
511                     *pix_fmt = AV_PIX_FMT_RGB24;
512                 break;
513             case 16:
514                 if (fmt->bits_per_pixel == 16)
515                     *pix_fmt = AV_PIX_FMT_RGB565;
516                 break;
517             case 15:
518                 if (fmt->bits_per_pixel == 16)
519                     *pix_fmt = AV_PIX_FMT_RGB555;
520                 break;
521             case 8:
522                 if (fmt->bits_per_pixel == 8)
523                     *pix_fmt = AV_PIX_FMT_RGB8;
524                 break;
525             }
526         }
527
528         if (*pix_fmt) {
529             c->bpp        = fmt->bits_per_pixel;
530             c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
531 #if CONFIG_LIBXCB_SHM
532             c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE,
533                                                c->conn, allocate_shm_buffer, NULL);
534             if (!c->shm_pool)
535                 return AVERROR(ENOMEM);
536 #endif
537             return 0;
538         }
539
540         fmt++;
541     }
542     avpriv_report_missing_feature(s, "Mapping this pixmap format");
543
544     return AVERROR_PATCHWELCOME;
545 }
546
547 static int create_stream(AVFormatContext *s)
548 {
549     XCBGrabContext *c = s->priv_data;
550     AVStream *st      = avformat_new_stream(s, NULL);
551     xcb_get_geometry_cookie_t gc;
552     xcb_get_geometry_reply_t *geo;
553     int ret;
554
555     if (!st)
556         return AVERROR(ENOMEM);
557
558     ret = av_parse_video_size(&c->width, &c->height, c->video_size);
559     if (ret < 0)
560         return ret;
561
562     ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
563     if (ret < 0)
564         return ret;
565
566     avpriv_set_pts_info(st, 64, 1, 1000000);
567
568     gc  = xcb_get_geometry(c->conn, c->screen->root);
569     geo = xcb_get_geometry_reply(c->conn, gc, NULL);
570     if (!geo)
571         return AVERROR_EXTERNAL;
572
573     if (c->x + c->width > geo->width ||
574         c->y + c->height > geo->height) {
575         av_log(s, AV_LOG_ERROR,
576                "Capture area %dx%d at position %d.%d "
577                "outside the screen size %dx%d\n",
578                c->width, c->height,
579                c->x, c->y,
580                geo->width, geo->height);
581         free(geo);
582         return AVERROR(EINVAL);
583     }
584
585     c->time_base  = (AVRational){ st->avg_frame_rate.den,
586                                   st->avg_frame_rate.num };
587     c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
588     c->time_frame = av_gettime();
589
590     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
591     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
592     st->codecpar->width      = c->width;
593     st->codecpar->height     = c->height;
594
595     ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format);
596
597     free(geo);
598
599     return ret;
600 }
601
602 static void draw_rectangle(AVFormatContext *s)
603 {
604     XCBGrabContext *c = s->priv_data;
605     xcb_gcontext_t gc = xcb_generate_id(c->conn);
606     uint32_t mask     = XCB_GC_FOREGROUND |
607                         XCB_GC_BACKGROUND |
608                         XCB_GC_LINE_WIDTH |
609                         XCB_GC_LINE_STYLE |
610                         XCB_GC_FILL_STYLE;
611     uint32_t values[] = { c->screen->black_pixel,
612                           c->screen->white_pixel,
613                           c->region_border,
614                           XCB_LINE_STYLE_DOUBLE_DASH,
615                           XCB_FILL_STYLE_SOLID };
616     xcb_rectangle_t r = { 1, 1,
617                           c->width  + c->region_border * 2 - 3,
618                           c->height + c->region_border * 2 - 3 };
619
620     xcb_create_gc(c->conn, gc, c->window, mask, values);
621
622     xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
623 }
624
625 static void setup_window(AVFormatContext *s)
626 {
627     XCBGrabContext *c = s->priv_data;
628     uint32_t mask     = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
629     uint32_t values[] = { 1,
630                           XCB_EVENT_MASK_EXPOSURE |
631                           XCB_EVENT_MASK_STRUCTURE_NOTIFY };
632     av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
633
634     c->window = xcb_generate_id(c->conn);
635
636     xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
637                       c->window,
638                       c->screen->root,
639                       c->x - c->region_border,
640                       c->y - c->region_border,
641                       c->width + c->region_border * 2,
642                       c->height + c->region_border * 2,
643                       0,
644                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
645                       XCB_COPY_FROM_PARENT,
646                       mask, values);
647
648 #if CONFIG_LIBXCB_SHAPE
649     xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
650                          XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
651                          c->window,
652                          c->region_border, c->region_border,
653                          1, &rect);
654 #endif
655
656     xcb_map_window(c->conn, c->window);
657
658     draw_rectangle(s);
659 }
660
661 static av_cold int xcbgrab_read_header(AVFormatContext *s)
662 {
663     XCBGrabContext *c = s->priv_data;
664     int screen_num, ret;
665     const xcb_setup_t *setup;
666     char *display_name = av_strdup(s->url);
667
668     if (!display_name)
669         return AVERROR(ENOMEM);
670
671     if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
672         *display_name = 0;
673         sscanf(s->url, "+%d,%d", &c->x, &c->y);
674     }
675
676     c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
677     av_freep(&display_name);
678
679     if ((ret = xcb_connection_has_error(c->conn))) {
680         av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
681                s->url[0] ? s->url : "default", ret);
682         return AVERROR(EIO);
683     }
684
685     setup = xcb_get_setup(c->conn);
686
687     c->screen = get_screen(setup, screen_num);
688     if (!c->screen) {
689         av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
690                screen_num);
691         xcbgrab_read_close(s);
692         return AVERROR(EIO);
693     }
694
695     ret = create_stream(s);
696
697     if (ret < 0) {
698         xcbgrab_read_close(s);
699         return ret;
700     }
701
702 #if CONFIG_LIBXCB_SHM
703     c->has_shm = check_shm(c->conn);
704 #endif
705
706 #if CONFIG_LIBXCB_XFIXES
707     if (c->draw_mouse) {
708         if (!(c->draw_mouse = check_xfixes(c->conn))) {
709             av_log(s, AV_LOG_WARNING,
710                    "XFixes not available, cannot draw the mouse.\n");
711         }
712         if (c->bpp < 24) {
713             avpriv_report_missing_feature(s, "%d bits per pixel screen",
714                                           c->bpp);
715             c->draw_mouse = 0;
716         }
717     }
718 #endif
719
720     if (c->show_region)
721         setup_window(s);
722
723     return 0;
724 }
725
726 AVInputFormat ff_xcbgrab_demuxer = {
727     .name           = "x11grab",
728     .long_name      = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
729     .priv_data_size = sizeof(XCBGrabContext),
730     .read_header    = xcbgrab_read_header,
731     .read_packet    = xcbgrab_read_packet,
732     .read_close     = xcbgrab_read_close,
733     .flags          = AVFMT_NOFILE,
734     .priv_class     = &xcbgrab_class,
735 };