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