1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29
30 #include "gstsbcutil.h"
31 #include "gstsbcparse.h"
32
33 GST_DEBUG_CATEGORY_STATIC(sbc_parse_debug);
34 #define GST_CAT_DEFAULT sbc_parse_debug
35
36 GST_BOILERPLATE(GstSbcParse, gst_sbc_parse, GstElement, GST_TYPE_ELEMENT);
37
38 static const GstElementDetails sbc_parse_details =
39 GST_ELEMENT_DETAILS("Bluetooth SBC parser",
40 "Codec/Parser/Audio",
41 "Parse a SBC audio stream",
42 "Marcel Holtmann <marcel@holtmann.org>");
43
44 static GstStaticPadTemplate sbc_parse_sink_factory =
45 GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
46 GST_STATIC_CAPS("audio/x-sbc,"
47 "parsed = (boolean) false"));
48
49 static GstStaticPadTemplate sbc_parse_src_factory =
50 GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS,
51 GST_STATIC_CAPS("audio/x-sbc, "
52 "rate = (int) { 16000, 32000, 44100, 48000 }, "
53 "channels = (int) [ 1, 2 ], "
54 "mode = (string) { \"mono\", \"dual\", \"stereo\", \"joint\" }, "
55 "blocks = (int) { 4, 8, 12, 16 }, "
56 "subbands = (int) { 4, 8 }, "
57 "allocation = (string) { \"snr\", \"loudness\" },"
58 "bitpool = (int) [ 2, 64 ],"
59 "parsed = (boolean) true"));
60
sbc_parse_chain(GstPad * pad,GstBuffer * buffer)61 static GstFlowReturn sbc_parse_chain(GstPad *pad, GstBuffer *buffer)
62 {
63 GstSbcParse *parse = GST_SBC_PARSE(gst_pad_get_parent(pad));
64 GstFlowReturn res = GST_FLOW_OK;
65 guint size, offset = 0;
66 guint8 *data;
67
68 /* FIXME use a gstadpter */
69 if (parse->buffer) {
70 GstBuffer *temp;
71 temp = buffer;
72 buffer = gst_buffer_span(parse->buffer, 0, buffer,
73 GST_BUFFER_SIZE(parse->buffer)
74 + GST_BUFFER_SIZE(buffer));
75 gst_buffer_unref(parse->buffer);
76 gst_buffer_unref(temp);
77 parse->buffer = NULL;
78 }
79
80 data = GST_BUFFER_DATA(buffer);
81 size = GST_BUFFER_SIZE(buffer);
82
83 while (offset < size) {
84 GstBuffer *output;
85 int consumed;
86
87 consumed = sbc_parse(&parse->new_sbc, data + offset,
88 size - offset);
89 if (consumed <= 0)
90 break;
91
92 if (parse->first_parsing || (memcmp(&parse->sbc,
93 &parse->new_sbc, sizeof(sbc_t)) != 0)) {
94
95 memcpy(&parse->sbc, &parse->new_sbc, sizeof(sbc_t));
96 if (parse->outcaps != NULL)
97 gst_caps_unref(parse->outcaps);
98
99 parse->outcaps = gst_sbc_parse_caps_from_sbc(
100 &parse->sbc);
101
102 parse->first_parsing = FALSE;
103 }
104
105 res = gst_pad_alloc_buffer_and_set_caps(parse->srcpad,
106 GST_BUFFER_OFFSET_NONE,
107 consumed, parse->outcaps, &output);
108
109 if (res != GST_FLOW_OK)
110 goto done;
111
112 memcpy(GST_BUFFER_DATA(output), data + offset, consumed);
113
114 res = gst_pad_push(parse->srcpad, output);
115 if (res != GST_FLOW_OK)
116 goto done;
117
118 offset += consumed;
119 }
120
121 if (offset < size)
122 parse->buffer = gst_buffer_create_sub(buffer,
123 offset, size - offset);
124
125 done:
126 gst_buffer_unref(buffer);
127 gst_object_unref(parse);
128
129 return res;
130 }
131
sbc_parse_change_state(GstElement * element,GstStateChange transition)132 static GstStateChangeReturn sbc_parse_change_state(GstElement *element,
133 GstStateChange transition)
134 {
135 GstSbcParse *parse = GST_SBC_PARSE(element);
136
137 switch (transition) {
138 case GST_STATE_CHANGE_READY_TO_PAUSED:
139 GST_DEBUG("Setup subband codec");
140
141 parse->channels = -1;
142 parse->rate = -1;
143 parse->first_parsing = TRUE;
144
145 sbc_init(&parse->sbc, 0);
146 break;
147
148 case GST_STATE_CHANGE_PAUSED_TO_READY:
149 GST_DEBUG("Finish subband codec");
150
151 if (parse->buffer) {
152 gst_buffer_unref(parse->buffer);
153 parse->buffer = NULL;
154 }
155 if (parse->outcaps != NULL) {
156 gst_caps_unref(parse->outcaps);
157 parse->outcaps = NULL;
158 }
159
160 sbc_finish(&parse->sbc);
161 break;
162
163 default:
164 break;
165 }
166
167 return parent_class->change_state(element, transition);
168 }
169
gst_sbc_parse_base_init(gpointer g_class)170 static void gst_sbc_parse_base_init(gpointer g_class)
171 {
172 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
173
174 gst_element_class_add_pad_template(element_class,
175 gst_static_pad_template_get(&sbc_parse_sink_factory));
176
177 gst_element_class_add_pad_template(element_class,
178 gst_static_pad_template_get(&sbc_parse_src_factory));
179
180 gst_element_class_set_details(element_class, &sbc_parse_details);
181 }
182
gst_sbc_parse_class_init(GstSbcParseClass * klass)183 static void gst_sbc_parse_class_init(GstSbcParseClass *klass)
184 {
185 GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
186
187 parent_class = g_type_class_peek_parent(klass);
188
189 element_class->change_state = GST_DEBUG_FUNCPTR(sbc_parse_change_state);
190
191 GST_DEBUG_CATEGORY_INIT(sbc_parse_debug, "sbcparse", 0,
192 "SBC parsing element");
193 }
194
gst_sbc_parse_init(GstSbcParse * self,GstSbcParseClass * klass)195 static void gst_sbc_parse_init(GstSbcParse *self, GstSbcParseClass *klass)
196 {
197 self->sinkpad = gst_pad_new_from_static_template(
198 &sbc_parse_sink_factory, "sink");
199 gst_pad_set_chain_function(self->sinkpad,
200 GST_DEBUG_FUNCPTR(sbc_parse_chain));
201 gst_element_add_pad(GST_ELEMENT(self), self->sinkpad);
202
203 self->srcpad = gst_pad_new_from_static_template(
204 &sbc_parse_src_factory, "src");
205 gst_element_add_pad(GST_ELEMENT(self), self->srcpad);
206
207 self->outcaps = NULL;
208 self->buffer = NULL;
209 self->channels = -1;
210 self->rate = -1;
211 self->first_parsing = TRUE;
212 }
213
gst_sbc_parse_plugin_init(GstPlugin * plugin)214 gboolean gst_sbc_parse_plugin_init(GstPlugin *plugin)
215 {
216 return gst_element_register(plugin, "sbcparse", GST_RANK_NONE,
217 GST_TYPE_SBC_PARSE);
218 }
219
220