GST_FLOW_WRONG_STATE -> GST_FLOW_FLUSHING
[platform/upstream/gstreamer.git] / sys / ximage / gstximagesrc.c
1 /* GStreamer
2  *
3  * Copyright (C) 2006 Zaheer Merali <zaheerabbas at merali dot org>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-ximagesrc
23  *
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.
29  *
30  * <refsect2>
31  * <title>Example pipelines</title>
32  * |[
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.
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 #include "gstximagesrc.h"
42
43 #include <string.h>
44 #include <stdlib.h>
45
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48
49 #include <gst/gst.h>
50 #include <gst/gst-i18n-plugin.h>
51
52 #include "gst/glib-compat-private.h"
53
54 GST_DEBUG_CATEGORY_STATIC (gst_debug_ximage_src);
55 #define GST_CAT_DEFAULT gst_debug_ximage_src
56
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 ]"));
63
64 enum
65 {
66   PROP_0,
67   PROP_DISPLAY_NAME,
68   PROP_SCREEN_NUM,
69   PROP_SHOW_POINTER,
70   PROP_USE_DAMAGE,
71   PROP_STARTX,
72   PROP_STARTY,
73   PROP_ENDX,
74   PROP_ENDY,
75   PROP_REMOTE,
76   PROP_XID,
77   PROP_XNAME,
78 };
79
80 #define gst_ximage_src_parent_class parent_class
81 G_DEFINE_TYPE (GstXImageSrc, gst_ximage_src, GST_TYPE_PUSH_SRC);
82
83 static void gst_ximage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
84 static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
85
86 /* Called when a buffer is returned from the pipeline */
87 static void
88 gst_ximage_src_return_buf (GstXImageSrc * ximagesrc, GstBuffer * ximage)
89 {
90   GstMetaXImage *meta = GST_META_XIMAGE_GET (ximage);
91
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);
100   } else {
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);
109   }
110 }
111
112 static Window
113 gst_ximage_src_find_window (GstXImageSrc * src, Window root, const char *name)
114 {
115   Window *children;
116   Window window = 0, root_return, parent_return;
117   unsigned int nchildren;
118   char *tmpname;
119   int n, status;
120
121   status = XFetchName (src->xcontext->disp, root, &tmpname);
122   if (status && !strcmp (name, tmpname))
123     return root;
124
125   status =
126       XQueryTree (src->xcontext->disp, root, &root_return, &parent_return,
127       &children, &nchildren);
128   if (!status || !children)
129     return (Window) 0;
130
131   for (n = 0; n < nchildren; ++n) {
132     window = gst_ximage_src_find_window (src, children[n], name);
133     if (window != 0)
134       break;
135   }
136
137   XFree (children);
138   return window;
139 }
140
141 static gboolean
142 gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
143 {
144   g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
145
146   if (s->xcontext != NULL)
147     return TRUE;
148
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"));
156     return FALSE;
157   }
158   s->width = s->xcontext->width;
159   s->height = s->xcontext->height;
160
161   s->xwindow = s->xcontext->root;
162   if (s->xid != 0 || s->xname) {
163     int status;
164     XWindowAttributes attrs;
165     Window window;
166
167     if (s->xid != 0) {
168       status = XGetWindowAttributes (s->xcontext->disp, s->xid, &attrs);
169       if (status) {
170         GST_DEBUG_OBJECT (s, "Found window XID %" G_GUINT64_FORMAT, s->xid);
171         s->xwindow = s->xid;
172         goto window_found;
173       } else {
174         GST_WARNING_OBJECT (s, "Failed to get window %" G_GUINT64_FORMAT
175             " attributes", s->xid);
176       }
177     }
178
179     if (s->xname) {
180       GST_DEBUG_OBJECT (s, "Looking for window %s", s->xname);
181       window = gst_ximage_src_find_window (s, s->xcontext->root, s->xname);
182       if (window != 0) {
183         GST_DEBUG_OBJECT (s, "Found window named %s, ", s->xname);
184         status = XGetWindowAttributes (s->xcontext->disp, window, &attrs);
185         if (status) {
186           s->xwindow = window;
187           goto window_found;
188         } else {
189           GST_WARNING_OBJECT (s, "Failed to get window attributes for "
190               "window named %s", s->xname);
191         }
192       }
193     }
194
195     GST_INFO_OBJECT (s, "Using root window");
196     goto use_root_window;
197
198   window_found:
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);
204   }
205 use_root_window:
206
207 #ifdef HAVE_XFIXES
208   /* check if xfixes supported */
209   {
210     int error_base;
211
212     if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
213             &error_base)) {
214       s->have_xfixes = TRUE;
215       GST_DEBUG_OBJECT (s, "X Server supports XFixes");
216     } else {
217
218       GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
219     }
220   }
221
222 #ifdef HAVE_XDAMAGE
223   /* check if xdamage is supported */
224   {
225     int error_base;
226     long evmask = NoEventMask;
227
228     s->have_xdamage = FALSE;
229     s->damage = None;
230     s->damage_copy_gc = None;
231     s->damage_region = None;
232
233     if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
234             &error_base)) {
235       s->damage =
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) {
240           XGCValues values;
241
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);
247
248           s->have_xdamage = TRUE;
249         } else {
250           XDamageDestroy (s->xcontext->disp, s->damage);
251           s->damage = None;
252         }
253       } else
254         GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
255     } else {
256       GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
257     }
258   }
259 #endif
260 #endif
261
262   g_mutex_unlock (&s->x_lock);
263
264   if (s->xcontext == NULL)
265     return FALSE;
266
267   return TRUE;
268 }
269
270 static gboolean
271 gst_ximage_src_start (GstBaseSrc * basesrc)
272 {
273   GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
274
275   s->last_frame_no = -1;
276 #ifdef HAVE_XDAMAGE
277   if (s->last_ximage)
278     gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
279   s->last_ximage = NULL;
280 #endif
281   return gst_ximage_src_open_display (s, s->display_name);
282 }
283
284 static gboolean
285 gst_ximage_src_stop (GstBaseSrc * basesrc)
286 {
287   GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
288
289 #ifdef HAVE_XDAMAGE
290   if (src->last_ximage)
291     gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
292   src->last_ximage = NULL;
293 #endif
294
295   gst_ximage_src_clear_bufpool (src);
296
297 #ifdef HAVE_XFIXES
298   if (src->cursor_image)
299     XFree (src->cursor_image);
300   src->cursor_image = NULL;
301 #endif
302
303   if (src->xcontext) {
304     g_mutex_lock (&src->x_lock);
305
306 #ifdef HAVE_XDAMAGE
307     if (src->damage_copy_gc != None) {
308       XFreeGC (src->xcontext->disp, src->damage_copy_gc);
309       src->damage_copy_gc = None;
310     }
311     if (src->damage_region != None) {
312       XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
313       src->damage_region = None;
314     }
315     if (src->damage != None) {
316       XDamageDestroy (src->xcontext->disp, src->damage);
317       src->damage = None;
318     }
319 #endif
320
321     ximageutil_xcontext_clear (src->xcontext);
322     src->xcontext = NULL;
323     g_mutex_unlock (&src->x_lock);
324   }
325
326   return TRUE;
327 }
328
329 static gboolean
330 gst_ximage_src_unlock (GstBaseSrc * basesrc)
331 {
332   GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
333
334   /* Awaken the create() func if it's waiting on the clock */
335   GST_OBJECT_LOCK (src);
336   if (src->clock_id) {
337     GST_DEBUG_OBJECT (src, "Waking up waiting clock");
338     gst_clock_id_unschedule (src->clock_id);
339   }
340   GST_OBJECT_UNLOCK (src);
341
342   return TRUE;
343 }
344
345 static gboolean
346 gst_ximage_src_recalc (GstXImageSrc * src)
347 {
348   if (!src->xcontext)
349     return FALSE;
350
351   /* Maybe later we can check the display hasn't changed size */
352   /* We could use XQueryPointer to get only the current window. */
353   return TRUE;
354 }
355
356 #ifdef HAVE_XFIXES
357 static void
358 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
359 {
360   guint8 r = src[2];
361   guint8 g = src[1];
362   guint8 b = src[0];
363   guint8 a = src[3];
364   guint8 dr, dg, db;
365   guint32 color;
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;
369
370   switch (xcontext->bpp) {
371     case 8:
372       color = *dest;
373       break;
374     case 16:
375       color = GUINT16_FROM_LE (*(guint16 *) (dest));
376       break;
377     case 32:
378       color = GUINT32_FROM_LE (*(guint32 *) (dest));
379       break;
380     default:
381       /* Should not reach here */
382       g_return_if_reached ();
383   }
384
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));
389       g_shift++);
390   for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
391
392   for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
393       r_shift_out++);
394   for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
395       g_shift_out++);
396   for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
397       b_shift_out++);
398
399
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);
403
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))
407
408   dr = (RGBXXX_R (color) * 255) / r_max;
409   dg = (RGBXXX_G (color) * 255) / g_max;
410   db = (RGBXXX_B (color) * 255) / b_max;
411
412   dr = (r * a + (0xff - a) * dr) / 0xff;
413   dg = (g * a + (0xff - a) * dg) / 0xff;
414   db = (b * a + (0xff - a) * db) / 0xff;
415
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);
419
420   switch (xcontext->bpp) {
421     case 8:
422       *dest = color;
423       break;
424     case 16:
425       *(guint16 *) (dest) = color;
426       break;
427     case 32:
428       *(guint32 *) (dest) = color;
429       break;
430     default:
431       g_warning ("bpp %d not supported\n", xcontext->bpp);
432   }
433 }
434 #endif
435
436 #ifdef HAVE_XDAMAGE
437 static void
438 copy_buffer (GstBuffer * dest, GstBuffer * src)
439 {
440   GstMapInfo map;
441
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);
445 }
446 #endif
447
448 /* Retrieve an XImageSrcBuffer, preferably from our
449  * pool of existing images and populate it from the window */
450 static GstBuffer *
451 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
452 {
453   GstBuffer *ximage = NULL;
454   GstMetaXImage *meta;
455
456   g_mutex_lock (&ximagesrc->pool_lock);
457   while (ximagesrc->buffer_pool != NULL) {
458     ximage = ximagesrc->buffer_pool->data;
459
460     meta = GST_META_XIMAGE_GET (ximage);
461
462     if ((meta->width != ximagesrc->width) ||
463         (meta->height != ximagesrc->height)) {
464       gst_ximage_buffer_free (ximage);
465     }
466
467     ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
468         ximagesrc->buffer_pool);
469   }
470   g_mutex_unlock (&ximagesrc->pool_lock);
471
472   if (ximage == NULL) {
473     GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
474         ximagesrc->width, ximagesrc->height);
475
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,
483               ximagesrc->height));
484       g_mutex_unlock (&ximagesrc->x_lock);
485       return NULL;
486     }
487
488     g_mutex_unlock (&ximagesrc->x_lock);
489   }
490
491   g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
492
493   meta = GST_META_XIMAGE_GET (ximage);
494
495 #ifdef HAVE_XDAMAGE
496   if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
497       ximagesrc->last_ximage != NULL) {
498     XEvent ev;
499
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;
503
504     GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
505
506     do {
507       XNextEvent (ximagesrc->xcontext->disp, &ev);
508
509       if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
510         XserverRegion parts;
511         XRectangle *rects;
512         int nrects;
513
514         parts = XFixesCreateRegion (ximagesrc->xcontext->disp, 0, 0);
515         XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
516             parts);
517         /* Now copy out all of the damaged rectangles. */
518         rects = XFixesFetchRegion (ximagesrc->xcontext->disp, parts, &nrects);
519         if (rects != NULL) {
520           int i;
521
522           if (!have_frame) {
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);
527             have_frame = TRUE;
528           }
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);
533
534             /* if we only want a small area, clip this damage region to
535              * area we want */
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) {
541                 /* trivial reject */
542               } else if (rects[i].y + rects[i].height - 1 < ximagesrc->starty ||
543                   rects[i].y > ximagesrc->endy) {
544                 /* trivial reject */
545               } else {
546                 /* find intersect region */
547                 int startx, starty, width, height;
548
549                 startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
550                     rects[i].x;
551                 starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
552                     rects[i].y;
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 -
558                     starty + 1;
559
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);
567               }
568             } else {
569
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);
573
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);
578             }
579           }
580           free (rects);
581         }
582       }
583     } while (XPending (ximagesrc->xcontext->disp));
584     if (!have_frame) {
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);
589     }
590 #ifdef HAVE_XFIXES
591     /* re-get area where last mouse pointer was  but only if in our clipping
592      * bounds */
593     if (ximagesrc->cursor_image) {
594       gint x, y, width, height;
595
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;
600
601       /* bounds checking */
602       if (x < 0)
603         x = 0;
604       if (y < 0)
605         y = 0;
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;
610       g_assert (x >= 0);
611       g_assert (y >= 0);
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) {
619         /* check bounds */
620         if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
621           /* trivial reject */
622         } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
623           /* trivial reject */
624         } else {
625           /* find intersect region */
626           int startx, starty, iwidth, iheight;
627
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);
639         }
640       } else {
641
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);
645       }
646     }
647 #endif
648
649
650   } else {
651 #endif
652
653 #ifdef HAVE_XSHM
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);
658
659     } else
660 #endif /* HAVE_XSHM */
661     {
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);
667       } else {
668         meta->ximage =
669             XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
670             ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
671             ximagesrc->height, AllPlanes, ZPixmap);
672       }
673     }
674 #ifdef HAVE_XDAMAGE
675   }
676 #endif
677
678 #ifdef HAVE_XFIXES
679   if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {
680
681     GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
682     /* get 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;
690
691       cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
692       if (cx < 0)
693         cx = 0;
694       cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
695       if (cy < 0)
696         cy = 0;
697       count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
698
699       /* only get where cursor last was, if it is in our range */
700       if (ximagesrc->endx > ximagesrc->startx &&
701           ximagesrc->endy > ximagesrc->starty) {
702         /* check bounds */
703         if (cx + ximagesrc->cursor_image->width < ximagesrc->startx ||
704             cx > ximagesrc->endx) {
705           /* trivial reject */
706           cursor_in_image = FALSE;
707         } else if (cy + ximagesrc->cursor_image->height < ximagesrc->starty ||
708             cy > ximagesrc->endy) {
709           /* trivial reject */
710           cursor_in_image = FALSE;
711         } else {
712           /* find intersect region */
713
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;
722         }
723       } else {
724         startx = cx;
725         starty = cy;
726         iwidth = ximagesrc->cursor_image->width;
727         iheight = ximagesrc->cursor_image->height;
728       }
729
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 */
736         for (j = starty;
737             j < starty + iheight && j < ximagesrc->starty + ximagesrc->height;
738             j++) {
739           for (i = startx;
740               i < startx + iwidth && i < ximagesrc->startx + ximagesrc->width;
741               i++) {
742             guint8 *src, *dest;
743
744             src =
745                 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
746                             cy) * ximagesrc->cursor_image->width + (i - cx))]);
747             dest =
748                 (guint8 *) & (meta->ximage->data[((j -
749                             ximagesrc->starty) * ximagesrc->width + (i -
750                             ximagesrc->startx)) * (ximagesrc->xcontext->bpp /
751                         8)]);
752
753             composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
754                 (guint8 *) src);
755           }
756         }
757       }
758     }
759   }
760 #endif
761 #ifdef HAVE_XDAMAGE
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);
767     }
768     ximagesrc->last_ximage = ximage;
769     GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
770   }
771 #endif
772   return ximage;
773 }
774
775 static GstFlowReturn
776 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
777 {
778   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
779   GstBuffer *image;
780   GstClockTime base_time;
781   GstClockTime next_capture_ts;
782   GstClockTime dur;
783   gint64 next_frame_no;
784
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;
789   }
790
791   if (s->fps_n <= 0 || s->fps_d <= 0)
792     return GST_FLOW_NOT_NEGOTIATED;     /* FPS must be > 0 */
793
794   /* Now, we might need to wait for the next multiple of the fps
795    * before capturing */
796
797   GST_OBJECT_LOCK (s);
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;
803   }
804
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;
808
809   /* Figure out which 'frame number' position we're at, based on the cur time
810    * and frame rate */
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) {
814     GstClockID id;
815     GstClockReturn ret;
816
817     /* Need to wait for the next frame */
818     next_frame_no += 1;
819
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);
823
824     id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
825         next_capture_ts + base_time);
826     s->clock_id = id;
827
828     /* release the object lock while waiting */
829     GST_OBJECT_UNLOCK (s);
830
831     GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
832         next_capture_ts);
833     ret = gst_clock_id_wait (id, NULL);
834     GST_OBJECT_LOCK (s);
835
836     gst_clock_id_unref (id);
837     s->clock_id = NULL;
838     if (ret == GST_CLOCK_UNSCHEDULED) {
839       /* Got woken up by the unlock function */
840       GST_OBJECT_UNLOCK (s);
841       return GST_FLOW_FLUSHING;
842     }
843     /* Duration is a complete 1/fps frame duration */
844     dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
845   } else {
846     GstClockTime next_frame_ts;
847
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;
855   }
856   s->last_frame_no = next_frame_no;
857   GST_OBJECT_UNLOCK (s);
858
859   image = gst_ximage_src_ximage_get (s);
860   if (!image)
861     return GST_FLOW_ERROR;
862
863   *buf = image;
864   GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
865   GST_BUFFER_DURATION (*buf) = dur;
866
867   return GST_FLOW_OK;
868 }
869
870 static void
871 gst_ximage_src_set_property (GObject * object, guint prop_id,
872     const GValue * value, GParamSpec * pspec)
873 {
874   GstXImageSrc *src = GST_XIMAGE_SRC (object);
875
876   switch (prop_id) {
877     case PROP_DISPLAY_NAME:
878
879       g_free (src->display_name);
880       src->display_name = g_strdup (g_value_get_string (value));
881       break;
882     case PROP_SCREEN_NUM:
883       src->screen_num = g_value_get_uint (value);
884       break;
885     case PROP_SHOW_POINTER:
886       src->show_pointer = g_value_get_boolean (value);
887       break;
888     case PROP_USE_DAMAGE:
889       src->use_damage = g_value_get_boolean (value);
890       break;
891     case PROP_STARTX:
892       src->startx = g_value_get_uint (value);
893       break;
894     case PROP_STARTY:
895       src->starty = g_value_get_uint (value);
896       break;
897     case PROP_ENDX:
898       src->endx = g_value_get_uint (value);
899       break;
900     case PROP_ENDY:
901       src->endy = g_value_get_uint (value);
902       break;
903     case PROP_REMOTE:
904       src->remote = g_value_get_boolean (value);
905       break;
906     case PROP_XID:
907       if (src->xcontext != NULL) {
908         g_warning ("ximagesrc window ID must be set before opening display");
909         break;
910       }
911       src->xid = g_value_get_uint64 (value);
912       break;
913     case PROP_XNAME:
914       if (src->xcontext != NULL) {
915         g_warning ("ximagesrc window name must be set before opening display");
916         break;
917       }
918       g_free (src->xname);
919       src->xname = g_strdup (g_value_get_string (value));
920       break;
921     default:
922       break;
923   }
924 }
925
926 static void
927 gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
928     GParamSpec * pspec)
929 {
930   GstXImageSrc *src = GST_XIMAGE_SRC (object);
931
932   switch (prop_id) {
933     case PROP_DISPLAY_NAME:
934       if (src->xcontext)
935         g_value_set_string (value, DisplayString (src->xcontext->disp));
936       else
937         g_value_set_string (value, src->display_name);
938
939       break;
940     case PROP_SCREEN_NUM:
941       g_value_set_uint (value, src->screen_num);
942       break;
943     case PROP_SHOW_POINTER:
944       g_value_set_boolean (value, src->show_pointer);
945       break;
946     case PROP_USE_DAMAGE:
947       g_value_set_boolean (value, src->use_damage);
948       break;
949     case PROP_STARTX:
950       g_value_set_uint (value, src->startx);
951       break;
952     case PROP_STARTY:
953       g_value_set_uint (value, src->starty);
954       break;
955     case PROP_ENDX:
956       g_value_set_uint (value, src->endx);
957       break;
958     case PROP_ENDY:
959       g_value_set_uint (value, src->endy);
960       break;
961     case PROP_REMOTE:
962       g_value_set_boolean (value, src->remote);
963       break;
964     case PROP_XID:
965       g_value_set_uint64 (value, src->xid);
966       break;
967     case PROP_XNAME:
968       g_value_set_string (value, src->xname);
969       break;
970     default:
971       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
972       break;
973   }
974 }
975
976 static void
977 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
978 {
979   g_mutex_lock (&ximagesrc->pool_lock);
980   while (ximagesrc->buffer_pool != NULL) {
981     GstBuffer *ximage = ximagesrc->buffer_pool->data;
982
983     gst_ximage_buffer_free (ximage);
984
985     ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
986         ximagesrc->buffer_pool);
987   }
988   g_mutex_unlock (&ximagesrc->pool_lock);
989 }
990
991 static void
992 gst_ximage_src_dispose (GObject * object)
993 {
994   /* Drop references in the buffer_pool */
995   gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
996
997   G_OBJECT_CLASS (parent_class)->dispose (object);
998 }
999
1000 static void
1001 gst_ximage_src_finalize (GObject * object)
1002 {
1003   GstXImageSrc *src = GST_XIMAGE_SRC (object);
1004
1005   if (src->xcontext)
1006     ximageutil_xcontext_clear (src->xcontext);
1007
1008   g_free (src->xname);
1009   g_mutex_clear (&src->pool_lock);
1010   g_mutex_clear (&src->x_lock);
1011
1012   G_OBJECT_CLASS (parent_class)->finalize (object);
1013 }
1014
1015 static GstCaps *
1016 gst_ximage_src_get_caps (GstBaseSrc * bs, GstCaps * filter)
1017 {
1018   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1019   GstXContext *xcontext;
1020   gint width, height;
1021
1022   if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
1023     return
1024         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1025             (s)->srcpad));
1026
1027   if (!gst_ximage_src_recalc (s))
1028     return
1029         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1030             (s)->srcpad));
1031
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);
1038     if (status) {
1039       width = attrs.width;
1040       height = attrs.height;
1041     }
1042   }
1043
1044   /* property comments say 0 means right/bottom, means we can't capture
1045      the top left pixel alone */
1046   if (s->endx == 0)
1047     s->endx = width - 1;
1048   if (s->endy == 0)
1049     s->endy = height - 1;
1050
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;
1059     } else {
1060       GST_WARNING
1061           ("User put in co-ordinates overshooting the X resolution, setting to full screen");
1062       s->startx = 0;
1063       s->starty = 0;
1064       s->endx = width - 1;
1065       s->endy = height - 1;
1066     }
1067   } else {
1068     GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
1069     s->startx = 0;
1070     s->starty = 0;
1071     s->endx = width - 1;
1072     s->endy = height - 1;
1073   }
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,
1086       NULL);
1087 }
1088
1089 static gboolean
1090 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
1091 {
1092   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1093   GstStructure *structure;
1094   const GValue *new_fps;
1095
1096   /* If not yet opened, disallow setcaps until later */
1097   if (!s->xcontext)
1098     return FALSE;
1099
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");
1103   if (!new_fps)
1104     return FALSE;
1105
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);
1109
1110   GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1111
1112   return TRUE;
1113 }
1114
1115 static void
1116 gst_ximage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
1117 {
1118   gint i;
1119   GstStructure *structure;
1120
1121   for (i = 0; i < gst_caps_get_size (caps); ++i) {
1122     structure = gst_caps_get_structure (caps, i);
1123
1124     gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1125   }
1126   GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
1127 }
1128
1129 static void
1130 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1131 {
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);
1136
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;
1141
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));
1152   /**
1153    * GstXImageSrc:use-damage
1154    *
1155    * Use XDamage (if the XDamage extension is enabled)
1156    *
1157    * Since: 0.10.4
1158    **/
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));
1163   /**
1164    * GstXImageSrc:startx
1165    *
1166    * X coordinate of top left corner of area to be recorded
1167    * (0 for top left of screen)
1168    *
1169    * Since: 0.10.4
1170    **/
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));
1175   /**
1176    * GstXImageSrc:starty
1177    *
1178    * Y coordinate of top left corner of area to be recorded
1179    * (0 for top left of screen)
1180    *
1181    * Since: 0.10.4
1182    **/
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));
1187   /**
1188    * GstXImageSrc:endx
1189    *
1190    * X coordinate of bottom right corner of area to be recorded
1191    * (0 for bottom right of screen)
1192    *
1193    * Since: 0.10.4
1194    **/
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));
1199   /**
1200    * GstXImageSrc:endy
1201    *
1202    * Y coordinate of bottom right corner of area to be recorded
1203    * (0 for bottom right of screen)
1204    *
1205    * Since: 0.10.4
1206    **/
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));
1211
1212   /**
1213    * GstXImageSrc:remote
1214    *
1215    * Whether the X display is remote. The element will try to use alternate calls
1216    * known to work better with remote displays.
1217    *
1218    * Since: 0.10.26
1219    **/
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));
1224   /**
1225    * GstXImageSrc:xid
1226    *
1227    * The XID of the window to capture. 0 for the root window (default).
1228    *
1229    * Since: 0.10.31
1230    **/
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));
1235   /**
1236    * GstXImageSrc:xname
1237    *
1238    * The name of the window to capture, if any.
1239    *
1240    * Since: 0.10.31
1241    **/
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));
1246
1247   gst_element_class_set_details_simple (ec, "Ximage video source",
1248       "Source/Video",
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));
1254
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;
1262 }
1263
1264 static void
1265 gst_ximage_src_init (GstXImageSrc * ximagesrc)
1266 {
1267   gst_base_src_set_format (GST_BASE_SRC (ximagesrc), GST_FORMAT_TIME);
1268   gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1269
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;
1279 }
1280
1281 static gboolean
1282 plugin_init (GstPlugin * plugin)
1283 {
1284   gboolean ret;
1285
1286   GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
1287       "ximagesrc element debug");
1288
1289   ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
1290       GST_TYPE_XIMAGE_SRC);
1291
1292   return ret;
1293 }
1294
1295 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1296     GST_VERSION_MINOR,
1297     "ximagesrc",
1298     "X11 video input plugin using standard Xlib calls",
1299     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);