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