87bdbd093f2c43a35c2508c559a2c3aab04dcb83
[platform/upstream/gstreamer.git] / gst / debug / rndbuffersize.c
1 /* GStreamer
2  * Copyright (C) 2007 Nokia Corporation (contact <stefan.kost@nokia.com>)
3  *
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.
8  *
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.
13  *
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.
18  */
19 /**
20  * SECTION:element-rndbuffersize
21  *
22  * This element pulls buffers with random sizes from the source.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include <gst/gst.h>
30
31 GST_DEBUG_CATEGORY_STATIC (gst_rnd_buffer_size_debug);
32 #define GST_CAT_DEFAULT gst_rnd_buffer_size_debug
33
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))
39
40 typedef struct _GstRndBufferSize GstRndBufferSize;
41 typedef struct _GstRndBufferSizeClass GstRndBufferSizeClass;
42
43 struct _GstRndBufferSize
44 {
45   GstElement parent;
46
47   /*< private > */
48   GRand *rand;
49   gulong seed;
50   glong min, max;
51
52   GstPad *sinkpad, *srcpad;
53   guint64 offset;
54 };
55
56 struct _GstRndBufferSizeClass
57 {
58   GstElementClass parent_class;
59 };
60
61 enum
62 {
63   ARG_SEED = 1,
64   ARG_MINIMUM,
65   ARG_MAXIMUM
66 };
67
68 #define DEFAULT_SEED 0
69 #define DEFAULT_MIN  1
70 #define DEFAULT_MAX  (8*1024)
71
72 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS_ANY);
76
77 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
78     GST_PAD_SINK,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS_ANY);
81
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);
87
88 static gboolean gst_rnd_buffer_size_activate (GstPad * pad);
89 static gboolean gst_rnd_buffer_size_activate_pull (GstPad * pad,
90     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);
94
95
96 GST_BOILERPLATE (GstRndBufferSize, gst_rnd_buffer_size, GstElement,
97     GST_TYPE_ELEMENT);
98
99
100 static void
101 gst_rnd_buffer_size_base_init (gpointer g_class)
102 {
103   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
104
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));
109
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>)");
113 }
114
115
116 static void
117 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
118 {
119   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
121
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);
127
128   gstelement_class->change_state =
129       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
130
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));
144 }
145
146 static void
147 gst_rnd_buffer_size_init (GstRndBufferSize * self,
148     GstRndBufferSizeClass * g_class)
149 {
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);
156
157   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
158   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
159 }
160
161
162 static void
163 gst_rnd_buffer_size_finalize (GObject * object)
164 {
165   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
166
167   if (self->rand) {
168     g_rand_free (self->rand);
169     self->rand = NULL;
170   }
171
172   G_OBJECT_CLASS (parent_class)->finalize (object);
173 }
174
175
176 static void
177 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
178     const GValue * value, GParamSpec * pspec)
179 {
180   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
181
182   switch (prop_id) {
183     case ARG_SEED:
184       self->seed = g_value_get_ulong (value);
185       break;
186     case ARG_MINIMUM:
187       self->min = g_value_get_long (value);
188       break;
189     case ARG_MAXIMUM:
190       self->max = g_value_get_long (value);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195   }
196 }
197
198
199 static void
200 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
201     GValue * value, GParamSpec * pspec)
202 {
203   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
204
205   switch (prop_id) {
206     case ARG_SEED:
207       g_value_set_ulong (value, self->seed);
208       break;
209     case ARG_MINIMUM:
210       g_value_set_long (value, self->min);
211       break;
212     case ARG_MAXIMUM:
213       g_value_set_long (value, self->max);
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218   }
219 }
220
221
222 static gboolean
223 gst_rnd_buffer_size_activate (GstPad * pad)
224 {
225   if (gst_pad_check_pull_range (pad)) {
226     return gst_pad_activate_pull (pad, TRUE);
227   } else {
228     GST_INFO_OBJECT (pad, "push mode not supported");
229     return FALSE;
230   }
231 }
232
233
234 static gboolean
235 gst_rnd_buffer_size_activate_pull (GstPad * pad, gboolean active)
236 {
237   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (GST_OBJECT_PARENT (pad));
238
239   if (active) {
240     GST_INFO_OBJECT (self, "starting pull");
241     return gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
242         self);
243   } else {
244     GST_INFO_OBJECT (self, "stopping pull");
245     return gst_pad_stop_task (pad);
246   }
247 }
248
249
250 static void
251 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
252 {
253   GstBuffer *buf = NULL;
254   GstFlowReturn ret;
255   guint num_bytes;
256
257   if (G_UNLIKELY (self->min > self->max))
258     goto bogus_minmax;
259
260   if (G_UNLIKELY (self->min != self->max)) {
261     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
262   } else {
263     num_bytes = self->min;
264   }
265
266   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
267       num_bytes, self->offset);
268
269   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
270
271   if (ret != GST_FLOW_OK)
272     goto pull_failed;
273
274   if (GST_BUFFER_SIZE (buf) < num_bytes) {
275     GST_WARNING_OBJECT (self, "short buffer: %u bytes", GST_BUFFER_SIZE (buf));
276   }
277
278   self->offset += GST_BUFFER_SIZE (buf);
279
280   ret = gst_pad_push (self->srcpad, buf);
281
282   if (ret != GST_FLOW_OK)
283     goto push_failed;
284
285   return;
286
287 pause_task:
288   {
289     GST_DEBUG_OBJECT (self, "pausing task");
290     gst_pad_pause_task (self->sinkpad);
291     return;
292   }
293
294 pull_failed:
295   {
296     if (ret == GST_FLOW_UNEXPECTED) {
297       GST_DEBUG_OBJECT (self, "eos");
298       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
299     } else {
300       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
301     }
302     goto pause_task;
303   }
304
305 push_failed:
306   {
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)));
315     }
316     goto pause_task;
317   }
318
319 bogus_minmax:
320   {
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));
324     goto pause_task;
325   }
326 }
327
328 static GstStateChangeReturn
329 gst_rnd_buffer_size_change_state (GstElement * element,
330     GstStateChange transition)
331 {
332   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
333   GstStateChangeReturn ret;
334
335   switch (transition) {
336     case GST_STATE_CHANGE_NULL_TO_READY:
337       break;
338     case GST_STATE_CHANGE_READY_TO_PAUSED:
339       self->offset = 0;
340       if (!self->rand) {
341         self->rand = g_rand_new_with_seed (self->seed);
342       }
343       break;
344     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
345       break;
346     default:
347       break;
348   }
349
350   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
351
352   switch (transition) {
353     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
354       break;
355     case GST_STATE_CHANGE_PAUSED_TO_READY:
356       if (self->rand) {
357         g_rand_free (self->rand);
358         self->rand = NULL;
359       }
360       break;
361     case GST_STATE_CHANGE_READY_TO_NULL:
362       break;
363     default:
364       break;
365   }
366
367   return ret;
368 }
369
370
371 gboolean
372 gst_rnd_buffer_size_plugin_init (GstPlugin * plugin)
373 {
374   if (!gst_element_register (plugin, "rndbuffersize", GST_RANK_NONE,
375           GST_TYPE_RND_BUFFER_SIZE))
376     return FALSE;
377
378   GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0,
379       "rndbuffersize element");
380
381   return TRUE;
382 }