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