xcbgrab.c 28 KB

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