adder: rework timestamping
[platform/upstream/gstreamer.git] / gst / adder / gstadder.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2001 Thomas <thomas@apestaart.org>
4  *               2005,2006 Wim Taymans <wim@fluendo.com>
5  *
6  * adder.c: Adder element, N in, one out, samples are added
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  * SECTION:element-adder
25  *
26  * The adder allows to mix several streams into one by adding the data.
27  * Mixed data is clamped to the min/max values of the data format.
28  *
29  * The adder currently mixes all data received on the sinkpads as soon as
30  * possible without trying to synchronize the streams.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36  * ]| This pipeline produces two sine waves mixed together.
37  * </refsect2>
38  *
39  * Last reviewed on 2006-05-09 (0.10.7)
40  */
41 /* Element-Checklist-Version: 5 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #include "gstadder.h"
47 #include <gst/audio/audio.h>
48 #include <string.h>             /* strcmp */
49 /*#include <liboil/liboil.h>*/
50
51 /* highest positive/lowest negative x-bit value we can use for clamping */
52 #define MAX_INT_32  ((gint32) (0x7fffffff))
53 #define MAX_INT_16  ((gint16) (0x7fff))
54 #define MAX_INT_8   ((gint8)  (0x7f))
55 #define MAX_UINT_32 ((guint32)(0xffffffff))
56 #define MAX_UINT_16 ((guint16)(0xffff))
57 #define MAX_UINT_8  ((guint8) (0xff))
58
59 #define MIN_INT_32  ((gint32) (0x80000000))
60 #define MIN_INT_16  ((gint16) (0x8000))
61 #define MIN_INT_8   ((gint8)  (0x80))
62 #define MIN_UINT_32 ((guint32)(0x00000000))
63 #define MIN_UINT_16 ((guint16)(0x0000))
64 #define MIN_UINT_8  ((guint8) (0x00))
65
66 enum
67 {
68   PROP_0,
69   PROP_FILTER_CAPS
70 };
71
72 #define GST_CAT_DEFAULT gst_adder_debug
73 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
74
75 /* elementfactory information */
76
77 #define CAPS \
78   "audio/x-raw-int, " \
79   "rate = (int) [ 1, MAX ], " \
80   "channels = (int) [ 1, MAX ], " \
81   "endianness = (int) BYTE_ORDER, " \
82   "width = (int) 32, " \
83   "depth = (int) 32, " \
84   "signed = (boolean) { true, false } ;" \
85   "audio/x-raw-int, " \
86   "rate = (int) [ 1, MAX ], " \
87   "channels = (int) [ 1, MAX ], " \
88   "endianness = (int) BYTE_ORDER, " \
89   "width = (int) 16, " \
90   "depth = (int) 16, " \
91   "signed = (boolean) { true, false } ;" \
92   "audio/x-raw-int, " \
93   "rate = (int) [ 1, MAX ], " \
94   "channels = (int) [ 1, MAX ], " \
95   "endianness = (int) BYTE_ORDER, " \
96   "width = (int) 8, " \
97   "depth = (int) 8, " \
98   "signed = (boolean) { true, false } ;" \
99   "audio/x-raw-float, " \
100   "rate = (int) [ 1, MAX ], " \
101   "channels = (int) [ 1, MAX ], " \
102   "endianness = (int) BYTE_ORDER, " \
103   "width = (int) { 32, 64 }"
104
105 static GstStaticPadTemplate gst_adder_src_template =
106 GST_STATIC_PAD_TEMPLATE ("src",
107     GST_PAD_SRC,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS (CAPS)
110     );
111
112 static GstStaticPadTemplate gst_adder_sink_template =
113 GST_STATIC_PAD_TEMPLATE ("sink%d",
114     GST_PAD_SINK,
115     GST_PAD_REQUEST,
116     GST_STATIC_CAPS (CAPS)
117     );
118
119 static void gst_adder_class_init (GstAdderClass * klass);
120 static void gst_adder_init (GstAdder * adder);
121 static void gst_adder_dispose (GObject * object);
122 static void gst_adder_set_property (GObject * object, guint prop_id,
123     const GValue * value, GParamSpec * pspec);
124 static void gst_adder_get_property (GObject * object, guint prop_id,
125     GValue * value, GParamSpec * pspec);
126
127 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
128 static gboolean gst_adder_query (GstPad * pad, GstQuery * query);
129 static gboolean gst_adder_src_event (GstPad * pad, GstEvent * event);
130 static gboolean gst_adder_sink_event (GstPad * pad, GstEvent * event);
131
132 static GstPad *gst_adder_request_new_pad (GstElement * element,
133     GstPadTemplate * temp, const gchar * unused);
134 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
135
136 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
137     GstStateChange transition);
138
139 static GstBuffer *gst_adder_do_clip (GstCollectPads * pads,
140     GstCollectData * data, GstBuffer * buffer, gpointer user_data);
141 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
142     gpointer user_data);
143
144 static GstElementClass *parent_class = NULL;
145
146 GType
147 gst_adder_get_type (void)
148 {
149   static GType adder_type = 0;
150
151   if (G_UNLIKELY (adder_type == 0)) {
152     static const GTypeInfo adder_info = {
153       sizeof (GstAdderClass), NULL, NULL,
154       (GClassInitFunc) gst_adder_class_init, NULL, NULL,
155       sizeof (GstAdder), 0,
156       (GInstanceInitFunc) gst_adder_init,
157     };
158
159     adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
160         &adder_info, 0);
161     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
162         "audio channel mixing element");
163   }
164   return adder_type;
165 }
166
167 /* clipping versions (for int)
168  * FIXME: what about: oil_add_s16 (out, out, in, bytes / sizeof (type))
169  */
170 #define MAKE_FUNC(name,type,ttype,min,max)                      \
171 static void name (type *out, type *in, gint bytes) {            \
172   gint i;                                                       \
173   ttype add;                                                    \
174   for (i = 0; i < bytes / sizeof (type); i++) {                 \
175     add = (ttype)out[i] + (ttype)in[i];                         \
176     out[i] = CLAMP (add, min, max);                             \
177   }                                                             \
178 }
179
180 /* unsigned versions (for int) */
181 #define MAKE_FUNC_US(name,type,ttype,max)                       \
182 static void name (type *out, type *in, gint bytes) {            \
183   gint i;                                                       \
184   ttype add;                                                    \
185   for (i = 0; i < bytes / sizeof (type); i++) {                 \
186     add = (ttype)out[i] + (ttype)in[i];                         \
187     out[i] = ((add <= max) ? add : max);                        \
188   }                                                             \
189 }
190
191 /* non-clipping versions (for float) */
192 #define MAKE_FUNC_NC(name,type)                                 \
193 static void name (type *out, type *in, gint bytes) {            \
194   gint i;                                                       \
195   for (i = 0; i < bytes / sizeof (type); i++)                   \
196     out[i] += in[i];                                            \
197 }
198
199 #if 0
200 /* right now, the liboil function don't seems to be faster on x86
201  * time gst-launch audiotestsrc num-buffers=50000 ! audio/x-raw-float ! adder name=m ! fakesink audiotestsrc num-buffers=50000 ! audio/x-raw-float ! m.
202  * time gst-launch audiotestsrc num-buffers=50000 ! audio/x-raw-float,width=32 ! adder name=m ! fakesink audiotestsrc num-buffers=50000 ! audio/x-raw-float,width=32 ! m.
203  */
204 static void
205 add_float32 (gfloat * out, gfloat * in, gint bytes)
206 {
207   oil_add_f32 (out, out, in, bytes / sizeof (gfloat));
208 }
209
210 static void
211 add_float64 (gdouble * out, gdouble * in, gint bytes)
212 {
213   oil_add_f64 (out, out, in, bytes / sizeof (gdouble));
214 }
215 #endif
216
217 /* *INDENT-OFF* */
218 MAKE_FUNC (add_int32, gint32, gint64, MIN_INT_32, MAX_INT_32)
219 MAKE_FUNC (add_int16, gint16, gint32, MIN_INT_16, MAX_INT_16)
220 MAKE_FUNC (add_int8, gint8, gint16, MIN_INT_8, MAX_INT_8)
221 MAKE_FUNC_US (add_uint32, guint32, guint64, MAX_UINT_32)
222 MAKE_FUNC_US (add_uint16, guint16, guint32, MAX_UINT_16)
223 MAKE_FUNC_US (add_uint8, guint8, guint16, MAX_UINT_8)
224 MAKE_FUNC_NC (add_float64, gdouble)
225 MAKE_FUNC_NC (add_float32, gfloat)
226 /* *INDENT-ON* */
227
228 /* we can only accept caps that we and downstream can handle.
229  * if we have filtercaps set, use those to constrain the target caps.
230  */
231 static GstCaps *
232 gst_adder_sink_getcaps (GstPad * pad)
233 {
234   GstAdder *adder;
235   GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
236
237   adder = GST_ADDER (GST_PAD_PARENT (pad));
238
239   GST_OBJECT_LOCK (adder);
240   /* take filter */
241   if ((filter_caps = adder->filter_caps))
242     gst_caps_ref (filter_caps);
243   GST_OBJECT_UNLOCK (adder);
244
245   /* get the downstream possible caps */
246   peercaps = gst_pad_peer_get_caps (adder->srcpad);
247
248   /* get the allowed caps on this sinkpad, we use the fixed caps function so
249    * that it does not call recursively in this function. */
250   sinkcaps = gst_pad_get_fixed_caps_func (pad);
251   if (peercaps) {
252     /* restrict with filter-caps if any */
253     if (filter_caps) {
254       GST_DEBUG_OBJECT (adder, "filtering peer caps");
255       result = gst_caps_intersect (peercaps, filter_caps);
256       gst_caps_unref (peercaps);
257       peercaps = result;
258     }
259     /* if the peer has caps, intersect */
260     GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
261     result = gst_caps_intersect (peercaps, sinkcaps);
262     gst_caps_unref (peercaps);
263     gst_caps_unref (sinkcaps);
264   } else {
265     /* the peer has no caps (or there is no peer), just use the allowed caps
266      * of this sinkpad. */
267     /* restrict with filter-caps if any */
268     if (filter_caps) {
269       GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
270       result = gst_caps_intersect (sinkcaps, filter_caps);
271       gst_caps_unref (sinkcaps);
272     } else {
273       GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
274       result = sinkcaps;
275     }
276   }
277
278   if (filter_caps)
279     gst_caps_unref (filter_caps);
280
281   GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
282       GST_PAD_NAME (pad), result);
283
284   return result;
285 }
286
287 /* the first caps we receive on any of the sinkpads will define the caps for all
288  * the other sinkpads because we can only mix streams with the same caps.
289  */
290 static gboolean
291 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
292 {
293   GstAdder *adder;
294   GList *pads;
295   GstStructure *structure;
296   const char *media_type;
297
298   adder = GST_ADDER (GST_PAD_PARENT (pad));
299
300   GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
301       GST_PAD_NAME (pad), caps);
302
303   /* FIXME, see if the other pads can accept the format. Also lock the
304    * format on the other pads to this new format. */
305   GST_OBJECT_LOCK (adder);
306   pads = GST_ELEMENT (adder)->pads;
307   while (pads) {
308     GstPad *otherpad = GST_PAD (pads->data);
309
310     if (otherpad != pad) {
311       gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
312     }
313     pads = g_list_next (pads);
314   }
315   GST_OBJECT_UNLOCK (adder);
316
317   /* parse caps now */
318   structure = gst_caps_get_structure (caps, 0);
319   media_type = gst_structure_get_name (structure);
320   if (strcmp (media_type, "audio/x-raw-int") == 0) {
321     adder->format = GST_ADDER_FORMAT_INT;
322     gst_structure_get_int (structure, "width", &adder->width);
323     gst_structure_get_int (structure, "depth", &adder->depth);
324     gst_structure_get_int (structure, "endianness", &adder->endianness);
325     gst_structure_get_boolean (structure, "signed", &adder->is_signed);
326
327     GST_INFO_OBJECT (pad, "parse_caps sets adder to format int, %d bit",
328         adder->width);
329
330     if (adder->endianness != G_BYTE_ORDER)
331       goto not_supported;
332
333     switch (adder->width) {
334       case 8:
335         adder->func = (adder->is_signed ?
336             (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
337         break;
338       case 16:
339         adder->func = (adder->is_signed ?
340             (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
341         break;
342       case 32:
343         adder->func = (adder->is_signed ?
344             (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
345         break;
346       default:
347         goto not_supported;
348     }
349   } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
350     adder->format = GST_ADDER_FORMAT_FLOAT;
351     gst_structure_get_int (structure, "width", &adder->width);
352     gst_structure_get_int (structure, "endianness", &adder->endianness);
353
354     GST_INFO_OBJECT (pad, "parse_caps sets adder to format float, %d bit",
355         adder->width);
356
357     if (adder->endianness != G_BYTE_ORDER)
358       goto not_supported;
359
360     switch (adder->width) {
361       case 32:
362         adder->func = (GstAdderFunction) add_float32;
363         break;
364       case 64:
365         adder->func = (GstAdderFunction) add_float64;
366         break;
367       default:
368         goto not_supported;
369     }
370   } else {
371     goto not_supported;
372   }
373
374   gst_structure_get_int (structure, "channels", &adder->channels);
375   gst_structure_get_int (structure, "rate", &adder->rate);
376   /* precalc bps */
377   adder->bps = (adder->width / 8) * adder->channels;
378
379   return TRUE;
380
381   /* ERRORS */
382 not_supported:
383   {
384     GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
385     return FALSE;
386   }
387 }
388
389 /* FIXME, the duration query should reflect how long you will produce
390  * data, that is the amount of stream time until you will emit EOS.
391  *
392  * For synchronized mixing this is always the max of all the durations
393  * of upstream since we emit EOS when all of them finished.
394  *
395  * We don't do synchronized mixing so this really depends on where the
396  * streams where punched in and what their relative offsets are against
397  * eachother which we can get from the first timestamps we see.
398  *
399  * When we add a new stream (or remove a stream) the duration might
400  * also become invalid again and we need to post a new DURATION
401  * message to notify this fact to the parent.
402  * For now we take the max of all the upstream elements so the simple
403  * cases work at least somewhat.
404  */
405 static gboolean
406 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
407 {
408   gint64 max;
409   gboolean res;
410   GstFormat format;
411   GstIterator *it;
412   gboolean done;
413
414   /* parse format */
415   gst_query_parse_duration (query, &format, NULL);
416
417   max = -1;
418   res = TRUE;
419   done = FALSE;
420
421   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
422   while (!done) {
423     GstIteratorResult ires;
424
425     gpointer item;
426
427     ires = gst_iterator_next (it, &item);
428     switch (ires) {
429       case GST_ITERATOR_DONE:
430         done = TRUE;
431         break;
432       case GST_ITERATOR_OK:
433       {
434         GstPad *pad = GST_PAD_CAST (item);
435
436         gint64 duration;
437
438         /* ask sink peer for duration */
439         res &= gst_pad_query_peer_duration (pad, &format, &duration);
440         /* take max from all valid return values */
441         if (res) {
442           /* valid unknown length, stop searching */
443           if (duration == -1) {
444             max = duration;
445             done = TRUE;
446           }
447           /* else see if bigger than current max */
448           else if (duration > max)
449             max = duration;
450         }
451         gst_object_unref (pad);
452         break;
453       }
454       case GST_ITERATOR_RESYNC:
455         max = -1;
456         res = TRUE;
457         gst_iterator_resync (it);
458         break;
459       default:
460         res = FALSE;
461         done = TRUE;
462         break;
463     }
464   }
465   gst_iterator_free (it);
466
467   if (res) {
468     /* and store the max */
469     GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
470         GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
471     gst_query_set_duration (query, format, max);
472   }
473
474   return res;
475 }
476
477 static gboolean
478 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
479 {
480   GstClockTime min, max;
481   gboolean live;
482   gboolean res;
483   GstIterator *it;
484   gboolean done;
485
486   res = TRUE;
487   done = FALSE;
488
489   live = FALSE;
490   min = 0;
491   max = GST_CLOCK_TIME_NONE;
492
493   /* Take maximum of all latency values */
494   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
495   while (!done) {
496     GstIteratorResult ires;
497
498     gpointer item;
499
500     ires = gst_iterator_next (it, &item);
501     switch (ires) {
502       case GST_ITERATOR_DONE:
503         done = TRUE;
504         break;
505       case GST_ITERATOR_OK:
506       {
507         GstPad *pad = GST_PAD_CAST (item);
508         GstQuery *peerquery;
509         GstClockTime min_cur, max_cur;
510         gboolean live_cur;
511
512         peerquery = gst_query_new_latency ();
513
514         /* Ask peer for latency */
515         res &= gst_pad_peer_query (pad, peerquery);
516
517         /* take max from all valid return values */
518         if (res) {
519           gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
520
521           if (min_cur > min)
522             min = min_cur;
523
524           if (max_cur != GST_CLOCK_TIME_NONE &&
525               ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
526                   (max == GST_CLOCK_TIME_NONE)))
527             max = max_cur;
528
529           live = live || live_cur;
530         }
531
532         gst_query_unref (peerquery);
533         gst_object_unref (pad);
534         break;
535       }
536       case GST_ITERATOR_RESYNC:
537         live = FALSE;
538         min = 0;
539         max = GST_CLOCK_TIME_NONE;
540         res = TRUE;
541         gst_iterator_resync (it);
542         break;
543       default:
544         res = FALSE;
545         done = TRUE;
546         break;
547     }
548   }
549   gst_iterator_free (it);
550
551   if (res) {
552     /* store the results */
553     GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
554         GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
555         (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
556     gst_query_set_latency (query, live, min, max);
557   }
558
559   return res;
560 }
561
562 static gboolean
563 gst_adder_query (GstPad * pad, GstQuery * query)
564 {
565   GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
566   gboolean res = FALSE;
567
568   switch (GST_QUERY_TYPE (query)) {
569     case GST_QUERY_POSITION:
570     {
571       GstFormat format;
572
573       gst_query_parse_position (query, &format, NULL);
574
575       switch (format) {
576         case GST_FORMAT_TIME:
577           /* FIXME, bring to stream time, might be tricky */
578           gst_query_set_position (query, format, adder->timestamp);
579           res = TRUE;
580           break;
581         case GST_FORMAT_DEFAULT:
582           gst_query_set_position (query, format, adder->offset);
583           res = TRUE;
584           break;
585         default:
586           break;
587       }
588       break;
589     }
590     case GST_QUERY_DURATION:
591       res = gst_adder_query_duration (adder, query);
592       break;
593     case GST_QUERY_LATENCY:
594       res = gst_adder_query_latency (adder, query);
595       break;
596     default:
597       /* FIXME, needs a custom query handler because we have multiple
598        * sinkpads */
599       res = gst_pad_query_default (pad, query);
600       break;
601   }
602
603   gst_object_unref (adder);
604   return res;
605 }
606
607 typedef struct
608 {
609   GstEvent *event;
610   gboolean flush;
611 } EventData;
612
613 static gboolean
614 forward_event_func (GstPad * pad, GValue * ret, EventData * data)
615 {
616   GstEvent *event = data->event;
617
618   gst_event_ref (event);
619   GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
620   if (!gst_pad_push_event (pad, event)) {
621     GST_WARNING_OBJECT (pad, "Sending event  %p (%s) failed.",
622         event, GST_EVENT_TYPE_NAME (event));
623     /* quick hack to unflush the pads, ideally we need a way to just unflush
624      * this single collect pad */
625     if (data->flush)
626       gst_pad_send_event (pad, gst_event_new_flush_stop ());
627   } else {
628     g_value_set_boolean (ret, TRUE);
629     GST_LOG_OBJECT (pad, "Sent event  %p (%s).",
630         event, GST_EVENT_TYPE_NAME (event));
631   }
632   gst_object_unref (pad);
633
634   /* continue on other pads, even if one failed */
635   return TRUE;
636 }
637
638 /* forwards the event to all sinkpads, takes ownership of the
639  * event
640  *
641  * Returns: TRUE if the event could be forwarded on all
642  * sinkpads.
643  */
644 static gboolean
645 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
646 {
647   gboolean ret;
648   GstIterator *it;
649   GstIteratorResult ires;
650   GValue vret = { 0 };
651   EventData data;
652
653   GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
654       GST_EVENT_TYPE_NAME (event));
655
656   data.event = event;
657   data.flush = flush;
658
659   g_value_init (&vret, G_TYPE_BOOLEAN);
660   g_value_set_boolean (&vret, FALSE);
661   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
662   while (TRUE) {
663     ires = gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
664         &vret, &data);
665     switch (ires) {
666       case GST_ITERATOR_RESYNC:
667         GST_WARNING ("resync");
668         gst_iterator_resync (it);
669         g_value_set_boolean (&vret, TRUE);
670         break;
671       case GST_ITERATOR_OK:
672       case GST_ITERATOR_DONE:
673         ret = g_value_get_boolean (&vret);
674         goto done;
675       default:
676         ret = FALSE;
677         goto done;
678     }
679   }
680 done:
681   gst_iterator_free (it);
682   GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
683       GST_EVENT_TYPE_NAME (event), ret);
684   gst_event_unref (event);
685
686   return ret;
687 }
688
689 static gboolean
690 gst_adder_src_event (GstPad * pad, GstEvent * event)
691 {
692   GstAdder *adder;
693   gboolean result;
694
695   adder = GST_ADDER (gst_pad_get_parent (pad));
696
697   switch (GST_EVENT_TYPE (event)) {
698     case GST_EVENT_SEEK:
699     {
700       GstSeekFlags flags;
701       GstSeekType curtype, endtype;
702       gint64 cur, end;
703       gboolean flush;
704
705       /* parse the seek parameters */
706       gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
707           &cur, &endtype, &end);
708
709       flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
710
711       /* check if we are flushing */
712       if (flush) {
713         /* make sure we accept nothing anymore and return WRONG_STATE */
714         gst_collect_pads_set_flushing (adder->collect, TRUE);
715
716         /* flushing seek, start flush downstream, the flush will be done
717          * when all pads received a FLUSH_STOP. */
718         gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
719       }
720       GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
721
722       /* now wait for the collected to be finished and mark a new
723        * segment. After we have the lock, no collect function is running and no
724        * new collect function will be called for as long as we're flushing. */
725       GST_OBJECT_LOCK (adder->collect);
726       if (curtype == GST_SEEK_TYPE_SET)
727         adder->segment_start = cur;
728       else
729         adder->segment_start = 0;
730       if (endtype == GST_SEEK_TYPE_SET)
731         adder->segment_end = end;
732       else
733         adder->segment_end = GST_CLOCK_TIME_NONE;
734       /* make sure we push a new segment, to inform about new basetime
735        * see FIXME in gst_adder_collected() */
736       adder->segment_pending = TRUE;
737       if (flush) {
738         /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
739          * have stopped so that the cookie gets properly updated. */
740         gst_collect_pads_set_flushing (adder->collect, TRUE);
741       }
742       /* we might have a pending flush_stop event now. This event will either be
743        * sent by an upstream element when it completes the seek or we will push
744        * one in the collected callback ourself */
745       adder->flush_stop_pending = flush;
746       GST_OBJECT_UNLOCK (adder->collect);
747       GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
748           event);
749
750       result = forward_event (adder, event, flush);
751       if (!result) {
752         /* seek failed. maybe source is a live source. */
753         GST_DEBUG_OBJECT (adder, "seeking failed");
754       }
755       /* FIXME: ideally we would like to send a flush-stop event from here but
756        * collectpads does not have a method that allows us to do that. Instead
757        * we forward all flush-stop events we receive on the sinkpads. We might
758        * be sending too many flush-stop events. */
759       break;
760     }
761     case GST_EVENT_QOS:
762       /* QoS might be tricky */
763       result = FALSE;
764       break;
765     case GST_EVENT_NAVIGATION:
766       /* navigation is rather pointless. */
767       result = FALSE;
768       break;
769     default:
770       /* just forward the rest for now */
771       GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
772           GST_EVENT_TYPE_NAME (event));
773       result = forward_event (adder, event, FALSE);
774       break;
775   }
776   gst_object_unref (adder);
777
778   return result;
779 }
780
781 static gboolean
782 gst_adder_sink_event (GstPad * pad, GstEvent * event)
783 {
784   GstAdder *adder;
785   gboolean ret = TRUE;
786
787   adder = GST_ADDER (gst_pad_get_parent (pad));
788
789   GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
790       GST_DEBUG_PAD_NAME (pad));
791
792   switch (GST_EVENT_TYPE (event)) {
793     case GST_EVENT_FLUSH_STOP:
794       /* we received a flush-stop. The collect_event function will push the
795        * event past our element. We simply forward all flush-stop events, even
796        * when no flush-stop was pending, this is required because collectpads
797        * does not provide an API to handle-but-not-forward the flush-stop.
798        * We unset the pending flush-stop flag so that we don't send anymore
799        * flush-stop from the collect function later.
800        */
801       GST_OBJECT_LOCK (adder->collect);
802       adder->segment_pending = TRUE;
803       adder->flush_stop_pending = FALSE;
804       /* Clear pending tags */
805       if (adder->pending_events) {
806         g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
807         g_list_free (adder->pending_events);
808         adder->pending_events = NULL;
809       }
810       GST_OBJECT_UNLOCK (adder->collect);
811       break;
812     case GST_EVENT_TAG:
813       GST_OBJECT_LOCK (adder->collect);
814       /* collect tags here so we can push them out when we collect data */
815       adder->pending_events = g_list_append (adder->pending_events, event);
816       GST_OBJECT_UNLOCK (adder->collect);
817       goto beach;
818     default:
819       break;
820   }
821
822   /* now GstCollectPads can take care of the rest, e.g. EOS */
823   ret = adder->collect_event (pad, event);
824
825 beach:
826   gst_object_unref (adder);
827   return ret;
828 }
829
830 static void
831 gst_adder_class_init (GstAdderClass * klass)
832 {
833   GObjectClass *gobject_class = (GObjectClass *) klass;
834   GstElementClass *gstelement_class = (GstElementClass *) klass;
835
836   gobject_class->set_property = gst_adder_set_property;
837   gobject_class->get_property = gst_adder_get_property;
838   gobject_class->dispose = gst_adder_dispose;
839
840   gst_element_class_add_pad_template (gstelement_class,
841       gst_static_pad_template_get (&gst_adder_src_template));
842   gst_element_class_add_pad_template (gstelement_class,
843       gst_static_pad_template_get (&gst_adder_sink_template));
844   gst_element_class_set_details_simple (gstelement_class, "Adder",
845       "Generic/Audio",
846       "Add N audio channels together",
847       "Thomas Vander Stichele <thomas at apestaart dot org>");
848
849   parent_class = g_type_class_peek_parent (klass);
850
851   /**
852    * GstAdder:caps:
853    *
854    * Since: 0.10.24
855    */
856   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
857       g_param_spec_boxed ("caps", "Target caps",
858           "Set target format for mixing (NULL means ANY). "
859           "Setting this property takes a reference to the supplied GstCaps "
860           "object.", GST_TYPE_CAPS,
861           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
862
863   gstelement_class->request_new_pad =
864       GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
865   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
866   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
867 }
868
869 static void
870 gst_adder_init (GstAdder * adder)
871 {
872   GstPadTemplate *template;
873
874   template = gst_static_pad_template_get (&gst_adder_src_template);
875   adder->srcpad = gst_pad_new_from_template (template, "src");
876   gst_object_unref (template);
877
878   gst_pad_set_getcaps_function (adder->srcpad,
879       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
880   gst_pad_set_setcaps_function (adder->srcpad,
881       GST_DEBUG_FUNCPTR (gst_adder_setcaps));
882   gst_pad_set_query_function (adder->srcpad,
883       GST_DEBUG_FUNCPTR (gst_adder_query));
884   gst_pad_set_event_function (adder->srcpad,
885       GST_DEBUG_FUNCPTR (gst_adder_src_event));
886   gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
887
888   adder->format = GST_ADDER_FORMAT_UNSET;
889   adder->padcount = 0;
890   adder->func = NULL;
891
892   adder->filter_caps = NULL;
893
894   /* keep track of the sinkpads requested */
895   adder->collect = gst_collect_pads_new ();
896   gst_collect_pads_set_function (adder->collect,
897       GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
898   gst_collect_pads_set_clip_function (adder->collect,
899       GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
900 }
901
902 static void
903 gst_adder_dispose (GObject * object)
904 {
905   GstAdder *adder = GST_ADDER (object);
906
907   if (adder->collect) {
908     gst_object_unref (adder->collect);
909     adder->collect = NULL;
910   }
911   gst_caps_replace (&adder->filter_caps, NULL);
912   if (adder->pending_events) {
913     g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
914     g_list_free (adder->pending_events);
915     adder->pending_events = NULL;
916   }
917
918   G_OBJECT_CLASS (parent_class)->dispose (object);
919 }
920
921 static void
922 gst_adder_set_property (GObject * object, guint prop_id,
923     const GValue * value, GParamSpec * pspec)
924 {
925   GstAdder *adder = GST_ADDER (object);
926
927   switch (prop_id) {
928     case PROP_FILTER_CAPS:{
929       GstCaps *new_caps = NULL;
930       GstCaps *old_caps;
931       const GstCaps *new_caps_val = gst_value_get_caps (value);
932
933       if (new_caps_val != NULL) {
934         new_caps = (GstCaps *) new_caps_val;
935         gst_caps_ref (new_caps);
936       }
937
938       GST_OBJECT_LOCK (adder);
939       old_caps = adder->filter_caps;
940       adder->filter_caps = new_caps;
941       GST_OBJECT_UNLOCK (adder);
942
943       if (old_caps)
944         gst_caps_unref (old_caps);
945
946       GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
947       break;
948     }
949     default:
950       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
951       break;
952   }
953 }
954
955 static void
956 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
957     GParamSpec * pspec)
958 {
959   GstAdder *adder = GST_ADDER (object);
960
961   switch (prop_id) {
962     case PROP_FILTER_CAPS:
963       GST_OBJECT_LOCK (adder);
964       gst_value_set_caps (value, adder->filter_caps);
965       GST_OBJECT_UNLOCK (adder);
966       break;
967     default:
968       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
969       break;
970   }
971 }
972
973
974 static GstPad *
975 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
976     const gchar * unused)
977 {
978   gchar *name;
979   GstAdder *adder;
980   GstPad *newpad;
981   gint padcount;
982
983   if (templ->direction != GST_PAD_SINK)
984     goto not_sink;
985
986   adder = GST_ADDER (element);
987
988   /* increment pad counter */
989   padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
990
991   name = g_strdup_printf ("sink%d", padcount);
992   newpad = gst_pad_new_from_template (templ, name);
993   GST_DEBUG_OBJECT (adder, "request new pad %s", name);
994   g_free (name);
995
996   gst_pad_set_getcaps_function (newpad,
997       GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
998   gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
999   gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
1000
1001   /* FIXME: hacked way to override/extend the event function of
1002    * GstCollectPads; because it sets its own event function giving the
1003    * element no access to events */
1004   adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
1005   gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
1006
1007   /* takes ownership of the pad */
1008   if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
1009     goto could_not_add;
1010
1011   return newpad;
1012
1013   /* errors */
1014 not_sink:
1015   {
1016     g_warning ("gstadder: request new pad that is not a SINK pad\n");
1017     return NULL;
1018   }
1019 could_not_add:
1020   {
1021     GST_DEBUG_OBJECT (adder, "could not add pad");
1022     gst_collect_pads_remove_pad (adder->collect, newpad);
1023     gst_object_unref (newpad);
1024     return NULL;
1025   }
1026 }
1027
1028 static void
1029 gst_adder_release_pad (GstElement * element, GstPad * pad)
1030 {
1031   GstAdder *adder;
1032
1033   adder = GST_ADDER (element);
1034
1035   GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1036
1037   gst_collect_pads_remove_pad (adder->collect, pad);
1038   gst_element_remove_pad (element, pad);
1039 }
1040
1041 static GstBuffer *
1042 gst_adder_do_clip (GstCollectPads * pads, GstCollectData * data,
1043     GstBuffer * buffer, gpointer user_data)
1044 {
1045   GstAdder *adder = GST_ADDER (user_data);
1046
1047   buffer = gst_audio_buffer_clip (buffer, &data->segment, adder->rate,
1048       adder->bps);
1049
1050   return buffer;
1051 }
1052
1053 static GstFlowReturn
1054 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
1055 {
1056   /*
1057    * combine streams by adding data values
1058    * basic algorithm :
1059    * - this function is called when all pads have a buffer
1060    * - get available bytes on all pads.
1061    * - repeat for each input pad :
1062    *   - read available bytes, copy or add to target buffer
1063    *   - if there's an EOS event, remove the input channel
1064    * - push out the output buffer
1065    *
1066    * todo:
1067    * - would be nice to have a mixing mode, where instead of adding we mix
1068    *   - for float we could downscale after collect loop
1069    *   - for int we need to downscale each input to avoid clipping or
1070    *     mix into a temp (float) buffer and scale afterwards as well
1071    */
1072   GstAdder *adder;
1073   GSList *collected, *next = NULL;
1074   GstFlowReturn ret;
1075   GstBuffer *outbuf = NULL, *gapbuf = NULL;
1076   gpointer outdata = NULL;
1077   guint outsize;
1078   gint64 next_offset;
1079   gint64 next_timestamp;
1080
1081   adder = GST_ADDER (user_data);
1082
1083   /* this is fatal */
1084   if (G_UNLIKELY (adder->func == NULL))
1085     goto not_negotiated;
1086
1087   if (adder->flush_stop_pending) {
1088     gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop ());
1089     adder->flush_stop_pending = FALSE;
1090   }
1091
1092   /* get available bytes for reading, this can be 0 which could mean empty
1093    * buffers or EOS, which we will catch when we loop over the pads. */
1094   outsize = gst_collect_pads_available (pads);
1095   /* can only happen when no pads to collect or all EOS */
1096   if (outsize == 0)
1097     goto eos;
1098
1099   GST_LOG_OBJECT (adder,
1100       "starting to cycle through channels, %d bytes available (bps = %d)",
1101       outsize, adder->bps);
1102
1103   for (collected = pads->data; collected; collected = next) {
1104     GstCollectData *collect_data;
1105     GstBuffer *inbuf;
1106     gboolean is_gap;
1107
1108     /* take next to see if this is the last collectdata */
1109     next = g_slist_next (collected);
1110
1111     collect_data = (GstCollectData *) collected->data;
1112
1113     /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1114      * bytes big. */
1115     inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
1116     /* NULL means EOS or an empty buffer so we still need to flush in
1117      * case of an empty buffer. */
1118     if (inbuf == NULL) {
1119       GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1120       continue;
1121     }
1122
1123     is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1124
1125     /* Try to make an output buffer */
1126     if (outbuf == NULL) {
1127       /* if this is a gap buffer but we have some more pads to check, skip it.
1128        * If we are at the last buffer, take it, regardless if it is a GAP
1129        * buffer or not. */
1130       if (is_gap && next) {
1131         GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1132         /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1133          * EOS, we can use this one as the output buffer. */
1134         if (gapbuf == NULL)
1135           gapbuf = inbuf;
1136         else
1137           gst_buffer_unref (inbuf);
1138         continue;
1139       }
1140
1141       GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1142           collect_data, outsize);
1143       /* make data and metadata writable, can simply return the inbuf when we
1144        * are the only one referencing this buffer. If this is the last (and
1145        * only) GAP buffer, it will automatically copy the GAP flag. */
1146       outbuf = gst_buffer_make_writable (inbuf);
1147       outdata = GST_BUFFER_DATA (outbuf);
1148       gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
1149     } else {
1150       if (!is_gap) {
1151         /* we had a previous output buffer, mix this non-GAP buffer */
1152         guint8 *indata;
1153         guint insize;
1154
1155         indata = GST_BUFFER_DATA (inbuf);
1156         insize = GST_BUFFER_SIZE (inbuf);
1157
1158         /* all buffers should have outsize, there are no short buffers because we
1159          * asked for the max size above */
1160         g_assert (insize == outsize);
1161
1162         GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
1163             collect_data, insize, indata);
1164
1165         /* further buffers, need to add them */
1166         adder->func ((gpointer) outdata, (gpointer) indata, insize);
1167       } else {
1168         /* skip gap buffer */
1169         GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1170       }
1171       gst_buffer_unref (inbuf);
1172     }
1173   }
1174
1175   if (outbuf == NULL) {
1176     /* no output buffer, reuse one of the GAP buffers then if we have one */
1177     if (gapbuf) {
1178       GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1179       outbuf = gapbuf;
1180     } else
1181       /* assume EOS otherwise, this should not happen, really */
1182       goto eos;
1183   } else if (gapbuf)
1184     /* we had an output buffer, unref the gapbuffer we kept */
1185     gst_buffer_unref (gapbuf);
1186
1187   if (adder->segment_pending) {
1188     GstEvent *event;
1189
1190     /* FIXME, use rate/applied_rate as set on all sinkpads.
1191      * - currently we just set rate as received from last seek-event
1192      *
1193      * When seeking we set the start and stop positions as given in the seek
1194      * event. We also adjust offset & timestamp acordingly.
1195      * This basically ignores all newsegments sent by upstream.
1196      */
1197     event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
1198         1.0, GST_FORMAT_TIME, adder->segment_start, adder->segment_end,
1199         adder->segment_start);
1200     if (adder->segment_rate > 0.0) {
1201       adder->timestamp = adder->segment_start;
1202     } else {
1203       adder->timestamp = adder->segment_end;
1204     }
1205     adder->offset = gst_util_uint64_scale (adder->timestamp,
1206         adder->rate, GST_SECOND);
1207     GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1208         G_GUINT64_FORMAT, adder->segment_start, adder->segment_end);
1209     GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1210         G_GINT64_FORMAT, adder->timestamp, adder->offset);
1211
1212     if (event) {
1213       if (!gst_pad_push_event (adder->srcpad, event)) {
1214         GST_WARNING_OBJECT (adder->srcpad, "Sending event  %p (%s) failed.",
1215             event, GST_EVENT_TYPE_NAME (event));
1216       }
1217       adder->segment_pending = FALSE;
1218     } else {
1219       GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1220           "start:%" G_GINT64_FORMAT "  end:%" G_GINT64_FORMAT " failed",
1221           adder->segment_start, adder->segment_end);
1222     }
1223   }
1224
1225   if (G_UNLIKELY (adder->pending_events)) {
1226     GList *tmp = adder->pending_events;
1227
1228     while (tmp) {
1229       GstEvent *ev = (GstEvent *) tmp->data;
1230
1231       gst_pad_push_event (adder->srcpad, ev);
1232       tmp = g_list_next (tmp);
1233     }
1234     g_list_free (adder->pending_events);
1235     adder->pending_events = NULL;
1236   }
1237
1238   /* for the next timestamp, use the sample counter, which will
1239    * never accumulate rounding errors */
1240   if (adder->segment_rate > 0.0) {
1241     next_offset = adder->offset + outsize / adder->bps;
1242   } else {
1243     next_offset = adder->offset - outsize / adder->bps;
1244   }
1245   next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, adder->rate);
1246
1247
1248   /* set timestamps on the output buffer */
1249   if (adder->segment_rate > 0.0) {
1250     GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
1251     GST_BUFFER_OFFSET (outbuf) = adder->offset;
1252     GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->timestamp;
1253   } else {
1254     GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1255     GST_BUFFER_OFFSET (outbuf) = next_offset;
1256     GST_BUFFER_DURATION (outbuf) = adder->timestamp - next_timestamp;
1257   }
1258
1259   adder->offset = next_offset;
1260   adder->timestamp = next_timestamp;
1261
1262   /* send it out */
1263   GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1264       " offset %" G_GINT64_FORMAT, outbuf,
1265       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1266       GST_BUFFER_OFFSET (outbuf));
1267   ret = gst_pad_push (adder->srcpad, outbuf);
1268
1269   GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1270
1271   return ret;
1272
1273   /* ERRORS */
1274 not_negotiated:
1275   {
1276     GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1277         ("Unknown data received, not negotiated"));
1278     return GST_FLOW_NOT_NEGOTIATED;
1279   }
1280 eos:
1281   {
1282     GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1283     gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1284     return GST_FLOW_UNEXPECTED;
1285   }
1286 }
1287
1288 static GstStateChangeReturn
1289 gst_adder_change_state (GstElement * element, GstStateChange transition)
1290 {
1291   GstAdder *adder;
1292   GstStateChangeReturn ret;
1293
1294   adder = GST_ADDER (element);
1295
1296   switch (transition) {
1297     case GST_STATE_CHANGE_NULL_TO_READY:
1298       break;
1299     case GST_STATE_CHANGE_READY_TO_PAUSED:
1300       adder->timestamp = 0;
1301       adder->offset = 0;
1302       adder->flush_stop_pending = FALSE;
1303       adder->segment_pending = TRUE;
1304       adder->segment_start = 0;
1305       adder->segment_end = GST_CLOCK_TIME_NONE;
1306       adder->segment_rate = 1.0;
1307       gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1308       gst_collect_pads_start (adder->collect);
1309       break;
1310     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1311       break;
1312     case GST_STATE_CHANGE_PAUSED_TO_READY:
1313       /* need to unblock the collectpads before calling the
1314        * parent change_state so that streaming can finish */
1315       gst_collect_pads_stop (adder->collect);
1316       break;
1317     default:
1318       break;
1319   }
1320
1321   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1322
1323   switch (transition) {
1324     default:
1325       break;
1326   }
1327
1328   return ret;
1329 }
1330
1331
1332 static gboolean
1333 plugin_init (GstPlugin * plugin)
1334 {
1335   /*oil_init (); */
1336
1337   if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1338     return FALSE;
1339   }
1340
1341   return TRUE;
1342 }
1343
1344 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1345     GST_VERSION_MINOR,
1346     "adder",
1347     "Adds multiple streams",
1348     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)