x11grab.c 21 KB

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