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 #include <stdlib.h>
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "../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 GstElementDetails gst_identity_details = GST_ELEMENT_DETAILS ("Identity",
49 "Generic",
50 "Pass data without modification",
51 "Erik Walthinsen <omega@cse.ogi.edu>");
54 /* Identity signals and args */
55 enum
56 {
57 SIGNAL_HANDOFF,
58 /* FILL ME */
59 LAST_SIGNAL
60 };
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
73 enum
74 {
75 PROP_0,
76 PROP_SLEEP_TIME,
77 PROP_ERROR_AFTER,
78 PROP_DROP_PROBABILITY,
79 PROP_DATARATE,
80 PROP_SILENT,
81 PROP_SINGLE_SEGMENT,
82 PROP_LAST_MESSAGE,
83 PROP_DUMP,
84 PROP_SYNC,
85 PROP_CHECK_PERFECT
86 };
89 #define _do_init(bla) \
90 GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
92 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstBaseTransform,
93 GST_TYPE_BASE_TRANSFORM, _do_init);
95 static void gst_identity_finalize (GObject * object);
96 static void gst_identity_set_property (GObject * object, guint prop_id,
97 const GValue * value, GParamSpec * pspec);
98 static void gst_identity_get_property (GObject * object, guint prop_id,
99 GValue * value, GParamSpec * pspec);
101 static gboolean gst_identity_event (GstBaseTransform * trans, GstEvent * event);
102 static GstFlowReturn gst_identity_transform_ip (GstBaseTransform * trans,
103 GstBuffer * buf);
104 static gboolean gst_identity_start (GstBaseTransform * trans);
105 static gboolean gst_identity_stop (GstBaseTransform * trans);
107 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
109 static void
110 gst_identity_base_init (gpointer g_class)
111 {
112 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
114 gst_element_class_add_pad_template (gstelement_class,
115 gst_static_pad_template_get (&srctemplate));
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&sinktemplate));
118 gst_element_class_set_details (gstelement_class, &gst_identity_details);
119 }
121 static void
122 gst_identity_finalize (GObject * object)
123 {
124 GstIdentity *identity;
126 identity = GST_IDENTITY (object);
128 g_free (identity->last_message);
130 G_OBJECT_CLASS (parent_class)->finalize (object);
131 }
133 /* fixme: do something about this */
134 static void
135 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
136 guint n_param_values, const GValue * param_values, gpointer invocation_hint,
137 gpointer marshal_data)
138 {
139 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
140 gpointer data2);
141 register marshalfunc_VOID__MINIOBJECT callback;
142 register GCClosure *cc = (GCClosure *) closure;
143 register gpointer data1, data2;
145 g_return_if_fail (n_param_values == 2);
147 if (G_CCLOSURE_SWAP_DATA (closure)) {
148 data1 = closure->data;
149 data2 = g_value_peek_pointer (param_values + 0);
150 } else {
151 data1 = g_value_peek_pointer (param_values + 0);
152 data2 = closure->data;
153 }
154 callback =
155 (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->
156 callback);
158 callback (data1, gst_value_get_mini_object (param_values + 1), data2);
159 }
161 static void
162 gst_identity_class_init (GstIdentityClass * klass)
163 {
164 GObjectClass *gobject_class;
165 GstElementClass *gstelement_class;
166 GstBaseTransformClass *gstbasetrans_class;
168 gobject_class = G_OBJECT_CLASS (klass);
169 gstelement_class = GST_ELEMENT_CLASS (klass);
170 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
172 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);
173 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
175 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SLEEP_TIME,
176 g_param_spec_uint ("sleep-time", "Sleep time",
177 "Microseconds to sleep between processing", 0, G_MAXUINT,
178 DEFAULT_SLEEP_TIME, G_PARAM_READWRITE));
179 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ERROR_AFTER,
180 g_param_spec_int ("error_after", "Error After", "Error after N buffers",
181 G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER, G_PARAM_READWRITE));
182 g_object_class_install_property (G_OBJECT_CLASS (klass),
183 PROP_DROP_PROBABILITY, g_param_spec_float ("drop_probability",
184 "Drop Probability", "The Probability a buffer is dropped", 0.0, 1.0,
185 DEFAULT_DROP_PROBABILITY, G_PARAM_READWRITE));
186 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATARATE,
187 g_param_spec_int ("datarate", "Datarate",
188 "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
189 0, G_MAXINT, DEFAULT_DATARATE, G_PARAM_READWRITE));
190 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SILENT,
191 g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
192 G_PARAM_READWRITE));
193 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SINGLE_SEGMENT,
194 g_param_spec_boolean ("single-segment", "Single Segment",
195 "Timestamp buffers and eat newsegments so as to appear as one segment",
196 DEFAULT_SINGLE_SEGMENT, G_PARAM_READWRITE));
197 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE,
198 g_param_spec_string ("last-message", "last-message", "last-message", NULL,
199 G_PARAM_READABLE));
200 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DUMP,
201 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents",
202 DEFAULT_DUMP, G_PARAM_READWRITE));
203 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
204 g_param_spec_boolean ("sync", "Synchronize",
205 "Synchronize to pipeline clock", DEFAULT_SYNC, G_PARAM_READWRITE));
206 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHECK_PERFECT,
207 g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
208 "Verify that the stream is time- and data-contiguous",
209 DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE));
211 gst_identity_signals[SIGNAL_HANDOFF] =
212 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
213 G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
214 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
216 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_identity_finalize);
218 gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_identity_event);
219 gstbasetrans_class->transform_ip =
220 GST_DEBUG_FUNCPTR (gst_identity_transform_ip);
221 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_identity_start);
222 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_identity_stop);
223 }
225 static void
226 gst_identity_init (GstIdentity * identity, GstIdentityClass * g_class)
227 {
228 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (identity), TRUE);
230 identity->sleep_time = DEFAULT_SLEEP_TIME;
231 identity->error_after = DEFAULT_ERROR_AFTER;
232 identity->drop_probability = DEFAULT_DROP_PROBABILITY;
233 identity->datarate = DEFAULT_DATARATE;
234 identity->silent = DEFAULT_SILENT;
235 identity->single_segment = DEFAULT_SINGLE_SEGMENT;
236 identity->sync = DEFAULT_SYNC;
237 identity->check_perfect = DEFAULT_CHECK_PERFECT;
238 identity->dump = DEFAULT_DUMP;
239 identity->last_message = NULL;
240 }
242 static gboolean
243 gst_identity_event (GstBaseTransform * trans, GstEvent * event)
244 {
245 GstIdentity *identity;
246 gboolean ret = TRUE;
248 identity = GST_IDENTITY (trans);
250 if (!identity->silent) {
251 const GstStructure *s;
252 gchar *sstr;
254 GST_OBJECT_LOCK (identity);
255 g_free (identity->last_message);
257 if ((s = gst_event_get_structure (event)))
258 sstr = gst_structure_to_string (s);
259 else
260 sstr = g_strdup ("");
262 identity->last_message =
263 g_strdup_printf ("event ******* (%s:%s) E (type: %d, %s) %p",
264 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_EVENT_TYPE (event), sstr,
265 event);
266 g_free (sstr);
267 GST_OBJECT_UNLOCK (identity);
269 g_object_notify (G_OBJECT (identity), "last_message");
270 }
272 if (identity->single_segment
273 && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
274 if (trans->have_newsegment == FALSE) {
275 GstEvent *news;
276 GstFormat format;
278 gst_event_parse_new_segment (event, NULL, NULL, &format, NULL, NULL,
279 NULL);
281 /* This is the first newsegment, send out a (0, -1) newsegment */
282 news = gst_event_new_new_segment (TRUE, 1.0, format, 0, -1, 0);
284 if (!(gst_pad_event_default (trans->sinkpad, news)))
285 return FALSE;
286 }
287 }
289 GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
291 if (identity->single_segment
292 && (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)) {
293 /* eat up segments */
294 ret = FALSE;
295 }
297 return ret;
298 }
300 static void
301 gst_identity_check_perfect (GstIdentity * identity, GstBuffer * buf)
302 {
303 GstClockTime timestamp;
305 timestamp = GST_BUFFER_TIMESTAMP (buf);
307 /* see if we need to do perfect stream checking */
308 /* invalid timestamp drops us out of check. FIXME: maybe warn ? */
309 if (timestamp != GST_CLOCK_TIME_NONE) {
310 /* check if we had a previous buffer to compare to */
311 if (identity->prev_timestamp != GST_CLOCK_TIME_NONE) {
312 guint64 offset;
314 if (identity->prev_timestamp + identity->prev_duration != timestamp) {
315 GST_WARNING_OBJECT (identity,
316 "Buffer not time-contiguous with previous one: " "prev ts %"
317 GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
318 GST_TIME_FORMAT, GST_TIME_ARGS (identity->prev_timestamp),
319 GST_TIME_ARGS (identity->prev_duration), GST_TIME_ARGS (timestamp));
320 }
322 offset = GST_BUFFER_OFFSET (buf);
323 if (identity->prev_offset_end != offset) {
324 GST_WARNING_OBJECT (identity,
325 "Buffer not data-contiguous with previous one: "
326 "prev offset_end %" G_GINT64_FORMAT ", new offset %"
327 G_GINT64_FORMAT, identity->prev_offset_end, offset);
328 }
329 }
330 /* update prev values */
331 identity->prev_timestamp = timestamp;
332 identity->prev_duration = GST_BUFFER_DURATION (buf);
333 identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
334 }
335 }
337 static GstFlowReturn
338 gst_identity_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
339 {
340 GstFlowReturn ret = GST_FLOW_OK;
341 GstIdentity *identity = GST_IDENTITY (trans);
342 GstClockTime runtimestamp;
344 if (identity->check_perfect)
345 gst_identity_check_perfect (identity, buf);
347 if (identity->error_after >= 0) {
348 identity->error_after--;
349 if (identity->error_after == 0) {
350 GST_ELEMENT_ERROR (identity, CORE, FAILED,
351 (_("Failed after iterations as requested.")), (NULL));
352 return GST_FLOW_ERROR;
353 }
354 }
356 if (identity->drop_probability > 0.0) {
357 if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability) {
358 GST_OBJECT_LOCK (identity);
359 g_free (identity->last_message);
360 identity->last_message =
361 g_strdup_printf ("dropping ******* (%s:%s)i (%d bytes, timestamp: %"
362 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
363 G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
364 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_BUFFER_SIZE (buf),
365 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
366 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
367 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
368 GST_BUFFER_FLAGS (buf), buf);
369 GST_OBJECT_UNLOCK (identity);
370 g_object_notify (G_OBJECT (identity), "last-message");
371 return GST_FLOW_OK;
372 }
373 }
375 if (identity->dump) {
376 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
377 }
379 if (!identity->silent) {
380 GST_OBJECT_LOCK (identity);
381 g_free (identity->last_message);
382 identity->last_message =
383 g_strdup_printf ("chain ******* (%s:%s)i (%d bytes, timestamp: %"
384 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
385 G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
386 GST_DEBUG_PAD_NAME (trans->sinkpad), GST_BUFFER_SIZE (buf),
387 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
388 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
389 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
390 GST_BUFFER_FLAGS (buf), buf);
391 GST_OBJECT_UNLOCK (identity);
392 g_object_notify (G_OBJECT (identity), "last-message");
393 }
395 if (identity->datarate > 0) {
396 GstClockTime time = identity->offset * GST_SECOND / identity->datarate;
398 GST_BUFFER_TIMESTAMP (buf) = time;
399 GST_BUFFER_DURATION (buf) =
400 GST_BUFFER_SIZE (buf) * GST_SECOND / identity->datarate;
401 }
403 g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
404 buf);
406 runtimestamp = gst_segment_to_running_time (&trans->segment,
407 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf));
409 if (identity->sync) {
410 GstClock *clock;
412 GST_OBJECT_LOCK (identity);
413 if ((clock = GST_ELEMENT (identity)->clock)) {
414 GstClockReturn cret;
415 GstClockTime timestamp;
417 timestamp = runtimestamp + GST_ELEMENT (identity)->base_time;
419 /* save id if we need to unlock */
420 /* FIXME: actually unlock this somewhere in the state changes */
421 identity->clock_id = gst_clock_new_single_shot_id (clock, timestamp);
422 GST_OBJECT_UNLOCK (identity);
424 cret = gst_clock_id_wait (identity->clock_id, NULL);
426 GST_OBJECT_LOCK (identity);
427 if (identity->clock_id) {
428 gst_clock_id_unref (identity->clock_id);
429 identity->clock_id = NULL;
430 }
431 if (cret == GST_CLOCK_UNSCHEDULED)
432 ret = GST_FLOW_UNEXPECTED;
433 }
434 GST_OBJECT_UNLOCK (identity);
435 }
437 identity->offset += GST_BUFFER_SIZE (buf);
439 if (identity->sleep_time && ret == GST_FLOW_OK)
440 g_usleep (identity->sleep_time);
442 if (identity->single_segment && ret == GST_FLOW_OK)
443 GST_BUFFER_TIMESTAMP (buf) = runtimestamp;
445 return ret;
446 }
448 static void
449 gst_identity_set_property (GObject * object, guint prop_id,
450 const GValue * value, GParamSpec * pspec)
451 {
452 GstIdentity *identity;
454 identity = GST_IDENTITY (object);
456 switch (prop_id) {
457 case PROP_SLEEP_TIME:
458 identity->sleep_time = g_value_get_uint (value);
459 break;
460 case PROP_SILENT:
461 identity->silent = g_value_get_boolean (value);
462 break;
463 case PROP_SINGLE_SEGMENT:
464 identity->single_segment = g_value_get_boolean (value);
465 break;
466 case PROP_DUMP:
467 identity->dump = g_value_get_boolean (value);
468 break;
469 case PROP_ERROR_AFTER:
470 identity->error_after = g_value_get_int (value);
471 break;
472 case PROP_DROP_PROBABILITY:
473 identity->drop_probability = g_value_get_float (value);
474 break;
475 case PROP_DATARATE:
476 identity->datarate = g_value_get_int (value);
477 break;
478 case PROP_SYNC:
479 identity->sync = g_value_get_boolean (value);
480 break;
481 case PROP_CHECK_PERFECT:
482 identity->check_perfect = g_value_get_boolean (value);
483 break;
484 default:
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 break;
487 }
488 }
490 static void
491 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
492 GParamSpec * pspec)
493 {
494 GstIdentity *identity;
496 identity = GST_IDENTITY (object);
498 switch (prop_id) {
499 case PROP_SLEEP_TIME:
500 g_value_set_uint (value, identity->sleep_time);
501 break;
502 case PROP_ERROR_AFTER:
503 g_value_set_int (value, identity->error_after);
504 break;
505 case PROP_DROP_PROBABILITY:
506 g_value_set_float (value, identity->drop_probability);
507 break;
508 case PROP_DATARATE:
509 g_value_set_int (value, identity->datarate);
510 break;
511 case PROP_SILENT:
512 g_value_set_boolean (value, identity->silent);
513 break;
514 case PROP_SINGLE_SEGMENT:
515 g_value_set_boolean (value, identity->single_segment);
516 break;
517 case PROP_DUMP:
518 g_value_set_boolean (value, identity->dump);
519 break;
520 case PROP_LAST_MESSAGE:
521 GST_OBJECT_LOCK (identity);
522 g_value_set_string (value, identity->last_message);
523 GST_OBJECT_UNLOCK (identity);
524 break;
525 case PROP_SYNC:
526 g_value_set_boolean (value, identity->sync);
527 break;
528 case PROP_CHECK_PERFECT:
529 g_value_set_boolean (value, identity->check_perfect);
530 break;
531 default:
532 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
533 break;
534 }
535 }
537 static gboolean
538 gst_identity_start (GstBaseTransform * trans)
539 {
540 GstIdentity *identity;
542 identity = GST_IDENTITY (trans);
544 identity->offset = 0;
545 identity->prev_timestamp = GST_CLOCK_TIME_NONE;
546 identity->prev_duration = GST_CLOCK_TIME_NONE;
547 identity->prev_offset_end = -1;
549 return TRUE;
550 }
552 static gboolean
553 gst_identity_stop (GstBaseTransform * trans)
554 {
555 GstIdentity *identity;
557 identity = GST_IDENTITY (trans);
559 GST_OBJECT_LOCK (identity);
560 g_free (identity->last_message);
561 identity->last_message = NULL;
562 GST_OBJECT_UNLOCK (identity);
564 return TRUE;
565 }