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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 * SECTION:element-chopmydata
21 * @title: gstchopmydata
23 * The chopmydata element takes an incoming stream and chops it up
24 * into randomly sized buffers. Size of outgoing buffers are determined
25 * by the max-size, min-size, and step-size properties.
27 * ## Example launch line
29 * gst-launch-1.0 -v audiotestsrc num-buffers=10 ! chopmydata 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
48 #include "gstdebugutilsbadelements.h"
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, GstObject * parent,
64 static gboolean gst_chop_my_data_sink_event (GstPad * pad, GstObject * parent,
66 static gboolean gst_chop_my_data_src_event (GstPad * pad, GstObject * parent,
69 #define DEFAULT_MAX_SIZE 4096
70 #define DEFAULT_MIN_SIZE 1
71 #define DEFAULT_STEP_SIZE 1
83 static GstStaticPadTemplate gst_chop_my_data_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("sink",
89 static GstStaticPadTemplate gst_chop_my_data_src_template =
90 GST_STATIC_PAD_TEMPLATE ("src",
95 /* class initialization */
97 #define gst_chop_my_data_parent_class parent_class
98 G_DEFINE_TYPE (GstChopMyData, gst_chop_my_data, GST_TYPE_ELEMENT);
99 GST_ELEMENT_REGISTER_DEFINE (chopmydata, "chopmydata",
100 GST_RANK_NONE, gst_chop_my_data_get_type ());
103 gst_chop_my_data_class_init (GstChopMyDataClass * klass)
105 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108 gobject_class->set_property = gst_chop_my_data_set_property;
109 gobject_class->get_property = gst_chop_my_data_get_property;
110 element_class->change_state =
111 GST_DEBUG_FUNCPTR (gst_chop_my_data_change_state);
113 g_object_class_install_property (gobject_class, PROP_MAX_SIZE,
114 g_param_spec_int ("max-size", "max-size",
115 "Maximum size of outgoing buffers", 1, G_MAXINT,
116 DEFAULT_MAX_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117 g_object_class_install_property (gobject_class, PROP_MIN_SIZE,
118 g_param_spec_int ("min-size", "max-size",
119 "Minimum size of outgoing buffers", 1, G_MAXINT,
120 DEFAULT_MIN_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121 g_object_class_install_property (gobject_class, PROP_STEP_SIZE,
122 g_param_spec_int ("step-size", "step-size",
123 "Step increment for random buffer sizes", 1, G_MAXINT,
124 DEFAULT_MAX_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 gst_element_class_add_static_pad_template (element_class,
127 &gst_chop_my_data_src_template);
128 gst_element_class_add_static_pad_template (element_class,
129 &gst_chop_my_data_sink_template);
131 gst_element_class_set_static_metadata (element_class, "Chop my data",
132 "Generic", "Split up a stream into randomly-sized buffers",
133 "David Schleef <ds@schleef.org>");
137 gst_chop_my_data_init (GstChopMyData * chopmydata)
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_PROXY_CAPS (chopmydata->sinkpad);
148 gst_element_add_pad (GST_ELEMENT (chopmydata), chopmydata->sinkpad);
151 gst_pad_new_from_static_template (&gst_chop_my_data_src_template, "src");
152 gst_pad_set_event_function (chopmydata->srcpad,
153 GST_DEBUG_FUNCPTR (gst_chop_my_data_src_event));
154 GST_PAD_SET_PROXY_CAPS (chopmydata->srcpad);
155 gst_element_add_pad (GST_ELEMENT (chopmydata), chopmydata->srcpad);
157 chopmydata->step_size = DEFAULT_STEP_SIZE;
158 chopmydata->min_size = DEFAULT_MIN_SIZE;
159 chopmydata->max_size = DEFAULT_MAX_SIZE;
163 gst_chop_my_data_set_property (GObject * object, guint property_id,
164 const GValue * value, GParamSpec * pspec)
166 GstChopMyData *chopmydata;
168 g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
169 chopmydata = GST_CHOP_MY_DATA (object);
171 switch (property_id) {
173 chopmydata->max_size = g_value_get_int (value);
176 chopmydata->min_size = g_value_get_int (value);
179 chopmydata->step_size = g_value_get_int (value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
188 gst_chop_my_data_get_property (GObject * object, guint property_id,
189 GValue * value, GParamSpec * pspec)
191 GstChopMyData *chopmydata;
193 g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
194 chopmydata = GST_CHOP_MY_DATA (object);
196 switch (property_id) {
198 g_value_set_int (value, chopmydata->max_size);
201 g_value_set_int (value, chopmydata->min_size);
204 g_value_set_int (value, chopmydata->step_size);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
212 static GstStateChangeReturn
213 gst_chop_my_data_change_state (GstElement * element, GstStateChange transition)
215 GstChopMyData *chopmydata;
216 GstStateChangeReturn ret;
218 chopmydata = GST_CHOP_MY_DATA (element);
220 switch (transition) {
221 case GST_STATE_CHANGE_NULL_TO_READY:
223 case GST_STATE_CHANGE_READY_TO_PAUSED:
224 GST_OBJECT_LOCK (chopmydata);
225 chopmydata->adapter = gst_adapter_new ();
226 chopmydata->rand = g_rand_new ();
227 chopmydata->next_size = 0;
228 GST_OBJECT_UNLOCK (chopmydata);
230 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
236 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
238 switch (transition) {
239 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
241 case GST_STATE_CHANGE_PAUSED_TO_READY:
242 GST_OBJECT_LOCK (chopmydata);
243 g_object_unref (chopmydata->adapter);
244 chopmydata->adapter = NULL;
245 g_rand_free (chopmydata->rand);
246 GST_OBJECT_UNLOCK (chopmydata);
248 case GST_STATE_CHANGE_READY_TO_NULL:
258 get_next_size (GstChopMyData * chopmydata)
263 begin = (chopmydata->min_size + chopmydata->step_size - 1) /
264 chopmydata->step_size;
265 end = (chopmydata->max_size + chopmydata->step_size) / chopmydata->step_size;
268 chopmydata->next_size = begin * chopmydata->step_size;
272 chopmydata->next_size =
273 g_rand_int_range (chopmydata->rand, begin, end) * chopmydata->step_size;
277 gst_chop_my_data_process (GstChopMyData * chopmydata, gboolean flush)
279 GstFlowReturn ret = GST_FLOW_OK;
282 if (chopmydata->next_size == 0) {
283 get_next_size (chopmydata);
286 while (gst_adapter_available (chopmydata->adapter) >= chopmydata->next_size) {
288 gst_adapter_take_buffer (chopmydata->adapter, chopmydata->next_size);
290 GST_BUFFER_PTS (buffer) = gst_adapter_prev_pts (chopmydata->adapter, NULL);
291 GST_BUFFER_DTS (buffer) = gst_adapter_prev_dts (chopmydata->adapter, NULL);
293 chopmydata->next_size = 0;
295 ret = gst_pad_push (chopmydata->srcpad, buffer);
296 if (ret != GST_FLOW_OK) {
300 get_next_size (chopmydata);
304 guint min_size = chopmydata->min_size;
306 while (gst_adapter_available (chopmydata->adapter) >= min_size) {
307 buffer = gst_adapter_take_buffer (chopmydata->adapter, min_size);
308 ret = gst_pad_push (chopmydata->srcpad, buffer);
309 if (ret != GST_FLOW_OK)
312 gst_adapter_clear (chopmydata->adapter);
319 gst_chop_my_data_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
321 GstChopMyData *chopmydata;
324 chopmydata = GST_CHOP_MY_DATA (parent);
326 GST_DEBUG_OBJECT (chopmydata, "chain");
328 gst_adapter_push (chopmydata->adapter, buffer);
329 ret = gst_chop_my_data_process (chopmydata, FALSE);
335 gst_chop_my_data_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
338 GstChopMyData *chopmydata;
340 chopmydata = GST_CHOP_MY_DATA (parent);
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_SEGMENT:
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);
368 gst_chop_my_data_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
371 GstChopMyData *chopmydata;
373 chopmydata = GST_CHOP_MY_DATA (parent);
375 GST_DEBUG_OBJECT (chopmydata, "event");
377 switch (GST_EVENT_TYPE (event)) {
379 res = gst_pad_push_event (chopmydata->sinkpad, event);
382 res = gst_pad_push_event (chopmydata->sinkpad, event);