• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:element-chopmydata
21  * @title: gstchopmydata
22  *
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.
26  *
27  * ## Example launch line
28  * |[
29  * gst-launch-1.0 -v audiotestsrc num-buffers=10 ! chopmydata 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  */
41 
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 
46 #include <gst/gst.h>
47 #include <gst/gst.h>
48 #include "gstdebugutilsbadelements.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, GstObject * parent,
63     GstBuffer * buffer);
64 static gboolean gst_chop_my_data_sink_event (GstPad * pad, GstObject * parent,
65     GstEvent * event);
66 static gboolean gst_chop_my_data_src_event (GstPad * pad, GstObject * parent,
67     GstEvent * event);
68 
69 #define DEFAULT_MAX_SIZE 4096
70 #define DEFAULT_MIN_SIZE 1
71 #define DEFAULT_STEP_SIZE 1
72 
73 enum
74 {
75   PROP_0,
76   PROP_MAX_SIZE,
77   PROP_MIN_SIZE,
78   PROP_STEP_SIZE
79 };
80 
81 /* pad templates */
82 
83 static GstStaticPadTemplate gst_chop_my_data_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS_ANY);
88 
89 static GstStaticPadTemplate gst_chop_my_data_src_template =
90 GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS_ANY);
94 
95 /* class initialization */
96 
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 ());
101 
102 static void
gst_chop_my_data_class_init(GstChopMyDataClass * klass)103 gst_chop_my_data_class_init (GstChopMyDataClass * klass)
104 {
105   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107 
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);
112 
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));
125 
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);
130 
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>");
134 }
135 
136 static void
gst_chop_my_data_init(GstChopMyData * chopmydata)137 gst_chop_my_data_init (GstChopMyData * chopmydata)
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_PROXY_CAPS (chopmydata->sinkpad);
148   gst_element_add_pad (GST_ELEMENT (chopmydata), chopmydata->sinkpad);
149 
150   chopmydata->srcpad =
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);
156 
157   chopmydata->step_size = DEFAULT_STEP_SIZE;
158   chopmydata->min_size = DEFAULT_MIN_SIZE;
159   chopmydata->max_size = DEFAULT_MAX_SIZE;
160 }
161 
162 void
gst_chop_my_data_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)163 gst_chop_my_data_set_property (GObject * object, guint property_id,
164     const GValue * value, GParamSpec * pspec)
165 {
166   GstChopMyData *chopmydata;
167 
168   g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
169   chopmydata = GST_CHOP_MY_DATA (object);
170 
171   switch (property_id) {
172     case PROP_MAX_SIZE:
173       chopmydata->max_size = g_value_get_int (value);
174       break;
175     case PROP_MIN_SIZE:
176       chopmydata->min_size = g_value_get_int (value);
177       break;
178     case PROP_STEP_SIZE:
179       chopmydata->step_size = g_value_get_int (value);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
183       break;
184   }
185 }
186 
187 void
gst_chop_my_data_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)188 gst_chop_my_data_get_property (GObject * object, guint property_id,
189     GValue * value, GParamSpec * pspec)
190 {
191   GstChopMyData *chopmydata;
192 
193   g_return_if_fail (GST_IS_CHOP_MY_DATA (object));
194   chopmydata = GST_CHOP_MY_DATA (object);
195 
196   switch (property_id) {
197     case PROP_MAX_SIZE:
198       g_value_set_int (value, chopmydata->max_size);
199       break;
200     case PROP_MIN_SIZE:
201       g_value_set_int (value, chopmydata->min_size);
202       break;
203     case PROP_STEP_SIZE:
204       g_value_set_int (value, chopmydata->step_size);
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
208       break;
209   }
210 }
211 
212 static GstStateChangeReturn
gst_chop_my_data_change_state(GstElement * element,GstStateChange transition)213 gst_chop_my_data_change_state (GstElement * element, GstStateChange transition)
214 {
215   GstChopMyData *chopmydata;
216   GstStateChangeReturn ret;
217 
218   chopmydata = GST_CHOP_MY_DATA (element);
219 
220   switch (transition) {
221     case GST_STATE_CHANGE_NULL_TO_READY:
222       break;
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);
229       break;
230     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
231       break;
232     default:
233       break;
234   }
235 
236   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
237 
238   switch (transition) {
239     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
240       break;
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);
247       break;
248     case GST_STATE_CHANGE_READY_TO_NULL:
249       break;
250     default:
251       break;
252   }
253 
254   return ret;
255 }
256 
257 static void
get_next_size(GstChopMyData * chopmydata)258 get_next_size (GstChopMyData * chopmydata)
259 {
260   int begin;
261   int end;
262 
263   begin = (chopmydata->min_size + chopmydata->step_size - 1) /
264       chopmydata->step_size;
265   end = (chopmydata->max_size + chopmydata->step_size) / chopmydata->step_size;
266 
267   if (begin >= end) {
268     chopmydata->next_size = begin * chopmydata->step_size;
269     return;
270   }
271 
272   chopmydata->next_size =
273       g_rand_int_range (chopmydata->rand, begin, end) * chopmydata->step_size;
274 }
275 
276 static GstFlowReturn
gst_chop_my_data_process(GstChopMyData * chopmydata,gboolean flush)277 gst_chop_my_data_process (GstChopMyData * chopmydata, gboolean flush)
278 {
279   GstFlowReturn ret = GST_FLOW_OK;
280   GstBuffer *buffer;
281 
282   if (chopmydata->next_size == 0) {
283     get_next_size (chopmydata);
284   }
285 
286   while (gst_adapter_available (chopmydata->adapter) >= chopmydata->next_size) {
287     buffer =
288         gst_adapter_take_buffer (chopmydata->adapter, chopmydata->next_size);
289 
290     GST_BUFFER_PTS (buffer) = gst_adapter_prev_pts (chopmydata->adapter, NULL);
291     GST_BUFFER_DTS (buffer) = gst_adapter_prev_dts (chopmydata->adapter, NULL);
292 
293     chopmydata->next_size = 0;
294 
295     ret = gst_pad_push (chopmydata->srcpad, buffer);
296     if (ret != GST_FLOW_OK) {
297       return ret;
298     }
299 
300     get_next_size (chopmydata);
301   }
302 
303   if (flush) {
304     guint min_size = chopmydata->min_size;
305 
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)
310         break;
311     }
312     gst_adapter_clear (chopmydata->adapter);
313   }
314 
315   return ret;
316 }
317 
318 static GstFlowReturn
gst_chop_my_data_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)319 gst_chop_my_data_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
320 {
321   GstChopMyData *chopmydata;
322   GstFlowReturn ret;
323 
324   chopmydata = GST_CHOP_MY_DATA (parent);
325 
326   GST_DEBUG_OBJECT (chopmydata, "chain");
327 
328   gst_adapter_push (chopmydata->adapter, buffer);
329   ret = gst_chop_my_data_process (chopmydata, FALSE);
330 
331   return ret;
332 }
333 
334 static gboolean
gst_chop_my_data_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)335 gst_chop_my_data_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
336 {
337   gboolean res;
338   GstChopMyData *chopmydata;
339 
340   chopmydata = GST_CHOP_MY_DATA (parent);
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_SEGMENT:
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   return res;
365 }
366 
367 static gboolean
gst_chop_my_data_src_event(GstPad * pad,GstObject * parent,GstEvent * event)368 gst_chop_my_data_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
369 {
370   gboolean res;
371   GstChopMyData *chopmydata;
372 
373   chopmydata = GST_CHOP_MY_DATA (parent);
374 
375   GST_DEBUG_OBJECT (chopmydata, "event");
376 
377   switch (GST_EVENT_TYPE (event)) {
378     case GST_EVENT_SEEK:
379       res = gst_pad_push_event (chopmydata->sinkpad, event);
380       break;
381     default:
382       res = gst_pad_push_event (chopmydata->sinkpad, event);
383       break;
384   }
385 
386   return res;
387 }
388