do CVS surgery and related build fixery to move elements and indexers in a new gstrea...
[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  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstidentity.c:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24
25 #include <stdlib.h>
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "../../gst/gst-i18n-lib.h"
32 #include "gstidentity.h"
33 #include <gst/gstmarshal.h>
34
35 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS_ANY);
39
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS_ANY);
44
45 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
46 #define GST_CAT_DEFAULT gst_identity_debug
47
48 static GstElementDetails gst_identity_details = GST_ELEMENT_DETAILS ("Identity",
49     "Generic",
50     "Pass data without modification",
51     "Erik Walthinsen <omega@cse.ogi.edu>");
52
53
54 /* Identity signals and args */
55 enum
56 {
57   SIGNAL_HANDOFF,
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 #define DEFAULT_SLEEP_TIME              0
63 #define DEFAULT_DUPLICATE               1
64 #define DEFAULT_ERROR_AFTER             -1
65 #define DEFAULT_DROP_PROBABILITY        0.0
66 #define DEFAULT_DATARATE                0
67 #define DEFAULT_SILENT                  FALSE
68 #define DEFAULT_SINGLE_SEGMENT          FALSE
69 #define DEFAULT_DUMP                    FALSE
70 #define DEFAULT_SYNC                    FALSE
71 #define DEFAULT_CHECK_PERFECT           FALSE
72
73 enum
74 {
75   PROP_0,
76   PROP_SLEEP_TIME,
77   PROP_ERROR_AFTER,
78   PROP_DROP_PROBABILITY,
79   PROP_DATARATE,
80   PROP_SILENT,
81   PROP_SINGLE_SEGMENT,
82   PROP_LAST_MESSAGE,
83   PROP_DUMP,
84   PROP_SYNC,
85   PROP_CHECK_PERFECT
86 };
87
88
89 #define _do_init(bla) \
90     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
91
92 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstBaseTransform,
93     GST_TYPE_BASE_TRANSFORM, _do_init);
94
95 static void gst_identity_finalize (GObject * object);
96 static void gst_identity_set_property (GObject * object, guint prop_id,
97     const GValue * value, GParamSpec * pspec);
98 static void gst_identity_get_property (GObject * object, guint prop_id,
99     GValue * value, GParamSpec * pspec);
100
101 static gboolean gst_identity_event (GstBaseTransform * trans, GstEvent * event);
102 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
103     GstBuffer * buf);
104 static gboolean gst_identity_start (GstBaseTransform * trans);
105 static gboolean gst_identity_stop (GstBaseTransform * trans);
106
107 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
108
109 static void
110 gst_identity_base_init (gpointer g_class)
111 {
112   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
113
114   gst_element_class_add_pad_template (gstelement_class,
115       gst_static_pad_template_get (&srctemplate));
116   gst_element_class_add_pad_template (gstelement_class,
117       gst_static_pad_template_get (&sinktemplate));
118   gst_element_class_set_details (gstelement_class, &gst_identity_details);
119 }
120
121 static void
122 gst_identity_finalize (GObject * object)
123 {
124   GstIdentity *identity;
125
126   identity = GST_IDENTITY (object);
127
128   g_free (identity->last_message);
129
130   G_OBJECT_CLASS (parent_class)->finalize (object);
131 }
132
133 /* fixme: do something about this */
134 static void
135 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
136     guint n_param_values, const GValue * param_values, gpointer invocation_hint,
137     gpointer marshal_data)
138 {
139   typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
140       gpointer data2);
141   register marshalfunc_VOID__MINIOBJECT callback;
142   register GCClosure *cc = (GCClosure *) closure;
143   register gpointer data1, data2;
144
145   g_return_if_fail (n_param_values == 2);
146
147   if (G_CCLOSURE_SWAP_DATA (closure)) {
148     data1 = closure->data;
149     data2 = g_value_peek_pointer (param_values + 0);
150   } else {
151     data1 = g_value_peek_pointer (param_values + 0);
152     data2 = closure->data;
153   }
154   callback =
155       (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->
156       callback);
157
158   callback (data1, gst_value_get_mini_object (param_values + 1), data2);
159 }
160
161 static void
162 gst_identity_class_init (GstIdentityClass * klass)
163 {
164   GObjectClass *gobject_class;
165   GstElementClass *gstelement_class;
166   GstBaseTransformClass *gstbasetrans_class;
167
168   gobject_class = G_OBJECT_CLASS (klass);
169   gstelement_class = GST_ELEMENT_CLASS (klass);
170   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
171
172   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);
173   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
174
175   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SLEEP_TIME,
176       g_param_spec_uint ("sleep-time", "Sleep time",
177           "Microseconds to sleep between processing", 0, G_MAXUINT,
178           DEFAULT_SLEEP_TIME, G_PARAM_READWRITE));
179   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ERROR_AFTER,
180       g_param_spec_int ("error_after", "Error After", "Error after N buffers",
181           G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER, G_PARAM_READWRITE));
182   g_object_class_install_property (G_OBJECT_CLASS (klass),
183       PROP_DROP_PROBABILITY, g_param_spec_float ("drop_probability",
184           "Drop Probability", "The Probability a buffer is dropped", 0.0, 1.0,
185           DEFAULT_DROP_PROBABILITY, G_PARAM_READWRITE));
186   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATARATE,
187       g_param_spec_int ("datarate", "Datarate",
188           "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
189           0, G_MAXINT, DEFAULT_DATARATE, G_PARAM_READWRITE));
190   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SILENT,
191       g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
192           G_PARAM_READWRITE));
193   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SINGLE_SEGMENT,
194       g_param_spec_boolean ("single-segment", "Single Segment",
195           "Timestamp buffers and eat newsegments so as to appear as one segment",
196           DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE));
197   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE,
198       g_param_spec_string ("last-message", "last-message", "last-message", NULL,
199           G_PARAM_READABLE));
200   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DUMP,
201       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents",
202           DEFAULT_DUMP, G_PARAM_READWRITE));
203   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
204       g_param_spec_boolean ("sync", "Synchronize",
205           "Synchronize to pipeline clock", DEFAULT_SYNC, G_PARAM_READWRITE));
206   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHECK_PERFECT,
207       g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
208           "Verify that the stream is time- and data-contiguous",
209           DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE));
210
211   gst_identity_signals[SIGNAL_HANDOFF] =
212       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
213       G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
214       marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
215
216   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_identity_finalize);
217
218   gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_identity_event);
219   gstbasetrans_class->transform_ip =
220       GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
221   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
222   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
223 }
224
225 static void
226 gst_identity_init (GstIdentity * identity, GstIdentityClass * g_class)
227 {
228   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
229
230   identity->sleep_time = DEFAULT_SLEEP_TIME;
231   identity->error_after = DEFAULT_ERROR_AFTER;
232   identity->drop_probability = DEFAULT_DROP_PROBABILITY;
233   identity->datarate = DEFAULT_DATARATE;
234   identity->silent = DEFAULT_SILENT;
235   identity->single_segment = DEFAULT_SINGLE_SEGMENT;
236   identity->sync = DEFAULT_SYNC;
237   identity->check_perfect = DEFAULT_CHECK_PERFECT;
238   identity->dump = DEFAULT_DUMP;
239   identity->last_message = NULL;
240 }
241
242 static gboolean
243 gst_identity_event (GstBaseTransform * trans, GstEvent * event)
244 {
245   GstIdentity *identity;
246   gboolean ret = TRUE;
247
248   identity = GST_IDENTITY (trans);
249
250   if (!identity->silent) {
251     const GstStructure *s;
252     gchar *sstr;
253
254     GST_OBJECT_LOCK (identity);
255     g_free (identity->last_message);
256
257     if ((s = gst_event_get_structure (event)))
258       sstr = gst_structure_to_string (s);
259     else
260       sstr = g_strdup ("");
261
262     identity->last_message =
263         g_strdup_printf ("event   ******* (%s:%s) E (type: %d, %s) %p",
264         GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
265         event);
266     g_free (sstr);
267     GST_OBJECT_UNLOCK (identity);
268
269     g_object_notify (G_OBJECT (identity), "last_message");
270   }
271
272   if (identity->single_segment
273       && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
274     if (trans->have_newsegment == FALSE) {
275       GstEvent *news;
276       GstFormat format;
277
278       gst_event_parse_new_segment (event, NULL, NULL, &format, NULL, NULL,
279           NULL);
280
281       /* This is the first newsegment, send out a (0, -1) newsegment */
282       news = gst_event_new_new_segment (TRUE, 1.0, format, 0, -1, 0);
283
284       if (!(gst_pad_event_default (trans->sinkpad, news)))
285         return FALSE;
286     }
287   }
288
289   GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
290
291   if (identity->single_segment
292       && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
293     /* eat up segments */
294     ret = FALSE;
295   }
296
297   return ret;
298 }
299
300 static void
301 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
302 {
303   GstClockTime timestamp;
304
305   timestamp = GST_BUFFER_TIMESTAMP (buf);
306
307   /* see if we need to do perfect stream checking */
308   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
309   if (timestamp != GST_CLOCK_TIME_NONE) {
310     /* check if we had a previous buffer to compare to */
311     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE) {
312       guint64 offset;
313
314       if (identity->prev_timestamp + identity->prev_duration != timestamp) {
315         GST_WARNING_OBJECT (identity,
316             "Buffer not time-contiguous with previous one: " "prev ts %"
317             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
318             GST_TIME_FORMAT, GST_TIME_ARGS (identity->prev_timestamp),
319             GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp));
320       }
321
322       offset = GST_BUFFER_OFFSET (buf);
323       if (identity->prev_offset_end != offset) {
324         GST_WARNING_OBJECT (identity,
325             "Buffer not data-contiguous with previous one: "
326             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
327             G_GINT64_FORMAT, identity->prev_offset_end, offset);
328       }
329     }
330     /* update prev values */
331     identity->prev_timestamp = timestamp;
332     identity->prev_duration = GST_BUFFER_DURATION (buf);
333     identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
334   }
335 }
336
337 static GstFlowReturn
338 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
339 {
340   GstFlowReturn ret = GST_FLOW_OK;
341   GstIdentity *identity = GST_IDENTITY (trans);
342   GstClockTime runtimestamp = 0LL;
343
344   if (identity->check_perfect)
345     gst_identity_check_perfect (identity, buf);
346
347   if (identity->error_after >= 0) {
348     identity->error_after--;
349     if (identity->error_after == 0) {
350       GST_ELEMENT_ERROR (identity, CORE, FAILED,
351           (_("Failed after iterations as requested.")), (NULL));
352       return GST_FLOW_ERROR;
353     }
354   }
355
356   if (identity->drop_probability > 0.0) {
357     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability) {
358       GST_OBJECT_LOCK (identity);
359       g_free (identity->last_message);
360       identity->last_message =
361           g_strdup_printf ("dropping   ******* (%s:%s)i (%d bytes, timestamp: %"
362           GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
363           G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
364           GST_DEBUG_PAD_NAME (trans->sinkpad), GST_BUFFER_SIZE (buf),
365           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
366           GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
367           GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
368           GST_BUFFER_FLAGS (buf), buf);
369       GST_OBJECT_UNLOCK (identity);
370       g_object_notify (G_OBJECT (identity), "last-message");
371       return GST_FLOW_OK;
372     }
373   }
374
375   if (identity->dump) {
376     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
377   }
378
379   if (!identity->silent) {
380     GST_OBJECT_LOCK (identity);
381     g_free (identity->last_message);
382     identity->last_message =
383         g_strdup_printf ("chain   ******* (%s:%s)i (%d bytes, timestamp: %"
384         GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
385         G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
386         GST_DEBUG_PAD_NAME (trans->sinkpad), GST_BUFFER_SIZE (buf),
387         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
388         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
389         GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
390         GST_BUFFER_FLAGS (buf), buf);
391     GST_OBJECT_UNLOCK (identity);
392     g_object_notify (G_OBJECT (identity), "last-message");
393   }
394
395   if (identity->datarate > 0) {
396     GstClockTime time = identity->offset * GST_SECOND / identity->datarate;
397
398     GST_BUFFER_TIMESTAMP (buf) = time;
399     GST_BUFFER_DURATION (buf) =
400         GST_BUFFER_SIZE (buf) * GST_SECOND / identity->datarate;
401   }
402
403   g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
404       buf);
405
406   if (trans->segment.format == GST_FORMAT_TIME)
407     runtimestamp = gst_segment_to_running_time (&trans->segment,
408         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
409
410   if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
411     GstClock *clock;
412
413     GST_OBJECT_LOCK (identity);
414     if ((clock = GST_ELEMENT (identity)->clock)) {
415       GstClockReturn cret;
416       GstClockTime timestamp;
417
418       timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
419
420       /* save id if we need to unlock */
421       /* FIXME: actually unlock this somewhere in the state changes */
422       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
423       GST_OBJECT_UNLOCK (identity);
424
425       cret = gst_clock_id_wait (identity->clock_id, NULL);
426
427       GST_OBJECT_LOCK (identity);
428       if (identity->clock_id) {
429         gst_clock_id_unref (identity->clock_id);
430         identity->clock_id = NULL;
431       }
432       if (cret == GST_CLOCK_UNSCHEDULED)
433         ret = GST_FLOW_UNEXPECTED;
434     }
435     GST_OBJECT_UNLOCK (identity);
436   }
437
438   identity->offset += GST_BUFFER_SIZE (buf);
439
440   if (identity->sleep_time && ret == GST_FLOW_OK)
441     g_usleep (identity->sleep_time);
442
443   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
444       && (ret == GST_FLOW_OK))
445     GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
446
447   return ret;
448 }
449
450 static void
451 gst_identity_set_property (GObject * object, guint prop_id,
452     const GValue * value, GParamSpec * pspec)
453 {
454   GstIdentity *identity;
455
456   identity = GST_IDENTITY (object);
457
458   switch (prop_id) {
459     case PROP_SLEEP_TIME:
460       identity->sleep_time = g_value_get_uint (value);
461       break;
462     case PROP_SILENT:
463       identity->silent = g_value_get_boolean (value);
464       break;
465     case PROP_SINGLE_SEGMENT:
466       identity->single_segment = g_value_get_boolean (value);
467       break;
468     case PROP_DUMP:
469       identity->dump = g_value_get_boolean (value);
470       break;
471     case PROP_ERROR_AFTER:
472       identity->error_after = g_value_get_int (value);
473       break;
474     case PROP_DROP_PROBABILITY:
475       identity->drop_probability = g_value_get_float (value);
476       break;
477     case PROP_DATARATE:
478       identity->datarate = g_value_get_int (value);
479       break;
480     case PROP_SYNC:
481       identity->sync = g_value_get_boolean (value);
482       break;
483     case PROP_CHECK_PERFECT:
484       identity->check_perfect = g_value_get_boolean (value);
485       break;
486     default:
487       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488       break;
489   }
490 }
491
492 static void
493 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
494     GParamSpec * pspec)
495 {
496   GstIdentity *identity;
497
498   identity = GST_IDENTITY (object);
499
500   switch (prop_id) {
501     case PROP_SLEEP_TIME:
502       g_value_set_uint (value, identity->sleep_time);
503       break;
504     case PROP_ERROR_AFTER:
505       g_value_set_int (value, identity->error_after);
506       break;
507     case PROP_DROP_PROBABILITY:
508       g_value_set_float (value, identity->drop_probability);
509       break;
510     case PROP_DATARATE:
511       g_value_set_int (value, identity->datarate);
512       break;
513     case PROP_SILENT:
514       g_value_set_boolean (value, identity->silent);
515       break;
516     case PROP_SINGLE_SEGMENT:
517       g_value_set_boolean (value, identity->single_segment);
518       break;
519     case PROP_DUMP:
520       g_value_set_boolean (value, identity->dump);
521       break;
522     case PROP_LAST_MESSAGE:
523       GST_OBJECT_LOCK (identity);
524       g_value_set_string (value, identity->last_message);
525       GST_OBJECT_UNLOCK (identity);
526       break;
527     case PROP_SYNC:
528       g_value_set_boolean (value, identity->sync);
529       break;
530     case PROP_CHECK_PERFECT:
531       g_value_set_boolean (value, identity->check_perfect);
532       break;
533     default:
534       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
535       break;
536   }
537 }
538
539 static gboolean
540 gst_identity_start (GstBaseTransform * trans)
541 {
542   GstIdentity *identity;
543
544   identity = GST_IDENTITY (trans);
545
546   identity->offset = 0;
547   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
548   identity->prev_duration = GST_CLOCK_TIME_NONE;
549   identity->prev_offset_end = -1;
550
551   return TRUE;
552 }
553
554 static gboolean
555 gst_identity_stop (GstBaseTransform * trans)
556 {
557   GstIdentity *identity;
558
559   identity = GST_IDENTITY (trans);
560
561   GST_OBJECT_LOCK (identity);
562   g_free (identity->last_message);
563   identity->last_message = NULL;
564   GST_OBJECT_UNLOCK (identity);
565
566   return TRUE;
567 }