Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / debugutils / gstchopmydata.c
1 /* GStreamer
2  * Copyright (C) 2010 David Schleef <ds@schleef.org>
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-gstchopmydata
21  *
22  * The chopmydata element takes an incoming stream and chops it up
23  * into randomly sized buffers.  Size of outgoing buffers are determined
24  * by the max-size, min-size, and step-size properties.
25  *
26  * <refsect2>
27  * <title>Example launch line</title>
28  * |[
29  * gst-launch -v audiotestsrc num-buffers=10 ! gstchopmydata min-size=100
30  * max-size=200 step-size=2 ! fakesink -v
31  * ]|
32  * 
33  * This pipeline will create 10 buffers that are by default 2048 bytes
34  * each (1024 samples each), and chop them up into buffers that range
35  * in size from 100 bytes to 200 bytes, with the restriction that sizes
36  * are a multiple of 2.  This restriction is important, because the
37  * default sample size for audiotestsrc is 2 bytes (one channel, 16-bit
38  * audio).
39  * 
40  * </refsect2>
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <gst/gst.h>
48 #include <gst/gst.h>
49 #include "gstchopmydata.h"
50
51 /* prototypes */
52
53
54 static void gst_chop_my_data_set_property (GObject * object,
55     guint property_id, const GValue * value, GParamSpec * pspec);
56 static void gst_chop_my_data_get_property (GObject * object,
57     guint property_id, GValue * value, GParamSpec * pspec);
58
59 static GstStateChangeReturn
60 gst_chop_my_data_change_state (GstElement * element, GstStateChange transition);
61
62 static GstFlowReturn gst_chop_my_data_chain (GstPad * pad, GstBuffer * buffer);
63 static gboolean gst_chop_my_data_sink_event (GstPad * pad, GstEvent * event);
64 static gboolean gst_chop_my_data_src_event (GstPad * pad, GstEvent * event);
65
66 #define DEFAULT_MAX_SIZE 4096
67 #define DEFAULT_MIN_SIZE 1
68 #define DEFAULT_STEP_SIZE 1
69
70 enum
71 {
72   PROP_0,
73   PROP_MAX_SIZE,
74   PROP_MIN_SIZE,
75   PROP_STEP_SIZE
76 };
77
78 /* pad templates */
79
80 static GstStaticPadTemplate gst_chop_my_data_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
82     GST_PAD_SINK,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS_ANY);
85
86 static GstStaticPadTemplate gst_chop_my_data_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
88     GST_PAD_SRC,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS_ANY);
91
92 /* class initialization */
93
94 GST_BOILERPLATE (GstChopMyData, gst_chop_my_data, GstElement, GST_TYPE_ELEMENT);
95
96 static void
97 gst_chop_my_data_base_init (gpointer g_class)
98 {
99   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
100
101   gst_element_class_add_static_pad_template (element_class,
102       &gst_chop_my_data_src_template);
103   gst_element_class_add_static_pad_template (element_class,
104       &gst_chop_my_data_sink_template);
105
106   gst_element_class_set_details_simple (element_class, "FIXME",
107       "Generic", "FIXME", "David Schleef <ds@schleef.org>");
108 }
109
110 static void
111 gst_chop_my_data_class_init (GstChopMyDataClass * klass)
112 {
113   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
115
116   gobject_class->set_property = gst_chop_my_data_set_property;
117   gobject_class->get_property = gst_chop_my_data_get_property;
118   element_class->change_state =
119       GST_DEBUG_FUNCPTR (gst_chop_my_data_change_state);
120
121   g_object_class_install_property (gobject_class, PROP_MAX_SIZE,
122       g_param_spec_int ("max-size", "max-size",
123           "Maximum size of outgoing buffers", 1, G_MAXINT,
124           DEFAULT_MAX_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125   g_object_class_install_property (gobject_class, PROP_MIN_SIZE,
126       g_param_spec_int ("min-size", "max-size",
127           "Minimum size of outgoing buffers", 1, G_MAXINT,
128           DEFAULT_MIN_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129   g_object_class_install_property (gobject_class, PROP_STEP_SIZE,
130       g_param_spec_int ("step-size", "step-size",
131           "Step increment for random buffer sizes", 1, G_MAXINT,
132           DEFAULT_MAX_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133 }
134
135 static void
136 gst_chop_my_data_init (GstChopMyData * chopmydata,
137     GstChopMyDataClass * chopmydata_class)
138 {
139
140   chopmydata->sinkpad =
141       gst_pad_new_from_static_template (&gst_chop_my_data_sink_template,
142       "sink");
143   gst_pad_set_event_function (chopmydata->sinkpad,
144       GST_DEBUG_FUNCPTR (gst_chop_my_data_sink_event));
145   gst_pad_set_chain_function (chopmydata->sinkpad,
146       GST_DEBUG_FUNCPTR (gst_chop_my_data_chain));
147   gst_pad_set_getcaps_function (chopmydata->sinkpad, gst_pad_proxy_getcaps);
148   gst_pad_set_setcaps_function (chopmydata->sinkpad, gst_pad_proxy_setcaps);
149   gst_element_add_pad (GST_ELEMENT (chopmydata), chopmydata->sinkpad);
150
151   chopmydata->srcpad =
152       gst_pad_new_from_static_template (&gst_chop_my_data_src_template, "src");
153   gst_pad_set_event_function (chopmydata->srcpad,
154       GST_DEBUG_FUNCPTR (gst_chop_my_data_src_event));
155   gst_pad_set_getcaps_function (chopmydata->srcpad, gst_pad_proxy_getcaps);
156   gst_pad_set_setcaps_function (chopmydata->srcpad, gst_pad_proxy_setcaps);
157   gst_element_add_pad (GST_ELEMENT (chopmydata), chopmydata->srcpad);
158
159   chopmydata->step_size = DEFAULT_STEP_SIZE;
160   chopmydata->min_size = DEFAULT_MIN_SIZE;
161   chopmydata->max_size = DEFAULT_MAX_SIZE;
162 }
163
164 void
165 gst_chop_my_data_set_property (GObject * object, guint property_id,
166     const GValue * value, GParamSpec * pspec)
167 {
168   GstChopMyData *chopmydata;
169
170   g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
171   chopmydata = GST_CHOP_MY_DATA (object);
172
173   switch (property_id) {
174     case PROP_MAX_SIZE:
175       chopmydata->max_size = g_value_get_int (value);
176       break;
177     case PROP_MIN_SIZE:
178       chopmydata->min_size = g_value_get_int (value);
179       break;
180     case PROP_STEP_SIZE:
181       chopmydata->step_size = g_value_get_int (value);
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
185       break;
186   }
187 }
188
189 void
190 gst_chop_my_data_get_property (GObject * object, guint property_id,
191     GValue * value, GParamSpec * pspec)
192 {
193   GstChopMyData *chopmydata;
194
195   g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
196   chopmydata = GST_CHOP_MY_DATA (object);
197
198   switch (property_id) {
199     case PROP_MAX_SIZE:
200       g_value_set_int (value, chopmydata->max_size);
201       break;
202     case PROP_MIN_SIZE:
203       g_value_set_int (value, chopmydata->min_size);
204       break;
205     case PROP_STEP_SIZE:
206       g_value_set_int (value, chopmydata->step_size);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
210       break;
211   }
212 }
213
214 static GstStateChangeReturn
215 gst_chop_my_data_change_state (GstElement * element, GstStateChange transition)
216 {
217   GstChopMyData *chopmydata;
218   GstStateChangeReturn ret;
219
220   chopmydata = GST_CHOP_MY_DATA (element);
221
222   switch (transition) {
223     case GST_STATE_CHANGE_NULL_TO_READY:
224       break;
225     case GST_STATE_CHANGE_READY_TO_PAUSED:
226       GST_OBJECT_LOCK (chopmydata);
227       chopmydata->adapter = gst_adapter_new ();
228       chopmydata->rand = g_rand_new ();
229       chopmydata->next_size = 0;
230       GST_OBJECT_UNLOCK (chopmydata);
231       break;
232     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
233       break;
234     default:
235       break;
236   }
237
238   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
239
240   switch (transition) {
241     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
242       break;
243     case GST_STATE_CHANGE_PAUSED_TO_READY:
244       GST_OBJECT_LOCK (chopmydata);
245       g_object_unref (chopmydata->adapter);
246       chopmydata->adapter = NULL;
247       g_rand_free (chopmydata->rand);
248       GST_OBJECT_UNLOCK (chopmydata);
249       break;
250     case GST_STATE_CHANGE_READY_TO_NULL:
251       break;
252     default:
253       break;
254   }
255
256   return ret;
257 }
258
259 static void
260 get_next_size (GstChopMyData * chopmydata)
261 {
262   int begin;
263   int end;
264
265   begin = (chopmydata->min_size + chopmydata->step_size - 1) /
266       chopmydata->step_size;
267   end = (chopmydata->max_size + chopmydata->step_size) / chopmydata->step_size;
268
269   if (begin >= end) {
270     chopmydata->next_size = begin * chopmydata->step_size;
271     return;
272   }
273
274   chopmydata->next_size =
275       g_rand_int_range (chopmydata->rand, begin, end) * chopmydata->step_size;
276 }
277
278 static GstFlowReturn
279 gst_chop_my_data_process (GstChopMyData * chopmydata, gboolean flush)
280 {
281   GstFlowReturn ret = GST_FLOW_OK;
282   GstBuffer *buffer;
283
284   if (chopmydata->next_size == 0) {
285     get_next_size (chopmydata);
286   }
287
288   while (gst_adapter_available (chopmydata->adapter) >= chopmydata->next_size) {
289     buffer =
290         gst_adapter_take_buffer (chopmydata->adapter, chopmydata->next_size);
291
292     chopmydata->next_size = 0;
293
294     ret = gst_pad_push (chopmydata->srcpad, buffer);
295     if (ret != GST_FLOW_OK) {
296       return ret;
297     }
298
299     get_next_size (chopmydata);
300   }
301
302   if (flush) {
303     guint min_size = chopmydata->min_size;
304
305     while (gst_adapter_available (chopmydata->adapter) >= min_size) {
306       buffer = gst_adapter_take_buffer (chopmydata->adapter, min_size);
307       ret = gst_pad_push (chopmydata->srcpad, buffer);
308       if (ret != GST_FLOW_OK)
309         break;
310     }
311     gst_adapter_clear (chopmydata->adapter);
312   }
313
314   return ret;
315 }
316
317 static GstFlowReturn
318 gst_chop_my_data_chain (GstPad * pad, GstBuffer * buffer)
319 {
320   GstChopMyData *chopmydata;
321   GstFlowReturn ret;
322
323   chopmydata = GST_CHOP_MY_DATA (gst_pad_get_parent (pad));
324
325   GST_DEBUG_OBJECT (chopmydata, "chain");
326
327   gst_adapter_push (chopmydata->adapter, buffer);
328   ret = gst_chop_my_data_process (chopmydata, FALSE);
329
330   gst_object_unref (chopmydata);
331   return ret;
332 }
333
334 static gboolean
335 gst_chop_my_data_sink_event (GstPad * pad, GstEvent * event)
336 {
337   gboolean res;
338   GstChopMyData *chopmydata;
339
340   chopmydata = GST_CHOP_MY_DATA (gst_pad_get_parent (pad));
341
342   GST_DEBUG_OBJECT (chopmydata, "event");
343
344   switch (GST_EVENT_TYPE (event)) {
345     case GST_EVENT_FLUSH_START:
346       res = gst_pad_push_event (chopmydata->srcpad, event);
347       break;
348     case GST_EVENT_FLUSH_STOP:
349       gst_adapter_clear (chopmydata->adapter);
350       res = gst_pad_push_event (chopmydata->srcpad, event);
351       break;
352     case GST_EVENT_NEWSEGMENT:
353       res = gst_pad_push_event (chopmydata->srcpad, event);
354       break;
355     case GST_EVENT_EOS:
356       gst_chop_my_data_process (chopmydata, TRUE);
357       res = gst_pad_push_event (chopmydata->srcpad, event);
358       break;
359     default:
360       res = gst_pad_push_event (chopmydata->srcpad, event);
361       break;
362   }
363
364   gst_object_unref (chopmydata);
365   return res;
366 }
367
368 static gboolean
369 gst_chop_my_data_src_event (GstPad * pad, GstEvent * event)
370 {
371   gboolean res;
372   GstChopMyData *chopmydata;
373
374   chopmydata = GST_CHOP_MY_DATA (gst_pad_get_parent (pad));
375
376   GST_DEBUG_OBJECT (chopmydata, "event");
377
378   switch (GST_EVENT_TYPE (event)) {
379     case GST_EVENT_SEEK:
380       res = gst_pad_push_event (chopmydata->sinkpad, event);
381       break;
382     default:
383       res = gst_pad_push_event (chopmydata->sinkpad, event);
384       break;
385   }
386
387   gst_object_unref (chopmydata);
388   return res;
389 }