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