Next big merge.
[platform/upstream/gstreamer.git] / gst / elements / gstidentity.c
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  */
23
24
25 #include <stdlib.h>
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "../gst-i18n-lib.h"
32 #include "gstidentity.h"
33 #include <gst/gstmarshal.h>
34
35 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS_ANY);
39
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS_ANY);
44
45 GST_DEBUG_CATEGORY_STATIC (gst_identity_debug);
46 #define GST_CAT_DEFAULT gst_identity_debug
47
48 GstElementDetails gst_identity_details = GST_ELEMENT_DETAILS ("Identity",
49     "Generic",
50     "Pass data without modification",
51     "Erik Walthinsen <omega@cse.ogi.edu>");
52
53
54 /* Identity signals and args */
55 enum
56 {
57   SIGNAL_HANDOFF,
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 #define DEFAULT_LOOP_BASED              FALSE
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_DUMP                    FALSE
70 #define DEFAULT_SYNC                    FALSE
71 #define DEFAULT_CHECK_PERFECT           FALSE
72
73 enum
74 {
75   PROP_0,
76   PROP_HAS_GETRANGE,
77   PROP_HAS_CHAIN,
78   PROP_HAS_SINK_LOOP,
79   PROP_HAS_SRC_LOOP,
80   PROP_LOOP_BASED,
81   PROP_SLEEP_TIME,
82   PROP_DUPLICATE,
83   PROP_ERROR_AFTER,
84   PROP_DROP_PROBABILITY,
85   PROP_DATARATE,
86   PROP_SILENT,
87   PROP_LAST_MESSAGE,
88   PROP_DUMP,
89   PROP_SYNC,
90   PROP_CHECK_PERFECT
91 };
92
93
94 typedef GstFlowReturn (*IdentityPushFunc) (GstIdentity *, GstBuffer *);
95
96
97 #define _do_init(bla) \
98     GST_DEBUG_CATEGORY_INIT (gst_identity_debug, "identity", 0, "identity element");
99
100 GST_BOILERPLATE_FULL (GstIdentity, gst_identity, GstElement, GST_TYPE_ELEMENT,
101     _do_init);
102
103 static void gst_identity_finalize (GObject * object);
104 static void gst_identity_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec);
106 static void gst_identity_get_property (GObject * object, guint prop_id,
107     GValue * value, GParamSpec * pspec);
108 static GstElementStateReturn gst_identity_change_state (GstElement * element);
109
110 static gboolean gst_identity_event (GstPad * pad, GstEvent * event);
111 static GstFlowReturn gst_identity_getrange (GstPad * pad, guint64 offset,
112     guint length, GstBuffer ** buffer);
113 static GstFlowReturn gst_identity_chain (GstPad * pad, GstBuffer * buffer);
114 static void gst_identity_src_loop (GstPad * pad);
115 static void gst_identity_sink_loop (GstPad * pad);
116 static GstFlowReturn gst_identity_handle_buffer (GstIdentity * identity,
117     GstBuffer * buf);
118 static void gst_identity_set_clock (GstElement * element, GstClock * clock);
119 static GstCaps *gst_identity_proxy_getcaps (GstPad * pad);
120
121
122 static guint gst_identity_signals[LAST_SIGNAL] = { 0 };
123
124 static void
125 gst_identity_base_init (gpointer g_class)
126 {
127   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
128
129   gst_element_class_add_pad_template (gstelement_class,
130       gst_static_pad_template_get (&srctemplate));
131   gst_element_class_add_pad_template (gstelement_class,
132       gst_static_pad_template_get (&sinktemplate));
133   gst_element_class_set_details (gstelement_class, &gst_identity_details);
134 }
135
136 static void
137 gst_identity_finalize (GObject * object)
138 {
139   GstIdentity *identity;
140
141   identity = GST_IDENTITY (object);
142
143   g_mutex_free (identity->pen_lock);
144   g_cond_free (identity->pen_cond);
145
146   g_free (identity->last_message);
147
148   G_OBJECT_CLASS (parent_class)->finalize (object);
149 }
150
151 static void
152 gst_identity_class_init (GstIdentityClass * klass)
153 {
154   GObjectClass *gobject_class;
155   GstElementClass *gstelement_class;
156
157   gobject_class = G_OBJECT_CLASS (klass);
158   gstelement_class = GST_ELEMENT_CLASS (klass);
159
160   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);
161   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
162
163   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
164       g_param_spec_boolean ("has-getrange", "Has getrange",
165           "If the src pad will implement a getrange function",
166           TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
167   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_CHAIN,
168       g_param_spec_boolean ("has-chain", "Has chain",
169           "If the sink pad will implement a chain function",
170           TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
171   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_SRC_LOOP,
172       g_param_spec_boolean ("has-src-loop", "Has src loop",
173           "If the src pad will implement a loop function",
174           FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
175   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_SINK_LOOP,
176       g_param_spec_boolean ("has-sink-loop", "Has sink loop",
177           "If the sink pad will implement a loop function",
178           FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
179   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SLEEP_TIME,
180       g_param_spec_uint ("sleep-time", "Sleep time",
181           "Microseconds to sleep between processing", 0, G_MAXUINT,
182           DEFAULT_SLEEP_TIME, G_PARAM_READWRITE));
183   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DUPLICATE,
184       g_param_spec_uint ("duplicate", "Duplicate Buffers",
185           "Push the buffers N times", 0, G_MAXUINT, DEFAULT_DUPLICATE,
186           G_PARAM_READWRITE));
187   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ERROR_AFTER,
188       g_param_spec_int ("error_after", "Error After", "Error after N buffers",
189           G_MININT, G_MAXINT, DEFAULT_ERROR_AFTER, G_PARAM_READWRITE));
190   g_object_class_install_property (G_OBJECT_CLASS (klass),
191       PROP_DROP_PROBABILITY, g_param_spec_float ("drop_probability",
192           "Drop Probability", "The Probability a buffer is dropped", 0.0, 1.0,
193           DEFAULT_DROP_PROBABILITY, G_PARAM_READWRITE));
194   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATARATE,
195       g_param_spec_int ("datarate", "Datarate",
196           "(Re)timestamps buffers with number of bytes per second (0 = inactive)",
197           0, G_MAXINT, DEFAULT_DATARATE, G_PARAM_READWRITE));
198   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SILENT,
199       g_param_spec_boolean ("silent", "silent", "silent", DEFAULT_SILENT,
200           G_PARAM_READWRITE));
201   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE,
202       g_param_spec_string ("last-message", "last-message", "last-message", NULL,
203           G_PARAM_READABLE));
204   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DUMP,
205       g_param_spec_boolean ("dump", "Dump", "Dump buffer contents",
206           DEFAULT_DUMP, G_PARAM_READWRITE));
207   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
208       g_param_spec_boolean ("sync", "Synchronize",
209           "Synchronize to pipeline clock", DEFAULT_SYNC, G_PARAM_READWRITE));
210   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHECK_PERFECT,
211       g_param_spec_boolean ("check-perfect", "Check For Perfect Stream",
212           "Verify that the stream is time- and data-contiguous",
213           DEFAULT_CHECK_PERFECT, G_PARAM_READWRITE));
214
215   gst_identity_signals[SIGNAL_HANDOFF] =
216       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
217       G_STRUCT_OFFSET (GstIdentityClass, handoff), NULL, NULL,
218       gst_marshal_VOID__BOXED, G_TYPE_NONE, 1,
219       GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
220
221   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_identity_finalize);
222
223   gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_identity_set_clock);
224   gstelement_class->change_state =
225       GST_DEBUG_FUNCPTR (gst_identity_change_state);
226
227 }
228
229 static void
230 gst_identity_init (GstIdentity * identity)
231 {
232   identity->sinkpad =
233       gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate),
234       "sink");
235   gst_element_add_pad (GST_ELEMENT (identity), identity->sinkpad);
236   gst_pad_set_getcaps_function (identity->sinkpad,
237       GST_DEBUG_FUNCPTR (gst_identity_proxy_getcaps));
238   gst_pad_set_event_function (identity->sinkpad,
239       GST_DEBUG_FUNCPTR (gst_identity_event));
240
241   identity->srcpad =
242       gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
243       "src");
244   gst_pad_set_getcaps_function (identity->srcpad,
245       GST_DEBUG_FUNCPTR (gst_identity_proxy_getcaps));
246   gst_pad_set_getrange_function (identity->srcpad,
247       GST_DEBUG_FUNCPTR (gst_identity_getrange));
248   gst_element_add_pad (GST_ELEMENT (identity), identity->srcpad);
249
250   identity->sleep_time = DEFAULT_SLEEP_TIME;
251   identity->duplicate = DEFAULT_DUPLICATE;
252   identity->error_after = DEFAULT_ERROR_AFTER;
253   identity->drop_probability = DEFAULT_DROP_PROBABILITY;
254   identity->datarate = DEFAULT_DATARATE;
255   identity->silent = DEFAULT_SILENT;
256   identity->sync = DEFAULT_SYNC;
257   identity->check_perfect = DEFAULT_CHECK_PERFECT;
258   identity->dump = DEFAULT_DUMP;
259   identity->last_message = NULL;
260   identity->srccaps = NULL;
261
262   identity->pen_data = NULL;
263   identity->pen_lock = g_mutex_new ();
264   identity->pen_cond = g_cond_new ();
265   identity->pen_flushing = FALSE;
266 }
267
268 static void
269 gst_identity_set_clock (GstElement * element, GstClock * clock)
270 {
271   GstIdentity *identity = GST_IDENTITY (element);
272
273   gst_object_replace ((GstObject **) & identity->clock, (GstObject *) clock);
274 }
275
276 static GstCaps *
277 gst_identity_proxy_getcaps (GstPad * pad)
278 {
279   GstPad *otherpad;
280   GstIdentity *identity = GST_IDENTITY (GST_OBJECT_PARENT (pad));
281
282   otherpad = pad == identity->srcpad ? identity->sinkpad : identity->srcpad;
283
284   return gst_pad_peer_get_caps (otherpad);
285 }
286
287 static gboolean
288 identity_queue_push (GstIdentity * identity, GstData * data)
289 {
290   gboolean ret;
291
292   g_mutex_lock (identity->pen_lock);
293   while (identity->pen_data && !identity->pen_flushing)
294     g_cond_wait (identity->pen_cond, identity->pen_lock);
295   if (identity->pen_flushing) {
296     gst_data_unref (identity->pen_data);
297     identity->pen_data = NULL;
298     gst_data_unref (data);
299     ret = FALSE;
300   } else {
301     identity->pen_data = data;
302     ret = TRUE;
303   }
304   g_cond_signal (identity->pen_cond);
305   g_mutex_unlock (identity->pen_lock);
306
307   return ret;
308 }
309
310 static GstData *
311 identity_queue_pop (GstIdentity * identity)
312 {
313   GstData *ret;
314
315   g_mutex_lock (identity->pen_lock);
316   while (!(ret = identity->pen_data) && !identity->pen_flushing)
317     g_cond_wait (identity->pen_cond, identity->pen_lock);
318   g_cond_signal (identity->pen_cond);
319   g_mutex_unlock (identity->pen_lock);
320
321   return ret;
322 }
323
324 static void
325 identity_queue_flush (GstIdentity * identity)
326 {
327   g_mutex_lock (identity->pen_lock);
328   identity->pen_flushing = TRUE;
329   g_cond_signal (identity->pen_cond);
330   g_mutex_unlock (identity->pen_lock);
331 }
332
333 static gboolean
334 gst_identity_event (GstPad * pad, GstEvent * event)
335 {
336   GstIdentity *identity;
337   gboolean ret;
338
339   identity = GST_IDENTITY (GST_PAD_PARENT (pad));
340
341   GST_STREAM_LOCK (pad);
342
343   if (!identity->silent) {
344     g_free (identity->last_message);
345
346     identity->last_message =
347         g_strdup_printf ("chain   ******* (%s:%s)E (type: %d) %p",
348         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE (event), event);
349
350     g_object_notify (G_OBJECT (identity), "last_message");
351   }
352
353   switch (GST_EVENT_TYPE (event)) {
354     case GST_EVENT_FLUSH:
355       /* forward event */
356       gst_pad_event_default (pad, event);
357       if (GST_EVENT_FLUSH_DONE (event)) {
358         if (identity->sink_mode == GST_ACTIVATE_PULL) {
359           /* already have the sink stream lock */
360           gst_task_start (GST_RPAD_TASK (identity->sinkpad));
361         }
362         if (identity->src_mode == GST_ACTIVATE_PUSH) {
363           GST_STREAM_LOCK (identity->srcpad);
364           gst_task_start (GST_RPAD_TASK (identity->srcpad));
365           GST_STREAM_UNLOCK (identity->srcpad);
366         }
367       } else {
368         /* unblock both functions */
369         identity_queue_flush (identity);
370
371       }
372       ret = TRUE;
373       goto done;
374     case GST_EVENT_EOS:
375       if (identity->sink_mode == GST_ACTIVATE_PULL) {
376         /* already have the sink stream lock */
377         gst_task_pause (GST_RPAD_TASK (identity->sinkpad));
378       }
379       break;
380     default:
381       break;
382   }
383
384   if (identity->decoupled) {
385     ret = identity_queue_push (identity, (GstData *) event);
386   } else {
387     ret = gst_pad_push_event (identity->srcpad, event);
388   }
389
390 done:
391   GST_STREAM_UNLOCK (pad);
392   return ret;
393 }
394
395 static GstFlowReturn
396 gst_identity_getrange (GstPad * pad, guint64 offset,
397     guint length, GstBuffer ** buffer)
398 {
399   GstIdentity *identity;
400   GstFlowReturn ret;
401
402   identity = GST_IDENTITY (GST_PAD_PARENT (pad));
403
404   GST_STREAM_LOCK (pad);
405
406   ret = gst_pad_pull_range (identity->sinkpad, offset, length, buffer);
407
408   GST_STREAM_UNLOCK (pad);
409
410   return ret;
411 }
412
413 static GstFlowReturn
414 gst_identity_chain (GstPad * pad, GstBuffer * buffer)
415 {
416   GstIdentity *identity;
417   GstFlowReturn ret = GST_FLOW_OK;
418
419   identity = GST_IDENTITY (GST_PAD_PARENT (pad));
420
421   GST_STREAM_LOCK (pad);
422
423   ret = gst_identity_handle_buffer (identity, buffer);
424
425   GST_STREAM_UNLOCK (pad);
426
427   return ret;
428 }
429
430 #define DEFAULT_PULL_SIZE 1024
431
432 static void
433 gst_identity_sink_loop (GstPad * pad)
434 {
435   GstIdentity *identity;
436   GstBuffer *buffer;
437   GstFlowReturn ret;
438
439   identity = GST_IDENTITY (GST_PAD_PARENT (pad));
440
441   GST_STREAM_LOCK (pad);
442
443   ret = gst_pad_pull_range (pad, identity->offset, DEFAULT_PULL_SIZE, &buffer);
444   if (ret != GST_FLOW_OK)
445     goto sink_loop_pause;
446
447   ret = gst_identity_handle_buffer (identity, buffer);
448   if (ret != GST_FLOW_OK)
449     goto sink_loop_pause;
450
451   GST_STREAM_UNLOCK (pad);
452   return;
453
454 sink_loop_pause:
455   gst_task_pause (GST_RPAD_TASK (identity->sinkpad));
456   GST_STREAM_UNLOCK (pad);
457   return;
458 }
459
460 static void
461 gst_identity_src_loop (GstPad * pad)
462 {
463   GstIdentity *identity;
464   GstData *data;
465   GstFlowReturn ret;
466
467   identity = GST_IDENTITY (GST_PAD_PARENT (pad));
468
469   GST_STREAM_LOCK (pad);
470
471   data = identity_queue_pop (identity);
472   if (!data)                    /* we're getting flushed */
473     goto src_loop_pause;
474
475   if (GST_IS_EVENT (data)) {
476     if (GST_EVENT_TYPE (data) == GST_EVENT_EOS)
477       gst_task_pause (GST_RPAD_TASK (identity->srcpad));
478     gst_pad_push_event (identity->srcpad, GST_EVENT (data));
479   } else {
480     ret = gst_pad_push (identity->srcpad, (GstBuffer *) data);
481     if (ret != GST_FLOW_OK)
482       goto src_loop_pause;
483   }
484
485   GST_STREAM_UNLOCK (pad);
486   return;
487
488 src_loop_pause:
489   gst_task_pause (GST_RPAD_TASK (identity->srcpad));
490   GST_STREAM_UNLOCK (pad);
491   return;
492 }
493
494 static GstFlowReturn
495 gst_identity_handle_buffer (GstIdentity * identity, GstBuffer * buf)
496 {
497   GstFlowReturn ret = GST_FLOW_OK;
498   guint i;
499
500   /* see if we need to do perfect stream checking */
501   /* invalid timestamp drops us out of check.  FIXME: maybe warn ? */
502   if (identity->check_perfect &&
503       GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
504     /* check if we had a previous buffer to compare to */
505     if (identity->prev_timestamp != GST_CLOCK_TIME_NONE) {
506       if (identity->prev_timestamp + identity->prev_duration !=
507           GST_BUFFER_TIMESTAMP (buf)) {
508         GST_WARNING_OBJECT (identity,
509             "Buffer not time-contiguous with previous one: " "prev ts %"
510             GST_TIME_FORMAT ", prev dur %" GST_TIME_FORMAT ", new ts %"
511             GST_TIME_FORMAT, GST_TIME_ARGS (identity->prev_timestamp),
512             GST_TIME_ARGS (identity->prev_duration),
513             GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
514       }
515       if (identity->prev_offset_end != GST_BUFFER_OFFSET (buf)) {
516         GST_WARNING_OBJECT (identity,
517             "Buffer not data-contiguous with previous one: "
518             "prev offset_end %" G_GINT64_FORMAT ", new offset %"
519             G_GINT64_FORMAT, identity->prev_offset_end,
520             GST_BUFFER_OFFSET (buf));
521       }
522     }
523     /* update prev values */
524     identity->prev_timestamp = GST_BUFFER_TIMESTAMP (buf);
525     identity->prev_duration = GST_BUFFER_DURATION (buf);
526     identity->prev_offset_end = GST_BUFFER_OFFSET_END (buf);
527   }
528
529   if (identity->error_after >= 0) {
530     identity->error_after--;
531     if (identity->error_after == 0) {
532       gst_buffer_unref (buf);
533       GST_ELEMENT_ERROR (identity, CORE, FAILED,
534           (_("Failed after iterations as requested.")), (NULL));
535       return GST_FLOW_ERROR;
536     }
537   }
538
539   if (identity->drop_probability > 0.0) {
540     if ((gfloat) (1.0 * rand () / (RAND_MAX)) < identity->drop_probability) {
541       g_free (identity->last_message);
542       identity->last_message =
543           g_strdup_printf ("dropping   ******* (%s:%s)i (%d bytes, timestamp: %"
544           GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
545           G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
546           GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf),
547           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
548           GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
549           GST_BUFFER_OFFSET_END (buf), GST_BUFFER_FLAGS (buf), buf);
550       g_object_notify (G_OBJECT (identity), "last-message");
551       gst_buffer_unref (buf);
552       return GST_FLOW_OK;
553     }
554   }
555
556   if (identity->dump) {
557     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
558   }
559
560   for (i = identity->duplicate; i; i--) {
561     GstClockTime time;
562
563     if (!identity->silent) {
564       g_free (identity->last_message);
565       identity->last_message =
566           g_strdup_printf ("chain   ******* (%s:%s)i (%d bytes, timestamp: %"
567           GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
568           G_GINT64_FORMAT ", offset_end: % " G_GINT64_FORMAT ", flags: %d) %p",
569           GST_DEBUG_PAD_NAME (identity->sinkpad), GST_BUFFER_SIZE (buf),
570           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
571           GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
572           GST_BUFFER_OFFSET_END (buf), GST_BUFFER_FLAGS (buf), buf);
573       g_object_notify (G_OBJECT (identity), "last-message");
574     }
575
576     time = GST_BUFFER_TIMESTAMP (buf);
577
578     if (identity->datarate > 0) {
579       time = identity->offset * GST_SECOND / identity->datarate;
580
581       GST_BUFFER_TIMESTAMP (buf) = time;
582       GST_BUFFER_DURATION (buf) =
583           GST_BUFFER_SIZE (buf) * GST_SECOND / identity->datarate;
584     }
585
586     g_signal_emit (G_OBJECT (identity), gst_identity_signals[SIGNAL_HANDOFF], 0,
587         buf);
588
589     if (i > 1)
590       gst_buffer_ref (buf);
591
592     if (identity->sync) {
593       if (identity->clock) {
594         /* gst_element_wait (GST_ELEMENT (identity), time); */
595       }
596     }
597
598     identity->offset += GST_BUFFER_SIZE (buf);
599     if (identity->decoupled) {
600       if (!identity_queue_push (identity, (GstData *) buf))
601         return GST_FLOW_UNEXPECTED;
602     } else {
603       ret = gst_pad_push (identity->srcpad, buf);
604       if (ret != GST_FLOW_OK)
605         return ret;
606     }
607
608     if (identity->sleep_time)
609       g_usleep (identity->sleep_time);
610   }
611
612   return ret;
613 }
614
615 static void
616 gst_identity_set_dataflow_funcs (GstIdentity * identity)
617 {
618   if (identity->has_getrange)
619     gst_pad_set_getrange_function (identity->srcpad, gst_identity_getrange);
620   else
621     gst_pad_set_getrange_function (identity->srcpad, NULL);
622
623   if (identity->has_chain)
624     gst_pad_set_chain_function (identity->sinkpad, gst_identity_chain);
625   else
626     gst_pad_set_chain_function (identity->sinkpad, NULL);
627
628   if (identity->has_src_loop)
629     gst_pad_set_loop_function (identity->srcpad, gst_identity_src_loop);
630   else
631     gst_pad_set_loop_function (identity->srcpad, NULL);
632
633   if (identity->has_sink_loop)
634     gst_pad_set_loop_function (identity->sinkpad, gst_identity_sink_loop);
635   else
636     gst_pad_set_loop_function (identity->sinkpad, NULL);
637 }
638
639 static void
640 gst_identity_set_property (GObject * object, guint prop_id,
641     const GValue * value, GParamSpec * pspec)
642 {
643   GstIdentity *identity;
644
645   identity = GST_IDENTITY (object);
646
647   switch (prop_id) {
648     case PROP_HAS_GETRANGE:
649       identity->has_getrange = g_value_get_boolean (value);
650       gst_identity_set_dataflow_funcs (identity);
651       break;
652     case PROP_HAS_CHAIN:
653       identity->has_chain = g_value_get_boolean (value);
654       gst_identity_set_dataflow_funcs (identity);
655       break;
656     case PROP_HAS_SRC_LOOP:
657       identity->has_src_loop = g_value_get_boolean (value);
658       gst_identity_set_dataflow_funcs (identity);
659       break;
660     case PROP_HAS_SINK_LOOP:
661       identity->has_sink_loop = g_value_get_boolean (value);
662       gst_identity_set_dataflow_funcs (identity);
663       break;
664     case PROP_SLEEP_TIME:
665       identity->sleep_time = g_value_get_uint (value);
666       break;
667     case PROP_SILENT:
668       identity->silent = g_value_get_boolean (value);
669       break;
670     case PROP_DUPLICATE:
671       identity->duplicate = g_value_get_uint (value);
672       break;
673     case PROP_DUMP:
674       identity->dump = g_value_get_boolean (value);
675       break;
676     case PROP_ERROR_AFTER:
677       identity->error_after = g_value_get_int (value);
678       break;
679     case PROP_DROP_PROBABILITY:
680       identity->drop_probability = g_value_get_float (value);
681       break;
682     case PROP_DATARATE:
683       identity->datarate = g_value_get_int (value);
684       break;
685     case PROP_SYNC:
686       identity->sync = g_value_get_boolean (value);
687       break;
688     case PROP_CHECK_PERFECT:
689       identity->check_perfect = g_value_get_boolean (value);
690       break;
691     default:
692       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
693       break;
694   }
695 }
696
697 static void
698 gst_identity_get_property (GObject * object, guint prop_id, GValue * value,
699     GParamSpec * pspec)
700 {
701   GstIdentity *identity;
702
703   identity = GST_IDENTITY (object);
704
705   switch (prop_id) {
706     case PROP_HAS_GETRANGE:
707       g_value_set_boolean (value, identity->has_getrange);
708       break;
709     case PROP_HAS_CHAIN:
710       g_value_set_boolean (value, identity->has_chain);
711       break;
712     case PROP_HAS_SRC_LOOP:
713       g_value_set_boolean (value, identity->has_src_loop);
714       break;
715     case PROP_HAS_SINK_LOOP:
716       g_value_set_boolean (value, identity->has_sink_loop);
717       break;
718     case PROP_SLEEP_TIME:
719       g_value_set_uint (value, identity->sleep_time);
720       break;
721     case PROP_DUPLICATE:
722       g_value_set_uint (value, identity->duplicate);
723       break;
724     case PROP_ERROR_AFTER:
725       g_value_set_int (value, identity->error_after);
726       break;
727     case PROP_DROP_PROBABILITY:
728       g_value_set_float (value, identity->drop_probability);
729       break;
730     case PROP_DATARATE:
731       g_value_set_int (value, identity->datarate);
732       break;
733     case PROP_SILENT:
734       g_value_set_boolean (value, identity->silent);
735       break;
736     case PROP_DUMP:
737       g_value_set_boolean (value, identity->dump);
738       break;
739     case PROP_LAST_MESSAGE:
740       g_value_set_string (value, identity->last_message);
741       break;
742     case PROP_SYNC:
743       g_value_set_boolean (value, identity->sync);
744       break;
745     case PROP_CHECK_PERFECT:
746       g_value_set_boolean (value, identity->check_perfect);
747       break;
748     default:
749       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
750       break;
751   }
752 }
753
754 static GstElementStateReturn
755 gst_identity_change_state (GstElement * element)
756 {
757   GstIdentity *identity;
758
759   g_return_val_if_fail (GST_IS_IDENTITY (element), GST_STATE_FAILURE);
760
761   identity = GST_IDENTITY (element);
762
763   switch (GST_STATE_TRANSITION (element)) {
764     case GST_STATE_NULL_TO_READY:
765       break;
766     case GST_STATE_READY_TO_PAUSED:
767       identity->offset = 0;
768       identity->prev_timestamp = GST_CLOCK_TIME_NONE;
769       identity->prev_duration = GST_CLOCK_TIME_NONE;
770       identity->prev_offset_end = -1;
771       break;
772     case GST_STATE_PAUSED_TO_PLAYING:
773     case GST_STATE_PLAYING_TO_PAUSED:
774       break;
775     case GST_STATE_PAUSED_TO_READY:
776       g_free (identity->last_message);
777       identity->last_message = NULL;
778       break;
779     case GST_STATE_READY_TO_NULL:
780       break;
781     default:
782       break;
783   }
784
785   if (GST_ELEMENT_CLASS (parent_class)->change_state)
786     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
787
788   return GST_STATE_SUCCESS;
789 }