2 * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpspeexpay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpspeexpay_debug);
31 #define GST_CAT_DEFAULT (rtpspeexpay_debug)
33 static GstStaticPadTemplate gst_rtp_speex_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_STATIC_CAPS ("audio/x-speex, "
38 "rate = (int) [ 6000, 48000 ], " "channels = (int) 1")
41 static GstStaticPadTemplate gst_rtp_speex_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
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\"")
53 static GstStateChangeReturn gst_rtp_speex_pay_change_state (GstElement *
54 element, GstStateChange transition);
56 static gboolean gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload,
58 static GstCaps *gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload,
59 GstPad * pad, GstCaps * filter);
60 static GstFlowReturn gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload *
61 payload, GstBuffer * buffer);
63 #define gst_rtp_speex_pay_parent_class parent_class
64 G_DEFINE_TYPE (GstRtpSPEEXPay, gst_rtp_speex_pay, GST_TYPE_BASE_RTP_PAYLOAD);
67 gst_rtp_speex_pay_class_init (GstRtpSPEEXPayClass * klass)
69 GstElementClass *gstelement_class;
70 GstBaseRTPPayloadClass *gstbasertppayload_class;
72 gstelement_class = (GstElementClass *) klass;
73 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
75 gstelement_class->change_state = gst_rtp_speex_pay_change_state;
77 gstbasertppayload_class->set_caps = gst_rtp_speex_pay_setcaps;
78 gstbasertppayload_class->get_caps = gst_rtp_speex_pay_getcaps;
79 gstbasertppayload_class->handle_buffer = gst_rtp_speex_pay_handle_buffer;
81 gst_element_class_add_pad_template (gstelement_class,
82 gst_static_pad_template_get (&gst_rtp_speex_pay_sink_template));
83 gst_element_class_add_pad_template (gstelement_class,
84 gst_static_pad_template_get (&gst_rtp_speex_pay_src_template));
85 gst_element_class_set_details_simple (gstelement_class, "RTP Speex payloader",
86 "Codec/Payloader/Network/RTP",
87 "Payload-encodes Speex audio into a RTP packet",
88 "Edgard Lima <edgard.lima@indt.org.br>");
90 GST_DEBUG_CATEGORY_INIT (rtpspeexpay_debug, "rtpspeexpay", 0,
91 "Speex RTP Payloader");
95 gst_rtp_speex_pay_init (GstRtpSPEEXPay * rtpspeexpay)
97 GST_BASE_RTP_PAYLOAD (rtpspeexpay)->clock_rate = 8000;
98 GST_BASE_RTP_PAYLOAD_PT (rtpspeexpay) = 110; /* Create String */
102 gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
104 /* don't configure yet, we wait for the ident packet */
110 gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload, GstPad * pad,
113 GstCaps *otherpadcaps;
116 otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
117 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
120 if (!gst_caps_is_empty (otherpadcaps)) {
121 GstStructure *ps = gst_caps_get_structure (otherpadcaps, 0);
122 GstStructure *s = gst_caps_get_structure (caps, 0);
125 if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
126 gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
129 gst_caps_unref (otherpadcaps);
133 GstCaps *tcaps = caps;
135 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
136 gst_caps_unref (tcaps);
143 gst_rtp_speex_pay_parse_ident (GstRtpSPEEXPay * rtpspeexpay,
144 const guint8 * data, guint size)
146 guint32 version, header_size, rate, mode, nb_channels;
147 GstBaseRTPPayload *payload;
151 /* we need the header string (8), the version string (20), the version
152 * and the header length. */
156 if (!g_str_has_prefix ((const gchar *) data, "Speex "))
159 /* skip header and version string */
162 version = GST_READ_UINT32_LE (data);
168 header_size = GST_READ_UINT32_LE (data);
169 if (header_size < 80)
170 goto header_too_small;
172 if (size < header_size)
173 goto payload_too_small;
176 rate = GST_READ_UINT32_LE (data);
178 mode = GST_READ_UINT32_LE (data);
180 nb_channels = GST_READ_UINT32_LE (data);
182 GST_DEBUG_OBJECT (rtpspeexpay, "rate %d, mode %d, nb_channels %d",
183 rate, mode, nb_channels);
185 payload = GST_BASE_RTP_PAYLOAD (rtpspeexpay);
187 gst_basertppayload_set_options (payload, "audio", FALSE, "SPEEX", rate);
188 cstr = g_strdup_printf ("%d", nb_channels);
189 res = gst_basertppayload_set_outcaps (payload, "encoding-params",
190 G_TYPE_STRING, cstr, NULL);
198 GST_DEBUG_OBJECT (rtpspeexpay,
199 "ident packet too small, need at least 32 bytes");
204 GST_DEBUG_OBJECT (rtpspeexpay,
205 "ident packet does not start with \"Speex \"");
210 GST_DEBUG_OBJECT (rtpspeexpay, "can only handle version 1, have version %d",
216 GST_DEBUG_OBJECT (rtpspeexpay,
217 "header size too small, need at least 80 bytes, " "got only %d",
223 GST_DEBUG_OBJECT (rtpspeexpay,
224 "payload too small, need at least %d bytes, got only %d", header_size,
231 gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload * basepayload,
234 GstRtpSPEEXPay *rtpspeexpay;
238 guint8 *payload, *data;
239 GstClockTime timestamp, duration;
243 rtpspeexpay = GST_RTP_SPEEX_PAY (basepayload);
245 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
247 switch (rtpspeexpay->packet) {
249 /* ident packet. We need to parse the headers to construct the RTP
251 if (!gst_rtp_speex_pay_parse_ident (rtpspeexpay, data, size))
257 /* comment packet, we ignore it */
261 /* other packets go in the payload */
265 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP)) {
270 timestamp = GST_BUFFER_TIMESTAMP (buffer);
271 duration = GST_BUFFER_DURATION (buffer);
273 /* FIXME, only one SPEEX frame per RTP packet for now */
276 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
277 /* FIXME, assert for now */
278 g_assert (payload_len <= GST_BASE_RTP_PAYLOAD_MTU (rtpspeexpay));
280 /* copy timestamp and duration */
281 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
282 GST_BUFFER_DURATION (outbuf) = duration;
284 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
286 payload = gst_rtp_buffer_get_payload (&rtp);
288 /* copy data in payload */
289 memcpy (&payload[0], data, size);
291 gst_rtp_buffer_unmap (&rtp);
293 ret = gst_basertppayload_push (basepayload, outbuf);
296 gst_buffer_unmap (buffer, data, -1);
297 gst_buffer_unref (buffer);
299 rtpspeexpay->packet++;
306 GST_ELEMENT_ERROR (rtpspeexpay, STREAM, DECODE, (NULL),
307 ("Error parsing first identification packet."));
308 gst_buffer_unmap (buffer, data, -1);
309 gst_buffer_unref (buffer);
310 return GST_FLOW_ERROR;
314 static GstStateChangeReturn
315 gst_rtp_speex_pay_change_state (GstElement * element, GstStateChange transition)
317 GstRtpSPEEXPay *rtpspeexpay;
318 GstStateChangeReturn ret;
320 rtpspeexpay = GST_RTP_SPEEX_PAY (element);
322 switch (transition) {
323 case GST_STATE_CHANGE_NULL_TO_READY:
325 case GST_STATE_CHANGE_READY_TO_PAUSED:
326 rtpspeexpay->packet = 0;
332 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
334 switch (transition) {
335 case GST_STATE_CHANGE_READY_TO_NULL:
344 gst_rtp_speex_pay_plugin_init (GstPlugin * plugin)
346 return gst_element_register (plugin, "rtpspeexpay",
347 GST_RANK_SECONDARY, GST_TYPE_RTP_SPEEX_PAY);