2f01b21b595cf08866a7e9570d078a442c18aa7d
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4apay.c
1 /* GStreamer
2  * Copyright (C) <2008> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpmp4apay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpmp4apay_debug);
33 #define GST_CAT_DEFAULT (rtpmp4apay_debug)
34
35 /* FIXME: add framed=(boolean)true once our encoders have this field set
36  * on their output caps */
37 static GstStaticPadTemplate gst_rtp_mp4a_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
39     GST_PAD_SINK,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("audio/mpeg, mpegversion=(int)4, "
42         "stream-format=(string)raw")
43     );
44
45 static GstStaticPadTemplate gst_rtp_mp4a_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"audio\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) [1, MAX ], "
53         "encoding-name = (string) \"MP4A-LATM\""
54         /* All optional parameters
55          *
56          * "cpresent = (string) \"0\""
57          * "config=" 
58          */
59     )
60     );
61
62 static void gst_rtp_mp4a_pay_finalize (GObject * object);
63
64 static gboolean gst_rtp_mp4a_pay_setcaps (GstRTPBasePayload * payload,
65     GstCaps * caps);
66 static GstFlowReturn gst_rtp_mp4a_pay_handle_buffer (GstRTPBasePayload *
67     payload, GstBuffer * buffer);
68
69 #define gst_rtp_mp4a_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpMP4APay, gst_rtp_mp4a_pay, GST_TYPE_RTP_BASE_PAYLOAD);
71
72 static void
73 gst_rtp_mp4a_pay_class_init (GstRtpMP4APayClass * klass)
74 {
75   GObjectClass *gobject_class;
76   GstElementClass *gstelement_class;
77   GstRTPBasePayloadClass *gstrtpbasepayload_class;
78
79   gobject_class = (GObjectClass *) klass;
80   gstelement_class = (GstElementClass *) klass;
81   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
82
83   gobject_class->finalize = gst_rtp_mp4a_pay_finalize;
84
85   gstrtpbasepayload_class->set_caps = gst_rtp_mp4a_pay_setcaps;
86   gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4a_pay_handle_buffer;
87
88   gst_element_class_add_static_pad_template (gstelement_class,
89       &gst_rtp_mp4a_pay_src_template);
90   gst_element_class_add_static_pad_template (gstelement_class,
91       &gst_rtp_mp4a_pay_sink_template);
92
93   gst_element_class_set_static_metadata (gstelement_class,
94       "RTP MPEG4 audio payloader", "Codec/Payloader/Network/RTP",
95       "Payload MPEG4 audio as RTP packets (RFC 3016)",
96       "Wim Taymans <wim.taymans@gmail.com>");
97
98   GST_DEBUG_CATEGORY_INIT (rtpmp4apay_debug, "rtpmp4apay", 0,
99       "MP4A-LATM RTP Payloader");
100 }
101
102 static void
103 gst_rtp_mp4a_pay_init (GstRtpMP4APay * rtpmp4apay)
104 {
105   rtpmp4apay->rate = 90000;
106   rtpmp4apay->profile = g_strdup ("1");
107 }
108
109 static void
110 gst_rtp_mp4a_pay_finalize (GObject * object)
111 {
112   GstRtpMP4APay *rtpmp4apay;
113
114   rtpmp4apay = GST_RTP_MP4A_PAY (object);
115
116   g_free (rtpmp4apay->params);
117   rtpmp4apay->params = NULL;
118
119   if (rtpmp4apay->config)
120     gst_buffer_unref (rtpmp4apay->config);
121   rtpmp4apay->config = NULL;
122
123   g_free (rtpmp4apay->profile);
124   rtpmp4apay->profile = NULL;
125
126   G_OBJECT_CLASS (parent_class)->finalize (object);
127 }
128
129 static const unsigned int sampling_table[16] = {
130   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
131   16000, 12000, 11025, 8000, 7350, 0, 0, 0
132 };
133
134 static gboolean
135 gst_rtp_mp4a_pay_parse_audio_config (GstRtpMP4APay * rtpmp4apay,
136     GstBuffer * buffer)
137 {
138   GstMapInfo map;
139   guint8 *data;
140   gsize size;
141   guint8 objectType;
142   guint8 samplingIdx;
143   guint8 channelCfg;
144
145   gst_buffer_map (buffer, &map, GST_MAP_READ);
146   data = map.data;
147   size = map.size;
148
149   if (size < 2)
150     goto too_short;
151
152   /* any object type is fine, we need to copy it to the profile-level-id field. */
153   objectType = (data[0] & 0xf8) >> 3;
154   if (objectType == 0)
155     goto invalid_object;
156
157   samplingIdx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
158   /* only fixed values for now */
159   if (samplingIdx > 12 && samplingIdx != 15)
160     goto wrong_freq;
161
162   channelCfg = ((data[1] & 0x78) >> 3);
163   if (channelCfg > 7)
164     goto wrong_channels;
165
166   /* rtp rate depends on sampling rate of the audio */
167   if (samplingIdx == 15) {
168     if (size < 5)
169       goto too_short;
170
171     /* index of 15 means we get the rate in the next 24 bits */
172     rtpmp4apay->rate = ((data[1] & 0x7f) << 17) |
173         ((data[2]) << 9) | ((data[3]) << 1) | ((data[4] & 0x80) >> 7);
174   } else {
175     /* else use the rate from the table */
176     rtpmp4apay->rate = sampling_table[samplingIdx];
177   }
178   /* extra rtp params contain the number of channels */
179   g_free (rtpmp4apay->params);
180   rtpmp4apay->params = g_strdup_printf ("%d", channelCfg);
181   /* audio stream type */
182   rtpmp4apay->streamtype = "5";
183   /* profile */
184   g_free (rtpmp4apay->profile);
185   rtpmp4apay->profile = g_strdup_printf ("%d", objectType);
186
187   GST_DEBUG_OBJECT (rtpmp4apay,
188       "objectType: %d, samplingIdx: %d (%d), channelCfg: %d", objectType,
189       samplingIdx, rtpmp4apay->rate, channelCfg);
190
191   gst_buffer_unmap (buffer, &map);
192
193   return TRUE;
194
195   /* ERROR */
196 too_short:
197   {
198     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, FORMAT,
199         (NULL),
200         ("config string too short, expected 2 bytes, got %" G_GSIZE_FORMAT,
201             size));
202     gst_buffer_unmap (buffer, &map);
203     return FALSE;
204   }
205 invalid_object:
206   {
207     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, FORMAT,
208         (NULL), ("invalid object type 0"));
209     gst_buffer_unmap (buffer, &map);
210     return FALSE;
211   }
212 wrong_freq:
213   {
214     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, NOT_IMPLEMENTED,
215         (NULL), ("unsupported frequency index %d", samplingIdx));
216     gst_buffer_unmap (buffer, &map);
217     return FALSE;
218   }
219 wrong_channels:
220   {
221     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, NOT_IMPLEMENTED,
222         (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
223     gst_buffer_unmap (buffer, &map);
224     return FALSE;
225   }
226 }
227
228 static gboolean
229 gst_rtp_mp4a_pay_new_caps (GstRtpMP4APay * rtpmp4apay)
230 {
231   gchar *config;
232   GValue v = { 0 };
233   gboolean res;
234
235   g_value_init (&v, GST_TYPE_BUFFER);
236   gst_value_set_buffer (&v, rtpmp4apay->config);
237   config = gst_value_serialize (&v);
238
239   res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4apay),
240       "cpresent", G_TYPE_STRING, "0", "config", G_TYPE_STRING, config, NULL);
241
242   g_value_unset (&v);
243   g_free (config);
244
245   return res;
246 }
247
248 static gboolean
249 gst_rtp_mp4a_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
250 {
251   GstRtpMP4APay *rtpmp4apay;
252   GstStructure *structure;
253   const GValue *codec_data;
254   gboolean res, framed = TRUE;
255   const gchar *stream_format;
256
257   rtpmp4apay = GST_RTP_MP4A_PAY (payload);
258
259   structure = gst_caps_get_structure (caps, 0);
260
261   /* this is already handled by the template caps, but it is better
262    * to leave here to have meaningful warning messages when linking
263    * fails */
264   stream_format = gst_structure_get_string (structure, "stream-format");
265   if (stream_format) {
266     if (strcmp (stream_format, "raw") != 0) {
267       GST_WARNING_OBJECT (rtpmp4apay, "AAC's stream-format must be 'raw', "
268           "%s is not supported", stream_format);
269       return FALSE;
270     }
271   } else {
272     GST_WARNING_OBJECT (rtpmp4apay, "AAC's stream-format not specified, "
273         "assuming 'raw'");
274   }
275
276   codec_data = gst_structure_get_value (structure, "codec_data");
277   if (codec_data) {
278     GST_LOG_OBJECT (rtpmp4apay, "got codec_data");
279     if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
280       GstBuffer *buffer, *cbuffer;
281       GstMapInfo map;
282       GstMapInfo cmap;
283       guint i;
284
285       buffer = gst_value_get_buffer (codec_data);
286       GST_LOG_OBJECT (rtpmp4apay, "configuring codec_data");
287
288       /* parse buffer */
289       res = gst_rtp_mp4a_pay_parse_audio_config (rtpmp4apay, buffer);
290
291       if (!res)
292         goto config_failed;
293
294       gst_buffer_map (buffer, &map, GST_MAP_READ);
295
296       /* make the StreamMuxConfig, we need 15 bits for the header */
297       cbuffer = gst_buffer_new_and_alloc (map.size + 2);
298       gst_buffer_map (cbuffer, &cmap, GST_MAP_WRITE);
299
300       memset (cmap.data, 0, map.size + 2);
301
302       /* Create StreamMuxConfig according to ISO/IEC 14496-3:
303        *
304        * audioMuxVersion           == 0 (1 bit)
305        * allStreamsSameTimeFraming == 1 (1 bit)
306        * numSubFrames              == numSubFrames (6 bits)
307        * numProgram                == 0 (4 bits)
308        * numLayer                  == 0 (3 bits)
309        */
310       cmap.data[0] = 0x40;
311       cmap.data[1] = 0x00;
312
313       /* append the config bits, shifting them 1 bit left */
314       for (i = 0; i < map.size; i++) {
315         cmap.data[i + 1] |= ((map.data[i] & 0x80) >> 7);
316         cmap.data[i + 2] |= ((map.data[i] & 0x7f) << 1);
317       }
318
319       gst_buffer_unmap (cbuffer, &cmap);
320       gst_buffer_unmap (buffer, &map);
321
322       /* now we can configure the buffer */
323       if (rtpmp4apay->config)
324         gst_buffer_unref (rtpmp4apay->config);
325       rtpmp4apay->config = cbuffer;
326     }
327   }
328
329   if (gst_structure_get_boolean (structure, "framed", &framed) && !framed) {
330     GST_WARNING_OBJECT (payload, "Need framed AAC data as input!");
331   }
332
333   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "MP4A-LATM",
334       rtpmp4apay->rate);
335
336   res = gst_rtp_mp4a_pay_new_caps (rtpmp4apay);
337
338   return res;
339
340   /* ERRORS */
341 config_failed:
342   {
343     GST_DEBUG_OBJECT (rtpmp4apay, "failed to parse config");
344     return FALSE;
345   }
346 }
347
348 #define RTP_HEADER_LEN 12
349
350 /* we expect buffers as exactly one complete AU
351  */
352 static GstFlowReturn
353 gst_rtp_mp4a_pay_handle_buffer (GstRTPBasePayload * basepayload,
354     GstBuffer * buffer)
355 {
356   GstRtpMP4APay *rtpmp4apay;
357   GstFlowReturn ret;
358   GstBufferList *list;
359   guint mtu;
360   guint offset;
361   gsize size;
362   gboolean fragmented;
363   GstClockTime timestamp;
364
365   ret = GST_FLOW_OK;
366
367   rtpmp4apay = GST_RTP_MP4A_PAY (basepayload);
368
369   offset = 0;
370   size = gst_buffer_get_size (buffer);
371
372   timestamp = GST_BUFFER_PTS (buffer);
373
374   fragmented = FALSE;
375   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4apay);
376
377   list = gst_buffer_list_new_sized (size / (mtu - RTP_HEADER_LEN) + 1);
378
379   while (size > 0) {
380     guint towrite;
381     GstBuffer *outbuf;
382     guint payload_len;
383     guint packet_len;
384     guint header_len;
385     GstBuffer *paybuf;
386     GstRTPBuffer rtp = { NULL };
387
388     header_len = 0;
389     if (!fragmented) {
390       guint count;
391       /* first packet calculate space for the packet including the header */
392       count = size;
393       while (count >= 0xff) {
394         header_len++;
395         count -= 0xff;
396       }
397       header_len++;
398     }
399
400     packet_len = gst_rtp_buffer_calc_packet_len (header_len + size, 0, 0);
401     towrite = MIN (packet_len, mtu);
402     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
403     payload_len -= header_len;
404
405     GST_DEBUG_OBJECT (rtpmp4apay,
406         "avail %" G_GSIZE_FORMAT
407         ", header_len %d, packet_len %d, payload_len %d", size, header_len,
408         packet_len, payload_len);
409
410     /* create buffer to hold the payload. */
411     outbuf = gst_rtp_buffer_new_allocate (header_len, 0, 0);
412
413     /* copy payload */
414     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
415
416     if (!fragmented) {
417       guint8 *payload = gst_rtp_buffer_get_payload (&rtp);
418       guint count;
419
420       /* first packet write the header */
421       count = size;
422       while (count >= 0xff) {
423         *payload++ = 0xff;
424         count -= 0xff;
425       }
426       *payload++ = count;
427     }
428
429     /* marker only if the packet is complete */
430     gst_rtp_buffer_set_marker (&rtp, size == payload_len);
431
432     gst_rtp_buffer_unmap (&rtp);
433
434     /* create a new buf to hold the payload */
435     paybuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL,
436         offset, payload_len);
437
438     /* join memory parts */
439     gst_rtp_copy_audio_meta (rtpmp4apay, outbuf, paybuf);
440     outbuf = gst_buffer_append (outbuf, paybuf);
441     gst_buffer_list_add (list, outbuf);
442     offset += payload_len;
443     size -= payload_len;
444
445     /* copy incoming timestamp (if any) to outgoing buffers */
446     GST_BUFFER_PTS (outbuf) = timestamp;
447
448     fragmented = TRUE;
449   }
450
451   ret =
452       gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4apay), list);
453
454   gst_buffer_unref (buffer);
455
456   return ret;
457 }
458
459 gboolean
460 gst_rtp_mp4a_pay_plugin_init (GstPlugin * plugin)
461 {
462   return gst_element_register (plugin, "rtpmp4apay",
463       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4A_PAY);
464 }