Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpspeexdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
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 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpspeexdepay.h"
29
30 /* elementfactory information */
31 static const GstElementDetails gst_rtp_speexdepay_details =
32 GST_ELEMENT_DETAILS ("RTP Speex depayloader",
33     "Codec/Depayloader/Network",
34     "Extracts Speex audio from RTP packets",
35     "Edgard Lima <edgard.lima@indt.org.br>");
36
37 /* RtpSPEEXDepay signals and args */
38 enum
39 {
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum
45 {
46   ARG_0
47 };
48
49 static GstStaticPadTemplate gst_rtp_speex_depay_sink_template =
50 GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("application/x-rtp, "
54         "media = (string) \"audio\", "
55         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56         "clock-rate =  (int) [6000, 48000], "
57         "encoding-name = (string) \"SPEEX\", "
58         "encoding-params = (string) \"1\"")
59     );
60
61 static GstStaticPadTemplate gst_rtp_speex_depay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("audio/x-speex")
66     );
67
68 static GstBuffer *gst_rtp_speex_depay_process (GstBaseRTPDepayload * depayload,
69     GstBuffer * buf);
70 static gboolean gst_rtp_speex_depay_setcaps (GstBaseRTPDepayload * depayload,
71     GstCaps * caps);
72
73 GST_BOILERPLATE (GstRtpSPEEXDepay, gst_rtp_speex_depay, GstBaseRTPDepayload,
74     GST_TYPE_BASE_RTP_DEPAYLOAD);
75
76 static void
77 gst_rtp_speex_depay_base_init (gpointer klass)
78 {
79   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
80
81   gst_element_class_add_pad_template (element_class,
82       gst_static_pad_template_get (&gst_rtp_speex_depay_src_template));
83   gst_element_class_add_pad_template (element_class,
84       gst_static_pad_template_get (&gst_rtp_speex_depay_sink_template));
85   gst_element_class_set_details (element_class, &gst_rtp_speexdepay_details);
86 }
87
88 static void
89 gst_rtp_speex_depay_class_init (GstRtpSPEEXDepayClass * klass)
90 {
91   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
92
93   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
94
95   gstbasertpdepayload_class->process = gst_rtp_speex_depay_process;
96   gstbasertpdepayload_class->set_caps = gst_rtp_speex_depay_setcaps;
97 }
98
99 static void
100 gst_rtp_speex_depay_init (GstRtpSPEEXDepay * rtpspeexdepay,
101     GstRtpSPEEXDepayClass * klass)
102 {
103 }
104
105 static gint
106 gst_rtp_speex_depay_get_mode (gint rate)
107 {
108   if (rate > 25000)
109     return 2;
110   else if (rate > 12500)
111     return 1;
112   else
113     return 0;
114 }
115
116 /* len 4 bytes LE,
117  * vendor string (len bytes),
118  * user_len 4 (0) bytes LE
119  */
120 static const gchar gst_rtp_speex_comment[] =
121     "\045\0\0\0Depayloaded with GStreamer speexdepay\0\0\0\0";
122
123 static gboolean
124 gst_rtp_speex_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
125 {
126   GstStructure *structure;
127   GstRtpSPEEXDepay *rtpspeexdepay;
128   gint clock_rate, nb_channels;
129   GstBuffer *buf;
130   guint8 *data;
131   const gchar *params;
132   GstCaps *srccaps;
133   gboolean res;
134
135   rtpspeexdepay = GST_RTP_SPEEX_DEPAY (depayload);
136
137   structure = gst_caps_get_structure (caps, 0);
138
139   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
140     goto no_clockrate;
141   depayload->clock_rate = clock_rate;
142
143   if (!(params = gst_structure_get_string (structure, "encoding-params")))
144     nb_channels = 1;
145   else {
146     nb_channels = atoi (params);
147   }
148
149   /* construct minimal header and comment packet for the decoder */
150   buf = gst_buffer_new_and_alloc (80);
151   data = GST_BUFFER_DATA (buf);
152   memcpy (data, "Speex   ", 8);
153   data += 8;
154   memcpy (data, "1.1.12", 7);
155   data += 20;
156   GST_WRITE_UINT32_LE (data, 1);        /* version */
157   data += 4;
158   GST_WRITE_UINT32_LE (data, 80);       /* header_size */
159   data += 4;
160   GST_WRITE_UINT32_LE (data, clock_rate);       /* rate */
161   data += 4;
162   GST_WRITE_UINT32_LE (data, gst_rtp_speex_depay_get_mode (clock_rate));        /* mode */
163   data += 4;
164   GST_WRITE_UINT32_LE (data, 4);        /* mode_bitstream_version */
165   data += 4;
166   GST_WRITE_UINT32_LE (data, nb_channels);      /* nb_channels */
167   data += 4;
168   GST_WRITE_UINT32_LE (data, -1);       /* bitrate */
169   data += 4;
170   GST_WRITE_UINT32_LE (data, 0xa0);     /* frame_size */
171   data += 4;
172   GST_WRITE_UINT32_LE (data, 0);        /* VBR */
173   data += 4;
174   GST_WRITE_UINT32_LE (data, 1);        /* frames_per_packet */
175   data += 4;
176   GST_WRITE_UINT32_LE (data, 0);        /* extra_headers */
177   data += 4;
178   GST_WRITE_UINT32_LE (data, 0);        /* reserved1 */
179   data += 4;
180   GST_WRITE_UINT32_LE (data, 0);        /* reserved2 */
181
182   srccaps = gst_caps_new_simple ("audio/x-speex", NULL);
183   res = gst_pad_set_caps (depayload->srcpad, srccaps);
184   gst_caps_unref (srccaps);
185
186   gst_buffer_set_caps (buf, GST_PAD_CAPS (depayload->srcpad));
187   gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpspeexdepay), buf);
188
189   buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_speex_comment));
190   memcpy (GST_BUFFER_DATA (buf), gst_rtp_speex_comment,
191       sizeof (gst_rtp_speex_comment));
192
193   gst_buffer_set_caps (buf, GST_PAD_CAPS (depayload->srcpad));
194   gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpspeexdepay), buf);
195
196   return res;
197
198   /* ERRORS */
199 no_clockrate:
200   {
201     GST_DEBUG_OBJECT (depayload, "no clock-rate specified");
202     return FALSE;
203   }
204 }
205
206 static GstBuffer *
207 gst_rtp_speex_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
208 {
209   GstBuffer *outbuf = NULL;
210
211   GST_DEBUG ("process : got %d bytes, mark %d ts %u seqn %d",
212       GST_BUFFER_SIZE (buf),
213       gst_rtp_buffer_get_marker (buf),
214       gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
215
216   /* nothing special to be done */
217   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
218
219   GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
220
221   return outbuf;
222 }
223
224 gboolean
225 gst_rtp_speex_depay_plugin_init (GstPlugin * plugin)
226 {
227   return gst_element_register (plugin, "rtpspeexdepay",
228       GST_RANK_MARGINAL, GST_TYPE_RTP_SPEEX_DEPAY);
229 }