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 * SECTION:gstfakesrc
24 * @short_description: Push empty (no data) buffers around
25 * @see_also: #GstFakeSink
26 *
27 * Dummy source that generates empty buffers.
28 */
30 #include <stdlib.h>
31 #include <string.h>
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include "gstfakesrc.h"
38 #include <gst/gstmarshal.h>
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41 GST_PAD_SRC,
42 GST_PAD_ALWAYS,
43 GST_STATIC_CAPS_ANY);
45 GST_DEBUG_CATEGORY_STATIC (gst_fake_src_debug);
46 #define GST_CAT_DEFAULT gst_fake_src_debug
48 GstElementDetails gst_fake_src_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>");
55 /* FakeSrc signals and args */
56 enum
57 {
58 /* FILL ME */
59 SIGNAL_HANDOFF,
60 LAST_SIGNAL
61 };
63 #define DEFAULT_OUTPUT FAKE_SRC_FIRST_LAST_LOOP
64 #define DEFAULT_DATA FAKE_SRC_DATA_ALLOCATE
65 #define DEFAULT_SIZETYPE FAKE_SRC_SIZETYPE_NULL
66 #define DEFAULT_SIZEMIN 0
67 #define DEFAULT_SIZEMAX 4096
68 #define DEFAULT_FILLTYPE FAKE_SRC_FILLTYPE_NULL
69 #define DEFAULT_DATARATE 0
70 #define DEFAULT_SYNC FALSE
71 #define DEFAULT_PATTERN NULL
72 #define DEFAULT_EOS FALSE
73 #define DEFAULT_SIGNAL_HANDOFFS FALSE
74 #define DEFAULT_SILENT FALSE
75 #define DEFAULT_DUMP FALSE
76 #define DEFAULT_PARENTSIZE 4096*10
78 enum
79 {
80 PROP_0,
81 PROP_OUTPUT,
82 PROP_DATA,
83 PROP_SIZETYPE,
84 PROP_SIZEMIN,
85 PROP_SIZEMAX,
86 PROP_FILLTYPE,
87 PROP_DATARATE,
88 PROP_SYNC,
89 PROP_PATTERN,
90 PROP_EOS,
91 PROP_SIGNAL_HANDOFFS,
92 PROP_SILENT,
93 PROP_DUMP,
94 PROP_PARENTSIZE,
95 PROP_LAST_MESSAGE,
96 PROP_HAS_LOOP,
97 PROP_HAS_GETRANGE,
98 PROP_IS_LIVE
99 };
101 /* not implemented
102 #define GST_TYPE_FAKE_SRC_OUTPUT (gst_fake_src_output_get_type())
103 static GType
104 gst_fake_src_output_get_type (void)
105 {
106 static GType fakesrc_output_type = 0;
107 static GEnumValue fakesrc_output[] = {
108 {FAKE_SRC_FIRST_LAST_LOOP, "1", "First-Last loop"},
109 {FAKE_SRC_LAST_FIRST_LOOP, "2", "Last-First loop"},
110 {FAKE_SRC_PING_PONG, "3", "Ping-Pong"},
111 {FAKE_SRC_ORDERED_RANDOM, "4", "Ordered Random"},
112 {FAKE_SRC_RANDOM, "5", "Random"},
113 {FAKE_SRC_PATTERN_LOOP, "6", "Patttern loop"},
114 {FAKE_SRC_PING_PONG_PATTERN, "7", "Ping-Pong Pattern"},
115 {FAKE_SRC_GET_ALWAYS_SUCEEDS, "8", "'_get' Always succeeds"},
116 {0, NULL, NULL},
117 };
119 if (!fakesrc_output_type) {
120 fakesrc_output_type =
121 g_enum_register_static ("GstFakeSrcOutput", fakesrc_output);
122 }
123 return fakesrc_output_type;
124 }
125 */
127 #define GST_TYPE_FAKE_SRC_DATA (gst_fake_src_data_get_type())
128 static GType
129 gst_fake_src_data_get_type (void)
130 {
131 static GType fakesrc_data_type = 0;
132 static GEnumValue fakesrc_data[] = {
133 {FAKE_SRC_DATA_ALLOCATE, "1", "Allocate data"},
134 {FAKE_SRC_DATA_SUBBUFFER, "2", "Subbuffer data"},
135 {0, NULL, NULL},
136 };
138 if (!fakesrc_data_type) {
139 fakesrc_data_type = g_enum_register_static ("GstFakeSrcData", fakesrc_data);
140 }
141 return fakesrc_data_type;
142 }
144 #define GST_TYPE_FAKE_SRC_SIZETYPE (gst_fake_src_sizetype_get_type())
145 static GType
146 gst_fake_src_sizetype_get_type (void)
147 {
148 static GType fakesrc_sizetype_type = 0;
149 static GEnumValue fakesrc_sizetype[] = {
150 {FAKE_SRC_SIZETYPE_NULL, "1", "Send empty buffers"},
151 {FAKE_SRC_SIZETYPE_FIXED, "2", "Fixed size buffers (sizemax sized)"},
152 {FAKE_SRC_SIZETYPE_RANDOM, "3",
153 "Random sized buffers (sizemin <= size <= sizemax)"},
154 {0, NULL, NULL},
155 };
157 if (!fakesrc_sizetype_type) {
158 fakesrc_sizetype_type =
159 g_enum_register_static ("GstFakeSrcSizeType", fakesrc_sizetype);
160 }
161 return fakesrc_sizetype_type;
162 }
164 #define GST_TYPE_FAKE_SRC_FILLTYPE (gst_fake_src_filltype_get_type())
165 static GType
166 gst_fake_src_filltype_get_type (void)
167 {
168 static GType fakesrc_filltype_type = 0;
169 static GEnumValue fakesrc_filltype[] = {
170 {FAKE_SRC_FILLTYPE_NOTHING, "1", "Leave data as malloced"},
171 {FAKE_SRC_FILLTYPE_NULL, "2", "Fill buffers with zeros"},
172 {FAKE_SRC_FILLTYPE_RANDOM, "3", "Fill buffers with random crap"},
173 {FAKE_SRC_FILLTYPE_PATTERN, "4", "Fill buffers with pattern 0x00 -> 0xff"},
174 {FAKE_SRC_FILLTYPE_PATTERN_CONT, "5",
175 "Fill buffers with pattern 0x00 -> 0xff that spans buffers"},
176 {0, NULL, NULL},
177 };
179 if (!fakesrc_filltype_type) {
180 fakesrc_filltype_type =
181 g_enum_register_static ("GstFakeSrcFillType", fakesrc_filltype);
182 }
183 return fakesrc_filltype_type;
184 }
186 #define _do_init(bla) \
187 GST_DEBUG_CATEGORY_INIT (gst_fake_src_debug, "fakesrc", 0, "fakesrc element");
189 GST_BOILERPLATE_FULL (GstFakeSrc, gst_fake_src, GstBaseSrc, GST_TYPE_BASE_SRC,
190 _do_init);
192 static void gst_fake_src_set_property (GObject * object, guint prop_id,
193 const GValue * value, GParamSpec * pspec);
194 static void gst_fake_src_get_property (GObject * object, guint prop_id,
195 GValue * value, GParamSpec * pspec);
197 static gboolean gst_fake_src_start (GstBaseSrc * basesrc);
198 static gboolean gst_fake_src_stop (GstBaseSrc * basesrc);
200 static gboolean gst_fake_src_event_handler (GstBaseSrc * src, GstEvent * event);
201 static GstFlowReturn gst_fake_src_create (GstBaseSrc * src, guint64 offset,
202 guint length, GstBuffer ** buf);
204 static guint gst_fake_src_signals[LAST_SIGNAL] = { 0 };
206 static void
207 gst_fake_src_base_init (gpointer g_class)
208 {
209 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
211 gst_element_class_add_pad_template (gstelement_class,
212 gst_static_pad_template_get (&srctemplate));
214 gst_element_class_set_details (gstelement_class, &gst_fake_src_details);
215 }
217 static void
218 gst_fake_src_class_init (GstFakeSrcClass * klass)
219 {
220 GObjectClass *gobject_class;
221 GstElementClass *gstelement_class;
222 GstBaseSrcClass *gstbase_src_class;
224 gobject_class = (GObjectClass *) klass;
225 gstelement_class = (GstElementClass *) klass;
226 gstbase_src_class = (GstBaseSrcClass *) klass;
228 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fake_src_set_property);
229 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fake_src_get_property);
231 /*
232 FIXME: this is not implemented; would make sense once basesrc and fakesrc
233 support multiple pads
234 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_OUTPUT,
235 g_param_spec_enum ("output", "output", "Output method (currently unused)",
236 GST_TYPE_FAKE_SRC_OUTPUT, DEFAULT_OUTPUT, G_PARAM_READWRITE));
237 */
238 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATA,
239 g_param_spec_enum ("data", "data", "Data allocation method",
240 GST_TYPE_FAKE_SRC_DATA, DEFAULT_DATA, G_PARAM_READWRITE));
241 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIZETYPE,
242 g_param_spec_enum ("sizetype", "sizetype",
243 "How to determine buffer sizes", GST_TYPE_FAKE_SRC_SIZETYPE,
244 DEFAULT_SIZETYPE, G_PARAM_READWRITE));
245 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIZEMIN,
246 g_param_spec_int ("sizemin", "sizemin", "Minimum buffer size", 0,
247 G_MAXINT, DEFAULT_SIZEMIN, G_PARAM_READWRITE));
248 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIZEMAX,
249 g_param_spec_int ("sizemax", "sizemax", "Maximum buffer size", 0,
250 G_MAXINT, DEFAULT_SIZEMAX, G_PARAM_READWRITE));
251 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PARENTSIZE,
252 g_param_spec_int ("parentsize", "parentsize",
253 "Size of parent buffer for sub-buffered allocation", 0, G_MAXINT,
254 DEFAULT_PARENTSIZE, G_PARAM_READWRITE));
255 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILLTYPE,
256 g_param_spec_enum ("filltype", "filltype",
257 "How to fill the buffer, if at all", GST_TYPE_FAKE_SRC_FILLTYPE,
258 DEFAULT_FILLTYPE, G_PARAM_READWRITE));
259 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATARATE,
260 g_param_spec_int ("datarate", "Datarate",
261 "Timestamps buffers with number of bytes per second (0 = none)", 0,
262 G_MAXINT, DEFAULT_DATARATE, G_PARAM_READWRITE));
263 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
264 g_param_spec_boolean ("sync", "Sync", "Sync to the clock to the datarate",
265 DEFAULT_SYNC, G_PARAM_READWRITE));
266 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PATTERN,
267 g_param_spec_string ("pattern", "pattern", "pattern", DEFAULT_PATTERN,
268 G_PARAM_READWRITE));
269 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE,
270 g_param_spec_string ("last-message", "last-message",
271 "The last status message", NULL, G_PARAM_READABLE));
272 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SILENT,
273 g_param_spec_boolean ("silent", "Silent",
274 "Don't produce last_message events", DEFAULT_SILENT,
275 G_PARAM_READWRITE));
276 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SIGNAL_HANDOFFS,
277 g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
278 "Send a signal before pushing the buffer", DEFAULT_SIGNAL_HANDOFFS,
279 G_PARAM_READWRITE));
280 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DUMP,
281 g_param_spec_boolean ("dump", "Dump", "Dump produced bytes to stdout",
282 DEFAULT_DUMP, G_PARAM_READWRITE));
283 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
284 g_param_spec_boolean ("has-loop", "Has loop function",
285 "True if the element exposes a loop function", TRUE,
286 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
287 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
288 g_param_spec_boolean ("has-getrange", "Has getrange function",
289 "True if the element exposes a getrange function", TRUE,
290 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
291 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_IS_LIVE,
292 g_param_spec_boolean ("is-live", "Is this a live source",
293 "True if the element cannot produce data in PAUSED", FALSE,
294 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
296 gst_fake_src_signals[SIGNAL_HANDOFF] =
297 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
298 G_STRUCT_OFFSET (GstFakeSrcClass, handoff), NULL, NULL,
299 gst_marshal_VOID__OBJECT_OBJECT, G_TYPE_NONE, 2, GST_TYPE_BUFFER,
300 GST_TYPE_PAD);
302 /*gstbase_src_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fake_src_is_seekable); */
303 gstbase_src_class->start = GST_DEBUG_FUNCPTR (gst_fake_src_start);
304 gstbase_src_class->stop = GST_DEBUG_FUNCPTR (gst_fake_src_stop);
305 gstbase_src_class->event = GST_DEBUG_FUNCPTR (gst_fake_src_event_handler);
306 gstbase_src_class->create = GST_DEBUG_FUNCPTR (gst_fake_src_create);
307 }
309 static void
310 gst_fake_src_init (GstFakeSrc * fakesrc)
311 {
312 fakesrc->output = FAKE_SRC_FIRST_LAST_LOOP;
313 fakesrc->segment_start = -1;
314 fakesrc->segment_end = -1;
315 fakesrc->buffer_count = 0;
316 fakesrc->silent = DEFAULT_SILENT;
317 fakesrc->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
318 fakesrc->dump = DEFAULT_DUMP;
319 fakesrc->pattern_byte = 0x00;
320 fakesrc->data = FAKE_SRC_DATA_ALLOCATE;
321 fakesrc->sizetype = FAKE_SRC_SIZETYPE_NULL;
322 fakesrc->filltype = FAKE_SRC_FILLTYPE_NOTHING;
323 fakesrc->sizemin = DEFAULT_SIZEMIN;
324 fakesrc->sizemax = DEFAULT_SIZEMAX;
325 fakesrc->parent = NULL;
326 fakesrc->parentsize = DEFAULT_PARENTSIZE;
327 fakesrc->last_message = NULL;
328 fakesrc->datarate = DEFAULT_DATARATE;
329 fakesrc->sync = DEFAULT_SYNC;
330 }
332 static gboolean
333 gst_fake_src_event_handler (GstBaseSrc * basesrc, GstEvent * event)
334 {
335 GstFakeSrc *src;
337 src = GST_FAKE_SRC (basesrc);
339 if (!src->silent) {
340 g_free (src->last_message);
342 src->last_message =
343 g_strdup_printf ("event ******* E (type: %d) %p",
344 GST_EVENT_TYPE (event), event);
346 g_object_notify (G_OBJECT (src), "last_message");
347 }
350 return TRUE;
351 }
353 static void
354 gst_fake_src_alloc_parent (GstFakeSrc * src)
355 {
356 GstBuffer *buf;
358 buf = gst_buffer_new ();
359 GST_BUFFER_DATA (buf) = g_malloc (src->parentsize);
360 GST_BUFFER_SIZE (buf) = src->parentsize;
362 src->parent = buf;
363 src->parentoffset = 0;
364 }
366 static void
367 gst_fake_src_set_property (GObject * object, guint prop_id,
368 const GValue * value, GParamSpec * pspec)
369 {
370 GstFakeSrc *src;
371 GstBaseSrc *basesrc;
373 src = GST_FAKE_SRC (object);
374 basesrc = GST_BASE_SRC (object);
376 switch (prop_id) {
377 case PROP_OUTPUT:
378 g_warning ("not yet implemented");
379 break;
380 case PROP_DATA:
381 src->data = g_value_get_enum (value);
383 if (src->data == FAKE_SRC_DATA_SUBBUFFER) {
384 if (!src->parent)
385 gst_fake_src_alloc_parent (src);
386 } else {
387 if (src->parent) {
388 gst_buffer_unref (src->parent);
389 src->parent = NULL;
390 }
391 }
392 break;
393 case PROP_SIZETYPE:
394 src->sizetype = g_value_get_enum (value);
395 break;
396 case PROP_SIZEMIN:
397 src->sizemin = g_value_get_int (value);
398 break;
399 case PROP_SIZEMAX:
400 src->sizemax = g_value_get_int (value);
401 break;
402 case PROP_PARENTSIZE:
403 src->parentsize = g_value_get_int (value);
404 break;
405 case PROP_FILLTYPE:
406 src->filltype = g_value_get_enum (value);
407 break;
408 case PROP_DATARATE:
409 src->datarate = g_value_get_int (value);
410 break;
411 case PROP_SYNC:
412 src->sync = g_value_get_boolean (value);
413 break;
414 case PROP_PATTERN:
415 break;
416 case PROP_SILENT:
417 src->silent = g_value_get_boolean (value);
418 break;
419 case PROP_SIGNAL_HANDOFFS:
420 src->signal_handoffs = g_value_get_boolean (value);
421 break;
422 case PROP_DUMP:
423 src->dump = g_value_get_boolean (value);
424 break;
425 case PROP_HAS_LOOP:
426 g_return_if_fail (!GST_FLAG_IS_SET (object, GST_BASE_SRC_STARTED));
427 src->has_loop = g_value_get_boolean (value);
428 break;
429 case PROP_HAS_GETRANGE:
430 g_return_if_fail (!GST_FLAG_IS_SET (object, GST_BASE_SRC_STARTED));
431 src->has_getrange = g_value_get_boolean (value);
432 break;
433 case PROP_IS_LIVE:
434 gst_base_src_set_live (basesrc, g_value_get_boolean (value));
435 break;
436 default:
437 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
438 break;
439 }
440 }
442 static void
443 gst_fake_src_get_property (GObject * object, guint prop_id, GValue * value,
444 GParamSpec * pspec)
445 {
446 GstFakeSrc *src;
447 GstBaseSrc *basesrc;
449 g_return_if_fail (GST_IS_FAKE_SRC (object));
451 src = GST_FAKE_SRC (object);
452 basesrc = GST_BASE_SRC (object);
454 switch (prop_id) {
455 case PROP_OUTPUT:
456 g_value_set_enum (value, src->output);
457 break;
458 case PROP_DATA:
459 g_value_set_enum (value, src->data);
460 break;
461 case PROP_SIZETYPE:
462 g_value_set_enum (value, src->sizetype);
463 break;
464 case PROP_SIZEMIN:
465 g_value_set_int (value, src->sizemin);
466 break;
467 case PROP_SIZEMAX:
468 g_value_set_int (value, src->sizemax);
469 break;
470 case PROP_PARENTSIZE:
471 g_value_set_int (value, src->parentsize);
472 break;
473 case PROP_FILLTYPE:
474 g_value_set_enum (value, src->filltype);
475 break;
476 case PROP_DATARATE:
477 g_value_set_int (value, src->datarate);
478 break;
479 case PROP_SYNC:
480 g_value_set_boolean (value, src->sync);
481 break;
482 case PROP_PATTERN:
483 g_value_set_string (value, src->pattern);
484 break;
485 case PROP_SILENT:
486 g_value_set_boolean (value, src->silent);
487 break;
488 case PROP_SIGNAL_HANDOFFS:
489 g_value_set_boolean (value, src->signal_handoffs);
490 break;
491 case PROP_DUMP:
492 g_value_set_boolean (value, src->dump);
493 break;
494 case PROP_LAST_MESSAGE:
495 g_value_set_string (value, src->last_message);
496 break;
497 case PROP_HAS_LOOP:
498 g_value_set_boolean (value, src->has_loop);
499 break;
500 case PROP_HAS_GETRANGE:
501 g_value_set_boolean (value, src->has_getrange);
502 break;
503 case PROP_IS_LIVE:
504 g_value_set_boolean (value, gst_base_src_is_live (basesrc));
505 break;
506 default:
507 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
508 break;
509 }
510 }
512 static void
513 gst_fake_src_prepare_buffer (GstFakeSrc * src, GstBuffer * buf)
514 {
515 if (GST_BUFFER_SIZE (buf) == 0)
516 return;
518 switch (src->filltype) {
519 case FAKE_SRC_FILLTYPE_NULL:
520 memset (GST_BUFFER_DATA (buf), 0, GST_BUFFER_SIZE (buf));
521 break;
522 case FAKE_SRC_FILLTYPE_RANDOM:
523 {
524 gint i;
525 guint8 *ptr = GST_BUFFER_DATA (buf);
527 for (i = GST_BUFFER_SIZE (buf); i; i--) {
528 *ptr++ = (gint8) ((255.0) * rand () / (RAND_MAX));
529 }
530 break;
531 }
532 case FAKE_SRC_FILLTYPE_PATTERN:
533 src->pattern_byte = 0x00;
534 case FAKE_SRC_FILLTYPE_PATTERN_CONT:
535 {
536 gint i;
537 guint8 *ptr = GST_BUFFER_DATA (buf);
539 for (i = GST_BUFFER_SIZE (buf); i; i--) {
540 *ptr++ = src->pattern_byte++;
541 }
542 break;
543 }
544 case FAKE_SRC_FILLTYPE_NOTHING:
545 default:
546 break;
547 }
548 }
550 static GstBuffer *
551 gst_fake_src_alloc_buffer (GstFakeSrc * src, guint size)
552 {
553 GstBuffer *buf;
555 buf = gst_buffer_new ();
556 GST_BUFFER_SIZE (buf) = size;
558 if (size != 0) {
559 switch (src->filltype) {
560 case FAKE_SRC_FILLTYPE_NOTHING:
561 GST_BUFFER_DATA (buf) = g_malloc (size);
562 break;
563 case FAKE_SRC_FILLTYPE_NULL:
564 GST_BUFFER_DATA (buf) = g_malloc0 (size);
565 break;
566 case FAKE_SRC_FILLTYPE_RANDOM:
567 case FAKE_SRC_FILLTYPE_PATTERN:
568 case FAKE_SRC_FILLTYPE_PATTERN_CONT:
569 default:
570 GST_BUFFER_DATA (buf) = g_malloc (size);
571 gst_fake_src_prepare_buffer (src, buf);
572 break;
573 }
574 }
576 return buf;
577 }
579 static guint
580 gst_fake_src_get_size (GstFakeSrc * src)
581 {
582 guint size;
584 switch (src->sizetype) {
585 case FAKE_SRC_SIZETYPE_FIXED:
586 size = src->sizemax;
587 break;
588 case FAKE_SRC_SIZETYPE_RANDOM:
589 size =
590 src->sizemin +
591 (guint8) (((gfloat) src->sizemax) * rand () / (RAND_MAX +
592 (gfloat) src->sizemin));
593 break;
594 case FAKE_SRC_SIZETYPE_NULL:
595 default:
596 size = 0;
597 break;
598 }
600 return size;
601 }
603 static GstBuffer *
604 gst_fake_src_create_buffer (GstFakeSrc * src)
605 {
606 GstBuffer *buf;
607 guint size;
608 gboolean dump = src->dump;
610 size = gst_fake_src_get_size (src);
611 if (size == 0)
612 return gst_buffer_new ();
614 switch (src->data) {
615 case FAKE_SRC_DATA_ALLOCATE:
616 buf = gst_fake_src_alloc_buffer (src, size);
617 break;
618 case FAKE_SRC_DATA_SUBBUFFER:
619 /* see if we have a parent to subbuffer */
620 if (!src->parent) {
621 gst_fake_src_alloc_parent (src);
622 g_assert (src->parent);
623 }
624 /* see if it's large enough */
625 if ((GST_BUFFER_SIZE (src->parent) - src->parentoffset) >= size) {
626 buf = gst_buffer_create_sub (src->parent, src->parentoffset, size);
627 src->parentoffset += size;
628 } else {
629 /* the parent is useless now */
630 gst_buffer_unref (src->parent);
631 src->parent = NULL;
632 /* try again (this will allocate a new parent) */
633 return gst_fake_src_create_buffer (src);
634 }
635 gst_fake_src_prepare_buffer (src, buf);
636 break;
637 default:
638 g_warning ("fakesrc: dunno how to allocate buffers !");
639 buf = gst_buffer_new ();
640 break;
641 }
642 if (dump) {
643 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
644 }
646 return buf;
647 }
649 static GstFlowReturn
650 gst_fake_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
651 GstBuffer ** ret)
652 {
653 GstFakeSrc *src;
654 GstBuffer *buf;
655 GstClockTime time;
657 src = GST_FAKE_SRC (basesrc);
659 if (src->buffer_count == src->segment_end) {
660 GST_INFO ("buffer_count reaches segment_end %d %d", src->buffer_count,
661 src->segment_end);
662 return GST_FLOW_UNEXPECTED;
663 }
665 buf = gst_fake_src_create_buffer (src);
666 GST_BUFFER_OFFSET (buf) = src->buffer_count++;
668 time = GST_CLOCK_TIME_NONE;
670 if (src->datarate > 0) {
671 time = (src->bytes_sent * GST_SECOND) / src->datarate;
672 if (src->sync) {
673 /* gst_element_wait (GST_ELEMENT (src), time); */
674 }
676 GST_BUFFER_DURATION (buf) =
677 GST_BUFFER_SIZE (buf) * GST_SECOND / src->datarate;
678 }
679 GST_BUFFER_TIMESTAMP (buf) = time;
681 if (!src->silent) {
682 g_free (src->last_message);
684 src->last_message =
685 g_strdup_printf ("get ******* > (%d bytes, timestamp: %"
686 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
687 G_GINT64_FORMAT ", offset_end: %" G_GINT64_FORMAT ", flags: %d) %p",
688 GST_BUFFER_SIZE (buf),
689 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
690 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
691 GST_BUFFER_OFFSET_END (buf), GST_MINI_OBJECT (buf)->flags, buf);
693 g_object_notify (G_OBJECT (src), "last_message");
694 }
696 if (src->signal_handoffs) {
697 GST_LOG_OBJECT (src, "pre handoff emit");
698 g_signal_emit (G_OBJECT (src), gst_fake_src_signals[SIGNAL_HANDOFF], 0,
699 buf, basesrc->srcpad);
700 GST_LOG_OBJECT (src, "post handoff emit");
701 }
703 src->bytes_sent += GST_BUFFER_SIZE (buf);
705 *ret = buf;
706 return GST_FLOW_OK;
707 }
709 static gboolean
710 gst_fake_src_start (GstBaseSrc * basesrc)
711 {
712 GstFakeSrc *src;
714 src = GST_FAKE_SRC (basesrc);
716 src->buffer_count = 0;
717 src->pattern_byte = 0x00;
718 src->bytes_sent = 0;
720 return TRUE;
721 }
723 static gboolean
724 gst_fake_src_stop (GstBaseSrc * basesrc)
725 {
726 GstFakeSrc *src;
728 src = GST_FAKE_SRC (basesrc);
730 if (src->parent) {
731 gst_buffer_unref (src->parent);
732 src->parent = NULL;
733 }
734 g_free (src->last_message);
735 src->last_message = NULL;
737 return TRUE;
738 }