--- /dev/null
+/* GStreamer
+ *
+ * Copyright (C) 2006 Zaheer Merali <zaheerabbas at merali dot org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * SECTION:element-xcbimagesrc
+ * @title: xcbimagesrc
+ *
+ * This element captures your X Display and creates raw RGB video. It uses
+ * the XDamage extension if available to only capture areas of the screen that
+ * have changed since the last frame. It uses the XFixes extension if
+ * available to also capture your mouse pointer. By default it will fixate to
+ * 25 frames per second.
+ *
+ * ## Example pipelines
+ * |[
+ * gst-launch-1.0 xcbimagesrc ! video/x-raw,framerate=5/1 ! videoconvert ! theoraenc ! oggmux ! filesink location=desktop.ogg
+ * ]| Encodes your X display to an Ogg theora video at 5 frames per second.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "gstxcbimagesrc.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+
+#include <gst/gst.h>
+//#include <gst/gst-i18n-plugin.h>
+#define _(x) x
+#include <gst/video/video.h>
+
+//#include "gst/glib-compat-private.h"
+
+GST_DEBUG_CATEGORY_STATIC (gst_debug_xcbimage_src);
+#define GST_CAT_DEFAULT gst_debug_xcbimage_src
+
+static GstStaticPadTemplate t =
+GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
+ GST_STATIC_CAPS ("video/x-raw, "
+ "framerate = (fraction) [ 0, MAX ], "
+ "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
+ "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
+
+enum
+{
+ PROP_0,
+ PROP_DISPLAY_NAME,
+ PROP_SHOW_POINTER,
+ PROP_USE_DAMAGE,
+ PROP_STARTX,
+ PROP_STARTY,
+ PROP_ENDX,
+ PROP_ENDY,
+ PROP_REMOTE,
+ PROP_XID,
+ PROP_XNAME,
+};
+
+#define gst_xcbimage_src_parent_class parent_class
+G_DEFINE_TYPE (GstXcbImageSrc, gst_xcbimage_src, GST_TYPE_PUSH_SRC);
+
+static GstCaps *gst_xcbimage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
+static void gst_xcbimage_src_clear_bufpool (GstXcbImageSrc * xcbimagesrc);
+
+/* Called when a buffer is returned from the pipeline */
+static gboolean
+gst_xcbimage_src_return_buf (GstXcbImageSrc * xcbimagesrc, GstBuffer * xcbimage)
+{
+ GstMetaXcbImage *meta = GST_META_XCBIMAGE_GET (xcbimage);
+ /* True will make dispose free the buffer, while false will keep it */
+ gboolean ret = TRUE;
+
+ /* If our geometry changed we can't reuse that image. */
+ if ((meta->width != xcbimagesrc->width) || (meta->height != xcbimagesrc->height)) {
+ GST_DEBUG_OBJECT (xcbimagesrc,
+ "destroy image %p as its size changed %dx%d vs current %dx%d",
+ xcbimage, meta->width, meta->height, xcbimagesrc->width, xcbimagesrc->height);
+ g_mutex_lock (&xcbimagesrc->x_lock);
+ gst_xcbimageutil_xcbimage_destroy (xcbimagesrc->xcontext, xcbimage);
+ g_mutex_unlock (&xcbimagesrc->x_lock);
+ } else {
+ /* In that case we can reuse the image and add it to our image pool. */
+ GST_LOG_OBJECT (xcbimagesrc, "recycling image %p in pool", xcbimage);
+ /* need to increment the refcount again to recycle */
+ gst_buffer_ref (xcbimage);
+ g_mutex_lock (&xcbimagesrc->pool_lock);
+ GST_BUFFER_FLAGS (GST_BUFFER (xcbimage)) = 0; /* clear out any flags from the previous use */
+ xcbimagesrc->buffer_pool = g_slist_prepend (xcbimagesrc->buffer_pool, xcbimage);
+ g_mutex_unlock (&xcbimagesrc->pool_lock);
+ ret = FALSE;
+ }
+
+ return ret;
+}
+
+static Window
+gst_xcbimage_src_find_window (GstXcbImageSrc * src, Window root, const char *name)
+{
+ Window *children;
+ Window window = 0, root_return, parent_return;
+ unsigned int nchildren;
+ char *tmpname;
+ int n, status;
+
+ status = XFetchName (src->xcontext->disp, root, &tmpname);
+ if (status && !strcmp (name, tmpname))
+ return root;
+
+ status =
+ XQueryTree (src->xcontext->disp, root, &root_return, &parent_return,
+ &children, &nchildren);
+ if (!status || !children)
+ return (Window) 0;
+
+ for (n = 0; n < nchildren; ++n) {
+ window = gst_xcbimage_src_find_window (src, children[n], name);
+ if (window != 0)
+ break;
+ }
+
+ XFree (children);
+ return window;
+}
+
+static gboolean
+gst_xcbimage_src_open_display (GstXcbImageSrc * s, const gchar * name)
+{
+ g_return_val_if_fail (GST_IS_XCBIMAGE_SRC (s), FALSE);
+
+ if (s->xcontext != NULL)
+ return TRUE;
+
+ g_mutex_lock (&s->x_lock);
+ s->xcontext = xcbimageutil_xcontext_get (GST_ELEMENT (s), name);
+ if (s->xcontext == NULL) {
+ g_mutex_unlock (&s->x_lock);
+ GST_ELEMENT_ERROR (s, RESOURCE, OPEN_READ,
+ ("Could not open X display for reading"),
+ ("NULL returned from getting xcontext"));
+ return FALSE;
+ }
+ s->width = s->xcontext->width;
+ s->height = s->xcontext->height;
+
+ s->xwindow = s->xcontext->root;
+ if (s->xid != 0 || s->xname) {
+ int status;
+ XWindowAttributes attrs;
+ Window window;
+ int x, y;
+ Window child;
+ Bool coord_translated;
+
+ if (s->xid != 0) {
+ status = XGetWindowAttributes (s->xcontext->disp, s->xid, &attrs);
+ if (status) {
+ GST_DEBUG_OBJECT (s, "Found window XID %" G_GUINT64_FORMAT, s->xid);
+ s->xwindow = s->xid;
+ goto window_found;
+ } else {
+ GST_WARNING_OBJECT (s, "Failed to get window %" G_GUINT64_FORMAT
+ " attributes", s->xid);
+ }
+ }
+
+ if (s->xname) {
+ GST_DEBUG_OBJECT (s, "Looking for window %s", s->xname);
+ window = gst_xcbimage_src_find_window (s, s->xcontext->root, s->xname);
+ if (window != 0) {
+ GST_DEBUG_OBJECT (s, "Found window named %s, ", s->xname);
+ status = XGetWindowAttributes (s->xcontext->disp, window, &attrs);
+ if (status) {
+ s->xwindow = window;
+ goto window_found;
+ } else {
+ GST_WARNING_OBJECT (s, "Failed to get window attributes for "
+ "window named %s", s->xname);
+ }
+ }
+ }
+
+ GST_INFO_OBJECT (s, "Using root window");
+ goto use_root_window;
+
+ window_found:
+ g_assert (s->xwindow != 0);
+ s->width = attrs.width;
+ s->height = attrs.height;
+
+ coord_translated = XTranslateCoordinates (s->xcontext->disp, s->xwindow,
+ s->xcontext->root, 0, 0, &x, &y, &child);
+ if (coord_translated) {
+ s->x = x;
+ s->y = y;
+ } else {
+ s->x = 0;
+ s->y = 0;
+ }
+
+ GST_INFO_OBJECT (s, "Using default window size of %dx%d at location %d,%d",
+ s->width, s->height, s->x, s->y);
+ }
+use_root_window:
+
+#ifdef HAVE_XFIXES
+ /* check if xfixes supported */
+ {
+ int error_base;
+
+ if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
+ &error_base)) {
+ s->have_xfixes = TRUE;
+ GST_DEBUG_OBJECT (s, "X Server supports XFixes");
+ } else {
+
+ GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
+ }
+ }
+
+#ifdef HAVE_XDAMAGE
+ /* check if xdamage is supported */
+ {
+ int error_base;
+ long evmask = NoEventMask;
+
+ s->have_xdamage = FALSE;
+ s->damage = None;
+ s->damage_copy_gc = None;
+ s->damage_region = None;
+
+ if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
+ &error_base)) {
+ s->damage =
+ XDamageCreate (s->xcontext->disp, s->xwindow, XDamageReportNonEmpty);
+ if (s->damage != None) {
+ s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
+ if (s->damage_region != None) {
+ XGCValues values;
+
+ GST_DEBUG_OBJECT (s, "Using XDamage extension");
+ values.subwindow_mode = IncludeInferiors;
+ s->damage_copy_gc = XCreateGC (s->xcontext->disp,
+ s->xwindow, GCSubwindowMode, &values);
+ XSelectInput (s->xcontext->disp, s->xwindow, evmask);
+
+ s->have_xdamage = TRUE;
+ } else {
+ XDamageDestroy (s->xcontext->disp, s->damage);
+ s->damage = None;
+ }
+ } else
+ GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
+ } else {
+ GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
+ }
+ }
+#endif
+#endif
+
+ g_mutex_unlock (&s->x_lock);
+
+ if (s->xcontext == NULL)
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean
+gst_xcbimage_src_start (GstBaseSrc * basesrc)
+{
+ GstXcbImageSrc *s = GST_XCBIMAGE_SRC (basesrc);
+
+ s->last_frame_no = -1;
+#ifdef HAVE_XDAMAGE
+ if (s->last_ximage)
+ gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
+ s->last_ximage = NULL;
+#endif
+ return gst_xcbimage_src_open_display (s, s->display_name);
+}
+
+static gboolean
+gst_xcbimage_src_stop (GstBaseSrc * basesrc)
+{
+ GstXcbImageSrc *src = GST_XCBIMAGE_SRC (basesrc);
+
+#ifdef HAVE_XDAMAGE
+ if (src->last_ximage)
+ gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
+ src->last_ximage = NULL;
+#endif
+
+ gst_xcbimage_src_clear_bufpool (src);
+
+#ifdef HAVE_XFIXES
+ if (src->cursor_image)
+ XFree (src->cursor_image);
+ src->cursor_image = NULL;
+#endif
+
+ if (src->xcontext) {
+ g_mutex_lock (&src->x_lock);
+
+#ifdef HAVE_XDAMAGE
+ if (src->damage_copy_gc != None) {
+ XFreeGC (src->xcontext->disp, src->damage_copy_gc);
+ src->damage_copy_gc = None;
+ }
+ if (src->damage_region != None) {
+ XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
+ src->damage_region = None;
+ }
+ if (src->damage != None) {
+ XDamageDestroy (src->xcontext->disp, src->damage);
+ src->damage = None;
+ }
+#endif
+
+ xcbimageutil_xcontext_clear (src->xcontext);
+ src->xcontext = NULL;
+ g_mutex_unlock (&src->x_lock);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+gst_xcbimage_src_unlock (GstBaseSrc * basesrc)
+{
+ GstXcbImageSrc *src = GST_XCBIMAGE_SRC (basesrc);
+
+ /* Awaken the create() func if it's waiting on the clock */
+ GST_OBJECT_LOCK (src);
+ if (src->clock_id) {
+ GST_DEBUG_OBJECT (src, "Waking up waiting clock");
+ gst_clock_id_unschedule (src->clock_id);
+ }
+ GST_OBJECT_UNLOCK (src);
+
+ return TRUE;
+}
+
+static gboolean
+gst_xcbimage_src_recalc (GstXcbImageSrc * src)
+{
+ if (!src->xcontext)
+ return FALSE;
+
+ /* Maybe later we can check the display hasn't changed size */
+ /* We could use XQueryPointer to get only the current window. */
+ return TRUE;
+}
+
+#ifdef HAVE_XFIXES
+static gboolean
+gst_xcbimage_is_pointer_in_region (GstXcbImageSrc * src)
+{
+ Window window_returned;
+ int root_x, root_y;
+ int win_x, win_y;
+ unsigned int mask_return;
+ Bool on_window;
+
+ on_window = XQueryPointer (src->xcontext->disp, src->xwindow,
+ &window_returned, &window_returned, &root_x, &root_y, &win_x, &win_y,
+ &mask_return);
+
+ return (on_window && (win_x >= src->startx) && (win_y >= src->starty) &&
+ (win_x < src->endx) && (win_y < src->endy));
+}
+#endif
+
+#ifdef HAVE_XFIXES
+static void
+composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
+{
+ guint8 r = src[2];
+ guint8 g = src[1];
+ guint8 b = src[0];
+ guint8 a = src[3];
+ guint8 dr, dg, db;
+ guint32 color;
+ gint r_shift, r_max, r_shift_out;
+ gint g_shift, g_max, g_shift_out;
+ gint b_shift, b_max, b_shift_out;
+
+ switch (xcontext->bpp) {
+ case 8:
+ color = *dest;
+ break;
+ case 16:
+ color = GUINT16_FROM_LE (*(guint16 *) (dest));
+ break;
+ case 32:
+ color = GUINT32_FROM_LE (*(guint32 *) (dest));
+ break;
+ default:
+ /* Should not reach here */
+ g_return_if_reached ();
+ }
+
+ /* possible optimisation:
+ * move the code that finds shift and max in the _link function */
+ for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
+ for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
+ g_shift++);
+ for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
+
+ for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
+ r_shift_out++);
+ for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
+ g_shift_out++);
+ for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
+ b_shift_out++);
+
+
+ r_max = (xcontext->visual->red_mask >> r_shift);
+ b_max = (xcontext->visual->blue_mask >> b_shift);
+ g_max = (xcontext->visual->green_mask >> g_shift);
+
+#define RGBXXX_R(x) (((x)>>r_shift) & (r_max))
+#define RGBXXX_G(x) (((x)>>g_shift) & (g_max))
+#define RGBXXX_B(x) (((x)>>b_shift) & (b_max))
+
+ dr = (RGBXXX_R (color) * 255) / r_max;
+ dg = (RGBXXX_G (color) * 255) / g_max;
+ db = (RGBXXX_B (color) * 255) / b_max;
+
+ dr = (r * a + (0xff - a) * dr) / 0xff;
+ dg = (g * a + (0xff - a) * dg) / 0xff;
+ db = (b * a + (0xff - a) * db) / 0xff;
+
+ color = (((dr * r_max) / 255) << r_shift_out) +
+ (((dg * g_max) / 255) << g_shift_out) +
+ (((db * b_max) / 255) << b_shift_out);
+
+ switch (xcontext->bpp) {
+ case 8:
+ *dest = color;
+ break;
+ case 16:
+ *(guint16 *) (dest) = color;
+ break;
+ case 32:
+ *(guint32 *) (dest) = color;
+ break;
+ default:
+ g_warning ("bpp %d not supported\n", xcontext->bpp);
+ }
+}
+#endif
+
+#ifdef HAVE_XDAMAGE
+static void
+copy_buffer (GstBuffer * dest, GstBuffer * src)
+{
+ GstMapInfo map;
+
+ gst_buffer_map (src, &map, GST_MAP_READ);
+ gst_buffer_fill (dest, 0, map.data, map.size);
+ gst_buffer_unmap (src, &map);
+}
+#endif
+
+/* Retrieve an XcbImageSrcBuffer, preferably from our
+ * pool of existing images and populate it from the window */
+static GstBuffer *
+gst_xcbimage_src_xcbimage_get (GstXcbImageSrc * xcbimagesrc)
+{
+ GstBuffer *xcbimage = NULL;
+ GstMetaXcbImage *meta;
+
+ g_mutex_lock (&xcbimagesrc->pool_lock);
+ while (xcbimagesrc->buffer_pool != NULL) {
+ xcbimage = xcbimagesrc->buffer_pool->data;
+
+ meta = GST_META_XCBIMAGE_GET (xcbimage);
+
+ xcbimagesrc->buffer_pool = g_slist_delete_link (xcbimagesrc->buffer_pool,
+ xcbimagesrc->buffer_pool);
+
+ if ((meta->width == xcbimagesrc->width) ||
+ (meta->height == xcbimagesrc->height))
+ break;
+
+ gst_xcbimage_buffer_free (xcbimage);
+ xcbimage = NULL;
+ }
+ g_mutex_unlock (&xcbimagesrc->pool_lock);
+
+ if (xcbimage == NULL) {
+ GST_DEBUG_OBJECT (xcbimagesrc, "creating image (%dx%d)",
+ xcbimagesrc->width, xcbimagesrc->height);
+
+ g_mutex_lock (&xcbimagesrc->x_lock);
+ xcbimage = gst_xcbimageutil_xcbimage_new (xcbimagesrc->xcontext,
+ GST_ELEMENT (xcbimagesrc), xcbimagesrc->width, xcbimagesrc->height,
+ (BufferReturnFunc) (gst_xcbimage_src_return_buf));
+ if (xcbimage == NULL) {
+ GST_ELEMENT_ERROR (xcbimagesrc, RESOURCE, WRITE, (NULL),
+ ("could not create a %dx%d xcbimage", xcbimagesrc->width,
+ xcbimagesrc->height));
+ g_mutex_unlock (&xcbimagesrc->x_lock);
+ return NULL;
+ }
+
+ g_mutex_unlock (&xcbimagesrc->x_lock);
+ }
+
+ g_return_val_if_fail (GST_IS_XCBIMAGE_SRC (xcbimagesrc), NULL);
+
+ meta = GST_META_XCBIMAGE_GET (xcbimage);
+
+#ifdef HAVE_XDAMAGE
+ if (xcbimagesrc->have_xdamage && xcbimagesrc->use_damage &&
+ xcbimagesrc->last_ximage != NULL) {
+ XEvent ev;
+ gboolean have_damage = FALSE;
+
+ /* have_frame is TRUE when either the entire screen has been
+ * grabbed or when the last image has been copied */
+ gboolean have_frame = FALSE;
+
+ GST_DEBUG_OBJECT (xcbimagesrc, "Retrieving screen using XDamage");
+
+ do {
+ XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *) (&ev);
+
+ XNextEvent (xcbimagesrc->xcontext->disp, &ev);
+
+ if (ev.type == xcbimagesrc->damage_event_base + XDamageNotify &&
+ damage_ev->level == XDamageReportNonEmpty) {
+
+ XDamageSubtract (xcbimagesrc->xcontext->disp, xcbimagesrc->damage, None,
+ xcbimagesrc->damage_region);
+ have_damage = TRUE;
+ }
+ } while (XPending (xcbimagesrc->xcontext->disp));
+
+ if (have_damage) {
+ XRectangle *rects;
+ int nrects;
+
+ /* Now copy out all of the damaged rectangles. */
+ rects =
+ XFixesFetchRegion (xcbimagesrc->xcontext->disp,
+ xcbimagesrc->damage_region, &nrects);
+ if (rects != NULL) {
+ int i;
+
+ if (!have_frame) {
+ GST_LOG_OBJECT (xcbimagesrc,
+ "Copying from last frame xcbimage->size: %" G_GSIZE_FORMAT,
+ gst_buffer_get_size (xcbimage));
+ copy_buffer (xcbimage, xcbimagesrc->last_ximage);
+ have_frame = TRUE;
+ }
+ for (i = 0; i < nrects; i++) {
+ GST_LOG_OBJECT (xcbimagesrc,
+ "Damaged sub-region @ %d,%d size %dx%d reported",
+ rects[i].x, rects[i].y, rects[i].width, rects[i].height);
+
+ /* if we only want a small area, clip this damage region to
+ * area we want */
+ if (xcbimagesrc->endx > xcbimagesrc->startx &&
+ xcbimagesrc->endy > xcbimagesrc->starty) {
+ /* see if damage area intersects */
+ if (rects[i].x + rects[i].width - 1 < xcbimagesrc->startx ||
+ rects[i].x > xcbimagesrc->endx) {
+ /* trivial reject */
+ } else if (rects[i].y + rects[i].height - 1 < xcbimagesrc->starty ||
+ rects[i].y > xcbimagesrc->endy) {
+ /* trivial reject */
+ } else {
+ /* find intersect region */
+ int startx, starty, width, height;
+
+ startx = (rects[i].x < xcbimagesrc->startx) ? xcbimagesrc->startx :
+ rects[i].x;
+ starty = (rects[i].y < xcbimagesrc->starty) ? xcbimagesrc->starty :
+ rects[i].y;
+ width = (rects[i].x + rects[i].width - 1 < xcbimagesrc->endx) ?
+ rects[i].x + rects[i].width - startx :
+ xcbimagesrc->endx - startx + 1;
+ height = (rects[i].y + rects[i].height - 1 < xcbimagesrc->endy) ?
+ rects[i].y + rects[i].height - starty : xcbimagesrc->endy -
+ starty + 1;
+
+ GST_LOG_OBJECT (xcbimagesrc,
+ "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
+ startx, starty, width, height);
+ XGetSubImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ startx, starty, width, height, AllPlanes, ZPixmap,
+ meta->ximage, startx - xcbimagesrc->startx,
+ starty - xcbimagesrc->starty);
+ }
+ } else {
+
+ GST_LOG_OBJECT (xcbimagesrc,
+ "Retrieving damaged sub-region @ %d,%d size %dx%d",
+ rects[i].x, rects[i].y, rects[i].width, rects[i].height);
+
+ XGetSubImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ rects[i].x, rects[i].y,
+ rects[i].width, rects[i].height,
+ AllPlanes, ZPixmap, meta->ximage, rects[i].x, rects[i].y);
+ }
+ }
+ XFree (rects);
+ }
+ }
+ if (!have_frame) {
+ GST_LOG_OBJECT (xcbimagesrc,
+ "Copying from last frame xcbimage->size: %" G_GSIZE_FORMAT,
+ gst_buffer_get_size (xcbimage));
+ copy_buffer (xcbimage, xcbimagesrc->last_ximage);
+ }
+#ifdef HAVE_XFIXES
+ /* re-get area where last mouse pointer was but only if in our clipping
+ * bounds */
+ if (xcbimagesrc->cursor_image) {
+ gint x, y, width, height;
+
+ x = xcbimagesrc->cursor_image->x - xcbimagesrc->cursor_image->xhot -
+ xcbimagesrc->x;
+ y = xcbimagesrc->cursor_image->y - xcbimagesrc->cursor_image->yhot -
+ xcbimagesrc->y;
+ width = xcbimagesrc->cursor_image->width;
+ height = xcbimagesrc->cursor_image->height;
+
+ /* bounds checking */
+ if (x < 0)
+ x = 0;
+ if (y < 0)
+ y = 0;
+ if (x + width > xcbimagesrc->xcontext->width)
+ width = xcbimagesrc->xcontext->width - x;
+ if (y + height > xcbimagesrc->xcontext->height)
+ height = xcbimagesrc->xcontext->height - y;
+ g_assert (x >= 0);
+ g_assert (y >= 0);
+ GST_DEBUG_OBJECT (xcbimagesrc,
+ "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
+ x, y, width, height, xcbimagesrc->startx, xcbimagesrc->starty,
+ xcbimagesrc->endx, xcbimagesrc->endy);
+ /* only get where cursor last was, if it is in our range */
+ if (xcbimagesrc->endx > xcbimagesrc->startx &&
+ xcbimagesrc->endy > xcbimagesrc->starty) {
+ /* check bounds */
+ if (x + width < xcbimagesrc->startx || x > xcbimagesrc->endx) {
+ /* trivial reject */
+ } else if (y + height < xcbimagesrc->starty || y > xcbimagesrc->endy) {
+ /* trivial reject */
+ } else {
+ /* find intersect region */
+ int startx, starty, iwidth, iheight;
+
+ startx = (x < xcbimagesrc->startx) ? xcbimagesrc->startx : x;
+ starty = (y < xcbimagesrc->starty) ? xcbimagesrc->starty : y;
+ iwidth = (x + width < xcbimagesrc->endx) ?
+ x + width - startx : xcbimagesrc->endx - startx;
+ iheight = (y + height < xcbimagesrc->endy) ?
+ y + height - starty : xcbimagesrc->endy - starty;
+ GST_DEBUG_OBJECT (xcbimagesrc, "Removing cursor from %d,%d", x, y);
+ XGetSubImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
+ meta->ximage, startx - xcbimagesrc->startx,
+ starty - xcbimagesrc->starty);
+ }
+ } else {
+
+ GST_DEBUG_OBJECT (xcbimagesrc, "Removing cursor from %d,%d", x, y);
+ XGetSubImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ x, y, width, height, AllPlanes, ZPixmap, meta->ximage, x, y);
+ }
+ }
+#endif
+
+
+ } else {
+#endif
+
+#ifdef HAVE_XSHM
+ if (xcbimagesrc->xcontext->use_xshm) {
+ GST_DEBUG_OBJECT (xcbimagesrc, "Retrieving screen using XShm");
+ XShmGetImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ meta->ximage, xcbimagesrc->startx, xcbimagesrc->starty, AllPlanes);
+
+ } else
+#endif /* HAVE_XSHM */
+ {
+ GST_DEBUG_OBJECT (xcbimagesrc, "Retrieving screen using XGetImage");
+ if (xcbimagesrc->remote) {
+ XGetSubImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ xcbimagesrc->startx, xcbimagesrc->starty, xcbimagesrc->width,
+ xcbimagesrc->height, AllPlanes, ZPixmap, meta->ximage, 0, 0);
+ } else {
+ meta->ximage =
+ XGetImage (xcbimagesrc->xcontext->disp, xcbimagesrc->xwindow,
+ xcbimagesrc->startx, xcbimagesrc->starty, xcbimagesrc->width,
+ xcbimagesrc->height, AllPlanes, ZPixmap);
+ }
+ }
+#ifdef HAVE_XDAMAGE
+ }
+#endif
+
+#ifdef HAVE_XFIXES
+ if (xcbimagesrc->show_pointer && xcbimagesrc->have_xfixes
+ && gst_xcbimage_is_pointer_in_region (xcbimagesrc)) {
+
+ GST_DEBUG_OBJECT (xcbimagesrc, "Using XFixes to draw cursor");
+ /* get cursor */
+ if (xcbimagesrc->cursor_image)
+ XFree (xcbimagesrc->cursor_image);
+ xcbimagesrc->cursor_image = XFixesGetCursorImage (xcbimagesrc->xcontext->disp);
+ if (xcbimagesrc->cursor_image != NULL) {
+ int cx, cy, i, j, count;
+ int startx, starty, iwidth, iheight;
+ gboolean cursor_in_image = TRUE;
+
+ cx = xcbimagesrc->cursor_image->x - xcbimagesrc->cursor_image->xhot -
+ xcbimagesrc->x;
+ cy = xcbimagesrc->cursor_image->y - xcbimagesrc->cursor_image->yhot -
+ xcbimagesrc->y;
+ count = xcbimagesrc->cursor_image->width * xcbimagesrc->cursor_image->height;
+
+ /* only get where cursor last was, if it is in our range */
+ if (xcbimagesrc->endx > xcbimagesrc->startx &&
+ xcbimagesrc->endy > xcbimagesrc->starty) {
+ /* check bounds */
+ if (cx + xcbimagesrc->cursor_image->width < (int) xcbimagesrc->startx ||
+ cx > (int) xcbimagesrc->endx) {
+ /* trivial reject */
+ cursor_in_image = FALSE;
+ } else if (cy + xcbimagesrc->cursor_image->height <
+ (int) xcbimagesrc->starty || cy > (int) xcbimagesrc->endy) {
+ /* trivial reject */
+ cursor_in_image = FALSE;
+ } else {
+ /* find intersect region */
+
+ startx = (cx < (int) xcbimagesrc->startx) ? xcbimagesrc->startx : cx;
+ starty = (cy < (int) xcbimagesrc->starty) ? xcbimagesrc->starty : cy;
+ iwidth = (cx + xcbimagesrc->cursor_image->width < xcbimagesrc->endx) ?
+ cx + xcbimagesrc->cursor_image->width - startx :
+ xcbimagesrc->endx - startx;
+ iheight =
+ (cy + xcbimagesrc->cursor_image->height <
+ xcbimagesrc->endy) ? cy + xcbimagesrc->cursor_image->height -
+ starty : xcbimagesrc->endy - starty;
+ }
+ } else {
+ startx = cx;
+ starty = cy;
+ iwidth = xcbimagesrc->cursor_image->width;
+ iheight = xcbimagesrc->cursor_image->height;
+ }
+
+ if (cursor_in_image) {
+ GST_DEBUG_OBJECT (xcbimagesrc, "Cursor is in image so trying to draw it");
+ for (i = 0; i < count; i++)
+ xcbimagesrc->cursor_image->pixels[i] =
+ GUINT_TO_LE (xcbimagesrc->cursor_image->pixels[i]);
+ /* copy those pixels across */
+ for (j = starty;
+ j < starty + iheight
+ && j < xcbimagesrc->starty + xcbimagesrc->height; j++) {
+ for (i = startx;
+ i < startx + iwidth
+ && i < xcbimagesrc->startx + xcbimagesrc->width; i++) {
+ guint8 *src, *dest;
+
+ src =
+ (guint8 *) & (xcbimagesrc->cursor_image->pixels[((j -
+ cy) * xcbimagesrc->cursor_image->width + (i - cx))]);
+ dest =
+ (guint8 *) & (meta->ximage->data[((j -
+ xcbimagesrc->starty) * xcbimagesrc->width + (i -
+ xcbimagesrc->startx)) *
+ (xcbimagesrc->xcontext->bpp / 8)]);
+
+ composite_pixel (xcbimagesrc->xcontext, (guint8 *) dest,
+ (guint8 *) src);
+ }
+ }
+ }
+ }
+ }
+#endif
+#ifdef HAVE_XDAMAGE
+ if (xcbimagesrc->have_xdamage && xcbimagesrc->use_damage) {
+ /* need to ref xcbimage to put in last_ximage */
+ gst_buffer_ref (xcbimage);
+ if (xcbimagesrc->last_ximage) {
+ gst_buffer_unref (xcbimagesrc->last_ximage);
+ }
+ xcbimagesrc->last_ximage = xcbimage;
+ GST_LOG_OBJECT (xcbimagesrc, "reffing current buffer for last_ximage");
+ }
+#endif
+ return xcbimage;
+}
+
+static GstFlowReturn
+gst_xcbimage_src_create (GstPushSrc * bs, GstBuffer ** buf)
+{
+ GstXcbImageSrc *s = GST_XCBIMAGE_SRC (bs);
+ GstBuffer *image;
+ GstClockTime base_time;
+ GstClockTime next_capture_ts;
+ GstClockTime dur;
+ gint64 next_frame_no;
+
+ if (!gst_xcbimage_src_recalc (s)) {
+ GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
+ (_("Changing resolution at runtime is not yet supported.")), (NULL));
+ return GST_FLOW_ERROR;
+ }
+
+ if (s->fps_n <= 0 || s->fps_d <= 0)
+ return GST_FLOW_NOT_NEGOTIATED; /* FPS must be > 0 */
+
+ /* Now, we might need to wait for the next multiple of the fps
+ * before capturing */
+
+ GST_OBJECT_LOCK (s);
+ if (GST_ELEMENT_CLOCK (s) == NULL) {
+ GST_OBJECT_UNLOCK (s);
+ GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
+ (_("Cannot operate without a clock")), (NULL));
+ return GST_FLOW_ERROR;
+ }
+
+ base_time = GST_ELEMENT_CAST (s)->base_time;
+ next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
+ next_capture_ts -= base_time;
+
+ /* Figure out which 'frame number' position we're at, based on the cur time
+ * and frame rate */
+ next_frame_no = gst_util_uint64_scale (next_capture_ts,
+ s->fps_n, GST_SECOND * s->fps_d);
+ if (next_frame_no == s->last_frame_no) {
+ GstClockID id;
+ GstClockReturn ret;
+
+ /* Need to wait for the next frame */
+ next_frame_no += 1;
+
+ /* Figure out what the next frame time is */
+ next_capture_ts = gst_util_uint64_scale (next_frame_no,
+ s->fps_d * GST_SECOND, s->fps_n);
+
+ id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
+ next_capture_ts + base_time);
+ s->clock_id = id;
+
+ /* release the object lock while waiting */
+ GST_OBJECT_UNLOCK (s);
+
+ GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
+ next_capture_ts);
+ ret = gst_clock_id_wait (id, NULL);
+ GST_OBJECT_LOCK (s);
+
+ gst_clock_id_unref (id);
+ s->clock_id = NULL;
+ if (ret == GST_CLOCK_UNSCHEDULED) {
+ /* Got woken up by the unlock function */
+ GST_OBJECT_UNLOCK (s);
+ return GST_FLOW_FLUSHING;
+ }
+ /* Duration is a complete 1/fps frame duration */
+ dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
+ } else {
+ GstClockTime next_frame_ts;
+
+ GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
+ G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
+ G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
+ next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
+ s->fps_d * GST_SECOND, s->fps_n);
+ /* Frame duration is from now until the next expected capture time */
+ dur = next_frame_ts - next_capture_ts;
+ }
+ s->last_frame_no = next_frame_no;
+ GST_OBJECT_UNLOCK (s);
+
+ image = gst_xcbimage_src_xcbimage_get (s);
+ if (!image)
+ return GST_FLOW_ERROR;
+
+ *buf = image;
+ GST_BUFFER_DTS (*buf) = GST_CLOCK_TIME_NONE;
+ GST_BUFFER_PTS (*buf) = next_capture_ts;
+ GST_BUFFER_DURATION (*buf) = dur;
+
+ return GST_FLOW_OK;
+}
+
+static void
+gst_xcbimage_src_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec)
+{
+ GstXcbImageSrc *src = GST_XCBIMAGE_SRC (object);
+
+ switch (prop_id) {
+ case PROP_DISPLAY_NAME:
+
+ g_free (src->display_name);
+ src->display_name = g_strdup (g_value_get_string (value));
+ break;
+ case PROP_SHOW_POINTER:
+ src->show_pointer = g_value_get_boolean (value);
+ break;
+ case PROP_USE_DAMAGE:
+ src->use_damage = g_value_get_boolean (value);
+ break;
+ case PROP_STARTX:
+ src->startx = g_value_get_uint (value);
+ break;
+ case PROP_STARTY:
+ src->starty = g_value_get_uint (value);
+ break;
+ case PROP_ENDX:
+ src->endx = g_value_get_uint (value);
+ break;
+ case PROP_ENDY:
+ src->endy = g_value_get_uint (value);
+ break;
+ case PROP_REMOTE:
+ src->remote = g_value_get_boolean (value);
+ break;
+ case PROP_XID:
+ if (src->xcontext != NULL) {
+ g_warning ("xcbimagesrc window ID must be set before opening display");
+ break;
+ }
+ src->xid = g_value_get_uint64 (value);
+ break;
+ case PROP_XNAME:
+ if (src->xcontext != NULL) {
+ g_warning ("xcbimagesrc window name must be set before opening display");
+ break;
+ }
+ g_free (src->xname);
+ src->xname = g_strdup (g_value_get_string (value));
+ break;
+ default:
+ break;
+ }
+}
+
+static void
+gst_xcbimage_src_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec)
+{
+ GstXcbImageSrc *src = GST_XCBIMAGE_SRC (object);
+
+ switch (prop_id) {
+ case PROP_DISPLAY_NAME:
+ if (src->xcontext)
+ g_value_set_string (value, DisplayString (src->xcontext->disp));
+ else
+ g_value_set_string (value, src->display_name);
+
+ break;
+ case PROP_SHOW_POINTER:
+ g_value_set_boolean (value, src->show_pointer);
+ break;
+ case PROP_USE_DAMAGE:
+ g_value_set_boolean (value, src->use_damage);
+ break;
+ case PROP_STARTX:
+ g_value_set_uint (value, src->startx);
+ break;
+ case PROP_STARTY:
+ g_value_set_uint (value, src->starty);
+ break;
+ case PROP_ENDX:
+ g_value_set_uint (value, src->endx);
+ break;
+ case PROP_ENDY:
+ g_value_set_uint (value, src->endy);
+ break;
+ case PROP_REMOTE:
+ g_value_set_boolean (value, src->remote);
+ break;
+ case PROP_XID:
+ g_value_set_uint64 (value, src->xid);
+ break;
+ case PROP_XNAME:
+ g_value_set_string (value, src->xname);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gst_xcbimage_src_clear_bufpool (GstXcbImageSrc * xcbimagesrc)
+{
+ g_mutex_lock (&xcbimagesrc->pool_lock);
+ while (xcbimagesrc->buffer_pool != NULL) {
+ GstBuffer *xcbimage = xcbimagesrc->buffer_pool->data;
+
+ gst_xcbimage_buffer_free (xcbimage);
+
+ xcbimagesrc->buffer_pool = g_slist_delete_link (xcbimagesrc->buffer_pool,
+ xcbimagesrc->buffer_pool);
+ }
+ g_mutex_unlock (&xcbimagesrc->pool_lock);
+}
+
+static void
+gst_xcbimage_src_dispose (GObject * object)
+{
+ /* Drop references in the buffer_pool */
+ gst_xcbimage_src_clear_bufpool (GST_XCBIMAGE_SRC (object));
+
+ G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gst_xcbimage_src_finalize (GObject * object)
+{
+ GstXcbImageSrc *src = GST_XCBIMAGE_SRC (object);
+
+ if (src->xcontext)
+ xcbimageutil_xcontext_clear (src->xcontext);
+
+ g_free (src->xname);
+ g_mutex_clear (&src->pool_lock);
+ g_mutex_clear (&src->x_lock);
+
+ G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static GstCaps *
+gst_xcbimage_src_get_caps (GstBaseSrc * bs, GstCaps * filter)
+{
+ GstXcbImageSrc *s = GST_XCBIMAGE_SRC (bs);
+ GstXContext *xcontext;
+ gint width, height;
+ GstVideoFormat format;
+ guint32 alpha_mask;
+
+ if ((!s->xcontext) && (!gst_xcbimage_src_open_display (s, s->display_name)))
+ return gst_pad_get_pad_template_caps (GST_BASE_SRC (s)->srcpad);
+
+ if (!gst_xcbimage_src_recalc (s))
+ return gst_pad_get_pad_template_caps (GST_BASE_SRC (s)->srcpad);
+
+ xcontext = s->xcontext;
+ width = s->xcontext->width;
+ height = s->xcontext->height;
+ if (s->xwindow != 0) {
+ XWindowAttributes attrs;
+ int status = XGetWindowAttributes (s->xcontext->disp, s->xwindow, &attrs);
+ if (status) {
+ width = attrs.width;
+ height = attrs.height;
+ }
+ }
+
+ /* property comments say 0 means right/bottom, means we can't capture
+ the top left pixel alone */
+ if (s->endx == 0)
+ s->endx = width - 1;
+ if (s->endy == 0)
+ s->endy = height - 1;
+
+ if (s->endx >= s->startx && s->endy >= s->starty) {
+ /* this means user has put in values */
+ if (s->startx < xcontext->width && s->endx < xcontext->width &&
+ s->starty < xcontext->height && s->endy < xcontext->height) {
+ /* values are fine */
+ s->width = width = s->endx - s->startx + 1;
+ s->height = height = s->endy - s->starty + 1;
+ } else {
+ GST_WARNING
+ ("User put in co-ordinates overshooting the X resolution, setting to full screen");
+ s->startx = 0;
+ s->starty = 0;
+ s->endx = width - 1;
+ s->endy = height - 1;
+ }
+ } else {
+ GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
+ s->startx = 0;
+ s->starty = 0;
+ s->endx = width - 1;
+ s->endy = height - 1;
+ }
+ GST_DEBUG ("width = %d, height=%d", width, height);
+
+ /* extrapolate alpha mask */
+ if (xcontext->depth == 32) {
+ alpha_mask = ~(xcontext->r_mask_output
+ | xcontext->g_mask_output | xcontext->b_mask_output);
+ } else {
+ alpha_mask = 0;
+ }
+
+ format =
+ gst_video_format_from_masks (xcontext->depth, xcontext->bpp,
+ xcontext->endianness, xcontext->r_mask_output,
+ xcontext->g_mask_output, xcontext->b_mask_output, alpha_mask);
+
+ return gst_caps_new_simple ("video/x-raw",
+ "format", G_TYPE_STRING, gst_video_format_to_string (format),
+ "width", G_TYPE_INT, width,
+ "height", G_TYPE_INT, height,
+ "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
+ "pixel-aspect-ratio", GST_TYPE_FRACTION, xcontext->par_n,
+ xcontext->par_d, NULL);
+}
+
+static gboolean
+gst_xcbimage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
+{
+ GstXcbImageSrc *s = GST_XCBIMAGE_SRC (bs);
+ GstStructure *structure;
+ const GValue *new_fps;
+
+ /* If not yet opened, disallow setcaps until later */
+ if (!s->xcontext)
+ return FALSE;
+
+ /* The only thing that can change is the framerate downstream wants */
+ structure = gst_caps_get_structure (caps, 0);
+ new_fps = gst_structure_get_value (structure, "framerate");
+ if (!new_fps)
+ return FALSE;
+
+ /* Store this FPS for use when generating buffers */
+ s->fps_n = gst_value_get_fraction_numerator (new_fps);
+ s->fps_d = gst_value_get_fraction_denominator (new_fps);
+
+ GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
+
+ return TRUE;
+}
+
+static GstCaps *
+gst_xcbimage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
+{
+ gint i;
+ GstStructure *structure;
+
+ caps = gst_caps_make_writable (caps);
+
+ for (i = 0; i < gst_caps_get_size (caps); ++i) {
+ structure = gst_caps_get_structure (caps, i);
+
+ gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
+ }
+ caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
+
+ return caps;
+}
+
+static void
+gst_xcbimage_src_class_init (GstXcbImageSrcClass * klass)
+{
+ GObjectClass *gc = G_OBJECT_CLASS (klass);
+ GstElementClass *ec = GST_ELEMENT_CLASS (klass);
+ GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
+ GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
+
+ gc->set_property = gst_xcbimage_src_set_property;
+ gc->get_property = gst_xcbimage_src_get_property;
+ gc->dispose = gst_xcbimage_src_dispose;
+ gc->finalize = gst_xcbimage_src_finalize;
+
+ g_object_class_install_property (gc, PROP_DISPLAY_NAME,
+ g_param_spec_string ("display-name", "Display", "X Display Name",
+ NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (gc, PROP_SHOW_POINTER,
+ g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
+ "Show mouse pointer (if XFixes extension enabled)", TRUE,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ /**
+ * GstXcbImageSrc:use-damage:
+ *
+ * Use XDamage (if the XDamage extension is enabled)
+ */
+ g_object_class_install_property (gc, PROP_USE_DAMAGE,
+ g_param_spec_boolean ("use-damage", "Use XDamage",
+ "Use XDamage (if XDamage extension enabled)", TRUE,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ /**
+ * GstXcbImageSrc:startx:
+ *
+ * X coordinate of top left corner of area to be recorded
+ * (0 for top left of screen)
+ */
+ g_object_class_install_property (gc, PROP_STARTX,
+ g_param_spec_uint ("startx", "Start X co-ordinate",
+ "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
+ 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ /**
+ * GstXcbImageSrc:starty:
+ *
+ * Y coordinate of top left corner of area to be recorded
+ * (0 for top left of screen)
+ */
+ g_object_class_install_property (gc, PROP_STARTY,
+ g_param_spec_uint ("starty", "Start Y co-ordinate",
+ "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
+ 0, G_MAXINT, 0, G_PARAM_READWRITE));
+ /**
+ * GstXcbImageSrc:endx:
+ *
+ * X coordinate of bottom right corner of area to be recorded
+ * (0 for bottom right of screen)
+ */
+ g_object_class_install_property (gc, PROP_ENDX,
+ g_param_spec_uint ("endx", "End X",
+ "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
+ 0, G_MAXINT, 0, G_PARAM_READWRITE));
+ /**
+ * GstXcbImageSrc:endy:
+ *
+ * Y coordinate of bottom right corner of area to be recorded
+ * (0 for bottom right of screen)
+ */
+ g_object_class_install_property (gc, PROP_ENDY,
+ g_param_spec_uint ("endy", "End Y",
+ "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
+ 0, G_MAXINT, 0, G_PARAM_READWRITE));
+
+ /**
+ * GstXcbImageSrc:remote:
+ *
+ * Whether the X display is remote. The element will try to use alternate calls
+ * known to work better with remote displays.
+ */
+ g_object_class_install_property (gc, PROP_REMOTE,
+ g_param_spec_boolean ("remote", "Remote display",
+ "Whether the display is remote", FALSE,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ /**
+ * GstXcbImageSrc:xid:
+ *
+ * The XID of the window to capture. 0 for the root window (default).
+ */
+ g_object_class_install_property (gc, PROP_XID,
+ g_param_spec_uint64 ("xid", "Window XID",
+ "Window XID to capture from", 0, G_MAXUINT64, 0,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ /**
+ * GstXcbImageSrc:xname:
+ *
+ * The name of the window to capture, if any.
+ */
+ g_object_class_install_property (gc, PROP_XNAME,
+ g_param_spec_string ("xname", "Window name",
+ "Window name to capture from", NULL,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ gst_element_class_set_static_metadata (ec, "XcbImage video source",
+ "Source/Video",
+ "Creates a screenshot video stream",
+ "Lutz Mueller <lutz@users.sourceforge.net>, "
+ "Jan Schmidt <thaytan@mad.scientist.com>, "
+ "Zaheer Merali <zaheerabbas at merali dot org>");
+ gst_element_class_add_static_pad_template (ec, &t);
+
+ bc->fixate = gst_xcbimage_src_fixate;
+ bc->get_caps = gst_xcbimage_src_get_caps;
+ bc->set_caps = gst_xcbimage_src_set_caps;
+ bc->start = gst_xcbimage_src_start;
+ bc->stop = gst_xcbimage_src_stop;
+ bc->unlock = gst_xcbimage_src_unlock;
+ push_class->create = gst_xcbimage_src_create;
+}
+
+static void
+gst_xcbimage_src_init (GstXcbImageSrc * xcbimagesrc)
+{
+ gst_base_src_set_format (GST_BASE_SRC (xcbimagesrc), GST_FORMAT_TIME);
+ gst_base_src_set_live (GST_BASE_SRC (xcbimagesrc), TRUE);
+
+ g_mutex_init (&xcbimagesrc->pool_lock);
+ g_mutex_init (&xcbimagesrc->x_lock);
+ xcbimagesrc->show_pointer = TRUE;
+ xcbimagesrc->use_damage = TRUE;
+ xcbimagesrc->startx = 0;
+ xcbimagesrc->starty = 0;
+ xcbimagesrc->endx = 0;
+ xcbimagesrc->endy = 0;
+ xcbimagesrc->remote = FALSE;
+}
+
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+ gboolean ret;
+
+ GST_DEBUG_CATEGORY_INIT (gst_debug_xcbimage_src, "xcbimagesrc", 0,
+ "xcbimagesrc element debug");
+
+ ret = gst_element_register (plugin, "xcbimagesrc", GST_RANK_NONE,
+ GST_TYPE_XCBIMAGE_SRC);
+
+ return ret;
+}
+
+GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
+ GST_VERSION_MINOR,
+ xcbimagesrc,
+ "X11 video input plugin using libxcb",
+ plugin_init, VERSION, "LGPL", "Pidgin Chime plugin", "http://localhost/");
--- /dev/null
+/* GStreamer
+ * Copyright (C) <2005> Luca Ognibene <luogni@tin.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "xcbimageutil.h"
+
+GType
+gst_meta_xcbimage_api_get_type (void)
+{
+ static volatile GType type;
+ static const gchar *tags[] = { "memory", NULL };
+
+ if (g_once_init_enter (&type)) {
+ GType _type = gst_meta_api_type_register ("GstMetaXcbImageSrcAPI", tags);
+ g_once_init_leave (&type, _type);
+ }
+ return type;
+}
+
+static gboolean
+gst_meta_xcbimage_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
+{
+ GstMetaXcbImage *emeta = (GstMetaXcbImage *) meta;
+
+ emeta->parent = NULL;
+ emeta->ximage = NULL;
+#ifdef HAVE_XSHM
+ emeta->SHMInfo.shmaddr = ((void *) -1);
+ emeta->SHMInfo.shmid = -1;
+ emeta->SHMInfo.readOnly = TRUE;
+#endif
+ emeta->width = emeta->height = emeta->size = 0;
+ emeta->return_func = NULL;
+
+ return TRUE;
+}
+
+const GstMetaInfo *
+gst_meta_xcbimage_get_info (void)
+{
+ static const GstMetaInfo *meta_xcbimage_info = NULL;
+
+ if (g_once_init_enter (&meta_xcbimage_info)) {
+ const GstMetaInfo *meta =
+ gst_meta_register (gst_meta_xcbimage_api_get_type (), "GstMetaXcbImageSrc",
+ sizeof (GstMetaXcbImage), (GstMetaInitFunction) gst_meta_xcbimage_init,
+ (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
+ g_once_init_leave (&meta_xcbimage_info, meta);
+ }
+ return meta_xcbimage_info;
+}
+
+#ifdef HAVE_XSHM
+static gboolean error_caught = FALSE;
+
+static int
+xcbimageutil_handle_xerror (Display * display, XErrorEvent * xevent)
+{
+ char error_msg[1024];
+
+ XGetErrorText (display, xevent->error_code, error_msg, 1024);
+ GST_DEBUG ("xcbimageutil failed to use XShm calls. error: %s", error_msg);
+ error_caught = TRUE;
+ return 0;
+}
+
+/* This function checks that it is actually really possible to create an image
+ using XShm */
+gboolean
+xcbimageutil_check_xshm_calls (GstXContext * xcontext)
+{
+ XImage *xcbimage;
+ XShmSegmentInfo SHMInfo;
+ size_t size;
+ int (*handler) (Display *, XErrorEvent *);
+ gboolean result = FALSE;
+ gboolean did_attach = FALSE;
+
+ g_return_val_if_fail (xcontext != NULL, FALSE);
+
+ /* Sync to ensure any older errors are already processed */
+ XSync (xcontext->disp, FALSE);
+
+ /* Set defaults so we don't free these later unnecessarily */
+ SHMInfo.shmaddr = ((void *) -1);
+ SHMInfo.shmid = -1;
+
+ /* Setting an error handler to catch failure */
+ error_caught = FALSE;
+ handler = XSetErrorHandler (xcbimageutil_handle_xerror);
+
+ /* Trying to create a 1x1 xcbimage */
+ GST_DEBUG ("XShmCreateImage of 1x1");
+
+ xcbimage = XShmCreateImage (xcontext->disp, xcontext->visual,
+ xcontext->depth, ZPixmap, NULL, &SHMInfo, 1, 1);
+
+ /* Might cause an error, sync to ensure it is noticed */
+ XSync (xcontext->disp, FALSE);
+ if (!xcbimage || error_caught) {
+ GST_WARNING ("could not XShmCreateImage a 1x1 image");
+ goto beach;
+ }
+ size = xcbimage->height * xcbimage->bytes_per_line;
+
+ SHMInfo.shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
+ if (SHMInfo.shmid == -1) {
+ GST_WARNING ("could not get shared memory of %" G_GSIZE_FORMAT " bytes",
+ size);
+ goto beach;
+ }
+
+ SHMInfo.shmaddr = shmat (SHMInfo.shmid, 0, 0);
+ if (SHMInfo.shmaddr == ((void *) -1)) {
+ GST_WARNING ("Failed to shmat: %s", g_strerror (errno));
+ goto beach;
+ }
+
+ /* Delete the SHM segment. It will actually go away automatically
+ * when we detach now */
+ shmctl (SHMInfo.shmid, IPC_RMID, 0);
+
+ xcbimage->data = SHMInfo.shmaddr;
+ SHMInfo.readOnly = FALSE;
+
+ if (XShmAttach (xcontext->disp, &SHMInfo) == 0) {
+ GST_WARNING ("Failed to XShmAttach");
+ goto beach;
+ }
+
+ /* Sync to ensure we see any errors we caused */
+ XSync (xcontext->disp, FALSE);
+
+ if (!error_caught) {
+ did_attach = TRUE;
+ /* store whether we succeeded in result */
+ result = TRUE;
+ }
+beach:
+ /* Sync to ensure we swallow any errors we caused and reset error_caught */
+ XSync (xcontext->disp, FALSE);
+ error_caught = FALSE;
+ XSetErrorHandler (handler);
+
+ if (did_attach) {
+ XShmDetach (xcontext->disp, &SHMInfo);
+ XSync (xcontext->disp, FALSE);
+ }
+ if (SHMInfo.shmaddr != ((void *) -1))
+ shmdt (SHMInfo.shmaddr);
+ if (xcbimage)
+ XDestroyImage (xcbimage);
+ return result;
+}
+#endif /* HAVE_XSHM */
+
+/* This function gets the X Display and global info about it. Everything is
+ stored in our object and will be cleaned when the object is disposed. Note
+ here that caps for supported format are generated without any window or
+ image creation */
+GstXContext *
+xcbimageutil_xcontext_get (GstElement * parent, const gchar * display_name)
+{
+ GstXContext *xcontext = NULL;
+ XPixmapFormatValues *px_formats = NULL;
+ gint nb_formats = 0, i;
+
+ xcontext = g_new0 (GstXContext, 1);
+
+ xcontext->disp = XOpenDisplay (display_name);
+ GST_DEBUG_OBJECT (parent, "opened display %p", xcontext->disp);
+ if (!xcontext->disp) {
+ g_free (xcontext);
+ return NULL;
+ }
+ xcontext->screen = DefaultScreenOfDisplay (xcontext->disp);
+ xcontext->visual = DefaultVisualOfScreen (xcontext->screen);
+ xcontext->root = RootWindowOfScreen (xcontext->screen);
+ xcontext->white = WhitePixelOfScreen (xcontext->screen);
+ xcontext->black = BlackPixelOfScreen (xcontext->screen);
+ xcontext->depth = DefaultDepthOfScreen (xcontext->screen);
+
+ xcontext->width = WidthOfScreen (xcontext->screen);
+ xcontext->height = HeightOfScreen (xcontext->screen);
+
+ xcontext->widthmm = WidthMMOfScreen (xcontext->screen);
+ xcontext->heightmm = HeightMMOfScreen (xcontext->screen);
+
+ xcontext->caps = NULL;
+
+ GST_DEBUG_OBJECT (parent, "X reports %dx%d pixels and %d mm x %d mm",
+ xcontext->width, xcontext->height, xcontext->widthmm, xcontext->heightmm);
+ xcbimageutil_calculate_pixel_aspect_ratio (xcontext);
+
+ /* We get supported pixmap formats at supported depth */
+ px_formats = XListPixmapFormats (xcontext->disp, &nb_formats);
+
+ if (!px_formats) {
+ XCloseDisplay (xcontext->disp);
+ g_free (xcontext);
+ return NULL;
+ }
+
+ /* We get bpp value corresponding to our running depth */
+ for (i = 0; i < nb_formats; i++) {
+ if (px_formats[i].depth == xcontext->depth)
+ xcontext->bpp = px_formats[i].bits_per_pixel;
+ }
+
+ XFree (px_formats);
+
+ xcontext->endianness =
+ (ImageByteOrder (xcontext->disp) ==
+ LSBFirst) ? G_LITTLE_ENDIAN : G_BIG_ENDIAN;
+
+#ifdef HAVE_XSHM
+ /* Search for XShm extension support */
+ if (XShmQueryExtension (xcontext->disp) &&
+ xcbimageutil_check_xshm_calls (xcontext)) {
+ xcontext->use_xshm = TRUE;
+ GST_DEBUG ("xcbimageutil is using XShm extension");
+ } else {
+ xcontext->use_xshm = FALSE;
+ GST_DEBUG ("xcbimageutil is not using XShm extension");
+ }
+#endif /* HAVE_XSHM */
+
+ /* our caps system handles 24/32bpp RGB as big-endian. */
+ if ((xcontext->bpp == 24 || xcontext->bpp == 32) &&
+ xcontext->endianness == G_LITTLE_ENDIAN) {
+ xcontext->endianness = G_BIG_ENDIAN;
+ xcontext->r_mask_output = GUINT32_TO_BE (xcontext->visual->red_mask);
+ xcontext->g_mask_output = GUINT32_TO_BE (xcontext->visual->green_mask);
+ xcontext->b_mask_output = GUINT32_TO_BE (xcontext->visual->blue_mask);
+ if (xcontext->bpp == 24) {
+ xcontext->r_mask_output >>= 8;
+ xcontext->g_mask_output >>= 8;
+ xcontext->b_mask_output >>= 8;
+ }
+ } else {
+ xcontext->r_mask_output = xcontext->visual->red_mask;
+ xcontext->g_mask_output = xcontext->visual->green_mask;
+ xcontext->b_mask_output = xcontext->visual->blue_mask;
+ }
+
+ return xcontext;
+}
+
+/* This function cleans the X context. Closing the Display and unrefing the
+ caps for supported formats. */
+void
+xcbimageutil_xcontext_clear (GstXContext * xcontext)
+{
+ g_return_if_fail (xcontext != NULL);
+
+ if (xcontext->caps != NULL)
+ gst_caps_unref (xcontext->caps);
+
+ XCloseDisplay (xcontext->disp);
+
+ g_free (xcontext);
+}
+
+/* This function calculates the pixel aspect ratio based on the properties
+ * in the xcontext structure and stores it there. */
+void
+xcbimageutil_calculate_pixel_aspect_ratio (GstXContext * xcontext)
+{
+ gint par[][2] = {
+ {1, 1}, /* regular screen */
+ {16, 15}, /* PAL TV */
+ {11, 10}, /* 525 line Rec.601 video */
+ {54, 59} /* 625 line Rec.601 video */
+ };
+ gint i;
+ gint index;
+ gdouble ratio;
+ gdouble delta;
+
+#define DELTA(idx) (ABS (ratio - ((gdouble) par[idx][0] / par[idx][1])))
+
+ /* first calculate the "real" ratio based on the X values;
+ * which is the "physical" w/h divided by the w/h in pixels of the display */
+ ratio = (gdouble) (xcontext->widthmm * xcontext->height)
+ / (xcontext->heightmm * xcontext->width);
+
+ /* DirectFB's X in 720x576 reports the physical dimensions wrong, so
+ * override here */
+ if (xcontext->width == 720 && xcontext->height == 576) {
+ ratio = 4.0 * 576 / (3.0 * 720);
+ }
+ GST_DEBUG ("calculated pixel aspect ratio: %f", ratio);
+
+ /* now find the one from par[][2] with the lowest delta to the real one */
+ delta = DELTA (0);
+ index = 0;
+
+ for (i = 1; i < sizeof (par) / (sizeof (gint) * 2); ++i) {
+ gdouble this_delta = DELTA (i);
+
+ if (this_delta < delta) {
+ index = i;
+ delta = this_delta;
+ }
+ }
+
+ GST_DEBUG ("Decided on index %d (%d/%d)", index,
+ par[index][0], par[index][1]);
+
+ xcontext->par_n = par[index][0];
+ xcontext->par_d = par[index][1];
+ GST_DEBUG ("set xcontext PAR to %d/%d\n", xcontext->par_n, xcontext->par_d);
+}
+
+static gboolean
+gst_xcbimagesrc_buffer_dispose (GstBuffer * xcbimage)
+{
+ GstElement *parent;
+ GstMetaXcbImage *meta;
+ gboolean ret = TRUE;
+
+ meta = GST_META_XCBIMAGE_GET (xcbimage);
+
+ parent = meta->parent;
+ if (parent == NULL) {
+ g_warning ("XcbImageSrcBuffer->xcbimagesrc == NULL");
+ goto beach;
+ }
+
+ if (meta->return_func)
+ ret = meta->return_func (parent, xcbimage);
+
+beach:
+ return ret;
+}
+
+void
+gst_xcbimage_buffer_free (GstBuffer * xcbimage)
+{
+ GstMetaXcbImage *meta;
+
+ meta = GST_META_XCBIMAGE_GET (xcbimage);
+
+ /* make sure it is not recycled */
+ meta->width = -1;
+ meta->height = -1;
+ gst_buffer_unref (xcbimage);
+}
+
+/* This function handles GstXcbImageSrcBuffer creation depending on XShm availability */
+GstBuffer *
+gst_xcbimageutil_xcbimage_new (GstXContext * xcontext,
+ GstElement * parent, int width, int height, BufferReturnFunc return_func)
+{
+ GstBuffer *xcbimage = NULL;
+ GstMetaXcbImage *meta;
+ gboolean succeeded = FALSE;
+
+ xcbimage = gst_buffer_new ();
+ GST_MINI_OBJECT_CAST (xcbimage)->dispose =
+ (GstMiniObjectDisposeFunction) gst_xcbimagesrc_buffer_dispose;
+
+ meta = GST_META_XCBIMAGE_ADD (xcbimage);
+ meta->width = width;
+ meta->height = height;
+
+#ifdef HAVE_XSHM
+ meta->SHMInfo.shmaddr = ((void *) -1);
+ meta->SHMInfo.shmid = -1;
+
+ if (xcontext->use_xshm) {
+ meta->ximage = XShmCreateImage (xcontext->disp,
+ xcontext->visual, xcontext->depth,
+ ZPixmap, NULL, &meta->SHMInfo, meta->width, meta->height);
+ if (!meta->ximage) {
+ GST_WARNING_OBJECT (parent,
+ "could not XShmCreateImage a %dx%d image", meta->width, meta->height);
+
+ /* Retry without XShm */
+ xcontext->use_xshm = FALSE;
+ goto no_xshm;
+ }
+
+ /* we have to use the returned bytes_per_line for our shm size */
+ meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
+ meta->SHMInfo.shmid = shmget (IPC_PRIVATE, meta->size, IPC_CREAT | 0777);
+ if (meta->SHMInfo.shmid == -1)
+ goto beach;
+
+ meta->SHMInfo.shmaddr = shmat (meta->SHMInfo.shmid, 0, 0);
+ if (meta->SHMInfo.shmaddr == ((void *) -1))
+ goto beach;
+
+ /* Delete the SHM segment. It will actually go away automatically
+ * when we detach now */
+ shmctl (meta->SHMInfo.shmid, IPC_RMID, 0);
+
+ meta->ximage->data = meta->SHMInfo.shmaddr;
+ meta->SHMInfo.readOnly = FALSE;
+
+ if (XShmAttach (xcontext->disp, &meta->SHMInfo) == 0)
+ goto beach;
+
+ XSync (xcontext->disp, FALSE);
+ } else
+ no_xshm:
+#endif /* HAVE_XSHM */
+ {
+ meta->ximage = XCreateImage (xcontext->disp,
+ xcontext->visual,
+ xcontext->depth,
+ ZPixmap, 0, NULL, meta->width, meta->height, xcontext->bpp, 0);
+ if (!meta->ximage)
+ goto beach;
+
+ /* we have to use the returned bytes_per_line for our image size */
+ meta->size = meta->ximage->bytes_per_line * meta->ximage->height;
+ meta->ximage->data = g_malloc (meta->size);
+
+ XSync (xcontext->disp, FALSE);
+ }
+ succeeded = TRUE;
+
+ gst_buffer_append_memory (xcbimage,
+ gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, meta->ximage->data,
+ meta->size, 0, meta->size, NULL, NULL));
+
+ /* Keep a ref to our src */
+ meta->parent = gst_object_ref (parent);
+ meta->return_func = return_func;
+beach:
+ if (!succeeded) {
+ gst_xcbimage_buffer_free (xcbimage);
+ xcbimage = NULL;
+ }
+
+ return xcbimage;
+}
+
+/* This function destroys a GstXcbImageBuffer handling XShm availability */
+void
+gst_xcbimageutil_xcbimage_destroy (GstXContext * xcontext, GstBuffer * xcbimage)
+{
+ GstMetaXcbImage *meta;
+
+ meta = GST_META_XCBIMAGE_GET (xcbimage);
+
+ /* We might have some buffers destroyed after changing state to NULL */
+ if (!xcontext)
+ goto beach;
+
+ g_return_if_fail (xcbimage != NULL);
+
+#ifdef HAVE_XSHM
+ if (xcontext->use_xshm) {
+ if (meta->SHMInfo.shmaddr != ((void *) -1)) {
+ XShmDetach (xcontext->disp, &meta->SHMInfo);
+ XSync (xcontext->disp, 0);
+ shmdt (meta->SHMInfo.shmaddr);
+ }
+ if (meta->ximage)
+ XDestroyImage (meta->ximage);
+
+ } else
+#endif /* HAVE_XSHM */
+ {
+ if (meta->ximage) {
+ XDestroyImage (meta->ximage);
+ }
+ }
+
+ XSync (xcontext->disp, FALSE);
+beach:
+ if (meta->parent) {
+ /* Release the ref to our parent */
+ gst_object_unref (meta->parent);
+ meta->parent = NULL;
+ }
+
+ return;
+}