various: fix pad template leaks
[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 GST_DEBUG_CATEGORY_STATIC (gst_debug_ximage_src);
53 #define GST_CAT_DEFAULT gst_debug_ximage_src
54
55 static GstStaticPadTemplate t =
56 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("video/x-raw-rgb, "
58         "framerate = (fraction) [ 0, MAX ], "
59         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
60         "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
61
62 enum
63 {
64   PROP_0,
65   PROP_DISPLAY_NAME,
66   PROP_SCREEN_NUM,
67   PROP_SHOW_POINTER,
68   PROP_USE_DAMAGE,
69   PROP_STARTX,
70   PROP_STARTY,
71   PROP_ENDX,
72   PROP_ENDY,
73   PROP_REMOTE,
74   PROP_XID,
75   PROP_XNAME,
76 };
77
78 GST_BOILERPLATE (GstXImageSrc, gst_ximage_src, GstPushSrc, GST_TYPE_PUSH_SRC);
79
80 static void gst_ximage_src_fixate (GstPad * pad, GstCaps * caps);
81 static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
82
83 /* Called when a buffer is returned from the pipeline */
84 static void
85 gst_ximage_src_return_buf (GstXImageSrc * ximagesrc,
86     GstXImageSrcBuffer * ximage)
87 {
88   /* If our geometry changed we can't reuse that image. */
89   if ((ximage->width != ximagesrc->width) ||
90       (ximage->height != ximagesrc->height)) {
91     GST_DEBUG_OBJECT (ximagesrc,
92         "destroy image %p as its size changed %dx%d vs current %dx%d",
93         ximage, ximage->width, ximage->height,
94         ximagesrc->width, ximagesrc->height);
95     g_mutex_lock (ximagesrc->x_lock);
96     gst_ximageutil_ximage_destroy (ximagesrc->xcontext, ximage);
97     g_mutex_unlock (ximagesrc->x_lock);
98   } else {
99     /* In that case we can reuse the image and add it to our image pool. */
100     GST_LOG_OBJECT (ximagesrc, "recycling image %p in pool", ximage);
101     /* need to increment the refcount again to recycle */
102     gst_buffer_ref (GST_BUFFER (ximage));
103     g_mutex_lock (ximagesrc->pool_lock);
104     GST_BUFFER_FLAGS (GST_BUFFER (ximage)) = 0; /* clear out any flags from the previous use */
105     ximagesrc->buffer_pool = g_slist_prepend (ximagesrc->buffer_pool, ximage);
106     g_mutex_unlock (ximagesrc->pool_lock);
107   }
108 }
109
110 static Window
111 gst_ximage_src_find_window (GstXImageSrc * src, Window root, const char *name)
112 {
113   Window *children;
114   Window window = 0, root_return, parent_return;
115   unsigned int nchildren;
116   char *tmpname;
117   int n, status;
118
119   status = XFetchName (src->xcontext->disp, root, &tmpname);
120   if (status && !strcmp (name, tmpname))
121     return root;
122
123   status =
124       XQueryTree (src->xcontext->disp, root, &root_return, &parent_return,
125       &children, &nchildren);
126   if (!status || !children)
127     return (Window) 0;
128
129   for (n = 0; n < nchildren; ++n) {
130     window = gst_ximage_src_find_window (src, children[n], name);
131     if (window != 0)
132       break;
133   }
134
135   XFree (children);
136   return window;
137 }
138
139 static gboolean
140 gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
141 {
142   g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
143
144   if (s->xcontext != NULL)
145     return TRUE;
146
147   g_mutex_lock (s->x_lock);
148   s->xcontext = ximageutil_xcontext_get (GST_ELEMENT (s), name);
149   if (s->xcontext == NULL) {
150     g_mutex_unlock (s->x_lock);
151     GST_ELEMENT_ERROR (s, RESOURCE, OPEN_READ,
152         ("Could not open X display for reading"),
153         ("NULL returned from getting xcontext"));
154     return FALSE;
155   }
156   s->width = s->xcontext->width;
157   s->height = s->xcontext->height;
158
159   s->xwindow = s->xcontext->root;
160   if (s->xid != 0 || s->xname) {
161     int status;
162     XWindowAttributes attrs;
163     Window window;
164
165     if (s->xid != 0) {
166       status = XGetWindowAttributes (s->xcontext->disp, s->xid, &attrs);
167       if (status) {
168         GST_DEBUG_OBJECT (s, "Found window XID %" G_GUINT64_FORMAT, s->xid);
169         s->xwindow = s->xid;
170         goto window_found;
171       } else {
172         GST_WARNING_OBJECT (s, "Failed to get window %" G_GUINT64_FORMAT
173             " attributes", s->xid);
174       }
175     }
176
177     if (s->xname) {
178       GST_DEBUG_OBJECT (s, "Looking for window %s", s->xname);
179       window = gst_ximage_src_find_window (s, s->xcontext->root, s->xname);
180       if (window != 0) {
181         GST_DEBUG_OBJECT (s, "Found window named %s, ", s->xname);
182         status = XGetWindowAttributes (s->xcontext->disp, window, &attrs);
183         if (status) {
184           s->xwindow = window;
185           goto window_found;
186         } else {
187           GST_WARNING_OBJECT (s, "Failed to get window attributes for "
188               "window named %s", s->xname);
189         }
190       }
191     }
192
193     GST_INFO_OBJECT (s, "Using root window");
194     goto use_root_window;
195
196   window_found:
197     g_assert (s->xwindow != 0);
198     s->width = attrs.width;
199     s->height = attrs.height;
200     GST_INFO_OBJECT (s, "Using default window size of %dx%d",
201         s->width, s->height);
202   }
203 use_root_window:
204
205 #ifdef HAVE_XFIXES
206   /* check if xfixes supported */
207   {
208     int error_base;
209
210     if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
211             &error_base)) {
212       s->have_xfixes = TRUE;
213       GST_DEBUG_OBJECT (s, "X Server supports XFixes");
214     } else {
215
216       GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
217     }
218   }
219
220 #ifdef HAVE_XDAMAGE
221   /* check if xdamage is supported */
222   {
223     int error_base;
224     long evmask = NoEventMask;
225
226     s->have_xdamage = FALSE;
227     s->damage = None;
228     s->damage_copy_gc = None;
229     s->damage_region = None;
230
231     if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
232             &error_base)) {
233       s->damage =
234           XDamageCreate (s->xcontext->disp, s->xwindow, XDamageReportNonEmpty);
235       if (s->damage != None) {
236         s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
237         if (s->damage_region != None) {
238           XGCValues values;
239
240           GST_DEBUG_OBJECT (s, "Using XDamage extension");
241           values.subwindow_mode = IncludeInferiors;
242           s->damage_copy_gc = XCreateGC (s->xcontext->disp,
243               s->xwindow, GCSubwindowMode, &values);
244           XSelectInput (s->xcontext->disp, s->xwindow, evmask);
245
246           s->have_xdamage = TRUE;
247         } else {
248           XDamageDestroy (s->xcontext->disp, s->damage);
249           s->damage = None;
250         }
251       } else
252         GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
253     } else {
254       GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
255     }
256   }
257 #endif
258 #endif
259
260   g_mutex_unlock (s->x_lock);
261
262   if (s->xcontext == NULL)
263     return FALSE;
264
265   return TRUE;
266 }
267
268 static gboolean
269 gst_ximage_src_start (GstBaseSrc * basesrc)
270 {
271   GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
272
273   s->last_frame_no = -1;
274 #ifdef HAVE_XDAMAGE
275   if (s->last_ximage)
276     gst_buffer_unref (GST_BUFFER_CAST (s->last_ximage));
277   s->last_ximage = NULL;
278 #endif
279   return gst_ximage_src_open_display (s, s->display_name);
280 }
281
282 static gboolean
283 gst_ximage_src_stop (GstBaseSrc * basesrc)
284 {
285   GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
286
287 #ifdef HAVE_XDAMAGE
288   if (src->last_ximage)
289     gst_buffer_unref (GST_BUFFER_CAST (src->last_ximage));
290   src->last_ximage = NULL;
291 #endif
292
293   gst_ximage_src_clear_bufpool (src);
294
295 #ifdef HAVE_XFIXES
296   if (src->cursor_image)
297     XFree (src->cursor_image);
298   src->cursor_image = NULL;
299 #endif
300
301   if (src->xcontext) {
302     g_mutex_lock (src->x_lock);
303
304 #ifdef HAVE_XDAMAGE
305     if (src->damage_copy_gc != None) {
306       XFreeGC (src->xcontext->disp, src->damage_copy_gc);
307       src->damage_copy_gc = None;
308     }
309     if (src->damage_region != None) {
310       XFixesDestroyRegion (src->xcontext->disp, src->damage_region);
311       src->damage_region = None;
312     }
313     if (src->damage != None) {
314       XDamageDestroy (src->xcontext->disp, src->damage);
315       src->damage = None;
316     }
317 #endif
318
319     ximageutil_xcontext_clear (src->xcontext);
320     src->xcontext = NULL;
321     g_mutex_unlock (src->x_lock);
322   }
323
324   return TRUE;
325 }
326
327 static gboolean
328 gst_ximage_src_unlock (GstBaseSrc * basesrc)
329 {
330   GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
331
332   /* Awaken the create() func if it's waiting on the clock */
333   GST_OBJECT_LOCK (src);
334   if (src->clock_id) {
335     GST_DEBUG_OBJECT (src, "Waking up waiting clock");
336     gst_clock_id_unschedule (src->clock_id);
337   }
338   GST_OBJECT_UNLOCK (src);
339
340   return TRUE;
341 }
342
343 static gboolean
344 gst_ximage_src_recalc (GstXImageSrc * src)
345 {
346   if (!src->xcontext)
347     return FALSE;
348
349   /* Maybe later we can check the display hasn't changed size */
350   /* We could use XQueryPointer to get only the current window. */
351   return TRUE;
352 }
353
354 #ifdef HAVE_XFIXES
355 static void
356 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
357 {
358   guint8 r = src[2];
359   guint8 g = src[1];
360   guint8 b = src[0];
361   guint8 a = src[3];
362   guint8 dr, dg, db;
363   guint32 color;
364   gint r_shift, r_max, r_shift_out;
365   gint g_shift, g_max, g_shift_out;
366   gint b_shift, b_max, b_shift_out;
367
368   switch (xcontext->bpp) {
369     case 8:
370       color = *dest;
371       break;
372     case 16:
373       color = GUINT16_FROM_LE (*(guint16 *) (dest));
374       break;
375     case 32:
376       color = GUINT32_FROM_LE (*(guint32 *) (dest));
377       break;
378     default:
379       /* Should not reach here */
380       g_return_if_reached ();
381   }
382
383   /* possible optimisation:
384    * move the code that finds shift and max in the _link function */
385   for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
386   for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
387       g_shift++);
388   for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
389
390   for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
391       r_shift_out++);
392   for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
393       g_shift_out++);
394   for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
395       b_shift_out++);
396
397
398   r_max = (xcontext->visual->red_mask >> r_shift);
399   b_max = (xcontext->visual->blue_mask >> b_shift);
400   g_max = (xcontext->visual->green_mask >> g_shift);
401
402 #define RGBXXX_R(x)  (((x)>>r_shift) & (r_max))
403 #define RGBXXX_G(x)  (((x)>>g_shift) & (g_max))
404 #define RGBXXX_B(x)  (((x)>>b_shift) & (b_max))
405
406   dr = (RGBXXX_R (color) * 255) / r_max;
407   dg = (RGBXXX_G (color) * 255) / g_max;
408   db = (RGBXXX_B (color) * 255) / b_max;
409
410   dr = (r * a + (0xff - a) * dr) / 0xff;
411   dg = (g * a + (0xff - a) * dg) / 0xff;
412   db = (b * a + (0xff - a) * db) / 0xff;
413
414   color = (((dr * r_max) / 255) << r_shift_out) +
415       (((dg * g_max) / 255) << g_shift_out) +
416       (((db * b_max) / 255) << b_shift_out);
417
418   switch (xcontext->bpp) {
419     case 8:
420       *dest = color;
421       break;
422     case 16:
423       *(guint16 *) (dest) = color;
424       break;
425     case 32:
426       *(guint32 *) (dest) = color;
427       break;
428     default:
429       g_warning ("bpp %d not supported\n", xcontext->bpp);
430   }
431 }
432 #endif
433
434 /* Retrieve an XImageSrcBuffer, preferably from our
435  * pool of existing images and populate it from the window */
436 static GstXImageSrcBuffer *
437 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
438 {
439   GstXImageSrcBuffer *ximage = NULL;
440
441   g_mutex_lock (ximagesrc->pool_lock);
442   while (ximagesrc->buffer_pool != NULL) {
443     ximage = ximagesrc->buffer_pool->data;
444
445     if ((ximage->width != ximagesrc->width) ||
446         (ximage->height != ximagesrc->height)) {
447       gst_ximage_buffer_free (ximage);
448     }
449
450     ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
451         ximagesrc->buffer_pool);
452   }
453   g_mutex_unlock (ximagesrc->pool_lock);
454
455   if (ximage == NULL) {
456     GstXContext *xcontext;
457     GstCaps *caps = NULL;
458
459     GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
460         ximagesrc->width, ximagesrc->height);
461
462     g_mutex_lock (ximagesrc->x_lock);
463     ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
464         GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
465         (BufferReturnFunc) (gst_ximage_src_return_buf));
466     if (ximage == NULL) {
467       GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
468           ("could not create a %dx%d ximage", ximagesrc->width,
469               ximagesrc->height));
470       g_mutex_unlock (ximagesrc->x_lock);
471       return NULL;
472     }
473
474     xcontext = ximagesrc->xcontext;
475
476
477     caps = gst_caps_new_simple ("video/x-raw-rgb",
478         "bpp", G_TYPE_INT, xcontext->bpp,
479         "depth", G_TYPE_INT, xcontext->depth,
480         "endianness", G_TYPE_INT, xcontext->endianness,
481         "red_mask", G_TYPE_INT, xcontext->r_mask_output,
482         "green_mask", G_TYPE_INT, xcontext->g_mask_output,
483         "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
484         "width", G_TYPE_INT, ximagesrc->width,
485         "height", G_TYPE_INT, ximagesrc->height,
486         "framerate", GST_TYPE_FRACTION, ximagesrc->fps_n, ximagesrc->fps_d,
487         "pixel-aspect-ratio", GST_TYPE_FRACTION,
488         gst_value_get_fraction_numerator (xcontext->par),
489         gst_value_get_fraction_denominator (xcontext->par), NULL);
490
491     gst_buffer_set_caps (GST_BUFFER (ximage), caps);
492     g_mutex_unlock (ximagesrc->x_lock);
493
494     gst_caps_unref (caps);
495   }
496
497   g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
498 #ifdef HAVE_XDAMAGE
499   if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
500       ximagesrc->last_ximage != NULL) {
501     XEvent ev;
502
503     /* have_frame is TRUE when either the entire screen has been
504      * grabbed or when the last image has been copied */
505     gboolean have_frame = FALSE;
506
507     GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
508
509     do {
510       XNextEvent (ximagesrc->xcontext->disp, &ev);
511
512       if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
513         XserverRegion parts;
514         XRectangle *rects;
515         int nrects;
516
517         parts = XFixesCreateRegion (ximagesrc->xcontext->disp, 0, 0);
518         XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
519             parts);
520         /* Now copy out all of the damaged rectangles. */
521         rects = XFixesFetchRegion (ximagesrc->xcontext->disp, parts, &nrects);
522         if (rects != NULL) {
523           int i;
524
525           if (!have_frame) {
526             GST_LOG_OBJECT (ximagesrc,
527                 "Copying from last frame ximage->size: %d",
528                 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
529             memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
530                 GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
531                 GST_BUFFER_SIZE (GST_BUFFER (ximage)));
532             have_frame = TRUE;
533           }
534           for (i = 0; i < nrects; i++) {
535             GST_LOG_OBJECT (ximagesrc,
536                 "Damaged sub-region @ %d,%d size %dx%d reported",
537                 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
538
539             /* if we only want a small area, clip this damage region to
540              * area we want */
541             if (ximagesrc->endx > ximagesrc->startx &&
542                 ximagesrc->endy > ximagesrc->starty) {
543               /* see if damage area intersects */
544               if (rects[i].x + rects[i].width - 1 < ximagesrc->startx ||
545                   rects[i].x > ximagesrc->endx) {
546                 /* trivial reject */
547               } else if (rects[i].y + rects[i].height - 1 < ximagesrc->starty ||
548                   rects[i].y > ximagesrc->endy) {
549                 /* trivial reject */
550               } else {
551                 /* find intersect region */
552                 int startx, starty, width, height;
553
554                 startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
555                     rects[i].x;
556                 starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
557                     rects[i].y;
558                 width = (rects[i].x + rects[i].width - 1 < ximagesrc->endx) ?
559                     rects[i].x + rects[i].width - startx :
560                     ximagesrc->endx - startx + 1;
561                 height = (rects[i].y + rects[i].height - 1 < ximagesrc->endy) ?
562                     rects[i].y + rects[i].height - starty : ximagesrc->endy -
563                     starty + 1;
564
565                 GST_LOG_OBJECT (ximagesrc,
566                     "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
567                     startx, starty, width, height);
568                 XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
569                     startx, starty, width, height, AllPlanes, ZPixmap,
570                     ximage->ximage, startx - ximagesrc->startx,
571                     starty - ximagesrc->starty);
572               }
573             } else {
574
575               GST_LOG_OBJECT (ximagesrc,
576                   "Retrieving damaged sub-region @ %d,%d size %dx%d",
577                   rects[i].x, rects[i].y, rects[i].width, rects[i].height);
578
579               XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
580                   rects[i].x, rects[i].y,
581                   rects[i].width, rects[i].height,
582                   AllPlanes, ZPixmap, ximage->ximage, rects[i].x, rects[i].y);
583             }
584           }
585           free (rects);
586         }
587       }
588     } while (XPending (ximagesrc->xcontext->disp));
589     if (!have_frame) {
590       GST_LOG_OBJECT (ximagesrc,
591           "Copying from last frame ximage->size: %d",
592           GST_BUFFER_SIZE (GST_BUFFER (ximage)));
593       memcpy (GST_BUFFER_DATA (GST_BUFFER (ximage)),
594           GST_BUFFER_DATA (GST_BUFFER (ximagesrc->last_ximage)),
595           GST_BUFFER_SIZE (GST_BUFFER (ximage)));
596     }
597 #ifdef HAVE_XFIXES
598     /* re-get area where last mouse pointer was  but only if in our clipping
599      * bounds */
600     if (ximagesrc->cursor_image) {
601       gint x, y, width, height;
602
603       x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
604       y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
605       width = ximagesrc->cursor_image->width;
606       height = ximagesrc->cursor_image->height;
607
608       /* bounds checking */
609       if (x < 0)
610         x = 0;
611       if (y < 0)
612         y = 0;
613       if (x + width > ximagesrc->xcontext->width)
614         width = ximagesrc->xcontext->width - x;
615       if (y + height > ximagesrc->xcontext->height)
616         height = ximagesrc->xcontext->height - y;
617       g_assert (x >= 0);
618       g_assert (y >= 0);
619       GST_DEBUG_OBJECT (ximagesrc,
620           "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
621           x, y, width, height, ximagesrc->startx, ximagesrc->starty,
622           ximagesrc->endx, ximagesrc->endy);
623       /* only get where cursor last was, if it is in our range */
624       if (ximagesrc->endx > ximagesrc->startx &&
625           ximagesrc->endy > ximagesrc->starty) {
626         /* check bounds */
627         if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
628           /* trivial reject */
629         } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
630           /* trivial reject */
631         } else {
632           /* find intersect region */
633           int startx, starty, iwidth, iheight;
634
635           startx = (x < ximagesrc->startx) ? ximagesrc->startx : x;
636           starty = (y < ximagesrc->starty) ? ximagesrc->starty : y;
637           iwidth = (x + width < ximagesrc->endx) ?
638               x + width - startx : ximagesrc->endx - startx;
639           iheight = (y + height < ximagesrc->endy) ?
640               y + height - starty : ximagesrc->endy - starty;
641           GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
642           XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
643               startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
644               ximage->ximage, startx - ximagesrc->startx,
645               starty - ximagesrc->starty);
646         }
647       } else {
648
649         GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
650         XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
651             x, y, width, height, AllPlanes, ZPixmap, ximage->ximage, x, y);
652       }
653     }
654 #endif
655
656
657   } else {
658 #endif
659
660 #ifdef HAVE_XSHM
661     if (ximagesrc->xcontext->use_xshm) {
662       GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
663       XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
664           ximage->ximage, ximagesrc->startx, ximagesrc->starty, AllPlanes);
665
666     } else
667 #endif /* HAVE_XSHM */
668     {
669       GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
670       if (ximagesrc->remote) {
671         XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
672             ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
673             ximagesrc->height, AllPlanes, ZPixmap, ximage->ximage, 0, 0);
674       } else {
675         ximage->ximage =
676             XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
677             ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
678             ximagesrc->height, AllPlanes, ZPixmap);
679       }
680     }
681 #ifdef HAVE_XDAMAGE
682   }
683 #endif
684
685 #ifdef HAVE_XFIXES
686   if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {
687
688     GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
689     /* get cursor */
690     if (ximagesrc->cursor_image)
691       XFree (ximagesrc->cursor_image);
692     ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
693     if (ximagesrc->cursor_image != NULL) {
694       int cx, cy, i, j, count;
695       int startx, starty, iwidth, iheight;
696       gboolean cursor_in_image = TRUE;
697
698       cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
699       if (cx < 0)
700         cx = 0;
701       cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
702       if (cy < 0)
703         cy = 0;
704       count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
705
706       /* only get where cursor last was, if it is in our range */
707       if (ximagesrc->endx > ximagesrc->startx &&
708           ximagesrc->endy > ximagesrc->starty) {
709         /* check bounds */
710         if (cx + ximagesrc->cursor_image->width < ximagesrc->startx ||
711             cx > ximagesrc->endx) {
712           /* trivial reject */
713           cursor_in_image = FALSE;
714         } else if (cy + ximagesrc->cursor_image->height < ximagesrc->starty ||
715             cy > ximagesrc->endy) {
716           /* trivial reject */
717           cursor_in_image = FALSE;
718         } else {
719           /* find intersect region */
720
721           startx = (cx < ximagesrc->startx) ? ximagesrc->startx : cx;
722           starty = (cy < ximagesrc->starty) ? ximagesrc->starty : cy;
723           iwidth = (cx + ximagesrc->cursor_image->width < ximagesrc->endx) ?
724               cx + ximagesrc->cursor_image->width - startx :
725               ximagesrc->endx - startx;
726           iheight = (cy + ximagesrc->cursor_image->height < ximagesrc->endy) ?
727               cy + ximagesrc->cursor_image->height - starty :
728               ximagesrc->endy - starty;
729         }
730       } else {
731         startx = cx;
732         starty = cy;
733         iwidth = ximagesrc->cursor_image->width;
734         iheight = ximagesrc->cursor_image->height;
735       }
736
737       if (cursor_in_image) {
738         GST_DEBUG_OBJECT (ximagesrc, "Cursor is in image so trying to draw it");
739         for (i = 0; i < count; i++)
740           ximagesrc->cursor_image->pixels[i] =
741               GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
742         /* copy those pixels across */
743         for (j = starty;
744             j < starty + iheight && j < ximagesrc->starty + ximagesrc->height;
745             j++) {
746           for (i = startx;
747               i < startx + iwidth && i < ximagesrc->startx + ximagesrc->width;
748               i++) {
749             guint8 *src, *dest;
750
751             src =
752                 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
753                             cy) * ximagesrc->cursor_image->width + (i - cx))]);
754             dest =
755                 (guint8 *) & (ximage->ximage->data[((j -
756                             ximagesrc->starty) * ximagesrc->width + (i -
757                             ximagesrc->startx)) * (ximagesrc->xcontext->bpp /
758                         8)]);
759
760             composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
761                 (guint8 *) src);
762           }
763         }
764       }
765     }
766   }
767 #endif
768 #ifdef HAVE_XDAMAGE
769   if (ximagesrc->have_xdamage && ximagesrc->use_damage) {
770     /* need to ref ximage to put in last_ximage */
771     gst_buffer_ref (GST_BUFFER (ximage));
772     if (ximagesrc->last_ximage) {
773       gst_buffer_unref (GST_BUFFER (ximagesrc->last_ximage));
774     }
775     ximagesrc->last_ximage = ximage;
776     GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
777   }
778 #endif
779   return ximage;
780 }
781
782 static GstFlowReturn
783 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
784 {
785   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
786   GstXImageSrcBuffer *image;
787   GstClockTime base_time;
788   GstClockTime next_capture_ts;
789   GstClockTime dur;
790   gint64 next_frame_no;
791
792   if (!gst_ximage_src_recalc (s)) {
793     GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
794         (_("Changing resolution at runtime is not yet supported.")), (NULL));
795     return GST_FLOW_ERROR;
796   }
797
798   if (s->fps_n <= 0 || s->fps_d <= 0)
799     return GST_FLOW_NOT_NEGOTIATED;     /* FPS must be > 0 */
800
801   /* Now, we might need to wait for the next multiple of the fps
802    * before capturing */
803
804   GST_OBJECT_LOCK (s);
805   if (GST_ELEMENT_CLOCK (s) == NULL) {
806     GST_OBJECT_UNLOCK (s);
807     GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
808         (_("Cannot operate without a clock")), (NULL));
809     return GST_FLOW_ERROR;
810   }
811
812   base_time = GST_ELEMENT_CAST (s)->base_time;
813   next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
814   next_capture_ts -= base_time;
815
816   /* Figure out which 'frame number' position we're at, based on the cur time
817    * and frame rate */
818   next_frame_no = gst_util_uint64_scale (next_capture_ts,
819       s->fps_n, GST_SECOND * s->fps_d);
820   if (next_frame_no == s->last_frame_no) {
821     GstClockID id;
822     GstClockReturn ret;
823
824     /* Need to wait for the next frame */
825     next_frame_no += 1;
826
827     /* Figure out what the next frame time is */
828     next_capture_ts = gst_util_uint64_scale (next_frame_no,
829         s->fps_d * GST_SECOND, s->fps_n);
830
831     id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
832         next_capture_ts + base_time);
833     s->clock_id = id;
834
835     /* release the object lock while waiting */
836     GST_OBJECT_UNLOCK (s);
837
838     GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
839         next_capture_ts);
840     ret = gst_clock_id_wait (id, NULL);
841     GST_OBJECT_LOCK (s);
842
843     gst_clock_id_unref (id);
844     s->clock_id = NULL;
845     if (ret == GST_CLOCK_UNSCHEDULED) {
846       /* Got woken up by the unlock function */
847       GST_OBJECT_UNLOCK (s);
848       return GST_FLOW_WRONG_STATE;
849     }
850     /* Duration is a complete 1/fps frame duration */
851     dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
852   } else {
853     GstClockTime next_frame_ts;
854
855     GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
856         G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
857         G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
858     next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
859         s->fps_d * GST_SECOND, s->fps_n);
860     /* Frame duration is from now until the next expected capture time */
861     dur = next_frame_ts - next_capture_ts;
862   }
863   s->last_frame_no = next_frame_no;
864   GST_OBJECT_UNLOCK (s);
865
866   image = gst_ximage_src_ximage_get (s);
867   if (!image)
868     return GST_FLOW_ERROR;
869
870   *buf = GST_BUFFER (image);
871   GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
872   GST_BUFFER_DURATION (*buf) = dur;
873
874   return GST_FLOW_OK;
875 }
876
877 static void
878 gst_ximage_src_set_property (GObject * object, guint prop_id,
879     const GValue * value, GParamSpec * pspec)
880 {
881   GstXImageSrc *src = GST_XIMAGE_SRC (object);
882
883   switch (prop_id) {
884     case PROP_DISPLAY_NAME:
885
886       g_free (src->display_name);
887       src->display_name = g_strdup (g_value_get_string (value));
888       break;
889     case PROP_SCREEN_NUM:
890       src->screen_num = g_value_get_uint (value);
891       break;
892     case PROP_SHOW_POINTER:
893       src->show_pointer = g_value_get_boolean (value);
894       break;
895     case PROP_USE_DAMAGE:
896       src->use_damage = g_value_get_boolean (value);
897       break;
898     case PROP_STARTX:
899       src->startx = g_value_get_uint (value);
900       break;
901     case PROP_STARTY:
902       src->starty = g_value_get_uint (value);
903       break;
904     case PROP_ENDX:
905       src->endx = g_value_get_uint (value);
906       break;
907     case PROP_ENDY:
908       src->endy = g_value_get_uint (value);
909       break;
910     case PROP_REMOTE:
911       src->remote = g_value_get_boolean (value);
912       break;
913     case PROP_XID:
914       if (src->xcontext != NULL) {
915         g_warning ("ximagesrc window ID must be set before opening display");
916         break;
917       }
918       src->xid = g_value_get_uint64 (value);
919       break;
920     case PROP_XNAME:
921       if (src->xcontext != NULL) {
922         g_warning ("ximagesrc window name must be set before opening display");
923         break;
924       }
925       g_free (src->xname);
926       src->xname = g_strdup (g_value_get_string (value));
927       break;
928     default:
929       break;
930   }
931 }
932
933 static void
934 gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
935     GParamSpec * pspec)
936 {
937   GstXImageSrc *src = GST_XIMAGE_SRC (object);
938
939   switch (prop_id) {
940     case PROP_DISPLAY_NAME:
941       if (src->xcontext)
942         g_value_set_string (value, DisplayString (src->xcontext->disp));
943       else
944         g_value_set_string (value, src->display_name);
945
946       break;
947     case PROP_SCREEN_NUM:
948       g_value_set_uint (value, src->screen_num);
949       break;
950     case PROP_SHOW_POINTER:
951       g_value_set_boolean (value, src->show_pointer);
952       break;
953     case PROP_USE_DAMAGE:
954       g_value_set_boolean (value, src->use_damage);
955       break;
956     case PROP_STARTX:
957       g_value_set_uint (value, src->startx);
958       break;
959     case PROP_STARTY:
960       g_value_set_uint (value, src->starty);
961       break;
962     case PROP_ENDX:
963       g_value_set_uint (value, src->endx);
964       break;
965     case PROP_ENDY:
966       g_value_set_uint (value, src->endy);
967       break;
968     case PROP_REMOTE:
969       g_value_set_boolean (value, src->remote);
970       break;
971     case PROP_XID:
972       g_value_set_uint64 (value, src->xid);
973       break;
974     case PROP_XNAME:
975       g_value_set_string (value, src->xname);
976       break;
977     default:
978       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
979       break;
980   }
981 }
982
983 static void
984 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
985 {
986   g_mutex_lock (ximagesrc->pool_lock);
987   while (ximagesrc->buffer_pool != NULL) {
988     GstXImageSrcBuffer *ximage = ximagesrc->buffer_pool->data;
989
990     gst_ximage_buffer_free (ximage);
991
992     ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
993         ximagesrc->buffer_pool);
994   }
995   g_mutex_unlock (ximagesrc->pool_lock);
996 }
997
998 static void
999 gst_ximage_src_base_init (gpointer g_class)
1000 {
1001   GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
1002
1003   gst_element_class_set_details_simple (ec, "Ximage video source",
1004       "Source/Video",
1005       "Creates a screenshot video stream",
1006       "Lutz Mueller <lutz@users.sourceforge.net>, "
1007       "Jan Schmidt <thaytan@mad.scientist.com>, "
1008       "Zaheer Merali <zaheerabbas at merali dot org>");
1009   gst_element_class_add_static_pad_template (ec, &t);
1010 }
1011
1012 static void
1013 gst_ximage_src_dispose (GObject * object)
1014 {
1015   /* Drop references in the buffer_pool */
1016   gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
1017
1018   G_OBJECT_CLASS (parent_class)->dispose (object);
1019 }
1020
1021 static void
1022 gst_ximage_src_finalize (GObject * object)
1023 {
1024   GstXImageSrc *src = GST_XIMAGE_SRC (object);
1025
1026   if (src->xcontext)
1027     ximageutil_xcontext_clear (src->xcontext);
1028
1029   g_free (src->xname);
1030   g_mutex_free (src->pool_lock);
1031   g_mutex_free (src->x_lock);
1032
1033   G_OBJECT_CLASS (parent_class)->finalize (object);
1034 }
1035
1036 static GstCaps *
1037 gst_ximage_src_get_caps (GstBaseSrc * bs)
1038 {
1039   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1040   GstXContext *xcontext;
1041   gint width, height;
1042
1043   if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
1044     return
1045         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1046             (s)->srcpad));
1047
1048   if (!gst_ximage_src_recalc (s))
1049     return
1050         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC
1051             (s)->srcpad));
1052
1053   xcontext = s->xcontext;
1054   width = s->xcontext->width;
1055   height = s->xcontext->height;
1056   if (s->xwindow != 0) {
1057     XWindowAttributes attrs;
1058     int status = XGetWindowAttributes (s->xcontext->disp, s->xwindow, &attrs);
1059     if (status) {
1060       width = attrs.width;
1061       height = attrs.height;
1062     }
1063   }
1064
1065   /* property comments say 0 means right/bottom, means we can't capture
1066      the top left pixel alone */
1067   if (s->endx == 0)
1068     s->endx = width - 1;
1069   if (s->endy == 0)
1070     s->endy = height - 1;
1071
1072   if (s->endx >= s->startx && s->endy >= s->starty) {
1073     /* this means user has put in values */
1074     if (s->startx < xcontext->width && s->endx < xcontext->width &&
1075         s->starty < xcontext->height && s->endy < xcontext->height &&
1076         s->startx >= 0 && s->starty >= 0) {
1077       /* values are fine */
1078       s->width = width = s->endx - s->startx + 1;
1079       s->height = height = s->endy - s->starty + 1;
1080     } else {
1081       GST_WARNING
1082           ("User put in co-ordinates overshooting the X resolution, setting to full screen");
1083       s->startx = 0;
1084       s->starty = 0;
1085       s->endx = width - 1;
1086       s->endy = height - 1;
1087     }
1088   } else {
1089     GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
1090     s->startx = 0;
1091     s->starty = 0;
1092     s->endx = width - 1;
1093     s->endy = height - 1;
1094   }
1095   GST_DEBUG ("width = %d, height=%d", width, height);
1096   return gst_caps_new_simple ("video/x-raw-rgb",
1097       "bpp", G_TYPE_INT, xcontext->bpp,
1098       "depth", G_TYPE_INT, xcontext->depth,
1099       "endianness", G_TYPE_INT, xcontext->endianness,
1100       "red_mask", G_TYPE_INT, xcontext->r_mask_output,
1101       "green_mask", G_TYPE_INT, xcontext->g_mask_output,
1102       "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
1103       "width", G_TYPE_INT, width,
1104       "height", G_TYPE_INT, height,
1105       "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1106       "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1107       NULL);
1108 }
1109
1110 static gboolean
1111 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
1112 {
1113   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1114   GstStructure *structure;
1115   const GValue *new_fps;
1116
1117   /* If not yet opened, disallow setcaps until later */
1118   if (!s->xcontext)
1119     return FALSE;
1120
1121   /* The only thing that can change is the framerate downstream wants */
1122   structure = gst_caps_get_structure (caps, 0);
1123   new_fps = gst_structure_get_value (structure, "framerate");
1124   if (!new_fps)
1125     return FALSE;
1126
1127   /* Store this FPS for use when generating buffers */
1128   s->fps_n = gst_value_get_fraction_numerator (new_fps);
1129   s->fps_d = gst_value_get_fraction_denominator (new_fps);
1130
1131   GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1132
1133   return TRUE;
1134 }
1135
1136 static void
1137 gst_ximage_src_fixate (GstPad * pad, GstCaps * caps)
1138 {
1139   gint i;
1140   GstStructure *structure;
1141
1142   for (i = 0; i < gst_caps_get_size (caps); ++i) {
1143     structure = gst_caps_get_structure (caps, i);
1144
1145     gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1146   }
1147 }
1148
1149 static void
1150 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1151 {
1152   GObjectClass *gc = G_OBJECT_CLASS (klass);
1153   GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
1154   GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
1155
1156   gc->set_property = gst_ximage_src_set_property;
1157   gc->get_property = gst_ximage_src_get_property;
1158   gc->dispose = gst_ximage_src_dispose;
1159   gc->finalize = gst_ximage_src_finalize;
1160
1161   g_object_class_install_property (gc, PROP_DISPLAY_NAME,
1162       g_param_spec_string ("display-name", "Display", "X Display Name", NULL,
1163           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1164   g_object_class_install_property (gc, PROP_SCREEN_NUM,
1165       g_param_spec_uint ("screen-num", "Screen number", "X Screen Number",
1166           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1167   g_object_class_install_property (gc, PROP_SHOW_POINTER,
1168       g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
1169           "Show mouse pointer (if XFixes extension enabled)", TRUE,
1170           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1171   /**
1172    * GstXImageSrc:use-damage
1173    *
1174    * Use XDamage (if the XDamage extension is enabled)
1175    *
1176    * Since: 0.10.4
1177    **/
1178   g_object_class_install_property (gc, PROP_USE_DAMAGE,
1179       g_param_spec_boolean ("use-damage", "Use XDamage",
1180           "Use XDamage (if XDamage extension enabled)", TRUE,
1181           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1182   /**
1183    * GstXImageSrc:startx
1184    *
1185    * X coordinate of top left corner of area to be recorded
1186    * (0 for top left of screen)
1187    *
1188    * Since: 0.10.4
1189    **/
1190   g_object_class_install_property (gc, PROP_STARTX,
1191       g_param_spec_uint ("startx", "Start X co-ordinate",
1192           "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
1193           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1194   /**
1195    * GstXImageSrc:starty
1196    *
1197    * Y coordinate of top left corner of area to be recorded
1198    * (0 for top left of screen)
1199    *
1200    * Since: 0.10.4
1201    **/
1202   g_object_class_install_property (gc, PROP_STARTY,
1203       g_param_spec_uint ("starty", "Start Y co-ordinate",
1204           "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
1205           0, G_MAXINT, 0, G_PARAM_READWRITE));
1206   /**
1207    * GstXImageSrc:endx
1208    *
1209    * X coordinate of bottom right corner of area to be recorded
1210    * (0 for bottom right of screen)
1211    *
1212    * Since: 0.10.4
1213    **/
1214   g_object_class_install_property (gc, PROP_ENDX,
1215       g_param_spec_uint ("endx", "End X",
1216           "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1217           0, G_MAXINT, 0, G_PARAM_READWRITE));
1218   /**
1219    * GstXImageSrc:endy
1220    *
1221    * Y coordinate of bottom right corner of area to be recorded
1222    * (0 for bottom right of screen)
1223    *
1224    * Since: 0.10.4
1225    **/
1226   g_object_class_install_property (gc, PROP_ENDY,
1227       g_param_spec_uint ("endy", "End Y",
1228           "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1229           0, G_MAXINT, 0, G_PARAM_READWRITE));
1230
1231   /**
1232    * GstXImageSrc:remote
1233    *
1234    * Whether the X display is remote. The element will try to use alternate calls
1235    * known to work better with remote displays.
1236    *
1237    * Since: 0.10.26
1238    **/
1239   g_object_class_install_property (gc, PROP_REMOTE,
1240       g_param_spec_boolean ("remote", "Remote dispay",
1241           "Whether the display is remote", FALSE,
1242           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1243
1244   /**
1245    * GstXImageSrc:xid
1246    *
1247    * The XID of the window to capture. 0 for the root window (default).
1248    *
1249    * Since: 0.10.31
1250    **/
1251   g_object_class_install_property (gc, PROP_XID,
1252       g_param_spec_uint64 ("xid", "Window XID",
1253           "Window XID to capture from", 0, G_MAXUINT64, 0,
1254           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1255
1256   /**
1257    * GstXImageSrc:xname
1258    *
1259    * The name of the window to capture, if any.
1260    *
1261    * Since: 0.10.31
1262    **/
1263   g_object_class_install_property (gc, PROP_XNAME,
1264       g_param_spec_string ("xname", "Window name",
1265           "Window name to capture from", NULL,
1266           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1267
1268   parent_class = g_type_class_peek_parent (klass);
1269
1270   push_class->create = gst_ximage_src_create;
1271   bc->get_caps = gst_ximage_src_get_caps;
1272   bc->set_caps = gst_ximage_src_set_caps;
1273   bc->start = gst_ximage_src_start;
1274   bc->stop = gst_ximage_src_stop;
1275   bc->unlock = gst_ximage_src_unlock;
1276 }
1277
1278 static void
1279 gst_ximage_src_init (GstXImageSrc * ximagesrc, GstXImageSrcClass * klass)
1280 {
1281   gst_base_src_set_format (GST_BASE_SRC (ximagesrc), GST_FORMAT_TIME);
1282   gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1283   gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (ximagesrc),
1284       gst_ximage_src_fixate);
1285
1286   ximagesrc->pool_lock = g_mutex_new ();
1287   ximagesrc->x_lock = g_mutex_new ();
1288   ximagesrc->show_pointer = TRUE;
1289   ximagesrc->use_damage = TRUE;
1290   ximagesrc->startx = 0;
1291   ximagesrc->starty = 0;
1292   ximagesrc->endx = 0;
1293   ximagesrc->endy = 0;
1294   ximagesrc->remote = FALSE;
1295 }
1296
1297 static gboolean
1298 plugin_init (GstPlugin * plugin)
1299 {
1300   gboolean ret;
1301
1302   GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
1303       "ximagesrc element debug");
1304
1305   ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
1306       GST_TYPE_XIMAGE_SRC);
1307
1308   return ret;
1309 }
1310
1311 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1312     GST_VERSION_MINOR,
1313     "ximagesrc",
1314     "X11 video input plugin using standard Xlib calls",
1315     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);