2 * Copyright (C) <2005> 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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-rtpamrpay
22 * @see_also: rtpamrdepay
24 * Payload AMR audio into RTP packets according to RFC 3267.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3267.txt
28 * <title>Example pipeline</title>
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.
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.
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;
46 * (3GPP TS 26.201 version 6.0.0 Release 6)
55 #include <gst/rtp/gstrtpbuffer.h>
56 #include <gst/audio/audio.h>
58 #include "gstrtpamrpay.h"
59 #include "gstrtputils.h"
61 GST_DEBUG_CATEGORY_STATIC (rtpamrpay_debug);
62 #define GST_CAT_DEFAULT (rtpamrpay_debug)
64 static GstStaticPadTemplate gst_rtp_amr_pay_sink_template =
65 GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS ("audio/AMR, channels=(int)1, rate=(int)8000; "
69 "audio/AMR-WB, channels=(int)1, rate=(int)16000")
72 static GstStaticPadTemplate gst_rtp_amr_pay_src_template =
73 GST_STATIC_PAD_TEMPLATE ("src",
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 ];"
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 ]")
106 static gboolean gst_rtp_amr_pay_setcaps (GstRTPBasePayload * basepayload,
108 static GstFlowReturn gst_rtp_amr_pay_handle_buffer (GstRTPBasePayload * pad,
111 static GstStateChangeReturn
112 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition);
114 #define gst_rtp_amr_pay_parent_class parent_class
115 G_DEFINE_TYPE (GstRtpAMRPay, gst_rtp_amr_pay, GST_TYPE_RTP_BASE_PAYLOAD);
118 gst_rtp_amr_pay_class_init (GstRtpAMRPayClass * klass)
120 GstElementClass *gstelement_class;
121 GstRTPBasePayloadClass *gstrtpbasepayload_class;
123 gstelement_class = (GstElementClass *) klass;
124 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
126 gstelement_class->change_state = gst_rtp_amr_pay_change_state;
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);
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>");
138 gstrtpbasepayload_class->set_caps = gst_rtp_amr_pay_setcaps;
139 gstrtpbasepayload_class->handle_buffer = gst_rtp_amr_pay_handle_buffer;
141 GST_DEBUG_CATEGORY_INIT (rtpamrpay_debug, "rtpamrpay", 0,
142 "AMR/AMR-WB RTP Payloader");
146 gst_rtp_amr_pay_init (GstRtpAMRPay * rtpamrpay)
151 gst_rtp_amr_pay_reset (GstRtpAMRPay * pay)
153 pay->next_rtp_time = 0;
154 pay->first_ts = GST_CLOCK_TIME_NONE;
155 pay->first_rtp_time = 0;
159 gst_rtp_amr_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
161 GstRtpAMRPay *rtpamrpay;
163 const GstStructure *s;
166 rtpamrpay = GST_RTP_AMR_PAY (basepayload);
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;
180 if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
181 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "AMR", 8000);
183 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "AMR-WB",
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
190 * "crc", G_TYPE_STRING, "0",
191 * "robust-sorting", G_TYPE_STRING, "0",
192 * "interleaving", G_TYPE_STRING, "0",
201 GST_ERROR_OBJECT (rtpamrpay, "unsupported media type '%s'",
208 gst_rtp_amr_pay_recalc_rtp_time (GstRtpAMRPay * rtpamrpay,
209 GstClockTime timestamp)
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) {
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);
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
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
242 gst_rtp_amr_pay_handle_buffer (GstRTPBasePayload * basepayload,
245 GstRtpAMRPay *rtpamrpay;
246 const gint *frame_size;
251 guint8 *payload, *ptr, *payload_amr;
252 GstClockTime timestamp, duration;
253 guint packet_len, mtu;
254 gint i, num_packets, num_nonempty_packets;
256 gboolean sid = FALSE;
257 GstRTPBuffer rtp = { NULL };
259 rtpamrpay = GST_RTP_AMR_PAY (basepayload);
260 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpamrpay);
262 gst_buffer_map (buffer, &map, GST_MAP_READ);
264 timestamp = GST_BUFFER_PTS (buffer);
265 duration = GST_BUFFER_DURATION (buffer);
267 /* setup frame size pointer */
268 if (rtpamrpay->mode == GST_RTP_AMR_P_MODE_NB)
269 frame_size = nb_frame_size;
271 frame_size = wb_frame_size;
273 GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", map.size);
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. */
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++) {
286 FT = (map.data[i] & 0x78) >> 3;
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.. */
298 num_nonempty_packets++;
302 if (amr_len > map.size)
303 goto incomplete_frame;
305 /* we need one extra byte for the CMR, the ToC is in the input
307 payload_len = map.size + 1;
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)
314 /* now alloc output buffer */
315 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
317 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
320 GST_BUFFER_PTS (outbuf) = timestamp;
322 if (duration != GST_CLOCK_TIME_NONE)
323 GST_BUFFER_DURATION (outbuf) = duration;
325 GST_BUFFER_DURATION (outbuf) = num_packets * 20 * GST_MSECOND;
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);
335 if (G_UNLIKELY (sid)) {
336 gst_rtp_amr_pay_recalc_rtp_time (rtpamrpay, timestamp);
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;
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);
348 /* get payload, this is now writable */
349 payload = gst_rtp_buffer_get_payload (&rtp);
356 payload[0] = 0xF0; /* CMR, no specific mode requested */
358 /* this is where we copy the AMR data, after num_packets FTs and the
360 payload_amr = payload + num_packets + 1;
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. */
365 for (i = 1; i <= num_packets; i++) {
371 * |F| FT |Q|P|P| more FT...
374 FT = (*ptr & 0x78) >> 3;
376 fr_size = frame_size[FT];
378 if (i == num_packets)
379 /* last packet, clear F flag */
380 payload[i] = *ptr & 0x7f;
383 payload[i] = *ptr | 0x80;
385 memcpy (payload_amr, &ptr[1], fr_size);
387 /* all sizes are > 0 since we checked for that above */
389 payload_amr += fr_size;
392 gst_buffer_unmap (buffer, &map);
393 gst_rtp_buffer_unmap (&rtp);
395 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpamrpay), outbuf, buffer,
396 g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
398 gst_buffer_unref (buffer);
400 ret = gst_rtp_base_payload_push (basepayload, outbuf);
407 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
408 (NULL), ("received AMR frame with size <= 0"));
409 gst_buffer_unmap (buffer, &map);
410 gst_buffer_unref (buffer);
412 return GST_FLOW_ERROR;
416 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
417 (NULL), ("received incomplete AMR frames"));
418 gst_buffer_unmap (buffer, &map);
419 gst_buffer_unref (buffer);
421 return GST_FLOW_ERROR;
425 GST_ELEMENT_ERROR (basepayload, STREAM, FORMAT,
426 (NULL), ("received too many AMR frames for MTU"));
427 gst_buffer_unmap (buffer, &map);
428 gst_buffer_unref (buffer);
430 return GST_FLOW_ERROR;
434 static GstStateChangeReturn
435 gst_rtp_amr_pay_change_state (GstElement * element, GstStateChange transition)
437 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
439 /* handle upwards state changes here */
440 switch (transition) {
445 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
447 /* handle downwards state changes */
448 switch (transition) {
449 case GST_STATE_CHANGE_PAUSED_TO_READY:
450 gst_rtp_amr_pay_reset (GST_RTP_AMR_PAY (element));
460 gst_rtp_amr_pay_plugin_init (GstPlugin * plugin)
462 return gst_element_register (plugin, "rtpamrpay",
463 GST_RANK_SECONDARY, GST_TYPE_RTP_AMR_PAY);