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