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., 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 "gstrtpspeexdepay.h"
30 #include "gstrtputils.h"
32 /* RtpSPEEXDepay signals and args */
44 static GstStaticPadTemplate gst_rtp_speex_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) \"audio\", "
50 "clock-rate = (int) [6000, 48000], "
51 "encoding-name = (string) \"SPEEX\"")
52 /* "encoding-params = (string) \"1\"" */
55 static GstStaticPadTemplate gst_rtp_speex_depay_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
59 GST_STATIC_CAPS ("audio/x-speex")
62 static GstBuffer *gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
64 static gboolean gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload,
67 G_DEFINE_TYPE (GstRtpSPEEXDepay, gst_rtp_speex_depay,
68 GST_TYPE_RTP_BASE_DEPAYLOAD);
71 gst_rtp_speex_depay_class_init (GstRtpSPEEXDepayClass * klass)
73 GstElementClass *gstelement_class;
74 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
76 gstelement_class = (GstElementClass *) klass;
77 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
79 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_speex_depay_process;
80 gstrtpbasedepayload_class->set_caps = gst_rtp_speex_depay_setcaps;
82 gst_element_class_add_static_pad_template (gstelement_class,
83 &gst_rtp_speex_depay_src_template);
84 gst_element_class_add_static_pad_template (gstelement_class,
85 &gst_rtp_speex_depay_sink_template);
86 gst_element_class_set_static_metadata (gstelement_class,
87 "RTP Speex depayloader", "Codec/Depayloader/Network/RTP",
88 "Extracts Speex audio from RTP packets",
89 "Edgard Lima <edgard.lima@indt.org.br>");
93 gst_rtp_speex_depay_init (GstRtpSPEEXDepay * rtpspeexdepay)
98 gst_rtp_speex_depay_get_mode (gint rate)
102 else if (rate > 12500)
109 * vendor string (len bytes),
110 * user_len 4 (0) bytes LE
112 static const gchar gst_rtp_speex_comment[] =
113 "\045\0\0\0Depayloaded with GStreamer speexdepay\0\0\0\0";
116 gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
118 GstStructure *structure;
119 GstRtpSPEEXDepay *rtpspeexdepay;
120 gint clock_rate, nb_channels;
128 rtpspeexdepay = GST_RTP_SPEEX_DEPAY (depayload);
130 structure = gst_caps_get_structure (caps, 0);
132 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
134 depayload->clock_rate = clock_rate;
136 if (!(params = gst_structure_get_string (structure, "encoding-params")))
139 nb_channels = atoi (params);
142 /* construct minimal header and comment packet for the decoder */
143 buf = gst_buffer_new_and_alloc (80);
144 gst_buffer_map (buf, &map, GST_MAP_WRITE);
146 memcpy (data, "Speex ", 8);
148 memcpy (data, "1.1.12", 7);
150 GST_WRITE_UINT32_LE (data, 1); /* version */
152 GST_WRITE_UINT32_LE (data, 80); /* header_size */
154 GST_WRITE_UINT32_LE (data, clock_rate); /* rate */
156 GST_WRITE_UINT32_LE (data, gst_rtp_speex_depay_get_mode (clock_rate)); /* mode */
158 GST_WRITE_UINT32_LE (data, 4); /* mode_bitstream_version */
160 GST_WRITE_UINT32_LE (data, nb_channels); /* nb_channels */
162 GST_WRITE_UINT32_LE (data, -1); /* bitrate */
164 GST_WRITE_UINT32_LE (data, 0xa0); /* frame_size */
166 GST_WRITE_UINT32_LE (data, 0); /* VBR */
168 GST_WRITE_UINT32_LE (data, 1); /* frames_per_packet */
170 GST_WRITE_UINT32_LE (data, 0); /* extra_headers */
172 GST_WRITE_UINT32_LE (data, 0); /* reserved1 */
174 GST_WRITE_UINT32_LE (data, 0); /* reserved2 */
175 gst_buffer_unmap (buf, &map);
177 srccaps = gst_caps_new_empty_simple ("audio/x-speex");
178 res = gst_pad_set_caps (depayload->srcpad, srccaps);
179 gst_caps_unref (srccaps);
181 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
183 buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_speex_comment));
184 gst_buffer_fill (buf, 0, gst_rtp_speex_comment,
185 sizeof (gst_rtp_speex_comment));
187 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
194 GST_DEBUG_OBJECT (depayload, "no clock-rate specified");
200 gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
203 GstBuffer *outbuf = NULL;
205 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
206 gst_buffer_get_size (rtp->buffer),
207 gst_rtp_buffer_get_marker (rtp),
208 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
210 /* nothing special to be done */
211 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
214 GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
215 gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload), outbuf,
216 g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
223 gst_rtp_speex_depay_plugin_init (GstPlugin * plugin)
225 return gst_element_register (plugin, "rtpspeexdepay",
226 GST_RANK_SECONDARY, GST_TYPE_RTP_SPEEX_DEPAY);