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 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
48 * with newer GLib versions (>= 2.31.0) */
49 #define GLIB_DISABLE_DEPRECATION_WARNINGS
52 #include <gst/audio/audio.h>
53 #include <string.h> /* strcmp */
54 #include "gstadderorc.h"
56 /* highest positive/lowest negative x-bit value we can use for clamping */
57 #define MAX_INT_32 ((gint32) (0x7fffffff))
58 #define MAX_INT_16 ((gint16) (0x7fff))
59 #define MAX_INT_8 ((gint8) (0x7f))
60 #define MAX_UINT_32 ((guint32)(0xffffffff))
61 #define MAX_UINT_16 ((guint16)(0xffff))
62 #define MAX_UINT_8 ((guint8) (0xff))
64 #define MIN_INT_32 ((gint32) (0x80000000))
65 #define MIN_INT_16 ((gint16) (0x8000))
66 #define MIN_INT_8 ((gint8) (0x80))
67 #define MIN_UINT_32 ((guint32)(0x00000000))
68 #define MIN_UINT_16 ((guint16)(0x0000))
69 #define MIN_UINT_8 ((guint8) (0x00))
77 #define GST_CAT_DEFAULT gst_adder_debug
78 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
80 /* elementfactory information */
82 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
84 GST_AUDIO_CAPS_MAKE ("{ S32LE, U32LE, S16LE, U16LE, S8, U8, F32LE, F64LE }") \
85 ", layout = (string) { interleaved, non-interleaved }"
88 GST_AUDIO_CAPS_MAKE ("{ S32BE, U32BE, S16BE, U16BE, S8, U8, F32BE, F64BE }") \
89 ", layout = (string) { interleaved, non-interleaved }"
92 static GstStaticPadTemplate gst_adder_src_template =
93 GST_STATIC_PAD_TEMPLATE ("src",
96 GST_STATIC_CAPS (CAPS)
99 static GstStaticPadTemplate gst_adder_sink_template =
100 GST_STATIC_PAD_TEMPLATE ("sink_%u",
103 GST_STATIC_CAPS (CAPS)
106 #define gst_adder_parent_class parent_class
107 G_DEFINE_TYPE (GstAdder, gst_adder, GST_TYPE_ELEMENT);
109 static void gst_adder_dispose (GObject * object);
110 static void gst_adder_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void gst_adder_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static gboolean gst_adder_setcaps (GstAdder * adder, GstPad * pad,
117 static gboolean gst_adder_src_query (GstPad * pad, GstObject * parent,
119 static gboolean gst_adder_sink_query (GstPad * pad, GstObject * parent,
121 static gboolean gst_adder_src_event (GstPad * pad, GstObject * parent,
123 static gboolean gst_adder_sink_event (GstPad * pad, GstObject * parent,
126 static GstPad *gst_adder_request_new_pad (GstElement * element,
127 GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
128 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
130 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
131 GstStateChange transition);
133 static GstFlowReturn gst_adder_do_clip (GstCollectPads2 * pads,
134 GstCollectData2 * data, GstBuffer * buffer, GstBuffer ** out,
136 static GstFlowReturn gst_adder_collected (GstCollectPads2 * pads,
138 static gboolean gst_adder_event (GstCollectPads2 * pads, GstCollectData2 * pad,
139 GstEvent * event, gpointer user_data);
141 /* non-clipping versions (for float) */
142 #define MAKE_FUNC_NC(name,type) \
143 static void name (type *out, type *in, gint samples) { \
145 for (i = 0; i < samples; i++) \
150 MAKE_FUNC_NC (add_float64, gdouble)
153 /* we can only accept caps that we and downstream can handle.
154 * if we have filtercaps set, use those to constrain the target caps.
157 gst_adder_sink_getcaps (GstPad * pad, GstCaps * filter)
160 GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
162 adder = GST_ADDER (GST_PAD_PARENT (pad));
164 GST_OBJECT_LOCK (adder);
166 if ((filter_caps = adder->filter_caps)) {
169 gst_caps_intersect_full (filter, filter_caps,
170 GST_CAPS_INTERSECT_FIRST);
172 gst_caps_ref (filter_caps);
174 filter_caps = filter ? gst_caps_ref (filter) : NULL;
176 GST_OBJECT_UNLOCK (adder);
178 if (filter_caps && gst_caps_is_empty (filter_caps)) {
179 GST_WARNING_OBJECT (pad, "Empty filter caps");
183 /* get the downstream possible caps */
184 peercaps = gst_pad_peer_query_caps (adder->srcpad, filter_caps);
186 /* get the allowed caps on this sinkpad */
187 sinkcaps = gst_pad_get_current_caps (pad);
188 if (sinkcaps == NULL) {
189 sinkcaps = gst_pad_get_pad_template_caps (pad);
191 sinkcaps = gst_caps_new_any ();
195 /* if the peer has caps, intersect */
196 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
198 gst_caps_intersect_full (peercaps, sinkcaps, GST_CAPS_INTERSECT_FIRST);
199 gst_caps_unref (peercaps);
200 gst_caps_unref (sinkcaps);
202 /* the peer has no caps (or there is no peer), just use the allowed caps
203 * of this sinkpad. */
204 /* restrict with filter-caps if any */
206 GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
208 gst_caps_intersect_full (filter_caps, sinkcaps,
209 GST_CAPS_INTERSECT_FIRST);
210 gst_caps_unref (sinkcaps);
212 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
218 gst_caps_unref (filter_caps);
220 GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
221 GST_PAD_NAME (pad), result);
227 gst_adder_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
229 gboolean res = FALSE;
231 switch (GST_QUERY_TYPE (query)) {
234 GstCaps *filter, *caps;
236 gst_query_parse_caps (query, &filter);
237 caps = gst_adder_sink_getcaps (pad, filter);
238 gst_query_set_caps_result (query, caps);
239 gst_caps_unref (caps);
244 res = gst_pad_query_default (pad, parent, query);
257 setcapsfunc (const GValue * item, IterData * data)
259 GstPad *otherpad = g_value_get_object (item);
261 if (otherpad != data->pad)
262 gst_pad_set_caps (data->pad, data->caps);
265 /* the first caps we receive on any of the sinkpads will define the caps for all
266 * the other sinkpads because we can only mix streams with the same caps.
269 gst_adder_setcaps (GstAdder * adder, GstPad * pad, GstCaps * caps)
272 GstIteratorResult ires;
276 /* this get called recursively due to gst_iterator_foreach calling
277 * gst_pad_set_caps() */
278 if (adder->in_setcaps)
281 GST_LOG_OBJECT (adder, "setting caps pad %p,%s to %" GST_PTR_FORMAT, pad,
282 GST_PAD_NAME (pad), caps);
284 it = gst_element_iterate_pads (GST_ELEMENT_CAST (adder));
286 /* FIXME, see if the other pads can accept the format. Also lock the
287 * format on the other pads to this new format. */
291 adder->in_setcaps = TRUE;
294 ires = gst_iterator_foreach (it, (GstIteratorForeachFunction) setcapsfunc,
298 case GST_ITERATOR_RESYNC:
299 gst_iterator_resync (it);
306 adder->in_setcaps = FALSE;
308 GST_LOG_OBJECT (adder, "handle caps changes on pad %p,%s to %" GST_PTR_FORMAT,
309 pad, GST_PAD_NAME (pad), caps);
311 if (!gst_audio_info_from_caps (&adder->info, caps))
314 switch (GST_AUDIO_INFO_FORMAT (&adder->info)) {
315 case GST_AUDIO_FORMAT_S8:
316 adder->func = (GstAdderFunction) add_int8;
318 case GST_AUDIO_FORMAT_U8:
319 adder->func = (GstAdderFunction) add_uint8;
321 case GST_AUDIO_FORMAT_S16:
322 adder->func = (GstAdderFunction) add_int16;
324 case GST_AUDIO_FORMAT_U16:
325 adder->func = (GstAdderFunction) add_uint16;
327 case GST_AUDIO_FORMAT_S32:
328 adder->func = (GstAdderFunction) add_int32;
330 case GST_AUDIO_FORMAT_U32:
331 adder->func = (GstAdderFunction) add_uint32;
333 case GST_AUDIO_FORMAT_F32:
334 adder->func = (GstAdderFunction) add_float32;
336 case GST_AUDIO_FORMAT_F64:
337 adder->func = (GstAdderFunction) add_float64;
347 GST_DEBUG_OBJECT (adder, "invalid format set as caps");
352 /* FIXME, the duration query should reflect how long you will produce
353 * data, that is the amount of stream time until you will emit EOS.
355 * For synchronized mixing this is always the max of all the durations
356 * of upstream since we emit EOS when all of them finished.
358 * We don't do synchronized mixing so this really depends on where the
359 * streams where punched in and what their relative offsets are against
360 * eachother which we can get from the first timestamps we see.
362 * When we add a new stream (or remove a stream) the duration might
363 * also become invalid again and we need to post a new DURATION
364 * message to notify this fact to the parent.
365 * For now we take the max of all the upstream elements so the simple
366 * cases work at least somewhat.
369 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
376 GValue item = { 0, };
379 gst_query_parse_duration (query, &format, NULL);
385 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
387 GstIteratorResult ires;
389 ires = gst_iterator_next (it, &item);
391 case GST_ITERATOR_DONE:
394 case GST_ITERATOR_OK:
396 GstPad *pad = g_value_get_object (&item);
399 /* ask sink peer for duration */
400 res &= gst_pad_peer_query_duration (pad, format, &duration);
401 /* take max from all valid return values */
403 /* valid unknown length, stop searching */
404 if (duration == -1) {
408 /* else see if bigger than current max */
409 else if (duration > max)
412 g_value_reset (&item);
415 case GST_ITERATOR_RESYNC:
418 gst_iterator_resync (it);
426 g_value_unset (&item);
427 gst_iterator_free (it);
430 /* and store the max */
431 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
432 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
433 gst_query_set_duration (query, format, max);
440 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
442 GstClockTime min, max;
447 GValue item = { 0, };
454 max = GST_CLOCK_TIME_NONE;
456 /* Take maximum of all latency values */
457 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
459 GstIteratorResult ires;
461 ires = gst_iterator_next (it, &item);
463 case GST_ITERATOR_DONE:
466 case GST_ITERATOR_OK:
468 GstPad *pad = g_value_get_object (&item);
470 GstClockTime min_cur, max_cur;
473 peerquery = gst_query_new_latency ();
475 /* Ask peer for latency */
476 res &= gst_pad_peer_query (pad, peerquery);
478 /* take max from all valid return values */
480 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
485 if (max_cur != GST_CLOCK_TIME_NONE &&
486 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
487 (max == GST_CLOCK_TIME_NONE)))
490 live = live || live_cur;
493 gst_query_unref (peerquery);
494 g_value_reset (&item);
497 case GST_ITERATOR_RESYNC:
500 max = GST_CLOCK_TIME_NONE;
502 gst_iterator_resync (it);
510 g_value_unset (&item);
511 gst_iterator_free (it);
514 /* store the results */
515 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
516 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
517 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
518 gst_query_set_latency (query, live, min, max);
525 gst_adder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
527 GstAdder *adder = GST_ADDER (parent);
528 gboolean res = FALSE;
530 switch (GST_QUERY_TYPE (query)) {
531 case GST_QUERY_POSITION:
535 gst_query_parse_position (query, &format, NULL);
538 case GST_FORMAT_TIME:
539 /* FIXME, bring to stream time, might be tricky */
540 gst_query_set_position (query, format, adder->segment.position);
543 case GST_FORMAT_DEFAULT:
544 gst_query_set_position (query, format, adder->offset);
552 case GST_QUERY_DURATION:
553 res = gst_adder_query_duration (adder, query);
555 case GST_QUERY_LATENCY:
556 res = gst_adder_query_latency (adder, query);
559 /* FIXME, needs a custom query handler because we have multiple
561 res = gst_pad_query_default (pad, parent, query);
575 forward_event_func (const GValue * val, GValue * ret, EventData * data)
577 GstPad *pad = g_value_get_object (val);
578 GstEvent *event = data->event;
580 gst_event_ref (event);
581 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
582 if (!gst_pad_push_event (pad, event)) {
583 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
584 event, GST_EVENT_TYPE_NAME (event));
585 /* quick hack to unflush the pads, ideally we need a way to just unflush
586 * this single collect pad */
588 gst_pad_send_event (pad, gst_event_new_flush_stop (TRUE));
590 g_value_set_boolean (ret, TRUE);
591 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
592 event, GST_EVENT_TYPE_NAME (event));
595 /* continue on other pads, even if one failed */
599 /* forwards the event to all sinkpads, takes ownership of the
602 * Returns: TRUE if the event could be forwarded on all
606 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
610 GstIteratorResult ires;
614 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
615 GST_EVENT_TYPE_NAME (event));
620 g_value_init (&vret, G_TYPE_BOOLEAN);
621 g_value_set_boolean (&vret, FALSE);
622 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
625 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
628 case GST_ITERATOR_RESYNC:
629 GST_WARNING ("resync");
630 gst_iterator_resync (it);
631 g_value_set_boolean (&vret, TRUE);
633 case GST_ITERATOR_OK:
634 case GST_ITERATOR_DONE:
635 ret = g_value_get_boolean (&vret);
643 gst_iterator_free (it);
644 GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
645 GST_EVENT_TYPE_NAME (event), ret);
646 gst_event_unref (event);
652 gst_adder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
657 adder = GST_ADDER (parent);
659 GST_DEBUG_OBJECT (pad, "Got %s event on src pad",
660 GST_EVENT_TYPE_NAME (event));
662 switch (GST_EVENT_TYPE (event)) {
667 GstSeekType curtype, endtype;
671 /* parse the seek parameters */
672 gst_event_parse_seek (event, &rate, NULL, &flags, &curtype,
673 &cur, &endtype, &end);
675 if ((curtype != GST_SEEK_TYPE_NONE) && (curtype != GST_SEEK_TYPE_SET)) {
677 GST_DEBUG_OBJECT (adder,
678 "seeking failed, unhandled seek type for start: %d", curtype);
681 if ((endtype != GST_SEEK_TYPE_NONE) && (endtype != GST_SEEK_TYPE_SET)) {
683 GST_DEBUG_OBJECT (adder,
684 "seeking failed, unhandled seek type for end: %d", endtype);
688 flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
690 /* check if we are flushing */
692 /* flushing seek, start flush downstream, the flush will be done
693 * when all pads received a FLUSH_STOP.
694 * Make sure we accept nothing anymore and return WRONG_STATE.
695 * We send a flush-start before, to ensure no streaming is done
696 * as we need to take the stream lock.
698 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
699 gst_collect_pads2_set_flushing (adder->collect, TRUE);
701 /* We can't send FLUSH_STOP here since upstream could start pushing data
702 * after we unlock adder->collect.
703 * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
704 * forwarding the seek upstream or from gst_adder_collected,
705 * whichever happens first.
707 g_atomic_int_set (&adder->flush_stop_pending, TRUE);
709 GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
711 /* now wait for the collected to be finished and mark a new
712 * segment. After we have the lock, no collect function is running and no
713 * new collect function will be called for as long as we're flushing. */
714 GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
715 adder->segment.rate = rate;
716 if (curtype == GST_SEEK_TYPE_SET)
717 adder->segment.start = cur;
719 adder->segment.start = 0;
720 if (endtype == GST_SEEK_TYPE_SET)
721 adder->segment.stop = end;
723 adder->segment.stop = GST_CLOCK_TIME_NONE;
725 /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
726 * have stopped so that the cookie gets properly updated. */
727 gst_collect_pads2_set_flushing (adder->collect, TRUE);
729 GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
730 GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
733 /* we're forwarding seek to all upstream peers and wait for one to reply
734 * with a newsegment-event before we send a newsegment-event downstream */
735 g_atomic_int_set (&adder->wait_for_new_segment, TRUE);
736 result = forward_event (adder, event, flush);
738 /* seek failed. maybe source is a live source. */
739 GST_DEBUG_OBJECT (adder, "seeking failed");
741 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
743 GST_DEBUG_OBJECT (adder, "pending flush stop");
744 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
749 /* QoS might be tricky */
752 case GST_EVENT_NAVIGATION:
753 /* navigation is rather pointless. */
757 /* just forward the rest for now */
758 GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
759 GST_EVENT_TYPE_NAME (event));
760 result = forward_event (adder, event, FALSE);
770 gst_adder_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
775 adder = GST_ADDER (parent);
777 GST_DEBUG_OBJECT (pad, "Got %s event on sink pad",
778 GST_EVENT_TYPE_NAME (event));
780 switch (GST_EVENT_TYPE (event)) {
785 gst_event_parse_caps (event, &caps);
786 ret = gst_adder_setcaps (adder, pad, caps);
787 gst_event_unref (event);
791 case GST_EVENT_FLUSH_STOP:
792 /* we received a flush-stop. The collect_event function will call the
793 * gst_adder_event function we have set on the GstCollectPads2, so we
794 * have control over whether the event is sent past our element.
795 * We will only forward it when flush_stop_pending is set, and we will
798 GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
799 g_atomic_int_set (&adder->new_segment_pending, TRUE);
800 /* Clear pending tags */
801 if (adder->pending_events) {
802 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
803 g_list_free (adder->pending_events);
804 adder->pending_events = NULL;
806 GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
809 GST_COLLECT_PADS2_STREAM_LOCK (adder->collect);
810 /* collect tags here so we can push them out when we collect data */
811 adder->pending_events = g_list_append (adder->pending_events, event);
812 GST_COLLECT_PADS2_STREAM_UNLOCK (adder->collect);
814 case GST_EVENT_SEGMENT:
815 if (g_atomic_int_compare_and_exchange (&adder->wait_for_new_segment,
817 /* make sure we push a new segment, to inform about new basetime
818 * see FIXME in gst_adder_collected() */
819 g_atomic_int_set (&adder->new_segment_pending, TRUE);
826 /* now GstCollectPads2 can take care of the rest, e.g. EOS */
827 ret = adder->collect_event (pad, parent, event);
834 gst_adder_class_init (GstAdderClass * klass)
836 GObjectClass *gobject_class = (GObjectClass *) klass;
837 GstElementClass *gstelement_class = (GstElementClass *) klass;
839 gobject_class->set_property = gst_adder_set_property;
840 gobject_class->get_property = gst_adder_get_property;
841 gobject_class->dispose = gst_adder_dispose;
848 g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
849 g_param_spec_boxed ("caps", "Target caps",
850 "Set target format for mixing (NULL means ANY). "
851 "Setting this property takes a reference to the supplied GstCaps "
852 "object.", GST_TYPE_CAPS,
853 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
855 gst_element_class_add_pad_template (gstelement_class,
856 gst_static_pad_template_get (&gst_adder_src_template));
857 gst_element_class_add_pad_template (gstelement_class,
858 gst_static_pad_template_get (&gst_adder_sink_template));
859 gst_element_class_set_details_simple (gstelement_class, "Adder",
861 "Add N audio channels together",
862 "Thomas Vander Stichele <thomas at apestaart dot org>");
864 gstelement_class->request_new_pad =
865 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
866 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
867 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
871 gst_adder_init (GstAdder * adder)
873 GstPadTemplate *template;
875 template = gst_static_pad_template_get (&gst_adder_src_template);
876 adder->srcpad = gst_pad_new_from_template (template, "src");
877 gst_object_unref (template);
879 gst_pad_set_query_function (adder->srcpad,
880 GST_DEBUG_FUNCPTR (gst_adder_src_query));
881 gst_pad_set_event_function (adder->srcpad,
882 GST_DEBUG_FUNCPTR (gst_adder_src_event));
883 GST_PAD_SET_PROXY_CAPS (adder->srcpad);
884 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
886 gst_audio_info_init (&adder->info);
890 adder->filter_caps = NULL;
892 /* keep track of the sinkpads requested */
893 adder->collect = gst_collect_pads2_new ();
894 gst_collect_pads2_set_function (adder->collect,
895 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
896 gst_collect_pads2_set_clip_function (adder->collect,
897 GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
898 gst_collect_pads2_set_event_function (adder->collect,
899 GST_DEBUG_FUNCPTR (gst_adder_event), adder);
903 gst_adder_dispose (GObject * object)
905 GstAdder *adder = GST_ADDER (object);
907 if (adder->collect) {
908 gst_object_unref (adder->collect);
909 adder->collect = NULL;
911 gst_caps_replace (&adder->filter_caps, NULL);
912 if (adder->pending_events) {
913 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
914 g_list_free (adder->pending_events);
915 adder->pending_events = NULL;
918 G_OBJECT_CLASS (parent_class)->dispose (object);
922 gst_adder_set_property (GObject * object, guint prop_id,
923 const GValue * value, GParamSpec * pspec)
925 GstAdder *adder = GST_ADDER (object);
928 case PROP_FILTER_CAPS:{
929 GstCaps *new_caps = NULL;
931 const GstCaps *new_caps_val = gst_value_get_caps (value);
933 if (new_caps_val != NULL) {
934 new_caps = (GstCaps *) new_caps_val;
935 gst_caps_ref (new_caps);
938 GST_OBJECT_LOCK (adder);
939 old_caps = adder->filter_caps;
940 adder->filter_caps = new_caps;
941 GST_OBJECT_UNLOCK (adder);
944 gst_caps_unref (old_caps);
946 GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
956 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
959 GstAdder *adder = GST_ADDER (object);
962 case PROP_FILTER_CAPS:
963 GST_OBJECT_LOCK (adder);
964 gst_value_set_caps (value, adder->filter_caps);
965 GST_OBJECT_UNLOCK (adder);
968 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
975 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
976 const gchar * unused, const GstCaps * caps)
983 if (templ->direction != GST_PAD_SINK)
986 adder = GST_ADDER (element);
988 /* increment pad counter */
989 padcount = g_atomic_int_add (&adder->padcount, 1);
991 name = g_strdup_printf ("sink_%u", padcount);
992 newpad = gst_pad_new_from_template (templ, name);
993 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
996 gst_pad_set_query_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_query));
997 gst_collect_pads2_add_pad (adder->collect, newpad, sizeof (GstCollectData2));
999 /* FIXME: hacked way to override/extend the event function of
1000 * GstCollectPads2; because it sets its own event function giving the
1001 * element no access to events */
1002 adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
1003 gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
1005 /* takes ownership of the pad */
1006 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
1014 g_warning ("gstadder: request new pad that is not a SINK pad\n");
1019 GST_DEBUG_OBJECT (adder, "could not add pad");
1020 gst_collect_pads2_remove_pad (adder->collect, newpad);
1021 gst_object_unref (newpad);
1027 gst_adder_release_pad (GstElement * element, GstPad * pad)
1031 adder = GST_ADDER (element);
1033 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1035 gst_collect_pads2_remove_pad (adder->collect, pad);
1036 gst_element_remove_pad (element, pad);
1039 static GstFlowReturn
1040 gst_adder_do_clip (GstCollectPads2 * pads, GstCollectData2 * data,
1041 GstBuffer * buffer, GstBuffer ** out, gpointer user_data)
1043 GstAdder *adder = GST_ADDER (user_data);
1046 rate = GST_AUDIO_INFO_RATE (&adder->info);
1047 bpf = GST_AUDIO_INFO_BPF (&adder->info);
1049 buffer = gst_audio_buffer_clip (buffer, &data->segment, rate, bpf);
1055 static GstFlowReturn
1056 gst_adder_collected (GstCollectPads2 * pads, gpointer user_data)
1059 * combine streams by adding data values
1061 * - this function is called when all pads have a buffer
1062 * - get available bytes on all pads.
1063 * - repeat for each input pad :
1064 * - read available bytes, copy or add to target buffer
1065 * - if there's an EOS event, remove the input channel
1066 * - push out the output buffer
1069 * - would be nice to have a mixing mode, where instead of adding we mix
1070 * - for float we could downscale after collect loop
1071 * - for int we need to downscale each input to avoid clipping or
1072 * mix into a temp (float) buffer and scale afterwards as well
1075 GSList *collected, *next = NULL;
1077 GstBuffer *outbuf = NULL, *gapbuf = NULL;
1078 GstMapInfo outmap = { NULL };
1081 gint64 next_timestamp;
1082 gint rate, bps, bpf;
1084 adder = GST_ADDER (user_data);
1087 if (G_UNLIKELY (adder->func == NULL))
1088 goto not_negotiated;
1090 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1092 GST_DEBUG_OBJECT (adder, "pending flush stop");
1093 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
1096 /* get available bytes for reading, this can be 0 which could mean empty
1097 * buffers or EOS, which we will catch when we loop over the pads. */
1098 outsize = gst_collect_pads2_available (pads);
1099 /* can only happen when no pads to collect or all EOS */
1103 rate = GST_AUDIO_INFO_RATE (&adder->info);
1104 bps = GST_AUDIO_INFO_BPS (&adder->info);
1105 bpf = GST_AUDIO_INFO_BPF (&adder->info);
1107 GST_LOG_OBJECT (adder,
1108 "starting to cycle through channels, %d bytes available (bps = %d, bpf = %d)",
1111 for (collected = pads->data; collected; collected = next) {
1112 GstCollectData2 *collect_data;
1116 /* take next to see if this is the last collectdata */
1117 next = g_slist_next (collected);
1119 collect_data = (GstCollectData2 *) collected->data;
1121 /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1123 inbuf = gst_collect_pads2_take_buffer (pads, collect_data, outsize);
1124 /* NULL means EOS or an empty buffer so we still need to flush in
1125 * case of an empty buffer. */
1126 if (inbuf == NULL) {
1127 GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1131 is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1133 /* Try to make an output buffer */
1134 if (outbuf == NULL) {
1135 /* if this is a gap buffer but we have some more pads to check, skip it.
1136 * If we are at the last buffer, take it, regardless if it is a GAP
1138 if (is_gap && next) {
1139 GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1140 /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1141 * EOS, we can use this one as the output buffer. */
1145 gst_buffer_unref (inbuf);
1149 GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1150 collect_data, outsize);
1152 /* make data and metadata writable, can simply return the inbuf when we
1153 * are the only one referencing this buffer. If this is the last (and
1154 * only) GAP buffer, it will automatically copy the GAP flag. */
1155 outbuf = gst_buffer_make_writable (inbuf);
1156 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
1159 /* we had a previous output buffer, mix this non-GAP buffer */
1162 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
1164 /* all buffers should have outsize, there are no short buffers because we
1165 * asked for the max size above */
1166 g_assert (inmap.size == outmap.size);
1168 GST_LOG_OBJECT (adder, "channel %p: mixing %" G_GSIZE_FORMAT " bytes"
1169 " from data %p", collect_data, inmap.size, inmap.data);
1171 /* further buffers, need to add them */
1172 adder->func ((gpointer) outmap.data, (gpointer) inmap.data,
1174 gst_buffer_unmap (inbuf, &inmap);
1176 /* skip gap buffer */
1177 GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1179 gst_buffer_unref (inbuf);
1183 gst_buffer_unmap (outbuf, &outmap);
1185 if (outbuf == NULL) {
1186 /* no output buffer, reuse one of the GAP buffers then if we have one */
1188 GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1191 /* assume EOS otherwise, this should not happen, really */
1194 /* we had an output buffer, unref the gapbuffer we kept */
1195 gst_buffer_unref (gapbuf);
1197 if (g_atomic_int_compare_and_exchange (&adder->new_segment_pending, TRUE,
1201 /* FIXME, use rate/applied_rate as set on all sinkpads.
1202 * - currently we just set rate as received from last seek-event
1204 * When seeking we set the start and stop positions as given in the seek
1205 * event. We also adjust offset & timestamp accordingly.
1206 * This basically ignores all newsegments sent by upstream.
1208 event = gst_event_new_segment (&adder->segment);
1210 if (adder->segment.rate > 0.0) {
1211 adder->segment.position = adder->segment.start;
1213 adder->segment.position = adder->segment.stop;
1215 adder->offset = gst_util_uint64_scale (adder->segment.position,
1217 GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1218 G_GUINT64_FORMAT, adder->segment.start, adder->segment.stop);
1219 GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1220 G_GINT64_FORMAT, adder->segment.position, adder->offset);
1223 if (!gst_pad_push_event (adder->srcpad, event)) {
1224 GST_WARNING_OBJECT (adder->srcpad, "Sending event failed");
1227 GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1228 "start:%" G_GINT64_FORMAT " end:%" G_GINT64_FORMAT " failed",
1229 adder->segment.start, adder->segment.stop);
1233 if (G_UNLIKELY (adder->pending_events)) {
1234 GList *tmp = adder->pending_events;
1237 GstEvent *ev = (GstEvent *) tmp->data;
1239 gst_pad_push_event (adder->srcpad, ev);
1240 tmp = g_list_next (tmp);
1242 g_list_free (adder->pending_events);
1243 adder->pending_events = NULL;
1246 /* for the next timestamp, use the sample counter, which will
1247 * never accumulate rounding errors */
1248 if (adder->segment.rate > 0.0) {
1249 next_offset = adder->offset + outsize / bpf;
1251 next_offset = adder->offset - outsize / bpf;
1253 next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, rate);
1256 /* set timestamps on the output buffer */
1257 if (adder->segment.rate > 0.0) {
1258 GST_BUFFER_TIMESTAMP (outbuf) = adder->segment.position;
1259 GST_BUFFER_OFFSET (outbuf) = adder->offset;
1260 GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1261 GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->segment.position;
1263 GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1264 GST_BUFFER_OFFSET (outbuf) = next_offset;
1265 GST_BUFFER_OFFSET_END (outbuf) = adder->offset;
1266 GST_BUFFER_DURATION (outbuf) = adder->segment.position - next_timestamp;
1269 adder->offset = next_offset;
1270 adder->segment.position = next_timestamp;
1273 GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1274 " offset %" G_GINT64_FORMAT, outbuf,
1275 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1276 GST_BUFFER_OFFSET (outbuf));
1277 ret = gst_pad_push (adder->srcpad, outbuf);
1279 GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1286 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1287 ("Unknown data received, not negotiated"));
1288 return GST_FLOW_NOT_NEGOTIATED;
1292 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1293 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1294 return GST_FLOW_EOS;
1299 gst_adder_event (GstCollectPads2 * pads, GstCollectData2 * pad,
1300 GstEvent * event, gpointer user_data)
1302 GstAdder *adder = GST_ADDER (user_data);
1303 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1304 if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1308 gst_event_unref (event);
1316 static GstStateChangeReturn
1317 gst_adder_change_state (GstElement * element, GstStateChange transition)
1320 GstStateChangeReturn ret;
1322 adder = GST_ADDER (element);
1324 switch (transition) {
1325 case GST_STATE_CHANGE_NULL_TO_READY:
1327 case GST_STATE_CHANGE_READY_TO_PAUSED:
1328 adder->segment.position = 0;
1330 adder->flush_stop_pending = FALSE;
1331 adder->new_segment_pending = TRUE;
1332 adder->wait_for_new_segment = FALSE;
1333 gst_segment_init (&adder->segment, GST_FORMAT_TIME);
1334 gst_collect_pads2_start (adder->collect);
1336 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1338 case GST_STATE_CHANGE_PAUSED_TO_READY:
1339 /* need to unblock the collectpads2 before calling the
1340 * parent change_state so that streaming can finish */
1341 gst_collect_pads2_stop (adder->collect);
1347 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1349 switch (transition) {
1359 plugin_init (GstPlugin * plugin)
1361 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
1362 "audio channel mixing element");
1364 gst_adder_orc_init ();
1366 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1373 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1376 "Adds multiple streams",
1377 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)