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 <string.h>
25 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpelements.h"
30 #include "gstrtpspeexdepay.h"
31 #include "gstrtputils.h"
32
33 /* RtpSPEEXDepay signals and args */
34 enum
35 {
36 /* FILL ME */
37 LAST_SIGNAL
38 };
39
40 enum
41 {
42 PROP_0
43 };
44
45 static GstStaticPadTemplate gst_rtp_speex_depay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
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\"" */
54 );
55
56 static GstStaticPadTemplate gst_rtp_speex_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_PAD_SRC,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("audio/x-speex")
61 );
62
63 static GstBuffer *gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
64 GstRTPBuffer * rtp);
65 static gboolean gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload,
66 GstCaps * caps);
67
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));
72
73 static void
gst_rtp_speex_depay_class_init(GstRtpSPEEXDepayClass * klass)74 gst_rtp_speex_depay_class_init (GstRtpSPEEXDepayClass * klass)
75 {
76 GstElementClass *gstelement_class;
77 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
78
79 gstelement_class = (GstElementClass *) klass;
80 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
81
82 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_speex_depay_process;
83 gstrtpbasedepayload_class->set_caps = gst_rtp_speex_depay_setcaps;
84
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>");
93 }
94
95 static void
gst_rtp_speex_depay_init(GstRtpSPEEXDepay * rtpspeexdepay)96 gst_rtp_speex_depay_init (GstRtpSPEEXDepay * rtpspeexdepay)
97 {
98 }
99
100 static gint
gst_rtp_speex_depay_get_mode(gint rate)101 gst_rtp_speex_depay_get_mode (gint rate)
102 {
103 if (rate > 25000)
104 return 2;
105 else if (rate > 12500)
106 return 1;
107 else
108 return 0;
109 }
110
111 /* len 4 bytes LE,
112 * vendor string (len bytes),
113 * user_len 4 (0) bytes LE
114 */
115 static const gchar gst_rtp_speex_comment[] =
116 "\045\0\0\0Depayloaded with GStreamer speexdepay\0\0\0\0";
117
118 static gboolean
gst_rtp_speex_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)119 gst_rtp_speex_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
120 {
121 GstStructure *structure;
122 GstRtpSPEEXDepay *rtpspeexdepay;
123 gint clock_rate, nb_channels;
124 GstBuffer *buf;
125 GstMapInfo map;
126 guint8 *data;
127 const gchar *params;
128 GstCaps *srccaps;
129 gboolean res;
130
131 rtpspeexdepay = GST_RTP_SPEEX_DEPAY (depayload);
132
133 structure = gst_caps_get_structure (caps, 0);
134
135 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
136 goto no_clockrate;
137 depayload->clock_rate = clock_rate;
138
139 if (!(params = gst_structure_get_string (structure, "encoding-params")))
140 nb_channels = 1;
141 else {
142 nb_channels = atoi (params);
143 }
144
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);
148 data = map.data;
149 memcpy (data, "Speex ", 8);
150 data += 8;
151 memcpy (data, "1.1.12", 7);
152 data += 20;
153 GST_WRITE_UINT32_LE (data, 1); /* version */
154 data += 4;
155 GST_WRITE_UINT32_LE (data, 80); /* header_size */
156 data += 4;
157 GST_WRITE_UINT32_LE (data, clock_rate); /* rate */
158 data += 4;
159 GST_WRITE_UINT32_LE (data, gst_rtp_speex_depay_get_mode (clock_rate)); /* mode */
160 data += 4;
161 GST_WRITE_UINT32_LE (data, 4); /* mode_bitstream_version */
162 data += 4;
163 GST_WRITE_UINT32_LE (data, nb_channels); /* nb_channels */
164 data += 4;
165 GST_WRITE_UINT32_LE (data, -1); /* bitrate */
166 data += 4;
167 GST_WRITE_UINT32_LE (data, 0xa0); /* frame_size */
168 data += 4;
169 GST_WRITE_UINT32_LE (data, 0); /* VBR */
170 data += 4;
171 GST_WRITE_UINT32_LE (data, 1); /* frames_per_packet */
172 data += 4;
173 GST_WRITE_UINT32_LE (data, 0); /* extra_headers */
174 data += 4;
175 GST_WRITE_UINT32_LE (data, 0); /* reserved1 */
176 data += 4;
177 GST_WRITE_UINT32_LE (data, 0); /* reserved2 */
178 gst_buffer_unmap (buf, &map);
179
180 srccaps = gst_caps_new_empty_simple ("audio/x-speex");
181 res = gst_pad_set_caps (depayload->srcpad, srccaps);
182 gst_caps_unref (srccaps);
183
184 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
185
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));
189
190 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpspeexdepay), buf);
191
192 return res;
193
194 /* ERRORS */
195 no_clockrate:
196 {
197 GST_DEBUG_OBJECT (depayload, "no clock-rate specified");
198 return FALSE;
199 }
200 }
201
202 static GstBuffer *
gst_rtp_speex_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)203 gst_rtp_speex_depay_process (GstRTPBaseDepayload * depayload,
204 GstRTPBuffer * rtp)
205 {
206 GstBuffer *outbuf = NULL;
207
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));
212
213 /* nothing special to be done */
214 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
215
216 if (outbuf) {
217 GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
218 gst_rtp_drop_non_audio_meta (depayload, outbuf);
219 }
220
221 return outbuf;
222 }
223