xcbgrab.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 "libavformat/avformat.h"
  35. #include "libavformat/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/parseutils.h"
  39. #include "libavutil/time.h"
  40. typedef struct XCBGrabContext {
  41. const AVClass *class;
  42. xcb_connection_t *conn;
  43. xcb_screen_t *screen;
  44. xcb_window_t window;
  45. #if CONFIG_LIBXCB_SHM
  46. xcb_shm_seg_t segment;
  47. #endif
  48. int64_t time_frame;
  49. AVRational time_base;
  50. int x, y;
  51. int width, height;
  52. int frame_size;
  53. int bpp;
  54. int draw_mouse;
  55. int follow_mouse;
  56. int show_region;
  57. int region_border;
  58. int centered;
  59. const char *video_size;
  60. const char *framerate;
  61. int has_shm;
  62. } XCBGrabContext;
  63. #define FOLLOW_CENTER -1
  64. #define OFFSET(x) offsetof(XCBGrabContext, x)
  65. #define D AV_OPT_FLAG_DECODING_PARAM
  66. static const AVOption options[] = {
  67. { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  68. { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  69. { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  70. { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  71. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "vga" }, 0, 0, D },
  72. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
  73. { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
  74. { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
  75. OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, "follow_mouse" },
  76. { "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" },
  77. { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
  78. { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
  79. { NULL },
  80. };
  81. static const AVClass xcbgrab_class = {
  82. .class_name = "xcbgrab indev",
  83. .item_name = av_default_item_name,
  84. .option = options,
  85. .version = LIBAVUTIL_VERSION_INT,
  86. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  87. };
  88. static int xcbgrab_reposition(AVFormatContext *s,
  89. xcb_query_pointer_reply_t *p,
  90. xcb_get_geometry_reply_t *geo)
  91. {
  92. XCBGrabContext *c = s->priv_data;
  93. int x = c->x, y = c->y;
  94. int w = c->width, h = c->height, f = c->follow_mouse;
  95. int p_x, p_y;
  96. if (!p || !geo)
  97. return AVERROR(EIO);
  98. p_x = p->win_x;
  99. p_y = p->win_y;
  100. if (f == FOLLOW_CENTER) {
  101. x = p_x - w / 2;
  102. y = p_y - h / 2;
  103. } else {
  104. int left = x + f;
  105. int right = x + w - f;
  106. int top = y + f;
  107. int bottom = y + h + f;
  108. if (p_x > right) {
  109. x += p_x - right;
  110. } else if (p_x < left) {
  111. x -= left - p_x;
  112. }
  113. if (p_y > bottom) {
  114. y += p_y - bottom;
  115. } else if (p_y < top) {
  116. y -= top - p_y;
  117. }
  118. }
  119. c->x = FFMIN(FFMAX(0, x), geo->width - w);
  120. c->y = FFMIN(FFMAX(0, y), geo->height - h);
  121. return 0;
  122. }
  123. static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
  124. {
  125. XCBGrabContext *c = s->priv_data;
  126. xcb_get_image_cookie_t iq;
  127. xcb_get_image_reply_t *img;
  128. xcb_drawable_t drawable = c->screen->root;
  129. xcb_generic_error_t *e = NULL;
  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, &e);
  135. if (e) {
  136. av_log(s, AV_LOG_ERROR,
  137. "Cannot get the image data "
  138. "event_error: response_type:%u error_code:%u "
  139. "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
  140. e->response_type, e->error_code,
  141. e->sequence, e->resource_id, e->minor_code, e->major_code);
  142. return AVERROR(EACCES);
  143. }
  144. if (!img)
  145. return AVERROR(EAGAIN);
  146. data = xcb_get_image_data(img);
  147. length = xcb_get_image_data_length(img);
  148. ret = av_new_packet(pkt, length);
  149. if (!ret)
  150. memcpy(pkt->data, data, length);
  151. free(img);
  152. return ret;
  153. }
  154. static void wait_frame(AVFormatContext *s, AVPacket *pkt)
  155. {
  156. XCBGrabContext *c = s->priv_data;
  157. int64_t curtime, delay;
  158. int64_t frame_time = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
  159. c->time_frame += frame_time;
  160. for (;;) {
  161. curtime = av_gettime();
  162. delay = c->time_frame - curtime;
  163. if (delay <= 0)
  164. break;
  165. av_usleep(delay);
  166. }
  167. pkt->pts = curtime;
  168. }
  169. #if CONFIG_LIBXCB_SHM
  170. static int check_shm(xcb_connection_t *conn)
  171. {
  172. xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
  173. xcb_shm_query_version_reply_t *reply;
  174. reply = xcb_shm_query_version_reply(conn, cookie, NULL);
  175. if (reply) {
  176. free(reply);
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. static void dealloc_shm(void *unused, uint8_t *data)
  182. {
  183. shmdt(data);
  184. }
  185. static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
  186. {
  187. XCBGrabContext *c = s->priv_data;
  188. xcb_shm_get_image_cookie_t iq;
  189. xcb_shm_get_image_reply_t *img;
  190. xcb_drawable_t drawable = c->screen->root;
  191. uint8_t *data;
  192. int size = c->frame_size + FF_INPUT_BUFFER_PADDING_SIZE;
  193. int id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
  194. xcb_generic_error_t *e = NULL;
  195. if (id == -1) {
  196. char errbuf[1024];
  197. int err = AVERROR(errno);
  198. av_strerror(err, errbuf, sizeof(errbuf));
  199. av_log(s, AV_LOG_ERROR, "Cannot get %d bytes of shared memory: %s.\n",
  200. size, errbuf);
  201. return err;
  202. }
  203. xcb_shm_attach(c->conn, c->segment, id, 0);
  204. iq = xcb_shm_get_image(c->conn, drawable,
  205. c->x, c->y, c->width, c->height, ~0,
  206. XCB_IMAGE_FORMAT_Z_PIXMAP, c->segment, 0);
  207. xcb_shm_detach(c->conn, c->segment);
  208. img = xcb_shm_get_image_reply(c->conn, iq, &e);
  209. xcb_flush(c->conn);
  210. if (e) {
  211. av_log(s, AV_LOG_ERROR,
  212. "Cannot get the image data "
  213. "event_error: response_type:%u error_code:%u "
  214. "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
  215. e->response_type, e->error_code,
  216. e->sequence, e->resource_id, e->minor_code, e->major_code);
  217. shmctl(id, IPC_RMID, 0);
  218. return AVERROR(EACCES);
  219. }
  220. free(img);
  221. data = shmat(id, NULL, 0);
  222. shmctl(id, IPC_RMID, 0);
  223. if ((intptr_t)data == -1)
  224. return AVERROR(errno);
  225. pkt->buf = av_buffer_create(data, size, dealloc_shm, NULL, 0);
  226. if (!pkt->buf) {
  227. shmdt(data);
  228. return AVERROR(ENOMEM);
  229. }
  230. pkt->data = pkt->buf->data;
  231. pkt->size = c->frame_size;
  232. return 0;
  233. }
  234. #endif /* CONFIG_LIBXCB_SHM */
  235. #if CONFIG_LIBXCB_XFIXES
  236. static int check_xfixes(xcb_connection_t *conn)
  237. {
  238. xcb_xfixes_query_version_cookie_t cookie;
  239. xcb_xfixes_query_version_reply_t *reply;
  240. cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
  241. XCB_XFIXES_MINOR_VERSION);
  242. reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
  243. if (reply) {
  244. free(reply);
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. #define BLEND(target, source, alpha) \
  250. (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
  251. static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
  252. xcb_query_pointer_reply_t *p,
  253. xcb_get_geometry_reply_t *geo)
  254. {
  255. XCBGrabContext *gr = s->priv_data;
  256. uint32_t *cursor;
  257. uint8_t *image = pkt->data;
  258. int stride = gr->bpp / 8;
  259. xcb_xfixes_get_cursor_image_cookie_t cc;
  260. xcb_xfixes_get_cursor_image_reply_t *ci;
  261. int cx, cy, x, y, w, h, c_off, i_off;
  262. cc = xcb_xfixes_get_cursor_image(gr->conn);
  263. ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
  264. if (!ci)
  265. return;
  266. cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
  267. if (!cursor)
  268. return;
  269. cx = ci->x - ci->xhot;
  270. cy = ci->y - ci->yhot;
  271. x = FFMAX(cx, gr->x);
  272. y = FFMAX(cy, gr->y);
  273. w = FFMIN(cx + ci->width, gr->x + gr->width) - x;
  274. h = FFMIN(cy + ci->height, gr->y + gr->height) - y;
  275. c_off = x - cx;
  276. i_off = x - gr->x;
  277. cursor += (y - cy) * ci->width;
  278. image += (y - gr->y) * gr->width * stride;
  279. for (y = 0; y < h; y++) {
  280. cursor += c_off;
  281. image += i_off * stride;
  282. for (x = 0; x < w; x++, cursor++, image += stride) {
  283. int r, g, b, a;
  284. r = *cursor & 0xff;
  285. g = (*cursor >> 8) & 0xff;
  286. b = (*cursor >> 16) & 0xff;
  287. a = (*cursor >> 24) & 0xff;
  288. if (!a)
  289. continue;
  290. if (a == 255) {
  291. image[0] = r;
  292. image[1] = g;
  293. image[2] = b;
  294. } else {
  295. image[0] = BLEND(r, image[0], a);
  296. image[1] = BLEND(g, image[1], a);
  297. image[2] = BLEND(b, image[2], a);
  298. }
  299. }
  300. cursor += ci->width - w - c_off;
  301. image += (gr->width - w - i_off) * stride;
  302. }
  303. free(ci);
  304. }
  305. #endif /* CONFIG_LIBXCB_XFIXES */
  306. static void xcbgrab_update_region(AVFormatContext *s)
  307. {
  308. XCBGrabContext *c = s->priv_data;
  309. const uint32_t args[] = { c->x - c->region_border,
  310. c->y - c->region_border };
  311. xcb_configure_window(c->conn,
  312. c->window,
  313. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
  314. args);
  315. }
  316. static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
  317. {
  318. XCBGrabContext *c = s->priv_data;
  319. xcb_query_pointer_cookie_t pc;
  320. xcb_get_geometry_cookie_t gc;
  321. xcb_query_pointer_reply_t *p = NULL;
  322. xcb_get_geometry_reply_t *geo = NULL;
  323. int ret = 0;
  324. wait_frame(s, pkt);
  325. if (c->follow_mouse || c->draw_mouse) {
  326. pc = xcb_query_pointer(c->conn, c->screen->root);
  327. gc = xcb_get_geometry(c->conn, c->screen->root);
  328. p = xcb_query_pointer_reply(c->conn, pc, NULL);
  329. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  330. }
  331. if (c->follow_mouse && p->same_screen)
  332. xcbgrab_reposition(s, p, geo);
  333. if (c->show_region)
  334. xcbgrab_update_region(s);
  335. #if CONFIG_LIBXCB_SHM
  336. if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0)
  337. c->has_shm = 0;
  338. #endif
  339. if (!c->has_shm)
  340. ret = xcbgrab_frame(s, pkt);
  341. #if CONFIG_LIBXCB_XFIXES
  342. if (ret >= 0 && c->draw_mouse && p->same_screen)
  343. xcbgrab_draw_mouse(s, pkt, p, geo);
  344. #endif
  345. free(p);
  346. free(geo);
  347. return ret;
  348. }
  349. static av_cold int xcbgrab_read_close(AVFormatContext *s)
  350. {
  351. XCBGrabContext *ctx = s->priv_data;
  352. xcb_disconnect(ctx->conn);
  353. return 0;
  354. }
  355. static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
  356. {
  357. xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
  358. xcb_screen_t *screen = NULL;
  359. for (; it.rem > 0; xcb_screen_next (&it)) {
  360. if (!screen_num) {
  361. screen = it.data;
  362. break;
  363. }
  364. screen_num--;
  365. }
  366. return screen;
  367. }
  368. static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
  369. int *pix_fmt)
  370. {
  371. XCBGrabContext *c = s->priv_data;
  372. const xcb_setup_t *setup = xcb_get_setup(c->conn);
  373. const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
  374. int length = xcb_setup_pixmap_formats_length(setup);
  375. *pix_fmt = 0;
  376. while (length--) {
  377. if (fmt->depth == depth) {
  378. switch (depth) {
  379. case 32:
  380. if (fmt->bits_per_pixel == 32)
  381. *pix_fmt = AV_PIX_FMT_0RGB;
  382. break;
  383. case 24:
  384. if (fmt->bits_per_pixel == 32)
  385. *pix_fmt = AV_PIX_FMT_0RGB32;
  386. else if (fmt->bits_per_pixel == 24)
  387. *pix_fmt = AV_PIX_FMT_RGB24;
  388. break;
  389. case 16:
  390. if (fmt->bits_per_pixel == 16)
  391. *pix_fmt = AV_PIX_FMT_RGB565;
  392. break;
  393. case 15:
  394. if (fmt->bits_per_pixel == 16)
  395. *pix_fmt = AV_PIX_FMT_RGB555;
  396. break;
  397. case 8:
  398. if (fmt->bits_per_pixel == 8)
  399. *pix_fmt = AV_PIX_FMT_RGB8;
  400. break;
  401. }
  402. }
  403. if (*pix_fmt) {
  404. c->bpp = fmt->bits_per_pixel;
  405. c->frame_size = c->width * c->height * fmt->bits_per_pixel / 8;
  406. return 0;
  407. }
  408. fmt++;
  409. }
  410. av_log(s, AV_LOG_ERROR, "Pixmap format not mappable.\n");
  411. return AVERROR_PATCHWELCOME;
  412. }
  413. static int create_stream(AVFormatContext *s)
  414. {
  415. XCBGrabContext *c = s->priv_data;
  416. AVStream *st = avformat_new_stream(s, NULL);
  417. xcb_get_geometry_cookie_t gc;
  418. xcb_get_geometry_reply_t *geo;
  419. int ret;
  420. if (!st)
  421. return AVERROR(ENOMEM);
  422. ret = av_parse_video_size(&c->width, &c->height, c->video_size);
  423. if (ret < 0)
  424. return ret;
  425. ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
  426. if (ret < 0)
  427. return ret;
  428. avpriv_set_pts_info(st, 64, 1, 1000000);
  429. gc = xcb_get_geometry(c->conn, c->screen->root);
  430. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  431. if (c->x + c->width > geo->width ||
  432. c->y + c->height > geo->height) {
  433. av_log(s, AV_LOG_ERROR,
  434. "Capture area %dx%d at position %d.%d "
  435. "outside the screen size %dx%d\n",
  436. c->width, c->height,
  437. c->x, c->y,
  438. geo->width, geo->height);
  439. return AVERROR(EINVAL);
  440. }
  441. c->time_base = (AVRational){ st->avg_frame_rate.den,
  442. st->avg_frame_rate.num };
  443. c->time_frame = av_gettime();
  444. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  445. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  446. st->codec->width = c->width;
  447. st->codec->height = c->height;
  448. st->codec->time_base = c->time_base;
  449. ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codec->pix_fmt);
  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. 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. };