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