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 "gstrtpsbcpay.h"
28 #include <math.h>
29 #include <string.h>
30 #include "gstrtputils.h"
31
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)
35
36 /* BEGIN: Packing for rtp_payload */
37 #ifdef _MSC_VER
38 #pragma pack(push, 1)
39 #endif
40
41 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
42 /* FIXME: this seems all a bit over the top for a single byte.. */
43 struct rtp_payload
44 {
45 guint8 frame_count:4;
46 guint8 rfa0:1;
47 guint8 is_last_fragment:1;
48 guint8 is_first_fragment:1;
49 guint8 is_fragmented:1;
50 }
51 #elif G_BYTE_ORDER == G_BIG_ENDIAN
52 struct rtp_payload
53 {
54 guint8 is_fragmented:1;
55 guint8 is_first_fragment:1;
56 guint8 is_last_fragment:1;
57 guint8 rfa0:1;
58 guint8 frame_count:4;
59 }
60 #else
61 #error "Unknown byte order"
62 #endif
63
64 #ifdef _MSC_VER
65 ;
66 #pragma pack(pop)
67 #else
68 __attribute__ ((packed));
69 #endif
70 /* END: Packing for rtp_payload */
71
72 enum
73 {
74 PROP_0,
75 PROP_MIN_FRAMES
76 };
77
78 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
79 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
80
81 #define parent_class gst_rtp_sbc_pay_parent_class
82 G_DEFINE_TYPE (GstRtpSBCPay, gst_rtp_sbc_pay, GST_TYPE_RTP_BASE_PAYLOAD);
83
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 ]")
94 );
95
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")
103 );
104
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);
109
110 static gint
gst_rtp_sbc_pay_get_frame_len(gint subbands,gint channels,gint blocks,gint bitpool,const gchar * channel_mode)111 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
112 gint blocks, gint bitpool, const gchar * channel_mode)
113 {
114 gint len;
115 gint join;
116
117 len = 4 + (4 * subbands * channels) / 8;
118
119 if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
120 len += ((blocks * channels * bitpool) + 7) / 8;
121 else {
122 join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
123 len += ((join * subbands + blocks * bitpool) + 7) / 8;
124 }
125
126 return len;
127 }
128
129 static gboolean
gst_rtp_sbc_pay_set_caps(GstRTPBasePayload * payload,GstCaps * caps)130 gst_rtp_sbc_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
131 {
132 GstRtpSBCPay *sbcpay;
133 gint rate, subbands, channels, blocks, bitpool;
134 gint frame_len;
135 const gchar *channel_mode;
136 GstStructure *structure;
137
138 sbcpay = GST_RTP_SBC_PAY (payload);
139
140 structure = gst_caps_get_structure (caps, 0);
141 if (!gst_structure_get_int (structure, "rate", &rate))
142 return FALSE;
143 if (!gst_structure_get_int (structure, "channels", &channels))
144 return FALSE;
145 if (!gst_structure_get_int (structure, "blocks", &blocks))
146 return FALSE;
147 if (!gst_structure_get_int (structure, "bitpool", &bitpool))
148 return FALSE;
149 if (!gst_structure_get_int (structure, "subbands", &subbands))
150 return FALSE;
151
152 channel_mode = gst_structure_get_string (structure, "channel-mode");
153 if (!channel_mode)
154 return FALSE;
155
156 frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
157 bitpool, channel_mode);
158
159 sbcpay->frame_length = frame_len;
160 sbcpay->frame_duration = ((blocks * subbands) * GST_SECOND) / rate;
161 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
162
163 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
164
165 GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
166
167 return gst_rtp_base_payload_set_outcaps (payload, NULL);
168 }
169
170 static GstFlowReturn
gst_rtp_sbc_pay_flush_buffers(GstRtpSBCPay * sbcpay)171 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
172 {
173 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
174 guint available;
175 guint max_payload;
176 GstBuffer *outbuf, *paybuf;
177 guint8 *payload_data;
178 guint frame_count;
179 guint payload_length;
180 struct rtp_payload *payload;
181 GstFlowReturn res;
182
183 if (sbcpay->frame_length == 0) {
184 GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
185 return GST_FLOW_ERROR;
186 }
187
188 do {
189 available = gst_adapter_available (sbcpay->adapter);
190
191 max_payload =
192 gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
193 RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
194
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 */
199 return GST_FLOW_OK;
200
201 outbuf = gst_rtp_buffer_new_allocate (RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
202
203 /* get payload */
204 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
205
206 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
207
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;
213
214 gst_rtp_buffer_unmap (&rtp);
215
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);
219
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)));
224
225 sbcpay->last_timestamp += frame_count * sbcpay->frame_duration;
226
227 res = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
228
229 /* try to send another RTP buffer if available data exceeds MTU size */
230 } while (res == GST_FLOW_OK);
231
232 return res;
233 }
234
235 static GstFlowReturn
gst_rtp_sbc_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buffer)236 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
237 {
238 GstRtpSBCPay *sbcpay;
239 guint available;
240
241 /* FIXME check for negotiation */
242
243 sbcpay = GST_RTP_SBC_PAY (payload);
244
245 if (GST_BUFFER_IS_DISCONT (buffer)) {
246 /* Try to flush whatever's left */
247 gst_rtp_sbc_pay_flush_buffers (sbcpay);
248 /* Drop the rest */
249 gst_adapter_flush (sbcpay->adapter,
250 gst_adapter_available (sbcpay->adapter));
251 /* Reset timestamps */
252 sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
253 }
254
255 if (sbcpay->last_timestamp == GST_CLOCK_TIME_NONE)
256 sbcpay->last_timestamp = GST_BUFFER_PTS (buffer);
257
258 gst_adapter_push (sbcpay->adapter, buffer);
259
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);
265
266 return GST_FLOW_OK;
267 }
268
269 static gboolean
gst_rtp_sbc_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)270 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
271 {
272 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
273
274 switch (GST_EVENT_TYPE (event)) {
275 case GST_EVENT_EOS:
276 gst_rtp_sbc_pay_flush_buffers (sbcpay);
277 break;
278 default:
279 break;
280 }
281
282 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
283 }
284
285 static void
gst_rtp_sbc_pay_finalize(GObject * object)286 gst_rtp_sbc_pay_finalize (GObject * object)
287 {
288 GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
289
290 g_object_unref (sbcpay->adapter);
291
292 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
293 }
294
295 static void
gst_rtp_sbc_pay_class_init(GstRtpSBCPayClass * klass)296 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
297 {
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);
301
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;
305
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);
310
311 /* properties */
312 g_object_class_install_property (G_OBJECT_CLASS (klass),
313 PROP_MIN_FRAMES,
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));
318
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);
323
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>");
327
328 GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
329 "RTP SBC payloader");
330 }
331
332 static void
gst_rtp_sbc_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)333 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
334 const GValue * value, GParamSpec * pspec)
335 {
336 GstRtpSBCPay *sbcpay;
337
338 sbcpay = GST_RTP_SBC_PAY (object);
339
340 switch (prop_id) {
341 case PROP_MIN_FRAMES:
342 sbcpay->min_frames = g_value_get_int (value);
343 break;
344 default:
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 break;
347 }
348 }
349
350 static void
gst_rtp_sbc_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)351 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
352 GValue * value, GParamSpec * pspec)
353 {
354 GstRtpSBCPay *sbcpay;
355
356 sbcpay = GST_RTP_SBC_PAY (object);
357
358 switch (prop_id) {
359 case PROP_MIN_FRAMES:
360 g_value_set_int (value, sbcpay->min_frames);
361 break;
362 default:
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 break;
365 }
366 }
367
368 static void
gst_rtp_sbc_pay_init(GstRtpSBCPay * self)369 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
370 {
371 self->adapter = gst_adapter_new ();
372 self->frame_length = 0;
373 self->last_timestamp = GST_CLOCK_TIME_NONE;
374
375 self->min_frames = DEFAULT_MIN_FRAMES;
376 }
377
378 gboolean
gst_rtp_sbc_pay_plugin_init(GstPlugin * plugin)379 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
380 {
381 return gst_element_register (plugin, "rtpsbcpay", GST_RANK_NONE,
382 GST_TYPE_RTP_SBC_PAY);
383 }
384