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.

670 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. switch (image->bits_per_pixel) {
  160. case 8:
  161. *pix_fmt = AV_PIX_FMT_PAL8;
  162. break;
  163. case 16:
  164. if (image->red_mask == 0xf800 &&
  165. image->green_mask == 0x07e0 &&
  166. image->blue_mask == 0x001f) {
  167. *pix_fmt = AV_PIX_FMT_RGB565;
  168. } else if (image->red_mask == 0x7c00 &&
  169. image->green_mask == 0x03e0 &&
  170. image->blue_mask == 0x001f) {
  171. *pix_fmt = AV_PIX_FMT_RGB555;
  172. }
  173. break;
  174. case 24:
  175. if (image->red_mask == 0xff0000 &&
  176. image->green_mask == 0x00ff00 &&
  177. image->blue_mask == 0x0000ff) {
  178. *pix_fmt = AV_PIX_FMT_BGR24;
  179. } else if (image->red_mask == 0x0000ff &&
  180. image->green_mask == 0x00ff00 &&
  181. image->blue_mask == 0xff0000) {
  182. *pix_fmt = AV_PIX_FMT_RGB24;
  183. }
  184. break;
  185. case 32:
  186. if (image->red_mask == 0xff0000 &&
  187. image->green_mask == 0x00ff00 &&
  188. image->blue_mask == 0x0000ff ) {
  189. *pix_fmt = AV_PIX_FMT_0RGB32;
  190. }
  191. break;
  192. default:
  193. av_log(s, AV_LOG_ERROR,
  194. "XImages with RGB mask 0x%.6lx 0x%.6lx 0x%.6lx and depth %i "
  195. "are currently not supported.\n",
  196. image->red_mask,
  197. image->green_mask,
  198. image->blue_mask,
  199. image->bits_per_pixel);
  200. return AVERROR_PATCHWELCOME;
  201. }
  202. return 0;
  203. }
  204. /**
  205. * Initialize the x11 grab device demuxer (public device demuxer API).
  206. *
  207. * @param s1 Context from avformat core
  208. * @return <ul>
  209. * <li>AVERROR(ENOMEM) no memory left</li>
  210. * <li>AVERROR(EIO) other failure case</li>
  211. * <li>0 success</li>
  212. * </ul>
  213. */
  214. static int x11grab_read_header(AVFormatContext *s1)
  215. {
  216. X11GrabContext *x11grab = s1->priv_data;
  217. Display *dpy;
  218. AVStream *st = NULL;
  219. XImage *image;
  220. int x_off = 0, y_off = 0, ret = 0, screen, use_shm = 0;
  221. char *dpyname, *offset;
  222. Colormap color_map;
  223. XColor color[256];
  224. int i;
  225. dpyname = av_strdup(s1->filename);
  226. if (!dpyname)
  227. goto out;
  228. offset = strchr(dpyname, '+');
  229. if (offset) {
  230. sscanf(offset, "%d,%d", &x_off, &y_off);
  231. if (strstr(offset, "nomouse")) {
  232. av_log(s1, AV_LOG_WARNING,
  233. "'nomouse' specification in argument is deprecated: "
  234. "use 'draw_mouse' option with value 0 instead\n");
  235. x11grab->draw_mouse = 0;
  236. }
  237. *offset = 0;
  238. }
  239. av_log(s1, AV_LOG_INFO,
  240. "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
  241. s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height);
  242. dpy = XOpenDisplay(dpyname);
  243. av_freep(&dpyname);
  244. if (!dpy) {
  245. av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
  246. ret = AVERROR(EIO);
  247. goto out;
  248. }
  249. st = avformat_new_stream(s1, NULL);
  250. if (!st) {
  251. ret = AVERROR(ENOMEM);
  252. goto out;
  253. }
  254. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  255. screen = DefaultScreen(dpy);
  256. if (x11grab->follow_mouse) {
  257. int screen_w, screen_h;
  258. Window w;
  259. screen_w = DisplayWidth(dpy, screen);
  260. screen_h = DisplayHeight(dpy, screen);
  261. XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
  262. &ret, &ret, &ret);
  263. x_off -= x11grab->width / 2;
  264. y_off -= x11grab->height / 2;
  265. x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
  266. y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
  267. av_log(s1, AV_LOG_INFO,
  268. "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
  269. x_off, y_off);
  270. }
  271. if (x11grab->use_shm) {
  272. use_shm = XShmQueryExtension(dpy);
  273. av_log(s1, AV_LOG_INFO,
  274. "shared memory extension %sfound\n", use_shm ? "" : "not ");
  275. }
  276. if (use_shm && setup_shm(s1, dpy, &image) < 0) {
  277. av_log(s1, AV_LOG_WARNING, "Falling back to XGetImage\n");
  278. use_shm = 0;
  279. }
  280. if (!use_shm) {
  281. image = XGetImage(dpy, RootWindow(dpy, screen),
  282. x_off, y_off,
  283. x11grab->width, x11grab->height,
  284. AllPlanes, ZPixmap);
  285. }
  286. x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
  287. x11grab->dpy = dpy;
  288. x11grab->time_base = av_inv_q(x11grab->framerate);
  289. x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
  290. x11grab->x_off = x_off;
  291. x11grab->y_off = y_off;
  292. x11grab->image = image;
  293. x11grab->use_shm = use_shm;
  294. ret = pixfmt_from_image(s1, image, &st->codec->pix_fmt);
  295. if (ret < 0)
  296. goto out;
  297. if (st->codec->pix_fmt == AV_PIX_FMT_PAL8) {
  298. color_map = DefaultColormap(dpy, screen);
  299. for (i = 0; i < 256; ++i)
  300. color[i].pixel = i;
  301. XQueryColors(dpy, color_map, color, 256);
  302. for (i = 0; i < 256; ++i)
  303. x11grab->palette[i] = (color[i].red & 0xFF00) << 8 |
  304. (color[i].green & 0xFF00) |
  305. (color[i].blue & 0xFF00) >> 8;
  306. x11grab->palette_changed = 1;
  307. }
  308. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  309. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  310. st->codec->width = x11grab->width;
  311. st->codec->height = x11grab->height;
  312. st->codec->time_base = x11grab->time_base;
  313. st->codec->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
  314. out:
  315. av_free(dpyname);
  316. return ret;
  317. }
  318. /**
  319. * Paint a mouse pointer in an X11 image.
  320. *
  321. * @param image image to paint the mouse pointer to
  322. * @param s context used to retrieve original grabbing rectangle
  323. * coordinates
  324. */
  325. static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
  326. {
  327. X11GrabContext *s = s1->priv_data;
  328. int x_off = s->x_off;
  329. int y_off = s->y_off;
  330. int width = s->width;
  331. int height = s->height;
  332. Display *dpy = s->dpy;
  333. XFixesCursorImage *xcim;
  334. int x, y;
  335. int line, column;
  336. int to_line, to_column;
  337. int pixstride = image->bits_per_pixel >> 3;
  338. /* Warning: in its insanity, xlib provides unsigned image data through a
  339. * char* pointer, so we have to make it uint8_t to make things not break.
  340. * Anyone who performs further investigation of the xlib API likely risks
  341. * permanent brain damage. */
  342. uint8_t *pix = image->data;
  343. Window w;
  344. XSetWindowAttributes attr;
  345. /* Code doesn't currently support 16-bit or PAL8 */
  346. if (image->bits_per_pixel != 24 && image->bits_per_pixel != 32)
  347. return;
  348. if (!s->c)
  349. s->c = XCreateFontCursor(dpy, XC_left_ptr);
  350. w = DefaultRootWindow(dpy);
  351. attr.cursor = s->c;
  352. XChangeWindowAttributes(dpy, w, CWCursor, &attr);
  353. xcim = XFixesGetCursorImage(dpy);
  354. if (!xcim) {
  355. av_log(s1, AV_LOG_WARNING,
  356. "XFixes extension not available, impossible to draw cursor\n");
  357. s->draw_mouse = 0;
  358. return;
  359. }
  360. x = xcim->x - xcim->xhot;
  361. y = xcim->y - xcim->yhot;
  362. to_line = FFMIN((y + xcim->height), (height + y_off));
  363. to_column = FFMIN((x + xcim->width), (width + x_off));
  364. for (line = FFMAX(y, y_off); line < to_line; line++) {
  365. for (column = FFMAX(x, x_off); column < to_column; column++) {
  366. int xcim_addr = (line - y) * xcim->width + column - x;
  367. int image_addr = ((line - y_off) * width + column - x_off) * pixstride;
  368. int r = (uint8_t)(xcim->pixels[xcim_addr] >> 0);
  369. int g = (uint8_t)(xcim->pixels[xcim_addr] >> 8);
  370. int b = (uint8_t)(xcim->pixels[xcim_addr] >> 16);
  371. int a = (uint8_t)(xcim->pixels[xcim_addr] >> 24);
  372. if (a == 255) {
  373. pix[image_addr + 0] = r;
  374. pix[image_addr + 1] = g;
  375. pix[image_addr + 2] = b;
  376. } else if (a) {
  377. /* pixel values from XFixesGetCursorImage come premultiplied by alpha */
  378. pix[image_addr + 0] = r + (pix[image_addr + 0] * (255 - a) + 255 / 2) / 255;
  379. pix[image_addr + 1] = g + (pix[image_addr + 1] * (255 - a) + 255 / 2) / 255;
  380. pix[image_addr + 2] = b + (pix[image_addr + 2] * (255 - a) + 255 / 2) / 255;
  381. }
  382. }
  383. }
  384. XFree(xcim);
  385. xcim = NULL;
  386. }
  387. /**
  388. * Read new data in the image structure.
  389. *
  390. * @param dpy X11 display to grab from
  391. * @param d
  392. * @param image Image where the grab will be put
  393. * @param x Top-Left grabbing rectangle horizontal coordinate
  394. * @param y Top-Left grabbing rectangle vertical coordinate
  395. * @return 0 if error, !0 if successful
  396. */
  397. static int xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  398. {
  399. xGetImageReply rep;
  400. xGetImageReq *req;
  401. long nbytes;
  402. if (!image)
  403. return 0;
  404. LockDisplay(dpy);
  405. GetReq(GetImage, req);
  406. /* First set up the standard stuff in the request */
  407. req->drawable = d;
  408. req->x = x;
  409. req->y = y;
  410. req->width = image->width;
  411. req->height = image->height;
  412. req->planeMask = (unsigned int)AllPlanes;
  413. req->format = ZPixmap;
  414. if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
  415. UnlockDisplay(dpy);
  416. SyncHandle();
  417. return 0;
  418. }
  419. nbytes = (long)rep.length << 2;
  420. _XReadPad(dpy, image->data, nbytes);
  421. UnlockDisplay(dpy);
  422. SyncHandle();
  423. return 1;
  424. }
  425. /**
  426. * Grab a frame from x11 (public device demuxer API).
  427. *
  428. * @param s1 Context from avformat core
  429. * @param pkt Packet holding the brabbed frame
  430. * @return frame size in bytes
  431. */
  432. static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  433. {
  434. X11GrabContext *s = s1->priv_data;
  435. Display *dpy = s->dpy;
  436. XImage *image = s->image;
  437. int x_off = s->x_off;
  438. int y_off = s->y_off;
  439. int follow_mouse = s->follow_mouse;
  440. int screen;
  441. Window root;
  442. int64_t curtime, delay;
  443. struct timespec ts;
  444. /* Calculate the time of the next frame */
  445. s->time_frame += INT64_C(1000000);
  446. /* wait based on the frame rate */
  447. for (;;) {
  448. curtime = av_gettime();
  449. delay = s->time_frame * av_q2d(s->time_base) - curtime;
  450. if (delay <= 0) {
  451. if (delay < INT64_C(-1000000) * av_q2d(s->time_base))
  452. s->time_frame += INT64_C(1000000);
  453. break;
  454. }
  455. ts.tv_sec = delay / 1000000;
  456. ts.tv_nsec = (delay % 1000000) * 1000;
  457. nanosleep(&ts, NULL);
  458. }
  459. av_init_packet(pkt);
  460. pkt->data = image->data;
  461. pkt->size = s->frame_size;
  462. pkt->pts = curtime;
  463. if (s->palette_changed) {
  464. uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  465. AVPALETTE_SIZE);
  466. if (!pal) {
  467. av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
  468. } else {
  469. memcpy(pal, s->palette, AVPALETTE_SIZE);
  470. s->palette_changed = 0;
  471. }
  472. }
  473. screen = DefaultScreen(dpy);
  474. root = RootWindow(dpy, screen);
  475. if (follow_mouse) {
  476. int screen_w, screen_h;
  477. int pointer_x, pointer_y, _;
  478. Window w;
  479. screen_w = DisplayWidth(dpy, screen);
  480. screen_h = DisplayHeight(dpy, screen);
  481. XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
  482. if (follow_mouse == -1) {
  483. // follow the mouse, put it at center of grabbing region
  484. x_off += pointer_x - s->width / 2 - x_off;
  485. y_off += pointer_y - s->height / 2 - y_off;
  486. } else {
  487. // follow the mouse, but only move the grabbing region when mouse
  488. // reaches within certain pixels to the edge.
  489. if (pointer_x > x_off + s->width - follow_mouse)
  490. x_off += pointer_x - (x_off + s->width - follow_mouse);
  491. else if (pointer_x < x_off + follow_mouse)
  492. x_off -= (x_off + follow_mouse) - pointer_x;
  493. if (pointer_y > y_off + s->height - follow_mouse)
  494. y_off += pointer_y - (y_off + s->height - follow_mouse);
  495. else if (pointer_y < y_off + follow_mouse)
  496. y_off -= (y_off + follow_mouse) - pointer_y;
  497. }
  498. // adjust grabbing region position if it goes out of screen.
  499. s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
  500. s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
  501. if (s->show_region && s->region_win)
  502. XMoveWindow(dpy, s->region_win,
  503. s->x_off - REGION_WIN_BORDER,
  504. s->y_off - REGION_WIN_BORDER);
  505. }
  506. if (s->show_region) {
  507. if (s->region_win) {
  508. XEvent evt = { .type = NoEventMask };
  509. // Clean up the events, and do the initial draw or redraw.
  510. while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
  511. &evt))
  512. ;
  513. if (evt.type)
  514. x11grab_draw_region_win(s);
  515. } else {
  516. x11grab_region_win_init(s);
  517. }
  518. }
  519. if (s->use_shm) {
  520. if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
  521. av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  522. } else {
  523. if (!xget_zpixmap(dpy, root, image, x_off, y_off))
  524. av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  525. }
  526. if (s->draw_mouse)
  527. paint_mouse_pointer(image, s1);
  528. return s->frame_size;
  529. }
  530. /**
  531. * Close x11 frame grabber (public device demuxer API).
  532. *
  533. * @param s1 Context from avformat core
  534. * @return 0 success, !0 failure
  535. */
  536. static int x11grab_read_close(AVFormatContext *s1)
  537. {
  538. X11GrabContext *x11grab = s1->priv_data;
  539. /* Detach cleanly from shared mem */
  540. if (x11grab->use_shm) {
  541. XShmDetach(x11grab->dpy, &x11grab->shminfo);
  542. shmdt(x11grab->shminfo.shmaddr);
  543. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  544. }
  545. /* Destroy X11 image */
  546. if (x11grab->image) {
  547. XDestroyImage(x11grab->image);
  548. x11grab->image = NULL;
  549. }
  550. if (x11grab->region_win)
  551. XDestroyWindow(x11grab->dpy, x11grab->region_win);
  552. /* Free X11 display */
  553. XCloseDisplay(x11grab->dpy);
  554. return 0;
  555. }
  556. #define OFFSET(x) offsetof(X11GrabContext, x)
  557. #define DEC AV_OPT_FLAG_DECODING_PARAM
  558. static const AVOption options[] = {
  559. { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  560. { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
  561. OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
  562. { "centered", "keep the mouse pointer at the center of grabbing region when following",
  563. 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "follow_mouse" },
  564. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
  565. { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  566. { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
  567. { "use_shm", "use MIT-SHM extension", OFFSET(use_shm), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  568. { NULL },
  569. };
  570. static const AVClass x11_class = {
  571. .class_name = "X11grab indev",
  572. .item_name = av_default_item_name,
  573. .option = options,
  574. .version = LIBAVUTIL_VERSION_INT,
  575. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  576. };
  577. /** x11 grabber device demuxer declaration */
  578. AVInputFormat ff_x11grab_demuxer = {
  579. .name = "x11grab",
  580. .long_name = NULL_IF_CONFIG_SMALL("X11grab"),
  581. .priv_data_size = sizeof(X11GrabContext),
  582. .read_header = x11grab_read_header,
  583. .read_packet = x11grab_read_packet,
  584. .read_close = x11grab_read_close,
  585. .flags = AVFMT_NOFILE,
  586. .priv_class = &x11_class,
  587. };