1 /* GStreamer
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>
5 *
6 * gstidentity.c:
7 *
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.
12 *
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.
17 *
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.
22 */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <stdlib.h>
31 #include "../../gst/gst-i18n-lib.h"
32 #include "gstidentity.h"
33 #include <gst/gstmarshal.h>
35 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
36 GST_PAD_SINK,
37 GST_PAD_ALWAYS,
38 GST_STATIC_CAPS_ANY);
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41 GST_PAD_SRC,
42 GST_PAD_ALWAYS,
43 GST_STATIC_CAPS_ANY);
45 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
46 #define GST_CAT_DEFAULT gst_identity_debug
48 static const GstElementDetails gst_identity_details =
49 GST_ELEMENT_DETAILS ("Identity",
50 "Generic",
51 "Pass data without modification",
52 "Erik Walthinsen <omega@cse.ogi.edu>");
55 /* Identity signals and args */
56 enum
57 {
58 SIGNAL_HANDOFF,
59 /* FILL ME */
60 LAST_SIGNAL
61 };
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_DATARATE 0
68 #define DEFAULT_SILENT FALSE
69 #define DEFAULT_SINGLE_SEGMENT FALSE
70 #define DEFAULT_DUMP FALSE
71 #define DEFAULT_SYNC FALSE
72 #define DEFAULT_CHECK_PERFECT FALSE
74 enum
75 {
76 PROP_0,
77 PROP_SLEEP_TIME,
78 PROP_ERROR_AFTER,
79 PROP_DROP_PROBABILITY,
80 PROP_DATARATE,
81 PROP_SILENT,
82 PROP_SINGLE_SEGMENT,
83 PROP_LAST_MESSAGE,
84 PROP_DUMP,
85 PROP_SYNC,
86 PROP_CHECK_PERFECT
87 };
90 #define _do_init(bla) \
91 GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
93 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstBaseTransform,
94 GST_TYPE_BASE_TRANSFORM, _do_init);
96 static void gst_identity_finalize (GObject * object);
97 static void gst_identity_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void gst_identity_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
102 static gboolean gst_identity_event (GstBaseTransform * trans, GstEvent * event);
103 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
104 GstBuffer * buf);
105 static gboolean gst_identity_start (GstBaseTransform * trans);
106 static gboolean gst_identity_stop (GstBaseTransform * trans);
108 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
110 static void
111 gst_identity_base_init (gpointer g_class)
112 {
113 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
115 gst_element_class_add_pad_template (gstelement_class,
116 gst_static_pad_template_get (&srctemplate));
117 gst_element_class_add_pad_template (gstelement_class,
118 gst_static_pad_template_get (&sinktemplate));
119 gst_element_class_set_details (gstelement_class, &gst_identity_details);
120 }
122 static void
123 gst_identity_finalize (GObject * object)
124 {
125 GstIdentity *identity;
127 identity = GST_IDENTITY (object);
129 g_free (identity->last_message);
131 G_OBJECT_CLASS (parent_class)->finalize (object);
132 }
134 /* fixme: do something about this */
135 static void
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)
139 {
140 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
141 gpointer data2);
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);
151 } else {
152 data1 = g_value_peek_pointer (param_values + 0);
153 data2 = closure->data;
154 }
155 callback =
156 (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->
157 callback);
159 callback (data1, gst_value_get_mini_object (param_values + 1), data2);
160 }
162 static void
163 gst_identity_class_init (GstIdentityClass * klass)
164 {
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_DEBUG_FUNCPTR (gst_identity_set_property);
174 gobject_class->get_property = GST_DEBUG_FUNCPTR (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));
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, G_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class,
184 PROP_DROP_PROBABILITY, g_param_spec_float ("drop_probability",
185 "Drop Probability",
186 "The Probability a buffer is dropped (not implemented)", 0.0, 1.0,
187 DEFAULT_DROP_PROBABILITY, G_PARAM_READWRITE));
188 g_object_class_install_property (gobject_class, PROP_DATARATE,
189 g_param_spec_int ("datarate", "Datarate",
190 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
191 0, G_MAXINT, DEFAULT_DATARATE, G_PARAM_READWRITE));
192 g_object_class_install_property (gobject_class, PROP_SILENT,
193 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
194 G_PARAM_READWRITE));
195 g_object_class_install_property (gobject_class, PROP_SINGLE_SEGMENT,
196 g_param_spec_boolean ("single-segment", "Single Segment",
197 "Timestamp buffers and eat newsegments so as to appear as one segment",
198 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE));
199 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
200 g_param_spec_string ("last-message", "last-message", "last-message", NULL,
201 G_PARAM_READABLE));
202 g_object_class_install_property (gobject_class, PROP_DUMP,
203 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents",
204 DEFAULT_DUMP, G_PARAM_READWRITE));
205 g_object_class_install_property (gobject_class, PROP_SYNC,
206 g_param_spec_boolean ("sync", "Synchronize",
207 "Synchronize to pipeline clock", DEFAULT_SYNC, G_PARAM_READWRITE));
208 g_object_class_install_property (gobject_class, PROP_CHECK_PERFECT,
209 g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
210 "Verify that the stream is time- and data-contiguous",
211 DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE));
213 /**
214 * GstIdentity::handoff:
215 * @identity: the identity instance
216 * @buffer: the buffer that just has been received
217 * @pad: the pad that received it
218 *
219 * This signal gets emitted before passing the buffer downstream.
220 */
221 gst_identity_signals[SIGNAL_HANDOFF] =
222 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
223 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
224 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
226 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_identity_finalize);
228 gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_identity_event);
229 gstbasetrans_class->transform_ip =
230 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
231 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
232 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
233 }
235 static void
236 gst_identity_init (GstIdentity * identity, GstIdentityClass * g_class)
237 {
238 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
240 identity->sleep_time = DEFAULT_SLEEP_TIME;
241 identity->error_after = DEFAULT_ERROR_AFTER;
242 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
243 identity->datarate = DEFAULT_DATARATE;
244 identity->silent = DEFAULT_SILENT;
245 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
246 identity->sync = DEFAULT_SYNC;
247 identity->check_perfect = DEFAULT_CHECK_PERFECT;
248 identity->dump = DEFAULT_DUMP;
249 identity->last_message = NULL;
250 }
252 static gboolean
253 gst_identity_event (GstBaseTransform * trans, GstEvent * event)
254 {
255 GstIdentity *identity;
256 gboolean ret = TRUE;
258 identity = GST_IDENTITY (trans);
260 if (!identity->silent) {
261 const GstStructure *s;
262 gchar *sstr;
264 GST_OBJECT_LOCK (identity);
265 g_free (identity->last_message);
267 if ((s = gst_event_get_structure (event)))
268 sstr = gst_structure_to_string (s);
269 else
270 sstr = g_strdup ("");
272 identity->last_message =
273 g_strdup_printf ("event ******* (%s:%s) E (type: %d, %s) %p",
274 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
275 event);
276 g_free (sstr);
277 GST_OBJECT_UNLOCK (identity);
279 g_object_notify (G_OBJECT (identity), "last_message");
280 }
282 if (identity->single_segment
283 && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
284 if (trans->have_newsegment == FALSE) {
285 GstEvent *news;
286 GstFormat format;
288 gst_event_parse_new_segment (event, NULL, NULL, &format, NULL, NULL,
289 NULL);
291 /* This is the first newsegment, send out a (0, -1) newsegment */
292 news = gst_event_new_new_segment (TRUE, 1.0, format, 0, -1, 0);
294 if (!(gst_pad_event_default (trans->sinkpad, news)))
295 return FALSE;
296 }
297 }
299 GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
301 if (identity->single_segment
302 && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
303 /* eat up segments */
304 ret = FALSE;
305 }
307 return ret;
308 }
310 static void
311 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
312 {
313 GstClockTime timestamp;
315 timestamp = GST_BUFFER_TIMESTAMP (buf);
317 /* see if we need to do perfect stream checking */
318 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
319 if (timestamp != GST_CLOCK_TIME_NONE) {
320 /* check if we had a previous buffer to compare to */
321 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE) {
322 guint64 offset;
324 if (identity->prev_timestamp + identity->prev_duration != timestamp) {
325 GST_WARNING_OBJECT (identity,
326 "Buffer not time-contiguous with previous one: " "prev ts %"
327 GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
328 GST_TIME_FORMAT, GST_TIME_ARGS (identity->prev_timestamp),
329 GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp));
330 }
332 offset = GST_BUFFER_OFFSET (buf);
333 if (identity->prev_offset_end != offset) {
334 GST_WARNING_OBJECT (identity,
335 "Buffer not data-contiguous with previous one: "
336 "prev offset_end %" G_GINT64_FORMAT ", new offset %"
337 G_GINT64_FORMAT, identity->prev_offset_end, offset);
338 }
339 }
340 /* update prev values */
341 identity->prev_timestamp = timestamp;
342 identity->prev_duration = GST_BUFFER_DURATION (buf);
343 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
344 }
345 }
347 static GstFlowReturn
348 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
349 {
350 GstFlowReturn ret = GST_FLOW_OK;
351 GstIdentity *identity = GST_IDENTITY (trans);
352 GstClockTime runtimestamp = G_GINT64_CONSTANT (0);
354 if (identity->check_perfect)
355 gst_identity_check_perfect (identity, buf);
357 if (identity->error_after >= 0) {
358 identity->error_after--;
359 if (identity->error_after == 0) {
360 GST_ELEMENT_ERROR (identity, CORE, FAILED,
361 (_("Failed after iterations as requested.")), (NULL));
362 return GST_FLOW_ERROR;
363 }
364 }
366 if (identity->drop_probability > 0.0) {
367 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability) {
368 if (!identity->silent) {
369 GST_OBJECT_LOCK (identity);
370 g_free (identity->last_message);
371 identity->last_message =
372 g_strdup_printf
373 ("dropping ******* (%s:%s)i (%d bytes, timestamp: %"
374 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
375 G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT
376 ", flags: %d) %p", GST_DEBUG_PAD_NAME (trans->sinkpad),
377 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
378 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
379 GST_BUFFER_OFFSET_END (buf), GST_BUFFER_FLAGS (buf), buf);
380 GST_OBJECT_UNLOCK (identity);
381 g_object_notify (G_OBJECT (identity), "last-message");
382 }
383 /* FIXME, this does not drop the buffer in basetransform. Actually
384 * dropping the buffer in transform_ip is not possible without a new
385 * custom GstFlowReturn value. */
386 return GST_FLOW_OK;
387 }
388 }
390 if (identity->dump) {
391 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
392 }
394 if (!identity->silent) {
395 GST_OBJECT_LOCK (identity);
396 g_free (identity->last_message);
397 identity->last_message =
398 g_strdup_printf ("chain ******* (%s:%s)i (%d bytes, timestamp: %"
399 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
400 G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
401 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_BUFFER_SIZE (buf),
402 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
403 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
404 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
405 GST_BUFFER_FLAGS (buf), buf);
406 GST_OBJECT_UNLOCK (identity);
407 g_object_notify (G_OBJECT (identity), "last-message");
408 }
410 if (identity->datarate > 0) {
411 GstClockTime time = identity->offset * GST_SECOND / identity->datarate;
413 GST_BUFFER_TIMESTAMP (buf) = time;
414 GST_BUFFER_DURATION (buf) =
415 GST_BUFFER_SIZE (buf) * GST_SECOND / identity->datarate;
416 }
418 g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
419 buf);
421 if (trans->segment.format == GST_FORMAT_TIME)
422 runtimestamp = gst_segment_to_running_time (&trans->segment,
423 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
425 if ((identity->sync) && (trans->segment.format == GST_FORMAT_TIME)) {
426 GstClock *clock;
428 GST_OBJECT_LOCK (identity);
429 if ((clock = GST_ELEMENT (identity)->clock)) {
430 GstClockReturn cret;
431 GstClockTime timestamp;
433 timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
435 /* save id if we need to unlock */
436 /* FIXME: actually unlock this somewhere in the state changes */
437 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
438 GST_OBJECT_UNLOCK (identity);
440 cret = gst_clock_id_wait (identity->clock_id, NULL);
442 GST_OBJECT_LOCK (identity);
443 if (identity->clock_id) {
444 gst_clock_id_unref (identity->clock_id);
445 identity->clock_id = NULL;
446 }
447 if (cret == GST_CLOCK_UNSCHEDULED)
448 ret = GST_FLOW_UNEXPECTED;
449 }
450 GST_OBJECT_UNLOCK (identity);
451 }
453 identity->offset += GST_BUFFER_SIZE (buf);
455 if (identity->sleep_time && ret == GST_FLOW_OK)
456 g_usleep (identity->sleep_time);
458 if (identity->single_segment && (trans->segment.format == GST_FORMAT_TIME)
459 && (ret == GST_FLOW_OK)) {
460 GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
461 GST_BUFFER_OFFSET (buf) = GST_CLOCK_TIME_NONE;
462 GST_BUFFER_OFFSET_END (buf) = GST_CLOCK_TIME_NONE;
463 }
465 return ret;
466 }
468 static void
469 gst_identity_set_property (GObject * object, guint prop_id,
470 const GValue * value, GParamSpec * pspec)
471 {
472 GstIdentity *identity;
474 identity = GST_IDENTITY (object);
476 switch (prop_id) {
477 case PROP_SLEEP_TIME:
478 identity->sleep_time = g_value_get_uint (value);
479 break;
480 case PROP_SILENT:
481 identity->silent = g_value_get_boolean (value);
482 break;
483 case PROP_SINGLE_SEGMENT:
484 identity->single_segment = g_value_get_boolean (value);
485 break;
486 case PROP_DUMP:
487 identity->dump = g_value_get_boolean (value);
488 break;
489 case PROP_ERROR_AFTER:
490 identity->error_after = g_value_get_int (value);
491 break;
492 case PROP_DROP_PROBABILITY:
493 identity->drop_probability = g_value_get_float (value);
494 break;
495 case PROP_DATARATE:
496 identity->datarate = g_value_get_int (value);
497 break;
498 case PROP_SYNC:
499 identity->sync = g_value_get_boolean (value);
500 break;
501 case PROP_CHECK_PERFECT:
502 identity->check_perfect = g_value_get_boolean (value);
503 break;
504 default:
505 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506 break;
507 }
508 }
510 static void
511 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
512 GParamSpec * pspec)
513 {
514 GstIdentity *identity;
516 identity = GST_IDENTITY (object);
518 switch (prop_id) {
519 case PROP_SLEEP_TIME:
520 g_value_set_uint (value, identity->sleep_time);
521 break;
522 case PROP_ERROR_AFTER:
523 g_value_set_int (value, identity->error_after);
524 break;
525 case PROP_DROP_PROBABILITY:
526 g_value_set_float (value, identity->drop_probability);
527 break;
528 case PROP_DATARATE:
529 g_value_set_int (value, identity->datarate);
530 break;
531 case PROP_SILENT:
532 g_value_set_boolean (value, identity->silent);
533 break;
534 case PROP_SINGLE_SEGMENT:
535 g_value_set_boolean (value, identity->single_segment);
536 break;
537 case PROP_DUMP:
538 g_value_set_boolean (value, identity->dump);
539 break;
540 case PROP_LAST_MESSAGE:
541 GST_OBJECT_LOCK (identity);
542 g_value_set_string (value, identity->last_message);
543 GST_OBJECT_UNLOCK (identity);
544 break;
545 case PROP_SYNC:
546 g_value_set_boolean (value, identity->sync);
547 break;
548 case PROP_CHECK_PERFECT:
549 g_value_set_boolean (value, identity->check_perfect);
550 break;
551 default:
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553 break;
554 }
555 }
557 static gboolean
558 gst_identity_start (GstBaseTransform * trans)
559 {
560 GstIdentity *identity;
562 identity = GST_IDENTITY (trans);
564 identity->offset = 0;
565 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
566 identity->prev_duration = GST_CLOCK_TIME_NONE;
567 identity->prev_offset_end = -1;
569 return TRUE;
570 }
572 static gboolean
573 gst_identity_stop (GstBaseTransform * trans)
574 {
575 GstIdentity *identity;
577 identity = GST_IDENTITY (trans);
579 GST_OBJECT_LOCK (identity);
580 g_free (identity->last_message);
581 identity->last_message = NULL;
582 GST_OBJECT_UNLOCK (identity);
584 return TRUE;
585 }