1689e46547727a4ba15793e510f8cb67e348b040
[platform/upstream/gstreamer.git] / plugins / elements / gstfakesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim@fluendo.com>
4  *
5  * gstfakesrc.c:
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:element-fakesrc
24  * @see_also: #GstFakeSink
25  *
26  * The fakesrc element is a multipurpose element that can generate
27  * a wide range of buffers and can operate in various scheduling modes.
28  *
29  * It is mostly used as a testing element, one trivial example for testing
30  * basic <application>GStreamer</application> core functionality is:
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch -v fakesrc num-buffers=5 ! fakesink
36  * ]| This pipeline will push 5 empty buffers to the fakesink element and then
37  * sends an EOS.
38  * </refsect2>
39  *
40  * Last reviewed on 2008-06-20 (0.10.21)
41  */
42
43 /* FIXME: this ignores basesrc::blocksize property, which could be used as an
44  * alias to ::sizemax (see gst_base_src_get_blocksize()).
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #  include "config.h"
49 #endif
50
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "gstfakesrc.h"
55 #include <gst/gstmarshal.h>
56
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS_ANY);
61
62 GST_DEBUG_CATEGORY_STATIC (gst_fake_src_debug);
63 #define GST_CAT_DEFAULT gst_fake_src_debug
64
65 /* FakeSrc signals and args */
66 enum
67 {
68   /* FILL ME */
69   SIGNAL_HANDOFF,
70   LAST_SIGNAL
71 };
72
73 #define DEFAULT_OUTPUT          FAKE_SRC_FIRST_LAST_LOOP
74 #define DEFAULT_DATA            FAKE_SRC_DATA_ALLOCATE
75 #define DEFAULT_SIZETYPE        FAKE_SRC_SIZETYPE_EMPTY
76 #define DEFAULT_SIZEMIN         0
77 #define DEFAULT_SIZEMAX         4096
78 #define DEFAULT_FILLTYPE        FAKE_SRC_FILLTYPE_ZERO
79 #define DEFAULT_DATARATE        0
80 #define DEFAULT_SYNC            FALSE
81 #define DEFAULT_PATTERN         NULL
82 #define DEFAULT_EOS             FALSE
83 #define DEFAULT_SIGNAL_HANDOFFS FALSE
84 #define DEFAULT_SILENT          TRUE
85 #define DEFAULT_DUMP            FALSE
86 #define DEFAULT_PARENTSIZE      4096*10
87 #define DEFAULT_CAN_ACTIVATE_PULL TRUE
88 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
89 #define DEFAULT_FORMAT          GST_FORMAT_BYTES
90
91 enum
92 {
93   PROP_0,
94   PROP_OUTPUT,
95   PROP_DATA,
96   PROP_SIZETYPE,
97   PROP_SIZEMIN,
98   PROP_SIZEMAX,
99   PROP_FILLTYPE,
100   PROP_DATARATE,
101   PROP_SYNC,
102   PROP_PATTERN,
103   PROP_EOS,
104   PROP_SIGNAL_HANDOFFS,
105   PROP_SILENT,
106   PROP_DUMP,
107   PROP_PARENTSIZE,
108   PROP_LAST_MESSAGE,
109   PROP_CAN_ACTIVATE_PULL,
110   PROP_CAN_ACTIVATE_PUSH,
111   PROP_IS_LIVE,
112   PROP_FORMAT,
113   PROP_LAST,
114 };
115
116 /* not implemented
117 #define GST_TYPE_FAKE_SRC_OUTPUT (gst_fake_src_output_get_type())
118 static GType
119 gst_fake_src_output_get_type (void)
120 {
121   static GType fakesrc_output_type = 0;
122   static const GEnumValue fakesrc_output[] = {
123     {FAKE_SRC_FIRST_LAST_LOOP, "1", "First-Last loop"},
124     {FAKE_SRC_LAST_FIRST_LOOP, "2", "Last-First loop"},
125     {FAKE_SRC_PING_PONG, "3", "Ping-Pong"},
126     {FAKE_SRC_ORDERED_RANDOM, "4", "Ordered Random"},
127     {FAKE_SRC_RANDOM, "5", "Random"},
128     {FAKE_SRC_PATTERN_LOOP, "6", "Patttern loop"},
129     {FAKE_SRC_PING_PONG_PATTERN, "7", "Ping-Pong Pattern"},
130     {FAKE_SRC_GET_ALWAYS_SUCEEDS, "8", "'_get' Always succeeds"},
131     {0, NULL, NULL},
132   };
133
134   if (!fakesrc_output_type) {
135     fakesrc_output_type =
136         g_enum_register_static ("GstFakeSrcOutput", fakesrc_output);
137   }
138   return fakesrc_output_type;
139 }
140 */
141
142 #define GST_TYPE_FAKE_SRC_DATA (gst_fake_src_data_get_type())
143 static GType
144 gst_fake_src_data_get_type (void)
145 {
146   static GType fakesrc_data_type = 0;
147   static const GEnumValue fakesrc_data[] = {
148     {FAKE_SRC_DATA_ALLOCATE, "Allocate data", "allocate"},
149     {FAKE_SRC_DATA_SUBBUFFER, "Subbuffer data", "subbuffer"},
150     {0, NULL, NULL},
151   };
152
153   if (!fakesrc_data_type) {
154     fakesrc_data_type =
155         g_enum_register_static ("GstFakeSrcDataType", fakesrc_data);
156   }
157   return fakesrc_data_type;
158 }
159
160 #define GST_TYPE_FAKE_SRC_SIZETYPE (gst_fake_src_sizetype_get_type())
161 static GType
162 gst_fake_src_sizetype_get_type (void)
163 {
164   static GType fakesrc_sizetype_type = 0;
165   static const GEnumValue fakesrc_sizetype[] = {
166     {FAKE_SRC_SIZETYPE_EMPTY, "Send empty buffers", "empty"},
167     {FAKE_SRC_SIZETYPE_FIXED, "Fixed size buffers (sizemax sized)", "fixed"},
168     {FAKE_SRC_SIZETYPE_RANDOM,
169         "Random sized buffers (sizemin <= size <= sizemax)", "random"},
170     {0, NULL, NULL},
171   };
172
173   if (!fakesrc_sizetype_type) {
174     fakesrc_sizetype_type =
175         g_enum_register_static ("GstFakeSrcSizeType", fakesrc_sizetype);
176   }
177   return fakesrc_sizetype_type;
178 }
179
180 #define GST_TYPE_FAKE_SRC_FILLTYPE (gst_fake_src_filltype_get_type())
181 static GType
182 gst_fake_src_filltype_get_type (void)
183 {
184   static GType fakesrc_filltype_type = 0;
185   static const GEnumValue fakesrc_filltype[] = {
186     {FAKE_SRC_FILLTYPE_NOTHING, "Leave data as malloced", "nothing"},
187     {FAKE_SRC_FILLTYPE_ZERO, "Fill buffers with zeros", "zero"},
188     {FAKE_SRC_FILLTYPE_RANDOM, "Fill buffers with random crap", "random"},
189     {FAKE_SRC_FILLTYPE_PATTERN, "Fill buffers with pattern 0x00 -> 0xff",
190         "pattern"},
191     {FAKE_SRC_FILLTYPE_PATTERN_CONT,
192           "Fill buffers with pattern 0x00 -> 0xff that spans buffers",
193         "pattern-span"},
194     {0, NULL, NULL},
195   };
196
197   if (!fakesrc_filltype_type) {
198     fakesrc_filltype_type =
199         g_enum_register_static ("GstFakeSrcFillType", fakesrc_filltype);
200   }
201   return fakesrc_filltype_type;
202 }
203
204 #define _do_init \
205     GST_DEBUG_CATEGORY_INIT (gst_fake_src_debug, "fakesrc", 0, "fakesrc element");
206 #define gst_fake_src_parent_class parent_class
207 G_DEFINE_TYPE_WITH_CODE (GstFakeSrc, gst_fake_src, GST_TYPE_BASE_SRC, _do_init);
208
209 static void gst_fake_src_finalize (GObject * object);
210 static void gst_fake_src_set_property (GObject * object, guint prop_id,
211     const GValue * value, GParamSpec * pspec);
212 static void gst_fake_src_get_property (GObject * object, guint prop_id,
213     GValue * value, GParamSpec * pspec);
214
215 static gboolean gst_fake_src_start (GstBaseSrc * basesrc);
216 static gboolean gst_fake_src_stop (GstBaseSrc * basesrc);
217 static gboolean gst_fake_src_is_seekable (GstBaseSrc * basesrc);
218
219 static gboolean gst_fake_src_event_handler (GstBaseSrc * src, GstEvent * event);
220 static void gst_fake_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
221     GstClockTime * start, GstClockTime * end);
222 static GstFlowReturn gst_fake_src_create (GstBaseSrc * src, guint64 offset,
223     guint length, GstBuffer ** buf);
224
225 static guint gst_fake_src_signals[LAST_SIGNAL] = { 0 };
226
227 static GParamSpec *pspec_last_message = NULL;
228
229 static void
230 gst_fake_src_class_init (GstFakeSrcClass * klass)
231 {
232   GObjectClass *gobject_class;
233   GstElementClass *gstelement_class;
234   GstBaseSrcClass *gstbase_src_class;
235
236   gobject_class = G_OBJECT_CLASS (klass);
237   gstelement_class = GST_ELEMENT_CLASS (klass);
238   gstbase_src_class = GST_BASE_SRC_CLASS (klass);
239
240   gobject_class->finalize = gst_fake_src_finalize;
241
242   gobject_class->set_property = gst_fake_src_set_property;
243   gobject_class->get_property = gst_fake_src_get_property;
244
245 /*
246   FIXME: this is not implemented; would make sense once basesrc and fakesrc
247   support multiple pads
248   g_object_class_install_property (gobject_class, PROP_OUTPUT,
249       g_param_spec_enum ("output", "output", "Output method (currently unused)",
250           GST_TYPE_FAKE_SRC_OUTPUT, DEFAULT_OUTPUT, G_PARAM_READWRITE));
251 */
252   g_object_class_install_property (gobject_class, PROP_DATA,
253       g_param_spec_enum ("data", "data", "Data allocation method",
254           GST_TYPE_FAKE_SRC_DATA, DEFAULT_DATA,
255           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIZETYPE,
257       g_param_spec_enum ("sizetype", "sizetype",
258           "How to determine buffer sizes", GST_TYPE_FAKE_SRC_SIZETYPE,
259           DEFAULT_SIZETYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260   g_object_class_install_property (gobject_class, PROP_SIZEMIN,
261       g_param_spec_int ("sizemin", "sizemin", "Minimum buffer size", 0,
262           G_MAXINT, DEFAULT_SIZEMIN,
263           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264   g_object_class_install_property (gobject_class, PROP_SIZEMAX,
265       g_param_spec_int ("sizemax", "sizemax", "Maximum buffer size", 0,
266           G_MAXINT, DEFAULT_SIZEMAX,
267           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
268   g_object_class_install_property (gobject_class, PROP_PARENTSIZE,
269       g_param_spec_int ("parentsize", "parentsize",
270           "Size of parent buffer for sub-buffered allocation", 0, G_MAXINT,
271           DEFAULT_PARENTSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
272   g_object_class_install_property (gobject_class, PROP_FILLTYPE,
273       g_param_spec_enum ("filltype", "filltype",
274           "How to fill the buffer, if at all", GST_TYPE_FAKE_SRC_FILLTYPE,
275           DEFAULT_FILLTYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276   g_object_class_install_property (gobject_class, PROP_DATARATE,
277       g_param_spec_int ("datarate", "Datarate",
278           "Timestamps buffers with number of bytes per second (0 = none)", 0,
279           G_MAXINT, DEFAULT_DATARATE,
280           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281   g_object_class_install_property (gobject_class, PROP_SYNC,
282       g_param_spec_boolean ("sync", "Sync", "Sync to the clock to the datarate",
283           DEFAULT_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284   g_object_class_install_property (gobject_class, PROP_PATTERN,
285       g_param_spec_string ("pattern", "pattern", "pattern", DEFAULT_PATTERN,
286           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287   pspec_last_message = g_param_spec_string ("last-message", "last-message",
288       "The last status message", NULL,
289       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
290   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
291       pspec_last_message);
292   g_object_class_install_property (gobject_class, PROP_SILENT,
293       g_param_spec_boolean ("silent", "Silent",
294           "Don't produce last_message events", DEFAULT_SILENT,
295           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296   g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
297       g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
298           "Send a signal before pushing the buffer", DEFAULT_SIGNAL_HANDOFFS,
299           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300   g_object_class_install_property (gobject_class, PROP_DUMP,
301       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
302           DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303   g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PUSH,
304       g_param_spec_boolean ("can-activate-push", "Can activate push",
305           "Can activate in push mode", DEFAULT_CAN_ACTIVATE_PUSH,
306           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
307   g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
308       g_param_spec_boolean ("can-activate-pull", "Can activate pull",
309           "Can activate in pull mode", DEFAULT_CAN_ACTIVATE_PULL,
310           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
311   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
312       g_param_spec_boolean ("is-live", "Is this a live source",
313           "True if the element cannot produce data in PAUSED", FALSE,
314           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
315   /**
316    * GstFakeSrc:format
317    *
318    * Set the format of the newsegment events to produce.
319    *
320    * Since: 0.10.20
321    */
322   g_object_class_install_property (gobject_class, PROP_FORMAT,
323       g_param_spec_enum ("format", "Format",
324           "The format of the segment events", GST_TYPE_FORMAT,
325           DEFAULT_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326
327   /**
328    * GstFakeSrc::handoff:
329    * @fakesrc: the fakesrc instance
330    * @buffer: the buffer that will be pushed
331    * @pad: the pad that will sent it
332    *
333    * This signal gets emitted before sending the buffer.
334    */
335   gst_fake_src_signals[SIGNAL_HANDOFF] =
336       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
337       G_STRUCT_OFFSET (GstFakeSrcClass, handoff), NULL, NULL,
338       gst_marshal_VOID__BOXED_OBJECT, G_TYPE_NONE, 2,
339       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, GST_TYPE_PAD);
340
341   gst_element_class_set_details_simple (gstelement_class,
342       "Fake Source",
343       "Source",
344       "Push empty (no data) buffers around",
345       "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
346   gst_element_class_add_pad_template (gstelement_class,
347       gst_static_pad_template_get (&srctemplate));
348
349   gstbase_src_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fake_src_is_seekable);
350   gstbase_src_class->start = GST_DEBUG_FUNCPTR (gst_fake_src_start);
351   gstbase_src_class->stop = GST_DEBUG_FUNCPTR (gst_fake_src_stop);
352   gstbase_src_class->event = GST_DEBUG_FUNCPTR (gst_fake_src_event_handler);
353   gstbase_src_class->get_times = GST_DEBUG_FUNCPTR (gst_fake_src_get_times);
354   gstbase_src_class->create = GST_DEBUG_FUNCPTR (gst_fake_src_create);
355 }
356
357 static void
358 gst_fake_src_init (GstFakeSrc * fakesrc)
359 {
360   fakesrc->output = FAKE_SRC_FIRST_LAST_LOOP;
361   fakesrc->buffer_count = 0;
362   fakesrc->silent = DEFAULT_SILENT;
363   fakesrc->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
364   fakesrc->dump = DEFAULT_DUMP;
365   fakesrc->pattern_byte = 0x00;
366   fakesrc->data = FAKE_SRC_DATA_ALLOCATE;
367   fakesrc->sizetype = FAKE_SRC_SIZETYPE_EMPTY;
368   fakesrc->filltype = FAKE_SRC_FILLTYPE_NOTHING;
369   fakesrc->sizemin = DEFAULT_SIZEMIN;
370   fakesrc->sizemax = DEFAULT_SIZEMAX;
371   fakesrc->parent = NULL;
372   fakesrc->parentsize = DEFAULT_PARENTSIZE;
373   fakesrc->last_message = NULL;
374   fakesrc->datarate = DEFAULT_DATARATE;
375   fakesrc->sync = DEFAULT_SYNC;
376   fakesrc->format = DEFAULT_FORMAT;
377 }
378
379 static void
380 gst_fake_src_finalize (GObject * object)
381 {
382   GstFakeSrc *src;
383
384   src = GST_FAKE_SRC (object);
385
386   g_free (src->last_message);
387   if (src->parent) {
388     gst_buffer_unref (src->parent);
389     src->parent = NULL;
390   }
391
392   G_OBJECT_CLASS (parent_class)->finalize (object);
393 }
394
395 static gboolean
396 gst_fake_src_event_handler (GstBaseSrc * basesrc, GstEvent * event)
397 {
398   GstFakeSrc *src;
399
400   src = GST_FAKE_SRC (basesrc);
401
402   if (!src->silent) {
403     const GstStructure *s;
404     const gchar *tstr;
405     gchar *sstr;
406
407     GST_OBJECT_LOCK (src);
408     g_free (src->last_message);
409
410     tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
411
412     if ((s = gst_event_get_structure (event)))
413       sstr = gst_structure_to_string (s);
414     else
415       sstr = g_strdup ("");
416
417     src->last_message =
418         g_strdup_printf ("event   ******* (%s:%s) E (type: %s (%d), %s) %p",
419         GST_DEBUG_PAD_NAME (GST_BASE_SRC_CAST (src)->srcpad),
420         tstr, GST_EVENT_TYPE (event), sstr, event);
421     g_free (sstr);
422     GST_OBJECT_UNLOCK (src);
423
424     g_object_notify_by_pspec ((GObject *) src, pspec_last_message);
425   }
426
427   return GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
428 }
429
430 static void
431 gst_fake_src_alloc_parent (GstFakeSrc * src)
432 {
433   GstBuffer *buf;
434
435   buf = gst_buffer_new_allocate (NULL, src->parentsize, 0);
436
437   src->parent = buf;
438   src->parentoffset = 0;
439 }
440
441 static void
442 gst_fake_src_set_property (GObject * object, guint prop_id,
443     const GValue * value, GParamSpec * pspec)
444 {
445   GstFakeSrc *src;
446   GstBaseSrc *basesrc;
447
448   src = GST_FAKE_SRC (object);
449   basesrc = GST_BASE_SRC (object);
450
451   switch (prop_id) {
452     case PROP_OUTPUT:
453       g_warning ("not yet implemented");
454       break;
455     case PROP_DATA:
456       src->data = g_value_get_enum (value);
457
458       if (src->data == FAKE_SRC_DATA_SUBBUFFER) {
459         if (!src->parent)
460           gst_fake_src_alloc_parent (src);
461       } else {
462         if (src->parent) {
463           gst_buffer_unref (src->parent);
464           src->parent = NULL;
465         }
466       }
467       break;
468     case PROP_SIZETYPE:
469       src->sizetype = g_value_get_enum (value);
470       break;
471     case PROP_SIZEMIN:
472       src->sizemin = g_value_get_int (value);
473       break;
474     case PROP_SIZEMAX:
475       src->sizemax = g_value_get_int (value);
476       break;
477     case PROP_PARENTSIZE:
478       src->parentsize = g_value_get_int (value);
479       break;
480     case PROP_FILLTYPE:
481       src->filltype = g_value_get_enum (value);
482       break;
483     case PROP_DATARATE:
484       src->datarate = g_value_get_int (value);
485       break;
486     case PROP_SYNC:
487       src->sync = g_value_get_boolean (value);
488       break;
489     case PROP_PATTERN:
490       break;
491     case PROP_SILENT:
492       src->silent = g_value_get_boolean (value);
493       break;
494     case PROP_SIGNAL_HANDOFFS:
495       src->signal_handoffs = g_value_get_boolean (value);
496       break;
497     case PROP_DUMP:
498       src->dump = g_value_get_boolean (value);
499       break;
500     case PROP_CAN_ACTIVATE_PUSH:
501       g_return_if_fail (!GST_OBJECT_FLAG_IS_SET (object,
502               GST_BASE_SRC_FLAG_STARTED));
503       GST_BASE_SRC (src)->can_activate_push = g_value_get_boolean (value);
504       break;
505     case PROP_CAN_ACTIVATE_PULL:
506       g_return_if_fail (!GST_OBJECT_FLAG_IS_SET (object,
507               GST_BASE_SRC_FLAG_STARTED));
508       src->can_activate_pull = g_value_get_boolean (value);
509       break;
510     case PROP_IS_LIVE:
511       gst_base_src_set_live (basesrc, g_value_get_boolean (value));
512       break;
513     case PROP_FORMAT:
514       src->format = g_value_get_enum (value);
515       break;
516     default:
517       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
518       break;
519   }
520 }
521
522 static void
523 gst_fake_src_get_property (GObject * object, guint prop_id, GValue * value,
524     GParamSpec * pspec)
525 {
526   GstFakeSrc *src;
527   GstBaseSrc *basesrc;
528
529   g_return_if_fail (GST_IS_FAKE_SRC (object));
530
531   src = GST_FAKE_SRC (object);
532   basesrc = GST_BASE_SRC (object);
533
534   switch (prop_id) {
535     case PROP_OUTPUT:
536       g_value_set_enum (value, src->output);
537       break;
538     case PROP_DATA:
539       g_value_set_enum (value, src->data);
540       break;
541     case PROP_SIZETYPE:
542       g_value_set_enum (value, src->sizetype);
543       break;
544     case PROP_SIZEMIN:
545       g_value_set_int (value, src->sizemin);
546       break;
547     case PROP_SIZEMAX:
548       g_value_set_int (value, src->sizemax);
549       break;
550     case PROP_PARENTSIZE:
551       g_value_set_int (value, src->parentsize);
552       break;
553     case PROP_FILLTYPE:
554       g_value_set_enum (value, src->filltype);
555       break;
556     case PROP_DATARATE:
557       g_value_set_int (value, src->datarate);
558       break;
559     case PROP_SYNC:
560       g_value_set_boolean (value, src->sync);
561       break;
562     case PROP_PATTERN:
563       g_value_set_string (value, src->pattern);
564       break;
565     case PROP_SILENT:
566       g_value_set_boolean (value, src->silent);
567       break;
568     case PROP_SIGNAL_HANDOFFS:
569       g_value_set_boolean (value, src->signal_handoffs);
570       break;
571     case PROP_DUMP:
572       g_value_set_boolean (value, src->dump);
573       break;
574     case PROP_LAST_MESSAGE:
575       GST_OBJECT_LOCK (src);
576       g_value_set_string (value, src->last_message);
577       GST_OBJECT_UNLOCK (src);
578       break;
579     case PROP_CAN_ACTIVATE_PUSH:
580       g_value_set_boolean (value, GST_BASE_SRC (src)->can_activate_push);
581       break;
582     case PROP_CAN_ACTIVATE_PULL:
583       g_value_set_boolean (value, src->can_activate_pull);
584       break;
585     case PROP_IS_LIVE:
586       g_value_set_boolean (value, gst_base_src_is_live (basesrc));
587       break;
588     case PROP_FORMAT:
589       g_value_set_enum (value, src->format);
590       break;
591     default:
592       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
593       break;
594   }
595 }
596
597 static void
598 gst_fake_src_prepare_buffer (GstFakeSrc * src, guint8 * data, gsize size)
599 {
600   if (size == 0)
601     return;
602
603   switch (src->filltype) {
604     case FAKE_SRC_FILLTYPE_ZERO:
605       memset (data, 0, size);
606       break;
607     case FAKE_SRC_FILLTYPE_RANDOM:
608     {
609       gint i;
610       guint8 *ptr = data;
611
612       for (i = size; i; i--) {
613         *ptr++ = g_random_int_range (0, 256);
614       }
615       break;
616     }
617     case FAKE_SRC_FILLTYPE_PATTERN:
618       src->pattern_byte = 0x00;
619     case FAKE_SRC_FILLTYPE_PATTERN_CONT:
620     {
621       gint i;
622       guint8 *ptr = data;
623
624       for (i = size; i; i--) {
625         *ptr++ = src->pattern_byte++;
626       }
627       break;
628     }
629     case FAKE_SRC_FILLTYPE_NOTHING:
630     default:
631       break;
632   }
633 }
634
635 static GstBuffer *
636 gst_fake_src_alloc_buffer (GstFakeSrc * src, guint size)
637 {
638   GstBuffer *buf;
639   gpointer data;
640   gboolean do_prepare = FALSE;
641
642   buf = gst_buffer_new ();
643
644   if (size != 0) {
645     switch (src->filltype) {
646       case FAKE_SRC_FILLTYPE_NOTHING:
647         data = g_malloc (size);
648         break;
649       case FAKE_SRC_FILLTYPE_ZERO:
650         data = g_malloc0 (size);
651         break;
652       case FAKE_SRC_FILLTYPE_RANDOM:
653       case FAKE_SRC_FILLTYPE_PATTERN:
654       case FAKE_SRC_FILLTYPE_PATTERN_CONT:
655       default:
656         data = g_malloc (size);
657         do_prepare = TRUE;
658         break;
659     }
660     if (do_prepare)
661       gst_fake_src_prepare_buffer (src, data, size);
662
663     gst_buffer_take_memory (buf, -1,
664         gst_memory_new_wrapped (0, data, g_free, size, 0, size));
665   }
666
667   return buf;
668 }
669
670 static guint
671 gst_fake_src_get_size (GstFakeSrc * src)
672 {
673   guint size;
674
675   switch (src->sizetype) {
676     case FAKE_SRC_SIZETYPE_FIXED:
677       size = src->sizemax;
678       break;
679     case FAKE_SRC_SIZETYPE_RANDOM:
680       size = g_random_int_range (src->sizemin, src->sizemax);
681       break;
682     case FAKE_SRC_SIZETYPE_EMPTY:
683     default:
684       size = 0;
685       break;
686   }
687
688   return size;
689 }
690
691 static GstBuffer *
692 gst_fake_src_create_buffer (GstFakeSrc * src, gsize * bufsize)
693 {
694   GstBuffer *buf;
695   gsize size = gst_fake_src_get_size (src);
696   gboolean dump = src->dump;
697   GstMapInfo info;
698
699   *bufsize = size;
700
701   switch (src->data) {
702     case FAKE_SRC_DATA_ALLOCATE:
703       buf = gst_fake_src_alloc_buffer (src, size);
704       break;
705     case FAKE_SRC_DATA_SUBBUFFER:
706       /* see if we have a parent to subbuffer */
707       if (!src->parent) {
708         gst_fake_src_alloc_parent (src);
709         g_assert (src->parent);
710       }
711       /* see if it's large enough */
712       if ((src->parentsize - src->parentoffset) >= size) {
713         buf =
714             gst_buffer_copy_region (src->parent, GST_BUFFER_COPY_ALL,
715             src->parentoffset, size);
716         src->parentoffset += size;
717       } else {
718         /* the parent is useless now */
719         gst_buffer_unref (src->parent);
720         src->parent = NULL;
721         /* try again (this will allocate a new parent) */
722         return gst_fake_src_create_buffer (src, bufsize);
723       }
724       gst_buffer_map (buf, &info, GST_MAP_WRITE);
725       gst_fake_src_prepare_buffer (src, info.data, info.size);
726       gst_buffer_unmap (buf, &info);
727       break;
728     default:
729       g_warning ("fakesrc: dunno how to allocate buffers !");
730       buf = gst_buffer_new ();
731       break;
732   }
733   if (dump) {
734     gst_buffer_map (buf, &info, GST_MAP_READ);
735     gst_util_dump_mem (info.data, info.size);
736     gst_buffer_unmap (buf, &info);
737   }
738
739   return buf;
740 }
741
742 static void
743 gst_fake_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
744     GstClockTime * start, GstClockTime * end)
745 {
746   GstFakeSrc *src;
747
748   src = GST_FAKE_SRC (basesrc);
749
750   /* sync on the timestamp of the buffer if requested. */
751   if (src->sync) {
752     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
753
754     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
755       /* get duration to calculate end time */
756       GstClockTime duration = GST_BUFFER_DURATION (buffer);
757
758       if (GST_CLOCK_TIME_IS_VALID (duration)) {
759         *end = timestamp + duration;
760       }
761       *start = timestamp;
762     }
763   } else {
764     *start = -1;
765     *end = -1;
766   }
767 }
768
769 static GstFlowReturn
770 gst_fake_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
771     GstBuffer ** ret)
772 {
773   GstFakeSrc *src;
774   GstBuffer *buf;
775   GstClockTime time;
776   gsize size;
777
778   src = GST_FAKE_SRC (basesrc);
779
780   buf = gst_fake_src_create_buffer (src, &size);
781   GST_BUFFER_OFFSET (buf) = src->buffer_count++;
782
783   if (src->datarate > 0) {
784     time = (src->bytes_sent * GST_SECOND) / src->datarate;
785
786     GST_BUFFER_DURATION (buf) = size * GST_SECOND / src->datarate;
787   } else if (gst_base_src_is_live (basesrc)) {
788     GstClock *clock;
789
790     clock = gst_element_get_clock (GST_ELEMENT (src));
791
792     if (clock) {
793       time = gst_clock_get_time (clock);
794       time -= gst_element_get_base_time (GST_ELEMENT (src));
795       gst_object_unref (clock);
796     } else {
797       /* not an error not to have a clock */
798       time = GST_CLOCK_TIME_NONE;
799     }
800   } else {
801     time = GST_CLOCK_TIME_NONE;
802   }
803
804   GST_BUFFER_TIMESTAMP (buf) = time;
805
806   if (!src->silent) {
807     gchar ts_str[64], dur_str[64];
808     gchar flag_str[100];
809
810     GST_OBJECT_LOCK (src);
811     g_free (src->last_message);
812
813     if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
814       g_snprintf (ts_str, sizeof (ts_str), "%" GST_TIME_FORMAT,
815           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
816     } else {
817       g_strlcpy (ts_str, "none", sizeof (ts_str));
818     }
819
820     if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
821       g_snprintf (dur_str, sizeof (dur_str), "%" GST_TIME_FORMAT,
822           GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
823     } else {
824       g_strlcpy (dur_str, "none", sizeof (dur_str));
825     }
826
827     {
828       const char *flag_list[15] = {
829         "", "", "", "", "live", "decode-only", "discont", "resync", "corrupted",
830         "marker", "header", "gap", "droppable", "delta-unit", "in-caps"
831       };
832       int i;
833       char *end = flag_str;
834       end[0] = '\0';
835       for (i = 0; i < G_N_ELEMENTS (flag_list); i++) {
836         if (GST_MINI_OBJECT_CAST (buf)->flags & (1 << i)) {
837           strcpy (end, flag_list[i]);
838           end += strlen (end);
839           end[0] = ' ';
840           end[1] = '\0';
841           end++;
842         }
843       }
844     }
845
846     src->last_message =
847         g_strdup_printf ("create   ******* (%s:%s) (%u bytes, timestamp: %s"
848         ", duration: %s, offset: %" G_GINT64_FORMAT ", offset_end: %"
849         G_GINT64_FORMAT ", flags: %d %s) %p",
850         GST_DEBUG_PAD_NAME (GST_BASE_SRC_CAST (src)->srcpad), (guint) size,
851         ts_str, dur_str, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
852         GST_MINI_OBJECT_CAST (buf)->flags, flag_str, buf);
853     GST_OBJECT_UNLOCK (src);
854
855     g_object_notify_by_pspec ((GObject *) src, pspec_last_message);
856   }
857
858   if (src->signal_handoffs) {
859     GST_LOG_OBJECT (src, "pre handoff emit");
860     g_signal_emit (src, gst_fake_src_signals[SIGNAL_HANDOFF], 0, buf,
861         basesrc->srcpad);
862     GST_LOG_OBJECT (src, "post handoff emit");
863   }
864
865   src->bytes_sent += size;
866
867   *ret = buf;
868   return GST_FLOW_OK;
869 }
870
871 static gboolean
872 gst_fake_src_start (GstBaseSrc * basesrc)
873 {
874   GstFakeSrc *src;
875
876   src = GST_FAKE_SRC (basesrc);
877
878   src->buffer_count = 0;
879   src->pattern_byte = 0x00;
880   src->bytes_sent = 0;
881
882   gst_base_src_set_format (basesrc, src->format);
883
884   return TRUE;
885 }
886
887 static gboolean
888 gst_fake_src_stop (GstBaseSrc * basesrc)
889 {
890   GstFakeSrc *src;
891
892   src = GST_FAKE_SRC (basesrc);
893
894   GST_OBJECT_LOCK (src);
895   if (src->parent) {
896     gst_buffer_unref (src->parent);
897     src->parent = NULL;
898   }
899   g_free (src->last_message);
900   src->last_message = NULL;
901   GST_OBJECT_UNLOCK (src);
902
903   return TRUE;
904 }
905
906 static gboolean
907 gst_fake_src_is_seekable (GstBaseSrc * basesrc)
908 {
909   GstFakeSrc *src = GST_FAKE_SRC (basesrc);
910
911   return src->can_activate_pull;
912 }