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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmpapay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmpapay_debug);
31 #define GST_CAT_DEFAULT (rtpmpapay_debug)
33 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
40 static GstStaticPadTemplate gst_rtp_mpa_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_MPA_STRING ", "
47 "clock-rate = (int) 90000; "
49 "media = (string) \"audio\", "
50 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51 "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
54 static void gst_rtp_mpa_pay_finalize (GObject * object);
56 static GstStateChangeReturn gst_rtp_mpa_pay_change_state (GstElement * element,
57 GstStateChange transition);
59 static gboolean gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload,
61 static gboolean gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload,
63 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
64 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * payload,
67 #define gst_rtp_mpa_pay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpMPAPay, gst_rtp_mpa_pay, GST_TYPE_RTP_BASE_PAYLOAD);
71 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
73 GObjectClass *gobject_class;
74 GstElementClass *gstelement_class;
75 GstRTPBasePayloadClass *gstrtpbasepayload_class;
77 GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
78 "MPEG Audio RTP Depayloader");
80 gobject_class = (GObjectClass *) klass;
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
84 gobject_class->finalize = gst_rtp_mpa_pay_finalize;
86 gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
88 gst_element_class_add_pad_template (gstelement_class,
89 gst_static_pad_template_get (&gst_rtp_mpa_pay_src_template));
90 gst_element_class_add_pad_template (gstelement_class,
91 gst_static_pad_template_get (&gst_rtp_mpa_pay_sink_template));
93 gst_element_class_set_static_metadata (gstelement_class,
94 "RTP MPEG audio payloader", "Codec/Payloader/Network/RTP",
95 "Payload MPEG audio as RTP packets (RFC 2038)",
96 "Wim Taymans <wim.taymans@gmail.com>");
98 gstrtpbasepayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
99 gstrtpbasepayload_class->sink_event = gst_rtp_mpa_pay_sink_event;
100 gstrtpbasepayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
104 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
106 rtpmpapay->adapter = gst_adapter_new ();
110 gst_rtp_mpa_pay_finalize (GObject * object)
112 GstRtpMPAPay *rtpmpapay;
114 rtpmpapay = GST_RTP_MPA_PAY (object);
116 g_object_unref (rtpmpapay->adapter);
117 rtpmpapay->adapter = NULL;
119 G_OBJECT_CLASS (parent_class)->finalize (object);
123 gst_rtp_mpa_pay_reset (GstRtpMPAPay * pay)
127 gst_adapter_clear (pay->adapter);
128 GST_DEBUG_OBJECT (pay, "reset depayloader");
132 gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
136 gst_rtp_base_payload_set_options (payload, "audio", TRUE, "MPA", 90000);
137 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
143 gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
146 GstRtpMPAPay *rtpmpapay;
148 rtpmpapay = GST_RTP_MPA_PAY (payload);
150 switch (GST_EVENT_TYPE (event)) {
152 /* make sure we push the last packets in the adapter on EOS */
153 gst_rtp_mpa_pay_flush (rtpmpapay);
155 case GST_EVENT_FLUSH_STOP:
156 gst_rtp_mpa_pay_reset (rtpmpapay);
162 ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
168 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
175 /* the data available in the adapter is either smaller
176 * than the MTU or bigger. In the case it is smaller, the complete
177 * adapter contents can be put in one packet. In the case the
178 * adapter has more than one MTU, we need to split the MPA data
179 * over multiple packets. The frag_offset in each packet header
180 * needs to be updated with the position in the MPA frame. */
181 avail = gst_adapter_available (rtpmpapay->adapter);
191 GstRTPBuffer rtp = { NULL };
194 /* this will be the total length of the packet */
195 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
197 /* fill one MTU or all available bytes */
198 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
200 /* this is the payload length */
201 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
203 /* create buffer to hold the payload */
204 outbuf = gst_rtp_buffer_new_allocate (4, 0, 0);
206 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
210 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
214 * 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
215 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216 * | MBZ | Frag_offset |
217 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 payload = gst_rtp_buffer_get_payload (&rtp);
222 payload[2] = frag_offset >> 8;
223 payload[3] = frag_offset & 0xff;
225 avail -= payload_len;
226 frag_offset += payload_len;
229 gst_rtp_buffer_set_marker (&rtp, TRUE);
231 gst_rtp_buffer_unmap (&rtp);
233 paybuf = gst_adapter_take_buffer_fast (rtpmpapay->adapter, payload_len);
234 outbuf = gst_buffer_append (outbuf, paybuf);
236 GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
237 GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
239 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmpapay), outbuf);
246 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
249 GstRtpMPAPay *rtpmpapay;
253 GstClockTime duration, timestamp;
255 rtpmpapay = GST_RTP_MPA_PAY (basepayload);
257 size = gst_buffer_get_size (buffer);
258 duration = GST_BUFFER_DURATION (buffer);
259 timestamp = GST_BUFFER_TIMESTAMP (buffer);
261 if (GST_BUFFER_IS_DISCONT (buffer)) {
262 GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
263 gst_rtp_mpa_pay_reset (rtpmpapay);
266 avail = gst_adapter_available (rtpmpapay->adapter);
268 /* get packet length of previous data and this new data,
269 * payload length includes a 4 byte header */
270 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
272 /* if this buffer is going to overflow the packet, flush what we
274 if (gst_rtp_base_payload_is_filled (basepayload,
275 packet_len, rtpmpapay->duration + duration)) {
276 ret = gst_rtp_mpa_pay_flush (rtpmpapay);
283 GST_DEBUG_OBJECT (rtpmpapay,
284 "first packet, save timestamp %" GST_TIME_FORMAT,
285 GST_TIME_ARGS (timestamp));
286 rtpmpapay->first_ts = timestamp;
287 rtpmpapay->duration = 0;
290 gst_adapter_push (rtpmpapay->adapter, buffer);
291 rtpmpapay->duration = duration;
296 static GstStateChangeReturn
297 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
299 GstRtpMPAPay *rtpmpapay;
300 GstStateChangeReturn ret;
302 rtpmpapay = GST_RTP_MPA_PAY (element);
304 switch (transition) {
305 case GST_STATE_CHANGE_READY_TO_PAUSED:
306 gst_rtp_mpa_pay_reset (rtpmpapay);
312 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
314 switch (transition) {
315 case GST_STATE_CHANGE_PAUSED_TO_READY:
316 gst_rtp_mpa_pay_reset (rtpmpapay);
325 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
327 return gst_element_register (plugin, "rtpmpapay",
328 GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);