23cbf6d390000f6740a40f34eed472c5c3d8d1e5
[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 newsegments 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
352       && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
353     if (trans->have_newsegment == FALSE) {
354       GstEvent *news;
355       GstFormat format;
356
357       gst_event_parse_new_segment (event, NULL, NULL, NULL, &format, NULL, NULL,
358           NULL);
359
360       /* This is the first newsegment, send out a (0, -1) newsegment */
361       news = gst_event_new_new_segment (TRUE, 1.0, 1.0, format, 0, -1, 0);
362
363       gst_pad_event_default (trans->sinkpad, news);
364     }
365   }
366
367   /* Reset previous timestamp, duration and offsets on NEWSEGMENT
368    * to prevent false warnings when checking for perfect streams */
369   if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
370     identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
371     identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
372   }
373
374   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
375
376   if (identity->single_segment
377       && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
378     /* eat up segments */
379     ret = FALSE;
380   }
381
382   return ret;
383 }
384
385 static GstFlowReturn
386 gst_identity_prepare_output_buffer (GstBaseTransform * trans,
387     GstBuffer * in_buf, gint out_size, GstCaps * out_caps, GstBuffer ** out_buf)
388 {
389   GstIdentity *identity = GST_IDENTITY (trans);
390
391   /* only bother if we may have to alter metadata */
392   if (identity->datarate > 0 || identity->single_segment) {
393     if (gst_buffer_is_writable (in_buf))
394       *out_buf = gst_buffer_ref (in_buf);
395     else {
396       /* make even less writable */
397       gst_buffer_ref (in_buf);
398       /* extra ref is dropped going through the official process */
399       *out_buf = gst_buffer_make_writable (in_buf);
400     }
401   } else
402     *out_buf = gst_buffer_ref (in_buf);
403
404   return GST_FLOW_OK;
405 }
406
407 static void
408 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
409 {
410   GstClockTime timestamp;
411
412   timestamp = GST_BUFFER_TIMESTAMP (buf);
413
414   /* see if we need to do perfect stream checking */
415   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
416   if (timestamp != GST_CLOCK_TIME_NONE) {
417     /* check if we had a previous buffer to compare to */
418     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
419         identity->prev_duration != GST_CLOCK_TIME_NONE) {
420       guint64 offset, t_expected;
421       gint64 dt;
422
423       t_expected = identity->prev_timestamp + identity->prev_duration;
424       dt = timestamp - t_expected;
425       if (dt != 0) {
426         GST_WARNING_OBJECT (identity,
427             "Buffer not time-contiguous with previous one: " "prev ts %"
428             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
429             GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
430             GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
431             GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
432             GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
433             GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
434       }
435
436       offset = GST_BUFFER_OFFSET (buf);
437       if (identity->prev_offset_end != offset &&
438           identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
439           offset != GST_BUFFER_OFFSET_NONE) {
440         GST_WARNING_OBJECT (identity,
441             "Buffer not data-contiguous with previous one: "
442             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
443             G_GINT64_FORMAT, identity->prev_offset_end, offset);
444       }
445     } else {
446       GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
447           "and/or duration were set on previous buffer");
448     }
449   }
450 }
451
452 static void
453 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
454 {
455   GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
456
457   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
458   if (timestamp != GST_CLOCK_TIME_NONE) {
459     /* check if we had a previous buffer to compare to */
460     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
461         identity->prev_duration != GST_CLOCK_TIME_NONE) {
462       GstClockTime t_expected;
463       GstClockTimeDiff dt;
464
465       t_expected = identity->prev_timestamp + identity->prev_duration;
466       dt = GST_CLOCK_DIFF (t_expected, timestamp);
467       if (dt != 0) {
468         /*
469          * "imperfect-timestamp" bus message:
470          * @identity:        the identity instance
471          * @prev-timestamp:  the previous buffer timestamp
472          * @prev-duration:   the previous buffer duration
473          * @prev-offset:     the previous buffer offset
474          * @prev-offset-end: the previous buffer offset end
475          * @cur-timestamp:   the current buffer timestamp
476          * @cur-duration:    the current buffer duration
477          * @cur-offset:      the current buffer offset
478          * @cur-offset-end:  the current buffer offset end
479          *
480          * This bus message gets emitted if the check-imperfect-timestamp
481          * property is set and there is a gap in time between the
482          * last buffer and the newly received buffer.
483          */
484         gst_element_post_message (GST_ELEMENT (identity),
485             gst_message_new_element (GST_OBJECT (identity),
486                 gst_structure_new ("imperfect-timestamp",
487                     "prev-timestamp", G_TYPE_UINT64,
488                     identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
489                     identity->prev_duration, "prev-offset", G_TYPE_UINT64,
490                     identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
491                     identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
492                     timestamp, "cur-duration", G_TYPE_UINT64,
493                     GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
494                     GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
495                     GST_BUFFER_OFFSET_END (buf), NULL)));
496       }
497     } else {
498       GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
499           "offset_end was set on previous buffer");
500     }
501   }
502 }
503
504 static void
505 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
506 {
507   guint64 offset;
508
509   offset = GST_BUFFER_OFFSET (buf);
510
511   if (identity->prev_offset_end != offset &&
512       identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
513       offset != GST_BUFFER_OFFSET_NONE) {
514     /*
515      * "imperfect-offset" bus message:
516      * @identity:        the identity instance
517      * @prev-timestamp:  the previous buffer timestamp
518      * @prev-duration:   the previous buffer duration
519      * @prev-offset:     the previous buffer offset
520      * @prev-offset-end: the previous buffer offset end
521      * @cur-timestamp:   the current buffer timestamp
522      * @cur-duration:    the current buffer duration
523      * @cur-offset:      the current buffer offset
524      * @cur-offset-end:  the current buffer offset end
525      *
526      * This bus message gets emitted if the check-imperfect-offset
527      * property is set and there is a gap in offsets between the
528      * last buffer and the newly received buffer.
529      */
530     gst_element_post_message (GST_ELEMENT (identity),
531         gst_message_new_element (GST_OBJECT (identity),
532             gst_structure_new ("imperfect-offset", "prev-timestamp",
533                 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
534                 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
535                 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
536                 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
537                 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
538                 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
539                 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
540                 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
541   } else {
542     GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
543         "and/or offset_end were set on previous buffer");
544   }
545 }
546
547 static const gchar *
548 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
549 {
550   if (ts == GST_CLOCK_TIME_NONE)
551     return "none";
552
553   g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
554   return ts_str;
555 }
556
557 static void
558 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
559     const gchar * action, GstBuffer * buf, gsize size)
560 {
561   gchar ts_str[64], dur_str[64];
562
563   GST_OBJECT_LOCK (identity);
564
565   g_free (identity->last_message);
566   identity->last_message = g_strdup_printf ("%s   ******* (%s:%s)i "
567       "(%" G_GSIZE_FORMAT " bytes, timestamp: %s, duration: %s, offset: %"
568       G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
569       action, GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad),
570       size, print_pretty_time (ts_str, sizeof (ts_str),
571           GST_BUFFER_TIMESTAMP (buf)), print_pretty_time (dur_str,
572           sizeof (dur_str), GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
573       GST_BUFFER_OFFSET_END (buf), GST_BUFFER_FLAGS (buf), buf);
574
575   GST_OBJECT_UNLOCK (identity);
576
577   gst_identity_notify_last_message (identity);
578 }
579
580 static GstFlowReturn
581 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
582 {
583   GstFlowReturn ret = GST_FLOW_OK;
584   GstIdentity *identity = GST_IDENTITY (trans);
585   GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
586   guint8 *data;
587   gsize size;
588
589   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
590
591   if (identity->check_perfect)
592     gst_identity_check_perfect (identity, buf);
593   if (identity->check_imperfect_timestamp)
594     gst_identity_check_imperfect_timestamp (identity, buf);
595   if (identity->check_imperfect_offset)
596     gst_identity_check_imperfect_offset (identity, buf);
597
598   /* update prev values */
599   identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
600   identity->prev_duration = GST_BUFFER_DURATION (buf);
601   identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
602   identity->prev_offset = GST_BUFFER_OFFSET (buf);
603
604   if (identity->error_after >= 0) {
605     identity->error_after--;
606     if (identity->error_after == 0)
607       goto error_after;
608   }
609
610   if (identity->drop_probability > 0.0) {
611     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
612       goto dropped;
613   }
614
615   if (identity->dump) {
616     gst_util_dump_mem (data, size);
617   }
618
619   if (!identity->silent) {
620     gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
621   }
622
623   if (identity->datarate > 0) {
624     GstClockTime time = gst_util_uint64_scale_int (identity->offset,
625         GST_SECOND, identity->datarate);
626
627     GST_BUFFER_TIMESTAMP (buf) = time;
628     GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
629   }
630
631   if (identity->signal_handoffs)
632     g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
633
634   if (trans->segment.format == GST_FORMAT_TIME)
635     runtimestamp = gst_segment_to_running_time (&trans->segment,
636         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
637
638   if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
639     GstClock *clock;
640
641     GST_OBJECT_LOCK (identity);
642     if ((clock = GST_ELEMENT (identity)->clock)) {
643       GstClockReturn cret;
644       GstClockTime timestamp;
645
646       timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
647
648       /* save id if we need to unlock */
649       /* FIXME: actually unlock this somewhere in the state changes */
650       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
651       GST_OBJECT_UNLOCK (identity);
652
653       cret = gst_clock_id_wait (identity->clock_id, NULL);
654
655       GST_OBJECT_LOCK (identity);
656       if (identity->clock_id) {
657         gst_clock_id_unref (identity->clock_id);
658         identity->clock_id = NULL;
659       }
660       if (cret == GST_CLOCK_UNSCHEDULED)
661         ret = GST_FLOW_UNEXPECTED;
662     }
663     GST_OBJECT_UNLOCK (identity);
664   }
665
666   identity->offset += size;
667
668   if (identity->sleep_time && ret == GST_FLOW_OK)
669     g_usleep (identity->sleep_time);
670
671   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
672       && (ret == GST_FLOW_OK)) {
673     GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
674     GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
675     GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
676   }
677
678   gst_buffer_unmap (buf, data, size);
679
680   return ret;
681
682   /* ERRORS */
683 error_after:
684   {
685     GST_ELEMENT_ERROR (identity, CORE, FAILED,
686         (_("Failed after iterations as requested.")), (NULL));
687     gst_buffer_unmap (buf, data, size);
688     return GST_FLOW_ERROR;
689   }
690 dropped:
691   {
692     if (!identity->silent) {
693       gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
694           size);
695     }
696     gst_buffer_unmap (buf, data, size);
697     /* return DROPPED to basetransform. */
698     return GST_BASE_TRANSFORM_FLOW_DROPPED;
699   }
700 }
701
702 static void
703 gst_identity_set_property (GObject * object, guint prop_id,
704     const GValue * value, GParamSpec * pspec)
705 {
706   GstIdentity *identity;
707
708   identity = GST_IDENTITY (object);
709
710   switch (prop_id) {
711     case PROP_SLEEP_TIME:
712       identity->sleep_time = g_value_get_uint (value);
713       break;
714     case PROP_SILENT:
715       identity->silent = g_value_get_boolean (value);
716       break;
717     case PROP_SINGLE_SEGMENT:
718       identity->single_segment = g_value_get_boolean (value);
719       break;
720     case PROP_DUMP:
721       identity->dump = g_value_get_boolean (value);
722       break;
723     case PROP_ERROR_AFTER:
724       identity->error_after = g_value_get_int (value);
725       break;
726     case PROP_DROP_PROBABILITY:
727       identity->drop_probability = g_value_get_float (value);
728       break;
729     case PROP_DATARATE:
730       identity->datarate = g_value_get_int (value);
731       break;
732     case PROP_SYNC:
733       identity->sync = g_value_get_boolean (value);
734       break;
735     case PROP_CHECK_PERFECT:
736       identity->check_perfect = g_value_get_boolean (value);
737       break;
738     case PROP_CHECK_IMPERFECT_TIMESTAMP:
739       identity->check_imperfect_timestamp = g_value_get_boolean (value);
740       break;
741     case PROP_CHECK_IMPERFECT_OFFSET:
742       identity->check_imperfect_offset = g_value_get_boolean (value);
743       break;
744     case PROP_SIGNAL_HANDOFFS:
745       identity->signal_handoffs = g_value_get_boolean (value);
746       break;
747     default:
748       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
749       break;
750   }
751 }
752
753 static void
754 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
755     GParamSpec * pspec)
756 {
757   GstIdentity *identity;
758
759   identity = GST_IDENTITY (object);
760
761   switch (prop_id) {
762     case PROP_SLEEP_TIME:
763       g_value_set_uint (value, identity->sleep_time);
764       break;
765     case PROP_ERROR_AFTER:
766       g_value_set_int (value, identity->error_after);
767       break;
768     case PROP_DROP_PROBABILITY:
769       g_value_set_float (value, identity->drop_probability);
770       break;
771     case PROP_DATARATE:
772       g_value_set_int (value, identity->datarate);
773       break;
774     case PROP_SILENT:
775       g_value_set_boolean (value, identity->silent);
776       break;
777     case PROP_SINGLE_SEGMENT:
778       g_value_set_boolean (value, identity->single_segment);
779       break;
780     case PROP_DUMP:
781       g_value_set_boolean (value, identity->dump);
782       break;
783     case PROP_LAST_MESSAGE:
784       GST_OBJECT_LOCK (identity);
785       g_value_set_string (value, identity->last_message);
786       GST_OBJECT_UNLOCK (identity);
787       break;
788     case PROP_SYNC:
789       g_value_set_boolean (value, identity->sync);
790       break;
791     case PROP_CHECK_PERFECT:
792       g_value_set_boolean (value, identity->check_perfect);
793       break;
794     case PROP_CHECK_IMPERFECT_TIMESTAMP:
795       g_value_set_boolean (value, identity->check_imperfect_timestamp);
796       break;
797     case PROP_CHECK_IMPERFECT_OFFSET:
798       g_value_set_boolean (value, identity->check_imperfect_offset);
799       break;
800     case PROP_SIGNAL_HANDOFFS:
801       g_value_set_boolean (value, identity->signal_handoffs);
802       break;
803     default:
804       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
805       break;
806   }
807 }
808
809 static gboolean
810 gst_identity_start (GstBaseTransform * trans)
811 {
812   GstIdentity *identity;
813
814   identity = GST_IDENTITY (trans);
815
816   identity->offset = 0;
817   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
818   identity->prev_duration = GST_CLOCK_TIME_NONE;
819   identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
820   identity->prev_offset = GST_BUFFER_OFFSET_NONE;
821
822   return TRUE;
823 }
824
825 static gboolean
826 gst_identity_stop (GstBaseTransform * trans)
827 {
828   GstIdentity *identity;
829
830   identity = GST_IDENTITY (trans);
831
832   GST_OBJECT_LOCK (identity);
833   g_free (identity->last_message);
834   identity->last_message = NULL;
835   GST_OBJECT_UNLOCK (identity);
836
837   return TRUE;
838 }