rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmpapay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpmpapay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpmpapay_debug);
33 #define GST_CAT_DEFAULT (rtpmpapay_debug)
34
35 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
40     );
41
42 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
43     GST_STATIC_PAD_TEMPLATE ("src",
44     GST_PAD_SRC,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("application/x-rtp, "
47         "media = (string) \"audio\", "
48         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
49         "clock-rate = (int) 90000; "
50         "application/x-rtp, "
51         "media = (string) \"audio\", "
52         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
54     );
55
56 static void gst_rtp_mpa_pay_finalize (GObject * object);
57
58 static GstStateChangeReturn gst_rtp_mpa_pay_change_state (GstElement * element,
59     GstStateChange transition);
60
61 static gboolean gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload,
62     GstCaps * caps);
63 static gboolean gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload,
64     GstEvent * event);
65 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
66 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * payload,
67     GstBuffer * buffer);
68
69 #define gst_rtp_mpa_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpMPAPay, gst_rtp_mpa_pay, GST_TYPE_RTP_BASE_PAYLOAD);
71
72 static void
73 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
74 {
75   GObjectClass *gobject_class;
76   GstElementClass *gstelement_class;
77   GstRTPBasePayloadClass *gstrtpbasepayload_class;
78
79   GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
80       "MPEG Audio RTP Depayloader");
81
82   gobject_class = (GObjectClass *) klass;
83   gstelement_class = (GstElementClass *) klass;
84   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
85
86   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
87
88   gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
89
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);
94
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>");
99
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;
103 }
104
105 static void
106 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
107 {
108   rtpmpapay->adapter = gst_adapter_new ();
109
110   GST_RTP_BASE_PAYLOAD (rtpmpapay)->pt = GST_RTP_PAYLOAD_MPA;
111 }
112
113 static void
114 gst_rtp_mpa_pay_finalize (GObject * object)
115 {
116   GstRtpMPAPay *rtpmpapay;
117
118   rtpmpapay = GST_RTP_MPA_PAY (object);
119
120   g_object_unref (rtpmpapay->adapter);
121   rtpmpapay->adapter = NULL;
122
123   G_OBJECT_CLASS (parent_class)->finalize (object);
124 }
125
126 static void
127 gst_rtp_mpa_pay_reset (GstRtpMPAPay * pay)
128 {
129   pay->first_ts = -1;
130   pay->duration = 0;
131   gst_adapter_clear (pay->adapter);
132   GST_DEBUG_OBJECT (pay, "reset depayloader");
133 }
134
135 static gboolean
136 gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
137 {
138   gboolean res;
139
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);
143
144   return res;
145 }
146
147 static gboolean
148 gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
149 {
150   gboolean ret;
151   GstRtpMPAPay *rtpmpapay;
152
153   rtpmpapay = GST_RTP_MPA_PAY (payload);
154
155   switch (GST_EVENT_TYPE (event)) {
156     case GST_EVENT_EOS:
157       /* make sure we push the last packets in the adapter on EOS */
158       gst_rtp_mpa_pay_flush (rtpmpapay);
159       break;
160     case GST_EVENT_FLUSH_STOP:
161       gst_rtp_mpa_pay_reset (rtpmpapay);
162       break;
163     default:
164       break;
165   }
166
167   ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
168
169   return ret;
170 }
171
172 #define RTP_HEADER_LEN 12
173
174 static GstFlowReturn
175 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
176 {
177   guint avail;
178   GstBuffer *outbuf;
179   GstFlowReturn ret;
180   guint16 frag_offset;
181   GstBufferList *list;
182
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);
190
191   ret = GST_FLOW_OK;
192
193   list =
194       gst_buffer_list_new_sized (avail / (GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay) -
195           RTP_HEADER_LEN) + 1);
196
197   frag_offset = 0;
198   while (avail > 0) {
199     guint towrite;
200     guint8 *payload;
201     guint payload_len;
202     guint packet_len;
203     GstRTPBuffer rtp = { NULL };
204     GstBuffer *paybuf;
205
206     /* this will be the total length of the packet */
207     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
208
209     /* fill one MTU or all available bytes */
210     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
211
212     /* this is the payload length */
213     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
214
215     /* create buffer to hold the payload */
216     outbuf =
217         gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
218         (rtpmpapay), 4, 0, 0);
219
220     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
221
222     payload_len -= 4;
223
224     gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
225
226     /*
227      *  0                   1                   2                   3
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      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
232      */
233     payload = gst_rtp_buffer_get_payload (&rtp);
234     payload[0] = 0;
235     payload[1] = 0;
236     payload[2] = frag_offset >> 8;
237     payload[3] = frag_offset & 0xff;
238
239     avail -= payload_len;
240     frag_offset += payload_len;
241
242     if (avail == 0)
243       gst_rtp_buffer_set_marker (&rtp, TRUE);
244
245     gst_rtp_buffer_unmap (&rtp);
246
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);
250
251     GST_BUFFER_PTS (outbuf) = rtpmpapay->first_ts;
252     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
253     gst_buffer_list_add (list, outbuf);
254   }
255
256   ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpapay), list);
257
258   return ret;
259 }
260
261 static GstFlowReturn
262 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
263     GstBuffer * buffer)
264 {
265   GstRtpMPAPay *rtpmpapay;
266   GstFlowReturn ret;
267   guint size, avail;
268   guint packet_len;
269   GstClockTime duration, timestamp;
270
271   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
272
273   size = gst_buffer_get_size (buffer);
274   duration = GST_BUFFER_DURATION (buffer);
275   timestamp = GST_BUFFER_PTS (buffer);
276
277   if (GST_BUFFER_IS_DISCONT (buffer)) {
278     GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
279     gst_rtp_mpa_pay_reset (rtpmpapay);
280   }
281
282   avail = gst_adapter_available (rtpmpapay->adapter);
283
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);
287
288   /* if this buffer is going to overflow the packet, flush what we
289    * have. */
290   if (gst_rtp_base_payload_is_filled (basepayload,
291           packet_len, rtpmpapay->duration + duration)) {
292     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
293     avail = 0;
294   } else {
295     ret = GST_FLOW_OK;
296   }
297
298   if (avail == 0) {
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;
304   }
305
306   gst_adapter_push (rtpmpapay->adapter, buffer);
307   rtpmpapay->duration = duration;
308
309   return ret;
310 }
311
312 static GstStateChangeReturn
313 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
314 {
315   GstRtpMPAPay *rtpmpapay;
316   GstStateChangeReturn ret;
317
318   rtpmpapay = GST_RTP_MPA_PAY (element);
319
320   switch (transition) {
321     case GST_STATE_CHANGE_READY_TO_PAUSED:
322       gst_rtp_mpa_pay_reset (rtpmpapay);
323       break;
324     default:
325       break;
326   }
327
328   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
329
330   switch (transition) {
331     case GST_STATE_CHANGE_PAUSED_TO_READY:
332       gst_rtp_mpa_pay_reset (rtpmpapay);
333       break;
334     default:
335       break;
336   }
337   return ret;
338 }
339
340 gboolean
341 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
342 {
343   return gst_element_register (plugin, "rtpmpapay",
344       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);
345 }