rtp: use boilerplate
[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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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
28 #include "gstrtpmp4apay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4apay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4apay_debug)
32
33 /* elementfactory information */
34 static const GstElementDetails gst_rtp_mp4apay_details =
35 GST_ELEMENT_DETAILS ("RTP MPEG4 audio payloader",
36     "Codec/Payloader/Network",
37     "Payload MPEG4 audio as RTP packets (RFC 3016)",
38     "Wim Taymans <wim.taymans@gmail.com>");
39
40 /* FIXME: add framed=(boolean)true once our encoders have this field set
41  * on their output caps */
42 static GstStaticPadTemplate gst_rtp_mp4a_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("audio/mpeg, mpegversion=(int)4")
47     );
48
49 static GstStaticPadTemplate gst_rtp_mp4a_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
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
59          *
60          * "cpresent = (string) \"0\""
61          * "config=" 
62          */
63     )
64     );
65
66 static void gst_rtp_mp4a_pay_finalize (GObject * object);
67
68 static gboolean gst_rtp_mp4a_pay_setcaps (GstBaseRTPPayload * payload,
69     GstCaps * caps);
70 static GstFlowReturn gst_rtp_mp4a_pay_handle_buffer (GstBaseRTPPayload *
71     payload, GstBuffer * buffer);
72
73 GST_BOILERPLATE (GstRtpMP4APay, gst_rtp_mp4a_pay, GstBaseRTPPayload,
74     GST_TYPE_BASE_RTP_PAYLOAD)
75
76      static void gst_rtp_mp4a_pay_base_init (gpointer klass)
77 {
78   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
79
80   gst_element_class_add_pad_template (element_class,
81       gst_static_pad_template_get (&gst_rtp_mp4a_pay_src_template));
82   gst_element_class_add_pad_template (element_class,
83       gst_static_pad_template_get (&gst_rtp_mp4a_pay_sink_template));
84
85   gst_element_class_set_details (element_class, &gst_rtp_mp4apay_details);
86 }
87
88 static void
89 gst_rtp_mp4a_pay_class_init (GstRtpMP4APayClass * klass)
90 {
91   GObjectClass *gobject_class;
92   GstBaseRTPPayloadClass *gstbasertppayload_class;
93
94   gobject_class = (GObjectClass *) klass;
95   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
96
97   gobject_class->finalize = gst_rtp_mp4a_pay_finalize;
98
99   gstbasertppayload_class->set_caps = gst_rtp_mp4a_pay_setcaps;
100   gstbasertppayload_class->handle_buffer = gst_rtp_mp4a_pay_handle_buffer;
101
102   GST_DEBUG_CATEGORY_INIT (rtpmp4apay_debug, "rtpmp4apay", 0,
103       "MP4A-LATM RTP Payloader");
104 }
105
106 static void
107 gst_rtp_mp4a_pay_init (GstRtpMP4APay * rtpmp4apay, GstRtpMP4APayClass * klass)
108 {
109   rtpmp4apay->rate = 90000;
110   rtpmp4apay->profile = g_strdup ("1");
111 }
112
113 static void
114 gst_rtp_mp4a_pay_finalize (GObject * object)
115 {
116   GstRtpMP4APay *rtpmp4apay;
117
118   rtpmp4apay = GST_RTP_MP4A_PAY (object);
119
120   g_free (rtpmp4apay->params);
121   rtpmp4apay->params = NULL;
122
123   if (rtpmp4apay->config)
124     gst_buffer_unref (rtpmp4apay->config);
125   rtpmp4apay->config = NULL;
126
127   g_free (rtpmp4apay->profile);
128   rtpmp4apay->profile = NULL;
129
130   G_OBJECT_CLASS (parent_class)->finalize (object);
131 }
132
133 static unsigned sampling_table[16] = {
134   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
135   16000, 12000, 11025, 8000, 7350, 0, 0, 0
136 };
137
138 static gboolean
139 gst_rtp_mp4a_pay_parse_audio_config (GstRtpMP4APay * rtpmp4apay,
140     GstBuffer * buffer)
141 {
142   guint8 *data;
143   guint size;
144   guint8 objectType;
145   guint8 samplingIdx;
146   guint8 channelCfg;
147
148   data = GST_BUFFER_DATA (buffer);
149   size = GST_BUFFER_SIZE (buffer);
150
151   if (size < 2)
152     goto too_short;
153
154   /* any object type is fine, we need to copy it to the profile-level-id field. */
155   objectType = (data[0] & 0xf8) >> 3;
156   if (objectType == 0)
157     goto invalid_object;
158
159   samplingIdx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
160   /* only fixed values for now */
161   if (samplingIdx > 12 && samplingIdx != 15)
162     goto wrong_freq;
163
164   channelCfg = ((data[1] & 0x78) >> 3);
165   if (channelCfg > 7)
166     goto wrong_channels;
167
168   /* rtp rate depends on sampling rate of the audio */
169   if (samplingIdx == 15) {
170     if (size < 5)
171       goto too_short;
172
173     /* index of 15 means we get the rate in the next 24 bits */
174     rtpmp4apay->rate = ((data[1] & 0x7f) << 17) |
175         ((data[2]) << 9) | ((data[3]) << 1) | ((data[4] & 0x80) >> 7);
176   } else {
177     /* else use the rate from the table */
178     rtpmp4apay->rate = sampling_table[samplingIdx];
179   }
180   /* extra rtp params contain the number of channels */
181   g_free (rtpmp4apay->params);
182   rtpmp4apay->params = g_strdup_printf ("%d", channelCfg);
183   /* audio stream type */
184   rtpmp4apay->streamtype = "5";
185   /* profile */
186   g_free (rtpmp4apay->profile);
187   rtpmp4apay->profile = g_strdup_printf ("%d", objectType);
188
189   GST_DEBUG_OBJECT (rtpmp4apay,
190       "objectType: %d, samplingIdx: %d (%d), channelCfg: %d", objectType,
191       samplingIdx, rtpmp4apay->rate, channelCfg);
192
193   return TRUE;
194
195   /* ERROR */
196 too_short:
197   {
198     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, FORMAT,
199         (NULL), ("config string too short, expected 2 bytes, got %d", size));
200     return FALSE;
201   }
202 invalid_object:
203   {
204     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, FORMAT,
205         (NULL), ("invalid object type 0"));
206     return FALSE;
207   }
208 wrong_freq:
209   {
210     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, NOT_IMPLEMENTED,
211         (NULL), ("unsupported frequency index %d", samplingIdx));
212     return FALSE;
213   }
214 wrong_channels:
215   {
216     GST_ELEMENT_ERROR (rtpmp4apay, STREAM, NOT_IMPLEMENTED,
217         (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
218     return FALSE;
219   }
220 }
221
222 static gboolean
223 gst_rtp_mp4a_pay_new_caps (GstRtpMP4APay * rtpmp4apay)
224 {
225   gchar *config;
226   GValue v = { 0 };
227   gboolean res;
228
229   g_value_init (&v, GST_TYPE_BUFFER);
230   gst_value_set_buffer (&v, rtpmp4apay->config);
231   config = gst_value_serialize (&v);
232
233   res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4apay),
234       "cpresent", G_TYPE_STRING, "0", "config", G_TYPE_STRING, config, NULL);
235
236   g_value_unset (&v);
237   g_free (config);
238
239   return res;
240 }
241
242 static gboolean
243 gst_rtp_mp4a_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
244 {
245   GstRtpMP4APay *rtpmp4apay;
246   GstStructure *structure;
247   const GValue *codec_data;
248   gboolean res, framed = TRUE;
249
250   rtpmp4apay = GST_RTP_MP4A_PAY (payload);
251
252   structure = gst_caps_get_structure (caps, 0);
253
254   codec_data = gst_structure_get_value (structure, "codec_data");
255   if (codec_data) {
256     GST_LOG_OBJECT (rtpmp4apay, "got codec_data");
257     if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
258       GstBuffer *buffer, *cbuffer;
259       guint8 *config;
260       guint8 *data;
261       guint size, i;
262
263       buffer = gst_value_get_buffer (codec_data);
264       GST_LOG_OBJECT (rtpmp4apay, "configuring codec_data");
265
266       /* parse buffer */
267       res = gst_rtp_mp4a_pay_parse_audio_config (rtpmp4apay, buffer);
268
269       if (!res)
270         goto config_failed;
271
272       size = GST_BUFFER_SIZE (buffer);
273       data = GST_BUFFER_DATA (buffer);
274
275       /* make the StreamMuxConfig, we need 15 bits for the header */
276       config = g_malloc0 (size + 2);
277
278       /* Create StreamMuxConfig according to ISO/IEC 14496-3:
279        *
280        * audioMuxVersion           == 0 (1 bit)
281        * allStreamsSameTimeFraming == 1 (1 bit)
282        * numSubFrames              == numSubFrames (6 bits)
283        * numProgram                == 0 (4 bits)
284        * numLayer                  == 0 (3 bits)
285        */
286       config[0] = 0x40;
287       config[1] = 0x00;
288
289       /* append the config bits, shifting them 1 bit left */
290       for (i = 0; i < size; i++) {
291         config[i + 1] |= ((data[i] & 0x80) >> 7);
292         config[i + 2] |= ((data[i] & 0x7f) << 1);
293       }
294
295       cbuffer = gst_buffer_new ();
296       GST_BUFFER_DATA (cbuffer) = config;
297       GST_BUFFER_MALLOCDATA (cbuffer) = config;
298       GST_BUFFER_SIZE (cbuffer) = size + 2;
299
300       /* now we can configure the buffer */
301       if (rtpmp4apay->config)
302         gst_buffer_unref (rtpmp4apay->config);
303       rtpmp4apay->config = cbuffer;
304     }
305   }
306
307   if (gst_structure_get_boolean (structure, "framed", &framed) && !framed) {
308     GST_WARNING_OBJECT (payload, "Need framed AAC data as input!");
309   }
310
311   gst_basertppayload_set_options (payload, "audio", TRUE, "MP4A-LATM",
312       rtpmp4apay->rate);
313
314   res = gst_rtp_mp4a_pay_new_caps (rtpmp4apay);
315
316   return res;
317
318   /* ERRORS */
319 config_failed:
320   {
321     GST_DEBUG_OBJECT (rtpmp4apay, "failed to parse config");
322     return FALSE;
323   }
324 }
325
326 /* we expect buffers as exactly one complete AU
327  */
328 static GstFlowReturn
329 gst_rtp_mp4a_pay_handle_buffer (GstBaseRTPPayload * basepayload,
330     GstBuffer * buffer)
331 {
332   GstRtpMP4APay *rtpmp4apay;
333   GstFlowReturn ret;
334   GstBuffer *outbuf;
335   guint count, mtu, size;
336   guint8 *data;
337   gboolean fragmented;
338   GstClockTime timestamp;
339
340   ret = GST_FLOW_OK;
341
342   rtpmp4apay = GST_RTP_MP4A_PAY (basepayload);
343
344   size = GST_BUFFER_SIZE (buffer);
345   data = GST_BUFFER_DATA (buffer);
346   timestamp = GST_BUFFER_TIMESTAMP (buffer);
347
348   fragmented = FALSE;
349   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpmp4apay);
350
351   while (size > 0) {
352     guint towrite;
353     guint8 *payload;
354     guint payload_len;
355     guint packet_len;
356
357     /* this will be the total lenght of the packet */
358     packet_len = gst_rtp_buffer_calc_packet_len (size, 0, 0);
359
360     if (!fragmented) {
361       /* first packet calculate space for the packet including the header */
362       count = size;
363       while (count >= 0xff) {
364         packet_len++;
365         count -= 0xff;
366       }
367       packet_len++;
368     }
369
370     /* fill one MTU or all available bytes */
371     towrite = MIN (packet_len, mtu);
372
373     /* this is the payload length */
374     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
375
376     GST_DEBUG_OBJECT (rtpmp4apay,
377         "avail %d, towrite %d, packet_len %d, payload_len %d", size, towrite,
378         packet_len, payload_len);
379
380     /* create buffer to hold the payload. */
381     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
382
383     /* copy payload */
384     payload = gst_rtp_buffer_get_payload (outbuf);
385
386     if (!fragmented) {
387       /* first packet write the header */
388       count = size;
389       while (count >= 0xff) {
390         *payload++ = 0xff;
391         payload_len--;
392         count -= 0xff;
393       }
394       *payload++ = count;
395       payload_len--;
396     }
397
398     /* copy data to payload */
399     memcpy (payload, data, payload_len);
400     data += payload_len;
401     size -= payload_len;
402
403     /* marker only if the packet is complete */
404     gst_rtp_buffer_set_marker (outbuf, size == 0);
405
406     /* copy incomming timestamp (if any) to outgoing buffers */
407     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
408
409     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4apay), outbuf);
410
411     fragmented = TRUE;
412   }
413
414   gst_buffer_unref (buffer);
415
416   return ret;
417 }
418
419 gboolean
420 gst_rtp_mp4a_pay_plugin_init (GstPlugin * plugin)
421 {
422   return gst_element_register (plugin, "rtpmp4apay",
423       GST_RANK_NONE, GST_TYPE_RTP_MP4A_PAY);
424 }