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