blob: 57020dc4b9ec0ca48bb32b1c2b270ae16b4102b6 [file] [log] [blame]
Wim Taymans6db12cb2010-12-21 22:34:49 +01001/* GStreamer
2 * Copyright (C) <2010> 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
Tim-Philipp Müller230cf412012-11-04 00:07:18 +000016 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
Wim Taymans6db12cb2010-12-21 22:34:49 +010018 */
19
Wim Taymans1df2e622013-04-25 13:19:35 +020020/**
21 * SECTION:element-rtpac3pay
22 * @see_also: rtpac3depay
23 *
24 * Payload AC3 audio into RTP packets according to RFC 4184.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc4184.txt
26 *
27 * <refsect2>
28 * <title>Example pipeline</title>
29 * |[
Tim-Philipp Müller2e412a42015-05-10 11:05:00 +010030 * gst-launch-1.0 -v audiotestsrc ! avenc_ac3 ! rtpac3pay ! udpsink
Wim Taymans1df2e622013-04-25 13:19:35 +020031 * ]| This example pipeline will encode and payload AC3 stream. Refer to
32 * the rtpac3depay example to depayload and decode the RTP stream.
33 * </refsect2>
Wim Taymans1df2e622013-04-25 13:19:35 +020034 */
35
Wim Taymans6db12cb2010-12-21 22:34:49 +010036#ifdef HAVE_CONFIG_H
37# include "config.h"
38#endif
39
40#include <string.h>
41
42#include <gst/rtp/gstrtpbuffer.h>
Sebastian Drögeb1089fb2015-08-04 20:59:17 +030043#include <gst/audio/audio.h>
Wim Taymans6db12cb2010-12-21 22:34:49 +010044
45#include "gstrtpac3pay.h"
Sebastian Drögeb1089fb2015-08-04 20:59:17 +030046#include "gstrtputils.h"
Wim Taymans6db12cb2010-12-21 22:34:49 +010047
48GST_DEBUG_CATEGORY_STATIC (rtpac3pay_debug);
49#define GST_CAT_DEFAULT (rtpac3pay_debug)
50
51static GstStaticPadTemplate gst_rtp_ac3_pay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_PAD_SINK,
54 GST_PAD_ALWAYS,
55 GST_STATIC_CAPS ("audio/ac3; " "audio/x-ac3; ")
56 );
57
58static GstStaticPadTemplate gst_rtp_ac3_pay_src_template =
59GST_STATIC_PAD_TEMPLATE ("src",
60 GST_PAD_SRC,
61 GST_PAD_ALWAYS,
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\"")
67 );
68
69static void gst_rtp_ac3_pay_finalize (GObject * object);
70
71static GstStateChangeReturn gst_rtp_ac3_pay_change_state (GstElement * element,
72 GstStateChange transition);
73
Wim Taymans249d0082011-11-11 12:25:01 +010074static gboolean gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload,
Wim Taymans6db12cb2010-12-21 22:34:49 +010075 GstCaps * caps);
Wim Taymans75dc9632011-11-15 16:31:45 +010076static gboolean gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload,
Wim Taymansb0fbb172011-06-13 13:25:49 +020077 GstEvent * event);
Wim Taymans6db12cb2010-12-21 22:34:49 +010078static GstFlowReturn gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay);
Wim Taymans249d0082011-11-11 12:25:01 +010079static GstFlowReturn gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * payload,
Wim Taymans6db12cb2010-12-21 22:34:49 +010080 GstBuffer * buffer);
81
Wim Taymans60db07b2011-04-25 13:16:58 +020082#define gst_rtp_ac3_pay_parent_class parent_class
Wim Taymans249d0082011-11-11 12:25:01 +010083G_DEFINE_TYPE (GstRtpAC3Pay, gst_rtp_ac3_pay, GST_TYPE_RTP_BASE_PAYLOAD);
Wim Taymans6db12cb2010-12-21 22:34:49 +010084
85static void
86gst_rtp_ac3_pay_class_init (GstRtpAC3PayClass * klass)
87{
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
Wim Taymans249d0082011-11-11 12:25:01 +010090 GstRTPBasePayloadClass *gstrtpbasepayload_class;
Wim Taymans6db12cb2010-12-21 22:34:49 +010091
Wim Taymans60db07b2011-04-25 13:16:58 +020092 GST_DEBUG_CATEGORY_INIT (rtpac3pay_debug, "rtpac3pay", 0,
93 "AC3 Audio RTP Depayloader");
94
Wim Taymans6db12cb2010-12-21 22:34:49 +010095 gobject_class = (GObjectClass *) klass;
96 gstelement_class = (GstElementClass *) klass;
Wim Taymans249d0082011-11-11 12:25:01 +010097 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
Wim Taymans6db12cb2010-12-21 22:34:49 +010098
99 gobject_class->finalize = gst_rtp_ac3_pay_finalize;
100
101 gstelement_class->change_state = gst_rtp_ac3_pay_change_state;
102
Vineeth TM10713092016-03-04 10:30:12 +0900103 gst_element_class_add_static_pad_template (gstelement_class,
104 &gst_rtp_ac3_pay_src_template);
105 gst_element_class_add_static_pad_template (gstelement_class,
106 &gst_rtp_ac3_pay_sink_template);
Wim Taymans60db07b2011-04-25 13:16:58 +0200107
Tim-Philipp Müllere09ae572012-04-10 00:51:41 +0100108 gst_element_class_set_static_metadata (gstelement_class,
Wim Taymans60db07b2011-04-25 13:16:58 +0200109 "RTP AC3 audio payloader", "Codec/Payloader/Network/RTP",
110 "Payload AC3 audio as RTP packets (RFC 4184)",
111 "Wim Taymans <wim.taymans@gmail.com>");
112
Wim Taymans249d0082011-11-11 12:25:01 +0100113 gstrtpbasepayload_class->set_caps = gst_rtp_ac3_pay_setcaps;
Wim Taymans75dc9632011-11-15 16:31:45 +0100114 gstrtpbasepayload_class->sink_event = gst_rtp_ac3_pay_sink_event;
Wim Taymans249d0082011-11-11 12:25:01 +0100115 gstrtpbasepayload_class->handle_buffer = gst_rtp_ac3_pay_handle_buffer;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100116}
117
118static void
Wim Taymans60db07b2011-04-25 13:16:58 +0200119gst_rtp_ac3_pay_init (GstRtpAC3Pay * rtpac3pay)
Wim Taymans6db12cb2010-12-21 22:34:49 +0100120{
121 rtpac3pay->adapter = gst_adapter_new ();
122}
123
124static void
125gst_rtp_ac3_pay_finalize (GObject * object)
126{
127 GstRtpAC3Pay *rtpac3pay;
128
129 rtpac3pay = GST_RTP_AC3_PAY (object);
130
131 g_object_unref (rtpac3pay->adapter);
132
133 G_OBJECT_CLASS (parent_class)->finalize (object);
134}
135
136static void
137gst_rtp_ac3_pay_reset (GstRtpAC3Pay * pay)
138{
139 pay->first_ts = -1;
140 pay->duration = 0;
141 gst_adapter_clear (pay->adapter);
142 GST_DEBUG_OBJECT (pay, "reset depayloader");
143}
144
145static gboolean
Wim Taymans249d0082011-11-11 12:25:01 +0100146gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
Wim Taymans6db12cb2010-12-21 22:34:49 +0100147{
148 gboolean res;
149 gint rate;
150 GstStructure *structure;
151
152 structure = gst_caps_get_structure (caps, 0);
153
154 if (!gst_structure_get_int (structure, "rate", &rate))
155 rate = 90000; /* default */
156
Wim Taymans249d0082011-11-11 12:25:01 +0100157 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "AC3", rate);
158 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100159
160 return res;
161}
162
163static gboolean
Wim Taymans75dc9632011-11-15 16:31:45 +0100164gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
Wim Taymans6db12cb2010-12-21 22:34:49 +0100165{
Wim Taymansb0fbb172011-06-13 13:25:49 +0200166 gboolean res;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100167 GstRtpAC3Pay *rtpac3pay;
168
Wim Taymansb0fbb172011-06-13 13:25:49 +0200169 rtpac3pay = GST_RTP_AC3_PAY (payload);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100170
171 switch (GST_EVENT_TYPE (event)) {
172 case GST_EVENT_EOS:
173 /* make sure we push the last packets in the adapter on EOS */
174 gst_rtp_ac3_pay_flush (rtpac3pay);
175 break;
176 case GST_EVENT_FLUSH_STOP:
177 gst_rtp_ac3_pay_reset (rtpac3pay);
178 break;
179 default:
180 break;
181 }
182
Wim Taymans75dc9632011-11-15 16:31:45 +0100183 res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100184
Wim Taymansb0fbb172011-06-13 13:25:49 +0200185 return res;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100186}
187
188struct frmsize_s
189{
190 guint16 bit_rate;
191 guint16 frm_size[3];
192};
193
194static const struct frmsize_s frmsizecod_tbl[] = {
195 {32, {64, 69, 96}},
196 {32, {64, 70, 96}},
197 {40, {80, 87, 120}},
198 {40, {80, 88, 120}},
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}}
233};
234
235static GstFlowReturn
236gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
237{
238 guint avail, FT, NF, mtu;
239 GstBuffer *outbuf;
240 GstFlowReturn ret;
241
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);
248
249 ret = GST_FLOW_OK;
250
251 FT = 0;
252 /* number of frames */
253 NF = rtpac3pay->NF;
254
Wim Taymans249d0082011-11-11 12:25:01 +0100255 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpac3pay);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100256
257 GST_LOG_OBJECT (rtpac3pay, "flushing %u bytes", avail);
258
259 while (avail > 0) {
260 guint towrite;
261 guint8 *payload;
262 guint payload_len;
263 guint packet_len;
Wim Taymans00243002011-04-07 19:04:33 +0200264 GstRTPBuffer rtp = { NULL, };
Sebastian Dröge3af36ed2015-07-01 15:40:25 +0200265 GstBuffer *payload_buffer;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100266
267 /* this will be the total length of the packet */
268 packet_len = gst_rtp_buffer_calc_packet_len (2 + avail, 0, 0);
269
270 /* fill one MTU or all available bytes */
271 towrite = MIN (packet_len, mtu);
272
273 /* this is the payload length */
274 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
275
276 /* create buffer to hold the payload */
Sebastian Dröge3af36ed2015-07-01 15:40:25 +0200277 outbuf = gst_rtp_buffer_new_allocate (2, 0, 0);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100278
279 if (FT == 0) {
280 /* check if it all fits */
281 if (towrite < packet_len) {
282 guint maxlen;
283
284 GST_LOG_OBJECT (rtpac3pay, "we need to fragment");
285 /* check if we will be able to put at least 5/8th of the total
286 * frame in this first frame. */
287 if ((avail * 5) / 8 >= (payload_len - 2))
288 FT = 1;
289 else
290 FT = 2;
291 /* check how many fragments we will need */
292 maxlen = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
293 NF = (avail + maxlen - 1) / maxlen;
294 }
295 } else if (FT != 3) {
296 /* remaining fragment */
297 FT = 3;
298 }
299
300 /*
301 * 0 1
302 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
303 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
304 * | MBZ | FT| NF |
305 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
306 *
307 * FT: 0: one or more complete frames
308 * 1: initial 5/8 fragment
309 * 2: initial fragment not 5/8
310 * 3: other fragment
311 * NF: amount of frames if FT = 0, else number of fragments.
312 */
Wim Taymans00243002011-04-07 19:04:33 +0200313 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100314 GST_LOG_OBJECT (rtpac3pay, "FT %u, NF %u", FT, NF);
Wim Taymans00243002011-04-07 19:04:33 +0200315 payload = gst_rtp_buffer_get_payload (&rtp);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100316 payload[0] = (FT & 3);
317 payload[1] = NF;
318 payload_len -= 2;
319
Sebastian Dröge3af36ed2015-07-01 15:40:25 +0200320 if (avail == payload_len)
Wim Taymans00243002011-04-07 19:04:33 +0200321 gst_rtp_buffer_set_marker (&rtp, TRUE);
322 gst_rtp_buffer_unmap (&rtp);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100323
Sebastian Dröge3af36ed2015-07-01 15:40:25 +0200324 payload_buffer =
325 gst_adapter_take_buffer_fast (rtpac3pay->adapter, payload_len);
Tim-Philipp Müller4a28e642017-05-24 12:57:10 +0100326
327 gst_rtp_copy_audio_meta (rtpac3pay, outbuf, payload_buffer);
Sebastian Drögeb1089fb2015-08-04 20:59:17 +0300328
Sebastian Dröge3af36ed2015-07-01 15:40:25 +0200329 outbuf = gst_buffer_append (outbuf, payload_buffer);
330
331 avail -= payload_len;
332
Sebastian Drögedc059ef2015-06-10 14:33:50 +0200333 GST_BUFFER_PTS (outbuf) = rtpac3pay->first_ts;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100334 GST_BUFFER_DURATION (outbuf) = rtpac3pay->duration;
335
Wim Taymans249d0082011-11-11 12:25:01 +0100336 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpac3pay), outbuf);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100337 }
338
339 return ret;
340}
341
342static GstFlowReturn
Wim Taymans249d0082011-11-11 12:25:01 +0100343gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
Wim Taymans6db12cb2010-12-21 22:34:49 +0100344 GstBuffer * buffer)
345{
346 GstRtpAC3Pay *rtpac3pay;
347 GstFlowReturn ret;
Wim Taymans583d39d2012-01-23 17:25:37 +0100348 gsize avail, left, NF;
349 GstMapInfo map;
350 guint8 *p;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100351 guint packet_len;
352 GstClockTime duration, timestamp;
353
354 rtpac3pay = GST_RTP_AC3_PAY (basepayload);
355
Wim Taymans583d39d2012-01-23 17:25:37 +0100356 gst_buffer_map (buffer, &map, GST_MAP_READ);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100357 duration = GST_BUFFER_DURATION (buffer);
Sebastian Drögedc059ef2015-06-10 14:33:50 +0200358 timestamp = GST_BUFFER_PTS (buffer);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100359
360 if (GST_BUFFER_IS_DISCONT (buffer)) {
361 GST_DEBUG_OBJECT (rtpac3pay, "DISCONT");
362 gst_rtp_ac3_pay_reset (rtpac3pay);
363 }
364
365 /* count the amount of incomming packets */
366 NF = 0;
Wim Taymans583d39d2012-01-23 17:25:37 +0100367 left = map.size;
368 p = map.data;
Wim Taymans6db12cb2010-12-21 22:34:49 +0100369 while (TRUE) {
370 guint bsid, fscod, frmsizecod, frame_size;
371
372 if (left < 6)
373 break;
374
375 if (p[0] != 0x0b || p[1] != 0x77)
376 break;
377
378 bsid = p[5] >> 3;
379 if (bsid > 8)
380 break;
381
382 frmsizecod = p[4] & 0x3f;
383 fscod = p[4] >> 6;
384
385 GST_DEBUG_OBJECT (rtpac3pay, "fscod %u, %u", fscod, frmsizecod);
386
387 if (fscod >= 3 || frmsizecod >= 38)
388 break;
389
390 frame_size = frmsizecod_tbl[frmsizecod].frm_size[fscod] * 2;
391 if (frame_size > left)
392 break;
393
394 NF++;
Matej Knopp1e5dd9e2011-11-21 20:31:31 +0100395 GST_DEBUG_OBJECT (rtpac3pay, "found frame %" G_GSIZE_FORMAT " of size %u",
396 NF, frame_size);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100397
398 p += frame_size;
399 left -= frame_size;
400 }
Wim Taymans583d39d2012-01-23 17:25:37 +0100401 gst_buffer_unmap (buffer, &map);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100402 if (NF == 0)
403 goto no_frames;
404
405 avail = gst_adapter_available (rtpac3pay->adapter);
406
407 /* get packet length of previous data and this new data,
408 * payload length includes a 4 byte header */
Wim Taymans583d39d2012-01-23 17:25:37 +0100409 packet_len = gst_rtp_buffer_calc_packet_len (2 + avail + map.size, 0, 0);
Wim Taymans6db12cb2010-12-21 22:34:49 +0100410
411 /* if this buffer is going to overflow the packet, flush what we
412 * have. */
Wim Taymans249d0082011-11-11 12:25:01 +0100413 if (gst_rtp_base_payload_is_filled (basepayload,
Wim Taymans6db12cb2010-12-21 22:34:49 +0100414 packet_len, rtpac3pay->duration + duration)) {
415 ret = gst_rtp_ac3_pay_flush (rtpac3pay);
416 avail = 0;
417 } else {
418 ret = GST_FLOW_OK;
419 }
420
421 if (avail == 0) {
422 GST_DEBUG_OBJECT (rtpac3pay,
423 "first packet, save timestamp %" GST_TIME_FORMAT,
424 GST_TIME_ARGS (timestamp));
425 rtpac3pay->first_ts = timestamp;
426 rtpac3pay->duration = 0;
427 rtpac3pay->NF = 0;
428 }
429
430 gst_adapter_push (rtpac3pay->adapter, buffer);
431 rtpac3pay->duration += duration;
432 rtpac3pay->NF += NF;
433
434 return ret;
435
436 /* ERRORS */
437no_frames:
438 {
439 GST_WARNING_OBJECT (rtpac3pay, "no valid AC3 frames found");
440 return GST_FLOW_OK;
441 }
442}
443
444static GstStateChangeReturn
445gst_rtp_ac3_pay_change_state (GstElement * element, GstStateChange transition)
446{
447 GstRtpAC3Pay *rtpac3pay;
448 GstStateChangeReturn ret;
449
450 rtpac3pay = GST_RTP_AC3_PAY (element);
451
452 switch (transition) {
453 case GST_STATE_CHANGE_READY_TO_PAUSED:
454 gst_rtp_ac3_pay_reset (rtpac3pay);
455 break;
456 default:
457 break;
458 }
459
460 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
461
462 switch (transition) {
463 case GST_STATE_CHANGE_PAUSED_TO_READY:
464 gst_rtp_ac3_pay_reset (rtpac3pay);
465 break;
466 default:
467 break;
468 }
469 return ret;
470}
471
472gboolean
473gst_rtp_ac3_pay_plugin_init (GstPlugin * plugin)
474{
475 return gst_element_register (plugin, "rtpac3pay",
476 GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_PAY);
477}