1 /*
2 * GStreamer RTP SBC depayloader
3 *
4 * Copyright (C) 2012 Collabora Ltd.
5 * @author: Arun Raghavan <arun.raghavan@collabora.co.uk>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/audio/audio.h>
29 #include "gstrtpelements.h"
30 #include "gstrtpsbcdepay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpsbcdepay_debug);
34 #define GST_CAT_DEFAULT (rtpsbcdepay_debug)
35
36 static GstStaticPadTemplate gst_rtp_sbc_depay_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
38 GST_STATIC_CAPS ("audio/x-sbc, "
39 "rate = (int) { 16000, 32000, 44100, 48000 }, "
40 "channels = (int) [ 1, 2 ], "
41 "mode = (string) { mono, dual, stereo, joint }, "
42 "blocks = (int) { 4, 8, 12, 16 }, "
43 "subbands = (int) { 4, 8 }, "
44 "allocation-method = (string) { snr, loudness }, "
45 "bitpool = (int) [ 2, 64 ]")
46 );
47
48 static GstStaticPadTemplate gst_rtp_sbc_depay_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
50 GST_STATIC_CAPS ("application/x-rtp, "
51 "media = (string) audio,"
52 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53 "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
54 "encoding-name = (string) SBC")
55 );
56
57 enum
58 {
59 PROP_0,
60 PROP_IGNORE_TIMESTAMPS,
61 PROP_LAST
62 };
63
64 #define DEFAULT_IGNORE_TIMESTAMPS FALSE
65
66 #define gst_rtp_sbc_depay_parent_class parent_class
67 G_DEFINE_TYPE (GstRtpSbcDepay, gst_rtp_sbc_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
68 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpsbcdepay, "rtpsbcdepay",
69 GST_RANK_SECONDARY, GST_TYPE_RTP_SBC_DEPAY, rtp_element_init (plugin));
70
71 static void gst_rtp_sbc_depay_set_property (GObject * object,
72 guint prop_id, const GValue * value, GParamSpec * pspec);
73 static void gst_rtp_sbc_depay_get_property (GObject * object,
74 guint prop_id, GValue * value, GParamSpec * pspec);
75 static void gst_rtp_sbc_depay_finalize (GObject * object);
76
77 static gboolean gst_rtp_sbc_depay_setcaps (GstRTPBaseDepayload * base,
78 GstCaps * caps);
79 static GstBuffer *gst_rtp_sbc_depay_process (GstRTPBaseDepayload * base,
80 GstRTPBuffer * rtp);
81
82 static void
gst_rtp_sbc_depay_class_init(GstRtpSbcDepayClass * klass)83 gst_rtp_sbc_depay_class_init (GstRtpSbcDepayClass * klass)
84 {
85 GstRTPBaseDepayloadClass *gstbasertpdepayload_class =
86 GST_RTP_BASE_DEPAYLOAD_CLASS (klass);
87 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
88 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89
90 gobject_class->finalize = gst_rtp_sbc_depay_finalize;
91 gobject_class->set_property = gst_rtp_sbc_depay_set_property;
92 gobject_class->get_property = gst_rtp_sbc_depay_get_property;
93
94 g_object_class_install_property (gobject_class, PROP_IGNORE_TIMESTAMPS,
95 g_param_spec_boolean ("ignore-timestamps", "Ignore Timestamps",
96 "Various statistics", DEFAULT_IGNORE_TIMESTAMPS,
97 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
98
99 gstbasertpdepayload_class->set_caps = gst_rtp_sbc_depay_setcaps;
100 gstbasertpdepayload_class->process_rtp_packet = gst_rtp_sbc_depay_process;
101
102 gst_element_class_add_static_pad_template (element_class,
103 &gst_rtp_sbc_depay_src_template);
104 gst_element_class_add_static_pad_template (element_class,
105 &gst_rtp_sbc_depay_sink_template);
106
107 GST_DEBUG_CATEGORY_INIT (rtpsbcdepay_debug, "rtpsbcdepay", 0,
108 "SBC Audio RTP Depayloader");
109
110 gst_element_class_set_static_metadata (element_class,
111 "RTP SBC audio depayloader",
112 "Codec/Depayloader/Network/RTP",
113 "Extracts SBC audio from RTP packets",
114 "Arun Raghavan <arun.raghavan@collabora.co.uk>");
115 }
116
117 static void
gst_rtp_sbc_depay_init(GstRtpSbcDepay * rtpsbcdepay)118 gst_rtp_sbc_depay_init (GstRtpSbcDepay * rtpsbcdepay)
119 {
120 rtpsbcdepay->adapter = gst_adapter_new ();
121 rtpsbcdepay->stream_align =
122 gst_audio_stream_align_new (48000, 40 * GST_MSECOND, 1 * GST_SECOND);
123 rtpsbcdepay->ignore_timestamps = DEFAULT_IGNORE_TIMESTAMPS;
124 }
125
126 static void
gst_rtp_sbc_depay_finalize(GObject * object)127 gst_rtp_sbc_depay_finalize (GObject * object)
128 {
129 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (object);
130
131 gst_audio_stream_align_free (depay->stream_align);
132 gst_object_unref (depay->adapter);
133
134 G_OBJECT_CLASS (parent_class)->finalize (object);
135 }
136
137 static void
gst_rtp_sbc_depay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)138 gst_rtp_sbc_depay_set_property (GObject * object,
139 guint prop_id, const GValue * value, GParamSpec * pspec)
140 {
141 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (object);
142
143 switch (prop_id) {
144 case PROP_IGNORE_TIMESTAMPS:
145 depay->ignore_timestamps = g_value_get_boolean (value);
146 break;
147 default:
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 break;
150 }
151 }
152
153 static void
gst_rtp_sbc_depay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)154 gst_rtp_sbc_depay_get_property (GObject * object,
155 guint prop_id, GValue * value, GParamSpec * pspec)
156 {
157 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (object);
158
159 switch (prop_id) {
160 case PROP_IGNORE_TIMESTAMPS:
161 g_value_set_boolean (value, depay->ignore_timestamps);
162 break;
163 default:
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 break;
166 }
167 }
168
169 /* FIXME: This duplicates similar functionality rtpsbcpay, but there isn't a
170 * simple way to consolidate the two. This is best done by moving the function
171 * to the codec-utils library in gst-plugins-base when these elements move to
172 * GStreamer. */
173 static int
gst_rtp_sbc_depay_get_params(GstRtpSbcDepay * depay,const guint8 * data,gint size,int * framelen,int * samples)174 gst_rtp_sbc_depay_get_params (GstRtpSbcDepay * depay, const guint8 * data,
175 gint size, int *framelen, int *samples)
176 {
177 int blocks, channel_mode, channels, subbands, bitpool;
178 int length;
179
180 if (size < 3) {
181 /* Not enough data for the header */
182 return -1;
183 }
184
185 /* Sanity check */
186 if (data[0] != 0x9c) {
187 GST_WARNING_OBJECT (depay, "Bad packet: couldn't find syncword");
188 return -2;
189 }
190
191 blocks = (data[1] >> 4) & 0x3;
192 blocks = (blocks + 1) * 4;
193 channel_mode = (data[1] >> 2) & 0x3;
194 channels = channel_mode ? 2 : 1;
195 subbands = (data[1] & 0x1);
196 subbands = (subbands + 1) * 4;
197 bitpool = data[2];
198
199 length = 4 + ((4 * subbands * channels) / 8);
200
201 if (channel_mode == 0 || channel_mode == 1) {
202 /* Mono || Dual channel */
203 length += ((blocks * channels * bitpool)
204 + 4 /* round up */ ) / 8;
205 } else {
206 /* Stereo || Joint stereo */
207 gboolean joint = (channel_mode == 3);
208
209 length += ((joint * subbands) + (blocks * bitpool)
210 + 4 /* round up */ ) / 8;
211 }
212
213 *framelen = length;
214 *samples = blocks * subbands;
215
216 return 0;
217 }
218
219 static gboolean
gst_rtp_sbc_depay_setcaps(GstRTPBaseDepayload * base,GstCaps * caps)220 gst_rtp_sbc_depay_setcaps (GstRTPBaseDepayload * base, GstCaps * caps)
221 {
222 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (base);
223 GstStructure *structure;
224 GstCaps *outcaps, *oldcaps;
225
226 structure = gst_caps_get_structure (caps, 0);
227
228 if (!gst_structure_get_int (structure, "clock-rate", &depay->rate))
229 goto bad_caps;
230
231 outcaps = gst_caps_new_simple ("audio/x-sbc", "rate", G_TYPE_INT,
232 depay->rate, NULL);
233
234 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (base), outcaps);
235
236 oldcaps = gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SINKPAD (base));
237 if (oldcaps && !gst_caps_can_intersect (oldcaps, caps)) {
238 /* Caps have changed, flush old data */
239 gst_adapter_clear (depay->adapter);
240 }
241
242 gst_caps_unref (outcaps);
243 if (oldcaps)
244 gst_caps_unref (oldcaps);
245
246 /* Reset when the caps are changing */
247 gst_audio_stream_align_set_rate (depay->stream_align, depay->rate);
248
249 return TRUE;
250
251 bad_caps:
252 GST_WARNING_OBJECT (depay, "Can't support the caps we got: %"
253 GST_PTR_FORMAT, caps);
254 return FALSE;
255 }
256
257 static GstBuffer *
gst_rtp_sbc_depay_process(GstRTPBaseDepayload * base,GstRTPBuffer * rtp)258 gst_rtp_sbc_depay_process (GstRTPBaseDepayload * base, GstRTPBuffer * rtp)
259 {
260 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (base);
261 GstBuffer *data = NULL;
262
263 gboolean fragment, start, last;
264 guint8 nframes;
265 guint8 *payload;
266 guint payload_len;
267 gint samples = 0;
268
269 GstClockTime timestamp;
270
271 GST_LOG_OBJECT (depay, "Got %" G_GSIZE_FORMAT " bytes",
272 gst_buffer_get_size (rtp->buffer));
273
274 if (gst_rtp_buffer_get_marker (rtp)) {
275 /* Marker isn't supposed to be set */
276 GST_WARNING_OBJECT (depay, "Marker bit was set");
277 goto bad_packet;
278 }
279
280 timestamp = GST_BUFFER_DTS_OR_PTS (rtp->buffer);
281 if (depay->ignore_timestamps && timestamp == GST_CLOCK_TIME_NONE) {
282 GstClockTime initial_timestamp;
283 guint64 n_samples;
284
285 initial_timestamp =
286 gst_audio_stream_align_get_timestamp_at_discont (depay->stream_align);
287 n_samples =
288 gst_audio_stream_align_get_samples_since_discont (depay->stream_align);
289
290 if (initial_timestamp == GST_CLOCK_TIME_NONE) {
291 GST_ERROR_OBJECT (depay,
292 "Can only ignore timestamps on streams without valid initial timestamp");
293 return NULL;
294 }
295
296 timestamp =
297 initial_timestamp + gst_util_uint64_scale (n_samples, GST_SECOND,
298 depay->rate);
299 }
300
301 payload = gst_rtp_buffer_get_payload (rtp);
302 payload_len = gst_rtp_buffer_get_payload_len (rtp);
303
304 fragment = payload[0] & 0x80;
305 start = payload[0] & 0x40;
306 last = payload[0] & 0x20;
307 nframes = payload[0] & 0x0f;
308
309 payload += 1;
310 payload_len -= 1;
311
312 data = gst_rtp_buffer_get_payload_subbuffer (rtp, 1, -1);
313
314 if (fragment) {
315 /* Got a packet with a fragment */
316 GST_LOG_OBJECT (depay, "Got fragment");
317
318 if (start && gst_adapter_available (depay->adapter)) {
319 GST_WARNING_OBJECT (depay, "Missing last fragment");
320 gst_adapter_clear (depay->adapter);
321
322 } else if (!start && !gst_adapter_available (depay->adapter)) {
323 GST_WARNING_OBJECT (depay, "Missing start fragment");
324 gst_buffer_unref (data);
325 data = NULL;
326 goto out;
327 }
328
329 gst_adapter_push (depay->adapter, data);
330
331 if (last) {
332 gint framelen, samples;
333 guint8 header[4];
334
335 data = gst_adapter_take_buffer (depay->adapter,
336 gst_adapter_available (depay->adapter));
337 gst_rtp_drop_non_audio_meta (depay, data);
338
339 if (gst_buffer_extract (data, 0, &header, 4) != 4 ||
340 gst_rtp_sbc_depay_get_params (depay, header,
341 payload_len, &framelen, &samples) < 0) {
342 gst_buffer_unref (data);
343 goto bad_packet;
344 }
345 } else {
346 data = NULL;
347 }
348 } else {
349 /* !fragment */
350 gint framelen;
351
352 GST_LOG_OBJECT (depay, "Got %d frames", nframes);
353
354 if (gst_rtp_sbc_depay_get_params (depay, payload,
355 payload_len, &framelen, &samples) < 0) {
356 gst_adapter_clear (depay->adapter);
357 goto bad_packet;
358 }
359
360 samples *= nframes;
361
362 GST_LOG_OBJECT (depay, "Got payload of %d", payload_len);
363
364 if (nframes * framelen > (gint) payload_len) {
365 GST_WARNING_OBJECT (depay, "Short packet");
366 goto bad_packet;
367 } else if (nframes * framelen < (gint) payload_len) {
368 GST_WARNING_OBJECT (depay, "Junk at end of packet");
369 }
370 }
371
372 if (depay->ignore_timestamps && data) {
373 GstClockTime duration;
374
375 gst_audio_stream_align_process (depay->stream_align,
376 GST_BUFFER_IS_DISCONT (rtp->buffer), timestamp, samples, ×tamp,
377 &duration, NULL);
378
379 GST_BUFFER_PTS (data) = timestamp;
380 GST_BUFFER_DTS (data) = GST_CLOCK_TIME_NONE;
381 GST_BUFFER_DURATION (data) = duration;
382 }
383
384 out:
385 return data;
386
387 bad_packet:
388 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
389 ("Received invalid RTP payload, dropping"), (NULL));
390 goto out;
391 }
392