You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

673 lines
22KB

  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 w;
  346. XSetWindowAttributes attr;
  347. /* Code doesn't currently support 16-bit or PAL8 */
  348. if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
  349. return;
  350. if (!s->c)
  351. s->c = XCreateFontCursor(dpy, XC_left_ptr);
  352. w = DefaultRootWindow(dpy);
  353. attr.cursor = s->c;
  354. XChangeWindowAttributes(dpy, w, CWCursor, &attr);
  355. xcim = XFixesGetCursorImage(dpy);
  356. if (!xcim) {
  357. av_log(s1, AV_LOG_WARNING,
  358. "XFixes extension not available, impossible to draw cursor\n");
  359. s->draw_mouse = 0;
  360. return;
  361. }
  362. x = xcim->x - xcim->xhot;
  363. y = xcim->y - xcim->yhot;
  364. to_line = FFMIN((y + xcim->height), (height + y_off));
  365. to_column = FFMIN((x + xcim->width), (width + x_off));
  366. for (line = FFMAX(y, y_off); line < to_line; line++) {
  367. for (column = FFMAX(x, x_off); column < to_column; column++) {
  368. int xcim_addr = (line - y) * xcim->width + column - x;
  369. int image_addr = ((line - y_off) * width + column - x_off) * pixstride;
  370. int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
  371. int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
  372. int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
  373. int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
  374. if (a == 255) {
  375. pix[image_addr + 0] = r;
  376. pix[image_addr + 1] = g;
  377. pix[image_addr + 2] = b;
  378. } else if (a) {
  379. /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
  380. pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) + 255 / 2) / 255;
  381. pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) + 255 / 2) / 255;
  382. pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) + 255 / 2) / 255;
  383. }
  384. }
  385. }
  386. XFree(xcim);
  387. xcim = NULL;
  388. }
  389. /**
  390. * Read new data in the image structure.
  391. *
  392. * @param dpy X11 display to grab from
  393. * @param d
  394. * @param image Image where the grab will be put
  395. * @param x Top-Left grabbing rectangle horizontal coordinate
  396. * @param y Top-Left grabbing rectangle vertical coordinate
  397. * @return 0 if error, !0 if successful
  398. */
  399. static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  400. {
  401. xGetImageReply rep;
  402. xGetImageReq *req;
  403. long nbytes;
  404. if (!image)
  405. return 0;
  406. LockDisplay(dpy);
  407. GetReq(GetImage, req);
  408. /* First set up the standard stuff in the request */
  409. req->drawable = d;
  410. req->x = x;
  411. req->y = y;
  412. req->width = image->width;
  413. req->height = image->height;
  414. req->planeMask = (unsigned int)AllPlanes;
  415. req->format = ZPixmap;
  416. if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
  417. UnlockDisplay(dpy);
  418. SyncHandle();
  419. return 0;
  420. }
  421. nbytes = (long)rep.length << 2;
  422. _XReadPad(dpy, image->data, nbytes);
  423. UnlockDisplay(dpy);
  424. SyncHandle();
  425. return 1;
  426. }
  427. /**
  428. * Grab a frame from x11 (public device demuxer API).
  429. *
  430. * @param s1 Context from avformat core
  431. * @param pkt Packet holding the brabbed frame
  432. * @return frame size in bytes
  433. */
  434. static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  435. {
  436. X11GrabContext *s = s1->priv_data;
  437. Display *dpy = s->dpy;
  438. XImage *image = s->image;
  439. int x_off = s->x_off;
  440. int y_off = s->y_off;
  441. int follow_mouse = s->follow_mouse;
  442. int screen;
  443. Window root;
  444. int64_t curtime, delay;
  445. struct timespec ts;
  446. /* Calculate the time of the next frame */
  447. s->time_frame += INT64_C(1000000);
  448. /* wait based on the frame rate */
  449. for (;;) {
  450. curtime = av_gettime();
  451. delay = s->time_frame * av_q2d(s->time_base) - curtime;
  452. if (delay <= 0) {
  453. if (delay < INT64_C(-1000000) * av_q2d(s->time_base))
  454. s->time_frame += INT64_C(1000000);
  455. break;
  456. }
  457. ts.tv_sec = delay / 1000000;
  458. ts.tv_nsec = (delay % 1000000) * 1000;
  459. nanosleep(&ts, NULL);
  460. }
  461. av_init_packet(pkt);
  462. pkt->data = image->data;
  463. pkt->size = s->frame_size;
  464. pkt->pts = curtime;
  465. if (s->palette_changed) {
  466. uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  467. AVPALETTE_SIZE);
  468. if (!pal) {
  469. av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
  470. } else {
  471. memcpy(pal, s->palette, AVPALETTE_SIZE);
  472. s->palette_changed = 0;
  473. }
  474. }
  475. screen = DefaultScreen(dpy);
  476. root = RootWindow(dpy, screen);
  477. if (follow_mouse) {
  478. int screen_w, screen_h;
  479. int pointer_x, pointer_y, _;
  480. Window w;
  481. screen_w = DisplayWidth(dpy, screen);
  482. screen_h = DisplayHeight(dpy, screen);
  483. XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
  484. if (follow_mouse == -1) {
  485. // follow the mouse, put it at center of grabbing region
  486. x_off += pointer_x - s->width / 2 - x_off;
  487. y_off += pointer_y - s->height / 2 - y_off;
  488. } else {
  489. // follow the mouse, but only move the grabbing region when mouse
  490. // reaches within certain pixels to the edge.
  491. if (pointer_x > x_off + s->width - follow_mouse)
  492. x_off += pointer_x - (x_off + s->width - follow_mouse);
  493. else if (pointer_x < x_off + follow_mouse)
  494. x_off -= (x_off + follow_mouse) - pointer_x;
  495. if (pointer_y > y_off + s->height - follow_mouse)
  496. y_off += pointer_y - (y_off + s->height - follow_mouse);
  497. else if (pointer_y < y_off + follow_mouse)
  498. y_off -= (y_off + follow_mouse) - pointer_y;
  499. }
  500. // adjust grabbing region position if it goes out of screen.
  501. s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
  502. s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
  503. if (s->show_region && s->region_win)
  504. XMoveWindow(dpy, s->region_win,
  505. s->x_off - REGION_WIN_BORDER,
  506. s->y_off - REGION_WIN_BORDER);
  507. }
  508. if (s->show_region) {
  509. if (s->region_win) {
  510. XEvent evt = { .type = NoEventMask };
  511. // Clean up the events, and do the initial draw or redraw.
  512. while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
  513. &evt))
  514. ;
  515. if (evt.type)
  516. x11grab_draw_region_win(s);
  517. } else {
  518. x11grab_region_win_init(s);
  519. }
  520. }
  521. if (s->use_shm) {
  522. if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
  523. av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  524. } else {
  525. if (!xget_zpixmap(dpy, root, image, x_off, y_off))
  526. av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  527. }
  528. if (s->draw_mouse)
  529. paint_mouse_pointer(image, s1);
  530. return s->frame_size;
  531. }
  532. /**
  533. * Close x11 frame grabber (public device demuxer API).
  534. *
  535. * @param s1 Context from avformat core
  536. * @return 0 success, !0 failure
  537. */
  538. static int x11grab_read_close(AVFormatContext *s1)
  539. {
  540. X11GrabContext *x11grab = s1->priv_data;
  541. /* Detach cleanly from shared mem */
  542. if (x11grab->use_shm) {
  543. XShmDetach(x11grab->dpy, &x11grab->shminfo);
  544. shmdt(x11grab->shminfo.shmaddr);
  545. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  546. }
  547. /* Destroy X11 image */
  548. if (x11grab->image) {
  549. XDestroyImage(x11grab->image);
  550. x11grab->image = NULL;
  551. }
  552. if (x11grab->region_win)
  553. XDestroyWindow(x11grab->dpy, x11grab->region_win);
  554. /* Free X11 display */
  555. XCloseDisplay(x11grab->dpy);
  556. return 0;
  557. }
  558. #define OFFSET(x) offsetof(X11GrabContext, x)
  559. #define DEC AV_OPT_FLAG_DECODING_PARAM
  560. static const AVOption options[] = {
  561. { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  562. { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
  563. OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
  564. { "centered", "keep the mouse pointer at the center of grabbing region when following",
  565. 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "follow_mouse" },
  566. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
  567. { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  568. { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
  569. { "use_shm", "use MIT-SHM extension", OFFSET(use_shm), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  570. { NULL },
  571. };
  572. static const AVClass x11_class = {
  573. .class_name = "X11grab indev",
  574. .item_name = av_default_item_name,
  575. .option = options,
  576. .version = LIBAVUTIL_VERSION_INT,
  577. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  578. };
  579. /** x11 grabber device demuxer declaration */
  580. AVInputFormat ff_x11grab_demuxer = {
  581. .name = "x11grab",
  582. .long_name = NULL_IF_CONFIG_SMALL("X11grab"),
  583. .priv_data_size = sizeof(X11GrabContext),
  584. .read_header = x11grab_read_header,
  585. .read_packet = x11grab_read_packet,
  586. .read_close = x11grab_read_close,
  587. .flags = AVFMT_NOFILE,
  588. .priv_class = &x11_class,
  589. };