xcbgrab.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. #include "config.h"
  22. #include <stdlib.h>
  23. #include <xcb/xcb.h>
  24. #if CONFIG_LIBXCB_XFIXES
  25. #include <xcb/xfixes.h>
  26. #endif
  27. #if CONFIG_LIBXCB_SHM
  28. #include <sys/shm.h>
  29. #include <xcb/shm.h>
  30. #endif
  31. #if CONFIG_LIBXCB_SHAPE
  32. #include <xcb/shape.h>
  33. #endif
  34. #include "libavutil/internal.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/parseutils.h"
  38. #include "libavutil/time.h"
  39. #include "libavformat/avformat.h"
  40. #include "libavformat/internal.h"
  41. typedef struct XCBGrabContext {
  42. const AVClass *class;
  43. xcb_connection_t *conn;
  44. xcb_screen_t *screen;
  45. xcb_window_t window;
  46. #if CONFIG_LIBXCB_SHM
  47. xcb_shm_seg_t segment;
  48. #endif
  49. int64_t time_frame;
  50. AVRational time_base;
  51. int x, y;
  52. int width, height;
  53. int frame_size;
  54. int bpp;
  55. int draw_mouse;
  56. int follow_mouse;
  57. int show_region;
  58. int region_border;
  59. int centered;
  60. const char *video_size;
  61. const char *framerate;
  62. int has_shm;
  63. } XCBGrabContext;
  64. #define FOLLOW_CENTER -1
  65. #define OFFSET(x) offsetof(XCBGrabContext, x)
  66. #define D AV_OPT_FLAG_DECODING_PARAM
  67. static const AVOption options[] = {
  68. { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  69. { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  70. { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  71. { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  72. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
  73. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
  74. { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
  75. { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
  76. OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
  77. { "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" },
  78. { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
  79. { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
  80. { NULL },
  81. };
  82. static const AVClass xcbgrab_class = {
  83. .class_name = "xcbgrab indev",
  84. .item_name = av_default_item_name,
  85. .option = options,
  86. .version = LIBAVUTIL_VERSION_INT,
  87. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  88. };
  89. static int xcbgrab_reposition(AVFormatContext *s,
  90. xcb_query_pointer_reply_t *p,
  91. xcb_get_geometry_reply_t *geo)
  92. {
  93. XCBGrabContext *c = s->priv_data;
  94. int x = c->x, y = c->y;
  95. int w = c->width, h = c->height, f = c->follow_mouse;
  96. int p_x, p_y;
  97. if (!p || !geo)
  98. return AVERROR(EIO);
  99. p_x = p->win_x;
  100. p_y = p->win_y;
  101. if (f == FOLLOW_CENTER) {
  102. x = p_x - w / 2;
  103. y = p_y - h / 2;
  104. } else {
  105. int left = x + f;
  106. int right = x + w - f;
  107. int top = y + f;
  108. int bottom = y + h + f;
  109. if (p_x > right) {
  110. x += p_x - right;
  111. } else if (p_x < left) {
  112. x -= left - p_x;
  113. }
  114. if (p_y > bottom) {
  115. y += p_y - bottom;
  116. } else if (p_y < top) {
  117. y -= top - p_y;
  118. }
  119. }
  120. c->x = FFMIN(FFMAX(0, x), geo->width - w);
  121. c->y = FFMIN(FFMAX(0, y), geo->height - h);
  122. return 0;
  123. }
  124. static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
  125. {
  126. XCBGrabContext *c = s->priv_data;
  127. xcb_get_image_cookie_t iq;
  128. xcb_get_image_reply_t *img;
  129. xcb_drawable_t drawable = c->screen->root;
  130. uint8_t *data;
  131. int length, ret;
  132. iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
  133. c->x, c->y, c->width, c->height, ~0);
  134. img = xcb_get_image_reply(c->conn, iq, NULL);
  135. if (!img)
  136. return AVERROR(EAGAIN);
  137. data = xcb_get_image_data(img);
  138. length = xcb_get_image_data_length(img);
  139. ret = av_new_packet(pkt, length);
  140. if (!ret)
  141. memcpy(pkt->data, data, length);
  142. free(img);
  143. return ret;
  144. }
  145. static void wait_frame(AVFormatContext *s, AVPacket *pkt)
  146. {
  147. XCBGrabContext *c = s->priv_data;
  148. int64_t curtime, delay;
  149. int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
  150. c->time_frame += frame_time;
  151. for (;;) {
  152. curtime = av_gettime();
  153. delay = c->time_frame - curtime;
  154. if (delay <= 0)
  155. break;
  156. av_usleep(delay);
  157. }
  158. pkt->pts = curtime;
  159. }
  160. #if CONFIG_LIBXCB_SHM
  161. static int check_shm(xcb_connection_t *conn)
  162. {
  163. xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
  164. xcb_shm_query_version_reply_t *reply;
  165. reply = xcb_shm_query_version_reply(conn, cookie, NULL);
  166. if (reply) {
  167. free(reply);
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static void dealloc_shm(void *unused, uint8_t *data)
  173. {
  174. shmdt(data);
  175. }
  176. static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
  177. {
  178. XCBGrabContext *c = s->priv_data;
  179. xcb_shm_get_image_cookie_t iq;
  180. xcb_shm_get_image_reply_t *img;
  181. xcb_drawable_t drawable = c->screen->root;
  182. uint8_t *data;
  183. int size = c->frame_size + FF_INPUT_BUFFER_PADDING_SIZE;
  184. int id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
  185. xcb_generic_error_t *e = NULL;
  186. if (id == -1) {
  187. char errbuf[1024];
  188. int err = AVERROR(errno);
  189. av_strerror(err, errbuf, sizeof(errbuf));
  190. av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
  191. size, errbuf);
  192. return err;
  193. }
  194. xcb_shm_attach(c->conn, c->segment, id, 0);
  195. iq = xcb_shm_get_image(c->conn, drawable,
  196. c->x, c->y, c->width, c->height, ~0,
  197. XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
  198. xcb_shm_detach(c->conn, c->segment);
  199. img = xcb_shm_get_image_reply(c->conn, iq, &e);
  200. xcb_flush(c->conn);
  201. if (e) {
  202. av_log(s, AV_LOG_ERROR,
  203. "Cannot get the image data "
  204. "event_error: response_type:%u error_code:%u "
  205. "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
  206. e->response_type, e->error_code,
  207. e->sequence, e->resource_id, e->minor_code, e->major_code);
  208. shmctl(id, IPC_RMID, 0);
  209. return AVERROR(EACCES);
  210. }
  211. free(img);
  212. data = shmat(id, NULL, 0);
  213. shmctl(id, IPC_RMID, 0);
  214. if ((intptr_t)data == -1)
  215. return AVERROR(errno);
  216. pkt->buf = av_buffer_create(data, size, dealloc_shm, NULL, 0);
  217. if (!pkt->buf) {
  218. shmdt(data);
  219. return AVERROR(ENOMEM);
  220. }
  221. pkt->data = pkt->buf->data;
  222. pkt->size = c->frame_size;
  223. return 0;
  224. }
  225. #endif /* CONFIG_LIBXCB_SHM */
  226. #if CONFIG_LIBXCB_XFIXES
  227. static int check_xfixes(xcb_connection_t *conn)
  228. {
  229. xcb_xfixes_query_version_cookie_t cookie;
  230. xcb_xfixes_query_version_reply_t *reply;
  231. cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
  232. XCB_XFIXES_MINOR_VERSION);
  233. reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
  234. if (reply) {
  235. free(reply);
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. #define BLEND(target, source, alpha) \
  241. (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
  242. static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
  243. xcb_query_pointer_reply_t *p,
  244. xcb_get_geometry_reply_t *geo)
  245. {
  246. XCBGrabContext *gr = s->priv_data;
  247. uint32_t *cursor;
  248. uint8_t *image = pkt->data;
  249. int stride = gr->bpp / 8;
  250. xcb_xfixes_get_cursor_image_cookie_t cc;
  251. xcb_xfixes_get_cursor_image_reply_t *ci;
  252. int cx, cy, x, y, w, h, c_off, i_off;
  253. cc = xcb_xfixes_get_cursor_image(gr->conn);
  254. ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
  255. if (!ci)
  256. return;
  257. cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
  258. if (!cursor)
  259. return;
  260. cx = ci->x - ci->xhot;
  261. cy = ci->y - ci->yhot;
  262. x = FFMAX(cx, gr->x);
  263. y = FFMAX(cy, gr->y);
  264. w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
  265. h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
  266. c_off = x - cx;
  267. i_off = x - gr->x;
  268. cursor += (y - cy) * ci->width;
  269. image += (y - gr->y) * gr->width * stride;
  270. for (y = 0; y < h; y++) {
  271. cursor += c_off;
  272. image += i_off * stride;
  273. for (x = 0; x < w; x++, cursor++, image += stride) {
  274. int r, g, b, a;
  275. r = *cursor & 0xff;
  276. g = (*cursor >> 8) & 0xff;
  277. b = (*cursor >> 16) & 0xff;
  278. a = (*cursor >> 24) & 0xff;
  279. if (!a)
  280. continue;
  281. if (a == 255) {
  282. image[0] = r;
  283. image[1] = g;
  284. image[2] = b;
  285. } else {
  286. image[0] = BLEND(r, image[0], a);
  287. image[1] = BLEND(g, image[1], a);
  288. image[2] = BLEND(b, image[2], a);
  289. }
  290. }
  291. cursor += ci->width - w - c_off;
  292. image += (gr->width - w - i_off) * stride;
  293. }
  294. free(ci);
  295. }
  296. #endif /* CONFIG_LIBXCB_XFIXES */
  297. static void xcbgrab_update_region(AVFormatContext *s)
  298. {
  299. XCBGrabContext *c = s->priv_data;
  300. const uint32_t args[] = { c->x - c->region_border,
  301. c->y - c->region_border };
  302. xcb_configure_window(c->conn,
  303. c->window,
  304. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
  305. args);
  306. }
  307. static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
  308. {
  309. XCBGrabContext *c = s->priv_data;
  310. xcb_query_pointer_cookie_t pc;
  311. xcb_get_geometry_cookie_t gc;
  312. xcb_query_pointer_reply_t *p = NULL;
  313. xcb_get_geometry_reply_t *geo = NULL;
  314. int ret = 0;
  315. wait_frame(s, pkt);
  316. if (c->follow_mouse || c->draw_mouse) {
  317. pc = xcb_query_pointer(c->conn, c->screen->root);
  318. gc = xcb_get_geometry(c->conn, c->screen->root);
  319. p = xcb_query_pointer_reply(c->conn, pc, NULL);
  320. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  321. }
  322. if (c->follow_mouse && p->same_screen)
  323. xcbgrab_reposition(s, p, geo);
  324. if (c->show_region)
  325. xcbgrab_update_region(s);
  326. #if CONFIG_LIBXCB_SHM
  327. if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
  328. c->has_shm = 0;
  329. #endif
  330. if (!c->has_shm)
  331. ret = xcbgrab_frame(s, pkt);
  332. #if CONFIG_LIBXCB_XFIXES
  333. if (c->draw_mouse && p->same_screen)
  334. xcbgrab_draw_mouse(s, pkt, p, geo);
  335. #endif
  336. free(p);
  337. free(geo);
  338. return ret;
  339. }
  340. static av_cold int xcbgrab_read_close(AVFormatContext *s)
  341. {
  342. XCBGrabContext *ctx = s->priv_data;
  343. xcb_disconnect(ctx->conn);
  344. return 0;
  345. }
  346. static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
  347. {
  348. xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
  349. xcb_screen_t *screen = NULL;
  350. for (; it.rem > 0; xcb_screen_next (&it)) {
  351. if (!screen_num) {
  352. screen = it.data;
  353. break;
  354. }
  355. screen_num--;
  356. }
  357. return screen;
  358. }
  359. static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
  360. int *pix_fmt)
  361. {
  362. XCBGrabContext *c = s->priv_data;
  363. const xcb_setup_t *setup = xcb_get_setup(c->conn);
  364. const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
  365. int length = xcb_setup_pixmap_formats_length(setup);
  366. *pix_fmt = 0;
  367. while (length--) {
  368. if (fmt->depth == depth) {
  369. switch (depth) {
  370. case 32:
  371. if (fmt->bits_per_pixel == 32)
  372. *pix_fmt = AV_PIX_FMT_0RGB;
  373. break;
  374. case 24:
  375. if (fmt->bits_per_pixel == 32)
  376. *pix_fmt = AV_PIX_FMT_0RGB32;
  377. else if (fmt->bits_per_pixel == 24)
  378. *pix_fmt = AV_PIX_FMT_RGB24;
  379. break;
  380. case 16:
  381. if (fmt->bits_per_pixel == 16)
  382. *pix_fmt = AV_PIX_FMT_RGB565;
  383. break;
  384. case 15:
  385. if (fmt->bits_per_pixel == 16)
  386. *pix_fmt = AV_PIX_FMT_RGB555;
  387. break;
  388. case 8:
  389. if (fmt->bits_per_pixel == 8)
  390. *pix_fmt = AV_PIX_FMT_RGB8;
  391. break;
  392. }
  393. }
  394. if (*pix_fmt) {
  395. c->bpp = fmt->bits_per_pixel;
  396. c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
  397. return 0;
  398. }
  399. fmt++;
  400. }
  401. av_log(s, AV_LOG_ERROR, "Pixmap format not mappable.\n");
  402. return AVERROR_PATCHWELCOME;
  403. }
  404. static int create_stream(AVFormatContext *s)
  405. {
  406. XCBGrabContext *c = s->priv_data;
  407. AVStream *st = avformat_new_stream(s, NULL);
  408. xcb_get_geometry_cookie_t gc;
  409. xcb_get_geometry_reply_t *geo;
  410. int ret;
  411. if (!st)
  412. return AVERROR(ENOMEM);
  413. ret = av_parse_video_size(&c->width, &c->height, c->video_size);
  414. if (ret < 0)
  415. return ret;
  416. ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
  417. if (ret < 0)
  418. return ret;
  419. avpriv_set_pts_info(st, 64, 1, 1000000);
  420. gc = xcb_get_geometry(c->conn, c->screen->root);
  421. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  422. c->width = FFMIN(geo->width, c->width);
  423. c->height = FFMIN(geo->height, c->height);
  424. c->time_base = (AVRational){ st->avg_frame_rate.den,
  425. st->avg_frame_rate.num };
  426. c->time_frame = av_gettime();
  427. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  428. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  429. st->codec->width = c->width;
  430. st->codec->height = c->height;
  431. st->codec->time_base = c->time_base;
  432. ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codec->pix_fmt);
  433. free(geo);
  434. return ret;
  435. }
  436. static void draw_rectangle(AVFormatContext *s)
  437. {
  438. XCBGrabContext *c = s->priv_data;
  439. xcb_gcontext_t gc = xcb_generate_id(c->conn);
  440. uint32_t mask = XCB_GC_FOREGROUND |
  441. XCB_GC_BACKGROUND |
  442. XCB_GC_LINE_WIDTH |
  443. XCB_GC_LINE_STYLE |
  444. XCB_GC_FILL_STYLE;
  445. uint32_t values[] = { c->screen->black_pixel,
  446. c->screen->white_pixel,
  447. c->region_border,
  448. XCB_LINE_STYLE_DOUBLE_DASH,
  449. XCB_FILL_STYLE_SOLID };
  450. xcb_rectangle_t r = { 1, 1,
  451. c->width + c->region_border * 2 - 3,
  452. c->height + c->region_border * 2 - 3 };
  453. xcb_create_gc(c->conn, gc, c->window, mask, values);
  454. xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
  455. }
  456. static void setup_window(AVFormatContext *s)
  457. {
  458. XCBGrabContext *c = s->priv_data;
  459. uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
  460. uint32_t values[] = { 1,
  461. XCB_EVENT_MASK_EXPOSURE |
  462. XCB_EVENT_MASK_STRUCTURE_NOTIFY };
  463. xcb_rectangle_t rect = { 0, 0, c->width, c->height };
  464. c->window = xcb_generate_id(c->conn);
  465. xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
  466. c->window,
  467. c->screen->root,
  468. c->x - c->region_border,
  469. c->y - c->region_border,
  470. c->width + c->region_border * 2,
  471. c->height + c->region_border * 2,
  472. 0,
  473. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  474. XCB_COPY_FROM_PARENT,
  475. mask, values);
  476. #if CONFIG_LIBXCB_SHAPE
  477. xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
  478. XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
  479. c->window,
  480. c->region_border, c->region_border,
  481. 1, &rect);
  482. #endif
  483. xcb_map_window(c->conn, c->window);
  484. draw_rectangle(s);
  485. }
  486. static av_cold int xcbgrab_read_header(AVFormatContext *s)
  487. {
  488. XCBGrabContext *c = s->priv_data;
  489. int screen_num, ret;
  490. const xcb_setup_t *setup;
  491. char *display_name = av_strdup(s->filename);
  492. if (!display_name)
  493. return AVERROR(ENOMEM);
  494. if (!sscanf(s->filename, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
  495. *display_name = 0;
  496. sscanf(s->filename, "+%d,%d", &c->x, &c->y);
  497. }
  498. c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
  499. av_freep(&display_name);
  500. if ((ret = xcb_connection_has_error(c->conn))) {
  501. av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
  502. s->filename[0] ? s->filename : "default", ret);
  503. return AVERROR(EIO);
  504. }
  505. setup = xcb_get_setup(c->conn);
  506. c->screen = get_screen(setup, screen_num);
  507. if (!c->screen) {
  508. av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
  509. screen_num);
  510. xcbgrab_read_close(s);
  511. return AVERROR(EIO);
  512. }
  513. ret = create_stream(s);
  514. if (ret < 0) {
  515. xcbgrab_read_close(s);
  516. return ret;
  517. }
  518. #if CONFIG_LIBXCB_SHM
  519. if ((c->has_shm = check_shm(c->conn)))
  520. c->segment = xcb_generate_id(c->conn);
  521. #endif
  522. #if CONFIG_LIBXCB_XFIXES
  523. if (c->draw_mouse) {
  524. if (!(c->draw_mouse = check_xfixes(c->conn))) {
  525. av_log(s, AV_LOG_WARNING,
  526. "XFixes not available, cannot draw the mouse.\n");
  527. }
  528. if (c->bpp < 24) {
  529. avpriv_report_missing_feature(s, "%d bits per pixel screen",
  530. c->bpp);
  531. c->draw_mouse = 0;
  532. }
  533. }
  534. #endif
  535. if (c->show_region)
  536. setup_window(s);
  537. return 0;
  538. }
  539. AVInputFormat ff_x11grab_xcb_demuxer = {
  540. .name = "x11grab",
  541. .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
  542. .priv_data_size = sizeof(XCBGrabContext),
  543. .read_header = xcbgrab_read_header,
  544. .read_packet = xcbgrab_read_packet,
  545. .read_close = xcbgrab_read_close,
  546. .flags = AVFMT_NOFILE,
  547. .priv_class = &xcbgrab_class,
  548. };