3 * Copyright (C) 2014 Luca Barbato <lu_zero@gentoo.org>
5 * This file is part of FFmpeg.
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.
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.
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
28 #if CONFIG_LIBXCB_XFIXES
29 #include <xcb/xfixes.h>
37 #if CONFIG_LIBXCB_SHAPE
38 #include <xcb/shape.h>
41 #include "libavutil/internal.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/opt.h"
44 #include "libavutil/parseutils.h"
45 #include "libavutil/time.h"
47 #include "libavformat/avformat.h"
48 #include "libavformat/internal.h"
50 typedef struct XCBGrabContext {
53 xcb_connection_t *conn;
57 AVBufferPool *shm_pool;
61 int64_t frame_duration;
75 const char *framerate;
80 #define FOLLOW_CENTER -1
82 #define OFFSET(x) offsetof(XCBGrabContext, x)
83 #define D AV_OPT_FLAG_DECODING_PARAM
84 static const AVOption options[] = {
85 { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
86 { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
87 { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
88 { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
89 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL }, 0, 0, D },
90 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
91 { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
92 { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
93 OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
94 { "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" },
95 { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
96 { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
97 { "select_region", "Select the grabbing region graphically using the pointer.", OFFSET(select_region), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
101 static const AVClass xcbgrab_class = {
102 .class_name = "xcbgrab indev",
103 .item_name = av_default_item_name,
105 .version = LIBAVUTIL_VERSION_INT,
106 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
109 static int xcbgrab_reposition(AVFormatContext *s,
110 xcb_query_pointer_reply_t *p,
111 xcb_get_geometry_reply_t *geo)
113 XCBGrabContext *c = s->priv_data;
114 int x = c->x, y = c->y;
115 int w = c->width, h = c->height, f = c->follow_mouse;
124 if (f == FOLLOW_CENTER) {
129 int right = x + w - f;
131 int bottom = y + h - f;
134 } else if (p_x < left) {
139 } else if (p_y < top) {
144 c->x = FFMIN(FFMAX(0, x), geo->width - w);
145 c->y = FFMIN(FFMAX(0, y), geo->height - h);
150 static void xcbgrab_image_reply_free(void *opaque, uint8_t *data)
155 static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
157 XCBGrabContext *c = s->priv_data;
158 xcb_get_image_cookie_t iq;
159 xcb_get_image_reply_t *img;
160 xcb_drawable_t drawable = c->screen->root;
161 xcb_generic_error_t *e = NULL;
165 iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
166 c->x, c->y, c->width, c->height, ~0);
168 img = xcb_get_image_reply(c->conn, iq, &e);
171 av_log(s, AV_LOG_ERROR,
172 "Cannot get the image data "
173 "event_error: response_type:%u error_code:%u "
174 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
175 e->response_type, e->error_code,
176 e->sequence, e->resource_id, e->minor_code, e->major_code);
178 return AVERROR(EACCES);
182 return AVERROR(EAGAIN);
184 data = xcb_get_image_data(img);
185 length = xcb_get_image_data_length(img);
189 pkt->buf = av_buffer_create(data, length, xcbgrab_image_reply_free, img, 0);
192 return AVERROR(ENOMEM);
201 static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
203 XCBGrabContext *c = s->priv_data;
204 int64_t curtime, delay;
206 c->time_frame += c->frame_duration;
209 curtime = av_gettime_relative();
210 delay = c->time_frame - curtime;
219 #if CONFIG_LIBXCB_SHM
220 static int check_shm(xcb_connection_t *conn)
222 xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
223 xcb_shm_query_version_reply_t *reply;
225 reply = xcb_shm_query_version_reply(conn, cookie, NULL);
234 static void free_shm_buffer(void *opaque, uint8_t *data)
239 static AVBufferRef *allocate_shm_buffer(void *opaque, buffer_size_t size)
241 xcb_connection_t *conn = opaque;
242 xcb_shm_seg_t segment;
247 id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
251 segment = xcb_generate_id(conn);
252 xcb_shm_attach(conn, segment, id, 0);
253 data = shmat(id, NULL, 0);
254 shmctl(id, IPC_RMID, 0);
255 if ((intptr_t)data == -1 || !data)
258 ref = av_buffer_create(data, size, free_shm_buffer, (void *)(ptrdiff_t)segment, 0);
265 static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
267 XCBGrabContext *c = s->priv_data;
268 xcb_shm_get_image_cookie_t iq;
269 xcb_shm_get_image_reply_t *img;
270 xcb_drawable_t drawable = c->screen->root;
271 xcb_generic_error_t *e = NULL;
273 xcb_shm_seg_t segment;
275 buf = av_buffer_pool_get(c->shm_pool);
277 av_log(s, AV_LOG_ERROR, "Could not get shared memory buffer.\n");
278 return AVERROR(ENOMEM);
280 segment = (xcb_shm_seg_t)av_buffer_pool_buffer_get_opaque(buf);
282 iq = xcb_shm_get_image(c->conn, drawable,
283 c->x, c->y, c->width, c->height, ~0,
284 XCB_IMAGE_FORMAT_Z_PIXMAP, segment, 0);
285 img = xcb_shm_get_image_reply(c->conn, iq, &e);
290 av_log(s, AV_LOG_ERROR,
291 "Cannot get the image data "
292 "event_error: response_type:%u error_code:%u "
293 "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
294 e->response_type, e->error_code,
295 e->sequence, e->resource_id, e->minor_code, e->major_code);
298 av_buffer_unref(&buf);
299 return AVERROR(EACCES);
307 pkt->data = buf->data;
308 pkt->size = c->frame_size;
312 #endif /* CONFIG_LIBXCB_SHM */
314 #if CONFIG_LIBXCB_XFIXES
315 static int check_xfixes(xcb_connection_t *conn)
317 xcb_xfixes_query_version_cookie_t cookie;
318 xcb_xfixes_query_version_reply_t *reply;
320 cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
321 XCB_XFIXES_MINOR_VERSION);
322 reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
331 #define BLEND(target, source, alpha) \
332 (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
334 static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
335 xcb_query_pointer_reply_t *p,
336 xcb_get_geometry_reply_t *geo)
338 XCBGrabContext *gr = s->priv_data;
340 uint8_t *image = pkt->data;
341 int stride = gr->bpp / 8;
342 xcb_xfixes_get_cursor_image_cookie_t cc;
343 xcb_xfixes_get_cursor_image_reply_t *ci;
344 int cx, cy, x, y, w, h, c_off, i_off;
346 cc = xcb_xfixes_get_cursor_image(gr->conn);
347 ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
351 cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
355 cx = ci->x - ci->xhot;
356 cy = ci->y - ci->yhot;
358 x = FFMAX(cx, gr->x);
359 y = FFMAX(cy, gr->y);
361 w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
362 h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
367 cursor += (y - cy) * ci->width;
368 image += (y - gr->y) * gr->width * stride;
370 for (y = 0; y < h; y++) {
372 image += i_off * stride;
373 for (x = 0; x < w; x++, cursor++, image += stride) {
377 g = (*cursor >> 8) & 0xff;
378 b = (*cursor >> 16) & 0xff;
379 a = (*cursor >> 24) & 0xff;
389 image[0] = BLEND(r, image[0], a);
390 image[1] = BLEND(g, image[1], a);
391 image[2] = BLEND(b, image[2], a);
395 cursor += ci->width - w - c_off;
396 image += (gr->width - w - i_off) * stride;
401 #endif /* CONFIG_LIBXCB_XFIXES */
403 static void xcbgrab_update_region(AVFormatContext *s)
405 XCBGrabContext *c = s->priv_data;
406 const uint32_t args[] = { c->x - c->region_border,
407 c->y - c->region_border };
409 xcb_configure_window(c->conn,
411 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
415 static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
417 XCBGrabContext *c = s->priv_data;
418 xcb_query_pointer_cookie_t pc;
419 xcb_get_geometry_cookie_t gc;
420 xcb_query_pointer_reply_t *p = NULL;
421 xcb_get_geometry_reply_t *geo = NULL;
428 if (c->follow_mouse || c->draw_mouse) {
429 pc = xcb_query_pointer(c->conn, c->screen->root);
430 gc = xcb_get_geometry(c->conn, c->screen->root);
431 p = xcb_query_pointer_reply(c->conn, pc, NULL);
433 av_log(s, AV_LOG_ERROR, "Failed to query xcb pointer\n");
434 return AVERROR_EXTERNAL;
436 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
438 av_log(s, AV_LOG_ERROR, "Failed to get xcb geometry\n");
440 return AVERROR_EXTERNAL;
444 if (c->follow_mouse && p->same_screen)
445 xcbgrab_reposition(s, p, geo);
448 xcbgrab_update_region(s);
450 #if CONFIG_LIBXCB_SHM
451 if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0) {
452 av_log(s, AV_LOG_WARNING, "Continuing without shared memory.\n");
457 ret = xcbgrab_frame(s, pkt);
458 pkt->dts = pkt->pts = pts;
459 pkt->duration = c->frame_duration;
461 #if CONFIG_LIBXCB_XFIXES
462 if (ret >= 0 && c->draw_mouse && p->same_screen)
463 xcbgrab_draw_mouse(s, pkt, p, geo);
472 static av_cold int xcbgrab_read_close(AVFormatContext *s)
474 XCBGrabContext *ctx = s->priv_data;
476 #if CONFIG_LIBXCB_SHM
477 av_buffer_pool_uninit(&ctx->shm_pool);
480 xcb_disconnect(ctx->conn);
485 static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
487 xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
488 xcb_screen_t *screen = NULL;
490 for (; it.rem > 0; xcb_screen_next (&it)) {
502 static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
503 int *pix_fmt, int *bpp)
505 XCBGrabContext *c = s->priv_data;
506 const xcb_setup_t *setup = xcb_get_setup(c->conn);
507 const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
508 int length = xcb_setup_pixmap_formats_length(setup);
513 if (fmt->depth == depth) {
516 if (fmt->bits_per_pixel == 32)
517 *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
518 AV_PIX_FMT_BGR0 : AV_PIX_FMT_0RGB;
521 if (fmt->bits_per_pixel == 32)
522 *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
523 AV_PIX_FMT_BGR0 : AV_PIX_FMT_0RGB;
524 else if (fmt->bits_per_pixel == 24)
525 *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
526 AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
529 if (fmt->bits_per_pixel == 16)
530 *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
531 AV_PIX_FMT_RGB565LE : AV_PIX_FMT_RGB565BE;
534 if (fmt->bits_per_pixel == 16)
535 *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
536 AV_PIX_FMT_RGB555LE : AV_PIX_FMT_RGB555BE;
539 if (fmt->bits_per_pixel == 8)
540 *pix_fmt = AV_PIX_FMT_RGB8;
546 *bpp = fmt->bits_per_pixel;
552 avpriv_report_missing_feature(s, "Mapping this pixmap format");
554 return AVERROR_PATCHWELCOME;
557 static int create_stream(AVFormatContext *s)
559 XCBGrabContext *c = s->priv_data;
560 AVStream *st = avformat_new_stream(s, NULL);
561 xcb_get_geometry_cookie_t gc;
562 xcb_get_geometry_reply_t *geo;
563 int64_t frame_size_bits;
567 return AVERROR(ENOMEM);
569 ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
573 avpriv_set_pts_info(st, 64, 1, 1000000);
575 gc = xcb_get_geometry(c->conn, c->screen->root);
576 geo = xcb_get_geometry_reply(c->conn, gc, NULL);
578 return AVERROR_EXTERNAL;
580 if (!c->width || !c->height) {
581 c->width = geo->width;
582 c->height = geo->height;
585 if (c->x + c->width > geo->width ||
586 c->y + c->height > geo->height) {
587 av_log(s, AV_LOG_ERROR,
588 "Capture area %dx%d at position %d.%d "
589 "outside the screen size %dx%d\n",
592 geo->width, geo->height);
594 return AVERROR(EINVAL);
597 c->time_base = (AVRational){ st->avg_frame_rate.den,
598 st->avg_frame_rate.num };
599 c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
600 c->time_frame = av_gettime_relative();
602 ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
607 frame_size_bits = (int64_t)c->width * c->height * c->bpp;
608 if (frame_size_bits / 8 + AV_INPUT_BUFFER_PADDING_SIZE > INT_MAX) {
609 av_log(s, AV_LOG_ERROR, "Captured area is too large\n");
610 return AVERROR_PATCHWELCOME;
612 c->frame_size = frame_size_bits / 8;
614 #if CONFIG_LIBXCB_SHM
615 c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE,
616 c->conn, allocate_shm_buffer, NULL);
618 return AVERROR(ENOMEM);
621 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
622 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
623 st->codecpar->width = c->width;
624 st->codecpar->height = c->height;
625 st->codecpar->bit_rate = av_rescale(frame_size_bits, st->avg_frame_rate.num, st->avg_frame_rate.den);
630 static void draw_rectangle(AVFormatContext *s)
632 XCBGrabContext *c = s->priv_data;
633 xcb_gcontext_t gc = xcb_generate_id(c->conn);
634 uint32_t mask = XCB_GC_FOREGROUND |
639 uint32_t values[] = { c->screen->black_pixel,
640 c->screen->white_pixel,
642 XCB_LINE_STYLE_DOUBLE_DASH,
643 XCB_FILL_STYLE_SOLID };
644 xcb_rectangle_t r = { 1, 1,
645 c->width + c->region_border * 2 - 3,
646 c->height + c->region_border * 2 - 3 };
648 xcb_create_gc(c->conn, gc, c->window, mask, values);
650 xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
653 static void setup_window(AVFormatContext *s)
655 XCBGrabContext *c = s->priv_data;
656 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
657 uint32_t values[] = { 1,
658 XCB_EVENT_MASK_EXPOSURE |
659 XCB_EVENT_MASK_STRUCTURE_NOTIFY };
660 av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
662 c->window = xcb_generate_id(c->conn);
664 xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
667 c->x - c->region_border,
668 c->y - c->region_border,
669 c->width + c->region_border * 2,
670 c->height + c->region_border * 2,
672 XCB_WINDOW_CLASS_INPUT_OUTPUT,
673 XCB_COPY_FROM_PARENT,
676 #if CONFIG_LIBXCB_SHAPE
677 xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
678 XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
680 c->region_border, c->region_border,
684 xcb_map_window(c->conn, c->window);
689 #define CROSSHAIR_CURSOR 34
691 static xcb_rectangle_t rectangle_from_corners(xcb_point_t *corner_a,
692 xcb_point_t *corner_b)
694 xcb_rectangle_t rectangle;
695 rectangle.x = FFMIN(corner_a->x, corner_b->x);
696 rectangle.y = FFMIN(corner_a->y, corner_b->y);
697 rectangle.width = FFABS(corner_a->x - corner_b->x);
698 rectangle.height = FFABS(corner_a->y - corner_b->y);
702 static int select_region(AVFormatContext *s)
704 XCBGrabContext *c = s->priv_data;
705 xcb_connection_t *conn = c->conn;
706 xcb_screen_t *screen = c->screen;
708 int ret = 0, done = 0, was_pressed = 0;
710 xcb_font_t cursor_font;
711 xcb_point_t press_position;
712 xcb_generic_event_t *event;
713 xcb_rectangle_t rectangle = { 0 };
714 xcb_grab_pointer_reply_t *reply;
715 xcb_grab_pointer_cookie_t cookie;
717 xcb_window_t root_window = screen->root;
718 xcb_gcontext_t gc = xcb_generate_id(conn);
719 uint32_t mask = XCB_GC_FUNCTION | XCB_GC_SUBWINDOW_MODE;
720 uint32_t values[] = { XCB_GX_INVERT, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS };
721 xcb_create_gc(conn, gc, root_window, mask, values);
723 cursor_font = xcb_generate_id(conn);
724 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
725 cursor = xcb_generate_id(conn);
726 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
727 CROSSHAIR_CURSOR, CROSSHAIR_CURSOR + 1, 0, 0, 0,
728 0xFFFF, 0xFFFF, 0xFFFF);
729 cookie = xcb_grab_pointer(conn, 0, root_window,
730 XCB_EVENT_MASK_BUTTON_PRESS |
731 XCB_EVENT_MASK_BUTTON_RELEASE |
732 XCB_EVENT_MASK_BUTTON_MOTION,
733 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
734 root_window, cursor, XCB_CURRENT_TIME);
735 reply = xcb_grab_pointer_reply(conn, cookie, NULL);
736 if (!reply || reply->status != XCB_GRAB_STATUS_SUCCESS) {
737 av_log(s, AV_LOG_ERROR,
738 "Failed to select region. Could not grab pointer.\n");
745 xcb_grab_server(conn);
747 while (!done && (event = xcb_wait_for_event(conn))) {
748 switch (event->response_type & ~0x80) {
749 case XCB_BUTTON_PRESS: {
750 xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
751 press_position = (xcb_point_t){ press->event_x, press->event_y };
752 rectangle.x = press_position.x;
753 rectangle.y = press_position.y;
754 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
758 case XCB_MOTION_NOTIFY: {
760 xcb_motion_notify_event_t *motion =
761 (xcb_motion_notify_event_t *)event;
762 xcb_point_t cursor_position = { motion->event_x, motion->event_y };
763 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
764 rectangle = rectangle_from_corners(&press_position, &cursor_position);
765 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
769 case XCB_BUTTON_RELEASE: {
770 xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
780 c->width = rectangle.width;
781 c->height = rectangle.height;
782 if (c->width && c->height) {
789 xcb_ungrab_server(conn);
790 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
794 xcb_free_cursor(conn, cursor);
795 xcb_close_font(conn, cursor_font);
796 xcb_free_gc(conn, gc);
800 static av_cold int xcbgrab_read_header(AVFormatContext *s)
802 XCBGrabContext *c = s->priv_data;
804 const xcb_setup_t *setup;
805 char *display_name = av_strdup(s->url);
808 return AVERROR(ENOMEM);
810 if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
812 sscanf(s->url, "+%d,%d", &c->x, &c->y);
815 c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
816 av_freep(&display_name);
818 if ((ret = xcb_connection_has_error(c->conn))) {
819 av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
820 s->url[0] ? s->url : "default", ret);
824 setup = xcb_get_setup(c->conn);
826 c->screen = get_screen(setup, screen_num);
828 av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
830 xcbgrab_read_close(s);
834 if (c->select_region) {
835 ret = select_region(s);
837 xcbgrab_read_close(s);
842 ret = create_stream(s);
845 xcbgrab_read_close(s);
849 #if CONFIG_LIBXCB_SHM
850 c->has_shm = check_shm(c->conn);
853 #if CONFIG_LIBXCB_XFIXES
855 if (!(c->draw_mouse = check_xfixes(c->conn))) {
856 av_log(s, AV_LOG_WARNING,
857 "XFixes not available, cannot draw the mouse.\n");
860 avpriv_report_missing_feature(s, "%d bits per pixel screen",
873 AVInputFormat ff_xcbgrab_demuxer = {
875 .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
876 .priv_data_size = sizeof(XCBGrabContext),
877 .read_header = xcbgrab_read_header,
878 .read_packet = xcbgrab_read_packet,
879 .read_close = xcbgrab_read_close,
880 .flags = AVFMT_NOFILE,
881 .priv_class = &xcbgrab_class,