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
28 * The Adder allows to mix several streams into one by adding the data.
29 * Mixed data is clamped to the min/max values of the data format.
31 * <title>Example launch line</title>
34 * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36 * This pipeline produces two sine waves mixed together.
39 * The Adder currently mixes all data received on the sinkpads as soon as possible
40 * without trying to synchronize the streams.
44 * Last reviewed on 2006-05-09 (0.10.7)
46 /* Element-Checklist-Version: 5 */
52 #include <gst/audio/audio.h>
53 #include <string.h> /* strcmp */
55 /* highest positive/lowest negative x-bit value we can use for clamping */
56 #define MAX_INT_32 ((gint32) (0x7fffffff))
57 #define MAX_INT_16 ((gint16) (0x7fff))
58 #define MAX_INT_8 ((gint8) (0x7f))
59 #define MAX_UINT_32 ((guint32)(0xffffffff))
60 #define MAX_UINT_16 ((guint16)(0xffff))
61 #define MAX_UINT_8 ((guint8) (0xff))
63 #define MIN_INT_32 ((gint32) (0x80000000))
64 #define MIN_INT_16 ((gint16) (0x8000))
65 #define MIN_INT_8 ((gint8) (0x80))
66 #define MIN_UINT_32 ((guint32)(0x00000000))
67 #define MIN_UINT_16 ((guint16)(0x0000))
68 #define MIN_UINT_8 ((guint8) (0x00))
70 #define GST_CAT_DEFAULT gst_adder_debug
71 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
73 /* elementfactory information */
77 "rate = (int) [ 1, MAX ], " \
78 "channels = (int) [ 1, MAX ], " \
79 "endianness = (int) BYTE_ORDER, " \
80 "width = (int) 32, " \
81 "depth = (int) 32, " \
82 "signed = (boolean) { true, false } ;" \
84 "rate = (int) [ 1, MAX ], " \
85 "channels = (int) [ 1, MAX ], " \
86 "endianness = (int) BYTE_ORDER, " \
87 "width = (int) 16, " \
88 "depth = (int) 16, " \
89 "signed = (boolean) { true, false } ;" \
91 "rate = (int) [ 1, MAX ], " \
92 "channels = (int) [ 1, MAX ], " \
93 "endianness = (int) BYTE_ORDER, " \
96 "signed = (boolean) { true, false } ;" \
97 "audio/x-raw-float, " \
98 "rate = (int) [ 1, MAX ], " \
99 "channels = (int) [ 1, MAX ], " \
100 "endianness = (int) BYTE_ORDER, " \
101 "width = (int) { 32, 64 }"
103 static GstStaticPadTemplate gst_adder_src_template =
104 GST_STATIC_PAD_TEMPLATE ("src",
107 GST_STATIC_CAPS (CAPS)
110 static GstStaticPadTemplate gst_adder_sink_template =
111 GST_STATIC_PAD_TEMPLATE ("sink%d",
114 GST_STATIC_CAPS (CAPS)
117 static void gst_adder_class_init (GstAdderClass * klass);
119 static void gst_adder_init (GstAdder * adder);
121 static void gst_adder_finalize (GObject * object);
123 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
125 static gboolean gst_adder_query (GstPad * pad, GstQuery * query);
127 static gboolean gst_adder_src_event (GstPad * pad, GstEvent * event);
129 static gboolean gst_adder_sink_event (GstPad * pad, GstEvent * event);
131 static GstPad *gst_adder_request_new_pad (GstElement * element,
132 GstPadTemplate * temp, const gchar * unused);
133 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
135 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
136 GstStateChange transition);
138 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
141 static GstElementClass *parent_class = NULL;
144 gst_adder_get_type (void)
146 static GType adder_type = 0;
148 if (G_UNLIKELY (adder_type == 0)) {
149 static const GTypeInfo adder_info = {
150 sizeof (GstAdderClass), NULL, NULL,
151 (GClassInitFunc) gst_adder_class_init, NULL, NULL,
152 sizeof (GstAdder), 0,
153 (GInstanceInitFunc) gst_adder_init,
156 adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
158 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
159 "audio channel mixing element");
164 /* clipping versions */
165 #define MAKE_FUNC(name,type,ttype,min,max) \
166 static void name (type *out, type *in, gint bytes) { \
168 for (i = 0; i < bytes / sizeof (type); i++) \
169 out[i] = CLAMP ((ttype)out[i] + (ttype)in[i], min, max); \
172 /* non-clipping versions (for float) */
173 #define MAKE_FUNC_NC(name,type,ttype) \
174 static void name (type *out, type *in, gint bytes) { \
176 for (i = 0; i < bytes / sizeof (type); i++) \
177 out[i] = (ttype)out[i] + (ttype)in[i]; \
181 MAKE_FUNC (add_int32, gint32, gint64, MIN_INT_32, MAX_INT_32)
182 MAKE_FUNC (add_int16, gint16, gint32, MIN_INT_16, MAX_INT_16)
183 MAKE_FUNC (add_int8, gint8, gint16, MIN_INT_8, MAX_INT_8)
184 MAKE_FUNC (add_uint32, guint32, guint64, MIN_UINT_32, MAX_UINT_32)
185 MAKE_FUNC (add_uint16, guint16, guint32, MIN_UINT_16, MAX_UINT_16)
186 MAKE_FUNC (add_uint8, guint8, guint16, MIN_UINT_8, MAX_UINT_8)
187 MAKE_FUNC_NC (add_float64, gdouble, gdouble)
188 MAKE_FUNC_NC (add_float32, gfloat, gfloat)
191 /* we can only accept caps that we and downstream can handle. */
193 gst_adder_sink_getcaps (GstPad * pad)
197 GstCaps *result, *peercaps, *sinkcaps;
199 adder = GST_ADDER (GST_PAD_PARENT (pad));
201 GST_OBJECT_LOCK (adder);
202 /* get the downstream possible caps */
203 peercaps = gst_pad_peer_get_caps (adder->srcpad);
204 /* get the allowed caps on this sinkpad, we use the fixed caps function so
205 * that it does not call recursively in this function. */
206 sinkcaps = gst_pad_get_fixed_caps_func (pad);
208 /* if the peer has caps, intersect */
209 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
210 result = gst_caps_intersect (peercaps, sinkcaps);
211 gst_caps_unref (peercaps);
212 gst_caps_unref (sinkcaps);
214 /* the peer has no caps (or there is no peer), just use the allowed caps
215 * of this sinkpad. */
216 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
219 GST_OBJECT_UNLOCK (adder);
224 /* the first caps we receive on any of the sinkpads will define the caps for all
225 * the other sinkpads because we can only mix streams with the same caps.
228 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
234 GstStructure *structure;
236 const char *media_type;
238 adder = GST_ADDER (GST_PAD_PARENT (pad));
240 GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
241 GST_PAD_NAME (pad), caps);
243 /* FIXME, see if the other pads can accept the format. Also lock the
244 * format on the other pads to this new format. */
245 GST_OBJECT_LOCK (adder);
246 pads = GST_ELEMENT (adder)->pads;
248 GstPad *otherpad = GST_PAD (pads->data);
250 if (otherpad != pad) {
251 gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
253 pads = g_list_next (pads);
255 GST_OBJECT_UNLOCK (adder);
258 structure = gst_caps_get_structure (caps, 0);
259 media_type = gst_structure_get_name (structure);
260 if (strcmp (media_type, "audio/x-raw-int") == 0) {
261 GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format int");
262 adder->format = GST_ADDER_FORMAT_INT;
263 gst_structure_get_int (structure, "width", &adder->width);
264 gst_structure_get_int (structure, "depth", &adder->depth);
265 gst_structure_get_int (structure, "endianness", &adder->endianness);
266 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
268 if (adder->endianness != G_BYTE_ORDER)
271 switch (adder->width) {
273 adder->func = (adder->is_signed ?
274 (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
277 adder->func = (adder->is_signed ?
278 (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
281 adder->func = (adder->is_signed ?
282 (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
287 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
288 GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format float");
289 adder->format = GST_ADDER_FORMAT_FLOAT;
290 gst_structure_get_int (structure, "width", &adder->width);
291 gst_structure_get_int (structure, "endianness", &adder->endianness);
293 if (adder->endianness != G_BYTE_ORDER)
296 switch (adder->width) {
298 adder->func = (GstAdderFunction) add_float32;
301 adder->func = (GstAdderFunction) add_float64;
310 gst_structure_get_int (structure, "channels", &adder->channels);
311 gst_structure_get_int (structure, "rate", &adder->rate);
313 adder->bps = (adder->width / 8) * adder->channels;
320 GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
325 /* FIXME, the duration query should reflect how long you will produce
326 * data, that is the amount of stream time until you will emit EOS.
328 * For synchronized mixing this is always the max of all the durations
329 * of upstream since we emit EOS when all of them finished.
331 * We don't do synchronized mixing so this really depends on where the
332 * streams where punched in and what their relative offsets are against
333 * eachother which we can get from the first timestamps we see.
335 * When we add a new stream (or remove a stream) the duration might
336 * also become invalid again and we need to post a new DURATION
337 * message to notify this fact to the parent.
338 * For now we take the max of all the upstream elements so the simple
339 * cases work at least somewhat.
342 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
355 gst_query_parse_duration (query, &format, NULL);
361 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
363 GstIteratorResult ires;
367 ires = gst_iterator_next (it, &item);
369 case GST_ITERATOR_DONE:
372 case GST_ITERATOR_OK:
374 GstPad *pad = GST_PAD_CAST (item);
378 /* ask sink peer for duration */
379 res &= gst_pad_query_peer_duration (pad, &format, &duration);
380 /* take max from all valid return values */
382 /* valid unknown length, stop searching */
383 if (duration == -1) {
387 /* else see if bigger than current max */
388 else if (duration > max)
391 gst_object_unref (pad);
394 case GST_ITERATOR_RESYNC:
397 gst_iterator_resync (it);
405 gst_iterator_free (it);
408 /* and store the max */
409 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
410 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
411 gst_query_set_duration (query, format, max);
418 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
420 GstClockTime min, max;
435 max = GST_CLOCK_TIME_NONE;
437 /* Take maximum of all latency values */
438 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
440 GstIteratorResult ires;
444 ires = gst_iterator_next (it, &item);
446 case GST_ITERATOR_DONE:
449 case GST_ITERATOR_OK:
451 GstPad *pad = GST_PAD_CAST (item);
455 GstClockTime min_cur, max_cur;
459 peerquery = gst_query_new_latency ();
461 /* Ask peer for latency */
462 res &= gst_pad_peer_query (pad, peerquery);
464 /* take max from all valid return values */
466 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
471 if (max_cur != GST_CLOCK_TIME_NONE &&
472 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
473 (max == GST_CLOCK_TIME_NONE)))
476 live = live || live_cur;
479 gst_query_unref (peerquery);
480 gst_object_unref (pad);
483 case GST_ITERATOR_RESYNC:
486 max = GST_CLOCK_TIME_NONE;
488 gst_iterator_resync (it);
496 gst_iterator_free (it);
499 /* store the results */
500 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
501 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
502 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
503 gst_query_set_latency (query, live, min, max);
510 gst_adder_query (GstPad * pad, GstQuery * query)
512 GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
514 gboolean res = FALSE;
516 switch (GST_QUERY_TYPE (query)) {
517 case GST_QUERY_POSITION:
521 gst_query_parse_position (query, &format, NULL);
524 case GST_FORMAT_TIME:
525 /* FIXME, bring to stream time, might be tricky */
526 gst_query_set_position (query, format, adder->timestamp);
529 case GST_FORMAT_DEFAULT:
530 gst_query_set_position (query, format, adder->offset);
538 case GST_QUERY_DURATION:
539 res = gst_adder_query_duration (adder, query);
541 case GST_QUERY_LATENCY:
542 res = gst_adder_query_latency (adder, query);
545 /* FIXME, needs a custom query handler because we have multiple
547 res = gst_pad_query_default (pad, query);
551 gst_object_unref (adder);
556 forward_event_func (GstPad * pad, GValue * ret, GstEvent * event)
558 gst_event_ref (event);
559 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
560 if (!gst_pad_push_event (pad, event)) {
561 g_value_set_boolean (ret, FALSE);
562 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
563 event, GST_EVENT_TYPE_NAME (event));
565 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
566 event, GST_EVENT_TYPE_NAME (event));
568 gst_object_unref (pad);
572 /* forwards the event to all sinkpads, takes ownership of the
575 * Returns: TRUE if the event could be forwarded on all
579 forward_event (GstAdder * adder, GstEvent * event)
586 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
587 GST_EVENT_TYPE_NAME (event));
591 g_value_init (&vret, G_TYPE_BOOLEAN);
592 g_value_set_boolean (&vret, TRUE);
593 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
594 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
596 gst_iterator_free (it);
597 gst_event_unref (event);
599 ret = g_value_get_boolean (&vret);
605 gst_adder_src_event (GstPad * pad, GstEvent * event)
611 adder = GST_ADDER (gst_pad_get_parent (pad));
613 switch (GST_EVENT_TYPE (event)) {
615 /* QoS might be tricky */
626 /* parse the seek parameters */
627 gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
630 /* check if we are flushing */
631 if (flags & GST_SEEK_FLAG_FLUSH) {
632 /* make sure we accept nothing anymore and return WRONG_STATE */
633 gst_collect_pads_set_flushing (adder->collect, TRUE);
635 /* flushing seek, start flush downstream, the flush will be done
636 * when all pads received a FLUSH_STOP. */
637 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
640 /* now wait for the collected to be finished and mark a new
642 GST_OBJECT_LOCK (adder->collect);
643 if (curtype == GST_SEEK_TYPE_SET)
644 adder->segment_position = cur;
646 adder->segment_position = 0;
647 adder->segment_pending = TRUE;
648 GST_OBJECT_UNLOCK (adder->collect);
650 result = forward_event (adder, event);
653 case GST_EVENT_NAVIGATION:
654 /* navigation is rather pointless. */
658 /* just forward the rest for now */
659 result = forward_event (adder, event);
662 gst_object_unref (adder);
668 gst_adder_sink_event (GstPad * pad, GstEvent * event)
674 adder = GST_ADDER (gst_pad_get_parent (pad));
676 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
677 GST_DEBUG_PAD_NAME (pad));
679 switch (GST_EVENT_TYPE (event)) {
680 case GST_EVENT_FLUSH_STOP:
681 /* mark a pending new segment. This event is synchronized
682 * with the streaming thread so we can safely update the
683 * variable without races. It's somewhat weird because we
684 * assume the collectpads forwarded the FLUSH_STOP past us
685 * and downstream (using our source pad, the bastard!).
687 adder->segment_pending = TRUE;
693 /* now GstCollectPads can take care of the rest, e.g. EOS */
694 ret = adder->collect_event (pad, event);
696 gst_object_unref (adder);
701 gst_adder_class_init (GstAdderClass * klass)
703 GObjectClass *gobject_class;
705 GstElementClass *gstelement_class;
707 gobject_class = (GObjectClass *) klass;
709 gobject_class->finalize = gst_adder_finalize;
711 gstelement_class = (GstElementClass *) klass;
713 gst_element_class_add_pad_template (gstelement_class,
714 gst_static_pad_template_get (&gst_adder_src_template));
715 gst_element_class_add_pad_template (gstelement_class,
716 gst_static_pad_template_get (&gst_adder_sink_template));
717 gst_element_class_set_details_simple (gstelement_class, "Adder",
719 "Add N audio channels together", "Thomas <thomas@apestaart.org>");
721 parent_class = g_type_class_peek_parent (klass);
723 gstelement_class->request_new_pad = gst_adder_request_new_pad;
724 gstelement_class->release_pad = gst_adder_release_pad;
725 gstelement_class->change_state = gst_adder_change_state;
729 gst_adder_init (GstAdder * adder)
731 GstPadTemplate *template;
733 template = gst_static_pad_template_get (&gst_adder_src_template);
734 adder->srcpad = gst_pad_new_from_template (template, "src");
735 gst_object_unref (template);
736 gst_pad_set_getcaps_function (adder->srcpad,
737 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
738 gst_pad_set_setcaps_function (adder->srcpad,
739 GST_DEBUG_FUNCPTR (gst_adder_setcaps));
740 gst_pad_set_query_function (adder->srcpad,
741 GST_DEBUG_FUNCPTR (gst_adder_query));
742 gst_pad_set_event_function (adder->srcpad,
743 GST_DEBUG_FUNCPTR (gst_adder_src_event));
744 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
746 adder->format = GST_ADDER_FORMAT_UNSET;
750 /* keep track of the sinkpads requested */
751 adder->collect = gst_collect_pads_new ();
752 gst_collect_pads_set_function (adder->collect,
753 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
757 gst_adder_finalize (GObject * object)
759 GstAdder *adder = GST_ADDER (object);
761 gst_object_unref (adder->collect);
762 adder->collect = NULL;
764 G_OBJECT_CLASS (parent_class)->finalize (object);
768 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
769 const gchar * unused)
779 if (templ->direction != GST_PAD_SINK)
782 adder = GST_ADDER (element);
784 /* increment pad counter */
785 padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
787 name = g_strdup_printf ("sink%d", padcount);
788 newpad = gst_pad_new_from_template (templ, name);
789 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
792 gst_pad_set_getcaps_function (newpad,
793 GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
794 gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
795 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
797 /* FIXME: hacked way to override/extend the event function of
798 * GstCollectPads; because it sets its own event function giving the
799 * element no access to events */
800 adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
801 gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
803 /* takes ownership of the pad */
804 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
812 g_warning ("gstadder: request new pad that is not a SINK pad\n");
817 GST_DEBUG_OBJECT (adder, "could not add pad");
818 gst_collect_pads_remove_pad (adder->collect, newpad);
819 gst_object_unref (newpad);
825 gst_adder_release_pad (GstElement * element, GstPad * pad)
829 adder = GST_ADDER (element);
831 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
833 gst_collect_pads_remove_pad (adder->collect, pad);
834 gst_element_remove_pad (element, pad);
838 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
841 * combine channels by adding sample values
843 * - this function is called when all pads have a buffer
844 * - get available bytes on all pads.
845 * - repeat for each input pad :
846 * - read available bytes, copy or add to target buffer
847 * - if there's an EOS event, remove the input channel
848 * - push out the output buffer
862 gboolean empty = TRUE;
864 adder = GST_ADDER (user_data);
867 if (G_UNLIKELY (adder->func == NULL))
870 /* get available bytes for reading, this can be 0 which could mean
871 * empty buffers or EOS, which we will catch when we loop over the
873 size = gst_collect_pads_available (pads);
875 GST_LOG_OBJECT (adder,
876 "starting to cycle through channels, %d bytes available (bps = %d)", size,
882 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
883 GstCollectData *data;
891 data = (GstCollectData *) collected->data;
893 /* get a subbuffer of size bytes */
894 inbuf = gst_collect_pads_take_buffer (pads, data, size);
895 /* NULL means EOS or an empty buffer so we still need to flush in
896 * case of an empty buffer. */
898 GST_LOG_OBJECT (adder, "channel %p: no bytes available", data);
902 bytes = GST_BUFFER_DATA (inbuf);
903 len = GST_BUFFER_SIZE (inbuf);
905 if (outbuf == NULL) {
906 GST_LOG_OBJECT (adder, "channel %p: making output buffer of %d bytes",
909 /* first buffer, alloc size bytes. FIXME, we can easily subbuffer
910 * and _make_writable. */
911 outbuf = gst_buffer_new_and_alloc (size);
912 outbytes = GST_BUFFER_DATA (outbuf);
913 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
915 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
916 /* clear if we are only going to fill a partial buffer */
917 if (G_UNLIKELY (size > len))
918 memset (outbytes, 0, size);
920 GST_LOG_OBJECT (adder, "channel %p: copying %d bytes from data %p",
923 /* and copy the data into it */
924 memcpy (outbytes, bytes, len);
927 GST_LOG_OBJECT (adder, "channel %p: zeroing %d bytes from data %p",
929 memset (outbytes, 0, size);
932 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
933 GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
935 /* other buffers, need to add them */
936 adder->func ((gpointer) outbytes, (gpointer) bytes, len);
939 GST_LOG_OBJECT (adder, "channel %p: skipping %d bytes from data %p",
945 gst_buffer_unref (inbuf);
948 /* can only happen when no pads to collect or all EOS */
952 /* our timestamping is very simple, just an ever incrementing
953 * counter, the new segment time will take care of their respective
955 if (adder->segment_pending) {
958 /* FIXME, use rate/applied_rate as set on all sinkpads.
959 * - currently we just set rate as received from last seek-event
960 * We could potentially figure out the duration as well using
961 * the current segment positions and the stated stop positions.
962 * Also we just start from stream time 0 which is rather
963 * weird. For non-synchronized mixing, the time should be
964 * the min of the stream times of all received segments,
965 * rationale being that the duration is at least going to
966 * be as long as the earliest stream we start mixing. This
967 * would also be correct for synchronized mixing but then
968 * the later streams would be delayed until the stream times
971 event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
972 1.0, GST_FORMAT_TIME, adder->timestamp, -1, adder->segment_position);
974 gst_pad_push_event (adder->srcpad, event);
975 adder->segment_pending = FALSE;
976 adder->segment_position = 0;
979 /* set timestamps on the output buffer */
980 GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
981 GST_BUFFER_OFFSET (outbuf) = adder->offset;
983 /* for the next timestamp, use the sample counter, which will
984 * never accumulate rounding errors */
985 adder->offset += size / adder->bps;
986 adder->timestamp = gst_util_uint64_scale_int (adder->offset,
987 GST_SECOND, adder->rate);
989 /* now we can set the duration of the buffer */
990 GST_BUFFER_DURATION (outbuf) = adder->timestamp -
991 GST_BUFFER_TIMESTAMP (outbuf);
993 /* if we only processed silence, mark output again as silence */
995 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
998 GST_LOG_OBJECT (adder, "pushing outbuf, timestamp %" GST_TIME_FORMAT,
999 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
1000 ret = gst_pad_push (adder->srcpad, outbuf);
1007 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1008 ("Unknown data received, not negotiated"));
1009 return GST_FLOW_NOT_NEGOTIATED;
1013 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1014 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1015 return GST_FLOW_UNEXPECTED;
1019 static GstStateChangeReturn
1020 gst_adder_change_state (GstElement * element, GstStateChange transition)
1024 GstStateChangeReturn ret;
1026 adder = GST_ADDER (element);
1028 switch (transition) {
1029 case GST_STATE_CHANGE_NULL_TO_READY:
1031 case GST_STATE_CHANGE_READY_TO_PAUSED:
1032 adder->timestamp = 0;
1034 adder->segment_pending = TRUE;
1035 adder->segment_position = 0;
1036 adder->segment_rate = 1.0;
1037 gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1038 gst_collect_pads_start (adder->collect);
1040 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1042 case GST_STATE_CHANGE_PAUSED_TO_READY:
1043 /* need to unblock the collectpads before calling the
1044 * parent change_state so that streaming can finish */
1045 gst_collect_pads_stop (adder->collect);
1051 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1053 switch (transition) {
1063 plugin_init (GstPlugin * plugin)
1065 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1072 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1075 "Adds multiple streams",
1076 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)