gst/rtp/: Fix the descriptions and fix some email addresses.
[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 /* elementfactory information */
31 static const GstElementDetails gst_rtp_mpapay_details =
32 GST_ELEMENT_DETAILS ("RTP MPEG audio payloader",
33     "Codec/Payloader/Network",
34     "Payload MPEG audio as RTP packets (RFC 2038)",
35     "Wim Taymans <wim.taymans@gmail.com>");
36
37 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
39     GST_PAD_SINK,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
42     );
43
44 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
45     GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
51         "clock-rate = (int) 90000; "
52         "application/x-rtp, "
53         "media = (string) \"audio\", "
54         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
56     );
57
58 static void gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass);
59 static void gst_rtp_mpa_pay_base_init (GstRtpMPAPayClass * klass);
60 static void gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay);
61 static void gst_rtp_mpa_pay_finalize (GObject * object);
62
63 static gboolean gst_rtp_mpa_pay_setcaps (GstBaseRTPPayload * payload,
64     GstCaps * caps);
65 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * payload,
66     GstBuffer * buffer);
67
68 static GstBaseRTPPayloadClass *parent_class = NULL;
69
70 static GType
71 gst_rtp_mpa_pay_get_type (void)
72 {
73   static GType rtpmpapay_type = 0;
74
75   if (!rtpmpapay_type) {
76     static const GTypeInfo rtpmpapay_info = {
77       sizeof (GstRtpMPAPayClass),
78       (GBaseInitFunc) gst_rtp_mpa_pay_base_init,
79       NULL,
80       (GClassInitFunc) gst_rtp_mpa_pay_class_init,
81       NULL,
82       NULL,
83       sizeof (GstRtpMPAPay),
84       0,
85       (GInstanceInitFunc) gst_rtp_mpa_pay_init,
86     };
87
88     rtpmpapay_type =
89         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpMPAPay",
90         &rtpmpapay_info, 0);
91   }
92   return rtpmpapay_type;
93 }
94
95 static void
96 gst_rtp_mpa_pay_base_init (GstRtpMPAPayClass * klass)
97 {
98   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
99
100   gst_element_class_add_pad_template (element_class,
101       gst_static_pad_template_get (&gst_rtp_mpa_pay_src_template));
102   gst_element_class_add_pad_template (element_class,
103       gst_static_pad_template_get (&gst_rtp_mpa_pay_sink_template));
104
105   gst_element_class_set_details (element_class, &gst_rtp_mpapay_details);
106 }
107
108 static void
109 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
110 {
111   GObjectClass *gobject_class;
112   GstElementClass *gstelement_class;
113   GstBaseRTPPayloadClass *gstbasertppayload_class;
114
115   gobject_class = (GObjectClass *) klass;
116   gstelement_class = (GstElementClass *) klass;
117   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
118
119   parent_class = g_type_class_peek_parent (klass);
120
121   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
122
123   gstbasertppayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
124   gstbasertppayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
125 }
126
127 static void
128 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
129 {
130   rtpmpapay->adapter = gst_adapter_new ();
131 }
132
133 static void
134 gst_rtp_mpa_pay_finalize (GObject * object)
135 {
136   GstRtpMPAPay *rtpmpapay;
137
138   rtpmpapay = GST_RTP_MPA_PAY (object);
139
140   g_object_unref (rtpmpapay->adapter);
141   rtpmpapay->adapter = NULL;
142
143   G_OBJECT_CLASS (parent_class)->finalize (object);
144 }
145
146 static gboolean
147 gst_rtp_mpa_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
148 {
149   gboolean res;
150
151   gst_basertppayload_set_options (payload, "audio", TRUE, "MPA", 90000);
152   res = gst_basertppayload_set_outcaps (payload, NULL);
153
154   return res;
155 }
156
157 static GstFlowReturn
158 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
159 {
160   guint avail;
161   GstBuffer *outbuf;
162   GstFlowReturn ret;
163   guint16 frag_offset;
164
165   /* the data available in the adapter is either smaller
166    * than the MTU or bigger. In the case it is smaller, the complete
167    * adapter contents can be put in one packet. In the case the
168    * adapter has more than one MTU, we need to split the MPA data
169    * over multiple packets. The frag_offset in each packet header
170    * needs to be updated with the position in the MPA frame. */
171   avail = gst_adapter_available (rtpmpapay->adapter);
172
173   ret = GST_FLOW_OK;
174
175   frag_offset = 0;
176   while (avail > 0) {
177     guint towrite;
178     guint8 *payload;
179     guint payload_len;
180     guint packet_len;
181
182     /* this will be the total lenght of the packet */
183     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
184
185     /* fill one MTU or all available bytes */
186     towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmpapay));
187
188     /* this is the payload length */
189     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
190
191     /* create buffer to hold the payload */
192     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
193
194     payload_len -= 4;
195
196     gst_rtp_buffer_set_payload_type (outbuf, GST_RTP_PAYLOAD_MPA);
197
198     /*
199      *  0                   1                   2                   3
200      *  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
201      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202      * |             MBZ               |          Frag_offset          |
203      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
204      */
205     payload = gst_rtp_buffer_get_payload (outbuf);
206     payload[0] = 0;
207     payload[1] = 0;
208     payload[2] = frag_offset >> 8;
209     payload[3] = frag_offset & 0xff;
210
211     gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
212     gst_adapter_flush (rtpmpapay->adapter, payload_len);
213
214     avail -= payload_len;
215     frag_offset += payload_len;
216
217     if (avail == 0)
218       gst_rtp_buffer_set_marker (outbuf, TRUE);
219
220     GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
221     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
222
223     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpapay), outbuf);
224   }
225
226   return ret;
227 }
228
229 static GstFlowReturn
230 gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * basepayload,
231     GstBuffer * buffer)
232 {
233   GstRtpMPAPay *rtpmpapay;
234   GstFlowReturn ret;
235   guint size, avail;
236   guint packet_len;
237   GstClockTime duration;
238
239   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
240
241   size = GST_BUFFER_SIZE (buffer);
242   duration = GST_BUFFER_DURATION (buffer);
243
244   avail = gst_adapter_available (rtpmpapay->adapter);
245   if (avail == 0) {
246     rtpmpapay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
247     rtpmpapay->duration = 0;
248   }
249
250   /* get packet length of previous data and this new data, 
251    * payload length includes a 4 byte header */
252   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
253
254   /* if this buffer is going to overflow the packet, flush what we
255    * have. */
256   if (gst_basertppayload_is_filled (basepayload,
257           packet_len, rtpmpapay->duration + duration)) {
258     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
259     rtpmpapay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
260     rtpmpapay->duration = 0;
261   } else {
262     ret = GST_FLOW_OK;
263   }
264
265   gst_adapter_push (rtpmpapay->adapter, buffer);
266   rtpmpapay->duration += duration;
267
268   return ret;
269 }
270
271 gboolean
272 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
273 {
274   return gst_element_register (plugin, "rtpmpapay",
275       GST_RANK_NONE, GST_TYPE_RTP_MPA_PAY);
276 }