xcbgrab.c 20 KB

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