GST_DEBUG reorganization containing loads of stuff:
[platform/upstream/gstreamer.git] / gst / 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 "gstidentity.h"
31
32 GST_DEBUG_CATEGORY (gst_identity_debug);
33 #define GST_CAT_DEFAULT gst_identity_debug
34
35 GstElementDetails gst_identity_details = {
36   "Identity",
37   "Generic",
38   "LGPL",
39   "Pass data without modification",
40   VERSION,
41   "Erik Walthinsen <omega@cse.ogi.edu>",
42   "(C) 1999",
43 };
44
45
46 /* Identity signals and args */
47 enum {
48   SIGNAL_HANDOFF,
49   /* FILL ME */
50   LAST_SIGNAL
51 };
52
53 enum {
54   ARG_0,
55   ARG_LOOP_BASED,
56   ARG_SLEEP_TIME,
57   ARG_DUPLICATE,
58   ARG_ERROR_AFTER,
59   ARG_DROP_PROBABILITY,
60   ARG_SILENT,
61   ARG_LAST_MESSAGE,
62   ARG_DUMP,
63   ARG_DELAY_CAPSNEGO,
64 };
65
66
67 static void gst_identity_class_init     (GstIdentityClass *klass);
68 static void gst_identity_init           (GstIdentity *identity);
69
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);
72
73 static void gst_identity_chain          (GstPad *pad, GstBuffer *buf);
74
75 static GstElementClass *parent_class = NULL;
76 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
77
78 GType
79 gst_identity_get_type (void) 
80 {
81   static GType identity_type = 0;
82
83   if (!identity_type) {
84     static const GTypeInfo identity_info = {
85       sizeof(GstIdentityClass),      NULL,
86       NULL,
87       (GClassInitFunc)gst_identity_class_init,
88       NULL,
89       NULL,
90       sizeof(GstIdentity),
91       0,
92       (GInstanceInitFunc)gst_identity_init,
93     };
94     identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstIdentity", &identity_info, 0);
95   }
96   return identity_type;
97 }
98
99 static void 
100 gst_identity_class_init (GstIdentityClass *klass) 
101 {
102   GObjectClass *gobject_class;
103
104   gobject_class = (GObjectClass*)klass;
105
106   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
107
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));
136
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,
141                    G_TYPE_POINTER);
142
143   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);  
144   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
145 }
146
147 static GstBufferPool*
148 gst_identity_get_bufferpool (GstPad *pad)
149 {
150   GstIdentity *identity;
151
152   identity = GST_IDENTITY (gst_pad_get_parent (pad));
153
154   return gst_pad_get_bufferpool (identity->srcpad);
155 }
156
157 static GstCaps*
158 gst_identity_getcaps (GstPad *pad, GstCaps *caps)
159 {
160   GstIdentity *identity;
161   GstPad *otherpad;
162   
163   identity = GST_IDENTITY (gst_pad_get_parent (pad));
164
165   if (identity->delay_capsnego) {
166     return NULL;
167   }
168
169   otherpad = (pad == identity->srcpad ? identity->sinkpad : identity->srcpad);
170
171   return gst_pad_get_allowed_caps (otherpad);
172 }
173
174 static GstPadLinkReturn
175 gst_identity_link (GstPad *pad, GstCaps *caps)
176 {
177   GstIdentity *identity;
178   
179   identity = GST_IDENTITY (gst_pad_get_parent (pad));
180
181   if (GST_CAPS_IS_FIXED (caps)) {
182     if (identity->delay_capsnego && GST_PAD_IS_SINK (pad)) {
183       identity->srccaps = gst_caps_ref (caps);
184
185       return GST_PAD_LINK_OK;
186     }
187     else {
188       GstPad *otherpad;
189       
190       otherpad = (pad == identity->srcpad ? identity->sinkpad : identity->srcpad);
191
192       return gst_pad_try_set_caps (otherpad, caps);
193     }
194   }
195   else
196     return GST_PAD_LINK_DELAYED;
197 }
198
199 static void 
200 gst_identity_init (GstIdentity *identity) 
201 {
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);
208   
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);
213
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;
224 }
225
226 static void 
227 gst_identity_chain (GstPad *pad, GstBuffer *buf) 
228 {
229   GstIdentity *identity;
230   guint i;
231
232   g_return_if_fail (pad != NULL);
233   g_return_if_fail (GST_IS_PAD (pad));
234   g_return_if_fail (buf != NULL);
235
236   identity = GST_IDENTITY (gst_pad_get_parent (pad));
237
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);
242         return;
243       }
244     }
245     identity->srccaps = NULL;
246   }
247
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");
253       return;
254     }
255   }
256
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);
261       }
262       identity->last_message = g_strdup_printf ("dropping   ******* (%s:%s)i (%d bytes, %"
263                                                 G_GINT64_FORMAT ")",
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);
267       return;
268     }
269   }
270   if (identity->dump) {
271     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
272   }
273
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, %"
278                                                 G_GINT64_FORMAT ")",
279               GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
280       g_object_notify (G_OBJECT (identity), "last-message");
281     }
282
283     g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
284                                buf);
285
286     if (i>1) 
287       gst_buffer_ref (buf);
288
289     gst_pad_push (identity->srcpad, buf);
290
291     if (identity->sleep_time)
292       g_usleep (identity->sleep_time);
293   }
294 }
295
296 static void 
297 gst_identity_loop (GstElement *element) 
298 {
299   GstIdentity *identity;
300   GstBuffer *buf;
301
302   g_return_if_fail (element != NULL);
303   g_return_if_fail (GST_IS_IDENTITY (element));
304
305   identity = GST_IDENTITY (element);
306   
307   buf = gst_pad_pull (identity->sinkpad);
308   if (GST_IS_EVENT (buf)) {
309     GstEvent *event = GST_EVENT (buf);
310
311     if (GST_EVENT_IS_INTERRUPT (event)) {
312       gst_event_unref (event);
313     }
314     else {
315       gst_pad_event_default (identity->sinkpad, event);
316     }
317   }
318   else {
319     gst_identity_chain (identity->sinkpad, buf);
320   }
321 }
322
323 static void 
324 gst_identity_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
325 {
326   GstIdentity *identity;
327
328   /* it's not null if we got it, but it might not be ours */
329   g_return_if_fail (GST_IS_IDENTITY (object));
330   
331   identity = GST_IDENTITY (object);
332
333   switch (prop_id) {
334     case ARG_LOOP_BASED:
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);
339       }
340       else {
341         gst_pad_set_chain_function (identity->sinkpad, gst_identity_chain);
342         gst_element_set_loop_function (GST_ELEMENT (identity), NULL);
343       }
344       break;
345     case ARG_SLEEP_TIME:
346       identity->sleep_time = g_value_get_uint (value);
347       break;
348     case ARG_SILENT:
349       identity->silent = g_value_get_boolean (value);
350       break;
351     case ARG_DUPLICATE:
352       identity->duplicate = g_value_get_uint (value);
353       break;
354     case ARG_DUMP:
355       identity->dump = g_value_get_boolean (value);
356       break;
357     case ARG_DELAY_CAPSNEGO:
358       identity->delay_capsnego = g_value_get_boolean (value);
359       break;
360     case ARG_ERROR_AFTER:
361       identity->error_after = g_value_get_int (value);
362       break;
363     case ARG_DROP_PROBABILITY:
364       identity->drop_probability = g_value_get_float (value);
365       break;
366     default:
367       break;
368   }
369 }
370
371 static void gst_identity_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
372   GstIdentity *identity;
373
374   /* it's not null if we got it, but it might not be ours */
375   g_return_if_fail (GST_IS_IDENTITY (object));
376   
377   identity = GST_IDENTITY (object);
378
379   switch (prop_id) {
380     case ARG_LOOP_BASED:
381       g_value_set_boolean (value, identity->loop_based);
382       break;
383     case ARG_SLEEP_TIME:
384       g_value_set_uint (value, identity->sleep_time);
385       break;
386     case ARG_DUPLICATE:
387       g_value_set_uint (value, identity->duplicate);
388       break;
389     case ARG_ERROR_AFTER:
390       g_value_set_int (value, identity->error_after);
391       break;
392     case ARG_DROP_PROBABILITY:
393       g_value_set_float (value, identity->drop_probability);
394       break;
395     case ARG_SILENT:
396       g_value_set_boolean (value, identity->silent);
397       break;
398     case ARG_DUMP:
399       g_value_set_boolean (value, identity->dump);
400       break;
401     case ARG_DELAY_CAPSNEGO:
402       g_value_set_boolean (value, identity->delay_capsnego);
403       break;
404     case ARG_LAST_MESSAGE:
405       g_value_set_string (value, identity->last_message);
406       break;
407     default:
408       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
409       break;
410   }
411 }