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
gst_rtp_mpa_pay_class_init(GstRtpMPAPayClass * klass)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
gst_rtp_mpa_pay_init(GstRtpMPAPay * rtpmpapay)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
gst_rtp_mpa_pay_finalize(GObject * object)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
gst_rtp_mpa_pay_reset(GstRtpMPAPay * pay)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
gst_rtp_mpa_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)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
gst_rtp_mpa_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)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
gst_rtp_mpa_pay_flush(GstRtpMPAPay * rtpmpapay)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 = gst_rtp_buffer_new_allocate (4, 0, 0);
217 
218     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
219 
220     payload_len -= 4;
221 
222     gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
223 
224     /*
225      *  0                   1                   2                   3
226      *  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
227      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
228      * |             MBZ               |          Frag_offset          |
229      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230      */
231     payload = gst_rtp_buffer_get_payload (&rtp);
232     payload[0] = 0;
233     payload[1] = 0;
234     payload[2] = frag_offset >> 8;
235     payload[3] = frag_offset & 0xff;
236 
237     avail -= payload_len;
238     frag_offset += payload_len;
239 
240     if (avail == 0)
241       gst_rtp_buffer_set_marker (&rtp, TRUE);
242 
243     gst_rtp_buffer_unmap (&rtp);
244 
245     paybuf = gst_adapter_take_buffer_fast (rtpmpapay->adapter, payload_len);
246     gst_rtp_copy_audio_meta (rtpmpapay, outbuf, paybuf);
247     outbuf = gst_buffer_append (outbuf, paybuf);
248 
249     GST_BUFFER_PTS (outbuf) = rtpmpapay->first_ts;
250     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
251     gst_buffer_list_add (list, outbuf);
252   }
253 
254   ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpapay), list);
255 
256   return ret;
257 }
258 
259 static GstFlowReturn
gst_rtp_mpa_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)260 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
261     GstBuffer * buffer)
262 {
263   GstRtpMPAPay *rtpmpapay;
264   GstFlowReturn ret;
265   guint size, avail;
266   guint packet_len;
267   GstClockTime duration, timestamp;
268 
269   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
270 
271   size = gst_buffer_get_size (buffer);
272   duration = GST_BUFFER_DURATION (buffer);
273   timestamp = GST_BUFFER_PTS (buffer);
274 
275   if (GST_BUFFER_IS_DISCONT (buffer)) {
276     GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
277     gst_rtp_mpa_pay_reset (rtpmpapay);
278   }
279 
280   avail = gst_adapter_available (rtpmpapay->adapter);
281 
282   /* get packet length of previous data and this new data,
283    * payload length includes a 4 byte header */
284   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
285 
286   /* if this buffer is going to overflow the packet, flush what we
287    * have. */
288   if (gst_rtp_base_payload_is_filled (basepayload,
289           packet_len, rtpmpapay->duration + duration)) {
290     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
291     avail = 0;
292   } else {
293     ret = GST_FLOW_OK;
294   }
295 
296   if (avail == 0) {
297     GST_DEBUG_OBJECT (rtpmpapay,
298         "first packet, save timestamp %" GST_TIME_FORMAT,
299         GST_TIME_ARGS (timestamp));
300     rtpmpapay->first_ts = timestamp;
301     rtpmpapay->duration = 0;
302   }
303 
304   gst_adapter_push (rtpmpapay->adapter, buffer);
305   rtpmpapay->duration = duration;
306 
307   return ret;
308 }
309 
310 static GstStateChangeReturn
gst_rtp_mpa_pay_change_state(GstElement * element,GstStateChange transition)311 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
312 {
313   GstRtpMPAPay *rtpmpapay;
314   GstStateChangeReturn ret;
315 
316   rtpmpapay = GST_RTP_MPA_PAY (element);
317 
318   switch (transition) {
319     case GST_STATE_CHANGE_READY_TO_PAUSED:
320       gst_rtp_mpa_pay_reset (rtpmpapay);
321       break;
322     default:
323       break;
324   }
325 
326   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
327 
328   switch (transition) {
329     case GST_STATE_CHANGE_PAUSED_TO_READY:
330       gst_rtp_mpa_pay_reset (rtpmpapay);
331       break;
332     default:
333       break;
334   }
335   return ret;
336 }
337 
338 gboolean
gst_rtp_mpa_pay_plugin_init(GstPlugin * plugin)339 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
340 {
341   return gst_element_register (plugin, "rtpmpapay",
342       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);
343 }
344