x11grab.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * X11 video grab interface
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg integration:
  7. * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org>
  8. * Edouard Gomez <ed.gomez@free.fr>
  9. *
  10. * This file contains code from grab.c:
  11. * Copyright (c) 2000-2001 Fabrice Bellard
  12. *
  13. * This file contains code from the xvidcap project:
  14. * Copyright (C) 1997-1998 Rasca, Berlin
  15. * 2003-2004 Karl H. Beckers, Frankfurt
  16. *
  17. * FFmpeg is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * FFmpeg is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with FFmpeg; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. /**
  32. * @file
  33. * X11 frame device demuxer
  34. * @author Clemens Fruhwirth <clemens@endorphin.org>
  35. * @author Edouard Gomez <ed.gomez@free.fr>
  36. */
  37. #include "config.h"
  38. #include <time.h>
  39. #include <sys/shm.h>
  40. #include <X11/cursorfont.h>
  41. #include <X11/X.h>
  42. #include <X11/Xlib.h>
  43. #include <X11/Xlibint.h>
  44. #include <X11/Xproto.h>
  45. #include <X11/Xutil.h>
  46. #include <X11/extensions/shape.h>
  47. #include <X11/extensions/Xfixes.h>
  48. #include <X11/extensions/XShm.h>
  49. #include "avdevice.h"
  50. #include "libavutil/log.h"
  51. #include "libavutil/opt.h"
  52. #include "libavutil/parseutils.h"
  53. #include "libavutil/time.h"
  54. #include "libavformat/internal.h"
  55. /** X11 device demuxer context */
  56. typedef struct X11GrabContext {
  57. const AVClass *class; /**< Class for private options. */
  58. int frame_size; /**< Size in bytes of a grabbed frame */
  59. AVRational time_base; /**< Time base */
  60. int64_t time_frame; /**< Current time */
  61. int width; /**< Width of the grab frame */
  62. int height; /**< Height of the grab frame */
  63. int x_off; /**< Horizontal top-left corner coordinate */
  64. int y_off; /**< Vertical top-left corner coordinate */
  65. Display *dpy; /**< X11 display from which x11grab grabs frames */
  66. XImage *image; /**< X11 image holding the grab */
  67. int use_shm; /**< !0 when using XShm extension */
  68. XShmSegmentInfo shminfo; /**< When using XShm, keeps track of XShm infos */
  69. int draw_mouse; /**< Set by a private option. */
  70. int follow_mouse; /**< Set by a private option. */
  71. int show_region; /**< set by a private option. */
  72. AVRational framerate; /**< Set by a private option. */
  73. int palette_changed;
  74. uint32_t palette[256];
  75. Cursor c;
  76. Window region_win; /**< This is used by show_region option. */
  77. } X11GrabContext;
  78. #define REGION_WIN_BORDER 3
  79. /**
  80. * Draw grabbing region window
  81. *
  82. * @param s x11grab context
  83. */
  84. static void x11grab_draw_region_win(X11GrabContext *s)
  85. {
  86. Display *dpy = s->dpy;
  87. Window win = s->region_win;
  88. int screen = DefaultScreen(dpy);
  89. GC gc = XCreateGC(dpy, win, 0, 0);
  90. XSetForeground(dpy, gc, WhitePixel(dpy, screen));
  91. XSetBackground(dpy, gc, BlackPixel(dpy, screen));
  92. XSetLineAttributes(dpy, gc, REGION_WIN_BORDER, LineDoubleDash, 0, 0);
  93. XDrawRectangle(dpy, win, gc, 1, 1,
  94. (s->width + REGION_WIN_BORDER * 2) - 1 * 2 - 1,
  95. (s->height + REGION_WIN_BORDER * 2) - 1 * 2 - 1);
  96. XFreeGC(dpy, gc);
  97. }
  98. /**
  99. * Initialize grabbing region window
  100. *
  101. * @param s x11grab context
  102. */
  103. static void x11grab_region_win_init(X11GrabContext *s)
  104. {
  105. Display *dpy = s->dpy;
  106. XRectangle rect;
  107. XSetWindowAttributes attribs = { .override_redirect = True };
  108. int screen = DefaultScreen(dpy);
  109. s->region_win = XCreateWindow(dpy, RootWindow(dpy, screen),
  110. s->x_off - REGION_WIN_BORDER,
  111. s->y_off - REGION_WIN_BORDER,
  112. s->width + REGION_WIN_BORDER * 2,
  113. s->height + REGION_WIN_BORDER * 2,
  114. 0, CopyFromParent,
  115. InputOutput, CopyFromParent,
  116. CWOverrideRedirect, &attribs);
  117. rect.x = 0;
  118. rect.y = 0;
  119. rect.width = s->width;
  120. rect.height = s->height;
  121. XShapeCombineRectangles(dpy, s->region_win,
  122. ShapeBounding, REGION_WIN_BORDER, REGION_WIN_BORDER,
  123. &rect, 1, ShapeSubtract, 0);
  124. XMapWindow(dpy, s->region_win);
  125. XSelectInput(dpy, s->region_win, ExposureMask | StructureNotifyMask);
  126. x11grab_draw_region_win(s);
  127. }
  128. static int setup_shm(AVFormatContext *s, Display *dpy, XImage **image)
  129. {
  130. X11GrabContext *g = s->priv_data;
  131. int scr = XDefaultScreen(dpy);
  132. XImage *img = XShmCreateImage(dpy, DefaultVisual(dpy, scr),
  133. DefaultDepth(dpy, scr), ZPixmap, NULL,
  134. &g->shminfo, g->width, g->height);
  135. g->shminfo.shmid = shmget(IPC_PRIVATE, img->bytes_per_line * img->height,
  136. IPC_CREAT | 0777);
  137. if (g->shminfo.shmid == -1) {
  138. av_log(s, AV_LOG_ERROR, "Cannot get shared memory!\n");
  139. return AVERROR(ENOMEM);
  140. }
  141. g->shminfo.shmaddr = img->data = shmat(g->shminfo.shmid, 0, 0);
  142. g->shminfo.readOnly = False;
  143. if (!XShmAttach(dpy, &g->shminfo)) {
  144. av_log(s, AV_LOG_ERROR, "Failed to attach shared memory!\n");
  145. /* needs some better error subroutine :) */
  146. return AVERROR(EIO);
  147. }
  148. *image = img;
  149. return 0;
  150. }
  151. static int pixfmt_from_image(AVFormatContext *s, XImage *image, int *pix_fmt)
  152. {
  153. av_log(s, AV_LOG_DEBUG,
  154. "Image r 0x%.6lx g 0x%.6lx b 0x%.6lx and depth %i\n",
  155. image->red_mask,
  156. image->green_mask,
  157. image->blue_mask,
  158. image->bits_per_pixel);
  159. *pix_fmt = AV_PIX_FMT_NONE;
  160. switch (image->bits_per_pixel) {
  161. case 8:
  162. *pix_fmt = AV_PIX_FMT_PAL8;
  163. break;
  164. case 16:
  165. if (image->red_mask == 0xf800 &&
  166. image->green_mask == 0x07e0 &&
  167. image->blue_mask == 0x001f) {
  168. *pix_fmt = AV_PIX_FMT_RGB565;
  169. } else if (image->red_mask == 0x7c00 &&
  170. image->green_mask == 0x03e0 &&
  171. image->blue_mask == 0x001f) {
  172. *pix_fmt = AV_PIX_FMT_RGB555;
  173. }
  174. break;
  175. case 24:
  176. if (image->red_mask == 0xff0000 &&
  177. image->green_mask == 0x00ff00 &&
  178. image->blue_mask == 0x0000ff) {
  179. *pix_fmt = AV_PIX_FMT_BGR24;
  180. } else if (image->red_mask == 0x0000ff &&
  181. image->green_mask == 0x00ff00 &&
  182. image->blue_mask == 0xff0000) {
  183. *pix_fmt = AV_PIX_FMT_RGB24;
  184. }
  185. break;
  186. case 32:
  187. if (image->red_mask == 0xff0000 &&
  188. image->green_mask == 0x00ff00 &&
  189. image->blue_mask == 0x0000ff ) {
  190. *pix_fmt = AV_PIX_FMT_0RGB32;
  191. }
  192. break;
  193. }
  194. if (*pix_fmt == AV_PIX_FMT_NONE) {
  195. av_log(s, AV_LOG_ERROR,
  196. "XImages with RGB mask 0x%.6lx 0x%.6lx 0x%.6lx and depth %i "
  197. "are currently not supported.\n",
  198. image->red_mask,
  199. image->green_mask,
  200. image->blue_mask,
  201. image->bits_per_pixel);
  202. return AVERROR_PATCHWELCOME;
  203. }
  204. return 0;
  205. }
  206. /**
  207. * Initialize the x11 grab device demuxer (public device demuxer API).
  208. *
  209. * @param s1 Context from avformat core
  210. * @return <ul>
  211. * <li>AVERROR(ENOMEM) no memory left</li>
  212. * <li>AVERROR(EIO) other failure case</li>
  213. * <li>0 success</li>
  214. * </ul>
  215. */
  216. static int x11grab_read_header(AVFormatContext *s1)
  217. {
  218. X11GrabContext *x11grab = s1->priv_data;
  219. Display *dpy;
  220. AVStream *st = NULL;
  221. XImage *image;
  222. int x_off = 0, y_off = 0, ret = 0, screen, use_shm = 0;
  223. char *dpyname, *offset;
  224. Colormap color_map;
  225. XColor color[256];
  226. int i;
  227. dpyname = av_strdup(s1->filename);
  228. if (!dpyname)
  229. goto out;
  230. offset = strchr(dpyname, '+');
  231. if (offset) {
  232. sscanf(offset, "%d,%d", &x_off, &y_off);
  233. if (strstr(offset, "nomouse")) {
  234. av_log(s1, AV_LOG_WARNING,
  235. "'nomouse' specification in argument is deprecated: "
  236. "use 'draw_mouse' option with value 0 instead\n");
  237. x11grab->draw_mouse = 0;
  238. }
  239. *offset = 0;
  240. }
  241. av_log(s1, AV_LOG_INFO,
  242. "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
  243. s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height);
  244. dpy = XOpenDisplay(dpyname);
  245. av_freep(&dpyname);
  246. if (!dpy) {
  247. av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
  248. ret = AVERROR(EIO);
  249. goto out;
  250. }
  251. st = avformat_new_stream(s1, NULL);
  252. if (!st) {
  253. ret = AVERROR(ENOMEM);
  254. goto out;
  255. }
  256. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  257. screen = DefaultScreen(dpy);
  258. if (x11grab->follow_mouse) {
  259. int screen_w, screen_h;
  260. Window w;
  261. screen_w = DisplayWidth(dpy, screen);
  262. screen_h = DisplayHeight(dpy, screen);
  263. XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
  264. &ret, &ret, &ret);
  265. x_off -= x11grab->width / 2;
  266. y_off -= x11grab->height / 2;
  267. x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
  268. y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
  269. av_log(s1, AV_LOG_INFO,
  270. "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
  271. x_off, y_off);
  272. }
  273. if (x11grab->use_shm) {
  274. use_shm = XShmQueryExtension(dpy);
  275. av_log(s1, AV_LOG_INFO,
  276. "shared memory extension %sfound\n", use_shm ? "" : "not ");
  277. }
  278. if (use_shm && setup_shm(s1, dpy, &image) < 0) {
  279. av_log(s1, AV_LOG_WARNING, "Falling back to XGetImage\n");
  280. use_shm = 0;
  281. }
  282. if (!use_shm) {
  283. image = XGetImage(dpy, RootWindow(dpy, screen),
  284. x_off, y_off,
  285. x11grab->width, x11grab->height,
  286. AllPlanes, ZPixmap);
  287. }
  288. x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
  289. x11grab->dpy = dpy;
  290. x11grab->time_base = av_inv_q(x11grab->framerate);
  291. x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
  292. x11grab->x_off = x_off;
  293. x11grab->y_off = y_off;
  294. x11grab->image = image;
  295. x11grab->use_shm = use_shm;
  296. ret = pixfmt_from_image(s1, image, &st->codec->pix_fmt);
  297. if (ret < 0)
  298. goto out;
  299. if (st->codec->pix_fmt == AV_PIX_FMT_PAL8) {
  300. color_map = DefaultColormap(dpy, screen);
  301. for (i = 0; i < 256; ++i)
  302. color[i].pixel = i;
  303. XQueryColors(dpy, color_map, color, 256);
  304. for (i = 0; i < 256; ++i)
  305. x11grab->palette[i] = (color[i].red & 0xFF00) << 8 |
  306. (color[i].green & 0xFF00) |
  307. (color[i].blue & 0xFF00) >> 8;
  308. x11grab->palette_changed = 1;
  309. }
  310. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  311. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  312. st->codec->width = x11grab->width;
  313. st->codec->height = x11grab->height;
  314. st->codec->time_base = x11grab->time_base;
  315. st->codec->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
  316. out:
  317. av_free(dpyname);
  318. return ret;
  319. }
  320. /**
  321. * Paint a mouse pointer in an X11 image.
  322. *
  323. * @param image image to paint the mouse pointer to
  324. * @param s context used to retrieve original grabbing rectangle
  325. * coordinates
  326. */
  327. static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
  328. {
  329. X11GrabContext *s = s1->priv_data;
  330. int x_off = s->x_off;
  331. int y_off = s->y_off;
  332. int width = s->width;
  333. int height = s->height;
  334. Display *dpy = s->dpy;
  335. XFixesCursorImage *xcim;
  336. int x, y;
  337. int line, column;
  338. int to_line, to_column;
  339. int pixstride = image->bits_per_pixel >> 3;
  340. /* Warning: in its insanity, xlib provides unsigned image data through a
  341. * char* pointer, so we have to make it uint8_t to make things not break.
  342. * Anyone who performs further investigation of the xlib API likely risks
  343. * permanent brain damage. */
  344. uint8_t *pix = image->data;
  345. Window root;
  346. XSetWindowAttributes attr;
  347. Bool pointer_on_screen;
  348. Window w;
  349. int _;
  350. root = DefaultRootWindow(dpy);
  351. pointer_on_screen = XQueryPointer(dpy, root, &w, &w, &_, &_, &_, &_, &_);
  352. if (!pointer_on_screen)
  353. return;
  354. /* Code doesn't currently support 16-bit or PAL8 */
  355. if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
  356. return;
  357. if (!s->c)
  358. s->c = XCreateFontCursor(dpy, XC_left_ptr);
  359. attr.cursor = s->c;
  360. XChangeWindowAttributes(dpy, root, CWCursor, &attr);
  361. xcim = XFixesGetCursorImage(dpy);
  362. if (!xcim) {
  363. av_log(s1, AV_LOG_WARNING,
  364. "XFixes extension not available, impossible to draw cursor\n");
  365. s->draw_mouse = 0;
  366. return;
  367. }
  368. x = xcim->x - xcim->xhot;
  369. y = xcim->y - xcim->yhot;
  370. to_line = FFMIN((y + xcim->height), (height + y_off));
  371. to_column = FFMIN((x + xcim->width), (width + x_off));
  372. for (line = FFMAX(y, y_off); line < to_line; line++) {
  373. for (column = FFMAX(x, x_off); column < to_column; column++) {
  374. int xcim_addr = (line - y) * xcim->width + column - x;
  375. int image_addr = ((line - y_off) * width + column - x_off) * pixstride;
  376. int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
  377. int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
  378. int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
  379. int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
  380. if (a == 255) {
  381. pix[image_addr + 0] = r;
  382. pix[image_addr + 1] = g;
  383. pix[image_addr + 2] = b;
  384. } else if (a) {
  385. /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
  386. pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) + 255 / 2) / 255;
  387. pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) + 255 / 2) / 255;
  388. pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) + 255 / 2) / 255;
  389. }
  390. }
  391. }
  392. XFree(xcim);
  393. xcim = NULL;
  394. }
  395. /**
  396. * Read new data in the image structure.
  397. *
  398. * @param dpy X11 display to grab from
  399. * @param d
  400. * @param image Image where the grab will be put
  401. * @param x Top-Left grabbing rectangle horizontal coordinate
  402. * @param y Top-Left grabbing rectangle vertical coordinate
  403. * @return 0 if error, !0 if successful
  404. */
  405. static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  406. {
  407. xGetImageReply rep;
  408. xGetImageReq *req;
  409. long nbytes;
  410. if (!image)
  411. return 0;
  412. LockDisplay(dpy);
  413. GetReq(GetImage, req);
  414. /* First set up the standard stuff in the request */
  415. req->drawable = d;
  416. req->x = x;
  417. req->y = y;
  418. req->width = image->width;
  419. req->height = image->height;
  420. req->planeMask = (unsigned int)AllPlanes;
  421. req->format = ZPixmap;
  422. if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
  423. UnlockDisplay(dpy);
  424. SyncHandle();
  425. return 0;
  426. }
  427. nbytes = (long)rep.length << 2;
  428. _XReadPad(dpy, image->data, nbytes);
  429. UnlockDisplay(dpy);
  430. SyncHandle();
  431. return 1;
  432. }
  433. /**
  434. * Grab a frame from x11 (public device demuxer API).
  435. *
  436. * @param s1 Context from avformat core
  437. * @param pkt Packet holding the brabbed frame
  438. * @return frame size in bytes
  439. */
  440. static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  441. {
  442. X11GrabContext *s = s1->priv_data;
  443. Display *dpy = s->dpy;
  444. XImage *image = s->image;
  445. int x_off = s->x_off;
  446. int y_off = s->y_off;
  447. int follow_mouse = s->follow_mouse;
  448. int screen;
  449. Window root;
  450. int64_t curtime, delay;
  451. struct timespec ts;
  452. /* Calculate the time of the next frame */
  453. s->time_frame += INT64_C(1000000);
  454. /* wait based on the frame rate */
  455. for (;;) {
  456. curtime = av_gettime();
  457. delay = s->time_frame * av_q2d(s->time_base) - curtime;
  458. if (delay <= 0) {
  459. if (delay < INT64_C(-1000000) * av_q2d(s->time_base))
  460. s->time_frame += INT64_C(1000000);
  461. break;
  462. }
  463. ts.tv_sec = delay / 1000000;
  464. ts.tv_nsec = (delay % 1000000) * 1000;
  465. nanosleep(&ts, NULL);
  466. }
  467. av_init_packet(pkt);
  468. pkt->data = image->data;
  469. pkt->size = s->frame_size;
  470. pkt->pts = curtime;
  471. if (s->palette_changed) {
  472. uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  473. AVPALETTE_SIZE);
  474. if (!pal) {
  475. av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
  476. } else {
  477. memcpy(pal, s->palette, AVPALETTE_SIZE);
  478. s->palette_changed = 0;
  479. }
  480. }
  481. screen = DefaultScreen(dpy);
  482. root = RootWindow(dpy, screen);
  483. if (follow_mouse) {
  484. int screen_w, screen_h;
  485. int pointer_x, pointer_y, _;
  486. Window w;
  487. screen_w = DisplayWidth(dpy, screen);
  488. screen_h = DisplayHeight(dpy, screen);
  489. XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
  490. if (follow_mouse == -1) {
  491. // follow the mouse, put it at center of grabbing region
  492. x_off += pointer_x - s->width / 2 - x_off;
  493. y_off += pointer_y - s->height / 2 - y_off;
  494. } else {
  495. // follow the mouse, but only move the grabbing region when mouse
  496. // reaches within certain pixels to the edge.
  497. if (pointer_x > x_off + s->width - follow_mouse)
  498. x_off += pointer_x - (x_off + s->width - follow_mouse);
  499. else if (pointer_x < x_off + follow_mouse)
  500. x_off -= (x_off + follow_mouse) - pointer_x;
  501. if (pointer_y > y_off + s->height - follow_mouse)
  502. y_off += pointer_y - (y_off + s->height - follow_mouse);
  503. else if (pointer_y < y_off + follow_mouse)
  504. y_off -= (y_off + follow_mouse) - pointer_y;
  505. }
  506. // adjust grabbing region position if it goes out of screen.
  507. s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
  508. s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
  509. if (s->show_region && s->region_win)
  510. XMoveWindow(dpy, s->region_win,
  511. s->x_off - REGION_WIN_BORDER,
  512. s->y_off - REGION_WIN_BORDER);
  513. }
  514. if (s->show_region) {
  515. if (s->region_win) {
  516. XEvent evt = { .type = NoEventMask };
  517. // Clean up the events, and do the initial draw or redraw.
  518. while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
  519. &evt))
  520. ;
  521. if (evt.type)
  522. x11grab_draw_region_win(s);
  523. } else {
  524. x11grab_region_win_init(s);
  525. }
  526. }
  527. if (s->use_shm) {
  528. if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
  529. av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  530. } else {
  531. if (!xget_zpixmap(dpy, root, image, x_off, y_off))
  532. av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  533. }
  534. if (s->draw_mouse)
  535. paint_mouse_pointer(image, s1);
  536. return s->frame_size;
  537. }
  538. /**
  539. * Close x11 frame grabber (public device demuxer API).
  540. *
  541. * @param s1 Context from avformat core
  542. * @return 0 success, !0 failure
  543. */
  544. static int x11grab_read_close(AVFormatContext *s1)
  545. {
  546. X11GrabContext *x11grab = s1->priv_data;
  547. /* Detach cleanly from shared mem */
  548. if (x11grab->use_shm) {
  549. XShmDetach(x11grab->dpy, &x11grab->shminfo);
  550. shmdt(x11grab->shminfo.shmaddr);
  551. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  552. }
  553. /* Destroy X11 image */
  554. if (x11grab->image) {
  555. XDestroyImage(x11grab->image);
  556. x11grab->image = NULL;
  557. }
  558. if (x11grab->region_win)
  559. XDestroyWindow(x11grab->dpy, x11grab->region_win);
  560. /* Free X11 display */
  561. XCloseDisplay(x11grab->dpy);
  562. return 0;
  563. }
  564. #define OFFSET(x) offsetof(X11GrabContext, x)
  565. #define DEC AV_OPT_FLAG_DECODING_PARAM
  566. static const AVOption options[] = {
  567. { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  568. { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
  569. OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
  570. { "centered", "keep the mouse pointer at the center of grabbing region when following",
  571. 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "follow_mouse" },
  572. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
  573. { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  574. { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
  575. { "use_shm", "use MIT-SHM extension", OFFSET(use_shm), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  576. { NULL },
  577. };
  578. static const AVClass x11_class = {
  579. .class_name = "X11grab indev",
  580. .item_name = av_default_item_name,
  581. .option = options,
  582. .version = LIBAVUTIL_VERSION_INT,
  583. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  584. };
  585. /** x11 grabber device demuxer declaration */
  586. AVInputFormat ff_x11grab_demuxer = {
  587. .name = "x11grab",
  588. .long_name = NULL_IF_CONFIG_SMALL("X11grab"),
  589. .priv_data_size = sizeof(X11GrabContext),
  590. .read_header = x11grab_read_header,
  591. .read_packet = x11grab_read_packet,
  592. .read_close = x11grab_read_close,
  593. .flags = AVFMT_NOFILE,
  594. .priv_class = &x11_class,
  595. };