1 /* GStreamer
2 * Copyright (C) 2004 Benjamin Otte <otte@gnome.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-breakmydata
21 *
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.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <gst/gst.h>
32 #include <gst/base/gstbasetransform.h>
33
34 GST_DEBUG_CATEGORY_STATIC (gst_break_my_data_debug);
35 #define GST_CAT_DEFAULT gst_break_my_data_debug
36
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))
47
48 GType gst_break_my_data_get_type (void);
49
50 enum
51 {
52 PROP_0,
53 PROP_SEED,
54 PROP_SET_TO,
55 PROP_SKIP,
56 PROP_PROBABILITY
57 };
58
59 typedef struct _GstBreakMyData GstBreakMyData;
60 typedef struct _GstBreakMyDataClass GstBreakMyDataClass;
61
62 struct _GstBreakMyData
63 {
64 GstBaseTransform basetransform;
65
66 GRand *rand;
67 guint skipped;
68
69 guint32 seed;
70 gint set;
71 guint skip;
72 gdouble probability;
73 };
74
75 struct _GstBreakMyDataClass
76 {
77 GstBaseTransformClass parent_class;
78 };
79
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);
84
85 static GstFlowReturn gst_break_my_data_transform_ip (GstBaseTransform * trans,
86 GstBuffer * buf);
87 static gboolean gst_break_my_data_stop (GstBaseTransform * trans);
88 static gboolean gst_break_my_data_start (GstBaseTransform * trans);
89
90 GstStaticPadTemplate bmd_src_template = GST_STATIC_PAD_TEMPLATE ("src",
91 GST_PAD_SRC,
92 GST_PAD_ALWAYS,
93 GST_STATIC_CAPS_ANY);
94
95 GstStaticPadTemplate bmd_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_PAD_SINK,
97 GST_PAD_ALWAYS,
98 GST_STATIC_CAPS_ANY);
99
100 #define gst_break_my_data_parent_class parent_class
101 G_DEFINE_TYPE (GstBreakMyData, gst_break_my_data, GST_TYPE_BASE_TRANSFORM);
102
103
104 static void
gst_break_my_data_class_init(GstBreakMyDataClass * klass)105 gst_break_my_data_class_init (GstBreakMyDataClass * klass)
106 {
107 GstBaseTransformClass *gstbasetrans_class;
108 GstElementClass *gstelement_class;
109 GObjectClass *gobject_class;
110
111 gobject_class = G_OBJECT_CLASS (klass);
112 gstelement_class = GST_ELEMENT_CLASS (klass);
113 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
114
115 GST_DEBUG_CATEGORY_INIT (gst_break_my_data_debug, "breakmydata", 0,
116 "debugging category for breakmydata element");
117
118 gobject_class->set_property = gst_break_my_data_set_property;
119 gobject_class->get_property = gst_break_my_data_get_property;
120
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)",
124 0, G_MAXUINT32, 0,
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",
129 -1, G_MAXUINT8, -1,
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",
134 0, G_MAXUINT, 0,
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));
140
141 gst_element_class_add_static_pad_template (gstelement_class,
142 &bmd_sink_template);
143 gst_element_class_add_static_pad_template (gstelement_class,
144 &bmd_src_template);
145
146 gst_element_class_set_static_metadata (gstelement_class, "Break my data",
147 "Testing",
148 "randomly change data in the stream", "Benjamin Otte <otte@gnome>");
149
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);
154 }
155
156 static void
gst_break_my_data_init(GstBreakMyData * bmd)157 gst_break_my_data_init (GstBreakMyData * bmd)
158 {
159 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (bmd), TRUE);
160 }
161
162 static void
gst_break_my_data_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)163 gst_break_my_data_set_property (GObject * object, guint prop_id,
164 const GValue * value, GParamSpec * pspec)
165 {
166 GstBreakMyData *bmd = GST_BREAK_MY_DATA (object);
167
168 GST_OBJECT_LOCK (bmd);
169
170 switch (prop_id) {
171 case PROP_SEED:
172 bmd->seed = g_value_get_uint (value);
173 break;
174 case PROP_SET_TO:
175 bmd->set = g_value_get_int (value);
176 break;
177 case PROP_SKIP:
178 bmd->skip = g_value_get_uint (value);
179 break;
180 case PROP_PROBABILITY:
181 bmd->probability = g_value_get_double (value);
182 break;
183 default:
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 break;
186 }
187
188 GST_OBJECT_UNLOCK (bmd);
189 }
190
191 static void
gst_break_my_data_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)192 gst_break_my_data_get_property (GObject * object, guint prop_id, GValue * value,
193 GParamSpec * pspec)
194 {
195 GstBreakMyData *bmd = GST_BREAK_MY_DATA (object);
196
197 GST_OBJECT_LOCK (bmd);
198
199 switch (prop_id) {
200 case PROP_SEED:
201 g_value_set_uint (value, bmd->seed);
202 break;
203 case PROP_SET_TO:
204 g_value_set_int (value, bmd->set);
205 break;
206 case PROP_SKIP:
207 g_value_set_uint (value, bmd->skip);
208 break;
209 case PROP_PROBABILITY:
210 g_value_set_double (value, bmd->probability);
211 break;
212 default:
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 break;
215 }
216
217 GST_OBJECT_UNLOCK (bmd);
218 }
219
220 static GstFlowReturn
gst_break_my_data_transform_ip(GstBaseTransform * trans,GstBuffer * buf)221 gst_break_my_data_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
222 {
223 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
224 GstMapInfo map;
225 gsize i;
226
227 g_return_val_if_fail (gst_buffer_is_writable (buf), GST_FLOW_ERROR);
228
229 GST_OBJECT_LOCK (bmd);
230
231 if (bmd->skipped < bmd->skip) {
232 i = bmd->skip - bmd->skipped;
233 } else {
234 i = 0;
235 }
236
237 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
238
239 GST_LOG_OBJECT (bmd,
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));
243
244 for (; i < map.size; i++) {
245 if (g_rand_double_range (bmd->rand, 0, 1.0) <= bmd->probability) {
246 guint8 new;
247
248 if (bmd->set < 0) {
249 new = g_rand_int_range (bmd->rand, 0, 256);
250 } else {
251 new = bmd->set;
252 }
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));
256 map.data[i] = new;
257 }
258 }
259 /* don't overflow */
260 bmd->skipped += MIN (G_MAXUINT - bmd->skipped, map.size);
261
262 gst_buffer_unmap (buf, &map);
263
264 GST_OBJECT_UNLOCK (bmd);
265
266 return GST_FLOW_OK;
267 }
268
269 static gboolean
gst_break_my_data_start(GstBaseTransform * trans)270 gst_break_my_data_start (GstBaseTransform * trans)
271 {
272 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
273
274 GST_OBJECT_LOCK (bmd);
275 bmd->rand = g_rand_new_with_seed (bmd->seed);
276 bmd->skipped = 0;
277 GST_OBJECT_UNLOCK (bmd);
278
279 return TRUE;
280 }
281
282 static gboolean
gst_break_my_data_stop(GstBaseTransform * trans)283 gst_break_my_data_stop (GstBaseTransform * trans)
284 {
285 GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
286
287 GST_OBJECT_LOCK (bmd);
288 g_rand_free (bmd->rand);
289 bmd->rand = NULL;
290 GST_OBJECT_UNLOCK (bmd);
291
292 return TRUE;
293 }
294