2 * Copyright (C) <2008> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmp4apay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4apay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4apay_debug)
33 /* FIXME: add framed=(boolean)true once our encoders have this field set
34 * on their output caps */
35 static GstStaticPadTemplate gst_rtp_mp4a_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("audio/mpeg, mpegversion=(int)4, "
40 "stream-format=(string)raw")
43 static GstStaticPadTemplate gst_rtp_mp4a_pay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
47 GST_STATIC_CAPS ("application/x-rtp, "
48 "media = (string) \"audio\", "
49 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
50 "clock-rate = (int) [1, MAX ], "
51 "encoding-name = (string) \"MP4A-LATM\""
52 /* All optional parameters
54 * "cpresent = (string) \"0\""
60 static void gst_rtp_mp4a_pay_finalize (GObject * object);
62 static gboolean gst_rtp_mp4a_pay_setcaps (GstRTPBasePayload * payload,
64 static GstFlowReturn gst_rtp_mp4a_pay_handle_buffer (GstRTPBasePayload *
65 payload, GstBuffer * buffer);
67 #define gst_rtp_mp4a_pay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpMP4APay, gst_rtp_mp4a_pay, GST_TYPE_RTP_BASE_PAYLOAD)
70 static void gst_rtp_mp4a_pay_class_init (GstRtpMP4APayClass * klass)
72 GObjectClass *gobject_class;
73 GstElementClass *gstelement_class;
74 GstRTPBasePayloadClass *gstrtpbasepayload_class;
76 gobject_class = (GObjectClass *) klass;
77 gstelement_class = (GstElementClass *) klass;
78 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
80 gobject_class->finalize = gst_rtp_mp4a_pay_finalize;
82 gstrtpbasepayload_class->set_caps = gst_rtp_mp4a_pay_setcaps;
83 gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4a_pay_handle_buffer;
85 gst_element_class_add_pad_template (gstelement_class,
86 gst_static_pad_template_get (&gst_rtp_mp4a_pay_src_template));
87 gst_element_class_add_pad_template (gstelement_class,
88 gst_static_pad_template_get (&gst_rtp_mp4a_pay_sink_template));
90 gst_element_class_set_details_simple (gstelement_class,
91 "RTP MPEG4 audio payloader", "Codec/Payloader/Network/RTP",
92 "Payload MPEG4 audio as RTP packets (RFC 3016)",
93 "Wim Taymans <wim.taymans@gmail.com>");
95 GST_DEBUG_CATEGORY_INIT (rtpmp4apay_debug, "rtpmp4apay", 0,
96 "MP4A-LATM RTP Payloader");
100 gst_rtp_mp4a_pay_init (GstRtpMP4APay * rtpmp4apay)
102 rtpmp4apay->rate = 90000;
103 rtpmp4apay->profile = g_strdup ("1");
107 gst_rtp_mp4a_pay_finalize (GObject * object)
109 GstRtpMP4APay *rtpmp4apay;
111 rtpmp4apay = GST_RTP_MP4A_PAY (object);
113 g_free (rtpmp4apay->params);
114 rtpmp4apay->params = NULL;
116 if (rtpmp4apay->config)
117 gst_buffer_unref (rtpmp4apay->config);
118 rtpmp4apay->config = NULL;
120 g_free (rtpmp4apay->profile);
121 rtpmp4apay->profile = NULL;
123 G_OBJECT_CLASS (parent_class)->finalize (object);
126 static const unsigned int sampling_table[16] = {
127 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
128 16000, 12000, 11025, 8000, 7350, 0, 0, 0
132 gst_rtp_mp4a_pay_parse_audio_config (GstRtpMP4APay * rtpmp4apay,
142 gst_buffer_map (buffer, &map, GST_MAP_READ);
149 /* any object type is fine, we need to copy it to the profile-level-id field. */
150 objectType = (data[0] & 0xf8) >> 3;
154 samplingIdx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
155 /* only fixed values for now */
156 if (samplingIdx > 12 && samplingIdx != 15)
159 channelCfg = ((data[1] & 0x78) >> 3);
163 /* rtp rate depends on sampling rate of the audio */
164 if (samplingIdx == 15) {
168 /* index of 15 means we get the rate in the next 24 bits */
169 rtpmp4apay->rate = ((data[1] & 0x7f) << 17) |
170 ((data[2]) << 9) | ((data[3]) << 1) | ((data[4] & 0x80) >> 7);
172 /* else use the rate from the table */
173 rtpmp4apay->rate = sampling_table[samplingIdx];
175 /* extra rtp params contain the number of channels */
176 g_free (rtpmp4apay->params);
177 rtpmp4apay->params = g_strdup_printf ("%d", channelCfg);
178 /* audio stream type */
179 rtpmp4apay->streamtype = "5";
181 g_free (rtpmp4apay->profile);
182 rtpmp4apay->profile = g_strdup_printf ("%d", objectType);
184 GST_DEBUG_OBJECT (rtpmp4apay,
185 "objectType: %d, samplingIdx: %d (%d), channelCfg: %d", objectType,
186 samplingIdx, rtpmp4apay->rate, channelCfg);
188 gst_buffer_unmap (buffer, &map);
195 GST_ELEMENT_ERROR (rtpmp4apay, STREAM, FORMAT,
197 ("config string too short, expected 2 bytes, got %" G_GSIZE_FORMAT,
199 gst_buffer_unmap (buffer, &map);
204 GST_ELEMENT_ERROR (rtpmp4apay, STREAM, FORMAT,
205 (NULL), ("invalid object type 0"));
206 gst_buffer_unmap (buffer, &map);
211 GST_ELEMENT_ERROR (rtpmp4apay, STREAM, NOT_IMPLEMENTED,
212 (NULL), ("unsupported frequency index %d", samplingIdx));
213 gst_buffer_unmap (buffer, &map);
218 GST_ELEMENT_ERROR (rtpmp4apay, STREAM, NOT_IMPLEMENTED,
219 (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
220 gst_buffer_unmap (buffer, &map);
226 gst_rtp_mp4a_pay_new_caps (GstRtpMP4APay * rtpmp4apay)
232 g_value_init (&v, GST_TYPE_BUFFER);
233 gst_value_set_buffer (&v, rtpmp4apay->config);
234 config = gst_value_serialize (&v);
236 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4apay),
237 "cpresent", G_TYPE_STRING, "0", "config", G_TYPE_STRING, config, NULL);
246 gst_rtp_mp4a_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
248 GstRtpMP4APay *rtpmp4apay;
249 GstStructure *structure;
250 const GValue *codec_data;
251 gboolean res, framed = TRUE;
252 const gchar *stream_format;
254 rtpmp4apay = GST_RTP_MP4A_PAY (payload);
256 structure = gst_caps_get_structure (caps, 0);
258 /* this is already handled by the template caps, but it is better
259 * to leave here to have meaningful warning messages when linking
261 stream_format = gst_structure_get_string (structure, "stream-format");
263 if (strcmp (stream_format, "raw") != 0) {
264 GST_WARNING_OBJECT (rtpmp4apay, "AAC's stream-format must be 'raw', "
265 "%s is not supported", stream_format);
269 GST_WARNING_OBJECT (rtpmp4apay, "AAC's stream-format not specified, "
273 codec_data = gst_structure_get_value (structure, "codec_data");
275 GST_LOG_OBJECT (rtpmp4apay, "got codec_data");
276 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
277 GstBuffer *buffer, *cbuffer;
282 buffer = gst_value_get_buffer (codec_data);
283 GST_LOG_OBJECT (rtpmp4apay, "configuring codec_data");
286 res = gst_rtp_mp4a_pay_parse_audio_config (rtpmp4apay, buffer);
291 gst_buffer_map (buffer, &map, GST_MAP_READ);
293 /* make the StreamMuxConfig, we need 15 bits for the header */
294 cbuffer = gst_buffer_new_and_alloc (map.size + 2);
295 gst_buffer_map (cbuffer, &cmap, GST_MAP_WRITE);
297 /* Create StreamMuxConfig according to ISO/IEC 14496-3:
299 * audioMuxVersion == 0 (1 bit)
300 * allStreamsSameTimeFraming == 1 (1 bit)
301 * numSubFrames == numSubFrames (6 bits)
302 * numProgram == 0 (4 bits)
303 * numLayer == 0 (3 bits)
308 /* append the config bits, shifting them 1 bit left */
309 for (i = 0; i < map.size; i++) {
310 cmap.data[i + 1] |= ((map.data[i] & 0x80) >> 7);
311 cmap.data[i + 2] |= ((map.data[i] & 0x7f) << 1);
314 gst_buffer_unmap (cbuffer, &cmap);
315 gst_buffer_unmap (buffer, &map);
317 /* now we can configure the buffer */
318 if (rtpmp4apay->config)
319 gst_buffer_unref (rtpmp4apay->config);
320 rtpmp4apay->config = cbuffer;
324 if (gst_structure_get_boolean (structure, "framed", &framed) && !framed) {
325 GST_WARNING_OBJECT (payload, "Need framed AAC data as input!");
328 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "MP4A-LATM",
331 res = gst_rtp_mp4a_pay_new_caps (rtpmp4apay);
338 GST_DEBUG_OBJECT (rtpmp4apay, "failed to parse config");
343 /* we expect buffers as exactly one complete AU
346 gst_rtp_mp4a_pay_handle_buffer (GstRTPBasePayload * basepayload,
349 GstRtpMP4APay *rtpmp4apay;
357 GstClockTime timestamp;
361 rtpmp4apay = GST_RTP_MP4A_PAY (basepayload);
363 gst_buffer_map (buffer, &map, GST_MAP_READ);
367 timestamp = GST_BUFFER_TIMESTAMP (buffer);
370 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4apay);
377 GstRTPBuffer rtp = { NULL };
379 /* this will be the total lenght of the packet */
380 packet_len = gst_rtp_buffer_calc_packet_len (size, 0, 0);
383 /* first packet calculate space for the packet including the header */
385 while (count >= 0xff) {
392 /* fill one MTU or all available bytes */
393 towrite = MIN (packet_len, mtu);
395 /* this is the payload length */
396 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
398 GST_DEBUG_OBJECT (rtpmp4apay,
399 "avail %" G_GSIZE_FORMAT ", towrite %d, packet_len %d, payload_len %d",
400 size, towrite, packet_len, payload_len);
402 /* create buffer to hold the payload. */
403 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
406 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
407 payload = gst_rtp_buffer_get_payload (&rtp);
410 /* first packet write the header */
412 while (count >= 0xff) {
421 /* copy data to payload */
422 memcpy (payload, data, payload_len);
426 /* marker only if the packet is complete */
427 gst_rtp_buffer_set_marker (&rtp, size == 0);
429 gst_rtp_buffer_unmap (&rtp);
431 /* copy incomming timestamp (if any) to outgoing buffers */
432 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
434 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp4apay), outbuf);
439 gst_buffer_unmap (buffer, &map);
440 gst_buffer_unref (buffer);
446 gst_rtp_mp4a_pay_plugin_init (GstPlugin * plugin)
448 return gst_element_register (plugin, "rtpmp4apay",
449 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4A_PAY);