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.

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