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 */
48 #include <gst/audio/audio.h>
49 #include <string.h> /* strcmp */
50 #include "gstadderorc.h"
52 /* highest positive/lowest negative x-bit value we can use for clamping */
53 #define MAX_INT_32 ((gint32) (0x7fffffff))
54 #define MAX_INT_16 ((gint16) (0x7fff))
55 #define MAX_INT_8 ((gint8) (0x7f))
56 #define MAX_UINT_32 ((guint32)(0xffffffff))
57 #define MAX_UINT_16 ((guint16)(0xffff))
58 #define MAX_UINT_8 ((guint8) (0xff))
60 #define MIN_INT_32 ((gint32) (0x80000000))
61 #define MIN_INT_16 ((gint16) (0x8000))
62 #define MIN_INT_8 ((gint8) (0x80))
63 #define MIN_UINT_32 ((guint32)(0x00000000))
64 #define MIN_UINT_16 ((guint16)(0x0000))
65 #define MIN_UINT_8 ((guint8) (0x00))
73 #define GST_CAT_DEFAULT gst_adder_debug
74 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
76 /* elementfactory information */
78 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
80 GST_AUDIO_CAPS_MAKE ("{ S32LE, U32LE, S16LE, U16LE, S8, U8, F32LE, F64LE }") \
81 ", layout = (string) { interleaved, non-interleaved }"
84 GST_AUDIO_CAPS_MAKE ("{ S32BE, U32BE, S16BE, U16BE, S8, U8, F32BE, F64BE }") \
85 ", layout = (string) { interleaved, non-interleaved }"
88 static GstStaticPadTemplate gst_adder_src_template =
89 GST_STATIC_PAD_TEMPLATE ("src",
92 GST_STATIC_CAPS (CAPS)
95 static GstStaticPadTemplate gst_adder_sink_template =
96 GST_STATIC_PAD_TEMPLATE ("sink_%u",
99 GST_STATIC_CAPS (CAPS)
102 #define gst_adder_parent_class parent_class
103 G_DEFINE_TYPE (GstAdder, gst_adder, GST_TYPE_ELEMENT);
105 static void gst_adder_dispose (GObject * object);
106 static void gst_adder_set_property (GObject * object, guint prop_id,
107 const GValue * value, GParamSpec * pspec);
108 static void gst_adder_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec);
111 static gboolean gst_adder_setcaps (GstAdder * adder, GstPad * pad,
113 static gboolean gst_adder_src_query (GstPad * pad, GstObject * parent,
115 static gboolean gst_adder_sink_query (GstPad * pad, GstObject * parent,
117 static gboolean gst_adder_src_event (GstPad * pad, GstObject * parent,
119 static gboolean gst_adder_sink_event (GstCollectPads2 * pads,
120 GstCollectData2 * pad, GstEvent * event, gpointer user_data);
122 static GstPad *gst_adder_request_new_pad (GstElement * element,
123 GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
124 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
126 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
127 GstStateChange transition);
129 static GstFlowReturn gst_adder_do_clip (GstCollectPads2 * pads,
130 GstCollectData2 * data, GstBuffer * buffer, GstBuffer ** out,
132 static GstFlowReturn gst_adder_collected (GstCollectPads2 * pads,
135 /* non-clipping versions (for float) */
136 #define MAKE_FUNC_NC(name,type) \
137 static void name (type *out, type *in, gint samples) { \
139 for (i = 0; i < samples; i++) \
144 MAKE_FUNC_NC (add_float64, gdouble)
147 /* we can only accept caps that we and downstream can handle.
148 * if we have filtercaps set, use those to constrain the target caps.
151 gst_adder_sink_getcaps (GstPad * pad, GstCaps * filter)
154 GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
156 adder = GST_ADDER (GST_PAD_PARENT (pad));
158 GST_OBJECT_LOCK (adder);
160 if ((filter_caps = adder->filter_caps)) {
163 gst_caps_intersect_full (filter, filter_caps,
164 GST_CAPS_INTERSECT_FIRST);
166 gst_caps_ref (filter_caps);
168 filter_caps = filter ? gst_caps_ref (filter) : NULL;
170 GST_OBJECT_UNLOCK (adder);
172 if (filter_caps && gst_caps_is_empty (filter_caps)) {
173 GST_WARNING_OBJECT (pad, "Empty filter caps");
177 /* get the downstream possible caps */
178 peercaps = gst_pad_peer_query_caps (adder->srcpad, filter_caps);
180 /* get the allowed caps on this sinkpad */
181 sinkcaps = gst_pad_get_current_caps (pad);
182 if (sinkcaps == NULL) {
183 sinkcaps = gst_pad_get_pad_template_caps (pad);
185 sinkcaps = gst_caps_new_any ();
189 /* if the peer has caps, intersect */
190 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
192 gst_caps_intersect_full (peercaps, sinkcaps, GST_CAPS_INTERSECT_FIRST);
193 gst_caps_unref (peercaps);
194 gst_caps_unref (sinkcaps);
196 /* the peer has no caps (or there is no peer), just use the allowed caps
197 * of this sinkpad. */
198 /* restrict with filter-caps if any */
200 GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
202 gst_caps_intersect_full (filter_caps, sinkcaps,
203 GST_CAPS_INTERSECT_FIRST);
204 gst_caps_unref (sinkcaps);
206 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
212 gst_caps_unref (filter_caps);
214 GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
215 GST_PAD_NAME (pad), result);
221 gst_adder_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
223 gboolean res = FALSE;
225 switch (GST_QUERY_TYPE (query)) {
228 GstCaps *filter, *caps;
230 gst_query_parse_caps (query, &filter);
231 caps = gst_adder_sink_getcaps (pad, filter);
232 gst_query_set_caps_result (query, caps);
233 gst_caps_unref (caps);
238 res = gst_pad_query_default (pad, parent, query);
251 setcapsfunc (const GValue * item, IterData * data)
253 GstPad *otherpad = g_value_get_object (item);
255 if (otherpad != data->pad)
256 gst_pad_set_caps (data->pad, data->caps);
259 /* the first caps we receive on any of the sinkpads will define the caps for all
260 * the other sinkpads because we can only mix streams with the same caps.
263 gst_adder_setcaps (GstAdder * adder, GstPad * pad, GstCaps * caps)
266 GstIteratorResult ires;
270 /* this get called recursively due to gst_iterator_foreach calling
271 * gst_pad_set_caps() */
272 if (adder->in_setcaps)
275 GST_LOG_OBJECT (adder, "setting caps pad %p,%s to %" GST_PTR_FORMAT, pad,
276 GST_PAD_NAME (pad), caps);
278 it = gst_element_iterate_pads (GST_ELEMENT_CAST (adder));
280 /* FIXME, see if the other pads can accept the format. Also lock the
281 * format on the other pads to this new format. */
285 adder->in_setcaps = TRUE;
288 ires = gst_iterator_foreach (it, (GstIteratorForeachFunction) setcapsfunc,
292 case GST_ITERATOR_RESYNC:
293 gst_iterator_resync (it);
300 adder->in_setcaps = FALSE;
302 GST_LOG_OBJECT (adder, "handle caps changes on pad %p,%s to %" GST_PTR_FORMAT,
303 pad, GST_PAD_NAME (pad), caps);
305 if (!gst_audio_info_from_caps (&adder->info, caps))
308 switch (GST_AUDIO_INFO_FORMAT (&adder->info)) {
309 case GST_AUDIO_FORMAT_S8:
310 adder->func = (GstAdderFunction) add_int8;
312 case GST_AUDIO_FORMAT_U8:
313 adder->func = (GstAdderFunction) add_uint8;
315 case GST_AUDIO_FORMAT_S16:
316 adder->func = (GstAdderFunction) add_int16;
318 case GST_AUDIO_FORMAT_U16:
319 adder->func = (GstAdderFunction) add_uint16;
321 case GST_AUDIO_FORMAT_S32:
322 adder->func = (GstAdderFunction) add_int32;
324 case GST_AUDIO_FORMAT_U32:
325 adder->func = (GstAdderFunction) add_uint32;
327 case GST_AUDIO_FORMAT_F32:
328 adder->func = (GstAdderFunction) add_float32;
330 case GST_AUDIO_FORMAT_F64:
331 adder->func = (GstAdderFunction) add_float64;
341 GST_DEBUG_OBJECT (adder, "invalid format set as caps");
346 /* FIXME, the duration query should reflect how long you will produce
347 * data, that is the amount of stream time until you will emit EOS.
349 * For synchronized mixing this is always the max of all the durations
350 * of upstream since we emit EOS when all of them finished.
352 * We don't do synchronized mixing so this really depends on where the
353 * streams where punched in and what their relative offsets are against
354 * eachother which we can get from the first timestamps we see.
356 * When we add a new stream (or remove a stream) the duration might
357 * also become invalid again and we need to post a new DURATION
358 * message to notify this fact to the parent.
359 * For now we take the max of all the upstream elements so the simple
360 * cases work at least somewhat.
363 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
370 GValue item = { 0, };
373 gst_query_parse_duration (query, &format, NULL);
379 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
381 GstIteratorResult ires;
383 ires = gst_iterator_next (it, &item);
385 case GST_ITERATOR_DONE:
388 case GST_ITERATOR_OK:
390 GstPad *pad = g_value_get_object (&item);
393 /* ask sink peer for duration */
394 res &= gst_pad_peer_query_duration (pad, format, &duration);
395 /* take max from all valid return values */
397 /* valid unknown length, stop searching */
398 if (duration == -1) {
402 /* else see if bigger than current max */
403 else if (duration > max)
406 g_value_reset (&item);
409 case GST_ITERATOR_RESYNC:
412 gst_iterator_resync (it);
420 g_value_unset (&item);
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;
441 GValue item = { 0, };
448 max = GST_CLOCK_TIME_NONE;
450 /* Take maximum of all latency values */
451 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
453 GstIteratorResult ires;
455 ires = gst_iterator_next (it, &item);
457 case GST_ITERATOR_DONE:
460 case GST_ITERATOR_OK:
462 GstPad *pad = g_value_get_object (&item);
464 GstClockTime min_cur, max_cur;
467 peerquery = gst_query_new_latency ();
469 /* Ask peer for latency */
470 res &= gst_pad_peer_query (pad, peerquery);
472 /* take max from all valid return values */
474 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
479 if (max_cur != GST_CLOCK_TIME_NONE &&
480 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
481 (max == GST_CLOCK_TIME_NONE)))
484 live = live || live_cur;
487 gst_query_unref (peerquery);
488 g_value_reset (&item);
491 case GST_ITERATOR_RESYNC:
494 max = GST_CLOCK_TIME_NONE;
496 gst_iterator_resync (it);
504 g_value_unset (&item);
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_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
521 GstAdder *adder = GST_ADDER (parent);
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->segment.position);
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, parent, query);
571 forward_event_func (const GValue * val, GValue * ret, EventData * data)
573 GstPad *pad = g_value_get_object (val);
574 GstEvent *event = data->event;
576 gst_event_ref (event);
577 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
578 if (!gst_pad_push_event (pad, event)) {
579 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
580 event, GST_EVENT_TYPE_NAME (event));
581 /* quick hack to unflush the pads, ideally we need a way to just unflush
582 * this single collect pad */
584 gst_pad_send_event (pad, gst_event_new_flush_stop (TRUE));
586 g_value_set_boolean (ret, TRUE);
587 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
588 event, GST_EVENT_TYPE_NAME (event));
591 /* continue on other pads, even if one failed */
595 /* forwards the event to all sinkpads, takes ownership of the
598 * Returns: TRUE if the event could be forwarded on all
602 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
606 GstIteratorResult ires;
610 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
611 GST_EVENT_TYPE_NAME (event));
616 g_value_init (&vret, G_TYPE_BOOLEAN);
617 g_value_set_boolean (&vret, FALSE);
618 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
621 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
624 case GST_ITERATOR_RESYNC:
625 GST_WARNING ("resync");
626 gst_iterator_resync (it);
627 g_value_set_boolean (&vret, TRUE);
629 case GST_ITERATOR_OK:
630 case GST_ITERATOR_DONE:
631 ret = g_value_get_boolean (&vret);
639 gst_iterator_free (it);
640 GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
641 GST_EVENT_TYPE_NAME (event), ret);
642 gst_event_unref (event);
648 gst_adder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
653 adder = GST_ADDER (parent);
655 GST_DEBUG_OBJECT (pad, "Got %s event on src pad",
656 GST_EVENT_TYPE_NAME (event));
658 switch (GST_EVENT_TYPE (event)) {
663 GstSeekType curtype, endtype;
667 /* parse the seek parameters */
668 gst_event_parse_seek (event, &rate, NULL, &flags, &curtype,
669 &cur, &endtype, &end);
671 if ((curtype != GST_SEEK_TYPE_NONE) && (curtype != GST_SEEK_TYPE_SET)) {
673 GST_DEBUG_OBJECT (adder,
674 "seeking failed, unhandled seek type for start: %d", curtype);
677 if ((endtype != GST_SEEK_TYPE_NONE) && (endtype != GST_SEEK_TYPE_SET)) {
679 GST_DEBUG_OBJECT (adder,
680 "seeking failed, unhandled seek type for end: %d", endtype);
684 flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
686 /* check if we are flushing */
688 /* flushing seek, start flush downstream, the flush will be done
689 * when all pads received a FLUSH_STOP.
690 * Make sure we accept nothing anymore and return WRONG_STATE.
691 * We send a flush-start before, to ensure no streaming is done
692 * as we need to take the stream lock.
694 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
695 gst_collect_pads2_set_flushing (adder->collect, TRUE);
697 /* We can't send FLUSH_STOP here since upstream could start pushing data
698 * after we unlock adder->collect.
699 * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
700 * forwarding the seek upstream or from gst_adder_collected,
701 * whichever happens first.
703 g_atomic_int_set (&adder->flush_stop_pending, TRUE);
705 GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
707 /* now wait for the collected to be finished and mark a new
708 * segment. After we have the lock, no collect function is running and no
709 * new collect function will be called for as long as we're flushing. */
710 GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
711 adder->segment.rate = rate;
712 if (curtype == GST_SEEK_TYPE_SET)
713 adder->segment.start = cur;
715 adder->segment.start = 0;
716 if (endtype == GST_SEEK_TYPE_SET)
717 adder->segment.stop = end;
719 adder->segment.stop = GST_CLOCK_TIME_NONE;
721 /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
722 * have stopped so that the cookie gets properly updated. */
723 gst_collect_pads2_set_flushing (adder->collect, TRUE);
725 GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
726 GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
729 /* we're forwarding seek to all upstream peers and wait for one to reply
730 * with a newsegment-event before we send a newsegment-event downstream */
731 g_atomic_int_set (&adder->wait_for_new_segment, TRUE);
732 result = forward_event (adder, event, flush);
734 /* seek failed. maybe source is a live source. */
735 GST_DEBUG_OBJECT (adder, "seeking failed");
737 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
739 GST_DEBUG_OBJECT (adder, "pending flush stop");
740 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
745 /* QoS might be tricky */
748 case GST_EVENT_NAVIGATION:
749 /* navigation is rather pointless. */
753 /* just forward the rest for now */
754 GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
755 GST_EVENT_TYPE_NAME (event));
756 result = forward_event (adder, event, FALSE);
766 gst_adder_sink_event (GstCollectPads2 * pads, GstCollectData2 * pad,
767 GstEvent * event, gpointer user_data)
769 GstAdder *adder = GST_ADDER (user_data);
770 gboolean res = FALSE;
772 GST_DEBUG_OBJECT (pad->pad, "Got %s event on sink pad",
773 GST_EVENT_TYPE_NAME (event));
775 switch (GST_EVENT_TYPE (event)) {
780 gst_event_parse_caps (event, &caps);
781 res = gst_adder_setcaps (adder, pad->pad, caps);
782 gst_event_unref (event);
786 case GST_EVENT_FLUSH_START:
787 res = gst_pad_event_default (pad->pad, GST_OBJECT (adder), event);
789 case GST_EVENT_FLUSH_STOP:
790 /* we received a flush-stop. We will only forward it when
791 * flush_stop_pending is set, and we will unset it then.
793 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
795 g_atomic_int_set (&adder->new_segment_pending, TRUE);
796 GST_DEBUG_OBJECT (pad->pad, "forwarding flush stop");
798 gst_event_unref (event);
800 GST_DEBUG_OBJECT (pad->pad, "eating flush stop");
802 /* Clear pending tags */
803 if (adder->pending_events) {
804 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
805 g_list_free (adder->pending_events);
806 adder->pending_events = NULL;
808 res = gst_pad_event_default (pad->pad, GST_OBJECT (adder), event);
811 /* collect tags here so we can push them out when we collect data */
812 adder->pending_events = g_list_append (adder->pending_events, event);
815 case GST_EVENT_SEGMENT:
816 if (g_atomic_int_compare_and_exchange (&adder->wait_for_new_segment,
818 /* make sure we push a new segment, to inform about new basetime
819 * see FIXME in gst_adder_collected() */
820 g_atomic_int_set (&adder->new_segment_pending, TRUE);
822 gst_event_unref (event);
826 gst_event_unref (event);
830 res = gst_pad_event_default (pad->pad, GST_OBJECT (adder), event);
838 gst_adder_class_init (GstAdderClass * klass)
840 GObjectClass *gobject_class = (GObjectClass *) klass;
841 GstElementClass *gstelement_class = (GstElementClass *) klass;
843 gobject_class->set_property = gst_adder_set_property;
844 gobject_class->get_property = gst_adder_get_property;
845 gobject_class->dispose = gst_adder_dispose;
852 g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
853 g_param_spec_boxed ("caps", "Target caps",
854 "Set target format for mixing (NULL means ANY). "
855 "Setting this property takes a reference to the supplied GstCaps "
856 "object.", GST_TYPE_CAPS,
857 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
859 gst_element_class_add_pad_template (gstelement_class,
860 gst_static_pad_template_get (&gst_adder_src_template));
861 gst_element_class_add_pad_template (gstelement_class,
862 gst_static_pad_template_get (&gst_adder_sink_template));
863 gst_element_class_set_details_simple (gstelement_class, "Adder",
865 "Add N audio channels together",
866 "Thomas Vander Stichele <thomas at apestaart dot org>");
868 gstelement_class->request_new_pad =
869 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
870 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
871 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
875 gst_adder_init (GstAdder * adder)
877 GstPadTemplate *template;
879 template = gst_static_pad_template_get (&gst_adder_src_template);
880 adder->srcpad = gst_pad_new_from_template (template, "src");
881 gst_object_unref (template);
883 gst_pad_set_query_function (adder->srcpad,
884 GST_DEBUG_FUNCPTR (gst_adder_src_query));
885 gst_pad_set_event_function (adder->srcpad,
886 GST_DEBUG_FUNCPTR (gst_adder_src_event));
887 GST_PAD_SET_PROXY_CAPS (adder->srcpad);
888 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
890 gst_audio_info_init (&adder->info);
894 adder->filter_caps = NULL;
896 /* keep track of the sinkpads requested */
897 adder->collect = gst_collect_pads2_new ();
898 gst_collect_pads2_set_function (adder->collect,
899 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
900 gst_collect_pads2_set_clip_function (adder->collect,
901 GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
902 gst_collect_pads2_set_event_function (adder->collect,
903 GST_DEBUG_FUNCPTR (gst_adder_sink_event), adder);
907 gst_adder_dispose (GObject * object)
909 GstAdder *adder = GST_ADDER (object);
911 if (adder->collect) {
912 gst_object_unref (adder->collect);
913 adder->collect = NULL;
915 gst_caps_replace (&adder->filter_caps, NULL);
916 if (adder->pending_events) {
917 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
918 g_list_free (adder->pending_events);
919 adder->pending_events = NULL;
922 G_OBJECT_CLASS (parent_class)->dispose (object);
926 gst_adder_set_property (GObject * object, guint prop_id,
927 const GValue * value, GParamSpec * pspec)
929 GstAdder *adder = GST_ADDER (object);
932 case PROP_FILTER_CAPS:{
933 GstCaps *new_caps = NULL;
935 const GstCaps *new_caps_val = gst_value_get_caps (value);
937 if (new_caps_val != NULL) {
938 new_caps = (GstCaps *) new_caps_val;
939 gst_caps_ref (new_caps);
942 GST_OBJECT_LOCK (adder);
943 old_caps = adder->filter_caps;
944 adder->filter_caps = new_caps;
945 GST_OBJECT_UNLOCK (adder);
948 gst_caps_unref (old_caps);
950 GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
954 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
960 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
963 GstAdder *adder = GST_ADDER (object);
966 case PROP_FILTER_CAPS:
967 GST_OBJECT_LOCK (adder);
968 gst_value_set_caps (value, adder->filter_caps);
969 GST_OBJECT_UNLOCK (adder);
972 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
979 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
980 const gchar * unused, const GstCaps * caps)
987 if (templ->direction != GST_PAD_SINK)
990 adder = GST_ADDER (element);
992 /* increment pad counter */
993 padcount = g_atomic_int_add (&adder->padcount, 1);
995 name = g_strdup_printf ("sink_%u", padcount);
996 newpad = gst_pad_new_from_template (templ, name);
997 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
1000 gst_pad_set_query_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_query));
1001 gst_collect_pads2_add_pad (adder->collect, newpad, sizeof (GstCollectData2));
1003 /* takes ownership of the pad */
1004 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
1012 g_warning ("gstadder: request new pad that is not a SINK pad\n");
1017 GST_DEBUG_OBJECT (adder, "could not add pad");
1018 gst_collect_pads2_remove_pad (adder->collect, newpad);
1019 gst_object_unref (newpad);
1025 gst_adder_release_pad (GstElement * element, GstPad * pad)
1029 adder = GST_ADDER (element);
1031 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1034 gst_collect_pads2_remove_pad (adder->collect, pad);
1035 gst_element_remove_pad (element, pad);
1038 static GstFlowReturn
1039 gst_adder_do_clip (GstCollectPads2 * pads, GstCollectData2 * data,
1040 GstBuffer * buffer, GstBuffer ** out, gpointer user_data)
1042 GstAdder *adder = GST_ADDER (user_data);
1045 rate = GST_AUDIO_INFO_RATE (&adder->info);
1046 bpf = GST_AUDIO_INFO_BPF (&adder->info);
1048 buffer = gst_audio_buffer_clip (buffer, &data->segment, rate, bpf);
1054 static GstFlowReturn
1055 gst_adder_collected (GstCollectPads2 * pads, gpointer user_data)
1058 * combine streams by adding data values
1060 * - this function is called when all pads have a buffer
1061 * - get available bytes on all pads.
1062 * - repeat for each input pad :
1063 * - read available bytes, copy or add to target buffer
1064 * - if there's an EOS event, remove the input channel
1065 * - push out the output buffer
1068 * - would be nice to have a mixing mode, where instead of adding we mix
1069 * - for float we could downscale after collect loop
1070 * - for int we need to downscale each input to avoid clipping or
1071 * mix into a temp (float) buffer and scale afterwards as well
1074 GSList *collected, *next = NULL;
1076 GstBuffer *outbuf = NULL, *gapbuf = NULL;
1077 GstMapInfo outmap = { NULL };
1080 gint64 next_timestamp;
1081 gint rate, bps, bpf;
1083 adder = GST_ADDER (user_data);
1086 if (G_UNLIKELY (adder->func == NULL))
1087 goto not_negotiated;
1089 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1091 GST_DEBUG_OBJECT (adder, "pending flush stop");
1092 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
1095 /* get available bytes for reading, this can be 0 which could mean empty
1096 * buffers or EOS, which we will catch when we loop over the pads. */
1097 outsize = gst_collect_pads2_available (pads);
1098 /* can only happen when no pads to collect or all EOS */
1102 rate = GST_AUDIO_INFO_RATE (&adder->info);
1103 bps = GST_AUDIO_INFO_BPS (&adder->info);
1104 bpf = GST_AUDIO_INFO_BPF (&adder->info);
1106 GST_LOG_OBJECT (adder,
1107 "starting to cycle through channels, %d bytes available (bps = %d, bpf = %d)",
1110 for (collected = pads->data; collected; collected = next) {
1111 GstCollectData2 *collect_data;
1115 /* take next to see if this is the last collectdata */
1116 next = g_slist_next (collected);
1118 collect_data = (GstCollectData2 *) collected->data;
1120 /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1122 inbuf = gst_collect_pads2_take_buffer (pads, collect_data, outsize);
1123 /* NULL means EOS or an empty buffer so we still need to flush in
1124 * case of an empty buffer. */
1125 if (inbuf == NULL) {
1126 GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1130 is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1132 /* Try to make an output buffer */
1133 if (outbuf == NULL) {
1134 /* if this is a gap buffer but we have some more pads to check, skip it.
1135 * If we are at the last buffer, take it, regardless if it is a GAP
1137 if (is_gap && next) {
1138 GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1139 /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1140 * EOS, we can use this one as the output buffer. */
1144 gst_buffer_unref (inbuf);
1148 GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1149 collect_data, outsize);
1151 /* make data and metadata writable, can simply return the inbuf when we
1152 * are the only one referencing this buffer. If this is the last (and
1153 * only) GAP buffer, it will automatically copy the GAP flag. */
1154 outbuf = gst_buffer_make_writable (inbuf);
1155 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1158 /* we had a previous output buffer, mix this non-GAP buffer */
1161 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
1163 /* all buffers should have outsize, there are no short buffers because we
1164 * asked for the max size above */
1165 g_assert (inmap.size == outmap.size);
1167 GST_LOG_OBJECT (adder, "channel %p: mixing %" G_GSIZE_FORMAT " bytes"
1168 " from data %p", collect_data, inmap.size, inmap.data);
1170 /* further buffers, need to add them */
1171 adder->func ((gpointer) outmap.data, (gpointer) inmap.data,
1173 gst_buffer_unmap (inbuf, &inmap);
1175 /* skip gap buffer */
1176 GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1178 gst_buffer_unref (inbuf);
1182 gst_buffer_unmap (outbuf, &outmap);
1184 if (outbuf == NULL) {
1185 /* no output buffer, reuse one of the GAP buffers then if we have one */
1187 GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1190 /* assume EOS otherwise, this should not happen, really */
1193 /* we had an output buffer, unref the gapbuffer we kept */
1194 gst_buffer_unref (gapbuf);
1196 if (g_atomic_int_compare_and_exchange (&adder->new_segment_pending, TRUE,
1200 /* FIXME, use rate/applied_rate as set on all sinkpads.
1201 * - currently we just set rate as received from last seek-event
1203 * When seeking we set the start and stop positions as given in the seek
1204 * event. We also adjust offset & timestamp accordingly.
1205 * This basically ignores all newsegments sent by upstream.
1207 event = gst_event_new_segment (&adder->segment);
1209 if (adder->segment.rate > 0.0) {
1210 adder->segment.position = adder->segment.start;
1212 adder->segment.position = adder->segment.stop;
1214 adder->offset = gst_util_uint64_scale (adder->segment.position,
1216 GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1217 G_GUINT64_FORMAT, adder->segment.start, adder->segment.stop);
1218 GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1219 G_GINT64_FORMAT, adder->segment.position, adder->offset);
1222 if (!gst_pad_push_event (adder->srcpad, event)) {
1223 GST_WARNING_OBJECT (adder->srcpad, "Sending new segment event failed");
1226 GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1227 "start:%" G_GINT64_FORMAT " end:%" G_GINT64_FORMAT " failed",
1228 adder->segment.start, adder->segment.stop);
1232 if (G_UNLIKELY (adder->pending_events)) {
1233 GList *tmp = adder->pending_events;
1236 GstEvent *ev = (GstEvent *) tmp->data;
1238 gst_pad_push_event (adder->srcpad, ev);
1239 tmp = g_list_next (tmp);
1241 g_list_free (adder->pending_events);
1242 adder->pending_events = NULL;
1245 /* for the next timestamp, use the sample counter, which will
1246 * never accumulate rounding errors */
1247 if (adder->segment.rate > 0.0) {
1248 next_offset = adder->offset + outsize / bpf;
1250 next_offset = adder->offset - outsize / bpf;
1252 next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, rate);
1255 /* set timestamps on the output buffer */
1256 if (adder->segment.rate > 0.0) {
1257 GST_BUFFER_TIMESTAMP (outbuf) = adder->segment.position;
1258 GST_BUFFER_OFFSET (outbuf) = adder->offset;
1259 GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1260 GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->segment.position;
1262 GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1263 GST_BUFFER_OFFSET (outbuf) = next_offset;
1264 GST_BUFFER_OFFSET_END (outbuf) = adder->offset;
1265 GST_BUFFER_DURATION (outbuf) = adder->segment.position - next_timestamp;
1268 adder->offset = next_offset;
1269 adder->segment.position = next_timestamp;
1272 GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1273 " offset %" G_GINT64_FORMAT, outbuf,
1274 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1275 GST_BUFFER_OFFSET (outbuf));
1276 ret = gst_pad_push (adder->srcpad, outbuf);
1278 GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1285 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1286 ("Unknown data received, not negotiated"));
1287 return GST_FLOW_NOT_NEGOTIATED;
1291 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1292 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1293 return GST_FLOW_EOS;
1297 static GstStateChangeReturn
1298 gst_adder_change_state (GstElement * element, GstStateChange transition)
1301 GstStateChangeReturn ret;
1303 adder = GST_ADDER (element);
1305 switch (transition) {
1306 case GST_STATE_CHANGE_NULL_TO_READY:
1308 case GST_STATE_CHANGE_READY_TO_PAUSED:
1309 adder->segment.position = 0;
1311 adder->flush_stop_pending = FALSE;
1312 adder->new_segment_pending = TRUE;
1313 adder->wait_for_new_segment = FALSE;
1314 gst_segment_init (&adder->segment, GST_FORMAT_TIME);
1315 gst_collect_pads2_start (adder->collect);
1317 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1319 case GST_STATE_CHANGE_PAUSED_TO_READY:
1320 /* need to unblock the collectpads2 before calling the
1321 * parent change_state so that streaming can finish */
1322 gst_collect_pads2_stop (adder->collect);
1328 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1330 switch (transition) {
1339 plugin_init (GstPlugin * plugin)
1341 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
1342 "audio channel mixing element");
1344 gst_adder_orc_init ();
1346 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1353 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1356 "Adds multiple streams",
1357 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)