2001-12-22 22:43:48 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2001 Thomas <thomas@apestaart.org>
|
2005-05-05 09:46:03 +00:00
|
|
|
* 2005 Wim Taymans <wim@fluendo.com>
|
2001-12-22 22:43:48 +00:00
|
|
|
*
|
|
|
|
* adder.c: Adder element, N in, one out, samples are added
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2004-01-27 09:00:01 +00:00
|
|
|
/* Element-Checklist-Version: 5 */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-22 22:43:48 +00:00
|
|
|
#include "gstadder.h"
|
2002-09-09 09:26:23 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2004-03-15 19:32:28 +00:00
|
|
|
#include <string.h> /* strcmp */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
/* highest positive/lowest negative x-bit value we can use for clamping */
|
2005-05-05 09:46:03 +00:00
|
|
|
#define MAX_INT_32 ((gint32) (0x7fffffff))
|
|
|
|
#define MAX_INT_16 ((gint16) (0x7fff))
|
|
|
|
#define MAX_INT_8 ((gint8) (0x7f))
|
|
|
|
#define MAX_UINT_32 ((guint32)(0xffffffff))
|
|
|
|
#define MAX_UINT_16 ((guint16)(0xffff))
|
|
|
|
#define MAX_UINT_8 ((guint8) (0xff))
|
|
|
|
|
|
|
|
#define MIN_INT_32 ((gint32) (0x80000000))
|
|
|
|
#define MIN_INT_16 ((gint16) (0x8000))
|
|
|
|
#define MIN_INT_8 ((gint8) (0x80))
|
|
|
|
#define MIN_UINT_32 ((guint32)(0x00000000))
|
|
|
|
#define MIN_UINT_16 ((guint16)(0x0000))
|
|
|
|
#define MIN_UINT_8 ((guint8) (0x00))
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_adder_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_adder_debug
|
|
|
|
|
2002-09-18 19:02:52 +00:00
|
|
|
/* elementfactory information */
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstElementDetails adder_details = GST_ELEMENT_DETAILS ("Adder",
|
|
|
|
"Generic/Audio",
|
|
|
|
"Add N audio channels together",
|
|
|
|
"Thomas <thomas@apestaart.org>");
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_adder_src_template =
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
|
2004-03-14 22:34:34 +00:00
|
|
|
);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_adder_sink_template =
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink%d",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
|
|
|
GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
|
2004-03-14 22:34:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static void gst_adder_class_init (GstAdderClass * klass);
|
|
|
|
static void gst_adder_init (GstAdder * adder);
|
2005-11-16 18:21:46 +00:00
|
|
|
static void gst_adder_dispose (GObject * object);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
|
|
|
static GstPad *gst_adder_request_new_pad (GstElement * element,
|
|
|
|
GstPadTemplate * temp, const gchar * unused);
|
2005-09-02 15:43:18 +00:00
|
|
|
static GstStateChangeReturn gst_adder_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
|
|
|
|
|
|
|
|
static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
|
|
|
|
gpointer user_data);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
GType
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_get_type (void)
|
|
|
|
{
|
2001-12-22 22:43:48 +00:00
|
|
|
static GType adder_type = 0;
|
|
|
|
|
|
|
|
if (!adder_type) {
|
|
|
|
static const GTypeInfo adder_info = {
|
2002-09-09 09:26:23 +00:00
|
|
|
sizeof (GstAdderClass), NULL, NULL,
|
|
|
|
(GClassInitFunc) gst_adder_class_init, NULL, NULL,
|
2002-09-23 09:39:33 +00:00
|
|
|
sizeof (GstAdder), 0,
|
2002-09-09 09:26:23 +00:00
|
|
|
(GInstanceInitFunc) gst_adder_init,
|
2001-12-22 22:43:48 +00:00
|
|
|
};
|
2004-03-15 19:32:28 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
|
2004-03-15 19:32:28 +00:00
|
|
|
&adder_info, 0);
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_adder_debug, "adder", 0,
|
|
|
|
"audio channel mixing element");
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
return adder_type;
|
|
|
|
}
|
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
#define MAKE_FUNC(name,type,ttype,min,max) \
|
|
|
|
static void name (type *out, type *in, gint bytes) { \
|
|
|
|
gint i; \
|
|
|
|
for (i = 0; i < bytes / sizeof (type); i++) \
|
|
|
|
out[i] = CLAMP ((ttype)out[i] + (ttype)in[i], min, max); \
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_FUNC (add_int32, gint32, gint64, MIN_INT_32, MAX_INT_32)
|
|
|
|
MAKE_FUNC (add_int16, gint16, gint32, MIN_INT_16, MAX_INT_16)
|
|
|
|
MAKE_FUNC (add_int8, gint8, gint16, MIN_INT_8, MAX_INT_8)
|
|
|
|
MAKE_FUNC (add_uint32, guint32, guint64, MIN_UINT_32, MAX_UINT_32)
|
|
|
|
MAKE_FUNC (add_uint16, guint16, guint32, MIN_UINT_16, MAX_UINT_16)
|
|
|
|
MAKE_FUNC (add_uint8, guint8, guint16, MIN_UINT_8, MAX_UINT_8)
|
|
|
|
MAKE_FUNC (add_float64, gdouble, gdouble, -1.0, 1.0)
|
|
|
|
MAKE_FUNC (add_float32, gfloat, gfloat, -1.0, 1.0)
|
|
|
|
|
|
|
|
static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps)
|
2002-02-19 05:59:06 +00:00
|
|
|
{
|
|
|
|
GstAdder *adder;
|
2005-05-05 09:46:03 +00:00
|
|
|
GList *pads;
|
2004-01-27 09:00:01 +00:00
|
|
|
GstStructure *structure;
|
2005-05-05 09:46:03 +00:00
|
|
|
const char *media_type;
|
2004-01-22 03:25:16 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
adder = GST_ADDER (GST_PAD_PARENT (pad));
|
2004-01-22 03:25:16 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* see if the other pads can accept the format */
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_LOCK (adder);
|
2005-05-05 09:46:03 +00:00
|
|
|
pads = GST_ELEMENT (adder)->pads;
|
2004-01-27 09:00:01 +00:00
|
|
|
while (pads) {
|
|
|
|
GstPad *otherpad = GST_PAD (pads->data);
|
|
|
|
|
|
|
|
if (otherpad != pad) {
|
2005-05-05 09:46:03 +00:00
|
|
|
gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
2004-01-27 09:00:01 +00:00
|
|
|
pads = g_list_next (pads);
|
|
|
|
}
|
2005-11-21 16:35:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (adder);
|
2004-01-27 09:00:01 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* parse caps now */
|
2004-01-27 09:00:01 +00:00
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
media_type = gst_structure_get_name (structure);
|
|
|
|
if (strcmp (media_type, "audio/x-raw-int") == 0) {
|
2005-08-24 18:40:27 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format int");
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_INT;
|
|
|
|
gst_structure_get_int (structure, "width", &adder->width);
|
|
|
|
gst_structure_get_int (structure, "depth", &adder->depth);
|
|
|
|
gst_structure_get_int (structure, "endianness", &adder->endianness);
|
|
|
|
gst_structure_get_boolean (structure, "signed", &adder->is_signed);
|
2005-05-05 09:46:03 +00:00
|
|
|
|
|
|
|
if (adder->endianness != G_BYTE_ORDER)
|
|
|
|
goto not_supported;
|
|
|
|
|
|
|
|
switch (adder->width) {
|
|
|
|
case 8:
|
|
|
|
adder->func = (adder->is_signed ?
|
|
|
|
(GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
adder->func = (adder->is_signed ?
|
|
|
|
(GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
adder->func = (adder->is_signed ?
|
|
|
|
(GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto not_supported;
|
|
|
|
}
|
2004-01-27 09:00:01 +00:00
|
|
|
} else if (strcmp (media_type, "audio/x-raw-float") == 0) {
|
2005-08-24 18:40:27 +00:00
|
|
|
GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format float");
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_FLOAT;
|
|
|
|
gst_structure_get_int (structure, "width", &adder->width);
|
2005-05-05 09:46:03 +00:00
|
|
|
|
|
|
|
switch (adder->width) {
|
|
|
|
case 32:
|
|
|
|
adder->func = (GstAdderFunction) add_float32;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
adder->func = (GstAdderFunction) add_float64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto not_supported;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
goto not_supported;
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
gst_structure_get_int (structure, "channels", &adder->channels);
|
|
|
|
gst_structure_get_int (structure, "rate", &adder->rate);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
not_supported:
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
|
2005-10-09 08:56:54 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adder_query (GstPad * pad, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
|
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_POSITION:
|
|
|
|
{
|
|
|
|
GstFormat format;
|
|
|
|
|
Query API update.
Original commit message from CVS:
* examples/seeking/seek.c: (make_avi_msmpeg4v3_mp3_pipeline),
(query_positions_elems), (query_positions_pads), (update_scale),
(do_seek), (set_update_scale), (message_received), (main):
* ext/ogg/gstoggdemux.c: (gst_ogg_pad_src_query),
(gst_ogg_demux_perform_seek), (gst_ogg_demux_find_chains),
(gst_ogg_demux_loop):
* ext/ogg/gstogmparse.c: (gst_ogm_parse_sink_query):
* ext/theora/theoradec.c: (theora_dec_src_query),
(theora_dec_sink_event):
* ext/vorbis/vorbisdec.c: (vorbis_dec_src_query),
(vorbis_dec_sink_event), (vorbis_handle_data_packet):
* gst/adder/gstadder.c: (gst_adder_query):
* gst/audiotestsrc/gstaudiotestsrc.c: (gst_audiotestsrc_src_query):
* gst/playback/test3.c: (update_scale):
* gst/playback/test5.c: (new_pad), (no_more_pads), (start_finding),
(dump_element_stats), (main):
* gst/playback/test6.c: (main):
* gst/sine/gstsinesrc.c: (gst_sinesrc_src_query):
Query API update.
2005-10-19 15:55:33 +00:00
|
|
|
gst_query_parse_position (query, &format, NULL);
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case GST_FORMAT_TIME:
|
|
|
|
gst_query_set_position (query, GST_FORMAT_TIME, adder->timestamp);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
case GST_FORMAT_DEFAULT:
|
|
|
|
gst_query_set_position (query, GST_FORMAT_DEFAULT, adder->offset);
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2005-10-09 08:56:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
Query API update.
Original commit message from CVS:
* examples/seeking/seek.c: (make_avi_msmpeg4v3_mp3_pipeline),
(query_positions_elems), (query_positions_pads), (update_scale),
(do_seek), (set_update_scale), (message_received), (main):
* ext/ogg/gstoggdemux.c: (gst_ogg_pad_src_query),
(gst_ogg_demux_perform_seek), (gst_ogg_demux_find_chains),
(gst_ogg_demux_loop):
* ext/ogg/gstogmparse.c: (gst_ogm_parse_sink_query):
* ext/theora/theoradec.c: (theora_dec_src_query),
(theora_dec_sink_event):
* ext/vorbis/vorbisdec.c: (vorbis_dec_src_query),
(vorbis_dec_sink_event), (vorbis_handle_data_packet):
* gst/adder/gstadder.c: (gst_adder_query):
* gst/audiotestsrc/gstaudiotestsrc.c: (gst_audiotestsrc_src_query):
* gst/playback/test3.c: (update_scale):
* gst/playback/test5.c: (new_pad), (no_more_pads), (start_finding),
(dump_element_stats), (main):
* gst/playback/test6.c: (main):
* gst/sine/gstsinesrc.c: (gst_sinesrc_src_query):
Query API update.
2005-10-19 15:55:33 +00:00
|
|
|
/* FIXME: what to do about the length? query all pads upstream and
|
|
|
|
* pick the longest length? or the shortest length? or what? */
|
|
|
|
case GST_QUERY_DURATION:
|
|
|
|
break;
|
2005-10-09 08:56:54 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_unref (adder);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_class_init (GstAdderClass * klass)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2005-11-16 18:21:46 +00:00
|
|
|
|
|
|
|
gobject_class->dispose = gst_adder_dispose;
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2002-09-09 09:26:23 +00:00
|
|
|
|
2003-11-01 01:10:23 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_adder_src_template));
|
2003-11-01 01:10:23 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_adder_sink_template));
|
2003-11-01 01:10:23 +00:00
|
|
|
gst_element_class_set_details (gstelement_class, &adder_details);
|
|
|
|
|
2005-10-09 08:56:54 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
2002-09-09 09:26:23 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
gstelement_class->request_new_pad = gst_adder_request_new_pad;
|
2004-03-14 22:34:34 +00:00
|
|
|
gstelement_class->change_state = gst_adder_change_state;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static void
|
|
|
|
gst_adder_init (GstAdder * adder)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->srcpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_adder_src_template), "src");
|
2005-10-09 08:56:54 +00:00
|
|
|
gst_pad_set_getcaps_function (adder->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
|
|
|
|
gst_pad_set_setcaps_function (adder->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_adder_setcaps));
|
|
|
|
gst_pad_set_query_function (adder->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_adder_query));
|
2005-05-05 09:46:03 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_UNSET;
|
2005-05-05 09:46:03 +00:00
|
|
|
adder->numpads = 0;
|
|
|
|
adder->func = NULL;
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
/* keep track of the sinkpads requested */
|
2005-11-21 11:54:16 +00:00
|
|
|
adder->collect = gst_collect_pads_new ();
|
|
|
|
gst_collect_pads_set_function (adder->collect, gst_adder_collected, adder);
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 18:21:46 +00:00
|
|
|
static void
|
|
|
|
gst_adder_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstAdder *adder = GST_ADDER (object);
|
|
|
|
|
|
|
|
gst_object_unref (adder->collect);
|
|
|
|
adder->collect = NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstPad *
|
|
|
|
gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
|
|
|
|
const gchar * unused)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
gchar *name;
|
|
|
|
GstAdder *adder;
|
2005-05-05 09:46:03 +00:00
|
|
|
GstPad *newpad;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_ADDER (element), NULL);
|
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
if (templ->direction != GST_PAD_SINK)
|
|
|
|
goto not_sink;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder = GST_ADDER (element);
|
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
name = g_strdup_printf ("sink%d", adder->numpads);
|
|
|
|
newpad = gst_pad_new_from_template (templ, name);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-10-09 08:56:54 +00:00
|
|
|
gst_pad_set_getcaps_function (newpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
|
|
|
|
gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
|
2005-11-21 11:54:16 +00:00
|
|
|
gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
|
2005-05-05 09:46:03 +00:00
|
|
|
if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
|
|
|
|
goto could_not_add;
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
adder->numpads++;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
return newpad;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* errors */
|
|
|
|
not_sink:
|
|
|
|
{
|
|
|
|
g_warning ("gstadder: request new pad that is not a SINK pad\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
could_not_add:
|
|
|
|
{
|
2005-11-21 11:54:16 +00:00
|
|
|
gst_collect_pads_remove_pad (adder->collect, newpad);
|
2005-06-28 10:16:13 +00:00
|
|
|
gst_object_unref (newpad);
|
2005-05-05 09:46:03 +00:00
|
|
|
return NULL;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_adder_collected (GstCollectPads * pads, gpointer user_data)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* combine channels by adding sample values
|
|
|
|
* basic algorithm :
|
2005-05-05 09:46:03 +00:00
|
|
|
* - this function is called when all pads have a buffer
|
|
|
|
* - get available bytes on all pads.
|
|
|
|
* - repeat for each input pad :
|
|
|
|
* - read available bytes, copy or add to target buffer
|
2002-01-22 04:45:10 +00:00
|
|
|
* - if there's an EOS event, remove the input channel
|
2001-12-22 22:43:48 +00:00
|
|
|
* - push out the output buffer
|
|
|
|
*/
|
2004-03-14 22:34:34 +00:00
|
|
|
GstAdder *adder;
|
2005-05-05 09:46:03 +00:00
|
|
|
guint size;
|
|
|
|
GSList *collected;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
gpointer outbytes;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
adder = GST_ADDER (user_data);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* get available bytes for reading */
|
2005-11-21 11:54:16 +00:00
|
|
|
size = gst_collect_pads_available (pads);
|
2005-05-05 09:46:03 +00:00
|
|
|
if (size == 0)
|
|
|
|
return GST_FLOW_OK;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
outbuf = NULL;
|
|
|
|
outbytes = NULL;
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
if (adder->func == NULL)
|
|
|
|
goto not_negotiated;
|
2003-01-11 16:28:29 +00:00
|
|
|
|
2005-08-24 18:40:27 +00:00
|
|
|
GST_LOG_OBJECT (adder,
|
|
|
|
"starting to cycle through channels, collecting %d bytes", size);
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
for (collected = pads->data; collected; collected = g_slist_next (collected)) {
|
|
|
|
GstCollectData *data;
|
|
|
|
guint8 *bytes;
|
|
|
|
guint len;
|
2003-01-11 16:28:29 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
data = (GstCollectData *) collected->data;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
GST_LOG_OBJECT (adder, "looking into channel %p", data);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* get pointer to copy size bytes */
|
2005-11-21 11:54:16 +00:00
|
|
|
len = gst_collect_pads_read (pads, data, &bytes, size);
|
2005-05-05 09:46:03 +00:00
|
|
|
if (len == 0)
|
|
|
|
continue;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-08-24 18:40:27 +00:00
|
|
|
GST_LOG_OBJECT (adder, " copying %d bytes (format %d,%d)",
|
2005-05-05 09:46:03 +00:00
|
|
|
len, adder->format, adder->width);
|
2005-08-24 18:40:27 +00:00
|
|
|
GST_LOG_OBJECT (adder, " from channel %p from input data %p", data, bytes);
|
2003-01-11 16:28:29 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
if (outbuf == NULL) {
|
|
|
|
/* first buffer, alloc size bytes */
|
|
|
|
outbuf = gst_buffer_new_and_alloc (size);
|
|
|
|
gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
|
|
|
|
outbytes = GST_BUFFER_DATA (outbuf);
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
memset (outbytes, 0, size);
|
2002-07-26 18:55:43 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* and copy the data into it */
|
|
|
|
memcpy (outbytes, bytes, len);
|
2003-01-11 16:28:29 +00:00
|
|
|
} else {
|
2005-05-05 09:46:03 +00:00
|
|
|
/* other buffers, need to add them */
|
|
|
|
adder->func ((gpointer) outbytes, (gpointer) bytes, len);
|
2001-12-23 23:01:28 +00:00
|
|
|
}
|
2005-11-21 11:54:16 +00:00
|
|
|
gst_collect_pads_flush (pads, data, len);
|
2004-06-06 23:43:15 +00:00
|
|
|
}
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* set timestamps on the output buffer */
|
|
|
|
{
|
2005-10-27 09:54:06 +00:00
|
|
|
guint64 samples;
|
2005-05-05 09:46:03 +00:00
|
|
|
guint64 duration;
|
|
|
|
|
2005-09-28 18:59:19 +00:00
|
|
|
/* width is in bits and we need bytes */
|
2005-10-27 09:54:06 +00:00
|
|
|
samples = size / ((adder->width / 8) * adder->channels);
|
|
|
|
duration = samples * GST_SECOND / adder->rate;
|
2005-05-05 09:46:03 +00:00
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
|
|
|
|
GST_BUFFER_OFFSET (outbuf) = adder->offset;
|
|
|
|
|
2005-10-27 09:54:06 +00:00
|
|
|
adder->offset += samples;
|
2005-05-05 09:46:03 +00:00
|
|
|
adder->timestamp = adder->offset * GST_SECOND / adder->rate;
|
2005-10-27 09:54:06 +00:00
|
|
|
|
|
|
|
GST_BUFFER_DURATION (outbuf) = adder->timestamp -
|
|
|
|
GST_BUFFER_TIMESTAMP (outbuf);
|
2005-05-05 09:46:03 +00:00
|
|
|
}
|
2003-01-11 16:28:29 +00:00
|
|
|
|
|
|
|
/* send it out */
|
2005-08-24 18:40:27 +00:00
|
|
|
GST_LOG_OBJECT (adder, "pushing outbuf");
|
2005-05-05 09:46:03 +00:00
|
|
|
ret = gst_pad_push (adder->srcpad, outbuf);
|
2003-01-11 16:28:29 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
return ret;
|
2003-01-11 16:28:29 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
/* ERRORS */
|
|
|
|
not_negotiated:
|
|
|
|
{
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
}
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2005-09-02 15:43:18 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_adder_change_state (GstElement * element, GstStateChange transition)
|
2003-01-11 16:28:29 +00:00
|
|
|
{
|
|
|
|
GstAdder *adder;
|
2005-09-02 15:43:18 +00:00
|
|
|
GstStateChangeReturn ret;
|
2002-09-23 09:39:33 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
adder = GST_ADDER (element);
|
2002-05-27 04:48:57 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
switch (transition) {
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2003-01-11 16:28:29 +00:00
|
|
|
break;
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2003-01-11 16:28:29 +00:00
|
|
|
adder->timestamp = 0;
|
|
|
|
adder->offset = 0;
|
2005-11-21 11:54:16 +00:00
|
|
|
gst_collect_pads_start (adder->collect);
|
2003-01-11 16:28:29 +00:00
|
|
|
break;
|
2005-09-02 15:43:18 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
2005-05-05 09:46:03 +00:00
|
|
|
break;
|
2005-10-27 09:54:06 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
/* need to unblock the collectpads before calling the
|
|
|
|
* parent change_state so that streaming can finish */
|
2005-11-21 11:54:16 +00:00
|
|
|
gst_collect_pads_stop (adder->collect);
|
2005-10-27 09:54:06 +00:00
|
|
|
break;
|
2005-10-09 08:56:54 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
2003-01-11 16:28:29 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-08-01 21:14:22 +00:00
|
|
|
|
2005-05-05 09:46:03 +00:00
|
|
|
return ret;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2003-11-01 01:10:23 +00:00
|
|
|
if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"adder",
|
|
|
|
"Adds multiple streams",
|
2005-10-16 13:54:44 +00:00
|
|
|
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|