3bb50a6e453abd0aff09c526a455e310e2329946
[platform/upstream/gstreamer.git] / plugins / elements / gstidentity.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstidentity.c:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:element-identity
25  *
26  * Dummy element that passes incoming data through unmodified. It has some
27  * useful diagnostic functions, such as offset and timestamp checking.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "../../gst/gst-i18n-lib.h"
38 #include "gstidentity.h"
39 #include <gst/gstmarshal.h>
40
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS_ANY);
45
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS_ANY);
50
51 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
52 #define GST_CAT_DEFAULT gst_identity_debug
53
54 /* Identity signals and args */
55 enum
56 {
57   SIGNAL_HANDOFF,
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 #define DEFAULT_SLEEP_TIME              0
63 #define DEFAULT_DUPLICATE               1
64 #define DEFAULT_ERROR_AFTER             -1
65 #define DEFAULT_DROP_PROBABILITY        0.0
66 #define DEFAULT_DATARATE                0
67 #define DEFAULT_SILENT                  TRUE
68 #define DEFAULT_SINGLE_SEGMENT          FALSE
69 #define DEFAULT_DUMP                    FALSE
70 #define DEFAULT_SYNC                    FALSE
71 #define DEFAULT_CHECK_PERFECT           FALSE
72 #define DEFAULT_CHECK_IMPERFECT_TIMESTAMP FALSE
73 #define DEFAULT_CHECK_IMPERFECT_OFFSET    FALSE
74 #define DEFAULT_SIGNAL_HANDOFFS           TRUE
75
76 enum
77 {
78   PROP_0,
79   PROP_SLEEP_TIME,
80   PROP_ERROR_AFTER,
81   PROP_DROP_PROBABILITY,
82   PROP_DATARATE,
83   PROP_SILENT,
84   PROP_SINGLE_SEGMENT,
85   PROP_LAST_MESSAGE,
86   PROP_DUMP,
87   PROP_SYNC,
88   PROP_CHECK_PERFECT,
89   PROP_CHECK_IMPERFECT_TIMESTAMP,
90   PROP_CHECK_IMPERFECT_OFFSET,
91   PROP_SIGNAL_HANDOFFS
92 };
93
94
95 #define _do_init \
96     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
97 #define gst_identity_parent_class parent_class
98 G_DEFINE_TYPE_WITH_CODE (GstIdentity, gst_identity, GST_TYPE_BASE_TRANSFORM,
99     _do_init);
100
101 static void gst_identity_finalize (GObject * object);
102 static void gst_identity_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_identity_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106
107 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
108     GstEvent * event);
109 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
110     GstBuffer * buf);
111 static 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
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, PROP_CHECK_PERFECT,
183       g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
184           "Verify that the stream is time- and data-contiguous. "
185           "This only logs in the debug log.  This will be deprecated in favor "
186           "of the check-imperfect-timestamp/offset properties.",
187           DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188   g_object_class_install_property (gobject_class,
189       PROP_CHECK_IMPERFECT_TIMESTAMP,
190       g_param_spec_boolean ("check-imperfect-timestamp",
191           "Check for discontiguous timestamps",
192           "Send element messages if timestamps and durations do not match up",
193           DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
194           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195   g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
196       g_param_spec_boolean ("check-imperfect-offset",
197           "Check for discontiguous offset",
198           "Send element messages if offset and offset_end do not match up",
199           DEFAULT_CHECK_IMPERFECT_OFFSET,
200           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201
202   /**
203    * GstIdentity:signal-handoffs
204    *
205    * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
206    * When set to #FALSE, no signal will be emited, which might improve performance.
207    *
208    * Since: 0.10.16
209    */
210   g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
211       g_param_spec_boolean ("signal-handoffs",
212           "Signal handoffs", "Send a signal before pushing the buffer",
213           DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214
215   /**
216    * GstIdentity::handoff:
217    * @identity: the identity instance
218    * @buffer: the buffer that just has been received
219    * @pad: the pad that received it
220    *
221    * This signal gets emitted before passing the buffer downstream.
222    */
223   gst_identity_signals[SIGNAL_HANDOFF] =
224       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
225       G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
226       g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1,
227       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
228
229   gobject_class->finalize = gst_identity_finalize;
230
231   gst_element_class_set_details_simple (gstelement_class,
232       "Identity",
233       "Generic",
234       "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
235   gst_element_class_add_pad_template (gstelement_class,
236       gst_static_pad_template_get (&srctemplate));
237   gst_element_class_add_pad_template (gstelement_class,
238       gst_static_pad_template_get (&sinktemplate));
239
240   gstelement_class->change_state =
241       GST_DEBUG_FUNCPTR (gst_identity_change_state);
242
243   gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
244   gstbasetrans_class->transform_ip =
245       GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
246   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
247   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
248 }
249
250 static void
251 gst_identity_init (GstIdentity * identity)
252 {
253   identity->sleep_time = DEFAULT_SLEEP_TIME;
254   identity->error_after = DEFAULT_ERROR_AFTER;
255   identity->drop_probability = DEFAULT_DROP_PROBABILITY;
256   identity->datarate = DEFAULT_DATARATE;
257   identity->silent = DEFAULT_SILENT;
258   identity->single_segment = DEFAULT_SINGLE_SEGMENT;
259   identity->sync = DEFAULT_SYNC;
260   identity->check_perfect = DEFAULT_CHECK_PERFECT;
261   identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
262   identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
263   identity->dump = DEFAULT_DUMP;
264   identity->last_message = NULL;
265   identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
266
267   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
268 }
269
270 static void
271 gst_identity_notify_last_message (GstIdentity * identity)
272 {
273   g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
274 }
275
276 static gboolean
277 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
278 {
279   GstIdentity *identity;
280   gboolean ret = TRUE;
281
282   identity = GST_IDENTITY (trans);
283
284   if (!identity->silent) {
285     const GstStructure *s;
286     const gchar *tstr;
287     gchar *sstr;
288
289     GST_OBJECT_LOCK (identity);
290     g_free (identity->last_message);
291
292     tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
293     if ((s = gst_event_get_structure (event)))
294       sstr = gst_structure_to_string (s);
295     else
296       sstr = g_strdup ("");
297
298     identity->last_message =
299         g_strdup_printf ("event   ******* (%s:%s) E (type: %s (%d), %s) %p",
300         GST_DEBUG_PAD_NAME (trans->sinkpad), tstr, GST_EVENT_TYPE (event),
301         sstr, event);
302     g_free (sstr);
303     GST_OBJECT_UNLOCK (identity);
304
305     gst_identity_notify_last_message (identity);
306   }
307
308   if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
309     if (trans->have_segment == FALSE) {
310       GstEvent *news;
311       GstSegment segment;
312
313       gst_event_copy_segment (event, &segment);
314       gst_event_copy_segment (event, &trans->segment);
315       trans->have_segment = TRUE;
316
317       /* This is the first segment, send out a (0, -1) segment */
318       gst_segment_init (&segment, segment.format);
319       news = gst_event_new_segment (&segment);
320
321       gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
322     }
323   }
324
325   /* Reset previous timestamp, duration and offsets on NEWSEGMENT
326    * to prevent false warnings when checking for perfect streams */
327   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
328     identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
329     identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
330   }
331
332   if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
333     /* eat up segments */
334     gst_event_unref (event);
335     ret = TRUE;
336   } else {
337     if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
338       GST_OBJECT_LOCK (identity);
339       if (identity->clock_id) {
340         GST_DEBUG_OBJECT (identity, "unlock clock wait");
341         gst_clock_id_unschedule (identity->clock_id);
342         gst_clock_id_unref (identity->clock_id);
343         identity->clock_id = NULL;
344       }
345       GST_OBJECT_UNLOCK (identity);
346     }
347
348     ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
349   }
350
351   return ret;
352 }
353
354 static void
355 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
356 {
357   GstClockTime timestamp;
358
359   timestamp = GST_BUFFER_TIMESTAMP (buf);
360
361   /* see if we need to do perfect stream checking */
362   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
363   if (timestamp != GST_CLOCK_TIME_NONE) {
364     /* check if we had a previous buffer to compare to */
365     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
366         identity->prev_duration != GST_CLOCK_TIME_NONE) {
367       guint64 offset, t_expected;
368       gint64 dt;
369
370       t_expected = identity->prev_timestamp + identity->prev_duration;
371       dt = timestamp - t_expected;
372       if (dt != 0) {
373         GST_WARNING_OBJECT (identity,
374             "Buffer not time-contiguous with previous one: " "prev ts %"
375             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
376             GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
377             GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
378             GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
379             GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
380             GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
381       }
382
383       offset = GST_BUFFER_OFFSET (buf);
384       if (identity->prev_offset_end != offset &&
385           identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
386           offset != GST_BUFFER_OFFSET_NONE) {
387         GST_WARNING_OBJECT (identity,
388             "Buffer not data-contiguous with previous one: "
389             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
390             G_GINT64_FORMAT, identity->prev_offset_end, offset);
391       }
392     } else {
393       GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
394           "and/or duration were set on previous buffer");
395     }
396   }
397 }
398
399 static void
400 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
401 {
402   GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
403
404   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
405   if (timestamp != GST_CLOCK_TIME_NONE) {
406     /* check if we had a previous buffer to compare to */
407     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
408         identity->prev_duration != GST_CLOCK_TIME_NONE) {
409       GstClockTime t_expected;
410       GstClockTimeDiff dt;
411
412       t_expected = identity->prev_timestamp + identity->prev_duration;
413       dt = GST_CLOCK_DIFF (t_expected, timestamp);
414       if (dt != 0) {
415         /*
416          * "imperfect-timestamp" bus message:
417          * @identity:        the identity instance
418          * @prev-timestamp:  the previous buffer timestamp
419          * @prev-duration:   the previous buffer duration
420          * @prev-offset:     the previous buffer offset
421          * @prev-offset-end: the previous buffer offset end
422          * @cur-timestamp:   the current buffer timestamp
423          * @cur-duration:    the current buffer duration
424          * @cur-offset:      the current buffer offset
425          * @cur-offset-end:  the current buffer offset end
426          *
427          * This bus message gets emitted if the check-imperfect-timestamp
428          * property is set and there is a gap in time between the
429          * last buffer and the newly received buffer.
430          */
431         gst_element_post_message (GST_ELEMENT (identity),
432             gst_message_new_element (GST_OBJECT (identity),
433                 gst_structure_new ("imperfect-timestamp",
434                     "prev-timestamp", G_TYPE_UINT64,
435                     identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
436                     identity->prev_duration, "prev-offset", G_TYPE_UINT64,
437                     identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
438                     identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
439                     timestamp, "cur-duration", G_TYPE_UINT64,
440                     GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
441                     GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
442                     GST_BUFFER_OFFSET_END (buf), NULL)));
443       }
444     } else {
445       GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
446           "offset_end was set on previous buffer");
447     }
448   }
449 }
450
451 static void
452 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
453 {
454   guint64 offset;
455
456   offset = GST_BUFFER_OFFSET (buf);
457
458   if (identity->prev_offset_end != offset &&
459       identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
460       offset != GST_BUFFER_OFFSET_NONE) {
461     /*
462      * "imperfect-offset" bus message:
463      * @identity:        the identity instance
464      * @prev-timestamp:  the previous buffer timestamp
465      * @prev-duration:   the previous buffer duration
466      * @prev-offset:     the previous buffer offset
467      * @prev-offset-end: the previous buffer offset end
468      * @cur-timestamp:   the current buffer timestamp
469      * @cur-duration:    the current buffer duration
470      * @cur-offset:      the current buffer offset
471      * @cur-offset-end:  the current buffer offset end
472      *
473      * This bus message gets emitted if the check-imperfect-offset
474      * property is set and there is a gap in offsets between the
475      * last buffer and the newly received buffer.
476      */
477     gst_element_post_message (GST_ELEMENT (identity),
478         gst_message_new_element (GST_OBJECT (identity),
479             gst_structure_new ("imperfect-offset", "prev-timestamp",
480                 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
481                 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
482                 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
483                 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
484                 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
485                 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
486                 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
487                 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
488   } else {
489     GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
490         "and/or offset_end were set on previous buffer");
491   }
492 }
493
494 static const gchar *
495 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
496 {
497   if (ts == GST_CLOCK_TIME_NONE)
498     return "none";
499
500   g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
501   return ts_str;
502 }
503
504 static void
505 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
506     const gchar * action, GstBuffer * buf, gsize size)
507 {
508   gchar dts_str[64], pts_str[64], dur_str[64];
509   gchar flag_str[100];
510
511   GST_OBJECT_LOCK (identity);
512
513   {
514     const char *flag_list[15] = {
515       "", "", "", "", "live", "decode-only", "discont", "resync", "corrupted",
516       "marker", "header", "gap", "droppable", "delta-unit", "in-caps"
517     };
518     int i;
519     char *end = flag_str;
520     end[0] = '\0';
521     for (i = 0; i < G_N_ELEMENTS (flag_list); i++) {
522       if (GST_MINI_OBJECT_CAST (buf)->flags & (1 << i)) {
523         strcpy (end, flag_list[i]);
524         end += strlen (end);
525         end[0] = ' ';
526         end[1] = '\0';
527         end++;
528       }
529     }
530   }
531
532   g_free (identity->last_message);
533   identity->last_message = g_strdup_printf ("%s   ******* (%s:%s) "
534       "(%" G_GSIZE_FORMAT " bytes, dts: %s, pts:%s, duration: %s, offset: %"
535       G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
536       ", flags: %d %s) %p", action,
537       GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
538       print_pretty_time (dts_str, sizeof (dts_str), GST_BUFFER_DTS (buf)),
539       print_pretty_time (pts_str, sizeof (pts_str), GST_BUFFER_PTS (buf)),
540       print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
541       GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
542       GST_BUFFER_FLAGS (buf), flag_str, buf);
543
544   GST_OBJECT_UNLOCK (identity);
545
546   gst_identity_notify_last_message (identity);
547 }
548
549 static GstFlowReturn
550 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
551 {
552   GstFlowReturn ret = GST_FLOW_OK;
553   GstIdentity *identity = GST_IDENTITY (trans);
554   GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
555   gsize size;
556
557   size = gst_buffer_get_size (buf);
558
559   if (identity->check_perfect)
560     gst_identity_check_perfect (identity, buf);
561   if (identity->check_imperfect_timestamp)
562     gst_identity_check_imperfect_timestamp (identity, buf);
563   if (identity->check_imperfect_offset)
564     gst_identity_check_imperfect_offset (identity, buf);
565
566   /* update prev values */
567   identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
568   identity->prev_duration = GST_BUFFER_DURATION (buf);
569   identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
570   identity->prev_offset = GST_BUFFER_OFFSET (buf);
571
572   if (identity->error_after >= 0) {
573     identity->error_after--;
574     if (identity->error_after == 0)
575       goto error_after;
576   }
577
578   if (identity->drop_probability > 0.0) {
579     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
580       goto dropped;
581   }
582
583   if (identity->dump) {
584     GstMapInfo info;
585
586     gst_buffer_map (buf, &info, GST_MAP_READ);
587     gst_util_dump_mem (info.data, info.size);
588     gst_buffer_unmap (buf, &info);
589   }
590
591   if (!identity->silent) {
592     gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
593   }
594
595   if (identity->datarate > 0) {
596     GstClockTime time = gst_util_uint64_scale_int (identity->offset,
597         GST_SECOND, identity->datarate);
598
599     GST_BUFFER_TIMESTAMP (buf) = time;
600     GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
601   }
602
603   if (identity->signal_handoffs)
604     g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
605
606   if (trans->segment.format == GST_FORMAT_TIME)
607     runtimestamp = gst_segment_to_running_time (&trans->segment,
608         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
609
610   if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
611     GstClock *clock;
612
613     GST_OBJECT_LOCK (identity);
614     if ((clock = GST_ELEMENT (identity)->clock)) {
615       GstClockReturn cret;
616       GstClockTime timestamp;
617
618       timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
619
620       /* save id if we need to unlock */
621       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
622       GST_OBJECT_UNLOCK (identity);
623
624       cret = gst_clock_id_wait (identity->clock_id, NULL);
625
626       GST_OBJECT_LOCK (identity);
627       if (identity->clock_id) {
628         gst_clock_id_unref (identity->clock_id);
629         identity->clock_id = NULL;
630       }
631       if (cret == GST_CLOCK_UNSCHEDULED)
632         ret = GST_FLOW_EOS;
633     }
634     GST_OBJECT_UNLOCK (identity);
635   }
636
637   identity->offset += size;
638
639   if (identity->sleep_time && ret == GST_FLOW_OK)
640     g_usleep (identity->sleep_time);
641
642   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
643       && (ret == GST_FLOW_OK)) {
644     GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
645     GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
646     GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
647   }
648
649   return ret;
650
651   /* ERRORS */
652 error_after:
653   {
654     GST_ELEMENT_ERROR (identity, CORE, FAILED,
655         (_("Failed after iterations as requested.")), (NULL));
656     return GST_FLOW_ERROR;
657   }
658 dropped:
659   {
660     if (!identity->silent) {
661       gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
662           size);
663     }
664     /* return DROPPED to basetransform. */
665     return GST_BASE_TRANSFORM_FLOW_DROPPED;
666   }
667 }
668
669 static void
670 gst_identity_set_property (GObject * object, guint prop_id,
671     const GValue * value, GParamSpec * pspec)
672 {
673   GstIdentity *identity;
674
675   identity = GST_IDENTITY (object);
676
677   switch (prop_id) {
678     case PROP_SLEEP_TIME:
679       identity->sleep_time = g_value_get_uint (value);
680       break;
681     case PROP_SILENT:
682       identity->silent = g_value_get_boolean (value);
683       break;
684     case PROP_SINGLE_SEGMENT:
685       identity->single_segment = g_value_get_boolean (value);
686       break;
687     case PROP_DUMP:
688       identity->dump = g_value_get_boolean (value);
689       break;
690     case PROP_ERROR_AFTER:
691       identity->error_after = g_value_get_int (value);
692       break;
693     case PROP_DROP_PROBABILITY:
694       identity->drop_probability = g_value_get_float (value);
695       break;
696     case PROP_DATARATE:
697       identity->datarate = g_value_get_int (value);
698       break;
699     case PROP_SYNC:
700       identity->sync = g_value_get_boolean (value);
701       break;
702     case PROP_CHECK_PERFECT:
703       identity->check_perfect = g_value_get_boolean (value);
704       break;
705     case PROP_CHECK_IMPERFECT_TIMESTAMP:
706       identity->check_imperfect_timestamp = g_value_get_boolean (value);
707       break;
708     case PROP_CHECK_IMPERFECT_OFFSET:
709       identity->check_imperfect_offset = g_value_get_boolean (value);
710       break;
711     case PROP_SIGNAL_HANDOFFS:
712       identity->signal_handoffs = g_value_get_boolean (value);
713       break;
714     default:
715       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
716       break;
717   }
718   if (identity->datarate > 0 || identity->single_segment)
719     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
720   else
721     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
722 }
723
724 static void
725 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
726     GParamSpec * pspec)
727 {
728   GstIdentity *identity;
729
730   identity = GST_IDENTITY (object);
731
732   switch (prop_id) {
733     case PROP_SLEEP_TIME:
734       g_value_set_uint (value, identity->sleep_time);
735       break;
736     case PROP_ERROR_AFTER:
737       g_value_set_int (value, identity->error_after);
738       break;
739     case PROP_DROP_PROBABILITY:
740       g_value_set_float (value, identity->drop_probability);
741       break;
742     case PROP_DATARATE:
743       g_value_set_int (value, identity->datarate);
744       break;
745     case PROP_SILENT:
746       g_value_set_boolean (value, identity->silent);
747       break;
748     case PROP_SINGLE_SEGMENT:
749       g_value_set_boolean (value, identity->single_segment);
750       break;
751     case PROP_DUMP:
752       g_value_set_boolean (value, identity->dump);
753       break;
754     case PROP_LAST_MESSAGE:
755       GST_OBJECT_LOCK (identity);
756       g_value_set_string (value, identity->last_message);
757       GST_OBJECT_UNLOCK (identity);
758       break;
759     case PROP_SYNC:
760       g_value_set_boolean (value, identity->sync);
761       break;
762     case PROP_CHECK_PERFECT:
763       g_value_set_boolean (value, identity->check_perfect);
764       break;
765     case PROP_CHECK_IMPERFECT_TIMESTAMP:
766       g_value_set_boolean (value, identity->check_imperfect_timestamp);
767       break;
768     case PROP_CHECK_IMPERFECT_OFFSET:
769       g_value_set_boolean (value, identity->check_imperfect_offset);
770       break;
771     case PROP_SIGNAL_HANDOFFS:
772       g_value_set_boolean (value, identity->signal_handoffs);
773       break;
774     default:
775       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
776       break;
777   }
778 }
779
780 static gboolean
781 gst_identity_start (GstBaseTransform * trans)
782 {
783   GstIdentity *identity;
784
785   identity = GST_IDENTITY (trans);
786
787   identity->offset = 0;
788   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
789   identity->prev_duration = GST_CLOCK_TIME_NONE;
790   identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
791   identity->prev_offset = GST_BUFFER_OFFSET_NONE;
792
793   return TRUE;
794 }
795
796 static gboolean
797 gst_identity_stop (GstBaseTransform * trans)
798 {
799   GstIdentity *identity;
800
801   identity = GST_IDENTITY (trans);
802
803   GST_OBJECT_LOCK (identity);
804   g_free (identity->last_message);
805   identity->last_message = NULL;
806   GST_OBJECT_UNLOCK (identity);
807
808   return TRUE;
809 }
810
811 static GstStateChangeReturn
812 gst_identity_change_state (GstElement * element, GstStateChange transition)
813 {
814   GstStateChangeReturn ret;
815   GstIdentity *identity = GST_IDENTITY (element);
816
817   switch (transition) {
818     case GST_STATE_CHANGE_NULL_TO_READY:
819       break;
820     case GST_STATE_CHANGE_READY_TO_PAUSED:
821       break;
822     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
823       break;
824     case GST_STATE_CHANGE_PAUSED_TO_READY:
825       GST_OBJECT_LOCK (identity);
826       if (identity->clock_id) {
827         GST_DEBUG_OBJECT (identity, "unlock clock wait");
828         gst_clock_id_unschedule (identity->clock_id);
829         gst_clock_id_unref (identity->clock_id);
830         identity->clock_id = NULL;
831       }
832       GST_OBJECT_UNLOCK (identity);
833       break;
834     default:
835       break;
836   }
837
838   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
839
840   switch (transition) {
841     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
842       break;
843     case GST_STATE_CHANGE_PAUSED_TO_READY:
844       break;
845     case GST_STATE_CHANGE_READY_TO_NULL:
846       break;
847     default:
848       break;
849   }
850
851   return ret;
852 }