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;
160 sbcpay->frame_duration = ((blocks * subbands) * GST_SECOND) / rate;
161 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
163 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
165 GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
167 return gst_rtp_base_payload_set_outcaps (payload, NULL);
171 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
173 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
176 GstBuffer *outbuf, *paybuf;
177 guint8 *payload_data;
179 guint payload_length;
180 struct rtp_payload *payload;
183 if (sbcpay->frame_length == 0) {
184 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
185 return GST_FLOW_ERROR;
189 available = gst_adapter_available (sbcpay->adapter);
192 gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
193 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
195 max_payload = MIN (max_payload, available);
196 frame_count = max_payload / sbcpay->frame_length;
197 payload_length = frame_count * sbcpay->frame_length;
198 if (payload_length == 0) /* Nothing to send */
201 outbuf = gst_rtp_buffer_new_allocate (RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
204 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
206 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
208 /* write header and copy data into payload */
209 payload_data = gst_rtp_buffer_get_payload (&rtp);
210 payload = (struct rtp_payload *) payload_data;
211 memset (payload, 0, sizeof (struct rtp_payload));
212 payload->frame_count = frame_count;
214 gst_rtp_buffer_unmap (&rtp);
216 paybuf = gst_adapter_take_buffer_fast (sbcpay->adapter, payload_length);
217 gst_rtp_copy_audio_meta (sbcpay, outbuf, paybuf);
218 outbuf = gst_buffer_append (outbuf, paybuf);
220 GST_BUFFER_PTS (outbuf) = sbcpay->last_timestamp;
221 GST_BUFFER_DURATION (outbuf) = frame_count * sbcpay->frame_duration;
222 GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes: %" GST_TIME_FORMAT,
223 payload_length, GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
225 sbcpay->last_timestamp += frame_count * sbcpay->frame_duration;
227 res = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
229 /* try to send another RTP buffer if available data exceeds MTU size */
230 } while (res == GST_FLOW_OK);
236 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
238 GstRtpSBCPay *sbcpay;
241 /* FIXME check for negotiation */
243 sbcpay = GST_RTP_SBC_PAY (payload);
245 if (GST_BUFFER_IS_DISCONT (buffer)) {
246 /* Try to flush whatever's left */
247 gst_rtp_sbc_pay_flush_buffers (sbcpay);
249 gst_adapter_flush (sbcpay->adapter,
250 gst_adapter_available (sbcpay->adapter));
251 /* Reset timestamps */
252 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
255 if (sbcpay->last_timestamp == GST_CLOCK_TIME_NONE)
256 sbcpay->last_timestamp = GST_BUFFER_PTS (buffer);
258 gst_adapter_push (sbcpay->adapter, buffer);
260 available = gst_adapter_available (sbcpay->adapter);
261 if (available + RTP_SBC_HEADER_TOTAL >=
262 GST_RTP_BASE_PAYLOAD_MTU (sbcpay) ||
263 (available > (sbcpay->min_frames * sbcpay->frame_length)))
264 return gst_rtp_sbc_pay_flush_buffers (sbcpay);
270 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
272 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
274 switch (GST_EVENT_TYPE (event)) {
276 gst_rtp_sbc_pay_flush_buffers (sbcpay);
282 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
286 gst_rtp_sbc_pay_finalize (GObject * object)
288 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
290 g_object_unref (sbcpay->adapter);
292 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
296 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
298 GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
299 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
300 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
302 gobject_class->finalize = gst_rtp_sbc_pay_finalize;
303 gobject_class->set_property = gst_rtp_sbc_pay_set_property;
304 gobject_class->get_property = gst_rtp_sbc_pay_get_property;
306 payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
307 payload_class->handle_buffer =
308 GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
309 payload_class->sink_event = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_sink_event);
312 g_object_class_install_property (G_OBJECT_CLASS (klass),
314 g_param_spec_int ("min-frames", "minimum frame number",
315 "Minimum quantity of frames to send in one packet "
316 "(-1 for maximum allowed by the mtu)",
317 -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
319 gst_element_class_add_static_pad_template (element_class,
320 &gst_rtp_sbc_pay_sink_factory);
321 gst_element_class_add_static_pad_template (element_class,
322 &gst_rtp_sbc_pay_src_factory);
324 gst_element_class_set_static_metadata (element_class, "RTP packet payloader",
325 "Codec/Payloader/Network", "Payload SBC audio as RTP packets",
326 "Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>");
328 GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
329 "RTP SBC payloader");
333 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
334 const GValue * value, GParamSpec * pspec)
336 GstRtpSBCPay *sbcpay;
338 sbcpay = GST_RTP_SBC_PAY (object);
341 case PROP_MIN_FRAMES:
342 sbcpay->min_frames = g_value_get_int (value);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
352 GValue * value, GParamSpec * pspec)
354 GstRtpSBCPay *sbcpay;
356 sbcpay = GST_RTP_SBC_PAY (object);
359 case PROP_MIN_FRAMES:
360 g_value_set_int (value, sbcpay->min_frames);
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
369 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
371 self->adapter = gst_adapter_new ();
372 self->frame_length = 0;
373 self->last_timestamp = GST_CLOCK_TIME_NONE;
375 self->min_frames = DEFAULT_MIN_FRAMES;
379 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
381 return gst_element_register (plugin, "rtpsbcpay", GST_RANK_NONE,
382 GST_TYPE_RTP_SBC_PAY);