rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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         "clock-rate =  (int) [6000, 48000], "
49         "encoding-name = (string) \"SPEEX\", "
50         "encoding-params = (string) \"1\"")
51     );
52
53 static GstStaticPadTemplate gst_rtp_speex_depay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("audio/x-speex")
58     );
59
60 static GstBuffer *gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
61     GstBuffer * buf);
62 static gboolean gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload,
63     GstCaps * caps);
64
65 G_DEFINE_TYPE (GstRtpSPEEXDepay, gst_rtp_speex_depay,
66     GST_TYPE_RTP_BASE_DEPAYLOAD);
67
68 static void
69 gst_rtp_speex_depay_class_init (GstRtpSPEEXDepayClass * klass)
70 {
71   GstElementClass *gstelement_class;
72   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
73
74   gstelement_class = (GstElementClass *) klass;
75   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
76
77   gstrtpbasedepayload_class->process = gst_rtp_speex_depay_process;
78   gstrtpbasedepayload_class->set_caps = gst_rtp_speex_depay_setcaps;
79
80   gst_element_class_add_pad_template (gstelement_class,
81       gst_static_pad_template_get (&gst_rtp_speex_depay_src_template));
82   gst_element_class_add_pad_template (gstelement_class,
83       gst_static_pad_template_get (&gst_rtp_speex_depay_sink_template));
84   gst_element_class_set_static_metadata (gstelement_class,
85       "RTP Speex depayloader", "Codec/Depayloader/Network/RTP",
86       "Extracts Speex audio from RTP packets",
87       "Edgard Lima <edgard.lima@indt.org.br>");
88 }
89
90 static void
91 gst_rtp_speex_depay_init (GstRtpSPEEXDepay * rtpspeexdepay)
92 {
93 }
94
95 static gint
96 gst_rtp_speex_depay_get_mode (gint rate)
97 {
98   if (rate > 25000)
99     return 2;
100   else if (rate > 12500)
101     return 1;
102   else
103     return 0;
104 }
105
106 /* len 4 bytes LE,
107  * vendor string (len bytes),
108  * user_len 4 (0) bytes LE
109  */
110 static const gchar gst_rtp_speex_comment[] =
111     "\045\0\0\0Depayloaded with GStreamer speexdepay\0\0\0\0";
112
113 static gboolean
114 gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
115 {
116   GstStructure *structure;
117   GstRtpSPEEXDepay *rtpspeexdepay;
118   gint clock_rate, nb_channels;
119   GstBuffer *buf;
120   GstMapInfo map;
121   guint8 *data;
122   const gchar *params;
123   GstCaps *srccaps;
124   gboolean res;
125
126   rtpspeexdepay = GST_RTP_SPEEX_DEPAY (depayload);
127
128   structure = gst_caps_get_structure (caps, 0);
129
130   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
131     goto no_clockrate;
132   depayload->clock_rate = clock_rate;
133
134   if (!(params = gst_structure_get_string (structure, "encoding-params")))
135     nb_channels = 1;
136   else {
137     nb_channels = atoi (params);
138   }
139
140   /* construct minimal header and comment packet for the decoder */
141   buf = gst_buffer_new_and_alloc (80);
142   gst_buffer_map (buf, &map, GST_MAP_WRITE);
143   data = map.data;
144   memcpy (data, "Speex   ", 8);
145   data += 8;
146   memcpy (data, "1.1.12", 7);
147   data += 20;
148   GST_WRITE_UINT32_LE (data, 1);        /* version */
149   data += 4;
150   GST_WRITE_UINT32_LE (data, 80);       /* header_size */
151   data += 4;
152   GST_WRITE_UINT32_LE (data, clock_rate);       /* rate */
153   data += 4;
154   GST_WRITE_UINT32_LE (data, gst_rtp_speex_depay_get_mode (clock_rate));        /* mode */
155   data += 4;
156   GST_WRITE_UINT32_LE (data, 4);        /* mode_bitstream_version */
157   data += 4;
158   GST_WRITE_UINT32_LE (data, nb_channels);      /* nb_channels */
159   data += 4;
160   GST_WRITE_UINT32_LE (data, -1);       /* bitrate */
161   data += 4;
162   GST_WRITE_UINT32_LE (data, 0xa0);     /* frame_size */
163   data += 4;
164   GST_WRITE_UINT32_LE (data, 0);        /* VBR */
165   data += 4;
166   GST_WRITE_UINT32_LE (data, 1);        /* frames_per_packet */
167   data += 4;
168   GST_WRITE_UINT32_LE (data, 0);        /* extra_headers */
169   data += 4;
170   GST_WRITE_UINT32_LE (data, 0);        /* reserved1 */
171   data += 4;
172   GST_WRITE_UINT32_LE (data, 0);        /* reserved2 */
173   gst_buffer_unmap (buf, &map);
174
175   srccaps = gst_caps_new_empty_simple ("audio/x-speex");
176   res = gst_pad_set_caps (depayload->srcpad, srccaps);
177   gst_caps_unref (srccaps);
178
179   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
180
181   buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_speex_comment));
182   gst_buffer_fill (buf, 0, gst_rtp_speex_comment,
183       sizeof (gst_rtp_speex_comment));
184
185   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
186
187   return res;
188
189   /* ERRORS */
190 no_clockrate:
191   {
192     GST_DEBUG_OBJECT (depayload, "no clock-rate specified");
193     return FALSE;
194   }
195 }
196
197 static GstBuffer *
198 gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
199 {
200   GstBuffer *outbuf = NULL;
201   GstRTPBuffer rtp = { NULL };
202
203   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
204
205   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
206       gst_buffer_get_size (buf),
207       gst_rtp_buffer_get_marker (&rtp),
208       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
209
210   /* nothing special to be done */
211   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
212   gst_rtp_buffer_unmap (&rtp);
213
214   if (outbuf)
215     GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
216
217   return outbuf;
218 }
219
220 gboolean
221 gst_rtp_speex_depay_plugin_init (GstPlugin * plugin)
222 {
223   return gst_element_register (plugin, "rtpspeexdepay",
224       GST_RANK_SECONDARY, GST_TYPE_RTP_SPEEX_DEPAY);
225 }