ab577b7e6265d9de88a1ec8ed6aae2aca4ca04b5
[platform/upstream/gstreamer.git] / subprojects / gstreamer / 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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 /**
24  * SECTION:element-identity
25  * @title: identity
26  *
27  * Dummy element that passes incoming data through unmodified. It has some
28  * useful diagnostic functions, such as offset and timestamp checking.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "gstelements_private.h"
39 #include <glib/gi18n-lib.h>
40 #include "gstidentity.h"
41 #include "gstcoreelementselements.h"
42
43 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS_ANY);
47
48 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS_ANY);
52
53 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
54 #define GST_CAT_DEFAULT gst_identity_debug
55
56 /* Identity signals and args */
57 enum
58 {
59   SIGNAL_HANDOFF,
60   /* FILL ME */
61   LAST_SIGNAL
62 };
63
64 #define DEFAULT_SLEEP_TIME              0
65 #define DEFAULT_DUPLICATE               1
66 #define DEFAULT_ERROR_AFTER             -1
67 #define DEFAULT_DROP_PROBABILITY        0.0
68 #define DEFAULT_DROP_BUFFER_FLAGS       0
69 #define DEFAULT_DATARATE                0
70 #define DEFAULT_SILENT                  TRUE
71 #define DEFAULT_SINGLE_SEGMENT          FALSE
72 #define DEFAULT_DUMP                    FALSE
73 #define DEFAULT_SYNC                    FALSE
74 #define DEFAULT_CHECK_IMPERFECT_TIMESTAMP FALSE
75 #define DEFAULT_CHECK_IMPERFECT_OFFSET    FALSE
76 #define DEFAULT_SIGNAL_HANDOFFS           TRUE
77 #define DEFAULT_TS_OFFSET               0
78 #define DEFAULT_DROP_ALLOCATION         FALSE
79 #define DEFAULT_EOS_AFTER               -1
80
81 enum
82 {
83   PROP_0,
84   PROP_SLEEP_TIME,
85   PROP_ERROR_AFTER,
86   PROP_DROP_PROBABILITY,
87   PROP_DROP_BUFFER_FLAGS,
88   PROP_DATARATE,
89   PROP_SILENT,
90   PROP_SINGLE_SEGMENT,
91   PROP_LAST_MESSAGE,
92   PROP_DUMP,
93   PROP_SYNC,
94   PROP_TS_OFFSET,
95   PROP_CHECK_IMPERFECT_TIMESTAMP,
96   PROP_CHECK_IMPERFECT_OFFSET,
97   PROP_SIGNAL_HANDOFFS,
98   PROP_DROP_ALLOCATION,
99   PROP_EOS_AFTER,
100   PROP_STATS
101 };
102
103
104 #define _do_init \
105     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
106 #define gst_identity_parent_class parent_class
107 G_DEFINE_TYPE_WITH_CODE (GstIdentity, gst_identity, GST_TYPE_BASE_TRANSFORM,
108     _do_init);
109 GST_ELEMENT_REGISTER_DEFINE (identity, "identity", GST_RANK_NONE,
110     GST_TYPE_IDENTITY);
111
112 static void gst_identity_finalize (GObject * object);
113 static void gst_identity_set_property (GObject * object, guint prop_id,
114     const GValue * value, GParamSpec * pspec);
115 static void gst_identity_get_property (GObject * object, guint prop_id,
116     GValue * value, GParamSpec * pspec);
117
118 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
119     GstEvent * event);
120 static gboolean gst_identity_src_event (GstBaseTransform * trans,
121     GstEvent * event);
122 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
123     GstBuffer * buf);
124 static gboolean gst_identity_start (GstBaseTransform * trans);
125 static gboolean gst_identity_stop (GstBaseTransform * trans);
126 static GstStateChangeReturn gst_identity_change_state (GstElement * element,
127     GstStateChange transition);
128 static gboolean gst_identity_accept_caps (GstBaseTransform * base,
129     GstPadDirection direction, GstCaps * caps);
130 static gboolean gst_identity_query (GstBaseTransform * base,
131     GstPadDirection direction, GstQuery * query);
132 static GstClock *gst_identity_provide_clock (GstElement * element);
133
134 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
135
136 static GParamSpec *pspec_last_message = NULL;
137
138 static void
139 gst_identity_finalize (GObject * object)
140 {
141   GstIdentity *identity;
142
143   identity = GST_IDENTITY (object);
144
145   g_free (identity->last_message);
146   g_cond_clear (&identity->blocked_cond);
147
148   G_OBJECT_CLASS (parent_class)->finalize (object);
149 }
150
151 static void
152 gst_identity_class_init (GstIdentityClass * klass)
153 {
154   GObjectClass *gobject_class;
155   GstElementClass *gstelement_class;
156   GstBaseTransformClass *gstbasetrans_class;
157
158   gobject_class = G_OBJECT_CLASS (klass);
159   gstelement_class = GST_ELEMENT_CLASS (klass);
160   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
161
162   gobject_class->set_property = gst_identity_set_property;
163   gobject_class->get_property = gst_identity_get_property;
164
165   g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
166       g_param_spec_uint ("sleep-time", "Sleep time",
167           "Microseconds to sleep between processing", 0, G_MAXUINT,
168           DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169   g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
170       g_param_spec_int ("error-after", "Error After", "Error after N buffers",
171           -1, G_MAXINT, DEFAULT_ERROR_AFTER,
172           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173   g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
174       g_param_spec_float ("drop-probability", "Drop Probability",
175           "The Probability a buffer is dropped", 0.0, 1.0,
176           DEFAULT_DROP_PROBABILITY,
177           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178
179   /**
180    * GstIdentity:drop-buffer-flags:
181    *
182    * Drop buffers with the given flags.
183    *
184    * Since: 1.8
185    **/
186   g_object_class_install_property (gobject_class, PROP_DROP_BUFFER_FLAGS,
187       g_param_spec_flags ("drop-buffer-flags", "Check flags to drop buffers",
188           "Drop buffers with the given flags",
189           GST_TYPE_BUFFER_FLAGS, DEFAULT_DROP_BUFFER_FLAGS,
190           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191   g_object_class_install_property (gobject_class, PROP_DATARATE,
192       g_param_spec_int ("datarate", "Datarate",
193           "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
194           0, G_MAXINT, DEFAULT_DATARATE,
195           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196   g_object_class_install_property (gobject_class, PROP_SILENT,
197       g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
198           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199   g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
200       g_param_spec_boolean ("single-segment", "Single Segment",
201           "Timestamp buffers and eat segments so as to appear as one segment",
202           DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203   pspec_last_message = g_param_spec_string ("last-message", "last-message",
204       "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
205   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
206       pspec_last_message);
207   g_object_class_install_property (gobject_class, PROP_DUMP,
208       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
209           DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210   g_object_class_install_property (gobject_class, PROP_SYNC,
211       g_param_spec_boolean ("sync", "Synchronize",
212           "Synchronize to pipeline clock", DEFAULT_SYNC,
213           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214   g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
215       g_param_spec_int64 ("ts-offset", "Timestamp offset for synchronisation",
216           "Timestamp offset in nanoseconds for synchronisation, negative for earlier sync",
217           G_MININT64, G_MAXINT64, DEFAULT_TS_OFFSET,
218           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 emitted, which might improve performance.
238    */
239   g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
240       g_param_spec_boolean ("signal-handoffs",
241           "Signal handoffs", "Send a signal before pushing the buffer",
242           DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243
244   g_object_class_install_property (gobject_class, PROP_DROP_ALLOCATION,
245       g_param_spec_boolean ("drop-allocation", "Drop allocation query",
246           "Don't forward allocation queries", DEFAULT_DROP_ALLOCATION,
247           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248
249   /**
250    * GstIdentity:eos-after
251    *
252    * EOS after N buffers.
253    *
254    * Since: 1.16
255    **/
256   g_object_class_install_property (gobject_class, PROP_EOS_AFTER,
257       g_param_spec_int ("eos-after", "EOS After", "EOS after N buffers",
258           -1, G_MAXINT, DEFAULT_EOS_AFTER,
259           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260
261   /**
262    * GstIdentity::handoff:
263    * @identity: the identity instance
264    * @buffer: the buffer that just has been received
265    *
266    * This signal gets emitted before passing the buffer downstream.
267    */
268   gst_identity_signals[SIGNAL_HANDOFF] =
269       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
270       G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
271       NULL, G_TYPE_NONE, 1, GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
272
273   /**
274    * GstIdentity:stats:
275
276    * Various statistics. This property returns a GstStructure
277    * with name application/x-identity-stats with the following fields:
278    *
279    * <itemizedlist>
280    * <listitem>
281    *   <para>
282    *   #guint64
283    *   <classname>&quot;num-buffers&quot;</classname>:
284    *   the number of buffers that passed through.
285    *   </para>
286    * </listitem>
287    * <listitem>
288    *   <para>
289    *   #guint64
290    *   <classname>&quot;num-bytes&quot;</classname>:
291    *   the number of bytes that passed through.
292    *   </para>
293    * </listitem>
294    * </itemizedlist>
295    *
296    * Since: 1.20
297    */
298   g_object_class_install_property (gobject_class, PROP_STATS,
299       g_param_spec_boxed ("stats", "Statistics",
300           "Statistics", GST_TYPE_STRUCTURE,
301           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
302
303   gobject_class->finalize = gst_identity_finalize;
304
305   gst_element_class_set_static_metadata (gstelement_class,
306       "Identity",
307       "Generic",
308       "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
309   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
310   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
311
312   gstelement_class->change_state =
313       GST_DEBUG_FUNCPTR (gst_identity_change_state);
314   gstelement_class->provide_clock =
315       GST_DEBUG_FUNCPTR (gst_identity_provide_clock);
316
317   gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
318   gstbasetrans_class->src_event = GST_DEBUG_FUNCPTR (gst_identity_src_event);
319   gstbasetrans_class->transform_ip =
320       GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
321   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
322   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
323   gstbasetrans_class->accept_caps =
324       GST_DEBUG_FUNCPTR (gst_identity_accept_caps);
325   gstbasetrans_class->query = gst_identity_query;
326 }
327
328 static void
329 gst_identity_init (GstIdentity * identity)
330 {
331   identity->sleep_time = DEFAULT_SLEEP_TIME;
332   identity->error_after = DEFAULT_ERROR_AFTER;
333   identity->error_after_counter = DEFAULT_ERROR_AFTER;
334   identity->drop_probability = DEFAULT_DROP_PROBABILITY;
335   identity->drop_buffer_flags = DEFAULT_DROP_BUFFER_FLAGS;
336   identity->datarate = DEFAULT_DATARATE;
337   identity->silent = DEFAULT_SILENT;
338   identity->single_segment = DEFAULT_SINGLE_SEGMENT;
339   identity->sync = DEFAULT_SYNC;
340   identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
341   identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
342   identity->dump = DEFAULT_DUMP;
343   identity->last_message = NULL;
344   identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
345   identity->ts_offset = DEFAULT_TS_OFFSET;
346   g_cond_init (&identity->blocked_cond);
347   identity->eos_after = DEFAULT_EOS_AFTER;
348   identity->eos_after_counter = DEFAULT_EOS_AFTER;
349
350   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
351
352   GST_OBJECT_FLAG_SET (identity, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
353 }
354
355 static void
356 gst_identity_notify_last_message (GstIdentity * identity)
357 {
358   g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
359 }
360
361 static GstFlowReturn
362 gst_identity_do_sync (GstIdentity * identity, GstClockTime running_time)
363 {
364   GstFlowReturn ret = GST_FLOW_OK;
365
366   if (identity->sync &&
367       GST_BASE_TRANSFORM_CAST (identity)->segment.format == GST_FORMAT_TIME) {
368     GstClock *clock;
369
370     GST_OBJECT_LOCK (identity);
371
372     if (identity->flushing) {
373       GST_OBJECT_UNLOCK (identity);
374       return GST_FLOW_FLUSHING;
375     }
376
377     while (identity->blocked && !identity->flushing)
378       g_cond_wait (&identity->blocked_cond, GST_OBJECT_GET_LOCK (identity));
379
380     if (identity->flushing) {
381       GST_OBJECT_UNLOCK (identity);
382       return GST_FLOW_FLUSHING;
383     }
384
385     if ((clock = GST_ELEMENT (identity)->clock)) {
386       GstClockReturn cret;
387       GstClockTime timestamp;
388       GstClockTimeDiff ts_offset = identity->ts_offset;
389
390       timestamp = running_time + GST_ELEMENT (identity)->base_time +
391           identity->upstream_latency;
392       if (ts_offset < 0) {
393         ts_offset = -ts_offset;
394         if (ts_offset < timestamp)
395           timestamp -= ts_offset;
396         else
397           timestamp = 0;
398       } else
399         timestamp += ts_offset;
400
401       /* save id if we need to unlock */
402       identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
403       GST_OBJECT_UNLOCK (identity);
404
405       cret = gst_clock_id_wait (identity->clock_id, NULL);
406
407       GST_OBJECT_LOCK (identity);
408       if (identity->clock_id) {
409         gst_clock_id_unref (identity->clock_id);
410         identity->clock_id = NULL;
411       }
412       if (cret == GST_CLOCK_UNSCHEDULED || identity->flushing)
413         ret = GST_FLOW_FLUSHING;
414     }
415     GST_OBJECT_UNLOCK (identity);
416   }
417
418   return ret;
419 }
420
421 static gboolean
422 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
423 {
424   GstIdentity *identity;
425   gboolean ret = TRUE;
426
427   identity = GST_IDENTITY (trans);
428
429   if (!identity->silent) {
430     const GstStructure *s;
431     const gchar *tstr;
432     gchar *sstr;
433
434     GST_OBJECT_LOCK (identity);
435     g_free (identity->last_message);
436
437     tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
438     if ((s = gst_event_get_structure (event)))
439       sstr = gst_structure_to_string (s);
440     else
441       sstr = g_strdup ("");
442
443     identity->last_message =
444         g_strdup_printf ("event   ******* (%s:%s) E (type: %s (%d), %s) %p",
445         GST_DEBUG_PAD_NAME (trans->sinkpad), tstr, GST_EVENT_TYPE (event),
446         sstr, event);
447     g_free (sstr);
448     GST_OBJECT_UNLOCK (identity);
449
450     gst_identity_notify_last_message (identity);
451   }
452
453   if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
454     if (!trans->have_segment) {
455       GstEvent *news;
456       GstSegment segment;
457
458       gst_event_copy_segment (event, &segment);
459       gst_event_copy_segment (event, &trans->segment);
460       trans->have_segment = TRUE;
461
462       /* This is the first segment, send out a (0, -1) segment */
463       gst_segment_init (&segment, segment.format);
464       if (identity->seek_segment.format != GST_FORMAT_UNDEFINED) {
465         segment.time = identity->seek_segment.time;
466         segment.base = identity->seek_segment.base;
467         gst_segment_init (&identity->seek_segment, GST_FORMAT_UNDEFINED);
468       }
469
470       news = gst_event_new_segment (&segment);
471       GST_EVENT_SEQNUM (news) = GST_EVENT_SEQNUM (event);
472
473       gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
474     } else {
475       /* need to track segment for proper running time */
476       gst_event_copy_segment (event, &trans->segment);
477     }
478   }
479
480   if (GST_EVENT_TYPE (event) == GST_EVENT_GAP &&
481       trans->have_segment && trans->segment.format == GST_FORMAT_TIME) {
482     GstClockTime start, dur, old_start;
483
484     gst_event_parse_gap (event, &old_start, &dur);
485
486     start = gst_segment_to_running_time (&trans->segment,
487         GST_FORMAT_TIME, old_start);
488
489     gst_identity_do_sync (identity,
490         GST_CLOCK_TIME_IS_VALID (start) ? start : 0);
491
492     /* also transform GAP timestamp similar to buffer timestamps */
493     if (identity->single_segment) {
494       guint64 clip_start, clip_stop;
495       if (GST_CLOCK_TIME_IS_VALID (start)) {
496         gst_event_unref (event);
497         event = gst_event_new_gap (start, dur);
498       } else if (GST_CLOCK_TIME_IS_VALID (dur) &&
499           gst_segment_clip (&trans->segment, GST_FORMAT_TIME, old_start,
500               old_start + dur, &clip_start, &clip_stop)) {
501         gst_event_unref (event);
502         event = gst_event_new_gap (clip_start, clip_stop - clip_start);
503       } else {
504         /* Gap event is completely outside the segment, drop it */
505         GST_DEBUG_OBJECT (identity,
506             "Dropping GAP event outside segment: %" GST_PTR_FORMAT, event);
507         gst_event_unref (event);
508         ret = TRUE;
509         goto done;
510       }
511     }
512   }
513
514   /* Reset previous timestamp, duration and offsets on SEGMENT
515    * to prevent false warnings when checking for perfect streams */
516   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
517     identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
518     identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
519   }
520
521   if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
522     /* eat up segments */
523     gst_event_unref (event);
524     ret = TRUE;
525     goto done;
526   }
527
528   switch (GST_EVENT_TYPE (event)) {
529     case GST_EVENT_FLUSH_START:
530       GST_OBJECT_LOCK (identity);
531       identity->flushing = TRUE;
532       g_cond_signal (&identity->blocked_cond);
533       if (identity->clock_id) {
534         GST_DEBUG_OBJECT (identity, "unlock clock wait");
535         gst_clock_id_unschedule (identity->clock_id);
536       }
537       GST_OBJECT_UNLOCK (identity);
538       break;
539     case GST_EVENT_FLUSH_STOP:
540       GST_OBJECT_LOCK (identity);
541       identity->flushing = FALSE;
542       trans->have_segment = FALSE;
543       GST_OBJECT_UNLOCK (identity);
544       break;
545     default:
546       break;
547   }
548
549   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
550
551 done:
552   return ret;
553 }
554
555 static gboolean
556 gst_identity_src_event (GstBaseTransform * trans, GstEvent * event)
557 {
558   GstIdentity *identity = GST_IDENTITY (trans);
559
560   switch (GST_EVENT_TYPE (event)) {
561     case GST_EVENT_SEEK:
562     {
563       gdouble rate;
564       GstFormat fmt;
565       GstSeekFlags flags;
566       GstSeekType start_type, stop_type;
567       gint64 start, stop;
568       gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
569           &start, &stop_type, &stop);
570
571       GST_OBJECT_LOCK (identity);
572       gst_segment_init (&identity->seek_segment, fmt);
573       if (!gst_segment_do_seek (&identity->seek_segment, rate, fmt,
574               flags, start_type, start, stop_type, stop, NULL)) {
575         GST_WARNING_OBJECT (identity, "Could not run seek %" GST_PTR_FORMAT,
576             event);
577         GST_OBJECT_UNLOCK (identity);
578
579         return FALSE;
580       }
581       GST_OBJECT_UNLOCK (identity);
582
583       break;
584     }
585     default:
586       break;
587   }
588
589   return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
590 }
591
592 static void
593 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
594 {
595   GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
596
597   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
598   if (timestamp != GST_CLOCK_TIME_NONE) {
599     /* check if we had a previous buffer to compare to */
600     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
601         identity->prev_duration != GST_CLOCK_TIME_NONE) {
602       GstClockTime t_expected;
603       GstClockTimeDiff dt;
604
605       t_expected = identity->prev_timestamp + identity->prev_duration;
606       dt = GST_CLOCK_DIFF (t_expected, timestamp);
607       if (dt != 0) {
608         /*
609          * "imperfect-timestamp" bus message:
610          * @identity:        the identity instance
611          * @delta:           the GST_CLOCK_DIFF to the prev timestamp
612          * @prev-timestamp:  the previous buffer timestamp
613          * @prev-duration:   the previous buffer duration
614          * @prev-offset:     the previous buffer offset
615          * @prev-offset-end: the previous buffer offset end
616          * @cur-timestamp:   the current buffer timestamp
617          * @cur-duration:    the current buffer duration
618          * @cur-offset:      the current buffer offset
619          * @cur-offset-end:  the current buffer offset end
620          *
621          * This bus message gets emitted if the check-imperfect-timestamp
622          * property is set and there is a gap in time between the
623          * last buffer and the newly received buffer.
624          */
625         gst_element_post_message (GST_ELEMENT (identity),
626             gst_message_new_element (GST_OBJECT (identity),
627                 gst_structure_new ("imperfect-timestamp",
628                     "delta", G_TYPE_INT64, dt,
629                     "prev-timestamp", G_TYPE_UINT64,
630                     identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
631                     identity->prev_duration, "prev-offset", G_TYPE_UINT64,
632                     identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
633                     identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
634                     timestamp, "cur-duration", G_TYPE_UINT64,
635                     GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
636                     GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
637                     GST_BUFFER_OFFSET_END (buf), NULL)));
638       }
639     } else {
640       GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
641           "offset_end was set on previous buffer");
642     }
643   }
644 }
645
646 static void
647 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
648 {
649   guint64 offset;
650
651   offset = GST_BUFFER_OFFSET (buf);
652
653   if (identity->prev_offset_end != offset &&
654       identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
655       offset != GST_BUFFER_OFFSET_NONE) {
656     /*
657      * "imperfect-offset" bus message:
658      * @identity:        the identity instance
659      * @prev-timestamp:  the previous buffer timestamp
660      * @prev-duration:   the previous buffer duration
661      * @prev-offset:     the previous buffer offset
662      * @prev-offset-end: the previous buffer offset end
663      * @cur-timestamp:   the current buffer timestamp
664      * @cur-duration:    the current buffer duration
665      * @cur-offset:      the current buffer offset
666      * @cur-offset-end:  the current buffer offset end
667      *
668      * This bus message gets emitted if the check-imperfect-offset
669      * property is set and there is a gap in offsets between the
670      * last buffer and the newly received buffer.
671      */
672     gst_element_post_message (GST_ELEMENT (identity),
673         gst_message_new_element (GST_OBJECT (identity),
674             gst_structure_new ("imperfect-offset", "prev-timestamp",
675                 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
676                 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
677                 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
678                 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
679                 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
680                 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
681                 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
682                 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
683   } else {
684     GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
685         "and/or offset_end were set on previous buffer");
686   }
687 }
688
689 static const gchar *
690 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
691 {
692   if (ts == GST_CLOCK_TIME_NONE)
693     return "none";
694
695   g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
696   return ts_str;
697 }
698
699 static void
700 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
701     const gchar * action, GstBuffer * buf, gsize size)
702 {
703   gchar dts_str[64], pts_str[64], dur_str[64];
704   gchar *flag_str, *meta_str;
705
706   GST_OBJECT_LOCK (identity);
707
708   flag_str = gst_buffer_get_flags_string (buf);
709   meta_str = gst_buffer_get_meta_string (buf);
710
711   g_free (identity->last_message);
712   identity->last_message = g_strdup_printf ("%s   ******* (%s:%s) "
713       "(%" G_GSIZE_FORMAT " bytes, dts: %s, pts: %s, duration: %s, offset: %"
714       G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
715       ", flags: %08x %s, meta: %s) %p", action,
716       GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
717       print_pretty_time (dts_str, sizeof (dts_str), GST_BUFFER_DTS (buf)),
718       print_pretty_time (pts_str, sizeof (pts_str), GST_BUFFER_PTS (buf)),
719       print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
720       GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
721       GST_BUFFER_FLAGS (buf), flag_str, meta_str ? meta_str : "none", buf);
722   g_free (flag_str);
723   g_free (meta_str);
724   GST_TRACE_OBJECT (identity, "%s", identity->last_message);
725
726   GST_OBJECT_UNLOCK (identity);
727
728   gst_identity_notify_last_message (identity);
729 }
730
731 static GstFlowReturn
732 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
733 {
734   GstFlowReturn ret = GST_FLOW_OK;
735   GstIdentity *identity = GST_IDENTITY (trans);
736   GstClockTime rundts = GST_CLOCK_TIME_NONE;
737   GstClockTime runpts = GST_CLOCK_TIME_NONE;
738   GstClockTime ts, duration, runtimestamp;
739   gsize size;
740
741   size = gst_buffer_get_size (buf);
742
743   if (identity->check_imperfect_timestamp)
744     gst_identity_check_imperfect_timestamp (identity, buf);
745   if (identity->check_imperfect_offset)
746     gst_identity_check_imperfect_offset (identity, buf);
747
748   /* update prev values */
749   identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
750   identity->prev_duration = GST_BUFFER_DURATION (buf);
751   identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
752   identity->prev_offset = GST_BUFFER_OFFSET (buf);
753
754   if (identity->error_after_counter >= 0) {
755     identity->error_after_counter--;
756     if (identity->error_after_counter == 0)
757       goto error_after;
758   }
759
760   if (identity->eos_after_counter >= 0) {
761     identity->eos_after_counter--;
762     if (identity->eos_after_counter == 0)
763       goto eos_after;
764   }
765
766   if (identity->drop_probability > 0.0) {
767     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
768       goto dropped;
769   }
770
771   if (GST_BUFFER_FLAG_IS_SET (buf, identity->drop_buffer_flags))
772     goto dropped;
773
774   if (identity->dump) {
775     GstMapInfo info;
776
777     if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
778       gst_util_dump_mem (info.data, info.size);
779       gst_buffer_unmap (buf, &info);
780     }
781   }
782
783   if (!identity->silent) {
784     gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
785   }
786
787   if (identity->datarate > 0) {
788     GstClockTime time = gst_util_uint64_scale_int (identity->offset,
789         GST_SECOND, identity->datarate);
790
791     GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf) = time;
792     GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
793   }
794
795   if (identity->signal_handoffs)
796     g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
797
798   if (trans->segment.format == GST_FORMAT_TIME) {
799     if (trans->segment.rate > 0) {
800       runpts = gst_segment_to_running_time (&trans->segment,
801           GST_FORMAT_TIME, GST_BUFFER_PTS (buf));
802       rundts = gst_segment_to_running_time (&trans->segment,
803           GST_FORMAT_TIME, GST_BUFFER_DTS (buf));
804     } else {
805       runpts = gst_segment_to_running_time (&trans->segment,
806           GST_FORMAT_TIME, GST_CLOCK_TIME_IS_VALID (buf->duration)
807           && GST_CLOCK_TIME_IS_VALID (buf->pts) ? buf->pts +
808           buf->duration : buf->pts);
809       rundts = gst_segment_to_running_time (&trans->segment,
810           GST_FORMAT_TIME, GST_CLOCK_TIME_IS_VALID (buf->duration)
811           && GST_CLOCK_TIME_IS_VALID (buf->dts) ? buf->dts +
812           buf->duration : buf->dts);
813     }
814   }
815
816   if (GST_CLOCK_TIME_IS_VALID (rundts))
817     runtimestamp = rundts;
818   else if (GST_CLOCK_TIME_IS_VALID (runpts))
819     runtimestamp = runpts;
820   else
821     runtimestamp = 0;
822   ret = gst_identity_do_sync (identity, runtimestamp);
823
824   identity->offset += size;
825
826   if (identity->sleep_time && ret == GST_FLOW_OK)
827     g_usleep (identity->sleep_time);
828
829   if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
830       && (ret == GST_FLOW_OK)) {
831     GST_BUFFER_DTS (buf) = rundts;
832     GST_BUFFER_PTS (buf) = runpts;
833     GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
834     GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
835   }
836
837   GST_OBJECT_LOCK (trans);
838   identity->num_bytes += gst_buffer_get_size (buf);
839   identity->num_buffers++;
840   GST_OBJECT_UNLOCK (trans);
841
842   return ret;
843
844   /* ERRORS */
845 error_after:
846   {
847     GST_ELEMENT_ERROR (identity, CORE, FAILED,
848         (_("Failed after iterations as requested.")), (NULL));
849     return GST_FLOW_ERROR;
850   }
851 eos_after:
852   {
853     GST_DEBUG_OBJECT (identity, "EOS after iterations as requested.");
854     return GST_FLOW_EOS;
855   }
856 dropped:
857   {
858     if (!identity->silent) {
859       gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
860           size);
861     }
862
863     ts = GST_BUFFER_TIMESTAMP (buf);
864     if (GST_CLOCK_TIME_IS_VALID (ts)) {
865       duration = GST_BUFFER_DURATION (buf);
866       gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (identity),
867           gst_event_new_gap (ts, duration));
868     }
869
870     /* return DROPPED to basetransform. */
871     return GST_BASE_TRANSFORM_FLOW_DROPPED;
872   }
873 }
874
875 static void
876 gst_identity_set_property (GObject * object, guint prop_id,
877     const GValue * value, GParamSpec * pspec)
878 {
879   GstIdentity *identity;
880   GstMessage *clock_message = NULL;
881   gboolean sync;
882
883   identity = GST_IDENTITY (object);
884
885   switch (prop_id) {
886     case PROP_SLEEP_TIME:
887       identity->sleep_time = g_value_get_uint (value);
888       break;
889     case PROP_SILENT:
890       identity->silent = g_value_get_boolean (value);
891       break;
892     case PROP_SINGLE_SEGMENT:
893       identity->single_segment = g_value_get_boolean (value);
894       break;
895     case PROP_DUMP:
896       identity->dump = g_value_get_boolean (value);
897       break;
898     case PROP_ERROR_AFTER:
899       identity->error_after = g_value_get_int (value);
900       break;
901     case PROP_DROP_PROBABILITY:
902       identity->drop_probability = g_value_get_float (value);
903       break;
904     case PROP_DROP_BUFFER_FLAGS:
905       identity->drop_buffer_flags = g_value_get_flags (value);
906       break;
907     case PROP_DATARATE:
908       identity->datarate = g_value_get_int (value);
909       break;
910     case PROP_SYNC:
911       sync = g_value_get_boolean (value);
912       GST_OBJECT_LOCK (identity);
913       if (sync != identity->sync) {
914         identity->sync = sync;
915         if (sync) {
916           GST_OBJECT_FLAG_SET (identity, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
917           clock_message =
918               gst_message_new_clock_provide (GST_OBJECT_CAST (identity),
919               gst_system_clock_obtain (), TRUE);
920         } else {
921           GST_OBJECT_FLAG_UNSET (identity, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
922           clock_message =
923               gst_message_new_clock_lost (GST_OBJECT_CAST (identity),
924               gst_system_clock_obtain ());
925         }
926       }
927       GST_OBJECT_UNLOCK (identity);
928       if (clock_message)
929         gst_element_post_message (GST_ELEMENT_CAST (identity), clock_message);
930       break;
931     case PROP_TS_OFFSET:
932       identity->ts_offset = g_value_get_int64 (value);
933       break;
934     case PROP_CHECK_IMPERFECT_TIMESTAMP:
935       identity->check_imperfect_timestamp = g_value_get_boolean (value);
936       break;
937     case PROP_CHECK_IMPERFECT_OFFSET:
938       identity->check_imperfect_offset = g_value_get_boolean (value);
939       break;
940     case PROP_SIGNAL_HANDOFFS:
941       identity->signal_handoffs = g_value_get_boolean (value);
942       break;
943     case PROP_DROP_ALLOCATION:
944       identity->drop_allocation = g_value_get_boolean (value);
945       break;
946     case PROP_EOS_AFTER:
947       identity->eos_after = g_value_get_int (value);
948       break;
949     default:
950       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
951       break;
952   }
953   if (identity->datarate > 0 || identity->single_segment)
954     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
955   else
956     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
957 }
958
959 static GstStructure *
960 gst_identity_create_stats (GstIdentity * identity)
961 {
962   GstStructure *s;
963
964   GST_OBJECT_LOCK (identity);
965   s = gst_structure_new ("application/x-identity-stats",
966       "num-bytes", G_TYPE_UINT64, identity->num_bytes,
967       "num-buffers", G_TYPE_UINT64, identity->num_buffers, NULL);
968   GST_OBJECT_UNLOCK (identity);
969
970   return s;
971 }
972
973 static void
974 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
975     GParamSpec * pspec)
976 {
977   GstIdentity *identity;
978
979   identity = GST_IDENTITY (object);
980
981   switch (prop_id) {
982     case PROP_SLEEP_TIME:
983       g_value_set_uint (value, identity->sleep_time);
984       break;
985     case PROP_ERROR_AFTER:
986       g_value_set_int (value, identity->error_after);
987       break;
988     case PROP_DROP_PROBABILITY:
989       g_value_set_float (value, identity->drop_probability);
990       break;
991     case PROP_DROP_BUFFER_FLAGS:
992       g_value_set_flags (value, identity->drop_buffer_flags);
993       break;
994     case PROP_DATARATE:
995       g_value_set_int (value, identity->datarate);
996       break;
997     case PROP_SILENT:
998       g_value_set_boolean (value, identity->silent);
999       break;
1000     case PROP_SINGLE_SEGMENT:
1001       g_value_set_boolean (value, identity->single_segment);
1002       break;
1003     case PROP_DUMP:
1004       g_value_set_boolean (value, identity->dump);
1005       break;
1006     case PROP_LAST_MESSAGE:
1007       GST_OBJECT_LOCK (identity);
1008       g_value_set_string (value, identity->last_message);
1009       GST_OBJECT_UNLOCK (identity);
1010       break;
1011     case PROP_SYNC:
1012       g_value_set_boolean (value, identity->sync);
1013       break;
1014     case PROP_TS_OFFSET:
1015       g_value_set_int64 (value, identity->ts_offset);
1016       break;
1017     case PROP_CHECK_IMPERFECT_TIMESTAMP:
1018       g_value_set_boolean (value, identity->check_imperfect_timestamp);
1019       break;
1020     case PROP_CHECK_IMPERFECT_OFFSET:
1021       g_value_set_boolean (value, identity->check_imperfect_offset);
1022       break;
1023     case PROP_SIGNAL_HANDOFFS:
1024       g_value_set_boolean (value, identity->signal_handoffs);
1025       break;
1026     case PROP_DROP_ALLOCATION:
1027       g_value_set_boolean (value, identity->drop_allocation);
1028       break;
1029     case PROP_EOS_AFTER:
1030       g_value_set_int (value, identity->eos_after);
1031       break;
1032     case PROP_STATS:
1033       g_value_take_boxed (value, gst_identity_create_stats (identity));
1034       break;
1035     default:
1036       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1037       break;
1038   }
1039 }
1040
1041 static gboolean
1042 gst_identity_start (GstBaseTransform * trans)
1043 {
1044   GstIdentity *identity;
1045
1046   identity = GST_IDENTITY (trans);
1047
1048   if (identity->eos_after != DEFAULT_EOS_AFTER
1049       && identity->error_after != DEFAULT_ERROR_AFTER)
1050     goto both_afters_defined;
1051
1052   identity->offset = 0;
1053   identity->prev_timestamp = GST_CLOCK_TIME_NONE;
1054   identity->prev_duration = GST_CLOCK_TIME_NONE;
1055   identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
1056   identity->prev_offset = GST_BUFFER_OFFSET_NONE;
1057   identity->error_after_counter = identity->error_after;
1058   identity->eos_after_counter = identity->eos_after;
1059
1060   return TRUE;
1061
1062   /* ERROR */
1063 both_afters_defined:
1064   {
1065     GST_ELEMENT_ERROR (identity, CORE, FAILED,
1066         (_("eos-after and error-after can't both be defined.")), (NULL));
1067     return FALSE;
1068   }
1069 }
1070
1071 static gboolean
1072 gst_identity_stop (GstBaseTransform * trans)
1073 {
1074   GstIdentity *identity;
1075
1076   identity = GST_IDENTITY (trans);
1077
1078   GST_OBJECT_LOCK (identity);
1079   g_free (identity->last_message);
1080   identity->last_message = NULL;
1081   GST_OBJECT_UNLOCK (identity);
1082
1083   return TRUE;
1084 }
1085
1086 static gboolean
1087 gst_identity_accept_caps (GstBaseTransform * base,
1088     GstPadDirection direction, GstCaps * caps)
1089 {
1090   gboolean ret;
1091   GstPad *pad;
1092
1093   /* Proxy accept-caps */
1094
1095   if (direction == GST_PAD_SRC)
1096     pad = GST_BASE_TRANSFORM_SINK_PAD (base);
1097   else
1098     pad = GST_BASE_TRANSFORM_SRC_PAD (base);
1099
1100   ret = gst_pad_peer_query_accept_caps (pad, caps);
1101
1102   return ret;
1103 }
1104
1105 static gboolean
1106 gst_identity_query (GstBaseTransform * base, GstPadDirection direction,
1107     GstQuery * query)
1108 {
1109   GstIdentity *identity;
1110   gboolean ret;
1111
1112   identity = GST_IDENTITY (base);
1113
1114   if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION &&
1115       identity->drop_allocation) {
1116     GST_DEBUG_OBJECT (identity, "Dropping allocation query.");
1117     return FALSE;
1118   }
1119
1120   ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (base, direction, query);
1121
1122   if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
1123     gboolean live = FALSE;
1124     GstClockTime min = 0, max = 0;
1125
1126     if (ret) {
1127       gst_query_parse_latency (query, &live, &min, &max);
1128
1129       if (identity->sync && max < min) {
1130         GST_ELEMENT_WARNING (base, CORE, CLOCK, (NULL),
1131             ("Impossible to configure latency before identity sync=true:"
1132                 " max %" GST_TIME_FORMAT " < min %"
1133                 GST_TIME_FORMAT ". Add queues or other buffering elements.",
1134                 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
1135       }
1136     }
1137
1138     /* Ignore the upstream latency if it is not live */
1139     GST_OBJECT_LOCK (identity);
1140     if (live)
1141       identity->upstream_latency = min;
1142     else {
1143       identity->upstream_latency = 0;
1144       /* if we interface a non-live source, then we know there is no
1145        * limit in the maximum latency */
1146       max = -1;
1147     }
1148     GST_OBJECT_UNLOCK (identity);
1149
1150     gst_query_set_latency (query, live || identity->sync, min, max);
1151     ret = TRUE;
1152   }
1153   return ret;
1154 }
1155
1156 static GstStateChangeReturn
1157 gst_identity_change_state (GstElement * element, GstStateChange transition)
1158 {
1159   GstStateChangeReturn ret;
1160   GstIdentity *identity = GST_IDENTITY (element);
1161   gboolean no_preroll = FALSE;
1162
1163   switch (transition) {
1164     case GST_STATE_CHANGE_NULL_TO_READY:
1165       break;
1166     case GST_STATE_CHANGE_READY_TO_PAUSED:
1167       GST_OBJECT_LOCK (identity);
1168       identity->flushing = FALSE;
1169       identity->blocked = TRUE;
1170       GST_OBJECT_UNLOCK (identity);
1171       if (identity->sync)
1172         no_preroll = TRUE;
1173       identity->num_bytes = 0;
1174       identity->num_buffers = 0;
1175       break;
1176     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1177       GST_OBJECT_LOCK (identity);
1178       identity->blocked = FALSE;
1179       g_cond_signal (&identity->blocked_cond);
1180       GST_OBJECT_UNLOCK (identity);
1181       break;
1182     case GST_STATE_CHANGE_PAUSED_TO_READY:
1183       GST_OBJECT_LOCK (identity);
1184       identity->flushing = TRUE;
1185       if (identity->clock_id) {
1186         GST_DEBUG_OBJECT (identity, "unlock clock wait");
1187         gst_clock_id_unschedule (identity->clock_id);
1188       }
1189       identity->blocked = FALSE;
1190       g_cond_signal (&identity->blocked_cond);
1191       GST_OBJECT_UNLOCK (identity);
1192       break;
1193     default:
1194       break;
1195   }
1196
1197   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1198
1199   switch (transition) {
1200     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1201       GST_OBJECT_LOCK (identity);
1202       identity->upstream_latency = 0;
1203       identity->blocked = TRUE;
1204       GST_OBJECT_UNLOCK (identity);
1205       if (identity->sync)
1206         no_preroll = TRUE;
1207       break;
1208     case GST_STATE_CHANGE_PAUSED_TO_READY:
1209       break;
1210     case GST_STATE_CHANGE_READY_TO_NULL:
1211       break;
1212     default:
1213       break;
1214   }
1215
1216   if (no_preroll && ret == GST_STATE_CHANGE_SUCCESS)
1217     ret = GST_STATE_CHANGE_NO_PREROLL;
1218
1219   return ret;
1220 }
1221
1222 /* FIXME: GStreamer 2.0 */
1223 static GstClock *
1224 gst_identity_provide_clock (GstElement * element)
1225 {
1226   GstIdentity *identity = GST_IDENTITY (element);
1227
1228   if (!identity->sync)
1229     return NULL;
1230
1231   return gst_system_clock_obtain ();
1232 }