1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 *
5 * gstidentity.c:
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
24 #include <stdlib.h>
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "../gst-i18n-lib.h"
31 #include "gstidentity.h"
32 #include <gst/gstmarshal.h>
34 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
35 #define GST_CAT_DEFAULT gst_identity_debug
37 GstElementDetails gst_identity_details = GST_ELEMENT_DETAILS ("Identity",
38 "Generic",
39 "Pass data without modification",
40 "Erik Walthinsen <omega@cse.ogi.edu>");
43 /* Identity signals and args */
44 enum
45 {
46 SIGNAL_HANDOFF,
47 /* FILL ME */
48 LAST_SIGNAL
49 };
51 enum
52 {
53 ARG_0,
54 ARG_LOOP_BASED,
55 ARG_SLEEP_TIME,
56 ARG_DUPLICATE,
57 ARG_ERROR_AFTER,
58 ARG_DROP_PROBABILITY,
59 ARG_SILENT,
60 ARG_LAST_MESSAGE,
61 ARG_DUMP
62 };
65 #define _do_init(bla) \
66 GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
68 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstElement, GST_TYPE_ELEMENT,
69 _do_init);
71 static void gst_identity_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_identity_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
76 static void gst_identity_chain (GstPad * pad, GstData * _data);
78 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
80 static void
81 gst_identity_base_init (gpointer g_class)
82 {
83 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
85 gst_element_class_set_details (gstelement_class, &gst_identity_details);
86 }
87 static void
88 gst_identity_class_init (GstIdentityClass * klass)
89 {
90 GObjectClass *gobject_class;
92 gobject_class = G_OBJECT_CLASS (klass);
95 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOOP_BASED,
96 g_param_spec_boolean ("loop-based", "Loop-based",
97 "Set to TRUE to use loop-based rather than chain-based scheduling",
98 TRUE, G_PARAM_READWRITE));
99 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SLEEP_TIME,
100 g_param_spec_uint ("sleep-time", "Sleep time",
101 "Microseconds to sleep between processing", 0, G_MAXUINT, 0,
102 G_PARAM_READWRITE));
103 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUPLICATE,
104 g_param_spec_uint ("duplicate", "Duplicate Buffers",
105 "Push the buffers N times", 0, G_MAXUINT, 1, G_PARAM_READWRITE));
106 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ERROR_AFTER,
107 g_param_spec_int ("error_after", "Error After", "Error after N buffers",
108 G_MININT, G_MAXINT, -1, G_PARAM_READWRITE));
109 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DROP_PROBABILITY,
110 g_param_spec_float ("drop_probability", "Drop Probability",
111 "The Probability a buffer is dropped", 0.0, 1.0, 0.0,
112 G_PARAM_READWRITE));
113 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
114 g_param_spec_boolean ("silent", "silent", "silent", FALSE,
115 G_PARAM_READWRITE));
116 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
117 g_param_spec_string ("last-message", "last-message", "last-message", NULL,
118 G_PARAM_READABLE));
119 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUMP,
120 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents", FALSE,
121 G_PARAM_READWRITE));
123 gst_identity_signals[SIGNAL_HANDOFF] =
124 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
125 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
126 gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
128 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);
129 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
130 }
132 static void
133 gst_identity_init (GstIdentity * identity)
134 {
135 identity->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
136 gst_element_add_pad (GST_ELEMENT (identity), identity->sinkpad);
137 gst_pad_set_chain_function (identity->sinkpad,
138 GST_DEBUG_FUNCPTR (gst_identity_chain));
139 gst_pad_set_link_function (identity->sinkpad, gst_pad_proxy_pad_link);
140 gst_pad_set_getcaps_function (identity->sinkpad, gst_pad_proxy_getcaps);
142 identity->srcpad = gst_pad_new ("src", GST_PAD_SRC);
143 gst_element_add_pad (GST_ELEMENT (identity), identity->srcpad);
144 gst_pad_set_link_function (identity->srcpad, gst_pad_proxy_pad_link);
145 gst_pad_set_getcaps_function (identity->srcpad, gst_pad_proxy_getcaps);
147 identity->loop_based = FALSE;
148 identity->sleep_time = 0;
149 identity->duplicate = 1;
150 identity->error_after = -1;
151 identity->drop_probability = 0.0;
152 identity->silent = FALSE;
153 identity->dump = FALSE;
154 identity->last_message = NULL;
155 identity->srccaps = NULL;
156 }
158 static void
159 gst_identity_chain (GstPad * pad, GstData * _data)
160 {
161 GstBuffer *buf = GST_BUFFER (_data);
162 GstIdentity *identity;
163 guint i;
165 g_return_if_fail (pad != NULL);
166 g_return_if_fail (GST_IS_PAD (pad));
167 g_return_if_fail (buf != NULL);
169 identity = GST_IDENTITY (gst_pad_get_parent (pad));
171 if (identity->error_after >= 0) {
172 identity->error_after--;
173 if (identity->error_after == 0) {
174 gst_buffer_unref (buf);
175 GST_ELEMENT_ERROR (identity, CORE, FAILED,
176 (_("Failed after iterations as requested.")), (NULL));
177 return;
178 }
179 }
181 if (identity->drop_probability > 0.0) {
182 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability) {
183 if (identity->last_message != NULL) {
184 g_free (identity->last_message);
185 }
186 identity->last_message =
187 g_strdup_printf ("dropping ******* (%s:%s)i (%d bytes, %"
188 G_GINT64_FORMAT ")", GST_DEBUG_PAD_NAME (identity->sinkpad),
189 GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
190 g_object_notify (G_OBJECT (identity), "last-message");
191 gst_buffer_unref (buf);
192 return;
193 }
194 }
195 if (identity->dump) {
196 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
197 }
199 for (i = identity->duplicate; i; i--) {
200 if (!identity->silent) {
201 g_free (identity->last_message);
202 identity->last_message =
203 g_strdup_printf ("chain ******* (%s:%s)i (%d bytes, %"
204 G_GINT64_FORMAT ")", GST_DEBUG_PAD_NAME (identity->sinkpad),
205 GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
206 g_object_notify (G_OBJECT (identity), "last-message");
207 }
209 g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
210 buf);
212 if (i > 1)
213 gst_buffer_ref (buf);
215 gst_pad_push (identity->srcpad, GST_DATA (buf));
217 if (identity->sleep_time)
218 g_usleep (identity->sleep_time);
219 }
220 }
222 static void
223 gst_identity_loop (GstElement * element)
224 {
225 GstIdentity *identity;
226 GstBuffer *buf;
228 g_return_if_fail (element != NULL);
229 g_return_if_fail (GST_IS_IDENTITY (element));
231 identity = GST_IDENTITY (element);
233 buf = GST_BUFFER (gst_pad_pull (identity->sinkpad));
234 if (GST_IS_EVENT (buf)) {
235 GstEvent *event = GST_EVENT (buf);
237 if (GST_EVENT_IS_INTERRUPT (event)) {
238 gst_event_unref (event);
239 } else {
240 gst_pad_event_default (identity->sinkpad, event);
241 }
242 } else {
243 gst_identity_chain (identity->sinkpad, GST_DATA (buf));
244 }
245 }
247 static void
248 gst_identity_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec)
250 {
251 GstIdentity *identity;
253 /* it's not null if we got it, but it might not be ours */
254 g_return_if_fail (GST_IS_IDENTITY (object));
256 identity = GST_IDENTITY (object);
258 switch (prop_id) {
259 case ARG_LOOP_BASED:
260 identity->loop_based = g_value_get_boolean (value);
261 if (identity->loop_based) {
262 gst_element_set_loop_function (GST_ELEMENT (identity),
263 gst_identity_loop);
264 gst_pad_set_chain_function (identity->sinkpad, NULL);
265 } else {
266 gst_pad_set_chain_function (identity->sinkpad, gst_identity_chain);
267 gst_element_set_loop_function (GST_ELEMENT (identity), NULL);
268 }
269 break;
270 case ARG_SLEEP_TIME:
271 identity->sleep_time = g_value_get_uint (value);
272 break;
273 case ARG_SILENT:
274 identity->silent = g_value_get_boolean (value);
275 break;
276 case ARG_DUPLICATE:
277 identity->duplicate = g_value_get_uint (value);
278 break;
279 case ARG_DUMP:
280 identity->dump = g_value_get_boolean (value);
281 break;
282 case ARG_ERROR_AFTER:
283 identity->error_after = g_value_get_int (value);
284 break;
285 case ARG_DROP_PROBABILITY:
286 identity->drop_probability = g_value_get_float (value);
287 break;
288 default:
289 break;
290 }
291 }
293 static void
294 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
295 GParamSpec * pspec)
296 {
297 GstIdentity *identity;
299 /* it's not null if we got it, but it might not be ours */
300 g_return_if_fail (GST_IS_IDENTITY (object));
302 identity = GST_IDENTITY (object);
304 switch (prop_id) {
305 case ARG_LOOP_BASED:
306 g_value_set_boolean (value, identity->loop_based);
307 break;
308 case ARG_SLEEP_TIME:
309 g_value_set_uint (value, identity->sleep_time);
310 break;
311 case ARG_DUPLICATE:
312 g_value_set_uint (value, identity->duplicate);
313 break;
314 case ARG_ERROR_AFTER:
315 g_value_set_int (value, identity->error_after);
316 break;
317 case ARG_DROP_PROBABILITY:
318 g_value_set_float (value, identity->drop_probability);
319 break;
320 case ARG_SILENT:
321 g_value_set_boolean (value, identity->silent);
322 break;
323 case ARG_DUMP:
324 g_value_set_boolean (value, identity->dump);
325 break;
326 case ARG_LAST_MESSAGE:
327 g_value_set_string (value, identity->last_message);
328 break;
329 default:
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 break;
332 }
333 }