1 /* GStreamer
2 * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org>
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 #include "gstrtpelements.h"
29 #include "gstrtpilbcdepay.h"
30 #include "gstrtputils.h"
31
32 /* RtpiLBCDepay signals and args */
33 enum
34 {
35 /* FILL ME */
36 LAST_SIGNAL
37 };
38
39 #define DEFAULT_MODE GST_ILBC_MODE_30
40
41 enum
42 {
43 PROP_0,
44 PROP_MODE
45 };
46
47 /* FIXME, mode should be string because it is a parameter in SDP fmtp */
48 static GstStaticPadTemplate gst_rtp_ilbc_depay_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_PAD_SINK,
51 GST_PAD_ALWAYS,
52 GST_STATIC_CAPS ("application/x-rtp, "
53 "media = (string) \"audio\", "
54 "clock-rate = (int) 8000, " "encoding-name = (string) \"ILBC\"")
55 /* "mode = (string) { \"20\", \"30\" }" */
56 );
57
58 static GstStaticPadTemplate gst_rtp_ilbc_depay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_PAD_SRC,
61 GST_PAD_ALWAYS,
62 GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) { 20, 30 }")
63 );
64
65 static void gst_ilbc_depay_set_property (GObject * object,
66 guint prop_id, const GValue * value, GParamSpec * pspec);
67 static void gst_ilbc_depay_get_property (GObject * object,
68 guint prop_id, GValue * value, GParamSpec * pspec);
69
70 static GstBuffer *gst_rtp_ilbc_depay_process (GstRTPBaseDepayload * depayload,
71 GstRTPBuffer * rtp);
72 static gboolean gst_rtp_ilbc_depay_setcaps (GstRTPBaseDepayload * depayload,
73 GstCaps * caps);
74
75 #define gst_rtp_ilbc_depay_parent_class parent_class
76 G_DEFINE_TYPE (GstRTPiLBCDepay, gst_rtp_ilbc_depay,
77 GST_TYPE_RTP_BASE_DEPAYLOAD);
78 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpilbcdepay, "rtpilbcdepay",
79 GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_DEPAY, rtp_element_init (plugin));
80
81 #define GST_TYPE_ILBC_MODE (gst_ilbc_mode_get_type())
82 static GType
gst_ilbc_mode_get_type(void)83 gst_ilbc_mode_get_type (void)
84 {
85 static GType ilbc_mode_type = 0;
86 static const GEnumValue ilbc_modes[] = {
87 {GST_ILBC_MODE_20, "20ms frames", "20ms"},
88 {GST_ILBC_MODE_30, "30ms frames", "30ms"},
89 {0, NULL, NULL},
90 };
91
92 if (!ilbc_mode_type) {
93 ilbc_mode_type = g_enum_register_static ("iLBCMode", ilbc_modes);
94 }
95 return ilbc_mode_type;
96 }
97
98 static void
gst_rtp_ilbc_depay_class_init(GstRTPiLBCDepayClass * klass)99 gst_rtp_ilbc_depay_class_init (GstRTPiLBCDepayClass * klass)
100 {
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
103 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
104
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
108
109 gobject_class->set_property = gst_ilbc_depay_set_property;
110 gobject_class->get_property = gst_ilbc_depay_get_property;
111
112 /* FIXME, mode is in the caps */
113 g_object_class_install_property (gobject_class, PROP_MODE,
114 g_param_spec_enum ("mode", "Mode", "iLBC frame mode",
115 GST_TYPE_ILBC_MODE, DEFAULT_MODE,
116 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117
118 gst_element_class_add_static_pad_template (gstelement_class,
119 &gst_rtp_ilbc_depay_src_template);
120 gst_element_class_add_static_pad_template (gstelement_class,
121 &gst_rtp_ilbc_depay_sink_template);
122
123 gst_element_class_set_static_metadata (gstelement_class,
124 "RTP iLBC depayloader", "Codec/Depayloader/Network/RTP",
125 "Extracts iLBC audio from RTP packets (RFC 3952)",
126 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
127
128 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_ilbc_depay_process;
129 gstrtpbasedepayload_class->set_caps = gst_rtp_ilbc_depay_setcaps;
130
131 gst_type_mark_as_plugin_api (GST_TYPE_ILBC_MODE, 0);
132 }
133
134 static void
gst_rtp_ilbc_depay_init(GstRTPiLBCDepay * rtpilbcdepay)135 gst_rtp_ilbc_depay_init (GstRTPiLBCDepay * rtpilbcdepay)
136 {
137 /* Set default mode */
138 rtpilbcdepay->mode = DEFAULT_MODE;
139 }
140
141 static gboolean
gst_rtp_ilbc_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)142 gst_rtp_ilbc_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
143 {
144 GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (depayload);
145 GstCaps *srccaps;
146 GstStructure *structure;
147 const gchar *mode_str = NULL;
148 gint mode, clock_rate;
149 gboolean ret;
150
151 structure = gst_caps_get_structure (caps, 0);
152
153 mode = rtpilbcdepay->mode;
154
155 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
156 clock_rate = 8000;
157 depayload->clock_rate = clock_rate;
158
159 /* parse mode, if we can */
160 mode_str = gst_structure_get_string (structure, "mode");
161 if (mode_str) {
162 mode = strtol (mode_str, NULL, 10);
163 if (mode != 20 && mode != 30)
164 mode = rtpilbcdepay->mode;
165 }
166
167 rtpilbcdepay->mode = mode;
168
169 srccaps = gst_caps_new_simple ("audio/x-iLBC",
170 "mode", G_TYPE_INT, rtpilbcdepay->mode, NULL);
171 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
172
173 GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
174 gst_caps_unref (srccaps);
175
176 return ret;
177 }
178
179 static GstBuffer *
gst_rtp_ilbc_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)180 gst_rtp_ilbc_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
181 {
182 GstBuffer *outbuf;
183 gboolean marker;
184
185 marker = gst_rtp_buffer_get_marker (rtp);
186
187 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
188 gst_buffer_get_size (rtp->buffer), marker,
189 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
190
191 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
192
193 if (marker && outbuf) {
194 /* mark start of talkspurt with RESYNC */
195 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
196 }
197
198 if (outbuf) {
199 gst_rtp_drop_non_audio_meta (depayload, outbuf);
200 }
201
202 return outbuf;
203 }
204
205 static void
gst_ilbc_depay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)206 gst_ilbc_depay_set_property (GObject * object,
207 guint prop_id, const GValue * value, GParamSpec * pspec)
208 {
209 GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
210
211 switch (prop_id) {
212 case PROP_MODE:
213 rtpilbcdepay->mode = g_value_get_enum (value);
214 break;
215 default:
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 break;
218 }
219 }
220
221 static void
gst_ilbc_depay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)222 gst_ilbc_depay_get_property (GObject * object,
223 guint prop_id, GValue * value, GParamSpec * pspec)
224 {
225 GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
226
227 switch (prop_id) {
228 case PROP_MODE:
229 g_value_set_enum (value, rtpilbcdepay->mode);
230 break;
231 default:
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 break;
234 }
235 }
236