You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

717 lines
20KB

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