1 /* GStreamer
2  * Copyright (C) <2005> 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 /**
21  * SECTION:element-rtpamrpay
22  * @see_also: rtpamrdepay
23  *
24  * Payload AMR audio into RTP packets according to RFC 3267.
25  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3267.txt
26  *
27  * <refsect2>
28  * <title>Example pipeline</title>
29  * |[
30  * gst-launch-1.0 -v audiotestsrc ! amrnbenc ! rtpamrpay ! udpsink
31  * ]| This example pipeline will encode and payload an AMR stream. Refer to
32  * the rtpamrdepay example to depayload and decode the RTP stream.
33  * </refsect2>
34  */
35 
36 /* references:
37  *
38  * RFC 3267 - Real-Time Transport Protocol (RTP) Payload Format and File
39  *    Storage Format for the Adaptive Multi-Rate (AMR) and Adaptive
40  *    Multi-Rate Wideband (AMR-WB) Audio Codecs.
41  *
42  * ETSI TS 126 201 V6.0.0 (2004-12) - Digital cellular telecommunications system (Phase 2+);
43  *                 Universal Mobile Telecommunications System (UMTS);
44  *                          AMR speech codec, wideband;
45  *                                 Frame structure
46  *                    (3GPP TS 26.201 version 6.0.0 Release 6)
47  */
48 
49 #ifdef HAVE_CONFIG_H
50 #  include "config.h"
51 #endif
52 
53 #include <string.h>
54 
55 #include <gst/rtp/gstrtpbuffer.h>
56 #include <gst/audio/audio.h>
57 
58 #include "gstrtpamrpay.h"
59 #include "gstrtputils.h"
60 
61 GST_DEBUG_CATEGORY_STATIC (rtpamrpay_debug);
62 #define GST_CAT_DEFAULT (rtpamrpay_debug)
63 
64 static GstStaticPadTemplate gst_rtp_amr_pay_sink_template =
65     GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("audio/AMR, channels=(int)1, rate=(int)8000; "
69         "audio/AMR-WB, channels=(int)1, rate=(int)16000")
70     );
71 
72 static GstStaticPadTemplate gst_rtp_amr_pay_src_template =
73     GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS ("application/x-rtp, "
77         "media = (string) \"audio\", "
78         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
79         "clock-rate = (int) 8000, "
80         "encoding-name = (string) \"AMR\", "
81         "encoding-params = (string) \"1\", "
82         "octet-align = (string) \"1\", "
83         "crc = (string) \"0\", "
84         "robust-sorting = (string) \"0\", "
85         "interleaving = (string) \"0\", "
86         "mode-set = (int) [ 0, 7 ], "
87         "mode-change-period = (int) [ 1, MAX ], "
88         "mode-change-neighbor = (string) { \"0\", \"1\" }, "
89         "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ];"
90         "application/x-rtp, "
91         "media = (string) \"audio\", "
92         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
93         "clock-rate = (int) 16000, "
94         "encoding-name = (string) \"AMR-WB\", "
95         "encoding-params = (string) \"1\", "
96         "octet-align = (string) \"1\", "
97         "crc = (string) \"0\", "
98         "robust-sorting = (string) \"0\", "
99         "interleaving = (string) \"0\", "
100         "mode-set = (int) [ 0, 7 ], "
101         "mode-change-period = (int) [ 1, MAX ], "
102         "mode-change-neighbor = (string) { \"0\", \"1\" }, "
103         "maxptime = (int) [ 20, MAX ], " "ptime = (int) [ 20, MAX ]")
104     );
105 
106 static gboolean gst_rtp_amr_pay_setcaps (GstRTPBasePayload * basepayload,
107     GstCaps * caps);
108 static GstFlowReturn gst_rtp_amr_pay_handle_buffer (GstRTPBasePayload * pad,
109     GstBuffer * buffer);
110 
111 static GstStateChangeReturn
112 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition);
113 
114 #define gst_rtp_amr_pay_parent_class parent_class
115 G_DEFINE_TYPE (GstRtpAMRPay, gst_rtp_amr_pay, GST_TYPE_RTP_BASE_PAYLOAD);
116 
117 static void
gst_rtp_amr_pay_class_init(GstRtpAMRPayClass * klass)118 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
119 {
120   GstElementClass *gstelement_class;
121   GstRTPBasePayloadClass *gstrtpbasepayload_class;
122 
123   gstelement_class = (GstElementClass *) klass;
124   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
125 
126   gstelement_class->change_state = gst_rtp_amr_pay_change_state;
127 
128   gst_element_class_add_static_pad_template (gstelement_class,
129       &gst_rtp_amr_pay_src_template);
130   gst_element_class_add_static_pad_template (gstelement_class,
131       &gst_rtp_amr_pay_sink_template);
132 
133   gst_element_class_set_static_metadata (gstelement_class, "RTP AMR payloader",
134       "Codec/Payloader/Network/RTP",
135       "Payload-encode AMR or AMR-WB audio into RTP packets (RFC 3267)",
136       "Wim Taymans <wim.taymans@gmail.com>");
137 
138   gstrtpbasepayload_class->set_caps = gst_rtp_amr_pay_setcaps;
139   gstrtpbasepayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
140 
141   GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
142       "AMR/AMR-WB RTP Payloader");
143 }
144 
145 static void
gst_rtp_amr_pay_init(GstRtpAMRPay * rtpamrpay)146 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay)
147 {
148 }
149 
150 static void
gst_rtp_amr_pay_reset(GstRtpAMRPay * pay)151 gst_rtp_amr_pay_reset (GstRtpAMRPay * pay)
152 {
153   pay->next_rtp_time = 0;
154   pay->first_ts = GST_CLOCK_TIME_NONE;
155   pay->first_rtp_time = 0;
156 }
157 
158 static gboolean
gst_rtp_amr_pay_setcaps(GstRTPBasePayload * basepayload,GstCaps * caps)159 gst_rtp_amr_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
160 {
161   GstRtpAMRPay *rtpamrpay;
162   gboolean res;
163   const GstStructure *s;
164   const gchar *str;
165 
166   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
167 
168   /* figure out the mode Narrow or Wideband */
169   s = gst_caps_get_structure (caps, 0);
170   if ((str = gst_structure_get_name (s))) {
171     if (strcmp (str, "audio/AMR") == 0)
172       rtpamrpay->mode = GST_RTP_AMR_P_MODE_NB;
173     else if (strcmp (str, "audio/AMR-WB") == 0)
174       rtpamrpay->mode = GST_RTP_AMR_P_MODE_WB;
175     else
176       goto wrong_type;
177   } else
178     goto wrong_type;
179 
180   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
181     gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
182   else
183     gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "AMR-WB",
184         16000);
185 
186   res = gst_rtp_base_payload_set_outcaps (basepayload,
187       "encoding-params", G_TYPE_STRING, "1", "octet-align", G_TYPE_STRING, "1",
188       /* don't set the defaults
189        *
190        * "crc", G_TYPE_STRING, "0",
191        * "robust-sorting", G_TYPE_STRING, "0",
192        * "interleaving", G_TYPE_STRING, "0",
193        */
194       NULL);
195 
196   return res;
197 
198   /* ERRORS */
199 wrong_type:
200   {
201     GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
202         GST_STR_NULL (str));
203     return FALSE;
204   }
205 }
206 
207 static void
gst_rtp_amr_pay_recalc_rtp_time(GstRtpAMRPay * rtpamrpay,GstClockTime timestamp)208 gst_rtp_amr_pay_recalc_rtp_time (GstRtpAMRPay * rtpamrpay,
209     GstClockTime timestamp)
210 {
211   /* re-sync rtp time */
212   if (GST_CLOCK_TIME_IS_VALID (rtpamrpay->first_ts) &&
213       GST_CLOCK_TIME_IS_VALID (timestamp) && timestamp >= rtpamrpay->first_ts) {
214     GstClockTime diff;
215     guint32 rtpdiff;
216 
217     /* interpolate to reproduce gap from start, rather than intermediate
218      * intervals to avoid roundup accumulation errors */
219     diff = timestamp - rtpamrpay->first_ts;
220     rtpdiff = ((diff / GST_MSECOND) * 8) <<
221         (rtpamrpay->mode == GST_RTP_AMR_P_MODE_WB);
222     rtpamrpay->next_rtp_time = rtpamrpay->first_rtp_time + rtpdiff;
223     GST_DEBUG_OBJECT (rtpamrpay,
224         "elapsed time %" GST_TIME_FORMAT ", rtp %" G_GUINT32_FORMAT ", "
225         "new offset %" G_GUINT32_FORMAT, GST_TIME_ARGS (diff), rtpdiff,
226         rtpamrpay->next_rtp_time);
227   }
228 }
229 
230 /* -1 is invalid */
231 static const gint nb_frame_size[16] = {
232   12, 13, 15, 17, 19, 20, 26, 31,
233   5, -1, -1, -1, -1, -1, -1, 0
234 };
235 
236 static const gint wb_frame_size[16] = {
237   17, 23, 32, 36, 40, 46, 50, 58,
238   60, 5, -1, -1, -1, -1, -1, 0
239 };
240 
241 static GstFlowReturn
gst_rtp_amr_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)242 gst_rtp_amr_pay_handle_buffer (GstRTPBasePayload * basepayload,
243     GstBuffer * buffer)
244 {
245   GstRtpAMRPay *rtpamrpay;
246   const gint *frame_size;
247   GstFlowReturn ret;
248   guint payload_len;
249   GstMapInfo map;
250   GstBuffer *outbuf;
251   guint8 *payload, *ptr, *payload_amr;
252   GstClockTime timestamp, duration;
253   guint packet_len, mtu;
254   gint i, num_packets, num_nonempty_packets;
255   gint amr_len;
256   gboolean sid = FALSE;
257   GstRTPBuffer rtp = { NULL };
258 
259   rtpamrpay = GST_RTP_AMR_PAY (basepayload);
260   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpamrpay);
261 
262   gst_buffer_map (buffer, &map, GST_MAP_READ);
263 
264   timestamp = GST_BUFFER_PTS (buffer);
265   duration = GST_BUFFER_DURATION (buffer);
266 
267   /* setup frame size pointer */
268   if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
269     frame_size = nb_frame_size;
270   else
271     frame_size = wb_frame_size;
272 
273   GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", map.size);
274 
275   /* FIXME, only
276    * octet aligned, no interleaving, single channel, no CRC,
277    * no robust-sorting. To fix this you need to implement the downstream
278    * negotiation function. */
279 
280   /* first count number of packets and total amr frame size */
281   amr_len = num_packets = num_nonempty_packets = 0;
282   for (i = 0; i < map.size; i++) {
283     guint8 FT;
284     gint fr_size;
285 
286     FT = (map.data[i] & 0x78) >> 3;
287 
288     fr_size = frame_size[FT];
289     GST_DEBUG_OBJECT (basepayload, "frame type %d, frame size %d", FT, fr_size);
290     /* FIXME, we don't handle this yet.. */
291     if (fr_size <= 0)
292       goto wrong_size;
293 
294     if (fr_size == 5)
295       sid = TRUE;
296 
297     amr_len += fr_size;
298     num_nonempty_packets++;
299     num_packets++;
300     i += fr_size;
301   }
302   if (amr_len > map.size)
303     goto incomplete_frame;
304 
305   /* we need one extra byte for the CMR, the ToC is in the input
306    * data */
307   payload_len = map.size + 1;
308 
309   /* get packet len to check against MTU */
310   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
311   if (packet_len > mtu)
312     goto too_big;
313 
314   /* now alloc output buffer */
315   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
316 
317   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
318 
319   /* copy timestamp */
320   GST_BUFFER_PTS (outbuf) = timestamp;
321 
322   if (duration != GST_CLOCK_TIME_NONE)
323     GST_BUFFER_DURATION (outbuf) = duration;
324   else {
325     GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
326   }
327 
328   if (GST_BUFFER_IS_DISCONT (buffer)) {
329     GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
330     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
331     gst_rtp_buffer_set_marker (&rtp, TRUE);
332     gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
333   }
334 
335   if (G_UNLIKELY (sid)) {
336     gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
337   }
338 
339   /* perfect rtptime */
340   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rtpamrpay->first_ts))) {
341     rtpamrpay->first_ts = timestamp;
342     rtpamrpay->first_rtp_time = rtpamrpay->next_rtp_time;
343   }
344   GST_BUFFER_OFFSET (outbuf) = rtpamrpay->next_rtp_time;
345   rtpamrpay->next_rtp_time +=
346       (num_packets * 160) << (rtpamrpay->mode == GST_RTP_AMR_P_MODE_WB);
347 
348   /* get payload, this is now writable */
349   payload = gst_rtp_buffer_get_payload (&rtp);
350 
351   /*   0 1 2 3 4 5 6 7
352    *  +-+-+-+-+-+-+-+-+
353    *  |  CMR  |R|R|R|R|
354    *  +-+-+-+-+-+-+-+-+
355    */
356   payload[0] = 0xF0;            /* CMR, no specific mode requested */
357 
358   /* this is where we copy the AMR data, after num_packets FTs and the
359    * CMR. */
360   payload_amr = payload + num_packets + 1;
361 
362   /* copy data in payload, first we copy all the FTs then all
363    * the AMR data. The last FT has to have the F flag cleared. */
364   ptr = map.data;
365   for (i = 1; i <= num_packets; i++) {
366     guint8 FT;
367     gint fr_size;
368 
369     /*   0 1 2 3 4 5 6 7
370      *  +-+-+-+-+-+-+-+-+
371      *  |F|  FT   |Q|P|P| more FT...
372      *  +-+-+-+-+-+-+-+-+
373      */
374     FT = (*ptr & 0x78) >> 3;
375 
376     fr_size = frame_size[FT];
377 
378     if (i == num_packets)
379       /* last packet, clear F flag */
380       payload[i] = *ptr & 0x7f;
381     else
382       /* set F flag */
383       payload[i] = *ptr | 0x80;
384 
385     memcpy (payload_amr, &ptr[1], fr_size);
386 
387     /* all sizes are > 0 since we checked for that above */
388     ptr += fr_size + 1;
389     payload_amr += fr_size;
390   }
391 
392   gst_buffer_unmap (buffer, &map);
393   gst_rtp_buffer_unmap (&rtp);
394 
395   gst_rtp_copy_audio_meta (rtpamrpay, outbuf, buffer);
396 
397   gst_buffer_unref (buffer);
398 
399   ret = gst_rtp_base_payload_push (basepayload, outbuf);
400 
401   return ret;
402 
403   /* ERRORS */
404 wrong_size:
405   {
406     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
407         (NULL), ("received AMR frame with size <= 0"));
408     gst_buffer_unmap (buffer, &map);
409     gst_buffer_unref (buffer);
410 
411     return GST_FLOW_ERROR;
412   }
413 incomplete_frame:
414   {
415     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
416         (NULL), ("received incomplete AMR frames"));
417     gst_buffer_unmap (buffer, &map);
418     gst_buffer_unref (buffer);
419 
420     return GST_FLOW_ERROR;
421   }
422 too_big:
423   {
424     GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
425         (NULL), ("received too many AMR frames for MTU"));
426     gst_buffer_unmap (buffer, &map);
427     gst_buffer_unref (buffer);
428 
429     return GST_FLOW_ERROR;
430   }
431 }
432 
433 static GstStateChangeReturn
gst_rtp_amr_pay_change_state(GstElement * element,GstStateChange transition)434 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition)
435 {
436   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
437 
438   /* handle upwards state changes here */
439   switch (transition) {
440     default:
441       break;
442   }
443 
444   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
445 
446   /* handle downwards state changes */
447   switch (transition) {
448     case GST_STATE_CHANGE_PAUSED_TO_READY:
449       gst_rtp_amr_pay_reset (GST_RTP_AMR_PAY (element));
450       break;
451     default:
452       break;
453   }
454 
455   return ret;
456 }
457 
458 gboolean
gst_rtp_amr_pay_plugin_init(GstPlugin * plugin)459 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
460 {
461   return gst_element_register (plugin, "rtpamrpay",
462       GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_PAY);
463 }
464