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)
196 GstCaps *result, *peercaps, *sinkcaps;
198 adder = GST_ADDER (GST_PAD_PARENT (pad));
200 GST_OBJECT_LOCK (adder);
201 /* get the downstream possible caps */
202 peercaps = gst_pad_peer_get_caps (adder->srcpad);
203 /* get the allowed caps on this sinkpad, we use the fixed caps function so
204 * that it does not call recursively in this function. */
205 sinkcaps = gst_pad_get_fixed_caps_func (pad);
207 /* if the peer has caps, intersect */
208 GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
209 result = gst_caps_intersect (peercaps, sinkcaps);
210 gst_caps_unref (peercaps);
211 gst_caps_unref (sinkcaps);
213 /* the peer has no caps (or there is no peer), just use the allowed caps
214 * of this sinkpad. */
215 GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
218 GST_OBJECT_UNLOCK (adder);
223 /* the first caps we receive on any of the sinkpads will define the caps for all
224 * the other sinkpads because we can only mix streams with the same caps.
227 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
233 GstStructure *structure;
235 const char *media_type;
237 adder = GST_ADDER (GST_PAD_PARENT (pad));
239 GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
240 GST_PAD_NAME (pad), caps);
242 /* FIXME, see if the other pads can accept the format. Also lock the
243 * format on the other pads to this new format. */
244 GST_OBJECT_LOCK (adder);
245 pads = GST_ELEMENT (adder)->pads;
247 GstPad *otherpad = GST_PAD (pads->data);
249 if (otherpad != pad) {
250 gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
252 pads = g_list_next (pads);
254 GST_OBJECT_UNLOCK (adder);
257 structure = gst_caps_get_structure (caps, 0);
258 media_type = gst_structure_get_name (structure);
259 if (strcmp (media_type, "audio/x-raw-int") == 0) {
260 GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format int");
261 adder->format = GST_ADDER_FORMAT_INT;
262 gst_structure_get_int (structure, "width", &adder->width);
263 gst_structure_get_int (structure, "depth", &adder->depth);
264 gst_structure_get_int (structure, "endianness", &adder->endianness);
265 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
267 if (adder->endianness != G_BYTE_ORDER)
270 switch (adder->width) {
272 adder->func = (adder->is_signed ?
273 (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
276 adder->func = (adder->is_signed ?
277 (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
280 adder->func = (adder->is_signed ?
281 (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
286 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
287 GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format float");
288 adder->format = GST_ADDER_FORMAT_FLOAT;
289 gst_structure_get_int (structure, "width", &adder->width);
290 gst_structure_get_int (structure, "endianness", &adder->endianness);
292 if (adder->endianness != G_BYTE_ORDER)
295 switch (adder->width) {
297 adder->func = (GstAdderFunction) add_float32;
300 adder->func = (GstAdderFunction) add_float64;
309 gst_structure_get_int (structure, "channels", &adder->channels);
310 gst_structure_get_int (structure, "rate", &adder->rate);
312 adder->bps = (adder->width / 8) * adder->channels;
319 GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
324 /* FIXME, the duration query should reflect how long you will produce
325 * data, that is the amount of stream time until you will emit EOS.
327 * For synchronized mixing this is always the max of all the durations
328 * of upstream since we emit EOS when all of them finished.
330 * We don't do synchronized mixing so this really depends on where the
331 * streams where punched in and what their relative offsets are against
332 * eachother which we can get from the first timestamps we see.
334 * When we add a new stream (or remove a stream) the duration might
335 * also become invalid again and we need to post a new DURATION
336 * message to notify this fact to the parent.
337 * For now we take the max of all the upstream elements so the simple
338 * cases work at least somewhat.
341 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
354 gst_query_parse_duration (query, &format, NULL);
360 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
362 GstIteratorResult ires;
366 ires = gst_iterator_next (it, &item);
368 case GST_ITERATOR_DONE:
371 case GST_ITERATOR_OK:
373 GstPad *pad = GST_PAD_CAST (item);
377 /* ask sink peer for duration */
378 res &= gst_pad_query_peer_duration (pad, &format, &duration);
379 /* take max from all valid return values */
381 /* valid unknown length, stop searching */
382 if (duration == -1) {
386 /* else see if bigger than current max */
387 else if (duration > max)
390 gst_object_unref (pad);
393 case GST_ITERATOR_RESYNC:
396 gst_iterator_resync (it);
404 gst_iterator_free (it);
407 /* and store the max */
408 GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
409 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
410 gst_query_set_duration (query, format, max);
417 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
419 GstClockTime min, max;
434 max = GST_CLOCK_TIME_NONE;
436 /* Take maximum of all latency values */
437 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
439 GstIteratorResult ires;
443 ires = gst_iterator_next (it, &item);
445 case GST_ITERATOR_DONE:
448 case GST_ITERATOR_OK:
450 GstPad *pad = GST_PAD_CAST (item);
454 GstClockTime min_cur, max_cur;
458 peerquery = gst_query_new_latency ();
460 /* Ask peer for latency */
461 res &= gst_pad_peer_query (pad, peerquery);
463 /* take max from all valid return values */
465 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
470 if (max_cur != GST_CLOCK_TIME_NONE &&
471 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
472 (max == GST_CLOCK_TIME_NONE)))
475 live = live || live_cur;
478 gst_query_unref (peerquery);
479 gst_object_unref (pad);
482 case GST_ITERATOR_RESYNC:
485 max = GST_CLOCK_TIME_NONE;
487 gst_iterator_resync (it);
495 gst_iterator_free (it);
498 /* store the results */
499 GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
500 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
501 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
502 gst_query_set_latency (query, live, min, max);
509 gst_adder_query (GstPad * pad, GstQuery * query)
511 GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
513 gboolean res = FALSE;
515 switch (GST_QUERY_TYPE (query)) {
516 case GST_QUERY_POSITION:
520 gst_query_parse_position (query, &format, NULL);
523 case GST_FORMAT_TIME:
524 /* FIXME, bring to stream time, might be tricky */
525 gst_query_set_position (query, format, adder->timestamp);
528 case GST_FORMAT_DEFAULT:
529 gst_query_set_position (query, format, adder->offset);
537 case GST_QUERY_DURATION:
538 res = gst_adder_query_duration (adder, query);
540 case GST_QUERY_LATENCY:
541 res = gst_adder_query_latency (adder, query);
544 /* FIXME, needs a custom query handler because we have multiple
546 res = gst_pad_query_default (pad, query);
550 gst_object_unref (adder);
555 forward_event_func (GstPad * pad, GValue * ret, GstEvent * event)
557 gst_event_ref (event);
558 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
559 if (!gst_pad_push_event (pad, event)) {
560 g_value_set_boolean (ret, FALSE);
561 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
562 event, GST_EVENT_TYPE_NAME (event));
564 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
565 event, GST_EVENT_TYPE_NAME (event));
567 gst_object_unref (pad);
571 /* forwards the event to all sinkpads, takes ownership of the
574 * Returns: TRUE if the event could be forwarded on all
578 forward_event (GstAdder * adder, GstEvent * event)
585 GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
586 GST_EVENT_TYPE_NAME (event));
590 g_value_init (&vret, G_TYPE_BOOLEAN);
591 g_value_set_boolean (&vret, TRUE);
592 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
593 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
595 gst_iterator_free (it);
596 gst_event_unref (event);
598 ret = g_value_get_boolean (&vret);
604 gst_adder_src_event (GstPad * pad, GstEvent * event)
610 adder = GST_ADDER (gst_pad_get_parent (pad));
612 switch (GST_EVENT_TYPE (event)) {
614 /* QoS might be tricky */
625 /* parse the seek parameters */
626 gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
629 /* check if we are flushing */
630 if (flags & GST_SEEK_FLAG_FLUSH) {
631 /* make sure we accept nothing anymore and return WRONG_STATE */
632 gst_collect_pads_set_flushing (adder->collect, TRUE);
634 /* flushing seek, start flush downstream, the flush will be done
635 * when all pads received a FLUSH_STOP. */
636 gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
639 /* now wait for the collected to be finished and mark a new
641 GST_OBJECT_LOCK (adder->collect);
642 if (curtype == GST_SEEK_TYPE_SET)
643 adder->segment_position = cur;
645 adder->segment_position = 0;
646 adder->segment_pending = TRUE;
647 GST_OBJECT_UNLOCK (adder->collect);
649 result = forward_event (adder, event);
652 case GST_EVENT_NAVIGATION:
653 /* navigation is rather pointless. */
657 /* just forward the rest for now */
658 result = forward_event (adder, event);
661 gst_object_unref (adder);
667 gst_adder_sink_event (GstPad * pad, GstEvent * event)
673 adder = GST_ADDER (gst_pad_get_parent (pad));
675 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
676 GST_DEBUG_PAD_NAME (pad));
678 switch (GST_EVENT_TYPE (event)) {
679 case GST_EVENT_FLUSH_STOP:
680 /* mark a pending new segment. This event is synchronized
681 * with the streaming thread so we can safely update the
682 * variable without races. It's somewhat weird because we
683 * assume the collectpads forwarded the FLUSH_STOP past us
684 * and downstream (using our source pad, the bastard!).
686 adder->segment_pending = TRUE;
692 /* now GstCollectPads can take care of the rest, e.g. EOS */
693 ret = adder->collect_event (pad, event);
695 gst_object_unref (adder);
700 gst_adder_class_init (GstAdderClass * klass)
702 GObjectClass *gobject_class;
704 GstElementClass *gstelement_class;
706 gobject_class = (GObjectClass *) klass;
708 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_adder_finalize);
710 gstelement_class = (GstElementClass *) klass;
712 gst_element_class_add_pad_template (gstelement_class,
713 gst_static_pad_template_get (&gst_adder_src_template));
714 gst_element_class_add_pad_template (gstelement_class,
715 gst_static_pad_template_get (&gst_adder_sink_template));
716 gst_element_class_set_details_simple (gstelement_class, "Adder",
718 "Add N audio channels together", "Thomas <thomas@apestaart.org>");
720 parent_class = g_type_class_peek_parent (klass);
722 gstelement_class->request_new_pad =
723 GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
724 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
725 gstelement_class->change_state = GST_DEBUG_FUNCPTR (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);
737 gst_pad_set_getcaps_function (adder->srcpad,
738 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
739 gst_pad_set_setcaps_function (adder->srcpad,
740 GST_DEBUG_FUNCPTR (gst_adder_setcaps));
741 gst_pad_set_query_function (adder->srcpad,
742 GST_DEBUG_FUNCPTR (gst_adder_query));
743 gst_pad_set_event_function (adder->srcpad,
744 GST_DEBUG_FUNCPTR (gst_adder_src_event));
745 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
747 adder->format = GST_ADDER_FORMAT_UNSET;
751 /* keep track of the sinkpads requested */
752 adder->collect = gst_collect_pads_new ();
753 gst_collect_pads_set_function (adder->collect,
754 GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
758 gst_adder_finalize (GObject * object)
760 GstAdder *adder = GST_ADDER (object);
762 gst_object_unref (adder->collect);
763 adder->collect = NULL;
765 G_OBJECT_CLASS (parent_class)->finalize (object);
769 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
770 const gchar * unused)
777 if (templ->direction != GST_PAD_SINK)
780 adder = GST_ADDER (element);
782 /* increment pad counter */
783 padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
785 name = g_strdup_printf ("sink%d", padcount);
786 newpad = gst_pad_new_from_template (templ, name);
787 GST_DEBUG_OBJECT (adder, "request new pad %s", name);
790 gst_pad_set_getcaps_function (newpad,
791 GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
792 gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
793 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
795 /* FIXME: hacked way to override/extend the event function of
796 * GstCollectPads; because it sets its own event function giving the
797 * element no access to events */
798 adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
799 gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
801 /* takes ownership of the pad */
802 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
810 g_warning ("gstadder: request new pad that is not a SINK pad\n");
815 GST_DEBUG_OBJECT (adder, "could not add pad");
816 gst_collect_pads_remove_pad (adder->collect, newpad);
817 gst_object_unref (newpad);
823 gst_adder_release_pad (GstElement * element, GstPad * pad)
827 adder = GST_ADDER (element);
829 GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
831 gst_collect_pads_remove_pad (adder->collect, pad);
832 gst_element_remove_pad (element, pad);
836 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
839 * combine channels by adding sample values
841 * - this function is called when all pads have a buffer
842 * - get available bytes on all pads.
843 * - repeat for each input pad :
844 * - read available bytes, copy or add to target buffer
845 * - if there's an EOS event, remove the input channel
846 * - push out the output buffer
860 gboolean empty = TRUE;
862 adder = GST_ADDER (user_data);
865 if (G_UNLIKELY (adder->func == NULL))
868 /* get available bytes for reading, this can be 0 which could mean
869 * empty buffers or EOS, which we will catch when we loop over the
871 size = gst_collect_pads_available (pads);
873 GST_LOG_OBJECT (adder,
874 "starting to cycle through channels, %d bytes available (bps = %d)", size,
880 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
881 GstCollectData *data;
889 data = (GstCollectData *) collected->data;
891 /* get a subbuffer of size bytes */
892 inbuf = gst_collect_pads_take_buffer (pads, data, size);
893 /* NULL means EOS or an empty buffer so we still need to flush in
894 * case of an empty buffer. */
896 GST_LOG_OBJECT (adder, "channel %p: no bytes available", data);
900 bytes = GST_BUFFER_DATA (inbuf);
901 len = GST_BUFFER_SIZE (inbuf);
903 if (outbuf == NULL) {
904 GST_LOG_OBJECT (adder, "channel %p: making output buffer of %d bytes",
907 /* first buffer, alloc size bytes. FIXME, we can easily subbuffer
908 * and _make_writable. */
909 outbuf = gst_buffer_new_and_alloc (size);
910 outbytes = GST_BUFFER_DATA (outbuf);
911 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
913 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
914 /* clear if we are only going to fill a partial buffer */
915 if (G_UNLIKELY (size > len))
916 memset (outbytes, 0, size);
918 GST_LOG_OBJECT (adder, "channel %p: copying %d bytes from data %p",
921 /* and copy the data into it */
922 memcpy (outbytes, bytes, len);
925 GST_LOG_OBJECT (adder, "channel %p: zeroing %d bytes from data %p",
927 memset (outbytes, 0, size);
930 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP)) {
931 GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
933 /* other buffers, need to add them */
934 adder->func ((gpointer) outbytes, (gpointer) bytes, len);
937 GST_LOG_OBJECT (adder, "channel %p: skipping %d bytes from data %p",
943 gst_buffer_unref (inbuf);
946 /* can only happen when no pads to collect or all EOS */
950 /* our timestamping is very simple, just an ever incrementing
951 * counter, the new segment time will take care of their respective
953 if (adder->segment_pending) {
956 /* FIXME, use rate/applied_rate as set on all sinkpads.
957 * - currently we just set rate as received from last seek-event
958 * We could potentially figure out the duration as well using
959 * the current segment positions and the stated stop positions.
960 * Also we just start from stream time 0 which is rather
961 * weird. For non-synchronized mixing, the time should be
962 * the min of the stream times of all received segments,
963 * rationale being that the duration is at least going to
964 * be as long as the earliest stream we start mixing. This
965 * would also be correct for synchronized mixing but then
966 * the later streams would be delayed until the stream times
969 event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
970 1.0, GST_FORMAT_TIME, adder->timestamp, -1, adder->segment_position);
972 gst_pad_push_event (adder->srcpad, event);
973 adder->segment_pending = FALSE;
974 adder->segment_position = 0;
977 /* set timestamps on the output buffer */
978 GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
979 GST_BUFFER_OFFSET (outbuf) = adder->offset;
981 /* for the next timestamp, use the sample counter, which will
982 * never accumulate rounding errors */
983 adder->offset += size / adder->bps;
984 adder->timestamp = gst_util_uint64_scale_int (adder->offset,
985 GST_SECOND, adder->rate);
987 /* now we can set the duration of the buffer */
988 GST_BUFFER_DURATION (outbuf) = adder->timestamp -
989 GST_BUFFER_TIMESTAMP (outbuf);
991 /* if we only processed silence, mark output again as silence */
993 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
996 GST_LOG_OBJECT (adder, "pushing outbuf, timestamp %" GST_TIME_FORMAT,
997 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
998 ret = gst_pad_push (adder->srcpad, outbuf);
1005 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1006 ("Unknown data received, not negotiated"));
1007 return GST_FLOW_NOT_NEGOTIATED;
1011 GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1012 gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1013 return GST_FLOW_UNEXPECTED;
1017 static GstStateChangeReturn
1018 gst_adder_change_state (GstElement * element, GstStateChange transition)
1022 GstStateChangeReturn ret;
1024 adder = GST_ADDER (element);
1026 switch (transition) {
1027 case GST_STATE_CHANGE_NULL_TO_READY:
1029 case GST_STATE_CHANGE_READY_TO_PAUSED:
1030 adder->timestamp = 0;
1032 adder->segment_pending = TRUE;
1033 adder->segment_position = 0;
1034 adder->segment_rate = 1.0;
1035 gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1036 gst_collect_pads_start (adder->collect);
1038 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1040 case GST_STATE_CHANGE_PAUSED_TO_READY:
1041 /* need to unblock the collectpads before calling the
1042 * parent change_state so that streaming can finish */
1043 gst_collect_pads_stop (adder->collect);
1049 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1051 switch (transition) {
1061 plugin_init (GstPlugin * plugin)
1063 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1070 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1073 "Adds multiple streams",
1074 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)