2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
27 #include <gstfakesrc.h>
30 GstElementDetails gst_fakesrc_details = {
33 "Push empty (no data) buffers around",
35 "Erik Walthinsen <omega@cse.ogi.edu>\n"
36 "Wim Taymans <wim.taymans@chello.be>",
41 /* FakeSrc signals and args */
67 GST_PADTEMPLATE_FACTORY (fakesrc_src_factory,
74 #define GST_TYPE_FAKESRC_OUTPUT (gst_fakesrc_output_get_type())
76 gst_fakesrc_output_get_type (void)
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"},
90 if (!fakesrc_output_type) {
91 fakesrc_output_type = g_enum_register_static ("GstFakeSrcOutput", fakesrc_output);
93 return fakesrc_output_type;
96 #define GST_TYPE_FAKESRC_DATA (gst_fakesrc_data_get_type())
98 gst_fakesrc_data_get_type (void)
100 static GType fakesrc_data_type = 0;
101 static GEnumValue fakesrc_data[] = {
102 { FAKESRC_DATA_ALLOCATE, "2", "Allocate data"},
103 { FAKESRC_DATA_SUBBUFFER, "3", "Subbuffer data"},
106 if (!fakesrc_data_type) {
107 fakesrc_data_type = g_enum_register_static ("GstFakeSrcData", fakesrc_data);
109 return fakesrc_data_type;
112 #define GST_TYPE_FAKESRC_SIZETYPE (gst_fakesrc_sizetype_get_type())
114 gst_fakesrc_sizetype_get_type (void)
116 static GType fakesrc_sizetype_type = 0;
117 static GEnumValue fakesrc_sizetype[] = {
118 { FAKESRC_SIZETYPE_NULL, "1", "Send empty buffers"},
119 { FAKESRC_SIZETYPE_FIXED, "2", "Fixed size buffers (sizemax sized)"},
120 { FAKESRC_SIZETYPE_RANDOM, "3", "Random sized buffers (sizemin <= size <= sizemax)"},
123 if (!fakesrc_sizetype_type) {
124 fakesrc_sizetype_type = g_enum_register_static ("GstFakeSrcSizeType", fakesrc_sizetype);
126 return fakesrc_sizetype_type;
129 #define GST_TYPE_FAKESRC_FILLTYPE (gst_fakesrc_filltype_get_type())
131 gst_fakesrc_filltype_get_type (void)
133 static GType fakesrc_filltype_type = 0;
134 static GEnumValue fakesrc_filltype[] = {
135 { FAKESRC_FILLTYPE_NOTHING, "1", "Leave data as malloced"},
136 { FAKESRC_FILLTYPE_NULL, "2", "Fill buffers with zeros"},
137 { FAKESRC_FILLTYPE_RANDOM, "3", "Fill buffers with random crap"},
138 { FAKESRC_FILLTYPE_PATTERN, "4", "Fill buffers with pattern 0x00 -> 0xff"},
139 { FAKESRC_FILLTYPE_PATTERN_CONT, "5", "Fill buffers with pattern 0x00 -> 0xff that spans buffers"},
142 if (!fakesrc_filltype_type) {
143 fakesrc_filltype_type = g_enum_register_static ("GstFakeSrcFillType", fakesrc_filltype);
145 return fakesrc_filltype_type;
148 static void gst_fakesrc_class_init (GstFakeSrcClass *klass);
149 static void gst_fakesrc_init (GstFakeSrc *fakesrc);
151 static GstPad* gst_fakesrc_request_new_pad (GstElement *element, GstPadTemplate *templ);
152 static void gst_fakesrc_update_functions (GstFakeSrc *src);
153 static void gst_fakesrc_set_property (GObject *object, guint prop_id,
154 const GValue *value, GParamSpec *pspec);
155 static void gst_fakesrc_get_property (GObject *object, guint prop_id,
156 GValue *value, GParamSpec *pspec);
158 static GstElementStateReturn gst_fakesrc_change_state (GstElement *element);
160 static GstBuffer* gst_fakesrc_get (GstPad *pad);
161 static void gst_fakesrc_loop (GstElement *element);
163 static GstElementClass *parent_class = NULL;
164 static guint gst_fakesrc_signals[LAST_SIGNAL] = { 0 };
167 gst_fakesrc_get_type (void)
169 static GType fakesrc_type = 0;
172 static const GTypeInfo fakesrc_info = {
173 sizeof(GstFakeSrcClass),
176 (GClassInitFunc)gst_fakesrc_class_init,
181 (GInstanceInitFunc)gst_fakesrc_init,
183 fakesrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFakeSrc", &fakesrc_info, 0);
189 gst_fakesrc_class_init (GstFakeSrcClass *klass)
191 GObjectClass *gobject_class;
192 GstElementClass *gstelement_class;
194 gobject_class = (GObjectClass*)klass;
195 gstelement_class = (GstElementClass*)klass;
197 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
199 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_SOURCES,
200 g_param_spec_int ("num_sources", "num_sources", "num_sources",
201 1, G_MAXINT, 1, G_PARAM_READABLE));
202 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOOP_BASED,
203 g_param_spec_boolean("loop_based","loop_based","loop_based",
204 FALSE, G_PARAM_READWRITE)); /* CHECKME */
205 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_OUTPUT,
206 g_param_spec_enum("output","output","output",
207 GST_TYPE_FAKESRC_OUTPUT,FAKESRC_FIRST_LAST_LOOP,G_PARAM_READWRITE)); /* CHECKME! */
208 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DATA,
209 g_param_spec_enum ("data", "data", "data",
210 GST_TYPE_FAKESRC_DATA, FAKESRC_DATA_ALLOCATE, G_PARAM_READWRITE));
211 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZETYPE,
212 g_param_spec_enum ("sizetype", "sizetype", "sizetype",
213 GST_TYPE_FAKESRC_SIZETYPE, FAKESRC_SIZETYPE_NULL, G_PARAM_READWRITE));
214 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZEMIN,
215 g_param_spec_int ("sizemin","sizemin","sizemin",
216 0, G_MAXINT, 0, G_PARAM_READWRITE));
217 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZEMAX,
218 g_param_spec_int ("sizemax","sizemax","sizemax",
219 0, G_MAXINT, 4096, G_PARAM_READWRITE));
220 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PARENTSIZE,
221 g_param_spec_int ("parentsize","parentsize","parentsize",
222 0, G_MAXINT, 4096 * 10, G_PARAM_READWRITE));
223 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILLTYPE,
224 g_param_spec_enum ("filltype", "filltype", "filltype",
225 GST_TYPE_FAKESRC_FILLTYPE, FAKESRC_FILLTYPE_NULL, G_PARAM_READWRITE));
226 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PATTERN,
227 g_param_spec_string("pattern","pattern","pattern",
228 NULL, G_PARAM_READWRITE)); /* CHECKME */
229 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NUM_BUFFERS,
230 g_param_spec_int("num_buffers","num_buffers","num_buffers",
231 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
232 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_EOS,
233 g_param_spec_boolean("eos","eos","eos",
234 TRUE,G_PARAM_READWRITE)); /* CHECKME */
235 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
236 g_param_spec_string ("last_message", "last_message", "last_message",
237 NULL, G_PARAM_READABLE));
239 gst_element_class_install_std_props (
240 GST_ELEMENT_CLASS (klass),
241 "silent", ARG_SILENT, G_PARAM_READWRITE,
242 "dump", ARG_DUMP, G_PARAM_READWRITE,
245 gst_fakesrc_signals[SIGNAL_HANDOFF] =
246 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
247 G_STRUCT_OFFSET (GstFakeSrcClass, handoff), NULL, NULL,
248 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
251 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fakesrc_set_property);
252 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fakesrc_get_property);
254 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (gst_fakesrc_request_new_pad);
255 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_fakesrc_change_state);
259 gst_fakesrc_init (GstFakeSrc *fakesrc)
263 /* create our first output pad */
264 pad = gst_pad_new ("src", GST_PAD_SRC);
265 gst_element_add_pad (GST_ELEMENT (fakesrc), pad);
267 fakesrc->loop_based = FALSE;
268 gst_fakesrc_update_functions (fakesrc);
270 fakesrc->output = FAKESRC_FIRST_LAST_LOOP;
271 fakesrc->num_buffers = -1;
272 fakesrc->rt_num_buffers = -1;
273 fakesrc->buffer_count = 0;
274 fakesrc->silent = FALSE;
275 fakesrc->dump = FALSE;
276 fakesrc->pattern_byte = 0x00;
277 fakesrc->need_flush = FALSE;
278 fakesrc->data = FAKESRC_DATA_ALLOCATE;
279 fakesrc->sizetype = FAKESRC_SIZETYPE_NULL;
280 fakesrc->filltype = FAKESRC_FILLTYPE_NOTHING;
281 fakesrc->sizemin = 0;
282 fakesrc->sizemax = 4096;
283 fakesrc->parent = NULL;
284 fakesrc->parentsize = 4096 * 10;
285 fakesrc->last_message = NULL;
289 gst_fakesrc_request_new_pad (GstElement *element, GstPadTemplate *templ)
295 g_return_val_if_fail (GST_IS_FAKESRC (element), NULL);
297 if (templ->direction != GST_PAD_SRC) {
298 g_warning ("gstfakesrc: request new pad that is not a SRC pad\n");
302 fakesrc = GST_FAKESRC (element);
304 name = g_strdup_printf ("src%d", GST_ELEMENT (fakesrc)->numsrcpads);
306 srcpad = gst_pad_new_from_template (templ, name);
307 gst_element_add_pad (GST_ELEMENT (fakesrc), srcpad);
315 gst_fakesrc_event_handler (GstPad *pad, GstEvent *event)
319 src = GST_FAKESRC (gst_pad_get_parent (pad));
321 switch (GST_EVENT_TYPE (event)) {
323 src->buffer_count = GST_EVENT_SEEK_OFFSET (event);
324 if (!GST_EVENT_SEEK_FLUSH (event)) {
325 gst_event_free (event);
328 /* else we do a flush too */
329 case GST_EVENT_FLUSH:
330 src->need_flush = TRUE;
340 gst_fakesrc_update_functions (GstFakeSrc *src)
344 if (src->loop_based) {
345 gst_element_set_loop_function (GST_ELEMENT (src), GST_DEBUG_FUNCPTR (gst_fakesrc_loop));
348 gst_element_set_loop_function (GST_ELEMENT (src), NULL);
351 pads = GST_ELEMENT (src)->pads;
353 GstPad *pad = GST_PAD (pads->data);
355 if (src->loop_based) {
356 gst_pad_set_get_function (pad, NULL);
359 gst_pad_set_get_function (pad, GST_DEBUG_FUNCPTR (gst_fakesrc_get));
362 gst_pad_set_event_function (pad, gst_fakesrc_event_handler);
363 pads = g_list_next (pads);
368 gst_fakesrc_alloc_parent (GstFakeSrc *src)
372 buf = gst_buffer_new ();
373 GST_BUFFER_DATA (buf) = g_malloc (src->parentsize);
374 GST_BUFFER_SIZE (buf) = src->parentsize;
377 src->parentoffset = 0;
381 gst_fakesrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
385 /* it's not null if we got it, but it might not be ours */
386 src = GST_FAKESRC (object);
390 src->loop_based = g_value_get_boolean (value);
391 gst_fakesrc_update_functions (src);
394 g_warning ("not yet implemented");
397 src->data = g_value_get_enum (value);
399 case FAKESRC_DATA_ALLOCATE:
401 gst_buffer_unref (src->parent);
405 case FAKESRC_DATA_SUBBUFFER:
407 gst_fakesrc_alloc_parent (src);
413 src->sizetype = g_value_get_enum (value);
416 src->sizemin = g_value_get_int (value);
419 src->sizemax = g_value_get_int (value);
422 src->parentsize = g_value_get_int (value);
425 src->filltype = g_value_get_enum (value);
429 case ARG_NUM_BUFFERS:
430 src->num_buffers = g_value_get_int (value);
433 src->eos = g_value_get_boolean (value);
434 GST_INFO (0, "will EOS on next buffer");
437 src->silent = g_value_get_boolean (value);
440 src->dump = g_value_get_boolean (value);
448 gst_fakesrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
452 /* it's not null if we got it, but it might not be ours */
453 g_return_if_fail (GST_IS_FAKESRC (object));
455 src = GST_FAKESRC (object);
458 case ARG_NUM_SOURCES:
459 g_value_set_int (value, GST_ELEMENT (src)->numsrcpads);
462 g_value_set_boolean (value, src->loop_based);
465 g_value_set_enum (value, src->output);
468 g_value_set_enum (value, src->data);
471 g_value_set_enum (value, src->sizetype);
474 g_value_set_int (value, src->sizemin);
477 g_value_set_int (value, src->sizemax);
480 g_value_set_int (value, src->parentsize);
483 g_value_set_enum (value, src->filltype);
486 g_value_set_string (value, src->pattern);
488 case ARG_NUM_BUFFERS:
489 g_value_set_int (value, src->num_buffers);
492 g_value_set_boolean (value, src->eos);
495 g_value_set_boolean (value, src->silent);
498 g_value_set_boolean (value, src->dump);
500 case ARG_LAST_MESSAGE:
501 g_value_set_string (value, src->last_message);
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
510 gst_fakesrc_prepare_buffer (GstFakeSrc *src, GstBuffer *buf)
512 if (GST_BUFFER_SIZE (buf) == 0)
515 switch (src->filltype) {
516 case FAKESRC_FILLTYPE_NULL:
517 memset (GST_BUFFER_DATA (buf), 0, GST_BUFFER_SIZE (buf));
519 case FAKESRC_FILLTYPE_RANDOM:
522 guint8 *ptr = GST_BUFFER_DATA (buf);
524 for (i = GST_BUFFER_SIZE (buf); i; i--) {
525 *ptr++ = (gint8)((255.0)*rand()/(RAND_MAX));
529 case FAKESRC_FILLTYPE_PATTERN:
530 src->pattern_byte = 0x00;
531 case FAKESRC_FILLTYPE_PATTERN_CONT:
534 guint8 *ptr = GST_BUFFER_DATA (buf);
536 for (i = GST_BUFFER_SIZE (buf); i; i--) {
537 *ptr++ = src->pattern_byte++;
541 case FAKESRC_FILLTYPE_NOTHING:
548 gst_fakesrc_alloc_buffer (GstFakeSrc *src, guint size)
552 buf = gst_buffer_new ();
553 GST_BUFFER_SIZE(buf) = size;
556 switch (src->filltype) {
557 case FAKESRC_FILLTYPE_NOTHING:
558 GST_BUFFER_DATA(buf) = g_malloc (size);
560 case FAKESRC_FILLTYPE_NULL:
561 GST_BUFFER_DATA(buf) = g_malloc0 (size);
563 case FAKESRC_FILLTYPE_RANDOM:
564 case FAKESRC_FILLTYPE_PATTERN:
565 case FAKESRC_FILLTYPE_PATTERN_CONT:
567 GST_BUFFER_DATA(buf) = g_malloc (size);
568 gst_fakesrc_prepare_buffer (src, buf);
577 gst_fakesrc_get_size (GstFakeSrc *src)
581 switch (src->sizetype) {
582 case FAKESRC_SIZETYPE_FIXED:
585 case FAKESRC_SIZETYPE_RANDOM:
586 size = src->sizemin + (guint8)(((gfloat)src->sizemax)*rand()/(RAND_MAX + (gfloat)src->sizemin));
588 case FAKESRC_SIZETYPE_NULL:
598 gst_fakesrc_create_buffer (GstFakeSrc *src)
602 gboolean dump = src->dump;
604 size = gst_fakesrc_get_size (src);
606 return gst_buffer_new();
609 case FAKESRC_DATA_ALLOCATE:
610 buf = gst_fakesrc_alloc_buffer (src, size);
612 case FAKESRC_DATA_SUBBUFFER:
613 /* see if we have a parent to subbuffer */
615 gst_fakesrc_alloc_parent (src);
616 g_assert (src->parent);
618 /* see if it's large enough */
619 if ((GST_BUFFER_SIZE (src->parent) - src->parentoffset) >= size) {
620 buf = gst_buffer_create_sub (src->parent, src->parentoffset, size);
621 src->parentoffset += size;
624 /* the parent is useless now */
625 gst_buffer_unref (src->parent);
627 /* try again (this will allocate a new parent) */
628 return gst_fakesrc_create_buffer (src);
630 gst_fakesrc_prepare_buffer (src, buf);
633 g_warning ("fakesrc: dunno how to allocate buffers !");
634 buf = gst_buffer_new();
638 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
645 gst_fakesrc_get(GstPad *pad)
650 g_return_val_if_fail (pad != NULL, NULL);
652 src = GST_FAKESRC (gst_pad_get_parent (pad));
654 g_return_val_if_fail (GST_IS_FAKESRC (src), NULL);
656 if (src->need_flush) {
657 src->need_flush = FALSE;
658 return GST_BUFFER(gst_event_new (GST_EVENT_FLUSH));
661 if (src->rt_num_buffers == 0) {
662 gst_element_set_eos (GST_ELEMENT (src));
663 return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
666 if (src->rt_num_buffers > 0)
667 src->rt_num_buffers--;
671 GST_INFO (0, "fakesrc is setting eos on pad");
672 return GST_BUFFER(gst_event_new (GST_EVENT_EOS));
675 buf = gst_fakesrc_create_buffer (src);
676 GST_BUFFER_TIMESTAMP (buf) = src->buffer_count++;
679 if (src->last_message)
680 g_free (src->last_message);
682 src->last_message = g_strdup_printf ("get ******* (%s:%s)> (%d bytes, %llu)",
683 GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
685 g_object_notify (G_OBJECT (src), "last_message");
688 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, src, "pre handoff emit\n");
689 g_signal_emit (G_OBJECT (src), gst_fakesrc_signals[SIGNAL_HANDOFF], 0,
691 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, src, "post handoff emit\n");
698 * @element: the faksesrc to loop
700 * generate an empty buffer and push it to the next element.
703 gst_fakesrc_loop(GstElement *element)
708 g_return_if_fail(element != NULL);
709 g_return_if_fail(GST_IS_FAKESRC(element));
711 src = GST_FAKESRC (element);
713 pads = gst_element_get_pad_list (element);
716 GstPad *pad = GST_PAD (pads->data);
719 if (src->rt_num_buffers == 0) {
723 if (src->rt_num_buffers > 0)
724 src->rt_num_buffers--;
728 gst_pad_push(pad, GST_BUFFER(gst_event_new (GST_EVENT_EOS)));
732 buf = gst_fakesrc_create_buffer (src);
733 GST_BUFFER_TIMESTAMP (buf) = src->buffer_count++;
736 if (src->last_message)
737 g_free (src->last_message);
739 src->last_message = g_strdup_printf ("fakesrc: loop ******* (%s:%s) > (%d bytes, %llu)",
740 GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
742 g_object_notify (G_OBJECT (src), "last_message");
745 g_signal_emit (G_OBJECT (src), gst_fakesrc_signals[SIGNAL_HANDOFF], 0,
747 gst_pad_push (pad, buf);
749 pads = g_list_next (pads);
753 static GstElementStateReturn
754 gst_fakesrc_change_state (GstElement *element)
758 g_return_val_if_fail (GST_IS_FAKESRC (element), GST_STATE_FAILURE);
760 fakesrc = GST_FAKESRC (element);
762 switch (GST_STATE_TRANSITION (element)) {
763 case GST_STATE_PAUSED_TO_READY:
764 case GST_STATE_NULL_TO_READY:
765 fakesrc->buffer_count = 0;
766 fakesrc->pattern_byte = 0x00;
767 fakesrc->need_flush = FALSE;
768 fakesrc->eos = FALSE;
769 fakesrc->rt_num_buffers = fakesrc->num_buffers;
770 if (fakesrc->parent) {
771 gst_buffer_unref (fakesrc->parent);
772 fakesrc->parent = NULL;
775 case GST_STATE_READY_TO_PAUSED:
776 case GST_STATE_PAUSED_TO_PLAYING:
777 case GST_STATE_PLAYING_TO_PAUSED:
778 case GST_STATE_READY_TO_NULL:
781 g_assert_not_reached ();
785 if (GST_ELEMENT_CLASS (parent_class)->change_state)
786 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
788 return GST_STATE_SUCCESS;
792 gst_fakesrc_factory_init (GstElementFactory *factory)
794 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (fakesrc_src_factory));