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>
6 * adder.c: Adder element, N in, one out, samples are added
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.
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.
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.
24 * SECTION:element-adder
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.
29 * The adder currently mixes all data received on the sinkpads as soon as
30 * possible without trying to synchronize the streams.
33 * <title>Example launch line</title>
35 * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36 * ]| This pipeline produces two sine waves mixed together.
39 * Last reviewed on 2006-05-09 (0.10.7)
41 /* Element-Checklist-Version: 5 */
47 #include <gst/audio/audio.h>
48 #include <string.h> /* strcmp */
49 /*#include <liboil/liboil.h>*/
51 /* highest positive/lowest negative x-bit value we can use for clamping */
52 #define MAX_INT_32 ((gint32) (0x7fffffff))
53 #define MAX_INT_16 ((gint16) (0x7fff))
54 #define MAX_INT_8 ((gint8) (0x7f))
55 #define MAX_UINT_32 ((guint32)(0xffffffff))
56 #define MAX_UINT_16 ((guint16)(0xffff))
57 #define MAX_UINT_8 ((guint8) (0xff))
59 #define MIN_INT_32 ((gint32) (0x80000000))
60 #define MIN_INT_16 ((gint16) (0x8000))
61 #define MIN_INT_8 ((gint8) (0x80))
62 #define MIN_UINT_32 ((guint32)(0x00000000))
63 #define MIN_UINT_16 ((guint16)(0x0000))
64 #define MIN_UINT_8 ((guint8) (0x00))
66 #define GST_CAT_DEFAULT gst_adder_debug
67 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
69 /* elementfactory information */
73 "rate = (int) [ 1, MAX ], " \
74 "channels = (int) [ 1, MAX ], " \
75 "endianness = (int) BYTE_ORDER, " \
76 "width = (int) 32, " \
77 "depth = (int) 32, " \
78 "signed = (boolean) { true, false } ;" \
80 "rate = (int) [ 1, MAX ], " \
81 "channels = (int) [ 1, MAX ], " \
82 "endianness = (int) BYTE_ORDER, " \
83 "width = (int) 16, " \
84 "depth = (int) 16, " \
85 "signed = (boolean) { true, false } ;" \
87 "rate = (int) [ 1, MAX ], " \
88 "channels = (int) [ 1, MAX ], " \
89 "endianness = (int) BYTE_ORDER, " \
92 "signed = (boolean) { true, false } ;" \
93 "audio/x-raw-float, " \
94 "rate = (int) [ 1, MAX ], " \
95 "channels = (int) [ 1, MAX ], " \
96 "endianness = (int) BYTE_ORDER, " \
97 "width = (int) { 32, 64 }"
99 static GstStaticPadTemplate gst_adder_src_template =
100 GST_STATIC_PAD_TEMPLATE ("src",
103 GST_STATIC_CAPS (CAPS)
106 static GstStaticPadTemplate gst_adder_sink_template =
107 GST_STATIC_PAD_TEMPLATE ("sink%d",
110 GST_STATIC_CAPS (CAPS)
113 static void gst_adder_class_init (GstAdderClass * klass);
114 static void gst_adder_init (GstAdder * adder);
115 static void gst_adder_finalize (GObject * object);
117 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
118 static gboolean gst_adder_query (GstPad * pad, GstQuery * query);
119 static gboolean gst_adder_src_event (GstPad * pad, GstEvent * event);
120 static gboolean gst_adder_sink_event (GstPad * pad, GstEvent * event);
122 static GstPad *gst_adder_request_new_pad (GstElement * element,
123 GstPadTemplate * temp, const gchar * unused);
124 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
126 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
127 GstStateChange transition);
129 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
132 static GstElementClass *parent_class = NULL;
135 gst_adder_get_type (void)
137 static GType adder_type = 0;
139 if (G_UNLIKELY (adder_type == 0)) {
140 static const GTypeInfo adder_info = {
141 sizeof (GstAdderClass), NULL, NULL,
142 (GClassInitFunc) gst_adder_class_init, NULL, NULL,
143 sizeof (GstAdder), 0,
144 (GInstanceInitFunc) gst_adder_init,
147 adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
149 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
150 "audio channel mixing element");
155 /* clipping versions (for int)
156 * FIXME: what about: oil_add_s16 (out, out, in, bytes / sizeof (type))
158 #define MAKE_FUNC(name,type,ttype,min,max) \
159 static void name (type *out, type *in, gint bytes) { \
162 for (i = 0; i < bytes / sizeof (type); i++) { \
163 add = (ttype)out[i] + (ttype)in[i]; \
164 out[i] = CLAMP (add, min, max); \
168 /* unsigned versions (for int) */
169 #define MAKE_FUNC_US(name,type,ttype,max) \
170 static void name (type *out, type *in, gint bytes) { \
173 for (i = 0; i < bytes / sizeof (type); i++) { \
174 add = (ttype)out[i] + (ttype)in[i]; \
175 out[i] = ((add <= max) ? add : max); \
179 /* non-clipping versions (for float) */
180 #define MAKE_FUNC_NC(name,type) \
181 static void name (type *out, type *in, gint bytes) { \
183 for (i = 0; i < bytes / sizeof (type); i++) \
188 /* right now, the liboil function don't seems to be faster on x86
189 * time gst-launch audiotestsrc num-buffers=50000 ! audio/x-raw-float ! adder name=m ! fakesink audiotestsrc num-buffers=50000 ! audio/x-raw-float ! m.
190 * time gst-launch audiotestsrc num-buffers=50000 ! audio/x-raw-float,width=32 ! adder name=m ! fakesink audiotestsrc num-buffers=50000 ! audio/x-raw-float,width=32 ! m.
193 add_float32 (gfloat * out, gfloat * in, gint bytes)
195 oil_add_f32 (out, out, in, bytes / sizeof (gfloat));
199 add_float64 (gdouble * out, gdouble * in, gint bytes)
201 oil_add_f64 (out, out, in, bytes / sizeof (gdouble));
206 MAKE_FUNC (add_int32, gint32, gint64, MIN_INT_32, MAX_INT_32)
207 MAKE_FUNC (add_int16, gint16, gint32, MIN_INT_16, MAX_INT_16)
208 MAKE_FUNC (add_int8, gint8, gint16, MIN_INT_8, MAX_INT_8)
209 MAKE_FUNC_US (add_uint32, guint32, guint64, MAX_UINT_32)
210 MAKE_FUNC_US (add_uint16, guint16, guint32, MAX_UINT_16)
211 MAKE_FUNC_US (add_uint8, guint8, guint16, MAX_UINT_8)
212 MAKE_FUNC_NC (add_float64, gdouble)
213 MAKE_FUNC_NC (add_float32, gfloat)
216 /* we can only accept caps that we and downstream can handle. */
218 gst_adder_sink_getcaps (GstPad * pad)
221 GstCaps *result, *peercaps, *sinkcaps;
223 adder = GST_ADDER (GST_PAD_PARENT (pad));
225 GST_OBJECT_LOCK (adder);
226 /* get the downstream possible caps */
227 peercaps = gst_pad_peer_get_caps (adder->srcpad);
228 /* get the allowed caps on this sinkpad, we use the fixed caps function so
229 * that it does not call recursively in this function. */
230 sinkcaps = gst_pad_get_fixed_caps_func (pad);
232 /* if the peer has caps, intersect */
233 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
234 result = gst_caps_intersect (peercaps, sinkcaps);
235 gst_caps_unref (peercaps);
236 gst_caps_unref (sinkcaps);
238 /* the peer has no caps (or there is no peer), just use the allowed caps
239 * of this sinkpad. */
240 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
243 GST_OBJECT_UNLOCK (adder);
245 GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
246 GST_PAD_NAME (pad), result);
251 /* the first caps we receive on any of the sinkpads will define the caps for all
252 * the other sinkpads because we can only mix streams with the same caps.
255 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
259 GstStructure *structure;
260 const char *media_type;
262 adder = GST_ADDER (GST_PAD_PARENT (pad));
264 GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
265 GST_PAD_NAME (pad), caps);
267 /* FIXME, see if the other pads can accept the format. Also lock the
268 * format on the other pads to this new format. */
269 GST_OBJECT_LOCK (adder);
270 pads = GST_ELEMENT (adder)->pads;
272 GstPad *otherpad = GST_PAD (pads->data);
274 if (otherpad != pad) {
275 gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
277 pads = g_list_next (pads);
279 GST_OBJECT_UNLOCK (adder);
282 structure = gst_caps_get_structure (caps, 0);
283 media_type = gst_structure_get_name (structure);
284 if (strcmp (media_type, "audio/x-raw-int") == 0) {
285 adder->format = GST_ADDER_FORMAT_INT;
286 gst_structure_get_int (structure, "width", &adder->width);
287 gst_structure_get_int (structure, "depth", &adder->depth);
288 gst_structure_get_int (structure, "endianness", &adder->endianness);
289 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
291 GST_INFO_OBJECT (pad, "parse_caps sets adder to format int, %d bit",
294 if (adder->endianness != G_BYTE_ORDER)
297 switch (adder->width) {
299 adder->func = (adder->is_signed ?
300 (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
303 adder->func = (adder->is_signed ?
304 (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
307 adder->func = (adder->is_signed ?
308 (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
313 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
314 adder->format = GST_ADDER_FORMAT_FLOAT;
315 gst_structure_get_int (structure, "width", &adder->width);
316 gst_structure_get_int (structure, "endianness", &adder->endianness);
318 GST_INFO_OBJECT (pad, "parse_caps sets adder to format float, %d bit",
321 if (adder->endianness != G_BYTE_ORDER)
324 switch (adder->width) {
326 adder->func = (GstAdderFunction) add_float32;
329 adder->func = (GstAdderFunction) add_float64;
338 gst_structure_get_int (structure, "channels", &adder->channels);
339 gst_structure_get_int (structure, "rate", &adder->rate);
341 adder->bps = (adder->width / 8) * adder->channels;
348 GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
353 /* FIXME, the duration query should reflect how long you will produce
354 * data, that is the amount of stream time until you will emit EOS.
356 * For synchronized mixing this is always the max of all the durations
357 * of upstream since we emit EOS when all of them finished.
359 * We don't do synchronized mixing so this really depends on where the
360 * streams where punched in and what their relative offsets are against
361 * eachother which we can get from the first timestamps we see.
363 * When we add a new stream (or remove a stream) the duration might
364 * also become invalid again and we need to post a new DURATION
365 * message to notify this fact to the parent.
366 * For now we take the max of all the upstream elements so the simple
367 * cases work at least somewhat.
370 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
379 gst_query_parse_duration (query, &format, NULL);
385 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
387 GstIteratorResult ires;
391 ires = gst_iterator_next (it, &item);
393 case GST_ITERATOR_DONE:
396 case GST_ITERATOR_OK:
398 GstPad *pad = GST_PAD_CAST (item);
402 /* ask sink peer for duration */
403 res &= gst_pad_query_peer_duration (pad, &format, &duration);
404 /* take max from all valid return values */
406 /* valid unknown length, stop searching */
407 if (duration == -1) {
411 /* else see if bigger than current max */
412 else if (duration > max)
415 gst_object_unref (pad);
418 case GST_ITERATOR_RESYNC:
421 gst_iterator_resync (it);
429 gst_iterator_free (it);
432 /* and store the max */
433 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
434 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
435 gst_query_set_duration (query, format, max);
442 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
444 GstClockTime min, max;
455 max = GST_CLOCK_TIME_NONE;
457 /* Take maximum of all latency values */
458 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
460 GstIteratorResult ires;
464 ires = gst_iterator_next (it, &item);
466 case GST_ITERATOR_DONE:
469 case GST_ITERATOR_OK:
471 GstPad *pad = GST_PAD_CAST (item);
473 GstClockTime min_cur, max_cur;
476 peerquery = gst_query_new_latency ();
478 /* Ask peer for latency */
479 res &= gst_pad_peer_query (pad, peerquery);
481 /* take max from all valid return values */
483 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
488 if (max_cur != GST_CLOCK_TIME_NONE &&
489 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
490 (max == GST_CLOCK_TIME_NONE)))
493 live = live || live_cur;
496 gst_query_unref (peerquery);
497 gst_object_unref (pad);
500 case GST_ITERATOR_RESYNC:
503 max = GST_CLOCK_TIME_NONE;
505 gst_iterator_resync (it);
513 gst_iterator_free (it);
516 /* store the results */
517 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
518 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
519 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
520 gst_query_set_latency (query, live, min, max);
527 gst_adder_query (GstPad * pad, GstQuery * query)
529 GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
530 gboolean res = FALSE;
532 switch (GST_QUERY_TYPE (query)) {
533 case GST_QUERY_POSITION:
537 gst_query_parse_position (query, &format, NULL);
540 case GST_FORMAT_TIME:
541 /* FIXME, bring to stream time, might be tricky */
542 gst_query_set_position (query, format, adder->timestamp);
545 case GST_FORMAT_DEFAULT:
546 gst_query_set_position (query, format, adder->offset);
554 case GST_QUERY_DURATION:
555 res = gst_adder_query_duration (adder, query);
557 case GST_QUERY_LATENCY:
558 res = gst_adder_query_latency (adder, query);
561 /* FIXME, needs a custom query handler because we have multiple
563 res = gst_pad_query_default (pad, query);
567 gst_object_unref (adder);
572 forward_event_func (GstPad * pad, GValue * ret, GstEvent * event)
574 gst_event_ref (event);
575 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
576 if (!gst_pad_push_event (pad, event)) {
577 g_value_set_boolean (ret, FALSE);
578 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
579 event, GST_EVENT_TYPE_NAME (event));
581 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
582 event, GST_EVENT_TYPE_NAME (event));
584 gst_object_unref (pad);
585 /* continue on other pads, even if one failed */
589 /* forwards the event to all sinkpads, takes ownership of the
592 * Returns: TRUE if the event could be forwarded on all
596 forward_event (GstAdder * adder, GstEvent * event)
600 GstIteratorResult ires;
603 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
604 GST_EVENT_TYPE_NAME (event));
608 g_value_init (&vret, G_TYPE_BOOLEAN);
609 g_value_set_boolean (&vret, TRUE);
610 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
612 ires = gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
615 case GST_ITERATOR_RESYNC:
616 GST_WARNING ("resync");
617 gst_iterator_resync (it);
618 g_value_set_boolean (&vret, TRUE);
620 case GST_ITERATOR_OK:
621 case GST_ITERATOR_DONE:
622 ret = g_value_get_boolean (&vret);
630 gst_iterator_free (it);
631 GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
632 GST_EVENT_TYPE_NAME (event), ret);
633 gst_event_unref (event);
639 gst_adder_src_event (GstPad * pad, GstEvent * event)
644 adder = GST_ADDER (gst_pad_get_parent (pad));
646 switch (GST_EVENT_TYPE (event)) {
653 /* parse the seek parameters */
654 gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
657 /* check if we are flushing */
658 if (flags & GST_SEEK_FLAG_FLUSH) {
659 /* make sure we accept nothing anymore and return WRONG_STATE */
660 gst_collect_pads_set_flushing (adder->collect, TRUE);
662 /* flushing seek, start flush downstream, the flush will be done
663 * when all pads received a FLUSH_STOP. */
664 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
666 GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
667 /* now wait for the collected to be finished and mark a new
669 GST_OBJECT_LOCK (adder->collect);
670 if (curtype == GST_SEEK_TYPE_SET)
671 adder->segment_position = cur;
673 adder->segment_position = 0;
674 adder->segment_pending = TRUE;
675 GST_OBJECT_UNLOCK (adder->collect);
676 GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
679 result = forward_event (adder, event);
681 /* seek failed. maybe source is a live source. send a flush_stop
682 * FIXME: ideally we just forward flush event, but live sources don't
683 * send anything and we need a flush events to unlock the collect
686 GST_DEBUG_OBJECT (adder, "seeking failed, mark pending flush_stop");
687 adder->flush_stop_pending =
688 ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
693 /* QoS might be tricky */
696 case GST_EVENT_NAVIGATION:
697 /* navigation is rather pointless. */
701 /* just forward the rest for now */
702 GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
703 GST_EVENT_TYPE_NAME (event));
704 result = forward_event (adder, event);
707 gst_object_unref (adder);
713 gst_adder_sink_event (GstPad * pad, GstEvent * event)
718 adder = GST_ADDER (gst_pad_get_parent (pad));
720 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
721 GST_DEBUG_PAD_NAME (pad));
723 switch (GST_EVENT_TYPE (event)) {
724 case GST_EVENT_FLUSH_STOP:
725 /* mark a pending new segment. This event is synchronized
726 * with the streaming thread so we can safely update the
727 * variable without races. It's somewhat weird because we
728 * assume the collectpads forwarded the FLUSH_STOP past us
729 * and downstream (using our source pad, the bastard!).
731 adder->segment_pending = TRUE;
732 adder->flush_stop_pending = FALSE;
738 /* now GstCollectPads can take care of the rest, e.g. EOS */
739 ret = adder->collect_event (pad, event);
741 gst_object_unref (adder);
746 gst_adder_class_init (GstAdderClass * klass)
748 GObjectClass *gobject_class = (GObjectClass *) klass;
749 GstElementClass *gstelement_class = (GstElementClass *) klass;
751 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_adder_finalize);
753 gst_element_class_add_pad_template (gstelement_class,
754 gst_static_pad_template_get (&gst_adder_src_template));
755 gst_element_class_add_pad_template (gstelement_class,
756 gst_static_pad_template_get (&gst_adder_sink_template));
757 gst_element_class_set_details_simple (gstelement_class, "Adder",
759 "Add N audio channels together",
760 "Thomas Vander Stichele <thomas at apestaart dot org>");
762 parent_class = g_type_class_peek_parent (klass);
764 gstelement_class->request_new_pad =
765 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
766 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
767 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
771 gst_adder_init (GstAdder * adder)
773 GstPadTemplate *template;
775 template = gst_static_pad_template_get (&gst_adder_src_template);
776 adder->srcpad = gst_pad_new_from_template (template, "src");
777 gst_object_unref (template);
779 gst_pad_set_getcaps_function (adder->srcpad,
780 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
781 gst_pad_set_setcaps_function (adder->srcpad,
782 GST_DEBUG_FUNCPTR (gst_adder_setcaps));
783 gst_pad_set_query_function (adder->srcpad,
784 GST_DEBUG_FUNCPTR (gst_adder_query));
785 gst_pad_set_event_function (adder->srcpad,
786 GST_DEBUG_FUNCPTR (gst_adder_src_event));
787 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
789 adder->format = GST_ADDER_FORMAT_UNSET;
793 /* keep track of the sinkpads requested */
794 adder->collect = gst_collect_pads_new ();
795 gst_collect_pads_set_function (adder->collect,
796 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
800 gst_adder_finalize (GObject * object)
802 GstAdder *adder = GST_ADDER (object);
804 gst_object_unref (adder->collect);
805 adder->collect = NULL;
807 G_OBJECT_CLASS (parent_class)->finalize (object);
811 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
812 const gchar * unused)
819 if (templ->direction != GST_PAD_SINK)
822 adder = GST_ADDER (element);
824 /* increment pad counter */
825 padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
827 name = g_strdup_printf ("sink%d", padcount);
828 newpad = gst_pad_new_from_template (templ, name);
829 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
832 gst_pad_set_getcaps_function (newpad,
833 GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
834 gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
835 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
837 /* FIXME: hacked way to override/extend the event function of
838 * GstCollectPads; because it sets its own event function giving the
839 * element no access to events */
840 adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
841 gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
843 /* takes ownership of the pad */
844 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
852 g_warning ("gstadder: request new pad that is not a SINK pad\n");
857 GST_DEBUG_OBJECT (adder, "could not add pad");
858 gst_collect_pads_remove_pad (adder->collect, newpad);
859 gst_object_unref (newpad);
865 gst_adder_release_pad (GstElement * element, GstPad * pad)
869 adder = GST_ADDER (element);
871 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
873 gst_collect_pads_remove_pad (adder->collect, pad);
874 gst_element_remove_pad (element, pad);
878 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
881 * combine streams by adding data values
883 * - this function is called when all pads have a buffer
884 * - get available bytes on all pads.
885 * - repeat for each input pad :
886 * - read available bytes, copy or add to target buffer
887 * - if there's an EOS event, remove the input channel
888 * - push out the output buffer
891 * - would be nice to have a mixing mode, where instead of adding we mix
892 * - for float we could downscale after collect loop
893 * - for int we need to downscale each input to avoid clipping or
894 * mix into a temp (float) buffer and scale afterwards as well
899 GstBuffer *outbuf = NULL;
900 gpointer outdata = NULL;
902 gboolean empty = TRUE;
904 adder = GST_ADDER (user_data);
907 if (G_UNLIKELY (adder->func == NULL))
910 if (adder->flush_stop_pending) {
911 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop ());
912 adder->flush_stop_pending = FALSE;
915 /* get available bytes for reading, this can be 0 which could mean empty
916 * buffers or EOS, which we will catch when we loop over the pads. */
917 outsize = gst_collect_pads_available (pads);
919 GST_LOG_OBJECT (adder,
920 "starting to cycle through channels, %d bytes available (bps = %d)",
921 outsize, adder->bps);
923 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
924 GstCollectData *collect_data;
929 collect_data = (GstCollectData *) collected->data;
931 /* get a subbuffer of size bytes */
932 inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
933 /* NULL means EOS or an empty buffer so we still need to flush in
934 * case of an empty buffer. */
936 GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
940 indata = GST_BUFFER_DATA (inbuf);
941 insize = GST_BUFFER_SIZE (inbuf);
943 if (outbuf == NULL) {
944 GST_LOG_OBJECT (adder, "channel %p: making output buffer of %d bytes",
945 collect_data, outsize);
947 /* first buffer, alloc outsize.
948 * FIXME: we can easily subbuffer and _make_writable.
949 * FIXME: only create empty buffer for first non-gap buffer, so that we
950 * only use adder function when really adding
952 outbuf = gst_buffer_new_and_alloc (outsize);
953 outdata = GST_BUFFER_DATA (outbuf);
954 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
956 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
957 GST_LOG_OBJECT (adder, "channel %p: copying %d bytes from data %p",
958 collect_data, insize, indata);
959 /* clear if we are only going to fill a partial buffer */
960 if (G_UNLIKELY (outsize > insize))
961 memset ((guint8 *) outdata + insize, 0, outsize - insize);
962 /* and copy the data into it */
963 memcpy (outdata, indata, insize);
966 /* clear whole buffer */
967 GST_LOG_OBJECT (adder, "channel %p: zeroing %d bytes from data %p",
968 collect_data, insize, indata);
969 memset (outdata, 0, outsize);
972 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
973 GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
974 collect_data, insize, indata);
975 /* further buffers, need to add them */
976 adder->func ((gpointer) outdata, (gpointer) indata, insize);
979 GST_LOG_OBJECT (adder, "channel %p: skipping %d bytes from data %p",
980 collect_data, insize, indata);
983 gst_buffer_unref (inbuf);
986 /* can only happen when no pads to collect or all EOS */
990 /* our timestamping is very simple, just an ever incrementing
991 * counter, the new segment time will take care of their respective
993 if (adder->segment_pending) {
996 /* FIXME, use rate/applied_rate as set on all sinkpads.
997 * - currently we just set rate as received from last seek-event
998 * We could potentially figure out the duration as well using
999 * the current segment positions and the stated stop positions.
1000 * Also we just start from stream time 0 which is rather
1001 * weird. For non-synchronized mixing, the time should be
1002 * the min of the stream times of all received segments,
1003 * rationale being that the duration is at least going to
1004 * be as long as the earliest stream we start mixing. This
1005 * would also be correct for synchronized mixing but then
1006 * the later streams would be delayed until the stream times
1009 event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
1010 1.0, GST_FORMAT_TIME, adder->timestamp, -1, adder->segment_position);
1013 if (!gst_pad_push_event (adder->srcpad, event)) {
1014 GST_WARNING_OBJECT (adder->srcpad, "Sending event %p (%s) failed.",
1015 event, GST_EVENT_TYPE_NAME (event));
1017 adder->segment_pending = FALSE;
1018 adder->segment_position = 0;
1020 GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1021 "start:%" G_GINT64_FORMAT " pos:%" G_GINT64_FORMAT " failed",
1022 adder->timestamp, adder->segment_position);
1026 /* set timestamps on the output buffer */
1027 GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
1028 GST_BUFFER_OFFSET (outbuf) = adder->offset;
1030 /* for the next timestamp, use the sample counter, which will
1031 * never accumulate rounding errors */
1032 adder->offset += outsize / adder->bps;
1033 adder->timestamp = gst_util_uint64_scale_int (adder->offset,
1034 GST_SECOND, adder->rate);
1036 /* now we can set the duration of the buffer */
1037 GST_BUFFER_DURATION (outbuf) = adder->timestamp -
1038 GST_BUFFER_TIMESTAMP (outbuf);
1040 /* if we only processed silence, mark output again as silence */
1042 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
1045 GST_LOG_OBJECT (adder, "pushing outbuf, timestamp %" GST_TIME_FORMAT,
1046 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
1047 ret = gst_pad_push (adder->srcpad, outbuf);
1049 GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1056 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1057 ("Unknown data received, not negotiated"));
1058 return GST_FLOW_NOT_NEGOTIATED;
1062 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1063 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1064 return GST_FLOW_UNEXPECTED;
1068 static GstStateChangeReturn
1069 gst_adder_change_state (GstElement * element, GstStateChange transition)
1072 GstStateChangeReturn ret;
1074 adder = GST_ADDER (element);
1076 switch (transition) {
1077 case GST_STATE_CHANGE_NULL_TO_READY:
1079 case GST_STATE_CHANGE_READY_TO_PAUSED:
1080 adder->timestamp = 0;
1082 adder->segment_pending = TRUE;
1083 adder->segment_position = 0;
1084 adder->segment_rate = 1.0;
1085 gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1086 gst_collect_pads_start (adder->collect);
1088 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1090 case GST_STATE_CHANGE_PAUSED_TO_READY:
1091 /* need to unblock the collectpads before calling the
1092 * parent change_state so that streaming can finish */
1093 gst_collect_pads_stop (adder->collect);
1099 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1101 switch (transition) {
1111 plugin_init (GstPlugin * plugin)
1115 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1122 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1125 "Adds multiple streams",
1126 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)