Add new GstMapInfo
[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     gchar *sstr;
317
318     GST_OBJECT_LOCK (identity);
319     g_free (identity->last_message);
320
321     if ((s = gst_event_get_structure (event)))
322       sstr = gst_structure_to_string (s);
323     else
324       sstr = g_strdup ("");
325
326     identity->last_message =
327         g_strdup_printf ("event   ******* (%s:%s) E (type: %d, %s) %p",
328         GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
329         event);
330     g_free (sstr);
331     GST_OBJECT_UNLOCK (identity);
332
333     gst_identity_notify_last_message (identity);
334   }
335
336   if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
337     if (trans->have_segment == FALSE) {
338       GstEvent *news;
339       GstSegment segment;
340
341       gst_event_copy_segment (event, &segment);
342       gst_event_copy_segment (event, &trans->segment);
343       trans->have_segment = TRUE;
344
345       /* This is the first segment, send out a (0, -1) segment */
346       gst_segment_init (&segment, segment.format);
347       news = gst_event_new_segment (&segment);
348
349       gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
350     }
351   }
352
353   /* Reset previous timestamp, duration and offsets on NEWSEGMENT
354    * to prevent false warnings when checking for perfect streams */
355   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
356     identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
357     identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
358   }
359
360   if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
361     /* eat up segments */
362     gst_event_unref (event);
363     ret = TRUE;
364   } else {
365     if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
366       GST_OBJECT_LOCK (identity);
367       if (identity->clock_id) {
368         GST_DEBUG_OBJECT (identity, "unlock clock wait");
369         gst_clock_id_unschedule (identity->clock_id);
370         gst_clock_id_unref (identity->clock_id);
371         identity->clock_id = NULL;
372       }
373       GST_OBJECT_UNLOCK (identity);
374     }
375
376     ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
377   }
378
379   return ret;
380 }
381
382 static GstFlowReturn
383 gst_identity_prepare_output_buffer (GstBaseTransform * trans,
384     GstBuffer * in_buf, GstBuffer ** out_buf)
385 {
386   GstIdentity *identity = GST_IDENTITY (trans);
387
388   /* only bother if we may have to alter metadata */
389   if (identity->datarate > 0 || identity->single_segment) {
390     if (gst_buffer_is_writable (in_buf))
391       /* reuse */
392       *out_buf = in_buf;
393     else {
394       /* copy */
395       *out_buf = gst_buffer_copy (in_buf);
396     }
397   } else
398     *out_buf = in_buf;
399
400   return GST_FLOW_OK;
401 }
402
403 static void
404 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
405 {
406   GstClockTime timestamp;
407
408   timestamp = GST_BUFFER_TIMESTAMP (buf);
409
410   /* see if we need to do perfect stream checking */
411   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
412   if (timestamp != GST_CLOCK_TIME_NONE) {
413     /* check if we had a previous buffer to compare to */
414     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
415         identity->prev_duration != GST_CLOCK_TIME_NONE) {
416       guint64 offset, t_expected;
417       gint64 dt;
418
419       t_expected = identity->prev_timestamp + identity->prev_duration;
420       dt = timestamp - t_expected;
421       if (dt != 0) {
422         GST_WARNING_OBJECT (identity,
423             "Buffer not time-contiguous with previous one: " "prev ts %"
424             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
425             GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
426             GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
427             GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
428             GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
429             GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
430       }
431
432       offset = GST_BUFFER_OFFSET (buf);
433       if (identity->prev_offset_end != offset &&
434           identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
435           offset != GST_BUFFER_OFFSET_NONE) {
436         GST_WARNING_OBJECT (identity,
437             "Buffer not data-contiguous with previous one: "
438             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
439             G_GINT64_FORMAT, identity->prev_offset_end, offset);
440       }
441     } else {
442       GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
443           "and/or duration were set on previous buffer");
444     }
445   }
446 }
447
448 static void
449 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
450 {
451   GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
452
453   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
454   if (timestamp != GST_CLOCK_TIME_NONE) {
455     /* check if we had a previous buffer to compare to */
456     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
457         identity->prev_duration != GST_CLOCK_TIME_NONE) {
458       GstClockTime t_expected;
459       GstClockTimeDiff dt;
460
461       t_expected = identity->prev_timestamp + identity->prev_duration;
462       dt = GST_CLOCK_DIFF (t_expected, timestamp);
463       if (dt != 0) {
464         /*
465          * "imperfect-timestamp" bus message:
466          * @identity:        the identity instance
467          * @prev-timestamp:  the previous buffer timestamp
468          * @prev-duration:   the previous buffer duration
469          * @prev-offset:     the previous buffer offset
470          * @prev-offset-end: the previous buffer offset end
471          * @cur-timestamp:   the current buffer timestamp
472          * @cur-duration:    the current buffer duration
473          * @cur-offset:      the current buffer offset
474          * @cur-offset-end:  the current buffer offset end
475          *
476          * This bus message gets emitted if the check-imperfect-timestamp
477          * property is set and there is a gap in time between the
478          * last buffer and the newly received buffer.
479          */
480         gst_element_post_message (GST_ELEMENT (identity),
481             gst_message_new_element (GST_OBJECT (identity),
482                 gst_structure_new ("imperfect-timestamp",
483                     "prev-timestamp", G_TYPE_UINT64,
484                     identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
485                     identity->prev_duration, "prev-offset", G_TYPE_UINT64,
486                     identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
487                     identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
488                     timestamp, "cur-duration", G_TYPE_UINT64,
489                     GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
490                     GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
491                     GST_BUFFER_OFFSET_END (buf), NULL)));
492       }
493     } else {
494       GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
495           "offset_end was set on previous buffer");
496     }
497   }
498 }
499
500 static void
501 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
502 {
503   guint64 offset;
504
505   offset = GST_BUFFER_OFFSET (buf);
506
507   if (identity->prev_offset_end != offset &&
508       identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
509       offset != GST_BUFFER_OFFSET_NONE) {
510     /*
511      * "imperfect-offset" bus message:
512      * @identity:        the identity instance
513      * @prev-timestamp:  the previous buffer timestamp
514      * @prev-duration:   the previous buffer duration
515      * @prev-offset:     the previous buffer offset
516      * @prev-offset-end: the previous buffer offset end
517      * @cur-timestamp:   the current buffer timestamp
518      * @cur-duration:    the current buffer duration
519      * @cur-offset:      the current buffer offset
520      * @cur-offset-end:  the current buffer offset end
521      *
522      * This bus message gets emitted if the check-imperfect-offset
523      * property is set and there is a gap in offsets between the
524      * last buffer and the newly received buffer.
525      */
526     gst_element_post_message (GST_ELEMENT (identity),
527         gst_message_new_element (GST_OBJECT (identity),
528             gst_structure_new ("imperfect-offset", "prev-timestamp",
529                 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
530                 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
531                 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
532                 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
533                 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
534                 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
535                 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
536                 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
537   } else {
538     GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
539         "and/or offset_end were set on previous buffer");
540   }
541 }
542
543 static const gchar *
544 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
545 {
546   if (ts == GST_CLOCK_TIME_NONE)
547     return "none";
548
549   g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
550   return ts_str;
551 }
552
553 static void
554 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
555     const gchar * action, GstBuffer * buf, gsize size)
556 {
557   gchar ts_str[64], dur_str[64];
558   gchar flag_str[100];
559
560   GST_OBJECT_LOCK (identity);
561
562   {
563     const char *flag_list[15] = {
564       "", "", "", "", "live", "decode-only", "discont", "resync", "corrupted",
565       "marker", "header", "gap", "droppable", "delta-unit", "in-caps"
566     };
567     int i;
568     char *end = flag_str;
569     end[0] = '\0';
570     for (i = 0; i < G_N_ELEMENTS (flag_list); i++) {
571       if (GST_MINI_OBJECT_CAST (buf)->flags & (1 << i)) {
572         strcpy (end, flag_list[i]);
573         end += strlen (end);
574         end[0] = ' ';
575         end[1] = '\0';
576         end++;
577       }
578     }
579   }
580
581   g_free (identity->last_message);
582   identity->last_message = g_strdup_printf ("%s   ******* (%s:%s) "
583       "(%" G_GSIZE_FORMAT " bytes, timestamp: %s, duration: %s, offset: %"
584       G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
585       ", flags: %d %s) %p", action,
586       GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
587       print_pretty_time (ts_str, sizeof (ts_str), GST_BUFFER_TIMESTAMP (buf)),
588       print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
589       GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
590       GST_BUFFER_FLAGS (buf), flag_str, buf);
591
592   GST_OBJECT_UNLOCK (identity);
593
594   gst_identity_notify_last_message (identity);
595 }
596
597 static GstFlowReturn
598 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
599 {
600   GstFlowReturn ret = GST_FLOW_OK;
601   GstIdentity *identity = GST_IDENTITY (trans);
602   GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
603   GstMapInfo info;
604
605   gst_buffer_map (buf, &info, GST_MAP_READ);
606
607   if (identity->check_perfect)
608     gst_identity_check_perfect (identity, buf);
609   if (identity->check_imperfect_timestamp)
610     gst_identity_check_imperfect_timestamp (identity, buf);
611   if (identity->check_imperfect_offset)
612     gst_identity_check_imperfect_offset (identity, buf);
613
614   /* update prev values */
615   identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
616   identity->prev_duration = GST_BUFFER_DURATION (buf);
617   identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
618   identity->prev_offset = GST_BUFFER_OFFSET (buf);
619
620   if (identity->error_after >= 0) {
621     identity->error_after--;
622     if (identity->error_after == 0)
623       goto error_after;
624   }
625
626   if (identity->drop_probability > 0.0) {
627     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
628       goto dropped;
629   }
630
631   if (identity->dump) {
632     gst_util_dump_mem (info.data, info.size);
633   }
634
635   if (!identity->silent) {
636     gst_identity_update_last_message_for_buffer (identity, "chain", buf,
637         info.size);
638   }
639
640   if (identity->datarate > 0) {
641     GstClockTime time = gst_util_uint64_scale_int (identity->offset,
642         GST_SECOND, identity->datarate);
643
644     GST_BUFFER_TIMESTAMP (buf) = time;
645     GST_BUFFER_DURATION (buf) = info.size * GST_SECOND / identity->datarate;
646   }
647
648   if (identity->signal_handoffs)
649     g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
650
651   if (trans->segment.format == GST_FORMAT_TIME)
652     runtimestamp = gst_segment_to_running_time (&trans->segment,
653         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
654
655   if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
656     GstClock *clock;
657
658     GST_OBJECT_LOCK (identity);
659     if ((clock = GST_ELEMENT (identity)->clock)) {
660       GstClockReturn cret;
661       GstClockTime timestamp;
662
663       timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
664
665       /* save id if we need to unlock */
666       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
667       GST_OBJECT_UNLOCK (identity);
668
669       cret = gst_clock_id_wait (identity->clock_id, NULL);
670
671       GST_OBJECT_LOCK (identity);
672       if (identity->clock_id) {
673         gst_clock_id_unref (identity->clock_id);
674         identity->clock_id = NULL;
675       }
676       if (cret == GST_CLOCK_UNSCHEDULED)
677         ret = GST_FLOW_EOS;
678     }
679     GST_OBJECT_UNLOCK (identity);
680   }
681
682   identity->offset += info.size;
683
684   if (identity->sleep_time && ret == GST_FLOW_OK)
685     g_usleep (identity->sleep_time);
686
687   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
688       && (ret == GST_FLOW_OK)) {
689     GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
690     GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
691     GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
692   }
693
694   gst_buffer_unmap (buf, &info);
695
696   return ret;
697
698   /* ERRORS */
699 error_after:
700   {
701     GST_ELEMENT_ERROR (identity, CORE, FAILED,
702         (_("Failed after iterations as requested.")), (NULL));
703     gst_buffer_unmap (buf, &info);
704     return GST_FLOW_ERROR;
705   }
706 dropped:
707   {
708     if (!identity->silent) {
709       gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
710           info.size);
711     }
712     gst_buffer_unmap (buf, &info);
713     /* return DROPPED to basetransform. */
714     return GST_BASE_TRANSFORM_FLOW_DROPPED;
715   }
716 }
717
718 static void
719 gst_identity_set_property (GObject * object, guint prop_id,
720     const GValue * value, GParamSpec * pspec)
721 {
722   GstIdentity *identity;
723
724   identity = GST_IDENTITY (object);
725
726   switch (prop_id) {
727     case PROP_SLEEP_TIME:
728       identity->sleep_time = g_value_get_uint (value);
729       break;
730     case PROP_SILENT:
731       identity->silent = g_value_get_boolean (value);
732       break;
733     case PROP_SINGLE_SEGMENT:
734       identity->single_segment = g_value_get_boolean (value);
735       break;
736     case PROP_DUMP:
737       identity->dump = g_value_get_boolean (value);
738       break;
739     case PROP_ERROR_AFTER:
740       identity->error_after = g_value_get_int (value);
741       break;
742     case PROP_DROP_PROBABILITY:
743       identity->drop_probability = g_value_get_float (value);
744       break;
745     case PROP_DATARATE:
746       identity->datarate = g_value_get_int (value);
747       break;
748     case PROP_SYNC:
749       identity->sync = g_value_get_boolean (value);
750       break;
751     case PROP_CHECK_PERFECT:
752       identity->check_perfect = g_value_get_boolean (value);
753       break;
754     case PROP_CHECK_IMPERFECT_TIMESTAMP:
755       identity->check_imperfect_timestamp = g_value_get_boolean (value);
756       break;
757     case PROP_CHECK_IMPERFECT_OFFSET:
758       identity->check_imperfect_offset = g_value_get_boolean (value);
759       break;
760     case PROP_SIGNAL_HANDOFFS:
761       identity->signal_handoffs = g_value_get_boolean (value);
762       break;
763     default:
764       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
765       break;
766   }
767 }
768
769 static void
770 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
771     GParamSpec * pspec)
772 {
773   GstIdentity *identity;
774
775   identity = GST_IDENTITY (object);
776
777   switch (prop_id) {
778     case PROP_SLEEP_TIME:
779       g_value_set_uint (value, identity->sleep_time);
780       break;
781     case PROP_ERROR_AFTER:
782       g_value_set_int (value, identity->error_after);
783       break;
784     case PROP_DROP_PROBABILITY:
785       g_value_set_float (value, identity->drop_probability);
786       break;
787     case PROP_DATARATE:
788       g_value_set_int (value, identity->datarate);
789       break;
790     case PROP_SILENT:
791       g_value_set_boolean (value, identity->silent);
792       break;
793     case PROP_SINGLE_SEGMENT:
794       g_value_set_boolean (value, identity->single_segment);
795       break;
796     case PROP_DUMP:
797       g_value_set_boolean (value, identity->dump);
798       break;
799     case PROP_LAST_MESSAGE:
800       GST_OBJECT_LOCK (identity);
801       g_value_set_string (value, identity->last_message);
802       GST_OBJECT_UNLOCK (identity);
803       break;
804     case PROP_SYNC:
805       g_value_set_boolean (value, identity->sync);
806       break;
807     case PROP_CHECK_PERFECT:
808       g_value_set_boolean (value, identity->check_perfect);
809       break;
810     case PROP_CHECK_IMPERFECT_TIMESTAMP:
811       g_value_set_boolean (value, identity->check_imperfect_timestamp);
812       break;
813     case PROP_CHECK_IMPERFECT_OFFSET:
814       g_value_set_boolean (value, identity->check_imperfect_offset);
815       break;
816     case PROP_SIGNAL_HANDOFFS:
817       g_value_set_boolean (value, identity->signal_handoffs);
818       break;
819     default:
820       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
821       break;
822   }
823 }
824
825 static gboolean
826 gst_identity_start (GstBaseTransform * trans)
827 {
828   GstIdentity *identity;
829
830   identity = GST_IDENTITY (trans);
831
832   identity->offset = 0;
833   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
834   identity->prev_duration = GST_CLOCK_TIME_NONE;
835   identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
836   identity->prev_offset = GST_BUFFER_OFFSET_NONE;
837
838   return TRUE;
839 }
840
841 static gboolean
842 gst_identity_stop (GstBaseTransform * trans)
843 {
844   GstIdentity *identity;
845
846   identity = GST_IDENTITY (trans);
847
848   GST_OBJECT_LOCK (identity);
849   g_free (identity->last_message);
850   identity->last_message = NULL;
851   GST_OBJECT_UNLOCK (identity);
852
853   return TRUE;
854 }
855
856 static GstStateChangeReturn
857 gst_identity_change_state (GstElement * element, GstStateChange transition)
858 {
859   GstStateChangeReturn ret;
860   GstIdentity *identity = GST_IDENTITY (element);
861
862   switch (transition) {
863     case GST_STATE_CHANGE_NULL_TO_READY:
864       break;
865     case GST_STATE_CHANGE_READY_TO_PAUSED:
866       break;
867     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
868       break;
869     case GST_STATE_CHANGE_PAUSED_TO_READY:
870       GST_OBJECT_LOCK (identity);
871       if (identity->clock_id) {
872         GST_DEBUG_OBJECT (identity, "unlock clock wait");
873         gst_clock_id_unschedule (identity->clock_id);
874         gst_clock_id_unref (identity->clock_id);
875         identity->clock_id = NULL;
876       }
877       GST_OBJECT_UNLOCK (identity);
878       break;
879     default:
880       break;
881   }
882
883   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
884
885   switch (transition) {
886     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
887       break;
888     case GST_STATE_CHANGE_PAUSED_TO_READY:
889       break;
890     case GST_STATE_CHANGE_READY_TO_NULL:
891       break;
892     default:
893       break;
894   }
895
896   return ret;
897 }