Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpL16pay.c
1 /* GStreamer
2  * Copyright (C) <2007> 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/audio/audio.h>
27 #include <gst/audio/multichannel.h>
28 #include <gst/rtp/gstrtpbuffer.h>
29
30 #include "gstrtpL16pay.h"
31 #include "gstrtpchannels.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
34 #define GST_CAT_DEFAULT (rtpL16pay_debug)
35
36 /* elementfactory information */
37 static const GstElementDetails gst_rtp_L16_pay_details =
38 GST_ELEMENT_DETAILS ("RTP audio payloader",
39     "Codec/Payloader/Network",
40     "Payload-encode Raw audio into RTP packets (RFC 3551)",
41     "Wim Taymans <wim.taymans@gmail.com>");
42
43 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("audio/x-raw-int, "
48         "endianness = (int) BIG_ENDIAN, "
49         "signed = (boolean) true, "
50         "width = (int) 16, "
51         "depth = (int) 16, "
52         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
53     );
54
55 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
56     GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp, "
60         "media = (string) \"audio\", "
61         "payload = (int) [ 96, 127 ], "
62         "clock-rate = (int) [ 1, MAX ], "
63         "encoding-name = (string) \"L16\", "
64         "channels = (int) [ 1, MAX ];"
65         "application/x-rtp, "
66         "media = (string) \"audio\", "
67         "encoding-name = (string) \"L16\", "
68         "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
69         "clock-rate = (int) 44100;"
70         "application/x-rtp, "
71         "media = (string) \"audio\", "
72         "encoding-name = (string) \"L16\", "
73         "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
74         "clock-rate = (int) 44100")
75     );
76
77 static void gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass);
78 static void gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass);
79 static void gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay);
80 static void gst_rtp_L16_pay_finalize (GObject * object);
81
82 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
83     GstCaps * caps);
84 static GstFlowReturn gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * pad,
85     GstBuffer * buffer);
86 static GstCaps *gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload,
87     GstPad * pad);
88
89 static GstBaseRTPPayloadClass *parent_class = NULL;
90
91 static GType
92 gst_rtp_L16_pay_get_type (void)
93 {
94   static GType rtpL16pay_type = 0;
95
96   if (!rtpL16pay_type) {
97     static const GTypeInfo rtpL16pay_info = {
98       sizeof (GstRtpL16PayClass),
99       (GBaseInitFunc) gst_rtp_L16_pay_base_init,
100       NULL,
101       (GClassInitFunc) gst_rtp_L16_pay_class_init,
102       NULL,
103       NULL,
104       sizeof (GstRtpL16Pay),
105       0,
106       (GInstanceInitFunc) gst_rtp_L16_pay_init,
107     };
108
109     rtpL16pay_type =
110         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpL16Pay",
111         &rtpL16pay_info, 0);
112   }
113   return rtpL16pay_type;
114 }
115
116 static void
117 gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass)
118 {
119   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
120
121   gst_element_class_add_pad_template (element_class,
122       gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
123   gst_element_class_add_pad_template (element_class,
124       gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
125
126   gst_element_class_set_details (element_class, &gst_rtp_L16_pay_details);
127 }
128
129 static void
130 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
131 {
132   GObjectClass *gobject_class;
133   GstBaseRTPPayloadClass *gstbasertppayload_class;
134
135   gobject_class = (GObjectClass *) klass;
136   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
137
138   parent_class = g_type_class_peek_parent (klass);
139
140   gobject_class->finalize = gst_rtp_L16_pay_finalize;
141
142   gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
143   gstbasertppayload_class->get_caps = gst_rtp_L16_pay_getcaps;
144   gstbasertppayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
145
146   GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
147       "L16 RTP Payloader");
148 }
149
150 static void
151 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
152 {
153   rtpL16pay->adapter = gst_adapter_new ();
154 }
155
156 static void
157 gst_rtp_L16_pay_finalize (GObject * object)
158 {
159   GstRtpL16Pay *rtpL16pay;
160
161   rtpL16pay = GST_RTP_L16_PAY (object);
162
163   g_object_unref (rtpL16pay->adapter);
164   rtpL16pay->adapter = NULL;
165
166   G_OBJECT_CLASS (parent_class)->finalize (object);
167 }
168
169 static gboolean
170 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
171 {
172   GstRtpL16Pay *rtpL16pay;
173   GstStructure *structure;
174   gint channels, rate;
175   gboolean res;
176   gchar *params;
177   GstAudioChannelPosition *pos;
178   const GstRTPChannelOrder *order;
179
180   rtpL16pay = GST_RTP_L16_PAY (basepayload);
181
182   structure = gst_caps_get_structure (caps, 0);
183
184   /* first parse input caps */
185   if (!gst_structure_get_int (structure, "rate", &rate))
186     goto no_rate;
187
188   if (!gst_structure_get_int (structure, "channels", &channels))
189     goto no_channels;
190
191   /* get the channel order */
192   pos = gst_audio_get_channel_positions (structure);
193   if (pos)
194     order = gst_rtp_channels_get_by_pos (channels, pos);
195   else
196     order = NULL;
197
198   gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
199   params = g_strdup_printf ("%d", channels);
200
201   if (!order && channels > 2) {
202     GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
203         (NULL), ("Unknown channel order for %d channels", channels));
204   }
205
206   if (order && order->name) {
207     res = gst_basertppayload_set_outcaps (basepayload,
208         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
209         channels, "channel-order", G_TYPE_STRING, order->name, NULL);
210   } else {
211     res = gst_basertppayload_set_outcaps (basepayload,
212         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
213         channels, NULL);
214   }
215
216   g_free (params);
217   g_free (pos);
218
219   rtpL16pay->rate = rate;
220   rtpL16pay->channels = channels;
221
222   return res;
223
224   /* ERRORS */
225 no_rate:
226   {
227     GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
228     return FALSE;
229   }
230 no_channels:
231   {
232     GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
233     return FALSE;
234   }
235 }
236
237 static GstFlowReturn
238 gst_rtp_L16_pay_flush (GstRtpL16Pay * rtpL16pay, guint len)
239 {
240   GstBuffer *outbuf;
241   guint8 *payload;
242   GstFlowReturn ret;
243   guint samples;
244   GstClockTime duration;
245
246   /* calculate the amount of samples and round down the length */
247   samples = len / (2 * rtpL16pay->channels);
248   len = samples * (2 * rtpL16pay->channels);
249
250   /* now alloc output buffer */
251   outbuf = gst_rtp_buffer_new_allocate (len, 0, 0);
252
253   /* get payload, this is now writable */
254   payload = gst_rtp_buffer_get_payload (outbuf);
255
256   /* copy and flush data out of adapter into the RTP payload */
257   gst_adapter_copy (rtpL16pay->adapter, payload, 0, len);
258   gst_adapter_flush (rtpL16pay->adapter, len);
259
260   duration = gst_util_uint64_scale_int (samples, GST_SECOND, rtpL16pay->rate);
261
262   GST_BUFFER_TIMESTAMP (outbuf) = rtpL16pay->first_ts;
263   GST_BUFFER_DURATION (outbuf) = duration;
264
265   /* increase count (in ts) of data pushed to basertppayload */
266   if (GST_CLOCK_TIME_IS_VALID (rtpL16pay->first_ts))
267     rtpL16pay->first_ts += duration;
268
269   ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpL16pay), outbuf);
270
271   return ret;
272 }
273
274 static GstFlowReturn
275 gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * basepayload,
276     GstBuffer * buffer)
277 {
278   GstRtpL16Pay *rtpL16pay;
279   GstFlowReturn ret = GST_FLOW_OK;
280   guint payload_len;
281   GstClockTime timestamp;
282   guint mtu, avail;
283
284   rtpL16pay = GST_RTP_L16_PAY (basepayload);
285   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpL16pay);
286
287   timestamp = GST_BUFFER_TIMESTAMP (buffer);
288
289   if (GST_BUFFER_IS_DISCONT (buffer))
290     gst_adapter_clear (rtpL16pay->adapter);
291
292   avail = gst_adapter_available (rtpL16pay->adapter);
293   if (avail == 0) {
294     rtpL16pay->first_ts = timestamp;
295   }
296
297   /* push buffer in adapter */
298   gst_adapter_push (rtpL16pay->adapter, buffer);
299
300   /* get payload len for MTU */
301   payload_len = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
302
303   /* flush complete MTU while we have enough data in the adapter */
304   while (avail >= payload_len) {
305     /* flush payload_len bytes */
306     ret = gst_rtp_L16_pay_flush (rtpL16pay, payload_len);
307     if (ret != GST_FLOW_OK)
308       break;
309
310     avail = gst_adapter_available (rtpL16pay->adapter);
311   }
312   return ret;
313 }
314
315 static GstCaps *
316 gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
317 {
318   GstCaps *otherpadcaps;
319   GstCaps *caps;
320
321   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
322   caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
323
324   if (otherpadcaps) {
325     if (!gst_caps_is_empty (otherpadcaps)) {
326       GstStructure *structure;
327       gint channels;
328       gint pt;
329       gint rate;
330
331       structure = gst_caps_get_structure (otherpadcaps, 0);
332
333       if (gst_structure_get_int (structure, "channels", &channels)) {
334         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
335       } else if (gst_structure_get_int (structure, "payload", &pt)) {
336         if (pt == 10)
337           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
338         else if (pt == 11)
339           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
340       }
341
342       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
343         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
344       } else if (gst_structure_get_int (structure, "payload", &pt)) {
345         if (pt == 10 || pt == 11)
346           gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
347       }
348
349     }
350     gst_caps_unref (otherpadcaps);
351   }
352   return caps;
353 }
354
355 gboolean
356 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
357 {
358   return gst_element_register (plugin, "rtpL16pay",
359       GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);
360 }