3 * Copyright (C) 2006 Zaheer Merali <zaheerabbas at merali dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-ximagesrc
24 * This element captures your X Display and creates raw RGB video. It uses
25 * the XDamage extension if available to only capture areas of the screen that
26 * have changed since the last frame. It uses the XFixes extension if
27 * available to also capture your mouse pointer. By default it will fixate to
28 * 25 frames per second.
31 * <title>Example pipelines</title>
33 * gst-launch ximagesrc ! video/x-raw-rgb,framerate=5/1 ! ffmpegcolorspace ! theoraenc ! oggmux ! filesink location=desktop.ogg
34 * ]| Encodes your X display to an Ogg theora video at 5 frames per second.
41 #include "gstximagesrc.h"
47 #include <X11/Xutil.h>
50 #include <gst/gst-i18n-plugin.h>
52 GST_DEBUG_CATEGORY_STATIC (gst_debug_ximage_src);
53 #define GST_CAT_DEFAULT gst_debug_ximage_src
55 static GstStaticPadTemplate t =
56 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
57 GST_STATIC_CAPS ("video/x-raw-rgb, "
58 "framerate = (fraction) [ 0, MAX ], "
59 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
60 "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
78 GST_BOILERPLATE (GstXImageSrc, gst_ximage_src, GstPushSrc, GST_TYPE_PUSH_SRC);
80 static void gst_ximage_src_fixate (GstPad * pad, GstCaps * caps);
81 static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
83 /* Called when a buffer is returned from the pipeline */
85 gst_ximage_src_return_buf (GstXImageSrc * ximagesrc,
86 GstXImageSrcBuffer * ximage)
88 /* If our geometry changed we can't reuse that image. */
89 if ((ximage->width != ximagesrc->width) ||
90 (ximage->height != ximagesrc->height)) {
91 GST_DEBUG_OBJECT (ximagesrc,
92 "destroy image %p as its size changed %dx%d vs current %dx%d",
93 ximage, ximage->width, ximage->height,
94 ximagesrc->width, ximagesrc->height);
95 g_mutex_lock (ximagesrc->x_lock);
96 gst_ximageutil_ximage_destroy (ximagesrc->xcontext, ximage);
97 g_mutex_unlock (ximagesrc->x_lock);
99 /* In that case we can reuse the image and add it to our image pool. */
100 GST_LOG_OBJECT (ximagesrc, "recycling image %p in pool", ximage);
101 /* need to increment the refcount again to recycle */
102 gst_buffer_ref (GST_BUFFER (ximage));
103 g_mutex_lock (ximagesrc->pool_lock);
104 GST_BUFFER_FLAGS (GST_BUFFER (ximage)) = 0; /* clear out any flags from the previous use */
105 ximagesrc->buffer_pool = g_slist_prepend (ximagesrc->buffer_pool, ximage);
106 g_mutex_unlock (ximagesrc->pool_lock);
111 gst_ximage_src_find_window (GstXImageSrc * src, Window root, const char *name)
114 Window window = 0, root_return, parent_return;
115 unsigned int nchildren;
119 status = XFetchName (src->xcontext->disp, root, &tmpname);
120 if (status && !strcmp (name, tmpname))
124 XQueryTree (src->xcontext->disp, root, &root_return, &parent_return,
125 &children, &nchildren);
126 if (!status || !children)
129 for (n = 0; n < nchildren; ++n) {
130 window = gst_ximage_src_find_window (src, children[n], name);
140 gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
142 g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
144 if (s->xcontext != NULL)
147 g_mutex_lock (s->x_lock);
148 s->xcontext = ximageutil_xcontext_get (GST_ELEMENT (s), name);
149 if (s->xcontext == NULL) {
150 g_mutex_unlock (s->x_lock);
151 GST_ELEMENT_ERROR (s, RESOURCE, OPEN_READ,
152 ("Could not open X display for reading"),
153 ("NULL returned from getting xcontext"));
156 s->width = s->xcontext->width;
157 s->height = s->xcontext->height;
159 s->xwindow = s->xcontext->root;
160 if (s->xid != 0 || s->xname) {
162 XWindowAttributes attrs;
166 status = XGetWindowAttributes (s->xcontext->disp, s->xid, &attrs);
168 GST_DEBUG_OBJECT (s, "Found window XID %" G_GUINT64_FORMAT, s->xid);
172 GST_WARNING_OBJECT (s, "Failed to get window %" G_GUINT64_FORMAT
173 " attributes", s->xid);
178 GST_DEBUG_OBJECT (s, "Looking for window %s", s->xname);
179 window = gst_ximage_src_find_window (s, s->xcontext->root, s->xname);
181 GST_DEBUG_OBJECT (s, "Found window named %s, ", s->xname);
182 status = XGetWindowAttributes (s->xcontext->disp, window, &attrs);
187 GST_WARNING_OBJECT (s, "Failed to get window attributes for "
188 "window named %s", s->xname);
193 GST_INFO_OBJECT (s, "Using root window");
194 goto use_root_window;
197 g_assert (s->xwindow != 0);
198 s->width = attrs.width;
199 s->height = attrs.height;
200 GST_INFO_OBJECT (s, "Using default window size of %dx%d",
201 s->width, s->height);
206 /* check if xfixes supported */
210 if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
212 s->have_xfixes = TRUE;
213 GST_DEBUG_OBJECT (s, "X Server supports XFixes");
216 GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
221 /* check if xdamage is supported */
224 long evmask = NoEventMask;
226 s->have_xdamage = FALSE;
228 s->damage_copy_gc = None;
229 s->damage_region = None;
231 if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
234 XDamageCreate (s->xcontext->disp, s->xwindow, XDamageReportNonEmpty);
235 if (s->damage != None) {
236 s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
237 if (s->damage_region != None) {
240 GST_DEBUG_OBJECT (s, "Using XDamage extension");
241 values.subwindow_mode = IncludeInferiors;
242 s->damage_copy_gc = XCreateGC (s->xcontext->disp,
243 s->xwindow, GCSubwindowMode, &values);
244 XSelectInput (s->xcontext->disp, s->xwindow, evmask);
246 s->have_xdamage = TRUE;
248 XDamageDestroy (s->xcontext->disp, s->damage);
252 GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
254 GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
260 g_mutex_unlock (s->x_lock);
262 if (s->xcontext == NULL)
269 gst_ximage_src_start (GstBaseSrc * basesrc)
271 GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
273 s->last_frame_no = -1;
276 gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
277 s->last_ximage = NULL;
279 return gst_ximage_src_open_display (s, s->display_name);
283 gst_ximage_src_stop (GstBaseSrc * basesrc)
285 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
288 if (src->last_ximage)
289 gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
290 src->last_ximage = NULL;
293 gst_ximage_src_clear_bufpool (src);
296 if (src->cursor_image)
297 XFree (src->cursor_image);
298 src->cursor_image = NULL;
302 g_mutex_lock (src->x_lock);
305 if (src->damage_copy_gc != None) {
306 XFreeGC (src->xcontext->disp, src->damage_copy_gc);
307 src->damage_copy_gc = None;
309 if (src->damage_region != None) {
310 XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
311 src->damage_region = None;
313 if (src->damage != None) {
314 XDamageDestroy (src->xcontext->disp, src->damage);
319 ximageutil_xcontext_clear (src->xcontext);
320 src->xcontext = NULL;
321 g_mutex_unlock (src->x_lock);
328 gst_ximage_src_unlock (GstBaseSrc * basesrc)
330 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
332 /* Awaken the create() func if it's waiting on the clock */
333 GST_OBJECT_LOCK (src);
335 GST_DEBUG_OBJECT (src, "Waking up waiting clock");
336 gst_clock_id_unschedule (src->clock_id);
338 GST_OBJECT_UNLOCK (src);
344 gst_ximage_src_recalc (GstXImageSrc * src)
349 /* Maybe later we can check the display hasn't changed size */
350 /* We could use XQueryPointer to get only the current window. */
356 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
364 gint r_shift, r_max, r_shift_out;
365 gint g_shift, g_max, g_shift_out;
366 gint b_shift, b_max, b_shift_out;
368 switch (xcontext->bpp) {
373 color = GUINT16_FROM_LE (*(guint16 *) (dest));
376 color = GUINT32_FROM_LE (*(guint32 *) (dest));
379 /* Should not reach here */
380 g_return_if_reached ();
383 /* possible optimisation:
384 * move the code that finds shift and max in the _link function */
385 for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
386 for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
388 for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
390 for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
392 for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
394 for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
398 r_max = (xcontext->visual->red_mask >> r_shift);
399 b_max = (xcontext->visual->blue_mask >> b_shift);
400 g_max = (xcontext->visual->green_mask >> g_shift);
402 #define RGBXXX_R(x) (((x)>>r_shift) & (r_max))
403 #define RGBXXX_G(x) (((x)>>g_shift) & (g_max))
404 #define RGBXXX_B(x) (((x)>>b_shift) & (b_max))
406 dr = (RGBXXX_R (color) * 255) / r_max;
407 dg = (RGBXXX_G (color) * 255) / g_max;
408 db = (RGBXXX_B (color) * 255) / b_max;
410 dr = (r * a + (0xff - a) * dr) / 0xff;
411 dg = (g * a + (0xff - a) * dg) / 0xff;
412 db = (b * a + (0xff - a) * db) / 0xff;
414 color = (((dr * r_max) / 255) << r_shift_out) +
415 (((dg * g_max) / 255) << g_shift_out) +
416 (((db * b_max) / 255) << b_shift_out);
418 switch (xcontext->bpp) {
423 *(guint16 *) (dest) = color;
426 *(guint32 *) (dest) = color;
429 g_warning ("bpp %d not supported\n", xcontext->bpp);
434 /* Retrieve an XImageSrcBuffer, preferably from our
435 * pool of existing images and populate it from the window */
436 static GstXImageSrcBuffer *
437 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
439 GstXImageSrcBuffer *ximage = NULL;
441 g_mutex_lock (ximagesrc->pool_lock);
442 while (ximagesrc->buffer_pool != NULL) {
443 ximage = ximagesrc->buffer_pool->data;
445 if ((ximage->width != ximagesrc->width) ||
446 (ximage->height != ximagesrc->height)) {
447 gst_ximage_buffer_free (ximage);
450 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
451 ximagesrc->buffer_pool);
453 g_mutex_unlock (ximagesrc->pool_lock);
455 if (ximage == NULL) {
456 GstXContext *xcontext;
457 GstCaps *caps = NULL;
459 GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
460 ximagesrc->width, ximagesrc->height);
462 g_mutex_lock (ximagesrc->x_lock);
463 ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
464 GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
465 (BufferReturnFunc) (gst_ximage_src_return_buf));
466 if (ximage == NULL) {
467 GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
468 ("could not create a %dx%d ximage", ximagesrc->width,
470 g_mutex_unlock (ximagesrc->x_lock);
474 xcontext = ximagesrc->xcontext;
477 caps = gst_caps_new_simple ("video/x-raw-rgb",
478 "bpp", G_TYPE_INT, xcontext->bpp,
479 "depth", G_TYPE_INT, xcontext->depth,
480 "endianness", G_TYPE_INT, xcontext->endianness,
481 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
482 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
483 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
484 "width", G_TYPE_INT, ximagesrc->width,
485 "height", G_TYPE_INT, ximagesrc->height,
486 "framerate", GST_TYPE_FRACTION, ximagesrc->fps_n, ximagesrc->fps_d,
487 "pixel-aspect-ratio", GST_TYPE_FRACTION,
488 gst_value_get_fraction_numerator (xcontext->par),
489 gst_value_get_fraction_denominator (xcontext->par), NULL);
491 gst_buffer_set_caps (GST_BUFFER (ximage), caps);
492 g_mutex_unlock (ximagesrc->x_lock);
494 gst_caps_unref (caps);
497 g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
499 if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
500 ximagesrc->last_ximage != NULL) {
503 /* have_frame is TRUE when either the entire screen has been
504 * grabbed or when the last image has been copied */
505 gboolean have_frame = FALSE;
507 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
510 XNextEvent (ximagesrc->xcontext->disp, &ev);
512 if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
517 parts = XFixesCreateRegion (ximagesrc->xcontext->disp, 0, 0);
518 XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
520 /* Now copy out all of the damaged rectangles. */
521 rects = XFixesFetchRegion (ximagesrc->xcontext->disp, parts, &nrects);
526 GST_LOG_OBJECT (ximagesrc,
527 "Copying from last frame ximage->size: %d",
528 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
529 memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
530 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
531 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
534 for (i = 0; i < nrects; i++) {
535 GST_LOG_OBJECT (ximagesrc,
536 "Damaged sub-region @ %d,%d size %dx%d reported",
537 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
539 /* if we only want a small area, clip this damage region to
541 if (ximagesrc->endx > ximagesrc->startx &&
542 ximagesrc->endy > ximagesrc->starty) {
543 /* see if damage area intersects */
544 if (rects[i].x + rects[i].width - 1 < ximagesrc->startx ||
545 rects[i].x > ximagesrc->endx) {
547 } else if (rects[i].y + rects[i].height - 1 < ximagesrc->starty ||
548 rects[i].y > ximagesrc->endy) {
551 /* find intersect region */
552 int startx, starty, width, height;
554 startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
556 starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
558 width = (rects[i].x + rects[i].width - 1 < ximagesrc->endx) ?
559 rects[i].x + rects[i].width - startx :
560 ximagesrc->endx - startx + 1;
561 height = (rects[i].y + rects[i].height - 1 < ximagesrc->endy) ?
562 rects[i].y + rects[i].height - starty : ximagesrc->endy -
565 GST_LOG_OBJECT (ximagesrc,
566 "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
567 startx, starty, width, height);
568 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
569 startx, starty, width, height, AllPlanes, ZPixmap,
570 ximage->ximage, startx - ximagesrc->startx,
571 starty - ximagesrc->starty);
575 GST_LOG_OBJECT (ximagesrc,
576 "Retrieving damaged sub-region @ %d,%d size %dx%d",
577 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
579 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
580 rects[i].x, rects[i].y,
581 rects[i].width, rects[i].height,
582 AllPlanes, ZPixmap, ximage->ximage, rects[i].x, rects[i].y);
588 } while (XPending (ximagesrc->xcontext->disp));
590 GST_LOG_OBJECT (ximagesrc,
591 "Copying from last frame ximage->size: %d",
592 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
593 memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
594 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
595 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
598 /* re-get area where last mouse pointer was but only if in our clipping
600 if (ximagesrc->cursor_image) {
601 gint x, y, width, height;
603 x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
604 y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
605 width = ximagesrc->cursor_image->width;
606 height = ximagesrc->cursor_image->height;
608 /* bounds checking */
613 if (x + width > ximagesrc->xcontext->width)
614 width = ximagesrc->xcontext->width - x;
615 if (y + height > ximagesrc->xcontext->height)
616 height = ximagesrc->xcontext->height - y;
619 GST_DEBUG_OBJECT (ximagesrc,
620 "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
621 x, y, width, height, ximagesrc->startx, ximagesrc->starty,
622 ximagesrc->endx, ximagesrc->endy);
623 /* only get where cursor last was, if it is in our range */
624 if (ximagesrc->endx > ximagesrc->startx &&
625 ximagesrc->endy > ximagesrc->starty) {
627 if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
629 } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
632 /* find intersect region */
633 int startx, starty, iwidth, iheight;
635 startx = (x < ximagesrc->startx) ? ximagesrc->startx : x;
636 starty = (y < ximagesrc->starty) ? ximagesrc->starty : y;
637 iwidth = (x + width < ximagesrc->endx) ?
638 x + width - startx : ximagesrc->endx - startx;
639 iheight = (y + height < ximagesrc->endy) ?
640 y + height - starty : ximagesrc->endy - starty;
641 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
642 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
643 startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
644 ximage->ximage, startx - ximagesrc->startx,
645 starty - ximagesrc->starty);
649 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
650 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
651 x, y, width, height, AllPlanes, ZPixmap, ximage->ximage, x, y);
661 if (ximagesrc->xcontext->use_xshm) {
662 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
663 XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
664 ximage->ximage, ximagesrc->startx, ximagesrc->starty, AllPlanes);
667 #endif /* HAVE_XSHM */
669 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
670 if (ximagesrc->remote) {
671 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
672 ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
673 ximagesrc->height, AllPlanes, ZPixmap, ximage->ximage, 0, 0);
676 XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
677 ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
678 ximagesrc->height, AllPlanes, ZPixmap);
686 if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {
688 GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
690 if (ximagesrc->cursor_image)
691 XFree (ximagesrc->cursor_image);
692 ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
693 if (ximagesrc->cursor_image != NULL) {
694 int cx, cy, i, j, count;
695 int startx, starty, iwidth, iheight;
696 gboolean cursor_in_image = TRUE;
698 cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
701 cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
704 count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
706 /* only get where cursor last was, if it is in our range */
707 if (ximagesrc->endx > ximagesrc->startx &&
708 ximagesrc->endy > ximagesrc->starty) {
710 if (cx + ximagesrc->cursor_image->width < ximagesrc->startx ||
711 cx > ximagesrc->endx) {
713 cursor_in_image = FALSE;
714 } else if (cy + ximagesrc->cursor_image->height < ximagesrc->starty ||
715 cy > ximagesrc->endy) {
717 cursor_in_image = FALSE;
719 /* find intersect region */
721 startx = (cx < ximagesrc->startx) ? ximagesrc->startx : cx;
722 starty = (cy < ximagesrc->starty) ? ximagesrc->starty : cy;
723 iwidth = (cx + ximagesrc->cursor_image->width < ximagesrc->endx) ?
724 cx + ximagesrc->cursor_image->width - startx :
725 ximagesrc->endx - startx;
726 iheight = (cy + ximagesrc->cursor_image->height < ximagesrc->endy) ?
727 cy + ximagesrc->cursor_image->height - starty :
728 ximagesrc->endy - starty;
733 iwidth = ximagesrc->cursor_image->width;
734 iheight = ximagesrc->cursor_image->height;
737 if (cursor_in_image) {
738 GST_DEBUG_OBJECT (ximagesrc, "Cursor is in image so trying to draw it");
739 for (i = 0; i < count; i++)
740 ximagesrc->cursor_image->pixels[i] =
741 GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
742 /* copy those pixels across */
744 j < starty + iheight && j < ximagesrc->starty + ximagesrc->height;
747 i < startx + iwidth && i < ximagesrc->startx + ximagesrc->width;
752 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
753 cy) * ximagesrc->cursor_image->width + (i - cx))]);
755 (guint8 *) & (ximage->ximage->data[((j -
756 ximagesrc->starty) * ximagesrc->width + (i -
757 ximagesrc->startx)) * (ximagesrc->xcontext->bpp /
760 composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
769 if (ximagesrc->have_xdamage && ximagesrc->use_damage) {
770 /* need to ref ximage to put in last_ximage */
771 gst_buffer_ref (GST_BUFFER (ximage));
772 if (ximagesrc->last_ximage) {
773 gst_buffer_unref (GST_BUFFER (ximagesrc->last_ximage));
775 ximagesrc->last_ximage = ximage;
776 GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
783 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
785 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
786 GstXImageSrcBuffer *image;
787 GstClockTime base_time;
788 GstClockTime next_capture_ts;
790 gint64 next_frame_no;
792 if (!gst_ximage_src_recalc (s)) {
793 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
794 (_("Changing resolution at runtime is not yet supported.")), (NULL));
795 return GST_FLOW_ERROR;
798 if (s->fps_n <= 0 || s->fps_d <= 0)
799 return GST_FLOW_NOT_NEGOTIATED; /* FPS must be > 0 */
801 /* Now, we might need to wait for the next multiple of the fps
802 * before capturing */
805 if (GST_ELEMENT_CLOCK (s) == NULL) {
806 GST_OBJECT_UNLOCK (s);
807 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
808 (_("Cannot operate without a clock")), (NULL));
809 return GST_FLOW_ERROR;
812 base_time = GST_ELEMENT_CAST (s)->base_time;
813 next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
814 next_capture_ts -= base_time;
816 /* Figure out which 'frame number' position we're at, based on the cur time
818 next_frame_no = gst_util_uint64_scale (next_capture_ts,
819 s->fps_n, GST_SECOND * s->fps_d);
820 if (next_frame_no == s->last_frame_no) {
824 /* Need to wait for the next frame */
827 /* Figure out what the next frame time is */
828 next_capture_ts = gst_util_uint64_scale (next_frame_no,
829 s->fps_d * GST_SECOND, s->fps_n);
831 id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
832 next_capture_ts + base_time);
835 /* release the object lock while waiting */
836 GST_OBJECT_UNLOCK (s);
838 GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
840 ret = gst_clock_id_wait (id, NULL);
843 gst_clock_id_unref (id);
845 if (ret == GST_CLOCK_UNSCHEDULED) {
846 /* Got woken up by the unlock function */
847 GST_OBJECT_UNLOCK (s);
848 return GST_FLOW_WRONG_STATE;
850 /* Duration is a complete 1/fps frame duration */
851 dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
853 GstClockTime next_frame_ts;
855 GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
856 G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
857 G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
858 next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
859 s->fps_d * GST_SECOND, s->fps_n);
860 /* Frame duration is from now until the next expected capture time */
861 dur = next_frame_ts - next_capture_ts;
863 s->last_frame_no = next_frame_no;
864 GST_OBJECT_UNLOCK (s);
866 image = gst_ximage_src_ximage_get (s);
868 return GST_FLOW_ERROR;
870 *buf = GST_BUFFER (image);
871 GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
872 GST_BUFFER_DURATION (*buf) = dur;
878 gst_ximage_src_set_property (GObject * object, guint prop_id,
879 const GValue * value, GParamSpec * pspec)
881 GstXImageSrc *src = GST_XIMAGE_SRC (object);
884 case PROP_DISPLAY_NAME:
886 g_free (src->display_name);
887 src->display_name = g_strdup (g_value_get_string (value));
889 case PROP_SCREEN_NUM:
890 src->screen_num = g_value_get_uint (value);
892 case PROP_SHOW_POINTER:
893 src->show_pointer = g_value_get_boolean (value);
895 case PROP_USE_DAMAGE:
896 src->use_damage = g_value_get_boolean (value);
899 src->startx = g_value_get_uint (value);
902 src->starty = g_value_get_uint (value);
905 src->endx = g_value_get_uint (value);
908 src->endy = g_value_get_uint (value);
911 src->remote = g_value_get_boolean (value);
914 if (src->xcontext != NULL) {
915 g_warning ("ximagesrc window ID must be set before opening display");
918 src->xid = g_value_get_uint64 (value);
921 if (src->xcontext != NULL) {
922 g_warning ("ximagesrc window name must be set before opening display");
926 src->xname = g_strdup (g_value_get_string (value));
934 gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
937 GstXImageSrc *src = GST_XIMAGE_SRC (object);
940 case PROP_DISPLAY_NAME:
942 g_value_set_string (value, DisplayString (src->xcontext->disp));
944 g_value_set_string (value, src->display_name);
947 case PROP_SCREEN_NUM:
948 g_value_set_uint (value, src->screen_num);
950 case PROP_SHOW_POINTER:
951 g_value_set_boolean (value, src->show_pointer);
953 case PROP_USE_DAMAGE:
954 g_value_set_boolean (value, src->use_damage);
957 g_value_set_uint (value, src->startx);
960 g_value_set_uint (value, src->starty);
963 g_value_set_uint (value, src->endx);
966 g_value_set_uint (value, src->endy);
969 g_value_set_boolean (value, src->remote);
972 g_value_set_uint64 (value, src->xid);
975 g_value_set_string (value, src->xname);
978 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
984 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
986 g_mutex_lock (ximagesrc->pool_lock);
987 while (ximagesrc->buffer_pool != NULL) {
988 GstXImageSrcBuffer *ximage = ximagesrc->buffer_pool->data;
990 gst_ximage_buffer_free (ximage);
992 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
993 ximagesrc->buffer_pool);
995 g_mutex_unlock (ximagesrc->pool_lock);
999 gst_ximage_src_base_init (gpointer g_class)
1001 GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
1003 gst_element_class_set_details_simple (ec, "Ximage video source",
1005 "Creates a screenshot video stream",
1006 "Lutz Mueller <lutz@users.sourceforge.net>, "
1007 "Jan Schmidt <thaytan@mad.scientist.com>, "
1008 "Zaheer Merali <zaheerabbas at merali dot org>");
1009 gst_element_class_add_static_pad_template (ec, &t);
1013 gst_ximage_src_dispose (GObject * object)
1015 /* Drop references in the buffer_pool */
1016 gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
1018 G_OBJECT_CLASS (parent_class)->dispose (object);
1022 gst_ximage_src_finalize (GObject * object)
1024 GstXImageSrc *src = GST_XIMAGE_SRC (object);
1027 ximageutil_xcontext_clear (src->xcontext);
1029 g_free (src->xname);
1030 g_mutex_free (src->pool_lock);
1031 g_mutex_free (src->x_lock);
1033 G_OBJECT_CLASS (parent_class)->finalize (object);
1037 gst_ximage_src_get_caps (GstBaseSrc * bs)
1039 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1040 GstXContext *xcontext;
1043 if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
1045 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1048 if (!gst_ximage_src_recalc (s))
1050 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1053 xcontext = s->xcontext;
1054 width = s->xcontext->width;
1055 height = s->xcontext->height;
1056 if (s->xwindow != 0) {
1057 XWindowAttributes attrs;
1058 int status = XGetWindowAttributes (s->xcontext->disp, s->xwindow, &attrs);
1060 width = attrs.width;
1061 height = attrs.height;
1065 /* property comments say 0 means right/bottom, means we can't capture
1066 the top left pixel alone */
1068 s->endx = width - 1;
1070 s->endy = height - 1;
1072 if (s->endx >= s->startx && s->endy >= s->starty) {
1073 /* this means user has put in values */
1074 if (s->startx < xcontext->width && s->endx < xcontext->width &&
1075 s->starty < xcontext->height && s->endy < xcontext->height &&
1076 s->startx >= 0 && s->starty >= 0) {
1077 /* values are fine */
1078 s->width = width = s->endx - s->startx + 1;
1079 s->height = height = s->endy - s->starty + 1;
1082 ("User put in co-ordinates overshooting the X resolution, setting to full screen");
1085 s->endx = width - 1;
1086 s->endy = height - 1;
1089 GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
1092 s->endx = width - 1;
1093 s->endy = height - 1;
1095 GST_DEBUG ("width = %d, height=%d", width, height);
1096 return gst_caps_new_simple ("video/x-raw-rgb",
1097 "bpp", G_TYPE_INT, xcontext->bpp,
1098 "depth", G_TYPE_INT, xcontext->depth,
1099 "endianness", G_TYPE_INT, xcontext->endianness,
1100 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
1101 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
1102 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
1103 "width", G_TYPE_INT, width,
1104 "height", G_TYPE_INT, height,
1105 "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1106 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1111 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
1113 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1114 GstStructure *structure;
1115 const GValue *new_fps;
1117 /* If not yet opened, disallow setcaps until later */
1121 /* The only thing that can change is the framerate downstream wants */
1122 structure = gst_caps_get_structure (caps, 0);
1123 new_fps = gst_structure_get_value (structure, "framerate");
1127 /* Store this FPS for use when generating buffers */
1128 s->fps_n = gst_value_get_fraction_numerator (new_fps);
1129 s->fps_d = gst_value_get_fraction_denominator (new_fps);
1131 GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1137 gst_ximage_src_fixate (GstPad * pad, GstCaps * caps)
1140 GstStructure *structure;
1142 for (i = 0; i < gst_caps_get_size (caps); ++i) {
1143 structure = gst_caps_get_structure (caps, i);
1145 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1150 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1152 GObjectClass *gc = G_OBJECT_CLASS (klass);
1153 GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
1154 GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
1156 gc->set_property = gst_ximage_src_set_property;
1157 gc->get_property = gst_ximage_src_get_property;
1158 gc->dispose = gst_ximage_src_dispose;
1159 gc->finalize = gst_ximage_src_finalize;
1161 g_object_class_install_property (gc, PROP_DISPLAY_NAME,
1162 g_param_spec_string ("display-name", "Display", "X Display Name", NULL,
1163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1164 g_object_class_install_property (gc, PROP_SCREEN_NUM,
1165 g_param_spec_uint ("screen-num", "Screen number", "X Screen Number",
1166 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1167 g_object_class_install_property (gc, PROP_SHOW_POINTER,
1168 g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
1169 "Show mouse pointer (if XFixes extension enabled)", TRUE,
1170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1172 * GstXImageSrc:use-damage
1174 * Use XDamage (if the XDamage extension is enabled)
1178 g_object_class_install_property (gc, PROP_USE_DAMAGE,
1179 g_param_spec_boolean ("use-damage", "Use XDamage",
1180 "Use XDamage (if XDamage extension enabled)", TRUE,
1181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1183 * GstXImageSrc:startx
1185 * X coordinate of top left corner of area to be recorded
1186 * (0 for top left of screen)
1190 g_object_class_install_property (gc, PROP_STARTX,
1191 g_param_spec_uint ("startx", "Start X co-ordinate",
1192 "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
1193 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1195 * GstXImageSrc:starty
1197 * Y coordinate of top left corner of area to be recorded
1198 * (0 for top left of screen)
1202 g_object_class_install_property (gc, PROP_STARTY,
1203 g_param_spec_uint ("starty", "Start Y co-ordinate",
1204 "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
1205 0, G_MAXINT, 0, G_PARAM_READWRITE));
1209 * X coordinate of bottom right corner of area to be recorded
1210 * (0 for bottom right of screen)
1214 g_object_class_install_property (gc, PROP_ENDX,
1215 g_param_spec_uint ("endx", "End X",
1216 "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1217 0, G_MAXINT, 0, G_PARAM_READWRITE));
1221 * Y coordinate of bottom right corner of area to be recorded
1222 * (0 for bottom right of screen)
1226 g_object_class_install_property (gc, PROP_ENDY,
1227 g_param_spec_uint ("endy", "End Y",
1228 "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1229 0, G_MAXINT, 0, G_PARAM_READWRITE));
1232 * GstXImageSrc:remote
1234 * Whether the X display is remote. The element will try to use alternate calls
1235 * known to work better with remote displays.
1239 g_object_class_install_property (gc, PROP_REMOTE,
1240 g_param_spec_boolean ("remote", "Remote dispay",
1241 "Whether the display is remote", FALSE,
1242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1247 * The XID of the window to capture. 0 for the root window (default).
1251 g_object_class_install_property (gc, PROP_XID,
1252 g_param_spec_uint64 ("xid", "Window XID",
1253 "Window XID to capture from", 0, G_MAXUINT64, 0,
1254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1257 * GstXImageSrc:xname
1259 * The name of the window to capture, if any.
1263 g_object_class_install_property (gc, PROP_XNAME,
1264 g_param_spec_string ("xname", "Window name",
1265 "Window name to capture from", NULL,
1266 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1268 parent_class = g_type_class_peek_parent (klass);
1270 push_class->create = gst_ximage_src_create;
1271 bc->get_caps = gst_ximage_src_get_caps;
1272 bc->set_caps = gst_ximage_src_set_caps;
1273 bc->start = gst_ximage_src_start;
1274 bc->stop = gst_ximage_src_stop;
1275 bc->unlock = gst_ximage_src_unlock;
1279 gst_ximage_src_init (GstXImageSrc * ximagesrc, GstXImageSrcClass * klass)
1281 gst_base_src_set_format (GST_BASE_SRC (ximagesrc), GST_FORMAT_TIME);
1282 gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1283 gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (ximagesrc),
1284 gst_ximage_src_fixate);
1286 ximagesrc->pool_lock = g_mutex_new ();
1287 ximagesrc->x_lock = g_mutex_new ();
1288 ximagesrc->show_pointer = TRUE;
1289 ximagesrc->use_damage = TRUE;
1290 ximagesrc->startx = 0;
1291 ximagesrc->starty = 0;
1292 ximagesrc->endx = 0;
1293 ximagesrc->endy = 0;
1294 ximagesrc->remote = FALSE;
1298 plugin_init (GstPlugin * plugin)
1302 GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
1303 "ximagesrc element debug");
1305 ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
1306 GST_TYPE_XIMAGE_SRC);
1311 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1314 "X11 video input plugin using standard Xlib calls",
1315 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);