rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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
28 #include "gstrtpmpapay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmpapay_debug);
31 #define GST_CAT_DEFAULT (rtpmpapay_debug)
32
33 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
38     );
39
40 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
41     GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("application/x-rtp, "
45         "media = (string) \"audio\", "
46         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
47         "clock-rate = (int) 90000; "
48         "application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
52     );
53
54 static void gst_rtp_mpa_pay_finalize (GObject * object);
55
56 static GstStateChangeReturn gst_rtp_mpa_pay_change_state (GstElement * element,
57     GstStateChange transition);
58
59 static gboolean gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload,
60     GstCaps * caps);
61 static gboolean gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload,
62     GstEvent * event);
63 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
64 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * payload,
65     GstBuffer * buffer);
66
67 #define gst_rtp_mpa_pay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpMPAPay, gst_rtp_mpa_pay, GST_TYPE_RTP_BASE_PAYLOAD);
69
70 static void
71 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
72 {
73   GObjectClass *gobject_class;
74   GstElementClass *gstelement_class;
75   GstRTPBasePayloadClass *gstrtpbasepayload_class;
76
77   GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
78       "MPEG Audio RTP Depayloader");
79
80   gobject_class = (GObjectClass *) klass;
81   gstelement_class = (GstElementClass *) klass;
82   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
83
84   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
85
86   gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
87
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));
92
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>");
97
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;
101 }
102
103 static void
104 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
105 {
106   rtpmpapay->adapter = gst_adapter_new ();
107 }
108
109 static void
110 gst_rtp_mpa_pay_finalize (GObject * object)
111 {
112   GstRtpMPAPay *rtpmpapay;
113
114   rtpmpapay = GST_RTP_MPA_PAY (object);
115
116   g_object_unref (rtpmpapay->adapter);
117   rtpmpapay->adapter = NULL;
118
119   G_OBJECT_CLASS (parent_class)->finalize (object);
120 }
121
122 static void
123 gst_rtp_mpa_pay_reset (GstRtpMPAPay * pay)
124 {
125   pay->first_ts = -1;
126   pay->duration = 0;
127   gst_adapter_clear (pay->adapter);
128   GST_DEBUG_OBJECT (pay, "reset depayloader");
129 }
130
131 static gboolean
132 gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
133 {
134   gboolean res;
135
136   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "MPA", 90000);
137   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
138
139   return res;
140 }
141
142 static gboolean
143 gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
144 {
145   gboolean ret;
146   GstRtpMPAPay *rtpmpapay;
147
148   rtpmpapay = GST_RTP_MPA_PAY (payload);
149
150   switch (GST_EVENT_TYPE (event)) {
151     case GST_EVENT_EOS:
152       /* make sure we push the last packets in the adapter on EOS */
153       gst_rtp_mpa_pay_flush (rtpmpapay);
154       break;
155     case GST_EVENT_FLUSH_STOP:
156       gst_rtp_mpa_pay_reset (rtpmpapay);
157       break;
158     default:
159       break;
160   }
161
162   ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
163
164   return ret;
165 }
166
167 static GstFlowReturn
168 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
169 {
170   guint avail;
171   GstBuffer *outbuf;
172   GstFlowReturn ret;
173   guint16 frag_offset;
174
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);
182
183   ret = GST_FLOW_OK;
184
185   frag_offset = 0;
186   while (avail > 0) {
187     guint towrite;
188     guint8 *payload;
189     guint payload_len;
190     guint packet_len;
191     GstRTPBuffer rtp = { NULL };
192
193     /* this will be the total length of the packet */
194     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
195
196     /* fill one MTU or all available bytes */
197     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
198
199     /* this is the payload length */
200     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
201
202     /* create buffer to hold the payload */
203     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
204
205     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
206
207     payload_len -= 4;
208
209     gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
210
211     /*
212      *  0                   1                   2                   3
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      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217      */
218     payload = gst_rtp_buffer_get_payload (&rtp);
219     payload[0] = 0;
220     payload[1] = 0;
221     payload[2] = frag_offset >> 8;
222     payload[3] = frag_offset & 0xff;
223
224     gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
225     gst_adapter_flush (rtpmpapay->adapter, payload_len);
226
227     avail -= payload_len;
228     frag_offset += payload_len;
229
230     if (avail == 0)
231       gst_rtp_buffer_set_marker (&rtp, TRUE);
232
233     gst_rtp_buffer_unmap (&rtp);
234
235     GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
236     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
237
238     ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmpapay), outbuf);
239   }
240
241   return ret;
242 }
243
244 static GstFlowReturn
245 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
246     GstBuffer * buffer)
247 {
248   GstRtpMPAPay *rtpmpapay;
249   GstFlowReturn ret;
250   guint size, avail;
251   guint packet_len;
252   GstClockTime duration, timestamp;
253
254   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
255
256   size = gst_buffer_get_size (buffer);
257   duration = GST_BUFFER_DURATION (buffer);
258   timestamp = GST_BUFFER_TIMESTAMP (buffer);
259
260   if (GST_BUFFER_IS_DISCONT (buffer)) {
261     GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
262     gst_rtp_mpa_pay_reset (rtpmpapay);
263   }
264
265   avail = gst_adapter_available (rtpmpapay->adapter);
266
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);
270
271   /* if this buffer is going to overflow the packet, flush what we
272    * have. */
273   if (gst_rtp_base_payload_is_filled (basepayload,
274           packet_len, rtpmpapay->duration + duration)) {
275     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
276     avail = 0;
277   } else {
278     ret = GST_FLOW_OK;
279   }
280
281   if (avail == 0) {
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;
287   }
288
289   gst_adapter_push (rtpmpapay->adapter, buffer);
290   rtpmpapay->duration = duration;
291
292   return ret;
293 }
294
295 static GstStateChangeReturn
296 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
297 {
298   GstRtpMPAPay *rtpmpapay;
299   GstStateChangeReturn ret;
300
301   rtpmpapay = GST_RTP_MPA_PAY (element);
302
303   switch (transition) {
304     case GST_STATE_CHANGE_READY_TO_PAUSED:
305       gst_rtp_mpa_pay_reset (rtpmpapay);
306       break;
307     default:
308       break;
309   }
310
311   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
312
313   switch (transition) {
314     case GST_STATE_CHANGE_PAUSED_TO_READY:
315       gst_rtp_mpa_pay_reset (rtpmpapay);
316       break;
317     default:
318       break;
319   }
320   return ret;
321 }
322
323 gboolean
324 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
325 {
326   return gst_element_register (plugin, "rtpmpapay",
327       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);
328 }