2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2001 Thomas <thomas@apestaart.org>
4 * 2005,2006 Wim Taymans <wim@fluendo.com>
6 * adder.c: Adder element, N in, one out, samples are added
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 * SECTION:element-adder
26 * The adder allows to mix several streams into one by adding the data.
27 * Mixed data is clamped to the min/max values of the data format.
29 * The adder currently mixes all data received on the sinkpads as soon as
30 * possible without trying to synchronize the streams.
33 * <title>Example launch line</title>
35 * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36 * ]| This pipeline produces two sine waves mixed together.
39 * Last reviewed on 2006-05-09 (0.10.7)
41 /* Element-Checklist-Version: 5 */
47 #include <gst/audio/audio.h>
48 #include <string.h> /* strcmp */
49 /*#include <liboil/liboil.h>*/
51 /* highest positive/lowest negative x-bit value we can use for clamping */
52 #define MAX_INT_32 ((gint32) (0x7fffffff))
53 #define MAX_INT_16 ((gint16) (0x7fff))
54 #define MAX_INT_8 ((gint8) (0x7f))
55 #define MAX_UINT_32 ((guint32)(0xffffffff))
56 #define MAX_UINT_16 ((guint16)(0xffff))
57 #define MAX_UINT_8 ((guint8) (0xff))
59 #define MIN_INT_32 ((gint32) (0x80000000))
60 #define MIN_INT_16 ((gint16) (0x8000))
61 #define MIN_INT_8 ((gint8) (0x80))
62 #define MIN_UINT_32 ((guint32)(0x00000000))
63 #define MIN_UINT_16 ((guint16)(0x0000))
64 #define MIN_UINT_8 ((guint8) (0x00))
72 #define GST_CAT_DEFAULT gst_adder_debug
73 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
75 /* elementfactory information */
79 "rate = (int) [ 1, MAX ], " \
80 "channels = (int) [ 1, MAX ], " \
81 "endianness = (int) BYTE_ORDER, " \
82 "width = (int) 32, " \
83 "depth = (int) 32, " \
84 "signed = (boolean) { true, false } ;" \
86 "rate = (int) [ 1, MAX ], " \
87 "channels = (int) [ 1, MAX ], " \
88 "endianness = (int) BYTE_ORDER, " \
89 "width = (int) 16, " \
90 "depth = (int) 16, " \
91 "signed = (boolean) { true, false } ;" \
93 "rate = (int) [ 1, MAX ], " \
94 "channels = (int) [ 1, MAX ], " \
95 "endianness = (int) BYTE_ORDER, " \
98 "signed = (boolean) { true, false } ;" \
99 "audio/x-raw-float, " \
100 "rate = (int) [ 1, MAX ], " \
101 "channels = (int) [ 1, MAX ], " \
102 "endianness = (int) BYTE_ORDER, " \
103 "width = (int) { 32, 64 }"
105 static GstStaticPadTemplate gst_adder_src_template =
106 GST_STATIC_PAD_TEMPLATE ("src",
109 GST_STATIC_CAPS (CAPS)
112 static GstStaticPadTemplate gst_adder_sink_template =
113 GST_STATIC_PAD_TEMPLATE ("sink%d",
116 GST_STATIC_CAPS (CAPS)
119 static void gst_adder_class_init (GstAdderClass * klass);
120 static void gst_adder_init (GstAdder * adder);
121 static void gst_adder_dispose (GObject * object);
122 static void gst_adder_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_adder_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
128 static gboolean gst_adder_query (GstPad * pad, GstQuery * query);
129 static gboolean gst_adder_src_event (GstPad * pad, GstEvent * event);
130 static gboolean gst_adder_sink_event (GstPad * pad, GstEvent * event);
132 static GstPad *gst_adder_request_new_pad (GstElement * element,
133 GstPadTemplate * temp, const gchar * unused);
134 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
136 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
137 GstStateChange transition);
139 static GstBuffer *gst_adder_do_clip (GstCollectPads * pads,
140 GstCollectData * data, GstBuffer * buffer, gpointer user_data);
141 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
144 static GstElementClass *parent_class = NULL;
147 gst_adder_get_type (void)
149 static GType adder_type = 0;
151 if (G_UNLIKELY (adder_type == 0)) {
152 static const GTypeInfo adder_info = {
153 sizeof (GstAdderClass), NULL, NULL,
154 (GClassInitFunc) gst_adder_class_init, NULL, NULL,
155 sizeof (GstAdder), 0,
156 (GInstanceInitFunc) gst_adder_init,
159 adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
161 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
162 "audio channel mixing element");
167 /* clipping versions (for int)
168 * FIXME: what about: oil_add_s16 (out, out, in, bytes / sizeof (type))
170 #define MAKE_FUNC(name,type,ttype,min,max) \
171 static void name (type *out, type *in, gint bytes) { \
174 for (i = 0; i < bytes / sizeof (type); i++) { \
175 add = (ttype)out[i] + (ttype)in[i]; \
176 out[i] = CLAMP (add, min, max); \
180 /* unsigned versions (for int) */
181 #define MAKE_FUNC_US(name,type,ttype,max) \
182 static void name (type *out, type *in, gint bytes) { \
185 for (i = 0; i < bytes / sizeof (type); i++) { \
186 add = (ttype)out[i] + (ttype)in[i]; \
187 out[i] = ((add <= max) ? add : max); \
191 /* non-clipping versions (for float) */
192 #define MAKE_FUNC_NC(name,type) \
193 static void name (type *out, type *in, gint bytes) { \
195 for (i = 0; i < bytes / sizeof (type); i++) \
200 /* right now, the liboil function don't seems to be faster on x86
201 * time gst-launch audiotestsrc num-buffers=50000 ! audio/x-raw-float ! adder name=m ! fakesink audiotestsrc num-buffers=50000 ! audio/x-raw-float ! m.
202 * time gst-launch audiotestsrc num-buffers=50000 ! audio/x-raw-float,width=32 ! adder name=m ! fakesink audiotestsrc num-buffers=50000 ! audio/x-raw-float,width=32 ! m.
205 add_float32 (gfloat * out, gfloat * in, gint bytes)
207 oil_add_f32 (out, out, in, bytes / sizeof (gfloat));
211 add_float64 (gdouble * out, gdouble * in, gint bytes)
213 oil_add_f64 (out, out, in, bytes / sizeof (gdouble));
218 MAKE_FUNC (add_int32, gint32, gint64, MIN_INT_32, MAX_INT_32)
219 MAKE_FUNC (add_int16, gint16, gint32, MIN_INT_16, MAX_INT_16)
220 MAKE_FUNC (add_int8, gint8, gint16, MIN_INT_8, MAX_INT_8)
221 MAKE_FUNC_US (add_uint32, guint32, guint64, MAX_UINT_32)
222 MAKE_FUNC_US (add_uint16, guint16, guint32, MAX_UINT_16)
223 MAKE_FUNC_US (add_uint8, guint8, guint16, MAX_UINT_8)
224 MAKE_FUNC_NC (add_float64, gdouble)
225 MAKE_FUNC_NC (add_float32, gfloat)
228 /* we can only accept caps that we and downstream can handle.
229 * if we have filtercaps set, use those to constrain the target caps.
232 gst_adder_sink_getcaps (GstPad * pad)
235 GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
237 adder = GST_ADDER (GST_PAD_PARENT (pad));
239 GST_OBJECT_LOCK (adder);
241 if ((filter_caps = adder->filter_caps))
242 gst_caps_ref (filter_caps);
243 GST_OBJECT_UNLOCK (adder);
245 /* get the downstream possible caps */
246 peercaps = gst_pad_peer_get_caps (adder->srcpad);
248 /* get the allowed caps on this sinkpad, we use the fixed caps function so
249 * that it does not call recursively in this function. */
250 sinkcaps = gst_pad_get_fixed_caps_func (pad);
252 /* restrict with filter-caps if any */
254 GST_DEBUG_OBJECT (adder, "filtering peer caps");
255 result = gst_caps_intersect (peercaps, filter_caps);
256 gst_caps_unref (peercaps);
259 /* if the peer has caps, intersect */
260 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
261 result = gst_caps_intersect (peercaps, sinkcaps);
262 gst_caps_unref (peercaps);
263 gst_caps_unref (sinkcaps);
265 /* the peer has no caps (or there is no peer), just use the allowed caps
266 * of this sinkpad. */
267 /* restrict with filter-caps if any */
269 GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
270 result = gst_caps_intersect (sinkcaps, filter_caps);
271 gst_caps_unref (sinkcaps);
273 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
279 gst_caps_unref (filter_caps);
281 GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
282 GST_PAD_NAME (pad), result);
287 /* the first caps we receive on any of the sinkpads will define the caps for all
288 * the other sinkpads because we can only mix streams with the same caps.
291 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
295 GstStructure *structure;
296 const char *media_type;
298 adder = GST_ADDER (GST_PAD_PARENT (pad));
300 GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
301 GST_PAD_NAME (pad), caps);
303 /* FIXME, see if the other pads can accept the format. Also lock the
304 * format on the other pads to this new format. */
305 GST_OBJECT_LOCK (adder);
306 pads = GST_ELEMENT (adder)->pads;
308 GstPad *otherpad = GST_PAD (pads->data);
310 if (otherpad != pad) {
311 gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
313 pads = g_list_next (pads);
315 GST_OBJECT_UNLOCK (adder);
318 structure = gst_caps_get_structure (caps, 0);
319 media_type = gst_structure_get_name (structure);
320 if (strcmp (media_type, "audio/x-raw-int") == 0) {
321 adder->format = GST_ADDER_FORMAT_INT;
322 gst_structure_get_int (structure, "width", &adder->width);
323 gst_structure_get_int (structure, "depth", &adder->depth);
324 gst_structure_get_int (structure, "endianness", &adder->endianness);
325 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
327 GST_INFO_OBJECT (pad, "parse_caps sets adder to format int, %d bit",
330 if (adder->endianness != G_BYTE_ORDER)
333 switch (adder->width) {
335 adder->func = (adder->is_signed ?
336 (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
339 adder->func = (adder->is_signed ?
340 (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
343 adder->func = (adder->is_signed ?
344 (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
349 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
350 adder->format = GST_ADDER_FORMAT_FLOAT;
351 gst_structure_get_int (structure, "width", &adder->width);
352 gst_structure_get_int (structure, "endianness", &adder->endianness);
354 GST_INFO_OBJECT (pad, "parse_caps sets adder to format float, %d bit",
357 if (adder->endianness != G_BYTE_ORDER)
360 switch (adder->width) {
362 adder->func = (GstAdderFunction) add_float32;
365 adder->func = (GstAdderFunction) add_float64;
374 gst_structure_get_int (structure, "channels", &adder->channels);
375 gst_structure_get_int (structure, "rate", &adder->rate);
377 adder->bps = (adder->width / 8) * adder->channels;
384 GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
389 /* FIXME, the duration query should reflect how long you will produce
390 * data, that is the amount of stream time until you will emit EOS.
392 * For synchronized mixing this is always the max of all the durations
393 * of upstream since we emit EOS when all of them finished.
395 * We don't do synchronized mixing so this really depends on where the
396 * streams where punched in and what their relative offsets are against
397 * eachother which we can get from the first timestamps we see.
399 * When we add a new stream (or remove a stream) the duration might
400 * also become invalid again and we need to post a new DURATION
401 * message to notify this fact to the parent.
402 * For now we take the max of all the upstream elements so the simple
403 * cases work at least somewhat.
406 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
415 gst_query_parse_duration (query, &format, NULL);
421 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
423 GstIteratorResult ires;
427 ires = gst_iterator_next (it, &item);
429 case GST_ITERATOR_DONE:
432 case GST_ITERATOR_OK:
434 GstPad *pad = GST_PAD_CAST (item);
438 /* ask sink peer for duration */
439 res &= gst_pad_query_peer_duration (pad, &format, &duration);
440 /* take max from all valid return values */
442 /* valid unknown length, stop searching */
443 if (duration == -1) {
447 /* else see if bigger than current max */
448 else if (duration > max)
451 gst_object_unref (pad);
454 case GST_ITERATOR_RESYNC:
457 gst_iterator_resync (it);
465 gst_iterator_free (it);
468 /* and store the max */
469 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
470 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
471 gst_query_set_duration (query, format, max);
478 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
480 GstClockTime min, max;
491 max = GST_CLOCK_TIME_NONE;
493 /* Take maximum of all latency values */
494 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
496 GstIteratorResult ires;
500 ires = gst_iterator_next (it, &item);
502 case GST_ITERATOR_DONE:
505 case GST_ITERATOR_OK:
507 GstPad *pad = GST_PAD_CAST (item);
509 GstClockTime min_cur, max_cur;
512 peerquery = gst_query_new_latency ();
514 /* Ask peer for latency */
515 res &= gst_pad_peer_query (pad, peerquery);
517 /* take max from all valid return values */
519 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
524 if (max_cur != GST_CLOCK_TIME_NONE &&
525 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
526 (max == GST_CLOCK_TIME_NONE)))
529 live = live || live_cur;
532 gst_query_unref (peerquery);
533 gst_object_unref (pad);
536 case GST_ITERATOR_RESYNC:
539 max = GST_CLOCK_TIME_NONE;
541 gst_iterator_resync (it);
549 gst_iterator_free (it);
552 /* store the results */
553 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
554 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
555 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
556 gst_query_set_latency (query, live, min, max);
563 gst_adder_query (GstPad * pad, GstQuery * query)
565 GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
566 gboolean res = FALSE;
568 switch (GST_QUERY_TYPE (query)) {
569 case GST_QUERY_POSITION:
573 gst_query_parse_position (query, &format, NULL);
576 case GST_FORMAT_TIME:
577 /* FIXME, bring to stream time, might be tricky */
578 gst_query_set_position (query, format, adder->timestamp);
581 case GST_FORMAT_DEFAULT:
582 gst_query_set_position (query, format, adder->offset);
590 case GST_QUERY_DURATION:
591 res = gst_adder_query_duration (adder, query);
593 case GST_QUERY_LATENCY:
594 res = gst_adder_query_latency (adder, query);
597 /* FIXME, needs a custom query handler because we have multiple
599 res = gst_pad_query_default (pad, query);
603 gst_object_unref (adder);
614 forward_event_func (GstPad * pad, GValue * ret, EventData * data)
616 GstEvent *event = data->event;
618 gst_event_ref (event);
619 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
620 if (!gst_pad_push_event (pad, event)) {
621 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
622 event, GST_EVENT_TYPE_NAME (event));
623 /* quick hack to unflush the pads, ideally we need a way to just unflush
624 * this single collect pad */
626 gst_pad_send_event (pad, gst_event_new_flush_stop ());
628 g_value_set_boolean (ret, TRUE);
629 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
630 event, GST_EVENT_TYPE_NAME (event));
632 gst_object_unref (pad);
634 /* continue on other pads, even if one failed */
638 /* forwards the event to all sinkpads, takes ownership of the
641 * Returns: TRUE if the event could be forwarded on all
645 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
649 GstIteratorResult ires;
653 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
654 GST_EVENT_TYPE_NAME (event));
659 g_value_init (&vret, G_TYPE_BOOLEAN);
660 g_value_set_boolean (&vret, FALSE);
661 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
663 ires = gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
666 case GST_ITERATOR_RESYNC:
667 GST_WARNING ("resync");
668 gst_iterator_resync (it);
669 g_value_set_boolean (&vret, TRUE);
671 case GST_ITERATOR_OK:
672 case GST_ITERATOR_DONE:
673 ret = g_value_get_boolean (&vret);
681 gst_iterator_free (it);
682 GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
683 GST_EVENT_TYPE_NAME (event), ret);
684 gst_event_unref (event);
690 gst_adder_src_event (GstPad * pad, GstEvent * event)
695 adder = GST_ADDER (gst_pad_get_parent (pad));
697 switch (GST_EVENT_TYPE (event)) {
705 /* parse the seek parameters */
706 gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
709 flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
711 /* check if we are flushing */
713 /* make sure we accept nothing anymore and return WRONG_STATE */
714 gst_collect_pads_set_flushing (adder->collect, TRUE);
716 /* flushing seek, start flush downstream, the flush will be done
717 * when all pads received a FLUSH_STOP. */
718 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
720 GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
722 /* now wait for the collected to be finished and mark a new
723 * segment. After we have the lock, no collect function is running and no
724 * new collect function will be called for as long as we're flushing. */
725 GST_OBJECT_LOCK (adder->collect);
726 if (curtype == GST_SEEK_TYPE_SET)
727 adder->segment_position = cur;
729 adder->segment_position = 0;
730 /* make sure we push a new segment, to inform about new basetime
731 * see FIXME in gst_adder_collected() */
732 adder->segment_pending = TRUE;
734 /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
735 * have stopped so that the cookie gets properly updated. */
736 gst_collect_pads_set_flushing (adder->collect, TRUE);
738 /* we might have a pending flush_stop event now. This event will either be
739 * sent by an upstream element when it completes the seek or we will push
740 * one in the collected callback ourself */
741 adder->flush_stop_pending = flush;
742 GST_OBJECT_UNLOCK (adder->collect);
743 GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
746 result = forward_event (adder, event, flush);
748 /* seek failed. maybe source is a live source. */
749 GST_DEBUG_OBJECT (adder, "seeking failed");
751 /* FIXME: ideally we would like to send a flush-stop event from here but
752 * collectpads does not have a method that allows us to do that. Instead
753 * we forward all flush-stop events we receive on the sinkpads. We might
754 * be sending too many flush-stop events. */
758 /* QoS might be tricky */
761 case GST_EVENT_NAVIGATION:
762 /* navigation is rather pointless. */
766 /* just forward the rest for now */
767 GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
768 GST_EVENT_TYPE_NAME (event));
769 result = forward_event (adder, event, FALSE);
772 gst_object_unref (adder);
778 gst_adder_sink_event (GstPad * pad, GstEvent * event)
783 adder = GST_ADDER (gst_pad_get_parent (pad));
785 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
786 GST_DEBUG_PAD_NAME (pad));
788 switch (GST_EVENT_TYPE (event)) {
789 case GST_EVENT_FLUSH_STOP:
790 /* we received a flush-stop. The collect_event function will push the
791 * event past our element. We simply forward all flush-stop events, even
792 * when no flush-stop was pending, this is required because collectpads
793 * does not provide an API to handle-but-not-forward the flush-stop.
794 * We unset the pending flush-stop flag so that we don't send anymore
795 * flush-stop from the collect function later.
797 GST_OBJECT_LOCK (adder->collect);
798 adder->segment_pending = TRUE;
799 adder->flush_stop_pending = FALSE;
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_OBJECT_UNLOCK (adder->collect);
809 GST_OBJECT_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_OBJECT_UNLOCK (adder->collect);
818 /* now GstCollectPads can take care of the rest, e.g. EOS */
819 ret = adder->collect_event (pad, event);
822 gst_object_unref (adder);
827 gst_adder_class_init (GstAdderClass * klass)
829 GObjectClass *gobject_class = (GObjectClass *) klass;
830 GstElementClass *gstelement_class = (GstElementClass *) klass;
832 gobject_class->set_property = gst_adder_set_property;
833 gobject_class->get_property = gst_adder_get_property;
834 gobject_class->dispose = gst_adder_dispose;
836 gst_element_class_add_pad_template (gstelement_class,
837 gst_static_pad_template_get (&gst_adder_src_template));
838 gst_element_class_add_pad_template (gstelement_class,
839 gst_static_pad_template_get (&gst_adder_sink_template));
840 gst_element_class_set_details_simple (gstelement_class, "Adder",
842 "Add N audio channels together",
843 "Thomas Vander Stichele <thomas at apestaart dot org>");
845 parent_class = g_type_class_peek_parent (klass);
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 gstelement_class->request_new_pad =
860 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
861 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
862 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
866 gst_adder_init (GstAdder * adder)
868 GstPadTemplate *template;
870 template = gst_static_pad_template_get (&gst_adder_src_template);
871 adder->srcpad = gst_pad_new_from_template (template, "src");
872 gst_object_unref (template);
874 gst_pad_set_getcaps_function (adder->srcpad,
875 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
876 gst_pad_set_setcaps_function (adder->srcpad,
877 GST_DEBUG_FUNCPTR (gst_adder_setcaps));
878 gst_pad_set_query_function (adder->srcpad,
879 GST_DEBUG_FUNCPTR (gst_adder_query));
880 gst_pad_set_event_function (adder->srcpad,
881 GST_DEBUG_FUNCPTR (gst_adder_src_event));
882 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
884 adder->format = GST_ADDER_FORMAT_UNSET;
888 adder->filter_caps = NULL;
890 /* keep track of the sinkpads requested */
891 adder->collect = gst_collect_pads_new ();
892 gst_collect_pads_set_function (adder->collect,
893 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
894 gst_collect_pads_set_clip_function (adder->collect,
895 GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
899 gst_adder_dispose (GObject * object)
901 GstAdder *adder = GST_ADDER (object);
903 if (adder->collect) {
904 gst_object_unref (adder->collect);
905 adder->collect = NULL;
907 gst_caps_replace (&adder->filter_caps, NULL);
908 if (adder->pending_events) {
909 g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
910 g_list_free (adder->pending_events);
911 adder->pending_events = NULL;
914 G_OBJECT_CLASS (parent_class)->dispose (object);
918 gst_adder_set_property (GObject * object, guint prop_id,
919 const GValue * value, GParamSpec * pspec)
921 GstAdder *adder = GST_ADDER (object);
924 case PROP_FILTER_CAPS:{
925 GstCaps *new_caps = NULL;
927 const GstCaps *new_caps_val = gst_value_get_caps (value);
929 if (new_caps_val != NULL) {
930 new_caps = (GstCaps *) new_caps_val;
931 gst_caps_ref (new_caps);
934 GST_OBJECT_LOCK (adder);
935 old_caps = adder->filter_caps;
936 adder->filter_caps = new_caps;
937 GST_OBJECT_UNLOCK (adder);
940 gst_caps_unref (old_caps);
942 GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
946 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
952 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
955 GstAdder *adder = GST_ADDER (object);
958 case PROP_FILTER_CAPS:
959 GST_OBJECT_LOCK (adder);
960 gst_value_set_caps (value, adder->filter_caps);
961 GST_OBJECT_UNLOCK (adder);
964 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
971 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
972 const gchar * unused)
979 if (templ->direction != GST_PAD_SINK)
982 adder = GST_ADDER (element);
984 /* increment pad counter */
985 padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
987 name = g_strdup_printf ("sink%d", padcount);
988 newpad = gst_pad_new_from_template (templ, name);
989 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
992 gst_pad_set_getcaps_function (newpad,
993 GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
994 gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
995 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
997 /* FIXME: hacked way to override/extend the event function of
998 * GstCollectPads; because it sets its own event function giving the
999 * element no access to events */
1000 adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
1001 gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
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_pads_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));
1033 gst_collect_pads_remove_pad (adder->collect, pad);
1034 gst_element_remove_pad (element, pad);
1038 gst_adder_do_clip (GstCollectPads * pads, GstCollectData * data,
1039 GstBuffer * buffer, gpointer user_data)
1041 GstAdder *adder = GST_ADDER (user_data);
1043 buffer = gst_audio_buffer_clip (buffer, &data->segment, adder->rate,
1049 static GstFlowReturn
1050 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
1053 * combine streams by adding data values
1055 * - this function is called when all pads have a buffer
1056 * - get available bytes on all pads.
1057 * - repeat for each input pad :
1058 * - read available bytes, copy or add to target buffer
1059 * - if there's an EOS event, remove the input channel
1060 * - push out the output buffer
1063 * - would be nice to have a mixing mode, where instead of adding we mix
1064 * - for float we could downscale after collect loop
1065 * - for int we need to downscale each input to avoid clipping or
1066 * mix into a temp (float) buffer and scale afterwards as well
1069 GSList *collected, *next = NULL;
1071 GstBuffer *outbuf = NULL, *gapbuf = NULL;
1072 gpointer outdata = NULL;
1075 adder = GST_ADDER (user_data);
1078 if (G_UNLIKELY (adder->func == NULL))
1079 goto not_negotiated;
1081 if (adder->flush_stop_pending) {
1082 gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop ());
1083 adder->flush_stop_pending = FALSE;
1086 /* get available bytes for reading, this can be 0 which could mean empty
1087 * buffers or EOS, which we will catch when we loop over the pads. */
1088 outsize = gst_collect_pads_available (pads);
1089 /* can only happen when no pads to collect or all EOS */
1093 GST_LOG_OBJECT (adder,
1094 "starting to cycle through channels, %d bytes available (bps = %d)",
1095 outsize, adder->bps);
1097 for (collected = pads->data; collected; collected = next) {
1098 GstCollectData *collect_data;
1102 /* take next to see if this is the last collectdata */
1103 next = g_slist_next (collected);
1105 collect_data = (GstCollectData *) collected->data;
1107 /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1109 inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
1110 /* NULL means EOS or an empty buffer so we still need to flush in
1111 * case of an empty buffer. */
1112 if (inbuf == NULL) {
1113 GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1117 is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1119 /* Try to make an output buffer */
1120 if (outbuf == NULL) {
1121 /* if this is a gap buffer but we have some more pads to check, skip it.
1122 * If we are at the last buffer, take it, regardless if it is a GAP
1124 if (is_gap && next) {
1125 GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1126 /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1127 * EOS, we can use this one as the output buffer. */
1131 gst_buffer_unref (inbuf);
1135 GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1136 collect_data, outsize);
1137 /* make data and metadata writable, can simply return the inbuf when we
1138 * are the only one referencing this buffer. If this is the last (and
1139 * only) GAP buffer, it will automatically copy the GAP flag. */
1140 outbuf = gst_buffer_make_writable (inbuf);
1141 outdata = GST_BUFFER_DATA (outbuf);
1142 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
1145 /* we had a previous output buffer, mix this non-GAP buffer */
1149 indata = GST_BUFFER_DATA (inbuf);
1150 insize = GST_BUFFER_SIZE (inbuf);
1152 /* all buffers should have outsize, there are no short buffers because we
1153 * asked for the max size above */
1154 g_assert (insize == outsize);
1156 GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
1157 collect_data, insize, indata);
1159 /* further buffers, need to add them */
1160 adder->func ((gpointer) outdata, (gpointer) indata, insize);
1162 /* skip gap buffer */
1163 GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1165 gst_buffer_unref (inbuf);
1169 if (outbuf == NULL) {
1170 /* no output buffer, reuse one of the GAP buffers then if we have one */
1172 GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1175 /* assume EOS otherwise, this should not happen, really */
1178 /* we had an output buffer, unref the gapbuffer we kept */
1179 gst_buffer_unref (gapbuf);
1181 /* our timestamping is very simple, just an ever incrementing
1182 * counter, the new segment time will take care of their respective
1184 if (adder->segment_pending) {
1187 /* FIXME, use rate/applied_rate as set on all sinkpads.
1188 * - currently we just set rate as received from last seek-event
1189 * We could potentially figure out the duration as well using
1190 * the current segment positions and the stated stop positions.
1191 * Also we just start from stream time 0 which is rather
1192 * weird. For non-synchronized mixing, the time should be
1193 * the min of the stream times of all received segments,
1194 * rationale being that the duration is at least going to
1195 * be as long as the earliest stream we start mixing. This
1196 * would also be correct for synchronized mixing but then
1197 * the later streams would be delayed until the stream times
1200 if (adder->segment_rate > 0.0) {
1201 event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
1202 1.0, GST_FORMAT_TIME, adder->timestamp, GST_CLOCK_TIME_NONE,
1203 adder->segment_position);
1205 event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
1206 1.0, GST_FORMAT_TIME, G_GINT64_CONSTANT (0), adder->timestamp,
1207 adder->segment_position);
1209 GST_INFO_OBJECT (adder->srcpad, "new segment event for "
1210 "rate:%lf start:%" G_GINT64_FORMAT " cur:%" G_GUINT64_FORMAT,
1211 adder->segment_rate, adder->timestamp, adder->segment_position);
1214 if (!gst_pad_push_event (adder->srcpad, event)) {
1215 GST_WARNING_OBJECT (adder->srcpad, "Sending event %p (%s) failed.",
1216 event, GST_EVENT_TYPE_NAME (event));
1218 adder->segment_pending = FALSE;
1219 adder->segment_position = 0;
1221 GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1222 "start:%" G_GINT64_FORMAT " pos:%" G_GUINT64_FORMAT " failed",
1223 adder->timestamp, adder->segment_position);
1227 if (G_UNLIKELY (adder->pending_events)) {
1228 GList *tmp = adder->pending_events;
1231 GstEvent *ev = (GstEvent *) tmp->data;
1233 gst_pad_push_event (adder->srcpad, ev);
1234 tmp = g_list_next (tmp);
1236 g_list_free (adder->pending_events);
1237 adder->pending_events = NULL;
1240 /* set timestamps on the output buffer */
1241 GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
1242 GST_BUFFER_OFFSET (outbuf) = adder->offset;
1244 /* for the next timestamp, use the sample counter, which will
1245 * never accumulate rounding errors */
1246 if (adder->segment_rate > 0.0) {
1247 adder->offset += outsize / adder->bps;
1249 adder->offset -= outsize / adder->bps;
1251 adder->timestamp = gst_util_uint64_scale_int (adder->offset,
1252 GST_SECOND, adder->rate);
1254 /* now we can set the duration of the buffer */
1255 GST_BUFFER_DURATION (outbuf) = adder->timestamp -
1256 GST_BUFFER_TIMESTAMP (outbuf);
1259 GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT,
1260 outbuf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
1261 ret = gst_pad_push (adder->srcpad, outbuf);
1263 GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1270 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1271 ("Unknown data received, not negotiated"));
1272 return GST_FLOW_NOT_NEGOTIATED;
1276 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1277 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1278 return GST_FLOW_UNEXPECTED;
1282 static GstStateChangeReturn
1283 gst_adder_change_state (GstElement * element, GstStateChange transition)
1286 GstStateChangeReturn ret;
1288 adder = GST_ADDER (element);
1290 switch (transition) {
1291 case GST_STATE_CHANGE_NULL_TO_READY:
1293 case GST_STATE_CHANGE_READY_TO_PAUSED:
1294 adder->timestamp = 0;
1296 adder->flush_stop_pending = FALSE;
1297 adder->segment_pending = TRUE;
1298 adder->segment_position = 0;
1299 adder->segment_rate = 1.0;
1300 gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1301 gst_collect_pads_start (adder->collect);
1303 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1305 case GST_STATE_CHANGE_PAUSED_TO_READY:
1306 /* need to unblock the collectpads before calling the
1307 * parent change_state so that streaming can finish */
1308 gst_collect_pads_stop (adder->collect);
1314 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1316 switch (transition) {
1326 plugin_init (GstPlugin * plugin)
1330 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1337 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1340 "Adds multiple streams",
1341 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)