fa1f957e7cafcbf1fecc8f5d6e5f27eb5c55f0f4
[platform/upstream/gstreamer.git] / gst / adder / gstadder.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2001 Thomas <thomas@apestaart.org>
4  *               2005,2006 Wim Taymans <wim@fluendo.com>
5  *
6  * adder.c: Adder element, N in, one out, samples are added
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:element-adder
25  *
26  * The adder allows to mix several streams into one by adding the data.
27  * Mixed data is clamped to the min/max values of the data format.
28  *
29  * The adder currently mixes all data received on the sinkpads as soon as
30  * possible without trying to synchronize the streams.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36  * ]| This pipeline produces two sine waves mixed together.
37  * </refsect2>
38  *
39  * Last reviewed on 2006-05-09 (0.10.7)
40  */
41 /* Element-Checklist-Version: 5 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #include "gstadder.h"
47 #include <gst/audio/audio.h>
48 #include <string.h>             /* strcmp */
49 #include "gstadderorc.h"
50
51 /* highest positive/lowest negative x-bit value we can use for clamping */
52 #define MAX_INT_32  ((gint32) (0x7fffffff))
53 #define MAX_INT_16  ((gint16) (0x7fff))
54 #define MAX_INT_8   ((gint8)  (0x7f))
55 #define MAX_UINT_32 ((guint32)(0xffffffff))
56 #define MAX_UINT_16 ((guint16)(0xffff))
57 #define MAX_UINT_8  ((guint8) (0xff))
58
59 #define MIN_INT_32  ((gint32) (0x80000000))
60 #define MIN_INT_16  ((gint16) (0x8000))
61 #define MIN_INT_8   ((gint8)  (0x80))
62 #define MIN_UINT_32 ((guint32)(0x00000000))
63 #define MIN_UINT_16 ((guint16)(0x0000))
64 #define MIN_UINT_8  ((guint8) (0x00))
65
66 enum
67 {
68   PROP_0,
69   PROP_FILTER_CAPS
70 };
71
72 #define GST_CAT_DEFAULT gst_adder_debug
73 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
74
75 /* elementfactory information */
76
77 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
78 #define CAPS \
79   GST_AUDIO_CAPS_MAKE ("{ S32LE, U32LE, S16LE, U16LE, S8, U8, F32LE, F64LE }") \
80   ", layout = (string) { interleaved, non-interleaved }"
81 #else
82 #define CAPS \
83   GST_AUDIO_CAPS_MAKE ("{ S32BE, U32BE, S16BE, U16BE, S8, U8, F32BE, F64BE }") \
84   ", layout = (string) { interleaved, non-interleaved }"
85 #endif
86
87 static GstStaticPadTemplate gst_adder_src_template =
88 GST_STATIC_PAD_TEMPLATE ("src",
89     GST_PAD_SRC,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (CAPS)
92     );
93
94 static GstStaticPadTemplate gst_adder_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink_%u",
96     GST_PAD_SINK,
97     GST_PAD_REQUEST,
98     GST_STATIC_CAPS (CAPS)
99     );
100
101 #define gst_adder_parent_class parent_class
102 G_DEFINE_TYPE (GstAdder, gst_adder, GST_TYPE_ELEMENT);
103
104 static void gst_adder_dispose (GObject * object);
105 static void gst_adder_set_property (GObject * object, guint prop_id,
106     const GValue * value, GParamSpec * pspec);
107 static void gst_adder_get_property (GObject * object, guint prop_id,
108     GValue * value, GParamSpec * pspec);
109
110 static gboolean gst_adder_setcaps (GstAdder * adder, GstPad * pad,
111     GstCaps * caps);
112 static gboolean gst_adder_src_query (GstPad * pad, GstObject * parent,
113     GstQuery * query);
114 static gboolean gst_adder_sink_query (GstPad * pad, GstObject * parent,
115     GstQuery * query);
116 static gboolean gst_adder_src_event (GstPad * pad, GstObject * parent,
117     GstEvent * event);
118 static gboolean gst_adder_sink_event (GstPad * pad, GstObject * parent,
119     GstEvent * event);
120
121 static GstPad *gst_adder_request_new_pad (GstElement * element,
122     GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
123 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
124
125 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
126     GstStateChange transition);
127
128 static GstFlowReturn gst_adder_do_clip (GstCollectPads2 * pads,
129     GstCollectData2 * data, GstBuffer * buffer, GstBuffer ** out,
130     gpointer user_data);
131 static GstFlowReturn gst_adder_collected (GstCollectPads2 * pads,
132     gpointer user_data);
133 static gboolean gst_adder_event (GstCollectPads2 * pads, GstCollectData2 * pad,
134     GstEvent * event, gpointer user_data);
135
136 /* non-clipping versions (for float) */
137 #define MAKE_FUNC_NC(name,type)                                 \
138 static void name (type *out, type *in, gint samples) {          \
139   gint i;                                                       \
140   for (i = 0; i < samples; i++)                                 \
141     out[i] += in[i];                                            \
142 }
143
144 /* *INDENT-OFF* */
145 MAKE_FUNC_NC (add_float64, gdouble)
146 /* *INDENT-ON* */
147
148 /* we can only accept caps that we and downstream can handle.
149  * if we have filtercaps set, use those to constrain the target caps.
150  */
151 static GstCaps *
152 gst_adder_sink_getcaps (GstPad * pad, GstCaps * filter)
153 {
154   GstAdder *adder;
155   GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
156
157   adder = GST_ADDER (GST_PAD_PARENT (pad));
158
159   GST_OBJECT_LOCK (adder);
160   /* take filter */
161   if ((filter_caps = adder->filter_caps)) {
162     if (filter)
163       filter_caps =
164           gst_caps_intersect_full (filter, filter_caps,
165           GST_CAPS_INTERSECT_FIRST);
166     else
167       gst_caps_ref (filter_caps);
168   } else {
169     filter_caps = filter ? gst_caps_ref (filter) : NULL;
170   }
171   GST_OBJECT_UNLOCK (adder);
172
173   if (filter_caps && gst_caps_is_empty (filter_caps)) {
174     GST_WARNING_OBJECT (pad, "Empty filter caps");
175     return filter_caps;
176   }
177
178   /* get the downstream possible caps */
179   peercaps = gst_pad_peer_query_caps (adder->srcpad, filter_caps);
180
181   /* get the allowed caps on this sinkpad */
182   sinkcaps = gst_pad_get_current_caps (pad);
183   if (sinkcaps == NULL) {
184     sinkcaps = gst_pad_get_pad_template_caps (pad);
185     if (!sinkcaps)
186       sinkcaps = gst_caps_new_any ();
187   }
188
189   if (peercaps) {
190     /* if the peer has caps, intersect */
191     GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
192     result =
193         gst_caps_intersect_full (peercaps, sinkcaps, GST_CAPS_INTERSECT_FIRST);
194     gst_caps_unref (peercaps);
195     gst_caps_unref (sinkcaps);
196   } else {
197     /* the peer has no caps (or there is no peer), just use the allowed caps
198      * of this sinkpad. */
199     /* restrict with filter-caps if any */
200     if (filter_caps) {
201       GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
202       result =
203           gst_caps_intersect_full (filter_caps, sinkcaps,
204           GST_CAPS_INTERSECT_FIRST);
205       gst_caps_unref (sinkcaps);
206     } else {
207       GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
208       result = sinkcaps;
209     }
210   }
211
212   if (filter_caps)
213     gst_caps_unref (filter_caps);
214
215   GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
216       GST_PAD_NAME (pad), result);
217
218   return result;
219 }
220
221 static gboolean
222 gst_adder_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
223 {
224   gboolean res = FALSE;
225
226   switch (GST_QUERY_TYPE (query)) {
227     case GST_QUERY_CAPS:
228     {
229       GstCaps *filter, *caps;
230
231       gst_query_parse_caps (query, &filter);
232       caps = gst_adder_sink_getcaps (pad, filter);
233       gst_query_set_caps_result (query, caps);
234       gst_caps_unref (caps);
235       res = TRUE;
236       break;
237     }
238     default:
239       res = gst_pad_query_default (pad, parent, query);
240       break;
241   }
242   return res;
243 }
244
245 typedef struct
246 {
247   GstPad *pad;
248   GstCaps *caps;
249 } IterData;
250
251 static void
252 setcapsfunc (const GValue * item, IterData * data)
253 {
254   GstPad *otherpad = g_value_get_object (item);
255
256   if (otherpad != data->pad)
257     gst_pad_set_caps (data->pad, data->caps);
258 }
259
260 /* the first caps we receive on any of the sinkpads will define the caps for all
261  * the other sinkpads because we can only mix streams with the same caps.
262  */
263 static gboolean
264 gst_adder_setcaps (GstAdder * adder, GstPad * pad, GstCaps * caps)
265 {
266   GstIterator *it;
267   GstIteratorResult ires;
268   IterData idata;
269   gboolean done;
270
271   /* this get called recursively due to gst_iterator_foreach  calling
272    * gst_pad_set_caps() */
273   if (adder->in_setcaps)
274     return TRUE;
275
276   GST_LOG_OBJECT (adder, "setting caps pad %p,%s to %" GST_PTR_FORMAT, pad,
277       GST_PAD_NAME (pad), caps);
278
279   it = gst_element_iterate_pads (GST_ELEMENT_CAST (adder));
280
281   /* FIXME, see if the other pads can accept the format. Also lock the
282    * format on the other pads to this new format. */
283   idata.caps = caps;
284   idata.pad = pad;
285
286   adder->in_setcaps = TRUE;
287   done = FALSE;
288   while (!done) {
289     ires = gst_iterator_foreach (it, (GstIteratorForeachFunction) setcapsfunc,
290         &idata);
291
292     switch (ires) {
293       case GST_ITERATOR_RESYNC:
294         gst_iterator_resync (it);
295         break;
296       default:
297         done = TRUE;
298         break;
299     }
300   }
301   adder->in_setcaps = FALSE;
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) add_int8;
312       break;
313     case GST_AUDIO_FORMAT_U8:
314       adder->func = (GstAdderFunction) add_uint8;
315       break;
316     case GST_AUDIO_FORMAT_S16:
317       adder->func = (GstAdderFunction) add_int16;
318       break;
319     case GST_AUDIO_FORMAT_U16:
320       adder->func = (GstAdderFunction) add_uint16;
321       break;
322     case GST_AUDIO_FORMAT_S32:
323       adder->func = (GstAdderFunction) add_int32;
324       break;
325     case GST_AUDIO_FORMAT_U32:
326       adder->func = (GstAdderFunction) add_uint32;
327       break;
328     case GST_AUDIO_FORMAT_F32:
329       adder->func = (GstAdderFunction) add_float32;
330       break;
331     case GST_AUDIO_FORMAT_F64:
332       adder->func = (GstAdderFunction) 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 typedef struct
564 {
565   GstEvent *event;
566   gboolean flush;
567 } EventData;
568
569 static gboolean
570 forward_event_func (const GValue * val, GValue * ret, EventData * data)
571 {
572   GstPad *pad = g_value_get_object (val);
573   GstEvent *event = data->event;
574
575   gst_event_ref (event);
576   GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
577   if (!gst_pad_push_event (pad, event)) {
578     GST_WARNING_OBJECT (pad, "Sending event  %p (%s) failed.",
579         event, GST_EVENT_TYPE_NAME (event));
580     /* quick hack to unflush the pads, ideally we need a way to just unflush
581      * this single collect pad */
582     if (data->flush)
583       gst_pad_send_event (pad, gst_event_new_flush_stop (TRUE));
584   } else {
585     g_value_set_boolean (ret, TRUE);
586     GST_LOG_OBJECT (pad, "Sent event  %p (%s).",
587         event, GST_EVENT_TYPE_NAME (event));
588   }
589
590   /* continue on other pads, even if one failed */
591   return TRUE;
592 }
593
594 /* forwards the event to all sinkpads, takes ownership of the
595  * event
596  *
597  * Returns: TRUE if the event could be forwarded on all
598  * sinkpads.
599  */
600 static gboolean
601 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
602 {
603   gboolean ret;
604   GstIterator *it;
605   GstIteratorResult ires;
606   GValue vret = { 0 };
607   EventData data;
608
609   GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
610       GST_EVENT_TYPE_NAME (event));
611
612   data.event = event;
613   data.flush = flush;
614
615   g_value_init (&vret, G_TYPE_BOOLEAN);
616   g_value_set_boolean (&vret, FALSE);
617   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
618   while (TRUE) {
619     ires =
620         gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
621         &vret, &data);
622     switch (ires) {
623       case GST_ITERATOR_RESYNC:
624         GST_WARNING ("resync");
625         gst_iterator_resync (it);
626         g_value_set_boolean (&vret, TRUE);
627         break;
628       case GST_ITERATOR_OK:
629       case GST_ITERATOR_DONE:
630         ret = g_value_get_boolean (&vret);
631         goto done;
632       default:
633         ret = FALSE;
634         goto done;
635     }
636   }
637 done:
638   gst_iterator_free (it);
639   GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
640       GST_EVENT_TYPE_NAME (event), ret);
641   gst_event_unref (event);
642
643   return ret;
644 }
645
646 static gboolean
647 gst_adder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
648 {
649   GstAdder *adder;
650   gboolean result;
651
652   adder = GST_ADDER (parent);
653
654   GST_DEBUG_OBJECT (pad, "Got %s event on src pad",
655       GST_EVENT_TYPE_NAME (event));
656
657   switch (GST_EVENT_TYPE (event)) {
658     case GST_EVENT_SEEK:
659     {
660       GstSeekFlags flags;
661       gdouble rate;
662       GstSeekType curtype, endtype;
663       gint64 cur, end;
664       gboolean flush;
665
666       /* parse the seek parameters */
667       gst_event_parse_seek (event, &rate, NULL, &flags, &curtype,
668           &cur, &endtype, &end);
669
670       if ((curtype != GST_SEEK_TYPE_NONE) && (curtype != GST_SEEK_TYPE_SET)) {
671         result = FALSE;
672         GST_DEBUG_OBJECT (adder,
673             "seeking failed, unhandled seek type for start: %d", curtype);
674         goto done;
675       }
676       if ((endtype != GST_SEEK_TYPE_NONE) && (endtype != GST_SEEK_TYPE_SET)) {
677         result = FALSE;
678         GST_DEBUG_OBJECT (adder,
679             "seeking failed, unhandled seek type for end: %d", endtype);
680         goto done;
681       }
682
683       flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
684
685       /* check if we are flushing */
686       if (flush) {
687         /* flushing seek, start flush downstream, the flush will be done
688          * when all pads received a FLUSH_STOP.
689          * Make sure we accept nothing anymore and return WRONG_STATE.
690          * We send a flush-start before, to ensure no streaming is done
691          * as we need to take the stream lock.
692          */
693         gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
694         gst_collect_pads2_set_flushing (adder->collect, TRUE);
695
696         /* We can't send FLUSH_STOP here since upstream could start pushing data
697          * after we unlock adder->collect.
698          * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
699          * forwarding the seek upstream or from gst_adder_collected,
700          * whichever happens first.
701          */
702         g_atomic_int_set (&adder->flush_stop_pending, TRUE);
703       }
704       GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
705
706       /* now wait for the collected to be finished and mark a new
707        * segment. After we have the lock, no collect function is running and no
708        * new collect function will be called for as long as we're flushing. */
709       GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
710       adder->segment.rate = rate;
711       if (curtype == GST_SEEK_TYPE_SET)
712         adder->segment.start = cur;
713       else
714         adder->segment.start = 0;
715       if (endtype == GST_SEEK_TYPE_SET)
716         adder->segment.stop = end;
717       else
718         adder->segment.stop = GST_CLOCK_TIME_NONE;
719       if (flush) {
720         /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
721          * have stopped so that the cookie gets properly updated. */
722         gst_collect_pads2_set_flushing (adder->collect, TRUE);
723       }
724       GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
725       GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
726           event);
727
728       /* we're forwarding seek to all upstream peers and wait for one to reply
729        * with a newsegment-event before we send a newsegment-event downstream */
730       g_atomic_int_set (&adder->wait_for_new_segment, TRUE);
731       result = forward_event (adder, event, flush);
732       if (!result) {
733         /* seek failed. maybe source is a live source. */
734         GST_DEBUG_OBJECT (adder, "seeking failed");
735       }
736       if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
737               TRUE, FALSE)) {
738         GST_DEBUG_OBJECT (adder, "pending flush stop");
739         gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
740       }
741       break;
742     }
743     case GST_EVENT_QOS:
744       /* QoS might be tricky */
745       result = FALSE;
746       break;
747     case GST_EVENT_NAVIGATION:
748       /* navigation is rather pointless. */
749       result = FALSE;
750       break;
751     default:
752       /* just forward the rest for now */
753       GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
754           GST_EVENT_TYPE_NAME (event));
755       result = forward_event (adder, event, FALSE);
756       break;
757   }
758
759 done:
760
761   return result;
762 }
763
764 static gboolean
765 gst_adder_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
766 {
767   GstAdder *adder;
768   gboolean ret = TRUE;
769
770   adder = GST_ADDER (parent);
771
772   GST_DEBUG_OBJECT (pad, "Got %s event on sink pad",
773       GST_EVENT_TYPE_NAME (event));
774
775   switch (GST_EVENT_TYPE (event)) {
776     case GST_EVENT_CAPS:
777     {
778       GstCaps *caps;
779
780       gst_event_parse_caps (event, &caps);
781       ret = gst_adder_setcaps (adder, pad, caps);
782       gst_event_unref (event);
783
784       goto beach;
785     }
786     case GST_EVENT_FLUSH_STOP:
787       /* we received a flush-stop. The collect_event function will call the
788        * gst_adder_event function we have set on the GstCollectPads2, so we
789        * have control over whether the event is sent past our element.
790        * We will only forward it when flush_stop_pending is set, and we will
791        * unset it then.
792        */
793       GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
794       g_atomic_int_set (&adder->new_segment_pending, TRUE);
795       /* Clear pending tags */
796       if (adder->pending_events) {
797         g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
798         g_list_free (adder->pending_events);
799         adder->pending_events = NULL;
800       }
801       GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
802       break;
803     case GST_EVENT_TAG:
804       GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
805       /* collect tags here so we can push them out when we collect data */
806       adder->pending_events = g_list_append (adder->pending_events, event);
807       GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
808       goto beach;
809     case GST_EVENT_SEGMENT:
810       if (g_atomic_int_compare_and_exchange (&adder->wait_for_new_segment,
811               TRUE, FALSE)) {
812         /* make sure we push a new segment, to inform about new basetime
813          * see FIXME in gst_adder_collected() */
814         g_atomic_int_set (&adder->new_segment_pending, TRUE);
815       }
816       break;
817     default:
818       break;
819   }
820
821   /* now GstCollectPads2 can take care of the rest, e.g. EOS */
822   ret = adder->collect_event (pad, parent, event);
823
824 beach:
825   return ret;
826 }
827
828 static void
829 gst_adder_class_init (GstAdderClass * klass)
830 {
831   GObjectClass *gobject_class = (GObjectClass *) klass;
832   GstElementClass *gstelement_class = (GstElementClass *) klass;
833
834   gobject_class->set_property = gst_adder_set_property;
835   gobject_class->get_property = gst_adder_get_property;
836   gobject_class->dispose = gst_adder_dispose;
837
838   /**
839    * GstAdder:caps:
840    *
841    * Since: 0.10.24
842    */
843   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
844       g_param_spec_boxed ("caps", "Target caps",
845           "Set target format for mixing (NULL means ANY). "
846           "Setting this property takes a reference to the supplied GstCaps "
847           "object.", GST_TYPE_CAPS,
848           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
849
850   gst_element_class_add_pad_template (gstelement_class,
851       gst_static_pad_template_get (&gst_adder_src_template));
852   gst_element_class_add_pad_template (gstelement_class,
853       gst_static_pad_template_get (&gst_adder_sink_template));
854   gst_element_class_set_details_simple (gstelement_class, "Adder",
855       "Generic/Audio",
856       "Add N audio channels together",
857       "Thomas Vander Stichele <thomas at apestaart dot org>");
858
859   gstelement_class->request_new_pad =
860       GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
861   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
862   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
863 }
864
865 static void
866 gst_adder_init (GstAdder * adder)
867 {
868   GstPadTemplate *template;
869
870   template = gst_static_pad_template_get (&gst_adder_src_template);
871   adder->srcpad = gst_pad_new_from_template (template, "src");
872   gst_object_unref (template);
873
874   gst_pad_set_query_function (adder->srcpad,
875       GST_DEBUG_FUNCPTR (gst_adder_src_query));
876   gst_pad_set_event_function (adder->srcpad,
877       GST_DEBUG_FUNCPTR (gst_adder_src_event));
878   GST_PAD_SET_PROXY_CAPS (adder->srcpad);
879   gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
880
881   gst_audio_info_init (&adder->info);
882   adder->padcount = 0;
883   adder->func = NULL;
884
885   adder->filter_caps = NULL;
886
887   /* keep track of the sinkpads requested */
888   adder->collect = gst_collect_pads2_new ();
889   gst_collect_pads2_set_function (adder->collect,
890       GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
891   gst_collect_pads2_set_clip_function (adder->collect,
892       GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
893   gst_collect_pads2_set_event_function (adder->collect,
894       GST_DEBUG_FUNCPTR (gst_adder_event), adder);
895 }
896
897 static void
898 gst_adder_dispose (GObject * object)
899 {
900   GstAdder *adder = GST_ADDER (object);
901
902   if (adder->collect) {
903     gst_object_unref (adder->collect);
904     adder->collect = NULL;
905   }
906   gst_caps_replace (&adder->filter_caps, NULL);
907   if (adder->pending_events) {
908     g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
909     g_list_free (adder->pending_events);
910     adder->pending_events = NULL;
911   }
912
913   G_OBJECT_CLASS (parent_class)->dispose (object);
914 }
915
916 static void
917 gst_adder_set_property (GObject * object, guint prop_id,
918     const GValue * value, GParamSpec * pspec)
919 {
920   GstAdder *adder = GST_ADDER (object);
921
922   switch (prop_id) {
923     case PROP_FILTER_CAPS:{
924       GstCaps *new_caps = NULL;
925       GstCaps *old_caps;
926       const GstCaps *new_caps_val = gst_value_get_caps (value);
927
928       if (new_caps_val != NULL) {
929         new_caps = (GstCaps *) new_caps_val;
930         gst_caps_ref (new_caps);
931       }
932
933       GST_OBJECT_LOCK (adder);
934       old_caps = adder->filter_caps;
935       adder->filter_caps = new_caps;
936       GST_OBJECT_UNLOCK (adder);
937
938       if (old_caps)
939         gst_caps_unref (old_caps);
940
941       GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
942       break;
943     }
944     default:
945       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
946       break;
947   }
948 }
949
950 static void
951 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
952     GParamSpec * pspec)
953 {
954   GstAdder *adder = GST_ADDER (object);
955
956   switch (prop_id) {
957     case PROP_FILTER_CAPS:
958       GST_OBJECT_LOCK (adder);
959       gst_value_set_caps (value, adder->filter_caps);
960       GST_OBJECT_UNLOCK (adder);
961       break;
962     default:
963       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
964       break;
965   }
966 }
967
968
969 static GstPad *
970 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
971     const gchar * unused, const GstCaps * caps)
972 {
973   gchar *name;
974   GstAdder *adder;
975   GstPad *newpad;
976   gint padcount;
977
978   if (templ->direction != GST_PAD_SINK)
979     goto not_sink;
980
981   adder = GST_ADDER (element);
982
983   /* increment pad counter */
984 #if GLIB_CHECK_VERSION(2,29,5)
985   padcount = g_atomic_int_add (&adder->padcount, 1);
986 #else
987   padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
988 #endif
989
990   name = g_strdup_printf ("sink_%u", padcount);
991   newpad = gst_pad_new_from_template (templ, name);
992   GST_DEBUG_OBJECT (adder, "request new pad %s", name);
993   g_free (name);
994
995   gst_pad_set_query_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_query));
996   gst_collect_pads2_add_pad (adder->collect, newpad, sizeof (GstCollectData2));
997
998   /* FIXME: hacked way to override/extend the event function of
999    * GstCollectPads2; because it sets its own event function giving the
1000    * element no access to events */
1001   adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
1002   gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
1003
1004   /* takes ownership of the pad */
1005   if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
1006     goto could_not_add;
1007
1008   return newpad;
1009
1010   /* errors */
1011 not_sink:
1012   {
1013     g_warning ("gstadder: request new pad that is not a SINK pad\n");
1014     return NULL;
1015   }
1016 could_not_add:
1017   {
1018     GST_DEBUG_OBJECT (adder, "could not add pad");
1019     gst_collect_pads2_remove_pad (adder->collect, newpad);
1020     gst_object_unref (newpad);
1021     return NULL;
1022   }
1023 }
1024
1025 static void
1026 gst_adder_release_pad (GstElement * element, GstPad * pad)
1027 {
1028   GstAdder *adder;
1029
1030   adder = GST_ADDER (element);
1031
1032   GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1033
1034   gst_collect_pads2_remove_pad (adder->collect, pad);
1035   gst_element_remove_pad (element, pad);
1036 }
1037
1038 static GstFlowReturn
1039 gst_adder_do_clip (GstCollectPads2 * pads, GstCollectData2 * data,
1040     GstBuffer * buffer, GstBuffer ** out, gpointer user_data)
1041 {
1042   GstAdder *adder = GST_ADDER (user_data);
1043   gint rate, bpf;
1044
1045   rate = GST_AUDIO_INFO_RATE (&adder->info);
1046   bpf = GST_AUDIO_INFO_BPF (&adder->info);
1047
1048   buffer = gst_audio_buffer_clip (buffer, &data->segment, rate, bpf);
1049
1050   *out = buffer;
1051   return GST_FLOW_OK;
1052 }
1053
1054 static GstFlowReturn
1055 gst_adder_collected (GstCollectPads2 * pads, gpointer user_data)
1056 {
1057   /*
1058    * combine streams by adding data values
1059    * basic algorithm :
1060    * - this function is called when all pads have a buffer
1061    * - get available bytes on all pads.
1062    * - repeat for each input pad :
1063    *   - read available bytes, copy or add to target buffer
1064    *   - if there's an EOS event, remove the input channel
1065    * - push out the output buffer
1066    *
1067    * todo:
1068    * - would be nice to have a mixing mode, where instead of adding we mix
1069    *   - for float we could downscale after collect loop
1070    *   - for int we need to downscale each input to avoid clipping or
1071    *     mix into a temp (float) buffer and scale afterwards as well
1072    */
1073   GstAdder *adder;
1074   GSList *collected, *next = NULL;
1075   GstFlowReturn ret;
1076   GstBuffer *outbuf = NULL, *gapbuf = NULL;
1077   gpointer outdata = NULL;
1078   guint outsize;
1079   gint64 next_offset;
1080   gint64 next_timestamp;
1081   gint rate, bps, bpf;
1082
1083   adder = GST_ADDER (user_data);
1084
1085   /* this is fatal */
1086   if (G_UNLIKELY (adder->func == NULL))
1087     goto not_negotiated;
1088
1089   if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1090           TRUE, FALSE)) {
1091     GST_DEBUG_OBJECT (adder, "pending flush stop");
1092     gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
1093   }
1094
1095   /* get available bytes for reading, this can be 0 which could mean empty
1096    * buffers or EOS, which we will catch when we loop over the pads. */
1097   outsize = gst_collect_pads2_available (pads);
1098   /* can only happen when no pads to collect or all EOS */
1099   if (outsize == 0)
1100     goto eos;
1101
1102   rate = GST_AUDIO_INFO_RATE (&adder->info);
1103   bps = GST_AUDIO_INFO_BPS (&adder->info);
1104   bpf = GST_AUDIO_INFO_BPF (&adder->info);
1105
1106   GST_LOG_OBJECT (adder,
1107       "starting to cycle through channels, %d bytes available (bps = %d, bpf = %d)",
1108       outsize, bps, bpf);
1109
1110   for (collected = pads->data; collected; collected = next) {
1111     GstCollectData2 *collect_data;
1112     GstBuffer *inbuf;
1113     gboolean is_gap;
1114
1115     /* take next to see if this is the last collectdata */
1116     next = g_slist_next (collected);
1117
1118     collect_data = (GstCollectData2 *) collected->data;
1119
1120     /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1121      * bytes big. */
1122     inbuf = gst_collect_pads2_take_buffer (pads, collect_data, outsize);
1123     /* NULL means EOS or an empty buffer so we still need to flush in
1124      * case of an empty buffer. */
1125     if (inbuf == NULL) {
1126       GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1127       continue;
1128     }
1129
1130     is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1131
1132     /* Try to make an output buffer */
1133     if (outbuf == NULL) {
1134       /* if this is a gap buffer but we have some more pads to check, skip it.
1135        * If we are at the last buffer, take it, regardless if it is a GAP
1136        * buffer or not. */
1137       if (is_gap && next) {
1138         GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1139         /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1140          * EOS, we can use this one as the output buffer. */
1141         if (gapbuf == NULL)
1142           gapbuf = inbuf;
1143         else
1144           gst_buffer_unref (inbuf);
1145         continue;
1146       }
1147
1148       GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1149           collect_data, outsize);
1150
1151       /* make data and metadata writable, can simply return the inbuf when we
1152        * are the only one referencing this buffer. If this is the last (and
1153        * only) GAP buffer, it will automatically copy the GAP flag. */
1154       outbuf = gst_buffer_make_writable (inbuf);
1155       outdata = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
1156     } else {
1157       if (!is_gap) {
1158         /* we had a previous output buffer, mix this non-GAP buffer */
1159         guint8 *indata;
1160         gsize insize;
1161
1162         indata = gst_buffer_map (inbuf, &insize, NULL, 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 (insize == outsize);
1167
1168         GST_LOG_OBJECT (adder, "channel %p: mixing %" G_GSIZE_FORMAT " bytes"
1169             " from data %p", collect_data, insize, indata);
1170
1171         /* further buffers, need to add them */
1172         adder->func ((gpointer) outdata, (gpointer) indata, insize / bps);
1173         gst_buffer_unmap (inbuf, indata, insize);
1174       } else {
1175         /* skip gap buffer */
1176         GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1177       }
1178       gst_buffer_unref (inbuf);
1179     }
1180   }
1181   if (outbuf)
1182     gst_buffer_unmap (outbuf, outdata, outsize);
1183
1184   if (outbuf == NULL) {
1185     /* no output buffer, reuse one of the GAP buffers then if we have one */
1186     if (gapbuf) {
1187       GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1188       outbuf = gapbuf;
1189     } else
1190       /* assume EOS otherwise, this should not happen, really */
1191       goto eos;
1192   } else if (gapbuf)
1193     /* we had an output buffer, unref the gapbuffer we kept */
1194     gst_buffer_unref (gapbuf);
1195
1196   if (g_atomic_int_compare_and_exchange (&adder->new_segment_pending, TRUE,
1197           FALSE)) {
1198     GstEvent *event;
1199
1200     /* FIXME, use rate/applied_rate as set on all sinkpads.
1201      * - currently we just set rate as received from last seek-event
1202      *
1203      * When seeking we set the start and stop positions as given in the seek
1204      * event. We also adjust offset & timestamp accordingly.
1205      * This basically ignores all newsegments sent by upstream.
1206      */
1207     event = gst_event_new_segment (&adder->segment);
1208
1209     if (adder->segment.rate > 0.0) {
1210       adder->segment.position = adder->segment.start;
1211     } else {
1212       adder->segment.position = adder->segment.stop;
1213     }
1214     adder->offset = gst_util_uint64_scale (adder->segment.position,
1215         rate, GST_SECOND);
1216     GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1217         G_GUINT64_FORMAT, adder->segment.start, adder->segment.stop);
1218     GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1219         G_GINT64_FORMAT, adder->segment.position, adder->offset);
1220
1221     if (event) {
1222       if (!gst_pad_push_event (adder->srcpad, event)) {
1223         GST_WARNING_OBJECT (adder->srcpad, "Sending event failed");
1224       }
1225     } else {
1226       GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1227           "start:%" G_GINT64_FORMAT "  end:%" G_GINT64_FORMAT " failed",
1228           adder->segment.start, adder->segment.stop);
1229     }
1230   }
1231
1232   if (G_UNLIKELY (adder->pending_events)) {
1233     GList *tmp = adder->pending_events;
1234
1235     while (tmp) {
1236       GstEvent *ev = (GstEvent *) tmp->data;
1237
1238       gst_pad_push_event (adder->srcpad, ev);
1239       tmp = g_list_next (tmp);
1240     }
1241     g_list_free (adder->pending_events);
1242     adder->pending_events = NULL;
1243   }
1244
1245   /* for the next timestamp, use the sample counter, which will
1246    * never accumulate rounding errors */
1247   if (adder->segment.rate > 0.0) {
1248     next_offset = adder->offset + outsize / bpf;
1249   } else {
1250     next_offset = adder->offset - outsize / bpf;
1251   }
1252   next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, rate);
1253
1254
1255   /* set timestamps on the output buffer */
1256   if (adder->segment.rate > 0.0) {
1257     GST_BUFFER_TIMESTAMP (outbuf) = adder->segment.position;
1258     GST_BUFFER_OFFSET (outbuf) = adder->offset;
1259     GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1260     GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->segment.position;
1261   } else {
1262     GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1263     GST_BUFFER_OFFSET (outbuf) = next_offset;
1264     GST_BUFFER_OFFSET_END (outbuf) = adder->offset;
1265     GST_BUFFER_DURATION (outbuf) = adder->segment.position - next_timestamp;
1266   }
1267
1268   adder->offset = next_offset;
1269   adder->segment.position = next_timestamp;
1270
1271   /* send it out */
1272   GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1273       " offset %" G_GINT64_FORMAT, outbuf,
1274       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1275       GST_BUFFER_OFFSET (outbuf));
1276   ret = gst_pad_push (adder->srcpad, outbuf);
1277
1278   GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1279
1280   return ret;
1281
1282   /* ERRORS */
1283 not_negotiated:
1284   {
1285     GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1286         ("Unknown data received, not negotiated"));
1287     return GST_FLOW_NOT_NEGOTIATED;
1288   }
1289 eos:
1290   {
1291     GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1292     gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1293     return GST_FLOW_EOS;
1294   }
1295 }
1296
1297 static gboolean
1298 gst_adder_event (GstCollectPads2 * pads, GstCollectData2 * pad,
1299     GstEvent * event, gpointer user_data)
1300 {
1301   GstAdder *adder = GST_ADDER (user_data);
1302   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1303     if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1304             TRUE, FALSE)) {
1305       return FALSE;
1306     } else {
1307       gst_event_unref (event);
1308       return TRUE;
1309     }
1310   } else {
1311     return FALSE;
1312   }
1313 }
1314
1315 static GstStateChangeReturn
1316 gst_adder_change_state (GstElement * element, GstStateChange transition)
1317 {
1318   GstAdder *adder;
1319   GstStateChangeReturn ret;
1320
1321   adder = GST_ADDER (element);
1322
1323   switch (transition) {
1324     case GST_STATE_CHANGE_NULL_TO_READY:
1325       break;
1326     case GST_STATE_CHANGE_READY_TO_PAUSED:
1327       adder->segment.position = 0;
1328       adder->offset = 0;
1329       adder->flush_stop_pending = FALSE;
1330       adder->new_segment_pending = TRUE;
1331       adder->wait_for_new_segment = FALSE;
1332       gst_segment_init (&adder->segment, GST_FORMAT_TIME);
1333       gst_collect_pads2_start (adder->collect);
1334       break;
1335     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1336       break;
1337     case GST_STATE_CHANGE_PAUSED_TO_READY:
1338       /* need to unblock the collectpads2 before calling the
1339        * parent change_state so that streaming can finish */
1340       gst_collect_pads2_stop (adder->collect);
1341       break;
1342     default:
1343       break;
1344   }
1345
1346   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1347
1348   switch (transition) {
1349     default:
1350       break;
1351   }
1352
1353   return ret;
1354 }
1355
1356
1357 static gboolean
1358 plugin_init (GstPlugin * plugin)
1359 {
1360   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
1361       "audio channel mixing element");
1362
1363   gst_adder_orc_init ();
1364
1365   if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1366     return FALSE;
1367   }
1368
1369   return TRUE;
1370 }
1371
1372 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1373     GST_VERSION_MINOR,
1374     "adder",
1375     "Adds multiple streams",
1376     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)