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