x11grab.c 22 KB

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