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