2 * Copyright (C) 2004 Benjamin Otte <otte@gnome.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-breakmydata
22 * This element modifies the contents of the buffer it is passed randomly
23 * according to the parameters set.
24 * It otherwise acts as an identity.
32 #include <gst/base/gstbasetransform.h>
34 GST_DEBUG_CATEGORY_STATIC (gst_break_my_data_debug);
35 #define GST_CAT_DEFAULT gst_break_my_data_debug
37 #define GST_TYPE_BREAK_MY_DATA \
38 (gst_break_my_data_get_type())
39 #define GST_BREAK_MY_DATA(obj) \
40 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BREAK_MY_DATA,GstBreakMyData))
41 #define GST_BREAK_MY_DATA_CLASS(klass) \
42 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BREAK_MY_DATA,GstBreakMyDataClass))
43 #define GST_IS_BREAK_MY_DATA(obj) \
44 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BREAK_MY_DATA))
45 #define GST_IS_BREAK_MY_DATA_CLASS(klass) \
46 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BREAK_MY_DATA))
57 typedef struct _GstBreakMyData GstBreakMyData;
58 typedef struct _GstBreakMyDataClass GstBreakMyDataClass;
60 struct _GstBreakMyData
62 GstBaseTransform basetransform;
73 struct _GstBreakMyDataClass
75 GstBaseTransformClass parent_class;
78 static void gst_break_my_data_set_property (GObject * object,
79 guint prop_id, const GValue * value, GParamSpec * pspec);
80 static void gst_break_my_data_get_property (GObject * object,
81 guint prop_id, GValue * value, GParamSpec * pspec);
83 static GstFlowReturn gst_break_my_data_transform_ip (GstBaseTransform * trans,
85 static gboolean gst_break_my_data_stop (GstBaseTransform * trans);
86 static gboolean gst_break_my_data_start (GstBaseTransform * trans);
88 GstStaticPadTemplate bmd_src_template = GST_STATIC_PAD_TEMPLATE ("src",
93 GstStaticPadTemplate bmd_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
99 #define DEBUG_INIT(bla) \
100 GST_DEBUG_CATEGORY_INIT (gst_break_my_data_debug, "breakmydata", 0, \
101 "debugging category for breakmydata element");
103 GType gst_break_my_data_get_type (void);
104 GST_BOILERPLATE_FULL (GstBreakMyData, gst_break_my_data, GstBaseTransform,
105 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
109 gst_break_my_data_base_init (gpointer g_class)
111 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
113 gst_element_class_add_static_pad_template (gstelement_class,
115 gst_element_class_add_static_pad_template (gstelement_class,
118 gst_element_class_set_details_simple (gstelement_class, "Break my data",
120 "randomly change data in the stream", "Benjamin Otte <otte@gnome>");
124 gst_break_my_data_class_init (GstBreakMyDataClass * klass)
126 GstBaseTransformClass *gstbasetrans_class;
127 GObjectClass *gobject_class;
129 gobject_class = G_OBJECT_CLASS (klass);
130 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
132 gobject_class->set_property = gst_break_my_data_set_property;
133 gobject_class->get_property = gst_break_my_data_get_property;
135 g_object_class_install_property (gobject_class, ARG_SEED,
136 g_param_spec_uint ("seed", "seed",
137 "seed for randomness (initialized when going from READY to PAUSED)",
139 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
140 g_object_class_install_property (gobject_class, ARG_SET_TO,
141 g_param_spec_int ("set-to", "set-to",
142 "set changed bytes to this value (-1 means random value",
144 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
145 g_object_class_install_property (gobject_class, ARG_SKIP,
146 g_param_spec_uint ("skip", "skip",
147 "amount of bytes skipped at the beginning of stream",
149 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
150 g_object_class_install_property (gobject_class, ARG_PROBABILITY,
151 g_param_spec_double ("probability", "probability",
152 "probability for each byte in the buffer to be changed", 0.0, 1.0,
153 0.0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
155 gstbasetrans_class->transform_ip =
156 GST_DEBUG_FUNCPTR (gst_break_my_data_transform_ip);
157 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_break_my_data_start);
158 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_break_my_data_stop);
162 gst_break_my_data_init (GstBreakMyData * bmd, GstBreakMyDataClass * g_class)
164 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (bmd), TRUE);
168 gst_break_my_data_set_property (GObject * object, guint prop_id,
169 const GValue * value, GParamSpec * pspec)
171 GstBreakMyData *bmd = GST_BREAK_MY_DATA (object);
173 GST_OBJECT_LOCK (bmd);
177 bmd->seed = g_value_get_uint (value);
180 bmd->set = g_value_get_int (value);
183 bmd->skip = g_value_get_uint (value);
185 case ARG_PROBABILITY:
186 bmd->probability = g_value_get_double (value);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 GST_OBJECT_UNLOCK (bmd);
197 gst_break_my_data_get_property (GObject * object, guint prop_id, GValue * value,
200 GstBreakMyData *bmd = GST_BREAK_MY_DATA (object);
202 GST_OBJECT_LOCK (bmd);
206 g_value_set_uint (value, bmd->seed);
209 g_value_set_int (value, bmd->set);
212 g_value_set_uint (value, bmd->skip);
214 case ARG_PROBABILITY:
215 g_value_set_double (value, bmd->probability);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 GST_OBJECT_UNLOCK (bmd);
226 gst_break_my_data_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
228 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
231 g_return_val_if_fail (gst_buffer_is_writable (buf), GST_FLOW_ERROR);
233 GST_OBJECT_LOCK (bmd);
235 if (bmd->skipped < bmd->skip) {
236 i = bmd->skip - bmd->skipped;
241 size = GST_BUFFER_SIZE (buf);
244 "got buffer %p (size %u, timestamp %" G_GUINT64_FORMAT ", offset %"
245 G_GUINT64_FORMAT "", buf, size, GST_BUFFER_TIMESTAMP (buf),
246 GST_BUFFER_OFFSET (buf));
248 for (; i < size; i++) {
249 if (g_rand_double_range (bmd->rand, 0, 1.0) <= bmd->probability) {
253 new = g_rand_int_range (bmd->rand, 0, 256);
257 GST_INFO_OBJECT (bmd, "changing byte %u from 0x%02X to 0x%02X", i,
258 (guint) GST_READ_UINT8 (GST_BUFFER_DATA (buf) + i),
259 (guint) ((guint8) new));
260 GST_BUFFER_DATA (buf)[i] = new;
264 bmd->skipped += MIN (G_MAXUINT - bmd->skipped, GST_BUFFER_SIZE (buf));
266 GST_OBJECT_UNLOCK (bmd);
272 gst_break_my_data_start (GstBaseTransform * trans)
274 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
276 GST_OBJECT_LOCK (bmd);
277 bmd->rand = g_rand_new_with_seed (bmd->seed);
279 GST_OBJECT_UNLOCK (bmd);
285 gst_break_my_data_stop (GstBaseTransform * trans)
287 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
289 GST_OBJECT_LOCK (bmd);
290 g_rand_free (bmd->rand);
292 GST_OBJECT_UNLOCK (bmd);