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>
27 #include <gst/audio/audio.h>
29 #include "gstrtpmpapay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpmpapay_debug);
33 #define GST_CAT_DEFAULT (rtpmpapay_debug)
35 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
42 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"audio\", "
48 "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
49 "clock-rate = (int) 90000; "
51 "media = (string) \"audio\", "
52 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53 "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
56 static void gst_rtp_mpa_pay_finalize (GObject * object);
58 static GstStateChangeReturn gst_rtp_mpa_pay_change_state (GstElement * element,
59 GstStateChange transition);
61 static gboolean gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload,
63 static gboolean gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload,
65 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
66 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * payload,
69 #define gst_rtp_mpa_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpMPAPay, gst_rtp_mpa_pay, GST_TYPE_RTP_BASE_PAYLOAD);
73 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
75 GObjectClass *gobject_class;
76 GstElementClass *gstelement_class;
77 GstRTPBasePayloadClass *gstrtpbasepayload_class;
79 GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
80 "MPEG Audio RTP Depayloader");
82 gobject_class = (GObjectClass *) klass;
83 gstelement_class = (GstElementClass *) klass;
84 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
86 gobject_class->finalize = gst_rtp_mpa_pay_finalize;
88 gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
90 gst_element_class_add_static_pad_template (gstelement_class,
91 &gst_rtp_mpa_pay_src_template);
92 gst_element_class_add_static_pad_template (gstelement_class,
93 &gst_rtp_mpa_pay_sink_template);
95 gst_element_class_set_static_metadata (gstelement_class,
96 "RTP MPEG audio payloader", "Codec/Payloader/Network/RTP",
97 "Payload MPEG audio as RTP packets (RFC 2038)",
98 "Wim Taymans <wim.taymans@gmail.com>");
100 gstrtpbasepayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
101 gstrtpbasepayload_class->sink_event = gst_rtp_mpa_pay_sink_event;
102 gstrtpbasepayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
106 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
108 rtpmpapay->adapter = gst_adapter_new ();
110 GST_RTP_BASE_PAYLOAD (rtpmpapay)->pt = GST_RTP_PAYLOAD_MPA;
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 (GstRTPBasePayload * payload, GstCaps * caps)
140 gst_rtp_base_payload_set_options (payload, "audio",
141 payload->pt != GST_RTP_PAYLOAD_MPA, "MPA", 90000);
142 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
148 gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
151 GstRtpMPAPay *rtpmpapay;
153 rtpmpapay = GST_RTP_MPA_PAY (payload);
155 switch (GST_EVENT_TYPE (event)) {
157 /* make sure we push the last packets in the adapter on EOS */
158 gst_rtp_mpa_pay_flush (rtpmpapay);
160 case GST_EVENT_FLUSH_STOP:
161 gst_rtp_mpa_pay_reset (rtpmpapay);
167 ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
172 #define RTP_HEADER_LEN 12
175 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
183 /* the data available in the adapter is either smaller
184 * than the MTU or bigger. In the case it is smaller, the complete
185 * adapter contents can be put in one packet. In the case the
186 * adapter has more than one MTU, we need to split the MPA data
187 * over multiple packets. The frag_offset in each packet header
188 * needs to be updated with the position in the MPA frame. */
189 avail = gst_adapter_available (rtpmpapay->adapter);
194 gst_buffer_list_new_sized (avail / (GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay) -
195 RTP_HEADER_LEN) + 1);
203 GstRTPBuffer rtp = { NULL };
206 /* this will be the total length of the packet */
207 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
209 /* fill one MTU or all available bytes */
210 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
212 /* this is the payload length */
213 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
215 /* create buffer to hold the payload */
217 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
218 (rtpmpapay), 4, 0, 0);
220 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
224 gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
228 * 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
229 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230 * | MBZ | Frag_offset |
231 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233 payload = gst_rtp_buffer_get_payload (&rtp);
236 payload[2] = frag_offset >> 8;
237 payload[3] = frag_offset & 0xff;
239 avail -= payload_len;
240 frag_offset += payload_len;
243 gst_rtp_buffer_set_marker (&rtp, TRUE);
245 gst_rtp_buffer_unmap (&rtp);
247 paybuf = gst_adapter_take_buffer_fast (rtpmpapay->adapter, payload_len);
248 gst_rtp_copy_audio_meta (rtpmpapay, outbuf, paybuf);
249 outbuf = gst_buffer_append (outbuf, paybuf);
251 GST_BUFFER_PTS (outbuf) = rtpmpapay->first_ts;
252 GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
253 gst_buffer_list_add (list, outbuf);
256 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpapay), list);
262 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
265 GstRtpMPAPay *rtpmpapay;
269 GstClockTime duration, timestamp;
271 rtpmpapay = GST_RTP_MPA_PAY (basepayload);
273 size = gst_buffer_get_size (buffer);
274 duration = GST_BUFFER_DURATION (buffer);
275 timestamp = GST_BUFFER_PTS (buffer);
277 if (GST_BUFFER_IS_DISCONT (buffer)) {
278 GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
279 gst_rtp_mpa_pay_reset (rtpmpapay);
282 avail = gst_adapter_available (rtpmpapay->adapter);
284 /* get packet length of previous data and this new data,
285 * payload length includes a 4 byte header */
286 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
288 /* if this buffer is going to overflow the packet, flush what we
290 if (gst_rtp_base_payload_is_filled (basepayload,
291 packet_len, rtpmpapay->duration + duration)) {
292 ret = gst_rtp_mpa_pay_flush (rtpmpapay);
299 GST_DEBUG_OBJECT (rtpmpapay,
300 "first packet, save timestamp %" GST_TIME_FORMAT,
301 GST_TIME_ARGS (timestamp));
302 rtpmpapay->first_ts = timestamp;
303 rtpmpapay->duration = 0;
306 gst_adapter_push (rtpmpapay->adapter, buffer);
307 rtpmpapay->duration = duration;
312 static GstStateChangeReturn
313 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
315 GstRtpMPAPay *rtpmpapay;
316 GstStateChangeReturn ret;
318 rtpmpapay = GST_RTP_MPA_PAY (element);
320 switch (transition) {
321 case GST_STATE_CHANGE_READY_TO_PAUSED:
322 gst_rtp_mpa_pay_reset (rtpmpapay);
328 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
330 switch (transition) {
331 case GST_STATE_CHANGE_PAUSED_TO_READY:
332 gst_rtp_mpa_pay_reset (rtpmpapay);
341 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
343 return gst_element_register (plugin, "rtpmpapay",
344 GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);