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
26 * Dummy element that passes incoming data through unmodified. It has some
27 * useful diagnostic functions, such as offset and timestamp checking.
37 #include "gstelements_private.h"
38 #include "../../gst/gst-i18n-lib.h"
39 #include "gstidentity.h"
41 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
51 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
52 #define GST_CAT_DEFAULT gst_identity_debug
54 /* Identity signals and args */
62 #define DEFAULT_SLEEP_TIME 0
63 #define DEFAULT_DUPLICATE 1
64 #define DEFAULT_ERROR_AFTER -1
65 #define DEFAULT_DROP_PROBABILITY 0.0
66 #define DEFAULT_DROP_BUFFER_FLAGS 0
67 #define DEFAULT_DATARATE 0
68 #define DEFAULT_SILENT TRUE
69 #define DEFAULT_SINGLE_SEGMENT FALSE
70 #define DEFAULT_DUMP FALSE
71 #define DEFAULT_SYNC FALSE
72 #define DEFAULT_CHECK_IMPERFECT_TIMESTAMP FALSE
73 #define DEFAULT_CHECK_IMPERFECT_OFFSET FALSE
74 #define DEFAULT_SIGNAL_HANDOFFS TRUE
75 #define DEFAULT_TS_OFFSET 0
82 PROP_DROP_PROBABILITY,
83 PROP_DROP_BUFFER_FLAGS,
91 PROP_CHECK_IMPERFECT_TIMESTAMP,
92 PROP_CHECK_IMPERFECT_OFFSET,
98 GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
99 #define gst_identity_parent_class parent_class
100 G_DEFINE_TYPE_WITH_CODE (GstIdentity, gst_identity, GST_TYPE_BASE_TRANSFORM,
103 static void gst_identity_finalize (GObject * object);
104 static void gst_identity_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_identity_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
111 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
113 static gboolean gst_identity_start (GstBaseTransform * trans);
114 static gboolean gst_identity_stop (GstBaseTransform * trans);
115 static GstStateChangeReturn gst_identity_change_state (GstElement * element,
116 GstStateChange transition);
117 static gboolean gst_identity_accept_caps (GstBaseTransform * base,
118 GstPadDirection direction, GstCaps * caps);
119 static gboolean gst_identity_query (GstBaseTransform * base,
120 GstPadDirection direction, GstQuery * query);
122 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
124 static GParamSpec *pspec_last_message = NULL;
127 gst_identity_finalize (GObject * object)
129 GstIdentity *identity;
131 identity = GST_IDENTITY (object);
133 g_free (identity->last_message);
134 g_cond_clear (&identity->blocked_cond);
136 G_OBJECT_CLASS (parent_class)->finalize (object);
140 gst_identity_class_init (GstIdentityClass * klass)
142 GObjectClass *gobject_class;
143 GstElementClass *gstelement_class;
144 GstBaseTransformClass *gstbasetrans_class;
146 gobject_class = G_OBJECT_CLASS (klass);
147 gstelement_class = GST_ELEMENT_CLASS (klass);
148 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
150 gobject_class->set_property = gst_identity_set_property;
151 gobject_class->get_property = gst_identity_get_property;
153 g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
154 g_param_spec_uint ("sleep-time", "Sleep time",
155 "Microseconds to sleep between processing", 0, G_MAXUINT,
156 DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
158 g_param_spec_int ("error-after", "Error After", "Error after N buffers",
159 G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
162 g_param_spec_float ("drop-probability", "Drop Probability",
163 "The Probability a buffer is dropped", 0.0, 1.0,
164 DEFAULT_DROP_PROBABILITY,
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 * GstIdentity:drop-buffer-flags:
170 * Drop buffers with the given flags.
174 g_object_class_install_property (gobject_class, PROP_DROP_BUFFER_FLAGS,
175 g_param_spec_flags ("drop-buffer-flags", "Check flags to drop buffers",
176 "Drop buffers with the given flags",
177 GST_TYPE_BUFFER_FLAGS, DEFAULT_DROP_BUFFER_FLAGS,
178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (gobject_class, PROP_DATARATE,
180 g_param_spec_int ("datarate", "Datarate",
181 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
182 0, G_MAXINT, DEFAULT_DATARATE,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class, PROP_SILENT,
185 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
188 g_param_spec_boolean ("single-segment", "Single Segment",
189 "Timestamp buffers and eat segments so as to appear as one segment",
190 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 pspec_last_message = g_param_spec_string ("last-message", "last-message",
192 "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
193 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
195 g_object_class_install_property (gobject_class, PROP_DUMP,
196 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
197 DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_SYNC,
199 g_param_spec_boolean ("sync", "Synchronize",
200 "Synchronize to pipeline clock", DEFAULT_SYNC,
201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
203 g_param_spec_int64 ("ts-offset", "Timestamp offset for synchronisation",
204 "Timestamp offset in nanoseconds for synchronisation, negative for earlier sync",
205 G_MININT64, G_MAXINT64, DEFAULT_TS_OFFSET,
206 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (gobject_class,
208 PROP_CHECK_IMPERFECT_TIMESTAMP,
209 g_param_spec_boolean ("check-imperfect-timestamp",
210 "Check for discontiguous timestamps",
211 "Send element messages if timestamps and durations do not match up",
212 DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
215 g_param_spec_boolean ("check-imperfect-offset",
216 "Check for discontiguous offset",
217 "Send element messages if offset and offset_end do not match up",
218 DEFAULT_CHECK_IMPERFECT_OFFSET,
219 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 * GstIdentity:signal-handoffs
224 * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
225 * When set to #FALSE, no signal will be emitted, which might improve performance.
227 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
228 g_param_spec_boolean ("signal-handoffs",
229 "Signal handoffs", "Send a signal before pushing the buffer",
230 DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 * GstIdentity::handoff:
234 * @identity: the identity instance
235 * @buffer: the buffer that just has been received
236 * @pad: the pad that received it
238 * This signal gets emitted before passing the buffer downstream.
240 gst_identity_signals[SIGNAL_HANDOFF] =
241 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
242 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
243 g_cclosure_marshal_generic, G_TYPE_NONE, 1,
244 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
246 gobject_class->finalize = gst_identity_finalize;
248 gst_element_class_set_static_metadata (gstelement_class,
251 "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
252 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
253 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
255 gstelement_class->change_state =
256 GST_DEBUG_FUNCPTR (gst_identity_change_state);
258 gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
259 gstbasetrans_class->transform_ip =
260 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
261 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
262 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
263 gstbasetrans_class->accept_caps =
264 GST_DEBUG_FUNCPTR (gst_identity_accept_caps);
265 gstbasetrans_class->query = gst_identity_query;
269 gst_identity_init (GstIdentity * identity)
271 identity->sleep_time = DEFAULT_SLEEP_TIME;
272 identity->error_after = DEFAULT_ERROR_AFTER;
273 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
274 identity->drop_buffer_flags = DEFAULT_DROP_BUFFER_FLAGS;
275 identity->datarate = DEFAULT_DATARATE;
276 identity->silent = DEFAULT_SILENT;
277 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
278 identity->sync = DEFAULT_SYNC;
279 identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
280 identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
281 identity->dump = DEFAULT_DUMP;
282 identity->last_message = NULL;
283 identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
284 identity->ts_offset = DEFAULT_TS_OFFSET;
285 g_cond_init (&identity->blocked_cond);
287 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
291 gst_identity_notify_last_message (GstIdentity * identity)
293 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
297 gst_identity_do_sync (GstIdentity * identity, GstClockTime running_time)
299 GstFlowReturn ret = GST_FLOW_OK;
301 if (identity->sync &&
302 GST_BASE_TRANSFORM_CAST (identity)->segment.format == GST_FORMAT_TIME) {
305 GST_OBJECT_LOCK (identity);
307 while (identity->blocked)
308 g_cond_wait (&identity->blocked_cond, GST_OBJECT_GET_LOCK (identity));
311 if ((clock = GST_ELEMENT (identity)->clock)) {
313 GstClockTime timestamp;
314 GstClockTimeDiff ts_offset = identity->ts_offset;
316 timestamp = running_time + GST_ELEMENT (identity)->base_time +
317 identity->upstream_latency;
319 ts_offset = -ts_offset;
320 if (ts_offset < timestamp)
321 timestamp -= ts_offset;
325 timestamp += ts_offset;
327 /* save id if we need to unlock */
328 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
329 GST_OBJECT_UNLOCK (identity);
331 cret = gst_clock_id_wait (identity->clock_id, NULL);
333 GST_OBJECT_LOCK (identity);
334 if (identity->clock_id) {
335 gst_clock_id_unref (identity->clock_id);
336 identity->clock_id = NULL;
338 if (cret == GST_CLOCK_UNSCHEDULED)
341 GST_OBJECT_UNLOCK (identity);
348 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
350 GstIdentity *identity;
353 identity = GST_IDENTITY (trans);
355 if (!identity->silent) {
356 const GstStructure *s;
360 GST_OBJECT_LOCK (identity);
361 g_free (identity->last_message);
363 tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
364 if ((s = gst_event_get_structure (event)))
365 sstr = gst_structure_to_string (s);
367 sstr = g_strdup ("");
369 identity->last_message =
370 g_strdup_printf ("event ******* (%s:%s) E (type: %s (%d), %s) %p",
371 GST_DEBUG_PAD_NAME (trans->sinkpad), tstr, GST_EVENT_TYPE (event),
374 GST_OBJECT_UNLOCK (identity);
376 gst_identity_notify_last_message (identity);
379 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
380 if (!trans->have_segment) {
384 gst_event_copy_segment (event, &segment);
385 gst_event_copy_segment (event, &trans->segment);
386 trans->have_segment = TRUE;
388 /* This is the first segment, send out a (0, -1) segment */
389 gst_segment_init (&segment, segment.format);
390 news = gst_event_new_segment (&segment);
392 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
394 /* need to track segment for proper running time */
395 gst_event_copy_segment (event, &trans->segment);
399 if (GST_EVENT_TYPE (event) == GST_EVENT_GAP &&
400 trans->have_segment && trans->segment.format == GST_FORMAT_TIME) {
401 GstClockTime start, dur;
403 gst_event_parse_gap (event, &start, &dur);
404 if (GST_CLOCK_TIME_IS_VALID (start)) {
405 start = gst_segment_to_running_time (&trans->segment,
406 GST_FORMAT_TIME, start);
408 gst_identity_do_sync (identity, start);
410 /* also transform GAP timestamp similar to buffer timestamps */
411 if (identity->single_segment) {
412 gst_event_unref (event);
413 event = gst_event_new_gap (start, dur);
418 /* Reset previous timestamp, duration and offsets on SEGMENT
419 * to prevent false warnings when checking for perfect streams */
420 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
421 identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
422 identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
425 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
426 /* eat up segments */
427 gst_event_unref (event);
430 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
431 GST_OBJECT_LOCK (identity);
432 if (identity->clock_id) {
433 GST_DEBUG_OBJECT (identity, "unlock clock wait");
434 gst_clock_id_unschedule (identity->clock_id);
436 GST_OBJECT_UNLOCK (identity);
439 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
446 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
448 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
450 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
451 if (timestamp != GST_CLOCK_TIME_NONE) {
452 /* check if we had a previous buffer to compare to */
453 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
454 identity->prev_duration != GST_CLOCK_TIME_NONE) {
455 GstClockTime t_expected;
458 t_expected = identity->prev_timestamp + identity->prev_duration;
459 dt = GST_CLOCK_DIFF (t_expected, timestamp);
462 * "imperfect-timestamp" bus message:
463 * @identity: the identity instance
464 * @delta: the GST_CLOCK_DIFF to the prev timestamp
465 * @prev-timestamp: the previous buffer timestamp
466 * @prev-duration: the previous buffer duration
467 * @prev-offset: the previous buffer offset
468 * @prev-offset-end: the previous buffer offset end
469 * @cur-timestamp: the current buffer timestamp
470 * @cur-duration: the current buffer duration
471 * @cur-offset: the current buffer offset
472 * @cur-offset-end: the current buffer offset end
474 * This bus message gets emitted if the check-imperfect-timestamp
475 * property is set and there is a gap in time between the
476 * last buffer and the newly received buffer.
478 gst_element_post_message (GST_ELEMENT (identity),
479 gst_message_new_element (GST_OBJECT (identity),
480 gst_structure_new ("imperfect-timestamp",
481 "delta", G_TYPE_INT64, dt,
482 "prev-timestamp", G_TYPE_UINT64,
483 identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
484 identity->prev_duration, "prev-offset", G_TYPE_UINT64,
485 identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
486 identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
487 timestamp, "cur-duration", G_TYPE_UINT64,
488 GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
489 GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
490 GST_BUFFER_OFFSET_END (buf), NULL)));
493 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
494 "offset_end was set on previous buffer");
500 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
504 offset = GST_BUFFER_OFFSET (buf);
506 if (identity->prev_offset_end != offset &&
507 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
508 offset != GST_BUFFER_OFFSET_NONE) {
510 * "imperfect-offset" bus message:
511 * @identity: the identity instance
512 * @prev-timestamp: the previous buffer timestamp
513 * @prev-duration: the previous buffer duration
514 * @prev-offset: the previous buffer offset
515 * @prev-offset-end: the previous buffer offset end
516 * @cur-timestamp: the current buffer timestamp
517 * @cur-duration: the current buffer duration
518 * @cur-offset: the current buffer offset
519 * @cur-offset-end: the current buffer offset end
521 * This bus message gets emitted if the check-imperfect-offset
522 * property is set and there is a gap in offsets between the
523 * last buffer and the newly received buffer.
525 gst_element_post_message (GST_ELEMENT (identity),
526 gst_message_new_element (GST_OBJECT (identity),
527 gst_structure_new ("imperfect-offset", "prev-timestamp",
528 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
529 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
530 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
531 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
532 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
533 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
534 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
535 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
537 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
538 "and/or offset_end were set on previous buffer");
543 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
545 if (ts == GST_CLOCK_TIME_NONE)
548 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
553 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
554 const gchar * action, GstBuffer * buf, gsize size)
556 gchar dts_str[64], pts_str[64], dur_str[64];
557 gchar *flag_str, *meta_str;
559 GST_OBJECT_LOCK (identity);
561 flag_str = gst_buffer_get_flags_string (buf);
562 meta_str = gst_buffer_get_meta_string (buf);
564 g_free (identity->last_message);
565 identity->last_message = g_strdup_printf ("%s ******* (%s:%s) "
566 "(%" G_GSIZE_FORMAT " bytes, dts: %s, pts: %s, duration: %s, offset: %"
567 G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
568 ", flags: %08x %s, meta: %s) %p", action,
569 GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
570 print_pretty_time (dts_str, sizeof (dts_str), GST_BUFFER_DTS (buf)),
571 print_pretty_time (pts_str, sizeof (pts_str), GST_BUFFER_PTS (buf)),
572 print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
573 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
574 GST_BUFFER_FLAGS (buf), flag_str, meta_str ? meta_str : "none", buf);
577 GST_OBJECT_UNLOCK (identity);
579 gst_identity_notify_last_message (identity);
583 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
585 GstFlowReturn ret = GST_FLOW_OK;
586 GstIdentity *identity = GST_IDENTITY (trans);
587 GstClockTime rundts = GST_CLOCK_TIME_NONE;
588 GstClockTime runpts = GST_CLOCK_TIME_NONE;
589 GstClockTime ts, duration, runtimestamp;
592 size = gst_buffer_get_size (buf);
594 if (identity->check_imperfect_timestamp)
595 gst_identity_check_imperfect_timestamp (identity, buf);
596 if (identity->check_imperfect_offset)
597 gst_identity_check_imperfect_offset (identity, buf);
599 /* update prev values */
600 identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
601 identity->prev_duration = GST_BUFFER_DURATION (buf);
602 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
603 identity->prev_offset = GST_BUFFER_OFFSET (buf);
605 if (identity->error_after >= 0) {
606 identity->error_after--;
607 if (identity->error_after == 0)
611 if (identity->drop_probability > 0.0) {
612 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
616 if (GST_BUFFER_FLAG_IS_SET (buf, identity->drop_buffer_flags))
619 if (identity->dump) {
622 if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
623 gst_util_dump_mem (info.data, info.size);
624 gst_buffer_unmap (buf, &info);
628 if (!identity->silent) {
629 gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
632 if (identity->datarate > 0) {
633 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
634 GST_SECOND, identity->datarate);
636 GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf) = time;
637 GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
640 if (identity->signal_handoffs)
641 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
643 if (trans->segment.format == GST_FORMAT_TIME) {
644 rundts = gst_segment_to_running_time (&trans->segment,
645 GST_FORMAT_TIME, GST_BUFFER_DTS (buf));
646 runpts = gst_segment_to_running_time (&trans->segment,
647 GST_FORMAT_TIME, GST_BUFFER_PTS (buf));
650 if (GST_CLOCK_TIME_IS_VALID (rundts))
651 runtimestamp = rundts;
652 else if (GST_CLOCK_TIME_IS_VALID (runpts))
653 runtimestamp = runpts;
656 ret = gst_identity_do_sync (identity, runtimestamp);
658 identity->offset += size;
660 if (identity->sleep_time && ret == GST_FLOW_OK)
661 g_usleep (identity->sleep_time);
663 if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
664 && (ret == GST_FLOW_OK)) {
665 GST_BUFFER_DTS (buf) = rundts;
666 GST_BUFFER_PTS (buf) = runpts;
667 GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
668 GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
676 GST_ELEMENT_ERROR (identity, CORE, FAILED,
677 (_("Failed after iterations as requested.")), (NULL));
678 return GST_FLOW_ERROR;
682 if (!identity->silent) {
683 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
687 ts = GST_BUFFER_TIMESTAMP (buf);
688 if (GST_CLOCK_TIME_IS_VALID (ts)) {
689 duration = GST_BUFFER_DURATION (buf);
690 gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (identity),
691 gst_event_new_gap (ts, duration));
694 /* return DROPPED to basetransform. */
695 return GST_BASE_TRANSFORM_FLOW_DROPPED;
700 gst_identity_set_property (GObject * object, guint prop_id,
701 const GValue * value, GParamSpec * pspec)
703 GstIdentity *identity;
705 identity = GST_IDENTITY (object);
708 case PROP_SLEEP_TIME:
709 identity->sleep_time = g_value_get_uint (value);
712 identity->silent = g_value_get_boolean (value);
714 case PROP_SINGLE_SEGMENT:
715 identity->single_segment = g_value_get_boolean (value);
718 identity->dump = g_value_get_boolean (value);
720 case PROP_ERROR_AFTER:
721 identity->error_after = g_value_get_int (value);
723 case PROP_DROP_PROBABILITY:
724 identity->drop_probability = g_value_get_float (value);
726 case PROP_DROP_BUFFER_FLAGS:
727 identity->drop_buffer_flags = g_value_get_flags (value);
730 identity->datarate = g_value_get_int (value);
733 identity->sync = g_value_get_boolean (value);
736 identity->ts_offset = g_value_get_int64 (value);
738 case PROP_CHECK_IMPERFECT_TIMESTAMP:
739 identity->check_imperfect_timestamp = g_value_get_boolean (value);
741 case PROP_CHECK_IMPERFECT_OFFSET:
742 identity->check_imperfect_offset = g_value_get_boolean (value);
744 case PROP_SIGNAL_HANDOFFS:
745 identity->signal_handoffs = g_value_get_boolean (value);
748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
751 if (identity->datarate > 0 || identity->single_segment)
752 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
754 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
758 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
761 GstIdentity *identity;
763 identity = GST_IDENTITY (object);
766 case PROP_SLEEP_TIME:
767 g_value_set_uint (value, identity->sleep_time);
769 case PROP_ERROR_AFTER:
770 g_value_set_int (value, identity->error_after);
772 case PROP_DROP_PROBABILITY:
773 g_value_set_float (value, identity->drop_probability);
775 case PROP_DROP_BUFFER_FLAGS:
776 g_value_set_flags (value, identity->drop_buffer_flags);
779 g_value_set_int (value, identity->datarate);
782 g_value_set_boolean (value, identity->silent);
784 case PROP_SINGLE_SEGMENT:
785 g_value_set_boolean (value, identity->single_segment);
788 g_value_set_boolean (value, identity->dump);
790 case PROP_LAST_MESSAGE:
791 GST_OBJECT_LOCK (identity);
792 g_value_set_string (value, identity->last_message);
793 GST_OBJECT_UNLOCK (identity);
796 g_value_set_boolean (value, identity->sync);
799 identity->ts_offset = g_value_get_int64 (value);
801 case PROP_CHECK_IMPERFECT_TIMESTAMP:
802 g_value_set_boolean (value, identity->check_imperfect_timestamp);
804 case PROP_CHECK_IMPERFECT_OFFSET:
805 g_value_set_boolean (value, identity->check_imperfect_offset);
807 case PROP_SIGNAL_HANDOFFS:
808 g_value_set_boolean (value, identity->signal_handoffs);
811 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
817 gst_identity_start (GstBaseTransform * trans)
819 GstIdentity *identity;
821 identity = GST_IDENTITY (trans);
823 identity->offset = 0;
824 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
825 identity->prev_duration = GST_CLOCK_TIME_NONE;
826 identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
827 identity->prev_offset = GST_BUFFER_OFFSET_NONE;
833 gst_identity_stop (GstBaseTransform * trans)
835 GstIdentity *identity;
837 identity = GST_IDENTITY (trans);
839 GST_OBJECT_LOCK (identity);
840 g_free (identity->last_message);
841 identity->last_message = NULL;
842 GST_OBJECT_UNLOCK (identity);
848 gst_identity_accept_caps (GstBaseTransform * base,
849 GstPadDirection direction, GstCaps * caps)
854 /* Proxy accept-caps */
856 if (direction == GST_PAD_SRC)
857 pad = GST_BASE_TRANSFORM_SINK_PAD (base);
859 pad = GST_BASE_TRANSFORM_SRC_PAD (base);
861 ret = gst_pad_peer_query_accept_caps (pad, caps);
867 gst_identity_query (GstBaseTransform * base, GstPadDirection direction,
870 GstIdentity *identity;
873 identity = GST_IDENTITY (base);
875 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (base, direction, query);
877 if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
878 gboolean live = FALSE;
879 GstClockTime min = 0, max = 0;
882 gst_query_parse_latency (query, &live, &min, &max);
884 if (identity->sync && max < min) {
885 GST_ELEMENT_WARNING (base, CORE, CLOCK, (NULL),
886 ("Impossible to configure latency before identity sync=true:"
887 " max %" GST_TIME_FORMAT " < min %"
888 GST_TIME_FORMAT ". Add queues or other buffering elements.",
889 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
893 /* Ignore the upstream latency if it is not live */
894 GST_OBJECT_LOCK (identity);
896 identity->upstream_latency = min;
898 identity->upstream_latency = 0;
899 GST_OBJECT_UNLOCK (identity);
901 gst_query_set_latency (query, live || identity->sync, min, max);
907 static GstStateChangeReturn
908 gst_identity_change_state (GstElement * element, GstStateChange transition)
910 GstStateChangeReturn ret;
911 GstIdentity *identity = GST_IDENTITY (element);
912 gboolean no_preroll = FALSE;
914 switch (transition) {
915 case GST_STATE_CHANGE_NULL_TO_READY:
917 case GST_STATE_CHANGE_READY_TO_PAUSED:
918 GST_OBJECT_LOCK (identity);
919 identity->blocked = TRUE;
920 GST_OBJECT_UNLOCK (identity);
924 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
925 GST_OBJECT_LOCK (identity);
926 identity->blocked = FALSE;
927 g_cond_broadcast (&identity->blocked_cond);
928 GST_OBJECT_UNLOCK (identity);
930 case GST_STATE_CHANGE_PAUSED_TO_READY:
931 GST_OBJECT_LOCK (identity);
932 if (identity->clock_id) {
933 GST_DEBUG_OBJECT (identity, "unlock clock wait");
934 gst_clock_id_unschedule (identity->clock_id);
936 identity->blocked = FALSE;
937 g_cond_broadcast (&identity->blocked_cond);
938 GST_OBJECT_UNLOCK (identity);
944 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
946 switch (transition) {
947 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
948 GST_OBJECT_LOCK (identity);
949 identity->upstream_latency = 0;
950 identity->blocked = TRUE;
951 GST_OBJECT_UNLOCK (identity);
955 case GST_STATE_CHANGE_PAUSED_TO_READY:
957 case GST_STATE_CHANGE_READY_TO_NULL:
963 if (no_preroll && ret == GST_STATE_CHANGE_SUCCESS)
964 ret = GST_STATE_CHANGE_NO_PREROLL;