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
265 * @pad: the pad that received it
267 * This signal gets emitted before passing the buffer downstream.
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);
277 * Various statistics. This property returns a GstStructure
278 * with name application/x-identity-stats with the following fields:
284 * <classname>"num-buffers"</classname>:
285 * the number of buffers that passed through.
291 * <classname>"num-bytes"</classname>:
292 * the number of bytes that passed through.
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));
304 gobject_class->finalize = gst_identity_finalize;
306 gst_element_class_set_static_metadata (gstelement_class,
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);
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);
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;
330 gst_identity_init (GstIdentity * identity)
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;
351 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
353 GST_OBJECT_FLAG_SET (identity, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
357 gst_identity_notify_last_message (GstIdentity * identity)
359 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
363 gst_identity_do_sync (GstIdentity * identity, GstClockTime running_time)
365 GstFlowReturn ret = GST_FLOW_OK;
367 if (identity->sync &&
368 GST_BASE_TRANSFORM_CAST (identity)->segment.format == GST_FORMAT_TIME) {
371 GST_OBJECT_LOCK (identity);
373 if (identity->flushing) {
374 GST_OBJECT_UNLOCK (identity);
375 return GST_FLOW_FLUSHING;
378 while (identity->blocked && !identity->flushing)
379 g_cond_wait (&identity->blocked_cond, GST_OBJECT_GET_LOCK (identity));
381 if (identity->flushing) {
382 GST_OBJECT_UNLOCK (identity);
383 return GST_FLOW_FLUSHING;
386 if ((clock = GST_ELEMENT (identity)->clock)) {
388 GstClockTime timestamp;
389 GstClockTimeDiff ts_offset = identity->ts_offset;
391 timestamp = running_time + GST_ELEMENT (identity)->base_time +
392 identity->upstream_latency;
394 ts_offset = -ts_offset;
395 if (ts_offset < timestamp)
396 timestamp -= ts_offset;
400 timestamp += ts_offset;
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);
406 cret = gst_clock_id_wait (identity->clock_id, NULL);
408 GST_OBJECT_LOCK (identity);
409 if (identity->clock_id) {
410 gst_clock_id_unref (identity->clock_id);
411 identity->clock_id = NULL;
413 if (cret == GST_CLOCK_UNSCHEDULED || identity->flushing)
414 ret = GST_FLOW_FLUSHING;
416 GST_OBJECT_UNLOCK (identity);
423 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
425 GstIdentity *identity;
428 identity = GST_IDENTITY (trans);
430 if (!identity->silent) {
431 const GstStructure *s;
435 GST_OBJECT_LOCK (identity);
436 g_free (identity->last_message);
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);
442 sstr = g_strdup ("");
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),
449 GST_OBJECT_UNLOCK (identity);
451 gst_identity_notify_last_message (identity);
454 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
455 if (!trans->have_segment) {
459 gst_event_copy_segment (event, &segment);
460 gst_event_copy_segment (event, &trans->segment);
461 trans->have_segment = TRUE;
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);
471 news = gst_event_new_segment (&segment);
472 GST_EVENT_SEQNUM (news) = GST_EVENT_SEQNUM (event);
474 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
476 /* need to track segment for proper running time */
477 gst_event_copy_segment (event, &trans->segment);
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;
485 gst_event_parse_gap (event, &old_start, &dur);
487 start = gst_segment_to_running_time (&trans->segment,
488 GST_FORMAT_TIME, old_start);
490 gst_identity_do_sync (identity,
491 GST_CLOCK_TIME_IS_VALID (start) ? start : 0);
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);
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);
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;
522 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
523 /* eat up segments */
524 gst_event_unref (event);
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);
538 GST_OBJECT_UNLOCK (identity);
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);
550 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
557 gst_identity_src_event (GstBaseTransform * trans, GstEvent * event)
559 GstIdentity *identity = GST_IDENTITY (trans);
561 switch (GST_EVENT_TYPE (event)) {
567 GstSeekType start_type, stop_type;
569 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
570 &start, &stop_type, &stop);
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,
578 GST_OBJECT_UNLOCK (identity);
582 GST_OBJECT_UNLOCK (identity);
590 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
594 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
596 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
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;
606 t_expected = identity->prev_timestamp + identity->prev_duration;
607 dt = GST_CLOCK_DIFF (t_expected, timestamp);
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
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.
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)));
641 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
642 "offset_end was set on previous buffer");
648 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
652 offset = GST_BUFFER_OFFSET (buf);
654 if (identity->prev_offset_end != offset &&
655 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
656 offset != GST_BUFFER_OFFSET_NONE) {
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
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.
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)));
685 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
686 "and/or offset_end were set on previous buffer");
691 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
693 if (ts == GST_CLOCK_TIME_NONE)
696 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
701 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
702 const gchar * action, GstBuffer * buf, gsize size)
704 gchar dts_str[64], pts_str[64], dur_str[64];
705 gchar *flag_str, *meta_str;
707 GST_OBJECT_LOCK (identity);
709 flag_str = gst_buffer_get_flags_string (buf);
710 meta_str = gst_buffer_get_meta_string (buf);
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);
725 GST_TRACE_OBJECT (identity, "%s", identity->last_message);
727 GST_OBJECT_UNLOCK (identity);
729 gst_identity_notify_last_message (identity);
733 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
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;
742 size = gst_buffer_get_size (buf);
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);
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);
755 if (identity->error_after_counter >= 0) {
756 identity->error_after_counter--;
757 if (identity->error_after_counter == 0)
761 if (identity->eos_after_counter >= 0) {
762 identity->eos_after_counter--;
763 if (identity->eos_after_counter == 0)
767 if (identity->drop_probability > 0.0) {
768 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
772 if (GST_BUFFER_FLAG_IS_SET (buf, identity->drop_buffer_flags))
775 if (identity->dump) {
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);
784 if (!identity->silent) {
785 gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
788 if (identity->datarate > 0) {
789 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
790 GST_SECOND, identity->datarate);
792 GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf) = time;
793 GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
796 if (identity->signal_handoffs)
797 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
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));
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);
817 if (GST_CLOCK_TIME_IS_VALID (rundts))
818 runtimestamp = rundts;
819 else if (GST_CLOCK_TIME_IS_VALID (runpts))
820 runtimestamp = runpts;
823 ret = gst_identity_do_sync (identity, runtimestamp);
825 identity->offset += size;
827 if (identity->sleep_time && ret == GST_FLOW_OK)
828 g_usleep (identity->sleep_time);
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;
838 GST_OBJECT_LOCK (trans);
839 identity->num_bytes += gst_buffer_get_size (buf);
840 identity->num_buffers++;
841 GST_OBJECT_UNLOCK (trans);
848 GST_ELEMENT_ERROR (identity, CORE, FAILED,
849 (_("Failed after iterations as requested.")), (NULL));
850 return GST_FLOW_ERROR;
854 GST_DEBUG_OBJECT (identity, "EOS after iterations as requested.");
859 if (!identity->silent) {
860 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
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));
871 /* return DROPPED to basetransform. */
872 return GST_BASE_TRANSFORM_FLOW_DROPPED;
877 gst_identity_set_property (GObject * object, guint prop_id,
878 const GValue * value, GParamSpec * pspec)
880 GstIdentity *identity;
881 GstMessage *clock_message = NULL;
884 identity = GST_IDENTITY (object);
887 case PROP_SLEEP_TIME:
888 identity->sleep_time = g_value_get_uint (value);
891 identity->silent = g_value_get_boolean (value);
893 case PROP_SINGLE_SEGMENT:
894 identity->single_segment = g_value_get_boolean (value);
897 identity->dump = g_value_get_boolean (value);
899 case PROP_ERROR_AFTER:
900 identity->error_after = g_value_get_int (value);
902 case PROP_DROP_PROBABILITY:
903 identity->drop_probability = g_value_get_float (value);
905 case PROP_DROP_BUFFER_FLAGS:
906 identity->drop_buffer_flags = g_value_get_flags (value);
909 identity->datarate = g_value_get_int (value);
912 sync = g_value_get_boolean (value);
913 GST_OBJECT_LOCK (identity);
914 if (sync != identity->sync) {
915 identity->sync = sync;
917 GST_OBJECT_FLAG_SET (identity, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
919 gst_message_new_clock_provide (GST_OBJECT_CAST (identity),
920 gst_system_clock_obtain (), TRUE);
922 GST_OBJECT_FLAG_UNSET (identity, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
924 gst_message_new_clock_lost (GST_OBJECT_CAST (identity),
925 gst_system_clock_obtain ());
928 GST_OBJECT_UNLOCK (identity);
930 gst_element_post_message (GST_ELEMENT_CAST (identity), clock_message);
933 identity->ts_offset = g_value_get_int64 (value);
935 case PROP_CHECK_IMPERFECT_TIMESTAMP:
936 identity->check_imperfect_timestamp = g_value_get_boolean (value);
938 case PROP_CHECK_IMPERFECT_OFFSET:
939 identity->check_imperfect_offset = g_value_get_boolean (value);
941 case PROP_SIGNAL_HANDOFFS:
942 identity->signal_handoffs = g_value_get_boolean (value);
944 case PROP_DROP_ALLOCATION:
945 identity->drop_allocation = g_value_get_boolean (value);
948 identity->eos_after = g_value_get_int (value);
951 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
954 if (identity->datarate > 0 || identity->single_segment)
955 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
957 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
960 static GstStructure *
961 gst_identity_create_stats (GstIdentity * identity)
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);
975 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
978 GstIdentity *identity;
980 identity = GST_IDENTITY (object);
983 case PROP_SLEEP_TIME:
984 g_value_set_uint (value, identity->sleep_time);
986 case PROP_ERROR_AFTER:
987 g_value_set_int (value, identity->error_after);
989 case PROP_DROP_PROBABILITY:
990 g_value_set_float (value, identity->drop_probability);
992 case PROP_DROP_BUFFER_FLAGS:
993 g_value_set_flags (value, identity->drop_buffer_flags);
996 g_value_set_int (value, identity->datarate);
999 g_value_set_boolean (value, identity->silent);
1001 case PROP_SINGLE_SEGMENT:
1002 g_value_set_boolean (value, identity->single_segment);
1005 g_value_set_boolean (value, identity->dump);
1007 case PROP_LAST_MESSAGE:
1008 GST_OBJECT_LOCK (identity);
1009 g_value_set_string (value, identity->last_message);
1010 GST_OBJECT_UNLOCK (identity);
1013 g_value_set_boolean (value, identity->sync);
1015 case PROP_TS_OFFSET:
1016 g_value_set_int64 (value, identity->ts_offset);
1018 case PROP_CHECK_IMPERFECT_TIMESTAMP:
1019 g_value_set_boolean (value, identity->check_imperfect_timestamp);
1021 case PROP_CHECK_IMPERFECT_OFFSET:
1022 g_value_set_boolean (value, identity->check_imperfect_offset);
1024 case PROP_SIGNAL_HANDOFFS:
1025 g_value_set_boolean (value, identity->signal_handoffs);
1027 case PROP_DROP_ALLOCATION:
1028 g_value_set_boolean (value, identity->drop_allocation);
1030 case PROP_EOS_AFTER:
1031 g_value_set_int (value, identity->eos_after);
1034 g_value_take_boxed (value, gst_identity_create_stats (identity));
1037 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1043 gst_identity_start (GstBaseTransform * trans)
1045 GstIdentity *identity;
1047 identity = GST_IDENTITY (trans);
1049 if (identity->eos_after != DEFAULT_EOS_AFTER
1050 && identity->error_after != DEFAULT_ERROR_AFTER)
1051 goto both_afters_defined;
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;
1064 both_afters_defined:
1066 GST_ELEMENT_ERROR (identity, CORE, FAILED,
1067 (_("eos-after and error-after can't both be defined.")), (NULL));
1073 gst_identity_stop (GstBaseTransform * trans)
1075 GstIdentity *identity;
1077 identity = GST_IDENTITY (trans);
1079 GST_OBJECT_LOCK (identity);
1080 g_free (identity->last_message);
1081 identity->last_message = NULL;
1082 GST_OBJECT_UNLOCK (identity);
1088 gst_identity_accept_caps (GstBaseTransform * base,
1089 GstPadDirection direction, GstCaps * caps)
1094 /* Proxy accept-caps */
1096 if (direction == GST_PAD_SRC)
1097 pad = GST_BASE_TRANSFORM_SINK_PAD (base);
1099 pad = GST_BASE_TRANSFORM_SRC_PAD (base);
1101 ret = gst_pad_peer_query_accept_caps (pad, caps);
1107 gst_identity_query (GstBaseTransform * base, GstPadDirection direction,
1110 GstIdentity *identity;
1113 identity = GST_IDENTITY (base);
1115 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION &&
1116 identity->drop_allocation) {
1117 GST_DEBUG_OBJECT (identity, "Dropping allocation query.");
1121 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (base, direction, query);
1123 if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
1124 gboolean live = FALSE;
1125 GstClockTime min = 0, max = 0;
1128 gst_query_parse_latency (query, &live, &min, &max);
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)));
1139 /* Ignore the upstream latency if it is not live */
1140 GST_OBJECT_LOCK (identity);
1142 identity->upstream_latency = min;
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 */
1149 GST_OBJECT_UNLOCK (identity);
1151 gst_query_set_latency (query, live || identity->sync, min, max);
1157 static GstStateChangeReturn
1158 gst_identity_change_state (GstElement * element, GstStateChange transition)
1160 GstStateChangeReturn ret;
1161 GstIdentity *identity = GST_IDENTITY (element);
1162 gboolean no_preroll = FALSE;
1164 switch (transition) {
1165 case GST_STATE_CHANGE_NULL_TO_READY:
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);
1174 identity->num_bytes = 0;
1175 identity->num_buffers = 0;
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);
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);
1190 identity->blocked = FALSE;
1191 g_cond_signal (&identity->blocked_cond);
1192 GST_OBJECT_UNLOCK (identity);
1198 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
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);
1209 case GST_STATE_CHANGE_PAUSED_TO_READY:
1211 case GST_STATE_CHANGE_READY_TO_NULL:
1217 if (no_preroll && ret == GST_STATE_CHANGE_SUCCESS)
1218 ret = GST_STATE_CHANGE_NO_PREROLL;
1223 /* FIXME: GStreamer 2.0 */
1225 gst_identity_provide_clock (GstElement * element)
1227 GstIdentity *identity = GST_IDENTITY (element);
1229 if (!identity->sync)
1232 return gst_system_clock_obtain ();