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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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))
48 GType gst_break_my_data_get_type (void);
59 typedef struct _GstBreakMyData GstBreakMyData;
60 typedef struct _GstBreakMyDataClass GstBreakMyDataClass;
62 struct _GstBreakMyData
64 GstBaseTransform basetransform;
75 struct _GstBreakMyDataClass
77 GstBaseTransformClass parent_class;
80 static void gst_break_my_data_set_property (GObject * object,
81 guint prop_id, const GValue * value, GParamSpec * pspec);
82 static void gst_break_my_data_get_property (GObject * object,
83 guint prop_id, GValue * value, GParamSpec * pspec);
85 static GstFlowReturn gst_break_my_data_transform_ip (GstBaseTransform * trans,
87 static gboolean gst_break_my_data_stop (GstBaseTransform * trans);
88 static gboolean gst_break_my_data_start (GstBaseTransform * trans);
90 GstStaticPadTemplate bmd_src_template = GST_STATIC_PAD_TEMPLATE ("src",
95 GstStaticPadTemplate bmd_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
100 #define gst_break_my_data_parent_class parent_class
101 G_DEFINE_TYPE (GstBreakMyData, gst_break_my_data, GST_TYPE_BASE_TRANSFORM);
105 gst_break_my_data_class_init (GstBreakMyDataClass * klass)
107 GstBaseTransformClass *gstbasetrans_class;
108 GstElementClass *gstelement_class;
109 GObjectClass *gobject_class;
111 gobject_class = G_OBJECT_CLASS (klass);
112 gstelement_class = GST_ELEMENT_CLASS (klass);
113 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
115 GST_DEBUG_CATEGORY_INIT (gst_break_my_data_debug, "breakmydata", 0,
116 "debugging category for breakmydata element");
118 gobject_class->set_property = gst_break_my_data_set_property;
119 gobject_class->get_property = gst_break_my_data_get_property;
121 g_object_class_install_property (gobject_class, PROP_SEED,
122 g_param_spec_uint ("seed", "seed",
123 "seed for randomness (initialized when going from READY to PAUSED)",
125 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
126 g_object_class_install_property (gobject_class, PROP_SET_TO,
127 g_param_spec_int ("set-to", "set-to",
128 "set changed bytes to this value (-1 means random value",
130 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
131 g_object_class_install_property (gobject_class, PROP_SKIP,
132 g_param_spec_uint ("skip", "skip",
133 "amount of bytes skipped at the beginning of stream",
135 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
136 g_object_class_install_property (gobject_class, PROP_PROBABILITY,
137 g_param_spec_double ("probability", "probability",
138 "probability for each byte in the buffer to be changed", 0.0, 1.0,
139 0.0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
141 gst_element_class_add_static_pad_template (gstelement_class,
143 gst_element_class_add_static_pad_template (gstelement_class,
146 gst_element_class_set_static_metadata (gstelement_class, "Break my data",
148 "randomly change data in the stream", "Benjamin Otte <otte@gnome>");
150 gstbasetrans_class->transform_ip =
151 GST_DEBUG_FUNCPTR (gst_break_my_data_transform_ip);
152 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_break_my_data_start);
153 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_break_my_data_stop);
157 gst_break_my_data_init (GstBreakMyData * bmd)
159 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (bmd), TRUE);
163 gst_break_my_data_set_property (GObject * object, guint prop_id,
164 const GValue * value, GParamSpec * pspec)
166 GstBreakMyData *bmd = GST_BREAK_MY_DATA (object);
168 GST_OBJECT_LOCK (bmd);
172 bmd->seed = g_value_get_uint (value);
175 bmd->set = g_value_get_int (value);
178 bmd->skip = g_value_get_uint (value);
180 case PROP_PROBABILITY:
181 bmd->probability = g_value_get_double (value);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 GST_OBJECT_UNLOCK (bmd);
192 gst_break_my_data_get_property (GObject * object, guint prop_id, GValue * value,
195 GstBreakMyData *bmd = GST_BREAK_MY_DATA (object);
197 GST_OBJECT_LOCK (bmd);
201 g_value_set_uint (value, bmd->seed);
204 g_value_set_int (value, bmd->set);
207 g_value_set_uint (value, bmd->skip);
209 case PROP_PROBABILITY:
210 g_value_set_double (value, bmd->probability);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 GST_OBJECT_UNLOCK (bmd);
221 gst_break_my_data_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
223 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
227 g_return_val_if_fail (gst_buffer_is_writable (buf), GST_FLOW_ERROR);
229 GST_OBJECT_LOCK (bmd);
231 if (bmd->skipped < bmd->skip) {
232 i = bmd->skip - bmd->skipped;
237 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
240 "got buffer %p (size %" G_GSIZE_FORMAT ", timestamp %" G_GUINT64_FORMAT
241 ", offset %" G_GUINT64_FORMAT "", buf, map.size,
242 GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_OFFSET (buf));
244 for (; i < map.size; i++) {
245 if (g_rand_double_range (bmd->rand, 0, 1.0) <= bmd->probability) {
249 new = g_rand_int_range (bmd->rand, 0, 256);
253 GST_INFO_OBJECT (bmd,
254 "changing byte %" G_GSIZE_FORMAT " from 0x%02X to 0x%02X", i,
255 (guint) GST_READ_UINT8 (map.data + i), (guint) ((guint8) new));
260 bmd->skipped += MIN (G_MAXUINT - bmd->skipped, map.size);
262 gst_buffer_unmap (buf, &map);
264 GST_OBJECT_UNLOCK (bmd);
270 gst_break_my_data_start (GstBaseTransform * trans)
272 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
274 GST_OBJECT_LOCK (bmd);
275 bmd->rand = g_rand_new_with_seed (bmd->seed);
277 GST_OBJECT_UNLOCK (bmd);
283 gst_break_my_data_stop (GstBaseTransform * trans)
285 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
287 GST_OBJECT_LOCK (bmd);
288 g_rand_free (bmd->rand);
290 GST_OBJECT_UNLOCK (bmd);