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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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"
58 #define GST_CAT_DEFAULT gst_adder_debug
59 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
61 /* elementfactory information */
63 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
65 GST_AUDIO_CAPS_MAKE ("{ S32LE, U32LE, S16LE, U16LE, S8, U8, F32LE, F64LE }") \
66 ", layout = (string) { interleaved, non-interleaved }"
69 GST_AUDIO_CAPS_MAKE ("{ S32BE, U32BE, S16BE, U16BE, S8, U8, F32BE, F64BE }") \
70 ", layout = (string) { interleaved, non-interleaved }"
73 static GstStaticPadTemplate gst_adder_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS (CAPS)
80 static GstStaticPadTemplate gst_adder_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink_%u",
84 GST_STATIC_CAPS (CAPS)
87 #define gst_adder_parent_class parent_class
88 G_DEFINE_TYPE (GstAdder, gst_adder, GST_TYPE_ELEMENT);
90 static void gst_adder_dispose (GObject * object);
91 static void gst_adder_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_adder_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static gboolean gst_adder_setcaps (GstAdder * adder, GstPad * pad,
98 static gboolean gst_adder_src_query (GstPad * pad, GstObject * parent,
100 static gboolean gst_adder_sink_query (GstCollectPads * pads,
101 GstCollectData * pad, GstQuery * query, gpointer user_data);
102 static gboolean gst_adder_src_event (GstPad * pad, GstObject * parent,
104 static gboolean gst_adder_sink_event (GstCollectPads * pads,
105 GstCollectData * pad, GstEvent * event, gpointer user_data);
107 static GstPad *gst_adder_request_new_pad (GstElement * element,
108 GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
109 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
111 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
112 GstStateChange transition);
114 static GstFlowReturn gst_adder_do_clip (GstCollectPads * pads,
115 GstCollectData * data, GstBuffer * buffer, GstBuffer ** out,
117 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
120 /* non-clipping versions (for float) */
121 #define MAKE_FUNC_NC(name,type) \
122 static void name (type *out, type *in, gint samples) { \
124 for (i = 0; i < samples; i++) \
129 MAKE_FUNC_NC (adder_orc_add_float64, gdouble)
132 /* we can only accept caps that we and downstream can handle.
133 * if we have filtercaps set, use those to constrain the target caps.
136 gst_adder_sink_getcaps (GstPad * pad, GstCaps * filter)
139 GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
141 adder = GST_ADDER (GST_PAD_PARENT (pad));
143 GST_OBJECT_LOCK (adder);
145 if ((filter_caps = adder->filter_caps)) {
148 gst_caps_intersect_full (filter, filter_caps,
149 GST_CAPS_INTERSECT_FIRST);
151 gst_caps_ref (filter_caps);
153 filter_caps = filter ? gst_caps_ref (filter) : NULL;
155 GST_OBJECT_UNLOCK (adder);
157 if (filter_caps && gst_caps_is_empty (filter_caps)) {
158 GST_WARNING_OBJECT (pad, "Empty filter caps");
162 /* get the downstream possible caps */
163 peercaps = gst_pad_peer_query_caps (adder->srcpad, filter_caps);
165 /* get the allowed caps on this sinkpad */
166 sinkcaps = gst_pad_get_current_caps (pad);
167 if (sinkcaps == NULL) {
168 sinkcaps = gst_pad_get_pad_template_caps (pad);
170 sinkcaps = gst_caps_new_any ();
174 /* if the peer has caps, intersect */
175 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
177 gst_caps_intersect_full (peercaps, sinkcaps, GST_CAPS_INTERSECT_FIRST);
178 gst_caps_unref (peercaps);
179 gst_caps_unref (sinkcaps);
181 /* the peer has no caps (or there is no peer), just use the allowed caps
182 * of this sinkpad. */
183 /* restrict with filter-caps if any */
185 GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
187 gst_caps_intersect_full (filter_caps, sinkcaps,
188 GST_CAPS_INTERSECT_FIRST);
189 gst_caps_unref (sinkcaps);
191 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
197 gst_caps_unref (filter_caps);
199 GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
200 GST_PAD_NAME (pad), result);
206 gst_adder_sink_query (GstCollectPads * pads, GstCollectData * pad,
207 GstQuery * query, gpointer user_data)
209 gboolean res = FALSE;
211 switch (GST_QUERY_TYPE (query)) {
214 GstCaps *filter, *caps;
216 gst_query_parse_caps (query, &filter);
217 caps = gst_adder_sink_getcaps (pad->pad, filter);
218 gst_query_set_caps_result (query, caps);
219 gst_caps_unref (caps);
224 res = gst_collect_pads_query_default (pads, pad, query, FALSE);
238 setcapsfunc (const GValue * item, IterData * data)
240 GstPad *otherpad = g_value_get_object (item);
242 if (otherpad != data->pad) {
243 GST_LOG_OBJECT (data->pad, "calling set_caps with %" GST_PTR_FORMAT,
245 gst_pad_set_caps (data->pad, data->caps);
246 gst_pad_use_fixed_caps (data->pad);
250 /* the first caps we receive on any of the sinkpads will define the caps for all
251 * the other sinkpads because we can only mix streams with the same caps.
254 gst_adder_setcaps (GstAdder * adder, GstPad * pad, GstCaps * caps)
257 GstIteratorResult ires;
261 /* this gets called recursively due to gst_iterator_foreach calling
262 * gst_pad_set_caps() */
263 if (adder->in_setcaps)
266 /* don't allow reconfiguration for now; there's still a race between the
267 * different upstream threads doing query_caps + accept_caps + sending
268 * (possibly different) CAPS events, but there's not much we can do about
269 * that, upstream needs to deal with it. */
270 if (adder->current_caps != NULL) {
271 /* FIXME: not quite right for optional fields such as channel-mask, which
272 * may or may not be present for mono/stereo */
273 if (gst_caps_is_equal (caps, adder->current_caps)) {
276 GST_DEBUG_OBJECT (pad, "got input caps %" GST_PTR_FORMAT ", but "
277 "current caps are %" GST_PTR_FORMAT, caps, adder->current_caps);
278 gst_pad_push_event (pad, gst_event_new_reconfigure ());
283 GST_INFO_OBJECT (pad, "setting caps to %" GST_PTR_FORMAT, caps);
285 adder->current_caps = gst_caps_ref (caps);
286 /* send caps event later, after stream-start event */
288 it = gst_element_iterate_pads (GST_ELEMENT_CAST (adder));
290 /* FIXME, see if the other pads can accept the format. Also lock the
291 * format on the other pads to this new format. */
295 adder->in_setcaps = TRUE;
298 ires = gst_iterator_foreach (it, (GstIteratorForeachFunction) setcapsfunc,
302 case GST_ITERATOR_RESYNC:
303 gst_iterator_resync (it);
310 adder->in_setcaps = FALSE;
311 gst_iterator_free (it);
313 GST_INFO_OBJECT (pad, "handle caps change to %" GST_PTR_FORMAT, caps);
315 if (!gst_audio_info_from_caps (&adder->info, caps))
318 switch (GST_AUDIO_INFO_FORMAT (&adder->info)) {
319 case GST_AUDIO_FORMAT_S8:
320 adder->func = (GstAdderFunction) adder_orc_add_int8;
322 case GST_AUDIO_FORMAT_U8:
323 adder->func = (GstAdderFunction) adder_orc_add_uint8;
325 case GST_AUDIO_FORMAT_S16:
326 adder->func = (GstAdderFunction) adder_orc_add_int16;
328 case GST_AUDIO_FORMAT_U16:
329 adder->func = (GstAdderFunction) adder_orc_add_uint16;
331 case GST_AUDIO_FORMAT_S32:
332 adder->func = (GstAdderFunction) adder_orc_add_int32;
334 case GST_AUDIO_FORMAT_U32:
335 adder->func = (GstAdderFunction) adder_orc_add_uint32;
337 case GST_AUDIO_FORMAT_F32:
338 adder->func = (GstAdderFunction) adder_orc_add_float32;
340 case GST_AUDIO_FORMAT_F64:
341 adder->func = (GstAdderFunction) adder_orc_add_float64;
351 GST_WARNING_OBJECT (adder, "invalid format set as caps");
356 /* FIXME, the duration query should reflect how long you will produce
357 * data, that is the amount of stream time until you will emit EOS.
359 * For synchronized mixing this is always the max of all the durations
360 * of upstream since we emit EOS when all of them finished.
362 * We don't do synchronized mixing so this really depends on where the
363 * streams where punched in and what their relative offsets are against
364 * eachother which we can get from the first timestamps we see.
366 * When we add a new stream (or remove a stream) the duration might
367 * also become invalid again and we need to post a new DURATION
368 * message to notify this fact to the parent.
369 * For now we take the max of all the upstream elements so the simple
370 * cases work at least somewhat.
373 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
380 GValue item = { 0, };
383 gst_query_parse_duration (query, &format, NULL);
389 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
391 GstIteratorResult ires;
393 ires = gst_iterator_next (it, &item);
395 case GST_ITERATOR_DONE:
398 case GST_ITERATOR_OK:
400 GstPad *pad = g_value_get_object (&item);
403 /* ask sink peer for duration */
404 res &= gst_pad_peer_query_duration (pad, format, &duration);
405 /* take max from all valid return values */
407 /* valid unknown length, stop searching */
408 if (duration == -1) {
412 /* else see if bigger than current max */
413 else if (duration > max)
416 g_value_reset (&item);
419 case GST_ITERATOR_RESYNC:
422 gst_iterator_resync (it);
430 g_value_unset (&item);
431 gst_iterator_free (it);
434 /* and store the max */
435 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
436 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
437 gst_query_set_duration (query, format, max);
444 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
446 GstClockTime min, max;
451 GValue item = { 0, };
458 max = GST_CLOCK_TIME_NONE;
460 /* Take maximum of all latency values */
461 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
463 GstIteratorResult ires;
465 ires = gst_iterator_next (it, &item);
467 case GST_ITERATOR_DONE:
470 case GST_ITERATOR_OK:
472 GstPad *pad = g_value_get_object (&item);
474 GstClockTime min_cur, max_cur;
477 peerquery = gst_query_new_latency ();
479 /* Ask peer for latency */
480 res &= gst_pad_peer_query (pad, peerquery);
482 /* take max from all valid return values */
484 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
489 if (max_cur != GST_CLOCK_TIME_NONE &&
490 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
491 (max == GST_CLOCK_TIME_NONE)))
494 live = live || live_cur;
497 gst_query_unref (peerquery);
498 g_value_reset (&item);
501 case GST_ITERATOR_RESYNC:
504 max = GST_CLOCK_TIME_NONE;
506 gst_iterator_resync (it);
514 g_value_unset (&item);
515 gst_iterator_free (it);
518 /* store the results */
519 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
520 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
521 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
522 gst_query_set_latency (query, live, min, max);
529 gst_adder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
531 GstAdder *adder = GST_ADDER (parent);
532 gboolean res = FALSE;
534 switch (GST_QUERY_TYPE (query)) {
535 case GST_QUERY_POSITION:
539 gst_query_parse_position (query, &format, NULL);
542 case GST_FORMAT_TIME:
543 /* FIXME, bring to stream time, might be tricky */
544 gst_query_set_position (query, format, adder->segment.position);
547 case GST_FORMAT_DEFAULT:
548 gst_query_set_position (query, format, adder->offset);
556 case GST_QUERY_DURATION:
557 res = gst_adder_query_duration (adder, query);
559 case GST_QUERY_LATENCY:
560 res = gst_adder_query_latency (adder, query);
563 /* FIXME, needs a custom query handler because we have multiple
565 res = gst_pad_query_default (pad, parent, query);
581 forward_event_func (const GValue * val, GValue * ret, EventData * data)
583 GstPad *pad = g_value_get_object (val);
584 GstEvent *event = data->event;
587 gst_event_ref (event);
588 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
589 peer = gst_pad_get_peer (pad);
590 /* collect pad might have been set flushing,
591 * so bypass core checking that and send directly to peer */
592 if (!peer || !gst_pad_send_event (peer, event)) {
594 gst_event_unref (event);
595 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
596 event, GST_EVENT_TYPE_NAME (event));
597 /* quick hack to unflush the pads, ideally we need a way to just unflush
598 * this single collect pad */
600 gst_pad_send_event (pad, gst_event_new_flush_stop (TRUE));
602 g_value_set_boolean (ret, TRUE);
603 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
604 event, GST_EVENT_TYPE_NAME (event));
607 gst_object_unref (peer);
609 /* continue on other pads, even if one failed */
613 /* forwards the event to all sinkpads, takes ownership of the
616 * Returns: TRUE if the event could be forwarded on all
620 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
624 GstIteratorResult ires;
628 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
629 GST_EVENT_TYPE_NAME (event));
634 g_value_init (&vret, G_TYPE_BOOLEAN);
635 g_value_set_boolean (&vret, FALSE);
636 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
639 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
642 case GST_ITERATOR_RESYNC:
643 GST_WARNING ("resync");
644 gst_iterator_resync (it);
645 g_value_set_boolean (&vret, TRUE);
647 case GST_ITERATOR_OK:
648 case GST_ITERATOR_DONE:
649 ret = g_value_get_boolean (&vret);
657 gst_iterator_free (it);
658 GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
659 GST_EVENT_TYPE_NAME (event), ret);
660 gst_event_unref (event);
666 gst_adder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
671 adder = GST_ADDER (parent);
673 GST_DEBUG_OBJECT (pad, "Got %s event on src pad",
674 GST_EVENT_TYPE_NAME (event));
676 switch (GST_EVENT_TYPE (event)) {
681 GstSeekType start_type, stop_type;
683 GstFormat seek_format, dest_format;
686 /* parse the seek parameters */
687 gst_event_parse_seek (event, &rate, &seek_format, &flags, &start_type,
688 &start, &stop_type, &stop);
690 if ((start_type != GST_SEEK_TYPE_NONE)
691 && (start_type != GST_SEEK_TYPE_SET)) {
693 GST_DEBUG_OBJECT (adder,
694 "seeking failed, unhandled seek type for start: %d", start_type);
697 if ((stop_type != GST_SEEK_TYPE_NONE) && (stop_type != GST_SEEK_TYPE_SET)) {
699 GST_DEBUG_OBJECT (adder,
700 "seeking failed, unhandled seek type for end: %d", stop_type);
704 dest_format = adder->segment.format;
705 if (seek_format != dest_format) {
707 GST_DEBUG_OBJECT (adder,
708 "seeking failed, unhandled seek format: %d", seek_format);
712 flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
714 /* check if we are flushing */
716 /* flushing seek, start flush downstream, the flush will be done
717 * when all pads received a FLUSH_STOP.
718 * Make sure we accept nothing anymore and return WRONG_STATE.
719 * We send a flush-start before, to ensure no streaming is done
720 * as we need to take the stream lock.
722 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
723 gst_collect_pads_set_flushing (adder->collect, TRUE);
725 /* We can't send FLUSH_STOP here since upstream could start pushing data
726 * after we unlock adder->collect.
727 * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
728 * forwarding the seek upstream or from gst_adder_collected,
729 * whichever happens first.
731 g_atomic_int_set (&adder->flush_stop_pending, TRUE);
732 GST_DEBUG_OBJECT (adder, "mark pending flush stop event");
734 GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
736 /* now wait for the collected to be finished and mark a new
737 * segment. After we have the lock, no collect function is running and no
738 * new collect function will be called for as long as we're flushing. */
739 GST_COLLECT_PADS_STREAM_LOCK (adder->collect);
740 /* clip position and update our segment */
741 if (adder->segment.stop != -1) {
742 adder->segment.position = adder->segment.stop;
744 gst_segment_do_seek (&adder->segment, rate, seek_format, flags,
745 start_type, start, stop_type, stop, NULL);
748 /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
749 * have stopped so that the cookie gets properly updated. */
750 gst_collect_pads_set_flushing (adder->collect, TRUE);
752 GST_COLLECT_PADS_STREAM_UNLOCK (adder->collect);
753 GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
755 GST_DEBUG_OBJECT (adder, "updated segment: %" GST_SEGMENT_FORMAT,
758 /* we're forwarding seek to all upstream peers and wait for one to reply
759 * with a newsegment-event before we send a newsegment-event downstream */
760 g_atomic_int_set (&adder->wait_for_new_segment, TRUE);
761 result = forward_event (adder, event, flush);
763 /* seek failed. maybe source is a live source. */
764 GST_DEBUG_OBJECT (adder, "seeking failed");
766 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
768 GST_DEBUG_OBJECT (adder, "pending flush stop");
769 if (!gst_pad_push_event (adder->srcpad,
770 gst_event_new_flush_stop (TRUE))) {
771 GST_WARNING_OBJECT (adder, "Sending flush stop event failed");
777 /* QoS might be tricky */
779 gst_event_unref (event);
781 case GST_EVENT_NAVIGATION:
782 /* navigation is rather pointless. */
784 gst_event_unref (event);
787 /* just forward the rest for now */
788 GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
789 GST_EVENT_TYPE_NAME (event));
790 result = forward_event (adder, event, FALSE);
800 gst_adder_sink_event (GstCollectPads * pads, GstCollectData * pad,
801 GstEvent * event, gpointer user_data)
803 GstAdder *adder = GST_ADDER (user_data);
804 gboolean res = TRUE, discard = FALSE;
806 GST_DEBUG_OBJECT (pad->pad, "Got %s event on sink pad",
807 GST_EVENT_TYPE_NAME (event));
809 switch (GST_EVENT_TYPE (event)) {
814 gst_event_parse_caps (event, &caps);
815 res = gst_adder_setcaps (adder, pad->pad, caps);
816 gst_event_unref (event);
819 case GST_EVENT_FLUSH_START:
820 /* discard flush start events, as we forwarded one already when handing the
821 * flushing seek on the sink pad */
822 g_atomic_int_set (&adder->need_flush_stop, TRUE);
824 GST_DEBUG_OBJECT (pad->pad, "eating flush start");
826 case GST_EVENT_FLUSH_STOP:
827 /* we received a flush-stop. We will only forward it when
828 * flush_stop_pending is set, and we will unset it then.
830 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
832 g_atomic_int_set (&adder->new_segment_pending, TRUE);
833 GST_DEBUG_OBJECT (pad->pad, "forwarding flush stop");
836 GST_DEBUG_OBJECT (pad->pad, "eating flush stop");
838 /* Clear pending tags */
839 if (adder->pending_events) {
840 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
841 g_list_free (adder->pending_events);
842 adder->pending_events = NULL;
846 /* collect tags here so we can push them out when we collect data */
847 adder->pending_events = g_list_append (adder->pending_events, event);
850 case GST_EVENT_SEGMENT:
851 if (g_atomic_int_compare_and_exchange (&adder->wait_for_new_segment,
853 /* make sure we push a new segment, to inform about new basetime
854 * see FIXME in gst_adder_collected() */
855 g_atomic_int_set (&adder->new_segment_pending, TRUE);
857 if (g_atomic_int_compare_and_exchange (&adder->need_flush_stop,
859 /* ensure that we'll eventually send a flush-stop
860 * (e.g. after a flushing seek directly sent to an upstream element) */
861 g_atomic_int_set (&adder->flush_stop_pending, TRUE);
862 GST_DEBUG_OBJECT (adder, "mark pending flush stop event");
870 if (G_LIKELY (event))
871 return gst_collect_pads_event_default (pads, pad, event, discard);
877 gst_adder_class_init (GstAdderClass * klass)
879 GObjectClass *gobject_class = (GObjectClass *) klass;
880 GstElementClass *gstelement_class = (GstElementClass *) klass;
882 gobject_class->set_property = gst_adder_set_property;
883 gobject_class->get_property = gst_adder_get_property;
884 gobject_class->dispose = gst_adder_dispose;
886 g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
887 g_param_spec_boxed ("caps", "Target caps",
888 "Set target format for mixing (NULL means ANY). "
889 "Setting this property takes a reference to the supplied GstCaps "
890 "object.", GST_TYPE_CAPS,
891 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
893 gst_element_class_add_pad_template (gstelement_class,
894 gst_static_pad_template_get (&gst_adder_src_template));
895 gst_element_class_add_pad_template (gstelement_class,
896 gst_static_pad_template_get (&gst_adder_sink_template));
897 gst_element_class_set_static_metadata (gstelement_class, "Adder",
899 "Add N audio channels together",
900 "Thomas Vander Stichele <thomas at apestaart dot org>");
902 gstelement_class->request_new_pad =
903 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
904 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
905 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
909 gst_adder_init (GstAdder * adder)
911 GstPadTemplate *template;
913 template = gst_static_pad_template_get (&gst_adder_src_template);
914 adder->srcpad = gst_pad_new_from_template (template, "src");
915 gst_object_unref (template);
917 gst_pad_set_query_function (adder->srcpad,
918 GST_DEBUG_FUNCPTR (gst_adder_src_query));
919 gst_pad_set_event_function (adder->srcpad,
920 GST_DEBUG_FUNCPTR (gst_adder_src_event));
921 GST_PAD_SET_PROXY_CAPS (adder->srcpad);
922 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
924 adder->current_caps = NULL;
925 gst_audio_info_init (&adder->info);
929 adder->filter_caps = NULL;
931 /* keep track of the sinkpads requested */
932 adder->collect = gst_collect_pads_new ();
933 gst_collect_pads_set_function (adder->collect,
934 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
935 gst_collect_pads_set_clip_function (adder->collect,
936 GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
937 gst_collect_pads_set_event_function (adder->collect,
938 GST_DEBUG_FUNCPTR (gst_adder_sink_event), adder);
939 gst_collect_pads_set_query_function (adder->collect,
940 GST_DEBUG_FUNCPTR (gst_adder_sink_query), adder);
944 gst_adder_dispose (GObject * object)
946 GstAdder *adder = GST_ADDER (object);
948 if (adder->collect) {
949 gst_object_unref (adder->collect);
950 adder->collect = NULL;
952 gst_caps_replace (&adder->filter_caps, NULL);
953 gst_caps_replace (&adder->current_caps, NULL);
955 if (adder->pending_events) {
956 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
957 g_list_free (adder->pending_events);
958 adder->pending_events = NULL;
961 G_OBJECT_CLASS (parent_class)->dispose (object);
965 gst_adder_set_property (GObject * object, guint prop_id,
966 const GValue * value, GParamSpec * pspec)
968 GstAdder *adder = GST_ADDER (object);
971 case PROP_FILTER_CAPS:{
972 GstCaps *new_caps = NULL;
974 const GstCaps *new_caps_val = gst_value_get_caps (value);
976 if (new_caps_val != NULL) {
977 new_caps = (GstCaps *) new_caps_val;
978 gst_caps_ref (new_caps);
981 GST_OBJECT_LOCK (adder);
982 old_caps = adder->filter_caps;
983 adder->filter_caps = new_caps;
984 GST_OBJECT_UNLOCK (adder);
987 gst_caps_unref (old_caps);
989 GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
993 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
999 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
1002 GstAdder *adder = GST_ADDER (object);
1005 case PROP_FILTER_CAPS:
1006 GST_OBJECT_LOCK (adder);
1007 gst_value_set_caps (value, adder->filter_caps);
1008 GST_OBJECT_UNLOCK (adder);
1011 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1018 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
1019 const gchar * unused, const GstCaps * caps)
1026 if (templ->direction != GST_PAD_SINK)
1029 adder = GST_ADDER (element);
1031 /* increment pad counter */
1032 padcount = g_atomic_int_add (&adder->padcount, 1);
1034 name = g_strdup_printf ("sink_%u", padcount);
1035 newpad = gst_pad_new_from_template (templ, name);
1036 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
1039 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData),
1042 /* takes ownership of the pad */
1043 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
1051 g_warning ("gstadder: request new pad that is not a SINK pad\n");
1056 GST_DEBUG_OBJECT (adder, "could not add pad");
1057 gst_collect_pads_remove_pad (adder->collect, newpad);
1058 gst_object_unref (newpad);
1064 gst_adder_release_pad (GstElement * element, GstPad * pad)
1068 adder = GST_ADDER (element);
1070 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1073 gst_collect_pads_remove_pad (adder->collect, pad);
1074 gst_element_remove_pad (element, pad);
1077 static GstFlowReturn
1078 gst_adder_do_clip (GstCollectPads * pads, GstCollectData * data,
1079 GstBuffer * buffer, GstBuffer ** out, gpointer user_data)
1081 GstAdder *adder = GST_ADDER (user_data);
1084 rate = GST_AUDIO_INFO_RATE (&adder->info);
1085 bpf = GST_AUDIO_INFO_BPF (&adder->info);
1087 buffer = gst_audio_buffer_clip (buffer, &data->segment, rate, bpf);
1093 static GstFlowReturn
1094 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
1097 * combine streams by adding data values
1099 * - this function is called when all pads have a buffer
1100 * - get available bytes on all pads.
1101 * - repeat for each input pad :
1102 * - read available bytes, copy or add to target buffer
1103 * - if there's an EOS event, remove the input channel
1104 * - push out the output buffer
1107 * - would be nice to have a mixing mode, where instead of adding we mix
1108 * - for float we could downscale after collect loop
1109 * - for int we need to downscale each input to avoid clipping or
1110 * mix into a temp (float) buffer and scale afterwards as well
1113 GSList *collected, *next = NULL;
1115 GstBuffer *outbuf = NULL, *gapbuf = NULL;
1116 GstMapInfo outmap = { NULL };
1119 gint64 next_timestamp;
1120 gint rate, bps, bpf;
1122 adder = GST_ADDER (user_data);
1125 if (G_UNLIKELY (adder->func == NULL))
1126 goto not_negotiated;
1128 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1130 GST_INFO_OBJECT (adder->srcpad, "send pending flush stop event");
1131 if (!gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE))) {
1132 GST_WARNING_OBJECT (adder->srcpad, "Sending flush stop event failed");
1136 if (adder->send_stream_start) {
1139 GST_INFO_OBJECT (adder->srcpad, "send pending stream start event");
1140 /* stream-start (FIXME: create id based on input ids) */
1141 g_snprintf (s_id, sizeof (s_id), "adder-%08x", g_random_int ());
1142 if (!gst_pad_push_event (adder->srcpad, gst_event_new_stream_start (s_id))) {
1143 GST_WARNING_OBJECT (adder->srcpad, "Sending stream start event failed");
1145 adder->send_stream_start = FALSE;
1148 if (adder->send_caps) {
1149 GstEvent *caps_event;
1151 caps_event = gst_event_new_caps (adder->current_caps);
1152 GST_INFO_OBJECT (adder->srcpad, "send pending caps event %" GST_PTR_FORMAT,
1154 if (!gst_pad_push_event (adder->srcpad, caps_event)) {
1155 GST_WARNING_OBJECT (adder->srcpad, "Sending caps event failed");
1157 adder->send_caps = FALSE;
1160 /* get available bytes for reading, this can be 0 which could mean empty
1161 * buffers or EOS, which we will catch when we loop over the pads. */
1162 outsize = gst_collect_pads_available (pads);
1163 /* can only happen when no pads to collect or all EOS */
1167 rate = GST_AUDIO_INFO_RATE (&adder->info);
1168 bps = GST_AUDIO_INFO_BPS (&adder->info);
1169 bpf = GST_AUDIO_INFO_BPF (&adder->info);
1171 GST_LOG_OBJECT (adder,
1172 "starting to cycle through channels, %d bytes available (bps = %d, bpf = %d)",
1175 for (collected = pads->data; collected; collected = next) {
1176 GstCollectData *collect_data;
1180 /* take next to see if this is the last collectdata */
1181 next = g_slist_next (collected);
1183 collect_data = (GstCollectData *) collected->data;
1185 /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1187 inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
1188 /* NULL means EOS or an empty buffer so we still need to flush in
1189 * case of an empty buffer. */
1190 if (inbuf == NULL) {
1191 GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1195 is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1197 /* Try to make an output buffer */
1198 if (outbuf == NULL) {
1199 /* if this is a gap buffer but we have some more pads to check, skip it.
1200 * If we are at the last buffer, take it, regardless if it is a GAP
1202 if (is_gap && next) {
1203 GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1204 /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1205 * EOS, we can use this one as the output buffer. */
1209 gst_buffer_unref (inbuf);
1213 GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1214 collect_data, outsize);
1216 /* make data and metadata writable, can simply return the inbuf when we
1217 * are the only one referencing this buffer. If this is the last (and
1218 * only) GAP buffer, it will automatically copy the GAP flag. */
1219 outbuf = gst_buffer_make_writable (inbuf);
1220 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1223 /* we had a previous output buffer, mix this non-GAP buffer */
1226 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
1228 /* all buffers should have outsize, there are no short buffers because we
1229 * asked for the max size above */
1230 g_assert (inmap.size == outmap.size);
1232 GST_LOG_OBJECT (adder, "channel %p: mixing %" G_GSIZE_FORMAT " bytes"
1233 " from data %p", collect_data, inmap.size, inmap.data);
1235 /* further buffers, need to add them */
1236 adder->func ((gpointer) outmap.data, (gpointer) inmap.data,
1238 gst_buffer_unmap (inbuf, &inmap);
1240 /* skip gap buffer */
1241 GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1243 gst_buffer_unref (inbuf);
1247 gst_buffer_unmap (outbuf, &outmap);
1249 if (outbuf == NULL) {
1250 /* no output buffer, reuse one of the GAP buffers then if we have one */
1252 GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1255 /* assume EOS otherwise, this should not happen, really */
1258 /* we had an output buffer, unref the gapbuffer we kept */
1259 gst_buffer_unref (gapbuf);
1261 if (g_atomic_int_compare_and_exchange (&adder->new_segment_pending, TRUE,
1265 /* FIXME, use rate/applied_rate as set on all sinkpads.
1266 * - currently we just set rate as received from last seek-event
1268 * When seeking we set the start and stop positions as given in the seek
1269 * event. We also adjust offset & timestamp accordingly.
1270 * This basically ignores all newsegments sent by upstream.
1272 event = gst_event_new_segment (&adder->segment);
1273 if (adder->segment.rate > 0.0) {
1274 adder->segment.position = adder->segment.start;
1276 adder->segment.position = adder->segment.stop;
1278 adder->offset = gst_util_uint64_scale (adder->segment.position,
1281 GST_INFO_OBJECT (adder->srcpad, "sending pending new segment event %"
1282 GST_SEGMENT_FORMAT, &adder->segment);
1284 if (!gst_pad_push_event (adder->srcpad, event)) {
1285 GST_WARNING_OBJECT (adder->srcpad, "Sending new segment event failed");
1288 GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1289 "start:%" G_GINT64_FORMAT " end:%" G_GINT64_FORMAT " failed",
1290 adder->segment.start, adder->segment.stop);
1294 if (G_UNLIKELY (adder->pending_events)) {
1295 GList *tmp = adder->pending_events;
1298 GstEvent *ev = (GstEvent *) tmp->data;
1300 gst_pad_push_event (adder->srcpad, ev);
1301 tmp = g_list_next (tmp);
1303 g_list_free (adder->pending_events);
1304 adder->pending_events = NULL;
1307 /* for the next timestamp, use the sample counter, which will
1308 * never accumulate rounding errors */
1309 if (adder->segment.rate > 0.0) {
1310 next_offset = adder->offset + outsize / bpf;
1312 next_offset = adder->offset - outsize / bpf;
1314 next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, rate);
1317 /* set timestamps on the output buffer */
1318 if (adder->segment.rate > 0.0) {
1319 GST_BUFFER_TIMESTAMP (outbuf) = adder->segment.position;
1320 GST_BUFFER_OFFSET (outbuf) = adder->offset;
1321 GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1322 GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->segment.position;
1324 GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1325 GST_BUFFER_OFFSET (outbuf) = next_offset;
1326 GST_BUFFER_OFFSET_END (outbuf) = adder->offset;
1327 GST_BUFFER_DURATION (outbuf) = adder->segment.position - next_timestamp;
1330 adder->offset = next_offset;
1331 adder->segment.position = next_timestamp;
1334 GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1335 " offset %" G_GINT64_FORMAT, outbuf,
1336 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1337 GST_BUFFER_OFFSET (outbuf));
1338 ret = gst_pad_push (adder->srcpad, outbuf);
1340 GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1347 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1348 ("Unknown data received, not negotiated"));
1349 return GST_FLOW_NOT_NEGOTIATED;
1353 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1354 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1355 return GST_FLOW_EOS;
1359 static GstStateChangeReturn
1360 gst_adder_change_state (GstElement * element, GstStateChange transition)
1363 GstStateChangeReturn ret;
1365 adder = GST_ADDER (element);
1367 switch (transition) {
1368 case GST_STATE_CHANGE_NULL_TO_READY:
1370 case GST_STATE_CHANGE_READY_TO_PAUSED:
1372 adder->flush_stop_pending = FALSE;
1373 adder->new_segment_pending = TRUE;
1374 adder->wait_for_new_segment = FALSE;
1375 adder->send_stream_start = TRUE;
1376 adder->send_caps = TRUE;
1377 gst_caps_replace (&adder->current_caps, NULL);
1378 gst_segment_init (&adder->segment, GST_FORMAT_TIME);
1379 gst_collect_pads_start (adder->collect);
1381 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1383 case GST_STATE_CHANGE_PAUSED_TO_READY:
1384 /* need to unblock the collectpads before calling the
1385 * parent change_state so that streaming can finish */
1386 gst_collect_pads_stop (adder->collect);
1392 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1394 switch (transition) {
1403 plugin_init (GstPlugin * plugin)
1405 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
1406 "audio channel mixing element");
1408 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1415 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1418 "Adds multiple streams",
1419 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)