eb2f6b57b731772366409f4b975798746d89c866
[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  * SECTION:element-identity
25  *
26  * Dummy element that passes incoming data through unmodified. It has some
27  * useful diagnostic functions, such as offset and timestamp checking.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "../../gst/gst-i18n-lib.h"
38 #include "gstidentity.h"
39 #include <gst/gstmarshal.h>
40
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS_ANY);
45
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS_ANY);
50
51 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
52 #define GST_CAT_DEFAULT gst_identity_debug
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                  TRUE
68 #define DEFAULT_SINGLE_SEGMENT          FALSE
69 #define DEFAULT_DUMP                    FALSE
70 #define DEFAULT_SYNC                    FALSE
71 #define DEFAULT_CHECK_PERFECT           FALSE
72 #define DEFAULT_CHECK_IMPERFECT_TIMESTAMP FALSE
73 #define DEFAULT_CHECK_IMPERFECT_OFFSET    FALSE
74 #define DEFAULT_SIGNAL_HANDOFFS           TRUE
75
76 enum
77 {
78   PROP_0,
79   PROP_SLEEP_TIME,
80   PROP_ERROR_AFTER,
81   PROP_DROP_PROBABILITY,
82   PROP_DATARATE,
83   PROP_SILENT,
84   PROP_SINGLE_SEGMENT,
85   PROP_LAST_MESSAGE,
86   PROP_DUMP,
87   PROP_SYNC,
88   PROP_CHECK_PERFECT,
89   PROP_CHECK_IMPERFECT_TIMESTAMP,
90   PROP_CHECK_IMPERFECT_OFFSET,
91   PROP_SIGNAL_HANDOFFS
92 };
93
94
95 #define _do_init \
96     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
97 #define gst_identity_parent_class parent_class
98 G_DEFINE_TYPE_WITH_CODE (GstIdentity, gst_identity, GST_TYPE_BASE_TRANSFORM,
99     _do_init);
100
101 static void gst_identity_finalize (GObject * object);
102 static void gst_identity_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_identity_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106
107 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
108     GstEvent * event);
109 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
110     GstBuffer * buf);
111 static gboolean gst_identity_start (GstBaseTransform * trans);
112 static gboolean gst_identity_stop (GstBaseTransform * trans);
113 static GstStateChangeReturn gst_identity_change_state (GstElement * element,
114     GstStateChange transition);
115
116 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
117
118 static GParamSpec *pspec_last_message = NULL;
119
120 static void
121 gst_identity_finalize (GObject * object)
122 {
123   GstIdentity *identity;
124
125   identity = GST_IDENTITY (object);
126
127   g_free (identity->last_message);
128
129   G_OBJECT_CLASS (parent_class)->finalize (object);
130 }
131
132 static void
133 gst_identity_class_init (GstIdentityClass * klass)
134 {
135   GObjectClass *gobject_class;
136   GstElementClass *gstelement_class;
137   GstBaseTransformClass *gstbasetrans_class;
138
139   gobject_class = G_OBJECT_CLASS (klass);
140   gstelement_class = GST_ELEMENT_CLASS (klass);
141   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
142
143   gobject_class->set_property = gst_identity_set_property;
144   gobject_class->get_property = gst_identity_get_property;
145
146   g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
147       g_param_spec_uint ("sleep-time", "Sleep time",
148           "Microseconds to sleep between processing", 0, G_MAXUINT,
149           DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150   g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
151       g_param_spec_int ("error-after", "Error After", "Error after N buffers",
152           G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER,
153           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154   g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
155       g_param_spec_float ("drop-probability", "Drop Probability",
156           "The Probability a buffer is dropped", 0.0, 1.0,
157           DEFAULT_DROP_PROBABILITY,
158           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159   g_object_class_install_property (gobject_class, PROP_DATARATE,
160       g_param_spec_int ("datarate", "Datarate",
161           "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
162           0, G_MAXINT, DEFAULT_DATARATE,
163           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164   g_object_class_install_property (gobject_class, PROP_SILENT,
165       g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
166           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167   g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
168       g_param_spec_boolean ("single-segment", "Single Segment",
169           "Timestamp buffers and eat segments so as to appear as one segment",
170           DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171   pspec_last_message = g_param_spec_string ("last-message", "last-message",
172       "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
173   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
174       pspec_last_message);
175   g_object_class_install_property (gobject_class, PROP_DUMP,
176       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
177           DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178   g_object_class_install_property (gobject_class, PROP_SYNC,
179       g_param_spec_boolean ("sync", "Synchronize",
180           "Synchronize to pipeline clock", DEFAULT_SYNC,
181           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182   g_object_class_install_property (gobject_class, PROP_CHECK_PERFECT,
183       g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
184           "Verify that the stream is time- and data-contiguous. "
185           "This only logs in the debug log.  This will be deprecated in favor "
186           "of the check-imperfect-timestamp/offset properties.",
187           DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188   g_object_class_install_property (gobject_class,
189       PROP_CHECK_IMPERFECT_TIMESTAMP,
190       g_param_spec_boolean ("check-imperfect-timestamp",
191           "Check for discontiguous timestamps",
192           "Send element messages if timestamps and durations do not match up",
193           DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
194           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195   g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
196       g_param_spec_boolean ("check-imperfect-offset",
197           "Check for discontiguous offset",
198           "Send element messages if offset and offset_end do not match up",
199           DEFAULT_CHECK_IMPERFECT_OFFSET,
200           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201
202   /**
203    * GstIdentity:signal-handoffs
204    *
205    * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
206    * When set to #FALSE, no signal will be emited, which might improve performance.
207    *
208    * Since: 0.10.16
209    */
210   g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
211       g_param_spec_boolean ("signal-handoffs",
212           "Signal handoffs", "Send a signal before pushing the buffer",
213           DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214
215   /**
216    * GstIdentity::handoff:
217    * @identity: the identity instance
218    * @buffer: the buffer that just has been received
219    * @pad: the pad that received it
220    *
221    * This signal gets emitted before passing the buffer downstream.
222    */
223   gst_identity_signals[SIGNAL_HANDOFF] =
224       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
225       G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
226       g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1,
227       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
228
229   gobject_class->finalize = gst_identity_finalize;
230
231   gst_element_class_set_details_simple (gstelement_class,
232       "Identity",
233       "Generic",
234       "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
235   gst_element_class_add_pad_template (gstelement_class,
236       gst_static_pad_template_get (&srctemplate));
237   gst_element_class_add_pad_template (gstelement_class,
238       gst_static_pad_template_get (&sinktemplate));
239
240   gstelement_class->change_state =
241       GST_DEBUG_FUNCPTR (gst_identity_change_state);
242
243   gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
244   gstbasetrans_class->transform_ip =
245       GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
246   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
247   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
248 }
249
250 static void
251 gst_identity_init (GstIdentity * identity)
252 {
253   identity->sleep_time = DEFAULT_SLEEP_TIME;
254   identity->error_after = DEFAULT_ERROR_AFTER;
255   identity->drop_probability = DEFAULT_DROP_PROBABILITY;
256   identity->datarate = DEFAULT_DATARATE;
257   identity->silent = DEFAULT_SILENT;
258   identity->single_segment = DEFAULT_SINGLE_SEGMENT;
259   identity->sync = DEFAULT_SYNC;
260   identity->check_perfect = DEFAULT_CHECK_PERFECT;
261   identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
262   identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
263   identity->dump = DEFAULT_DUMP;
264   identity->last_message = NULL;
265   identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
266
267   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
268 }
269
270 static void
271 gst_identity_notify_last_message (GstIdentity * identity)
272 {
273   g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
274 }
275
276 static gboolean
277 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
278 {
279   GstIdentity *identity;
280   gboolean ret = TRUE;
281
282   identity = GST_IDENTITY (trans);
283
284   if (!identity->silent) {
285     const GstStructure *s;
286     const gchar *tstr;
287     gchar *sstr;
288
289     GST_OBJECT_LOCK (identity);
290     g_free (identity->last_message);
291
292     tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
293     if ((s = gst_event_get_structure (event)))
294       sstr = gst_structure_to_string (s);
295     else
296       sstr = g_strdup ("");
297
298     identity->last_message =
299         g_strdup_printf ("event   ******* (%s:%s) E (type: %s (%d), %s) %p",
300         GST_DEBUG_PAD_NAME (trans->sinkpad), tstr, GST_EVENT_TYPE (event),
301         sstr, event);
302     g_free (sstr);
303     GST_OBJECT_UNLOCK (identity);
304
305     gst_identity_notify_last_message (identity);
306   }
307
308   if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
309     if (trans->have_segment == FALSE) {
310       GstEvent *news;
311       GstSegment segment;
312
313       gst_event_copy_segment (event, &segment);
314       gst_event_copy_segment (event, &trans->segment);
315       trans->have_segment = TRUE;
316
317       /* This is the first segment, send out a (0, -1) segment */
318       gst_segment_init (&segment, segment.format);
319       news = gst_event_new_segment (&segment);
320
321       gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
322     }
323   }
324
325   /* Reset previous timestamp, duration and offsets on NEWSEGMENT
326    * to prevent false warnings when checking for perfect streams */
327   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
328     identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
329     identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
330   }
331
332   if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
333     /* eat up segments */
334     gst_event_unref (event);
335     ret = TRUE;
336   } else {
337     if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
338       GST_OBJECT_LOCK (identity);
339       if (identity->clock_id) {
340         GST_DEBUG_OBJECT (identity, "unlock clock wait");
341         gst_clock_id_unschedule (identity->clock_id);
342         gst_clock_id_unref (identity->clock_id);
343         identity->clock_id = NULL;
344       }
345       GST_OBJECT_UNLOCK (identity);
346     }
347
348     ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
349   }
350
351   return ret;
352 }
353
354 static void
355 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
356 {
357   GstClockTime timestamp;
358
359   timestamp = GST_BUFFER_TIMESTAMP (buf);
360
361   /* see if we need to do perfect stream checking */
362   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
363   if (timestamp != GST_CLOCK_TIME_NONE) {
364     /* check if we had a previous buffer to compare to */
365     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
366         identity->prev_duration != GST_CLOCK_TIME_NONE) {
367       guint64 offset, t_expected;
368       gint64 dt;
369
370       t_expected = identity->prev_timestamp + identity->prev_duration;
371       dt = timestamp - t_expected;
372       if (dt != 0) {
373         GST_WARNING_OBJECT (identity,
374             "Buffer not time-contiguous with previous one: " "prev ts %"
375             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
376             GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
377             GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
378             GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
379             GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
380             GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
381       }
382
383       offset = GST_BUFFER_OFFSET (buf);
384       if (identity->prev_offset_end != offset &&
385           identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
386           offset != GST_BUFFER_OFFSET_NONE) {
387         GST_WARNING_OBJECT (identity,
388             "Buffer not data-contiguous with previous one: "
389             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
390             G_GINT64_FORMAT, identity->prev_offset_end, offset);
391       }
392     } else {
393       GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
394           "and/or duration were set on previous buffer");
395     }
396   }
397 }
398
399 static void
400 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
401 {
402   GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
403
404   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
405   if (timestamp != GST_CLOCK_TIME_NONE) {
406     /* check if we had a previous buffer to compare to */
407     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
408         identity->prev_duration != GST_CLOCK_TIME_NONE) {
409       GstClockTime t_expected;
410       GstClockTimeDiff dt;
411
412       t_expected = identity->prev_timestamp + identity->prev_duration;
413       dt = GST_CLOCK_DIFF (t_expected, timestamp);
414       if (dt != 0) {
415         /*
416          * "imperfect-timestamp" bus message:
417          * @identity:        the identity instance
418          * @prev-timestamp:  the previous buffer timestamp
419          * @prev-duration:   the previous buffer duration
420          * @prev-offset:     the previous buffer offset
421          * @prev-offset-end: the previous buffer offset end
422          * @cur-timestamp:   the current buffer timestamp
423          * @cur-duration:    the current buffer duration
424          * @cur-offset:      the current buffer offset
425          * @cur-offset-end:  the current buffer offset end
426          *
427          * This bus message gets emitted if the check-imperfect-timestamp
428          * property is set and there is a gap in time between the
429          * last buffer and the newly received buffer.
430          */
431         gst_element_post_message (GST_ELEMENT (identity),
432             gst_message_new_element (GST_OBJECT (identity),
433                 gst_structure_new ("imperfect-timestamp",
434                     "prev-timestamp", G_TYPE_UINT64,
435                     identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
436                     identity->prev_duration, "prev-offset", G_TYPE_UINT64,
437                     identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
438                     identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
439                     timestamp, "cur-duration", G_TYPE_UINT64,
440                     GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
441                     GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
442                     GST_BUFFER_OFFSET_END (buf), NULL)));
443       }
444     } else {
445       GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
446           "offset_end was set on previous buffer");
447     }
448   }
449 }
450
451 static void
452 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
453 {
454   guint64 offset;
455
456   offset = GST_BUFFER_OFFSET (buf);
457
458   if (identity->prev_offset_end != offset &&
459       identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
460       offset != GST_BUFFER_OFFSET_NONE) {
461     /*
462      * "imperfect-offset" bus message:
463      * @identity:        the identity instance
464      * @prev-timestamp:  the previous buffer timestamp
465      * @prev-duration:   the previous buffer duration
466      * @prev-offset:     the previous buffer offset
467      * @prev-offset-end: the previous buffer offset end
468      * @cur-timestamp:   the current buffer timestamp
469      * @cur-duration:    the current buffer duration
470      * @cur-offset:      the current buffer offset
471      * @cur-offset-end:  the current buffer offset end
472      *
473      * This bus message gets emitted if the check-imperfect-offset
474      * property is set and there is a gap in offsets between the
475      * last buffer and the newly received buffer.
476      */
477     gst_element_post_message (GST_ELEMENT (identity),
478         gst_message_new_element (GST_OBJECT (identity),
479             gst_structure_new ("imperfect-offset", "prev-timestamp",
480                 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
481                 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
482                 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
483                 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
484                 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
485                 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
486                 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
487                 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
488   } else {
489     GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
490         "and/or offset_end were set on previous buffer");
491   }
492 }
493
494 static const gchar *
495 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
496 {
497   if (ts == GST_CLOCK_TIME_NONE)
498     return "none";
499
500   g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
501   return ts_str;
502 }
503
504 static void
505 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
506     const gchar * action, GstBuffer * buf, gsize size)
507 {
508   gchar ts_str[64], dur_str[64];
509   gchar flag_str[100];
510
511   GST_OBJECT_LOCK (identity);
512
513   {
514     const char *flag_list[15] = {
515       "", "", "", "", "live", "decode-only", "discont", "resync", "corrupted",
516       "marker", "header", "gap", "droppable", "delta-unit", "in-caps"
517     };
518     int i;
519     char *end = flag_str;
520     end[0] = '\0';
521     for (i = 0; i < G_N_ELEMENTS (flag_list); i++) {
522       if (GST_MINI_OBJECT_CAST (buf)->flags & (1 << i)) {
523         strcpy (end, flag_list[i]);
524         end += strlen (end);
525         end[0] = ' ';
526         end[1] = '\0';
527         end++;
528       }
529     }
530   }
531
532   g_free (identity->last_message);
533   identity->last_message = g_strdup_printf ("%s   ******* (%s:%s) "
534       "(%" G_GSIZE_FORMAT " bytes, timestamp: %s, duration: %s, offset: %"
535       G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
536       ", flags: %d %s) %p", action,
537       GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
538       print_pretty_time (ts_str, sizeof (ts_str), GST_BUFFER_TIMESTAMP (buf)),
539       print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
540       GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
541       GST_BUFFER_FLAGS (buf), flag_str, buf);
542
543   GST_OBJECT_UNLOCK (identity);
544
545   gst_identity_notify_last_message (identity);
546 }
547
548 static GstFlowReturn
549 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
550 {
551   GstFlowReturn ret = GST_FLOW_OK;
552   GstIdentity *identity = GST_IDENTITY (trans);
553   GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
554   gsize size;
555
556   size = gst_buffer_get_size (buf);
557
558   if (identity->check_perfect)
559     gst_identity_check_perfect (identity, buf);
560   if (identity->check_imperfect_timestamp)
561     gst_identity_check_imperfect_timestamp (identity, buf);
562   if (identity->check_imperfect_offset)
563     gst_identity_check_imperfect_offset (identity, buf);
564
565   /* update prev values */
566   identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
567   identity->prev_duration = GST_BUFFER_DURATION (buf);
568   identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
569   identity->prev_offset = GST_BUFFER_OFFSET (buf);
570
571   if (identity->error_after >= 0) {
572     identity->error_after--;
573     if (identity->error_after == 0)
574       goto error_after;
575   }
576
577   if (identity->drop_probability > 0.0) {
578     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
579       goto dropped;
580   }
581
582   if (identity->dump) {
583     GstMapInfo info;
584
585     gst_buffer_map (buf, &info, GST_MAP_READ);
586     gst_util_dump_mem (info.data, info.size);
587     gst_buffer_unmap (buf, &info);
588   }
589
590   if (!identity->silent) {
591     gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
592   }
593
594   if (identity->datarate > 0) {
595     GstClockTime time = gst_util_uint64_scale_int (identity->offset,
596         GST_SECOND, identity->datarate);
597
598     GST_BUFFER_TIMESTAMP (buf) = time;
599     GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
600   }
601
602   if (identity->signal_handoffs)
603     g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
604
605   if (trans->segment.format == GST_FORMAT_TIME)
606     runtimestamp = gst_segment_to_running_time (&trans->segment,
607         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
608
609   if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
610     GstClock *clock;
611
612     GST_OBJECT_LOCK (identity);
613     if ((clock = GST_ELEMENT (identity)->clock)) {
614       GstClockReturn cret;
615       GstClockTime timestamp;
616
617       timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
618
619       /* save id if we need to unlock */
620       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
621       GST_OBJECT_UNLOCK (identity);
622
623       cret = gst_clock_id_wait (identity->clock_id, NULL);
624
625       GST_OBJECT_LOCK (identity);
626       if (identity->clock_id) {
627         gst_clock_id_unref (identity->clock_id);
628         identity->clock_id = NULL;
629       }
630       if (cret == GST_CLOCK_UNSCHEDULED)
631         ret = GST_FLOW_EOS;
632     }
633     GST_OBJECT_UNLOCK (identity);
634   }
635
636   identity->offset += size;
637
638   if (identity->sleep_time && ret == GST_FLOW_OK)
639     g_usleep (identity->sleep_time);
640
641   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
642       && (ret == GST_FLOW_OK)) {
643     GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
644     GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
645     GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
646   }
647
648   return ret;
649
650   /* ERRORS */
651 error_after:
652   {
653     GST_ELEMENT_ERROR (identity, CORE, FAILED,
654         (_("Failed after iterations as requested.")), (NULL));
655     return GST_FLOW_ERROR;
656   }
657 dropped:
658   {
659     if (!identity->silent) {
660       gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
661           size);
662     }
663     /* return DROPPED to basetransform. */
664     return GST_BASE_TRANSFORM_FLOW_DROPPED;
665   }
666 }
667
668 static void
669 gst_identity_set_property (GObject * object, guint prop_id,
670     const GValue * value, GParamSpec * pspec)
671 {
672   GstIdentity *identity;
673
674   identity = GST_IDENTITY (object);
675
676   switch (prop_id) {
677     case PROP_SLEEP_TIME:
678       identity->sleep_time = g_value_get_uint (value);
679       break;
680     case PROP_SILENT:
681       identity->silent = g_value_get_boolean (value);
682       break;
683     case PROP_SINGLE_SEGMENT:
684       identity->single_segment = g_value_get_boolean (value);
685       break;
686     case PROP_DUMP:
687       identity->dump = g_value_get_boolean (value);
688       break;
689     case PROP_ERROR_AFTER:
690       identity->error_after = g_value_get_int (value);
691       break;
692     case PROP_DROP_PROBABILITY:
693       identity->drop_probability = g_value_get_float (value);
694       break;
695     case PROP_DATARATE:
696       identity->datarate = g_value_get_int (value);
697       break;
698     case PROP_SYNC:
699       identity->sync = g_value_get_boolean (value);
700       break;
701     case PROP_CHECK_PERFECT:
702       identity->check_perfect = g_value_get_boolean (value);
703       break;
704     case PROP_CHECK_IMPERFECT_TIMESTAMP:
705       identity->check_imperfect_timestamp = g_value_get_boolean (value);
706       break;
707     case PROP_CHECK_IMPERFECT_OFFSET:
708       identity->check_imperfect_offset = g_value_get_boolean (value);
709       break;
710     case PROP_SIGNAL_HANDOFFS:
711       identity->signal_handoffs = g_value_get_boolean (value);
712       break;
713     default:
714       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
715       break;
716   }
717   if (identity->datarate > 0 || identity->single_segment)
718     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
719   else
720     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
721 }
722
723 static void
724 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
725     GParamSpec * pspec)
726 {
727   GstIdentity *identity;
728
729   identity = GST_IDENTITY (object);
730
731   switch (prop_id) {
732     case PROP_SLEEP_TIME:
733       g_value_set_uint (value, identity->sleep_time);
734       break;
735     case PROP_ERROR_AFTER:
736       g_value_set_int (value, identity->error_after);
737       break;
738     case PROP_DROP_PROBABILITY:
739       g_value_set_float (value, identity->drop_probability);
740       break;
741     case PROP_DATARATE:
742       g_value_set_int (value, identity->datarate);
743       break;
744     case PROP_SILENT:
745       g_value_set_boolean (value, identity->silent);
746       break;
747     case PROP_SINGLE_SEGMENT:
748       g_value_set_boolean (value, identity->single_segment);
749       break;
750     case PROP_DUMP:
751       g_value_set_boolean (value, identity->dump);
752       break;
753     case PROP_LAST_MESSAGE:
754       GST_OBJECT_LOCK (identity);
755       g_value_set_string (value, identity->last_message);
756       GST_OBJECT_UNLOCK (identity);
757       break;
758     case PROP_SYNC:
759       g_value_set_boolean (value, identity->sync);
760       break;
761     case PROP_CHECK_PERFECT:
762       g_value_set_boolean (value, identity->check_perfect);
763       break;
764     case PROP_CHECK_IMPERFECT_TIMESTAMP:
765       g_value_set_boolean (value, identity->check_imperfect_timestamp);
766       break;
767     case PROP_CHECK_IMPERFECT_OFFSET:
768       g_value_set_boolean (value, identity->check_imperfect_offset);
769       break;
770     case PROP_SIGNAL_HANDOFFS:
771       g_value_set_boolean (value, identity->signal_handoffs);
772       break;
773     default:
774       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
775       break;
776   }
777 }
778
779 static gboolean
780 gst_identity_start (GstBaseTransform * trans)
781 {
782   GstIdentity *identity;
783
784   identity = GST_IDENTITY (trans);
785
786   identity->offset = 0;
787   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
788   identity->prev_duration = GST_CLOCK_TIME_NONE;
789   identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
790   identity->prev_offset = GST_BUFFER_OFFSET_NONE;
791
792   return TRUE;
793 }
794
795 static gboolean
796 gst_identity_stop (GstBaseTransform * trans)
797 {
798   GstIdentity *identity;
799
800   identity = GST_IDENTITY (trans);
801
802   GST_OBJECT_LOCK (identity);
803   g_free (identity->last_message);
804   identity->last_message = NULL;
805   GST_OBJECT_UNLOCK (identity);
806
807   return TRUE;
808 }
809
810 static GstStateChangeReturn
811 gst_identity_change_state (GstElement * element, GstStateChange transition)
812 {
813   GstStateChangeReturn ret;
814   GstIdentity *identity = GST_IDENTITY (element);
815
816   switch (transition) {
817     case GST_STATE_CHANGE_NULL_TO_READY:
818       break;
819     case GST_STATE_CHANGE_READY_TO_PAUSED:
820       break;
821     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
822       break;
823     case GST_STATE_CHANGE_PAUSED_TO_READY:
824       GST_OBJECT_LOCK (identity);
825       if (identity->clock_id) {
826         GST_DEBUG_OBJECT (identity, "unlock clock wait");
827         gst_clock_id_unschedule (identity->clock_id);
828         gst_clock_id_unref (identity->clock_id);
829         identity->clock_id = NULL;
830       }
831       GST_OBJECT_UNLOCK (identity);
832       break;
833     default:
834       break;
835   }
836
837   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
838
839   switch (transition) {
840     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
841       break;
842     case GST_STATE_CHANGE_PAUSED_TO_READY:
843       break;
844     case GST_STATE_CHANGE_READY_TO_NULL:
845       break;
846     default:
847       break;
848   }
849
850   return ret;
851 }