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>
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.
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.
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.
24 * SECTION:element-identity
27 * Dummy element that passes incoming data through unmodified. It has some
28 * useful diagnostic functions, such as offset and timestamp checking.
38 #include "gstelements_private.h"
39 #include <glib/gi18n-lib.h>
40 #include "gstidentity.h"
41 #include "gstcoreelementselements.h"
43 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
48 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
53 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
54 #define GST_CAT_DEFAULT gst_identity_debug
56 /* Identity signals and args */
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
86 PROP_DROP_PROBABILITY,
87 PROP_DROP_BUFFER_FLAGS,
95 PROP_CHECK_IMPERFECT_TIMESTAMP,
96 PROP_CHECK_IMPERFECT_OFFSET,
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,
109 GST_ELEMENT_REGISTER_DEFINE (identity, "identity", GST_RANK_NONE,
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);
118 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
120 static gboolean gst_identity_src_event (GstBaseTransform * trans,
122 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
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);
134 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
136 static GParamSpec *pspec_last_message = NULL;
139 gst_identity_finalize (GObject * object)
141 GstIdentity *identity;
143 identity = GST_IDENTITY (object);
145 g_free (identity->last_message);
146 g_cond_clear (&identity->blocked_cond);
148 G_OBJECT_CLASS (parent_class)->finalize (object);
152 gst_identity_class_init (GstIdentityClass * klass)
154 GObjectClass *gobject_class;
155 GstElementClass *gstelement_class;
156 GstBaseTransformClass *gstbasetrans_class;
158 gobject_class = G_OBJECT_CLASS (klass);
159 gstelement_class = GST_ELEMENT_CLASS (klass);
160 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
162 gobject_class->set_property = gst_identity_set_property;
163 gobject_class->get_property = gst_identity_get_property;
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));
180 * GstIdentity:drop-buffer-flags:
182 * Drop buffers with the given flags.
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,
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));
234 * GstIdentity:signal-handoffs
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.
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));
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));
250 * GstIdentity:eos-after
252 * EOS after N buffers.
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));
262 * GstIdentity::handoff:
263 * @identity: the identity instance
264 * @buffer: the buffer that just has been received
266 * This signal gets emitted before passing the buffer downstream.
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);
276 * Various statistics. This property returns a GstStructure
277 * with name application/x-identity-stats with the following fields:
283 * <classname>"num-buffers"</classname>:
284 * the number of buffers that passed through.
290 * <classname>"num-bytes"</classname>:
291 * the number of bytes that passed through.
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));
303 gobject_class->finalize = gst_identity_finalize;
305 gst_element_class_set_static_metadata (gstelement_class,
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);
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);
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;
329 gst_identity_init (GstIdentity * identity)
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;
350 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
352 GST_OBJECT_FLAG_SET (identity, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
356 gst_identity_notify_last_message (GstIdentity * identity)
358 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
362 gst_identity_do_sync (GstIdentity * identity, GstClockTime running_time)
364 GstFlowReturn ret = GST_FLOW_OK;
366 if (identity->sync &&
367 GST_BASE_TRANSFORM_CAST (identity)->segment.format == GST_FORMAT_TIME) {
370 GST_OBJECT_LOCK (identity);
372 if (identity->flushing) {
373 GST_OBJECT_UNLOCK (identity);
374 return GST_FLOW_FLUSHING;
377 while (identity->blocked && !identity->flushing)
378 g_cond_wait (&identity->blocked_cond, GST_OBJECT_GET_LOCK (identity));
380 if (identity->flushing) {
381 GST_OBJECT_UNLOCK (identity);
382 return GST_FLOW_FLUSHING;
385 if ((clock = GST_ELEMENT (identity)->clock)) {
387 GstClockTime timestamp;
388 GstClockTimeDiff ts_offset = identity->ts_offset;
390 timestamp = running_time + GST_ELEMENT (identity)->base_time +
391 identity->upstream_latency;
393 ts_offset = -ts_offset;
394 if (ts_offset < timestamp)
395 timestamp -= ts_offset;
399 timestamp += ts_offset;
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);
405 cret = gst_clock_id_wait (identity->clock_id, NULL);
407 GST_OBJECT_LOCK (identity);
408 if (identity->clock_id) {
409 gst_clock_id_unref (identity->clock_id);
410 identity->clock_id = NULL;
412 if (cret == GST_CLOCK_UNSCHEDULED || identity->flushing)
413 ret = GST_FLOW_FLUSHING;
415 GST_OBJECT_UNLOCK (identity);
422 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
424 GstIdentity *identity;
427 identity = GST_IDENTITY (trans);
429 if (!identity->silent) {
430 const GstStructure *s;
434 GST_OBJECT_LOCK (identity);
435 g_free (identity->last_message);
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);
441 sstr = g_strdup ("");
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),
448 GST_OBJECT_UNLOCK (identity);
450 gst_identity_notify_last_message (identity);
453 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
454 if (!trans->have_segment) {
458 gst_event_copy_segment (event, &segment);
459 gst_event_copy_segment (event, &trans->segment);
460 trans->have_segment = TRUE;
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);
470 news = gst_event_new_segment (&segment);
471 GST_EVENT_SEQNUM (news) = GST_EVENT_SEQNUM (event);
473 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
475 /* need to track segment for proper running time */
476 gst_event_copy_segment (event, &trans->segment);
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;
484 gst_event_parse_gap (event, &old_start, &dur);
486 start = gst_segment_to_running_time (&trans->segment,
487 GST_FORMAT_TIME, old_start);
489 gst_identity_do_sync (identity,
490 GST_CLOCK_TIME_IS_VALID (start) ? start : 0);
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);
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);
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;
521 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
522 /* eat up segments */
523 gst_event_unref (event);
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);
537 GST_OBJECT_UNLOCK (identity);
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);
549 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
556 gst_identity_src_event (GstBaseTransform * trans, GstEvent * event)
558 GstIdentity *identity = GST_IDENTITY (trans);
560 switch (GST_EVENT_TYPE (event)) {
566 GstSeekType start_type, stop_type;
568 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
569 &start, &stop_type, &stop);
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,
577 GST_OBJECT_UNLOCK (identity);
581 GST_OBJECT_UNLOCK (identity);
589 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
593 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
595 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
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;
605 t_expected = identity->prev_timestamp + identity->prev_duration;
606 dt = GST_CLOCK_DIFF (t_expected, timestamp);
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
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.
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)));
640 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
641 "offset_end was set on previous buffer");
647 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
651 offset = GST_BUFFER_OFFSET (buf);
653 if (identity->prev_offset_end != offset &&
654 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
655 offset != GST_BUFFER_OFFSET_NONE) {
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
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.
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)));
684 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
685 "and/or offset_end were set on previous buffer");
690 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
692 if (ts == GST_CLOCK_TIME_NONE)
695 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
700 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
701 const gchar * action, GstBuffer * buf, gsize size)
703 gchar dts_str[64], pts_str[64], dur_str[64];
704 gchar *flag_str, *meta_str;
706 GST_OBJECT_LOCK (identity);
708 flag_str = gst_buffer_get_flags_string (buf);
709 meta_str = gst_buffer_get_meta_string (buf);
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);
724 GST_TRACE_OBJECT (identity, "%s", identity->last_message);
726 GST_OBJECT_UNLOCK (identity);
728 gst_identity_notify_last_message (identity);
732 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
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;
741 size = gst_buffer_get_size (buf);
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);
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);
754 if (identity->error_after_counter >= 0) {
755 identity->error_after_counter--;
756 if (identity->error_after_counter == 0)
760 if (identity->eos_after_counter >= 0) {
761 identity->eos_after_counter--;
762 if (identity->eos_after_counter == 0)
766 if (identity->drop_probability > 0.0) {
767 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
771 if (GST_BUFFER_FLAG_IS_SET (buf, identity->drop_buffer_flags))
774 if (identity->dump) {
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);
783 if (!identity->silent) {
784 gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
787 if (identity->datarate > 0) {
788 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
789 GST_SECOND, identity->datarate);
791 GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf) = time;
792 GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
795 if (identity->signal_handoffs)
796 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
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));
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);
816 if (GST_CLOCK_TIME_IS_VALID (rundts))
817 runtimestamp = rundts;
818 else if (GST_CLOCK_TIME_IS_VALID (runpts))
819 runtimestamp = runpts;
822 ret = gst_identity_do_sync (identity, runtimestamp);
824 identity->offset += size;
826 if (identity->sleep_time && ret == GST_FLOW_OK)
827 g_usleep (identity->sleep_time);
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;
837 GST_OBJECT_LOCK (trans);
838 identity->num_bytes += gst_buffer_get_size (buf);
839 identity->num_buffers++;
840 GST_OBJECT_UNLOCK (trans);
847 GST_ELEMENT_ERROR (identity, CORE, FAILED,
848 (_("Failed after iterations as requested.")), (NULL));
849 return GST_FLOW_ERROR;
853 GST_DEBUG_OBJECT (identity, "EOS after iterations as requested.");
858 if (!identity->silent) {
859 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
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));
870 /* return DROPPED to basetransform. */
871 return GST_BASE_TRANSFORM_FLOW_DROPPED;
876 gst_identity_set_property (GObject * object, guint prop_id,
877 const GValue * value, GParamSpec * pspec)
879 GstIdentity *identity;
880 GstMessage *clock_message = NULL;
883 identity = GST_IDENTITY (object);
886 case PROP_SLEEP_TIME:
887 identity->sleep_time = g_value_get_uint (value);
890 identity->silent = g_value_get_boolean (value);
892 case PROP_SINGLE_SEGMENT:
893 identity->single_segment = g_value_get_boolean (value);
896 identity->dump = g_value_get_boolean (value);
898 case PROP_ERROR_AFTER:
899 identity->error_after = g_value_get_int (value);
901 case PROP_DROP_PROBABILITY:
902 identity->drop_probability = g_value_get_float (value);
904 case PROP_DROP_BUFFER_FLAGS:
905 identity->drop_buffer_flags = g_value_get_flags (value);
908 identity->datarate = g_value_get_int (value);
911 sync = g_value_get_boolean (value);
912 GST_OBJECT_LOCK (identity);
913 if (sync != identity->sync) {
914 identity->sync = sync;
916 GST_OBJECT_FLAG_SET (identity, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
918 gst_message_new_clock_provide (GST_OBJECT_CAST (identity),
919 gst_system_clock_obtain (), TRUE);
921 GST_OBJECT_FLAG_UNSET (identity, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
923 gst_message_new_clock_lost (GST_OBJECT_CAST (identity),
924 gst_system_clock_obtain ());
927 GST_OBJECT_UNLOCK (identity);
929 gst_element_post_message (GST_ELEMENT_CAST (identity), clock_message);
932 identity->ts_offset = g_value_get_int64 (value);
934 case PROP_CHECK_IMPERFECT_TIMESTAMP:
935 identity->check_imperfect_timestamp = g_value_get_boolean (value);
937 case PROP_CHECK_IMPERFECT_OFFSET:
938 identity->check_imperfect_offset = g_value_get_boolean (value);
940 case PROP_SIGNAL_HANDOFFS:
941 identity->signal_handoffs = g_value_get_boolean (value);
943 case PROP_DROP_ALLOCATION:
944 identity->drop_allocation = g_value_get_boolean (value);
947 identity->eos_after = g_value_get_int (value);
950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
953 if (identity->datarate > 0 || identity->single_segment)
954 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
956 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
959 static GstStructure *
960 gst_identity_create_stats (GstIdentity * identity)
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);
974 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
977 GstIdentity *identity;
979 identity = GST_IDENTITY (object);
982 case PROP_SLEEP_TIME:
983 g_value_set_uint (value, identity->sleep_time);
985 case PROP_ERROR_AFTER:
986 g_value_set_int (value, identity->error_after);
988 case PROP_DROP_PROBABILITY:
989 g_value_set_float (value, identity->drop_probability);
991 case PROP_DROP_BUFFER_FLAGS:
992 g_value_set_flags (value, identity->drop_buffer_flags);
995 g_value_set_int (value, identity->datarate);
998 g_value_set_boolean (value, identity->silent);
1000 case PROP_SINGLE_SEGMENT:
1001 g_value_set_boolean (value, identity->single_segment);
1004 g_value_set_boolean (value, identity->dump);
1006 case PROP_LAST_MESSAGE:
1007 GST_OBJECT_LOCK (identity);
1008 g_value_set_string (value, identity->last_message);
1009 GST_OBJECT_UNLOCK (identity);
1012 g_value_set_boolean (value, identity->sync);
1014 case PROP_TS_OFFSET:
1015 g_value_set_int64 (value, identity->ts_offset);
1017 case PROP_CHECK_IMPERFECT_TIMESTAMP:
1018 g_value_set_boolean (value, identity->check_imperfect_timestamp);
1020 case PROP_CHECK_IMPERFECT_OFFSET:
1021 g_value_set_boolean (value, identity->check_imperfect_offset);
1023 case PROP_SIGNAL_HANDOFFS:
1024 g_value_set_boolean (value, identity->signal_handoffs);
1026 case PROP_DROP_ALLOCATION:
1027 g_value_set_boolean (value, identity->drop_allocation);
1029 case PROP_EOS_AFTER:
1030 g_value_set_int (value, identity->eos_after);
1033 g_value_take_boxed (value, gst_identity_create_stats (identity));
1036 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1042 gst_identity_start (GstBaseTransform * trans)
1044 GstIdentity *identity;
1046 identity = GST_IDENTITY (trans);
1048 if (identity->eos_after != DEFAULT_EOS_AFTER
1049 && identity->error_after != DEFAULT_ERROR_AFTER)
1050 goto both_afters_defined;
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;
1063 both_afters_defined:
1065 GST_ELEMENT_ERROR (identity, CORE, FAILED,
1066 (_("eos-after and error-after can't both be defined.")), (NULL));
1072 gst_identity_stop (GstBaseTransform * trans)
1074 GstIdentity *identity;
1076 identity = GST_IDENTITY (trans);
1078 GST_OBJECT_LOCK (identity);
1079 g_free (identity->last_message);
1080 identity->last_message = NULL;
1081 GST_OBJECT_UNLOCK (identity);
1087 gst_identity_accept_caps (GstBaseTransform * base,
1088 GstPadDirection direction, GstCaps * caps)
1093 /* Proxy accept-caps */
1095 if (direction == GST_PAD_SRC)
1096 pad = GST_BASE_TRANSFORM_SINK_PAD (base);
1098 pad = GST_BASE_TRANSFORM_SRC_PAD (base);
1100 ret = gst_pad_peer_query_accept_caps (pad, caps);
1106 gst_identity_query (GstBaseTransform * base, GstPadDirection direction,
1109 GstIdentity *identity;
1112 identity = GST_IDENTITY (base);
1114 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION &&
1115 identity->drop_allocation) {
1116 GST_DEBUG_OBJECT (identity, "Dropping allocation query.");
1120 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (base, direction, query);
1122 if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
1123 gboolean live = FALSE;
1124 GstClockTime min = 0, max = 0;
1127 gst_query_parse_latency (query, &live, &min, &max);
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)));
1138 /* Ignore the upstream latency if it is not live */
1139 GST_OBJECT_LOCK (identity);
1141 identity->upstream_latency = min;
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 */
1148 GST_OBJECT_UNLOCK (identity);
1150 gst_query_set_latency (query, live || identity->sync, min, max);
1156 static GstStateChangeReturn
1157 gst_identity_change_state (GstElement * element, GstStateChange transition)
1159 GstStateChangeReturn ret;
1160 GstIdentity *identity = GST_IDENTITY (element);
1161 gboolean no_preroll = FALSE;
1163 switch (transition) {
1164 case GST_STATE_CHANGE_NULL_TO_READY:
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);
1173 identity->num_bytes = 0;
1174 identity->num_buffers = 0;
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);
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);
1189 identity->blocked = FALSE;
1190 g_cond_signal (&identity->blocked_cond);
1191 GST_OBJECT_UNLOCK (identity);
1197 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
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);
1208 case GST_STATE_CHANGE_PAUSED_TO_READY:
1210 case GST_STATE_CHANGE_READY_TO_NULL:
1216 if (no_preroll && ret == GST_STATE_CHANGE_SUCCESS)
1217 ret = GST_STATE_CHANGE_NO_PREROLL;
1222 /* FIXME: GStreamer 2.0 */
1224 gst_identity_provide_clock (GstElement * element)
1226 GstIdentity *identity = GST_IDENTITY (element);
1228 if (!identity->sync)
1231 return gst_system_clock_obtain ();