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