upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.git] / gst / debugutils / 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 #define DEBUG_INIT(bla) \
96   GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0, \
97       "rndbuffersize element");
98
99 GType gst_rnd_buffer_size_get_type (void);
100 GST_BOILERPLATE_FULL (GstRndBufferSize, gst_rnd_buffer_size, GstElement,
101     GST_TYPE_ELEMENT, DEBUG_INIT);
102
103
104 static void
105 gst_rnd_buffer_size_base_init (gpointer g_class)
106 {
107   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
108
109   gst_element_class_add_pad_template (gstelement_class,
110       gst_static_pad_template_get (&sink_template));
111   gst_element_class_add_pad_template (gstelement_class,
112       gst_static_pad_template_get (&src_template));
113
114   gst_element_class_set_details_simple (gstelement_class, "Random buffer size",
115       "Testing", "pull random sized buffers",
116       "Stefan Kost <stefan.kost@nokia.com>)");
117 }
118
119
120 static void
121 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
122 {
123   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
124   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
125
126   gobject_class->set_property = gst_rnd_buffer_size_set_property;
127   gobject_class->get_property = gst_rnd_buffer_size_get_property;
128   gobject_class->finalize = gst_rnd_buffer_size_finalize;
129
130   gstelement_class->change_state =
131       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
132
133   /* FIXME 0.11: these should all be int instead of long, to avoid bugs
134    * when passing these as varargs with g_object_set(), and there was no
135    * reason to use long in the first place here */
136   g_object_class_install_property (gobject_class, ARG_SEED,
137       g_param_spec_ulong ("seed", "random number seed",
138           "seed for randomness (initialized when going from READY to PAUSED)",
139           0, G_MAXUINT32, DEFAULT_SEED,
140           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
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,
144           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
145   g_object_class_install_property (gobject_class, ARG_MAXIMUM,
146       g_param_spec_long ("max", "maximum", "maximum buffer size",
147           1, G_MAXINT32, DEFAULT_MAX,
148           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
149 }
150
151 static void
152 gst_rnd_buffer_size_init (GstRndBufferSize * self,
153     GstRndBufferSizeClass * g_class)
154 {
155   self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
156   gst_pad_set_activate_function (self->sinkpad,
157       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate));
158   gst_pad_set_activatepull_function (self->sinkpad,
159       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_pull));
160   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
161
162   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
163   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
164 }
165
166
167 static void
168 gst_rnd_buffer_size_finalize (GObject * object)
169 {
170   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
171
172   if (self->rand) {
173     g_rand_free (self->rand);
174     self->rand = NULL;
175   }
176
177   G_OBJECT_CLASS (parent_class)->finalize (object);
178 }
179
180
181 static void
182 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
183     const GValue * value, GParamSpec * pspec)
184 {
185   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
186
187   switch (prop_id) {
188     case ARG_SEED:
189       self->seed = g_value_get_ulong (value);
190       break;
191     case ARG_MINIMUM:
192       self->min = g_value_get_long (value);
193       break;
194     case ARG_MAXIMUM:
195       self->max = g_value_get_long (value);
196       break;
197     default:
198       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199       break;
200   }
201 }
202
203
204 static void
205 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
206     GValue * value, GParamSpec * pspec)
207 {
208   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
209
210   switch (prop_id) {
211     case ARG_SEED:
212       g_value_set_ulong (value, self->seed);
213       break;
214     case ARG_MINIMUM:
215       g_value_set_long (value, self->min);
216       break;
217     case ARG_MAXIMUM:
218       g_value_set_long (value, self->max);
219       break;
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222       break;
223   }
224 }
225
226
227 static gboolean
228 gst_rnd_buffer_size_activate (GstPad * pad)
229 {
230   if (gst_pad_check_pull_range (pad)) {
231     return gst_pad_activate_pull (pad, TRUE);
232   } else {
233     GST_INFO_OBJECT (pad, "push mode not supported");
234     return FALSE;
235   }
236 }
237
238
239 static gboolean
240 gst_rnd_buffer_size_activate_pull (GstPad * pad, gboolean active)
241 {
242   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (GST_OBJECT_PARENT (pad));
243
244   if (active) {
245     GST_INFO_OBJECT (self, "starting pull");
246     return gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
247         self);
248   } else {
249     GST_INFO_OBJECT (self, "stopping pull");
250     return gst_pad_stop_task (pad);
251   }
252 }
253
254
255 static void
256 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
257 {
258   GstBuffer *buf = NULL;
259   GstFlowReturn ret;
260   guint num_bytes;
261
262   if (G_UNLIKELY (self->min > self->max))
263     goto bogus_minmax;
264
265   if (G_UNLIKELY (self->min != self->max)) {
266     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
267   } else {
268     num_bytes = self->min;
269   }
270
271   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
272       num_bytes, self->offset);
273
274   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
275
276   if (ret != GST_FLOW_OK)
277     goto pull_failed;
278
279   if (GST_BUFFER_SIZE (buf) < num_bytes) {
280     GST_WARNING_OBJECT (self, "short buffer: %u bytes", GST_BUFFER_SIZE (buf));
281   }
282
283   self->offset += GST_BUFFER_SIZE (buf);
284
285   ret = gst_pad_push (self->srcpad, buf);
286
287   if (ret != GST_FLOW_OK)
288     goto push_failed;
289
290   return;
291
292 pause_task:
293   {
294     GST_DEBUG_OBJECT (self, "pausing task");
295     gst_pad_pause_task (self->sinkpad);
296     return;
297   }
298
299 pull_failed:
300   {
301     if (ret == GST_FLOW_UNEXPECTED) {
302       GST_DEBUG_OBJECT (self, "eos");
303       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
304     } else {
305       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
306     }
307     goto pause_task;
308   }
309
310 push_failed:
311   {
312     GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
313     if (ret == GST_FLOW_UNEXPECTED) {
314       GST_DEBUG_OBJECT (self, "eos");
315       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
316     } else if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) {
317       GST_ELEMENT_ERROR (self, STREAM, FAILED,
318           ("Internal data stream error."),
319           ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
320     }
321     goto pause_task;
322   }
323
324 bogus_minmax:
325   {
326     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
327         ("The minimum buffer size is smaller than the maximum buffer size."),
328         ("buffer sizes: max=%ld, min=%ld", self->min, self->max));
329     goto pause_task;
330   }
331 }
332
333 static GstStateChangeReturn
334 gst_rnd_buffer_size_change_state (GstElement * element,
335     GstStateChange transition)
336 {
337   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
338   GstStateChangeReturn ret;
339
340   switch (transition) {
341     case GST_STATE_CHANGE_NULL_TO_READY:
342       break;
343     case GST_STATE_CHANGE_READY_TO_PAUSED:
344       self->offset = 0;
345       if (!self->rand) {
346         self->rand = g_rand_new_with_seed (self->seed);
347       }
348       break;
349     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
350       break;
351     default:
352       break;
353   }
354
355   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
356
357   switch (transition) {
358     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
359       break;
360     case GST_STATE_CHANGE_PAUSED_TO_READY:
361       if (self->rand) {
362         g_rand_free (self->rand);
363         self->rand = NULL;
364       }
365       break;
366     case GST_STATE_CHANGE_READY_TO_NULL:
367       break;
368     default:
369       break;
370   }
371
372   return ret;
373 }