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