x11grab.c 21 KB

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