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 (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 };
193 /* this will be the total length of the packet */
194 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
196 /* fill one MTU or all available bytes */
197 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
199 /* this is the payload length */
200 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
202 /* create buffer to hold the payload */
203 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
205 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
209 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
213 * 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
214 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215 * | MBZ | Frag_offset |
216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218 payload = gst_rtp_buffer_get_payload (&rtp);
221 payload[2] = frag_offset >> 8;
222 payload[3] = frag_offset & 0xff;
224 gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
225 gst_adapter_flush (rtpmpapay->adapter, payload_len);
227 avail -= payload_len;
228 frag_offset += payload_len;
231 gst_rtp_buffer_set_marker (&rtp, TRUE);
233 gst_rtp_buffer_unmap (&rtp);
235 GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
236 GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
238 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmpapay), outbuf);
245 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
248 GstRtpMPAPay *rtpmpapay;
252 GstClockTime duration, timestamp;
254 rtpmpapay = GST_RTP_MPA_PAY (basepayload);
256 size = gst_buffer_get_size (buffer);
257 duration = GST_BUFFER_DURATION (buffer);
258 timestamp = GST_BUFFER_TIMESTAMP (buffer);
260 if (GST_BUFFER_IS_DISCONT (buffer)) {
261 GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
262 gst_rtp_mpa_pay_reset (rtpmpapay);
265 avail = gst_adapter_available (rtpmpapay->adapter);
267 /* get packet length of previous data and this new data,
268 * payload length includes a 4 byte header */
269 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
271 /* if this buffer is going to overflow the packet, flush what we
273 if (gst_rtp_base_payload_is_filled (basepayload,
274 packet_len, rtpmpapay->duration + duration)) {
275 ret = gst_rtp_mpa_pay_flush (rtpmpapay);
282 GST_DEBUG_OBJECT (rtpmpapay,
283 "first packet, save timestamp %" GST_TIME_FORMAT,
284 GST_TIME_ARGS (timestamp));
285 rtpmpapay->first_ts = timestamp;
286 rtpmpapay->duration = 0;
289 gst_adapter_push (rtpmpapay->adapter, buffer);
290 rtpmpapay->duration = duration;
295 static GstStateChangeReturn
296 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
298 GstRtpMPAPay *rtpmpapay;
299 GstStateChangeReturn ret;
301 rtpmpapay = GST_RTP_MPA_PAY (element);
303 switch (transition) {
304 case GST_STATE_CHANGE_READY_TO_PAUSED:
305 gst_rtp_mpa_pay_reset (rtpmpapay);
311 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
313 switch (transition) {
314 case GST_STATE_CHANGE_PAUSED_TO_READY:
315 gst_rtp_mpa_pay_reset (rtpmpapay);
324 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
326 return gst_element_register (plugin, "rtpmpapay",
327 GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);