tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpspeexpay.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 <stdlib.h>
25 #include <string.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpspeexpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpspeexpay_debug);
31 #define GST_CAT_DEFAULT (rtpspeexpay_debug)
32
33 static GstStaticPadTemplate gst_rtp_speex_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("audio/x-speex, "
38         "rate = (int) [ 6000, 48000 ], " "channels = (int) 1")
39     );
40
41 static GstStaticPadTemplate gst_rtp_speex_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"audio\", "
47         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
48         "clock-rate =  (int) [ 6000, 48000 ], "
49         "encoding-name = (string) \"SPEEX\", "
50         "encoding-params = (string) \"1\"")
51     );
52
53 static GstStateChangeReturn gst_rtp_speex_pay_change_state (GstElement *
54     element, GstStateChange transition);
55
56 static gboolean gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload,
57     GstCaps * caps);
58 static GstCaps *gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload,
59     GstPad * pad);
60 static GstFlowReturn gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload *
61     payload, GstBuffer * buffer);
62
63 GST_BOILERPLATE (GstRtpSPEEXPay, gst_rtp_speex_pay, GstBaseRTPPayload,
64     GST_TYPE_BASE_RTP_PAYLOAD);
65
66 static void
67 gst_rtp_speex_pay_base_init (gpointer klass)
68 {
69   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
70
71   gst_element_class_add_static_pad_template (element_class,
72       &gst_rtp_speex_pay_sink_template);
73   gst_element_class_add_static_pad_template (element_class,
74       &gst_rtp_speex_pay_src_template);
75   gst_element_class_set_details_simple (element_class, "RTP Speex payloader",
76       "Codec/Payloader/Network/RTP",
77       "Payload-encodes Speex audio into a RTP packet",
78       "Edgard Lima <edgard.lima@indt.org.br>");
79
80   GST_DEBUG_CATEGORY_INIT (rtpspeexpay_debug, "rtpspeexpay", 0,
81       "Speex RTP Payloader");
82 }
83
84 static void
85 gst_rtp_speex_pay_class_init (GstRtpSPEEXPayClass * klass)
86 {
87   GstElementClass *gstelement_class;
88   GstBaseRTPPayloadClass *gstbasertppayload_class;
89
90   gstelement_class = (GstElementClass *) klass;
91   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
92
93   gstelement_class->change_state = gst_rtp_speex_pay_change_state;
94
95   gstbasertppayload_class->set_caps = gst_rtp_speex_pay_setcaps;
96   gstbasertppayload_class->get_caps = gst_rtp_speex_pay_getcaps;
97   gstbasertppayload_class->handle_buffer = gst_rtp_speex_pay_handle_buffer;
98 }
99
100 static void
101 gst_rtp_speex_pay_init (GstRtpSPEEXPay * rtpspeexpay,
102     GstRtpSPEEXPayClass * klass)
103 {
104   GST_BASE_RTP_PAYLOAD (rtpspeexpay)->clock_rate = 8000;
105   GST_BASE_RTP_PAYLOAD_PT (rtpspeexpay) = 110;  /* Create String */
106 }
107
108 static gboolean
109 gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
110 {
111   /* don't configure yet, we wait for the ident packet */
112   return TRUE;
113 }
114
115
116 static GstCaps *
117 gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload, GstPad * pad)
118 {
119   GstCaps *otherpadcaps;
120   GstCaps *caps;
121
122   otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
123   caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
124
125   if (otherpadcaps) {
126     if (!gst_caps_is_empty (otherpadcaps)) {
127       GstStructure *ps = gst_caps_get_structure (otherpadcaps, 0);
128       GstStructure *s = gst_caps_get_structure (caps, 0);
129       gint clock_rate;
130
131       if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
132         gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
133       }
134     }
135     gst_caps_unref (otherpadcaps);
136   }
137
138   return caps;
139 }
140
141 static gboolean
142 gst_rtp_speex_pay_parse_ident (GstRtpSPEEXPay * rtpspeexpay,
143     const guint8 * data, guint size)
144 {
145   guint32 version, header_size, rate, mode, nb_channels;
146   GstBaseRTPPayload *payload;
147   gchar *cstr;
148   gboolean res;
149
150   /* we need the header string (8), the version string (20), the version
151    * and the header length. */
152   if (size < 36)
153     goto too_small;
154
155   if (!g_str_has_prefix ((const gchar *) data, "Speex   "))
156     goto wrong_header;
157
158   /* skip header and version string */
159   data += 28;
160
161   version = GST_READ_UINT32_LE (data);
162   if (version != 1)
163     goto wrong_version;
164
165   data += 4;
166   /* ensure sizes */
167   header_size = GST_READ_UINT32_LE (data);
168   if (header_size < 80)
169     goto header_too_small;
170
171   if (size < header_size)
172     goto payload_too_small;
173
174   data += 4;
175   rate = GST_READ_UINT32_LE (data);
176   data += 4;
177   mode = GST_READ_UINT32_LE (data);
178   data += 8;
179   nb_channels = GST_READ_UINT32_LE (data);
180
181   GST_DEBUG_OBJECT (rtpspeexpay, "rate %d, mode %d, nb_channels %d",
182       rate, mode, nb_channels);
183
184   payload = GST_BASE_RTP_PAYLOAD (rtpspeexpay);
185
186   gst_basertppayload_set_options (payload, "audio", FALSE, "SPEEX", rate);
187   cstr = g_strdup_printf ("%d", nb_channels);
188   res = gst_basertppayload_set_outcaps (payload, "encoding-params",
189       G_TYPE_STRING, cstr, NULL);
190   g_free (cstr);
191
192   return res;
193
194   /* ERRORS */
195 too_small:
196   {
197     GST_DEBUG_OBJECT (rtpspeexpay,
198         "ident packet too small, need at least 32 bytes");
199     return FALSE;
200   }
201 wrong_header:
202   {
203     GST_DEBUG_OBJECT (rtpspeexpay,
204         "ident packet does not start with \"Speex   \"");
205     return FALSE;
206   }
207 wrong_version:
208   {
209     GST_DEBUG_OBJECT (rtpspeexpay, "can only handle version 1, have version %d",
210         version);
211     return FALSE;
212   }
213 header_too_small:
214   {
215     GST_DEBUG_OBJECT (rtpspeexpay,
216         "header size too small, need at least 80 bytes, " "got only %d",
217         header_size);
218     return FALSE;
219   }
220 payload_too_small:
221   {
222     GST_DEBUG_OBJECT (rtpspeexpay,
223         "payload too small, need at least %d bytes, got only %d", header_size,
224         size);
225     return FALSE;
226   }
227 }
228
229 static GstFlowReturn
230 gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload * basepayload,
231     GstBuffer * buffer)
232 {
233   GstRtpSPEEXPay *rtpspeexpay;
234   guint size, payload_len;
235   GstBuffer *outbuf;
236   guint8 *payload, *data;
237   GstClockTime timestamp, duration;
238   GstFlowReturn ret;
239
240   rtpspeexpay = GST_RTP_SPEEX_PAY (basepayload);
241
242   size = GST_BUFFER_SIZE (buffer);
243   data = GST_BUFFER_DATA (buffer);
244
245   switch (rtpspeexpay->packet) {
246     case 0:
247       /* ident packet. We need to parse the headers to construct the RTP
248        * properties. */
249       if (!gst_rtp_speex_pay_parse_ident (rtpspeexpay, data, size))
250         goto parse_error;
251
252       ret = GST_FLOW_OK;
253       goto done;
254     case 1:
255       /* comment packet, we ignore it */
256       ret = GST_FLOW_OK;
257       goto done;
258     default:
259       /* other packets go in the payload */
260       break;
261   }
262
263   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP)) {
264     ret = GST_FLOW_OK;
265     goto done;
266   }
267
268   timestamp = GST_BUFFER_TIMESTAMP (buffer);
269   duration = GST_BUFFER_DURATION (buffer);
270
271   /* FIXME, only one SPEEX frame per RTP packet for now */
272   payload_len = size;
273
274   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
275   /* FIXME, assert for now */
276   g_assert (payload_len <= GST_BASE_RTP_PAYLOAD_MTU (rtpspeexpay));
277
278   /* copy timestamp and duration */
279   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
280   GST_BUFFER_DURATION (outbuf) = duration;
281
282   /* get payload */
283   payload = gst_rtp_buffer_get_payload (outbuf);
284
285   /* copy data in payload */
286   memcpy (&payload[0], data, size);
287
288   ret = gst_basertppayload_push (basepayload, outbuf);
289
290 done:
291   gst_buffer_unref (buffer);
292
293   rtpspeexpay->packet++;
294
295   return ret;
296
297   /* ERRORS */
298 parse_error:
299   {
300     GST_ELEMENT_ERROR (rtpspeexpay, STREAM, DECODE, (NULL),
301         ("Error parsing first identification packet."));
302     gst_buffer_unref (buffer);
303     return GST_FLOW_ERROR;
304   }
305 }
306
307 static GstStateChangeReturn
308 gst_rtp_speex_pay_change_state (GstElement * element, GstStateChange transition)
309 {
310   GstRtpSPEEXPay *rtpspeexpay;
311   GstStateChangeReturn ret;
312
313   rtpspeexpay = GST_RTP_SPEEX_PAY (element);
314
315   switch (transition) {
316     case GST_STATE_CHANGE_NULL_TO_READY:
317       break;
318     case GST_STATE_CHANGE_READY_TO_PAUSED:
319       rtpspeexpay->packet = 0;
320       break;
321     default:
322       break;
323   }
324
325   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
326
327   switch (transition) {
328     case GST_STATE_CHANGE_READY_TO_NULL:
329       break;
330     default:
331       break;
332   }
333   return ret;
334 }
335
336 gboolean
337 gst_rtp_speex_pay_plugin_init (GstPlugin * plugin)
338 {
339   return gst_element_register (plugin, "rtpspeexpay",
340       GST_RANK_SECONDARY, GST_TYPE_RTP_SPEEX_PAY);
341 }