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 G_OBJECT_CLASS (parent_class)->finalize (object);
134 /* fixme: do something about this */
136 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
137 guint n_param_values, const GValue * param_values, gpointer invocation_hint,
138 gpointer marshal_data)
140 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
142 register marshalfunc_VOID__MINIOBJECT callback;
143 register GCClosure *cc = (GCClosure *) closure;
144 register gpointer data1, data2;
146 g_return_if_fail (n_param_values == 2);
148 if (G_CCLOSURE_SWAP_DATA (closure)) {
149 data1 = closure->data;
150 data2 = g_value_peek_pointer (param_values + 0);
152 data1 = g_value_peek_pointer (param_values + 0);
153 data2 = closure->data;
156 (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data :
159 callback (data1, g_value_get_boxed (param_values + 1), data2);
163 gst_identity_class_init (GstIdentityClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167 GstBaseTransformClass *gstbasetrans_class;
169 gobject_class = G_OBJECT_CLASS (klass);
170 gstelement_class = GST_ELEMENT_CLASS (klass);
171 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
173 gobject_class->set_property = gst_identity_set_property;
174 gobject_class->get_property = gst_identity_get_property;
176 g_object_class_install_property (gobject_class, PROP_SLEEP_TIME,
177 g_param_spec_uint ("sleep-time", "Sleep time",
178 "Microseconds to sleep between processing", 0, G_MAXUINT,
179 DEFAULT_SLEEP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_class, PROP_ERROR_AFTER,
181 g_param_spec_int ("error-after", "Error After", "Error after N buffers",
182 G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class, PROP_DROP_PROBABILITY,
185 g_param_spec_float ("drop-probability", "Drop Probability",
186 "The Probability a buffer is dropped", 0.0, 1.0,
187 DEFAULT_DROP_PROBABILITY,
188 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class, PROP_DATARATE,
190 g_param_spec_int ("datarate", "Datarate",
191 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
192 0, G_MAXINT, DEFAULT_DATARATE,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (gobject_class, PROP_SILENT,
195 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
198 g_param_spec_boolean ("single-segment", "Single Segment",
199 "Timestamp buffers and eat segments so as to appear as one segment",
200 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 pspec_last_message = g_param_spec_string ("last-message", "last-message",
202 "last-message", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
203 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
205 g_object_class_install_property (gobject_class, PROP_DUMP,
206 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
207 DEFAULT_DUMP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_SYNC,
209 g_param_spec_boolean ("sync", "Synchronize",
210 "Synchronize to pipeline clock", DEFAULT_SYNC,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_CHECK_PERFECT,
213 g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
214 "Verify that the stream is time- and data-contiguous. "
215 "This only logs in the debug log. This will be deprecated in favor "
216 "of the check-imperfect-timestamp/offset properties.",
217 DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 g_object_class_install_property (gobject_class,
219 PROP_CHECK_IMPERFECT_TIMESTAMP,
220 g_param_spec_boolean ("check-imperfect-timestamp",
221 "Check for discontiguous timestamps",
222 "Send element messages if timestamps and durations do not match up",
223 DEFAULT_CHECK_IMPERFECT_TIMESTAMP,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (gobject_class, PROP_CHECK_IMPERFECT_OFFSET,
226 g_param_spec_boolean ("check-imperfect-offset",
227 "Check for discontiguous offset",
228 "Send element messages if offset and offset_end do not match up",
229 DEFAULT_CHECK_IMPERFECT_OFFSET,
230 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 * GstIdentity:signal-handoffs
235 * If set to #TRUE, the identity will emit a handoff signal when handling a buffer.
236 * When set to #FALSE, no signal will be emited, which might improve performance.
240 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
241 g_param_spec_boolean ("signal-handoffs",
242 "Signal handoffs", "Send a signal before pushing the buffer",
243 DEFAULT_SIGNAL_HANDOFFS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 * GstIdentity::handoff:
247 * @identity: the identity instance
248 * @buffer: the buffer that just has been received
249 * @pad: the pad that received it
251 * This signal gets emitted before passing the buffer downstream.
253 gst_identity_signals[SIGNAL_HANDOFF] =
254 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
255 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
256 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
258 gobject_class->finalize = gst_identity_finalize;
260 gst_element_class_set_details_simple (gstelement_class,
263 "Pass data without modification", "Erik Walthinsen <omega@cse.ogi.edu>");
264 gst_element_class_add_pad_template (gstelement_class,
265 gst_static_pad_template_get (&srctemplate));
266 gst_element_class_add_pad_template (gstelement_class,
267 gst_static_pad_template_get (&sinktemplate));
269 gstelement_class->change_state =
270 GST_DEBUG_FUNCPTR (gst_identity_change_state);
271 gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_identity_sink_event);
272 gstbasetrans_class->transform_ip =
273 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
274 gstbasetrans_class->prepare_output_buffer =
275 GST_DEBUG_FUNCPTR (gst_identity_prepare_output_buffer);
276 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
277 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
281 gst_identity_init (GstIdentity * identity)
283 identity->sleep_time = DEFAULT_SLEEP_TIME;
284 identity->error_after = DEFAULT_ERROR_AFTER;
285 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
286 identity->datarate = DEFAULT_DATARATE;
287 identity->silent = DEFAULT_SILENT;
288 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
289 identity->sync = DEFAULT_SYNC;
290 identity->check_perfect = DEFAULT_CHECK_PERFECT;
291 identity->check_imperfect_timestamp = DEFAULT_CHECK_IMPERFECT_TIMESTAMP;
292 identity->check_imperfect_offset = DEFAULT_CHECK_IMPERFECT_OFFSET;
293 identity->dump = DEFAULT_DUMP;
294 identity->last_message = NULL;
295 identity->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
297 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM_CAST (identity), TRUE);
301 gst_identity_notify_last_message (GstIdentity * identity)
303 g_object_notify_by_pspec ((GObject *) identity, pspec_last_message);
307 gst_identity_sink_event (GstBaseTransform * trans, GstEvent * event)
309 GstIdentity *identity;
312 identity = GST_IDENTITY (trans);
314 if (!identity->silent) {
315 const GstStructure *s;
318 GST_OBJECT_LOCK (identity);
319 g_free (identity->last_message);
321 if ((s = gst_event_get_structure (event)))
322 sstr = gst_structure_to_string (s);
324 sstr = g_strdup ("");
326 identity->last_message =
327 g_strdup_printf ("event ******* (%s:%s) E (type: %d, %s) %p",
328 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
331 GST_OBJECT_UNLOCK (identity);
333 gst_identity_notify_last_message (identity);
336 if (identity->single_segment && (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
337 if (trans->have_segment == FALSE) {
341 gst_event_copy_segment (event, &segment);
342 gst_event_copy_segment (event, &trans->segment);
343 trans->have_segment = TRUE;
345 /* This is the first segment, send out a (0, -1) segment */
346 gst_segment_init (&segment, segment.format);
347 news = gst_event_new_segment (&segment);
349 gst_pad_event_default (trans->sinkpad, GST_OBJECT_CAST (trans), news);
353 /* Reset previous timestamp, duration and offsets on NEWSEGMENT
354 * to prevent false warnings when checking for perfect streams */
355 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
356 identity->prev_timestamp = identity->prev_duration = GST_CLOCK_TIME_NONE;
357 identity->prev_offset = identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
360 if (identity->single_segment && GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
361 /* eat up segments */
362 gst_event_unref (event);
365 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
366 GST_OBJECT_LOCK (identity);
367 if (identity->clock_id) {
368 GST_DEBUG_OBJECT (identity, "unlock clock wait");
369 gst_clock_id_unschedule (identity->clock_id);
370 gst_clock_id_unref (identity->clock_id);
371 identity->clock_id = NULL;
373 GST_OBJECT_UNLOCK (identity);
376 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
383 gst_identity_prepare_output_buffer (GstBaseTransform * trans,
384 GstBuffer * in_buf, GstBuffer ** out_buf)
386 GstIdentity *identity = GST_IDENTITY (trans);
388 /* only bother if we may have to alter metadata */
389 if (identity->datarate > 0 || identity->single_segment) {
390 if (gst_buffer_is_writable (in_buf))
395 *out_buf = gst_buffer_copy (in_buf);
404 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
406 GstClockTime timestamp;
408 timestamp = GST_BUFFER_TIMESTAMP (buf);
410 /* see if we need to do perfect stream checking */
411 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
412 if (timestamp != GST_CLOCK_TIME_NONE) {
413 /* check if we had a previous buffer to compare to */
414 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
415 identity->prev_duration != GST_CLOCK_TIME_NONE) {
416 guint64 offset, t_expected;
419 t_expected = identity->prev_timestamp + identity->prev_duration;
420 dt = timestamp - t_expected;
422 GST_WARNING_OBJECT (identity,
423 "Buffer not time-contiguous with previous one: " "prev ts %"
424 GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
425 GST_TIME_FORMAT " (expected ts %" GST_TIME_FORMAT ", delta=%c%"
426 GST_TIME_FORMAT ")", GST_TIME_ARGS (identity->prev_timestamp),
427 GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp),
428 GST_TIME_ARGS (t_expected), (dt < 0) ? '-' : '+',
429 GST_TIME_ARGS ((dt < 0) ? (GstClockTime) (-dt) : dt));
432 offset = GST_BUFFER_OFFSET (buf);
433 if (identity->prev_offset_end != offset &&
434 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
435 offset != GST_BUFFER_OFFSET_NONE) {
436 GST_WARNING_OBJECT (identity,
437 "Buffer not data-contiguous with previous one: "
438 "prev offset_end %" G_GINT64_FORMAT ", new offset %"
439 G_GINT64_FORMAT, identity->prev_offset_end, offset);
442 GST_DEBUG_OBJECT (identity, "can't check time-contiguity, no timestamp "
443 "and/or duration were set on previous buffer");
449 gst_identity_check_imperfect_timestamp (GstIdentity * identity, GstBuffer * buf)
451 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
453 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
454 if (timestamp != GST_CLOCK_TIME_NONE) {
455 /* check if we had a previous buffer to compare to */
456 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE &&
457 identity->prev_duration != GST_CLOCK_TIME_NONE) {
458 GstClockTime t_expected;
461 t_expected = identity->prev_timestamp + identity->prev_duration;
462 dt = GST_CLOCK_DIFF (t_expected, timestamp);
465 * "imperfect-timestamp" bus message:
466 * @identity: the identity instance
467 * @prev-timestamp: the previous buffer timestamp
468 * @prev-duration: the previous buffer duration
469 * @prev-offset: the previous buffer offset
470 * @prev-offset-end: the previous buffer offset end
471 * @cur-timestamp: the current buffer timestamp
472 * @cur-duration: the current buffer duration
473 * @cur-offset: the current buffer offset
474 * @cur-offset-end: the current buffer offset end
476 * This bus message gets emitted if the check-imperfect-timestamp
477 * property is set and there is a gap in time between the
478 * last buffer and the newly received buffer.
480 gst_element_post_message (GST_ELEMENT (identity),
481 gst_message_new_element (GST_OBJECT (identity),
482 gst_structure_new ("imperfect-timestamp",
483 "prev-timestamp", G_TYPE_UINT64,
484 identity->prev_timestamp, "prev-duration", G_TYPE_UINT64,
485 identity->prev_duration, "prev-offset", G_TYPE_UINT64,
486 identity->prev_offset, "prev-offset-end", G_TYPE_UINT64,
487 identity->prev_offset_end, "cur-timestamp", G_TYPE_UINT64,
488 timestamp, "cur-duration", G_TYPE_UINT64,
489 GST_BUFFER_DURATION (buf), "cur-offset", G_TYPE_UINT64,
490 GST_BUFFER_OFFSET (buf), "cur-offset-end", G_TYPE_UINT64,
491 GST_BUFFER_OFFSET_END (buf), NULL)));
494 GST_DEBUG_OBJECT (identity, "can't check data-contiguity, no "
495 "offset_end was set on previous buffer");
501 gst_identity_check_imperfect_offset (GstIdentity * identity, GstBuffer * buf)
505 offset = GST_BUFFER_OFFSET (buf);
507 if (identity->prev_offset_end != offset &&
508 identity->prev_offset_end != GST_BUFFER_OFFSET_NONE &&
509 offset != GST_BUFFER_OFFSET_NONE) {
511 * "imperfect-offset" bus message:
512 * @identity: the identity instance
513 * @prev-timestamp: the previous buffer timestamp
514 * @prev-duration: the previous buffer duration
515 * @prev-offset: the previous buffer offset
516 * @prev-offset-end: the previous buffer offset end
517 * @cur-timestamp: the current buffer timestamp
518 * @cur-duration: the current buffer duration
519 * @cur-offset: the current buffer offset
520 * @cur-offset-end: the current buffer offset end
522 * This bus message gets emitted if the check-imperfect-offset
523 * property is set and there is a gap in offsets between the
524 * last buffer and the newly received buffer.
526 gst_element_post_message (GST_ELEMENT (identity),
527 gst_message_new_element (GST_OBJECT (identity),
528 gst_structure_new ("imperfect-offset", "prev-timestamp",
529 G_TYPE_UINT64, identity->prev_timestamp, "prev-duration",
530 G_TYPE_UINT64, identity->prev_duration, "prev-offset",
531 G_TYPE_UINT64, identity->prev_offset, "prev-offset-end",
532 G_TYPE_UINT64, identity->prev_offset_end, "cur-timestamp",
533 G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (buf), "cur-duration",
534 G_TYPE_UINT64, GST_BUFFER_DURATION (buf), "cur-offset",
535 G_TYPE_UINT64, GST_BUFFER_OFFSET (buf), "cur-offset-end",
536 G_TYPE_UINT64, GST_BUFFER_OFFSET_END (buf), NULL)));
538 GST_DEBUG_OBJECT (identity, "can't check offset contiguity, no offset "
539 "and/or offset_end were set on previous buffer");
544 print_pretty_time (gchar * ts_str, gsize ts_str_len, GstClockTime ts)
546 if (ts == GST_CLOCK_TIME_NONE)
549 g_snprintf (ts_str, ts_str_len, "%" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
554 gst_identity_update_last_message_for_buffer (GstIdentity * identity,
555 const gchar * action, GstBuffer * buf, gsize size)
557 gchar ts_str[64], dur_str[64];
560 GST_OBJECT_LOCK (identity);
563 const char *flag_list[15] = {
564 "", "", "", "", "live", "decode-only", "discont", "resync", "corrupted",
565 "marker", "header", "gap", "droppable", "delta-unit", "in-caps"
568 char *end = flag_str;
570 for (i = 0; i < G_N_ELEMENTS (flag_list); i++) {
571 if (GST_MINI_OBJECT_CAST (buf)->flags & (1 << i)) {
572 strcpy (end, flag_list[i]);
581 g_free (identity->last_message);
582 identity->last_message = g_strdup_printf ("%s ******* (%s:%s) "
583 "(%" G_GSIZE_FORMAT " bytes, timestamp: %s, duration: %s, offset: %"
584 G_GINT64_FORMAT ", " "offset_end: % " G_GINT64_FORMAT
585 ", flags: %d %s) %p", action,
586 GST_DEBUG_PAD_NAME (GST_BASE_TRANSFORM_CAST (identity)->sinkpad), size,
587 print_pretty_time (ts_str, sizeof (ts_str), GST_BUFFER_TIMESTAMP (buf)),
588 print_pretty_time (dur_str, sizeof (dur_str), GST_BUFFER_DURATION (buf)),
589 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
590 GST_BUFFER_FLAGS (buf), flag_str, buf);
592 GST_OBJECT_UNLOCK (identity);
594 gst_identity_notify_last_message (identity);
598 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
600 GstFlowReturn ret = GST_FLOW_OK;
601 GstIdentity *identity = GST_IDENTITY (trans);
602 GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
605 gst_buffer_map (buf, &info, GST_MAP_READ);
607 if (identity->check_perfect)
608 gst_identity_check_perfect (identity, buf);
609 if (identity->check_imperfect_timestamp)
610 gst_identity_check_imperfect_timestamp (identity, buf);
611 if (identity->check_imperfect_offset)
612 gst_identity_check_imperfect_offset (identity, buf);
614 /* update prev values */
615 identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
616 identity->prev_duration = GST_BUFFER_DURATION (buf);
617 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
618 identity->prev_offset = GST_BUFFER_OFFSET (buf);
620 if (identity->error_after >= 0) {
621 identity->error_after--;
622 if (identity->error_after == 0)
626 if (identity->drop_probability > 0.0) {
627 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability)
631 if (identity->dump) {
632 gst_util_dump_mem (info.data, info.size);
635 if (!identity->silent) {
636 gst_identity_update_last_message_for_buffer (identity, "chain", buf,
640 if (identity->datarate > 0) {
641 GstClockTime time = gst_util_uint64_scale_int (identity->offset,
642 GST_SECOND, identity->datarate);
644 GST_BUFFER_TIMESTAMP (buf) = time;
645 GST_BUFFER_DURATION (buf) = info.size * GST_SECOND / identity->datarate;
648 if (identity->signal_handoffs)
649 g_signal_emit (identity, gst_identity_signals[SIGNAL_HANDOFF], 0, buf);
651 if (trans->segment.format == GST_FORMAT_TIME)
652 runtimestamp = gst_segment_to_running_time (&trans->segment,
653 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
655 if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
658 GST_OBJECT_LOCK (identity);
659 if ((clock = GST_ELEMENT (identity)->clock)) {
661 GstClockTime timestamp;
663 timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
665 /* save id if we need to unlock */
666 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
667 GST_OBJECT_UNLOCK (identity);
669 cret = gst_clock_id_wait (identity->clock_id, NULL);
671 GST_OBJECT_LOCK (identity);
672 if (identity->clock_id) {
673 gst_clock_id_unref (identity->clock_id);
674 identity->clock_id = NULL;
676 if (cret == GST_CLOCK_UNSCHEDULED)
679 GST_OBJECT_UNLOCK (identity);
682 identity->offset += info.size;
684 if (identity->sleep_time && ret == GST_FLOW_OK)
685 g_usleep (identity->sleep_time);
687 if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
688 && (ret == GST_FLOW_OK)) {
689 GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
690 GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
691 GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
694 gst_buffer_unmap (buf, &info);
701 GST_ELEMENT_ERROR (identity, CORE, FAILED,
702 (_("Failed after iterations as requested.")), (NULL));
703 gst_buffer_unmap (buf, &info);
704 return GST_FLOW_ERROR;
708 if (!identity->silent) {
709 gst_identity_update_last_message_for_buffer (identity, "dropping", buf,
712 gst_buffer_unmap (buf, &info);
713 /* return DROPPED to basetransform. */
714 return GST_BASE_TRANSFORM_FLOW_DROPPED;
719 gst_identity_set_property (GObject * object, guint prop_id,
720 const GValue * value, GParamSpec * pspec)
722 GstIdentity *identity;
724 identity = GST_IDENTITY (object);
727 case PROP_SLEEP_TIME:
728 identity->sleep_time = g_value_get_uint (value);
731 identity->silent = g_value_get_boolean (value);
733 case PROP_SINGLE_SEGMENT:
734 identity->single_segment = g_value_get_boolean (value);
737 identity->dump = g_value_get_boolean (value);
739 case PROP_ERROR_AFTER:
740 identity->error_after = g_value_get_int (value);
742 case PROP_DROP_PROBABILITY:
743 identity->drop_probability = g_value_get_float (value);
746 identity->datarate = g_value_get_int (value);
749 identity->sync = g_value_get_boolean (value);
751 case PROP_CHECK_PERFECT:
752 identity->check_perfect = g_value_get_boolean (value);
754 case PROP_CHECK_IMPERFECT_TIMESTAMP:
755 identity->check_imperfect_timestamp = g_value_get_boolean (value);
757 case PROP_CHECK_IMPERFECT_OFFSET:
758 identity->check_imperfect_offset = g_value_get_boolean (value);
760 case PROP_SIGNAL_HANDOFFS:
761 identity->signal_handoffs = g_value_get_boolean (value);
764 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
770 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
773 GstIdentity *identity;
775 identity = GST_IDENTITY (object);
778 case PROP_SLEEP_TIME:
779 g_value_set_uint (value, identity->sleep_time);
781 case PROP_ERROR_AFTER:
782 g_value_set_int (value, identity->error_after);
784 case PROP_DROP_PROBABILITY:
785 g_value_set_float (value, identity->drop_probability);
788 g_value_set_int (value, identity->datarate);
791 g_value_set_boolean (value, identity->silent);
793 case PROP_SINGLE_SEGMENT:
794 g_value_set_boolean (value, identity->single_segment);
797 g_value_set_boolean (value, identity->dump);
799 case PROP_LAST_MESSAGE:
800 GST_OBJECT_LOCK (identity);
801 g_value_set_string (value, identity->last_message);
802 GST_OBJECT_UNLOCK (identity);
805 g_value_set_boolean (value, identity->sync);
807 case PROP_CHECK_PERFECT:
808 g_value_set_boolean (value, identity->check_perfect);
810 case PROP_CHECK_IMPERFECT_TIMESTAMP:
811 g_value_set_boolean (value, identity->check_imperfect_timestamp);
813 case PROP_CHECK_IMPERFECT_OFFSET:
814 g_value_set_boolean (value, identity->check_imperfect_offset);
816 case PROP_SIGNAL_HANDOFFS:
817 g_value_set_boolean (value, identity->signal_handoffs);
820 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
826 gst_identity_start (GstBaseTransform * trans)
828 GstIdentity *identity;
830 identity = GST_IDENTITY (trans);
832 identity->offset = 0;
833 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
834 identity->prev_duration = GST_CLOCK_TIME_NONE;
835 identity->prev_offset_end = GST_BUFFER_OFFSET_NONE;
836 identity->prev_offset = GST_BUFFER_OFFSET_NONE;
842 gst_identity_stop (GstBaseTransform * trans)
844 GstIdentity *identity;
846 identity = GST_IDENTITY (trans);
848 GST_OBJECT_LOCK (identity);
849 g_free (identity->last_message);
850 identity->last_message = NULL;
851 GST_OBJECT_UNLOCK (identity);
856 static GstStateChangeReturn
857 gst_identity_change_state (GstElement * element, GstStateChange transition)
859 GstStateChangeReturn ret;
860 GstIdentity *identity = GST_IDENTITY (element);
862 switch (transition) {
863 case GST_STATE_CHANGE_NULL_TO_READY:
865 case GST_STATE_CHANGE_READY_TO_PAUSED:
867 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
869 case GST_STATE_CHANGE_PAUSED_TO_READY:
870 GST_OBJECT_LOCK (identity);
871 if (identity->clock_id) {
872 GST_DEBUG_OBJECT (identity, "unlock clock wait");
873 gst_clock_id_unschedule (identity->clock_id);
874 gst_clock_id_unref (identity->clock_id);
875 identity->clock_id = NULL;
877 GST_OBJECT_UNLOCK (identity);
883 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
885 switch (transition) {
886 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
888 case GST_STATE_CHANGE_PAUSED_TO_READY:
890 case GST_STATE_CHANGE_READY_TO_NULL: