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 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 (GstBaseRTPPayload * payload,
61 static gboolean gst_rtp_mpa_pay_handle_event (GstPad * pad, GstEvent * event);
62 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
63 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * payload,
66 GST_BOILERPLATE (GstRtpMPAPay, gst_rtp_mpa_pay, GstBaseRTPPayload,
67 GST_TYPE_BASE_RTP_PAYLOAD)
69 static void gst_rtp_mpa_pay_base_init (gpointer klass)
71 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
73 gst_element_class_add_static_pad_template (element_class,
74 &gst_rtp_mpa_pay_src_template);
75 gst_element_class_add_static_pad_template (element_class,
76 &gst_rtp_mpa_pay_sink_template);
78 gst_element_class_set_details_simple (element_class,
79 "RTP MPEG audio payloader", "Codec/Payloader/Network/RTP",
80 "Payload MPEG audio as RTP packets (RFC 2038)",
81 "Wim Taymans <wim.taymans@gmail.com>");
85 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
87 GObjectClass *gobject_class;
88 GstElementClass *gstelement_class;
89 GstBaseRTPPayloadClass *gstbasertppayload_class;
91 gobject_class = (GObjectClass *) klass;
92 gstelement_class = (GstElementClass *) klass;
93 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
95 gobject_class->finalize = gst_rtp_mpa_pay_finalize;
97 gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
99 gstbasertppayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
100 gstbasertppayload_class->handle_event = gst_rtp_mpa_pay_handle_event;
101 gstbasertppayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
103 GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
104 "MPEG Audio RTP Depayloader");
108 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay, GstRtpMPAPayClass * klass)
110 rtpmpapay->adapter = gst_adapter_new ();
114 gst_rtp_mpa_pay_finalize (GObject * object)
116 GstRtpMPAPay *rtpmpapay;
118 rtpmpapay = GST_RTP_MPA_PAY (object);
120 g_object_unref (rtpmpapay->adapter);
121 rtpmpapay->adapter = NULL;
123 G_OBJECT_CLASS (parent_class)->finalize (object);
127 gst_rtp_mpa_pay_reset (GstRtpMPAPay * pay)
131 gst_adapter_clear (pay->adapter);
132 GST_DEBUG_OBJECT (pay, "reset depayloader");
136 gst_rtp_mpa_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
140 gst_basertppayload_set_options (payload, "audio", TRUE, "MPA", 90000);
141 res = gst_basertppayload_set_outcaps (payload, NULL);
147 gst_rtp_mpa_pay_handle_event (GstPad * pad, GstEvent * event)
149 GstRtpMPAPay *rtpmpapay;
151 rtpmpapay = GST_RTP_MPA_PAY (gst_pad_get_parent (pad));
153 switch (GST_EVENT_TYPE (event)) {
155 /* make sure we push the last packets in the adapter on EOS */
156 gst_rtp_mpa_pay_flush (rtpmpapay);
158 case GST_EVENT_FLUSH_STOP:
159 gst_rtp_mpa_pay_reset (rtpmpapay);
165 gst_object_unref (rtpmpapay);
167 /* FALSE to let the parent handle the event as well */
172 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
179 /* the data available in the adapter is either smaller
180 * than the MTU or bigger. In the case it is smaller, the complete
181 * adapter contents can be put in one packet. In the case the
182 * adapter has more than one MTU, we need to split the MPA data
183 * over multiple packets. The frag_offset in each packet header
184 * needs to be updated with the position in the MPA frame. */
185 avail = gst_adapter_available (rtpmpapay->adapter);
196 /* this will be the total length of the packet */
197 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
199 /* fill one MTU or all available bytes */
200 towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmpapay));
202 /* this is the payload length */
203 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
205 /* create buffer to hold the payload */
206 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
210 gst_rtp_buffer_set_payload_type (outbuf, 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 (outbuf);
222 payload[2] = frag_offset >> 8;
223 payload[3] = frag_offset & 0xff;
225 gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
226 gst_adapter_flush (rtpmpapay->adapter, payload_len);
228 avail -= payload_len;
229 frag_offset += payload_len;
232 gst_rtp_buffer_set_marker (outbuf, TRUE);
234 GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
235 GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
237 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpapay), outbuf);
244 gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * basepayload,
247 GstRtpMPAPay *rtpmpapay;
251 GstClockTime duration, timestamp;
253 rtpmpapay = GST_RTP_MPA_PAY (basepayload);
255 size = GST_BUFFER_SIZE (buffer);
256 duration = GST_BUFFER_DURATION (buffer);
257 timestamp = GST_BUFFER_TIMESTAMP (buffer);
259 if (GST_BUFFER_IS_DISCONT (buffer)) {
260 GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
261 gst_rtp_mpa_pay_reset (rtpmpapay);
264 avail = gst_adapter_available (rtpmpapay->adapter);
266 /* get packet length of previous data and this new data,
267 * payload length includes a 4 byte header */
268 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
270 /* if this buffer is going to overflow the packet, flush what we
272 if (gst_basertppayload_is_filled (basepayload,
273 packet_len, rtpmpapay->duration + duration)) {
274 ret = gst_rtp_mpa_pay_flush (rtpmpapay);
281 GST_DEBUG_OBJECT (rtpmpapay,
282 "first packet, save timestamp %" GST_TIME_FORMAT,
283 GST_TIME_ARGS (timestamp));
284 rtpmpapay->first_ts = timestamp;
285 rtpmpapay->duration = 0;
288 gst_adapter_push (rtpmpapay->adapter, buffer);
289 rtpmpapay->duration = duration;
294 static GstStateChangeReturn
295 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
297 GstRtpMPAPay *rtpmpapay;
298 GstStateChangeReturn ret;
300 rtpmpapay = GST_RTP_MPA_PAY (element);
302 switch (transition) {
303 case GST_STATE_CHANGE_READY_TO_PAUSED:
304 gst_rtp_mpa_pay_reset (rtpmpapay);
310 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
312 switch (transition) {
313 case GST_STATE_CHANGE_PAUSED_TO_READY:
314 gst_rtp_mpa_pay_reset (rtpmpapay);
323 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
325 return gst_element_register (plugin, "rtpmpapay",
326 GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);