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);
89 static gboolean gst_rnd_buffer_size_activate_pull (GstPad * pad,
91 static void gst_rnd_buffer_size_loop (GstRndBufferSize * self);
92 static GstStateChangeReturn gst_rnd_buffer_size_change_state (GstElement *
93 element, GstStateChange transition);
96 GST_BOILERPLATE (GstRndBufferSize, gst_rnd_buffer_size, GstElement,
101 gst_rnd_buffer_size_base_init (gpointer g_class)
103 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
105 gst_element_class_add_pad_template (gstelement_class,
106 gst_static_pad_template_get (&sink_template));
107 gst_element_class_add_pad_template (gstelement_class,
108 gst_static_pad_template_get (&src_template));
110 gst_element_class_set_details_simple (gstelement_class, "Random buffer size",
111 "Testing", "pull random sized buffers",
112 "Stefan Kost <stefan.kost@nokia.com>)");
117 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
122 gobject_class->set_property =
123 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_set_property);
124 gobject_class->get_property =
125 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_get_property);
126 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_finalize);
128 gstelement_class->change_state =
129 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
131 /* FIXME 0.11: these should all be int instead of long, to avoid bugs
132 * when passing these as varargs with g_object_set(), and there was no
133 * reason to use long in the first place here */
134 g_object_class_install_property (gobject_class, ARG_SEED,
135 g_param_spec_ulong ("seed", "random number seed",
136 "seed for randomness (initialized when going from READY to PAUSED)",
137 0, G_MAXUINT32, DEFAULT_SEED, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
138 g_object_class_install_property (gobject_class, ARG_MINIMUM,
139 g_param_spec_long ("min", "mininum", "mininum buffer size",
140 0, G_MAXINT32, DEFAULT_MIN, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
141 g_object_class_install_property (gobject_class, ARG_MAXIMUM,
142 g_param_spec_long ("max", "maximum", "maximum buffer size",
143 1, G_MAXINT32, DEFAULT_MAX, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
147 gst_rnd_buffer_size_init (GstRndBufferSize * self,
148 GstRndBufferSizeClass * g_class)
150 self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
151 gst_pad_set_activate_function (self->sinkpad,
152 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate));
153 gst_pad_set_activatepull_function (self->sinkpad,
154 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_pull));
155 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
157 self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
158 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
163 gst_rnd_buffer_size_finalize (GObject * object)
165 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
168 g_rand_free (self->rand);
172 G_OBJECT_CLASS (parent_class)->finalize (object);
177 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
178 const GValue * value, GParamSpec * pspec)
180 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
184 self->seed = g_value_get_ulong (value);
187 self->min = g_value_get_long (value);
190 self->max = g_value_get_long (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
201 GValue * value, GParamSpec * pspec)
203 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
207 g_value_set_ulong (value, self->seed);
210 g_value_set_long (value, self->min);
213 g_value_set_long (value, self->max);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_rnd_buffer_size_activate (GstPad * pad)
225 if (gst_pad_check_pull_range (pad)) {
226 return gst_pad_activate_pull (pad, TRUE);
228 GST_INFO_OBJECT (pad, "push mode not supported");
235 gst_rnd_buffer_size_activate_pull (GstPad * pad, gboolean active)
237 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (GST_OBJECT_PARENT (pad));
240 GST_INFO_OBJECT (self, "starting pull");
241 return gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
244 GST_INFO_OBJECT (self, "stopping pull");
245 return gst_pad_stop_task (pad);
251 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
253 GstBuffer *buf = NULL;
257 if (G_UNLIKELY (self->min > self->max))
260 if (G_UNLIKELY (self->min != self->max)) {
261 num_bytes = g_rand_int_range (self->rand, self->min, self->max);
263 num_bytes = self->min;
266 GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
267 num_bytes, self->offset);
269 ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
271 if (ret != GST_FLOW_OK)
274 if (GST_BUFFER_SIZE (buf) < num_bytes) {
275 GST_WARNING_OBJECT (self, "short buffer: %u bytes", GST_BUFFER_SIZE (buf));
278 self->offset += GST_BUFFER_SIZE (buf);
280 ret = gst_pad_push (self->srcpad, buf);
282 if (ret != GST_FLOW_OK)
289 GST_DEBUG_OBJECT (self, "pausing task");
290 gst_pad_pause_task (self->sinkpad);
296 if (ret == GST_FLOW_UNEXPECTED) {
297 GST_DEBUG_OBJECT (self, "eos");
298 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
300 GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
307 GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
308 if (ret == GST_FLOW_UNEXPECTED) {
309 GST_DEBUG_OBJECT (self, "eos");
310 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
311 } else if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
312 GST_ELEMENT_ERROR (self, STREAM, FAILED,
313 ("Internal data stream error."),
314 ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
321 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
322 ("The minimum buffer size is smaller than the maximum buffer size."),
323 ("buffer sizes: max=%ld, min=%ld", self->min, self->max));
328 static GstStateChangeReturn
329 gst_rnd_buffer_size_change_state (GstElement * element,
330 GstStateChange transition)
332 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
333 GstStateChangeReturn ret;
335 switch (transition) {
336 case GST_STATE_CHANGE_NULL_TO_READY:
338 case GST_STATE_CHANGE_READY_TO_PAUSED:
341 self->rand = g_rand_new_with_seed (self->seed);
344 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
350 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
352 switch (transition) {
353 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
355 case GST_STATE_CHANGE_PAUSED_TO_READY:
357 g_rand_free (self->rand);
361 case GST_STATE_CHANGE_READY_TO_NULL:
372 gst_rnd_buffer_size_plugin_init (GstPlugin * plugin)
374 if (!gst_element_register (plugin, "rndbuffersize", GST_RANK_NONE,
375 GST_TYPE_RND_BUFFER_SIZE))
378 GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0,
379 "rndbuffersize element");