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