484519fdeb1c0fb255d9e55987c3315d9d0b9363
[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 <glib/gi18n-lib.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   XWindowAttributes attrs;
368   gboolean has_changed = FALSE;
369
370   g_assert (src->xwindow != 0 && src->xcontext);
371
372   if (XGetWindowAttributes (src->xcontext->disp, src->xwindow, &attrs)) {
373     if (src->xwin_width != attrs.width) {
374       src->xwin_width = attrs.width;
375       has_changed = TRUE;
376     }
377     if (src->xwin_height != attrs.height) {
378       src->xwin_height = attrs.height;
379       has_changed = TRUE;
380     }
381   }
382
383   return has_changed;
384 }
385
386 #ifdef HAVE_XFIXES
387 static gboolean
388 gst_ximage_is_pointer_in_region (GstXImageSrc * src)
389 {
390   Window window_returned;
391   int root_x, root_y;
392   int win_x, win_y;
393   unsigned int mask_return;
394   Bool on_window;
395
396   on_window = XQueryPointer (src->xcontext->disp, src->xwindow,
397       &window_returned, &window_returned, &root_x, &root_y, &win_x, &win_y,
398       &mask_return);
399
400   return (on_window && (win_x >= src->startx) && (win_y >= src->starty) &&
401       (win_x < src->endx) && (win_y < src->endy));
402 }
403 #endif
404
405 #ifdef HAVE_XFIXES
406 static void
407 composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
408 {
409   guint8 r = src[2];
410   guint8 g = src[1];
411   guint8 b = src[0];
412   guint8 a = src[3];
413   guint8 dr, dg, db;
414   guint32 color;
415   gint r_shift, r_max, r_shift_out;
416   gint g_shift, g_max, g_shift_out;
417   gint b_shift, b_max, b_shift_out;
418
419   switch (xcontext->bpp) {
420     case 8:
421       color = *dest;
422       break;
423     case 16:
424       color = GUINT16_FROM_LE (*(guint16 *) (dest));
425       break;
426     case 32:
427       color = GUINT32_FROM_LE (*(guint32 *) (dest));
428       break;
429     default:
430       /* Should not reach here */
431       g_return_if_reached ();
432   }
433
434   /* possible optimisation:
435    * move the code that finds shift and max in the _link function */
436   for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
437   for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
438       g_shift++);
439   for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);
440
441   for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
442       r_shift_out++);
443   for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
444       g_shift_out++);
445   for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
446       b_shift_out++);
447
448
449   r_max = (xcontext->visual->red_mask >> r_shift);
450   b_max = (xcontext->visual->blue_mask >> b_shift);
451   g_max = (xcontext->visual->green_mask >> g_shift);
452
453 #define RGBXXX_R(x)  (((x)>>r_shift) & (r_max))
454 #define RGBXXX_G(x)  (((x)>>g_shift) & (g_max))
455 #define RGBXXX_B(x)  (((x)>>b_shift) & (b_max))
456
457   dr = (RGBXXX_R (color) * 255) / r_max;
458   dg = (RGBXXX_G (color) * 255) / g_max;
459   db = (RGBXXX_B (color) * 255) / b_max;
460
461   dr = (r * a + (0xff - a) * dr) / 0xff;
462   dg = (g * a + (0xff - a) * dg) / 0xff;
463   db = (b * a + (0xff - a) * db) / 0xff;
464
465   color = (((dr * r_max) / 255) << r_shift_out) +
466       (((dg * g_max) / 255) << g_shift_out) +
467       (((db * b_max) / 255) << b_shift_out);
468
469   switch (xcontext->bpp) {
470     case 8:
471       *dest = color;
472       break;
473     case 16:
474       *(guint16 *) (dest) = color;
475       break;
476     case 32:
477       *(guint32 *) (dest) = color;
478       break;
479     default:
480       g_warning ("bpp %d not supported\n", xcontext->bpp);
481   }
482 }
483 #endif
484
485 #ifdef HAVE_XDAMAGE
486 static void
487 copy_buffer (GstBuffer * dest, GstBuffer * src)
488 {
489   GstMapInfo map;
490
491   gst_buffer_map (src, &map, GST_MAP_READ);
492   gst_buffer_fill (dest, 0, map.data, map.size);
493   gst_buffer_unmap (src, &map);
494 }
495 #endif
496
497 /* Retrieve an XImageSrcBuffer, preferably from our
498  * pool of existing images and populate it from the window */
499 static GstBuffer *
500 gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
501 {
502   GstBuffer *ximage = NULL;
503   GstMetaXImage *meta;
504
505   g_mutex_lock (&ximagesrc->pool_lock);
506   while (ximagesrc->buffer_pool != NULL) {
507     ximage = ximagesrc->buffer_pool->data;
508
509     meta = GST_META_XIMAGE_GET (ximage);
510
511     ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
512         ximagesrc->buffer_pool);
513
514     if ((meta->width == ximagesrc->width) &&
515         (meta->height == ximagesrc->height))
516       break;
517
518     gst_ximage_buffer_free (ximage);
519     ximage = NULL;
520   }
521   g_mutex_unlock (&ximagesrc->pool_lock);
522
523   if (ximage == NULL) {
524     GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
525         ximagesrc->width, ximagesrc->height);
526
527     g_mutex_lock (&ximagesrc->x_lock);
528     ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
529         GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
530         (BufferReturnFunc) (gst_ximage_src_return_buf));
531     if (ximage == NULL) {
532       GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
533           ("could not create a %dx%d ximage", ximagesrc->width,
534               ximagesrc->height));
535       g_mutex_unlock (&ximagesrc->x_lock);
536       return NULL;
537     }
538
539     g_mutex_unlock (&ximagesrc->x_lock);
540   }
541
542   g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
543
544   meta = GST_META_XIMAGE_GET (ximage);
545
546 #ifdef HAVE_XDAMAGE
547   if (ximagesrc->have_xdamage && ximagesrc->use_damage &&
548       ximagesrc->last_ximage != NULL) {
549     XEvent ev;
550     gboolean have_damage = FALSE;
551
552     /* have_frame is TRUE when either the entire screen has been
553      * grabbed or when the last image has been copied */
554     gboolean have_frame = FALSE;
555
556     GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");
557
558     do {
559       XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *) (&ev);
560
561       if (XCheckTypedEvent (ximagesrc->xcontext->disp,
562               ximagesrc->damage_event_base + XDamageNotify, &ev) &&
563           damage_ev->level == XDamageReportNonEmpty) {
564
565         XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None,
566             ximagesrc->damage_region);
567         have_damage = TRUE;
568       }
569     } while (XPending (ximagesrc->xcontext->disp));
570
571     if (have_damage) {
572       XRectangle *rects;
573       int nrects;
574
575       /* Now copy out all of the damaged rectangles. */
576       rects =
577           XFixesFetchRegion (ximagesrc->xcontext->disp,
578           ximagesrc->damage_region, &nrects);
579       if (rects != NULL) {
580         int i;
581
582         if (!have_frame) {
583           GST_LOG_OBJECT (ximagesrc,
584               "Copying from last frame ximage->size: %" G_GSIZE_FORMAT,
585               gst_buffer_get_size (ximage));
586           copy_buffer (ximage, ximagesrc->last_ximage);
587           have_frame = TRUE;
588         }
589         for (i = 0; i < nrects; i++) {
590           GST_LOG_OBJECT (ximagesrc,
591               "Damaged sub-region @ %d,%d size %dx%d reported",
592               rects[i].x, rects[i].y, rects[i].width, rects[i].height);
593
594           /* if we only want a small area, clip this damage region to
595            * area we want */
596           if (ximagesrc->endx > ximagesrc->startx &&
597               ximagesrc->endy > ximagesrc->starty) {
598             /* see if damage area intersects */
599             if (rects[i].x + rects[i].width - 1 < ximagesrc->startx ||
600                 rects[i].x > ximagesrc->endx) {
601               /* trivial reject */
602             } else if (rects[i].y + rects[i].height - 1 < ximagesrc->starty ||
603                 rects[i].y > ximagesrc->endy) {
604               /* trivial reject */
605             } else {
606               /* find intersect region */
607               int startx, starty, width, height;
608
609               startx = (rects[i].x < ximagesrc->startx) ? ximagesrc->startx :
610                   rects[i].x;
611               starty = (rects[i].y < ximagesrc->starty) ? ximagesrc->starty :
612                   rects[i].y;
613               width = (rects[i].x + rects[i].width - 1 < ximagesrc->endx) ?
614                   rects[i].x + rects[i].width - startx :
615                   ximagesrc->endx - startx + 1;
616               height = (rects[i].y + rects[i].height - 1 < ximagesrc->endy) ?
617                   rects[i].y + rects[i].height - starty : ximagesrc->endy -
618                   starty + 1;
619
620               GST_LOG_OBJECT (ximagesrc,
621                   "Retrieving damaged sub-region @ %d,%d size %dx%d as intersect region",
622                   startx, starty, width, height);
623               XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
624                   startx, starty, width, height, AllPlanes, ZPixmap,
625                   meta->ximage, startx - ximagesrc->startx,
626                   starty - ximagesrc->starty);
627             }
628           } else {
629
630             GST_LOG_OBJECT (ximagesrc,
631                 "Retrieving damaged sub-region @ %d,%d size %dx%d",
632                 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
633
634             XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
635                 rects[i].x, rects[i].y,
636                 rects[i].width, rects[i].height,
637                 AllPlanes, ZPixmap, meta->ximage, rects[i].x, rects[i].y);
638           }
639         }
640         XFree (rects);
641       }
642     }
643     if (!have_frame) {
644       GST_LOG_OBJECT (ximagesrc,
645           "Copying from last frame ximage->size: %" G_GSIZE_FORMAT,
646           gst_buffer_get_size (ximage));
647       copy_buffer (ximage, ximagesrc->last_ximage);
648     }
649 #ifdef HAVE_XFIXES
650     /* re-get area where last mouse pointer was  but only if in our clipping
651      * bounds */
652     if (ximagesrc->cursor_image) {
653       gint x, y, width, height;
654
655       x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot -
656           ximagesrc->x;
657       y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot -
658           ximagesrc->y;
659       width = ximagesrc->cursor_image->width;
660       height = ximagesrc->cursor_image->height;
661
662       /* bounds checking */
663       if (x < 0)
664         x = 0;
665       if (y < 0)
666         y = 0;
667       if (x + width > ximagesrc->xcontext->width)
668         width = ximagesrc->xcontext->width - x;
669       if (y + height > ximagesrc->xcontext->height)
670         height = ximagesrc->xcontext->height - y;
671       g_assert (x >= 0);
672       g_assert (y >= 0);
673       GST_DEBUG_OBJECT (ximagesrc,
674           "Cursor was at (%d,%d) width: %d, height: %d and our range is: (%d,%d) - (%d,%d)",
675           x, y, width, height, ximagesrc->startx, ximagesrc->starty,
676           ximagesrc->endx, ximagesrc->endy);
677       /* only get where cursor last was, if it is in our range */
678       if (ximagesrc->endx > ximagesrc->startx &&
679           ximagesrc->endy > ximagesrc->starty) {
680         /* check bounds */
681         if (x + width < ximagesrc->startx || x > ximagesrc->endx) {
682           /* trivial reject */
683         } else if (y + height < ximagesrc->starty || y > ximagesrc->endy) {
684           /* trivial reject */
685         } else {
686           /* find intersect region */
687           int startx, starty, iwidth, iheight;
688
689           startx = (x < ximagesrc->startx) ? ximagesrc->startx : x;
690           starty = (y < ximagesrc->starty) ? ximagesrc->starty : y;
691           iwidth = (x + width < ximagesrc->endx) ?
692               x + width - startx : ximagesrc->endx - startx;
693           iheight = (y + height < ximagesrc->endy) ?
694               y + height - starty : ximagesrc->endy - starty;
695           GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
696           XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
697               startx, starty, iwidth, iheight, AllPlanes, ZPixmap,
698               meta->ximage, startx - ximagesrc->startx,
699               starty - ximagesrc->starty);
700         }
701       } else {
702
703         GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
704         XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
705             x, y, width, height, AllPlanes, ZPixmap, meta->ximage, x, y);
706       }
707     }
708 #endif
709
710
711   } else {
712 #endif
713
714 #ifdef HAVE_XSHM
715     if (ximagesrc->xcontext->use_xshm) {
716       GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
717       XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
718           meta->ximage, ximagesrc->startx, ximagesrc->starty, AllPlanes);
719
720     } else
721 #endif /* HAVE_XSHM */
722     {
723       GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
724       if (ximagesrc->remote) {
725         XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
726             ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
727             ximagesrc->height, AllPlanes, ZPixmap, meta->ximage, 0, 0);
728       } else {
729         meta->ximage =
730             XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
731             ximagesrc->startx, ximagesrc->starty, ximagesrc->width,
732             ximagesrc->height, AllPlanes, ZPixmap);
733       }
734     }
735 #ifdef HAVE_XDAMAGE
736   }
737 #endif
738
739 #ifdef HAVE_XFIXES
740   if (ximagesrc->show_pointer && ximagesrc->have_xfixes
741       && gst_ximage_is_pointer_in_region (ximagesrc)) {
742
743     GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
744     /* get cursor */
745     if (ximagesrc->cursor_image)
746       XFree (ximagesrc->cursor_image);
747     ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
748     if (ximagesrc->cursor_image != NULL) {
749       int cx, cy, i, j, count;
750       int startx, starty, iwidth, iheight;
751       gboolean cursor_in_image = TRUE;
752
753       cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot -
754           ximagesrc->x;
755       cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot -
756           ximagesrc->y;
757       count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
758
759       /* only get where cursor last was, if it is in our range */
760       if (ximagesrc->endx > ximagesrc->startx &&
761           ximagesrc->endy > ximagesrc->starty) {
762         /* check bounds */
763         if (cx + ximagesrc->cursor_image->width < (int) ximagesrc->startx ||
764             cx > (int) ximagesrc->endx) {
765           /* trivial reject */
766           cursor_in_image = FALSE;
767         } else if (cy + ximagesrc->cursor_image->height <
768             (int) ximagesrc->starty || cy > (int) ximagesrc->endy) {
769           /* trivial reject */
770           cursor_in_image = FALSE;
771         } else {
772           /* find intersect region */
773
774           startx = (cx < (int) ximagesrc->startx) ? ximagesrc->startx : cx;
775           starty = (cy < (int) ximagesrc->starty) ? ximagesrc->starty : cy;
776           iwidth = (cx + ximagesrc->cursor_image->width < ximagesrc->endx) ?
777               cx + ximagesrc->cursor_image->width - startx :
778               ximagesrc->endx - startx;
779           iheight =
780               (cy + ximagesrc->cursor_image->height <
781               ximagesrc->endy) ? cy + ximagesrc->cursor_image->height -
782               starty : ximagesrc->endy - starty;
783         }
784       } else {
785         startx = cx;
786         starty = cy;
787         iwidth = ximagesrc->cursor_image->width;
788         iheight = ximagesrc->cursor_image->height;
789       }
790
791       if (cursor_in_image) {
792         GST_DEBUG_OBJECT (ximagesrc, "Cursor is in image so trying to draw it");
793         for (i = 0; i < count; i++)
794           ximagesrc->cursor_image->pixels[i] =
795               GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
796         /* copy those pixels across */
797         for (j = starty;
798             j < starty + iheight
799             && j < ximagesrc->starty + ximagesrc->height; j++) {
800           for (i = startx;
801               i < startx + iwidth
802               && i < ximagesrc->startx + ximagesrc->width; i++) {
803             guint8 *src, *dest;
804
805             src =
806                 (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
807                             cy) * ximagesrc->cursor_image->width + (i - cx))]);
808             dest =
809                 (guint8 *) & (meta->ximage->data[((j -
810                             ximagesrc->starty) * ximagesrc->width + (i -
811                             ximagesrc->startx)) *
812                     (ximagesrc->xcontext->bpp / 8)]);
813
814             composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
815                 (guint8 *) src);
816           }
817         }
818       }
819     }
820   }
821 #endif
822 #ifdef HAVE_XDAMAGE
823   if (ximagesrc->have_xdamage && ximagesrc->use_damage) {
824     /* need to ref ximage to put in last_ximage */
825     gst_buffer_ref (ximage);
826     if (ximagesrc->last_ximage) {
827       gst_buffer_unref (ximagesrc->last_ximage);
828     }
829     ximagesrc->last_ximage = ximage;
830     GST_LOG_OBJECT (ximagesrc, "reffing current buffer for last_ximage");
831   }
832 #endif
833   return ximage;
834 }
835
836 static GstFlowReturn
837 gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
838 {
839   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
840   GstBuffer *image;
841   GstClockTime base_time;
842   GstClockTime next_capture_ts;
843   GstClockTime dur;
844   gint64 next_frame_no;
845
846   if (s->fps_n <= 0 || s->fps_d <= 0)
847     return GST_FLOW_NOT_NEGOTIATED;     /* FPS must be > 0 */
848
849   /* Now, we might need to wait for the next multiple of the fps
850    * before capturing */
851
852   GST_OBJECT_LOCK (s);
853   if (GST_ELEMENT_CLOCK (s) == NULL) {
854     GST_OBJECT_UNLOCK (s);
855     GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
856         (_("Cannot operate without a clock")), (NULL));
857     return GST_FLOW_ERROR;
858   }
859
860   base_time = GST_ELEMENT_CAST (s)->base_time;
861   next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
862   next_capture_ts -= base_time;
863
864   /* Figure out which 'frame number' position we're at, based on the cur time
865    * and frame rate */
866   next_frame_no = gst_util_uint64_scale (next_capture_ts,
867       s->fps_n, GST_SECOND * s->fps_d);
868   if (next_frame_no == s->last_frame_no) {
869     GstClockID id;
870     GstClockReturn ret;
871
872     /* Need to wait for the next frame */
873     next_frame_no += 1;
874
875     /* Figure out what the next frame time is */
876     next_capture_ts = gst_util_uint64_scale (next_frame_no,
877         s->fps_d * GST_SECOND, s->fps_n);
878
879     id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
880         next_capture_ts + base_time);
881     s->clock_id = id;
882
883     /* release the object lock while waiting */
884     GST_OBJECT_UNLOCK (s);
885
886     GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
887         next_capture_ts);
888     ret = gst_clock_id_wait (id, NULL);
889     GST_OBJECT_LOCK (s);
890
891     gst_clock_id_unref (id);
892     s->clock_id = NULL;
893     if (ret == GST_CLOCK_UNSCHEDULED) {
894       /* Got woken up by the unlock function */
895       GST_OBJECT_UNLOCK (s);
896       return GST_FLOW_FLUSHING;
897     }
898     /* Duration is a complete 1/fps frame duration */
899     dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
900   } else {
901     GstClockTime next_frame_ts;
902
903     GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
904         G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
905         G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
906     next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
907         s->fps_d * GST_SECOND, s->fps_n);
908     /* Frame duration is from now until the next expected capture time */
909     dur = next_frame_ts - next_capture_ts;
910   }
911   s->last_frame_no = next_frame_no;
912   GST_OBJECT_UNLOCK (s);
913
914   if (gst_ximage_src_recalc (s) && !gst_base_src_negotiate (GST_BASE_SRC (s))) {
915     return GST_FLOW_NOT_NEGOTIATED;
916   }
917
918   image = gst_ximage_src_ximage_get (s);
919
920   if (!image)
921     return GST_FLOW_ERROR;
922
923   *buf = image;
924   GST_BUFFER_DTS (*buf) = GST_CLOCK_TIME_NONE;
925   GST_BUFFER_PTS (*buf) = next_capture_ts;
926   GST_BUFFER_DURATION (*buf) = dur;
927
928   return GST_FLOW_OK;
929 }
930
931 static void
932 gst_ximage_src_set_property (GObject * object, guint prop_id,
933     const GValue * value, GParamSpec * pspec)
934 {
935   GstXImageSrc *src = GST_XIMAGE_SRC (object);
936
937   switch (prop_id) {
938     case PROP_DISPLAY_NAME:
939
940       g_free (src->display_name);
941       src->display_name = g_value_dup_string (value);
942       break;
943     case PROP_SHOW_POINTER:
944       src->show_pointer = g_value_get_boolean (value);
945       break;
946     case PROP_USE_DAMAGE:
947       src->use_damage = g_value_get_boolean (value);
948       break;
949     case PROP_STARTX:
950       src->startx = g_value_get_uint (value);
951       break;
952     case PROP_STARTY:
953       src->starty = g_value_get_uint (value);
954       break;
955     case PROP_ENDX:
956       src->endx = g_value_get_uint (value);
957       /* property comments say 0 means right/bottom, means we can't capture
958        * the top left pixel alone */
959       src->endx_fit_to_screen = (src->endx == 0);
960       break;
961     case PROP_ENDY:
962       src->endy = g_value_get_uint (value);
963       src->endy_fit_to_screen = (src->endy == 0);
964       break;
965     case PROP_REMOTE:
966       src->remote = g_value_get_boolean (value);
967       break;
968     case PROP_XID:
969       if (src->xcontext != NULL) {
970         g_warning ("ximagesrc window ID must be set before opening display");
971         break;
972       }
973       src->xid = g_value_get_uint64 (value);
974       break;
975     case PROP_XNAME:
976       if (src->xcontext != NULL) {
977         g_warning ("ximagesrc window name must be set before opening display");
978         break;
979       }
980       g_free (src->xname);
981       src->xname = g_value_dup_string (value);
982       break;
983     default:
984       break;
985   }
986 }
987
988 static void
989 gst_ximage_src_get_property (GObject * object, guint prop_id,
990     GValue * value, GParamSpec * pspec)
991 {
992   GstXImageSrc *src = GST_XIMAGE_SRC (object);
993
994   switch (prop_id) {
995     case PROP_DISPLAY_NAME:
996       if (src->xcontext)
997         g_value_set_string (value, DisplayString (src->xcontext->disp));
998       else
999         g_value_set_string (value, src->display_name);
1000
1001       break;
1002     case PROP_SHOW_POINTER:
1003       g_value_set_boolean (value, src->show_pointer);
1004       break;
1005     case PROP_USE_DAMAGE:
1006       g_value_set_boolean (value, src->use_damage);
1007       break;
1008     case PROP_STARTX:
1009       g_value_set_uint (value, src->startx);
1010       break;
1011     case PROP_STARTY:
1012       g_value_set_uint (value, src->starty);
1013       break;
1014     case PROP_ENDX:
1015       g_value_set_uint (value, src->endx);
1016       break;
1017     case PROP_ENDY:
1018       g_value_set_uint (value, src->endy);
1019       break;
1020     case PROP_REMOTE:
1021       g_value_set_boolean (value, src->remote);
1022       break;
1023     case PROP_XID:
1024       g_value_set_uint64 (value, src->xid);
1025       break;
1026     case PROP_XNAME:
1027       g_value_set_string (value, src->xname);
1028       break;
1029     default:
1030       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1031       break;
1032   }
1033 }
1034
1035 static void
1036 gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
1037 {
1038   g_mutex_lock (&ximagesrc->pool_lock);
1039   while (ximagesrc->buffer_pool != NULL) {
1040     GstBuffer *ximage = ximagesrc->buffer_pool->data;
1041
1042     gst_ximage_buffer_free (ximage);
1043
1044     ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
1045         ximagesrc->buffer_pool);
1046   }
1047   g_mutex_unlock (&ximagesrc->pool_lock);
1048 }
1049
1050 static void
1051 gst_ximage_src_dispose (GObject * object)
1052 {
1053   /* Drop references in the buffer_pool */
1054   gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
1055
1056   G_OBJECT_CLASS (parent_class)->dispose (object);
1057 }
1058
1059 static void
1060 gst_ximage_src_finalize (GObject * object)
1061 {
1062   GstXImageSrc *src = GST_XIMAGE_SRC (object);
1063
1064   if (src->xcontext)
1065     ximageutil_xcontext_clear (src->xcontext);
1066
1067   g_free (src->xname);
1068   g_mutex_clear (&src->pool_lock);
1069   g_mutex_clear (&src->x_lock);
1070
1071   G_OBJECT_CLASS (parent_class)->finalize (object);
1072 }
1073
1074 static GstCaps *
1075 gst_ximage_src_get_caps (GstBaseSrc * bs, GstCaps * filter)
1076 {
1077   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1078   GstXContext *xcontext;
1079   gint width, height;
1080   GstVideoFormat format;
1081   guint32 alpha_mask;
1082
1083   if ((!s->xcontext) && (!gst_ximage_src_open_display (s, s->display_name)))
1084     return gst_pad_get_pad_template_caps (GST_BASE_SRC (s)->srcpad);
1085
1086   gst_ximage_src_recalc (s);
1087
1088   xcontext = s->xcontext;
1089   width = s->xwin_width;
1090   height = s->xwin_height;
1091
1092   if (s->endx_fit_to_screen)
1093     s->endx = width - 1;
1094   if (s->endy_fit_to_screen)
1095     s->endy = height - 1;
1096
1097   if (s->endx >= s->startx && s->endy >= s->starty) {
1098     /* this means user has put in values */
1099     if (s->startx < width && s->endx < width &&
1100         s->starty < height && s->endy < height) {
1101       /* values are fine */
1102       s->width = width = s->endx - s->startx + 1;
1103       s->height = height = s->endy - s->starty + 1;
1104     } else {
1105       GST_WARNING
1106           ("User put in co-ordinates overshooting the X resolution, setting to full screen");
1107       s->startx = 0;
1108       s->starty = 0;
1109       s->endx = width - 1;
1110       s->endy = height - 1;
1111       s->endx_fit_to_screen = FALSE;
1112       s->endy_fit_to_screen = FALSE;
1113     }
1114   } else {
1115     GST_WARNING ("User put in bogus co-ordinates, setting to full screen");
1116     s->startx = 0;
1117     s->starty = 0;
1118     s->endx = width - 1;
1119     s->endy = height - 1;
1120     s->endx_fit_to_screen = FALSE;
1121     s->endy_fit_to_screen = FALSE;
1122   }
1123   GST_DEBUG ("width = %d, height=%d", width, height);
1124
1125   /* extrapolate alpha mask */
1126   if (xcontext->depth == 32) {
1127     alpha_mask = ~(xcontext->r_mask_output
1128         | xcontext->g_mask_output | xcontext->b_mask_output);
1129   } else {
1130     alpha_mask = 0;
1131   }
1132
1133   format =
1134       gst_video_format_from_masks (xcontext->depth, xcontext->bpp,
1135       xcontext->endianness, xcontext->r_mask_output,
1136       xcontext->g_mask_output, xcontext->b_mask_output, alpha_mask);
1137
1138   return gst_caps_new_simple ("video/x-raw",
1139       "format", G_TYPE_STRING, gst_video_format_to_string (format),
1140       "width", G_TYPE_INT, width,
1141       "height", G_TYPE_INT, height,
1142       "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
1143       "pixel-aspect-ratio", GST_TYPE_FRACTION, xcontext->par_n,
1144       xcontext->par_d, NULL);
1145 }
1146
1147 static gboolean
1148 gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
1149 {
1150   GstXImageSrc *s = GST_XIMAGE_SRC (bs);
1151   GstStructure *structure;
1152   const GValue *new_fps;
1153
1154   /* If not yet opened, disallow setcaps until later */
1155   if (!s->xcontext)
1156     return FALSE;
1157
1158   /* The only thing that can change is the framerate downstream wants */
1159   structure = gst_caps_get_structure (caps, 0);
1160   new_fps = gst_structure_get_value (structure, "framerate");
1161   if (!new_fps)
1162     return FALSE;
1163
1164   /* Store this FPS for use when generating buffers */
1165   s->fps_n = gst_value_get_fraction_numerator (new_fps);
1166   s->fps_d = gst_value_get_fraction_denominator (new_fps);
1167
1168   GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);
1169
1170   return TRUE;
1171 }
1172
1173 static GstCaps *
1174 gst_ximage_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
1175 {
1176   gint i;
1177   GstStructure *structure;
1178
1179   caps = gst_caps_make_writable (caps);
1180
1181   for (i = 0; i < gst_caps_get_size (caps); ++i) {
1182     structure = gst_caps_get_structure (caps, i);
1183
1184     gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
1185   }
1186   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
1187
1188   return caps;
1189 }
1190
1191 static void
1192 gst_ximage_src_class_init (GstXImageSrcClass * klass)
1193 {
1194   GObjectClass *gc = G_OBJECT_CLASS (klass);
1195   GstElementClass *ec = GST_ELEMENT_CLASS (klass);
1196   GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
1197   GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);
1198
1199   gc->set_property = gst_ximage_src_set_property;
1200   gc->get_property = gst_ximage_src_get_property;
1201   gc->dispose = gst_ximage_src_dispose;
1202   gc->finalize = gst_ximage_src_finalize;
1203
1204   g_object_class_install_property (gc, PROP_DISPLAY_NAME,
1205       g_param_spec_string ("display-name", "Display", "X Display Name",
1206           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1207   g_object_class_install_property (gc, PROP_SHOW_POINTER,
1208       g_param_spec_boolean ("show-pointer", "Show Mouse Pointer",
1209           "Show mouse pointer (if XFixes extension enabled)", TRUE,
1210           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1211   /**
1212    * GstXImageSrc:use-damage:
1213    *
1214    * Use XDamage (if the XDamage extension is enabled)
1215    */
1216   g_object_class_install_property (gc, PROP_USE_DAMAGE,
1217       g_param_spec_boolean ("use-damage", "Use XDamage",
1218           "Use XDamage (if XDamage extension enabled)", TRUE,
1219           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1220   /**
1221    * GstXImageSrc:startx:
1222    *
1223    * X coordinate of top left corner of area to be recorded
1224    * (0 for top left of screen)
1225    */
1226   g_object_class_install_property (gc, PROP_STARTX,
1227       g_param_spec_uint ("startx", "Start X co-ordinate",
1228           "X coordinate of top left corner of area to be recorded (0 for top left of screen)",
1229           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1230   /**
1231    * GstXImageSrc:starty:
1232    *
1233    * Y coordinate of top left corner of area to be recorded
1234    * (0 for top left of screen)
1235    */
1236   g_object_class_install_property (gc, PROP_STARTY,
1237       g_param_spec_uint ("starty", "Start Y co-ordinate",
1238           "Y coordinate of top left corner of area to be recorded (0 for top left of screen)",
1239           0, G_MAXINT, 0, G_PARAM_READWRITE));
1240   /**
1241    * GstXImageSrc:endx:
1242    *
1243    * X coordinate of bottom right corner of area to be recorded
1244    * (0 for bottom right of screen)
1245    */
1246   g_object_class_install_property (gc, PROP_ENDX,
1247       g_param_spec_uint ("endx", "End X",
1248           "X coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1249           0, G_MAXINT, 0, G_PARAM_READWRITE));
1250   /**
1251    * GstXImageSrc:endy:
1252    *
1253    * Y coordinate of bottom right corner of area to be recorded
1254    * (0 for bottom right of screen)
1255    */
1256   g_object_class_install_property (gc, PROP_ENDY,
1257       g_param_spec_uint ("endy", "End Y",
1258           "Y coordinate of bottom right corner of area to be recorded (0 for bottom right of screen)",
1259           0, G_MAXINT, 0, G_PARAM_READWRITE));
1260
1261   /**
1262    * GstXImageSrc:remote:
1263    *
1264    * Whether the X display is remote. The element will try to use alternate calls
1265    * known to work better with remote displays.
1266    */
1267   g_object_class_install_property (gc, PROP_REMOTE,
1268       g_param_spec_boolean ("remote", "Remote display",
1269           "Whether the display is remote", FALSE,
1270           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1271   /**
1272    * GstXImageSrc:xid:
1273    *
1274    * The XID of the window to capture. 0 for the root window (default).
1275    */
1276   g_object_class_install_property (gc, PROP_XID,
1277       g_param_spec_uint64 ("xid", "Window XID",
1278           "Window XID to capture from", 0, G_MAXUINT64, 0,
1279           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1280   /**
1281    * GstXImageSrc:xname:
1282    *
1283    * The name of the window to capture, if any.
1284    */
1285   g_object_class_install_property (gc, PROP_XNAME,
1286       g_param_spec_string ("xname", "Window name",
1287           "Window name to capture from", NULL,
1288           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1289
1290   gst_element_class_set_static_metadata (ec, "Ximage video source",
1291       "Source/Video",
1292       "Creates a screenshot video stream",
1293       "Lutz Mueller <lutz@users.sourceforge.net>, "
1294       "Jan Schmidt <thaytan@mad.scientist.com>, "
1295       "Zaheer Merali <zaheerabbas at merali dot org>");
1296   gst_element_class_add_static_pad_template (ec, &t);
1297
1298   bc->fixate = gst_ximage_src_fixate;
1299   bc->get_caps = gst_ximage_src_get_caps;
1300   bc->set_caps = gst_ximage_src_set_caps;
1301   bc->start = gst_ximage_src_start;
1302   bc->stop = gst_ximage_src_stop;
1303   bc->unlock = gst_ximage_src_unlock;
1304   push_class->create = gst_ximage_src_create;
1305 }
1306
1307 static void
1308 gst_ximage_src_init (GstXImageSrc * ximagesrc)
1309 {
1310   gst_base_src_set_format (GST_BASE_SRC (ximagesrc), GST_FORMAT_TIME);
1311   gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
1312
1313   g_mutex_init (&ximagesrc->pool_lock);
1314   g_mutex_init (&ximagesrc->x_lock);
1315   ximagesrc->show_pointer = TRUE;
1316   ximagesrc->use_damage = TRUE;
1317   ximagesrc->startx = 0;
1318   ximagesrc->starty = 0;
1319   ximagesrc->endx = 0;
1320   ximagesrc->endy = 0;
1321   ximagesrc->endx_fit_to_screen = TRUE;
1322   ximagesrc->endy_fit_to_screen = TRUE;
1323   ximagesrc->remote = FALSE;
1324 }
1325
1326 static gboolean
1327 plugin_init (GstPlugin * plugin)
1328 {
1329   gboolean ret;
1330
1331   GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
1332       "ximagesrc element debug");
1333
1334   ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
1335       GST_TYPE_XIMAGE_SRC);
1336
1337   return ret;
1338 }
1339
1340 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1341     GST_VERSION_MINOR,
1342     ximagesrc,
1343     "X11 video input plugin using standard Xlib calls",
1344     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);