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 "gstadderorc.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))
72 #define GST_CAT_DEFAULT gst_adder_debug
73 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
75 /* elementfactory information */
79 "rate = (int) [ 1, MAX ], " \
80 "channels = (int) [ 1, MAX ], " \
81 "endianness = (int) BYTE_ORDER, " \
82 "width = (int) 32, " \
83 "depth = (int) 32, " \
84 "signed = (boolean) { true, false } ;" \
86 "rate = (int) [ 1, MAX ], " \
87 "channels = (int) [ 1, MAX ], " \
88 "endianness = (int) BYTE_ORDER, " \
89 "width = (int) 16, " \
90 "depth = (int) 16, " \
91 "signed = (boolean) { true, false } ;" \
93 "rate = (int) [ 1, MAX ], " \
94 "channels = (int) [ 1, MAX ], " \
95 "endianness = (int) BYTE_ORDER, " \
98 "signed = (boolean) { true, false } ;" \
99 "audio/x-raw-float, " \
100 "rate = (int) [ 1, MAX ], " \
101 "channels = (int) [ 1, MAX ], " \
102 "endianness = (int) BYTE_ORDER, " \
103 "width = (int) { 32, 64 }"
105 static GstStaticPadTemplate gst_adder_src_template =
106 GST_STATIC_PAD_TEMPLATE ("src",
109 GST_STATIC_CAPS (CAPS)
112 static GstStaticPadTemplate gst_adder_sink_template =
113 GST_STATIC_PAD_TEMPLATE ("sink%d",
116 GST_STATIC_CAPS (CAPS)
119 static void gst_adder_class_init (GstAdderClass * klass);
120 static void gst_adder_init (GstAdder * adder);
121 static void gst_adder_dispose (GObject * object);
122 static void gst_adder_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_adder_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
128 static gboolean gst_adder_query (GstPad * pad, GstQuery * query);
129 static gboolean gst_adder_src_event (GstPad * pad, GstEvent * event);
130 static gboolean gst_adder_sink_event (GstPad * pad, GstEvent * event);
132 static GstPad *gst_adder_request_new_pad (GstElement * element,
133 GstPadTemplate * temp, const gchar * unused);
134 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
136 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
137 GstStateChange transition);
139 static GstBuffer *gst_adder_do_clip (GstCollectPads * pads,
140 GstCollectData * data, GstBuffer * buffer, gpointer user_data);
141 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
144 static GstElementClass *parent_class = NULL;
147 gst_adder_get_type (void)
149 static GType adder_type = 0;
151 if (G_UNLIKELY (adder_type == 0)) {
152 static const GTypeInfo adder_info = {
153 sizeof (GstAdderClass), NULL, NULL,
154 (GClassInitFunc) gst_adder_class_init, NULL, NULL,
155 sizeof (GstAdder), 0,
156 (GInstanceInitFunc) gst_adder_init,
159 adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
161 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
162 "audio channel mixing element");
167 /* non-clipping versions (for float) */
168 #define MAKE_FUNC_NC(name,type) \
169 static void name (type *out, type *in, gint samples) { \
171 for (i = 0; i < samples; i++) \
176 MAKE_FUNC_NC (add_float64, gdouble)
179 /* we can only accept caps that we and downstream can handle.
180 * if we have filtercaps set, use those to constrain the target caps.
183 gst_adder_sink_getcaps (GstPad * pad)
186 GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
188 adder = GST_ADDER (GST_PAD_PARENT (pad));
190 GST_OBJECT_LOCK (adder);
192 if ((filter_caps = adder->filter_caps))
193 gst_caps_ref (filter_caps);
194 GST_OBJECT_UNLOCK (adder);
196 /* get the downstream possible caps */
197 peercaps = gst_pad_peer_get_caps (adder->srcpad);
199 /* get the allowed caps on this sinkpad, we use the fixed caps function so
200 * that it does not call recursively in this function. */
201 sinkcaps = gst_pad_get_fixed_caps_func (pad);
203 /* restrict with filter-caps if any */
205 GST_DEBUG_OBJECT (adder, "filtering peer caps");
206 result = gst_caps_intersect (peercaps, filter_caps);
207 gst_caps_unref (peercaps);
210 /* if the peer has caps, intersect */
211 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
212 result = gst_caps_intersect (peercaps, sinkcaps);
213 gst_caps_unref (peercaps);
214 gst_caps_unref (sinkcaps);
216 /* the peer has no caps (or there is no peer), just use the allowed caps
217 * of this sinkpad. */
218 /* restrict with filter-caps if any */
220 GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
221 result = gst_caps_intersect (sinkcaps, filter_caps);
222 gst_caps_unref (sinkcaps);
224 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
230 gst_caps_unref (filter_caps);
232 GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
233 GST_PAD_NAME (pad), result);
238 /* the first caps we receive on any of the sinkpads will define the caps for all
239 * the other sinkpads because we can only mix streams with the same caps.
242 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
246 GstStructure *structure;
247 const char *media_type;
249 adder = GST_ADDER (GST_PAD_PARENT (pad));
251 GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
252 GST_PAD_NAME (pad), caps);
254 /* FIXME, see if the other pads can accept the format. Also lock the
255 * format on the other pads to this new format. */
256 GST_OBJECT_LOCK (adder);
257 pads = GST_ELEMENT (adder)->pads;
259 GstPad *otherpad = GST_PAD (pads->data);
261 if (otherpad != pad) {
262 gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
264 pads = g_list_next (pads);
266 GST_OBJECT_UNLOCK (adder);
269 structure = gst_caps_get_structure (caps, 0);
270 media_type = gst_structure_get_name (structure);
271 if (strcmp (media_type, "audio/x-raw-int") == 0) {
272 adder->format = GST_ADDER_FORMAT_INT;
273 gst_structure_get_int (structure, "width", &adder->width);
274 gst_structure_get_int (structure, "depth", &adder->depth);
275 gst_structure_get_int (structure, "endianness", &adder->endianness);
276 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
278 GST_INFO_OBJECT (pad, "parse_caps sets adder to format int, %d bit",
281 if (adder->endianness != G_BYTE_ORDER)
284 switch (adder->width) {
286 adder->func = (adder->is_signed ?
287 (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
288 adder->sample_size = 1;
291 adder->func = (adder->is_signed ?
292 (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
293 adder->sample_size = 2;
296 adder->func = (adder->is_signed ?
297 (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
298 adder->sample_size = 4;
303 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
304 adder->format = GST_ADDER_FORMAT_FLOAT;
305 gst_structure_get_int (structure, "width", &adder->width);
306 gst_structure_get_int (structure, "endianness", &adder->endianness);
308 GST_INFO_OBJECT (pad, "parse_caps sets adder to format float, %d bit",
311 if (adder->endianness != G_BYTE_ORDER)
314 switch (adder->width) {
316 adder->func = (GstAdderFunction) add_float32;
317 adder->sample_size = 4;
320 adder->func = (GstAdderFunction) add_float64;
321 adder->sample_size = 8;
330 gst_structure_get_int (structure, "channels", &adder->channels);
331 gst_structure_get_int (structure, "rate", &adder->rate);
333 adder->bps = (adder->width / 8) * adder->channels;
340 GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
345 /* FIXME, the duration query should reflect how long you will produce
346 * data, that is the amount of stream time until you will emit EOS.
348 * For synchronized mixing this is always the max of all the durations
349 * of upstream since we emit EOS when all of them finished.
351 * We don't do synchronized mixing so this really depends on where the
352 * streams where punched in and what their relative offsets are against
353 * eachother which we can get from the first timestamps we see.
355 * When we add a new stream (or remove a stream) the duration might
356 * also become invalid again and we need to post a new DURATION
357 * message to notify this fact to the parent.
358 * For now we take the max of all the upstream elements so the simple
359 * cases work at least somewhat.
362 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
371 gst_query_parse_duration (query, &format, NULL);
377 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
379 GstIteratorResult ires;
383 ires = gst_iterator_next (it, &item);
385 case GST_ITERATOR_DONE:
388 case GST_ITERATOR_OK:
390 GstPad *pad = GST_PAD_CAST (item);
394 /* ask sink peer for duration */
395 res &= gst_pad_query_peer_duration (pad, &format, &duration);
396 /* take max from all valid return values */
398 /* valid unknown length, stop searching */
399 if (duration == -1) {
403 /* else see if bigger than current max */
404 else if (duration > max)
407 gst_object_unref (pad);
410 case GST_ITERATOR_RESYNC:
413 gst_iterator_resync (it);
421 gst_iterator_free (it);
424 /* and store the max */
425 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
426 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
427 gst_query_set_duration (query, format, max);
434 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
436 GstClockTime min, max;
447 max = GST_CLOCK_TIME_NONE;
449 /* Take maximum of all latency values */
450 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
452 GstIteratorResult ires;
456 ires = gst_iterator_next (it, &item);
458 case GST_ITERATOR_DONE:
461 case GST_ITERATOR_OK:
463 GstPad *pad = GST_PAD_CAST (item);
465 GstClockTime min_cur, max_cur;
468 peerquery = gst_query_new_latency ();
470 /* Ask peer for latency */
471 res &= gst_pad_peer_query (pad, peerquery);
473 /* take max from all valid return values */
475 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
480 if (max_cur != GST_CLOCK_TIME_NONE &&
481 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
482 (max == GST_CLOCK_TIME_NONE)))
485 live = live || live_cur;
488 gst_query_unref (peerquery);
489 gst_object_unref (pad);
492 case GST_ITERATOR_RESYNC:
495 max = GST_CLOCK_TIME_NONE;
497 gst_iterator_resync (it);
505 gst_iterator_free (it);
508 /* store the results */
509 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
510 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
511 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
512 gst_query_set_latency (query, live, min, max);
519 gst_adder_query (GstPad * pad, GstQuery * query)
521 GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
522 gboolean res = FALSE;
524 switch (GST_QUERY_TYPE (query)) {
525 case GST_QUERY_POSITION:
529 gst_query_parse_position (query, &format, NULL);
532 case GST_FORMAT_TIME:
533 /* FIXME, bring to stream time, might be tricky */
534 gst_query_set_position (query, format, adder->timestamp);
537 case GST_FORMAT_DEFAULT:
538 gst_query_set_position (query, format, adder->offset);
546 case GST_QUERY_DURATION:
547 res = gst_adder_query_duration (adder, query);
549 case GST_QUERY_LATENCY:
550 res = gst_adder_query_latency (adder, query);
553 /* FIXME, needs a custom query handler because we have multiple
555 res = gst_pad_query_default (pad, query);
559 gst_object_unref (adder);
570 forward_event_func (GstPad * pad, GValue * ret, EventData * data)
572 GstEvent *event = data->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 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
578 event, GST_EVENT_TYPE_NAME (event));
579 /* quick hack to unflush the pads, ideally we need a way to just unflush
580 * this single collect pad */
582 gst_pad_send_event (pad, gst_event_new_flush_stop ());
584 g_value_set_boolean (ret, TRUE);
585 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
586 event, GST_EVENT_TYPE_NAME (event));
588 gst_object_unref (pad);
590 /* continue on other pads, even if one failed */
594 /* forwards the event to all sinkpads, takes ownership of the
597 * Returns: TRUE if the event could be forwarded on all
601 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
605 GstIteratorResult ires;
609 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
610 GST_EVENT_TYPE_NAME (event));
615 g_value_init (&vret, G_TYPE_BOOLEAN);
616 g_value_set_boolean (&vret, FALSE);
617 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
619 ires = gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
622 case GST_ITERATOR_RESYNC:
623 GST_WARNING ("resync");
624 gst_iterator_resync (it);
625 g_value_set_boolean (&vret, TRUE);
627 case GST_ITERATOR_OK:
628 case GST_ITERATOR_DONE:
629 ret = g_value_get_boolean (&vret);
637 gst_iterator_free (it);
638 GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
639 GST_EVENT_TYPE_NAME (event), ret);
640 gst_event_unref (event);
646 gst_adder_src_event (GstPad * pad, GstEvent * event)
651 adder = GST_ADDER (gst_pad_get_parent (pad));
653 switch (GST_EVENT_TYPE (event)) {
657 GstSeekType curtype, endtype;
661 /* parse the seek parameters */
662 gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
663 &cur, &endtype, &end);
665 if ((curtype != GST_SEEK_TYPE_NONE) && (curtype != GST_SEEK_TYPE_SET)) {
667 GST_DEBUG_OBJECT (adder,
668 "seeking failed, unhandled seek type for start: %d", curtype);
671 if ((endtype != GST_SEEK_TYPE_NONE) && (endtype != GST_SEEK_TYPE_SET)) {
673 GST_DEBUG_OBJECT (adder,
674 "seeking failed, unhandled seek type for end: %d", endtype);
678 flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
680 /* check if we are flushing */
682 /* make sure we accept nothing anymore and return WRONG_STATE */
683 gst_collect_pads_set_flushing (adder->collect, TRUE);
685 /* flushing seek, start flush downstream, the flush will be done
686 * when all pads received a FLUSH_STOP. */
687 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
689 GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
691 /* now wait for the collected to be finished and mark a new
692 * segment. After we have the lock, no collect function is running and no
693 * new collect function will be called for as long as we're flushing. */
694 GST_OBJECT_LOCK (adder->collect);
695 if (curtype == GST_SEEK_TYPE_SET)
696 adder->segment_start = cur;
698 adder->segment_start = 0;
699 if (endtype == GST_SEEK_TYPE_SET)
700 adder->segment_end = end;
702 adder->segment_end = GST_CLOCK_TIME_NONE;
703 /* make sure we push a new segment, to inform about new basetime
704 * see FIXME in gst_adder_collected() */
705 adder->segment_pending = TRUE;
707 /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
708 * have stopped so that the cookie gets properly updated. */
709 gst_collect_pads_set_flushing (adder->collect, TRUE);
711 /* we might have a pending flush_stop event now. This event will either be
712 * sent by an upstream element when it completes the seek or we will push
713 * one in the collected callback ourself */
714 adder->flush_stop_pending = flush;
715 GST_OBJECT_UNLOCK (adder->collect);
716 GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
719 result = forward_event (adder, event, flush);
721 /* seek failed. maybe source is a live source. */
722 GST_DEBUG_OBJECT (adder, "seeking failed");
724 /* FIXME: ideally we would like to send a flush-stop event from here but
725 * collectpads does not have a method that allows us to do that. Instead
726 * we forward all flush-stop events we receive on the sinkpads. We might
727 * be sending too many flush-stop events. */
731 /* QoS might be tricky */
734 case GST_EVENT_NAVIGATION:
735 /* navigation is rather pointless. */
739 /* just forward the rest for now */
740 GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
741 GST_EVENT_TYPE_NAME (event));
742 result = forward_event (adder, event, FALSE);
747 gst_object_unref (adder);
753 gst_adder_sink_event (GstPad * pad, GstEvent * event)
758 adder = GST_ADDER (gst_pad_get_parent (pad));
760 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
761 GST_DEBUG_PAD_NAME (pad));
763 switch (GST_EVENT_TYPE (event)) {
764 case GST_EVENT_FLUSH_STOP:
765 /* we received a flush-stop. The collect_event function will push the
766 * event past our element. We simply forward all flush-stop events, even
767 * when no flush-stop was pending, this is required because collectpads
768 * does not provide an API to handle-but-not-forward the flush-stop.
769 * We unset the pending flush-stop flag so that we don't send anymore
770 * flush-stop from the collect function later.
772 GST_OBJECT_LOCK (adder->collect);
773 adder->segment_pending = TRUE;
774 adder->flush_stop_pending = FALSE;
775 /* Clear pending tags */
776 if (adder->pending_events) {
777 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
778 g_list_free (adder->pending_events);
779 adder->pending_events = NULL;
781 GST_OBJECT_UNLOCK (adder->collect);
784 GST_OBJECT_LOCK (adder->collect);
785 /* collect tags here so we can push them out when we collect data */
786 adder->pending_events = g_list_append (adder->pending_events, event);
787 GST_OBJECT_UNLOCK (adder->collect);
793 /* now GstCollectPads can take care of the rest, e.g. EOS */
794 ret = adder->collect_event (pad, event);
797 gst_object_unref (adder);
802 gst_adder_class_init (GstAdderClass * klass)
804 GObjectClass *gobject_class = (GObjectClass *) klass;
805 GstElementClass *gstelement_class = (GstElementClass *) klass;
807 gobject_class->set_property = gst_adder_set_property;
808 gobject_class->get_property = gst_adder_get_property;
809 gobject_class->dispose = gst_adder_dispose;
811 gst_element_class_add_pad_template (gstelement_class,
812 gst_static_pad_template_get (&gst_adder_src_template));
813 gst_element_class_add_pad_template (gstelement_class,
814 gst_static_pad_template_get (&gst_adder_sink_template));
815 gst_element_class_set_details_simple (gstelement_class, "Adder",
817 "Add N audio channels together",
818 "Thomas Vander Stichele <thomas at apestaart dot org>");
820 parent_class = g_type_class_peek_parent (klass);
827 g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
828 g_param_spec_boxed ("caps", "Target caps",
829 "Set target format for mixing (NULL means ANY). "
830 "Setting this property takes a reference to the supplied GstCaps "
831 "object.", GST_TYPE_CAPS,
832 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
834 gstelement_class->request_new_pad =
835 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
836 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
837 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
841 gst_adder_init (GstAdder * adder)
843 GstPadTemplate *template;
845 template = gst_static_pad_template_get (&gst_adder_src_template);
846 adder->srcpad = gst_pad_new_from_template (template, "src");
847 gst_object_unref (template);
849 gst_pad_set_getcaps_function (adder->srcpad,
850 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
851 gst_pad_set_setcaps_function (adder->srcpad,
852 GST_DEBUG_FUNCPTR (gst_adder_setcaps));
853 gst_pad_set_query_function (adder->srcpad,
854 GST_DEBUG_FUNCPTR (gst_adder_query));
855 gst_pad_set_event_function (adder->srcpad,
856 GST_DEBUG_FUNCPTR (gst_adder_src_event));
857 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
859 adder->format = GST_ADDER_FORMAT_UNSET;
863 adder->filter_caps = NULL;
865 /* keep track of the sinkpads requested */
866 adder->collect = gst_collect_pads_new ();
867 gst_collect_pads_set_function (adder->collect,
868 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
869 gst_collect_pads_set_clip_function (adder->collect,
870 GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
874 gst_adder_dispose (GObject * object)
876 GstAdder *adder = GST_ADDER (object);
878 if (adder->collect) {
879 gst_object_unref (adder->collect);
880 adder->collect = NULL;
882 gst_caps_replace (&adder->filter_caps, NULL);
883 if (adder->pending_events) {
884 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
885 g_list_free (adder->pending_events);
886 adder->pending_events = NULL;
889 G_OBJECT_CLASS (parent_class)->dispose (object);
893 gst_adder_set_property (GObject * object, guint prop_id,
894 const GValue * value, GParamSpec * pspec)
896 GstAdder *adder = GST_ADDER (object);
899 case PROP_FILTER_CAPS:{
900 GstCaps *new_caps = NULL;
902 const GstCaps *new_caps_val = gst_value_get_caps (value);
904 if (new_caps_val != NULL) {
905 new_caps = (GstCaps *) new_caps_val;
906 gst_caps_ref (new_caps);
909 GST_OBJECT_LOCK (adder);
910 old_caps = adder->filter_caps;
911 adder->filter_caps = new_caps;
912 GST_OBJECT_UNLOCK (adder);
915 gst_caps_unref (old_caps);
917 GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
921 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
927 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
930 GstAdder *adder = GST_ADDER (object);
933 case PROP_FILTER_CAPS:
934 GST_OBJECT_LOCK (adder);
935 gst_value_set_caps (value, adder->filter_caps);
936 GST_OBJECT_UNLOCK (adder);
939 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
946 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
947 const gchar * unused)
954 if (templ->direction != GST_PAD_SINK)
957 adder = GST_ADDER (element);
959 /* increment pad counter */
960 padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
962 name = g_strdup_printf ("sink%d", padcount);
963 newpad = gst_pad_new_from_template (templ, name);
964 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
967 gst_pad_set_getcaps_function (newpad,
968 GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
969 gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
970 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
972 /* FIXME: hacked way to override/extend the event function of
973 * GstCollectPads; because it sets its own event function giving the
974 * element no access to events */
975 adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
976 gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
978 /* takes ownership of the pad */
979 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
987 g_warning ("gstadder: request new pad that is not a SINK pad\n");
992 GST_DEBUG_OBJECT (adder, "could not add pad");
993 gst_collect_pads_remove_pad (adder->collect, newpad);
994 gst_object_unref (newpad);
1000 gst_adder_release_pad (GstElement * element, GstPad * pad)
1004 adder = GST_ADDER (element);
1006 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1008 gst_collect_pads_remove_pad (adder->collect, pad);
1009 gst_element_remove_pad (element, pad);
1013 gst_adder_do_clip (GstCollectPads * pads, GstCollectData * data,
1014 GstBuffer * buffer, gpointer user_data)
1016 GstAdder *adder = GST_ADDER (user_data);
1018 buffer = gst_audio_buffer_clip (buffer, &data->segment, adder->rate,
1024 static GstFlowReturn
1025 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
1028 * combine streams by adding data values
1030 * - this function is called when all pads have a buffer
1031 * - get available bytes on all pads.
1032 * - repeat for each input pad :
1033 * - read available bytes, copy or add to target buffer
1034 * - if there's an EOS event, remove the input channel
1035 * - push out the output buffer
1038 * - would be nice to have a mixing mode, where instead of adding we mix
1039 * - for float we could downscale after collect loop
1040 * - for int we need to downscale each input to avoid clipping or
1041 * mix into a temp (float) buffer and scale afterwards as well
1044 GSList *collected, *next = NULL;
1046 GstBuffer *outbuf = NULL, *gapbuf = NULL;
1047 gpointer outdata = NULL;
1050 gint64 next_timestamp;
1052 adder = GST_ADDER (user_data);
1055 if (G_UNLIKELY (adder->func == NULL))
1056 goto not_negotiated;
1058 if (adder->flush_stop_pending) {
1059 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop ());
1060 adder->flush_stop_pending = FALSE;
1063 /* get available bytes for reading, this can be 0 which could mean empty
1064 * buffers or EOS, which we will catch when we loop over the pads. */
1065 outsize = gst_collect_pads_available (pads);
1066 /* can only happen when no pads to collect or all EOS */
1070 GST_LOG_OBJECT (adder,
1071 "starting to cycle through channels, %d bytes available (bps = %d)",
1072 outsize, adder->bps);
1074 for (collected = pads->data; collected; collected = next) {
1075 GstCollectData *collect_data;
1079 /* take next to see if this is the last collectdata */
1080 next = g_slist_next (collected);
1082 collect_data = (GstCollectData *) collected->data;
1084 /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1086 inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
1087 /* NULL means EOS or an empty buffer so we still need to flush in
1088 * case of an empty buffer. */
1089 if (inbuf == NULL) {
1090 GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1094 is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1096 /* Try to make an output buffer */
1097 if (outbuf == NULL) {
1098 /* if this is a gap buffer but we have some more pads to check, skip it.
1099 * If we are at the last buffer, take it, regardless if it is a GAP
1101 if (is_gap && next) {
1102 GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1103 /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1104 * EOS, we can use this one as the output buffer. */
1108 gst_buffer_unref (inbuf);
1112 GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1113 collect_data, outsize);
1114 /* make data and metadata writable, can simply return the inbuf when we
1115 * are the only one referencing this buffer. If this is the last (and
1116 * only) GAP buffer, it will automatically copy the GAP flag. */
1117 outbuf = gst_buffer_make_writable (inbuf);
1118 outdata = GST_BUFFER_DATA (outbuf);
1119 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
1122 /* we had a previous output buffer, mix this non-GAP buffer */
1126 indata = GST_BUFFER_DATA (inbuf);
1127 insize = GST_BUFFER_SIZE (inbuf);
1129 /* all buffers should have outsize, there are no short buffers because we
1130 * asked for the max size above */
1131 g_assert (insize == outsize);
1133 GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
1134 collect_data, insize, indata);
1136 /* further buffers, need to add them */
1137 adder->func ((gpointer) outdata, (gpointer) indata,
1138 insize / adder->sample_size);
1140 /* skip gap buffer */
1141 GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1143 gst_buffer_unref (inbuf);
1147 if (outbuf == NULL) {
1148 /* no output buffer, reuse one of the GAP buffers then if we have one */
1150 GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1153 /* assume EOS otherwise, this should not happen, really */
1156 /* we had an output buffer, unref the gapbuffer we kept */
1157 gst_buffer_unref (gapbuf);
1159 if (adder->segment_pending) {
1162 /* FIXME, use rate/applied_rate as set on all sinkpads.
1163 * - currently we just set rate as received from last seek-event
1165 * When seeking we set the start and stop positions as given in the seek
1166 * event. We also adjust offset & timestamp acordingly.
1167 * This basically ignores all newsegments sent by upstream.
1169 event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
1170 1.0, GST_FORMAT_TIME, adder->segment_start, adder->segment_end,
1171 adder->segment_start);
1172 if (adder->segment_rate > 0.0) {
1173 adder->timestamp = adder->segment_start;
1175 adder->timestamp = adder->segment_end;
1177 adder->offset = gst_util_uint64_scale (adder->timestamp,
1178 adder->rate, GST_SECOND);
1179 GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1180 G_GUINT64_FORMAT, adder->segment_start, adder->segment_end);
1181 GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1182 G_GINT64_FORMAT, adder->timestamp, adder->offset);
1185 if (!gst_pad_push_event (adder->srcpad, event)) {
1186 GST_WARNING_OBJECT (adder->srcpad, "Sending event %p (%s) failed.",
1187 event, GST_EVENT_TYPE_NAME (event));
1189 adder->segment_pending = FALSE;
1191 GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1192 "start:%" G_GINT64_FORMAT " end:%" G_GINT64_FORMAT " failed",
1193 adder->segment_start, adder->segment_end);
1197 if (G_UNLIKELY (adder->pending_events)) {
1198 GList *tmp = adder->pending_events;
1201 GstEvent *ev = (GstEvent *) tmp->data;
1203 gst_pad_push_event (adder->srcpad, ev);
1204 tmp = g_list_next (tmp);
1206 g_list_free (adder->pending_events);
1207 adder->pending_events = NULL;
1210 /* for the next timestamp, use the sample counter, which will
1211 * never accumulate rounding errors */
1212 if (adder->segment_rate > 0.0) {
1213 next_offset = adder->offset + outsize / adder->bps;
1215 next_offset = adder->offset - outsize / adder->bps;
1217 next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, adder->rate);
1220 /* set timestamps on the output buffer */
1221 if (adder->segment_rate > 0.0) {
1222 GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
1223 GST_BUFFER_OFFSET (outbuf) = adder->offset;
1224 GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->timestamp;
1226 GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1227 GST_BUFFER_OFFSET (outbuf) = next_offset;
1228 GST_BUFFER_DURATION (outbuf) = adder->timestamp - next_timestamp;
1231 adder->offset = next_offset;
1232 adder->timestamp = next_timestamp;
1235 GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1236 " offset %" G_GINT64_FORMAT, outbuf,
1237 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1238 GST_BUFFER_OFFSET (outbuf));
1239 ret = gst_pad_push (adder->srcpad, outbuf);
1241 GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1248 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1249 ("Unknown data received, not negotiated"));
1250 return GST_FLOW_NOT_NEGOTIATED;
1254 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1255 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1256 return GST_FLOW_UNEXPECTED;
1260 static GstStateChangeReturn
1261 gst_adder_change_state (GstElement * element, GstStateChange transition)
1264 GstStateChangeReturn ret;
1266 adder = GST_ADDER (element);
1268 switch (transition) {
1269 case GST_STATE_CHANGE_NULL_TO_READY:
1271 case GST_STATE_CHANGE_READY_TO_PAUSED:
1272 adder->timestamp = 0;
1274 adder->flush_stop_pending = FALSE;
1275 adder->segment_pending = TRUE;
1276 adder->segment_start = 0;
1277 adder->segment_end = GST_CLOCK_TIME_NONE;
1278 adder->segment_rate = 1.0;
1279 gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1280 gst_collect_pads_start (adder->collect);
1282 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1284 case GST_STATE_CHANGE_PAUSED_TO_READY:
1285 /* need to unblock the collectpads before calling the
1286 * parent change_state so that streaming can finish */
1287 gst_collect_pads_stop (adder->collect);
1293 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1295 switch (transition) {
1305 plugin_init (GstPlugin * plugin)
1307 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1314 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1317 "Adds multiple streams",
1318 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)