webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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 "gstrtpelements.h"
30 #include "gstrtpmpapay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpmpapay_debug);
34 #define GST_CAT_DEFAULT (rtpmpapay_debug)
35
36 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
41     );
42
43 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
44     GST_STATIC_PAD_TEMPLATE ("src",
45     GST_PAD_SRC,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) \"audio\", "
49         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
50         "clock-rate = (int) 90000; "
51         "application/x-rtp, "
52         "media = (string) \"audio\", "
53         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
54         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
55     );
56
57 static void gst_rtp_mpa_pay_finalize (GObject * object);
58
59 static GstStateChangeReturn gst_rtp_mpa_pay_change_state (GstElement * element,
60     GstStateChange transition);
61
62 static gboolean gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload,
63     GstCaps * caps);
64 static gboolean gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload,
65     GstEvent * event);
66 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
67 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * payload,
68     GstBuffer * buffer);
69
70 #define gst_rtp_mpa_pay_parent_class parent_class
71 G_DEFINE_TYPE (GstRtpMPAPay, gst_rtp_mpa_pay, GST_TYPE_RTP_BASE_PAYLOAD);
72 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpmpapay, "rtpmpapay",
73     GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY, rtp_element_init (plugin));
74
75 static void
76 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
77 {
78   GObjectClass *gobject_class;
79   GstElementClass *gstelement_class;
80   GstRTPBasePayloadClass *gstrtpbasepayload_class;
81
82   GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
83       "MPEG Audio RTP Depayloader");
84
85   gobject_class = (GObjectClass *) klass;
86   gstelement_class = (GstElementClass *) klass;
87   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
88
89   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
90
91   gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
92
93   gst_element_class_add_static_pad_template (gstelement_class,
94       &gst_rtp_mpa_pay_src_template);
95   gst_element_class_add_static_pad_template (gstelement_class,
96       &gst_rtp_mpa_pay_sink_template);
97
98   gst_element_class_set_static_metadata (gstelement_class,
99       "RTP MPEG audio payloader", "Codec/Payloader/Network/RTP",
100       "Payload MPEG audio as RTP packets (RFC 2038)",
101       "Wim Taymans <wim.taymans@gmail.com>");
102
103   gstrtpbasepayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
104   gstrtpbasepayload_class->sink_event = gst_rtp_mpa_pay_sink_event;
105   gstrtpbasepayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
106 }
107
108 static void
109 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
110 {
111   rtpmpapay->adapter = gst_adapter_new ();
112
113   GST_RTP_BASE_PAYLOAD (rtpmpapay)->pt = GST_RTP_PAYLOAD_MPA;
114 }
115
116 static void
117 gst_rtp_mpa_pay_finalize (GObject * object)
118 {
119   GstRtpMPAPay *rtpmpapay;
120
121   rtpmpapay = GST_RTP_MPA_PAY (object);
122
123   g_object_unref (rtpmpapay->adapter);
124   rtpmpapay->adapter = NULL;
125
126   G_OBJECT_CLASS (parent_class)->finalize (object);
127 }
128
129 static void
130 gst_rtp_mpa_pay_reset (GstRtpMPAPay * pay)
131 {
132   pay->first_ts = -1;
133   pay->duration = 0;
134   gst_adapter_clear (pay->adapter);
135   GST_DEBUG_OBJECT (pay, "reset depayloader");
136 }
137
138 static gboolean
139 gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
140 {
141   gboolean res;
142
143   gst_rtp_base_payload_set_options (payload, "audio",
144       payload->pt != GST_RTP_PAYLOAD_MPA, "MPA", 90000);
145   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
146
147   return res;
148 }
149
150 static gboolean
151 gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
152 {
153   gboolean ret;
154   GstRtpMPAPay *rtpmpapay;
155
156   rtpmpapay = GST_RTP_MPA_PAY (payload);
157
158   switch (GST_EVENT_TYPE (event)) {
159     case GST_EVENT_EOS:
160       /* make sure we push the last packets in the adapter on EOS */
161       gst_rtp_mpa_pay_flush (rtpmpapay);
162       break;
163     case GST_EVENT_FLUSH_STOP:
164       gst_rtp_mpa_pay_reset (rtpmpapay);
165       break;
166     default:
167       break;
168   }
169
170   ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
171
172   return ret;
173 }
174
175 #define RTP_HEADER_LEN 12
176
177 static GstFlowReturn
178 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
179 {
180   guint avail;
181   GstBuffer *outbuf;
182   GstFlowReturn ret;
183   guint16 frag_offset;
184   GstBufferList *list;
185
186   /* the data available in the adapter is either smaller
187    * than the MTU or bigger. In the case it is smaller, the complete
188    * adapter contents can be put in one packet. In the case the
189    * adapter has more than one MTU, we need to split the MPA data
190    * over multiple packets. The frag_offset in each packet header
191    * needs to be updated with the position in the MPA frame. */
192   avail = gst_adapter_available (rtpmpapay->adapter);
193
194   ret = GST_FLOW_OK;
195
196   list =
197       gst_buffer_list_new_sized (avail / (GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay) -
198           RTP_HEADER_LEN) + 1);
199
200   frag_offset = 0;
201   while (avail > 0) {
202     guint towrite;
203     guint8 *payload;
204     guint payload_len;
205     guint packet_len;
206     GstRTPBuffer rtp = { NULL };
207     GstBuffer *paybuf;
208
209     /* this will be the total length of the packet */
210     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
211
212     /* fill one MTU or all available bytes */
213     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
214
215     /* this is the payload length */
216     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
217
218     /* create buffer to hold the payload */
219     outbuf =
220         gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
221         (rtpmpapay), 4, 0, 0);
222
223     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
224
225     payload_len -= 4;
226
227     gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
228
229     /*
230      *  0                   1                   2                   3
231      *  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
232      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233      * |             MBZ               |          Frag_offset          |
234      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235      */
236     payload = gst_rtp_buffer_get_payload (&rtp);
237     payload[0] = 0;
238     payload[1] = 0;
239     payload[2] = frag_offset >> 8;
240     payload[3] = frag_offset & 0xff;
241
242     avail -= payload_len;
243     frag_offset += payload_len;
244
245     if (avail == 0) {
246       gst_rtp_buffer_set_marker (&rtp, TRUE);
247       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
248     }
249
250     gst_rtp_buffer_unmap (&rtp);
251
252     paybuf = gst_adapter_take_buffer_fast (rtpmpapay->adapter, payload_len);
253     gst_rtp_copy_audio_meta (rtpmpapay, outbuf, paybuf);
254     outbuf = gst_buffer_append (outbuf, paybuf);
255
256     GST_BUFFER_PTS (outbuf) = rtpmpapay->first_ts;
257     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
258     gst_buffer_list_add (list, outbuf);
259   }
260
261   ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpapay), list);
262
263   return ret;
264 }
265
266 static GstFlowReturn
267 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
268     GstBuffer * buffer)
269 {
270   GstRtpMPAPay *rtpmpapay;
271   GstFlowReturn ret;
272   guint size, avail;
273   guint packet_len;
274   GstClockTime duration, timestamp;
275
276   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
277
278   size = gst_buffer_get_size (buffer);
279   duration = GST_BUFFER_DURATION (buffer);
280   timestamp = GST_BUFFER_PTS (buffer);
281
282   if (GST_BUFFER_IS_DISCONT (buffer)) {
283     GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
284     gst_rtp_mpa_pay_reset (rtpmpapay);
285   }
286
287   avail = gst_adapter_available (rtpmpapay->adapter);
288
289   /* get packet length of previous data and this new data,
290    * payload length includes a 4 byte header */
291   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
292
293   /* if this buffer is going to overflow the packet, flush what we
294    * have. */
295   if (gst_rtp_base_payload_is_filled (basepayload,
296           packet_len, rtpmpapay->duration + duration)) {
297     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
298     avail = 0;
299   } else {
300     ret = GST_FLOW_OK;
301   }
302
303   if (avail == 0) {
304     GST_DEBUG_OBJECT (rtpmpapay,
305         "first packet, save timestamp %" GST_TIME_FORMAT,
306         GST_TIME_ARGS (timestamp));
307     rtpmpapay->first_ts = timestamp;
308     rtpmpapay->duration = 0;
309   }
310
311   gst_adapter_push (rtpmpapay->adapter, buffer);
312   rtpmpapay->duration = duration;
313
314   return ret;
315 }
316
317 static GstStateChangeReturn
318 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
319 {
320   GstRtpMPAPay *rtpmpapay;
321   GstStateChangeReturn ret;
322
323   rtpmpapay = GST_RTP_MPA_PAY (element);
324
325   switch (transition) {
326     case GST_STATE_CHANGE_READY_TO_PAUSED:
327       gst_rtp_mpa_pay_reset (rtpmpapay);
328       break;
329     default:
330       break;
331   }
332
333   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
334
335   switch (transition) {
336     case GST_STATE_CHANGE_PAUSED_TO_READY:
337       gst_rtp_mpa_pay_reset (rtpmpapay);
338       break;
339     default:
340       break;
341   }
342   return ret;
343 }