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