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 * @short_description: Generate (meaningless) buffers
25 * @see_also: #GstFakeSink
26 *
27 * <refsect2>
28 * <para>
29 * The fakesrc element is a multipurpose element that can generate
30 * a wide range of buffers and can operate in various scheduling modes.
31 * </para>
32 * <para>
33 * It is mostly used as a testing element, one trivial example for testing
34 * basic <application>GStreamer</application> core functionality is:
35 * </para>
36 * <title>Example launch line</title>
37 * <para>
38 * <programlisting>
39 * gst-launch -v fakesrc num-buffers=5 ! fakesink
40 * </programlisting>
41 * This pipeline will push 5 empty buffers to the fakesink element and then
42 * sends an EOS.
43 * </para>
44 * <para>
45 * Last reviewed on 2005-12-02 (0.9.7)
46 * </para>
47 * </refsect2>
48 */
50 #ifdef HAVE_CONFIG_H
51 # include "config.h"
52 #endif
54 #include <stdlib.h>
55 #include <string.h>
57 #include "gstfakesrc.h"
58 #include <gst/gstmarshal.h>
60 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_PAD_SRC,
62 GST_PAD_ALWAYS,
63 GST_STATIC_CAPS_ANY);
65 GST_DEBUG_CATEGORY_STATIC (gst_fake_src_debug);
66 #define GST_CAT_DEFAULT gst_fake_src_debug
69 /* FakeSrc signals and args */
70 enum
71 {
72 /* FILL ME */
73 SIGNAL_HANDOFF,
74 LAST_SIGNAL
75 };
77 #define DEFAULT_OUTPUT FAKE_SRC_FIRST_LAST_LOOP
78 #define DEFAULT_DATA FAKE_SRC_DATA_ALLOCATE
79 #define DEFAULT_SIZETYPE FAKE_SRC_SIZETYPE_EMPTY
80 #define DEFAULT_SIZEMIN 0
81 #define DEFAULT_SIZEMAX 4096
82 #define DEFAULT_FILLTYPE FAKE_SRC_FILLTYPE_ZERO
83 #define DEFAULT_DATARATE 0
84 #define DEFAULT_SYNC FALSE
85 #define DEFAULT_PATTERN NULL
86 #define DEFAULT_EOS FALSE
87 #define DEFAULT_SIGNAL_HANDOFFS FALSE
88 #define DEFAULT_SILENT FALSE
89 #define DEFAULT_DUMP FALSE
90 #define DEFAULT_PARENTSIZE 4096*10
91 #define DEFAULT_CAN_ACTIVATE_PULL TRUE
92 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
94 enum
95 {
96 PROP_0,
97 PROP_OUTPUT,
98 PROP_DATA,
99 PROP_SIZETYPE,
100 PROP_SIZEMIN,
101 PROP_SIZEMAX,
102 PROP_FILLTYPE,
103 PROP_DATARATE,
104 PROP_SYNC,
105 PROP_PATTERN,
106 PROP_EOS,
107 PROP_SIGNAL_HANDOFFS,
108 PROP_SILENT,
109 PROP_DUMP,
110 PROP_PARENTSIZE,
111 PROP_LAST_MESSAGE,
112 PROP_CAN_ACTIVATE_PULL,
113 PROP_CAN_ACTIVATE_PUSH,
114 PROP_IS_LIVE
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)
121 {
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;
140 }
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)
146 {
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;
159 }
161 #define GST_TYPE_FAKE_SRC_SIZETYPE (gst_fake_src_sizetype_get_type())
162 static GType
163 gst_fake_src_sizetype_get_type (void)
164 {
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;
179 }
181 #define GST_TYPE_FAKE_SRC_FILLTYPE (gst_fake_src_filltype_get_type())
182 static GType
183 gst_fake_src_filltype_get_type (void)
184 {
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;
203 }
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 gst_fake_src_base_init (gpointer g_class)
231 {
232 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
234 gst_element_class_set_details_simple (gstelement_class,
235 "Fake Source",
236 "Source",
237 "Push empty (no data) buffers around",
238 "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
239 gst_element_class_add_pad_template (gstelement_class,
240 gst_static_pad_template_get (&srctemplate));
241 }
243 static void
244 gst_fake_src_class_init (GstFakeSrcClass * klass)
245 {
246 GObjectClass *gobject_class;
247 GstElementClass *gstelement_class;
248 GstBaseSrcClass *gstbase_src_class;
250 gobject_class = G_OBJECT_CLASS (klass);
251 gstelement_class = GST_ELEMENT_CLASS (klass);
252 gstbase_src_class = GST_BASE_SRC_CLASS (klass);
254 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_fake_src_finalize);
256 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fake_src_set_property);
257 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fake_src_get_property);
259 /*
260 FIXME: this is not implemented; would make sense once basesrc and fakesrc
261 support multiple pads
262 g_object_class_install_property (gobject_class, PROP_OUTPUT,
263 g_param_spec_enum ("output", "output", "Output method (currently unused)",
264 GST_TYPE_FAKE_SRC_OUTPUT, DEFAULT_OUTPUT, G_PARAM_READWRITE));
265 */
266 g_object_class_install_property (gobject_class, PROP_DATA,
267 g_param_spec_enum ("data", "data", "Data allocation method",
268 GST_TYPE_FAKE_SRC_DATA, DEFAULT_DATA, G_PARAM_READWRITE));
269 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIZETYPE,
270 g_param_spec_enum ("sizetype", "sizetype",
271 "How to determine buffer sizes", GST_TYPE_FAKE_SRC_SIZETYPE,
272 DEFAULT_SIZETYPE, G_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class, PROP_SIZEMIN,
274 g_param_spec_int ("sizemin", "sizemin", "Minimum buffer size", 0,
275 G_MAXINT, DEFAULT_SIZEMIN, G_PARAM_READWRITE));
276 g_object_class_install_property (gobject_class, PROP_SIZEMAX,
277 g_param_spec_int ("sizemax", "sizemax", "Maximum buffer size", 0,
278 G_MAXINT, DEFAULT_SIZEMAX, G_PARAM_READWRITE));
279 g_object_class_install_property (gobject_class, PROP_PARENTSIZE,
280 g_param_spec_int ("parentsize", "parentsize",
281 "Size of parent buffer for sub-buffered allocation", 0, G_MAXINT,
282 DEFAULT_PARENTSIZE, G_PARAM_READWRITE));
283 g_object_class_install_property (gobject_class, PROP_FILLTYPE,
284 g_param_spec_enum ("filltype", "filltype",
285 "How to fill the buffer, if at all", GST_TYPE_FAKE_SRC_FILLTYPE,
286 DEFAULT_FILLTYPE, G_PARAM_READWRITE));
287 g_object_class_install_property (gobject_class, PROP_DATARATE,
288 g_param_spec_int ("datarate", "Datarate",
289 "Timestamps buffers with number of bytes per second (0 = none)", 0,
290 G_MAXINT, DEFAULT_DATARATE, G_PARAM_READWRITE));
291 g_object_class_install_property (gobject_class, PROP_SYNC,
292 g_param_spec_boolean ("sync", "Sync", "Sync to the clock to the datarate",
293 DEFAULT_SYNC, G_PARAM_READWRITE));
294 g_object_class_install_property (gobject_class, PROP_PATTERN,
295 g_param_spec_string ("pattern", "pattern", "pattern", DEFAULT_PATTERN,
296 G_PARAM_READWRITE));
297 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
298 g_param_spec_string ("last-message", "last-message",
299 "The last status message", NULL, G_PARAM_READABLE));
300 g_object_class_install_property (gobject_class, PROP_SILENT,
301 g_param_spec_boolean ("silent", "Silent",
302 "Don't produce last_message events", DEFAULT_SILENT,
303 G_PARAM_READWRITE));
304 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
305 g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
306 "Send a signal before pushing the buffer", DEFAULT_SIGNAL_HANDOFFS,
307 G_PARAM_READWRITE));
308 g_object_class_install_property (gobject_class, PROP_DUMP,
309 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
310 DEFAULT_DUMP, G_PARAM_READWRITE));
311 g_object_class_install_property (gobject_class,
312 PROP_CAN_ACTIVATE_PUSH,
313 g_param_spec_boolean ("can-activate-push", "Can activate push",
314 "Can activate in push mode", DEFAULT_CAN_ACTIVATE_PUSH,
315 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
316 g_object_class_install_property (gobject_class,
317 PROP_CAN_ACTIVATE_PULL,
318 g_param_spec_boolean ("can-activate-pull", "Can activate pull",
319 "Can activate in pull mode", DEFAULT_CAN_ACTIVATE_PULL,
320 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
321 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
322 g_param_spec_boolean ("is-live", "Is this a live source",
323 "True if the element cannot produce data in PAUSED", FALSE,
324 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
326 /**
327 * GstFakeSrc::handoff:
328 * @fakesrc: the fakesrc instance
329 * @buffer: the buffer that will be pushed
330 * @pad: the pad that will sent it
331 *
332 * This signal gets emitted before sending the buffer.
333 */
334 gst_fake_src_signals[SIGNAL_HANDOFF] =
335 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
336 G_STRUCT_OFFSET (GstFakeSrcClass, handoff), NULL, NULL,
337 gst_marshal_VOID__OBJECT_OBJECT, G_TYPE_NONE, 2, GST_TYPE_BUFFER,
338 GST_TYPE_PAD);
340 gstbase_src_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fake_src_is_seekable);
341 gstbase_src_class->start = GST_DEBUG_FUNCPTR (gst_fake_src_start);
342 gstbase_src_class->stop = GST_DEBUG_FUNCPTR (gst_fake_src_stop);
343 gstbase_src_class->event = GST_DEBUG_FUNCPTR (gst_fake_src_event_handler);
344 gstbase_src_class->get_times = GST_DEBUG_FUNCPTR (gst_fake_src_get_times);
345 gstbase_src_class->create = GST_DEBUG_FUNCPTR (gst_fake_src_create);
346 }
348 static void
349 gst_fake_src_init (GstFakeSrc * fakesrc, GstFakeSrcClass * g_class)
350 {
351 fakesrc->output = FAKE_SRC_FIRST_LAST_LOOP;
352 fakesrc->buffer_count = 0;
353 fakesrc->silent = DEFAULT_SILENT;
354 fakesrc->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
355 fakesrc->dump = DEFAULT_DUMP;
356 fakesrc->pattern_byte = 0x00;
357 fakesrc->data = FAKE_SRC_DATA_ALLOCATE;
358 fakesrc->sizetype = FAKE_SRC_SIZETYPE_EMPTY;
359 fakesrc->filltype = FAKE_SRC_FILLTYPE_NOTHING;
360 fakesrc->sizemin = DEFAULT_SIZEMIN;
361 fakesrc->sizemax = DEFAULT_SIZEMAX;
362 fakesrc->parent = NULL;
363 fakesrc->parentsize = DEFAULT_PARENTSIZE;
364 fakesrc->last_message = NULL;
365 fakesrc->datarate = DEFAULT_DATARATE;
366 fakesrc->sync = DEFAULT_SYNC;
367 }
369 static void
370 gst_fake_src_finalize (GObject * object)
371 {
372 GstFakeSrc *src;
374 src = GST_FAKE_SRC (object);
376 g_free (src->last_message);
377 if (src->parent) {
378 gst_buffer_unref (src->parent);
379 src->parent = NULL;
380 }
382 G_OBJECT_CLASS (parent_class)->finalize (object);
383 }
385 static gboolean
386 gst_fake_src_event_handler (GstBaseSrc * basesrc, GstEvent * event)
387 {
388 GstFakeSrc *src;
390 src = GST_FAKE_SRC (basesrc);
392 if (!src->silent) {
393 const GstStructure *s;
394 gchar *sstr;
396 GST_OBJECT_LOCK (src);
397 g_free (src->last_message);
399 if ((s = gst_event_get_structure (event)))
400 sstr = gst_structure_to_string (s);
401 else
402 sstr = g_strdup ("");
404 src->last_message =
405 g_strdup_printf ("event ******* E (type: %d, %s) %p",
406 GST_EVENT_TYPE (event), sstr, event);
407 g_free (sstr);
408 GST_OBJECT_UNLOCK (src);
410 g_object_notify (G_OBJECT (src), "last_message");
411 }
414 return TRUE;
415 }
417 static void
418 gst_fake_src_alloc_parent (GstFakeSrc * src)
419 {
420 GstBuffer *buf;
422 buf = gst_buffer_new ();
423 GST_BUFFER_DATA (buf) = g_malloc (src->parentsize);
424 GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
425 GST_BUFFER_SIZE (buf) = src->parentsize;
427 src->parent = buf;
428 src->parentoffset = 0;
429 }
431 static void
432 gst_fake_src_set_property (GObject * object, guint prop_id,
433 const GValue * value, GParamSpec * pspec)
434 {
435 GstFakeSrc *src;
436 GstBaseSrc *basesrc;
438 src = GST_FAKE_SRC (object);
439 basesrc = GST_BASE_SRC (object);
441 switch (prop_id) {
442 case PROP_OUTPUT:
443 g_warning ("not yet implemented");
444 break;
445 case PROP_DATA:
446 src->data = g_value_get_enum (value);
448 if (src->data == FAKE_SRC_DATA_SUBBUFFER) {
449 if (!src->parent)
450 gst_fake_src_alloc_parent (src);
451 } else {
452 if (src->parent) {
453 gst_buffer_unref (src->parent);
454 src->parent = NULL;
455 }
456 }
457 break;
458 case PROP_SIZETYPE:
459 src->sizetype = g_value_get_enum (value);
460 break;
461 case PROP_SIZEMIN:
462 src->sizemin = g_value_get_int (value);
463 break;
464 case PROP_SIZEMAX:
465 src->sizemax = g_value_get_int (value);
466 break;
467 case PROP_PARENTSIZE:
468 src->parentsize = g_value_get_int (value);
469 break;
470 case PROP_FILLTYPE:
471 src->filltype = g_value_get_enum (value);
472 break;
473 case PROP_DATARATE:
474 src->datarate = g_value_get_int (value);
475 break;
476 case PROP_SYNC:
477 src->sync = g_value_get_boolean (value);
478 break;
479 case PROP_PATTERN:
480 break;
481 case PROP_SILENT:
482 src->silent = g_value_get_boolean (value);
483 break;
484 case PROP_SIGNAL_HANDOFFS:
485 src->signal_handoffs = g_value_get_boolean (value);
486 break;
487 case PROP_DUMP:
488 src->dump = g_value_get_boolean (value);
489 break;
490 case PROP_CAN_ACTIVATE_PUSH:
491 g_return_if_fail (!GST_OBJECT_FLAG_IS_SET (object, GST_BASE_SRC_STARTED));
492 GST_BASE_SRC (src)->can_activate_push = g_value_get_boolean (value);
493 break;
494 case PROP_CAN_ACTIVATE_PULL:
495 g_return_if_fail (!GST_OBJECT_FLAG_IS_SET (object, GST_BASE_SRC_STARTED));
496 src->can_activate_pull = g_value_get_boolean (value);
497 break;
498 case PROP_IS_LIVE:
499 gst_base_src_set_live (basesrc, g_value_get_boolean (value));
500 break;
501 default:
502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 break;
504 }
505 }
507 static void
508 gst_fake_src_get_property (GObject * object, guint prop_id, GValue * value,
509 GParamSpec * pspec)
510 {
511 GstFakeSrc *src;
512 GstBaseSrc *basesrc;
514 g_return_if_fail (GST_IS_FAKE_SRC (object));
516 src = GST_FAKE_SRC (object);
517 basesrc = GST_BASE_SRC (object);
519 switch (prop_id) {
520 case PROP_OUTPUT:
521 g_value_set_enum (value, src->output);
522 break;
523 case PROP_DATA:
524 g_value_set_enum (value, src->data);
525 break;
526 case PROP_SIZETYPE:
527 g_value_set_enum (value, src->sizetype);
528 break;
529 case PROP_SIZEMIN:
530 g_value_set_int (value, src->sizemin);
531 break;
532 case PROP_SIZEMAX:
533 g_value_set_int (value, src->sizemax);
534 break;
535 case PROP_PARENTSIZE:
536 g_value_set_int (value, src->parentsize);
537 break;
538 case PROP_FILLTYPE:
539 g_value_set_enum (value, src->filltype);
540 break;
541 case PROP_DATARATE:
542 g_value_set_int (value, src->datarate);
543 break;
544 case PROP_SYNC:
545 g_value_set_boolean (value, src->sync);
546 break;
547 case PROP_PATTERN:
548 g_value_set_string (value, src->pattern);
549 break;
550 case PROP_SILENT:
551 g_value_set_boolean (value, src->silent);
552 break;
553 case PROP_SIGNAL_HANDOFFS:
554 g_value_set_boolean (value, src->signal_handoffs);
555 break;
556 case PROP_DUMP:
557 g_value_set_boolean (value, src->dump);
558 break;
559 case PROP_LAST_MESSAGE:
560 GST_OBJECT_LOCK (src);
561 g_value_set_string (value, src->last_message);
562 GST_OBJECT_UNLOCK (src);
563 break;
564 case PROP_CAN_ACTIVATE_PUSH:
565 g_value_set_boolean (value, GST_BASE_SRC (src)->can_activate_push);
566 break;
567 case PROP_CAN_ACTIVATE_PULL:
568 g_value_set_boolean (value, src->can_activate_pull);
569 break;
570 case PROP_IS_LIVE:
571 g_value_set_boolean (value, gst_base_src_is_live (basesrc));
572 break;
573 default:
574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
575 break;
576 }
577 }
579 static void
580 gst_fake_src_prepare_buffer (GstFakeSrc * src, GstBuffer * buf)
581 {
582 if (GST_BUFFER_SIZE (buf) == 0)
583 return;
585 switch (src->filltype) {
586 case FAKE_SRC_FILLTYPE_ZERO:
587 memset (GST_BUFFER_DATA (buf), 0, GST_BUFFER_SIZE (buf));
588 break;
589 case FAKE_SRC_FILLTYPE_RANDOM:
590 {
591 gint i;
592 guint8 *ptr = GST_BUFFER_DATA (buf);
594 for (i = GST_BUFFER_SIZE (buf); i; i--) {
595 *ptr++ = g_random_int_range (0, 256);
596 }
597 break;
598 }
599 case FAKE_SRC_FILLTYPE_PATTERN:
600 src->pattern_byte = 0x00;
601 case FAKE_SRC_FILLTYPE_PATTERN_CONT:
602 {
603 gint i;
604 guint8 *ptr = GST_BUFFER_DATA (buf);
606 for (i = GST_BUFFER_SIZE (buf); i; i--) {
607 *ptr++ = src->pattern_byte++;
608 }
609 break;
610 }
611 case FAKE_SRC_FILLTYPE_NOTHING:
612 default:
613 break;
614 }
615 }
617 static GstBuffer *
618 gst_fake_src_alloc_buffer (GstFakeSrc * src, guint size)
619 {
620 GstBuffer *buf;
622 buf = gst_buffer_new ();
623 GST_BUFFER_SIZE (buf) = size;
625 if (size != 0) {
626 switch (src->filltype) {
627 case FAKE_SRC_FILLTYPE_NOTHING:
628 GST_BUFFER_DATA (buf) = g_malloc (size);
629 GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
630 break;
631 case FAKE_SRC_FILLTYPE_ZERO:
632 GST_BUFFER_DATA (buf) = g_malloc0 (size);
633 GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
634 break;
635 case FAKE_SRC_FILLTYPE_RANDOM:
636 case FAKE_SRC_FILLTYPE_PATTERN:
637 case FAKE_SRC_FILLTYPE_PATTERN_CONT:
638 default:
639 GST_BUFFER_DATA (buf) = g_malloc (size);
640 GST_BUFFER_MALLOCDATA (buf) = GST_BUFFER_DATA (buf);
641 gst_fake_src_prepare_buffer (src, buf);
642 break;
643 }
644 }
646 return buf;
647 }
649 static guint
650 gst_fake_src_get_size (GstFakeSrc * src)
651 {
652 guint size;
654 switch (src->sizetype) {
655 case FAKE_SRC_SIZETYPE_FIXED:
656 size = src->sizemax;
657 break;
658 case FAKE_SRC_SIZETYPE_RANDOM:
659 size = g_random_int_range (src->sizemin, src->sizemax);
660 break;
661 case FAKE_SRC_SIZETYPE_EMPTY:
662 default:
663 size = 0;
664 break;
665 }
667 return size;
668 }
670 static GstBuffer *
671 gst_fake_src_create_buffer (GstFakeSrc * src)
672 {
673 GstBuffer *buf;
674 guint size;
675 gboolean dump = src->dump;
677 size = gst_fake_src_get_size (src);
678 if (size == 0)
679 return gst_buffer_new ();
681 switch (src->data) {
682 case FAKE_SRC_DATA_ALLOCATE:
683 buf = gst_fake_src_alloc_buffer (src, size);
684 break;
685 case FAKE_SRC_DATA_SUBBUFFER:
686 /* see if we have a parent to subbuffer */
687 if (!src->parent) {
688 gst_fake_src_alloc_parent (src);
689 g_assert (src->parent);
690 }
691 /* see if it's large enough */
692 if ((GST_BUFFER_SIZE (src->parent) - src->parentoffset) >= size) {
693 buf = gst_buffer_create_sub (src->parent, src->parentoffset, size);
694 src->parentoffset += size;
695 } else {
696 /* the parent is useless now */
697 gst_buffer_unref (src->parent);
698 src->parent = NULL;
699 /* try again (this will allocate a new parent) */
700 return gst_fake_src_create_buffer (src);
701 }
702 gst_fake_src_prepare_buffer (src, buf);
703 break;
704 default:
705 g_warning ("fakesrc: dunno how to allocate buffers !");
706 buf = gst_buffer_new ();
707 break;
708 }
709 if (dump) {
710 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
711 }
713 return buf;
714 }
716 static void
717 gst_fake_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
718 GstClockTime * start, GstClockTime * end)
719 {
720 GstFakeSrc *src;
722 src = GST_FAKE_SRC (basesrc);
724 /* sync on the timestamp of the buffer if requested. */
725 if (src->sync) {
726 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
728 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
729 /* get duration to calculate end time */
730 GstClockTime duration = GST_BUFFER_DURATION (buffer);
732 if (GST_CLOCK_TIME_IS_VALID (duration)) {
733 *end = timestamp + duration;
734 }
735 *start = timestamp;
736 }
737 } else {
738 *start = -1;
739 *end = -1;
740 }
741 }
743 static GstFlowReturn
744 gst_fake_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
745 GstBuffer ** ret)
746 {
747 GstFakeSrc *src;
748 GstBuffer *buf;
749 GstClockTime time;
751 src = GST_FAKE_SRC (basesrc);
753 buf = gst_fake_src_create_buffer (src);
754 GST_BUFFER_OFFSET (buf) = src->buffer_count++;
756 if (src->datarate > 0) {
757 time = (src->bytes_sent * GST_SECOND) / src->datarate;
759 GST_BUFFER_DURATION (buf) =
760 GST_BUFFER_SIZE (buf) * GST_SECOND / src->datarate;
761 } else if (gst_base_src_is_live (basesrc)) {
762 GstClock *clock;
764 clock = gst_element_get_clock (GST_ELEMENT (src));
766 if (clock) {
767 time = gst_clock_get_time (clock);
768 time -= gst_element_get_base_time (GST_ELEMENT (src));
769 gst_object_unref (clock);
770 } else {
771 /* not an error not to have a clock */
772 time = GST_CLOCK_TIME_NONE;
773 }
774 } else {
775 time = GST_CLOCK_TIME_NONE;
776 }
778 GST_BUFFER_TIMESTAMP (buf) = time;
780 if (!src->silent) {
781 gchar ts_str[64], dur_str[64];
783 GST_OBJECT_LOCK (src);
784 g_free (src->last_message);
786 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
787 g_snprintf (ts_str, sizeof (ts_str), "%" GST_TIME_FORMAT,
788 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
789 } else {
790 g_strlcpy (ts_str, "none", sizeof (ts_str));
791 }
793 if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
794 g_snprintf (dur_str, sizeof (dur_str), "%" GST_TIME_FORMAT,
795 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
796 } else {
797 g_strlcpy (dur_str, "none", sizeof (dur_str));
798 }
800 src->last_message =
801 g_strdup_printf ("get ******* > (%5d bytes, timestamp: %s"
802 ", duration: %s, offset: %" G_GINT64_FORMAT ", offset_end: %"
803 G_GINT64_FORMAT ", flags: %d) %p", GST_BUFFER_SIZE (buf), ts_str,
804 dur_str, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
805 GST_MINI_OBJECT (buf)->flags, buf);
806 GST_OBJECT_UNLOCK (src);
808 g_object_notify (G_OBJECT (src), "last_message");
809 }
811 if (src->signal_handoffs) {
812 GST_LOG_OBJECT (src, "pre handoff emit");
813 g_signal_emit (G_OBJECT (src), gst_fake_src_signals[SIGNAL_HANDOFF], 0,
814 buf, basesrc->srcpad);
815 GST_LOG_OBJECT (src, "post handoff emit");
816 }
818 src->bytes_sent += GST_BUFFER_SIZE (buf);
820 *ret = buf;
821 return GST_FLOW_OK;
822 }
824 static gboolean
825 gst_fake_src_start (GstBaseSrc * basesrc)
826 {
827 GstFakeSrc *src;
829 src = GST_FAKE_SRC (basesrc);
831 src->buffer_count = 0;
832 src->pattern_byte = 0x00;
833 src->bytes_sent = 0;
835 return TRUE;
836 }
838 static gboolean
839 gst_fake_src_stop (GstBaseSrc * basesrc)
840 {
841 GstFakeSrc *src;
843 src = GST_FAKE_SRC (basesrc);
845 GST_OBJECT_LOCK (src);
846 if (src->parent) {
847 gst_buffer_unref (src->parent);
848 src->parent = NULL;
849 }
850 g_free (src->last_message);
851 src->last_message = NULL;
852 GST_OBJECT_UNLOCK (src);
854 return TRUE;
855 }
857 static gboolean
858 gst_fake_src_is_seekable (GstBaseSrc * basesrc)
859 {
860 GstFakeSrc *src = GST_FAKE_SRC (basesrc);
862 return src->can_activate_pull;
863 }