1 /* GStreamer
2 * Copyright (C) <2007> Wim Taymans <wim.taymans@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
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpg722depay.h"
30 #include "gstrtpchannels.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpg722depay_debug);
34 #define GST_CAT_DEFAULT (rtpg722depay_debug)
35
36 static GstStaticPadTemplate gst_rtp_g722_depay_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src",
38 GST_PAD_SRC,
39 GST_PAD_ALWAYS,
40 GST_STATIC_CAPS ("audio/G722, "
41 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
42 );
43
44 static GstStaticPadTemplate gst_rtp_g722_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\", " "clock-rate = (int) 8000, "
50 /* "channels = (int) [1, MAX]" */
51 /* "channel-order = (string) ANY" */
52 "encoding-name = (string) \"G722\";"
53 "application/x-rtp, "
54 "media = (string) \"audio\", "
55 "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
56 "clock-rate = (int) [ 1, MAX ]"
57 /* "channels = (int) [1, MAX]" */
58 /* "emphasis = (string) ANY" */
59 /* "channel-order = (string) ANY" */
60 )
61 );
62
63 #define gst_rtp_g722_depay_parent_class parent_class
64 G_DEFINE_TYPE (GstRtpG722Depay, gst_rtp_g722_depay,
65 GST_TYPE_RTP_BASE_DEPAYLOAD);
66
67 static gboolean gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload,
68 GstCaps * caps);
69 static GstBuffer *gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload,
70 GstRTPBuffer * rtp);
71
72 static void
gst_rtp_g722_depay_class_init(GstRtpG722DepayClass * klass)73 gst_rtp_g722_depay_class_init (GstRtpG722DepayClass * klass)
74 {
75 GstElementClass *gstelement_class;
76 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
77
78 GST_DEBUG_CATEGORY_INIT (rtpg722depay_debug, "rtpg722depay", 0,
79 "G722 RTP Depayloader");
80
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
83
84 gst_element_class_add_static_pad_template (gstelement_class,
85 &gst_rtp_g722_depay_src_template);
86 gst_element_class_add_static_pad_template (gstelement_class,
87 &gst_rtp_g722_depay_sink_template);
88
89 gst_element_class_set_static_metadata (gstelement_class,
90 "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
91 "Extracts G722 audio from RTP packets",
92 "Wim Taymans <wim.taymans@gmail.com>");
93
94 gstrtpbasedepayload_class->set_caps = gst_rtp_g722_depay_setcaps;
95 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g722_depay_process;
96 }
97
98 static void
gst_rtp_g722_depay_init(GstRtpG722Depay * rtpg722depay)99 gst_rtp_g722_depay_init (GstRtpG722Depay * rtpg722depay)
100 {
101 }
102
103 static gint
gst_rtp_g722_depay_parse_int(GstStructure * structure,const gchar * field,gint def)104 gst_rtp_g722_depay_parse_int (GstStructure * structure, const gchar * field,
105 gint def)
106 {
107 const gchar *str;
108 gint res;
109
110 if ((str = gst_structure_get_string (structure, field)))
111 return atoi (str);
112
113 if (gst_structure_get_int (structure, field, &res))
114 return res;
115
116 return def;
117 }
118
119 static gboolean
gst_rtp_g722_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)120 gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
121 {
122 GstStructure *structure;
123 GstRtpG722Depay *rtpg722depay;
124 gint clock_rate, payload, samplerate;
125 gint channels;
126 GstCaps *srccaps;
127 gboolean res;
128 #if 0
129 const gchar *channel_order;
130 const GstRTPChannelOrder *order;
131 #endif
132
133 rtpg722depay = GST_RTP_G722_DEPAY (depayload);
134
135 structure = gst_caps_get_structure (caps, 0);
136
137 payload = 96;
138 gst_structure_get_int (structure, "payload", &payload);
139 switch (payload) {
140 case GST_RTP_PAYLOAD_G722:
141 channels = 1;
142 clock_rate = 8000;
143 samplerate = 16000;
144 break;
145 default:
146 /* no fixed mapping, we need clock-rate */
147 channels = 0;
148 clock_rate = 0;
149 samplerate = 0;
150 break;
151 }
152
153 /* caps can overwrite defaults */
154 clock_rate =
155 gst_rtp_g722_depay_parse_int (structure, "clock-rate", clock_rate);
156 if (clock_rate == 0)
157 goto no_clockrate;
158
159 if (clock_rate == 8000)
160 samplerate = 16000;
161
162 if (samplerate == 0)
163 samplerate = clock_rate;
164
165 channels =
166 gst_rtp_g722_depay_parse_int (structure, "encoding-params", channels);
167 if (channels == 0) {
168 channels = gst_rtp_g722_depay_parse_int (structure, "channels", channels);
169 if (channels == 0) {
170 /* channels defaults to 1 otherwise */
171 channels = 1;
172 }
173 }
174
175 depayload->clock_rate = clock_rate;
176 rtpg722depay->rate = samplerate;
177 rtpg722depay->channels = channels;
178
179 srccaps = gst_caps_new_simple ("audio/G722",
180 "rate", G_TYPE_INT, samplerate, "channels", G_TYPE_INT, channels, NULL);
181
182 /* FIXME: Do something with the channel order */
183 #if 0
184 /* add channel positions */
185 channel_order = gst_structure_get_string (structure, "channel-order");
186
187 order = gst_rtp_channels_get_by_order (channels, channel_order);
188 if (order) {
189 gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
190 order->pos);
191 } else {
192 GstAudioChannelPosition *pos;
193
194 GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
195 (NULL), ("Unknown channel order '%s' for %d channels",
196 GST_STR_NULL (channel_order), channels));
197 /* create default NONE layout */
198 pos = gst_rtp_channels_create_default (channels);
199 gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
200 g_free (pos);
201 }
202 #endif
203
204 res = gst_pad_set_caps (depayload->srcpad, srccaps);
205 gst_caps_unref (srccaps);
206
207 return res;
208
209 /* ERRORS */
210 no_clockrate:
211 {
212 GST_ERROR_OBJECT (depayload, "no clock-rate specified");
213 return FALSE;
214 }
215 }
216
217 static GstBuffer *
gst_rtp_g722_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)218 gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
219 {
220 GstRtpG722Depay *rtpg722depay;
221 GstBuffer *outbuf;
222 gint payload_len;
223 gboolean marker;
224
225 rtpg722depay = GST_RTP_G722_DEPAY (depayload);
226
227 payload_len = gst_rtp_buffer_get_payload_len (rtp);
228
229 if (payload_len <= 0)
230 goto empty_packet;
231
232 GST_DEBUG_OBJECT (rtpg722depay, "got payload of %d bytes", payload_len);
233
234 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
235 marker = gst_rtp_buffer_get_marker (rtp);
236
237 if (marker && outbuf) {
238 /* mark talk spurt with RESYNC */
239 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
240 }
241
242 if (outbuf) {
243 gst_rtp_drop_non_audio_meta (rtpg722depay, outbuf);
244 }
245
246 return outbuf;
247
248 /* ERRORS */
249 empty_packet:
250 {
251 GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
252 ("Empty Payload."), (NULL));
253 return NULL;
254 }
255 }
256
257 gboolean
gst_rtp_g722_depay_plugin_init(GstPlugin * plugin)258 gst_rtp_g722_depay_plugin_init (GstPlugin * plugin)
259 {
260 return gst_element_register (plugin, "rtpg722depay",
261 GST_RANK_SECONDARY, GST_TYPE_RTP_G722_DEPAY);
262 }
263