2 * GStreamer RTP SBC depayloader
4 * Copyright (C) 2012 Collabora Ltd.
5 * @author: Arun Raghavan <arun.raghavan@collabora.co.uk>
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.
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.
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.
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/audio/audio.h>
29 #include "gstrtpelements.h"
30 #include "gstrtpsbcdepay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpsbcdepay_debug);
34 #define GST_CAT_DEFAULT (rtpsbcdepay_debug)
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 ]")
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")
60 PROP_IGNORE_TIMESTAMPS,
64 #define DEFAULT_IGNORE_TIMESTAMPS FALSE
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));
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);
77 static gboolean gst_rtp_sbc_depay_setcaps (GstRTPBaseDepayload * base,
79 static GstBuffer *gst_rtp_sbc_depay_process (GstRTPBaseDepayload * base,
83 gst_rtp_sbc_depay_class_init (GstRtpSbcDepayClass * klass)
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);
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;
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));
99 gstbasertpdepayload_class->set_caps = gst_rtp_sbc_depay_setcaps;
100 gstbasertpdepayload_class->process_rtp_packet = gst_rtp_sbc_depay_process;
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);
107 GST_DEBUG_CATEGORY_INIT (rtpsbcdepay_debug, "rtpsbcdepay", 0,
108 "SBC Audio RTP Depayloader");
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>");
118 gst_rtp_sbc_depay_init (GstRtpSbcDepay * rtpsbcdepay)
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;
127 gst_rtp_sbc_depay_finalize (GObject * object)
129 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (object);
131 gst_audio_stream_align_free (depay->stream_align);
132 gst_object_unref (depay->adapter);
134 G_OBJECT_CLASS (parent_class)->finalize (object);
138 gst_rtp_sbc_depay_set_property (GObject * object,
139 guint prop_id, const GValue * value, GParamSpec * pspec)
141 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (object);
144 case PROP_IGNORE_TIMESTAMPS:
145 depay->ignore_timestamps = g_value_get_boolean (value);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 gst_rtp_sbc_depay_get_property (GObject * object,
155 guint prop_id, GValue * value, GParamSpec * pspec)
157 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (object);
160 case PROP_IGNORE_TIMESTAMPS:
161 g_value_set_boolean (value, depay->ignore_timestamps);
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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
174 gst_rtp_sbc_depay_get_params (GstRtpSbcDepay * depay, const guint8 * data,
175 gint size, int *framelen, int *samples)
177 int blocks, channel_mode, channels, subbands, bitpool;
181 /* Not enough data for the header */
186 if (data[0] != 0x9c) {
187 GST_WARNING_OBJECT (depay, "Bad packet: couldn't find syncword");
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;
199 length = 4 + ((4 * subbands * channels) / 8);
201 if (channel_mode == 0 || channel_mode == 1) {
202 /* Mono || Dual channel */
203 length += ((blocks * channels * bitpool)
204 + 4 /* round up */ ) / 8;
206 /* Stereo || Joint stereo */
207 gboolean joint = (channel_mode == 3);
209 length += ((joint * subbands) + (blocks * bitpool)
210 + 4 /* round up */ ) / 8;
214 *samples = blocks * subbands;
220 gst_rtp_sbc_depay_setcaps (GstRTPBaseDepayload * base, GstCaps * caps)
222 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (base);
223 GstStructure *structure;
224 GstCaps *outcaps, *oldcaps;
226 structure = gst_caps_get_structure (caps, 0);
228 if (!gst_structure_get_int (structure, "clock-rate", &depay->rate))
231 outcaps = gst_caps_new_simple ("audio/x-sbc", "rate", G_TYPE_INT,
234 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (base), outcaps);
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);
242 gst_caps_unref (outcaps);
244 gst_caps_unref (oldcaps);
246 /* Reset when the caps are changing */
247 gst_audio_stream_align_set_rate (depay->stream_align, depay->rate);
252 GST_WARNING_OBJECT (depay, "Can't support the caps we got: %"
253 GST_PTR_FORMAT, caps);
258 gst_rtp_sbc_depay_process (GstRTPBaseDepayload * base, GstRTPBuffer * rtp)
260 GstRtpSbcDepay *depay = GST_RTP_SBC_DEPAY (base);
261 GstBuffer *data = NULL;
263 gboolean fragment, start, last;
269 GstClockTime timestamp;
271 GST_LOG_OBJECT (depay, "Got %" G_GSIZE_FORMAT " bytes",
272 gst_buffer_get_size (rtp->buffer));
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");
280 timestamp = GST_BUFFER_DTS_OR_PTS (rtp->buffer);
281 if (depay->ignore_timestamps && timestamp == GST_CLOCK_TIME_NONE) {
282 GstClockTime initial_timestamp;
286 gst_audio_stream_align_get_timestamp_at_discont (depay->stream_align);
288 gst_audio_stream_align_get_samples_since_discont (depay->stream_align);
290 if (initial_timestamp == GST_CLOCK_TIME_NONE) {
291 GST_ERROR_OBJECT (depay,
292 "Can only ignore timestamps on streams without valid initial timestamp");
297 initial_timestamp + gst_util_uint64_scale (n_samples, GST_SECOND,
301 payload = gst_rtp_buffer_get_payload (rtp);
302 payload_len = gst_rtp_buffer_get_payload_len (rtp);
304 fragment = payload[0] & 0x80;
305 start = payload[0] & 0x40;
306 last = payload[0] & 0x20;
307 nframes = payload[0] & 0x0f;
312 data = gst_rtp_buffer_get_payload_subbuffer (rtp, 1, -1);
315 /* Got a packet with a fragment */
316 GST_LOG_OBJECT (depay, "Got fragment");
318 if (start && gst_adapter_available (depay->adapter)) {
319 GST_WARNING_OBJECT (depay, "Missing last fragment");
320 gst_adapter_clear (depay->adapter);
322 } else if (!start && !gst_adapter_available (depay->adapter)) {
323 GST_WARNING_OBJECT (depay, "Missing start fragment");
324 gst_buffer_unref (data);
329 gst_adapter_push (depay->adapter, data);
332 gint framelen, samples;
335 data = gst_adapter_take_buffer (depay->adapter,
336 gst_adapter_available (depay->adapter));
337 gst_rtp_drop_non_audio_meta (depay, data);
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);
352 GST_LOG_OBJECT (depay, "Got %d frames", nframes);
354 if (gst_rtp_sbc_depay_get_params (depay, payload,
355 payload_len, &framelen, &samples) < 0) {
356 gst_adapter_clear (depay->adapter);
362 GST_LOG_OBJECT (depay, "Got payload of %d", payload_len);
364 if (nframes * framelen > (gint) payload_len) {
365 GST_WARNING_OBJECT (depay, "Short packet");
367 } else if (nframes * framelen < (gint) payload_len) {
368 GST_WARNING_OBJECT (depay, "Junk at end of packet");
372 if (depay->ignore_timestamps && data) {
373 GstClockTime duration;
375 gst_audio_stream_align_process (depay->stream_align,
376 GST_BUFFER_IS_DISCONT (rtp->buffer), timestamp, samples, ×tamp,
379 GST_BUFFER_PTS (data) = timestamp;
380 GST_BUFFER_DTS (data) = GST_CLOCK_TIME_NONE;
381 GST_BUFFER_DURATION (data) = duration;
388 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
389 ("Received invalid RTP payload, dropping"), (NULL));