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 /* elementfactory information */
56 static const GstElementDetails ximagesrc_details =
57 GST_ELEMENT_DETAILS ("Ximage video source",
59 "Creates a screenshot video stream",
60 "Lutz Mueller <lutz@users.sourceforge.net>, "
61 "Jan Schmidt <thaytan@mad.scientist.com>, "
62 "Zaheer Merali <zaheerabbas at merali dot org>");
64 static GstStaticPadTemplate t =
65 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
66 GST_STATIC_CAPS ("video/x-raw-rgb, "
67 "framerate = (fraction) [ 0, MAX ], "
68 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
69 "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
84 GST_BOILERPLATE (GstXImageSrc, gst_ximage_src, GstPushSrc, GST_TYPE_PUSH_SRC);
86 static void gst_ximage_src_fixate (GstPad * pad, GstCaps * caps);
87 static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
89 /* Called when a buffer is returned from the pipeline */
91 gst_ximage_src_return_buf (GstXImageSrc * ximagesrc,
92 GstXImageSrcBuffer * ximage)
94 /* If our geometry changed we can't reuse that image. */
95 if ((ximage->width != ximagesrc->width) ||
96 (ximage->height != ximagesrc->height)) {
97 GST_DEBUG_OBJECT (ximagesrc,
98 "destroy image %p as its size changed %dx%d vs current %dx%d",
99 ximage, ximage->width, ximage->height,
100 ximagesrc->width, ximagesrc->height);
101 g_mutex_lock (ximagesrc->x_lock);
102 gst_ximageutil_ximage_destroy (ximagesrc->xcontext, ximage);
103 g_mutex_unlock (ximagesrc->x_lock);
105 /* In that case we can reuse the image and add it to our image pool. */
106 GST_LOG_OBJECT (ximagesrc, "recycling image %p in pool", ximage);
107 /* need to increment the refcount again to recycle */
108 gst_buffer_ref (GST_BUFFER (ximage));
109 g_mutex_lock (ximagesrc->pool_lock);
110 ximagesrc->buffer_pool = g_slist_prepend (ximagesrc->buffer_pool, ximage);
111 g_mutex_unlock (ximagesrc->pool_lock);
116 gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
118 g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
120 if (s->xcontext != NULL)
123 g_mutex_lock (s->x_lock);
124 s->xcontext = ximageutil_xcontext_get (GST_ELEMENT (s), name);
125 if (s->xcontext == NULL) {
126 g_mutex_unlock (s->x_lock);
127 GST_ELEMENT_ERROR (s, RESOURCE, OPEN_READ,
128 ("Could not open X display for reading"),
129 ("NULL returned from getting xcontext"));
132 s->width = s->xcontext->width;
133 s->height = s->xcontext->height;
135 /* Always capture root window, for now */
136 s->xwindow = s->xcontext->root;
139 /* check if xfixes supported */
143 if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
145 s->have_xfixes = TRUE;
146 GST_DEBUG_OBJECT (s, "X Server supports XFixes");
149 GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
154 /* check if xdamage is supported */
157 long evmask = NoEventMask;
159 s->have_xdamage = FALSE;
161 s->damage_copy_gc = None;
162 s->damage_region = None;
164 if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
167 XDamageCreate (s->xcontext->disp, s->xwindow, XDamageReportNonEmpty);
168 if (s->damage != None) {
169 s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
170 if (s->damage_region != None) {
173 GST_DEBUG_OBJECT (s, "Using XDamage extension");
174 values.subwindow_mode = IncludeInferiors;
175 s->damage_copy_gc = XCreateGC (s->xcontext->disp,
176 s->xwindow, GCSubwindowMode, &values);
177 XSelectInput (s->xcontext->disp, s->xwindow, evmask);
179 s->have_xdamage = TRUE;
181 XDamageDestroy (s->xcontext->disp, s->damage);
185 GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
187 GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
193 g_mutex_unlock (s->x_lock);
195 if (s->xcontext == NULL)
202 gst_ximage_src_start (GstBaseSrc * basesrc)
204 GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
206 s->last_frame_no = -1;
209 gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
210 s->last_ximage = NULL;
212 return gst_ximage_src_open_display (s, s->display_name);
216 gst_ximage_src_stop (GstBaseSrc * basesrc)
218 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
221 if (src->last_ximage)
222 gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
223 src->last_ximage = NULL;
226 gst_ximage_src_clear_bufpool (src);
229 if (src->cursor_image)
230 XFree (src->cursor_image);
231 src->cursor_image = NULL;
235 g_mutex_lock (src->x_lock);
238 if (src->damage_copy_gc != None) {
239 XFreeGC (src->xcontext->disp, src->damage_copy_gc);
240 src->damage_copy_gc = None;
242 if (src->damage_region != None) {
243 XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
244 src->damage_region = None;
246 if (src->damage != None) {
247 XDamageDestroy (src->xcontext->disp, src->damage);
252 ximageutil_xcontext_clear (src->xcontext);
253 src->xcontext = NULL;
254 g_mutex_unlock (src->x_lock);
261 gst_ximage_src_unlock (GstBaseSrc * basesrc)
263 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
265 /* Awaken the create() func if it's waiting on the clock */
266 GST_OBJECT_LOCK (src);
268 GST_DEBUG_OBJECT (src, "Waking up waiting clock");
269 gst_clock_id_unschedule (src->clock_id);
271 GST_OBJECT_UNLOCK (src);
277 gst_ximage_src_recalc (GstXImageSrc * src)
282 /* Maybe later we can check the display hasn't changed size */
283 /* We could use XQueryPointer to get only the current window. */
289 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
297 gint r_shift, r_max, r_shift_out;
298 gint g_shift, g_max, g_shift_out;
299 gint b_shift, b_max, b_shift_out;
301 switch (xcontext->bpp) {
306 color = GUINT16_FROM_LE (*(guint16 *) (dest));
309 color = GUINT32_FROM_LE (*(guint32 *) (dest));
312 /* Should not reach here */
313 g_return_if_reached ();
316 /* possible optimisation:
317 * move the code that finds shift and max in the _link function */
318 for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
319 for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
321 for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
323 for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
325 for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
327 for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
331 r_max = (xcontext->visual->red_mask >> r_shift);
332 b_max = (xcontext->visual->blue_mask >> b_shift);
333 g_max = (xcontext->visual->green_mask >> g_shift);
335 #define RGBXXX_R(x) (((x)>>r_shift) & (r_max))
336 #define RGBXXX_G(x) (((x)>>g_shift) & (g_max))
337 #define RGBXXX_B(x) (((x)>>b_shift) & (b_max))
339 dr = (RGBXXX_R (color) * 255) / r_max;
340 dg = (RGBXXX_G (color) * 255) / g_max;
341 db = (RGBXXX_B (color) * 255) / b_max;
343 dr = (r * a + (0xff - a) * dr) / 0xff;
344 dg = (g * a + (0xff - a) * dg) / 0xff;
345 db = (b * a + (0xff - a) * db) / 0xff;
347 color = (((dr * r_max) / 255) << r_shift_out) +
348 (((dg * g_max) / 255) << g_shift_out) +
349 (((db * b_max) / 255) << b_shift_out);
351 switch (xcontext->bpp) {
356 *(guint16 *) (dest) = color;
359 *(guint32 *) (dest) = color;
362 g_warning ("bpp %d not supported\n", xcontext->bpp);
367 /* Retrieve an XImageSrcBuffer, preferably from our
368 * pool of existing images and populate it from the window */
369 static GstXImageSrcBuffer *
370 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
372 GstXImageSrcBuffer *ximage = NULL;
374 g_mutex_lock (ximagesrc->pool_lock);
375 while (ximagesrc->buffer_pool != NULL) {
376 ximage = ximagesrc->buffer_pool->data;
378 if ((ximage->width != ximagesrc->width) ||
379 (ximage->height != ximagesrc->height)) {
380 gst_ximage_buffer_free (ximage);
383 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
384 ximagesrc->buffer_pool);
386 g_mutex_unlock (ximagesrc->pool_lock);
388 if (ximage == NULL) {
389 GstXContext *xcontext;
390 GstCaps *caps = NULL;
392 GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
393 ximagesrc->width, ximagesrc->height);
395 g_mutex_lock (ximagesrc->x_lock);
396 ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
397 GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
398 (BufferReturnFunc) (gst_ximage_src_return_buf));
399 if (ximage == NULL) {
400 GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
401 ("could not create a %dx%d ximage", ximage->width, ximage->height));
402 g_mutex_unlock (ximagesrc->x_lock);
406 xcontext = ximagesrc->xcontext;
409 caps = gst_caps_new_simple ("video/x-raw-rgb",
410 "bpp", G_TYPE_INT, xcontext->bpp,
411 "depth", G_TYPE_INT, xcontext->depth,
412 "endianness", G_TYPE_INT, xcontext->endianness,
413 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
414 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
415 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
416 "width", G_TYPE_INT, ximagesrc->width,
417 "height", G_TYPE_INT, ximagesrc->height,
418 "framerate", GST_TYPE_FRACTION, ximagesrc->fps_n, ximagesrc->fps_d,
419 "pixel-aspect-ratio", GST_TYPE_FRACTION,
420 gst_value_get_fraction_numerator (xcontext->par),
421 gst_value_get_fraction_denominator (xcontext->par), NULL);
423 gst_buffer_set_caps (GST_BUFFER (ximage), caps);
424 g_mutex_unlock (ximagesrc->x_lock);
426 gst_caps_unref (caps);
429 g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
431 if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
432 ximagesrc->last_ximage != NULL) {
435 /* have_frame is TRUE when either the entire screen has been
436 * grabbed or when the last image has been copied */
437 gboolean have_frame = FALSE;
439 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
442 XNextEvent (ximagesrc->xcontext->disp, &ev);
444 if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
449 parts = XFixesCreateRegion (ximagesrc->xcontext->disp, 0, 0);
450 XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
452 /* Now copy out all of the damaged rectangles. */
453 rects = XFixesFetchRegion (ximagesrc->xcontext->disp, parts, &nrects);
458 GST_LOG_OBJECT (ximagesrc,
459 "Copying from last frame ximage->size: %d",
460 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
461 memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
462 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
463 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
466 for (i = 0; i < nrects; i++) {
467 GST_LOG_OBJECT (ximagesrc,
468 "Damaged sub-region @ %d,%d size %dx%d reported",
469 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
471 /* if we only want a small area, clip this damage region to
473 if (ximagesrc->endx > ximagesrc->startx &&
474 ximagesrc->endy > ximagesrc->starty) {
475 /* see if damage area intersects */
476 if (rects[i].x + rects[i].width < ximagesrc->startx ||
477 rects[i].x > ximagesrc->endx) {
479 } else if (rects[i].y + rects[i].height < ximagesrc->starty ||
480 rects[i].y > ximagesrc->endy) {
483 /* find intersect region */
484 int startx, starty, width, height;
486 startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
488 starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
490 width = (rects[i].x + rects[i].width < ximagesrc->endx) ?
491 rects[i].x + rects[i].width - startx :
492 ximagesrc->endx - startx;
493 height = (rects[i].y + rects[i].height < ximagesrc->endy) ?
494 rects[i].y + rects[i].height - starty : ximagesrc->endy -
497 GST_LOG_OBJECT (ximagesrc,
498 "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
499 startx, starty, width, height);
500 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
501 startx, starty, width, height, AllPlanes, ZPixmap,
502 ximage->ximage, startx - ximagesrc->startx,
503 starty - ximagesrc->starty);
507 GST_LOG_OBJECT (ximagesrc,
508 "Retrieving damaged sub-region @ %d,%d size %dx%d",
509 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
511 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
512 rects[i].x, rects[i].y,
513 rects[i].width, rects[i].height,
514 AllPlanes, ZPixmap, ximage->ximage, rects[i].x, rects[i].y);
520 } while (XPending (ximagesrc->xcontext->disp));
522 GST_LOG_OBJECT (ximagesrc,
523 "Copying from last frame ximage->size: %d",
524 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
525 memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
526 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
527 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
531 /* re-get area where last mouse pointer was but only if in our clipping
533 if (ximagesrc->cursor_image) {
534 gint x, y, width, height;
536 x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
537 y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
538 width = ximagesrc->cursor_image->width;
539 height = ximagesrc->cursor_image->height;
541 /* bounds checking */
546 if (x + width > ximagesrc->xcontext->width)
547 width = ximagesrc->xcontext->width - x;
548 if (y + height > ximagesrc->xcontext->height)
549 height = ximagesrc->xcontext->height - y;
552 GST_DEBUG_OBJECT (ximagesrc,
553 "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
554 x, y, width, height, ximagesrc->startx, ximagesrc->starty,
555 ximagesrc->endx, ximagesrc->endy);
556 /* only get where cursor last was, if it is in our range */
557 if (ximagesrc->endx > ximagesrc->startx &&
558 ximagesrc->endy > ximagesrc->starty) {
560 if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
562 } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
565 /* find intersect region */
566 int startx, starty, iwidth, iheight;
568 startx = (x < ximagesrc->startx) ? ximagesrc->startx : x;
569 starty = (y < ximagesrc->starty) ? ximagesrc->starty : y;
570 iwidth = (x + width < ximagesrc->endx) ?
571 x + width - startx : ximagesrc->endx - startx;
572 iheight = (y + height < ximagesrc->endy) ?
573 y + height - starty : ximagesrc->endy - starty;
574 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
575 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
576 startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
577 ximage->ximage, startx - ximagesrc->startx,
578 starty - ximagesrc->starty);
582 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
583 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
584 x, y, width, height, AllPlanes, ZPixmap, ximage->ximage, x, y);
594 if (ximagesrc->xcontext->use_xshm) {
595 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
596 XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
597 ximage->ximage, ximagesrc->startx, ximagesrc->starty, AllPlanes);
600 #endif /* HAVE_XSHM */
602 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
603 ximage->ximage = XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
604 ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
605 ximagesrc->height, AllPlanes, ZPixmap);
612 if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {
614 GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
616 if (ximagesrc->cursor_image)
617 XFree (ximagesrc->cursor_image);
618 ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
619 if (ximagesrc->cursor_image != NULL) {
620 int cx, cy, i, j, count;
621 int startx, starty, iwidth, iheight;
622 gboolean clipped = FALSE;
623 gboolean cursor_in_image = TRUE;
625 cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
628 cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
631 count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
633 /* only get where cursor last was, if it is in our range */
634 if (ximagesrc->endx > ximagesrc->startx &&
635 ximagesrc->endy > ximagesrc->starty) {
637 if (cx + ximagesrc->cursor_image->width < ximagesrc->startx ||
638 cx > ximagesrc->endx) {
640 cursor_in_image = FALSE;
641 } else if (cy + ximagesrc->cursor_image->height < ximagesrc->starty ||
642 cy > ximagesrc->endy) {
644 cursor_in_image = FALSE;
646 /* find intersect region */
648 startx = (cx < ximagesrc->startx) ? ximagesrc->startx : cx;
649 starty = (cy < ximagesrc->starty) ? ximagesrc->starty : cy;
650 iwidth = (cx + ximagesrc->cursor_image->width < ximagesrc->endx) ?
651 cx + ximagesrc->cursor_image->width - startx :
652 ximagesrc->endx - startx;
653 iheight = (cy + ximagesrc->cursor_image->height < ximagesrc->endy) ?
654 cy + ximagesrc->cursor_image->height - starty :
655 ximagesrc->endy - starty;
661 iwidth = ximagesrc->cursor_image->width;
662 iheight = ximagesrc->cursor_image->height;
665 if (cursor_in_image) {
666 GST_DEBUG_OBJECT (ximagesrc, "Cursor is in image so trying to draw it");
667 for (i = 0; i < count; i++)
668 ximagesrc->cursor_image->pixels[i] =
669 GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
670 /* copy those pixels across */
672 j < starty + iheight && j < ximagesrc->starty + ximagesrc->height;
675 i < startx + iwidth && i < ximagesrc->startx + ximagesrc->width;
680 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
681 cy) * ximagesrc->cursor_image->width + (i - cx))]);
683 (guint8 *) & (ximage->ximage->data[((j -
684 ximagesrc->starty) * ximagesrc->width + (i -
685 ximagesrc->startx)) * (ximagesrc->xcontext->bpp /
688 composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
697 if (ximagesrc->have_xdamage && ximagesrc->use_damage) {
698 /* need to ref ximage to put in last_ximage */
699 gst_buffer_ref (GST_BUFFER (ximage));
700 if (ximagesrc->last_ximage) {
701 gst_buffer_unref (GST_BUFFER (ximagesrc->last_ximage));
703 ximagesrc->last_ximage = ximage;
704 GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
711 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
713 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
714 GstXImageSrcBuffer *image;
715 GstClockTime base_time;
716 GstClockTime next_capture_ts;
718 gint64 next_frame_no;
720 if (!gst_ximage_src_recalc (s)) {
721 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
722 (_("Changing resolution at runtime is not yet supported.")), (NULL));
723 return GST_FLOW_ERROR;
726 if (s->fps_n <= 0 || s->fps_d <= 0)
727 return GST_FLOW_NOT_NEGOTIATED; /* FPS must be > 0 */
729 /* Now, we might need to wait for the next multiple of the fps
730 * before capturing */
733 if (GST_ELEMENT_CLOCK (s) == NULL) {
734 GST_OBJECT_UNLOCK (s);
735 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
736 (_("Cannot operate without a clock")), (NULL));
737 return GST_FLOW_ERROR;
740 base_time = GST_ELEMENT_CAST (s)->base_time;
741 next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
742 next_capture_ts -= base_time;
744 /* Figure out which 'frame number' position we're at, based on the cur time
746 next_frame_no = gst_util_uint64_scale (next_capture_ts,
747 s->fps_n, GST_SECOND * s->fps_d);
748 if (next_frame_no == s->last_frame_no) {
752 /* Need to wait for the next frame */
755 /* Figure out what the next frame time is */
756 next_capture_ts = gst_util_uint64_scale (next_frame_no,
757 s->fps_d * GST_SECOND, s->fps_n);
759 id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
760 next_capture_ts + base_time);
763 /* release the object lock while waiting */
764 GST_OBJECT_UNLOCK (s);
766 GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
768 ret = gst_clock_id_wait (id, NULL);
771 gst_clock_id_unref (id);
773 if (ret == GST_CLOCK_UNSCHEDULED) {
774 /* Got woken up by the unlock function */
775 GST_OBJECT_UNLOCK (s);
776 return GST_FLOW_WRONG_STATE;
778 /* Duration is a complete 1/fps frame duration */
779 dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
781 GstClockTime next_frame_ts;
783 GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
784 G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
785 G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
786 next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
787 s->fps_d * GST_SECOND, s->fps_n);
788 /* Frame duration is from now until the next expected capture time */
789 dur = next_frame_ts - next_capture_ts;
791 s->last_frame_no = next_frame_no;
792 GST_OBJECT_UNLOCK (s);
794 image = gst_ximage_src_ximage_get (s);
796 return GST_FLOW_ERROR;
798 *buf = GST_BUFFER (image);
799 GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
800 GST_BUFFER_DURATION (*buf) = dur;
806 gst_ximage_src_set_property (GObject * object, guint prop_id,
807 const GValue * value, GParamSpec * pspec)
809 GstXImageSrc *src = GST_XIMAGE_SRC (object);
812 case PROP_DISPLAY_NAME:
814 g_free (src->display_name);
815 src->display_name = g_strdup (g_value_get_string (value));
817 case PROP_SCREEN_NUM:
818 src->screen_num = g_value_get_uint (value);
820 case PROP_SHOW_POINTER:
821 src->show_pointer = g_value_get_boolean (value);
823 case PROP_USE_DAMAGE:
824 src->use_damage = g_value_get_boolean (value);
827 src->startx = g_value_get_uint (value);
830 src->starty = g_value_get_uint (value);
833 src->endx = g_value_get_uint (value);
836 src->endy = g_value_get_uint (value);
844 gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
847 GstXImageSrc *src = GST_XIMAGE_SRC (object);
850 case PROP_DISPLAY_NAME:
852 g_value_set_string (value, DisplayString (src->xcontext->disp));
854 g_value_set_string (value, src->display_name);
857 case PROP_SCREEN_NUM:
858 g_value_set_uint (value, src->screen_num);
860 case PROP_SHOW_POINTER:
861 g_value_set_boolean (value, src->show_pointer);
863 case PROP_USE_DAMAGE:
864 g_value_set_boolean (value, src->use_damage);
867 g_value_set_uint (value, src->startx);
870 g_value_set_uint (value, src->starty);
873 g_value_set_uint (value, src->endx);
876 g_value_set_uint (value, src->endy);
879 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
885 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
887 g_mutex_lock (ximagesrc->pool_lock);
888 while (ximagesrc->buffer_pool != NULL) {
889 GstXImageSrcBuffer *ximage = ximagesrc->buffer_pool->data;
891 gst_ximage_buffer_free (ximage);
893 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
894 ximagesrc->buffer_pool);
896 g_mutex_unlock (ximagesrc->pool_lock);
900 gst_ximage_src_base_init (gpointer g_class)
902 GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
904 gst_element_class_set_details (ec, &ximagesrc_details);
905 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t));
909 gst_ximage_src_dispose (GObject * object)
911 /* Drop references in the buffer_pool */
912 gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
914 G_OBJECT_CLASS (parent_class)->dispose (object);
918 gst_ximage_src_finalize (GObject * object)
920 GstXImageSrc *src = GST_XIMAGE_SRC (object);
923 ximageutil_xcontext_clear (src->xcontext);
925 g_mutex_free (src->pool_lock);
926 g_mutex_free (src->x_lock);
928 G_OBJECT_CLASS (parent_class)->finalize (object);
932 gst_ximage_src_get_caps (GstBaseSrc * bs)
934 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
935 GstXContext *xcontext;
936 gint x, y, width, height;
938 if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
940 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
943 if (!gst_ximage_src_recalc (s))
945 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
948 xcontext = s->xcontext;
951 width = xcontext->width;
952 height = xcontext->height;
953 if (s->endx > s->startx && s->endy > s->starty) {
954 /* this means user has put in values */
955 if (s->startx < xcontext->width && s->endx < xcontext->width &&
956 s->starty < xcontext->height && s->endy < xcontext->height) {
957 /* values are fine */
960 s->width = width = s->endx - s->startx;
961 s->height = height = s->endy - s->starty;
964 ("User put in co-ordinates overshooting the X resolution, setting to full screen");
971 GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
977 GST_DEBUG ("width = %d, height=%d", width, height);
978 return gst_caps_new_simple ("video/x-raw-rgb",
979 "bpp", G_TYPE_INT, xcontext->bpp,
980 "depth", G_TYPE_INT, xcontext->depth,
981 "endianness", G_TYPE_INT, xcontext->endianness,
982 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
983 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
984 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
985 "width", G_TYPE_INT, width,
986 "height", G_TYPE_INT, height,
987 "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
988 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
993 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
995 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
996 GstStructure *structure;
997 const GValue *new_fps;
999 /* If not yet opened, disallow setcaps until later */
1003 /* The only thing that can change is the framerate downstream wants */
1004 structure = gst_caps_get_structure (caps, 0);
1005 new_fps = gst_structure_get_value (structure, "framerate");
1009 /* Store this FPS for use when generating buffers */
1010 s->fps_n = gst_value_get_fraction_numerator (new_fps);
1011 s->fps_d = gst_value_get_fraction_denominator (new_fps);
1013 GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1019 gst_ximage_src_fixate (GstPad * pad, GstCaps * caps)
1022 GstStructure *structure;
1024 for (i = 0; i < gst_caps_get_size (caps); ++i) {
1025 structure = gst_caps_get_structure (caps, i);
1027 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1032 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1034 GObjectClass *gc = G_OBJECT_CLASS (klass);
1035 GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
1036 GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
1038 gc->set_property = gst_ximage_src_set_property;
1039 gc->get_property = gst_ximage_src_get_property;
1040 gc->dispose = gst_ximage_src_dispose;
1041 gc->finalize = gst_ximage_src_finalize;
1043 g_object_class_install_property (gc, PROP_DISPLAY_NAME,
1044 g_param_spec_string ("display-name", "Display", "X Display Name", NULL,
1045 G_PARAM_READWRITE));
1046 g_object_class_install_property (gc, PROP_SCREEN_NUM,
1047 g_param_spec_uint ("screen-num", "Screen number", "X Screen Number",
1048 0, G_MAXINT, 0, G_PARAM_READWRITE));
1049 g_object_class_install_property (gc, PROP_SHOW_POINTER,
1050 g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
1051 "Show mouse pointer (if XFixes extension enabled)", TRUE,
1052 G_PARAM_READWRITE));
1054 * GstXImageSrc:use-damage
1056 * Use XDamage (if the XDamage extension is enabled)
1060 g_object_class_install_property (gc, PROP_USE_DAMAGE,
1061 g_param_spec_boolean ("use-damage", "Use XDamage",
1062 "Use XDamage (if XDamage extension enabled)", TRUE,
1063 G_PARAM_READWRITE));
1065 * GstXImageSrc:startx
1067 * X coordinate of top left corner of area to be recorded
1068 * (0 for top left of screen)
1072 g_object_class_install_property (gc, PROP_STARTX,
1073 g_param_spec_uint ("startx", "Start X co-ordinate",
1074 "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
1075 0, G_MAXINT, 0, G_PARAM_READWRITE));
1077 * GstXImageSrc:starty
1079 * Y coordinate of top left corner of area to be recorded
1080 * (0 for top left of screen)
1084 g_object_class_install_property (gc, PROP_STARTY,
1085 g_param_spec_uint ("starty", "Start Y co-ordinate",
1086 "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
1087 0, G_MAXINT, 0, G_PARAM_READWRITE));
1091 * X coordinate of bottom right corner of area to be recorded
1092 * (0 for bottom right of screen)
1096 g_object_class_install_property (gc, PROP_ENDX,
1097 g_param_spec_uint ("endx", "End X",
1098 "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1099 0, G_MAXINT, 0, G_PARAM_READWRITE));
1103 * Y coordinate of bottom right corner of area to be recorded
1104 * (0 for bottom right of screen)
1108 g_object_class_install_property (gc, PROP_ENDY,
1109 g_param_spec_uint ("endy", "End Y",
1110 "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1111 0, G_MAXINT, 0, G_PARAM_READWRITE));
1113 parent_class = g_type_class_peek_parent (klass);
1115 push_class->create = gst_ximage_src_create;
1116 bc->get_caps = gst_ximage_src_get_caps;
1117 bc->set_caps = gst_ximage_src_set_caps;
1118 bc->start = gst_ximage_src_start;
1119 bc->stop = gst_ximage_src_stop;
1120 bc->unlock = gst_ximage_src_unlock;
1124 gst_ximage_src_init (GstXImageSrc * ximagesrc, GstXImageSrcClass * klass)
1126 gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1127 gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (ximagesrc),
1128 gst_ximage_src_fixate);
1130 ximagesrc->pool_lock = g_mutex_new ();
1131 ximagesrc->x_lock = g_mutex_new ();
1132 ximagesrc->show_pointer = TRUE;
1133 ximagesrc->use_damage = TRUE;
1134 ximagesrc->startx = 0;
1135 ximagesrc->starty = 0;
1136 ximagesrc->endx = 0;
1137 ximagesrc->endy = 0;
1141 plugin_init (GstPlugin * plugin)
1145 GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
1146 "ximagesrc element debug");
1148 ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
1149 GST_TYPE_XIMAGE_SRC);
1154 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1157 "X11 video input plugin using standard Xlib calls",
1158 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);