1 /* GStreamer RTP SBC payloader
2 * BlueZ - Bluetooth protocol stack for Linux
4 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <gst/audio/audio.h>
27 #include "gstrtpsbcpay.h"
30 #include "gstrtputils.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 /* BEGIN: Packing for rtp_payload */
41 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
42 /* FIXME: this seems all a bit over the top for a single byte.. */
47 guint8 is_last_fragment:1;
48 guint8 is_first_fragment:1;
49 guint8 is_fragmented:1;
51 #elif G_BYTE_ORDER == G_BIG_ENDIAN
54 guint8 is_fragmented:1;
55 guint8 is_first_fragment:1;
56 guint8 is_last_fragment:1;
61 #error "Unknown byte order"
68 __attribute__ ((packed));
70 /* END: Packing for rtp_payload */
78 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
79 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
81 #define parent_class gst_rtp_sbc_pay_parent_class
82 G_DEFINE_TYPE (GstRtpSBCPay, gst_rtp_sbc_pay, GST_TYPE_RTP_BASE_PAYLOAD);
84 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
85 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
86 GST_STATIC_CAPS ("audio/x-sbc, "
87 "rate = (int) { 16000, 32000, 44100, 48000 }, "
88 "channels = (int) [ 1, 2 ], "
89 "channel-mode = (string) { mono, dual, stereo, joint }, "
90 "blocks = (int) { 4, 8, 12, 16 }, "
91 "subbands = (int) { 4, 8 }, "
92 "allocation-method = (string) { snr, loudness }, "
93 "bitpool = (int) [ 2, 64 ]")
96 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
97 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
98 GST_STATIC_CAPS ("application/x-rtp, "
99 "media = (string) audio,"
100 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
101 "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
102 "encoding-name = (string) SBC")
105 static void gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
111 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
112 gint blocks, gint bitpool, const gchar * channel_mode)
117 len = 4 + (4 * subbands * channels) / 8;
119 if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
120 len += ((blocks * channels * bitpool) + 7) / 8;
122 join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
123 len += ((join * subbands + blocks * bitpool) + 7) / 8;
130 gst_rtp_sbc_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
132 GstRtpSBCPay *sbcpay;
133 gint rate, subbands, channels, blocks, bitpool;
135 const gchar *channel_mode;
136 GstStructure *structure;
138 sbcpay = GST_RTP_SBC_PAY (payload);
140 structure = gst_caps_get_structure (caps, 0);
141 if (!gst_structure_get_int (structure, "rate", &rate))
143 if (!gst_structure_get_int (structure, "channels", &channels))
145 if (!gst_structure_get_int (structure, "blocks", &blocks))
147 if (!gst_structure_get_int (structure, "bitpool", &bitpool))
149 if (!gst_structure_get_int (structure, "subbands", &subbands))
152 channel_mode = gst_structure_get_string (structure, "channel-mode");
156 frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
157 bitpool, channel_mode);
159 sbcpay->frame_length = frame_len;
161 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
163 GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
165 return gst_rtp_base_payload_set_outcaps (payload, NULL);
169 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
171 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
174 GstBuffer *outbuf, *paybuf;
175 guint8 *payload_data;
177 guint payload_length;
178 struct rtp_payload *payload;
180 if (sbcpay->frame_length == 0) {
181 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
182 return GST_FLOW_ERROR;
185 available = gst_adapter_available (sbcpay->adapter);
188 gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
189 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
191 max_payload = MIN (max_payload, available);
192 frame_count = max_payload / sbcpay->frame_length;
193 payload_length = frame_count * sbcpay->frame_length;
194 if (payload_length == 0) /* Nothing to send */
197 outbuf = gst_rtp_buffer_new_allocate (RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
200 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
202 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
204 /* write header and copy data into payload */
205 payload_data = gst_rtp_buffer_get_payload (&rtp);
206 payload = (struct rtp_payload *) payload_data;
207 memset (payload, 0, sizeof (struct rtp_payload));
208 payload->frame_count = frame_count;
210 gst_rtp_buffer_unmap (&rtp);
212 paybuf = gst_adapter_take_buffer_fast (sbcpay->adapter, payload_length);
213 gst_rtp_copy_meta (GST_ELEMENT_CAST (sbcpay), outbuf, paybuf,
214 g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
215 outbuf = gst_buffer_append (outbuf, paybuf);
217 /* FIXME: what about duration? */
218 GST_BUFFER_PTS (outbuf) = sbcpay->timestamp;
219 GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes", payload_length);
221 return gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
225 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
227 GstRtpSBCPay *sbcpay;
230 /* FIXME check for negotiation */
232 sbcpay = GST_RTP_SBC_PAY (payload);
233 sbcpay->timestamp = GST_BUFFER_PTS (buffer);
235 gst_adapter_push (sbcpay->adapter, buffer);
237 available = gst_adapter_available (sbcpay->adapter);
238 if (available + RTP_SBC_HEADER_TOTAL >=
239 GST_RTP_BASE_PAYLOAD_MTU (sbcpay) ||
240 (available > (sbcpay->min_frames * sbcpay->frame_length)))
241 return gst_rtp_sbc_pay_flush_buffers (sbcpay);
247 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
249 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
251 switch (GST_EVENT_TYPE (event)) {
253 gst_rtp_sbc_pay_flush_buffers (sbcpay);
259 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
263 gst_rtp_sbc_pay_finalize (GObject * object)
265 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
267 g_object_unref (sbcpay->adapter);
269 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
273 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
275 GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
276 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
277 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
279 gobject_class->finalize = gst_rtp_sbc_pay_finalize;
280 gobject_class->set_property = gst_rtp_sbc_pay_set_property;
281 gobject_class->get_property = gst_rtp_sbc_pay_get_property;
283 payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
284 payload_class->handle_buffer =
285 GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
286 payload_class->sink_event = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_sink_event);
289 g_object_class_install_property (G_OBJECT_CLASS (klass),
291 g_param_spec_int ("min-frames", "minimum frame number",
292 "Minimum quantity of frames to send in one packet "
293 "(-1 for maximum allowed by the mtu)",
294 -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
296 gst_element_class_add_pad_template (element_class,
297 gst_static_pad_template_get (&gst_rtp_sbc_pay_sink_factory));
298 gst_element_class_add_pad_template (element_class,
299 gst_static_pad_template_get (&gst_rtp_sbc_pay_src_factory));
301 gst_element_class_set_static_metadata (element_class, "RTP packet payloader",
302 "Codec/Payloader/Network", "Payload SBC audio as RTP packets",
303 "Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>");
305 GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
306 "RTP SBC payloader");
310 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
311 const GValue * value, GParamSpec * pspec)
313 GstRtpSBCPay *sbcpay;
315 sbcpay = GST_RTP_SBC_PAY (object);
318 case PROP_MIN_FRAMES:
319 sbcpay->min_frames = g_value_get_int (value);
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
329 GValue * value, GParamSpec * pspec)
331 GstRtpSBCPay *sbcpay;
333 sbcpay = GST_RTP_SBC_PAY (object);
336 case PROP_MIN_FRAMES:
337 g_value_set_int (value, sbcpay->min_frames);
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
348 self->adapter = gst_adapter_new ();
349 self->frame_length = 0;
352 self->min_frames = DEFAULT_MIN_FRAMES;
356 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
358 return gst_element_register (plugin, "rtpsbcpay", GST_RANK_NONE,
359 GST_TYPE_RTP_SBC_PAY);