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.

666 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 <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. /**
  129. * Initialize the x11 grab device demuxer (public device demuxer API).
  130. *
  131. * @param s1 Context from avformat core
  132. * @return <ul>
  133. * <li>AVERROR(ENOMEM) no memory left</li>
  134. * <li>AVERROR(EIO) other failure case</li>
  135. * <li>0 success</li>
  136. * </ul>
  137. */
  138. static int x11grab_read_header(AVFormatContext *s1)
  139. {
  140. X11GrabContext *x11grab = s1->priv_data;
  141. Display *dpy;
  142. AVStream *st = NULL;
  143. enum AVPixelFormat input_pixfmt;
  144. XImage *image;
  145. int x_off = 0, y_off = 0, ret = 0, screen, use_shm = 0;
  146. char *dpyname, *offset;
  147. Colormap color_map;
  148. XColor color[256];
  149. int i;
  150. dpyname = av_strdup(s1->filename);
  151. if (!dpyname)
  152. goto out;
  153. offset = strchr(dpyname, '+');
  154. if (offset) {
  155. sscanf(offset, "%d,%d", &x_off, &y_off);
  156. if (strstr(offset, "nomouse")) {
  157. av_log(s1, AV_LOG_WARNING,
  158. "'nomouse' specification in argument is deprecated: "
  159. "use 'draw_mouse' option with value 0 instead\n");
  160. x11grab->draw_mouse = 0;
  161. }
  162. *offset = 0;
  163. }
  164. av_log(s1, AV_LOG_INFO,
  165. "device: %s -> display: %s x: %d y: %d width: %d height: %d\n",
  166. s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height);
  167. dpy = XOpenDisplay(dpyname);
  168. av_freep(&dpyname);
  169. if (!dpy) {
  170. av_log(s1, AV_LOG_ERROR, "Could not open X display.\n");
  171. ret = AVERROR(EIO);
  172. goto out;
  173. }
  174. st = avformat_new_stream(s1, NULL);
  175. if (!st) {
  176. ret = AVERROR(ENOMEM);
  177. goto out;
  178. }
  179. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  180. screen = DefaultScreen(dpy);
  181. if (x11grab->follow_mouse) {
  182. int screen_w, screen_h;
  183. Window w;
  184. screen_w = DisplayWidth(dpy, screen);
  185. screen_h = DisplayHeight(dpy, screen);
  186. XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, &x_off, &y_off,
  187. &ret, &ret, &ret);
  188. x_off -= x11grab->width / 2;
  189. y_off -= x11grab->height / 2;
  190. x_off = FFMIN(FFMAX(x_off, 0), screen_w - x11grab->width);
  191. y_off = FFMIN(FFMAX(y_off, 0), screen_h - x11grab->height);
  192. av_log(s1, AV_LOG_INFO,
  193. "followmouse is enabled, resetting grabbing region to x: %d y: %d\n",
  194. x_off, y_off);
  195. }
  196. if (x11grab->use_shm) {
  197. use_shm = XShmQueryExtension(dpy);
  198. av_log(s1, AV_LOG_INFO,
  199. "shared memory extension %sfound\n", use_shm ? "" : "not ");
  200. }
  201. if (use_shm) {
  202. int scr = XDefaultScreen(dpy);
  203. image = XShmCreateImage(dpy,
  204. DefaultVisual(dpy, scr),
  205. DefaultDepth(dpy, scr),
  206. ZPixmap,
  207. NULL,
  208. &x11grab->shminfo,
  209. x11grab->width, x11grab->height);
  210. x11grab->shminfo.shmid = shmget(IPC_PRIVATE,
  211. image->bytes_per_line * image->height,
  212. IPC_CREAT | 0777);
  213. if (x11grab->shminfo.shmid == -1) {
  214. av_log(s1, AV_LOG_ERROR, "Fatal: Can't get shared memory!\n");
  215. ret = AVERROR(ENOMEM);
  216. goto out;
  217. }
  218. x11grab->shminfo.shmaddr = image->data = shmat(x11grab->shminfo.shmid, 0, 0);
  219. x11grab->shminfo.readOnly = False;
  220. if (!XShmAttach(dpy, &x11grab->shminfo)) {
  221. av_log(s1, AV_LOG_ERROR, "Fatal: Failed to attach shared memory!\n");
  222. /* needs some better error subroutine :) */
  223. ret = AVERROR(EIO);
  224. goto out;
  225. }
  226. } else {
  227. image = XGetImage(dpy, RootWindow(dpy, screen),
  228. x_off, y_off,
  229. x11grab->width, x11grab->height,
  230. AllPlanes, ZPixmap);
  231. }
  232. switch (image->bits_per_pixel) {
  233. case 8:
  234. av_log(s1, AV_LOG_DEBUG, "8 bit palette\n");
  235. input_pixfmt = AV_PIX_FMT_PAL8;
  236. color_map = DefaultColormap(dpy, screen);
  237. for (i = 0; i < 256; ++i)
  238. color[i].pixel = i;
  239. XQueryColors(dpy, color_map, color, 256);
  240. for (i = 0; i < 256; ++i)
  241. x11grab->palette[i] = (color[i].red & 0xFF00) << 8 |
  242. (color[i].green & 0xFF00) |
  243. (color[i].blue & 0xFF00) >> 8;
  244. x11grab->palette_changed = 1;
  245. break;
  246. case 16:
  247. if (image->red_mask == 0xf800 &&
  248. image->green_mask == 0x07e0 &&
  249. image->blue_mask == 0x001f) {
  250. av_log(s1, AV_LOG_DEBUG, "16 bit RGB565\n");
  251. input_pixfmt = AV_PIX_FMT_RGB565;
  252. } else if (image->red_mask == 0x7c00 &&
  253. image->green_mask == 0x03e0 &&
  254. image->blue_mask == 0x001f) {
  255. av_log(s1, AV_LOG_DEBUG, "16 bit RGB555\n");
  256. input_pixfmt = AV_PIX_FMT_RGB555;
  257. } else {
  258. av_log(s1, AV_LOG_ERROR,
  259. "RGB ordering at image depth %i not supported ... aborting\n",
  260. image->bits_per_pixel);
  261. av_log(s1, AV_LOG_ERROR,
  262. "color masks: r 0x%.6lx g 0x%.6lx b 0x%.6lx\n",
  263. image->red_mask, image->green_mask, image->blue_mask);
  264. ret = AVERROR_PATCHWELCOME;
  265. goto out;
  266. }
  267. break;
  268. case 24:
  269. if (image->red_mask == 0xff0000 &&
  270. image->green_mask == 0x00ff00 &&
  271. image->blue_mask == 0x0000ff) {
  272. input_pixfmt = AV_PIX_FMT_BGR24;
  273. } else if (image->red_mask == 0x0000ff &&
  274. image->green_mask == 0x00ff00 &&
  275. image->blue_mask == 0xff0000) {
  276. input_pixfmt = AV_PIX_FMT_RGB24;
  277. } else {
  278. av_log(s1, AV_LOG_ERROR,
  279. "rgb ordering at image depth %i not supported ... aborting\n",
  280. image->bits_per_pixel);
  281. av_log(s1, AV_LOG_ERROR,
  282. "color masks: r 0x%.6lx g 0x%.6lx b 0x%.6lx\n",
  283. image->red_mask, image->green_mask, image->blue_mask);
  284. ret = AVERROR_PATCHWELCOME;
  285. goto out;
  286. }
  287. break;
  288. case 32:
  289. if ( image->red_mask == 0xff0000 &&
  290. image->green_mask == 0x00ff00 &&
  291. image->blue_mask == 0x0000ff ) {
  292. input_pixfmt = AV_PIX_FMT_0RGB32;
  293. } else {
  294. av_log(s1, AV_LOG_ERROR,"rgb ordering at image depth %i not supported ... aborting\n", image->bits_per_pixel);
  295. 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);
  296. ret = AVERROR_PATCHWELCOME;
  297. goto out;
  298. }
  299. break;
  300. default:
  301. av_log(s1, AV_LOG_ERROR,
  302. "image depth %i not supported ... aborting\n",
  303. image->bits_per_pixel);
  304. ret = AVERROR_PATCHWELCOME;
  305. goto out;
  306. }
  307. x11grab->frame_size = x11grab->width * x11grab->height * image->bits_per_pixel / 8;
  308. x11grab->dpy = dpy;
  309. x11grab->time_base = av_inv_q(x11grab->framerate);
  310. x11grab->time_frame = av_gettime() / av_q2d(x11grab->time_base);
  311. x11grab->x_off = x_off;
  312. x11grab->y_off = y_off;
  313. x11grab->image = image;
  314. x11grab->use_shm = use_shm;
  315. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  316. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  317. st->codec->width = x11grab->width;
  318. st->codec->height = x11grab->height;
  319. st->codec->pix_fmt = input_pixfmt;
  320. st->codec->time_base = x11grab->time_base;
  321. st->codec->bit_rate = x11grab->frame_size * 1 / av_q2d(x11grab->time_base) * 8;
  322. out:
  323. av_free(dpyname);
  324. return ret;
  325. }
  326. /**
  327. * Paint a mouse pointer in an X11 image.
  328. *
  329. * @param image image to paint the mouse pointer to
  330. * @param s context used to retrieve original grabbing rectangle
  331. * coordinates
  332. */
  333. static void paint_mouse_pointer(XImage *image, AVFormatContext *s1)
  334. {
  335. X11GrabContext *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 xget_zpixmap(Display *dpy, Drawable d, XImage *image, int x, int y)
  406. {
  407. xGetImageReply rep;
  408. xGetImageReq *req;
  409. long nbytes;
  410. if (!image)
  411. return 0;
  412. LockDisplay(dpy);
  413. GetReq(GetImage, req);
  414. /* First set up the standard stuff in the request */
  415. req->drawable = d;
  416. req->x = x;
  417. req->y = y;
  418. req->width = image->width;
  419. req->height = image->height;
  420. req->planeMask = (unsigned int)AllPlanes;
  421. req->format = ZPixmap;
  422. if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.length) {
  423. UnlockDisplay(dpy);
  424. SyncHandle();
  425. return 0;
  426. }
  427. nbytes = (long)rep.length << 2;
  428. _XReadPad(dpy, image->data, nbytes);
  429. UnlockDisplay(dpy);
  430. SyncHandle();
  431. return 1;
  432. }
  433. /**
  434. * Grab a frame from x11 (public device demuxer API).
  435. *
  436. * @param s1 Context from avformat core
  437. * @param pkt Packet holding the brabbed frame
  438. * @return frame size in bytes
  439. */
  440. static int x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
  441. {
  442. X11GrabContext *s = s1->priv_data;
  443. Display *dpy = s->dpy;
  444. XImage *image = s->image;
  445. int x_off = s->x_off;
  446. int y_off = s->y_off;
  447. int follow_mouse = s->follow_mouse;
  448. int screen;
  449. Window root;
  450. int64_t curtime, delay;
  451. struct timespec ts;
  452. /* Calculate the time of the next frame */
  453. s->time_frame += INT64_C(1000000);
  454. /* wait based on the frame rate */
  455. for (;;) {
  456. curtime = av_gettime();
  457. delay = s->time_frame * av_q2d(s->time_base) - curtime;
  458. if (delay <= 0) {
  459. if (delay < INT64_C(-1000000) * av_q2d(s->time_base))
  460. s->time_frame += INT64_C(1000000);
  461. break;
  462. }
  463. ts.tv_sec = delay / 1000000;
  464. ts.tv_nsec = (delay % 1000000) * 1000;
  465. nanosleep(&ts, NULL);
  466. }
  467. av_init_packet(pkt);
  468. pkt->data = image->data;
  469. pkt->size = s->frame_size;
  470. pkt->pts = curtime;
  471. if (s->palette_changed) {
  472. uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  473. AVPALETTE_SIZE);
  474. if (!pal) {
  475. av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
  476. } else {
  477. memcpy(pal, s->palette, AVPALETTE_SIZE);
  478. s->palette_changed = 0;
  479. }
  480. }
  481. screen = DefaultScreen(dpy);
  482. root = RootWindow(dpy, screen);
  483. if (follow_mouse) {
  484. int screen_w, screen_h;
  485. int pointer_x, pointer_y, _;
  486. Window w;
  487. screen_w = DisplayWidth(dpy, screen);
  488. screen_h = DisplayHeight(dpy, screen);
  489. XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
  490. if (follow_mouse == -1) {
  491. // follow the mouse, put it at center of grabbing region
  492. x_off += pointer_x - s->width / 2 - x_off;
  493. y_off += pointer_y - s->height / 2 - y_off;
  494. } else {
  495. // follow the mouse, but only move the grabbing region when mouse
  496. // reaches within certain pixels to the edge.
  497. if (pointer_x > x_off + s->width - follow_mouse)
  498. x_off += pointer_x - (x_off + s->width - follow_mouse);
  499. else if (pointer_x < x_off + follow_mouse)
  500. x_off -= (x_off + follow_mouse) - pointer_x;
  501. if (pointer_y > y_off + s->height - follow_mouse)
  502. y_off += pointer_y - (y_off + s->height - follow_mouse);
  503. else if (pointer_y < y_off + follow_mouse)
  504. y_off -= (y_off + follow_mouse) - pointer_y;
  505. }
  506. // adjust grabbing region position if it goes out of screen.
  507. s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
  508. s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);
  509. if (s->show_region && s->region_win)
  510. XMoveWindow(dpy, s->region_win,
  511. s->x_off - REGION_WIN_BORDER,
  512. s->y_off - REGION_WIN_BORDER);
  513. }
  514. if (s->show_region) {
  515. if (s->region_win) {
  516. XEvent evt = { .type = NoEventMask };
  517. // Clean up the events, and do the initial draw or redraw.
  518. while (XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask,
  519. &evt))
  520. ;
  521. if (evt.type)
  522. x11grab_draw_region_win(s);
  523. } else {
  524. x11grab_region_win_init(s);
  525. }
  526. }
  527. if (s->use_shm) {
  528. if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes))
  529. av_log(s1, AV_LOG_INFO, "XShmGetImage() failed\n");
  530. } else {
  531. if (!xget_zpixmap(dpy, root, image, x_off, y_off))
  532. av_log(s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
  533. }
  534. if (s->draw_mouse)
  535. paint_mouse_pointer(image, s1);
  536. return s->frame_size;
  537. }
  538. /**
  539. * Close x11 frame grabber (public device demuxer API).
  540. *
  541. * @param s1 Context from avformat core
  542. * @return 0 success, !0 failure
  543. */
  544. static int x11grab_read_close(AVFormatContext *s1)
  545. {
  546. X11GrabContext *x11grab = s1->priv_data;
  547. /* Detach cleanly from shared mem */
  548. if (x11grab->use_shm) {
  549. XShmDetach(x11grab->dpy, &x11grab->shminfo);
  550. shmdt(x11grab->shminfo.shmaddr);
  551. shmctl(x11grab->shminfo.shmid, IPC_RMID, NULL);
  552. }
  553. /* Destroy X11 image */
  554. if (x11grab->image) {
  555. XDestroyImage(x11grab->image);
  556. x11grab->image = NULL;
  557. }
  558. if (x11grab->region_win)
  559. XDestroyWindow(x11grab->dpy, x11grab->region_win);
  560. /* Free X11 display */
  561. XCloseDisplay(x11grab->dpy);
  562. return 0;
  563. }
  564. #define OFFSET(x) offsetof(X11GrabContext, x)
  565. #define DEC AV_OPT_FLAG_DECODING_PARAM
  566. static const AVOption options[] = {
  567. { "draw_mouse", "draw the mouse pointer", OFFSET(draw_mouse), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  568. { "follow_mouse", "move the grabbing region when the mouse pointer reaches within specified amount of pixels to the edge of region",
  569. OFFSET(follow_mouse), AV_OPT_TYPE_INT, {.i64 = 0}, -1, INT_MAX, DEC, "follow_mouse" },
  570. { "centered", "keep the mouse pointer at the center of grabbing region when following",
  571. 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, DEC, "follow_mouse" },
  572. { "framerate", "set video frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, 0, DEC },
  573. { "show_region", "show the grabbing region", OFFSET(show_region), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  574. { "video_size", "set video frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = "vga"}, 0, 0, DEC },
  575. { "use_shm", "use MIT-SHM extension", OFFSET(use_shm), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
  576. { NULL },
  577. };
  578. static const AVClass x11_class = {
  579. .class_name = "X11grab indev",
  580. .item_name = av_default_item_name,
  581. .option = options,
  582. .version = LIBAVUTIL_VERSION_INT,
  583. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  584. };
  585. /** x11 grabber device demuxer declaration */
  586. AVInputFormat ff_x11grab_demuxer = {
  587. .name = "x11grab",
  588. .long_name = NULL_IF_CONFIG_SMALL("X11grab"),
  589. .priv_data_size = sizeof(X11GrabContext),
  590. .read_header = x11grab_read_header,
  591. .read_packet = x11grab_read_packet,
  592. .read_close = x11grab_read_close,
  593. .flags = AVFMT_NOFILE,
  594. .priv_class = &x11_class,
  595. };