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.

660 lines
18KB

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