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