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
81 PROP_DROP_PROBABILITY,
82 PROP_DROP_BUFFER_FLAGS,
89 PROP_CHECK_IMPERFECT_TIMESTAMP,
90 PROP_CHECK_IMPERFECT_OFFSET,
96 GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
97 #define gst_identity_parent_class parent_class
98 G_DEFINE_TYPE_WITH_CODE (GstIdentity, gst_identity, GST_TYPE_BASE_TRANSFORM,
101 static void gst_identity_finalize (GObject * object);
102 static void gst_identity_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_identity_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
109 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
111 static gboolean gst_identity_start (GstBaseTransform * trans);
112 static gboolean gst_identity_stop (GstBaseTransform * trans);
113 static GstStateChangeReturn gst_identity_change_state (GstElement * element,
114 GstStateChange transition);
115 static gboolean gst_identity_accept_caps (GstBaseTransform * base,
116 GstPadDirection direction, GstCaps * caps);
117 static gboolean gst_identity_query (GstBaseTransform * base,
118 GstPadDirection direction, GstQuery * query);
120 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
122 static GParamSpec *pspec_last_message = NULL;
125 gst_identity_finalize (GObject * object)
127 GstIdentity *identity;
129 identity = GST_IDENTITY (object);
131 g_free (identity->last_message);
132 g_cond_clear (&identity->blocked_cond);
134 G_OBJECT_CLASS (parent_class)->finalize (object);
138 gst_identity_class_init (GstIdentityClass * klass)
140 GObjectClass *gobject_class;
141 GstElementClass *gstelement_class;
142 GstBaseTransformClass *gstbasetrans_class;
144 gobject_class = G_OBJECT_CLASS (klass);
145 gstelement_class = GST_ELEMENT_CLASS (klass);
146 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
148 gobject_class->set_property = gst_identity_set_property;
149 gobject_class->get_property = gst_identity_get_property;
151 g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
152 g_param_spec_uint ("sleep-time", "Sleep time",
153 "Microseconds to sleep between processing", 0, G_MAXUINT,
154 DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
156 g_param_spec_int ("error-after", "Error After", "Error after N buffers",
157 G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
160 g_param_spec_float ("drop-probability", "Drop Probability",
161 "The Probability a buffer is dropped", 0.0, 1.0,
162 DEFAULT_DROP_PROBABILITY,
163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 * GstIdentity:drop-buffer-flags:
168 * Drop buffers with the given flags.
172 g_object_class_install_property (gobject_class, PROP_DROP_BUFFER_FLAGS,
173 g_param_spec_flags ("drop-buffer-flags", "Check flags to drop buffers",
174 "Drop buffers with the given flags",
175 GST_TYPE_BUFFER_FLAGS, DEFAULT_DROP_BUFFER_FLAGS,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_DATARATE,
178 g_param_spec_int ("datarate", "Datarate",
179 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
180 0, G_MAXINT, DEFAULT_DATARATE,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_class, PROP_SILENT,
183 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
186 g_param_spec_boolean ("single-segment", "Single Segment",
187 "Timestamp buffers and eat segments so as to appear as one segment",
188 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 pspec_last_message = g_param_spec_string ("last-message", "last-message",
190 "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
191 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
193 g_object_class_install_property (gobject_class, PROP_DUMP,
194 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
195 DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_class, PROP_SYNC,
197 g_param_spec_boolean ("sync", "Synchronize",
198 "Synchronize to pipeline clock", DEFAULT_SYNC,
199 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class,
201 PROP_CHECK_IMPERFECT_TIMESTAMP,
202 g_param_spec_boolean ("check-imperfect-timestamp",
203 "Check for discontiguous timestamps",
204 "Send element messages if timestamps and durations do not match up",
205 DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
206 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
208 g_param_spec_boolean ("check-imperfect-offset",
209 "Check for discontiguous offset",
210 "Send element messages if offset and offset_end do not match up",
211 DEFAULT_CHECK_IMPERFECT_OFFSET,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 * GstIdentity:signal-handoffs
217 * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
218 * When set to #FALSE, no signal will be emitted, which might improve performance.
220 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
221 g_param_spec_boolean ("signal-handoffs",
222 "Signal handoffs", "Send a signal before pushing the buffer",
223 DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 * GstIdentity::handoff:
227 * @identity: the identity instance
228 * @buffer: the buffer that just has been received
229 * @pad: the pad that received it
231 * This signal gets emitted before passing the buffer downstream.
233 gst_identity_signals[SIGNAL_HANDOFF] =
234 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
235 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
236 g_cclosure_marshal_generic, G_TYPE_NONE, 1,
237 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
239 gobject_class->finalize = gst_identity_finalize;
241 gst_element_class_set_static_metadata (gstelement_class,
244 "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
245 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
246 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
248 gstelement_class->change_state =
249 GST_DEBUG_FUNCPTR (gst_identity_change_state);
251 gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
252 gstbasetrans_class->transform_ip =
253 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
254 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
255 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
256 gstbasetrans_class->accept_caps =
257 GST_DEBUG_FUNCPTR (gst_identity_accept_caps);
258 gstbasetrans_class->query = gst_identity_query;
262 gst_identity_init (GstIdentity * identity)
264 identity->sleep_time = DEFAULT_SLEEP_TIME;
265 identity->error_after = DEFAULT_ERROR_AFTER;
266 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
267 identity->drop_buffer_flags = DEFAULT_DROP_BUFFER_FLAGS;
268 identity->datarate = DEFAULT_DATARATE;
269 identity->silent = DEFAULT_SILENT;
270 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
271 identity->sync = DEFAULT_SYNC;
272 identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
273 identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
274 identity->dump = DEFAULT_DUMP;
275 identity->last_message = NULL;
276 identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
277 g_cond_init (&identity->blocked_cond);
279 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
283 gst_identity_notify_last_message (GstIdentity * identity)
285 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
289 gst_identity_do_sync (GstIdentity * identity, GstClockTime running_time)
291 GstFlowReturn ret = GST_FLOW_OK;
293 if (identity->sync &&
294 GST_BASE_TRANSFORM_CAST (identity)->segment.format == GST_FORMAT_TIME) {
297 GST_OBJECT_LOCK (identity);
299 while (identity->blocked)
300 g_cond_wait (&identity->blocked_cond, GST_OBJECT_GET_LOCK (identity));
303 if ((clock = GST_ELEMENT (identity)->clock)) {
305 GstClockTime timestamp;
307 timestamp = running_time + GST_ELEMENT (identity)->base_time +
308 identity->upstream_latency;
310 /* save id if we need to unlock */
311 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
312 GST_OBJECT_UNLOCK (identity);
314 cret = gst_clock_id_wait (identity->clock_id, NULL);
316 GST_OBJECT_LOCK (identity);
317 if (identity->clock_id) {
318 gst_clock_id_unref (identity->clock_id);
319 identity->clock_id = NULL;
321 if (cret == GST_CLOCK_UNSCHEDULED)
324 GST_OBJECT_UNLOCK (identity);
331 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
333 GstIdentity *identity;
336 identity = GST_IDENTITY (trans);
338 if (!identity->silent) {
339 const GstStructure *s;
343 GST_OBJECT_LOCK (identity);
344 g_free (identity->last_message);
346 tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
347 if ((s = gst_event_get_structure (event)))
348 sstr = gst_structure_to_string (s);
350 sstr = g_strdup ("");
352 identity->last_message =
353 g_strdup_printf ("event ******* (%s:%s) E (type: %s (%d), %s) %p",
354 GST_DEBUG_PAD_NAME (trans->sinkpad), tstr, GST_EVENT_TYPE (event),
357 GST_OBJECT_UNLOCK (identity);
359 gst_identity_notify_last_message (identity);
362 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
363 if (!trans->have_segment) {
367 gst_event_copy_segment (event, &segment);
368 gst_event_copy_segment (event, &trans->segment);
369 trans->have_segment = TRUE;
371 /* This is the first segment, send out a (0, -1) segment */
372 gst_segment_init (&segment, segment.format);
373 news = gst_event_new_segment (&segment);
375 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
377 /* need to track segment for proper running time */
378 gst_event_copy_segment (event, &trans->segment);
382 if (GST_EVENT_TYPE (event) == GST_EVENT_GAP &&
383 trans->have_segment && trans->segment.format == GST_FORMAT_TIME) {
384 GstClockTime start, dur;
386 gst_event_parse_gap (event, &start, &dur);
387 if (GST_CLOCK_TIME_IS_VALID (start)) {
388 start = gst_segment_to_running_time (&trans->segment,
389 GST_FORMAT_TIME, start);
391 gst_identity_do_sync (identity, start);
393 /* also transform GAP timestamp similar to buffer timestamps */
394 if (identity->single_segment) {
395 gst_event_unref (event);
396 event = gst_event_new_gap (start, dur);
401 /* Reset previous timestamp, duration and offsets on SEGMENT
402 * to prevent false warnings when checking for perfect streams */
403 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
404 identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
405 identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
408 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
409 /* eat up segments */
410 gst_event_unref (event);
413 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
414 GST_OBJECT_LOCK (identity);
415 if (identity->clock_id) {
416 GST_DEBUG_OBJECT (identity, "unlock clock wait");
417 gst_clock_id_unschedule (identity->clock_id);
419 GST_OBJECT_UNLOCK (identity);
422 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
429 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
431 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
433 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
434 if (timestamp != GST_CLOCK_TIME_NONE) {
435 /* check if we had a previous buffer to compare to */
436 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
437 identity->prev_duration != GST_CLOCK_TIME_NONE) {
438 GstClockTime t_expected;
441 t_expected = identity->prev_timestamp + identity->prev_duration;
442 dt = GST_CLOCK_DIFF (t_expected, timestamp);
445 * "imperfect-timestamp" bus message:
446 * @identity: the identity instance
447 * @delta: the GST_CLOCK_DIFF to the prev timestamp
448 * @prev-timestamp: the previous buffer timestamp
449 * @prev-duration: the previous buffer duration
450 * @prev-offset: the previous buffer offset
451 * @prev-offset-end: the previous buffer offset end
452 * @cur-timestamp: the current buffer timestamp
453 * @cur-duration: the current buffer duration
454 * @cur-offset: the current buffer offset
455 * @cur-offset-end: the current buffer offset end
457 * This bus message gets emitted if the check-imperfect-timestamp
458 * property is set and there is a gap in time between the
459 * last buffer and the newly received buffer.
461 gst_element_post_message (GST_ELEMENT (identity),
462 gst_message_new_element (GST_OBJECT (identity),
463 gst_structure_new ("imperfect-timestamp",
464 "delta", G_TYPE_INT64, dt,
465 "prev-timestamp", G_TYPE_UINT64,
466 identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
467 identity->prev_duration, "prev-offset", G_TYPE_UINT64,
468 identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
469 identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
470 timestamp, "cur-duration", G_TYPE_UINT64,
471 GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
472 GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
473 GST_BUFFER_OFFSET_END (buf), NULL)));
476 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
477 "offset_end was set on previous buffer");
483 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
487 offset = GST_BUFFER_OFFSET (buf);
489 if (identity->prev_offset_end != offset &&
490 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
491 offset != GST_BUFFER_OFFSET_NONE) {
493 * "imperfect-offset" bus message:
494 * @identity: the identity instance
495 * @prev-timestamp: the previous buffer timestamp
496 * @prev-duration: the previous buffer duration
497 * @prev-offset: the previous buffer offset
498 * @prev-offset-end: the previous buffer offset end
499 * @cur-timestamp: the current buffer timestamp
500 * @cur-duration: the current buffer duration
501 * @cur-offset: the current buffer offset
502 * @cur-offset-end: the current buffer offset end
504 * This bus message gets emitted if the check-imperfect-offset
505 * property is set and there is a gap in offsets between the
506 * last buffer and the newly received buffer.
508 gst_element_post_message (GST_ELEMENT (identity),
509 gst_message_new_element (GST_OBJECT (identity),
510 gst_structure_new ("imperfect-offset", "prev-timestamp",
511 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
512 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
513 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
514 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
515 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
516 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
517 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
518 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
520 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
521 "and/or offset_end were set on previous buffer");
526 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
528 if (ts == GST_CLOCK_TIME_NONE)
531 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
536 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
537 const gchar * action, GstBuffer * buf, gsize size)
539 gchar dts_str[64], pts_str[64], dur_str[64];
542 GST_OBJECT_LOCK (identity);
544 flag_str = gst_buffer_get_flags_string (buf);
546 g_free (identity->last_message);
547 identity->last_message = g_strdup_printf ("%s ******* (%s:%s) "
548 "(%" G_GSIZE_FORMAT " bytes, dts: %s, pts: %s, duration: %s, offset: %"
549 G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
550 ", flags: %08x %s) %p", action,
551 GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
552 print_pretty_time (dts_str, sizeof (dts_str), GST_BUFFER_DTS (buf)),
553 print_pretty_time (pts_str, sizeof (pts_str), GST_BUFFER_PTS (buf)),
554 print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
555 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
556 GST_BUFFER_FLAGS (buf), flag_str, buf);
559 GST_OBJECT_UNLOCK (identity);
561 gst_identity_notify_last_message (identity);
565 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
567 GstFlowReturn ret = GST_FLOW_OK;
568 GstIdentity *identity = GST_IDENTITY (trans);
569 GstClockTime rundts = GST_CLOCK_TIME_NONE;
570 GstClockTime runpts = GST_CLOCK_TIME_NONE;
571 GstClockTime ts, duration, runtimestamp;
574 size = gst_buffer_get_size (buf);
576 if (identity->check_imperfect_timestamp)
577 gst_identity_check_imperfect_timestamp (identity, buf);
578 if (identity->check_imperfect_offset)
579 gst_identity_check_imperfect_offset (identity, buf);
581 /* update prev values */
582 identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
583 identity->prev_duration = GST_BUFFER_DURATION (buf);
584 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
585 identity->prev_offset = GST_BUFFER_OFFSET (buf);
587 if (identity->error_after >= 0) {
588 identity->error_after--;
589 if (identity->error_after == 0)
593 if (identity->drop_probability > 0.0) {
594 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
598 if (GST_BUFFER_FLAG_IS_SET (buf, identity->drop_buffer_flags))
601 if (identity->dump) {
604 if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
605 gst_util_dump_mem (info.data, info.size);
606 gst_buffer_unmap (buf, &info);
610 if (!identity->silent) {
611 gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
614 if (identity->datarate > 0) {
615 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
616 GST_SECOND, identity->datarate);
618 GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf) = time;
619 GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
622 if (identity->signal_handoffs)
623 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
625 if (trans->segment.format == GST_FORMAT_TIME) {
626 rundts = gst_segment_to_running_time (&trans->segment,
627 GST_FORMAT_TIME, GST_BUFFER_DTS (buf));
628 runpts = gst_segment_to_running_time (&trans->segment,
629 GST_FORMAT_TIME, GST_BUFFER_PTS (buf));
632 if (GST_CLOCK_TIME_IS_VALID (rundts))
633 runtimestamp = rundts;
634 else if (GST_CLOCK_TIME_IS_VALID (runpts))
635 runtimestamp = runpts;
638 ret = gst_identity_do_sync (identity, runtimestamp);
640 identity->offset += size;
642 if (identity->sleep_time && ret == GST_FLOW_OK)
643 g_usleep (identity->sleep_time);
645 if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
646 && (ret == GST_FLOW_OK)) {
647 GST_BUFFER_DTS (buf) = rundts;
648 GST_BUFFER_PTS (buf) = runpts;
649 GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
650 GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
658 GST_ELEMENT_ERROR (identity, CORE, FAILED,
659 (_("Failed after iterations as requested.")), (NULL));
660 return GST_FLOW_ERROR;
664 if (!identity->silent) {
665 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
669 ts = GST_BUFFER_TIMESTAMP (buf);
670 if (GST_CLOCK_TIME_IS_VALID (ts)) {
671 duration = GST_BUFFER_DURATION (buf);
672 gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (identity),
673 gst_event_new_gap (ts, duration));
676 /* return DROPPED to basetransform. */
677 return GST_BASE_TRANSFORM_FLOW_DROPPED;
682 gst_identity_set_property (GObject * object, guint prop_id,
683 const GValue * value, GParamSpec * pspec)
685 GstIdentity *identity;
687 identity = GST_IDENTITY (object);
690 case PROP_SLEEP_TIME:
691 identity->sleep_time = g_value_get_uint (value);
694 identity->silent = g_value_get_boolean (value);
696 case PROP_SINGLE_SEGMENT:
697 identity->single_segment = g_value_get_boolean (value);
700 identity->dump = g_value_get_boolean (value);
702 case PROP_ERROR_AFTER:
703 identity->error_after = g_value_get_int (value);
705 case PROP_DROP_PROBABILITY:
706 identity->drop_probability = g_value_get_float (value);
708 case PROP_DROP_BUFFER_FLAGS:
709 identity->drop_buffer_flags = g_value_get_flags (value);
712 identity->datarate = g_value_get_int (value);
715 identity->sync = g_value_get_boolean (value);
717 case PROP_CHECK_IMPERFECT_TIMESTAMP:
718 identity->check_imperfect_timestamp = g_value_get_boolean (value);
720 case PROP_CHECK_IMPERFECT_OFFSET:
721 identity->check_imperfect_offset = g_value_get_boolean (value);
723 case PROP_SIGNAL_HANDOFFS:
724 identity->signal_handoffs = g_value_get_boolean (value);
727 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
730 if (identity->datarate > 0 || identity->single_segment)
731 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
733 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
737 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
740 GstIdentity *identity;
742 identity = GST_IDENTITY (object);
745 case PROP_SLEEP_TIME:
746 g_value_set_uint (value, identity->sleep_time);
748 case PROP_ERROR_AFTER:
749 g_value_set_int (value, identity->error_after);
751 case PROP_DROP_PROBABILITY:
752 g_value_set_float (value, identity->drop_probability);
754 case PROP_DROP_BUFFER_FLAGS:
755 g_value_set_flags (value, identity->drop_buffer_flags);
758 g_value_set_int (value, identity->datarate);
761 g_value_set_boolean (value, identity->silent);
763 case PROP_SINGLE_SEGMENT:
764 g_value_set_boolean (value, identity->single_segment);
767 g_value_set_boolean (value, identity->dump);
769 case PROP_LAST_MESSAGE:
770 GST_OBJECT_LOCK (identity);
771 g_value_set_string (value, identity->last_message);
772 GST_OBJECT_UNLOCK (identity);
775 g_value_set_boolean (value, identity->sync);
777 case PROP_CHECK_IMPERFECT_TIMESTAMP:
778 g_value_set_boolean (value, identity->check_imperfect_timestamp);
780 case PROP_CHECK_IMPERFECT_OFFSET:
781 g_value_set_boolean (value, identity->check_imperfect_offset);
783 case PROP_SIGNAL_HANDOFFS:
784 g_value_set_boolean (value, identity->signal_handoffs);
787 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
793 gst_identity_start (GstBaseTransform * trans)
795 GstIdentity *identity;
797 identity = GST_IDENTITY (trans);
799 identity->offset = 0;
800 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
801 identity->prev_duration = GST_CLOCK_TIME_NONE;
802 identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
803 identity->prev_offset = GST_BUFFER_OFFSET_NONE;
809 gst_identity_stop (GstBaseTransform * trans)
811 GstIdentity *identity;
813 identity = GST_IDENTITY (trans);
815 GST_OBJECT_LOCK (identity);
816 g_free (identity->last_message);
817 identity->last_message = NULL;
818 GST_OBJECT_UNLOCK (identity);
824 gst_identity_accept_caps (GstBaseTransform * base,
825 GstPadDirection direction, GstCaps * caps)
830 /* Proxy accept-caps */
832 if (direction == GST_PAD_SRC)
833 pad = GST_BASE_TRANSFORM_SINK_PAD (base);
835 pad = GST_BASE_TRANSFORM_SRC_PAD (base);
837 ret = gst_pad_peer_query_accept_caps (pad, caps);
843 gst_identity_query (GstBaseTransform * base, GstPadDirection direction,
846 GstIdentity *identity;
849 identity = GST_IDENTITY (base);
851 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (base, direction, query);
853 if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
854 gboolean live = FALSE;
855 GstClockTime min = 0, max = 0;
858 gst_query_parse_latency (query, &live, &min, &max);
860 if (identity->sync && max < min) {
861 GST_ELEMENT_WARNING (base, CORE, CLOCK, (NULL),
862 ("Impossible to configure latency before identity sync=true:"
863 " max %" GST_TIME_FORMAT " < min %"
864 GST_TIME_FORMAT ". Add queues or other buffering elements.",
865 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
869 /* Ignore the upstream latency if it is not live */
870 GST_OBJECT_LOCK (identity);
872 identity->upstream_latency = min;
874 identity->upstream_latency = 0;
875 GST_OBJECT_UNLOCK (identity);
877 gst_query_set_latency (query, live || identity->sync, min, max);
883 static GstStateChangeReturn
884 gst_identity_change_state (GstElement * element, GstStateChange transition)
886 GstStateChangeReturn ret;
887 GstIdentity *identity = GST_IDENTITY (element);
888 gboolean no_preroll = FALSE;
890 switch (transition) {
891 case GST_STATE_CHANGE_NULL_TO_READY:
893 case GST_STATE_CHANGE_READY_TO_PAUSED:
894 GST_OBJECT_LOCK (identity);
895 identity->blocked = TRUE;
896 GST_OBJECT_UNLOCK (identity);
900 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
901 GST_OBJECT_LOCK (identity);
902 identity->blocked = FALSE;
903 g_cond_broadcast (&identity->blocked_cond);
904 GST_OBJECT_UNLOCK (identity);
906 case GST_STATE_CHANGE_PAUSED_TO_READY:
907 GST_OBJECT_LOCK (identity);
908 if (identity->clock_id) {
909 GST_DEBUG_OBJECT (identity, "unlock clock wait");
910 gst_clock_id_unschedule (identity->clock_id);
912 identity->blocked = FALSE;
913 g_cond_broadcast (&identity->blocked_cond);
914 GST_OBJECT_UNLOCK (identity);
920 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
922 switch (transition) {
923 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
924 GST_OBJECT_LOCK (identity);
925 identity->upstream_latency = 0;
926 identity->blocked = TRUE;
927 GST_OBJECT_UNLOCK (identity);
931 case GST_STATE_CHANGE_PAUSED_TO_READY:
933 case GST_STATE_CHANGE_READY_TO_NULL:
939 if (no_preroll && ret == GST_STATE_CHANGE_SUCCESS)
940 ret = GST_STATE_CHANGE_NO_PREROLL;