2008-04-10 07:01:16 +00:00
|
|
|
/*
|
|
|
|
* Siren Encoder Gst Element
|
|
|
|
*
|
|
|
|
* @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2009-02-10 19:20:26 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-sirenenc
|
|
|
|
*
|
|
|
|
* This encodes audio buffers into the Siren 16 codec (a 16khz extension of
|
|
|
|
* G.722.1) that is meant to be compatible with the Microsoft Windows Live
|
|
|
|
* Messenger(tm) implementation.
|
|
|
|
*
|
|
|
|
* Ref: http://www.polycom.com/company/about_us/technology/siren_g7221/index.html
|
|
|
|
*/
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstsirenenc.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (sirenenc_debug);
|
|
|
|
#define GST_CAT_DEFAULT (sirenenc_debug)
|
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
#define FRAME_DURATION (20 * GST_MSECOND)
|
|
|
|
|
2008-04-10 07:01:16 +00:00
|
|
|
/* elementfactory information */
|
|
|
|
static const GstElementDetails gst_siren_enc_details =
|
2009-02-10 19:20:19 +00:00
|
|
|
GST_ELEMENT_DETAILS ("Siren Encoder element",
|
|
|
|
"Codec/Encoder/Audio ",
|
|
|
|
"Encode 16bit PCM streams into the Siren7 codec",
|
|
|
|
"Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2009-02-10 19:20:19 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
|
|
|
"width = (int) 16, "
|
|
|
|
"depth = (int) 16, "
|
|
|
|
"endianness = (int) 1234, "
|
|
|
|
"signed = (boolean) true, "
|
2009-02-10 19:20:19 +00:00
|
|
|
"rate = (int) 16000, " "channels = (int) 1"));
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
/* signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
static void gst_siren_enc_finalize (GObject * object);
|
|
|
|
|
|
|
|
static gboolean gst_siren_enc_sink_setcaps (GstPad * pad, GstCaps * caps);
|
|
|
|
static gboolean gst_siren_enc_sink_event (GstPad * pad, GstEvent * event);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2009-02-10 19:20:19 +00:00
|
|
|
static GstFlowReturn gst_siren_enc_chain (GstPad * pad, GstBuffer * buf);
|
2008-07-04 19:51:02 +00:00
|
|
|
static GstStateChangeReturn
|
2009-02-10 19:20:19 +00:00
|
|
|
gst_siren_change_state (GstElement * element, GstStateChange transition);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
_do_init (GType type)
|
|
|
|
{
|
2009-02-10 19:20:19 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (sirenenc_debug, "sirenenc", 0, "sirenenc");
|
2008-04-10 07:01:16 +00:00
|
|
|
}
|
|
|
|
|
2008-07-04 19:51:02 +00:00
|
|
|
GST_BOILERPLATE_FULL (GstSirenEnc, gst_siren_enc, GstElement,
|
|
|
|
GST_TYPE_ELEMENT, _do_init);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_siren_enc_base_init (gpointer klass)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&srctemplate));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sinktemplate));
|
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &gst_siren_enc_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-10 19:20:19 +00:00
|
|
|
gst_siren_enc_class_init (GstSirenEncClass * klass)
|
2008-04-10 07:01:16 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2008-07-04 19:51:02 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2008-07-04 19:51:02 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("Initializing Class");
|
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_siren_enc_finalize);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2008-07-04 19:51:02 +00:00
|
|
|
gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_siren_change_state);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("Class Init done");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-10 19:20:19 +00:00
|
|
|
gst_siren_enc_init (GstSirenEnc * enc, GstSirenEncClass * klass)
|
2008-04-10 07:01:16 +00:00
|
|
|
{
|
|
|
|
|
2008-07-04 20:18:30 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "Initializing");
|
2008-07-04 19:51:02 +00:00
|
|
|
enc->encoder = Siren7_NewEncoder (16000);
|
2008-04-10 07:01:16 +00:00
|
|
|
enc->adapter = gst_adapter_new ();
|
|
|
|
|
2008-07-04 19:51:02 +00:00
|
|
|
enc->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
|
|
|
|
enc->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
|
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
gst_pad_set_setcaps_function (enc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_siren_enc_sink_setcaps));
|
|
|
|
gst_pad_set_event_function (enc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_siren_enc_sink_event));
|
2008-07-04 19:51:02 +00:00
|
|
|
gst_pad_set_chain_function (enc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_siren_enc_chain));
|
|
|
|
|
|
|
|
gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
|
|
|
|
|
2008-07-04 20:18:30 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "Init done");
|
2008-04-10 07:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-09-03 17:04:33 +00:00
|
|
|
gst_siren_enc_finalize (GObject * object)
|
2008-04-10 07:01:16 +00:00
|
|
|
{
|
|
|
|
GstSirenEnc *enc = GST_SIREN_ENC (object);
|
|
|
|
|
2008-07-04 20:18:30 +00:00
|
|
|
GST_DEBUG_OBJECT (object, "Disposing");
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
Siren7_CloseEncoder (enc->encoder);
|
|
|
|
g_object_unref (enc->adapter);
|
2008-07-04 20:17:44 +00:00
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
static gboolean
|
|
|
|
gst_siren_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstSirenEnc *enc;
|
|
|
|
gboolean res;
|
|
|
|
GstCaps *outcaps;
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
|
|
|
|
|
|
|
|
outcaps = gst_static_pad_template_get_caps (&srctemplate);
|
|
|
|
res = gst_pad_set_caps (enc->srcpad, outcaps);
|
|
|
|
gst_caps_unref (outcaps);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_siren_enc_sink_event (GstPad * pad, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstSirenEnc *enc;
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
gst_adapter_clear (enc->adapter);
|
|
|
|
res = gst_pad_push_event (enc->srcpad, event);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
gst_adapter_clear (enc->adapter);
|
|
|
|
res = gst_pad_push_event (enc->srcpad, event);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = gst_pad_push_event (enc->srcpad, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
2008-04-10 07:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2009-02-10 19:20:19 +00:00
|
|
|
gst_siren_enc_chain (GstPad * pad, GstBuffer * buf)
|
2008-04-10 07:01:16 +00:00
|
|
|
{
|
2008-07-04 19:51:02 +00:00
|
|
|
GstSirenEnc *enc = GST_SIREN_ENC (gst_pad_get_parent_element (pad));
|
2008-04-10 07:01:16 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2009-09-03 16:43:26 +00:00
|
|
|
GstBuffer *out_buf;
|
|
|
|
guint8 *in_data, *out_data;
|
|
|
|
guint8 *to_free = NULL;
|
|
|
|
guint i, size, num_frames;
|
|
|
|
gint out_size, in_size;
|
|
|
|
gint encode_ret;
|
|
|
|
gboolean discont;
|
|
|
|
GstClockTime timestamp;
|
|
|
|
guint64 distance;
|
|
|
|
|
|
|
|
discont = GST_BUFFER_IS_DISCONT (buf);
|
|
|
|
if (discont) {
|
|
|
|
GST_DEBUG_OBJECT (enc, "received DISCONT, flush adapter");
|
|
|
|
gst_adapter_clear (enc->adapter);
|
|
|
|
enc->discont = TRUE;
|
|
|
|
}
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2008-07-04 19:51:02 +00:00
|
|
|
gst_adapter_push (enc->adapter, buf);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2008-07-04 20:18:30 +00:00
|
|
|
GST_LOG_OBJECT (enc, "Received buffer of size %d with adapter of size : %d",
|
2008-07-04 19:51:02 +00:00
|
|
|
GST_BUFFER_SIZE (buf), gst_adapter_available (enc->adapter));
|
|
|
|
|
|
|
|
size = gst_adapter_available (enc->adapter);
|
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
/* we need to process 640 input bytes to produce 40 output bytes */
|
|
|
|
/* calculate the amount of frames we will handle */
|
|
|
|
num_frames = size / 640;
|
2008-07-04 22:17:43 +00:00
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
/* no frames, wait some more */
|
|
|
|
if (num_frames == 0)
|
|
|
|
goto done;
|
2008-07-04 22:17:43 +00:00
|
|
|
|
2009-09-03 17:04:33 +00:00
|
|
|
/* this is the input/output size */
|
2009-09-03 16:43:26 +00:00
|
|
|
in_size = num_frames * 640;
|
|
|
|
out_size = num_frames * 40;
|
2008-07-04 19:51:02 +00:00
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
GST_LOG_OBJECT (enc, "we have %u frames, %u in, %u out", num_frames, in_size,
|
|
|
|
out_size);
|
2008-07-04 19:51:02 +00:00
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
/* get a buffer */
|
|
|
|
ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad, -1,
|
2009-09-03 17:04:33 +00:00
|
|
|
out_size, GST_PAD_CAPS (enc->srcpad), &out_buf);
|
2008-07-04 22:17:43 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
2009-09-03 16:43:26 +00:00
|
|
|
goto alloc_failed;
|
|
|
|
|
|
|
|
/* get the timestamp for the output buffer */
|
|
|
|
timestamp = gst_adapter_prev_timestamp (enc->adapter, &distance);
|
|
|
|
|
|
|
|
/* add the amount of time taken by the distance */
|
|
|
|
timestamp += gst_util_uint64_scale_int (distance / 2, GST_SECOND, 16000);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (enc,
|
|
|
|
"timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
|
|
|
|
GST_TIME_ARGS (timestamp), distance);
|
|
|
|
|
|
|
|
/* get the input data for all the frames */
|
|
|
|
to_free = in_data = gst_adapter_take (enc->adapter, in_size);
|
|
|
|
out_data = GST_BUFFER_DATA (out_buf);
|
|
|
|
|
|
|
|
for (i = 0; i < num_frames; i++) {
|
|
|
|
GST_LOG_OBJECT (enc, "Encoding frame %u/%u", i, num_frames);
|
|
|
|
|
|
|
|
/* encode 640 input bytes to 40 output bytes */
|
|
|
|
encode_ret = Siren7_EncodeFrame (enc->encoder, in_data, out_data);
|
|
|
|
if (encode_ret != 0)
|
|
|
|
goto encode_error;
|
|
|
|
|
|
|
|
/* move to next frame */
|
|
|
|
out_data += 40;
|
|
|
|
in_data += 640;
|
2008-04-10 07:01:16 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
GST_LOG_OBJECT (enc, "Finished encoding");
|
|
|
|
|
|
|
|
/* mark discont */
|
|
|
|
if (enc->discont) {
|
|
|
|
GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
enc->discont = FALSE;
|
|
|
|
}
|
|
|
|
GST_BUFFER_TIMESTAMP (out_buf) = timestamp;
|
|
|
|
GST_BUFFER_DURATION (out_buf) = num_frames * FRAME_DURATION;
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
ret = gst_pad_push (enc->srcpad, out_buf);
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
done:
|
|
|
|
if (to_free)
|
|
|
|
g_free (to_free);
|
2008-07-04 20:04:54 +00:00
|
|
|
|
2008-07-04 22:17:43 +00:00
|
|
|
gst_object_unref (enc);
|
2009-09-03 16:43:26 +00:00
|
|
|
|
2008-04-10 07:01:16 +00:00
|
|
|
return ret;
|
2009-09-03 16:43:26 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
alloc_failed:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (enc, "failed to pad_alloc buffer: %d (%d)", ret,
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
encode_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
|
|
|
|
("Error encoding frame: %d", encode_ret));
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
gst_buffer_unref (out_buf);
|
|
|
|
goto done;
|
|
|
|
}
|
2008-04-10 07:01:16 +00:00
|
|
|
}
|
|
|
|
|
2008-07-04 19:51:02 +00:00
|
|
|
static GstStateChangeReturn
|
2009-02-10 19:20:19 +00:00
|
|
|
gst_siren_change_state (GstElement * element, GstStateChange transition)
|
2008-07-04 19:51:02 +00:00
|
|
|
{
|
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
|
|
|
GstSirenEnc *enc = GST_SIREN_ENC (element);
|
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
enc->discont = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-07-04 19:51:02 +00:00
|
|
|
|
2009-09-03 16:43:26 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
2008-07-04 19:51:02 +00:00
|
|
|
|
2009-02-10 19:20:19 +00:00
|
|
|
switch (transition) {
|
2008-07-04 19:51:02 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
gst_adapter_clear (enc->adapter);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-04-10 07:01:16 +00:00
|
|
|
|
2008-07-04 19:51:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2008-04-10 07:01:16 +00:00
|
|
|
|
|
|
|
gboolean
|
2009-02-10 19:20:19 +00:00
|
|
|
gst_siren_enc_plugin_init (GstPlugin * plugin)
|
2008-04-10 07:01:16 +00:00
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "sirenenc",
|
|
|
|
GST_RANK_MARGINAL, GST_TYPE_SIREN_ENC);
|
|
|
|
}
|