2001-12-22 22:43:48 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wim.taymans@chello.be>
|
|
|
|
* 2001 Thomas <thomas@apestaart.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gstadder.h"
|
2002-09-09 09:26:23 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2002-04-06 03:40:13 +00:00
|
|
|
#include <string.h> /* strcmp */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
#define GST_ADDER_BUFFER_SIZE 4096
|
|
|
|
#define GST_ADDER_NUM_BUFFERS 8
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-18 19:02:52 +00:00
|
|
|
/* elementfactory information */
|
2001-12-22 22:43:48 +00:00
|
|
|
GstElementDetails adder_details = {
|
|
|
|
"Adder",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Filter/Audio",
|
2002-09-18 19:02:52 +00:00
|
|
|
"LGPL",
|
2002-07-15 11:03:24 +00:00
|
|
|
"N-to-1 audio adder/mixer",
|
2001-12-22 22:43:48 +00:00
|
|
|
VERSION,
|
|
|
|
"Thomas <thomas@apestaart.org>",
|
2002-09-09 09:26:23 +00:00
|
|
|
"(C) 2001, 2002",
|
2001-12-22 22:43:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Adder signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_NUM_PADS,
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
2002-04-11 20:42:27 +00:00
|
|
|
GST_PAD_TEMPLATE_FACTORY (gst_adder_src_template_factory,
|
2001-12-22 22:43:48 +00:00
|
|
|
"src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_caps_new ("int_src", "audio/raw",
|
|
|
|
GST_AUDIO_INT_PAD_TEMPLATE_PROPS),
|
|
|
|
gst_caps_new ("float_src", "audio/raw",
|
|
|
|
GST_AUDIO_FLOAT_MONO_PAD_TEMPLATE_PROPS)
|
2001-12-22 22:43:48 +00:00
|
|
|
);
|
|
|
|
|
2002-04-11 20:42:27 +00:00
|
|
|
GST_PAD_TEMPLATE_FACTORY (gst_adder_sink_template_factory,
|
2001-12-22 22:43:48 +00:00
|
|
|
"sink%d",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_caps_new ("int_sink", "audio/raw",
|
|
|
|
GST_AUDIO_INT_PAD_TEMPLATE_PROPS),
|
|
|
|
gst_caps_new ("float_sink", "audio/raw",
|
|
|
|
GST_AUDIO_FLOAT_MONO_PAD_TEMPLATE_PROPS)
|
2001-12-22 22:43:48 +00:00
|
|
|
);
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
static void gst_adder_class_init (GstAdderClass *klass);
|
|
|
|
static void gst_adder_init (GstAdder *adder);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
static void gst_adder_get_property (GObject *object, guint prop_id,
|
|
|
|
GValue *value, GParamSpec *pspec);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
static GstPad* gst_adder_request_new_pad (GstElement *element, GstPadTemplate *temp,
|
|
|
|
const gchar *unused);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
/* we do need a loop function */
|
2002-09-23 09:39:33 +00:00
|
|
|
static void gst_adder_loop (GstElement *element);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2002-01-22 04:45:10 +00:00
|
|
|
/* static guint gst_adder_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
GType
|
2002-09-09 09:26:23 +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
|
|
|
};
|
2002-09-09 09:26:23 +00:00
|
|
|
adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
|
|
|
|
&adder_info, 0);
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
return adder_type;
|
|
|
|
}
|
|
|
|
|
2002-02-19 05:59:06 +00:00
|
|
|
static gboolean
|
|
|
|
gst_adder_parse_caps (GstAdder *adder, GstCaps *caps)
|
2002-01-22 04:45:10 +00:00
|
|
|
{
|
2002-03-30 17:06:26 +00:00
|
|
|
const gchar *format;
|
2002-09-09 09:26:23 +00:00
|
|
|
GstElement *el = GST_ELEMENT (adder);
|
2002-03-30 17:06:26 +00:00
|
|
|
|
|
|
|
gst_caps_get_string (caps, "format", &format);
|
2002-01-22 04:45:10 +00:00
|
|
|
|
|
|
|
if (adder->format == GST_ADDER_FORMAT_UNSET) {
|
2002-09-23 09:39:33 +00:00
|
|
|
/* the caps haven't been set yet at all,
|
|
|
|
* so we need to go ahead and set all
|
2002-01-22 04:45:10 +00:00
|
|
|
the relevant values. */
|
|
|
|
if (strcmp (format, "int") == 0) {
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "parse_caps sets adder to format int");
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_INT;
|
2002-03-30 17:06:26 +00:00
|
|
|
gst_caps_get_int (caps, "width", &adder->width);
|
|
|
|
gst_caps_get_int (caps, "depth", &adder->depth);
|
|
|
|
gst_caps_get_int (caps, "law", &adder->law);
|
|
|
|
gst_caps_get_int (caps, "endianness", &adder->endianness);
|
|
|
|
gst_caps_get_boolean (caps, "signed", &adder->is_signed);
|
|
|
|
gst_caps_get_int (caps, "channels", &adder->channels);
|
2002-05-27 04:48:57 +00:00
|
|
|
gst_caps_get_int (caps, "rate", &adder->rate);
|
2002-01-22 04:45:10 +00:00
|
|
|
} else if (strcmp (format, "float") == 0) {
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "parse_caps sets adder to format float");
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_FLOAT;
|
2002-03-30 17:06:26 +00:00
|
|
|
gst_caps_get_string (caps, "layout", &adder->layout);
|
|
|
|
gst_caps_get_float (caps, "intercept", &adder->intercept);
|
|
|
|
gst_caps_get_float (caps, "slope", &adder->slope);
|
|
|
|
gst_caps_get_int (caps, "channels", &adder->channels);
|
2002-05-27 04:48:57 +00:00
|
|
|
gst_caps_get_int (caps, "rate", &adder->rate);
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* otherwise, a previously-connected pad has set all the values. we should
|
|
|
|
barf if some of the attempted new values don't match. */
|
|
|
|
if (strcmp (format, "int") == 0) {
|
2002-05-27 04:48:57 +00:00
|
|
|
gint width, channels, rate;
|
2002-03-30 17:06:26 +00:00
|
|
|
gboolean is_signed;
|
|
|
|
|
|
|
|
gst_caps_get_int (caps, "width", &width);
|
|
|
|
gst_caps_get_int (caps, "channels", &channels);
|
|
|
|
gst_caps_get_boolean (caps, "signed", &is_signed);
|
2002-05-27 04:48:57 +00:00
|
|
|
gst_caps_get_int (caps, "rate", &rate);
|
2002-03-30 17:06:26 +00:00
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
/* provide an error message if we can't connect */
|
|
|
|
if (adder->format != GST_ADDER_FORMAT_INT) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el, "can't connect a non-int pad to an int adder");
|
|
|
|
return FALSE;
|
2002-09-09 09:26:23 +00:00
|
|
|
}
|
|
|
|
if (adder->channels != channels) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el,
|
|
|
|
"can't connect %d-channel pad with %d-channel adder",
|
|
|
|
channels, adder->channels);
|
|
|
|
return FALSE;
|
2002-09-09 09:26:23 +00:00
|
|
|
}
|
|
|
|
if (adder->rate != rate) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el, "can't connect %d Hz pad with %d Hz adder",
|
|
|
|
rate, adder->rate);
|
|
|
|
return FALSE;
|
2002-09-09 09:26:23 +00:00
|
|
|
}
|
|
|
|
if (adder->width != width) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el, "can't connect %d-bit pad with %d-bit adder",
|
|
|
|
width, adder->width);
|
|
|
|
return FALSE;
|
2002-09-09 09:26:23 +00:00
|
|
|
}
|
|
|
|
if (adder->is_signed != is_signed) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el,
|
|
|
|
"can't connect %ssigned pad with %ssigned adder",
|
|
|
|
adder->is_signed ? "" : "un",
|
|
|
|
is_signed ? "" : "un");
|
|
|
|
return FALSE;
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
} else if (strcmp (format, "float") == 0) {
|
2002-05-27 04:48:57 +00:00
|
|
|
gint channels, rate;
|
2002-03-30 17:06:26 +00:00
|
|
|
|
|
|
|
gst_caps_get_int (caps, "channels", &channels);
|
2002-05-27 04:48:57 +00:00
|
|
|
gst_caps_get_int (caps, "rate", &rate);
|
2002-03-30 17:06:26 +00:00
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
if (adder->format != GST_ADDER_FORMAT_FLOAT) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el,
|
2002-09-09 09:26:23 +00:00
|
|
|
"can't connect a non-float pad to a float adder");
|
2002-09-23 09:39:33 +00:00
|
|
|
return FALSE;
|
2002-09-09 09:26:23 +00:00
|
|
|
}
|
|
|
|
if (adder->channels != channels) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el,
|
|
|
|
"can't connect %d-channel pad with %d-channel adder",
|
|
|
|
channels, adder->channels);
|
|
|
|
return FALSE;
|
2002-09-09 09:26:23 +00:00
|
|
|
}
|
|
|
|
if (adder->rate != rate) {
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (el, "can't connect %d Hz pad with %d Hz adder",
|
|
|
|
rate, adder->rate);
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
/* whoa, we don't know what's trying to connect with us ! barf ! */
|
|
|
|
return FALSE;
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-19 05:59:06 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstPadConnectReturn
|
|
|
|
gst_adder_connect (GstPad *pad, GstCaps *caps)
|
|
|
|
{
|
|
|
|
GstAdder *adder;
|
2002-09-12 20:53:56 +00:00
|
|
|
const GList *sinkpads;
|
|
|
|
GList *remove = NULL;
|
2002-03-03 00:53:25 +00:00
|
|
|
GSList *channels;
|
2002-02-19 05:59:06 +00:00
|
|
|
GstPad *p;
|
|
|
|
|
|
|
|
g_return_val_if_fail (caps != NULL, GST_PAD_CONNECT_REFUSED);
|
|
|
|
g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_REFUSED);
|
|
|
|
|
|
|
|
adder = GST_ADDER (GST_PAD_PARENT (pad));
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2002-02-19 05:59:06 +00:00
|
|
|
if (GST_CAPS_IS_FIXED (caps)) {
|
|
|
|
if (!gst_adder_parse_caps (adder, caps))
|
|
|
|
return GST_PAD_CONNECT_REFUSED;
|
|
|
|
|
2002-09-10 09:31:40 +00:00
|
|
|
if (pad == adder->srcpad || gst_pad_try_set_caps (adder->srcpad, caps) > 0) {
|
2002-09-23 09:39:33 +00:00
|
|
|
sinkpads = gst_element_get_pad_list ((GstElement *) adder);
|
2002-02-19 05:59:06 +00:00
|
|
|
while (sinkpads) {
|
2002-09-23 09:39:33 +00:00
|
|
|
p = (GstPad *) sinkpads->data;
|
2002-02-19 05:59:06 +00:00
|
|
|
if (p != pad && p != adder->srcpad) {
|
2002-09-10 09:31:40 +00:00
|
|
|
if (gst_pad_try_set_caps (p, caps) <= 0) {
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO,
|
|
|
|
"caps mismatch; disconnecting and removing pad %s:%s "
|
|
|
|
"(peer %s:%s)",
|
|
|
|
GST_DEBUG_PAD_NAME (p),
|
|
|
|
GST_DEBUG_PAD_NAME (GST_PAD_PEER (p)));
|
2002-02-19 05:59:06 +00:00
|
|
|
gst_pad_disconnect (GST_PAD (GST_PAD_PEER (p)), p);
|
2002-03-03 00:53:25 +00:00
|
|
|
remove = g_list_prepend (remove, p);
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sinkpads = g_list_next (sinkpads);
|
|
|
|
}
|
2002-03-03 00:53:25 +00:00
|
|
|
while (remove) {
|
2002-09-09 09:26:23 +00:00
|
|
|
gst_element_remove_pad (GST_ELEMENT (adder),
|
|
|
|
GST_PAD_CAST (remove->data));
|
2002-03-03 00:53:25 +00:00
|
|
|
restart:
|
|
|
|
channels = adder->input_channels;
|
|
|
|
while (channels) {
|
2002-09-23 09:39:33 +00:00
|
|
|
GstAdderInputChannel *channel;
|
|
|
|
channel = (GstAdderInputChannel*) channels->data;
|
2002-03-03 00:53:25 +00:00
|
|
|
if (channel->sinkpad == GST_PAD_CAST (remove->data)) {
|
|
|
|
gst_bytestream_destroy (channel->bytestream);
|
2002-09-23 09:39:33 +00:00
|
|
|
adder->input_channels = g_slist_remove_link (adder->input_channels,
|
2002-09-09 09:26:23 +00:00
|
|
|
channels);
|
2002-03-03 21:30:14 +00:00
|
|
|
adder->numsinkpads--;
|
2002-03-03 00:53:25 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
channels = g_slist_next (channels);
|
|
|
|
}
|
|
|
|
remove = g_list_next (remove);
|
|
|
|
}
|
2002-02-17 17:34:23 +00:00
|
|
|
return GST_PAD_CONNECT_OK;
|
2002-02-19 05:59:06 +00:00
|
|
|
} else {
|
2002-02-17 17:34:23 +00:00
|
|
|
return GST_PAD_CONNECT_REFUSED;
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2002-02-17 17:34:23 +00:00
|
|
|
return GST_PAD_CONNECT_DELAYED;
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
static void
|
|
|
|
gst_adder_class_init (GstAdderClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2002-09-09 09:26:23 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
|
2002-09-23 09:39:33 +00:00
|
|
|
g_param_spec_int ("num_pads","number of pads","Number Of Pads",
|
|
|
|
0, G_MAXINT, 0, G_PARAM_READABLE));
|
|
|
|
|
|
|
|
gobject_class->get_property = gst_adder_get_property;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
gstelement_class->request_new_pad = gst_adder_request_new_pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_adder_init (GstAdder *adder)
|
|
|
|
{
|
2002-09-09 09:26:23 +00:00
|
|
|
adder->srcpad = gst_pad_new_from_template (gst_adder_src_template_factory (),
|
|
|
|
"src");
|
2001-12-22 22:43:48 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
|
|
|
|
gst_element_set_loop_function (GST_ELEMENT (adder), gst_adder_loop);
|
2002-02-19 05:59:06 +00:00
|
|
|
gst_pad_set_connect_function (adder->srcpad, gst_adder_connect);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_UNSET;
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
/* keep track of the sinkpads requested */
|
|
|
|
|
|
|
|
adder->numsinkpads = 0;
|
|
|
|
adder->input_channels = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstPad*
|
2002-09-09 09:26:23 +00:00
|
|
|
gst_adder_request_new_pad (GstElement *element, GstPadTemplate *templ,
|
|
|
|
const gchar *unused)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2002-01-22 04:45:10 +00:00
|
|
|
gchar *name;
|
|
|
|
GstAdder *adder;
|
|
|
|
GstAdderInputChannel *input;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_ADDER (element), NULL);
|
|
|
|
|
|
|
|
if (templ->direction != GST_PAD_SINK) {
|
|
|
|
g_warning ("gstadder: request new pad that is not a SINK pad\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate space for the input_channel */
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
input = (GstAdderInputChannel *) g_malloc (sizeof (GstAdderInputChannel));
|
|
|
|
if (input == NULL) {
|
2002-09-23 09:39:33 +00:00
|
|
|
g_warning ("gstadder: could not allocate adder input channel !\n");
|
2002-01-22 04:45:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
adder = GST_ADDER (element);
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
/* fill in input_channel structure */
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
name = g_strdup_printf ("sink%d", adder->numsinkpads);
|
|
|
|
input->sinkpad = gst_pad_new_from_template (templ, name);
|
|
|
|
input->bytestream = gst_bytestream_new (input->sinkpad);
|
|
|
|
|
|
|
|
gst_element_add_pad (GST_ELEMENT (adder), input->sinkpad);
|
2002-02-19 05:59:06 +00:00
|
|
|
gst_pad_set_connect_function(input->sinkpad, gst_adder_connect);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
/* add the input_channel to the list of input channels */
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->input_channels = g_slist_append (adder->input_channels, input);
|
2001-12-22 22:43:48 +00:00
|
|
|
adder->numsinkpads++;
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
return input->sinkpad;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_adder_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
|
|
|
GstAdder *adder;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_ADDER (object));
|
|
|
|
|
|
|
|
adder = GST_ADDER (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_NUM_PADS:
|
|
|
|
g_value_set_int (value, adder->numsinkpads);
|
|
|
|
break;
|
|
|
|
default:
|
2002-04-11 20:42:27 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-12-22 22:43:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use this loop */
|
|
|
|
static void
|
|
|
|
gst_adder_loop (GstElement *element)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* combine channels by adding sample values
|
|
|
|
* basic algorithm :
|
2002-01-22 04:45:10 +00:00
|
|
|
* - request an output buffer from the pool
|
2001-12-22 22:43:48 +00:00
|
|
|
* - repeat for each input pipe :
|
2002-01-22 04:45:10 +00:00
|
|
|
* - get number of bytes from the channel's bytestream to fill output buffer
|
|
|
|
* - if there's an EOS event, remove the input channel
|
|
|
|
* - otherwise add the gotten bytes to the output buffer
|
2001-12-22 22:43:48 +00:00
|
|
|
* - push out the output buffer
|
|
|
|
*/
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
GstAdder *adder;
|
|
|
|
GstBuffer *buf_out;
|
|
|
|
GstEvent *event = NULL;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
GSList *inputs;
|
|
|
|
GstAdderInputChannel *input;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-05-15 19:03:59 +00:00
|
|
|
guint8 *raw_in;
|
2002-01-22 04:45:10 +00:00
|
|
|
guint32 waiting;
|
2002-05-15 19:03:59 +00:00
|
|
|
guint32 got_bytes;
|
2002-05-27 04:48:57 +00:00
|
|
|
gint64 timestamp = 0;
|
|
|
|
gint64 offset = 0;
|
2002-01-22 04:45:10 +00:00
|
|
|
register guint i;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
g_return_if_fail (element != NULL);
|
|
|
|
g_return_if_fail (GST_IS_ADDER (element));
|
|
|
|
|
|
|
|
adder = GST_ADDER (element);
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->bufpool = gst_pad_get_bufferpool (adder->srcpad);
|
|
|
|
if (adder->bufpool == NULL) {
|
2002-09-09 09:26:23 +00:00
|
|
|
adder->bufpool = gst_buffer_pool_get_default (GST_ADDER_BUFFER_SIZE,
|
|
|
|
GST_ADDER_NUM_BUFFERS);
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* get new output buffer */
|
|
|
|
buf_out = gst_buffer_new_from_pool (adder->bufpool, 0, 0);
|
|
|
|
|
|
|
|
if (buf_out == NULL)
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_error (GST_ELEMENT (adder),
|
|
|
|
"could not get new output buffer !\n");
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
/* initialize the output data to 0 */
|
2002-09-23 09:39:33 +00:00
|
|
|
memset (GST_BUFFER_DATA (buf_out), 0, GST_BUFFER_SIZE (buf_out));
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
/* get data from all of the sinks */
|
|
|
|
inputs = adder->input_channels;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "starting to cycle through channels");
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
while (inputs) {
|
|
|
|
input = (GstAdderInputChannel *) inputs->data;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "looking into channel %p", input);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-08-26 15:49:34 +00:00
|
|
|
if (!GST_PAD_IS_USABLE (input->sinkpad)) {
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "adder ignoring pad %s:%s",
|
|
|
|
GST_DEBUG_PAD_NAME (input->sinkpad));
|
2002-07-26 18:55:43 +00:00
|
|
|
inputs = inputs->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
/* Get data from the bytestream of each input channel.
|
2002-09-23 09:39:33 +00:00
|
|
|
* We need to check for events before passing on the data
|
|
|
|
* to the output buffer. */
|
|
|
|
got_bytes = gst_bytestream_peek_bytes (input->bytestream, &raw_in,
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_BUFFER_SIZE (buf_out));
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
/* FIXME we should do something with the data if
|
|
|
|
* got_bytes is more than zero */
|
|
|
|
if (got_bytes < GST_BUFFER_SIZE(buf_out)) {
|
2002-01-22 04:45:10 +00:00
|
|
|
/* we need to check for an event. */
|
|
|
|
gst_bytestream_get_status (input->bytestream, &waiting, &event);
|
|
|
|
|
|
|
|
if (event) {
|
2002-09-09 09:26:23 +00:00
|
|
|
if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
|
2002-01-22 04:45:10 +00:00
|
|
|
/* if we get an EOS event from one of our sink pads, we assume that
|
|
|
|
pad's finished handling data. delete the bytestream, free up the
|
|
|
|
pad, and free up the memory associated with the input channel. */
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "got an EOS event");
|
2002-01-22 04:45:10 +00:00
|
|
|
|
|
|
|
gst_bytestream_destroy (input->bytestream);
|
2002-09-09 09:26:23 +00:00
|
|
|
/* gst_object_unref (GST_OBJECT (input->sinkpad));
|
|
|
|
* this causes problems */
|
2002-01-22 04:45:10 +00:00
|
|
|
g_free (input);
|
|
|
|
|
|
|
|
adder->input_channels = g_slist_delete_link (inputs, inputs);
|
|
|
|
inputs = adder->input_channels;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* here's where the data gets copied. this is a little nasty looking
|
|
|
|
because it's the same code pretty much 3 times, except each time uses
|
|
|
|
different data types and clamp limits. */
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO,
|
|
|
|
"copying %d bytes from channel %p to output data %p "
|
|
|
|
"in buffer %p",
|
|
|
|
GST_BUFFER_SIZE (buf_out), input,
|
|
|
|
GST_BUFFER_DATA (buf_out), buf_out);
|
2002-01-22 04:45:10 +00:00
|
|
|
|
|
|
|
if (adder->format == GST_ADDER_FORMAT_INT) {
|
|
|
|
if (adder->width == 16) {
|
|
|
|
gint16 *in = (gint16 *) raw_in;
|
|
|
|
gint16 *out = (gint16 *) GST_BUFFER_DATA (buf_out);
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / 2; i++)
|
|
|
|
out[i] = CLAMP(out[i] + in[i], -32768, 32767);
|
|
|
|
} else if (adder->width == 8) {
|
|
|
|
gint8 *in = (gint8 *) raw_in;
|
|
|
|
gint8 *out = (gint8 *) GST_BUFFER_DATA (buf_out);
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out); i++)
|
|
|
|
out[i] = CLAMP(out[i] + in[i], -128, 127);
|
|
|
|
} else {
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_ERROR (GST_ELEMENT (adder),
|
|
|
|
"invalid width (%d) for int format in gstadder\n",
|
|
|
|
adder->width);
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
} else if (adder->format == GST_ADDER_FORMAT_FLOAT) {
|
|
|
|
gfloat *in = (gfloat *) raw_in;
|
|
|
|
gfloat *out = (gfloat *) GST_BUFFER_DATA (buf_out);
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / sizeof (gfloat); i++)
|
|
|
|
out[i] += in[i];
|
|
|
|
} else {
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_ERROR (GST_ELEMENT (adder),
|
|
|
|
"invalid audio format (%d) in gstadder\n",
|
|
|
|
adder->format);
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_bytestream_flush (input->bytestream, GST_BUFFER_SIZE (buf_out));
|
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "done copying data");
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
inputs = g_slist_next (inputs);
|
2001-12-23 23:01:28 +00:00
|
|
|
}
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
if (adder->format == GST_ADDER_FORMAT_UNSET) {
|
|
|
|
GstCaps *caps =
|
|
|
|
gst_caps_new ("default_adder_caps",
|
|
|
|
"audio/raw",
|
|
|
|
gst_props_new ("format", GST_PROPS_STRING ("int"),
|
|
|
|
"width", GST_PROPS_INT (16),
|
|
|
|
"depth", GST_PROPS_INT (16),
|
|
|
|
"rate", GST_PROPS_INT (44100),
|
|
|
|
"channels", GST_PROPS_INT (2),
|
|
|
|
"law", GST_PROPS_INT (0),
|
|
|
|
"endianness", GST_PROPS_INT (G_BYTE_ORDER),
|
|
|
|
"signed", GST_PROPS_BOOLEAN (TRUE),
|
|
|
|
NULL));
|
|
|
|
|
|
|
|
if (gst_pad_try_set_caps (adder->srcpad, caps) < 0) {
|
|
|
|
gst_element_error (GST_ELEMENT (adder),
|
|
|
|
"Couldn't set the default caps, "
|
|
|
|
"use connect_filtered instead");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gst_adder_parse_caps (adder, caps);
|
|
|
|
}
|
|
|
|
|
2002-05-27 04:48:57 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buf_out) = timestamp;
|
|
|
|
if (adder->format == GST_ADDER_FORMAT_FLOAT)
|
|
|
|
offset += GST_BUFFER_SIZE (buf_out) / sizeof (gfloat) / adder->channels;
|
|
|
|
else
|
|
|
|
offset += GST_BUFFER_SIZE (buf_out) * 8 / adder->width / adder->channels;
|
|
|
|
timestamp = offset * GST_SECOND / adder->rate;
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
/* send it out */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
GST_DEBUG (GST_CAT_PLUGIN_INFO, "pushing buf_out");
|
2002-01-22 04:45:10 +00:00
|
|
|
gst_pad_push (adder->srcpad, buf_out);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-05-25 18:03:10 +00:00
|
|
|
/* give another element a chance to do something */
|
|
|
|
gst_element_yield (element);
|
2002-01-22 04:45:10 +00:00
|
|
|
} while (TRUE);
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
plugin_init (GModule *module, GstPlugin *plugin)
|
|
|
|
{
|
|
|
|
GstElementFactory *factory;
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
factory = gst_element_factory_new ("adder", GST_TYPE_ADDER, &adder_details);
|
|
|
|
g_return_val_if_fail (factory != NULL, FALSE);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-09-09 23:24:27 +00:00
|
|
|
if (! gst_library_load ("gstbytestream"))
|
2002-01-22 04:45:10 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
gst_element_factory_add_pad_template (factory,
|
|
|
|
GST_PAD_TEMPLATE_GET (gst_adder_src_template_factory));
|
|
|
|
gst_element_factory_add_pad_template (factory,
|
|
|
|
GST_PAD_TEMPLATE_GET (gst_adder_sink_template_factory));
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstPluginDesc plugin_desc = {
|
|
|
|
GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"adder",
|
|
|
|
plugin_init
|
|
|
|
};
|
|
|
|
|