2007-08-26 21:59:05 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2010-01-02 09:08:17 +08:00
|
|
|
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
|
2007-08-26 21:59:05 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-05-19 22:59:30 +08:00
|
|
|
#include "gstpragma.h"
|
2007-10-19 05:47:53 +08:00
|
|
|
#include "gstsbcutil.h"
|
2008-12-23 12:25:50 +08:00
|
|
|
#include "gstsbcparse.h"
|
2007-08-26 21:59:05 +08:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC(sbc_parse_debug);
|
|
|
|
#define GST_CAT_DEFAULT sbc_parse_debug
|
|
|
|
|
|
|
|
GST_BOILERPLATE(GstSbcParse, gst_sbc_parse, GstElement, GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
static const GstElementDetails sbc_parse_details =
|
|
|
|
GST_ELEMENT_DETAILS("Bluetooth SBC parser",
|
|
|
|
"Codec/Parser/Audio",
|
|
|
|
"Parse a SBC audio stream",
|
|
|
|
"Marcel Holtmann <marcel@holtmann.org>");
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sbc_parse_sink_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
2008-01-30 22:21:43 +08:00
|
|
|
GST_STATIC_CAPS("audio/x-sbc,"
|
|
|
|
"parsed = (boolean) false"));
|
2007-08-26 21:59:05 +08:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sbc_parse_src_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS("audio/x-sbc, "
|
|
|
|
"rate = (int) { 16000, 32000, 44100, 48000 }, "
|
|
|
|
"channels = (int) [ 1, 2 ], "
|
2008-02-20 21:37:00 +08:00
|
|
|
"mode = (string) { \"mono\", \"dual\", \"stereo\", \"joint\" }, "
|
2007-08-26 21:59:05 +08:00
|
|
|
"blocks = (int) { 4, 8, 12, 16 }, "
|
|
|
|
"subbands = (int) { 4, 8 }, "
|
2008-02-20 21:37:00 +08:00
|
|
|
"allocation = (string) { \"snr\", \"loudness\" },"
|
2008-01-30 22:21:43 +08:00
|
|
|
"bitpool = (int) [ 2, 64 ],"
|
|
|
|
"parsed = (boolean) true"));
|
2007-10-19 05:47:53 +08:00
|
|
|
|
2007-08-26 21:59:05 +08:00
|
|
|
static GstFlowReturn sbc_parse_chain(GstPad *pad, GstBuffer *buffer)
|
|
|
|
{
|
|
|
|
GstSbcParse *parse = GST_SBC_PARSE(gst_pad_get_parent(pad));
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
guint size, offset = 0;
|
|
|
|
guint8 *data;
|
|
|
|
|
2008-01-31 01:30:27 +08:00
|
|
|
/* FIXME use a gstadpter */
|
2007-08-26 21:59:05 +08:00
|
|
|
if (parse->buffer) {
|
2008-01-23 21:14:02 +08:00
|
|
|
GstBuffer *temp;
|
|
|
|
temp = buffer;
|
2007-08-26 21:59:05 +08:00
|
|
|
buffer = gst_buffer_span(parse->buffer, 0, buffer,
|
2008-01-23 21:14:02 +08:00
|
|
|
GST_BUFFER_SIZE(parse->buffer)
|
|
|
|
+ GST_BUFFER_SIZE(buffer));
|
2007-08-26 21:59:05 +08:00
|
|
|
gst_buffer_unref(parse->buffer);
|
2008-01-23 21:14:02 +08:00
|
|
|
gst_buffer_unref(temp);
|
2007-08-26 21:59:05 +08:00
|
|
|
parse->buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = GST_BUFFER_DATA(buffer);
|
|
|
|
size = GST_BUFFER_SIZE(buffer);
|
|
|
|
|
|
|
|
while (offset < size) {
|
|
|
|
GstBuffer *output;
|
|
|
|
int consumed;
|
|
|
|
|
2008-01-31 01:30:27 +08:00
|
|
|
consumed = sbc_parse(&parse->new_sbc, data + offset,
|
|
|
|
size - offset);
|
2007-08-26 21:59:05 +08:00
|
|
|
if (consumed <= 0)
|
|
|
|
break;
|
|
|
|
|
2008-01-31 01:30:27 +08:00
|
|
|
if (parse->first_parsing || (memcmp(&parse->sbc,
|
|
|
|
&parse->new_sbc, sizeof(sbc_t)) != 0)) {
|
|
|
|
|
|
|
|
memcpy(&parse->sbc, &parse->new_sbc, sizeof(sbc_t));
|
|
|
|
if (parse->outcaps != NULL)
|
|
|
|
gst_caps_unref(parse->outcaps);
|
|
|
|
|
|
|
|
parse->outcaps = gst_sbc_parse_caps_from_sbc(
|
|
|
|
&parse->sbc);
|
|
|
|
|
|
|
|
parse->first_parsing = FALSE;
|
|
|
|
}
|
2007-08-26 21:59:05 +08:00
|
|
|
|
|
|
|
res = gst_pad_alloc_buffer_and_set_caps(parse->srcpad,
|
|
|
|
GST_BUFFER_OFFSET_NONE,
|
2008-01-31 01:30:27 +08:00
|
|
|
consumed, parse->outcaps, &output);
|
2007-08-26 21:59:05 +08:00
|
|
|
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
memcpy(GST_BUFFER_DATA(output), data + offset, consumed);
|
|
|
|
|
|
|
|
res = gst_pad_push(parse->srcpad, output);
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
offset += consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < size)
|
|
|
|
parse->buffer = gst_buffer_create_sub(buffer,
|
|
|
|
offset, size - offset);
|
|
|
|
|
|
|
|
done:
|
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
gst_object_unref(parse);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn sbc_parse_change_state(GstElement *element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstSbcParse *parse = GST_SBC_PARSE(element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
GST_DEBUG("Setup subband codec");
|
2008-01-31 01:30:27 +08:00
|
|
|
|
|
|
|
parse->channels = -1;
|
|
|
|
parse->rate = -1;
|
|
|
|
parse->first_parsing = TRUE;
|
|
|
|
|
2007-08-26 21:59:05 +08:00
|
|
|
sbc_init(&parse->sbc, 0);
|
|
|
|
break;
|
2007-10-19 06:46:12 +08:00
|
|
|
|
2007-08-26 21:59:05 +08:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
GST_DEBUG("Finish subband codec");
|
2008-01-23 21:14:02 +08:00
|
|
|
|
2007-08-26 21:59:05 +08:00
|
|
|
if (parse->buffer) {
|
|
|
|
gst_buffer_unref(parse->buffer);
|
|
|
|
parse->buffer = NULL;
|
|
|
|
}
|
2008-01-31 01:30:27 +08:00
|
|
|
if (parse->outcaps != NULL) {
|
|
|
|
gst_caps_unref(parse->outcaps);
|
|
|
|
parse->outcaps = NULL;
|
|
|
|
}
|
2008-01-23 21:14:02 +08:00
|
|
|
|
2008-01-31 01:30:27 +08:00
|
|
|
sbc_finish(&parse->sbc);
|
2007-08-26 21:59:05 +08:00
|
|
|
break;
|
2007-10-19 06:46:12 +08:00
|
|
|
|
2007-08-26 21:59:05 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent_class->change_state(element, transition);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_sbc_parse_base_init(gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template(element_class,
|
|
|
|
gst_static_pad_template_get(&sbc_parse_sink_factory));
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template(element_class,
|
|
|
|
gst_static_pad_template_get(&sbc_parse_src_factory));
|
|
|
|
|
|
|
|
gst_element_class_set_details(element_class, &sbc_parse_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_sbc_parse_class_init(GstSbcParseClass *klass)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent(klass);
|
|
|
|
|
|
|
|
element_class->change_state = GST_DEBUG_FUNCPTR(sbc_parse_change_state);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT(sbc_parse_debug, "sbcparse", 0,
|
2007-10-19 05:47:53 +08:00
|
|
|
"SBC parsing element");
|
2007-08-26 21:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_sbc_parse_init(GstSbcParse *self, GstSbcParseClass *klass)
|
|
|
|
{
|
2008-01-23 21:14:02 +08:00
|
|
|
self->sinkpad = gst_pad_new_from_static_template(
|
|
|
|
&sbc_parse_sink_factory, "sink");
|
|
|
|
gst_pad_set_chain_function(self->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR(sbc_parse_chain));
|
2007-08-26 21:59:05 +08:00
|
|
|
gst_element_add_pad(GST_ELEMENT(self), self->sinkpad);
|
|
|
|
|
2008-01-23 21:14:02 +08:00
|
|
|
self->srcpad = gst_pad_new_from_static_template(
|
|
|
|
&sbc_parse_src_factory, "src");
|
2007-08-26 21:59:05 +08:00
|
|
|
gst_element_add_pad(GST_ELEMENT(self), self->srcpad);
|
2008-01-31 01:30:27 +08:00
|
|
|
|
|
|
|
self->outcaps = NULL;
|
|
|
|
self->buffer = NULL;
|
|
|
|
self->channels = -1;
|
|
|
|
self->rate = -1;
|
|
|
|
self->first_parsing = TRUE;
|
2007-08-26 21:59:05 +08:00
|
|
|
}
|
2008-01-23 21:14:02 +08:00
|
|
|
|
2008-12-21 03:42:49 +08:00
|
|
|
gboolean gst_sbc_parse_plugin_init(GstPlugin *plugin)
|
2008-01-23 21:14:02 +08:00
|
|
|
{
|
2008-12-21 03:42:49 +08:00
|
|
|
return gst_element_register(plugin, "sbcparse", GST_RANK_NONE,
|
|
|
|
GST_TYPE_SBC_PARSE);
|
2008-01-23 21:14:02 +08:00
|
|
|
}
|
|
|
|
|