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 "gstrtpsbcpay.h"
30 #define RTP_SBC_PAYLOAD_HEADER_SIZE 1
31 #define DEFAULT_MIN_FRAMES 0
32 #define RTP_SBC_HEADER_TOTAL (12 + RTP_SBC_PAYLOAD_HEADER_SIZE)
34 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
36 /* FIXME: this seems all a bit over the top for a single byte.. */
41 guint8 is_last_fragment:1;
42 guint8 is_first_fragment:1;
43 guint8 is_fragmented:1;
44 } __attribute__ ((packed));
46 #elif G_BYTE_ORDER == G_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"
67 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
68 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
70 #define parent_class gst_rtp_sbc_pay_parent_class
71 G_DEFINE_TYPE (GstRtpSBCPay, gst_rtp_sbc_pay, GST_TYPE_RTP_BASE_PAYLOAD);
73 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
74 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
75 GST_STATIC_CAPS ("audio/x-sbc, "
76 "rate = (int) { 16000, 32000, 44100, 48000 }, "
77 "channels = (int) [ 1, 2 ], "
78 "channel-mode = (string) { mono, dual, stereo, joint }, "
79 "blocks = (int) { 4, 8, 12, 16 }, "
80 "subbands = (int) { 4, 8 }, "
81 "allocation-method = (string) { snr, loudness }, "
82 "bitpool = (int) [ 2, 64 ]")
85 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
86 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
87 GST_STATIC_CAPS ("application/x-rtp, "
88 "media = (string) audio,"
89 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
90 "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
91 "encoding-name = (string) SBC")
94 static void gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
100 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
101 gint blocks, gint bitpool, const gchar * channel_mode)
106 len = 4 + (4 * subbands * channels) / 8;
108 if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
109 len += ((blocks * channels * bitpool) + 7) / 8;
111 join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
112 len += ((join * subbands + blocks * bitpool) + 7) / 8;
119 gst_rtp_sbc_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
121 GstRtpSBCPay *sbcpay;
122 gint rate, subbands, channels, blocks, bitpool;
124 const gchar *channel_mode;
125 GstStructure *structure;
127 sbcpay = GST_RTP_SBC_PAY (payload);
129 structure = gst_caps_get_structure (caps, 0);
130 if (!gst_structure_get_int (structure, "rate", &rate))
132 if (!gst_structure_get_int (structure, "channels", &channels))
134 if (!gst_structure_get_int (structure, "blocks", &blocks))
136 if (!gst_structure_get_int (structure, "bitpool", &bitpool))
138 if (!gst_structure_get_int (structure, "subbands", &subbands))
141 channel_mode = gst_structure_get_string (structure, "channel-mode");
145 frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
146 bitpool, channel_mode);
148 sbcpay->frame_length = frame_len;
150 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
152 GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
154 return gst_rtp_base_payload_set_outcaps (payload, NULL);
158 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
160 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
164 guint8 *payload_data;
166 guint payload_length;
167 struct rtp_payload *payload;
169 if (sbcpay->frame_length == 0) {
170 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
171 return GST_FLOW_ERROR;
174 available = gst_adapter_available (sbcpay->adapter);
177 gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
178 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
180 max_payload = MIN (max_payload, available);
181 frame_count = max_payload / sbcpay->frame_length;
182 payload_length = frame_count * sbcpay->frame_length;
183 if (payload_length == 0) /* Nothing to send */
186 outbuf = gst_rtp_buffer_new_allocate (payload_length +
187 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
190 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
192 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
194 /* write header and copy data into payload */
195 payload_data = gst_rtp_buffer_get_payload (&rtp);
196 payload = (struct rtp_payload *) payload_data;
197 memset (payload, 0, sizeof (struct rtp_payload));
198 payload->frame_count = frame_count;
200 gst_adapter_copy (sbcpay->adapter, payload_data +
201 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, payload_length);
203 gst_rtp_buffer_unmap (&rtp);
205 gst_adapter_flush (sbcpay->adapter, payload_length);
207 /* FIXME: what about duration? */
208 GST_BUFFER_TIMESTAMP (outbuf) = sbcpay->timestamp;
209 GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes", payload_length);
211 return gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
215 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
217 GstRtpSBCPay *sbcpay;
220 /* FIXME check for negotiation */
222 sbcpay = GST_RTP_SBC_PAY (payload);
223 sbcpay->timestamp = GST_BUFFER_TIMESTAMP (buffer);
225 gst_adapter_push (sbcpay->adapter, buffer);
227 available = gst_adapter_available (sbcpay->adapter);
228 if (available + RTP_SBC_HEADER_TOTAL >=
229 GST_RTP_BASE_PAYLOAD_MTU (sbcpay) ||
230 (available > (sbcpay->min_frames * sbcpay->frame_length)))
231 return gst_rtp_sbc_pay_flush_buffers (sbcpay);
237 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
239 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
241 switch (GST_EVENT_TYPE (event)) {
243 gst_rtp_sbc_pay_flush_buffers (sbcpay);
249 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
253 gst_rtp_sbc_pay_finalize (GObject * object)
255 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
257 g_object_unref (sbcpay->adapter);
259 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
263 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
265 GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
266 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
267 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
269 gobject_class->finalize = gst_rtp_sbc_pay_finalize;
270 gobject_class->set_property = gst_rtp_sbc_pay_set_property;
271 gobject_class->get_property = gst_rtp_sbc_pay_get_property;
273 payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
274 payload_class->handle_buffer =
275 GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
276 payload_class->sink_event = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_sink_event);
279 g_object_class_install_property (G_OBJECT_CLASS (klass),
281 g_param_spec_int ("min-frames", "minimum frame number",
282 "Minimum quantity of frames to send in one packet "
283 "(-1 for maximum allowed by the mtu)",
284 -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
286 gst_element_class_add_pad_template (element_class,
287 gst_static_pad_template_get (&gst_rtp_sbc_pay_sink_factory));
288 gst_element_class_add_pad_template (element_class,
289 gst_static_pad_template_get (&gst_rtp_sbc_pay_src_factory));
291 gst_element_class_set_static_metadata (element_class, "RTP packet payloader",
292 "Codec/Payloader/Network", "Payload SBC audio as RTP packets",
293 "Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>");
295 GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
296 "RTP SBC payloader");
300 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
301 const GValue * value, GParamSpec * pspec)
303 GstRtpSBCPay *sbcpay;
305 sbcpay = GST_RTP_SBC_PAY (object);
308 case PROP_MIN_FRAMES:
309 sbcpay->min_frames = g_value_get_int (value);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
319 GValue * value, GParamSpec * pspec)
321 GstRtpSBCPay *sbcpay;
323 sbcpay = GST_RTP_SBC_PAY (object);
326 case PROP_MIN_FRAMES:
327 g_value_set_int (value, sbcpay->min_frames);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
338 self->adapter = gst_adapter_new ();
339 self->frame_length = 0;
342 self->min_frames = DEFAULT_MIN_FRAMES;
346 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
348 return gst_element_register (plugin, "rtpsbcpay", GST_RANK_NONE,
349 GST_TYPE_RTP_SBC_PAY);