1 /* GStreamer RTP SBC payloader
2 * BlueZ - Bluetooth protocol stack for Linux
3 *
4 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gst/audio/audio.h>
27 #include "gstrtpelements.h"
28 #include "gstrtpsbcpay.h"
29 #include <math.h>
30 #include <string.h>
31 #include "gstrtputils.h"
32
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)
36
37 enum
38 {
39 PROP_0,
40 PROP_MIN_FRAMES
41 };
42
43 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
44 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
45
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));
50
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 ]")
61 );
62
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")
70 );
71
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);
78
79 static gint
gst_rtp_sbc_pay_get_frame_len(gint subbands,gint channels,gint blocks,gint bitpool,const gchar * channel_mode)80 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
81 gint blocks, gint bitpool, const gchar * channel_mode)
82 {
83 gint len;
84 gint join;
85
86 len = 4 + (4 * subbands * channels) / 8;
87
88 if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
89 len += ((blocks * channels * bitpool) + 7) / 8;
90 else {
91 join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
92 len += ((join * subbands + blocks * bitpool) + 7) / 8;
93 }
94
95 return len;
96 }
97
98 static gboolean
gst_rtp_sbc_pay_set_caps(GstRTPBasePayload * payload,GstCaps * caps)99 gst_rtp_sbc_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
100 {
101 GstRtpSBCPay *sbcpay;
102 gint rate, subbands, channels, blocks, bitpool;
103 gint frame_len;
104 const gchar *channel_mode;
105 GstStructure *structure;
106
107 sbcpay = GST_RTP_SBC_PAY (payload);
108
109 structure = gst_caps_get_structure (caps, 0);
110 if (!gst_structure_get_int (structure, "rate", &rate))
111 return FALSE;
112 if (!gst_structure_get_int (structure, "channels", &channels))
113 return FALSE;
114 if (!gst_structure_get_int (structure, "blocks", &blocks))
115 return FALSE;
116 if (!gst_structure_get_int (structure, "bitpool", &bitpool))
117 return FALSE;
118 if (!gst_structure_get_int (structure, "subbands", &subbands))
119 return FALSE;
120
121 channel_mode = gst_structure_get_string (structure, "channel-mode");
122 if (!channel_mode)
123 return FALSE;
124
125 frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
126 bitpool, channel_mode);
127
128 sbcpay->frame_length = frame_len;
129 sbcpay->frame_duration = ((blocks * subbands) * GST_SECOND) / rate;
130 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
131
132 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
133
134 GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
135
136 return gst_rtp_base_payload_set_outcaps (payload, NULL);
137 }
138
139 static GstFlowReturn
gst_rtp_sbc_pay_drain_buffers(GstRtpSBCPay * sbcpay)140 gst_rtp_sbc_pay_drain_buffers (GstRtpSBCPay * sbcpay)
141 {
142 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
143 guint available;
144 guint max_payload;
145 GstBuffer *outbuf, *paybuf;
146 guint8 *payload_data;
147 guint frame_count;
148 guint payload_length;
149 GstFlowReturn res;
150
151 if (sbcpay->frame_length == 0) {
152 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
153 return GST_FLOW_ERROR;
154 }
155
156 do {
157 available = gst_adapter_available (sbcpay->adapter);
158
159 max_payload =
160 gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
161 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
162
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 */
167 return GST_FLOW_OK;
168
169 outbuf =
170 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
171 (sbcpay), RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
172
173 /* get payload */
174 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
175
176 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
177
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;
182
183 gst_rtp_buffer_unmap (&rtp);
184
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);
188
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)));
193
194 sbcpay->last_timestamp += frame_count * sbcpay->frame_duration;
195
196 res = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
197
198 /* try to send another RTP buffer if available data exceeds MTU size */
199 } while (res == GST_FLOW_OK);
200
201 return res;
202 }
203
204 static GstFlowReturn
gst_rtp_sbc_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buffer)205 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
206 {
207 GstRtpSBCPay *sbcpay;
208 guint available;
209
210 /* FIXME check for negotiation */
211
212 sbcpay = GST_RTP_SBC_PAY (payload);
213
214 if (GST_BUFFER_IS_DISCONT (buffer)) {
215 /* Try to flush whatever's left */
216 gst_rtp_sbc_pay_drain_buffers (sbcpay);
217 /* Drop the rest */
218 gst_adapter_flush (sbcpay->adapter,
219 gst_adapter_available (sbcpay->adapter));
220 /* Reset timestamps */
221 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
222 }
223
224 if (sbcpay->last_timestamp == GST_CLOCK_TIME_NONE)
225 sbcpay->last_timestamp = GST_BUFFER_PTS (buffer);
226
227 gst_adapter_push (sbcpay->adapter, buffer);
228
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);
234
235 return GST_FLOW_OK;
236 }
237
238 static gboolean
gst_rtp_sbc_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)239 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
240 {
241 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
242
243 switch (GST_EVENT_TYPE (event)) {
244 case GST_EVENT_EOS:
245 gst_rtp_sbc_pay_drain_buffers (sbcpay);
246 break;
247 case GST_EVENT_FLUSH_STOP:
248 gst_adapter_clear (sbcpay->adapter);
249 break;
250 case GST_EVENT_SEGMENT:
251 gst_rtp_sbc_pay_drain_buffers (sbcpay);
252 break;
253 default:
254 break;
255 }
256
257 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
258 }
259
260 static GstStateChangeReturn
gst_rtp_sbc_pay_change_state(GstElement * element,GstStateChange transition)261 gst_rtp_sbc_pay_change_state (GstElement * element, GstStateChange transition)
262 {
263 GstStateChangeReturn ret;
264 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (element);
265
266 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
267
268 switch (transition) {
269 case GST_STATE_CHANGE_PAUSED_TO_READY:
270 gst_adapter_clear (sbcpay->adapter);
271 break;
272 default:
273 break;
274 }
275
276 return ret;
277 }
278
279 static void
gst_rtp_sbc_pay_finalize(GObject * object)280 gst_rtp_sbc_pay_finalize (GObject * object)
281 {
282 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
283
284 g_object_unref (sbcpay->adapter);
285
286 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
287 }
288
289 static void
gst_rtp_sbc_pay_class_init(GstRtpSBCPayClass * klass)290 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
291 {
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);
295
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;
299
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);
304
305 element_class->change_state = gst_rtp_sbc_pay_change_state;
306
307 /* properties */
308 g_object_class_install_property (G_OBJECT_CLASS (klass),
309 PROP_MIN_FRAMES,
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));
314
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);
319
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>");
323
324 GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
325 "RTP SBC payloader");
326 }
327
328 static void
gst_rtp_sbc_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)329 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
330 const GValue * value, GParamSpec * pspec)
331 {
332 GstRtpSBCPay *sbcpay;
333
334 sbcpay = GST_RTP_SBC_PAY (object);
335
336 switch (prop_id) {
337 case PROP_MIN_FRAMES:
338 sbcpay->min_frames = g_value_get_int (value);
339 break;
340 default:
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 break;
343 }
344 }
345
346 static void
gst_rtp_sbc_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)347 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
348 GValue * value, GParamSpec * pspec)
349 {
350 GstRtpSBCPay *sbcpay;
351
352 sbcpay = GST_RTP_SBC_PAY (object);
353
354 switch (prop_id) {
355 case PROP_MIN_FRAMES:
356 g_value_set_int (value, sbcpay->min_frames);
357 break;
358 default:
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 break;
361 }
362 }
363
364 static void
gst_rtp_sbc_pay_init(GstRtpSBCPay * self)365 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
366 {
367 self->adapter = gst_adapter_new ();
368 self->frame_length = 0;
369 self->last_timestamp = GST_CLOCK_TIME_NONE;
370
371 self->min_frames = DEFAULT_MIN_FRAMES;
372 }
373