2 * Copyright (C) <2007> Nokia Corporation (contact <stefan.kost@nokia.com>)
3 * <2007> Wim Taymans <wim.taymans@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpmp4adepay.h"
29 GST_DEBUG_CATEGORY_STATIC (rtpmp4adepay_debug);
30 #define GST_CAT_DEFAULT (rtpmp4adepay_debug)
32 /* elementfactory information */
33 static const GstElementDetails gst_rtp_mp4adepay_details =
34 GST_ELEMENT_DETAILS ("RTP MPEG4 audio depayloader",
35 "Codec/Depayloader/Network",
36 "Extracts MPEG4 audio from RTP packets (RFC 3016)",
37 "Nokia Corporation (contact <stefan.kost@nokia.com>), "
38 "Wim Taymans <wim.taymans@gmail.com>");
40 static GstStaticPadTemplate gst_rtp_mp4a_depay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("audio/mpeg,"
45 "mpegversion = (int) 4," "framed = (boolean) true, "
46 "stream-format = (string) raw")
49 static GstStaticPadTemplate gst_rtp_mp4a_depay_sink_template =
50 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 "media = (string) \"audio\", "
55 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 "clock-rate = (int) [1, MAX ], "
57 "encoding-name = (string) \"MP4A-LATM\""
58 /* All optional parameters
60 * "profile-level-id=[1,MAX]"
66 GST_BOILERPLATE (GstRtpMP4ADepay, gst_rtp_mp4a_depay, GstBaseRTPDepayload,
67 GST_TYPE_BASE_RTP_DEPAYLOAD);
69 static void gst_rtp_mp4a_depay_finalize (GObject * object);
71 static gboolean gst_rtp_mp4a_depay_setcaps (GstBaseRTPDepayload * depayload,
73 static GstBuffer *gst_rtp_mp4a_depay_process (GstBaseRTPDepayload * depayload,
76 static GstStateChangeReturn gst_rtp_mp4a_depay_change_state (GstElement *
77 element, GstStateChange transition);
81 gst_rtp_mp4a_depay_base_init (gpointer klass)
83 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85 gst_element_class_add_pad_template (element_class,
86 gst_static_pad_template_get (&gst_rtp_mp4a_depay_src_template));
87 gst_element_class_add_pad_template (element_class,
88 gst_static_pad_template_get (&gst_rtp_mp4a_depay_sink_template));
90 gst_element_class_set_details (element_class, &gst_rtp_mp4adepay_details);
94 gst_rtp_mp4a_depay_class_init (GstRtpMP4ADepayClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
98 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
102 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
104 gobject_class->finalize = gst_rtp_mp4a_depay_finalize;
106 gstelement_class->change_state = gst_rtp_mp4a_depay_change_state;
108 gstbasertpdepayload_class->process = gst_rtp_mp4a_depay_process;
109 gstbasertpdepayload_class->set_caps = gst_rtp_mp4a_depay_setcaps;
111 GST_DEBUG_CATEGORY_INIT (rtpmp4adepay_debug, "rtpmp4adepay", 0,
112 "MPEG4 audio RTP Depayloader");
116 gst_rtp_mp4a_depay_init (GstRtpMP4ADepay * rtpmp4adepay,
117 GstRtpMP4ADepayClass * klass)
119 rtpmp4adepay->adapter = gst_adapter_new ();
123 gst_rtp_mp4a_depay_finalize (GObject * object)
125 GstRtpMP4ADepay *rtpmp4adepay;
127 rtpmp4adepay = GST_RTP_MP4A_DEPAY (object);
129 g_object_unref (rtpmp4adepay->adapter);
130 rtpmp4adepay->adapter = NULL;
132 G_OBJECT_CLASS (parent_class)->finalize (object);
136 gst_rtp_mp4a_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
138 GstStructure *structure;
139 GstRtpMP4ADepay *rtpmp4adepay;
144 gint channels = 2; /* default */
147 rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
149 structure = gst_caps_get_structure (caps, 0);
151 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
152 clock_rate = 90000; /* default */
153 depayload->clock_rate = clock_rate;
155 if (!gst_structure_get_int (structure, "object", &object_type))
156 object_type = 2; /* AAC LC default */
158 srccaps = gst_caps_new_simple ("audio/mpeg",
159 "mpegversion", G_TYPE_INT, 4,
160 "framed", G_TYPE_BOOLEAN, TRUE, "channels", G_TYPE_INT, channels,
161 "stream-format", G_TYPE_STRING, "raw", NULL);
163 if ((str = gst_structure_get_string (structure, "config"))) {
166 g_value_init (&v, GST_TYPE_BUFFER);
167 if (gst_value_deserialize (&v, str)) {
173 buffer = gst_value_get_buffer (&v);
174 gst_buffer_ref (buffer);
177 data = GST_BUFFER_DATA (buffer);
178 size = GST_BUFFER_SIZE (buffer);
181 GST_WARNING_OBJECT (depayload, "config too short (%d < 2)", size);
185 /* Parse StreamMuxConfig according to ISO/IEC 14496-3:
187 * audioMuxVersion == 0 (1 bit)
188 * allStreamsSameTimeFraming == 1 (1 bit)
189 * numSubFrames == rtpmp4adepay->numSubFrames (6 bits)
190 * numProgram == 0 (4 bits)
191 * numLayer == 0 (3 bits)
193 * We only require audioMuxVersion == 0;
195 * The remaining bit of the second byte and the rest of the bits are used
196 * for audioSpecificConfig which we need to set in codec_info.
198 if ((data[0] & 0x80) != 0x00) {
199 GST_WARNING_OBJECT (depayload, "unknown audioMuxVersion 1");
203 rtpmp4adepay->numSubFrames = (data[0] & 0x3F);
205 GST_LOG_OBJECT (rtpmp4adepay, "numSubFrames %d",
206 rtpmp4adepay->numSubFrames);
208 /* shift rest of string 15 bits down */
210 for (i = 0; i < size; i++) {
211 data[i] = ((data[i + 1] & 1) << 7) | ((data[i + 2] & 0xfe) >> 1);
214 /* ignore remaining bit, we're only interested in full bytes */
215 GST_BUFFER_SIZE (buffer) = size;
217 gst_caps_set_simple (srccaps,
218 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
219 gst_buffer_unref (buffer);
221 g_warning ("cannot convert config to buffer");
225 res = gst_pad_set_caps (depayload->srcpad, srccaps);
226 gst_caps_unref (srccaps);
232 gst_rtp_mp4a_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
234 GstRtpMP4ADepay *rtpmp4adepay;
237 rtpmp4adepay = GST_RTP_MP4A_DEPAY (depayload);
239 /* flush remaining data on discont */
240 if (GST_BUFFER_IS_DISCONT (buf)) {
241 gst_adapter_clear (rtpmp4adepay->adapter);
244 outbuf = gst_rtp_buffer_get_payload_buffer (buf);
246 gst_adapter_push (rtpmp4adepay->adapter, outbuf);
248 /* RTP marker bit indicates the last packet of the AudioMuxElement => create
249 * and push a buffer */
250 if (gst_rtp_buffer_get_marker (buf)) {
256 avail = gst_adapter_available (rtpmp4adepay->adapter);
258 GST_LOG_OBJECT (rtpmp4adepay, "have marker and %u available", avail);
260 outbuf = gst_adapter_take_buffer (rtpmp4adepay->adapter, avail);
261 data = GST_BUFFER_DATA (outbuf);
262 /* position in data we are at */
265 /* looping through the number of sub-frames in the audio payload */
266 for (i = 0; i <= rtpmp4adepay->numSubFrames; i++) {
267 /* determine payload length and set buffer data pointer accordingly */
272 GstBuffer *tmp = NULL;
274 timestamp = gst_rtp_buffer_get_timestamp (buf);
276 /* each subframe starts with a variable length encoding */
278 for (skip = 0; skip < avail; skip++) {
279 data_len += data[skip];
280 if (data[skip] != 0xff)
285 /* this can not be possible, we have not enough data or the length
286 * decoding failed because we ran out of data. */
287 if (skip + data_len > avail)
290 GST_LOG_OBJECT (rtpmp4adepay,
291 "subframe %u, header len %u, data len %u, left %u", i, skip, data_len,
294 /* take data out, skip the header */
296 tmp = gst_buffer_create_sub (outbuf, pos, data_len);
302 /* update our pointers whith what we consumed */
306 gst_buffer_set_caps (tmp, GST_PAD_CAPS (depayload->srcpad));
308 /* only apply the timestamp for the first buffer. Based on gstrtpmp4gdepay.c */
310 gst_base_rtp_depayload_push_ts (depayload, timestamp, tmp);
312 gst_base_rtp_depayload_push (depayload, tmp);
315 /* just a check that lengths match */
317 GST_ELEMENT_WARNING (depayload, STREAM, DECODE,
318 ("Packet invalid"), ("Not all payload consumed: "
319 "possible wrongly encoded packet."));
322 gst_buffer_unref (outbuf);
329 GST_ELEMENT_WARNING (rtpmp4adepay, STREAM, DECODE,
330 ("Packet did not validate"), ("wrong packet size"));
331 gst_buffer_unref (outbuf);
336 static GstStateChangeReturn
337 gst_rtp_mp4a_depay_change_state (GstElement * element,
338 GstStateChange transition)
340 GstRtpMP4ADepay *rtpmp4adepay;
341 GstStateChangeReturn ret;
343 rtpmp4adepay = GST_RTP_MP4A_DEPAY (element);
345 switch (transition) {
346 case GST_STATE_CHANGE_READY_TO_PAUSED:
347 gst_adapter_clear (rtpmp4adepay->adapter);
353 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
355 switch (transition) {
363 gst_rtp_mp4a_depay_plugin_init (GstPlugin * plugin)
365 return gst_element_register (plugin, "rtpmp4adepay",
366 GST_RANK_MARGINAL, GST_TYPE_RTP_MP4A_DEPAY);