3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
28 #include "gstpragma.h"
29 #include "gstrtpsbcpay.h"
33 #define RTP_SBC_PAYLOAD_HEADER_SIZE 1
34 #define DEFAULT_MIN_FRAMES 0
35 #define RTP_SBC_HEADER_TOTAL (12 + RTP_SBC_PAYLOAD_HEADER_SIZE)
37 #if __BYTE_ORDER == __LITTLE_ENDIAN
42 guint8 is_last_fragment:1;
43 guint8 is_first_fragment:1;
44 guint8 is_fragmented:1;
45 } __attribute__ ((packed));
47 #elif __BYTE_ORDER == __BIG_ENDIAN
50 guint8 is_fragmented:1;
51 guint8 is_first_fragment:1;
52 guint8 is_last_fragment:1;
55 } __attribute__ ((packed));
58 #error "Unknown byte order"
66 GST_DEBUG_CATEGORY_STATIC(gst_rtp_sbc_pay_debug);
67 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
69 GST_BOILERPLATE(GstRtpSBCPay, gst_rtp_sbc_pay, GstBaseRTPPayload,
70 GST_TYPE_BASE_RTP_PAYLOAD);
72 static const GstElementDetails gst_rtp_sbc_pay_details =
73 GST_ELEMENT_DETAILS("RTP packet payloader",
74 "Codec/Payloader/Network",
75 "Payload SBC audio as RTP packets",
76 "Thiago Sousa Santos "
77 "<thiagoss@lcc.ufcg.edu.br>");
79 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
80 GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
81 GST_STATIC_CAPS("audio/x-sbc, "
82 "rate = (int) { 16000, 32000, 44100, 48000 }, "
83 "channels = (int) [ 1, 2 ], "
84 "mode = (string) { \"mono\", \"dual\", \"stereo\", \"joint\" }, "
85 "blocks = (int) { 4, 8, 12, 16 }, "
86 "subbands = (int) { 4, 8 }, "
87 "allocation = (string) { \"snr\", \"loudness\" }, "
88 "bitpool = (int) [ 2, 64 ]")
91 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
92 GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS,
95 "media = (string) \"audio\","
96 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
97 "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
98 "encoding-name = (string) \"SBC\"")
101 static void gst_rtp_sbc_pay_set_property(GObject *object, guint prop_id,
102 const GValue *value, GParamSpec *pspec);
103 static void gst_rtp_sbc_pay_get_property(GObject *object, guint prop_id,
104 GValue *value, GParamSpec *pspec);
106 static gint gst_rtp_sbc_pay_get_frame_len(gint subbands, gint channels,
107 gint blocks, gint bitpool, const gchar *channel_mode)
112 len = 4 + (4 * subbands * channels)/8;
114 if (strcmp(channel_mode, "mono") == 0 ||
115 strcmp(channel_mode, "dual") == 0)
116 len += ((blocks * channels * bitpool) + 7) / 8;
118 join = strcmp(channel_mode, "joint") == 0 ? 1 : 0;
119 len += ((join * subbands + blocks * bitpool) + 7) / 8;
125 static gboolean gst_rtp_sbc_pay_set_caps(GstBaseRTPPayload *payload,
128 GstRtpSBCPay *sbcpay;
129 gint rate, subbands, channels, blocks, bitpool;
131 const gchar *channel_mode;
132 GstStructure *structure;
134 sbcpay = GST_RTP_SBC_PAY(payload);
136 structure = gst_caps_get_structure(caps, 0);
137 if (!gst_structure_get_int(structure, "rate", &rate))
139 if (!gst_structure_get_int(structure, "channels", &channels))
141 if (!gst_structure_get_int(structure, "blocks", &blocks))
143 if (!gst_structure_get_int(structure, "bitpool", &bitpool))
145 if (!gst_structure_get_int(structure, "subbands", &subbands))
148 channel_mode = gst_structure_get_string(structure, "mode");
152 frame_len = gst_rtp_sbc_pay_get_frame_len(subbands, channels, blocks,
153 bitpool, channel_mode);
155 sbcpay->frame_length = frame_len;
157 gst_basertppayload_set_options(payload, "audio", TRUE, "SBC", rate);
159 GST_DEBUG_OBJECT(payload, "calculated frame length: %d ", frame_len);
161 return gst_basertppayload_set_outcaps(payload, NULL);
164 static GstFlowReturn gst_rtp_sbc_pay_flush_buffers(GstRtpSBCPay *sbcpay)
169 guint8 *payload_data;
171 guint payload_length;
172 struct rtp_payload *payload;
174 if (sbcpay->frame_length == 0) {
175 GST_ERROR_OBJECT(sbcpay, "Frame length is 0");
176 return GST_FLOW_ERROR;
179 available = gst_adapter_available(sbcpay->adapter);
181 max_payload = gst_rtp_buffer_calc_payload_len(
182 GST_BASE_RTP_PAYLOAD_MTU(sbcpay) - RTP_SBC_PAYLOAD_HEADER_SIZE,
185 max_payload = MIN(max_payload, available);
186 frame_count = max_payload / sbcpay->frame_length;
187 payload_length = frame_count * sbcpay->frame_length;
188 if (payload_length == 0) /* Nothing to send */
191 outbuf = gst_rtp_buffer_new_allocate(payload_length +
192 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
194 gst_rtp_buffer_set_payload_type(outbuf,
195 GST_BASE_RTP_PAYLOAD_PT(sbcpay));
197 payload_data = gst_rtp_buffer_get_payload(outbuf);
198 payload = (struct rtp_payload *) payload_data;
199 memset(payload, 0, sizeof(struct rtp_payload));
200 payload->frame_count = frame_count;
202 gst_adapter_copy(sbcpay->adapter, payload_data +
203 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, payload_length);
204 gst_adapter_flush(sbcpay->adapter, payload_length);
206 GST_BUFFER_TIMESTAMP(outbuf) = sbcpay->timestamp;
207 GST_DEBUG_OBJECT(sbcpay, "Pushing %d bytes", payload_length);
209 return gst_basertppayload_push(GST_BASE_RTP_PAYLOAD(sbcpay), outbuf);
212 static GstFlowReturn gst_rtp_sbc_pay_handle_buffer(GstBaseRTPPayload *payload,
215 GstRtpSBCPay *sbcpay;
218 /* FIXME check for negotiation */
220 sbcpay = GST_RTP_SBC_PAY(payload);
221 sbcpay->timestamp = GST_BUFFER_TIMESTAMP(buffer);
223 gst_adapter_push(sbcpay->adapter, buffer);
225 available = gst_adapter_available(sbcpay->adapter);
226 if (available + RTP_SBC_HEADER_TOTAL >=
227 GST_BASE_RTP_PAYLOAD_MTU(sbcpay) ||
229 (sbcpay->min_frames * sbcpay->frame_length)))
230 return gst_rtp_sbc_pay_flush_buffers(sbcpay);
235 static gboolean gst_rtp_sbc_pay_handle_event(GstPad *pad,
238 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY(GST_PAD_PARENT(pad));
240 switch (GST_EVENT_TYPE(event)) {
242 gst_rtp_sbc_pay_flush_buffers(sbcpay);
251 static void gst_rtp_sbc_pay_base_init(gpointer g_class)
253 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
255 gst_element_class_add_pad_template(element_class,
256 gst_static_pad_template_get(&gst_rtp_sbc_pay_sink_factory));
257 gst_element_class_add_pad_template(element_class,
258 gst_static_pad_template_get(&gst_rtp_sbc_pay_src_factory));
260 gst_element_class_set_details(element_class, &gst_rtp_sbc_pay_details);
263 static void gst_rtp_sbc_pay_finalize(GObject *object)
265 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY(object);
266 g_object_unref(sbcpay->adapter);
268 GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
271 static void gst_rtp_sbc_pay_class_init(GstRtpSBCPayClass *klass)
273 GObjectClass *gobject_class;
274 GstBaseRTPPayloadClass *payload_class =
275 GST_BASE_RTP_PAYLOAD_CLASS(klass);
277 gobject_class = G_OBJECT_CLASS(klass);
278 parent_class = g_type_class_peek_parent(klass);
280 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_rtp_sbc_pay_finalize);
281 gobject_class->set_property = GST_DEBUG_FUNCPTR(
282 gst_rtp_sbc_pay_set_property);
283 gobject_class->get_property = GST_DEBUG_FUNCPTR(
284 gst_rtp_sbc_pay_get_property);
286 payload_class->set_caps = GST_DEBUG_FUNCPTR(gst_rtp_sbc_pay_set_caps);
287 payload_class->handle_buffer = GST_DEBUG_FUNCPTR(
288 gst_rtp_sbc_pay_handle_buffer);
289 payload_class->handle_event = GST_DEBUG_FUNCPTR(
290 gst_rtp_sbc_pay_handle_event);
293 g_object_class_install_property(G_OBJECT_CLASS(klass),
295 g_param_spec_int("min-frames", "minimum frame number",
296 "Minimum quantity of frames to send in one packet "
297 "(-1 for maximum allowed by the mtu)",
298 -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
300 GST_DEBUG_CATEGORY_INIT(gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
301 "RTP SBC payloader");
304 static void gst_rtp_sbc_pay_set_property(GObject *object, guint prop_id,
305 const GValue *value, GParamSpec *pspec)
307 GstRtpSBCPay *sbcpay;
309 sbcpay = GST_RTP_SBC_PAY(object);
312 case PROP_MIN_FRAMES:
313 sbcpay->min_frames = g_value_get_int(value);
316 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
321 static void gst_rtp_sbc_pay_get_property(GObject *object, guint prop_id,
322 GValue *value, GParamSpec *pspec)
324 GstRtpSBCPay *sbcpay;
326 sbcpay = GST_RTP_SBC_PAY(object);
329 case PROP_MIN_FRAMES:
330 g_value_set_int(value, sbcpay->min_frames);
333 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
338 static void gst_rtp_sbc_pay_init(GstRtpSBCPay *self, GstRtpSBCPayClass *klass)
340 self->adapter = gst_adapter_new();
341 self->frame_length = 0;
344 self->min_frames = DEFAULT_MIN_FRAMES;
347 gboolean gst_rtp_sbc_pay_plugin_init(GstPlugin *plugin)
349 return gst_element_register(plugin, "rtpsbcpay", GST_RANK_NONE,
350 GST_TYPE_RTP_SBC_PAY);