3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2008 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 "gstrtpsbcpay.h"
32 #define RTP_SBC_PAYLOAD_HEADER_SIZE 1
33 #define DEFAULT_MIN_FRAMES 0
34 #define RTP_SBC_HEADER_TOTAL (12 + RTP_SBC_PAYLOAD_HEADER_SIZE)
36 #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
51 guint8 is_fragmented:1;
52 guint8 is_first_fragment:1;
53 guint8 is_last_fragment:1;
56 } __attribute__ ((packed));
59 #error "Unknown byte order"
68 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
69 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
71 GST_BOILERPLATE (GstRtpSBCPay, gst_rtp_sbc_pay, GstBaseRTPPayload,
72 GST_TYPE_BASE_RTP_PAYLOAD);
74 static const GstElementDetails gst_rtp_sbc_pay_details =
75 GST_ELEMENT_DETAILS ("RTP packet payloader",
76 "Codec/Payloader/Network",
77 "Payload SBC audio as RTP packets",
78 "Thiago Sousa Santos " "<thiagoss@lcc.ufcg.edu.br>");
80 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
81 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
82 GST_STATIC_CAPS ("audio/x-sbc, "
83 "rate = (int) { 16000, 32000, 44100, 48000 }, "
84 "channels = (int) [ 1, 2 ], "
85 "mode = (string) { mono, dual, stereo, joint }, "
86 "blocks = (int) { 4, 8, 12, 16 }, "
87 "subbands = (int) { 4, 8 }, "
88 "allocation = (string) { snr, loudness },"
89 "bitpool = (int) [ 2, 64 ]; ")
92 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
93 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
94 GST_STATIC_CAPS ("application/x-rtp, "
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);
107 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
108 gint blocks, gint bitpool, const gchar * channel_mode)
113 len = 4 + (4 * subbands * channels) / 8;
115 if (strcmp (channel_mode, "mono") == 0 || 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;
126 gst_rtp_sbc_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
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);
165 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
170 guint8 *payload_data;
173 guint payload_length;
174 struct rtp_payload *payload;
176 if (sbcpay->frame_length == 0) {
177 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
178 return GST_FLOW_ERROR;
181 available = gst_adapter_available (sbcpay->adapter);
184 gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU (sbcpay) -
185 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
187 max_payload = MIN (max_payload, available);
188 frame_count = max_payload / sbcpay->frame_length;
189 payload_length = frame_count * sbcpay->frame_length;
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, GST_BASE_RTP_PAYLOAD_PT (sbcpay));
196 payload_data = gst_rtp_buffer_get_payload (outbuf);
197 payload = (struct rtp_payload *) payload_data;
198 memset (payload, 0, sizeof (struct rtp_payload));
199 payload->frame_count = frame_count;
201 data = gst_adapter_take (sbcpay->adapter, payload_length);
202 memcpy (payload_data + RTP_SBC_PAYLOAD_HEADER_SIZE, data, payload_length);
205 GST_BUFFER_TIMESTAMP (outbuf) = sbcpay->timestamp;
206 GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes", payload_length);
208 return gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (sbcpay), outbuf);
212 gst_rtp_sbc_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
214 GstRtpSBCPay *sbcpay;
217 /* FIXME check for negotiation */
219 sbcpay = GST_RTP_SBC_PAY (payload);
220 gst_adapter_push (sbcpay->adapter, gst_buffer_copy (buffer));
222 sbcpay->timestamp = GST_BUFFER_TIMESTAMP (buffer);
223 available = gst_adapter_available (sbcpay->adapter);
224 if (available + RTP_SBC_HEADER_TOTAL >=
225 GST_BASE_RTP_PAYLOAD_MTU (sbcpay) ||
226 (sbcpay->min_frames != -1 && available >
227 (sbcpay->min_frames * sbcpay->frame_length)))
228 return gst_rtp_sbc_pay_flush_buffers (sbcpay);
234 gst_rtp_sbc_pay_handle_event (GstPad * pad, GstEvent * event)
236 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (GST_PAD_PARENT (pad));
238 switch (GST_EVENT_TYPE (event)) {
240 gst_rtp_sbc_pay_flush_buffers (sbcpay);
250 gst_rtp_sbc_pay_base_init (gpointer g_class)
252 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
254 gst_element_class_add_pad_template (element_class,
255 gst_static_pad_template_get (&gst_rtp_sbc_pay_sink_factory));
256 gst_element_class_add_pad_template (element_class,
257 gst_static_pad_template_get (&gst_rtp_sbc_pay_src_factory));
259 gst_element_class_set_details (element_class, &gst_rtp_sbc_pay_details);
263 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));
272 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
274 GObjectClass *gobject_class;
275 GstBaseRTPPayloadClass *payload_class = 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 =
282 GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_property);
283 gobject_class->get_property =
284 GST_DEBUG_FUNCPTR (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 =
288 GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
289 payload_class->handle_event =
290 GST_DEBUG_FUNCPTR (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");
305 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
306 const GValue * value, GParamSpec * pspec)
308 GstRtpSBCPay *sbcpay;
310 sbcpay = GST_RTP_SBC_PAY (object);
313 case PROP_MIN_FRAMES:
314 sbcpay->min_frames = g_value_get_int (value);
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
324 GValue * value, GParamSpec * pspec)
326 GstRtpSBCPay *sbcpay;
328 sbcpay = GST_RTP_SBC_PAY (object);
331 case PROP_MIN_FRAMES:
332 g_value_set_int (value, sbcpay->min_frames);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gst_rtp_sbc_pay_init (GstRtpSBCPay * self, GstRtpSBCPayClass * klass)
343 self->adapter = gst_adapter_new ();
344 self->frame_length = 0;
347 self->min_frames = DEFAULT_MIN_FRAMES;
351 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
353 return gst_element_register (plugin, "rtpsbcpay",
354 GST_RANK_NONE, GST_TYPE_RTP_SBC_PAY);