gstximagesrc.c 24.8 KB
Newer Older
1
/* GStreamer
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
2
3
 *
 * Copyright (C) 2006 Zaheer Merali <zaheerabbas at merali dot org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

21
22
23
24
25
26
27
28
29
/**
 * SECTION:element-ximagesrc
 * @short_description: a source that captures your X Display
 *
 * <refsect2>
 * <para>
 * This element captures your X Display and creates raw RGB video.  It uses
 * the XDamage extension if available to only capture areas of the screen that
 * have changed since the last frame.  It uses the XFixes extension if
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
30
31
 * available to also capture your mouse pointer.  By default it will fixate to
 * 25 frames per second.
32
33
34
35
 * </para>
 * <title>Example pipelines</title>
 * <para>
 * <programlisting>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
36
 * gst-launch -v ximagesrc ! video/x-raw-rgb,framerate=5/1 ! ffmpegcolorspace ! theoraenc ! oggmux ! filesink location=desktop.ogg
37
 * </programlisting>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
38
39
 * Encodes your X display to an Ogg theora video at 5 frames per second.
 * </para>
40
41
42
 * </refsect2>
 */

43
44
45
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
46
#include "gstximagesrc.h"
47
48
49
50
51
52
53

#include <string.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

54
55
56
#include <gst/gst.h>
#include <gst/gst-i18n-plugin.h>

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
57
58
GST_DEBUG_CATEGORY_STATIC (gst_debug_ximage_src);
#define GST_CAT_DEFAULT gst_debug_ximage_src
59
60

/* elementfactory information */
61
static const GstElementDetails ximagesrc_details =
62
63
64
GST_ELEMENT_DETAILS ("Ximage video source",
    "Source/Video",
    "Creates a screenshot video stream",
65
66
    "Lutz Mueller <lutz@users.sourceforge.net>, "
    "Jan Schmidt <thaytan@mad.scientist.com>, "
67
    "Zaheer Merali <zaheerabbas at merali dot org>");
68
69
70
71
72

static GstStaticPadTemplate t =
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-rgb, "
        "framerate = (fraction) [ 0, MAX ], "
73
74
        "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
        "pixel-aspect-ratio = (fraction) [ 0, MAX ]"));
75
76
77
78
79

enum
{
  PROP_0,
  PROP_DISPLAY_NAME,
80
81
  PROP_SCREEN_NUM,
  PROP_SHOW_POINTER
82
83
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
84
GST_BOILERPLATE (GstXImageSrc, gst_ximage_src, GstPushSrc, GST_TYPE_PUSH_SRC);
85

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
86
87
static void gst_ximage_src_fixate (GstPad * pad, GstCaps * caps);
static void gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc);
88
89
90

/* Called when a buffer is returned from the pipeline */
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
91
92
gst_ximage_src_return_buf (GstXImageSrc * ximagesrc,
    GstXImageSrcBuffer * ximage)
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
  /* If our geometry changed we can't reuse that image. */
  if ((ximage->width != ximagesrc->width) ||
      (ximage->height != ximagesrc->height)) {
    GST_DEBUG_OBJECT (ximagesrc,
        "destroy image %p as its size changed %dx%d vs current %dx%d",
        ximage, ximage->width, ximage->height,
        ximagesrc->width, ximagesrc->height);
    g_mutex_lock (ximagesrc->x_lock);
    gst_ximageutil_ximage_destroy (ximagesrc->xcontext, ximage);
    g_mutex_unlock (ximagesrc->x_lock);
  } else {
    /* In that case we can reuse the image and add it to our image pool. */
    GST_LOG_OBJECT (ximagesrc, "recycling image %p in pool", ximage);
    /* need to increment the refcount again to recycle */
    gst_buffer_ref (GST_BUFFER (ximage));
    g_mutex_lock (ximagesrc->pool_lock);
    ximagesrc->buffer_pool = g_slist_prepend (ximagesrc->buffer_pool, ximage);
    g_mutex_unlock (ximagesrc->pool_lock);
  }
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
gst_ximage_src_open_display (GstXImageSrc * s, const gchar * name)
117
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118
  g_return_val_if_fail (GST_IS_XIMAGE_SRC (s), FALSE);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

  if (s->xcontext != NULL)
    return TRUE;

  g_mutex_lock (s->x_lock);
  s->xcontext = ximageutil_xcontext_get (GST_ELEMENT (s), name);
  s->width = s->xcontext->width;
  s->height = s->xcontext->height;

  /* Always capture root window, for now */
  s->xwindow = s->xcontext->root;

#ifdef HAVE_XFIXES
  /* check if xfixes supported */
  {
    int error_base;

    if (XFixesQueryExtension (s->xcontext->disp, &s->fixes_event_base,
            &error_base)) {
      s->have_xfixes = TRUE;
      GST_DEBUG_OBJECT (s, "X Server supports XFixes");
    } else {
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
      GST_DEBUG_OBJECT (s, "X Server does not support XFixes");
    }
  }

#ifdef HAVE_XDAMAGE
  /* check if xdamage is supported */
  {
    int error_base;
    long evmask = NoEventMask;

    if (XDamageQueryExtension (s->xcontext->disp, &s->damage_event_base,
            &error_base)) {
      s->damage =
          XDamageCreate (s->xcontext->disp, s->xwindow,
          XDamageReportRawRectangles);
      if (s->damage != None) {
        s->damage_region = XFixesCreateRegion (s->xcontext->disp, NULL, 0);
        if (s->damage_region != None) {
          XGCValues values;

          GST_DEBUG_OBJECT (s, "Using XDamage extension");
          values.subwindow_mode = IncludeInferiors;
          s->damage_copy_gc = XCreateGC (s->xcontext->disp,
              s->xwindow, GCSubwindowMode, &values);
          XSelectInput (s->xcontext->disp, s->xwindow, evmask);

          s->have_xdamage = TRUE;
        } else {
          XDamageDestroy (s->xcontext->disp, s->damage);
          s->damage = None;
        }
      } else
        GST_DEBUG_OBJECT (s, "Could not attach to XDamage");
    } else {
      GST_DEBUG_OBJECT (s, "X Server does not have XDamage extension");
    }
  }
#endif
#endif

  g_mutex_unlock (s->x_lock);

  if (s->xcontext == NULL)
    return FALSE;

  return TRUE;
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
191
gst_ximage_src_start (GstBaseSrc * basesrc)
192
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
193
  GstXImageSrc *s = GST_XIMAGE_SRC (basesrc);
194
195
196

  s->last_frame_no = -1;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
197
  return gst_ximage_src_open_display (s, NULL);
198
199
200
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
201
gst_ximage_src_stop (GstBaseSrc * basesrc)
202
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
203
  GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
204

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
205
  gst_ximage_src_clear_bufpool (src);
206
207
208
209
210
211
212
213
214
215
216
217

  if (src->xcontext) {
    g_mutex_lock (src->x_lock);
    ximageutil_xcontext_clear (src->xcontext);
    src->xcontext = NULL;
    g_mutex_unlock (src->x_lock);
  }

  return TRUE;
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
218
gst_ximage_src_unlock (GstBaseSrc * basesrc)
219
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
220
  GstXImageSrc *src = GST_XIMAGE_SRC (basesrc);
221
222
223
224
225
226
227
228
229
230
231
232
233

  /* Awaken the create() func if it's waiting on the clock */
  GST_OBJECT_LOCK (src);
  if (src->clock_id) {
    GST_DEBUG_OBJECT (src, "Waking up waiting clock");
    gst_clock_id_unschedule (src->clock_id);
  }
  GST_OBJECT_UNLOCK (src);

  return TRUE;
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
234
gst_ximage_src_recalc (GstXImageSrc * src)
235
236
237
238
{
  if (!src->xcontext)
    return FALSE;

239
  /* Maybe later we can check the display hasn't changed size */
240
241
242
243
244
245
246
247
248
249
250
251
  /* We could use XQueryPointer to get only the current window. */
  return TRUE;
}

#ifdef HAVE_XFIXES
static void
composite_pixel (GstXContext * xcontext, guchar * dest, guchar * src)
{
  guint8 r = src[2];
  guint8 g = src[1];
  guint8 b = src[0];
  guint8 a = src[3];
252
  guint8 dr, dg, db;
253
  guint32 color;
254
255
256
  gint r_shift, r_max, r_shift_out;
  gint g_shift, g_max, g_shift_out;
  gint b_shift, b_max, b_shift_out;
257
258
259
260
261
262
263
264
265
266
267
268

  switch (xcontext->bpp) {
    case 8:
      color = *dest;
      break;
    case 16:
      color = GUINT16_FROM_LE (*(guint32 *) (dest));
      break;
    case 32:
      color = GUINT32_FROM_LE (*(guint32 *) (dest));
      break;
    default:
269
270
      /* Should not reach here */
      g_assert_not_reached ();
271
272
  }

273
274
  /* possible optimisation:
   * move the code that finds shift and max in the _link function */
275
276
277
278
279
  for (r_shift = 0; !(xcontext->visual->red_mask & (1 << r_shift)); r_shift++);
  for (g_shift = 0; !(xcontext->visual->green_mask & (1 << g_shift));
      g_shift++);
  for (b_shift = 0; !(xcontext->visual->blue_mask & (1 << b_shift)); b_shift++);

280
281
282
283
284
285
286
287
  for (r_shift_out = 0; !(xcontext->visual->red_mask & (1 << r_shift_out));
      r_shift_out++);
  for (g_shift_out = 0; !(xcontext->visual->green_mask & (1 << g_shift_out));
      g_shift_out++);
  for (b_shift_out = 0; !(xcontext->visual->blue_mask & (1 << b_shift_out));
      b_shift_out++);


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  r_max = (xcontext->visual->red_mask >> r_shift);
  b_max = (xcontext->visual->blue_mask >> b_shift);
  g_max = (xcontext->visual->green_mask >> g_shift);

#define RGBXXX_R(x)  (((x)>>r_shift) & (r_max))
#define RGBXXX_G(x)  (((x)>>g_shift) & (g_max))
#define RGBXXX_B(x)  (((x)>>b_shift) & (b_max))

  dr = (RGBXXX_R (color) * 255) / r_max;
  dg = (RGBXXX_G (color) * 255) / g_max;
  db = (RGBXXX_B (color) * 255) / b_max;

  dr = (r * a + (0xff - a) * dr) / 0xff;
  dg = (g * a + (0xff - a) * dg) / 0xff;
  db = (b * a + (0xff - a) * db) / 0xff;

304
305
306
  color = (((dr * r_max) / 255) << r_shift_out) +
      (((dg * g_max) / 255) << g_shift_out) +
      (((db * b_max) / 255) << b_shift_out);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

  switch (xcontext->bpp) {
    case 8:
      *dest = color;
      break;
    case 16:
      *(guint16 *) (dest) = color;
      break;
    case 32:
      *(guint32 *) (dest) = color;
      break;
    default:
      g_warning ("bpp %d not supported\n", xcontext->bpp);
  }
}
#endif

/* Retrieve an XImageSrcBuffer, preferably from our
 * pool of existing images and populate it from the window */
static GstXImageSrcBuffer *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
327
gst_ximage_src_ximage_get (GstXImageSrc * ximagesrc)
328
329
{
  GstXImageSrcBuffer *ximage = NULL;
330
  GstCaps *caps = NULL;
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

  g_mutex_lock (ximagesrc->pool_lock);
  while (ximagesrc->buffer_pool != NULL) {
    ximage = ximagesrc->buffer_pool->data;

    if ((ximage->width != ximagesrc->width) ||
        (ximage->height != ximagesrc->height)) {
      gst_ximage_buffer_free (ximage);
    }

    ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
        ximagesrc->buffer_pool);
  }
  g_mutex_unlock (ximagesrc->pool_lock);

  if (ximage == NULL) {
    GstXContext *xcontext;

    GST_DEBUG_OBJECT (ximagesrc, "creating image (%dx%d)",
        ximagesrc->width, ximagesrc->height);

    g_mutex_lock (ximagesrc->x_lock);
    ximage = gst_ximageutil_ximage_new (ximagesrc->xcontext,
        GST_ELEMENT (ximagesrc), ximagesrc->width, ximagesrc->height,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
355
        (BufferReturnFunc) (gst_ximage_src_return_buf));
356
357
358
359
360
361
362
363
364
    if (ximage == NULL) {
      GST_ELEMENT_ERROR (ximagesrc, RESOURCE, WRITE, (NULL),
          ("could not create a %dx%d ximage", ximage->width, ximage->height));
      g_mutex_unlock (ximagesrc->x_lock);
      return NULL;
    }

    xcontext = ximagesrc->xcontext;

365
366
367
368
369
370
371
372
373
374
    caps = gst_caps_new_simple ("video/x-raw-rgb",
        "bpp", G_TYPE_INT, xcontext->bpp,
        "depth", G_TYPE_INT, xcontext->depth,
        "endianness", G_TYPE_INT, xcontext->endianness,
        "red_mask", G_TYPE_INT, xcontext->r_mask_output,
        "green_mask", G_TYPE_INT, xcontext->g_mask_output,
        "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
        "width", G_TYPE_INT, xcontext->width,
        "height", G_TYPE_INT, xcontext->height,
        "framerate", GST_TYPE_FRACTION, ximagesrc->fps_n, ximagesrc->fps_d,
375
376
377
        "pixel-aspect-ratio", GST_TYPE_FRACTION,
        gst_value_get_fraction_numerator (xcontext->par),
        gst_value_get_fraction_denominator (xcontext->par), NULL);
378
379

    gst_buffer_set_caps (GST_BUFFER (ximage), caps);
380
381
382
    g_mutex_unlock (ximagesrc->x_lock);
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
383
  g_return_val_if_fail (GST_IS_XIMAGE_SRC (ximagesrc), NULL);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

#ifdef HAVE_XDAMAGE
  if (ximagesrc->have_xdamage) {
    XEvent ev;

    GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XDamage");

    do {
      XNextEvent (ximagesrc->xcontext->disp, &ev);
      if (ev.type == ximagesrc->damage_event_base + XDamageNotify) {
        XDamageNotifyEvent *dev = (XDamageNotifyEvent *) & ev;

#ifdef HAVE_XSHM
        if (ximagesrc->xcontext->use_xshm &&
            dev->area.width == ximagesrc->width &&
            dev->area.height == ximagesrc->height) {
          GST_DEBUG_OBJECT (ximagesrc, "Entire screen was damaged");
          XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
              ximage->ximage, 0, 0, AllPlanes);
          /* No need to collect more events */
          while (XPending (ximagesrc->xcontext->disp)) {
            XNextEvent (ximagesrc->xcontext->disp, &ev);
          }
          break;
        } else
#endif
        {
          GST_LOG_OBJECT (ximagesrc,
              "Retrieving damaged sub-region @ %d,%d size %dx%d",
              dev->area.x, dev->area.y, dev->area.width, dev->area.height);

          XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
              dev->area.x, dev->area.y,
              dev->area.width, dev->area.height,
              AllPlanes, ZPixmap, ximage->ximage, dev->area.x, dev->area.y);
        }
      }
    } while (XPending (ximagesrc->xcontext->disp));
    XDamageSubtract (ximagesrc->xcontext->disp, ximagesrc->damage, None, None);
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#ifdef HAVE_XFIXES
    /* re-get area where last mouse pointer was */
    if (ximagesrc->cursor_image) {
      gint x, y, width, height;

      x = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
      y = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
      width = ximagesrc->cursor_image->width;
      height = ximagesrc->cursor_image->height;

      /* bounds checking */
      if (x < 0)
        x = 0;
      if (y < 0)
        y = 0;
      if (x + width > ximagesrc->width)
        width = ximagesrc->width - x;
      if (y + height > ximagesrc->height)
        height = ximagesrc->height - y;
      g_assert (x >= 0);
      g_assert (y >= 0);

      GST_DEBUG_OBJECT (ximagesrc, "Removing cursor from %d,%d", x, y);
      XGetSubImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
          x, y, width, height, AllPlanes, ZPixmap, ximage->ximage, x, y);
    }
#endif


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  } else {
#endif

#ifdef HAVE_XSHM
    if (ximagesrc->xcontext->use_xshm) {
      GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XShm");
      XShmGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
          ximage->ximage, 0, 0, AllPlanes);

    } else
#endif /* HAVE_XSHM */
    {
      GST_DEBUG_OBJECT (ximagesrc, "Retrieving screen using XGetImage");
      ximage->ximage = XGetImage (ximagesrc->xcontext->disp, ximagesrc->xwindow,
          0, 0, ximagesrc->width, ximagesrc->height, AllPlanes, ZPixmap);
    }
#ifdef HAVE_XDAMAGE
  }
#endif

#ifdef HAVE_XFIXES
  if (ximagesrc->show_pointer && ximagesrc->have_xfixes) {

    GST_DEBUG_OBJECT (ximagesrc, "Using XFixes to draw cursor");
    /* get cursor */
477
478
    ximagesrc->cursor_image = XFixesGetCursorImage (ximagesrc->xcontext->disp);
    if (ximagesrc->cursor_image != NULL) {
479
480
      int cx, cy, i, j, count;

481
      cx = ximagesrc->cursor_image->x - ximagesrc->cursor_image->xhot;
482
483
      if (cx < 0)
        cx = 0;
484
      cy = ximagesrc->cursor_image->y - ximagesrc->cursor_image->yhot;
485
486
      if (cy < 0)
        cy = 0;
487
      count = ximagesrc->cursor_image->width * ximagesrc->cursor_image->height;
488
      for (i = 0; i < count; i++)
489
490
        ximagesrc->cursor_image->pixels[i] =
            GUINT_TO_LE (ximagesrc->cursor_image->pixels[i]);
491
492

      /* copy those pixels across */
493
494
495
496
497
498
      for (j = cy;
          j < cy + ximagesrc->cursor_image->height && j < ximagesrc->height;
          j++) {
        for (i = cx;
            i < cx + ximagesrc->cursor_image->width && i < ximagesrc->width;
            i++) {
499
500
501
          guint8 *src, *dest;

          src =
502
503
              (guint8 *) & (ximagesrc->cursor_image->pixels[((j -
                          cy) * ximagesrc->cursor_image->width + (i - cx))]);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
          dest =
              (guint8 *) & (ximage->ximage->data[(j * ximagesrc->width +
                      i) * (ximagesrc->xcontext->bpp / 8)]);

          composite_pixel (ximagesrc->xcontext, (guint8 *) dest,
              (guint8 *) src);
        }
      }
    }
  }
#endif

  return ximage;
}

static GstFlowReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
520
gst_ximage_src_create (GstPushSrc * bs, GstBuffer ** buf)
521
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
522
  GstXImageSrc *s = GST_XIMAGE_SRC (bs);
523
524
525
526
527
528
  GstXImageSrcBuffer *image;
  GstClockTime base_time;
  GstClockTime next_capture_ts;
  GstClockTime dur;
  gint64 next_frame_no;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
529
  if (!gst_ximage_src_recalc (s)) {
530
    GST_ELEMENT_ERROR (s, RESOURCE, FAILED,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
531
        (_("Changing resolution at runtime is not yet supported.")), (NULL));
532
533
534
535
536
537
    return GST_FLOW_ERROR;
  }

  if (s->fps_n <= 0 || s->fps_d <= 0)
    return GST_FLOW_NOT_NEGOTIATED;     /* FPS must be > 0 */

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
538
  /* Now, we might need to wait for the next multiple of the fps
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
   * before capturing */

  GST_OBJECT_LOCK (s);
  base_time = GST_ELEMENT_CAST (s)->base_time;
  next_capture_ts = gst_clock_get_time (GST_ELEMENT_CLOCK (s));
  next_capture_ts -= base_time;

  /* Figure out which 'frame number' position we're at, based on the cur time
   * and frame rate */
  next_frame_no = gst_util_uint64_scale (next_capture_ts,
      s->fps_n, GST_SECOND * s->fps_d);
  if (next_frame_no == s->last_frame_no) {
    GstClockID id;
    GstClockReturn ret;

    /* Need to wait for the next frame */
    next_frame_no += 1;

    /* Figure out what the next frame time is */
    next_capture_ts = gst_util_uint64_scale (next_frame_no,
        s->fps_d * GST_SECOND, s->fps_n);

    id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (s),
        next_capture_ts + base_time);
    s->clock_id = id;

    /* release the object lock while waiting */
    GST_OBJECT_UNLOCK (s);

    GST_DEBUG_OBJECT (s, "Waiting for next frame time %" G_GUINT64_FORMAT,
        next_capture_ts);
    ret = gst_clock_id_wait (id, NULL);
    GST_OBJECT_LOCK (s);

    gst_clock_id_unref (id);
    s->clock_id = NULL;
    if (ret == GST_CLOCK_UNSCHEDULED) {
      /* Got woken up by the unlock function */
      GST_OBJECT_UNLOCK (s);
      return GST_FLOW_WRONG_STATE;
    }
    /* Duration is a complete 1/fps frame duration */
    dur = gst_util_uint64_scale_int (GST_SECOND, s->fps_d, s->fps_n);
  } else {
    GstClockTime next_frame_ts;

    GST_DEBUG_OBJECT (s, "No need to wait for next frame time %"
        G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
        G_GINT64_FORMAT, next_capture_ts, next_frame_no, s->last_frame_no);
    next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
        s->fps_d * GST_SECOND, s->fps_n);
    /* Frame duration is from now until the next expected capture time */
    dur = next_frame_ts - next_capture_ts;
  }
  s->last_frame_no = next_frame_no;
  GST_OBJECT_UNLOCK (s);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
596
  image = gst_ximage_src_ximage_get (s);
597
598
599
600
601
602
603
604
605
606
607
  if (!image)
    return GST_FLOW_ERROR;

  *buf = GST_BUFFER (image);
  GST_BUFFER_TIMESTAMP (*buf) = next_capture_ts;
  GST_BUFFER_DURATION (*buf) = dur;

  return GST_FLOW_OK;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
608
gst_ximage_src_set_property (GObject * object, guint prop_id,
609
610
    const GValue * value, GParamSpec * pspec)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
611
  GstXImageSrc *src = GST_XIMAGE_SRC (object);
612
613
614
615
616
617
618
619
620
621

  switch (prop_id) {
    case PROP_DISPLAY_NAME:

      g_free (src->display_name);
      src->display_name = g_strdup (g_value_get_string (value));
      break;
    case PROP_SCREEN_NUM:
      src->screen_num = g_value_get_uint (value);
      break;
622
623
624
    case PROP_SHOW_POINTER:
      src->show_pointer = g_value_get_boolean (value);
      break;
625
626
627
628
629
630
    default:
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
631
gst_ximage_src_get_property (GObject * object, guint prop_id, GValue * value,
632
633
    GParamSpec * pspec)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
634
  GstXImageSrc *src = GST_XIMAGE_SRC (object);
635
636
637
638
639
640
641
642
643
644
645
646

  switch (prop_id) {
    case PROP_DISPLAY_NAME:
      if (src->xcontext)
        g_value_set_string (value, DisplayString (src->xcontext->disp));
      else
        g_value_set_string (value, src->display_name);

      break;
    case PROP_SCREEN_NUM:
      g_value_set_uint (value, src->screen_num);
      break;
647
648
649
    case PROP_SHOW_POINTER:
      g_value_set_boolean (value, src->show_pointer);
      break;
650
651
652
653
654
655
656
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
657
gst_ximage_src_clear_bufpool (GstXImageSrc * ximagesrc)
658
659
660
661
662
663
664
665
666
667
668
669
670
671
{
  g_mutex_lock (ximagesrc->pool_lock);
  while (ximagesrc->buffer_pool != NULL) {
    GstXImageSrcBuffer *ximage = ximagesrc->buffer_pool->data;

    gst_ximage_buffer_free (ximage);

    ximagesrc->buffer_pool = g_slist_delete_link (ximagesrc->buffer_pool,
        ximagesrc->buffer_pool);
  }
  g_mutex_unlock (ximagesrc->pool_lock);
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
672
gst_ximage_src_base_init (gpointer g_class)
673
674
675
676
677
678
679
680
{
  GstElementClass *ec = GST_ELEMENT_CLASS (g_class);

  gst_element_class_set_details (ec, &ximagesrc_details);
  gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t));
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
681
gst_ximage_src_dispose (GObject * object)
682
683
{
  /* Drop references in the buffer_pool */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
684
  gst_ximage_src_clear_bufpool (GST_XIMAGE_SRC (object));
685
686
687
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
688
gst_ximage_src_finalize (GObject * object)
689
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
690
  GstXImageSrc *src = GST_XIMAGE_SRC (object);
691
692
693
694
695
696
697
698
699
700
701

  if (src->xcontext)
    ximageutil_xcontext_clear (src->xcontext);

  g_mutex_free (src->pool_lock);
  g_mutex_free (src->x_lock);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static GstCaps *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
702
gst_ximage_src_get_caps (GstBaseSrc * bs)
703
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
704
  GstXImageSrc *s = GST_XIMAGE_SRC (bs);
705
706
  GstXContext *xcontext;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
707
  if ((!s->xcontext) && (!gst_ximage_src_open_display (s, NULL)))
708
709
710
    return gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC (s)->
            srcpad));

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
711
  if (!gst_ximage_src_recalc (s))
712
713
714
715
716
717
718
719
720
    return gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC (s)->
            srcpad));

  xcontext = s->xcontext;

  return gst_caps_new_simple ("video/x-raw-rgb",
      "bpp", G_TYPE_INT, xcontext->bpp,
      "depth", G_TYPE_INT, xcontext->depth,
      "endianness", G_TYPE_INT, xcontext->endianness,
721
722
723
      "red_mask", G_TYPE_INT, xcontext->r_mask_output,
      "green_mask", G_TYPE_INT, xcontext->g_mask_output,
      "blue_mask", G_TYPE_INT, xcontext->b_mask_output,
724
725
      "width", G_TYPE_INT, xcontext->width,
      "height", G_TYPE_INT, xcontext->height,
726
727
728
      "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
      "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1,
      NULL);
729
730
731
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
732
gst_ximage_src_set_caps (GstBaseSrc * bs, GstCaps * caps)
733
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
734
  GstXImageSrc *s = GST_XIMAGE_SRC (bs);
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
  GstStructure *structure;
  const GValue *new_fps;

  /* If not yet opened, disallow setcaps until later */
  if (!s->xcontext)
    return FALSE;

  /* The only thing that can change is the framerate downstream wants */
  structure = gst_caps_get_structure (caps, 0);
  new_fps = gst_structure_get_value (structure, "framerate");
  if (!new_fps)
    return FALSE;

  /* Store this FPS for use when generating buffers */
  s->fps_n = gst_value_get_fraction_numerator (new_fps);
  s->fps_d = gst_value_get_fraction_denominator (new_fps);

  GST_DEBUG_OBJECT (s, "peer wants %d/%d fps", s->fps_n, s->fps_d);

  return TRUE;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
758
gst_ximage_src_fixate (GstPad * pad, GstCaps * caps)
759
760
761
762
763
764
765
766
767
768
769
770
{
  gint i;
  GstStructure *structure;

  for (i = 0; i < gst_caps_get_size (caps); ++i) {
    structure = gst_caps_get_structure (caps, i);

    gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
771
gst_ximage_src_class_init (GstXImageSrcClass * klass)
772
773
774
775
776
{
  GObjectClass *gc = G_OBJECT_CLASS (klass);
  GstBaseSrcClass *bc = GST_BASE_SRC_CLASS (klass);
  GstPushSrcClass *push_class = GST_PUSH_SRC_CLASS (klass);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
777
778
779
780
  gc->set_property = gst_ximage_src_set_property;
  gc->get_property = gst_ximage_src_get_property;
  gc->dispose = gst_ximage_src_dispose;
  gc->finalize = gst_ximage_src_finalize;
781
782

  g_object_class_install_property (gc, PROP_DISPLAY_NAME,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
783
      g_param_spec_string ("display_name", "Display", "X Display Name", NULL,
784
785
          G_PARAM_READWRITE));
  g_object_class_install_property (gc, PROP_SCREEN_NUM,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
786
      g_param_spec_uint ("screen_num", "Screen number", "X Screen Number",
787
          0, G_MAXINT, 0, G_PARAM_READWRITE));
788
789
  g_object_class_install_property (gc, PROP_SHOW_POINTER,
      g_param_spec_boolean ("show_pointer", "Show Mouse Pointer",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
790
          "Show mouse pointer (if XFixes extension enabled)", TRUE,
791
          G_PARAM_READWRITE));
792

793
  parent_class = g_type_class_peek_parent (klass);
794

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
795
796
797
798
799
800
  push_class->create = gst_ximage_src_create;
  bc->get_caps = gst_ximage_src_get_caps;
  bc->set_caps = gst_ximage_src_set_caps;
  bc->start = gst_ximage_src_start;
  bc->stop = gst_ximage_src_stop;
  bc->unlock = gst_ximage_src_unlock;
801
802
803
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
804
gst_ximage_src_init (GstXImageSrc * ximagesrc, GstXImageSrcClass * klass)
805
806
807
{
  gst_base_src_set_live (GST_BASE_SRC (ximagesrc), TRUE);
  gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (ximagesrc),
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
808
      gst_ximage_src_fixate);
809
810
811

  ximagesrc->pool_lock = g_mutex_new ();
  ximagesrc->x_lock = g_mutex_new ();
812
  ximagesrc->show_pointer = TRUE;
813
814
815
816
817
818
819
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  gboolean ret;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
820
  GST_DEBUG_CATEGORY_INIT (gst_debug_ximage_src, "ximagesrc", 0,
821
822
823
      "ximagesrc element debug");

  ret = gst_element_register (plugin, "ximagesrc", GST_RANK_NONE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
824
      GST_TYPE_XIMAGE_SRC);
825
826
827
828
829
830
831

  return ret;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "ximagesrc",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
832
833
    "X11 video input plugin using standard Xlib calls",
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);