]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - plugins/elements/gstfakesrc.c
fix whitespace
[glsdk/gstreamer0-10.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  */
43 /* FIXME: this ignores basesrc::blocksize property, which could be used as an
44  * alias to ::sizemax (see gst_base_src_get_blocksize()).
45  */
47 #ifdef HAVE_CONFIG_H
48 #  include "config.h"
49 #endif
51 #include <stdlib.h>
52 #include <string.h>
54 #include "gstfakesrc.h"
55 #include <gst/gstmarshal.h>
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS_ANY);
62 GST_DEBUG_CATEGORY_STATIC (gst_fake_src_debug);
63 #define GST_CAT_DEFAULT gst_fake_src_debug
66 /* FakeSrc signals and args */
67 enum
68 {
69   /* FILL ME */
70   SIGNAL_HANDOFF,
71   LAST_SIGNAL
72 };
74 #define DEFAULT_OUTPUT          FAKE_SRC_FIRST_LAST_LOOP
75 #define DEFAULT_DATA            FAKE_SRC_DATA_ALLOCATE
76 #define DEFAULT_SIZETYPE        FAKE_SRC_SIZETYPE_EMPTY
77 #define DEFAULT_SIZEMIN         0
78 #define DEFAULT_SIZEMAX         4096
79 #define DEFAULT_FILLTYPE        FAKE_SRC_FILLTYPE_ZERO
80 #define DEFAULT_DATARATE        0
81 #define DEFAULT_SYNC            FALSE
82 #define DEFAULT_PATTERN         NULL
83 #define DEFAULT_EOS             FALSE
84 #define DEFAULT_SIGNAL_HANDOFFS FALSE
85 #define DEFAULT_SILENT          FALSE
86 #define DEFAULT_DUMP            FALSE
87 #define DEFAULT_PARENTSIZE      4096*10
88 #define DEFAULT_CAN_ACTIVATE_PULL TRUE
89 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
90 #define DEFAULT_FORMAT          GST_FORMAT_BYTES
92 enum
93 {
94   PROP_0,
95   PROP_OUTPUT,
96   PROP_DATA,
97   PROP_SIZETYPE,
98   PROP_SIZEMIN,
99   PROP_SIZEMAX,
100   PROP_FILLTYPE,
101   PROP_DATARATE,
102   PROP_SYNC,
103   PROP_PATTERN,
104   PROP_EOS,
105   PROP_SIGNAL_HANDOFFS,
106   PROP_SILENT,
107   PROP_DUMP,
108   PROP_PARENTSIZE,
109   PROP_LAST_MESSAGE,
110   PROP_CAN_ACTIVATE_PULL,
111   PROP_CAN_ACTIVATE_PUSH,
112   PROP_IS_LIVE,
113   PROP_FORMAT,
114   PROP_LAST,
115 };
117 /* not implemented
118 #define GST_TYPE_FAKE_SRC_OUTPUT (gst_fake_src_output_get_type())
119 static GType
120 gst_fake_src_output_get_type (void)
122   static GType fakesrc_output_type = 0;
123   static const GEnumValue fakesrc_output[] = {
124     {FAKE_SRC_FIRST_LAST_LOOP, "1", "First-Last loop"},
125     {FAKE_SRC_LAST_FIRST_LOOP, "2", "Last-First loop"},
126     {FAKE_SRC_PING_PONG, "3", "Ping-Pong"},
127     {FAKE_SRC_ORDERED_RANDOM, "4", "Ordered Random"},
128     {FAKE_SRC_RANDOM, "5", "Random"},
129     {FAKE_SRC_PATTERN_LOOP, "6", "Patttern loop"},
130     {FAKE_SRC_PING_PONG_PATTERN, "7", "Ping-Pong Pattern"},
131     {FAKE_SRC_GET_ALWAYS_SUCEEDS, "8", "'_get' Always succeeds"},
132     {0, NULL, NULL},
133   };
135   if (!fakesrc_output_type) {
136     fakesrc_output_type =
137         g_enum_register_static ("GstFakeSrcOutput", fakesrc_output);
138   }
139   return fakesrc_output_type;
141 */
143 #define GST_TYPE_FAKE_SRC_DATA (gst_fake_src_data_get_type())
144 static GType
145 gst_fake_src_data_get_type (void)
147   static GType fakesrc_data_type = 0;
148   static const GEnumValue fakesrc_data[] = {
149     {FAKE_SRC_DATA_ALLOCATE, "Allocate data", "allocate"},
150     {FAKE_SRC_DATA_SUBBUFFER, "Subbuffer data", "subbuffer"},
151     {0, NULL, NULL},
152   };
154   if (!fakesrc_data_type) {
155     fakesrc_data_type =
156         g_enum_register_static ("GstFakeSrcDataType", fakesrc_data);
157   }
158   return fakesrc_data_type;
161 #define GST_TYPE_FAKE_SRC_SIZETYPE (gst_fake_src_sizetype_get_type())
162 static GType
163 gst_fake_src_sizetype_get_type (void)
165   static GType fakesrc_sizetype_type = 0;
166   static const GEnumValue fakesrc_sizetype[] = {
167     {FAKE_SRC_SIZETYPE_EMPTY, "Send empty buffers", "empty"},
168     {FAKE_SRC_SIZETYPE_FIXED, "Fixed size buffers (sizemax sized)", "fixed"},
169     {FAKE_SRC_SIZETYPE_RANDOM,
170         "Random sized buffers (sizemin <= size <= sizemax)", "random"},
171     {0, NULL, NULL},
172   };
174   if (!fakesrc_sizetype_type) {
175     fakesrc_sizetype_type =
176         g_enum_register_static ("GstFakeSrcSizeType", fakesrc_sizetype);
177   }
178   return fakesrc_sizetype_type;
181 #define GST_TYPE_FAKE_SRC_FILLTYPE (gst_fake_src_filltype_get_type())
182 static GType
183 gst_fake_src_filltype_get_type (void)
185   static GType fakesrc_filltype_type = 0;
186   static const GEnumValue fakesrc_filltype[] = {
187     {FAKE_SRC_FILLTYPE_NOTHING, "Leave data as malloced", "nothing"},
188     {FAKE_SRC_FILLTYPE_ZERO, "Fill buffers with zeros", "zero"},
189     {FAKE_SRC_FILLTYPE_RANDOM, "Fill buffers with random crap", "random"},
190     {FAKE_SRC_FILLTYPE_PATTERN, "Fill buffers with pattern 0x00 -> 0xff",
191         "pattern"},
192     {FAKE_SRC_FILLTYPE_PATTERN_CONT,
193           "Fill buffers with pattern 0x00 -> 0xff that spans buffers",
194         "pattern-span"},
195     {0, NULL, NULL},
196   };
198   if (!fakesrc_filltype_type) {
199     fakesrc_filltype_type =
200         g_enum_register_static ("GstFakeSrcFillType", fakesrc_filltype);
201   }
202   return fakesrc_filltype_type;
205 #define _do_init(bla) \
206     GST_DEBUG_CATEGORY_INIT (gst_fake_src_debug, "fakesrc", 0, "fakesrc element");
208 GST_BOILERPLATE_FULL (GstFakeSrc, gst_fake_src, GstBaseSrc, GST_TYPE_BASE_SRC,
209     _do_init);
211 static void gst_fake_src_finalize (GObject * object);
212 static void gst_fake_src_set_property (GObject * object, guint prop_id,
213     const GValue * value, GParamSpec * pspec);
214 static void gst_fake_src_get_property (GObject * object, guint prop_id,
215     GValue * value, GParamSpec * pspec);
217 static gboolean gst_fake_src_start (GstBaseSrc * basesrc);
218 static gboolean gst_fake_src_stop (GstBaseSrc * basesrc);
219 static gboolean gst_fake_src_is_seekable (GstBaseSrc * basesrc);
221 static gboolean gst_fake_src_event_handler (GstBaseSrc * src, GstEvent * event);
222 static void gst_fake_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
223     GstClockTime * start, GstClockTime * end);
224 static GstFlowReturn gst_fake_src_create (GstBaseSrc * src, guint64 offset,
225     guint length, GstBuffer ** buf);
227 static guint gst_fake_src_signals[LAST_SIGNAL] = { 0 };
229 static void
230 marshal_VOID__MINIOBJECT_OBJECT (GClosure * closure, GValue * return_value,
231     guint n_param_values, const GValue * param_values, gpointer invocation_hint,
232     gpointer marshal_data)
234   typedef void (*marshalfunc_VOID__MINIOBJECT_OBJECT) (gpointer obj,
235       gpointer arg1, gpointer arg2, gpointer data2);
236   register marshalfunc_VOID__MINIOBJECT_OBJECT callback;
237   register GCClosure *cc = (GCClosure *) closure;
238   register gpointer data1, data2;
240   g_return_if_fail (n_param_values == 3);
242   if (G_CCLOSURE_SWAP_DATA (closure)) {
243     data1 = closure->data;
244     data2 = g_value_peek_pointer (param_values + 0);
245   } else {
246     data1 = g_value_peek_pointer (param_values + 0);
247     data2 = closure->data;
248   }
249   callback =
250       (marshalfunc_VOID__MINIOBJECT_OBJECT) (marshal_data ? marshal_data :
251       cc->callback);
253   callback (data1, gst_value_get_mini_object (param_values + 1),
254       g_value_get_object (param_values + 2), data2);
257 static void
258 gst_fake_src_base_init (gpointer g_class)
260   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
262   gst_element_class_set_details_simple (gstelement_class,
263       "Fake Source",
264       "Source",
265       "Push empty (no data) buffers around",
266       "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
267   gst_element_class_add_pad_template (gstelement_class,
268       gst_static_pad_template_get (&srctemplate));
271 static void
272 gst_fake_src_class_init (GstFakeSrcClass * klass)
274   GObjectClass *gobject_class;
275   GstBaseSrcClass *gstbase_src_class;
277   gobject_class = G_OBJECT_CLASS (klass);
278   gstbase_src_class = GST_BASE_SRC_CLASS (klass);
280   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_fake_src_finalize);
282   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fake_src_set_property);
283   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fake_src_get_property);
285 /*
286   FIXME: this is not implemented; would make sense once basesrc and fakesrc
287   support multiple pads
288   g_object_class_install_property (gobject_class, PROP_OUTPUT,
289       g_param_spec_enum ("output", "output", "Output method (currently unused)",
290           GST_TYPE_FAKE_SRC_OUTPUT, DEFAULT_OUTPUT, G_PARAM_READWRITE));
291 */
292   g_object_class_install_property (gobject_class, PROP_DATA,
293       g_param_spec_enum ("data", "data", "Data allocation method",
294           GST_TYPE_FAKE_SRC_DATA, DEFAULT_DATA,
295           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIZETYPE,
297       g_param_spec_enum ("sizetype", "sizetype",
298           "How to determine buffer sizes", GST_TYPE_FAKE_SRC_SIZETYPE,
299           DEFAULT_SIZETYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300   g_object_class_install_property (gobject_class, PROP_SIZEMIN,
301       g_param_spec_int ("sizemin", "sizemin", "Minimum buffer size", 0,
302           G_MAXINT, DEFAULT_SIZEMIN,
303           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304   g_object_class_install_property (gobject_class, PROP_SIZEMAX,
305       g_param_spec_int ("sizemax", "sizemax", "Maximum buffer size", 0,
306           G_MAXINT, DEFAULT_SIZEMAX,
307           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308   g_object_class_install_property (gobject_class, PROP_PARENTSIZE,
309       g_param_spec_int ("parentsize", "parentsize",
310           "Size of parent buffer for sub-buffered allocation", 0, G_MAXINT,
311           DEFAULT_PARENTSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312   g_object_class_install_property (gobject_class, PROP_FILLTYPE,
313       g_param_spec_enum ("filltype", "filltype",
314           "How to fill the buffer, if at all", GST_TYPE_FAKE_SRC_FILLTYPE,
315           DEFAULT_FILLTYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316   g_object_class_install_property (gobject_class, PROP_DATARATE,
317       g_param_spec_int ("datarate", "Datarate",
318           "Timestamps buffers with number of bytes per second (0 = none)", 0,
319           G_MAXINT, DEFAULT_DATARATE,
320           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321   g_object_class_install_property (gobject_class, PROP_SYNC,
322       g_param_spec_boolean ("sync", "Sync", "Sync to the clock to the datarate",
323           DEFAULT_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
324   g_object_class_install_property (gobject_class, PROP_PATTERN,
325       g_param_spec_string ("pattern", "pattern", "pattern", DEFAULT_PATTERN,
326           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
328       g_param_spec_string ("last-message", "last-message",
329           "The last status message", NULL,
330           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
331   g_object_class_install_property (gobject_class, PROP_SILENT,
332       g_param_spec_boolean ("silent", "Silent",
333           "Don't produce last_message events", DEFAULT_SILENT,
334           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335   g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
336       g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
337           "Send a signal before pushing the buffer", DEFAULT_SIGNAL_HANDOFFS,
338           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339   g_object_class_install_property (gobject_class, PROP_DUMP,
340       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
341           DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342   g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PUSH,
343       g_param_spec_boolean ("can-activate-push", "Can activate push",
344           "Can activate in push mode", DEFAULT_CAN_ACTIVATE_PUSH,
345           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
346   g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
347       g_param_spec_boolean ("can-activate-pull", "Can activate pull",
348           "Can activate in pull mode", DEFAULT_CAN_ACTIVATE_PULL,
349           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
350   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
351       g_param_spec_boolean ("is-live", "Is this a live source",
352           "True if the element cannot produce data in PAUSED", FALSE,
353           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
354   /**
355    * GstFakeSrc:format
356    *
357    * Set the format of the newsegment events to produce.
358    *
359    * Since: 0.10.20
360    */
361   g_object_class_install_property (gobject_class, PROP_FORMAT,
362       g_param_spec_enum ("format", "Format",
363           "The format of the segment events", GST_TYPE_FORMAT,
364           DEFAULT_FORMAT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366   /**
367    * GstFakeSrc::handoff:
368    * @fakesrc: the fakesrc instance
369    * @buffer: the buffer that will be pushed
370    * @pad: the pad that will sent it
371    *
372    * This signal gets emitted before sending the buffer.
373    */
374   gst_fake_src_signals[SIGNAL_HANDOFF] =
375       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
376       G_STRUCT_OFFSET (GstFakeSrcClass, handoff), NULL, NULL,
377       marshal_VOID__MINIOBJECT_OBJECT, G_TYPE_NONE, 2, GST_TYPE_BUFFER,
378       GST_TYPE_PAD);
380   gstbase_src_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fake_src_is_seekable);
381   gstbase_src_class->start = GST_DEBUG_FUNCPTR (gst_fake_src_start);
382   gstbase_src_class->stop = GST_DEBUG_FUNCPTR (gst_fake_src_stop);
383   gstbase_src_class->event = GST_DEBUG_FUNCPTR (gst_fake_src_event_handler);
384   gstbase_src_class->get_times = GST_DEBUG_FUNCPTR (gst_fake_src_get_times);
385   gstbase_src_class->create = GST_DEBUG_FUNCPTR (gst_fake_src_create);
388 static void
389 gst_fake_src_init (GstFakeSrc * fakesrc, GstFakeSrcClass * g_class)
391   fakesrc->output = FAKE_SRC_FIRST_LAST_LOOP;
392   fakesrc->buffer_count = 0;
393   fakesrc->silent = DEFAULT_SILENT;
394   fakesrc->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
395   fakesrc->dump = DEFAULT_DUMP;
396   fakesrc->pattern_byte = 0x00;
397   fakesrc->data = FAKE_SRC_DATA_ALLOCATE;
398   fakesrc->sizetype = FAKE_SRC_SIZETYPE_EMPTY;
399   fakesrc->filltype = FAKE_SRC_FILLTYPE_NOTHING;
400   fakesrc->sizemin = DEFAULT_SIZEMIN;
401   fakesrc->sizemax = DEFAULT_SIZEMAX;
402   fakesrc->parent = NULL;
403   fakesrc->parentsize = DEFAULT_PARENTSIZE;
404   fakesrc->last_message = NULL;
405   fakesrc->datarate = DEFAULT_DATARATE;
406   fakesrc->sync = DEFAULT_SYNC;
407   fakesrc->format = DEFAULT_FORMAT;
410 static void
411 gst_fake_src_finalize (GObject * object)
413   GstFakeSrc *src;
415   src = GST_FAKE_SRC (object);
417   g_free (src->last_message);
418   if (src->parent) {
419     gst_buffer_unref (src->parent);
420     src->parent = NULL;
421   }
423   G_OBJECT_CLASS (parent_class)->finalize (object);
426 static gboolean
427 gst_fake_src_event_handler (GstBaseSrc * basesrc, GstEvent * event)
429   GstFakeSrc *src;
431   src = GST_FAKE_SRC (basesrc);
433   if (!src->silent) {
434     const GstStructure *s;
435     gchar *sstr;
437     GST_OBJECT_LOCK (src);
438     g_free (src->last_message);
440     if ((s = gst_event_get_structure (event)))
441       sstr = gst_structure_to_string (s);
442     else
443       sstr = g_strdup ("");
445     src->last_message =
446         g_strdup_printf ("event   ******* E (type: %d, %s) %p",
447         GST_EVENT_TYPE (event), sstr, event);
448     g_free (sstr);
449     GST_OBJECT_UNLOCK (src);
451     g_object_notify (G_OBJECT (src), "last_message");
452   }
454   return GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
457 static void
458 gst_fake_src_alloc_parent (GstFakeSrc * src)
460   GstBuffer *buf;
462   buf = gst_buffer_new ();
463   GST_BUFFER_DATA (buf) = g_malloc (src->parentsize);
464   GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
465   GST_BUFFER_SIZE (buf) = src->parentsize;
467   src->parent = buf;
468   src->parentoffset = 0;
471 static void
472 gst_fake_src_set_property (GObject * object, guint prop_id,
473     const GValue * value, GParamSpec * pspec)
475   GstFakeSrc *src;
476   GstBaseSrc *basesrc;
478   src = GST_FAKE_SRC (object);
479   basesrc = GST_BASE_SRC (object);
481   switch (prop_id) {
482     case PROP_OUTPUT:
483       g_warning ("not yet implemented");
484       break;
485     case PROP_DATA:
486       src->data = g_value_get_enum (value);
488       if (src->data == FAKE_SRC_DATA_SUBBUFFER) {
489         if (!src->parent)
490           gst_fake_src_alloc_parent (src);
491       } else {
492         if (src->parent) {
493           gst_buffer_unref (src->parent);
494           src->parent = NULL;
495         }
496       }
497       break;
498     case PROP_SIZETYPE:
499       src->sizetype = g_value_get_enum (value);
500       break;
501     case PROP_SIZEMIN:
502       src->sizemin = g_value_get_int (value);
503       break;
504     case PROP_SIZEMAX:
505       src->sizemax = g_value_get_int (value);
506       break;
507     case PROP_PARENTSIZE:
508       src->parentsize = g_value_get_int (value);
509       break;
510     case PROP_FILLTYPE:
511       src->filltype = g_value_get_enum (value);
512       break;
513     case PROP_DATARATE:
514       src->datarate = g_value_get_int (value);
515       break;
516     case PROP_SYNC:
517       src->sync = g_value_get_boolean (value);
518       break;
519     case PROP_PATTERN:
520       break;
521     case PROP_SILENT:
522       src->silent = g_value_get_boolean (value);
523       break;
524     case PROP_SIGNAL_HANDOFFS:
525       src->signal_handoffs = g_value_get_boolean (value);
526       break;
527     case PROP_DUMP:
528       src->dump = g_value_get_boolean (value);
529       break;
530     case PROP_CAN_ACTIVATE_PUSH:
531       g_return_if_fail (!GST_OBJECT_FLAG_IS_SET (object, GST_BASE_SRC_STARTED));
532       GST_BASE_SRC (src)->can_activate_push = g_value_get_boolean (value);
533       break;
534     case PROP_CAN_ACTIVATE_PULL:
535       g_return_if_fail (!GST_OBJECT_FLAG_IS_SET (object, GST_BASE_SRC_STARTED));
536       src->can_activate_pull = g_value_get_boolean (value);
537       break;
538     case PROP_IS_LIVE:
539       gst_base_src_set_live (basesrc, g_value_get_boolean (value));
540       break;
541     case PROP_FORMAT:
542       src->format = g_value_get_enum (value);
543       break;
544     default:
545       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546       break;
547   }
550 static void
551 gst_fake_src_get_property (GObject * object, guint prop_id, GValue * value,
552     GParamSpec * pspec)
554   GstFakeSrc *src;
555   GstBaseSrc *basesrc;
557   g_return_if_fail (GST_IS_FAKE_SRC (object));
559   src = GST_FAKE_SRC (object);
560   basesrc = GST_BASE_SRC (object);
562   switch (prop_id) {
563     case PROP_OUTPUT:
564       g_value_set_enum (value, src->output);
565       break;
566     case PROP_DATA:
567       g_value_set_enum (value, src->data);
568       break;
569     case PROP_SIZETYPE:
570       g_value_set_enum (value, src->sizetype);
571       break;
572     case PROP_SIZEMIN:
573       g_value_set_int (value, src->sizemin);
574       break;
575     case PROP_SIZEMAX:
576       g_value_set_int (value, src->sizemax);
577       break;
578     case PROP_PARENTSIZE:
579       g_value_set_int (value, src->parentsize);
580       break;
581     case PROP_FILLTYPE:
582       g_value_set_enum (value, src->filltype);
583       break;
584     case PROP_DATARATE:
585       g_value_set_int (value, src->datarate);
586       break;
587     case PROP_SYNC:
588       g_value_set_boolean (value, src->sync);
589       break;
590     case PROP_PATTERN:
591       g_value_set_string (value, src->pattern);
592       break;
593     case PROP_SILENT:
594       g_value_set_boolean (value, src->silent);
595       break;
596     case PROP_SIGNAL_HANDOFFS:
597       g_value_set_boolean (value, src->signal_handoffs);
598       break;
599     case PROP_DUMP:
600       g_value_set_boolean (value, src->dump);
601       break;
602     case PROP_LAST_MESSAGE:
603       GST_OBJECT_LOCK (src);
604       g_value_set_string (value, src->last_message);
605       GST_OBJECT_UNLOCK (src);
606       break;
607     case PROP_CAN_ACTIVATE_PUSH:
608       g_value_set_boolean (value, GST_BASE_SRC (src)->can_activate_push);
609       break;
610     case PROP_CAN_ACTIVATE_PULL:
611       g_value_set_boolean (value, src->can_activate_pull);
612       break;
613     case PROP_IS_LIVE:
614       g_value_set_boolean (value, gst_base_src_is_live (basesrc));
615       break;
616     case PROP_FORMAT:
617       g_value_set_enum (value, src->format);
618       break;
619     default:
620       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
621       break;
622   }
625 static void
626 gst_fake_src_prepare_buffer (GstFakeSrc * src, GstBuffer * buf)
628   if (GST_BUFFER_SIZE (buf) == 0)
629     return;
631   switch (src->filltype) {
632     case FAKE_SRC_FILLTYPE_ZERO:
633       memset (GST_BUFFER_DATA (buf), 0, GST_BUFFER_SIZE (buf));
634       break;
635     case FAKE_SRC_FILLTYPE_RANDOM:
636     {
637       gint i;
638       guint8 *ptr = GST_BUFFER_DATA (buf);
640       for (i = GST_BUFFER_SIZE (buf); i; i--) {
641         *ptr++ = g_random_int_range (0, 256);
642       }
643       break;
644     }
645     case FAKE_SRC_FILLTYPE_PATTERN:
646       src->pattern_byte = 0x00;
647     case FAKE_SRC_FILLTYPE_PATTERN_CONT:
648     {
649       gint i;
650       guint8 *ptr = GST_BUFFER_DATA (buf);
652       for (i = GST_BUFFER_SIZE (buf); i; i--) {
653         *ptr++ = src->pattern_byte++;
654       }
655       break;
656     }
657     case FAKE_SRC_FILLTYPE_NOTHING:
658     default:
659       break;
660   }
663 static GstBuffer *
664 gst_fake_src_alloc_buffer (GstFakeSrc * src, guint size)
666   GstBuffer *buf;
668   buf = gst_buffer_new ();
669   GST_BUFFER_SIZE (buf) = size;
671   if (size != 0) {
672     switch (src->filltype) {
673       case FAKE_SRC_FILLTYPE_NOTHING:
674         GST_BUFFER_DATA (buf) = g_malloc (size);
675         GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
676         break;
677       case FAKE_SRC_FILLTYPE_ZERO:
678         GST_BUFFER_DATA (buf) = g_malloc0 (size);
679         GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
680         break;
681       case FAKE_SRC_FILLTYPE_RANDOM:
682       case FAKE_SRC_FILLTYPE_PATTERN:
683       case FAKE_SRC_FILLTYPE_PATTERN_CONT:
684       default:
685         GST_BUFFER_DATA (buf) = g_malloc (size);
686         GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
687         gst_fake_src_prepare_buffer (src, buf);
688         break;
689     }
690   }
692   return buf;
695 static guint
696 gst_fake_src_get_size (GstFakeSrc * src)
698   guint size;
700   switch (src->sizetype) {
701     case FAKE_SRC_SIZETYPE_FIXED:
702       size = src->sizemax;
703       break;
704     case FAKE_SRC_SIZETYPE_RANDOM:
705       size = g_random_int_range (src->sizemin, src->sizemax);
706       break;
707     case FAKE_SRC_SIZETYPE_EMPTY:
708     default:
709       size = 0;
710       break;
711   }
713   return size;
716 static GstBuffer *
717 gst_fake_src_create_buffer (GstFakeSrc * src)
719   GstBuffer *buf;
720   guint size;
721   gboolean dump = src->dump;
723   size = gst_fake_src_get_size (src);
724   if (size == 0)
725     return gst_buffer_new ();
727   switch (src->data) {
728     case FAKE_SRC_DATA_ALLOCATE:
729       buf = gst_fake_src_alloc_buffer (src, size);
730       break;
731     case FAKE_SRC_DATA_SUBBUFFER:
732       /* see if we have a parent to subbuffer */
733       if (!src->parent) {
734         gst_fake_src_alloc_parent (src);
735         g_assert (src->parent);
736       }
737       /* see if it's large enough */
738       if ((GST_BUFFER_SIZE (src->parent) - src->parentoffset) >= size) {
739         buf = gst_buffer_create_sub (src->parent, src->parentoffset, size);
740         src->parentoffset += size;
741       } else {
742         /* the parent is useless now */
743         gst_buffer_unref (src->parent);
744         src->parent = NULL;
745         /* try again (this will allocate a new parent) */
746         return gst_fake_src_create_buffer (src);
747       }
748       gst_fake_src_prepare_buffer (src, buf);
749       break;
750     default:
751       g_warning ("fakesrc: dunno how to allocate buffers !");
752       buf = gst_buffer_new ();
753       break;
754   }
755   if (dump) {
756     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
757   }
759   return buf;
762 static void
763 gst_fake_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
764     GstClockTime * start, GstClockTime * end)
766   GstFakeSrc *src;
768   src = GST_FAKE_SRC (basesrc);
770   /* sync on the timestamp of the buffer if requested. */
771   if (src->sync) {
772     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
774     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
775       /* get duration to calculate end time */
776       GstClockTime duration = GST_BUFFER_DURATION (buffer);
778       if (GST_CLOCK_TIME_IS_VALID (duration)) {
779         *end = timestamp + duration;
780       }
781       *start = timestamp;
782     }
783   } else {
784     *start = -1;
785     *end = -1;
786   }
789 static GstFlowReturn
790 gst_fake_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
791     GstBuffer ** ret)
793   GstFakeSrc *src;
794   GstBuffer *buf;
795   GstClockTime time;
797   src = GST_FAKE_SRC (basesrc);
799   buf = gst_fake_src_create_buffer (src);
800   GST_BUFFER_OFFSET (buf) = src->buffer_count++;
802   if (src->datarate > 0) {
803     time = (src->bytes_sent * GST_SECOND) / src->datarate;
805     GST_BUFFER_DURATION (buf) =
806         GST_BUFFER_SIZE (buf) * GST_SECOND / src->datarate;
807   } else if (gst_base_src_is_live (basesrc)) {
808     GstClock *clock;
810     clock = gst_element_get_clock (GST_ELEMENT (src));
812     if (clock) {
813       time = gst_clock_get_time (clock);
814       time -= gst_element_get_base_time (GST_ELEMENT (src));
815       gst_object_unref (clock);
816     } else {
817       /* not an error not to have a clock */
818       time = GST_CLOCK_TIME_NONE;
819     }
820   } else {
821     time = GST_CLOCK_TIME_NONE;
822   }
824   GST_BUFFER_TIMESTAMP (buf) = time;
826   if (!src->silent) {
827     gchar ts_str[64], dur_str[64];
829     GST_OBJECT_LOCK (src);
830     g_free (src->last_message);
832     if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
833       g_snprintf (ts_str, sizeof (ts_str), "%" GST_TIME_FORMAT,
834           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
835     } else {
836       g_strlcpy (ts_str, "none", sizeof (ts_str));
837     }
839     if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
840       g_snprintf (dur_str, sizeof (dur_str), "%" GST_TIME_FORMAT,
841           GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
842     } else {
843       g_strlcpy (dur_str, "none", sizeof (dur_str));
844     }
846     src->last_message =
847         g_strdup_printf ("get      ******* > (%5d bytes, timestamp: %s"
848         ", duration: %s, offset: %" G_GINT64_FORMAT ", offset_end: %"
849         G_GINT64_FORMAT ", flags: %d) %p", GST_BUFFER_SIZE (buf), ts_str,
850         dur_str, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
851         GST_MINI_OBJECT (buf)->flags, buf);
852     GST_OBJECT_UNLOCK (src);
854     g_object_notify (G_OBJECT (src), "last_message");
855   }
857   if (src->signal_handoffs) {
858     GST_LOG_OBJECT (src, "pre handoff emit");
859     g_signal_emit (G_OBJECT (src), gst_fake_src_signals[SIGNAL_HANDOFF], 0,
860         buf, basesrc->srcpad);
861     GST_LOG_OBJECT (src, "post handoff emit");
862   }
864   src->bytes_sent += GST_BUFFER_SIZE (buf);
866   *ret = buf;
867   return GST_FLOW_OK;
870 static gboolean
871 gst_fake_src_start (GstBaseSrc * basesrc)
873   GstFakeSrc *src;
875   src = GST_FAKE_SRC (basesrc);
877   src->buffer_count = 0;
878   src->pattern_byte = 0x00;
879   src->bytes_sent = 0;
881   gst_base_src_set_format (basesrc, src->format);
883   return TRUE;
886 static gboolean
887 gst_fake_src_stop (GstBaseSrc * basesrc)
889   GstFakeSrc *src;
891   src = GST_FAKE_SRC (basesrc);
893   GST_OBJECT_LOCK (src);
894   if (src->parent) {
895     gst_buffer_unref (src->parent);
896     src->parent = NULL;
897   }
898   g_free (src->last_message);
899   src->last_message = NULL;
900   GST_OBJECT_UNLOCK (src);
902   return TRUE;
905 static gboolean
906 gst_fake_src_is_seekable (GstBaseSrc * basesrc)
908   GstFakeSrc *src = GST_FAKE_SRC (basesrc);
910   return src->can_activate_pull;