Remove dummy plugin_init. Remove some undefined entries from doc- section file. Add...
[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 #define DEBUG_INIT(bla) \
96   GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0, \
97       "rndbuffersize element");
98
99 GST_BOILERPLATE_FULL (GstRndBufferSize, gst_rnd_buffer_size, GstElement,
100     GST_TYPE_ELEMENT, DEBUG_INIT);
101
102
103 static void
104 gst_rnd_buffer_size_base_init (gpointer g_class)
105 {
106   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
107
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));
112
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>)");
116 }
117
118
119 static void
120 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
121 {
122   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
123   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
124
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);
130
131   gstelement_class->change_state =
132       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
133
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));
147 }
148
149 static void
150 gst_rnd_buffer_size_init (GstRndBufferSize * self,
151     GstRndBufferSizeClass * g_class)
152 {
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);
159
160   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
161   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
162 }
163
164
165 static void
166 gst_rnd_buffer_size_finalize (GObject * object)
167 {
168   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
169
170   if (self->rand) {
171     g_rand_free (self->rand);
172     self->rand = NULL;
173   }
174
175   G_OBJECT_CLASS (parent_class)->finalize (object);
176 }
177
178
179 static void
180 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
181     const GValue * value, GParamSpec * pspec)
182 {
183   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
184
185   switch (prop_id) {
186     case ARG_SEED:
187       self->seed = g_value_get_ulong (value);
188       break;
189     case ARG_MINIMUM:
190       self->min = g_value_get_long (value);
191       break;
192     case ARG_MAXIMUM:
193       self->max = g_value_get_long (value);
194       break;
195     default:
196       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197       break;
198   }
199 }
200
201
202 static void
203 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
204     GValue * value, GParamSpec * pspec)
205 {
206   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
207
208   switch (prop_id) {
209     case ARG_SEED:
210       g_value_set_ulong (value, self->seed);
211       break;
212     case ARG_MINIMUM:
213       g_value_set_long (value, self->min);
214       break;
215     case ARG_MAXIMUM:
216       g_value_set_long (value, self->max);
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221   }
222 }
223
224
225 static gboolean
226 gst_rnd_buffer_size_activate (GstPad * pad)
227 {
228   if (gst_pad_check_pull_range (pad)) {
229     return gst_pad_activate_pull (pad, TRUE);
230   } else {
231     GST_INFO_OBJECT (pad, "push mode not supported");
232     return FALSE;
233   }
234 }
235
236
237 static gboolean
238 gst_rnd_buffer_size_activate_pull (GstPad * pad, gboolean active)
239 {
240   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (GST_OBJECT_PARENT (pad));
241
242   if (active) {
243     GST_INFO_OBJECT (self, "starting pull");
244     return gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
245         self);
246   } else {
247     GST_INFO_OBJECT (self, "stopping pull");
248     return gst_pad_stop_task (pad);
249   }
250 }
251
252
253 static void
254 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
255 {
256   GstBuffer *buf = NULL;
257   GstFlowReturn ret;
258   guint num_bytes;
259
260   if (G_UNLIKELY (self->min > self->max))
261     goto bogus_minmax;
262
263   if (G_UNLIKELY (self->min != self->max)) {
264     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
265   } else {
266     num_bytes = self->min;
267   }
268
269   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
270       num_bytes, self->offset);
271
272   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
273
274   if (ret != GST_FLOW_OK)
275     goto pull_failed;
276
277   if (GST_BUFFER_SIZE (buf) < num_bytes) {
278     GST_WARNING_OBJECT (self, "short buffer: %u bytes", GST_BUFFER_SIZE (buf));
279   }
280
281   self->offset += GST_BUFFER_SIZE (buf);
282
283   ret = gst_pad_push (self->srcpad, buf);
284
285   if (ret != GST_FLOW_OK)
286     goto push_failed;
287
288   return;
289
290 pause_task:
291   {
292     GST_DEBUG_OBJECT (self, "pausing task");
293     gst_pad_pause_task (self->sinkpad);
294     return;
295   }
296
297 pull_failed:
298   {
299     if (ret == GST_FLOW_UNEXPECTED) {
300       GST_DEBUG_OBJECT (self, "eos");
301       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
302     } else {
303       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
304     }
305     goto pause_task;
306   }
307
308 push_failed:
309   {
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)));
318     }
319     goto pause_task;
320   }
321
322 bogus_minmax:
323   {
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));
327     goto pause_task;
328   }
329 }
330
331 static GstStateChangeReturn
332 gst_rnd_buffer_size_change_state (GstElement * element,
333     GstStateChange transition)
334 {
335   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
336   GstStateChangeReturn ret;
337
338   switch (transition) {
339     case GST_STATE_CHANGE_NULL_TO_READY:
340       break;
341     case GST_STATE_CHANGE_READY_TO_PAUSED:
342       self->offset = 0;
343       if (!self->rand) {
344         self->rand = g_rand_new_with_seed (self->seed);
345       }
346       break;
347     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
348       break;
349     default:
350       break;
351   }
352
353   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
354
355   switch (transition) {
356     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
357       break;
358     case GST_STATE_CHANGE_PAUSED_TO_READY:
359       if (self->rand) {
360         g_rand_free (self->rand);
361         self->rand = NULL;
362       }
363       break;
364     case GST_STATE_CHANGE_READY_TO_NULL:
365       break;
366     default:
367       break;
368   }
369
370   return ret;
371 }