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