2 * Copyright (C) <2010> 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-rtpac3pay
22 * @see_also: rtpac3depay
24 * Payload AC3 audio into RTP packets according to RFC 4184.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc4184.txt
28 * <title>Example pipeline</title>
30 * gst-launch -v audiotestsrc ! avenc_ac3 ! rtpac3pay ! udpsink
31 * ]| This example pipeline will encode and payload AC3 stream. Refer to
32 * the rtpac3depay example to depayload and decode the RTP stream.
35 * Last reviewed on 2013-04-25 (1.1.0)
44 #include <gst/rtp/gstrtpbuffer.h>
46 #include "gstrtpac3pay.h"
48 GST_DEBUG_CATEGORY_STATIC (rtpac3pay_debug);
49 #define GST_CAT_DEFAULT (rtpac3pay_debug)
51 static GstStaticPadTemplate gst_rtp_ac3_pay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("audio/ac3; " "audio/x-ac3; ")
58 static GstStaticPadTemplate gst_rtp_ac3_pay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-rtp, "
63 "media = (string) \"audio\", "
64 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
65 "clock-rate = (int) { 32000, 44100, 48000 }, "
66 "encoding-name = (string) \"AC3\"")
69 static void gst_rtp_ac3_pay_finalize (GObject * object);
71 static GstStateChangeReturn gst_rtp_ac3_pay_change_state (GstElement * element,
72 GstStateChange transition);
74 static gboolean gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload,
76 static gboolean gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload,
78 static GstFlowReturn gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay);
79 static GstFlowReturn gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * payload,
82 #define gst_rtp_ac3_pay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpAC3Pay, gst_rtp_ac3_pay, GST_TYPE_RTP_BASE_PAYLOAD);
86 gst_rtp_ac3_pay_class_init (GstRtpAC3PayClass * klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
90 GstRTPBasePayloadClass *gstrtpbasepayload_class;
92 GST_DEBUG_CATEGORY_INIT (rtpac3pay_debug, "rtpac3pay", 0,
93 "AC3 Audio RTP Depayloader");
95 gobject_class = (GObjectClass *) klass;
96 gstelement_class = (GstElementClass *) klass;
97 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
99 gobject_class->finalize = gst_rtp_ac3_pay_finalize;
101 gstelement_class->change_state = gst_rtp_ac3_pay_change_state;
103 gst_element_class_add_pad_template (gstelement_class,
104 gst_static_pad_template_get (&gst_rtp_ac3_pay_src_template));
105 gst_element_class_add_pad_template (gstelement_class,
106 gst_static_pad_template_get (&gst_rtp_ac3_pay_sink_template));
108 gst_element_class_set_static_metadata (gstelement_class,
109 "RTP AC3 audio payloader", "Codec/Payloader/Network/RTP",
110 "Payload AC3 audio as RTP packets (RFC 4184)",
111 "Wim Taymans <wim.taymans@gmail.com>");
113 gstrtpbasepayload_class->set_caps = gst_rtp_ac3_pay_setcaps;
114 gstrtpbasepayload_class->sink_event = gst_rtp_ac3_pay_sink_event;
115 gstrtpbasepayload_class->handle_buffer = gst_rtp_ac3_pay_handle_buffer;
119 gst_rtp_ac3_pay_init (GstRtpAC3Pay * rtpac3pay)
121 rtpac3pay->adapter = gst_adapter_new ();
125 gst_rtp_ac3_pay_finalize (GObject * object)
127 GstRtpAC3Pay *rtpac3pay;
129 rtpac3pay = GST_RTP_AC3_PAY (object);
131 g_object_unref (rtpac3pay->adapter);
133 G_OBJECT_CLASS (parent_class)->finalize (object);
137 gst_rtp_ac3_pay_reset (GstRtpAC3Pay * pay)
141 gst_adapter_clear (pay->adapter);
142 GST_DEBUG_OBJECT (pay, "reset depayloader");
146 gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
150 GstStructure *structure;
152 structure = gst_caps_get_structure (caps, 0);
154 if (!gst_structure_get_int (structure, "rate", &rate))
155 rate = 90000; /* default */
157 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "AC3", rate);
158 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
164 gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
167 GstRtpAC3Pay *rtpac3pay;
169 rtpac3pay = GST_RTP_AC3_PAY (payload);
171 switch (GST_EVENT_TYPE (event)) {
173 /* make sure we push the last packets in the adapter on EOS */
174 gst_rtp_ac3_pay_flush (rtpac3pay);
176 case GST_EVENT_FLUSH_STOP:
177 gst_rtp_ac3_pay_reset (rtpac3pay);
183 res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
194 static const struct frmsize_s frmsizecod_tbl[] = {
199 {48, {96, 104, 144}},
200 {48, {96, 105, 144}},
201 {56, {112, 121, 168}},
202 {56, {112, 122, 168}},
203 {64, {128, 139, 192}},
204 {64, {128, 140, 192}},
205 {80, {160, 174, 240}},
206 {80, {160, 175, 240}},
207 {96, {192, 208, 288}},
208 {96, {192, 209, 288}},
209 {112, {224, 243, 336}},
210 {112, {224, 244, 336}},
211 {128, {256, 278, 384}},
212 {128, {256, 279, 384}},
213 {160, {320, 348, 480}},
214 {160, {320, 349, 480}},
215 {192, {384, 417, 576}},
216 {192, {384, 418, 576}},
217 {224, {448, 487, 672}},
218 {224, {448, 488, 672}},
219 {256, {512, 557, 768}},
220 {256, {512, 558, 768}},
221 {320, {640, 696, 960}},
222 {320, {640, 697, 960}},
223 {384, {768, 835, 1152}},
224 {384, {768, 836, 1152}},
225 {448, {896, 975, 1344}},
226 {448, {896, 976, 1344}},
227 {512, {1024, 1114, 1536}},
228 {512, {1024, 1115, 1536}},
229 {576, {1152, 1253, 1728}},
230 {576, {1152, 1254, 1728}},
231 {640, {1280, 1393, 1920}},
232 {640, {1280, 1394, 1920}}
236 gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
238 guint avail, FT, NF, mtu;
242 /* the data available in the adapter is either smaller
243 * than the MTU or bigger. In the case it is smaller, the complete
244 * adapter contents can be put in one packet. In the case the
245 * adapter has more than one MTU, we need to split the AC3 data
246 * over multiple packets. */
247 avail = gst_adapter_available (rtpac3pay->adapter);
252 /* number of frames */
255 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpac3pay);
257 GST_LOG_OBJECT (rtpac3pay, "flushing %u bytes", avail);
264 GstRTPBuffer rtp = { NULL, };
266 /* this will be the total length of the packet */
267 packet_len = gst_rtp_buffer_calc_packet_len (2 + avail, 0, 0);
269 /* fill one MTU or all available bytes */
270 towrite = MIN (packet_len, mtu);
272 /* this is the payload length */
273 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
275 /* create buffer to hold the payload */
276 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
279 /* check if it all fits */
280 if (towrite < packet_len) {
283 GST_LOG_OBJECT (rtpac3pay, "we need to fragment");
284 /* check if we will be able to put at least 5/8th of the total
285 * frame in this first frame. */
286 if ((avail * 5) / 8 >= (payload_len - 2))
290 /* check how many fragments we will need */
291 maxlen = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
292 NF = (avail + maxlen - 1) / maxlen;
294 } else if (FT != 3) {
295 /* remaining fragment */
301 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
302 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
304 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
306 * FT: 0: one or more complete frames
307 * 1: initial 5/8 fragment
308 * 2: initial fragment not 5/8
310 * NF: amount of frames if FT = 0, else number of fragments.
312 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
313 GST_LOG_OBJECT (rtpac3pay, "FT %u, NF %u", FT, NF);
314 payload = gst_rtp_buffer_get_payload (&rtp);
315 payload[0] = (FT & 3);
319 gst_adapter_copy (rtpac3pay->adapter, &payload[2], 0, payload_len);
320 gst_adapter_flush (rtpac3pay->adapter, payload_len);
322 avail -= payload_len;
324 gst_rtp_buffer_set_marker (&rtp, TRUE);
325 gst_rtp_buffer_unmap (&rtp);
327 GST_BUFFER_TIMESTAMP (outbuf) = rtpac3pay->first_ts;
328 GST_BUFFER_DURATION (outbuf) = rtpac3pay->duration;
330 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpac3pay), outbuf);
337 gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
340 GstRtpAC3Pay *rtpac3pay;
342 gsize avail, left, NF;
346 GstClockTime duration, timestamp;
348 rtpac3pay = GST_RTP_AC3_PAY (basepayload);
350 gst_buffer_map (buffer, &map, GST_MAP_READ);
351 duration = GST_BUFFER_DURATION (buffer);
352 timestamp = GST_BUFFER_TIMESTAMP (buffer);
354 if (GST_BUFFER_IS_DISCONT (buffer)) {
355 GST_DEBUG_OBJECT (rtpac3pay, "DISCONT");
356 gst_rtp_ac3_pay_reset (rtpac3pay);
359 /* count the amount of incomming packets */
364 guint bsid, fscod, frmsizecod, frame_size;
369 if (p[0] != 0x0b || p[1] != 0x77)
376 frmsizecod = p[4] & 0x3f;
379 GST_DEBUG_OBJECT (rtpac3pay, "fscod %u, %u", fscod, frmsizecod);
381 if (fscod >= 3 || frmsizecod >= 38)
384 frame_size = frmsizecod_tbl[frmsizecod].frm_size[fscod] * 2;
385 if (frame_size > left)
389 GST_DEBUG_OBJECT (rtpac3pay, "found frame %" G_GSIZE_FORMAT " of size %u",
395 gst_buffer_unmap (buffer, &map);
399 avail = gst_adapter_available (rtpac3pay->adapter);
401 /* get packet length of previous data and this new data,
402 * payload length includes a 4 byte header */
403 packet_len = gst_rtp_buffer_calc_packet_len (2 + avail + map.size, 0, 0);
405 /* if this buffer is going to overflow the packet, flush what we
407 if (gst_rtp_base_payload_is_filled (basepayload,
408 packet_len, rtpac3pay->duration + duration)) {
409 ret = gst_rtp_ac3_pay_flush (rtpac3pay);
416 GST_DEBUG_OBJECT (rtpac3pay,
417 "first packet, save timestamp %" GST_TIME_FORMAT,
418 GST_TIME_ARGS (timestamp));
419 rtpac3pay->first_ts = timestamp;
420 rtpac3pay->duration = 0;
424 gst_adapter_push (rtpac3pay->adapter, buffer);
425 rtpac3pay->duration += duration;
433 GST_WARNING_OBJECT (rtpac3pay, "no valid AC3 frames found");
438 static GstStateChangeReturn
439 gst_rtp_ac3_pay_change_state (GstElement * element, GstStateChange transition)
441 GstRtpAC3Pay *rtpac3pay;
442 GstStateChangeReturn ret;
444 rtpac3pay = GST_RTP_AC3_PAY (element);
446 switch (transition) {
447 case GST_STATE_CHANGE_READY_TO_PAUSED:
448 gst_rtp_ac3_pay_reset (rtpac3pay);
454 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
456 switch (transition) {
457 case GST_STATE_CHANGE_PAUSED_TO_READY:
458 gst_rtp_ac3_pay_reset (rtpac3pay);
467 gst_rtp_ac3_pay_plugin_init (GstPlugin * plugin)
469 return gst_element_register (plugin, "rtpac3pay",
470 GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_PAY);