2 * Copyright (C) 2010 David Schleef <ds@schleef.org>
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.
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.
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.
20 * SECTION:element-gstchopmydata
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.
27 * <title>Example launch line</title>
29 * gst-launch -v audiotestsrc num-buffers=10 ! gstchopmydata min-size=100
30 * max-size=200 step-size=2 ! fakesink -v
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
49 #include "gstchopmydata.h"
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);
59 static GstStateChangeReturn
60 gst_chop_my_data_change_state (GstElement * element, GstStateChange transition);
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);
66 #define DEFAULT_MAX_SIZE 4096
67 #define DEFAULT_MIN_SIZE 1
68 #define DEFAULT_STEP_SIZE 1
80 static GstStaticPadTemplate gst_chop_my_data_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
86 static GstStaticPadTemplate gst_chop_my_data_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
92 /* class initialization */
94 GST_BOILERPLATE (GstChopMyData, gst_chop_my_data, GstElement, GST_TYPE_ELEMENT);
97 gst_chop_my_data_base_init (gpointer g_class)
99 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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);
106 gst_element_class_set_details_simple (element_class, "FIXME",
107 "Generic", "FIXME", "David Schleef <ds@schleef.org>");
111 gst_chop_my_data_class_init (GstChopMyDataClass * klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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);
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));
136 gst_chop_my_data_init (GstChopMyData * chopmydata,
137 GstChopMyDataClass * chopmydata_class)
140 chopmydata->sinkpad =
141 gst_pad_new_from_static_template (&gst_chop_my_data_sink_template,
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);
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);
159 chopmydata->step_size = DEFAULT_STEP_SIZE;
160 chopmydata->min_size = DEFAULT_MIN_SIZE;
161 chopmydata->max_size = DEFAULT_MAX_SIZE;
165 gst_chop_my_data_set_property (GObject * object, guint property_id,
166 const GValue * value, GParamSpec * pspec)
168 GstChopMyData *chopmydata;
170 g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
171 chopmydata = GST_CHOP_MY_DATA (object);
173 switch (property_id) {
175 chopmydata->max_size = g_value_get_int (value);
178 chopmydata->min_size = g_value_get_int (value);
181 chopmydata->step_size = g_value_get_int (value);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
190 gst_chop_my_data_get_property (GObject * object, guint property_id,
191 GValue * value, GParamSpec * pspec)
193 GstChopMyData *chopmydata;
195 g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
196 chopmydata = GST_CHOP_MY_DATA (object);
198 switch (property_id) {
200 g_value_set_int (value, chopmydata->max_size);
203 g_value_set_int (value, chopmydata->min_size);
206 g_value_set_int (value, chopmydata->step_size);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
214 static GstStateChangeReturn
215 gst_chop_my_data_change_state (GstElement * element, GstStateChange transition)
217 GstChopMyData *chopmydata;
218 GstStateChangeReturn ret;
220 chopmydata = GST_CHOP_MY_DATA (element);
222 switch (transition) {
223 case GST_STATE_CHANGE_NULL_TO_READY:
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);
232 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
238 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
240 switch (transition) {
241 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
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);
250 case GST_STATE_CHANGE_READY_TO_NULL:
260 get_next_size (GstChopMyData * chopmydata)
265 begin = (chopmydata->min_size + chopmydata->step_size - 1) /
266 chopmydata->step_size;
267 end = (chopmydata->max_size + chopmydata->step_size) / chopmydata->step_size;
270 chopmydata->next_size = begin * chopmydata->step_size;
274 chopmydata->next_size =
275 g_rand_int_range (chopmydata->rand, begin, end) * chopmydata->step_size;
279 gst_chop_my_data_process (GstChopMyData * chopmydata, gboolean flush)
281 GstFlowReturn ret = GST_FLOW_OK;
284 if (chopmydata->next_size == 0) {
285 get_next_size (chopmydata);
288 while (gst_adapter_available (chopmydata->adapter) >= chopmydata->next_size) {
290 gst_adapter_take_buffer (chopmydata->adapter, chopmydata->next_size);
292 chopmydata->next_size = 0;
294 ret = gst_pad_push (chopmydata->srcpad, buffer);
295 if (ret != GST_FLOW_OK) {
299 get_next_size (chopmydata);
303 guint min_size = chopmydata->min_size;
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)
311 gst_adapter_clear (chopmydata->adapter);
318 gst_chop_my_data_chain (GstPad * pad, GstBuffer * buffer)
320 GstChopMyData *chopmydata;
323 chopmydata = GST_CHOP_MY_DATA (gst_pad_get_parent (pad));
325 GST_DEBUG_OBJECT (chopmydata, "chain");
327 gst_adapter_push (chopmydata->adapter, buffer);
328 ret = gst_chop_my_data_process (chopmydata, FALSE);
330 gst_object_unref (chopmydata);
335 gst_chop_my_data_sink_event (GstPad * pad, GstEvent * event)
338 GstChopMyData *chopmydata;
340 chopmydata = GST_CHOP_MY_DATA (gst_pad_get_parent (pad));
342 GST_DEBUG_OBJECT (chopmydata, "event");
344 switch (GST_EVENT_TYPE (event)) {
345 case GST_EVENT_FLUSH_START:
346 res = gst_pad_push_event (chopmydata->srcpad, event);
348 case GST_EVENT_FLUSH_STOP:
349 gst_adapter_clear (chopmydata->adapter);
350 res = gst_pad_push_event (chopmydata->srcpad, event);
352 case GST_EVENT_NEWSEGMENT:
353 res = gst_pad_push_event (chopmydata->srcpad, event);
356 gst_chop_my_data_process (chopmydata, TRUE);
357 res = gst_pad_push_event (chopmydata->srcpad, event);
360 res = gst_pad_push_event (chopmydata->srcpad, event);
364 gst_object_unref (chopmydata);
369 gst_chop_my_data_src_event (GstPad * pad, GstEvent * event)
372 GstChopMyData *chopmydata;
374 chopmydata = GST_CHOP_MY_DATA (gst_pad_get_parent (pad));
376 GST_DEBUG_OBJECT (chopmydata, "event");
378 switch (GST_EVENT_TYPE (event)) {
380 res = gst_pad_push_event (chopmydata->sinkpad, event);
383 res = gst_pad_push_event (chopmydata->sinkpad, event);
387 gst_object_unref (chopmydata);