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 "gstrtpelements.h"
28 #include "gstrtpsbcpay.h"
31 #include "gstrtputils.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)
43 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
44 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
46 #define parent_class gst_rtp_sbc_pay_parent_class
47 G_DEFINE_TYPE (GstRtpSBCPay, gst_rtp_sbc_pay, GST_TYPE_RTP_BASE_PAYLOAD);
48 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpsbcpay, "rtpsbcpay", GST_RANK_NONE,
49 GST_TYPE_RTP_SBC_PAY, rtp_element_init (plugin));
51 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
52 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
53 GST_STATIC_CAPS ("audio/x-sbc, "
54 "rate = (int) { 16000, 32000, 44100, 48000 }, "
55 "channels = (int) [ 1, 2 ], "
56 "channel-mode = (string) { mono, dual, stereo, joint }, "
57 "blocks = (int) { 4, 8, 12, 16 }, "
58 "subbands = (int) { 4, 8 }, "
59 "allocation-method = (string) { snr, loudness }, "
60 "bitpool = (int) [ 2, 64 ]")
63 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
64 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
65 GST_STATIC_CAPS ("application/x-rtp, "
66 "media = (string) audio,"
67 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
68 "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
69 "encoding-name = (string) SBC")
72 static void gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
76 static GstStateChangeReturn gst_rtp_sbc_pay_change_state (GstElement * element,
77 GstStateChange transition);
80 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
81 gint blocks, gint bitpool, const gchar * channel_mode)
86 len = 4 + (4 * subbands * channels) / 8;
88 if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
89 len += ((blocks * channels * bitpool) + 7) / 8;
91 join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
92 len += ((join * subbands + blocks * bitpool) + 7) / 8;
99 gst_rtp_sbc_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
101 GstRtpSBCPay *sbcpay;
102 gint rate, subbands, channels, blocks, bitpool;
104 const gchar *channel_mode;
105 GstStructure *structure;
107 sbcpay = GST_RTP_SBC_PAY (payload);
109 structure = gst_caps_get_structure (caps, 0);
110 if (!gst_structure_get_int (structure, "rate", &rate))
112 if (!gst_structure_get_int (structure, "channels", &channels))
114 if (!gst_structure_get_int (structure, "blocks", &blocks))
116 if (!gst_structure_get_int (structure, "bitpool", &bitpool))
118 if (!gst_structure_get_int (structure, "subbands", &subbands))
121 channel_mode = gst_structure_get_string (structure, "channel-mode");
125 frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
126 bitpool, channel_mode);
128 sbcpay->frame_length = frame_len;
129 sbcpay->frame_duration = ((blocks * subbands) * GST_SECOND) / rate;
130 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
132 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
134 GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
136 return gst_rtp_base_payload_set_outcaps (payload, NULL);
140 gst_rtp_sbc_pay_drain_buffers (GstRtpSBCPay * sbcpay)
142 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
145 GstBuffer *outbuf, *paybuf;
146 guint8 *payload_data;
148 guint payload_length;
151 if (sbcpay->frame_length == 0) {
152 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
153 return GST_FLOW_ERROR;
157 available = gst_adapter_available (sbcpay->adapter);
160 gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
161 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
163 max_payload = MIN (max_payload, available);
164 frame_count = max_payload / sbcpay->frame_length;
165 payload_length = frame_count * sbcpay->frame_length;
166 if (payload_length == 0) /* Nothing to send */
170 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
171 (sbcpay), RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
174 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
176 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
178 /* write header and copy data into payload */
179 payload_data = gst_rtp_buffer_get_payload (&rtp);
180 /* upper 3 fragment bits not used, ref A2DP v13, 4.3.4 */
181 payload_data[0] = frame_count & 0x0f;
183 gst_rtp_buffer_unmap (&rtp);
185 paybuf = gst_adapter_take_buffer_fast (sbcpay->adapter, payload_length);
186 gst_rtp_copy_audio_meta (sbcpay, outbuf, paybuf);
187 outbuf = gst_buffer_append (outbuf, paybuf);
189 GST_BUFFER_PTS (outbuf) = sbcpay->last_timestamp;
190 GST_BUFFER_DURATION (outbuf) = frame_count * sbcpay->frame_duration;
191 GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes: %" GST_TIME_FORMAT,
192 payload_length, GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
194 sbcpay->last_timestamp += frame_count * sbcpay->frame_duration;
196 res = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
198 /* try to send another RTP buffer if available data exceeds MTU size */
199 } while (res == GST_FLOW_OK);
205 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
207 GstRtpSBCPay *sbcpay;
210 /* FIXME check for negotiation */
212 sbcpay = GST_RTP_SBC_PAY (payload);
214 if (GST_BUFFER_IS_DISCONT (buffer)) {
215 /* Try to flush whatever's left */
216 gst_rtp_sbc_pay_drain_buffers (sbcpay);
218 gst_adapter_flush (sbcpay->adapter,
219 gst_adapter_available (sbcpay->adapter));
220 /* Reset timestamps */
221 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
224 if (sbcpay->last_timestamp == GST_CLOCK_TIME_NONE)
225 sbcpay->last_timestamp = GST_BUFFER_PTS (buffer);
227 gst_adapter_push (sbcpay->adapter, buffer);
229 available = gst_adapter_available (sbcpay->adapter);
230 if (available + RTP_SBC_HEADER_TOTAL >=
231 GST_RTP_BASE_PAYLOAD_MTU (sbcpay) ||
232 (available > (sbcpay->min_frames * sbcpay->frame_length)))
233 return gst_rtp_sbc_pay_drain_buffers (sbcpay);
239 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
241 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
243 switch (GST_EVENT_TYPE (event)) {
245 gst_rtp_sbc_pay_drain_buffers (sbcpay);
247 case GST_EVENT_FLUSH_STOP:
248 gst_adapter_clear (sbcpay->adapter);
250 case GST_EVENT_SEGMENT:
251 gst_rtp_sbc_pay_drain_buffers (sbcpay);
257 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
260 static GstStateChangeReturn
261 gst_rtp_sbc_pay_change_state (GstElement * element, GstStateChange transition)
263 GstStateChangeReturn ret;
264 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (element);
266 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
268 switch (transition) {
269 case GST_STATE_CHANGE_PAUSED_TO_READY:
270 gst_adapter_clear (sbcpay->adapter);
280 gst_rtp_sbc_pay_finalize (GObject * object)
282 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
284 g_object_unref (sbcpay->adapter);
286 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
290 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
292 GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
293 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
294 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
296 gobject_class->finalize = gst_rtp_sbc_pay_finalize;
297 gobject_class->set_property = gst_rtp_sbc_pay_set_property;
298 gobject_class->get_property = gst_rtp_sbc_pay_get_property;
300 payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
301 payload_class->handle_buffer =
302 GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
303 payload_class->sink_event = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_sink_event);
305 element_class->change_state = gst_rtp_sbc_pay_change_state;
308 g_object_class_install_property (G_OBJECT_CLASS (klass),
310 g_param_spec_int ("min-frames", "minimum frame number",
311 "Minimum quantity of frames to send in one packet "
312 "(-1 for maximum allowed by the mtu)",
313 -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
315 gst_element_class_add_static_pad_template (element_class,
316 &gst_rtp_sbc_pay_sink_factory);
317 gst_element_class_add_static_pad_template (element_class,
318 &gst_rtp_sbc_pay_src_factory);
320 gst_element_class_set_static_metadata (element_class, "RTP packet payloader",
321 "Codec/Payloader/Network", "Payload SBC audio as RTP packets",
322 "Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>");
324 GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
325 "RTP SBC payloader");
329 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
330 const GValue * value, GParamSpec * pspec)
332 GstRtpSBCPay *sbcpay;
334 sbcpay = GST_RTP_SBC_PAY (object);
337 case PROP_MIN_FRAMES:
338 sbcpay->min_frames = g_value_get_int (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
348 GValue * value, GParamSpec * pspec)
350 GstRtpSBCPay *sbcpay;
352 sbcpay = GST_RTP_SBC_PAY (object);
355 case PROP_MIN_FRAMES:
356 g_value_set_int (value, sbcpay->min_frames);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
367 self->adapter = gst_adapter_new ();
368 self->frame_length = 0;
369 self->last_timestamp = GST_CLOCK_TIME_NONE;
371 self->min_frames = DEFAULT_MIN_FRAMES;