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