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);
95 #define DEBUG_INIT(bla) \
96 GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0, \
97 "rndbuffersize element");
99 GST_BOILERPLATE_FULL (GstRndBufferSize, gst_rnd_buffer_size, GstElement,
100 GST_TYPE_ELEMENT, DEBUG_INIT);
104 gst_rnd_buffer_size_base_init (gpointer g_class)
106 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
108 gst_element_class_add_pad_template (gstelement_class,
109 gst_static_pad_template_get (&sink_template));
110 gst_element_class_add_pad_template (gstelement_class,
111 gst_static_pad_template_get (&src_template));
113 gst_element_class_set_details_simple (gstelement_class, "Random buffer size",
114 "Testing", "pull random sized buffers",
115 "Stefan Kost <stefan.kost@nokia.com>)");
120 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
122 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
123 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
125 gobject_class->set_property =
126 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_set_property);
127 gobject_class->get_property =
128 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_get_property);
129 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_finalize);
131 gstelement_class->change_state =
132 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
134 /* FIXME 0.11: these should all be int instead of long, to avoid bugs
135 * when passing these as varargs with g_object_set(), and there was no
136 * reason to use long in the first place here */
137 g_object_class_install_property (gobject_class, ARG_SEED,
138 g_param_spec_ulong ("seed", "random number seed",
139 "seed for randomness (initialized when going from READY to PAUSED)",
140 0, G_MAXUINT32, DEFAULT_SEED, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
141 g_object_class_install_property (gobject_class, ARG_MINIMUM,
142 g_param_spec_long ("min", "mininum", "mininum buffer size",
143 0, G_MAXINT32, DEFAULT_MIN, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
144 g_object_class_install_property (gobject_class, ARG_MAXIMUM,
145 g_param_spec_long ("max", "maximum", "maximum buffer size",
146 1, G_MAXINT32, DEFAULT_MAX, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
150 gst_rnd_buffer_size_init (GstRndBufferSize * self,
151 GstRndBufferSizeClass * g_class)
153 self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
154 gst_pad_set_activate_function (self->sinkpad,
155 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate));
156 gst_pad_set_activatepull_function (self->sinkpad,
157 GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_pull));
158 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
160 self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
161 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
166 gst_rnd_buffer_size_finalize (GObject * object)
168 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
171 g_rand_free (self->rand);
175 G_OBJECT_CLASS (parent_class)->finalize (object);
180 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
181 const GValue * value, GParamSpec * pspec)
183 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
187 self->seed = g_value_get_ulong (value);
190 self->min = g_value_get_long (value);
193 self->max = g_value_get_long (value);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
204 GValue * value, GParamSpec * pspec)
206 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
210 g_value_set_ulong (value, self->seed);
213 g_value_set_long (value, self->min);
216 g_value_set_long (value, self->max);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gst_rnd_buffer_size_activate (GstPad * pad)
228 if (gst_pad_check_pull_range (pad)) {
229 return gst_pad_activate_pull (pad, TRUE);
231 GST_INFO_OBJECT (pad, "push mode not supported");
238 gst_rnd_buffer_size_activate_pull (GstPad * pad, gboolean active)
240 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (GST_OBJECT_PARENT (pad));
243 GST_INFO_OBJECT (self, "starting pull");
244 return gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
247 GST_INFO_OBJECT (self, "stopping pull");
248 return gst_pad_stop_task (pad);
254 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
256 GstBuffer *buf = NULL;
260 if (G_UNLIKELY (self->min > self->max))
263 if (G_UNLIKELY (self->min != self->max)) {
264 num_bytes = g_rand_int_range (self->rand, self->min, self->max);
266 num_bytes = self->min;
269 GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
270 num_bytes, self->offset);
272 ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
274 if (ret != GST_FLOW_OK)
277 if (GST_BUFFER_SIZE (buf) < num_bytes) {
278 GST_WARNING_OBJECT (self, "short buffer: %u bytes", GST_BUFFER_SIZE (buf));
281 self->offset += GST_BUFFER_SIZE (buf);
283 ret = gst_pad_push (self->srcpad, buf);
285 if (ret != GST_FLOW_OK)
292 GST_DEBUG_OBJECT (self, "pausing task");
293 gst_pad_pause_task (self->sinkpad);
299 if (ret == GST_FLOW_UNEXPECTED) {
300 GST_DEBUG_OBJECT (self, "eos");
301 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
303 GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
310 GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
311 if (ret == GST_FLOW_UNEXPECTED) {
312 GST_DEBUG_OBJECT (self, "eos");
313 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
314 } else if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
315 GST_ELEMENT_ERROR (self, STREAM, FAILED,
316 ("Internal data stream error."),
317 ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
324 GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
325 ("The minimum buffer size is smaller than the maximum buffer size."),
326 ("buffer sizes: max=%ld, min=%ld", self->min, self->max));
331 static GstStateChangeReturn
332 gst_rnd_buffer_size_change_state (GstElement * element,
333 GstStateChange transition)
335 GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
336 GstStateChangeReturn ret;
338 switch (transition) {
339 case GST_STATE_CHANGE_NULL_TO_READY:
341 case GST_STATE_CHANGE_READY_TO_PAUSED:
344 self->rand = g_rand_new_with_seed (self->seed);
347 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
353 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
355 switch (transition) {
356 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
358 case GST_STATE_CHANGE_PAUSED_TO_READY:
360 g_rand_free (self->rand);
364 case GST_STATE_CHANGE_READY_TO_NULL: