2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
30 #include "gstidentity.h"
32 GST_DEBUG_CATEGORY (gst_identity_debug);
33 #define GST_CAT_DEFAULT gst_identity_debug
35 GstElementDetails gst_identity_details = {
39 "Pass data without modification",
41 "Erik Walthinsen <omega@cse.ogi.edu>",
46 /* Identity signals and args */
67 static void gst_identity_class_init (GstIdentityClass *klass);
68 static void gst_identity_init (GstIdentity *identity);
70 static void gst_identity_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
71 static void gst_identity_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
73 static void gst_identity_chain (GstPad *pad, GstBuffer *buf);
75 static GstElementClass *parent_class = NULL;
76 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
79 gst_identity_get_type (void)
81 static GType identity_type = 0;
84 static const GTypeInfo identity_info = {
85 sizeof(GstIdentityClass), NULL,
87 (GClassInitFunc)gst_identity_class_init,
92 (GInstanceInitFunc)gst_identity_init,
94 identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstIdentity", &identity_info, 0);
100 gst_identity_class_init (GstIdentityClass *klass)
102 GObjectClass *gobject_class;
104 gobject_class = (GObjectClass*)klass;
106 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
108 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOOP_BASED,
109 g_param_spec_boolean ("loop-based", "Loop-based",
110 "Set to TRUE to use loop-based rather than chain-based scheduling",
111 TRUE, G_PARAM_READWRITE));
112 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SLEEP_TIME,
113 g_param_spec_uint ("sleep-time", "Sleep time", "Microseconds to sleep between processing",
114 0, G_MAXUINT, 0, G_PARAM_READWRITE));
115 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUPLICATE,
116 g_param_spec_uint ("duplicate", "Duplicate Buffers", "Push the buffers N times",
117 0, G_MAXUINT, 1, G_PARAM_READWRITE));
118 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ERROR_AFTER,
119 g_param_spec_int ("error_after", "Error After", "Error after N buffers",
120 G_MININT, G_MAXINT, -1, G_PARAM_READWRITE));
121 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DROP_PROBABILITY,
122 g_param_spec_float ("drop_probability", "Drop Probability", "The Probability a buffer is dropped",
123 0.0, 1.0, 0.0, G_PARAM_READWRITE));
124 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
125 g_param_spec_boolean ("silent", "silent", "silent",
126 FALSE, G_PARAM_READWRITE));
127 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
128 g_param_spec_string ("last-message", "last-message", "last-message",
129 NULL, G_PARAM_READABLE));
130 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUMP,
131 g_param_spec_boolean("dump", "Dump", "Dump buffer contents",
132 FALSE, G_PARAM_READWRITE));
133 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DELAY_CAPSNEGO,
134 g_param_spec_boolean("delay_capsnego", "Delay Caps Nego", "Delay capsnegotiation to loop/chain function",
135 FALSE, G_PARAM_READWRITE));
137 gst_identity_signals[SIGNAL_HANDOFF] =
138 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
139 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
140 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
143 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);
144 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
147 static GstBufferPool*
148 gst_identity_get_bufferpool (GstPad *pad)
150 GstIdentity *identity;
152 identity = GST_IDENTITY (gst_pad_get_parent (pad));
154 return gst_pad_get_bufferpool (identity->srcpad);
158 gst_identity_getcaps (GstPad *pad, GstCaps *caps)
160 GstIdentity *identity;
163 identity = GST_IDENTITY (gst_pad_get_parent (pad));
165 if (identity->delay_capsnego) {
169 otherpad = (pad == identity->srcpad ? identity->sinkpad : identity->srcpad);
171 return gst_pad_get_allowed_caps (otherpad);
174 static GstPadLinkReturn
175 gst_identity_link (GstPad *pad, GstCaps *caps)
177 GstIdentity *identity;
179 identity = GST_IDENTITY (gst_pad_get_parent (pad));
181 if (GST_CAPS_IS_FIXED (caps)) {
182 if (identity->delay_capsnego && GST_PAD_IS_SINK (pad)) {
183 identity->srccaps = gst_caps_ref (caps);
185 return GST_PAD_LINK_OK;
190 otherpad = (pad == identity->srcpad ? identity->sinkpad : identity->srcpad);
192 return gst_pad_try_set_caps (otherpad, caps);
196 return GST_PAD_LINK_DELAYED;
200 gst_identity_init (GstIdentity *identity)
202 identity->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
203 gst_element_add_pad (GST_ELEMENT (identity), identity->sinkpad);
204 gst_pad_set_chain_function (identity->sinkpad, GST_DEBUG_FUNCPTR (gst_identity_chain));
205 gst_pad_set_bufferpool_function (identity->sinkpad, gst_identity_get_bufferpool);
206 gst_pad_set_link_function (identity->sinkpad, gst_identity_link);
207 gst_pad_set_getcaps_function (identity->sinkpad, gst_identity_getcaps);
209 identity->srcpad = gst_pad_new ("src", GST_PAD_SRC);
210 gst_element_add_pad (GST_ELEMENT (identity), identity->srcpad);
211 gst_pad_set_link_function (identity->srcpad, gst_identity_link);
212 gst_pad_set_getcaps_function (identity->srcpad, gst_identity_getcaps);
214 identity->loop_based = FALSE;
215 identity->sleep_time = 0;
216 identity->duplicate = 1;
217 identity->error_after = -1;
218 identity->drop_probability = 0.0;
219 identity->silent = FALSE;
220 identity->dump = FALSE;
221 identity->last_message = NULL;
222 identity->delay_capsnego = FALSE;
223 identity->srccaps = NULL;
227 gst_identity_chain (GstPad *pad, GstBuffer *buf)
229 GstIdentity *identity;
232 g_return_if_fail (pad != NULL);
233 g_return_if_fail (GST_IS_PAD (pad));
234 g_return_if_fail (buf != NULL);
236 identity = GST_IDENTITY (gst_pad_get_parent (pad));
238 if (identity->delay_capsnego && identity->srccaps) {
239 if (gst_pad_try_set_caps (identity->srcpad, identity->srccaps) <= 0) {
240 if (!gst_pad_recover_caps_error (identity->srcpad, identity->srccaps)) {
241 gst_buffer_unref (buf);
245 identity->srccaps = NULL;
248 if (identity->error_after >= 0) {
249 identity->error_after--;
250 if (identity->error_after == 0) {
251 gst_buffer_unref (buf);
252 gst_element_error (GST_ELEMENT (identity), "errored after iterations as requested");
257 if (identity->drop_probability > 0.0) {
258 if ((gfloat)(1.0*rand()/(RAND_MAX)) < identity->drop_probability) {
259 if (identity->last_message != NULL) {
260 g_free (identity->last_message);
262 identity->last_message = g_strdup_printf ("dropping ******* (%s:%s)i (%d bytes, %"
264 GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
265 g_object_notify (G_OBJECT (identity), "last-message");
266 gst_buffer_unref (buf);
270 if (identity->dump) {
271 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
274 for (i = identity->duplicate; i; i--) {
275 if (!identity->silent) {
276 g_free (identity->last_message);
277 identity->last_message = g_strdup_printf ("chain ******* (%s:%s)i (%d bytes, %"
279 GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
280 g_object_notify (G_OBJECT (identity), "last-message");
283 g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
287 gst_buffer_ref (buf);
289 gst_pad_push (identity->srcpad, buf);
291 if (identity->sleep_time)
292 g_usleep (identity->sleep_time);
297 gst_identity_loop (GstElement *element)
299 GstIdentity *identity;
302 g_return_if_fail (element != NULL);
303 g_return_if_fail (GST_IS_IDENTITY (element));
305 identity = GST_IDENTITY (element);
307 buf = gst_pad_pull (identity->sinkpad);
308 if (GST_IS_EVENT (buf)) {
309 GstEvent *event = GST_EVENT (buf);
311 if (GST_EVENT_IS_INTERRUPT (event)) {
312 gst_event_unref (event);
315 gst_pad_event_default (identity->sinkpad, event);
319 gst_identity_chain (identity->sinkpad, buf);
324 gst_identity_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
326 GstIdentity *identity;
328 /* it's not null if we got it, but it might not be ours */
329 g_return_if_fail (GST_IS_IDENTITY (object));
331 identity = GST_IDENTITY (object);
335 identity->loop_based = g_value_get_boolean (value);
336 if (identity->loop_based) {
337 gst_element_set_loop_function (GST_ELEMENT (identity), gst_identity_loop);
338 gst_pad_set_chain_function (identity->sinkpad, NULL);
341 gst_pad_set_chain_function (identity->sinkpad, gst_identity_chain);
342 gst_element_set_loop_function (GST_ELEMENT (identity), NULL);
346 identity->sleep_time = g_value_get_uint (value);
349 identity->silent = g_value_get_boolean (value);
352 identity->duplicate = g_value_get_uint (value);
355 identity->dump = g_value_get_boolean (value);
357 case ARG_DELAY_CAPSNEGO:
358 identity->delay_capsnego = g_value_get_boolean (value);
360 case ARG_ERROR_AFTER:
361 identity->error_after = g_value_get_int (value);
363 case ARG_DROP_PROBABILITY:
364 identity->drop_probability = g_value_get_float (value);
371 static void gst_identity_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
372 GstIdentity *identity;
374 /* it's not null if we got it, but it might not be ours */
375 g_return_if_fail (GST_IS_IDENTITY (object));
377 identity = GST_IDENTITY (object);
381 g_value_set_boolean (value, identity->loop_based);
384 g_value_set_uint (value, identity->sleep_time);
387 g_value_set_uint (value, identity->duplicate);
389 case ARG_ERROR_AFTER:
390 g_value_set_int (value, identity->error_after);
392 case ARG_DROP_PROBABILITY:
393 g_value_set_float (value, identity->drop_probability);
396 g_value_set_boolean (value, identity->silent);
399 g_value_set_boolean (value, identity->dump);
401 case ARG_DELAY_CAPSNEGO:
402 g_value_set_boolean (value, identity->delay_capsnego);
404 case ARG_LAST_MESSAGE:
405 g_value_set_string (value, identity->last_message);
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);