parts of the patch submitted in bug #113913
[platform/upstream/gstreamer.git] / plugins / elements / gstidentity.c
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  */
22
23
24 #include <stdlib.h>
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "../gst-i18n-lib.h"
31 #include "gstidentity.h"
32
33 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
34 #define GST_CAT_DEFAULT gst_identity_debug
35
36 GstElementDetails gst_identity_details = GST_ELEMENT_DETAILS (
37   "Identity",
38   "Generic",
39   "Pass data without modification",
40   "Erik Walthinsen <omega@cse.ogi.edu>"
41 );
42
43
44 /* Identity signals and args */
45 enum {
46   SIGNAL_HANDOFF,
47   /* FILL ME */
48   LAST_SIGNAL
49 };
50
51 enum {
52   ARG_0,
53   ARG_LOOP_BASED,
54   ARG_SLEEP_TIME,
55   ARG_DUPLICATE,
56   ARG_ERROR_AFTER,
57   ARG_DROP_PROBABILITY,
58   ARG_SILENT,
59   ARG_LAST_MESSAGE,
60   ARG_DUMP
61 };
62
63
64 #define _do_init(bla) \
65     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
66
67 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstElement, GST_TYPE_ELEMENT, _do_init);
68
69 static void gst_identity_set_property   (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
70 static void gst_identity_get_property   (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
71
72 static void gst_identity_chain          (GstPad *pad, GstData *_data);
73
74 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
75
76 static void
77 gst_identity_base_init (gpointer g_class)
78 {
79   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
80   
81   gst_element_class_set_details (gstelement_class, &gst_identity_details);
82 }
83 static void 
84 gst_identity_class_init (GstIdentityClass *klass) 
85 {
86   GObjectClass *gobject_class;
87
88   gobject_class = G_OBJECT_CLASS (klass);
89
90
91   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOOP_BASED,
92     g_param_spec_boolean ("loop-based", "Loop-based", 
93                           "Set to TRUE to use loop-based rather than chain-based scheduling",
94                           TRUE, G_PARAM_READWRITE)); 
95   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SLEEP_TIME,
96     g_param_spec_uint ("sleep-time", "Sleep time", "Microseconds to sleep between processing",
97                        0, G_MAXUINT, 0, G_PARAM_READWRITE));
98   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUPLICATE,
99     g_param_spec_uint ("duplicate", "Duplicate Buffers", "Push the buffers N times",
100                        0, G_MAXUINT, 1, G_PARAM_READWRITE));
101   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ERROR_AFTER,
102     g_param_spec_int ("error_after", "Error After", "Error after N buffers",
103                        G_MININT, G_MAXINT, -1, G_PARAM_READWRITE));
104   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DROP_PROBABILITY,
105     g_param_spec_float ("drop_probability", "Drop Probability", "The Probability a buffer is dropped",
106                         0.0, 1.0, 0.0, G_PARAM_READWRITE));
107   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
108     g_param_spec_boolean ("silent", "silent", "silent",
109                           FALSE, G_PARAM_READWRITE)); 
110   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
111     g_param_spec_string ("last-message", "last-message", "last-message",
112                          NULL, G_PARAM_READABLE)); 
113   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUMP,
114     g_param_spec_boolean("dump", "Dump", "Dump buffer contents",
115                          FALSE, G_PARAM_READWRITE));
116
117   gst_identity_signals[SIGNAL_HANDOFF] =
118     g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
119                    G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
120                    gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
121                    GST_TYPE_BUFFER);
122
123   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);  
124   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
125 }
126
127 static void 
128 gst_identity_init (GstIdentity *identity) 
129 {
130   identity->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
131   gst_element_add_pad (GST_ELEMENT (identity), identity->sinkpad);
132   gst_pad_set_chain_function (identity->sinkpad, GST_DEBUG_FUNCPTR (gst_identity_chain));
133   gst_pad_set_link_function (identity->sinkpad, gst_pad_proxy_pad_link);
134   gst_pad_set_getcaps_function (identity->sinkpad, gst_pad_proxy_getcaps);
135   
136   identity->srcpad = gst_pad_new ("src", GST_PAD_SRC);
137   gst_element_add_pad (GST_ELEMENT (identity), identity->srcpad);
138   gst_pad_set_link_function (identity->srcpad, gst_pad_proxy_pad_link);
139   gst_pad_set_getcaps_function (identity->srcpad, gst_pad_proxy_getcaps);
140
141   identity->loop_based = FALSE;
142   identity->sleep_time = 0;
143   identity->duplicate = 1;
144   identity->error_after = -1;
145   identity->drop_probability = 0.0;
146   identity->silent = FALSE;
147   identity->dump = FALSE;
148   identity->last_message = NULL;
149   identity->srccaps = NULL;
150 }
151
152 static void 
153 gst_identity_chain (GstPad *pad, GstData *_data) 
154 {
155   GstBuffer *buf = GST_BUFFER (_data);
156   GstIdentity *identity;
157   guint i;
158
159   g_return_if_fail (pad != NULL);
160   g_return_if_fail (GST_IS_PAD (pad));
161   g_return_if_fail (buf != NULL);
162
163   identity = GST_IDENTITY (gst_pad_get_parent (pad));
164
165   if (identity->error_after >= 0) {
166     identity->error_after--;
167     if (identity->error_after == 0) {
168       gst_buffer_unref (buf);
169       GST_ELEMENT_ERROR (identity, CORE, FAILED,
170                            (_("Failed after iterations as requested")),
171                            NULL);
172       return;
173     }
174   }
175
176   if (identity->drop_probability > 0.0) {
177     if ((gfloat)(1.0*rand()/(RAND_MAX)) < identity->drop_probability) {
178       if (identity->last_message != NULL) {
179         g_free (identity->last_message);
180       }
181       identity->last_message = g_strdup_printf ("dropping   ******* (%s:%s)i (%d bytes, %"
182                                                 G_GINT64_FORMAT ")",
183               GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
184       g_object_notify (G_OBJECT (identity), "last-message");
185       gst_buffer_unref (buf);
186       return;
187     }
188   }
189   if (identity->dump) {
190     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
191   }
192
193   for (i = identity->duplicate; i; i--) {
194     if (!identity->silent) {
195       g_free (identity->last_message);
196       identity->last_message = g_strdup_printf ("chain   ******* (%s:%s)i (%d bytes, %"
197                                                 G_GINT64_FORMAT ")",
198               GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
199       g_object_notify (G_OBJECT (identity), "last-message");
200     }
201
202     g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
203                                buf);
204
205     if (i>1) 
206       gst_buffer_ref (buf);
207
208     gst_pad_push (identity->srcpad, GST_DATA (buf));
209
210     if (identity->sleep_time)
211       g_usleep (identity->sleep_time);
212   }
213 }
214
215 static void 
216 gst_identity_loop (GstElement *element) 
217 {
218   GstIdentity *identity;
219   GstBuffer *buf;
220
221   g_return_if_fail (element != NULL);
222   g_return_if_fail (GST_IS_IDENTITY (element));
223
224   identity = GST_IDENTITY (element);
225   
226   buf = GST_BUFFER (gst_pad_pull (identity->sinkpad));
227   if (GST_IS_EVENT (buf)) {
228     GstEvent *event = GST_EVENT (buf);
229
230     if (GST_EVENT_IS_INTERRUPT (event)) {
231       gst_event_unref (event);
232     }
233     else {
234       gst_pad_event_default (identity->sinkpad, event);
235     }
236   }
237   else {
238     gst_identity_chain (identity->sinkpad, GST_DATA (buf));
239   }
240 }
241
242 static void 
243 gst_identity_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
244 {
245   GstIdentity *identity;
246
247   /* it's not null if we got it, but it might not be ours */
248   g_return_if_fail (GST_IS_IDENTITY (object));
249   
250   identity = GST_IDENTITY (object);
251
252   switch (prop_id) {
253     case ARG_LOOP_BASED:
254       identity->loop_based = g_value_get_boolean (value);
255       if (identity->loop_based) {
256         gst_element_set_loop_function (GST_ELEMENT (identity), gst_identity_loop);
257         gst_pad_set_chain_function (identity->sinkpad, NULL);
258       }
259       else {
260         gst_pad_set_chain_function (identity->sinkpad, gst_identity_chain);
261         gst_element_set_loop_function (GST_ELEMENT (identity), NULL);
262       }
263       break;
264     case ARG_SLEEP_TIME:
265       identity->sleep_time = g_value_get_uint (value);
266       break;
267     case ARG_SILENT:
268       identity->silent = g_value_get_boolean (value);
269       break;
270     case ARG_DUPLICATE:
271       identity->duplicate = g_value_get_uint (value);
272       break;
273     case ARG_DUMP:
274       identity->dump = g_value_get_boolean (value);
275       break;
276     case ARG_ERROR_AFTER:
277       identity->error_after = g_value_get_int (value);
278       break;
279     case ARG_DROP_PROBABILITY:
280       identity->drop_probability = g_value_get_float (value);
281       break;
282     default:
283       break;
284   }
285 }
286
287 static void gst_identity_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
288   GstIdentity *identity;
289
290   /* it's not null if we got it, but it might not be ours */
291   g_return_if_fail (GST_IS_IDENTITY (object));
292   
293   identity = GST_IDENTITY (object);
294
295   switch (prop_id) {
296     case ARG_LOOP_BASED:
297       g_value_set_boolean (value, identity->loop_based);
298       break;
299     case ARG_SLEEP_TIME:
300       g_value_set_uint (value, identity->sleep_time);
301       break;
302     case ARG_DUPLICATE:
303       g_value_set_uint (value, identity->duplicate);
304       break;
305     case ARG_ERROR_AFTER:
306       g_value_set_int (value, identity->error_after);
307       break;
308     case ARG_DROP_PROBABILITY:
309       g_value_set_float (value, identity->drop_probability);
310       break;
311     case ARG_SILENT:
312       g_value_set_boolean (value, identity->silent);
313       break;
314     case ARG_DUMP:
315       g_value_set_boolean (value, identity->dump);
316       break;
317     case ARG_LAST_MESSAGE:
318       g_value_set_string (value, identity->last_message);
319       break;
320     default:
321       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322       break;
323   }
324 }