gstvideotestsrc.c 18.1 KB
Newer Older
1
2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Andy Wingo's avatar
Andy Wingo committed
3
 * Copyright (C) <2002> David A. Schleef <ds@schleef.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
/**
 * SECTION:element-videotestsrc
 *
 * <refsect2>
 * <title>Example launch line</title>
 * <para>
Wim Taymans's avatar
Wim Taymans committed
27
28
29
30
31
 * The videotestsrc element is used to produce test video data in a wide variaty
 * of formats. The video test data produced can be controlled with the "pattern"
 * property.
 * </para>
 * <para>
32
33
34
 * <programlisting>
 * gst-launch -v videotestsrc pattern=snow ! ximagesink
 * </programlisting>
Wim Taymans's avatar
Wim Taymans committed
35
 * Shows random noise in an X window.
36
37
38
 * </para>
 * </refsect2>
 */
Andy Wingo's avatar
Andy Wingo committed
39

40
41
42
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
43
44
#include "gstvideotestsrc.h"
#include "videotestsrc.h"
45

Steve Baker's avatar
Steve Baker committed
46
#include <string.h>
47
#include <stdlib.h>
48
#include <liboil/liboil.h>
49

50
#define USE_PEER_BUFFERALLOC
Andy Wingo's avatar
Andy Wingo committed
51

52
GST_DEBUG_CATEGORY_STATIC (video_test_src_debug);
53
#define GST_CAT_DEFAULT video_test_src_debug
54

Andy Wingo's avatar
Andy Wingo committed
55

Stefan Kost's avatar
Stefan Kost committed
56
static const GstElementDetails video_test_src_details =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
57
58
59
60
GST_ELEMENT_DETAILS ("Video test source",
    "Source/Video",
    "Creates a test video stream",
    "David A. Schleef <ds@schleef.org>");
61
62


63
64
enum
{
Andy Wingo's avatar
Andy Wingo committed
65
66
67
  PROP_0,
  PROP_PATTERN,
  PROP_TIMESTAMP_OFFSET,
68
69
  PROP_IS_LIVE
      /* FILL ME */
70
71
};

72

73
GST_BOILERPLATE (GstVideoTestSrc, gst_video_test_src, GstPushSrc,
Andy Wingo's avatar
Andy Wingo committed
74
    GST_TYPE_PUSH_SRC);
75

Andy Wingo's avatar
Andy Wingo committed
76

77
static void gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78
    int pattern_type);
79
static void gst_video_test_src_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80
    const GValue * value, GParamSpec * pspec);
81
static void gst_video_test_src_get_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
82
    GValue * value, GParamSpec * pspec);
83

84
85
86
static GstCaps *gst_video_test_src_getcaps (GstBaseSrc * bsrc);
static gboolean gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
static void gst_video_test_src_src_fixate (GstPad * pad, GstCaps * caps);
87

88
89
90
91
static gboolean gst_video_test_src_is_seekable (GstBaseSrc * psrc);
static gboolean gst_video_test_src_do_seek (GstBaseSrc * bsrc,
    GstSegment * segment);
static gboolean gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query);
92

93
static void gst_video_test_src_get_times (GstBaseSrc * basesrc,
94
    GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
95
static GstFlowReturn gst_video_test_src_create (GstPushSrc * psrc,
96
    GstBuffer ** buffer);
97
static gboolean gst_video_test_src_start (GstBaseSrc * basesrc);
98

99
#define GST_TYPE_VIDEO_TEST_SRC_PATTERN (gst_video_test_src_pattern_get_type ())
100
static GType
101
gst_video_test_src_pattern_get_type (void)
102
{
103
  static GType video_test_src_pattern_type = 0;
104
  static const GEnumValue pattern_types[] = {
105
106
107
    {GST_VIDEO_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
    {GST_VIDEO_TEST_SRC_SNOW, "Random (television snow)", "snow"},
    {GST_VIDEO_TEST_SRC_BLACK, "100% Black", "black"},
108
109
110
111
    {GST_VIDEO_TEST_SRC_WHITE, "100% White", "white"},
    {GST_VIDEO_TEST_SRC_RED, "Red", "red"},
    {GST_VIDEO_TEST_SRC_GREEN, "Green", "green"},
    {GST_VIDEO_TEST_SRC_BLUE, "Blue", "blue"},
112
113
114
115
    {GST_VIDEO_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
    {GST_VIDEO_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
    {GST_VIDEO_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
    {GST_VIDEO_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
116
    {0, NULL, NULL}
117
118
  };

119
120
  if (!video_test_src_pattern_type) {
    video_test_src_pattern_type =
Andy Wingo's avatar
Andy Wingo committed
121
        g_enum_register_static ("GstVideoTestSrcPattern", pattern_types);
122
  }
123
  return video_test_src_pattern_type;
124
125
}

Benjamin Otte's avatar
Benjamin Otte committed
126
static void
127
gst_video_test_src_base_init (gpointer g_class)
Benjamin Otte's avatar
Benjamin Otte committed
128
129
130
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

131
  gst_element_class_set_details (element_class, &video_test_src_details);
Benjamin Otte's avatar
Benjamin Otte committed
132
133

  gst_element_class_add_pad_template (element_class,
Andy Wingo's avatar
Andy Wingo committed
134
      gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
135
          gst_video_test_src_getcaps (NULL)));
Benjamin Otte's avatar
Benjamin Otte committed
136
}
Andy Wingo's avatar
Andy Wingo committed
137

138
static void
139
gst_video_test_src_class_init (GstVideoTestSrcClass * klass)
140
141
{
  GObjectClass *gobject_class;
142
143
  GstBaseSrcClass *gstbasesrc_class;
  GstPushSrcClass *gstpushsrc_class;
144

145
  gobject_class = (GObjectClass *) klass;
146
147
  gstbasesrc_class = (GstBaseSrcClass *) klass;
  gstpushsrc_class = (GstPushSrcClass *) klass;
148

149
150
  gobject_class->set_property = gst_video_test_src_set_property;
  gobject_class->get_property = gst_video_test_src_get_property;
151

152
  g_object_class_install_property (gobject_class, PROP_PATTERN,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
153
      g_param_spec_enum ("pattern", "Pattern",
154
155
          "Type of test pattern to generate", GST_TYPE_VIDEO_TEST_SRC_PATTERN,
          1, G_PARAM_READWRITE));
156
  g_object_class_install_property (gobject_class,
Andy Wingo's avatar
Andy Wingo committed
157
158
159
160
      PROP_TIMESTAMP_OFFSET, g_param_spec_int64 ("timestamp-offset",
          "Timestamp offset",
          "An offset added to timestamps set on buffers (in ns)", G_MININT64,
          G_MAXINT64, 0, G_PARAM_READWRITE));
161
162
163
  g_object_class_install_property (gobject_class, PROP_IS_LIVE,
      g_param_spec_boolean ("is-live", "Is Live",
          "Whether to act as a live source", FALSE, G_PARAM_READWRITE));
164

165
166
  gstbasesrc_class->get_caps = gst_video_test_src_getcaps;
  gstbasesrc_class->set_caps = gst_video_test_src_setcaps;
167
168
169
  gstbasesrc_class->is_seekable = gst_video_test_src_is_seekable;
  gstbasesrc_class->do_seek = gst_video_test_src_do_seek;
  gstbasesrc_class->query = gst_video_test_src_query;
170
  gstbasesrc_class->get_times = gst_video_test_src_get_times;
171
  gstbasesrc_class->start = gst_video_test_src_start;
172

173
  gstpushsrc_class->create = gst_video_test_src_create;
174
175
}

Andy Wingo's avatar
Andy Wingo committed
176
static void
177
gst_video_test_src_init (GstVideoTestSrc * src, GstVideoTestSrcClass * g_class)
David Schleef's avatar
David Schleef committed
178
{
179
180
  GstPad *pad = GST_BASE_SRC_PAD (src);

181
  gst_pad_set_fixatecaps_function (pad, gst_video_test_src_src_fixate);
182

183
  gst_video_test_src_set_pattern (src, GST_VIDEO_TEST_SRC_SMPTE);
David Schleef's avatar
David Schleef committed
184

185
186
  src->timestamp_offset = 0;

187
188
  /* we operate in time */
  gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
189
  gst_base_src_set_live (GST_BASE_SRC (src), FALSE);
Andy Wingo's avatar
Andy Wingo committed
190
}
David Schleef's avatar
David Schleef committed
191

192
static void
193
gst_video_test_src_src_fixate (GstPad * pad, GstCaps * caps)
194
195
196
197
198
{
  GstStructure *structure;

  structure = gst_caps_get_structure (caps, 0);

199
200
  gst_structure_fixate_field_nearest_int (structure, "width", 320);
  gst_structure_fixate_field_nearest_int (structure, "height", 240);
Jan Schmidt's avatar
Jan Schmidt committed
201
  gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
202
203
}

Andy Wingo's avatar
Andy Wingo committed
204
static void
205
206
gst_video_test_src_set_pattern (GstVideoTestSrc * videotestsrc,
    int pattern_type)
Andy Wingo's avatar
Andy Wingo committed
207
208
{
  videotestsrc->pattern_type = pattern_type;
David Schleef's avatar
David Schleef committed
209

Andy Wingo's avatar
Andy Wingo committed
210
  GST_DEBUG_OBJECT (videotestsrc, "setting pattern to %d", pattern_type);
David Schleef's avatar
David Schleef committed
211

Andy Wingo's avatar
Andy Wingo committed
212
  switch (pattern_type) {
213
214
    case GST_VIDEO_TEST_SRC_SMPTE:
      videotestsrc->make_image = gst_video_test_src_smpte;
Andy Wingo's avatar
Andy Wingo committed
215
      break;
216
217
    case GST_VIDEO_TEST_SRC_SNOW:
      videotestsrc->make_image = gst_video_test_src_snow;
Andy Wingo's avatar
Andy Wingo committed
218
      break;
219
220
    case GST_VIDEO_TEST_SRC_BLACK:
      videotestsrc->make_image = gst_video_test_src_black;
Andy Wingo's avatar
Andy Wingo committed
221
      break;
222
223
224
225
226
227
228
229
230
231
232
233
    case GST_VIDEO_TEST_SRC_WHITE:
      videotestsrc->make_image = gst_video_test_src_white;
      break;
    case GST_VIDEO_TEST_SRC_RED:
      videotestsrc->make_image = gst_video_test_src_red;
      break;
    case GST_VIDEO_TEST_SRC_GREEN:
      videotestsrc->make_image = gst_video_test_src_green;
      break;
    case GST_VIDEO_TEST_SRC_BLUE:
      videotestsrc->make_image = gst_video_test_src_blue;
      break;
234
235
236
237
238
239
240
241
242
243
244
245
    case GST_VIDEO_TEST_SRC_CHECKERS1:
      videotestsrc->make_image = gst_video_test_src_checkers1;
      break;
    case GST_VIDEO_TEST_SRC_CHECKERS2:
      videotestsrc->make_image = gst_video_test_src_checkers2;
      break;
    case GST_VIDEO_TEST_SRC_CHECKERS4:
      videotestsrc->make_image = gst_video_test_src_checkers4;
      break;
    case GST_VIDEO_TEST_SRC_CHECKERS8:
      videotestsrc->make_image = gst_video_test_src_checkers8;
      break;
Andy Wingo's avatar
Andy Wingo committed
246
247
248
249
    default:
      g_assert_not_reached ();
  }
}
250

Andy Wingo's avatar
Andy Wingo committed
251
static void
252
gst_video_test_src_set_property (GObject * object, guint prop_id,
Andy Wingo's avatar
Andy Wingo committed
253
254
    const GValue * value, GParamSpec * pspec)
{
255
  GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
David Schleef's avatar
David Schleef committed
256

Andy Wingo's avatar
Andy Wingo committed
257
258
  switch (prop_id) {
    case PROP_PATTERN:
259
      gst_video_test_src_set_pattern (src, g_value_get_enum (value));
Andy Wingo's avatar
Andy Wingo committed
260
261
      break;
    case PROP_TIMESTAMP_OFFSET:
262
263
264
265
      src->timestamp_offset = g_value_get_int64 (value);
      break;
    case PROP_IS_LIVE:
      gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
Andy Wingo's avatar
Andy Wingo committed
266
267
268
      break;
    default:
      break;
269
  }
Andy Wingo's avatar
Andy Wingo committed
270
}
David Schleef's avatar
David Schleef committed
271

Andy Wingo's avatar
Andy Wingo committed
272
static void
273
274
gst_video_test_src_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
Andy Wingo's avatar
Andy Wingo committed
275
{
276
  GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (object);
Andy Wingo's avatar
Andy Wingo committed
277
278
279

  switch (prop_id) {
    case PROP_PATTERN:
280
      g_value_set_enum (value, src->pattern_type);
Andy Wingo's avatar
Andy Wingo committed
281
282
      break;
    case PROP_TIMESTAMP_OFFSET:
283
284
285
286
      g_value_set_int64 (value, src->timestamp_offset);
      break;
    case PROP_IS_LIVE:
      g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
Andy Wingo's avatar
Andy Wingo committed
287
288
289
290
291
292
293
294
295
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

/* threadsafe because this gets called as the plugin is loaded */
static GstCaps *
296
gst_video_test_src_getcaps (GstBaseSrc * unused)
Andy Wingo's avatar
Andy Wingo committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
{
  static GstCaps *capslist = NULL;

  if (!capslist) {
    GstCaps *caps;
    GstStructure *structure;
    int i;

    caps = gst_caps_new_empty ();
    for (i = 0; i < n_fourccs; i++) {
      structure = paint_get_structure (fourcc_list + i);
      gst_structure_set (structure,
          "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
          "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
311
          "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
Andy Wingo's avatar
Andy Wingo committed
312
313
314
315
316
317
318
      gst_caps_append_structure (caps, structure);
    }

    capslist = caps;
  }

  return gst_caps_copy (capslist);
319
320
321
}

static gboolean
322
gst_video_test_src_parse_caps (const GstCaps * caps,
323
    gint * width, gint * height, gint * rate_numerator, gint * rate_denominator,
324
    struct fourcc_list_struct **fourcc)
325
{
David Schleef's avatar
David Schleef committed
326
327
  const GstStructure *structure;
  GstPadLinkReturn ret;
328
  const GValue *framerate;
329

330
331
332
333
  GST_DEBUG ("parsing caps");

  if (gst_caps_get_size (caps) < 1)
    return FALSE;
334

David Schleef's avatar
David Schleef committed
335
  structure = gst_caps_get_structure (caps, 0);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
336

337
338
  if (!(*fourcc = paintinfo_find_by_structure (structure)))
    goto unknown_format;
339

340
341
  ret = gst_structure_get_int (structure, "width", width);
  ret &= gst_structure_get_int (structure, "height", height);
342
343
344
345
346
347
  framerate = gst_structure_get_value (structure, "framerate");

  if (framerate) {
    *rate_numerator = gst_value_get_fraction_numerator (framerate);
    *rate_denominator = gst_value_get_fraction_denominator (framerate);
  } else
348
    goto no_framerate;
349
350

  return ret;
351
352
353
354
355
356
357
358
359
360
361
362

  /* ERRORS */
unknown_format:
  {
    GST_DEBUG ("videotestsrc format not found");
    return FALSE;
  }
no_framerate:
  {
    GST_DEBUG ("videotestsrc no framerate given");
    return FALSE;
  }
363
}
364

365
static gboolean
366
gst_video_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
367
368
{
  gboolean res;
369
  gint width, height, rate_denominator, rate_numerator;
370
  struct fourcc_list_struct *fourcc;
Andy Wingo's avatar
Andy Wingo committed
371
  GstVideoTestSrc *videotestsrc;
372

373
  videotestsrc = GST_VIDEO_TEST_SRC (bsrc);
374

375
  res = gst_video_test_src_parse_caps (caps, &width, &height,
376
      &rate_numerator, &rate_denominator, &fourcc);
377
378
379
380
381
  if (res) {
    /* looks ok here */
    videotestsrc->fourcc = fourcc;
    videotestsrc->width = width;
    videotestsrc->height = height;
382
383
    videotestsrc->rate_numerator = rate_numerator;
    videotestsrc->rate_denominator = rate_denominator;
384
385
    videotestsrc->bpp = videotestsrc->fourcc->bitspp;

386
387
388
    GST_DEBUG_OBJECT (videotestsrc, "size %dx%d, %d/%d fps",
        videotestsrc->width, videotestsrc->height,
        videotestsrc->rate_numerator, videotestsrc->rate_denominator);
389
390
  }
  return res;
391
392
}

393
static gboolean
394
gst_video_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
395
{
396
397
  gboolean res;
  GstVideoTestSrc *src;
398

399
  src = GST_VIDEO_TEST_SRC (bsrc);
400

401
402
  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CONVERT:
403
    {
404
405
      GstFormat src_fmt, dest_fmt;
      gint64 src_val, dest_val;
406

407
408
409
410
411
      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
      if (src_fmt == dest_fmt) {
        dest_val = src_val;
        goto done;
      }
412

413
      switch (src_fmt) {
414
        case GST_FORMAT_DEFAULT:
415
416
417
          switch (dest_fmt) {
            case GST_FORMAT_TIME:
              /* frames to time */
418
419
420
421
422
423
              if (src->rate_numerator) {
                dest_val = gst_util_uint64_scale (src_val,
                    src->rate_denominator * GST_SECOND, src->rate_numerator);
              } else {
                dest_val = 0;
              }
424
425
426
427
              break;
            default:
              goto error;
          }
428
          break;
429
430
431
432
        case GST_FORMAT_TIME:
          switch (dest_fmt) {
            case GST_FORMAT_DEFAULT:
              /* time to frames */
433
434
435
436
437
438
              if (src->rate_numerator) {
                dest_val = gst_util_uint64_scale (src_val,
                    src->rate_numerator, src->rate_denominator * GST_SECOND);
              } else {
                dest_val = 0;
              }
439
440
441
442
              break;
            default:
              goto error;
          }
443
          break;
444
445
        default:
          goto error;
446
      }
447
448
449
    done:
      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      res = TRUE;
450
451
452
      break;
    }
    default:
453
      res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
454
  }
455
  return res;
456

457
458
459
460
461
  /* ERROR */
error:
  {
    GST_DEBUG_OBJECT (src, "query failed");
    return FALSE;
462
463
  }
}
464

465
static void
466
gst_video_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
467
    GstClockTime * start, GstClockTime * end)
468
{
469
470
471
  /* for live sources, sync on the timestamp of the buffer */
  if (gst_base_src_is_live (basesrc)) {
    GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
472

473
474
475
    if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
      /* get duration to calculate end time */
      GstClockTime duration = GST_BUFFER_DURATION (buffer);
476

477
478
479
480
481
482
483
484
485
      if (GST_CLOCK_TIME_IS_VALID (duration)) {
        *end = timestamp + duration;
      }
      *start = timestamp;
    }
  } else {
    *start = -1;
    *end = -1;
  }
486
487
}

488
489
490
491
492
493
494
495
496
497
498
static gboolean
gst_video_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
{
  GstClockTime time;
  GstVideoTestSrc *src;

  src = GST_VIDEO_TEST_SRC (bsrc);

  time = segment->time = segment->start;

  /* now move to the time indicated */
499
500
501
502
503
504
505
506
507
508
509
510
511
  if (src->rate_numerator) {
    src->n_frames = gst_util_uint64_scale (time,
        src->rate_numerator, src->rate_denominator * GST_SECOND);
  } else {
    src->n_frames = 0;
  }
  if (src->rate_numerator) {
    src->running_time = gst_util_uint64_scale (src->n_frames,
        src->rate_denominator * GST_SECOND, src->rate_numerator);
  } else {
    /* FIXME : Not sure what to set here */
    src->running_time = 0;
  }
512
513
514
515
516
517
518
519
520
521
522
523
524

  g_assert (src->running_time <= time);

  return TRUE;
}

static gboolean
gst_video_test_src_is_seekable (GstBaseSrc * psrc)
{
  /* we're seekable... */
  return TRUE;
}

525
static GstFlowReturn
526
gst_video_test_src_create (GstPushSrc * psrc, GstBuffer ** buffer)
527
{
528
  GstVideoTestSrc *src;
529
  gulong newsize;
530
  GstBuffer *outbuf;
Wim Taymans's avatar
Wim Taymans committed
531
  GstFlowReturn res;
532
  GstClockTime next_time;
533

534
  src = GST_VIDEO_TEST_SRC (psrc);
535

536
  if (G_UNLIKELY (src->fourcc == NULL))
537
    goto not_negotiated;
538

539
  /* 0 framerate and we are at the second frame, eos */
540
  if (G_UNLIKELY (src->rate_numerator == 0 && src->n_frames == 1))
541
542
    goto eos;

543
  newsize = gst_video_test_src_get_size (src, src->width, src->height);
544
545

  g_return_val_if_fail (newsize > 0, GST_FLOW_ERROR);
546

547
  GST_LOG_OBJECT (src,
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
548
549
      "creating buffer of %lu bytes with %dx%d image for frame %d", newsize,
      src->width, src->height, (gint) src->n_frames);
550

551
#ifdef USE_PEER_BUFFERALLOC
Wim Taymans's avatar
Wim Taymans committed
552
  res =
Andy Wingo's avatar
Andy Wingo committed
553
554
555
      gst_pad_alloc_buffer_and_set_caps (GST_BASE_SRC_PAD (psrc),
      GST_BUFFER_OFFSET_NONE, newsize, GST_PAD_CAPS (GST_BASE_SRC_PAD (psrc)),
      &outbuf);
Wim Taymans's avatar
Wim Taymans committed
556
  if (res != GST_FLOW_OK)
557
    goto no_buffer;
558
559
560
561
#else
  outbuf = gst_buffer_new_and_alloc (newsize);
  gst_buffer_set_caps (outbuf, GST_PAD_CAPS (GST_BASE_SRC_PAD (psrc)));
#endif
David Schleef's avatar
David Schleef committed
562

563
564
  src->make_image (src, (void *) GST_BUFFER_DATA (outbuf),
      src->width, src->height);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
565

566
  GST_BUFFER_TIMESTAMP (outbuf) = src->timestamp_offset + src->running_time;
567
  GST_BUFFER_OFFSET (outbuf) = src->n_frames;
568
  src->n_frames++;
569
  GST_BUFFER_OFFSET_END (outbuf) = src->n_frames;
570
571
572
573
574
575
576
577
578
  if (src->rate_numerator) {
    next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
        src->rate_denominator, src->rate_numerator);
    GST_BUFFER_DURATION (outbuf) = next_time - src->running_time;
  } else {
    next_time = src->timestamp_offset;
    /* NONE means forever */
    GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
  }
579
580

  src->running_time = next_time;
581

582
  *buffer = outbuf;
583

584
  return GST_FLOW_OK;
585

586
not_negotiated:
587
  {
588
    GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
589
590
        ("format wasn't negotiated before get function"));
    return GST_FLOW_NOT_NEGOTIATED;
591
  }
592
593
eos:
  {
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
594
    GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
595
596
    return GST_FLOW_UNEXPECTED;
  }
597
598
no_buffer:
  {
599
600
    GST_DEBUG_OBJECT (src, "could not allocate buffer, reason %s",
        gst_flow_get_name (res));
601
602
603
604
    return res;
  }
}

605
606
607
608
609
610
611
612
613
614
615
static gboolean
gst_video_test_src_start (GstBaseSrc * basesrc)
{
  GstVideoTestSrc *src = GST_VIDEO_TEST_SRC (basesrc);

  src->running_time = 0;
  src->n_frames = 0;

  return TRUE;
}

616
static gboolean
Benjamin Otte's avatar
Benjamin Otte committed
617
plugin_init (GstPlugin * plugin)
618
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
619
  oil_init ();
620

621
  GST_DEBUG_CATEGORY_INIT (video_test_src_debug, "videotestsrc", 0,
Andy Wingo's avatar
Andy Wingo committed
622
      "Video Test Source");
623

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
624
  return gst_element_register (plugin, "videotestsrc", GST_RANK_NONE,
625
      GST_TYPE_VIDEO_TEST_SRC);
626
627
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
628
629
630
631
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "videotestsrc",
    "Creates a test video stream",
632
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)