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.

669 lines
23KB

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