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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 "../../gst/gst-i18n-lib.h"
38 #include "gstidentity.h"
39 #include <gst/gstmarshal.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_DATARATE 0
67 #define DEFAULT_SILENT FALSE
68 #define DEFAULT_SINGLE_SEGMENT FALSE
69 #define DEFAULT_DUMP FALSE
70 #define DEFAULT_SYNC FALSE
71 #define DEFAULT_CHECK_PERFECT 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,
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 GstFlowReturn gst_identity_prepare_output_buffer (GstBaseTransform *
112 trans, GstBuffer * in_buf, GstBuffer ** out_buf);
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);
118 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
120 static GParamSpec *pspec_last_message = NULL;
123 gst_identity_finalize (GObject * object)
125 GstIdentity *identity;
127 identity = GST_IDENTITY (object);
129 g_free (identity->last_message);
131 #if !GLIB_CHECK_VERSION(2,26,0)
132 g_static_rec_mutex_free (&identity->notify_lock);
135 G_OBJECT_CLASS (parent_class)->finalize (object);
138 /* fixme: do something about this */
140 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
141 guint n_param_values, const GValue * param_values, gpointer invocation_hint,
142 gpointer marshal_data)
144 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
146 register marshalfunc_VOID__MINIOBJECT callback;
147 register GCClosure *cc = (GCClosure *) closure;
148 register gpointer data1, data2;
150 g_return_if_fail (n_param_values == 2);
152 if (G_CCLOSURE_SWAP_DATA (closure)) {
153 data1 = closure->data;
154 data2 = g_value_peek_pointer (param_values + 0);
156 data1 = g_value_peek_pointer (param_values + 0);
157 data2 = closure->data;
160 (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data :
163 callback (data1, g_value_get_boxed (param_values + 1), data2);
167 gst_identity_class_init (GstIdentityClass * klass)
169 GObjectClass *gobject_class;
170 GstElementClass *gstelement_class;
171 GstBaseTransformClass *gstbasetrans_class;
173 gobject_class = G_OBJECT_CLASS (klass);
174 gstelement_class = GST_ELEMENT_CLASS (klass);
175 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
177 gobject_class->set_property = gst_identity_set_property;
178 gobject_class->get_property = gst_identity_get_property;
180 g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
181 g_param_spec_uint ("sleep-time", "Sleep time",
182 "Microseconds to sleep between processing", 0, G_MAXUINT,
183 DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
185 g_param_spec_int ("error-after", "Error After", "Error after N buffers",
186 G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER,
187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
189 g_param_spec_float ("drop-probability", "Drop Probability",
190 "The Probability a buffer is dropped", 0.0, 1.0,
191 DEFAULT_DROP_PROBABILITY,
192 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_DATARATE,
194 g_param_spec_int ("datarate", "Datarate",
195 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
196 0, G_MAXINT, DEFAULT_DATARATE,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_SILENT,
199 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
202 g_param_spec_boolean ("single-segment", "Single Segment",
203 "Timestamp buffers and eat segments so as to appear as one segment",
204 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 pspec_last_message = g_param_spec_string ("last-message", "last-message",
206 "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
207 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
209 g_object_class_install_property (gobject_class, PROP_DUMP,
210 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
211 DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_SYNC,
213 g_param_spec_boolean ("sync", "Synchronize",
214 "Synchronize to pipeline clock", DEFAULT_SYNC,
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_CHECK_PERFECT,
217 g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
218 "Verify that the stream is time- and data-contiguous. "
219 "This only logs in the debug log. This will be deprecated in favor "
220 "of the check-imperfect-timestamp/offset properties.",
221 DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (gobject_class,
223 PROP_CHECK_IMPERFECT_TIMESTAMP,
224 g_param_spec_boolean ("check-imperfect-timestamp",
225 "Check for discontiguous timestamps",
226 "Send element messages if timestamps and durations do not match up",
227 DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
230 g_param_spec_boolean ("check-imperfect-offset",
231 "Check for discontiguous offset",
232 "Send element messages if offset and offset_end do not match up",
233 DEFAULT_CHECK_IMPERFECT_OFFSET,
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 * GstIdentity:signal-handoffs
239 * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
240 * When set to #FALSE, no signal will be emited, which might improve performance.
244 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
245 g_param_spec_boolean ("signal-handoffs",
246 "Signal handoffs", "Send a signal before pushing the buffer",
247 DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 * GstIdentity::handoff:
251 * @identity: the identity instance
252 * @buffer: the buffer that just has been received
253 * @pad: the pad that received it
255 * This signal gets emitted before passing the buffer downstream.
257 gst_identity_signals[SIGNAL_HANDOFF] =
258 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
259 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
260 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
262 gobject_class->finalize = gst_identity_finalize;
264 gst_element_class_set_details_simple (gstelement_class,
267 "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
268 gst_element_class_add_pad_template (gstelement_class,
269 gst_static_pad_template_get (&srctemplate));
270 gst_element_class_add_pad_template (gstelement_class,
271 gst_static_pad_template_get (&sinktemplate));
273 gstelement_class->change_state =
274 GST_DEBUG_FUNCPTR (gst_identity_change_state);
275 gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
276 gstbasetrans_class->transform_ip =
277 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
278 gstbasetrans_class->prepare_output_buffer =
279 GST_DEBUG_FUNCPTR (gst_identity_prepare_output_buffer);
280 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
281 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
285 gst_identity_init (GstIdentity * identity)
287 identity->sleep_time = DEFAULT_SLEEP_TIME;
288 identity->error_after = DEFAULT_ERROR_AFTER;
289 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
290 identity->datarate = DEFAULT_DATARATE;
291 identity->silent = DEFAULT_SILENT;
292 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
293 identity->sync = DEFAULT_SYNC;
294 identity->check_perfect = DEFAULT_CHECK_PERFECT;
295 identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
296 identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
297 identity->dump = DEFAULT_DUMP;
298 identity->last_message = NULL;
299 identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
301 #if !GLIB_CHECK_VERSION(2,26,0)
302 g_static_rec_mutex_init (&identity->notify_lock);
305 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
309 gst_identity_notify_last_message (GstIdentity * identity)
311 /* FIXME: this hacks around a bug in GLib/GObject: doing concurrent
312 * g_object_notify() on the same object might lead to crashes, see
313 * http://bugzilla.gnome.org/show_bug.cgi?id=166020#c60 and follow-ups.
314 * So we really don't want to do a g_object_notify() here for out-of-band
315 * events with the streaming thread possibly also doing a g_object_notify()
316 * for an in-band buffer or event. This is fixed in GLib >= 2.26 */
317 #if !GLIB_CHECK_VERSION(2,26,0)
318 g_static_rec_mutex_lock (&identity->notify_lock);
319 g_object_notify ((GObject *) identity, "last-message");
320 g_static_rec_mutex_unlock (&identity->notify_lock);
322 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
327 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
329 GstIdentity *identity;
332 identity = GST_IDENTITY (trans);
334 if (!identity->silent) {
335 const GstStructure *s;
338 GST_OBJECT_LOCK (identity);
339 g_free (identity->last_message);
341 if ((s = gst_event_get_structure (event)))
342 sstr = gst_structure_to_string (s);
344 sstr = g_strdup ("");
346 identity->last_message =
347 g_strdup_printf ("event ******* (%s:%s) E (type: %d, %s) %p",
348 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
351 GST_OBJECT_UNLOCK (identity);
353 gst_identity_notify_last_message (identity);
356 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
357 if (trans->have_segment == FALSE) {
361 gst_event_copy_segment (event, &segment);
362 gst_event_copy_segment (event, &trans->segment);
363 trans->have_segment = TRUE;
365 /* This is the first segment, send out a (0, -1) segment */
366 gst_segment_init (&segment, segment.format);
367 news = gst_event_new_segment (&segment);
369 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
373 /* Reset previous timestamp, duration and offsets on NEWSEGMENT
374 * to prevent false warnings when checking for perfect streams */
375 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
376 identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
377 identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
380 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
381 /* eat up segments */
382 gst_event_unref (event);
385 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
386 GST_OBJECT_LOCK (identity);
387 if (identity->clock_id) {
388 GST_DEBUG_OBJECT (identity, "unlock clock wait");
389 gst_clock_id_unschedule (identity->clock_id);
390 gst_clock_id_unref (identity->clock_id);
391 identity->clock_id = NULL;
393 GST_OBJECT_UNLOCK (identity);
396 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
403 gst_identity_prepare_output_buffer (GstBaseTransform * trans,
404 GstBuffer * in_buf, GstBuffer ** out_buf)
406 GstIdentity *identity = GST_IDENTITY (trans);
408 /* only bother if we may have to alter metadata */
409 if (identity->datarate > 0 || identity->single_segment) {
410 if (gst_buffer_is_writable (in_buf))
415 *out_buf = gst_buffer_copy (in_buf);
424 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
426 GstClockTime timestamp;
428 timestamp = GST_BUFFER_TIMESTAMP (buf);
430 /* see if we need to do perfect stream checking */
431 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
432 if (timestamp != GST_CLOCK_TIME_NONE) {
433 /* check if we had a previous buffer to compare to */
434 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
435 identity->prev_duration != GST_CLOCK_TIME_NONE) {
436 guint64 offset, t_expected;
439 t_expected = identity->prev_timestamp + identity->prev_duration;
440 dt = timestamp - t_expected;
442 GST_WARNING_OBJECT (identity,
443 "Buffer not time-contiguous with previous one: " "prev ts %"
444 GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
445 GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
446 GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
447 GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
448 GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
449 GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
452 offset = GST_BUFFER_OFFSET (buf);
453 if (identity->prev_offset_end != offset &&
454 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
455 offset != GST_BUFFER_OFFSET_NONE) {
456 GST_WARNING_OBJECT (identity,
457 "Buffer not data-contiguous with previous one: "
458 "prev offset_end %" G_GINT64_FORMAT ", new offset %"
459 G_GINT64_FORMAT, identity->prev_offset_end, offset);
462 GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
463 "and/or duration were set on previous buffer");
469 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
471 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
473 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
474 if (timestamp != GST_CLOCK_TIME_NONE) {
475 /* check if we had a previous buffer to compare to */
476 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
477 identity->prev_duration != GST_CLOCK_TIME_NONE) {
478 GstClockTime t_expected;
481 t_expected = identity->prev_timestamp + identity->prev_duration;
482 dt = GST_CLOCK_DIFF (t_expected, timestamp);
485 * "imperfect-timestamp" bus message:
486 * @identity: the identity instance
487 * @prev-timestamp: the previous buffer timestamp
488 * @prev-duration: the previous buffer duration
489 * @prev-offset: the previous buffer offset
490 * @prev-offset-end: the previous buffer offset end
491 * @cur-timestamp: the current buffer timestamp
492 * @cur-duration: the current buffer duration
493 * @cur-offset: the current buffer offset
494 * @cur-offset-end: the current buffer offset end
496 * This bus message gets emitted if the check-imperfect-timestamp
497 * property is set and there is a gap in time between the
498 * last buffer and the newly received buffer.
500 gst_element_post_message (GST_ELEMENT (identity),
501 gst_message_new_element (GST_OBJECT (identity),
502 gst_structure_new ("imperfect-timestamp",
503 "prev-timestamp", G_TYPE_UINT64,
504 identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
505 identity->prev_duration, "prev-offset", G_TYPE_UINT64,
506 identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
507 identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
508 timestamp, "cur-duration", G_TYPE_UINT64,
509 GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
510 GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
511 GST_BUFFER_OFFSET_END (buf), NULL)));
514 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
515 "offset_end was set on previous buffer");
521 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
525 offset = GST_BUFFER_OFFSET (buf);
527 if (identity->prev_offset_end != offset &&
528 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
529 offset != GST_BUFFER_OFFSET_NONE) {
531 * "imperfect-offset" bus message:
532 * @identity: the identity instance
533 * @prev-timestamp: the previous buffer timestamp
534 * @prev-duration: the previous buffer duration
535 * @prev-offset: the previous buffer offset
536 * @prev-offset-end: the previous buffer offset end
537 * @cur-timestamp: the current buffer timestamp
538 * @cur-duration: the current buffer duration
539 * @cur-offset: the current buffer offset
540 * @cur-offset-end: the current buffer offset end
542 * This bus message gets emitted if the check-imperfect-offset
543 * property is set and there is a gap in offsets between the
544 * last buffer and the newly received buffer.
546 gst_element_post_message (GST_ELEMENT (identity),
547 gst_message_new_element (GST_OBJECT (identity),
548 gst_structure_new ("imperfect-offset", "prev-timestamp",
549 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
550 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
551 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
552 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
553 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
554 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
555 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
556 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
558 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
559 "and/or offset_end were set on previous buffer");
564 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
566 if (ts == GST_CLOCK_TIME_NONE)
569 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
574 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
575 const gchar * action, GstBuffer * buf, gsize size)
577 gchar ts_str[64], dur_str[64];
580 GST_OBJECT_LOCK (identity);
583 const char *flag_list[15] = {
584 "", "", "", "", "live", "decode-only", "discont", "resync", "corrupted",
585 "marker", "header", "gap", "droppable", "delta-unit", "in-caps"
588 char *end = flag_str;
590 for (i = 0; i < G_N_ELEMENTS (flag_list); i++) {
591 if (GST_MINI_OBJECT_CAST (buf)->flags & (1 << i)) {
592 strcpy (end, flag_list[i]);
601 g_free (identity->last_message);
602 identity->last_message = g_strdup_printf ("%s ******* (%s:%s) "
603 "(%" G_GSIZE_FORMAT " bytes, timestamp: %s, duration: %s, offset: %"
604 G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
605 ", flags: %d %s) %p", action,
606 GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
607 print_pretty_time (ts_str, sizeof (ts_str), GST_BUFFER_TIMESTAMP (buf)),
608 print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
609 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
610 GST_BUFFER_FLAGS (buf), flag_str, buf);
612 GST_OBJECT_UNLOCK (identity);
614 gst_identity_notify_last_message (identity);
618 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
620 GstFlowReturn ret = GST_FLOW_OK;
621 GstIdentity *identity = GST_IDENTITY (trans);
622 GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
626 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
628 if (identity->check_perfect)
629 gst_identity_check_perfect (identity, buf);
630 if (identity->check_imperfect_timestamp)
631 gst_identity_check_imperfect_timestamp (identity, buf);
632 if (identity->check_imperfect_offset)
633 gst_identity_check_imperfect_offset (identity, buf);
635 /* update prev values */
636 identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
637 identity->prev_duration = GST_BUFFER_DURATION (buf);
638 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
639 identity->prev_offset = GST_BUFFER_OFFSET (buf);
641 if (identity->error_after >= 0) {
642 identity->error_after--;
643 if (identity->error_after == 0)
647 if (identity->drop_probability > 0.0) {
648 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
652 if (identity->dump) {
653 gst_util_dump_mem (data, size);
656 if (!identity->silent) {
657 gst_identity_update_last_message_for_buffer (identity, "chain", buf, size);
660 if (identity->datarate > 0) {
661 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
662 GST_SECOND, identity->datarate);
664 GST_BUFFER_TIMESTAMP (buf) = time;
665 GST_BUFFER_DURATION (buf) = size * GST_SECOND / identity->datarate;
668 if (identity->signal_handoffs)
669 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
671 if (trans->segment.format == GST_FORMAT_TIME)
672 runtimestamp = gst_segment_to_running_time (&trans->segment,
673 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
675 if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
678 GST_OBJECT_LOCK (identity);
679 if ((clock = GST_ELEMENT (identity)->clock)) {
681 GstClockTime timestamp;
683 timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
685 /* save id if we need to unlock */
686 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
687 GST_OBJECT_UNLOCK (identity);
689 cret = gst_clock_id_wait (identity->clock_id, NULL);
691 GST_OBJECT_LOCK (identity);
692 if (identity->clock_id) {
693 gst_clock_id_unref (identity->clock_id);
694 identity->clock_id = NULL;
696 if (cret == GST_CLOCK_UNSCHEDULED)
699 GST_OBJECT_UNLOCK (identity);
702 identity->offset += size;
704 if (identity->sleep_time && ret == GST_FLOW_OK)
705 g_usleep (identity->sleep_time);
707 if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
708 && (ret == GST_FLOW_OK)) {
709 GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
710 GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
711 GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
714 gst_buffer_unmap (buf, data, size);
721 GST_ELEMENT_ERROR (identity, CORE, FAILED,
722 (_("Failed after iterations as requested.")), (NULL));
723 gst_buffer_unmap (buf, data, size);
724 return GST_FLOW_ERROR;
728 if (!identity->silent) {
729 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
732 gst_buffer_unmap (buf, data, size);
733 /* return DROPPED to basetransform. */
734 return GST_BASE_TRANSFORM_FLOW_DROPPED;
739 gst_identity_set_property (GObject * object, guint prop_id,
740 const GValue * value, GParamSpec * pspec)
742 GstIdentity *identity;
744 identity = GST_IDENTITY (object);
747 case PROP_SLEEP_TIME:
748 identity->sleep_time = g_value_get_uint (value);
751 identity->silent = g_value_get_boolean (value);
753 case PROP_SINGLE_SEGMENT:
754 identity->single_segment = g_value_get_boolean (value);
757 identity->dump = g_value_get_boolean (value);
759 case PROP_ERROR_AFTER:
760 identity->error_after = g_value_get_int (value);
762 case PROP_DROP_PROBABILITY:
763 identity->drop_probability = g_value_get_float (value);
766 identity->datarate = g_value_get_int (value);
769 identity->sync = g_value_get_boolean (value);
771 case PROP_CHECK_PERFECT:
772 identity->check_perfect = g_value_get_boolean (value);
774 case PROP_CHECK_IMPERFECT_TIMESTAMP:
775 identity->check_imperfect_timestamp = g_value_get_boolean (value);
777 case PROP_CHECK_IMPERFECT_OFFSET:
778 identity->check_imperfect_offset = g_value_get_boolean (value);
780 case PROP_SIGNAL_HANDOFFS:
781 identity->signal_handoffs = g_value_get_boolean (value);
784 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
790 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
793 GstIdentity *identity;
795 identity = GST_IDENTITY (object);
798 case PROP_SLEEP_TIME:
799 g_value_set_uint (value, identity->sleep_time);
801 case PROP_ERROR_AFTER:
802 g_value_set_int (value, identity->error_after);
804 case PROP_DROP_PROBABILITY:
805 g_value_set_float (value, identity->drop_probability);
808 g_value_set_int (value, identity->datarate);
811 g_value_set_boolean (value, identity->silent);
813 case PROP_SINGLE_SEGMENT:
814 g_value_set_boolean (value, identity->single_segment);
817 g_value_set_boolean (value, identity->dump);
819 case PROP_LAST_MESSAGE:
820 GST_OBJECT_LOCK (identity);
821 g_value_set_string (value, identity->last_message);
822 GST_OBJECT_UNLOCK (identity);
825 g_value_set_boolean (value, identity->sync);
827 case PROP_CHECK_PERFECT:
828 g_value_set_boolean (value, identity->check_perfect);
830 case PROP_CHECK_IMPERFECT_TIMESTAMP:
831 g_value_set_boolean (value, identity->check_imperfect_timestamp);
833 case PROP_CHECK_IMPERFECT_OFFSET:
834 g_value_set_boolean (value, identity->check_imperfect_offset);
836 case PROP_SIGNAL_HANDOFFS:
837 g_value_set_boolean (value, identity->signal_handoffs);
840 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
846 gst_identity_start (GstBaseTransform * trans)
848 GstIdentity *identity;
850 identity = GST_IDENTITY (trans);
852 identity->offset = 0;
853 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
854 identity->prev_duration = GST_CLOCK_TIME_NONE;
855 identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
856 identity->prev_offset = GST_BUFFER_OFFSET_NONE;
862 gst_identity_stop (GstBaseTransform * trans)
864 GstIdentity *identity;
866 identity = GST_IDENTITY (trans);
868 GST_OBJECT_LOCK (identity);
869 g_free (identity->last_message);
870 identity->last_message = NULL;
871 GST_OBJECT_UNLOCK (identity);
876 static GstStateChangeReturn
877 gst_identity_change_state (GstElement * element, GstStateChange transition)
879 GstStateChangeReturn ret;
880 GstIdentity *identity = GST_IDENTITY (element);
882 switch (transition) {
883 case GST_STATE_CHANGE_NULL_TO_READY:
885 case GST_STATE_CHANGE_READY_TO_PAUSED:
887 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
889 case GST_STATE_CHANGE_PAUSED_TO_READY:
890 GST_OBJECT_LOCK (identity);
891 if (identity->clock_id) {
892 GST_DEBUG_OBJECT (identity, "unlock clock wait");
893 gst_clock_id_unschedule (identity->clock_id);
894 gst_clock_id_unref (identity->clock_id);
895 identity->clock_id = NULL;
897 GST_OBJECT_UNLOCK (identity);
903 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
905 switch (transition) {
906 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
908 case GST_STATE_CHANGE_PAUSED_TO_READY:
910 case GST_STATE_CHANGE_READY_TO_NULL: