2 * Copyright (C) 2007 Nokia Corporation (contact <stefan.kost@nokia.com>)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:element-rndbuffersize
22 * This element pulls buffers with random sizes from the source.
31 GST_DEBUG_CATEGORY_STATIC (gst_rnd_buffer_size_debug);
32 #define GST_CAT_DEFAULT gst_rnd_buffer_size_debug
34 #define GST_TYPE_RND_BUFFER_SIZE (gst_rnd_buffer_size_get_type())
35 #define GST_RND_BUFFER_SIZE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RND_BUFFER_SIZE,GstRndBufferSize))
36 #define GST_RND_BUFFER_SIZE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RND_BUFFER_SIZE,GstRndBufferSizeClass))
37 #define GST_IS_RND_BUFFER_SIZE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RND_BUFFER_SIZE))
38 #define GST_IS_RND_BUFFER_SIZE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RND_BUFFER_SIZE))
40 typedef struct _GstRndBufferSize GstRndBufferSize;
41 typedef struct _GstRndBufferSizeClass GstRndBufferSizeClass;
43 struct _GstRndBufferSize
52 GstPad *sinkpad, *srcpad;
56 struct _GstRndBufferSizeClass
58 GstElementClass parent_class;
68 #define DEFAULT_SEED 0
70 #define DEFAULT_MAX (8*1024)
72 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
77 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
82 static void gst_rnd_buffer_size_finalize (GObject * object);
83 static void gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
88 static gboolean gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent);
89 static gboolean gst_rnd_buffer_size_activate_mode (GstPad * pad,
90 GstObject * parent, GstPadMode mode, gboolean active);
91 static void gst_rnd_buffer_size_loop (GstRndBufferSize * self);
92 static GstStateChangeReturn gst_rnd_buffer_size_change_state (GstElement *
93 element, GstStateChange transition);
95 GType gst_rnd_buffer_size_get_type (void);
96 #define gst_rnd_buffer_size_parent_class parent_class
97 G_DEFINE_TYPE (GstRndBufferSize, gst_rnd_buffer_size, GST_TYPE_ELEMENT);
100 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
102 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
105 GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0,
106 "rndbuffersize element");
108 gobject_class->set_property = gst_rnd_buffer_size_set_property;
109 gobject_class->get_property = gst_rnd_buffer_size_get_property;
110 gobject_class->finalize = gst_rnd_buffer_size_finalize;
112 gst_element_class_add_pad_template (gstelement_class,
113 gst_static_pad_template_get (&sink_template));
114 gst_element_class_add_pad_template (gstelement_class,
115 gst_static_pad_template_get (&src_template));
117 gst_element_class_set_details_simple (gstelement_class, "Random buffer size",
118 "Testing", "pull random sized buffers",
119 "Stefan Kost <stefan.kost@nokia.com>");
121 gstelement_class->change_state =
122 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
124 /* FIXME 0.11: these should all be int instead of long, to avoid bugs
125 * when passing these as varargs with g_object_set(), and there was no
126 * reason to use long in the first place here */
127 g_object_class_install_property (gobject_class, ARG_SEED,
128 g_param_spec_ulong ("seed", "random number seed",
129 "seed for randomness (initialized when going from READY to PAUSED)",
130 0, G_MAXUINT32, DEFAULT_SEED,
131 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
132 g_object_class_install_property (gobject_class, ARG_MINIMUM,
133 g_param_spec_long ("min", "mininum", "mininum buffer size",
134 0, G_MAXINT32, DEFAULT_MIN,
135 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
136 g_object_class_install_property (gobject_class, ARG_MAXIMUM,
137 g_param_spec_long ("max", "maximum", "maximum buffer size",
138 1, G_MAXINT32, DEFAULT_MAX,
139 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
143 gst_rnd_buffer_size_init (GstRndBufferSize * self)
145 self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
146 gst_pad_set_activate_function (self->sinkpad,
147 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate));
148 gst_pad_set_activatemode_function (self->sinkpad,
149 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_mode));
150 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
152 self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
153 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
158 gst_rnd_buffer_size_finalize (GObject * object)
160 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
163 g_rand_free (self->rand);
167 G_OBJECT_CLASS (parent_class)->finalize (object);
172 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec)
175 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
179 self->seed = g_value_get_ulong (value);
182 self->min = g_value_get_long (value);
185 self->max = g_value_get_long (value);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
196 GValue * value, GParamSpec * pspec)
198 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
202 g_value_set_ulong (value, self->seed);
205 g_value_set_long (value, self->min);
208 g_value_set_long (value, self->max);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent)
223 query = gst_query_new_scheduling ();
225 if (!gst_pad_peer_query (pad, query)) {
226 gst_query_unref (query);
230 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
231 gst_query_unref (query);
236 GST_DEBUG_OBJECT (pad, "activating pull");
237 return gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
242 GST_DEBUG_OBJECT (pad, "pull mode not supported");
249 gst_rnd_buffer_size_activate_mode (GstPad * pad, GstObject * parent,
250 GstPadMode mode, gboolean active)
253 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (parent);
256 case GST_PAD_MODE_PULL:
258 GST_INFO_OBJECT (self, "starting pull");
260 gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
263 GST_INFO_OBJECT (self, "stopping pull");
264 res = gst_pad_stop_task (pad);
276 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
278 GstBuffer *buf = NULL;
280 guint num_bytes, size;
282 if (G_UNLIKELY (self->min > self->max))
285 if (G_UNLIKELY (self->min != self->max)) {
286 num_bytes = g_rand_int_range (self->rand, self->min, self->max);
288 num_bytes = self->min;
291 GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
292 num_bytes, self->offset);
294 ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
296 if (ret != GST_FLOW_OK)
299 size = gst_buffer_get_size (buf);
301 if (size < num_bytes) {
302 GST_WARNING_OBJECT (self, "short buffer: %u bytes", size);
305 self->offset += size;
307 ret = gst_pad_push (self->srcpad, buf);
309 if (ret != GST_FLOW_OK)
316 GST_DEBUG_OBJECT (self, "pausing task");
317 gst_pad_pause_task (self->sinkpad);
323 if (ret == GST_FLOW_UNEXPECTED) {
324 GST_DEBUG_OBJECT (self, "eos");
325 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
327 GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
334 GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
335 if (ret == GST_FLOW_UNEXPECTED) {
336 GST_DEBUG_OBJECT (self, "eos");
337 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
338 } else if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) {
339 GST_ELEMENT_ERROR (self, STREAM, FAILED,
340 ("Internal data stream error."),
341 ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
348 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
349 ("The minimum buffer size is smaller than the maximum buffer size."),
350 ("buffer sizes: max=%ld, min=%ld", self->min, self->max));
355 static GstStateChangeReturn
356 gst_rnd_buffer_size_change_state (GstElement * element,
357 GstStateChange transition)
359 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
360 GstStateChangeReturn ret;
362 switch (transition) {
363 case GST_STATE_CHANGE_NULL_TO_READY:
365 case GST_STATE_CHANGE_READY_TO_PAUSED:
368 self->rand = g_rand_new_with_seed (self->seed);
371 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
377 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
379 switch (transition) {
380 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
382 case GST_STATE_CHANGE_PAUSED_TO_READY:
384 g_rand_free (self->rand);
388 case GST_STATE_CHANGE_READY_TO_NULL: