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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmpapay.h"
30 /* elementfactory information */
31 static const GstElementDetails gst_rtp_mpapay_details =
32 GST_ELEMENT_DETAILS ("RTP MPEG audio payloader",
33 "Codec/Payloader/Network",
34 "Payload MPEG audio as RTP packets (RFC 2038)",
35 "Wim Taymans <wim.taymans@gmail.com>");
37 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
41 GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
44 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) \"audio\", "
50 "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
51 "clock-rate = (int) 90000; "
53 "media = (string) \"audio\", "
54 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55 "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
58 static void gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass);
59 static void gst_rtp_mpa_pay_base_init (GstRtpMPAPayClass * klass);
60 static void gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay);
61 static void gst_rtp_mpa_pay_finalize (GObject * object);
63 static gboolean gst_rtp_mpa_pay_setcaps (GstBaseRTPPayload * payload,
65 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * payload,
68 static GstBaseRTPPayloadClass *parent_class = NULL;
71 gst_rtp_mpa_pay_get_type (void)
73 static GType rtpmpapay_type = 0;
75 if (!rtpmpapay_type) {
76 static const GTypeInfo rtpmpapay_info = {
77 sizeof (GstRtpMPAPayClass),
78 (GBaseInitFunc) gst_rtp_mpa_pay_base_init,
80 (GClassInitFunc) gst_rtp_mpa_pay_class_init,
83 sizeof (GstRtpMPAPay),
85 (GInstanceInitFunc) gst_rtp_mpa_pay_init,
89 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpMPAPay",
92 return rtpmpapay_type;
96 gst_rtp_mpa_pay_base_init (GstRtpMPAPayClass * klass)
98 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
100 gst_element_class_add_pad_template (element_class,
101 gst_static_pad_template_get (&gst_rtp_mpa_pay_src_template));
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&gst_rtp_mpa_pay_sink_template));
105 gst_element_class_set_details (element_class, &gst_rtp_mpapay_details);
109 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
111 GObjectClass *gobject_class;
112 GstBaseRTPPayloadClass *gstbasertppayload_class;
114 gobject_class = (GObjectClass *) klass;
115 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
117 parent_class = g_type_class_peek_parent (klass);
119 gobject_class->finalize = gst_rtp_mpa_pay_finalize;
121 gstbasertppayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
122 gstbasertppayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
126 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
128 rtpmpapay->adapter = gst_adapter_new ();
132 gst_rtp_mpa_pay_finalize (GObject * object)
134 GstRtpMPAPay *rtpmpapay;
136 rtpmpapay = GST_RTP_MPA_PAY (object);
138 g_object_unref (rtpmpapay->adapter);
139 rtpmpapay->adapter = NULL;
141 G_OBJECT_CLASS (parent_class)->finalize (object);
145 gst_rtp_mpa_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
149 gst_basertppayload_set_options (payload, "audio", TRUE, "MPA", 90000);
150 res = gst_basertppayload_set_outcaps (payload, NULL);
156 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
163 /* the data available in the adapter is either smaller
164 * than the MTU or bigger. In the case it is smaller, the complete
165 * adapter contents can be put in one packet. In the case the
166 * adapter has more than one MTU, we need to split the MPA data
167 * over multiple packets. The frag_offset in each packet header
168 * needs to be updated with the position in the MPA frame. */
169 avail = gst_adapter_available (rtpmpapay->adapter);
180 /* this will be the total lenght of the packet */
181 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
183 /* fill one MTU or all available bytes */
184 towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmpapay));
186 /* this is the payload length */
187 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
189 /* create buffer to hold the payload */
190 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
194 gst_rtp_buffer_set_payload_type (outbuf, GST_RTP_PAYLOAD_MPA);
198 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200 * | MBZ | Frag_offset |
201 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203 payload = gst_rtp_buffer_get_payload (outbuf);
206 payload[2] = frag_offset >> 8;
207 payload[3] = frag_offset & 0xff;
209 gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
210 gst_adapter_flush (rtpmpapay->adapter, payload_len);
212 avail -= payload_len;
213 frag_offset += payload_len;
216 gst_rtp_buffer_set_marker (outbuf, TRUE);
218 GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
219 GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
221 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpapay), outbuf);
228 gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * basepayload,
231 GstRtpMPAPay *rtpmpapay;
235 GstClockTime duration;
237 rtpmpapay = GST_RTP_MPA_PAY (basepayload);
239 size = GST_BUFFER_SIZE (buffer);
240 duration = GST_BUFFER_DURATION (buffer);
242 avail = gst_adapter_available (rtpmpapay->adapter);
244 rtpmpapay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
245 rtpmpapay->duration = 0;
248 /* get packet length of previous data and this new data,
249 * payload length includes a 4 byte header */
250 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
252 /* if this buffer is going to overflow the packet, flush what we
254 if (gst_basertppayload_is_filled (basepayload,
255 packet_len, rtpmpapay->duration + duration)) {
256 ret = gst_rtp_mpa_pay_flush (rtpmpapay);
257 rtpmpapay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
258 rtpmpapay->duration = 0;
263 gst_adapter_push (rtpmpapay->adapter, buffer);
264 rtpmpapay->duration += duration;
270 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
272 return gst_element_register (plugin, "rtpmpapay",
273 GST_RANK_NONE, GST_TYPE_RTP_MPA_PAY);