add parent to query function
[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);
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 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_activatepull_function (self->sinkpad,
149       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_pull));
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)
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   gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
231
232   if (!pull_mode)
233     goto no_pull;
234
235   GST_DEBUG_OBJECT (pad, "activating pull");
236   return gst_pad_activate_pull (pad, TRUE);
237
238   /* ERRORS */
239 no_pull:
240   {
241     GST_DEBUG_OBJECT (pad, "pull mode not supported");
242     return FALSE;
243   }
244 }
245
246
247 static gboolean
248 gst_rnd_buffer_size_activate_pull (GstPad * pad, gboolean active)
249 {
250   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (GST_OBJECT_PARENT (pad));
251
252   if (active) {
253     GST_INFO_OBJECT (self, "starting pull");
254     return gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
255         self);
256   } else {
257     GST_INFO_OBJECT (self, "stopping pull");
258     return gst_pad_stop_task (pad);
259   }
260 }
261
262
263 static void
264 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
265 {
266   GstBuffer *buf = NULL;
267   GstFlowReturn ret;
268   guint num_bytes, size;
269
270   if (G_UNLIKELY (self->min > self->max))
271     goto bogus_minmax;
272
273   if (G_UNLIKELY (self->min != self->max)) {
274     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
275   } else {
276     num_bytes = self->min;
277   }
278
279   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
280       num_bytes, self->offset);
281
282   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
283
284   if (ret != GST_FLOW_OK)
285     goto pull_failed;
286
287   size = gst_buffer_get_size (buf);
288
289   if (size < num_bytes) {
290     GST_WARNING_OBJECT (self, "short buffer: %u bytes", size);
291   }
292
293   self->offset += size;
294
295   ret = gst_pad_push (self->srcpad, buf);
296
297   if (ret != GST_FLOW_OK)
298     goto push_failed;
299
300   return;
301
302 pause_task:
303   {
304     GST_DEBUG_OBJECT (self, "pausing task");
305     gst_pad_pause_task (self->sinkpad);
306     return;
307   }
308
309 pull_failed:
310   {
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 {
315       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
316     }
317     goto pause_task;
318   }
319
320 push_failed:
321   {
322     GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
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 if (ret < GST_FLOW_UNEXPECTED || ret == GST_FLOW_NOT_LINKED) {
327       GST_ELEMENT_ERROR (self, STREAM, FAILED,
328           ("Internal data stream error."),
329           ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
330     }
331     goto pause_task;
332   }
333
334 bogus_minmax:
335   {
336     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
337         ("The minimum buffer size is smaller than the maximum buffer size."),
338         ("buffer sizes: max=%ld, min=%ld", self->min, self->max));
339     goto pause_task;
340   }
341 }
342
343 static GstStateChangeReturn
344 gst_rnd_buffer_size_change_state (GstElement * element,
345     GstStateChange transition)
346 {
347   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
348   GstStateChangeReturn ret;
349
350   switch (transition) {
351     case GST_STATE_CHANGE_NULL_TO_READY:
352       break;
353     case GST_STATE_CHANGE_READY_TO_PAUSED:
354       self->offset = 0;
355       if (!self->rand) {
356         self->rand = g_rand_new_with_seed (self->seed);
357       }
358       break;
359     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
360       break;
361     default:
362       break;
363   }
364
365   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
366
367   switch (transition) {
368     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
369       break;
370     case GST_STATE_CHANGE_PAUSED_TO_READY:
371       if (self->rand) {
372         g_rand_free (self->rand);
373         self->rand = NULL;
374       }
375       break;
376     case GST_STATE_CHANGE_READY_TO_NULL:
377       break;
378     default:
379       break;
380   }
381
382   return ret;
383 }