2 * Copyright (C) <2005> Edgard Lima <edgard.lima@gmail.com>
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
29 #include "gstrtpelements.h"
30 #include "gstrtpspeexpay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpspeexpay_debug);
34 #define GST_CAT_DEFAULT (rtpspeexpay_debug)
36 static GstStaticPadTemplate gst_rtp_speex_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("audio/x-speex, "
41 "rate = (int) [ 6000, 48000 ], " "channels = (int) 1")
44 static GstStaticPadTemplate gst_rtp_speex_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
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\"")
56 static GstStateChangeReturn gst_rtp_speex_pay_change_state (GstElement *
57 element, GstStateChange transition);
59 static gboolean gst_rtp_speex_pay_setcaps (GstRTPBasePayload * payload,
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);
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));
72 gst_rtp_speex_pay_class_init (GstRtpSPEEXPayClass * klass)
74 GstElementClass *gstelement_class;
75 GstRTPBasePayloadClass *gstrtpbasepayload_class;
77 gstelement_class = (GstElementClass *) klass;
78 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
80 gstelement_class->change_state = gst_rtp_speex_pay_change_state;
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;
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>");
95 GST_DEBUG_CATEGORY_INIT (rtpspeexpay_debug, "rtpspeexpay", 0,
96 "Speex RTP Payloader");
100 gst_rtp_speex_pay_init (GstRtpSPEEXPay * rtpspeexpay)
102 GST_RTP_BASE_PAYLOAD (rtpspeexpay)->clock_rate = 8000;
103 GST_RTP_BASE_PAYLOAD_PT (rtpspeexpay) = 110; /* Create String */
107 gst_rtp_speex_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
109 /* don't configure yet, we wait for the ident packet */
115 gst_rtp_speex_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
118 GstCaps *otherpadcaps;
121 otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
122 caps = gst_pad_get_pad_template_caps (pad);
125 if (!gst_caps_is_empty (otherpadcaps)) {
130 ps = gst_caps_get_structure (otherpadcaps, 0);
131 caps = gst_caps_make_writable (caps);
132 s = gst_caps_get_structure (caps, 0);
134 if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
135 gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
138 gst_caps_unref (otherpadcaps);
142 GstCaps *tcaps = caps;
144 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
145 gst_caps_unref (tcaps);
152 gst_rtp_speex_pay_parse_ident (GstRtpSPEEXPay * rtpspeexpay,
153 const guint8 * data, guint size)
155 guint32 version, header_size, rate, mode, nb_channels;
156 GstRTPBasePayload *payload;
160 /* we need the header string (8), the version string (20), the version
161 * and the header length. */
165 if (!g_str_has_prefix ((const gchar *) data, "Speex "))
168 /* skip header and version string */
171 version = GST_READ_UINT32_LE (data);
177 header_size = GST_READ_UINT32_LE (data);
178 if (header_size < 80)
179 goto header_too_small;
181 if (size < header_size)
182 goto payload_too_small;
185 rate = GST_READ_UINT32_LE (data);
187 mode = GST_READ_UINT32_LE (data);
189 nb_channels = GST_READ_UINT32_LE (data);
191 GST_DEBUG_OBJECT (rtpspeexpay, "rate %d, mode %d, nb_channels %d",
192 rate, mode, nb_channels);
194 payload = GST_RTP_BASE_PAYLOAD (rtpspeexpay);
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);
207 GST_DEBUG_OBJECT (rtpspeexpay,
208 "ident packet too small, need at least 32 bytes");
213 GST_DEBUG_OBJECT (rtpspeexpay,
214 "ident packet does not start with \"Speex \"");
219 GST_DEBUG_OBJECT (rtpspeexpay, "can only handle version 1, have version %d",
225 GST_DEBUG_OBJECT (rtpspeexpay,
226 "header size too small, need at least 80 bytes, " "got only %d",
232 GST_DEBUG_OBJECT (rtpspeexpay,
233 "payload too small, need at least %d bytes, got only %d", header_size,
240 gst_rtp_speex_pay_handle_buffer (GstRTPBasePayload * basepayload,
243 GstRtpSPEEXPay *rtpspeexpay;
246 GstClockTime timestamp, duration;
249 rtpspeexpay = GST_RTP_SPEEX_PAY (basepayload);
251 gst_buffer_map (buffer, &map, GST_MAP_READ);
253 switch (rtpspeexpay->packet) {
255 /* ident packet. We need to parse the headers to construct the RTP
257 if (!gst_rtp_speex_pay_parse_ident (rtpspeexpay, map.data, map.size)) {
258 gst_buffer_unmap (buffer, &map);
263 gst_buffer_unmap (buffer, &map);
266 /* comment packet, we ignore it */
268 gst_buffer_unmap (buffer, &map);
271 /* other packets go in the payload */
274 gst_buffer_unmap (buffer, &map);
276 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP)) {
281 timestamp = GST_BUFFER_PTS (buffer);
282 duration = GST_BUFFER_DURATION (buffer);
284 /* FIXME, only one SPEEX frame per RTP packet for now */
285 outbuf = gst_rtp_base_payload_allocate_output_buffer (basepayload, 0, 0, 0);
287 /* FIXME, assert for now */
288 g_assert (gst_buffer_get_size (buffer) <=
289 GST_RTP_BASE_PAYLOAD_MTU (rtpspeexpay));
291 /* copy timestamp and duration */
292 GST_BUFFER_PTS (outbuf) = timestamp;
293 GST_BUFFER_DURATION (outbuf) = duration;
295 gst_rtp_copy_audio_meta (basepayload, outbuf, buffer);
296 outbuf = gst_buffer_append (outbuf, buffer);
299 ret = gst_rtp_base_payload_push (basepayload, outbuf);
303 gst_buffer_unref (buffer);
305 rtpspeexpay->packet++;
312 GST_ELEMENT_ERROR (rtpspeexpay, STREAM, DECODE, (NULL),
313 ("Error parsing first identification packet."));
314 gst_buffer_unref (buffer);
315 return GST_FLOW_ERROR;
319 static GstStateChangeReturn
320 gst_rtp_speex_pay_change_state (GstElement * element, GstStateChange transition)
322 GstRtpSPEEXPay *rtpspeexpay;
323 GstStateChangeReturn ret;
325 rtpspeexpay = GST_RTP_SPEEX_PAY (element);
327 switch (transition) {
328 case GST_STATE_CHANGE_NULL_TO_READY:
330 case GST_STATE_CHANGE_READY_TO_PAUSED:
331 rtpspeexpay->packet = 0;
337 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
339 switch (transition) {
340 case GST_STATE_CHANGE_READY_TO_NULL: