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