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>
51 GST_DEBUG_CATEGORY_STATIC (gst_debug_ximage_src);
52 #define GST_CAT_DEFAULT gst_debug_ximage_src
54 /* elementfactory information */
55 static const GstElementDetails ximagesrc_details =
56 GST_ELEMENT_DETAILS ("Ximage video source",
58 "Creates a screenshot video stream",
59 "Lutz Mueller <lutz@users.sourceforge.net>, "
60 "Jan Schmidt <thaytan@mad.scientist.com>, "
61 "Zaheer Merali <zaheerabbas at merali dot org>");
63 static GstStaticPadTemplate t =
64 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
65 GST_STATIC_CAPS ("video/x-raw-rgb, "
66 "framerate = (fraction) [ 0, MAX ], "
67 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
68 "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
83 GST_BOILERPLATE (GstXImageSrc, gst_ximage_src, GstPushSrc, GST_TYPE_PUSH_SRC);
85 static void gst_ximage_src_fixate (GstPad * pad, GstCaps * caps);
86 static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
88 /* Called when a buffer is returned from the pipeline */
90 gst_ximage_src_return_buf (GstXImageSrc * ximagesrc,
91 GstXImageSrcBuffer * ximage)
93 /* If our geometry changed we can't reuse that image. */
94 if ((ximage->width != ximagesrc->width) ||
95 (ximage->height != ximagesrc->height)) {
96 GST_DEBUG_OBJECT (ximagesrc,
97 "destroy image %p as its size changed %dx%d vs current %dx%d",
98 ximage, ximage->width, ximage->height,
99 ximagesrc->width, ximagesrc->height);
100 g_mutex_lock (ximagesrc->x_lock);
101 gst_ximageutil_ximage_destroy (ximagesrc->xcontext, ximage);
102 g_mutex_unlock (ximagesrc->x_lock);
104 /* In that case we can reuse the image and add it to our image pool. */
105 GST_LOG_OBJECT (ximagesrc, "recycling image %p in pool", ximage);
106 /* need to increment the refcount again to recycle */
107 gst_buffer_ref (GST_BUFFER (ximage));
108 g_mutex_lock (ximagesrc->pool_lock);
109 ximagesrc->buffer_pool = g_slist_prepend (ximagesrc->buffer_pool, ximage);
110 g_mutex_unlock (ximagesrc->pool_lock);
115 gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
117 g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
119 if (s->xcontext != NULL)
122 g_mutex_lock (s->x_lock);
123 s->xcontext = ximageutil_xcontext_get (GST_ELEMENT (s), name);
124 if (s->xcontext == NULL) {
125 g_mutex_unlock (s->x_lock);
126 GST_ELEMENT_ERROR (s, RESOURCE, OPEN_READ,
127 ("Could not open X display for reading"),
128 ("NULL returned from getting xcontext"));
131 s->width = s->xcontext->width;
132 s->height = s->xcontext->height;
134 /* Always capture root window, for now */
135 s->xwindow = s->xcontext->root;
138 /* check if xfixes supported */
142 if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
144 s->have_xfixes = TRUE;
145 GST_DEBUG_OBJECT (s, "X Server supports XFixes");
148 GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
153 /* check if xdamage is supported */
156 long evmask = NoEventMask;
158 s->have_xdamage = FALSE;
160 s->damage_copy_gc = None;
161 s->damage_region = None;
163 if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
166 XDamageCreate (s->xcontext->disp, s->xwindow, XDamageReportNonEmpty);
167 if (s->damage != None) {
168 s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
169 if (s->damage_region != None) {
172 GST_DEBUG_OBJECT (s, "Using XDamage extension");
173 values.subwindow_mode = IncludeInferiors;
174 s->damage_copy_gc = XCreateGC (s->xcontext->disp,
175 s->xwindow, GCSubwindowMode, &values);
176 XSelectInput (s->xcontext->disp, s->xwindow, evmask);
178 s->have_xdamage = TRUE;
180 XDamageDestroy (s->xcontext->disp, s->damage);
184 GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
186 GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
192 g_mutex_unlock (s->x_lock);
194 if (s->xcontext == NULL)
201 gst_ximage_src_start (GstBaseSrc * basesrc)
203 GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
205 s->last_frame_no = -1;
208 gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
209 s->last_ximage = NULL;
211 return gst_ximage_src_open_display (s, s->display_name);
215 gst_ximage_src_stop (GstBaseSrc * basesrc)
217 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
220 if (src->last_ximage)
221 gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
222 src->last_ximage = NULL;
225 gst_ximage_src_clear_bufpool (src);
228 if (src->cursor_image)
229 XFree (src->cursor_image);
230 src->cursor_image = NULL;
234 g_mutex_lock (src->x_lock);
237 if (src->damage_copy_gc != None) {
238 XFreeGC (src->xcontext->disp, src->damage_copy_gc);
239 src->damage_copy_gc = None;
241 if (src->damage_region != None) {
242 XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
243 src->damage_region = None;
245 if (src->damage != None) {
246 XDamageDestroy (src->xcontext->disp, src->damage);
251 ximageutil_xcontext_clear (src->xcontext);
252 src->xcontext = NULL;
253 g_mutex_unlock (src->x_lock);
260 gst_ximage_src_unlock (GstBaseSrc * basesrc)
262 GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
264 /* Awaken the create() func if it's waiting on the clock */
265 GST_OBJECT_LOCK (src);
267 GST_DEBUG_OBJECT (src, "Waking up waiting clock");
268 gst_clock_id_unschedule (src->clock_id);
270 GST_OBJECT_UNLOCK (src);
276 gst_ximage_src_recalc (GstXImageSrc * src)
281 /* Maybe later we can check the display hasn't changed size */
282 /* We could use XQueryPointer to get only the current window. */
288 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
296 gint r_shift, r_max, r_shift_out;
297 gint g_shift, g_max, g_shift_out;
298 gint b_shift, b_max, b_shift_out;
300 switch (xcontext->bpp) {
305 color = GUINT16_FROM_LE (*(guint16 *) (dest));
308 color = GUINT32_FROM_LE (*(guint32 *) (dest));
311 /* Should not reach here */
312 g_return_if_reached ();
315 /* possible optimisation:
316 * move the code that finds shift and max in the _link function */
317 for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
318 for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
320 for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
322 for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
324 for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
326 for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
330 r_max = (xcontext->visual->red_mask >> r_shift);
331 b_max = (xcontext->visual->blue_mask >> b_shift);
332 g_max = (xcontext->visual->green_mask >> g_shift);
334 #define RGBXXX_R(x) (((x)>>r_shift) & (r_max))
335 #define RGBXXX_G(x) (((x)>>g_shift) & (g_max))
336 #define RGBXXX_B(x) (((x)>>b_shift) & (b_max))
338 dr = (RGBXXX_R (color) * 255) / r_max;
339 dg = (RGBXXX_G (color) * 255) / g_max;
340 db = (RGBXXX_B (color) * 255) / b_max;
342 dr = (r * a + (0xff - a) * dr) / 0xff;
343 dg = (g * a + (0xff - a) * dg) / 0xff;
344 db = (b * a + (0xff - a) * db) / 0xff;
346 color = (((dr * r_max) / 255) << r_shift_out) +
347 (((dg * g_max) / 255) << g_shift_out) +
348 (((db * b_max) / 255) << b_shift_out);
350 switch (xcontext->bpp) {
355 *(guint16 *) (dest) = color;
358 *(guint32 *) (dest) = color;
361 g_warning ("bpp %d not supported\n", xcontext->bpp);
366 /* Retrieve an XImageSrcBuffer, preferably from our
367 * pool of existing images and populate it from the window */
368 static GstXImageSrcBuffer *
369 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
371 GstXImageSrcBuffer *ximage = NULL;
373 g_mutex_lock (ximagesrc->pool_lock);
374 while (ximagesrc->buffer_pool != NULL) {
375 ximage = ximagesrc->buffer_pool->data;
377 if ((ximage->width != ximagesrc->width) ||
378 (ximage->height != ximagesrc->height)) {
379 gst_ximage_buffer_free (ximage);
382 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
383 ximagesrc->buffer_pool);
385 g_mutex_unlock (ximagesrc->pool_lock);
387 if (ximage == NULL) {
388 GstXContext *xcontext;
389 GstCaps *caps = NULL;
391 GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
392 ximagesrc->width, ximagesrc->height);
394 g_mutex_lock (ximagesrc->x_lock);
395 ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
396 GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
397 (BufferReturnFunc) (gst_ximage_src_return_buf));
398 if (ximage == NULL) {
399 GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
400 ("could not create a %dx%d ximage", ximage->width, ximage->height));
401 g_mutex_unlock (ximagesrc->x_lock);
405 xcontext = ximagesrc->xcontext;
408 caps = gst_caps_new_simple ("video/x-raw-rgb",
409 "bpp", G_TYPE_INT, xcontext->bpp,
410 "depth", G_TYPE_INT, xcontext->depth,
411 "endianness", G_TYPE_INT, xcontext->endianness,
412 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
413 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
414 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
415 "width", G_TYPE_INT, ximagesrc->width,
416 "height", G_TYPE_INT, ximagesrc->height,
417 "framerate", GST_TYPE_FRACTION, ximagesrc->fps_n, ximagesrc->fps_d,
418 "pixel-aspect-ratio", GST_TYPE_FRACTION,
419 gst_value_get_fraction_numerator (xcontext->par),
420 gst_value_get_fraction_denominator (xcontext->par), NULL);
422 gst_buffer_set_caps (GST_BUFFER (ximage), caps);
423 g_mutex_unlock (ximagesrc->x_lock);
425 gst_caps_unref (caps);
428 g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
430 if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
431 ximagesrc->last_ximage != NULL) {
434 /* have_frame is TRUE when either the entire screen has been
435 * grabbed or when the last image has been copied */
436 gboolean have_frame = FALSE;
438 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
441 XNextEvent (ximagesrc->xcontext->disp, &ev);
443 if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
448 parts = XFixesCreateRegion (ximagesrc->xcontext->disp, 0, 0);
449 XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
451 /* Now copy out all of the damaged rectangles. */
452 rects = XFixesFetchRegion (ximagesrc->xcontext->disp, parts, &nrects);
457 GST_LOG_OBJECT (ximagesrc,
458 "Copying from last frame ximage->size: %d",
459 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
460 memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
461 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
462 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
465 for (i = 0; i < nrects; i++) {
466 GST_LOG_OBJECT (ximagesrc,
467 "Damaged sub-region @ %d,%d size %dx%d reported",
468 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
470 /* if we only want a small area, clip this damage region to
472 if (ximagesrc->endx > ximagesrc->startx &&
473 ximagesrc->endy > ximagesrc->starty) {
474 /* see if damage area intersects */
475 if (rects[i].x + rects[i].width < ximagesrc->startx ||
476 rects[i].x > ximagesrc->endx) {
478 } else if (rects[i].y + rects[i].height < ximagesrc->starty ||
479 rects[i].y > ximagesrc->endy) {
482 /* find intersect region */
483 int startx, starty, width, height;
485 startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
487 starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
489 width = (rects[i].x + rects[i].width < ximagesrc->endx) ?
490 rects[i].x + rects[i].width - startx :
491 ximagesrc->endx - startx;
492 height = (rects[i].y + rects[i].height < ximagesrc->endy) ?
493 rects[i].y + rects[i].height - starty : ximagesrc->endy -
496 GST_LOG_OBJECT (ximagesrc,
497 "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
498 startx, starty, width, height);
499 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
500 startx, starty, width, height, AllPlanes, ZPixmap,
501 ximage->ximage, startx - ximagesrc->startx,
502 starty - ximagesrc->starty);
506 GST_LOG_OBJECT (ximagesrc,
507 "Retrieving damaged sub-region @ %d,%d size %dx%d",
508 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
510 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
511 rects[i].x, rects[i].y,
512 rects[i].width, rects[i].height,
513 AllPlanes, ZPixmap, ximage->ximage, rects[i].x, rects[i].y);
519 } while (XPending (ximagesrc->xcontext->disp));
521 GST_LOG_OBJECT (ximagesrc,
522 "Copying from last frame ximage->size: %d",
523 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
524 memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
525 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
526 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
530 /* re-get area where last mouse pointer was but only if in our clipping
532 if (ximagesrc->cursor_image) {
533 gint x, y, width, height;
535 x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
536 y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
537 width = ximagesrc->cursor_image->width;
538 height = ximagesrc->cursor_image->height;
540 /* bounds checking */
545 if (x + width > ximagesrc->xcontext->width)
546 width = ximagesrc->xcontext->width - x;
547 if (y + height > ximagesrc->xcontext->height)
548 height = ximagesrc->xcontext->height - y;
551 GST_DEBUG_OBJECT (ximagesrc,
552 "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
553 x, y, width, height, ximagesrc->startx, ximagesrc->starty,
554 ximagesrc->endx, ximagesrc->endy);
555 /* only get where cursor last was, if it is in our range */
556 if (ximagesrc->endx > ximagesrc->startx &&
557 ximagesrc->endy > ximagesrc->starty) {
559 if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
561 } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
564 /* find intersect region */
565 int startx, starty, iwidth, iheight;
567 startx = (x < ximagesrc->startx) ? ximagesrc->startx : x;
568 starty = (y < ximagesrc->starty) ? ximagesrc->starty : y;
569 iwidth = (x + width < ximagesrc->endx) ?
570 x + width - startx : ximagesrc->endx - startx;
571 iheight = (y + height < ximagesrc->endy) ?
572 y + height - starty : ximagesrc->endy - starty;
573 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
574 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
575 startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
576 ximage->ximage, startx - ximagesrc->startx,
577 starty - ximagesrc->starty);
581 GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
582 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
583 x, y, width, height, AllPlanes, ZPixmap, ximage->ximage, x, y);
593 if (ximagesrc->xcontext->use_xshm) {
594 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
595 XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
596 ximage->ximage, ximagesrc->startx, ximagesrc->starty, AllPlanes);
599 #endif /* HAVE_XSHM */
601 GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
602 ximage->ximage = XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
603 ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
604 ximagesrc->height, AllPlanes, ZPixmap);
611 if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {
613 GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
615 if (ximagesrc->cursor_image)
616 XFree (ximagesrc->cursor_image);
617 ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
618 if (ximagesrc->cursor_image != NULL) {
619 int cx, cy, i, j, count;
620 int startx, starty, iwidth, iheight;
621 gboolean clipped = FALSE;
622 gboolean cursor_in_image = TRUE;
624 cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
627 cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
630 count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
632 /* only get where cursor last was, if it is in our range */
633 if (ximagesrc->endx > ximagesrc->startx &&
634 ximagesrc->endy > ximagesrc->starty) {
636 if (cx + ximagesrc->cursor_image->width < ximagesrc->startx ||
637 cx > ximagesrc->endx) {
639 cursor_in_image = FALSE;
640 } else if (cy + ximagesrc->cursor_image->height < ximagesrc->starty ||
641 cy > ximagesrc->endy) {
643 cursor_in_image = FALSE;
645 /* find intersect region */
647 startx = (cx < ximagesrc->startx) ? ximagesrc->startx : cx;
648 starty = (cy < ximagesrc->starty) ? ximagesrc->starty : cy;
649 iwidth = (cx + ximagesrc->cursor_image->width < ximagesrc->endx) ?
650 cx + ximagesrc->cursor_image->width - startx :
651 ximagesrc->endx - startx;
652 iheight = (cy + ximagesrc->cursor_image->height < ximagesrc->endy) ?
653 cy + ximagesrc->cursor_image->height - starty :
654 ximagesrc->endy - starty;
660 iwidth = ximagesrc->cursor_image->width;
661 iheight = ximagesrc->cursor_image->height;
664 if (cursor_in_image) {
665 GST_DEBUG_OBJECT (ximagesrc, "Cursor is in image so trying to draw it");
666 for (i = 0; i < count; i++)
667 ximagesrc->cursor_image->pixels[i] =
668 GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
669 /* copy those pixels across */
671 j < starty + iheight && j < ximagesrc->starty + ximagesrc->height;
674 i < startx + iwidth && i < ximagesrc->startx + ximagesrc->width;
679 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
680 cy) * ximagesrc->cursor_image->width + (i - cx))]);
682 (guint8 *) & (ximage->ximage->data[((j -
683 ximagesrc->starty) * ximagesrc->width + (i -
684 ximagesrc->startx)) * (ximagesrc->xcontext->bpp /
687 composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
696 if (ximagesrc->have_xdamage && ximagesrc->use_damage) {
697 /* need to ref ximage to put in last_ximage */
698 gst_buffer_ref (GST_BUFFER (ximage));
699 if (ximagesrc->last_ximage) {
700 gst_buffer_unref (GST_BUFFER (ximagesrc->last_ximage));
702 ximagesrc->last_ximage = ximage;
703 GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
710 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
712 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
713 GstXImageSrcBuffer *image;
714 GstClockTime base_time;
715 GstClockTime next_capture_ts;
717 gint64 next_frame_no;
719 if (!gst_ximage_src_recalc (s)) {
720 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
721 ("Changing resolution at runtime is not yet supported."), (NULL));
722 return GST_FLOW_ERROR;
725 if (s->fps_n <= 0 || s->fps_d <= 0)
726 return GST_FLOW_NOT_NEGOTIATED; /* FPS must be > 0 */
728 /* Now, we might need to wait for the next multiple of the fps
729 * before capturing */
732 if (GST_ELEMENT_CLOCK (s) == NULL) {
733 GST_OBJECT_UNLOCK (s);
734 GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
735 ("Cannot operate without a clock"), (NULL));
736 return GST_FLOW_ERROR;
739 base_time = GST_ELEMENT_CAST (s)->base_time;
740 next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
741 next_capture_ts -= base_time;
743 /* Figure out which 'frame number' position we're at, based on the cur time
745 next_frame_no = gst_util_uint64_scale (next_capture_ts,
746 s->fps_n, GST_SECOND * s->fps_d);
747 if (next_frame_no == s->last_frame_no) {
751 /* Need to wait for the next frame */
754 /* Figure out what the next frame time is */
755 next_capture_ts = gst_util_uint64_scale (next_frame_no,
756 s->fps_d * GST_SECOND, s->fps_n);
758 id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
759 next_capture_ts + base_time);
762 /* release the object lock while waiting */
763 GST_OBJECT_UNLOCK (s);
765 GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
767 ret = gst_clock_id_wait (id, NULL);
770 gst_clock_id_unref (id);
772 if (ret == GST_CLOCK_UNSCHEDULED) {
773 /* Got woken up by the unlock function */
774 GST_OBJECT_UNLOCK (s);
775 return GST_FLOW_WRONG_STATE;
777 /* Duration is a complete 1/fps frame duration */
778 dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
780 GstClockTime next_frame_ts;
782 GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
783 G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
784 G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
785 next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
786 s->fps_d * GST_SECOND, s->fps_n);
787 /* Frame duration is from now until the next expected capture time */
788 dur = next_frame_ts - next_capture_ts;
790 s->last_frame_no = next_frame_no;
791 GST_OBJECT_UNLOCK (s);
793 image = gst_ximage_src_ximage_get (s);
795 return GST_FLOW_ERROR;
797 *buf = GST_BUFFER (image);
798 GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
799 GST_BUFFER_DURATION (*buf) = dur;
805 gst_ximage_src_set_property (GObject * object, guint prop_id,
806 const GValue * value, GParamSpec * pspec)
808 GstXImageSrc *src = GST_XIMAGE_SRC (object);
811 case PROP_DISPLAY_NAME:
813 g_free (src->display_name);
814 src->display_name = g_strdup (g_value_get_string (value));
816 case PROP_SCREEN_NUM:
817 src->screen_num = g_value_get_uint (value);
819 case PROP_SHOW_POINTER:
820 src->show_pointer = g_value_get_boolean (value);
822 case PROP_USE_DAMAGE:
823 src->use_damage = g_value_get_boolean (value);
826 src->startx = g_value_get_uint (value);
829 src->starty = g_value_get_uint (value);
832 src->endx = g_value_get_uint (value);
835 src->endy = g_value_get_uint (value);
843 gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
846 GstXImageSrc *src = GST_XIMAGE_SRC (object);
849 case PROP_DISPLAY_NAME:
851 g_value_set_string (value, DisplayString (src->xcontext->disp));
853 g_value_set_string (value, src->display_name);
856 case PROP_SCREEN_NUM:
857 g_value_set_uint (value, src->screen_num);
859 case PROP_SHOW_POINTER:
860 g_value_set_boolean (value, src->show_pointer);
862 case PROP_USE_DAMAGE:
863 g_value_set_boolean (value, src->use_damage);
866 g_value_set_uint (value, src->startx);
869 g_value_set_uint (value, src->starty);
872 g_value_set_uint (value, src->endx);
875 g_value_set_uint (value, src->endy);
878 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
884 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
886 g_mutex_lock (ximagesrc->pool_lock);
887 while (ximagesrc->buffer_pool != NULL) {
888 GstXImageSrcBuffer *ximage = ximagesrc->buffer_pool->data;
890 gst_ximage_buffer_free (ximage);
892 ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
893 ximagesrc->buffer_pool);
895 g_mutex_unlock (ximagesrc->pool_lock);
899 gst_ximage_src_base_init (gpointer g_class)
901 GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
903 gst_element_class_set_details (ec, &ximagesrc_details);
904 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t));
908 gst_ximage_src_dispose (GObject * object)
910 /* Drop references in the buffer_pool */
911 gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
913 G_OBJECT_CLASS (parent_class)->dispose (object);
917 gst_ximage_src_finalize (GObject * object)
919 GstXImageSrc *src = GST_XIMAGE_SRC (object);
922 ximageutil_xcontext_clear (src->xcontext);
924 g_mutex_free (src->pool_lock);
925 g_mutex_free (src->x_lock);
927 G_OBJECT_CLASS (parent_class)->finalize (object);
931 gst_ximage_src_get_caps (GstBaseSrc * bs)
933 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
934 GstXContext *xcontext;
935 gint x, y, width, height;
937 if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
939 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
942 if (!gst_ximage_src_recalc (s))
944 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
947 xcontext = s->xcontext;
950 width = xcontext->width;
951 height = xcontext->height;
952 if (s->endx > s->startx && s->endy > s->starty) {
953 /* this means user has put in values */
954 if (s->startx < xcontext->width && s->endx < xcontext->width &&
955 s->starty < xcontext->height && s->endy < xcontext->height) {
956 /* values are fine */
959 s->width = width = s->endx - s->startx;
960 s->height = height = s->endy - s->starty;
963 ("User put in co-ordinates overshooting the X resolution, setting to full screen");
970 GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
976 GST_DEBUG ("width = %d, height=%d", width, height);
977 return gst_caps_new_simple ("video/x-raw-rgb",
978 "bpp", G_TYPE_INT, xcontext->bpp,
979 "depth", G_TYPE_INT, xcontext->depth,
980 "endianness", G_TYPE_INT, xcontext->endianness,
981 "red_mask", G_TYPE_INT, xcontext->r_mask_output,
982 "green_mask", G_TYPE_INT, xcontext->g_mask_output,
983 "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
984 "width", G_TYPE_INT, width,
985 "height", G_TYPE_INT, height,
986 "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
987 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
992 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
994 GstXImageSrc *s = GST_XIMAGE_SRC (bs);
995 GstStructure *structure;
996 const GValue *new_fps;
998 /* If not yet opened, disallow setcaps until later */
1002 /* The only thing that can change is the framerate downstream wants */
1003 structure = gst_caps_get_structure (caps, 0);
1004 new_fps = gst_structure_get_value (structure, "framerate");
1008 /* Store this FPS for use when generating buffers */
1009 s->fps_n = gst_value_get_fraction_numerator (new_fps);
1010 s->fps_d = gst_value_get_fraction_denominator (new_fps);
1012 GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1018 gst_ximage_src_fixate (GstPad * pad, GstCaps * caps)
1021 GstStructure *structure;
1023 for (i = 0; i < gst_caps_get_size (caps); ++i) {
1024 structure = gst_caps_get_structure (caps, i);
1026 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1031 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1033 GObjectClass *gc = G_OBJECT_CLASS (klass);
1034 GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
1035 GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
1037 gc->set_property = gst_ximage_src_set_property;
1038 gc->get_property = gst_ximage_src_get_property;
1039 gc->dispose = gst_ximage_src_dispose;
1040 gc->finalize = gst_ximage_src_finalize;
1042 g_object_class_install_property (gc, PROP_DISPLAY_NAME,
1043 g_param_spec_string ("display-name", "Display", "X Display Name", NULL,
1044 G_PARAM_READWRITE));
1045 g_object_class_install_property (gc, PROP_SCREEN_NUM,
1046 g_param_spec_uint ("screen-num", "Screen number", "X Screen Number",
1047 0, G_MAXINT, 0, G_PARAM_READWRITE));
1048 g_object_class_install_property (gc, PROP_SHOW_POINTER,
1049 g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
1050 "Show mouse pointer (if XFixes extension enabled)", TRUE,
1051 G_PARAM_READWRITE));
1053 * GstXImageSrc:use-damage
1055 * Use XDamage (if the XDamage extension is enabled)
1059 g_object_class_install_property (gc, PROP_USE_DAMAGE,
1060 g_param_spec_boolean ("use-damage", "Use XDamage",
1061 "Use XDamage (if XDamage extension enabled)", TRUE,
1062 G_PARAM_READWRITE));
1064 * GstXImageSrc:startx
1066 * X coordinate of top left corner of area to be recorded
1067 * (0 for top left of screen)
1071 g_object_class_install_property (gc, PROP_STARTX,
1072 g_param_spec_uint ("startx", "Start X co-ordinate",
1073 "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
1074 0, G_MAXINT, 0, G_PARAM_READWRITE));
1076 * GstXImageSrc:starty
1078 * Y coordinate of top left corner of area to be recorded
1079 * (0 for top left of screen)
1083 g_object_class_install_property (gc, PROP_STARTY,
1084 g_param_spec_uint ("starty", "Start Y co-ordinate",
1085 "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
1086 0, G_MAXINT, 0, G_PARAM_READWRITE));
1090 * X coordinate of bottom right corner of area to be recorded
1091 * (0 for bottom right of screen)
1095 g_object_class_install_property (gc, PROP_ENDX,
1096 g_param_spec_uint ("endx", "End X",
1097 "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1098 0, G_MAXINT, 0, G_PARAM_READWRITE));
1102 * Y coordinate of bottom right corner of area to be recorded
1103 * (0 for bottom right of screen)
1107 g_object_class_install_property (gc, PROP_ENDY,
1108 g_param_spec_uint ("endy", "End Y",
1109 "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1110 0, G_MAXINT, 0, G_PARAM_READWRITE));
1112 parent_class = g_type_class_peek_parent (klass);
1114 push_class->create = gst_ximage_src_create;
1115 bc->get_caps = gst_ximage_src_get_caps;
1116 bc->set_caps = gst_ximage_src_set_caps;
1117 bc->start = gst_ximage_src_start;
1118 bc->stop = gst_ximage_src_stop;
1119 bc->unlock = gst_ximage_src_unlock;
1123 gst_ximage_src_init (GstXImageSrc * ximagesrc, GstXImageSrcClass * klass)
1125 gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1126 gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (ximagesrc),
1127 gst_ximage_src_fixate);
1129 ximagesrc->pool_lock = g_mutex_new ();
1130 ximagesrc->x_lock = g_mutex_new ();
1131 ximagesrc->show_pointer = TRUE;
1132 ximagesrc->use_damage = TRUE;
1133 ximagesrc->startx = 0;
1134 ximagesrc->starty = 0;
1135 ximagesrc->endx = 0;
1136 ximagesrc->endy = 0;
1140 plugin_init (GstPlugin * plugin)
1144 GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagewsrc", 0,
1145 "ximagesrc element debug");
1147 ret = gst_element_register (plugin, "ximagewsrc", GST_RANK_NONE,
1148 GST_TYPE_XIMAGE_SRC);
1153 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1156 "X11 video input plugin using standard Xlib calls, with added watermark and scaling",
1157 plugin_init, VERSION, GST_LICENSE, "GStreamer private plugins", "By JP");