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