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 "../../gst/gst-i18n-lib.h"
40 #include "gstidentity.h"
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
52 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
53 #define GST_CAT_DEFAULT gst_identity_debug
55 /* Identity signals and args */
63 #define DEFAULT_SLEEP_TIME 0
64 #define DEFAULT_DUPLICATE 1
65 #define DEFAULT_ERROR_AFTER -1
66 #define DEFAULT_DROP_PROBABILITY 0.0
67 #define DEFAULT_DROP_BUFFER_FLAGS 0
68 #define DEFAULT_DATARATE 0
69 #define DEFAULT_SILENT TRUE
70 #define DEFAULT_SINGLE_SEGMENT FALSE
71 #define DEFAULT_DUMP FALSE
72 #define DEFAULT_SYNC FALSE
73 #define DEFAULT_CHECK_IMPERFECT_TIMESTAMP FALSE
74 #define DEFAULT_CHECK_IMPERFECT_OFFSET FALSE
75 #define DEFAULT_SIGNAL_HANDOFFS TRUE
76 #define DEFAULT_TS_OFFSET 0
77 #define DEFAULT_DROP_ALLOCATION FALSE
78 #define DEFAULT_EOS_AFTER -1
85 PROP_DROP_PROBABILITY,
86 PROP_DROP_BUFFER_FLAGS,
94 PROP_CHECK_IMPERFECT_TIMESTAMP,
95 PROP_CHECK_IMPERFECT_OFFSET,
103 GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
104 #define gst_identity_parent_class parent_class
105 G_DEFINE_TYPE_WITH_CODE (GstIdentity, gst_identity, GST_TYPE_BASE_TRANSFORM,
108 static void gst_identity_finalize (GObject * object);
109 static void gst_identity_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void gst_identity_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 static gboolean gst_identity_sink_event (GstBaseTransform * trans,
116 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
118 static gboolean gst_identity_start (GstBaseTransform * trans);
119 static gboolean gst_identity_stop (GstBaseTransform * trans);
120 static GstStateChangeReturn gst_identity_change_state (GstElement * element,
121 GstStateChange transition);
122 static gboolean gst_identity_accept_caps (GstBaseTransform * base,
123 GstPadDirection direction, GstCaps * caps);
124 static gboolean gst_identity_query (GstBaseTransform * base,
125 GstPadDirection direction, GstQuery * query);
127 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
129 static GParamSpec *pspec_last_message = NULL;
132 gst_identity_finalize (GObject * object)
134 GstIdentity *identity;
136 identity = GST_IDENTITY (object);
138 g_free (identity->last_message);
139 g_cond_clear (&identity->blocked_cond);
141 G_OBJECT_CLASS (parent_class)->finalize (object);
145 gst_identity_class_init (GstIdentityClass * klass)
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
149 GstBaseTransformClass *gstbasetrans_class;
151 gobject_class = G_OBJECT_CLASS (klass);
152 gstelement_class = GST_ELEMENT_CLASS (klass);
153 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
155 gobject_class->set_property = gst_identity_set_property;
156 gobject_class->get_property = gst_identity_get_property;
158 g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
159 g_param_spec_uint ("sleep-time", "Sleep time",
160 "Microseconds to sleep between processing", 0, G_MAXUINT,
161 DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
163 g_param_spec_int ("error-after", "Error After", "Error after N buffers",
164 -1, G_MAXINT, DEFAULT_ERROR_AFTER,
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
167 g_param_spec_float ("drop-probability", "Drop Probability",
168 "The Probability a buffer is dropped", 0.0, 1.0,
169 DEFAULT_DROP_PROBABILITY,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173 * GstIdentity:drop-buffer-flags:
175 * Drop buffers with the given flags.
179 g_object_class_install_property (gobject_class, PROP_DROP_BUFFER_FLAGS,
180 g_param_spec_flags ("drop-buffer-flags", "Check flags to drop buffers",
181 "Drop buffers with the given flags",
182 GST_TYPE_BUFFER_FLAGS, DEFAULT_DROP_BUFFER_FLAGS,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class, PROP_DATARATE,
185 g_param_spec_int ("datarate", "Datarate",
186 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
187 0, G_MAXINT, DEFAULT_DATARATE,
188 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class, PROP_SILENT,
190 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
193 g_param_spec_boolean ("single-segment", "Single Segment",
194 "Timestamp buffers and eat segments so as to appear as one segment",
195 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 pspec_last_message = g_param_spec_string ("last-message", "last-message",
197 "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
198 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
200 g_object_class_install_property (gobject_class, PROP_DUMP,
201 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
202 DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_SYNC,
204 g_param_spec_boolean ("sync", "Synchronize",
205 "Synchronize to pipeline clock", DEFAULT_SYNC,
206 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
208 g_param_spec_int64 ("ts-offset", "Timestamp offset for synchronisation",
209 "Timestamp offset in nanoseconds for synchronisation, negative for earlier sync",
210 G_MININT64, G_MAXINT64, DEFAULT_TS_OFFSET,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class,
213 PROP_CHECK_IMPERFECT_TIMESTAMP,
214 g_param_spec_boolean ("check-imperfect-timestamp",
215 "Check for discontiguous timestamps",
216 "Send element messages if timestamps and durations do not match up",
217 DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
220 g_param_spec_boolean ("check-imperfect-offset",
221 "Check for discontiguous offset",
222 "Send element messages if offset and offset_end do not match up",
223 DEFAULT_CHECK_IMPERFECT_OFFSET,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 * GstIdentity:signal-handoffs
229 * If set to %TRUE, the identity will emit a handoff signal when handling a buffer.
230 * When set to %FALSE, no signal will be emitted, which might improve performance.
232 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
233 g_param_spec_boolean ("signal-handoffs",
234 "Signal handoffs", "Send a signal before pushing the buffer",
235 DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (gobject_class, PROP_DROP_ALLOCATION,
238 g_param_spec_boolean ("drop-allocation", "Drop allocation query",
239 "Don't forward allocation queries", DEFAULT_DROP_ALLOCATION,
240 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 * GstIdentity:eos-after
245 * EOS after N buffers.
249 g_object_class_install_property (gobject_class, PROP_EOS_AFTER,
250 g_param_spec_int ("eos-after", "EOS After", "EOS after N buffers",
251 -1, G_MAXINT, DEFAULT_EOS_AFTER,
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 * GstIdentity::handoff:
256 * @identity: the identity instance
257 * @buffer: the buffer that just has been received
258 * @pad: the pad that received it
260 * This signal gets emitted before passing the buffer downstream.
262 gst_identity_signals[SIGNAL_HANDOFF] =
263 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
264 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
265 g_cclosure_marshal_generic, G_TYPE_NONE, 1,
266 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
268 gobject_class->finalize = gst_identity_finalize;
270 gst_element_class_set_static_metadata (gstelement_class,
273 "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
274 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
275 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
277 gstelement_class->change_state =
278 GST_DEBUG_FUNCPTR (gst_identity_change_state);
280 gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
281 gstbasetrans_class->transform_ip =
282 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
283 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
284 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
285 gstbasetrans_class->accept_caps =
286 GST_DEBUG_FUNCPTR (gst_identity_accept_caps);
287 gstbasetrans_class->query = gst_identity_query;
291 gst_identity_init (GstIdentity * identity)
293 identity->sleep_time = DEFAULT_SLEEP_TIME;
294 identity->error_after = DEFAULT_ERROR_AFTER;
295 identity->error_after_counter = DEFAULT_ERROR_AFTER;
296 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
297 identity->drop_buffer_flags = DEFAULT_DROP_BUFFER_FLAGS;
298 identity->datarate = DEFAULT_DATARATE;
299 identity->silent = DEFAULT_SILENT;
300 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
301 identity->sync = DEFAULT_SYNC;
302 identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
303 identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
304 identity->dump = DEFAULT_DUMP;
305 identity->last_message = NULL;
306 identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
307 identity->ts_offset = DEFAULT_TS_OFFSET;
308 g_cond_init (&identity->blocked_cond);
309 identity->eos_after = DEFAULT_EOS_AFTER;
310 identity->eos_after_counter = DEFAULT_EOS_AFTER;
312 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
316 gst_identity_notify_last_message (GstIdentity * identity)
318 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
322 gst_identity_do_sync (GstIdentity * identity, GstClockTime running_time)
324 GstFlowReturn ret = GST_FLOW_OK;
326 if (identity->sync &&
327 GST_BASE_TRANSFORM_CAST (identity)->segment.format == GST_FORMAT_TIME) {
330 GST_OBJECT_LOCK (identity);
332 if (identity->flushing) {
333 GST_OBJECT_UNLOCK (identity);
334 return GST_FLOW_FLUSHING;
337 while (identity->blocked)
338 g_cond_wait (&identity->blocked_cond, GST_OBJECT_GET_LOCK (identity));
340 if (identity->flushing) {
341 GST_OBJECT_UNLOCK (identity);
342 return GST_FLOW_FLUSHING;
345 if ((clock = GST_ELEMENT (identity)->clock)) {
347 GstClockTime timestamp;
348 GstClockTimeDiff ts_offset = identity->ts_offset;
350 timestamp = running_time + GST_ELEMENT (identity)->base_time +
351 identity->upstream_latency;
353 ts_offset = -ts_offset;
354 if (ts_offset < timestamp)
355 timestamp -= ts_offset;
359 timestamp += ts_offset;
361 /* save id if we need to unlock */
362 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
363 GST_OBJECT_UNLOCK (identity);
365 cret = gst_clock_id_wait (identity->clock_id, NULL);
367 GST_OBJECT_LOCK (identity);
368 if (identity->clock_id) {
369 gst_clock_id_unref (identity->clock_id);
370 identity->clock_id = NULL;
372 if (cret == GST_CLOCK_UNSCHEDULED || identity->flushing)
373 ret = GST_FLOW_FLUSHING;
375 GST_OBJECT_UNLOCK (identity);
382 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
384 GstIdentity *identity;
387 identity = GST_IDENTITY (trans);
389 if (!identity->silent) {
390 const GstStructure *s;
394 GST_OBJECT_LOCK (identity);
395 g_free (identity->last_message);
397 tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
398 if ((s = gst_event_get_structure (event)))
399 sstr = gst_structure_to_string (s);
401 sstr = g_strdup ("");
403 identity->last_message =
404 g_strdup_printf ("event ******* (%s:%s) E (type: %s (%d), %s) %p",
405 GST_DEBUG_PAD_NAME (trans->sinkpad), tstr, GST_EVENT_TYPE (event),
408 GST_OBJECT_UNLOCK (identity);
410 gst_identity_notify_last_message (identity);
413 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
414 if (!trans->have_segment) {
418 gst_event_copy_segment (event, &segment);
419 gst_event_copy_segment (event, &trans->segment);
420 trans->have_segment = TRUE;
422 /* This is the first segment, send out a (0, -1) segment */
423 gst_segment_init (&segment, segment.format);
424 news = gst_event_new_segment (&segment);
426 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
428 /* need to track segment for proper running time */
429 gst_event_copy_segment (event, &trans->segment);
433 if (GST_EVENT_TYPE (event) == GST_EVENT_GAP &&
434 trans->have_segment && trans->segment.format == GST_FORMAT_TIME) {
435 GstClockTime start, dur;
437 gst_event_parse_gap (event, &start, &dur);
438 if (GST_CLOCK_TIME_IS_VALID (start)) {
439 start = gst_segment_to_running_time (&trans->segment,
440 GST_FORMAT_TIME, start);
442 gst_identity_do_sync (identity, start);
444 /* also transform GAP timestamp similar to buffer timestamps */
445 if (identity->single_segment) {
446 gst_event_unref (event);
447 event = gst_event_new_gap (start, dur);
452 /* Reset previous timestamp, duration and offsets on SEGMENT
453 * to prevent false warnings when checking for perfect streams */
454 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
455 identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
456 identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
459 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
460 /* eat up segments */
461 gst_event_unref (event);
464 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
465 GST_OBJECT_LOCK (identity);
466 identity->flushing = TRUE;
467 if (identity->clock_id) {
468 GST_DEBUG_OBJECT (identity, "unlock clock wait");
469 gst_clock_id_unschedule (identity->clock_id);
471 GST_OBJECT_UNLOCK (identity);
472 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
473 GST_OBJECT_LOCK (identity);
474 identity->flushing = FALSE;
475 GST_OBJECT_UNLOCK (identity);
478 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
485 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
487 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
489 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
490 if (timestamp != GST_CLOCK_TIME_NONE) {
491 /* check if we had a previous buffer to compare to */
492 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
493 identity->prev_duration != GST_CLOCK_TIME_NONE) {
494 GstClockTime t_expected;
497 t_expected = identity->prev_timestamp + identity->prev_duration;
498 dt = GST_CLOCK_DIFF (t_expected, timestamp);
501 * "imperfect-timestamp" bus message:
502 * @identity: the identity instance
503 * @delta: the GST_CLOCK_DIFF to the prev timestamp
504 * @prev-timestamp: the previous buffer timestamp
505 * @prev-duration: the previous buffer duration
506 * @prev-offset: the previous buffer offset
507 * @prev-offset-end: the previous buffer offset end
508 * @cur-timestamp: the current buffer timestamp
509 * @cur-duration: the current buffer duration
510 * @cur-offset: the current buffer offset
511 * @cur-offset-end: the current buffer offset end
513 * This bus message gets emitted if the check-imperfect-timestamp
514 * property is set and there is a gap in time between the
515 * last buffer and the newly received buffer.
517 gst_element_post_message (GST_ELEMENT (identity),
518 gst_message_new_element (GST_OBJECT (identity),
519 gst_structure_new ("imperfect-timestamp",
520 "delta", G_TYPE_INT64, dt,
521 "prev-timestamp", G_TYPE_UINT64,
522 identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
523 identity->prev_duration, "prev-offset", G_TYPE_UINT64,
524 identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
525 identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
526 timestamp, "cur-duration", G_TYPE_UINT64,
527 GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
528 GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
529 GST_BUFFER_OFFSET_END (buf), NULL)));
532 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
533 "offset_end was set on previous buffer");
539 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
543 offset = GST_BUFFER_OFFSET (buf);
545 if (identity->prev_offset_end != offset &&
546 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
547 offset != GST_BUFFER_OFFSET_NONE) {
549 * "imperfect-offset" bus message:
550 * @identity: the identity instance
551 * @prev-timestamp: the previous buffer timestamp
552 * @prev-duration: the previous buffer duration
553 * @prev-offset: the previous buffer offset
554 * @prev-offset-end: the previous buffer offset end
555 * @cur-timestamp: the current buffer timestamp
556 * @cur-duration: the current buffer duration
557 * @cur-offset: the current buffer offset
558 * @cur-offset-end: the current buffer offset end
560 * This bus message gets emitted if the check-imperfect-offset
561 * property is set and there is a gap in offsets between the
562 * last buffer and the newly received buffer.
564 gst_element_post_message (GST_ELEMENT (identity),
565 gst_message_new_element (GST_OBJECT (identity),
566 gst_structure_new ("imperfect-offset", "prev-timestamp",
567 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
568 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
569 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
570 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
571 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
572 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
573 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
574 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
576 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
577 "and/or offset_end were set on previous buffer");
582 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
584 if (ts == GST_CLOCK_TIME_NONE)
587 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
592 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
593 const gchar * action, GstBuffer * buf, gsize size)
595 gchar dts_str[64], pts_str[64], dur_str[64];
596 gchar *flag_str, *meta_str;
598 GST_OBJECT_LOCK (identity);
600 flag_str = gst_buffer_get_flags_string (buf);
601 meta_str = gst_buffer_get_meta_string (buf);
603 g_free (identity->last_message);
604 identity->last_message = g_strdup_printf ("%s ******* (%s:%s) "
605 "(%" G_GSIZE_FORMAT " bytes, dts: %s, pts: %s, duration: %s, offset: %"
606 G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
607 ", flags: %08x %s, meta: %s) %p", action,
608 GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
609 print_pretty_time (dts_str, sizeof (dts_str), GST_BUFFER_DTS (buf)),
610 print_pretty_time (pts_str, sizeof (pts_str), GST_BUFFER_PTS (buf)),
611 print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
612 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
613 GST_BUFFER_FLAGS (buf), flag_str, meta_str ? meta_str : "none", buf);
616 GST_OBJECT_UNLOCK (identity);
618 gst_identity_notify_last_message (identity);
622 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
624 GstFlowReturn ret = GST_FLOW_OK;
625 GstIdentity *identity = GST_IDENTITY (trans);
626 GstClockTime rundts = GST_CLOCK_TIME_NONE;
627 GstClockTime runpts = GST_CLOCK_TIME_NONE;
628 GstClockTime ts, duration, runtimestamp;
631 size = gst_buffer_get_size (buf);
633 if (identity->check_imperfect_timestamp)
634 gst_identity_check_imperfect_timestamp (identity, buf);
635 if (identity->check_imperfect_offset)
636 gst_identity_check_imperfect_offset (identity, buf);
638 /* update prev values */
639 identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
640 identity->prev_duration = GST_BUFFER_DURATION (buf);
641 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
642 identity->prev_offset = GST_BUFFER_OFFSET (buf);
644 if (identity->error_after_counter >= 0) {
645 identity->error_after_counter--;
646 if (identity->error_after_counter == 0)
650 if (identity->eos_after_counter >= 0) {
651 identity->eos_after_counter--;
652 if (identity->eos_after_counter == 0)
656 if (identity->drop_probability > 0.0) {
657 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
661 if (GST_BUFFER_FLAG_IS_SET (buf, identity->drop_buffer_flags))
664 if (identity->dump) {
667 if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
668 gst_util_dump_mem (info.data, info.size);
669 gst_buffer_unmap (buf, &info);
673 if (!identity->silent) {
674 gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
677 if (identity->datarate > 0) {
678 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
679 GST_SECOND, identity->datarate);
681 GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf) = time;
682 GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
685 if (identity->signal_handoffs)
686 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
688 if (trans->segment.format == GST_FORMAT_TIME) {
689 rundts = gst_segment_to_running_time (&trans->segment,
690 GST_FORMAT_TIME, GST_BUFFER_DTS (buf));
691 runpts = gst_segment_to_running_time (&trans->segment,
692 GST_FORMAT_TIME, GST_BUFFER_PTS (buf));
695 if (GST_CLOCK_TIME_IS_VALID (rundts))
696 runtimestamp = rundts;
697 else if (GST_CLOCK_TIME_IS_VALID (runpts))
698 runtimestamp = runpts;
701 ret = gst_identity_do_sync (identity, runtimestamp);
703 identity->offset += size;
705 if (identity->sleep_time && ret == GST_FLOW_OK)
706 g_usleep (identity->sleep_time);
708 if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
709 && (ret == GST_FLOW_OK)) {
710 GST_BUFFER_DTS (buf) = rundts;
711 GST_BUFFER_PTS (buf) = runpts;
712 GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
713 GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
721 GST_ELEMENT_ERROR (identity, CORE, FAILED,
722 (_("Failed after iterations as requested.")), (NULL));
723 return GST_FLOW_ERROR;
727 GST_DEBUG_OBJECT (identity, "EOS after iterations as requested.");
732 if (!identity->silent) {
733 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
737 ts = GST_BUFFER_TIMESTAMP (buf);
738 if (GST_CLOCK_TIME_IS_VALID (ts)) {
739 duration = GST_BUFFER_DURATION (buf);
740 gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (identity),
741 gst_event_new_gap (ts, duration));
744 /* return DROPPED to basetransform. */
745 return GST_BASE_TRANSFORM_FLOW_DROPPED;
750 gst_identity_set_property (GObject * object, guint prop_id,
751 const GValue * value, GParamSpec * pspec)
753 GstIdentity *identity;
755 identity = GST_IDENTITY (object);
758 case PROP_SLEEP_TIME:
759 identity->sleep_time = g_value_get_uint (value);
762 identity->silent = g_value_get_boolean (value);
764 case PROP_SINGLE_SEGMENT:
765 identity->single_segment = g_value_get_boolean (value);
768 identity->dump = g_value_get_boolean (value);
770 case PROP_ERROR_AFTER:
771 identity->error_after = g_value_get_int (value);
773 case PROP_DROP_PROBABILITY:
774 identity->drop_probability = g_value_get_float (value);
776 case PROP_DROP_BUFFER_FLAGS:
777 identity->drop_buffer_flags = g_value_get_flags (value);
780 identity->datarate = g_value_get_int (value);
783 identity->sync = g_value_get_boolean (value);
786 identity->ts_offset = g_value_get_int64 (value);
788 case PROP_CHECK_IMPERFECT_TIMESTAMP:
789 identity->check_imperfect_timestamp = g_value_get_boolean (value);
791 case PROP_CHECK_IMPERFECT_OFFSET:
792 identity->check_imperfect_offset = g_value_get_boolean (value);
794 case PROP_SIGNAL_HANDOFFS:
795 identity->signal_handoffs = g_value_get_boolean (value);
797 case PROP_DROP_ALLOCATION:
798 identity->drop_allocation = g_value_get_boolean (value);
801 identity->eos_after = g_value_get_int (value);
804 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
807 if (identity->datarate > 0 || identity->single_segment)
808 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), FALSE);
810 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
814 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
817 GstIdentity *identity;
819 identity = GST_IDENTITY (object);
822 case PROP_SLEEP_TIME:
823 g_value_set_uint (value, identity->sleep_time);
825 case PROP_ERROR_AFTER:
826 g_value_set_int (value, identity->error_after);
828 case PROP_DROP_PROBABILITY:
829 g_value_set_float (value, identity->drop_probability);
831 case PROP_DROP_BUFFER_FLAGS:
832 g_value_set_flags (value, identity->drop_buffer_flags);
835 g_value_set_int (value, identity->datarate);
838 g_value_set_boolean (value, identity->silent);
840 case PROP_SINGLE_SEGMENT:
841 g_value_set_boolean (value, identity->single_segment);
844 g_value_set_boolean (value, identity->dump);
846 case PROP_LAST_MESSAGE:
847 GST_OBJECT_LOCK (identity);
848 g_value_set_string (value, identity->last_message);
849 GST_OBJECT_UNLOCK (identity);
852 g_value_set_boolean (value, identity->sync);
855 identity->ts_offset = g_value_get_int64 (value);
857 case PROP_CHECK_IMPERFECT_TIMESTAMP:
858 g_value_set_boolean (value, identity->check_imperfect_timestamp);
860 case PROP_CHECK_IMPERFECT_OFFSET:
861 g_value_set_boolean (value, identity->check_imperfect_offset);
863 case PROP_SIGNAL_HANDOFFS:
864 g_value_set_boolean (value, identity->signal_handoffs);
866 case PROP_DROP_ALLOCATION:
867 g_value_set_boolean (value, identity->drop_allocation);
870 g_value_set_int (value, identity->eos_after);
873 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
879 gst_identity_start (GstBaseTransform * trans)
881 GstIdentity *identity;
883 identity = GST_IDENTITY (trans);
885 if (identity->eos_after != DEFAULT_EOS_AFTER
886 && identity->error_after != DEFAULT_ERROR_AFTER)
887 goto both_afters_defined;
889 identity->offset = 0;
890 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
891 identity->prev_duration = GST_CLOCK_TIME_NONE;
892 identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
893 identity->prev_offset = GST_BUFFER_OFFSET_NONE;
894 identity->error_after_counter = identity->error_after;
895 identity->eos_after_counter = identity->eos_after;
902 GST_ELEMENT_ERROR (identity, CORE, FAILED,
903 (_("eos-after and error-after can't both be defined.")), (NULL));
909 gst_identity_stop (GstBaseTransform * trans)
911 GstIdentity *identity;
913 identity = GST_IDENTITY (trans);
915 GST_OBJECT_LOCK (identity);
916 g_free (identity->last_message);
917 identity->last_message = NULL;
918 GST_OBJECT_UNLOCK (identity);
924 gst_identity_accept_caps (GstBaseTransform * base,
925 GstPadDirection direction, GstCaps * caps)
930 /* Proxy accept-caps */
932 if (direction == GST_PAD_SRC)
933 pad = GST_BASE_TRANSFORM_SINK_PAD (base);
935 pad = GST_BASE_TRANSFORM_SRC_PAD (base);
937 ret = gst_pad_peer_query_accept_caps (pad, caps);
943 gst_identity_query (GstBaseTransform * base, GstPadDirection direction,
946 GstIdentity *identity;
949 identity = GST_IDENTITY (base);
951 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION &&
952 identity->drop_allocation) {
953 GST_DEBUG_OBJECT (identity, "Dropping allocation query.");
957 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (base, direction, query);
959 if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
960 gboolean live = FALSE;
961 GstClockTime min = 0, max = 0;
964 gst_query_parse_latency (query, &live, &min, &max);
966 if (identity->sync && max < min) {
967 GST_ELEMENT_WARNING (base, CORE, CLOCK, (NULL),
968 ("Impossible to configure latency before identity sync=true:"
969 " max %" GST_TIME_FORMAT " < min %"
970 GST_TIME_FORMAT ". Add queues or other buffering elements.",
971 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
975 /* Ignore the upstream latency if it is not live */
976 GST_OBJECT_LOCK (identity);
978 identity->upstream_latency = min;
980 identity->upstream_latency = 0;
981 GST_OBJECT_UNLOCK (identity);
983 gst_query_set_latency (query, live || identity->sync, min, max);
989 static GstStateChangeReturn
990 gst_identity_change_state (GstElement * element, GstStateChange transition)
992 GstStateChangeReturn ret;
993 GstIdentity *identity = GST_IDENTITY (element);
994 gboolean no_preroll = FALSE;
996 switch (transition) {
997 case GST_STATE_CHANGE_NULL_TO_READY:
999 case GST_STATE_CHANGE_READY_TO_PAUSED:
1000 GST_OBJECT_LOCK (identity);
1001 identity->flushing = FALSE;
1002 identity->blocked = TRUE;
1003 GST_OBJECT_UNLOCK (identity);
1007 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1008 GST_OBJECT_LOCK (identity);
1009 identity->blocked = FALSE;
1010 g_cond_broadcast (&identity->blocked_cond);
1011 GST_OBJECT_UNLOCK (identity);
1013 case GST_STATE_CHANGE_PAUSED_TO_READY:
1014 GST_OBJECT_LOCK (identity);
1015 identity->flushing = TRUE;
1016 if (identity->clock_id) {
1017 GST_DEBUG_OBJECT (identity, "unlock clock wait");
1018 gst_clock_id_unschedule (identity->clock_id);
1020 identity->blocked = FALSE;
1021 g_cond_broadcast (&identity->blocked_cond);
1022 GST_OBJECT_UNLOCK (identity);
1028 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1030 switch (transition) {
1031 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1032 GST_OBJECT_LOCK (identity);
1033 identity->upstream_latency = 0;
1034 identity->blocked = TRUE;
1035 GST_OBJECT_UNLOCK (identity);
1039 case GST_STATE_CHANGE_PAUSED_TO_READY:
1041 case GST_STATE_CHANGE_READY_TO_NULL:
1047 if (no_preroll && ret == GST_STATE_CHANGE_SUCCESS)
1048 ret = GST_STATE_CHANGE_NO_PREROLL;