Remove unused variables in _class_init
[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   GstBaseRTPPayloadClass *gstbasertppayload_class;
113
114   gobject_class = (GObjectClass *) klass;
115   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
116
117   parent_class = g_type_class_peek_parent (klass);
118
119   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
120
121   gstbasertppayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
122   gstbasertppayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
123 }
124
125 static void
126 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
127 {
128   rtpmpapay->adapter = gst_adapter_new ();
129 }
130
131 static void
132 gst_rtp_mpa_pay_finalize (GObject * object)
133 {
134   GstRtpMPAPay *rtpmpapay;
135
136   rtpmpapay = GST_RTP_MPA_PAY (object);
137
138   g_object_unref (rtpmpapay->adapter);
139   rtpmpapay->adapter = NULL;
140
141   G_OBJECT_CLASS (parent_class)->finalize (object);
142 }
143
144 static gboolean
145 gst_rtp_mpa_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
146 {
147   gboolean res;
148
149   gst_basertppayload_set_options (payload, "audio", TRUE, "MPA", 90000);
150   res = gst_basertppayload_set_outcaps (payload, NULL);
151
152   return res;
153 }
154
155 static GstFlowReturn
156 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
157 {
158   guint avail;
159   GstBuffer *outbuf;
160   GstFlowReturn ret;
161   guint16 frag_offset;
162
163   /* the data available in the adapter is either smaller
164    * than the MTU or bigger. In the case it is smaller, the complete
165    * adapter contents can be put in one packet. In the case the
166    * adapter has more than one MTU, we need to split the MPA data
167    * over multiple packets. The frag_offset in each packet header
168    * needs to be updated with the position in the MPA frame. */
169   avail = gst_adapter_available (rtpmpapay->adapter);
170
171   ret = GST_FLOW_OK;
172
173   frag_offset = 0;
174   while (avail > 0) {
175     guint towrite;
176     guint8 *payload;
177     guint payload_len;
178     guint packet_len;
179
180     /* this will be the total lenght of the packet */
181     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
182
183     /* fill one MTU or all available bytes */
184     towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmpapay));
185
186     /* this is the payload length */
187     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
188
189     /* create buffer to hold the payload */
190     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
191
192     payload_len -= 4;
193
194     gst_rtp_buffer_set_payload_type (outbuf, GST_RTP_PAYLOAD_MPA);
195
196     /*
197      *  0                   1                   2                   3
198      *  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
199      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200      * |             MBZ               |          Frag_offset          |
201      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
202      */
203     payload = gst_rtp_buffer_get_payload (outbuf);
204     payload[0] = 0;
205     payload[1] = 0;
206     payload[2] = frag_offset >> 8;
207     payload[3] = frag_offset & 0xff;
208
209     gst_adapter_copy (rtpmpapay->adapter, &payload[4], 0, payload_len);
210     gst_adapter_flush (rtpmpapay->adapter, payload_len);
211
212     avail -= payload_len;
213     frag_offset += payload_len;
214
215     if (avail == 0)
216       gst_rtp_buffer_set_marker (outbuf, TRUE);
217
218     GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
219     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
220
221     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpapay), outbuf);
222   }
223
224   return ret;
225 }
226
227 static GstFlowReturn
228 gst_rtp_mpa_pay_handle_buffer (GstBaseRTPPayload * basepayload,
229     GstBuffer * buffer)
230 {
231   GstRtpMPAPay *rtpmpapay;
232   GstFlowReturn ret;
233   guint size, avail;
234   guint packet_len;
235   GstClockTime duration;
236
237   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
238
239   size = GST_BUFFER_SIZE (buffer);
240   duration = GST_BUFFER_DURATION (buffer);
241
242   avail = gst_adapter_available (rtpmpapay->adapter);
243   if (avail == 0) {
244     rtpmpapay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
245     rtpmpapay->duration = 0;
246   }
247
248   /* get packet length of previous data and this new data, 
249    * payload length includes a 4 byte header */
250   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
251
252   /* if this buffer is going to overflow the packet, flush what we
253    * have. */
254   if (gst_basertppayload_is_filled (basepayload,
255           packet_len, rtpmpapay->duration + duration)) {
256     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
257     rtpmpapay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
258     rtpmpapay->duration = 0;
259   } else {
260     ret = GST_FLOW_OK;
261   }
262
263   gst_adapter_push (rtpmpapay->adapter, buffer);
264   rtpmpapay->duration += duration;
265
266   return ret;
267 }
268
269 gboolean
270 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
271 {
272   return gst_element_register (plugin, "rtpmpapay",
273       GST_RANK_NONE, GST_TYPE_RTP_MPA_PAY);
274 }