- Make the return GList of gst_element_get_pad_list as const
[platform/upstream/gstreamer.git] / gst / elements / gstfakesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
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
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <gstfakesrc.h>
28
29
30 GstElementDetails gst_fakesrc_details = {
31   "Fake Source",
32   "Source",
33   "Push empty (no data) buffers around",
34   VERSION,
35   "Erik Walthinsen <omega@cse.ogi.edu>\n"
36   "Wim Taymans <wim.taymans@chello.be>",
37   "(C) 1999",
38 };
39
40
41 /* FakeSrc signals and args */
42 enum {
43   /* FILL ME */
44   SIGNAL_HANDOFF,
45   LAST_SIGNAL
46 };
47
48 enum {
49   ARG_0,
50   ARG_NUM_SOURCES,
51   ARG_LOOP_BASED,
52   ARG_OUTPUT,
53   ARG_DATA,
54   ARG_SIZETYPE,
55   ARG_SIZEMIN,
56   ARG_SIZEMAX,
57   ARG_FILLTYPE,
58   ARG_PATTERN,
59   ARG_NUM_BUFFERS,
60   ARG_EOS,
61   ARG_SILENT,
62   ARG_DUMP,
63   ARG_PARENTSIZE,
64   ARG_LAST_MESSAGE,
65 };
66
67 GST_PAD_TEMPLATE_FACTORY (fakesrc_src_factory,
68   "src%d",
69   GST_PAD_SRC,
70   GST_PAD_REQUEST,
71   NULL                  /* no caps */
72 );
73
74 #define GST_TYPE_FAKESRC_OUTPUT (gst_fakesrc_output_get_type())
75 static GType
76 gst_fakesrc_output_get_type (void) 
77 {
78   static GType fakesrc_output_type = 0;
79   static GEnumValue fakesrc_output[] = {
80     { FAKESRC_FIRST_LAST_LOOP,          "1", "First-Last loop"},
81     { FAKESRC_LAST_FIRST_LOOP,          "2", "Last-First loop"},
82     { FAKESRC_PING_PONG,                "3", "Ping-Pong"},
83     { FAKESRC_ORDERED_RANDOM,           "4", "Ordered Random"},
84     { FAKESRC_RANDOM,                   "5", "Random"},
85     { FAKESRC_PATTERN_LOOP,             "6", "Patttern loop"},
86     { FAKESRC_PING_PONG_PATTERN,        "7", "Ping-Pong Pattern"},
87     { FAKESRC_GET_ALWAYS_SUCEEDS,       "8", "'_get' Always succeeds"},
88     {0, NULL, NULL},
89   };
90   if (!fakesrc_output_type) {
91     fakesrc_output_type = g_enum_register_static ("GstFakeSrcOutput", fakesrc_output);
92   }
93   return fakesrc_output_type;
94 }
95
96 #define GST_TYPE_FAKESRC_DATA (gst_fakesrc_data_get_type())
97 static GType
98 gst_fakesrc_data_get_type (void) 
99 {
100   static GType fakesrc_data_type = 0;
101   static GEnumValue fakesrc_data[] = {
102     { FAKESRC_DATA_ALLOCATE,            "1", "Allocate data"},
103     { FAKESRC_DATA_SUBBUFFER,           "2", "Subbuffer data"},
104     { FAKESRC_DATA_BUFFERPOOL,          "3", "Use the default buffer pool (forces sizetype=2)"},
105     {0, NULL, NULL},
106   };
107   if (!fakesrc_data_type) {
108     fakesrc_data_type = g_enum_register_static ("GstFakeSrcData", fakesrc_data);
109   }
110   return fakesrc_data_type;
111 }
112
113 #define GST_TYPE_FAKESRC_SIZETYPE (gst_fakesrc_sizetype_get_type())
114 static GType
115 gst_fakesrc_sizetype_get_type (void) 
116 {
117   static GType fakesrc_sizetype_type = 0;
118   static GEnumValue fakesrc_sizetype[] = {
119     { FAKESRC_SIZETYPE_NULL,            "1", "Send empty buffers"},
120     { FAKESRC_SIZETYPE_FIXED,           "2", "Fixed size buffers (sizemax sized)"},
121     { FAKESRC_SIZETYPE_RANDOM,          "3", "Random sized buffers (sizemin <= size <= sizemax)"},
122     {0, NULL, NULL},
123   };
124   if (!fakesrc_sizetype_type) {
125     fakesrc_sizetype_type = g_enum_register_static ("GstFakeSrcSizeType", fakesrc_sizetype);
126   }
127   return fakesrc_sizetype_type;
128 }
129
130 #define GST_TYPE_FAKESRC_FILLTYPE (gst_fakesrc_filltype_get_type())
131 static GType
132 gst_fakesrc_filltype_get_type (void) 
133 {
134   static GType fakesrc_filltype_type = 0;
135   static GEnumValue fakesrc_filltype[] = {
136     { FAKESRC_FILLTYPE_NOTHING,         "1", "Leave data as malloced"},
137     { FAKESRC_FILLTYPE_NULL,            "2", "Fill buffers with zeros"},
138     { FAKESRC_FILLTYPE_RANDOM,          "3", "Fill buffers with random crap"},
139     { FAKESRC_FILLTYPE_PATTERN,         "4", "Fill buffers with pattern 0x00 -> 0xff"},
140     { FAKESRC_FILLTYPE_PATTERN_CONT,    "5", "Fill buffers with pattern 0x00 -> 0xff that spans buffers"},
141     {0, NULL, NULL},
142   };
143   if (!fakesrc_filltype_type) {
144     fakesrc_filltype_type = g_enum_register_static ("GstFakeSrcFillType", fakesrc_filltype);
145   }
146   return fakesrc_filltype_type;
147 }
148
149 static void             gst_fakesrc_class_init          (GstFakeSrcClass *klass);
150 static void             gst_fakesrc_init                (GstFakeSrc *fakesrc);
151
152 static GstPad*          gst_fakesrc_request_new_pad     (GstElement *element, GstPadTemplate *templ);
153 static void             gst_fakesrc_update_functions    (GstFakeSrc *src);
154 static void             gst_fakesrc_set_property        (GObject *object, guint prop_id, 
155                                                          const GValue *value, GParamSpec *pspec);
156 static void             gst_fakesrc_get_property        (GObject *object, guint prop_id, 
157                                                          GValue *value, GParamSpec *pspec);
158
159 static GstElementStateReturn gst_fakesrc_change_state   (GstElement *element);
160
161 static GstBuffer*       gst_fakesrc_get                 (GstPad *pad);
162 static void             gst_fakesrc_loop                (GstElement *element);
163
164 static GstElementClass *parent_class = NULL;
165 static guint gst_fakesrc_signals[LAST_SIGNAL] = { 0 };
166
167 GType
168 gst_fakesrc_get_type (void) 
169 {
170   static GType fakesrc_type = 0;
171
172   if (!fakesrc_type) {
173     static const GTypeInfo fakesrc_info = {
174       sizeof(GstFakeSrcClass),
175       NULL,
176       NULL,
177       (GClassInitFunc)gst_fakesrc_class_init,
178       NULL,
179       NULL,
180       sizeof(GstFakeSrc),
181       0,
182       (GInstanceInitFunc)gst_fakesrc_init,
183     };
184     fakesrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFakeSrc", &fakesrc_info, 0);
185   }
186   return fakesrc_type;
187 }
188
189 static void
190 gst_fakesrc_class_init (GstFakeSrcClass *klass) 
191 {
192   GObjectClass *gobject_class;
193   GstElementClass *gstelement_class;
194
195   gobject_class = (GObjectClass*)klass;
196   gstelement_class = (GstElementClass*)klass;
197
198   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
199
200   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_SOURCES,
201     g_param_spec_int ("num-sources", "num-sources", "Number of sources",
202                       1, G_MAXINT, 1, G_PARAM_READABLE));
203   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOOP_BASED,
204     g_param_spec_boolean("loop-based","loop-based","Enable loop-based operation",
205                          FALSE, G_PARAM_READWRITE));
206   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_OUTPUT,
207     g_param_spec_enum("output","output","Output method (currently unused)",
208                       GST_TYPE_FAKESRC_OUTPUT,FAKESRC_FIRST_LAST_LOOP,G_PARAM_READWRITE));
209   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DATA,
210     g_param_spec_enum ("data", "data", "Data allocation method",
211                        GST_TYPE_FAKESRC_DATA, FAKESRC_DATA_ALLOCATE, G_PARAM_READWRITE)); 
212   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZETYPE,
213     g_param_spec_enum ("sizetype", "sizetype", "How to determine buffer sizes",
214                        GST_TYPE_FAKESRC_SIZETYPE, FAKESRC_SIZETYPE_NULL, G_PARAM_READWRITE)); 
215   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZEMIN,
216     g_param_spec_int ("sizemin","sizemin","Minimum buffer size",
217                       0, G_MAXINT, 0, G_PARAM_READWRITE)); 
218   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZEMAX,
219     g_param_spec_int ("sizemax","sizemax","Maximum buffer size",
220                       0, G_MAXINT, 4096, G_PARAM_READWRITE)); 
221   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PARENTSIZE,
222     g_param_spec_int ("parentsize","parentsize","Size of parent buffer for sub-buffered allocation",
223                       0, G_MAXINT, 4096 * 10, G_PARAM_READWRITE)); 
224   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILLTYPE,
225     g_param_spec_enum ("filltype", "filltype", "How to fill the buffer, if at all",
226                        GST_TYPE_FAKESRC_FILLTYPE, FAKESRC_FILLTYPE_NULL, G_PARAM_READWRITE)); 
227   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PATTERN,
228     g_param_spec_string("pattern","pattern","pattern",
229                         NULL, G_PARAM_READWRITE));
230   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NUM_BUFFERS,
231     g_param_spec_int("num-buffers","num-buffers","Number of buffers to output before sending EOS",
232                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
233   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_EOS,
234     g_param_spec_boolean("eos","eos","Send out the EOS event?",
235                          TRUE,G_PARAM_READWRITE));
236   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
237     g_param_spec_string ("last-message", "last-message", "The last status message",
238                          NULL, G_PARAM_READABLE)); 
239
240   gst_element_class_install_std_props (
241           GST_ELEMENT_CLASS (klass),
242           "silent", ARG_SILENT, G_PARAM_READWRITE,
243           "dump",   ARG_DUMP,   G_PARAM_READWRITE,
244           NULL);
245
246   gst_fakesrc_signals[SIGNAL_HANDOFF] =
247     g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
248                   G_STRUCT_OFFSET (GstFakeSrcClass, handoff), NULL, NULL,
249                   g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
250                   G_TYPE_POINTER);
251
252   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fakesrc_set_property);
253   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fakesrc_get_property);
254
255   gstelement_class->request_new_pad =   GST_DEBUG_FUNCPTR (gst_fakesrc_request_new_pad);
256   gstelement_class->change_state =      GST_DEBUG_FUNCPTR (gst_fakesrc_change_state);
257 }
258
259 static void 
260 gst_fakesrc_init (GstFakeSrc *fakesrc) 
261 {
262   GstPad *pad;
263
264   /* create our first output pad */
265   pad = gst_pad_new ("src", GST_PAD_SRC);
266   gst_element_add_pad (GST_ELEMENT (fakesrc), pad);
267
268   fakesrc->loop_based = FALSE;
269   gst_fakesrc_update_functions (fakesrc);
270
271   fakesrc->output = FAKESRC_FIRST_LAST_LOOP;
272   fakesrc->segment_start = -1;
273   fakesrc->segment_end = -1;
274   fakesrc->num_buffers = -1;
275   fakesrc->rt_num_buffers = -1;
276   fakesrc->buffer_count = 0;
277   fakesrc->silent = FALSE;
278   fakesrc->dump = FALSE;
279   fakesrc->pattern_byte = 0x00;
280   fakesrc->need_flush = FALSE;
281   fakesrc->data = FAKESRC_DATA_ALLOCATE;
282   fakesrc->sizetype = FAKESRC_SIZETYPE_NULL;
283   fakesrc->filltype = FAKESRC_FILLTYPE_NOTHING;
284   fakesrc->sizemin = 0;
285   fakesrc->sizemax = 4096;
286   fakesrc->parent = NULL;
287   fakesrc->parentsize = 4096 * 10;
288   fakesrc->last_message = NULL;
289 }
290
291 static GstPad*
292 gst_fakesrc_request_new_pad (GstElement *element, GstPadTemplate *templ)
293 {
294   gchar *name;
295   GstPad *srcpad;
296   GstFakeSrc *fakesrc;
297
298   g_return_val_if_fail (GST_IS_FAKESRC (element), NULL);
299
300   if (templ->direction != GST_PAD_SRC) {
301     g_warning ("gstfakesrc: request new pad that is not a SRC pad\n");
302     return NULL;
303   }
304
305   fakesrc = GST_FAKESRC (element);
306
307   name = g_strdup_printf ("src%d", GST_ELEMENT (fakesrc)->numsrcpads);
308
309   srcpad = gst_pad_new_from_template (templ, name);
310   gst_element_add_pad (GST_ELEMENT (fakesrc), srcpad);
311   gst_fakesrc_update_functions (fakesrc);
312
313   g_free (name);
314
315   return srcpad;
316 }
317
318 GST_FORMATS_FUNCTION (gst_fakesrc_get_formats,
319   GST_FORMAT_DEFAULT
320 )
321
322 GST_PAD_QUERY_TYPE_FUNCTION (gst_fakesrc_get_query_types,
323   GST_PAD_QUERY_TOTAL,
324   GST_PAD_QUERY_POSITION,
325   GST_PAD_QUERY_START,
326   GST_PAD_QUERY_SEGMENT_END
327
328
329 static gboolean
330 gst_fakesrc_query (GstPad *pad, GstPadQueryType type,
331                    GstFormat *format, gint64 *value)
332 {
333   GstFakeSrc *src = GST_FAKESRC (GST_PAD_PARENT (pad));
334
335   switch (type) {
336     case GST_PAD_QUERY_TOTAL:
337       *value = src->num_buffers;
338       break;
339     case GST_PAD_QUERY_POSITION:
340       *value = src->buffer_count;
341       break;
342     case GST_PAD_QUERY_START:
343       *value = src->segment_start;
344       break;
345     case GST_PAD_QUERY_SEGMENT_END:
346       *value = src->segment_end;
347       break;
348     default:
349       return FALSE;
350   } 
351   return TRUE;
352 }
353
354 GST_EVENT_MASK_FUNCTION (gst_fakesrc_get_event_mask,
355   { GST_EVENT_SEEK, GST_SEEK_FLAG_FLUSH },
356   { GST_EVENT_SEEK_SEGMENT, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_SEGMENT_LOOP },
357   { GST_EVENT_FLUSH, 0 }
358 )
359
360 static gboolean
361 gst_fakesrc_event_handler (GstPad *pad, GstEvent *event)
362 {
363   GstFakeSrc *src;
364
365   src = GST_FAKESRC (gst_pad_get_parent (pad));
366
367   switch (GST_EVENT_TYPE (event)) {
368     case GST_EVENT_SEEK:
369       src->buffer_count = GST_EVENT_SEEK_OFFSET (event);
370
371       if (!GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH) {
372         break;
373       }
374       /* else we do a flush too */
375     case GST_EVENT_SEEK_SEGMENT:
376       src->segment_start = GST_EVENT_SEEK_OFFSET (event);
377       src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
378       src->buffer_count = src->segment_start;
379       src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
380       break;
381     case GST_EVENT_FLUSH:
382       src->need_flush = TRUE;
383       break;
384     default:
385       break;
386   }
387   gst_event_unref (event);
388
389   return TRUE;
390 }
391
392 static void
393 gst_fakesrc_update_functions (GstFakeSrc *src)
394 {
395   GList *pads;
396
397   if (src->loop_based) {
398     gst_element_set_loop_function (GST_ELEMENT (src), GST_DEBUG_FUNCPTR (gst_fakesrc_loop));
399   }
400   else {
401     gst_element_set_loop_function (GST_ELEMENT (src), NULL);
402   }
403
404   pads = GST_ELEMENT (src)->pads;
405   while (pads) {
406     GstPad *pad = GST_PAD (pads->data);
407
408     if (src->loop_based) {
409       gst_pad_set_get_function (pad, NULL);
410     }
411     else {
412       gst_pad_set_get_function (pad, GST_DEBUG_FUNCPTR (gst_fakesrc_get));
413     }
414
415     gst_pad_set_event_function (pad, gst_fakesrc_event_handler);
416     gst_pad_set_event_mask_function (pad, gst_fakesrc_get_event_mask);
417     gst_pad_set_query_function (pad, gst_fakesrc_query);
418     gst_pad_set_query_type_function (pad, gst_fakesrc_get_query_types);
419     gst_pad_set_formats_function (pad, gst_fakesrc_get_formats);
420     pads = g_list_next (pads);
421   }
422 }
423
424 static void
425 gst_fakesrc_alloc_parent (GstFakeSrc *src)
426 {
427   GstBuffer *buf;
428
429   buf = gst_buffer_new ();
430   GST_BUFFER_DATA (buf) = g_malloc (src->parentsize);
431   GST_BUFFER_SIZE (buf) = src->parentsize;
432
433   src->parent = buf;
434   src->parentoffset = 0;
435 }
436
437 static void
438 gst_fakesrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
439 {
440   GstFakeSrc *src;
441
442   /* it's not null if we got it, but it might not be ours */
443   src = GST_FAKESRC (object);
444    
445   switch (prop_id) {
446     case ARG_LOOP_BASED:
447       src->loop_based = g_value_get_boolean (value);
448       gst_fakesrc_update_functions (src);
449       break;
450     case ARG_OUTPUT:
451       g_warning ("not yet implemented");
452       break;
453     case ARG_DATA:
454       src->data = g_value_get_enum (value);
455
456       if (src->data == FAKESRC_DATA_SUBBUFFER) {
457         if (!src->parent)
458           gst_fakesrc_alloc_parent (src);
459       } else {
460         if (src->parent) {
461           gst_buffer_unref (src->parent);
462           src->parent = NULL;
463         }
464       }
465       
466       if (src->data == FAKESRC_DATA_BUFFERPOOL) {
467         if (src->sizetype != FAKESRC_SIZETYPE_FIXED)
468           g_object_set (src, "sizetype", FAKESRC_SIZETYPE_FIXED, NULL);
469         
470         if (!src->pool) 
471           src->pool = gst_buffer_pool_get_default (src->sizemax, 10);
472       } else {
473         if (src->pool) {
474           gst_buffer_pool_free (src->pool);
475           src->pool = NULL;
476         }
477       }
478       break;
479     case ARG_SIZETYPE:
480       src->sizetype = g_value_get_enum (value);
481       break;
482     case ARG_SIZEMIN:
483       src->sizemin = g_value_get_int (value);
484       break;
485     case ARG_SIZEMAX:
486       src->sizemax = g_value_get_int (value);
487       break;
488     case ARG_PARENTSIZE:
489       src->parentsize = g_value_get_int (value);
490       break;
491     case ARG_FILLTYPE:
492       src->filltype = g_value_get_enum (value);
493       break;
494     case ARG_PATTERN:
495       break;
496     case ARG_NUM_BUFFERS:
497       src->num_buffers = g_value_get_int (value);
498       break;
499     case ARG_EOS:
500       src->eos = g_value_get_boolean (value);
501       GST_INFO (0, "will EOS on next buffer");
502       break;
503     case ARG_SILENT:
504       src->silent = g_value_get_boolean (value);
505       break;
506     case ARG_DUMP:
507       src->dump = g_value_get_boolean (value);
508       break;
509     default:
510       break;
511   }
512 }
513
514 static void 
515 gst_fakesrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
516 {
517   GstFakeSrc *src;
518    
519   /* it's not null if we got it, but it might not be ours */
520   g_return_if_fail (GST_IS_FAKESRC (object));
521   
522   src = GST_FAKESRC (object);
523    
524   switch (prop_id) {
525     case ARG_NUM_SOURCES:
526       g_value_set_int (value, GST_ELEMENT (src)->numsrcpads);
527       break;
528     case ARG_LOOP_BASED:
529       g_value_set_boolean (value, src->loop_based);
530       break;
531     case ARG_OUTPUT:
532       g_value_set_enum (value, src->output);
533       break;
534     case ARG_DATA:
535       g_value_set_enum (value, src->data);
536       break;
537     case ARG_SIZETYPE:
538       g_value_set_enum (value, src->sizetype);
539       break;
540     case ARG_SIZEMIN:
541       g_value_set_int (value, src->sizemin);
542       break;
543     case ARG_SIZEMAX:
544       g_value_set_int (value, src->sizemax);
545       break;
546     case ARG_PARENTSIZE:
547       g_value_set_int (value, src->parentsize);
548       break;
549     case ARG_FILLTYPE:
550       g_value_set_enum (value, src->filltype);
551       break;
552     case ARG_PATTERN:
553       g_value_set_string (value, src->pattern);
554       break;
555     case ARG_NUM_BUFFERS:
556       g_value_set_int (value, src->num_buffers);
557       break;
558     case ARG_EOS:
559       g_value_set_boolean (value, src->eos);
560       break;
561     case ARG_SILENT:
562       g_value_set_boolean (value, src->silent);
563       break;
564     case ARG_DUMP:
565       g_value_set_boolean (value, src->dump);
566       break;
567     case ARG_LAST_MESSAGE:
568       g_value_set_string (value, src->last_message);
569       break;
570     default:
571       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
572       break;
573   }
574 }
575
576 static void
577 gst_fakesrc_prepare_buffer (GstFakeSrc *src, GstBuffer *buf)
578 {
579   if (GST_BUFFER_SIZE (buf) == 0) 
580     return;
581
582   switch (src->filltype) {
583     case FAKESRC_FILLTYPE_NULL:
584       memset (GST_BUFFER_DATA (buf), 0, GST_BUFFER_SIZE (buf));
585       break;
586     case FAKESRC_FILLTYPE_RANDOM:
587     {
588       gint i;
589       guint8 *ptr = GST_BUFFER_DATA (buf);
590
591       for (i = GST_BUFFER_SIZE (buf); i; i--) {
592         *ptr++ = (gint8)((255.0)*rand()/(RAND_MAX));
593       }
594       break;
595     }
596     case FAKESRC_FILLTYPE_PATTERN:
597       src->pattern_byte = 0x00;
598     case FAKESRC_FILLTYPE_PATTERN_CONT:
599     {
600       gint i;
601       guint8 *ptr = GST_BUFFER_DATA (buf);
602
603       for (i = GST_BUFFER_SIZE (buf); i; i--) {
604         *ptr++ = src->pattern_byte++;
605       }
606       break;
607     }
608     case FAKESRC_FILLTYPE_NOTHING:
609     default:
610       break;
611   }
612 }
613
614 static GstBuffer*
615 gst_fakesrc_alloc_buffer (GstFakeSrc *src, guint size)
616 {
617   GstBuffer *buf;
618
619   buf = gst_buffer_new ();
620   GST_BUFFER_SIZE(buf) = size;
621
622   if (size != 0) { 
623     switch (src->filltype) {
624       case FAKESRC_FILLTYPE_NOTHING:
625         GST_BUFFER_DATA(buf) = g_malloc (size);
626         break;
627       case FAKESRC_FILLTYPE_NULL:
628         GST_BUFFER_DATA(buf) = g_malloc0 (size);
629         break;
630       case FAKESRC_FILLTYPE_RANDOM:
631       case FAKESRC_FILLTYPE_PATTERN:
632       case FAKESRC_FILLTYPE_PATTERN_CONT:
633       default:
634         GST_BUFFER_DATA(buf) = g_malloc (size);
635         gst_fakesrc_prepare_buffer (src, buf);
636         break;
637     }
638   }
639
640   return buf;
641 }
642
643 static guint
644 gst_fakesrc_get_size (GstFakeSrc *src)
645 {
646   guint size;
647
648   switch (src->sizetype) {
649     case FAKESRC_SIZETYPE_FIXED:
650       size = src->sizemax;
651       break;
652     case FAKESRC_SIZETYPE_RANDOM:
653       size = src->sizemin + (guint8)(((gfloat)src->sizemax)*rand()/(RAND_MAX + (gfloat)src->sizemin));
654       break;
655     case FAKESRC_SIZETYPE_NULL:
656     default:
657       size = 0; 
658       break;
659   }
660
661   return size;
662 }
663
664 static GstBuffer *
665 gst_fakesrc_create_buffer (GstFakeSrc *src)
666 {
667   GstBuffer *buf;
668   guint size;
669   gboolean dump = src->dump;
670
671   size = gst_fakesrc_get_size (src);
672   if (size == 0)
673     return gst_buffer_new();
674
675   switch (src->data) {
676     case FAKESRC_DATA_ALLOCATE:
677       buf = gst_fakesrc_alloc_buffer (src, size);
678       break;
679     case FAKESRC_DATA_SUBBUFFER:
680       /* see if we have a parent to subbuffer */
681       if (!src->parent) {
682         gst_fakesrc_alloc_parent (src);
683         g_assert (src->parent);
684       }
685       /* see if it's large enough */
686       if ((GST_BUFFER_SIZE (src->parent) - src->parentoffset) >= size) {
687          buf = gst_buffer_create_sub (src->parent, src->parentoffset, size);
688          src->parentoffset += size;
689       }
690       else {
691         /* the parent is useless now */
692         gst_buffer_unref (src->parent);
693         src->parent = NULL;
694         /* try again (this will allocate a new parent) */
695         return gst_fakesrc_create_buffer (src);
696       }
697       gst_fakesrc_prepare_buffer (src, buf);
698       break;
699     case FAKESRC_DATA_BUFFERPOOL:
700       buf = gst_buffer_new_from_pool (src->pool, 0, 0);
701       gst_fakesrc_prepare_buffer (src, buf);
702       break;
703     default:
704       g_warning ("fakesrc: dunno how to allocate buffers !");
705       buf = gst_buffer_new();
706       break;
707   }
708   if (dump) {
709     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
710   }
711
712   return buf;
713 }
714
715 static GstBuffer *
716 gst_fakesrc_get(GstPad *pad)
717 {
718   GstFakeSrc *src;
719   GstBuffer *buf;
720
721   g_return_val_if_fail (pad != NULL, NULL);
722
723   src = GST_FAKESRC (gst_pad_get_parent (pad));
724
725   g_return_val_if_fail (GST_IS_FAKESRC (src), NULL);
726
727   if (src->need_flush) {
728     src->need_flush = FALSE;
729     return GST_BUFFER(gst_event_new (GST_EVENT_FLUSH));
730   }
731
732   if (src->buffer_count == src->segment_end) {
733     if (src->segment_loop) {
734       return GST_BUFFER(gst_event_new (GST_EVENT_SEGMENT_DONE));
735     }
736     else {
737       gst_element_set_eos (GST_ELEMENT (src));
738       return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
739     }
740   }
741
742   if (src->rt_num_buffers == 0) {
743     gst_element_set_eos (GST_ELEMENT (src));
744     return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
745   }
746   else {
747     if (src->rt_num_buffers > 0)
748       src->rt_num_buffers--;
749   }
750
751   if (src->eos) {
752     GST_INFO (0, "fakesrc is setting eos on pad");
753     return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
754   }
755
756   buf = gst_fakesrc_create_buffer (src);
757   GST_BUFFER_TIMESTAMP (buf) = src->buffer_count++;
758
759   if (!src->silent) {
760     if (src->last_message)
761       g_free (src->last_message);
762
763     src->last_message = g_strdup_printf ("get      ******* (%s:%s)> (%d bytes, %llu) %p",
764                       GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), buf);
765
766     g_object_notify (G_OBJECT (src), "last_message");
767   }
768
769   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, src, "pre handoff emit");
770   g_signal_emit (G_OBJECT (src), gst_fakesrc_signals[SIGNAL_HANDOFF], 0,
771                    buf, pad);
772   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, src, "post handoff emit");
773
774   return buf;
775 }
776
777 /**
778  * gst_fakesrc_loop:
779  * @element: the faksesrc to loop
780  * 
781  * generate an empty buffer and push it to the next element.
782  */
783 static void
784 gst_fakesrc_loop(GstElement *element)
785 {
786   GstFakeSrc *src;
787   const GList *pads;
788
789   g_return_if_fail(element != NULL);
790   g_return_if_fail(GST_IS_FAKESRC(element));
791
792   src = GST_FAKESRC (element);
793
794   pads = gst_element_get_pad_list (element);
795
796   while (pads) {
797     GstPad *pad = GST_PAD (pads->data);
798     GstBuffer *buf;
799
800     buf = gst_fakesrc_get (pad);
801     gst_pad_push (pad, buf);
802
803     if (src->eos) {
804       return;
805     }
806
807     pads = g_list_next (pads);
808   }
809 }
810
811 static GstElementStateReturn
812 gst_fakesrc_change_state (GstElement *element)
813 {
814   GstFakeSrc *fakesrc;
815
816   g_return_val_if_fail (GST_IS_FAKESRC (element), GST_STATE_FAILURE);
817
818   fakesrc = GST_FAKESRC (element);
819
820   switch (GST_STATE_TRANSITION (element)) {
821     case GST_STATE_PAUSED_TO_READY:
822     case GST_STATE_NULL_TO_READY:
823       fakesrc->buffer_count = 0;
824       fakesrc->pattern_byte = 0x00;
825       fakesrc->need_flush = FALSE;
826       fakesrc->eos = FALSE;
827       fakesrc->rt_num_buffers = fakesrc->num_buffers;
828       break;
829     case GST_STATE_READY_TO_PAUSED:
830     case GST_STATE_PAUSED_TO_PLAYING:
831     case GST_STATE_PLAYING_TO_PAUSED:
832       break;
833     case GST_STATE_READY_TO_NULL:
834       if (fakesrc->parent) {
835         gst_buffer_unref (fakesrc->parent);
836         fakesrc->parent = NULL;
837       }
838       if (fakesrc->pool) {
839         gst_buffer_pool_unref (fakesrc->pool);
840         fakesrc->pool = NULL;
841       }
842       break;
843     default:
844       g_assert_not_reached ();
845       break;
846   }
847
848   if (GST_ELEMENT_CLASS (parent_class)->change_state)
849     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
850
851   return GST_STATE_SUCCESS;
852 }
853
854 gboolean
855 gst_fakesrc_factory_init (GstElementFactory *factory)
856 {
857   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (fakesrc_src_factory));
858
859   return TRUE;
860 }
861