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.

736 lines
21KB

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