Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.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, 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);
94
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);
98
99 static void
100 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
101 {
102   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
104
105   GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0,
106       "rndbuffersize element");
107
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;
111
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));
116
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>");
120
121   gstelement_class->change_state =
122       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
123
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));
140 }
141
142 static void
143 gst_rnd_buffer_size_init (GstRndBufferSize * self)
144 {
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);
151
152   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
153   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
154 }
155
156
157 static void
158 gst_rnd_buffer_size_finalize (GObject * object)
159 {
160   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
161
162   if (self->rand) {
163     g_rand_free (self->rand);
164     self->rand = NULL;
165   }
166
167   G_OBJECT_CLASS (parent_class)->finalize (object);
168 }
169
170
171 static void
172 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec)
174 {
175   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
176
177   switch (prop_id) {
178     case ARG_SEED:
179       self->seed = g_value_get_ulong (value);
180       break;
181     case ARG_MINIMUM:
182       self->min = g_value_get_long (value);
183       break;
184     case ARG_MAXIMUM:
185       self->max = g_value_get_long (value);
186       break;
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189       break;
190   }
191 }
192
193
194 static void
195 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
196     GValue * value, GParamSpec * pspec)
197 {
198   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
199
200   switch (prop_id) {
201     case ARG_SEED:
202       g_value_set_ulong (value, self->seed);
203       break;
204     case ARG_MINIMUM:
205       g_value_set_long (value, self->min);
206       break;
207     case ARG_MAXIMUM:
208       g_value_set_long (value, self->max);
209       break;
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213   }
214 }
215
216
217 static gboolean
218 gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent)
219 {
220   GstQuery *query;
221   gboolean pull_mode;
222
223   query = gst_query_new_scheduling ();
224
225   if (!gst_pad_peer_query (pad, query)) {
226     gst_query_unref (query);
227     goto no_pull;
228   }
229
230   pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
231   gst_query_unref (query);
232
233   if (!pull_mode)
234     goto no_pull;
235
236   GST_DEBUG_OBJECT (pad, "activating pull");
237   return gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
238
239   /* ERRORS */
240 no_pull:
241   {
242     GST_DEBUG_OBJECT (pad, "pull mode not supported");
243     return FALSE;
244   }
245 }
246
247
248 static gboolean
249 gst_rnd_buffer_size_activate_mode (GstPad * pad, GstObject * parent,
250     GstPadMode mode, gboolean active)
251 {
252   gboolean res;
253   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (parent);
254
255   switch (mode) {
256     case GST_PAD_MODE_PULL:
257       if (active) {
258         GST_INFO_OBJECT (self, "starting pull");
259         res =
260             gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
261             self);
262       } else {
263         GST_INFO_OBJECT (self, "stopping pull");
264         res = gst_pad_stop_task (pad);
265       }
266       break;
267     default:
268       res = FALSE;
269       break;
270   }
271   return res;
272 }
273
274
275 static void
276 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
277 {
278   GstBuffer *buf = NULL;
279   GstFlowReturn ret;
280   guint num_bytes, size;
281
282   if (G_UNLIKELY (self->min > self->max))
283     goto bogus_minmax;
284
285   if (G_UNLIKELY (self->min != self->max)) {
286     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
287   } else {
288     num_bytes = self->min;
289   }
290
291   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
292       num_bytes, self->offset);
293
294   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
295
296   if (ret != GST_FLOW_OK)
297     goto pull_failed;
298
299   size = gst_buffer_get_size (buf);
300
301   if (size < num_bytes) {
302     GST_WARNING_OBJECT (self, "short buffer: %u bytes", size);
303   }
304
305   self->offset += size;
306
307   ret = gst_pad_push (self->srcpad, buf);
308
309   if (ret != GST_FLOW_OK)
310     goto push_failed;
311
312   return;
313
314 pause_task:
315   {
316     GST_DEBUG_OBJECT (self, "pausing task");
317     gst_pad_pause_task (self->sinkpad);
318     return;
319   }
320
321 pull_failed:
322   {
323     if (ret == GST_FLOW_UNEXPECTED) {
324       GST_DEBUG_OBJECT (self, "eos");
325       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
326     } else {
327       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
328     }
329     goto pause_task;
330   }
331
332 push_failed:
333   {
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)));
342     }
343     goto pause_task;
344   }
345
346 bogus_minmax:
347   {
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));
351     goto pause_task;
352   }
353 }
354
355 static GstStateChangeReturn
356 gst_rnd_buffer_size_change_state (GstElement * element,
357     GstStateChange transition)
358 {
359   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
360   GstStateChangeReturn ret;
361
362   switch (transition) {
363     case GST_STATE_CHANGE_NULL_TO_READY:
364       break;
365     case GST_STATE_CHANGE_READY_TO_PAUSED:
366       self->offset = 0;
367       if (!self->rand) {
368         self->rand = g_rand_new_with_seed (self->seed);
369       }
370       break;
371     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
372       break;
373     default:
374       break;
375   }
376
377   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
378
379   switch (transition) {
380     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
381       break;
382     case GST_STATE_CHANGE_PAUSED_TO_READY:
383       if (self->rand) {
384         g_rand_free (self->rand);
385         self->rand = NULL;
386       }
387       break;
388     case GST_STATE_CHANGE_READY_TO_NULL:
389       break;
390     default:
391       break;
392   }
393
394   return ret;
395 }