xcbgrab.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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 <string.h>
  24. #include <xcb/xcb.h>
  25. #if CONFIG_LIBXCB_XFIXES
  26. #include <xcb/xfixes.h>
  27. #endif
  28. #if CONFIG_LIBXCB_SHM
  29. #include <sys/shm.h>
  30. #include <xcb/shm.h>
  31. #endif
  32. #if CONFIG_LIBXCB_SHAPE
  33. #include <xcb/shape.h>
  34. #endif
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/parseutils.h"
  39. #include "libavutil/time.h"
  40. #include "libavformat/avformat.h"
  41. #include "libavformat/demux.h"
  42. #include "libavformat/internal.h"
  43. typedef struct XCBGrabContext {
  44. const AVClass *class;
  45. xcb_connection_t *conn;
  46. xcb_screen_t *screen;
  47. xcb_window_t window;
  48. #if CONFIG_LIBXCB_SHM
  49. AVBufferPool *shm_pool;
  50. #endif
  51. int64_t time_frame;
  52. AVRational time_base;
  53. int64_t frame_duration;
  54. xcb_window_t window_id;
  55. int x, y;
  56. int width, height;
  57. int frame_size;
  58. int bpp;
  59. int draw_mouse;
  60. int follow_mouse;
  61. int show_region;
  62. int region_border;
  63. int centered;
  64. int select_region;
  65. const char *framerate;
  66. int has_shm;
  67. } XCBGrabContext;
  68. #define FOLLOW_CENTER -1
  69. #define OFFSET(x) offsetof(XCBGrabContext, x)
  70. #define D AV_OPT_FLAG_DECODING_PARAM
  71. static const AVOption options[] = {
  72. { "window_id", "Window to capture.", OFFSET(window_id), AV_OPT_TYPE_INT, { .i64 = XCB_NONE }, 0, UINT32_MAX, D },
  73. { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  74. { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  75. { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  76. { "grab_y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  77. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL }, 0, 0, D },
  78. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc" }, 0, 0, D },
  79. { "draw_mouse", "Draw the mouse pointer.", OFFSET(draw_mouse), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
  80. { "follow_mouse", "Move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region.",
  81. OFFSET(follow_mouse), AV_OPT_TYPE_INT, { .i64 = 0 }, FOLLOW_CENTER, INT_MAX, D, .unit = "follow_mouse" },
  82. { "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, .unit = "follow_mouse" },
  83. { "show_region", "Show the grabbing region.", OFFSET(show_region), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
  84. { "region_border", "Set the region border thickness.", OFFSET(region_border), AV_OPT_TYPE_INT, { .i64 = 3 }, 1, 128, D },
  85. { "select_region", "Select the grabbing region graphically using the pointer.", OFFSET(select_region), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
  86. { NULL },
  87. };
  88. static const AVClass xcbgrab_class = {
  89. .class_name = "xcbgrab indev",
  90. .item_name = av_default_item_name,
  91. .option = options,
  92. .version = LIBAVUTIL_VERSION_INT,
  93. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  94. };
  95. static int xcbgrab_reposition(AVFormatContext *s,
  96. xcb_query_pointer_reply_t *p,
  97. xcb_get_geometry_reply_t *geo)
  98. {
  99. XCBGrabContext *c = s->priv_data;
  100. int x = c->x, y = c->y;
  101. int w = c->width, h = c->height, f = c->follow_mouse;
  102. int p_x, p_y;
  103. if (!p || !geo)
  104. return AVERROR(EIO);
  105. p_x = p->win_x;
  106. p_y = p->win_y;
  107. if (f == FOLLOW_CENTER) {
  108. x = p_x - w / 2;
  109. y = p_y - h / 2;
  110. } else {
  111. int left = x + f;
  112. int right = x + w - f;
  113. int top = y + f;
  114. int bottom = y + h - f;
  115. if (p_x > right) {
  116. x += p_x - right;
  117. } else if (p_x < left) {
  118. x -= left - p_x;
  119. }
  120. if (p_y > bottom) {
  121. y += p_y - bottom;
  122. } else if (p_y < top) {
  123. y -= top - p_y;
  124. }
  125. }
  126. c->x = FFMIN(FFMAX(0, x), geo->width - w);
  127. c->y = FFMIN(FFMAX(0, y), geo->height - h);
  128. return 0;
  129. }
  130. static void xcbgrab_image_reply_free(void *opaque, uint8_t *data)
  131. {
  132. free(opaque);
  133. }
  134. static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
  135. {
  136. XCBGrabContext *c = s->priv_data;
  137. xcb_get_image_cookie_t iq;
  138. xcb_get_image_reply_t *img;
  139. xcb_drawable_t drawable = c->window_id;
  140. xcb_generic_error_t *e = NULL;
  141. uint8_t *data;
  142. int length;
  143. iq = xcb_get_image(c->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
  144. c->x, c->y, c->width, c->height, ~0);
  145. img = xcb_get_image_reply(c->conn, iq, &e);
  146. if (e) {
  147. av_log(s, AV_LOG_ERROR,
  148. "Cannot get the image data "
  149. "event_error: response_type:%u error_code:%u "
  150. "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
  151. e->response_type, e->error_code,
  152. e->sequence, e->resource_id, e->minor_code, e->major_code);
  153. free(e);
  154. return AVERROR(EACCES);
  155. }
  156. if (!img)
  157. return AVERROR(EAGAIN);
  158. data = xcb_get_image_data(img);
  159. length = xcb_get_image_data_length(img);
  160. pkt->buf = av_buffer_create(data, length, xcbgrab_image_reply_free, img, 0);
  161. if (!pkt->buf) {
  162. free(img);
  163. return AVERROR(ENOMEM);
  164. }
  165. pkt->data = data;
  166. pkt->size = length;
  167. return 0;
  168. }
  169. static int64_t wait_frame(AVFormatContext *s, AVPacket *pkt)
  170. {
  171. XCBGrabContext *c = s->priv_data;
  172. int64_t curtime, delay;
  173. c->time_frame += c->frame_duration;
  174. for (;;) {
  175. curtime = av_gettime_relative();
  176. delay = c->time_frame - curtime;
  177. if (delay <= 0)
  178. break;
  179. av_usleep(delay);
  180. }
  181. return curtime;
  182. }
  183. #if CONFIG_LIBXCB_SHM
  184. static int check_shm(xcb_connection_t *conn)
  185. {
  186. xcb_shm_query_version_cookie_t cookie = xcb_shm_query_version(conn);
  187. xcb_shm_query_version_reply_t *reply;
  188. reply = xcb_shm_query_version_reply(conn, cookie, NULL);
  189. if (reply) {
  190. free(reply);
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. static void free_shm_buffer(void *opaque, uint8_t *data)
  196. {
  197. shmdt(data);
  198. }
  199. static AVBufferRef *allocate_shm_buffer(void *opaque, size_t size)
  200. {
  201. xcb_connection_t *conn = opaque;
  202. xcb_shm_seg_t segment;
  203. AVBufferRef *ref;
  204. uint8_t *data;
  205. int id;
  206. id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
  207. if (id == -1)
  208. return NULL;
  209. segment = xcb_generate_id(conn);
  210. xcb_shm_attach(conn, segment, id, 0);
  211. data = shmat(id, NULL, 0);
  212. shmctl(id, IPC_RMID, 0);
  213. if ((intptr_t)data == -1 || !data)
  214. return NULL;
  215. ref = av_buffer_create(data, size, free_shm_buffer, (void *)(ptrdiff_t)segment, 0);
  216. if (!ref)
  217. shmdt(data);
  218. return ref;
  219. }
  220. static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
  221. {
  222. XCBGrabContext *c = s->priv_data;
  223. xcb_shm_get_image_cookie_t iq;
  224. xcb_shm_get_image_reply_t *img;
  225. xcb_drawable_t drawable = c->window_id;
  226. xcb_generic_error_t *e = NULL;
  227. AVBufferRef *buf;
  228. xcb_shm_seg_t segment;
  229. buf = av_buffer_pool_get(c->shm_pool);
  230. if (!buf) {
  231. av_log(s, AV_LOG_ERROR, "Could not get shared memory buffer.\n");
  232. return AVERROR(ENOMEM);
  233. }
  234. segment = (xcb_shm_seg_t)(uintptr_t)av_buffer_pool_buffer_get_opaque(buf);
  235. iq = xcb_shm_get_image(c->conn, drawable,
  236. c->x, c->y, c->width, c->height, ~0,
  237. XCB_IMAGE_FORMAT_Z_PIXMAP, segment, 0);
  238. img = xcb_shm_get_image_reply(c->conn, iq, &e);
  239. xcb_flush(c->conn);
  240. if (e) {
  241. av_log(s, AV_LOG_ERROR,
  242. "Cannot get the image data "
  243. "event_error: response_type:%u error_code:%u "
  244. "sequence:%u resource_id:%u minor_code:%u major_code:%u.\n",
  245. e->response_type, e->error_code,
  246. e->sequence, e->resource_id, e->minor_code, e->major_code);
  247. free(e);
  248. av_buffer_unref(&buf);
  249. return AVERROR(EACCES);
  250. }
  251. free(img);
  252. pkt->buf = buf;
  253. pkt->data = buf->data;
  254. pkt->size = c->frame_size;
  255. return 0;
  256. }
  257. #endif /* CONFIG_LIBXCB_SHM */
  258. #if CONFIG_LIBXCB_XFIXES
  259. static int check_xfixes(xcb_connection_t *conn)
  260. {
  261. xcb_xfixes_query_version_cookie_t cookie;
  262. xcb_xfixes_query_version_reply_t *reply;
  263. cookie = xcb_xfixes_query_version(conn, XCB_XFIXES_MAJOR_VERSION,
  264. XCB_XFIXES_MINOR_VERSION);
  265. reply = xcb_xfixes_query_version_reply(conn, cookie, NULL);
  266. if (reply) {
  267. free(reply);
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. #define BLEND(target, source, alpha) \
  273. (target) + ((source) * (255 - (alpha)) + 255 / 2) / 255
  274. static void xcbgrab_draw_mouse(AVFormatContext *s, AVPacket *pkt,
  275. xcb_query_pointer_reply_t *p,
  276. xcb_get_geometry_reply_t *geo,
  277. int win_x, int win_y)
  278. {
  279. XCBGrabContext *gr = s->priv_data;
  280. uint32_t *cursor;
  281. uint8_t *image = pkt->data;
  282. int stride = gr->bpp / 8;
  283. xcb_xfixes_get_cursor_image_cookie_t cc;
  284. xcb_xfixes_get_cursor_image_reply_t *ci;
  285. int cx, cy, x, y, w, h, c_off, i_off;
  286. cc = xcb_xfixes_get_cursor_image(gr->conn);
  287. ci = xcb_xfixes_get_cursor_image_reply(gr->conn, cc, NULL);
  288. if (!ci)
  289. return;
  290. cursor = xcb_xfixes_get_cursor_image_cursor_image(ci);
  291. if (!cursor)
  292. return;
  293. cx = ci->x - ci->xhot;
  294. cy = ci->y - ci->yhot;
  295. x = FFMAX(cx, win_x + gr->x);
  296. y = FFMAX(cy, win_y + gr->y);
  297. w = FFMIN(cx + ci->width, win_x + gr->x + gr->width) - x;
  298. h = FFMIN(cy + ci->height, win_y + gr->y + gr->height) - y;
  299. c_off = x - cx;
  300. i_off = x - gr->x - win_x;
  301. cursor += (y - cy) * ci->width;
  302. image += (y - gr->y - win_y) * gr->width * stride;
  303. for (y = 0; y < h; y++) {
  304. cursor += c_off;
  305. image += i_off * stride;
  306. for (x = 0; x < w; x++, cursor++, image += stride) {
  307. int r, g, b, a;
  308. r = *cursor & 0xff;
  309. g = (*cursor >> 8) & 0xff;
  310. b = (*cursor >> 16) & 0xff;
  311. a = (*cursor >> 24) & 0xff;
  312. if (!a)
  313. continue;
  314. if (a == 255) {
  315. image[0] = r;
  316. image[1] = g;
  317. image[2] = b;
  318. } else {
  319. image[0] = BLEND(r, image[0], a);
  320. image[1] = BLEND(g, image[1], a);
  321. image[2] = BLEND(b, image[2], a);
  322. }
  323. }
  324. cursor += ci->width - w - c_off;
  325. image += (gr->width - w - i_off) * stride;
  326. }
  327. free(ci);
  328. }
  329. #endif /* CONFIG_LIBXCB_XFIXES */
  330. static void xcbgrab_update_region(AVFormatContext *s, int win_x, int win_y)
  331. {
  332. XCBGrabContext *c = s->priv_data;
  333. const uint32_t args[] = { win_x + c->x - c->region_border,
  334. win_y + c->y - c->region_border };
  335. xcb_configure_window(c->conn,
  336. c->window,
  337. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
  338. args);
  339. }
  340. static int xcbgrab_read_packet(AVFormatContext *s, AVPacket *pkt)
  341. {
  342. XCBGrabContext *c = s->priv_data;
  343. xcb_query_pointer_cookie_t pc;
  344. xcb_get_geometry_cookie_t gc;
  345. xcb_translate_coordinates_cookie_t tc;
  346. xcb_query_pointer_reply_t *p = NULL;
  347. xcb_get_geometry_reply_t *geo = NULL;
  348. xcb_translate_coordinates_reply_t *translate = NULL;
  349. int ret = 0;
  350. int64_t pts;
  351. int win_x = 0, win_y = 0;
  352. wait_frame(s, pkt);
  353. pts = av_gettime();
  354. if (c->follow_mouse || c->draw_mouse) {
  355. pc = xcb_query_pointer(c->conn, c->window_id);
  356. gc = xcb_get_geometry(c->conn, c->window_id);
  357. p = xcb_query_pointer_reply(c->conn, pc, NULL);
  358. if (!p) {
  359. av_log(s, AV_LOG_ERROR, "Failed to query xcb pointer\n");
  360. return AVERROR_EXTERNAL;
  361. }
  362. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  363. if (!geo) {
  364. av_log(s, AV_LOG_ERROR, "Failed to get xcb geometry\n");
  365. free(p);
  366. return AVERROR_EXTERNAL;
  367. }
  368. }
  369. if (c->window_id != c->screen->root) {
  370. tc = xcb_translate_coordinates(c->conn, c->window_id, c->screen->root, 0, 0);
  371. translate = xcb_translate_coordinates_reply(c->conn, tc, NULL);
  372. if (!translate) {
  373. free(p);
  374. free(geo);
  375. av_log(s, AV_LOG_ERROR, "Failed to translate xcb geometry\n");
  376. return AVERROR_EXTERNAL;
  377. }
  378. win_x = translate->dst_x;
  379. win_y = translate->dst_y;
  380. free(translate);
  381. }
  382. if (c->follow_mouse && p->same_screen)
  383. xcbgrab_reposition(s, p, geo);
  384. if (c->show_region)
  385. xcbgrab_update_region(s, win_x, win_y);
  386. #if CONFIG_LIBXCB_SHM
  387. if (c->has_shm && xcbgrab_frame_shm(s, pkt) < 0) {
  388. av_log(s, AV_LOG_WARNING, "Continuing without shared memory.\n");
  389. c->has_shm = 0;
  390. }
  391. #endif
  392. if (!c->has_shm)
  393. ret = xcbgrab_frame(s, pkt);
  394. pkt->dts = pkt->pts = pts;
  395. pkt->duration = c->frame_duration;
  396. #if CONFIG_LIBXCB_XFIXES
  397. if (ret >= 0 && c->draw_mouse && p->same_screen)
  398. xcbgrab_draw_mouse(s, pkt, p, geo, win_x, win_y);
  399. #endif
  400. free(p);
  401. free(geo);
  402. return ret;
  403. }
  404. static av_cold int xcbgrab_read_close(AVFormatContext *s)
  405. {
  406. XCBGrabContext *ctx = s->priv_data;
  407. #if CONFIG_LIBXCB_SHM
  408. av_buffer_pool_uninit(&ctx->shm_pool);
  409. #endif
  410. xcb_disconnect(ctx->conn);
  411. return 0;
  412. }
  413. static xcb_screen_t *get_screen(const xcb_setup_t *setup, int screen_num)
  414. {
  415. xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
  416. xcb_screen_t *screen = NULL;
  417. for (; it.rem > 0; xcb_screen_next (&it)) {
  418. if (!screen_num) {
  419. screen = it.data;
  420. break;
  421. }
  422. screen_num--;
  423. }
  424. return screen;
  425. }
  426. static int pixfmt_from_pixmap_format(AVFormatContext *s, int depth,
  427. int *pix_fmt, int *bpp)
  428. {
  429. XCBGrabContext *c = s->priv_data;
  430. const xcb_setup_t *setup = xcb_get_setup(c->conn);
  431. const xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
  432. int length = xcb_setup_pixmap_formats_length(setup);
  433. *pix_fmt = 0;
  434. while (length--) {
  435. if (fmt->depth == depth) {
  436. switch (depth) {
  437. case 32:
  438. if (fmt->bits_per_pixel == 32)
  439. *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
  440. AV_PIX_FMT_BGR0 : AV_PIX_FMT_0RGB;
  441. break;
  442. case 24:
  443. if (fmt->bits_per_pixel == 32)
  444. *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
  445. AV_PIX_FMT_BGR0 : AV_PIX_FMT_0RGB;
  446. else if (fmt->bits_per_pixel == 24)
  447. *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
  448. AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
  449. break;
  450. case 16:
  451. if (fmt->bits_per_pixel == 16)
  452. *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
  453. AV_PIX_FMT_RGB565LE : AV_PIX_FMT_RGB565BE;
  454. break;
  455. case 15:
  456. if (fmt->bits_per_pixel == 16)
  457. *pix_fmt = setup->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST ?
  458. AV_PIX_FMT_RGB555LE : AV_PIX_FMT_RGB555BE;
  459. break;
  460. case 8:
  461. if (fmt->bits_per_pixel == 8)
  462. *pix_fmt = AV_PIX_FMT_RGB8;
  463. break;
  464. }
  465. }
  466. if (*pix_fmt) {
  467. *bpp = fmt->bits_per_pixel;
  468. return 0;
  469. }
  470. fmt++;
  471. }
  472. avpriv_report_missing_feature(s, "Mapping this pixmap format");
  473. return AVERROR_PATCHWELCOME;
  474. }
  475. static int create_stream(AVFormatContext *s)
  476. {
  477. XCBGrabContext *c = s->priv_data;
  478. AVStream *st = avformat_new_stream(s, NULL);
  479. xcb_get_geometry_cookie_t gc;
  480. xcb_get_geometry_reply_t *geo;
  481. int64_t frame_size_bits;
  482. int ret;
  483. if (!st)
  484. return AVERROR(ENOMEM);
  485. ret = av_parse_video_rate(&st->avg_frame_rate, c->framerate);
  486. if (ret < 0)
  487. return ret;
  488. avpriv_set_pts_info(st, 64, 1, 1000000);
  489. gc = xcb_get_geometry(c->conn, c->window_id);
  490. geo = xcb_get_geometry_reply(c->conn, gc, NULL);
  491. if (!geo) {
  492. av_log(s, AV_LOG_ERROR, "Can't find window '0x%x', aborting.\n", c->window_id);
  493. return AVERROR_EXTERNAL;
  494. }
  495. if (!c->width || !c->height) {
  496. c->width = geo->width;
  497. c->height = geo->height;
  498. }
  499. if (c->x + c->width > geo->width ||
  500. c->y + c->height > geo->height) {
  501. av_log(s, AV_LOG_ERROR,
  502. "Capture area %dx%d at position %d.%d "
  503. "outside the screen size %dx%d\n",
  504. c->width, c->height,
  505. c->x, c->y,
  506. geo->width, geo->height);
  507. free(geo);
  508. return AVERROR(EINVAL);
  509. }
  510. c->time_base = (AVRational){ st->avg_frame_rate.den,
  511. st->avg_frame_rate.num };
  512. c->frame_duration = av_rescale_q(1, c->time_base, AV_TIME_BASE_Q);
  513. c->time_frame = av_gettime_relative();
  514. ret = pixfmt_from_pixmap_format(s, geo->depth, &st->codecpar->format, &c->bpp);
  515. free(geo);
  516. if (ret < 0)
  517. return ret;
  518. frame_size_bits = (int64_t)c->width * c->height * c->bpp;
  519. if (frame_size_bits / 8 + AV_INPUT_BUFFER_PADDING_SIZE > INT_MAX) {
  520. av_log(s, AV_LOG_ERROR, "Captured area is too large\n");
  521. return AVERROR_PATCHWELCOME;
  522. }
  523. c->frame_size = frame_size_bits / 8;
  524. #if CONFIG_LIBXCB_SHM
  525. c->shm_pool = av_buffer_pool_init2(c->frame_size + AV_INPUT_BUFFER_PADDING_SIZE,
  526. c->conn, allocate_shm_buffer, NULL);
  527. if (!c->shm_pool)
  528. return AVERROR(ENOMEM);
  529. #endif
  530. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  531. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  532. st->codecpar->width = c->width;
  533. st->codecpar->height = c->height;
  534. st->codecpar->bit_rate = av_rescale(frame_size_bits, st->avg_frame_rate.num, st->avg_frame_rate.den);
  535. return ret;
  536. }
  537. static void draw_rectangle(AVFormatContext *s)
  538. {
  539. XCBGrabContext *c = s->priv_data;
  540. xcb_gcontext_t gc = xcb_generate_id(c->conn);
  541. uint32_t mask = XCB_GC_FOREGROUND |
  542. XCB_GC_BACKGROUND |
  543. XCB_GC_LINE_WIDTH |
  544. XCB_GC_LINE_STYLE |
  545. XCB_GC_FILL_STYLE;
  546. uint32_t values[] = { c->screen->black_pixel,
  547. c->screen->white_pixel,
  548. c->region_border,
  549. XCB_LINE_STYLE_DOUBLE_DASH,
  550. XCB_FILL_STYLE_SOLID };
  551. xcb_rectangle_t r = { 1, 1,
  552. c->width + c->region_border * 2 - 3,
  553. c->height + c->region_border * 2 - 3 };
  554. xcb_create_gc(c->conn, gc, c->window, mask, values);
  555. xcb_poly_rectangle(c->conn, c->window, gc, 1, &r);
  556. }
  557. static void setup_window(AVFormatContext *s)
  558. {
  559. XCBGrabContext *c = s->priv_data;
  560. uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
  561. uint32_t values[] = { 1,
  562. XCB_EVENT_MASK_EXPOSURE |
  563. XCB_EVENT_MASK_STRUCTURE_NOTIFY };
  564. av_unused xcb_rectangle_t rect = { 0, 0, c->width, c->height };
  565. c->window = xcb_generate_id(c->conn);
  566. xcb_create_window(c->conn, XCB_COPY_FROM_PARENT,
  567. c->window,
  568. c->screen->root,
  569. c->x - c->region_border,
  570. c->y - c->region_border,
  571. c->width + c->region_border * 2,
  572. c->height + c->region_border * 2,
  573. 0,
  574. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  575. XCB_COPY_FROM_PARENT,
  576. mask, values);
  577. #if CONFIG_LIBXCB_SHAPE
  578. xcb_shape_rectangles(c->conn, XCB_SHAPE_SO_SUBTRACT,
  579. XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
  580. c->window,
  581. c->region_border, c->region_border,
  582. 1, &rect);
  583. #endif
  584. xcb_map_window(c->conn, c->window);
  585. draw_rectangle(s);
  586. }
  587. #define CROSSHAIR_CURSOR 34
  588. static xcb_rectangle_t rectangle_from_corners(xcb_point_t *corner_a,
  589. xcb_point_t *corner_b)
  590. {
  591. xcb_rectangle_t rectangle;
  592. rectangle.x = FFMIN(corner_a->x, corner_b->x);
  593. rectangle.y = FFMIN(corner_a->y, corner_b->y);
  594. rectangle.width = FFABS(corner_a->x - corner_b->x);
  595. rectangle.height = FFABS(corner_a->y - corner_b->y);
  596. return rectangle;
  597. }
  598. static int select_region(AVFormatContext *s)
  599. {
  600. XCBGrabContext *c = s->priv_data;
  601. xcb_connection_t *conn = c->conn;
  602. xcb_screen_t *screen = c->screen;
  603. int ret = 0, done = 0, was_pressed = 0;
  604. xcb_cursor_t cursor;
  605. xcb_font_t cursor_font;
  606. xcb_point_t press_position;
  607. xcb_generic_event_t *event;
  608. xcb_rectangle_t rectangle = { 0 };
  609. xcb_grab_pointer_reply_t *reply;
  610. xcb_grab_pointer_cookie_t cookie;
  611. xcb_window_t root_window = screen->root;
  612. xcb_gcontext_t gc = xcb_generate_id(conn);
  613. uint32_t mask = XCB_GC_FUNCTION | XCB_GC_SUBWINDOW_MODE;
  614. uint32_t values[] = { XCB_GX_INVERT, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS };
  615. xcb_create_gc(conn, gc, root_window, mask, values);
  616. cursor_font = xcb_generate_id(conn);
  617. xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
  618. cursor = xcb_generate_id(conn);
  619. xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
  620. CROSSHAIR_CURSOR, CROSSHAIR_CURSOR + 1, 0, 0, 0,
  621. 0xFFFF, 0xFFFF, 0xFFFF);
  622. cookie = xcb_grab_pointer(conn, 0, root_window,
  623. XCB_EVENT_MASK_BUTTON_PRESS |
  624. XCB_EVENT_MASK_BUTTON_RELEASE |
  625. XCB_EVENT_MASK_BUTTON_MOTION,
  626. XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
  627. root_window, cursor, XCB_CURRENT_TIME);
  628. reply = xcb_grab_pointer_reply(conn, cookie, NULL);
  629. if (!reply || reply->status != XCB_GRAB_STATUS_SUCCESS) {
  630. av_log(s, AV_LOG_ERROR,
  631. "Failed to select region. Could not grab pointer.\n");
  632. ret = AVERROR(EIO);
  633. free(reply);
  634. goto fail;
  635. }
  636. free(reply);
  637. xcb_grab_server(conn);
  638. while (!done && (event = xcb_wait_for_event(conn))) {
  639. switch (event->response_type & ~0x80) {
  640. case XCB_BUTTON_PRESS: {
  641. xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
  642. press_position = (xcb_point_t){ press->event_x, press->event_y };
  643. rectangle.x = press_position.x;
  644. rectangle.y = press_position.y;
  645. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  646. was_pressed = 1;
  647. break;
  648. }
  649. case XCB_MOTION_NOTIFY: {
  650. if (was_pressed) {
  651. xcb_motion_notify_event_t *motion =
  652. (xcb_motion_notify_event_t *)event;
  653. xcb_point_t cursor_position = { motion->event_x, motion->event_y };
  654. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  655. rectangle = rectangle_from_corners(&press_position, &cursor_position);
  656. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  657. }
  658. break;
  659. }
  660. case XCB_BUTTON_RELEASE: {
  661. xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
  662. done = 1;
  663. break;
  664. }
  665. default:
  666. break;
  667. }
  668. xcb_flush(conn);
  669. free(event);
  670. }
  671. c->width = rectangle.width;
  672. c->height = rectangle.height;
  673. if (c->width && c->height) {
  674. c->x = rectangle.x;
  675. c->y = rectangle.y;
  676. } else {
  677. c->x = 0;
  678. c->y = 0;
  679. }
  680. xcb_ungrab_server(conn);
  681. xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
  682. xcb_flush(conn);
  683. fail:
  684. xcb_free_cursor(conn, cursor);
  685. xcb_close_font(conn, cursor_font);
  686. xcb_free_gc(conn, gc);
  687. return ret;
  688. }
  689. static av_cold int xcbgrab_read_header(AVFormatContext *s)
  690. {
  691. XCBGrabContext *c = s->priv_data;
  692. int screen_num, ret;
  693. const xcb_setup_t *setup;
  694. char *display_name = av_strdup(s->url);
  695. if (!display_name)
  696. return AVERROR(ENOMEM);
  697. if (!sscanf(s->url, "%[^+]+%d,%d", display_name, &c->x, &c->y)) {
  698. *display_name = 0;
  699. sscanf(s->url, "+%d,%d", &c->x, &c->y);
  700. }
  701. c->conn = xcb_connect(display_name[0] ? display_name : NULL, &screen_num);
  702. av_freep(&display_name);
  703. if ((ret = xcb_connection_has_error(c->conn))) {
  704. av_log(s, AV_LOG_ERROR, "Cannot open display %s, error %d.\n",
  705. s->url[0] ? s->url : "default", ret);
  706. return AVERROR(EIO);
  707. }
  708. setup = xcb_get_setup(c->conn);
  709. c->screen = get_screen(setup, screen_num);
  710. if (!c->screen) {
  711. av_log(s, AV_LOG_ERROR, "The screen %d does not exist.\n",
  712. screen_num);
  713. xcbgrab_read_close(s);
  714. return AVERROR(EIO);
  715. }
  716. if (c->window_id == XCB_NONE)
  717. c->window_id = c->screen->root;
  718. else {
  719. if (c->select_region) {
  720. av_log(s, AV_LOG_WARNING, "select_region ignored with window_id.\n");
  721. c->select_region = 0;
  722. }
  723. if (c->follow_mouse) {
  724. av_log(s, AV_LOG_WARNING, "follow_mouse ignored with window_id.\n");
  725. c->follow_mouse = 0;
  726. }
  727. }
  728. if (c->select_region) {
  729. ret = select_region(s);
  730. if (ret < 0) {
  731. xcbgrab_read_close(s);
  732. return ret;
  733. }
  734. }
  735. ret = create_stream(s);
  736. if (ret < 0) {
  737. xcbgrab_read_close(s);
  738. return ret;
  739. }
  740. #if CONFIG_LIBXCB_SHM
  741. c->has_shm = check_shm(c->conn);
  742. #endif
  743. #if CONFIG_LIBXCB_XFIXES
  744. if (c->draw_mouse) {
  745. if (!(c->draw_mouse = check_xfixes(c->conn))) {
  746. av_log(s, AV_LOG_WARNING,
  747. "XFixes not available, cannot draw the mouse.\n");
  748. }
  749. if (c->bpp < 24) {
  750. avpriv_report_missing_feature(s, "%d bits per pixel screen",
  751. c->bpp);
  752. c->draw_mouse = 0;
  753. }
  754. }
  755. #endif
  756. if (c->show_region)
  757. setup_window(s);
  758. return 0;
  759. }
  760. const FFInputFormat ff_xcbgrab_demuxer = {
  761. .p.name = "x11grab",
  762. .p.long_name = NULL_IF_CONFIG_SMALL("X11 screen capture, using XCB"),
  763. .p.flags = AVFMT_NOFILE,
  764. .p.priv_class = &xcbgrab_class,
  765. .priv_data_size = sizeof(XCBGrabContext),
  766. .read_header = xcbgrab_read_header,
  767. .read_packet = xcbgrab_read_packet,
  768. .read_close = xcbgrab_read_close,
  769. };