xcbgrab.c 27 KB

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