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