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