mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-27 12:11:13 +00:00
gst/adder/: Ported adder as an example of a mixer element using collect pads. Needs more negotiation work.
Original commit message from CVS: * gst/adder/Makefile.am: * gst/adder/gstadder.c: (gst_adder_setcaps), (gst_adder_class_init), (gst_adder_init), (gst_adder_request_new_pad), (gst_adder_collected), (gst_adder_change_state): * gst/adder/gstadder.h: Ported adder as an example of a mixer element using collect pads. Needs more negotiation work.
This commit is contained in:
parent
4a77aaddbc
commit
80d4778bd4
4 changed files with 224 additions and 293 deletions
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
||||||
|
2005-05-05 Wim Taymans <wim@fluendo.com>
|
||||||
|
|
||||||
|
* gst/adder/Makefile.am:
|
||||||
|
* gst/adder/gstadder.c: (gst_adder_setcaps),
|
||||||
|
(gst_adder_class_init), (gst_adder_init),
|
||||||
|
(gst_adder_request_new_pad), (gst_adder_collected),
|
||||||
|
(gst_adder_change_state):
|
||||||
|
* gst/adder/gstadder.h:
|
||||||
|
Ported adder as an example of a mixer element using
|
||||||
|
collect pads. Needs more negotiation work.
|
||||||
|
|
||||||
2005-05-05 Wim Taymans <wim@fluendo.com>
|
2005-05-05 Wim Taymans <wim@fluendo.com>
|
||||||
|
|
||||||
* ext/theora/theoradec.c: (_inc_granulepos),
|
* ext/theora/theoradec.c: (_inc_granulepos),
|
||||||
|
|
|
@ -4,6 +4,6 @@ plugin_LTLIBRARIES = libgstadder.la
|
||||||
libgstadder_la_SOURCES = gstadder.c
|
libgstadder_la_SOURCES = gstadder.c
|
||||||
libgstadder_la_CFLAGS = $(GST_CFLAGS)
|
libgstadder_la_CFLAGS = $(GST_CFLAGS)
|
||||||
libgstadder_la_LIBADD =
|
libgstadder_la_LIBADD =
|
||||||
libgstadder_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
|
libgstadder_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) $(GST_BASE_LIBS)
|
||||||
|
|
||||||
noinst_HEADERS = gstadder.h
|
noinst_HEADERS = gstadder.h
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/* GStreamer
|
/* GStreamer
|
||||||
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
||||||
* 2000 Wim Taymans <wim.taymans@chello.be>
|
|
||||||
* 2001 Thomas <thomas@apestaart.org>
|
* 2001 Thomas <thomas@apestaart.org>
|
||||||
|
* 2005 Wim Taymans <wim@fluendo.com>
|
||||||
*
|
*
|
||||||
* adder.c: Adder element, N in, one out, samples are added
|
* adder.c: Adder element, N in, one out, samples are added
|
||||||
*
|
*
|
||||||
|
@ -30,19 +30,19 @@
|
||||||
#include <string.h> /* strcmp */
|
#include <string.h> /* strcmp */
|
||||||
|
|
||||||
/* highest positive/lowest negative x-bit value we can use for clamping */
|
/* highest positive/lowest negative x-bit value we can use for clamping */
|
||||||
//#define MAX_INT_x ((guint) 1 << (x - 1))
|
#define MAX_INT_32 ((gint32) (0x7fffffff))
|
||||||
#define MAX_INT_32 2147483647L
|
#define MAX_INT_16 ((gint16) (0x7fff))
|
||||||
#define MAX_INT_16 32767
|
#define MAX_INT_8 ((gint8) (0x7f))
|
||||||
#define MAX_INT_8 127
|
#define MAX_UINT_32 ((guint32)(0xffffffff))
|
||||||
|
#define MAX_UINT_16 ((guint16)(0xffff))
|
||||||
|
#define MAX_UINT_8 ((guint8) (0xff))
|
||||||
|
|
||||||
//#define MIN_INT_x (-((guint) 1 << (x - 1)))
|
#define MIN_INT_32 ((gint32) (0x80000000))
|
||||||
/* to make non-C90 happy we need to specify the constant differently */
|
#define MIN_INT_16 ((gint16) (0x8000))
|
||||||
#define MIN_INT_32 (-2147483647L -1L)
|
#define MIN_INT_8 ((gint8) (0x80))
|
||||||
#define MIN_INT_16 -32768
|
#define MIN_UINT_32 ((guint32)(0x00000000))
|
||||||
#define MIN_INT_8 -128
|
#define MIN_UINT_16 ((guint16)(0x0000))
|
||||||
|
#define MIN_UINT_8 ((guint8) (0x00))
|
||||||
#define GST_ADDER_BUFFER_SIZE 4096
|
|
||||||
#define GST_ADDER_NUM_BUFFERS 8
|
|
||||||
|
|
||||||
GST_DEBUG_CATEGORY_STATIC (gst_adder_debug);
|
GST_DEBUG_CATEGORY_STATIC (gst_adder_debug);
|
||||||
#define GST_CAT_DEFAULT gst_adder_debug
|
#define GST_CAT_DEFAULT gst_adder_debug
|
||||||
|
@ -63,7 +63,6 @@ enum
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
ARG_0,
|
ARG_0,
|
||||||
ARG_NUM_PADS
|
|
||||||
/* FILL ME */
|
/* FILL ME */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -86,15 +85,14 @@ static GstStaticPadTemplate gst_adder_sink_template =
|
||||||
static void gst_adder_class_init (GstAdderClass * klass);
|
static void gst_adder_class_init (GstAdderClass * klass);
|
||||||
static void gst_adder_init (GstAdder * adder);
|
static void gst_adder_init (GstAdder * adder);
|
||||||
|
|
||||||
static void gst_adder_get_property (GObject * object, guint prop_id,
|
|
||||||
GValue * value, GParamSpec * pspec);
|
|
||||||
|
|
||||||
static GstPad *gst_adder_request_new_pad (GstElement * element,
|
static GstPad *gst_adder_request_new_pad (GstElement * element,
|
||||||
GstPadTemplate * temp, const gchar * unused);
|
GstPadTemplate * temp, const gchar * unused);
|
||||||
static GstElementStateReturn gst_adder_change_state (GstElement * element);
|
static GstElementStateReturn gst_adder_change_state (GstElement * element);
|
||||||
|
|
||||||
/* we do need a loop function */
|
static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
|
||||||
static void gst_adder_loop (GstElement * element);
|
|
||||||
|
static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
|
||||||
|
gpointer user_data);
|
||||||
|
|
||||||
static GstElementClass *parent_class = NULL;
|
static GstElementClass *parent_class = NULL;
|
||||||
|
|
||||||
|
@ -121,49 +119,45 @@ gst_adder_get_type (void)
|
||||||
return adder_type;
|
return adder_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstPadLinkReturn
|
#define MAKE_FUNC(name,type,ttype,min,max) \
|
||||||
gst_adder_link (GstPad * pad, const GstCaps * caps)
|
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)
|
||||||
{
|
{
|
||||||
GstAdder *adder;
|
GstAdder *adder;
|
||||||
const char *media_type;
|
GList *pads;
|
||||||
const GList *pads;
|
|
||||||
GstStructure *structure;
|
GstStructure *structure;
|
||||||
GstPadLinkReturn ret;
|
const char *media_type;
|
||||||
GstElement *element;
|
|
||||||
|
|
||||||
g_return_val_if_fail (caps != NULL, GST_PAD_LINK_REFUSED);
|
adder = GST_ADDER (GST_PAD_PARENT (pad));
|
||||||
g_return_val_if_fail (pad != NULL, GST_PAD_LINK_REFUSED);
|
|
||||||
|
|
||||||
element = GST_PAD_PARENT (pad);
|
/* see if the other pads can accept the format */
|
||||||
adder = GST_ADDER (element);
|
GST_LOCK (adder);
|
||||||
|
pads = GST_ELEMENT (adder)->pads;
|
||||||
pads = gst_element_get_pad_list (element);
|
|
||||||
while (pads) {
|
while (pads) {
|
||||||
GstPad *otherpad = GST_PAD (pads->data);
|
GstPad *otherpad = GST_PAD (pads->data);
|
||||||
|
|
||||||
if (otherpad != pad) {
|
if (otherpad != pad) {
|
||||||
ret = gst_pad_try_set_caps (otherpad, caps);
|
gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
|
||||||
if (GST_PAD_LINK_FAILED (ret)) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pads = g_list_next (pads);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pads = gst_element_get_pad_list (GST_ELEMENT (adder));
|
|
||||||
while (pads) {
|
|
||||||
GstPad *otherpad = GST_PAD (pads->data);
|
|
||||||
|
|
||||||
if (otherpad != pad) {
|
|
||||||
ret = gst_pad_try_set_caps (otherpad, caps);
|
|
||||||
if (GST_PAD_LINK_FAILED (ret)) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pads = g_list_next (pads);
|
pads = g_list_next (pads);
|
||||||
}
|
}
|
||||||
|
GST_UNLOCK (adder);
|
||||||
|
|
||||||
|
/* parse caps now */
|
||||||
structure = gst_caps_get_structure (caps, 0);
|
structure = gst_caps_get_structure (caps, 0);
|
||||||
media_type = gst_structure_get_name (structure);
|
media_type = gst_structure_get_name (structure);
|
||||||
if (strcmp (media_type, "audio/x-raw-int") == 0) {
|
if (strcmp (media_type, "audio/x-raw-int") == 0) {
|
||||||
|
@ -173,17 +167,54 @@ gst_adder_link (GstPad * pad, const GstCaps * caps)
|
||||||
gst_structure_get_int (structure, "depth", &adder->depth);
|
gst_structure_get_int (structure, "depth", &adder->depth);
|
||||||
gst_structure_get_int (structure, "endianness", &adder->endianness);
|
gst_structure_get_int (structure, "endianness", &adder->endianness);
|
||||||
gst_structure_get_boolean (structure, "signed", &adder->is_signed);
|
gst_structure_get_boolean (structure, "signed", &adder->is_signed);
|
||||||
gst_structure_get_int (structure, "channels", &adder->channels);
|
|
||||||
gst_structure_get_int (structure, "rate", &adder->rate);
|
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;
|
||||||
|
}
|
||||||
} else if (strcmp (media_type, "audio/x-raw-float") == 0) {
|
} else if (strcmp (media_type, "audio/x-raw-float") == 0) {
|
||||||
GST_DEBUG ("parse_caps sets adder to format float");
|
GST_DEBUG ("parse_caps sets adder to format float");
|
||||||
adder->format = GST_ADDER_FORMAT_FLOAT;
|
adder->format = GST_ADDER_FORMAT_FLOAT;
|
||||||
gst_structure_get_int (structure, "width", &adder->width);
|
gst_structure_get_int (structure, "width", &adder->width);
|
||||||
gst_structure_get_int (structure, "channels", &adder->channels);
|
|
||||||
gst_structure_get_int (structure, "rate", &adder->rate);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
return GST_PAD_LINK_OK;
|
gst_structure_get_int (structure, "channels", &adder->channels);
|
||||||
|
gst_structure_get_int (structure, "rate", &adder->rate);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
not_supported:
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -203,12 +234,6 @@ gst_adder_class_init (GstAdderClass * klass)
|
||||||
|
|
||||||
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
||||||
|
|
||||||
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
|
|
||||||
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;
|
|
||||||
|
|
||||||
gstelement_class->request_new_pad = gst_adder_request_new_pad;
|
gstelement_class->request_new_pad = gst_adder_request_new_pad;
|
||||||
gstelement_class->change_state = gst_adder_change_state;
|
gstelement_class->change_state = gst_adder_change_state;
|
||||||
}
|
}
|
||||||
|
@ -219,17 +244,17 @@ gst_adder_init (GstAdder * adder)
|
||||||
adder->srcpad =
|
adder->srcpad =
|
||||||
gst_pad_new_from_template (gst_static_pad_template_get
|
gst_pad_new_from_template (gst_static_pad_template_get
|
||||||
(&gst_adder_src_template), "src");
|
(&gst_adder_src_template), "src");
|
||||||
gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
|
|
||||||
gst_element_set_loop_function (GST_ELEMENT (adder), gst_adder_loop);
|
|
||||||
gst_pad_set_getcaps_function (adder->srcpad, gst_pad_proxy_getcaps);
|
gst_pad_set_getcaps_function (adder->srcpad, gst_pad_proxy_getcaps);
|
||||||
gst_pad_set_link_function (adder->srcpad, gst_adder_link);
|
gst_pad_set_setcaps_function (adder->srcpad, gst_adder_setcaps);
|
||||||
|
gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
|
||||||
|
|
||||||
adder->format = GST_ADDER_FORMAT_UNSET;
|
adder->format = GST_ADDER_FORMAT_UNSET;
|
||||||
|
adder->numpads = 0;
|
||||||
|
adder->func = NULL;
|
||||||
|
|
||||||
/* keep track of the sinkpads requested */
|
/* keep track of the sinkpads requested */
|
||||||
|
adder->collect = gst_collectpads_new ();
|
||||||
adder->numsinkpads = 0;
|
gst_collectpads_set_function (adder->collect, gst_adder_collected, adder);
|
||||||
adder->input_channels = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstPad *
|
static GstPad *
|
||||||
|
@ -238,279 +263,180 @@ gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
|
||||||
{
|
{
|
||||||
gchar *name;
|
gchar *name;
|
||||||
GstAdder *adder;
|
GstAdder *adder;
|
||||||
GstAdderInputChannel *input;
|
GstPad *newpad;
|
||||||
|
|
||||||
g_return_val_if_fail (GST_IS_ADDER (element), NULL);
|
g_return_val_if_fail (GST_IS_ADDER (element), NULL);
|
||||||
|
|
||||||
if (templ->direction != GST_PAD_SINK) {
|
if (templ->direction != GST_PAD_SINK)
|
||||||
g_warning ("gstadder: request new pad that is not a SINK pad\n");
|
goto not_sink;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* allocate space for the input_channel */
|
|
||||||
|
|
||||||
input = (GstAdderInputChannel *) g_malloc (sizeof (GstAdderInputChannel));
|
|
||||||
if (input == NULL) {
|
|
||||||
g_warning ("gstadder: could not allocate adder input channel !\n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
adder = GST_ADDER (element);
|
adder = GST_ADDER (element);
|
||||||
|
|
||||||
/* fill in input_channel structure */
|
name = g_strdup_printf ("sink%d", adder->numpads);
|
||||||
|
newpad = gst_pad_new_from_template (templ, name);
|
||||||
|
|
||||||
name = g_strdup_printf ("sink%d", adder->numsinkpads);
|
gst_pad_set_getcaps_function (newpad, gst_pad_proxy_getcaps);
|
||||||
input->sinkpad = gst_pad_new_from_template (templ, name);
|
gst_pad_set_setcaps_function (newpad, gst_adder_setcaps);
|
||||||
input->bytestream = gst_bytestream_new (input->sinkpad);
|
gst_collectpads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
|
||||||
|
if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
|
||||||
|
goto could_not_add;
|
||||||
|
|
||||||
gst_element_add_pad (GST_ELEMENT (adder), input->sinkpad);
|
adder->numpads++;
|
||||||
gst_pad_set_getcaps_function (input->sinkpad, gst_pad_proxy_getcaps);
|
|
||||||
gst_pad_set_link_function (input->sinkpad, gst_adder_link);
|
|
||||||
|
|
||||||
/* add the input_channel to the list of input channels */
|
return newpad;
|
||||||
|
|
||||||
adder->input_channels = g_slist_append (adder->input_channels, input);
|
/* errors */
|
||||||
adder->numsinkpads++;
|
not_sink:
|
||||||
|
{
|
||||||
return input->sinkpad;
|
g_warning ("gstadder: request new pad that is not a SINK pad\n");
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
static void
|
could_not_add:
|
||||||
gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
|
{
|
||||||
GParamSpec * pspec)
|
gst_collectpads_remove_pad (adder->collect, newpad);
|
||||||
{
|
gst_object_unref (GST_OBJECT (newpad));
|
||||||
GstAdder *adder;
|
return NULL;
|
||||||
|
|
||||||
/* 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:
|
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* use this loop */
|
static GstFlowReturn
|
||||||
static void
|
gst_adder_collected (GstCollectPads * pads, gpointer user_data)
|
||||||
gst_adder_loop (GstElement * element)
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* combine channels by adding sample values
|
* combine channels by adding sample values
|
||||||
* basic algorithm :
|
* basic algorithm :
|
||||||
* - request an output buffer from the pool
|
* - this function is called when all pads have a buffer
|
||||||
* - repeat for each input pipe :
|
* - get available bytes on all pads.
|
||||||
* - get number of bytes from the channel's bytestream to fill output buffer
|
* - repeat for each input pad :
|
||||||
|
* - read available bytes, copy or add to target buffer
|
||||||
* - if there's an EOS event, remove the input channel
|
* - if there's an EOS event, remove the input channel
|
||||||
* - otherwise add the gotten bytes to the output buffer
|
|
||||||
* - push out the output buffer
|
* - push out the output buffer
|
||||||
*/
|
*/
|
||||||
GstAdder *adder;
|
GstAdder *adder;
|
||||||
GstBuffer *buf_out;
|
guint size;
|
||||||
|
GSList *collected;
|
||||||
|
GstBuffer *outbuf;
|
||||||
|
GstFlowReturn ret;
|
||||||
|
gpointer outbytes;
|
||||||
|
|
||||||
GSList *inputs;
|
adder = GST_ADDER (user_data);
|
||||||
|
|
||||||
register guint i;
|
/* get available bytes for reading */
|
||||||
|
size = gst_collectpads_available (pads);
|
||||||
|
if (size == 0)
|
||||||
|
return GST_FLOW_OK;
|
||||||
|
|
||||||
g_return_if_fail (element != NULL);
|
outbuf = NULL;
|
||||||
g_return_if_fail (GST_IS_ADDER (element));
|
outbytes = NULL;
|
||||||
|
|
||||||
adder = GST_ADDER (element);
|
if (adder->func == NULL)
|
||||||
|
goto not_negotiated;
|
||||||
|
|
||||||
/* get new output buffer */
|
GST_LOG ("starting to cycle through channels, collecting %d bytes", size);
|
||||||
/* FIXME the 1024 is arbitrary */
|
|
||||||
buf_out = gst_buffer_new_and_alloc (1024);
|
|
||||||
|
|
||||||
if (buf_out == NULL) {
|
for (collected = pads->data; collected; collected = g_slist_next (collected)) {
|
||||||
GST_ELEMENT_ERROR (adder, CORE, TOO_LAZY, (NULL),
|
GstCollectData *data;
|
||||||
("could not get new output buffer"));
|
guint8 *bytes;
|
||||||
return;
|
guint len;
|
||||||
}
|
|
||||||
|
|
||||||
/* initialize the output data to 0 */
|
data = (GstCollectData *) collected->data;
|
||||||
memset (GST_BUFFER_DATA (buf_out), 0, GST_BUFFER_SIZE (buf_out));
|
|
||||||
|
|
||||||
/* get data from all of the sinks */
|
GST_LOG_OBJECT (adder, "looking into channel %p", data);
|
||||||
inputs = adder->input_channels;
|
|
||||||
|
|
||||||
GST_LOG ("starting to cycle through channels");
|
/* get pointer to copy size bytes */
|
||||||
|
len = gst_collectpads_read (pads, data, &bytes, size);
|
||||||
while (inputs) {
|
if (len == 0)
|
||||||
guint32 got_bytes;
|
|
||||||
guint8 *raw_in;
|
|
||||||
GstAdderInputChannel *input;
|
|
||||||
|
|
||||||
input = (GstAdderInputChannel *) inputs->data;
|
|
||||||
|
|
||||||
inputs = inputs->next;
|
|
||||||
|
|
||||||
GST_LOG_OBJECT (adder, " looking into channel %p", input);
|
|
||||||
|
|
||||||
if (!GST_PAD_IS_USABLE (input->sinkpad)) {
|
|
||||||
GST_LOG_OBJECT (adder, " adder ignoring pad %s:%s",
|
|
||||||
GST_DEBUG_PAD_NAME (input->sinkpad));
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
/* Get data from the bytestream of each input channel. We need to check for
|
|
||||||
events before passing on the data to the output buffer. */
|
|
||||||
repeat:
|
|
||||||
got_bytes = gst_bytestream_peek_bytes (input->bytestream, &raw_in,
|
|
||||||
GST_BUFFER_SIZE (buf_out));
|
|
||||||
|
|
||||||
/* FIXME we should do something with the data if got_bytes > 0 */
|
|
||||||
if (got_bytes < GST_BUFFER_SIZE (buf_out)) {
|
|
||||||
GstEvent *event = NULL;
|
|
||||||
guint32 waiting;
|
|
||||||
|
|
||||||
/* we need to check for an event. */
|
|
||||||
gst_bytestream_get_status (input->bytestream, &waiting, &event);
|
|
||||||
|
|
||||||
if (event) {
|
|
||||||
switch (GST_EVENT_TYPE (event)) {
|
|
||||||
case GST_EVENT_EOS:
|
|
||||||
/* if we get an EOS event from one of our sink pads, we assume that
|
|
||||||
pad's finished handling data. just skip this pad. */
|
|
||||||
GST_DEBUG (" got an EOS event");
|
|
||||||
gst_event_unref (event);
|
|
||||||
continue;
|
|
||||||
case GST_EVENT_INTERRUPT:
|
|
||||||
gst_event_unref (event);
|
|
||||||
GST_DEBUG (" got an interrupt event");
|
|
||||||
/* we have to call interrupt here, the scheduler will switch out
|
|
||||||
this element ASAP or returns TRUE if we need to exit the loop */
|
|
||||||
if (gst_element_interrupt (GST_ELEMENT (adder))) {
|
|
||||||
gst_buffer_unref (buf_out);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
GST_LOG_OBJECT (adder, "pulling again after event");
|
|
||||||
goto repeat;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* here's where the data gets copied. */
|
|
||||||
|
|
||||||
GST_LOG (" copying %d bytes (format %d,%d)",
|
GST_LOG (" copying %d bytes (format %d,%d)",
|
||||||
GST_BUFFER_SIZE (buf_out), adder->format, adder->width);
|
len, adder->format, adder->width);
|
||||||
GST_LOG (" from channel %p from input data %p", input, raw_in);
|
GST_LOG (" from channel %p from input data %p", data, bytes);
|
||||||
GST_LOG (" to output data %p in buffer %p",
|
|
||||||
GST_BUFFER_DATA (buf_out), buf_out);
|
|
||||||
|
|
||||||
if (adder->format == GST_ADDER_FORMAT_INT) {
|
if (outbuf == NULL) {
|
||||||
if (adder->width == 32) {
|
/* first buffer, alloc size bytes */
|
||||||
gint32 *in = (gint32 *) raw_in;
|
outbuf = gst_buffer_new_and_alloc (size);
|
||||||
gint32 *out = (gint32 *) GST_BUFFER_DATA (buf_out);
|
gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
|
||||||
|
outbytes = GST_BUFFER_DATA (outbuf);
|
||||||
|
|
||||||
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / 4; i++)
|
memset (outbytes, 0, size);
|
||||||
out[i] = CLAMP (((gint64) out[i]) + ((gint64) in[i]),
|
|
||||||
MIN_INT_32, MAX_INT_32);
|
|
||||||
} else 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++)
|
/* and copy the data into it */
|
||||||
out[i] = CLAMP (out[i] + in[i], MIN_INT_16, MAX_INT_16);
|
memcpy (outbytes, bytes, len);
|
||||||
} 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], MIN_INT_8, MAX_INT_8);
|
|
||||||
} else {
|
} else {
|
||||||
GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
|
/* other buffers, need to add them */
|
||||||
("invalid width (%u) for integer audio in gstadder",
|
adder->func ((gpointer) outbytes, (gpointer) bytes, len);
|
||||||
adder->width));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else if (adder->format == GST_ADDER_FORMAT_FLOAT) {
|
gst_collectpads_flush (pads, data, len);
|
||||||
if (adder->width == 64) {
|
|
||||||
gdouble *in = (gdouble *) raw_in;
|
|
||||||
gdouble *out = (gdouble *) GST_BUFFER_DATA (buf_out);
|
|
||||||
|
|
||||||
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / sizeof (gdouble); i++)
|
|
||||||
out[i] = CLAMP (out[i] + in[i], -1.0, 1.0);
|
|
||||||
} else if (adder->width == 32) {
|
|
||||||
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] = CLAMP (out[i] + in[i], -1.0, 1.0);
|
|
||||||
} else {
|
|
||||||
GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
|
|
||||||
("invalid width (%u) for float audio in gstadder", adder->width));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
|
|
||||||
("invalid audio format (%d) in gstadder", adder->format));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gst_bytestream_flush (input->bytestream, GST_BUFFER_SIZE (buf_out));
|
/* set timestamps on the output buffer */
|
||||||
|
{
|
||||||
|
guint64 duration;
|
||||||
|
|
||||||
GST_LOG ("done copying data");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (adder->width == 0) {
|
|
||||||
GST_ELEMENT_ERROR (adder, CORE, NEGOTIATION, (NULL), ("width is 0"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (adder->channels == 0) {
|
|
||||||
GST_ELEMENT_ERROR (adder, CORE, NEGOTIATION, (NULL), ("channels is 0"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (adder->rate == 0) {
|
|
||||||
GST_ELEMENT_ERROR (adder, CORE, NEGOTIATION, (NULL), ("rate is 0"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GST_BUFFER_TIMESTAMP (buf_out) = adder->timestamp;
|
|
||||||
if (adder->format == GST_ADDER_FORMAT_FLOAT)
|
if (adder->format == GST_ADDER_FORMAT_FLOAT)
|
||||||
adder->offset += GST_BUFFER_SIZE (buf_out) / adder->width / adder->channels;
|
duration = size / adder->width / adder->channels;
|
||||||
else
|
else
|
||||||
adder->offset +=
|
duration = size * 8 / adder->width / adder->channels;
|
||||||
GST_BUFFER_SIZE (buf_out) * 8 / adder->width / adder->channels;
|
|
||||||
|
GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
|
||||||
|
GST_BUFFER_OFFSET (outbuf) = adder->offset;
|
||||||
|
GST_BUFFER_DURATION (outbuf) = duration;
|
||||||
|
|
||||||
|
adder->offset += duration;
|
||||||
adder->timestamp = adder->offset * GST_SECOND / adder->rate;
|
adder->timestamp = adder->offset * GST_SECOND / adder->rate;
|
||||||
|
}
|
||||||
|
|
||||||
/* send it out */
|
/* send it out */
|
||||||
|
GST_LOG ("pushing outbuf");
|
||||||
|
ret = gst_pad_push (adder->srcpad, outbuf);
|
||||||
|
|
||||||
GST_LOG ("pushing buf_out");
|
return ret;
|
||||||
gst_pad_push (adder->srcpad, GST_DATA (buf_out));
|
|
||||||
|
/* ERRORS */
|
||||||
|
not_negotiated:
|
||||||
|
{
|
||||||
|
return GST_FLOW_NOT_NEGOTIATED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static GstElementStateReturn
|
static GstElementStateReturn
|
||||||
gst_adder_change_state (GstElement * element)
|
gst_adder_change_state (GstElement * element)
|
||||||
{
|
{
|
||||||
GstAdder *adder;
|
GstAdder *adder;
|
||||||
|
GstElementStateReturn ret;
|
||||||
|
gint transition;
|
||||||
|
|
||||||
adder = GST_ADDER (element);
|
adder = GST_ADDER (element);
|
||||||
|
transition = GST_STATE_TRANSITION (element);
|
||||||
|
|
||||||
switch (GST_STATE_TRANSITION (element)) {
|
switch (transition) {
|
||||||
case GST_STATE_NULL_TO_READY:
|
case GST_STATE_NULL_TO_READY:
|
||||||
break;
|
break;
|
||||||
case GST_STATE_READY_TO_PAUSED:
|
case GST_STATE_READY_TO_PAUSED:
|
||||||
adder->timestamp = 0;
|
adder->timestamp = 0;
|
||||||
adder->offset = 0;
|
adder->offset = 0;
|
||||||
|
gst_collectpads_start (adder->collect);
|
||||||
break;
|
break;
|
||||||
case GST_STATE_PAUSED_TO_PLAYING:
|
case GST_STATE_PAUSED_TO_PLAYING:
|
||||||
case GST_STATE_PLAYING_TO_PAUSED:
|
break;
|
||||||
case GST_STATE_PAUSED_TO_READY:
|
|
||||||
case GST_STATE_READY_TO_NULL:
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
||||||
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
||||||
|
|
||||||
return GST_STATE_SUCCESS;
|
switch (transition) {
|
||||||
|
case GST_STATE_PLAYING_TO_PAUSED:
|
||||||
|
break;
|
||||||
|
case GST_STATE_PAUSED_TO_READY:
|
||||||
|
gst_collectpads_stop (adder->collect);
|
||||||
|
break;
|
||||||
|
case GST_STATE_READY_TO_NULL:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,11 +24,9 @@
|
||||||
#define __GST_ADDER_H__
|
#define __GST_ADDER_H__
|
||||||
|
|
||||||
#include <gst/gst.h>
|
#include <gst/gst.h>
|
||||||
#include <gst/bytestream/bytestream.h>
|
#include <gst/base/gstcollectpads.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
G_BEGIN_DECLS
|
||||||
extern "C" {
|
|
||||||
#endif /* __cplusplus */
|
|
||||||
|
|
||||||
extern GstElementDetails gst_adder_details;
|
extern GstElementDetails gst_adder_details;
|
||||||
|
|
||||||
|
@ -54,19 +52,14 @@ enum _GstAdderFormat {
|
||||||
GST_ADDER_FORMAT_FLOAT
|
GST_ADDER_FORMAT_FLOAT
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GstAdderInputChannel {
|
typedef void (*GstAdderFunction) (gpointer out, gpointer in, guint size);
|
||||||
GstPad *sinkpad;
|
|
||||||
GstByteStream *bytestream;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct _GstAdder {
|
struct _GstAdder {
|
||||||
GstElement element;
|
GstElement element;
|
||||||
|
|
||||||
GstPad *srcpad;
|
GstPad *srcpad;
|
||||||
|
GstCollectPads *collect;
|
||||||
/* keep track of the sinkpads */
|
gint numpads;
|
||||||
guint numsinkpads;
|
|
||||||
GSList *input_channels;
|
|
||||||
|
|
||||||
/* the next are valid for both int and float */
|
/* the next are valid for both int and float */
|
||||||
GstAdderFormat format;
|
GstAdderFormat format;
|
||||||
|
@ -79,6 +72,9 @@ struct _GstAdder {
|
||||||
guint depth;
|
guint depth;
|
||||||
gboolean is_signed;
|
gboolean is_signed;
|
||||||
|
|
||||||
|
/* function to add samples */
|
||||||
|
GstAdderFunction func;
|
||||||
|
|
||||||
/* counters to keep track of timestamps */
|
/* counters to keep track of timestamps */
|
||||||
gint64 timestamp;
|
gint64 timestamp;
|
||||||
gint64 offset;
|
gint64 offset;
|
||||||
|
@ -91,9 +87,7 @@ struct _GstAdderClass {
|
||||||
GType gst_adder_get_type (void);
|
GType gst_adder_get_type (void);
|
||||||
gboolean gst_adder_factory_init (GstElementFactory *factory);
|
gboolean gst_adder_factory_init (GstElementFactory *factory);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
G_END_DECLS
|
||||||
}
|
|
||||||
#endif /* __cplusplus */
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* __GST_ADDER_H__ */
|
#endif /* __GST_ADDER_H__ */
|
||||||
|
|
Loading…
Reference in a new issue