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 "gstrtpspeexdepay.h"
31 #include "gstrtputils.h"
33 /* RtpSPEEXDepay signals and args */
45 static GstStaticPadTemplate gst_rtp_speex_depay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "clock-rate = (int) [6000, 48000], "
52 "encoding-name = (string) \"SPEEX\"")
53 /* "encoding-params = (string) \"1\"" */
56 static GstStaticPadTemplate gst_rtp_speex_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS ("audio/x-speex")
63 static GstBuffer *gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
65 static gboolean gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload,
68 G_DEFINE_TYPE (GstRtpSPEEXDepay, gst_rtp_speex_depay,
69 GST_TYPE_RTP_BASE_DEPAYLOAD);
70 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpspeexdepay, "rtpspeexdepay",
71 GST_RANK_SECONDARY, GST_TYPE_RTP_SPEEX_DEPAY, rtp_element_init (plugin));
74 gst_rtp_speex_depay_class_init (GstRtpSPEEXDepayClass * klass)
76 GstElementClass *gstelement_class;
77 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
79 gstelement_class = (GstElementClass *) klass;
80 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
82 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_speex_depay_process;
83 gstrtpbasedepayload_class->set_caps = gst_rtp_speex_depay_setcaps;
85 gst_element_class_add_static_pad_template (gstelement_class,
86 &gst_rtp_speex_depay_src_template);
87 gst_element_class_add_static_pad_template (gstelement_class,
88 &gst_rtp_speex_depay_sink_template);
89 gst_element_class_set_static_metadata (gstelement_class,
90 "RTP Speex depayloader", "Codec/Depayloader/Network/RTP",
91 "Extracts Speex audio from RTP packets",
92 "Edgard Lima <edgard.lima@gmail.com>");
96 gst_rtp_speex_depay_init (GstRtpSPEEXDepay * rtpspeexdepay)
101 gst_rtp_speex_depay_get_mode (gint rate)
105 else if (rate > 12500)
112 * vendor string (len bytes),
113 * user_len 4 (0) bytes LE
115 static const gchar gst_rtp_speex_comment[] =
116 "\045\0\0\0Depayloaded with GStreamer speexdepay\0\0\0\0";
119 gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
121 GstStructure *structure;
122 GstRtpSPEEXDepay *rtpspeexdepay;
123 gint clock_rate, nb_channels;
131 rtpspeexdepay = GST_RTP_SPEEX_DEPAY (depayload);
133 structure = gst_caps_get_structure (caps, 0);
135 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
137 depayload->clock_rate = clock_rate;
139 if (!(params = gst_structure_get_string (structure, "encoding-params")))
142 nb_channels = atoi (params);
145 /* construct minimal header and comment packet for the decoder */
146 buf = gst_buffer_new_and_alloc (80);
147 gst_buffer_map (buf, &map, GST_MAP_WRITE);
149 memcpy (data, "Speex ", 8);
151 memcpy (data, "1.1.12", 7);
153 GST_WRITE_UINT32_LE (data, 1); /* version */
155 GST_WRITE_UINT32_LE (data, 80); /* header_size */
157 GST_WRITE_UINT32_LE (data, clock_rate); /* rate */
159 GST_WRITE_UINT32_LE (data, gst_rtp_speex_depay_get_mode (clock_rate)); /* mode */
161 GST_WRITE_UINT32_LE (data, 4); /* mode_bitstream_version */
163 GST_WRITE_UINT32_LE (data, nb_channels); /* nb_channels */
165 GST_WRITE_UINT32_LE (data, -1); /* bitrate */
167 GST_WRITE_UINT32_LE (data, 0xa0); /* frame_size */
169 GST_WRITE_UINT32_LE (data, 0); /* VBR */
171 GST_WRITE_UINT32_LE (data, 1); /* frames_per_packet */
173 GST_WRITE_UINT32_LE (data, 0); /* extra_headers */
175 GST_WRITE_UINT32_LE (data, 0); /* reserved1 */
177 GST_WRITE_UINT32_LE (data, 0); /* reserved2 */
178 gst_buffer_unmap (buf, &map);
180 srccaps = gst_caps_new_empty_simple ("audio/x-speex");
181 res = gst_pad_set_caps (depayload->srcpad, srccaps);
182 gst_caps_unref (srccaps);
184 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
186 buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_speex_comment));
187 gst_buffer_fill (buf, 0, gst_rtp_speex_comment,
188 sizeof (gst_rtp_speex_comment));
190 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
197 GST_DEBUG_OBJECT (depayload, "no clock-rate specified");
203 gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
206 GstBuffer *outbuf = NULL;
208 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
209 gst_buffer_get_size (rtp->buffer),
210 gst_rtp_buffer_get_marker (rtp),
211 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
213 /* nothing special to be done */
214 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
217 GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
218 gst_rtp_drop_non_audio_meta (depayload, outbuf);