xcbgrab.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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, int *bpp)
  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. *bpp = fmt->bits_per_pixel;
  432. return 0;
  433. }
  434. fmt++;
  435. }
  436. avpriv_report_missing_feature(s, "Mapping this pixmap format");
  437. return AVERROR_PATCHWELCOME;
  438. }
  439. static int create_stream(AVFormatContext *s)
  440. {
  441. XCBGrabContext *c = s->priv_data;
  442. AVStream *st = avformat_new_stream(s, NULL);
  443. xcb_get_geometry_cookie_t gc;
  444. xcb_get_geometry_reply_t *geo;
  445. int64_t frame_size_bits;
  446. int ret;
  447. if (!st)
  448. return AVERROR(ENOMEM);
  449. ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
  450. if (ret < 0)
  451. return ret;
  452. avpriv_set_pts_info(st, 64, 1, 1000000);
  453. gc = xcb_get_geometry(c->conn, c->screen->root);
  454. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  455. if (!geo)
  456. return AVERROR_EXTERNAL;
  457. if (!c->width || !c->height) {
  458. c->width = geo->width;
  459. c->height = geo->height;
  460. }
  461. if (c->x + c->width > geo->width ||
  462. c->y + c->height > geo->height) {
  463. av_log(s, AV_LOG_ERROR,
  464. "Capture area %dx%d at position %d.%d "
  465. "outside the screen size %dx%d\n",
  466. c->width, c->height,
  467. c->x, c->y,
  468. geo->width, geo->height);
  469. free(geo);
  470. return AVERROR(EINVAL);
  471. }
  472. c->time_base = (AVRational){ st->avg_frame_rate.den,
  473. st->avg_frame_rate.num };
  474. c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
  475. c->time_frame = av_gettime();
  476. ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
  477. free(geo);
  478. if (ret < 0)
  479. return ret;
  480. frame_size_bits = (int64_t)c->width * c->height * c->bpp;
  481. if (frame_size_bits / 8 + AV_INPUT_BUFFER_PADDING_SIZE > INT_MAX) {
  482. av_log(s, AV_LOG_ERROR, "Captured area is too large\n");
  483. return AVERROR_PATCHWELCOME;
  484. }
  485. c->frame_size = frame_size_bits / 8;
  486. #if CONFIG_LIBXCB_SHM
  487. c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE,
  488. c->conn, allocate_shm_buffer, NULL);
  489. if (!c->shm_pool)
  490. return AVERROR(ENOMEM);
  491. #endif
  492. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  493. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  494. st->codecpar->width = c->width;
  495. st->codecpar->height = c->height;
  496. st->codecpar->bit_rate = av_rescale(frame_size_bits, st->avg_frame_rate.num, st->avg_frame_rate.den);
  497. return ret;
  498. }
  499. static void draw_rectangle(AVFormatContext *s)
  500. {
  501. XCBGrabContext *c = s->priv_data;
  502. xcb_gcontext_t gc = xcb_generate_id(c->conn);
  503. uint32_t mask = XCB_GC_FOREGROUND |
  504. XCB_GC_BACKGROUND |
  505. XCB_GC_LINE_WIDTH |
  506. XCB_GC_LINE_STYLE |
  507. XCB_GC_FILL_STYLE;
  508. uint32_t values[] = { c->screen->black_pixel,
  509. c->screen->white_pixel,
  510. c->region_border,
  511. XCB_LINE_STYLE_DOUBLE_DASH,
  512. XCB_FILL_STYLE_SOLID };
  513. xcb_rectangle_t r = { 1, 1,
  514. c->width + c->region_border * 2 - 3,
  515. c->height + c->region_border * 2 - 3 };
  516. xcb_create_gc(c->conn, gc, c->window, mask, values);
  517. xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
  518. }
  519. static void setup_window(AVFormatContext *s)
  520. {
  521. XCBGrabContext *c = s->priv_data;
  522. uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
  523. uint32_t values[] = { 1,
  524. XCB_EVENT_MASK_EXPOSURE |
  525. XCB_EVENT_MASK_STRUCTURE_NOTIFY };
  526. av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
  527. c->window = xcb_generate_id(c->conn);
  528. xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
  529. c->window,
  530. c->screen->root,
  531. c->x - c->region_border,
  532. c->y - c->region_border,
  533. c->width + c->region_border * 2,
  534. c->height + c->region_border * 2,
  535. 0,
  536. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  537. XCB_COPY_FROM_PARENT,
  538. mask, values);
  539. #if CONFIG_LIBXCB_SHAPE
  540. xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
  541. XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
  542. c->window,
  543. c->region_border, c->region_border,
  544. 1, &rect);
  545. #endif
  546. xcb_map_window(c->conn, c->window);
  547. draw_rectangle(s);
  548. }
  549. static av_cold int xcbgrab_read_header(AVFormatContext *s)
  550. {
  551. XCBGrabContext *c = s->priv_data;
  552. int screen_num, ret;
  553. const xcb_setup_t *setup;
  554. char *display_name = av_strdup(s->url);
  555. if (!display_name)
  556. return AVERROR(ENOMEM);
  557. if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
  558. *display_name = 0;
  559. if(sscanf(s->url, "+%d,%d", &c->x, &c->y) != 2) {
  560. if (*s->url)
  561. av_log(s, AV_LOG_WARNING, "Ambigous URL: %s\n", s->url);
  562. }
  563. }
  564. c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
  565. av_freep(&display_name);
  566. if ((ret = xcb_connection_has_error(c->conn))) {
  567. av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
  568. s->url[0] ? s->url : "default", ret);
  569. return AVERROR(EIO);
  570. }
  571. setup = xcb_get_setup(c->conn);
  572. c->screen = get_screen(setup, screen_num);
  573. if (!c->screen) {
  574. av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
  575. screen_num);
  576. xcbgrab_read_close(s);
  577. return AVERROR(EIO);
  578. }
  579. ret = create_stream(s);
  580. if (ret < 0) {
  581. xcbgrab_read_close(s);
  582. return ret;
  583. }
  584. #if CONFIG_LIBXCB_SHM
  585. c->has_shm = check_shm(c->conn);
  586. #endif
  587. #if CONFIG_LIBXCB_XFIXES
  588. if (c->draw_mouse) {
  589. if (!(c->draw_mouse = check_xfixes(c->conn))) {
  590. av_log(s, AV_LOG_WARNING,
  591. "XFixes not available, cannot draw the mouse.\n");
  592. }
  593. if (c->bpp < 24) {
  594. avpriv_report_missing_feature(s, "%d bits per pixel screen",
  595. c->bpp);
  596. c->draw_mouse = 0;
  597. }
  598. }
  599. #endif
  600. if (c->show_region)
  601. setup_window(s);
  602. return 0;
  603. }
  604. AVInputFormat ff_xcbgrab_demuxer = {
  605. .name = "x11grab",
  606. .long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
  607. .priv_data_size = sizeof(XCBGrabContext),
  608. .read_header = xcbgrab_read_header,
  609. .read_packet = xcbgrab_read_packet,
  610. .read_close = xcbgrab_read_close,
  611. .flags = AVFMT_NOFILE,
  612. .priv_class = &xcbgrab_class,
  613. };