]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - plugins/elements/gstidentity.c
f51ca5ae7591f819f98283f030326176aaa63fd7
[glsdk/gstreamer0-10.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  */
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
34 #include <stdlib.h>
36 #include "../../gst/gst-i18n-lib.h"
37 #include "gstidentity.h"
38 #include <gst/gstmarshal.h>
40 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS_ANY);
45 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS_ANY);
50 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
51 #define GST_CAT_DEFAULT gst_identity_debug
53 /* Identity signals and args */
54 enum
55 {
56   SIGNAL_HANDOFF,
57   /* FILL ME */
58   LAST_SIGNAL
59 };
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
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 };
94 #define _do_init(bla) \
95     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
97 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstBaseTransform,
98     GST_TYPE_BASE_TRANSFORM, _do_init);
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);
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 static GstStateChangeReturn gst_identity_change_state (GstElement * element,
115     GstStateChange transition);
117 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
119 static GParamSpec *pspec_last_message = NULL;
121 static void
122 gst_identity_base_init (gpointer g_class)
124   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
126   gst_element_class_set_details_simple (gstelement_class,
127       "Identity",
128       "Generic",
129       "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
130   gst_element_class_add_pad_template (gstelement_class,
131       gst_static_pad_template_get (&srctemplate));
132   gst_element_class_add_pad_template (gstelement_class,
133       gst_static_pad_template_get (&sinktemplate));
136 static void
137 gst_identity_finalize (GObject * object)
139   GstIdentity *identity;
141   identity = GST_IDENTITY (object);
143   g_free (identity->last_message);
145 #if !GLIB_CHECK_VERSION(2,26,0)
146   g_static_rec_mutex_free (&identity->notify_lock);
147 #endif
149   G_OBJECT_CLASS (parent_class)->finalize (object);
152 /* fixme: do something about this */
153 static void
154 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
155     guint n_param_values, const GValue * param_values, gpointer invocation_hint,
156     gpointer marshal_data)
158   typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
159       gpointer data2);
160   register marshalfunc_VOID__MINIOBJECT callback;
161   register GCClosure *cc = (GCClosure *) closure;
162   register gpointer data1, data2;
164   g_return_if_fail (n_param_values == 2);
166   if (G_CCLOSURE_SWAP_DATA (closure)) {
167     data1 = closure->data;
168     data2 = g_value_peek_pointer (param_values + 0);
169   } else {
170     data1 = g_value_peek_pointer (param_values + 0);
171     data2 = closure->data;
172   }
173   callback =
174       (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data :
175       cc->callback);
177   callback (data1, gst_value_get_mini_object (param_values + 1), data2);
180 static void
181 gst_identity_class_init (GstIdentityClass * klass)
183   GObjectClass *gobject_class;
184   GstElementClass *gstelement_class;
185   GstBaseTransformClass *gstbasetrans_class;
187   gobject_class = G_OBJECT_CLASS (klass);
188   gstelement_class = GST_ELEMENT_CLASS (klass);
189   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
191   gobject_class->set_property = gst_identity_set_property;
192   gobject_class->get_property = gst_identity_get_property;
194   g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
195       g_param_spec_uint ("sleep-time", "Sleep time",
196           "Microseconds to sleep between processing", 0, G_MAXUINT,
197           DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198   g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
199       g_param_spec_int ("error-after", "Error After", "Error after N buffers",
200           G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER,
201           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202   g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
203       g_param_spec_float ("drop-probability", "Drop Probability",
204           "The Probability a buffer is dropped", 0.0, 1.0,
205           DEFAULT_DROP_PROBABILITY,
206           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207   g_object_class_install_property (gobject_class, PROP_DATARATE,
208       g_param_spec_int ("datarate", "Datarate",
209           "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
210           0, G_MAXINT, DEFAULT_DATARATE,
211           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212   g_object_class_install_property (gobject_class, PROP_SILENT,
213       g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
214           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215   g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
216       g_param_spec_boolean ("single-segment", "Single Segment",
217           "Timestamp buffers and eat newsegments so as to appear as one segment",
218           DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219   pspec_last_message = g_param_spec_string ("last-message", "last-message",
220       "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
221   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
222       pspec_last_message);
223   g_object_class_install_property (gobject_class, PROP_DUMP,
224       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
225           DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226   g_object_class_install_property (gobject_class, PROP_SYNC,
227       g_param_spec_boolean ("sync", "Synchronize",
228           "Synchronize to pipeline clock", DEFAULT_SYNC,
229           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230   g_object_class_install_property (gobject_class, PROP_CHECK_PERFECT,
231       g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
232           "Verify that the stream is time- and data-contiguous. "
233           "This only logs in the debug log.  This will be deprecated in favor "
234           "of the check-imperfect-timestamp/offset properties.",
235           DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236   g_object_class_install_property (gobject_class,
237       PROP_CHECK_IMPERFECT_TIMESTAMP,
238       g_param_spec_boolean ("check-imperfect-timestamp",
239           "Check for discontiguous timestamps",
240           "Send element messages if timestamps and durations do not match up",
241           DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
242           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243   g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
244       g_param_spec_boolean ("check-imperfect-offset",
245           "Check for discontiguous offset",
246           "Send element messages if offset and offset_end do not match up",
247           DEFAULT_CHECK_IMPERFECT_OFFSET,
248           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250   /**
251    * GstIdentity:signal-handoffs
252    *
253    * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
254    * When set to #FALSE, no signal will be emited, which might improve performance.
255    *
256    * Since: 0.10.16
257    */
258   g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
259       g_param_spec_boolean ("signal-handoffs",
260           "Signal handoffs", "Send a signal before pushing the buffer",
261           DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263   /**
264    * GstIdentity::handoff:
265    * @identity: the identity instance
266    * @buffer: the buffer that just has been received
267    * @pad: the pad that received it
268    *
269    * This signal gets emitted before passing the buffer downstream.
270    */
271   gst_identity_signals[SIGNAL_HANDOFF] =
272       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
273       G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
274       marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
276   gobject_class->finalize = gst_identity_finalize;
278   gstelement_class->change_state =
279       GST_DEBUG_FUNCPTR (gst_identity_change_state);
281   gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_identity_event);
282   gstbasetrans_class->transform_ip =
283       GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
284   gstbasetrans_class->prepare_output_buffer =
285       GST_DEBUG_FUNCPTR (gst_identity_prepare_output_buffer);
286   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
287   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
290 static void
291 gst_identity_init (GstIdentity * identity, GstIdentityClass * g_class)
293   identity->sleep_time = DEFAULT_SLEEP_TIME;
294   identity->error_after = DEFAULT_ERROR_AFTER;
295   identity->drop_probability = DEFAULT_DROP_PROBABILITY;
296   identity->datarate = DEFAULT_DATARATE;
297   identity->silent = DEFAULT_SILENT;
298   identity->single_segment = DEFAULT_SINGLE_SEGMENT;
299   identity->sync = DEFAULT_SYNC;
300   identity->check_perfect = DEFAULT_CHECK_PERFECT;
301   identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
302   identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
303   identity->dump = DEFAULT_DUMP;
304   identity->last_message = NULL;
305   identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
307 #if !GLIB_CHECK_VERSION(2,26,0)
308   g_static_rec_mutex_init (&identity->notify_lock);
309 #endif
311   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
314 static void
315 gst_identity_notify_last_message (GstIdentity * identity)
317   /* FIXME: this hacks around a bug in GLib/GObject: doing concurrent
318    * g_object_notify() on the same object might lead to crashes, see
319    * http://bugzilla.gnome.org/show_bug.cgi?id=166020#c60 and follow-ups.
320    * So we really don't want to do a g_object_notify() here for out-of-band
321    * events with the streaming thread possibly also doing a g_object_notify()
322    * for an in-band buffer or event. This is fixed in GLib >= 2.26 */
323 #if !GLIB_CHECK_VERSION(2,26,0)
324   g_static_rec_mutex_lock (&identity->notify_lock);
325   g_object_notify ((GObject *) identity, "last-message");
326   g_static_rec_mutex_unlock (&identity->notify_lock);
327 #else
328   g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
329 #endif
332 static gboolean
333 gst_identity_event (GstBaseTransform * trans, GstEvent * event)
335   GstIdentity *identity;
336   gboolean ret = TRUE;
338   identity = GST_IDENTITY (trans);
340   if (!identity->silent) {
341     const GstStructure *s;
342     gchar *sstr;
344     GST_OBJECT_LOCK (identity);
345     g_free (identity->last_message);
347     if ((s = gst_event_get_structure (event)))
348       sstr = gst_structure_to_string (s);
349     else
350       sstr = g_strdup ("");
352     identity->last_message =
353         g_strdup_printf ("event   ******* (%s:%s) E (type: %d, %s) %p",
354         GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
355         event);
356     g_free (sstr);
357     GST_OBJECT_UNLOCK (identity);
359     gst_identity_notify_last_message (identity);
360   }
362   if (identity->single_segment
363       && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
364     if (trans->have_newsegment == FALSE) {
365       GstEvent *news;
366       GstFormat format;
368       gst_event_parse_new_segment (event, NULL, NULL, &format, NULL, NULL,
369           NULL);
371       /* This is the first newsegment, send out a (0, -1) newsegment */
372       news = gst_event_new_new_segment (TRUE, 1.0, format, 0, -1, 0);
374       gst_pad_event_default (trans->sinkpad, news);
375     }
376   }
378   /* Reset previous timestamp, duration and offsets on NEWSEGMENT
379    * to prevent false warnings when checking for perfect streams */
380   if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
381     identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
382     identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
383   }
385   ret = parent_class->event (trans, event);
387   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
388     GST_OBJECT_LOCK (identity);
389     if (identity->clock_id) {
390       GST_DEBUG_OBJECT (identity, "unlock clock wait");
391       gst_clock_id_unschedule (identity->clock_id);
392       gst_clock_id_unref (identity->clock_id);
393       identity->clock_id = NULL;
394     }
395     GST_OBJECT_UNLOCK (identity);
396   }
398   if (identity->single_segment
399       && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
400     /* eat up segments */
401     ret = FALSE;
402   }
404   return ret;
407 static GstFlowReturn
408 gst_identity_prepare_output_buffer (GstBaseTransform * trans,
409     GstBuffer * in_buf, gint out_size, GstCaps * out_caps, GstBuffer ** out_buf)
411   GstIdentity *identity = GST_IDENTITY (trans);
413   /* only bother if we may have to alter metadata */
414   if (identity->datarate > 0 || identity->single_segment) {
415     if (gst_buffer_is_metadata_writable (in_buf))
416       *out_buf = gst_buffer_ref (in_buf);
417     else {
418       /* make even less writable */
419       gst_buffer_ref (in_buf);
420       /* extra ref is dropped going through the official process */
421       *out_buf = gst_buffer_make_metadata_writable (in_buf);
422     }
423   } else
424     *out_buf = gst_buffer_ref (in_buf);
426   return GST_FLOW_OK;
429 static void
430 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
432   GstClockTime timestamp;
434   timestamp = GST_BUFFER_TIMESTAMP (buf);
436   /* see if we need to do perfect stream checking */
437   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
438   if (timestamp != GST_CLOCK_TIME_NONE) {
439     /* check if we had a previous buffer to compare to */
440     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
441         identity->prev_duration != GST_CLOCK_TIME_NONE) {
442       guint64 offset, t_expected;
443       gint64 dt;
445       t_expected = identity->prev_timestamp + identity->prev_duration;
446       dt = timestamp - t_expected;
447       if (dt != 0) {
448         GST_WARNING_OBJECT (identity,
449             "Buffer not time-contiguous with previous one: " "prev ts %"
450             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
451             GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
452             GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
453             GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
454             GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
455             GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
456       }
458       offset = GST_BUFFER_OFFSET (buf);
459       if (identity->prev_offset_end != offset &&
460           identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
461           offset != GST_BUFFER_OFFSET_NONE) {
462         GST_WARNING_OBJECT (identity,
463             "Buffer not data-contiguous with previous one: "
464             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
465             G_GINT64_FORMAT, identity->prev_offset_end, offset);
466       }
467     } else {
468       GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
469           "and/or duration were set on previous buffer");
470     }
471   }
474 static void
475 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
477   GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
479   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
480   if (timestamp != GST_CLOCK_TIME_NONE) {
481     /* check if we had a previous buffer to compare to */
482     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
483         identity->prev_duration != GST_CLOCK_TIME_NONE) {
484       GstClockTime t_expected;
485       GstClockTimeDiff dt;
487       t_expected = identity->prev_timestamp + identity->prev_duration;
488       dt = GST_CLOCK_DIFF (t_expected, timestamp);
489       if (dt != 0) {
490         /*
491          * "imperfect-timestamp" bus message:
492          * @identity:        the identity instance
493          * @prev-timestamp:  the previous buffer timestamp
494          * @prev-duration:   the previous buffer duration
495          * @prev-offset:     the previous buffer offset
496          * @prev-offset-end: the previous buffer offset end
497          * @cur-timestamp:   the current buffer timestamp
498          * @cur-duration:    the current buffer duration
499          * @cur-offset:      the current buffer offset
500          * @cur-offset-end:  the current buffer offset end
501          *
502          * This bus message gets emitted if the check-imperfect-timestamp
503          * property is set and there is a gap in time between the
504          * last buffer and the newly received buffer.
505          */
506         gst_element_post_message (GST_ELEMENT (identity),
507             gst_message_new_element (GST_OBJECT (identity),
508                 gst_structure_new ("imperfect-timestamp",
509                     "prev-timestamp", G_TYPE_UINT64,
510                     identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
511                     identity->prev_duration, "prev-offset", G_TYPE_UINT64,
512                     identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
513                     identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
514                     timestamp, "cur-duration", G_TYPE_UINT64,
515                     GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
516                     GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
517                     GST_BUFFER_OFFSET_END (buf), NULL)));
518       }
519     } else {
520       GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
521           "offset_end was set on previous buffer");
522     }
523   }
526 static void
527 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
529   guint64 offset;
531   offset = GST_BUFFER_OFFSET (buf);
533   if (identity->prev_offset_end != offset &&
534       identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
535       offset != GST_BUFFER_OFFSET_NONE) {
536     /*
537      * "imperfect-offset" bus message:
538      * @identity:        the identity instance
539      * @prev-timestamp:  the previous buffer timestamp
540      * @prev-duration:   the previous buffer duration
541      * @prev-offset:     the previous buffer offset
542      * @prev-offset-end: the previous buffer offset end
543      * @cur-timestamp:   the current buffer timestamp
544      * @cur-duration:    the current buffer duration
545      * @cur-offset:      the current buffer offset
546      * @cur-offset-end:  the current buffer offset end
547      *
548      * This bus message gets emitted if the check-imperfect-offset
549      * property is set and there is a gap in offsets between the
550      * last buffer and the newly received buffer.
551      */
552     gst_element_post_message (GST_ELEMENT (identity),
553         gst_message_new_element (GST_OBJECT (identity),
554             gst_structure_new ("imperfect-offset", "prev-timestamp",
555                 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
556                 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
557                 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
558                 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
559                 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
560                 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
561                 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
562                 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
563   } else {
564     GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
565         "and/or offset_end were set on previous buffer");
566   }
569 static const gchar *
570 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
572   if (ts == GST_CLOCK_TIME_NONE)
573     return "none";
575   g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
576   return ts_str;
579 static void
580 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
581     const gchar * action, GstBuffer * buf)
583   gchar ts_str[64], dur_str[64];
585   GST_OBJECT_LOCK (identity);
587   g_free (identity->last_message);
588   identity->last_message = g_strdup_printf ("%s   ******* (%s:%s)i "
589       "(%u bytes, timestamp: %s, duration: %s, offset: %" G_GINT64_FORMAT ", "
590       "offset_end: % " G_GINT64_FORMAT ", flags: %d) %p", action,
591       GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad),
592       GST_BUFFER_SIZE (buf),
593       print_pretty_time (ts_str, sizeof (ts_str), GST_BUFFER_TIMESTAMP (buf)),
594       print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
595       GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
596       GST_BUFFER_FLAGS (buf), buf);
598   GST_OBJECT_UNLOCK (identity);
600   gst_identity_notify_last_message (identity);
603 static GstFlowReturn
604 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
606   GstFlowReturn ret = GST_FLOW_OK;
607   GstIdentity *identity = GST_IDENTITY (trans);
608   GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
610   if (identity->check_perfect)
611     gst_identity_check_perfect (identity, buf);
612   if (identity->check_imperfect_timestamp)
613     gst_identity_check_imperfect_timestamp (identity, buf);
614   if (identity->check_imperfect_offset)
615     gst_identity_check_imperfect_offset (identity, buf);
617   /* update prev values */
618   identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
619   identity->prev_duration = GST_BUFFER_DURATION (buf);
620   identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
621   identity->prev_offset = GST_BUFFER_OFFSET (buf);
623   if (identity->error_after >= 0) {
624     identity->error_after--;
625     if (identity->error_after == 0) {
626       GST_ELEMENT_ERROR (identity, CORE, FAILED,
627           (_("Failed after iterations as requested.")), (NULL));
628       return GST_FLOW_ERROR;
629     }
630   }
632   if (identity->drop_probability > 0.0) {
633     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability) {
634       if (!identity->silent) {
635         gst_identity_update_last_message_for_buffer (identity, "dropping", buf);
636       }
637       /* return DROPPED to basetransform. */
638       return GST_BASE_TRANSFORM_FLOW_DROPPED;
639     }
640   }
642   if (identity->dump) {
643     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
644   }
646   if (!identity->silent) {
647     gst_identity_update_last_message_for_buffer (identity, "chain", buf);
648   }
650   if (identity->datarate > 0) {
651     GstClockTime time = gst_util_uint64_scale_int (identity->offset,
652         GST_SECOND, identity->datarate);
654     GST_BUFFER_TIMESTAMP (buf) = time;
655     GST_BUFFER_DURATION (buf) =
656         GST_BUFFER_SIZE (buf) * GST_SECOND / identity->datarate;
657   }
659   if (identity->signal_handoffs)
660     g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
662   if (trans->segment.format == GST_FORMAT_TIME)
663     runtimestamp = gst_segment_to_running_time (&trans->segment,
664         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
666   if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
667     GstClock *clock;
669     GST_OBJECT_LOCK (identity);
670     if ((clock = GST_ELEMENT (identity)->clock)) {
671       GstClockReturn cret;
672       GstClockTime timestamp;
674       timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
676       /* save id if we need to unlock */
677       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
678       GST_OBJECT_UNLOCK (identity);
680       cret = gst_clock_id_wait (identity->clock_id, NULL);
682       GST_OBJECT_LOCK (identity);
683       if (identity->clock_id) {
684         gst_clock_id_unref (identity->clock_id);
685         identity->clock_id = NULL;
686       }
687       if (cret == GST_CLOCK_UNSCHEDULED)
688         ret = GST_FLOW_UNEXPECTED;
689     }
690     GST_OBJECT_UNLOCK (identity);
691   }
693   identity->offset += GST_BUFFER_SIZE (buf);
695   if (identity->sleep_time && ret == GST_FLOW_OK)
696     g_usleep (identity->sleep_time);
698   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
699       && (ret == GST_FLOW_OK)) {
700     GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
701     GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
702     GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
703   }
705   return ret;
708 static void
709 gst_identity_set_property (GObject * object, guint prop_id,
710     const GValue * value, GParamSpec * pspec)
712   GstIdentity *identity;
714   identity = GST_IDENTITY (object);
716   switch (prop_id) {
717     case PROP_SLEEP_TIME:
718       identity->sleep_time = g_value_get_uint (value);
719       break;
720     case PROP_SILENT:
721       identity->silent = g_value_get_boolean (value);
722       break;
723     case PROP_SINGLE_SEGMENT:
724       identity->single_segment = g_value_get_boolean (value);
725       break;
726     case PROP_DUMP:
727       identity->dump = g_value_get_boolean (value);
728       break;
729     case PROP_ERROR_AFTER:
730       identity->error_after = g_value_get_int (value);
731       break;
732     case PROP_DROP_PROBABILITY:
733       identity->drop_probability = g_value_get_float (value);
734       break;
735     case PROP_DATARATE:
736       identity->datarate = g_value_get_int (value);
737       break;
738     case PROP_SYNC:
739       identity->sync = g_value_get_boolean (value);
740       break;
741     case PROP_CHECK_PERFECT:
742       identity->check_perfect = g_value_get_boolean (value);
743       break;
744     case PROP_CHECK_IMPERFECT_TIMESTAMP:
745       identity->check_imperfect_timestamp = g_value_get_boolean (value);
746       break;
747     case PROP_CHECK_IMPERFECT_OFFSET:
748       identity->check_imperfect_offset = g_value_get_boolean (value);
749       break;
750     case PROP_SIGNAL_HANDOFFS:
751       identity->signal_handoffs = g_value_get_boolean (value);
752       break;
753     default:
754       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
755       break;
756   }
759 static void
760 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
761     GParamSpec * pspec)
763   GstIdentity *identity;
765   identity = GST_IDENTITY (object);
767   switch (prop_id) {
768     case PROP_SLEEP_TIME:
769       g_value_set_uint (value, identity->sleep_time);
770       break;
771     case PROP_ERROR_AFTER:
772       g_value_set_int (value, identity->error_after);
773       break;
774     case PROP_DROP_PROBABILITY:
775       g_value_set_float (value, identity->drop_probability);
776       break;
777     case PROP_DATARATE:
778       g_value_set_int (value, identity->datarate);
779       break;
780     case PROP_SILENT:
781       g_value_set_boolean (value, identity->silent);
782       break;
783     case PROP_SINGLE_SEGMENT:
784       g_value_set_boolean (value, identity->single_segment);
785       break;
786     case PROP_DUMP:
787       g_value_set_boolean (value, identity->dump);
788       break;
789     case PROP_LAST_MESSAGE:
790       GST_OBJECT_LOCK (identity);
791       g_value_set_string (value, identity->last_message);
792       GST_OBJECT_UNLOCK (identity);
793       break;
794     case PROP_SYNC:
795       g_value_set_boolean (value, identity->sync);
796       break;
797     case PROP_CHECK_PERFECT:
798       g_value_set_boolean (value, identity->check_perfect);
799       break;
800     case PROP_CHECK_IMPERFECT_TIMESTAMP:
801       g_value_set_boolean (value, identity->check_imperfect_timestamp);
802       break;
803     case PROP_CHECK_IMPERFECT_OFFSET:
804       g_value_set_boolean (value, identity->check_imperfect_offset);
805       break;
806     case PROP_SIGNAL_HANDOFFS:
807       g_value_set_boolean (value, identity->signal_handoffs);
808       break;
809     default:
810       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
811       break;
812   }
815 static gboolean
816 gst_identity_start (GstBaseTransform * trans)
818   GstIdentity *identity;
820   identity = GST_IDENTITY (trans);
822   identity->offset = 0;
823   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
824   identity->prev_duration = GST_CLOCK_TIME_NONE;
825   identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
826   identity->prev_offset = GST_BUFFER_OFFSET_NONE;
828   return TRUE;
831 static gboolean
832 gst_identity_stop (GstBaseTransform * trans)
834   GstIdentity *identity;
836   identity = GST_IDENTITY (trans);
838   GST_OBJECT_LOCK (identity);
839   g_free (identity->last_message);
840   identity->last_message = NULL;
841   GST_OBJECT_UNLOCK (identity);
843   return TRUE;
846 static GstStateChangeReturn
847 gst_identity_change_state (GstElement * element, GstStateChange transition)
849   GstStateChangeReturn ret;
850   GstIdentity *identity = GST_IDENTITY (element);
852   switch (transition) {
853     case GST_STATE_CHANGE_NULL_TO_READY:
854       break;
855     case GST_STATE_CHANGE_READY_TO_PAUSED:
856       break;
857     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
858       break;
859     case GST_STATE_CHANGE_PAUSED_TO_READY:
860       GST_OBJECT_LOCK (identity);
861       if (identity->clock_id) {
862         GST_DEBUG_OBJECT (identity, "unlock clock wait");
863         gst_clock_id_unschedule (identity->clock_id);
864         gst_clock_id_unref (identity->clock_id);
865         identity->clock_id = NULL;
866       }
867       GST_OBJECT_UNLOCK (identity);
868       break;
869     default:
870       break;
871   }
873   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
875   switch (transition) {
876     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
877       break;
878     case GST_STATE_CHANGE_PAUSED_TO_READY:
879       break;
880     case GST_STATE_CHANGE_READY_TO_NULL:
881       break;
882     default:
883       break;
884   }
886   return ret;