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