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 #include "gst/glib-compat-private.h"
54 GST_DEBUG_CATEGORY_STATIC (gst_debug_ximage_src);
55 #define GST_CAT_DEFAULT gst_debug_ximage_src
57 static GstStaticPadTemplate t =
58 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
59 GST_STATIC_CAPS ("video/x-raw-rgb, "
60 "framerate = (fraction) [ 0, MAX ], "
61 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
62 "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
80 #define gst_ximage_src_parent_class parent_class
81 G_DEFINE_TYPE (GstXImageSrc, gst_ximage_src, GST_TYPE_PUSH_SRC);
83 static void gst_ximage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
84 static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
86 /* Called when a buffer is returned from the pipeline */
88 gst_ximage_src_return_buf (GstXImageSrc * ximagesrc, GstBuffer * ximage)
90 GstMetaXImage *meta = GST_META_XIMAGE_GET (ximage);
92 /* If our geometry changed we can't reuse that image. */
93 if ((meta->width != ximagesrc->width) || (meta->height != ximagesrc->height)) {
94 GST_DEBUG_OBJECT (ximagesrc,
95 "destroy image %p as its size changed %dx%d vs current %dx%d",
96 ximage, meta->width, meta->height, ximagesrc->width, ximagesrc->height);
97 g_mutex_lock (&ximagesrc->x_lock);
98 gst_ximageutil_ximage_destroy (ximagesrc->xcontext, ximage);
99 g_mutex_unlock (&ximagesrc->x_lock);
101 /* In that case we can reuse the image and add it to our image pool. */
102 GST_LOG_OBJECT (ximagesrc, "recycling image %p in pool", ximage);
103 /* need to increment the refcount again to recycle */
104 gst_buffer_ref (ximage);
105 g_mutex_lock (&ximagesrc->pool_lock);
106 GST_BUFFER_FLAGS (GST_BUFFER (ximage)) = 0; /* clear out any flags from the previous use */
107 ximagesrc->buffer_pool = g_slist_prepend (ximagesrc->buffer_pool, ximage);
108 g_mutex_unlock (&ximagesrc->pool_lock);
113 gst_ximage_src_find_window (GstXImageSrc * src, Window root, const char *name)
116 Window window = 0, root_return, parent_return;
117 unsigned int nchildren;
121 status = XFetchName (src->xcontext->disp, root, &tmpname);
122 if (status && !strcmp (name, tmpname))
126 XQueryTree (src->xcontext->disp, root, &root_return, &parent_return,
127 &children, &nchildren);
128 if (!status || !children)
131 for (n = 0; n < nchildren; ++n) {
132 window = gst_ximage_src_find_window (src, children[n], name);
142 gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
144 g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
146 if (s->xcontext != NULL)
149 g_mutex_lock (&s->x_lock);
150 s->xcontext = ximageutil_xcontext_get (GST_ELEMENT (s), name);
151 if (s->xcontext == NULL) {
152 g_mutex_unlock (&s->x_lock);
153 GST_ELEMENT_ERROR (s, RESOURCE, OPEN_READ,
154 ("Could not open X display for reading"),
155 ("NULL returned from getting xcontext"));
158 s->width = s->xcontext->width;
159 s->height = s->xcontext->height;
161 s->xwindow = s->xcontext->root;
162 if (s->xid != 0 || s->xname) {
164 XWindowAttributes attrs;
168 status = XGetWindowAttributes (s->xcontext->disp, s->xid, &attrs);
170 GST_DEBUG_OBJECT (s, "Found window XID %" G_GUINT64_FORMAT, s->xid);
174 GST_WARNING_OBJECT (s, "Failed to get window %" G_GUINT64_FORMAT
175 " attributes", s->xid);
180 GST_DEBUG_OBJECT (s, "Looking for window %s", s->xname);
181 window = gst_ximage_src_find_window (s, s->xcontext->root, s->xname);
183 GST_DEBUG_OBJECT (s, "Found window named %s, ", s->xname);
184 status = XGetWindowAttributes (s->xcontext->disp, window, &attrs);
189 GST_WARNING_OBJECT (s, "Failed to get window attributes for "
190 "window named %s", s->xname);
195 GST_INFO_OBJECT (s, "Using root window");
196 goto use_root_window;
199 g_assert (s->xwindow != 0);
200 s->width = attrs.width;
201 s->height = attrs.height;
202 GST_INFO_OBJECT (s, "Using default window size of %dx%d",
203 s->width, s->height);
208 /* check if xfixes supported */
212 if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
214 s->have_xfixes = TRUE;
215 GST_DEBUG_OBJECT (s, "X Server supports XFixes");
218 GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
223 /* check if xdamage is supported */
226 long evmask = NoEventMask;
228 s->have_xdamage = FALSE;
230 s->damage_copy_gc = None;
231 s->damage_region = None;
233 if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
236 XDamageCreate (s->xcontext->disp, s->xwindow, XDamageReportNonEmpty);
237 if (s->damage != None) {
238 s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
239 if (s->damage_region != None) {
242 GST_DEBUG_OBJECT (s, "Using XDamage extension");
243 values.subwindow_mode = IncludeInferiors;
244 s->damage_copy_gc = XCreateGC (s->xcontext->disp,
245 s->xwindow, GCSubwindowMode, &values);
246 XSelectInput (s->xcontext->disp, s->xwindow, evmask);
248 s->have_xdamage = TRUE;
250 XDamageDestroy (s->xcontext->disp, s->damage);
254 GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
256 GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
262 g_mutex_unlock (&s->x_lock);
264 if (s->xcontext == NULL)
271 gst_ximage_src_start (GstBaseSrc * basesrc)
273 GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
275 s->last_frame_no = -1;
278 gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
279 s->last_ximage = NULL;
281 return gst_ximage_src_open_display (s, s->display_name);
285 gst_ximage_src_stop (GstBaseSrc * basesrc)
287 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
290 if (src->last_ximage)
291 gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
292 src->last_ximage = NULL;
295 gst_ximage_src_clear_bufpool (src);
298 if (src->cursor_image)
299 XFree (src->cursor_image);
300 src->cursor_image = NULL;
304 g_mutex_lock (&src->x_lock);
307 if (src->damage_copy_gc != None) {
308 XFreeGC (src->xcontext->disp, src->damage_copy_gc);
309 src->damage_copy_gc = None;
311 if (src->damage_region != None) {
312 XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
313 src->damage_region = None;
315 if (src->damage != None) {
316 XDamageDestroy (src->xcontext->disp, src->damage);
321 ximageutil_xcontext_clear (src->xcontext);
322 src->xcontext = NULL;
323 g_mutex_unlock (&src->x_lock);
330 gst_ximage_src_unlock (GstBaseSrc * basesrc)
332 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
334 /* Awaken the create() func if it's waiting on the clock */
335 GST_OBJECT_LOCK (src);
337 GST_DEBUG_OBJECT (src, "Waking up waiting clock");
338 gst_clock_id_unschedule (src->clock_id);
340 GST_OBJECT_UNLOCK (src);
346 gst_ximage_src_recalc (GstXImageSrc * src)
351 /* Maybe later we can check the display hasn't changed size */
352 /* We could use XQueryPointer to get only the current window. */
358 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
366 gint r_shift, r_max, r_shift_out;
367 gint g_shift, g_max, g_shift_out;
368 gint b_shift, b_max, b_shift_out;
370 switch (xcontext->bpp) {
375 color = GUINT16_FROM_LE (*(guint16 *) (dest));
378 color = GUINT32_FROM_LE (*(guint32 *) (dest));
381 /* Should not reach here */
382 g_return_if_reached ();
385 /* possible optimisation:
386 * move the code that finds shift and max in the _link function */
387 for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
388 for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
390 for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
392 for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
394 for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
396 for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
400 r_max = (xcontext->visual->red_mask >> r_shift);
401 b_max = (xcontext->visual->blue_mask >> b_shift);
402 g_max = (xcontext->visual->green_mask >> g_shift);
404 #define RGBXXX_R(x) (((x)>>r_shift) & (r_max))
405 #define RGBXXX_G(x) (((x)>>g_shift) & (g_max))
406 #define RGBXXX_B(x) (((x)>>b_shift) & (b_max))
408 dr = (RGBXXX_R (color) * 255) / r_max;
409 dg = (RGBXXX_G (color) * 255) / g_max;
410 db = (RGBXXX_B (color) * 255) / b_max;
412 dr = (r * a + (0xff - a) * dr) / 0xff;
413 dg = (g * a + (0xff - a) * dg) / 0xff;
414 db = (b * a + (0xff - a) * db) / 0xff;
416 color = (((dr * r_max) / 255) << r_shift_out) +
417 (((dg * g_max) / 255) << g_shift_out) +
418 (((db * b_max) / 255) << b_shift_out);
420 switch (xcontext->bpp) {
425 *(guint16 *) (dest) = color;
428 *(guint32 *) (dest) = color;
431 g_warning ("bpp %d not supported\n", xcontext->bpp);
438 copy_buffer (GstBuffer * dest, GstBuffer * src)
442 gst_buffer_map (src, &map, GST_MAP_READ);
443 gst_buffer_fill (dest, 0, map.data, map.size);
444 gst_buffer_unmap (src, &map);
448 /* Retrieve an XImageSrcBuffer, preferably from our
449 * pool of existing images and populate it from the window */
451 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
453 GstBuffer *ximage = NULL;
456 g_mutex_lock (&ximagesrc->pool_lock);
457 while (ximagesrc->buffer_pool != NULL) {
458 ximage = ximagesrc->buffer_pool->data;
460 meta = GST_META_XIMAGE_GET (ximage);
462 if ((meta->width != ximagesrc->width) ||
463 (meta->height != ximagesrc->height)) {
464 gst_ximage_buffer_free (ximage);
467 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
468 ximagesrc->buffer_pool);
470 g_mutex_unlock (&ximagesrc->pool_lock);
472 if (ximage == NULL) {
473 GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
474 ximagesrc->width, ximagesrc->height);
476 g_mutex_lock (&ximagesrc->x_lock);
477 ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
478 GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
479 (BufferReturnFunc) (gst_ximage_src_return_buf));
480 if (ximage == NULL) {
481 GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
482 ("could not create a %dx%d ximage", ximagesrc->width,
484 g_mutex_unlock (&ximagesrc->x_lock);
488 g_mutex_unlock (&ximagesrc->x_lock);
491 g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
493 meta = GST_META_XIMAGE_GET (ximage);
496 if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
497 ximagesrc->last_ximage != NULL) {
500 /* have_frame is TRUE when either the entire screen has been
501 * grabbed or when the last image has been copied */
502 gboolean have_frame = FALSE;
504 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
507 XNextEvent (ximagesrc->xcontext->disp, &ev);
509 if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
514 parts = XFixesCreateRegion (ximagesrc->xcontext->disp, 0, 0);
515 XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
517 /* Now copy out all of the damaged rectangles. */
518 rects = XFixesFetchRegion (ximagesrc->xcontext->disp, parts, &nrects);
523 GST_LOG_OBJECT (ximagesrc,
524 "Copying from last frame ximage->size: %" G_GSIZE_FORMAT,
525 gst_buffer_get_size (ximage));
526 copy_buffer (ximage, ximagesrc->last_ximage);
529 for (i = 0; i < nrects; i++) {
530 GST_LOG_OBJECT (ximagesrc,
531 "Damaged sub-region @ %d,%d size %dx%d reported",
532 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
534 /* if we only want a small area, clip this damage region to
536 if (ximagesrc->endx > ximagesrc->startx &&
537 ximagesrc->endy > ximagesrc->starty) {
538 /* see if damage area intersects */
539 if (rects[i].x + rects[i].width - 1 < ximagesrc->startx ||
540 rects[i].x > ximagesrc->endx) {
542 } else if (rects[i].y + rects[i].height - 1 < ximagesrc->starty ||
543 rects[i].y > ximagesrc->endy) {
546 /* find intersect region */
547 int startx, starty, width, height;
549 startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
551 starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
553 width = (rects[i].x + rects[i].width - 1 < ximagesrc->endx) ?
554 rects[i].x + rects[i].width - startx :
555 ximagesrc->endx - startx + 1;
556 height = (rects[i].y + rects[i].height - 1 < ximagesrc->endy) ?
557 rects[i].y + rects[i].height - starty : ximagesrc->endy -
560 GST_LOG_OBJECT (ximagesrc,
561 "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
562 startx, starty, width, height);
563 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
564 startx, starty, width, height, AllPlanes, ZPixmap,
565 meta->ximage, startx - ximagesrc->startx,
566 starty - ximagesrc->starty);
570 GST_LOG_OBJECT (ximagesrc,
571 "Retrieving damaged sub-region @ %d,%d size %dx%d",
572 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
574 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
575 rects[i].x, rects[i].y,
576 rects[i].width, rects[i].height,
577 AllPlanes, ZPixmap, meta->ximage, rects[i].x, rects[i].y);
583 } while (XPending (ximagesrc->xcontext->disp));
585 GST_LOG_OBJECT (ximagesrc,
586 "Copying from last frame ximage->size: %" G_GSIZE_FORMAT,
587 gst_buffer_get_size (ximage));
588 copy_buffer (ximage, ximagesrc->last_ximage);
591 /* re-get area where last mouse pointer was but only if in our clipping
593 if (ximagesrc->cursor_image) {
594 gint x, y, width, height;
596 x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
597 y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
598 width = ximagesrc->cursor_image->width;
599 height = ximagesrc->cursor_image->height;
601 /* bounds checking */
606 if (x + width > ximagesrc->xcontext->width)
607 width = ximagesrc->xcontext->width - x;
608 if (y + height > ximagesrc->xcontext->height)
609 height = ximagesrc->xcontext->height - y;
612 GST_DEBUG_OBJECT (ximagesrc,
613 "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
614 x, y, width, height, ximagesrc->startx, ximagesrc->starty,
615 ximagesrc->endx, ximagesrc->endy);
616 /* only get where cursor last was, if it is in our range */
617 if (ximagesrc->endx > ximagesrc->startx &&
618 ximagesrc->endy > ximagesrc->starty) {
620 if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
622 } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
625 /* find intersect region */
626 int startx, starty, iwidth, iheight;
628 startx = (x < ximagesrc->startx) ? ximagesrc->startx : x;
629 starty = (y < ximagesrc->starty) ? ximagesrc->starty : y;
630 iwidth = (x + width < ximagesrc->endx) ?
631 x + width - startx : ximagesrc->endx - startx;
632 iheight = (y + height < ximagesrc->endy) ?
633 y + height - starty : ximagesrc->endy - starty;
634 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
635 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
636 startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
637 meta->ximage, startx - ximagesrc->startx,
638 starty - ximagesrc->starty);
642 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
643 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
644 x, y, width, height, AllPlanes, ZPixmap, meta->ximage, x, y);
654 if (ximagesrc->xcontext->use_xshm) {
655 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
656 XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
657 meta->ximage, ximagesrc->startx, ximagesrc->starty, AllPlanes);
660 #endif /* HAVE_XSHM */
662 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
663 if (ximagesrc->remote) {
664 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
665 ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
666 ximagesrc->height, AllPlanes, ZPixmap, meta->ximage, 0, 0);
669 XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
670 ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
671 ximagesrc->height, AllPlanes, ZPixmap);
679 if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {
681 GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
683 if (ximagesrc->cursor_image)
684 XFree (ximagesrc->cursor_image);
685 ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
686 if (ximagesrc->cursor_image != NULL) {
687 int cx, cy, i, j, count;
688 int startx, starty, iwidth, iheight;
689 gboolean cursor_in_image = TRUE;
691 cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
694 cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
697 count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
699 /* only get where cursor last was, if it is in our range */
700 if (ximagesrc->endx > ximagesrc->startx &&
701 ximagesrc->endy > ximagesrc->starty) {
703 if (cx + ximagesrc->cursor_image->width < ximagesrc->startx ||
704 cx > ximagesrc->endx) {
706 cursor_in_image = FALSE;
707 } else if (cy + ximagesrc->cursor_image->height < ximagesrc->starty ||
708 cy > ximagesrc->endy) {
710 cursor_in_image = FALSE;
712 /* find intersect region */
714 startx = (cx < ximagesrc->startx) ? ximagesrc->startx : cx;
715 starty = (cy < ximagesrc->starty) ? ximagesrc->starty : cy;
716 iwidth = (cx + ximagesrc->cursor_image->width < ximagesrc->endx) ?
717 cx + ximagesrc->cursor_image->width - startx :
718 ximagesrc->endx - startx;
719 iheight = (cy + ximagesrc->cursor_image->height < ximagesrc->endy) ?
720 cy + ximagesrc->cursor_image->height - starty :
721 ximagesrc->endy - starty;
726 iwidth = ximagesrc->cursor_image->width;
727 iheight = ximagesrc->cursor_image->height;
730 if (cursor_in_image) {
731 GST_DEBUG_OBJECT (ximagesrc, "Cursor is in image so trying to draw it");
732 for (i = 0; i < count; i++)
733 ximagesrc->cursor_image->pixels[i] =
734 GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
735 /* copy those pixels across */
737 j < starty + iheight && j < ximagesrc->starty + ximagesrc->height;
740 i < startx + iwidth && i < ximagesrc->startx + ximagesrc->width;
745 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
746 cy) * ximagesrc->cursor_image->width + (i - cx))]);
748 (guint8 *) & (meta->ximage->data[((j -
749 ximagesrc->starty) * ximagesrc->width + (i -
750 ximagesrc->startx)) * (ximagesrc->xcontext->bpp /
753 composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
762 if (ximagesrc->have_xdamage && ximagesrc->use_damage) {
763 /* need to ref ximage to put in last_ximage */
764 gst_buffer_ref (ximage);
765 if (ximagesrc->last_ximage) {
766 gst_buffer_unref (ximagesrc->last_ximage);
768 ximagesrc->last_ximage = ximage;
769 GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
776 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
778 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
780 GstClockTime base_time;
781 GstClockTime next_capture_ts;
783 gint64 next_frame_no;
785 if (!gst_ximage_src_recalc (s)) {
786 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
787 (_("Changing resolution at runtime is not yet supported.")), (NULL));
788 return GST_FLOW_ERROR;
791 if (s->fps_n <= 0 || s->fps_d <= 0)
792 return GST_FLOW_NOT_NEGOTIATED; /* FPS must be > 0 */
794 /* Now, we might need to wait for the next multiple of the fps
795 * before capturing */
798 if (GST_ELEMENT_CLOCK (s) == NULL) {
799 GST_OBJECT_UNLOCK (s);
800 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
801 (_("Cannot operate without a clock")), (NULL));
802 return GST_FLOW_ERROR;
805 base_time = GST_ELEMENT_CAST (s)->base_time;
806 next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
807 next_capture_ts -= base_time;
809 /* Figure out which 'frame number' position we're at, based on the cur time
811 next_frame_no = gst_util_uint64_scale (next_capture_ts,
812 s->fps_n, GST_SECOND * s->fps_d);
813 if (next_frame_no == s->last_frame_no) {
817 /* Need to wait for the next frame */
820 /* Figure out what the next frame time is */
821 next_capture_ts = gst_util_uint64_scale (next_frame_no,
822 s->fps_d * GST_SECOND, s->fps_n);
824 id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
825 next_capture_ts + base_time);
828 /* release the object lock while waiting */
829 GST_OBJECT_UNLOCK (s);
831 GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
833 ret = gst_clock_id_wait (id, NULL);
836 gst_clock_id_unref (id);
838 if (ret == GST_CLOCK_UNSCHEDULED) {
839 /* Got woken up by the unlock function */
840 GST_OBJECT_UNLOCK (s);
841 return GST_FLOW_FLUSHING;
843 /* Duration is a complete 1/fps frame duration */
844 dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
846 GstClockTime next_frame_ts;
848 GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
849 G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
850 G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
851 next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
852 s->fps_d * GST_SECOND, s->fps_n);
853 /* Frame duration is from now until the next expected capture time */
854 dur = next_frame_ts - next_capture_ts;
856 s->last_frame_no = next_frame_no;
857 GST_OBJECT_UNLOCK (s);
859 image = gst_ximage_src_ximage_get (s);
861 return GST_FLOW_ERROR;
864 GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
865 GST_BUFFER_DURATION (*buf) = dur;
871 gst_ximage_src_set_property (GObject * object, guint prop_id,
872 const GValue * value, GParamSpec * pspec)
874 GstXImageSrc *src = GST_XIMAGE_SRC (object);
877 case PROP_DISPLAY_NAME:
879 g_free (src->display_name);
880 src->display_name = g_strdup (g_value_get_string (value));
882 case PROP_SCREEN_NUM:
883 src->screen_num = g_value_get_uint (value);
885 case PROP_SHOW_POINTER:
886 src->show_pointer = g_value_get_boolean (value);
888 case PROP_USE_DAMAGE:
889 src->use_damage = g_value_get_boolean (value);
892 src->startx = g_value_get_uint (value);
895 src->starty = g_value_get_uint (value);
898 src->endx = g_value_get_uint (value);
901 src->endy = g_value_get_uint (value);
904 src->remote = g_value_get_boolean (value);
907 if (src->xcontext != NULL) {
908 g_warning ("ximagesrc window ID must be set before opening display");
911 src->xid = g_value_get_uint64 (value);
914 if (src->xcontext != NULL) {
915 g_warning ("ximagesrc window name must be set before opening display");
919 src->xname = g_strdup (g_value_get_string (value));
927 gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
930 GstXImageSrc *src = GST_XIMAGE_SRC (object);
933 case PROP_DISPLAY_NAME:
935 g_value_set_string (value, DisplayString (src->xcontext->disp));
937 g_value_set_string (value, src->display_name);
940 case PROP_SCREEN_NUM:
941 g_value_set_uint (value, src->screen_num);
943 case PROP_SHOW_POINTER:
944 g_value_set_boolean (value, src->show_pointer);
946 case PROP_USE_DAMAGE:
947 g_value_set_boolean (value, src->use_damage);
950 g_value_set_uint (value, src->startx);
953 g_value_set_uint (value, src->starty);
956 g_value_set_uint (value, src->endx);
959 g_value_set_uint (value, src->endy);
962 g_value_set_boolean (value, src->remote);
965 g_value_set_uint64 (value, src->xid);
968 g_value_set_string (value, src->xname);
971 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
977 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
979 g_mutex_lock (&ximagesrc->pool_lock);
980 while (ximagesrc->buffer_pool != NULL) {
981 GstBuffer *ximage = ximagesrc->buffer_pool->data;
983 gst_ximage_buffer_free (ximage);
985 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
986 ximagesrc->buffer_pool);
988 g_mutex_unlock (&ximagesrc->pool_lock);
992 gst_ximage_src_dispose (GObject * object)
994 /* Drop references in the buffer_pool */
995 gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
997 G_OBJECT_CLASS (parent_class)->dispose (object);
1001 gst_ximage_src_finalize (GObject * object)
1003 GstXImageSrc *src = GST_XIMAGE_SRC (object);
1006 ximageutil_xcontext_clear (src->xcontext);
1008 g_free (src->xname);
1009 g_mutex_clear (&src->pool_lock);
1010 g_mutex_clear (&src->x_lock);
1012 G_OBJECT_CLASS (parent_class)->finalize (object);
1016 gst_ximage_src_get_caps (GstBaseSrc * bs, GstCaps * filter)
1018 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1019 GstXContext *xcontext;
1022 if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
1024 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1027 if (!gst_ximage_src_recalc (s))
1029 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1032 xcontext = s->xcontext;
1033 width = s->xcontext->width;
1034 height = s->xcontext->height;
1035 if (s->xwindow != 0) {
1036 XWindowAttributes attrs;
1037 int status = XGetWindowAttributes (s->xcontext->disp, s->xwindow, &attrs);
1039 width = attrs.width;
1040 height = attrs.height;
1044 /* property comments say 0 means right/bottom, means we can't capture
1045 the top left pixel alone */
1047 s->endx = width - 1;
1049 s->endy = height - 1;
1051 if (s->endx >= s->startx && s->endy >= s->starty) {
1052 /* this means user has put in values */
1053 if (s->startx < xcontext->width && s->endx < xcontext->width &&
1054 s->starty < xcontext->height && s->endy < xcontext->height &&
1055 s->startx >= 0 && s->starty >= 0) {
1056 /* values are fine */
1057 s->width = width = s->endx - s->startx + 1;
1058 s->height = height = s->endy - s->starty + 1;
1061 ("User put in co-ordinates overshooting the X resolution, setting to full screen");
1064 s->endx = width - 1;
1065 s->endy = height - 1;
1068 GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
1071 s->endx = width - 1;
1072 s->endy = height - 1;
1074 GST_DEBUG ("width = %d, height=%d", width, height);
1075 return gst_caps_new_simple ("video/x-raw-rgb",
1076 "bpp", G_TYPE_INT, xcontext->bpp,
1077 "depth", G_TYPE_INT, xcontext->depth,
1078 "endianness", G_TYPE_INT, xcontext->endianness,
1079 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
1080 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
1081 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
1082 "width", G_TYPE_INT, width,
1083 "height", G_TYPE_INT, height,
1084 "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1085 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1090 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
1092 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1093 GstStructure *structure;
1094 const GValue *new_fps;
1096 /* If not yet opened, disallow setcaps until later */
1100 /* The only thing that can change is the framerate downstream wants */
1101 structure = gst_caps_get_structure (caps, 0);
1102 new_fps = gst_structure_get_value (structure, "framerate");
1106 /* Store this FPS for use when generating buffers */
1107 s->fps_n = gst_value_get_fraction_numerator (new_fps);
1108 s->fps_d = gst_value_get_fraction_denominator (new_fps);
1110 GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1116 gst_ximage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
1119 GstStructure *structure;
1121 for (i = 0; i < gst_caps_get_size (caps); ++i) {
1122 structure = gst_caps_get_structure (caps, i);
1124 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1126 GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
1130 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1132 GObjectClass *gc = G_OBJECT_CLASS (klass);
1133 GstElementClass *ec = GST_ELEMENT_CLASS (klass);
1134 GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
1135 GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
1137 gc->set_property = gst_ximage_src_set_property;
1138 gc->get_property = gst_ximage_src_get_property;
1139 gc->dispose = gst_ximage_src_dispose;
1140 gc->finalize = gst_ximage_src_finalize;
1142 g_object_class_install_property (gc, PROP_DISPLAY_NAME,
1143 g_param_spec_string ("display-name", "Display", "X Display Name", NULL,
1144 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1145 g_object_class_install_property (gc, PROP_SCREEN_NUM,
1146 g_param_spec_uint ("screen-num", "Screen number", "X Screen Number",
1147 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1148 g_object_class_install_property (gc, PROP_SHOW_POINTER,
1149 g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
1150 "Show mouse pointer (if XFixes extension enabled)", TRUE,
1151 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1153 * GstXImageSrc:use-damage
1155 * Use XDamage (if the XDamage extension is enabled)
1159 g_object_class_install_property (gc, PROP_USE_DAMAGE,
1160 g_param_spec_boolean ("use-damage", "Use XDamage",
1161 "Use XDamage (if XDamage extension enabled)", TRUE,
1162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1164 * GstXImageSrc:startx
1166 * X coordinate of top left corner of area to be recorded
1167 * (0 for top left of screen)
1171 g_object_class_install_property (gc, PROP_STARTX,
1172 g_param_spec_uint ("startx", "Start X co-ordinate",
1173 "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
1174 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1176 * GstXImageSrc:starty
1178 * Y coordinate of top left corner of area to be recorded
1179 * (0 for top left of screen)
1183 g_object_class_install_property (gc, PROP_STARTY,
1184 g_param_spec_uint ("starty", "Start Y co-ordinate",
1185 "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
1186 0, G_MAXINT, 0, G_PARAM_READWRITE));
1190 * X coordinate of bottom right corner of area to be recorded
1191 * (0 for bottom right of screen)
1195 g_object_class_install_property (gc, PROP_ENDX,
1196 g_param_spec_uint ("endx", "End X",
1197 "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1198 0, G_MAXINT, 0, G_PARAM_READWRITE));
1202 * Y coordinate of bottom right corner of area to be recorded
1203 * (0 for bottom right of screen)
1207 g_object_class_install_property (gc, PROP_ENDY,
1208 g_param_spec_uint ("endy", "End Y",
1209 "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1210 0, G_MAXINT, 0, G_PARAM_READWRITE));
1213 * GstXImageSrc:remote
1215 * Whether the X display is remote. The element will try to use alternate calls
1216 * known to work better with remote displays.
1220 g_object_class_install_property (gc, PROP_REMOTE,
1221 g_param_spec_boolean ("remote", "Remote dispay",
1222 "Whether the display is remote", FALSE,
1223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1227 * The XID of the window to capture. 0 for the root window (default).
1231 g_object_class_install_property (gc, PROP_XID,
1232 g_param_spec_uint64 ("xid", "Window XID",
1233 "Window XID to capture from", 0, G_MAXUINT64, 0,
1234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1236 * GstXImageSrc:xname
1238 * The name of the window to capture, if any.
1242 g_object_class_install_property (gc, PROP_XNAME,
1243 g_param_spec_string ("xname", "Window name",
1244 "Window name to capture from", NULL,
1245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1247 gst_element_class_set_details_simple (ec, "Ximage video source",
1249 "Creates a screenshot video stream",
1250 "Lutz Mueller <lutz@users.sourceforge.net>, "
1251 "Jan Schmidt <thaytan@mad.scientist.com>, "
1252 "Zaheer Merali <zaheerabbas at merali dot org>");
1253 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t));
1255 bc->fixate = gst_ximage_src_fixate;
1256 bc->get_caps = gst_ximage_src_get_caps;
1257 bc->set_caps = gst_ximage_src_set_caps;
1258 bc->start = gst_ximage_src_start;
1259 bc->stop = gst_ximage_src_stop;
1260 bc->unlock = gst_ximage_src_unlock;
1261 push_class->create = gst_ximage_src_create;
1265 gst_ximage_src_init (GstXImageSrc * ximagesrc)
1267 gst_base_src_set_format (GST_BASE_SRC (ximagesrc), GST_FORMAT_TIME);
1268 gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1270 g_mutex_init (&ximagesrc->pool_lock);
1271 g_mutex_init (&ximagesrc->x_lock);
1272 ximagesrc->show_pointer = TRUE;
1273 ximagesrc->use_damage = TRUE;
1274 ximagesrc->startx = 0;
1275 ximagesrc->starty = 0;
1276 ximagesrc->endx = 0;
1277 ximagesrc->endy = 0;
1278 ximagesrc->remote = FALSE;
1282 plugin_init (GstPlugin * plugin)
1286 GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
1287 "ximagesrc element debug");
1289 ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
1290 GST_TYPE_XIMAGE_SRC);
1295 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1298 "X11 video input plugin using standard Xlib calls",
1299 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);