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