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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpac3pay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpac3pay_debug);
31 #define GST_CAT_DEFAULT (rtpac3pay_debug)
33 static GstStaticPadTemplate gst_rtp_ac3_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_STATIC_CAPS ("audio/ac3; " "audio/x-ac3; ")
40 static GstStaticPadTemplate gst_rtp_ac3_pay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("application/x-rtp, "
45 "media = (string) \"audio\", "
46 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
47 "clock-rate = (int) { 32000, 44100, 48000 }, "
48 "encoding-name = (string) \"AC3\"")
51 static void gst_rtp_ac3_pay_finalize (GObject * object);
53 static GstStateChangeReturn gst_rtp_ac3_pay_change_state (GstElement * element,
54 GstStateChange transition);
56 static gboolean gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload,
58 static gboolean gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload,
60 static GstFlowReturn gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay);
61 static GstFlowReturn gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * payload,
64 #define gst_rtp_ac3_pay_parent_class parent_class
65 G_DEFINE_TYPE (GstRtpAC3Pay, gst_rtp_ac3_pay, GST_TYPE_RTP_BASE_PAYLOAD);
68 gst_rtp_ac3_pay_class_init (GstRtpAC3PayClass * klass)
70 GObjectClass *gobject_class;
71 GstElementClass *gstelement_class;
72 GstRTPBasePayloadClass *gstrtpbasepayload_class;
74 GST_DEBUG_CATEGORY_INIT (rtpac3pay_debug, "rtpac3pay", 0,
75 "AC3 Audio RTP Depayloader");
77 gobject_class = (GObjectClass *) klass;
78 gstelement_class = (GstElementClass *) klass;
79 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
81 gobject_class->finalize = gst_rtp_ac3_pay_finalize;
83 gstelement_class->change_state = gst_rtp_ac3_pay_change_state;
85 gst_element_class_add_pad_template (gstelement_class,
86 gst_static_pad_template_get (&gst_rtp_ac3_pay_src_template));
87 gst_element_class_add_pad_template (gstelement_class,
88 gst_static_pad_template_get (&gst_rtp_ac3_pay_sink_template));
90 gst_element_class_set_details_simple (gstelement_class,
91 "RTP AC3 audio payloader", "Codec/Payloader/Network/RTP",
92 "Payload AC3 audio as RTP packets (RFC 4184)",
93 "Wim Taymans <wim.taymans@gmail.com>");
95 gstrtpbasepayload_class->set_caps = gst_rtp_ac3_pay_setcaps;
96 gstrtpbasepayload_class->sink_event = gst_rtp_ac3_pay_sink_event;
97 gstrtpbasepayload_class->handle_buffer = gst_rtp_ac3_pay_handle_buffer;
101 gst_rtp_ac3_pay_init (GstRtpAC3Pay * rtpac3pay)
103 rtpac3pay->adapter = gst_adapter_new ();
107 gst_rtp_ac3_pay_finalize (GObject * object)
109 GstRtpAC3Pay *rtpac3pay;
111 rtpac3pay = GST_RTP_AC3_PAY (object);
113 g_object_unref (rtpac3pay->adapter);
115 G_OBJECT_CLASS (parent_class)->finalize (object);
119 gst_rtp_ac3_pay_reset (GstRtpAC3Pay * pay)
123 gst_adapter_clear (pay->adapter);
124 GST_DEBUG_OBJECT (pay, "reset depayloader");
128 gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
132 GstStructure *structure;
134 structure = gst_caps_get_structure (caps, 0);
136 if (!gst_structure_get_int (structure, "rate", &rate))
137 rate = 90000; /* default */
139 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "AC3", rate);
140 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
146 gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
149 GstRtpAC3Pay *rtpac3pay;
151 rtpac3pay = GST_RTP_AC3_PAY (payload);
153 switch (GST_EVENT_TYPE (event)) {
155 /* make sure we push the last packets in the adapter on EOS */
156 gst_rtp_ac3_pay_flush (rtpac3pay);
158 case GST_EVENT_FLUSH_STOP:
159 gst_rtp_ac3_pay_reset (rtpac3pay);
165 res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
176 static const struct frmsize_s frmsizecod_tbl[] = {
181 {48, {96, 104, 144}},
182 {48, {96, 105, 144}},
183 {56, {112, 121, 168}},
184 {56, {112, 122, 168}},
185 {64, {128, 139, 192}},
186 {64, {128, 140, 192}},
187 {80, {160, 174, 240}},
188 {80, {160, 175, 240}},
189 {96, {192, 208, 288}},
190 {96, {192, 209, 288}},
191 {112, {224, 243, 336}},
192 {112, {224, 244, 336}},
193 {128, {256, 278, 384}},
194 {128, {256, 279, 384}},
195 {160, {320, 348, 480}},
196 {160, {320, 349, 480}},
197 {192, {384, 417, 576}},
198 {192, {384, 418, 576}},
199 {224, {448, 487, 672}},
200 {224, {448, 488, 672}},
201 {256, {512, 557, 768}},
202 {256, {512, 558, 768}},
203 {320, {640, 696, 960}},
204 {320, {640, 697, 960}},
205 {384, {768, 835, 1152}},
206 {384, {768, 836, 1152}},
207 {448, {896, 975, 1344}},
208 {448, {896, 976, 1344}},
209 {512, {1024, 1114, 1536}},
210 {512, {1024, 1115, 1536}},
211 {576, {1152, 1253, 1728}},
212 {576, {1152, 1254, 1728}},
213 {640, {1280, 1393, 1920}},
214 {640, {1280, 1394, 1920}}
218 gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
220 guint avail, FT, NF, mtu;
224 /* the data available in the adapter is either smaller
225 * than the MTU or bigger. In the case it is smaller, the complete
226 * adapter contents can be put in one packet. In the case the
227 * adapter has more than one MTU, we need to split the AC3 data
228 * over multiple packets. */
229 avail = gst_adapter_available (rtpac3pay->adapter);
234 /* number of frames */
237 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpac3pay);
239 GST_LOG_OBJECT (rtpac3pay, "flushing %u bytes", avail);
246 GstRTPBuffer rtp = { NULL, };
248 /* this will be the total length of the packet */
249 packet_len = gst_rtp_buffer_calc_packet_len (2 + avail, 0, 0);
251 /* fill one MTU or all available bytes */
252 towrite = MIN (packet_len, mtu);
254 /* this is the payload length */
255 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
257 /* create buffer to hold the payload */
258 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
261 /* check if it all fits */
262 if (towrite < packet_len) {
265 GST_LOG_OBJECT (rtpac3pay, "we need to fragment");
266 /* check if we will be able to put at least 5/8th of the total
267 * frame in this first frame. */
268 if ((avail * 5) / 8 >= (payload_len - 2))
272 /* check how many fragments we will need */
273 maxlen = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
274 NF = (avail + maxlen - 1) / maxlen;
276 } else if (FT != 3) {
277 /* remaining fragment */
283 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
284 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
286 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
288 * FT: 0: one or more complete frames
289 * 1: initial 5/8 fragment
290 * 2: initial fragment not 5/8
292 * NF: amount of frames if FT = 0, else number of fragments.
294 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
295 GST_LOG_OBJECT (rtpac3pay, "FT %u, NF %u", FT, NF);
296 payload = gst_rtp_buffer_get_payload (&rtp);
297 payload[0] = (FT & 3);
301 gst_adapter_copy (rtpac3pay->adapter, &payload[2], 0, payload_len);
302 gst_adapter_flush (rtpac3pay->adapter, payload_len);
304 avail -= payload_len;
306 gst_rtp_buffer_set_marker (&rtp, TRUE);
307 gst_rtp_buffer_unmap (&rtp);
309 GST_BUFFER_TIMESTAMP (outbuf) = rtpac3pay->first_ts;
310 GST_BUFFER_DURATION (outbuf) = rtpac3pay->duration;
312 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpac3pay), outbuf);
319 gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
322 GstRtpAC3Pay *rtpac3pay;
324 gsize size, avail, left, NF;
327 GstClockTime duration, timestamp;
329 rtpac3pay = GST_RTP_AC3_PAY (basepayload);
331 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
332 duration = GST_BUFFER_DURATION (buffer);
333 timestamp = GST_BUFFER_TIMESTAMP (buffer);
335 if (GST_BUFFER_IS_DISCONT (buffer)) {
336 GST_DEBUG_OBJECT (rtpac3pay, "DISCONT");
337 gst_rtp_ac3_pay_reset (rtpac3pay);
340 /* count the amount of incomming packets */
345 guint bsid, fscod, frmsizecod, frame_size;
350 if (p[0] != 0x0b || p[1] != 0x77)
357 frmsizecod = p[4] & 0x3f;
360 GST_DEBUG_OBJECT (rtpac3pay, "fscod %u, %u", fscod, frmsizecod);
362 if (fscod >= 3 || frmsizecod >= 38)
365 frame_size = frmsizecod_tbl[frmsizecod].frm_size[fscod] * 2;
366 if (frame_size > left)
370 GST_DEBUG_OBJECT (rtpac3pay, "found frame %" G_GSIZE_FORMAT " of size %u",
376 gst_buffer_unmap (buffer, data, size);
380 avail = gst_adapter_available (rtpac3pay->adapter);
382 /* get packet length of previous data and this new data,
383 * payload length includes a 4 byte header */
384 packet_len = gst_rtp_buffer_calc_packet_len (2 + avail + size, 0, 0);
386 /* if this buffer is going to overflow the packet, flush what we
388 if (gst_rtp_base_payload_is_filled (basepayload,
389 packet_len, rtpac3pay->duration + duration)) {
390 ret = gst_rtp_ac3_pay_flush (rtpac3pay);
397 GST_DEBUG_OBJECT (rtpac3pay,
398 "first packet, save timestamp %" GST_TIME_FORMAT,
399 GST_TIME_ARGS (timestamp));
400 rtpac3pay->first_ts = timestamp;
401 rtpac3pay->duration = 0;
405 gst_adapter_push (rtpac3pay->adapter, buffer);
406 rtpac3pay->duration += duration;
414 GST_WARNING_OBJECT (rtpac3pay, "no valid AC3 frames found");
419 static GstStateChangeReturn
420 gst_rtp_ac3_pay_change_state (GstElement * element, GstStateChange transition)
422 GstRtpAC3Pay *rtpac3pay;
423 GstStateChangeReturn ret;
425 rtpac3pay = GST_RTP_AC3_PAY (element);
427 switch (transition) {
428 case GST_STATE_CHANGE_READY_TO_PAUSED:
429 gst_rtp_ac3_pay_reset (rtpac3pay);
435 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
437 switch (transition) {
438 case GST_STATE_CHANGE_PAUSED_TO_READY:
439 gst_rtp_ac3_pay_reset (rtpac3pay);
448 gst_rtp_ac3_pay_plugin_init (GstPlugin * plugin)
450 return gst_element_register (plugin, "rtpac3pay",
451 GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_PAY);