Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 (GstBaseRTPPayload * payload,
60     GstCaps * caps);
61 static gboolean gst_rtp_mpa_pay_handle_event (GstPad * pad, GstEvent * event);
62 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
63 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * payload,
64     GstBuffer * buffer);
65
66 GST_BOILERPLATE (GstRtpMPAPay, gst_rtp_mpa_pay, GstBaseRTPPayload,
67     GST_TYPE_BASE_RTP_PAYLOAD)
68
69      static void gst_rtp_mpa_pay_base_init (gpointer klass)
70 {
71   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
72
73   gst_element_class_add_static_pad_template (element_class,
74       &gst_rtp_mpa_pay_src_template);
75   gst_element_class_add_static_pad_template (element_class,
76       &gst_rtp_mpa_pay_sink_template);
77
78   gst_element_class_set_details_simple (element_class,
79       "RTP MPEG audio payloader", "Codec/Payloader/Network/RTP",
80       "Payload MPEG audio as RTP packets (RFC 2038)",
81       "Wim Taymans <wim.taymans@gmail.com>");
82 }
83
84 static void
85 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
86 {
87   GObjectClass *gobject_class;
88   GstElementClass *gstelement_class;
89   GstBaseRTPPayloadClass *gstbasertppayload_class;
90
91   gobject_class = (GObjectClass *) klass;
92   gstelement_class = (GstElementClass *) klass;
93   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
94
95   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
96
97   gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
98
99   gstbasertppayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
100   gstbasertppayload_class->handle_event = gst_rtp_mpa_pay_handle_event;
101   gstbasertppayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
102
103   GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
104       "MPEG Audio RTP Depayloader");
105 }
106
107 static void
108 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay, GstRtpMPAPayClass * klass)
109 {
110   rtpmpapay->adapter = gst_adapter_new ();
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 (GstBaseRTPPayload * payload, GstCaps * caps)
137 {
138   gboolean res;
139
140   gst_basertppayload_set_options (payload, "audio", TRUE, "MPA", 90000);
141   res = gst_basertppayload_set_outcaps (payload, NULL);
142
143   return res;
144 }
145
146 static gboolean
147 gst_rtp_mpa_pay_handle_event (GstPad * pad, GstEvent * event)
148 {
149   GstRtpMPAPay *rtpmpapay;
150
151   rtpmpapay = GST_RTP_MPA_PAY (gst_pad_get_parent (pad));
152
153   switch (GST_EVENT_TYPE (event)) {
154     case GST_EVENT_EOS:
155       /* make sure we push the last packets in the adapter on EOS */
156       gst_rtp_mpa_pay_flush (rtpmpapay);
157       break;
158     case GST_EVENT_FLUSH_STOP:
159       gst_rtp_mpa_pay_reset (rtpmpapay);
160       break;
161     default:
162       break;
163   }
164
165   gst_object_unref (rtpmpapay);
166
167   /* FALSE to let the parent handle the event as well */
168   return FALSE;
169 }
170
171 static GstFlowReturn
172 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
173 {
174   guint avail;
175   GstBuffer *outbuf;
176   GstFlowReturn ret;
177   guint16 frag_offset;
178
179   /* the data available in the adapter is either smaller
180    * than the MTU or bigger. In the case it is smaller, the complete
181    * adapter contents can be put in one packet. In the case the
182    * adapter has more than one MTU, we need to split the MPA data
183    * over multiple packets. The frag_offset in each packet header
184    * needs to be updated with the position in the MPA frame. */
185   avail = gst_adapter_available (rtpmpapay->adapter);
186
187   ret = GST_FLOW_OK;
188
189   frag_offset = 0;
190   while (avail > 0) {
191     guint towrite;
192     guint8 *payload;
193     guint payload_len;
194     guint packet_len;
195
196     /* this will be the total length of the packet */
197     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
198
199     /* fill one MTU or all available bytes */
200     towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmpapay));
201
202     /* this is the payload length */
203     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
204
205     /* create buffer to hold the payload */
206     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
207
208     payload_len -= 4;
209
210     gst_rtp_buffer_set_payload_type (outbuf, GST_RTP_PAYLOAD_MPA);
211
212     /*
213      *  0                   1                   2                   3
214      *  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
215      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216      * |             MBZ               |          Frag_offset          |
217      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218      */
219     payload = gst_rtp_buffer_get_payload (outbuf);
220     payload[0] = 0;
221     payload[1] = 0;
222     payload[2] = frag_offset >> 8;
223     payload[3] = frag_offset & 0xff;
224
225     gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
226     gst_adapter_flush (rtpmpapay->adapter, payload_len);
227
228     avail -= payload_len;
229     frag_offset += payload_len;
230
231     if (avail == 0)
232       gst_rtp_buffer_set_marker (outbuf, TRUE);
233
234     GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
235     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
236
237     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpapay), outbuf);
238   }
239
240   return ret;
241 }
242
243 static GstFlowReturn
244 gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * basepayload,
245     GstBuffer * buffer)
246 {
247   GstRtpMPAPay *rtpmpapay;
248   GstFlowReturn ret;
249   guint size, avail;
250   guint packet_len;
251   GstClockTime duration, timestamp;
252
253   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
254
255   size = GST_BUFFER_SIZE (buffer);
256   duration = GST_BUFFER_DURATION (buffer);
257   timestamp = GST_BUFFER_TIMESTAMP (buffer);
258
259   if (GST_BUFFER_IS_DISCONT (buffer)) {
260     GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
261     gst_rtp_mpa_pay_reset (rtpmpapay);
262   }
263
264   avail = gst_adapter_available (rtpmpapay->adapter);
265
266   /* get packet length of previous data and this new data,
267    * payload length includes a 4 byte header */
268   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
269
270   /* if this buffer is going to overflow the packet, flush what we
271    * have. */
272   if (gst_basertppayload_is_filled (basepayload,
273           packet_len, rtpmpapay->duration + duration)) {
274     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
275     avail = 0;
276   } else {
277     ret = GST_FLOW_OK;
278   }
279
280   if (avail == 0) {
281     GST_DEBUG_OBJECT (rtpmpapay,
282         "first packet, save timestamp %" GST_TIME_FORMAT,
283         GST_TIME_ARGS (timestamp));
284     rtpmpapay->first_ts = timestamp;
285     rtpmpapay->duration = 0;
286   }
287
288   gst_adapter_push (rtpmpapay->adapter, buffer);
289   rtpmpapay->duration = duration;
290
291   return ret;
292 }
293
294 static GstStateChangeReturn
295 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
296 {
297   GstRtpMPAPay *rtpmpapay;
298   GstStateChangeReturn ret;
299
300   rtpmpapay = GST_RTP_MPA_PAY (element);
301
302   switch (transition) {
303     case GST_STATE_CHANGE_READY_TO_PAUSED:
304       gst_rtp_mpa_pay_reset (rtpmpapay);
305       break;
306     default:
307       break;
308   }
309
310   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
311
312   switch (transition) {
313     case GST_STATE_CHANGE_PAUSED_TO_READY:
314       gst_rtp_mpa_pay_reset (rtpmpapay);
315       break;
316     default:
317       break;
318   }
319   return ret;
320 }
321
322 gboolean
323 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
324 {
325   return gst_element_register (plugin, "rtpmpapay",
326       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);
327 }